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