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