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