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