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