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