skbuff: skb_mac_header_was_set is always true on >32 bit
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / core / skbuff.c
blob436695d8deb8a718bf7f14ee325e1919fb8bd289
1 /*
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>
7 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #include <linux/module.h>
40 #include <linux/types.h>
41 #include <linux/kernel.h>
42 #include <linux/mm.h>
43 #include <linux/interrupt.h>
44 #include <linux/in.h>
45 #include <linux/inet.h>
46 #include <linux/slab.h>
47 #include <linux/netdevice.h>
48 #ifdef CONFIG_NET_CLS_ACT
49 #include <net/pkt_sched.h>
50 #endif
51 #include <linux/string.h>
52 #include <linux/skbuff.h>
53 #include <linux/splice.h>
54 #include <linux/cache.h>
55 #include <linux/rtnetlink.h>
56 #include <linux/init.h>
57 #include <linux/scatterlist.h>
58 #include <linux/errqueue.h>
60 #include <net/protocol.h>
61 #include <net/dst.h>
62 #include <net/sock.h>
63 #include <net/checksum.h>
64 #include <net/xfrm.h>
66 #include <asm/uaccess.h>
67 #include <asm/system.h>
68 #include <trace/events/skb.h>
70 #include "kmap_skb.h"
72 static struct kmem_cache *skbuff_head_cache __read_mostly;
73 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
75 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
76 struct pipe_buffer *buf)
78 put_page(buf->page);
81 static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
82 struct pipe_buffer *buf)
84 get_page(buf->page);
87 static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
88 struct pipe_buffer *buf)
90 return 1;
94 /* Pipe buffer operations for a socket. */
95 static struct pipe_buf_operations sock_pipe_buf_ops = {
96 .can_merge = 0,
97 .map = generic_pipe_buf_map,
98 .unmap = generic_pipe_buf_unmap,
99 .confirm = generic_pipe_buf_confirm,
100 .release = sock_pipe_buf_release,
101 .steal = sock_pipe_buf_steal,
102 .get = sock_pipe_buf_get,
106 * Keep out-of-line to prevent kernel bloat.
107 * __builtin_return_address is not used because it is not always
108 * reliable.
112 * skb_over_panic - private function
113 * @skb: buffer
114 * @sz: size
115 * @here: address
117 * Out of line support code for skb_put(). Not user callable.
119 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
121 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
122 "data:%p tail:%#lx end:%#lx dev:%s\n",
123 here, skb->len, sz, skb->head, skb->data,
124 (unsigned long)skb->tail, (unsigned long)skb->end,
125 skb->dev ? skb->dev->name : "<NULL>");
126 BUG();
128 EXPORT_SYMBOL(skb_over_panic);
131 * skb_under_panic - private function
132 * @skb: buffer
133 * @sz: size
134 * @here: address
136 * Out of line support code for skb_push(). Not user callable.
139 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
141 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
142 "data:%p tail:%#lx end:%#lx dev:%s\n",
143 here, skb->len, sz, skb->head, skb->data,
144 (unsigned long)skb->tail, (unsigned long)skb->end,
145 skb->dev ? skb->dev->name : "<NULL>");
146 BUG();
148 EXPORT_SYMBOL(skb_under_panic);
150 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
151 * 'private' fields and also do memory statistics to find all the
152 * [BEEP] leaks.
157 * __alloc_skb - allocate a network buffer
158 * @size: size to allocate
159 * @gfp_mask: allocation mask
160 * @fclone: allocate from fclone cache instead of head cache
161 * and allocate a cloned (child) skb
162 * @node: numa node to allocate memory on
164 * Allocate a new &sk_buff. The returned buffer has no headroom and a
165 * tail room of size bytes. The object has a reference count of one.
166 * The return is the buffer. On a failure the return is %NULL.
168 * Buffers may only be allocated from interrupts using a @gfp_mask of
169 * %GFP_ATOMIC.
171 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
172 int fclone, int node)
174 struct kmem_cache *cache;
175 struct skb_shared_info *shinfo;
176 struct sk_buff *skb;
177 u8 *data;
179 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
181 /* Get the HEAD */
182 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
183 if (!skb)
184 goto out;
186 size = SKB_DATA_ALIGN(size);
187 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
188 gfp_mask, node);
189 if (!data)
190 goto nodata;
193 * Only clear those fields we need to clear, not those that we will
194 * actually initialise below. Hence, don't put any more fields after
195 * the tail pointer in struct sk_buff!
197 memset(skb, 0, offsetof(struct sk_buff, tail));
198 skb->truesize = size + sizeof(struct sk_buff);
199 atomic_set(&skb->users, 1);
200 skb->head = data;
201 skb->data = data;
202 skb_reset_tail_pointer(skb);
203 skb->end = skb->tail + size;
204 #ifdef NET_SKBUFF_DATA_USES_OFFSET
205 skb->mac_header = ~0U;
206 #endif
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->tx_flags.flags = 0;
217 skb_frag_list_init(skb);
218 memset(&shinfo->hwtstamps, 0, sizeof(shinfo->hwtstamps));
220 if (fclone) {
221 struct sk_buff *child = skb + 1;
222 atomic_t *fclone_ref = (atomic_t *) (child + 1);
224 skb->fclone = SKB_FCLONE_ORIG;
225 atomic_set(fclone_ref, 1);
227 child->fclone = SKB_FCLONE_UNAVAILABLE;
229 out:
230 return skb;
231 nodata:
232 kmem_cache_free(cache, skb);
233 skb = NULL;
234 goto out;
236 EXPORT_SYMBOL(__alloc_skb);
239 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
240 * @dev: network device to receive on
241 * @length: length to allocate
242 * @gfp_mask: get_free_pages mask, passed to alloc_skb
244 * Allocate a new &sk_buff and assign it a usage count of one. The
245 * buffer has unspecified headroom built in. Users should allocate
246 * the headroom they think they need without accounting for the
247 * built in space. The built in space is used for optimisations.
249 * %NULL is returned if there is no free memory.
251 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
252 unsigned int length, gfp_t gfp_mask)
254 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
255 struct sk_buff *skb;
257 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
258 if (likely(skb)) {
259 skb_reserve(skb, NET_SKB_PAD);
260 skb->dev = dev;
262 return skb;
264 EXPORT_SYMBOL(__netdev_alloc_skb);
266 struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
268 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
269 struct page *page;
271 page = alloc_pages_node(node, gfp_mask, 0);
272 return page;
274 EXPORT_SYMBOL(__netdev_alloc_page);
276 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
277 int size)
279 skb_fill_page_desc(skb, i, page, off, size);
280 skb->len += size;
281 skb->data_len += size;
282 skb->truesize += size;
284 EXPORT_SYMBOL(skb_add_rx_frag);
287 * dev_alloc_skb - allocate an skbuff for receiving
288 * @length: length to allocate
290 * Allocate a new &sk_buff and assign it a usage count of one. The
291 * buffer has unspecified headroom built in. Users should allocate
292 * the headroom they think they need without accounting for the
293 * built in space. The built in space is used for optimisations.
295 * %NULL is returned if there is no free memory. Although this function
296 * allocates memory it can be called from an interrupt.
298 struct sk_buff *dev_alloc_skb(unsigned int length)
301 * There is more code here than it seems:
302 * __dev_alloc_skb is an inline
304 return __dev_alloc_skb(length, GFP_ATOMIC);
306 EXPORT_SYMBOL(dev_alloc_skb);
308 static void skb_drop_list(struct sk_buff **listp)
310 struct sk_buff *list = *listp;
312 *listp = NULL;
314 do {
315 struct sk_buff *this = list;
316 list = list->next;
317 kfree_skb(this);
318 } while (list);
321 static inline void skb_drop_fraglist(struct sk_buff *skb)
323 skb_drop_list(&skb_shinfo(skb)->frag_list);
326 static void skb_clone_fraglist(struct sk_buff *skb)
328 struct sk_buff *list;
330 skb_walk_frags(skb, list)
331 skb_get(list);
334 static void skb_release_data(struct sk_buff *skb)
336 if (!skb->cloned ||
337 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
338 &skb_shinfo(skb)->dataref)) {
339 if (skb_shinfo(skb)->nr_frags) {
340 int i;
341 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
342 put_page(skb_shinfo(skb)->frags[i].page);
345 if (skb_has_frags(skb))
346 skb_drop_fraglist(skb);
348 kfree(skb->head);
353 * Free an skbuff by memory without cleaning the state.
355 static void kfree_skbmem(struct sk_buff *skb)
357 struct sk_buff *other;
358 atomic_t *fclone_ref;
360 switch (skb->fclone) {
361 case SKB_FCLONE_UNAVAILABLE:
362 kmem_cache_free(skbuff_head_cache, skb);
363 break;
365 case SKB_FCLONE_ORIG:
366 fclone_ref = (atomic_t *) (skb + 2);
367 if (atomic_dec_and_test(fclone_ref))
368 kmem_cache_free(skbuff_fclone_cache, skb);
369 break;
371 case SKB_FCLONE_CLONE:
372 fclone_ref = (atomic_t *) (skb + 1);
373 other = skb - 1;
375 /* The clone portion is available for
376 * fast-cloning again.
378 skb->fclone = SKB_FCLONE_UNAVAILABLE;
380 if (atomic_dec_and_test(fclone_ref))
381 kmem_cache_free(skbuff_fclone_cache, other);
382 break;
386 static void skb_release_head_state(struct sk_buff *skb)
388 skb_dst_drop(skb);
389 #ifdef CONFIG_XFRM
390 secpath_put(skb->sp);
391 #endif
392 if (skb->destructor) {
393 WARN_ON(in_irq());
394 skb->destructor(skb);
396 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
397 nf_conntrack_put(skb->nfct);
398 nf_conntrack_put_reasm(skb->nfct_reasm);
399 #endif
400 #ifdef CONFIG_BRIDGE_NETFILTER
401 nf_bridge_put(skb->nf_bridge);
402 #endif
403 /* XXX: IS this still necessary? - JHS */
404 #ifdef CONFIG_NET_SCHED
405 skb->tc_index = 0;
406 #ifdef CONFIG_NET_CLS_ACT
407 skb->tc_verd = 0;
408 #endif
409 #endif
412 /* Free everything but the sk_buff shell. */
413 static void skb_release_all(struct sk_buff *skb)
415 skb_release_head_state(skb);
416 skb_release_data(skb);
420 * __kfree_skb - private function
421 * @skb: buffer
423 * Free an sk_buff. Release anything attached to the buffer.
424 * Clean the state. This is an internal helper function. Users should
425 * always call kfree_skb
428 void __kfree_skb(struct sk_buff *skb)
430 skb_release_all(skb);
431 kfree_skbmem(skb);
433 EXPORT_SYMBOL(__kfree_skb);
436 * kfree_skb - free an sk_buff
437 * @skb: buffer to free
439 * Drop a reference to the buffer and free it if the usage count has
440 * hit zero.
442 void kfree_skb(struct sk_buff *skb)
444 if (unlikely(!skb))
445 return;
446 if (likely(atomic_read(&skb->users) == 1))
447 smp_rmb();
448 else if (likely(!atomic_dec_and_test(&skb->users)))
449 return;
450 trace_kfree_skb(skb, __builtin_return_address(0));
451 __kfree_skb(skb);
453 EXPORT_SYMBOL(kfree_skb);
456 * consume_skb - free an skbuff
457 * @skb: buffer to free
459 * Drop a ref to the buffer and free it if the usage count has hit zero
460 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
461 * is being dropped after a failure and notes that
463 void consume_skb(struct sk_buff *skb)
465 if (unlikely(!skb))
466 return;
467 if (likely(atomic_read(&skb->users) == 1))
468 smp_rmb();
469 else if (likely(!atomic_dec_and_test(&skb->users)))
470 return;
471 __kfree_skb(skb);
473 EXPORT_SYMBOL(consume_skb);
476 * skb_recycle_check - check if skb can be reused for receive
477 * @skb: buffer
478 * @skb_size: minimum receive buffer size
480 * Checks that the skb passed in is not shared or cloned, and
481 * that it is linear and its head portion at least as large as
482 * skb_size so that it can be recycled as a receive buffer.
483 * If these conditions are met, this function does any necessary
484 * reference count dropping and cleans up the skbuff as if it
485 * just came from __alloc_skb().
487 int skb_recycle_check(struct sk_buff *skb, int skb_size)
489 struct skb_shared_info *shinfo;
491 if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
492 return 0;
494 skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
495 if (skb_end_pointer(skb) - skb->head < skb_size)
496 return 0;
498 if (skb_shared(skb) || skb_cloned(skb))
499 return 0;
501 skb_release_head_state(skb);
502 shinfo = skb_shinfo(skb);
503 atomic_set(&shinfo->dataref, 1);
504 shinfo->nr_frags = 0;
505 shinfo->gso_size = 0;
506 shinfo->gso_segs = 0;
507 shinfo->gso_type = 0;
508 shinfo->ip6_frag_id = 0;
509 shinfo->tx_flags.flags = 0;
510 skb_frag_list_init(skb);
511 memset(&shinfo->hwtstamps, 0, sizeof(shinfo->hwtstamps));
513 memset(skb, 0, offsetof(struct sk_buff, tail));
514 skb->data = skb->head + NET_SKB_PAD;
515 skb_reset_tail_pointer(skb);
517 return 1;
519 EXPORT_SYMBOL(skb_recycle_check);
521 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
523 new->tstamp = old->tstamp;
524 new->dev = old->dev;
525 new->transport_header = old->transport_header;
526 new->network_header = old->network_header;
527 new->mac_header = old->mac_header;
528 skb_dst_set(new, dst_clone(skb_dst(old)));
529 #ifdef CONFIG_XFRM
530 new->sp = secpath_get(old->sp);
531 #endif
532 memcpy(new->cb, old->cb, sizeof(old->cb));
533 new->csum = old->csum;
534 new->local_df = old->local_df;
535 new->pkt_type = old->pkt_type;
536 new->ip_summed = old->ip_summed;
537 skb_copy_queue_mapping(new, old);
538 new->priority = old->priority;
539 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
540 new->ipvs_property = old->ipvs_property;
541 #endif
542 new->protocol = old->protocol;
543 new->mark = old->mark;
544 new->iif = old->iif;
545 __nf_copy(new, old);
546 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
547 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
548 new->nf_trace = old->nf_trace;
549 #endif
550 #ifdef CONFIG_NET_SCHED
551 new->tc_index = old->tc_index;
552 #ifdef CONFIG_NET_CLS_ACT
553 new->tc_verd = old->tc_verd;
554 #endif
555 #endif
556 new->vlan_tci = old->vlan_tci;
557 #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
558 new->do_not_encrypt = old->do_not_encrypt;
559 #endif
561 skb_copy_secmark(new, old);
565 * You should not add any new code to this function. Add it to
566 * __copy_skb_header above instead.
568 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
570 #define C(x) n->x = skb->x
572 n->next = n->prev = NULL;
573 n->sk = NULL;
574 __copy_skb_header(n, skb);
576 C(len);
577 C(data_len);
578 C(mac_len);
579 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
580 n->cloned = 1;
581 n->nohdr = 0;
582 n->destructor = NULL;
583 C(tail);
584 C(end);
585 C(head);
586 C(data);
587 C(truesize);
588 atomic_set(&n->users, 1);
590 atomic_inc(&(skb_shinfo(skb)->dataref));
591 skb->cloned = 1;
593 return n;
594 #undef C
598 * skb_morph - morph one skb into another
599 * @dst: the skb to receive the contents
600 * @src: the skb to supply the contents
602 * This is identical to skb_clone except that the target skb is
603 * supplied by the user.
605 * The target skb is returned upon exit.
607 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
609 skb_release_all(dst);
610 return __skb_clone(dst, src);
612 EXPORT_SYMBOL_GPL(skb_morph);
615 * skb_clone - duplicate an sk_buff
616 * @skb: buffer to clone
617 * @gfp_mask: allocation priority
619 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
620 * copies share the same packet data but not structure. The new
621 * buffer has a reference count of 1. If the allocation fails the
622 * function returns %NULL otherwise the new buffer is returned.
624 * If this function is called from an interrupt gfp_mask() must be
625 * %GFP_ATOMIC.
628 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
630 struct sk_buff *n;
632 n = skb + 1;
633 if (skb->fclone == SKB_FCLONE_ORIG &&
634 n->fclone == SKB_FCLONE_UNAVAILABLE) {
635 atomic_t *fclone_ref = (atomic_t *) (n + 1);
636 n->fclone = SKB_FCLONE_CLONE;
637 atomic_inc(fclone_ref);
638 } else {
639 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
640 if (!n)
641 return NULL;
642 n->fclone = SKB_FCLONE_UNAVAILABLE;
645 return __skb_clone(n, skb);
647 EXPORT_SYMBOL(skb_clone);
649 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
651 #ifndef NET_SKBUFF_DATA_USES_OFFSET
653 * Shift between the two data areas in bytes
655 unsigned long offset = new->data - old->data;
656 #endif
658 __copy_skb_header(new, old);
660 #ifndef NET_SKBUFF_DATA_USES_OFFSET
661 /* {transport,network,mac}_header are relative to skb->head */
662 new->transport_header += offset;
663 new->network_header += offset;
664 new->mac_header += offset;
665 #endif
666 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
667 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
668 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
672 * skb_copy - create private copy of an sk_buff
673 * @skb: buffer to copy
674 * @gfp_mask: allocation priority
676 * Make a copy of both an &sk_buff and its data. This is used when the
677 * caller wishes to modify the data and needs a private copy of the
678 * data to alter. Returns %NULL on failure or the pointer to the buffer
679 * on success. The returned buffer has a reference count of 1.
681 * As by-product this function converts non-linear &sk_buff to linear
682 * one, so that &sk_buff becomes completely private and caller is allowed
683 * to modify all the data of returned buffer. This means that this
684 * function is not recommended for use in circumstances when only
685 * header is going to be modified. Use pskb_copy() instead.
688 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
690 int headerlen = skb->data - skb->head;
692 * Allocate the copy buffer
694 struct sk_buff *n;
695 #ifdef NET_SKBUFF_DATA_USES_OFFSET
696 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
697 #else
698 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
699 #endif
700 if (!n)
701 return NULL;
703 /* Set the data pointer */
704 skb_reserve(n, headerlen);
705 /* Set the tail pointer and length */
706 skb_put(n, skb->len);
708 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
709 BUG();
711 copy_skb_header(n, skb);
712 return n;
714 EXPORT_SYMBOL(skb_copy);
717 * pskb_copy - create copy of an sk_buff with private head.
718 * @skb: buffer to copy
719 * @gfp_mask: allocation priority
721 * Make a copy of both an &sk_buff and part of its data, located
722 * in header. Fragmented data remain shared. This is used when
723 * the caller wishes to modify only header of &sk_buff and needs
724 * private copy of the header to alter. Returns %NULL on failure
725 * or the pointer to the buffer on success.
726 * The returned buffer has a reference count of 1.
729 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
732 * Allocate the copy buffer
734 struct sk_buff *n;
735 #ifdef NET_SKBUFF_DATA_USES_OFFSET
736 n = alloc_skb(skb->end, gfp_mask);
737 #else
738 n = alloc_skb(skb->end - skb->head, gfp_mask);
739 #endif
740 if (!n)
741 goto out;
743 /* Set the data pointer */
744 skb_reserve(n, skb->data - skb->head);
745 /* Set the tail pointer and length */
746 skb_put(n, skb_headlen(skb));
747 /* Copy the bytes */
748 skb_copy_from_linear_data(skb, n->data, n->len);
750 n->truesize += skb->data_len;
751 n->data_len = skb->data_len;
752 n->len = skb->len;
754 if (skb_shinfo(skb)->nr_frags) {
755 int i;
757 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
758 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
759 get_page(skb_shinfo(n)->frags[i].page);
761 skb_shinfo(n)->nr_frags = i;
764 if (skb_has_frags(skb)) {
765 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
766 skb_clone_fraglist(n);
769 copy_skb_header(n, skb);
770 out:
771 return n;
773 EXPORT_SYMBOL(pskb_copy);
776 * pskb_expand_head - reallocate header of &sk_buff
777 * @skb: buffer to reallocate
778 * @nhead: room to add at head
779 * @ntail: room to add at tail
780 * @gfp_mask: allocation priority
782 * Expands (or creates identical copy, if &nhead and &ntail are zero)
783 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
784 * reference count of 1. Returns zero in the case of success or error,
785 * if expansion failed. In the last case, &sk_buff is not changed.
787 * All the pointers pointing into skb header may change and must be
788 * reloaded after call to this function.
791 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
792 gfp_t gfp_mask)
794 int i;
795 u8 *data;
796 #ifdef NET_SKBUFF_DATA_USES_OFFSET
797 int size = nhead + skb->end + ntail;
798 #else
799 int size = nhead + (skb->end - skb->head) + ntail;
800 #endif
801 long off;
803 BUG_ON(nhead < 0);
805 if (skb_shared(skb))
806 BUG();
808 size = SKB_DATA_ALIGN(size);
810 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
811 if (!data)
812 goto nodata;
814 /* Copy only real data... and, alas, header. This should be
815 * optimized for the cases when header is void. */
816 #ifdef NET_SKBUFF_DATA_USES_OFFSET
817 memcpy(data + nhead, skb->head, skb->tail);
818 #else
819 memcpy(data + nhead, skb->head, skb->tail - skb->head);
820 #endif
821 memcpy(data + size, skb_end_pointer(skb),
822 sizeof(struct skb_shared_info));
824 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
825 get_page(skb_shinfo(skb)->frags[i].page);
827 if (skb_has_frags(skb))
828 skb_clone_fraglist(skb);
830 skb_release_data(skb);
832 off = (data + nhead) - skb->head;
834 skb->head = data;
835 skb->data += off;
836 #ifdef NET_SKBUFF_DATA_USES_OFFSET
837 skb->end = size;
838 off = nhead;
839 #else
840 skb->end = skb->head + size;
841 #endif
842 /* {transport,network,mac}_header and tail are relative to skb->head */
843 skb->tail += off;
844 skb->transport_header += off;
845 skb->network_header += off;
846 skb->mac_header += off;
847 skb->csum_start += nhead;
848 skb->cloned = 0;
849 skb->hdr_len = 0;
850 skb->nohdr = 0;
851 atomic_set(&skb_shinfo(skb)->dataref, 1);
852 return 0;
854 nodata:
855 return -ENOMEM;
857 EXPORT_SYMBOL(pskb_expand_head);
859 /* Make private copy of skb with writable head and some headroom */
861 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
863 struct sk_buff *skb2;
864 int delta = headroom - skb_headroom(skb);
866 if (delta <= 0)
867 skb2 = pskb_copy(skb, GFP_ATOMIC);
868 else {
869 skb2 = skb_clone(skb, GFP_ATOMIC);
870 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
871 GFP_ATOMIC)) {
872 kfree_skb(skb2);
873 skb2 = NULL;
876 return skb2;
878 EXPORT_SYMBOL(skb_realloc_headroom);
881 * skb_copy_expand - copy and expand sk_buff
882 * @skb: buffer to copy
883 * @newheadroom: new free bytes at head
884 * @newtailroom: new free bytes at tail
885 * @gfp_mask: allocation priority
887 * Make a copy of both an &sk_buff and its data and while doing so
888 * allocate additional space.
890 * This is used when the caller wishes to modify the data and needs a
891 * private copy of the data to alter as well as more space for new fields.
892 * Returns %NULL on failure or the pointer to the buffer
893 * on success. The returned buffer has a reference count of 1.
895 * You must pass %GFP_ATOMIC as the allocation priority if this function
896 * is called from an interrupt.
898 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
899 int newheadroom, int newtailroom,
900 gfp_t gfp_mask)
903 * Allocate the copy buffer
905 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
906 gfp_mask);
907 int oldheadroom = skb_headroom(skb);
908 int head_copy_len, head_copy_off;
909 int off;
911 if (!n)
912 return NULL;
914 skb_reserve(n, newheadroom);
916 /* Set the tail pointer and length */
917 skb_put(n, skb->len);
919 head_copy_len = oldheadroom;
920 head_copy_off = 0;
921 if (newheadroom <= head_copy_len)
922 head_copy_len = newheadroom;
923 else
924 head_copy_off = newheadroom - head_copy_len;
926 /* Copy the linear header and data. */
927 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
928 skb->len + head_copy_len))
929 BUG();
931 copy_skb_header(n, skb);
933 off = newheadroom - oldheadroom;
934 n->csum_start += off;
935 #ifdef NET_SKBUFF_DATA_USES_OFFSET
936 n->transport_header += off;
937 n->network_header += off;
938 n->mac_header += off;
939 #endif
941 return n;
943 EXPORT_SYMBOL(skb_copy_expand);
946 * skb_pad - zero pad the tail of an skb
947 * @skb: buffer to pad
948 * @pad: space to pad
950 * Ensure that a buffer is followed by a padding area that is zero
951 * filled. Used by network drivers which may DMA or transfer data
952 * beyond the buffer end onto the wire.
954 * May return error in out of memory cases. The skb is freed on error.
957 int skb_pad(struct sk_buff *skb, int pad)
959 int err;
960 int ntail;
962 /* If the skbuff is non linear tailroom is always zero.. */
963 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
964 memset(skb->data+skb->len, 0, pad);
965 return 0;
968 ntail = skb->data_len + pad - (skb->end - skb->tail);
969 if (likely(skb_cloned(skb) || ntail > 0)) {
970 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
971 if (unlikely(err))
972 goto free_skb;
975 /* FIXME: The use of this function with non-linear skb's really needs
976 * to be audited.
978 err = skb_linearize(skb);
979 if (unlikely(err))
980 goto free_skb;
982 memset(skb->data + skb->len, 0, pad);
983 return 0;
985 free_skb:
986 kfree_skb(skb);
987 return err;
989 EXPORT_SYMBOL(skb_pad);
992 * skb_put - add data to a buffer
993 * @skb: buffer to use
994 * @len: amount of data to add
996 * This function extends the used data area of the buffer. If this would
997 * exceed the total buffer size the kernel will panic. A pointer to the
998 * first byte of the extra data is returned.
1000 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1002 unsigned char *tmp = skb_tail_pointer(skb);
1003 SKB_LINEAR_ASSERT(skb);
1004 skb->tail += len;
1005 skb->len += len;
1006 if (unlikely(skb->tail > skb->end))
1007 skb_over_panic(skb, len, __builtin_return_address(0));
1008 return tmp;
1010 EXPORT_SYMBOL(skb_put);
1013 * skb_push - add data to the start of a buffer
1014 * @skb: buffer to use
1015 * @len: amount of data to add
1017 * This function extends the used data area of the buffer at the buffer
1018 * start. If this would exceed the total buffer headroom the kernel will
1019 * panic. A pointer to the first byte of the extra data is returned.
1021 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1023 skb->data -= len;
1024 skb->len += len;
1025 if (unlikely(skb->data<skb->head))
1026 skb_under_panic(skb, len, __builtin_return_address(0));
1027 return skb->data;
1029 EXPORT_SYMBOL(skb_push);
1032 * skb_pull - remove data from the start of a buffer
1033 * @skb: buffer to use
1034 * @len: amount of data to remove
1036 * This function removes data from the start of a buffer, returning
1037 * the memory to the headroom. A pointer to the next data in the buffer
1038 * is returned. Once the data has been pulled future pushes will overwrite
1039 * the old data.
1041 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1043 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
1045 EXPORT_SYMBOL(skb_pull);
1048 * skb_trim - remove end from a buffer
1049 * @skb: buffer to alter
1050 * @len: new length
1052 * Cut the length of a buffer down by removing data from the tail. If
1053 * the buffer is already under the length specified it is not modified.
1054 * The skb must be linear.
1056 void skb_trim(struct sk_buff *skb, unsigned int len)
1058 if (skb->len > len)
1059 __skb_trim(skb, len);
1061 EXPORT_SYMBOL(skb_trim);
1063 /* Trims skb to length len. It can change skb pointers.
1066 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1068 struct sk_buff **fragp;
1069 struct sk_buff *frag;
1070 int offset = skb_headlen(skb);
1071 int nfrags = skb_shinfo(skb)->nr_frags;
1072 int i;
1073 int err;
1075 if (skb_cloned(skb) &&
1076 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1077 return err;
1079 i = 0;
1080 if (offset >= len)
1081 goto drop_pages;
1083 for (; i < nfrags; i++) {
1084 int end = offset + skb_shinfo(skb)->frags[i].size;
1086 if (end < len) {
1087 offset = end;
1088 continue;
1091 skb_shinfo(skb)->frags[i++].size = len - offset;
1093 drop_pages:
1094 skb_shinfo(skb)->nr_frags = i;
1096 for (; i < nfrags; i++)
1097 put_page(skb_shinfo(skb)->frags[i].page);
1099 if (skb_has_frags(skb))
1100 skb_drop_fraglist(skb);
1101 goto done;
1104 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1105 fragp = &frag->next) {
1106 int end = offset + frag->len;
1108 if (skb_shared(frag)) {
1109 struct sk_buff *nfrag;
1111 nfrag = skb_clone(frag, GFP_ATOMIC);
1112 if (unlikely(!nfrag))
1113 return -ENOMEM;
1115 nfrag->next = frag->next;
1116 kfree_skb(frag);
1117 frag = nfrag;
1118 *fragp = frag;
1121 if (end < len) {
1122 offset = end;
1123 continue;
1126 if (end > len &&
1127 unlikely((err = pskb_trim(frag, len - offset))))
1128 return err;
1130 if (frag->next)
1131 skb_drop_list(&frag->next);
1132 break;
1135 done:
1136 if (len > skb_headlen(skb)) {
1137 skb->data_len -= skb->len - len;
1138 skb->len = len;
1139 } else {
1140 skb->len = len;
1141 skb->data_len = 0;
1142 skb_set_tail_pointer(skb, len);
1145 return 0;
1147 EXPORT_SYMBOL(___pskb_trim);
1150 * __pskb_pull_tail - advance tail of skb header
1151 * @skb: buffer to reallocate
1152 * @delta: number of bytes to advance tail
1154 * The function makes a sense only on a fragmented &sk_buff,
1155 * it expands header moving its tail forward and copying necessary
1156 * data from fragmented part.
1158 * &sk_buff MUST have reference count of 1.
1160 * Returns %NULL (and &sk_buff does not change) if pull failed
1161 * or value of new tail of skb in the case of success.
1163 * All the pointers pointing into skb header may change and must be
1164 * reloaded after call to this function.
1167 /* Moves tail of skb head forward, copying data from fragmented part,
1168 * when it is necessary.
1169 * 1. It may fail due to malloc failure.
1170 * 2. It may change skb pointers.
1172 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1174 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1176 /* If skb has not enough free space at tail, get new one
1177 * plus 128 bytes for future expansions. If we have enough
1178 * room at tail, reallocate without expansion only if skb is cloned.
1180 int i, k, eat = (skb->tail + delta) - skb->end;
1182 if (eat > 0 || skb_cloned(skb)) {
1183 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1184 GFP_ATOMIC))
1185 return NULL;
1188 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1189 BUG();
1191 /* Optimization: no fragments, no reasons to preestimate
1192 * size of pulled pages. Superb.
1194 if (!skb_has_frags(skb))
1195 goto pull_pages;
1197 /* Estimate size of pulled pages. */
1198 eat = delta;
1199 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1200 if (skb_shinfo(skb)->frags[i].size >= eat)
1201 goto pull_pages;
1202 eat -= skb_shinfo(skb)->frags[i].size;
1205 /* If we need update frag list, we are in troubles.
1206 * Certainly, it possible to add an offset to skb data,
1207 * but taking into account that pulling is expected to
1208 * be very rare operation, it is worth to fight against
1209 * further bloating skb head and crucify ourselves here instead.
1210 * Pure masohism, indeed. 8)8)
1212 if (eat) {
1213 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1214 struct sk_buff *clone = NULL;
1215 struct sk_buff *insp = NULL;
1217 do {
1218 BUG_ON(!list);
1220 if (list->len <= eat) {
1221 /* Eaten as whole. */
1222 eat -= list->len;
1223 list = list->next;
1224 insp = list;
1225 } else {
1226 /* Eaten partially. */
1228 if (skb_shared(list)) {
1229 /* Sucks! We need to fork list. :-( */
1230 clone = skb_clone(list, GFP_ATOMIC);
1231 if (!clone)
1232 return NULL;
1233 insp = list->next;
1234 list = clone;
1235 } else {
1236 /* This may be pulled without
1237 * problems. */
1238 insp = list;
1240 if (!pskb_pull(list, eat)) {
1241 kfree_skb(clone);
1242 return NULL;
1244 break;
1246 } while (eat);
1248 /* Free pulled out fragments. */
1249 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1250 skb_shinfo(skb)->frag_list = list->next;
1251 kfree_skb(list);
1253 /* And insert new clone at head. */
1254 if (clone) {
1255 clone->next = list;
1256 skb_shinfo(skb)->frag_list = clone;
1259 /* Success! Now we may commit changes to skb data. */
1261 pull_pages:
1262 eat = delta;
1263 k = 0;
1264 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1265 if (skb_shinfo(skb)->frags[i].size <= eat) {
1266 put_page(skb_shinfo(skb)->frags[i].page);
1267 eat -= skb_shinfo(skb)->frags[i].size;
1268 } else {
1269 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1270 if (eat) {
1271 skb_shinfo(skb)->frags[k].page_offset += eat;
1272 skb_shinfo(skb)->frags[k].size -= eat;
1273 eat = 0;
1275 k++;
1278 skb_shinfo(skb)->nr_frags = k;
1280 skb->tail += delta;
1281 skb->data_len -= delta;
1283 return skb_tail_pointer(skb);
1285 EXPORT_SYMBOL(__pskb_pull_tail);
1287 /* Copy some data bits from skb to kernel buffer. */
1289 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1291 int start = skb_headlen(skb);
1292 struct sk_buff *frag_iter;
1293 int i, copy;
1295 if (offset > (int)skb->len - len)
1296 goto fault;
1298 /* Copy header. */
1299 if ((copy = start - offset) > 0) {
1300 if (copy > len)
1301 copy = len;
1302 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1303 if ((len -= copy) == 0)
1304 return 0;
1305 offset += copy;
1306 to += copy;
1309 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1310 int end;
1312 WARN_ON(start > offset + len);
1314 end = start + skb_shinfo(skb)->frags[i].size;
1315 if ((copy = end - offset) > 0) {
1316 u8 *vaddr;
1318 if (copy > len)
1319 copy = len;
1321 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1322 memcpy(to,
1323 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1324 offset - start, copy);
1325 kunmap_skb_frag(vaddr);
1327 if ((len -= copy) == 0)
1328 return 0;
1329 offset += copy;
1330 to += copy;
1332 start = end;
1335 skb_walk_frags(skb, frag_iter) {
1336 int end;
1338 WARN_ON(start > offset + len);
1340 end = start + frag_iter->len;
1341 if ((copy = end - offset) > 0) {
1342 if (copy > len)
1343 copy = len;
1344 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1345 goto fault;
1346 if ((len -= copy) == 0)
1347 return 0;
1348 offset += copy;
1349 to += copy;
1351 start = end;
1353 if (!len)
1354 return 0;
1356 fault:
1357 return -EFAULT;
1359 EXPORT_SYMBOL(skb_copy_bits);
1362 * Callback from splice_to_pipe(), if we need to release some pages
1363 * at the end of the spd in case we error'ed out in filling the pipe.
1365 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1367 put_page(spd->pages[i]);
1370 static inline struct page *linear_to_page(struct page *page, unsigned int *len,
1371 unsigned int *offset,
1372 struct sk_buff *skb, struct sock *sk)
1374 struct page *p = sk->sk_sndmsg_page;
1375 unsigned int off;
1377 if (!p) {
1378 new_page:
1379 p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
1380 if (!p)
1381 return NULL;
1383 off = sk->sk_sndmsg_off = 0;
1384 /* hold one ref to this page until it's full */
1385 } else {
1386 unsigned int mlen;
1388 off = sk->sk_sndmsg_off;
1389 mlen = PAGE_SIZE - off;
1390 if (mlen < 64 && mlen < *len) {
1391 put_page(p);
1392 goto new_page;
1395 *len = min_t(unsigned int, *len, mlen);
1398 memcpy(page_address(p) + off, page_address(page) + *offset, *len);
1399 sk->sk_sndmsg_off += *len;
1400 *offset = off;
1401 get_page(p);
1403 return p;
1407 * Fill page/offset/length into spd, if it can hold more pages.
1409 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1410 unsigned int *len, unsigned int offset,
1411 struct sk_buff *skb, int linear,
1412 struct sock *sk)
1414 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1415 return 1;
1417 if (linear) {
1418 page = linear_to_page(page, len, &offset, skb, sk);
1419 if (!page)
1420 return 1;
1421 } else
1422 get_page(page);
1424 spd->pages[spd->nr_pages] = page;
1425 spd->partial[spd->nr_pages].len = *len;
1426 spd->partial[spd->nr_pages].offset = offset;
1427 spd->nr_pages++;
1429 return 0;
1432 static inline void __segment_seek(struct page **page, unsigned int *poff,
1433 unsigned int *plen, unsigned int off)
1435 unsigned long n;
1437 *poff += off;
1438 n = *poff / PAGE_SIZE;
1439 if (n)
1440 *page = nth_page(*page, n);
1442 *poff = *poff % PAGE_SIZE;
1443 *plen -= off;
1446 static inline int __splice_segment(struct page *page, unsigned int poff,
1447 unsigned int plen, unsigned int *off,
1448 unsigned int *len, struct sk_buff *skb,
1449 struct splice_pipe_desc *spd, int linear,
1450 struct sock *sk)
1452 if (!*len)
1453 return 1;
1455 /* skip this segment if already processed */
1456 if (*off >= plen) {
1457 *off -= plen;
1458 return 0;
1461 /* ignore any bits we already processed */
1462 if (*off) {
1463 __segment_seek(&page, &poff, &plen, *off);
1464 *off = 0;
1467 do {
1468 unsigned int flen = min(*len, plen);
1470 /* the linear region may spread across several pages */
1471 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1473 if (spd_fill_page(spd, page, &flen, poff, skb, linear, sk))
1474 return 1;
1476 __segment_seek(&page, &poff, &plen, flen);
1477 *len -= flen;
1479 } while (*len && plen);
1481 return 0;
1485 * Map linear and fragment data from the skb to spd. It reports failure if the
1486 * pipe is full or if we already spliced the requested length.
1488 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1489 unsigned int *len, struct splice_pipe_desc *spd,
1490 struct sock *sk)
1492 int seg;
1495 * map the linear part
1497 if (__splice_segment(virt_to_page(skb->data),
1498 (unsigned long) skb->data & (PAGE_SIZE - 1),
1499 skb_headlen(skb),
1500 offset, len, skb, spd, 1, sk))
1501 return 1;
1504 * then map the fragments
1506 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1507 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1509 if (__splice_segment(f->page, f->page_offset, f->size,
1510 offset, len, skb, spd, 0, sk))
1511 return 1;
1514 return 0;
1518 * Map data from the skb to a pipe. Should handle both the linear part,
1519 * the fragments, and the frag list. It does NOT handle frag lists within
1520 * the frag list, if such a thing exists. We'd probably need to recurse to
1521 * handle that cleanly.
1523 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1524 struct pipe_inode_info *pipe, unsigned int tlen,
1525 unsigned int flags)
1527 struct partial_page partial[PIPE_BUFFERS];
1528 struct page *pages[PIPE_BUFFERS];
1529 struct splice_pipe_desc spd = {
1530 .pages = pages,
1531 .partial = partial,
1532 .flags = flags,
1533 .ops = &sock_pipe_buf_ops,
1534 .spd_release = sock_spd_release,
1536 struct sk_buff *frag_iter;
1537 struct sock *sk = skb->sk;
1540 * __skb_splice_bits() only fails if the output has no room left,
1541 * so no point in going over the frag_list for the error case.
1543 if (__skb_splice_bits(skb, &offset, &tlen, &spd, sk))
1544 goto done;
1545 else if (!tlen)
1546 goto done;
1549 * now see if we have a frag_list to map
1551 skb_walk_frags(skb, frag_iter) {
1552 if (!tlen)
1553 break;
1554 if (__skb_splice_bits(frag_iter, &offset, &tlen, &spd, sk))
1555 break;
1558 done:
1559 if (spd.nr_pages) {
1560 int ret;
1563 * Drop the socket lock, otherwise we have reverse
1564 * locking dependencies between sk_lock and i_mutex
1565 * here as compared to sendfile(). We enter here
1566 * with the socket lock held, and splice_to_pipe() will
1567 * grab the pipe inode lock. For sendfile() emulation,
1568 * we call into ->sendpage() with the i_mutex lock held
1569 * and networking will grab the socket lock.
1571 release_sock(sk);
1572 ret = splice_to_pipe(pipe, &spd);
1573 lock_sock(sk);
1574 return ret;
1577 return 0;
1581 * skb_store_bits - store bits from kernel buffer to skb
1582 * @skb: destination buffer
1583 * @offset: offset in destination
1584 * @from: source buffer
1585 * @len: number of bytes to copy
1587 * Copy the specified number of bytes from the source buffer to the
1588 * destination skb. This function handles all the messy bits of
1589 * traversing fragment lists and such.
1592 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1594 int start = skb_headlen(skb);
1595 struct sk_buff *frag_iter;
1596 int i, copy;
1598 if (offset > (int)skb->len - len)
1599 goto fault;
1601 if ((copy = start - offset) > 0) {
1602 if (copy > len)
1603 copy = len;
1604 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1605 if ((len -= copy) == 0)
1606 return 0;
1607 offset += copy;
1608 from += copy;
1611 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1612 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1613 int end;
1615 WARN_ON(start > offset + len);
1617 end = start + frag->size;
1618 if ((copy = end - offset) > 0) {
1619 u8 *vaddr;
1621 if (copy > len)
1622 copy = len;
1624 vaddr = kmap_skb_frag(frag);
1625 memcpy(vaddr + frag->page_offset + offset - start,
1626 from, copy);
1627 kunmap_skb_frag(vaddr);
1629 if ((len -= copy) == 0)
1630 return 0;
1631 offset += copy;
1632 from += copy;
1634 start = end;
1637 skb_walk_frags(skb, frag_iter) {
1638 int end;
1640 WARN_ON(start > offset + len);
1642 end = start + frag_iter->len;
1643 if ((copy = end - offset) > 0) {
1644 if (copy > len)
1645 copy = len;
1646 if (skb_store_bits(frag_iter, offset - start,
1647 from, copy))
1648 goto fault;
1649 if ((len -= copy) == 0)
1650 return 0;
1651 offset += copy;
1652 from += copy;
1654 start = end;
1656 if (!len)
1657 return 0;
1659 fault:
1660 return -EFAULT;
1662 EXPORT_SYMBOL(skb_store_bits);
1664 /* Checksum skb data. */
1666 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1667 int len, __wsum csum)
1669 int start = skb_headlen(skb);
1670 int i, copy = start - offset;
1671 struct sk_buff *frag_iter;
1672 int pos = 0;
1674 /* Checksum header. */
1675 if (copy > 0) {
1676 if (copy > len)
1677 copy = len;
1678 csum = csum_partial(skb->data + offset, copy, csum);
1679 if ((len -= copy) == 0)
1680 return csum;
1681 offset += copy;
1682 pos = copy;
1685 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1686 int end;
1688 WARN_ON(start > offset + len);
1690 end = start + skb_shinfo(skb)->frags[i].size;
1691 if ((copy = end - offset) > 0) {
1692 __wsum csum2;
1693 u8 *vaddr;
1694 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1696 if (copy > len)
1697 copy = len;
1698 vaddr = kmap_skb_frag(frag);
1699 csum2 = csum_partial(vaddr + frag->page_offset +
1700 offset - start, copy, 0);
1701 kunmap_skb_frag(vaddr);
1702 csum = csum_block_add(csum, csum2, pos);
1703 if (!(len -= copy))
1704 return csum;
1705 offset += copy;
1706 pos += copy;
1708 start = end;
1711 skb_walk_frags(skb, frag_iter) {
1712 int end;
1714 WARN_ON(start > offset + len);
1716 end = start + frag_iter->len;
1717 if ((copy = end - offset) > 0) {
1718 __wsum csum2;
1719 if (copy > len)
1720 copy = len;
1721 csum2 = skb_checksum(frag_iter, offset - start,
1722 copy, 0);
1723 csum = csum_block_add(csum, csum2, pos);
1724 if ((len -= copy) == 0)
1725 return csum;
1726 offset += copy;
1727 pos += copy;
1729 start = end;
1731 BUG_ON(len);
1733 return csum;
1735 EXPORT_SYMBOL(skb_checksum);
1737 /* Both of above in one bottle. */
1739 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1740 u8 *to, int len, __wsum csum)
1742 int start = skb_headlen(skb);
1743 int i, copy = start - offset;
1744 struct sk_buff *frag_iter;
1745 int pos = 0;
1747 /* Copy header. */
1748 if (copy > 0) {
1749 if (copy > len)
1750 copy = len;
1751 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1752 copy, csum);
1753 if ((len -= copy) == 0)
1754 return csum;
1755 offset += copy;
1756 to += copy;
1757 pos = copy;
1760 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1761 int end;
1763 WARN_ON(start > offset + len);
1765 end = start + skb_shinfo(skb)->frags[i].size;
1766 if ((copy = end - offset) > 0) {
1767 __wsum csum2;
1768 u8 *vaddr;
1769 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1771 if (copy > len)
1772 copy = len;
1773 vaddr = kmap_skb_frag(frag);
1774 csum2 = csum_partial_copy_nocheck(vaddr +
1775 frag->page_offset +
1776 offset - start, to,
1777 copy, 0);
1778 kunmap_skb_frag(vaddr);
1779 csum = csum_block_add(csum, csum2, pos);
1780 if (!(len -= copy))
1781 return csum;
1782 offset += copy;
1783 to += copy;
1784 pos += copy;
1786 start = end;
1789 skb_walk_frags(skb, frag_iter) {
1790 __wsum csum2;
1791 int end;
1793 WARN_ON(start > offset + len);
1795 end = start + frag_iter->len;
1796 if ((copy = end - offset) > 0) {
1797 if (copy > len)
1798 copy = len;
1799 csum2 = skb_copy_and_csum_bits(frag_iter,
1800 offset - start,
1801 to, copy, 0);
1802 csum = csum_block_add(csum, csum2, pos);
1803 if ((len -= copy) == 0)
1804 return csum;
1805 offset += copy;
1806 to += copy;
1807 pos += copy;
1809 start = end;
1811 BUG_ON(len);
1812 return csum;
1814 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1816 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1818 __wsum csum;
1819 long csstart;
1821 if (skb->ip_summed == CHECKSUM_PARTIAL)
1822 csstart = skb->csum_start - skb_headroom(skb);
1823 else
1824 csstart = skb_headlen(skb);
1826 BUG_ON(csstart > skb_headlen(skb));
1828 skb_copy_from_linear_data(skb, to, csstart);
1830 csum = 0;
1831 if (csstart != skb->len)
1832 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1833 skb->len - csstart, 0);
1835 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1836 long csstuff = csstart + skb->csum_offset;
1838 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1841 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1844 * skb_dequeue - remove from the head of the queue
1845 * @list: list to dequeue from
1847 * Remove the head of the list. The list lock is taken so the function
1848 * may be used safely with other locking list functions. The head item is
1849 * returned or %NULL if the list is empty.
1852 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1854 unsigned long flags;
1855 struct sk_buff *result;
1857 spin_lock_irqsave(&list->lock, flags);
1858 result = __skb_dequeue(list);
1859 spin_unlock_irqrestore(&list->lock, flags);
1860 return result;
1862 EXPORT_SYMBOL(skb_dequeue);
1865 * skb_dequeue_tail - remove from the tail of the queue
1866 * @list: list to dequeue from
1868 * Remove the tail of the list. The list lock is taken so the function
1869 * may be used safely with other locking list functions. The tail item is
1870 * returned or %NULL if the list is empty.
1872 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1874 unsigned long flags;
1875 struct sk_buff *result;
1877 spin_lock_irqsave(&list->lock, flags);
1878 result = __skb_dequeue_tail(list);
1879 spin_unlock_irqrestore(&list->lock, flags);
1880 return result;
1882 EXPORT_SYMBOL(skb_dequeue_tail);
1885 * skb_queue_purge - empty a list
1886 * @list: list to empty
1888 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1889 * the list and one reference dropped. This function takes the list
1890 * lock and is atomic with respect to other list locking functions.
1892 void skb_queue_purge(struct sk_buff_head *list)
1894 struct sk_buff *skb;
1895 while ((skb = skb_dequeue(list)) != NULL)
1896 kfree_skb(skb);
1898 EXPORT_SYMBOL(skb_queue_purge);
1901 * skb_queue_head - queue a buffer at the list head
1902 * @list: list to use
1903 * @newsk: buffer to queue
1905 * Queue a buffer at the start of the list. This function takes the
1906 * list lock and can be used safely with other locking &sk_buff functions
1907 * safely.
1909 * A buffer cannot be placed on two lists at the same time.
1911 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1913 unsigned long flags;
1915 spin_lock_irqsave(&list->lock, flags);
1916 __skb_queue_head(list, newsk);
1917 spin_unlock_irqrestore(&list->lock, flags);
1919 EXPORT_SYMBOL(skb_queue_head);
1922 * skb_queue_tail - queue a buffer at the list tail
1923 * @list: list to use
1924 * @newsk: buffer to queue
1926 * Queue a buffer at the tail of the list. This function takes the
1927 * list lock and can be used safely with other locking &sk_buff functions
1928 * safely.
1930 * A buffer cannot be placed on two lists at the same time.
1932 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1934 unsigned long flags;
1936 spin_lock_irqsave(&list->lock, flags);
1937 __skb_queue_tail(list, newsk);
1938 spin_unlock_irqrestore(&list->lock, flags);
1940 EXPORT_SYMBOL(skb_queue_tail);
1943 * skb_unlink - remove a buffer from a list
1944 * @skb: buffer to remove
1945 * @list: list to use
1947 * Remove a packet from a list. The list locks are taken and this
1948 * function is atomic with respect to other list locked calls
1950 * You must know what list the SKB is on.
1952 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1954 unsigned long flags;
1956 spin_lock_irqsave(&list->lock, flags);
1957 __skb_unlink(skb, list);
1958 spin_unlock_irqrestore(&list->lock, flags);
1960 EXPORT_SYMBOL(skb_unlink);
1963 * skb_append - append a buffer
1964 * @old: buffer to insert after
1965 * @newsk: buffer to insert
1966 * @list: list to use
1968 * Place a packet after a given packet in a list. The list locks are taken
1969 * and this function is atomic with respect to other list locked calls.
1970 * A buffer cannot be placed on two lists at the same time.
1972 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1974 unsigned long flags;
1976 spin_lock_irqsave(&list->lock, flags);
1977 __skb_queue_after(list, old, newsk);
1978 spin_unlock_irqrestore(&list->lock, flags);
1980 EXPORT_SYMBOL(skb_append);
1983 * skb_insert - insert a buffer
1984 * @old: buffer to insert before
1985 * @newsk: buffer to insert
1986 * @list: list to use
1988 * Place a packet before a given packet in a list. The list locks are
1989 * taken and this function is atomic with respect to other list locked
1990 * calls.
1992 * A buffer cannot be placed on two lists at the same time.
1994 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1996 unsigned long flags;
1998 spin_lock_irqsave(&list->lock, flags);
1999 __skb_insert(newsk, old->prev, old, list);
2000 spin_unlock_irqrestore(&list->lock, flags);
2002 EXPORT_SYMBOL(skb_insert);
2004 static inline void skb_split_inside_header(struct sk_buff *skb,
2005 struct sk_buff* skb1,
2006 const u32 len, const int pos)
2008 int i;
2010 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2011 pos - len);
2012 /* And move data appendix as is. */
2013 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2014 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2016 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2017 skb_shinfo(skb)->nr_frags = 0;
2018 skb1->data_len = skb->data_len;
2019 skb1->len += skb1->data_len;
2020 skb->data_len = 0;
2021 skb->len = len;
2022 skb_set_tail_pointer(skb, len);
2025 static inline void skb_split_no_header(struct sk_buff *skb,
2026 struct sk_buff* skb1,
2027 const u32 len, int pos)
2029 int i, k = 0;
2030 const int nfrags = skb_shinfo(skb)->nr_frags;
2032 skb_shinfo(skb)->nr_frags = 0;
2033 skb1->len = skb1->data_len = skb->len - len;
2034 skb->len = len;
2035 skb->data_len = len - pos;
2037 for (i = 0; i < nfrags; i++) {
2038 int size = skb_shinfo(skb)->frags[i].size;
2040 if (pos + size > len) {
2041 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2043 if (pos < len) {
2044 /* Split frag.
2045 * We have two variants in this case:
2046 * 1. Move all the frag to the second
2047 * part, if it is possible. F.e.
2048 * this approach is mandatory for TUX,
2049 * where splitting is expensive.
2050 * 2. Split is accurately. We make this.
2052 get_page(skb_shinfo(skb)->frags[i].page);
2053 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2054 skb_shinfo(skb1)->frags[0].size -= len - pos;
2055 skb_shinfo(skb)->frags[i].size = len - pos;
2056 skb_shinfo(skb)->nr_frags++;
2058 k++;
2059 } else
2060 skb_shinfo(skb)->nr_frags++;
2061 pos += size;
2063 skb_shinfo(skb1)->nr_frags = k;
2067 * skb_split - Split fragmented skb to two parts at length len.
2068 * @skb: the buffer to split
2069 * @skb1: the buffer to receive the second part
2070 * @len: new length for skb
2072 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2074 int pos = skb_headlen(skb);
2076 if (len < pos) /* Split line is inside header. */
2077 skb_split_inside_header(skb, skb1, len, pos);
2078 else /* Second chunk has no header, nothing to copy. */
2079 skb_split_no_header(skb, skb1, len, pos);
2081 EXPORT_SYMBOL(skb_split);
2083 /* Shifting from/to a cloned skb is a no-go.
2085 * Caller cannot keep skb_shinfo related pointers past calling here!
2087 static int skb_prepare_for_shift(struct sk_buff *skb)
2089 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2093 * skb_shift - Shifts paged data partially from skb to another
2094 * @tgt: buffer into which tail data gets added
2095 * @skb: buffer from which the paged data comes from
2096 * @shiftlen: shift up to this many bytes
2098 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2099 * the length of the skb, from tgt to skb. Returns number bytes shifted.
2100 * It's up to caller to free skb if everything was shifted.
2102 * If @tgt runs out of frags, the whole operation is aborted.
2104 * Skb cannot include anything else but paged data while tgt is allowed
2105 * to have non-paged data as well.
2107 * TODO: full sized shift could be optimized but that would need
2108 * specialized skb free'er to handle frags without up-to-date nr_frags.
2110 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2112 int from, to, merge, todo;
2113 struct skb_frag_struct *fragfrom, *fragto;
2115 BUG_ON(shiftlen > skb->len);
2116 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2118 todo = shiftlen;
2119 from = 0;
2120 to = skb_shinfo(tgt)->nr_frags;
2121 fragfrom = &skb_shinfo(skb)->frags[from];
2123 /* Actual merge is delayed until the point when we know we can
2124 * commit all, so that we don't have to undo partial changes
2126 if (!to ||
2127 !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
2128 merge = -1;
2129 } else {
2130 merge = to - 1;
2132 todo -= fragfrom->size;
2133 if (todo < 0) {
2134 if (skb_prepare_for_shift(skb) ||
2135 skb_prepare_for_shift(tgt))
2136 return 0;
2138 /* All previous frag pointers might be stale! */
2139 fragfrom = &skb_shinfo(skb)->frags[from];
2140 fragto = &skb_shinfo(tgt)->frags[merge];
2142 fragto->size += shiftlen;
2143 fragfrom->size -= shiftlen;
2144 fragfrom->page_offset += shiftlen;
2146 goto onlymerged;
2149 from++;
2152 /* Skip full, not-fitting skb to avoid expensive operations */
2153 if ((shiftlen == skb->len) &&
2154 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2155 return 0;
2157 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2158 return 0;
2160 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2161 if (to == MAX_SKB_FRAGS)
2162 return 0;
2164 fragfrom = &skb_shinfo(skb)->frags[from];
2165 fragto = &skb_shinfo(tgt)->frags[to];
2167 if (todo >= fragfrom->size) {
2168 *fragto = *fragfrom;
2169 todo -= fragfrom->size;
2170 from++;
2171 to++;
2173 } else {
2174 get_page(fragfrom->page);
2175 fragto->page = fragfrom->page;
2176 fragto->page_offset = fragfrom->page_offset;
2177 fragto->size = todo;
2179 fragfrom->page_offset += todo;
2180 fragfrom->size -= todo;
2181 todo = 0;
2183 to++;
2184 break;
2188 /* Ready to "commit" this state change to tgt */
2189 skb_shinfo(tgt)->nr_frags = to;
2191 if (merge >= 0) {
2192 fragfrom = &skb_shinfo(skb)->frags[0];
2193 fragto = &skb_shinfo(tgt)->frags[merge];
2195 fragto->size += fragfrom->size;
2196 put_page(fragfrom->page);
2199 /* Reposition in the original skb */
2200 to = 0;
2201 while (from < skb_shinfo(skb)->nr_frags)
2202 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2203 skb_shinfo(skb)->nr_frags = to;
2205 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2207 onlymerged:
2208 /* Most likely the tgt won't ever need its checksum anymore, skb on
2209 * the other hand might need it if it needs to be resent
2211 tgt->ip_summed = CHECKSUM_PARTIAL;
2212 skb->ip_summed = CHECKSUM_PARTIAL;
2214 /* Yak, is it really working this way? Some helper please? */
2215 skb->len -= shiftlen;
2216 skb->data_len -= shiftlen;
2217 skb->truesize -= shiftlen;
2218 tgt->len += shiftlen;
2219 tgt->data_len += shiftlen;
2220 tgt->truesize += shiftlen;
2222 return shiftlen;
2226 * skb_prepare_seq_read - Prepare a sequential read of skb data
2227 * @skb: the buffer to read
2228 * @from: lower offset of data to be read
2229 * @to: upper offset of data to be read
2230 * @st: state variable
2232 * Initializes the specified state variable. Must be called before
2233 * invoking skb_seq_read() for the first time.
2235 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2236 unsigned int to, struct skb_seq_state *st)
2238 st->lower_offset = from;
2239 st->upper_offset = to;
2240 st->root_skb = st->cur_skb = skb;
2241 st->frag_idx = st->stepped_offset = 0;
2242 st->frag_data = NULL;
2244 EXPORT_SYMBOL(skb_prepare_seq_read);
2247 * skb_seq_read - Sequentially read skb data
2248 * @consumed: number of bytes consumed by the caller so far
2249 * @data: destination pointer for data to be returned
2250 * @st: state variable
2252 * Reads a block of skb data at &consumed relative to the
2253 * lower offset specified to skb_prepare_seq_read(). Assigns
2254 * the head of the data block to &data and returns the length
2255 * of the block or 0 if the end of the skb data or the upper
2256 * offset has been reached.
2258 * The caller is not required to consume all of the data
2259 * returned, i.e. &consumed is typically set to the number
2260 * of bytes already consumed and the next call to
2261 * skb_seq_read() will return the remaining part of the block.
2263 * Note 1: The size of each block of data returned can be arbitary,
2264 * this limitation is the cost for zerocopy seqeuental
2265 * reads of potentially non linear data.
2267 * Note 2: Fragment lists within fragments are not implemented
2268 * at the moment, state->root_skb could be replaced with
2269 * a stack for this purpose.
2271 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2272 struct skb_seq_state *st)
2274 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2275 skb_frag_t *frag;
2277 if (unlikely(abs_offset >= st->upper_offset))
2278 return 0;
2280 next_skb:
2281 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2283 if (abs_offset < block_limit && !st->frag_data) {
2284 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2285 return block_limit - abs_offset;
2288 if (st->frag_idx == 0 && !st->frag_data)
2289 st->stepped_offset += skb_headlen(st->cur_skb);
2291 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2292 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2293 block_limit = frag->size + st->stepped_offset;
2295 if (abs_offset < block_limit) {
2296 if (!st->frag_data)
2297 st->frag_data = kmap_skb_frag(frag);
2299 *data = (u8 *) st->frag_data + frag->page_offset +
2300 (abs_offset - st->stepped_offset);
2302 return block_limit - abs_offset;
2305 if (st->frag_data) {
2306 kunmap_skb_frag(st->frag_data);
2307 st->frag_data = NULL;
2310 st->frag_idx++;
2311 st->stepped_offset += frag->size;
2314 if (st->frag_data) {
2315 kunmap_skb_frag(st->frag_data);
2316 st->frag_data = NULL;
2319 if (st->root_skb == st->cur_skb && skb_has_frags(st->root_skb)) {
2320 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2321 st->frag_idx = 0;
2322 goto next_skb;
2323 } else if (st->cur_skb->next) {
2324 st->cur_skb = st->cur_skb->next;
2325 st->frag_idx = 0;
2326 goto next_skb;
2329 return 0;
2331 EXPORT_SYMBOL(skb_seq_read);
2334 * skb_abort_seq_read - Abort a sequential read of skb data
2335 * @st: state variable
2337 * Must be called if skb_seq_read() was not called until it
2338 * returned 0.
2340 void skb_abort_seq_read(struct skb_seq_state *st)
2342 if (st->frag_data)
2343 kunmap_skb_frag(st->frag_data);
2345 EXPORT_SYMBOL(skb_abort_seq_read);
2347 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2349 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2350 struct ts_config *conf,
2351 struct ts_state *state)
2353 return skb_seq_read(offset, text, TS_SKB_CB(state));
2356 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2358 skb_abort_seq_read(TS_SKB_CB(state));
2362 * skb_find_text - Find a text pattern in skb data
2363 * @skb: the buffer to look in
2364 * @from: search offset
2365 * @to: search limit
2366 * @config: textsearch configuration
2367 * @state: uninitialized textsearch state variable
2369 * Finds a pattern in the skb data according to the specified
2370 * textsearch configuration. Use textsearch_next() to retrieve
2371 * subsequent occurrences of the pattern. Returns the offset
2372 * to the first occurrence or UINT_MAX if no match was found.
2374 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2375 unsigned int to, struct ts_config *config,
2376 struct ts_state *state)
2378 unsigned int ret;
2380 config->get_next_block = skb_ts_get_next_block;
2381 config->finish = skb_ts_finish;
2383 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2385 ret = textsearch_find(config, state);
2386 return (ret <= to - from ? ret : UINT_MAX);
2388 EXPORT_SYMBOL(skb_find_text);
2391 * skb_append_datato_frags: - append the user data to a skb
2392 * @sk: sock structure
2393 * @skb: skb structure to be appened with user data.
2394 * @getfrag: call back function to be used for getting the user data
2395 * @from: pointer to user message iov
2396 * @length: length of the iov message
2398 * Description: This procedure append the user data in the fragment part
2399 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2401 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2402 int (*getfrag)(void *from, char *to, int offset,
2403 int len, int odd, struct sk_buff *skb),
2404 void *from, int length)
2406 int frg_cnt = 0;
2407 skb_frag_t *frag = NULL;
2408 struct page *page = NULL;
2409 int copy, left;
2410 int offset = 0;
2411 int ret;
2413 do {
2414 /* Return error if we don't have space for new frag */
2415 frg_cnt = skb_shinfo(skb)->nr_frags;
2416 if (frg_cnt >= MAX_SKB_FRAGS)
2417 return -EFAULT;
2419 /* allocate a new page for next frag */
2420 page = alloc_pages(sk->sk_allocation, 0);
2422 /* If alloc_page fails just return failure and caller will
2423 * free previous allocated pages by doing kfree_skb()
2425 if (page == NULL)
2426 return -ENOMEM;
2428 /* initialize the next frag */
2429 sk->sk_sndmsg_page = page;
2430 sk->sk_sndmsg_off = 0;
2431 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2432 skb->truesize += PAGE_SIZE;
2433 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2435 /* get the new initialized frag */
2436 frg_cnt = skb_shinfo(skb)->nr_frags;
2437 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2439 /* copy the user data to page */
2440 left = PAGE_SIZE - frag->page_offset;
2441 copy = (length > left)? left : length;
2443 ret = getfrag(from, (page_address(frag->page) +
2444 frag->page_offset + frag->size),
2445 offset, copy, 0, skb);
2446 if (ret < 0)
2447 return -EFAULT;
2449 /* copy was successful so update the size parameters */
2450 sk->sk_sndmsg_off += copy;
2451 frag->size += copy;
2452 skb->len += copy;
2453 skb->data_len += copy;
2454 offset += copy;
2455 length -= copy;
2457 } while (length > 0);
2459 return 0;
2461 EXPORT_SYMBOL(skb_append_datato_frags);
2464 * skb_pull_rcsum - pull skb and update receive checksum
2465 * @skb: buffer to update
2466 * @len: length of data pulled
2468 * This function performs an skb_pull on the packet and updates
2469 * the CHECKSUM_COMPLETE checksum. It should be used on
2470 * receive path processing instead of skb_pull unless you know
2471 * that the checksum difference is zero (e.g., a valid IP header)
2472 * or you are setting ip_summed to CHECKSUM_NONE.
2474 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2476 BUG_ON(len > skb->len);
2477 skb->len -= len;
2478 BUG_ON(skb->len < skb->data_len);
2479 skb_postpull_rcsum(skb, skb->data, len);
2480 return skb->data += len;
2483 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2486 * skb_segment - Perform protocol segmentation on skb.
2487 * @skb: buffer to segment
2488 * @features: features for the output path (see dev->features)
2490 * This function performs segmentation on the given skb. It returns
2491 * a pointer to the first in a list of new skbs for the segments.
2492 * In case of error it returns ERR_PTR(err).
2494 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2496 struct sk_buff *segs = NULL;
2497 struct sk_buff *tail = NULL;
2498 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2499 unsigned int mss = skb_shinfo(skb)->gso_size;
2500 unsigned int doffset = skb->data - skb_mac_header(skb);
2501 unsigned int offset = doffset;
2502 unsigned int headroom;
2503 unsigned int len;
2504 int sg = features & NETIF_F_SG;
2505 int nfrags = skb_shinfo(skb)->nr_frags;
2506 int err = -ENOMEM;
2507 int i = 0;
2508 int pos;
2510 __skb_push(skb, doffset);
2511 headroom = skb_headroom(skb);
2512 pos = skb_headlen(skb);
2514 do {
2515 struct sk_buff *nskb;
2516 skb_frag_t *frag;
2517 int hsize;
2518 int size;
2520 len = skb->len - offset;
2521 if (len > mss)
2522 len = mss;
2524 hsize = skb_headlen(skb) - offset;
2525 if (hsize < 0)
2526 hsize = 0;
2527 if (hsize > len || !sg)
2528 hsize = len;
2530 if (!hsize && i >= nfrags) {
2531 BUG_ON(fskb->len != len);
2533 pos += len;
2534 nskb = skb_clone(fskb, GFP_ATOMIC);
2535 fskb = fskb->next;
2537 if (unlikely(!nskb))
2538 goto err;
2540 hsize = skb_end_pointer(nskb) - nskb->head;
2541 if (skb_cow_head(nskb, doffset + headroom)) {
2542 kfree_skb(nskb);
2543 goto err;
2546 nskb->truesize += skb_end_pointer(nskb) - nskb->head -
2547 hsize;
2548 skb_release_head_state(nskb);
2549 __skb_push(nskb, doffset);
2550 } else {
2551 nskb = alloc_skb(hsize + doffset + headroom,
2552 GFP_ATOMIC);
2554 if (unlikely(!nskb))
2555 goto err;
2557 skb_reserve(nskb, headroom);
2558 __skb_put(nskb, doffset);
2561 if (segs)
2562 tail->next = nskb;
2563 else
2564 segs = nskb;
2565 tail = nskb;
2567 __copy_skb_header(nskb, skb);
2568 nskb->mac_len = skb->mac_len;
2570 skb_reset_mac_header(nskb);
2571 skb_set_network_header(nskb, skb->mac_len);
2572 nskb->transport_header = (nskb->network_header +
2573 skb_network_header_len(skb));
2574 skb_copy_from_linear_data(skb, nskb->data, doffset);
2576 if (fskb != skb_shinfo(skb)->frag_list)
2577 continue;
2579 if (!sg) {
2580 nskb->ip_summed = CHECKSUM_NONE;
2581 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2582 skb_put(nskb, len),
2583 len, 0);
2584 continue;
2587 frag = skb_shinfo(nskb)->frags;
2589 skb_copy_from_linear_data_offset(skb, offset,
2590 skb_put(nskb, hsize), hsize);
2592 while (pos < offset + len && i < nfrags) {
2593 *frag = skb_shinfo(skb)->frags[i];
2594 get_page(frag->page);
2595 size = frag->size;
2597 if (pos < offset) {
2598 frag->page_offset += offset - pos;
2599 frag->size -= offset - pos;
2602 skb_shinfo(nskb)->nr_frags++;
2604 if (pos + size <= offset + len) {
2605 i++;
2606 pos += size;
2607 } else {
2608 frag->size -= pos + size - (offset + len);
2609 goto skip_fraglist;
2612 frag++;
2615 if (pos < offset + len) {
2616 struct sk_buff *fskb2 = fskb;
2618 BUG_ON(pos + fskb->len != offset + len);
2620 pos += fskb->len;
2621 fskb = fskb->next;
2623 if (fskb2->next) {
2624 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2625 if (!fskb2)
2626 goto err;
2627 } else
2628 skb_get(fskb2);
2630 SKB_FRAG_ASSERT(nskb);
2631 skb_shinfo(nskb)->frag_list = fskb2;
2634 skip_fraglist:
2635 nskb->data_len = len - hsize;
2636 nskb->len += nskb->data_len;
2637 nskb->truesize += nskb->data_len;
2638 } while ((offset += len) < skb->len);
2640 return segs;
2642 err:
2643 while ((skb = segs)) {
2644 segs = skb->next;
2645 kfree_skb(skb);
2647 return ERR_PTR(err);
2649 EXPORT_SYMBOL_GPL(skb_segment);
2651 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2653 struct sk_buff *p = *head;
2654 struct sk_buff *nskb;
2655 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2656 struct skb_shared_info *pinfo = skb_shinfo(p);
2657 unsigned int headroom;
2658 unsigned int len = skb_gro_len(skb);
2659 unsigned int offset = skb_gro_offset(skb);
2660 unsigned int headlen = skb_headlen(skb);
2662 if (p->len + len >= 65536)
2663 return -E2BIG;
2665 if (pinfo->frag_list)
2666 goto merge;
2667 else if (headlen <= offset) {
2668 skb_frag_t *frag;
2669 skb_frag_t *frag2;
2670 int i = skbinfo->nr_frags;
2671 int nr_frags = pinfo->nr_frags + i;
2673 offset -= headlen;
2675 if (nr_frags > MAX_SKB_FRAGS)
2676 return -E2BIG;
2678 pinfo->nr_frags = nr_frags;
2679 skbinfo->nr_frags = 0;
2681 frag = pinfo->frags + nr_frags;
2682 frag2 = skbinfo->frags + i;
2683 do {
2684 *--frag = *--frag2;
2685 } while (--i);
2687 frag->page_offset += offset;
2688 frag->size -= offset;
2690 skb->truesize -= skb->data_len;
2691 skb->len -= skb->data_len;
2692 skb->data_len = 0;
2694 NAPI_GRO_CB(skb)->free = 1;
2695 goto done;
2698 headroom = skb_headroom(p);
2699 nskb = netdev_alloc_skb(p->dev, headroom + skb_gro_offset(p));
2700 if (unlikely(!nskb))
2701 return -ENOMEM;
2703 __copy_skb_header(nskb, p);
2704 nskb->mac_len = p->mac_len;
2706 skb_reserve(nskb, headroom);
2707 __skb_put(nskb, skb_gro_offset(p));
2709 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
2710 skb_set_network_header(nskb, skb_network_offset(p));
2711 skb_set_transport_header(nskb, skb_transport_offset(p));
2713 __skb_pull(p, skb_gro_offset(p));
2714 memcpy(skb_mac_header(nskb), skb_mac_header(p),
2715 p->data - skb_mac_header(p));
2717 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
2718 skb_shinfo(nskb)->frag_list = p;
2719 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
2720 skb_header_release(p);
2721 nskb->prev = p;
2723 nskb->data_len += p->len;
2724 nskb->truesize += p->len;
2725 nskb->len += p->len;
2727 *head = nskb;
2728 nskb->next = p->next;
2729 p->next = NULL;
2731 p = nskb;
2733 merge:
2734 if (offset > headlen) {
2735 skbinfo->frags[0].page_offset += offset - headlen;
2736 skbinfo->frags[0].size -= offset - headlen;
2737 offset = headlen;
2740 __skb_pull(skb, offset);
2742 p->prev->next = skb;
2743 p->prev = skb;
2744 skb_header_release(skb);
2746 done:
2747 NAPI_GRO_CB(p)->count++;
2748 p->data_len += len;
2749 p->truesize += len;
2750 p->len += len;
2752 NAPI_GRO_CB(skb)->same_flow = 1;
2753 return 0;
2755 EXPORT_SYMBOL_GPL(skb_gro_receive);
2757 void __init skb_init(void)
2759 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2760 sizeof(struct sk_buff),
2762 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2763 NULL);
2764 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2765 (2*sizeof(struct sk_buff)) +
2766 sizeof(atomic_t),
2768 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2769 NULL);
2773 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2774 * @skb: Socket buffer containing the buffers to be mapped
2775 * @sg: The scatter-gather list to map into
2776 * @offset: The offset into the buffer's contents to start mapping
2777 * @len: Length of buffer space to be mapped
2779 * Fill the specified scatter-gather list with mappings/pointers into a
2780 * region of the buffer space attached to a socket buffer.
2782 static int
2783 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2785 int start = skb_headlen(skb);
2786 int i, copy = start - offset;
2787 struct sk_buff *frag_iter;
2788 int elt = 0;
2790 if (copy > 0) {
2791 if (copy > len)
2792 copy = len;
2793 sg_set_buf(sg, skb->data + offset, copy);
2794 elt++;
2795 if ((len -= copy) == 0)
2796 return elt;
2797 offset += copy;
2800 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2801 int end;
2803 WARN_ON(start > offset + len);
2805 end = start + skb_shinfo(skb)->frags[i].size;
2806 if ((copy = end - offset) > 0) {
2807 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2809 if (copy > len)
2810 copy = len;
2811 sg_set_page(&sg[elt], frag->page, copy,
2812 frag->page_offset+offset-start);
2813 elt++;
2814 if (!(len -= copy))
2815 return elt;
2816 offset += copy;
2818 start = end;
2821 skb_walk_frags(skb, frag_iter) {
2822 int end;
2824 WARN_ON(start > offset + len);
2826 end = start + frag_iter->len;
2827 if ((copy = end - offset) > 0) {
2828 if (copy > len)
2829 copy = len;
2830 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
2831 copy);
2832 if ((len -= copy) == 0)
2833 return elt;
2834 offset += copy;
2836 start = end;
2838 BUG_ON(len);
2839 return elt;
2842 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2844 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2846 sg_mark_end(&sg[nsg - 1]);
2848 return nsg;
2850 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2853 * skb_cow_data - Check that a socket buffer's data buffers are writable
2854 * @skb: The socket buffer to check.
2855 * @tailbits: Amount of trailing space to be added
2856 * @trailer: Returned pointer to the skb where the @tailbits space begins
2858 * Make sure that the data buffers attached to a socket buffer are
2859 * writable. If they are not, private copies are made of the data buffers
2860 * and the socket buffer is set to use these instead.
2862 * If @tailbits is given, make sure that there is space to write @tailbits
2863 * bytes of data beyond current end of socket buffer. @trailer will be
2864 * set to point to the skb in which this space begins.
2866 * The number of scatterlist elements required to completely map the
2867 * COW'd and extended socket buffer will be returned.
2869 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2871 int copyflag;
2872 int elt;
2873 struct sk_buff *skb1, **skb_p;
2875 /* If skb is cloned or its head is paged, reallocate
2876 * head pulling out all the pages (pages are considered not writable
2877 * at the moment even if they are anonymous).
2879 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2880 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2881 return -ENOMEM;
2883 /* Easy case. Most of packets will go this way. */
2884 if (!skb_has_frags(skb)) {
2885 /* A little of trouble, not enough of space for trailer.
2886 * This should not happen, when stack is tuned to generate
2887 * good frames. OK, on miss we reallocate and reserve even more
2888 * space, 128 bytes is fair. */
2890 if (skb_tailroom(skb) < tailbits &&
2891 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2892 return -ENOMEM;
2894 /* Voila! */
2895 *trailer = skb;
2896 return 1;
2899 /* Misery. We are in troubles, going to mincer fragments... */
2901 elt = 1;
2902 skb_p = &skb_shinfo(skb)->frag_list;
2903 copyflag = 0;
2905 while ((skb1 = *skb_p) != NULL) {
2906 int ntail = 0;
2908 /* The fragment is partially pulled by someone,
2909 * this can happen on input. Copy it and everything
2910 * after it. */
2912 if (skb_shared(skb1))
2913 copyflag = 1;
2915 /* If the skb is the last, worry about trailer. */
2917 if (skb1->next == NULL && tailbits) {
2918 if (skb_shinfo(skb1)->nr_frags ||
2919 skb_has_frags(skb1) ||
2920 skb_tailroom(skb1) < tailbits)
2921 ntail = tailbits + 128;
2924 if (copyflag ||
2925 skb_cloned(skb1) ||
2926 ntail ||
2927 skb_shinfo(skb1)->nr_frags ||
2928 skb_has_frags(skb1)) {
2929 struct sk_buff *skb2;
2931 /* Fuck, we are miserable poor guys... */
2932 if (ntail == 0)
2933 skb2 = skb_copy(skb1, GFP_ATOMIC);
2934 else
2935 skb2 = skb_copy_expand(skb1,
2936 skb_headroom(skb1),
2937 ntail,
2938 GFP_ATOMIC);
2939 if (unlikely(skb2 == NULL))
2940 return -ENOMEM;
2942 if (skb1->sk)
2943 skb_set_owner_w(skb2, skb1->sk);
2945 /* Looking around. Are we still alive?
2946 * OK, link new skb, drop old one */
2948 skb2->next = skb1->next;
2949 *skb_p = skb2;
2950 kfree_skb(skb1);
2951 skb1 = skb2;
2953 elt++;
2954 *trailer = skb1;
2955 skb_p = &skb1->next;
2958 return elt;
2960 EXPORT_SYMBOL_GPL(skb_cow_data);
2962 void skb_tstamp_tx(struct sk_buff *orig_skb,
2963 struct skb_shared_hwtstamps *hwtstamps)
2965 struct sock *sk = orig_skb->sk;
2966 struct sock_exterr_skb *serr;
2967 struct sk_buff *skb;
2968 int err;
2970 if (!sk)
2971 return;
2973 skb = skb_clone(orig_skb, GFP_ATOMIC);
2974 if (!skb)
2975 return;
2977 if (hwtstamps) {
2978 *skb_hwtstamps(skb) =
2979 *hwtstamps;
2980 } else {
2982 * no hardware time stamps available,
2983 * so keep the skb_shared_tx and only
2984 * store software time stamp
2986 skb->tstamp = ktime_get_real();
2989 serr = SKB_EXT_ERR(skb);
2990 memset(serr, 0, sizeof(*serr));
2991 serr->ee.ee_errno = ENOMSG;
2992 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
2993 err = sock_queue_err_skb(sk, skb);
2994 if (err)
2995 kfree_skb(skb);
2997 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3001 * skb_partial_csum_set - set up and verify partial csum values for packet
3002 * @skb: the skb to set
3003 * @start: the number of bytes after skb->data to start checksumming.
3004 * @off: the offset from start to place the checksum.
3006 * For untrusted partially-checksummed packets, we need to make sure the values
3007 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3009 * This function checks and sets those values and skb->ip_summed: if this
3010 * returns false you should drop the packet.
3012 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3014 if (unlikely(start > skb_headlen(skb)) ||
3015 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3016 if (net_ratelimit())
3017 printk(KERN_WARNING
3018 "bad partial csum: csum=%u/%u len=%u\n",
3019 start, off, skb_headlen(skb));
3020 return false;
3022 skb->ip_summed = CHECKSUM_PARTIAL;
3023 skb->csum_start = skb_headroom(skb) + start;
3024 skb->csum_offset = off;
3025 return true;
3027 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3029 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3031 if (net_ratelimit())
3032 pr_warning("%s: received packets cannot be forwarded"
3033 " while LRO is enabled\n", skb->dev->name);
3035 EXPORT_SYMBOL(__skb_warn_lro_forwarding);