OSDN Git Service

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