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