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