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