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