direct-io.hg

view linux-2.6-xen-sparse/net/core/skbuff.c @ 12351:ac10e3047f36

[LIBFSIMAGE] Don't leak FDs or RAM when closing filesystems
Signed-off-by: Tim Deegan <Tim.Deegan@xensource.com>
author Tim Deegan <Tim.Deegan@xensource.com>
date Fri Nov 10 12:27:56 2006 +0000 (2006-11-10)
parents 6e7027a2abca
children aaaa249e6f3b
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->gso_size = 0;
169 shinfo->gso_segs = 0;
170 shinfo->gso_type = 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->gso_size = 0;
241 shinfo->gso_segs = 0;
242 shinfo->gso_type = 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_data_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)->gso_size = skb_shinfo(old)->gso_size;
528 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
529 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
530 }
532 /**
533 * skb_copy - create private copy of an sk_buff
534 * @skb: buffer to copy
535 * @gfp_mask: allocation priority
536 *
537 * Make a copy of both an &sk_buff and its data. This is used when the
538 * caller wishes to modify the data and needs a private copy of the
539 * data to alter. Returns %NULL on failure or the pointer to the buffer
540 * on success. The returned buffer has a reference count of 1.
541 *
542 * As by-product this function converts non-linear &sk_buff to linear
543 * one, so that &sk_buff becomes completely private and caller is allowed
544 * to modify all the data of returned buffer. This means that this
545 * function is not recommended for use in circumstances when only
546 * header is going to be modified. Use pskb_copy() instead.
547 */
549 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
550 {
551 int headerlen = skb->data - skb->head;
552 /*
553 * Allocate the copy buffer
554 */
555 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
556 gfp_mask);
557 if (!n)
558 return NULL;
560 /* Set the data pointer */
561 skb_reserve(n, headerlen);
562 /* Set the tail pointer and length */
563 skb_put(n, skb->len);
564 n->csum = skb->csum;
565 n->ip_summed = skb->ip_summed;
567 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
568 BUG();
570 copy_skb_header(n, skb);
571 return n;
572 }
575 /**
576 * pskb_copy - create copy of an sk_buff with private head.
577 * @skb: buffer to copy
578 * @gfp_mask: allocation priority
579 *
580 * Make a copy of both an &sk_buff and part of its data, located
581 * in header. Fragmented data remain shared. This is used when
582 * the caller wishes to modify only header of &sk_buff and needs
583 * private copy of the header to alter. Returns %NULL on failure
584 * or the pointer to the buffer on success.
585 * The returned buffer has a reference count of 1.
586 */
588 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
589 {
590 /*
591 * Allocate the copy buffer
592 */
593 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
595 if (!n)
596 goto out;
598 /* Set the data pointer */
599 skb_reserve(n, skb->data - skb->head);
600 /* Set the tail pointer and length */
601 skb_put(n, skb_headlen(skb));
602 /* Copy the bytes */
603 memcpy(n->data, skb->data, n->len);
604 n->csum = skb->csum;
605 n->ip_summed = skb->ip_summed;
607 n->data_len = skb->data_len;
608 n->len = skb->len;
610 if (skb_shinfo(skb)->nr_frags) {
611 int i;
613 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
614 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
615 get_page(skb_shinfo(n)->frags[i].page);
616 }
617 skb_shinfo(n)->nr_frags = i;
618 }
620 if (skb_shinfo(skb)->frag_list) {
621 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
622 skb_clone_fraglist(n);
623 }
625 copy_skb_header(n, skb);
626 out:
627 return n;
628 }
630 /**
631 * pskb_expand_head - reallocate header of &sk_buff
632 * @skb: buffer to reallocate
633 * @nhead: room to add at head
634 * @ntail: room to add at tail
635 * @gfp_mask: allocation priority
636 *
637 * Expands (or creates identical copy, if &nhead and &ntail are zero)
638 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
639 * reference count of 1. Returns zero in the case of success or error,
640 * if expansion failed. In the last case, &sk_buff is not changed.
641 *
642 * All the pointers pointing into skb header may change and must be
643 * reloaded after call to this function.
644 */
646 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
647 gfp_t gfp_mask)
648 {
649 int i;
650 u8 *data;
651 int size = nhead + (skb->end - skb->head) + ntail;
652 long off;
654 if (skb_shared(skb))
655 BUG();
657 size = SKB_DATA_ALIGN(size);
659 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
660 if (!data)
661 goto nodata;
663 /* Copy only real data... and, alas, header. This should be
664 * optimized for the cases when header is void. */
665 memcpy(data + nhead, skb->head, skb->tail - skb->head);
666 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
668 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
669 get_page(skb_shinfo(skb)->frags[i].page);
671 if (skb_shinfo(skb)->frag_list)
672 skb_clone_fraglist(skb);
674 skb_release_data(skb);
676 off = (data + nhead) - skb->head;
678 skb->head = data;
679 skb->end = data + size;
680 skb->data += off;
681 skb->tail += off;
682 skb->mac.raw += off;
683 skb->h.raw += off;
684 skb->nh.raw += off;
685 skb->cloned = 0;
686 skb->nohdr = 0;
687 atomic_set(&skb_shinfo(skb)->dataref, 1);
688 return 0;
690 nodata:
691 return -ENOMEM;
692 }
694 /* Make private copy of skb with writable head and some headroom */
696 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
697 {
698 struct sk_buff *skb2;
699 int delta = headroom - skb_headroom(skb);
701 if (delta <= 0)
702 skb2 = pskb_copy(skb, GFP_ATOMIC);
703 else {
704 skb2 = skb_clone(skb, GFP_ATOMIC);
705 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
706 GFP_ATOMIC)) {
707 kfree_skb(skb2);
708 skb2 = NULL;
709 }
710 }
711 return skb2;
712 }
715 /**
716 * skb_copy_expand - copy and expand sk_buff
717 * @skb: buffer to copy
718 * @newheadroom: new free bytes at head
719 * @newtailroom: new free bytes at tail
720 * @gfp_mask: allocation priority
721 *
722 * Make a copy of both an &sk_buff and its data and while doing so
723 * allocate additional space.
724 *
725 * This is used when the caller wishes to modify the data and needs a
726 * private copy of the data to alter as well as more space for new fields.
727 * Returns %NULL on failure or the pointer to the buffer
728 * on success. The returned buffer has a reference count of 1.
729 *
730 * You must pass %GFP_ATOMIC as the allocation priority if this function
731 * is called from an interrupt.
732 *
733 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
734 * only by netfilter in the cases when checksum is recalculated? --ANK
735 */
736 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
737 int newheadroom, int newtailroom,
738 gfp_t gfp_mask)
739 {
740 /*
741 * Allocate the copy buffer
742 */
743 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
744 gfp_mask);
745 int head_copy_len, head_copy_off;
747 if (!n)
748 return NULL;
750 skb_reserve(n, newheadroom);
752 /* Set the tail pointer and length */
753 skb_put(n, skb->len);
755 head_copy_len = skb_headroom(skb);
756 head_copy_off = 0;
757 if (newheadroom <= head_copy_len)
758 head_copy_len = newheadroom;
759 else
760 head_copy_off = newheadroom - head_copy_len;
762 /* Copy the linear header and data. */
763 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
764 skb->len + head_copy_len))
765 BUG();
767 copy_skb_header(n, skb);
769 return n;
770 }
772 /**
773 * skb_pad - zero pad the tail of an skb
774 * @skb: buffer to pad
775 * @pad: space to pad
776 *
777 * Ensure that a buffer is followed by a padding area that is zero
778 * filled. Used by network drivers which may DMA or transfer data
779 * beyond the buffer end onto the wire.
780 *
781 * May return NULL in out of memory cases.
782 */
784 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
785 {
786 struct sk_buff *nskb;
788 /* If the skbuff is non linear tailroom is always zero.. */
789 if (skb_tailroom(skb) >= pad) {
790 memset(skb->data+skb->len, 0, pad);
791 return skb;
792 }
794 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
795 kfree_skb(skb);
796 if (nskb)
797 memset(nskb->data+nskb->len, 0, pad);
798 return nskb;
799 }
801 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
802 * If realloc==0 and trimming is impossible without change of data,
803 * it is BUG().
804 */
806 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
807 {
808 int offset = skb_headlen(skb);
809 int nfrags = skb_shinfo(skb)->nr_frags;
810 int i;
812 for (i = 0; i < nfrags; i++) {
813 int end = offset + skb_shinfo(skb)->frags[i].size;
814 if (end > len) {
815 if (skb_cloned(skb)) {
816 BUG_ON(!realloc);
817 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
818 return -ENOMEM;
819 }
820 if (len <= offset) {
821 put_page(skb_shinfo(skb)->frags[i].page);
822 skb_shinfo(skb)->nr_frags--;
823 } else {
824 skb_shinfo(skb)->frags[i].size = len - offset;
825 }
826 }
827 offset = end;
828 }
830 if (offset < len) {
831 skb->data_len -= skb->len - len;
832 skb->len = len;
833 } else {
834 if (len <= skb_headlen(skb)) {
835 skb->len = len;
836 skb->data_len = 0;
837 skb->tail = skb->data + len;
838 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
839 skb_drop_fraglist(skb);
840 } else {
841 skb->data_len -= skb->len - len;
842 skb->len = len;
843 }
844 }
846 return 0;
847 }
849 /**
850 * __pskb_pull_tail - advance tail of skb header
851 * @skb: buffer to reallocate
852 * @delta: number of bytes to advance tail
853 *
854 * The function makes a sense only on a fragmented &sk_buff,
855 * it expands header moving its tail forward and copying necessary
856 * data from fragmented part.
857 *
858 * &sk_buff MUST have reference count of 1.
859 *
860 * Returns %NULL (and &sk_buff does not change) if pull failed
861 * or value of new tail of skb in the case of success.
862 *
863 * All the pointers pointing into skb header may change and must be
864 * reloaded after call to this function.
865 */
867 /* Moves tail of skb head forward, copying data from fragmented part,
868 * when it is necessary.
869 * 1. It may fail due to malloc failure.
870 * 2. It may change skb pointers.
871 *
872 * It is pretty complicated. Luckily, it is called only in exceptional cases.
873 */
874 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
875 {
876 /* If skb has not enough free space at tail, get new one
877 * plus 128 bytes for future expansions. If we have enough
878 * room at tail, reallocate without expansion only if skb is cloned.
879 */
880 int i, k, eat = (skb->tail + delta) - skb->end;
882 if (eat > 0 || skb_cloned(skb)) {
883 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
884 GFP_ATOMIC))
885 return NULL;
886 }
888 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
889 BUG();
891 /* Optimization: no fragments, no reasons to preestimate
892 * size of pulled pages. Superb.
893 */
894 if (!skb_shinfo(skb)->frag_list)
895 goto pull_pages;
897 /* Estimate size of pulled pages. */
898 eat = delta;
899 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
900 if (skb_shinfo(skb)->frags[i].size >= eat)
901 goto pull_pages;
902 eat -= skb_shinfo(skb)->frags[i].size;
903 }
905 /* If we need update frag list, we are in troubles.
906 * Certainly, it possible to add an offset to skb data,
907 * but taking into account that pulling is expected to
908 * be very rare operation, it is worth to fight against
909 * further bloating skb head and crucify ourselves here instead.
910 * Pure masohism, indeed. 8)8)
911 */
912 if (eat) {
913 struct sk_buff *list = skb_shinfo(skb)->frag_list;
914 struct sk_buff *clone = NULL;
915 struct sk_buff *insp = NULL;
917 do {
918 BUG_ON(!list);
920 if (list->len <= eat) {
921 /* Eaten as whole. */
922 eat -= list->len;
923 list = list->next;
924 insp = list;
925 } else {
926 /* Eaten partially. */
928 if (skb_shared(list)) {
929 /* Sucks! We need to fork list. :-( */
930 clone = skb_clone(list, GFP_ATOMIC);
931 if (!clone)
932 return NULL;
933 insp = list->next;
934 list = clone;
935 } else {
936 /* This may be pulled without
937 * problems. */
938 insp = list;
939 }
940 if (!pskb_pull(list, eat)) {
941 if (clone)
942 kfree_skb(clone);
943 return NULL;
944 }
945 break;
946 }
947 } while (eat);
949 /* Free pulled out fragments. */
950 while ((list = skb_shinfo(skb)->frag_list) != insp) {
951 skb_shinfo(skb)->frag_list = list->next;
952 kfree_skb(list);
953 }
954 /* And insert new clone at head. */
955 if (clone) {
956 clone->next = list;
957 skb_shinfo(skb)->frag_list = clone;
958 }
959 }
960 /* Success! Now we may commit changes to skb data. */
962 pull_pages:
963 eat = delta;
964 k = 0;
965 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
966 if (skb_shinfo(skb)->frags[i].size <= eat) {
967 put_page(skb_shinfo(skb)->frags[i].page);
968 eat -= skb_shinfo(skb)->frags[i].size;
969 } else {
970 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
971 if (eat) {
972 skb_shinfo(skb)->frags[k].page_offset += eat;
973 skb_shinfo(skb)->frags[k].size -= eat;
974 eat = 0;
975 }
976 k++;
977 }
978 }
979 skb_shinfo(skb)->nr_frags = k;
981 skb->tail += delta;
982 skb->data_len -= delta;
984 return skb->tail;
985 }
987 /* Copy some data bits from skb to kernel buffer. */
989 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
990 {
991 int i, copy;
992 int start = skb_headlen(skb);
994 if (offset > (int)skb->len - len)
995 goto fault;
997 /* Copy header. */
998 if ((copy = start - offset) > 0) {
999 if (copy > len)
1000 copy = len;
1001 memcpy(to, skb->data + offset, copy);
1002 if ((len -= copy) == 0)
1003 return 0;
1004 offset += copy;
1005 to += copy;
1008 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1009 int end;
1011 BUG_TRAP(start <= offset + len);
1013 end = start + skb_shinfo(skb)->frags[i].size;
1014 if ((copy = end - offset) > 0) {
1015 u8 *vaddr;
1017 if (copy > len)
1018 copy = len;
1020 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1021 memcpy(to,
1022 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1023 offset - start, copy);
1024 kunmap_skb_frag(vaddr);
1026 if ((len -= copy) == 0)
1027 return 0;
1028 offset += copy;
1029 to += copy;
1031 start = end;
1034 if (skb_shinfo(skb)->frag_list) {
1035 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1037 for (; list; list = list->next) {
1038 int end;
1040 BUG_TRAP(start <= offset + len);
1042 end = start + list->len;
1043 if ((copy = end - offset) > 0) {
1044 if (copy > len)
1045 copy = len;
1046 if (skb_copy_bits(list, offset - start,
1047 to, copy))
1048 goto fault;
1049 if ((len -= copy) == 0)
1050 return 0;
1051 offset += copy;
1052 to += copy;
1054 start = end;
1057 if (!len)
1058 return 0;
1060 fault:
1061 return -EFAULT;
1064 /**
1065 * skb_store_bits - store bits from kernel buffer to skb
1066 * @skb: destination buffer
1067 * @offset: offset in destination
1068 * @from: source buffer
1069 * @len: number of bytes to copy
1071 * Copy the specified number of bytes from the source buffer to the
1072 * destination skb. This function handles all the messy bits of
1073 * traversing fragment lists and such.
1074 */
1076 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1078 int i, copy;
1079 int start = skb_headlen(skb);
1081 if (offset > (int)skb->len - len)
1082 goto fault;
1084 if ((copy = start - offset) > 0) {
1085 if (copy > len)
1086 copy = len;
1087 memcpy(skb->data + offset, from, copy);
1088 if ((len -= copy) == 0)
1089 return 0;
1090 offset += copy;
1091 from += copy;
1094 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1095 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1096 int end;
1098 BUG_TRAP(start <= offset + len);
1100 end = start + frag->size;
1101 if ((copy = end - offset) > 0) {
1102 u8 *vaddr;
1104 if (copy > len)
1105 copy = len;
1107 vaddr = kmap_skb_frag(frag);
1108 memcpy(vaddr + frag->page_offset + offset - start,
1109 from, copy);
1110 kunmap_skb_frag(vaddr);
1112 if ((len -= copy) == 0)
1113 return 0;
1114 offset += copy;
1115 from += copy;
1117 start = end;
1120 if (skb_shinfo(skb)->frag_list) {
1121 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1123 for (; list; list = list->next) {
1124 int end;
1126 BUG_TRAP(start <= offset + len);
1128 end = start + list->len;
1129 if ((copy = end - offset) > 0) {
1130 if (copy > len)
1131 copy = len;
1132 if (skb_store_bits(list, offset - start,
1133 from, copy))
1134 goto fault;
1135 if ((len -= copy) == 0)
1136 return 0;
1137 offset += copy;
1138 from += copy;
1140 start = end;
1143 if (!len)
1144 return 0;
1146 fault:
1147 return -EFAULT;
1150 EXPORT_SYMBOL(skb_store_bits);
1152 /* Checksum skb data. */
1154 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1155 int len, unsigned int csum)
1157 int start = skb_headlen(skb);
1158 int i, copy = start - offset;
1159 int pos = 0;
1161 /* Checksum header. */
1162 if (copy > 0) {
1163 if (copy > len)
1164 copy = len;
1165 csum = csum_partial(skb->data + offset, copy, csum);
1166 if ((len -= copy) == 0)
1167 return csum;
1168 offset += copy;
1169 pos = copy;
1172 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1173 int end;
1175 BUG_TRAP(start <= offset + len);
1177 end = start + skb_shinfo(skb)->frags[i].size;
1178 if ((copy = end - offset) > 0) {
1179 unsigned int csum2;
1180 u8 *vaddr;
1181 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1183 if (copy > len)
1184 copy = len;
1185 vaddr = kmap_skb_frag(frag);
1186 csum2 = csum_partial(vaddr + frag->page_offset +
1187 offset - start, copy, 0);
1188 kunmap_skb_frag(vaddr);
1189 csum = csum_block_add(csum, csum2, pos);
1190 if (!(len -= copy))
1191 return csum;
1192 offset += copy;
1193 pos += copy;
1195 start = end;
1198 if (skb_shinfo(skb)->frag_list) {
1199 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1201 for (; list; list = list->next) {
1202 int end;
1204 BUG_TRAP(start <= offset + len);
1206 end = start + list->len;
1207 if ((copy = end - offset) > 0) {
1208 unsigned int csum2;
1209 if (copy > len)
1210 copy = len;
1211 csum2 = skb_checksum(list, offset - start,
1212 copy, 0);
1213 csum = csum_block_add(csum, csum2, pos);
1214 if ((len -= copy) == 0)
1215 return csum;
1216 offset += copy;
1217 pos += copy;
1219 start = end;
1222 BUG_ON(len);
1224 return csum;
1227 /* Both of above in one bottle. */
1229 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1230 u8 *to, int len, unsigned int csum)
1232 int start = skb_headlen(skb);
1233 int i, copy = start - offset;
1234 int pos = 0;
1236 /* Copy header. */
1237 if (copy > 0) {
1238 if (copy > len)
1239 copy = len;
1240 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1241 copy, csum);
1242 if ((len -= copy) == 0)
1243 return csum;
1244 offset += copy;
1245 to += copy;
1246 pos = copy;
1249 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1250 int end;
1252 BUG_TRAP(start <= offset + len);
1254 end = start + skb_shinfo(skb)->frags[i].size;
1255 if ((copy = end - offset) > 0) {
1256 unsigned int csum2;
1257 u8 *vaddr;
1258 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1260 if (copy > len)
1261 copy = len;
1262 vaddr = kmap_skb_frag(frag);
1263 csum2 = csum_partial_copy_nocheck(vaddr +
1264 frag->page_offset +
1265 offset - start, to,
1266 copy, 0);
1267 kunmap_skb_frag(vaddr);
1268 csum = csum_block_add(csum, csum2, pos);
1269 if (!(len -= copy))
1270 return csum;
1271 offset += copy;
1272 to += copy;
1273 pos += copy;
1275 start = end;
1278 if (skb_shinfo(skb)->frag_list) {
1279 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1281 for (; list; list = list->next) {
1282 unsigned int csum2;
1283 int end;
1285 BUG_TRAP(start <= offset + len);
1287 end = start + list->len;
1288 if ((copy = end - offset) > 0) {
1289 if (copy > len)
1290 copy = len;
1291 csum2 = skb_copy_and_csum_bits(list,
1292 offset - start,
1293 to, copy, 0);
1294 csum = csum_block_add(csum, csum2, pos);
1295 if ((len -= copy) == 0)
1296 return csum;
1297 offset += copy;
1298 to += copy;
1299 pos += copy;
1301 start = end;
1304 BUG_ON(len);
1305 return csum;
1308 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1310 unsigned int csum;
1311 long csstart;
1313 if (skb->ip_summed == CHECKSUM_HW)
1314 csstart = skb->h.raw - skb->data;
1315 else
1316 csstart = skb_headlen(skb);
1318 BUG_ON(csstart > skb_headlen(skb));
1320 memcpy(to, skb->data, csstart);
1322 csum = 0;
1323 if (csstart != skb->len)
1324 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1325 skb->len - csstart, 0);
1327 if (skb->ip_summed == CHECKSUM_HW) {
1328 long csstuff = csstart + skb->csum;
1330 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1334 /**
1335 * skb_dequeue - remove from the head of the queue
1336 * @list: list to dequeue from
1338 * Remove the head of the list. The list lock is taken so the function
1339 * may be used safely with other locking list functions. The head item is
1340 * returned or %NULL if the list is empty.
1341 */
1343 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1345 unsigned long flags;
1346 struct sk_buff *result;
1348 spin_lock_irqsave(&list->lock, flags);
1349 result = __skb_dequeue(list);
1350 spin_unlock_irqrestore(&list->lock, flags);
1351 return result;
1354 /**
1355 * skb_dequeue_tail - remove from the tail of the queue
1356 * @list: list to dequeue from
1358 * Remove the tail of the list. The list lock is taken so the function
1359 * may be used safely with other locking list functions. The tail item is
1360 * returned or %NULL if the list is empty.
1361 */
1362 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1364 unsigned long flags;
1365 struct sk_buff *result;
1367 spin_lock_irqsave(&list->lock, flags);
1368 result = __skb_dequeue_tail(list);
1369 spin_unlock_irqrestore(&list->lock, flags);
1370 return result;
1373 /**
1374 * skb_queue_purge - empty a list
1375 * @list: list to empty
1377 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1378 * the list and one reference dropped. This function takes the list
1379 * lock and is atomic with respect to other list locking functions.
1380 */
1381 void skb_queue_purge(struct sk_buff_head *list)
1383 struct sk_buff *skb;
1384 while ((skb = skb_dequeue(list)) != NULL)
1385 kfree_skb(skb);
1388 /**
1389 * skb_queue_head - queue a buffer at the list head
1390 * @list: list to use
1391 * @newsk: buffer to queue
1393 * Queue a buffer at the start of the list. This function takes the
1394 * list lock and can be used safely with other locking &sk_buff functions
1395 * safely.
1397 * A buffer cannot be placed on two lists at the same time.
1398 */
1399 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1401 unsigned long flags;
1403 spin_lock_irqsave(&list->lock, flags);
1404 __skb_queue_head(list, newsk);
1405 spin_unlock_irqrestore(&list->lock, flags);
1408 /**
1409 * skb_queue_tail - queue a buffer at the list tail
1410 * @list: list to use
1411 * @newsk: buffer to queue
1413 * Queue a buffer at the tail of the list. This function takes the
1414 * list lock and can be used safely with other locking &sk_buff functions
1415 * safely.
1417 * A buffer cannot be placed on two lists at the same time.
1418 */
1419 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1421 unsigned long flags;
1423 spin_lock_irqsave(&list->lock, flags);
1424 __skb_queue_tail(list, newsk);
1425 spin_unlock_irqrestore(&list->lock, flags);
1428 /**
1429 * skb_unlink - remove a buffer from a list
1430 * @skb: buffer to remove
1431 * @list: list to use
1433 * Remove a packet from a list. The list locks are taken and this
1434 * function is atomic with respect to other list locked calls
1436 * You must know what list the SKB is on.
1437 */
1438 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1440 unsigned long flags;
1442 spin_lock_irqsave(&list->lock, flags);
1443 __skb_unlink(skb, list);
1444 spin_unlock_irqrestore(&list->lock, flags);
1447 /**
1448 * skb_append - append a buffer
1449 * @old: buffer to insert after
1450 * @newsk: buffer to insert
1451 * @list: list to use
1453 * Place a packet after a given packet in a list. The list locks are taken
1454 * and this function is atomic with respect to other list locked calls.
1455 * A buffer cannot be placed on two lists at the same time.
1456 */
1457 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1459 unsigned long flags;
1461 spin_lock_irqsave(&list->lock, flags);
1462 __skb_append(old, newsk, list);
1463 spin_unlock_irqrestore(&list->lock, flags);
1467 /**
1468 * skb_insert - insert a buffer
1469 * @old: buffer to insert before
1470 * @newsk: buffer to insert
1471 * @list: list to use
1473 * Place a packet before a given packet in a list. The list locks are
1474 * taken and this function is atomic with respect to other list locked
1475 * calls.
1477 * A buffer cannot be placed on two lists at the same time.
1478 */
1479 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1481 unsigned long flags;
1483 spin_lock_irqsave(&list->lock, flags);
1484 __skb_insert(newsk, old->prev, old, list);
1485 spin_unlock_irqrestore(&list->lock, flags);
1488 #if 0
1489 /*
1490 * Tune the memory allocator for a new MTU size.
1491 */
1492 void skb_add_mtu(int mtu)
1494 /* Must match allocation in alloc_skb */
1495 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1497 kmem_add_cache_size(mtu);
1499 #endif
1501 static inline void skb_split_inside_header(struct sk_buff *skb,
1502 struct sk_buff* skb1,
1503 const u32 len, const int pos)
1505 int i;
1507 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1509 /* And move data appendix as is. */
1510 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1511 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1513 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1514 skb_shinfo(skb)->nr_frags = 0;
1515 skb1->data_len = skb->data_len;
1516 skb1->len += skb1->data_len;
1517 skb->data_len = 0;
1518 skb->len = len;
1519 skb->tail = skb->data + len;
1522 static inline void skb_split_no_header(struct sk_buff *skb,
1523 struct sk_buff* skb1,
1524 const u32 len, int pos)
1526 int i, k = 0;
1527 const int nfrags = skb_shinfo(skb)->nr_frags;
1529 skb_shinfo(skb)->nr_frags = 0;
1530 skb1->len = skb1->data_len = skb->len - len;
1531 skb->len = len;
1532 skb->data_len = len - pos;
1534 for (i = 0; i < nfrags; i++) {
1535 int size = skb_shinfo(skb)->frags[i].size;
1537 if (pos + size > len) {
1538 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1540 if (pos < len) {
1541 /* Split frag.
1542 * We have two variants in this case:
1543 * 1. Move all the frag to the second
1544 * part, if it is possible. F.e.
1545 * this approach is mandatory for TUX,
1546 * where splitting is expensive.
1547 * 2. Split is accurately. We make this.
1548 */
1549 get_page(skb_shinfo(skb)->frags[i].page);
1550 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1551 skb_shinfo(skb1)->frags[0].size -= len - pos;
1552 skb_shinfo(skb)->frags[i].size = len - pos;
1553 skb_shinfo(skb)->nr_frags++;
1555 k++;
1556 } else
1557 skb_shinfo(skb)->nr_frags++;
1558 pos += size;
1560 skb_shinfo(skb1)->nr_frags = k;
1563 /**
1564 * skb_split - Split fragmented skb to two parts at length len.
1565 * @skb: the buffer to split
1566 * @skb1: the buffer to receive the second part
1567 * @len: new length for skb
1568 */
1569 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1571 int pos = skb_headlen(skb);
1573 if (len < pos) /* Split line is inside header. */
1574 skb_split_inside_header(skb, skb1, len, pos);
1575 else /* Second chunk has no header, nothing to copy. */
1576 skb_split_no_header(skb, skb1, len, pos);
1579 /**
1580 * skb_prepare_seq_read - Prepare a sequential read of skb data
1581 * @skb: the buffer to read
1582 * @from: lower offset of data to be read
1583 * @to: upper offset of data to be read
1584 * @st: state variable
1586 * Initializes the specified state variable. Must be called before
1587 * invoking skb_seq_read() for the first time.
1588 */
1589 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1590 unsigned int to, struct skb_seq_state *st)
1592 st->lower_offset = from;
1593 st->upper_offset = to;
1594 st->root_skb = st->cur_skb = skb;
1595 st->frag_idx = st->stepped_offset = 0;
1596 st->frag_data = NULL;
1599 /**
1600 * skb_seq_read - Sequentially read skb data
1601 * @consumed: number of bytes consumed by the caller so far
1602 * @data: destination pointer for data to be returned
1603 * @st: state variable
1605 * Reads a block of skb data at &consumed relative to the
1606 * lower offset specified to skb_prepare_seq_read(). Assigns
1607 * the head of the data block to &data and returns the length
1608 * of the block or 0 if the end of the skb data or the upper
1609 * offset has been reached.
1611 * The caller is not required to consume all of the data
1612 * returned, i.e. &consumed is typically set to the number
1613 * of bytes already consumed and the next call to
1614 * skb_seq_read() will return the remaining part of the block.
1616 * Note: The size of each block of data returned can be arbitary,
1617 * this limitation is the cost for zerocopy seqeuental
1618 * reads of potentially non linear data.
1620 * Note: Fragment lists within fragments are not implemented
1621 * at the moment, state->root_skb could be replaced with
1622 * a stack for this purpose.
1623 */
1624 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1625 struct skb_seq_state *st)
1627 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1628 skb_frag_t *frag;
1630 if (unlikely(abs_offset >= st->upper_offset))
1631 return 0;
1633 next_skb:
1634 block_limit = skb_headlen(st->cur_skb);
1636 if (abs_offset < block_limit) {
1637 *data = st->cur_skb->data + abs_offset;
1638 return block_limit - abs_offset;
1641 if (st->frag_idx == 0 && !st->frag_data)
1642 st->stepped_offset += skb_headlen(st->cur_skb);
1644 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1645 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1646 block_limit = frag->size + st->stepped_offset;
1648 if (abs_offset < block_limit) {
1649 if (!st->frag_data)
1650 st->frag_data = kmap_skb_frag(frag);
1652 *data = (u8 *) st->frag_data + frag->page_offset +
1653 (abs_offset - st->stepped_offset);
1655 return block_limit - abs_offset;
1658 if (st->frag_data) {
1659 kunmap_skb_frag(st->frag_data);
1660 st->frag_data = NULL;
1663 st->frag_idx++;
1664 st->stepped_offset += frag->size;
1667 if (st->cur_skb->next) {
1668 st->cur_skb = st->cur_skb->next;
1669 st->frag_idx = 0;
1670 goto next_skb;
1671 } else if (st->root_skb == st->cur_skb &&
1672 skb_shinfo(st->root_skb)->frag_list) {
1673 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1674 goto next_skb;
1677 return 0;
1680 /**
1681 * skb_abort_seq_read - Abort a sequential read of skb data
1682 * @st: state variable
1684 * Must be called if skb_seq_read() was not called until it
1685 * returned 0.
1686 */
1687 void skb_abort_seq_read(struct skb_seq_state *st)
1689 if (st->frag_data)
1690 kunmap_skb_frag(st->frag_data);
1693 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1695 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1696 struct ts_config *conf,
1697 struct ts_state *state)
1699 return skb_seq_read(offset, text, TS_SKB_CB(state));
1702 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1704 skb_abort_seq_read(TS_SKB_CB(state));
1707 /**
1708 * skb_find_text - Find a text pattern in skb data
1709 * @skb: the buffer to look in
1710 * @from: search offset
1711 * @to: search limit
1712 * @config: textsearch configuration
1713 * @state: uninitialized textsearch state variable
1715 * Finds a pattern in the skb data according to the specified
1716 * textsearch configuration. Use textsearch_next() to retrieve
1717 * subsequent occurrences of the pattern. Returns the offset
1718 * to the first occurrence or UINT_MAX if no match was found.
1719 */
1720 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1721 unsigned int to, struct ts_config *config,
1722 struct ts_state *state)
1724 config->get_next_block = skb_ts_get_next_block;
1725 config->finish = skb_ts_finish;
1727 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1729 return textsearch_find(config, state);
1732 /**
1733 * skb_append_datato_frags: - append the user data to a skb
1734 * @sk: sock structure
1735 * @skb: skb structure to be appened with user data.
1736 * @getfrag: call back function to be used for getting the user data
1737 * @from: pointer to user message iov
1738 * @length: length of the iov message
1740 * Description: This procedure append the user data in the fragment part
1741 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1742 */
1743 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1744 int (*getfrag)(void *from, char *to, int offset,
1745 int len, int odd, struct sk_buff *skb),
1746 void *from, int length)
1748 int frg_cnt = 0;
1749 skb_frag_t *frag = NULL;
1750 struct page *page = NULL;
1751 int copy, left;
1752 int offset = 0;
1753 int ret;
1755 do {
1756 /* Return error if we don't have space for new frag */
1757 frg_cnt = skb_shinfo(skb)->nr_frags;
1758 if (frg_cnt >= MAX_SKB_FRAGS)
1759 return -EFAULT;
1761 /* allocate a new page for next frag */
1762 page = alloc_pages(sk->sk_allocation, 0);
1764 /* If alloc_page fails just return failure and caller will
1765 * free previous allocated pages by doing kfree_skb()
1766 */
1767 if (page == NULL)
1768 return -ENOMEM;
1770 /* initialize the next frag */
1771 sk->sk_sndmsg_page = page;
1772 sk->sk_sndmsg_off = 0;
1773 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1774 skb->truesize += PAGE_SIZE;
1775 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1777 /* get the new initialized frag */
1778 frg_cnt = skb_shinfo(skb)->nr_frags;
1779 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1781 /* copy the user data to page */
1782 left = PAGE_SIZE - frag->page_offset;
1783 copy = (length > left)? left : length;
1785 ret = getfrag(from, (page_address(frag->page) +
1786 frag->page_offset + frag->size),
1787 offset, copy, 0, skb);
1788 if (ret < 0)
1789 return -EFAULT;
1791 /* copy was successful so update the size parameters */
1792 sk->sk_sndmsg_off += copy;
1793 frag->size += copy;
1794 skb->len += copy;
1795 skb->data_len += copy;
1796 offset += copy;
1797 length -= copy;
1799 } while (length > 0);
1801 return 0;
1804 /**
1805 * skb_segment - Perform protocol segmentation on skb.
1806 * @skb: buffer to segment
1807 * @features: features for the output path (see dev->features)
1809 * This function performs segmentation on the given skb. It returns
1810 * the segment at the given position. It returns NULL if there are
1811 * no more segments to generate, or when an error is encountered.
1812 */
1813 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1815 struct sk_buff *segs = NULL;
1816 struct sk_buff *tail = NULL;
1817 unsigned int mss = skb_shinfo(skb)->gso_size;
1818 unsigned int doffset = skb->data - skb->mac.raw;
1819 unsigned int offset = doffset;
1820 unsigned int headroom;
1821 unsigned int len;
1822 int sg = features & NETIF_F_SG;
1823 int nfrags = skb_shinfo(skb)->nr_frags;
1824 int err = -ENOMEM;
1825 int i = 0;
1826 int pos;
1828 __skb_push(skb, doffset);
1829 headroom = skb_headroom(skb);
1830 pos = skb_headlen(skb);
1832 do {
1833 struct sk_buff *nskb;
1834 skb_frag_t *frag;
1835 int hsize, nsize;
1836 int k;
1837 int size;
1839 len = skb->len - offset;
1840 if (len > mss)
1841 len = mss;
1843 hsize = skb_headlen(skb) - offset;
1844 if (hsize < 0)
1845 hsize = 0;
1846 nsize = hsize + doffset;
1847 if (nsize > len + doffset || !sg)
1848 nsize = len + doffset;
1850 nskb = alloc_skb(nsize + headroom, GFP_ATOMIC);
1851 if (unlikely(!nskb))
1852 goto err;
1854 if (segs)
1855 tail->next = nskb;
1856 else
1857 segs = nskb;
1858 tail = nskb;
1860 nskb->dev = skb->dev;
1861 nskb->priority = skb->priority;
1862 nskb->protocol = skb->protocol;
1863 nskb->dst = dst_clone(skb->dst);
1864 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1865 nskb->pkt_type = skb->pkt_type;
1866 nskb->mac_len = skb->mac_len;
1868 skb_reserve(nskb, headroom);
1869 nskb->mac.raw = nskb->data;
1870 nskb->nh.raw = nskb->data + skb->mac_len;
1871 nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
1872 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1874 if (!sg) {
1875 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1876 skb_put(nskb, len),
1877 len, 0);
1878 continue;
1881 frag = skb_shinfo(nskb)->frags;
1882 k = 0;
1884 nskb->ip_summed = CHECKSUM_HW;
1885 nskb->csum = skb->csum;
1886 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
1888 while (pos < offset + len) {
1889 BUG_ON(i >= nfrags);
1891 *frag = skb_shinfo(skb)->frags[i];
1892 get_page(frag->page);
1893 size = frag->size;
1895 if (pos < offset) {
1896 frag->page_offset += offset - pos;
1897 frag->size -= offset - pos;
1900 k++;
1902 if (pos + size <= offset + len) {
1903 i++;
1904 pos += size;
1905 } else {
1906 frag->size -= pos + size - (offset + len);
1907 break;
1910 frag++;
1913 skb_shinfo(nskb)->nr_frags = k;
1914 nskb->data_len = len - hsize;
1915 nskb->len += nskb->data_len;
1916 nskb->truesize += nskb->data_len;
1917 } while ((offset += len) < skb->len);
1919 return segs;
1921 err:
1922 while ((skb = segs)) {
1923 segs = skb->next;
1924 kfree(skb);
1926 return ERR_PTR(err);
1929 EXPORT_SYMBOL_GPL(skb_segment);
1931 void __init skb_init(void)
1933 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1934 sizeof(struct sk_buff),
1935 0,
1936 SLAB_HWCACHE_ALIGN,
1937 NULL, NULL);
1938 if (!skbuff_head_cache)
1939 panic("cannot create skbuff cache");
1941 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1942 (2*sizeof(struct sk_buff)) +
1943 sizeof(atomic_t),
1944 0,
1945 SLAB_HWCACHE_ALIGN,
1946 NULL, NULL);
1947 if (!skbuff_fclone_cache)
1948 panic("cannot create skbuff cache");
1951 EXPORT_SYMBOL(___pskb_trim);
1952 EXPORT_SYMBOL(__kfree_skb);
1953 EXPORT_SYMBOL(__pskb_pull_tail);
1954 EXPORT_SYMBOL(__alloc_skb);
1955 EXPORT_SYMBOL(pskb_copy);
1956 EXPORT_SYMBOL(pskb_expand_head);
1957 EXPORT_SYMBOL(skb_checksum);
1958 EXPORT_SYMBOL(skb_clone);
1959 EXPORT_SYMBOL(skb_clone_fraglist);
1960 EXPORT_SYMBOL(skb_copy);
1961 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1962 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1963 EXPORT_SYMBOL(skb_copy_bits);
1964 EXPORT_SYMBOL(skb_copy_expand);
1965 EXPORT_SYMBOL(skb_over_panic);
1966 EXPORT_SYMBOL(skb_pad);
1967 EXPORT_SYMBOL(skb_realloc_headroom);
1968 EXPORT_SYMBOL(skb_under_panic);
1969 EXPORT_SYMBOL(skb_dequeue);
1970 EXPORT_SYMBOL(skb_dequeue_tail);
1971 EXPORT_SYMBOL(skb_insert);
1972 EXPORT_SYMBOL(skb_queue_purge);
1973 EXPORT_SYMBOL(skb_queue_head);
1974 EXPORT_SYMBOL(skb_queue_tail);
1975 EXPORT_SYMBOL(skb_unlink);
1976 EXPORT_SYMBOL(skb_append);
1977 EXPORT_SYMBOL(skb_split);
1978 EXPORT_SYMBOL(skb_prepare_seq_read);
1979 EXPORT_SYMBOL(skb_seq_read);
1980 EXPORT_SYMBOL(skb_abort_seq_read);
1981 EXPORT_SYMBOL(skb_find_text);
1982 EXPORT_SYMBOL(skb_append_datato_frags);