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