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