]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/core/skbuff.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[net-next-2.6.git] / net / core / skbuff.c
1 /*
2  *      Routines having to do with the 'struct sk_buff' memory handlers.
3  *
4  *      Authors:        Alan Cox <iiitac@pyr.swan.ac.uk>
5  *                      Florian La Roche <rzsfl@rz.uni-sb.de>
6  *
7  *      Version:        $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
8  *
9  *      Fixes:
10  *              Alan Cox        :       Fixed the worst of the load
11  *                                      balancer bugs.
12  *              Dave Platt      :       Interrupt stacking fix.
13  *      Richard Kooijman        :       Timestamp fixes.
14  *              Alan Cox        :       Changed buffer format.
15  *              Alan Cox        :       destructor hook for AF_UNIX etc.
16  *              Linus Torvalds  :       Better skb_clone.
17  *              Alan Cox        :       Added skb_copy.
18  *              Alan Cox        :       Added all the changed routines Linus
19  *                                      only put in the headers
20  *              Ray VanTassle   :       Fixed --skb->lock in free
21  *              Alan Cox        :       skb_copy copy arp field
22  *              Andi Kleen      :       slabified it.
23  *              Robert Olsson   :       Removed skb_head_pool
24  *
25  *      NOTE:
26  *              The __skb_ routines should be called with interrupts
27  *      disabled, or you better be *real* sure that the operation is atomic
28  *      with respect to whatever list is being frobbed (e.g. via lock_sock()
29  *      or via disabling bottom half handlers, etc).
30  *
31  *      This program is free software; you can redistribute it and/or
32  *      modify it under the terms of the GNU General Public License
33  *      as published by the Free Software Foundation; either version
34  *      2 of the License, or (at your option) any later version.
35  */
36
37 /*
38  *      The functions in this file will not compile correctly with gcc 2.4.x
39  */
40
41 #include <linux/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 oldheadroom = skb_headroom(skb);
740         int head_copy_len, head_copy_off;
741         int off = 0;
742
743         if (!n)
744                 return NULL;
745
746         skb_reserve(n, newheadroom);
747
748         /* Set the tail pointer and length */
749         skb_put(n, skb->len);
750
751         head_copy_len = oldheadroom;
752         head_copy_off = 0;
753         if (newheadroom <= head_copy_len)
754                 head_copy_len = newheadroom;
755         else
756                 head_copy_off = newheadroom - head_copy_len;
757
758         /* Copy the linear header and data. */
759         if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
760                           skb->len + head_copy_len))
761                 BUG();
762
763         copy_skb_header(n, skb);
764
765 #ifdef NET_SKBUFF_DATA_USES_OFFSET
766         off                  = newheadroom - oldheadroom;
767 #endif
768         n->transport_header += off;
769         n->network_header   += off;
770         n->mac_header       += off;
771
772         return n;
773 }
774
775 /**
776  *      skb_pad                 -       zero pad the tail of an skb
777  *      @skb: buffer to pad
778  *      @pad: space to pad
779  *
780  *      Ensure that a buffer is followed by a padding area that is zero
781  *      filled. Used by network drivers which may DMA or transfer data
782  *      beyond the buffer end onto the wire.
783  *
784  *      May return error in out of memory cases. The skb is freed on error.
785  */
786
787 int skb_pad(struct sk_buff *skb, int pad)
788 {
789         int err;
790         int ntail;
791
792         /* If the skbuff is non linear tailroom is always zero.. */
793         if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
794                 memset(skb->data+skb->len, 0, pad);
795                 return 0;
796         }
797
798         ntail = skb->data_len + pad - (skb->end - skb->tail);
799         if (likely(skb_cloned(skb) || ntail > 0)) {
800                 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
801                 if (unlikely(err))
802                         goto free_skb;
803         }
804
805         /* FIXME: The use of this function with non-linear skb's really needs
806          * to be audited.
807          */
808         err = skb_linearize(skb);
809         if (unlikely(err))
810                 goto free_skb;
811
812         memset(skb->data + skb->len, 0, pad);
813         return 0;
814
815 free_skb:
816         kfree_skb(skb);
817         return err;
818 }
819
820 /* Trims skb to length len. It can change skb pointers.
821  */
822
823 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
824 {
825         struct sk_buff **fragp;
826         struct sk_buff *frag;
827         int offset = skb_headlen(skb);
828         int nfrags = skb_shinfo(skb)->nr_frags;
829         int i;
830         int err;
831
832         if (skb_cloned(skb) &&
833             unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
834                 return err;
835
836         i = 0;
837         if (offset >= len)
838                 goto drop_pages;
839
840         for (; i < nfrags; i++) {
841                 int end = offset + skb_shinfo(skb)->frags[i].size;
842
843                 if (end < len) {
844                         offset = end;
845                         continue;
846                 }
847
848                 skb_shinfo(skb)->frags[i++].size = len - offset;
849
850 drop_pages:
851                 skb_shinfo(skb)->nr_frags = i;
852
853                 for (; i < nfrags; i++)
854                         put_page(skb_shinfo(skb)->frags[i].page);
855
856                 if (skb_shinfo(skb)->frag_list)
857                         skb_drop_fraglist(skb);
858                 goto done;
859         }
860
861         for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
862              fragp = &frag->next) {
863                 int end = offset + frag->len;
864
865                 if (skb_shared(frag)) {
866                         struct sk_buff *nfrag;
867
868                         nfrag = skb_clone(frag, GFP_ATOMIC);
869                         if (unlikely(!nfrag))
870                                 return -ENOMEM;
871
872                         nfrag->next = frag->next;
873                         kfree_skb(frag);
874                         frag = nfrag;
875                         *fragp = frag;
876                 }
877
878                 if (end < len) {
879                         offset = end;
880                         continue;
881                 }
882
883                 if (end > len &&
884                     unlikely((err = pskb_trim(frag, len - offset))))
885                         return err;
886
887                 if (frag->next)
888                         skb_drop_list(&frag->next);
889                 break;
890         }
891
892 done:
893         if (len > skb_headlen(skb)) {
894                 skb->data_len -= skb->len - len;
895                 skb->len       = len;
896         } else {
897                 skb->len       = len;
898                 skb->data_len  = 0;
899                 skb_set_tail_pointer(skb, len);
900         }
901
902         return 0;
903 }
904
905 /**
906  *      __pskb_pull_tail - advance tail of skb header
907  *      @skb: buffer to reallocate
908  *      @delta: number of bytes to advance tail
909  *
910  *      The function makes a sense only on a fragmented &sk_buff,
911  *      it expands header moving its tail forward and copying necessary
912  *      data from fragmented part.
913  *
914  *      &sk_buff MUST have reference count of 1.
915  *
916  *      Returns %NULL (and &sk_buff does not change) if pull failed
917  *      or value of new tail of skb in the case of success.
918  *
919  *      All the pointers pointing into skb header may change and must be
920  *      reloaded after call to this function.
921  */
922
923 /* Moves tail of skb head forward, copying data from fragmented part,
924  * when it is necessary.
925  * 1. It may fail due to malloc failure.
926  * 2. It may change skb pointers.
927  *
928  * It is pretty complicated. Luckily, it is called only in exceptional cases.
929  */
930 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
931 {
932         /* If skb has not enough free space at tail, get new one
933          * plus 128 bytes for future expansions. If we have enough
934          * room at tail, reallocate without expansion only if skb is cloned.
935          */
936         int i, k, eat = (skb->tail + delta) - skb->end;
937
938         if (eat > 0 || skb_cloned(skb)) {
939                 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
940                                      GFP_ATOMIC))
941                         return NULL;
942         }
943
944         if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
945                 BUG();
946
947         /* Optimization: no fragments, no reasons to preestimate
948          * size of pulled pages. Superb.
949          */
950         if (!skb_shinfo(skb)->frag_list)
951                 goto pull_pages;
952
953         /* Estimate size of pulled pages. */
954         eat = delta;
955         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
956                 if (skb_shinfo(skb)->frags[i].size >= eat)
957                         goto pull_pages;
958                 eat -= skb_shinfo(skb)->frags[i].size;
959         }
960
961         /* If we need update frag list, we are in troubles.
962          * Certainly, it possible to add an offset to skb data,
963          * but taking into account that pulling is expected to
964          * be very rare operation, it is worth to fight against
965          * further bloating skb head and crucify ourselves here instead.
966          * Pure masohism, indeed. 8)8)
967          */
968         if (eat) {
969                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
970                 struct sk_buff *clone = NULL;
971                 struct sk_buff *insp = NULL;
972
973                 do {
974                         BUG_ON(!list);
975
976                         if (list->len <= eat) {
977                                 /* Eaten as whole. */
978                                 eat -= list->len;
979                                 list = list->next;
980                                 insp = list;
981                         } else {
982                                 /* Eaten partially. */
983
984                                 if (skb_shared(list)) {
985                                         /* Sucks! We need to fork list. :-( */
986                                         clone = skb_clone(list, GFP_ATOMIC);
987                                         if (!clone)
988                                                 return NULL;
989                                         insp = list->next;
990                                         list = clone;
991                                 } else {
992                                         /* This may be pulled without
993                                          * problems. */
994                                         insp = list;
995                                 }
996                                 if (!pskb_pull(list, eat)) {
997                                         if (clone)
998                                                 kfree_skb(clone);
999                                         return NULL;
1000                                 }
1001                                 break;
1002                         }
1003                 } while (eat);
1004
1005                 /* Free pulled out fragments. */
1006                 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1007                         skb_shinfo(skb)->frag_list = list->next;
1008                         kfree_skb(list);
1009                 }
1010                 /* And insert new clone at head. */
1011                 if (clone) {
1012                         clone->next = list;
1013                         skb_shinfo(skb)->frag_list = clone;
1014                 }
1015         }
1016         /* Success! Now we may commit changes to skb data. */
1017
1018 pull_pages:
1019         eat = delta;
1020         k = 0;
1021         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1022                 if (skb_shinfo(skb)->frags[i].size <= eat) {
1023                         put_page(skb_shinfo(skb)->frags[i].page);
1024                         eat -= skb_shinfo(skb)->frags[i].size;
1025                 } else {
1026                         skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1027                         if (eat) {
1028                                 skb_shinfo(skb)->frags[k].page_offset += eat;
1029                                 skb_shinfo(skb)->frags[k].size -= eat;
1030                                 eat = 0;
1031                         }
1032                         k++;
1033                 }
1034         }
1035         skb_shinfo(skb)->nr_frags = k;
1036
1037         skb->tail     += delta;
1038         skb->data_len -= delta;
1039
1040         return skb_tail_pointer(skb);
1041 }
1042
1043 /* Copy some data bits from skb to kernel buffer. */
1044
1045 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1046 {
1047         int i, copy;
1048         int end = skb_headlen(skb);
1049
1050         if (offset > (int)skb->len - len)
1051                 goto fault;
1052
1053         /* Copy header. */
1054         if ((copy = end - offset) > 0) {
1055                 if (copy > len)
1056                         copy = len;
1057                 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1058                 if ((len -= copy) == 0)
1059                         return 0;
1060                 offset += copy;
1061                 to     += copy;
1062         }
1063
1064         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1065                 BUG_TRAP(len >= 0);
1066
1067                 end = offset + skb_shinfo(skb)->frags[i].size;
1068                 if ((copy = end - offset) > 0) {
1069                         u8 *vaddr;
1070
1071                         if (copy > len)
1072                                 copy = len;
1073
1074                         vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1075                         memcpy(to,
1076                                vaddr + skb_shinfo(skb)->frags[i].page_offset,
1077                                copy);
1078                         kunmap_skb_frag(vaddr);
1079
1080                         if ((len -= copy) == 0)
1081                                 return 0;
1082                         offset += copy;
1083                         to     += copy;
1084                 }
1085         }
1086
1087         if (skb_shinfo(skb)->frag_list) {
1088                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1089
1090                 for (; list; list = list->next) {
1091                         BUG_TRAP(len >= 0);
1092
1093                         end = offset + list->len;
1094                         if ((copy = end - offset) > 0) {
1095                                 if (copy > len)
1096                                         copy = len;
1097                                 if (skb_copy_bits(list, 0, to, copy))
1098                                         goto fault;
1099                                 if ((len -= copy) == 0)
1100                                         return 0;
1101                                 offset += copy;
1102                                 to     += copy;
1103                         }
1104                 }
1105         }
1106         if (!len)
1107                 return 0;
1108
1109 fault:
1110         return -EFAULT;
1111 }
1112
1113 /**
1114  *      skb_store_bits - store bits from kernel buffer to skb
1115  *      @skb: destination buffer
1116  *      @offset: offset in destination
1117  *      @from: source buffer
1118  *      @len: number of bytes to copy
1119  *
1120  *      Copy the specified number of bytes from the source buffer to the
1121  *      destination skb.  This function handles all the messy bits of
1122  *      traversing fragment lists and such.
1123  */
1124
1125 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1126 {
1127         int i, copy;
1128         int end = skb_headlen(skb);
1129
1130         if (offset > (int)skb->len - len)
1131                 goto fault;
1132
1133         if ((copy = end - offset) > 0) {
1134                 if (copy > len)
1135                         copy = len;
1136                 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1137                 if ((len -= copy) == 0)
1138                         return 0;
1139                 offset += copy;
1140                 from += copy;
1141         }
1142
1143         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1144                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1145                 BUG_TRAP(len >= 0);
1146
1147                 end = offset + 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, from, copy);
1156                         kunmap_skb_frag(vaddr);
1157
1158                         if ((len -= copy) == 0)
1159                                 return 0;
1160                         offset += copy;
1161                         from += copy;
1162                 }
1163         }
1164
1165         if (skb_shinfo(skb)->frag_list) {
1166                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1167
1168                 for (; list; list = list->next) {
1169                         BUG_TRAP(len >= 0);
1170
1171                         end = offset + list->len;
1172                         if ((copy = end - offset) > 0) {
1173                                 if (copy > len)
1174                                         copy = len;
1175                                 if (skb_store_bits(list, 0, from, copy))
1176                                         goto fault;
1177                                 if ((len -= copy) == 0)
1178                                         return 0;
1179                                 offset += copy;
1180                                 from += copy;
1181                         }
1182                 }
1183         }
1184         if (!len)
1185                 return 0;
1186
1187 fault:
1188         return -EFAULT;
1189 }
1190
1191 EXPORT_SYMBOL(skb_store_bits);
1192
1193 /* Checksum skb data. */
1194
1195 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1196                           int len, __wsum csum)
1197 {
1198         int end = skb_headlen(skb);
1199         int i, copy = end - offset;
1200         int pos = 0;
1201
1202         /* Checksum header. */
1203         if (copy > 0) {
1204                 if (copy > len)
1205                         copy = len;
1206                 csum = csum_partial(skb->data + offset, copy, csum);
1207                 if ((len -= copy) == 0)
1208                         return csum;
1209                 offset += copy;
1210                 pos     = copy;
1211         }
1212
1213         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1214                 BUG_TRAP(len >= 0);
1215
1216                 end = offset + skb_shinfo(skb)->frags[i].size;
1217                 if ((copy = end - offset) > 0) {
1218                         __wsum csum2;
1219                         u8 *vaddr;
1220                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1221
1222                         if (copy > len)
1223                                 copy = len;
1224                         vaddr = kmap_skb_frag(frag);
1225                         csum2 = csum_partial(vaddr + frag->page_offset,
1226                                              copy, 0);
1227                         kunmap_skb_frag(vaddr);
1228                         csum = csum_block_add(csum, csum2, pos);
1229                         if (!(len -= copy))
1230                                 return csum;
1231                         offset += copy;
1232                         pos    += copy;
1233                 }
1234         }
1235
1236         if (skb_shinfo(skb)->frag_list) {
1237                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1238
1239                 for (; list; list = list->next) {
1240                         BUG_TRAP(len >= 0);
1241
1242                         end = offset + list->len;
1243                         if ((copy = end - offset) > 0) {
1244                                 __wsum csum2;
1245                                 if (copy > len)
1246                                         copy = len;
1247                                 csum2 = skb_checksum(list, 0, copy, 0);
1248                                 csum = csum_block_add(csum, csum2, pos);
1249                                 if ((len -= copy) == 0)
1250                                         return csum;
1251                                 offset += copy;
1252                                 pos    += copy;
1253                         }
1254                 }
1255         }
1256         BUG_ON(len);
1257
1258         return csum;
1259 }
1260
1261 /* Both of above in one bottle. */
1262
1263 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1264                                     u8 *to, int len, __wsum csum)
1265 {
1266         int end = skb_headlen(skb);
1267         int i, copy = end - offset;
1268         int pos = 0;
1269
1270         /* Copy header. */
1271         if (copy > 0) {
1272                 if (copy > len)
1273                         copy = len;
1274                 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1275                                                  copy, csum);
1276                 if ((len -= copy) == 0)
1277                         return csum;
1278                 offset += copy;
1279                 to     += copy;
1280                 pos     = copy;
1281         }
1282
1283         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1284                 BUG_TRAP(len >= 0);
1285
1286                 end = offset + skb_shinfo(skb)->frags[i].size;
1287                 if ((copy = end - offset) > 0) {
1288                         __wsum csum2;
1289                         u8 *vaddr;
1290                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1291
1292                         if (copy > len)
1293                                 copy = len;
1294                         vaddr = kmap_skb_frag(frag);
1295                         csum2 = csum_partial_copy_nocheck(vaddr +
1296                                                           frag->page_offset,
1297                                                           to, copy, 0);
1298                         kunmap_skb_frag(vaddr);
1299                         csum = csum_block_add(csum, csum2, pos);
1300                         if (!(len -= copy))
1301                                 return csum;
1302                         offset += copy;
1303                         to     += copy;
1304                         pos    += copy;
1305                 }
1306         }
1307
1308         if (skb_shinfo(skb)->frag_list) {
1309                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1310
1311                 for (; list; list = list->next) {
1312                         __wsum csum2;
1313                         BUG_TRAP(len >= 0);
1314
1315                         end = offset + list->len;
1316                         if ((copy = end - offset) > 0) {
1317                                 if (copy > len)
1318                                         copy = len;
1319                                 csum2 = skb_copy_and_csum_bits(list, 0,
1320                                                                to, copy, 0);
1321                                 csum = csum_block_add(csum, csum2, pos);
1322                                 if ((len -= copy) == 0)
1323                                         return csum;
1324                                 offset += copy;
1325                                 to     += copy;
1326                                 pos    += copy;
1327                         }
1328                 }
1329         }
1330         BUG_ON(len);
1331         return csum;
1332 }
1333
1334 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1335 {
1336         __wsum csum;
1337         long csstart;
1338
1339         if (skb->ip_summed == CHECKSUM_PARTIAL)
1340                 csstart = skb->csum_start - skb_headroom(skb);
1341         else
1342                 csstart = skb_headlen(skb);
1343
1344         BUG_ON(csstart > skb_headlen(skb));
1345
1346         skb_copy_from_linear_data(skb, to, csstart);
1347
1348         csum = 0;
1349         if (csstart != skb->len)
1350                 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1351                                               skb->len - csstart, 0);
1352
1353         if (skb->ip_summed == CHECKSUM_PARTIAL) {
1354                 long csstuff = csstart + skb->csum_offset;
1355
1356                 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1357         }
1358 }
1359
1360 /**
1361  *      skb_dequeue - remove from the head of the queue
1362  *      @list: list to dequeue from
1363  *
1364  *      Remove the head of the list. The list lock is taken so the function
1365  *      may be used safely with other locking list functions. The head item is
1366  *      returned or %NULL if the list is empty.
1367  */
1368
1369 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1370 {
1371         unsigned long flags;
1372         struct sk_buff *result;
1373
1374         spin_lock_irqsave(&list->lock, flags);
1375         result = __skb_dequeue(list);
1376         spin_unlock_irqrestore(&list->lock, flags);
1377         return result;
1378 }
1379
1380 /**
1381  *      skb_dequeue_tail - remove from the tail of the queue
1382  *      @list: list to dequeue from
1383  *
1384  *      Remove the tail of the list. The list lock is taken so the function
1385  *      may be used safely with other locking list functions. The tail item is
1386  *      returned or %NULL if the list is empty.
1387  */
1388 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1389 {
1390         unsigned long flags;
1391         struct sk_buff *result;
1392
1393         spin_lock_irqsave(&list->lock, flags);
1394         result = __skb_dequeue_tail(list);
1395         spin_unlock_irqrestore(&list->lock, flags);
1396         return result;
1397 }
1398
1399 /**
1400  *      skb_queue_purge - empty a list
1401  *      @list: list to empty
1402  *
1403  *      Delete all buffers on an &sk_buff list. Each buffer is removed from
1404  *      the list and one reference dropped. This function takes the list
1405  *      lock and is atomic with respect to other list locking functions.
1406  */
1407 void skb_queue_purge(struct sk_buff_head *list)
1408 {
1409         struct sk_buff *skb;
1410         while ((skb = skb_dequeue(list)) != NULL)
1411                 kfree_skb(skb);
1412 }
1413
1414 /**
1415  *      skb_queue_head - queue a buffer at the list head
1416  *      @list: list to use
1417  *      @newsk: buffer to queue
1418  *
1419  *      Queue a buffer at the start of the list. This function takes the
1420  *      list lock and can be used safely with other locking &sk_buff functions
1421  *      safely.
1422  *
1423  *      A buffer cannot be placed on two lists at the same time.
1424  */
1425 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1426 {
1427         unsigned long flags;
1428
1429         spin_lock_irqsave(&list->lock, flags);
1430         __skb_queue_head(list, newsk);
1431         spin_unlock_irqrestore(&list->lock, flags);
1432 }
1433
1434 /**
1435  *      skb_queue_tail - queue a buffer at the list tail
1436  *      @list: list to use
1437  *      @newsk: buffer to queue
1438  *
1439  *      Queue a buffer at the tail of the list. This function takes the
1440  *      list lock and can be used safely with other locking &sk_buff functions
1441  *      safely.
1442  *
1443  *      A buffer cannot be placed on two lists at the same time.
1444  */
1445 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1446 {
1447         unsigned long flags;
1448
1449         spin_lock_irqsave(&list->lock, flags);
1450         __skb_queue_tail(list, newsk);
1451         spin_unlock_irqrestore(&list->lock, flags);
1452 }
1453
1454 /**
1455  *      skb_unlink      -       remove a buffer from a list
1456  *      @skb: buffer to remove
1457  *      @list: list to use
1458  *
1459  *      Remove a packet from a list. The list locks are taken and this
1460  *      function is atomic with respect to other list locked calls
1461  *
1462  *      You must know what list the SKB is on.
1463  */
1464 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1465 {
1466         unsigned long flags;
1467
1468         spin_lock_irqsave(&list->lock, flags);
1469         __skb_unlink(skb, list);
1470         spin_unlock_irqrestore(&list->lock, flags);
1471 }
1472
1473 /**
1474  *      skb_append      -       append a buffer
1475  *      @old: buffer to insert after
1476  *      @newsk: buffer to insert
1477  *      @list: list to use
1478  *
1479  *      Place a packet after a given packet in a list. The list locks are taken
1480  *      and this function is atomic with respect to other list locked calls.
1481  *      A buffer cannot be placed on two lists at the same time.
1482  */
1483 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1484 {
1485         unsigned long flags;
1486
1487         spin_lock_irqsave(&list->lock, flags);
1488         __skb_append(old, newsk, list);
1489         spin_unlock_irqrestore(&list->lock, flags);
1490 }
1491
1492
1493 /**
1494  *      skb_insert      -       insert a buffer
1495  *      @old: buffer to insert before
1496  *      @newsk: buffer to insert
1497  *      @list: list to use
1498  *
1499  *      Place a packet before a given packet in a list. The list locks are
1500  *      taken and this function is atomic with respect to other list locked
1501  *      calls.
1502  *
1503  *      A buffer cannot be placed on two lists at the same time.
1504  */
1505 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1506 {
1507         unsigned long flags;
1508
1509         spin_lock_irqsave(&list->lock, flags);
1510         __skb_insert(newsk, old->prev, old, list);
1511         spin_unlock_irqrestore(&list->lock, flags);
1512 }
1513
1514 static inline void skb_split_inside_header(struct sk_buff *skb,
1515                                            struct sk_buff* skb1,
1516                                            const u32 len, const int pos)
1517 {
1518         int i;
1519
1520         skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1521                                          pos - len);
1522         /* And move data appendix as is. */
1523         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1524                 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1525
1526         skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1527         skb_shinfo(skb)->nr_frags  = 0;
1528         skb1->data_len             = skb->data_len;
1529         skb1->len                  += skb1->data_len;
1530         skb->data_len              = 0;
1531         skb->len                   = len;
1532         skb_set_tail_pointer(skb, len);
1533 }
1534
1535 static inline void skb_split_no_header(struct sk_buff *skb,
1536                                        struct sk_buff* skb1,
1537                                        const u32 len, int pos)
1538 {
1539         int i, k = 0;
1540         const int nfrags = skb_shinfo(skb)->nr_frags;
1541
1542         skb_shinfo(skb)->nr_frags = 0;
1543         skb1->len                 = skb1->data_len = skb->len - len;
1544         skb->len                  = len;
1545         skb->data_len             = len - pos;
1546
1547         for (i = 0; i < nfrags; i++) {
1548                 int size = skb_shinfo(skb)->frags[i].size;
1549
1550                 if (pos + size > len) {
1551                         skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1552
1553                         if (pos < len) {
1554                                 /* Split frag.
1555                                  * We have two variants in this case:
1556                                  * 1. Move all the frag to the second
1557                                  *    part, if it is possible. F.e.
1558                                  *    this approach is mandatory for TUX,
1559                                  *    where splitting is expensive.
1560                                  * 2. Split is accurately. We make this.
1561                                  */
1562                                 get_page(skb_shinfo(skb)->frags[i].page);
1563                                 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1564                                 skb_shinfo(skb1)->frags[0].size -= len - pos;
1565                                 skb_shinfo(skb)->frags[i].size  = len - pos;
1566                                 skb_shinfo(skb)->nr_frags++;
1567                         }
1568                         k++;
1569                 } else
1570                         skb_shinfo(skb)->nr_frags++;
1571                 pos += size;
1572         }
1573         skb_shinfo(skb1)->nr_frags = k;
1574 }
1575
1576 /**
1577  * skb_split - Split fragmented skb to two parts at length len.
1578  * @skb: the buffer to split
1579  * @skb1: the buffer to receive the second part
1580  * @len: new length for skb
1581  */
1582 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1583 {
1584         int pos = skb_headlen(skb);
1585
1586         if (len < pos)  /* Split line is inside header. */
1587                 skb_split_inside_header(skb, skb1, len, pos);
1588         else            /* Second chunk has no header, nothing to copy. */
1589                 skb_split_no_header(skb, skb1, len, pos);
1590 }
1591
1592 /**
1593  * skb_prepare_seq_read - Prepare a sequential read of skb data
1594  * @skb: the buffer to read
1595  * @from: lower offset of data to be read
1596  * @to: upper offset of data to be read
1597  * @st: state variable
1598  *
1599  * Initializes the specified state variable. Must be called before
1600  * invoking skb_seq_read() for the first time.
1601  */
1602 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1603                           unsigned int to, struct skb_seq_state *st)
1604 {
1605         st->lower_offset = from;
1606         st->upper_offset = to;
1607         st->root_skb = st->cur_skb = skb;
1608         st->frag_idx = st->stepped_offset = 0;
1609         st->frag_data = NULL;
1610 }
1611
1612 /**
1613  * skb_seq_read - Sequentially read skb data
1614  * @consumed: number of bytes consumed by the caller so far
1615  * @data: destination pointer for data to be returned
1616  * @st: state variable
1617  *
1618  * Reads a block of skb data at &consumed relative to the
1619  * lower offset specified to skb_prepare_seq_read(). Assigns
1620  * the head of the data block to &data and returns the length
1621  * of the block or 0 if the end of the skb data or the upper
1622  * offset has been reached.
1623  *
1624  * The caller is not required to consume all of the data
1625  * returned, i.e. &consumed is typically set to the number
1626  * of bytes already consumed and the next call to
1627  * skb_seq_read() will return the remaining part of the block.
1628  *
1629  * Note: The size of each block of data returned can be arbitary,
1630  *       this limitation is the cost for zerocopy seqeuental
1631  *       reads of potentially non linear data.
1632  *
1633  * Note: Fragment lists within fragments are not implemented
1634  *       at the moment, state->root_skb could be replaced with
1635  *       a stack for this purpose.
1636  */
1637 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1638                           struct skb_seq_state *st)
1639 {
1640         unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1641         skb_frag_t *frag;
1642
1643         if (unlikely(abs_offset >= st->upper_offset))
1644                 return 0;
1645
1646 next_skb:
1647         block_limit = skb_headlen(st->cur_skb);
1648
1649         if (abs_offset < block_limit) {
1650                 *data = st->cur_skb->data + abs_offset;
1651                 return block_limit - abs_offset;
1652         }
1653
1654         if (st->frag_idx == 0 && !st->frag_data)
1655                 st->stepped_offset += skb_headlen(st->cur_skb);
1656
1657         while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1658                 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1659                 block_limit = frag->size + st->stepped_offset;
1660
1661                 if (abs_offset < block_limit) {
1662                         if (!st->frag_data)
1663                                 st->frag_data = kmap_skb_frag(frag);
1664
1665                         *data = (u8 *) st->frag_data + frag->page_offset +
1666                                 (abs_offset - st->stepped_offset);
1667
1668                         return block_limit - abs_offset;
1669                 }
1670
1671                 if (st->frag_data) {
1672                         kunmap_skb_frag(st->frag_data);
1673                         st->frag_data = NULL;
1674                 }
1675
1676                 st->frag_idx++;
1677                 st->stepped_offset += frag->size;
1678         }
1679
1680         if (st->cur_skb->next) {
1681                 st->cur_skb = st->cur_skb->next;
1682                 st->frag_idx = 0;
1683                 goto next_skb;
1684         } else if (st->root_skb == st->cur_skb &&
1685                    skb_shinfo(st->root_skb)->frag_list) {
1686                 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1687                 goto next_skb;
1688         }
1689
1690         return 0;
1691 }
1692
1693 /**
1694  * skb_abort_seq_read - Abort a sequential read of skb data
1695  * @st: state variable
1696  *
1697  * Must be called if skb_seq_read() was not called until it
1698  * returned 0.
1699  */
1700 void skb_abort_seq_read(struct skb_seq_state *st)
1701 {
1702         if (st->frag_data)
1703                 kunmap_skb_frag(st->frag_data);
1704 }
1705
1706 #define TS_SKB_CB(state)        ((struct skb_seq_state *) &((state)->cb))
1707
1708 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1709                                           struct ts_config *conf,
1710                                           struct ts_state *state)
1711 {
1712         return skb_seq_read(offset, text, TS_SKB_CB(state));
1713 }
1714
1715 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1716 {
1717         skb_abort_seq_read(TS_SKB_CB(state));
1718 }
1719
1720 /**
1721  * skb_find_text - Find a text pattern in skb data
1722  * @skb: the buffer to look in
1723  * @from: search offset
1724  * @to: search limit
1725  * @config: textsearch configuration
1726  * @state: uninitialized textsearch state variable
1727  *
1728  * Finds a pattern in the skb data according to the specified
1729  * textsearch configuration. Use textsearch_next() to retrieve
1730  * subsequent occurrences of the pattern. Returns the offset
1731  * to the first occurrence or UINT_MAX if no match was found.
1732  */
1733 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1734                            unsigned int to, struct ts_config *config,
1735                            struct ts_state *state)
1736 {
1737         unsigned int ret;
1738
1739         config->get_next_block = skb_ts_get_next_block;
1740         config->finish = skb_ts_finish;
1741
1742         skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1743
1744         ret = textsearch_find(config, state);
1745         return (ret <= to - from ? ret : UINT_MAX);
1746 }
1747
1748 /**
1749  * skb_append_datato_frags: - append the user data to a skb
1750  * @sk: sock  structure
1751  * @skb: skb structure to be appened with user data.
1752  * @getfrag: call back function to be used for getting the user data
1753  * @from: pointer to user message iov
1754  * @length: length of the iov message
1755  *
1756  * Description: This procedure append the user data in the fragment part
1757  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
1758  */
1759 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1760                         int (*getfrag)(void *from, char *to, int offset,
1761                                         int len, int odd, struct sk_buff *skb),
1762                         void *from, int length)
1763 {
1764         int frg_cnt = 0;
1765         skb_frag_t *frag = NULL;
1766         struct page *page = NULL;
1767         int copy, left;
1768         int offset = 0;
1769         int ret;
1770
1771         do {
1772                 /* Return error if we don't have space for new frag */
1773                 frg_cnt = skb_shinfo(skb)->nr_frags;
1774                 if (frg_cnt >= MAX_SKB_FRAGS)
1775                         return -EFAULT;
1776
1777                 /* allocate a new page for next frag */
1778                 page = alloc_pages(sk->sk_allocation, 0);
1779
1780                 /* If alloc_page fails just return failure and caller will
1781                  * free previous allocated pages by doing kfree_skb()
1782                  */
1783                 if (page == NULL)
1784                         return -ENOMEM;
1785
1786                 /* initialize the next frag */
1787                 sk->sk_sndmsg_page = page;
1788                 sk->sk_sndmsg_off = 0;
1789                 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1790                 skb->truesize += PAGE_SIZE;
1791                 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1792
1793                 /* get the new initialized frag */
1794                 frg_cnt = skb_shinfo(skb)->nr_frags;
1795                 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1796
1797                 /* copy the user data to page */
1798                 left = PAGE_SIZE - frag->page_offset;
1799                 copy = (length > left)? left : length;
1800
1801                 ret = getfrag(from, (page_address(frag->page) +
1802                             frag->page_offset + frag->size),
1803                             offset, copy, 0, skb);
1804                 if (ret < 0)
1805                         return -EFAULT;
1806
1807                 /* copy was successful so update the size parameters */
1808                 sk->sk_sndmsg_off += copy;
1809                 frag->size += copy;
1810                 skb->len += copy;
1811                 skb->data_len += copy;
1812                 offset += copy;
1813                 length -= copy;
1814
1815         } while (length > 0);
1816
1817         return 0;
1818 }
1819
1820 /**
1821  *      skb_pull_rcsum - pull skb and update receive checksum
1822  *      @skb: buffer to update
1823  *      @start: start of data before pull
1824  *      @len: length of data pulled
1825  *
1826  *      This function performs an skb_pull on the packet and updates
1827  *      update the CHECKSUM_COMPLETE checksum.  It should be used on
1828  *      receive path processing instead of skb_pull unless you know
1829  *      that the checksum difference is zero (e.g., a valid IP header)
1830  *      or you are setting ip_summed to CHECKSUM_NONE.
1831  */
1832 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1833 {
1834         BUG_ON(len > skb->len);
1835         skb->len -= len;
1836         BUG_ON(skb->len < skb->data_len);
1837         skb_postpull_rcsum(skb, skb->data, len);
1838         return skb->data += len;
1839 }
1840
1841 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1842
1843 /**
1844  *      skb_segment - Perform protocol segmentation on skb.
1845  *      @skb: buffer to segment
1846  *      @features: features for the output path (see dev->features)
1847  *
1848  *      This function performs segmentation on the given skb.  It returns
1849  *      the segment at the given position.  It returns NULL if there are
1850  *      no more segments to generate, or when an error is encountered.
1851  */
1852 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1853 {
1854         struct sk_buff *segs = NULL;
1855         struct sk_buff *tail = NULL;
1856         unsigned int mss = skb_shinfo(skb)->gso_size;
1857         unsigned int doffset = skb->data - skb_mac_header(skb);
1858         unsigned int offset = doffset;
1859         unsigned int headroom;
1860         unsigned int len;
1861         int sg = features & NETIF_F_SG;
1862         int nfrags = skb_shinfo(skb)->nr_frags;
1863         int err = -ENOMEM;
1864         int i = 0;
1865         int pos;
1866
1867         __skb_push(skb, doffset);
1868         headroom = skb_headroom(skb);
1869         pos = skb_headlen(skb);
1870
1871         do {
1872                 struct sk_buff *nskb;
1873                 skb_frag_t *frag;
1874                 int hsize;
1875                 int k;
1876                 int size;
1877
1878                 len = skb->len - offset;
1879                 if (len > mss)
1880                         len = mss;
1881
1882                 hsize = skb_headlen(skb) - offset;
1883                 if (hsize < 0)
1884                         hsize = 0;
1885                 if (hsize > len || !sg)
1886                         hsize = len;
1887
1888                 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
1889                 if (unlikely(!nskb))
1890                         goto err;
1891
1892                 if (segs)
1893                         tail->next = nskb;
1894                 else
1895                         segs = nskb;
1896                 tail = nskb;
1897
1898                 nskb->dev = skb->dev;
1899                 nskb->priority = skb->priority;
1900                 nskb->protocol = skb->protocol;
1901                 nskb->dst = dst_clone(skb->dst);
1902                 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1903                 nskb->pkt_type = skb->pkt_type;
1904                 nskb->mac_len = skb->mac_len;
1905
1906                 skb_reserve(nskb, headroom);
1907                 skb_reset_mac_header(nskb);
1908                 skb_set_network_header(nskb, skb->mac_len);
1909                 nskb->transport_header = (nskb->network_header +
1910                                           skb_network_header_len(skb));
1911                 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
1912                                           doffset);
1913                 if (!sg) {
1914                         nskb->csum = skb_copy_and_csum_bits(skb, offset,
1915                                                             skb_put(nskb, len),
1916                                                             len, 0);
1917                         continue;
1918                 }
1919
1920                 frag = skb_shinfo(nskb)->frags;
1921                 k = 0;
1922
1923                 nskb->ip_summed = CHECKSUM_PARTIAL;
1924                 nskb->csum = skb->csum;
1925                 skb_copy_from_linear_data_offset(skb, offset,
1926                                                  skb_put(nskb, hsize), hsize);
1927
1928                 while (pos < offset + len) {
1929                         BUG_ON(i >= nfrags);
1930
1931                         *frag = skb_shinfo(skb)->frags[i];
1932                         get_page(frag->page);
1933                         size = frag->size;
1934
1935                         if (pos < offset) {
1936                                 frag->page_offset += offset - pos;
1937                                 frag->size -= offset - pos;
1938                         }
1939
1940                         k++;
1941
1942                         if (pos + size <= offset + len) {
1943                                 i++;
1944                                 pos += size;
1945                         } else {
1946                                 frag->size -= pos + size - (offset + len);
1947                                 break;
1948                         }
1949
1950                         frag++;
1951                 }
1952
1953                 skb_shinfo(nskb)->nr_frags = k;
1954                 nskb->data_len = len - hsize;
1955                 nskb->len += nskb->data_len;
1956                 nskb->truesize += nskb->data_len;
1957         } while ((offset += len) < skb->len);
1958
1959         return segs;
1960
1961 err:
1962         while ((skb = segs)) {
1963                 segs = skb->next;
1964                 kfree_skb(skb);
1965         }
1966         return ERR_PTR(err);
1967 }
1968
1969 EXPORT_SYMBOL_GPL(skb_segment);
1970
1971 void __init skb_init(void)
1972 {
1973         skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1974                                               sizeof(struct sk_buff),
1975                                               0,
1976                                               SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1977                                               NULL, NULL);
1978         skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1979                                                 (2*sizeof(struct sk_buff)) +
1980                                                 sizeof(atomic_t),
1981                                                 0,
1982                                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1983                                                 NULL, NULL);
1984 }
1985
1986 /**
1987  *      skb_to_sgvec - Fill a scatter-gather list from a socket buffer
1988  *      @skb: Socket buffer containing the buffers to be mapped
1989  *      @sg: The scatter-gather list to map into
1990  *      @offset: The offset into the buffer's contents to start mapping
1991  *      @len: Length of buffer space to be mapped
1992  *
1993  *      Fill the specified scatter-gather list with mappings/pointers into a
1994  *      region of the buffer space attached to a socket buffer.
1995  */
1996 int
1997 skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
1998 {
1999         int end = skb_headlen(skb);
2000         int i, copy = end - offset;
2001         int elt = 0;
2002
2003         if (copy > 0) {
2004                 if (copy > len)
2005                         copy = len;
2006                 sg[elt].page = virt_to_page(skb->data + offset);
2007                 sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
2008                 sg[elt].length = copy;
2009                 elt++;
2010                 if ((len -= copy) == 0)
2011                         return elt;
2012                 offset += copy;
2013         }
2014
2015         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2016                 BUG_TRAP(len >= 0);
2017
2018                 end = offset + skb_shinfo(skb)->frags[i].size;
2019                 if ((copy = end - offset) > 0) {
2020                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2021
2022                         if (copy > len)
2023                                 copy = len;
2024                         sg[elt].page = frag->page;
2025                         sg[elt].offset = frag->page_offset;
2026                         sg[elt].length = copy;
2027                         elt++;
2028                         if (!(len -= copy))
2029                                 return elt;
2030                         offset += copy;
2031                 }
2032         }
2033
2034         if (skb_shinfo(skb)->frag_list) {
2035                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2036
2037                 for (; list; list = list->next) {
2038                         BUG_TRAP(len >= 0);
2039
2040                         end = offset + list->len;
2041                         if ((copy = end - offset) > 0) {
2042                                 if (copy > len)
2043                                         copy = len;
2044                                 elt += skb_to_sgvec(list, sg+elt, 0, copy);
2045                                 if ((len -= copy) == 0)
2046                                         return elt;
2047                                 offset += copy;
2048                         }
2049                 }
2050         }
2051         BUG_ON(len);
2052         return elt;
2053 }
2054
2055 /**
2056  *      skb_cow_data - Check that a socket buffer's data buffers are writable
2057  *      @skb: The socket buffer to check.
2058  *      @tailbits: Amount of trailing space to be added
2059  *      @trailer: Returned pointer to the skb where the @tailbits space begins
2060  *
2061  *      Make sure that the data buffers attached to a socket buffer are
2062  *      writable. If they are not, private copies are made of the data buffers
2063  *      and the socket buffer is set to use these instead.
2064  *
2065  *      If @tailbits is given, make sure that there is space to write @tailbits
2066  *      bytes of data beyond current end of socket buffer.  @trailer will be
2067  *      set to point to the skb in which this space begins.
2068  *
2069  *      The number of scatterlist elements required to completely map the
2070  *      COW'd and extended socket buffer will be returned.
2071  */
2072 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2073 {
2074         int copyflag;
2075         int elt;
2076         struct sk_buff *skb1, **skb_p;
2077
2078         /* If skb is cloned or its head is paged, reallocate
2079          * head pulling out all the pages (pages are considered not writable
2080          * at the moment even if they are anonymous).
2081          */
2082         if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2083             __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2084                 return -ENOMEM;
2085
2086         /* Easy case. Most of packets will go this way. */
2087         if (!skb_shinfo(skb)->frag_list) {
2088                 /* A little of trouble, not enough of space for trailer.
2089                  * This should not happen, when stack is tuned to generate
2090                  * good frames. OK, on miss we reallocate and reserve even more
2091                  * space, 128 bytes is fair. */
2092
2093                 if (skb_tailroom(skb) < tailbits &&
2094                     pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2095                         return -ENOMEM;
2096
2097                 /* Voila! */
2098                 *trailer = skb;
2099                 return 1;
2100         }
2101
2102         /* Misery. We are in troubles, going to mincer fragments... */
2103
2104         elt = 1;
2105         skb_p = &skb_shinfo(skb)->frag_list;
2106         copyflag = 0;
2107
2108         while ((skb1 = *skb_p) != NULL) {
2109                 int ntail = 0;
2110
2111                 /* The fragment is partially pulled by someone,
2112                  * this can happen on input. Copy it and everything
2113                  * after it. */
2114
2115                 if (skb_shared(skb1))
2116                         copyflag = 1;
2117
2118                 /* If the skb is the last, worry about trailer. */
2119
2120                 if (skb1->next == NULL && tailbits) {
2121                         if (skb_shinfo(skb1)->nr_frags ||
2122                             skb_shinfo(skb1)->frag_list ||
2123                             skb_tailroom(skb1) < tailbits)
2124                                 ntail = tailbits + 128;
2125                 }
2126
2127                 if (copyflag ||
2128                     skb_cloned(skb1) ||
2129                     ntail ||
2130                     skb_shinfo(skb1)->nr_frags ||
2131                     skb_shinfo(skb1)->frag_list) {
2132                         struct sk_buff *skb2;
2133
2134                         /* Fuck, we are miserable poor guys... */
2135                         if (ntail == 0)
2136                                 skb2 = skb_copy(skb1, GFP_ATOMIC);
2137                         else
2138                                 skb2 = skb_copy_expand(skb1,
2139                                                        skb_headroom(skb1),
2140                                                        ntail,
2141                                                        GFP_ATOMIC);
2142                         if (unlikely(skb2 == NULL))
2143                                 return -ENOMEM;
2144
2145                         if (skb1->sk)
2146                                 skb_set_owner_w(skb2, skb1->sk);
2147
2148                         /* Looking around. Are we still alive?
2149                          * OK, link new skb, drop old one */
2150
2151                         skb2->next = skb1->next;
2152                         *skb_p = skb2;
2153                         kfree_skb(skb1);
2154                         skb1 = skb2;
2155                 }
2156                 elt++;
2157                 *trailer = skb1;
2158                 skb_p = &skb1->next;
2159         }
2160
2161         return elt;
2162 }
2163
2164 EXPORT_SYMBOL(___pskb_trim);
2165 EXPORT_SYMBOL(__kfree_skb);
2166 EXPORT_SYMBOL(kfree_skb);
2167 EXPORT_SYMBOL(__pskb_pull_tail);
2168 EXPORT_SYMBOL(__alloc_skb);
2169 EXPORT_SYMBOL(__netdev_alloc_skb);
2170 EXPORT_SYMBOL(pskb_copy);
2171 EXPORT_SYMBOL(pskb_expand_head);
2172 EXPORT_SYMBOL(skb_checksum);
2173 EXPORT_SYMBOL(skb_clone);
2174 EXPORT_SYMBOL(skb_clone_fraglist);
2175 EXPORT_SYMBOL(skb_copy);
2176 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2177 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2178 EXPORT_SYMBOL(skb_copy_bits);
2179 EXPORT_SYMBOL(skb_copy_expand);
2180 EXPORT_SYMBOL(skb_over_panic);
2181 EXPORT_SYMBOL(skb_pad);
2182 EXPORT_SYMBOL(skb_realloc_headroom);
2183 EXPORT_SYMBOL(skb_under_panic);
2184 EXPORT_SYMBOL(skb_dequeue);
2185 EXPORT_SYMBOL(skb_dequeue_tail);
2186 EXPORT_SYMBOL(skb_insert);
2187 EXPORT_SYMBOL(skb_queue_purge);
2188 EXPORT_SYMBOL(skb_queue_head);
2189 EXPORT_SYMBOL(skb_queue_tail);
2190 EXPORT_SYMBOL(skb_unlink);
2191 EXPORT_SYMBOL(skb_append);
2192 EXPORT_SYMBOL(skb_split);
2193 EXPORT_SYMBOL(skb_prepare_seq_read);
2194 EXPORT_SYMBOL(skb_seq_read);
2195 EXPORT_SYMBOL(skb_abort_seq_read);
2196 EXPORT_SYMBOL(skb_find_text);
2197 EXPORT_SYMBOL(skb_append_datato_frags);
2198
2199 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2200 EXPORT_SYMBOL_GPL(skb_cow_data);