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