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