root/src/linux/ar531x/linux-2.6.23/net/core/skbuff.c

Revision 12400, 55.3 kB (checked in by BrainSlayer, 5 months ago)

more flexible EOC5610 partition layout, small performance increase by module remapping, lzma decoder speed improvements

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