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