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