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