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 #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
491 atomic_set(&n->users, 1);
493 atomic_inc(&(skb_shinfo(skb)->dataref));
501 * skb_morph - morph one skb into another
502 * @dst: the skb to receive the contents
503 * @src: the skb to supply the contents
505 * This is identical to skb_clone except that the target skb is
506 * supplied by the user.
508 * The target skb is returned upon exit.
510 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
512 skb_release_all(dst);
513 return __skb_clone(dst, src);
515 EXPORT_SYMBOL_GPL(skb_morph);
518 * skb_clone - duplicate an sk_buff
519 * @skb: buffer to clone
520 * @gfp_mask: allocation priority
522 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
523 * copies share the same packet data but not structure. The new
524 * buffer has a reference count of 1. If the allocation fails the
525 * function returns %NULL otherwise the new buffer is returned.
527 * If this function is called from an interrupt gfp_mask() must be
531 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
536 if (skb->fclone == SKB_FCLONE_ORIG &&
537 n->fclone == SKB_FCLONE_UNAVAILABLE) {
538 atomic_t *fclone_ref = (atomic_t *) (n + 1);
539 n->fclone = SKB_FCLONE_CLONE;
540 atomic_inc(fclone_ref);
542 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
545 n->fclone = SKB_FCLONE_UNAVAILABLE;
548 return __skb_clone(n, skb);
551 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
553 #ifndef NET_SKBUFF_DATA_USES_OFFSET
555 * Shift between the two data areas in bytes
557 unsigned long offset = new->data - old->data;
560 __copy_skb_header(new, old);
562 #ifndef NET_SKBUFF_DATA_USES_OFFSET
563 /* {transport,network,mac}_header are relative to skb->head */
564 new->transport_header += offset;
565 new->network_header += offset;
566 new->mac_header += offset;
568 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
569 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
570 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
574 * skb_copy - create private copy of an sk_buff
575 * @skb: buffer to copy
576 * @gfp_mask: allocation priority
578 * Make a copy of both an &sk_buff and its data. This is used when the
579 * caller wishes to modify the data and needs a private copy of the
580 * data to alter. Returns %NULL on failure or the pointer to the buffer
581 * on success. The returned buffer has a reference count of 1.
583 * As by-product this function converts non-linear &sk_buff to linear
584 * one, so that &sk_buff becomes completely private and caller is allowed
585 * to modify all the data of returned buffer. This means that this
586 * function is not recommended for use in circumstances when only
587 * header is going to be modified. Use pskb_copy() instead.
590 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
592 int headerlen = skb->data - skb->head;
594 * Allocate the copy buffer
597 #ifdef NET_SKBUFF_DATA_USES_OFFSET
598 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
600 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
605 /* Set the data pointer */
606 skb_reserve(n, headerlen);
607 /* Set the tail pointer and length */
608 skb_put(n, skb->len);
610 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
613 copy_skb_header(n, skb);
619 * pskb_copy - create copy of an sk_buff with private head.
620 * @skb: buffer to copy
621 * @gfp_mask: allocation priority
623 * Make a copy of both an &sk_buff and part of its data, located
624 * in header. Fragmented data remain shared. This is used when
625 * the caller wishes to modify only header of &sk_buff and needs
626 * private copy of the header to alter. Returns %NULL on failure
627 * or the pointer to the buffer on success.
628 * The returned buffer has a reference count of 1.
631 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
634 * Allocate the copy buffer
637 #ifdef NET_SKBUFF_DATA_USES_OFFSET
638 n = alloc_skb(skb->end, gfp_mask);
640 n = alloc_skb(skb->end - skb->head, gfp_mask);
645 /* Set the data pointer */
646 skb_reserve(n, skb->data - skb->head);
647 /* Set the tail pointer and length */
648 skb_put(n, skb_headlen(skb));
650 skb_copy_from_linear_data(skb, n->data, n->len);
652 n->truesize += skb->data_len;
653 n->data_len = skb->data_len;
656 if (skb_shinfo(skb)->nr_frags) {
659 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
660 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
661 get_page(skb_shinfo(n)->frags[i].page);
663 skb_shinfo(n)->nr_frags = i;
666 if (skb_shinfo(skb)->frag_list) {
667 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
668 skb_clone_fraglist(n);
671 copy_skb_header(n, skb);
677 * pskb_expand_head - reallocate header of &sk_buff
678 * @skb: buffer to reallocate
679 * @nhead: room to add at head
680 * @ntail: room to add at tail
681 * @gfp_mask: allocation priority
683 * Expands (or creates identical copy, if &nhead and &ntail are zero)
684 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
685 * reference count of 1. Returns zero in the case of success or error,
686 * if expansion failed. In the last case, &sk_buff is not changed.
688 * All the pointers pointing into skb header may change and must be
689 * reloaded after call to this function.
692 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
697 #ifdef NET_SKBUFF_DATA_USES_OFFSET
698 int size = nhead + skb->end + ntail;
700 int size = nhead + (skb->end - skb->head) + ntail;
707 size = SKB_DATA_ALIGN(size);
709 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
713 /* Copy only real data... and, alas, header. This should be
714 * optimized for the cases when header is void. */
715 #ifdef NET_SKBUFF_DATA_USES_OFFSET
716 memcpy(data + nhead, skb->head, skb->tail);
718 memcpy(data + nhead, skb->head, skb->tail - skb->head);
720 memcpy(data + size, skb_end_pointer(skb),
721 sizeof(struct skb_shared_info));
723 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
724 get_page(skb_shinfo(skb)->frags[i].page);
726 if (skb_shinfo(skb)->frag_list)
727 skb_clone_fraglist(skb);
729 skb_release_data(skb);
731 off = (data + nhead) - skb->head;
735 #ifdef NET_SKBUFF_DATA_USES_OFFSET
739 skb->end = skb->head + size;
741 /* {transport,network,mac}_header and tail are relative to skb->head */
743 skb->transport_header += off;
744 skb->network_header += off;
745 skb->mac_header += off;
746 skb->csum_start += nhead;
750 atomic_set(&skb_shinfo(skb)->dataref, 1);
757 /* Make private copy of skb with writable head and some headroom */
759 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
761 struct sk_buff *skb2;
762 int delta = headroom - skb_headroom(skb);
765 skb2 = pskb_copy(skb, GFP_ATOMIC);
767 skb2 = skb_clone(skb, GFP_ATOMIC);
768 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
779 * skb_copy_expand - copy and expand sk_buff
780 * @skb: buffer to copy
781 * @newheadroom: new free bytes at head
782 * @newtailroom: new free bytes at tail
783 * @gfp_mask: allocation priority
785 * Make a copy of both an &sk_buff and its data and while doing so
786 * allocate additional space.
788 * This is used when the caller wishes to modify the data and needs a
789 * private copy of the data to alter as well as more space for new fields.
790 * Returns %NULL on failure or the pointer to the buffer
791 * on success. The returned buffer has a reference count of 1.
793 * You must pass %GFP_ATOMIC as the allocation priority if this function
794 * is called from an interrupt.
796 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
797 int newheadroom, int newtailroom,
801 * Allocate the copy buffer
803 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
805 int oldheadroom = skb_headroom(skb);
806 int head_copy_len, head_copy_off;
812 skb_reserve(n, newheadroom);
814 /* Set the tail pointer and length */
815 skb_put(n, skb->len);
817 head_copy_len = oldheadroom;
819 if (newheadroom <= head_copy_len)
820 head_copy_len = newheadroom;
822 head_copy_off = newheadroom - head_copy_len;
824 /* Copy the linear header and data. */
825 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
826 skb->len + head_copy_len))
829 copy_skb_header(n, skb);
831 off = newheadroom - oldheadroom;
832 n->csum_start += off;
833 #ifdef NET_SKBUFF_DATA_USES_OFFSET
834 n->transport_header += off;
835 n->network_header += off;
836 n->mac_header += off;
843 * skb_pad - zero pad the tail of an skb
844 * @skb: buffer to pad
847 * Ensure that a buffer is followed by a padding area that is zero
848 * filled. Used by network drivers which may DMA or transfer data
849 * beyond the buffer end onto the wire.
851 * May return error in out of memory cases. The skb is freed on error.
854 int skb_pad(struct sk_buff *skb, int pad)
859 /* If the skbuff is non linear tailroom is always zero.. */
860 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
861 memset(skb->data+skb->len, 0, pad);
865 ntail = skb->data_len + pad - (skb->end - skb->tail);
866 if (likely(skb_cloned(skb) || ntail > 0)) {
867 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
872 /* FIXME: The use of this function with non-linear skb's really needs
875 err = skb_linearize(skb);
879 memset(skb->data + skb->len, 0, pad);
888 * skb_put - add data to a buffer
889 * @skb: buffer to use
890 * @len: amount of data to add
892 * This function extends the used data area of the buffer. If this would
893 * exceed the total buffer size the kernel will panic. A pointer to the
894 * first byte of the extra data is returned.
896 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
898 unsigned char *tmp = skb_tail_pointer(skb);
899 SKB_LINEAR_ASSERT(skb);
902 if (unlikely(skb->tail > skb->end))
903 skb_over_panic(skb, len, __builtin_return_address(0));
906 EXPORT_SYMBOL(skb_put);
909 * skb_push - add data to the start of a buffer
910 * @skb: buffer to use
911 * @len: amount of data to add
913 * This function extends the used data area of the buffer at the buffer
914 * start. If this would exceed the total buffer headroom the kernel will
915 * panic. A pointer to the first byte of the extra data is returned.
917 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
921 if (unlikely(skb->data<skb->head))
922 skb_under_panic(skb, len, __builtin_return_address(0));
925 EXPORT_SYMBOL(skb_push);
928 * skb_pull - remove data from the start of a buffer
929 * @skb: buffer to use
930 * @len: amount of data to remove
932 * This function removes data from the start of a buffer, returning
933 * the memory to the headroom. A pointer to the next data in the buffer
934 * is returned. Once the data has been pulled future pushes will overwrite
937 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
939 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
941 EXPORT_SYMBOL(skb_pull);
944 * skb_trim - remove end from a buffer
945 * @skb: buffer to alter
948 * Cut the length of a buffer down by removing data from the tail. If
949 * the buffer is already under the length specified it is not modified.
950 * The skb must be linear.
952 void skb_trim(struct sk_buff *skb, unsigned int len)
955 __skb_trim(skb, len);
957 EXPORT_SYMBOL(skb_trim);
959 /* Trims skb to length len. It can change skb pointers.
962 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
964 struct sk_buff **fragp;
965 struct sk_buff *frag;
966 int offset = skb_headlen(skb);
967 int nfrags = skb_shinfo(skb)->nr_frags;
971 if (skb_cloned(skb) &&
972 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
979 for (; i < nfrags; i++) {
980 int end = offset + skb_shinfo(skb)->frags[i].size;
987 skb_shinfo(skb)->frags[i++].size = len - offset;
990 skb_shinfo(skb)->nr_frags = i;
992 for (; i < nfrags; i++)
993 put_page(skb_shinfo(skb)->frags[i].page);
995 if (skb_shinfo(skb)->frag_list)
996 skb_drop_fraglist(skb);
1000 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1001 fragp = &frag->next) {
1002 int end = offset + frag->len;
1004 if (skb_shared(frag)) {
1005 struct sk_buff *nfrag;
1007 nfrag = skb_clone(frag, GFP_ATOMIC);
1008 if (unlikely(!nfrag))
1011 nfrag->next = frag->next;
1023 unlikely((err = pskb_trim(frag, len - offset))))
1027 skb_drop_list(&frag->next);
1032 if (len > skb_headlen(skb)) {
1033 skb->data_len -= skb->len - len;
1038 skb_set_tail_pointer(skb, len);
1045 * __pskb_pull_tail - advance tail of skb header
1046 * @skb: buffer to reallocate
1047 * @delta: number of bytes to advance tail
1049 * The function makes a sense only on a fragmented &sk_buff,
1050 * it expands header moving its tail forward and copying necessary
1051 * data from fragmented part.
1053 * &sk_buff MUST have reference count of 1.
1055 * Returns %NULL (and &sk_buff does not change) if pull failed
1056 * or value of new tail of skb in the case of success.
1058 * All the pointers pointing into skb header may change and must be
1059 * reloaded after call to this function.
1062 /* Moves tail of skb head forward, copying data from fragmented part,
1063 * when it is necessary.
1064 * 1. It may fail due to malloc failure.
1065 * 2. It may change skb pointers.
1067 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1069 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1071 /* If skb has not enough free space at tail, get new one
1072 * plus 128 bytes for future expansions. If we have enough
1073 * room at tail, reallocate without expansion only if skb is cloned.
1075 int i, k, eat = (skb->tail + delta) - skb->end;
1077 if (eat > 0 || skb_cloned(skb)) {
1078 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1083 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1086 /* Optimization: no fragments, no reasons to preestimate
1087 * size of pulled pages. Superb.
1089 if (!skb_shinfo(skb)->frag_list)
1092 /* Estimate size of pulled pages. */
1094 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1095 if (skb_shinfo(skb)->frags[i].size >= eat)
1097 eat -= skb_shinfo(skb)->frags[i].size;
1100 /* If we need update frag list, we are in troubles.
1101 * Certainly, it possible to add an offset to skb data,
1102 * but taking into account that pulling is expected to
1103 * be very rare operation, it is worth to fight against
1104 * further bloating skb head and crucify ourselves here instead.
1105 * Pure masohism, indeed. 8)8)
1108 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1109 struct sk_buff *clone = NULL;
1110 struct sk_buff *insp = NULL;
1115 if (list->len <= eat) {
1116 /* Eaten as whole. */
1121 /* Eaten partially. */
1123 if (skb_shared(list)) {
1124 /* Sucks! We need to fork list. :-( */
1125 clone = skb_clone(list, GFP_ATOMIC);
1131 /* This may be pulled without
1135 if (!pskb_pull(list, eat)) {
1144 /* Free pulled out fragments. */
1145 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1146 skb_shinfo(skb)->frag_list = list->next;
1149 /* And insert new clone at head. */
1152 skb_shinfo(skb)->frag_list = clone;
1155 /* Success! Now we may commit changes to skb data. */
1160 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1161 if (skb_shinfo(skb)->frags[i].size <= eat) {
1162 put_page(skb_shinfo(skb)->frags[i].page);
1163 eat -= skb_shinfo(skb)->frags[i].size;
1165 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1167 skb_shinfo(skb)->frags[k].page_offset += eat;
1168 skb_shinfo(skb)->frags[k].size -= eat;
1174 skb_shinfo(skb)->nr_frags = k;
1177 skb->data_len -= delta;
1179 return skb_tail_pointer(skb);
1182 /* Copy some data bits from skb to kernel buffer. */
1184 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1187 int start = skb_headlen(skb);
1189 if (offset > (int)skb->len - len)
1193 if ((copy = start - offset) > 0) {
1196 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1197 if ((len -= copy) == 0)
1203 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1206 WARN_ON(start > offset + len);
1208 end = start + skb_shinfo(skb)->frags[i].size;
1209 if ((copy = end - offset) > 0) {
1215 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1217 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1218 offset - start, copy);
1219 kunmap_skb_frag(vaddr);
1221 if ((len -= copy) == 0)
1229 if (skb_shinfo(skb)->frag_list) {
1230 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1232 for (; list; list = list->next) {
1235 WARN_ON(start > offset + len);
1237 end = start + list->len;
1238 if ((copy = end - offset) > 0) {
1241 if (skb_copy_bits(list, offset - start,
1244 if ((len -= copy) == 0)
1260 * Callback from splice_to_pipe(), if we need to release some pages
1261 * at the end of the spd in case we error'ed out in filling the pipe.
1263 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1265 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1271 * Fill page/offset/length into spd, if it can hold more pages.
1273 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1274 unsigned int len, unsigned int offset,
1275 struct sk_buff *skb)
1277 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1280 spd->pages[spd->nr_pages] = page;
1281 spd->partial[spd->nr_pages].len = len;
1282 spd->partial[spd->nr_pages].offset = offset;
1283 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1288 static inline void __segment_seek(struct page **page, unsigned int *poff,
1289 unsigned int *plen, unsigned int off)
1292 *page += *poff / PAGE_SIZE;
1293 *poff = *poff % PAGE_SIZE;
1297 static inline int __splice_segment(struct page *page, unsigned int poff,
1298 unsigned int plen, unsigned int *off,
1299 unsigned int *len, struct sk_buff *skb,
1300 struct splice_pipe_desc *spd)
1305 /* skip this segment if already processed */
1311 /* ignore any bits we already processed */
1313 __segment_seek(&page, &poff, &plen, *off);
1318 unsigned int flen = min(*len, plen);
1320 /* the linear region may spread across several pages */
1321 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1323 if (spd_fill_page(spd, page, flen, poff, skb))
1326 __segment_seek(&page, &poff, &plen, flen);
1329 } while (*len && plen);
1335 * Map linear and fragment data from the skb to spd. It reports failure if the
1336 * pipe is full or if we already spliced the requested length.
1338 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1340 struct splice_pipe_desc *spd)
1345 * map the linear part
1347 if (__splice_segment(virt_to_page(skb->data),
1348 (unsigned long) skb->data & (PAGE_SIZE - 1),
1350 offset, len, skb, spd))
1354 * then map the fragments
1356 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1357 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1359 if (__splice_segment(f->page, f->page_offset, f->size,
1360 offset, len, skb, spd))
1368 * Map data from the skb to a pipe. Should handle both the linear part,
1369 * the fragments, and the frag list. It does NOT handle frag lists within
1370 * the frag list, if such a thing exists. We'd probably need to recurse to
1371 * handle that cleanly.
1373 int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1374 struct pipe_inode_info *pipe, unsigned int tlen,
1377 struct partial_page partial[PIPE_BUFFERS];
1378 struct page *pages[PIPE_BUFFERS];
1379 struct splice_pipe_desc spd = {
1383 .ops = &sock_pipe_buf_ops,
1384 .spd_release = sock_spd_release,
1386 struct sk_buff *skb;
1389 * I'd love to avoid the clone here, but tcp_read_sock()
1390 * ignores reference counts and unconditonally kills the sk_buff
1391 * on return from the actor.
1393 skb = skb_clone(__skb, GFP_KERNEL);
1398 * __skb_splice_bits() only fails if the output has no room left,
1399 * so no point in going over the frag_list for the error case.
1401 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1407 * now see if we have a frag_list to map
1409 if (skb_shinfo(skb)->frag_list) {
1410 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1412 for (; list && tlen; list = list->next) {
1413 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1420 * drop our reference to the clone, the pipe consumption will
1427 struct sock *sk = __skb->sk;
1430 * Drop the socket lock, otherwise we have reverse
1431 * locking dependencies between sk_lock and i_mutex
1432 * here as compared to sendfile(). We enter here
1433 * with the socket lock held, and splice_to_pipe() will
1434 * grab the pipe inode lock. For sendfile() emulation,
1435 * we call into ->sendpage() with the i_mutex lock held
1436 * and networking will grab the socket lock.
1439 ret = splice_to_pipe(pipe, &spd);
1448 * skb_store_bits - store bits from kernel buffer to skb
1449 * @skb: destination buffer
1450 * @offset: offset in destination
1451 * @from: source buffer
1452 * @len: number of bytes to copy
1454 * Copy the specified number of bytes from the source buffer to the
1455 * destination skb. This function handles all the messy bits of
1456 * traversing fragment lists and such.
1459 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1462 int start = skb_headlen(skb);
1464 if (offset > (int)skb->len - len)
1467 if ((copy = start - offset) > 0) {
1470 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1471 if ((len -= copy) == 0)
1477 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1478 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1481 WARN_ON(start > offset + len);
1483 end = start + frag->size;
1484 if ((copy = end - offset) > 0) {
1490 vaddr = kmap_skb_frag(frag);
1491 memcpy(vaddr + frag->page_offset + offset - start,
1493 kunmap_skb_frag(vaddr);
1495 if ((len -= copy) == 0)
1503 if (skb_shinfo(skb)->frag_list) {
1504 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1506 for (; list; list = list->next) {
1509 WARN_ON(start > offset + len);
1511 end = start + list->len;
1512 if ((copy = end - offset) > 0) {
1515 if (skb_store_bits(list, offset - start,
1518 if ((len -= copy) == 0)
1533 EXPORT_SYMBOL(skb_store_bits);
1535 /* Checksum skb data. */
1537 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1538 int len, __wsum csum)
1540 int start = skb_headlen(skb);
1541 int i, copy = start - offset;
1544 /* Checksum header. */
1548 csum = csum_partial(skb->data + offset, copy, csum);
1549 if ((len -= copy) == 0)
1555 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1558 WARN_ON(start > offset + len);
1560 end = start + skb_shinfo(skb)->frags[i].size;
1561 if ((copy = end - offset) > 0) {
1564 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1568 vaddr = kmap_skb_frag(frag);
1569 csum2 = csum_partial(vaddr + frag->page_offset +
1570 offset - start, copy, 0);
1571 kunmap_skb_frag(vaddr);
1572 csum = csum_block_add(csum, csum2, pos);
1581 if (skb_shinfo(skb)->frag_list) {
1582 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1584 for (; list; list = list->next) {
1587 WARN_ON(start > offset + len);
1589 end = start + list->len;
1590 if ((copy = end - offset) > 0) {
1594 csum2 = skb_checksum(list, offset - start,
1596 csum = csum_block_add(csum, csum2, pos);
1597 if ((len -= copy) == 0)
1610 /* Both of above in one bottle. */
1612 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1613 u8 *to, int len, __wsum csum)
1615 int start = skb_headlen(skb);
1616 int i, copy = start - offset;
1623 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1625 if ((len -= copy) == 0)
1632 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1635 WARN_ON(start > offset + len);
1637 end = start + skb_shinfo(skb)->frags[i].size;
1638 if ((copy = end - offset) > 0) {
1641 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1645 vaddr = kmap_skb_frag(frag);
1646 csum2 = csum_partial_copy_nocheck(vaddr +
1650 kunmap_skb_frag(vaddr);
1651 csum = csum_block_add(csum, csum2, pos);
1661 if (skb_shinfo(skb)->frag_list) {
1662 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1664 for (; list; list = list->next) {
1668 WARN_ON(start > offset + len);
1670 end = start + list->len;
1671 if ((copy = end - offset) > 0) {
1674 csum2 = skb_copy_and_csum_bits(list,
1677 csum = csum_block_add(csum, csum2, pos);
1678 if ((len -= copy) == 0)
1691 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1696 if (skb->ip_summed == CHECKSUM_PARTIAL)
1697 csstart = skb->csum_start - skb_headroom(skb);
1699 csstart = skb_headlen(skb);
1701 BUG_ON(csstart > skb_headlen(skb));
1703 skb_copy_from_linear_data(skb, to, csstart);
1706 if (csstart != skb->len)
1707 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1708 skb->len - csstart, 0);
1710 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1711 long csstuff = csstart + skb->csum_offset;
1713 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1718 * skb_dequeue - remove from the head of the queue
1719 * @list: list to dequeue from
1721 * Remove the head of the list. The list lock is taken so the function
1722 * may be used safely with other locking list functions. The head item is
1723 * returned or %NULL if the list is empty.
1726 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1728 unsigned long flags;
1729 struct sk_buff *result;
1731 spin_lock_irqsave(&list->lock, flags);
1732 result = __skb_dequeue(list);
1733 spin_unlock_irqrestore(&list->lock, flags);
1738 * skb_dequeue_tail - remove from the tail of the queue
1739 * @list: list to dequeue from
1741 * Remove the tail of the list. The list lock is taken so the function
1742 * may be used safely with other locking list functions. The tail item is
1743 * returned or %NULL if the list is empty.
1745 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1747 unsigned long flags;
1748 struct sk_buff *result;
1750 spin_lock_irqsave(&list->lock, flags);
1751 result = __skb_dequeue_tail(list);
1752 spin_unlock_irqrestore(&list->lock, flags);
1757 * skb_queue_purge - empty a list
1758 * @list: list to empty
1760 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1761 * the list and one reference dropped. This function takes the list
1762 * lock and is atomic with respect to other list locking functions.
1764 void skb_queue_purge(struct sk_buff_head *list)
1766 struct sk_buff *skb;
1767 while ((skb = skb_dequeue(list)) != NULL)
1772 * skb_queue_head - queue a buffer at the list head
1773 * @list: list to use
1774 * @newsk: buffer to queue
1776 * Queue a buffer at the start of the list. This function takes the
1777 * list lock and can be used safely with other locking &sk_buff functions
1780 * A buffer cannot be placed on two lists at the same time.
1782 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1784 unsigned long flags;
1786 spin_lock_irqsave(&list->lock, flags);
1787 __skb_queue_head(list, newsk);
1788 spin_unlock_irqrestore(&list->lock, flags);
1792 * skb_queue_tail - queue a buffer at the list tail
1793 * @list: list to use
1794 * @newsk: buffer to queue
1796 * Queue a buffer at the tail of the list. This function takes the
1797 * list lock and can be used safely with other locking &sk_buff functions
1800 * A buffer cannot be placed on two lists at the same time.
1802 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1804 unsigned long flags;
1806 spin_lock_irqsave(&list->lock, flags);
1807 __skb_queue_tail(list, newsk);
1808 spin_unlock_irqrestore(&list->lock, flags);
1812 * skb_unlink - remove a buffer from a list
1813 * @skb: buffer to remove
1814 * @list: list to use
1816 * Remove a packet from a list. The list locks are taken and this
1817 * function is atomic with respect to other list locked calls
1819 * You must know what list the SKB is on.
1821 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1823 unsigned long flags;
1825 spin_lock_irqsave(&list->lock, flags);
1826 __skb_unlink(skb, list);
1827 spin_unlock_irqrestore(&list->lock, flags);
1831 * skb_append - append a buffer
1832 * @old: buffer to insert after
1833 * @newsk: buffer to insert
1834 * @list: list to use
1836 * Place a packet after a given packet in a list. The list locks are taken
1837 * and this function is atomic with respect to other list locked calls.
1838 * A buffer cannot be placed on two lists at the same time.
1840 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1842 unsigned long flags;
1844 spin_lock_irqsave(&list->lock, flags);
1845 __skb_queue_after(list, old, newsk);
1846 spin_unlock_irqrestore(&list->lock, flags);
1851 * skb_insert - insert a buffer
1852 * @old: buffer to insert before
1853 * @newsk: buffer to insert
1854 * @list: list to use
1856 * Place a packet before a given packet in a list. The list locks are
1857 * taken and this function is atomic with respect to other list locked
1860 * A buffer cannot be placed on two lists at the same time.
1862 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1864 unsigned long flags;
1866 spin_lock_irqsave(&list->lock, flags);
1867 __skb_insert(newsk, old->prev, old, list);
1868 spin_unlock_irqrestore(&list->lock, flags);
1871 static inline void skb_split_inside_header(struct sk_buff *skb,
1872 struct sk_buff* skb1,
1873 const u32 len, const int pos)
1877 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1879 /* And move data appendix as is. */
1880 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1881 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1883 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1884 skb_shinfo(skb)->nr_frags = 0;
1885 skb1->data_len = skb->data_len;
1886 skb1->len += skb1->data_len;
1889 skb_set_tail_pointer(skb, len);
1892 static inline void skb_split_no_header(struct sk_buff *skb,
1893 struct sk_buff* skb1,
1894 const u32 len, int pos)
1897 const int nfrags = skb_shinfo(skb)->nr_frags;
1899 skb_shinfo(skb)->nr_frags = 0;
1900 skb1->len = skb1->data_len = skb->len - len;
1902 skb->data_len = len - pos;
1904 for (i = 0; i < nfrags; i++) {
1905 int size = skb_shinfo(skb)->frags[i].size;
1907 if (pos + size > len) {
1908 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1912 * We have two variants in this case:
1913 * 1. Move all the frag to the second
1914 * part, if it is possible. F.e.
1915 * this approach is mandatory for TUX,
1916 * where splitting is expensive.
1917 * 2. Split is accurately. We make this.
1919 get_page(skb_shinfo(skb)->frags[i].page);
1920 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1921 skb_shinfo(skb1)->frags[0].size -= len - pos;
1922 skb_shinfo(skb)->frags[i].size = len - pos;
1923 skb_shinfo(skb)->nr_frags++;
1927 skb_shinfo(skb)->nr_frags++;
1930 skb_shinfo(skb1)->nr_frags = k;
1934 * skb_split - Split fragmented skb to two parts at length len.
1935 * @skb: the buffer to split
1936 * @skb1: the buffer to receive the second part
1937 * @len: new length for skb
1939 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1941 int pos = skb_headlen(skb);
1943 if (len < pos) /* Split line is inside header. */
1944 skb_split_inside_header(skb, skb1, len, pos);
1945 else /* Second chunk has no header, nothing to copy. */
1946 skb_split_no_header(skb, skb1, len, pos);
1950 * skb_prepare_seq_read - Prepare a sequential read of skb data
1951 * @skb: the buffer to read
1952 * @from: lower offset of data to be read
1953 * @to: upper offset of data to be read
1954 * @st: state variable
1956 * Initializes the specified state variable. Must be called before
1957 * invoking skb_seq_read() for the first time.
1959 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1960 unsigned int to, struct skb_seq_state *st)
1962 st->lower_offset = from;
1963 st->upper_offset = to;
1964 st->root_skb = st->cur_skb = skb;
1965 st->frag_idx = st->stepped_offset = 0;
1966 st->frag_data = NULL;
1970 * skb_seq_read - Sequentially read skb data
1971 * @consumed: number of bytes consumed by the caller so far
1972 * @data: destination pointer for data to be returned
1973 * @st: state variable
1975 * Reads a block of skb data at &consumed relative to the
1976 * lower offset specified to skb_prepare_seq_read(). Assigns
1977 * the head of the data block to &data and returns the length
1978 * of the block or 0 if the end of the skb data or the upper
1979 * offset has been reached.
1981 * The caller is not required to consume all of the data
1982 * returned, i.e. &consumed is typically set to the number
1983 * of bytes already consumed and the next call to
1984 * skb_seq_read() will return the remaining part of the block.
1986 * Note 1: The size of each block of data returned can be arbitary,
1987 * this limitation is the cost for zerocopy seqeuental
1988 * reads of potentially non linear data.
1990 * Note 2: Fragment lists within fragments are not implemented
1991 * at the moment, state->root_skb could be replaced with
1992 * a stack for this purpose.
1994 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1995 struct skb_seq_state *st)
1997 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2000 if (unlikely(abs_offset >= st->upper_offset))
2004 block_limit = skb_headlen(st->cur_skb);
2006 if (abs_offset < block_limit) {
2007 *data = st->cur_skb->data + abs_offset;
2008 return block_limit - abs_offset;
2011 if (st->frag_idx == 0 && !st->frag_data)
2012 st->stepped_offset += skb_headlen(st->cur_skb);
2014 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2015 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2016 block_limit = frag->size + st->stepped_offset;
2018 if (abs_offset < block_limit) {
2020 st->frag_data = kmap_skb_frag(frag);
2022 *data = (u8 *) st->frag_data + frag->page_offset +
2023 (abs_offset - st->stepped_offset);
2025 return block_limit - abs_offset;
2028 if (st->frag_data) {
2029 kunmap_skb_frag(st->frag_data);
2030 st->frag_data = NULL;
2034 st->stepped_offset += frag->size;
2037 if (st->frag_data) {
2038 kunmap_skb_frag(st->frag_data);
2039 st->frag_data = NULL;
2042 if (st->cur_skb->next) {
2043 st->cur_skb = st->cur_skb->next;
2046 } else if (st->root_skb == st->cur_skb &&
2047 skb_shinfo(st->root_skb)->frag_list) {
2048 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2056 * skb_abort_seq_read - Abort a sequential read of skb data
2057 * @st: state variable
2059 * Must be called if skb_seq_read() was not called until it
2062 void skb_abort_seq_read(struct skb_seq_state *st)
2065 kunmap_skb_frag(st->frag_data);
2068 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2070 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2071 struct ts_config *conf,
2072 struct ts_state *state)
2074 return skb_seq_read(offset, text, TS_SKB_CB(state));
2077 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2079 skb_abort_seq_read(TS_SKB_CB(state));
2083 * skb_find_text - Find a text pattern in skb data
2084 * @skb: the buffer to look in
2085 * @from: search offset
2087 * @config: textsearch configuration
2088 * @state: uninitialized textsearch state variable
2090 * Finds a pattern in the skb data according to the specified
2091 * textsearch configuration. Use textsearch_next() to retrieve
2092 * subsequent occurrences of the pattern. Returns the offset
2093 * to the first occurrence or UINT_MAX if no match was found.
2095 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2096 unsigned int to, struct ts_config *config,
2097 struct ts_state *state)
2101 config->get_next_block = skb_ts_get_next_block;
2102 config->finish = skb_ts_finish;
2104 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2106 ret = textsearch_find(config, state);
2107 return (ret <= to - from ? ret : UINT_MAX);
2111 * skb_append_datato_frags: - append the user data to a skb
2112 * @sk: sock structure
2113 * @skb: skb structure to be appened with user data.
2114 * @getfrag: call back function to be used for getting the user data
2115 * @from: pointer to user message iov
2116 * @length: length of the iov message
2118 * Description: This procedure append the user data in the fragment part
2119 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2121 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2122 int (*getfrag)(void *from, char *to, int offset,
2123 int len, int odd, struct sk_buff *skb),
2124 void *from, int length)
2127 skb_frag_t *frag = NULL;
2128 struct page *page = NULL;
2134 /* Return error if we don't have space for new frag */
2135 frg_cnt = skb_shinfo(skb)->nr_frags;
2136 if (frg_cnt >= MAX_SKB_FRAGS)
2139 /* allocate a new page for next frag */
2140 page = alloc_pages(sk->sk_allocation, 0);
2142 /* If alloc_page fails just return failure and caller will
2143 * free previous allocated pages by doing kfree_skb()
2148 /* initialize the next frag */
2149 sk->sk_sndmsg_page = page;
2150 sk->sk_sndmsg_off = 0;
2151 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2152 skb->truesize += PAGE_SIZE;
2153 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2155 /* get the new initialized frag */
2156 frg_cnt = skb_shinfo(skb)->nr_frags;
2157 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2159 /* copy the user data to page */
2160 left = PAGE_SIZE - frag->page_offset;
2161 copy = (length > left)? left : length;
2163 ret = getfrag(from, (page_address(frag->page) +
2164 frag->page_offset + frag->size),
2165 offset, copy, 0, skb);
2169 /* copy was successful so update the size parameters */
2170 sk->sk_sndmsg_off += copy;
2173 skb->data_len += copy;
2177 } while (length > 0);
2183 * skb_pull_rcsum - pull skb and update receive checksum
2184 * @skb: buffer to update
2185 * @len: length of data pulled
2187 * This function performs an skb_pull on the packet and updates
2188 * the CHECKSUM_COMPLETE checksum. It should be used on
2189 * receive path processing instead of skb_pull unless you know
2190 * that the checksum difference is zero (e.g., a valid IP header)
2191 * or you are setting ip_summed to CHECKSUM_NONE.
2193 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2195 BUG_ON(len > skb->len);
2197 BUG_ON(skb->len < skb->data_len);
2198 skb_postpull_rcsum(skb, skb->data, len);
2199 return skb->data += len;
2202 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2205 * skb_segment - Perform protocol segmentation on skb.
2206 * @skb: buffer to segment
2207 * @features: features for the output path (see dev->features)
2209 * This function performs segmentation on the given skb. It returns
2210 * a pointer to the first in a list of new skbs for the segments.
2211 * In case of error it returns ERR_PTR(err).
2213 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2215 struct sk_buff *segs = NULL;
2216 struct sk_buff *tail = NULL;
2217 unsigned int mss = skb_shinfo(skb)->gso_size;
2218 unsigned int doffset = skb->data - skb_mac_header(skb);
2219 unsigned int offset = doffset;
2220 unsigned int headroom;
2222 int sg = features & NETIF_F_SG;
2223 int nfrags = skb_shinfo(skb)->nr_frags;
2228 __skb_push(skb, doffset);
2229 headroom = skb_headroom(skb);
2230 pos = skb_headlen(skb);
2233 struct sk_buff *nskb;
2239 len = skb->len - offset;
2243 hsize = skb_headlen(skb) - offset;
2246 if (hsize > len || !sg)
2249 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
2250 if (unlikely(!nskb))
2259 nskb->dev = skb->dev;
2260 skb_copy_queue_mapping(nskb, skb);
2261 nskb->priority = skb->priority;
2262 nskb->protocol = skb->protocol;
2263 nskb->vlan_tci = skb->vlan_tci;
2264 nskb->dst = dst_clone(skb->dst);
2265 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
2266 nskb->pkt_type = skb->pkt_type;
2267 nskb->mac_len = skb->mac_len;
2269 skb_reserve(nskb, headroom);
2270 skb_reset_mac_header(nskb);
2271 skb_set_network_header(nskb, skb->mac_len);
2272 nskb->transport_header = (nskb->network_header +
2273 skb_network_header_len(skb));
2274 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2277 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2283 frag = skb_shinfo(nskb)->frags;
2286 nskb->ip_summed = CHECKSUM_PARTIAL;
2287 nskb->csum = skb->csum;
2288 skb_copy_from_linear_data_offset(skb, offset,
2289 skb_put(nskb, hsize), hsize);
2291 while (pos < offset + len) {
2292 BUG_ON(i >= nfrags);
2294 *frag = skb_shinfo(skb)->frags[i];
2295 get_page(frag->page);
2299 frag->page_offset += offset - pos;
2300 frag->size -= offset - pos;
2305 if (pos + size <= offset + len) {
2309 frag->size -= pos + size - (offset + len);
2316 skb_shinfo(nskb)->nr_frags = k;
2317 nskb->data_len = len - hsize;
2318 nskb->len += nskb->data_len;
2319 nskb->truesize += nskb->data_len;
2320 } while ((offset += len) < skb->len);
2325 while ((skb = segs)) {
2329 return ERR_PTR(err);
2332 EXPORT_SYMBOL_GPL(skb_segment);
2334 void __init skb_init(void)
2336 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2337 sizeof(struct sk_buff),
2339 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2341 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2342 (2*sizeof(struct sk_buff)) +
2345 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2350 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2351 * @skb: Socket buffer containing the buffers to be mapped
2352 * @sg: The scatter-gather list to map into
2353 * @offset: The offset into the buffer's contents to start mapping
2354 * @len: Length of buffer space to be mapped
2356 * Fill the specified scatter-gather list with mappings/pointers into a
2357 * region of the buffer space attached to a socket buffer.
2360 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2362 int start = skb_headlen(skb);
2363 int i, copy = start - offset;
2369 sg_set_buf(sg, skb->data + offset, copy);
2371 if ((len -= copy) == 0)
2376 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2379 WARN_ON(start > offset + len);
2381 end = start + skb_shinfo(skb)->frags[i].size;
2382 if ((copy = end - offset) > 0) {
2383 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2387 sg_set_page(&sg[elt], frag->page, copy,
2388 frag->page_offset+offset-start);
2397 if (skb_shinfo(skb)->frag_list) {
2398 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2400 for (; list; list = list->next) {
2403 WARN_ON(start > offset + len);
2405 end = start + list->len;
2406 if ((copy = end - offset) > 0) {
2409 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2411 if ((len -= copy) == 0)
2422 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2424 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2426 sg_mark_end(&sg[nsg - 1]);
2432 * skb_cow_data - Check that a socket buffer's data buffers are writable
2433 * @skb: The socket buffer to check.
2434 * @tailbits: Amount of trailing space to be added
2435 * @trailer: Returned pointer to the skb where the @tailbits space begins
2437 * Make sure that the data buffers attached to a socket buffer are
2438 * writable. If they are not, private copies are made of the data buffers
2439 * and the socket buffer is set to use these instead.
2441 * If @tailbits is given, make sure that there is space to write @tailbits
2442 * bytes of data beyond current end of socket buffer. @trailer will be
2443 * set to point to the skb in which this space begins.
2445 * The number of scatterlist elements required to completely map the
2446 * COW'd and extended socket buffer will be returned.
2448 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2452 struct sk_buff *skb1, **skb_p;
2454 /* If skb is cloned or its head is paged, reallocate
2455 * head pulling out all the pages (pages are considered not writable
2456 * at the moment even if they are anonymous).
2458 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2459 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2462 /* Easy case. Most of packets will go this way. */
2463 if (!skb_shinfo(skb)->frag_list) {
2464 /* A little of trouble, not enough of space for trailer.
2465 * This should not happen, when stack is tuned to generate
2466 * good frames. OK, on miss we reallocate and reserve even more
2467 * space, 128 bytes is fair. */
2469 if (skb_tailroom(skb) < tailbits &&
2470 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2478 /* Misery. We are in troubles, going to mincer fragments... */
2481 skb_p = &skb_shinfo(skb)->frag_list;
2484 while ((skb1 = *skb_p) != NULL) {
2487 /* The fragment is partially pulled by someone,
2488 * this can happen on input. Copy it and everything
2491 if (skb_shared(skb1))
2494 /* If the skb is the last, worry about trailer. */
2496 if (skb1->next == NULL && tailbits) {
2497 if (skb_shinfo(skb1)->nr_frags ||
2498 skb_shinfo(skb1)->frag_list ||
2499 skb_tailroom(skb1) < tailbits)
2500 ntail = tailbits + 128;
2506 skb_shinfo(skb1)->nr_frags ||
2507 skb_shinfo(skb1)->frag_list) {
2508 struct sk_buff *skb2;
2510 /* Fuck, we are miserable poor guys... */
2512 skb2 = skb_copy(skb1, GFP_ATOMIC);
2514 skb2 = skb_copy_expand(skb1,
2518 if (unlikely(skb2 == NULL))
2522 skb_set_owner_w(skb2, skb1->sk);
2524 /* Looking around. Are we still alive?
2525 * OK, link new skb, drop old one */
2527 skb2->next = skb1->next;
2534 skb_p = &skb1->next;
2541 * skb_partial_csum_set - set up and verify partial csum values for packet
2542 * @skb: the skb to set
2543 * @start: the number of bytes after skb->data to start checksumming.
2544 * @off: the offset from start to place the checksum.
2546 * For untrusted partially-checksummed packets, we need to make sure the values
2547 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2549 * This function checks and sets those values and skb->ip_summed: if this
2550 * returns false you should drop the packet.
2552 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2554 if (unlikely(start > skb->len - 2) ||
2555 unlikely((int)start + off > skb->len - 2)) {
2556 if (net_ratelimit())
2558 "bad partial csum: csum=%u/%u len=%u\n",
2559 start, off, skb->len);
2562 skb->ip_summed = CHECKSUM_PARTIAL;
2563 skb->csum_start = skb_headroom(skb) + start;
2564 skb->csum_offset = off;
2568 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2570 if (net_ratelimit())
2571 pr_warning("%s: received packets cannot be forwarded"
2572 " while LRO is enabled\n", skb->dev->name);
2575 EXPORT_SYMBOL(___pskb_trim);
2576 EXPORT_SYMBOL(__kfree_skb);
2577 EXPORT_SYMBOL(kfree_skb);
2578 EXPORT_SYMBOL(__pskb_pull_tail);
2579 EXPORT_SYMBOL(__alloc_skb);
2580 EXPORT_SYMBOL(__netdev_alloc_skb);
2581 EXPORT_SYMBOL(pskb_copy);
2582 EXPORT_SYMBOL(pskb_expand_head);
2583 EXPORT_SYMBOL(skb_checksum);
2584 EXPORT_SYMBOL(skb_clone);
2585 EXPORT_SYMBOL(skb_copy);
2586 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2587 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2588 EXPORT_SYMBOL(skb_copy_bits);
2589 EXPORT_SYMBOL(skb_copy_expand);
2590 EXPORT_SYMBOL(skb_over_panic);
2591 EXPORT_SYMBOL(skb_pad);
2592 EXPORT_SYMBOL(skb_realloc_headroom);
2593 EXPORT_SYMBOL(skb_under_panic);
2594 EXPORT_SYMBOL(skb_dequeue);
2595 EXPORT_SYMBOL(skb_dequeue_tail);
2596 EXPORT_SYMBOL(skb_insert);
2597 EXPORT_SYMBOL(skb_queue_purge);
2598 EXPORT_SYMBOL(skb_queue_head);
2599 EXPORT_SYMBOL(skb_queue_tail);
2600 EXPORT_SYMBOL(skb_unlink);
2601 EXPORT_SYMBOL(skb_append);
2602 EXPORT_SYMBOL(skb_split);
2603 EXPORT_SYMBOL(skb_prepare_seq_read);
2604 EXPORT_SYMBOL(skb_seq_read);
2605 EXPORT_SYMBOL(skb_abort_seq_read);
2606 EXPORT_SYMBOL(skb_find_text);
2607 EXPORT_SYMBOL(skb_append_datato_frags);
2608 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
2610 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2611 EXPORT_SYMBOL_GPL(skb_cow_data);
2612 EXPORT_SYMBOL_GPL(skb_partial_csum_set);