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