allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / core / skbuff.c
blob1f3aade3d3eb031532dbf235730f15591221c1cf
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 skb->vlan_tci = 0;
210 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
211 skb->nfct_reasm = NULL;
212 skb->nfct = NULL;
213 skb->nfcache = 0;
214 #endif
215 #ifdef HNDCTF
216 skb->mac_len = 0;
217 skb->hdr_len = 0;
218 #endif
219 #ifdef CONFIG_BRIDGE_NETFILTER
220 skb->nf_bridge = NULL;
221 #endif
222 #ifdef CONFIG_NET_SCHED
223 skb->tc_index = 0;
224 #ifdef CONFIG_NET_CLS_ACT
225 skb->tc_verd = 0;
226 #endif
227 #endif
228 #ifdef CONFIG_NET_DMA
229 memset(&skb->dma_cookie, 0, sizeof(dma_cookie_t));
230 #endif
231 #ifdef CONFIG_NETWORK_SECMARK
232 skb->secmark =0;
233 #endif
235 /* make sure we initialize shinfo sequentially */
236 shinfo = skb_shinfo(skb);
237 memset(shinfo, 0, offsetof(struct skb_shared_info, frags));
238 atomic_set(&shinfo->dataref, 1);
240 #if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
241 skb->imq_flags = 0;
242 skb->nf_info = NULL;
243 #endif
245 if (fclone) {
246 struct sk_buff *child = skb + 1;
247 atomic_t *fclone_ref = (atomic_t *) (child + 1);
249 skb->fclone = SKB_FCLONE_ORIG;
250 atomic_set(fclone_ref, 1);
252 child->fclone = SKB_FCLONE_UNAVAILABLE;
254 out:
255 return skb;
256 nodata:
257 kmem_cache_free(cache, skb);
258 skb = NULL;
259 goto out;
263 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
264 * @dev: network device to receive on
265 * @length: length to allocate
266 * @gfp_mask: get_free_pages mask, passed to alloc_skb
268 * Allocate a new &sk_buff and assign it a usage count of one. The
269 * buffer has unspecified headroom built in. Users should allocate
270 * the headroom they think they need without accounting for the
271 * built in space. The built in space is used for optimisations.
273 * %NULL is returned if there is no free memory.
275 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
276 unsigned int length, gfp_t gfp_mask)
278 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
279 struct sk_buff *skb;
281 skb = __alloc_skb(length + NET_SKB_PAD_ALLOC, gfp_mask, 0, node);
282 if (likely(skb)) {
283 skb_reserve(skb, NET_SKB_PAD_ALLOC);
284 skb->dev = dev;
286 return skb;
289 static void skb_drop_list(struct sk_buff **listp)
291 struct sk_buff *list = *listp;
293 *listp = NULL;
295 do {
296 struct sk_buff *this = list;
297 list = list->next;
298 kfree_skb(this);
299 } while (list);
302 static inline void skb_drop_fraglist(struct sk_buff *skb)
304 skb_drop_list(&skb_shinfo(skb)->frag_list);
307 static void skb_clone_fraglist(struct sk_buff *skb)
309 struct sk_buff *list;
311 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
312 skb_get(list);
315 static void skb_release_data(struct sk_buff *skb)
317 if (!skb->cloned ||
318 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
319 &skb_shinfo(skb)->dataref)) {
320 if (skb_shinfo(skb)->nr_frags) {
321 int i;
322 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
323 put_page(skb_shinfo(skb)->frags[i].page);
326 if (skb_shinfo(skb)->frag_list)
327 skb_drop_fraglist(skb);
329 kfree(skb->head);
334 * Free an skbuff by memory without cleaning the state.
336 static void kfree_skbmem(struct sk_buff *skb)
338 struct sk_buff *other;
339 atomic_t *fclone_ref;
341 switch (skb->fclone) {
342 case SKB_FCLONE_UNAVAILABLE:
343 kmem_cache_free(skbuff_head_cache, skb);
344 break;
346 case SKB_FCLONE_ORIG:
347 fclone_ref = (atomic_t *) (skb + 2);
348 if (atomic_dec_and_test(fclone_ref))
349 kmem_cache_free(skbuff_fclone_cache, skb);
350 break;
352 case SKB_FCLONE_CLONE:
353 fclone_ref = (atomic_t *) (skb + 1);
354 other = skb - 1;
356 /* The clone portion is available for
357 * fast-cloning again.
359 skb->fclone = SKB_FCLONE_UNAVAILABLE;
361 if (atomic_dec_and_test(fclone_ref))
362 kmem_cache_free(skbuff_fclone_cache, other);
363 break;
367 /* Free everything but the sk_buff shell. */
368 static void skb_release_all(struct sk_buff *skb)
370 dst_release(skb->dst);
371 #ifdef CONFIG_XFRM
372 secpath_put(skb->sp);
373 #endif
374 if (skb->destructor) {
375 #ifdef CONFIG_NETCORE_DEBUG
376 WARN_ON(in_irq());
377 #endif
378 skb->destructor(skb);
380 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
381 nf_conntrack_put(skb->nfct);
382 nf_conntrack_put_reasm(skb->nfct_reasm);
383 #endif
384 #ifdef CONFIG_BRIDGE_NETFILTER
385 nf_bridge_put(skb->nf_bridge);
386 #endif
387 /* XXX: IS this still necessary? - JHS */
388 #ifdef CONFIG_NET_SCHED
389 skb->tc_index = 0;
390 #ifdef CONFIG_NET_CLS_ACT
391 skb->tc_verd = 0;
392 #endif
393 #endif
394 skb_release_data(skb);
398 * __kfree_skb - private function
399 * @skb: buffer
401 * Free an sk_buff. Release anything attached to the buffer.
402 * Clean the state. This is an internal helper function. Users should
403 * always call kfree_skb
406 void __kfree_skb(struct sk_buff *skb)
408 skb_release_all(skb);
409 kfree_skbmem(skb);
413 * kfree_skb - free an sk_buff
414 * @skb: buffer to free
416 * Drop a reference to the buffer and free it if the usage count has
417 * hit zero.
419 void kfree_skb(struct sk_buff *skb)
421 if (unlikely(!skb))
422 return;
423 if (likely(atomic_read(&skb->users) == 1))
424 smp_rmb();
425 else if (likely(!atomic_dec_and_test(&skb->users)))
426 return;
427 __kfree_skb(skb);
430 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
432 new->tstamp = old->tstamp;
433 new->dev = old->dev;
434 new->transport_header = old->transport_header;
435 new->network_header = old->network_header;
436 new->mac_header = old->mac_header;
437 new->dst = dst_clone(old->dst);
438 #ifdef CONFIG_INET
439 new->sp = secpath_get(old->sp);
440 #endif
441 memcpy(new->cb, old->cb, sizeof(old->cb));
442 new->csum = old->csum;
443 new->local_df = old->local_df;
444 new->pkt_type = old->pkt_type;
445 new->ip_summed = old->ip_summed;
446 new->priority = old->priority;
447 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
448 new->ipvs_property = old->ipvs_property;
449 #endif
450 new->protocol = old->protocol;
451 new->mark = old->mark;
452 new->iif = old->iif;
453 __nf_copy(new, old);
454 #ifdef CONFIG_NET_SCHED
455 new->tc_index = old->tc_index;
456 #ifdef CONFIG_NET_CLS_ACT
457 new->tc_verd = old->tc_verd;
458 #endif
459 #endif
460 new->vlan_tci = old->vlan_tci;
462 skb_copy_secmark(new, old);
463 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
464 new->nfcache = old->nfcache;
465 #endif
469 * You should not add any new code to this function. Add it to
470 * __copy_skb_header above instead.
472 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
474 #define C(x) n->x = skb->x
476 n->next = n->prev = NULL;
477 n->sk = NULL;
478 __copy_skb_header(n, skb);
480 C(len);
481 C(data_len);
482 C(mac_len);
483 #ifdef HNDCTF
484 C(ctf_mac_len); /* used by Broadcom CTF driver! */
485 #endif
486 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
487 n->cloned = 1;
488 n->nohdr = 0;
489 n->destructor = NULL;
490 C(tail);
491 C(end);
492 C(head);
493 C(data);
494 C(truesize);
495 atomic_set(&n->users, 1);
497 atomic_inc(&(skb_shinfo(skb)->dataref));
498 skb->cloned = 1;
500 return n;
501 #undef C
505 * skb_morph - morph one skb into another
506 * @dst: the skb to receive the contents
507 * @src: the skb to supply the contents
509 * This is identical to skb_clone except that the target skb is
510 * supplied by the user.
512 * The target skb is returned upon exit.
514 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
516 skb_release_all(dst);
517 return __skb_clone(dst, src);
519 EXPORT_SYMBOL_GPL(skb_morph);
522 * skb_clone - duplicate an sk_buff
523 * @skb: buffer to clone
524 * @gfp_mask: allocation priority
526 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
527 * copies share the same packet data but not structure. The new
528 * buffer has a reference count of 1. If the allocation fails the
529 * function returns %NULL otherwise the new buffer is returned.
531 * If this function is called from an interrupt gfp_mask() must be
532 * %GFP_ATOMIC.
535 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
537 struct sk_buff *n;
539 n = skb + 1;
540 if (skb->fclone == SKB_FCLONE_ORIG &&
541 n->fclone == SKB_FCLONE_UNAVAILABLE) {
542 atomic_t *fclone_ref = (atomic_t *) (n + 1);
543 n->fclone = SKB_FCLONE_CLONE;
544 atomic_inc(fclone_ref);
545 } else {
546 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
547 if (!n)
548 return NULL;
549 n->fclone = SKB_FCLONE_UNAVAILABLE;
552 return __skb_clone(n, skb);
555 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
557 #ifndef NET_SKBUFF_DATA_USES_OFFSET
559 * Shift between the two data areas in bytes
561 unsigned long offset = new->data - old->data;
562 #endif
564 __copy_skb_header(new, old);
566 #ifndef NET_SKBUFF_DATA_USES_OFFSET
567 /* {transport,network,mac}_header are relative to skb->head */
568 new->transport_header += offset;
569 new->network_header += offset;
570 if (skb_mac_header_was_set(new))
571 new->mac_header += offset;
572 #endif
573 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
574 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
575 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
579 * skb_copy - create private copy of an sk_buff
580 * @skb: buffer to copy
581 * @gfp_mask: allocation priority
583 * Make a copy of both an &sk_buff and its data. This is used when the
584 * caller wishes to modify the data and needs a private copy of the
585 * data to alter. Returns %NULL on failure or the pointer to the buffer
586 * on success. The returned buffer has a reference count of 1.
588 * As by-product this function converts non-linear &sk_buff to linear
589 * one, so that &sk_buff becomes completely private and caller is allowed
590 * to modify all the data of returned buffer. This means that this
591 * function is not recommended for use in circumstances when only
592 * header is going to be modified. Use pskb_copy() instead.
595 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
597 int headerlen = skb->data - skb->head;
599 * Allocate the copy buffer
601 struct sk_buff *n;
602 #ifdef NET_SKBUFF_DATA_USES_OFFSET
603 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
604 #else
605 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
606 #endif
607 if (!n)
608 return NULL;
610 /* Set the data pointer */
611 skb_reserve(n, headerlen);
612 /* Set the tail pointer and length */
613 skb_put(n, skb->len);
615 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
616 BUG();
618 copy_skb_header(n, skb);
619 return n;
624 * pskb_copy - create copy of an sk_buff with private head.
625 * @skb: buffer to copy
626 * @gfp_mask: allocation priority
628 * Make a copy of both an &sk_buff and part of its data, located
629 * in header. Fragmented data remain shared. This is used when
630 * the caller wishes to modify only header of &sk_buff and needs
631 * private copy of the header to alter. Returns %NULL on failure
632 * or the pointer to the buffer on success.
633 * The returned buffer has a reference count of 1.
636 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
639 * Allocate the copy buffer
641 struct sk_buff *n;
642 #ifdef NET_SKBUFF_DATA_USES_OFFSET
643 n = alloc_skb(skb->end, gfp_mask);
644 #else
645 n = alloc_skb(skb->end - skb->head, gfp_mask);
646 #endif
647 if (!n)
648 goto out;
650 /* Set the data pointer */
651 skb_reserve(n, skb->data - skb->head);
652 /* Set the tail pointer and length */
653 skb_put(n, skb_headlen(skb));
654 /* Copy the bytes */
655 skb_copy_from_linear_data(skb, n->data, n->len);
657 n->truesize += skb->data_len;
658 n->data_len = skb->data_len;
659 n->len = skb->len;
661 if (skb_shinfo(skb)->nr_frags) {
662 int i;
664 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
665 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
666 get_page(skb_shinfo(n)->frags[i].page);
668 skb_shinfo(n)->nr_frags = i;
671 if (skb_shinfo(skb)->frag_list) {
672 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
673 skb_clone_fraglist(n);
676 copy_skb_header(n, skb);
677 out:
678 return n;
682 * pskb_expand_head - reallocate header of &sk_buff
683 * @skb: buffer to reallocate
684 * @nhead: room to add at head
685 * @ntail: room to add at tail
686 * @gfp_mask: allocation priority
688 * Expands (or creates identical copy, if &nhead and &ntail are zero)
689 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
690 * reference count of 1. Returns zero in the case of success or error,
691 * if expansion failed. In the last case, &sk_buff is not changed.
693 * All the pointers pointing into skb header may change and must be
694 * reloaded after call to this function.
697 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
698 gfp_t gfp_mask)
700 int i;
701 u8 *data;
702 #ifdef NET_SKBUFF_DATA_USES_OFFSET
703 int size = nhead + skb->end + ntail;
704 #else
705 int size = nhead + (skb->end - skb->head) + ntail;
706 #endif
707 long off;
709 BUG_ON(nhead < 0);
711 if (skb_shared(skb))
712 BUG();
714 size = SKB_DATA_ALIGN(size);
716 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
717 if (!data)
718 goto nodata;
720 /* Copy only real data... and, alas, header. This should be
721 * optimized for the cases when header is void. */
722 #ifdef NET_SKBUFF_DATA_USES_OFFSET
723 memcpy(data + nhead, skb->head, skb->tail);
724 #else
725 memcpy(data + nhead, skb->head, skb->tail - skb->head);
726 #endif
727 memcpy(data + size, skb_end_pointer(skb),
728 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
730 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
731 get_page(skb_shinfo(skb)->frags[i].page);
733 if (skb_shinfo(skb)->frag_list)
734 skb_clone_fraglist(skb);
736 skb_release_data(skb);
738 off = (data + nhead) - skb->head;
740 skb->head = data;
741 skb->data += off;
742 #ifdef NET_SKBUFF_DATA_USES_OFFSET
743 skb->end = size;
744 off = nhead;
745 #else
746 skb->end = skb->head + size;
747 #endif
748 /* {transport,network,mac}_header and tail are relative to skb->head */
749 skb->tail += off;
750 skb->transport_header += off;
751 skb->network_header += off;
752 if (skb_mac_header_was_set(skb))
753 skb->mac_header += off;
754 /* Only adjust this if it actually is csum_start rather than csum */
755 if (skb->ip_summed == CHECKSUM_PARTIAL)
756 skb->csum_start += nhead;
757 skb->cloned = 0;
758 skb->hdr_len = 0;
759 skb->nohdr = 0;
760 atomic_set(&skb_shinfo(skb)->dataref, 1);
761 return 0;
763 nodata:
764 return -ENOMEM;
767 /* Make private copy of skb with writable head and some headroom */
769 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
771 struct sk_buff *skb2;
772 int delta = headroom - skb_headroom(skb);
774 if (delta <= 0)
775 skb2 = pskb_copy(skb, GFP_ATOMIC);
776 else {
777 skb2 = skb_clone(skb, GFP_ATOMIC);
778 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
779 GFP_ATOMIC)) {
780 kfree_skb(skb2);
781 skb2 = NULL;
784 return skb2;
789 * skb_copy_expand - copy and expand sk_buff
790 * @skb: buffer to copy
791 * @newheadroom: new free bytes at head
792 * @newtailroom: new free bytes at tail
793 * @gfp_mask: allocation priority
795 * Make a copy of both an &sk_buff and its data and while doing so
796 * allocate additional space.
798 * This is used when the caller wishes to modify the data and needs a
799 * private copy of the data to alter as well as more space for new fields.
800 * Returns %NULL on failure or the pointer to the buffer
801 * on success. The returned buffer has a reference count of 1.
803 * You must pass %GFP_ATOMIC as the allocation priority if this function
804 * is called from an interrupt.
806 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
807 int newheadroom, int newtailroom,
808 gfp_t gfp_mask)
811 * Allocate the copy buffer
813 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
814 gfp_mask);
815 int oldheadroom = skb_headroom(skb);
816 int head_copy_len, head_copy_off;
817 int off;
819 if (!n)
820 return NULL;
822 skb_reserve(n, newheadroom);
824 /* Set the tail pointer and length */
825 skb_put(n, skb->len);
827 head_copy_len = oldheadroom;
828 head_copy_off = 0;
829 if (newheadroom <= head_copy_len)
830 head_copy_len = newheadroom;
831 else
832 head_copy_off = newheadroom - head_copy_len;
834 /* Copy the linear header and data. */
835 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
836 skb->len + head_copy_len))
837 BUG();
839 copy_skb_header(n, skb);
841 off = newheadroom - oldheadroom;
842 if (n->ip_summed == CHECKSUM_PARTIAL)
843 n->csum_start += off;
844 #ifdef NET_SKBUFF_DATA_USES_OFFSET
845 n->transport_header += off;
846 n->network_header += off;
847 if (skb_mac_header_was_set(skb))
848 n->mac_header += off;
849 #endif
851 return n;
855 * skb_pad - zero pad the tail of an skb
856 * @skb: buffer to pad
857 * @pad: space to pad
859 * Ensure that a buffer is followed by a padding area that is zero
860 * filled. Used by network drivers which may DMA or transfer data
861 * beyond the buffer end onto the wire.
863 * May return error in out of memory cases. The skb is freed on error.
866 int skb_pad(struct sk_buff *skb, int pad)
868 int err;
869 int ntail;
871 /* If the skbuff is non linear tailroom is always zero.. */
872 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
873 memset(skb->data+skb->len, 0, pad);
874 return 0;
877 ntail = skb->data_len + pad - (skb->end - skb->tail);
878 if (likely(skb_cloned(skb) || ntail > 0)) {
879 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
880 if (unlikely(err))
881 goto free_skb;
884 /* FIXME: The use of this function with non-linear skb's really needs
885 * to be audited.
887 err = skb_linearize(skb);
888 if (unlikely(err))
889 goto free_skb;
891 memset(skb->data + skb->len, 0, pad);
892 return 0;
894 free_skb:
895 kfree_skb(skb);
896 return err;
899 /* Trims skb to length len. It can change skb pointers.
902 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
904 struct sk_buff **fragp;
905 struct sk_buff *frag;
906 int offset = skb_headlen(skb);
907 int nfrags = skb_shinfo(skb)->nr_frags;
908 int i;
909 int err;
911 if (skb_cloned(skb) &&
912 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
913 return err;
915 i = 0;
916 if (offset >= len)
917 goto drop_pages;
919 for (; i < nfrags; i++) {
920 int end = offset + skb_shinfo(skb)->frags[i].size;
922 if (end < len) {
923 offset = end;
924 continue;
927 skb_shinfo(skb)->frags[i++].size = len - offset;
929 drop_pages:
930 skb_shinfo(skb)->nr_frags = i;
932 for (; i < nfrags; i++)
933 put_page(skb_shinfo(skb)->frags[i].page);
935 if (skb_shinfo(skb)->frag_list)
936 skb_drop_fraglist(skb);
937 goto done;
940 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
941 fragp = &frag->next) {
942 int end = offset + frag->len;
944 if (skb_shared(frag)) {
945 struct sk_buff *nfrag;
947 nfrag = skb_clone(frag, GFP_ATOMIC);
948 if (unlikely(!nfrag))
949 return -ENOMEM;
951 nfrag->next = frag->next;
952 kfree_skb(frag);
953 frag = nfrag;
954 *fragp = frag;
957 if (end < len) {
958 offset = end;
959 continue;
962 if (end > len &&
963 unlikely((err = pskb_trim(frag, len - offset))))
964 return err;
966 if (frag->next)
967 skb_drop_list(&frag->next);
968 break;
971 done:
972 if (len > skb_headlen(skb)) {
973 skb->data_len -= skb->len - len;
974 skb->len = len;
975 } else {
976 skb->len = len;
977 skb->data_len = 0;
978 skb_set_tail_pointer(skb, len);
981 return 0;
985 * __pskb_pull_tail - advance tail of skb header
986 * @skb: buffer to reallocate
987 * @delta: number of bytes to advance tail
989 * The function makes a sense only on a fragmented &sk_buff,
990 * it expands header moving its tail forward and copying necessary
991 * data from fragmented part.
993 * &sk_buff MUST have reference count of 1.
995 * Returns %NULL (and &sk_buff does not change) if pull failed
996 * or value of new tail of skb in the case of success.
998 * All the pointers pointing into skb header may change and must be
999 * reloaded after call to this function.
1002 /* Moves tail of skb head forward, copying data from fragmented part,
1003 * when it is necessary.
1004 * 1. It may fail due to malloc failure.
1005 * 2. It may change skb pointers.
1007 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1009 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1011 /* If skb has not enough free space at tail, get new one
1012 * plus 128 bytes for future expansions. If we have enough
1013 * room at tail, reallocate without expansion only if skb is cloned.
1015 int i, k, eat = (skb->tail + delta) - skb->end;
1017 if (eat > 0 || skb_cloned(skb)) {
1018 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1019 GFP_ATOMIC))
1020 return NULL;
1023 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1024 BUG();
1026 /* Optimization: no fragments, no reasons to preestimate
1027 * size of pulled pages. Superb.
1029 if (!skb_shinfo(skb)->frag_list)
1030 goto pull_pages;
1032 /* Estimate size of pulled pages. */
1033 eat = delta;
1034 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1035 if (skb_shinfo(skb)->frags[i].size >= eat)
1036 goto pull_pages;
1037 eat -= skb_shinfo(skb)->frags[i].size;
1040 /* If we need update frag list, we are in troubles.
1041 * Certainly, it possible to add an offset to skb data,
1042 * but taking into account that pulling is expected to
1043 * be very rare operation, it is worth to fight against
1044 * further bloating skb head and crucify ourselves here instead.
1045 * Pure masohism, indeed. 8)8)
1047 if (eat) {
1048 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1049 struct sk_buff *clone = NULL;
1050 struct sk_buff *insp = NULL;
1052 do {
1053 BUG_ON(!list);
1055 if (list->len <= eat) {
1056 /* Eaten as whole. */
1057 eat -= list->len;
1058 list = list->next;
1059 insp = list;
1060 } else {
1061 /* Eaten partially. */
1063 if (skb_shared(list)) {
1064 /* Sucks! We need to fork list. :-( */
1065 clone = skb_clone(list, GFP_ATOMIC);
1066 if (!clone)
1067 return NULL;
1068 insp = list->next;
1069 list = clone;
1070 } else {
1071 /* This may be pulled without
1072 * problems. */
1073 insp = list;
1075 if (!pskb_pull(list, eat)) {
1076 if (clone)
1077 kfree_skb(clone);
1078 return NULL;
1080 break;
1082 } while (eat);
1084 /* Free pulled out fragments. */
1085 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1086 skb_shinfo(skb)->frag_list = list->next;
1087 kfree_skb(list);
1089 /* And insert new clone at head. */
1090 if (clone) {
1091 clone->next = list;
1092 skb_shinfo(skb)->frag_list = clone;
1095 /* Success! Now we may commit changes to skb data. */
1097 pull_pages:
1098 eat = delta;
1099 k = 0;
1100 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1101 if (skb_shinfo(skb)->frags[i].size <= eat) {
1102 put_page(skb_shinfo(skb)->frags[i].page);
1103 eat -= skb_shinfo(skb)->frags[i].size;
1104 } else {
1105 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1106 if (eat) {
1107 skb_shinfo(skb)->frags[k].page_offset += eat;
1108 skb_shinfo(skb)->frags[k].size -= eat;
1109 eat = 0;
1111 k++;
1114 skb_shinfo(skb)->nr_frags = k;
1116 skb->tail += delta;
1117 skb->data_len -= delta;
1119 return skb_tail_pointer(skb);
1122 /* Copy some data bits from skb to kernel buffer. */
1124 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1126 int i, copy;
1127 int start = skb_headlen(skb);
1129 if (offset > (int)skb->len - len)
1130 goto fault;
1132 /* Copy header. */
1133 if ((copy = start - offset) > 0) {
1134 if (copy > len)
1135 copy = len;
1136 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1137 if ((len -= copy) == 0)
1138 return 0;
1139 offset += copy;
1140 to += copy;
1143 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1144 int end;
1146 BUG_TRAP(start <= offset + len);
1148 end = start + skb_shinfo(skb)->frags[i].size;
1149 if ((copy = end - offset) > 0) {
1150 u8 *vaddr;
1152 if (copy > len)
1153 copy = len;
1155 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1156 memcpy(to,
1157 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1158 offset - start, copy);
1159 kunmap_skb_frag(vaddr);
1161 if ((len -= copy) == 0)
1162 return 0;
1163 offset += copy;
1164 to += copy;
1166 start = end;
1169 if (skb_shinfo(skb)->frag_list) {
1170 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1172 for (; list; list = list->next) {
1173 int end;
1175 BUG_TRAP(start <= offset + len);
1177 end = start + list->len;
1178 if ((copy = end - offset) > 0) {
1179 if (copy > len)
1180 copy = len;
1181 if (skb_copy_bits(list, offset - start,
1182 to, copy))
1183 goto fault;
1184 if ((len -= copy) == 0)
1185 return 0;
1186 offset += copy;
1187 to += copy;
1189 start = end;
1192 if (!len)
1193 return 0;
1195 fault:
1196 return -EFAULT;
1200 * Callback from splice_to_pipe(), if we need to release some pages
1201 * at the end of the spd in case we error'ed out in filling the pipe.
1203 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1205 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1207 kfree_skb(skb);
1211 * Fill page/offset/length into spd, if it can hold more pages.
1213 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1214 unsigned int len, unsigned int offset,
1215 struct sk_buff *skb)
1217 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1218 return 1;
1220 spd->pages[spd->nr_pages] = page;
1221 spd->partial[spd->nr_pages].len = len;
1222 spd->partial[spd->nr_pages].offset = offset;
1223 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1224 spd->nr_pages++;
1225 return 0;
1229 * Map linear and fragment data from the skb to spd. Returns number of
1230 * pages mapped.
1232 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1233 unsigned int *total_len,
1234 struct splice_pipe_desc *spd)
1236 unsigned int nr_pages = spd->nr_pages;
1237 unsigned int poff, plen, len, toff, tlen;
1238 int headlen, seg, error = 0;
1240 toff = *offset;
1241 tlen = *total_len;
1242 if (!tlen) {
1243 error = 1;
1244 goto err;
1248 * if the offset is greater than the linear part, go directly to
1249 * the fragments.
1251 headlen = skb_headlen(skb);
1252 if (toff >= headlen) {
1253 toff -= headlen;
1254 goto map_frag;
1258 * first map the linear region into the pages/partial map, skipping
1259 * any potential initial offset.
1261 len = 0;
1262 while (len < headlen) {
1263 void *p = skb->data + len;
1265 poff = (unsigned long) p & (PAGE_SIZE - 1);
1266 plen = min_t(unsigned int, headlen - len, PAGE_SIZE - poff);
1267 len += plen;
1269 if (toff) {
1270 if (plen <= toff) {
1271 toff -= plen;
1272 continue;
1274 plen -= toff;
1275 poff += toff;
1276 toff = 0;
1279 plen = min(plen, tlen);
1280 if (!plen)
1281 break;
1284 * just jump directly to update and return, no point
1285 * in going over fragments when the output is full.
1287 error = spd_fill_page(spd, virt_to_page(p), plen, poff, skb);
1288 if (error)
1289 goto done;
1291 tlen -= plen;
1295 * then map the fragments
1297 map_frag:
1298 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1299 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1301 plen = f->size;
1302 poff = f->page_offset;
1304 if (toff) {
1305 if (plen <= toff) {
1306 toff -= plen;
1307 continue;
1309 plen -= toff;
1310 poff += toff;
1311 toff = 0;
1314 plen = min(plen, tlen);
1315 if (!plen)
1316 break;
1318 error = spd_fill_page(spd, f->page, plen, poff, skb);
1319 if (error)
1320 break;
1322 tlen -= plen;
1325 done:
1326 if (spd->nr_pages - nr_pages) {
1327 *offset = 0;
1328 *total_len = tlen;
1329 return 0;
1331 err:
1332 /* update the offset to reflect the linear part skip, if any */
1333 if (!error)
1334 *offset = toff;
1335 return error;
1339 * Map data from the skb to a pipe. Should handle both the linear part,
1340 * the fragments, and the frag list. It does NOT handle frag lists within
1341 * the frag list, if such a thing exists. We'd probably need to recurse to
1342 * handle that cleanly.
1344 int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1345 struct pipe_inode_info *pipe, unsigned int tlen,
1346 unsigned int flags)
1348 struct partial_page partial[PIPE_BUFFERS];
1349 struct page *pages[PIPE_BUFFERS];
1350 struct splice_pipe_desc spd = {
1351 .pages = pages,
1352 .partial = partial,
1353 .flags = flags,
1354 .ops = &sock_pipe_buf_ops,
1355 .spd_release = sock_spd_release,
1357 struct sk_buff *skb;
1360 * I'd love to avoid the clone here, but tcp_read_sock()
1361 * ignores reference counts and unconditonally kills the sk_buff
1362 * on return from the actor.
1364 skb = skb_clone(__skb, GFP_KERNEL);
1365 if (unlikely(!skb))
1366 return -ENOMEM;
1369 * __skb_splice_bits() only fails if the output has no room left,
1370 * so no point in going over the frag_list for the error case.
1372 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1373 goto done;
1374 else if (!tlen)
1375 goto done;
1378 * now see if we have a frag_list to map
1380 if (skb_shinfo(skb)->frag_list) {
1381 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1383 for (; list && tlen; list = list->next) {
1384 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1385 break;
1389 done:
1391 * drop our reference to the clone, the pipe consumption will
1392 * drop the rest.
1394 kfree_skb(skb);
1396 if (spd.nr_pages) {
1397 int ret;
1398 struct sock *sk = __skb->sk;
1401 * Drop the socket lock, otherwise we have reverse
1402 * locking dependencies between sk_lock and i_mutex
1403 * here as compared to sendfile(). We enter here
1404 * with the socket lock held, and splice_to_pipe() will
1405 * grab the pipe inode lock. For sendfile() emulation,
1406 * we call into ->sendpage() with the i_mutex lock held
1407 * and networking will grab the socket lock.
1409 release_sock(sk);
1410 ret = splice_to_pipe(pipe, &spd);
1411 lock_sock(sk);
1412 return ret;
1415 return 0;
1419 * skb_store_bits - store bits from kernel buffer to skb
1420 * @skb: destination buffer
1421 * @offset: offset in destination
1422 * @from: source buffer
1423 * @len: number of bytes to copy
1425 * Copy the specified number of bytes from the source buffer to the
1426 * destination skb. This function handles all the messy bits of
1427 * traversing fragment lists and such.
1430 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1432 int i, copy;
1433 int start = skb_headlen(skb);
1435 if (offset > (int)skb->len - len)
1436 goto fault;
1438 if ((copy = start - offset) > 0) {
1439 if (copy > len)
1440 copy = len;
1441 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1442 if ((len -= copy) == 0)
1443 return 0;
1444 offset += copy;
1445 from += copy;
1448 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1449 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1450 int end;
1452 BUG_TRAP(start <= offset + len);
1454 end = start + frag->size;
1455 if ((copy = end - offset) > 0) {
1456 u8 *vaddr;
1458 if (copy > len)
1459 copy = len;
1461 vaddr = kmap_skb_frag(frag);
1462 memcpy(vaddr + frag->page_offset + offset - start,
1463 from, copy);
1464 kunmap_skb_frag(vaddr);
1466 if ((len -= copy) == 0)
1467 return 0;
1468 offset += copy;
1469 from += copy;
1471 start = end;
1474 if (skb_shinfo(skb)->frag_list) {
1475 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1477 for (; list; list = list->next) {
1478 int end;
1480 BUG_TRAP(start <= offset + len);
1482 end = start + list->len;
1483 if ((copy = end - offset) > 0) {
1484 if (copy > len)
1485 copy = len;
1486 if (skb_store_bits(list, offset - start,
1487 from, copy))
1488 goto fault;
1489 if ((len -= copy) == 0)
1490 return 0;
1491 offset += copy;
1492 from += copy;
1494 start = end;
1497 if (!len)
1498 return 0;
1500 fault:
1501 return -EFAULT;
1504 EXPORT_SYMBOL(skb_store_bits);
1506 /* Checksum skb data. */
1508 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1509 int len, __wsum csum)
1511 int start = skb_headlen(skb);
1512 int i, copy = start - offset;
1513 int pos = 0;
1515 /* Checksum header. */
1516 if (copy > 0) {
1517 if (copy > len)
1518 copy = len;
1519 csum = csum_partial(skb->data + offset, copy, csum);
1520 if ((len -= copy) == 0)
1521 return csum;
1522 offset += copy;
1523 pos = copy;
1526 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1527 int end;
1529 BUG_TRAP(start <= offset + len);
1531 end = start + skb_shinfo(skb)->frags[i].size;
1532 if ((copy = end - offset) > 0) {
1533 __wsum csum2;
1534 u8 *vaddr;
1535 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1537 if (copy > len)
1538 copy = len;
1539 vaddr = kmap_skb_frag(frag);
1540 csum2 = csum_partial(vaddr + frag->page_offset +
1541 offset - start, copy, 0);
1542 kunmap_skb_frag(vaddr);
1543 csum = csum_block_add(csum, csum2, pos);
1544 if (!(len -= copy))
1545 return csum;
1546 offset += copy;
1547 pos += copy;
1549 start = end;
1552 if (skb_shinfo(skb)->frag_list) {
1553 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1555 for (; list; list = list->next) {
1556 int end;
1558 BUG_TRAP(start <= offset + len);
1560 end = start + list->len;
1561 if ((copy = end - offset) > 0) {
1562 __wsum csum2;
1563 if (copy > len)
1564 copy = len;
1565 csum2 = skb_checksum(list, offset - start,
1566 copy, 0);
1567 csum = csum_block_add(csum, csum2, pos);
1568 if ((len -= copy) == 0)
1569 return csum;
1570 offset += copy;
1571 pos += copy;
1573 start = end;
1576 BUG_ON(len);
1578 return csum;
1581 /* Both of above in one bottle. */
1583 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1584 u8 *to, int len, __wsum csum)
1586 int start = skb_headlen(skb);
1587 int i, copy = start - offset;
1588 int pos = 0;
1590 /* Copy header. */
1591 if (copy > 0) {
1592 if (copy > len)
1593 copy = len;
1594 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1595 copy, csum);
1596 if ((len -= copy) == 0)
1597 return csum;
1598 offset += copy;
1599 to += copy;
1600 pos = copy;
1603 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1604 int end;
1606 BUG_TRAP(start <= offset + len);
1608 end = start + skb_shinfo(skb)->frags[i].size;
1609 if ((copy = end - offset) > 0) {
1610 __wsum csum2;
1611 u8 *vaddr;
1612 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1614 if (copy > len)
1615 copy = len;
1616 vaddr = kmap_skb_frag(frag);
1617 csum2 = csum_partial_copy_nocheck(vaddr +
1618 frag->page_offset +
1619 offset - start, to,
1620 copy, 0);
1621 kunmap_skb_frag(vaddr);
1622 csum = csum_block_add(csum, csum2, pos);
1623 if (!(len -= copy))
1624 return csum;
1625 offset += copy;
1626 to += copy;
1627 pos += copy;
1629 start = end;
1632 if (skb_shinfo(skb)->frag_list) {
1633 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1635 for (; list; list = list->next) {
1636 __wsum csum2;
1637 int end;
1639 BUG_TRAP(start <= offset + len);
1641 end = start + list->len;
1642 if ((copy = end - offset) > 0) {
1643 if (copy > len)
1644 copy = len;
1645 csum2 = skb_copy_and_csum_bits(list,
1646 offset - start,
1647 to, copy, 0);
1648 csum = csum_block_add(csum, csum2, pos);
1649 if ((len -= copy) == 0)
1650 return csum;
1651 offset += copy;
1652 to += copy;
1653 pos += copy;
1655 start = end;
1658 BUG_ON(len);
1659 return csum;
1662 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1664 __wsum csum;
1665 long csstart;
1667 if (skb->ip_summed == CHECKSUM_PARTIAL)
1668 csstart = skb->csum_start - skb_headroom(skb);
1669 else
1670 csstart = skb_headlen(skb);
1672 BUG_ON(csstart > skb_headlen(skb));
1674 skb_copy_from_linear_data(skb, to, csstart);
1676 csum = 0;
1677 if (csstart != skb->len)
1678 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1679 skb->len - csstart, 0);
1681 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1682 long csstuff = csstart + skb->csum_offset;
1684 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1689 * skb_dequeue - remove from the head of the queue
1690 * @list: list to dequeue from
1692 * Remove the head of the list. The list lock is taken so the function
1693 * may be used safely with other locking list functions. The head item is
1694 * returned or %NULL if the list is empty.
1697 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1699 unsigned long flags;
1700 struct sk_buff *result;
1702 spin_lock_irqsave(&list->lock, flags);
1703 result = __skb_dequeue(list);
1704 spin_unlock_irqrestore(&list->lock, flags);
1705 return result;
1709 * skb_dequeue_tail - remove from the tail of the queue
1710 * @list: list to dequeue from
1712 * Remove the tail of the list. The list lock is taken so the function
1713 * may be used safely with other locking list functions. The tail item is
1714 * returned or %NULL if the list is empty.
1716 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1718 unsigned long flags;
1719 struct sk_buff *result;
1721 spin_lock_irqsave(&list->lock, flags);
1722 result = __skb_dequeue_tail(list);
1723 spin_unlock_irqrestore(&list->lock, flags);
1724 return result;
1728 * skb_queue_purge - empty a list
1729 * @list: list to empty
1731 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1732 * the list and one reference dropped. This function takes the list
1733 * lock and is atomic with respect to other list locking functions.
1735 void skb_queue_purge(struct sk_buff_head *list)
1737 struct sk_buff *skb;
1738 while ((skb = skb_dequeue(list)) != NULL)
1739 kfree_skb(skb);
1743 * skb_queue_head - queue a buffer at the list head
1744 * @list: list to use
1745 * @newsk: buffer to queue
1747 * Queue a buffer at the start of the list. This function takes the
1748 * list lock and can be used safely with other locking &sk_buff functions
1749 * safely.
1751 * A buffer cannot be placed on two lists at the same time.
1753 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1755 unsigned long flags;
1757 spin_lock_irqsave(&list->lock, flags);
1758 __skb_queue_head(list, newsk);
1759 spin_unlock_irqrestore(&list->lock, flags);
1763 * skb_queue_tail - queue a buffer at the list tail
1764 * @list: list to use
1765 * @newsk: buffer to queue
1767 * Queue a buffer at the tail of the list. This function takes the
1768 * list lock and can be used safely with other locking &sk_buff functions
1769 * safely.
1771 * A buffer cannot be placed on two lists at the same time.
1773 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1775 unsigned long flags;
1777 spin_lock_irqsave(&list->lock, flags);
1778 __skb_queue_tail(list, newsk);
1779 spin_unlock_irqrestore(&list->lock, flags);
1783 * skb_unlink - remove a buffer from a list
1784 * @skb: buffer to remove
1785 * @list: list to use
1787 * Remove a packet from a list. The list locks are taken and this
1788 * function is atomic with respect to other list locked calls
1790 * You must know what list the SKB is on.
1792 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1794 unsigned long flags;
1796 spin_lock_irqsave(&list->lock, flags);
1797 __skb_unlink(skb, list);
1798 spin_unlock_irqrestore(&list->lock, flags);
1802 * skb_append - append a buffer
1803 * @old: buffer to insert after
1804 * @newsk: buffer to insert
1805 * @list: list to use
1807 * Place a packet after a given packet in a list. The list locks are taken
1808 * and this function is atomic with respect to other list locked calls.
1809 * A buffer cannot be placed on two lists at the same time.
1811 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1813 unsigned long flags;
1815 spin_lock_irqsave(&list->lock, flags);
1816 __skb_append(old, newsk, list);
1817 spin_unlock_irqrestore(&list->lock, flags);
1822 * skb_insert - insert a buffer
1823 * @old: buffer to insert before
1824 * @newsk: buffer to insert
1825 * @list: list to use
1827 * Place a packet before a given packet in a list. The list locks are
1828 * taken and this function is atomic with respect to other list locked
1829 * calls.
1831 * A buffer cannot be placed on two lists at the same time.
1833 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1835 unsigned long flags;
1837 spin_lock_irqsave(&list->lock, flags);
1838 __skb_insert(newsk, old->prev, old, list);
1839 spin_unlock_irqrestore(&list->lock, flags);
1842 static inline void skb_split_inside_header(struct sk_buff *skb,
1843 struct sk_buff* skb1,
1844 const u32 len, const int pos)
1846 int i;
1848 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1849 pos - len);
1850 /* And move data appendix as is. */
1851 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1852 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1854 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1855 skb_shinfo(skb)->nr_frags = 0;
1856 skb1->data_len = skb->data_len;
1857 skb1->len += skb1->data_len;
1858 skb->data_len = 0;
1859 skb->len = len;
1860 skb_set_tail_pointer(skb, len);
1863 static inline void skb_split_no_header(struct sk_buff *skb,
1864 struct sk_buff* skb1,
1865 const u32 len, int pos)
1867 int i, k = 0;
1868 const int nfrags = skb_shinfo(skb)->nr_frags;
1870 skb_shinfo(skb)->nr_frags = 0;
1871 skb1->len = skb1->data_len = skb->len - len;
1872 skb->len = len;
1873 skb->data_len = len - pos;
1875 for (i = 0; i < nfrags; i++) {
1876 int size = skb_shinfo(skb)->frags[i].size;
1878 if (pos + size > len) {
1879 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1881 if (pos < len) {
1882 /* Split frag.
1883 * We have two variants in this case:
1884 * 1. Move all the frag to the second
1885 * part, if it is possible. F.e.
1886 * this approach is mandatory for TUX,
1887 * where splitting is expensive.
1888 * 2. Split is accurately. We make this.
1890 get_page(skb_shinfo(skb)->frags[i].page);
1891 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1892 skb_shinfo(skb1)->frags[0].size -= len - pos;
1893 skb_shinfo(skb)->frags[i].size = len - pos;
1894 skb_shinfo(skb)->nr_frags++;
1896 k++;
1897 } else
1898 skb_shinfo(skb)->nr_frags++;
1899 pos += size;
1901 skb_shinfo(skb1)->nr_frags = k;
1905 * skb_split - Split fragmented skb to two parts at length len.
1906 * @skb: the buffer to split
1907 * @skb1: the buffer to receive the second part
1908 * @len: new length for skb
1910 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1912 int pos = skb_headlen(skb);
1914 if (len < pos) /* Split line is inside header. */
1915 skb_split_inside_header(skb, skb1, len, pos);
1916 else /* Second chunk has no header, nothing to copy. */
1917 skb_split_no_header(skb, skb1, len, pos);
1921 * skb_prepare_seq_read - Prepare a sequential read of skb data
1922 * @skb: the buffer to read
1923 * @from: lower offset of data to be read
1924 * @to: upper offset of data to be read
1925 * @st: state variable
1927 * Initializes the specified state variable. Must be called before
1928 * invoking skb_seq_read() for the first time.
1930 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1931 unsigned int to, struct skb_seq_state *st)
1933 st->lower_offset = from;
1934 st->upper_offset = to;
1935 st->root_skb = st->cur_skb = skb;
1936 st->frag_idx = st->stepped_offset = 0;
1937 st->frag_data = NULL;
1941 * skb_seq_read - Sequentially read skb data
1942 * @consumed: number of bytes consumed by the caller so far
1943 * @data: destination pointer for data to be returned
1944 * @st: state variable
1946 * Reads a block of skb data at &consumed relative to the
1947 * lower offset specified to skb_prepare_seq_read(). Assigns
1948 * the head of the data block to &data and returns the length
1949 * of the block or 0 if the end of the skb data or the upper
1950 * offset has been reached.
1952 * The caller is not required to consume all of the data
1953 * returned, i.e. &consumed is typically set to the number
1954 * of bytes already consumed and the next call to
1955 * skb_seq_read() will return the remaining part of the block.
1957 * Note: The size of each block of data returned can be arbitary,
1958 * this limitation is the cost for zerocopy seqeuental
1959 * reads of potentially non linear data.
1961 * Note: Fragment lists within fragments are not implemented
1962 * at the moment, state->root_skb could be replaced with
1963 * a stack for this purpose.
1965 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1966 struct skb_seq_state *st)
1968 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1969 skb_frag_t *frag;
1971 if (unlikely(abs_offset >= st->upper_offset))
1972 return 0;
1974 next_skb:
1975 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
1977 if (abs_offset < block_limit && !st->frag_data) {
1978 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
1979 return block_limit - abs_offset;
1982 if (st->frag_idx == 0 && !st->frag_data)
1983 st->stepped_offset += skb_headlen(st->cur_skb);
1985 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1986 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1987 block_limit = frag->size + st->stepped_offset;
1989 if (abs_offset < block_limit) {
1990 if (!st->frag_data)
1991 st->frag_data = kmap_skb_frag(frag);
1993 *data = (u8 *) st->frag_data + frag->page_offset +
1994 (abs_offset - st->stepped_offset);
1996 return block_limit - abs_offset;
1999 if (st->frag_data) {
2000 kunmap_skb_frag(st->frag_data);
2001 st->frag_data = NULL;
2004 st->frag_idx++;
2005 st->stepped_offset += frag->size;
2008 if (st->frag_data) {
2009 kunmap_skb_frag(st->frag_data);
2010 st->frag_data = NULL;
2013 if (st->root_skb == st->cur_skb &&
2014 skb_shinfo(st->root_skb)->frag_list) {
2015 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2016 st->frag_idx = 0;
2017 goto next_skb;
2018 } else if (st->cur_skb->next) {
2019 st->cur_skb = st->cur_skb->next;
2020 st->frag_idx = 0;
2021 goto next_skb;
2024 return 0;
2028 * skb_abort_seq_read - Abort a sequential read of skb data
2029 * @st: state variable
2031 * Must be called if skb_seq_read() was not called until it
2032 * returned 0.
2034 void skb_abort_seq_read(struct skb_seq_state *st)
2036 if (st->frag_data)
2037 kunmap_skb_frag(st->frag_data);
2040 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2042 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2043 struct ts_config *conf,
2044 struct ts_state *state)
2046 return skb_seq_read(offset, text, TS_SKB_CB(state));
2049 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2051 skb_abort_seq_read(TS_SKB_CB(state));
2055 * skb_find_text - Find a text pattern in skb data
2056 * @skb: the buffer to look in
2057 * @from: search offset
2058 * @to: search limit
2059 * @config: textsearch configuration
2060 * @state: uninitialized textsearch state variable
2062 * Finds a pattern in the skb data according to the specified
2063 * textsearch configuration. Use textsearch_next() to retrieve
2064 * subsequent occurrences of the pattern. Returns the offset
2065 * to the first occurrence or UINT_MAX if no match was found.
2067 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2068 unsigned int to, struct ts_config *config,
2069 struct ts_state *state)
2071 unsigned int ret;
2073 config->get_next_block = skb_ts_get_next_block;
2074 config->finish = skb_ts_finish;
2076 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2078 ret = textsearch_find(config, state);
2079 return (ret <= to - from ? ret : UINT_MAX);
2083 * skb_append_datato_frags: - append the user data to a skb
2084 * @sk: sock structure
2085 * @skb: skb structure to be appened with user data.
2086 * @getfrag: call back function to be used for getting the user data
2087 * @from: pointer to user message iov
2088 * @length: length of the iov message
2090 * Description: This procedure append the user data in the fragment part
2091 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2093 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2094 int (*getfrag)(void *from, char *to, int offset,
2095 int len, int odd, struct sk_buff *skb),
2096 void *from, int length)
2098 int frg_cnt = 0;
2099 skb_frag_t *frag = NULL;
2100 struct page *page = NULL;
2101 int copy, left;
2102 int offset = 0;
2103 int ret;
2105 do {
2106 /* Return error if we don't have space for new frag */
2107 frg_cnt = skb_shinfo(skb)->nr_frags;
2108 if (frg_cnt >= MAX_SKB_FRAGS)
2109 return -EFAULT;
2111 /* allocate a new page for next frag */
2112 page = alloc_pages(sk->sk_allocation, 0);
2114 /* If alloc_page fails just return failure and caller will
2115 * free previous allocated pages by doing kfree_skb()
2117 if (page == NULL)
2118 return -ENOMEM;
2120 /* initialize the next frag */
2121 sk->sk_sndmsg_page = page;
2122 sk->sk_sndmsg_off = 0;
2123 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2124 skb->truesize += PAGE_SIZE;
2125 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2127 /* get the new initialized frag */
2128 frg_cnt = skb_shinfo(skb)->nr_frags;
2129 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2131 /* copy the user data to page */
2132 left = PAGE_SIZE - frag->page_offset;
2133 copy = (length > left)? left : length;
2135 ret = getfrag(from, (page_address(frag->page) +
2136 frag->page_offset + frag->size),
2137 offset, copy, 0, skb);
2138 if (ret < 0)
2139 return -EFAULT;
2141 /* copy was successful so update the size parameters */
2142 sk->sk_sndmsg_off += copy;
2143 frag->size += copy;
2144 skb->len += copy;
2145 skb->data_len += copy;
2146 offset += copy;
2147 length -= copy;
2149 } while (length > 0);
2151 return 0;
2155 * skb_pull_rcsum - pull skb and update receive checksum
2156 * @skb: buffer to update
2157 * @start: start of data before pull
2158 * @len: length of data pulled
2160 * This function performs an skb_pull on the packet and updates
2161 * update the CHECKSUM_COMPLETE checksum. It should be used on
2162 * receive path processing instead of skb_pull unless you know
2163 * that the checksum difference is zero (e.g., a valid IP header)
2164 * or you are setting ip_summed to CHECKSUM_NONE.
2166 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2168 BUG_ON(len > skb->len);
2169 skb->len -= len;
2170 BUG_ON(skb->len < skb->data_len);
2171 skb_postpull_rcsum(skb, skb->data, len);
2172 return skb->data += len;
2175 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2178 * skb_segment - Perform protocol segmentation on skb.
2179 * @skb: buffer to segment
2180 * @features: features for the output path (see dev->features)
2182 * This function performs segmentation on the given skb. It returns
2183 * the segment at the given position. It returns NULL if there are
2184 * no more segments to generate, or when an error is encountered.
2186 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2188 struct sk_buff *segs = NULL;
2189 struct sk_buff *tail = NULL;
2190 unsigned int mss = skb_shinfo(skb)->gso_size;
2191 unsigned int doffset = skb->data - skb_mac_header(skb);
2192 unsigned int offset = doffset;
2193 unsigned int headroom;
2194 unsigned int len;
2195 int sg = features & NETIF_F_SG;
2196 int nfrags = skb_shinfo(skb)->nr_frags;
2197 int err = -ENOMEM;
2198 int i = 0;
2199 int pos;
2201 __skb_push(skb, doffset);
2202 headroom = skb_headroom(skb);
2203 pos = skb_headlen(skb);
2205 do {
2206 struct sk_buff *nskb;
2207 skb_frag_t *frag;
2208 int hsize;
2209 int k;
2210 int size;
2212 len = skb->len - offset;
2213 if (len > mss)
2214 len = mss;
2216 hsize = skb_headlen(skb) - offset;
2217 if (hsize < 0)
2218 hsize = 0;
2219 if (hsize > len || !sg)
2220 hsize = len;
2222 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
2223 if (unlikely(!nskb))
2224 goto err;
2226 if (segs)
2227 tail->next = nskb;
2228 else
2229 segs = nskb;
2230 tail = nskb;
2232 __copy_skb_header(nskb, skb);
2233 nskb->mac_len = skb->mac_len;
2234 #ifdef HNDCTF
2235 nskb->ctf_mac_len = skb->ctf_mac_len; /* used by Broadcom CTF driver! */
2236 #endif
2238 skb_reserve(nskb, headroom);
2239 skb_reset_mac_header(nskb);
2240 skb_set_network_header(nskb, skb->mac_len);
2241 nskb->transport_header = (nskb->network_header +
2242 skb_network_header_len(skb));
2243 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2244 doffset);
2245 if (!sg) {
2246 nskb->ip_summed = CHECKSUM_NONE;
2247 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2248 skb_put(nskb, len),
2249 len, 0);
2250 continue;
2253 frag = skb_shinfo(nskb)->frags;
2254 k = 0;
2256 skb_copy_from_linear_data_offset(skb, offset,
2257 skb_put(nskb, hsize), hsize);
2259 while (pos < offset + len) {
2260 BUG_ON(i >= nfrags);
2262 *frag = skb_shinfo(skb)->frags[i];
2263 get_page(frag->page);
2264 size = frag->size;
2266 if (pos < offset) {
2267 frag->page_offset += offset - pos;
2268 frag->size -= offset - pos;
2271 k++;
2273 if (pos + size <= offset + len) {
2274 i++;
2275 pos += size;
2276 } else {
2277 frag->size -= pos + size - (offset + len);
2278 break;
2281 frag++;
2284 skb_shinfo(nskb)->nr_frags = k;
2285 nskb->data_len = len - hsize;
2286 nskb->len += nskb->data_len;
2287 nskb->truesize += nskb->data_len;
2288 } while ((offset += len) < skb->len);
2290 return segs;
2292 err:
2293 while ((skb = segs)) {
2294 segs = skb->next;
2295 kfree_skb(skb);
2297 return ERR_PTR(err);
2300 EXPORT_SYMBOL_GPL(skb_segment);
2302 void __init skb_init(void)
2304 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2305 sizeof(struct sk_buff),
2307 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2308 NULL, NULL);
2309 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2310 (2*sizeof(struct sk_buff)) +
2311 sizeof(atomic_t),
2313 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2314 NULL, NULL);
2318 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2319 * @skb: Socket buffer containing the buffers to be mapped
2320 * @sg: The scatter-gather list to map into
2321 * @offset: The offset into the buffer's contents to start mapping
2322 * @len: Length of buffer space to be mapped
2324 * Fill the specified scatter-gather list with mappings/pointers into a
2325 * region of the buffer space attached to a socket buffer.
2328 skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2330 int start = skb_headlen(skb);
2331 int i, copy = start - offset;
2332 int elt = 0;
2334 if (copy > 0) {
2335 if (copy > len)
2336 copy = len;
2337 sg[elt].page = virt_to_page(skb->data + offset);
2338 sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
2339 sg[elt].length = copy;
2340 elt++;
2341 if ((len -= copy) == 0)
2342 return elt;
2343 offset += copy;
2346 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2347 int end;
2349 BUG_TRAP(start <= offset + len);
2351 end = start + skb_shinfo(skb)->frags[i].size;
2352 if ((copy = end - offset) > 0) {
2353 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2355 if (copy > len)
2356 copy = len;
2357 sg[elt].page = frag->page;
2358 sg[elt].offset = frag->page_offset+offset-start;
2359 sg[elt].length = copy;
2360 elt++;
2361 if (!(len -= copy))
2362 return elt;
2363 offset += copy;
2365 start = end;
2368 if (skb_shinfo(skb)->frag_list) {
2369 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2371 for (; list; list = list->next) {
2372 int end;
2374 BUG_TRAP(start <= offset + len);
2376 end = start + list->len;
2377 if ((copy = end - offset) > 0) {
2378 if (copy > len)
2379 copy = len;
2380 elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
2381 if ((len -= copy) == 0)
2382 return elt;
2383 offset += copy;
2385 start = end;
2388 BUG_ON(len);
2389 return elt;
2393 * skb_cow_data - Check that a socket buffer's data buffers are writable
2394 * @skb: The socket buffer to check.
2395 * @tailbits: Amount of trailing space to be added
2396 * @trailer: Returned pointer to the skb where the @tailbits space begins
2398 * Make sure that the data buffers attached to a socket buffer are
2399 * writable. If they are not, private copies are made of the data buffers
2400 * and the socket buffer is set to use these instead.
2402 * If @tailbits is given, make sure that there is space to write @tailbits
2403 * bytes of data beyond current end of socket buffer. @trailer will be
2404 * set to point to the skb in which this space begins.
2406 * The number of scatterlist elements required to completely map the
2407 * COW'd and extended socket buffer will be returned.
2409 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2411 int copyflag;
2412 int elt;
2413 struct sk_buff *skb1, **skb_p;
2415 /* If skb is cloned or its head is paged, reallocate
2416 * head pulling out all the pages (pages are considered not writable
2417 * at the moment even if they are anonymous).
2419 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2420 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2421 return -ENOMEM;
2423 /* Easy case. Most of packets will go this way. */
2424 if (!skb_shinfo(skb)->frag_list) {
2425 /* A little of trouble, not enough of space for trailer.
2426 * This should not happen, when stack is tuned to generate
2427 * good frames. OK, on miss we reallocate and reserve even more
2428 * space, 128 bytes is fair. */
2430 if (skb_tailroom(skb) < tailbits &&
2431 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2432 return -ENOMEM;
2434 /* Voila! */
2435 *trailer = skb;
2436 return 1;
2439 /* Misery. We are in troubles, going to mincer fragments... */
2441 elt = 1;
2442 skb_p = &skb_shinfo(skb)->frag_list;
2443 copyflag = 0;
2445 while ((skb1 = *skb_p) != NULL) {
2446 int ntail = 0;
2448 /* The fragment is partially pulled by someone,
2449 * this can happen on input. Copy it and everything
2450 * after it. */
2452 if (skb_shared(skb1))
2453 copyflag = 1;
2455 /* If the skb is the last, worry about trailer. */
2457 if (skb1->next == NULL && tailbits) {
2458 if (skb_shinfo(skb1)->nr_frags ||
2459 skb_shinfo(skb1)->frag_list ||
2460 skb_tailroom(skb1) < tailbits)
2461 ntail = tailbits + 128;
2464 if (copyflag ||
2465 skb_cloned(skb1) ||
2466 ntail ||
2467 skb_shinfo(skb1)->nr_frags ||
2468 skb_shinfo(skb1)->frag_list) {
2469 struct sk_buff *skb2;
2471 /* Fuck, we are miserable poor guys... */
2472 if (ntail == 0)
2473 skb2 = skb_copy(skb1, GFP_ATOMIC);
2474 else
2475 skb2 = skb_copy_expand(skb1,
2476 skb_headroom(skb1),
2477 ntail,
2478 GFP_ATOMIC);
2479 if (unlikely(skb2 == NULL))
2480 return -ENOMEM;
2482 if (skb1->sk)
2483 skb_set_owner_w(skb2, skb1->sk);
2485 /* Looking around. Are we still alive?
2486 * OK, link new skb, drop old one */
2488 skb2->next = skb1->next;
2489 *skb_p = skb2;
2490 kfree_skb(skb1);
2491 skb1 = skb2;
2493 elt++;
2494 *trailer = skb1;
2495 skb_p = &skb1->next;
2498 return elt;
2501 EXPORT_SYMBOL(___pskb_trim);
2502 EXPORT_SYMBOL(__kfree_skb);
2503 EXPORT_SYMBOL(kfree_skb);
2504 EXPORT_SYMBOL(__pskb_pull_tail);
2505 EXPORT_SYMBOL(__alloc_skb);
2506 EXPORT_SYMBOL(__netdev_alloc_skb);
2507 EXPORT_SYMBOL(pskb_copy);
2508 EXPORT_SYMBOL(pskb_expand_head);
2509 EXPORT_SYMBOL(skb_checksum);
2510 EXPORT_SYMBOL(skb_clone);
2511 EXPORT_SYMBOL(skb_copy);
2512 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2513 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2514 EXPORT_SYMBOL(skb_copy_bits);
2515 EXPORT_SYMBOL(skb_copy_expand);
2516 EXPORT_SYMBOL(skb_over_panic);
2517 EXPORT_SYMBOL(skb_pad);
2518 EXPORT_SYMBOL(skb_realloc_headroom);
2519 EXPORT_SYMBOL(skb_under_panic);
2520 EXPORT_SYMBOL(skb_dequeue);
2521 EXPORT_SYMBOL(skb_dequeue_tail);
2522 EXPORT_SYMBOL(skb_insert);
2523 EXPORT_SYMBOL(skb_queue_purge);
2524 EXPORT_SYMBOL(skb_queue_head);
2525 EXPORT_SYMBOL(skb_queue_tail);
2526 EXPORT_SYMBOL(skb_unlink);
2527 EXPORT_SYMBOL(skb_append);
2528 EXPORT_SYMBOL(skb_split);
2529 EXPORT_SYMBOL(skb_prepare_seq_read);
2530 EXPORT_SYMBOL(skb_seq_read);
2531 EXPORT_SYMBOL(skb_abort_seq_read);
2532 EXPORT_SYMBOL(skb_find_text);
2533 EXPORT_SYMBOL(skb_append_datato_frags);
2535 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2536 EXPORT_SYMBOL_GPL(skb_cow_data);