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