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