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