blob: e6564b0a683939c9a5f9773ddd1ae2de0a7cec44 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
36
37/*
38 * The functions in this file will not compile correctly with gcc 2.4.x
39 */
40
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>
61
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>
67
68#include <asm/uaccess.h>
69#include <asm/system.h>
70
71static kmem_cache_t *skbuff_head_cache;
72
73/*
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
76 * reliable.
77 */
78
79/**
80 * skb_over_panic - private function
81 * @skb: buffer
82 * @sz: size
83 * @here: address
84 *
85 * Out of line support code for skb_put(). Not user callable.
86 */
87void skb_over_panic(struct sk_buff *skb, int sz, void *here)
88{
Patrick McHardy26095452005-04-21 16:43:02 -070089 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
90 "data:%p tail:%p end:%p dev:%s\n",
91 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
92 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 BUG();
94}
95
96/**
97 * skb_under_panic - private function
98 * @skb: buffer
99 * @sz: size
100 * @here: address
101 *
102 * Out of line support code for skb_push(). Not user callable.
103 */
104
105void skb_under_panic(struct sk_buff *skb, int sz, void *here)
106{
Patrick McHardy26095452005-04-21 16:43:02 -0700107 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
108 "data:%p tail:%p end:%p dev:%s\n",
109 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
110 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 BUG();
112}
113
114/* Allocate a new skbuff. We do this ourselves so we can fill in a few
115 * 'private' fields and also do memory statistics to find all the
116 * [BEEP] leaks.
117 *
118 */
119
120/**
121 * alloc_skb - allocate a network buffer
122 * @size: size to allocate
123 * @gfp_mask: allocation mask
124 *
125 * Allocate a new &sk_buff. The returned buffer has no headroom and a
126 * tail room of size bytes. The object has a reference count of one.
127 * The return is the buffer. On a failure the return is %NULL.
128 *
129 * Buffers may only be allocated from interrupts using a @gfp_mask of
130 * %GFP_ATOMIC.
131 */
Victor Fusco86a76ca2005-07-08 14:57:47 -0700132struct sk_buff *alloc_skb(unsigned int size, unsigned int __nocast gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct sk_buff *skb;
135 u8 *data;
136
137 /* Get the HEAD */
138 skb = kmem_cache_alloc(skbuff_head_cache,
139 gfp_mask & ~__GFP_DMA);
140 if (!skb)
141 goto out;
142
143 /* Get the DATA. Size must match skb_add_mtu(). */
144 size = SKB_DATA_ALIGN(size);
145 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
146 if (!data)
147 goto nodata;
148
149 memset(skb, 0, offsetof(struct sk_buff, truesize));
150 skb->truesize = size + sizeof(struct sk_buff);
151 atomic_set(&skb->users, 1);
152 skb->head = data;
153 skb->data = data;
154 skb->tail = data;
155 skb->end = data + size;
156
157 atomic_set(&(skb_shinfo(skb)->dataref), 1);
158 skb_shinfo(skb)->nr_frags = 0;
159 skb_shinfo(skb)->tso_size = 0;
160 skb_shinfo(skb)->tso_segs = 0;
161 skb_shinfo(skb)->frag_list = NULL;
162out:
163 return skb;
164nodata:
165 kmem_cache_free(skbuff_head_cache, skb);
166 skb = NULL;
167 goto out;
168}
169
170/**
171 * alloc_skb_from_cache - allocate a network buffer
172 * @cp: kmem_cache from which to allocate the data area
173 * (object size must be big enough for @size bytes + skb overheads)
174 * @size: size to allocate
175 * @gfp_mask: allocation mask
176 *
177 * Allocate a new &sk_buff. The returned buffer has no headroom and
178 * tail room of size bytes. The object has a reference count of one.
179 * The return is the buffer. On a failure the return is %NULL.
180 *
181 * Buffers may only be allocated from interrupts using a @gfp_mask of
182 * %GFP_ATOMIC.
183 */
184struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700185 unsigned int size,
186 unsigned int __nocast gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct sk_buff *skb;
189 u8 *data;
190
191 /* Get the HEAD */
192 skb = kmem_cache_alloc(skbuff_head_cache,
193 gfp_mask & ~__GFP_DMA);
194 if (!skb)
195 goto out;
196
197 /* Get the DATA. */
198 size = SKB_DATA_ALIGN(size);
199 data = kmem_cache_alloc(cp, gfp_mask);
200 if (!data)
201 goto nodata;
202
203 memset(skb, 0, offsetof(struct sk_buff, truesize));
204 skb->truesize = size + sizeof(struct sk_buff);
205 atomic_set(&skb->users, 1);
206 skb->head = data;
207 skb->data = data;
208 skb->tail = data;
209 skb->end = data + size;
210
211 atomic_set(&(skb_shinfo(skb)->dataref), 1);
212 skb_shinfo(skb)->nr_frags = 0;
213 skb_shinfo(skb)->tso_size = 0;
214 skb_shinfo(skb)->tso_segs = 0;
215 skb_shinfo(skb)->frag_list = NULL;
216out:
217 return skb;
218nodata:
219 kmem_cache_free(skbuff_head_cache, skb);
220 skb = NULL;
221 goto out;
222}
223
224
225static void skb_drop_fraglist(struct sk_buff *skb)
226{
227 struct sk_buff *list = skb_shinfo(skb)->frag_list;
228
229 skb_shinfo(skb)->frag_list = NULL;
230
231 do {
232 struct sk_buff *this = list;
233 list = list->next;
234 kfree_skb(this);
235 } while (list);
236}
237
238static void skb_clone_fraglist(struct sk_buff *skb)
239{
240 struct sk_buff *list;
241
242 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
243 skb_get(list);
244}
245
246void skb_release_data(struct sk_buff *skb)
247{
248 if (!skb->cloned ||
249 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
250 &skb_shinfo(skb)->dataref)) {
251 if (skb_shinfo(skb)->nr_frags) {
252 int i;
253 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
254 put_page(skb_shinfo(skb)->frags[i].page);
255 }
256
257 if (skb_shinfo(skb)->frag_list)
258 skb_drop_fraglist(skb);
259
260 kfree(skb->head);
261 }
262}
263
264/*
265 * Free an skbuff by memory without cleaning the state.
266 */
267void kfree_skbmem(struct sk_buff *skb)
268{
269 skb_release_data(skb);
270 kmem_cache_free(skbuff_head_cache, skb);
271}
272
273/**
274 * __kfree_skb - private function
275 * @skb: buffer
276 *
277 * Free an sk_buff. Release anything attached to the buffer.
278 * Clean the state. This is an internal helper function. Users should
279 * always call kfree_skb
280 */
281
282void __kfree_skb(struct sk_buff *skb)
283{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 dst_release(skb->dst);
285#ifdef CONFIG_XFRM
286 secpath_put(skb->sp);
287#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700288 if (skb->destructor) {
289 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 skb->destructor(skb);
291 }
292#ifdef CONFIG_NETFILTER
293 nf_conntrack_put(skb->nfct);
294#ifdef CONFIG_BRIDGE_NETFILTER
295 nf_bridge_put(skb->nf_bridge);
296#endif
297#endif
298/* XXX: IS this still necessary? - JHS */
299#ifdef CONFIG_NET_SCHED
300 skb->tc_index = 0;
301#ifdef CONFIG_NET_CLS_ACT
302 skb->tc_verd = 0;
303 skb->tc_classid = 0;
304#endif
305#endif
306
307 kfree_skbmem(skb);
308}
309
310/**
311 * skb_clone - duplicate an sk_buff
312 * @skb: buffer to clone
313 * @gfp_mask: allocation priority
314 *
315 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
316 * copies share the same packet data but not structure. The new
317 * buffer has a reference count of 1. If the allocation fails the
318 * function returns %NULL otherwise the new buffer is returned.
319 *
320 * If this function is called from an interrupt gfp_mask() must be
321 * %GFP_ATOMIC.
322 */
323
Victor Fusco86a76ca2005-07-08 14:57:47 -0700324struct sk_buff *skb_clone(struct sk_buff *skb, unsigned int __nocast gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
327
328 if (!n)
329 return NULL;
330
331#define C(x) n->x = skb->x
332
333 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 n->sk = NULL;
335 C(stamp);
336 C(dev);
337 C(real_dev);
338 C(h);
339 C(nh);
340 C(mac);
341 C(dst);
342 dst_clone(skb->dst);
343 C(sp);
344#ifdef CONFIG_INET
345 secpath_get(skb->sp);
346#endif
347 memcpy(n->cb, skb->cb, sizeof(skb->cb));
348 C(len);
349 C(data_len);
350 C(csum);
351 C(local_df);
352 n->cloned = 1;
353 n->nohdr = 0;
354 C(pkt_type);
355 C(ip_summed);
356 C(priority);
357 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 n->destructor = NULL;
359#ifdef CONFIG_NETFILTER
360 C(nfmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 C(nfct);
362 nf_conntrack_get(skb->nfct);
363 C(nfctinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#ifdef CONFIG_BRIDGE_NETFILTER
365 C(nf_bridge);
366 nf_bridge_get(skb->nf_bridge);
367#endif
368#endif /*CONFIG_NETFILTER*/
369#if defined(CONFIG_HIPPI)
370 C(private);
371#endif
372#ifdef CONFIG_NET_SCHED
373 C(tc_index);
374#ifdef CONFIG_NET_CLS_ACT
375 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700376 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
377 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 C(input_dev);
379 C(tc_classid);
380#endif
381
382#endif
383 C(truesize);
384 atomic_set(&n->users, 1);
385 C(head);
386 C(data);
387 C(tail);
388 C(end);
389
390 atomic_inc(&(skb_shinfo(skb)->dataref));
391 skb->cloned = 1;
392
393 return n;
394}
395
396static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
397{
398 /*
399 * Shift between the two data areas in bytes
400 */
401 unsigned long offset = new->data - old->data;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 new->sk = NULL;
404 new->dev = old->dev;
405 new->real_dev = old->real_dev;
406 new->priority = old->priority;
407 new->protocol = old->protocol;
408 new->dst = dst_clone(old->dst);
409#ifdef CONFIG_INET
410 new->sp = secpath_get(old->sp);
411#endif
412 new->h.raw = old->h.raw + offset;
413 new->nh.raw = old->nh.raw + offset;
414 new->mac.raw = old->mac.raw + offset;
415 memcpy(new->cb, old->cb, sizeof(old->cb));
416 new->local_df = old->local_df;
417 new->pkt_type = old->pkt_type;
418 new->stamp = old->stamp;
419 new->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#ifdef CONFIG_NETFILTER
421 new->nfmark = old->nfmark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 new->nfct = old->nfct;
423 nf_conntrack_get(old->nfct);
424 new->nfctinfo = old->nfctinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#ifdef CONFIG_BRIDGE_NETFILTER
426 new->nf_bridge = old->nf_bridge;
427 nf_bridge_get(old->nf_bridge);
428#endif
429#endif
430#ifdef CONFIG_NET_SCHED
431#ifdef CONFIG_NET_CLS_ACT
432 new->tc_verd = old->tc_verd;
433#endif
434 new->tc_index = old->tc_index;
435#endif
436 atomic_set(&new->users, 1);
437 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
438 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
439}
440
441/**
442 * skb_copy - create private copy of an sk_buff
443 * @skb: buffer to copy
444 * @gfp_mask: allocation priority
445 *
446 * Make a copy of both an &sk_buff and its data. This is used when the
447 * caller wishes to modify the data and needs a private copy of the
448 * data to alter. Returns %NULL on failure or the pointer to the buffer
449 * on success. The returned buffer has a reference count of 1.
450 *
451 * As by-product this function converts non-linear &sk_buff to linear
452 * one, so that &sk_buff becomes completely private and caller is allowed
453 * to modify all the data of returned buffer. This means that this
454 * function is not recommended for use in circumstances when only
455 * header is going to be modified. Use pskb_copy() instead.
456 */
457
Victor Fusco86a76ca2005-07-08 14:57:47 -0700458struct sk_buff *skb_copy(const struct sk_buff *skb, unsigned int __nocast gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 int headerlen = skb->data - skb->head;
461 /*
462 * Allocate the copy buffer
463 */
464 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
465 gfp_mask);
466 if (!n)
467 return NULL;
468
469 /* Set the data pointer */
470 skb_reserve(n, headerlen);
471 /* Set the tail pointer and length */
472 skb_put(n, skb->len);
473 n->csum = skb->csum;
474 n->ip_summed = skb->ip_summed;
475
476 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
477 BUG();
478
479 copy_skb_header(n, skb);
480 return n;
481}
482
483
484/**
485 * pskb_copy - create copy of an sk_buff with private head.
486 * @skb: buffer to copy
487 * @gfp_mask: allocation priority
488 *
489 * Make a copy of both an &sk_buff and part of its data, located
490 * in header. Fragmented data remain shared. This is used when
491 * the caller wishes to modify only header of &sk_buff and needs
492 * private copy of the header to alter. Returns %NULL on failure
493 * or the pointer to the buffer on success.
494 * The returned buffer has a reference count of 1.
495 */
496
Victor Fusco86a76ca2005-07-08 14:57:47 -0700497struct sk_buff *pskb_copy(struct sk_buff *skb, unsigned int __nocast gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 /*
500 * Allocate the copy buffer
501 */
502 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
503
504 if (!n)
505 goto out;
506
507 /* Set the data pointer */
508 skb_reserve(n, skb->data - skb->head);
509 /* Set the tail pointer and length */
510 skb_put(n, skb_headlen(skb));
511 /* Copy the bytes */
512 memcpy(n->data, skb->data, n->len);
513 n->csum = skb->csum;
514 n->ip_summed = skb->ip_summed;
515
516 n->data_len = skb->data_len;
517 n->len = skb->len;
518
519 if (skb_shinfo(skb)->nr_frags) {
520 int i;
521
522 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
523 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
524 get_page(skb_shinfo(n)->frags[i].page);
525 }
526 skb_shinfo(n)->nr_frags = i;
527 }
528
529 if (skb_shinfo(skb)->frag_list) {
530 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
531 skb_clone_fraglist(n);
532 }
533
534 copy_skb_header(n, skb);
535out:
536 return n;
537}
538
539/**
540 * pskb_expand_head - reallocate header of &sk_buff
541 * @skb: buffer to reallocate
542 * @nhead: room to add at head
543 * @ntail: room to add at tail
544 * @gfp_mask: allocation priority
545 *
546 * Expands (or creates identical copy, if &nhead and &ntail are zero)
547 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
548 * reference count of 1. Returns zero in the case of success or error,
549 * if expansion failed. In the last case, &sk_buff is not changed.
550 *
551 * All the pointers pointing into skb header may change and must be
552 * reloaded after call to this function.
553 */
554
Victor Fusco86a76ca2005-07-08 14:57:47 -0700555int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
556 unsigned int __nocast gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 int i;
559 u8 *data;
560 int size = nhead + (skb->end - skb->head) + ntail;
561 long off;
562
563 if (skb_shared(skb))
564 BUG();
565
566 size = SKB_DATA_ALIGN(size);
567
568 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
569 if (!data)
570 goto nodata;
571
572 /* Copy only real data... and, alas, header. This should be
573 * optimized for the cases when header is void. */
574 memcpy(data + nhead, skb->head, skb->tail - skb->head);
575 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
576
577 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
578 get_page(skb_shinfo(skb)->frags[i].page);
579
580 if (skb_shinfo(skb)->frag_list)
581 skb_clone_fraglist(skb);
582
583 skb_release_data(skb);
584
585 off = (data + nhead) - skb->head;
586
587 skb->head = data;
588 skb->end = data + size;
589 skb->data += off;
590 skb->tail += off;
591 skb->mac.raw += off;
592 skb->h.raw += off;
593 skb->nh.raw += off;
594 skb->cloned = 0;
595 skb->nohdr = 0;
596 atomic_set(&skb_shinfo(skb)->dataref, 1);
597 return 0;
598
599nodata:
600 return -ENOMEM;
601}
602
603/* Make private copy of skb with writable head and some headroom */
604
605struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
606{
607 struct sk_buff *skb2;
608 int delta = headroom - skb_headroom(skb);
609
610 if (delta <= 0)
611 skb2 = pskb_copy(skb, GFP_ATOMIC);
612 else {
613 skb2 = skb_clone(skb, GFP_ATOMIC);
614 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
615 GFP_ATOMIC)) {
616 kfree_skb(skb2);
617 skb2 = NULL;
618 }
619 }
620 return skb2;
621}
622
623
624/**
625 * skb_copy_expand - copy and expand sk_buff
626 * @skb: buffer to copy
627 * @newheadroom: new free bytes at head
628 * @newtailroom: new free bytes at tail
629 * @gfp_mask: allocation priority
630 *
631 * Make a copy of both an &sk_buff and its data and while doing so
632 * allocate additional space.
633 *
634 * This is used when the caller wishes to modify the data and needs a
635 * private copy of the data to alter as well as more space for new fields.
636 * Returns %NULL on failure or the pointer to the buffer
637 * on success. The returned buffer has a reference count of 1.
638 *
639 * You must pass %GFP_ATOMIC as the allocation priority if this function
640 * is called from an interrupt.
641 *
642 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
643 * only by netfilter in the cases when checksum is recalculated? --ANK
644 */
645struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700646 int newheadroom, int newtailroom,
647 unsigned int __nocast gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 /*
650 * Allocate the copy buffer
651 */
652 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
653 gfp_mask);
654 int head_copy_len, head_copy_off;
655
656 if (!n)
657 return NULL;
658
659 skb_reserve(n, newheadroom);
660
661 /* Set the tail pointer and length */
662 skb_put(n, skb->len);
663
664 head_copy_len = skb_headroom(skb);
665 head_copy_off = 0;
666 if (newheadroom <= head_copy_len)
667 head_copy_len = newheadroom;
668 else
669 head_copy_off = newheadroom - head_copy_len;
670
671 /* Copy the linear header and data. */
672 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
673 skb->len + head_copy_len))
674 BUG();
675
676 copy_skb_header(n, skb);
677
678 return n;
679}
680
681/**
682 * skb_pad - zero pad the tail of an skb
683 * @skb: buffer to pad
684 * @pad: space to pad
685 *
686 * Ensure that a buffer is followed by a padding area that is zero
687 * filled. Used by network drivers which may DMA or transfer data
688 * beyond the buffer end onto the wire.
689 *
690 * May return NULL in out of memory cases.
691 */
692
693struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
694{
695 struct sk_buff *nskb;
696
697 /* If the skbuff is non linear tailroom is always zero.. */
698 if (skb_tailroom(skb) >= pad) {
699 memset(skb->data+skb->len, 0, pad);
700 return skb;
701 }
702
703 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
704 kfree_skb(skb);
705 if (nskb)
706 memset(nskb->data+nskb->len, 0, pad);
707 return nskb;
708}
709
710/* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
711 * If realloc==0 and trimming is impossible without change of data,
712 * it is BUG().
713 */
714
715int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
716{
717 int offset = skb_headlen(skb);
718 int nfrags = skb_shinfo(skb)->nr_frags;
719 int i;
720
721 for (i = 0; i < nfrags; i++) {
722 int end = offset + skb_shinfo(skb)->frags[i].size;
723 if (end > len) {
724 if (skb_cloned(skb)) {
725 if (!realloc)
726 BUG();
727 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
728 return -ENOMEM;
729 }
730 if (len <= offset) {
731 put_page(skb_shinfo(skb)->frags[i].page);
732 skb_shinfo(skb)->nr_frags--;
733 } else {
734 skb_shinfo(skb)->frags[i].size = len - offset;
735 }
736 }
737 offset = end;
738 }
739
740 if (offset < len) {
741 skb->data_len -= skb->len - len;
742 skb->len = len;
743 } else {
744 if (len <= skb_headlen(skb)) {
745 skb->len = len;
746 skb->data_len = 0;
747 skb->tail = skb->data + len;
748 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
749 skb_drop_fraglist(skb);
750 } else {
751 skb->data_len -= skb->len - len;
752 skb->len = len;
753 }
754 }
755
756 return 0;
757}
758
759/**
760 * __pskb_pull_tail - advance tail of skb header
761 * @skb: buffer to reallocate
762 * @delta: number of bytes to advance tail
763 *
764 * The function makes a sense only on a fragmented &sk_buff,
765 * it expands header moving its tail forward and copying necessary
766 * data from fragmented part.
767 *
768 * &sk_buff MUST have reference count of 1.
769 *
770 * Returns %NULL (and &sk_buff does not change) if pull failed
771 * or value of new tail of skb in the case of success.
772 *
773 * All the pointers pointing into skb header may change and must be
774 * reloaded after call to this function.
775 */
776
777/* Moves tail of skb head forward, copying data from fragmented part,
778 * when it is necessary.
779 * 1. It may fail due to malloc failure.
780 * 2. It may change skb pointers.
781 *
782 * It is pretty complicated. Luckily, it is called only in exceptional cases.
783 */
784unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
785{
786 /* If skb has not enough free space at tail, get new one
787 * plus 128 bytes for future expansions. If we have enough
788 * room at tail, reallocate without expansion only if skb is cloned.
789 */
790 int i, k, eat = (skb->tail + delta) - skb->end;
791
792 if (eat > 0 || skb_cloned(skb)) {
793 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
794 GFP_ATOMIC))
795 return NULL;
796 }
797
798 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
799 BUG();
800
801 /* Optimization: no fragments, no reasons to preestimate
802 * size of pulled pages. Superb.
803 */
804 if (!skb_shinfo(skb)->frag_list)
805 goto pull_pages;
806
807 /* Estimate size of pulled pages. */
808 eat = delta;
809 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
810 if (skb_shinfo(skb)->frags[i].size >= eat)
811 goto pull_pages;
812 eat -= skb_shinfo(skb)->frags[i].size;
813 }
814
815 /* If we need update frag list, we are in troubles.
816 * Certainly, it possible to add an offset to skb data,
817 * but taking into account that pulling is expected to
818 * be very rare operation, it is worth to fight against
819 * further bloating skb head and crucify ourselves here instead.
820 * Pure masohism, indeed. 8)8)
821 */
822 if (eat) {
823 struct sk_buff *list = skb_shinfo(skb)->frag_list;
824 struct sk_buff *clone = NULL;
825 struct sk_buff *insp = NULL;
826
827 do {
828 if (!list)
829 BUG();
830
831 if (list->len <= eat) {
832 /* Eaten as whole. */
833 eat -= list->len;
834 list = list->next;
835 insp = list;
836 } else {
837 /* Eaten partially. */
838
839 if (skb_shared(list)) {
840 /* Sucks! We need to fork list. :-( */
841 clone = skb_clone(list, GFP_ATOMIC);
842 if (!clone)
843 return NULL;
844 insp = list->next;
845 list = clone;
846 } else {
847 /* This may be pulled without
848 * problems. */
849 insp = list;
850 }
851 if (!pskb_pull(list, eat)) {
852 if (clone)
853 kfree_skb(clone);
854 return NULL;
855 }
856 break;
857 }
858 } while (eat);
859
860 /* Free pulled out fragments. */
861 while ((list = skb_shinfo(skb)->frag_list) != insp) {
862 skb_shinfo(skb)->frag_list = list->next;
863 kfree_skb(list);
864 }
865 /* And insert new clone at head. */
866 if (clone) {
867 clone->next = list;
868 skb_shinfo(skb)->frag_list = clone;
869 }
870 }
871 /* Success! Now we may commit changes to skb data. */
872
873pull_pages:
874 eat = delta;
875 k = 0;
876 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
877 if (skb_shinfo(skb)->frags[i].size <= eat) {
878 put_page(skb_shinfo(skb)->frags[i].page);
879 eat -= skb_shinfo(skb)->frags[i].size;
880 } else {
881 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
882 if (eat) {
883 skb_shinfo(skb)->frags[k].page_offset += eat;
884 skb_shinfo(skb)->frags[k].size -= eat;
885 eat = 0;
886 }
887 k++;
888 }
889 }
890 skb_shinfo(skb)->nr_frags = k;
891
892 skb->tail += delta;
893 skb->data_len -= delta;
894
895 return skb->tail;
896}
897
898/* Copy some data bits from skb to kernel buffer. */
899
900int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
901{
902 int i, copy;
903 int start = skb_headlen(skb);
904
905 if (offset > (int)skb->len - len)
906 goto fault;
907
908 /* Copy header. */
909 if ((copy = start - offset) > 0) {
910 if (copy > len)
911 copy = len;
912 memcpy(to, skb->data + offset, copy);
913 if ((len -= copy) == 0)
914 return 0;
915 offset += copy;
916 to += copy;
917 }
918
919 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
920 int end;
921
922 BUG_TRAP(start <= offset + len);
923
924 end = start + skb_shinfo(skb)->frags[i].size;
925 if ((copy = end - offset) > 0) {
926 u8 *vaddr;
927
928 if (copy > len)
929 copy = len;
930
931 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
932 memcpy(to,
933 vaddr + skb_shinfo(skb)->frags[i].page_offset+
934 offset - start, copy);
935 kunmap_skb_frag(vaddr);
936
937 if ((len -= copy) == 0)
938 return 0;
939 offset += copy;
940 to += copy;
941 }
942 start = end;
943 }
944
945 if (skb_shinfo(skb)->frag_list) {
946 struct sk_buff *list = skb_shinfo(skb)->frag_list;
947
948 for (; list; list = list->next) {
949 int end;
950
951 BUG_TRAP(start <= offset + len);
952
953 end = start + list->len;
954 if ((copy = end - offset) > 0) {
955 if (copy > len)
956 copy = len;
957 if (skb_copy_bits(list, offset - start,
958 to, copy))
959 goto fault;
960 if ((len -= copy) == 0)
961 return 0;
962 offset += copy;
963 to += copy;
964 }
965 start = end;
966 }
967 }
968 if (!len)
969 return 0;
970
971fault:
972 return -EFAULT;
973}
974
Herbert Xu357b40a2005-04-19 22:30:14 -0700975/**
976 * skb_store_bits - store bits from kernel buffer to skb
977 * @skb: destination buffer
978 * @offset: offset in destination
979 * @from: source buffer
980 * @len: number of bytes to copy
981 *
982 * Copy the specified number of bytes from the source buffer to the
983 * destination skb. This function handles all the messy bits of
984 * traversing fragment lists and such.
985 */
986
987int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
988{
989 int i, copy;
990 int start = skb_headlen(skb);
991
992 if (offset > (int)skb->len - len)
993 goto fault;
994
995 if ((copy = start - offset) > 0) {
996 if (copy > len)
997 copy = len;
998 memcpy(skb->data + offset, from, copy);
999 if ((len -= copy) == 0)
1000 return 0;
1001 offset += copy;
1002 from += copy;
1003 }
1004
1005 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1006 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1007 int end;
1008
1009 BUG_TRAP(start <= offset + len);
1010
1011 end = start + frag->size;
1012 if ((copy = end - offset) > 0) {
1013 u8 *vaddr;
1014
1015 if (copy > len)
1016 copy = len;
1017
1018 vaddr = kmap_skb_frag(frag);
1019 memcpy(vaddr + frag->page_offset + offset - start,
1020 from, copy);
1021 kunmap_skb_frag(vaddr);
1022
1023 if ((len -= copy) == 0)
1024 return 0;
1025 offset += copy;
1026 from += copy;
1027 }
1028 start = end;
1029 }
1030
1031 if (skb_shinfo(skb)->frag_list) {
1032 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1033
1034 for (; list; list = list->next) {
1035 int end;
1036
1037 BUG_TRAP(start <= offset + len);
1038
1039 end = start + list->len;
1040 if ((copy = end - offset) > 0) {
1041 if (copy > len)
1042 copy = len;
1043 if (skb_store_bits(list, offset - start,
1044 from, copy))
1045 goto fault;
1046 if ((len -= copy) == 0)
1047 return 0;
1048 offset += copy;
1049 from += copy;
1050 }
1051 start = end;
1052 }
1053 }
1054 if (!len)
1055 return 0;
1056
1057fault:
1058 return -EFAULT;
1059}
1060
1061EXPORT_SYMBOL(skb_store_bits);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063/* Checksum skb data. */
1064
1065unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1066 int len, unsigned int csum)
1067{
1068 int start = skb_headlen(skb);
1069 int i, copy = start - offset;
1070 int pos = 0;
1071
1072 /* Checksum header. */
1073 if (copy > 0) {
1074 if (copy > len)
1075 copy = len;
1076 csum = csum_partial(skb->data + offset, copy, csum);
1077 if ((len -= copy) == 0)
1078 return csum;
1079 offset += copy;
1080 pos = copy;
1081 }
1082
1083 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1084 int end;
1085
1086 BUG_TRAP(start <= offset + len);
1087
1088 end = start + skb_shinfo(skb)->frags[i].size;
1089 if ((copy = end - offset) > 0) {
1090 unsigned int csum2;
1091 u8 *vaddr;
1092 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1093
1094 if (copy > len)
1095 copy = len;
1096 vaddr = kmap_skb_frag(frag);
1097 csum2 = csum_partial(vaddr + frag->page_offset +
1098 offset - start, copy, 0);
1099 kunmap_skb_frag(vaddr);
1100 csum = csum_block_add(csum, csum2, pos);
1101 if (!(len -= copy))
1102 return csum;
1103 offset += copy;
1104 pos += copy;
1105 }
1106 start = end;
1107 }
1108
1109 if (skb_shinfo(skb)->frag_list) {
1110 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1111
1112 for (; list; list = list->next) {
1113 int end;
1114
1115 BUG_TRAP(start <= offset + len);
1116
1117 end = start + list->len;
1118 if ((copy = end - offset) > 0) {
1119 unsigned int csum2;
1120 if (copy > len)
1121 copy = len;
1122 csum2 = skb_checksum(list, offset - start,
1123 copy, 0);
1124 csum = csum_block_add(csum, csum2, pos);
1125 if ((len -= copy) == 0)
1126 return csum;
1127 offset += copy;
1128 pos += copy;
1129 }
1130 start = end;
1131 }
1132 }
1133 if (len)
1134 BUG();
1135
1136 return csum;
1137}
1138
1139/* Both of above in one bottle. */
1140
1141unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1142 u8 *to, int len, unsigned int csum)
1143{
1144 int start = skb_headlen(skb);
1145 int i, copy = start - offset;
1146 int pos = 0;
1147
1148 /* Copy header. */
1149 if (copy > 0) {
1150 if (copy > len)
1151 copy = len;
1152 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1153 copy, csum);
1154 if ((len -= copy) == 0)
1155 return csum;
1156 offset += copy;
1157 to += copy;
1158 pos = copy;
1159 }
1160
1161 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1162 int end;
1163
1164 BUG_TRAP(start <= offset + len);
1165
1166 end = start + skb_shinfo(skb)->frags[i].size;
1167 if ((copy = end - offset) > 0) {
1168 unsigned int csum2;
1169 u8 *vaddr;
1170 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1171
1172 if (copy > len)
1173 copy = len;
1174 vaddr = kmap_skb_frag(frag);
1175 csum2 = csum_partial_copy_nocheck(vaddr +
1176 frag->page_offset +
1177 offset - start, to,
1178 copy, 0);
1179 kunmap_skb_frag(vaddr);
1180 csum = csum_block_add(csum, csum2, pos);
1181 if (!(len -= copy))
1182 return csum;
1183 offset += copy;
1184 to += copy;
1185 pos += copy;
1186 }
1187 start = end;
1188 }
1189
1190 if (skb_shinfo(skb)->frag_list) {
1191 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1192
1193 for (; list; list = list->next) {
1194 unsigned int csum2;
1195 int end;
1196
1197 BUG_TRAP(start <= offset + len);
1198
1199 end = start + list->len;
1200 if ((copy = end - offset) > 0) {
1201 if (copy > len)
1202 copy = len;
1203 csum2 = skb_copy_and_csum_bits(list,
1204 offset - start,
1205 to, copy, 0);
1206 csum = csum_block_add(csum, csum2, pos);
1207 if ((len -= copy) == 0)
1208 return csum;
1209 offset += copy;
1210 to += copy;
1211 pos += copy;
1212 }
1213 start = end;
1214 }
1215 }
1216 if (len)
1217 BUG();
1218 return csum;
1219}
1220
1221void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1222{
1223 unsigned int csum;
1224 long csstart;
1225
1226 if (skb->ip_summed == CHECKSUM_HW)
1227 csstart = skb->h.raw - skb->data;
1228 else
1229 csstart = skb_headlen(skb);
1230
1231 if (csstart > skb_headlen(skb))
1232 BUG();
1233
1234 memcpy(to, skb->data, csstart);
1235
1236 csum = 0;
1237 if (csstart != skb->len)
1238 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1239 skb->len - csstart, 0);
1240
1241 if (skb->ip_summed == CHECKSUM_HW) {
1242 long csstuff = csstart + skb->csum;
1243
1244 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1245 }
1246}
1247
1248/**
1249 * skb_dequeue - remove from the head of the queue
1250 * @list: list to dequeue from
1251 *
1252 * Remove the head of the list. The list lock is taken so the function
1253 * may be used safely with other locking list functions. The head item is
1254 * returned or %NULL if the list is empty.
1255 */
1256
1257struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1258{
1259 unsigned long flags;
1260 struct sk_buff *result;
1261
1262 spin_lock_irqsave(&list->lock, flags);
1263 result = __skb_dequeue(list);
1264 spin_unlock_irqrestore(&list->lock, flags);
1265 return result;
1266}
1267
1268/**
1269 * skb_dequeue_tail - remove from the tail of the queue
1270 * @list: list to dequeue from
1271 *
1272 * Remove the tail of the list. The list lock is taken so the function
1273 * may be used safely with other locking list functions. The tail item is
1274 * returned or %NULL if the list is empty.
1275 */
1276struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1277{
1278 unsigned long flags;
1279 struct sk_buff *result;
1280
1281 spin_lock_irqsave(&list->lock, flags);
1282 result = __skb_dequeue_tail(list);
1283 spin_unlock_irqrestore(&list->lock, flags);
1284 return result;
1285}
1286
1287/**
1288 * skb_queue_purge - empty a list
1289 * @list: list to empty
1290 *
1291 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1292 * the list and one reference dropped. This function takes the list
1293 * lock and is atomic with respect to other list locking functions.
1294 */
1295void skb_queue_purge(struct sk_buff_head *list)
1296{
1297 struct sk_buff *skb;
1298 while ((skb = skb_dequeue(list)) != NULL)
1299 kfree_skb(skb);
1300}
1301
1302/**
1303 * skb_queue_head - queue a buffer at the list head
1304 * @list: list to use
1305 * @newsk: buffer to queue
1306 *
1307 * Queue a buffer at the start of the list. This function takes the
1308 * list lock and can be used safely with other locking &sk_buff functions
1309 * safely.
1310 *
1311 * A buffer cannot be placed on two lists at the same time.
1312 */
1313void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1314{
1315 unsigned long flags;
1316
1317 spin_lock_irqsave(&list->lock, flags);
1318 __skb_queue_head(list, newsk);
1319 spin_unlock_irqrestore(&list->lock, flags);
1320}
1321
1322/**
1323 * skb_queue_tail - queue a buffer at the list tail
1324 * @list: list to use
1325 * @newsk: buffer to queue
1326 *
1327 * Queue a buffer at the tail of the list. This function takes the
1328 * list lock and can be used safely with other locking &sk_buff functions
1329 * safely.
1330 *
1331 * A buffer cannot be placed on two lists at the same time.
1332 */
1333void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1334{
1335 unsigned long flags;
1336
1337 spin_lock_irqsave(&list->lock, flags);
1338 __skb_queue_tail(list, newsk);
1339 spin_unlock_irqrestore(&list->lock, flags);
1340}
David S. Miller8728b832005-08-09 19:25:21 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342/**
1343 * skb_unlink - remove a buffer from a list
1344 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001345 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *
David S. Miller8728b832005-08-09 19:25:21 -07001347 * Remove a packet from a list. The list locks are taken and this
1348 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 *
David S. Miller8728b832005-08-09 19:25:21 -07001350 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
David S. Miller8728b832005-08-09 19:25:21 -07001352void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
David S. Miller8728b832005-08-09 19:25:21 -07001354 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
David S. Miller8728b832005-08-09 19:25:21 -07001356 spin_lock_irqsave(&list->lock, flags);
1357 __skb_unlink(skb, list);
1358 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/**
1362 * skb_append - append a buffer
1363 * @old: buffer to insert after
1364 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001365 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 *
1367 * Place a packet after a given packet in a list. The list locks are taken
1368 * and this function is atomic with respect to other list locked calls.
1369 * A buffer cannot be placed on two lists at the same time.
1370 */
David S. Miller8728b832005-08-09 19:25:21 -07001371void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 unsigned long flags;
1374
David S. Miller8728b832005-08-09 19:25:21 -07001375 spin_lock_irqsave(&list->lock, flags);
1376 __skb_append(old, newsk, list);
1377 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
1380
1381/**
1382 * skb_insert - insert a buffer
1383 * @old: buffer to insert before
1384 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001385 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 *
David S. Miller8728b832005-08-09 19:25:21 -07001387 * Place a packet before a given packet in a list. The list locks are
1388 * taken and this function is atomic with respect to other list locked
1389 * calls.
1390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * A buffer cannot be placed on two lists at the same time.
1392 */
David S. Miller8728b832005-08-09 19:25:21 -07001393void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 unsigned long flags;
1396
David S. Miller8728b832005-08-09 19:25:21 -07001397 spin_lock_irqsave(&list->lock, flags);
1398 __skb_insert(newsk, old->prev, old, list);
1399 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402#if 0
1403/*
1404 * Tune the memory allocator for a new MTU size.
1405 */
1406void skb_add_mtu(int mtu)
1407{
1408 /* Must match allocation in alloc_skb */
1409 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1410
1411 kmem_add_cache_size(mtu);
1412}
1413#endif
1414
1415static inline void skb_split_inside_header(struct sk_buff *skb,
1416 struct sk_buff* skb1,
1417 const u32 len, const int pos)
1418{
1419 int i;
1420
1421 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1422
1423 /* And move data appendix as is. */
1424 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1425 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1426
1427 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1428 skb_shinfo(skb)->nr_frags = 0;
1429 skb1->data_len = skb->data_len;
1430 skb1->len += skb1->data_len;
1431 skb->data_len = 0;
1432 skb->len = len;
1433 skb->tail = skb->data + len;
1434}
1435
1436static inline void skb_split_no_header(struct sk_buff *skb,
1437 struct sk_buff* skb1,
1438 const u32 len, int pos)
1439{
1440 int i, k = 0;
1441 const int nfrags = skb_shinfo(skb)->nr_frags;
1442
1443 skb_shinfo(skb)->nr_frags = 0;
1444 skb1->len = skb1->data_len = skb->len - len;
1445 skb->len = len;
1446 skb->data_len = len - pos;
1447
1448 for (i = 0; i < nfrags; i++) {
1449 int size = skb_shinfo(skb)->frags[i].size;
1450
1451 if (pos + size > len) {
1452 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1453
1454 if (pos < len) {
1455 /* Split frag.
1456 * We have two variants in this case:
1457 * 1. Move all the frag to the second
1458 * part, if it is possible. F.e.
1459 * this approach is mandatory for TUX,
1460 * where splitting is expensive.
1461 * 2. Split is accurately. We make this.
1462 */
1463 get_page(skb_shinfo(skb)->frags[i].page);
1464 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1465 skb_shinfo(skb1)->frags[0].size -= len - pos;
1466 skb_shinfo(skb)->frags[i].size = len - pos;
1467 skb_shinfo(skb)->nr_frags++;
1468 }
1469 k++;
1470 } else
1471 skb_shinfo(skb)->nr_frags++;
1472 pos += size;
1473 }
1474 skb_shinfo(skb1)->nr_frags = k;
1475}
1476
1477/**
1478 * skb_split - Split fragmented skb to two parts at length len.
1479 * @skb: the buffer to split
1480 * @skb1: the buffer to receive the second part
1481 * @len: new length for skb
1482 */
1483void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1484{
1485 int pos = skb_headlen(skb);
1486
1487 if (len < pos) /* Split line is inside header. */
1488 skb_split_inside_header(skb, skb1, len, pos);
1489 else /* Second chunk has no header, nothing to copy. */
1490 skb_split_no_header(skb, skb1, len, pos);
1491}
1492
Thomas Graf677e90e2005-06-23 20:59:51 -07001493/**
1494 * skb_prepare_seq_read - Prepare a sequential read of skb data
1495 * @skb: the buffer to read
1496 * @from: lower offset of data to be read
1497 * @to: upper offset of data to be read
1498 * @st: state variable
1499 *
1500 * Initializes the specified state variable. Must be called before
1501 * invoking skb_seq_read() for the first time.
1502 */
1503void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1504 unsigned int to, struct skb_seq_state *st)
1505{
1506 st->lower_offset = from;
1507 st->upper_offset = to;
1508 st->root_skb = st->cur_skb = skb;
1509 st->frag_idx = st->stepped_offset = 0;
1510 st->frag_data = NULL;
1511}
1512
1513/**
1514 * skb_seq_read - Sequentially read skb data
1515 * @consumed: number of bytes consumed by the caller so far
1516 * @data: destination pointer for data to be returned
1517 * @st: state variable
1518 *
1519 * Reads a block of skb data at &consumed relative to the
1520 * lower offset specified to skb_prepare_seq_read(). Assigns
1521 * the head of the data block to &data and returns the length
1522 * of the block or 0 if the end of the skb data or the upper
1523 * offset has been reached.
1524 *
1525 * The caller is not required to consume all of the data
1526 * returned, i.e. &consumed is typically set to the number
1527 * of bytes already consumed and the next call to
1528 * skb_seq_read() will return the remaining part of the block.
1529 *
1530 * Note: The size of each block of data returned can be arbitary,
1531 * this limitation is the cost for zerocopy seqeuental
1532 * reads of potentially non linear data.
1533 *
1534 * Note: Fragment lists within fragments are not implemented
1535 * at the moment, state->root_skb could be replaced with
1536 * a stack for this purpose.
1537 */
1538unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1539 struct skb_seq_state *st)
1540{
1541 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1542 skb_frag_t *frag;
1543
1544 if (unlikely(abs_offset >= st->upper_offset))
1545 return 0;
1546
1547next_skb:
1548 block_limit = skb_headlen(st->cur_skb);
1549
1550 if (abs_offset < block_limit) {
1551 *data = st->cur_skb->data + abs_offset;
1552 return block_limit - abs_offset;
1553 }
1554
1555 if (st->frag_idx == 0 && !st->frag_data)
1556 st->stepped_offset += skb_headlen(st->cur_skb);
1557
1558 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1559 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1560 block_limit = frag->size + st->stepped_offset;
1561
1562 if (abs_offset < block_limit) {
1563 if (!st->frag_data)
1564 st->frag_data = kmap_skb_frag(frag);
1565
1566 *data = (u8 *) st->frag_data + frag->page_offset +
1567 (abs_offset - st->stepped_offset);
1568
1569 return block_limit - abs_offset;
1570 }
1571
1572 if (st->frag_data) {
1573 kunmap_skb_frag(st->frag_data);
1574 st->frag_data = NULL;
1575 }
1576
1577 st->frag_idx++;
1578 st->stepped_offset += frag->size;
1579 }
1580
1581 if (st->cur_skb->next) {
1582 st->cur_skb = st->cur_skb->next;
1583 st->frag_idx = 0;
1584 goto next_skb;
1585 } else if (st->root_skb == st->cur_skb &&
1586 skb_shinfo(st->root_skb)->frag_list) {
1587 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1588 goto next_skb;
1589 }
1590
1591 return 0;
1592}
1593
1594/**
1595 * skb_abort_seq_read - Abort a sequential read of skb data
1596 * @st: state variable
1597 *
1598 * Must be called if skb_seq_read() was not called until it
1599 * returned 0.
1600 */
1601void skb_abort_seq_read(struct skb_seq_state *st)
1602{
1603 if (st->frag_data)
1604 kunmap_skb_frag(st->frag_data);
1605}
1606
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001607#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1608
1609static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1610 struct ts_config *conf,
1611 struct ts_state *state)
1612{
1613 return skb_seq_read(offset, text, TS_SKB_CB(state));
1614}
1615
1616static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1617{
1618 skb_abort_seq_read(TS_SKB_CB(state));
1619}
1620
1621/**
1622 * skb_find_text - Find a text pattern in skb data
1623 * @skb: the buffer to look in
1624 * @from: search offset
1625 * @to: search limit
1626 * @config: textsearch configuration
1627 * @state: uninitialized textsearch state variable
1628 *
1629 * Finds a pattern in the skb data according to the specified
1630 * textsearch configuration. Use textsearch_next() to retrieve
1631 * subsequent occurrences of the pattern. Returns the offset
1632 * to the first occurrence or UINT_MAX if no match was found.
1633 */
1634unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1635 unsigned int to, struct ts_config *config,
1636 struct ts_state *state)
1637{
1638 config->get_next_block = skb_ts_get_next_block;
1639 config->finish = skb_ts_finish;
1640
1641 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1642
1643 return textsearch_find(config, state);
1644}
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646void __init skb_init(void)
1647{
1648 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1649 sizeof(struct sk_buff),
1650 0,
1651 SLAB_HWCACHE_ALIGN,
1652 NULL, NULL);
1653 if (!skbuff_head_cache)
1654 panic("cannot create skbuff cache");
1655}
1656
1657EXPORT_SYMBOL(___pskb_trim);
1658EXPORT_SYMBOL(__kfree_skb);
1659EXPORT_SYMBOL(__pskb_pull_tail);
1660EXPORT_SYMBOL(alloc_skb);
1661EXPORT_SYMBOL(pskb_copy);
1662EXPORT_SYMBOL(pskb_expand_head);
1663EXPORT_SYMBOL(skb_checksum);
1664EXPORT_SYMBOL(skb_clone);
1665EXPORT_SYMBOL(skb_clone_fraglist);
1666EXPORT_SYMBOL(skb_copy);
1667EXPORT_SYMBOL(skb_copy_and_csum_bits);
1668EXPORT_SYMBOL(skb_copy_and_csum_dev);
1669EXPORT_SYMBOL(skb_copy_bits);
1670EXPORT_SYMBOL(skb_copy_expand);
1671EXPORT_SYMBOL(skb_over_panic);
1672EXPORT_SYMBOL(skb_pad);
1673EXPORT_SYMBOL(skb_realloc_headroom);
1674EXPORT_SYMBOL(skb_under_panic);
1675EXPORT_SYMBOL(skb_dequeue);
1676EXPORT_SYMBOL(skb_dequeue_tail);
1677EXPORT_SYMBOL(skb_insert);
1678EXPORT_SYMBOL(skb_queue_purge);
1679EXPORT_SYMBOL(skb_queue_head);
1680EXPORT_SYMBOL(skb_queue_tail);
1681EXPORT_SYMBOL(skb_unlink);
1682EXPORT_SYMBOL(skb_append);
1683EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07001684EXPORT_SYMBOL(skb_prepare_seq_read);
1685EXPORT_SYMBOL(skb_seq_read);
1686EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001687EXPORT_SYMBOL(skb_find_text);