2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.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 #endif /* DDE_LINUX */
69 #include <asm/uaccess.h>
70 #include <asm/system.h>
74 static struct kmem_cache *skbuff_head_cache __read_mostly;
75 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
77 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
78 struct pipe_buffer *buf)
83 static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
84 struct pipe_buffer *buf)
89 static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
90 struct pipe_buffer *buf)
97 /* Pipe buffer operations for a socket. */
98 static struct pipe_buf_operations sock_pipe_buf_ops = {
101 .map = generic_pipe_buf_map,
102 .unmap = generic_pipe_buf_unmap,
103 .confirm = generic_pipe_buf_confirm,
104 .release = sock_pipe_buf_release,
105 .steal = sock_pipe_buf_steal,
106 .get = sock_pipe_buf_get,
112 * Keep out-of-line to prevent kernel bloat.
113 * __builtin_return_address is not used because it is not always
118 * skb_over_panic - private function
123 * Out of line support code for skb_put(). Not user callable.
125 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
127 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
128 "data:%p tail:%#lx end:%#lx dev:%s\n",
129 here, skb->len, sz, skb->head, skb->data,
130 (unsigned long)skb->tail, (unsigned long)skb->end,
131 skb->dev ? skb->dev->name : "<NULL>");
136 * skb_under_panic - private function
141 * Out of line support code for skb_push(). Not user callable.
144 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
146 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
147 "data:%p tail:%#lx end:%#lx dev:%s\n",
148 here, skb->len, sz, skb->head, skb->data,
149 (unsigned long)skb->tail, (unsigned long)skb->end,
150 skb->dev ? skb->dev->name : "<NULL>");
154 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
155 * 'private' fields and also do memory statistics to find all the
161 * __alloc_skb - allocate a network buffer
162 * @size: size to allocate
163 * @gfp_mask: allocation mask
164 * @fclone: allocate from fclone cache instead of head cache
165 * and allocate a cloned (child) skb
166 * @node: numa node to allocate memory on
168 * Allocate a new &sk_buff. The returned buffer has no headroom and a
169 * tail room of size bytes. The object has a reference count of one.
170 * The return is the buffer. On a failure the return is %NULL.
172 * Buffers may only be allocated from interrupts using a @gfp_mask of
175 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
176 int fclone, int node)
178 struct kmem_cache *cache;
179 struct skb_shared_info *shinfo;
183 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
186 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
190 size = SKB_DATA_ALIGN(size);
191 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
197 * Only clear those fields we need to clear, not those that we will
198 * actually initialise below. Hence, don't put any more fields after
199 * the tail pointer in struct sk_buff!
201 memset(skb, 0, offsetof(struct sk_buff, tail));
202 skb->truesize = size + sizeof(struct sk_buff);
203 atomic_set(&skb->users, 1);
206 skb_reset_tail_pointer(skb);
207 skb->end = skb->tail + size;
208 /* make sure we initialize shinfo sequentially */
209 shinfo = skb_shinfo(skb);
210 atomic_set(&shinfo->dataref, 1);
211 shinfo->nr_frags = 0;
212 shinfo->gso_size = 0;
213 shinfo->gso_segs = 0;
214 shinfo->gso_type = 0;
215 shinfo->ip6_frag_id = 0;
216 shinfo->frag_list = NULL;
219 struct sk_buff *child = skb + 1;
220 atomic_t *fclone_ref = (atomic_t *) (child + 1);
222 skb->fclone = SKB_FCLONE_ORIG;
223 atomic_set(fclone_ref, 1);
225 child->fclone = SKB_FCLONE_UNAVAILABLE;
230 kmem_cache_free(cache, skb);
236 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
237 * @dev: network device to receive on
238 * @length: length to allocate
239 * @gfp_mask: get_free_pages mask, passed to alloc_skb
241 * Allocate a new &sk_buff and assign it a usage count of one. The
242 * buffer has unspecified headroom built in. Users should allocate
243 * the headroom they think they need without accounting for the
244 * built in space. The built in space is used for optimisations.
246 * %NULL is returned if there is no free memory.
248 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
249 unsigned int length, gfp_t gfp_mask)
251 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
254 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
256 skb_reserve(skb, NET_SKB_PAD);
262 struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
264 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
267 page = alloc_pages_node(node, gfp_mask, 0);
270 EXPORT_SYMBOL(__netdev_alloc_page);
272 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
275 skb_fill_page_desc(skb, i, page, off, size);
277 skb->data_len += size;
278 skb->truesize += size;
280 EXPORT_SYMBOL(skb_add_rx_frag);
283 * dev_alloc_skb - allocate an skbuff for receiving
284 * @length: length to allocate
286 * Allocate a new &sk_buff and assign it a usage count of one. The
287 * buffer has unspecified headroom built in. Users should allocate
288 * the headroom they think they need without accounting for the
289 * built in space. The built in space is used for optimisations.
291 * %NULL is returned if there is no free memory. Although this function
292 * allocates memory it can be called from an interrupt.
294 struct sk_buff *dev_alloc_skb(unsigned int length)
297 * There is more code here than it seems:
298 * __dev_alloc_skb is an inline
300 return __dev_alloc_skb(length, GFP_ATOMIC);
302 EXPORT_SYMBOL(dev_alloc_skb);
304 static void skb_drop_list(struct sk_buff **listp)
306 struct sk_buff *list = *listp;
311 struct sk_buff *this = list;
317 static inline void skb_drop_fraglist(struct sk_buff *skb)
319 skb_drop_list(&skb_shinfo(skb)->frag_list);
322 static void skb_clone_fraglist(struct sk_buff *skb)
324 struct sk_buff *list;
326 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
330 static void skb_release_data(struct sk_buff *skb)
333 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
334 &skb_shinfo(skb)->dataref)) {
335 if (skb_shinfo(skb)->nr_frags) {
337 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
338 put_page(skb_shinfo(skb)->frags[i].page);
341 if (skb_shinfo(skb)->frag_list)
342 skb_drop_fraglist(skb);
349 * Free an skbuff by memory without cleaning the state.
351 static void kfree_skbmem(struct sk_buff *skb)
353 struct sk_buff *other;
354 atomic_t *fclone_ref;
356 switch (skb->fclone) {
357 case SKB_FCLONE_UNAVAILABLE:
358 kmem_cache_free(skbuff_head_cache, skb);
361 case SKB_FCLONE_ORIG:
362 fclone_ref = (atomic_t *) (skb + 2);
363 if (atomic_dec_and_test(fclone_ref))
364 kmem_cache_free(skbuff_fclone_cache, skb);
367 case SKB_FCLONE_CLONE:
368 fclone_ref = (atomic_t *) (skb + 1);
371 /* The clone portion is available for
372 * fast-cloning again.
374 skb->fclone = SKB_FCLONE_UNAVAILABLE;
376 if (atomic_dec_and_test(fclone_ref))
377 kmem_cache_free(skbuff_fclone_cache, other);
382 static void skb_release_head_state(struct sk_buff *skb)
385 dst_release(skb->dst);
388 secpath_put(skb->sp);
390 if (skb->destructor) {
392 skb->destructor(skb);
394 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
395 nf_conntrack_put(skb->nfct);
396 nf_conntrack_put_reasm(skb->nfct_reasm);
398 #ifdef CONFIG_BRIDGE_NETFILTER
399 nf_bridge_put(skb->nf_bridge);
401 /* XXX: IS this still necessary? - JHS */
402 #ifdef CONFIG_NET_SCHED
404 #ifdef CONFIG_NET_CLS_ACT
410 /* Free everything but the sk_buff shell. */
411 static void skb_release_all(struct sk_buff *skb)
413 skb_release_head_state(skb);
414 skb_release_data(skb);
418 * __kfree_skb - private function
421 * Free an sk_buff. Release anything attached to the buffer.
422 * Clean the state. This is an internal helper function. Users should
423 * always call kfree_skb
426 void __kfree_skb(struct sk_buff *skb)
428 skb_release_all(skb);
433 * kfree_skb - free an sk_buff
434 * @skb: buffer to free
436 * Drop a reference to the buffer and free it if the usage count has
439 void kfree_skb(struct sk_buff *skb)
443 if (likely(atomic_read(&skb->users) == 1))
445 else if (likely(!atomic_dec_and_test(&skb->users)))
451 * skb_recycle_check - check if skb can be reused for receive
453 * @skb_size: minimum receive buffer size
455 * Checks that the skb passed in is not shared or cloned, and
456 * that it is linear and its head portion at least as large as
457 * skb_size so that it can be recycled as a receive buffer.
458 * If these conditions are met, this function does any necessary
459 * reference count dropping and cleans up the skbuff as if it
460 * just came from __alloc_skb().
462 int skb_recycle_check(struct sk_buff *skb, int skb_size)
464 struct skb_shared_info *shinfo;
466 if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
469 skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
470 if (skb_end_pointer(skb) - skb->head < skb_size)
473 if (skb_shared(skb) || skb_cloned(skb))
476 skb_release_head_state(skb);
477 shinfo = skb_shinfo(skb);
478 atomic_set(&shinfo->dataref, 1);
479 shinfo->nr_frags = 0;
480 shinfo->gso_size = 0;
481 shinfo->gso_segs = 0;
482 shinfo->gso_type = 0;
483 shinfo->ip6_frag_id = 0;
484 shinfo->frag_list = NULL;
486 memset(skb, 0, offsetof(struct sk_buff, tail));
487 skb->data = skb->head + NET_SKB_PAD;
488 skb_reset_tail_pointer(skb);
492 EXPORT_SYMBOL(skb_recycle_check);
494 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
496 new->tstamp = old->tstamp;
498 new->transport_header = old->transport_header;
499 new->network_header = old->network_header;
500 new->mac_header = old->mac_header;
501 new->dst = dst_clone(old->dst);
503 new->sp = secpath_get(old->sp);
505 memcpy(new->cb, old->cb, sizeof(old->cb));
506 new->csum_start = old->csum_start;
507 new->csum_offset = old->csum_offset;
508 new->local_df = old->local_df;
509 new->pkt_type = old->pkt_type;
510 new->ip_summed = old->ip_summed;
511 skb_copy_queue_mapping(new, old);
512 new->priority = old->priority;
513 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
514 new->ipvs_property = old->ipvs_property;
516 new->protocol = old->protocol;
517 new->mark = old->mark;
519 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
520 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
521 new->nf_trace = old->nf_trace;
523 #ifdef CONFIG_NET_SCHED
524 new->tc_index = old->tc_index;
525 #ifdef CONFIG_NET_CLS_ACT
526 new->tc_verd = old->tc_verd;
529 new->vlan_tci = old->vlan_tci;
531 skb_copy_secmark(new, old);
534 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
536 #define C(x) n->x = skb->x
538 n->next = n->prev = NULL;
540 __copy_skb_header(n, skb);
545 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
548 n->destructor = NULL;
555 #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
559 atomic_set(&n->users, 1);
561 atomic_inc(&(skb_shinfo(skb)->dataref));
569 * skb_morph - morph one skb into another
570 * @dst: the skb to receive the contents
571 * @src: the skb to supply the contents
573 * This is identical to skb_clone except that the target skb is
574 * supplied by the user.
576 * The target skb is returned upon exit.
578 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
580 skb_release_all(dst);
581 return __skb_clone(dst, src);
583 EXPORT_SYMBOL_GPL(skb_morph);
586 * skb_clone - duplicate an sk_buff
587 * @skb: buffer to clone
588 * @gfp_mask: allocation priority
590 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
591 * copies share the same packet data but not structure. The new
592 * buffer has a reference count of 1. If the allocation fails the
593 * function returns %NULL otherwise the new buffer is returned.
595 * If this function is called from an interrupt gfp_mask() must be
599 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
604 if (skb->fclone == SKB_FCLONE_ORIG &&
605 n->fclone == SKB_FCLONE_UNAVAILABLE) {
606 atomic_t *fclone_ref = (atomic_t *) (n + 1);
607 n->fclone = SKB_FCLONE_CLONE;
608 atomic_inc(fclone_ref);
610 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
613 n->fclone = SKB_FCLONE_UNAVAILABLE;
616 return __skb_clone(n, skb);
619 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
621 #ifndef NET_SKBUFF_DATA_USES_OFFSET
623 * Shift between the two data areas in bytes
625 unsigned long offset = new->data - old->data;
628 __copy_skb_header(new, old);
630 #ifndef NET_SKBUFF_DATA_USES_OFFSET
631 /* {transport,network,mac}_header are relative to skb->head */
632 new->transport_header += offset;
633 new->network_header += offset;
634 new->mac_header += offset;
636 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
637 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
638 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
642 * skb_copy - create private copy of an sk_buff
643 * @skb: buffer to copy
644 * @gfp_mask: allocation priority
646 * Make a copy of both an &sk_buff and its data. This is used when the
647 * caller wishes to modify the data and needs a private copy of the
648 * data to alter. Returns %NULL on failure or the pointer to the buffer
649 * on success. The returned buffer has a reference count of 1.
651 * As by-product this function converts non-linear &sk_buff to linear
652 * one, so that &sk_buff becomes completely private and caller is allowed
653 * to modify all the data of returned buffer. This means that this
654 * function is not recommended for use in circumstances when only
655 * header is going to be modified. Use pskb_copy() instead.
658 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
660 int headerlen = skb->data - skb->head;
662 * Allocate the copy buffer
665 #ifdef NET_SKBUFF_DATA_USES_OFFSET
666 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
668 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
673 /* Set the data pointer */
674 skb_reserve(n, headerlen);
675 /* Set the tail pointer and length */
676 skb_put(n, skb->len);
678 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
681 copy_skb_header(n, skb);
687 * pskb_copy - create copy of an sk_buff with private head.
688 * @skb: buffer to copy
689 * @gfp_mask: allocation priority
691 * Make a copy of both an &sk_buff and part of its data, located
692 * in header. Fragmented data remain shared. This is used when
693 * the caller wishes to modify only header of &sk_buff and needs
694 * private copy of the header to alter. Returns %NULL on failure
695 * or the pointer to the buffer on success.
696 * The returned buffer has a reference count of 1.
699 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
702 * Allocate the copy buffer
705 #ifdef NET_SKBUFF_DATA_USES_OFFSET
706 n = alloc_skb(skb->end, gfp_mask);
708 n = alloc_skb(skb->end - skb->head, gfp_mask);
713 /* Set the data pointer */
714 skb_reserve(n, skb->data - skb->head);
715 /* Set the tail pointer and length */
716 skb_put(n, skb_headlen(skb));
718 skb_copy_from_linear_data(skb, n->data, n->len);
720 n->truesize += skb->data_len;
721 n->data_len = skb->data_len;
724 if (skb_shinfo(skb)->nr_frags) {
727 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
728 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
729 get_page(skb_shinfo(n)->frags[i].page);
731 skb_shinfo(n)->nr_frags = i;
734 if (skb_shinfo(skb)->frag_list) {
735 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
736 skb_clone_fraglist(n);
739 copy_skb_header(n, skb);
745 * pskb_expand_head - reallocate header of &sk_buff
746 * @skb: buffer to reallocate
747 * @nhead: room to add at head
748 * @ntail: room to add at tail
749 * @gfp_mask: allocation priority
751 * Expands (or creates identical copy, if &nhead and &ntail are zero)
752 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
753 * reference count of 1. Returns zero in the case of success or error,
754 * if expansion failed. In the last case, &sk_buff is not changed.
756 * All the pointers pointing into skb header may change and must be
757 * reloaded after call to this function.
760 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
765 #ifdef NET_SKBUFF_DATA_USES_OFFSET
766 int size = nhead + skb->end + ntail;
768 int size = nhead + (skb->end - skb->head) + ntail;
777 size = SKB_DATA_ALIGN(size);
779 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
783 /* Copy only real data... and, alas, header. This should be
784 * optimized for the cases when header is void. */
785 #ifdef NET_SKBUFF_DATA_USES_OFFSET
786 memcpy(data + nhead, skb->head, skb->tail);
788 memcpy(data + nhead, skb->head, skb->tail - skb->head);
790 memcpy(data + size, skb_end_pointer(skb),
791 sizeof(struct skb_shared_info));
793 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
794 get_page(skb_shinfo(skb)->frags[i].page);
796 if (skb_shinfo(skb)->frag_list)
797 skb_clone_fraglist(skb);
799 skb_release_data(skb);
801 off = (data + nhead) - skb->head;
805 #ifdef NET_SKBUFF_DATA_USES_OFFSET
809 skb->end = skb->head + size;
811 /* {transport,network,mac}_header and tail are relative to skb->head */
813 skb->transport_header += off;
814 skb->network_header += off;
815 skb->mac_header += off;
816 skb->csum_start += nhead;
820 atomic_set(&skb_shinfo(skb)->dataref, 1);
827 /* Make private copy of skb with writable head and some headroom */
829 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
831 struct sk_buff *skb2;
832 int delta = headroom - skb_headroom(skb);
835 skb2 = pskb_copy(skb, GFP_ATOMIC);
837 skb2 = skb_clone(skb, GFP_ATOMIC);
838 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
849 * skb_copy_expand - copy and expand sk_buff
850 * @skb: buffer to copy
851 * @newheadroom: new free bytes at head
852 * @newtailroom: new free bytes at tail
853 * @gfp_mask: allocation priority
855 * Make a copy of both an &sk_buff and its data and while doing so
856 * allocate additional space.
858 * This is used when the caller wishes to modify the data and needs a
859 * private copy of the data to alter as well as more space for new fields.
860 * Returns %NULL on failure or the pointer to the buffer
861 * on success. The returned buffer has a reference count of 1.
863 * You must pass %GFP_ATOMIC as the allocation priority if this function
864 * is called from an interrupt.
866 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
867 int newheadroom, int newtailroom,
871 * Allocate the copy buffer
873 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
875 int oldheadroom = skb_headroom(skb);
876 int head_copy_len, head_copy_off;
882 skb_reserve(n, newheadroom);
884 /* Set the tail pointer and length */
885 skb_put(n, skb->len);
887 head_copy_len = oldheadroom;
889 if (newheadroom <= head_copy_len)
890 head_copy_len = newheadroom;
892 head_copy_off = newheadroom - head_copy_len;
894 /* Copy the linear header and data. */
895 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
896 skb->len + head_copy_len))
899 copy_skb_header(n, skb);
901 off = newheadroom - oldheadroom;
902 n->csum_start += off;
903 #ifdef NET_SKBUFF_DATA_USES_OFFSET
904 n->transport_header += off;
905 n->network_header += off;
906 n->mac_header += off;
913 * skb_pad - zero pad the tail of an skb
914 * @skb: buffer to pad
917 * Ensure that a buffer is followed by a padding area that is zero
918 * filled. Used by network drivers which may DMA or transfer data
919 * beyond the buffer end onto the wire.
921 * May return error in out of memory cases. The skb is freed on error.
924 int skb_pad(struct sk_buff *skb, int pad)
929 /* If the skbuff is non linear tailroom is always zero.. */
930 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
931 memset(skb->data+skb->len, 0, pad);
935 ntail = skb->data_len + pad - (skb->end - skb->tail);
936 if (likely(skb_cloned(skb) || ntail > 0)) {
937 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
942 /* FIXME: The use of this function with non-linear skb's really needs
945 err = skb_linearize(skb);
949 memset(skb->data + skb->len, 0, pad);
958 * skb_put - add data to a buffer
959 * @skb: buffer to use
960 * @len: amount of data to add
962 * This function extends the used data area of the buffer. If this would
963 * exceed the total buffer size the kernel will panic. A pointer to the
964 * first byte of the extra data is returned.
966 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
968 unsigned char *tmp = skb_tail_pointer(skb);
969 SKB_LINEAR_ASSERT(skb);
972 if (unlikely(skb->tail > skb->end))
973 skb_over_panic(skb, len, __builtin_return_address(0));
976 EXPORT_SYMBOL(skb_put);
979 * skb_push - add data to the start of a buffer
980 * @skb: buffer to use
981 * @len: amount of data to add
983 * This function extends the used data area of the buffer at the buffer
984 * start. If this would exceed the total buffer headroom the kernel will
985 * panic. A pointer to the first byte of the extra data is returned.
987 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
991 if (unlikely(skb->data<skb->head))
992 skb_under_panic(skb, len, __builtin_return_address(0));
995 EXPORT_SYMBOL(skb_push);
998 * skb_pull - remove data from the start of a buffer
999 * @skb: buffer to use
1000 * @len: amount of data to remove
1002 * This function removes data from the start of a buffer, returning
1003 * the memory to the headroom. A pointer to the next data in the buffer
1004 * is returned. Once the data has been pulled future pushes will overwrite
1007 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1009 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
1011 EXPORT_SYMBOL(skb_pull);
1014 * skb_trim - remove end from a buffer
1015 * @skb: buffer to alter
1018 * Cut the length of a buffer down by removing data from the tail. If
1019 * the buffer is already under the length specified it is not modified.
1020 * The skb must be linear.
1022 void skb_trim(struct sk_buff *skb, unsigned int len)
1025 __skb_trim(skb, len);
1027 EXPORT_SYMBOL(skb_trim);
1029 /* Trims skb to length len. It can change skb pointers.
1032 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1034 struct sk_buff **fragp;
1035 struct sk_buff *frag;
1036 int offset = skb_headlen(skb);
1037 int nfrags = skb_shinfo(skb)->nr_frags;
1041 if (skb_cloned(skb) &&
1042 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1049 for (; i < nfrags; i++) {
1050 int end = offset + skb_shinfo(skb)->frags[i].size;
1057 skb_shinfo(skb)->frags[i++].size = len - offset;
1060 skb_shinfo(skb)->nr_frags = i;
1062 for (; i < nfrags; i++)
1063 put_page(skb_shinfo(skb)->frags[i].page);
1065 if (skb_shinfo(skb)->frag_list)
1066 skb_drop_fraglist(skb);
1070 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1071 fragp = &frag->next) {
1072 int end = offset + frag->len;
1074 if (skb_shared(frag)) {
1075 struct sk_buff *nfrag;
1077 nfrag = skb_clone(frag, GFP_ATOMIC);
1078 if (unlikely(!nfrag))
1081 nfrag->next = frag->next;
1093 unlikely((err = pskb_trim(frag, len - offset))))
1097 skb_drop_list(&frag->next);
1102 if (len > skb_headlen(skb)) {
1103 skb->data_len -= skb->len - len;
1108 skb_set_tail_pointer(skb, len);
1115 * __pskb_pull_tail - advance tail of skb header
1116 * @skb: buffer to reallocate
1117 * @delta: number of bytes to advance tail
1119 * The function makes a sense only on a fragmented &sk_buff,
1120 * it expands header moving its tail forward and copying necessary
1121 * data from fragmented part.
1123 * &sk_buff MUST have reference count of 1.
1125 * Returns %NULL (and &sk_buff does not change) if pull failed
1126 * or value of new tail of skb in the case of success.
1128 * All the pointers pointing into skb header may change and must be
1129 * reloaded after call to this function.
1132 /* Moves tail of skb head forward, copying data from fragmented part,
1133 * when it is necessary.
1134 * 1. It may fail due to malloc failure.
1135 * 2. It may change skb pointers.
1137 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1139 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1141 /* If skb has not enough free space at tail, get new one
1142 * plus 128 bytes for future expansions. If we have enough
1143 * room at tail, reallocate without expansion only if skb is cloned.
1145 int i, k, eat = (skb->tail + delta) - skb->end;
1147 if (eat > 0 || skb_cloned(skb)) {
1148 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1153 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1156 /* Optimization: no fragments, no reasons to preestimate
1157 * size of pulled pages. Superb.
1159 if (!skb_shinfo(skb)->frag_list)
1162 /* Estimate size of pulled pages. */
1164 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1165 if (skb_shinfo(skb)->frags[i].size >= eat)
1167 eat -= skb_shinfo(skb)->frags[i].size;
1170 /* If we need update frag list, we are in troubles.
1171 * Certainly, it possible to add an offset to skb data,
1172 * but taking into account that pulling is expected to
1173 * be very rare operation, it is worth to fight against
1174 * further bloating skb head and crucify ourselves here instead.
1175 * Pure masohism, indeed. 8)8)
1178 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1179 struct sk_buff *clone = NULL;
1180 struct sk_buff *insp = NULL;
1185 if (list->len <= eat) {
1186 /* Eaten as whole. */
1191 /* Eaten partially. */
1193 if (skb_shared(list)) {
1194 /* Sucks! We need to fork list. :-( */
1195 clone = skb_clone(list, GFP_ATOMIC);
1201 /* This may be pulled without
1205 if (!pskb_pull(list, eat)) {
1214 /* Free pulled out fragments. */
1215 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1216 skb_shinfo(skb)->frag_list = list->next;
1219 /* And insert new clone at head. */
1222 skb_shinfo(skb)->frag_list = clone;
1225 /* Success! Now we may commit changes to skb data. */
1230 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1231 if (skb_shinfo(skb)->frags[i].size <= eat) {
1232 put_page(skb_shinfo(skb)->frags[i].page);
1233 eat -= skb_shinfo(skb)->frags[i].size;
1235 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1237 skb_shinfo(skb)->frags[k].page_offset += eat;
1238 skb_shinfo(skb)->frags[k].size -= eat;
1244 skb_shinfo(skb)->nr_frags = k;
1247 skb->data_len -= delta;
1249 return skb_tail_pointer(skb);
1252 /* Copy some data bits from skb to kernel buffer. */
1254 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1257 int start = skb_headlen(skb);
1259 if (offset > (int)skb->len - len)
1263 if ((copy = start - offset) > 0) {
1266 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1267 if ((len -= copy) == 0)
1273 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1276 WARN_ON(start > offset + len);
1278 end = start + skb_shinfo(skb)->frags[i].size;
1279 if ((copy = end - offset) > 0) {
1285 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1287 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1288 offset - start, copy);
1289 kunmap_skb_frag(vaddr);
1291 if ((len -= copy) == 0)
1299 if (skb_shinfo(skb)->frag_list) {
1300 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1302 for (; list; list = list->next) {
1305 WARN_ON(start > offset + len);
1307 end = start + list->len;
1308 if ((copy = end - offset) > 0) {
1311 if (skb_copy_bits(list, offset - start,
1314 if ((len -= copy) == 0)
1330 * Callback from splice_to_pipe(), if we need to release some pages
1331 * at the end of the spd in case we error'ed out in filling the pipe.
1333 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1335 put_page(spd->pages[i]);
1338 static inline struct page *linear_to_page(struct page *page, unsigned int len,
1339 unsigned int offset)
1341 struct page *p = alloc_pages(GFP_KERNEL, 0);
1345 memcpy(page_address(p) + offset, page_address(page) + offset, len);
1351 * Fill page/offset/length into spd, if it can hold more pages.
1353 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1354 unsigned int len, unsigned int offset,
1355 struct sk_buff *skb, int linear)
1357 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1361 page = linear_to_page(page, len, offset);
1367 spd->pages[spd->nr_pages] = page;
1368 spd->partial[spd->nr_pages].len = len;
1369 spd->partial[spd->nr_pages].offset = offset;
1375 static inline void __segment_seek(struct page **page, unsigned int *poff,
1376 unsigned int *plen, unsigned int off)
1379 *page += *poff / PAGE_SIZE;
1380 *poff = *poff % PAGE_SIZE;
1384 static inline int __splice_segment(struct page *page, unsigned int poff,
1385 unsigned int plen, unsigned int *off,
1386 unsigned int *len, struct sk_buff *skb,
1387 struct splice_pipe_desc *spd, int linear)
1392 /* skip this segment if already processed */
1398 /* ignore any bits we already processed */
1400 __segment_seek(&page, &poff, &plen, *off);
1405 unsigned int flen = min(*len, plen);
1407 /* the linear region may spread across several pages */
1408 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1410 if (spd_fill_page(spd, page, flen, poff, skb, linear))
1413 __segment_seek(&page, &poff, &plen, flen);
1416 } while (*len && plen);
1422 * Map linear and fragment data from the skb to spd. It reports failure if the
1423 * pipe is full or if we already spliced the requested length.
1425 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1427 struct splice_pipe_desc *spd)
1432 * map the linear part
1434 if (__splice_segment(virt_to_page(skb->data),
1435 (unsigned long) skb->data & (PAGE_SIZE - 1),
1437 offset, len, skb, spd, 1))
1441 * then map the fragments
1443 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1444 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1446 if (__splice_segment(f->page, f->page_offset, f->size,
1447 offset, len, skb, spd, 0))
1455 * Map data from the skb to a pipe. Should handle both the linear part,
1456 * the fragments, and the frag list. It does NOT handle frag lists within
1457 * the frag list, if such a thing exists. We'd probably need to recurse to
1458 * handle that cleanly.
1460 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1461 struct pipe_inode_info *pipe, unsigned int tlen,
1464 struct partial_page partial[PIPE_BUFFERS];
1465 struct page *pages[PIPE_BUFFERS];
1466 struct splice_pipe_desc spd = {
1471 .ops = &sock_pipe_buf_ops,
1473 .spd_release = sock_spd_release,
1477 * __skb_splice_bits() only fails if the output has no room left,
1478 * so no point in going over the frag_list for the error case.
1480 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1486 * now see if we have a frag_list to map
1488 if (skb_shinfo(skb)->frag_list) {
1489 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1491 for (; list && tlen; list = list->next) {
1492 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1499 struct sock *sk = skb->sk;
1503 * Drop the socket lock, otherwise we have reverse
1504 * locking dependencies between sk_lock and i_mutex
1505 * here as compared to sendfile(). We enter here
1506 * with the socket lock held, and splice_to_pipe() will
1507 * grab the pipe inode lock. For sendfile() emulation,
1508 * we call into ->sendpage() with the i_mutex lock held
1509 * and networking will grab the socket lock.
1512 ret = splice_to_pipe(pipe, &spd);
1521 * skb_store_bits - store bits from kernel buffer to skb
1522 * @skb: destination buffer
1523 * @offset: offset in destination
1524 * @from: source buffer
1525 * @len: number of bytes to copy
1527 * Copy the specified number of bytes from the source buffer to the
1528 * destination skb. This function handles all the messy bits of
1529 * traversing fragment lists and such.
1532 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1535 int start = skb_headlen(skb);
1537 if (offset > (int)skb->len - len)
1540 if ((copy = start - offset) > 0) {
1543 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1544 if ((len -= copy) == 0)
1550 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1551 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1554 WARN_ON(start > offset + len);
1556 end = start + frag->size;
1557 if ((copy = end - offset) > 0) {
1563 vaddr = kmap_skb_frag(frag);
1564 memcpy(vaddr + frag->page_offset + offset - start,
1566 kunmap_skb_frag(vaddr);
1568 if ((len -= copy) == 0)
1576 if (skb_shinfo(skb)->frag_list) {
1577 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1579 for (; list; list = list->next) {
1582 WARN_ON(start > offset + len);
1584 end = start + list->len;
1585 if ((copy = end - offset) > 0) {
1588 if (skb_store_bits(list, offset - start,
1591 if ((len -= copy) == 0)
1606 EXPORT_SYMBOL(skb_store_bits);
1608 /* Checksum skb data. */
1610 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1611 int len, __wsum csum)
1613 int start = skb_headlen(skb);
1614 int i, copy = start - offset;
1617 /* Checksum header. */
1621 csum = csum_partial(skb->data + offset, copy, csum);
1622 if ((len -= copy) == 0)
1628 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1631 WARN_ON(start > offset + len);
1633 end = start + skb_shinfo(skb)->frags[i].size;
1634 if ((copy = end - offset) > 0) {
1637 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1641 vaddr = kmap_skb_frag(frag);
1642 csum2 = csum_partial(vaddr + frag->page_offset +
1643 offset - start, copy, 0);
1644 kunmap_skb_frag(vaddr);
1645 csum = csum_block_add(csum, csum2, pos);
1654 if (skb_shinfo(skb)->frag_list) {
1655 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1657 for (; list; list = list->next) {
1660 WARN_ON(start > offset + len);
1662 end = start + list->len;
1663 if ((copy = end - offset) > 0) {
1667 csum2 = skb_checksum(list, offset - start,
1669 csum = csum_block_add(csum, csum2, pos);
1670 if ((len -= copy) == 0)
1683 /* Both of above in one bottle. */
1685 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1686 u8 *to, int len, __wsum csum)
1688 int start = skb_headlen(skb);
1689 int i, copy = start - offset;
1696 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1698 if ((len -= copy) == 0)
1705 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1708 WARN_ON(start > offset + len);
1710 end = start + skb_shinfo(skb)->frags[i].size;
1711 if ((copy = end - offset) > 0) {
1714 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1718 vaddr = kmap_skb_frag(frag);
1719 csum2 = csum_partial_copy_nocheck(vaddr +
1723 kunmap_skb_frag(vaddr);
1724 csum = csum_block_add(csum, csum2, pos);
1734 if (skb_shinfo(skb)->frag_list) {
1735 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1737 for (; list; list = list->next) {
1741 WARN_ON(start > offset + len);
1743 end = start + list->len;
1744 if ((copy = end - offset) > 0) {
1747 csum2 = skb_copy_and_csum_bits(list,
1750 csum = csum_block_add(csum, csum2, pos);
1751 if ((len -= copy) == 0)
1764 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1769 if (skb->ip_summed == CHECKSUM_PARTIAL)
1770 csstart = skb->csum_start - skb_headroom(skb);
1772 csstart = skb_headlen(skb);
1774 BUG_ON(csstart > skb_headlen(skb));
1776 skb_copy_from_linear_data(skb, to, csstart);
1779 if (csstart != skb->len)
1780 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1781 skb->len - csstart, 0);
1783 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1784 long csstuff = csstart + skb->csum_offset;
1786 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1791 * skb_dequeue - remove from the head of the queue
1792 * @list: list to dequeue from
1794 * Remove the head of the list. The list lock is taken so the function
1795 * may be used safely with other locking list functions. The head item is
1796 * returned or %NULL if the list is empty.
1799 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1801 unsigned long flags;
1802 struct sk_buff *result;
1804 spin_lock_irqsave(&list->lock, flags);
1805 result = __skb_dequeue(list);
1806 spin_unlock_irqrestore(&list->lock, flags);
1811 * skb_dequeue_tail - remove from the tail of the queue
1812 * @list: list to dequeue from
1814 * Remove the tail of the list. The list lock is taken so the function
1815 * may be used safely with other locking list functions. The tail item is
1816 * returned or %NULL if the list is empty.
1818 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1820 unsigned long flags;
1821 struct sk_buff *result;
1823 spin_lock_irqsave(&list->lock, flags);
1824 result = __skb_dequeue_tail(list);
1825 spin_unlock_irqrestore(&list->lock, flags);
1830 * skb_queue_purge - empty a list
1831 * @list: list to empty
1833 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1834 * the list and one reference dropped. This function takes the list
1835 * lock and is atomic with respect to other list locking functions.
1837 void skb_queue_purge(struct sk_buff_head *list)
1839 struct sk_buff *skb;
1840 while ((skb = skb_dequeue(list)) != NULL)
1845 * skb_queue_head - queue a buffer at the list head
1846 * @list: list to use
1847 * @newsk: buffer to queue
1849 * Queue a buffer at the start of the list. This function takes the
1850 * list lock and can be used safely with other locking &sk_buff functions
1853 * A buffer cannot be placed on two lists at the same time.
1855 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1857 unsigned long flags;
1859 spin_lock_irqsave(&list->lock, flags);
1860 __skb_queue_head(list, newsk);
1861 spin_unlock_irqrestore(&list->lock, flags);
1865 * skb_queue_tail - queue a buffer at the list tail
1866 * @list: list to use
1867 * @newsk: buffer to queue
1869 * Queue a buffer at the tail of the list. This function takes the
1870 * list lock and can be used safely with other locking &sk_buff functions
1873 * A buffer cannot be placed on two lists at the same time.
1875 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1877 unsigned long flags;
1879 spin_lock_irqsave(&list->lock, flags);
1880 __skb_queue_tail(list, newsk);
1881 spin_unlock_irqrestore(&list->lock, flags);
1885 * skb_unlink - remove a buffer from a list
1886 * @skb: buffer to remove
1887 * @list: list to use
1889 * Remove a packet from a list. The list locks are taken and this
1890 * function is atomic with respect to other list locked calls
1892 * You must know what list the SKB is on.
1894 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1896 unsigned long flags;
1898 spin_lock_irqsave(&list->lock, flags);
1899 __skb_unlink(skb, list);
1900 spin_unlock_irqrestore(&list->lock, flags);
1904 * skb_append - append a buffer
1905 * @old: buffer to insert after
1906 * @newsk: buffer to insert
1907 * @list: list to use
1909 * Place a packet after a given packet in a list. The list locks are taken
1910 * and this function is atomic with respect to other list locked calls.
1911 * A buffer cannot be placed on two lists at the same time.
1913 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1915 unsigned long flags;
1917 spin_lock_irqsave(&list->lock, flags);
1918 __skb_queue_after(list, old, newsk);
1919 spin_unlock_irqrestore(&list->lock, flags);
1924 * skb_insert - insert a buffer
1925 * @old: buffer to insert before
1926 * @newsk: buffer to insert
1927 * @list: list to use
1929 * Place a packet before a given packet in a list. The list locks are
1930 * taken and this function is atomic with respect to other list locked
1933 * A buffer cannot be placed on two lists at the same time.
1935 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1937 unsigned long flags;
1939 spin_lock_irqsave(&list->lock, flags);
1940 __skb_insert(newsk, old->prev, old, list);
1941 spin_unlock_irqrestore(&list->lock, flags);
1944 static inline void skb_split_inside_header(struct sk_buff *skb,
1945 struct sk_buff* skb1,
1946 const u32 len, const int pos)
1950 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1952 /* And move data appendix as is. */
1953 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1954 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1956 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1957 skb_shinfo(skb)->nr_frags = 0;
1958 skb1->data_len = skb->data_len;
1959 skb1->len += skb1->data_len;
1962 skb_set_tail_pointer(skb, len);
1965 static inline void skb_split_no_header(struct sk_buff *skb,
1966 struct sk_buff* skb1,
1967 const u32 len, int pos)
1970 const int nfrags = skb_shinfo(skb)->nr_frags;
1972 skb_shinfo(skb)->nr_frags = 0;
1973 skb1->len = skb1->data_len = skb->len - len;
1975 skb->data_len = len - pos;
1977 for (i = 0; i < nfrags; i++) {
1978 int size = skb_shinfo(skb)->frags[i].size;
1980 if (pos + size > len) {
1981 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1985 * We have two variants in this case:
1986 * 1. Move all the frag to the second
1987 * part, if it is possible. F.e.
1988 * this approach is mandatory for TUX,
1989 * where splitting is expensive.
1990 * 2. Split is accurately. We make this.
1992 get_page(skb_shinfo(skb)->frags[i].page);
1993 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1994 skb_shinfo(skb1)->frags[0].size -= len - pos;
1995 skb_shinfo(skb)->frags[i].size = len - pos;
1996 skb_shinfo(skb)->nr_frags++;
2000 skb_shinfo(skb)->nr_frags++;
2003 skb_shinfo(skb1)->nr_frags = k;
2007 * skb_split - Split fragmented skb to two parts at length len.
2008 * @skb: the buffer to split
2009 * @skb1: the buffer to receive the second part
2010 * @len: new length for skb
2012 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2014 int pos = skb_headlen(skb);
2016 if (len < pos) /* Split line is inside header. */
2017 skb_split_inside_header(skb, skb1, len, pos);
2018 else /* Second chunk has no header, nothing to copy. */
2019 skb_split_no_header(skb, skb1, len, pos);
2022 /* Shifting from/to a cloned skb is a no-go.
2024 * Caller cannot keep skb_shinfo related pointers past calling here!
2026 static int skb_prepare_for_shift(struct sk_buff *skb)
2028 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2032 * skb_shift - Shifts paged data partially from skb to another
2033 * @tgt: buffer into which tail data gets added
2034 * @skb: buffer from which the paged data comes from
2035 * @shiftlen: shift up to this many bytes
2037 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2038 * the length of the skb, from tgt to skb. Returns number bytes shifted.
2039 * It's up to caller to free skb if everything was shifted.
2041 * If @tgt runs out of frags, the whole operation is aborted.
2043 * Skb cannot include anything else but paged data while tgt is allowed
2044 * to have non-paged data as well.
2046 * TODO: full sized shift could be optimized but that would need
2047 * specialized skb free'er to handle frags without up-to-date nr_frags.
2049 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2051 int from, to, merge, todo;
2052 struct skb_frag_struct *fragfrom, *fragto;
2054 BUG_ON(shiftlen > skb->len);
2055 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2059 to = skb_shinfo(tgt)->nr_frags;
2060 fragfrom = &skb_shinfo(skb)->frags[from];
2062 /* Actual merge is delayed until the point when we know we can
2063 * commit all, so that we don't have to undo partial changes
2066 !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
2071 todo -= fragfrom->size;
2073 if (skb_prepare_for_shift(skb) ||
2074 skb_prepare_for_shift(tgt))
2077 /* All previous frag pointers might be stale! */
2078 fragfrom = &skb_shinfo(skb)->frags[from];
2079 fragto = &skb_shinfo(tgt)->frags[merge];
2081 fragto->size += shiftlen;
2082 fragfrom->size -= shiftlen;
2083 fragfrom->page_offset += shiftlen;
2091 /* Skip full, not-fitting skb to avoid expensive operations */
2092 if ((shiftlen == skb->len) &&
2093 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2096 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2099 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2100 if (to == MAX_SKB_FRAGS)
2103 fragfrom = &skb_shinfo(skb)->frags[from];
2104 fragto = &skb_shinfo(tgt)->frags[to];
2106 if (todo >= fragfrom->size) {
2107 *fragto = *fragfrom;
2108 todo -= fragfrom->size;
2113 get_page(fragfrom->page);
2114 fragto->page = fragfrom->page;
2115 fragto->page_offset = fragfrom->page_offset;
2116 fragto->size = todo;
2118 fragfrom->page_offset += todo;
2119 fragfrom->size -= todo;
2127 /* Ready to "commit" this state change to tgt */
2128 skb_shinfo(tgt)->nr_frags = to;
2131 fragfrom = &skb_shinfo(skb)->frags[0];
2132 fragto = &skb_shinfo(tgt)->frags[merge];
2134 fragto->size += fragfrom->size;
2135 put_page(fragfrom->page);
2138 /* Reposition in the original skb */
2140 while (from < skb_shinfo(skb)->nr_frags)
2141 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2142 skb_shinfo(skb)->nr_frags = to;
2144 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2147 /* Most likely the tgt won't ever need its checksum anymore, skb on
2148 * the other hand might need it if it needs to be resent
2150 tgt->ip_summed = CHECKSUM_PARTIAL;
2151 skb->ip_summed = CHECKSUM_PARTIAL;
2153 /* Yak, is it really working this way? Some helper please? */
2154 skb->len -= shiftlen;
2155 skb->data_len -= shiftlen;
2156 skb->truesize -= shiftlen;
2157 tgt->len += shiftlen;
2158 tgt->data_len += shiftlen;
2159 tgt->truesize += shiftlen;
2165 * skb_prepare_seq_read - Prepare a sequential read of skb data
2166 * @skb: the buffer to read
2167 * @from: lower offset of data to be read
2168 * @to: upper offset of data to be read
2169 * @st: state variable
2171 * Initializes the specified state variable. Must be called before
2172 * invoking skb_seq_read() for the first time.
2174 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2175 unsigned int to, struct skb_seq_state *st)
2177 st->lower_offset = from;
2178 st->upper_offset = to;
2179 st->root_skb = st->cur_skb = skb;
2180 st->frag_idx = st->stepped_offset = 0;
2181 st->frag_data = NULL;
2185 * skb_seq_read - Sequentially read skb data
2186 * @consumed: number of bytes consumed by the caller so far
2187 * @data: destination pointer for data to be returned
2188 * @st: state variable
2190 * Reads a block of skb data at &consumed relative to the
2191 * lower offset specified to skb_prepare_seq_read(). Assigns
2192 * the head of the data block to &data and returns the length
2193 * of the block or 0 if the end of the skb data or the upper
2194 * offset has been reached.
2196 * The caller is not required to consume all of the data
2197 * returned, i.e. &consumed is typically set to the number
2198 * of bytes already consumed and the next call to
2199 * skb_seq_read() will return the remaining part of the block.
2201 * Note 1: The size of each block of data returned can be arbitary,
2202 * this limitation is the cost for zerocopy seqeuental
2203 * reads of potentially non linear data.
2205 * Note 2: Fragment lists within fragments are not implemented
2206 * at the moment, state->root_skb could be replaced with
2207 * a stack for this purpose.
2209 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2210 struct skb_seq_state *st)
2212 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2215 if (unlikely(abs_offset >= st->upper_offset))
2219 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2221 if (abs_offset < block_limit) {
2222 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2223 return block_limit - abs_offset;
2226 if (st->frag_idx == 0 && !st->frag_data)
2227 st->stepped_offset += skb_headlen(st->cur_skb);
2229 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2230 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2231 block_limit = frag->size + st->stepped_offset;
2233 if (abs_offset < block_limit) {
2235 st->frag_data = kmap_skb_frag(frag);
2237 *data = (u8 *) st->frag_data + frag->page_offset +
2238 (abs_offset - st->stepped_offset);
2240 return block_limit - abs_offset;
2243 if (st->frag_data) {
2244 kunmap_skb_frag(st->frag_data);
2245 st->frag_data = NULL;
2249 st->stepped_offset += frag->size;
2252 if (st->frag_data) {
2253 kunmap_skb_frag(st->frag_data);
2254 st->frag_data = NULL;
2257 if (st->root_skb == st->cur_skb &&
2258 skb_shinfo(st->root_skb)->frag_list) {
2259 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2262 } else if (st->cur_skb->next) {
2263 st->cur_skb = st->cur_skb->next;
2272 * skb_abort_seq_read - Abort a sequential read of skb data
2273 * @st: state variable
2275 * Must be called if skb_seq_read() was not called until it
2278 void skb_abort_seq_read(struct skb_seq_state *st)
2281 kunmap_skb_frag(st->frag_data);
2284 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2286 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2287 struct ts_config *conf,
2288 struct ts_state *state)
2290 return skb_seq_read(offset, text, TS_SKB_CB(state));
2293 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2295 skb_abort_seq_read(TS_SKB_CB(state));
2299 * skb_find_text - Find a text pattern in skb data
2300 * @skb: the buffer to look in
2301 * @from: search offset
2303 * @config: textsearch configuration
2304 * @state: uninitialized textsearch state variable
2306 * Finds a pattern in the skb data according to the specified
2307 * textsearch configuration. Use textsearch_next() to retrieve
2308 * subsequent occurrences of the pattern. Returns the offset
2309 * to the first occurrence or UINT_MAX if no match was found.
2311 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2312 unsigned int to, struct ts_config *config,
2313 struct ts_state *state)
2317 config->get_next_block = skb_ts_get_next_block;
2318 config->finish = skb_ts_finish;
2320 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2322 ret = textsearch_find(config, state);
2323 return (ret <= to - from ? ret : UINT_MAX);
2327 * skb_append_datato_frags: - append the user data to a skb
2328 * @sk: sock structure
2329 * @skb: skb structure to be appened with user data.
2330 * @getfrag: call back function to be used for getting the user data
2331 * @from: pointer to user message iov
2332 * @length: length of the iov message
2334 * Description: This procedure append the user data in the fragment part
2335 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2337 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2338 int (*getfrag)(void *from, char *to, int offset,
2339 int len, int odd, struct sk_buff *skb),
2340 void *from, int length)
2343 skb_frag_t *frag = NULL;
2344 struct page *page = NULL;
2350 /* Return error if we don't have space for new frag */
2351 frg_cnt = skb_shinfo(skb)->nr_frags;
2352 if (frg_cnt >= MAX_SKB_FRAGS)
2355 /* allocate a new page for next frag */
2356 page = alloc_pages(sk->sk_allocation, 0);
2358 /* If alloc_page fails just return failure and caller will
2359 * free previous allocated pages by doing kfree_skb()
2364 /* initialize the next frag */
2365 sk->sk_sndmsg_page = page;
2366 sk->sk_sndmsg_off = 0;
2367 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2368 skb->truesize += PAGE_SIZE;
2369 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2371 /* get the new initialized frag */
2372 frg_cnt = skb_shinfo(skb)->nr_frags;
2373 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2375 /* copy the user data to page */
2376 left = PAGE_SIZE - frag->page_offset;
2377 copy = (length > left)? left : length;
2379 ret = getfrag(from, (page_address(frag->page) +
2380 frag->page_offset + frag->size),
2381 offset, copy, 0, skb);
2385 /* copy was successful so update the size parameters */
2386 sk->sk_sndmsg_off += copy;
2389 skb->data_len += copy;
2393 } while (length > 0);
2399 * skb_pull_rcsum - pull skb and update receive checksum
2400 * @skb: buffer to update
2401 * @len: length of data pulled
2403 * This function performs an skb_pull on the packet and updates
2404 * the CHECKSUM_COMPLETE checksum. It should be used on
2405 * receive path processing instead of skb_pull unless you know
2406 * that the checksum difference is zero (e.g., a valid IP header)
2407 * or you are setting ip_summed to CHECKSUM_NONE.
2409 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2411 BUG_ON(len > skb->len);
2413 BUG_ON(skb->len < skb->data_len);
2414 skb_postpull_rcsum(skb, skb->data, len);
2415 return skb->data += len;
2418 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2421 * skb_segment - Perform protocol segmentation on skb.
2422 * @skb: buffer to segment
2423 * @features: features for the output path (see dev->features)
2425 * This function performs segmentation on the given skb. It returns
2426 * a pointer to the first in a list of new skbs for the segments.
2427 * In case of error it returns ERR_PTR(err).
2429 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2431 struct sk_buff *segs = NULL;
2432 struct sk_buff *tail = NULL;
2433 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2434 unsigned int mss = skb_shinfo(skb)->gso_size;
2435 unsigned int doffset = skb->data - skb_mac_header(skb);
2436 unsigned int offset = doffset;
2437 unsigned int headroom;
2439 int sg = features & NETIF_F_SG;
2440 int nfrags = skb_shinfo(skb)->nr_frags;
2445 __skb_push(skb, doffset);
2446 headroom = skb_headroom(skb);
2447 pos = skb_headlen(skb);
2450 struct sk_buff *nskb;
2455 len = skb->len - offset;
2459 hsize = skb_headlen(skb) - offset;
2462 if (hsize > len || !sg)
2465 if (!hsize && i >= nfrags) {
2466 BUG_ON(fskb->len != len);
2469 nskb = skb_clone(fskb, GFP_ATOMIC);
2472 if (unlikely(!nskb))
2475 hsize = skb_end_pointer(nskb) - nskb->head;
2476 if (skb_cow_head(nskb, doffset + headroom)) {
2481 nskb->truesize += skb_end_pointer(nskb) - nskb->head -
2483 skb_release_head_state(nskb);
2484 __skb_push(nskb, doffset);
2486 nskb = alloc_skb(hsize + doffset + headroom,
2489 if (unlikely(!nskb))
2492 skb_reserve(nskb, headroom);
2493 __skb_put(nskb, doffset);
2502 __copy_skb_header(nskb, skb);
2503 nskb->mac_len = skb->mac_len;
2505 skb_reset_mac_header(nskb);
2506 skb_set_network_header(nskb, skb->mac_len);
2507 nskb->transport_header = (nskb->network_header +
2508 skb_network_header_len(skb));
2509 skb_copy_from_linear_data(skb, nskb->data, doffset);
2511 if (pos >= offset + len)
2515 nskb->ip_summed = CHECKSUM_NONE;
2516 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2522 frag = skb_shinfo(nskb)->frags;
2524 skb_copy_from_linear_data_offset(skb, offset,
2525 skb_put(nskb, hsize), hsize);
2527 while (pos < offset + len && i < nfrags) {
2528 *frag = skb_shinfo(skb)->frags[i];
2529 get_page(frag->page);
2533 frag->page_offset += offset - pos;
2534 frag->size -= offset - pos;
2537 skb_shinfo(nskb)->nr_frags++;
2539 if (pos + size <= offset + len) {
2543 frag->size -= pos + size - (offset + len);
2550 if (pos < offset + len) {
2551 struct sk_buff *fskb2 = fskb;
2553 BUG_ON(pos + fskb->len != offset + len);
2559 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2565 BUG_ON(skb_shinfo(nskb)->frag_list);
2566 skb_shinfo(nskb)->frag_list = fskb2;
2570 nskb->data_len = len - hsize;
2571 nskb->len += nskb->data_len;
2572 nskb->truesize += nskb->data_len;
2573 } while ((offset += len) < skb->len);
2578 while ((skb = segs)) {
2582 return ERR_PTR(err);
2585 EXPORT_SYMBOL_GPL(skb_segment);
2587 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2589 struct sk_buff *p = *head;
2590 struct sk_buff *nskb;
2591 unsigned int headroom;
2592 unsigned int hlen = p->data - skb_mac_header(p);
2593 unsigned int len = skb->len;
2595 if (hlen + p->len + len >= 65536)
2598 if (skb_shinfo(p)->frag_list)
2600 else if (!skb_headlen(p) && !skb_headlen(skb) &&
2601 skb_shinfo(p)->nr_frags + skb_shinfo(skb)->nr_frags <
2603 memcpy(skb_shinfo(p)->frags + skb_shinfo(p)->nr_frags,
2604 skb_shinfo(skb)->frags,
2605 skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
2607 skb_shinfo(p)->nr_frags += skb_shinfo(skb)->nr_frags;
2608 skb_shinfo(skb)->nr_frags = 0;
2610 skb->truesize -= skb->data_len;
2611 skb->len -= skb->data_len;
2614 NAPI_GRO_CB(skb)->free = 1;
2618 headroom = skb_headroom(p);
2619 nskb = netdev_alloc_skb(p->dev, headroom);
2620 if (unlikely(!nskb))
2623 __copy_skb_header(nskb, p);
2624 nskb->mac_len = p->mac_len;
2626 skb_reserve(nskb, headroom);
2628 skb_set_mac_header(nskb, -hlen);
2629 skb_set_network_header(nskb, skb_network_offset(p));
2630 skb_set_transport_header(nskb, skb_transport_offset(p));
2632 memcpy(skb_mac_header(nskb), skb_mac_header(p), hlen);
2634 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
2635 skb_shinfo(nskb)->frag_list = p;
2636 skb_shinfo(nskb)->gso_size = skb_shinfo(p)->gso_size;
2637 skb_header_release(p);
2640 nskb->data_len += p->len;
2641 nskb->truesize += p->len;
2642 nskb->len += p->len;
2645 nskb->next = p->next;
2651 p->prev->next = skb;
2653 skb_header_release(skb);
2656 NAPI_GRO_CB(p)->count++;
2661 NAPI_GRO_CB(skb)->same_flow = 1;
2664 EXPORT_SYMBOL_GPL(skb_gro_receive);
2666 void __init skb_init(void)
2668 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2669 sizeof(struct sk_buff),
2671 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2673 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2674 (2*sizeof(struct sk_buff)) +
2677 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2682 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2683 * @skb: Socket buffer containing the buffers to be mapped
2684 * @sg: The scatter-gather list to map into
2685 * @offset: The offset into the buffer's contents to start mapping
2686 * @len: Length of buffer space to be mapped
2688 * Fill the specified scatter-gather list with mappings/pointers into a
2689 * region of the buffer space attached to a socket buffer.
2692 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2694 int start = skb_headlen(skb);
2695 int i, copy = start - offset;
2701 sg_set_buf(sg, skb->data + offset, copy);
2703 if ((len -= copy) == 0)
2708 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2711 WARN_ON(start > offset + len);
2713 end = start + skb_shinfo(skb)->frags[i].size;
2714 if ((copy = end - offset) > 0) {
2715 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2719 sg_set_page(&sg[elt], frag->page, copy,
2720 frag->page_offset+offset-start);
2729 if (skb_shinfo(skb)->frag_list) {
2730 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2732 for (; list; list = list->next) {
2735 WARN_ON(start > offset + len);
2737 end = start + list->len;
2738 if ((copy = end - offset) > 0) {
2741 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2743 if ((len -= copy) == 0)
2754 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2756 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2758 sg_mark_end(&sg[nsg - 1]);
2764 * skb_cow_data - Check that a socket buffer's data buffers are writable
2765 * @skb: The socket buffer to check.
2766 * @tailbits: Amount of trailing space to be added
2767 * @trailer: Returned pointer to the skb where the @tailbits space begins
2769 * Make sure that the data buffers attached to a socket buffer are
2770 * writable. If they are not, private copies are made of the data buffers
2771 * and the socket buffer is set to use these instead.
2773 * If @tailbits is given, make sure that there is space to write @tailbits
2774 * bytes of data beyond current end of socket buffer. @trailer will be
2775 * set to point to the skb in which this space begins.
2777 * The number of scatterlist elements required to completely map the
2778 * COW'd and extended socket buffer will be returned.
2780 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2784 struct sk_buff *skb1, **skb_p;
2786 /* If skb is cloned or its head is paged, reallocate
2787 * head pulling out all the pages (pages are considered not writable
2788 * at the moment even if they are anonymous).
2790 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2791 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2794 /* Easy case. Most of packets will go this way. */
2795 if (!skb_shinfo(skb)->frag_list) {
2796 /* A little of trouble, not enough of space for trailer.
2797 * This should not happen, when stack is tuned to generate
2798 * good frames. OK, on miss we reallocate and reserve even more
2799 * space, 128 bytes is fair. */
2801 if (skb_tailroom(skb) < tailbits &&
2802 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2810 /* Misery. We are in troubles, going to mincer fragments... */
2813 skb_p = &skb_shinfo(skb)->frag_list;
2816 while ((skb1 = *skb_p) != NULL) {
2819 /* The fragment is partially pulled by someone,
2820 * this can happen on input. Copy it and everything
2823 if (skb_shared(skb1))
2826 /* If the skb is the last, worry about trailer. */
2828 if (skb1->next == NULL && tailbits) {
2829 if (skb_shinfo(skb1)->nr_frags ||
2830 skb_shinfo(skb1)->frag_list ||
2831 skb_tailroom(skb1) < tailbits)
2832 ntail = tailbits + 128;
2838 skb_shinfo(skb1)->nr_frags ||
2839 skb_shinfo(skb1)->frag_list) {
2840 struct sk_buff *skb2;
2842 /* Fuck, we are miserable poor guys... */
2844 skb2 = skb_copy(skb1, GFP_ATOMIC);
2846 skb2 = skb_copy_expand(skb1,
2850 if (unlikely(skb2 == NULL))
2854 skb_set_owner_w(skb2, skb1->sk);
2856 /* Looking around. Are we still alive?
2857 * OK, link new skb, drop old one */
2859 skb2->next = skb1->next;
2866 skb_p = &skb1->next;
2873 * skb_partial_csum_set - set up and verify partial csum values for packet
2874 * @skb: the skb to set
2875 * @start: the number of bytes after skb->data to start checksumming.
2876 * @off: the offset from start to place the checksum.
2878 * For untrusted partially-checksummed packets, we need to make sure the values
2879 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2881 * This function checks and sets those values and skb->ip_summed: if this
2882 * returns false you should drop the packet.
2884 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2886 if (unlikely(start > skb->len - 2) ||
2887 unlikely((int)start + off > skb->len - 2)) {
2888 if (net_ratelimit())
2890 "bad partial csum: csum=%u/%u len=%u\n",
2891 start, off, skb->len);
2894 skb->ip_summed = CHECKSUM_PARTIAL;
2895 skb->csum_start = skb_headroom(skb) + start;
2896 skb->csum_offset = off;
2900 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2902 if (net_ratelimit())
2903 pr_warning("%s: received packets cannot be forwarded"
2904 " while LRO is enabled\n", skb->dev->name);
2907 EXPORT_SYMBOL(___pskb_trim);
2908 EXPORT_SYMBOL(__kfree_skb);
2909 EXPORT_SYMBOL(kfree_skb);
2910 EXPORT_SYMBOL(__pskb_pull_tail);
2911 EXPORT_SYMBOL(__alloc_skb);
2912 EXPORT_SYMBOL(__netdev_alloc_skb);
2913 EXPORT_SYMBOL(pskb_copy);
2914 EXPORT_SYMBOL(pskb_expand_head);
2915 EXPORT_SYMBOL(skb_checksum);
2916 EXPORT_SYMBOL(skb_clone);
2917 EXPORT_SYMBOL(skb_copy);
2918 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2919 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2920 EXPORT_SYMBOL(skb_copy_bits);
2921 EXPORT_SYMBOL(skb_copy_expand);
2922 EXPORT_SYMBOL(skb_over_panic);
2923 EXPORT_SYMBOL(skb_pad);
2924 EXPORT_SYMBOL(skb_realloc_headroom);
2925 EXPORT_SYMBOL(skb_under_panic);
2926 EXPORT_SYMBOL(skb_dequeue);
2927 EXPORT_SYMBOL(skb_dequeue_tail);
2928 EXPORT_SYMBOL(skb_insert);
2929 EXPORT_SYMBOL(skb_queue_purge);
2930 EXPORT_SYMBOL(skb_queue_head);
2931 EXPORT_SYMBOL(skb_queue_tail);
2932 EXPORT_SYMBOL(skb_unlink);
2933 EXPORT_SYMBOL(skb_append);
2934 EXPORT_SYMBOL(skb_split);
2935 EXPORT_SYMBOL(skb_prepare_seq_read);
2936 EXPORT_SYMBOL(skb_seq_read);
2937 EXPORT_SYMBOL(skb_abort_seq_read);
2938 EXPORT_SYMBOL(skb_find_text);
2939 EXPORT_SYMBOL(skb_append_datato_frags);
2940 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
2942 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2943 EXPORT_SYMBOL_GPL(skb_cow_data);
2944 EXPORT_SYMBOL_GPL(skb_partial_csum_set);