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