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