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