These changes are a raw update to a vanilla kernel 4.1.10, with the
[kvmfornfv.git] / kernel / net / core / skbuff.c
1 /*
2  *      Routines having to do with the 'struct sk_buff' memory handlers.
3  *
4  *      Authors:        Alan Cox <alan@lxorguk.ukuu.org.uk>
5  *                      Florian La Roche <rzsfl@rz.uni-sb.de>
6  *
7  *      Fixes:
8  *              Alan Cox        :       Fixed the worst of the load
9  *                                      balancer bugs.
10  *              Dave Platt      :       Interrupt stacking fix.
11  *      Richard Kooijman        :       Timestamp fixes.
12  *              Alan Cox        :       Changed buffer format.
13  *              Alan Cox        :       destructor hook for AF_UNIX etc.
14  *              Linus Torvalds  :       Better skb_clone.
15  *              Alan Cox        :       Added skb_copy.
16  *              Alan Cox        :       Added all the changed routines Linus
17  *                                      only put in the headers
18  *              Ray VanTassle   :       Fixed --skb->lock in free
19  *              Alan Cox        :       skb_copy copy arp field
20  *              Andi Kleen      :       slabified it.
21  *              Robert Olsson   :       Removed skb_head_pool
22  *
23  *      NOTE:
24  *              The __skb_ routines should be called with interrupts
25  *      disabled, or you better be *real* sure that the operation is atomic
26  *      with respect to whatever list is being frobbed (e.g. via lock_sock()
27  *      or via disabling bottom half handlers, etc).
28  *
29  *      This program is free software; you can redistribute it and/or
30  *      modify it under the terms of the GNU General Public License
31  *      as published by the Free Software Foundation; either version
32  *      2 of the License, or (at your option) any later version.
33  */
34
35 /*
36  *      The functions in this file will not compile correctly with gcc 2.4.x
37  */
38
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
45 #include <linux/mm.h>
46 #include <linux/interrupt.h>
47 #include <linux/in.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.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 #include <linux/locallock.h>
67
68 #include <net/protocol.h>
69 #include <net/dst.h>
70 #include <net/sock.h>
71 #include <net/checksum.h>
72 #include <net/ip6_checksum.h>
73 #include <net/xfrm.h>
74
75 #include <asm/uaccess.h>
76 #include <trace/events/skb.h>
77 #include <linux/highmem.h>
78 #include <linux/capability.h>
79 #include <linux/user_namespace.h>
80
81 struct kmem_cache *skbuff_head_cache __read_mostly;
82 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
83
84 /**
85  *      skb_panic - private function for out-of-line support
86  *      @skb:   buffer
87  *      @sz:    size
88  *      @addr:  address
89  *      @msg:   skb_over_panic or skb_under_panic
90  *
91  *      Out-of-line support for skb_put() and skb_push().
92  *      Called via the wrapper skb_over_panic() or skb_under_panic().
93  *      Keep out of line to prevent kernel bloat.
94  *      __builtin_return_address is not used because it is not always reliable.
95  */
96 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
97                       const char msg[])
98 {
99         pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
100                  msg, addr, skb->len, sz, skb->head, skb->data,
101                  (unsigned long)skb->tail, (unsigned long)skb->end,
102                  skb->dev ? skb->dev->name : "<NULL>");
103         BUG();
104 }
105
106 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
107 {
108         skb_panic(skb, sz, addr, __func__);
109 }
110
111 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
112 {
113         skb_panic(skb, sz, addr, __func__);
114 }
115
116 /*
117  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
118  * the caller if emergency pfmemalloc reserves are being used. If it is and
119  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
120  * may be used. Otherwise, the packet data may be discarded until enough
121  * memory is free
122  */
123 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
124          __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
125
126 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
127                                unsigned long ip, bool *pfmemalloc)
128 {
129         void *obj;
130         bool ret_pfmemalloc = false;
131
132         /*
133          * Try a regular allocation, when that fails and we're not entitled
134          * to the reserves, fail.
135          */
136         obj = kmalloc_node_track_caller(size,
137                                         flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
138                                         node);
139         if (obj || !(gfp_pfmemalloc_allowed(flags)))
140                 goto out;
141
142         /* Try again but now we are using pfmemalloc reserves */
143         ret_pfmemalloc = true;
144         obj = kmalloc_node_track_caller(size, flags, node);
145
146 out:
147         if (pfmemalloc)
148                 *pfmemalloc = ret_pfmemalloc;
149
150         return obj;
151 }
152
153 /*      Allocate a new skbuff. We do this ourselves so we can fill in a few
154  *      'private' fields and also do memory statistics to find all the
155  *      [BEEP] leaks.
156  *
157  */
158
159 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
160 {
161         struct sk_buff *skb;
162
163         /* Get the HEAD */
164         skb = kmem_cache_alloc_node(skbuff_head_cache,
165                                     gfp_mask & ~__GFP_DMA, node);
166         if (!skb)
167                 goto out;
168
169         /*
170          * Only clear those fields we need to clear, not those that we will
171          * actually initialise below. Hence, don't put any more fields after
172          * the tail pointer in struct sk_buff!
173          */
174         memset(skb, 0, offsetof(struct sk_buff, tail));
175         skb->head = NULL;
176         skb->truesize = sizeof(struct sk_buff);
177         atomic_set(&skb->users, 1);
178
179         skb->mac_header = (typeof(skb->mac_header))~0U;
180 out:
181         return skb;
182 }
183
184 /**
185  *      __alloc_skb     -       allocate a network buffer
186  *      @size: size to allocate
187  *      @gfp_mask: allocation mask
188  *      @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
189  *              instead of head cache and allocate a cloned (child) skb.
190  *              If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
191  *              allocations in case the data is required for writeback
192  *      @node: numa node to allocate memory on
193  *
194  *      Allocate a new &sk_buff. The returned buffer has no headroom and a
195  *      tail room of at least size bytes. The object has a reference count
196  *      of one. The return is the buffer. On a failure the return is %NULL.
197  *
198  *      Buffers may only be allocated from interrupts using a @gfp_mask of
199  *      %GFP_ATOMIC.
200  */
201 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
202                             int flags, int node)
203 {
204         struct kmem_cache *cache;
205         struct skb_shared_info *shinfo;
206         struct sk_buff *skb;
207         u8 *data;
208         bool pfmemalloc;
209
210         cache = (flags & SKB_ALLOC_FCLONE)
211                 ? skbuff_fclone_cache : skbuff_head_cache;
212
213         if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
214                 gfp_mask |= __GFP_MEMALLOC;
215
216         /* Get the HEAD */
217         skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
218         if (!skb)
219                 goto out;
220         prefetchw(skb);
221
222         /* We do our best to align skb_shared_info on a separate cache
223          * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
224          * aligned memory blocks, unless SLUB/SLAB debug is enabled.
225          * Both skb->head and skb_shared_info are cache line aligned.
226          */
227         size = SKB_DATA_ALIGN(size);
228         size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
229         data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
230         if (!data)
231                 goto nodata;
232         /* kmalloc(size) might give us more room than requested.
233          * Put skb_shared_info exactly at the end of allocated zone,
234          * to allow max possible filling before reallocation.
235          */
236         size = SKB_WITH_OVERHEAD(ksize(data));
237         prefetchw(data + size);
238
239         /*
240          * Only clear those fields we need to clear, not those that we will
241          * actually initialise below. Hence, don't put any more fields after
242          * the tail pointer in struct sk_buff!
243          */
244         memset(skb, 0, offsetof(struct sk_buff, tail));
245         /* Account for allocated memory : skb + skb->head */
246         skb->truesize = SKB_TRUESIZE(size);
247         skb->pfmemalloc = pfmemalloc;
248         atomic_set(&skb->users, 1);
249         skb->head = data;
250         skb->data = data;
251         skb_reset_tail_pointer(skb);
252         skb->end = skb->tail + size;
253         skb->mac_header = (typeof(skb->mac_header))~0U;
254         skb->transport_header = (typeof(skb->transport_header))~0U;
255
256         /* make sure we initialize shinfo sequentially */
257         shinfo = skb_shinfo(skb);
258         memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
259         atomic_set(&shinfo->dataref, 1);
260         kmemcheck_annotate_variable(shinfo->destructor_arg);
261
262         if (flags & SKB_ALLOC_FCLONE) {
263                 struct sk_buff_fclones *fclones;
264
265                 fclones = container_of(skb, struct sk_buff_fclones, skb1);
266
267                 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
268                 skb->fclone = SKB_FCLONE_ORIG;
269                 atomic_set(&fclones->fclone_ref, 1);
270
271                 fclones->skb2.fclone = SKB_FCLONE_CLONE;
272                 fclones->skb2.pfmemalloc = pfmemalloc;
273         }
274 out:
275         return skb;
276 nodata:
277         kmem_cache_free(cache, skb);
278         skb = NULL;
279         goto out;
280 }
281 EXPORT_SYMBOL(__alloc_skb);
282
283 /**
284  * __build_skb - build a network buffer
285  * @data: data buffer provided by caller
286  * @frag_size: size of data, or 0 if head was kmalloced
287  *
288  * Allocate a new &sk_buff. Caller provides space holding head and
289  * skb_shared_info. @data must have been allocated by kmalloc() only if
290  * @frag_size is 0, otherwise data should come from the page allocator
291  *  or vmalloc()
292  * The return is the new skb buffer.
293  * On a failure the return is %NULL, and @data is not freed.
294  * Notes :
295  *  Before IO, driver allocates only data buffer where NIC put incoming frame
296  *  Driver should add room at head (NET_SKB_PAD) and
297  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
298  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
299  *  before giving packet to stack.
300  *  RX rings only contains data buffers, not full skbs.
301  */
302 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
303 {
304         struct skb_shared_info *shinfo;
305         struct sk_buff *skb;
306         unsigned int size = frag_size ? : ksize(data);
307
308         skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
309         if (!skb)
310                 return NULL;
311
312         size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
313
314         memset(skb, 0, offsetof(struct sk_buff, tail));
315         skb->truesize = SKB_TRUESIZE(size);
316         atomic_set(&skb->users, 1);
317         skb->head = data;
318         skb->data = data;
319         skb_reset_tail_pointer(skb);
320         skb->end = skb->tail + size;
321         skb->mac_header = (typeof(skb->mac_header))~0U;
322         skb->transport_header = (typeof(skb->transport_header))~0U;
323
324         /* make sure we initialize shinfo sequentially */
325         shinfo = skb_shinfo(skb);
326         memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
327         atomic_set(&shinfo->dataref, 1);
328         kmemcheck_annotate_variable(shinfo->destructor_arg);
329
330         return skb;
331 }
332
333 /* build_skb() is wrapper over __build_skb(), that specifically
334  * takes care of skb->head and skb->pfmemalloc
335  * This means that if @frag_size is not zero, then @data must be backed
336  * by a page fragment, not kmalloc() or vmalloc()
337  */
338 struct sk_buff *build_skb(void *data, unsigned int frag_size)
339 {
340         struct sk_buff *skb = __build_skb(data, frag_size);
341
342         if (skb && frag_size) {
343                 skb->head_frag = 1;
344                 if (page_is_pfmemalloc(virt_to_head_page(data)))
345                         skb->pfmemalloc = 1;
346         }
347         return skb;
348 }
349 EXPORT_SYMBOL(build_skb);
350
351 struct netdev_alloc_cache {
352         struct page_frag        frag;
353         /* we maintain a pagecount bias, so that we dont dirty cache line
354          * containing page->_count every time we allocate a fragment.
355          */
356         unsigned int            pagecnt_bias;
357 };
358 static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
359 static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
360 static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
361
362 static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
363                                        gfp_t gfp_mask)
364 {
365         const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
366         struct page *page = NULL;
367         gfp_t gfp = gfp_mask;
368
369         if (order) {
370                 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY |
371                             __GFP_NOMEMALLOC;
372                 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
373                 nc->frag.size = PAGE_SIZE << (page ? order : 0);
374         }
375
376         if (unlikely(!page))
377                 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
378
379         nc->frag.page = page;
380
381         return page;
382 }
383
384 static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
385                                unsigned int fragsz, gfp_t gfp_mask)
386 {
387         struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
388         struct page *page = nc->frag.page;
389         unsigned int size;
390         int offset;
391
392         if (unlikely(!page)) {
393 refill:
394                 page = __page_frag_refill(nc, gfp_mask);
395                 if (!page)
396                         return NULL;
397
398                 /* if size can vary use frag.size else just use PAGE_SIZE */
399                 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
400
401                 /* Even if we own the page, we do not use atomic_set().
402                  * This would break get_page_unless_zero() users.
403                  */
404                 atomic_add(size - 1, &page->_count);
405
406                 /* reset page count bias and offset to start of new frag */
407                 nc->pagecnt_bias = size;
408                 nc->frag.offset = size;
409         }
410
411         offset = nc->frag.offset - fragsz;
412         if (unlikely(offset < 0)) {
413                 if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
414                         goto refill;
415
416                 /* if size can vary use frag.size else just use PAGE_SIZE */
417                 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
418
419                 /* OK, page count is 0, we can safely set it */
420                 atomic_set(&page->_count, size);
421
422                 /* reset page count bias and offset to start of new frag */
423                 nc->pagecnt_bias = size;
424                 offset = size - fragsz;
425         }
426
427         nc->pagecnt_bias--;
428         nc->frag.offset = offset;
429
430         return page_address(page) + offset;
431 }
432
433 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
434 {
435         unsigned long flags;
436         void *data;
437
438         local_lock_irqsave(netdev_alloc_lock, flags);
439         data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
440         local_unlock_irqrestore(netdev_alloc_lock, flags);
441         return data;
442 }
443
444 /**
445  * netdev_alloc_frag - allocate a page fragment
446  * @fragsz: fragment size
447  *
448  * Allocates a frag from a page for receive buffer.
449  * Uses GFP_ATOMIC allocations.
450  */
451 void *netdev_alloc_frag(unsigned int fragsz)
452 {
453         return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
454 }
455 EXPORT_SYMBOL(netdev_alloc_frag);
456
457 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
458 {
459         return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
460 }
461
462 void *napi_alloc_frag(unsigned int fragsz)
463 {
464         return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
465 }
466 EXPORT_SYMBOL(napi_alloc_frag);
467
468 /**
469  *      __alloc_rx_skb - allocate an skbuff for rx
470  *      @length: length to allocate
471  *      @gfp_mask: get_free_pages mask, passed to alloc_skb
472  *      @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
473  *              allocations in case we have to fallback to __alloc_skb()
474  *              If SKB_ALLOC_NAPI is set, page fragment will be allocated
475  *              from napi_cache instead of netdev_cache.
476  *
477  *      Allocate a new &sk_buff and assign it a usage count of one. The
478  *      buffer has unspecified headroom built in. Users should allocate
479  *      the headroom they think they need without accounting for the
480  *      built in space. The built in space is used for optimisations.
481  *
482  *      %NULL is returned if there is no free memory.
483  */
484 static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
485                                       int flags)
486 {
487         struct sk_buff *skb = NULL;
488         unsigned int fragsz = SKB_DATA_ALIGN(length) +
489                               SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
490
491         if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
492                 void *data;
493
494                 if (sk_memalloc_socks())
495                         gfp_mask |= __GFP_MEMALLOC;
496
497                 data = (flags & SKB_ALLOC_NAPI) ?
498                         __napi_alloc_frag(fragsz, gfp_mask) :
499                         __netdev_alloc_frag(fragsz, gfp_mask);
500
501                 if (likely(data)) {
502                         skb = build_skb(data, fragsz);
503                         if (unlikely(!skb))
504                                 put_page(virt_to_head_page(data));
505                 }
506         } else {
507                 skb = __alloc_skb(length, gfp_mask,
508                                   SKB_ALLOC_RX, NUMA_NO_NODE);
509         }
510         return skb;
511 }
512
513 /**
514  *      __netdev_alloc_skb - allocate an skbuff for rx on a specific device
515  *      @dev: network device to receive on
516  *      @length: length to allocate
517  *      @gfp_mask: get_free_pages mask, passed to alloc_skb
518  *
519  *      Allocate a new &sk_buff and assign it a usage count of one. The
520  *      buffer has NET_SKB_PAD headroom built in. Users should allocate
521  *      the headroom they think they need without accounting for the
522  *      built in space. The built in space is used for optimisations.
523  *
524  *      %NULL is returned if there is no free memory.
525  */
526 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
527                                    unsigned int length, gfp_t gfp_mask)
528 {
529         struct sk_buff *skb;
530
531         length += NET_SKB_PAD;
532         skb = __alloc_rx_skb(length, gfp_mask, 0);
533
534         if (likely(skb)) {
535                 skb_reserve(skb, NET_SKB_PAD);
536                 skb->dev = dev;
537         }
538
539         return skb;
540 }
541 EXPORT_SYMBOL(__netdev_alloc_skb);
542
543 /**
544  *      __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
545  *      @napi: napi instance this buffer was allocated for
546  *      @length: length to allocate
547  *      @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
548  *
549  *      Allocate a new sk_buff for use in NAPI receive.  This buffer will
550  *      attempt to allocate the head from a special reserved region used
551  *      only for NAPI Rx allocation.  By doing this we can save several
552  *      CPU cycles by avoiding having to disable and re-enable IRQs.
553  *
554  *      %NULL is returned if there is no free memory.
555  */
556 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
557                                  unsigned int length, gfp_t gfp_mask)
558 {
559         struct sk_buff *skb;
560
561         length += NET_SKB_PAD + NET_IP_ALIGN;
562         skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
563
564         if (likely(skb)) {
565                 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
566                 skb->dev = napi->dev;
567         }
568
569         return skb;
570 }
571 EXPORT_SYMBOL(__napi_alloc_skb);
572
573 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
574                      int size, unsigned int truesize)
575 {
576         skb_fill_page_desc(skb, i, page, off, size);
577         skb->len += size;
578         skb->data_len += size;
579         skb->truesize += truesize;
580 }
581 EXPORT_SYMBOL(skb_add_rx_frag);
582
583 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
584                           unsigned int truesize)
585 {
586         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
587
588         skb_frag_size_add(frag, size);
589         skb->len += size;
590         skb->data_len += size;
591         skb->truesize += truesize;
592 }
593 EXPORT_SYMBOL(skb_coalesce_rx_frag);
594
595 static void skb_drop_list(struct sk_buff **listp)
596 {
597         kfree_skb_list(*listp);
598         *listp = NULL;
599 }
600
601 static inline void skb_drop_fraglist(struct sk_buff *skb)
602 {
603         skb_drop_list(&skb_shinfo(skb)->frag_list);
604 }
605
606 static void skb_clone_fraglist(struct sk_buff *skb)
607 {
608         struct sk_buff *list;
609
610         skb_walk_frags(skb, list)
611                 skb_get(list);
612 }
613
614 static void skb_free_head(struct sk_buff *skb)
615 {
616         if (skb->head_frag)
617                 put_page(virt_to_head_page(skb->head));
618         else
619                 kfree(skb->head);
620 }
621
622 static void skb_release_data(struct sk_buff *skb)
623 {
624         struct skb_shared_info *shinfo = skb_shinfo(skb);
625         int i;
626
627         if (skb->cloned &&
628             atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
629                               &shinfo->dataref))
630                 return;
631
632         for (i = 0; i < shinfo->nr_frags; i++)
633                 __skb_frag_unref(&shinfo->frags[i]);
634
635         /*
636          * If skb buf is from userspace, we need to notify the caller
637          * the lower device DMA has done;
638          */
639         if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
640                 struct ubuf_info *uarg;
641
642                 uarg = shinfo->destructor_arg;
643                 if (uarg->callback)
644                         uarg->callback(uarg, true);
645         }
646
647         if (shinfo->frag_list)
648                 kfree_skb_list(shinfo->frag_list);
649
650         skb_free_head(skb);
651 }
652
653 /*
654  *      Free an skbuff by memory without cleaning the state.
655  */
656 static void kfree_skbmem(struct sk_buff *skb)
657 {
658         struct sk_buff_fclones *fclones;
659
660         switch (skb->fclone) {
661         case SKB_FCLONE_UNAVAILABLE:
662                 kmem_cache_free(skbuff_head_cache, skb);
663                 return;
664
665         case SKB_FCLONE_ORIG:
666                 fclones = container_of(skb, struct sk_buff_fclones, skb1);
667
668                 /* We usually free the clone (TX completion) before original skb
669                  * This test would have no chance to be true for the clone,
670                  * while here, branch prediction will be good.
671                  */
672                 if (atomic_read(&fclones->fclone_ref) == 1)
673                         goto fastpath;
674                 break;
675
676         default: /* SKB_FCLONE_CLONE */
677                 fclones = container_of(skb, struct sk_buff_fclones, skb2);
678                 break;
679         }
680         if (!atomic_dec_and_test(&fclones->fclone_ref))
681                 return;
682 fastpath:
683         kmem_cache_free(skbuff_fclone_cache, fclones);
684 }
685
686 static void skb_release_head_state(struct sk_buff *skb)
687 {
688         skb_dst_drop(skb);
689 #ifdef CONFIG_XFRM
690         secpath_put(skb->sp);
691 #endif
692         if (skb->destructor) {
693                 WARN_ON(in_irq());
694                 skb->destructor(skb);
695         }
696 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
697         nf_conntrack_put(skb->nfct);
698 #endif
699 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
700         nf_bridge_put(skb->nf_bridge);
701 #endif
702 }
703
704 /* Free everything but the sk_buff shell. */
705 static void skb_release_all(struct sk_buff *skb)
706 {
707         skb_release_head_state(skb);
708         if (likely(skb->head))
709                 skb_release_data(skb);
710 }
711
712 /**
713  *      __kfree_skb - private function
714  *      @skb: buffer
715  *
716  *      Free an sk_buff. Release anything attached to the buffer.
717  *      Clean the state. This is an internal helper function. Users should
718  *      always call kfree_skb
719  */
720
721 void __kfree_skb(struct sk_buff *skb)
722 {
723         skb_release_all(skb);
724         kfree_skbmem(skb);
725 }
726 EXPORT_SYMBOL(__kfree_skb);
727
728 /**
729  *      kfree_skb - free an sk_buff
730  *      @skb: buffer to free
731  *
732  *      Drop a reference to the buffer and free it if the usage count has
733  *      hit zero.
734  */
735 void kfree_skb(struct sk_buff *skb)
736 {
737         if (unlikely(!skb))
738                 return;
739         if (likely(atomic_read(&skb->users) == 1))
740                 smp_rmb();
741         else if (likely(!atomic_dec_and_test(&skb->users)))
742                 return;
743         trace_kfree_skb(skb, __builtin_return_address(0));
744         __kfree_skb(skb);
745 }
746 EXPORT_SYMBOL(kfree_skb);
747
748 void kfree_skb_list(struct sk_buff *segs)
749 {
750         while (segs) {
751                 struct sk_buff *next = segs->next;
752
753                 kfree_skb(segs);
754                 segs = next;
755         }
756 }
757 EXPORT_SYMBOL(kfree_skb_list);
758
759 /**
760  *      skb_tx_error - report an sk_buff xmit error
761  *      @skb: buffer that triggered an error
762  *
763  *      Report xmit error if a device callback is tracking this skb.
764  *      skb must be freed afterwards.
765  */
766 void skb_tx_error(struct sk_buff *skb)
767 {
768         if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
769                 struct ubuf_info *uarg;
770
771                 uarg = skb_shinfo(skb)->destructor_arg;
772                 if (uarg->callback)
773                         uarg->callback(uarg, false);
774                 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
775         }
776 }
777 EXPORT_SYMBOL(skb_tx_error);
778
779 /**
780  *      consume_skb - free an skbuff
781  *      @skb: buffer to free
782  *
783  *      Drop a ref to the buffer and free it if the usage count has hit zero
784  *      Functions identically to kfree_skb, but kfree_skb assumes that the frame
785  *      is being dropped after a failure and notes that
786  */
787 void consume_skb(struct sk_buff *skb)
788 {
789         if (unlikely(!skb))
790                 return;
791         if (likely(atomic_read(&skb->users) == 1))
792                 smp_rmb();
793         else if (likely(!atomic_dec_and_test(&skb->users)))
794                 return;
795         trace_consume_skb(skb);
796         __kfree_skb(skb);
797 }
798 EXPORT_SYMBOL(consume_skb);
799
800 /* Make sure a field is enclosed inside headers_start/headers_end section */
801 #define CHECK_SKB_FIELD(field) \
802         BUILD_BUG_ON(offsetof(struct sk_buff, field) <          \
803                      offsetof(struct sk_buff, headers_start));  \
804         BUILD_BUG_ON(offsetof(struct sk_buff, field) >          \
805                      offsetof(struct sk_buff, headers_end));    \
806
807 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
808 {
809         new->tstamp             = old->tstamp;
810         /* We do not copy old->sk */
811         new->dev                = old->dev;
812         memcpy(new->cb, old->cb, sizeof(old->cb));
813         skb_dst_copy(new, old);
814 #ifdef CONFIG_XFRM
815         new->sp                 = secpath_get(old->sp);
816 #endif
817         __nf_copy(new, old, false);
818
819         /* Note : this field could be in headers_start/headers_end section
820          * It is not yet because we do not want to have a 16 bit hole
821          */
822         new->queue_mapping = old->queue_mapping;
823
824         memcpy(&new->headers_start, &old->headers_start,
825                offsetof(struct sk_buff, headers_end) -
826                offsetof(struct sk_buff, headers_start));
827         CHECK_SKB_FIELD(protocol);
828         CHECK_SKB_FIELD(csum);
829         CHECK_SKB_FIELD(hash);
830         CHECK_SKB_FIELD(priority);
831         CHECK_SKB_FIELD(skb_iif);
832         CHECK_SKB_FIELD(vlan_proto);
833         CHECK_SKB_FIELD(vlan_tci);
834         CHECK_SKB_FIELD(transport_header);
835         CHECK_SKB_FIELD(network_header);
836         CHECK_SKB_FIELD(mac_header);
837         CHECK_SKB_FIELD(inner_protocol);
838         CHECK_SKB_FIELD(inner_transport_header);
839         CHECK_SKB_FIELD(inner_network_header);
840         CHECK_SKB_FIELD(inner_mac_header);
841         CHECK_SKB_FIELD(mark);
842 #ifdef CONFIG_NETWORK_SECMARK
843         CHECK_SKB_FIELD(secmark);
844 #endif
845 #ifdef CONFIG_NET_RX_BUSY_POLL
846         CHECK_SKB_FIELD(napi_id);
847 #endif
848 #ifdef CONFIG_XPS
849         CHECK_SKB_FIELD(sender_cpu);
850 #endif
851 #ifdef CONFIG_NET_SCHED
852         CHECK_SKB_FIELD(tc_index);
853 #ifdef CONFIG_NET_CLS_ACT
854         CHECK_SKB_FIELD(tc_verd);
855 #endif
856 #endif
857
858 }
859
860 /*
861  * You should not add any new code to this function.  Add it to
862  * __copy_skb_header above instead.
863  */
864 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
865 {
866 #define C(x) n->x = skb->x
867
868         n->next = n->prev = NULL;
869         n->sk = NULL;
870         __copy_skb_header(n, skb);
871
872         C(len);
873         C(data_len);
874         C(mac_len);
875         n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
876         n->cloned = 1;
877         n->nohdr = 0;
878         n->destructor = NULL;
879         C(tail);
880         C(end);
881         C(head);
882         C(head_frag);
883         C(data);
884         C(truesize);
885         atomic_set(&n->users, 1);
886
887         atomic_inc(&(skb_shinfo(skb)->dataref));
888         skb->cloned = 1;
889
890         return n;
891 #undef C
892 }
893
894 /**
895  *      skb_morph       -       morph one skb into another
896  *      @dst: the skb to receive the contents
897  *      @src: the skb to supply the contents
898  *
899  *      This is identical to skb_clone except that the target skb is
900  *      supplied by the user.
901  *
902  *      The target skb is returned upon exit.
903  */
904 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
905 {
906         skb_release_all(dst);
907         return __skb_clone(dst, src);
908 }
909 EXPORT_SYMBOL_GPL(skb_morph);
910
911 /**
912  *      skb_copy_ubufs  -       copy userspace skb frags buffers to kernel
913  *      @skb: the skb to modify
914  *      @gfp_mask: allocation priority
915  *
916  *      This must be called on SKBTX_DEV_ZEROCOPY skb.
917  *      It will copy all frags into kernel and drop the reference
918  *      to userspace pages.
919  *
920  *      If this function is called from an interrupt gfp_mask() must be
921  *      %GFP_ATOMIC.
922  *
923  *      Returns 0 on success or a negative error code on failure
924  *      to allocate kernel memory to copy to.
925  */
926 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
927 {
928         int i;
929         int num_frags = skb_shinfo(skb)->nr_frags;
930         struct page *page, *head = NULL;
931         struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
932
933         for (i = 0; i < num_frags; i++) {
934                 u8 *vaddr;
935                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
936
937                 page = alloc_page(gfp_mask);
938                 if (!page) {
939                         while (head) {
940                                 struct page *next = (struct page *)page_private(head);
941                                 put_page(head);
942                                 head = next;
943                         }
944                         return -ENOMEM;
945                 }
946                 vaddr = kmap_atomic(skb_frag_page(f));
947                 memcpy(page_address(page),
948                        vaddr + f->page_offset, skb_frag_size(f));
949                 kunmap_atomic(vaddr);
950                 set_page_private(page, (unsigned long)head);
951                 head = page;
952         }
953
954         /* skb frags release userspace buffers */
955         for (i = 0; i < num_frags; i++)
956                 skb_frag_unref(skb, i);
957
958         uarg->callback(uarg, false);
959
960         /* skb frags point to kernel buffers */
961         for (i = num_frags - 1; i >= 0; i--) {
962                 __skb_fill_page_desc(skb, i, head, 0,
963                                      skb_shinfo(skb)->frags[i].size);
964                 head = (struct page *)page_private(head);
965         }
966
967         skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
968         return 0;
969 }
970 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
971
972 /**
973  *      skb_clone       -       duplicate an sk_buff
974  *      @skb: buffer to clone
975  *      @gfp_mask: allocation priority
976  *
977  *      Duplicate an &sk_buff. The new one is not owned by a socket. Both
978  *      copies share the same packet data but not structure. The new
979  *      buffer has a reference count of 1. If the allocation fails the
980  *      function returns %NULL otherwise the new buffer is returned.
981  *
982  *      If this function is called from an interrupt gfp_mask() must be
983  *      %GFP_ATOMIC.
984  */
985
986 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
987 {
988         struct sk_buff_fclones *fclones = container_of(skb,
989                                                        struct sk_buff_fclones,
990                                                        skb1);
991         struct sk_buff *n;
992
993         if (skb_orphan_frags(skb, gfp_mask))
994                 return NULL;
995
996         if (skb->fclone == SKB_FCLONE_ORIG &&
997             atomic_read(&fclones->fclone_ref) == 1) {
998                 n = &fclones->skb2;
999                 atomic_set(&fclones->fclone_ref, 2);
1000         } else {
1001                 if (skb_pfmemalloc(skb))
1002                         gfp_mask |= __GFP_MEMALLOC;
1003
1004                 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1005                 if (!n)
1006                         return NULL;
1007
1008                 kmemcheck_annotate_bitfield(n, flags1);
1009                 n->fclone = SKB_FCLONE_UNAVAILABLE;
1010         }
1011
1012         return __skb_clone(n, skb);
1013 }
1014 EXPORT_SYMBOL(skb_clone);
1015
1016 static void skb_headers_offset_update(struct sk_buff *skb, int off)
1017 {
1018         /* Only adjust this if it actually is csum_start rather than csum */
1019         if (skb->ip_summed == CHECKSUM_PARTIAL)
1020                 skb->csum_start += off;
1021         /* {transport,network,mac}_header and tail are relative to skb->head */
1022         skb->transport_header += off;
1023         skb->network_header   += off;
1024         if (skb_mac_header_was_set(skb))
1025                 skb->mac_header += off;
1026         skb->inner_transport_header += off;
1027         skb->inner_network_header += off;
1028         skb->inner_mac_header += off;
1029 }
1030
1031 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1032 {
1033         __copy_skb_header(new, old);
1034
1035         skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1036         skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1037         skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1038 }
1039
1040 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1041 {
1042         if (skb_pfmemalloc(skb))
1043                 return SKB_ALLOC_RX;
1044         return 0;
1045 }
1046
1047 /**
1048  *      skb_copy        -       create private copy of an sk_buff
1049  *      @skb: buffer to copy
1050  *      @gfp_mask: allocation priority
1051  *
1052  *      Make a copy of both an &sk_buff and its data. This is used when the
1053  *      caller wishes to modify the data and needs a private copy of the
1054  *      data to alter. Returns %NULL on failure or the pointer to the buffer
1055  *      on success. The returned buffer has a reference count of 1.
1056  *
1057  *      As by-product this function converts non-linear &sk_buff to linear
1058  *      one, so that &sk_buff becomes completely private and caller is allowed
1059  *      to modify all the data of returned buffer. This means that this
1060  *      function is not recommended for use in circumstances when only
1061  *      header is going to be modified. Use pskb_copy() instead.
1062  */
1063
1064 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1065 {
1066         int headerlen = skb_headroom(skb);
1067         unsigned int size = skb_end_offset(skb) + skb->data_len;
1068         struct sk_buff *n = __alloc_skb(size, gfp_mask,
1069                                         skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1070
1071         if (!n)
1072                 return NULL;
1073
1074         /* Set the data pointer */
1075         skb_reserve(n, headerlen);
1076         /* Set the tail pointer and length */
1077         skb_put(n, skb->len);
1078
1079         if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1080                 BUG();
1081
1082         copy_skb_header(n, skb);
1083         return n;
1084 }
1085 EXPORT_SYMBOL(skb_copy);
1086
1087 /**
1088  *      __pskb_copy_fclone      -  create copy of an sk_buff with private head.
1089  *      @skb: buffer to copy
1090  *      @headroom: headroom of new skb
1091  *      @gfp_mask: allocation priority
1092  *      @fclone: if true allocate the copy of the skb from the fclone
1093  *      cache instead of the head cache; it is recommended to set this
1094  *      to true for the cases where the copy will likely be cloned
1095  *
1096  *      Make a copy of both an &sk_buff and part of its data, located
1097  *      in header. Fragmented data remain shared. This is used when
1098  *      the caller wishes to modify only header of &sk_buff and needs
1099  *      private copy of the header to alter. Returns %NULL on failure
1100  *      or the pointer to the buffer on success.
1101  *      The returned buffer has a reference count of 1.
1102  */
1103
1104 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1105                                    gfp_t gfp_mask, bool fclone)
1106 {
1107         unsigned int size = skb_headlen(skb) + headroom;
1108         int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1109         struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1110
1111         if (!n)
1112                 goto out;
1113
1114         /* Set the data pointer */
1115         skb_reserve(n, headroom);
1116         /* Set the tail pointer and length */
1117         skb_put(n, skb_headlen(skb));
1118         /* Copy the bytes */
1119         skb_copy_from_linear_data(skb, n->data, n->len);
1120
1121         n->truesize += skb->data_len;
1122         n->data_len  = skb->data_len;
1123         n->len       = skb->len;
1124
1125         if (skb_shinfo(skb)->nr_frags) {
1126                 int i;
1127
1128                 if (skb_orphan_frags(skb, gfp_mask)) {
1129                         kfree_skb(n);
1130                         n = NULL;
1131                         goto out;
1132                 }
1133                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1134                         skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1135                         skb_frag_ref(skb, i);
1136                 }
1137                 skb_shinfo(n)->nr_frags = i;
1138         }
1139
1140         if (skb_has_frag_list(skb)) {
1141                 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1142                 skb_clone_fraglist(n);
1143         }
1144
1145         copy_skb_header(n, skb);
1146 out:
1147         return n;
1148 }
1149 EXPORT_SYMBOL(__pskb_copy_fclone);
1150
1151 /**
1152  *      pskb_expand_head - reallocate header of &sk_buff
1153  *      @skb: buffer to reallocate
1154  *      @nhead: room to add at head
1155  *      @ntail: room to add at tail
1156  *      @gfp_mask: allocation priority
1157  *
1158  *      Expands (or creates identical copy, if @nhead and @ntail are zero)
1159  *      header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1160  *      reference count of 1. Returns zero in the case of success or error,
1161  *      if expansion failed. In the last case, &sk_buff is not changed.
1162  *
1163  *      All the pointers pointing into skb header may change and must be
1164  *      reloaded after call to this function.
1165  */
1166
1167 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1168                      gfp_t gfp_mask)
1169 {
1170         int i;
1171         u8 *data;
1172         int size = nhead + skb_end_offset(skb) + ntail;
1173         long off;
1174
1175         BUG_ON(nhead < 0);
1176
1177         if (skb_shared(skb))
1178                 BUG();
1179
1180         size = SKB_DATA_ALIGN(size);
1181
1182         if (skb_pfmemalloc(skb))
1183                 gfp_mask |= __GFP_MEMALLOC;
1184         data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1185                                gfp_mask, NUMA_NO_NODE, NULL);
1186         if (!data)
1187                 goto nodata;
1188         size = SKB_WITH_OVERHEAD(ksize(data));
1189
1190         /* Copy only real data... and, alas, header. This should be
1191          * optimized for the cases when header is void.
1192          */
1193         memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1194
1195         memcpy((struct skb_shared_info *)(data + size),
1196                skb_shinfo(skb),
1197                offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1198
1199         /*
1200          * if shinfo is shared we must drop the old head gracefully, but if it
1201          * is not we can just drop the old head and let the existing refcount
1202          * be since all we did is relocate the values
1203          */
1204         if (skb_cloned(skb)) {
1205                 /* copy this zero copy skb frags */
1206                 if (skb_orphan_frags(skb, gfp_mask))
1207                         goto nofrags;
1208                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1209                         skb_frag_ref(skb, i);
1210
1211                 if (skb_has_frag_list(skb))
1212                         skb_clone_fraglist(skb);
1213
1214                 skb_release_data(skb);
1215         } else {
1216                 skb_free_head(skb);
1217         }
1218         off = (data + nhead) - skb->head;
1219
1220         skb->head     = data;
1221         skb->head_frag = 0;
1222         skb->data    += off;
1223 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1224         skb->end      = size;
1225         off           = nhead;
1226 #else
1227         skb->end      = skb->head + size;
1228 #endif
1229         skb->tail             += off;
1230         skb_headers_offset_update(skb, nhead);
1231         skb->cloned   = 0;
1232         skb->hdr_len  = 0;
1233         skb->nohdr    = 0;
1234         atomic_set(&skb_shinfo(skb)->dataref, 1);
1235         return 0;
1236
1237 nofrags:
1238         kfree(data);
1239 nodata:
1240         return -ENOMEM;
1241 }
1242 EXPORT_SYMBOL(pskb_expand_head);
1243
1244 /* Make private copy of skb with writable head and some headroom */
1245
1246 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1247 {
1248         struct sk_buff *skb2;
1249         int delta = headroom - skb_headroom(skb);
1250
1251         if (delta <= 0)
1252                 skb2 = pskb_copy(skb, GFP_ATOMIC);
1253         else {
1254                 skb2 = skb_clone(skb, GFP_ATOMIC);
1255                 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1256                                              GFP_ATOMIC)) {
1257                         kfree_skb(skb2);
1258                         skb2 = NULL;
1259                 }
1260         }
1261         return skb2;
1262 }
1263 EXPORT_SYMBOL(skb_realloc_headroom);
1264
1265 /**
1266  *      skb_copy_expand -       copy and expand sk_buff
1267  *      @skb: buffer to copy
1268  *      @newheadroom: new free bytes at head
1269  *      @newtailroom: new free bytes at tail
1270  *      @gfp_mask: allocation priority
1271  *
1272  *      Make a copy of both an &sk_buff and its data and while doing so
1273  *      allocate additional space.
1274  *
1275  *      This is used when the caller wishes to modify the data and needs a
1276  *      private copy of the data to alter as well as more space for new fields.
1277  *      Returns %NULL on failure or the pointer to the buffer
1278  *      on success. The returned buffer has a reference count of 1.
1279  *
1280  *      You must pass %GFP_ATOMIC as the allocation priority if this function
1281  *      is called from an interrupt.
1282  */
1283 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1284                                 int newheadroom, int newtailroom,
1285                                 gfp_t gfp_mask)
1286 {
1287         /*
1288          *      Allocate the copy buffer
1289          */
1290         struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1291                                         gfp_mask, skb_alloc_rx_flag(skb),
1292                                         NUMA_NO_NODE);
1293         int oldheadroom = skb_headroom(skb);
1294         int head_copy_len, head_copy_off;
1295
1296         if (!n)
1297                 return NULL;
1298
1299         skb_reserve(n, newheadroom);
1300
1301         /* Set the tail pointer and length */
1302         skb_put(n, skb->len);
1303
1304         head_copy_len = oldheadroom;
1305         head_copy_off = 0;
1306         if (newheadroom <= head_copy_len)
1307                 head_copy_len = newheadroom;
1308         else
1309                 head_copy_off = newheadroom - head_copy_len;
1310
1311         /* Copy the linear header and data. */
1312         if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1313                           skb->len + head_copy_len))
1314                 BUG();
1315
1316         copy_skb_header(n, skb);
1317
1318         skb_headers_offset_update(n, newheadroom - oldheadroom);
1319
1320         return n;
1321 }
1322 EXPORT_SYMBOL(skb_copy_expand);
1323
1324 /**
1325  *      skb_pad                 -       zero pad the tail of an skb
1326  *      @skb: buffer to pad
1327  *      @pad: space to pad
1328  *
1329  *      Ensure that a buffer is followed by a padding area that is zero
1330  *      filled. Used by network drivers which may DMA or transfer data
1331  *      beyond the buffer end onto the wire.
1332  *
1333  *      May return error in out of memory cases. The skb is freed on error.
1334  */
1335
1336 int skb_pad(struct sk_buff *skb, int pad)
1337 {
1338         int err;
1339         int ntail;
1340
1341         /* If the skbuff is non linear tailroom is always zero.. */
1342         if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1343                 memset(skb->data+skb->len, 0, pad);
1344                 return 0;
1345         }
1346
1347         ntail = skb->data_len + pad - (skb->end - skb->tail);
1348         if (likely(skb_cloned(skb) || ntail > 0)) {
1349                 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1350                 if (unlikely(err))
1351                         goto free_skb;
1352         }
1353
1354         /* FIXME: The use of this function with non-linear skb's really needs
1355          * to be audited.
1356          */
1357         err = skb_linearize(skb);
1358         if (unlikely(err))
1359                 goto free_skb;
1360
1361         memset(skb->data + skb->len, 0, pad);
1362         return 0;
1363
1364 free_skb:
1365         kfree_skb(skb);
1366         return err;
1367 }
1368 EXPORT_SYMBOL(skb_pad);
1369
1370 /**
1371  *      pskb_put - add data to the tail of a potentially fragmented buffer
1372  *      @skb: start of the buffer to use
1373  *      @tail: tail fragment of the buffer to use
1374  *      @len: amount of data to add
1375  *
1376  *      This function extends the used data area of the potentially
1377  *      fragmented buffer. @tail must be the last fragment of @skb -- or
1378  *      @skb itself. If this would exceed the total buffer size the kernel
1379  *      will panic. A pointer to the first byte of the extra data is
1380  *      returned.
1381  */
1382
1383 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1384 {
1385         if (tail != skb) {
1386                 skb->data_len += len;
1387                 skb->len += len;
1388         }
1389         return skb_put(tail, len);
1390 }
1391 EXPORT_SYMBOL_GPL(pskb_put);
1392
1393 /**
1394  *      skb_put - add data to a buffer
1395  *      @skb: buffer to use
1396  *      @len: amount of data to add
1397  *
1398  *      This function extends the used data area of the buffer. If this would
1399  *      exceed the total buffer size the kernel will panic. A pointer to the
1400  *      first byte of the extra data is returned.
1401  */
1402 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1403 {
1404         unsigned char *tmp = skb_tail_pointer(skb);
1405         SKB_LINEAR_ASSERT(skb);
1406         skb->tail += len;
1407         skb->len  += len;
1408         if (unlikely(skb->tail > skb->end))
1409                 skb_over_panic(skb, len, __builtin_return_address(0));
1410         return tmp;
1411 }
1412 EXPORT_SYMBOL(skb_put);
1413
1414 /**
1415  *      skb_push - add data to the start of a buffer
1416  *      @skb: buffer to use
1417  *      @len: amount of data to add
1418  *
1419  *      This function extends the used data area of the buffer at the buffer
1420  *      start. If this would exceed the total buffer headroom the kernel will
1421  *      panic. A pointer to the first byte of the extra data is returned.
1422  */
1423 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1424 {
1425         skb->data -= len;
1426         skb->len  += len;
1427         if (unlikely(skb->data<skb->head))
1428                 skb_under_panic(skb, len, __builtin_return_address(0));
1429         return skb->data;
1430 }
1431 EXPORT_SYMBOL(skb_push);
1432
1433 /**
1434  *      skb_pull - remove data from the start of a buffer
1435  *      @skb: buffer to use
1436  *      @len: amount of data to remove
1437  *
1438  *      This function removes data from the start of a buffer, returning
1439  *      the memory to the headroom. A pointer to the next data in the buffer
1440  *      is returned. Once the data has been pulled future pushes will overwrite
1441  *      the old data.
1442  */
1443 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1444 {
1445         return skb_pull_inline(skb, len);
1446 }
1447 EXPORT_SYMBOL(skb_pull);
1448
1449 /**
1450  *      skb_trim - remove end from a buffer
1451  *      @skb: buffer to alter
1452  *      @len: new length
1453  *
1454  *      Cut the length of a buffer down by removing data from the tail. If
1455  *      the buffer is already under the length specified it is not modified.
1456  *      The skb must be linear.
1457  */
1458 void skb_trim(struct sk_buff *skb, unsigned int len)
1459 {
1460         if (skb->len > len)
1461                 __skb_trim(skb, len);
1462 }
1463 EXPORT_SYMBOL(skb_trim);
1464
1465 /* Trims skb to length len. It can change skb pointers.
1466  */
1467
1468 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1469 {
1470         struct sk_buff **fragp;
1471         struct sk_buff *frag;
1472         int offset = skb_headlen(skb);
1473         int nfrags = skb_shinfo(skb)->nr_frags;
1474         int i;
1475         int err;
1476
1477         if (skb_cloned(skb) &&
1478             unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1479                 return err;
1480
1481         i = 0;
1482         if (offset >= len)
1483                 goto drop_pages;
1484
1485         for (; i < nfrags; i++) {
1486                 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1487
1488                 if (end < len) {
1489                         offset = end;
1490                         continue;
1491                 }
1492
1493                 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1494
1495 drop_pages:
1496                 skb_shinfo(skb)->nr_frags = i;
1497
1498                 for (; i < nfrags; i++)
1499                         skb_frag_unref(skb, i);
1500
1501                 if (skb_has_frag_list(skb))
1502                         skb_drop_fraglist(skb);
1503                 goto done;
1504         }
1505
1506         for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1507              fragp = &frag->next) {
1508                 int end = offset + frag->len;
1509
1510                 if (skb_shared(frag)) {
1511                         struct sk_buff *nfrag;
1512
1513                         nfrag = skb_clone(frag, GFP_ATOMIC);
1514                         if (unlikely(!nfrag))
1515                                 return -ENOMEM;
1516
1517                         nfrag->next = frag->next;
1518                         consume_skb(frag);
1519                         frag = nfrag;
1520                         *fragp = frag;
1521                 }
1522
1523                 if (end < len) {
1524                         offset = end;
1525                         continue;
1526                 }
1527
1528                 if (end > len &&
1529                     unlikely((err = pskb_trim(frag, len - offset))))
1530                         return err;
1531
1532                 if (frag->next)
1533                         skb_drop_list(&frag->next);
1534                 break;
1535         }
1536
1537 done:
1538         if (len > skb_headlen(skb)) {
1539                 skb->data_len -= skb->len - len;
1540                 skb->len       = len;
1541         } else {
1542                 skb->len       = len;
1543                 skb->data_len  = 0;
1544                 skb_set_tail_pointer(skb, len);
1545         }
1546
1547         return 0;
1548 }
1549 EXPORT_SYMBOL(___pskb_trim);
1550
1551 /**
1552  *      __pskb_pull_tail - advance tail of skb header
1553  *      @skb: buffer to reallocate
1554  *      @delta: number of bytes to advance tail
1555  *
1556  *      The function makes a sense only on a fragmented &sk_buff,
1557  *      it expands header moving its tail forward and copying necessary
1558  *      data from fragmented part.
1559  *
1560  *      &sk_buff MUST have reference count of 1.
1561  *
1562  *      Returns %NULL (and &sk_buff does not change) if pull failed
1563  *      or value of new tail of skb in the case of success.
1564  *
1565  *      All the pointers pointing into skb header may change and must be
1566  *      reloaded after call to this function.
1567  */
1568
1569 /* Moves tail of skb head forward, copying data from fragmented part,
1570  * when it is necessary.
1571  * 1. It may fail due to malloc failure.
1572  * 2. It may change skb pointers.
1573  *
1574  * It is pretty complicated. Luckily, it is called only in exceptional cases.
1575  */
1576 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1577 {
1578         /* If skb has not enough free space at tail, get new one
1579          * plus 128 bytes for future expansions. If we have enough
1580          * room at tail, reallocate without expansion only if skb is cloned.
1581          */
1582         int i, k, eat = (skb->tail + delta) - skb->end;
1583
1584         if (eat > 0 || skb_cloned(skb)) {
1585                 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1586                                      GFP_ATOMIC))
1587                         return NULL;
1588         }
1589
1590         if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1591                 BUG();
1592
1593         /* Optimization: no fragments, no reasons to preestimate
1594          * size of pulled pages. Superb.
1595          */
1596         if (!skb_has_frag_list(skb))
1597                 goto pull_pages;
1598
1599         /* Estimate size of pulled pages. */
1600         eat = delta;
1601         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1602                 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1603
1604                 if (size >= eat)
1605                         goto pull_pages;
1606                 eat -= size;
1607         }
1608
1609         /* If we need update frag list, we are in troubles.
1610          * Certainly, it possible to add an offset to skb data,
1611          * but taking into account that pulling is expected to
1612          * be very rare operation, it is worth to fight against
1613          * further bloating skb head and crucify ourselves here instead.
1614          * Pure masohism, indeed. 8)8)
1615          */
1616         if (eat) {
1617                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1618                 struct sk_buff *clone = NULL;
1619                 struct sk_buff *insp = NULL;
1620
1621                 do {
1622                         BUG_ON(!list);
1623
1624                         if (list->len <= eat) {
1625                                 /* Eaten as whole. */
1626                                 eat -= list->len;
1627                                 list = list->next;
1628                                 insp = list;
1629                         } else {
1630                                 /* Eaten partially. */
1631
1632                                 if (skb_shared(list)) {
1633                                         /* Sucks! We need to fork list. :-( */
1634                                         clone = skb_clone(list, GFP_ATOMIC);
1635                                         if (!clone)
1636                                                 return NULL;
1637                                         insp = list->next;
1638                                         list = clone;
1639                                 } else {
1640                                         /* This may be pulled without
1641                                          * problems. */
1642                                         insp = list;
1643                                 }
1644                                 if (!pskb_pull(list, eat)) {
1645                                         kfree_skb(clone);
1646                                         return NULL;
1647                                 }
1648                                 break;
1649                         }
1650                 } while (eat);
1651
1652                 /* Free pulled out fragments. */
1653                 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1654                         skb_shinfo(skb)->frag_list = list->next;
1655                         kfree_skb(list);
1656                 }
1657                 /* And insert new clone at head. */
1658                 if (clone) {
1659                         clone->next = list;
1660                         skb_shinfo(skb)->frag_list = clone;
1661                 }
1662         }
1663         /* Success! Now we may commit changes to skb data. */
1664
1665 pull_pages:
1666         eat = delta;
1667         k = 0;
1668         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1669                 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1670
1671                 if (size <= eat) {
1672                         skb_frag_unref(skb, i);
1673                         eat -= size;
1674                 } else {
1675                         skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1676                         if (eat) {
1677                                 skb_shinfo(skb)->frags[k].page_offset += eat;
1678                                 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1679                                 eat = 0;
1680                         }
1681                         k++;
1682                 }
1683         }
1684         skb_shinfo(skb)->nr_frags = k;
1685
1686         skb->tail     += delta;
1687         skb->data_len -= delta;
1688
1689         return skb_tail_pointer(skb);
1690 }
1691 EXPORT_SYMBOL(__pskb_pull_tail);
1692
1693 /**
1694  *      skb_copy_bits - copy bits from skb to kernel buffer
1695  *      @skb: source skb
1696  *      @offset: offset in source
1697  *      @to: destination buffer
1698  *      @len: number of bytes to copy
1699  *
1700  *      Copy the specified number of bytes from the source skb to the
1701  *      destination buffer.
1702  *
1703  *      CAUTION ! :
1704  *              If its prototype is ever changed,
1705  *              check arch/{*}/net/{*}.S files,
1706  *              since it is called from BPF assembly code.
1707  */
1708 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1709 {
1710         int start = skb_headlen(skb);
1711         struct sk_buff *frag_iter;
1712         int i, copy;
1713
1714         if (offset > (int)skb->len - len)
1715                 goto fault;
1716
1717         /* Copy header. */
1718         if ((copy = start - offset) > 0) {
1719                 if (copy > len)
1720                         copy = len;
1721                 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1722                 if ((len -= copy) == 0)
1723                         return 0;
1724                 offset += copy;
1725                 to     += copy;
1726         }
1727
1728         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1729                 int end;
1730                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1731
1732                 WARN_ON(start > offset + len);
1733
1734                 end = start + skb_frag_size(f);
1735                 if ((copy = end - offset) > 0) {
1736                         u8 *vaddr;
1737
1738                         if (copy > len)
1739                                 copy = len;
1740
1741                         vaddr = kmap_atomic(skb_frag_page(f));
1742                         memcpy(to,
1743                                vaddr + f->page_offset + offset - start,
1744                                copy);
1745                         kunmap_atomic(vaddr);
1746
1747                         if ((len -= copy) == 0)
1748                                 return 0;
1749                         offset += copy;
1750                         to     += copy;
1751                 }
1752                 start = end;
1753         }
1754
1755         skb_walk_frags(skb, frag_iter) {
1756                 int end;
1757
1758                 WARN_ON(start > offset + len);
1759
1760                 end = start + frag_iter->len;
1761                 if ((copy = end - offset) > 0) {
1762                         if (copy > len)
1763                                 copy = len;
1764                         if (skb_copy_bits(frag_iter, offset - start, to, copy))
1765                                 goto fault;
1766                         if ((len -= copy) == 0)
1767                                 return 0;
1768                         offset += copy;
1769                         to     += copy;
1770                 }
1771                 start = end;
1772         }
1773
1774         if (!len)
1775                 return 0;
1776
1777 fault:
1778         return -EFAULT;
1779 }
1780 EXPORT_SYMBOL(skb_copy_bits);
1781
1782 /*
1783  * Callback from splice_to_pipe(), if we need to release some pages
1784  * at the end of the spd in case we error'ed out in filling the pipe.
1785  */
1786 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1787 {
1788         put_page(spd->pages[i]);
1789 }
1790
1791 static struct page *linear_to_page(struct page *page, unsigned int *len,
1792                                    unsigned int *offset,
1793                                    struct sock *sk)
1794 {
1795         struct page_frag *pfrag = sk_page_frag(sk);
1796
1797         if (!sk_page_frag_refill(sk, pfrag))
1798                 return NULL;
1799
1800         *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1801
1802         memcpy(page_address(pfrag->page) + pfrag->offset,
1803                page_address(page) + *offset, *len);
1804         *offset = pfrag->offset;
1805         pfrag->offset += *len;
1806
1807         return pfrag->page;
1808 }
1809
1810 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1811                              struct page *page,
1812                              unsigned int offset)
1813 {
1814         return  spd->nr_pages &&
1815                 spd->pages[spd->nr_pages - 1] == page &&
1816                 (spd->partial[spd->nr_pages - 1].offset +
1817                  spd->partial[spd->nr_pages - 1].len == offset);
1818 }
1819
1820 /*
1821  * Fill page/offset/length into spd, if it can hold more pages.
1822  */
1823 static bool spd_fill_page(struct splice_pipe_desc *spd,
1824                           struct pipe_inode_info *pipe, struct page *page,
1825                           unsigned int *len, unsigned int offset,
1826                           bool linear,
1827                           struct sock *sk)
1828 {
1829         if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1830                 return true;
1831
1832         if (linear) {
1833                 page = linear_to_page(page, len, &offset, sk);
1834                 if (!page)
1835                         return true;
1836         }
1837         if (spd_can_coalesce(spd, page, offset)) {
1838                 spd->partial[spd->nr_pages - 1].len += *len;
1839                 return false;
1840         }
1841         get_page(page);
1842         spd->pages[spd->nr_pages] = page;
1843         spd->partial[spd->nr_pages].len = *len;
1844         spd->partial[spd->nr_pages].offset = offset;
1845         spd->nr_pages++;
1846
1847         return false;
1848 }
1849
1850 static bool __splice_segment(struct page *page, unsigned int poff,
1851                              unsigned int plen, unsigned int *off,
1852                              unsigned int *len,
1853                              struct splice_pipe_desc *spd, bool linear,
1854                              struct sock *sk,
1855                              struct pipe_inode_info *pipe)
1856 {
1857         if (!*len)
1858                 return true;
1859
1860         /* skip this segment if already processed */
1861         if (*off >= plen) {
1862                 *off -= plen;
1863                 return false;
1864         }
1865
1866         /* ignore any bits we already processed */
1867         poff += *off;
1868         plen -= *off;
1869         *off = 0;
1870
1871         do {
1872                 unsigned int flen = min(*len, plen);
1873
1874                 if (spd_fill_page(spd, pipe, page, &flen, poff,
1875                                   linear, sk))
1876                         return true;
1877                 poff += flen;
1878                 plen -= flen;
1879                 *len -= flen;
1880         } while (*len && plen);
1881
1882         return false;
1883 }
1884
1885 /*
1886  * Map linear and fragment data from the skb to spd. It reports true if the
1887  * pipe is full or if we already spliced the requested length.
1888  */
1889 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1890                               unsigned int *offset, unsigned int *len,
1891                               struct splice_pipe_desc *spd, struct sock *sk)
1892 {
1893         int seg;
1894
1895         /* map the linear part :
1896          * If skb->head_frag is set, this 'linear' part is backed by a
1897          * fragment, and if the head is not shared with any clones then
1898          * we can avoid a copy since we own the head portion of this page.
1899          */
1900         if (__splice_segment(virt_to_page(skb->data),
1901                              (unsigned long) skb->data & (PAGE_SIZE - 1),
1902                              skb_headlen(skb),
1903                              offset, len, spd,
1904                              skb_head_is_locked(skb),
1905                              sk, pipe))
1906                 return true;
1907
1908         /*
1909          * then map the fragments
1910          */
1911         for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1912                 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1913
1914                 if (__splice_segment(skb_frag_page(f),
1915                                      f->page_offset, skb_frag_size(f),
1916                                      offset, len, spd, false, sk, pipe))
1917                         return true;
1918         }
1919
1920         return false;
1921 }
1922
1923 /*
1924  * Map data from the skb to a pipe. Should handle both the linear part,
1925  * the fragments, and the frag list. It does NOT handle frag lists within
1926  * the frag list, if such a thing exists. We'd probably need to recurse to
1927  * handle that cleanly.
1928  */
1929 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1930                     struct pipe_inode_info *pipe, unsigned int tlen,
1931                     unsigned int flags)
1932 {
1933         struct partial_page partial[MAX_SKB_FRAGS];
1934         struct page *pages[MAX_SKB_FRAGS];
1935         struct splice_pipe_desc spd = {
1936                 .pages = pages,
1937                 .partial = partial,
1938                 .nr_pages_max = MAX_SKB_FRAGS,
1939                 .flags = flags,
1940                 .ops = &nosteal_pipe_buf_ops,
1941                 .spd_release = sock_spd_release,
1942         };
1943         struct sk_buff *frag_iter;
1944         struct sock *sk = skb->sk;
1945         int ret = 0;
1946
1947         /*
1948          * __skb_splice_bits() only fails if the output has no room left,
1949          * so no point in going over the frag_list for the error case.
1950          */
1951         if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1952                 goto done;
1953         else if (!tlen)
1954                 goto done;
1955
1956         /*
1957          * now see if we have a frag_list to map
1958          */
1959         skb_walk_frags(skb, frag_iter) {
1960                 if (!tlen)
1961                         break;
1962                 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1963                         break;
1964         }
1965
1966 done:
1967         if (spd.nr_pages) {
1968                 /*
1969                  * Drop the socket lock, otherwise we have reverse
1970                  * locking dependencies between sk_lock and i_mutex
1971                  * here as compared to sendfile(). We enter here
1972                  * with the socket lock held, and splice_to_pipe() will
1973                  * grab the pipe inode lock. For sendfile() emulation,
1974                  * we call into ->sendpage() with the i_mutex lock held
1975                  * and networking will grab the socket lock.
1976                  */
1977                 release_sock(sk);
1978                 ret = splice_to_pipe(pipe, &spd);
1979                 lock_sock(sk);
1980         }
1981
1982         return ret;
1983 }
1984
1985 /**
1986  *      skb_store_bits - store bits from kernel buffer to skb
1987  *      @skb: destination buffer
1988  *      @offset: offset in destination
1989  *      @from: source buffer
1990  *      @len: number of bytes to copy
1991  *
1992  *      Copy the specified number of bytes from the source buffer to the
1993  *      destination skb.  This function handles all the messy bits of
1994  *      traversing fragment lists and such.
1995  */
1996
1997 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1998 {
1999         int start = skb_headlen(skb);
2000         struct sk_buff *frag_iter;
2001         int i, copy;
2002
2003         if (offset > (int)skb->len - len)
2004                 goto fault;
2005
2006         if ((copy = start - offset) > 0) {
2007                 if (copy > len)
2008                         copy = len;
2009                 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2010                 if ((len -= copy) == 0)
2011                         return 0;
2012                 offset += copy;
2013                 from += copy;
2014         }
2015
2016         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2017                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2018                 int end;
2019
2020                 WARN_ON(start > offset + len);
2021
2022                 end = start + skb_frag_size(frag);
2023                 if ((copy = end - offset) > 0) {
2024                         u8 *vaddr;
2025
2026                         if (copy > len)
2027                                 copy = len;
2028
2029                         vaddr = kmap_atomic(skb_frag_page(frag));
2030                         memcpy(vaddr + frag->page_offset + offset - start,
2031                                from, copy);
2032                         kunmap_atomic(vaddr);
2033
2034                         if ((len -= copy) == 0)
2035                                 return 0;
2036                         offset += copy;
2037                         from += copy;
2038                 }
2039                 start = end;
2040         }
2041
2042         skb_walk_frags(skb, frag_iter) {
2043                 int end;
2044
2045                 WARN_ON(start > offset + len);
2046
2047                 end = start + frag_iter->len;
2048                 if ((copy = end - offset) > 0) {
2049                         if (copy > len)
2050                                 copy = len;
2051                         if (skb_store_bits(frag_iter, offset - start,
2052                                            from, copy))
2053                                 goto fault;
2054                         if ((len -= copy) == 0)
2055                                 return 0;
2056                         offset += copy;
2057                         from += copy;
2058                 }
2059                 start = end;
2060         }
2061         if (!len)
2062                 return 0;
2063
2064 fault:
2065         return -EFAULT;
2066 }
2067 EXPORT_SYMBOL(skb_store_bits);
2068
2069 /* Checksum skb data. */
2070 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2071                       __wsum csum, const struct skb_checksum_ops *ops)
2072 {
2073         int start = skb_headlen(skb);
2074         int i, copy = start - offset;
2075         struct sk_buff *frag_iter;
2076         int pos = 0;
2077
2078         /* Checksum header. */
2079         if (copy > 0) {
2080                 if (copy > len)
2081                         copy = len;
2082                 csum = ops->update(skb->data + offset, copy, csum);
2083                 if ((len -= copy) == 0)
2084                         return csum;
2085                 offset += copy;
2086                 pos     = copy;
2087         }
2088
2089         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2090                 int end;
2091                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2092
2093                 WARN_ON(start > offset + len);
2094
2095                 end = start + skb_frag_size(frag);
2096                 if ((copy = end - offset) > 0) {
2097                         __wsum csum2;
2098                         u8 *vaddr;
2099
2100                         if (copy > len)
2101                                 copy = len;
2102                         vaddr = kmap_atomic(skb_frag_page(frag));
2103                         csum2 = ops->update(vaddr + frag->page_offset +
2104                                             offset - start, copy, 0);
2105                         kunmap_atomic(vaddr);
2106                         csum = ops->combine(csum, csum2, pos, copy);
2107                         if (!(len -= copy))
2108                                 return csum;
2109                         offset += copy;
2110                         pos    += copy;
2111                 }
2112                 start = end;
2113         }
2114
2115         skb_walk_frags(skb, frag_iter) {
2116                 int end;
2117
2118                 WARN_ON(start > offset + len);
2119
2120                 end = start + frag_iter->len;
2121                 if ((copy = end - offset) > 0) {
2122                         __wsum csum2;
2123                         if (copy > len)
2124                                 copy = len;
2125                         csum2 = __skb_checksum(frag_iter, offset - start,
2126                                                copy, 0, ops);
2127                         csum = ops->combine(csum, csum2, pos, copy);
2128                         if ((len -= copy) == 0)
2129                                 return csum;
2130                         offset += copy;
2131                         pos    += copy;
2132                 }
2133                 start = end;
2134         }
2135         BUG_ON(len);
2136
2137         return csum;
2138 }
2139 EXPORT_SYMBOL(__skb_checksum);
2140
2141 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2142                     int len, __wsum csum)
2143 {
2144         const struct skb_checksum_ops ops = {
2145                 .update  = csum_partial_ext,
2146                 .combine = csum_block_add_ext,
2147         };
2148
2149         return __skb_checksum(skb, offset, len, csum, &ops);
2150 }
2151 EXPORT_SYMBOL(skb_checksum);
2152
2153 /* Both of above in one bottle. */
2154
2155 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2156                                     u8 *to, int len, __wsum csum)
2157 {
2158         int start = skb_headlen(skb);
2159         int i, copy = start - offset;
2160         struct sk_buff *frag_iter;
2161         int pos = 0;
2162
2163         /* Copy header. */
2164         if (copy > 0) {
2165                 if (copy > len)
2166                         copy = len;
2167                 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2168                                                  copy, csum);
2169                 if ((len -= copy) == 0)
2170                         return csum;
2171                 offset += copy;
2172                 to     += copy;
2173                 pos     = copy;
2174         }
2175
2176         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2177                 int end;
2178
2179                 WARN_ON(start > offset + len);
2180
2181                 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2182                 if ((copy = end - offset) > 0) {
2183                         __wsum csum2;
2184                         u8 *vaddr;
2185                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2186
2187                         if (copy > len)
2188                                 copy = len;
2189                         vaddr = kmap_atomic(skb_frag_page(frag));
2190                         csum2 = csum_partial_copy_nocheck(vaddr +
2191                                                           frag->page_offset +
2192                                                           offset - start, to,
2193                                                           copy, 0);
2194                         kunmap_atomic(vaddr);
2195                         csum = csum_block_add(csum, csum2, pos);
2196                         if (!(len -= copy))
2197                                 return csum;
2198                         offset += copy;
2199                         to     += copy;
2200                         pos    += copy;
2201                 }
2202                 start = end;
2203         }
2204
2205         skb_walk_frags(skb, frag_iter) {
2206                 __wsum csum2;
2207                 int end;
2208
2209                 WARN_ON(start > offset + len);
2210
2211                 end = start + frag_iter->len;
2212                 if ((copy = end - offset) > 0) {
2213                         if (copy > len)
2214                                 copy = len;
2215                         csum2 = skb_copy_and_csum_bits(frag_iter,
2216                                                        offset - start,
2217                                                        to, copy, 0);
2218                         csum = csum_block_add(csum, csum2, pos);
2219                         if ((len -= copy) == 0)
2220                                 return csum;
2221                         offset += copy;
2222                         to     += copy;
2223                         pos    += copy;
2224                 }
2225                 start = end;
2226         }
2227         BUG_ON(len);
2228         return csum;
2229 }
2230 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2231
2232  /**
2233  *      skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2234  *      @from: source buffer
2235  *
2236  *      Calculates the amount of linear headroom needed in the 'to' skb passed
2237  *      into skb_zerocopy().
2238  */
2239 unsigned int
2240 skb_zerocopy_headlen(const struct sk_buff *from)
2241 {
2242         unsigned int hlen = 0;
2243
2244         if (!from->head_frag ||
2245             skb_headlen(from) < L1_CACHE_BYTES ||
2246             skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2247                 hlen = skb_headlen(from);
2248
2249         if (skb_has_frag_list(from))
2250                 hlen = from->len;
2251
2252         return hlen;
2253 }
2254 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2255
2256 /**
2257  *      skb_zerocopy - Zero copy skb to skb
2258  *      @to: destination buffer
2259  *      @from: source buffer
2260  *      @len: number of bytes to copy from source buffer
2261  *      @hlen: size of linear headroom in destination buffer
2262  *
2263  *      Copies up to `len` bytes from `from` to `to` by creating references
2264  *      to the frags in the source buffer.
2265  *
2266  *      The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2267  *      headroom in the `to` buffer.
2268  *
2269  *      Return value:
2270  *      0: everything is OK
2271  *      -ENOMEM: couldn't orphan frags of @from due to lack of memory
2272  *      -EFAULT: skb_copy_bits() found some problem with skb geometry
2273  */
2274 int
2275 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2276 {
2277         int i, j = 0;
2278         int plen = 0; /* length of skb->head fragment */
2279         int ret;
2280         struct page *page;
2281         unsigned int offset;
2282
2283         BUG_ON(!from->head_frag && !hlen);
2284
2285         /* dont bother with small payloads */
2286         if (len <= skb_tailroom(to))
2287                 return skb_copy_bits(from, 0, skb_put(to, len), len);
2288
2289         if (hlen) {
2290                 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2291                 if (unlikely(ret))
2292                         return ret;
2293                 len -= hlen;
2294         } else {
2295                 plen = min_t(int, skb_headlen(from), len);
2296                 if (plen) {
2297                         page = virt_to_head_page(from->head);
2298                         offset = from->data - (unsigned char *)page_address(page);
2299                         __skb_fill_page_desc(to, 0, page, offset, plen);
2300                         get_page(page);
2301                         j = 1;
2302                         len -= plen;
2303                 }
2304         }
2305
2306         to->truesize += len + plen;
2307         to->len += len + plen;
2308         to->data_len += len + plen;
2309
2310         if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2311                 skb_tx_error(from);
2312                 return -ENOMEM;
2313         }
2314
2315         for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2316                 if (!len)
2317                         break;
2318                 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2319                 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2320                 len -= skb_shinfo(to)->frags[j].size;
2321                 skb_frag_ref(to, j);
2322                 j++;
2323         }
2324         skb_shinfo(to)->nr_frags = j;
2325
2326         return 0;
2327 }
2328 EXPORT_SYMBOL_GPL(skb_zerocopy);
2329
2330 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2331 {
2332         __wsum csum;
2333         long csstart;
2334
2335         if (skb->ip_summed == CHECKSUM_PARTIAL)
2336                 csstart = skb_checksum_start_offset(skb);
2337         else
2338                 csstart = skb_headlen(skb);
2339
2340         BUG_ON(csstart > skb_headlen(skb));
2341
2342         skb_copy_from_linear_data(skb, to, csstart);
2343
2344         csum = 0;
2345         if (csstart != skb->len)
2346                 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2347                                               skb->len - csstart, 0);
2348
2349         if (skb->ip_summed == CHECKSUM_PARTIAL) {
2350                 long csstuff = csstart + skb->csum_offset;
2351
2352                 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2353         }
2354 }
2355 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2356
2357 /**
2358  *      skb_dequeue - remove from the head of the queue
2359  *      @list: list to dequeue from
2360  *
2361  *      Remove the head of the list. The list lock is taken so the function
2362  *      may be used safely with other locking list functions. The head item is
2363  *      returned or %NULL if the list is empty.
2364  */
2365
2366 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2367 {
2368         unsigned long flags;
2369         struct sk_buff *result;
2370
2371         spin_lock_irqsave(&list->lock, flags);
2372         result = __skb_dequeue(list);
2373         spin_unlock_irqrestore(&list->lock, flags);
2374         return result;
2375 }
2376 EXPORT_SYMBOL(skb_dequeue);
2377
2378 /**
2379  *      skb_dequeue_tail - remove from the tail of the queue
2380  *      @list: list to dequeue from
2381  *
2382  *      Remove the tail of the list. The list lock is taken so the function
2383  *      may be used safely with other locking list functions. The tail item is
2384  *      returned or %NULL if the list is empty.
2385  */
2386 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2387 {
2388         unsigned long flags;
2389         struct sk_buff *result;
2390
2391         spin_lock_irqsave(&list->lock, flags);
2392         result = __skb_dequeue_tail(list);
2393         spin_unlock_irqrestore(&list->lock, flags);
2394         return result;
2395 }
2396 EXPORT_SYMBOL(skb_dequeue_tail);
2397
2398 /**
2399  *      skb_queue_purge - empty a list
2400  *      @list: list to empty
2401  *
2402  *      Delete all buffers on an &sk_buff list. Each buffer is removed from
2403  *      the list and one reference dropped. This function takes the list
2404  *      lock and is atomic with respect to other list locking functions.
2405  */
2406 void skb_queue_purge(struct sk_buff_head *list)
2407 {
2408         struct sk_buff *skb;
2409         while ((skb = skb_dequeue(list)) != NULL)
2410                 kfree_skb(skb);
2411 }
2412 EXPORT_SYMBOL(skb_queue_purge);
2413
2414 /**
2415  *      skb_queue_head - queue a buffer at the list head
2416  *      @list: list to use
2417  *      @newsk: buffer to queue
2418  *
2419  *      Queue a buffer at the start of the list. This function takes the
2420  *      list lock and can be used safely with other locking &sk_buff functions
2421  *      safely.
2422  *
2423  *      A buffer cannot be placed on two lists at the same time.
2424  */
2425 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2426 {
2427         unsigned long flags;
2428
2429         spin_lock_irqsave(&list->lock, flags);
2430         __skb_queue_head(list, newsk);
2431         spin_unlock_irqrestore(&list->lock, flags);
2432 }
2433 EXPORT_SYMBOL(skb_queue_head);
2434
2435 /**
2436  *      skb_queue_tail - queue a buffer at the list tail
2437  *      @list: list to use
2438  *      @newsk: buffer to queue
2439  *
2440  *      Queue a buffer at the tail of the list. This function takes the
2441  *      list lock and can be used safely with other locking &sk_buff functions
2442  *      safely.
2443  *
2444  *      A buffer cannot be placed on two lists at the same time.
2445  */
2446 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2447 {
2448         unsigned long flags;
2449
2450         spin_lock_irqsave(&list->lock, flags);
2451         __skb_queue_tail(list, newsk);
2452         spin_unlock_irqrestore(&list->lock, flags);
2453 }
2454 EXPORT_SYMBOL(skb_queue_tail);
2455
2456 /**
2457  *      skb_unlink      -       remove a buffer from a list
2458  *      @skb: buffer to remove
2459  *      @list: list to use
2460  *
2461  *      Remove a packet from a list. The list locks are taken and this
2462  *      function is atomic with respect to other list locked calls
2463  *
2464  *      You must know what list the SKB is on.
2465  */
2466 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2467 {
2468         unsigned long flags;
2469
2470         spin_lock_irqsave(&list->lock, flags);
2471         __skb_unlink(skb, list);
2472         spin_unlock_irqrestore(&list->lock, flags);
2473 }
2474 EXPORT_SYMBOL(skb_unlink);
2475
2476 /**
2477  *      skb_append      -       append a buffer
2478  *      @old: buffer to insert after
2479  *      @newsk: buffer to insert
2480  *      @list: list to use
2481  *
2482  *      Place a packet after a given packet in a list. The list locks are taken
2483  *      and this function is atomic with respect to other list locked calls.
2484  *      A buffer cannot be placed on two lists at the same time.
2485  */
2486 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2487 {
2488         unsigned long flags;
2489
2490         spin_lock_irqsave(&list->lock, flags);
2491         __skb_queue_after(list, old, newsk);
2492         spin_unlock_irqrestore(&list->lock, flags);
2493 }
2494 EXPORT_SYMBOL(skb_append);
2495
2496 /**
2497  *      skb_insert      -       insert a buffer
2498  *      @old: buffer to insert before
2499  *      @newsk: buffer to insert
2500  *      @list: list to use
2501  *
2502  *      Place a packet before a given packet in a list. The list locks are
2503  *      taken and this function is atomic with respect to other list locked
2504  *      calls.
2505  *
2506  *      A buffer cannot be placed on two lists at the same time.
2507  */
2508 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2509 {
2510         unsigned long flags;
2511
2512         spin_lock_irqsave(&list->lock, flags);
2513         __skb_insert(newsk, old->prev, old, list);
2514         spin_unlock_irqrestore(&list->lock, flags);
2515 }
2516 EXPORT_SYMBOL(skb_insert);
2517
2518 static inline void skb_split_inside_header(struct sk_buff *skb,
2519                                            struct sk_buff* skb1,
2520                                            const u32 len, const int pos)
2521 {
2522         int i;
2523
2524         skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2525                                          pos - len);
2526         /* And move data appendix as is. */
2527         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2528                 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2529
2530         skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2531         skb_shinfo(skb)->nr_frags  = 0;
2532         skb1->data_len             = skb->data_len;
2533         skb1->len                  += skb1->data_len;
2534         skb->data_len              = 0;
2535         skb->len                   = len;
2536         skb_set_tail_pointer(skb, len);
2537 }
2538
2539 static inline void skb_split_no_header(struct sk_buff *skb,
2540                                        struct sk_buff* skb1,
2541                                        const u32 len, int pos)
2542 {
2543         int i, k = 0;
2544         const int nfrags = skb_shinfo(skb)->nr_frags;
2545
2546         skb_shinfo(skb)->nr_frags = 0;
2547         skb1->len                 = skb1->data_len = skb->len - len;
2548         skb->len                  = len;
2549         skb->data_len             = len - pos;
2550
2551         for (i = 0; i < nfrags; i++) {
2552                 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2553
2554                 if (pos + size > len) {
2555                         skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2556
2557                         if (pos < len) {
2558                                 /* Split frag.
2559                                  * We have two variants in this case:
2560                                  * 1. Move all the frag to the second
2561                                  *    part, if it is possible. F.e.
2562                                  *    this approach is mandatory for TUX,
2563                                  *    where splitting is expensive.
2564                                  * 2. Split is accurately. We make this.
2565                                  */
2566                                 skb_frag_ref(skb, i);
2567                                 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2568                                 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2569                                 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2570                                 skb_shinfo(skb)->nr_frags++;
2571                         }
2572                         k++;
2573                 } else
2574                         skb_shinfo(skb)->nr_frags++;
2575                 pos += size;
2576         }
2577         skb_shinfo(skb1)->nr_frags = k;
2578 }
2579
2580 /**
2581  * skb_split - Split fragmented skb to two parts at length len.
2582  * @skb: the buffer to split
2583  * @skb1: the buffer to receive the second part
2584  * @len: new length for skb
2585  */
2586 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2587 {
2588         int pos = skb_headlen(skb);
2589
2590         skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2591         if (len < pos)  /* Split line is inside header. */
2592                 skb_split_inside_header(skb, skb1, len, pos);
2593         else            /* Second chunk has no header, nothing to copy. */
2594                 skb_split_no_header(skb, skb1, len, pos);
2595 }
2596 EXPORT_SYMBOL(skb_split);
2597
2598 /* Shifting from/to a cloned skb is a no-go.
2599  *
2600  * Caller cannot keep skb_shinfo related pointers past calling here!
2601  */
2602 static int skb_prepare_for_shift(struct sk_buff *skb)
2603 {
2604         return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2605 }
2606
2607 /**
2608  * skb_shift - Shifts paged data partially from skb to another
2609  * @tgt: buffer into which tail data gets added
2610  * @skb: buffer from which the paged data comes from
2611  * @shiftlen: shift up to this many bytes
2612  *
2613  * Attempts to shift up to shiftlen worth of bytes, which may be less than
2614  * the length of the skb, from skb to tgt. Returns number bytes shifted.
2615  * It's up to caller to free skb if everything was shifted.
2616  *
2617  * If @tgt runs out of frags, the whole operation is aborted.
2618  *
2619  * Skb cannot include anything else but paged data while tgt is allowed
2620  * to have non-paged data as well.
2621  *
2622  * TODO: full sized shift could be optimized but that would need
2623  * specialized skb free'er to handle frags without up-to-date nr_frags.
2624  */
2625 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2626 {
2627         int from, to, merge, todo;
2628         struct skb_frag_struct *fragfrom, *fragto;
2629
2630         BUG_ON(shiftlen > skb->len);
2631         BUG_ON(skb_headlen(skb));       /* Would corrupt stream */
2632
2633         todo = shiftlen;
2634         from = 0;
2635         to = skb_shinfo(tgt)->nr_frags;
2636         fragfrom = &skb_shinfo(skb)->frags[from];
2637
2638         /* Actual merge is delayed until the point when we know we can
2639          * commit all, so that we don't have to undo partial changes
2640          */
2641         if (!to ||
2642             !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2643                               fragfrom->page_offset)) {
2644                 merge = -1;
2645         } else {
2646                 merge = to - 1;
2647
2648                 todo -= skb_frag_size(fragfrom);
2649                 if (todo < 0) {
2650                         if (skb_prepare_for_shift(skb) ||
2651                             skb_prepare_for_shift(tgt))
2652                                 return 0;
2653
2654                         /* All previous frag pointers might be stale! */
2655                         fragfrom = &skb_shinfo(skb)->frags[from];
2656                         fragto = &skb_shinfo(tgt)->frags[merge];
2657
2658                         skb_frag_size_add(fragto, shiftlen);
2659                         skb_frag_size_sub(fragfrom, shiftlen);
2660                         fragfrom->page_offset += shiftlen;
2661
2662                         goto onlymerged;
2663                 }
2664
2665                 from++;
2666         }
2667
2668         /* Skip full, not-fitting skb to avoid expensive operations */
2669         if ((shiftlen == skb->len) &&
2670             (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2671                 return 0;
2672
2673         if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2674                 return 0;
2675
2676         while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2677                 if (to == MAX_SKB_FRAGS)
2678                         return 0;
2679
2680                 fragfrom = &skb_shinfo(skb)->frags[from];
2681                 fragto = &skb_shinfo(tgt)->frags[to];
2682
2683                 if (todo >= skb_frag_size(fragfrom)) {
2684                         *fragto = *fragfrom;
2685                         todo -= skb_frag_size(fragfrom);
2686                         from++;
2687                         to++;
2688
2689                 } else {
2690                         __skb_frag_ref(fragfrom);
2691                         fragto->page = fragfrom->page;
2692                         fragto->page_offset = fragfrom->page_offset;
2693                         skb_frag_size_set(fragto, todo);
2694
2695                         fragfrom->page_offset += todo;
2696                         skb_frag_size_sub(fragfrom, todo);
2697                         todo = 0;
2698
2699                         to++;
2700                         break;
2701                 }
2702         }
2703
2704         /* Ready to "commit" this state change to tgt */
2705         skb_shinfo(tgt)->nr_frags = to;
2706
2707         if (merge >= 0) {
2708                 fragfrom = &skb_shinfo(skb)->frags[0];
2709                 fragto = &skb_shinfo(tgt)->frags[merge];
2710
2711                 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2712                 __skb_frag_unref(fragfrom);
2713         }
2714
2715         /* Reposition in the original skb */
2716         to = 0;
2717         while (from < skb_shinfo(skb)->nr_frags)
2718                 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2719         skb_shinfo(skb)->nr_frags = to;
2720
2721         BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2722
2723 onlymerged:
2724         /* Most likely the tgt won't ever need its checksum anymore, skb on
2725          * the other hand might need it if it needs to be resent
2726          */
2727         tgt->ip_summed = CHECKSUM_PARTIAL;
2728         skb->ip_summed = CHECKSUM_PARTIAL;
2729
2730         /* Yak, is it really working this way? Some helper please? */
2731         skb->len -= shiftlen;
2732         skb->data_len -= shiftlen;
2733         skb->truesize -= shiftlen;
2734         tgt->len += shiftlen;
2735         tgt->data_len += shiftlen;
2736         tgt->truesize += shiftlen;
2737
2738         return shiftlen;
2739 }
2740
2741 /**
2742  * skb_prepare_seq_read - Prepare a sequential read of skb data
2743  * @skb: the buffer to read
2744  * @from: lower offset of data to be read
2745  * @to: upper offset of data to be read
2746  * @st: state variable
2747  *
2748  * Initializes the specified state variable. Must be called before
2749  * invoking skb_seq_read() for the first time.
2750  */
2751 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2752                           unsigned int to, struct skb_seq_state *st)
2753 {
2754         st->lower_offset = from;
2755         st->upper_offset = to;
2756         st->root_skb = st->cur_skb = skb;
2757         st->frag_idx = st->stepped_offset = 0;
2758         st->frag_data = NULL;
2759 }
2760 EXPORT_SYMBOL(skb_prepare_seq_read);
2761
2762 /**
2763  * skb_seq_read - Sequentially read skb data
2764  * @consumed: number of bytes consumed by the caller so far
2765  * @data: destination pointer for data to be returned
2766  * @st: state variable
2767  *
2768  * Reads a block of skb data at @consumed relative to the
2769  * lower offset specified to skb_prepare_seq_read(). Assigns
2770  * the head of the data block to @data and returns the length
2771  * of the block or 0 if the end of the skb data or the upper
2772  * offset has been reached.
2773  *
2774  * The caller is not required to consume all of the data
2775  * returned, i.e. @consumed is typically set to the number
2776  * of bytes already consumed and the next call to
2777  * skb_seq_read() will return the remaining part of the block.
2778  *
2779  * Note 1: The size of each block of data returned can be arbitrary,
2780  *       this limitation is the cost for zerocopy sequential
2781  *       reads of potentially non linear data.
2782  *
2783  * Note 2: Fragment lists within fragments are not implemented
2784  *       at the moment, state->root_skb could be replaced with
2785  *       a stack for this purpose.
2786  */
2787 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2788                           struct skb_seq_state *st)
2789 {
2790         unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2791         skb_frag_t *frag;
2792
2793         if (unlikely(abs_offset >= st->upper_offset)) {
2794                 if (st->frag_data) {
2795                         kunmap_atomic(st->frag_data);
2796                         st->frag_data = NULL;
2797                 }
2798                 return 0;
2799         }
2800
2801 next_skb:
2802         block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2803
2804         if (abs_offset < block_limit && !st->frag_data) {
2805                 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2806                 return block_limit - abs_offset;
2807         }
2808
2809         if (st->frag_idx == 0 && !st->frag_data)
2810                 st->stepped_offset += skb_headlen(st->cur_skb);
2811
2812         while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2813                 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2814                 block_limit = skb_frag_size(frag) + st->stepped_offset;
2815
2816                 if (abs_offset < block_limit) {
2817                         if (!st->frag_data)
2818                                 st->frag_data = kmap_atomic(skb_frag_page(frag));
2819
2820                         *data = (u8 *) st->frag_data + frag->page_offset +
2821                                 (abs_offset - st->stepped_offset);
2822
2823                         return block_limit - abs_offset;
2824                 }
2825
2826                 if (st->frag_data) {
2827                         kunmap_atomic(st->frag_data);
2828                         st->frag_data = NULL;
2829                 }
2830
2831                 st->frag_idx++;
2832                 st->stepped_offset += skb_frag_size(frag);
2833         }
2834
2835         if (st->frag_data) {
2836                 kunmap_atomic(st->frag_data);
2837                 st->frag_data = NULL;
2838         }
2839
2840         if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2841                 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2842                 st->frag_idx = 0;
2843                 goto next_skb;
2844         } else if (st->cur_skb->next) {
2845                 st->cur_skb = st->cur_skb->next;
2846                 st->frag_idx = 0;
2847                 goto next_skb;
2848         }
2849
2850         return 0;
2851 }
2852 EXPORT_SYMBOL(skb_seq_read);
2853
2854 /**
2855  * skb_abort_seq_read - Abort a sequential read of skb data
2856  * @st: state variable
2857  *
2858  * Must be called if skb_seq_read() was not called until it
2859  * returned 0.
2860  */
2861 void skb_abort_seq_read(struct skb_seq_state *st)
2862 {
2863         if (st->frag_data)
2864                 kunmap_atomic(st->frag_data);
2865 }
2866 EXPORT_SYMBOL(skb_abort_seq_read);
2867
2868 #define TS_SKB_CB(state)        ((struct skb_seq_state *) &((state)->cb))
2869
2870 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2871                                           struct ts_config *conf,
2872                                           struct ts_state *state)
2873 {
2874         return skb_seq_read(offset, text, TS_SKB_CB(state));
2875 }
2876
2877 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2878 {
2879         skb_abort_seq_read(TS_SKB_CB(state));
2880 }
2881
2882 /**
2883  * skb_find_text - Find a text pattern in skb data
2884  * @skb: the buffer to look in
2885  * @from: search offset
2886  * @to: search limit
2887  * @config: textsearch configuration
2888  *
2889  * Finds a pattern in the skb data according to the specified
2890  * textsearch configuration. Use textsearch_next() to retrieve
2891  * subsequent occurrences of the pattern. Returns the offset
2892  * to the first occurrence or UINT_MAX if no match was found.
2893  */
2894 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2895                            unsigned int to, struct ts_config *config)
2896 {
2897         struct ts_state state;
2898         unsigned int ret;
2899
2900         config->get_next_block = skb_ts_get_next_block;
2901         config->finish = skb_ts_finish;
2902
2903         skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2904
2905         ret = textsearch_find(config, &state);
2906         return (ret <= to - from ? ret : UINT_MAX);
2907 }
2908 EXPORT_SYMBOL(skb_find_text);
2909
2910 /**
2911  * skb_append_datato_frags - append the user data to a skb
2912  * @sk: sock  structure
2913  * @skb: skb structure to be appended with user data.
2914  * @getfrag: call back function to be used for getting the user data
2915  * @from: pointer to user message iov
2916  * @length: length of the iov message
2917  *
2918  * Description: This procedure append the user data in the fragment part
2919  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2920  */
2921 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2922                         int (*getfrag)(void *from, char *to, int offset,
2923                                         int len, int odd, struct sk_buff *skb),
2924                         void *from, int length)
2925 {
2926         int frg_cnt = skb_shinfo(skb)->nr_frags;
2927         int copy;
2928         int offset = 0;
2929         int ret;
2930         struct page_frag *pfrag = &current->task_frag;
2931
2932         do {
2933                 /* Return error if we don't have space for new frag */
2934                 if (frg_cnt >= MAX_SKB_FRAGS)
2935                         return -EMSGSIZE;
2936
2937                 if (!sk_page_frag_refill(sk, pfrag))
2938                         return -ENOMEM;
2939
2940                 /* copy the user data to page */
2941                 copy = min_t(int, length, pfrag->size - pfrag->offset);
2942
2943                 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2944                               offset, copy, 0, skb);
2945                 if (ret < 0)
2946                         return -EFAULT;
2947
2948                 /* copy was successful so update the size parameters */
2949                 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2950                                    copy);
2951                 frg_cnt++;
2952                 pfrag->offset += copy;
2953                 get_page(pfrag->page);
2954
2955                 skb->truesize += copy;
2956                 atomic_add(copy, &sk->sk_wmem_alloc);
2957                 skb->len += copy;
2958                 skb->data_len += copy;
2959                 offset += copy;
2960                 length -= copy;
2961
2962         } while (length > 0);
2963
2964         return 0;
2965 }
2966 EXPORT_SYMBOL(skb_append_datato_frags);
2967
2968 /**
2969  *      skb_pull_rcsum - pull skb and update receive checksum
2970  *      @skb: buffer to update
2971  *      @len: length of data pulled
2972  *
2973  *      This function performs an skb_pull on the packet and updates
2974  *      the CHECKSUM_COMPLETE checksum.  It should be used on
2975  *      receive path processing instead of skb_pull unless you know
2976  *      that the checksum difference is zero (e.g., a valid IP header)
2977  *      or you are setting ip_summed to CHECKSUM_NONE.
2978  */
2979 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2980 {
2981         BUG_ON(len > skb->len);
2982         skb->len -= len;
2983         BUG_ON(skb->len < skb->data_len);
2984         skb_postpull_rcsum(skb, skb->data, len);
2985         return skb->data += len;
2986 }
2987 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2988
2989 /**
2990  *      skb_segment - Perform protocol segmentation on skb.
2991  *      @head_skb: buffer to segment
2992  *      @features: features for the output path (see dev->features)
2993  *
2994  *      This function performs segmentation on the given skb.  It returns
2995  *      a pointer to the first in a list of new skbs for the segments.
2996  *      In case of error it returns ERR_PTR(err).
2997  */
2998 struct sk_buff *skb_segment(struct sk_buff *head_skb,
2999                             netdev_features_t features)
3000 {
3001         struct sk_buff *segs = NULL;
3002         struct sk_buff *tail = NULL;
3003         struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3004         skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3005         unsigned int mss = skb_shinfo(head_skb)->gso_size;
3006         unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3007         struct sk_buff *frag_skb = head_skb;
3008         unsigned int offset = doffset;
3009         unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3010         unsigned int headroom;
3011         unsigned int len;
3012         __be16 proto;
3013         bool csum;
3014         int sg = !!(features & NETIF_F_SG);
3015         int nfrags = skb_shinfo(head_skb)->nr_frags;
3016         int err = -ENOMEM;
3017         int i = 0;
3018         int pos;
3019         int dummy;
3020
3021         __skb_push(head_skb, doffset);
3022         proto = skb_network_protocol(head_skb, &dummy);
3023         if (unlikely(!proto))
3024                 return ERR_PTR(-EINVAL);
3025
3026         csum = !head_skb->encap_hdr_csum &&
3027             !!can_checksum_protocol(features, proto);
3028
3029         headroom = skb_headroom(head_skb);
3030         pos = skb_headlen(head_skb);
3031
3032         do {
3033                 struct sk_buff *nskb;
3034                 skb_frag_t *nskb_frag;
3035                 int hsize;
3036                 int size;
3037
3038                 len = head_skb->len - offset;
3039                 if (len > mss)
3040                         len = mss;
3041
3042                 hsize = skb_headlen(head_skb) - offset;
3043                 if (hsize < 0)
3044                         hsize = 0;
3045                 if (hsize > len || !sg)
3046                         hsize = len;
3047
3048                 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3049                     (skb_headlen(list_skb) == len || sg)) {
3050                         BUG_ON(skb_headlen(list_skb) > len);
3051
3052                         i = 0;
3053                         nfrags = skb_shinfo(list_skb)->nr_frags;
3054                         frag = skb_shinfo(list_skb)->frags;
3055                         frag_skb = list_skb;
3056                         pos += skb_headlen(list_skb);
3057
3058                         while (pos < offset + len) {
3059                                 BUG_ON(i >= nfrags);
3060
3061                                 size = skb_frag_size(frag);
3062                                 if (pos + size > offset + len)
3063                                         break;
3064
3065                                 i++;
3066                                 pos += size;
3067                                 frag++;
3068                         }
3069
3070                         nskb = skb_clone(list_skb, GFP_ATOMIC);
3071                         list_skb = list_skb->next;
3072
3073                         if (unlikely(!nskb))
3074                                 goto err;
3075
3076                         if (unlikely(pskb_trim(nskb, len))) {
3077                                 kfree_skb(nskb);
3078                                 goto err;
3079                         }
3080
3081                         hsize = skb_end_offset(nskb);
3082                         if (skb_cow_head(nskb, doffset + headroom)) {
3083                                 kfree_skb(nskb);
3084                                 goto err;
3085                         }
3086
3087                         nskb->truesize += skb_end_offset(nskb) - hsize;
3088                         skb_release_head_state(nskb);
3089                         __skb_push(nskb, doffset);
3090                 } else {
3091                         nskb = __alloc_skb(hsize + doffset + headroom,
3092                                            GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3093                                            NUMA_NO_NODE);
3094
3095                         if (unlikely(!nskb))
3096                                 goto err;
3097
3098                         skb_reserve(nskb, headroom);
3099                         __skb_put(nskb, doffset);
3100                 }
3101
3102                 if (segs)
3103                         tail->next = nskb;
3104                 else
3105                         segs = nskb;
3106                 tail = nskb;
3107
3108                 __copy_skb_header(nskb, head_skb);
3109
3110                 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3111                 skb_reset_mac_len(nskb);
3112
3113                 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3114                                                  nskb->data - tnl_hlen,
3115                                                  doffset + tnl_hlen);
3116
3117                 if (nskb->len == len + doffset)
3118                         goto perform_csum_check;
3119
3120                 if (!sg && !nskb->remcsum_offload) {
3121                         nskb->ip_summed = CHECKSUM_NONE;
3122                         nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3123                                                             skb_put(nskb, len),
3124                                                             len, 0);
3125                         SKB_GSO_CB(nskb)->csum_start =
3126                             skb_headroom(nskb) + doffset;
3127                         continue;
3128                 }
3129
3130                 nskb_frag = skb_shinfo(nskb)->frags;
3131
3132                 skb_copy_from_linear_data_offset(head_skb, offset,
3133                                                  skb_put(nskb, hsize), hsize);
3134
3135                 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3136                         SKBTX_SHARED_FRAG;
3137
3138                 while (pos < offset + len) {
3139                         if (i >= nfrags) {
3140                                 BUG_ON(skb_headlen(list_skb));
3141
3142                                 i = 0;
3143                                 nfrags = skb_shinfo(list_skb)->nr_frags;
3144                                 frag = skb_shinfo(list_skb)->frags;
3145                                 frag_skb = list_skb;
3146
3147                                 BUG_ON(!nfrags);
3148
3149                                 list_skb = list_skb->next;
3150                         }
3151
3152                         if (unlikely(skb_shinfo(nskb)->nr_frags >=
3153                                      MAX_SKB_FRAGS)) {
3154                                 net_warn_ratelimited(
3155                                         "skb_segment: too many frags: %u %u\n",
3156                                         pos, mss);
3157                                 goto err;
3158                         }
3159
3160                         if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3161                                 goto err;
3162
3163                         *nskb_frag = *frag;
3164                         __skb_frag_ref(nskb_frag);
3165                         size = skb_frag_size(nskb_frag);
3166
3167                         if (pos < offset) {
3168                                 nskb_frag->page_offset += offset - pos;
3169                                 skb_frag_size_sub(nskb_frag, offset - pos);
3170                         }
3171
3172                         skb_shinfo(nskb)->nr_frags++;
3173
3174                         if (pos + size <= offset + len) {
3175                                 i++;
3176                                 frag++;
3177                                 pos += size;
3178                         } else {
3179                                 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3180                                 goto skip_fraglist;
3181                         }
3182
3183                         nskb_frag++;
3184                 }
3185
3186 skip_fraglist:
3187                 nskb->data_len = len - hsize;
3188                 nskb->len += nskb->data_len;
3189                 nskb->truesize += nskb->data_len;
3190
3191 perform_csum_check:
3192                 if (!csum && !nskb->remcsum_offload) {
3193                         nskb->csum = skb_checksum(nskb, doffset,
3194                                                   nskb->len - doffset, 0);
3195                         nskb->ip_summed = CHECKSUM_NONE;
3196                         SKB_GSO_CB(nskb)->csum_start =
3197                             skb_headroom(nskb) + doffset;
3198                 }
3199         } while ((offset += len) < head_skb->len);
3200
3201         /* Some callers want to get the end of the list.
3202          * Put it in segs->prev to avoid walking the list.
3203          * (see validate_xmit_skb_list() for example)
3204          */
3205         segs->prev = tail;
3206
3207         /* Following permits correct backpressure, for protocols
3208          * using skb_set_owner_w().
3209          * Idea is to tranfert ownership from head_skb to last segment.
3210          */
3211         if (head_skb->destructor == sock_wfree) {
3212                 swap(tail->truesize, head_skb->truesize);
3213                 swap(tail->destructor, head_skb->destructor);
3214                 swap(tail->sk, head_skb->sk);
3215         }
3216         return segs;
3217
3218 err:
3219         kfree_skb_list(segs);
3220         return ERR_PTR(err);
3221 }
3222 EXPORT_SYMBOL_GPL(skb_segment);
3223
3224 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3225 {
3226         struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3227         unsigned int offset = skb_gro_offset(skb);
3228         unsigned int headlen = skb_headlen(skb);
3229         unsigned int len = skb_gro_len(skb);
3230         struct sk_buff *lp, *p = *head;
3231         unsigned int delta_truesize;
3232
3233         if (unlikely(p->len + len >= 65536))
3234                 return -E2BIG;
3235
3236         lp = NAPI_GRO_CB(p)->last;
3237         pinfo = skb_shinfo(lp);
3238
3239         if (headlen <= offset) {
3240                 skb_frag_t *frag;
3241                 skb_frag_t *frag2;
3242                 int i = skbinfo->nr_frags;
3243                 int nr_frags = pinfo->nr_frags + i;
3244
3245                 if (nr_frags > MAX_SKB_FRAGS)
3246                         goto merge;
3247
3248                 offset -= headlen;
3249                 pinfo->nr_frags = nr_frags;
3250                 skbinfo->nr_frags = 0;
3251
3252                 frag = pinfo->frags + nr_frags;
3253                 frag2 = skbinfo->frags + i;
3254                 do {
3255                         *--frag = *--frag2;
3256                 } while (--i);
3257
3258                 frag->page_offset += offset;
3259                 skb_frag_size_sub(frag, offset);
3260
3261                 /* all fragments truesize : remove (head size + sk_buff) */
3262                 delta_truesize = skb->truesize -
3263                                  SKB_TRUESIZE(skb_end_offset(skb));
3264
3265                 skb->truesize -= skb->data_len;
3266                 skb->len -= skb->data_len;
3267                 skb->data_len = 0;
3268
3269                 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3270                 goto done;
3271         } else if (skb->head_frag) {
3272                 int nr_frags = pinfo->nr_frags;
3273                 skb_frag_t *frag = pinfo->frags + nr_frags;
3274                 struct page *page = virt_to_head_page(skb->head);
3275                 unsigned int first_size = headlen - offset;
3276                 unsigned int first_offset;
3277
3278                 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3279                         goto merge;
3280
3281                 first_offset = skb->data -
3282                                (unsigned char *)page_address(page) +
3283                                offset;
3284
3285                 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3286
3287                 frag->page.p      = page;
3288                 frag->page_offset = first_offset;
3289                 skb_frag_size_set(frag, first_size);
3290
3291                 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3292                 /* We dont need to clear skbinfo->nr_frags here */
3293
3294                 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3295                 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3296                 goto done;
3297         }
3298
3299 merge:
3300         delta_truesize = skb->truesize;
3301         if (offset > headlen) {
3302                 unsigned int eat = offset - headlen;
3303
3304                 skbinfo->frags[0].page_offset += eat;
3305                 skb_frag_size_sub(&skbinfo->frags[0], eat);
3306                 skb->data_len -= eat;
3307                 skb->len -= eat;
3308                 offset = headlen;
3309         }
3310
3311         __skb_pull(skb, offset);
3312
3313         if (NAPI_GRO_CB(p)->last == p)
3314                 skb_shinfo(p)->frag_list = skb;
3315         else
3316                 NAPI_GRO_CB(p)->last->next = skb;
3317         NAPI_GRO_CB(p)->last = skb;
3318         __skb_header_release(skb);
3319         lp = p;
3320
3321 done:
3322         NAPI_GRO_CB(p)->count++;
3323         p->data_len += len;
3324         p->truesize += delta_truesize;
3325         p->len += len;
3326         if (lp != p) {
3327                 lp->data_len += len;
3328                 lp->truesize += delta_truesize;
3329                 lp->len += len;
3330         }
3331         NAPI_GRO_CB(skb)->same_flow = 1;
3332         return 0;
3333 }
3334
3335 void __init skb_init(void)
3336 {
3337         skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3338                                               sizeof(struct sk_buff),
3339                                               0,
3340                                               SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3341                                               NULL);
3342         skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3343                                                 sizeof(struct sk_buff_fclones),
3344                                                 0,
3345                                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3346                                                 NULL);
3347 }
3348
3349 /**
3350  *      skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3351  *      @skb: Socket buffer containing the buffers to be mapped
3352  *      @sg: The scatter-gather list to map into
3353  *      @offset: The offset into the buffer's contents to start mapping
3354  *      @len: Length of buffer space to be mapped
3355  *
3356  *      Fill the specified scatter-gather list with mappings/pointers into a
3357  *      region of the buffer space attached to a socket buffer.
3358  */
3359 static int
3360 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3361 {
3362         int start = skb_headlen(skb);
3363         int i, copy = start - offset;
3364         struct sk_buff *frag_iter;
3365         int elt = 0;
3366
3367         if (copy > 0) {
3368                 if (copy > len)
3369                         copy = len;
3370                 sg_set_buf(sg, skb->data + offset, copy);
3371                 elt++;
3372                 if ((len -= copy) == 0)
3373                         return elt;
3374                 offset += copy;
3375         }
3376
3377         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3378                 int end;
3379
3380                 WARN_ON(start > offset + len);
3381
3382                 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3383                 if ((copy = end - offset) > 0) {
3384                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3385
3386                         if (copy > len)
3387                                 copy = len;
3388                         sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3389                                         frag->page_offset+offset-start);
3390                         elt++;
3391                         if (!(len -= copy))
3392                                 return elt;
3393                         offset += copy;
3394                 }
3395                 start = end;
3396         }
3397
3398         skb_walk_frags(skb, frag_iter) {
3399                 int end;
3400
3401                 WARN_ON(start > offset + len);
3402
3403                 end = start + frag_iter->len;
3404                 if ((copy = end - offset) > 0) {
3405                         if (copy > len)
3406                                 copy = len;
3407                         elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3408                                               copy);
3409                         if ((len -= copy) == 0)
3410                                 return elt;
3411                         offset += copy;
3412                 }
3413                 start = end;
3414         }
3415         BUG_ON(len);
3416         return elt;
3417 }
3418
3419 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3420  * sglist without mark the sg which contain last skb data as the end.
3421  * So the caller can mannipulate sg list as will when padding new data after
3422  * the first call without calling sg_unmark_end to expend sg list.
3423  *
3424  * Scenario to use skb_to_sgvec_nomark:
3425  * 1. sg_init_table
3426  * 2. skb_to_sgvec_nomark(payload1)
3427  * 3. skb_to_sgvec_nomark(payload2)
3428  *
3429  * This is equivalent to:
3430  * 1. sg_init_table
3431  * 2. skb_to_sgvec(payload1)
3432  * 3. sg_unmark_end
3433  * 4. skb_to_sgvec(payload2)
3434  *
3435  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3436  * is more preferable.
3437  */
3438 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3439                         int offset, int len)
3440 {
3441         return __skb_to_sgvec(skb, sg, offset, len);
3442 }
3443 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3444
3445 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3446 {
3447         int nsg = __skb_to_sgvec(skb, sg, offset, len);
3448
3449         sg_mark_end(&sg[nsg - 1]);
3450
3451         return nsg;
3452 }
3453 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3454
3455 /**
3456  *      skb_cow_data - Check that a socket buffer's data buffers are writable
3457  *      @skb: The socket buffer to check.
3458  *      @tailbits: Amount of trailing space to be added
3459  *      @trailer: Returned pointer to the skb where the @tailbits space begins
3460  *
3461  *      Make sure that the data buffers attached to a socket buffer are
3462  *      writable. If they are not, private copies are made of the data buffers
3463  *      and the socket buffer is set to use these instead.
3464  *
3465  *      If @tailbits is given, make sure that there is space to write @tailbits
3466  *      bytes of data beyond current end of socket buffer.  @trailer will be
3467  *      set to point to the skb in which this space begins.
3468  *
3469  *      The number of scatterlist elements required to completely map the
3470  *      COW'd and extended socket buffer will be returned.
3471  */
3472 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3473 {
3474         int copyflag;
3475         int elt;
3476         struct sk_buff *skb1, **skb_p;
3477
3478         /* If skb is cloned or its head is paged, reallocate
3479          * head pulling out all the pages (pages are considered not writable
3480          * at the moment even if they are anonymous).
3481          */
3482         if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3483             __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3484                 return -ENOMEM;
3485
3486         /* Easy case. Most of packets will go this way. */
3487         if (!skb_has_frag_list(skb)) {
3488                 /* A little of trouble, not enough of space for trailer.
3489                  * This should not happen, when stack is tuned to generate
3490                  * good frames. OK, on miss we reallocate and reserve even more
3491                  * space, 128 bytes is fair. */
3492
3493                 if (skb_tailroom(skb) < tailbits &&
3494                     pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3495                         return -ENOMEM;
3496
3497                 /* Voila! */
3498                 *trailer = skb;
3499                 return 1;
3500         }
3501
3502         /* Misery. We are in troubles, going to mincer fragments... */
3503
3504         elt = 1;
3505         skb_p = &skb_shinfo(skb)->frag_list;
3506         copyflag = 0;
3507
3508         while ((skb1 = *skb_p) != NULL) {
3509                 int ntail = 0;
3510
3511                 /* The fragment is partially pulled by someone,
3512                  * this can happen on input. Copy it and everything
3513                  * after it. */
3514
3515                 if (skb_shared(skb1))
3516                         copyflag = 1;
3517
3518                 /* If the skb is the last, worry about trailer. */
3519
3520                 if (skb1->next == NULL && tailbits) {
3521                         if (skb_shinfo(skb1)->nr_frags ||
3522                             skb_has_frag_list(skb1) ||
3523                             skb_tailroom(skb1) < tailbits)
3524                                 ntail = tailbits + 128;
3525                 }
3526
3527                 if (copyflag ||
3528                     skb_cloned(skb1) ||
3529                     ntail ||
3530                     skb_shinfo(skb1)->nr_frags ||
3531                     skb_has_frag_list(skb1)) {
3532                         struct sk_buff *skb2;
3533
3534                         /* Fuck, we are miserable poor guys... */
3535                         if (ntail == 0)
3536                                 skb2 = skb_copy(skb1, GFP_ATOMIC);
3537                         else
3538                                 skb2 = skb_copy_expand(skb1,
3539                                                        skb_headroom(skb1),
3540                                                        ntail,
3541                                                        GFP_ATOMIC);
3542                         if (unlikely(skb2 == NULL))
3543                                 return -ENOMEM;
3544
3545                         if (skb1->sk)
3546                                 skb_set_owner_w(skb2, skb1->sk);
3547
3548                         /* Looking around. Are we still alive?
3549                          * OK, link new skb, drop old one */
3550
3551                         skb2->next = skb1->next;
3552                         *skb_p = skb2;
3553                         kfree_skb(skb1);
3554                         skb1 = skb2;
3555                 }
3556                 elt++;
3557                 *trailer = skb1;
3558                 skb_p = &skb1->next;
3559         }
3560
3561         return elt;
3562 }
3563 EXPORT_SYMBOL_GPL(skb_cow_data);
3564
3565 static void sock_rmem_free(struct sk_buff *skb)
3566 {
3567         struct sock *sk = skb->sk;
3568
3569         atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3570 }
3571
3572 /*
3573  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3574  */
3575 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3576 {
3577         if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3578             (unsigned int)sk->sk_rcvbuf)
3579                 return -ENOMEM;
3580
3581         skb_orphan(skb);
3582         skb->sk = sk;
3583         skb->destructor = sock_rmem_free;
3584         atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3585
3586         /* before exiting rcu section, make sure dst is refcounted */
3587         skb_dst_force(skb);
3588
3589         skb_queue_tail(&sk->sk_error_queue, skb);
3590         if (!sock_flag(sk, SOCK_DEAD))
3591                 sk->sk_data_ready(sk);
3592         return 0;
3593 }
3594 EXPORT_SYMBOL(sock_queue_err_skb);
3595
3596 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3597 {
3598         struct sk_buff_head *q = &sk->sk_error_queue;
3599         struct sk_buff *skb, *skb_next;
3600         unsigned long flags;
3601         int err = 0;
3602
3603         spin_lock_irqsave(&q->lock, flags);
3604         skb = __skb_dequeue(q);
3605         if (skb && (skb_next = skb_peek(q)))
3606                 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3607         spin_unlock_irqrestore(&q->lock, flags);
3608
3609         sk->sk_err = err;
3610         if (err)
3611                 sk->sk_error_report(sk);
3612
3613         return skb;
3614 }
3615 EXPORT_SYMBOL(sock_dequeue_err_skb);
3616
3617 /**
3618  * skb_clone_sk - create clone of skb, and take reference to socket
3619  * @skb: the skb to clone
3620  *
3621  * This function creates a clone of a buffer that holds a reference on
3622  * sk_refcnt.  Buffers created via this function are meant to be
3623  * returned using sock_queue_err_skb, or free via kfree_skb.
3624  *
3625  * When passing buffers allocated with this function to sock_queue_err_skb
3626  * it is necessary to wrap the call with sock_hold/sock_put in order to
3627  * prevent the socket from being released prior to being enqueued on
3628  * the sk_error_queue.
3629  */
3630 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3631 {
3632         struct sock *sk = skb->sk;
3633         struct sk_buff *clone;
3634
3635         if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3636                 return NULL;
3637
3638         clone = skb_clone(skb, GFP_ATOMIC);
3639         if (!clone) {
3640                 sock_put(sk);
3641                 return NULL;
3642         }
3643
3644         clone->sk = sk;
3645         clone->destructor = sock_efree;
3646
3647         return clone;
3648 }
3649 EXPORT_SYMBOL(skb_clone_sk);
3650
3651 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3652                                         struct sock *sk,
3653                                         int tstype)
3654 {
3655         struct sock_exterr_skb *serr;
3656         int err;
3657
3658         serr = SKB_EXT_ERR(skb);
3659         memset(serr, 0, sizeof(*serr));
3660         serr->ee.ee_errno = ENOMSG;
3661         serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3662         serr->ee.ee_info = tstype;
3663         if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3664                 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3665                 if (sk->sk_protocol == IPPROTO_TCP)
3666                         serr->ee.ee_data -= sk->sk_tskey;
3667         }
3668
3669         err = sock_queue_err_skb(sk, skb);
3670
3671         if (err)
3672                 kfree_skb(skb);
3673 }
3674
3675 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3676 {
3677         bool ret;
3678
3679         if (likely(sysctl_tstamp_allow_data || tsonly))
3680                 return true;
3681
3682         read_lock_bh(&sk->sk_callback_lock);
3683         ret = sk->sk_socket && sk->sk_socket->file &&
3684               file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3685         read_unlock_bh(&sk->sk_callback_lock);
3686         return ret;
3687 }
3688
3689 void skb_complete_tx_timestamp(struct sk_buff *skb,
3690                                struct skb_shared_hwtstamps *hwtstamps)
3691 {
3692         struct sock *sk = skb->sk;
3693
3694         if (!skb_may_tx_timestamp(sk, false))
3695                 return;
3696
3697         /* take a reference to prevent skb_orphan() from freeing the socket */
3698         sock_hold(sk);
3699
3700         *skb_hwtstamps(skb) = *hwtstamps;
3701         __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3702
3703         sock_put(sk);
3704 }
3705 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3706
3707 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3708                      struct skb_shared_hwtstamps *hwtstamps,
3709                      struct sock *sk, int tstype)
3710 {
3711         struct sk_buff *skb;
3712         bool tsonly;
3713
3714         if (!sk)
3715                 return;
3716
3717         tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3718         if (!skb_may_tx_timestamp(sk, tsonly))
3719                 return;
3720
3721         if (tsonly)
3722                 skb = alloc_skb(0, GFP_ATOMIC);
3723         else
3724                 skb = skb_clone(orig_skb, GFP_ATOMIC);
3725         if (!skb)
3726                 return;
3727
3728         if (tsonly) {
3729                 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3730                 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3731         }
3732
3733         if (hwtstamps)
3734                 *skb_hwtstamps(skb) = *hwtstamps;
3735         else
3736                 skb->tstamp = ktime_get_real();
3737
3738         __skb_complete_tx_timestamp(skb, sk, tstype);
3739 }
3740 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3741
3742 void skb_tstamp_tx(struct sk_buff *orig_skb,
3743                    struct skb_shared_hwtstamps *hwtstamps)
3744 {
3745         return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3746                                SCM_TSTAMP_SND);
3747 }
3748 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3749
3750 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3751 {
3752         struct sock *sk = skb->sk;
3753         struct sock_exterr_skb *serr;
3754         int err;
3755
3756         skb->wifi_acked_valid = 1;
3757         skb->wifi_acked = acked;
3758
3759         serr = SKB_EXT_ERR(skb);
3760         memset(serr, 0, sizeof(*serr));
3761         serr->ee.ee_errno = ENOMSG;
3762         serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3763
3764         /* take a reference to prevent skb_orphan() from freeing the socket */
3765         sock_hold(sk);
3766
3767         err = sock_queue_err_skb(sk, skb);
3768         if (err)
3769                 kfree_skb(skb);
3770
3771         sock_put(sk);
3772 }
3773 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3774
3775 /**
3776  * skb_partial_csum_set - set up and verify partial csum values for packet
3777  * @skb: the skb to set
3778  * @start: the number of bytes after skb->data to start checksumming.
3779  * @off: the offset from start to place the checksum.
3780  *
3781  * For untrusted partially-checksummed packets, we need to make sure the values
3782  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3783  *
3784  * This function checks and sets those values and skb->ip_summed: if this
3785  * returns false you should drop the packet.
3786  */
3787 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3788 {
3789         if (unlikely(start > skb_headlen(skb)) ||
3790             unlikely((int)start + off > skb_headlen(skb) - 2)) {
3791                 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3792                                      start, off, skb_headlen(skb));
3793                 return false;
3794         }
3795         skb->ip_summed = CHECKSUM_PARTIAL;
3796         skb->csum_start = skb_headroom(skb) + start;
3797         skb->csum_offset = off;
3798         skb_set_transport_header(skb, start);
3799         return true;
3800 }
3801 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3802
3803 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3804                                unsigned int max)
3805 {
3806         if (skb_headlen(skb) >= len)
3807                 return 0;
3808
3809         /* If we need to pullup then pullup to the max, so we
3810          * won't need to do it again.
3811          */
3812         if (max > skb->len)
3813                 max = skb->len;
3814
3815         if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3816                 return -ENOMEM;
3817
3818         if (skb_headlen(skb) < len)
3819                 return -EPROTO;
3820
3821         return 0;
3822 }
3823
3824 #define MAX_TCP_HDR_LEN (15 * 4)
3825
3826 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3827                                       typeof(IPPROTO_IP) proto,
3828                                       unsigned int off)
3829 {
3830         switch (proto) {
3831                 int err;
3832
3833         case IPPROTO_TCP:
3834                 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3835                                           off + MAX_TCP_HDR_LEN);
3836                 if (!err && !skb_partial_csum_set(skb, off,
3837                                                   offsetof(struct tcphdr,
3838                                                            check)))
3839                         err = -EPROTO;
3840                 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3841
3842         case IPPROTO_UDP:
3843                 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3844                                           off + sizeof(struct udphdr));
3845                 if (!err && !skb_partial_csum_set(skb, off,
3846                                                   offsetof(struct udphdr,
3847                                                            check)))
3848                         err = -EPROTO;
3849                 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3850         }
3851
3852         return ERR_PTR(-EPROTO);
3853 }
3854
3855 /* This value should be large enough to cover a tagged ethernet header plus
3856  * maximally sized IP and TCP or UDP headers.
3857  */
3858 #define MAX_IP_HDR_LEN 128
3859
3860 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3861 {
3862         unsigned int off;
3863         bool fragment;
3864         __sum16 *csum;
3865         int err;
3866
3867         fragment = false;
3868
3869         err = skb_maybe_pull_tail(skb,
3870                                   sizeof(struct iphdr),
3871                                   MAX_IP_HDR_LEN);
3872         if (err < 0)
3873                 goto out;
3874
3875         if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3876                 fragment = true;
3877
3878         off = ip_hdrlen(skb);
3879
3880         err = -EPROTO;
3881
3882         if (fragment)
3883                 goto out;
3884
3885         csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3886         if (IS_ERR(csum))
3887                 return PTR_ERR(csum);
3888
3889         if (recalculate)
3890                 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3891                                            ip_hdr(skb)->daddr,
3892                                            skb->len - off,
3893                                            ip_hdr(skb)->protocol, 0);
3894         err = 0;
3895
3896 out:
3897         return err;
3898 }
3899
3900 /* This value should be large enough to cover a tagged ethernet header plus
3901  * an IPv6 header, all options, and a maximal TCP or UDP header.
3902  */
3903 #define MAX_IPV6_HDR_LEN 256
3904
3905 #define OPT_HDR(type, skb, off) \
3906         (type *)(skb_network_header(skb) + (off))
3907
3908 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3909 {
3910         int err;
3911         u8 nexthdr;
3912         unsigned int off;
3913         unsigned int len;
3914         bool fragment;
3915         bool done;
3916         __sum16 *csum;
3917
3918         fragment = false;
3919         done = false;
3920
3921         off = sizeof(struct ipv6hdr);
3922
3923         err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3924         if (err < 0)
3925                 goto out;
3926
3927         nexthdr = ipv6_hdr(skb)->nexthdr;
3928
3929         len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3930         while (off <= len && !done) {
3931                 switch (nexthdr) {
3932                 case IPPROTO_DSTOPTS:
3933                 case IPPROTO_HOPOPTS:
3934                 case IPPROTO_ROUTING: {
3935                         struct ipv6_opt_hdr *hp;
3936
3937                         err = skb_maybe_pull_tail(skb,
3938                                                   off +
3939                                                   sizeof(struct ipv6_opt_hdr),
3940                                                   MAX_IPV6_HDR_LEN);
3941                         if (err < 0)
3942                                 goto out;
3943
3944                         hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3945                         nexthdr = hp->nexthdr;
3946                         off += ipv6_optlen(hp);
3947                         break;
3948                 }
3949                 case IPPROTO_AH: {
3950                         struct ip_auth_hdr *hp;
3951
3952                         err = skb_maybe_pull_tail(skb,
3953                                                   off +
3954                                                   sizeof(struct ip_auth_hdr),
3955                                                   MAX_IPV6_HDR_LEN);
3956                         if (err < 0)
3957                                 goto out;
3958
3959                         hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3960                         nexthdr = hp->nexthdr;
3961                         off += ipv6_authlen(hp);
3962                         break;
3963                 }
3964                 case IPPROTO_FRAGMENT: {
3965                         struct frag_hdr *hp;
3966
3967                         err = skb_maybe_pull_tail(skb,
3968                                                   off +
3969                                                   sizeof(struct frag_hdr),
3970                                                   MAX_IPV6_HDR_LEN);
3971                         if (err < 0)
3972                                 goto out;
3973
3974                         hp = OPT_HDR(struct frag_hdr, skb, off);
3975
3976                         if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3977                                 fragment = true;
3978
3979                         nexthdr = hp->nexthdr;
3980                         off += sizeof(struct frag_hdr);
3981                         break;
3982                 }
3983                 default:
3984                         done = true;
3985                         break;
3986                 }
3987         }
3988
3989         err = -EPROTO;
3990
3991         if (!done || fragment)
3992                 goto out;
3993
3994         csum = skb_checksum_setup_ip(skb, nexthdr, off);
3995         if (IS_ERR(csum))
3996                 return PTR_ERR(csum);
3997
3998         if (recalculate)
3999                 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4000                                          &ipv6_hdr(skb)->daddr,
4001                                          skb->len - off, nexthdr, 0);
4002         err = 0;
4003
4004 out:
4005         return err;
4006 }
4007
4008 /**
4009  * skb_checksum_setup - set up partial checksum offset
4010  * @skb: the skb to set up
4011  * @recalculate: if true the pseudo-header checksum will be recalculated
4012  */
4013 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4014 {
4015         int err;
4016
4017         switch (skb->protocol) {
4018         case htons(ETH_P_IP):
4019                 err = skb_checksum_setup_ipv4(skb, recalculate);
4020                 break;
4021
4022         case htons(ETH_P_IPV6):
4023                 err = skb_checksum_setup_ipv6(skb, recalculate);
4024                 break;
4025
4026         default:
4027                 err = -EPROTO;
4028                 break;
4029         }
4030
4031         return err;
4032 }
4033 EXPORT_SYMBOL(skb_checksum_setup);
4034
4035 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4036 {
4037         net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4038                              skb->dev->name);
4039 }
4040 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4041
4042 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4043 {
4044         if (head_stolen) {
4045                 skb_release_head_state(skb);
4046                 kmem_cache_free(skbuff_head_cache, skb);
4047         } else {
4048                 __kfree_skb(skb);
4049         }
4050 }
4051 EXPORT_SYMBOL(kfree_skb_partial);
4052
4053 /**
4054  * skb_try_coalesce - try to merge skb to prior one
4055  * @to: prior buffer
4056  * @from: buffer to add
4057  * @fragstolen: pointer to boolean
4058  * @delta_truesize: how much more was allocated than was requested
4059  */
4060 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4061                       bool *fragstolen, int *delta_truesize)
4062 {
4063         int i, delta, len = from->len;
4064
4065         *fragstolen = false;
4066
4067         if (skb_cloned(to))
4068                 return false;
4069
4070         if (len <= skb_tailroom(to)) {
4071                 if (len)
4072                         BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4073                 *delta_truesize = 0;
4074                 return true;
4075         }
4076
4077         if (skb_has_frag_list(to) || skb_has_frag_list(from))
4078                 return false;
4079
4080         if (skb_headlen(from) != 0) {
4081                 struct page *page;
4082                 unsigned int offset;
4083
4084                 if (skb_shinfo(to)->nr_frags +
4085                     skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4086                         return false;
4087
4088                 if (skb_head_is_locked(from))
4089                         return false;
4090
4091                 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4092
4093                 page = virt_to_head_page(from->head);
4094                 offset = from->data - (unsigned char *)page_address(page);
4095
4096                 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4097                                    page, offset, skb_headlen(from));
4098                 *fragstolen = true;
4099         } else {
4100                 if (skb_shinfo(to)->nr_frags +
4101                     skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4102                         return false;
4103
4104                 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4105         }
4106
4107         WARN_ON_ONCE(delta < len);
4108
4109         memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4110                skb_shinfo(from)->frags,
4111                skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4112         skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4113
4114         if (!skb_cloned(from))
4115                 skb_shinfo(from)->nr_frags = 0;
4116
4117         /* if the skb is not cloned this does nothing
4118          * since we set nr_frags to 0.
4119          */
4120         for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4121                 skb_frag_ref(from, i);
4122
4123         to->truesize += delta;
4124         to->len += len;
4125         to->data_len += len;
4126
4127         *delta_truesize = delta;
4128         return true;
4129 }
4130 EXPORT_SYMBOL(skb_try_coalesce);
4131
4132 /**
4133  * skb_scrub_packet - scrub an skb
4134  *
4135  * @skb: buffer to clean
4136  * @xnet: packet is crossing netns
4137  *
4138  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4139  * into/from a tunnel. Some information have to be cleared during these
4140  * operations.
4141  * skb_scrub_packet can also be used to clean a skb before injecting it in
4142  * another namespace (@xnet == true). We have to clear all information in the
4143  * skb that could impact namespace isolation.
4144  */
4145 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4146 {
4147         skb->tstamp.tv64 = 0;
4148         skb->pkt_type = PACKET_HOST;
4149         skb->skb_iif = 0;
4150         skb->ignore_df = 0;
4151         skb_dst_drop(skb);
4152         skb_sender_cpu_clear(skb);
4153         secpath_reset(skb);
4154         nf_reset(skb);
4155         nf_reset_trace(skb);
4156
4157         if (!xnet)
4158                 return;
4159
4160         skb_orphan(skb);
4161         skb->mark = 0;
4162 }
4163 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4164
4165 /**
4166  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4167  *
4168  * @skb: GSO skb
4169  *
4170  * skb_gso_transport_seglen is used to determine the real size of the
4171  * individual segments, including Layer4 headers (TCP/UDP).
4172  *
4173  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4174  */
4175 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4176 {
4177         const struct skb_shared_info *shinfo = skb_shinfo(skb);
4178         unsigned int thlen = 0;
4179
4180         if (skb->encapsulation) {
4181                 thlen = skb_inner_transport_header(skb) -
4182                         skb_transport_header(skb);
4183
4184                 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4185                         thlen += inner_tcp_hdrlen(skb);
4186         } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4187                 thlen = tcp_hdrlen(skb);
4188         }
4189         /* UFO sets gso_size to the size of the fragmentation
4190          * payload, i.e. the size of the L4 (UDP) header is already
4191          * accounted for.
4192          */
4193         return thlen + shinfo->gso_size;
4194 }
4195 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4196
4197 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4198 {
4199         if (skb_cow(skb, skb_headroom(skb)) < 0) {
4200                 kfree_skb(skb);
4201                 return NULL;
4202         }
4203
4204         memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4205         skb->mac_header += VLAN_HLEN;
4206         return skb;
4207 }
4208
4209 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4210 {
4211         struct vlan_hdr *vhdr;
4212         u16 vlan_tci;
4213
4214         if (unlikely(skb_vlan_tag_present(skb))) {
4215                 /* vlan_tci is already set-up so leave this for another time */
4216                 return skb;
4217         }
4218
4219         skb = skb_share_check(skb, GFP_ATOMIC);
4220         if (unlikely(!skb))
4221                 goto err_free;
4222
4223         if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4224                 goto err_free;
4225
4226         vhdr = (struct vlan_hdr *)skb->data;
4227         vlan_tci = ntohs(vhdr->h_vlan_TCI);
4228         __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4229
4230         skb_pull_rcsum(skb, VLAN_HLEN);
4231         vlan_set_encap_proto(skb, vhdr);
4232
4233         skb = skb_reorder_vlan_header(skb);
4234         if (unlikely(!skb))
4235                 goto err_free;
4236
4237         skb_reset_network_header(skb);
4238         skb_reset_transport_header(skb);
4239         skb_reset_mac_len(skb);
4240
4241         return skb;
4242
4243 err_free:
4244         kfree_skb(skb);
4245         return NULL;
4246 }
4247 EXPORT_SYMBOL(skb_vlan_untag);
4248
4249 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4250 {
4251         if (!pskb_may_pull(skb, write_len))
4252                 return -ENOMEM;
4253
4254         if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4255                 return 0;
4256
4257         return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4258 }
4259 EXPORT_SYMBOL(skb_ensure_writable);
4260
4261 /* remove VLAN header from packet and update csum accordingly. */
4262 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4263 {
4264         struct vlan_hdr *vhdr;
4265         unsigned int offset = skb->data - skb_mac_header(skb);
4266         int err;
4267
4268         __skb_push(skb, offset);
4269         err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4270         if (unlikely(err))
4271                 goto pull;
4272
4273         skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4274
4275         vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4276         *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4277
4278         memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4279         __skb_pull(skb, VLAN_HLEN);
4280
4281         vlan_set_encap_proto(skb, vhdr);
4282         skb->mac_header += VLAN_HLEN;
4283
4284         if (skb_network_offset(skb) < ETH_HLEN)
4285                 skb_set_network_header(skb, ETH_HLEN);
4286
4287         skb_reset_mac_len(skb);
4288 pull:
4289         __skb_pull(skb, offset);
4290
4291         return err;
4292 }
4293
4294 int skb_vlan_pop(struct sk_buff *skb)
4295 {
4296         u16 vlan_tci;
4297         __be16 vlan_proto;
4298         int err;
4299
4300         if (likely(skb_vlan_tag_present(skb))) {
4301                 skb->vlan_tci = 0;
4302         } else {
4303                 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4304                               skb->protocol != htons(ETH_P_8021AD)) ||
4305                              skb->len < VLAN_ETH_HLEN))
4306                         return 0;
4307
4308                 err = __skb_vlan_pop(skb, &vlan_tci);
4309                 if (err)
4310                         return err;
4311         }
4312         /* move next vlan tag to hw accel tag */
4313         if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4314                     skb->protocol != htons(ETH_P_8021AD)) ||
4315                    skb->len < VLAN_ETH_HLEN))
4316                 return 0;
4317
4318         vlan_proto = skb->protocol;
4319         err = __skb_vlan_pop(skb, &vlan_tci);
4320         if (unlikely(err))
4321                 return err;
4322
4323         __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4324         return 0;
4325 }
4326 EXPORT_SYMBOL(skb_vlan_pop);
4327
4328 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4329 {
4330         if (skb_vlan_tag_present(skb)) {
4331                 unsigned int offset = skb->data - skb_mac_header(skb);
4332                 int err;
4333
4334                 /* __vlan_insert_tag expect skb->data pointing to mac header.
4335                  * So change skb->data before calling it and change back to
4336                  * original position later
4337                  */
4338                 __skb_push(skb, offset);
4339                 err = __vlan_insert_tag(skb, skb->vlan_proto,
4340                                         skb_vlan_tag_get(skb));
4341                 if (err)
4342                         return err;
4343                 skb->protocol = skb->vlan_proto;
4344                 skb->mac_len += VLAN_HLEN;
4345                 __skb_pull(skb, offset);
4346
4347                 if (skb->ip_summed == CHECKSUM_COMPLETE)
4348                         skb->csum = csum_add(skb->csum, csum_partial(skb->data
4349                                         + (2 * ETH_ALEN), VLAN_HLEN, 0));
4350         }
4351         __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4352         return 0;
4353 }
4354 EXPORT_SYMBOL(skb_vlan_push);
4355
4356 /**
4357  * alloc_skb_with_frags - allocate skb with page frags
4358  *
4359  * @header_len: size of linear part
4360  * @data_len: needed length in frags
4361  * @max_page_order: max page order desired.
4362  * @errcode: pointer to error code if any
4363  * @gfp_mask: allocation mask
4364  *
4365  * This can be used to allocate a paged skb, given a maximal order for frags.
4366  */
4367 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4368                                      unsigned long data_len,
4369                                      int max_page_order,
4370                                      int *errcode,
4371                                      gfp_t gfp_mask)
4372 {
4373         int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4374         unsigned long chunk;
4375         struct sk_buff *skb;
4376         struct page *page;
4377         gfp_t gfp_head;
4378         int i;
4379
4380         *errcode = -EMSGSIZE;
4381         /* Note this test could be relaxed, if we succeed to allocate
4382          * high order pages...
4383          */
4384         if (npages > MAX_SKB_FRAGS)
4385                 return NULL;
4386
4387         gfp_head = gfp_mask;
4388         if (gfp_head & __GFP_WAIT)
4389                 gfp_head |= __GFP_REPEAT;
4390
4391         *errcode = -ENOBUFS;
4392         skb = alloc_skb(header_len, gfp_head);
4393         if (!skb)
4394                 return NULL;
4395
4396         skb->truesize += npages << PAGE_SHIFT;
4397
4398         for (i = 0; npages > 0; i++) {
4399                 int order = max_page_order;
4400
4401                 while (order) {
4402                         if (npages >= 1 << order) {
4403                                 page = alloc_pages((gfp_mask & ~__GFP_WAIT) |
4404                                                    __GFP_COMP |
4405                                                    __GFP_NOWARN |
4406                                                    __GFP_NORETRY,
4407                                                    order);
4408                                 if (page)
4409                                         goto fill_page;
4410                                 /* Do not retry other high order allocations */
4411                                 order = 1;
4412                                 max_page_order = 0;
4413                         }
4414                         order--;
4415                 }
4416                 page = alloc_page(gfp_mask);
4417                 if (!page)
4418                         goto failure;
4419 fill_page:
4420                 chunk = min_t(unsigned long, data_len,
4421                               PAGE_SIZE << order);
4422                 skb_fill_page_desc(skb, i, page, 0, chunk);
4423                 data_len -= chunk;
4424                 npages -= 1 << order;
4425         }
4426         return skb;
4427
4428 failure:
4429         kfree_skb(skb);
4430         return NULL;
4431 }
4432 EXPORT_SYMBOL(alloc_skb_with_frags);