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