[SKBUFF]: add skb_morph & co.
[tomato.git] / release / src-rt / linux / linux-2.6 / net / core / skbuff.c
blobc2627de42dae063cc289e8efab7c96d018a9dba2
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 #endif
218 #ifdef CONFIG_BRIDGE_NETFILTER
219 skb->nf_bridge = NULL;
220 #endif
221 #ifdef CONFIG_NET_SCHED
222 skb->tc_index = 0;
223 //#ifdef CONFIG_NET_CLS_ACT
224 skb->tc_verd = 0;
225 //#endif
226 #endif
227 #ifdef CONFIG_NET_DMA
228 memset(&skb->dma_cookie, 0, sizeof(dma_cookie_t));
229 #endif
230 #ifdef CONFIG_NETWORK_SECMARK
231 skb->secmark =0;
232 #endif
234 /* make sure we initialize shinfo sequentially */
235 shinfo = skb_shinfo(skb);
236 memset(shinfo, 0, offsetof(struct skb_shared_info, frags));
237 atomic_set(&shinfo->dataref, 1);
239 #if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
240 skb->imq_flags = 0;
241 skb->nf_info = NULL;
242 #endif
244 if (fclone) {
245 struct sk_buff *child = skb + 1;
246 atomic_t *fclone_ref = (atomic_t *) (child + 1);
248 skb->fclone = SKB_FCLONE_ORIG;
249 atomic_set(fclone_ref, 1);
251 child->fclone = SKB_FCLONE_UNAVAILABLE;
253 out:
254 return skb;
255 nodata:
256 kmem_cache_free(cache, skb);
257 skb = NULL;
258 goto out;
262 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
263 * @dev: network device to receive on
264 * @length: length to allocate
265 * @gfp_mask: get_free_pages mask, passed to alloc_skb
267 * Allocate a new &sk_buff and assign it a usage count of one. The
268 * buffer has unspecified headroom built in. Users should allocate
269 * the headroom they think they need without accounting for the
270 * built in space. The built in space is used for optimisations.
272 * %NULL is returned if there is no free memory.
274 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
275 unsigned int length, gfp_t gfp_mask)
277 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
278 struct sk_buff *skb;
280 skb = __alloc_skb(length + NET_SKB_PAD_ALLOC, gfp_mask, 0, node);
281 if (likely(skb)) {
282 skb_reserve(skb, NET_SKB_PAD_ALLOC);
283 skb->dev = dev;
285 return skb;
288 static void skb_drop_list(struct sk_buff **listp)
290 struct sk_buff *list = *listp;
292 *listp = NULL;
294 do {
295 struct sk_buff *this = list;
296 list = list->next;
297 kfree_skb(this);
298 } while (list);
301 static inline void skb_drop_fraglist(struct sk_buff *skb)
303 skb_drop_list(&skb_shinfo(skb)->frag_list);
306 static void skb_clone_fraglist(struct sk_buff *skb)
308 struct sk_buff *list;
310 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
311 skb_get(list);
314 static void skb_release_data(struct sk_buff *skb)
316 if (!skb->cloned ||
317 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
318 &skb_shinfo(skb)->dataref)) {
319 if (skb_shinfo(skb)->nr_frags) {
320 int i;
321 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
322 put_page(skb_shinfo(skb)->frags[i].page);
325 if (skb_shinfo(skb)->frag_list)
326 skb_drop_fraglist(skb);
328 kfree(skb->head);
333 * Free an skbuff by memory without cleaning the state.
335 static void kfree_skbmem(struct sk_buff *skb)
337 struct sk_buff *other;
338 atomic_t *fclone_ref;
340 switch (skb->fclone) {
341 case SKB_FCLONE_UNAVAILABLE:
342 kmem_cache_free(skbuff_head_cache, skb);
343 break;
345 case SKB_FCLONE_ORIG:
346 fclone_ref = (atomic_t *) (skb + 2);
347 if (atomic_dec_and_test(fclone_ref))
348 kmem_cache_free(skbuff_fclone_cache, skb);
349 break;
351 case SKB_FCLONE_CLONE:
352 fclone_ref = (atomic_t *) (skb + 1);
353 other = skb - 1;
355 /* The clone portion is available for
356 * fast-cloning again.
358 skb->fclone = SKB_FCLONE_UNAVAILABLE;
360 if (atomic_dec_and_test(fclone_ref))
361 kmem_cache_free(skbuff_fclone_cache, other);
362 break;
366 /* Free everything but the sk_buff shell. */
367 static void skb_release_all(struct sk_buff *skb)
369 dst_release(skb->dst);
370 #ifdef CONFIG_XFRM
371 secpath_put(skb->sp);
372 #endif
373 if (skb->destructor) {
374 #ifdef CONFIG_NETCORE_DEBUG
375 WARN_ON(in_irq());
376 #endif
377 skb->destructor(skb);
379 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
380 nf_conntrack_put(skb->nfct);
381 nf_conntrack_put_reasm(skb->nfct_reasm);
382 #endif
383 #ifdef CONFIG_BRIDGE_NETFILTER
384 nf_bridge_put(skb->nf_bridge);
385 #endif
386 /* XXX: IS this still necessary? - JHS */
387 #ifdef CONFIG_NET_SCHED
388 skb->tc_index = 0;
389 //#ifdef CONFIG_NET_CLS_ACT
390 skb->tc_verd = 0;
391 //#endif
392 #endif
393 skb_release_data(skb);
397 * __kfree_skb - private function
398 * @skb: buffer
400 * Free an sk_buff. Release anything attached to the buffer.
401 * Clean the state. This is an internal helper function. Users should
402 * always call kfree_skb
405 void __kfree_skb(struct sk_buff *skb)
407 skb_release_all(skb);
408 kfree_skbmem(skb);
412 * kfree_skb - free an sk_buff
413 * @skb: buffer to free
415 * Drop a reference to the buffer and free it if the usage count has
416 * hit zero.
418 void kfree_skb(struct sk_buff *skb)
420 if (unlikely(!skb))
421 return;
422 if (likely(atomic_read(&skb->users) == 1))
423 smp_rmb();
424 else if (likely(!atomic_dec_and_test(&skb->users)))
425 return;
426 __kfree_skb(skb);
429 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
431 new->tstamp = old->tstamp;
432 new->dev = old->dev;
433 new->transport_header = old->transport_header;
434 new->network_header = old->network_header;
435 new->mac_header = old->mac_header;
436 new->dst = dst_clone(old->dst);
437 #ifdef CONFIG_INET
438 new->sp = secpath_get(old->sp);
439 #endif
440 memcpy(new->cb, old->cb, sizeof(old->cb));
441 new->csum = old->csum;
442 new->local_df = old->local_df;
443 new->pkt_type = old->pkt_type;
444 new->ip_summed = old->ip_summed;
445 new->priority = old->priority;
446 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
447 new->ipvs_property = old->ipvs_property;
448 #endif
449 new->protocol = old->protocol;
450 new->mark = old->mark;
451 new->iif = old->iif;
452 __nf_copy(new, old);
453 #ifdef CONFIG_NET_SCHED
454 new->tc_index = old->tc_index;
455 //#ifdef CONFIG_NET_CLS_ACT
456 new->tc_verd = old->tc_verd;
457 //#endif
458 #endif
459 new->vlan_tci = old->vlan_tci;
461 skb_copy_secmark(new, old);
462 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
463 new->nfcache = old->nfcache;
464 #endif
468 * You should not add any new code to this function. Add it to
469 * __copy_skb_header above instead.
471 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
473 #define C(x) n->x = skb->x
475 n->next = n->prev = NULL;
476 n->sk = NULL;
477 __copy_skb_header(n, skb);
479 C(len);
480 C(data_len);
481 C(mac_len);
482 #ifdef HNDCTF
483 C(ctf_mac_len); /* used by Broadcom CTF driver! */
484 #endif
485 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
486 n->cloned = 1;
487 n->nohdr = 0;
488 n->destructor = NULL;
489 C(tail);
490 C(end);
491 C(head);
492 C(data);
493 C(truesize);
494 atomic_set(&n->users, 1);
496 atomic_inc(&(skb_shinfo(skb)->dataref));
497 skb->cloned = 1;
499 return n;
500 #undef C
504 * skb_morph - morph one skb into another
505 * @dst: the skb to receive the contents
506 * @src: the skb to supply the contents
508 * This is identical to skb_clone except that the target skb is
509 * supplied by the user.
511 * The target skb is returned upon exit.
513 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
515 skb_release_all(dst);
516 return __skb_clone(dst, src);
518 EXPORT_SYMBOL_GPL(skb_morph);
521 * skb_clone - duplicate an sk_buff
522 * @skb: buffer to clone
523 * @gfp_mask: allocation priority
525 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
526 * copies share the same packet data but not structure. The new
527 * buffer has a reference count of 1. If the allocation fails the
528 * function returns %NULL otherwise the new buffer is returned.
530 * If this function is called from an interrupt gfp_mask() must be
531 * %GFP_ATOMIC.
534 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
536 struct sk_buff *n;
538 n = skb + 1;
539 if (skb->fclone == SKB_FCLONE_ORIG &&
540 n->fclone == SKB_FCLONE_UNAVAILABLE) {
541 atomic_t *fclone_ref = (atomic_t *) (n + 1);
542 n->fclone = SKB_FCLONE_CLONE;
543 atomic_inc(fclone_ref);
544 } else {
545 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
546 if (!n)
547 return NULL;
548 n->fclone = SKB_FCLONE_UNAVAILABLE;
551 return __skb_clone(n, skb);
554 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
556 #ifndef NET_SKBUFF_DATA_USES_OFFSET
558 * Shift between the two data areas in bytes
560 unsigned long offset = new->data - old->data;
561 #endif
563 __copy_skb_header(new, old);
565 #ifndef NET_SKBUFF_DATA_USES_OFFSET
566 /* {transport,network,mac}_header are relative to skb->head */
567 new->transport_header += offset;
568 new->network_header += offset;
569 if (skb_mac_header_was_set(new))
570 new->mac_header += offset;
571 #endif
572 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
573 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
574 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
578 * skb_copy - create private copy of an sk_buff
579 * @skb: buffer to copy
580 * @gfp_mask: allocation priority
582 * Make a copy of both an &sk_buff and its data. This is used when the
583 * caller wishes to modify the data and needs a private copy of the
584 * data to alter. Returns %NULL on failure or the pointer to the buffer
585 * on success. The returned buffer has a reference count of 1.
587 * As by-product this function converts non-linear &sk_buff to linear
588 * one, so that &sk_buff becomes completely private and caller is allowed
589 * to modify all the data of returned buffer. This means that this
590 * function is not recommended for use in circumstances when only
591 * header is going to be modified. Use pskb_copy() instead.
594 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
596 int headerlen = skb->data - skb->head;
598 * Allocate the copy buffer
600 struct sk_buff *n;
601 #ifdef NET_SKBUFF_DATA_USES_OFFSET
602 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
603 #else
604 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
605 #endif
606 if (!n)
607 return NULL;
609 /* Set the data pointer */
610 skb_reserve(n, headerlen);
611 /* Set the tail pointer and length */
612 skb_put(n, skb->len);
614 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
615 BUG();
617 copy_skb_header(n, skb);
618 return n;
623 * pskb_copy - create copy of an sk_buff with private head.
624 * @skb: buffer to copy
625 * @gfp_mask: allocation priority
627 * Make a copy of both an &sk_buff and part of its data, located
628 * in header. Fragmented data remain shared. This is used when
629 * the caller wishes to modify only header of &sk_buff and needs
630 * private copy of the header to alter. Returns %NULL on failure
631 * or the pointer to the buffer on success.
632 * The returned buffer has a reference count of 1.
635 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
638 * Allocate the copy buffer
640 struct sk_buff *n;
641 #ifdef NET_SKBUFF_DATA_USES_OFFSET
642 n = alloc_skb(skb->end, gfp_mask);
643 #else
644 n = alloc_skb(skb->end - skb->head, gfp_mask);
645 #endif
646 if (!n)
647 goto out;
649 /* Set the data pointer */
650 skb_reserve(n, skb->data - skb->head);
651 /* Set the tail pointer and length */
652 skb_put(n, skb_headlen(skb));
653 /* Copy the bytes */
654 skb_copy_from_linear_data(skb, n->data, n->len);
656 n->truesize += skb->data_len;
657 n->data_len = skb->data_len;
658 n->len = skb->len;
660 if (skb_shinfo(skb)->nr_frags) {
661 int i;
663 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
664 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
665 get_page(skb_shinfo(n)->frags[i].page);
667 skb_shinfo(n)->nr_frags = i;
670 if (skb_shinfo(skb)->frag_list) {
671 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
672 skb_clone_fraglist(n);
675 copy_skb_header(n, skb);
676 out:
677 return n;
681 * pskb_expand_head - reallocate header of &sk_buff
682 * @skb: buffer to reallocate
683 * @nhead: room to add at head
684 * @ntail: room to add at tail
685 * @gfp_mask: allocation priority
687 * Expands (or creates identical copy, if &nhead and &ntail are zero)
688 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
689 * reference count of 1. Returns zero in the case of success or error,
690 * if expansion failed. In the last case, &sk_buff is not changed.
692 * All the pointers pointing into skb header may change and must be
693 * reloaded after call to this function.
696 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
697 gfp_t gfp_mask)
699 int i;
700 u8 *data;
701 #ifdef NET_SKBUFF_DATA_USES_OFFSET
702 int size = nhead + skb->end + ntail;
703 #else
704 int size = nhead + (skb->end - skb->head) + ntail;
705 #endif
706 long off;
708 BUG_ON(nhead < 0);
710 if (skb_shared(skb))
711 BUG();
713 size = SKB_DATA_ALIGN(size);
715 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
716 if (!data)
717 goto nodata;
719 /* Copy only real data... and, alas, header. This should be
720 * optimized for the cases when header is void. */
721 #ifdef NET_SKBUFF_DATA_USES_OFFSET
722 memcpy(data + nhead, skb->head, skb->tail);
723 #else
724 memcpy(data + nhead, skb->head, skb->tail - skb->head);
725 #endif
726 memcpy(data + size, skb_end_pointer(skb),
727 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
729 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
730 get_page(skb_shinfo(skb)->frags[i].page);
732 if (skb_shinfo(skb)->frag_list)
733 skb_clone_fraglist(skb);
735 skb_release_data(skb);
737 off = (data + nhead) - skb->head;
739 skb->head = data;
740 skb->data += off;
741 #ifdef NET_SKBUFF_DATA_USES_OFFSET
742 skb->end = size;
743 off = nhead;
744 #else
745 skb->end = skb->head + size;
746 #endif
747 /* {transport,network,mac}_header and tail are relative to skb->head */
748 skb->tail += off;
749 skb->transport_header += off;
750 skb->network_header += off;
751 if (skb_mac_header_was_set(skb))
752 skb->mac_header += off;
753 /* Only adjust this if it actually is csum_start rather than csum */
754 if (skb->ip_summed == CHECKSUM_PARTIAL)
755 skb->csum_start += nhead;
756 skb->cloned = 0;
757 skb->hdr_len = 0;
758 skb->nohdr = 0;
759 atomic_set(&skb_shinfo(skb)->dataref, 1);
760 return 0;
762 nodata:
763 return -ENOMEM;
766 /* Make private copy of skb with writable head and some headroom */
768 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
770 struct sk_buff *skb2;
771 int delta = headroom - skb_headroom(skb);
773 if (delta <= 0)
774 skb2 = pskb_copy(skb, GFP_ATOMIC);
775 else {
776 skb2 = skb_clone(skb, GFP_ATOMIC);
777 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
778 GFP_ATOMIC)) {
779 kfree_skb(skb2);
780 skb2 = NULL;
783 return skb2;
788 * skb_copy_expand - copy and expand sk_buff
789 * @skb: buffer to copy
790 * @newheadroom: new free bytes at head
791 * @newtailroom: new free bytes at tail
792 * @gfp_mask: allocation priority
794 * Make a copy of both an &sk_buff and its data and while doing so
795 * allocate additional space.
797 * This is used when the caller wishes to modify the data and needs a
798 * private copy of the data to alter as well as more space for new fields.
799 * Returns %NULL on failure or the pointer to the buffer
800 * on success. The returned buffer has a reference count of 1.
802 * You must pass %GFP_ATOMIC as the allocation priority if this function
803 * is called from an interrupt.
805 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
806 int newheadroom, int newtailroom,
807 gfp_t gfp_mask)
810 * Allocate the copy buffer
812 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
813 gfp_mask);
814 int oldheadroom = skb_headroom(skb);
815 int head_copy_len, head_copy_off;
816 int off;
818 if (!n)
819 return NULL;
821 skb_reserve(n, newheadroom);
823 /* Set the tail pointer and length */
824 skb_put(n, skb->len);
826 head_copy_len = oldheadroom;
827 head_copy_off = 0;
828 if (newheadroom <= head_copy_len)
829 head_copy_len = newheadroom;
830 else
831 head_copy_off = newheadroom - head_copy_len;
833 /* Copy the linear header and data. */
834 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
835 skb->len + head_copy_len))
836 BUG();
838 copy_skb_header(n, skb);
840 off = newheadroom - oldheadroom;
841 if (n->ip_summed == CHECKSUM_PARTIAL)
842 n->csum_start += off;
843 #ifdef NET_SKBUFF_DATA_USES_OFFSET
844 n->transport_header += off;
845 n->network_header += off;
846 if (skb_mac_header_was_set(skb))
847 n->mac_header += off;
848 #endif
850 return n;
854 * skb_pad - zero pad the tail of an skb
855 * @skb: buffer to pad
856 * @pad: space to pad
858 * Ensure that a buffer is followed by a padding area that is zero
859 * filled. Used by network drivers which may DMA or transfer data
860 * beyond the buffer end onto the wire.
862 * May return error in out of memory cases. The skb is freed on error.
865 int skb_pad(struct sk_buff *skb, int pad)
867 int err;
868 int ntail;
870 /* If the skbuff is non linear tailroom is always zero.. */
871 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
872 memset(skb->data+skb->len, 0, pad);
873 return 0;
876 ntail = skb->data_len + pad - (skb->end - skb->tail);
877 if (likely(skb_cloned(skb) || ntail > 0)) {
878 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
879 if (unlikely(err))
880 goto free_skb;
883 /* FIXME: The use of this function with non-linear skb's really needs
884 * to be audited.
886 err = skb_linearize(skb);
887 if (unlikely(err))
888 goto free_skb;
890 memset(skb->data + skb->len, 0, pad);
891 return 0;
893 free_skb:
894 kfree_skb(skb);
895 return err;
898 /* Trims skb to length len. It can change skb pointers.
901 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
903 struct sk_buff **fragp;
904 struct sk_buff *frag;
905 int offset = skb_headlen(skb);
906 int nfrags = skb_shinfo(skb)->nr_frags;
907 int i;
908 int err;
910 if (skb_cloned(skb) &&
911 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
912 return err;
914 i = 0;
915 if (offset >= len)
916 goto drop_pages;
918 for (; i < nfrags; i++) {
919 int end = offset + skb_shinfo(skb)->frags[i].size;
921 if (end < len) {
922 offset = end;
923 continue;
926 skb_shinfo(skb)->frags[i++].size = len - offset;
928 drop_pages:
929 skb_shinfo(skb)->nr_frags = i;
931 for (; i < nfrags; i++)
932 put_page(skb_shinfo(skb)->frags[i].page);
934 if (skb_shinfo(skb)->frag_list)
935 skb_drop_fraglist(skb);
936 goto done;
939 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
940 fragp = &frag->next) {
941 int end = offset + frag->len;
943 if (skb_shared(frag)) {
944 struct sk_buff *nfrag;
946 nfrag = skb_clone(frag, GFP_ATOMIC);
947 if (unlikely(!nfrag))
948 return -ENOMEM;
950 nfrag->next = frag->next;
951 kfree_skb(frag);
952 frag = nfrag;
953 *fragp = frag;
956 if (end < len) {
957 offset = end;
958 continue;
961 if (end > len &&
962 unlikely((err = pskb_trim(frag, len - offset))))
963 return err;
965 if (frag->next)
966 skb_drop_list(&frag->next);
967 break;
970 done:
971 if (len > skb_headlen(skb)) {
972 skb->data_len -= skb->len - len;
973 skb->len = len;
974 } else {
975 skb->len = len;
976 skb->data_len = 0;
977 skb_set_tail_pointer(skb, len);
980 return 0;
984 * __pskb_pull_tail - advance tail of skb header
985 * @skb: buffer to reallocate
986 * @delta: number of bytes to advance tail
988 * The function makes a sense only on a fragmented &sk_buff,
989 * it expands header moving its tail forward and copying necessary
990 * data from fragmented part.
992 * &sk_buff MUST have reference count of 1.
994 * Returns %NULL (and &sk_buff does not change) if pull failed
995 * or value of new tail of skb in the case of success.
997 * All the pointers pointing into skb header may change and must be
998 * reloaded after call to this function.
1001 /* Moves tail of skb head forward, copying data from fragmented part,
1002 * when it is necessary.
1003 * 1. It may fail due to malloc failure.
1004 * 2. It may change skb pointers.
1006 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1008 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1010 /* If skb has not enough free space at tail, get new one
1011 * plus 128 bytes for future expansions. If we have enough
1012 * room at tail, reallocate without expansion only if skb is cloned.
1014 int i, k, eat = (skb->tail + delta) - skb->end;
1016 if (eat > 0 || skb_cloned(skb)) {
1017 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1018 GFP_ATOMIC))
1019 return NULL;
1022 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1023 BUG();
1025 /* Optimization: no fragments, no reasons to preestimate
1026 * size of pulled pages. Superb.
1028 if (!skb_shinfo(skb)->frag_list)
1029 goto pull_pages;
1031 /* Estimate size of pulled pages. */
1032 eat = delta;
1033 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1034 if (skb_shinfo(skb)->frags[i].size >= eat)
1035 goto pull_pages;
1036 eat -= skb_shinfo(skb)->frags[i].size;
1039 /* If we need update frag list, we are in troubles.
1040 * Certainly, it possible to add an offset to skb data,
1041 * but taking into account that pulling is expected to
1042 * be very rare operation, it is worth to fight against
1043 * further bloating skb head and crucify ourselves here instead.
1044 * Pure masohism, indeed. 8)8)
1046 if (eat) {
1047 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1048 struct sk_buff *clone = NULL;
1049 struct sk_buff *insp = NULL;
1051 do {
1052 BUG_ON(!list);
1054 if (list->len <= eat) {
1055 /* Eaten as whole. */
1056 eat -= list->len;
1057 list = list->next;
1058 insp = list;
1059 } else {
1060 /* Eaten partially. */
1062 if (skb_shared(list)) {
1063 /* Sucks! We need to fork list. :-( */
1064 clone = skb_clone(list, GFP_ATOMIC);
1065 if (!clone)
1066 return NULL;
1067 insp = list->next;
1068 list = clone;
1069 } else {
1070 /* This may be pulled without
1071 * problems. */
1072 insp = list;
1074 if (!pskb_pull(list, eat)) {
1075 if (clone)
1076 kfree_skb(clone);
1077 return NULL;
1079 break;
1081 } while (eat);
1083 /* Free pulled out fragments. */
1084 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1085 skb_shinfo(skb)->frag_list = list->next;
1086 kfree_skb(list);
1088 /* And insert new clone at head. */
1089 if (clone) {
1090 clone->next = list;
1091 skb_shinfo(skb)->frag_list = clone;
1094 /* Success! Now we may commit changes to skb data. */
1096 pull_pages:
1097 eat = delta;
1098 k = 0;
1099 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1100 if (skb_shinfo(skb)->frags[i].size <= eat) {
1101 put_page(skb_shinfo(skb)->frags[i].page);
1102 eat -= skb_shinfo(skb)->frags[i].size;
1103 } else {
1104 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1105 if (eat) {
1106 skb_shinfo(skb)->frags[k].page_offset += eat;
1107 skb_shinfo(skb)->frags[k].size -= eat;
1108 eat = 0;
1110 k++;
1113 skb_shinfo(skb)->nr_frags = k;
1115 skb->tail += delta;
1116 skb->data_len -= delta;
1118 return skb_tail_pointer(skb);
1121 /* Copy some data bits from skb to kernel buffer. */
1123 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1125 int i, copy;
1126 int start = skb_headlen(skb);
1128 if (offset > (int)skb->len - len)
1129 goto fault;
1131 /* Copy header. */
1132 if ((copy = start - offset) > 0) {
1133 if (copy > len)
1134 copy = len;
1135 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1136 if ((len -= copy) == 0)
1137 return 0;
1138 offset += copy;
1139 to += copy;
1142 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1143 int end;
1145 BUG_TRAP(start <= offset + len);
1147 end = start + skb_shinfo(skb)->frags[i].size;
1148 if ((copy = end - offset) > 0) {
1149 u8 *vaddr;
1151 if (copy > len)
1152 copy = len;
1154 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1155 memcpy(to,
1156 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1157 offset - start, copy);
1158 kunmap_skb_frag(vaddr);
1160 if ((len -= copy) == 0)
1161 return 0;
1162 offset += copy;
1163 to += copy;
1165 start = end;
1168 if (skb_shinfo(skb)->frag_list) {
1169 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1171 for (; list; list = list->next) {
1172 int end;
1174 BUG_TRAP(start <= offset + len);
1176 end = start + list->len;
1177 if ((copy = end - offset) > 0) {
1178 if (copy > len)
1179 copy = len;
1180 if (skb_copy_bits(list, offset - start,
1181 to, copy))
1182 goto fault;
1183 if ((len -= copy) == 0)
1184 return 0;
1185 offset += copy;
1186 to += copy;
1188 start = end;
1191 if (!len)
1192 return 0;
1194 fault:
1195 return -EFAULT;
1199 * Callback from splice_to_pipe(), if we need to release some pages
1200 * at the end of the spd in case we error'ed out in filling the pipe.
1202 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1204 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1206 kfree_skb(skb);
1210 * Fill page/offset/length into spd, if it can hold more pages.
1212 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1213 unsigned int len, unsigned int offset,
1214 struct sk_buff *skb)
1216 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1217 return 1;
1219 spd->pages[spd->nr_pages] = page;
1220 spd->partial[spd->nr_pages].len = len;
1221 spd->partial[spd->nr_pages].offset = offset;
1222 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1223 spd->nr_pages++;
1224 return 0;
1228 * Map linear and fragment data from the skb to spd. Returns number of
1229 * pages mapped.
1231 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1232 unsigned int *total_len,
1233 struct splice_pipe_desc *spd)
1235 unsigned int nr_pages = spd->nr_pages;
1236 unsigned int poff, plen, len, toff, tlen;
1237 int headlen, seg, error = 0;
1239 toff = *offset;
1240 tlen = *total_len;
1241 if (!tlen) {
1242 error = 1;
1243 goto err;
1247 * if the offset is greater than the linear part, go directly to
1248 * the fragments.
1250 headlen = skb_headlen(skb);
1251 if (toff >= headlen) {
1252 toff -= headlen;
1253 goto map_frag;
1257 * first map the linear region into the pages/partial map, skipping
1258 * any potential initial offset.
1260 len = 0;
1261 while (len < headlen) {
1262 void *p = skb->data + len;
1264 poff = (unsigned long) p & (PAGE_SIZE - 1);
1265 plen = min_t(unsigned int, headlen - len, PAGE_SIZE - poff);
1266 len += plen;
1268 if (toff) {
1269 if (plen <= toff) {
1270 toff -= plen;
1271 continue;
1273 plen -= toff;
1274 poff += toff;
1275 toff = 0;
1278 plen = min(plen, tlen);
1279 if (!plen)
1280 break;
1283 * just jump directly to update and return, no point
1284 * in going over fragments when the output is full.
1286 error = spd_fill_page(spd, virt_to_page(p), plen, poff, skb);
1287 if (error)
1288 goto done;
1290 tlen -= plen;
1294 * then map the fragments
1296 map_frag:
1297 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1298 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1300 plen = f->size;
1301 poff = f->page_offset;
1303 if (toff) {
1304 if (plen <= toff) {
1305 toff -= plen;
1306 continue;
1308 plen -= toff;
1309 poff += toff;
1310 toff = 0;
1313 plen = min(plen, tlen);
1314 if (!plen)
1315 break;
1317 error = spd_fill_page(spd, f->page, plen, poff, skb);
1318 if (error)
1319 break;
1321 tlen -= plen;
1324 done:
1325 if (spd->nr_pages - nr_pages) {
1326 *offset = 0;
1327 *total_len = tlen;
1328 return 0;
1330 err:
1331 /* update the offset to reflect the linear part skip, if any */
1332 if (!error)
1333 *offset = toff;
1334 return error;
1338 * Map data from the skb to a pipe. Should handle both the linear part,
1339 * the fragments, and the frag list. It does NOT handle frag lists within
1340 * the frag list, if such a thing exists. We'd probably need to recurse to
1341 * handle that cleanly.
1343 int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1344 struct pipe_inode_info *pipe, unsigned int tlen,
1345 unsigned int flags)
1347 struct partial_page partial[PIPE_BUFFERS];
1348 struct page *pages[PIPE_BUFFERS];
1349 struct splice_pipe_desc spd = {
1350 .pages = pages,
1351 .partial = partial,
1352 .flags = flags,
1353 .ops = &sock_pipe_buf_ops,
1354 .spd_release = sock_spd_release,
1356 struct sk_buff *skb;
1359 * I'd love to avoid the clone here, but tcp_read_sock()
1360 * ignores reference counts and unconditonally kills the sk_buff
1361 * on return from the actor.
1363 skb = skb_clone(__skb, GFP_KERNEL);
1364 if (unlikely(!skb))
1365 return -ENOMEM;
1368 * __skb_splice_bits() only fails if the output has no room left,
1369 * so no point in going over the frag_list for the error case.
1371 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1372 goto done;
1373 else if (!tlen)
1374 goto done;
1377 * now see if we have a frag_list to map
1379 if (skb_shinfo(skb)->frag_list) {
1380 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1382 for (; list && tlen; list = list->next) {
1383 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1384 break;
1388 done:
1390 * drop our reference to the clone, the pipe consumption will
1391 * drop the rest.
1393 kfree_skb(skb);
1395 if (spd.nr_pages) {
1396 int ret;
1397 struct sock *sk = __skb->sk;
1400 * Drop the socket lock, otherwise we have reverse
1401 * locking dependencies between sk_lock and i_mutex
1402 * here as compared to sendfile(). We enter here
1403 * with the socket lock held, and splice_to_pipe() will
1404 * grab the pipe inode lock. For sendfile() emulation,
1405 * we call into ->sendpage() with the i_mutex lock held
1406 * and networking will grab the socket lock.
1408 release_sock(sk);
1409 ret = splice_to_pipe(pipe, &spd);
1410 lock_sock(sk);
1411 return ret;
1414 return 0;
1418 * skb_store_bits - store bits from kernel buffer to skb
1419 * @skb: destination buffer
1420 * @offset: offset in destination
1421 * @from: source buffer
1422 * @len: number of bytes to copy
1424 * Copy the specified number of bytes from the source buffer to the
1425 * destination skb. This function handles all the messy bits of
1426 * traversing fragment lists and such.
1429 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1431 int i, copy;
1432 int start = skb_headlen(skb);
1434 if (offset > (int)skb->len - len)
1435 goto fault;
1437 if ((copy = start - offset) > 0) {
1438 if (copy > len)
1439 copy = len;
1440 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1441 if ((len -= copy) == 0)
1442 return 0;
1443 offset += copy;
1444 from += copy;
1447 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1448 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1449 int end;
1451 BUG_TRAP(start <= offset + len);
1453 end = start + frag->size;
1454 if ((copy = end - offset) > 0) {
1455 u8 *vaddr;
1457 if (copy > len)
1458 copy = len;
1460 vaddr = kmap_skb_frag(frag);
1461 memcpy(vaddr + frag->page_offset + offset - start,
1462 from, copy);
1463 kunmap_skb_frag(vaddr);
1465 if ((len -= copy) == 0)
1466 return 0;
1467 offset += copy;
1468 from += copy;
1470 start = end;
1473 if (skb_shinfo(skb)->frag_list) {
1474 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1476 for (; list; list = list->next) {
1477 int end;
1479 BUG_TRAP(start <= offset + len);
1481 end = start + list->len;
1482 if ((copy = end - offset) > 0) {
1483 if (copy > len)
1484 copy = len;
1485 if (skb_store_bits(list, offset - start,
1486 from, copy))
1487 goto fault;
1488 if ((len -= copy) == 0)
1489 return 0;
1490 offset += copy;
1491 from += copy;
1493 start = end;
1496 if (!len)
1497 return 0;
1499 fault:
1500 return -EFAULT;
1503 EXPORT_SYMBOL(skb_store_bits);
1505 /* Checksum skb data. */
1507 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1508 int len, __wsum csum)
1510 int start = skb_headlen(skb);
1511 int i, copy = start - offset;
1512 int pos = 0;
1514 /* Checksum header. */
1515 if (copy > 0) {
1516 if (copy > len)
1517 copy = len;
1518 csum = csum_partial(skb->data + offset, copy, csum);
1519 if ((len -= copy) == 0)
1520 return csum;
1521 offset += copy;
1522 pos = copy;
1525 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1526 int end;
1528 BUG_TRAP(start <= offset + len);
1530 end = start + skb_shinfo(skb)->frags[i].size;
1531 if ((copy = end - offset) > 0) {
1532 __wsum csum2;
1533 u8 *vaddr;
1534 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1536 if (copy > len)
1537 copy = len;
1538 vaddr = kmap_skb_frag(frag);
1539 csum2 = csum_partial(vaddr + frag->page_offset +
1540 offset - start, copy, 0);
1541 kunmap_skb_frag(vaddr);
1542 csum = csum_block_add(csum, csum2, pos);
1543 if (!(len -= copy))
1544 return csum;
1545 offset += copy;
1546 pos += copy;
1548 start = end;
1551 if (skb_shinfo(skb)->frag_list) {
1552 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1554 for (; list; list = list->next) {
1555 int end;
1557 BUG_TRAP(start <= offset + len);
1559 end = start + list->len;
1560 if ((copy = end - offset) > 0) {
1561 __wsum csum2;
1562 if (copy > len)
1563 copy = len;
1564 csum2 = skb_checksum(list, offset - start,
1565 copy, 0);
1566 csum = csum_block_add(csum, csum2, pos);
1567 if ((len -= copy) == 0)
1568 return csum;
1569 offset += copy;
1570 pos += copy;
1572 start = end;
1575 BUG_ON(len);
1577 return csum;
1580 /* Both of above in one bottle. */
1582 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1583 u8 *to, int len, __wsum csum)
1585 int start = skb_headlen(skb);
1586 int i, copy = start - offset;
1587 int pos = 0;
1589 /* Copy header. */
1590 if (copy > 0) {
1591 if (copy > len)
1592 copy = len;
1593 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1594 copy, csum);
1595 if ((len -= copy) == 0)
1596 return csum;
1597 offset += copy;
1598 to += copy;
1599 pos = copy;
1602 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1603 int end;
1605 BUG_TRAP(start <= offset + len);
1607 end = start + skb_shinfo(skb)->frags[i].size;
1608 if ((copy = end - offset) > 0) {
1609 __wsum csum2;
1610 u8 *vaddr;
1611 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1613 if (copy > len)
1614 copy = len;
1615 vaddr = kmap_skb_frag(frag);
1616 csum2 = csum_partial_copy_nocheck(vaddr +
1617 frag->page_offset +
1618 offset - start, to,
1619 copy, 0);
1620 kunmap_skb_frag(vaddr);
1621 csum = csum_block_add(csum, csum2, pos);
1622 if (!(len -= copy))
1623 return csum;
1624 offset += copy;
1625 to += copy;
1626 pos += copy;
1628 start = end;
1631 if (skb_shinfo(skb)->frag_list) {
1632 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1634 for (; list; list = list->next) {
1635 __wsum csum2;
1636 int end;
1638 BUG_TRAP(start <= offset + len);
1640 end = start + list->len;
1641 if ((copy = end - offset) > 0) {
1642 if (copy > len)
1643 copy = len;
1644 csum2 = skb_copy_and_csum_bits(list,
1645 offset - start,
1646 to, copy, 0);
1647 csum = csum_block_add(csum, csum2, pos);
1648 if ((len -= copy) == 0)
1649 return csum;
1650 offset += copy;
1651 to += copy;
1652 pos += copy;
1654 start = end;
1657 BUG_ON(len);
1658 return csum;
1661 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1663 __wsum csum;
1664 long csstart;
1666 if (skb->ip_summed == CHECKSUM_PARTIAL)
1667 csstart = skb->csum_start - skb_headroom(skb);
1668 else
1669 csstart = skb_headlen(skb);
1671 BUG_ON(csstart > skb_headlen(skb));
1673 skb_copy_from_linear_data(skb, to, csstart);
1675 csum = 0;
1676 if (csstart != skb->len)
1677 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1678 skb->len - csstart, 0);
1680 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1681 long csstuff = csstart + skb->csum_offset;
1683 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1688 * skb_dequeue - remove from the head of the queue
1689 * @list: list to dequeue from
1691 * Remove the head of the list. The list lock is taken so the function
1692 * may be used safely with other locking list functions. The head item is
1693 * returned or %NULL if the list is empty.
1696 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1698 unsigned long flags;
1699 struct sk_buff *result;
1701 spin_lock_irqsave(&list->lock, flags);
1702 result = __skb_dequeue(list);
1703 spin_unlock_irqrestore(&list->lock, flags);
1704 return result;
1708 * skb_dequeue_tail - remove from the tail of the queue
1709 * @list: list to dequeue from
1711 * Remove the tail of the list. The list lock is taken so the function
1712 * may be used safely with other locking list functions. The tail item is
1713 * returned or %NULL if the list is empty.
1715 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1717 unsigned long flags;
1718 struct sk_buff *result;
1720 spin_lock_irqsave(&list->lock, flags);
1721 result = __skb_dequeue_tail(list);
1722 spin_unlock_irqrestore(&list->lock, flags);
1723 return result;
1727 * skb_queue_purge - empty a list
1728 * @list: list to empty
1730 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1731 * the list and one reference dropped. This function takes the list
1732 * lock and is atomic with respect to other list locking functions.
1734 void skb_queue_purge(struct sk_buff_head *list)
1736 struct sk_buff *skb;
1737 while ((skb = skb_dequeue(list)) != NULL)
1738 kfree_skb(skb);
1742 * skb_queue_head - queue a buffer at the list head
1743 * @list: list to use
1744 * @newsk: buffer to queue
1746 * Queue a buffer at the start of the list. This function takes the
1747 * list lock and can be used safely with other locking &sk_buff functions
1748 * safely.
1750 * A buffer cannot be placed on two lists at the same time.
1752 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1754 unsigned long flags;
1756 spin_lock_irqsave(&list->lock, flags);
1757 __skb_queue_head(list, newsk);
1758 spin_unlock_irqrestore(&list->lock, flags);
1762 * skb_queue_tail - queue a buffer at the list tail
1763 * @list: list to use
1764 * @newsk: buffer to queue
1766 * Queue a buffer at the tail of the list. This function takes the
1767 * list lock and can be used safely with other locking &sk_buff functions
1768 * safely.
1770 * A buffer cannot be placed on two lists at the same time.
1772 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1774 unsigned long flags;
1776 spin_lock_irqsave(&list->lock, flags);
1777 __skb_queue_tail(list, newsk);
1778 spin_unlock_irqrestore(&list->lock, flags);
1782 * skb_unlink - remove a buffer from a list
1783 * @skb: buffer to remove
1784 * @list: list to use
1786 * Remove a packet from a list. The list locks are taken and this
1787 * function is atomic with respect to other list locked calls
1789 * You must know what list the SKB is on.
1791 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1793 unsigned long flags;
1795 spin_lock_irqsave(&list->lock, flags);
1796 __skb_unlink(skb, list);
1797 spin_unlock_irqrestore(&list->lock, flags);
1801 * skb_append - append a buffer
1802 * @old: buffer to insert after
1803 * @newsk: buffer to insert
1804 * @list: list to use
1806 * Place a packet after a given packet in a list. The list locks are taken
1807 * and this function is atomic with respect to other list locked calls.
1808 * A buffer cannot be placed on two lists at the same time.
1810 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1812 unsigned long flags;
1814 spin_lock_irqsave(&list->lock, flags);
1815 __skb_append(old, newsk, list);
1816 spin_unlock_irqrestore(&list->lock, flags);
1821 * skb_insert - insert a buffer
1822 * @old: buffer to insert before
1823 * @newsk: buffer to insert
1824 * @list: list to use
1826 * Place a packet before a given packet in a list. The list locks are
1827 * taken and this function is atomic with respect to other list locked
1828 * calls.
1830 * A buffer cannot be placed on two lists at the same time.
1832 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1834 unsigned long flags;
1836 spin_lock_irqsave(&list->lock, flags);
1837 __skb_insert(newsk, old->prev, old, list);
1838 spin_unlock_irqrestore(&list->lock, flags);
1841 static inline void skb_split_inside_header(struct sk_buff *skb,
1842 struct sk_buff* skb1,
1843 const u32 len, const int pos)
1845 int i;
1847 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1848 pos - len);
1849 /* And move data appendix as is. */
1850 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1851 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1853 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1854 skb_shinfo(skb)->nr_frags = 0;
1855 skb1->data_len = skb->data_len;
1856 skb1->len += skb1->data_len;
1857 skb->data_len = 0;
1858 skb->len = len;
1859 skb_set_tail_pointer(skb, len);
1862 static inline void skb_split_no_header(struct sk_buff *skb,
1863 struct sk_buff* skb1,
1864 const u32 len, int pos)
1866 int i, k = 0;
1867 const int nfrags = skb_shinfo(skb)->nr_frags;
1869 skb_shinfo(skb)->nr_frags = 0;
1870 skb1->len = skb1->data_len = skb->len - len;
1871 skb->len = len;
1872 skb->data_len = len - pos;
1874 for (i = 0; i < nfrags; i++) {
1875 int size = skb_shinfo(skb)->frags[i].size;
1877 if (pos + size > len) {
1878 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1880 if (pos < len) {
1881 /* Split frag.
1882 * We have two variants in this case:
1883 * 1. Move all the frag to the second
1884 * part, if it is possible. F.e.
1885 * this approach is mandatory for TUX,
1886 * where splitting is expensive.
1887 * 2. Split is accurately. We make this.
1889 get_page(skb_shinfo(skb)->frags[i].page);
1890 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1891 skb_shinfo(skb1)->frags[0].size -= len - pos;
1892 skb_shinfo(skb)->frags[i].size = len - pos;
1893 skb_shinfo(skb)->nr_frags++;
1895 k++;
1896 } else
1897 skb_shinfo(skb)->nr_frags++;
1898 pos += size;
1900 skb_shinfo(skb1)->nr_frags = k;
1904 * skb_split - Split fragmented skb to two parts at length len.
1905 * @skb: the buffer to split
1906 * @skb1: the buffer to receive the second part
1907 * @len: new length for skb
1909 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1911 int pos = skb_headlen(skb);
1913 if (len < pos) /* Split line is inside header. */
1914 skb_split_inside_header(skb, skb1, len, pos);
1915 else /* Second chunk has no header, nothing to copy. */
1916 skb_split_no_header(skb, skb1, len, pos);
1920 * skb_prepare_seq_read - Prepare a sequential read of skb data
1921 * @skb: the buffer to read
1922 * @from: lower offset of data to be read
1923 * @to: upper offset of data to be read
1924 * @st: state variable
1926 * Initializes the specified state variable. Must be called before
1927 * invoking skb_seq_read() for the first time.
1929 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1930 unsigned int to, struct skb_seq_state *st)
1932 st->lower_offset = from;
1933 st->upper_offset = to;
1934 st->root_skb = st->cur_skb = skb;
1935 st->frag_idx = st->stepped_offset = 0;
1936 st->frag_data = NULL;
1940 * skb_seq_read - Sequentially read skb data
1941 * @consumed: number of bytes consumed by the caller so far
1942 * @data: destination pointer for data to be returned
1943 * @st: state variable
1945 * Reads a block of skb data at &consumed relative to the
1946 * lower offset specified to skb_prepare_seq_read(). Assigns
1947 * the head of the data block to &data and returns the length
1948 * of the block or 0 if the end of the skb data or the upper
1949 * offset has been reached.
1951 * The caller is not required to consume all of the data
1952 * returned, i.e. &consumed is typically set to the number
1953 * of bytes already consumed and the next call to
1954 * skb_seq_read() will return the remaining part of the block.
1956 * Note: The size of each block of data returned can be arbitary,
1957 * this limitation is the cost for zerocopy seqeuental
1958 * reads of potentially non linear data.
1960 * Note: Fragment lists within fragments are not implemented
1961 * at the moment, state->root_skb could be replaced with
1962 * a stack for this purpose.
1964 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1965 struct skb_seq_state *st)
1967 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1968 skb_frag_t *frag;
1970 if (unlikely(abs_offset >= st->upper_offset))
1971 return 0;
1973 next_skb:
1974 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
1976 if (abs_offset < block_limit && !st->frag_data) {
1977 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
1978 return block_limit - abs_offset;
1981 if (st->frag_idx == 0 && !st->frag_data)
1982 st->stepped_offset += skb_headlen(st->cur_skb);
1984 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1985 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1986 block_limit = frag->size + st->stepped_offset;
1988 if (abs_offset < block_limit) {
1989 if (!st->frag_data)
1990 st->frag_data = kmap_skb_frag(frag);
1992 *data = (u8 *) st->frag_data + frag->page_offset +
1993 (abs_offset - st->stepped_offset);
1995 return block_limit - abs_offset;
1998 if (st->frag_data) {
1999 kunmap_skb_frag(st->frag_data);
2000 st->frag_data = NULL;
2003 st->frag_idx++;
2004 st->stepped_offset += frag->size;
2007 if (st->frag_data) {
2008 kunmap_skb_frag(st->frag_data);
2009 st->frag_data = NULL;
2012 if (st->root_skb == st->cur_skb &&
2013 skb_shinfo(st->root_skb)->frag_list) {
2014 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2015 st->frag_idx = 0;
2016 goto next_skb;
2017 } else if (st->cur_skb->next) {
2018 st->cur_skb = st->cur_skb->next;
2019 st->frag_idx = 0;
2020 goto next_skb;
2023 return 0;
2027 * skb_abort_seq_read - Abort a sequential read of skb data
2028 * @st: state variable
2030 * Must be called if skb_seq_read() was not called until it
2031 * returned 0.
2033 void skb_abort_seq_read(struct skb_seq_state *st)
2035 if (st->frag_data)
2036 kunmap_skb_frag(st->frag_data);
2039 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2041 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2042 struct ts_config *conf,
2043 struct ts_state *state)
2045 return skb_seq_read(offset, text, TS_SKB_CB(state));
2048 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2050 skb_abort_seq_read(TS_SKB_CB(state));
2054 * skb_find_text - Find a text pattern in skb data
2055 * @skb: the buffer to look in
2056 * @from: search offset
2057 * @to: search limit
2058 * @config: textsearch configuration
2059 * @state: uninitialized textsearch state variable
2061 * Finds a pattern in the skb data according to the specified
2062 * textsearch configuration. Use textsearch_next() to retrieve
2063 * subsequent occurrences of the pattern. Returns the offset
2064 * to the first occurrence or UINT_MAX if no match was found.
2066 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2067 unsigned int to, struct ts_config *config,
2068 struct ts_state *state)
2070 unsigned int ret;
2072 config->get_next_block = skb_ts_get_next_block;
2073 config->finish = skb_ts_finish;
2075 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2077 ret = textsearch_find(config, state);
2078 return (ret <= to - from ? ret : UINT_MAX);
2082 * skb_append_datato_frags: - append the user data to a skb
2083 * @sk: sock structure
2084 * @skb: skb structure to be appened with user data.
2085 * @getfrag: call back function to be used for getting the user data
2086 * @from: pointer to user message iov
2087 * @length: length of the iov message
2089 * Description: This procedure append the user data in the fragment part
2090 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2092 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2093 int (*getfrag)(void *from, char *to, int offset,
2094 int len, int odd, struct sk_buff *skb),
2095 void *from, int length)
2097 int frg_cnt = 0;
2098 skb_frag_t *frag = NULL;
2099 struct page *page = NULL;
2100 int copy, left;
2101 int offset = 0;
2102 int ret;
2104 do {
2105 /* Return error if we don't have space for new frag */
2106 frg_cnt = skb_shinfo(skb)->nr_frags;
2107 if (frg_cnt >= MAX_SKB_FRAGS)
2108 return -EFAULT;
2110 /* allocate a new page for next frag */
2111 page = alloc_pages(sk->sk_allocation, 0);
2113 /* If alloc_page fails just return failure and caller will
2114 * free previous allocated pages by doing kfree_skb()
2116 if (page == NULL)
2117 return -ENOMEM;
2119 /* initialize the next frag */
2120 sk->sk_sndmsg_page = page;
2121 sk->sk_sndmsg_off = 0;
2122 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2123 skb->truesize += PAGE_SIZE;
2124 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2126 /* get the new initialized frag */
2127 frg_cnt = skb_shinfo(skb)->nr_frags;
2128 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2130 /* copy the user data to page */
2131 left = PAGE_SIZE - frag->page_offset;
2132 copy = (length > left)? left : length;
2134 ret = getfrag(from, (page_address(frag->page) +
2135 frag->page_offset + frag->size),
2136 offset, copy, 0, skb);
2137 if (ret < 0)
2138 return -EFAULT;
2140 /* copy was successful so update the size parameters */
2141 sk->sk_sndmsg_off += copy;
2142 frag->size += copy;
2143 skb->len += copy;
2144 skb->data_len += copy;
2145 offset += copy;
2146 length -= copy;
2148 } while (length > 0);
2150 return 0;
2154 * skb_pull_rcsum - pull skb and update receive checksum
2155 * @skb: buffer to update
2156 * @start: start of data before pull
2157 * @len: length of data pulled
2159 * This function performs an skb_pull on the packet and updates
2160 * update the CHECKSUM_COMPLETE checksum. It should be used on
2161 * receive path processing instead of skb_pull unless you know
2162 * that the checksum difference is zero (e.g., a valid IP header)
2163 * or you are setting ip_summed to CHECKSUM_NONE.
2165 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2167 BUG_ON(len > skb->len);
2168 skb->len -= len;
2169 BUG_ON(skb->len < skb->data_len);
2170 skb_postpull_rcsum(skb, skb->data, len);
2171 return skb->data += len;
2174 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2177 * skb_segment - Perform protocol segmentation on skb.
2178 * @skb: buffer to segment
2179 * @features: features for the output path (see dev->features)
2181 * This function performs segmentation on the given skb. It returns
2182 * the segment at the given position. It returns NULL if there are
2183 * no more segments to generate, or when an error is encountered.
2185 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2187 struct sk_buff *segs = NULL;
2188 struct sk_buff *tail = NULL;
2189 unsigned int mss = skb_shinfo(skb)->gso_size;
2190 unsigned int doffset = skb->data - skb_mac_header(skb);
2191 unsigned int offset = doffset;
2192 unsigned int headroom;
2193 unsigned int len;
2194 int sg = features & NETIF_F_SG;
2195 int nfrags = skb_shinfo(skb)->nr_frags;
2196 int err = -ENOMEM;
2197 int i = 0;
2198 int pos;
2200 __skb_push(skb, doffset);
2201 headroom = skb_headroom(skb);
2202 pos = skb_headlen(skb);
2204 do {
2205 struct sk_buff *nskb;
2206 skb_frag_t *frag;
2207 int hsize;
2208 int k;
2209 int size;
2211 len = skb->len - offset;
2212 if (len > mss)
2213 len = mss;
2215 hsize = skb_headlen(skb) - offset;
2216 if (hsize < 0)
2217 hsize = 0;
2218 if (hsize > len || !sg)
2219 hsize = len;
2221 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
2222 if (unlikely(!nskb))
2223 goto err;
2225 if (segs)
2226 tail->next = nskb;
2227 else
2228 segs = nskb;
2229 tail = nskb;
2231 __copy_skb_header(nskb, skb);
2232 nskb->mac_len = skb->mac_len;
2233 #ifdef HNDCTF
2234 nskb->ctf_mac_len = skb->ctf_mac_len; /* used by Broadcom CTF driver! */
2235 #endif
2237 skb_reserve(nskb, headroom);
2238 skb_reset_mac_header(nskb);
2239 skb_set_network_header(nskb, skb->mac_len);
2240 nskb->transport_header = (nskb->network_header +
2241 skb_network_header_len(skb));
2242 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2243 doffset);
2244 if (!sg) {
2245 nskb->ip_summed = CHECKSUM_NONE;
2246 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2247 skb_put(nskb, len),
2248 len, 0);
2249 continue;
2252 frag = skb_shinfo(nskb)->frags;
2253 k = 0;
2255 skb_copy_from_linear_data_offset(skb, offset,
2256 skb_put(nskb, hsize), hsize);
2258 while (pos < offset + len) {
2259 BUG_ON(i >= nfrags);
2261 *frag = skb_shinfo(skb)->frags[i];
2262 get_page(frag->page);
2263 size = frag->size;
2265 if (pos < offset) {
2266 frag->page_offset += offset - pos;
2267 frag->size -= offset - pos;
2270 k++;
2272 if (pos + size <= offset + len) {
2273 i++;
2274 pos += size;
2275 } else {
2276 frag->size -= pos + size - (offset + len);
2277 break;
2280 frag++;
2283 skb_shinfo(nskb)->nr_frags = k;
2284 nskb->data_len = len - hsize;
2285 nskb->len += nskb->data_len;
2286 nskb->truesize += nskb->data_len;
2287 } while ((offset += len) < skb->len);
2289 return segs;
2291 err:
2292 while ((skb = segs)) {
2293 segs = skb->next;
2294 kfree_skb(skb);
2296 return ERR_PTR(err);
2299 EXPORT_SYMBOL_GPL(skb_segment);
2301 void __init skb_init(void)
2303 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2304 sizeof(struct sk_buff),
2306 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2307 NULL, NULL);
2308 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2309 (2*sizeof(struct sk_buff)) +
2310 sizeof(atomic_t),
2312 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2313 NULL, NULL);
2317 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2318 * @skb: Socket buffer containing the buffers to be mapped
2319 * @sg: The scatter-gather list to map into
2320 * @offset: The offset into the buffer's contents to start mapping
2321 * @len: Length of buffer space to be mapped
2323 * Fill the specified scatter-gather list with mappings/pointers into a
2324 * region of the buffer space attached to a socket buffer.
2327 skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2329 int start = skb_headlen(skb);
2330 int i, copy = start - offset;
2331 int elt = 0;
2333 if (copy > 0) {
2334 if (copy > len)
2335 copy = len;
2336 sg[elt].page = virt_to_page(skb->data + offset);
2337 sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
2338 sg[elt].length = copy;
2339 elt++;
2340 if ((len -= copy) == 0)
2341 return elt;
2342 offset += copy;
2345 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2346 int end;
2348 BUG_TRAP(start <= offset + len);
2350 end = start + skb_shinfo(skb)->frags[i].size;
2351 if ((copy = end - offset) > 0) {
2352 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2354 if (copy > len)
2355 copy = len;
2356 sg[elt].page = frag->page;
2357 sg[elt].offset = frag->page_offset+offset-start;
2358 sg[elt].length = copy;
2359 elt++;
2360 if (!(len -= copy))
2361 return elt;
2362 offset += copy;
2364 start = end;
2367 if (skb_shinfo(skb)->frag_list) {
2368 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2370 for (; list; list = list->next) {
2371 int end;
2373 BUG_TRAP(start <= offset + len);
2375 end = start + list->len;
2376 if ((copy = end - offset) > 0) {
2377 if (copy > len)
2378 copy = len;
2379 elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
2380 if ((len -= copy) == 0)
2381 return elt;
2382 offset += copy;
2384 start = end;
2387 BUG_ON(len);
2388 return elt;
2392 * skb_cow_data - Check that a socket buffer's data buffers are writable
2393 * @skb: The socket buffer to check.
2394 * @tailbits: Amount of trailing space to be added
2395 * @trailer: Returned pointer to the skb where the @tailbits space begins
2397 * Make sure that the data buffers attached to a socket buffer are
2398 * writable. If they are not, private copies are made of the data buffers
2399 * and the socket buffer is set to use these instead.
2401 * If @tailbits is given, make sure that there is space to write @tailbits
2402 * bytes of data beyond current end of socket buffer. @trailer will be
2403 * set to point to the skb in which this space begins.
2405 * The number of scatterlist elements required to completely map the
2406 * COW'd and extended socket buffer will be returned.
2408 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2410 int copyflag;
2411 int elt;
2412 struct sk_buff *skb1, **skb_p;
2414 /* If skb is cloned or its head is paged, reallocate
2415 * head pulling out all the pages (pages are considered not writable
2416 * at the moment even if they are anonymous).
2418 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2419 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2420 return -ENOMEM;
2422 /* Easy case. Most of packets will go this way. */
2423 if (!skb_shinfo(skb)->frag_list) {
2424 /* A little of trouble, not enough of space for trailer.
2425 * This should not happen, when stack is tuned to generate
2426 * good frames. OK, on miss we reallocate and reserve even more
2427 * space, 128 bytes is fair. */
2429 if (skb_tailroom(skb) < tailbits &&
2430 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2431 return -ENOMEM;
2433 /* Voila! */
2434 *trailer = skb;
2435 return 1;
2438 /* Misery. We are in troubles, going to mincer fragments... */
2440 elt = 1;
2441 skb_p = &skb_shinfo(skb)->frag_list;
2442 copyflag = 0;
2444 while ((skb1 = *skb_p) != NULL) {
2445 int ntail = 0;
2447 /* The fragment is partially pulled by someone,
2448 * this can happen on input. Copy it and everything
2449 * after it. */
2451 if (skb_shared(skb1))
2452 copyflag = 1;
2454 /* If the skb is the last, worry about trailer. */
2456 if (skb1->next == NULL && tailbits) {
2457 if (skb_shinfo(skb1)->nr_frags ||
2458 skb_shinfo(skb1)->frag_list ||
2459 skb_tailroom(skb1) < tailbits)
2460 ntail = tailbits + 128;
2463 if (copyflag ||
2464 skb_cloned(skb1) ||
2465 ntail ||
2466 skb_shinfo(skb1)->nr_frags ||
2467 skb_shinfo(skb1)->frag_list) {
2468 struct sk_buff *skb2;
2470 /* Fuck, we are miserable poor guys... */
2471 if (ntail == 0)
2472 skb2 = skb_copy(skb1, GFP_ATOMIC);
2473 else
2474 skb2 = skb_copy_expand(skb1,
2475 skb_headroom(skb1),
2476 ntail,
2477 GFP_ATOMIC);
2478 if (unlikely(skb2 == NULL))
2479 return -ENOMEM;
2481 if (skb1->sk)
2482 skb_set_owner_w(skb2, skb1->sk);
2484 /* Looking around. Are we still alive?
2485 * OK, link new skb, drop old one */
2487 skb2->next = skb1->next;
2488 *skb_p = skb2;
2489 kfree_skb(skb1);
2490 skb1 = skb2;
2492 elt++;
2493 *trailer = skb1;
2494 skb_p = &skb1->next;
2497 return elt;
2500 EXPORT_SYMBOL(___pskb_trim);
2501 EXPORT_SYMBOL(__kfree_skb);
2502 EXPORT_SYMBOL(kfree_skb);
2503 EXPORT_SYMBOL(__pskb_pull_tail);
2504 EXPORT_SYMBOL(__alloc_skb);
2505 EXPORT_SYMBOL(__netdev_alloc_skb);
2506 EXPORT_SYMBOL(pskb_copy);
2507 EXPORT_SYMBOL(pskb_expand_head);
2508 EXPORT_SYMBOL(skb_checksum);
2509 EXPORT_SYMBOL(skb_clone);
2510 EXPORT_SYMBOL(skb_copy);
2511 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2512 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2513 EXPORT_SYMBOL(skb_copy_bits);
2514 EXPORT_SYMBOL(skb_copy_expand);
2515 EXPORT_SYMBOL(skb_over_panic);
2516 EXPORT_SYMBOL(skb_pad);
2517 EXPORT_SYMBOL(skb_realloc_headroom);
2518 EXPORT_SYMBOL(skb_under_panic);
2519 EXPORT_SYMBOL(skb_dequeue);
2520 EXPORT_SYMBOL(skb_dequeue_tail);
2521 EXPORT_SYMBOL(skb_insert);
2522 EXPORT_SYMBOL(skb_queue_purge);
2523 EXPORT_SYMBOL(skb_queue_head);
2524 EXPORT_SYMBOL(skb_queue_tail);
2525 EXPORT_SYMBOL(skb_unlink);
2526 EXPORT_SYMBOL(skb_append);
2527 EXPORT_SYMBOL(skb_split);
2528 EXPORT_SYMBOL(skb_prepare_seq_read);
2529 EXPORT_SYMBOL(skb_seq_read);
2530 EXPORT_SYMBOL(skb_abort_seq_read);
2531 EXPORT_SYMBOL(skb_find_text);
2532 EXPORT_SYMBOL(skb_append_datato_frags);
2534 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2535 EXPORT_SYMBOL_GPL(skb_cow_data);