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