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