vserver 1.9.5.x5
[linux-2.6.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;
72
73 /*
74  *      Keep out-of-line to prevent kernel bloat.
75  *      __builtin_return_address is not used because it is not always
76  *      reliable.
77  */
78
79 /**
80  *      skb_over_panic  -       private function
81  *      @skb: buffer
82  *      @sz: size
83  *      @here: address
84  *
85  *      Out of line support code for skb_put(). Not user callable.
86  */
87 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
88 {
89         printk(KERN_INFO "skput:over: %p:%d put:%d dev:%s",
90                 here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
91         BUG();
92 }
93
94 /**
95  *      skb_under_panic -       private function
96  *      @skb: buffer
97  *      @sz: size
98  *      @here: address
99  *
100  *      Out of line support code for skb_push(). Not user callable.
101  */
102
103 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
104 {
105         printk(KERN_INFO "skput:under: %p:%d put:%d dev:%s",
106                here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
107         BUG();
108 }
109
110 /*      Allocate a new skbuff. We do this ourselves so we can fill in a few
111  *      'private' fields and also do memory statistics to find all the
112  *      [BEEP] leaks.
113  *
114  */
115
116 /**
117  *      alloc_skb       -       allocate a network buffer
118  *      @size: size to allocate
119  *      @gfp_mask: allocation mask
120  *
121  *      Allocate a new &sk_buff. The returned buffer has no headroom and a
122  *      tail room of size bytes. The object has a reference count of one.
123  *      The return is the buffer. On a failure the return is %NULL.
124  *
125  *      Buffers may only be allocated from interrupts using a @gfp_mask of
126  *      %GFP_ATOMIC.
127  */
128 struct sk_buff *alloc_skb(unsigned int size, int gfp_mask)
129 {
130         struct sk_buff *skb;
131         u8 *data;
132
133         /* Get the HEAD */
134         skb = kmem_cache_alloc(skbuff_head_cache,
135                                gfp_mask & ~__GFP_DMA);
136         if (!skb)
137                 goto out;
138
139         /* Get the DATA. Size must match skb_add_mtu(). */
140         size = SKB_DATA_ALIGN(size);
141         data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
142         if (!data)
143                 goto nodata;
144
145         memset(skb, 0, offsetof(struct sk_buff, truesize));
146         skb->truesize = size + sizeof(struct sk_buff);
147         atomic_set(&skb->users, 1);
148         skb->head = data;
149         skb->data = data;
150         skb->tail = data;
151         skb->end  = data + size;
152
153         atomic_set(&(skb_shinfo(skb)->dataref), 1);
154         skb_shinfo(skb)->nr_frags  = 0;
155         skb_shinfo(skb)->tso_size = 0;
156         skb_shinfo(skb)->tso_segs = 0;
157         skb_shinfo(skb)->frag_list = NULL;
158 out:
159         return skb;
160 nodata:
161         kmem_cache_free(skbuff_head_cache, skb);
162         skb = NULL;
163         goto out;
164 }
165
166 /**
167  *      alloc_skb_from_cache    -       allocate a network buffer
168  *      @cp: kmem_cache from which to allocate the data area
169  *           (object size must be big enough for @size bytes + skb overheads)
170  *      @size: size to allocate
171  *      @gfp_mask: allocation mask
172  *
173  *      Allocate a new &sk_buff. The returned buffer has no headroom and
174  *      tail room of size bytes. The object has a reference count of one.
175  *      The return is the buffer. On a failure the return is %NULL.
176  *
177  *      Buffers may only be allocated from interrupts using a @gfp_mask of
178  *      %GFP_ATOMIC.
179  */
180 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
181                                      unsigned int size, int gfp_mask)
182 {
183         struct sk_buff *skb;
184         u8 *data;
185
186         /* Get the HEAD */
187         skb = kmem_cache_alloc(skbuff_head_cache,
188                                gfp_mask & ~__GFP_DMA);
189         if (!skb)
190                 goto out;
191
192         /* Get the DATA. */
193         size = SKB_DATA_ALIGN(size);
194         data = kmem_cache_alloc(cp, gfp_mask);
195         if (!data)
196                 goto nodata;
197
198         memset(skb, 0, offsetof(struct sk_buff, truesize));
199         skb->truesize = size + sizeof(struct sk_buff);
200         atomic_set(&skb->users, 1);
201         skb->head = data;
202         skb->data = data;
203         skb->tail = data;
204         skb->end  = data + size;
205
206         atomic_set(&(skb_shinfo(skb)->dataref), 1);
207         skb_shinfo(skb)->nr_frags  = 0;
208         skb_shinfo(skb)->tso_size = 0;
209         skb_shinfo(skb)->tso_segs = 0;
210         skb_shinfo(skb)->frag_list = NULL;
211 out:
212         return skb;
213 nodata:
214         kmem_cache_free(skbuff_head_cache, skb);
215         skb = NULL;
216         goto out;
217 }
218
219
220 static void skb_drop_fraglist(struct sk_buff *skb)
221 {
222         struct sk_buff *list = skb_shinfo(skb)->frag_list;
223
224         skb_shinfo(skb)->frag_list = NULL;
225
226         do {
227                 struct sk_buff *this = list;
228                 list = list->next;
229                 kfree_skb(this);
230         } while (list);
231 }
232
233 static void skb_clone_fraglist(struct sk_buff *skb)
234 {
235         struct sk_buff *list;
236
237         for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
238                 skb_get(list);
239 }
240
241 void skb_release_data(struct sk_buff *skb)
242 {
243         if (!skb->cloned ||
244             atomic_dec_and_test(&(skb_shinfo(skb)->dataref))) {
245                 if (skb_shinfo(skb)->nr_frags) {
246                         int i;
247                         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
248                                 put_page(skb_shinfo(skb)->frags[i].page);
249                 }
250
251                 if (skb_shinfo(skb)->frag_list)
252                         skb_drop_fraglist(skb);
253
254                 kfree(skb->head);
255         }
256 }
257
258 /*
259  *      Free an skbuff by memory without cleaning the state.
260  */
261 void kfree_skbmem(struct sk_buff *skb)
262 {
263         skb_release_data(skb);
264         kmem_cache_free(skbuff_head_cache, skb);
265 }
266
267 /**
268  *      __kfree_skb - private function
269  *      @skb: buffer
270  *
271  *      Free an sk_buff. Release anything attached to the buffer.
272  *      Clean the state. This is an internal helper function. Users should
273  *      always call kfree_skb
274  */
275
276 void __kfree_skb(struct sk_buff *skb)
277 {
278         if (skb->list) {
279                 printk(KERN_WARNING "Warning: kfree_skb passed an skb still "
280                        "on a list (from %p).\n", NET_CALLER(skb));
281                 BUG();
282         }
283
284         dst_release(skb->dst);
285 #ifdef CONFIG_XFRM
286         secpath_put(skb->sp);
287 #endif
288         if(skb->destructor) {
289                 if (in_irq())
290                         printk(KERN_WARNING "Warning: kfree_skb on "
291                                             "hard IRQ %p\n", NET_CALLER(skb));
292                 skb->destructor(skb);
293         }
294 #ifdef CONFIG_NETFILTER
295         nf_conntrack_put(skb->nfct);
296 #ifdef CONFIG_BRIDGE_NETFILTER
297         nf_bridge_put(skb->nf_bridge);
298 #endif
299 #endif
300 /* XXX: IS this still necessary? - JHS */
301 #ifdef CONFIG_NET_SCHED
302         skb->tc_index = 0;
303 #ifdef CONFIG_NET_CLS_ACT
304         skb->tc_verd = 0;
305         skb->tc_classid = 0;
306 #endif
307 #endif
308
309         kfree_skbmem(skb);
310 }
311
312 /**
313  *      skb_clone       -       duplicate an sk_buff
314  *      @skb: buffer to clone
315  *      @gfp_mask: allocation priority
316  *
317  *      Duplicate an &sk_buff. The new one is not owned by a socket. Both
318  *      copies share the same packet data but not structure. The new
319  *      buffer has a reference count of 1. If the allocation fails the
320  *      function returns %NULL otherwise the new buffer is returned.
321  *
322  *      If this function is called from an interrupt gfp_mask() must be
323  *      %GFP_ATOMIC.
324  */
325
326 struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)
327 {
328         struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
329
330         if (!n) 
331                 return NULL;
332
333 #define C(x) n->x = skb->x
334
335         n->next = n->prev = NULL;
336         n->list = NULL;
337         n->sk = NULL;
338         C(stamp);
339         C(dev);
340         C(real_dev);
341         C(h);
342         C(nh);
343         C(mac);
344         C(dst);
345         dst_clone(skb->dst);
346         C(sp);
347 #ifdef CONFIG_INET
348         secpath_get(skb->sp);
349 #endif
350         memcpy(n->cb, skb->cb, sizeof(skb->cb));
351         C(len);
352         C(data_len);
353         C(csum);
354         C(local_df);
355         n->cloned = 1;
356         C(pkt_type);
357         C(ip_summed);
358         C(priority);
359         C(protocol);
360         C(security);
361         n->destructor = NULL;
362 #ifdef CONFIG_NETFILTER
363         C(nfmark);
364         C(nfcache);
365         C(nfct);
366         nf_conntrack_get(skb->nfct);
367         C(nfctinfo);
368 #ifdef CONFIG_NETFILTER_DEBUG
369         C(nf_debug);
370 #endif
371 #ifdef CONFIG_BRIDGE_NETFILTER
372         C(nf_bridge);
373         nf_bridge_get(skb->nf_bridge);
374 #endif
375 #endif /*CONFIG_NETFILTER*/
376 #if defined(CONFIG_HIPPI)
377         C(private);
378 #endif
379 #ifdef CONFIG_NET_SCHED
380         C(tc_index);
381 #ifdef CONFIG_NET_CLS_ACT
382         n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
383         n->tc_verd = CLR_TC_OK2MUNGE(skb->tc_verd);
384         n->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
385         C(input_dev);
386         C(tc_classid);
387 #endif
388
389 #endif
390         C(truesize);
391         atomic_set(&n->users, 1);
392         C(head);
393         C(data);
394         C(tail);
395         C(end);
396
397         atomic_inc(&(skb_shinfo(skb)->dataref));
398         skb->cloned = 1;
399
400         return n;
401 }
402
403 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
404 {
405         /*
406          *      Shift between the two data areas in bytes
407          */
408         unsigned long offset = new->data - old->data;
409
410         new->list       = NULL;
411         new->sk         = NULL;
412         new->dev        = old->dev;
413         new->real_dev   = old->real_dev;
414         new->priority   = old->priority;
415         new->protocol   = old->protocol;
416         new->dst        = dst_clone(old->dst);
417 #ifdef CONFIG_INET
418         new->sp         = secpath_get(old->sp);
419 #endif
420         new->h.raw      = old->h.raw + offset;
421         new->nh.raw     = old->nh.raw + offset;
422         new->mac.raw    = old->mac.raw + offset;
423         memcpy(new->cb, old->cb, sizeof(old->cb));
424         new->local_df   = old->local_df;
425         new->pkt_type   = old->pkt_type;
426         new->stamp      = old->stamp;
427         new->destructor = NULL;
428         new->security   = old->security;
429 #ifdef CONFIG_NETFILTER
430         new->nfmark     = old->nfmark;
431         new->nfcache    = old->nfcache;
432         new->nfct       = old->nfct;
433         nf_conntrack_get(old->nfct);
434         new->nfctinfo   = old->nfctinfo;
435 #ifdef CONFIG_NETFILTER_DEBUG
436         new->nf_debug   = old->nf_debug;
437 #endif
438 #ifdef CONFIG_BRIDGE_NETFILTER
439         new->nf_bridge  = old->nf_bridge;
440         nf_bridge_get(old->nf_bridge);
441 #endif
442 #endif
443 #ifdef CONFIG_NET_SCHED
444 #ifdef CONFIG_NET_CLS_ACT
445         new->tc_verd = old->tc_verd;
446 #endif
447         new->tc_index   = old->tc_index;
448 #endif
449         atomic_set(&new->users, 1);
450         skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
451         skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
452 }
453
454 /**
455  *      skb_copy        -       create private copy of an sk_buff
456  *      @skb: buffer to copy
457  *      @gfp_mask: allocation priority
458  *
459  *      Make a copy of both an &sk_buff and its data. This is used when the
460  *      caller wishes to modify the data and needs a private copy of the
461  *      data to alter. Returns %NULL on failure or the pointer to the buffer
462  *      on success. The returned buffer has a reference count of 1.
463  *
464  *      As by-product this function converts non-linear &sk_buff to linear
465  *      one, so that &sk_buff becomes completely private and caller is allowed
466  *      to modify all the data of returned buffer. This means that this
467  *      function is not recommended for use in circumstances when only
468  *      header is going to be modified. Use pskb_copy() instead.
469  */
470
471 struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)
472 {
473         int headerlen = skb->data - skb->head;
474         /*
475          *      Allocate the copy buffer
476          */
477         struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
478                                       gfp_mask);
479         if (!n)
480                 return NULL;
481
482         /* Set the data pointer */
483         skb_reserve(n, headerlen);
484         /* Set the tail pointer and length */
485         skb_put(n, skb->len);
486         n->csum      = skb->csum;
487         n->ip_summed = skb->ip_summed;
488
489         if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
490                 BUG();
491
492         copy_skb_header(n, skb);
493         return n;
494 }
495
496
497 /**
498  *      pskb_copy       -       create copy of an sk_buff with private head.
499  *      @skb: buffer to copy
500  *      @gfp_mask: allocation priority
501  *
502  *      Make a copy of both an &sk_buff and part of its data, located
503  *      in header. Fragmented data remain shared. This is used when
504  *      the caller wishes to modify only header of &sk_buff and needs
505  *      private copy of the header to alter. Returns %NULL on failure
506  *      or the pointer to the buffer on success.
507  *      The returned buffer has a reference count of 1.
508  */
509
510 struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask)
511 {
512         /*
513          *      Allocate the copy buffer
514          */
515         struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
516
517         if (!n)
518                 goto out;
519
520         /* Set the data pointer */
521         skb_reserve(n, skb->data - skb->head);
522         /* Set the tail pointer and length */
523         skb_put(n, skb_headlen(skb));
524         /* Copy the bytes */
525         memcpy(n->data, skb->data, n->len);
526         n->csum      = skb->csum;
527         n->ip_summed = skb->ip_summed;
528
529         n->data_len  = skb->data_len;
530         n->len       = skb->len;
531
532         if (skb_shinfo(skb)->nr_frags) {
533                 int i;
534
535                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
536                         skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
537                         get_page(skb_shinfo(n)->frags[i].page);
538                 }
539                 skb_shinfo(n)->nr_frags = i;
540         }
541
542         if (skb_shinfo(skb)->frag_list) {
543                 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
544                 skb_clone_fraglist(n);
545         }
546
547         copy_skb_header(n, skb);
548 out:
549         return n;
550 }
551
552 /**
553  *      pskb_expand_head - reallocate header of &sk_buff
554  *      @skb: buffer to reallocate
555  *      @nhead: room to add at head
556  *      @ntail: room to add at tail
557  *      @gfp_mask: allocation priority
558  *
559  *      Expands (or creates identical copy, if &nhead and &ntail are zero)
560  *      header of skb. &sk_buff itself is not changed. &sk_buff MUST have
561  *      reference count of 1. Returns zero in the case of success or error,
562  *      if expansion failed. In the last case, &sk_buff is not changed.
563  *
564  *      All the pointers pointing into skb header may change and must be
565  *      reloaded after call to this function.
566  */
567
568 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask)
569 {
570         int i;
571         u8 *data;
572         int size = nhead + (skb->end - skb->head) + ntail;
573         long off;
574
575         if (skb_shared(skb))
576                 BUG();
577
578         size = SKB_DATA_ALIGN(size);
579
580         data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
581         if (!data)
582                 goto nodata;
583
584         /* Copy only real data... and, alas, header. This should be
585          * optimized for the cases when header is void. */
586         memcpy(data + nhead, skb->head, skb->tail - skb->head);
587         memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
588
589         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
590                 get_page(skb_shinfo(skb)->frags[i].page);
591
592         if (skb_shinfo(skb)->frag_list)
593                 skb_clone_fraglist(skb);
594
595         skb_release_data(skb);
596
597         off = (data + nhead) - skb->head;
598
599         skb->head     = data;
600         skb->end      = data + size;
601         skb->data    += off;
602         skb->tail    += off;
603         skb->mac.raw += off;
604         skb->h.raw   += off;
605         skb->nh.raw  += off;
606         skb->cloned   = 0;
607         atomic_set(&skb_shinfo(skb)->dataref, 1);
608         return 0;
609
610 nodata:
611         return -ENOMEM;
612 }
613
614 /* Make private copy of skb with writable head and some headroom */
615
616 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
617 {
618         struct sk_buff *skb2;
619         int delta = headroom - skb_headroom(skb);
620
621         if (delta <= 0)
622                 skb2 = pskb_copy(skb, GFP_ATOMIC);
623         else {
624                 skb2 = skb_clone(skb, GFP_ATOMIC);
625                 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
626                                              GFP_ATOMIC)) {
627                         kfree_skb(skb2);
628                         skb2 = NULL;
629                 }
630         }
631         return skb2;
632 }
633
634
635 /**
636  *      skb_copy_expand -       copy and expand sk_buff
637  *      @skb: buffer to copy
638  *      @newheadroom: new free bytes at head
639  *      @newtailroom: new free bytes at tail
640  *      @gfp_mask: allocation priority
641  *
642  *      Make a copy of both an &sk_buff and its data and while doing so
643  *      allocate additional space.
644  *
645  *      This is used when the caller wishes to modify the data and needs a
646  *      private copy of the data to alter as well as more space for new fields.
647  *      Returns %NULL on failure or the pointer to the buffer
648  *      on success. The returned buffer has a reference count of 1.
649  *
650  *      You must pass %GFP_ATOMIC as the allocation priority if this function
651  *      is called from an interrupt.
652  *
653  *      BUG ALERT: ip_summed is not copied. Why does this work? Is it used
654  *      only by netfilter in the cases when checksum is recalculated? --ANK
655  */
656 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
657                                 int newheadroom, int newtailroom, int gfp_mask)
658 {
659         /*
660          *      Allocate the copy buffer
661          */
662         struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
663                                       gfp_mask);
664         int head_copy_len, head_copy_off;
665
666         if (!n)
667                 return NULL;
668
669         skb_reserve(n, newheadroom);
670
671         /* Set the tail pointer and length */
672         skb_put(n, skb->len);
673
674         head_copy_len = skb_headroom(skb);
675         head_copy_off = 0;
676         if (newheadroom <= head_copy_len)
677                 head_copy_len = newheadroom;
678         else
679                 head_copy_off = newheadroom - head_copy_len;
680
681         /* Copy the linear header and data. */
682         if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
683                           skb->len + head_copy_len))
684                 BUG();
685
686         copy_skb_header(n, skb);
687
688         return n;
689 }
690
691 /**
692  *      skb_pad                 -       zero pad the tail of an skb
693  *      @skb: buffer to pad
694  *      @pad: space to pad
695  *
696  *      Ensure that a buffer is followed by a padding area that is zero
697  *      filled. Used by network drivers which may DMA or transfer data
698  *      beyond the buffer end onto the wire.
699  *
700  *      May return NULL in out of memory cases.
701  */
702  
703 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
704 {
705         struct sk_buff *nskb;
706         
707         /* If the skbuff is non linear tailroom is always zero.. */
708         if (skb_tailroom(skb) >= pad) {
709                 memset(skb->data+skb->len, 0, pad);
710                 return skb;
711         }
712         
713         nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
714         kfree_skb(skb);
715         if (nskb)
716                 memset(nskb->data+nskb->len, 0, pad);
717         return nskb;
718 }       
719  
720 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
721  * If realloc==0 and trimming is impossible without change of data,
722  * it is BUG().
723  */
724
725 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
726 {
727         int offset = skb_headlen(skb);
728         int nfrags = skb_shinfo(skb)->nr_frags;
729         int i;
730
731         for (i = 0; i < nfrags; i++) {
732                 int end = offset + skb_shinfo(skb)->frags[i].size;
733                 if (end > len) {
734                         if (skb_cloned(skb)) {
735                                 if (!realloc)
736                                         BUG();
737                                 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
738                                         return -ENOMEM;
739                         }
740                         if (len <= offset) {
741                                 put_page(skb_shinfo(skb)->frags[i].page);
742                                 skb_shinfo(skb)->nr_frags--;
743                         } else {
744                                 skb_shinfo(skb)->frags[i].size = len - offset;
745                         }
746                 }
747                 offset = end;
748         }
749
750         if (offset < len) {
751                 skb->data_len -= skb->len - len;
752                 skb->len       = len;
753         } else {
754                 if (len <= skb_headlen(skb)) {
755                         skb->len      = len;
756                         skb->data_len = 0;
757                         skb->tail     = skb->data + len;
758                         if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
759                                 skb_drop_fraglist(skb);
760                 } else {
761                         skb->data_len -= skb->len - len;
762                         skb->len       = len;
763                 }
764         }
765
766         return 0;
767 }
768
769 /**
770  *      __pskb_pull_tail - advance tail of skb header
771  *      @skb: buffer to reallocate
772  *      @delta: number of bytes to advance tail
773  *
774  *      The function makes a sense only on a fragmented &sk_buff,
775  *      it expands header moving its tail forward and copying necessary
776  *      data from fragmented part.
777  *
778  *      &sk_buff MUST have reference count of 1.
779  *
780  *      Returns %NULL (and &sk_buff does not change) if pull failed
781  *      or value of new tail of skb in the case of success.
782  *
783  *      All the pointers pointing into skb header may change and must be
784  *      reloaded after call to this function.
785  */
786
787 /* Moves tail of skb head forward, copying data from fragmented part,
788  * when it is necessary.
789  * 1. It may fail due to malloc failure.
790  * 2. It may change skb pointers.
791  *
792  * It is pretty complicated. Luckily, it is called only in exceptional cases.
793  */
794 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
795 {
796         /* If skb has not enough free space at tail, get new one
797          * plus 128 bytes for future expansions. If we have enough
798          * room at tail, reallocate without expansion only if skb is cloned.
799          */
800         int i, k, eat = (skb->tail + delta) - skb->end;
801
802         if (eat > 0 || skb_cloned(skb)) {
803                 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
804                                      GFP_ATOMIC))
805                         return NULL;
806         }
807
808         if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
809                 BUG();
810
811         /* Optimization: no fragments, no reasons to preestimate
812          * size of pulled pages. Superb.
813          */
814         if (!skb_shinfo(skb)->frag_list)
815                 goto pull_pages;
816
817         /* Estimate size of pulled pages. */
818         eat = delta;
819         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
820                 if (skb_shinfo(skb)->frags[i].size >= eat)
821                         goto pull_pages;
822                 eat -= skb_shinfo(skb)->frags[i].size;
823         }
824
825         /* If we need update frag list, we are in troubles.
826          * Certainly, it possible to add an offset to skb data,
827          * but taking into account that pulling is expected to
828          * be very rare operation, it is worth to fight against
829          * further bloating skb head and crucify ourselves here instead.
830          * Pure masohism, indeed. 8)8)
831          */
832         if (eat) {
833                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
834                 struct sk_buff *clone = NULL;
835                 struct sk_buff *insp = NULL;
836
837                 do {
838                         if (!list)
839                                 BUG();
840
841                         if (list->len <= eat) {
842                                 /* Eaten as whole. */
843                                 eat -= list->len;
844                                 list = list->next;
845                                 insp = list;
846                         } else {
847                                 /* Eaten partially. */
848
849                                 if (skb_shared(list)) {
850                                         /* Sucks! We need to fork list. :-( */
851                                         clone = skb_clone(list, GFP_ATOMIC);
852                                         if (!clone)
853                                                 return NULL;
854                                         insp = list->next;
855                                         list = clone;
856                                 } else {
857                                         /* This may be pulled without
858                                          * problems. */
859                                         insp = list;
860                                 }
861                                 if (!pskb_pull(list, eat)) {
862                                         if (clone)
863                                                 kfree_skb(clone);
864                                         return NULL;
865                                 }
866                                 break;
867                         }
868                 } while (eat);
869
870                 /* Free pulled out fragments. */
871                 while ((list = skb_shinfo(skb)->frag_list) != insp) {
872                         skb_shinfo(skb)->frag_list = list->next;
873                         kfree_skb(list);
874                 }
875                 /* And insert new clone at head. */
876                 if (clone) {
877                         clone->next = list;
878                         skb_shinfo(skb)->frag_list = clone;
879                 }
880         }
881         /* Success! Now we may commit changes to skb data. */
882
883 pull_pages:
884         eat = delta;
885         k = 0;
886         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
887                 if (skb_shinfo(skb)->frags[i].size <= eat) {
888                         put_page(skb_shinfo(skb)->frags[i].page);
889                         eat -= skb_shinfo(skb)->frags[i].size;
890                 } else {
891                         skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
892                         if (eat) {
893                                 skb_shinfo(skb)->frags[k].page_offset += eat;
894                                 skb_shinfo(skb)->frags[k].size -= eat;
895                                 eat = 0;
896                         }
897                         k++;
898                 }
899         }
900         skb_shinfo(skb)->nr_frags = k;
901
902         skb->tail     += delta;
903         skb->data_len -= delta;
904
905         return skb->tail;
906 }
907
908 /* Copy some data bits from skb to kernel buffer. */
909
910 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
911 {
912         int i, copy;
913         int start = skb_headlen(skb);
914
915         if (offset > (int)skb->len - len)
916                 goto fault;
917
918         /* Copy header. */
919         if ((copy = start - offset) > 0) {
920                 if (copy > len)
921                         copy = len;
922                 memcpy(to, skb->data + offset, copy);
923                 if ((len -= copy) == 0)
924                         return 0;
925                 offset += copy;
926                 to     += copy;
927         }
928
929         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
930                 int end;
931
932                 BUG_TRAP(start <= offset + len);
933
934                 end = start + skb_shinfo(skb)->frags[i].size;
935                 if ((copy = end - offset) > 0) {
936                         u8 *vaddr;
937
938                         if (copy > len)
939                                 copy = len;
940
941                         vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
942                         memcpy(to,
943                                vaddr + skb_shinfo(skb)->frags[i].page_offset+
944                                offset - start, copy);
945                         kunmap_skb_frag(vaddr);
946
947                         if ((len -= copy) == 0)
948                                 return 0;
949                         offset += copy;
950                         to     += copy;
951                 }
952                 start = end;
953         }
954
955         if (skb_shinfo(skb)->frag_list) {
956                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
957
958                 for (; list; list = list->next) {
959                         int end;
960
961                         BUG_TRAP(start <= offset + len);
962
963                         end = start + list->len;
964                         if ((copy = end - offset) > 0) {
965                                 if (copy > len)
966                                         copy = len;
967                                 if (skb_copy_bits(list, offset - start,
968                                                   to, copy))
969                                         goto fault;
970                                 if ((len -= copy) == 0)
971                                         return 0;
972                                 offset += copy;
973                                 to     += copy;
974                         }
975                         start = end;
976                 }
977         }
978         if (!len)
979                 return 0;
980
981 fault:
982         return -EFAULT;
983 }
984
985 /* Keep iterating until skb_iter_next returns false. */
986 void skb_iter_first(const struct sk_buff *skb, struct skb_iter *i)
987 {
988         i->len = skb_headlen(skb);
989         i->data = (unsigned char *)skb->data;
990         i->nextfrag = 0;
991         i->fraglist = NULL;
992 }
993
994 int skb_iter_next(const struct sk_buff *skb, struct skb_iter *i)
995 {
996         /* Unmap previous, if not head fragment. */
997         if (i->nextfrag)
998                 kunmap_skb_frag(i->data);
999
1000         if (i->fraglist) {
1001         fraglist:
1002                 /* We're iterating through fraglist. */
1003                 if (i->nextfrag < skb_shinfo(i->fraglist)->nr_frags) {
1004                         i->data = kmap_skb_frag(&skb_shinfo(i->fraglist)
1005                                                 ->frags[i->nextfrag]);
1006                         i->len = skb_shinfo(i->fraglist)->frags[i->nextfrag]
1007                                 .size;
1008                         i->nextfrag++;
1009                         return 1;
1010                 }
1011                 /* Fragments with fragments?  Too hard! */
1012                 BUG_ON(skb_shinfo(i->fraglist)->frag_list);
1013                 i->fraglist = i->fraglist->next;
1014                 if (!i->fraglist)
1015                         goto end;
1016
1017                 i->len = skb_headlen(i->fraglist);
1018                 i->data = i->fraglist->data;
1019                 i->nextfrag = 0;
1020                 return 1;
1021         }
1022
1023         if (i->nextfrag < skb_shinfo(skb)->nr_frags) {
1024                 i->data = kmap_skb_frag(&skb_shinfo(skb)->frags[i->nextfrag]);
1025                 i->len = skb_shinfo(skb)->frags[i->nextfrag].size;
1026                 i->nextfrag++;
1027                 return 1;
1028         }
1029
1030         i->fraglist = skb_shinfo(skb)->frag_list;
1031         if (i->fraglist)
1032                 goto fraglist;
1033
1034 end:
1035         /* Bug trap for callers */
1036         i->data = NULL;
1037         return 0;
1038 }
1039
1040 void skb_iter_abort(const struct sk_buff *skb, struct skb_iter *i)
1041 {
1042         /* Unmap previous, if not head fragment. */
1043         if (i->data && i->nextfrag)
1044                 kunmap_skb_frag(i->data);
1045         /* Bug trap for callers */
1046         i->data = NULL;
1047 }
1048
1049 /* Checksum skb data. */
1050
1051 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1052                           int len, unsigned int csum)
1053 {
1054         int start = skb_headlen(skb);
1055         int i, copy = start - offset;
1056         int pos = 0;
1057
1058         /* Checksum header. */
1059         if (copy > 0) {
1060                 if (copy > len)
1061                         copy = len;
1062                 csum = csum_partial(skb->data + offset, copy, csum);
1063                 if ((len -= copy) == 0)
1064                         return csum;
1065                 offset += copy;
1066                 pos     = copy;
1067         }
1068
1069         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1070                 int end;
1071
1072                 BUG_TRAP(start <= offset + len);
1073
1074                 end = start + skb_shinfo(skb)->frags[i].size;
1075                 if ((copy = end - offset) > 0) {
1076                         unsigned int csum2;
1077                         u8 *vaddr;
1078                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1079
1080                         if (copy > len)
1081                                 copy = len;
1082                         vaddr = kmap_skb_frag(frag);
1083                         csum2 = csum_partial(vaddr + frag->page_offset +
1084                                              offset - start, copy, 0);
1085                         kunmap_skb_frag(vaddr);
1086                         csum = csum_block_add(csum, csum2, pos);
1087                         if (!(len -= copy))
1088                                 return csum;
1089                         offset += copy;
1090                         pos    += copy;
1091                 }
1092                 start = end;
1093         }
1094
1095         if (skb_shinfo(skb)->frag_list) {
1096                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1097
1098                 for (; list; list = list->next) {
1099                         int end;
1100
1101                         BUG_TRAP(start <= offset + len);
1102
1103                         end = start + list->len;
1104                         if ((copy = end - offset) > 0) {
1105                                 unsigned int csum2;
1106                                 if (copy > len)
1107                                         copy = len;
1108                                 csum2 = skb_checksum(list, offset - start,
1109                                                      copy, 0);
1110                                 csum = csum_block_add(csum, csum2, pos);
1111                                 if ((len -= copy) == 0)
1112                                         return csum;
1113                                 offset += copy;
1114                                 pos    += copy;
1115                         }
1116                         start = end;
1117                 }
1118         }
1119         if (len)
1120                 BUG();
1121
1122         return csum;
1123 }
1124
1125 /* Both of above in one bottle. */
1126
1127 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1128                                     u8 *to, int len, unsigned int csum)
1129 {
1130         int start = skb_headlen(skb);
1131         int i, copy = start - offset;
1132         int pos = 0;
1133
1134         /* Copy header. */
1135         if (copy > 0) {
1136                 if (copy > len)
1137                         copy = len;
1138                 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1139                                                  copy, csum);
1140                 if ((len -= copy) == 0)
1141                         return csum;
1142                 offset += copy;
1143                 to     += copy;
1144                 pos     = copy;
1145         }
1146
1147         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1148                 int end;
1149
1150                 BUG_TRAP(start <= offset + len);
1151
1152                 end = start + skb_shinfo(skb)->frags[i].size;
1153                 if ((copy = end - offset) > 0) {
1154                         unsigned int csum2;
1155                         u8 *vaddr;
1156                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1157
1158                         if (copy > len)
1159                                 copy = len;
1160                         vaddr = kmap_skb_frag(frag);
1161                         csum2 = csum_partial_copy_nocheck(vaddr +
1162                                                           frag->page_offset +
1163                                                           offset - start, to,
1164                                                           copy, 0);
1165                         kunmap_skb_frag(vaddr);
1166                         csum = csum_block_add(csum, csum2, pos);
1167                         if (!(len -= copy))
1168                                 return csum;
1169                         offset += copy;
1170                         to     += copy;
1171                         pos    += copy;
1172                 }
1173                 start = end;
1174         }
1175
1176         if (skb_shinfo(skb)->frag_list) {
1177                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1178
1179                 for (; list; list = list->next) {
1180                         unsigned int csum2;
1181                         int end;
1182
1183                         BUG_TRAP(start <= offset + len);
1184
1185                         end = start + list->len;
1186                         if ((copy = end - offset) > 0) {
1187                                 if (copy > len)
1188                                         copy = len;
1189                                 csum2 = skb_copy_and_csum_bits(list,
1190                                                                offset - start,
1191                                                                to, copy, 0);
1192                                 csum = csum_block_add(csum, csum2, pos);
1193                                 if ((len -= copy) == 0)
1194                                         return csum;
1195                                 offset += copy;
1196                                 to     += copy;
1197                                 pos    += copy;
1198                         }
1199                         start = end;
1200                 }
1201         }
1202         if (len)
1203                 BUG();
1204         return csum;
1205 }
1206
1207 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1208 {
1209         unsigned int csum;
1210         long csstart;
1211
1212         if (skb->ip_summed == CHECKSUM_HW)
1213                 csstart = skb->h.raw - skb->data;
1214         else
1215                 csstart = skb_headlen(skb);
1216
1217         if (csstart > skb_headlen(skb))
1218                 BUG();
1219
1220         memcpy(to, skb->data, csstart);
1221
1222         csum = 0;
1223         if (csstart != skb->len)
1224                 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1225                                               skb->len - csstart, 0);
1226
1227         if (skb->ip_summed == CHECKSUM_HW) {
1228                 long csstuff = csstart + skb->csum;
1229
1230                 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1231         }
1232 }
1233
1234 /**
1235  *      skb_dequeue - remove from the head of the queue
1236  *      @list: list to dequeue from
1237  *
1238  *      Remove the head of the list. The list lock is taken so the function
1239  *      may be used safely with other locking list functions. The head item is
1240  *      returned or %NULL if the list is empty.
1241  */
1242
1243 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1244 {
1245         unsigned long flags;
1246         struct sk_buff *result;
1247
1248         spin_lock_irqsave(&list->lock, flags);
1249         result = __skb_dequeue(list);
1250         spin_unlock_irqrestore(&list->lock, flags);
1251         return result;
1252 }
1253
1254 /**
1255  *      skb_dequeue_tail - remove from the tail of the queue
1256  *      @list: list to dequeue from
1257  *
1258  *      Remove the tail of the list. The list lock is taken so the function
1259  *      may be used safely with other locking list functions. The tail item is
1260  *      returned or %NULL if the list is empty.
1261  */
1262 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1263 {
1264         unsigned long flags;
1265         struct sk_buff *result;
1266
1267         spin_lock_irqsave(&list->lock, flags);
1268         result = __skb_dequeue_tail(list);
1269         spin_unlock_irqrestore(&list->lock, flags);
1270         return result;
1271 }
1272
1273 /**
1274  *      skb_queue_purge - empty a list
1275  *      @list: list to empty
1276  *
1277  *      Delete all buffers on an &sk_buff list. Each buffer is removed from
1278  *      the list and one reference dropped. This function takes the list
1279  *      lock and is atomic with respect to other list locking functions.
1280  */
1281 void skb_queue_purge(struct sk_buff_head *list)
1282 {
1283         struct sk_buff *skb;
1284         while ((skb = skb_dequeue(list)) != NULL)
1285                 kfree_skb(skb);
1286 }
1287
1288 /**
1289  *      skb_queue_head - queue a buffer at the list head
1290  *      @list: list to use
1291  *      @newsk: buffer to queue
1292  *
1293  *      Queue a buffer at the start of the list. This function takes the
1294  *      list lock and can be used safely with other locking &sk_buff functions
1295  *      safely.
1296  *
1297  *      A buffer cannot be placed on two lists at the same time.
1298  */
1299 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1300 {
1301         unsigned long flags;
1302
1303         spin_lock_irqsave(&list->lock, flags);
1304         __skb_queue_head(list, newsk);
1305         spin_unlock_irqrestore(&list->lock, flags);
1306 }
1307
1308 /**
1309  *      skb_queue_tail - queue a buffer at the list tail
1310  *      @list: list to use
1311  *      @newsk: buffer to queue
1312  *
1313  *      Queue a buffer at the tail of the list. This function takes the
1314  *      list lock and can be used safely with other locking &sk_buff functions
1315  *      safely.
1316  *
1317  *      A buffer cannot be placed on two lists at the same time.
1318  */
1319 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1320 {
1321         unsigned long flags;
1322
1323         spin_lock_irqsave(&list->lock, flags);
1324         __skb_queue_tail(list, newsk);
1325         spin_unlock_irqrestore(&list->lock, flags);
1326 }
1327 /**
1328  *      skb_unlink      -       remove a buffer from a list
1329  *      @skb: buffer to remove
1330  *
1331  *      Place a packet after a given packet in a list. The list locks are taken
1332  *      and this function is atomic with respect to other list locked calls
1333  *
1334  *      Works even without knowing the list it is sitting on, which can be
1335  *      handy at times. It also means that THE LIST MUST EXIST when you
1336  *      unlink. Thus a list must have its contents unlinked before it is
1337  *      destroyed.
1338  */
1339 void skb_unlink(struct sk_buff *skb)
1340 {
1341         struct sk_buff_head *list = skb->list;
1342
1343         if (list) {
1344                 unsigned long flags;
1345
1346                 spin_lock_irqsave(&list->lock, flags);
1347                 if (skb->list == list)
1348                         __skb_unlink(skb, skb->list);
1349                 spin_unlock_irqrestore(&list->lock, flags);
1350         }
1351 }
1352
1353
1354 /**
1355  *      skb_append      -       append a buffer
1356  *      @old: buffer to insert after
1357  *      @newsk: buffer to insert
1358  *
1359  *      Place a packet after a given packet in a list. The list locks are taken
1360  *      and this function is atomic with respect to other list locked calls.
1361  *      A buffer cannot be placed on two lists at the same time.
1362  */
1363
1364 void skb_append(struct sk_buff *old, struct sk_buff *newsk)
1365 {
1366         unsigned long flags;
1367
1368         spin_lock_irqsave(&old->list->lock, flags);
1369         __skb_append(old, newsk);
1370         spin_unlock_irqrestore(&old->list->lock, flags);
1371 }
1372
1373
1374 /**
1375  *      skb_insert      -       insert a buffer
1376  *      @old: buffer to insert before
1377  *      @newsk: buffer to insert
1378  *
1379  *      Place a packet before a given packet in a list. The list locks are taken
1380  *      and this function is atomic with respect to other list locked calls
1381  *      A buffer cannot be placed on two lists at the same time.
1382  */
1383
1384 void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
1385 {
1386         unsigned long flags;
1387
1388         spin_lock_irqsave(&old->list->lock, flags);
1389         __skb_insert(newsk, old->prev, old, old->list);
1390         spin_unlock_irqrestore(&old->list->lock, flags);
1391 }
1392
1393 #if 0
1394 /*
1395  *      Tune the memory allocator for a new MTU size.
1396  */
1397 void skb_add_mtu(int mtu)
1398 {
1399         /* Must match allocation in alloc_skb */
1400         mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1401
1402         kmem_add_cache_size(mtu);
1403 }
1404 #endif
1405
1406 static inline void skb_split_inside_header(struct sk_buff *skb,
1407                                            struct sk_buff* skb1,
1408                                            const u32 len, const int pos)
1409 {
1410         int i;
1411
1412         memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1413
1414         /* And move data appendix as is. */
1415         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1416                 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1417
1418         skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1419         skb_shinfo(skb)->nr_frags  = 0;
1420         skb1->data_len             = skb->data_len;
1421         skb1->len                  += skb1->data_len;
1422         skb->data_len              = 0;
1423         skb->len                   = len;
1424         skb->tail                  = skb->data + len;
1425 }
1426
1427 static inline void skb_split_no_header(struct sk_buff *skb,
1428                                        struct sk_buff* skb1,
1429                                        const u32 len, int pos)
1430 {
1431         int i, k = 0;
1432         const int nfrags = skb_shinfo(skb)->nr_frags;
1433
1434         skb_shinfo(skb)->nr_frags = 0;
1435         skb1->len                 = skb1->data_len = skb->len - len;
1436         skb->len                  = len;
1437         skb->data_len             = len - pos;
1438
1439         for (i = 0; i < nfrags; i++) {
1440                 int size = skb_shinfo(skb)->frags[i].size;
1441
1442                 if (pos + size > len) {
1443                         skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1444
1445                         if (pos < len) {
1446                                 /* Split frag.
1447                                  * We have to variants in this case:
1448                                  * 1. Move all the frag to the second
1449                                  *    part, if it is possible. F.e.
1450                                  *    this approach is mandatory for TUX,
1451                                  *    where splitting is expensive.
1452                                  * 2. Split is accurately. We make this.
1453                                  */
1454                                 get_page(skb_shinfo(skb)->frags[i].page);
1455                                 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1456                                 skb_shinfo(skb1)->frags[0].size -= len - pos;
1457                                 skb_shinfo(skb)->frags[i].size  = len - pos;
1458                                 skb_shinfo(skb)->nr_frags++;
1459                         }
1460                         k++;
1461                 } else
1462                         skb_shinfo(skb)->nr_frags++;
1463                 pos += size;
1464         }
1465         skb_shinfo(skb1)->nr_frags = k;
1466 }
1467
1468 /**
1469  * skb_split - Split fragmented skb to two parts at length len.
1470  */
1471 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1472 {
1473         int pos = skb_headlen(skb);
1474
1475         if (len < pos)  /* Split line is inside header. */
1476                 skb_split_inside_header(skb, skb1, len, pos);
1477         else            /* Second chunk has no header, nothing to copy. */
1478                 skb_split_no_header(skb, skb1, len, pos);
1479 }
1480
1481 void __init skb_init(void)
1482 {
1483         skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1484                                               sizeof(struct sk_buff),
1485                                               0,
1486                                               SLAB_HWCACHE_ALIGN,
1487                                               NULL, NULL);
1488         if (!skbuff_head_cache)
1489                 panic("cannot create skbuff cache");
1490 }
1491
1492 EXPORT_SYMBOL(___pskb_trim);
1493 EXPORT_SYMBOL(__kfree_skb);
1494 EXPORT_SYMBOL(__pskb_pull_tail);
1495 EXPORT_SYMBOL(alloc_skb);
1496 EXPORT_SYMBOL(pskb_copy);
1497 EXPORT_SYMBOL(pskb_expand_head);
1498 EXPORT_SYMBOL(skb_checksum);
1499 EXPORT_SYMBOL(skb_clone);
1500 EXPORT_SYMBOL(skb_clone_fraglist);
1501 EXPORT_SYMBOL(skb_copy);
1502 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1503 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1504 EXPORT_SYMBOL(skb_copy_bits);
1505 EXPORT_SYMBOL(skb_copy_expand);
1506 EXPORT_SYMBOL(skb_over_panic);
1507 EXPORT_SYMBOL(skb_pad);
1508 EXPORT_SYMBOL(skb_realloc_headroom);
1509 EXPORT_SYMBOL(skb_under_panic);
1510 EXPORT_SYMBOL(skb_dequeue);
1511 EXPORT_SYMBOL(skb_dequeue_tail);
1512 EXPORT_SYMBOL(skb_insert);
1513 EXPORT_SYMBOL(skb_queue_purge);
1514 EXPORT_SYMBOL(skb_queue_head);
1515 EXPORT_SYMBOL(skb_queue_tail);
1516 EXPORT_SYMBOL(skb_unlink);
1517 EXPORT_SYMBOL(skb_append);
1518 EXPORT_SYMBOL(skb_split);
1519 EXPORT_SYMBOL(skb_iter_first);
1520 EXPORT_SYMBOL(skb_iter_next);
1521 EXPORT_SYMBOL(skb_iter_abort);