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>
8 * Alan Cox : Fixed the worst of the load
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
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>
43 #include <linux/interrupt.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>
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>
62 #include <net/checksum.h>
65 #include <asm/uaccess.h>
66 #include <asm/system.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 struct sk_buff *skb = (struct sk_buff *) buf->private;
81 static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
82 struct pipe_buffer *buf)
84 struct sk_buff *skb = (struct sk_buff *) buf->private;
89 static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
90 struct pipe_buffer *buf)
96 /* Pipe buffer operations for a socket. */
97 static struct pipe_buf_operations sock_pipe_buf_ops = {
99 .map = generic_pipe_buf_map,
100 .unmap = generic_pipe_buf_unmap,
101 .confirm = generic_pipe_buf_confirm,
102 .release = sock_pipe_buf_release,
103 .steal = sock_pipe_buf_steal,
104 .get = sock_pipe_buf_get,
108 * Keep out-of-line to prevent kernel bloat.
109 * __builtin_return_address is not used because it is not always
114 * skb_over_panic - private function
119 * Out of line support code for skb_put(). Not user callable.
121 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
123 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
124 "data:%p tail:%#lx end:%#lx dev:%s\n",
125 here, skb->len, sz, skb->head, skb->data,
126 (unsigned long)skb->tail, (unsigned long)skb->end,
127 skb->dev ? skb->dev->name : "<NULL>");
132 * skb_under_panic - private function
137 * Out of line support code for skb_push(). Not user callable.
140 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
142 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
143 "data:%p tail:%#lx end:%#lx dev:%s\n",
144 here, skb->len, sz, skb->head, skb->data,
145 (unsigned long)skb->tail, (unsigned long)skb->end,
146 skb->dev ? skb->dev->name : "<NULL>");
150 void skb_truesize_bug(struct sk_buff *skb)
152 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
153 "len=%u, sizeof(sk_buff)=%Zd\n",
154 skb->truesize, skb->len, sizeof(struct sk_buff));
156 EXPORT_SYMBOL(skb_truesize_bug);
158 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
159 * 'private' fields and also do memory statistics to find all the
165 * __alloc_skb - allocate a network buffer
166 * @size: size to allocate
167 * @gfp_mask: allocation mask
168 * @fclone: allocate from fclone cache instead of head cache
169 * and allocate a cloned (child) skb
170 * @node: numa node to allocate memory on
172 * Allocate a new &sk_buff. The returned buffer has no headroom and a
173 * tail room of size bytes. The object has a reference count of one.
174 * The return is the buffer. On a failure the return is %NULL.
176 * Buffers may only be allocated from interrupts using a @gfp_mask of
179 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
180 int fclone, int node)
182 struct kmem_cache *cache;
183 struct skb_shared_info *shinfo;
187 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
190 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
194 size = SKB_DATA_ALIGN(size);
195 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
201 * Only clear those fields we need to clear, not those that we will
202 * actually initialise below. Hence, don't put any more fields after
203 * the tail pointer in struct sk_buff!
205 memset(skb, 0, offsetof(struct sk_buff, tail));
206 skb->truesize = size + sizeof(struct sk_buff);
207 atomic_set(&skb->users, 1);
210 skb_reset_tail_pointer(skb);
211 skb->end = skb->tail + size;
212 /* make sure we initialize shinfo sequentially */
213 shinfo = skb_shinfo(skb);
214 atomic_set(&shinfo->dataref, 1);
215 shinfo->nr_frags = 0;
216 shinfo->gso_size = 0;
217 shinfo->gso_segs = 0;
218 shinfo->gso_type = 0;
219 shinfo->ip6_frag_id = 0;
220 shinfo->frag_list = NULL;
223 struct sk_buff *child = skb + 1;
224 atomic_t *fclone_ref = (atomic_t *) (child + 1);
226 skb->fclone = SKB_FCLONE_ORIG;
227 atomic_set(fclone_ref, 1);
229 child->fclone = SKB_FCLONE_UNAVAILABLE;
234 kmem_cache_free(cache, skb);
240 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
241 * @dev: network device to receive on
242 * @length: length to allocate
243 * @gfp_mask: get_free_pages mask, passed to alloc_skb
245 * Allocate a new &sk_buff and assign it a usage count of one. The
246 * buffer has unspecified headroom built in. Users should allocate
247 * the headroom they think they need without accounting for the
248 * built in space. The built in space is used for optimisations.
250 * %NULL is returned if there is no free memory.
252 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
253 unsigned int length, gfp_t gfp_mask)
255 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
258 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
260 skb_reserve(skb, NET_SKB_PAD);
267 * dev_alloc_skb - allocate an skbuff for receiving
268 * @length: length to allocate
270 * Allocate a new &sk_buff and assign it a usage count of one. The
271 * buffer has unspecified headroom built in. Users should allocate
272 * the headroom they think they need without accounting for the
273 * built in space. The built in space is used for optimisations.
275 * %NULL is returned if there is no free memory. Although this function
276 * allocates memory it can be called from an interrupt.
278 struct sk_buff *dev_alloc_skb(unsigned int length)
281 * There is more code here than it seems:
282 * __dev_alloc_skb is an inline
284 return __dev_alloc_skb(length, GFP_ATOMIC);
286 EXPORT_SYMBOL(dev_alloc_skb);
288 static void skb_drop_list(struct sk_buff **listp)
290 struct sk_buff *list = *listp;
295 struct sk_buff *this = 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)
314 static void skb_release_data(struct sk_buff *skb)
317 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
318 &skb_shinfo(skb)->dataref)) {
319 if (skb_shinfo(skb)->nr_frags) {
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);
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);
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);
351 case SKB_FCLONE_CLONE:
352 fclone_ref = (atomic_t *) (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);
366 /* Free everything but the sk_buff shell. */
367 static void skb_release_all(struct sk_buff *skb)
369 dst_release(skb->dst);
371 secpath_put(skb->sp);
373 if (skb->destructor) {
375 skb->destructor(skb);
377 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
378 nf_conntrack_put(skb->nfct);
379 nf_conntrack_put_reasm(skb->nfct_reasm);
381 #ifdef CONFIG_BRIDGE_NETFILTER
382 nf_bridge_put(skb->nf_bridge);
384 /* XXX: IS this still necessary? - JHS */
385 #ifdef CONFIG_NET_SCHED
387 #ifdef CONFIG_NET_CLS_ACT
391 skb_release_data(skb);
395 * __kfree_skb - private function
398 * Free an sk_buff. Release anything attached to the buffer.
399 * Clean the state. This is an internal helper function. Users should
400 * always call kfree_skb
403 void __kfree_skb(struct sk_buff *skb)
405 skb_release_all(skb);
410 * kfree_skb - free an sk_buff
411 * @skb: buffer to free
413 * Drop a reference to the buffer and free it if the usage count has
416 void kfree_skb(struct sk_buff *skb)
420 if (likely(atomic_read(&skb->users) == 1))
422 else if (likely(!atomic_dec_and_test(&skb->users)))
427 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
429 new->tstamp = old->tstamp;
431 new->transport_header = old->transport_header;
432 new->network_header = old->network_header;
433 new->mac_header = old->mac_header;
434 new->dst = dst_clone(old->dst);
436 new->sp = secpath_get(old->sp);
438 memcpy(new->cb, old->cb, sizeof(old->cb));
439 new->csum_start = old->csum_start;
440 new->csum_offset = old->csum_offset;
441 new->local_df = old->local_df;
442 new->pkt_type = old->pkt_type;
443 new->ip_summed = old->ip_summed;
444 skb_copy_queue_mapping(new, old);
445 new->priority = old->priority;
446 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
447 new->ipvs_property = old->ipvs_property;
449 new->protocol = old->protocol;
450 new->mark = old->mark;
452 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
453 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
454 new->nf_trace = old->nf_trace;
456 #ifdef CONFIG_NET_SCHED
457 new->tc_index = old->tc_index;
458 #ifdef CONFIG_NET_CLS_ACT
459 new->tc_verd = old->tc_verd;
462 skb_copy_secmark(new, old);
465 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
467 #define C(x) n->x = skb->x
469 n->next = n->prev = NULL;
471 __copy_skb_header(n, skb);
476 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
479 n->destructor = NULL;
486 atomic_set(&n->users, 1);
488 atomic_inc(&(skb_shinfo(skb)->dataref));
496 * skb_morph - morph one skb into another
497 * @dst: the skb to receive the contents
498 * @src: the skb to supply the contents
500 * This is identical to skb_clone except that the target skb is
501 * supplied by the user.
503 * The target skb is returned upon exit.
505 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
507 skb_release_all(dst);
508 return __skb_clone(dst, src);
510 EXPORT_SYMBOL_GPL(skb_morph);
513 * skb_clone - duplicate an sk_buff
514 * @skb: buffer to clone
515 * @gfp_mask: allocation priority
517 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
518 * copies share the same packet data but not structure. The new
519 * buffer has a reference count of 1. If the allocation fails the
520 * function returns %NULL otherwise the new buffer is returned.
522 * If this function is called from an interrupt gfp_mask() must be
526 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
531 if (skb->fclone == SKB_FCLONE_ORIG &&
532 n->fclone == SKB_FCLONE_UNAVAILABLE) {
533 atomic_t *fclone_ref = (atomic_t *) (n + 1);
534 n->fclone = SKB_FCLONE_CLONE;
535 atomic_inc(fclone_ref);
537 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
540 n->fclone = SKB_FCLONE_UNAVAILABLE;
543 return __skb_clone(n, skb);
546 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
548 #ifndef NET_SKBUFF_DATA_USES_OFFSET
550 * Shift between the two data areas in bytes
552 unsigned long offset = new->data - old->data;
555 __copy_skb_header(new, old);
557 #ifndef NET_SKBUFF_DATA_USES_OFFSET
558 /* {transport,network,mac}_header are relative to skb->head */
559 new->transport_header += offset;
560 new->network_header += offset;
561 new->mac_header += offset;
563 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
564 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
565 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
569 * skb_copy - create private copy of an sk_buff
570 * @skb: buffer to copy
571 * @gfp_mask: allocation priority
573 * Make a copy of both an &sk_buff and its data. This is used when the
574 * caller wishes to modify the data and needs a private copy of the
575 * data to alter. Returns %NULL on failure or the pointer to the buffer
576 * on success. The returned buffer has a reference count of 1.
578 * As by-product this function converts non-linear &sk_buff to linear
579 * one, so that &sk_buff becomes completely private and caller is allowed
580 * to modify all the data of returned buffer. This means that this
581 * function is not recommended for use in circumstances when only
582 * header is going to be modified. Use pskb_copy() instead.
585 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
587 int headerlen = skb->data - skb->head;
589 * Allocate the copy buffer
592 #ifdef NET_SKBUFF_DATA_USES_OFFSET
593 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
595 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
600 /* Set the data pointer */
601 skb_reserve(n, headerlen);
602 /* Set the tail pointer and length */
603 skb_put(n, skb->len);
605 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
608 copy_skb_header(n, skb);
614 * pskb_copy - create copy of an sk_buff with private head.
615 * @skb: buffer to copy
616 * @gfp_mask: allocation priority
618 * Make a copy of both an &sk_buff and part of its data, located
619 * in header. Fragmented data remain shared. This is used when
620 * the caller wishes to modify only header of &sk_buff and needs
621 * private copy of the header to alter. Returns %NULL on failure
622 * or the pointer to the buffer on success.
623 * The returned buffer has a reference count of 1.
626 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
629 * Allocate the copy buffer
632 #ifdef NET_SKBUFF_DATA_USES_OFFSET
633 n = alloc_skb(skb->end, gfp_mask);
635 n = alloc_skb(skb->end - skb->head, gfp_mask);
640 /* Set the data pointer */
641 skb_reserve(n, skb->data - skb->head);
642 /* Set the tail pointer and length */
643 skb_put(n, skb_headlen(skb));
645 skb_copy_from_linear_data(skb, n->data, n->len);
647 n->truesize += skb->data_len;
648 n->data_len = skb->data_len;
651 if (skb_shinfo(skb)->nr_frags) {
654 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
655 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
656 get_page(skb_shinfo(n)->frags[i].page);
658 skb_shinfo(n)->nr_frags = i;
661 if (skb_shinfo(skb)->frag_list) {
662 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
663 skb_clone_fraglist(n);
666 copy_skb_header(n, skb);
672 * pskb_expand_head - reallocate header of &sk_buff
673 * @skb: buffer to reallocate
674 * @nhead: room to add at head
675 * @ntail: room to add at tail
676 * @gfp_mask: allocation priority
678 * Expands (or creates identical copy, if &nhead and &ntail are zero)
679 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
680 * reference count of 1. Returns zero in the case of success or error,
681 * if expansion failed. In the last case, &sk_buff is not changed.
683 * All the pointers pointing into skb header may change and must be
684 * reloaded after call to this function.
687 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
692 #ifdef NET_SKBUFF_DATA_USES_OFFSET
693 int size = nhead + skb->end + ntail;
695 int size = nhead + (skb->end - skb->head) + ntail;
702 size = SKB_DATA_ALIGN(size);
704 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
708 /* Copy only real data... and, alas, header. This should be
709 * optimized for the cases when header is void. */
710 #ifdef NET_SKBUFF_DATA_USES_OFFSET
711 memcpy(data + nhead, skb->head, skb->tail);
713 memcpy(data + nhead, skb->head, skb->tail - skb->head);
715 memcpy(data + size, skb_end_pointer(skb),
716 sizeof(struct skb_shared_info));
718 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
719 get_page(skb_shinfo(skb)->frags[i].page);
721 if (skb_shinfo(skb)->frag_list)
722 skb_clone_fraglist(skb);
724 skb_release_data(skb);
726 off = (data + nhead) - skb->head;
730 #ifdef NET_SKBUFF_DATA_USES_OFFSET
734 skb->end = skb->head + size;
736 /* {transport,network,mac}_header and tail are relative to skb->head */
738 skb->transport_header += off;
739 skb->network_header += off;
740 skb->mac_header += off;
741 skb->csum_start += nhead;
745 atomic_set(&skb_shinfo(skb)->dataref, 1);
752 /* Make private copy of skb with writable head and some headroom */
754 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
756 struct sk_buff *skb2;
757 int delta = headroom - skb_headroom(skb);
760 skb2 = pskb_copy(skb, GFP_ATOMIC);
762 skb2 = skb_clone(skb, GFP_ATOMIC);
763 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
774 * skb_copy_expand - copy and expand sk_buff
775 * @skb: buffer to copy
776 * @newheadroom: new free bytes at head
777 * @newtailroom: new free bytes at tail
778 * @gfp_mask: allocation priority
780 * Make a copy of both an &sk_buff and its data and while doing so
781 * allocate additional space.
783 * This is used when the caller wishes to modify the data and needs a
784 * private copy of the data to alter as well as more space for new fields.
785 * Returns %NULL on failure or the pointer to the buffer
786 * on success. The returned buffer has a reference count of 1.
788 * You must pass %GFP_ATOMIC as the allocation priority if this function
789 * is called from an interrupt.
791 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
792 int newheadroom, int newtailroom,
796 * Allocate the copy buffer
798 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
800 int oldheadroom = skb_headroom(skb);
801 int head_copy_len, head_copy_off;
807 skb_reserve(n, newheadroom);
809 /* Set the tail pointer and length */
810 skb_put(n, skb->len);
812 head_copy_len = oldheadroom;
814 if (newheadroom <= head_copy_len)
815 head_copy_len = newheadroom;
817 head_copy_off = newheadroom - head_copy_len;
819 /* Copy the linear header and data. */
820 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
821 skb->len + head_copy_len))
824 copy_skb_header(n, skb);
826 off = newheadroom - oldheadroom;
827 n->csum_start += off;
828 #ifdef NET_SKBUFF_DATA_USES_OFFSET
829 n->transport_header += off;
830 n->network_header += off;
831 n->mac_header += off;
838 * skb_pad - zero pad the tail of an skb
839 * @skb: buffer to pad
842 * Ensure that a buffer is followed by a padding area that is zero
843 * filled. Used by network drivers which may DMA or transfer data
844 * beyond the buffer end onto the wire.
846 * May return error in out of memory cases. The skb is freed on error.
849 int skb_pad(struct sk_buff *skb, int pad)
854 /* If the skbuff is non linear tailroom is always zero.. */
855 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
856 memset(skb->data+skb->len, 0, pad);
860 ntail = skb->data_len + pad - (skb->end - skb->tail);
861 if (likely(skb_cloned(skb) || ntail > 0)) {
862 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
867 /* FIXME: The use of this function with non-linear skb's really needs
870 err = skb_linearize(skb);
874 memset(skb->data + skb->len, 0, pad);
883 * skb_put - add data to a buffer
884 * @skb: buffer to use
885 * @len: amount of data to add
887 * This function extends the used data area of the buffer. If this would
888 * exceed the total buffer size the kernel will panic. A pointer to the
889 * first byte of the extra data is returned.
891 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
893 unsigned char *tmp = skb_tail_pointer(skb);
894 SKB_LINEAR_ASSERT(skb);
897 if (unlikely(skb->tail > skb->end))
898 skb_over_panic(skb, len, __builtin_return_address(0));
901 EXPORT_SYMBOL(skb_put);
904 * skb_push - add data to the start of a buffer
905 * @skb: buffer to use
906 * @len: amount of data to add
908 * This function extends the used data area of the buffer at the buffer
909 * start. If this would exceed the total buffer headroom the kernel will
910 * panic. A pointer to the first byte of the extra data is returned.
912 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
916 if (unlikely(skb->data<skb->head))
917 skb_under_panic(skb, len, __builtin_return_address(0));
920 EXPORT_SYMBOL(skb_push);
923 * skb_pull - remove data from the start of a buffer
924 * @skb: buffer to use
925 * @len: amount of data to remove
927 * This function removes data from the start of a buffer, returning
928 * the memory to the headroom. A pointer to the next data in the buffer
929 * is returned. Once the data has been pulled future pushes will overwrite
932 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
934 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
936 EXPORT_SYMBOL(skb_pull);
939 * skb_trim - remove end from a buffer
940 * @skb: buffer to alter
943 * Cut the length of a buffer down by removing data from the tail. If
944 * the buffer is already under the length specified it is not modified.
945 * The skb must be linear.
947 void skb_trim(struct sk_buff *skb, unsigned int len)
950 __skb_trim(skb, len);
952 EXPORT_SYMBOL(skb_trim);
954 /* Trims skb to length len. It can change skb pointers.
957 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
959 struct sk_buff **fragp;
960 struct sk_buff *frag;
961 int offset = skb_headlen(skb);
962 int nfrags = skb_shinfo(skb)->nr_frags;
966 if (skb_cloned(skb) &&
967 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
974 for (; i < nfrags; i++) {
975 int end = offset + skb_shinfo(skb)->frags[i].size;
982 skb_shinfo(skb)->frags[i++].size = len - offset;
985 skb_shinfo(skb)->nr_frags = i;
987 for (; i < nfrags; i++)
988 put_page(skb_shinfo(skb)->frags[i].page);
990 if (skb_shinfo(skb)->frag_list)
991 skb_drop_fraglist(skb);
995 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
996 fragp = &frag->next) {
997 int end = offset + frag->len;
999 if (skb_shared(frag)) {
1000 struct sk_buff *nfrag;
1002 nfrag = skb_clone(frag, GFP_ATOMIC);
1003 if (unlikely(!nfrag))
1006 nfrag->next = frag->next;
1018 unlikely((err = pskb_trim(frag, len - offset))))
1022 skb_drop_list(&frag->next);
1027 if (len > skb_headlen(skb)) {
1028 skb->data_len -= skb->len - len;
1033 skb_set_tail_pointer(skb, len);
1040 * __pskb_pull_tail - advance tail of skb header
1041 * @skb: buffer to reallocate
1042 * @delta: number of bytes to advance tail
1044 * The function makes a sense only on a fragmented &sk_buff,
1045 * it expands header moving its tail forward and copying necessary
1046 * data from fragmented part.
1048 * &sk_buff MUST have reference count of 1.
1050 * Returns %NULL (and &sk_buff does not change) if pull failed
1051 * or value of new tail of skb in the case of success.
1053 * All the pointers pointing into skb header may change and must be
1054 * reloaded after call to this function.
1057 /* Moves tail of skb head forward, copying data from fragmented part,
1058 * when it is necessary.
1059 * 1. It may fail due to malloc failure.
1060 * 2. It may change skb pointers.
1062 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1064 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1066 /* If skb has not enough free space at tail, get new one
1067 * plus 128 bytes for future expansions. If we have enough
1068 * room at tail, reallocate without expansion only if skb is cloned.
1070 int i, k, eat = (skb->tail + delta) - skb->end;
1072 if (eat > 0 || skb_cloned(skb)) {
1073 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1078 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1081 /* Optimization: no fragments, no reasons to preestimate
1082 * size of pulled pages. Superb.
1084 if (!skb_shinfo(skb)->frag_list)
1087 /* Estimate size of pulled pages. */
1089 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1090 if (skb_shinfo(skb)->frags[i].size >= eat)
1092 eat -= skb_shinfo(skb)->frags[i].size;
1095 /* If we need update frag list, we are in troubles.
1096 * Certainly, it possible to add an offset to skb data,
1097 * but taking into account that pulling is expected to
1098 * be very rare operation, it is worth to fight against
1099 * further bloating skb head and crucify ourselves here instead.
1100 * Pure masohism, indeed. 8)8)
1103 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1104 struct sk_buff *clone = NULL;
1105 struct sk_buff *insp = NULL;
1110 if (list->len <= eat) {
1111 /* Eaten as whole. */
1116 /* Eaten partially. */
1118 if (skb_shared(list)) {
1119 /* Sucks! We need to fork list. :-( */
1120 clone = skb_clone(list, GFP_ATOMIC);
1126 /* This may be pulled without
1130 if (!pskb_pull(list, eat)) {
1139 /* Free pulled out fragments. */
1140 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1141 skb_shinfo(skb)->frag_list = list->next;
1144 /* And insert new clone at head. */
1147 skb_shinfo(skb)->frag_list = clone;
1150 /* Success! Now we may commit changes to skb data. */
1155 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1156 if (skb_shinfo(skb)->frags[i].size <= eat) {
1157 put_page(skb_shinfo(skb)->frags[i].page);
1158 eat -= skb_shinfo(skb)->frags[i].size;
1160 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1162 skb_shinfo(skb)->frags[k].page_offset += eat;
1163 skb_shinfo(skb)->frags[k].size -= eat;
1169 skb_shinfo(skb)->nr_frags = k;
1172 skb->data_len -= delta;
1174 return skb_tail_pointer(skb);
1177 /* Copy some data bits from skb to kernel buffer. */
1179 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1182 int start = skb_headlen(skb);
1184 if (offset > (int)skb->len - len)
1188 if ((copy = start - offset) > 0) {
1191 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1192 if ((len -= copy) == 0)
1198 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1201 BUG_TRAP(start <= offset + len);
1203 end = start + skb_shinfo(skb)->frags[i].size;
1204 if ((copy = end - offset) > 0) {
1210 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1212 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1213 offset - start, copy);
1214 kunmap_skb_frag(vaddr);
1216 if ((len -= copy) == 0)
1224 if (skb_shinfo(skb)->frag_list) {
1225 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1227 for (; list; list = list->next) {
1230 BUG_TRAP(start <= offset + len);
1232 end = start + list->len;
1233 if ((copy = end - offset) > 0) {
1236 if (skb_copy_bits(list, offset - start,
1239 if ((len -= copy) == 0)
1255 * Callback from splice_to_pipe(), if we need to release some pages
1256 * at the end of the spd in case we error'ed out in filling the pipe.
1258 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1260 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1266 * Fill page/offset/length into spd, if it can hold more pages.
1268 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1269 unsigned int len, unsigned int offset,
1270 struct sk_buff *skb)
1272 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1275 spd->pages[spd->nr_pages] = page;
1276 spd->partial[spd->nr_pages].len = len;
1277 spd->partial[spd->nr_pages].offset = offset;
1278 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1284 * Map linear and fragment data from the skb to spd. Returns number of
1287 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1288 unsigned int *total_len,
1289 struct splice_pipe_desc *spd)
1291 unsigned int nr_pages = spd->nr_pages;
1292 unsigned int poff, plen, len, toff, tlen;
1293 int headlen, seg, error = 0;
1303 * if the offset is greater than the linear part, go directly to
1306 headlen = skb_headlen(skb);
1307 if (toff >= headlen) {
1313 * first map the linear region into the pages/partial map, skipping
1314 * any potential initial offset.
1317 while (len < headlen) {
1318 void *p = skb->data + len;
1320 poff = (unsigned long) p & (PAGE_SIZE - 1);
1321 plen = min_t(unsigned int, headlen - len, PAGE_SIZE - poff);
1334 plen = min(plen, tlen);
1339 * just jump directly to update and return, no point
1340 * in going over fragments when the output is full.
1342 error = spd_fill_page(spd, virt_to_page(p), plen, poff, skb);
1350 * then map the fragments
1353 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1354 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1357 poff = f->page_offset;
1369 plen = min(plen, tlen);
1373 error = spd_fill_page(spd, f->page, plen, poff, skb);
1381 if (spd->nr_pages - nr_pages) {
1387 /* update the offset to reflect the linear part skip, if any */
1394 * Map data from the skb to a pipe. Should handle both the linear part,
1395 * the fragments, and the frag list. It does NOT handle frag lists within
1396 * the frag list, if such a thing exists. We'd probably need to recurse to
1397 * handle that cleanly.
1399 int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1400 struct pipe_inode_info *pipe, unsigned int tlen,
1403 struct partial_page partial[PIPE_BUFFERS];
1404 struct page *pages[PIPE_BUFFERS];
1405 struct splice_pipe_desc spd = {
1409 .ops = &sock_pipe_buf_ops,
1410 .spd_release = sock_spd_release,
1412 struct sk_buff *skb;
1415 * I'd love to avoid the clone here, but tcp_read_sock()
1416 * ignores reference counts and unconditonally kills the sk_buff
1417 * on return from the actor.
1419 skb = skb_clone(__skb, GFP_KERNEL);
1424 * __skb_splice_bits() only fails if the output has no room left,
1425 * so no point in going over the frag_list for the error case.
1427 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1433 * now see if we have a frag_list to map
1435 if (skb_shinfo(skb)->frag_list) {
1436 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1438 for (; list && tlen; list = list->next) {
1439 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1446 * drop our reference to the clone, the pipe consumption will
1453 struct sock *sk = __skb->sk;
1456 * Drop the socket lock, otherwise we have reverse
1457 * locking dependencies between sk_lock and i_mutex
1458 * here as compared to sendfile(). We enter here
1459 * with the socket lock held, and splice_to_pipe() will
1460 * grab the pipe inode lock. For sendfile() emulation,
1461 * we call into ->sendpage() with the i_mutex lock held
1462 * and networking will grab the socket lock.
1465 ret = splice_to_pipe(pipe, &spd);
1474 * skb_store_bits - store bits from kernel buffer to skb
1475 * @skb: destination buffer
1476 * @offset: offset in destination
1477 * @from: source buffer
1478 * @len: number of bytes to copy
1480 * Copy the specified number of bytes from the source buffer to the
1481 * destination skb. This function handles all the messy bits of
1482 * traversing fragment lists and such.
1485 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1488 int start = skb_headlen(skb);
1490 if (offset > (int)skb->len - len)
1493 if ((copy = start - offset) > 0) {
1496 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1497 if ((len -= copy) == 0)
1503 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1504 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1507 BUG_TRAP(start <= offset + len);
1509 end = start + frag->size;
1510 if ((copy = end - offset) > 0) {
1516 vaddr = kmap_skb_frag(frag);
1517 memcpy(vaddr + frag->page_offset + offset - start,
1519 kunmap_skb_frag(vaddr);
1521 if ((len -= copy) == 0)
1529 if (skb_shinfo(skb)->frag_list) {
1530 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1532 for (; list; list = list->next) {
1535 BUG_TRAP(start <= offset + len);
1537 end = start + list->len;
1538 if ((copy = end - offset) > 0) {
1541 if (skb_store_bits(list, offset - start,
1544 if ((len -= copy) == 0)
1559 EXPORT_SYMBOL(skb_store_bits);
1561 /* Checksum skb data. */
1563 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1564 int len, __wsum csum)
1566 int start = skb_headlen(skb);
1567 int i, copy = start - offset;
1570 /* Checksum header. */
1574 csum = csum_partial(skb->data + offset, copy, csum);
1575 if ((len -= copy) == 0)
1581 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1584 BUG_TRAP(start <= offset + len);
1586 end = start + skb_shinfo(skb)->frags[i].size;
1587 if ((copy = end - offset) > 0) {
1590 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1594 vaddr = kmap_skb_frag(frag);
1595 csum2 = csum_partial(vaddr + frag->page_offset +
1596 offset - start, copy, 0);
1597 kunmap_skb_frag(vaddr);
1598 csum = csum_block_add(csum, csum2, pos);
1607 if (skb_shinfo(skb)->frag_list) {
1608 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1610 for (; list; list = list->next) {
1613 BUG_TRAP(start <= offset + len);
1615 end = start + list->len;
1616 if ((copy = end - offset) > 0) {
1620 csum2 = skb_checksum(list, offset - start,
1622 csum = csum_block_add(csum, csum2, pos);
1623 if ((len -= copy) == 0)
1636 /* Both of above in one bottle. */
1638 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1639 u8 *to, int len, __wsum csum)
1641 int start = skb_headlen(skb);
1642 int i, copy = start - offset;
1649 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1651 if ((len -= copy) == 0)
1658 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1661 BUG_TRAP(start <= offset + len);
1663 end = start + skb_shinfo(skb)->frags[i].size;
1664 if ((copy = end - offset) > 0) {
1667 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1671 vaddr = kmap_skb_frag(frag);
1672 csum2 = csum_partial_copy_nocheck(vaddr +
1676 kunmap_skb_frag(vaddr);
1677 csum = csum_block_add(csum, csum2, pos);
1687 if (skb_shinfo(skb)->frag_list) {
1688 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1690 for (; list; list = list->next) {
1694 BUG_TRAP(start <= offset + len);
1696 end = start + list->len;
1697 if ((copy = end - offset) > 0) {
1700 csum2 = skb_copy_and_csum_bits(list,
1703 csum = csum_block_add(csum, csum2, pos);
1704 if ((len -= copy) == 0)
1717 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1722 if (skb->ip_summed == CHECKSUM_PARTIAL)
1723 csstart = skb->csum_start - skb_headroom(skb);
1725 csstart = skb_headlen(skb);
1727 BUG_ON(csstart > skb_headlen(skb));
1729 skb_copy_from_linear_data(skb, to, csstart);
1732 if (csstart != skb->len)
1733 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1734 skb->len - csstart, 0);
1736 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1737 long csstuff = csstart + skb->csum_offset;
1739 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1744 * skb_dequeue - remove from the head of the queue
1745 * @list: list to dequeue from
1747 * Remove the head of the list. The list lock is taken so the function
1748 * may be used safely with other locking list functions. The head item is
1749 * returned or %NULL if the list is empty.
1752 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1754 unsigned long flags;
1755 struct sk_buff *result;
1757 spin_lock_irqsave(&list->lock, flags);
1758 result = __skb_dequeue(list);
1759 spin_unlock_irqrestore(&list->lock, flags);
1764 * skb_dequeue_tail - remove from the tail of the queue
1765 * @list: list to dequeue from
1767 * Remove the tail of the list. The list lock is taken so the function
1768 * may be used safely with other locking list functions. The tail item is
1769 * returned or %NULL if the list is empty.
1771 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1773 unsigned long flags;
1774 struct sk_buff *result;
1776 spin_lock_irqsave(&list->lock, flags);
1777 result = __skb_dequeue_tail(list);
1778 spin_unlock_irqrestore(&list->lock, flags);
1783 * skb_queue_purge - empty a list
1784 * @list: list to empty
1786 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1787 * the list and one reference dropped. This function takes the list
1788 * lock and is atomic with respect to other list locking functions.
1790 void skb_queue_purge(struct sk_buff_head *list)
1792 struct sk_buff *skb;
1793 while ((skb = skb_dequeue(list)) != NULL)
1798 * skb_queue_head - queue a buffer at the list head
1799 * @list: list to use
1800 * @newsk: buffer to queue
1802 * Queue a buffer at the start of the list. This function takes the
1803 * list lock and can be used safely with other locking &sk_buff functions
1806 * A buffer cannot be placed on two lists at the same time.
1808 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1810 unsigned long flags;
1812 spin_lock_irqsave(&list->lock, flags);
1813 __skb_queue_head(list, newsk);
1814 spin_unlock_irqrestore(&list->lock, flags);
1818 * skb_queue_tail - queue a buffer at the list tail
1819 * @list: list to use
1820 * @newsk: buffer to queue
1822 * Queue a buffer at the tail of the list. This function takes the
1823 * list lock and can be used safely with other locking &sk_buff functions
1826 * A buffer cannot be placed on two lists at the same time.
1828 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1830 unsigned long flags;
1832 spin_lock_irqsave(&list->lock, flags);
1833 __skb_queue_tail(list, newsk);
1834 spin_unlock_irqrestore(&list->lock, flags);
1838 * skb_unlink - remove a buffer from a list
1839 * @skb: buffer to remove
1840 * @list: list to use
1842 * Remove a packet from a list. The list locks are taken and this
1843 * function is atomic with respect to other list locked calls
1845 * You must know what list the SKB is on.
1847 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1849 unsigned long flags;
1851 spin_lock_irqsave(&list->lock, flags);
1852 __skb_unlink(skb, list);
1853 spin_unlock_irqrestore(&list->lock, flags);
1857 * skb_append - append a buffer
1858 * @old: buffer to insert after
1859 * @newsk: buffer to insert
1860 * @list: list to use
1862 * Place a packet after a given packet in a list. The list locks are taken
1863 * and this function is atomic with respect to other list locked calls.
1864 * A buffer cannot be placed on two lists at the same time.
1866 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1868 unsigned long flags;
1870 spin_lock_irqsave(&list->lock, flags);
1871 __skb_queue_after(list, old, newsk);
1872 spin_unlock_irqrestore(&list->lock, flags);
1877 * skb_insert - insert a buffer
1878 * @old: buffer to insert before
1879 * @newsk: buffer to insert
1880 * @list: list to use
1882 * Place a packet before a given packet in a list. The list locks are
1883 * taken and this function is atomic with respect to other list locked
1886 * A buffer cannot be placed on two lists at the same time.
1888 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1890 unsigned long flags;
1892 spin_lock_irqsave(&list->lock, flags);
1893 __skb_insert(newsk, old->prev, old, list);
1894 spin_unlock_irqrestore(&list->lock, flags);
1897 static inline void skb_split_inside_header(struct sk_buff *skb,
1898 struct sk_buff* skb1,
1899 const u32 len, const int pos)
1903 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1905 /* And move data appendix as is. */
1906 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1907 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1909 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1910 skb_shinfo(skb)->nr_frags = 0;
1911 skb1->data_len = skb->data_len;
1912 skb1->len += skb1->data_len;
1915 skb_set_tail_pointer(skb, len);
1918 static inline void skb_split_no_header(struct sk_buff *skb,
1919 struct sk_buff* skb1,
1920 const u32 len, int pos)
1923 const int nfrags = skb_shinfo(skb)->nr_frags;
1925 skb_shinfo(skb)->nr_frags = 0;
1926 skb1->len = skb1->data_len = skb->len - len;
1928 skb->data_len = len - pos;
1930 for (i = 0; i < nfrags; i++) {
1931 int size = skb_shinfo(skb)->frags[i].size;
1933 if (pos + size > len) {
1934 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1938 * We have two variants in this case:
1939 * 1. Move all the frag to the second
1940 * part, if it is possible. F.e.
1941 * this approach is mandatory for TUX,
1942 * where splitting is expensive.
1943 * 2. Split is accurately. We make this.
1945 get_page(skb_shinfo(skb)->frags[i].page);
1946 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1947 skb_shinfo(skb1)->frags[0].size -= len - pos;
1948 skb_shinfo(skb)->frags[i].size = len - pos;
1949 skb_shinfo(skb)->nr_frags++;
1953 skb_shinfo(skb)->nr_frags++;
1956 skb_shinfo(skb1)->nr_frags = k;
1960 * skb_split - Split fragmented skb to two parts at length len.
1961 * @skb: the buffer to split
1962 * @skb1: the buffer to receive the second part
1963 * @len: new length for skb
1965 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1967 int pos = skb_headlen(skb);
1969 if (len < pos) /* Split line is inside header. */
1970 skb_split_inside_header(skb, skb1, len, pos);
1971 else /* Second chunk has no header, nothing to copy. */
1972 skb_split_no_header(skb, skb1, len, pos);
1976 * skb_prepare_seq_read - Prepare a sequential read of skb data
1977 * @skb: the buffer to read
1978 * @from: lower offset of data to be read
1979 * @to: upper offset of data to be read
1980 * @st: state variable
1982 * Initializes the specified state variable. Must be called before
1983 * invoking skb_seq_read() for the first time.
1985 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1986 unsigned int to, struct skb_seq_state *st)
1988 st->lower_offset = from;
1989 st->upper_offset = to;
1990 st->root_skb = st->cur_skb = skb;
1991 st->frag_idx = st->stepped_offset = 0;
1992 st->frag_data = NULL;
1996 * skb_seq_read - Sequentially read skb data
1997 * @consumed: number of bytes consumed by the caller so far
1998 * @data: destination pointer for data to be returned
1999 * @st: state variable
2001 * Reads a block of skb data at &consumed relative to the
2002 * lower offset specified to skb_prepare_seq_read(). Assigns
2003 * the head of the data block to &data and returns the length
2004 * of the block or 0 if the end of the skb data or the upper
2005 * offset has been reached.
2007 * The caller is not required to consume all of the data
2008 * returned, i.e. &consumed is typically set to the number
2009 * of bytes already consumed and the next call to
2010 * skb_seq_read() will return the remaining part of the block.
2012 * Note 1: The size of each block of data returned can be arbitary,
2013 * this limitation is the cost for zerocopy seqeuental
2014 * reads of potentially non linear data.
2016 * Note 2: Fragment lists within fragments are not implemented
2017 * at the moment, state->root_skb could be replaced with
2018 * a stack for this purpose.
2020 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2021 struct skb_seq_state *st)
2023 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2026 if (unlikely(abs_offset >= st->upper_offset))
2030 block_limit = skb_headlen(st->cur_skb);
2032 if (abs_offset < block_limit) {
2033 *data = st->cur_skb->data + abs_offset;
2034 return block_limit - abs_offset;
2037 if (st->frag_idx == 0 && !st->frag_data)
2038 st->stepped_offset += skb_headlen(st->cur_skb);
2040 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2041 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2042 block_limit = frag->size + st->stepped_offset;
2044 if (abs_offset < block_limit) {
2046 st->frag_data = kmap_skb_frag(frag);
2048 *data = (u8 *) st->frag_data + frag->page_offset +
2049 (abs_offset - st->stepped_offset);
2051 return block_limit - abs_offset;
2054 if (st->frag_data) {
2055 kunmap_skb_frag(st->frag_data);
2056 st->frag_data = NULL;
2060 st->stepped_offset += frag->size;
2063 if (st->frag_data) {
2064 kunmap_skb_frag(st->frag_data);
2065 st->frag_data = NULL;
2068 if (st->cur_skb->next) {
2069 st->cur_skb = st->cur_skb->next;
2072 } else if (st->root_skb == st->cur_skb &&
2073 skb_shinfo(st->root_skb)->frag_list) {
2074 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2082 * skb_abort_seq_read - Abort a sequential read of skb data
2083 * @st: state variable
2085 * Must be called if skb_seq_read() was not called until it
2088 void skb_abort_seq_read(struct skb_seq_state *st)
2091 kunmap_skb_frag(st->frag_data);
2094 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2096 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2097 struct ts_config *conf,
2098 struct ts_state *state)
2100 return skb_seq_read(offset, text, TS_SKB_CB(state));
2103 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2105 skb_abort_seq_read(TS_SKB_CB(state));
2109 * skb_find_text - Find a text pattern in skb data
2110 * @skb: the buffer to look in
2111 * @from: search offset
2113 * @config: textsearch configuration
2114 * @state: uninitialized textsearch state variable
2116 * Finds a pattern in the skb data according to the specified
2117 * textsearch configuration. Use textsearch_next() to retrieve
2118 * subsequent occurrences of the pattern. Returns the offset
2119 * to the first occurrence or UINT_MAX if no match was found.
2121 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2122 unsigned int to, struct ts_config *config,
2123 struct ts_state *state)
2127 config->get_next_block = skb_ts_get_next_block;
2128 config->finish = skb_ts_finish;
2130 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2132 ret = textsearch_find(config, state);
2133 return (ret <= to - from ? ret : UINT_MAX);
2137 * skb_append_datato_frags: - append the user data to a skb
2138 * @sk: sock structure
2139 * @skb: skb structure to be appened with user data.
2140 * @getfrag: call back function to be used for getting the user data
2141 * @from: pointer to user message iov
2142 * @length: length of the iov message
2144 * Description: This procedure append the user data in the fragment part
2145 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2147 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2148 int (*getfrag)(void *from, char *to, int offset,
2149 int len, int odd, struct sk_buff *skb),
2150 void *from, int length)
2153 skb_frag_t *frag = NULL;
2154 struct page *page = NULL;
2160 /* Return error if we don't have space for new frag */
2161 frg_cnt = skb_shinfo(skb)->nr_frags;
2162 if (frg_cnt >= MAX_SKB_FRAGS)
2165 /* allocate a new page for next frag */
2166 page = alloc_pages(sk->sk_allocation, 0);
2168 /* If alloc_page fails just return failure and caller will
2169 * free previous allocated pages by doing kfree_skb()
2174 /* initialize the next frag */
2175 sk->sk_sndmsg_page = page;
2176 sk->sk_sndmsg_off = 0;
2177 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2178 skb->truesize += PAGE_SIZE;
2179 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2181 /* get the new initialized frag */
2182 frg_cnt = skb_shinfo(skb)->nr_frags;
2183 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2185 /* copy the user data to page */
2186 left = PAGE_SIZE - frag->page_offset;
2187 copy = (length > left)? left : length;
2189 ret = getfrag(from, (page_address(frag->page) +
2190 frag->page_offset + frag->size),
2191 offset, copy, 0, skb);
2195 /* copy was successful so update the size parameters */
2196 sk->sk_sndmsg_off += copy;
2199 skb->data_len += copy;
2203 } while (length > 0);
2209 * skb_pull_rcsum - pull skb and update receive checksum
2210 * @skb: buffer to update
2211 * @len: length of data pulled
2213 * This function performs an skb_pull on the packet and updates
2214 * the CHECKSUM_COMPLETE checksum. It should be used on
2215 * receive path processing instead of skb_pull unless you know
2216 * that the checksum difference is zero (e.g., a valid IP header)
2217 * or you are setting ip_summed to CHECKSUM_NONE.
2219 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2221 BUG_ON(len > skb->len);
2223 BUG_ON(skb->len < skb->data_len);
2224 skb_postpull_rcsum(skb, skb->data, len);
2225 return skb->data += len;
2228 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2231 * skb_segment - Perform protocol segmentation on skb.
2232 * @skb: buffer to segment
2233 * @features: features for the output path (see dev->features)
2235 * This function performs segmentation on the given skb. It returns
2236 * a pointer to the first in a list of new skbs for the segments.
2237 * In case of error it returns ERR_PTR(err).
2239 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2241 struct sk_buff *segs = NULL;
2242 struct sk_buff *tail = NULL;
2243 unsigned int mss = skb_shinfo(skb)->gso_size;
2244 unsigned int doffset = skb->data - skb_mac_header(skb);
2245 unsigned int offset = doffset;
2246 unsigned int headroom;
2248 int sg = features & NETIF_F_SG;
2249 int nfrags = skb_shinfo(skb)->nr_frags;
2254 __skb_push(skb, doffset);
2255 headroom = skb_headroom(skb);
2256 pos = skb_headlen(skb);
2259 struct sk_buff *nskb;
2265 len = skb->len - offset;
2269 hsize = skb_headlen(skb) - offset;
2272 if (hsize > len || !sg)
2275 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
2276 if (unlikely(!nskb))
2285 nskb->dev = skb->dev;
2286 skb_copy_queue_mapping(nskb, skb);
2287 nskb->priority = skb->priority;
2288 nskb->protocol = skb->protocol;
2289 nskb->dst = dst_clone(skb->dst);
2290 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
2291 nskb->pkt_type = skb->pkt_type;
2292 nskb->mac_len = skb->mac_len;
2294 skb_reserve(nskb, headroom);
2295 skb_reset_mac_header(nskb);
2296 skb_set_network_header(nskb, skb->mac_len);
2297 nskb->transport_header = (nskb->network_header +
2298 skb_network_header_len(skb));
2299 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2302 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2308 frag = skb_shinfo(nskb)->frags;
2311 nskb->ip_summed = CHECKSUM_PARTIAL;
2312 nskb->csum = skb->csum;
2313 skb_copy_from_linear_data_offset(skb, offset,
2314 skb_put(nskb, hsize), hsize);
2316 while (pos < offset + len) {
2317 BUG_ON(i >= nfrags);
2319 *frag = skb_shinfo(skb)->frags[i];
2320 get_page(frag->page);
2324 frag->page_offset += offset - pos;
2325 frag->size -= offset - pos;
2330 if (pos + size <= offset + len) {
2334 frag->size -= pos + size - (offset + len);
2341 skb_shinfo(nskb)->nr_frags = k;
2342 nskb->data_len = len - hsize;
2343 nskb->len += nskb->data_len;
2344 nskb->truesize += nskb->data_len;
2345 } while ((offset += len) < skb->len);
2350 while ((skb = segs)) {
2354 return ERR_PTR(err);
2357 EXPORT_SYMBOL_GPL(skb_segment);
2359 void __init skb_init(void)
2361 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2362 sizeof(struct sk_buff),
2364 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2366 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2367 (2*sizeof(struct sk_buff)) +
2370 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2375 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2376 * @skb: Socket buffer containing the buffers to be mapped
2377 * @sg: The scatter-gather list to map into
2378 * @offset: The offset into the buffer's contents to start mapping
2379 * @len: Length of buffer space to be mapped
2381 * Fill the specified scatter-gather list with mappings/pointers into a
2382 * region of the buffer space attached to a socket buffer.
2385 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2387 int start = skb_headlen(skb);
2388 int i, copy = start - offset;
2394 sg_set_buf(sg, skb->data + offset, copy);
2396 if ((len -= copy) == 0)
2401 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2404 BUG_TRAP(start <= offset + len);
2406 end = start + skb_shinfo(skb)->frags[i].size;
2407 if ((copy = end - offset) > 0) {
2408 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2412 sg_set_page(&sg[elt], frag->page, copy,
2413 frag->page_offset+offset-start);
2422 if (skb_shinfo(skb)->frag_list) {
2423 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2425 for (; list; list = list->next) {
2428 BUG_TRAP(start <= offset + len);
2430 end = start + list->len;
2431 if ((copy = end - offset) > 0) {
2434 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2436 if ((len -= copy) == 0)
2447 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2449 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2451 sg_mark_end(&sg[nsg - 1]);
2457 * skb_cow_data - Check that a socket buffer's data buffers are writable
2458 * @skb: The socket buffer to check.
2459 * @tailbits: Amount of trailing space to be added
2460 * @trailer: Returned pointer to the skb where the @tailbits space begins
2462 * Make sure that the data buffers attached to a socket buffer are
2463 * writable. If they are not, private copies are made of the data buffers
2464 * and the socket buffer is set to use these instead.
2466 * If @tailbits is given, make sure that there is space to write @tailbits
2467 * bytes of data beyond current end of socket buffer. @trailer will be
2468 * set to point to the skb in which this space begins.
2470 * The number of scatterlist elements required to completely map the
2471 * COW'd and extended socket buffer will be returned.
2473 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2477 struct sk_buff *skb1, **skb_p;
2479 /* If skb is cloned or its head is paged, reallocate
2480 * head pulling out all the pages (pages are considered not writable
2481 * at the moment even if they are anonymous).
2483 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2484 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2487 /* Easy case. Most of packets will go this way. */
2488 if (!skb_shinfo(skb)->frag_list) {
2489 /* A little of trouble, not enough of space for trailer.
2490 * This should not happen, when stack is tuned to generate
2491 * good frames. OK, on miss we reallocate and reserve even more
2492 * space, 128 bytes is fair. */
2494 if (skb_tailroom(skb) < tailbits &&
2495 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2503 /* Misery. We are in troubles, going to mincer fragments... */
2506 skb_p = &skb_shinfo(skb)->frag_list;
2509 while ((skb1 = *skb_p) != NULL) {
2512 /* The fragment is partially pulled by someone,
2513 * this can happen on input. Copy it and everything
2516 if (skb_shared(skb1))
2519 /* If the skb is the last, worry about trailer. */
2521 if (skb1->next == NULL && tailbits) {
2522 if (skb_shinfo(skb1)->nr_frags ||
2523 skb_shinfo(skb1)->frag_list ||
2524 skb_tailroom(skb1) < tailbits)
2525 ntail = tailbits + 128;
2531 skb_shinfo(skb1)->nr_frags ||
2532 skb_shinfo(skb1)->frag_list) {
2533 struct sk_buff *skb2;
2535 /* Fuck, we are miserable poor guys... */
2537 skb2 = skb_copy(skb1, GFP_ATOMIC);
2539 skb2 = skb_copy_expand(skb1,
2543 if (unlikely(skb2 == NULL))
2547 skb_set_owner_w(skb2, skb1->sk);
2549 /* Looking around. Are we still alive?
2550 * OK, link new skb, drop old one */
2552 skb2->next = skb1->next;
2559 skb_p = &skb1->next;
2566 * skb_partial_csum_set - set up and verify partial csum values for packet
2567 * @skb: the skb to set
2568 * @start: the number of bytes after skb->data to start checksumming.
2569 * @off: the offset from start to place the checksum.
2571 * For untrusted partially-checksummed packets, we need to make sure the values
2572 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2574 * This function checks and sets those values and skb->ip_summed: if this
2575 * returns false you should drop the packet.
2577 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2579 if (unlikely(start > skb->len - 2) ||
2580 unlikely((int)start + off > skb->len - 2)) {
2581 if (net_ratelimit())
2583 "bad partial csum: csum=%u/%u len=%u\n",
2584 start, off, skb->len);
2587 skb->ip_summed = CHECKSUM_PARTIAL;
2588 skb->csum_start = skb_headroom(skb) + start;
2589 skb->csum_offset = off;
2593 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2595 if (net_ratelimit())
2596 pr_warning("%s: received packets cannot be forwarded"
2597 " while LRO is enabled\n", skb->dev->name);
2600 EXPORT_SYMBOL(___pskb_trim);
2601 EXPORT_SYMBOL(__kfree_skb);
2602 EXPORT_SYMBOL(kfree_skb);
2603 EXPORT_SYMBOL(__pskb_pull_tail);
2604 EXPORT_SYMBOL(__alloc_skb);
2605 EXPORT_SYMBOL(__netdev_alloc_skb);
2606 EXPORT_SYMBOL(pskb_copy);
2607 EXPORT_SYMBOL(pskb_expand_head);
2608 EXPORT_SYMBOL(skb_checksum);
2609 EXPORT_SYMBOL(skb_clone);
2610 EXPORT_SYMBOL(skb_copy);
2611 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2612 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2613 EXPORT_SYMBOL(skb_copy_bits);
2614 EXPORT_SYMBOL(skb_copy_expand);
2615 EXPORT_SYMBOL(skb_over_panic);
2616 EXPORT_SYMBOL(skb_pad);
2617 EXPORT_SYMBOL(skb_realloc_headroom);
2618 EXPORT_SYMBOL(skb_under_panic);
2619 EXPORT_SYMBOL(skb_dequeue);
2620 EXPORT_SYMBOL(skb_dequeue_tail);
2621 EXPORT_SYMBOL(skb_insert);
2622 EXPORT_SYMBOL(skb_queue_purge);
2623 EXPORT_SYMBOL(skb_queue_head);
2624 EXPORT_SYMBOL(skb_queue_tail);
2625 EXPORT_SYMBOL(skb_unlink);
2626 EXPORT_SYMBOL(skb_append);
2627 EXPORT_SYMBOL(skb_split);
2628 EXPORT_SYMBOL(skb_prepare_seq_read);
2629 EXPORT_SYMBOL(skb_seq_read);
2630 EXPORT_SYMBOL(skb_abort_seq_read);
2631 EXPORT_SYMBOL(skb_find_text);
2632 EXPORT_SYMBOL(skb_append_datato_frags);
2633 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
2635 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2636 EXPORT_SYMBOL_GPL(skb_cow_data);
2637 EXPORT_SYMBOL_GPL(skb_partial_csum_set);