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