net: fix race on decreasing number of TX queues
[pandora-kernel.git] / net / core / skbuff.c
1 /*
2  *      Routines having to do with the 'struct sk_buff' memory handlers.
3  *
4  *      Authors:        Alan Cox <alan@lxorguk.ukuu.org.uk>
5  *                      Florian La Roche <rzsfl@rz.uni-sb.de>
6  *
7  *      Fixes:
8  *              Alan Cox        :       Fixed the worst of the load
9  *                                      balancer bugs.
10  *              Dave Platt      :       Interrupt stacking fix.
11  *      Richard Kooijman        :       Timestamp fixes.
12  *              Alan Cox        :       Changed buffer format.
13  *              Alan Cox        :       destructor hook for AF_UNIX etc.
14  *              Linus Torvalds  :       Better skb_clone.
15  *              Alan Cox        :       Added skb_copy.
16  *              Alan Cox        :       Added all the changed routines Linus
17  *                                      only put in the headers
18  *              Ray VanTassle   :       Fixed --skb->lock in free
19  *              Alan Cox        :       skb_copy copy arp field
20  *              Andi Kleen      :       slabified it.
21  *              Robert Olsson   :       Removed skb_head_pool
22  *
23  *      NOTE:
24  *              The __skb_ routines should be called with interrupts
25  *      disabled, or you better be *real* sure that the operation is atomic
26  *      with respect to whatever list is being frobbed (e.g. via lock_sock()
27  *      or via disabling bottom half handlers, etc).
28  *
29  *      This program is free software; you can redistribute it and/or
30  *      modify it under the terms of the GNU General Public License
31  *      as published by the Free Software Foundation; either version
32  *      2 of the License, or (at your option) any later version.
33  */
34
35 /*
36  *      The functions in this file will not compile correctly with gcc 2.4.x
37  */
38
39 #include <linux/module.h>
40 #include <linux/types.h>
41 #include <linux/kernel.h>
42 #include <linux/kmemcheck.h>
43 #include <linux/mm.h>
44 #include <linux/interrupt.h>
45 #include <linux/in.h>
46 #include <linux/inet.h>
47 #include <linux/slab.h>
48 #include <linux/tcp.h>
49 #include <linux/udp.h>
50 #include <linux/netdevice.h>
51 #ifdef CONFIG_NET_CLS_ACT
52 #include <net/pkt_sched.h>
53 #endif
54 #include <linux/string.h>
55 #include <linux/skbuff.h>
56 #include <linux/splice.h>
57 #include <linux/cache.h>
58 #include <linux/rtnetlink.h>
59 #include <linux/init.h>
60 #include <linux/scatterlist.h>
61 #include <linux/errqueue.h>
62 #include <linux/prefetch.h>
63
64 #include <net/protocol.h>
65 #include <net/dst.h>
66 #include <net/sock.h>
67 #include <net/checksum.h>
68 #include <net/xfrm.h>
69
70 #include <asm/uaccess.h>
71 #include <asm/system.h>
72 #include <trace/events/skb.h>
73
74 #include "kmap_skb.h"
75
76 static struct kmem_cache *skbuff_head_cache __read_mostly;
77 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
78
79 /*
80  *      Keep out-of-line to prevent kernel bloat.
81  *      __builtin_return_address is not used because it is not always
82  *      reliable.
83  */
84
85 /**
86  *      skb_over_panic  -       private function
87  *      @skb: buffer
88  *      @sz: size
89  *      @here: address
90  *
91  *      Out of line support code for skb_put(). Not user callable.
92  */
93 static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
94 {
95         printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
96                           "data:%p tail:%#lx end:%#lx dev:%s\n",
97                here, skb->len, sz, skb->head, skb->data,
98                (unsigned long)skb->tail, (unsigned long)skb->end,
99                skb->dev ? skb->dev->name : "<NULL>");
100         BUG();
101 }
102
103 /**
104  *      skb_under_panic -       private function
105  *      @skb: buffer
106  *      @sz: size
107  *      @here: address
108  *
109  *      Out of line support code for skb_push(). Not user callable.
110  */
111
112 static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
113 {
114         printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
115                           "data:%p tail:%#lx end:%#lx dev:%s\n",
116                here, skb->len, sz, skb->head, skb->data,
117                (unsigned long)skb->tail, (unsigned long)skb->end,
118                skb->dev ? skb->dev->name : "<NULL>");
119         BUG();
120 }
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         prefetchw(skb);
158
159         /* We do our best to align skb_shared_info on a separate cache
160          * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
161          * aligned memory blocks, unless SLUB/SLAB debug is enabled.
162          * Both skb->head and skb_shared_info are cache line aligned.
163          */
164         size = SKB_DATA_ALIGN(size);
165         size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
166         data = kmalloc_node_track_caller(size, gfp_mask, node);
167         if (!data)
168                 goto nodata;
169         /* kmalloc(size) might give us more room than requested.
170          * Put skb_shared_info exactly at the end of allocated zone,
171          * to allow max possible filling before reallocation.
172          */
173         size = SKB_WITH_OVERHEAD(ksize(data));
174         prefetchw(data + size);
175
176         /*
177          * Only clear those fields we need to clear, not those that we will
178          * actually initialise below. Hence, don't put any more fields after
179          * the tail pointer in struct sk_buff!
180          */
181         memset(skb, 0, offsetof(struct sk_buff, tail));
182         /* Account for allocated memory : skb + skb->head */
183         skb->truesize = SKB_TRUESIZE(size);
184         atomic_set(&skb->users, 1);
185         skb->head = data;
186         skb->data = data;
187         skb_reset_tail_pointer(skb);
188         skb->end = skb->tail + size;
189 #ifdef NET_SKBUFF_DATA_USES_OFFSET
190         skb->mac_header = ~0U;
191 #endif
192
193         /* make sure we initialize shinfo sequentially */
194         shinfo = skb_shinfo(skb);
195         memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
196         atomic_set(&shinfo->dataref, 1);
197         kmemcheck_annotate_variable(shinfo->destructor_arg);
198
199         if (fclone) {
200                 struct sk_buff *child = skb + 1;
201                 atomic_t *fclone_ref = (atomic_t *) (child + 1);
202
203                 kmemcheck_annotate_bitfield(child, flags1);
204                 kmemcheck_annotate_bitfield(child, flags2);
205                 skb->fclone = SKB_FCLONE_ORIG;
206                 atomic_set(fclone_ref, 1);
207
208                 child->fclone = SKB_FCLONE_UNAVAILABLE;
209         }
210 out:
211         return skb;
212 nodata:
213         kmem_cache_free(cache, skb);
214         skb = NULL;
215         goto out;
216 }
217 EXPORT_SYMBOL(__alloc_skb);
218
219 /**
220  *      __netdev_alloc_skb - allocate an skbuff for rx on a specific device
221  *      @dev: network device to receive on
222  *      @length: length to allocate
223  *      @gfp_mask: get_free_pages mask, passed to alloc_skb
224  *
225  *      Allocate a new &sk_buff and assign it a usage count of one. The
226  *      buffer has unspecified headroom built in. Users should allocate
227  *      the headroom they think they need without accounting for the
228  *      built in space. The built in space is used for optimisations.
229  *
230  *      %NULL is returned if there is no free memory.
231  */
232 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
233                 unsigned int length, gfp_t gfp_mask)
234 {
235         struct sk_buff *skb;
236
237         skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, NUMA_NO_NODE);
238         if (likely(skb)) {
239                 skb_reserve(skb, NET_SKB_PAD);
240                 skb->dev = dev;
241         }
242         return skb;
243 }
244 EXPORT_SYMBOL(__netdev_alloc_skb);
245
246 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
247                 int size)
248 {
249         skb_fill_page_desc(skb, i, page, off, size);
250         skb->len += size;
251         skb->data_len += size;
252         skb->truesize += size;
253 }
254 EXPORT_SYMBOL(skb_add_rx_frag);
255
256 /**
257  *      dev_alloc_skb - allocate an skbuff for receiving
258  *      @length: length to allocate
259  *
260  *      Allocate a new &sk_buff and assign it a usage count of one. The
261  *      buffer has unspecified headroom built in. Users should allocate
262  *      the headroom they think they need without accounting for the
263  *      built in space. The built in space is used for optimisations.
264  *
265  *      %NULL is returned if there is no free memory. Although this function
266  *      allocates memory it can be called from an interrupt.
267  */
268 struct sk_buff *dev_alloc_skb(unsigned int length)
269 {
270         /*
271          * There is more code here than it seems:
272          * __dev_alloc_skb is an inline
273          */
274         return __dev_alloc_skb(length, GFP_ATOMIC);
275 }
276 EXPORT_SYMBOL(dev_alloc_skb);
277
278 static void skb_drop_list(struct sk_buff **listp)
279 {
280         kfree_skb_list(*listp);
281         *listp = NULL;
282 }
283
284 static inline void skb_drop_fraglist(struct sk_buff *skb)
285 {
286         skb_drop_list(&skb_shinfo(skb)->frag_list);
287 }
288
289 static void skb_clone_fraglist(struct sk_buff *skb)
290 {
291         struct sk_buff *list;
292
293         skb_walk_frags(skb, list)
294                 skb_get(list);
295 }
296
297 static void skb_release_data(struct sk_buff *skb)
298 {
299         if (!skb->cloned ||
300             !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
301                                &skb_shinfo(skb)->dataref)) {
302                 if (skb_shinfo(skb)->nr_frags) {
303                         int i;
304                         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
305                                 skb_frag_unref(skb, i);
306                 }
307
308                 /*
309                  * If skb buf is from userspace, we need to notify the caller
310                  * the lower device DMA has done;
311                  */
312                 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
313                         struct ubuf_info *uarg;
314
315                         uarg = skb_shinfo(skb)->destructor_arg;
316                         if (uarg->callback)
317                                 uarg->callback(uarg);
318                 }
319
320                 if (skb_has_frag_list(skb))
321                         skb_drop_fraglist(skb);
322
323                 kfree(skb->head);
324         }
325 }
326
327 /*
328  *      Free an skbuff by memory without cleaning the state.
329  */
330 static void kfree_skbmem(struct sk_buff *skb)
331 {
332         struct sk_buff *other;
333         atomic_t *fclone_ref;
334
335         switch (skb->fclone) {
336         case SKB_FCLONE_UNAVAILABLE:
337                 kmem_cache_free(skbuff_head_cache, skb);
338                 break;
339
340         case SKB_FCLONE_ORIG:
341                 fclone_ref = (atomic_t *) (skb + 2);
342                 if (atomic_dec_and_test(fclone_ref))
343                         kmem_cache_free(skbuff_fclone_cache, skb);
344                 break;
345
346         case SKB_FCLONE_CLONE:
347                 fclone_ref = (atomic_t *) (skb + 1);
348                 other = skb - 1;
349
350                 /* The clone portion is available for
351                  * fast-cloning again.
352                  */
353                 skb->fclone = SKB_FCLONE_UNAVAILABLE;
354
355                 if (atomic_dec_and_test(fclone_ref))
356                         kmem_cache_free(skbuff_fclone_cache, other);
357                 break;
358         }
359 }
360
361 static void skb_release_head_state(struct sk_buff *skb)
362 {
363         skb_dst_drop(skb);
364 #ifdef CONFIG_XFRM
365         secpath_put(skb->sp);
366 #endif
367         if (skb->destructor) {
368                 WARN_ON(in_irq());
369                 skb->destructor(skb);
370         }
371 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
372         nf_conntrack_put(skb->nfct);
373 #endif
374 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
375         nf_conntrack_put_reasm(skb->nfct_reasm);
376 #endif
377 #ifdef CONFIG_BRIDGE_NETFILTER
378         nf_bridge_put(skb->nf_bridge);
379 #endif
380 /* XXX: IS this still necessary? - JHS */
381 #ifdef CONFIG_NET_SCHED
382         skb->tc_index = 0;
383 #ifdef CONFIG_NET_CLS_ACT
384         skb->tc_verd = 0;
385 #endif
386 #endif
387 }
388
389 /* Free everything but the sk_buff shell. */
390 static void skb_release_all(struct sk_buff *skb)
391 {
392         skb_release_head_state(skb);
393         skb_release_data(skb);
394 }
395
396 /**
397  *      __kfree_skb - private function
398  *      @skb: buffer
399  *
400  *      Free an sk_buff. Release anything attached to the buffer.
401  *      Clean the state. This is an internal helper function. Users should
402  *      always call kfree_skb
403  */
404
405 void __kfree_skb(struct sk_buff *skb)
406 {
407         skb_release_all(skb);
408         kfree_skbmem(skb);
409 }
410 EXPORT_SYMBOL(__kfree_skb);
411
412 /**
413  *      kfree_skb - free an sk_buff
414  *      @skb: buffer to free
415  *
416  *      Drop a reference to the buffer and free it if the usage count has
417  *      hit zero.
418  */
419 void kfree_skb(struct sk_buff *skb)
420 {
421         if (unlikely(!skb))
422                 return;
423         if (likely(atomic_read(&skb->users) == 1))
424                 smp_rmb();
425         else if (likely(!atomic_dec_and_test(&skb->users)))
426                 return;
427         trace_kfree_skb(skb, __builtin_return_address(0));
428         __kfree_skb(skb);
429 }
430 EXPORT_SYMBOL(kfree_skb);
431
432 void kfree_skb_list(struct sk_buff *segs)
433 {
434         while (segs) {
435                 struct sk_buff *next = segs->next;
436
437                 kfree_skb(segs);
438                 segs = next;
439         }
440 }
441 EXPORT_SYMBOL(kfree_skb_list);
442
443 /**
444  *      consume_skb - free an skbuff
445  *      @skb: buffer to free
446  *
447  *      Drop a ref to the buffer and free it if the usage count has hit zero
448  *      Functions identically to kfree_skb, but kfree_skb assumes that the frame
449  *      is being dropped after a failure and notes that
450  */
451 void consume_skb(struct sk_buff *skb)
452 {
453         if (unlikely(!skb))
454                 return;
455         if (likely(atomic_read(&skb->users) == 1))
456                 smp_rmb();
457         else if (likely(!atomic_dec_and_test(&skb->users)))
458                 return;
459         trace_consume_skb(skb);
460         __kfree_skb(skb);
461 }
462 EXPORT_SYMBOL(consume_skb);
463
464 /**
465  *      skb_recycle - clean up an skb for reuse
466  *      @skb: buffer
467  *
468  *      Recycles the skb to be reused as a receive buffer. This
469  *      function does any necessary reference count dropping, and
470  *      cleans up the skbuff as if it just came from __alloc_skb().
471  */
472 void skb_recycle(struct sk_buff *skb)
473 {
474         struct skb_shared_info *shinfo;
475
476         skb_release_head_state(skb);
477
478         shinfo = skb_shinfo(skb);
479         memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
480         atomic_set(&shinfo->dataref, 1);
481
482         memset(skb, 0, offsetof(struct sk_buff, tail));
483         skb->data = skb->head + NET_SKB_PAD;
484         skb_reset_tail_pointer(skb);
485 }
486 EXPORT_SYMBOL(skb_recycle);
487
488 /**
489  *      skb_recycle_check - check if skb can be reused for receive
490  *      @skb: buffer
491  *      @skb_size: minimum receive buffer size
492  *
493  *      Checks that the skb passed in is not shared or cloned, and
494  *      that it is linear and its head portion at least as large as
495  *      skb_size so that it can be recycled as a receive buffer.
496  *      If these conditions are met, this function does any necessary
497  *      reference count dropping and cleans up the skbuff as if it
498  *      just came from __alloc_skb().
499  */
500 bool skb_recycle_check(struct sk_buff *skb, int skb_size)
501 {
502         if (!skb_is_recycleable(skb, skb_size))
503                 return false;
504
505         skb_recycle(skb);
506
507         return true;
508 }
509 EXPORT_SYMBOL(skb_recycle_check);
510
511 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
512 {
513         new->tstamp             = old->tstamp;
514         new->dev                = old->dev;
515         new->transport_header   = old->transport_header;
516         new->network_header     = old->network_header;
517         new->mac_header         = old->mac_header;
518         skb_dst_copy(new, old);
519         new->rxhash             = old->rxhash;
520         new->ooo_okay           = old->ooo_okay;
521         new->l4_rxhash          = old->l4_rxhash;
522 #ifdef CONFIG_XFRM
523         new->sp                 = secpath_get(old->sp);
524 #endif
525         memcpy(new->cb, old->cb, sizeof(old->cb));
526         new->csum               = old->csum;
527         new->local_df           = old->local_df;
528         new->pkt_type           = old->pkt_type;
529         new->ip_summed          = old->ip_summed;
530         skb_copy_queue_mapping(new, old);
531         new->priority           = old->priority;
532 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
533         new->ipvs_property      = old->ipvs_property;
534 #endif
535         new->protocol           = old->protocol;
536         new->mark               = old->mark;
537         new->skb_iif            = old->skb_iif;
538         __nf_copy(new, old);
539 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
540     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
541         new->nf_trace           = old->nf_trace;
542 #endif
543 #ifdef CONFIG_NET_SCHED
544         new->tc_index           = old->tc_index;
545 #ifdef CONFIG_NET_CLS_ACT
546         new->tc_verd            = old->tc_verd;
547 #endif
548 #endif
549         new->vlan_tci           = old->vlan_tci;
550
551         skb_copy_secmark(new, old);
552 }
553
554 /*
555  * You should not add any new code to this function.  Add it to
556  * __copy_skb_header above instead.
557  */
558 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
559 {
560 #define C(x) n->x = skb->x
561
562         n->next = n->prev = NULL;
563         n->sk = NULL;
564         __copy_skb_header(n, skb);
565
566         C(len);
567         C(data_len);
568         C(mac_len);
569         n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
570         n->cloned = 1;
571         n->nohdr = 0;
572         n->destructor = NULL;
573         C(tail);
574         C(end);
575         C(head);
576         C(data);
577         C(truesize);
578         atomic_set(&n->users, 1);
579
580         atomic_inc(&(skb_shinfo(skb)->dataref));
581         skb->cloned = 1;
582
583         return n;
584 #undef C
585 }
586
587 /**
588  *      skb_morph       -       morph one skb into another
589  *      @dst: the skb to receive the contents
590  *      @src: the skb to supply the contents
591  *
592  *      This is identical to skb_clone except that the target skb is
593  *      supplied by the user.
594  *
595  *      The target skb is returned upon exit.
596  */
597 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
598 {
599         skb_release_all(dst);
600         return __skb_clone(dst, src);
601 }
602 EXPORT_SYMBOL_GPL(skb_morph);
603
604 /*      skb_copy_ubufs  -       copy userspace skb frags buffers to kernel
605  *      @skb: the skb to modify
606  *      @gfp_mask: allocation priority
607  *
608  *      This must be called on SKBTX_DEV_ZEROCOPY skb.
609  *      It will copy all frags into kernel and drop the reference
610  *      to userspace pages.
611  *
612  *      If this function is called from an interrupt gfp_mask() must be
613  *      %GFP_ATOMIC.
614  *
615  *      Returns 0 on success or a negative error code on failure
616  *      to allocate kernel memory to copy to.
617  */
618 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
619 {
620         int i;
621         int num_frags = skb_shinfo(skb)->nr_frags;
622         struct page *page, *head = NULL;
623         struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
624
625         for (i = 0; i < num_frags; i++) {
626                 u8 *vaddr;
627                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
628
629                 page = alloc_page(GFP_ATOMIC);
630                 if (!page) {
631                         while (head) {
632                                 struct page *next = (struct page *)head->private;
633                                 put_page(head);
634                                 head = next;
635                         }
636                         return -ENOMEM;
637                 }
638                 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
639                 memcpy(page_address(page),
640                        vaddr + f->page_offset, skb_frag_size(f));
641                 kunmap_skb_frag(vaddr);
642                 page->private = (unsigned long)head;
643                 head = page;
644         }
645
646         /* skb frags release userspace buffers */
647         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
648                 skb_frag_unref(skb, i);
649
650         uarg->callback(uarg);
651
652         /* skb frags point to kernel buffers */
653         for (i = skb_shinfo(skb)->nr_frags; i > 0; i--) {
654                 __skb_fill_page_desc(skb, i-1, head, 0,
655                                      skb_shinfo(skb)->frags[i - 1].size);
656                 head = (struct page *)head->private;
657         }
658
659         skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
660         return 0;
661 }
662 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
663
664 /**
665  *      skb_clone       -       duplicate an sk_buff
666  *      @skb: buffer to clone
667  *      @gfp_mask: allocation priority
668  *
669  *      Duplicate an &sk_buff. The new one is not owned by a socket. Both
670  *      copies share the same packet data but not structure. The new
671  *      buffer has a reference count of 1. If the allocation fails the
672  *      function returns %NULL otherwise the new buffer is returned.
673  *
674  *      If this function is called from an interrupt gfp_mask() must be
675  *      %GFP_ATOMIC.
676  */
677
678 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
679 {
680         struct sk_buff *n;
681
682         if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
683                 if (skb_copy_ubufs(skb, gfp_mask))
684                         return NULL;
685         }
686
687         n = skb + 1;
688         if (skb->fclone == SKB_FCLONE_ORIG &&
689             n->fclone == SKB_FCLONE_UNAVAILABLE) {
690                 atomic_t *fclone_ref = (atomic_t *) (n + 1);
691                 n->fclone = SKB_FCLONE_CLONE;
692                 atomic_inc(fclone_ref);
693         } else {
694                 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
695                 if (!n)
696                         return NULL;
697
698                 kmemcheck_annotate_bitfield(n, flags1);
699                 kmemcheck_annotate_bitfield(n, flags2);
700                 n->fclone = SKB_FCLONE_UNAVAILABLE;
701         }
702
703         return __skb_clone(n, skb);
704 }
705 EXPORT_SYMBOL(skb_clone);
706
707 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
708 {
709 #ifndef NET_SKBUFF_DATA_USES_OFFSET
710         /*
711          *      Shift between the two data areas in bytes
712          */
713         unsigned long offset = new->data - old->data;
714 #endif
715
716         __copy_skb_header(new, old);
717
718 #ifndef NET_SKBUFF_DATA_USES_OFFSET
719         /* {transport,network,mac}_header are relative to skb->head */
720         new->transport_header += offset;
721         new->network_header   += offset;
722         if (skb_mac_header_was_set(new))
723                 new->mac_header       += offset;
724 #endif
725         skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
726         skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
727         skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
728 }
729
730 /**
731  *      skb_copy        -       create private copy of an sk_buff
732  *      @skb: buffer to copy
733  *      @gfp_mask: allocation priority
734  *
735  *      Make a copy of both an &sk_buff and its data. This is used when the
736  *      caller wishes to modify the data and needs a private copy of the
737  *      data to alter. Returns %NULL on failure or the pointer to the buffer
738  *      on success. The returned buffer has a reference count of 1.
739  *
740  *      As by-product this function converts non-linear &sk_buff to linear
741  *      one, so that &sk_buff becomes completely private and caller is allowed
742  *      to modify all the data of returned buffer. This means that this
743  *      function is not recommended for use in circumstances when only
744  *      header is going to be modified. Use pskb_copy() instead.
745  */
746
747 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
748 {
749         int headerlen = skb_headroom(skb);
750         unsigned int size = skb_end_offset(skb) + skb->data_len;
751         struct sk_buff *n = alloc_skb(size, gfp_mask);
752
753         if (!n)
754                 return NULL;
755
756         /* Set the data pointer */
757         skb_reserve(n, headerlen);
758         /* Set the tail pointer and length */
759         skb_put(n, skb->len);
760
761         if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
762                 BUG();
763
764         copy_skb_header(n, skb);
765         return n;
766 }
767 EXPORT_SYMBOL(skb_copy);
768
769 /**
770  *      pskb_copy       -       create copy of an sk_buff with private head.
771  *      @skb: buffer to copy
772  *      @gfp_mask: allocation priority
773  *
774  *      Make a copy of both an &sk_buff and part of its data, located
775  *      in header. Fragmented data remain shared. This is used when
776  *      the caller wishes to modify only header of &sk_buff and needs
777  *      private copy of the header to alter. Returns %NULL on failure
778  *      or the pointer to the buffer on success.
779  *      The returned buffer has a reference count of 1.
780  */
781
782 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
783 {
784         unsigned int size = skb_end_pointer(skb) - skb->head;
785         struct sk_buff *n = alloc_skb(size, gfp_mask);
786
787         if (!n)
788                 goto out;
789
790         /* Set the data pointer */
791         skb_reserve(n, skb_headroom(skb));
792         /* Set the tail pointer and length */
793         skb_put(n, skb_headlen(skb));
794         /* Copy the bytes */
795         skb_copy_from_linear_data(skb, n->data, n->len);
796
797         n->truesize += skb->data_len;
798         n->data_len  = skb->data_len;
799         n->len       = skb->len;
800
801         if (skb_shinfo(skb)->nr_frags) {
802                 int i;
803
804                 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
805                         if (skb_copy_ubufs(skb, gfp_mask)) {
806                                 kfree_skb(n);
807                                 n = NULL;
808                                 goto out;
809                         }
810                 }
811                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
812                         skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
813                         skb_frag_ref(skb, i);
814                 }
815                 skb_shinfo(n)->nr_frags = i;
816         }
817
818         if (skb_has_frag_list(skb)) {
819                 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
820                 skb_clone_fraglist(n);
821         }
822
823         copy_skb_header(n, skb);
824 out:
825         return n;
826 }
827 EXPORT_SYMBOL(pskb_copy);
828
829 /**
830  *      pskb_expand_head - reallocate header of &sk_buff
831  *      @skb: buffer to reallocate
832  *      @nhead: room to add at head
833  *      @ntail: room to add at tail
834  *      @gfp_mask: allocation priority
835  *
836  *      Expands (or creates identical copy, if &nhead and &ntail are zero)
837  *      header of skb. &sk_buff itself is not changed. &sk_buff MUST have
838  *      reference count of 1. Returns zero in the case of success or error,
839  *      if expansion failed. In the last case, &sk_buff is not changed.
840  *
841  *      All the pointers pointing into skb header may change and must be
842  *      reloaded after call to this function.
843  */
844
845 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
846                      gfp_t gfp_mask)
847 {
848         int i;
849         u8 *data;
850         int size = nhead + skb_end_offset(skb) + ntail;
851         long off;
852         bool fastpath;
853
854         BUG_ON(nhead < 0);
855
856         if (skb_shared(skb))
857                 BUG();
858
859         size = SKB_DATA_ALIGN(size);
860
861         /* Check if we can avoid taking references on fragments if we own
862          * the last reference on skb->head. (see skb_release_data())
863          */
864         if (!skb->cloned)
865                 fastpath = true;
866         else {
867                 int delta = skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1;
868                 fastpath = atomic_read(&skb_shinfo(skb)->dataref) == delta;
869         }
870
871         if (fastpath &&
872             size + sizeof(struct skb_shared_info) <= ksize(skb->head)) {
873                 memmove(skb->head + size, skb_shinfo(skb),
874                         offsetof(struct skb_shared_info,
875                                  frags[skb_shinfo(skb)->nr_frags]));
876                 memmove(skb->head + nhead, skb->head,
877                         skb_tail_pointer(skb) - skb->head);
878                 off = nhead;
879                 goto adjust_others;
880         }
881
882         data = kmalloc(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
883                        gfp_mask);
884         if (!data)
885                 goto nodata;
886         size = SKB_WITH_OVERHEAD(ksize(data));
887
888         /* Copy only real data... and, alas, header. This should be
889          * optimized for the cases when header is void.
890          */
891         memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
892
893         memcpy((struct skb_shared_info *)(data + size),
894                skb_shinfo(skb),
895                offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
896
897         if (fastpath) {
898                 kfree(skb->head);
899         } else {
900                 /* copy this zero copy skb frags */
901                 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
902                         if (skb_copy_ubufs(skb, gfp_mask))
903                                 goto nofrags;
904                 }
905                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
906                         skb_frag_ref(skb, i);
907
908                 if (skb_has_frag_list(skb))
909                         skb_clone_fraglist(skb);
910
911                 skb_release_data(skb);
912         }
913         off = (data + nhead) - skb->head;
914
915         skb->head     = data;
916 adjust_others:
917         skb->data    += off;
918 #ifdef NET_SKBUFF_DATA_USES_OFFSET
919         skb->end      = size;
920         off           = nhead;
921 #else
922         skb->end      = skb->head + size;
923 #endif
924         /* {transport,network,mac}_header and tail are relative to skb->head */
925         skb->tail             += off;
926         skb->transport_header += off;
927         skb->network_header   += off;
928         if (skb_mac_header_was_set(skb))
929                 skb->mac_header += off;
930         /* Only adjust this if it actually is csum_start rather than csum */
931         if (skb->ip_summed == CHECKSUM_PARTIAL)
932                 skb->csum_start += nhead;
933         skb->cloned   = 0;
934         skb->hdr_len  = 0;
935         skb->nohdr    = 0;
936         atomic_set(&skb_shinfo(skb)->dataref, 1);
937         return 0;
938
939 nofrags:
940         kfree(data);
941 nodata:
942         return -ENOMEM;
943 }
944 EXPORT_SYMBOL(pskb_expand_head);
945
946 /* Make private copy of skb with writable head and some headroom */
947
948 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
949 {
950         struct sk_buff *skb2;
951         int delta = headroom - skb_headroom(skb);
952
953         if (delta <= 0)
954                 skb2 = pskb_copy(skb, GFP_ATOMIC);
955         else {
956                 skb2 = skb_clone(skb, GFP_ATOMIC);
957                 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
958                                              GFP_ATOMIC)) {
959                         kfree_skb(skb2);
960                         skb2 = NULL;
961                 }
962         }
963         return skb2;
964 }
965 EXPORT_SYMBOL(skb_realloc_headroom);
966
967 /**
968  *      skb_copy_expand -       copy and expand sk_buff
969  *      @skb: buffer to copy
970  *      @newheadroom: new free bytes at head
971  *      @newtailroom: new free bytes at tail
972  *      @gfp_mask: allocation priority
973  *
974  *      Make a copy of both an &sk_buff and its data and while doing so
975  *      allocate additional space.
976  *
977  *      This is used when the caller wishes to modify the data and needs a
978  *      private copy of the data to alter as well as more space for new fields.
979  *      Returns %NULL on failure or the pointer to the buffer
980  *      on success. The returned buffer has a reference count of 1.
981  *
982  *      You must pass %GFP_ATOMIC as the allocation priority if this function
983  *      is called from an interrupt.
984  */
985 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
986                                 int newheadroom, int newtailroom,
987                                 gfp_t gfp_mask)
988 {
989         /*
990          *      Allocate the copy buffer
991          */
992         struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
993                                       gfp_mask);
994         int oldheadroom = skb_headroom(skb);
995         int head_copy_len, head_copy_off;
996         int off;
997
998         if (!n)
999                 return NULL;
1000
1001         skb_reserve(n, newheadroom);
1002
1003         /* Set the tail pointer and length */
1004         skb_put(n, skb->len);
1005
1006         head_copy_len = oldheadroom;
1007         head_copy_off = 0;
1008         if (newheadroom <= head_copy_len)
1009                 head_copy_len = newheadroom;
1010         else
1011                 head_copy_off = newheadroom - head_copy_len;
1012
1013         /* Copy the linear header and data. */
1014         if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1015                           skb->len + head_copy_len))
1016                 BUG();
1017
1018         copy_skb_header(n, skb);
1019
1020         off                  = newheadroom - oldheadroom;
1021         if (n->ip_summed == CHECKSUM_PARTIAL)
1022                 n->csum_start += off;
1023 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1024         n->transport_header += off;
1025         n->network_header   += off;
1026         if (skb_mac_header_was_set(skb))
1027                 n->mac_header += off;
1028 #endif
1029
1030         return n;
1031 }
1032 EXPORT_SYMBOL(skb_copy_expand);
1033
1034 /**
1035  *      skb_pad                 -       zero pad the tail of an skb
1036  *      @skb: buffer to pad
1037  *      @pad: space to pad
1038  *
1039  *      Ensure that a buffer is followed by a padding area that is zero
1040  *      filled. Used by network drivers which may DMA or transfer data
1041  *      beyond the buffer end onto the wire.
1042  *
1043  *      May return error in out of memory cases. The skb is freed on error.
1044  */
1045
1046 int skb_pad(struct sk_buff *skb, int pad)
1047 {
1048         int err;
1049         int ntail;
1050
1051         /* If the skbuff is non linear tailroom is always zero.. */
1052         if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1053                 memset(skb->data+skb->len, 0, pad);
1054                 return 0;
1055         }
1056
1057         ntail = skb->data_len + pad - (skb->end - skb->tail);
1058         if (likely(skb_cloned(skb) || ntail > 0)) {
1059                 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1060                 if (unlikely(err))
1061                         goto free_skb;
1062         }
1063
1064         /* FIXME: The use of this function with non-linear skb's really needs
1065          * to be audited.
1066          */
1067         err = skb_linearize(skb);
1068         if (unlikely(err))
1069                 goto free_skb;
1070
1071         memset(skb->data + skb->len, 0, pad);
1072         return 0;
1073
1074 free_skb:
1075         kfree_skb(skb);
1076         return err;
1077 }
1078 EXPORT_SYMBOL(skb_pad);
1079
1080 /**
1081  *      skb_put - add data to a buffer
1082  *      @skb: buffer to use
1083  *      @len: amount of data to add
1084  *
1085  *      This function extends the used data area of the buffer. If this would
1086  *      exceed the total buffer size the kernel will panic. A pointer to the
1087  *      first byte of the extra data is returned.
1088  */
1089 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1090 {
1091         unsigned char *tmp = skb_tail_pointer(skb);
1092         SKB_LINEAR_ASSERT(skb);
1093         skb->tail += len;
1094         skb->len  += len;
1095         if (unlikely(skb->tail > skb->end))
1096                 skb_over_panic(skb, len, __builtin_return_address(0));
1097         return tmp;
1098 }
1099 EXPORT_SYMBOL(skb_put);
1100
1101 /**
1102  *      skb_push - add data to the start of a buffer
1103  *      @skb: buffer to use
1104  *      @len: amount of data to add
1105  *
1106  *      This function extends the used data area of the buffer at the buffer
1107  *      start. If this would exceed the total buffer headroom the kernel will
1108  *      panic. A pointer to the first byte of the extra data is returned.
1109  */
1110 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1111 {
1112         skb->data -= len;
1113         skb->len  += len;
1114         if (unlikely(skb->data<skb->head))
1115                 skb_under_panic(skb, len, __builtin_return_address(0));
1116         return skb->data;
1117 }
1118 EXPORT_SYMBOL(skb_push);
1119
1120 /**
1121  *      skb_pull - remove data from the start of a buffer
1122  *      @skb: buffer to use
1123  *      @len: amount of data to remove
1124  *
1125  *      This function removes data from the start of a buffer, returning
1126  *      the memory to the headroom. A pointer to the next data in the buffer
1127  *      is returned. Once the data has been pulled future pushes will overwrite
1128  *      the old data.
1129  */
1130 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1131 {
1132         return skb_pull_inline(skb, len);
1133 }
1134 EXPORT_SYMBOL(skb_pull);
1135
1136 /**
1137  *      skb_trim - remove end from a buffer
1138  *      @skb: buffer to alter
1139  *      @len: new length
1140  *
1141  *      Cut the length of a buffer down by removing data from the tail. If
1142  *      the buffer is already under the length specified it is not modified.
1143  *      The skb must be linear.
1144  */
1145 void skb_trim(struct sk_buff *skb, unsigned int len)
1146 {
1147         if (skb->len > len)
1148                 __skb_trim(skb, len);
1149 }
1150 EXPORT_SYMBOL(skb_trim);
1151
1152 /* Trims skb to length len. It can change skb pointers.
1153  */
1154
1155 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1156 {
1157         struct sk_buff **fragp;
1158         struct sk_buff *frag;
1159         int offset = skb_headlen(skb);
1160         int nfrags = skb_shinfo(skb)->nr_frags;
1161         int i;
1162         int err;
1163
1164         if (skb_cloned(skb) &&
1165             unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1166                 return err;
1167
1168         i = 0;
1169         if (offset >= len)
1170                 goto drop_pages;
1171
1172         for (; i < nfrags; i++) {
1173                 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1174
1175                 if (end < len) {
1176                         offset = end;
1177                         continue;
1178                 }
1179
1180                 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1181
1182 drop_pages:
1183                 skb_shinfo(skb)->nr_frags = i;
1184
1185                 for (; i < nfrags; i++)
1186                         skb_frag_unref(skb, i);
1187
1188                 if (skb_has_frag_list(skb))
1189                         skb_drop_fraglist(skb);
1190                 goto done;
1191         }
1192
1193         for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1194              fragp = &frag->next) {
1195                 int end = offset + frag->len;
1196
1197                 if (skb_shared(frag)) {
1198                         struct sk_buff *nfrag;
1199
1200                         nfrag = skb_clone(frag, GFP_ATOMIC);
1201                         if (unlikely(!nfrag))
1202                                 return -ENOMEM;
1203
1204                         nfrag->next = frag->next;
1205                         kfree_skb(frag);
1206                         frag = nfrag;
1207                         *fragp = frag;
1208                 }
1209
1210                 if (end < len) {
1211                         offset = end;
1212                         continue;
1213                 }
1214
1215                 if (end > len &&
1216                     unlikely((err = pskb_trim(frag, len - offset))))
1217                         return err;
1218
1219                 if (frag->next)
1220                         skb_drop_list(&frag->next);
1221                 break;
1222         }
1223
1224 done:
1225         if (len > skb_headlen(skb)) {
1226                 skb->data_len -= skb->len - len;
1227                 skb->len       = len;
1228         } else {
1229                 skb->len       = len;
1230                 skb->data_len  = 0;
1231                 skb_set_tail_pointer(skb, len);
1232         }
1233
1234         return 0;
1235 }
1236 EXPORT_SYMBOL(___pskb_trim);
1237
1238 /**
1239  *      __pskb_pull_tail - advance tail of skb header
1240  *      @skb: buffer to reallocate
1241  *      @delta: number of bytes to advance tail
1242  *
1243  *      The function makes a sense only on a fragmented &sk_buff,
1244  *      it expands header moving its tail forward and copying necessary
1245  *      data from fragmented part.
1246  *
1247  *      &sk_buff MUST have reference count of 1.
1248  *
1249  *      Returns %NULL (and &sk_buff does not change) if pull failed
1250  *      or value of new tail of skb in the case of success.
1251  *
1252  *      All the pointers pointing into skb header may change and must be
1253  *      reloaded after call to this function.
1254  */
1255
1256 /* Moves tail of skb head forward, copying data from fragmented part,
1257  * when it is necessary.
1258  * 1. It may fail due to malloc failure.
1259  * 2. It may change skb pointers.
1260  *
1261  * It is pretty complicated. Luckily, it is called only in exceptional cases.
1262  */
1263 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1264 {
1265         /* If skb has not enough free space at tail, get new one
1266          * plus 128 bytes for future expansions. If we have enough
1267          * room at tail, reallocate without expansion only if skb is cloned.
1268          */
1269         int i, k, eat = (skb->tail + delta) - skb->end;
1270
1271         if (eat > 0 || skb_cloned(skb)) {
1272                 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1273                                      GFP_ATOMIC))
1274                         return NULL;
1275         }
1276
1277         if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1278                 BUG();
1279
1280         /* Optimization: no fragments, no reasons to preestimate
1281          * size of pulled pages. Superb.
1282          */
1283         if (!skb_has_frag_list(skb))
1284                 goto pull_pages;
1285
1286         /* Estimate size of pulled pages. */
1287         eat = delta;
1288         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1289                 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1290
1291                 if (size >= eat)
1292                         goto pull_pages;
1293                 eat -= size;
1294         }
1295
1296         /* If we need update frag list, we are in troubles.
1297          * Certainly, it possible to add an offset to skb data,
1298          * but taking into account that pulling is expected to
1299          * be very rare operation, it is worth to fight against
1300          * further bloating skb head and crucify ourselves here instead.
1301          * Pure masohism, indeed. 8)8)
1302          */
1303         if (eat) {
1304                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1305                 struct sk_buff *clone = NULL;
1306                 struct sk_buff *insp = NULL;
1307
1308                 do {
1309                         BUG_ON(!list);
1310
1311                         if (list->len <= eat) {
1312                                 /* Eaten as whole. */
1313                                 eat -= list->len;
1314                                 list = list->next;
1315                                 insp = list;
1316                         } else {
1317                                 /* Eaten partially. */
1318
1319                                 if (skb_shared(list)) {
1320                                         /* Sucks! We need to fork list. :-( */
1321                                         clone = skb_clone(list, GFP_ATOMIC);
1322                                         if (!clone)
1323                                                 return NULL;
1324                                         insp = list->next;
1325                                         list = clone;
1326                                 } else {
1327                                         /* This may be pulled without
1328                                          * problems. */
1329                                         insp = list;
1330                                 }
1331                                 if (!pskb_pull(list, eat)) {
1332                                         kfree_skb(clone);
1333                                         return NULL;
1334                                 }
1335                                 break;
1336                         }
1337                 } while (eat);
1338
1339                 /* Free pulled out fragments. */
1340                 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1341                         skb_shinfo(skb)->frag_list = list->next;
1342                         kfree_skb(list);
1343                 }
1344                 /* And insert new clone at head. */
1345                 if (clone) {
1346                         clone->next = list;
1347                         skb_shinfo(skb)->frag_list = clone;
1348                 }
1349         }
1350         /* Success! Now we may commit changes to skb data. */
1351
1352 pull_pages:
1353         eat = delta;
1354         k = 0;
1355         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1356                 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1357
1358                 if (size <= eat) {
1359                         skb_frag_unref(skb, i);
1360                         eat -= size;
1361                 } else {
1362                         skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1363                         if (eat) {
1364                                 skb_shinfo(skb)->frags[k].page_offset += eat;
1365                                 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1366                                 eat = 0;
1367                         }
1368                         k++;
1369                 }
1370         }
1371         skb_shinfo(skb)->nr_frags = k;
1372
1373         skb->tail     += delta;
1374         skb->data_len -= delta;
1375
1376         return skb_tail_pointer(skb);
1377 }
1378 EXPORT_SYMBOL(__pskb_pull_tail);
1379
1380 /**
1381  *      skb_copy_bits - copy bits from skb to kernel buffer
1382  *      @skb: source skb
1383  *      @offset: offset in source
1384  *      @to: destination buffer
1385  *      @len: number of bytes to copy
1386  *
1387  *      Copy the specified number of bytes from the source skb to the
1388  *      destination buffer.
1389  *
1390  *      CAUTION ! :
1391  *              If its prototype is ever changed,
1392  *              check arch/{*}/net/{*}.S files,
1393  *              since it is called from BPF assembly code.
1394  */
1395 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1396 {
1397         int start = skb_headlen(skb);
1398         struct sk_buff *frag_iter;
1399         int i, copy;
1400
1401         if (offset > (int)skb->len - len)
1402                 goto fault;
1403
1404         /* Copy header. */
1405         if ((copy = start - offset) > 0) {
1406                 if (copy > len)
1407                         copy = len;
1408                 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1409                 if ((len -= copy) == 0)
1410                         return 0;
1411                 offset += copy;
1412                 to     += copy;
1413         }
1414
1415         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1416                 int end;
1417
1418                 WARN_ON(start > offset + len);
1419
1420                 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1421                 if ((copy = end - offset) > 0) {
1422                         u8 *vaddr;
1423
1424                         if (copy > len)
1425                                 copy = len;
1426
1427                         vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1428                         memcpy(to,
1429                                vaddr + skb_shinfo(skb)->frags[i].page_offset+
1430                                offset - start, copy);
1431                         kunmap_skb_frag(vaddr);
1432
1433                         if ((len -= copy) == 0)
1434                                 return 0;
1435                         offset += copy;
1436                         to     += copy;
1437                 }
1438                 start = end;
1439         }
1440
1441         skb_walk_frags(skb, frag_iter) {
1442                 int end;
1443
1444                 WARN_ON(start > offset + len);
1445
1446                 end = start + frag_iter->len;
1447                 if ((copy = end - offset) > 0) {
1448                         if (copy > len)
1449                                 copy = len;
1450                         if (skb_copy_bits(frag_iter, offset - start, to, copy))
1451                                 goto fault;
1452                         if ((len -= copy) == 0)
1453                                 return 0;
1454                         offset += copy;
1455                         to     += copy;
1456                 }
1457                 start = end;
1458         }
1459
1460         if (!len)
1461                 return 0;
1462
1463 fault:
1464         return -EFAULT;
1465 }
1466 EXPORT_SYMBOL(skb_copy_bits);
1467
1468 /*
1469  * Callback from splice_to_pipe(), if we need to release some pages
1470  * at the end of the spd in case we error'ed out in filling the pipe.
1471  */
1472 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1473 {
1474         put_page(spd->pages[i]);
1475 }
1476
1477 static inline struct page *linear_to_page(struct page *page, unsigned int *len,
1478                                           unsigned int *offset,
1479                                           struct sk_buff *skb, struct sock *sk)
1480 {
1481         struct page *p = sk->sk_sndmsg_page;
1482         unsigned int off;
1483
1484         if (!p) {
1485 new_page:
1486                 p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
1487                 if (!p)
1488                         return NULL;
1489
1490                 off = sk->sk_sndmsg_off = 0;
1491                 /* hold one ref to this page until it's full */
1492         } else {
1493                 unsigned int mlen;
1494
1495                 off = sk->sk_sndmsg_off;
1496                 mlen = PAGE_SIZE - off;
1497                 if (mlen < 64 && mlen < *len) {
1498                         put_page(p);
1499                         goto new_page;
1500                 }
1501
1502                 *len = min_t(unsigned int, *len, mlen);
1503         }
1504
1505         memcpy(page_address(p) + off, page_address(page) + *offset, *len);
1506         sk->sk_sndmsg_off += *len;
1507         *offset = off;
1508         get_page(p);
1509
1510         return p;
1511 }
1512
1513 /*
1514  * Fill page/offset/length into spd, if it can hold more pages.
1515  */
1516 static inline int spd_fill_page(struct splice_pipe_desc *spd,
1517                                 struct pipe_inode_info *pipe, struct page *page,
1518                                 unsigned int *len, unsigned int offset,
1519                                 struct sk_buff *skb, int linear,
1520                                 struct sock *sk)
1521 {
1522         if (unlikely(spd->nr_pages == pipe->buffers))
1523                 return 1;
1524
1525         if (linear) {
1526                 page = linear_to_page(page, len, &offset, skb, sk);
1527                 if (!page)
1528                         return 1;
1529         } else
1530                 get_page(page);
1531
1532         spd->pages[spd->nr_pages] = page;
1533         spd->partial[spd->nr_pages].len = *len;
1534         spd->partial[spd->nr_pages].offset = offset;
1535         spd->nr_pages++;
1536
1537         return 0;
1538 }
1539
1540 static inline void __segment_seek(struct page **page, unsigned int *poff,
1541                                   unsigned int *plen, unsigned int off)
1542 {
1543         unsigned long n;
1544
1545         *poff += off;
1546         n = *poff / PAGE_SIZE;
1547         if (n)
1548                 *page = nth_page(*page, n);
1549
1550         *poff = *poff % PAGE_SIZE;
1551         *plen -= off;
1552 }
1553
1554 static inline int __splice_segment(struct page *page, unsigned int poff,
1555                                    unsigned int plen, unsigned int *off,
1556                                    unsigned int *len, struct sk_buff *skb,
1557                                    struct splice_pipe_desc *spd, int linear,
1558                                    struct sock *sk,
1559                                    struct pipe_inode_info *pipe)
1560 {
1561         if (!*len)
1562                 return 1;
1563
1564         /* skip this segment if already processed */
1565         if (*off >= plen) {
1566                 *off -= plen;
1567                 return 0;
1568         }
1569
1570         /* ignore any bits we already processed */
1571         if (*off) {
1572                 __segment_seek(&page, &poff, &plen, *off);
1573                 *off = 0;
1574         }
1575
1576         do {
1577                 unsigned int flen = min(*len, plen);
1578
1579                 /* the linear region may spread across several pages  */
1580                 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1581
1582                 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
1583                         return 1;
1584
1585                 __segment_seek(&page, &poff, &plen, flen);
1586                 *len -= flen;
1587
1588         } while (*len && plen);
1589
1590         return 0;
1591 }
1592
1593 /*
1594  * Map linear and fragment data from the skb to spd. It reports failure if the
1595  * pipe is full or if we already spliced the requested length.
1596  */
1597 static int __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1598                              unsigned int *offset, unsigned int *len,
1599                              struct splice_pipe_desc *spd, struct sock *sk)
1600 {
1601         int seg;
1602
1603         /*
1604          * map the linear part
1605          */
1606         if (__splice_segment(virt_to_page(skb->data),
1607                              (unsigned long) skb->data & (PAGE_SIZE - 1),
1608                              skb_headlen(skb),
1609                              offset, len, skb, spd, 1, sk, pipe))
1610                 return 1;
1611
1612         /*
1613          * then map the fragments
1614          */
1615         for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1616                 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1617
1618                 if (__splice_segment(skb_frag_page(f),
1619                                      f->page_offset, skb_frag_size(f),
1620                                      offset, len, skb, spd, 0, sk, pipe))
1621                         return 1;
1622         }
1623
1624         return 0;
1625 }
1626
1627 /*
1628  * Map data from the skb to a pipe. Should handle both the linear part,
1629  * the fragments, and the frag list. It does NOT handle frag lists within
1630  * the frag list, if such a thing exists. We'd probably need to recurse to
1631  * handle that cleanly.
1632  */
1633 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1634                     struct pipe_inode_info *pipe, unsigned int tlen,
1635                     unsigned int flags)
1636 {
1637         struct partial_page partial[PIPE_DEF_BUFFERS];
1638         struct page *pages[PIPE_DEF_BUFFERS];
1639         struct splice_pipe_desc spd = {
1640                 .pages = pages,
1641                 .partial = partial,
1642                 .nr_pages_max = MAX_SKB_FRAGS,
1643                 .flags = flags,
1644                 .ops = &nosteal_pipe_buf_ops,
1645                 .spd_release = sock_spd_release,
1646         };
1647         struct sk_buff *frag_iter;
1648         struct sock *sk = skb->sk;
1649         int ret = 0;
1650
1651         if (splice_grow_spd(pipe, &spd))
1652                 return -ENOMEM;
1653
1654         /*
1655          * __skb_splice_bits() only fails if the output has no room left,
1656          * so no point in going over the frag_list for the error case.
1657          */
1658         if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1659                 goto done;
1660         else if (!tlen)
1661                 goto done;
1662
1663         /*
1664          * now see if we have a frag_list to map
1665          */
1666         skb_walk_frags(skb, frag_iter) {
1667                 if (!tlen)
1668                         break;
1669                 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1670                         break;
1671         }
1672
1673 done:
1674         if (spd.nr_pages) {
1675                 /*
1676                  * Drop the socket lock, otherwise we have reverse
1677                  * locking dependencies between sk_lock and i_mutex
1678                  * here as compared to sendfile(). We enter here
1679                  * with the socket lock held, and splice_to_pipe() will
1680                  * grab the pipe inode lock. For sendfile() emulation,
1681                  * we call into ->sendpage() with the i_mutex lock held
1682                  * and networking will grab the socket lock.
1683                  */
1684                 release_sock(sk);
1685                 ret = splice_to_pipe(pipe, &spd);
1686                 lock_sock(sk);
1687         }
1688
1689         splice_shrink_spd(&spd);
1690         return ret;
1691 }
1692
1693 /**
1694  *      skb_store_bits - store bits from kernel buffer to skb
1695  *      @skb: destination buffer
1696  *      @offset: offset in destination
1697  *      @from: source buffer
1698  *      @len: number of bytes to copy
1699  *
1700  *      Copy the specified number of bytes from the source buffer to the
1701  *      destination skb.  This function handles all the messy bits of
1702  *      traversing fragment lists and such.
1703  */
1704
1705 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1706 {
1707         int start = skb_headlen(skb);
1708         struct sk_buff *frag_iter;
1709         int i, copy;
1710
1711         if (offset > (int)skb->len - len)
1712                 goto fault;
1713
1714         if ((copy = start - offset) > 0) {
1715                 if (copy > len)
1716                         copy = len;
1717                 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1718                 if ((len -= copy) == 0)
1719                         return 0;
1720                 offset += copy;
1721                 from += copy;
1722         }
1723
1724         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1725                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1726                 int end;
1727
1728                 WARN_ON(start > offset + len);
1729
1730                 end = start + skb_frag_size(frag);
1731                 if ((copy = end - offset) > 0) {
1732                         u8 *vaddr;
1733
1734                         if (copy > len)
1735                                 copy = len;
1736
1737                         vaddr = kmap_skb_frag(frag);
1738                         memcpy(vaddr + frag->page_offset + offset - start,
1739                                from, copy);
1740                         kunmap_skb_frag(vaddr);
1741
1742                         if ((len -= copy) == 0)
1743                                 return 0;
1744                         offset += copy;
1745                         from += copy;
1746                 }
1747                 start = end;
1748         }
1749
1750         skb_walk_frags(skb, frag_iter) {
1751                 int end;
1752
1753                 WARN_ON(start > offset + len);
1754
1755                 end = start + frag_iter->len;
1756                 if ((copy = end - offset) > 0) {
1757                         if (copy > len)
1758                                 copy = len;
1759                         if (skb_store_bits(frag_iter, offset - start,
1760                                            from, copy))
1761                                 goto fault;
1762                         if ((len -= copy) == 0)
1763                                 return 0;
1764                         offset += copy;
1765                         from += copy;
1766                 }
1767                 start = end;
1768         }
1769         if (!len)
1770                 return 0;
1771
1772 fault:
1773         return -EFAULT;
1774 }
1775 EXPORT_SYMBOL(skb_store_bits);
1776
1777 /* Checksum skb data. */
1778
1779 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1780                           int len, __wsum csum)
1781 {
1782         int start = skb_headlen(skb);
1783         int i, copy = start - offset;
1784         struct sk_buff *frag_iter;
1785         int pos = 0;
1786
1787         /* Checksum header. */
1788         if (copy > 0) {
1789                 if (copy > len)
1790                         copy = len;
1791                 csum = csum_partial(skb->data + offset, copy, csum);
1792                 if ((len -= copy) == 0)
1793                         return csum;
1794                 offset += copy;
1795                 pos     = copy;
1796         }
1797
1798         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1799                 int end;
1800
1801                 WARN_ON(start > offset + len);
1802
1803                 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1804                 if ((copy = end - offset) > 0) {
1805                         __wsum csum2;
1806                         u8 *vaddr;
1807                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1808
1809                         if (copy > len)
1810                                 copy = len;
1811                         vaddr = kmap_skb_frag(frag);
1812                         csum2 = csum_partial(vaddr + frag->page_offset +
1813                                              offset - start, copy, 0);
1814                         kunmap_skb_frag(vaddr);
1815                         csum = csum_block_add(csum, csum2, pos);
1816                         if (!(len -= copy))
1817                                 return csum;
1818                         offset += copy;
1819                         pos    += copy;
1820                 }
1821                 start = end;
1822         }
1823
1824         skb_walk_frags(skb, frag_iter) {
1825                 int end;
1826
1827                 WARN_ON(start > offset + len);
1828
1829                 end = start + frag_iter->len;
1830                 if ((copy = end - offset) > 0) {
1831                         __wsum csum2;
1832                         if (copy > len)
1833                                 copy = len;
1834                         csum2 = skb_checksum(frag_iter, offset - start,
1835                                              copy, 0);
1836                         csum = csum_block_add(csum, csum2, pos);
1837                         if ((len -= copy) == 0)
1838                                 return csum;
1839                         offset += copy;
1840                         pos    += copy;
1841                 }
1842                 start = end;
1843         }
1844         BUG_ON(len);
1845
1846         return csum;
1847 }
1848 EXPORT_SYMBOL(skb_checksum);
1849
1850 /* Both of above in one bottle. */
1851
1852 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1853                                     u8 *to, int len, __wsum csum)
1854 {
1855         int start = skb_headlen(skb);
1856         int i, copy = start - offset;
1857         struct sk_buff *frag_iter;
1858         int pos = 0;
1859
1860         /* Copy header. */
1861         if (copy > 0) {
1862                 if (copy > len)
1863                         copy = len;
1864                 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1865                                                  copy, csum);
1866                 if ((len -= copy) == 0)
1867                         return csum;
1868                 offset += copy;
1869                 to     += copy;
1870                 pos     = copy;
1871         }
1872
1873         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1874                 int end;
1875
1876                 WARN_ON(start > offset + len);
1877
1878                 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1879                 if ((copy = end - offset) > 0) {
1880                         __wsum csum2;
1881                         u8 *vaddr;
1882                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1883
1884                         if (copy > len)
1885                                 copy = len;
1886                         vaddr = kmap_skb_frag(frag);
1887                         csum2 = csum_partial_copy_nocheck(vaddr +
1888                                                           frag->page_offset +
1889                                                           offset - start, to,
1890                                                           copy, 0);
1891                         kunmap_skb_frag(vaddr);
1892                         csum = csum_block_add(csum, csum2, pos);
1893                         if (!(len -= copy))
1894                                 return csum;
1895                         offset += copy;
1896                         to     += copy;
1897                         pos    += copy;
1898                 }
1899                 start = end;
1900         }
1901
1902         skb_walk_frags(skb, frag_iter) {
1903                 __wsum csum2;
1904                 int end;
1905
1906                 WARN_ON(start > offset + len);
1907
1908                 end = start + frag_iter->len;
1909                 if ((copy = end - offset) > 0) {
1910                         if (copy > len)
1911                                 copy = len;
1912                         csum2 = skb_copy_and_csum_bits(frag_iter,
1913                                                        offset - start,
1914                                                        to, copy, 0);
1915                         csum = csum_block_add(csum, csum2, pos);
1916                         if ((len -= copy) == 0)
1917                                 return csum;
1918                         offset += copy;
1919                         to     += copy;
1920                         pos    += copy;
1921                 }
1922                 start = end;
1923         }
1924         BUG_ON(len);
1925         return csum;
1926 }
1927 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1928
1929 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1930 {
1931         __wsum csum;
1932         long csstart;
1933
1934         if (skb->ip_summed == CHECKSUM_PARTIAL)
1935                 csstart = skb_checksum_start_offset(skb);
1936         else
1937                 csstart = skb_headlen(skb);
1938
1939         BUG_ON(csstart > skb_headlen(skb));
1940
1941         skb_copy_from_linear_data(skb, to, csstart);
1942
1943         csum = 0;
1944         if (csstart != skb->len)
1945                 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1946                                               skb->len - csstart, 0);
1947
1948         if (skb->ip_summed == CHECKSUM_PARTIAL) {
1949                 long csstuff = csstart + skb->csum_offset;
1950
1951                 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1952         }
1953 }
1954 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1955
1956 /**
1957  *      skb_dequeue - remove from the head of the queue
1958  *      @list: list to dequeue from
1959  *
1960  *      Remove the head of the list. The list lock is taken so the function
1961  *      may be used safely with other locking list functions. The head item is
1962  *      returned or %NULL if the list is empty.
1963  */
1964
1965 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1966 {
1967         unsigned long flags;
1968         struct sk_buff *result;
1969
1970         spin_lock_irqsave(&list->lock, flags);
1971         result = __skb_dequeue(list);
1972         spin_unlock_irqrestore(&list->lock, flags);
1973         return result;
1974 }
1975 EXPORT_SYMBOL(skb_dequeue);
1976
1977 /**
1978  *      skb_dequeue_tail - remove from the tail of the queue
1979  *      @list: list to dequeue from
1980  *
1981  *      Remove the tail of the list. The list lock is taken so the function
1982  *      may be used safely with other locking list functions. The tail item is
1983  *      returned or %NULL if the list is empty.
1984  */
1985 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1986 {
1987         unsigned long flags;
1988         struct sk_buff *result;
1989
1990         spin_lock_irqsave(&list->lock, flags);
1991         result = __skb_dequeue_tail(list);
1992         spin_unlock_irqrestore(&list->lock, flags);
1993         return result;
1994 }
1995 EXPORT_SYMBOL(skb_dequeue_tail);
1996
1997 /**
1998  *      skb_queue_purge - empty a list
1999  *      @list: list to empty
2000  *
2001  *      Delete all buffers on an &sk_buff list. Each buffer is removed from
2002  *      the list and one reference dropped. This function takes the list
2003  *      lock and is atomic with respect to other list locking functions.
2004  */
2005 void skb_queue_purge(struct sk_buff_head *list)
2006 {
2007         struct sk_buff *skb;
2008         while ((skb = skb_dequeue(list)) != NULL)
2009                 kfree_skb(skb);
2010 }
2011 EXPORT_SYMBOL(skb_queue_purge);
2012
2013 /**
2014  *      skb_queue_head - queue a buffer at the list head
2015  *      @list: list to use
2016  *      @newsk: buffer to queue
2017  *
2018  *      Queue a buffer at the start of the list. This function takes the
2019  *      list lock and can be used safely with other locking &sk_buff functions
2020  *      safely.
2021  *
2022  *      A buffer cannot be placed on two lists at the same time.
2023  */
2024 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2025 {
2026         unsigned long flags;
2027
2028         spin_lock_irqsave(&list->lock, flags);
2029         __skb_queue_head(list, newsk);
2030         spin_unlock_irqrestore(&list->lock, flags);
2031 }
2032 EXPORT_SYMBOL(skb_queue_head);
2033
2034 /**
2035  *      skb_queue_tail - queue a buffer at the list tail
2036  *      @list: list to use
2037  *      @newsk: buffer to queue
2038  *
2039  *      Queue a buffer at the tail of the list. This function takes the
2040  *      list lock and can be used safely with other locking &sk_buff functions
2041  *      safely.
2042  *
2043  *      A buffer cannot be placed on two lists at the same time.
2044  */
2045 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2046 {
2047         unsigned long flags;
2048
2049         spin_lock_irqsave(&list->lock, flags);
2050         __skb_queue_tail(list, newsk);
2051         spin_unlock_irqrestore(&list->lock, flags);
2052 }
2053 EXPORT_SYMBOL(skb_queue_tail);
2054
2055 /**
2056  *      skb_unlink      -       remove a buffer from a list
2057  *      @skb: buffer to remove
2058  *      @list: list to use
2059  *
2060  *      Remove a packet from a list. The list locks are taken and this
2061  *      function is atomic with respect to other list locked calls
2062  *
2063  *      You must know what list the SKB is on.
2064  */
2065 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2066 {
2067         unsigned long flags;
2068
2069         spin_lock_irqsave(&list->lock, flags);
2070         __skb_unlink(skb, list);
2071         spin_unlock_irqrestore(&list->lock, flags);
2072 }
2073 EXPORT_SYMBOL(skb_unlink);
2074
2075 /**
2076  *      skb_append      -       append a buffer
2077  *      @old: buffer to insert after
2078  *      @newsk: buffer to insert
2079  *      @list: list to use
2080  *
2081  *      Place a packet after a given packet in a list. The list locks are taken
2082  *      and this function is atomic with respect to other list locked calls.
2083  *      A buffer cannot be placed on two lists at the same time.
2084  */
2085 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2086 {
2087         unsigned long flags;
2088
2089         spin_lock_irqsave(&list->lock, flags);
2090         __skb_queue_after(list, old, newsk);
2091         spin_unlock_irqrestore(&list->lock, flags);
2092 }
2093 EXPORT_SYMBOL(skb_append);
2094
2095 /**
2096  *      skb_insert      -       insert a buffer
2097  *      @old: buffer to insert before
2098  *      @newsk: buffer to insert
2099  *      @list: list to use
2100  *
2101  *      Place a packet before a given packet in a list. The list locks are
2102  *      taken and this function is atomic with respect to other list locked
2103  *      calls.
2104  *
2105  *      A buffer cannot be placed on two lists at the same time.
2106  */
2107 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2108 {
2109         unsigned long flags;
2110
2111         spin_lock_irqsave(&list->lock, flags);
2112         __skb_insert(newsk, old->prev, old, list);
2113         spin_unlock_irqrestore(&list->lock, flags);
2114 }
2115 EXPORT_SYMBOL(skb_insert);
2116
2117 static inline void skb_split_inside_header(struct sk_buff *skb,
2118                                            struct sk_buff* skb1,
2119                                            const u32 len, const int pos)
2120 {
2121         int i;
2122
2123         skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2124                                          pos - len);
2125         /* And move data appendix as is. */
2126         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2127                 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2128
2129         skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2130         skb_shinfo(skb)->nr_frags  = 0;
2131         skb1->data_len             = skb->data_len;
2132         skb1->len                  += skb1->data_len;
2133         skb->data_len              = 0;
2134         skb->len                   = len;
2135         skb_set_tail_pointer(skb, len);
2136 }
2137
2138 static inline void skb_split_no_header(struct sk_buff *skb,
2139                                        struct sk_buff* skb1,
2140                                        const u32 len, int pos)
2141 {
2142         int i, k = 0;
2143         const int nfrags = skb_shinfo(skb)->nr_frags;
2144
2145         skb_shinfo(skb)->nr_frags = 0;
2146         skb1->len                 = skb1->data_len = skb->len - len;
2147         skb->len                  = len;
2148         skb->data_len             = len - pos;
2149
2150         for (i = 0; i < nfrags; i++) {
2151                 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2152
2153                 if (pos + size > len) {
2154                         skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2155
2156                         if (pos < len) {
2157                                 /* Split frag.
2158                                  * We have two variants in this case:
2159                                  * 1. Move all the frag to the second
2160                                  *    part, if it is possible. F.e.
2161                                  *    this approach is mandatory for TUX,
2162                                  *    where splitting is expensive.
2163                                  * 2. Split is accurately. We make this.
2164                                  */
2165                                 skb_frag_ref(skb, i);
2166                                 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2167                                 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2168                                 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2169                                 skb_shinfo(skb)->nr_frags++;
2170                         }
2171                         k++;
2172                 } else
2173                         skb_shinfo(skb)->nr_frags++;
2174                 pos += size;
2175         }
2176         skb_shinfo(skb1)->nr_frags = k;
2177 }
2178
2179 /**
2180  * skb_split - Split fragmented skb to two parts at length len.
2181  * @skb: the buffer to split
2182  * @skb1: the buffer to receive the second part
2183  * @len: new length for skb
2184  */
2185 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2186 {
2187         int pos = skb_headlen(skb);
2188
2189         if (len < pos)  /* Split line is inside header. */
2190                 skb_split_inside_header(skb, skb1, len, pos);
2191         else            /* Second chunk has no header, nothing to copy. */
2192                 skb_split_no_header(skb, skb1, len, pos);
2193 }
2194 EXPORT_SYMBOL(skb_split);
2195
2196 /* Shifting from/to a cloned skb is a no-go.
2197  *
2198  * Caller cannot keep skb_shinfo related pointers past calling here!
2199  */
2200 static int skb_prepare_for_shift(struct sk_buff *skb)
2201 {
2202         return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2203 }
2204
2205 /**
2206  * skb_shift - Shifts paged data partially from skb to another
2207  * @tgt: buffer into which tail data gets added
2208  * @skb: buffer from which the paged data comes from
2209  * @shiftlen: shift up to this many bytes
2210  *
2211  * Attempts to shift up to shiftlen worth of bytes, which may be less than
2212  * the length of the skb, from skb to tgt. Returns number bytes shifted.
2213  * It's up to caller to free skb if everything was shifted.
2214  *
2215  * If @tgt runs out of frags, the whole operation is aborted.
2216  *
2217  * Skb cannot include anything else but paged data while tgt is allowed
2218  * to have non-paged data as well.
2219  *
2220  * TODO: full sized shift could be optimized but that would need
2221  * specialized skb free'er to handle frags without up-to-date nr_frags.
2222  */
2223 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2224 {
2225         int from, to, merge, todo;
2226         struct skb_frag_struct *fragfrom, *fragto;
2227
2228         BUG_ON(shiftlen > skb->len);
2229         BUG_ON(skb_headlen(skb));       /* Would corrupt stream */
2230
2231         todo = shiftlen;
2232         from = 0;
2233         to = skb_shinfo(tgt)->nr_frags;
2234         fragfrom = &skb_shinfo(skb)->frags[from];
2235
2236         /* Actual merge is delayed until the point when we know we can
2237          * commit all, so that we don't have to undo partial changes
2238          */
2239         if (!to ||
2240             !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2241                               fragfrom->page_offset)) {
2242                 merge = -1;
2243         } else {
2244                 merge = to - 1;
2245
2246                 todo -= skb_frag_size(fragfrom);
2247                 if (todo < 0) {
2248                         if (skb_prepare_for_shift(skb) ||
2249                             skb_prepare_for_shift(tgt))
2250                                 return 0;
2251
2252                         /* All previous frag pointers might be stale! */
2253                         fragfrom = &skb_shinfo(skb)->frags[from];
2254                         fragto = &skb_shinfo(tgt)->frags[merge];
2255
2256                         skb_frag_size_add(fragto, shiftlen);
2257                         skb_frag_size_sub(fragfrom, shiftlen);
2258                         fragfrom->page_offset += shiftlen;
2259
2260                         goto onlymerged;
2261                 }
2262
2263                 from++;
2264         }
2265
2266         /* Skip full, not-fitting skb to avoid expensive operations */
2267         if ((shiftlen == skb->len) &&
2268             (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2269                 return 0;
2270
2271         if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2272                 return 0;
2273
2274         while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2275                 if (to == MAX_SKB_FRAGS)
2276                         return 0;
2277
2278                 fragfrom = &skb_shinfo(skb)->frags[from];
2279                 fragto = &skb_shinfo(tgt)->frags[to];
2280
2281                 if (todo >= skb_frag_size(fragfrom)) {
2282                         *fragto = *fragfrom;
2283                         todo -= skb_frag_size(fragfrom);
2284                         from++;
2285                         to++;
2286
2287                 } else {
2288                         __skb_frag_ref(fragfrom);
2289                         fragto->page = fragfrom->page;
2290                         fragto->page_offset = fragfrom->page_offset;
2291                         skb_frag_size_set(fragto, todo);
2292
2293                         fragfrom->page_offset += todo;
2294                         skb_frag_size_sub(fragfrom, todo);
2295                         todo = 0;
2296
2297                         to++;
2298                         break;
2299                 }
2300         }
2301
2302         /* Ready to "commit" this state change to tgt */
2303         skb_shinfo(tgt)->nr_frags = to;
2304
2305         if (merge >= 0) {
2306                 fragfrom = &skb_shinfo(skb)->frags[0];
2307                 fragto = &skb_shinfo(tgt)->frags[merge];
2308
2309                 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2310                 __skb_frag_unref(fragfrom);
2311         }
2312
2313         /* Reposition in the original skb */
2314         to = 0;
2315         while (from < skb_shinfo(skb)->nr_frags)
2316                 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2317         skb_shinfo(skb)->nr_frags = to;
2318
2319         BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2320
2321 onlymerged:
2322         /* Most likely the tgt won't ever need its checksum anymore, skb on
2323          * the other hand might need it if it needs to be resent
2324          */
2325         tgt->ip_summed = CHECKSUM_PARTIAL;
2326         skb->ip_summed = CHECKSUM_PARTIAL;
2327
2328         /* Yak, is it really working this way? Some helper please? */
2329         skb->len -= shiftlen;
2330         skb->data_len -= shiftlen;
2331         skb->truesize -= shiftlen;
2332         tgt->len += shiftlen;
2333         tgt->data_len += shiftlen;
2334         tgt->truesize += shiftlen;
2335
2336         return shiftlen;
2337 }
2338
2339 /**
2340  * skb_prepare_seq_read - Prepare a sequential read of skb data
2341  * @skb: the buffer to read
2342  * @from: lower offset of data to be read
2343  * @to: upper offset of data to be read
2344  * @st: state variable
2345  *
2346  * Initializes the specified state variable. Must be called before
2347  * invoking skb_seq_read() for the first time.
2348  */
2349 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2350                           unsigned int to, struct skb_seq_state *st)
2351 {
2352         st->lower_offset = from;
2353         st->upper_offset = to;
2354         st->root_skb = st->cur_skb = skb;
2355         st->frag_idx = st->stepped_offset = 0;
2356         st->frag_data = NULL;
2357 }
2358 EXPORT_SYMBOL(skb_prepare_seq_read);
2359
2360 /**
2361  * skb_seq_read - Sequentially read skb data
2362  * @consumed: number of bytes consumed by the caller so far
2363  * @data: destination pointer for data to be returned
2364  * @st: state variable
2365  *
2366  * Reads a block of skb data at &consumed relative to the
2367  * lower offset specified to skb_prepare_seq_read(). Assigns
2368  * the head of the data block to &data and returns the length
2369  * of the block or 0 if the end of the skb data or the upper
2370  * offset has been reached.
2371  *
2372  * The caller is not required to consume all of the data
2373  * returned, i.e. &consumed is typically set to the number
2374  * of bytes already consumed and the next call to
2375  * skb_seq_read() will return the remaining part of the block.
2376  *
2377  * Note 1: The size of each block of data returned can be arbitrary,
2378  *       this limitation is the cost for zerocopy seqeuental
2379  *       reads of potentially non linear data.
2380  *
2381  * Note 2: Fragment lists within fragments are not implemented
2382  *       at the moment, state->root_skb could be replaced with
2383  *       a stack for this purpose.
2384  */
2385 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2386                           struct skb_seq_state *st)
2387 {
2388         unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2389         skb_frag_t *frag;
2390
2391         if (unlikely(abs_offset >= st->upper_offset))
2392                 return 0;
2393
2394 next_skb:
2395         block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2396
2397         if (abs_offset < block_limit && !st->frag_data) {
2398                 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2399                 return block_limit - abs_offset;
2400         }
2401
2402         if (st->frag_idx == 0 && !st->frag_data)
2403                 st->stepped_offset += skb_headlen(st->cur_skb);
2404
2405         while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2406                 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2407                 block_limit = skb_frag_size(frag) + st->stepped_offset;
2408
2409                 if (abs_offset < block_limit) {
2410                         if (!st->frag_data)
2411                                 st->frag_data = kmap_skb_frag(frag);
2412
2413                         *data = (u8 *) st->frag_data + frag->page_offset +
2414                                 (abs_offset - st->stepped_offset);
2415
2416                         return block_limit - abs_offset;
2417                 }
2418
2419                 if (st->frag_data) {
2420                         kunmap_skb_frag(st->frag_data);
2421                         st->frag_data = NULL;
2422                 }
2423
2424                 st->frag_idx++;
2425                 st->stepped_offset += skb_frag_size(frag);
2426         }
2427
2428         if (st->frag_data) {
2429                 kunmap_skb_frag(st->frag_data);
2430                 st->frag_data = NULL;
2431         }
2432
2433         if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2434                 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2435                 st->frag_idx = 0;
2436                 goto next_skb;
2437         } else if (st->cur_skb->next) {
2438                 st->cur_skb = st->cur_skb->next;
2439                 st->frag_idx = 0;
2440                 goto next_skb;
2441         }
2442
2443         return 0;
2444 }
2445 EXPORT_SYMBOL(skb_seq_read);
2446
2447 /**
2448  * skb_abort_seq_read - Abort a sequential read of skb data
2449  * @st: state variable
2450  *
2451  * Must be called if skb_seq_read() was not called until it
2452  * returned 0.
2453  */
2454 void skb_abort_seq_read(struct skb_seq_state *st)
2455 {
2456         if (st->frag_data)
2457                 kunmap_skb_frag(st->frag_data);
2458 }
2459 EXPORT_SYMBOL(skb_abort_seq_read);
2460
2461 #define TS_SKB_CB(state)        ((struct skb_seq_state *) &((state)->cb))
2462
2463 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2464                                           struct ts_config *conf,
2465                                           struct ts_state *state)
2466 {
2467         return skb_seq_read(offset, text, TS_SKB_CB(state));
2468 }
2469
2470 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2471 {
2472         skb_abort_seq_read(TS_SKB_CB(state));
2473 }
2474
2475 /**
2476  * skb_find_text - Find a text pattern in skb data
2477  * @skb: the buffer to look in
2478  * @from: search offset
2479  * @to: search limit
2480  * @config: textsearch configuration
2481  * @state: uninitialized textsearch state variable
2482  *
2483  * Finds a pattern in the skb data according to the specified
2484  * textsearch configuration. Use textsearch_next() to retrieve
2485  * subsequent occurrences of the pattern. Returns the offset
2486  * to the first occurrence or UINT_MAX if no match was found.
2487  */
2488 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2489                            unsigned int to, struct ts_config *config,
2490                            struct ts_state *state)
2491 {
2492         unsigned int ret;
2493
2494         config->get_next_block = skb_ts_get_next_block;
2495         config->finish = skb_ts_finish;
2496
2497         skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2498
2499         ret = textsearch_find(config, state);
2500         return (ret <= to - from ? ret : UINT_MAX);
2501 }
2502 EXPORT_SYMBOL(skb_find_text);
2503
2504 /**
2505  * skb_append_datato_frags: - append the user data to a skb
2506  * @sk: sock  structure
2507  * @skb: skb structure to be appened with user data.
2508  * @getfrag: call back function to be used for getting the user data
2509  * @from: pointer to user message iov
2510  * @length: length of the iov message
2511  *
2512  * Description: This procedure append the user data in the fragment part
2513  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2514  */
2515 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2516                         int (*getfrag)(void *from, char *to, int offset,
2517                                         int len, int odd, struct sk_buff *skb),
2518                         void *from, int length)
2519 {
2520         int frg_cnt = 0;
2521         skb_frag_t *frag = NULL;
2522         struct page *page = NULL;
2523         int copy, left;
2524         int offset = 0;
2525         int ret;
2526
2527         do {
2528                 /* Return error if we don't have space for new frag */
2529                 frg_cnt = skb_shinfo(skb)->nr_frags;
2530                 if (frg_cnt >= MAX_SKB_FRAGS)
2531                         return -EFAULT;
2532
2533                 /* allocate a new page for next frag */
2534                 page = alloc_pages(sk->sk_allocation, 0);
2535
2536                 /* If alloc_page fails just return failure and caller will
2537                  * free previous allocated pages by doing kfree_skb()
2538                  */
2539                 if (page == NULL)
2540                         return -ENOMEM;
2541
2542                 /* initialize the next frag */
2543                 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2544                 skb->truesize += PAGE_SIZE;
2545                 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2546
2547                 /* get the new initialized frag */
2548                 frg_cnt = skb_shinfo(skb)->nr_frags;
2549                 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2550
2551                 /* copy the user data to page */
2552                 left = PAGE_SIZE - frag->page_offset;
2553                 copy = (length > left)? left : length;
2554
2555                 ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
2556                             offset, copy, 0, skb);
2557                 if (ret < 0)
2558                         return -EFAULT;
2559
2560                 /* copy was successful so update the size parameters */
2561                 skb_frag_size_add(frag, copy);
2562                 skb->len += copy;
2563                 skb->data_len += copy;
2564                 offset += copy;
2565                 length -= copy;
2566
2567         } while (length > 0);
2568
2569         return 0;
2570 }
2571 EXPORT_SYMBOL(skb_append_datato_frags);
2572
2573 /**
2574  *      skb_pull_rcsum - pull skb and update receive checksum
2575  *      @skb: buffer to update
2576  *      @len: length of data pulled
2577  *
2578  *      This function performs an skb_pull on the packet and updates
2579  *      the CHECKSUM_COMPLETE checksum.  It should be used on
2580  *      receive path processing instead of skb_pull unless you know
2581  *      that the checksum difference is zero (e.g., a valid IP header)
2582  *      or you are setting ip_summed to CHECKSUM_NONE.
2583  */
2584 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2585 {
2586         unsigned char *data = skb->data;
2587
2588         BUG_ON(len > skb->len);
2589         __skb_pull(skb, len);
2590         skb_postpull_rcsum(skb, data, len);
2591         return skb->data;
2592 }
2593 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2594
2595 /**
2596  *      skb_segment - Perform protocol segmentation on skb.
2597  *      @skb: buffer to segment
2598  *      @features: features for the output path (see dev->features)
2599  *
2600  *      This function performs segmentation on the given skb.  It returns
2601  *      a pointer to the first in a list of new skbs for the segments.
2602  *      In case of error it returns ERR_PTR(err).
2603  */
2604 struct sk_buff *skb_segment(struct sk_buff *skb, u32 features)
2605 {
2606         struct sk_buff *segs = NULL;
2607         struct sk_buff *tail = NULL;
2608         struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2609         unsigned int mss = skb_shinfo(skb)->gso_size;
2610         unsigned int doffset = skb->data - skb_mac_header(skb);
2611         unsigned int offset = doffset;
2612         unsigned int headroom;
2613         unsigned int len;
2614         int sg = !!(features & NETIF_F_SG);
2615         int nfrags = skb_shinfo(skb)->nr_frags;
2616         int err = -ENOMEM;
2617         int i = 0;
2618         int pos;
2619
2620         __skb_push(skb, doffset);
2621         headroom = skb_headroom(skb);
2622         pos = skb_headlen(skb);
2623
2624         do {
2625                 struct sk_buff *nskb;
2626                 skb_frag_t *frag;
2627                 int hsize;
2628                 int size;
2629
2630                 len = skb->len - offset;
2631                 if (len > mss)
2632                         len = mss;
2633
2634                 hsize = skb_headlen(skb) - offset;
2635                 if (hsize < 0)
2636                         hsize = 0;
2637                 if (hsize > len || !sg)
2638                         hsize = len;
2639
2640                 if (!hsize && i >= nfrags) {
2641                         BUG_ON(fskb->len != len);
2642
2643                         pos += len;
2644                         nskb = skb_clone(fskb, GFP_ATOMIC);
2645                         fskb = fskb->next;
2646
2647                         if (unlikely(!nskb))
2648                                 goto err;
2649
2650                         hsize = skb_end_offset(nskb);
2651                         if (skb_cow_head(nskb, doffset + headroom)) {
2652                                 kfree_skb(nskb);
2653                                 goto err;
2654                         }
2655
2656                         nskb->truesize += skb_end_offset(nskb) - hsize;
2657                         skb_release_head_state(nskb);
2658                         __skb_push(nskb, doffset);
2659                 } else {
2660                         nskb = alloc_skb(hsize + doffset + headroom,
2661                                          GFP_ATOMIC);
2662
2663                         if (unlikely(!nskb))
2664                                 goto err;
2665
2666                         skb_reserve(nskb, headroom);
2667                         __skb_put(nskb, doffset);
2668                 }
2669
2670                 if (segs)
2671                         tail->next = nskb;
2672                 else
2673                         segs = nskb;
2674                 tail = nskb;
2675
2676                 __copy_skb_header(nskb, skb);
2677                 nskb->mac_len = skb->mac_len;
2678
2679                 /* nskb and skb might have different headroom */
2680                 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2681                         nskb->csum_start += skb_headroom(nskb) - headroom;
2682
2683                 skb_reset_mac_header(nskb);
2684                 skb_set_network_header(nskb, skb->mac_len);
2685                 nskb->transport_header = (nskb->network_header +
2686                                           skb_network_header_len(skb));
2687                 skb_copy_from_linear_data(skb, nskb->data, doffset);
2688
2689                 if (fskb != skb_shinfo(skb)->frag_list)
2690                         continue;
2691
2692                 if (!sg) {
2693                         nskb->ip_summed = CHECKSUM_NONE;
2694                         nskb->csum = skb_copy_and_csum_bits(skb, offset,
2695                                                             skb_put(nskb, len),
2696                                                             len, 0);
2697                         continue;
2698                 }
2699
2700                 frag = skb_shinfo(nskb)->frags;
2701
2702                 skb_copy_from_linear_data_offset(skb, offset,
2703                                                  skb_put(nskb, hsize), hsize);
2704
2705                 while (pos < offset + len && i < nfrags) {
2706                         if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
2707                                 goto err;
2708
2709                         *frag = skb_shinfo(skb)->frags[i];
2710                         __skb_frag_ref(frag);
2711                         size = skb_frag_size(frag);
2712
2713                         if (pos < offset) {
2714                                 frag->page_offset += offset - pos;
2715                                 skb_frag_size_sub(frag, offset - pos);
2716                         }
2717
2718                         skb_shinfo(nskb)->nr_frags++;
2719
2720                         if (pos + size <= offset + len) {
2721                                 i++;
2722                                 pos += size;
2723                         } else {
2724                                 skb_frag_size_sub(frag, pos + size - (offset + len));
2725                                 goto skip_fraglist;
2726                         }
2727
2728                         frag++;
2729                 }
2730
2731                 if (pos < offset + len) {
2732                         struct sk_buff *fskb2 = fskb;
2733
2734                         BUG_ON(pos + fskb->len != offset + len);
2735
2736                         pos += fskb->len;
2737                         fskb = fskb->next;
2738
2739                         if (fskb2->next) {
2740                                 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2741                                 if (!fskb2)
2742                                         goto err;
2743                         } else
2744                                 skb_get(fskb2);
2745
2746                         SKB_FRAG_ASSERT(nskb);
2747                         skb_shinfo(nskb)->frag_list = fskb2;
2748                 }
2749
2750 skip_fraglist:
2751                 nskb->data_len = len - hsize;
2752                 nskb->len += nskb->data_len;
2753                 nskb->truesize += nskb->data_len;
2754         } while ((offset += len) < skb->len);
2755
2756         return segs;
2757
2758 err:
2759         while ((skb = segs)) {
2760                 segs = skb->next;
2761                 kfree_skb(skb);
2762         }
2763         return ERR_PTR(err);
2764 }
2765 EXPORT_SYMBOL_GPL(skb_segment);
2766
2767 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2768 {
2769         struct sk_buff *p = *head;
2770         struct sk_buff *nskb;
2771         struct skb_shared_info *skbinfo = skb_shinfo(skb);
2772         struct skb_shared_info *pinfo = skb_shinfo(p);
2773         unsigned int headroom;
2774         unsigned int len = skb_gro_len(skb);
2775         unsigned int offset = skb_gro_offset(skb);
2776         unsigned int headlen = skb_headlen(skb);
2777
2778         if (p->len + len >= 65536)
2779                 return -E2BIG;
2780
2781         if (pinfo->frag_list)
2782                 goto merge;
2783         else if (headlen <= offset) {
2784                 skb_frag_t *frag;
2785                 skb_frag_t *frag2;
2786                 int i = skbinfo->nr_frags;
2787                 int nr_frags = pinfo->nr_frags + i;
2788
2789                 offset -= headlen;
2790
2791                 if (nr_frags > MAX_SKB_FRAGS)
2792                         return -E2BIG;
2793
2794                 pinfo->nr_frags = nr_frags;
2795                 skbinfo->nr_frags = 0;
2796
2797                 frag = pinfo->frags + nr_frags;
2798                 frag2 = skbinfo->frags + i;
2799                 do {
2800                         *--frag = *--frag2;
2801                 } while (--i);
2802
2803                 frag->page_offset += offset;
2804                 skb_frag_size_sub(frag, offset);
2805
2806                 skb->truesize -= skb->data_len;
2807                 skb->len -= skb->data_len;
2808                 skb->data_len = 0;
2809
2810                 NAPI_GRO_CB(skb)->free = 1;
2811                 goto done;
2812         } else if (skb_gro_len(p) != pinfo->gso_size)
2813                 return -E2BIG;
2814
2815         headroom = skb_headroom(p);
2816         nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
2817         if (unlikely(!nskb))
2818                 return -ENOMEM;
2819
2820         __copy_skb_header(nskb, p);
2821         nskb->mac_len = p->mac_len;
2822
2823         skb_reserve(nskb, headroom);
2824         __skb_put(nskb, skb_gro_offset(p));
2825
2826         skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
2827         skb_set_network_header(nskb, skb_network_offset(p));
2828         skb_set_transport_header(nskb, skb_transport_offset(p));
2829
2830         __skb_pull(p, skb_gro_offset(p));
2831         memcpy(skb_mac_header(nskb), skb_mac_header(p),
2832                p->data - skb_mac_header(p));
2833
2834         *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
2835         skb_shinfo(nskb)->frag_list = p;
2836         skb_shinfo(nskb)->gso_size = pinfo->gso_size;
2837         pinfo->gso_size = 0;
2838         skb_header_release(p);
2839         nskb->prev = p;
2840
2841         nskb->data_len += p->len;
2842         nskb->truesize += p->len;
2843         nskb->len += p->len;
2844
2845         *head = nskb;
2846         nskb->next = p->next;
2847         p->next = NULL;
2848
2849         p = nskb;
2850
2851 merge:
2852         if (offset > headlen) {
2853                 unsigned int eat = offset - headlen;
2854
2855                 skbinfo->frags[0].page_offset += eat;
2856                 skb_frag_size_sub(&skbinfo->frags[0], eat);
2857                 skb->data_len -= eat;
2858                 skb->len -= eat;
2859                 offset = headlen;
2860         }
2861
2862         __skb_pull(skb, offset);
2863
2864         p->prev->next = skb;
2865         p->prev = skb;
2866         skb_header_release(skb);
2867
2868 done:
2869         NAPI_GRO_CB(p)->count++;
2870         p->data_len += len;
2871         p->truesize += len;
2872         p->len += len;
2873
2874         NAPI_GRO_CB(skb)->same_flow = 1;
2875         return 0;
2876 }
2877 EXPORT_SYMBOL_GPL(skb_gro_receive);
2878
2879 void __init skb_init(void)
2880 {
2881         skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2882                                               sizeof(struct sk_buff),
2883                                               0,
2884                                               SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2885                                               NULL);
2886         skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2887                                                 (2*sizeof(struct sk_buff)) +
2888                                                 sizeof(atomic_t),
2889                                                 0,
2890                                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2891                                                 NULL);
2892 }
2893
2894 /**
2895  *      skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2896  *      @skb: Socket buffer containing the buffers to be mapped
2897  *      @sg: The scatter-gather list to map into
2898  *      @offset: The offset into the buffer's contents to start mapping
2899  *      @len: Length of buffer space to be mapped
2900  *
2901  *      Fill the specified scatter-gather list with mappings/pointers into a
2902  *      region of the buffer space attached to a socket buffer.
2903  */
2904 static int
2905 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2906 {
2907         int start = skb_headlen(skb);
2908         int i, copy = start - offset;
2909         struct sk_buff *frag_iter;
2910         int elt = 0;
2911
2912         if (copy > 0) {
2913                 if (copy > len)
2914                         copy = len;
2915                 sg_set_buf(sg, skb->data + offset, copy);
2916                 elt++;
2917                 if ((len -= copy) == 0)
2918                         return elt;
2919                 offset += copy;
2920         }
2921
2922         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2923                 int end;
2924
2925                 WARN_ON(start > offset + len);
2926
2927                 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2928                 if ((copy = end - offset) > 0) {
2929                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2930
2931                         if (copy > len)
2932                                 copy = len;
2933                         sg_set_page(&sg[elt], skb_frag_page(frag), copy,
2934                                         frag->page_offset+offset-start);
2935                         elt++;
2936                         if (!(len -= copy))
2937                                 return elt;
2938                         offset += copy;
2939                 }
2940                 start = end;
2941         }
2942
2943         skb_walk_frags(skb, frag_iter) {
2944                 int end;
2945
2946                 WARN_ON(start > offset + len);
2947
2948                 end = start + frag_iter->len;
2949                 if ((copy = end - offset) > 0) {
2950                         if (copy > len)
2951                                 copy = len;
2952                         elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
2953                                               copy);
2954                         if ((len -= copy) == 0)
2955                                 return elt;
2956                         offset += copy;
2957                 }
2958                 start = end;
2959         }
2960         BUG_ON(len);
2961         return elt;
2962 }
2963
2964 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2965 {
2966         int nsg = __skb_to_sgvec(skb, sg, offset, len);
2967
2968         sg_mark_end(&sg[nsg - 1]);
2969
2970         return nsg;
2971 }
2972 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2973
2974 /**
2975  *      skb_cow_data - Check that a socket buffer's data buffers are writable
2976  *      @skb: The socket buffer to check.
2977  *      @tailbits: Amount of trailing space to be added
2978  *      @trailer: Returned pointer to the skb where the @tailbits space begins
2979  *
2980  *      Make sure that the data buffers attached to a socket buffer are
2981  *      writable. If they are not, private copies are made of the data buffers
2982  *      and the socket buffer is set to use these instead.
2983  *
2984  *      If @tailbits is given, make sure that there is space to write @tailbits
2985  *      bytes of data beyond current end of socket buffer.  @trailer will be
2986  *      set to point to the skb in which this space begins.
2987  *
2988  *      The number of scatterlist elements required to completely map the
2989  *      COW'd and extended socket buffer will be returned.
2990  */
2991 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2992 {
2993         int copyflag;
2994         int elt;
2995         struct sk_buff *skb1, **skb_p;
2996
2997         /* If skb is cloned or its head is paged, reallocate
2998          * head pulling out all the pages (pages are considered not writable
2999          * at the moment even if they are anonymous).
3000          */
3001         if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3002             __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3003                 return -ENOMEM;
3004
3005         /* Easy case. Most of packets will go this way. */
3006         if (!skb_has_frag_list(skb)) {
3007                 /* A little of trouble, not enough of space for trailer.
3008                  * This should not happen, when stack is tuned to generate
3009                  * good frames. OK, on miss we reallocate and reserve even more
3010                  * space, 128 bytes is fair. */
3011
3012                 if (skb_tailroom(skb) < tailbits &&
3013                     pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3014                         return -ENOMEM;
3015
3016                 /* Voila! */
3017                 *trailer = skb;
3018                 return 1;
3019         }
3020
3021         /* Misery. We are in troubles, going to mincer fragments... */
3022
3023         elt = 1;
3024         skb_p = &skb_shinfo(skb)->frag_list;
3025         copyflag = 0;
3026
3027         while ((skb1 = *skb_p) != NULL) {
3028                 int ntail = 0;
3029
3030                 /* The fragment is partially pulled by someone,
3031                  * this can happen on input. Copy it and everything
3032                  * after it. */
3033
3034                 if (skb_shared(skb1))
3035                         copyflag = 1;
3036
3037                 /* If the skb is the last, worry about trailer. */
3038
3039                 if (skb1->next == NULL && tailbits) {
3040                         if (skb_shinfo(skb1)->nr_frags ||
3041                             skb_has_frag_list(skb1) ||
3042                             skb_tailroom(skb1) < tailbits)
3043                                 ntail = tailbits + 128;
3044                 }
3045
3046                 if (copyflag ||
3047                     skb_cloned(skb1) ||
3048                     ntail ||
3049                     skb_shinfo(skb1)->nr_frags ||
3050                     skb_has_frag_list(skb1)) {
3051                         struct sk_buff *skb2;
3052
3053                         /* Fuck, we are miserable poor guys... */
3054                         if (ntail == 0)
3055                                 skb2 = skb_copy(skb1, GFP_ATOMIC);
3056                         else
3057                                 skb2 = skb_copy_expand(skb1,
3058                                                        skb_headroom(skb1),
3059                                                        ntail,
3060                                                        GFP_ATOMIC);
3061                         if (unlikely(skb2 == NULL))
3062                                 return -ENOMEM;
3063
3064                         if (skb1->sk)
3065                                 skb_set_owner_w(skb2, skb1->sk);
3066
3067                         /* Looking around. Are we still alive?
3068                          * OK, link new skb, drop old one */
3069
3070                         skb2->next = skb1->next;
3071                         *skb_p = skb2;
3072                         kfree_skb(skb1);
3073                         skb1 = skb2;
3074                 }
3075                 elt++;
3076                 *trailer = skb1;
3077                 skb_p = &skb1->next;
3078         }
3079
3080         return elt;
3081 }
3082 EXPORT_SYMBOL_GPL(skb_cow_data);
3083
3084 static void sock_rmem_free(struct sk_buff *skb)
3085 {
3086         struct sock *sk = skb->sk;
3087
3088         atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3089 }
3090
3091 /*
3092  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3093  */
3094 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3095 {
3096         int len = skb->len;
3097
3098         if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3099             (unsigned)sk->sk_rcvbuf)
3100                 return -ENOMEM;
3101
3102         skb_orphan(skb);
3103         skb->sk = sk;
3104         skb->destructor = sock_rmem_free;
3105         atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3106
3107         /* before exiting rcu section, make sure dst is refcounted */
3108         skb_dst_force(skb);
3109
3110         skb_queue_tail(&sk->sk_error_queue, skb);
3111         if (!sock_flag(sk, SOCK_DEAD))
3112                 sk->sk_data_ready(sk, len);
3113         return 0;
3114 }
3115 EXPORT_SYMBOL(sock_queue_err_skb);
3116
3117 void skb_tstamp_tx(struct sk_buff *orig_skb,
3118                 struct skb_shared_hwtstamps *hwtstamps)
3119 {
3120         struct sock *sk = orig_skb->sk;
3121         struct sock_exterr_skb *serr;
3122         struct sk_buff *skb;
3123         int err;
3124
3125         if (!sk)
3126                 return;
3127
3128         skb = skb_clone(orig_skb, GFP_ATOMIC);
3129         if (!skb)
3130                 return;
3131
3132         if (hwtstamps) {
3133                 *skb_hwtstamps(skb) =
3134                         *hwtstamps;
3135         } else {
3136                 /*
3137                  * no hardware time stamps available,
3138                  * so keep the shared tx_flags and only
3139                  * store software time stamp
3140                  */
3141                 skb->tstamp = ktime_get_real();
3142         }
3143
3144         serr = SKB_EXT_ERR(skb);
3145         memset(serr, 0, sizeof(*serr));
3146         serr->ee.ee_errno = ENOMSG;
3147         serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3148
3149         err = sock_queue_err_skb(sk, skb);
3150
3151         if (err)
3152                 kfree_skb(skb);
3153 }
3154 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3155
3156
3157 /**
3158  * skb_partial_csum_set - set up and verify partial csum values for packet
3159  * @skb: the skb to set
3160  * @start: the number of bytes after skb->data to start checksumming.
3161  * @off: the offset from start to place the checksum.
3162  *
3163  * For untrusted partially-checksummed packets, we need to make sure the values
3164  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3165  *
3166  * This function checks and sets those values and skb->ip_summed: if this
3167  * returns false you should drop the packet.
3168  */
3169 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3170 {
3171         if (unlikely(start > skb_headlen(skb)) ||
3172             unlikely((int)start + off > skb_headlen(skb) - 2)) {
3173                 if (net_ratelimit())
3174                         printk(KERN_WARNING
3175                                "bad partial csum: csum=%u/%u len=%u\n",
3176                                start, off, skb_headlen(skb));
3177                 return false;
3178         }
3179         skb->ip_summed = CHECKSUM_PARTIAL;
3180         skb->csum_start = skb_headroom(skb) + start;
3181         skb->csum_offset = off;
3182         return true;
3183 }
3184 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3185
3186 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3187 {
3188         if (net_ratelimit())
3189                 pr_warning("%s: received packets cannot be forwarded"
3190                            " while LRO is enabled\n", skb->dev->name);
3191 }
3192 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3193
3194 /**
3195  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3196  *
3197  * @skb: GSO skb
3198  *
3199  * skb_gso_transport_seglen is used to determine the real size of the
3200  * individual segments, including Layer4 headers (TCP/UDP).
3201  *
3202  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3203  */
3204 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
3205 {
3206         const struct skb_shared_info *shinfo = skb_shinfo(skb);
3207
3208         if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3209                 return tcp_hdrlen(skb) + shinfo->gso_size;
3210
3211         /* UFO sets gso_size to the size of the fragmentation
3212          * payload, i.e. the size of the L4 (UDP) header is already
3213          * accounted for.
3214          */
3215         return shinfo->gso_size;
3216 }
3217 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);