ACPI: thinkpad-acpi: start the event hunt season
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / core / skbuff.c
blob067599190291de5d426219a4c663c76a0ae2c360
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 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #include <linux/module.h>
40 #include <linux/types.h>
41 #include <linux/kernel.h>
42 #include <linux/mm.h>
43 #include <linux/interrupt.h>
44 #include <linux/in.h>
45 #include <linux/inet.h>
46 #include <linux/slab.h>
47 #include <linux/netdevice.h>
48 #ifdef CONFIG_NET_CLS_ACT
49 #include <net/pkt_sched.h>
50 #endif
51 #include <linux/string.h>
52 #include <linux/skbuff.h>
53 #include <linux/splice.h>
54 #include <linux/cache.h>
55 #include <linux/rtnetlink.h>
56 #include <linux/init.h>
57 #include <linux/scatterlist.h>
59 #include <net/protocol.h>
60 #include <net/dst.h>
61 #include <net/sock.h>
62 #include <net/checksum.h>
63 #include <net/xfrm.h>
65 #include <asm/uaccess.h>
66 #include <asm/system.h>
68 #include "kmap_skb.h"
70 static struct kmem_cache *skbuff_head_cache __read_mostly;
71 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
73 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
74 struct pipe_buffer *buf)
76 put_page(buf->page);
79 static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
80 struct pipe_buffer *buf)
82 get_page(buf->page);
85 static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
86 struct pipe_buffer *buf)
88 return 1;
92 /* Pipe buffer operations for a socket. */
93 static struct pipe_buf_operations sock_pipe_buf_ops = {
94 .can_merge = 0,
95 .map = generic_pipe_buf_map,
96 .unmap = generic_pipe_buf_unmap,
97 .confirm = generic_pipe_buf_confirm,
98 .release = sock_pipe_buf_release,
99 .steal = sock_pipe_buf_steal,
100 .get = sock_pipe_buf_get,
104 * Keep out-of-line to prevent kernel bloat.
105 * __builtin_return_address is not used because it is not always
106 * reliable.
110 * skb_over_panic - private function
111 * @skb: buffer
112 * @sz: size
113 * @here: address
115 * Out of line support code for skb_put(). Not user callable.
117 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
119 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
120 "data:%p tail:%#lx end:%#lx dev:%s\n",
121 here, skb->len, sz, skb->head, skb->data,
122 (unsigned long)skb->tail, (unsigned long)skb->end,
123 skb->dev ? skb->dev->name : "<NULL>");
124 BUG();
128 * skb_under_panic - private function
129 * @skb: buffer
130 * @sz: size
131 * @here: address
133 * Out of line support code for skb_push(). Not user callable.
136 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
138 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
139 "data:%p tail:%#lx end:%#lx dev:%s\n",
140 here, skb->len, sz, skb->head, skb->data,
141 (unsigned long)skb->tail, (unsigned long)skb->end,
142 skb->dev ? skb->dev->name : "<NULL>");
143 BUG();
146 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
147 * 'private' fields and also do memory statistics to find all the
148 * [BEEP] leaks.
153 * __alloc_skb - allocate a network buffer
154 * @size: size to allocate
155 * @gfp_mask: allocation mask
156 * @fclone: allocate from fclone cache instead of head cache
157 * and allocate a cloned (child) skb
158 * @node: numa node to allocate memory on
160 * Allocate a new &sk_buff. The returned buffer has no headroom and a
161 * tail room of size bytes. The object has a reference count of one.
162 * The return is the buffer. On a failure the return is %NULL.
164 * Buffers may only be allocated from interrupts using a @gfp_mask of
165 * %GFP_ATOMIC.
167 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
168 int fclone, int node)
170 struct kmem_cache *cache;
171 struct skb_shared_info *shinfo;
172 struct sk_buff *skb;
173 u8 *data;
175 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
177 /* Get the HEAD */
178 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
179 if (!skb)
180 goto out;
182 size = SKB_DATA_ALIGN(size);
183 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
184 gfp_mask, node);
185 if (!data)
186 goto nodata;
189 * Only clear those fields we need to clear, not those that we will
190 * actually initialise below. Hence, don't put any more fields after
191 * the tail pointer in struct sk_buff!
193 memset(skb, 0, offsetof(struct sk_buff, tail));
194 skb->truesize = size + sizeof(struct sk_buff);
195 atomic_set(&skb->users, 1);
196 skb->head = data;
197 skb->data = data;
198 skb_reset_tail_pointer(skb);
199 skb->end = skb->tail + size;
200 /* make sure we initialize shinfo sequentially */
201 shinfo = skb_shinfo(skb);
202 atomic_set(&shinfo->dataref, 1);
203 shinfo->nr_frags = 0;
204 shinfo->gso_size = 0;
205 shinfo->gso_segs = 0;
206 shinfo->gso_type = 0;
207 shinfo->ip6_frag_id = 0;
208 shinfo->frag_list = NULL;
210 if (fclone) {
211 struct sk_buff *child = skb + 1;
212 atomic_t *fclone_ref = (atomic_t *) (child + 1);
214 skb->fclone = SKB_FCLONE_ORIG;
215 atomic_set(fclone_ref, 1);
217 child->fclone = SKB_FCLONE_UNAVAILABLE;
219 out:
220 return skb;
221 nodata:
222 kmem_cache_free(cache, skb);
223 skb = NULL;
224 goto out;
228 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
229 * @dev: network device to receive on
230 * @length: length to allocate
231 * @gfp_mask: get_free_pages mask, passed to alloc_skb
233 * Allocate a new &sk_buff and assign it a usage count of one. The
234 * buffer has unspecified headroom built in. Users should allocate
235 * the headroom they think they need without accounting for the
236 * built in space. The built in space is used for optimisations.
238 * %NULL is returned if there is no free memory.
240 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
241 unsigned int length, gfp_t gfp_mask)
243 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
244 struct sk_buff *skb;
246 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
247 if (likely(skb)) {
248 skb_reserve(skb, NET_SKB_PAD);
249 skb->dev = dev;
251 return skb;
255 * dev_alloc_skb - allocate an skbuff for receiving
256 * @length: length to allocate
258 * Allocate a new &sk_buff and assign it a usage count of one. The
259 * buffer has unspecified headroom built in. Users should allocate
260 * the headroom they think they need without accounting for the
261 * built in space. The built in space is used for optimisations.
263 * %NULL is returned if there is no free memory. Although this function
264 * allocates memory it can be called from an interrupt.
266 struct sk_buff *dev_alloc_skb(unsigned int length)
269 * There is more code here than it seems:
270 * __dev_alloc_skb is an inline
272 return __dev_alloc_skb(length, GFP_ATOMIC);
274 EXPORT_SYMBOL(dev_alloc_skb);
276 static void skb_drop_list(struct sk_buff **listp)
278 struct sk_buff *list = *listp;
280 *listp = NULL;
282 do {
283 struct sk_buff *this = list;
284 list = list->next;
285 kfree_skb(this);
286 } while (list);
289 static inline void skb_drop_fraglist(struct sk_buff *skb)
291 skb_drop_list(&skb_shinfo(skb)->frag_list);
294 static void skb_clone_fraglist(struct sk_buff *skb)
296 struct sk_buff *list;
298 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
299 skb_get(list);
302 static void skb_release_data(struct sk_buff *skb)
304 if (!skb->cloned ||
305 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
306 &skb_shinfo(skb)->dataref)) {
307 if (skb_shinfo(skb)->nr_frags) {
308 int i;
309 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
310 put_page(skb_shinfo(skb)->frags[i].page);
313 if (skb_shinfo(skb)->frag_list)
314 skb_drop_fraglist(skb);
316 kfree(skb->head);
321 * Free an skbuff by memory without cleaning the state.
323 static void kfree_skbmem(struct sk_buff *skb)
325 struct sk_buff *other;
326 atomic_t *fclone_ref;
328 switch (skb->fclone) {
329 case SKB_FCLONE_UNAVAILABLE:
330 kmem_cache_free(skbuff_head_cache, skb);
331 break;
333 case SKB_FCLONE_ORIG:
334 fclone_ref = (atomic_t *) (skb + 2);
335 if (atomic_dec_and_test(fclone_ref))
336 kmem_cache_free(skbuff_fclone_cache, skb);
337 break;
339 case SKB_FCLONE_CLONE:
340 fclone_ref = (atomic_t *) (skb + 1);
341 other = skb - 1;
343 /* The clone portion is available for
344 * fast-cloning again.
346 skb->fclone = SKB_FCLONE_UNAVAILABLE;
348 if (atomic_dec_and_test(fclone_ref))
349 kmem_cache_free(skbuff_fclone_cache, other);
350 break;
354 /* Free everything but the sk_buff shell. */
355 static void skb_release_all(struct sk_buff *skb)
357 dst_release(skb->dst);
358 #ifdef CONFIG_XFRM
359 secpath_put(skb->sp);
360 #endif
361 if (skb->destructor) {
362 WARN_ON(in_irq());
363 skb->destructor(skb);
365 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
366 nf_conntrack_put(skb->nfct);
367 nf_conntrack_put_reasm(skb->nfct_reasm);
368 #endif
369 #ifdef CONFIG_BRIDGE_NETFILTER
370 nf_bridge_put(skb->nf_bridge);
371 #endif
372 /* XXX: IS this still necessary? - JHS */
373 #ifdef CONFIG_NET_SCHED
374 skb->tc_index = 0;
375 #ifdef CONFIG_NET_CLS_ACT
376 skb->tc_verd = 0;
377 #endif
378 #endif
379 skb_release_data(skb);
383 * __kfree_skb - private function
384 * @skb: buffer
386 * Free an sk_buff. Release anything attached to the buffer.
387 * Clean the state. This is an internal helper function. Users should
388 * always call kfree_skb
391 void __kfree_skb(struct sk_buff *skb)
393 skb_release_all(skb);
394 kfree_skbmem(skb);
398 * kfree_skb - free an sk_buff
399 * @skb: buffer to free
401 * Drop a reference to the buffer and free it if the usage count has
402 * hit zero.
404 void kfree_skb(struct sk_buff *skb)
406 if (unlikely(!skb))
407 return;
408 if (likely(atomic_read(&skb->users) == 1))
409 smp_rmb();
410 else if (likely(!atomic_dec_and_test(&skb->users)))
411 return;
412 __kfree_skb(skb);
415 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
417 new->tstamp = old->tstamp;
418 new->dev = old->dev;
419 new->transport_header = old->transport_header;
420 new->network_header = old->network_header;
421 new->mac_header = old->mac_header;
422 new->dst = dst_clone(old->dst);
423 #ifdef CONFIG_INET
424 new->sp = secpath_get(old->sp);
425 #endif
426 memcpy(new->cb, old->cb, sizeof(old->cb));
427 new->csum_start = old->csum_start;
428 new->csum_offset = old->csum_offset;
429 new->local_df = old->local_df;
430 new->pkt_type = old->pkt_type;
431 new->ip_summed = old->ip_summed;
432 skb_copy_queue_mapping(new, old);
433 new->priority = old->priority;
434 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
435 new->ipvs_property = old->ipvs_property;
436 #endif
437 new->protocol = old->protocol;
438 new->mark = old->mark;
439 __nf_copy(new, old);
440 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
441 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
442 new->nf_trace = old->nf_trace;
443 #endif
444 #ifdef CONFIG_NET_SCHED
445 new->tc_index = old->tc_index;
446 #ifdef CONFIG_NET_CLS_ACT
447 new->tc_verd = old->tc_verd;
448 #endif
449 #endif
450 new->vlan_tci = old->vlan_tci;
452 skb_copy_secmark(new, old);
455 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
457 #define C(x) n->x = skb->x
459 n->next = n->prev = NULL;
460 n->sk = NULL;
461 __copy_skb_header(n, skb);
463 C(len);
464 C(data_len);
465 C(mac_len);
466 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
467 n->cloned = 1;
468 n->nohdr = 0;
469 n->destructor = NULL;
470 C(iif);
471 C(tail);
472 C(end);
473 C(head);
474 C(data);
475 C(truesize);
476 #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
477 C(do_not_encrypt);
478 #endif
479 atomic_set(&n->users, 1);
481 atomic_inc(&(skb_shinfo(skb)->dataref));
482 skb->cloned = 1;
484 return n;
485 #undef C
489 * skb_morph - morph one skb into another
490 * @dst: the skb to receive the contents
491 * @src: the skb to supply the contents
493 * This is identical to skb_clone except that the target skb is
494 * supplied by the user.
496 * The target skb is returned upon exit.
498 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
500 skb_release_all(dst);
501 return __skb_clone(dst, src);
503 EXPORT_SYMBOL_GPL(skb_morph);
506 * skb_clone - duplicate an sk_buff
507 * @skb: buffer to clone
508 * @gfp_mask: allocation priority
510 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
511 * copies share the same packet data but not structure. The new
512 * buffer has a reference count of 1. If the allocation fails the
513 * function returns %NULL otherwise the new buffer is returned.
515 * If this function is called from an interrupt gfp_mask() must be
516 * %GFP_ATOMIC.
519 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
521 struct sk_buff *n;
523 n = skb + 1;
524 if (skb->fclone == SKB_FCLONE_ORIG &&
525 n->fclone == SKB_FCLONE_UNAVAILABLE) {
526 atomic_t *fclone_ref = (atomic_t *) (n + 1);
527 n->fclone = SKB_FCLONE_CLONE;
528 atomic_inc(fclone_ref);
529 } else {
530 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
531 if (!n)
532 return NULL;
533 n->fclone = SKB_FCLONE_UNAVAILABLE;
536 return __skb_clone(n, skb);
539 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
541 #ifndef NET_SKBUFF_DATA_USES_OFFSET
543 * Shift between the two data areas in bytes
545 unsigned long offset = new->data - old->data;
546 #endif
548 __copy_skb_header(new, old);
550 #ifndef NET_SKBUFF_DATA_USES_OFFSET
551 /* {transport,network,mac}_header are relative to skb->head */
552 new->transport_header += offset;
553 new->network_header += offset;
554 new->mac_header += offset;
555 #endif
556 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
557 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
558 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
562 * skb_copy - create private copy of an sk_buff
563 * @skb: buffer to copy
564 * @gfp_mask: allocation priority
566 * Make a copy of both an &sk_buff and its data. This is used when the
567 * caller wishes to modify the data and needs a private copy of the
568 * data to alter. Returns %NULL on failure or the pointer to the buffer
569 * on success. The returned buffer has a reference count of 1.
571 * As by-product this function converts non-linear &sk_buff to linear
572 * one, so that &sk_buff becomes completely private and caller is allowed
573 * to modify all the data of returned buffer. This means that this
574 * function is not recommended for use in circumstances when only
575 * header is going to be modified. Use pskb_copy() instead.
578 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
580 int headerlen = skb->data - skb->head;
582 * Allocate the copy buffer
584 struct sk_buff *n;
585 #ifdef NET_SKBUFF_DATA_USES_OFFSET
586 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
587 #else
588 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
589 #endif
590 if (!n)
591 return NULL;
593 /* Set the data pointer */
594 skb_reserve(n, headerlen);
595 /* Set the tail pointer and length */
596 skb_put(n, skb->len);
598 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
599 BUG();
601 copy_skb_header(n, skb);
602 return n;
607 * pskb_copy - create copy of an sk_buff with private head.
608 * @skb: buffer to copy
609 * @gfp_mask: allocation priority
611 * Make a copy of both an &sk_buff and part of its data, located
612 * in header. Fragmented data remain shared. This is used when
613 * the caller wishes to modify only header of &sk_buff and needs
614 * private copy of the header to alter. Returns %NULL on failure
615 * or the pointer to the buffer on success.
616 * The returned buffer has a reference count of 1.
619 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
622 * Allocate the copy buffer
624 struct sk_buff *n;
625 #ifdef NET_SKBUFF_DATA_USES_OFFSET
626 n = alloc_skb(skb->end, gfp_mask);
627 #else
628 n = alloc_skb(skb->end - skb->head, gfp_mask);
629 #endif
630 if (!n)
631 goto out;
633 /* Set the data pointer */
634 skb_reserve(n, skb->data - skb->head);
635 /* Set the tail pointer and length */
636 skb_put(n, skb_headlen(skb));
637 /* Copy the bytes */
638 skb_copy_from_linear_data(skb, n->data, n->len);
640 n->truesize += skb->data_len;
641 n->data_len = skb->data_len;
642 n->len = skb->len;
644 if (skb_shinfo(skb)->nr_frags) {
645 int i;
647 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
648 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
649 get_page(skb_shinfo(n)->frags[i].page);
651 skb_shinfo(n)->nr_frags = i;
654 if (skb_shinfo(skb)->frag_list) {
655 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
656 skb_clone_fraglist(n);
659 copy_skb_header(n, skb);
660 out:
661 return n;
665 * pskb_expand_head - reallocate header of &sk_buff
666 * @skb: buffer to reallocate
667 * @nhead: room to add at head
668 * @ntail: room to add at tail
669 * @gfp_mask: allocation priority
671 * Expands (or creates identical copy, if &nhead and &ntail are zero)
672 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
673 * reference count of 1. Returns zero in the case of success or error,
674 * if expansion failed. In the last case, &sk_buff is not changed.
676 * All the pointers pointing into skb header may change and must be
677 * reloaded after call to this function.
680 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
681 gfp_t gfp_mask)
683 int i;
684 u8 *data;
685 #ifdef NET_SKBUFF_DATA_USES_OFFSET
686 int size = nhead + skb->end + ntail;
687 #else
688 int size = nhead + (skb->end - skb->head) + ntail;
689 #endif
690 long off;
692 if (skb_shared(skb))
693 BUG();
695 size = SKB_DATA_ALIGN(size);
697 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
698 if (!data)
699 goto nodata;
701 /* Copy only real data... and, alas, header. This should be
702 * optimized for the cases when header is void. */
703 #ifdef NET_SKBUFF_DATA_USES_OFFSET
704 memcpy(data + nhead, skb->head, skb->tail);
705 #else
706 memcpy(data + nhead, skb->head, skb->tail - skb->head);
707 #endif
708 memcpy(data + size, skb_end_pointer(skb),
709 sizeof(struct skb_shared_info));
711 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
712 get_page(skb_shinfo(skb)->frags[i].page);
714 if (skb_shinfo(skb)->frag_list)
715 skb_clone_fraglist(skb);
717 skb_release_data(skb);
719 off = (data + nhead) - skb->head;
721 skb->head = data;
722 skb->data += off;
723 #ifdef NET_SKBUFF_DATA_USES_OFFSET
724 skb->end = size;
725 off = nhead;
726 #else
727 skb->end = skb->head + size;
728 #endif
729 /* {transport,network,mac}_header and tail are relative to skb->head */
730 skb->tail += off;
731 skb->transport_header += off;
732 skb->network_header += off;
733 skb->mac_header += off;
734 skb->csum_start += nhead;
735 skb->cloned = 0;
736 skb->hdr_len = 0;
737 skb->nohdr = 0;
738 atomic_set(&skb_shinfo(skb)->dataref, 1);
739 return 0;
741 nodata:
742 return -ENOMEM;
745 /* Make private copy of skb with writable head and some headroom */
747 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
749 struct sk_buff *skb2;
750 int delta = headroom - skb_headroom(skb);
752 if (delta <= 0)
753 skb2 = pskb_copy(skb, GFP_ATOMIC);
754 else {
755 skb2 = skb_clone(skb, GFP_ATOMIC);
756 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
757 GFP_ATOMIC)) {
758 kfree_skb(skb2);
759 skb2 = NULL;
762 return skb2;
767 * skb_copy_expand - copy and expand sk_buff
768 * @skb: buffer to copy
769 * @newheadroom: new free bytes at head
770 * @newtailroom: new free bytes at tail
771 * @gfp_mask: allocation priority
773 * Make a copy of both an &sk_buff and its data and while doing so
774 * allocate additional space.
776 * This is used when the caller wishes to modify the data and needs a
777 * private copy of the data to alter as well as more space for new fields.
778 * Returns %NULL on failure or the pointer to the buffer
779 * on success. The returned buffer has a reference count of 1.
781 * You must pass %GFP_ATOMIC as the allocation priority if this function
782 * is called from an interrupt.
784 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
785 int newheadroom, int newtailroom,
786 gfp_t gfp_mask)
789 * Allocate the copy buffer
791 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
792 gfp_mask);
793 int oldheadroom = skb_headroom(skb);
794 int head_copy_len, head_copy_off;
795 int off;
797 if (!n)
798 return NULL;
800 skb_reserve(n, newheadroom);
802 /* Set the tail pointer and length */
803 skb_put(n, skb->len);
805 head_copy_len = oldheadroom;
806 head_copy_off = 0;
807 if (newheadroom <= head_copy_len)
808 head_copy_len = newheadroom;
809 else
810 head_copy_off = newheadroom - head_copy_len;
812 /* Copy the linear header and data. */
813 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
814 skb->len + head_copy_len))
815 BUG();
817 copy_skb_header(n, skb);
819 off = newheadroom - oldheadroom;
820 n->csum_start += off;
821 #ifdef NET_SKBUFF_DATA_USES_OFFSET
822 n->transport_header += off;
823 n->network_header += off;
824 n->mac_header += off;
825 #endif
827 return n;
831 * skb_pad - zero pad the tail of an skb
832 * @skb: buffer to pad
833 * @pad: space to pad
835 * Ensure that a buffer is followed by a padding area that is zero
836 * filled. Used by network drivers which may DMA or transfer data
837 * beyond the buffer end onto the wire.
839 * May return error in out of memory cases. The skb is freed on error.
842 int skb_pad(struct sk_buff *skb, int pad)
844 int err;
845 int ntail;
847 /* If the skbuff is non linear tailroom is always zero.. */
848 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
849 memset(skb->data+skb->len, 0, pad);
850 return 0;
853 ntail = skb->data_len + pad - (skb->end - skb->tail);
854 if (likely(skb_cloned(skb) || ntail > 0)) {
855 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
856 if (unlikely(err))
857 goto free_skb;
860 /* FIXME: The use of this function with non-linear skb's really needs
861 * to be audited.
863 err = skb_linearize(skb);
864 if (unlikely(err))
865 goto free_skb;
867 memset(skb->data + skb->len, 0, pad);
868 return 0;
870 free_skb:
871 kfree_skb(skb);
872 return err;
876 * skb_put - add data to a buffer
877 * @skb: buffer to use
878 * @len: amount of data to add
880 * This function extends the used data area of the buffer. If this would
881 * exceed the total buffer size the kernel will panic. A pointer to the
882 * first byte of the extra data is returned.
884 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
886 unsigned char *tmp = skb_tail_pointer(skb);
887 SKB_LINEAR_ASSERT(skb);
888 skb->tail += len;
889 skb->len += len;
890 if (unlikely(skb->tail > skb->end))
891 skb_over_panic(skb, len, __builtin_return_address(0));
892 return tmp;
894 EXPORT_SYMBOL(skb_put);
897 * skb_push - add data to the start of a buffer
898 * @skb: buffer to use
899 * @len: amount of data to add
901 * This function extends the used data area of the buffer at the buffer
902 * start. If this would exceed the total buffer headroom the kernel will
903 * panic. A pointer to the first byte of the extra data is returned.
905 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
907 skb->data -= len;
908 skb->len += len;
909 if (unlikely(skb->data<skb->head))
910 skb_under_panic(skb, len, __builtin_return_address(0));
911 return skb->data;
913 EXPORT_SYMBOL(skb_push);
916 * skb_pull - remove data from the start of a buffer
917 * @skb: buffer to use
918 * @len: amount of data to remove
920 * This function removes data from the start of a buffer, returning
921 * the memory to the headroom. A pointer to the next data in the buffer
922 * is returned. Once the data has been pulled future pushes will overwrite
923 * the old data.
925 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
927 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
929 EXPORT_SYMBOL(skb_pull);
932 * skb_trim - remove end from a buffer
933 * @skb: buffer to alter
934 * @len: new length
936 * Cut the length of a buffer down by removing data from the tail. If
937 * the buffer is already under the length specified it is not modified.
938 * The skb must be linear.
940 void skb_trim(struct sk_buff *skb, unsigned int len)
942 if (skb->len > len)
943 __skb_trim(skb, len);
945 EXPORT_SYMBOL(skb_trim);
947 /* Trims skb to length len. It can change skb pointers.
950 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
952 struct sk_buff **fragp;
953 struct sk_buff *frag;
954 int offset = skb_headlen(skb);
955 int nfrags = skb_shinfo(skb)->nr_frags;
956 int i;
957 int err;
959 if (skb_cloned(skb) &&
960 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
961 return err;
963 i = 0;
964 if (offset >= len)
965 goto drop_pages;
967 for (; i < nfrags; i++) {
968 int end = offset + skb_shinfo(skb)->frags[i].size;
970 if (end < len) {
971 offset = end;
972 continue;
975 skb_shinfo(skb)->frags[i++].size = len - offset;
977 drop_pages:
978 skb_shinfo(skb)->nr_frags = i;
980 for (; i < nfrags; i++)
981 put_page(skb_shinfo(skb)->frags[i].page);
983 if (skb_shinfo(skb)->frag_list)
984 skb_drop_fraglist(skb);
985 goto done;
988 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
989 fragp = &frag->next) {
990 int end = offset + frag->len;
992 if (skb_shared(frag)) {
993 struct sk_buff *nfrag;
995 nfrag = skb_clone(frag, GFP_ATOMIC);
996 if (unlikely(!nfrag))
997 return -ENOMEM;
999 nfrag->next = frag->next;
1000 kfree_skb(frag);
1001 frag = nfrag;
1002 *fragp = frag;
1005 if (end < len) {
1006 offset = end;
1007 continue;
1010 if (end > len &&
1011 unlikely((err = pskb_trim(frag, len - offset))))
1012 return err;
1014 if (frag->next)
1015 skb_drop_list(&frag->next);
1016 break;
1019 done:
1020 if (len > skb_headlen(skb)) {
1021 skb->data_len -= skb->len - len;
1022 skb->len = len;
1023 } else {
1024 skb->len = len;
1025 skb->data_len = 0;
1026 skb_set_tail_pointer(skb, len);
1029 return 0;
1033 * __pskb_pull_tail - advance tail of skb header
1034 * @skb: buffer to reallocate
1035 * @delta: number of bytes to advance tail
1037 * The function makes a sense only on a fragmented &sk_buff,
1038 * it expands header moving its tail forward and copying necessary
1039 * data from fragmented part.
1041 * &sk_buff MUST have reference count of 1.
1043 * Returns %NULL (and &sk_buff does not change) if pull failed
1044 * or value of new tail of skb in the case of success.
1046 * All the pointers pointing into skb header may change and must be
1047 * reloaded after call to this function.
1050 /* Moves tail of skb head forward, copying data from fragmented part,
1051 * when it is necessary.
1052 * 1. It may fail due to malloc failure.
1053 * 2. It may change skb pointers.
1055 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1057 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1059 /* If skb has not enough free space at tail, get new one
1060 * plus 128 bytes for future expansions. If we have enough
1061 * room at tail, reallocate without expansion only if skb is cloned.
1063 int i, k, eat = (skb->tail + delta) - skb->end;
1065 if (eat > 0 || skb_cloned(skb)) {
1066 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1067 GFP_ATOMIC))
1068 return NULL;
1071 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1072 BUG();
1074 /* Optimization: no fragments, no reasons to preestimate
1075 * size of pulled pages. Superb.
1077 if (!skb_shinfo(skb)->frag_list)
1078 goto pull_pages;
1080 /* Estimate size of pulled pages. */
1081 eat = delta;
1082 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1083 if (skb_shinfo(skb)->frags[i].size >= eat)
1084 goto pull_pages;
1085 eat -= skb_shinfo(skb)->frags[i].size;
1088 /* If we need update frag list, we are in troubles.
1089 * Certainly, it possible to add an offset to skb data,
1090 * but taking into account that pulling is expected to
1091 * be very rare operation, it is worth to fight against
1092 * further bloating skb head and crucify ourselves here instead.
1093 * Pure masohism, indeed. 8)8)
1095 if (eat) {
1096 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1097 struct sk_buff *clone = NULL;
1098 struct sk_buff *insp = NULL;
1100 do {
1101 BUG_ON(!list);
1103 if (list->len <= eat) {
1104 /* Eaten as whole. */
1105 eat -= list->len;
1106 list = list->next;
1107 insp = list;
1108 } else {
1109 /* Eaten partially. */
1111 if (skb_shared(list)) {
1112 /* Sucks! We need to fork list. :-( */
1113 clone = skb_clone(list, GFP_ATOMIC);
1114 if (!clone)
1115 return NULL;
1116 insp = list->next;
1117 list = clone;
1118 } else {
1119 /* This may be pulled without
1120 * problems. */
1121 insp = list;
1123 if (!pskb_pull(list, eat)) {
1124 if (clone)
1125 kfree_skb(clone);
1126 return NULL;
1128 break;
1130 } while (eat);
1132 /* Free pulled out fragments. */
1133 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1134 skb_shinfo(skb)->frag_list = list->next;
1135 kfree_skb(list);
1137 /* And insert new clone at head. */
1138 if (clone) {
1139 clone->next = list;
1140 skb_shinfo(skb)->frag_list = clone;
1143 /* Success! Now we may commit changes to skb data. */
1145 pull_pages:
1146 eat = delta;
1147 k = 0;
1148 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1149 if (skb_shinfo(skb)->frags[i].size <= eat) {
1150 put_page(skb_shinfo(skb)->frags[i].page);
1151 eat -= skb_shinfo(skb)->frags[i].size;
1152 } else {
1153 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1154 if (eat) {
1155 skb_shinfo(skb)->frags[k].page_offset += eat;
1156 skb_shinfo(skb)->frags[k].size -= eat;
1157 eat = 0;
1159 k++;
1162 skb_shinfo(skb)->nr_frags = k;
1164 skb->tail += delta;
1165 skb->data_len -= delta;
1167 return skb_tail_pointer(skb);
1170 /* Copy some data bits from skb to kernel buffer. */
1172 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1174 int i, copy;
1175 int start = skb_headlen(skb);
1177 if (offset > (int)skb->len - len)
1178 goto fault;
1180 /* Copy header. */
1181 if ((copy = start - offset) > 0) {
1182 if (copy > len)
1183 copy = len;
1184 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1185 if ((len -= copy) == 0)
1186 return 0;
1187 offset += copy;
1188 to += copy;
1191 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1192 int end;
1194 WARN_ON(start > offset + len);
1196 end = start + skb_shinfo(skb)->frags[i].size;
1197 if ((copy = end - offset) > 0) {
1198 u8 *vaddr;
1200 if (copy > len)
1201 copy = len;
1203 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1204 memcpy(to,
1205 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1206 offset - start, copy);
1207 kunmap_skb_frag(vaddr);
1209 if ((len -= copy) == 0)
1210 return 0;
1211 offset += copy;
1212 to += copy;
1214 start = end;
1217 if (skb_shinfo(skb)->frag_list) {
1218 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1220 for (; list; list = list->next) {
1221 int end;
1223 WARN_ON(start > offset + len);
1225 end = start + list->len;
1226 if ((copy = end - offset) > 0) {
1227 if (copy > len)
1228 copy = len;
1229 if (skb_copy_bits(list, offset - start,
1230 to, copy))
1231 goto fault;
1232 if ((len -= copy) == 0)
1233 return 0;
1234 offset += copy;
1235 to += copy;
1237 start = end;
1240 if (!len)
1241 return 0;
1243 fault:
1244 return -EFAULT;
1248 * Callback from splice_to_pipe(), if we need to release some pages
1249 * at the end of the spd in case we error'ed out in filling the pipe.
1251 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1253 put_page(spd->pages[i]);
1256 static inline struct page *linear_to_page(struct page *page, unsigned int len,
1257 unsigned int offset)
1259 struct page *p = alloc_pages(GFP_KERNEL, 0);
1261 if (!p)
1262 return NULL;
1263 memcpy(page_address(p) + offset, page_address(page) + offset, len);
1265 return p;
1269 * Fill page/offset/length into spd, if it can hold more pages.
1271 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1272 unsigned int len, unsigned int offset,
1273 struct sk_buff *skb, int linear)
1275 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1276 return 1;
1278 if (linear) {
1279 page = linear_to_page(page, len, offset);
1280 if (!page)
1281 return 1;
1282 } else
1283 get_page(page);
1285 spd->pages[spd->nr_pages] = page;
1286 spd->partial[spd->nr_pages].len = len;
1287 spd->partial[spd->nr_pages].offset = offset;
1288 spd->nr_pages++;
1290 return 0;
1293 static inline void __segment_seek(struct page **page, unsigned int *poff,
1294 unsigned int *plen, unsigned int off)
1296 *poff += off;
1297 *page += *poff / PAGE_SIZE;
1298 *poff = *poff % PAGE_SIZE;
1299 *plen -= off;
1302 static inline int __splice_segment(struct page *page, unsigned int poff,
1303 unsigned int plen, unsigned int *off,
1304 unsigned int *len, struct sk_buff *skb,
1305 struct splice_pipe_desc *spd, int linear)
1307 if (!*len)
1308 return 1;
1310 /* skip this segment if already processed */
1311 if (*off >= plen) {
1312 *off -= plen;
1313 return 0;
1316 /* ignore any bits we already processed */
1317 if (*off) {
1318 __segment_seek(&page, &poff, &plen, *off);
1319 *off = 0;
1322 do {
1323 unsigned int flen = min(*len, plen);
1325 /* the linear region may spread across several pages */
1326 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1328 if (spd_fill_page(spd, page, flen, poff, skb, linear))
1329 return 1;
1331 __segment_seek(&page, &poff, &plen, flen);
1332 *len -= flen;
1334 } while (*len && plen);
1336 return 0;
1340 * Map linear and fragment data from the skb to spd. It reports failure if the
1341 * pipe is full or if we already spliced the requested length.
1343 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1344 unsigned int *len,
1345 struct splice_pipe_desc *spd)
1347 int seg;
1350 * map the linear part
1352 if (__splice_segment(virt_to_page(skb->data),
1353 (unsigned long) skb->data & (PAGE_SIZE - 1),
1354 skb_headlen(skb),
1355 offset, len, skb, spd, 1))
1356 return 1;
1359 * then map the fragments
1361 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1362 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1364 if (__splice_segment(f->page, f->page_offset, f->size,
1365 offset, len, skb, spd, 0))
1366 return 1;
1369 return 0;
1373 * Map data from the skb to a pipe. Should handle both the linear part,
1374 * the fragments, and the frag list. It does NOT handle frag lists within
1375 * the frag list, if such a thing exists. We'd probably need to recurse to
1376 * handle that cleanly.
1378 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1379 struct pipe_inode_info *pipe, unsigned int tlen,
1380 unsigned int flags)
1382 struct partial_page partial[PIPE_BUFFERS];
1383 struct page *pages[PIPE_BUFFERS];
1384 struct splice_pipe_desc spd = {
1385 .pages = pages,
1386 .partial = partial,
1387 .flags = flags,
1388 .ops = &sock_pipe_buf_ops,
1389 .spd_release = sock_spd_release,
1393 * __skb_splice_bits() only fails if the output has no room left,
1394 * so no point in going over the frag_list for the error case.
1396 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1397 goto done;
1398 else if (!tlen)
1399 goto done;
1402 * now see if we have a frag_list to map
1404 if (skb_shinfo(skb)->frag_list) {
1405 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1407 for (; list && tlen; list = list->next) {
1408 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1409 break;
1413 done:
1414 if (spd.nr_pages) {
1415 struct sock *sk = skb->sk;
1416 int ret;
1419 * Drop the socket lock, otherwise we have reverse
1420 * locking dependencies between sk_lock and i_mutex
1421 * here as compared to sendfile(). We enter here
1422 * with the socket lock held, and splice_to_pipe() will
1423 * grab the pipe inode lock. For sendfile() emulation,
1424 * we call into ->sendpage() with the i_mutex lock held
1425 * and networking will grab the socket lock.
1427 release_sock(sk);
1428 ret = splice_to_pipe(pipe, &spd);
1429 lock_sock(sk);
1430 return ret;
1433 return 0;
1437 * skb_store_bits - store bits from kernel buffer to skb
1438 * @skb: destination buffer
1439 * @offset: offset in destination
1440 * @from: source buffer
1441 * @len: number of bytes to copy
1443 * Copy the specified number of bytes from the source buffer to the
1444 * destination skb. This function handles all the messy bits of
1445 * traversing fragment lists and such.
1448 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1450 int i, copy;
1451 int start = skb_headlen(skb);
1453 if (offset > (int)skb->len - len)
1454 goto fault;
1456 if ((copy = start - offset) > 0) {
1457 if (copy > len)
1458 copy = len;
1459 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1460 if ((len -= copy) == 0)
1461 return 0;
1462 offset += copy;
1463 from += copy;
1466 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1467 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1468 int end;
1470 WARN_ON(start > offset + len);
1472 end = start + frag->size;
1473 if ((copy = end - offset) > 0) {
1474 u8 *vaddr;
1476 if (copy > len)
1477 copy = len;
1479 vaddr = kmap_skb_frag(frag);
1480 memcpy(vaddr + frag->page_offset + offset - start,
1481 from, copy);
1482 kunmap_skb_frag(vaddr);
1484 if ((len -= copy) == 0)
1485 return 0;
1486 offset += copy;
1487 from += copy;
1489 start = end;
1492 if (skb_shinfo(skb)->frag_list) {
1493 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1495 for (; list; list = list->next) {
1496 int end;
1498 WARN_ON(start > offset + len);
1500 end = start + list->len;
1501 if ((copy = end - offset) > 0) {
1502 if (copy > len)
1503 copy = len;
1504 if (skb_store_bits(list, offset - start,
1505 from, copy))
1506 goto fault;
1507 if ((len -= copy) == 0)
1508 return 0;
1509 offset += copy;
1510 from += copy;
1512 start = end;
1515 if (!len)
1516 return 0;
1518 fault:
1519 return -EFAULT;
1522 EXPORT_SYMBOL(skb_store_bits);
1524 /* Checksum skb data. */
1526 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1527 int len, __wsum csum)
1529 int start = skb_headlen(skb);
1530 int i, copy = start - offset;
1531 int pos = 0;
1533 /* Checksum header. */
1534 if (copy > 0) {
1535 if (copy > len)
1536 copy = len;
1537 csum = csum_partial(skb->data + offset, copy, csum);
1538 if ((len -= copy) == 0)
1539 return csum;
1540 offset += copy;
1541 pos = copy;
1544 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1545 int end;
1547 WARN_ON(start > offset + len);
1549 end = start + skb_shinfo(skb)->frags[i].size;
1550 if ((copy = end - offset) > 0) {
1551 __wsum csum2;
1552 u8 *vaddr;
1553 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1555 if (copy > len)
1556 copy = len;
1557 vaddr = kmap_skb_frag(frag);
1558 csum2 = csum_partial(vaddr + frag->page_offset +
1559 offset - start, copy, 0);
1560 kunmap_skb_frag(vaddr);
1561 csum = csum_block_add(csum, csum2, pos);
1562 if (!(len -= copy))
1563 return csum;
1564 offset += copy;
1565 pos += copy;
1567 start = end;
1570 if (skb_shinfo(skb)->frag_list) {
1571 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1573 for (; list; list = list->next) {
1574 int end;
1576 WARN_ON(start > offset + len);
1578 end = start + list->len;
1579 if ((copy = end - offset) > 0) {
1580 __wsum csum2;
1581 if (copy > len)
1582 copy = len;
1583 csum2 = skb_checksum(list, offset - start,
1584 copy, 0);
1585 csum = csum_block_add(csum, csum2, pos);
1586 if ((len -= copy) == 0)
1587 return csum;
1588 offset += copy;
1589 pos += copy;
1591 start = end;
1594 BUG_ON(len);
1596 return csum;
1599 /* Both of above in one bottle. */
1601 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1602 u8 *to, int len, __wsum csum)
1604 int start = skb_headlen(skb);
1605 int i, copy = start - offset;
1606 int pos = 0;
1608 /* Copy header. */
1609 if (copy > 0) {
1610 if (copy > len)
1611 copy = len;
1612 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1613 copy, csum);
1614 if ((len -= copy) == 0)
1615 return csum;
1616 offset += copy;
1617 to += copy;
1618 pos = copy;
1621 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1622 int end;
1624 WARN_ON(start > offset + len);
1626 end = start + skb_shinfo(skb)->frags[i].size;
1627 if ((copy = end - offset) > 0) {
1628 __wsum csum2;
1629 u8 *vaddr;
1630 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1632 if (copy > len)
1633 copy = len;
1634 vaddr = kmap_skb_frag(frag);
1635 csum2 = csum_partial_copy_nocheck(vaddr +
1636 frag->page_offset +
1637 offset - start, to,
1638 copy, 0);
1639 kunmap_skb_frag(vaddr);
1640 csum = csum_block_add(csum, csum2, pos);
1641 if (!(len -= copy))
1642 return csum;
1643 offset += copy;
1644 to += copy;
1645 pos += copy;
1647 start = end;
1650 if (skb_shinfo(skb)->frag_list) {
1651 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1653 for (; list; list = list->next) {
1654 __wsum csum2;
1655 int end;
1657 WARN_ON(start > offset + len);
1659 end = start + list->len;
1660 if ((copy = end - offset) > 0) {
1661 if (copy > len)
1662 copy = len;
1663 csum2 = skb_copy_and_csum_bits(list,
1664 offset - start,
1665 to, copy, 0);
1666 csum = csum_block_add(csum, csum2, pos);
1667 if ((len -= copy) == 0)
1668 return csum;
1669 offset += copy;
1670 to += copy;
1671 pos += copy;
1673 start = end;
1676 BUG_ON(len);
1677 return csum;
1680 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1682 __wsum csum;
1683 long csstart;
1685 if (skb->ip_summed == CHECKSUM_PARTIAL)
1686 csstart = skb->csum_start - skb_headroom(skb);
1687 else
1688 csstart = skb_headlen(skb);
1690 BUG_ON(csstart > skb_headlen(skb));
1692 skb_copy_from_linear_data(skb, to, csstart);
1694 csum = 0;
1695 if (csstart != skb->len)
1696 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1697 skb->len - csstart, 0);
1699 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1700 long csstuff = csstart + skb->csum_offset;
1702 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1707 * skb_dequeue - remove from the head of the queue
1708 * @list: list to dequeue from
1710 * Remove the head of the list. The list lock is taken so the function
1711 * may be used safely with other locking list functions. The head item is
1712 * returned or %NULL if the list is empty.
1715 struct sk_buff *skb_dequeue(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(list);
1722 spin_unlock_irqrestore(&list->lock, flags);
1723 return result;
1727 * skb_dequeue_tail - remove from the tail of the queue
1728 * @list: list to dequeue from
1730 * Remove the tail of the list. The list lock is taken so the function
1731 * may be used safely with other locking list functions. The tail item is
1732 * returned or %NULL if the list is empty.
1734 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1736 unsigned long flags;
1737 struct sk_buff *result;
1739 spin_lock_irqsave(&list->lock, flags);
1740 result = __skb_dequeue_tail(list);
1741 spin_unlock_irqrestore(&list->lock, flags);
1742 return result;
1746 * skb_queue_purge - empty a list
1747 * @list: list to empty
1749 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1750 * the list and one reference dropped. This function takes the list
1751 * lock and is atomic with respect to other list locking functions.
1753 void skb_queue_purge(struct sk_buff_head *list)
1755 struct sk_buff *skb;
1756 while ((skb = skb_dequeue(list)) != NULL)
1757 kfree_skb(skb);
1761 * skb_queue_head - queue a buffer at the list head
1762 * @list: list to use
1763 * @newsk: buffer to queue
1765 * Queue a buffer at the start of the list. This function takes the
1766 * list lock and can be used safely with other locking &sk_buff functions
1767 * safely.
1769 * A buffer cannot be placed on two lists at the same time.
1771 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1773 unsigned long flags;
1775 spin_lock_irqsave(&list->lock, flags);
1776 __skb_queue_head(list, newsk);
1777 spin_unlock_irqrestore(&list->lock, flags);
1781 * skb_queue_tail - queue a buffer at the list tail
1782 * @list: list to use
1783 * @newsk: buffer to queue
1785 * Queue a buffer at the tail of the list. This function takes the
1786 * list lock and can be used safely with other locking &sk_buff functions
1787 * safely.
1789 * A buffer cannot be placed on two lists at the same time.
1791 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1793 unsigned long flags;
1795 spin_lock_irqsave(&list->lock, flags);
1796 __skb_queue_tail(list, newsk);
1797 spin_unlock_irqrestore(&list->lock, flags);
1801 * skb_unlink - remove a buffer from a list
1802 * @skb: buffer to remove
1803 * @list: list to use
1805 * Remove a packet from a list. The list locks are taken and this
1806 * function is atomic with respect to other list locked calls
1808 * You must know what list the SKB is on.
1810 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1812 unsigned long flags;
1814 spin_lock_irqsave(&list->lock, flags);
1815 __skb_unlink(skb, list);
1816 spin_unlock_irqrestore(&list->lock, flags);
1820 * skb_append - append a buffer
1821 * @old: buffer to insert after
1822 * @newsk: buffer to insert
1823 * @list: list to use
1825 * Place a packet after a given packet in a list. The list locks are taken
1826 * and this function is atomic with respect to other list locked calls.
1827 * A buffer cannot be placed on two lists at the same time.
1829 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1831 unsigned long flags;
1833 spin_lock_irqsave(&list->lock, flags);
1834 __skb_queue_after(list, old, newsk);
1835 spin_unlock_irqrestore(&list->lock, flags);
1840 * skb_insert - insert a buffer
1841 * @old: buffer to insert before
1842 * @newsk: buffer to insert
1843 * @list: list to use
1845 * Place a packet before a given packet in a list. The list locks are
1846 * taken and this function is atomic with respect to other list locked
1847 * calls.
1849 * A buffer cannot be placed on two lists at the same time.
1851 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1853 unsigned long flags;
1855 spin_lock_irqsave(&list->lock, flags);
1856 __skb_insert(newsk, old->prev, old, list);
1857 spin_unlock_irqrestore(&list->lock, flags);
1860 static inline void skb_split_inside_header(struct sk_buff *skb,
1861 struct sk_buff* skb1,
1862 const u32 len, const int pos)
1864 int i;
1866 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1867 pos - len);
1868 /* And move data appendix as is. */
1869 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1870 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1872 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1873 skb_shinfo(skb)->nr_frags = 0;
1874 skb1->data_len = skb->data_len;
1875 skb1->len += skb1->data_len;
1876 skb->data_len = 0;
1877 skb->len = len;
1878 skb_set_tail_pointer(skb, len);
1881 static inline void skb_split_no_header(struct sk_buff *skb,
1882 struct sk_buff* skb1,
1883 const u32 len, int pos)
1885 int i, k = 0;
1886 const int nfrags = skb_shinfo(skb)->nr_frags;
1888 skb_shinfo(skb)->nr_frags = 0;
1889 skb1->len = skb1->data_len = skb->len - len;
1890 skb->len = len;
1891 skb->data_len = len - pos;
1893 for (i = 0; i < nfrags; i++) {
1894 int size = skb_shinfo(skb)->frags[i].size;
1896 if (pos + size > len) {
1897 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1899 if (pos < len) {
1900 /* Split frag.
1901 * We have two variants in this case:
1902 * 1. Move all the frag to the second
1903 * part, if it is possible. F.e.
1904 * this approach is mandatory for TUX,
1905 * where splitting is expensive.
1906 * 2. Split is accurately. We make this.
1908 get_page(skb_shinfo(skb)->frags[i].page);
1909 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1910 skb_shinfo(skb1)->frags[0].size -= len - pos;
1911 skb_shinfo(skb)->frags[i].size = len - pos;
1912 skb_shinfo(skb)->nr_frags++;
1914 k++;
1915 } else
1916 skb_shinfo(skb)->nr_frags++;
1917 pos += size;
1919 skb_shinfo(skb1)->nr_frags = k;
1923 * skb_split - Split fragmented skb to two parts at length len.
1924 * @skb: the buffer to split
1925 * @skb1: the buffer to receive the second part
1926 * @len: new length for skb
1928 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1930 int pos = skb_headlen(skb);
1932 if (len < pos) /* Split line is inside header. */
1933 skb_split_inside_header(skb, skb1, len, pos);
1934 else /* Second chunk has no header, nothing to copy. */
1935 skb_split_no_header(skb, skb1, len, pos);
1939 * skb_prepare_seq_read - Prepare a sequential read of skb data
1940 * @skb: the buffer to read
1941 * @from: lower offset of data to be read
1942 * @to: upper offset of data to be read
1943 * @st: state variable
1945 * Initializes the specified state variable. Must be called before
1946 * invoking skb_seq_read() for the first time.
1948 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1949 unsigned int to, struct skb_seq_state *st)
1951 st->lower_offset = from;
1952 st->upper_offset = to;
1953 st->root_skb = st->cur_skb = skb;
1954 st->frag_idx = st->stepped_offset = 0;
1955 st->frag_data = NULL;
1959 * skb_seq_read - Sequentially read skb data
1960 * @consumed: number of bytes consumed by the caller so far
1961 * @data: destination pointer for data to be returned
1962 * @st: state variable
1964 * Reads a block of skb data at &consumed relative to the
1965 * lower offset specified to skb_prepare_seq_read(). Assigns
1966 * the head of the data block to &data and returns the length
1967 * of the block or 0 if the end of the skb data or the upper
1968 * offset has been reached.
1970 * The caller is not required to consume all of the data
1971 * returned, i.e. &consumed is typically set to the number
1972 * of bytes already consumed and the next call to
1973 * skb_seq_read() will return the remaining part of the block.
1975 * Note 1: The size of each block of data returned can be arbitary,
1976 * this limitation is the cost for zerocopy seqeuental
1977 * reads of potentially non linear data.
1979 * Note 2: Fragment lists within fragments are not implemented
1980 * at the moment, state->root_skb could be replaced with
1981 * a stack for this purpose.
1983 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1984 struct skb_seq_state *st)
1986 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1987 skb_frag_t *frag;
1989 if (unlikely(abs_offset >= st->upper_offset))
1990 return 0;
1992 next_skb:
1993 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
1995 if (abs_offset < block_limit) {
1996 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
1997 return block_limit - abs_offset;
2000 if (st->frag_idx == 0 && !st->frag_data)
2001 st->stepped_offset += skb_headlen(st->cur_skb);
2003 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2004 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2005 block_limit = frag->size + st->stepped_offset;
2007 if (abs_offset < block_limit) {
2008 if (!st->frag_data)
2009 st->frag_data = kmap_skb_frag(frag);
2011 *data = (u8 *) st->frag_data + frag->page_offset +
2012 (abs_offset - st->stepped_offset);
2014 return block_limit - abs_offset;
2017 if (st->frag_data) {
2018 kunmap_skb_frag(st->frag_data);
2019 st->frag_data = NULL;
2022 st->frag_idx++;
2023 st->stepped_offset += frag->size;
2026 if (st->frag_data) {
2027 kunmap_skb_frag(st->frag_data);
2028 st->frag_data = NULL;
2031 if (st->root_skb == st->cur_skb &&
2032 skb_shinfo(st->root_skb)->frag_list) {
2033 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2034 st->frag_idx = 0;
2035 goto next_skb;
2036 } else if (st->cur_skb->next) {
2037 st->cur_skb = st->cur_skb->next;
2038 st->frag_idx = 0;
2039 goto next_skb;
2042 return 0;
2046 * skb_abort_seq_read - Abort a sequential read of skb data
2047 * @st: state variable
2049 * Must be called if skb_seq_read() was not called until it
2050 * returned 0.
2052 void skb_abort_seq_read(struct skb_seq_state *st)
2054 if (st->frag_data)
2055 kunmap_skb_frag(st->frag_data);
2058 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2060 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2061 struct ts_config *conf,
2062 struct ts_state *state)
2064 return skb_seq_read(offset, text, TS_SKB_CB(state));
2067 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2069 skb_abort_seq_read(TS_SKB_CB(state));
2073 * skb_find_text - Find a text pattern in skb data
2074 * @skb: the buffer to look in
2075 * @from: search offset
2076 * @to: search limit
2077 * @config: textsearch configuration
2078 * @state: uninitialized textsearch state variable
2080 * Finds a pattern in the skb data according to the specified
2081 * textsearch configuration. Use textsearch_next() to retrieve
2082 * subsequent occurrences of the pattern. Returns the offset
2083 * to the first occurrence or UINT_MAX if no match was found.
2085 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2086 unsigned int to, struct ts_config *config,
2087 struct ts_state *state)
2089 unsigned int ret;
2091 config->get_next_block = skb_ts_get_next_block;
2092 config->finish = skb_ts_finish;
2094 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2096 ret = textsearch_find(config, state);
2097 return (ret <= to - from ? ret : UINT_MAX);
2101 * skb_append_datato_frags: - append the user data to a skb
2102 * @sk: sock structure
2103 * @skb: skb structure to be appened with user data.
2104 * @getfrag: call back function to be used for getting the user data
2105 * @from: pointer to user message iov
2106 * @length: length of the iov message
2108 * Description: This procedure append the user data in the fragment part
2109 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2111 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2112 int (*getfrag)(void *from, char *to, int offset,
2113 int len, int odd, struct sk_buff *skb),
2114 void *from, int length)
2116 int frg_cnt = 0;
2117 skb_frag_t *frag = NULL;
2118 struct page *page = NULL;
2119 int copy, left;
2120 int offset = 0;
2121 int ret;
2123 do {
2124 /* Return error if we don't have space for new frag */
2125 frg_cnt = skb_shinfo(skb)->nr_frags;
2126 if (frg_cnt >= MAX_SKB_FRAGS)
2127 return -EFAULT;
2129 /* allocate a new page for next frag */
2130 page = alloc_pages(sk->sk_allocation, 0);
2132 /* If alloc_page fails just return failure and caller will
2133 * free previous allocated pages by doing kfree_skb()
2135 if (page == NULL)
2136 return -ENOMEM;
2138 /* initialize the next frag */
2139 sk->sk_sndmsg_page = page;
2140 sk->sk_sndmsg_off = 0;
2141 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2142 skb->truesize += PAGE_SIZE;
2143 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2145 /* get the new initialized frag */
2146 frg_cnt = skb_shinfo(skb)->nr_frags;
2147 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2149 /* copy the user data to page */
2150 left = PAGE_SIZE - frag->page_offset;
2151 copy = (length > left)? left : length;
2153 ret = getfrag(from, (page_address(frag->page) +
2154 frag->page_offset + frag->size),
2155 offset, copy, 0, skb);
2156 if (ret < 0)
2157 return -EFAULT;
2159 /* copy was successful so update the size parameters */
2160 sk->sk_sndmsg_off += copy;
2161 frag->size += copy;
2162 skb->len += copy;
2163 skb->data_len += copy;
2164 offset += copy;
2165 length -= copy;
2167 } while (length > 0);
2169 return 0;
2173 * skb_pull_rcsum - pull skb and update receive checksum
2174 * @skb: buffer to update
2175 * @len: length of data pulled
2177 * This function performs an skb_pull on the packet and updates
2178 * the CHECKSUM_COMPLETE checksum. It should be used on
2179 * receive path processing instead of skb_pull unless you know
2180 * that the checksum difference is zero (e.g., a valid IP header)
2181 * or you are setting ip_summed to CHECKSUM_NONE.
2183 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2185 BUG_ON(len > skb->len);
2186 skb->len -= len;
2187 BUG_ON(skb->len < skb->data_len);
2188 skb_postpull_rcsum(skb, skb->data, len);
2189 return skb->data += len;
2192 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2195 * skb_segment - Perform protocol segmentation on skb.
2196 * @skb: buffer to segment
2197 * @features: features for the output path (see dev->features)
2199 * This function performs segmentation on the given skb. It returns
2200 * a pointer to the first in a list of new skbs for the segments.
2201 * In case of error it returns ERR_PTR(err).
2203 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2205 struct sk_buff *segs = NULL;
2206 struct sk_buff *tail = NULL;
2207 unsigned int mss = skb_shinfo(skb)->gso_size;
2208 unsigned int doffset = skb->data - skb_mac_header(skb);
2209 unsigned int offset = doffset;
2210 unsigned int headroom;
2211 unsigned int len;
2212 int sg = features & NETIF_F_SG;
2213 int nfrags = skb_shinfo(skb)->nr_frags;
2214 int err = -ENOMEM;
2215 int i = 0;
2216 int pos;
2218 __skb_push(skb, doffset);
2219 headroom = skb_headroom(skb);
2220 pos = skb_headlen(skb);
2222 do {
2223 struct sk_buff *nskb;
2224 skb_frag_t *frag;
2225 int hsize;
2226 int k;
2227 int size;
2229 len = skb->len - offset;
2230 if (len > mss)
2231 len = mss;
2233 hsize = skb_headlen(skb) - offset;
2234 if (hsize < 0)
2235 hsize = 0;
2236 if (hsize > len || !sg)
2237 hsize = len;
2239 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
2240 if (unlikely(!nskb))
2241 goto err;
2243 if (segs)
2244 tail->next = nskb;
2245 else
2246 segs = nskb;
2247 tail = nskb;
2249 __copy_skb_header(nskb, skb);
2250 nskb->mac_len = skb->mac_len;
2252 skb_reserve(nskb, headroom);
2253 skb_reset_mac_header(nskb);
2254 skb_set_network_header(nskb, skb->mac_len);
2255 nskb->transport_header = (nskb->network_header +
2256 skb_network_header_len(skb));
2257 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2258 doffset);
2259 if (!sg) {
2260 nskb->ip_summed = CHECKSUM_NONE;
2261 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2262 skb_put(nskb, len),
2263 len, 0);
2264 continue;
2267 frag = skb_shinfo(nskb)->frags;
2268 k = 0;
2270 skb_copy_from_linear_data_offset(skb, offset,
2271 skb_put(nskb, hsize), hsize);
2273 while (pos < offset + len) {
2274 BUG_ON(i >= nfrags);
2276 *frag = skb_shinfo(skb)->frags[i];
2277 get_page(frag->page);
2278 size = frag->size;
2280 if (pos < offset) {
2281 frag->page_offset += offset - pos;
2282 frag->size -= offset - pos;
2285 k++;
2287 if (pos + size <= offset + len) {
2288 i++;
2289 pos += size;
2290 } else {
2291 frag->size -= pos + size - (offset + len);
2292 break;
2295 frag++;
2298 skb_shinfo(nskb)->nr_frags = k;
2299 nskb->data_len = len - hsize;
2300 nskb->len += nskb->data_len;
2301 nskb->truesize += nskb->data_len;
2302 } while ((offset += len) < skb->len);
2304 return segs;
2306 err:
2307 while ((skb = segs)) {
2308 segs = skb->next;
2309 kfree_skb(skb);
2311 return ERR_PTR(err);
2314 EXPORT_SYMBOL_GPL(skb_segment);
2316 void __init skb_init(void)
2318 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2319 sizeof(struct sk_buff),
2321 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2322 NULL);
2323 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2324 (2*sizeof(struct sk_buff)) +
2325 sizeof(atomic_t),
2327 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2328 NULL);
2332 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2333 * @skb: Socket buffer containing the buffers to be mapped
2334 * @sg: The scatter-gather list to map into
2335 * @offset: The offset into the buffer's contents to start mapping
2336 * @len: Length of buffer space to be mapped
2338 * Fill the specified scatter-gather list with mappings/pointers into a
2339 * region of the buffer space attached to a socket buffer.
2341 static int
2342 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2344 int start = skb_headlen(skb);
2345 int i, copy = start - offset;
2346 int elt = 0;
2348 if (copy > 0) {
2349 if (copy > len)
2350 copy = len;
2351 sg_set_buf(sg, skb->data + offset, copy);
2352 elt++;
2353 if ((len -= copy) == 0)
2354 return elt;
2355 offset += copy;
2358 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2359 int end;
2361 WARN_ON(start > offset + len);
2363 end = start + skb_shinfo(skb)->frags[i].size;
2364 if ((copy = end - offset) > 0) {
2365 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2367 if (copy > len)
2368 copy = len;
2369 sg_set_page(&sg[elt], frag->page, copy,
2370 frag->page_offset+offset-start);
2371 elt++;
2372 if (!(len -= copy))
2373 return elt;
2374 offset += copy;
2376 start = end;
2379 if (skb_shinfo(skb)->frag_list) {
2380 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2382 for (; list; list = list->next) {
2383 int end;
2385 WARN_ON(start > offset + len);
2387 end = start + list->len;
2388 if ((copy = end - offset) > 0) {
2389 if (copy > len)
2390 copy = len;
2391 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2392 copy);
2393 if ((len -= copy) == 0)
2394 return elt;
2395 offset += copy;
2397 start = end;
2400 BUG_ON(len);
2401 return elt;
2404 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2406 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2408 sg_mark_end(&sg[nsg - 1]);
2410 return nsg;
2414 * skb_cow_data - Check that a socket buffer's data buffers are writable
2415 * @skb: The socket buffer to check.
2416 * @tailbits: Amount of trailing space to be added
2417 * @trailer: Returned pointer to the skb where the @tailbits space begins
2419 * Make sure that the data buffers attached to a socket buffer are
2420 * writable. If they are not, private copies are made of the data buffers
2421 * and the socket buffer is set to use these instead.
2423 * If @tailbits is given, make sure that there is space to write @tailbits
2424 * bytes of data beyond current end of socket buffer. @trailer will be
2425 * set to point to the skb in which this space begins.
2427 * The number of scatterlist elements required to completely map the
2428 * COW'd and extended socket buffer will be returned.
2430 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2432 int copyflag;
2433 int elt;
2434 struct sk_buff *skb1, **skb_p;
2436 /* If skb is cloned or its head is paged, reallocate
2437 * head pulling out all the pages (pages are considered not writable
2438 * at the moment even if they are anonymous).
2440 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2441 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2442 return -ENOMEM;
2444 /* Easy case. Most of packets will go this way. */
2445 if (!skb_shinfo(skb)->frag_list) {
2446 /* A little of trouble, not enough of space for trailer.
2447 * This should not happen, when stack is tuned to generate
2448 * good frames. OK, on miss we reallocate and reserve even more
2449 * space, 128 bytes is fair. */
2451 if (skb_tailroom(skb) < tailbits &&
2452 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2453 return -ENOMEM;
2455 /* Voila! */
2456 *trailer = skb;
2457 return 1;
2460 /* Misery. We are in troubles, going to mincer fragments... */
2462 elt = 1;
2463 skb_p = &skb_shinfo(skb)->frag_list;
2464 copyflag = 0;
2466 while ((skb1 = *skb_p) != NULL) {
2467 int ntail = 0;
2469 /* The fragment is partially pulled by someone,
2470 * this can happen on input. Copy it and everything
2471 * after it. */
2473 if (skb_shared(skb1))
2474 copyflag = 1;
2476 /* If the skb is the last, worry about trailer. */
2478 if (skb1->next == NULL && tailbits) {
2479 if (skb_shinfo(skb1)->nr_frags ||
2480 skb_shinfo(skb1)->frag_list ||
2481 skb_tailroom(skb1) < tailbits)
2482 ntail = tailbits + 128;
2485 if (copyflag ||
2486 skb_cloned(skb1) ||
2487 ntail ||
2488 skb_shinfo(skb1)->nr_frags ||
2489 skb_shinfo(skb1)->frag_list) {
2490 struct sk_buff *skb2;
2492 /* Fuck, we are miserable poor guys... */
2493 if (ntail == 0)
2494 skb2 = skb_copy(skb1, GFP_ATOMIC);
2495 else
2496 skb2 = skb_copy_expand(skb1,
2497 skb_headroom(skb1),
2498 ntail,
2499 GFP_ATOMIC);
2500 if (unlikely(skb2 == NULL))
2501 return -ENOMEM;
2503 if (skb1->sk)
2504 skb_set_owner_w(skb2, skb1->sk);
2506 /* Looking around. Are we still alive?
2507 * OK, link new skb, drop old one */
2509 skb2->next = skb1->next;
2510 *skb_p = skb2;
2511 kfree_skb(skb1);
2512 skb1 = skb2;
2514 elt++;
2515 *trailer = skb1;
2516 skb_p = &skb1->next;
2519 return elt;
2523 * skb_partial_csum_set - set up and verify partial csum values for packet
2524 * @skb: the skb to set
2525 * @start: the number of bytes after skb->data to start checksumming.
2526 * @off: the offset from start to place the checksum.
2528 * For untrusted partially-checksummed packets, we need to make sure the values
2529 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2531 * This function checks and sets those values and skb->ip_summed: if this
2532 * returns false you should drop the packet.
2534 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2536 if (unlikely(start > skb->len - 2) ||
2537 unlikely((int)start + off > skb->len - 2)) {
2538 if (net_ratelimit())
2539 printk(KERN_WARNING
2540 "bad partial csum: csum=%u/%u len=%u\n",
2541 start, off, skb->len);
2542 return false;
2544 skb->ip_summed = CHECKSUM_PARTIAL;
2545 skb->csum_start = skb_headroom(skb) + start;
2546 skb->csum_offset = off;
2547 return true;
2550 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2552 if (net_ratelimit())
2553 pr_warning("%s: received packets cannot be forwarded"
2554 " while LRO is enabled\n", skb->dev->name);
2557 EXPORT_SYMBOL(___pskb_trim);
2558 EXPORT_SYMBOL(__kfree_skb);
2559 EXPORT_SYMBOL(kfree_skb);
2560 EXPORT_SYMBOL(__pskb_pull_tail);
2561 EXPORT_SYMBOL(__alloc_skb);
2562 EXPORT_SYMBOL(__netdev_alloc_skb);
2563 EXPORT_SYMBOL(pskb_copy);
2564 EXPORT_SYMBOL(pskb_expand_head);
2565 EXPORT_SYMBOL(skb_checksum);
2566 EXPORT_SYMBOL(skb_clone);
2567 EXPORT_SYMBOL(skb_copy);
2568 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2569 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2570 EXPORT_SYMBOL(skb_copy_bits);
2571 EXPORT_SYMBOL(skb_copy_expand);
2572 EXPORT_SYMBOL(skb_over_panic);
2573 EXPORT_SYMBOL(skb_pad);
2574 EXPORT_SYMBOL(skb_realloc_headroom);
2575 EXPORT_SYMBOL(skb_under_panic);
2576 EXPORT_SYMBOL(skb_dequeue);
2577 EXPORT_SYMBOL(skb_dequeue_tail);
2578 EXPORT_SYMBOL(skb_insert);
2579 EXPORT_SYMBOL(skb_queue_purge);
2580 EXPORT_SYMBOL(skb_queue_head);
2581 EXPORT_SYMBOL(skb_queue_tail);
2582 EXPORT_SYMBOL(skb_unlink);
2583 EXPORT_SYMBOL(skb_append);
2584 EXPORT_SYMBOL(skb_split);
2585 EXPORT_SYMBOL(skb_prepare_seq_read);
2586 EXPORT_SYMBOL(skb_seq_read);
2587 EXPORT_SYMBOL(skb_abort_seq_read);
2588 EXPORT_SYMBOL(skb_find_text);
2589 EXPORT_SYMBOL(skb_append_datato_frags);
2590 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
2592 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2593 EXPORT_SYMBOL_GPL(skb_cow_data);
2594 EXPORT_SYMBOL_GPL(skb_partial_csum_set);