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