Merge branch 'tomato-K26-WL' into tomato-RT
[tomato.git] / release / src-rt / linux / linux-2.6 / net / core / skbuff.c
blob6e3a6e56c5b68f69d4438e87430c767faca3a8f5
1 /*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
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
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).
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.
38 * The functions in this file will not compile correctly with gcc 2.4.x
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/mm.h>
45 #include <linux/interrupt.h>
46 #include <linux/in.h>
47 #include <linux/inet.h>
48 #include <linux/slab.h>
49 #include <linux/netdevice.h>
50 #ifdef CONFIG_NET_CLS_ACT
51 #include <net/pkt_sched.h>
52 #endif
53 #include <linux/string.h>
54 #include <linux/skbuff.h>
55 #include <linux/splice.h>
56 #include <linux/cache.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/init.h>
59 #include <linux/scatterlist.h>
61 #include <net/protocol.h>
62 #include <net/dst.h>
63 #include <net/sock.h>
64 #include <net/checksum.h>
65 #include <net/xfrm.h>
67 #include <asm/uaccess.h>
68 #include <asm/system.h>
70 #include "kmap_skb.h"
72 static struct kmem_cache *skbuff_head_cache __read_mostly;
73 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
75 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
76 struct pipe_buffer *buf)
78 struct sk_buff *skb = (struct sk_buff *) buf->private;
80 kfree_skb(skb);
83 static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
84 struct pipe_buffer *buf)
86 struct sk_buff *skb = (struct sk_buff *) buf->private;
88 skb_get(skb);
91 static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
92 struct pipe_buffer *buf)
94 return 1;
98 /* Pipe buffer operations for a socket. */
99 static struct pipe_buf_operations sock_pipe_buf_ops = {
100 .can_merge = 0,
101 .map = generic_pipe_buf_map,
102 .unmap = generic_pipe_buf_unmap,
103 .confirm = generic_pipe_buf_confirm,
104 .release = sock_pipe_buf_release,
105 .steal = sock_pipe_buf_steal,
106 .get = sock_pipe_buf_get,
110 * Keep out-of-line to prevent kernel bloat.
111 * __builtin_return_address is not used because it is not always
112 * reliable.
116 * skb_over_panic - private function
117 * @skb: buffer
118 * @sz: size
119 * @here: address
121 * Out of line support code for skb_put(). Not user callable.
123 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
125 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
126 "data:%p tail:%#lx end:%#lx dev:%s\n",
127 here, skb->len, sz, skb->head, skb->data,
128 (unsigned long)skb->tail, (unsigned long)skb->end,
129 skb->dev ? skb->dev->name : "<NULL>");
130 BUG();
134 * skb_under_panic - private function
135 * @skb: buffer
136 * @sz: size
137 * @here: address
139 * Out of line support code for skb_push(). Not user callable.
142 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
144 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
145 "data:%p tail:%#lx end:%#lx dev:%s\n",
146 here, skb->len, sz, skb->head, skb->data,
147 (unsigned long)skb->tail, (unsigned long)skb->end,
148 skb->dev ? skb->dev->name : "<NULL>");
149 BUG();
152 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
154 * [BEEP] leaks.
159 * __alloc_skb - allocate a network buffer
160 * @size: size to allocate
161 * @gfp_mask: allocation mask
162 * @fclone: allocate from fclone cache instead of head cache
163 * and allocate a cloned (child) skb
164 * @node: numa node to allocate memory on
166 * Allocate a new &sk_buff. The returned buffer has no headroom and a
167 * tail room of size bytes. The object has a reference count of one.
168 * The return is the buffer. On a failure the return is %NULL.
170 * Buffers may only be allocated from interrupts using a @gfp_mask of
171 * %GFP_ATOMIC.
173 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
174 int fclone, int node)
176 struct kmem_cache *cache;
177 struct skb_shared_info *shinfo;
178 struct sk_buff *skb;
179 u8 *data;
181 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
183 /* Get the HEAD */
184 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
185 if (!skb)
186 goto out;
187 prefetchw(skb);
189 size = SKB_DATA_ALIGN(size);
190 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
191 gfp_mask, node);
192 if (!data)
193 goto nodata;
194 prefetchw(data + size);
197 * See comment in sk_buff definition, just before the 'tail' member
199 memset(skb, 0, offsetof(struct sk_buff, tail));
200 skb->truesize = size + sizeof(struct sk_buff);
201 atomic_set(&skb->users, 1);
202 skb->head = data;
203 skb->data = data;
204 skb_reset_tail_pointer(skb);
205 skb->end = skb->tail + size;
206 #ifdef NET_SKBUFF_DATA_USES_OFFSET
207 skb->mac_header = ~0U;
208 #endif
209 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
210 skb->nfct_reasm = NULL;
211 skb->nfct = NULL;
212 skb->nfcache = 0;
213 #endif
214 #ifdef CONFIG_BRIDGE_NETFILTER
215 skb->nf_bridge = NULL;
216 #endif
217 #ifdef CONFIG_NET_SCHED
218 skb->tc_index = 0;
219 #ifdef CONFIG_NET_CLS_ACT
220 skb->tc_verd = 0;
221 #endif
222 #endif
223 #ifdef CONFIG_NET_DMA
224 memset(&skb->dma_cookie, 0, sizeof(dma_cookie_t));
225 #endif
226 #ifdef CONFIG_NETWORK_SECMARK
227 skb->secmark =0;
228 #endif
230 /* make sure we initialize shinfo sequentially */
231 shinfo = skb_shinfo(skb);
232 memset(shinfo, 0, offsetof(struct skb_shared_info, frags));
233 atomic_set(&shinfo->dataref, 1);
235 #if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
236 skb->imq_flags = 0;
237 skb->nf_info = NULL;
238 #endif
240 if (fclone) {
241 struct sk_buff *child = skb + 1;
242 atomic_t *fclone_ref = (atomic_t *) (child + 1);
244 skb->fclone = SKB_FCLONE_ORIG;
245 atomic_set(fclone_ref, 1);
247 child->fclone = SKB_FCLONE_UNAVAILABLE;
249 out:
250 return skb;
251 nodata:
252 kmem_cache_free(cache, skb);
253 skb = NULL;
254 goto out;
258 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
259 * @dev: network device to receive on
260 * @length: length to allocate
261 * @gfp_mask: get_free_pages mask, passed to alloc_skb
263 * Allocate a new &sk_buff and assign it a usage count of one. The
264 * buffer has unspecified headroom built in. Users should allocate
265 * the headroom they think they need without accounting for the
266 * built in space. The built in space is used for optimisations.
268 * %NULL is returned if there is no free memory.
270 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
271 unsigned int length, gfp_t gfp_mask)
273 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
274 struct sk_buff *skb;
276 skb = __alloc_skb(length + NET_SKB_PAD_ALLOC, gfp_mask, 0, node);
277 if (likely(skb)) {
278 skb_reserve(skb, NET_SKB_PAD_ALLOC);
279 skb->dev = dev;
281 return skb;
284 static void skb_drop_list(struct sk_buff **listp)
286 struct sk_buff *list = *listp;
288 *listp = NULL;
290 do {
291 struct sk_buff *this = list;
292 list = list->next;
293 kfree_skb(this);
294 } while (list);
297 static inline void skb_drop_fraglist(struct sk_buff *skb)
299 skb_drop_list(&skb_shinfo(skb)->frag_list);
302 static void skb_clone_fraglist(struct sk_buff *skb)
304 struct sk_buff *list;
306 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
307 skb_get(list);
310 static void skb_release_data(struct sk_buff *skb)
312 if (!skb->cloned ||
313 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
314 &skb_shinfo(skb)->dataref)) {
315 if (skb_shinfo(skb)->nr_frags) {
316 int i;
317 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
318 put_page(skb_shinfo(skb)->frags[i].page);
321 if (skb_shinfo(skb)->frag_list)
322 skb_drop_fraglist(skb);
324 kfree(skb->head);
329 * Free an skbuff by memory without cleaning the state.
331 void kfree_skbmem(struct sk_buff *skb)
333 struct sk_buff *other;
334 atomic_t *fclone_ref;
336 skb_release_data(skb);
337 switch (skb->fclone) {
338 case SKB_FCLONE_UNAVAILABLE:
339 kmem_cache_free(skbuff_head_cache, skb);
340 break;
342 case SKB_FCLONE_ORIG:
343 fclone_ref = (atomic_t *) (skb + 2);
344 if (atomic_dec_and_test(fclone_ref))
345 kmem_cache_free(skbuff_fclone_cache, skb);
346 break;
348 case SKB_FCLONE_CLONE:
349 fclone_ref = (atomic_t *) (skb + 1);
350 other = skb - 1;
352 /* The clone portion is available for
353 * fast-cloning again.
355 skb->fclone = SKB_FCLONE_UNAVAILABLE;
357 if (atomic_dec_and_test(fclone_ref))
358 kmem_cache_free(skbuff_fclone_cache, other);
359 break;
364 * __kfree_skb - private function
365 * @skb: buffer
367 * Free an sk_buff. Release anything attached to the buffer.
368 * Clean the state. This is an internal helper function. Users should
369 * always call kfree_skb
372 void __kfree_skb(struct sk_buff *skb)
374 dst_release(skb->dst);
375 #ifdef CONFIG_XFRM
376 secpath_put(skb->sp);
377 #endif
378 if (skb->destructor) {
379 WARN_ON(in_irq());
380 skb->destructor(skb);
382 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
383 nf_conntrack_put(skb->nfct);
384 nf_conntrack_put_reasm(skb->nfct_reasm);
385 #endif
386 #ifdef CONFIG_BRIDGE_NETFILTER
387 nf_bridge_put(skb->nf_bridge);
388 #endif
389 /* XXX: IS this still necessary? - JHS */
390 #ifdef CONFIG_NET_SCHED
391 skb->tc_index = 0;
392 #ifdef CONFIG_NET_CLS_ACT
393 skb->tc_verd = 0;
394 #endif
395 #endif
397 kfree_skbmem(skb);
401 * kfree_skb - free an sk_buff
402 * @skb: buffer to free
404 * Drop a reference to the buffer and free it if the usage count has
405 * hit zero.
407 void kfree_skb(struct sk_buff *skb)
409 if (unlikely(!skb))
410 return;
411 if (likely(atomic_read(&skb->users) == 1))
412 smp_rmb();
413 else if (likely(!atomic_dec_and_test(&skb->users)))
414 return;
415 __kfree_skb(skb);
418 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
420 new->tstamp = old->tstamp;
421 new->dev = old->dev;
422 new->transport_header = old->transport_header;
423 new->network_header = old->network_header;
424 new->mac_header = old->mac_header;
425 new->dst = dst_clone(old->dst);
426 #ifdef CONFIG_INET
427 new->sp = secpath_get(old->sp);
428 #endif
429 memcpy(new->cb, old->cb, sizeof(old->cb));
430 new->csum = old->csum;
431 new->local_df = old->local_df;
432 new->pkt_type = old->pkt_type;
433 new->ip_summed = old->ip_summed;
434 new->priority = old->priority;
435 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
436 new->ipvs_property = old->ipvs_property;
437 #endif
438 new->protocol = old->protocol;
439 new->mark = old->mark;
440 __nf_copy(new, old);
441 #ifdef CONFIG_NET_SCHED
442 new->tc_index = old->tc_index;
443 #ifdef CONFIG_NET_CLS_ACT
444 new->tc_verd = old->tc_verd;
445 #endif
446 #endif
447 skb_copy_secmark(new, old);
448 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
449 new->nfcache = old->nfcache;
450 #endif
454 * skb_clone - duplicate an sk_buff
455 * @skb: buffer to clone
456 * @gfp_mask: allocation priority
458 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
459 * copies share the same packet data but not structure. The new
460 * buffer has a reference count of 1. If the allocation fails the
461 * function returns %NULL otherwise the new buffer is returned.
463 * If this function is called from an interrupt gfp_mask() must be
464 * %GFP_ATOMIC.
467 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
469 struct sk_buff *n;
471 n = skb + 1;
472 if (skb->fclone == SKB_FCLONE_ORIG &&
473 n->fclone == SKB_FCLONE_UNAVAILABLE) {
474 atomic_t *fclone_ref = (atomic_t *) (n + 1);
475 n->fclone = SKB_FCLONE_CLONE;
476 atomic_inc(fclone_ref);
477 } else {
478 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
479 if (!n)
480 return NULL;
481 n->fclone = SKB_FCLONE_UNAVAILABLE;
484 #define C(x) n->x = skb->x
486 n->next = n->prev = NULL;
487 n->sk = NULL;
488 __copy_skb_header(n, skb);
490 C(len);
491 C(data_len);
492 C(mac_len);
493 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
494 n->cloned = 1;
495 n->nohdr = 0;
496 n->destructor = NULL;
497 C(iif);
498 C(tail);
499 C(end);
500 C(head);
501 C(data);
502 C(truesize);
503 atomic_set(&n->users, 1);
505 atomic_inc(&(skb_shinfo(skb)->dataref));
506 skb->cloned = 1;
508 return n;
511 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
513 #ifndef NET_SKBUFF_DATA_USES_OFFSET
515 * Shift between the two data areas in bytes
517 unsigned long offset = new->data - old->data;
518 #endif
520 __copy_skb_header(new, old);
522 #ifndef NET_SKBUFF_DATA_USES_OFFSET
523 /* {transport,network,mac}_header are relative to skb->head */
524 new->transport_header += offset;
525 new->network_header += offset;
526 if (skb_mac_header_was_set(new))
527 new->mac_header += offset;
528 #endif
529 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
530 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
531 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
535 * skb_copy - create private copy of an sk_buff
536 * @skb: buffer to copy
537 * @gfp_mask: allocation priority
539 * Make a copy of both an &sk_buff and its data. This is used when the
540 * caller wishes to modify the data and needs a private copy of the
541 * data to alter. Returns %NULL on failure or the pointer to the buffer
542 * on success. The returned buffer has a reference count of 1.
544 * As by-product this function converts non-linear &sk_buff to linear
545 * one, so that &sk_buff becomes completely private and caller is allowed
546 * to modify all the data of returned buffer. This means that this
547 * function is not recommended for use in circumstances when only
548 * header is going to be modified. Use pskb_copy() instead.
551 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
553 int headerlen = skb->data - skb->head;
555 * Allocate the copy buffer
557 struct sk_buff *n;
558 #ifdef NET_SKBUFF_DATA_USES_OFFSET
559 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
560 #else
561 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
562 #endif
563 if (!n)
564 return NULL;
566 /* Set the data pointer */
567 skb_reserve(n, headerlen);
568 /* Set the tail pointer and length */
569 skb_put(n, skb->len);
571 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
572 BUG();
574 copy_skb_header(n, skb);
575 return n;
580 * pskb_copy - create copy of an sk_buff with private head.
581 * @skb: buffer to copy
582 * @gfp_mask: allocation priority
584 * Make a copy of both an &sk_buff and part of its data, located
585 * in header. Fragmented data remain shared. This is used when
586 * the caller wishes to modify only header of &sk_buff and needs
587 * private copy of the header to alter. Returns %NULL on failure
588 * or the pointer to the buffer on success.
589 * The returned buffer has a reference count of 1.
592 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
595 * Allocate the copy buffer
597 struct sk_buff *n;
598 #ifdef NET_SKBUFF_DATA_USES_OFFSET
599 n = alloc_skb(skb->end, gfp_mask);
600 #else
601 n = alloc_skb(skb->end - skb->head, gfp_mask);
602 #endif
603 if (!n)
604 goto out;
606 /* Set the data pointer */
607 skb_reserve(n, skb->data - skb->head);
608 /* Set the tail pointer and length */
609 skb_put(n, skb_headlen(skb));
610 /* Copy the bytes */
611 skb_copy_from_linear_data(skb, n->data, n->len);
613 n->truesize += skb->data_len;
614 n->data_len = skb->data_len;
615 n->len = skb->len;
617 if (skb_shinfo(skb)->nr_frags) {
618 int i;
620 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
621 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
622 get_page(skb_shinfo(n)->frags[i].page);
624 skb_shinfo(n)->nr_frags = i;
627 if (skb_shinfo(skb)->frag_list) {
628 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
629 skb_clone_fraglist(n);
632 copy_skb_header(n, skb);
633 out:
634 return n;
638 * pskb_expand_head - reallocate header of &sk_buff
639 * @skb: buffer to reallocate
640 * @nhead: room to add at head
641 * @ntail: room to add at tail
642 * @gfp_mask: allocation priority
644 * Expands (or creates identical copy, if &nhead and &ntail are zero)
645 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
646 * reference count of 1. Returns zero in the case of success or error,
647 * if expansion failed. In the last case, &sk_buff is not changed.
649 * All the pointers pointing into skb header may change and must be
650 * reloaded after call to this function.
653 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
654 gfp_t gfp_mask)
656 int i;
657 u8 *data;
658 #ifdef NET_SKBUFF_DATA_USES_OFFSET
659 int size = nhead + skb->end + ntail;
660 #else
661 int size = nhead + (skb->end - skb->head) + ntail;
662 #endif
663 long off;
665 BUG_ON(nhead < 0);
667 if (skb_shared(skb))
668 BUG();
670 size = SKB_DATA_ALIGN(size);
672 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
673 if (!data)
674 goto nodata;
676 /* Copy only real data... and, alas, header. This should be
677 * optimized for the cases when header is void. */
678 #ifdef NET_SKBUFF_DATA_USES_OFFSET
679 memcpy(data + nhead, skb->head, skb->tail);
680 #else
681 memcpy(data + nhead, skb->head, skb->tail - skb->head);
682 #endif
683 memcpy(data + size, skb_end_pointer(skb),
684 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
686 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
687 get_page(skb_shinfo(skb)->frags[i].page);
689 if (skb_shinfo(skb)->frag_list)
690 skb_clone_fraglist(skb);
692 skb_release_data(skb);
694 off = (data + nhead) - skb->head;
696 skb->head = data;
697 skb->data += off;
698 #ifdef NET_SKBUFF_DATA_USES_OFFSET
699 skb->end = size;
700 off = nhead;
701 #else
702 skb->end = skb->head + size;
703 #endif
704 /* {transport,network,mac}_header and tail are relative to skb->head */
705 skb->tail += off;
706 skb->transport_header += off;
707 skb->network_header += off;
708 if (skb_mac_header_was_set(skb))
709 skb->mac_header += off;
710 /* Only adjust this if it actually is csum_start rather than csum */
711 if (skb->ip_summed == CHECKSUM_PARTIAL)
712 skb->csum_start += nhead;
713 skb->cloned = 0;
714 skb->hdr_len = 0;
715 skb->nohdr = 0;
716 atomic_set(&skb_shinfo(skb)->dataref, 1);
717 return 0;
719 nodata:
720 return -ENOMEM;
723 /* Make private copy of skb with writable head and some headroom */
725 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
727 struct sk_buff *skb2;
728 int delta = headroom - skb_headroom(skb);
730 if (delta <= 0)
731 skb2 = pskb_copy(skb, GFP_ATOMIC);
732 else {
733 skb2 = skb_clone(skb, GFP_ATOMIC);
734 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
735 GFP_ATOMIC)) {
736 kfree_skb(skb2);
737 skb2 = NULL;
740 return skb2;
745 * skb_copy_expand - copy and expand sk_buff
746 * @skb: buffer to copy
747 * @newheadroom: new free bytes at head
748 * @newtailroom: new free bytes at tail
749 * @gfp_mask: allocation priority
751 * Make a copy of both an &sk_buff and its data and while doing so
752 * allocate additional space.
754 * This is used when the caller wishes to modify the data and needs a
755 * private copy of the data to alter as well as more space for new fields.
756 * Returns %NULL on failure or the pointer to the buffer
757 * on success. The returned buffer has a reference count of 1.
759 * You must pass %GFP_ATOMIC as the allocation priority if this function
760 * is called from an interrupt.
762 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
763 int newheadroom, int newtailroom,
764 gfp_t gfp_mask)
767 * Allocate the copy buffer
769 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
770 gfp_mask);
771 int oldheadroom = skb_headroom(skb);
772 int head_copy_len, head_copy_off;
773 int off;
775 if (!n)
776 return NULL;
778 skb_reserve(n, newheadroom);
780 /* Set the tail pointer and length */
781 skb_put(n, skb->len);
783 head_copy_len = oldheadroom;
784 head_copy_off = 0;
785 if (newheadroom <= head_copy_len)
786 head_copy_len = newheadroom;
787 else
788 head_copy_off = newheadroom - head_copy_len;
790 /* Copy the linear header and data. */
791 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
792 skb->len + head_copy_len))
793 BUG();
795 copy_skb_header(n, skb);
797 off = newheadroom - oldheadroom;
798 if (n->ip_summed == CHECKSUM_PARTIAL)
799 n->csum_start += off;
800 #ifdef NET_SKBUFF_DATA_USES_OFFSET
801 n->transport_header += off;
802 n->network_header += off;
803 if (skb_mac_header_was_set(skb))
804 n->mac_header += off;
805 #endif
807 return n;
811 * skb_pad - zero pad the tail of an skb
812 * @skb: buffer to pad
813 * @pad: space to pad
815 * Ensure that a buffer is followed by a padding area that is zero
816 * filled. Used by network drivers which may DMA or transfer data
817 * beyond the buffer end onto the wire.
819 * May return error in out of memory cases. The skb is freed on error.
822 int skb_pad(struct sk_buff *skb, int pad)
824 int err;
825 int ntail;
827 /* If the skbuff is non linear tailroom is always zero.. */
828 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
829 memset(skb->data+skb->len, 0, pad);
830 return 0;
833 ntail = skb->data_len + pad - (skb->end - skb->tail);
834 if (likely(skb_cloned(skb) || ntail > 0)) {
835 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
836 if (unlikely(err))
837 goto free_skb;
840 /* FIXME: The use of this function with non-linear skb's really needs
841 * to be audited.
843 err = skb_linearize(skb);
844 if (unlikely(err))
845 goto free_skb;
847 memset(skb->data + skb->len, 0, pad);
848 return 0;
850 free_skb:
851 kfree_skb(skb);
852 return err;
855 /* Trims skb to length len. It can change skb pointers.
858 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
860 struct sk_buff **fragp;
861 struct sk_buff *frag;
862 int offset = skb_headlen(skb);
863 int nfrags = skb_shinfo(skb)->nr_frags;
864 int i;
865 int err;
867 if (skb_cloned(skb) &&
868 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
869 return err;
871 i = 0;
872 if (offset >= len)
873 goto drop_pages;
875 for (; i < nfrags; i++) {
876 int end = offset + skb_shinfo(skb)->frags[i].size;
878 if (end < len) {
879 offset = end;
880 continue;
883 skb_shinfo(skb)->frags[i++].size = len - offset;
885 drop_pages:
886 skb_shinfo(skb)->nr_frags = i;
888 for (; i < nfrags; i++)
889 put_page(skb_shinfo(skb)->frags[i].page);
891 if (skb_shinfo(skb)->frag_list)
892 skb_drop_fraglist(skb);
893 goto done;
896 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
897 fragp = &frag->next) {
898 int end = offset + frag->len;
900 if (skb_shared(frag)) {
901 struct sk_buff *nfrag;
903 nfrag = skb_clone(frag, GFP_ATOMIC);
904 if (unlikely(!nfrag))
905 return -ENOMEM;
907 nfrag->next = frag->next;
908 kfree_skb(frag);
909 frag = nfrag;
910 *fragp = frag;
913 if (end < len) {
914 offset = end;
915 continue;
918 if (end > len &&
919 unlikely((err = pskb_trim(frag, len - offset))))
920 return err;
922 if (frag->next)
923 skb_drop_list(&frag->next);
924 break;
927 done:
928 if (len > skb_headlen(skb)) {
929 skb->data_len -= skb->len - len;
930 skb->len = len;
931 } else {
932 skb->len = len;
933 skb->data_len = 0;
934 skb_set_tail_pointer(skb, len);
937 return 0;
941 * __pskb_pull_tail - advance tail of skb header
942 * @skb: buffer to reallocate
943 * @delta: number of bytes to advance tail
945 * The function makes a sense only on a fragmented &sk_buff,
946 * it expands header moving its tail forward and copying necessary
947 * data from fragmented part.
949 * &sk_buff MUST have reference count of 1.
951 * Returns %NULL (and &sk_buff does not change) if pull failed
952 * or value of new tail of skb in the case of success.
954 * All the pointers pointing into skb header may change and must be
955 * reloaded after call to this function.
958 /* Moves tail of skb head forward, copying data from fragmented part,
959 * when it is necessary.
960 * 1. It may fail due to malloc failure.
961 * 2. It may change skb pointers.
963 * It is pretty complicated. Luckily, it is called only in exceptional cases.
965 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
967 /* If skb has not enough free space at tail, get new one
968 * plus 128 bytes for future expansions. If we have enough
969 * room at tail, reallocate without expansion only if skb is cloned.
971 int i, k, eat = (skb->tail + delta) - skb->end;
973 if (eat > 0 || skb_cloned(skb)) {
974 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
975 GFP_ATOMIC))
976 return NULL;
979 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
980 BUG();
982 /* Optimization: no fragments, no reasons to preestimate
983 * size of pulled pages. Superb.
985 if (!skb_shinfo(skb)->frag_list)
986 goto pull_pages;
988 /* Estimate size of pulled pages. */
989 eat = delta;
990 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
991 if (skb_shinfo(skb)->frags[i].size >= eat)
992 goto pull_pages;
993 eat -= skb_shinfo(skb)->frags[i].size;
996 /* If we need update frag list, we are in troubles.
997 * Certainly, it possible to add an offset to skb data,
998 * but taking into account that pulling is expected to
999 * be very rare operation, it is worth to fight against
1000 * further bloating skb head and crucify ourselves here instead.
1001 * Pure masohism, indeed. 8)8)
1003 if (eat) {
1004 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1005 struct sk_buff *clone = NULL;
1006 struct sk_buff *insp = NULL;
1008 do {
1009 BUG_ON(!list);
1011 if (list->len <= eat) {
1012 /* Eaten as whole. */
1013 eat -= list->len;
1014 list = list->next;
1015 insp = list;
1016 } else {
1017 /* Eaten partially. */
1019 if (skb_shared(list)) {
1020 /* Sucks! We need to fork list. :-( */
1021 clone = skb_clone(list, GFP_ATOMIC);
1022 if (!clone)
1023 return NULL;
1024 insp = list->next;
1025 list = clone;
1026 } else {
1027 /* This may be pulled without
1028 * problems. */
1029 insp = list;
1031 if (!pskb_pull(list, eat)) {
1032 if (clone)
1033 kfree_skb(clone);
1034 return NULL;
1036 break;
1038 } while (eat);
1040 /* Free pulled out fragments. */
1041 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1042 skb_shinfo(skb)->frag_list = list->next;
1043 kfree_skb(list);
1045 /* And insert new clone at head. */
1046 if (clone) {
1047 clone->next = list;
1048 skb_shinfo(skb)->frag_list = clone;
1051 /* Success! Now we may commit changes to skb data. */
1053 pull_pages:
1054 eat = delta;
1055 k = 0;
1056 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1057 if (skb_shinfo(skb)->frags[i].size <= eat) {
1058 put_page(skb_shinfo(skb)->frags[i].page);
1059 eat -= skb_shinfo(skb)->frags[i].size;
1060 } else {
1061 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1062 if (eat) {
1063 skb_shinfo(skb)->frags[k].page_offset += eat;
1064 skb_shinfo(skb)->frags[k].size -= eat;
1065 eat = 0;
1067 k++;
1070 skb_shinfo(skb)->nr_frags = k;
1072 skb->tail += delta;
1073 skb->data_len -= delta;
1075 return skb_tail_pointer(skb);
1078 /* Copy some data bits from skb to kernel buffer. */
1080 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1082 int i, copy;
1083 int start = skb_headlen(skb);
1085 if (offset > (int)skb->len - len)
1086 goto fault;
1088 /* Copy header. */
1089 if ((copy = start - offset) > 0) {
1090 if (copy > len)
1091 copy = len;
1092 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1093 if ((len -= copy) == 0)
1094 return 0;
1095 offset += copy;
1096 to += copy;
1099 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1100 int end;
1102 BUG_TRAP(start <= offset + len);
1104 end = start + skb_shinfo(skb)->frags[i].size;
1105 if ((copy = end - offset) > 0) {
1106 u8 *vaddr;
1108 if (copy > len)
1109 copy = len;
1111 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1112 memcpy(to,
1113 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1114 offset - start, copy);
1115 kunmap_skb_frag(vaddr);
1117 if ((len -= copy) == 0)
1118 return 0;
1119 offset += copy;
1120 to += copy;
1122 start = end;
1125 if (skb_shinfo(skb)->frag_list) {
1126 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1128 for (; list; list = list->next) {
1129 int end;
1131 BUG_TRAP(start <= offset + len);
1133 end = start + list->len;
1134 if ((copy = end - offset) > 0) {
1135 if (copy > len)
1136 copy = len;
1137 if (skb_copy_bits(list, offset - start,
1138 to, copy))
1139 goto fault;
1140 if ((len -= copy) == 0)
1141 return 0;
1142 offset += copy;
1143 to += copy;
1145 start = end;
1148 if (!len)
1149 return 0;
1151 fault:
1152 return -EFAULT;
1156 * Callback from splice_to_pipe(), if we need to release some pages
1157 * at the end of the spd in case we error'ed out in filling the pipe.
1159 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1161 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1163 kfree_skb(skb);
1167 * Fill page/offset/length into spd, if it can hold more pages.
1169 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1170 unsigned int len, unsigned int offset,
1171 struct sk_buff *skb)
1173 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1174 return 1;
1176 spd->pages[spd->nr_pages] = page;
1177 spd->partial[spd->nr_pages].len = len;
1178 spd->partial[spd->nr_pages].offset = offset;
1179 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1180 spd->nr_pages++;
1181 return 0;
1185 * Map linear and fragment data from the skb to spd. Returns number of
1186 * pages mapped.
1188 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1189 unsigned int *total_len,
1190 struct splice_pipe_desc *spd)
1192 unsigned int nr_pages = spd->nr_pages;
1193 unsigned int poff, plen, len, toff, tlen;
1194 int headlen, seg, error = 0;
1196 toff = *offset;
1197 tlen = *total_len;
1198 if (!tlen) {
1199 error = 1;
1200 goto err;
1204 * if the offset is greater than the linear part, go directly to
1205 * the fragments.
1207 headlen = skb_headlen(skb);
1208 if (toff >= headlen) {
1209 toff -= headlen;
1210 goto map_frag;
1214 * first map the linear region into the pages/partial map, skipping
1215 * any potential initial offset.
1217 len = 0;
1218 while (len < headlen) {
1219 void *p = skb->data + len;
1221 poff = (unsigned long) p & (PAGE_SIZE - 1);
1222 plen = min_t(unsigned int, headlen - len, PAGE_SIZE - poff);
1223 len += plen;
1225 if (toff) {
1226 if (plen <= toff) {
1227 toff -= plen;
1228 continue;
1230 plen -= toff;
1231 poff += toff;
1232 toff = 0;
1235 plen = min(plen, tlen);
1236 if (!plen)
1237 break;
1240 * just jump directly to update and return, no point
1241 * in going over fragments when the output is full.
1243 error = spd_fill_page(spd, virt_to_page(p), plen, poff, skb);
1244 if (error)
1245 goto done;
1247 tlen -= plen;
1251 * then map the fragments
1253 map_frag:
1254 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1255 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1257 plen = f->size;
1258 poff = f->page_offset;
1260 if (toff) {
1261 if (plen <= toff) {
1262 toff -= plen;
1263 continue;
1265 plen -= toff;
1266 poff += toff;
1267 toff = 0;
1270 plen = min(plen, tlen);
1271 if (!plen)
1272 break;
1274 error = spd_fill_page(spd, f->page, plen, poff, skb);
1275 if (error)
1276 break;
1278 tlen -= plen;
1281 done:
1282 if (spd->nr_pages - nr_pages) {
1283 *offset = 0;
1284 *total_len = tlen;
1285 return 0;
1287 err:
1288 /* update the offset to reflect the linear part skip, if any */
1289 if (!error)
1290 *offset = toff;
1291 return error;
1295 * Map data from the skb to a pipe. Should handle both the linear part,
1296 * the fragments, and the frag list. It does NOT handle frag lists within
1297 * the frag list, if such a thing exists. We'd probably need to recurse to
1298 * handle that cleanly.
1300 int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1301 struct pipe_inode_info *pipe, unsigned int tlen,
1302 unsigned int flags)
1304 struct partial_page partial[PIPE_BUFFERS];
1305 struct page *pages[PIPE_BUFFERS];
1306 struct splice_pipe_desc spd = {
1307 .pages = pages,
1308 .partial = partial,
1309 .flags = flags,
1310 .ops = &sock_pipe_buf_ops,
1311 .spd_release = sock_spd_release,
1313 struct sk_buff *skb;
1316 * I'd love to avoid the clone here, but tcp_read_sock()
1317 * ignores reference counts and unconditonally kills the sk_buff
1318 * on return from the actor.
1320 skb = skb_clone(__skb, GFP_KERNEL);
1321 if (unlikely(!skb))
1322 return -ENOMEM;
1325 * __skb_splice_bits() only fails if the output has no room left,
1326 * so no point in going over the frag_list for the error case.
1328 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1329 goto done;
1330 else if (!tlen)
1331 goto done;
1334 * now see if we have a frag_list to map
1336 if (skb_shinfo(skb)->frag_list) {
1337 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1339 for (; list && tlen; list = list->next) {
1340 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1341 break;
1345 done:
1347 * drop our reference to the clone, the pipe consumption will
1348 * drop the rest.
1350 kfree_skb(skb);
1352 if (spd.nr_pages) {
1353 int ret;
1354 struct sock *sk = __skb->sk;
1357 * Drop the socket lock, otherwise we have reverse
1358 * locking dependencies between sk_lock and i_mutex
1359 * here as compared to sendfile(). We enter here
1360 * with the socket lock held, and splice_to_pipe() will
1361 * grab the pipe inode lock. For sendfile() emulation,
1362 * we call into ->sendpage() with the i_mutex lock held
1363 * and networking will grab the socket lock.
1365 release_sock(sk);
1366 ret = splice_to_pipe(pipe, &spd);
1367 lock_sock(sk);
1368 return ret;
1371 return 0;
1375 * skb_store_bits - store bits from kernel buffer to skb
1376 * @skb: destination buffer
1377 * @offset: offset in destination
1378 * @from: source buffer
1379 * @len: number of bytes to copy
1381 * Copy the specified number of bytes from the source buffer to the
1382 * destination skb. This function handles all the messy bits of
1383 * traversing fragment lists and such.
1386 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1388 int i, copy;
1389 int start = skb_headlen(skb);
1391 if (offset > (int)skb->len - len)
1392 goto fault;
1394 if ((copy = start - offset) > 0) {
1395 if (copy > len)
1396 copy = len;
1397 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1398 if ((len -= copy) == 0)
1399 return 0;
1400 offset += copy;
1401 from += copy;
1404 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1405 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1406 int end;
1408 BUG_TRAP(start <= offset + len);
1410 end = start + frag->size;
1411 if ((copy = end - offset) > 0) {
1412 u8 *vaddr;
1414 if (copy > len)
1415 copy = len;
1417 vaddr = kmap_skb_frag(frag);
1418 memcpy(vaddr + frag->page_offset + offset - start,
1419 from, copy);
1420 kunmap_skb_frag(vaddr);
1422 if ((len -= copy) == 0)
1423 return 0;
1424 offset += copy;
1425 from += copy;
1427 start = end;
1430 if (skb_shinfo(skb)->frag_list) {
1431 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1433 for (; list; list = list->next) {
1434 int end;
1436 BUG_TRAP(start <= offset + len);
1438 end = start + list->len;
1439 if ((copy = end - offset) > 0) {
1440 if (copy > len)
1441 copy = len;
1442 if (skb_store_bits(list, offset - start,
1443 from, copy))
1444 goto fault;
1445 if ((len -= copy) == 0)
1446 return 0;
1447 offset += copy;
1448 from += copy;
1450 start = end;
1453 if (!len)
1454 return 0;
1456 fault:
1457 return -EFAULT;
1460 EXPORT_SYMBOL(skb_store_bits);
1462 /* Checksum skb data. */
1464 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1465 int len, __wsum csum)
1467 int start = skb_headlen(skb);
1468 int i, copy = start - offset;
1469 int pos = 0;
1471 /* Checksum header. */
1472 if (copy > 0) {
1473 if (copy > len)
1474 copy = len;
1475 csum = csum_partial(skb->data + offset, copy, csum);
1476 if ((len -= copy) == 0)
1477 return csum;
1478 offset += copy;
1479 pos = copy;
1482 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1483 int end;
1485 BUG_TRAP(start <= offset + len);
1487 end = start + skb_shinfo(skb)->frags[i].size;
1488 if ((copy = end - offset) > 0) {
1489 __wsum csum2;
1490 u8 *vaddr;
1491 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1493 if (copy > len)
1494 copy = len;
1495 vaddr = kmap_skb_frag(frag);
1496 csum2 = csum_partial(vaddr + frag->page_offset +
1497 offset - start, copy, 0);
1498 kunmap_skb_frag(vaddr);
1499 csum = csum_block_add(csum, csum2, pos);
1500 if (!(len -= copy))
1501 return csum;
1502 offset += copy;
1503 pos += copy;
1505 start = end;
1508 if (skb_shinfo(skb)->frag_list) {
1509 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1511 for (; list; list = list->next) {
1512 int end;
1514 BUG_TRAP(start <= offset + len);
1516 end = start + list->len;
1517 if ((copy = end - offset) > 0) {
1518 __wsum csum2;
1519 if (copy > len)
1520 copy = len;
1521 csum2 = skb_checksum(list, offset - start,
1522 copy, 0);
1523 csum = csum_block_add(csum, csum2, pos);
1524 if ((len -= copy) == 0)
1525 return csum;
1526 offset += copy;
1527 pos += copy;
1529 start = end;
1532 BUG_ON(len);
1534 return csum;
1537 /* Both of above in one bottle. */
1539 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1540 u8 *to, int len, __wsum csum)
1542 int start = skb_headlen(skb);
1543 int i, copy = start - offset;
1544 int pos = 0;
1546 /* Copy header. */
1547 if (copy > 0) {
1548 if (copy > len)
1549 copy = len;
1550 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1551 copy, csum);
1552 if ((len -= copy) == 0)
1553 return csum;
1554 offset += copy;
1555 to += copy;
1556 pos = copy;
1559 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1560 int end;
1562 BUG_TRAP(start <= offset + len);
1564 end = start + skb_shinfo(skb)->frags[i].size;
1565 if ((copy = end - offset) > 0) {
1566 __wsum csum2;
1567 u8 *vaddr;
1568 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1570 if (copy > len)
1571 copy = len;
1572 vaddr = kmap_skb_frag(frag);
1573 csum2 = csum_partial_copy_nocheck(vaddr +
1574 frag->page_offset +
1575 offset - start, to,
1576 copy, 0);
1577 kunmap_skb_frag(vaddr);
1578 csum = csum_block_add(csum, csum2, pos);
1579 if (!(len -= copy))
1580 return csum;
1581 offset += copy;
1582 to += copy;
1583 pos += copy;
1585 start = end;
1588 if (skb_shinfo(skb)->frag_list) {
1589 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1591 for (; list; list = list->next) {
1592 __wsum csum2;
1593 int end;
1595 BUG_TRAP(start <= offset + len);
1597 end = start + list->len;
1598 if ((copy = end - offset) > 0) {
1599 if (copy > len)
1600 copy = len;
1601 csum2 = skb_copy_and_csum_bits(list,
1602 offset - start,
1603 to, copy, 0);
1604 csum = csum_block_add(csum, csum2, pos);
1605 if ((len -= copy) == 0)
1606 return csum;
1607 offset += copy;
1608 to += copy;
1609 pos += copy;
1611 start = end;
1614 BUG_ON(len);
1615 return csum;
1618 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1620 __wsum csum;
1621 long csstart;
1623 if (skb->ip_summed == CHECKSUM_PARTIAL)
1624 csstart = skb->csum_start - skb_headroom(skb);
1625 else
1626 csstart = skb_headlen(skb);
1628 BUG_ON(csstart > skb_headlen(skb));
1630 skb_copy_from_linear_data(skb, to, csstart);
1632 csum = 0;
1633 if (csstart != skb->len)
1634 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1635 skb->len - csstart, 0);
1637 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1638 long csstuff = csstart + skb->csum_offset;
1640 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1645 * skb_dequeue - remove from the head of the queue
1646 * @list: list to dequeue from
1648 * Remove the head of the list. The list lock is taken so the function
1649 * may be used safely with other locking list functions. The head item is
1650 * returned or %NULL if the list is empty.
1653 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1655 unsigned long flags;
1656 struct sk_buff *result;
1658 spin_lock_irqsave(&list->lock, flags);
1659 result = __skb_dequeue(list);
1660 spin_unlock_irqrestore(&list->lock, flags);
1661 return result;
1665 * skb_dequeue_tail - remove from the tail of the queue
1666 * @list: list to dequeue from
1668 * Remove the tail of the list. The list lock is taken so the function
1669 * may be used safely with other locking list functions. The tail item is
1670 * returned or %NULL if the list is empty.
1672 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1674 unsigned long flags;
1675 struct sk_buff *result;
1677 spin_lock_irqsave(&list->lock, flags);
1678 result = __skb_dequeue_tail(list);
1679 spin_unlock_irqrestore(&list->lock, flags);
1680 return result;
1684 * skb_queue_purge - empty a list
1685 * @list: list to empty
1687 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1688 * the list and one reference dropped. This function takes the list
1689 * lock and is atomic with respect to other list locking functions.
1691 void skb_queue_purge(struct sk_buff_head *list)
1693 struct sk_buff *skb;
1694 while ((skb = skb_dequeue(list)) != NULL)
1695 kfree_skb(skb);
1699 * skb_queue_head - queue a buffer at the list head
1700 * @list: list to use
1701 * @newsk: buffer to queue
1703 * Queue a buffer at the start of the list. This function takes the
1704 * list lock and can be used safely with other locking &sk_buff functions
1705 * safely.
1707 * A buffer cannot be placed on two lists at the same time.
1709 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1711 unsigned long flags;
1713 spin_lock_irqsave(&list->lock, flags);
1714 __skb_queue_head(list, newsk);
1715 spin_unlock_irqrestore(&list->lock, flags);
1719 * skb_queue_tail - queue a buffer at the list tail
1720 * @list: list to use
1721 * @newsk: buffer to queue
1723 * Queue a buffer at the tail of the list. This function takes the
1724 * list lock and can be used safely with other locking &sk_buff functions
1725 * safely.
1727 * A buffer cannot be placed on two lists at the same time.
1729 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1731 unsigned long flags;
1733 spin_lock_irqsave(&list->lock, flags);
1734 __skb_queue_tail(list, newsk);
1735 spin_unlock_irqrestore(&list->lock, flags);
1739 * skb_unlink - remove a buffer from a list
1740 * @skb: buffer to remove
1741 * @list: list to use
1743 * Remove a packet from a list. The list locks are taken and this
1744 * function is atomic with respect to other list locked calls
1746 * You must know what list the SKB is on.
1748 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1750 unsigned long flags;
1752 spin_lock_irqsave(&list->lock, flags);
1753 __skb_unlink(skb, list);
1754 spin_unlock_irqrestore(&list->lock, flags);
1758 * skb_append - append a buffer
1759 * @old: buffer to insert after
1760 * @newsk: buffer to insert
1761 * @list: list to use
1763 * Place a packet after a given packet in a list. The list locks are taken
1764 * and this function is atomic with respect to other list locked calls.
1765 * A buffer cannot be placed on two lists at the same time.
1767 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1769 unsigned long flags;
1771 spin_lock_irqsave(&list->lock, flags);
1772 __skb_append(old, newsk, list);
1773 spin_unlock_irqrestore(&list->lock, flags);
1778 * skb_insert - insert a buffer
1779 * @old: buffer to insert before
1780 * @newsk: buffer to insert
1781 * @list: list to use
1783 * Place a packet before a given packet in a list. The list locks are
1784 * taken and this function is atomic with respect to other list locked
1785 * calls.
1787 * A buffer cannot be placed on two lists at the same time.
1789 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1791 unsigned long flags;
1793 spin_lock_irqsave(&list->lock, flags);
1794 __skb_insert(newsk, old->prev, old, list);
1795 spin_unlock_irqrestore(&list->lock, flags);
1798 static inline void skb_split_inside_header(struct sk_buff *skb,
1799 struct sk_buff* skb1,
1800 const u32 len, const int pos)
1802 int i;
1804 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1805 pos - len);
1806 /* And move data appendix as is. */
1807 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1808 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1810 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1811 skb_shinfo(skb)->nr_frags = 0;
1812 skb1->data_len = skb->data_len;
1813 skb1->len += skb1->data_len;
1814 skb->data_len = 0;
1815 skb->len = len;
1816 skb_set_tail_pointer(skb, len);
1819 static inline void skb_split_no_header(struct sk_buff *skb,
1820 struct sk_buff* skb1,
1821 const u32 len, int pos)
1823 int i, k = 0;
1824 const int nfrags = skb_shinfo(skb)->nr_frags;
1826 skb_shinfo(skb)->nr_frags = 0;
1827 skb1->len = skb1->data_len = skb->len - len;
1828 skb->len = len;
1829 skb->data_len = len - pos;
1831 for (i = 0; i < nfrags; i++) {
1832 int size = skb_shinfo(skb)->frags[i].size;
1834 if (pos + size > len) {
1835 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1837 if (pos < len) {
1838 /* Split frag.
1839 * We have two variants in this case:
1840 * 1. Move all the frag to the second
1841 * part, if it is possible. F.e.
1842 * this approach is mandatory for TUX,
1843 * where splitting is expensive.
1844 * 2. Split is accurately. We make this.
1846 get_page(skb_shinfo(skb)->frags[i].page);
1847 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1848 skb_shinfo(skb1)->frags[0].size -= len - pos;
1849 skb_shinfo(skb)->frags[i].size = len - pos;
1850 skb_shinfo(skb)->nr_frags++;
1852 k++;
1853 } else
1854 skb_shinfo(skb)->nr_frags++;
1855 pos += size;
1857 skb_shinfo(skb1)->nr_frags = k;
1861 * skb_split - Split fragmented skb to two parts at length len.
1862 * @skb: the buffer to split
1863 * @skb1: the buffer to receive the second part
1864 * @len: new length for skb
1866 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1868 int pos = skb_headlen(skb);
1870 if (len < pos) /* Split line is inside header. */
1871 skb_split_inside_header(skb, skb1, len, pos);
1872 else /* Second chunk has no header, nothing to copy. */
1873 skb_split_no_header(skb, skb1, len, pos);
1877 * skb_prepare_seq_read - Prepare a sequential read of skb data
1878 * @skb: the buffer to read
1879 * @from: lower offset of data to be read
1880 * @to: upper offset of data to be read
1881 * @st: state variable
1883 * Initializes the specified state variable. Must be called before
1884 * invoking skb_seq_read() for the first time.
1886 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1887 unsigned int to, struct skb_seq_state *st)
1889 st->lower_offset = from;
1890 st->upper_offset = to;
1891 st->root_skb = st->cur_skb = skb;
1892 st->frag_idx = st->stepped_offset = 0;
1893 st->frag_data = NULL;
1897 * skb_seq_read - Sequentially read skb data
1898 * @consumed: number of bytes consumed by the caller so far
1899 * @data: destination pointer for data to be returned
1900 * @st: state variable
1902 * Reads a block of skb data at &consumed relative to the
1903 * lower offset specified to skb_prepare_seq_read(). Assigns
1904 * the head of the data block to &data and returns the length
1905 * of the block or 0 if the end of the skb data or the upper
1906 * offset has been reached.
1908 * The caller is not required to consume all of the data
1909 * returned, i.e. &consumed is typically set to the number
1910 * of bytes already consumed and the next call to
1911 * skb_seq_read() will return the remaining part of the block.
1913 * Note: The size of each block of data returned can be arbitary,
1914 * this limitation is the cost for zerocopy seqeuental
1915 * reads of potentially non linear data.
1917 * Note: Fragment lists within fragments are not implemented
1918 * at the moment, state->root_skb could be replaced with
1919 * a stack for this purpose.
1921 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1922 struct skb_seq_state *st)
1924 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1925 skb_frag_t *frag;
1927 if (unlikely(abs_offset >= st->upper_offset))
1928 return 0;
1930 next_skb:
1931 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
1933 if (abs_offset < block_limit && !st->frag_data) {
1934 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
1935 return block_limit - abs_offset;
1938 if (st->frag_idx == 0 && !st->frag_data)
1939 st->stepped_offset += skb_headlen(st->cur_skb);
1941 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1942 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1943 block_limit = frag->size + st->stepped_offset;
1945 if (abs_offset < block_limit) {
1946 if (!st->frag_data)
1947 st->frag_data = kmap_skb_frag(frag);
1949 *data = (u8 *) st->frag_data + frag->page_offset +
1950 (abs_offset - st->stepped_offset);
1952 return block_limit - abs_offset;
1955 if (st->frag_data) {
1956 kunmap_skb_frag(st->frag_data);
1957 st->frag_data = NULL;
1960 st->frag_idx++;
1961 st->stepped_offset += frag->size;
1964 if (st->frag_data) {
1965 kunmap_skb_frag(st->frag_data);
1966 st->frag_data = NULL;
1969 if (st->root_skb == st->cur_skb &&
1970 skb_shinfo(st->root_skb)->frag_list) {
1971 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1972 st->frag_idx = 0;
1973 goto next_skb;
1974 } else if (st->cur_skb->next) {
1975 st->cur_skb = st->cur_skb->next;
1976 st->frag_idx = 0;
1977 goto next_skb;
1980 return 0;
1984 * skb_abort_seq_read - Abort a sequential read of skb data
1985 * @st: state variable
1987 * Must be called if skb_seq_read() was not called until it
1988 * returned 0.
1990 void skb_abort_seq_read(struct skb_seq_state *st)
1992 if (st->frag_data)
1993 kunmap_skb_frag(st->frag_data);
1996 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1998 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1999 struct ts_config *conf,
2000 struct ts_state *state)
2002 return skb_seq_read(offset, text, TS_SKB_CB(state));
2005 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2007 skb_abort_seq_read(TS_SKB_CB(state));
2011 * skb_find_text - Find a text pattern in skb data
2012 * @skb: the buffer to look in
2013 * @from: search offset
2014 * @to: search limit
2015 * @config: textsearch configuration
2016 * @state: uninitialized textsearch state variable
2018 * Finds a pattern in the skb data according to the specified
2019 * textsearch configuration. Use textsearch_next() to retrieve
2020 * subsequent occurrences of the pattern. Returns the offset
2021 * to the first occurrence or UINT_MAX if no match was found.
2023 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2024 unsigned int to, struct ts_config *config,
2025 struct ts_state *state)
2027 unsigned int ret;
2029 config->get_next_block = skb_ts_get_next_block;
2030 config->finish = skb_ts_finish;
2032 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2034 ret = textsearch_find(config, state);
2035 return (ret <= to - from ? ret : UINT_MAX);
2039 * skb_append_datato_frags: - append the user data to a skb
2040 * @sk: sock structure
2041 * @skb: skb structure to be appened with user data.
2042 * @getfrag: call back function to be used for getting the user data
2043 * @from: pointer to user message iov
2044 * @length: length of the iov message
2046 * Description: This procedure append the user data in the fragment part
2047 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2049 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2050 int (*getfrag)(void *from, char *to, int offset,
2051 int len, int odd, struct sk_buff *skb),
2052 void *from, int length)
2054 int frg_cnt = 0;
2055 skb_frag_t *frag = NULL;
2056 struct page *page = NULL;
2057 int copy, left;
2058 int offset = 0;
2059 int ret;
2061 do {
2062 /* Return error if we don't have space for new frag */
2063 frg_cnt = skb_shinfo(skb)->nr_frags;
2064 if (frg_cnt >= MAX_SKB_FRAGS)
2065 return -EFAULT;
2067 /* allocate a new page for next frag */
2068 page = alloc_pages(sk->sk_allocation, 0);
2070 /* If alloc_page fails just return failure and caller will
2071 * free previous allocated pages by doing kfree_skb()
2073 if (page == NULL)
2074 return -ENOMEM;
2076 /* initialize the next frag */
2077 sk->sk_sndmsg_page = page;
2078 sk->sk_sndmsg_off = 0;
2079 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2080 skb->truesize += PAGE_SIZE;
2081 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2083 /* get the new initialized frag */
2084 frg_cnt = skb_shinfo(skb)->nr_frags;
2085 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2087 /* copy the user data to page */
2088 left = PAGE_SIZE - frag->page_offset;
2089 copy = (length > left)? left : length;
2091 ret = getfrag(from, (page_address(frag->page) +
2092 frag->page_offset + frag->size),
2093 offset, copy, 0, skb);
2094 if (ret < 0)
2095 return -EFAULT;
2097 /* copy was successful so update the size parameters */
2098 sk->sk_sndmsg_off += copy;
2099 frag->size += copy;
2100 skb->len += copy;
2101 skb->data_len += copy;
2102 offset += copy;
2103 length -= copy;
2105 } while (length > 0);
2107 return 0;
2111 * skb_pull_rcsum - pull skb and update receive checksum
2112 * @skb: buffer to update
2113 * @start: start of data before pull
2114 * @len: length of data pulled
2116 * This function performs an skb_pull on the packet and updates
2117 * update the CHECKSUM_COMPLETE checksum. It should be used on
2118 * receive path processing instead of skb_pull unless you know
2119 * that the checksum difference is zero (e.g., a valid IP header)
2120 * or you are setting ip_summed to CHECKSUM_NONE.
2122 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2124 BUG_ON(len > skb->len);
2125 skb->len -= len;
2126 BUG_ON(skb->len < skb->data_len);
2127 skb_postpull_rcsum(skb, skb->data, len);
2128 return skb->data += len;
2131 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2134 * skb_segment - Perform protocol segmentation on skb.
2135 * @skb: buffer to segment
2136 * @features: features for the output path (see dev->features)
2138 * This function performs segmentation on the given skb. It returns
2139 * the segment at the given position. It returns NULL if there are
2140 * no more segments to generate, or when an error is encountered.
2142 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2144 struct sk_buff *segs = NULL;
2145 struct sk_buff *tail = NULL;
2146 unsigned int mss = skb_shinfo(skb)->gso_size;
2147 unsigned int doffset = skb->data - skb_mac_header(skb);
2148 unsigned int offset = doffset;
2149 unsigned int headroom;
2150 unsigned int len;
2151 int sg = features & NETIF_F_SG;
2152 int nfrags = skb_shinfo(skb)->nr_frags;
2153 int err = -ENOMEM;
2154 int i = 0;
2155 int pos;
2157 __skb_push(skb, doffset);
2158 headroom = skb_headroom(skb);
2159 pos = skb_headlen(skb);
2161 do {
2162 struct sk_buff *nskb;
2163 skb_frag_t *frag;
2164 int hsize;
2165 int k;
2166 int size;
2168 len = skb->len - offset;
2169 if (len > mss)
2170 len = mss;
2172 hsize = skb_headlen(skb) - offset;
2173 if (hsize < 0)
2174 hsize = 0;
2175 if (hsize > len || !sg)
2176 hsize = len;
2178 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
2179 if (unlikely(!nskb))
2180 goto err;
2182 if (segs)
2183 tail->next = nskb;
2184 else
2185 segs = nskb;
2186 tail = nskb;
2188 __copy_skb_header(nskb, skb);
2189 nskb->mac_len = skb->mac_len;
2191 skb_reserve(nskb, headroom);
2192 skb_reset_mac_header(nskb);
2193 skb_set_network_header(nskb, skb->mac_len);
2194 nskb->transport_header = (nskb->network_header +
2195 skb_network_header_len(skb));
2196 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2197 doffset);
2198 if (!sg) {
2199 nskb->ip_summed = CHECKSUM_NONE;
2200 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2201 skb_put(nskb, len),
2202 len, 0);
2203 continue;
2206 frag = skb_shinfo(nskb)->frags;
2207 k = 0;
2209 skb_copy_from_linear_data_offset(skb, offset,
2210 skb_put(nskb, hsize), hsize);
2212 while (pos < offset + len) {
2213 BUG_ON(i >= nfrags);
2215 *frag = skb_shinfo(skb)->frags[i];
2216 get_page(frag->page);
2217 size = frag->size;
2219 if (pos < offset) {
2220 frag->page_offset += offset - pos;
2221 frag->size -= offset - pos;
2224 k++;
2226 if (pos + size <= offset + len) {
2227 i++;
2228 pos += size;
2229 } else {
2230 frag->size -= pos + size - (offset + len);
2231 break;
2234 frag++;
2237 skb_shinfo(nskb)->nr_frags = k;
2238 nskb->data_len = len - hsize;
2239 nskb->len += nskb->data_len;
2240 nskb->truesize += nskb->data_len;
2241 } while ((offset += len) < skb->len);
2243 return segs;
2245 err:
2246 while ((skb = segs)) {
2247 segs = skb->next;
2248 kfree_skb(skb);
2250 return ERR_PTR(err);
2253 EXPORT_SYMBOL_GPL(skb_segment);
2255 void __init skb_init(void)
2257 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2258 sizeof(struct sk_buff),
2260 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2261 NULL, NULL);
2262 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2263 (2*sizeof(struct sk_buff)) +
2264 sizeof(atomic_t),
2266 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2267 NULL, NULL);
2271 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2272 * @skb: Socket buffer containing the buffers to be mapped
2273 * @sg: The scatter-gather list to map into
2274 * @offset: The offset into the buffer's contents to start mapping
2275 * @len: Length of buffer space to be mapped
2277 * Fill the specified scatter-gather list with mappings/pointers into a
2278 * region of the buffer space attached to a socket buffer.
2281 skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2283 int start = skb_headlen(skb);
2284 int i, copy = start - offset;
2285 int elt = 0;
2287 if (copy > 0) {
2288 if (copy > len)
2289 copy = len;
2290 sg[elt].page = virt_to_page(skb->data + offset);
2291 sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
2292 sg[elt].length = copy;
2293 elt++;
2294 if ((len -= copy) == 0)
2295 return elt;
2296 offset += copy;
2299 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2300 int end;
2302 BUG_TRAP(start <= offset + len);
2304 end = start + skb_shinfo(skb)->frags[i].size;
2305 if ((copy = end - offset) > 0) {
2306 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2308 if (copy > len)
2309 copy = len;
2310 sg[elt].page = frag->page;
2311 sg[elt].offset = frag->page_offset+offset-start;
2312 sg[elt].length = copy;
2313 elt++;
2314 if (!(len -= copy))
2315 return elt;
2316 offset += copy;
2318 start = end;
2321 if (skb_shinfo(skb)->frag_list) {
2322 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2324 for (; list; list = list->next) {
2325 int end;
2327 BUG_TRAP(start <= offset + len);
2329 end = start + list->len;
2330 if ((copy = end - offset) > 0) {
2331 if (copy > len)
2332 copy = len;
2333 elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
2334 if ((len -= copy) == 0)
2335 return elt;
2336 offset += copy;
2338 start = end;
2341 BUG_ON(len);
2342 return elt;
2346 * skb_cow_data - Check that a socket buffer's data buffers are writable
2347 * @skb: The socket buffer to check.
2348 * @tailbits: Amount of trailing space to be added
2349 * @trailer: Returned pointer to the skb where the @tailbits space begins
2351 * Make sure that the data buffers attached to a socket buffer are
2352 * writable. If they are not, private copies are made of the data buffers
2353 * and the socket buffer is set to use these instead.
2355 * If @tailbits is given, make sure that there is space to write @tailbits
2356 * bytes of data beyond current end of socket buffer. @trailer will be
2357 * set to point to the skb in which this space begins.
2359 * The number of scatterlist elements required to completely map the
2360 * COW'd and extended socket buffer will be returned.
2362 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2364 int copyflag;
2365 int elt;
2366 struct sk_buff *skb1, **skb_p;
2368 /* If skb is cloned or its head is paged, reallocate
2369 * head pulling out all the pages (pages are considered not writable
2370 * at the moment even if they are anonymous).
2372 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2373 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2374 return -ENOMEM;
2376 /* Easy case. Most of packets will go this way. */
2377 if (!skb_shinfo(skb)->frag_list) {
2378 /* A little of trouble, not enough of space for trailer.
2379 * This should not happen, when stack is tuned to generate
2380 * good frames. OK, on miss we reallocate and reserve even more
2381 * space, 128 bytes is fair. */
2383 if (skb_tailroom(skb) < tailbits &&
2384 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2385 return -ENOMEM;
2387 /* Voila! */
2388 *trailer = skb;
2389 return 1;
2392 /* Misery. We are in troubles, going to mincer fragments... */
2394 elt = 1;
2395 skb_p = &skb_shinfo(skb)->frag_list;
2396 copyflag = 0;
2398 while ((skb1 = *skb_p) != NULL) {
2399 int ntail = 0;
2401 /* The fragment is partially pulled by someone,
2402 * this can happen on input. Copy it and everything
2403 * after it. */
2405 if (skb_shared(skb1))
2406 copyflag = 1;
2408 /* If the skb is the last, worry about trailer. */
2410 if (skb1->next == NULL && tailbits) {
2411 if (skb_shinfo(skb1)->nr_frags ||
2412 skb_shinfo(skb1)->frag_list ||
2413 skb_tailroom(skb1) < tailbits)
2414 ntail = tailbits + 128;
2417 if (copyflag ||
2418 skb_cloned(skb1) ||
2419 ntail ||
2420 skb_shinfo(skb1)->nr_frags ||
2421 skb_shinfo(skb1)->frag_list) {
2422 struct sk_buff *skb2;
2424 /* Fuck, we are miserable poor guys... */
2425 if (ntail == 0)
2426 skb2 = skb_copy(skb1, GFP_ATOMIC);
2427 else
2428 skb2 = skb_copy_expand(skb1,
2429 skb_headroom(skb1),
2430 ntail,
2431 GFP_ATOMIC);
2432 if (unlikely(skb2 == NULL))
2433 return -ENOMEM;
2435 if (skb1->sk)
2436 skb_set_owner_w(skb2, skb1->sk);
2438 /* Looking around. Are we still alive?
2439 * OK, link new skb, drop old one */
2441 skb2->next = skb1->next;
2442 *skb_p = skb2;
2443 kfree_skb(skb1);
2444 skb1 = skb2;
2446 elt++;
2447 *trailer = skb1;
2448 skb_p = &skb1->next;
2451 return elt;
2454 EXPORT_SYMBOL(___pskb_trim);
2455 EXPORT_SYMBOL(__kfree_skb);
2456 EXPORT_SYMBOL(kfree_skb);
2457 EXPORT_SYMBOL(__pskb_pull_tail);
2458 EXPORT_SYMBOL(__alloc_skb);
2459 EXPORT_SYMBOL(__netdev_alloc_skb);
2460 EXPORT_SYMBOL(pskb_copy);
2461 EXPORT_SYMBOL(pskb_expand_head);
2462 EXPORT_SYMBOL(skb_checksum);
2463 EXPORT_SYMBOL(skb_clone);
2464 EXPORT_SYMBOL(skb_copy);
2465 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2466 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2467 EXPORT_SYMBOL(skb_copy_bits);
2468 EXPORT_SYMBOL(skb_copy_expand);
2469 EXPORT_SYMBOL(skb_over_panic);
2470 EXPORT_SYMBOL(skb_pad);
2471 EXPORT_SYMBOL(skb_realloc_headroom);
2472 EXPORT_SYMBOL(skb_under_panic);
2473 EXPORT_SYMBOL(skb_dequeue);
2474 EXPORT_SYMBOL(skb_dequeue_tail);
2475 EXPORT_SYMBOL(skb_insert);
2476 EXPORT_SYMBOL(skb_queue_purge);
2477 EXPORT_SYMBOL(skb_queue_head);
2478 EXPORT_SYMBOL(skb_queue_tail);
2479 EXPORT_SYMBOL(skb_unlink);
2480 EXPORT_SYMBOL(skb_append);
2481 EXPORT_SYMBOL(skb_split);
2482 EXPORT_SYMBOL(skb_prepare_seq_read);
2483 EXPORT_SYMBOL(skb_seq_read);
2484 EXPORT_SYMBOL(skb_abort_seq_read);
2485 EXPORT_SYMBOL(skb_find_text);
2486 EXPORT_SYMBOL(skb_append_datato_frags);
2488 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2489 EXPORT_SYMBOL_GPL(skb_cow_data);