skbuff: fix coalescing for page_pool fragment recycling
[platform/kernel/linux-rpi.git] / net / core / skbuff.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      Routines having to do with the 'struct sk_buff' memory handlers.
4  *
5  *      Authors:        Alan Cox <alan@lxorguk.ukuu.org.uk>
6  *                      Florian La Roche <rzsfl@rz.uni-sb.de>
7  *
8  *      Fixes:
9  *              Alan Cox        :       Fixed the worst of the load
10  *                                      balancer bugs.
11  *              Dave Platt      :       Interrupt stacking fix.
12  *      Richard Kooijman        :       Timestamp fixes.
13  *              Alan Cox        :       Changed buffer format.
14  *              Alan Cox        :       destructor hook for AF_UNIX etc.
15  *              Linus Torvalds  :       Better skb_clone.
16  *              Alan Cox        :       Added skb_copy.
17  *              Alan Cox        :       Added all the changed routines Linus
18  *                                      only put in the headers
19  *              Ray VanTassle   :       Fixed --skb->lock in free
20  *              Alan Cox        :       skb_copy copy arp field
21  *              Andi Kleen      :       slabified it.
22  *              Robert Olsson   :       Removed skb_head_pool
23  *
24  *      NOTE:
25  *              The __skb_ routines should be called with interrupts
26  *      disabled, or you better be *real* sure that the operation is atomic
27  *      with respect to whatever list is being frobbed (e.g. via lock_sock()
28  *      or via disabling bottom half handlers, etc).
29  */
30
31 /*
32  *      The functions in this file will not compile correctly with gcc 2.4.x
33  */
34
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/kernel.h>
40 #include <linux/mm.h>
41 #include <linux/interrupt.h>
42 #include <linux/in.h>
43 #include <linux/inet.h>
44 #include <linux/slab.h>
45 #include <linux/tcp.h>
46 #include <linux/udp.h>
47 #include <linux/sctp.h>
48 #include <linux/netdevice.h>
49 #ifdef CONFIG_NET_CLS_ACT
50 #include <net/pkt_sched.h>
51 #endif
52 #include <linux/string.h>
53 #include <linux/skbuff.h>
54 #include <linux/splice.h>
55 #include <linux/cache.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/init.h>
58 #include <linux/scatterlist.h>
59 #include <linux/errqueue.h>
60 #include <linux/prefetch.h>
61 #include <linux/if_vlan.h>
62 #include <linux/mpls.h>
63 #include <linux/kcov.h>
64
65 #include <net/protocol.h>
66 #include <net/dst.h>
67 #include <net/sock.h>
68 #include <net/checksum.h>
69 #include <net/ip6_checksum.h>
70 #include <net/xfrm.h>
71 #include <net/mpls.h>
72 #include <net/mptcp.h>
73 #include <net/page_pool.h>
74
75 #include <linux/uaccess.h>
76 #include <trace/events/skb.h>
77 #include <linux/highmem.h>
78 #include <linux/capability.h>
79 #include <linux/user_namespace.h>
80 #include <linux/indirect_call_wrapper.h>
81
82 #include "datagram.h"
83 #include "sock_destructor.h"
84
85 struct kmem_cache *skbuff_head_cache __ro_after_init;
86 static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
87 #ifdef CONFIG_SKB_EXTENSIONS
88 static struct kmem_cache *skbuff_ext_cache __ro_after_init;
89 #endif
90 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
91 EXPORT_SYMBOL(sysctl_max_skb_frags);
92
93 /**
94  *      skb_panic - private function for out-of-line support
95  *      @skb:   buffer
96  *      @sz:    size
97  *      @addr:  address
98  *      @msg:   skb_over_panic or skb_under_panic
99  *
100  *      Out-of-line support for skb_put() and skb_push().
101  *      Called via the wrapper skb_over_panic() or skb_under_panic().
102  *      Keep out of line to prevent kernel bloat.
103  *      __builtin_return_address is not used because it is not always reliable.
104  */
105 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
106                       const char msg[])
107 {
108         pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
109                  msg, addr, skb->len, sz, skb->head, skb->data,
110                  (unsigned long)skb->tail, (unsigned long)skb->end,
111                  skb->dev ? skb->dev->name : "<NULL>");
112         BUG();
113 }
114
115 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
116 {
117         skb_panic(skb, sz, addr, __func__);
118 }
119
120 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
121 {
122         skb_panic(skb, sz, addr, __func__);
123 }
124
125 #define NAPI_SKB_CACHE_SIZE     64
126 #define NAPI_SKB_CACHE_BULK     16
127 #define NAPI_SKB_CACHE_HALF     (NAPI_SKB_CACHE_SIZE / 2)
128
129 struct napi_alloc_cache {
130         struct page_frag_cache page;
131         unsigned int skb_count;
132         void *skb_cache[NAPI_SKB_CACHE_SIZE];
133 };
134
135 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
136 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
137
138 static void *__alloc_frag_align(unsigned int fragsz, gfp_t gfp_mask,
139                                 unsigned int align_mask)
140 {
141         struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
142
143         return page_frag_alloc_align(&nc->page, fragsz, gfp_mask, align_mask);
144 }
145
146 void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
147 {
148         fragsz = SKB_DATA_ALIGN(fragsz);
149
150         return __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask);
151 }
152 EXPORT_SYMBOL(__napi_alloc_frag_align);
153
154 void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
155 {
156         struct page_frag_cache *nc;
157         void *data;
158
159         fragsz = SKB_DATA_ALIGN(fragsz);
160         if (in_hardirq() || irqs_disabled()) {
161                 nc = this_cpu_ptr(&netdev_alloc_cache);
162                 data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask);
163         } else {
164                 local_bh_disable();
165                 data = __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask);
166                 local_bh_enable();
167         }
168         return data;
169 }
170 EXPORT_SYMBOL(__netdev_alloc_frag_align);
171
172 static struct sk_buff *napi_skb_cache_get(void)
173 {
174         struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
175         struct sk_buff *skb;
176
177         if (unlikely(!nc->skb_count))
178                 nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache,
179                                                       GFP_ATOMIC,
180                                                       NAPI_SKB_CACHE_BULK,
181                                                       nc->skb_cache);
182         if (unlikely(!nc->skb_count))
183                 return NULL;
184
185         skb = nc->skb_cache[--nc->skb_count];
186         kasan_unpoison_object_data(skbuff_head_cache, skb);
187
188         return skb;
189 }
190
191 /* Caller must provide SKB that is memset cleared */
192 static void __build_skb_around(struct sk_buff *skb, void *data,
193                                unsigned int frag_size)
194 {
195         struct skb_shared_info *shinfo;
196         unsigned int size = frag_size ? : ksize(data);
197
198         size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
199
200         /* Assumes caller memset cleared SKB */
201         skb->truesize = SKB_TRUESIZE(size);
202         refcount_set(&skb->users, 1);
203         skb->head = data;
204         skb->data = data;
205         skb_reset_tail_pointer(skb);
206         skb_set_end_offset(skb, size);
207         skb->mac_header = (typeof(skb->mac_header))~0U;
208         skb->transport_header = (typeof(skb->transport_header))~0U;
209
210         /* make sure we initialize shinfo sequentially */
211         shinfo = skb_shinfo(skb);
212         memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
213         atomic_set(&shinfo->dataref, 1);
214
215         skb_set_kcov_handle(skb, kcov_common_handle());
216 }
217
218 /**
219  * __build_skb - build a network buffer
220  * @data: data buffer provided by caller
221  * @frag_size: size of data, or 0 if head was kmalloced
222  *
223  * Allocate a new &sk_buff. Caller provides space holding head and
224  * skb_shared_info. @data must have been allocated by kmalloc() only if
225  * @frag_size is 0, otherwise data should come from the page allocator
226  *  or vmalloc()
227  * The return is the new skb buffer.
228  * On a failure the return is %NULL, and @data is not freed.
229  * Notes :
230  *  Before IO, driver allocates only data buffer where NIC put incoming frame
231  *  Driver should add room at head (NET_SKB_PAD) and
232  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
233  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
234  *  before giving packet to stack.
235  *  RX rings only contains data buffers, not full skbs.
236  */
237 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
238 {
239         struct sk_buff *skb;
240
241         skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
242         if (unlikely(!skb))
243                 return NULL;
244
245         memset(skb, 0, offsetof(struct sk_buff, tail));
246         __build_skb_around(skb, data, frag_size);
247
248         return skb;
249 }
250
251 /* build_skb() is wrapper over __build_skb(), that specifically
252  * takes care of skb->head and skb->pfmemalloc
253  * This means that if @frag_size is not zero, then @data must be backed
254  * by a page fragment, not kmalloc() or vmalloc()
255  */
256 struct sk_buff *build_skb(void *data, unsigned int frag_size)
257 {
258         struct sk_buff *skb = __build_skb(data, frag_size);
259
260         if (skb && frag_size) {
261                 skb->head_frag = 1;
262                 if (page_is_pfmemalloc(virt_to_head_page(data)))
263                         skb->pfmemalloc = 1;
264         }
265         return skb;
266 }
267 EXPORT_SYMBOL(build_skb);
268
269 /**
270  * build_skb_around - build a network buffer around provided skb
271  * @skb: sk_buff provide by caller, must be memset cleared
272  * @data: data buffer provided by caller
273  * @frag_size: size of data, or 0 if head was kmalloced
274  */
275 struct sk_buff *build_skb_around(struct sk_buff *skb,
276                                  void *data, unsigned int frag_size)
277 {
278         if (unlikely(!skb))
279                 return NULL;
280
281         __build_skb_around(skb, data, frag_size);
282
283         if (frag_size) {
284                 skb->head_frag = 1;
285                 if (page_is_pfmemalloc(virt_to_head_page(data)))
286                         skb->pfmemalloc = 1;
287         }
288         return skb;
289 }
290 EXPORT_SYMBOL(build_skb_around);
291
292 /**
293  * __napi_build_skb - build a network buffer
294  * @data: data buffer provided by caller
295  * @frag_size: size of data, or 0 if head was kmalloced
296  *
297  * Version of __build_skb() that uses NAPI percpu caches to obtain
298  * skbuff_head instead of inplace allocation.
299  *
300  * Returns a new &sk_buff on success, %NULL on allocation failure.
301  */
302 static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size)
303 {
304         struct sk_buff *skb;
305
306         skb = napi_skb_cache_get();
307         if (unlikely(!skb))
308                 return NULL;
309
310         memset(skb, 0, offsetof(struct sk_buff, tail));
311         __build_skb_around(skb, data, frag_size);
312
313         return skb;
314 }
315
316 /**
317  * napi_build_skb - build a network buffer
318  * @data: data buffer provided by caller
319  * @frag_size: size of data, or 0 if head was kmalloced
320  *
321  * Version of __napi_build_skb() that takes care of skb->head_frag
322  * and skb->pfmemalloc when the data is a page or page fragment.
323  *
324  * Returns a new &sk_buff on success, %NULL on allocation failure.
325  */
326 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
327 {
328         struct sk_buff *skb = __napi_build_skb(data, frag_size);
329
330         if (likely(skb) && frag_size) {
331                 skb->head_frag = 1;
332                 skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
333         }
334
335         return skb;
336 }
337 EXPORT_SYMBOL(napi_build_skb);
338
339 /*
340  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
341  * the caller if emergency pfmemalloc reserves are being used. If it is and
342  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
343  * may be used. Otherwise, the packet data may be discarded until enough
344  * memory is free
345  */
346 static void *kmalloc_reserve(size_t size, gfp_t flags, int node,
347                              bool *pfmemalloc)
348 {
349         void *obj;
350         bool ret_pfmemalloc = false;
351
352         /*
353          * Try a regular allocation, when that fails and we're not entitled
354          * to the reserves, fail.
355          */
356         obj = kmalloc_node_track_caller(size,
357                                         flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
358                                         node);
359         if (obj || !(gfp_pfmemalloc_allowed(flags)))
360                 goto out;
361
362         /* Try again but now we are using pfmemalloc reserves */
363         ret_pfmemalloc = true;
364         obj = kmalloc_node_track_caller(size, flags, node);
365
366 out:
367         if (pfmemalloc)
368                 *pfmemalloc = ret_pfmemalloc;
369
370         return obj;
371 }
372
373 /*      Allocate a new skbuff. We do this ourselves so we can fill in a few
374  *      'private' fields and also do memory statistics to find all the
375  *      [BEEP] leaks.
376  *
377  */
378
379 /**
380  *      __alloc_skb     -       allocate a network buffer
381  *      @size: size to allocate
382  *      @gfp_mask: allocation mask
383  *      @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
384  *              instead of head cache and allocate a cloned (child) skb.
385  *              If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
386  *              allocations in case the data is required for writeback
387  *      @node: numa node to allocate memory on
388  *
389  *      Allocate a new &sk_buff. The returned buffer has no headroom and a
390  *      tail room of at least size bytes. The object has a reference count
391  *      of one. The return is the buffer. On a failure the return is %NULL.
392  *
393  *      Buffers may only be allocated from interrupts using a @gfp_mask of
394  *      %GFP_ATOMIC.
395  */
396 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
397                             int flags, int node)
398 {
399         struct kmem_cache *cache;
400         struct sk_buff *skb;
401         u8 *data;
402         bool pfmemalloc;
403
404         cache = (flags & SKB_ALLOC_FCLONE)
405                 ? skbuff_fclone_cache : skbuff_head_cache;
406
407         if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
408                 gfp_mask |= __GFP_MEMALLOC;
409
410         /* Get the HEAD */
411         if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI &&
412             likely(node == NUMA_NO_NODE || node == numa_mem_id()))
413                 skb = napi_skb_cache_get();
414         else
415                 skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node);
416         if (unlikely(!skb))
417                 return NULL;
418         prefetchw(skb);
419
420         /* We do our best to align skb_shared_info on a separate cache
421          * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
422          * aligned memory blocks, unless SLUB/SLAB debug is enabled.
423          * Both skb->head and skb_shared_info are cache line aligned.
424          */
425         size = SKB_DATA_ALIGN(size);
426         size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
427         data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
428         if (unlikely(!data))
429                 goto nodata;
430         /* kmalloc(size) might give us more room than requested.
431          * Put skb_shared_info exactly at the end of allocated zone,
432          * to allow max possible filling before reallocation.
433          */
434         size = SKB_WITH_OVERHEAD(ksize(data));
435         prefetchw(data + size);
436
437         /*
438          * Only clear those fields we need to clear, not those that we will
439          * actually initialise below. Hence, don't put any more fields after
440          * the tail pointer in struct sk_buff!
441          */
442         memset(skb, 0, offsetof(struct sk_buff, tail));
443         __build_skb_around(skb, data, 0);
444         skb->pfmemalloc = pfmemalloc;
445
446         if (flags & SKB_ALLOC_FCLONE) {
447                 struct sk_buff_fclones *fclones;
448
449                 fclones = container_of(skb, struct sk_buff_fclones, skb1);
450
451                 skb->fclone = SKB_FCLONE_ORIG;
452                 refcount_set(&fclones->fclone_ref, 1);
453
454                 fclones->skb2.fclone = SKB_FCLONE_CLONE;
455         }
456
457         return skb;
458
459 nodata:
460         kmem_cache_free(cache, skb);
461         return NULL;
462 }
463 EXPORT_SYMBOL(__alloc_skb);
464
465 /**
466  *      __netdev_alloc_skb - allocate an skbuff for rx on a specific device
467  *      @dev: network device to receive on
468  *      @len: length to allocate
469  *      @gfp_mask: get_free_pages mask, passed to alloc_skb
470  *
471  *      Allocate a new &sk_buff and assign it a usage count of one. The
472  *      buffer has NET_SKB_PAD headroom built in. Users should allocate
473  *      the headroom they think they need without accounting for the
474  *      built in space. The built in space is used for optimisations.
475  *
476  *      %NULL is returned if there is no free memory.
477  */
478 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
479                                    gfp_t gfp_mask)
480 {
481         struct page_frag_cache *nc;
482         struct sk_buff *skb;
483         bool pfmemalloc;
484         void *data;
485
486         len += NET_SKB_PAD;
487
488         /* If requested length is either too small or too big,
489          * we use kmalloc() for skb->head allocation.
490          */
491         if (len <= SKB_WITH_OVERHEAD(1024) ||
492             len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
493             (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
494                 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
495                 if (!skb)
496                         goto skb_fail;
497                 goto skb_success;
498         }
499
500         len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
501         len = SKB_DATA_ALIGN(len);
502
503         if (sk_memalloc_socks())
504                 gfp_mask |= __GFP_MEMALLOC;
505
506         if (in_hardirq() || irqs_disabled()) {
507                 nc = this_cpu_ptr(&netdev_alloc_cache);
508                 data = page_frag_alloc(nc, len, gfp_mask);
509                 pfmemalloc = nc->pfmemalloc;
510         } else {
511                 local_bh_disable();
512                 nc = this_cpu_ptr(&napi_alloc_cache.page);
513                 data = page_frag_alloc(nc, len, gfp_mask);
514                 pfmemalloc = nc->pfmemalloc;
515                 local_bh_enable();
516         }
517
518         if (unlikely(!data))
519                 return NULL;
520
521         skb = __build_skb(data, len);
522         if (unlikely(!skb)) {
523                 skb_free_frag(data);
524                 return NULL;
525         }
526
527         if (pfmemalloc)
528                 skb->pfmemalloc = 1;
529         skb->head_frag = 1;
530
531 skb_success:
532         skb_reserve(skb, NET_SKB_PAD);
533         skb->dev = dev;
534
535 skb_fail:
536         return skb;
537 }
538 EXPORT_SYMBOL(__netdev_alloc_skb);
539
540 /**
541  *      __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
542  *      @napi: napi instance this buffer was allocated for
543  *      @len: length to allocate
544  *      @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
545  *
546  *      Allocate a new sk_buff for use in NAPI receive.  This buffer will
547  *      attempt to allocate the head from a special reserved region used
548  *      only for NAPI Rx allocation.  By doing this we can save several
549  *      CPU cycles by avoiding having to disable and re-enable IRQs.
550  *
551  *      %NULL is returned if there is no free memory.
552  */
553 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
554                                  gfp_t gfp_mask)
555 {
556         struct napi_alloc_cache *nc;
557         struct sk_buff *skb;
558         void *data;
559
560         len += NET_SKB_PAD + NET_IP_ALIGN;
561
562         /* If requested length is either too small or too big,
563          * we use kmalloc() for skb->head allocation.
564          */
565         if (len <= SKB_WITH_OVERHEAD(1024) ||
566             len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
567             (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
568                 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI,
569                                   NUMA_NO_NODE);
570                 if (!skb)
571                         goto skb_fail;
572                 goto skb_success;
573         }
574
575         nc = this_cpu_ptr(&napi_alloc_cache);
576         len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
577         len = SKB_DATA_ALIGN(len);
578
579         if (sk_memalloc_socks())
580                 gfp_mask |= __GFP_MEMALLOC;
581
582         data = page_frag_alloc(&nc->page, len, gfp_mask);
583         if (unlikely(!data))
584                 return NULL;
585
586         skb = __napi_build_skb(data, len);
587         if (unlikely(!skb)) {
588                 skb_free_frag(data);
589                 return NULL;
590         }
591
592         if (nc->page.pfmemalloc)
593                 skb->pfmemalloc = 1;
594         skb->head_frag = 1;
595
596 skb_success:
597         skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
598         skb->dev = napi->dev;
599
600 skb_fail:
601         return skb;
602 }
603 EXPORT_SYMBOL(__napi_alloc_skb);
604
605 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
606                      int size, unsigned int truesize)
607 {
608         skb_fill_page_desc(skb, i, page, off, size);
609         skb->len += size;
610         skb->data_len += size;
611         skb->truesize += truesize;
612 }
613 EXPORT_SYMBOL(skb_add_rx_frag);
614
615 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
616                           unsigned int truesize)
617 {
618         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
619
620         skb_frag_size_add(frag, size);
621         skb->len += size;
622         skb->data_len += size;
623         skb->truesize += truesize;
624 }
625 EXPORT_SYMBOL(skb_coalesce_rx_frag);
626
627 static void skb_drop_list(struct sk_buff **listp)
628 {
629         kfree_skb_list(*listp);
630         *listp = NULL;
631 }
632
633 static inline void skb_drop_fraglist(struct sk_buff *skb)
634 {
635         skb_drop_list(&skb_shinfo(skb)->frag_list);
636 }
637
638 static void skb_clone_fraglist(struct sk_buff *skb)
639 {
640         struct sk_buff *list;
641
642         skb_walk_frags(skb, list)
643                 skb_get(list);
644 }
645
646 static void skb_free_head(struct sk_buff *skb)
647 {
648         unsigned char *head = skb->head;
649
650         if (skb->head_frag) {
651                 if (skb_pp_recycle(skb, head))
652                         return;
653                 skb_free_frag(head);
654         } else {
655                 kfree(head);
656         }
657 }
658
659 static void skb_release_data(struct sk_buff *skb)
660 {
661         struct skb_shared_info *shinfo = skb_shinfo(skb);
662         int i;
663
664         if (skb->cloned &&
665             atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
666                               &shinfo->dataref))
667                 goto exit;
668
669         skb_zcopy_clear(skb, true);
670
671         for (i = 0; i < shinfo->nr_frags; i++)
672                 __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle);
673
674         if (shinfo->frag_list)
675                 kfree_skb_list(shinfo->frag_list);
676
677         skb_free_head(skb);
678 exit:
679         /* When we clone an SKB we copy the reycling bit. The pp_recycle
680          * bit is only set on the head though, so in order to avoid races
681          * while trying to recycle fragments on __skb_frag_unref() we need
682          * to make one SKB responsible for triggering the recycle path.
683          * So disable the recycling bit if an SKB is cloned and we have
684          * additional references to to the fragmented part of the SKB.
685          * Eventually the last SKB will have the recycling bit set and it's
686          * dataref set to 0, which will trigger the recycling
687          */
688         skb->pp_recycle = 0;
689 }
690
691 /*
692  *      Free an skbuff by memory without cleaning the state.
693  */
694 static void kfree_skbmem(struct sk_buff *skb)
695 {
696         struct sk_buff_fclones *fclones;
697
698         switch (skb->fclone) {
699         case SKB_FCLONE_UNAVAILABLE:
700                 kmem_cache_free(skbuff_head_cache, skb);
701                 return;
702
703         case SKB_FCLONE_ORIG:
704                 fclones = container_of(skb, struct sk_buff_fclones, skb1);
705
706                 /* We usually free the clone (TX completion) before original skb
707                  * This test would have no chance to be true for the clone,
708                  * while here, branch prediction will be good.
709                  */
710                 if (refcount_read(&fclones->fclone_ref) == 1)
711                         goto fastpath;
712                 break;
713
714         default: /* SKB_FCLONE_CLONE */
715                 fclones = container_of(skb, struct sk_buff_fclones, skb2);
716                 break;
717         }
718         if (!refcount_dec_and_test(&fclones->fclone_ref))
719                 return;
720 fastpath:
721         kmem_cache_free(skbuff_fclone_cache, fclones);
722 }
723
724 void skb_release_head_state(struct sk_buff *skb)
725 {
726         skb_dst_drop(skb);
727         if (skb->destructor) {
728                 WARN_ON(in_hardirq());
729                 skb->destructor(skb);
730         }
731 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
732         nf_conntrack_put(skb_nfct(skb));
733 #endif
734         skb_ext_put(skb);
735 }
736
737 /* Free everything but the sk_buff shell. */
738 static void skb_release_all(struct sk_buff *skb)
739 {
740         skb_release_head_state(skb);
741         if (likely(skb->head))
742                 skb_release_data(skb);
743 }
744
745 /**
746  *      __kfree_skb - private function
747  *      @skb: buffer
748  *
749  *      Free an sk_buff. Release anything attached to the buffer.
750  *      Clean the state. This is an internal helper function. Users should
751  *      always call kfree_skb
752  */
753
754 void __kfree_skb(struct sk_buff *skb)
755 {
756         skb_release_all(skb);
757         kfree_skbmem(skb);
758 }
759 EXPORT_SYMBOL(__kfree_skb);
760
761 /**
762  *      kfree_skb - free an sk_buff
763  *      @skb: buffer to free
764  *
765  *      Drop a reference to the buffer and free it if the usage count has
766  *      hit zero.
767  */
768 void kfree_skb(struct sk_buff *skb)
769 {
770         if (!skb_unref(skb))
771                 return;
772
773         trace_kfree_skb(skb, __builtin_return_address(0));
774         __kfree_skb(skb);
775 }
776 EXPORT_SYMBOL(kfree_skb);
777
778 void kfree_skb_list(struct sk_buff *segs)
779 {
780         while (segs) {
781                 struct sk_buff *next = segs->next;
782
783                 kfree_skb(segs);
784                 segs = next;
785         }
786 }
787 EXPORT_SYMBOL(kfree_skb_list);
788
789 /* Dump skb information and contents.
790  *
791  * Must only be called from net_ratelimit()-ed paths.
792  *
793  * Dumps whole packets if full_pkt, only headers otherwise.
794  */
795 void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
796 {
797         struct skb_shared_info *sh = skb_shinfo(skb);
798         struct net_device *dev = skb->dev;
799         struct sock *sk = skb->sk;
800         struct sk_buff *list_skb;
801         bool has_mac, has_trans;
802         int headroom, tailroom;
803         int i, len, seg_len;
804
805         if (full_pkt)
806                 len = skb->len;
807         else
808                 len = min_t(int, skb->len, MAX_HEADER + 128);
809
810         headroom = skb_headroom(skb);
811         tailroom = skb_tailroom(skb);
812
813         has_mac = skb_mac_header_was_set(skb);
814         has_trans = skb_transport_header_was_set(skb);
815
816         printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
817                "mac=(%d,%d) net=(%d,%d) trans=%d\n"
818                "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
819                "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
820                "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
821                level, skb->len, headroom, skb_headlen(skb), tailroom,
822                has_mac ? skb->mac_header : -1,
823                has_mac ? skb_mac_header_len(skb) : -1,
824                skb->network_header,
825                has_trans ? skb_network_header_len(skb) : -1,
826                has_trans ? skb->transport_header : -1,
827                sh->tx_flags, sh->nr_frags,
828                sh->gso_size, sh->gso_type, sh->gso_segs,
829                skb->csum, skb->ip_summed, skb->csum_complete_sw,
830                skb->csum_valid, skb->csum_level,
831                skb->hash, skb->sw_hash, skb->l4_hash,
832                ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
833
834         if (dev)
835                 printk("%sdev name=%s feat=%pNF\n",
836                        level, dev->name, &dev->features);
837         if (sk)
838                 printk("%ssk family=%hu type=%u proto=%u\n",
839                        level, sk->sk_family, sk->sk_type, sk->sk_protocol);
840
841         if (full_pkt && headroom)
842                 print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
843                                16, 1, skb->head, headroom, false);
844
845         seg_len = min_t(int, skb_headlen(skb), len);
846         if (seg_len)
847                 print_hex_dump(level, "skb linear:   ", DUMP_PREFIX_OFFSET,
848                                16, 1, skb->data, seg_len, false);
849         len -= seg_len;
850
851         if (full_pkt && tailroom)
852                 print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
853                                16, 1, skb_tail_pointer(skb), tailroom, false);
854
855         for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
856                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
857                 u32 p_off, p_len, copied;
858                 struct page *p;
859                 u8 *vaddr;
860
861                 skb_frag_foreach_page(frag, skb_frag_off(frag),
862                                       skb_frag_size(frag), p, p_off, p_len,
863                                       copied) {
864                         seg_len = min_t(int, p_len, len);
865                         vaddr = kmap_atomic(p);
866                         print_hex_dump(level, "skb frag:     ",
867                                        DUMP_PREFIX_OFFSET,
868                                        16, 1, vaddr + p_off, seg_len, false);
869                         kunmap_atomic(vaddr);
870                         len -= seg_len;
871                         if (!len)
872                                 break;
873                 }
874         }
875
876         if (full_pkt && skb_has_frag_list(skb)) {
877                 printk("skb fraglist:\n");
878                 skb_walk_frags(skb, list_skb)
879                         skb_dump(level, list_skb, true);
880         }
881 }
882 EXPORT_SYMBOL(skb_dump);
883
884 /**
885  *      skb_tx_error - report an sk_buff xmit error
886  *      @skb: buffer that triggered an error
887  *
888  *      Report xmit error if a device callback is tracking this skb.
889  *      skb must be freed afterwards.
890  */
891 void skb_tx_error(struct sk_buff *skb)
892 {
893         skb_zcopy_clear(skb, true);
894 }
895 EXPORT_SYMBOL(skb_tx_error);
896
897 #ifdef CONFIG_TRACEPOINTS
898 /**
899  *      consume_skb - free an skbuff
900  *      @skb: buffer to free
901  *
902  *      Drop a ref to the buffer and free it if the usage count has hit zero
903  *      Functions identically to kfree_skb, but kfree_skb assumes that the frame
904  *      is being dropped after a failure and notes that
905  */
906 void consume_skb(struct sk_buff *skb)
907 {
908         if (!skb_unref(skb))
909                 return;
910
911         trace_consume_skb(skb);
912         __kfree_skb(skb);
913 }
914 EXPORT_SYMBOL(consume_skb);
915 #endif
916
917 /**
918  *      __consume_stateless_skb - free an skbuff, assuming it is stateless
919  *      @skb: buffer to free
920  *
921  *      Alike consume_skb(), but this variant assumes that this is the last
922  *      skb reference and all the head states have been already dropped
923  */
924 void __consume_stateless_skb(struct sk_buff *skb)
925 {
926         trace_consume_skb(skb);
927         skb_release_data(skb);
928         kfree_skbmem(skb);
929 }
930
931 static void napi_skb_cache_put(struct sk_buff *skb)
932 {
933         struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
934         u32 i;
935
936         kasan_poison_object_data(skbuff_head_cache, skb);
937         nc->skb_cache[nc->skb_count++] = skb;
938
939         if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
940                 for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++)
941                         kasan_unpoison_object_data(skbuff_head_cache,
942                                                    nc->skb_cache[i]);
943
944                 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF,
945                                      nc->skb_cache + NAPI_SKB_CACHE_HALF);
946                 nc->skb_count = NAPI_SKB_CACHE_HALF;
947         }
948 }
949
950 void __kfree_skb_defer(struct sk_buff *skb)
951 {
952         skb_release_all(skb);
953         napi_skb_cache_put(skb);
954 }
955
956 void napi_skb_free_stolen_head(struct sk_buff *skb)
957 {
958         if (unlikely(skb->slow_gro)) {
959                 nf_reset_ct(skb);
960                 skb_dst_drop(skb);
961                 skb_ext_put(skb);
962                 skb_orphan(skb);
963                 skb->slow_gro = 0;
964         }
965         napi_skb_cache_put(skb);
966 }
967
968 void napi_consume_skb(struct sk_buff *skb, int budget)
969 {
970         /* Zero budget indicate non-NAPI context called us, like netpoll */
971         if (unlikely(!budget)) {
972                 dev_consume_skb_any(skb);
973                 return;
974         }
975
976         lockdep_assert_in_softirq();
977
978         if (!skb_unref(skb))
979                 return;
980
981         /* if reaching here SKB is ready to free */
982         trace_consume_skb(skb);
983
984         /* if SKB is a clone, don't handle this case */
985         if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
986                 __kfree_skb(skb);
987                 return;
988         }
989
990         skb_release_all(skb);
991         napi_skb_cache_put(skb);
992 }
993 EXPORT_SYMBOL(napi_consume_skb);
994
995 /* Make sure a field is enclosed inside headers_start/headers_end section */
996 #define CHECK_SKB_FIELD(field) \
997         BUILD_BUG_ON(offsetof(struct sk_buff, field) <          \
998                      offsetof(struct sk_buff, headers_start));  \
999         BUILD_BUG_ON(offsetof(struct sk_buff, field) >          \
1000                      offsetof(struct sk_buff, headers_end));    \
1001
1002 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1003 {
1004         new->tstamp             = old->tstamp;
1005         /* We do not copy old->sk */
1006         new->dev                = old->dev;
1007         memcpy(new->cb, old->cb, sizeof(old->cb));
1008         skb_dst_copy(new, old);
1009         __skb_ext_copy(new, old);
1010         __nf_copy(new, old, false);
1011
1012         /* Note : this field could be in headers_start/headers_end section
1013          * It is not yet because we do not want to have a 16 bit hole
1014          */
1015         new->queue_mapping = old->queue_mapping;
1016
1017         memcpy(&new->headers_start, &old->headers_start,
1018                offsetof(struct sk_buff, headers_end) -
1019                offsetof(struct sk_buff, headers_start));
1020         CHECK_SKB_FIELD(protocol);
1021         CHECK_SKB_FIELD(csum);
1022         CHECK_SKB_FIELD(hash);
1023         CHECK_SKB_FIELD(priority);
1024         CHECK_SKB_FIELD(skb_iif);
1025         CHECK_SKB_FIELD(vlan_proto);
1026         CHECK_SKB_FIELD(vlan_tci);
1027         CHECK_SKB_FIELD(transport_header);
1028         CHECK_SKB_FIELD(network_header);
1029         CHECK_SKB_FIELD(mac_header);
1030         CHECK_SKB_FIELD(inner_protocol);
1031         CHECK_SKB_FIELD(inner_transport_header);
1032         CHECK_SKB_FIELD(inner_network_header);
1033         CHECK_SKB_FIELD(inner_mac_header);
1034         CHECK_SKB_FIELD(mark);
1035 #ifdef CONFIG_NETWORK_SECMARK
1036         CHECK_SKB_FIELD(secmark);
1037 #endif
1038 #ifdef CONFIG_NET_RX_BUSY_POLL
1039         CHECK_SKB_FIELD(napi_id);
1040 #endif
1041 #ifdef CONFIG_XPS
1042         CHECK_SKB_FIELD(sender_cpu);
1043 #endif
1044 #ifdef CONFIG_NET_SCHED
1045         CHECK_SKB_FIELD(tc_index);
1046 #endif
1047
1048 }
1049
1050 /*
1051  * You should not add any new code to this function.  Add it to
1052  * __copy_skb_header above instead.
1053  */
1054 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
1055 {
1056 #define C(x) n->x = skb->x
1057
1058         n->next = n->prev = NULL;
1059         n->sk = NULL;
1060         __copy_skb_header(n, skb);
1061
1062         C(len);
1063         C(data_len);
1064         C(mac_len);
1065         n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
1066         n->cloned = 1;
1067         n->nohdr = 0;
1068         n->peeked = 0;
1069         C(pfmemalloc);
1070         C(pp_recycle);
1071         n->destructor = NULL;
1072         C(tail);
1073         C(end);
1074         C(head);
1075         C(head_frag);
1076         C(data);
1077         C(truesize);
1078         refcount_set(&n->users, 1);
1079
1080         atomic_inc(&(skb_shinfo(skb)->dataref));
1081         skb->cloned = 1;
1082
1083         return n;
1084 #undef C
1085 }
1086
1087 /**
1088  * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1089  * @first: first sk_buff of the msg
1090  */
1091 struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1092 {
1093         struct sk_buff *n;
1094
1095         n = alloc_skb(0, GFP_ATOMIC);
1096         if (!n)
1097                 return NULL;
1098
1099         n->len = first->len;
1100         n->data_len = first->len;
1101         n->truesize = first->truesize;
1102
1103         skb_shinfo(n)->frag_list = first;
1104
1105         __copy_skb_header(n, first);
1106         n->destructor = NULL;
1107
1108         return n;
1109 }
1110 EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1111
1112 /**
1113  *      skb_morph       -       morph one skb into another
1114  *      @dst: the skb to receive the contents
1115  *      @src: the skb to supply the contents
1116  *
1117  *      This is identical to skb_clone except that the target skb is
1118  *      supplied by the user.
1119  *
1120  *      The target skb is returned upon exit.
1121  */
1122 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1123 {
1124         skb_release_all(dst);
1125         return __skb_clone(dst, src);
1126 }
1127 EXPORT_SYMBOL_GPL(skb_morph);
1128
1129 int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
1130 {
1131         unsigned long max_pg, num_pg, new_pg, old_pg;
1132         struct user_struct *user;
1133
1134         if (capable(CAP_IPC_LOCK) || !size)
1135                 return 0;
1136
1137         num_pg = (size >> PAGE_SHIFT) + 2;      /* worst case */
1138         max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1139         user = mmp->user ? : current_user();
1140
1141         do {
1142                 old_pg = atomic_long_read(&user->locked_vm);
1143                 new_pg = old_pg + num_pg;
1144                 if (new_pg > max_pg)
1145                         return -ENOBUFS;
1146         } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
1147                  old_pg);
1148
1149         if (!mmp->user) {
1150                 mmp->user = get_uid(user);
1151                 mmp->num_pg = num_pg;
1152         } else {
1153                 mmp->num_pg += num_pg;
1154         }
1155
1156         return 0;
1157 }
1158 EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
1159
1160 void mm_unaccount_pinned_pages(struct mmpin *mmp)
1161 {
1162         if (mmp->user) {
1163                 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1164                 free_uid(mmp->user);
1165         }
1166 }
1167 EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
1168
1169 struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)
1170 {
1171         struct ubuf_info *uarg;
1172         struct sk_buff *skb;
1173
1174         WARN_ON_ONCE(!in_task());
1175
1176         skb = sock_omalloc(sk, 0, GFP_KERNEL);
1177         if (!skb)
1178                 return NULL;
1179
1180         BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
1181         uarg = (void *)skb->cb;
1182         uarg->mmp.user = NULL;
1183
1184         if (mm_account_pinned_pages(&uarg->mmp, size)) {
1185                 kfree_skb(skb);
1186                 return NULL;
1187         }
1188
1189         uarg->callback = msg_zerocopy_callback;
1190         uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
1191         uarg->len = 1;
1192         uarg->bytelen = size;
1193         uarg->zerocopy = 1;
1194         uarg->flags = SKBFL_ZEROCOPY_FRAG;
1195         refcount_set(&uarg->refcnt, 1);
1196         sock_hold(sk);
1197
1198         return uarg;
1199 }
1200 EXPORT_SYMBOL_GPL(msg_zerocopy_alloc);
1201
1202 static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
1203 {
1204         return container_of((void *)uarg, struct sk_buff, cb);
1205 }
1206
1207 struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
1208                                        struct ubuf_info *uarg)
1209 {
1210         if (uarg) {
1211                 const u32 byte_limit = 1 << 19;         /* limit to a few TSO */
1212                 u32 bytelen, next;
1213
1214                 /* realloc only when socket is locked (TCP, UDP cork),
1215                  * so uarg->len and sk_zckey access is serialized
1216                  */
1217                 if (!sock_owned_by_user(sk)) {
1218                         WARN_ON_ONCE(1);
1219                         return NULL;
1220                 }
1221
1222                 bytelen = uarg->bytelen + size;
1223                 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1224                         /* TCP can create new skb to attach new uarg */
1225                         if (sk->sk_type == SOCK_STREAM)
1226                                 goto new_alloc;
1227                         return NULL;
1228                 }
1229
1230                 next = (u32)atomic_read(&sk->sk_zckey);
1231                 if ((u32)(uarg->id + uarg->len) == next) {
1232                         if (mm_account_pinned_pages(&uarg->mmp, size))
1233                                 return NULL;
1234                         uarg->len++;
1235                         uarg->bytelen = bytelen;
1236                         atomic_set(&sk->sk_zckey, ++next);
1237
1238                         /* no extra ref when appending to datagram (MSG_MORE) */
1239                         if (sk->sk_type == SOCK_STREAM)
1240                                 net_zcopy_get(uarg);
1241
1242                         return uarg;
1243                 }
1244         }
1245
1246 new_alloc:
1247         return msg_zerocopy_alloc(sk, size);
1248 }
1249 EXPORT_SYMBOL_GPL(msg_zerocopy_realloc);
1250
1251 static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1252 {
1253         struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1254         u32 old_lo, old_hi;
1255         u64 sum_len;
1256
1257         old_lo = serr->ee.ee_info;
1258         old_hi = serr->ee.ee_data;
1259         sum_len = old_hi - old_lo + 1ULL + len;
1260
1261         if (sum_len >= (1ULL << 32))
1262                 return false;
1263
1264         if (lo != old_hi + 1)
1265                 return false;
1266
1267         serr->ee.ee_data += len;
1268         return true;
1269 }
1270
1271 static void __msg_zerocopy_callback(struct ubuf_info *uarg)
1272 {
1273         struct sk_buff *tail, *skb = skb_from_uarg(uarg);
1274         struct sock_exterr_skb *serr;
1275         struct sock *sk = skb->sk;
1276         struct sk_buff_head *q;
1277         unsigned long flags;
1278         bool is_zerocopy;
1279         u32 lo, hi;
1280         u16 len;
1281
1282         mm_unaccount_pinned_pages(&uarg->mmp);
1283
1284         /* if !len, there was only 1 call, and it was aborted
1285          * so do not queue a completion notification
1286          */
1287         if (!uarg->len || sock_flag(sk, SOCK_DEAD))
1288                 goto release;
1289
1290         len = uarg->len;
1291         lo = uarg->id;
1292         hi = uarg->id + len - 1;
1293         is_zerocopy = uarg->zerocopy;
1294
1295         serr = SKB_EXT_ERR(skb);
1296         memset(serr, 0, sizeof(*serr));
1297         serr->ee.ee_errno = 0;
1298         serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
1299         serr->ee.ee_data = hi;
1300         serr->ee.ee_info = lo;
1301         if (!is_zerocopy)
1302                 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1303
1304         q = &sk->sk_error_queue;
1305         spin_lock_irqsave(&q->lock, flags);
1306         tail = skb_peek_tail(q);
1307         if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1308             !skb_zerocopy_notify_extend(tail, lo, len)) {
1309                 __skb_queue_tail(q, skb);
1310                 skb = NULL;
1311         }
1312         spin_unlock_irqrestore(&q->lock, flags);
1313
1314         sk_error_report(sk);
1315
1316 release:
1317         consume_skb(skb);
1318         sock_put(sk);
1319 }
1320
1321 void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg,
1322                            bool success)
1323 {
1324         uarg->zerocopy = uarg->zerocopy & success;
1325
1326         if (refcount_dec_and_test(&uarg->refcnt))
1327                 __msg_zerocopy_callback(uarg);
1328 }
1329 EXPORT_SYMBOL_GPL(msg_zerocopy_callback);
1330
1331 void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1332 {
1333         struct sock *sk = skb_from_uarg(uarg)->sk;
1334
1335         atomic_dec(&sk->sk_zckey);
1336         uarg->len--;
1337
1338         if (have_uref)
1339                 msg_zerocopy_callback(NULL, uarg, true);
1340 }
1341 EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort);
1342
1343 int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1344 {
1345         return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1346 }
1347 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1348
1349 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1350                              struct msghdr *msg, int len,
1351                              struct ubuf_info *uarg)
1352 {
1353         struct ubuf_info *orig_uarg = skb_zcopy(skb);
1354         struct iov_iter orig_iter = msg->msg_iter;
1355         int err, orig_len = skb->len;
1356
1357         /* An skb can only point to one uarg. This edge case happens when
1358          * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1359          */
1360         if (orig_uarg && uarg != orig_uarg)
1361                 return -EEXIST;
1362
1363         err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1364         if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1365                 struct sock *save_sk = skb->sk;
1366
1367                 /* Streams do not free skb on error. Reset to prev state. */
1368                 msg->msg_iter = orig_iter;
1369                 skb->sk = sk;
1370                 ___pskb_trim(skb, orig_len);
1371                 skb->sk = save_sk;
1372                 return err;
1373         }
1374
1375         skb_zcopy_set(skb, uarg, NULL);
1376         return skb->len - orig_len;
1377 }
1378 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1379
1380 static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
1381                               gfp_t gfp_mask)
1382 {
1383         if (skb_zcopy(orig)) {
1384                 if (skb_zcopy(nskb)) {
1385                         /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1386                         if (!gfp_mask) {
1387                                 WARN_ON_ONCE(1);
1388                                 return -ENOMEM;
1389                         }
1390                         if (skb_uarg(nskb) == skb_uarg(orig))
1391                                 return 0;
1392                         if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1393                                 return -EIO;
1394                 }
1395                 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
1396         }
1397         return 0;
1398 }
1399
1400 /**
1401  *      skb_copy_ubufs  -       copy userspace skb frags buffers to kernel
1402  *      @skb: the skb to modify
1403  *      @gfp_mask: allocation priority
1404  *
1405  *      This must be called on skb with SKBFL_ZEROCOPY_ENABLE.
1406  *      It will copy all frags into kernel and drop the reference
1407  *      to userspace pages.
1408  *
1409  *      If this function is called from an interrupt gfp_mask() must be
1410  *      %GFP_ATOMIC.
1411  *
1412  *      Returns 0 on success or a negative error code on failure
1413  *      to allocate kernel memory to copy to.
1414  */
1415 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1416 {
1417         int num_frags = skb_shinfo(skb)->nr_frags;
1418         struct page *page, *head = NULL;
1419         int i, new_frags;
1420         u32 d_off;
1421
1422         if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1423                 return -EINVAL;
1424
1425         if (!num_frags)
1426                 goto release;
1427
1428         new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1429         for (i = 0; i < new_frags; i++) {
1430                 page = alloc_page(gfp_mask);
1431                 if (!page) {
1432                         while (head) {
1433                                 struct page *next = (struct page *)page_private(head);
1434                                 put_page(head);
1435                                 head = next;
1436                         }
1437                         return -ENOMEM;
1438                 }
1439                 set_page_private(page, (unsigned long)head);
1440                 head = page;
1441         }
1442
1443         page = head;
1444         d_off = 0;
1445         for (i = 0; i < num_frags; i++) {
1446                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1447                 u32 p_off, p_len, copied;
1448                 struct page *p;
1449                 u8 *vaddr;
1450
1451                 skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
1452                                       p, p_off, p_len, copied) {
1453                         u32 copy, done = 0;
1454                         vaddr = kmap_atomic(p);
1455
1456                         while (done < p_len) {
1457                                 if (d_off == PAGE_SIZE) {
1458                                         d_off = 0;
1459                                         page = (struct page *)page_private(page);
1460                                 }
1461                                 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1462                                 memcpy(page_address(page) + d_off,
1463                                        vaddr + p_off + done, copy);
1464                                 done += copy;
1465                                 d_off += copy;
1466                         }
1467                         kunmap_atomic(vaddr);
1468                 }
1469         }
1470
1471         /* skb frags release userspace buffers */
1472         for (i = 0; i < num_frags; i++)
1473                 skb_frag_unref(skb, i);
1474
1475         /* skb frags point to kernel buffers */
1476         for (i = 0; i < new_frags - 1; i++) {
1477                 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
1478                 head = (struct page *)page_private(head);
1479         }
1480         __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1481         skb_shinfo(skb)->nr_frags = new_frags;
1482
1483 release:
1484         skb_zcopy_clear(skb, false);
1485         return 0;
1486 }
1487 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1488
1489 /**
1490  *      skb_clone       -       duplicate an sk_buff
1491  *      @skb: buffer to clone
1492  *      @gfp_mask: allocation priority
1493  *
1494  *      Duplicate an &sk_buff. The new one is not owned by a socket. Both
1495  *      copies share the same packet data but not structure. The new
1496  *      buffer has a reference count of 1. If the allocation fails the
1497  *      function returns %NULL otherwise the new buffer is returned.
1498  *
1499  *      If this function is called from an interrupt gfp_mask() must be
1500  *      %GFP_ATOMIC.
1501  */
1502
1503 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1504 {
1505         struct sk_buff_fclones *fclones = container_of(skb,
1506                                                        struct sk_buff_fclones,
1507                                                        skb1);
1508         struct sk_buff *n;
1509
1510         if (skb_orphan_frags(skb, gfp_mask))
1511                 return NULL;
1512
1513         if (skb->fclone == SKB_FCLONE_ORIG &&
1514             refcount_read(&fclones->fclone_ref) == 1) {
1515                 n = &fclones->skb2;
1516                 refcount_set(&fclones->fclone_ref, 2);
1517         } else {
1518                 if (skb_pfmemalloc(skb))
1519                         gfp_mask |= __GFP_MEMALLOC;
1520
1521                 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1522                 if (!n)
1523                         return NULL;
1524
1525                 n->fclone = SKB_FCLONE_UNAVAILABLE;
1526         }
1527
1528         return __skb_clone(n, skb);
1529 }
1530 EXPORT_SYMBOL(skb_clone);
1531
1532 void skb_headers_offset_update(struct sk_buff *skb, int off)
1533 {
1534         /* Only adjust this if it actually is csum_start rather than csum */
1535         if (skb->ip_summed == CHECKSUM_PARTIAL)
1536                 skb->csum_start += off;
1537         /* {transport,network,mac}_header and tail are relative to skb->head */
1538         skb->transport_header += off;
1539         skb->network_header   += off;
1540         if (skb_mac_header_was_set(skb))
1541                 skb->mac_header += off;
1542         skb->inner_transport_header += off;
1543         skb->inner_network_header += off;
1544         skb->inner_mac_header += off;
1545 }
1546 EXPORT_SYMBOL(skb_headers_offset_update);
1547
1548 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
1549 {
1550         __copy_skb_header(new, old);
1551
1552         skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1553         skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1554         skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1555 }
1556 EXPORT_SYMBOL(skb_copy_header);
1557
1558 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1559 {
1560         if (skb_pfmemalloc(skb))
1561                 return SKB_ALLOC_RX;
1562         return 0;
1563 }
1564
1565 /**
1566  *      skb_copy        -       create private copy of an sk_buff
1567  *      @skb: buffer to copy
1568  *      @gfp_mask: allocation priority
1569  *
1570  *      Make a copy of both an &sk_buff and its data. This is used when the
1571  *      caller wishes to modify the data and needs a private copy of the
1572  *      data to alter. Returns %NULL on failure or the pointer to the buffer
1573  *      on success. The returned buffer has a reference count of 1.
1574  *
1575  *      As by-product this function converts non-linear &sk_buff to linear
1576  *      one, so that &sk_buff becomes completely private and caller is allowed
1577  *      to modify all the data of returned buffer. This means that this
1578  *      function is not recommended for use in circumstances when only
1579  *      header is going to be modified. Use pskb_copy() instead.
1580  */
1581
1582 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1583 {
1584         int headerlen = skb_headroom(skb);
1585         unsigned int size = skb_end_offset(skb) + skb->data_len;
1586         struct sk_buff *n = __alloc_skb(size, gfp_mask,
1587                                         skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1588
1589         if (!n)
1590                 return NULL;
1591
1592         /* Set the data pointer */
1593         skb_reserve(n, headerlen);
1594         /* Set the tail pointer and length */
1595         skb_put(n, skb->len);
1596
1597         BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
1598
1599         skb_copy_header(n, skb);
1600         return n;
1601 }
1602 EXPORT_SYMBOL(skb_copy);
1603
1604 /**
1605  *      __pskb_copy_fclone      -  create copy of an sk_buff with private head.
1606  *      @skb: buffer to copy
1607  *      @headroom: headroom of new skb
1608  *      @gfp_mask: allocation priority
1609  *      @fclone: if true allocate the copy of the skb from the fclone
1610  *      cache instead of the head cache; it is recommended to set this
1611  *      to true for the cases where the copy will likely be cloned
1612  *
1613  *      Make a copy of both an &sk_buff and part of its data, located
1614  *      in header. Fragmented data remain shared. This is used when
1615  *      the caller wishes to modify only header of &sk_buff and needs
1616  *      private copy of the header to alter. Returns %NULL on failure
1617  *      or the pointer to the buffer on success.
1618  *      The returned buffer has a reference count of 1.
1619  */
1620
1621 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1622                                    gfp_t gfp_mask, bool fclone)
1623 {
1624         unsigned int size = skb_headlen(skb) + headroom;
1625         int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1626         struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1627
1628         if (!n)
1629                 goto out;
1630
1631         /* Set the data pointer */
1632         skb_reserve(n, headroom);
1633         /* Set the tail pointer and length */
1634         skb_put(n, skb_headlen(skb));
1635         /* Copy the bytes */
1636         skb_copy_from_linear_data(skb, n->data, n->len);
1637
1638         n->truesize += skb->data_len;
1639         n->data_len  = skb->data_len;
1640         n->len       = skb->len;
1641
1642         if (skb_shinfo(skb)->nr_frags) {
1643                 int i;
1644
1645                 if (skb_orphan_frags(skb, gfp_mask) ||
1646                     skb_zerocopy_clone(n, skb, gfp_mask)) {
1647                         kfree_skb(n);
1648                         n = NULL;
1649                         goto out;
1650                 }
1651                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1652                         skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1653                         skb_frag_ref(skb, i);
1654                 }
1655                 skb_shinfo(n)->nr_frags = i;
1656         }
1657
1658         if (skb_has_frag_list(skb)) {
1659                 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1660                 skb_clone_fraglist(n);
1661         }
1662
1663         skb_copy_header(n, skb);
1664 out:
1665         return n;
1666 }
1667 EXPORT_SYMBOL(__pskb_copy_fclone);
1668
1669 /**
1670  *      pskb_expand_head - reallocate header of &sk_buff
1671  *      @skb: buffer to reallocate
1672  *      @nhead: room to add at head
1673  *      @ntail: room to add at tail
1674  *      @gfp_mask: allocation priority
1675  *
1676  *      Expands (or creates identical copy, if @nhead and @ntail are zero)
1677  *      header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1678  *      reference count of 1. Returns zero in the case of success or error,
1679  *      if expansion failed. In the last case, &sk_buff is not changed.
1680  *
1681  *      All the pointers pointing into skb header may change and must be
1682  *      reloaded after call to this function.
1683  */
1684
1685 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1686                      gfp_t gfp_mask)
1687 {
1688         int i, osize = skb_end_offset(skb);
1689         int size = osize + nhead + ntail;
1690         long off;
1691         u8 *data;
1692
1693         BUG_ON(nhead < 0);
1694
1695         BUG_ON(skb_shared(skb));
1696
1697         size = SKB_DATA_ALIGN(size);
1698
1699         if (skb_pfmemalloc(skb))
1700                 gfp_mask |= __GFP_MEMALLOC;
1701         data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1702                                gfp_mask, NUMA_NO_NODE, NULL);
1703         if (!data)
1704                 goto nodata;
1705         size = SKB_WITH_OVERHEAD(ksize(data));
1706
1707         /* Copy only real data... and, alas, header. This should be
1708          * optimized for the cases when header is void.
1709          */
1710         memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1711
1712         memcpy((struct skb_shared_info *)(data + size),
1713                skb_shinfo(skb),
1714                offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1715
1716         /*
1717          * if shinfo is shared we must drop the old head gracefully, but if it
1718          * is not we can just drop the old head and let the existing refcount
1719          * be since all we did is relocate the values
1720          */
1721         if (skb_cloned(skb)) {
1722                 if (skb_orphan_frags(skb, gfp_mask))
1723                         goto nofrags;
1724                 if (skb_zcopy(skb))
1725                         refcount_inc(&skb_uarg(skb)->refcnt);
1726                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1727                         skb_frag_ref(skb, i);
1728
1729                 if (skb_has_frag_list(skb))
1730                         skb_clone_fraglist(skb);
1731
1732                 skb_release_data(skb);
1733         } else {
1734                 skb_free_head(skb);
1735         }
1736         off = (data + nhead) - skb->head;
1737
1738         skb->head     = data;
1739         skb->head_frag = 0;
1740         skb->data    += off;
1741
1742         skb_set_end_offset(skb, size);
1743 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1744         off           = nhead;
1745 #endif
1746         skb->tail             += off;
1747         skb_headers_offset_update(skb, nhead);
1748         skb->cloned   = 0;
1749         skb->hdr_len  = 0;
1750         skb->nohdr    = 0;
1751         atomic_set(&skb_shinfo(skb)->dataref, 1);
1752
1753         skb_metadata_clear(skb);
1754
1755         /* It is not generally safe to change skb->truesize.
1756          * For the moment, we really care of rx path, or
1757          * when skb is orphaned (not attached to a socket).
1758          */
1759         if (!skb->sk || skb->destructor == sock_edemux)
1760                 skb->truesize += size - osize;
1761
1762         return 0;
1763
1764 nofrags:
1765         kfree(data);
1766 nodata:
1767         return -ENOMEM;
1768 }
1769 EXPORT_SYMBOL(pskb_expand_head);
1770
1771 /* Make private copy of skb with writable head and some headroom */
1772
1773 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1774 {
1775         struct sk_buff *skb2;
1776         int delta = headroom - skb_headroom(skb);
1777
1778         if (delta <= 0)
1779                 skb2 = pskb_copy(skb, GFP_ATOMIC);
1780         else {
1781                 skb2 = skb_clone(skb, GFP_ATOMIC);
1782                 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1783                                              GFP_ATOMIC)) {
1784                         kfree_skb(skb2);
1785                         skb2 = NULL;
1786                 }
1787         }
1788         return skb2;
1789 }
1790 EXPORT_SYMBOL(skb_realloc_headroom);
1791
1792 int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri)
1793 {
1794         unsigned int saved_end_offset, saved_truesize;
1795         struct skb_shared_info *shinfo;
1796         int res;
1797
1798         saved_end_offset = skb_end_offset(skb);
1799         saved_truesize = skb->truesize;
1800
1801         res = pskb_expand_head(skb, 0, 0, pri);
1802         if (res)
1803                 return res;
1804
1805         skb->truesize = saved_truesize;
1806
1807         if (likely(skb_end_offset(skb) == saved_end_offset))
1808                 return 0;
1809
1810         shinfo = skb_shinfo(skb);
1811
1812         /* We are about to change back skb->end,
1813          * we need to move skb_shinfo() to its new location.
1814          */
1815         memmove(skb->head + saved_end_offset,
1816                 shinfo,
1817                 offsetof(struct skb_shared_info, frags[shinfo->nr_frags]));
1818
1819         skb_set_end_offset(skb, saved_end_offset);
1820
1821         return 0;
1822 }
1823
1824 /**
1825  *      skb_expand_head - reallocate header of &sk_buff
1826  *      @skb: buffer to reallocate
1827  *      @headroom: needed headroom
1828  *
1829  *      Unlike skb_realloc_headroom, this one does not allocate a new skb
1830  *      if possible; copies skb->sk to new skb as needed
1831  *      and frees original skb in case of failures.
1832  *
1833  *      It expect increased headroom and generates warning otherwise.
1834  */
1835
1836 struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom)
1837 {
1838         int delta = headroom - skb_headroom(skb);
1839         int osize = skb_end_offset(skb);
1840         struct sock *sk = skb->sk;
1841
1842         if (WARN_ONCE(delta <= 0,
1843                       "%s is expecting an increase in the headroom", __func__))
1844                 return skb;
1845
1846         delta = SKB_DATA_ALIGN(delta);
1847         /* pskb_expand_head() might crash, if skb is shared. */
1848         if (skb_shared(skb) || !is_skb_wmem(skb)) {
1849                 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1850
1851                 if (unlikely(!nskb))
1852                         goto fail;
1853
1854                 if (sk)
1855                         skb_set_owner_w(nskb, sk);
1856                 consume_skb(skb);
1857                 skb = nskb;
1858         }
1859         if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC))
1860                 goto fail;
1861
1862         if (sk && is_skb_wmem(skb)) {
1863                 delta = skb_end_offset(skb) - osize;
1864                 refcount_add(delta, &sk->sk_wmem_alloc);
1865                 skb->truesize += delta;
1866         }
1867         return skb;
1868
1869 fail:
1870         kfree_skb(skb);
1871         return NULL;
1872 }
1873 EXPORT_SYMBOL(skb_expand_head);
1874
1875 /**
1876  *      skb_copy_expand -       copy and expand sk_buff
1877  *      @skb: buffer to copy
1878  *      @newheadroom: new free bytes at head
1879  *      @newtailroom: new free bytes at tail
1880  *      @gfp_mask: allocation priority
1881  *
1882  *      Make a copy of both an &sk_buff and its data and while doing so
1883  *      allocate additional space.
1884  *
1885  *      This is used when the caller wishes to modify the data and needs a
1886  *      private copy of the data to alter as well as more space for new fields.
1887  *      Returns %NULL on failure or the pointer to the buffer
1888  *      on success. The returned buffer has a reference count of 1.
1889  *
1890  *      You must pass %GFP_ATOMIC as the allocation priority if this function
1891  *      is called from an interrupt.
1892  */
1893 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1894                                 int newheadroom, int newtailroom,
1895                                 gfp_t gfp_mask)
1896 {
1897         /*
1898          *      Allocate the copy buffer
1899          */
1900         struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1901                                         gfp_mask, skb_alloc_rx_flag(skb),
1902                                         NUMA_NO_NODE);
1903         int oldheadroom = skb_headroom(skb);
1904         int head_copy_len, head_copy_off;
1905
1906         if (!n)
1907                 return NULL;
1908
1909         skb_reserve(n, newheadroom);
1910
1911         /* Set the tail pointer and length */
1912         skb_put(n, skb->len);
1913
1914         head_copy_len = oldheadroom;
1915         head_copy_off = 0;
1916         if (newheadroom <= head_copy_len)
1917                 head_copy_len = newheadroom;
1918         else
1919                 head_copy_off = newheadroom - head_copy_len;
1920
1921         /* Copy the linear header and data. */
1922         BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1923                              skb->len + head_copy_len));
1924
1925         skb_copy_header(n, skb);
1926
1927         skb_headers_offset_update(n, newheadroom - oldheadroom);
1928
1929         return n;
1930 }
1931 EXPORT_SYMBOL(skb_copy_expand);
1932
1933 /**
1934  *      __skb_pad               -       zero pad the tail of an skb
1935  *      @skb: buffer to pad
1936  *      @pad: space to pad
1937  *      @free_on_error: free buffer on error
1938  *
1939  *      Ensure that a buffer is followed by a padding area that is zero
1940  *      filled. Used by network drivers which may DMA or transfer data
1941  *      beyond the buffer end onto the wire.
1942  *
1943  *      May return error in out of memory cases. The skb is freed on error
1944  *      if @free_on_error is true.
1945  */
1946
1947 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
1948 {
1949         int err;
1950         int ntail;
1951
1952         /* If the skbuff is non linear tailroom is always zero.. */
1953         if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1954                 memset(skb->data+skb->len, 0, pad);
1955                 return 0;
1956         }
1957
1958         ntail = skb->data_len + pad - (skb->end - skb->tail);
1959         if (likely(skb_cloned(skb) || ntail > 0)) {
1960                 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1961                 if (unlikely(err))
1962                         goto free_skb;
1963         }
1964
1965         /* FIXME: The use of this function with non-linear skb's really needs
1966          * to be audited.
1967          */
1968         err = skb_linearize(skb);
1969         if (unlikely(err))
1970                 goto free_skb;
1971
1972         memset(skb->data + skb->len, 0, pad);
1973         return 0;
1974
1975 free_skb:
1976         if (free_on_error)
1977                 kfree_skb(skb);
1978         return err;
1979 }
1980 EXPORT_SYMBOL(__skb_pad);
1981
1982 /**
1983  *      pskb_put - add data to the tail of a potentially fragmented buffer
1984  *      @skb: start of the buffer to use
1985  *      @tail: tail fragment of the buffer to use
1986  *      @len: amount of data to add
1987  *
1988  *      This function extends the used data area of the potentially
1989  *      fragmented buffer. @tail must be the last fragment of @skb -- or
1990  *      @skb itself. If this would exceed the total buffer size the kernel
1991  *      will panic. A pointer to the first byte of the extra data is
1992  *      returned.
1993  */
1994
1995 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1996 {
1997         if (tail != skb) {
1998                 skb->data_len += len;
1999                 skb->len += len;
2000         }
2001         return skb_put(tail, len);
2002 }
2003 EXPORT_SYMBOL_GPL(pskb_put);
2004
2005 /**
2006  *      skb_put - add data to a buffer
2007  *      @skb: buffer to use
2008  *      @len: amount of data to add
2009  *
2010  *      This function extends the used data area of the buffer. If this would
2011  *      exceed the total buffer size the kernel will panic. A pointer to the
2012  *      first byte of the extra data is returned.
2013  */
2014 void *skb_put(struct sk_buff *skb, unsigned int len)
2015 {
2016         void *tmp = skb_tail_pointer(skb);
2017         SKB_LINEAR_ASSERT(skb);
2018         skb->tail += len;
2019         skb->len  += len;
2020         if (unlikely(skb->tail > skb->end))
2021                 skb_over_panic(skb, len, __builtin_return_address(0));
2022         return tmp;
2023 }
2024 EXPORT_SYMBOL(skb_put);
2025
2026 /**
2027  *      skb_push - add data to the start of a buffer
2028  *      @skb: buffer to use
2029  *      @len: amount of data to add
2030  *
2031  *      This function extends the used data area of the buffer at the buffer
2032  *      start. If this would exceed the total buffer headroom the kernel will
2033  *      panic. A pointer to the first byte of the extra data is returned.
2034  */
2035 void *skb_push(struct sk_buff *skb, unsigned int len)
2036 {
2037         skb->data -= len;
2038         skb->len  += len;
2039         if (unlikely(skb->data < skb->head))
2040                 skb_under_panic(skb, len, __builtin_return_address(0));
2041         return skb->data;
2042 }
2043 EXPORT_SYMBOL(skb_push);
2044
2045 /**
2046  *      skb_pull - remove data from the start of a buffer
2047  *      @skb: buffer to use
2048  *      @len: amount of data to remove
2049  *
2050  *      This function removes data from the start of a buffer, returning
2051  *      the memory to the headroom. A pointer to the next data in the buffer
2052  *      is returned. Once the data has been pulled future pushes will overwrite
2053  *      the old data.
2054  */
2055 void *skb_pull(struct sk_buff *skb, unsigned int len)
2056 {
2057         return skb_pull_inline(skb, len);
2058 }
2059 EXPORT_SYMBOL(skb_pull);
2060
2061 /**
2062  *      skb_trim - remove end from a buffer
2063  *      @skb: buffer to alter
2064  *      @len: new length
2065  *
2066  *      Cut the length of a buffer down by removing data from the tail. If
2067  *      the buffer is already under the length specified it is not modified.
2068  *      The skb must be linear.
2069  */
2070 void skb_trim(struct sk_buff *skb, unsigned int len)
2071 {
2072         if (skb->len > len)
2073                 __skb_trim(skb, len);
2074 }
2075 EXPORT_SYMBOL(skb_trim);
2076
2077 /* Trims skb to length len. It can change skb pointers.
2078  */
2079
2080 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
2081 {
2082         struct sk_buff **fragp;
2083         struct sk_buff *frag;
2084         int offset = skb_headlen(skb);
2085         int nfrags = skb_shinfo(skb)->nr_frags;
2086         int i;
2087         int err;
2088
2089         if (skb_cloned(skb) &&
2090             unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
2091                 return err;
2092
2093         i = 0;
2094         if (offset >= len)
2095                 goto drop_pages;
2096
2097         for (; i < nfrags; i++) {
2098                 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2099
2100                 if (end < len) {
2101                         offset = end;
2102                         continue;
2103                 }
2104
2105                 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
2106
2107 drop_pages:
2108                 skb_shinfo(skb)->nr_frags = i;
2109
2110                 for (; i < nfrags; i++)
2111                         skb_frag_unref(skb, i);
2112
2113                 if (skb_has_frag_list(skb))
2114                         skb_drop_fraglist(skb);
2115                 goto done;
2116         }
2117
2118         for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
2119              fragp = &frag->next) {
2120                 int end = offset + frag->len;
2121
2122                 if (skb_shared(frag)) {
2123                         struct sk_buff *nfrag;
2124
2125                         nfrag = skb_clone(frag, GFP_ATOMIC);
2126                         if (unlikely(!nfrag))
2127                                 return -ENOMEM;
2128
2129                         nfrag->next = frag->next;
2130                         consume_skb(frag);
2131                         frag = nfrag;
2132                         *fragp = frag;
2133                 }
2134
2135                 if (end < len) {
2136                         offset = end;
2137                         continue;
2138                 }
2139
2140                 if (end > len &&
2141                     unlikely((err = pskb_trim(frag, len - offset))))
2142                         return err;
2143
2144                 if (frag->next)
2145                         skb_drop_list(&frag->next);
2146                 break;
2147         }
2148
2149 done:
2150         if (len > skb_headlen(skb)) {
2151                 skb->data_len -= skb->len - len;
2152                 skb->len       = len;
2153         } else {
2154                 skb->len       = len;
2155                 skb->data_len  = 0;
2156                 skb_set_tail_pointer(skb, len);
2157         }
2158
2159         if (!skb->sk || skb->destructor == sock_edemux)
2160                 skb_condense(skb);
2161         return 0;
2162 }
2163 EXPORT_SYMBOL(___pskb_trim);
2164
2165 /* Note : use pskb_trim_rcsum() instead of calling this directly
2166  */
2167 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
2168 {
2169         if (skb->ip_summed == CHECKSUM_COMPLETE) {
2170                 int delta = skb->len - len;
2171
2172                 skb->csum = csum_block_sub(skb->csum,
2173                                            skb_checksum(skb, len, delta, 0),
2174                                            len);
2175         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2176                 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
2177                 int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
2178
2179                 if (offset + sizeof(__sum16) > hdlen)
2180                         return -EINVAL;
2181         }
2182         return __pskb_trim(skb, len);
2183 }
2184 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
2185
2186 /**
2187  *      __pskb_pull_tail - advance tail of skb header
2188  *      @skb: buffer to reallocate
2189  *      @delta: number of bytes to advance tail
2190  *
2191  *      The function makes a sense only on a fragmented &sk_buff,
2192  *      it expands header moving its tail forward and copying necessary
2193  *      data from fragmented part.
2194  *
2195  *      &sk_buff MUST have reference count of 1.
2196  *
2197  *      Returns %NULL (and &sk_buff does not change) if pull failed
2198  *      or value of new tail of skb in the case of success.
2199  *
2200  *      All the pointers pointing into skb header may change and must be
2201  *      reloaded after call to this function.
2202  */
2203
2204 /* Moves tail of skb head forward, copying data from fragmented part,
2205  * when it is necessary.
2206  * 1. It may fail due to malloc failure.
2207  * 2. It may change skb pointers.
2208  *
2209  * It is pretty complicated. Luckily, it is called only in exceptional cases.
2210  */
2211 void *__pskb_pull_tail(struct sk_buff *skb, int delta)
2212 {
2213         /* If skb has not enough free space at tail, get new one
2214          * plus 128 bytes for future expansions. If we have enough
2215          * room at tail, reallocate without expansion only if skb is cloned.
2216          */
2217         int i, k, eat = (skb->tail + delta) - skb->end;
2218
2219         if (eat > 0 || skb_cloned(skb)) {
2220                 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
2221                                      GFP_ATOMIC))
2222                         return NULL;
2223         }
2224
2225         BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
2226                              skb_tail_pointer(skb), delta));
2227
2228         /* Optimization: no fragments, no reasons to preestimate
2229          * size of pulled pages. Superb.
2230          */
2231         if (!skb_has_frag_list(skb))
2232                 goto pull_pages;
2233
2234         /* Estimate size of pulled pages. */
2235         eat = delta;
2236         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2237                 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2238
2239                 if (size >= eat)
2240                         goto pull_pages;
2241                 eat -= size;
2242         }
2243
2244         /* If we need update frag list, we are in troubles.
2245          * Certainly, it is possible to add an offset to skb data,
2246          * but taking into account that pulling is expected to
2247          * be very rare operation, it is worth to fight against
2248          * further bloating skb head and crucify ourselves here instead.
2249          * Pure masohism, indeed. 8)8)
2250          */
2251         if (eat) {
2252                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2253                 struct sk_buff *clone = NULL;
2254                 struct sk_buff *insp = NULL;
2255
2256                 do {
2257                         if (list->len <= eat) {
2258                                 /* Eaten as whole. */
2259                                 eat -= list->len;
2260                                 list = list->next;
2261                                 insp = list;
2262                         } else {
2263                                 /* Eaten partially. */
2264
2265                                 if (skb_shared(list)) {
2266                                         /* Sucks! We need to fork list. :-( */
2267                                         clone = skb_clone(list, GFP_ATOMIC);
2268                                         if (!clone)
2269                                                 return NULL;
2270                                         insp = list->next;
2271                                         list = clone;
2272                                 } else {
2273                                         /* This may be pulled without
2274                                          * problems. */
2275                                         insp = list;
2276                                 }
2277                                 if (!pskb_pull(list, eat)) {
2278                                         kfree_skb(clone);
2279                                         return NULL;
2280                                 }
2281                                 break;
2282                         }
2283                 } while (eat);
2284
2285                 /* Free pulled out fragments. */
2286                 while ((list = skb_shinfo(skb)->frag_list) != insp) {
2287                         skb_shinfo(skb)->frag_list = list->next;
2288                         consume_skb(list);
2289                 }
2290                 /* And insert new clone at head. */
2291                 if (clone) {
2292                         clone->next = list;
2293                         skb_shinfo(skb)->frag_list = clone;
2294                 }
2295         }
2296         /* Success! Now we may commit changes to skb data. */
2297
2298 pull_pages:
2299         eat = delta;
2300         k = 0;
2301         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2302                 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2303
2304                 if (size <= eat) {
2305                         skb_frag_unref(skb, i);
2306                         eat -= size;
2307                 } else {
2308                         skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2309
2310                         *frag = skb_shinfo(skb)->frags[i];
2311                         if (eat) {
2312                                 skb_frag_off_add(frag, eat);
2313                                 skb_frag_size_sub(frag, eat);
2314                                 if (!i)
2315                                         goto end;
2316                                 eat = 0;
2317                         }
2318                         k++;
2319                 }
2320         }
2321         skb_shinfo(skb)->nr_frags = k;
2322
2323 end:
2324         skb->tail     += delta;
2325         skb->data_len -= delta;
2326
2327         if (!skb->data_len)
2328                 skb_zcopy_clear(skb, false);
2329
2330         return skb_tail_pointer(skb);
2331 }
2332 EXPORT_SYMBOL(__pskb_pull_tail);
2333
2334 /**
2335  *      skb_copy_bits - copy bits from skb to kernel buffer
2336  *      @skb: source skb
2337  *      @offset: offset in source
2338  *      @to: destination buffer
2339  *      @len: number of bytes to copy
2340  *
2341  *      Copy the specified number of bytes from the source skb to the
2342  *      destination buffer.
2343  *
2344  *      CAUTION ! :
2345  *              If its prototype is ever changed,
2346  *              check arch/{*}/net/{*}.S files,
2347  *              since it is called from BPF assembly code.
2348  */
2349 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2350 {
2351         int start = skb_headlen(skb);
2352         struct sk_buff *frag_iter;
2353         int i, copy;
2354
2355         if (offset > (int)skb->len - len)
2356                 goto fault;
2357
2358         /* Copy header. */
2359         if ((copy = start - offset) > 0) {
2360                 if (copy > len)
2361                         copy = len;
2362                 skb_copy_from_linear_data_offset(skb, offset, to, copy);
2363                 if ((len -= copy) == 0)
2364                         return 0;
2365                 offset += copy;
2366                 to     += copy;
2367         }
2368
2369         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2370                 int end;
2371                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
2372
2373                 WARN_ON(start > offset + len);
2374
2375                 end = start + skb_frag_size(f);
2376                 if ((copy = end - offset) > 0) {
2377                         u32 p_off, p_len, copied;
2378                         struct page *p;
2379                         u8 *vaddr;
2380
2381                         if (copy > len)
2382                                 copy = len;
2383
2384                         skb_frag_foreach_page(f,
2385                                               skb_frag_off(f) + offset - start,
2386                                               copy, p, p_off, p_len, copied) {
2387                                 vaddr = kmap_atomic(p);
2388                                 memcpy(to + copied, vaddr + p_off, p_len);
2389                                 kunmap_atomic(vaddr);
2390                         }
2391
2392                         if ((len -= copy) == 0)
2393                                 return 0;
2394                         offset += copy;
2395                         to     += copy;
2396                 }
2397                 start = end;
2398         }
2399
2400         skb_walk_frags(skb, frag_iter) {
2401                 int end;
2402
2403                 WARN_ON(start > offset + len);
2404
2405                 end = start + frag_iter->len;
2406                 if ((copy = end - offset) > 0) {
2407                         if (copy > len)
2408                                 copy = len;
2409                         if (skb_copy_bits(frag_iter, offset - start, to, copy))
2410                                 goto fault;
2411                         if ((len -= copy) == 0)
2412                                 return 0;
2413                         offset += copy;
2414                         to     += copy;
2415                 }
2416                 start = end;
2417         }
2418
2419         if (!len)
2420                 return 0;
2421
2422 fault:
2423         return -EFAULT;
2424 }
2425 EXPORT_SYMBOL(skb_copy_bits);
2426
2427 /*
2428  * Callback from splice_to_pipe(), if we need to release some pages
2429  * at the end of the spd in case we error'ed out in filling the pipe.
2430  */
2431 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2432 {
2433         put_page(spd->pages[i]);
2434 }
2435
2436 static struct page *linear_to_page(struct page *page, unsigned int *len,
2437                                    unsigned int *offset,
2438                                    struct sock *sk)
2439 {
2440         struct page_frag *pfrag = sk_page_frag(sk);
2441
2442         if (!sk_page_frag_refill(sk, pfrag))
2443                 return NULL;
2444
2445         *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
2446
2447         memcpy(page_address(pfrag->page) + pfrag->offset,
2448                page_address(page) + *offset, *len);
2449         *offset = pfrag->offset;
2450         pfrag->offset += *len;
2451
2452         return pfrag->page;
2453 }
2454
2455 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2456                              struct page *page,
2457                              unsigned int offset)
2458 {
2459         return  spd->nr_pages &&
2460                 spd->pages[spd->nr_pages - 1] == page &&
2461                 (spd->partial[spd->nr_pages - 1].offset +
2462                  spd->partial[spd->nr_pages - 1].len == offset);
2463 }
2464
2465 /*
2466  * Fill page/offset/length into spd, if it can hold more pages.
2467  */
2468 static bool spd_fill_page(struct splice_pipe_desc *spd,
2469                           struct pipe_inode_info *pipe, struct page *page,
2470                           unsigned int *len, unsigned int offset,
2471                           bool linear,
2472                           struct sock *sk)
2473 {
2474         if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2475                 return true;
2476
2477         if (linear) {
2478                 page = linear_to_page(page, len, &offset, sk);
2479                 if (!page)
2480                         return true;
2481         }
2482         if (spd_can_coalesce(spd, page, offset)) {
2483                 spd->partial[spd->nr_pages - 1].len += *len;
2484                 return false;
2485         }
2486         get_page(page);
2487         spd->pages[spd->nr_pages] = page;
2488         spd->partial[spd->nr_pages].len = *len;
2489         spd->partial[spd->nr_pages].offset = offset;
2490         spd->nr_pages++;
2491
2492         return false;
2493 }
2494
2495 static bool __splice_segment(struct page *page, unsigned int poff,
2496                              unsigned int plen, unsigned int *off,
2497                              unsigned int *len,
2498                              struct splice_pipe_desc *spd, bool linear,
2499                              struct sock *sk,
2500                              struct pipe_inode_info *pipe)
2501 {
2502         if (!*len)
2503                 return true;
2504
2505         /* skip this segment if already processed */
2506         if (*off >= plen) {
2507                 *off -= plen;
2508                 return false;
2509         }
2510
2511         /* ignore any bits we already processed */
2512         poff += *off;
2513         plen -= *off;
2514         *off = 0;
2515
2516         do {
2517                 unsigned int flen = min(*len, plen);
2518
2519                 if (spd_fill_page(spd, pipe, page, &flen, poff,
2520                                   linear, sk))
2521                         return true;
2522                 poff += flen;
2523                 plen -= flen;
2524                 *len -= flen;
2525         } while (*len && plen);
2526
2527         return false;
2528 }
2529
2530 /*
2531  * Map linear and fragment data from the skb to spd. It reports true if the
2532  * pipe is full or if we already spliced the requested length.
2533  */
2534 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2535                               unsigned int *offset, unsigned int *len,
2536                               struct splice_pipe_desc *spd, struct sock *sk)
2537 {
2538         int seg;
2539         struct sk_buff *iter;
2540
2541         /* map the linear part :
2542          * If skb->head_frag is set, this 'linear' part is backed by a
2543          * fragment, and if the head is not shared with any clones then
2544          * we can avoid a copy since we own the head portion of this page.
2545          */
2546         if (__splice_segment(virt_to_page(skb->data),
2547                              (unsigned long) skb->data & (PAGE_SIZE - 1),
2548                              skb_headlen(skb),
2549                              offset, len, spd,
2550                              skb_head_is_locked(skb),
2551                              sk, pipe))
2552                 return true;
2553
2554         /*
2555          * then map the fragments
2556          */
2557         for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2558                 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2559
2560                 if (__splice_segment(skb_frag_page(f),
2561                                      skb_frag_off(f), skb_frag_size(f),
2562                                      offset, len, spd, false, sk, pipe))
2563                         return true;
2564         }
2565
2566         skb_walk_frags(skb, iter) {
2567                 if (*offset >= iter->len) {
2568                         *offset -= iter->len;
2569                         continue;
2570                 }
2571                 /* __skb_splice_bits() only fails if the output has no room
2572                  * left, so no point in going over the frag_list for the error
2573                  * case.
2574                  */
2575                 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2576                         return true;
2577         }
2578
2579         return false;
2580 }
2581
2582 /*
2583  * Map data from the skb to a pipe. Should handle both the linear part,
2584  * the fragments, and the frag list.
2585  */
2586 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
2587                     struct pipe_inode_info *pipe, unsigned int tlen,
2588                     unsigned int flags)
2589 {
2590         struct partial_page partial[MAX_SKB_FRAGS];
2591         struct page *pages[MAX_SKB_FRAGS];
2592         struct splice_pipe_desc spd = {
2593                 .pages = pages,
2594                 .partial = partial,
2595                 .nr_pages_max = MAX_SKB_FRAGS,
2596                 .ops = &nosteal_pipe_buf_ops,
2597                 .spd_release = sock_spd_release,
2598         };
2599         int ret = 0;
2600
2601         __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
2602
2603         if (spd.nr_pages)
2604                 ret = splice_to_pipe(pipe, &spd);
2605
2606         return ret;
2607 }
2608 EXPORT_SYMBOL_GPL(skb_splice_bits);
2609
2610 static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg,
2611                             struct kvec *vec, size_t num, size_t size)
2612 {
2613         struct socket *sock = sk->sk_socket;
2614
2615         if (!sock)
2616                 return -EINVAL;
2617         return kernel_sendmsg(sock, msg, vec, num, size);
2618 }
2619
2620 static int sendpage_unlocked(struct sock *sk, struct page *page, int offset,
2621                              size_t size, int flags)
2622 {
2623         struct socket *sock = sk->sk_socket;
2624
2625         if (!sock)
2626                 return -EINVAL;
2627         return kernel_sendpage(sock, page, offset, size, flags);
2628 }
2629
2630 typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg,
2631                             struct kvec *vec, size_t num, size_t size);
2632 typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset,
2633                              size_t size, int flags);
2634 static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
2635                            int len, sendmsg_func sendmsg, sendpage_func sendpage)
2636 {
2637         unsigned int orig_len = len;
2638         struct sk_buff *head = skb;
2639         unsigned short fragidx;
2640         int slen, ret;
2641
2642 do_frag_list:
2643
2644         /* Deal with head data */
2645         while (offset < skb_headlen(skb) && len) {
2646                 struct kvec kv;
2647                 struct msghdr msg;
2648
2649                 slen = min_t(int, len, skb_headlen(skb) - offset);
2650                 kv.iov_base = skb->data + offset;
2651                 kv.iov_len = slen;
2652                 memset(&msg, 0, sizeof(msg));
2653                 msg.msg_flags = MSG_DONTWAIT;
2654
2655                 ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked,
2656                                       sendmsg_unlocked, sk, &msg, &kv, 1, slen);
2657                 if (ret <= 0)
2658                         goto error;
2659
2660                 offset += ret;
2661                 len -= ret;
2662         }
2663
2664         /* All the data was skb head? */
2665         if (!len)
2666                 goto out;
2667
2668         /* Make offset relative to start of frags */
2669         offset -= skb_headlen(skb);
2670
2671         /* Find where we are in frag list */
2672         for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2673                 skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
2674
2675                 if (offset < skb_frag_size(frag))
2676                         break;
2677
2678                 offset -= skb_frag_size(frag);
2679         }
2680
2681         for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2682                 skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
2683
2684                 slen = min_t(size_t, len, skb_frag_size(frag) - offset);
2685
2686                 while (slen) {
2687                         ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked,
2688                                               sendpage_unlocked, sk,
2689                                               skb_frag_page(frag),
2690                                               skb_frag_off(frag) + offset,
2691                                               slen, MSG_DONTWAIT);
2692                         if (ret <= 0)
2693                                 goto error;
2694
2695                         len -= ret;
2696                         offset += ret;
2697                         slen -= ret;
2698                 }
2699
2700                 offset = 0;
2701         }
2702
2703         if (len) {
2704                 /* Process any frag lists */
2705
2706                 if (skb == head) {
2707                         if (skb_has_frag_list(skb)) {
2708                                 skb = skb_shinfo(skb)->frag_list;
2709                                 goto do_frag_list;
2710                         }
2711                 } else if (skb->next) {
2712                         skb = skb->next;
2713                         goto do_frag_list;
2714                 }
2715         }
2716
2717 out:
2718         return orig_len - len;
2719
2720 error:
2721         return orig_len == len ? ret : orig_len - len;
2722 }
2723
2724 /* Send skb data on a socket. Socket must be locked. */
2725 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2726                          int len)
2727 {
2728         return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked,
2729                                kernel_sendpage_locked);
2730 }
2731 EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2732
2733 /* Send skb data on a socket. Socket must be unlocked. */
2734 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
2735 {
2736         return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked,
2737                                sendpage_unlocked);
2738 }
2739
2740 /**
2741  *      skb_store_bits - store bits from kernel buffer to skb
2742  *      @skb: destination buffer
2743  *      @offset: offset in destination
2744  *      @from: source buffer
2745  *      @len: number of bytes to copy
2746  *
2747  *      Copy the specified number of bytes from the source buffer to the
2748  *      destination skb.  This function handles all the messy bits of
2749  *      traversing fragment lists and such.
2750  */
2751
2752 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2753 {
2754         int start = skb_headlen(skb);
2755         struct sk_buff *frag_iter;
2756         int i, copy;
2757
2758         if (offset > (int)skb->len - len)
2759                 goto fault;
2760
2761         if ((copy = start - offset) > 0) {
2762                 if (copy > len)
2763                         copy = len;
2764                 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2765                 if ((len -= copy) == 0)
2766                         return 0;
2767                 offset += copy;
2768                 from += copy;
2769         }
2770
2771         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2772                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2773                 int end;
2774
2775                 WARN_ON(start > offset + len);
2776
2777                 end = start + skb_frag_size(frag);
2778                 if ((copy = end - offset) > 0) {
2779                         u32 p_off, p_len, copied;
2780                         struct page *p;
2781                         u8 *vaddr;
2782
2783                         if (copy > len)
2784                                 copy = len;
2785
2786                         skb_frag_foreach_page(frag,
2787                                               skb_frag_off(frag) + offset - start,
2788                                               copy, p, p_off, p_len, copied) {
2789                                 vaddr = kmap_atomic(p);
2790                                 memcpy(vaddr + p_off, from + copied, p_len);
2791                                 kunmap_atomic(vaddr);
2792                         }
2793
2794                         if ((len -= copy) == 0)
2795                                 return 0;
2796                         offset += copy;
2797                         from += copy;
2798                 }
2799                 start = end;
2800         }
2801
2802         skb_walk_frags(skb, frag_iter) {
2803                 int end;
2804
2805                 WARN_ON(start > offset + len);
2806
2807                 end = start + frag_iter->len;
2808                 if ((copy = end - offset) > 0) {
2809                         if (copy > len)
2810                                 copy = len;
2811                         if (skb_store_bits(frag_iter, offset - start,
2812                                            from, copy))
2813                                 goto fault;
2814                         if ((len -= copy) == 0)
2815                                 return 0;
2816                         offset += copy;
2817                         from += copy;
2818                 }
2819                 start = end;
2820         }
2821         if (!len)
2822                 return 0;
2823
2824 fault:
2825         return -EFAULT;
2826 }
2827 EXPORT_SYMBOL(skb_store_bits);
2828
2829 /* Checksum skb data. */
2830 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2831                       __wsum csum, const struct skb_checksum_ops *ops)
2832 {
2833         int start = skb_headlen(skb);
2834         int i, copy = start - offset;
2835         struct sk_buff *frag_iter;
2836         int pos = 0;
2837
2838         /* Checksum header. */
2839         if (copy > 0) {
2840                 if (copy > len)
2841                         copy = len;
2842                 csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
2843                                        skb->data + offset, copy, csum);
2844                 if ((len -= copy) == 0)
2845                         return csum;
2846                 offset += copy;
2847                 pos     = copy;
2848         }
2849
2850         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2851                 int end;
2852                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2853
2854                 WARN_ON(start > offset + len);
2855
2856                 end = start + skb_frag_size(frag);
2857                 if ((copy = end - offset) > 0) {
2858                         u32 p_off, p_len, copied;
2859                         struct page *p;
2860                         __wsum csum2;
2861                         u8 *vaddr;
2862
2863                         if (copy > len)
2864                                 copy = len;
2865
2866                         skb_frag_foreach_page(frag,
2867                                               skb_frag_off(frag) + offset - start,
2868                                               copy, p, p_off, p_len, copied) {
2869                                 vaddr = kmap_atomic(p);
2870                                 csum2 = INDIRECT_CALL_1(ops->update,
2871                                                         csum_partial_ext,
2872                                                         vaddr + p_off, p_len, 0);
2873                                 kunmap_atomic(vaddr);
2874                                 csum = INDIRECT_CALL_1(ops->combine,
2875                                                        csum_block_add_ext, csum,
2876                                                        csum2, pos, p_len);
2877                                 pos += p_len;
2878                         }
2879
2880                         if (!(len -= copy))
2881                                 return csum;
2882                         offset += copy;
2883                 }
2884                 start = end;
2885         }
2886
2887         skb_walk_frags(skb, frag_iter) {
2888                 int end;
2889
2890                 WARN_ON(start > offset + len);
2891
2892                 end = start + frag_iter->len;
2893                 if ((copy = end - offset) > 0) {
2894                         __wsum csum2;
2895                         if (copy > len)
2896                                 copy = len;
2897                         csum2 = __skb_checksum(frag_iter, offset - start,
2898                                                copy, 0, ops);
2899                         csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
2900                                                csum, csum2, pos, copy);
2901                         if ((len -= copy) == 0)
2902                                 return csum;
2903                         offset += copy;
2904                         pos    += copy;
2905                 }
2906                 start = end;
2907         }
2908         BUG_ON(len);
2909
2910         return csum;
2911 }
2912 EXPORT_SYMBOL(__skb_checksum);
2913
2914 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2915                     int len, __wsum csum)
2916 {
2917         const struct skb_checksum_ops ops = {
2918                 .update  = csum_partial_ext,
2919                 .combine = csum_block_add_ext,
2920         };
2921
2922         return __skb_checksum(skb, offset, len, csum, &ops);
2923 }
2924 EXPORT_SYMBOL(skb_checksum);
2925
2926 /* Both of above in one bottle. */
2927
2928 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2929                                     u8 *to, int len)
2930 {
2931         int start = skb_headlen(skb);
2932         int i, copy = start - offset;
2933         struct sk_buff *frag_iter;
2934         int pos = 0;
2935         __wsum csum = 0;
2936
2937         /* Copy header. */
2938         if (copy > 0) {
2939                 if (copy > len)
2940                         copy = len;
2941                 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2942                                                  copy);
2943                 if ((len -= copy) == 0)
2944                         return csum;
2945                 offset += copy;
2946                 to     += copy;
2947                 pos     = copy;
2948         }
2949
2950         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2951                 int end;
2952
2953                 WARN_ON(start > offset + len);
2954
2955                 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2956                 if ((copy = end - offset) > 0) {
2957                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2958                         u32 p_off, p_len, copied;
2959                         struct page *p;
2960                         __wsum csum2;
2961                         u8 *vaddr;
2962
2963                         if (copy > len)
2964                                 copy = len;
2965
2966                         skb_frag_foreach_page(frag,
2967                                               skb_frag_off(frag) + offset - start,
2968                                               copy, p, p_off, p_len, copied) {
2969                                 vaddr = kmap_atomic(p);
2970                                 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2971                                                                   to + copied,
2972                                                                   p_len);
2973                                 kunmap_atomic(vaddr);
2974                                 csum = csum_block_add(csum, csum2, pos);
2975                                 pos += p_len;
2976                         }
2977
2978                         if (!(len -= copy))
2979                                 return csum;
2980                         offset += copy;
2981                         to     += copy;
2982                 }
2983                 start = end;
2984         }
2985
2986         skb_walk_frags(skb, frag_iter) {
2987                 __wsum csum2;
2988                 int end;
2989
2990                 WARN_ON(start > offset + len);
2991
2992                 end = start + frag_iter->len;
2993                 if ((copy = end - offset) > 0) {
2994                         if (copy > len)
2995                                 copy = len;
2996                         csum2 = skb_copy_and_csum_bits(frag_iter,
2997                                                        offset - start,
2998                                                        to, copy);
2999                         csum = csum_block_add(csum, csum2, pos);
3000                         if ((len -= copy) == 0)
3001                                 return csum;
3002                         offset += copy;
3003                         to     += copy;
3004                         pos    += copy;
3005                 }
3006                 start = end;
3007         }
3008         BUG_ON(len);
3009         return csum;
3010 }
3011 EXPORT_SYMBOL(skb_copy_and_csum_bits);
3012
3013 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
3014 {
3015         __sum16 sum;
3016
3017         sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
3018         /* See comments in __skb_checksum_complete(). */
3019         if (likely(!sum)) {
3020                 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
3021                     !skb->csum_complete_sw)
3022                         netdev_rx_csum_fault(skb->dev, skb);
3023         }
3024         if (!skb_shared(skb))
3025                 skb->csum_valid = !sum;
3026         return sum;
3027 }
3028 EXPORT_SYMBOL(__skb_checksum_complete_head);
3029
3030 /* This function assumes skb->csum already holds pseudo header's checksum,
3031  * which has been changed from the hardware checksum, for example, by
3032  * __skb_checksum_validate_complete(). And, the original skb->csum must
3033  * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
3034  *
3035  * It returns non-zero if the recomputed checksum is still invalid, otherwise
3036  * zero. The new checksum is stored back into skb->csum unless the skb is
3037  * shared.
3038  */
3039 __sum16 __skb_checksum_complete(struct sk_buff *skb)
3040 {
3041         __wsum csum;
3042         __sum16 sum;
3043
3044         csum = skb_checksum(skb, 0, skb->len, 0);
3045
3046         sum = csum_fold(csum_add(skb->csum, csum));
3047         /* This check is inverted, because we already knew the hardware
3048          * checksum is invalid before calling this function. So, if the
3049          * re-computed checksum is valid instead, then we have a mismatch
3050          * between the original skb->csum and skb_checksum(). This means either
3051          * the original hardware checksum is incorrect or we screw up skb->csum
3052          * when moving skb->data around.
3053          */
3054         if (likely(!sum)) {
3055                 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
3056                     !skb->csum_complete_sw)
3057                         netdev_rx_csum_fault(skb->dev, skb);
3058         }
3059
3060         if (!skb_shared(skb)) {
3061                 /* Save full packet checksum */
3062                 skb->csum = csum;
3063                 skb->ip_summed = CHECKSUM_COMPLETE;
3064                 skb->csum_complete_sw = 1;
3065                 skb->csum_valid = !sum;
3066         }
3067
3068         return sum;
3069 }
3070 EXPORT_SYMBOL(__skb_checksum_complete);
3071
3072 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
3073 {
3074         net_warn_ratelimited(
3075                 "%s: attempt to compute crc32c without libcrc32c.ko\n",
3076                 __func__);
3077         return 0;
3078 }
3079
3080 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
3081                                        int offset, int len)
3082 {
3083         net_warn_ratelimited(
3084                 "%s: attempt to compute crc32c without libcrc32c.ko\n",
3085                 __func__);
3086         return 0;
3087 }
3088
3089 static const struct skb_checksum_ops default_crc32c_ops = {
3090         .update  = warn_crc32c_csum_update,
3091         .combine = warn_crc32c_csum_combine,
3092 };
3093
3094 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
3095         &default_crc32c_ops;
3096 EXPORT_SYMBOL(crc32c_csum_stub);
3097
3098  /**
3099  *      skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
3100  *      @from: source buffer
3101  *
3102  *      Calculates the amount of linear headroom needed in the 'to' skb passed
3103  *      into skb_zerocopy().
3104  */
3105 unsigned int
3106 skb_zerocopy_headlen(const struct sk_buff *from)
3107 {
3108         unsigned int hlen = 0;
3109
3110         if (!from->head_frag ||
3111             skb_headlen(from) < L1_CACHE_BYTES ||
3112             skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
3113                 hlen = skb_headlen(from);
3114                 if (!hlen)
3115                         hlen = from->len;
3116         }
3117
3118         if (skb_has_frag_list(from))
3119                 hlen = from->len;
3120
3121         return hlen;
3122 }
3123 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
3124
3125 /**
3126  *      skb_zerocopy - Zero copy skb to skb
3127  *      @to: destination buffer
3128  *      @from: source buffer
3129  *      @len: number of bytes to copy from source buffer
3130  *      @hlen: size of linear headroom in destination buffer
3131  *
3132  *      Copies up to `len` bytes from `from` to `to` by creating references
3133  *      to the frags in the source buffer.
3134  *
3135  *      The `hlen` as calculated by skb_zerocopy_headlen() specifies the
3136  *      headroom in the `to` buffer.
3137  *
3138  *      Return value:
3139  *      0: everything is OK
3140  *      -ENOMEM: couldn't orphan frags of @from due to lack of memory
3141  *      -EFAULT: skb_copy_bits() found some problem with skb geometry
3142  */
3143 int
3144 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
3145 {
3146         int i, j = 0;
3147         int plen = 0; /* length of skb->head fragment */
3148         int ret;
3149         struct page *page;
3150         unsigned int offset;
3151
3152         BUG_ON(!from->head_frag && !hlen);
3153
3154         /* dont bother with small payloads */
3155         if (len <= skb_tailroom(to))
3156                 return skb_copy_bits(from, 0, skb_put(to, len), len);
3157
3158         if (hlen) {
3159                 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
3160                 if (unlikely(ret))
3161                         return ret;
3162                 len -= hlen;
3163         } else {
3164                 plen = min_t(int, skb_headlen(from), len);
3165                 if (plen) {
3166                         page = virt_to_head_page(from->head);
3167                         offset = from->data - (unsigned char *)page_address(page);
3168                         __skb_fill_page_desc(to, 0, page, offset, plen);
3169                         get_page(page);
3170                         j = 1;
3171                         len -= plen;
3172                 }
3173         }
3174
3175         to->truesize += len + plen;
3176         to->len += len + plen;
3177         to->data_len += len + plen;
3178
3179         if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
3180                 skb_tx_error(from);
3181                 return -ENOMEM;
3182         }
3183         skb_zerocopy_clone(to, from, GFP_ATOMIC);
3184
3185         for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
3186                 int size;
3187
3188                 if (!len)
3189                         break;
3190                 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
3191                 size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3192                                         len);
3193                 skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3194                 len -= size;
3195                 skb_frag_ref(to, j);
3196                 j++;
3197         }
3198         skb_shinfo(to)->nr_frags = j;
3199
3200         return 0;
3201 }
3202 EXPORT_SYMBOL_GPL(skb_zerocopy);
3203
3204 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
3205 {
3206         __wsum csum;
3207         long csstart;
3208
3209         if (skb->ip_summed == CHECKSUM_PARTIAL)
3210                 csstart = skb_checksum_start_offset(skb);
3211         else
3212                 csstart = skb_headlen(skb);
3213
3214         BUG_ON(csstart > skb_headlen(skb));
3215
3216         skb_copy_from_linear_data(skb, to, csstart);
3217
3218         csum = 0;
3219         if (csstart != skb->len)
3220                 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
3221                                               skb->len - csstart);
3222
3223         if (skb->ip_summed == CHECKSUM_PARTIAL) {
3224                 long csstuff = csstart + skb->csum_offset;
3225
3226                 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
3227         }
3228 }
3229 EXPORT_SYMBOL(skb_copy_and_csum_dev);
3230
3231 /**
3232  *      skb_dequeue - remove from the head of the queue
3233  *      @list: list to dequeue from
3234  *
3235  *      Remove the head of the list. The list lock is taken so the function
3236  *      may be used safely with other locking list functions. The head item is
3237  *      returned or %NULL if the list is empty.
3238  */
3239
3240 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3241 {
3242         unsigned long flags;
3243         struct sk_buff *result;
3244
3245         spin_lock_irqsave(&list->lock, flags);
3246         result = __skb_dequeue(list);
3247         spin_unlock_irqrestore(&list->lock, flags);
3248         return result;
3249 }
3250 EXPORT_SYMBOL(skb_dequeue);
3251
3252 /**
3253  *      skb_dequeue_tail - remove from the tail of the queue
3254  *      @list: list to dequeue from
3255  *
3256  *      Remove the tail of the list. The list lock is taken so the function
3257  *      may be used safely with other locking list functions. The tail item is
3258  *      returned or %NULL if the list is empty.
3259  */
3260 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3261 {
3262         unsigned long flags;
3263         struct sk_buff *result;
3264
3265         spin_lock_irqsave(&list->lock, flags);
3266         result = __skb_dequeue_tail(list);
3267         spin_unlock_irqrestore(&list->lock, flags);
3268         return result;
3269 }
3270 EXPORT_SYMBOL(skb_dequeue_tail);
3271
3272 /**
3273  *      skb_queue_purge - empty a list
3274  *      @list: list to empty
3275  *
3276  *      Delete all buffers on an &sk_buff list. Each buffer is removed from
3277  *      the list and one reference dropped. This function takes the list
3278  *      lock and is atomic with respect to other list locking functions.
3279  */
3280 void skb_queue_purge(struct sk_buff_head *list)
3281 {
3282         struct sk_buff *skb;
3283         while ((skb = skb_dequeue(list)) != NULL)
3284                 kfree_skb(skb);
3285 }
3286 EXPORT_SYMBOL(skb_queue_purge);
3287
3288 /**
3289  *      skb_rbtree_purge - empty a skb rbtree
3290  *      @root: root of the rbtree to empty
3291  *      Return value: the sum of truesizes of all purged skbs.
3292  *
3293  *      Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3294  *      the list and one reference dropped. This function does not take
3295  *      any lock. Synchronization should be handled by the caller (e.g., TCP
3296  *      out-of-order queue is protected by the socket lock).
3297  */
3298 unsigned int skb_rbtree_purge(struct rb_root *root)
3299 {
3300         struct rb_node *p = rb_first(root);
3301         unsigned int sum = 0;
3302
3303         while (p) {
3304                 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3305
3306                 p = rb_next(p);
3307                 rb_erase(&skb->rbnode, root);
3308                 sum += skb->truesize;
3309                 kfree_skb(skb);
3310         }
3311         return sum;
3312 }
3313
3314 /**
3315  *      skb_queue_head - queue a buffer at the list head
3316  *      @list: list to use
3317  *      @newsk: buffer to queue
3318  *
3319  *      Queue a buffer at the start of the list. This function takes the
3320  *      list lock and can be used safely with other locking &sk_buff functions
3321  *      safely.
3322  *
3323  *      A buffer cannot be placed on two lists at the same time.
3324  */
3325 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3326 {
3327         unsigned long flags;
3328
3329         spin_lock_irqsave(&list->lock, flags);
3330         __skb_queue_head(list, newsk);
3331         spin_unlock_irqrestore(&list->lock, flags);
3332 }
3333 EXPORT_SYMBOL(skb_queue_head);
3334
3335 /**
3336  *      skb_queue_tail - queue a buffer at the list tail
3337  *      @list: list to use
3338  *      @newsk: buffer to queue
3339  *
3340  *      Queue a buffer at the tail of the list. This function takes the
3341  *      list lock and can be used safely with other locking &sk_buff functions
3342  *      safely.
3343  *
3344  *      A buffer cannot be placed on two lists at the same time.
3345  */
3346 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3347 {
3348         unsigned long flags;
3349
3350         spin_lock_irqsave(&list->lock, flags);
3351         __skb_queue_tail(list, newsk);
3352         spin_unlock_irqrestore(&list->lock, flags);
3353 }
3354 EXPORT_SYMBOL(skb_queue_tail);
3355
3356 /**
3357  *      skb_unlink      -       remove a buffer from a list
3358  *      @skb: buffer to remove
3359  *      @list: list to use
3360  *
3361  *      Remove a packet from a list. The list locks are taken and this
3362  *      function is atomic with respect to other list locked calls
3363  *
3364  *      You must know what list the SKB is on.
3365  */
3366 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
3367 {
3368         unsigned long flags;
3369
3370         spin_lock_irqsave(&list->lock, flags);
3371         __skb_unlink(skb, list);
3372         spin_unlock_irqrestore(&list->lock, flags);
3373 }
3374 EXPORT_SYMBOL(skb_unlink);
3375
3376 /**
3377  *      skb_append      -       append a buffer
3378  *      @old: buffer to insert after
3379  *      @newsk: buffer to insert
3380  *      @list: list to use
3381  *
3382  *      Place a packet after a given packet in a list. The list locks are taken
3383  *      and this function is atomic with respect to other list locked calls.
3384  *      A buffer cannot be placed on two lists at the same time.
3385  */
3386 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
3387 {
3388         unsigned long flags;
3389
3390         spin_lock_irqsave(&list->lock, flags);
3391         __skb_queue_after(list, old, newsk);
3392         spin_unlock_irqrestore(&list->lock, flags);
3393 }
3394 EXPORT_SYMBOL(skb_append);
3395
3396 static inline void skb_split_inside_header(struct sk_buff *skb,
3397                                            struct sk_buff* skb1,
3398                                            const u32 len, const int pos)
3399 {
3400         int i;
3401
3402         skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3403                                          pos - len);
3404         /* And move data appendix as is. */
3405         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3406                 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3407
3408         skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3409         skb_shinfo(skb)->nr_frags  = 0;
3410         skb1->data_len             = skb->data_len;
3411         skb1->len                  += skb1->data_len;
3412         skb->data_len              = 0;
3413         skb->len                   = len;
3414         skb_set_tail_pointer(skb, len);
3415 }
3416
3417 static inline void skb_split_no_header(struct sk_buff *skb,
3418                                        struct sk_buff* skb1,
3419                                        const u32 len, int pos)
3420 {
3421         int i, k = 0;
3422         const int nfrags = skb_shinfo(skb)->nr_frags;
3423
3424         skb_shinfo(skb)->nr_frags = 0;
3425         skb1->len                 = skb1->data_len = skb->len - len;
3426         skb->len                  = len;
3427         skb->data_len             = len - pos;
3428
3429         for (i = 0; i < nfrags; i++) {
3430                 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
3431
3432                 if (pos + size > len) {
3433                         skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3434
3435                         if (pos < len) {
3436                                 /* Split frag.
3437                                  * We have two variants in this case:
3438                                  * 1. Move all the frag to the second
3439                                  *    part, if it is possible. F.e.
3440                                  *    this approach is mandatory for TUX,
3441                                  *    where splitting is expensive.
3442                                  * 2. Split is accurately. We make this.
3443                                  */
3444                                 skb_frag_ref(skb, i);
3445                                 skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
3446                                 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3447                                 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
3448                                 skb_shinfo(skb)->nr_frags++;
3449                         }
3450                         k++;
3451                 } else
3452                         skb_shinfo(skb)->nr_frags++;
3453                 pos += size;
3454         }
3455         skb_shinfo(skb1)->nr_frags = k;
3456 }
3457
3458 /**
3459  * skb_split - Split fragmented skb to two parts at length len.
3460  * @skb: the buffer to split
3461  * @skb1: the buffer to receive the second part
3462  * @len: new length for skb
3463  */
3464 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3465 {
3466         int pos = skb_headlen(skb);
3467
3468         skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
3469         skb_zerocopy_clone(skb1, skb, 0);
3470         if (len < pos)  /* Split line is inside header. */
3471                 skb_split_inside_header(skb, skb1, len, pos);
3472         else            /* Second chunk has no header, nothing to copy. */
3473                 skb_split_no_header(skb, skb1, len, pos);
3474 }
3475 EXPORT_SYMBOL(skb_split);
3476
3477 /* Shifting from/to a cloned skb is a no-go.
3478  *
3479  * Caller cannot keep skb_shinfo related pointers past calling here!
3480  */
3481 static int skb_prepare_for_shift(struct sk_buff *skb)
3482 {
3483         return skb_unclone_keeptruesize(skb, GFP_ATOMIC);
3484 }
3485
3486 /**
3487  * skb_shift - Shifts paged data partially from skb to another
3488  * @tgt: buffer into which tail data gets added
3489  * @skb: buffer from which the paged data comes from
3490  * @shiftlen: shift up to this many bytes
3491  *
3492  * Attempts to shift up to shiftlen worth of bytes, which may be less than
3493  * the length of the skb, from skb to tgt. Returns number bytes shifted.
3494  * It's up to caller to free skb if everything was shifted.
3495  *
3496  * If @tgt runs out of frags, the whole operation is aborted.
3497  *
3498  * Skb cannot include anything else but paged data while tgt is allowed
3499  * to have non-paged data as well.
3500  *
3501  * TODO: full sized shift could be optimized but that would need
3502  * specialized skb free'er to handle frags without up-to-date nr_frags.
3503  */
3504 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3505 {
3506         int from, to, merge, todo;
3507         skb_frag_t *fragfrom, *fragto;
3508
3509         BUG_ON(shiftlen > skb->len);
3510
3511         if (skb_headlen(skb))
3512                 return 0;
3513         if (skb_zcopy(tgt) || skb_zcopy(skb))
3514                 return 0;
3515
3516         todo = shiftlen;
3517         from = 0;
3518         to = skb_shinfo(tgt)->nr_frags;
3519         fragfrom = &skb_shinfo(skb)->frags[from];
3520
3521         /* Actual merge is delayed until the point when we know we can
3522          * commit all, so that we don't have to undo partial changes
3523          */
3524         if (!to ||
3525             !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3526                               skb_frag_off(fragfrom))) {
3527                 merge = -1;
3528         } else {
3529                 merge = to - 1;
3530
3531                 todo -= skb_frag_size(fragfrom);
3532                 if (todo < 0) {
3533                         if (skb_prepare_for_shift(skb) ||
3534                             skb_prepare_for_shift(tgt))
3535                                 return 0;
3536
3537                         /* All previous frag pointers might be stale! */
3538                         fragfrom = &skb_shinfo(skb)->frags[from];
3539                         fragto = &skb_shinfo(tgt)->frags[merge];
3540
3541                         skb_frag_size_add(fragto, shiftlen);
3542                         skb_frag_size_sub(fragfrom, shiftlen);
3543                         skb_frag_off_add(fragfrom, shiftlen);
3544
3545                         goto onlymerged;
3546                 }
3547
3548                 from++;
3549         }
3550
3551         /* Skip full, not-fitting skb to avoid expensive operations */
3552         if ((shiftlen == skb->len) &&
3553             (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3554                 return 0;
3555
3556         if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3557                 return 0;
3558
3559         while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3560                 if (to == MAX_SKB_FRAGS)
3561                         return 0;
3562
3563                 fragfrom = &skb_shinfo(skb)->frags[from];
3564                 fragto = &skb_shinfo(tgt)->frags[to];
3565
3566                 if (todo >= skb_frag_size(fragfrom)) {
3567                         *fragto = *fragfrom;
3568                         todo -= skb_frag_size(fragfrom);
3569                         from++;
3570                         to++;
3571
3572                 } else {
3573                         __skb_frag_ref(fragfrom);
3574                         skb_frag_page_copy(fragto, fragfrom);
3575                         skb_frag_off_copy(fragto, fragfrom);
3576                         skb_frag_size_set(fragto, todo);
3577
3578                         skb_frag_off_add(fragfrom, todo);
3579                         skb_frag_size_sub(fragfrom, todo);
3580                         todo = 0;
3581
3582                         to++;
3583                         break;
3584                 }
3585         }
3586
3587         /* Ready to "commit" this state change to tgt */
3588         skb_shinfo(tgt)->nr_frags = to;
3589
3590         if (merge >= 0) {
3591                 fragfrom = &skb_shinfo(skb)->frags[0];
3592                 fragto = &skb_shinfo(tgt)->frags[merge];
3593
3594                 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3595                 __skb_frag_unref(fragfrom, skb->pp_recycle);
3596         }
3597
3598         /* Reposition in the original skb */
3599         to = 0;
3600         while (from < skb_shinfo(skb)->nr_frags)
3601                 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3602         skb_shinfo(skb)->nr_frags = to;
3603
3604         BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3605
3606 onlymerged:
3607         /* Most likely the tgt won't ever need its checksum anymore, skb on
3608          * the other hand might need it if it needs to be resent
3609          */
3610         tgt->ip_summed = CHECKSUM_PARTIAL;
3611         skb->ip_summed = CHECKSUM_PARTIAL;
3612
3613         /* Yak, is it really working this way? Some helper please? */
3614         skb->len -= shiftlen;
3615         skb->data_len -= shiftlen;
3616         skb->truesize -= shiftlen;
3617         tgt->len += shiftlen;
3618         tgt->data_len += shiftlen;
3619         tgt->truesize += shiftlen;
3620
3621         return shiftlen;
3622 }
3623
3624 /**
3625  * skb_prepare_seq_read - Prepare a sequential read of skb data
3626  * @skb: the buffer to read
3627  * @from: lower offset of data to be read
3628  * @to: upper offset of data to be read
3629  * @st: state variable
3630  *
3631  * Initializes the specified state variable. Must be called before
3632  * invoking skb_seq_read() for the first time.
3633  */
3634 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3635                           unsigned int to, struct skb_seq_state *st)
3636 {
3637         st->lower_offset = from;
3638         st->upper_offset = to;
3639         st->root_skb = st->cur_skb = skb;
3640         st->frag_idx = st->stepped_offset = 0;
3641         st->frag_data = NULL;
3642         st->frag_off = 0;
3643 }
3644 EXPORT_SYMBOL(skb_prepare_seq_read);
3645
3646 /**
3647  * skb_seq_read - Sequentially read skb data
3648  * @consumed: number of bytes consumed by the caller so far
3649  * @data: destination pointer for data to be returned
3650  * @st: state variable
3651  *
3652  * Reads a block of skb data at @consumed relative to the
3653  * lower offset specified to skb_prepare_seq_read(). Assigns
3654  * the head of the data block to @data and returns the length
3655  * of the block or 0 if the end of the skb data or the upper
3656  * offset has been reached.
3657  *
3658  * The caller is not required to consume all of the data
3659  * returned, i.e. @consumed is typically set to the number
3660  * of bytes already consumed and the next call to
3661  * skb_seq_read() will return the remaining part of the block.
3662  *
3663  * Note 1: The size of each block of data returned can be arbitrary,
3664  *       this limitation is the cost for zerocopy sequential
3665  *       reads of potentially non linear data.
3666  *
3667  * Note 2: Fragment lists within fragments are not implemented
3668  *       at the moment, state->root_skb could be replaced with
3669  *       a stack for this purpose.
3670  */
3671 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3672                           struct skb_seq_state *st)
3673 {
3674         unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3675         skb_frag_t *frag;
3676
3677         if (unlikely(abs_offset >= st->upper_offset)) {
3678                 if (st->frag_data) {
3679                         kunmap_atomic(st->frag_data);
3680                         st->frag_data = NULL;
3681                 }
3682                 return 0;
3683         }
3684
3685 next_skb:
3686         block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3687
3688         if (abs_offset < block_limit && !st->frag_data) {
3689                 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3690                 return block_limit - abs_offset;
3691         }
3692
3693         if (st->frag_idx == 0 && !st->frag_data)
3694                 st->stepped_offset += skb_headlen(st->cur_skb);
3695
3696         while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3697                 unsigned int pg_idx, pg_off, pg_sz;
3698
3699                 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3700
3701                 pg_idx = 0;
3702                 pg_off = skb_frag_off(frag);
3703                 pg_sz = skb_frag_size(frag);
3704
3705                 if (skb_frag_must_loop(skb_frag_page(frag))) {
3706                         pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
3707                         pg_off = offset_in_page(pg_off + st->frag_off);
3708                         pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
3709                                                     PAGE_SIZE - pg_off);
3710                 }
3711
3712                 block_limit = pg_sz + st->stepped_offset;
3713                 if (abs_offset < block_limit) {
3714                         if (!st->frag_data)
3715                                 st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
3716
3717                         *data = (u8 *)st->frag_data + pg_off +
3718                                 (abs_offset - st->stepped_offset);
3719
3720                         return block_limit - abs_offset;
3721                 }
3722
3723                 if (st->frag_data) {
3724                         kunmap_atomic(st->frag_data);
3725                         st->frag_data = NULL;
3726                 }
3727
3728                 st->stepped_offset += pg_sz;
3729                 st->frag_off += pg_sz;
3730                 if (st->frag_off == skb_frag_size(frag)) {
3731                         st->frag_off = 0;
3732                         st->frag_idx++;
3733                 }
3734         }
3735
3736         if (st->frag_data) {
3737                 kunmap_atomic(st->frag_data);
3738                 st->frag_data = NULL;
3739         }
3740
3741         if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3742                 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
3743                 st->frag_idx = 0;
3744                 goto next_skb;
3745         } else if (st->cur_skb->next) {
3746                 st->cur_skb = st->cur_skb->next;
3747                 st->frag_idx = 0;
3748                 goto next_skb;
3749         }
3750
3751         return 0;
3752 }
3753 EXPORT_SYMBOL(skb_seq_read);
3754
3755 /**
3756  * skb_abort_seq_read - Abort a sequential read of skb data
3757  * @st: state variable
3758  *
3759  * Must be called if skb_seq_read() was not called until it
3760  * returned 0.
3761  */
3762 void skb_abort_seq_read(struct skb_seq_state *st)
3763 {
3764         if (st->frag_data)
3765                 kunmap_atomic(st->frag_data);
3766 }
3767 EXPORT_SYMBOL(skb_abort_seq_read);
3768
3769 #define TS_SKB_CB(state)        ((struct skb_seq_state *) &((state)->cb))
3770
3771 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3772                                           struct ts_config *conf,
3773                                           struct ts_state *state)
3774 {
3775         return skb_seq_read(offset, text, TS_SKB_CB(state));
3776 }
3777
3778 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3779 {
3780         skb_abort_seq_read(TS_SKB_CB(state));
3781 }
3782
3783 /**
3784  * skb_find_text - Find a text pattern in skb data
3785  * @skb: the buffer to look in
3786  * @from: search offset
3787  * @to: search limit
3788  * @config: textsearch configuration
3789  *
3790  * Finds a pattern in the skb data according to the specified
3791  * textsearch configuration. Use textsearch_next() to retrieve
3792  * subsequent occurrences of the pattern. Returns the offset
3793  * to the first occurrence or UINT_MAX if no match was found.
3794  */
3795 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3796                            unsigned int to, struct ts_config *config)
3797 {
3798         struct ts_state state;
3799         unsigned int ret;
3800
3801         BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb));
3802
3803         config->get_next_block = skb_ts_get_next_block;
3804         config->finish = skb_ts_finish;
3805
3806         skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
3807
3808         ret = textsearch_find(config, &state);
3809         return (ret <= to - from ? ret : UINT_MAX);
3810 }
3811 EXPORT_SYMBOL(skb_find_text);
3812
3813 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3814                          int offset, size_t size)
3815 {
3816         int i = skb_shinfo(skb)->nr_frags;
3817
3818         if (skb_can_coalesce(skb, i, page, offset)) {
3819                 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3820         } else if (i < MAX_SKB_FRAGS) {
3821                 get_page(page);
3822                 skb_fill_page_desc(skb, i, page, offset, size);
3823         } else {
3824                 return -EMSGSIZE;
3825         }
3826
3827         return 0;
3828 }
3829 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3830
3831 /**
3832  *      skb_pull_rcsum - pull skb and update receive checksum
3833  *      @skb: buffer to update
3834  *      @len: length of data pulled
3835  *
3836  *      This function performs an skb_pull on the packet and updates
3837  *      the CHECKSUM_COMPLETE checksum.  It should be used on
3838  *      receive path processing instead of skb_pull unless you know
3839  *      that the checksum difference is zero (e.g., a valid IP header)
3840  *      or you are setting ip_summed to CHECKSUM_NONE.
3841  */
3842 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3843 {
3844         unsigned char *data = skb->data;
3845
3846         BUG_ON(len > skb->len);
3847         __skb_pull(skb, len);
3848         skb_postpull_rcsum(skb, data, len);
3849         return skb->data;
3850 }
3851 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3852
3853 static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3854 {
3855         skb_frag_t head_frag;
3856         struct page *page;
3857
3858         page = virt_to_head_page(frag_skb->head);
3859         __skb_frag_set_page(&head_frag, page);
3860         skb_frag_off_set(&head_frag, frag_skb->data -
3861                          (unsigned char *)page_address(page));
3862         skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
3863         return head_frag;
3864 }
3865
3866 struct sk_buff *skb_segment_list(struct sk_buff *skb,
3867                                  netdev_features_t features,
3868                                  unsigned int offset)
3869 {
3870         struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
3871         unsigned int tnl_hlen = skb_tnl_header_len(skb);
3872         unsigned int delta_truesize = 0;
3873         unsigned int delta_len = 0;
3874         struct sk_buff *tail = NULL;
3875         struct sk_buff *nskb, *tmp;
3876         int err;
3877
3878         skb_push(skb, -skb_network_offset(skb) + offset);
3879
3880         skb_shinfo(skb)->frag_list = NULL;
3881
3882         do {
3883                 nskb = list_skb;
3884                 list_skb = list_skb->next;
3885
3886                 err = 0;
3887                 delta_truesize += nskb->truesize;
3888                 if (skb_shared(nskb)) {
3889                         tmp = skb_clone(nskb, GFP_ATOMIC);
3890                         if (tmp) {
3891                                 consume_skb(nskb);
3892                                 nskb = tmp;
3893                                 err = skb_unclone(nskb, GFP_ATOMIC);
3894                         } else {
3895                                 err = -ENOMEM;
3896                         }
3897                 }
3898
3899                 if (!tail)
3900                         skb->next = nskb;
3901                 else
3902                         tail->next = nskb;
3903
3904                 if (unlikely(err)) {
3905                         nskb->next = list_skb;
3906                         goto err_linearize;
3907                 }
3908
3909                 tail = nskb;
3910
3911                 delta_len += nskb->len;
3912
3913                 skb_push(nskb, -skb_network_offset(nskb) + offset);
3914
3915                 skb_release_head_state(nskb);
3916                 __copy_skb_header(nskb, skb);
3917
3918                 skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
3919                 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
3920                                                  nskb->data - tnl_hlen,
3921                                                  offset + tnl_hlen);
3922
3923                 if (skb_needs_linearize(nskb, features) &&
3924                     __skb_linearize(nskb))
3925                         goto err_linearize;
3926
3927         } while (list_skb);
3928
3929         skb->truesize = skb->truesize - delta_truesize;
3930         skb->data_len = skb->data_len - delta_len;
3931         skb->len = skb->len - delta_len;
3932
3933         skb_gso_reset(skb);
3934
3935         skb->prev = tail;
3936
3937         if (skb_needs_linearize(skb, features) &&
3938             __skb_linearize(skb))
3939                 goto err_linearize;
3940
3941         skb_get(skb);
3942
3943         return skb;
3944
3945 err_linearize:
3946         kfree_skb_list(skb->next);
3947         skb->next = NULL;
3948         return ERR_PTR(-ENOMEM);
3949 }
3950 EXPORT_SYMBOL_GPL(skb_segment_list);
3951
3952 int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
3953 {
3954         if (unlikely(p->len + skb->len >= 65536))
3955                 return -E2BIG;
3956
3957         if (NAPI_GRO_CB(p)->last == p)
3958                 skb_shinfo(p)->frag_list = skb;
3959         else
3960                 NAPI_GRO_CB(p)->last->next = skb;
3961
3962         skb_pull(skb, skb_gro_offset(skb));
3963
3964         NAPI_GRO_CB(p)->last = skb;
3965         NAPI_GRO_CB(p)->count++;
3966         p->data_len += skb->len;
3967
3968         /* sk owenrship - if any - completely transferred to the aggregated packet */
3969         skb->destructor = NULL;
3970         p->truesize += skb->truesize;
3971         p->len += skb->len;
3972
3973         NAPI_GRO_CB(skb)->same_flow = 1;
3974
3975         return 0;
3976 }
3977
3978 /**
3979  *      skb_segment - Perform protocol segmentation on skb.
3980  *      @head_skb: buffer to segment
3981  *      @features: features for the output path (see dev->features)
3982  *
3983  *      This function performs segmentation on the given skb.  It returns
3984  *      a pointer to the first in a list of new skbs for the segments.
3985  *      In case of error it returns ERR_PTR(err).
3986  */
3987 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3988                             netdev_features_t features)
3989 {
3990         struct sk_buff *segs = NULL;
3991         struct sk_buff *tail = NULL;
3992         struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3993         skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3994         unsigned int mss = skb_shinfo(head_skb)->gso_size;
3995         unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3996         struct sk_buff *frag_skb = head_skb;
3997         unsigned int offset = doffset;
3998         unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3999         unsigned int partial_segs = 0;
4000         unsigned int headroom;
4001         unsigned int len = head_skb->len;
4002         __be16 proto;
4003         bool csum, sg;
4004         int nfrags = skb_shinfo(head_skb)->nr_frags;
4005         int err = -ENOMEM;
4006         int i = 0;
4007         int pos;
4008
4009         if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
4010             (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
4011                 /* gso_size is untrusted, and we have a frag_list with a linear
4012                  * non head_frag head.
4013                  *
4014                  * (we assume checking the first list_skb member suffices;
4015                  * i.e if either of the list_skb members have non head_frag
4016                  * head, then the first one has too).
4017                  *
4018                  * If head_skb's headlen does not fit requested gso_size, it
4019                  * means that the frag_list members do NOT terminate on exact
4020                  * gso_size boundaries. Hence we cannot perform skb_frag_t page
4021                  * sharing. Therefore we must fallback to copying the frag_list
4022                  * skbs; we do so by disabling SG.
4023                  */
4024                 if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
4025                         features &= ~NETIF_F_SG;
4026         }
4027
4028         __skb_push(head_skb, doffset);
4029         proto = skb_network_protocol(head_skb, NULL);
4030         if (unlikely(!proto))
4031                 return ERR_PTR(-EINVAL);
4032
4033         sg = !!(features & NETIF_F_SG);
4034         csum = !!can_checksum_protocol(features, proto);
4035
4036         if (sg && csum && (mss != GSO_BY_FRAGS))  {
4037                 if (!(features & NETIF_F_GSO_PARTIAL)) {
4038                         struct sk_buff *iter;
4039                         unsigned int frag_len;
4040
4041                         if (!list_skb ||
4042                             !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
4043                                 goto normal;
4044
4045                         /* If we get here then all the required
4046                          * GSO features except frag_list are supported.
4047                          * Try to split the SKB to multiple GSO SKBs
4048                          * with no frag_list.
4049                          * Currently we can do that only when the buffers don't
4050                          * have a linear part and all the buffers except
4051                          * the last are of the same length.
4052                          */
4053                         frag_len = list_skb->len;
4054                         skb_walk_frags(head_skb, iter) {
4055                                 if (frag_len != iter->len && iter->next)
4056                                         goto normal;
4057                                 if (skb_headlen(iter) && !iter->head_frag)
4058                                         goto normal;
4059
4060                                 len -= iter->len;
4061                         }
4062
4063                         if (len != frag_len)
4064                                 goto normal;
4065                 }
4066
4067                 /* GSO partial only requires that we trim off any excess that
4068                  * doesn't fit into an MSS sized block, so take care of that
4069                  * now.
4070                  */
4071                 partial_segs = len / mss;
4072                 if (partial_segs > 1)
4073                         mss *= partial_segs;
4074                 else
4075                         partial_segs = 0;
4076         }
4077
4078 normal:
4079         headroom = skb_headroom(head_skb);
4080         pos = skb_headlen(head_skb);
4081
4082         do {
4083                 struct sk_buff *nskb;
4084                 skb_frag_t *nskb_frag;
4085                 int hsize;
4086                 int size;
4087
4088                 if (unlikely(mss == GSO_BY_FRAGS)) {
4089                         len = list_skb->len;
4090                 } else {
4091                         len = head_skb->len - offset;
4092                         if (len > mss)
4093                                 len = mss;
4094                 }
4095
4096                 hsize = skb_headlen(head_skb) - offset;
4097
4098                 if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
4099                     (skb_headlen(list_skb) == len || sg)) {
4100                         BUG_ON(skb_headlen(list_skb) > len);
4101
4102                         i = 0;
4103                         nfrags = skb_shinfo(list_skb)->nr_frags;
4104                         frag = skb_shinfo(list_skb)->frags;
4105                         frag_skb = list_skb;
4106                         pos += skb_headlen(list_skb);
4107
4108                         while (pos < offset + len) {
4109                                 BUG_ON(i >= nfrags);
4110
4111                                 size = skb_frag_size(frag);
4112                                 if (pos + size > offset + len)
4113                                         break;
4114
4115                                 i++;
4116                                 pos += size;
4117                                 frag++;
4118                         }
4119
4120                         nskb = skb_clone(list_skb, GFP_ATOMIC);
4121                         list_skb = list_skb->next;
4122
4123                         if (unlikely(!nskb))
4124                                 goto err;
4125
4126                         if (unlikely(pskb_trim(nskb, len))) {
4127                                 kfree_skb(nskb);
4128                                 goto err;
4129                         }
4130
4131                         hsize = skb_end_offset(nskb);
4132                         if (skb_cow_head(nskb, doffset + headroom)) {
4133                                 kfree_skb(nskb);
4134                                 goto err;
4135                         }
4136
4137                         nskb->truesize += skb_end_offset(nskb) - hsize;
4138                         skb_release_head_state(nskb);
4139                         __skb_push(nskb, doffset);
4140                 } else {
4141                         if (hsize < 0)
4142                                 hsize = 0;
4143                         if (hsize > len || !sg)
4144                                 hsize = len;
4145
4146                         nskb = __alloc_skb(hsize + doffset + headroom,
4147                                            GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
4148                                            NUMA_NO_NODE);
4149
4150                         if (unlikely(!nskb))
4151                                 goto err;
4152
4153                         skb_reserve(nskb, headroom);
4154                         __skb_put(nskb, doffset);
4155                 }
4156
4157                 if (segs)
4158                         tail->next = nskb;
4159                 else
4160                         segs = nskb;
4161                 tail = nskb;
4162
4163                 __copy_skb_header(nskb, head_skb);
4164
4165                 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
4166                 skb_reset_mac_len(nskb);
4167
4168                 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
4169                                                  nskb->data - tnl_hlen,
4170                                                  doffset + tnl_hlen);
4171
4172                 if (nskb->len == len + doffset)
4173                         goto perform_csum_check;
4174
4175                 if (!sg) {
4176                         if (!csum) {
4177                                 if (!nskb->remcsum_offload)
4178                                         nskb->ip_summed = CHECKSUM_NONE;
4179                                 SKB_GSO_CB(nskb)->csum =
4180                                         skb_copy_and_csum_bits(head_skb, offset,
4181                                                                skb_put(nskb,
4182                                                                        len),
4183                                                                len);
4184                                 SKB_GSO_CB(nskb)->csum_start =
4185                                         skb_headroom(nskb) + doffset;
4186                         } else {
4187                                 skb_copy_bits(head_skb, offset,
4188                                               skb_put(nskb, len),
4189                                               len);
4190                         }
4191                         continue;
4192                 }
4193
4194                 nskb_frag = skb_shinfo(nskb)->frags;
4195
4196                 skb_copy_from_linear_data_offset(head_skb, offset,
4197                                                  skb_put(nskb, hsize), hsize);
4198
4199                 skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
4200                                            SKBFL_SHARED_FRAG;
4201
4202                 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4203                     skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4204                         goto err;
4205
4206                 while (pos < offset + len) {
4207                         if (i >= nfrags) {
4208                                 i = 0;
4209                                 nfrags = skb_shinfo(list_skb)->nr_frags;
4210                                 frag = skb_shinfo(list_skb)->frags;
4211                                 frag_skb = list_skb;
4212                                 if (!skb_headlen(list_skb)) {
4213                                         BUG_ON(!nfrags);
4214                                 } else {
4215                                         BUG_ON(!list_skb->head_frag);
4216
4217                                         /* to make room for head_frag. */
4218                                         i--;
4219                                         frag--;
4220                                 }
4221                                 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4222                                     skb_zerocopy_clone(nskb, frag_skb,
4223                                                        GFP_ATOMIC))
4224                                         goto err;
4225
4226                                 list_skb = list_skb->next;
4227                         }
4228
4229                         if (unlikely(skb_shinfo(nskb)->nr_frags >=
4230                                      MAX_SKB_FRAGS)) {
4231                                 net_warn_ratelimited(
4232                                         "skb_segment: too many frags: %u %u\n",
4233                                         pos, mss);
4234                                 err = -EINVAL;
4235                                 goto err;
4236                         }
4237
4238                         *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
4239                         __skb_frag_ref(nskb_frag);
4240                         size = skb_frag_size(nskb_frag);
4241
4242                         if (pos < offset) {
4243                                 skb_frag_off_add(nskb_frag, offset - pos);
4244                                 skb_frag_size_sub(nskb_frag, offset - pos);
4245                         }
4246
4247                         skb_shinfo(nskb)->nr_frags++;
4248
4249                         if (pos + size <= offset + len) {
4250                                 i++;
4251                                 frag++;
4252                                 pos += size;
4253                         } else {
4254                                 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
4255                                 goto skip_fraglist;
4256                         }
4257
4258                         nskb_frag++;
4259                 }
4260
4261 skip_fraglist:
4262                 nskb->data_len = len - hsize;
4263                 nskb->len += nskb->data_len;
4264                 nskb->truesize += nskb->data_len;
4265
4266 perform_csum_check:
4267                 if (!csum) {
4268                         if (skb_has_shared_frag(nskb) &&
4269                             __skb_linearize(nskb))
4270                                 goto err;
4271
4272                         if (!nskb->remcsum_offload)
4273                                 nskb->ip_summed = CHECKSUM_NONE;
4274                         SKB_GSO_CB(nskb)->csum =
4275                                 skb_checksum(nskb, doffset,
4276                                              nskb->len - doffset, 0);
4277                         SKB_GSO_CB(nskb)->csum_start =
4278                                 skb_headroom(nskb) + doffset;
4279                 }
4280         } while ((offset += len) < head_skb->len);
4281
4282         /* Some callers want to get the end of the list.
4283          * Put it in segs->prev to avoid walking the list.
4284          * (see validate_xmit_skb_list() for example)
4285          */
4286         segs->prev = tail;
4287
4288         if (partial_segs) {
4289                 struct sk_buff *iter;
4290                 int type = skb_shinfo(head_skb)->gso_type;
4291                 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
4292
4293                 /* Update type to add partial and then remove dodgy if set */
4294                 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
4295                 type &= ~SKB_GSO_DODGY;
4296
4297                 /* Update GSO info and prepare to start updating headers on
4298                  * our way back down the stack of protocols.
4299                  */
4300                 for (iter = segs; iter; iter = iter->next) {
4301                         skb_shinfo(iter)->gso_size = gso_size;
4302                         skb_shinfo(iter)->gso_segs = partial_segs;
4303                         skb_shinfo(iter)->gso_type = type;
4304                         SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
4305                 }
4306
4307                 if (tail->len - doffset <= gso_size)
4308                         skb_shinfo(tail)->gso_size = 0;
4309                 else if (tail != segs)
4310                         skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
4311         }
4312
4313         /* Following permits correct backpressure, for protocols
4314          * using skb_set_owner_w().
4315          * Idea is to tranfert ownership from head_skb to last segment.
4316          */
4317         if (head_skb->destructor == sock_wfree) {
4318                 swap(tail->truesize, head_skb->truesize);
4319                 swap(tail->destructor, head_skb->destructor);
4320                 swap(tail->sk, head_skb->sk);
4321         }
4322         return segs;
4323
4324 err:
4325         kfree_skb_list(segs);
4326         return ERR_PTR(err);
4327 }
4328 EXPORT_SYMBOL_GPL(skb_segment);
4329
4330 int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
4331 {
4332         struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
4333         unsigned int offset = skb_gro_offset(skb);
4334         unsigned int headlen = skb_headlen(skb);
4335         unsigned int len = skb_gro_len(skb);
4336         unsigned int delta_truesize;
4337         unsigned int new_truesize;
4338         struct sk_buff *lp;
4339
4340         if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
4341                 return -E2BIG;
4342
4343         lp = NAPI_GRO_CB(p)->last;
4344         pinfo = skb_shinfo(lp);
4345
4346         if (headlen <= offset) {
4347                 skb_frag_t *frag;
4348                 skb_frag_t *frag2;
4349                 int i = skbinfo->nr_frags;
4350                 int nr_frags = pinfo->nr_frags + i;
4351
4352                 if (nr_frags > MAX_SKB_FRAGS)
4353                         goto merge;
4354
4355                 offset -= headlen;
4356                 pinfo->nr_frags = nr_frags;
4357                 skbinfo->nr_frags = 0;
4358
4359                 frag = pinfo->frags + nr_frags;
4360                 frag2 = skbinfo->frags + i;
4361                 do {
4362                         *--frag = *--frag2;
4363                 } while (--i);
4364
4365                 skb_frag_off_add(frag, offset);
4366                 skb_frag_size_sub(frag, offset);
4367
4368                 /* all fragments truesize : remove (head size + sk_buff) */
4369                 new_truesize = SKB_TRUESIZE(skb_end_offset(skb));
4370                 delta_truesize = skb->truesize - new_truesize;
4371
4372                 skb->truesize = new_truesize;
4373                 skb->len -= skb->data_len;
4374                 skb->data_len = 0;
4375
4376                 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
4377                 goto done;
4378         } else if (skb->head_frag) {
4379                 int nr_frags = pinfo->nr_frags;
4380                 skb_frag_t *frag = pinfo->frags + nr_frags;
4381                 struct page *page = virt_to_head_page(skb->head);
4382                 unsigned int first_size = headlen - offset;
4383                 unsigned int first_offset;
4384
4385                 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
4386                         goto merge;
4387
4388                 first_offset = skb->data -
4389                                (unsigned char *)page_address(page) +
4390                                offset;
4391
4392                 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4393
4394                 __skb_frag_set_page(frag, page);
4395                 skb_frag_off_set(frag, first_offset);
4396                 skb_frag_size_set(frag, first_size);
4397
4398                 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4399                 /* We dont need to clear skbinfo->nr_frags here */
4400
4401                 new_truesize = SKB_DATA_ALIGN(sizeof(struct sk_buff));
4402                 delta_truesize = skb->truesize - new_truesize;
4403                 skb->truesize = new_truesize;
4404                 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4405                 goto done;
4406         }
4407
4408 merge:
4409         /* sk owenrship - if any - completely transferred to the aggregated packet */
4410         skb->destructor = NULL;
4411         delta_truesize = skb->truesize;
4412         if (offset > headlen) {
4413                 unsigned int eat = offset - headlen;
4414
4415                 skb_frag_off_add(&skbinfo->frags[0], eat);
4416                 skb_frag_size_sub(&skbinfo->frags[0], eat);
4417                 skb->data_len -= eat;
4418                 skb->len -= eat;
4419                 offset = headlen;
4420         }
4421
4422         __skb_pull(skb, offset);
4423
4424         if (NAPI_GRO_CB(p)->last == p)
4425                 skb_shinfo(p)->frag_list = skb;
4426         else
4427                 NAPI_GRO_CB(p)->last->next = skb;
4428         NAPI_GRO_CB(p)->last = skb;
4429         __skb_header_release(skb);
4430         lp = p;
4431
4432 done:
4433         NAPI_GRO_CB(p)->count++;
4434         p->data_len += len;
4435         p->truesize += delta_truesize;
4436         p->len += len;
4437         if (lp != p) {
4438                 lp->data_len += len;
4439                 lp->truesize += delta_truesize;
4440                 lp->len += len;
4441         }
4442         NAPI_GRO_CB(skb)->same_flow = 1;
4443         return 0;
4444 }
4445
4446 #ifdef CONFIG_SKB_EXTENSIONS
4447 #define SKB_EXT_ALIGN_VALUE     8
4448 #define SKB_EXT_CHUNKSIZEOF(x)  (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4449
4450 static const u8 skb_ext_type_len[] = {
4451 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4452         [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4453 #endif
4454 #ifdef CONFIG_XFRM
4455         [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4456 #endif
4457 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4458         [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
4459 #endif
4460 #if IS_ENABLED(CONFIG_MPTCP)
4461         [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
4462 #endif
4463 };
4464
4465 static __always_inline unsigned int skb_ext_total_length(void)
4466 {
4467         return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4468 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4469                 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4470 #endif
4471 #ifdef CONFIG_XFRM
4472                 skb_ext_type_len[SKB_EXT_SEC_PATH] +
4473 #endif
4474 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4475                 skb_ext_type_len[TC_SKB_EXT] +
4476 #endif
4477 #if IS_ENABLED(CONFIG_MPTCP)
4478                 skb_ext_type_len[SKB_EXT_MPTCP] +
4479 #endif
4480                 0;
4481 }
4482
4483 static void skb_extensions_init(void)
4484 {
4485         BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4486         BUILD_BUG_ON(skb_ext_total_length() > 255);
4487
4488         skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4489                                              SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4490                                              0,
4491                                              SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4492                                              NULL);
4493 }
4494 #else
4495 static void skb_extensions_init(void) {}
4496 #endif
4497
4498 void __init skb_init(void)
4499 {
4500         skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
4501                                               sizeof(struct sk_buff),
4502                                               0,
4503                                               SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4504                                               offsetof(struct sk_buff, cb),
4505                                               sizeof_field(struct sk_buff, cb),
4506                                               NULL);
4507         skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
4508                                                 sizeof(struct sk_buff_fclones),
4509                                                 0,
4510                                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4511                                                 NULL);
4512         skb_extensions_init();
4513 }
4514
4515 static int
4516 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4517                unsigned int recursion_level)
4518 {
4519         int start = skb_headlen(skb);
4520         int i, copy = start - offset;
4521         struct sk_buff *frag_iter;
4522         int elt = 0;
4523
4524         if (unlikely(recursion_level >= 24))
4525                 return -EMSGSIZE;
4526
4527         if (copy > 0) {
4528                 if (copy > len)
4529                         copy = len;
4530                 sg_set_buf(sg, skb->data + offset, copy);
4531                 elt++;
4532                 if ((len -= copy) == 0)
4533                         return elt;
4534                 offset += copy;
4535         }
4536
4537         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4538                 int end;
4539
4540                 WARN_ON(start > offset + len);
4541
4542                 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4543                 if ((copy = end - offset) > 0) {
4544                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4545                         if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4546                                 return -EMSGSIZE;
4547
4548                         if (copy > len)
4549                                 copy = len;
4550                         sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4551                                     skb_frag_off(frag) + offset - start);
4552                         elt++;
4553                         if (!(len -= copy))
4554                                 return elt;
4555                         offset += copy;
4556                 }
4557                 start = end;
4558         }
4559
4560         skb_walk_frags(skb, frag_iter) {
4561                 int end, ret;
4562
4563                 WARN_ON(start > offset + len);
4564
4565                 end = start + frag_iter->len;
4566                 if ((copy = end - offset) > 0) {
4567                         if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4568                                 return -EMSGSIZE;
4569
4570                         if (copy > len)
4571                                 copy = len;
4572                         ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4573                                               copy, recursion_level + 1);
4574                         if (unlikely(ret < 0))
4575                                 return ret;
4576                         elt += ret;
4577                         if ((len -= copy) == 0)
4578                                 return elt;
4579                         offset += copy;
4580                 }
4581                 start = end;
4582         }
4583         BUG_ON(len);
4584         return elt;
4585 }
4586
4587 /**
4588  *      skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4589  *      @skb: Socket buffer containing the buffers to be mapped
4590  *      @sg: The scatter-gather list to map into
4591  *      @offset: The offset into the buffer's contents to start mapping
4592  *      @len: Length of buffer space to be mapped
4593  *
4594  *      Fill the specified scatter-gather list with mappings/pointers into a
4595  *      region of the buffer space attached to a socket buffer. Returns either
4596  *      the number of scatterlist items used, or -EMSGSIZE if the contents
4597  *      could not fit.
4598  */
4599 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4600 {
4601         int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4602
4603         if (nsg <= 0)
4604                 return nsg;
4605
4606         sg_mark_end(&sg[nsg - 1]);
4607
4608         return nsg;
4609 }
4610 EXPORT_SYMBOL_GPL(skb_to_sgvec);
4611
4612 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4613  * sglist without mark the sg which contain last skb data as the end.
4614  * So the caller can mannipulate sg list as will when padding new data after
4615  * the first call without calling sg_unmark_end to expend sg list.
4616  *
4617  * Scenario to use skb_to_sgvec_nomark:
4618  * 1. sg_init_table
4619  * 2. skb_to_sgvec_nomark(payload1)
4620  * 3. skb_to_sgvec_nomark(payload2)
4621  *
4622  * This is equivalent to:
4623  * 1. sg_init_table
4624  * 2. skb_to_sgvec(payload1)
4625  * 3. sg_unmark_end
4626  * 4. skb_to_sgvec(payload2)
4627  *
4628  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4629  * is more preferable.
4630  */
4631 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4632                         int offset, int len)
4633 {
4634         return __skb_to_sgvec(skb, sg, offset, len, 0);
4635 }
4636 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4637
4638
4639
4640 /**
4641  *      skb_cow_data - Check that a socket buffer's data buffers are writable
4642  *      @skb: The socket buffer to check.
4643  *      @tailbits: Amount of trailing space to be added
4644  *      @trailer: Returned pointer to the skb where the @tailbits space begins
4645  *
4646  *      Make sure that the data buffers attached to a socket buffer are
4647  *      writable. If they are not, private copies are made of the data buffers
4648  *      and the socket buffer is set to use these instead.
4649  *
4650  *      If @tailbits is given, make sure that there is space to write @tailbits
4651  *      bytes of data beyond current end of socket buffer.  @trailer will be
4652  *      set to point to the skb in which this space begins.
4653  *
4654  *      The number of scatterlist elements required to completely map the
4655  *      COW'd and extended socket buffer will be returned.
4656  */
4657 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4658 {
4659         int copyflag;
4660         int elt;
4661         struct sk_buff *skb1, **skb_p;
4662
4663         /* If skb is cloned or its head is paged, reallocate
4664          * head pulling out all the pages (pages are considered not writable
4665          * at the moment even if they are anonymous).
4666          */
4667         if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4668             !__pskb_pull_tail(skb, __skb_pagelen(skb)))
4669                 return -ENOMEM;
4670
4671         /* Easy case. Most of packets will go this way. */
4672         if (!skb_has_frag_list(skb)) {
4673                 /* A little of trouble, not enough of space for trailer.
4674                  * This should not happen, when stack is tuned to generate
4675                  * good frames. OK, on miss we reallocate and reserve even more
4676                  * space, 128 bytes is fair. */
4677
4678                 if (skb_tailroom(skb) < tailbits &&
4679                     pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4680                         return -ENOMEM;
4681
4682                 /* Voila! */
4683                 *trailer = skb;
4684                 return 1;
4685         }
4686
4687         /* Misery. We are in troubles, going to mincer fragments... */
4688
4689         elt = 1;
4690         skb_p = &skb_shinfo(skb)->frag_list;
4691         copyflag = 0;
4692
4693         while ((skb1 = *skb_p) != NULL) {
4694                 int ntail = 0;
4695
4696                 /* The fragment is partially pulled by someone,
4697                  * this can happen on input. Copy it and everything
4698                  * after it. */
4699
4700                 if (skb_shared(skb1))
4701                         copyflag = 1;
4702
4703                 /* If the skb is the last, worry about trailer. */
4704
4705                 if (skb1->next == NULL && tailbits) {
4706                         if (skb_shinfo(skb1)->nr_frags ||
4707                             skb_has_frag_list(skb1) ||
4708                             skb_tailroom(skb1) < tailbits)
4709                                 ntail = tailbits + 128;
4710                 }
4711
4712                 if (copyflag ||
4713                     skb_cloned(skb1) ||
4714                     ntail ||
4715                     skb_shinfo(skb1)->nr_frags ||
4716                     skb_has_frag_list(skb1)) {
4717                         struct sk_buff *skb2;
4718
4719                         /* Fuck, we are miserable poor guys... */
4720                         if (ntail == 0)
4721                                 skb2 = skb_copy(skb1, GFP_ATOMIC);
4722                         else
4723                                 skb2 = skb_copy_expand(skb1,
4724                                                        skb_headroom(skb1),
4725                                                        ntail,
4726                                                        GFP_ATOMIC);
4727                         if (unlikely(skb2 == NULL))
4728                                 return -ENOMEM;
4729
4730                         if (skb1->sk)
4731                                 skb_set_owner_w(skb2, skb1->sk);
4732
4733                         /* Looking around. Are we still alive?
4734                          * OK, link new skb, drop old one */
4735
4736                         skb2->next = skb1->next;
4737                         *skb_p = skb2;
4738                         kfree_skb(skb1);
4739                         skb1 = skb2;
4740                 }
4741                 elt++;
4742                 *trailer = skb1;
4743                 skb_p = &skb1->next;
4744         }
4745
4746         return elt;
4747 }
4748 EXPORT_SYMBOL_GPL(skb_cow_data);
4749
4750 static void sock_rmem_free(struct sk_buff *skb)
4751 {
4752         struct sock *sk = skb->sk;
4753
4754         atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4755 }
4756
4757 static void skb_set_err_queue(struct sk_buff *skb)
4758 {
4759         /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4760          * So, it is safe to (mis)use it to mark skbs on the error queue.
4761          */
4762         skb->pkt_type = PACKET_OUTGOING;
4763         BUILD_BUG_ON(PACKET_OUTGOING == 0);
4764 }
4765
4766 /*
4767  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4768  */
4769 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4770 {
4771         if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4772             (unsigned int)READ_ONCE(sk->sk_rcvbuf))
4773                 return -ENOMEM;
4774
4775         skb_orphan(skb);
4776         skb->sk = sk;
4777         skb->destructor = sock_rmem_free;
4778         atomic_add(skb->truesize, &sk->sk_rmem_alloc);
4779         skb_set_err_queue(skb);
4780
4781         /* before exiting rcu section, make sure dst is refcounted */
4782         skb_dst_force(skb);
4783
4784         skb_queue_tail(&sk->sk_error_queue, skb);
4785         if (!sock_flag(sk, SOCK_DEAD))
4786                 sk_error_report(sk);
4787         return 0;
4788 }
4789 EXPORT_SYMBOL(sock_queue_err_skb);
4790
4791 static bool is_icmp_err_skb(const struct sk_buff *skb)
4792 {
4793         return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4794                        SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4795 }
4796
4797 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4798 {
4799         struct sk_buff_head *q = &sk->sk_error_queue;
4800         struct sk_buff *skb, *skb_next = NULL;
4801         bool icmp_next = false;
4802         unsigned long flags;
4803
4804         spin_lock_irqsave(&q->lock, flags);
4805         skb = __skb_dequeue(q);
4806         if (skb && (skb_next = skb_peek(q))) {
4807                 icmp_next = is_icmp_err_skb(skb_next);
4808                 if (icmp_next)
4809                         sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
4810         }
4811         spin_unlock_irqrestore(&q->lock, flags);
4812
4813         if (is_icmp_err_skb(skb) && !icmp_next)
4814                 sk->sk_err = 0;
4815
4816         if (skb_next)
4817                 sk_error_report(sk);
4818
4819         return skb;
4820 }
4821 EXPORT_SYMBOL(sock_dequeue_err_skb);
4822
4823 /**
4824  * skb_clone_sk - create clone of skb, and take reference to socket
4825  * @skb: the skb to clone
4826  *
4827  * This function creates a clone of a buffer that holds a reference on
4828  * sk_refcnt.  Buffers created via this function are meant to be
4829  * returned using sock_queue_err_skb, or free via kfree_skb.
4830  *
4831  * When passing buffers allocated with this function to sock_queue_err_skb
4832  * it is necessary to wrap the call with sock_hold/sock_put in order to
4833  * prevent the socket from being released prior to being enqueued on
4834  * the sk_error_queue.
4835  */
4836 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4837 {
4838         struct sock *sk = skb->sk;
4839         struct sk_buff *clone;
4840
4841         if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
4842                 return NULL;
4843
4844         clone = skb_clone(skb, GFP_ATOMIC);
4845         if (!clone) {
4846                 sock_put(sk);
4847                 return NULL;
4848         }
4849
4850         clone->sk = sk;
4851         clone->destructor = sock_efree;
4852
4853         return clone;
4854 }
4855 EXPORT_SYMBOL(skb_clone_sk);
4856
4857 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4858                                         struct sock *sk,
4859                                         int tstype,
4860                                         bool opt_stats)
4861 {
4862         struct sock_exterr_skb *serr;
4863         int err;
4864
4865         BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4866
4867         serr = SKB_EXT_ERR(skb);
4868         memset(serr, 0, sizeof(*serr));
4869         serr->ee.ee_errno = ENOMSG;
4870         serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4871         serr->ee.ee_info = tstype;
4872         serr->opt_stats = opt_stats;
4873         serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
4874         if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
4875                 serr->ee.ee_data = skb_shinfo(skb)->tskey;
4876                 if (sk->sk_protocol == IPPROTO_TCP &&
4877                     sk->sk_type == SOCK_STREAM)
4878                         serr->ee.ee_data -= atomic_read(&sk->sk_tskey);
4879         }
4880
4881         err = sock_queue_err_skb(sk, skb);
4882
4883         if (err)
4884                 kfree_skb(skb);
4885 }
4886
4887 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4888 {
4889         bool ret;
4890
4891         if (likely(sysctl_tstamp_allow_data || tsonly))
4892                 return true;
4893
4894         read_lock_bh(&sk->sk_callback_lock);
4895         ret = sk->sk_socket && sk->sk_socket->file &&
4896               file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4897         read_unlock_bh(&sk->sk_callback_lock);
4898         return ret;
4899 }
4900
4901 void skb_complete_tx_timestamp(struct sk_buff *skb,
4902                                struct skb_shared_hwtstamps *hwtstamps)
4903 {
4904         struct sock *sk = skb->sk;
4905
4906         if (!skb_may_tx_timestamp(sk, false))
4907                 goto err;
4908
4909         /* Take a reference to prevent skb_orphan() from freeing the socket,
4910          * but only if the socket refcount is not zero.
4911          */
4912         if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4913                 *skb_hwtstamps(skb) = *hwtstamps;
4914                 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
4915                 sock_put(sk);
4916                 return;
4917         }
4918
4919 err:
4920         kfree_skb(skb);
4921 }
4922 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4923
4924 void __skb_tstamp_tx(struct sk_buff *orig_skb,
4925                      const struct sk_buff *ack_skb,
4926                      struct skb_shared_hwtstamps *hwtstamps,
4927                      struct sock *sk, int tstype)
4928 {
4929         struct sk_buff *skb;
4930         bool tsonly, opt_stats = false;
4931
4932         if (!sk)
4933                 return;
4934
4935         if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4936             skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4937                 return;
4938
4939         tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4940         if (!skb_may_tx_timestamp(sk, tsonly))
4941                 return;
4942
4943         if (tsonly) {
4944 #ifdef CONFIG_INET
4945                 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4946                     sk->sk_protocol == IPPROTO_TCP &&
4947                     sk->sk_type == SOCK_STREAM) {
4948                         skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
4949                                                              ack_skb);
4950                         opt_stats = true;
4951                 } else
4952 #endif
4953                         skb = alloc_skb(0, GFP_ATOMIC);
4954         } else {
4955                 skb = skb_clone(orig_skb, GFP_ATOMIC);
4956         }
4957         if (!skb)
4958                 return;
4959
4960         if (tsonly) {
4961                 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4962                                              SKBTX_ANY_TSTAMP;
4963                 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4964         }
4965
4966         if (hwtstamps)
4967                 *skb_hwtstamps(skb) = *hwtstamps;
4968         else
4969                 skb->tstamp = ktime_get_real();
4970
4971         __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
4972 }
4973 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4974
4975 void skb_tstamp_tx(struct sk_buff *orig_skb,
4976                    struct skb_shared_hwtstamps *hwtstamps)
4977 {
4978         return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk,
4979                                SCM_TSTAMP_SND);
4980 }
4981 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4982
4983 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4984 {
4985         struct sock *sk = skb->sk;
4986         struct sock_exterr_skb *serr;
4987         int err = 1;
4988
4989         skb->wifi_acked_valid = 1;
4990         skb->wifi_acked = acked;
4991
4992         serr = SKB_EXT_ERR(skb);
4993         memset(serr, 0, sizeof(*serr));
4994         serr->ee.ee_errno = ENOMSG;
4995         serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4996
4997         /* Take a reference to prevent skb_orphan() from freeing the socket,
4998          * but only if the socket refcount is not zero.
4999          */
5000         if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
5001                 err = sock_queue_err_skb(sk, skb);
5002                 sock_put(sk);
5003         }
5004         if (err)
5005                 kfree_skb(skb);
5006 }
5007 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
5008
5009 /**
5010  * skb_partial_csum_set - set up and verify partial csum values for packet
5011  * @skb: the skb to set
5012  * @start: the number of bytes after skb->data to start checksumming.
5013  * @off: the offset from start to place the checksum.
5014  *
5015  * For untrusted partially-checksummed packets, we need to make sure the values
5016  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
5017  *
5018  * This function checks and sets those values and skb->ip_summed: if this
5019  * returns false you should drop the packet.
5020  */
5021 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
5022 {
5023         u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
5024         u32 csum_start = skb_headroom(skb) + (u32)start;
5025
5026         if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
5027                 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
5028                                      start, off, skb_headroom(skb), skb_headlen(skb));
5029                 return false;
5030         }
5031         skb->ip_summed = CHECKSUM_PARTIAL;
5032         skb->csum_start = csum_start;
5033         skb->csum_offset = off;
5034         skb_set_transport_header(skb, start);
5035         return true;
5036 }
5037 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
5038
5039 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
5040                                unsigned int max)
5041 {
5042         if (skb_headlen(skb) >= len)
5043                 return 0;
5044
5045         /* If we need to pullup then pullup to the max, so we
5046          * won't need to do it again.
5047          */
5048         if (max > skb->len)
5049                 max = skb->len;
5050
5051         if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
5052                 return -ENOMEM;
5053
5054         if (skb_headlen(skb) < len)
5055                 return -EPROTO;
5056
5057         return 0;
5058 }
5059
5060 #define MAX_TCP_HDR_LEN (15 * 4)
5061
5062 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
5063                                       typeof(IPPROTO_IP) proto,
5064                                       unsigned int off)
5065 {
5066         int err;
5067
5068         switch (proto) {
5069         case IPPROTO_TCP:
5070                 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
5071                                           off + MAX_TCP_HDR_LEN);
5072                 if (!err && !skb_partial_csum_set(skb, off,
5073                                                   offsetof(struct tcphdr,
5074                                                            check)))
5075                         err = -EPROTO;
5076                 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
5077
5078         case IPPROTO_UDP:
5079                 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
5080                                           off + sizeof(struct udphdr));
5081                 if (!err && !skb_partial_csum_set(skb, off,
5082                                                   offsetof(struct udphdr,
5083                                                            check)))
5084                         err = -EPROTO;
5085                 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
5086         }
5087
5088         return ERR_PTR(-EPROTO);
5089 }
5090
5091 /* This value should be large enough to cover a tagged ethernet header plus
5092  * maximally sized IP and TCP or UDP headers.
5093  */
5094 #define MAX_IP_HDR_LEN 128
5095
5096 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
5097 {
5098         unsigned int off;
5099         bool fragment;
5100         __sum16 *csum;
5101         int err;
5102
5103         fragment = false;
5104
5105         err = skb_maybe_pull_tail(skb,
5106                                   sizeof(struct iphdr),
5107                                   MAX_IP_HDR_LEN);
5108         if (err < 0)
5109                 goto out;
5110
5111         if (ip_is_fragment(ip_hdr(skb)))
5112                 fragment = true;
5113
5114         off = ip_hdrlen(skb);
5115
5116         err = -EPROTO;
5117
5118         if (fragment)
5119                 goto out;
5120
5121         csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
5122         if (IS_ERR(csum))
5123                 return PTR_ERR(csum);
5124
5125         if (recalculate)
5126                 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
5127                                            ip_hdr(skb)->daddr,
5128                                            skb->len - off,
5129                                            ip_hdr(skb)->protocol, 0);
5130         err = 0;
5131
5132 out:
5133         return err;
5134 }
5135
5136 /* This value should be large enough to cover a tagged ethernet header plus
5137  * an IPv6 header, all options, and a maximal TCP or UDP header.
5138  */
5139 #define MAX_IPV6_HDR_LEN 256
5140
5141 #define OPT_HDR(type, skb, off) \
5142         (type *)(skb_network_header(skb) + (off))
5143
5144 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
5145 {
5146         int err;
5147         u8 nexthdr;
5148         unsigned int off;
5149         unsigned int len;
5150         bool fragment;
5151         bool done;
5152         __sum16 *csum;
5153
5154         fragment = false;
5155         done = false;
5156
5157         off = sizeof(struct ipv6hdr);
5158
5159         err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
5160         if (err < 0)
5161                 goto out;
5162
5163         nexthdr = ipv6_hdr(skb)->nexthdr;
5164
5165         len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
5166         while (off <= len && !done) {
5167                 switch (nexthdr) {
5168                 case IPPROTO_DSTOPTS:
5169                 case IPPROTO_HOPOPTS:
5170                 case IPPROTO_ROUTING: {
5171                         struct ipv6_opt_hdr *hp;
5172
5173                         err = skb_maybe_pull_tail(skb,
5174                                                   off +
5175                                                   sizeof(struct ipv6_opt_hdr),
5176                                                   MAX_IPV6_HDR_LEN);
5177                         if (err < 0)
5178                                 goto out;
5179
5180                         hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
5181                         nexthdr = hp->nexthdr;
5182                         off += ipv6_optlen(hp);
5183                         break;
5184                 }
5185                 case IPPROTO_AH: {
5186                         struct ip_auth_hdr *hp;
5187
5188                         err = skb_maybe_pull_tail(skb,
5189                                                   off +
5190                                                   sizeof(struct ip_auth_hdr),
5191                                                   MAX_IPV6_HDR_LEN);
5192                         if (err < 0)
5193                                 goto out;
5194
5195                         hp = OPT_HDR(struct ip_auth_hdr, skb, off);
5196                         nexthdr = hp->nexthdr;
5197                         off += ipv6_authlen(hp);
5198                         break;
5199                 }
5200                 case IPPROTO_FRAGMENT: {
5201                         struct frag_hdr *hp;
5202
5203                         err = skb_maybe_pull_tail(skb,
5204                                                   off +
5205                                                   sizeof(struct frag_hdr),
5206                                                   MAX_IPV6_HDR_LEN);
5207                         if (err < 0)
5208                                 goto out;
5209
5210                         hp = OPT_HDR(struct frag_hdr, skb, off);
5211
5212                         if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5213                                 fragment = true;
5214
5215                         nexthdr = hp->nexthdr;
5216                         off += sizeof(struct frag_hdr);
5217                         break;
5218                 }
5219                 default:
5220                         done = true;
5221                         break;
5222                 }
5223         }
5224
5225         err = -EPROTO;
5226
5227         if (!done || fragment)
5228                 goto out;
5229
5230         csum = skb_checksum_setup_ip(skb, nexthdr, off);
5231         if (IS_ERR(csum))
5232                 return PTR_ERR(csum);
5233
5234         if (recalculate)
5235                 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5236                                          &ipv6_hdr(skb)->daddr,
5237                                          skb->len - off, nexthdr, 0);
5238         err = 0;
5239
5240 out:
5241         return err;
5242 }
5243
5244 /**
5245  * skb_checksum_setup - set up partial checksum offset
5246  * @skb: the skb to set up
5247  * @recalculate: if true the pseudo-header checksum will be recalculated
5248  */
5249 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5250 {
5251         int err;
5252
5253         switch (skb->protocol) {
5254         case htons(ETH_P_IP):
5255                 err = skb_checksum_setup_ipv4(skb, recalculate);
5256                 break;
5257
5258         case htons(ETH_P_IPV6):
5259                 err = skb_checksum_setup_ipv6(skb, recalculate);
5260                 break;
5261
5262         default:
5263                 err = -EPROTO;
5264                 break;
5265         }
5266
5267         return err;
5268 }
5269 EXPORT_SYMBOL(skb_checksum_setup);
5270
5271 /**
5272  * skb_checksum_maybe_trim - maybe trims the given skb
5273  * @skb: the skb to check
5274  * @transport_len: the data length beyond the network header
5275  *
5276  * Checks whether the given skb has data beyond the given transport length.
5277  * If so, returns a cloned skb trimmed to this transport length.
5278  * Otherwise returns the provided skb. Returns NULL in error cases
5279  * (e.g. transport_len exceeds skb length or out-of-memory).
5280  *
5281  * Caller needs to set the skb transport header and free any returned skb if it
5282  * differs from the provided skb.
5283  */
5284 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
5285                                                unsigned int transport_len)
5286 {
5287         struct sk_buff *skb_chk;
5288         unsigned int len = skb_transport_offset(skb) + transport_len;
5289         int ret;
5290
5291         if (skb->len < len)
5292                 return NULL;
5293         else if (skb->len == len)
5294                 return skb;
5295
5296         skb_chk = skb_clone(skb, GFP_ATOMIC);
5297         if (!skb_chk)
5298                 return NULL;
5299
5300         ret = pskb_trim_rcsum(skb_chk, len);
5301         if (ret) {
5302                 kfree_skb(skb_chk);
5303                 return NULL;
5304         }
5305
5306         return skb_chk;
5307 }
5308
5309 /**
5310  * skb_checksum_trimmed - validate checksum of an skb
5311  * @skb: the skb to check
5312  * @transport_len: the data length beyond the network header
5313  * @skb_chkf: checksum function to use
5314  *
5315  * Applies the given checksum function skb_chkf to the provided skb.
5316  * Returns a checked and maybe trimmed skb. Returns NULL on error.
5317  *
5318  * If the skb has data beyond the given transport length, then a
5319  * trimmed & cloned skb is checked and returned.
5320  *
5321  * Caller needs to set the skb transport header and free any returned skb if it
5322  * differs from the provided skb.
5323  */
5324 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5325                                      unsigned int transport_len,
5326                                      __sum16(*skb_chkf)(struct sk_buff *skb))
5327 {
5328         struct sk_buff *skb_chk;
5329         unsigned int offset = skb_transport_offset(skb);
5330         __sum16 ret;
5331
5332         skb_chk = skb_checksum_maybe_trim(skb, transport_len);
5333         if (!skb_chk)
5334                 goto err;
5335
5336         if (!pskb_may_pull(skb_chk, offset))
5337                 goto err;
5338
5339         skb_pull_rcsum(skb_chk, offset);
5340         ret = skb_chkf(skb_chk);
5341         skb_push_rcsum(skb_chk, offset);
5342
5343         if (ret)
5344                 goto err;
5345
5346         return skb_chk;
5347
5348 err:
5349         if (skb_chk && skb_chk != skb)
5350                 kfree_skb(skb_chk);
5351
5352         return NULL;
5353
5354 }
5355 EXPORT_SYMBOL(skb_checksum_trimmed);
5356
5357 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
5358 {
5359         net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5360                              skb->dev->name);
5361 }
5362 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
5363
5364 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5365 {
5366         if (head_stolen) {
5367                 skb_release_head_state(skb);
5368                 kmem_cache_free(skbuff_head_cache, skb);
5369         } else {
5370                 __kfree_skb(skb);
5371         }
5372 }
5373 EXPORT_SYMBOL(kfree_skb_partial);
5374
5375 /**
5376  * skb_try_coalesce - try to merge skb to prior one
5377  * @to: prior buffer
5378  * @from: buffer to add
5379  * @fragstolen: pointer to boolean
5380  * @delta_truesize: how much more was allocated than was requested
5381  */
5382 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5383                       bool *fragstolen, int *delta_truesize)
5384 {
5385         struct skb_shared_info *to_shinfo, *from_shinfo;
5386         int i, delta, len = from->len;
5387
5388         *fragstolen = false;
5389
5390         if (skb_cloned(to))
5391                 return false;
5392
5393         /* In general, avoid mixing slab allocated and page_pool allocated
5394          * pages within the same SKB. However when @to is not pp_recycle and
5395          * @from is cloned, we can transition frag pages from page_pool to
5396          * reference counted.
5397          *
5398          * On the other hand, don't allow coalescing two pp_recycle SKBs if
5399          * @from is cloned, in case the SKB is using page_pool fragment
5400          * references (PP_FLAG_PAGE_FRAG). Since we only take full page
5401          * references for cloned SKBs at the moment that would result in
5402          * inconsistent reference counts.
5403          */
5404         if (to->pp_recycle != (from->pp_recycle && !skb_cloned(from)))
5405                 return false;
5406
5407         if (len <= skb_tailroom(to)) {
5408                 if (len)
5409                         BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
5410                 *delta_truesize = 0;
5411                 return true;
5412         }
5413
5414         to_shinfo = skb_shinfo(to);
5415         from_shinfo = skb_shinfo(from);
5416         if (to_shinfo->frag_list || from_shinfo->frag_list)
5417                 return false;
5418         if (skb_zcopy(to) || skb_zcopy(from))
5419                 return false;
5420
5421         if (skb_headlen(from) != 0) {
5422                 struct page *page;
5423                 unsigned int offset;
5424
5425                 if (to_shinfo->nr_frags +
5426                     from_shinfo->nr_frags >= MAX_SKB_FRAGS)
5427                         return false;
5428
5429                 if (skb_head_is_locked(from))
5430                         return false;
5431
5432                 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5433
5434                 page = virt_to_head_page(from->head);
5435                 offset = from->data - (unsigned char *)page_address(page);
5436
5437                 skb_fill_page_desc(to, to_shinfo->nr_frags,
5438                                    page, offset, skb_headlen(from));
5439                 *fragstolen = true;
5440         } else {
5441                 if (to_shinfo->nr_frags +
5442                     from_shinfo->nr_frags > MAX_SKB_FRAGS)
5443                         return false;
5444
5445                 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
5446         }
5447
5448         WARN_ON_ONCE(delta < len);
5449
5450         memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5451                from_shinfo->frags,
5452                from_shinfo->nr_frags * sizeof(skb_frag_t));
5453         to_shinfo->nr_frags += from_shinfo->nr_frags;
5454
5455         if (!skb_cloned(from))
5456                 from_shinfo->nr_frags = 0;
5457
5458         /* if the skb is not cloned this does nothing
5459          * since we set nr_frags to 0.
5460          */
5461         for (i = 0; i < from_shinfo->nr_frags; i++)
5462                 __skb_frag_ref(&from_shinfo->frags[i]);
5463
5464         to->truesize += delta;
5465         to->len += len;
5466         to->data_len += len;
5467
5468         *delta_truesize = delta;
5469         return true;
5470 }
5471 EXPORT_SYMBOL(skb_try_coalesce);
5472
5473 /**
5474  * skb_scrub_packet - scrub an skb
5475  *
5476  * @skb: buffer to clean
5477  * @xnet: packet is crossing netns
5478  *
5479  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
5480  * into/from a tunnel. Some information have to be cleared during these
5481  * operations.
5482  * skb_scrub_packet can also be used to clean a skb before injecting it in
5483  * another namespace (@xnet == true). We have to clear all information in the
5484  * skb that could impact namespace isolation.
5485  */
5486 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
5487 {
5488         skb->pkt_type = PACKET_HOST;
5489         skb->skb_iif = 0;
5490         skb->ignore_df = 0;
5491         skb_dst_drop(skb);
5492         skb_ext_reset(skb);
5493         nf_reset_ct(skb);
5494         nf_reset_trace(skb);
5495
5496 #ifdef CONFIG_NET_SWITCHDEV
5497         skb->offload_fwd_mark = 0;
5498         skb->offload_l3_fwd_mark = 0;
5499 #endif
5500
5501         if (!xnet)
5502                 return;
5503
5504         ipvs_reset(skb);
5505         skb->mark = 0;
5506         skb->tstamp = 0;
5507 }
5508 EXPORT_SYMBOL_GPL(skb_scrub_packet);
5509
5510 /**
5511  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5512  *
5513  * @skb: GSO skb
5514  *
5515  * skb_gso_transport_seglen is used to determine the real size of the
5516  * individual segments, including Layer4 headers (TCP/UDP).
5517  *
5518  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5519  */
5520 static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
5521 {
5522         const struct skb_shared_info *shinfo = skb_shinfo(skb);
5523         unsigned int thlen = 0;
5524
5525         if (skb->encapsulation) {
5526                 thlen = skb_inner_transport_header(skb) -
5527                         skb_transport_header(skb);
5528
5529                 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5530                         thlen += inner_tcp_hdrlen(skb);
5531         } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5532                 thlen = tcp_hdrlen(skb);
5533         } else if (unlikely(skb_is_gso_sctp(skb))) {
5534                 thlen = sizeof(struct sctphdr);
5535         } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5536                 thlen = sizeof(struct udphdr);
5537         }
5538         /* UFO sets gso_size to the size of the fragmentation
5539          * payload, i.e. the size of the L4 (UDP) header is already
5540          * accounted for.
5541          */
5542         return thlen + shinfo->gso_size;
5543 }
5544
5545 /**
5546  * skb_gso_network_seglen - Return length of individual segments of a gso packet
5547  *
5548  * @skb: GSO skb
5549  *
5550  * skb_gso_network_seglen is used to determine the real size of the
5551  * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5552  *
5553  * The MAC/L2 header is not accounted for.
5554  */
5555 static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5556 {
5557         unsigned int hdr_len = skb_transport_header(skb) -
5558                                skb_network_header(skb);
5559
5560         return hdr_len + skb_gso_transport_seglen(skb);
5561 }
5562
5563 /**
5564  * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5565  *
5566  * @skb: GSO skb
5567  *
5568  * skb_gso_mac_seglen is used to determine the real size of the
5569  * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5570  * headers (TCP/UDP).
5571  */
5572 static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5573 {
5574         unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5575
5576         return hdr_len + skb_gso_transport_seglen(skb);
5577 }
5578
5579 /**
5580  * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5581  *
5582  * There are a couple of instances where we have a GSO skb, and we
5583  * want to determine what size it would be after it is segmented.
5584  *
5585  * We might want to check:
5586  * -    L3+L4+payload size (e.g. IP forwarding)
5587  * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5588  *
5589  * This is a helper to do that correctly considering GSO_BY_FRAGS.
5590  *
5591  * @skb: GSO skb
5592  *
5593  * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5594  *           GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5595  *
5596  * @max_len: The maximum permissible length.
5597  *
5598  * Returns true if the segmented length <= max length.
5599  */
5600 static inline bool skb_gso_size_check(const struct sk_buff *skb,
5601                                       unsigned int seg_len,
5602                                       unsigned int max_len) {
5603         const struct skb_shared_info *shinfo = skb_shinfo(skb);
5604         const struct sk_buff *iter;
5605
5606         if (shinfo->gso_size != GSO_BY_FRAGS)
5607                 return seg_len <= max_len;
5608
5609         /* Undo this so we can re-use header sizes */
5610         seg_len -= GSO_BY_FRAGS;
5611
5612         skb_walk_frags(skb, iter) {
5613                 if (seg_len + skb_headlen(iter) > max_len)
5614                         return false;
5615         }
5616
5617         return true;
5618 }
5619
5620 /**
5621  * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5622  *
5623  * @skb: GSO skb
5624  * @mtu: MTU to validate against
5625  *
5626  * skb_gso_validate_network_len validates if a given skb will fit a
5627  * wanted MTU once split. It considers L3 headers, L4 headers, and the
5628  * payload.
5629  */
5630 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5631 {
5632         return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5633 }
5634 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5635
5636 /**
5637  * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5638  *
5639  * @skb: GSO skb
5640  * @len: length to validate against
5641  *
5642  * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5643  * length once split, including L2, L3 and L4 headers and the payload.
5644  */
5645 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5646 {
5647         return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5648 }
5649 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5650
5651 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5652 {
5653         int mac_len, meta_len;
5654         void *meta;
5655
5656         if (skb_cow(skb, skb_headroom(skb)) < 0) {
5657                 kfree_skb(skb);
5658                 return NULL;
5659         }
5660
5661         mac_len = skb->data - skb_mac_header(skb);
5662         if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5663                 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5664                         mac_len - VLAN_HLEN - ETH_TLEN);
5665         }
5666
5667         meta_len = skb_metadata_len(skb);
5668         if (meta_len) {
5669                 meta = skb_metadata_end(skb) - meta_len;
5670                 memmove(meta + VLAN_HLEN, meta, meta_len);
5671         }
5672
5673         skb->mac_header += VLAN_HLEN;
5674         return skb;
5675 }
5676
5677 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5678 {
5679         struct vlan_hdr *vhdr;
5680         u16 vlan_tci;
5681
5682         if (unlikely(skb_vlan_tag_present(skb))) {
5683                 /* vlan_tci is already set-up so leave this for another time */
5684                 return skb;
5685         }
5686
5687         skb = skb_share_check(skb, GFP_ATOMIC);
5688         if (unlikely(!skb))
5689                 goto err_free;
5690         /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
5691         if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
5692                 goto err_free;
5693
5694         vhdr = (struct vlan_hdr *)skb->data;
5695         vlan_tci = ntohs(vhdr->h_vlan_TCI);
5696         __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5697
5698         skb_pull_rcsum(skb, VLAN_HLEN);
5699         vlan_set_encap_proto(skb, vhdr);
5700
5701         skb = skb_reorder_vlan_header(skb);
5702         if (unlikely(!skb))
5703                 goto err_free;
5704
5705         skb_reset_network_header(skb);
5706         if (!skb_transport_header_was_set(skb))
5707                 skb_reset_transport_header(skb);
5708         skb_reset_mac_len(skb);
5709
5710         return skb;
5711
5712 err_free:
5713         kfree_skb(skb);
5714         return NULL;
5715 }
5716 EXPORT_SYMBOL(skb_vlan_untag);
5717
5718 int skb_ensure_writable(struct sk_buff *skb, int write_len)
5719 {
5720         if (!pskb_may_pull(skb, write_len))
5721                 return -ENOMEM;
5722
5723         if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5724                 return 0;
5725
5726         return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5727 }
5728 EXPORT_SYMBOL(skb_ensure_writable);
5729
5730 /* remove VLAN header from packet and update csum accordingly.
5731  * expects a non skb_vlan_tag_present skb with a vlan tag payload
5732  */
5733 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
5734 {
5735         struct vlan_hdr *vhdr;
5736         int offset = skb->data - skb_mac_header(skb);
5737         int err;
5738
5739         if (WARN_ONCE(offset,
5740                       "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5741                       offset)) {
5742                 return -EINVAL;
5743         }
5744
5745         err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5746         if (unlikely(err))
5747                 return err;
5748
5749         skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5750
5751         vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5752         *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5753
5754         memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5755         __skb_pull(skb, VLAN_HLEN);
5756
5757         vlan_set_encap_proto(skb, vhdr);
5758         skb->mac_header += VLAN_HLEN;
5759
5760         if (skb_network_offset(skb) < ETH_HLEN)
5761                 skb_set_network_header(skb, ETH_HLEN);
5762
5763         skb_reset_mac_len(skb);
5764
5765         return err;
5766 }
5767 EXPORT_SYMBOL(__skb_vlan_pop);
5768
5769 /* Pop a vlan tag either from hwaccel or from payload.
5770  * Expects skb->data at mac header.
5771  */
5772 int skb_vlan_pop(struct sk_buff *skb)
5773 {
5774         u16 vlan_tci;
5775         __be16 vlan_proto;
5776         int err;
5777
5778         if (likely(skb_vlan_tag_present(skb))) {
5779                 __vlan_hwaccel_clear_tag(skb);
5780         } else {
5781                 if (unlikely(!eth_type_vlan(skb->protocol)))
5782                         return 0;
5783
5784                 err = __skb_vlan_pop(skb, &vlan_tci);
5785                 if (err)
5786                         return err;
5787         }
5788         /* move next vlan tag to hw accel tag */
5789         if (likely(!eth_type_vlan(skb->protocol)))
5790                 return 0;
5791
5792         vlan_proto = skb->protocol;
5793         err = __skb_vlan_pop(skb, &vlan_tci);
5794         if (unlikely(err))
5795                 return err;
5796
5797         __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5798         return 0;
5799 }
5800 EXPORT_SYMBOL(skb_vlan_pop);
5801
5802 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5803  * Expects skb->data at mac header.
5804  */
5805 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5806 {
5807         if (skb_vlan_tag_present(skb)) {
5808                 int offset = skb->data - skb_mac_header(skb);
5809                 int err;
5810
5811                 if (WARN_ONCE(offset,
5812                               "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5813                               offset)) {
5814                         return -EINVAL;
5815                 }
5816
5817                 err = __vlan_insert_tag(skb, skb->vlan_proto,
5818                                         skb_vlan_tag_get(skb));
5819                 if (err)
5820                         return err;
5821
5822                 skb->protocol = skb->vlan_proto;
5823                 skb->mac_len += VLAN_HLEN;
5824
5825                 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5826         }
5827         __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5828         return 0;
5829 }
5830 EXPORT_SYMBOL(skb_vlan_push);
5831
5832 /**
5833  * skb_eth_pop() - Drop the Ethernet header at the head of a packet
5834  *
5835  * @skb: Socket buffer to modify
5836  *
5837  * Drop the Ethernet header of @skb.
5838  *
5839  * Expects that skb->data points to the mac header and that no VLAN tags are
5840  * present.
5841  *
5842  * Returns 0 on success, -errno otherwise.
5843  */
5844 int skb_eth_pop(struct sk_buff *skb)
5845 {
5846         if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
5847             skb_network_offset(skb) < ETH_HLEN)
5848                 return -EPROTO;
5849
5850         skb_pull_rcsum(skb, ETH_HLEN);
5851         skb_reset_mac_header(skb);
5852         skb_reset_mac_len(skb);
5853
5854         return 0;
5855 }
5856 EXPORT_SYMBOL(skb_eth_pop);
5857
5858 /**
5859  * skb_eth_push() - Add a new Ethernet header at the head of a packet
5860  *
5861  * @skb: Socket buffer to modify
5862  * @dst: Destination MAC address of the new header
5863  * @src: Source MAC address of the new header
5864  *
5865  * Prepend @skb with a new Ethernet header.
5866  *
5867  * Expects that skb->data points to the mac header, which must be empty.
5868  *
5869  * Returns 0 on success, -errno otherwise.
5870  */
5871 int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
5872                  const unsigned char *src)
5873 {
5874         struct ethhdr *eth;
5875         int err;
5876
5877         if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
5878                 return -EPROTO;
5879
5880         err = skb_cow_head(skb, sizeof(*eth));
5881         if (err < 0)
5882                 return err;
5883
5884         skb_push(skb, sizeof(*eth));
5885         skb_reset_mac_header(skb);
5886         skb_reset_mac_len(skb);
5887
5888         eth = eth_hdr(skb);
5889         ether_addr_copy(eth->h_dest, dst);
5890         ether_addr_copy(eth->h_source, src);
5891         eth->h_proto = skb->protocol;
5892
5893         skb_postpush_rcsum(skb, eth, sizeof(*eth));
5894
5895         return 0;
5896 }
5897 EXPORT_SYMBOL(skb_eth_push);
5898
5899 /* Update the ethertype of hdr and the skb csum value if required. */
5900 static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
5901                              __be16 ethertype)
5902 {
5903         if (skb->ip_summed == CHECKSUM_COMPLETE) {
5904                 __be16 diff[] = { ~hdr->h_proto, ethertype };
5905
5906                 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5907         }
5908
5909         hdr->h_proto = ethertype;
5910 }
5911
5912 /**
5913  * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
5914  *                   the packet
5915  *
5916  * @skb: buffer
5917  * @mpls_lse: MPLS label stack entry to push
5918  * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
5919  * @mac_len: length of the MAC header
5920  * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
5921  *            ethernet
5922  *
5923  * Expects skb->data at mac header.
5924  *
5925  * Returns 0 on success, -errno otherwise.
5926  */
5927 int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
5928                   int mac_len, bool ethernet)
5929 {
5930         struct mpls_shim_hdr *lse;
5931         int err;
5932
5933         if (unlikely(!eth_p_mpls(mpls_proto)))
5934                 return -EINVAL;
5935
5936         /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
5937         if (skb->encapsulation)
5938                 return -EINVAL;
5939
5940         err = skb_cow_head(skb, MPLS_HLEN);
5941         if (unlikely(err))
5942                 return err;
5943
5944         if (!skb->inner_protocol) {
5945                 skb_set_inner_network_header(skb, skb_network_offset(skb));
5946                 skb_set_inner_protocol(skb, skb->protocol);
5947         }
5948
5949         skb_push(skb, MPLS_HLEN);
5950         memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
5951                 mac_len);
5952         skb_reset_mac_header(skb);
5953         skb_set_network_header(skb, mac_len);
5954         skb_reset_mac_len(skb);
5955
5956         lse = mpls_hdr(skb);
5957         lse->label_stack_entry = mpls_lse;
5958         skb_postpush_rcsum(skb, lse, MPLS_HLEN);
5959
5960         if (ethernet && mac_len >= ETH_HLEN)
5961                 skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
5962         skb->protocol = mpls_proto;
5963
5964         return 0;
5965 }
5966 EXPORT_SYMBOL_GPL(skb_mpls_push);
5967
5968 /**
5969  * skb_mpls_pop() - pop the outermost MPLS header
5970  *
5971  * @skb: buffer
5972  * @next_proto: ethertype of header after popped MPLS header
5973  * @mac_len: length of the MAC header
5974  * @ethernet: flag to indicate if the packet is ethernet
5975  *
5976  * Expects skb->data at mac header.
5977  *
5978  * Returns 0 on success, -errno otherwise.
5979  */
5980 int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
5981                  bool ethernet)
5982 {
5983         int err;
5984
5985         if (unlikely(!eth_p_mpls(skb->protocol)))
5986                 return 0;
5987
5988         err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
5989         if (unlikely(err))
5990                 return err;
5991
5992         skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
5993         memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
5994                 mac_len);
5995
5996         __skb_pull(skb, MPLS_HLEN);
5997         skb_reset_mac_header(skb);
5998         skb_set_network_header(skb, mac_len);
5999
6000         if (ethernet && mac_len >= ETH_HLEN) {
6001                 struct ethhdr *hdr;
6002
6003                 /* use mpls_hdr() to get ethertype to account for VLANs. */
6004                 hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
6005                 skb_mod_eth_type(skb, hdr, next_proto);
6006         }
6007         skb->protocol = next_proto;
6008
6009         return 0;
6010 }
6011 EXPORT_SYMBOL_GPL(skb_mpls_pop);
6012
6013 /**
6014  * skb_mpls_update_lse() - modify outermost MPLS header and update csum
6015  *
6016  * @skb: buffer
6017  * @mpls_lse: new MPLS label stack entry to update to
6018  *
6019  * Expects skb->data at mac header.
6020  *
6021  * Returns 0 on success, -errno otherwise.
6022  */
6023 int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
6024 {
6025         int err;
6026
6027         if (unlikely(!eth_p_mpls(skb->protocol)))
6028                 return -EINVAL;
6029
6030         err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
6031         if (unlikely(err))
6032                 return err;
6033
6034         if (skb->ip_summed == CHECKSUM_COMPLETE) {
6035                 __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
6036
6037                 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
6038         }
6039
6040         mpls_hdr(skb)->label_stack_entry = mpls_lse;
6041
6042         return 0;
6043 }
6044 EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
6045
6046 /**
6047  * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
6048  *
6049  * @skb: buffer
6050  *
6051  * Expects skb->data at mac header.
6052  *
6053  * Returns 0 on success, -errno otherwise.
6054  */
6055 int skb_mpls_dec_ttl(struct sk_buff *skb)
6056 {
6057         u32 lse;
6058         u8 ttl;
6059
6060         if (unlikely(!eth_p_mpls(skb->protocol)))
6061                 return -EINVAL;
6062
6063         if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
6064                 return -ENOMEM;
6065
6066         lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
6067         ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
6068         if (!--ttl)
6069                 return -EINVAL;
6070
6071         lse &= ~MPLS_LS_TTL_MASK;
6072         lse |= ttl << MPLS_LS_TTL_SHIFT;
6073
6074         return skb_mpls_update_lse(skb, cpu_to_be32(lse));
6075 }
6076 EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
6077
6078 /**
6079  * alloc_skb_with_frags - allocate skb with page frags
6080  *
6081  * @header_len: size of linear part
6082  * @data_len: needed length in frags
6083  * @max_page_order: max page order desired.
6084  * @errcode: pointer to error code if any
6085  * @gfp_mask: allocation mask
6086  *
6087  * This can be used to allocate a paged skb, given a maximal order for frags.
6088  */
6089 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
6090                                      unsigned long data_len,
6091                                      int max_page_order,
6092                                      int *errcode,
6093                                      gfp_t gfp_mask)
6094 {
6095         int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
6096         unsigned long chunk;
6097         struct sk_buff *skb;
6098         struct page *page;
6099         int i;
6100
6101         *errcode = -EMSGSIZE;
6102         /* Note this test could be relaxed, if we succeed to allocate
6103          * high order pages...
6104          */
6105         if (npages > MAX_SKB_FRAGS)
6106                 return NULL;
6107
6108         *errcode = -ENOBUFS;
6109         skb = alloc_skb(header_len, gfp_mask);
6110         if (!skb)
6111                 return NULL;
6112
6113         skb->truesize += npages << PAGE_SHIFT;
6114
6115         for (i = 0; npages > 0; i++) {
6116                 int order = max_page_order;
6117
6118                 while (order) {
6119                         if (npages >= 1 << order) {
6120                                 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
6121                                                    __GFP_COMP |
6122                                                    __GFP_NOWARN,
6123                                                    order);
6124                                 if (page)
6125                                         goto fill_page;
6126                                 /* Do not retry other high order allocations */
6127                                 order = 1;
6128                                 max_page_order = 0;
6129                         }
6130                         order--;
6131                 }
6132                 page = alloc_page(gfp_mask);
6133                 if (!page)
6134                         goto failure;
6135 fill_page:
6136                 chunk = min_t(unsigned long, data_len,
6137                               PAGE_SIZE << order);
6138                 skb_fill_page_desc(skb, i, page, 0, chunk);
6139                 data_len -= chunk;
6140                 npages -= 1 << order;
6141         }
6142         return skb;
6143
6144 failure:
6145         kfree_skb(skb);
6146         return NULL;
6147 }
6148 EXPORT_SYMBOL(alloc_skb_with_frags);
6149
6150 /* carve out the first off bytes from skb when off < headlen */
6151 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
6152                                     const int headlen, gfp_t gfp_mask)
6153 {
6154         int i;
6155         int size = skb_end_offset(skb);
6156         int new_hlen = headlen - off;
6157         u8 *data;
6158
6159         size = SKB_DATA_ALIGN(size);
6160
6161         if (skb_pfmemalloc(skb))
6162                 gfp_mask |= __GFP_MEMALLOC;
6163         data = kmalloc_reserve(size +
6164                                SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6165                                gfp_mask, NUMA_NO_NODE, NULL);
6166         if (!data)
6167                 return -ENOMEM;
6168
6169         size = SKB_WITH_OVERHEAD(ksize(data));
6170
6171         /* Copy real data, and all frags */
6172         skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
6173         skb->len -= off;
6174
6175         memcpy((struct skb_shared_info *)(data + size),
6176                skb_shinfo(skb),
6177                offsetof(struct skb_shared_info,
6178                         frags[skb_shinfo(skb)->nr_frags]));
6179         if (skb_cloned(skb)) {
6180                 /* drop the old head gracefully */
6181                 if (skb_orphan_frags(skb, gfp_mask)) {
6182                         kfree(data);
6183                         return -ENOMEM;
6184                 }
6185                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
6186                         skb_frag_ref(skb, i);
6187                 if (skb_has_frag_list(skb))
6188                         skb_clone_fraglist(skb);
6189                 skb_release_data(skb);
6190         } else {
6191                 /* we can reuse existing recount- all we did was
6192                  * relocate values
6193                  */
6194                 skb_free_head(skb);
6195         }
6196
6197         skb->head = data;
6198         skb->data = data;
6199         skb->head_frag = 0;
6200         skb_set_end_offset(skb, size);
6201         skb_set_tail_pointer(skb, skb_headlen(skb));
6202         skb_headers_offset_update(skb, 0);
6203         skb->cloned = 0;
6204         skb->hdr_len = 0;
6205         skb->nohdr = 0;
6206         atomic_set(&skb_shinfo(skb)->dataref, 1);
6207
6208         return 0;
6209 }
6210
6211 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
6212
6213 /* carve out the first eat bytes from skb's frag_list. May recurse into
6214  * pskb_carve()
6215  */
6216 static int pskb_carve_frag_list(struct sk_buff *skb,
6217                                 struct skb_shared_info *shinfo, int eat,
6218                                 gfp_t gfp_mask)
6219 {
6220         struct sk_buff *list = shinfo->frag_list;
6221         struct sk_buff *clone = NULL;
6222         struct sk_buff *insp = NULL;
6223
6224         do {
6225                 if (!list) {
6226                         pr_err("Not enough bytes to eat. Want %d\n", eat);
6227                         return -EFAULT;
6228                 }
6229                 if (list->len <= eat) {
6230                         /* Eaten as whole. */
6231                         eat -= list->len;
6232                         list = list->next;
6233                         insp = list;
6234                 } else {
6235                         /* Eaten partially. */
6236                         if (skb_shared(list)) {
6237                                 clone = skb_clone(list, gfp_mask);
6238                                 if (!clone)
6239                                         return -ENOMEM;
6240                                 insp = list->next;
6241                                 list = clone;
6242                         } else {
6243                                 /* This may be pulled without problems. */
6244                                 insp = list;
6245                         }
6246                         if (pskb_carve(list, eat, gfp_mask) < 0) {
6247                                 kfree_skb(clone);
6248                                 return -ENOMEM;
6249                         }
6250                         break;
6251                 }
6252         } while (eat);
6253
6254         /* Free pulled out fragments. */
6255         while ((list = shinfo->frag_list) != insp) {
6256                 shinfo->frag_list = list->next;
6257                 consume_skb(list);
6258         }
6259         /* And insert new clone at head. */
6260         if (clone) {
6261                 clone->next = list;
6262                 shinfo->frag_list = clone;
6263         }
6264         return 0;
6265 }
6266
6267 /* carve off first len bytes from skb. Split line (off) is in the
6268  * non-linear part of skb
6269  */
6270 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
6271                                        int pos, gfp_t gfp_mask)
6272 {
6273         int i, k = 0;
6274         int size = skb_end_offset(skb);
6275         u8 *data;
6276         const int nfrags = skb_shinfo(skb)->nr_frags;
6277         struct skb_shared_info *shinfo;
6278
6279         size = SKB_DATA_ALIGN(size);
6280
6281         if (skb_pfmemalloc(skb))
6282                 gfp_mask |= __GFP_MEMALLOC;
6283         data = kmalloc_reserve(size +
6284                                SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6285                                gfp_mask, NUMA_NO_NODE, NULL);
6286         if (!data)
6287                 return -ENOMEM;
6288
6289         size = SKB_WITH_OVERHEAD(ksize(data));
6290
6291         memcpy((struct skb_shared_info *)(data + size),
6292                skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
6293         if (skb_orphan_frags(skb, gfp_mask)) {
6294                 kfree(data);
6295                 return -ENOMEM;
6296         }
6297         shinfo = (struct skb_shared_info *)(data + size);
6298         for (i = 0; i < nfrags; i++) {
6299                 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
6300
6301                 if (pos + fsize > off) {
6302                         shinfo->frags[k] = skb_shinfo(skb)->frags[i];
6303
6304                         if (pos < off) {
6305                                 /* Split frag.
6306                                  * We have two variants in this case:
6307                                  * 1. Move all the frag to the second
6308                                  *    part, if it is possible. F.e.
6309                                  *    this approach is mandatory for TUX,
6310                                  *    where splitting is expensive.
6311                                  * 2. Split is accurately. We make this.
6312                                  */
6313                                 skb_frag_off_add(&shinfo->frags[0], off - pos);
6314                                 skb_frag_size_sub(&shinfo->frags[0], off - pos);
6315                         }
6316                         skb_frag_ref(skb, i);
6317                         k++;
6318                 }
6319                 pos += fsize;
6320         }
6321         shinfo->nr_frags = k;
6322         if (skb_has_frag_list(skb))
6323                 skb_clone_fraglist(skb);
6324
6325         /* split line is in frag list */
6326         if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6327                 /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6328                 if (skb_has_frag_list(skb))
6329                         kfree_skb_list(skb_shinfo(skb)->frag_list);
6330                 kfree(data);
6331                 return -ENOMEM;
6332         }
6333         skb_release_data(skb);
6334
6335         skb->head = data;
6336         skb->head_frag = 0;
6337         skb->data = data;
6338         skb_set_end_offset(skb, size);
6339         skb_reset_tail_pointer(skb);
6340         skb_headers_offset_update(skb, 0);
6341         skb->cloned   = 0;
6342         skb->hdr_len  = 0;
6343         skb->nohdr    = 0;
6344         skb->len -= off;
6345         skb->data_len = skb->len;
6346         atomic_set(&skb_shinfo(skb)->dataref, 1);
6347         return 0;
6348 }
6349
6350 /* remove len bytes from the beginning of the skb */
6351 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
6352 {
6353         int headlen = skb_headlen(skb);
6354
6355         if (len < headlen)
6356                 return pskb_carve_inside_header(skb, len, headlen, gfp);
6357         else
6358                 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
6359 }
6360
6361 /* Extract to_copy bytes starting at off from skb, and return this in
6362  * a new skb
6363  */
6364 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
6365                              int to_copy, gfp_t gfp)
6366 {
6367         struct sk_buff  *clone = skb_clone(skb, gfp);
6368
6369         if (!clone)
6370                 return NULL;
6371
6372         if (pskb_carve(clone, off, gfp) < 0 ||
6373             pskb_trim(clone, to_copy)) {
6374                 kfree_skb(clone);
6375                 return NULL;
6376         }
6377         return clone;
6378 }
6379 EXPORT_SYMBOL(pskb_extract);
6380
6381 /**
6382  * skb_condense - try to get rid of fragments/frag_list if possible
6383  * @skb: buffer
6384  *
6385  * Can be used to save memory before skb is added to a busy queue.
6386  * If packet has bytes in frags and enough tail room in skb->head,
6387  * pull all of them, so that we can free the frags right now and adjust
6388  * truesize.
6389  * Notes:
6390  *      We do not reallocate skb->head thus can not fail.
6391  *      Caller must re-evaluate skb->truesize if needed.
6392  */
6393 void skb_condense(struct sk_buff *skb)
6394 {
6395         if (skb->data_len) {
6396                 if (skb->data_len > skb->end - skb->tail ||
6397                     skb_cloned(skb))
6398                         return;
6399
6400                 /* Nice, we can free page frag(s) right now */
6401                 __pskb_pull_tail(skb, skb->data_len);
6402         }
6403         /* At this point, skb->truesize might be over estimated,
6404          * because skb had a fragment, and fragments do not tell
6405          * their truesize.
6406          * When we pulled its content into skb->head, fragment
6407          * was freed, but __pskb_pull_tail() could not possibly
6408          * adjust skb->truesize, not knowing the frag truesize.
6409          */
6410         skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6411 }
6412
6413 #ifdef CONFIG_SKB_EXTENSIONS
6414 static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6415 {
6416         return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6417 }
6418
6419 /**
6420  * __skb_ext_alloc - allocate a new skb extensions storage
6421  *
6422  * @flags: See kmalloc().
6423  *
6424  * Returns the newly allocated pointer. The pointer can later attached to a
6425  * skb via __skb_ext_set().
6426  * Note: caller must handle the skb_ext as an opaque data.
6427  */
6428 struct skb_ext *__skb_ext_alloc(gfp_t flags)
6429 {
6430         struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
6431
6432         if (new) {
6433                 memset(new->offset, 0, sizeof(new->offset));
6434                 refcount_set(&new->refcnt, 1);
6435         }
6436
6437         return new;
6438 }
6439
6440 static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
6441                                          unsigned int old_active)
6442 {
6443         struct skb_ext *new;
6444
6445         if (refcount_read(&old->refcnt) == 1)
6446                 return old;
6447
6448         new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6449         if (!new)
6450                 return NULL;
6451
6452         memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6453         refcount_set(&new->refcnt, 1);
6454
6455 #ifdef CONFIG_XFRM
6456         if (old_active & (1 << SKB_EXT_SEC_PATH)) {
6457                 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
6458                 unsigned int i;
6459
6460                 for (i = 0; i < sp->len; i++)
6461                         xfrm_state_hold(sp->xvec[i]);
6462         }
6463 #endif
6464         __skb_ext_put(old);
6465         return new;
6466 }
6467
6468 /**
6469  * __skb_ext_set - attach the specified extension storage to this skb
6470  * @skb: buffer
6471  * @id: extension id
6472  * @ext: extension storage previously allocated via __skb_ext_alloc()
6473  *
6474  * Existing extensions, if any, are cleared.
6475  *
6476  * Returns the pointer to the extension.
6477  */
6478 void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
6479                     struct skb_ext *ext)
6480 {
6481         unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
6482
6483         skb_ext_put(skb);
6484         newlen = newoff + skb_ext_type_len[id];
6485         ext->chunks = newlen;
6486         ext->offset[id] = newoff;
6487         skb->extensions = ext;
6488         skb->active_extensions = 1 << id;
6489         return skb_ext_get_ptr(ext, id);
6490 }
6491
6492 /**
6493  * skb_ext_add - allocate space for given extension, COW if needed
6494  * @skb: buffer
6495  * @id: extension to allocate space for
6496  *
6497  * Allocates enough space for the given extension.
6498  * If the extension is already present, a pointer to that extension
6499  * is returned.
6500  *
6501  * If the skb was cloned, COW applies and the returned memory can be
6502  * modified without changing the extension space of clones buffers.
6503  *
6504  * Returns pointer to the extension or NULL on allocation failure.
6505  */
6506 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6507 {
6508         struct skb_ext *new, *old = NULL;
6509         unsigned int newlen, newoff;
6510
6511         if (skb->active_extensions) {
6512                 old = skb->extensions;
6513
6514                 new = skb_ext_maybe_cow(old, skb->active_extensions);
6515                 if (!new)
6516                         return NULL;
6517
6518                 if (__skb_ext_exist(new, id))
6519                         goto set_active;
6520
6521                 newoff = new->chunks;
6522         } else {
6523                 newoff = SKB_EXT_CHUNKSIZEOF(*new);
6524
6525                 new = __skb_ext_alloc(GFP_ATOMIC);
6526                 if (!new)
6527                         return NULL;
6528         }
6529
6530         newlen = newoff + skb_ext_type_len[id];
6531         new->chunks = newlen;
6532         new->offset[id] = newoff;
6533 set_active:
6534         skb->slow_gro = 1;
6535         skb->extensions = new;
6536         skb->active_extensions |= 1 << id;
6537         return skb_ext_get_ptr(new, id);
6538 }
6539 EXPORT_SYMBOL(skb_ext_add);
6540
6541 #ifdef CONFIG_XFRM
6542 static void skb_ext_put_sp(struct sec_path *sp)
6543 {
6544         unsigned int i;
6545
6546         for (i = 0; i < sp->len; i++)
6547                 xfrm_state_put(sp->xvec[i]);
6548 }
6549 #endif
6550
6551 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6552 {
6553         struct skb_ext *ext = skb->extensions;
6554
6555         skb->active_extensions &= ~(1 << id);
6556         if (skb->active_extensions == 0) {
6557                 skb->extensions = NULL;
6558                 __skb_ext_put(ext);
6559 #ifdef CONFIG_XFRM
6560         } else if (id == SKB_EXT_SEC_PATH &&
6561                    refcount_read(&ext->refcnt) == 1) {
6562                 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
6563
6564                 skb_ext_put_sp(sp);
6565                 sp->len = 0;
6566 #endif
6567         }
6568 }
6569 EXPORT_SYMBOL(__skb_ext_del);
6570
6571 void __skb_ext_put(struct skb_ext *ext)
6572 {
6573         /* If this is last clone, nothing can increment
6574          * it after check passes.  Avoids one atomic op.
6575          */
6576         if (refcount_read(&ext->refcnt) == 1)
6577                 goto free_now;
6578
6579         if (!refcount_dec_and_test(&ext->refcnt))
6580                 return;
6581 free_now:
6582 #ifdef CONFIG_XFRM
6583         if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
6584                 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
6585 #endif
6586
6587         kmem_cache_free(skbuff_ext_cache, ext);
6588 }
6589 EXPORT_SYMBOL(__skb_ext_put);
6590 #endif /* CONFIG_SKB_EXTENSIONS */