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