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