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