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 new->vlan_tci = old->vlan_tci;
464 skb_copy_secmark(new, old);
467 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
469 #define C(x) n->x = skb->x
471 n->next = n->prev = NULL;
473 __copy_skb_header(n, skb);
478 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
481 n->destructor = NULL;
488 atomic_set(&n->users, 1);
490 atomic_inc(&(skb_shinfo(skb)->dataref));
498 * skb_morph - morph one skb into another
499 * @dst: the skb to receive the contents
500 * @src: the skb to supply the contents
502 * This is identical to skb_clone except that the target skb is
503 * supplied by the user.
505 * The target skb is returned upon exit.
507 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
509 skb_release_all(dst);
510 return __skb_clone(dst, src);
512 EXPORT_SYMBOL_GPL(skb_morph);
515 * skb_clone - duplicate an sk_buff
516 * @skb: buffer to clone
517 * @gfp_mask: allocation priority
519 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
520 * copies share the same packet data but not structure. The new
521 * buffer has a reference count of 1. If the allocation fails the
522 * function returns %NULL otherwise the new buffer is returned.
524 * If this function is called from an interrupt gfp_mask() must be
528 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
533 if (skb->fclone == SKB_FCLONE_ORIG &&
534 n->fclone == SKB_FCLONE_UNAVAILABLE) {
535 atomic_t *fclone_ref = (atomic_t *) (n + 1);
536 n->fclone = SKB_FCLONE_CLONE;
537 atomic_inc(fclone_ref);
539 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
542 n->fclone = SKB_FCLONE_UNAVAILABLE;
545 return __skb_clone(n, skb);
548 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
550 #ifndef NET_SKBUFF_DATA_USES_OFFSET
552 * Shift between the two data areas in bytes
554 unsigned long offset = new->data - old->data;
557 __copy_skb_header(new, old);
559 #ifndef NET_SKBUFF_DATA_USES_OFFSET
560 /* {transport,network,mac}_header are relative to skb->head */
561 new->transport_header += offset;
562 new->network_header += offset;
563 new->mac_header += offset;
565 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
566 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
567 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
571 * skb_copy - create private copy of an sk_buff
572 * @skb: buffer to copy
573 * @gfp_mask: allocation priority
575 * Make a copy of both an &sk_buff and its data. This is used when the
576 * caller wishes to modify the data and needs a private copy of the
577 * data to alter. Returns %NULL on failure or the pointer to the buffer
578 * on success. The returned buffer has a reference count of 1.
580 * As by-product this function converts non-linear &sk_buff to linear
581 * one, so that &sk_buff becomes completely private and caller is allowed
582 * to modify all the data of returned buffer. This means that this
583 * function is not recommended for use in circumstances when only
584 * header is going to be modified. Use pskb_copy() instead.
587 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
589 int headerlen = skb->data - skb->head;
591 * Allocate the copy buffer
594 #ifdef NET_SKBUFF_DATA_USES_OFFSET
595 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
597 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
602 /* Set the data pointer */
603 skb_reserve(n, headerlen);
604 /* Set the tail pointer and length */
605 skb_put(n, skb->len);
607 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
610 copy_skb_header(n, skb);
616 * pskb_copy - create copy of an sk_buff with private head.
617 * @skb: buffer to copy
618 * @gfp_mask: allocation priority
620 * Make a copy of both an &sk_buff and part of its data, located
621 * in header. Fragmented data remain shared. This is used when
622 * the caller wishes to modify only header of &sk_buff and needs
623 * private copy of the header to alter. Returns %NULL on failure
624 * or the pointer to the buffer on success.
625 * The returned buffer has a reference count of 1.
628 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
631 * Allocate the copy buffer
634 #ifdef NET_SKBUFF_DATA_USES_OFFSET
635 n = alloc_skb(skb->end, gfp_mask);
637 n = alloc_skb(skb->end - skb->head, gfp_mask);
642 /* Set the data pointer */
643 skb_reserve(n, skb->data - skb->head);
644 /* Set the tail pointer and length */
645 skb_put(n, skb_headlen(skb));
647 skb_copy_from_linear_data(skb, n->data, n->len);
649 n->truesize += skb->data_len;
650 n->data_len = skb->data_len;
653 if (skb_shinfo(skb)->nr_frags) {
656 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
657 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
658 get_page(skb_shinfo(n)->frags[i].page);
660 skb_shinfo(n)->nr_frags = i;
663 if (skb_shinfo(skb)->frag_list) {
664 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
665 skb_clone_fraglist(n);
668 copy_skb_header(n, skb);
674 * pskb_expand_head - reallocate header of &sk_buff
675 * @skb: buffer to reallocate
676 * @nhead: room to add at head
677 * @ntail: room to add at tail
678 * @gfp_mask: allocation priority
680 * Expands (or creates identical copy, if &nhead and &ntail are zero)
681 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
682 * reference count of 1. Returns zero in the case of success or error,
683 * if expansion failed. In the last case, &sk_buff is not changed.
685 * All the pointers pointing into skb header may change and must be
686 * reloaded after call to this function.
689 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
694 #ifdef NET_SKBUFF_DATA_USES_OFFSET
695 int size = nhead + skb->end + ntail;
697 int size = nhead + (skb->end - skb->head) + ntail;
704 size = SKB_DATA_ALIGN(size);
706 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
710 /* Copy only real data... and, alas, header. This should be
711 * optimized for the cases when header is void. */
712 #ifdef NET_SKBUFF_DATA_USES_OFFSET
713 memcpy(data + nhead, skb->head, skb->tail);
715 memcpy(data + nhead, skb->head, skb->tail - skb->head);
717 memcpy(data + size, skb_end_pointer(skb),
718 sizeof(struct skb_shared_info));
720 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
721 get_page(skb_shinfo(skb)->frags[i].page);
723 if (skb_shinfo(skb)->frag_list)
724 skb_clone_fraglist(skb);
726 skb_release_data(skb);
728 off = (data + nhead) - skb->head;
732 #ifdef NET_SKBUFF_DATA_USES_OFFSET
736 skb->end = skb->head + size;
738 /* {transport,network,mac}_header and tail are relative to skb->head */
740 skb->transport_header += off;
741 skb->network_header += off;
742 skb->mac_header += off;
743 skb->csum_start += nhead;
747 atomic_set(&skb_shinfo(skb)->dataref, 1);
754 /* Make private copy of skb with writable head and some headroom */
756 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
758 struct sk_buff *skb2;
759 int delta = headroom - skb_headroom(skb);
762 skb2 = pskb_copy(skb, GFP_ATOMIC);
764 skb2 = skb_clone(skb, GFP_ATOMIC);
765 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
776 * skb_copy_expand - copy and expand sk_buff
777 * @skb: buffer to copy
778 * @newheadroom: new free bytes at head
779 * @newtailroom: new free bytes at tail
780 * @gfp_mask: allocation priority
782 * Make a copy of both an &sk_buff and its data and while doing so
783 * allocate additional space.
785 * This is used when the caller wishes to modify the data and needs a
786 * private copy of the data to alter as well as more space for new fields.
787 * Returns %NULL on failure or the pointer to the buffer
788 * on success. The returned buffer has a reference count of 1.
790 * You must pass %GFP_ATOMIC as the allocation priority if this function
791 * is called from an interrupt.
793 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
794 int newheadroom, int newtailroom,
798 * Allocate the copy buffer
800 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
802 int oldheadroom = skb_headroom(skb);
803 int head_copy_len, head_copy_off;
809 skb_reserve(n, newheadroom);
811 /* Set the tail pointer and length */
812 skb_put(n, skb->len);
814 head_copy_len = oldheadroom;
816 if (newheadroom <= head_copy_len)
817 head_copy_len = newheadroom;
819 head_copy_off = newheadroom - head_copy_len;
821 /* Copy the linear header and data. */
822 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
823 skb->len + head_copy_len))
826 copy_skb_header(n, skb);
828 off = newheadroom - oldheadroom;
829 n->csum_start += off;
830 #ifdef NET_SKBUFF_DATA_USES_OFFSET
831 n->transport_header += off;
832 n->network_header += off;
833 n->mac_header += off;
840 * skb_pad - zero pad the tail of an skb
841 * @skb: buffer to pad
844 * Ensure that a buffer is followed by a padding area that is zero
845 * filled. Used by network drivers which may DMA or transfer data
846 * beyond the buffer end onto the wire.
848 * May return error in out of memory cases. The skb is freed on error.
851 int skb_pad(struct sk_buff *skb, int pad)
856 /* If the skbuff is non linear tailroom is always zero.. */
857 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
858 memset(skb->data+skb->len, 0, pad);
862 ntail = skb->data_len + pad - (skb->end - skb->tail);
863 if (likely(skb_cloned(skb) || ntail > 0)) {
864 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
869 /* FIXME: The use of this function with non-linear skb's really needs
872 err = skb_linearize(skb);
876 memset(skb->data + skb->len, 0, pad);
885 * skb_put - add data to a buffer
886 * @skb: buffer to use
887 * @len: amount of data to add
889 * This function extends the used data area of the buffer. If this would
890 * exceed the total buffer size the kernel will panic. A pointer to the
891 * first byte of the extra data is returned.
893 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
895 unsigned char *tmp = skb_tail_pointer(skb);
896 SKB_LINEAR_ASSERT(skb);
899 if (unlikely(skb->tail > skb->end))
900 skb_over_panic(skb, len, __builtin_return_address(0));
903 EXPORT_SYMBOL(skb_put);
906 * skb_push - add data to the start of a buffer
907 * @skb: buffer to use
908 * @len: amount of data to add
910 * This function extends the used data area of the buffer at the buffer
911 * start. If this would exceed the total buffer headroom the kernel will
912 * panic. A pointer to the first byte of the extra data is returned.
914 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
918 if (unlikely(skb->data<skb->head))
919 skb_under_panic(skb, len, __builtin_return_address(0));
922 EXPORT_SYMBOL(skb_push);
925 * skb_pull - remove data from the start of a buffer
926 * @skb: buffer to use
927 * @len: amount of data to remove
929 * This function removes data from the start of a buffer, returning
930 * the memory to the headroom. A pointer to the next data in the buffer
931 * is returned. Once the data has been pulled future pushes will overwrite
934 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
936 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
938 EXPORT_SYMBOL(skb_pull);
941 * skb_trim - remove end from a buffer
942 * @skb: buffer to alter
945 * Cut the length of a buffer down by removing data from the tail. If
946 * the buffer is already under the length specified it is not modified.
947 * The skb must be linear.
949 void skb_trim(struct sk_buff *skb, unsigned int len)
952 __skb_trim(skb, len);
954 EXPORT_SYMBOL(skb_trim);
956 /* Trims skb to length len. It can change skb pointers.
959 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
961 struct sk_buff **fragp;
962 struct sk_buff *frag;
963 int offset = skb_headlen(skb);
964 int nfrags = skb_shinfo(skb)->nr_frags;
968 if (skb_cloned(skb) &&
969 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
976 for (; i < nfrags; i++) {
977 int end = offset + skb_shinfo(skb)->frags[i].size;
984 skb_shinfo(skb)->frags[i++].size = len - offset;
987 skb_shinfo(skb)->nr_frags = i;
989 for (; i < nfrags; i++)
990 put_page(skb_shinfo(skb)->frags[i].page);
992 if (skb_shinfo(skb)->frag_list)
993 skb_drop_fraglist(skb);
997 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
998 fragp = &frag->next) {
999 int end = offset + frag->len;
1001 if (skb_shared(frag)) {
1002 struct sk_buff *nfrag;
1004 nfrag = skb_clone(frag, GFP_ATOMIC);
1005 if (unlikely(!nfrag))
1008 nfrag->next = frag->next;
1020 unlikely((err = pskb_trim(frag, len - offset))))
1024 skb_drop_list(&frag->next);
1029 if (len > skb_headlen(skb)) {
1030 skb->data_len -= skb->len - len;
1035 skb_set_tail_pointer(skb, len);
1042 * __pskb_pull_tail - advance tail of skb header
1043 * @skb: buffer to reallocate
1044 * @delta: number of bytes to advance tail
1046 * The function makes a sense only on a fragmented &sk_buff,
1047 * it expands header moving its tail forward and copying necessary
1048 * data from fragmented part.
1050 * &sk_buff MUST have reference count of 1.
1052 * Returns %NULL (and &sk_buff does not change) if pull failed
1053 * or value of new tail of skb in the case of success.
1055 * All the pointers pointing into skb header may change and must be
1056 * reloaded after call to this function.
1059 /* Moves tail of skb head forward, copying data from fragmented part,
1060 * when it is necessary.
1061 * 1. It may fail due to malloc failure.
1062 * 2. It may change skb pointers.
1064 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1066 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1068 /* If skb has not enough free space at tail, get new one
1069 * plus 128 bytes for future expansions. If we have enough
1070 * room at tail, reallocate without expansion only if skb is cloned.
1072 int i, k, eat = (skb->tail + delta) - skb->end;
1074 if (eat > 0 || skb_cloned(skb)) {
1075 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1080 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1083 /* Optimization: no fragments, no reasons to preestimate
1084 * size of pulled pages. Superb.
1086 if (!skb_shinfo(skb)->frag_list)
1089 /* Estimate size of pulled pages. */
1091 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1092 if (skb_shinfo(skb)->frags[i].size >= eat)
1094 eat -= skb_shinfo(skb)->frags[i].size;
1097 /* If we need update frag list, we are in troubles.
1098 * Certainly, it possible to add an offset to skb data,
1099 * but taking into account that pulling is expected to
1100 * be very rare operation, it is worth to fight against
1101 * further bloating skb head and crucify ourselves here instead.
1102 * Pure masohism, indeed. 8)8)
1105 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1106 struct sk_buff *clone = NULL;
1107 struct sk_buff *insp = NULL;
1112 if (list->len <= eat) {
1113 /* Eaten as whole. */
1118 /* Eaten partially. */
1120 if (skb_shared(list)) {
1121 /* Sucks! We need to fork list. :-( */
1122 clone = skb_clone(list, GFP_ATOMIC);
1128 /* This may be pulled without
1132 if (!pskb_pull(list, eat)) {
1141 /* Free pulled out fragments. */
1142 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1143 skb_shinfo(skb)->frag_list = list->next;
1146 /* And insert new clone at head. */
1149 skb_shinfo(skb)->frag_list = clone;
1152 /* Success! Now we may commit changes to skb data. */
1157 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1158 if (skb_shinfo(skb)->frags[i].size <= eat) {
1159 put_page(skb_shinfo(skb)->frags[i].page);
1160 eat -= skb_shinfo(skb)->frags[i].size;
1162 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1164 skb_shinfo(skb)->frags[k].page_offset += eat;
1165 skb_shinfo(skb)->frags[k].size -= eat;
1171 skb_shinfo(skb)->nr_frags = k;
1174 skb->data_len -= delta;
1176 return skb_tail_pointer(skb);
1179 /* Copy some data bits from skb to kernel buffer. */
1181 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1184 int start = skb_headlen(skb);
1186 if (offset > (int)skb->len - len)
1190 if ((copy = start - offset) > 0) {
1193 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1194 if ((len -= copy) == 0)
1200 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1203 BUG_TRAP(start <= offset + len);
1205 end = start + skb_shinfo(skb)->frags[i].size;
1206 if ((copy = end - offset) > 0) {
1212 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1214 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1215 offset - start, copy);
1216 kunmap_skb_frag(vaddr);
1218 if ((len -= copy) == 0)
1226 if (skb_shinfo(skb)->frag_list) {
1227 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1229 for (; list; list = list->next) {
1232 BUG_TRAP(start <= offset + len);
1234 end = start + list->len;
1235 if ((copy = end - offset) > 0) {
1238 if (skb_copy_bits(list, offset - start,
1241 if ((len -= copy) == 0)
1257 * Callback from splice_to_pipe(), if we need to release some pages
1258 * at the end of the spd in case we error'ed out in filling the pipe.
1260 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1262 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1268 * Fill page/offset/length into spd, if it can hold more pages.
1270 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1271 unsigned int len, unsigned int offset,
1272 struct sk_buff *skb)
1274 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1277 spd->pages[spd->nr_pages] = page;
1278 spd->partial[spd->nr_pages].len = len;
1279 spd->partial[spd->nr_pages].offset = offset;
1280 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1285 static inline void __segment_seek(struct page **page, unsigned int *poff,
1286 unsigned int *plen, unsigned int off)
1289 *page += *poff / PAGE_SIZE;
1290 *poff = *poff % PAGE_SIZE;
1294 static inline int __splice_segment(struct page *page, unsigned int poff,
1295 unsigned int plen, unsigned int *off,
1296 unsigned int *len, struct sk_buff *skb,
1297 struct splice_pipe_desc *spd)
1302 /* skip this segment if already processed */
1308 /* ignore any bits we already processed */
1310 __segment_seek(&page, &poff, &plen, *off);
1315 unsigned int flen = min(*len, plen);
1317 /* the linear region may spread across several pages */
1318 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1320 if (spd_fill_page(spd, page, flen, poff, skb))
1323 __segment_seek(&page, &poff, &plen, flen);
1326 } while (*len && plen);
1332 * Map linear and fragment data from the skb to spd. It reports failure if the
1333 * pipe is full or if we already spliced the requested length.
1335 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1337 struct splice_pipe_desc *spd)
1342 * map the linear part
1344 if (__splice_segment(virt_to_page(skb->data),
1345 (unsigned long) skb->data & (PAGE_SIZE - 1),
1347 offset, len, skb, spd))
1351 * 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];
1356 if (__splice_segment(f->page, f->page_offset, f->size,
1357 offset, len, skb, spd))
1365 * Map data from the skb to a pipe. Should handle both the linear part,
1366 * the fragments, and the frag list. It does NOT handle frag lists within
1367 * the frag list, if such a thing exists. We'd probably need to recurse to
1368 * handle that cleanly.
1370 int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1371 struct pipe_inode_info *pipe, unsigned int tlen,
1374 struct partial_page partial[PIPE_BUFFERS];
1375 struct page *pages[PIPE_BUFFERS];
1376 struct splice_pipe_desc spd = {
1380 .ops = &sock_pipe_buf_ops,
1381 .spd_release = sock_spd_release,
1383 struct sk_buff *skb;
1386 * I'd love to avoid the clone here, but tcp_read_sock()
1387 * ignores reference counts and unconditonally kills the sk_buff
1388 * on return from the actor.
1390 skb = skb_clone(__skb, GFP_KERNEL);
1395 * __skb_splice_bits() only fails if the output has no room left,
1396 * so no point in going over the frag_list for the error case.
1398 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1404 * now see if we have a frag_list to map
1406 if (skb_shinfo(skb)->frag_list) {
1407 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1409 for (; list && tlen; list = list->next) {
1410 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1417 * drop our reference to the clone, the pipe consumption will
1424 struct sock *sk = __skb->sk;
1427 * Drop the socket lock, otherwise we have reverse
1428 * locking dependencies between sk_lock and i_mutex
1429 * here as compared to sendfile(). We enter here
1430 * with the socket lock held, and splice_to_pipe() will
1431 * grab the pipe inode lock. For sendfile() emulation,
1432 * we call into ->sendpage() with the i_mutex lock held
1433 * and networking will grab the socket lock.
1436 ret = splice_to_pipe(pipe, &spd);
1445 * skb_store_bits - store bits from kernel buffer to skb
1446 * @skb: destination buffer
1447 * @offset: offset in destination
1448 * @from: source buffer
1449 * @len: number of bytes to copy
1451 * Copy the specified number of bytes from the source buffer to the
1452 * destination skb. This function handles all the messy bits of
1453 * traversing fragment lists and such.
1456 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1459 int start = skb_headlen(skb);
1461 if (offset > (int)skb->len - len)
1464 if ((copy = start - offset) > 0) {
1467 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1468 if ((len -= copy) == 0)
1474 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1475 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1478 BUG_TRAP(start <= offset + len);
1480 end = start + frag->size;
1481 if ((copy = end - offset) > 0) {
1487 vaddr = kmap_skb_frag(frag);
1488 memcpy(vaddr + frag->page_offset + offset - start,
1490 kunmap_skb_frag(vaddr);
1492 if ((len -= copy) == 0)
1500 if (skb_shinfo(skb)->frag_list) {
1501 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1503 for (; list; list = list->next) {
1506 BUG_TRAP(start <= offset + len);
1508 end = start + list->len;
1509 if ((copy = end - offset) > 0) {
1512 if (skb_store_bits(list, offset - start,
1515 if ((len -= copy) == 0)
1530 EXPORT_SYMBOL(skb_store_bits);
1532 /* Checksum skb data. */
1534 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1535 int len, __wsum csum)
1537 int start = skb_headlen(skb);
1538 int i, copy = start - offset;
1541 /* Checksum header. */
1545 csum = csum_partial(skb->data + offset, copy, csum);
1546 if ((len -= copy) == 0)
1552 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1555 BUG_TRAP(start <= offset + len);
1557 end = start + skb_shinfo(skb)->frags[i].size;
1558 if ((copy = end - offset) > 0) {
1561 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1565 vaddr = kmap_skb_frag(frag);
1566 csum2 = csum_partial(vaddr + frag->page_offset +
1567 offset - start, copy, 0);
1568 kunmap_skb_frag(vaddr);
1569 csum = csum_block_add(csum, csum2, pos);
1578 if (skb_shinfo(skb)->frag_list) {
1579 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1581 for (; list; list = list->next) {
1584 BUG_TRAP(start <= offset + len);
1586 end = start + list->len;
1587 if ((copy = end - offset) > 0) {
1591 csum2 = skb_checksum(list, offset - start,
1593 csum = csum_block_add(csum, csum2, pos);
1594 if ((len -= copy) == 0)
1607 /* Both of above in one bottle. */
1609 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1610 u8 *to, int len, __wsum csum)
1612 int start = skb_headlen(skb);
1613 int i, copy = start - offset;
1620 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1622 if ((len -= copy) == 0)
1629 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1632 BUG_TRAP(start <= offset + len);
1634 end = start + skb_shinfo(skb)->frags[i].size;
1635 if ((copy = end - offset) > 0) {
1638 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1642 vaddr = kmap_skb_frag(frag);
1643 csum2 = csum_partial_copy_nocheck(vaddr +
1647 kunmap_skb_frag(vaddr);
1648 csum = csum_block_add(csum, csum2, pos);
1658 if (skb_shinfo(skb)->frag_list) {
1659 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1661 for (; list; list = list->next) {
1665 BUG_TRAP(start <= offset + len);
1667 end = start + list->len;
1668 if ((copy = end - offset) > 0) {
1671 csum2 = skb_copy_and_csum_bits(list,
1674 csum = csum_block_add(csum, csum2, pos);
1675 if ((len -= copy) == 0)
1688 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1693 if (skb->ip_summed == CHECKSUM_PARTIAL)
1694 csstart = skb->csum_start - skb_headroom(skb);
1696 csstart = skb_headlen(skb);
1698 BUG_ON(csstart > skb_headlen(skb));
1700 skb_copy_from_linear_data(skb, to, csstart);
1703 if (csstart != skb->len)
1704 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1705 skb->len - csstart, 0);
1707 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1708 long csstuff = csstart + skb->csum_offset;
1710 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1715 * skb_dequeue - remove from the head of the queue
1716 * @list: list to dequeue from
1718 * Remove the head of the list. The list lock is taken so the function
1719 * may be used safely with other locking list functions. The head item is
1720 * returned or %NULL if the list is empty.
1723 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1725 unsigned long flags;
1726 struct sk_buff *result;
1728 spin_lock_irqsave(&list->lock, flags);
1729 result = __skb_dequeue(list);
1730 spin_unlock_irqrestore(&list->lock, flags);
1735 * skb_dequeue_tail - remove from the tail of the queue
1736 * @list: list to dequeue from
1738 * Remove the tail of the list. The list lock is taken so the function
1739 * may be used safely with other locking list functions. The tail item is
1740 * returned or %NULL if the list is empty.
1742 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1744 unsigned long flags;
1745 struct sk_buff *result;
1747 spin_lock_irqsave(&list->lock, flags);
1748 result = __skb_dequeue_tail(list);
1749 spin_unlock_irqrestore(&list->lock, flags);
1754 * skb_queue_purge - empty a list
1755 * @list: list to empty
1757 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1758 * the list and one reference dropped. This function takes the list
1759 * lock and is atomic with respect to other list locking functions.
1761 void skb_queue_purge(struct sk_buff_head *list)
1763 struct sk_buff *skb;
1764 while ((skb = skb_dequeue(list)) != NULL)
1769 * skb_queue_head - queue a buffer at the list head
1770 * @list: list to use
1771 * @newsk: buffer to queue
1773 * Queue a buffer at the start of the list. This function takes the
1774 * list lock and can be used safely with other locking &sk_buff functions
1777 * A buffer cannot be placed on two lists at the same time.
1779 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1781 unsigned long flags;
1783 spin_lock_irqsave(&list->lock, flags);
1784 __skb_queue_head(list, newsk);
1785 spin_unlock_irqrestore(&list->lock, flags);
1789 * skb_queue_tail - queue a buffer at the list tail
1790 * @list: list to use
1791 * @newsk: buffer to queue
1793 * Queue a buffer at the tail of the list. This function takes the
1794 * list lock and can be used safely with other locking &sk_buff functions
1797 * A buffer cannot be placed on two lists at the same time.
1799 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1801 unsigned long flags;
1803 spin_lock_irqsave(&list->lock, flags);
1804 __skb_queue_tail(list, newsk);
1805 spin_unlock_irqrestore(&list->lock, flags);
1809 * skb_unlink - remove a buffer from a list
1810 * @skb: buffer to remove
1811 * @list: list to use
1813 * Remove a packet from a list. The list locks are taken and this
1814 * function is atomic with respect to other list locked calls
1816 * You must know what list the SKB is on.
1818 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1820 unsigned long flags;
1822 spin_lock_irqsave(&list->lock, flags);
1823 __skb_unlink(skb, list);
1824 spin_unlock_irqrestore(&list->lock, flags);
1828 * skb_append - append a buffer
1829 * @old: buffer to insert after
1830 * @newsk: buffer to insert
1831 * @list: list to use
1833 * Place a packet after a given packet in a list. The list locks are taken
1834 * and this function is atomic with respect to other list locked calls.
1835 * A buffer cannot be placed on two lists at the same time.
1837 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1839 unsigned long flags;
1841 spin_lock_irqsave(&list->lock, flags);
1842 __skb_queue_after(list, old, newsk);
1843 spin_unlock_irqrestore(&list->lock, flags);
1848 * skb_insert - insert a buffer
1849 * @old: buffer to insert before
1850 * @newsk: buffer to insert
1851 * @list: list to use
1853 * Place a packet before a given packet in a list. The list locks are
1854 * taken and this function is atomic with respect to other list locked
1857 * A buffer cannot be placed on two lists at the same time.
1859 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1861 unsigned long flags;
1863 spin_lock_irqsave(&list->lock, flags);
1864 __skb_insert(newsk, old->prev, old, list);
1865 spin_unlock_irqrestore(&list->lock, flags);
1868 static inline void skb_split_inside_header(struct sk_buff *skb,
1869 struct sk_buff* skb1,
1870 const u32 len, const int pos)
1874 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1876 /* And move data appendix as is. */
1877 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1878 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1880 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1881 skb_shinfo(skb)->nr_frags = 0;
1882 skb1->data_len = skb->data_len;
1883 skb1->len += skb1->data_len;
1886 skb_set_tail_pointer(skb, len);
1889 static inline void skb_split_no_header(struct sk_buff *skb,
1890 struct sk_buff* skb1,
1891 const u32 len, int pos)
1894 const int nfrags = skb_shinfo(skb)->nr_frags;
1896 skb_shinfo(skb)->nr_frags = 0;
1897 skb1->len = skb1->data_len = skb->len - len;
1899 skb->data_len = len - pos;
1901 for (i = 0; i < nfrags; i++) {
1902 int size = skb_shinfo(skb)->frags[i].size;
1904 if (pos + size > len) {
1905 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1909 * We have two variants in this case:
1910 * 1. Move all the frag to the second
1911 * part, if it is possible. F.e.
1912 * this approach is mandatory for TUX,
1913 * where splitting is expensive.
1914 * 2. Split is accurately. We make this.
1916 get_page(skb_shinfo(skb)->frags[i].page);
1917 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1918 skb_shinfo(skb1)->frags[0].size -= len - pos;
1919 skb_shinfo(skb)->frags[i].size = len - pos;
1920 skb_shinfo(skb)->nr_frags++;
1924 skb_shinfo(skb)->nr_frags++;
1927 skb_shinfo(skb1)->nr_frags = k;
1931 * skb_split - Split fragmented skb to two parts at length len.
1932 * @skb: the buffer to split
1933 * @skb1: the buffer to receive the second part
1934 * @len: new length for skb
1936 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1938 int pos = skb_headlen(skb);
1940 if (len < pos) /* Split line is inside header. */
1941 skb_split_inside_header(skb, skb1, len, pos);
1942 else /* Second chunk has no header, nothing to copy. */
1943 skb_split_no_header(skb, skb1, len, pos);
1947 * skb_prepare_seq_read - Prepare a sequential read of skb data
1948 * @skb: the buffer to read
1949 * @from: lower offset of data to be read
1950 * @to: upper offset of data to be read
1951 * @st: state variable
1953 * Initializes the specified state variable. Must be called before
1954 * invoking skb_seq_read() for the first time.
1956 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1957 unsigned int to, struct skb_seq_state *st)
1959 st->lower_offset = from;
1960 st->upper_offset = to;
1961 st->root_skb = st->cur_skb = skb;
1962 st->frag_idx = st->stepped_offset = 0;
1963 st->frag_data = NULL;
1967 * skb_seq_read - Sequentially read skb data
1968 * @consumed: number of bytes consumed by the caller so far
1969 * @data: destination pointer for data to be returned
1970 * @st: state variable
1972 * Reads a block of skb data at &consumed relative to the
1973 * lower offset specified to skb_prepare_seq_read(). Assigns
1974 * the head of the data block to &data and returns the length
1975 * of the block or 0 if the end of the skb data or the upper
1976 * offset has been reached.
1978 * The caller is not required to consume all of the data
1979 * returned, i.e. &consumed is typically set to the number
1980 * of bytes already consumed and the next call to
1981 * skb_seq_read() will return the remaining part of the block.
1983 * Note 1: The size of each block of data returned can be arbitary,
1984 * this limitation is the cost for zerocopy seqeuental
1985 * reads of potentially non linear data.
1987 * Note 2: Fragment lists within fragments are not implemented
1988 * at the moment, state->root_skb could be replaced with
1989 * a stack for this purpose.
1991 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1992 struct skb_seq_state *st)
1994 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1997 if (unlikely(abs_offset >= st->upper_offset))
2001 block_limit = skb_headlen(st->cur_skb);
2003 if (abs_offset < block_limit) {
2004 *data = st->cur_skb->data + abs_offset;
2005 return block_limit - abs_offset;
2008 if (st->frag_idx == 0 && !st->frag_data)
2009 st->stepped_offset += skb_headlen(st->cur_skb);
2011 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2012 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2013 block_limit = frag->size + st->stepped_offset;
2015 if (abs_offset < block_limit) {
2017 st->frag_data = kmap_skb_frag(frag);
2019 *data = (u8 *) st->frag_data + frag->page_offset +
2020 (abs_offset - st->stepped_offset);
2022 return block_limit - abs_offset;
2025 if (st->frag_data) {
2026 kunmap_skb_frag(st->frag_data);
2027 st->frag_data = NULL;
2031 st->stepped_offset += frag->size;
2034 if (st->frag_data) {
2035 kunmap_skb_frag(st->frag_data);
2036 st->frag_data = NULL;
2039 if (st->cur_skb->next) {
2040 st->cur_skb = st->cur_skb->next;
2043 } else if (st->root_skb == st->cur_skb &&
2044 skb_shinfo(st->root_skb)->frag_list) {
2045 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2053 * skb_abort_seq_read - Abort a sequential read of skb data
2054 * @st: state variable
2056 * Must be called if skb_seq_read() was not called until it
2059 void skb_abort_seq_read(struct skb_seq_state *st)
2062 kunmap_skb_frag(st->frag_data);
2065 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2067 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2068 struct ts_config *conf,
2069 struct ts_state *state)
2071 return skb_seq_read(offset, text, TS_SKB_CB(state));
2074 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2076 skb_abort_seq_read(TS_SKB_CB(state));
2080 * skb_find_text - Find a text pattern in skb data
2081 * @skb: the buffer to look in
2082 * @from: search offset
2084 * @config: textsearch configuration
2085 * @state: uninitialized textsearch state variable
2087 * Finds a pattern in the skb data according to the specified
2088 * textsearch configuration. Use textsearch_next() to retrieve
2089 * subsequent occurrences of the pattern. Returns the offset
2090 * to the first occurrence or UINT_MAX if no match was found.
2092 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2093 unsigned int to, struct ts_config *config,
2094 struct ts_state *state)
2098 config->get_next_block = skb_ts_get_next_block;
2099 config->finish = skb_ts_finish;
2101 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2103 ret = textsearch_find(config, state);
2104 return (ret <= to - from ? ret : UINT_MAX);
2108 * skb_append_datato_frags: - append the user data to a skb
2109 * @sk: sock structure
2110 * @skb: skb structure to be appened with user data.
2111 * @getfrag: call back function to be used for getting the user data
2112 * @from: pointer to user message iov
2113 * @length: length of the iov message
2115 * Description: This procedure append the user data in the fragment part
2116 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2118 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2119 int (*getfrag)(void *from, char *to, int offset,
2120 int len, int odd, struct sk_buff *skb),
2121 void *from, int length)
2124 skb_frag_t *frag = NULL;
2125 struct page *page = NULL;
2131 /* Return error if we don't have space for new frag */
2132 frg_cnt = skb_shinfo(skb)->nr_frags;
2133 if (frg_cnt >= MAX_SKB_FRAGS)
2136 /* allocate a new page for next frag */
2137 page = alloc_pages(sk->sk_allocation, 0);
2139 /* If alloc_page fails just return failure and caller will
2140 * free previous allocated pages by doing kfree_skb()
2145 /* initialize the next frag */
2146 sk->sk_sndmsg_page = page;
2147 sk->sk_sndmsg_off = 0;
2148 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2149 skb->truesize += PAGE_SIZE;
2150 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2152 /* get the new initialized frag */
2153 frg_cnt = skb_shinfo(skb)->nr_frags;
2154 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2156 /* copy the user data to page */
2157 left = PAGE_SIZE - frag->page_offset;
2158 copy = (length > left)? left : length;
2160 ret = getfrag(from, (page_address(frag->page) +
2161 frag->page_offset + frag->size),
2162 offset, copy, 0, skb);
2166 /* copy was successful so update the size parameters */
2167 sk->sk_sndmsg_off += copy;
2170 skb->data_len += copy;
2174 } while (length > 0);
2180 * skb_pull_rcsum - pull skb and update receive checksum
2181 * @skb: buffer to update
2182 * @len: length of data pulled
2184 * This function performs an skb_pull on the packet and updates
2185 * the CHECKSUM_COMPLETE checksum. It should be used on
2186 * receive path processing instead of skb_pull unless you know
2187 * that the checksum difference is zero (e.g., a valid IP header)
2188 * or you are setting ip_summed to CHECKSUM_NONE.
2190 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2192 BUG_ON(len > skb->len);
2194 BUG_ON(skb->len < skb->data_len);
2195 skb_postpull_rcsum(skb, skb->data, len);
2196 return skb->data += len;
2199 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2202 * skb_segment - Perform protocol segmentation on skb.
2203 * @skb: buffer to segment
2204 * @features: features for the output path (see dev->features)
2206 * This function performs segmentation on the given skb. It returns
2207 * a pointer to the first in a list of new skbs for the segments.
2208 * In case of error it returns ERR_PTR(err).
2210 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2212 struct sk_buff *segs = NULL;
2213 struct sk_buff *tail = NULL;
2214 unsigned int mss = skb_shinfo(skb)->gso_size;
2215 unsigned int doffset = skb->data - skb_mac_header(skb);
2216 unsigned int offset = doffset;
2217 unsigned int headroom;
2219 int sg = features & NETIF_F_SG;
2220 int nfrags = skb_shinfo(skb)->nr_frags;
2225 __skb_push(skb, doffset);
2226 headroom = skb_headroom(skb);
2227 pos = skb_headlen(skb);
2230 struct sk_buff *nskb;
2236 len = skb->len - offset;
2240 hsize = skb_headlen(skb) - offset;
2243 if (hsize > len || !sg)
2246 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
2247 if (unlikely(!nskb))
2256 nskb->dev = skb->dev;
2257 skb_copy_queue_mapping(nskb, skb);
2258 nskb->priority = skb->priority;
2259 nskb->protocol = skb->protocol;
2260 nskb->vlan_tci = skb->vlan_tci;
2261 nskb->dst = dst_clone(skb->dst);
2262 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
2263 nskb->pkt_type = skb->pkt_type;
2264 nskb->mac_len = skb->mac_len;
2266 skb_reserve(nskb, headroom);
2267 skb_reset_mac_header(nskb);
2268 skb_set_network_header(nskb, skb->mac_len);
2269 nskb->transport_header = (nskb->network_header +
2270 skb_network_header_len(skb));
2271 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2274 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2280 frag = skb_shinfo(nskb)->frags;
2283 nskb->ip_summed = CHECKSUM_PARTIAL;
2284 nskb->csum = skb->csum;
2285 skb_copy_from_linear_data_offset(skb, offset,
2286 skb_put(nskb, hsize), hsize);
2288 while (pos < offset + len) {
2289 BUG_ON(i >= nfrags);
2291 *frag = skb_shinfo(skb)->frags[i];
2292 get_page(frag->page);
2296 frag->page_offset += offset - pos;
2297 frag->size -= offset - pos;
2302 if (pos + size <= offset + len) {
2306 frag->size -= pos + size - (offset + len);
2313 skb_shinfo(nskb)->nr_frags = k;
2314 nskb->data_len = len - hsize;
2315 nskb->len += nskb->data_len;
2316 nskb->truesize += nskb->data_len;
2317 } while ((offset += len) < skb->len);
2322 while ((skb = segs)) {
2326 return ERR_PTR(err);
2329 EXPORT_SYMBOL_GPL(skb_segment);
2331 void __init skb_init(void)
2333 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2334 sizeof(struct sk_buff),
2336 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2338 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2339 (2*sizeof(struct sk_buff)) +
2342 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2347 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2348 * @skb: Socket buffer containing the buffers to be mapped
2349 * @sg: The scatter-gather list to map into
2350 * @offset: The offset into the buffer's contents to start mapping
2351 * @len: Length of buffer space to be mapped
2353 * Fill the specified scatter-gather list with mappings/pointers into a
2354 * region of the buffer space attached to a socket buffer.
2357 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2359 int start = skb_headlen(skb);
2360 int i, copy = start - offset;
2366 sg_set_buf(sg, skb->data + offset, copy);
2368 if ((len -= copy) == 0)
2373 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2376 BUG_TRAP(start <= offset + len);
2378 end = start + skb_shinfo(skb)->frags[i].size;
2379 if ((copy = end - offset) > 0) {
2380 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2384 sg_set_page(&sg[elt], frag->page, copy,
2385 frag->page_offset+offset-start);
2394 if (skb_shinfo(skb)->frag_list) {
2395 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2397 for (; list; list = list->next) {
2400 BUG_TRAP(start <= offset + len);
2402 end = start + list->len;
2403 if ((copy = end - offset) > 0) {
2406 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2408 if ((len -= copy) == 0)
2419 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2421 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2423 sg_mark_end(&sg[nsg - 1]);
2429 * skb_cow_data - Check that a socket buffer's data buffers are writable
2430 * @skb: The socket buffer to check.
2431 * @tailbits: Amount of trailing space to be added
2432 * @trailer: Returned pointer to the skb where the @tailbits space begins
2434 * Make sure that the data buffers attached to a socket buffer are
2435 * writable. If they are not, private copies are made of the data buffers
2436 * and the socket buffer is set to use these instead.
2438 * If @tailbits is given, make sure that there is space to write @tailbits
2439 * bytes of data beyond current end of socket buffer. @trailer will be
2440 * set to point to the skb in which this space begins.
2442 * The number of scatterlist elements required to completely map the
2443 * COW'd and extended socket buffer will be returned.
2445 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2449 struct sk_buff *skb1, **skb_p;
2451 /* If skb is cloned or its head is paged, reallocate
2452 * head pulling out all the pages (pages are considered not writable
2453 * at the moment even if they are anonymous).
2455 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2456 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2459 /* Easy case. Most of packets will go this way. */
2460 if (!skb_shinfo(skb)->frag_list) {
2461 /* A little of trouble, not enough of space for trailer.
2462 * This should not happen, when stack is tuned to generate
2463 * good frames. OK, on miss we reallocate and reserve even more
2464 * space, 128 bytes is fair. */
2466 if (skb_tailroom(skb) < tailbits &&
2467 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2475 /* Misery. We are in troubles, going to mincer fragments... */
2478 skb_p = &skb_shinfo(skb)->frag_list;
2481 while ((skb1 = *skb_p) != NULL) {
2484 /* The fragment is partially pulled by someone,
2485 * this can happen on input. Copy it and everything
2488 if (skb_shared(skb1))
2491 /* If the skb is the last, worry about trailer. */
2493 if (skb1->next == NULL && tailbits) {
2494 if (skb_shinfo(skb1)->nr_frags ||
2495 skb_shinfo(skb1)->frag_list ||
2496 skb_tailroom(skb1) < tailbits)
2497 ntail = tailbits + 128;
2503 skb_shinfo(skb1)->nr_frags ||
2504 skb_shinfo(skb1)->frag_list) {
2505 struct sk_buff *skb2;
2507 /* Fuck, we are miserable poor guys... */
2509 skb2 = skb_copy(skb1, GFP_ATOMIC);
2511 skb2 = skb_copy_expand(skb1,
2515 if (unlikely(skb2 == NULL))
2519 skb_set_owner_w(skb2, skb1->sk);
2521 /* Looking around. Are we still alive?
2522 * OK, link new skb, drop old one */
2524 skb2->next = skb1->next;
2531 skb_p = &skb1->next;
2538 * skb_partial_csum_set - set up and verify partial csum values for packet
2539 * @skb: the skb to set
2540 * @start: the number of bytes after skb->data to start checksumming.
2541 * @off: the offset from start to place the checksum.
2543 * For untrusted partially-checksummed packets, we need to make sure the values
2544 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2546 * This function checks and sets those values and skb->ip_summed: if this
2547 * returns false you should drop the packet.
2549 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2551 if (unlikely(start > skb->len - 2) ||
2552 unlikely((int)start + off > skb->len - 2)) {
2553 if (net_ratelimit())
2555 "bad partial csum: csum=%u/%u len=%u\n",
2556 start, off, skb->len);
2559 skb->ip_summed = CHECKSUM_PARTIAL;
2560 skb->csum_start = skb_headroom(skb) + start;
2561 skb->csum_offset = off;
2565 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2567 if (net_ratelimit())
2568 pr_warning("%s: received packets cannot be forwarded"
2569 " while LRO is enabled\n", skb->dev->name);
2572 EXPORT_SYMBOL(___pskb_trim);
2573 EXPORT_SYMBOL(__kfree_skb);
2574 EXPORT_SYMBOL(kfree_skb);
2575 EXPORT_SYMBOL(__pskb_pull_tail);
2576 EXPORT_SYMBOL(__alloc_skb);
2577 EXPORT_SYMBOL(__netdev_alloc_skb);
2578 EXPORT_SYMBOL(pskb_copy);
2579 EXPORT_SYMBOL(pskb_expand_head);
2580 EXPORT_SYMBOL(skb_checksum);
2581 EXPORT_SYMBOL(skb_clone);
2582 EXPORT_SYMBOL(skb_copy);
2583 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2584 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2585 EXPORT_SYMBOL(skb_copy_bits);
2586 EXPORT_SYMBOL(skb_copy_expand);
2587 EXPORT_SYMBOL(skb_over_panic);
2588 EXPORT_SYMBOL(skb_pad);
2589 EXPORT_SYMBOL(skb_realloc_headroom);
2590 EXPORT_SYMBOL(skb_under_panic);
2591 EXPORT_SYMBOL(skb_dequeue);
2592 EXPORT_SYMBOL(skb_dequeue_tail);
2593 EXPORT_SYMBOL(skb_insert);
2594 EXPORT_SYMBOL(skb_queue_purge);
2595 EXPORT_SYMBOL(skb_queue_head);
2596 EXPORT_SYMBOL(skb_queue_tail);
2597 EXPORT_SYMBOL(skb_unlink);
2598 EXPORT_SYMBOL(skb_append);
2599 EXPORT_SYMBOL(skb_split);
2600 EXPORT_SYMBOL(skb_prepare_seq_read);
2601 EXPORT_SYMBOL(skb_seq_read);
2602 EXPORT_SYMBOL(skb_abort_seq_read);
2603 EXPORT_SYMBOL(skb_find_text);
2604 EXPORT_SYMBOL(skb_append_datato_frags);
2605 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
2607 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2608 EXPORT_SYMBOL_GPL(skb_cow_data);
2609 EXPORT_SYMBOL_GPL(skb_partial_csum_set);