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