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