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