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