Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Routines having to do with the 'struct sk_buff' memory handlers. |
| 3 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 4 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Florian La Roche <rzsfl@rz.uni-sb.de> |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 22 | * |
| 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). |
| 28 | * |
| 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. |
| 33 | */ |
| 34 | |
| 35 | /* |
| 36 | * The functions in this file will not compile correctly with gcc 2.4.x |
| 37 | */ |
| 38 | |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 39 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/module.h> |
| 42 | #include <linux/types.h> |
| 43 | #include <linux/kernel.h> |
Vegard Nossum | fe55f6d | 2008-08-30 12:16:35 +0200 | [diff] [blame] | 44 | #include <linux/kmemcheck.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/mm.h> |
| 46 | #include <linux/interrupt.h> |
| 47 | #include <linux/in.h> |
| 48 | #include <linux/inet.h> |
| 49 | #include <linux/slab.h> |
| 50 | #include <linux/netdevice.h> |
| 51 | #ifdef CONFIG_NET_CLS_ACT |
| 52 | #include <net/pkt_sched.h> |
| 53 | #endif |
| 54 | #include <linux/string.h> |
| 55 | #include <linux/skbuff.h> |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 56 | #include <linux/splice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <linux/cache.h> |
| 58 | #include <linux/rtnetlink.h> |
| 59 | #include <linux/init.h> |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 60 | #include <linux/scatterlist.h> |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 61 | #include <linux/errqueue.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 62 | #include <linux/prefetch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | #include <net/protocol.h> |
| 65 | #include <net/dst.h> |
| 66 | #include <net/sock.h> |
| 67 | #include <net/checksum.h> |
| 68 | #include <net/xfrm.h> |
| 69 | |
| 70 | #include <asm/uaccess.h> |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 71 | #include <trace/events/skb.h> |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 72 | #include <linux/highmem.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 73 | |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 74 | struct kmem_cache *skbuff_head_cache __read_mostly; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 75 | static struct kmem_cache *skbuff_fclone_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 77 | static void sock_pipe_buf_release(struct pipe_inode_info *pipe, |
| 78 | struct pipe_buffer *buf) |
| 79 | { |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 80 | put_page(buf->page); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void sock_pipe_buf_get(struct pipe_inode_info *pipe, |
| 84 | struct pipe_buffer *buf) |
| 85 | { |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 86 | get_page(buf->page); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, |
| 90 | struct pipe_buffer *buf) |
| 91 | { |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* Pipe buffer operations for a socket. */ |
Alexey Dobriyan | 28dfef8 | 2009-12-15 16:46:48 -0800 | [diff] [blame] | 97 | static const struct pipe_buf_operations sock_pipe_buf_ops = { |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 98 | .can_merge = 0, |
| 99 | .map = generic_pipe_buf_map, |
| 100 | .unmap = generic_pipe_buf_unmap, |
| 101 | .confirm = generic_pipe_buf_confirm, |
| 102 | .release = sock_pipe_buf_release, |
| 103 | .steal = sock_pipe_buf_steal, |
| 104 | .get = sock_pipe_buf_get, |
| 105 | }; |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /* |
| 108 | * Keep out-of-line to prevent kernel bloat. |
| 109 | * __builtin_return_address is not used because it is not always |
| 110 | * reliable. |
| 111 | */ |
| 112 | |
| 113 | /** |
| 114 | * skb_over_panic - private function |
| 115 | * @skb: buffer |
| 116 | * @sz: size |
| 117 | * @here: address |
| 118 | * |
| 119 | * Out of line support code for skb_put(). Not user callable. |
| 120 | */ |
Rami Rosen | ccb7c77 | 2010-04-20 22:39:53 -0700 | [diff] [blame] | 121 | static void skb_over_panic(struct sk_buff *skb, int sz, void *here) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 123 | pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", |
| 124 | __func__, here, skb->len, sz, skb->head, skb->data, |
| 125 | (unsigned long)skb->tail, (unsigned long)skb->end, |
| 126 | skb->dev ? skb->dev->name : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | BUG(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * skb_under_panic - private function |
| 132 | * @skb: buffer |
| 133 | * @sz: size |
| 134 | * @here: address |
| 135 | * |
| 136 | * Out of line support code for skb_push(). Not user callable. |
| 137 | */ |
| 138 | |
Rami Rosen | ccb7c77 | 2010-04-20 22:39:53 -0700 | [diff] [blame] | 139 | static void skb_under_panic(struct sk_buff *skb, int sz, void *here) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 141 | pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", |
| 142 | __func__, here, skb->len, sz, skb->head, skb->data, |
| 143 | (unsigned long)skb->tail, (unsigned long)skb->end, |
| 144 | skb->dev ? skb->dev->name : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | BUG(); |
| 146 | } |
| 147 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells |
| 151 | * the caller if emergency pfmemalloc reserves are being used. If it is and |
| 152 | * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves |
| 153 | * may be used. Otherwise, the packet data may be discarded until enough |
| 154 | * memory is free |
| 155 | */ |
| 156 | #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ |
| 157 | __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) |
| 158 | void *__kmalloc_reserve(size_t size, gfp_t flags, int node, unsigned long ip, |
| 159 | bool *pfmemalloc) |
| 160 | { |
| 161 | void *obj; |
| 162 | bool ret_pfmemalloc = false; |
| 163 | |
| 164 | /* |
| 165 | * Try a regular allocation, when that fails and we're not entitled |
| 166 | * to the reserves, fail. |
| 167 | */ |
| 168 | obj = kmalloc_node_track_caller(size, |
| 169 | flags | __GFP_NOMEMALLOC | __GFP_NOWARN, |
| 170 | node); |
| 171 | if (obj || !(gfp_pfmemalloc_allowed(flags))) |
| 172 | goto out; |
| 173 | |
| 174 | /* Try again but now we are using pfmemalloc reserves */ |
| 175 | ret_pfmemalloc = true; |
| 176 | obj = kmalloc_node_track_caller(size, flags, node); |
| 177 | |
| 178 | out: |
| 179 | if (pfmemalloc) |
| 180 | *pfmemalloc = ret_pfmemalloc; |
| 181 | |
| 182 | return obj; |
| 183 | } |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
| 186 | * 'private' fields and also do memory statistics to find all the |
| 187 | * [BEEP] leaks. |
| 188 | * |
| 189 | */ |
| 190 | |
| 191 | /** |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 192 | * __alloc_skb - allocate a network buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | * @size: size to allocate |
| 194 | * @gfp_mask: allocation mask |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 195 | * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache |
| 196 | * instead of head cache and allocate a cloned (child) skb. |
| 197 | * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for |
| 198 | * allocations in case the data is required for writeback |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 199 | * @node: numa node to allocate memory on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | * |
| 201 | * Allocate a new &sk_buff. The returned buffer has no headroom and a |
Ben Hutchings | 94b6042 | 2012-06-06 15:23:37 +0000 | [diff] [blame] | 202 | * tail room of at least size bytes. The object has a reference count |
| 203 | * of one. The return is the buffer. On a failure the return is %NULL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * |
| 205 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 206 | * %GFP_ATOMIC. |
| 207 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 208 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 209 | int flags, int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 211 | struct kmem_cache *cache; |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 212 | struct skb_shared_info *shinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | struct sk_buff *skb; |
| 214 | u8 *data; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 215 | bool pfmemalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 217 | cache = (flags & SKB_ALLOC_FCLONE) |
| 218 | ? skbuff_fclone_cache : skbuff_head_cache; |
| 219 | |
| 220 | if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) |
| 221 | gfp_mask |= __GFP_MEMALLOC; |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | /* Get the HEAD */ |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 224 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | if (!skb) |
| 226 | goto out; |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 227 | prefetchw(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 229 | /* We do our best to align skb_shared_info on a separate cache |
| 230 | * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives |
| 231 | * aligned memory blocks, unless SLUB/SLAB debug is enabled. |
| 232 | * Both skb->head and skb_shared_info are cache line aligned. |
| 233 | */ |
Tony Lindgren | bc417e3 | 2011-11-02 13:40:28 +0000 | [diff] [blame] | 234 | size = SKB_DATA_ALIGN(size); |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 235 | size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 236 | data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | if (!data) |
| 238 | goto nodata; |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 239 | /* kmalloc(size) might give us more room than requested. |
| 240 | * Put skb_shared_info exactly at the end of allocated zone, |
| 241 | * to allow max possible filling before reallocation. |
| 242 | */ |
| 243 | size = SKB_WITH_OVERHEAD(ksize(data)); |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 244 | prefetchw(data + size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Arnaldo Carvalho de Melo | ca0605a | 2007-03-19 10:48:59 -0300 | [diff] [blame] | 246 | /* |
Johannes Berg | c800578 | 2008-05-03 20:56:42 -0700 | [diff] [blame] | 247 | * Only clear those fields we need to clear, not those that we will |
| 248 | * actually initialise below. Hence, don't put any more fields after |
| 249 | * the tail pointer in struct sk_buff! |
Arnaldo Carvalho de Melo | ca0605a | 2007-03-19 10:48:59 -0300 | [diff] [blame] | 250 | */ |
| 251 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 252 | /* Account for allocated memory : skb + skb->head */ |
| 253 | skb->truesize = SKB_TRUESIZE(size); |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 254 | skb->pfmemalloc = pfmemalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | atomic_set(&skb->users, 1); |
| 256 | skb->head = data; |
| 257 | skb->data = data; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 258 | skb_reset_tail_pointer(skb); |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 259 | skb->end = skb->tail + size; |
Stephen Hemminger | 19633e1 | 2009-06-17 05:23:27 +0000 | [diff] [blame] | 260 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 261 | skb->mac_header = ~0U; |
| 262 | #endif |
| 263 | |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 264 | /* make sure we initialize shinfo sequentially */ |
| 265 | shinfo = skb_shinfo(skb); |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 266 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 267 | atomic_set(&shinfo->dataref, 1); |
Eric Dumazet | c2aa366 | 2011-01-25 23:18:38 +0000 | [diff] [blame] | 268 | kmemcheck_annotate_variable(shinfo->destructor_arg); |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 269 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 270 | if (flags & SKB_ALLOC_FCLONE) { |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 271 | struct sk_buff *child = skb + 1; |
| 272 | atomic_t *fclone_ref = (atomic_t *) (child + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Vegard Nossum | fe55f6d | 2008-08-30 12:16:35 +0200 | [diff] [blame] | 274 | kmemcheck_annotate_bitfield(child, flags1); |
| 275 | kmemcheck_annotate_bitfield(child, flags2); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 276 | skb->fclone = SKB_FCLONE_ORIG; |
| 277 | atomic_set(fclone_ref, 1); |
| 278 | |
| 279 | child->fclone = SKB_FCLONE_UNAVAILABLE; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 280 | child->pfmemalloc = pfmemalloc; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | out: |
| 283 | return skb; |
| 284 | nodata: |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 285 | kmem_cache_free(cache, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | skb = NULL; |
| 287 | goto out; |
| 288 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 289 | EXPORT_SYMBOL(__alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | /** |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 292 | * build_skb - build a network buffer |
| 293 | * @data: data buffer provided by caller |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 294 | * @frag_size: size of fragment, or 0 if head was kmalloced |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 295 | * |
| 296 | * Allocate a new &sk_buff. Caller provides space holding head and |
| 297 | * skb_shared_info. @data must have been allocated by kmalloc() |
| 298 | * The return is the new skb buffer. |
| 299 | * On a failure the return is %NULL, and @data is not freed. |
| 300 | * Notes : |
| 301 | * Before IO, driver allocates only data buffer where NIC put incoming frame |
| 302 | * Driver should add room at head (NET_SKB_PAD) and |
| 303 | * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) |
| 304 | * After IO, driver calls build_skb(), to allocate sk_buff and populate it |
| 305 | * before giving packet to stack. |
| 306 | * RX rings only contains data buffers, not full skbs. |
| 307 | */ |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 308 | struct sk_buff *build_skb(void *data, unsigned int frag_size) |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 309 | { |
| 310 | struct skb_shared_info *shinfo; |
| 311 | struct sk_buff *skb; |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 312 | unsigned int size = frag_size ? : ksize(data); |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 313 | |
| 314 | skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); |
| 315 | if (!skb) |
| 316 | return NULL; |
| 317 | |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 318 | size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 319 | |
| 320 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
| 321 | skb->truesize = SKB_TRUESIZE(size); |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 322 | skb->head_frag = frag_size != 0; |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 323 | atomic_set(&skb->users, 1); |
| 324 | skb->head = data; |
| 325 | skb->data = data; |
| 326 | skb_reset_tail_pointer(skb); |
| 327 | skb->end = skb->tail + size; |
| 328 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 329 | skb->mac_header = ~0U; |
| 330 | #endif |
| 331 | |
| 332 | /* make sure we initialize shinfo sequentially */ |
| 333 | shinfo = skb_shinfo(skb); |
| 334 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
| 335 | atomic_set(&shinfo->dataref, 1); |
| 336 | kmemcheck_annotate_variable(shinfo->destructor_arg); |
| 337 | |
| 338 | return skb; |
| 339 | } |
| 340 | EXPORT_SYMBOL(build_skb); |
| 341 | |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 342 | struct netdev_alloc_cache { |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 343 | struct page_frag frag; |
| 344 | /* we maintain a pagecount bias, so that we dont dirty cache line |
| 345 | * containing page->_count every time we allocate a fragment. |
| 346 | */ |
| 347 | unsigned int pagecnt_bias; |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 348 | }; |
| 349 | static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache); |
| 350 | |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 351 | #define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768) |
| 352 | #define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER) |
| 353 | #define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 354 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 355 | static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 356 | { |
| 357 | struct netdev_alloc_cache *nc; |
| 358 | void *data = NULL; |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 359 | int order; |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 360 | unsigned long flags; |
| 361 | |
| 362 | local_irq_save(flags); |
| 363 | nc = &__get_cpu_var(netdev_alloc_cache); |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 364 | if (unlikely(!nc->frag.page)) { |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 365 | refill: |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 366 | for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) { |
| 367 | gfp_t gfp = gfp_mask; |
| 368 | |
| 369 | if (order) |
| 370 | gfp |= __GFP_COMP | __GFP_NOWARN; |
| 371 | nc->frag.page = alloc_pages(gfp, order); |
| 372 | if (likely(nc->frag.page)) |
| 373 | break; |
| 374 | if (--order < 0) |
| 375 | goto end; |
| 376 | } |
| 377 | nc->frag.size = PAGE_SIZE << order; |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 378 | recycle: |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 379 | atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS); |
| 380 | nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS; |
| 381 | nc->frag.offset = 0; |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 382 | } |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 383 | |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 384 | if (nc->frag.offset + fragsz > nc->frag.size) { |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 385 | /* avoid unnecessary locked operations if possible */ |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 386 | if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) || |
| 387 | atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count)) |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 388 | goto recycle; |
| 389 | goto refill; |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 390 | } |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 391 | |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 392 | data = page_address(nc->frag.page) + nc->frag.offset; |
| 393 | nc->frag.offset += fragsz; |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 394 | nc->pagecnt_bias--; |
| 395 | end: |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 396 | local_irq_restore(flags); |
| 397 | return data; |
| 398 | } |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 399 | |
| 400 | /** |
| 401 | * netdev_alloc_frag - allocate a page fragment |
| 402 | * @fragsz: fragment size |
| 403 | * |
| 404 | * Allocates a frag from a page for receive buffer. |
| 405 | * Uses GFP_ATOMIC allocations. |
| 406 | */ |
| 407 | void *netdev_alloc_frag(unsigned int fragsz) |
| 408 | { |
| 409 | return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); |
| 410 | } |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 411 | EXPORT_SYMBOL(netdev_alloc_frag); |
| 412 | |
| 413 | /** |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 414 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device |
| 415 | * @dev: network device to receive on |
| 416 | * @length: length to allocate |
| 417 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
| 418 | * |
| 419 | * Allocate a new &sk_buff and assign it a usage count of one. The |
| 420 | * buffer has unspecified headroom built in. Users should allocate |
| 421 | * the headroom they think they need without accounting for the |
| 422 | * built in space. The built in space is used for optimisations. |
| 423 | * |
| 424 | * %NULL is returned if there is no free memory. |
| 425 | */ |
| 426 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 427 | unsigned int length, gfp_t gfp_mask) |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 428 | { |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 429 | struct sk_buff *skb = NULL; |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 430 | unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) + |
| 431 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 432 | |
Eric Dumazet | 310e158 | 2012-07-16 13:15:52 +0200 | [diff] [blame] | 433 | if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 434 | void *data; |
| 435 | |
| 436 | if (sk_memalloc_socks()) |
| 437 | gfp_mask |= __GFP_MEMALLOC; |
| 438 | |
| 439 | data = __netdev_alloc_frag(fragsz, gfp_mask); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 440 | |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 441 | if (likely(data)) { |
| 442 | skb = build_skb(data, fragsz); |
| 443 | if (unlikely(!skb)) |
| 444 | put_page(virt_to_head_page(data)); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 445 | } |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 446 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 447 | skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, |
| 448 | SKB_ALLOC_RX, NUMA_NO_NODE); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 449 | } |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 450 | if (likely(skb)) { |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 451 | skb_reserve(skb, NET_SKB_PAD); |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 452 | skb->dev = dev; |
| 453 | } |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 454 | return skb; |
| 455 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 456 | EXPORT_SYMBOL(__netdev_alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 458 | void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, |
Eric Dumazet | 50269e1 | 2012-03-23 23:59:33 +0000 | [diff] [blame] | 459 | int size, unsigned int truesize) |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 460 | { |
| 461 | skb_fill_page_desc(skb, i, page, off, size); |
| 462 | skb->len += size; |
| 463 | skb->data_len += size; |
Eric Dumazet | 50269e1 | 2012-03-23 23:59:33 +0000 | [diff] [blame] | 464 | skb->truesize += truesize; |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 465 | } |
| 466 | EXPORT_SYMBOL(skb_add_rx_frag); |
| 467 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 468 | static void skb_drop_list(struct sk_buff **listp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 470 | struct sk_buff *list = *listp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 472 | *listp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
| 474 | do { |
| 475 | struct sk_buff *this = list; |
| 476 | list = list->next; |
| 477 | kfree_skb(this); |
| 478 | } while (list); |
| 479 | } |
| 480 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 481 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
| 482 | { |
| 483 | skb_drop_list(&skb_shinfo(skb)->frag_list); |
| 484 | } |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | static void skb_clone_fraglist(struct sk_buff *skb) |
| 487 | { |
| 488 | struct sk_buff *list; |
| 489 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 490 | skb_walk_frags(skb, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | skb_get(list); |
| 492 | } |
| 493 | |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 494 | static void skb_free_head(struct sk_buff *skb) |
| 495 | { |
| 496 | if (skb->head_frag) |
| 497 | put_page(virt_to_head_page(skb->head)); |
| 498 | else |
| 499 | kfree(skb->head); |
| 500 | } |
| 501 | |
Adrian Bunk | 5bba171 | 2006-06-29 13:02:35 -0700 | [diff] [blame] | 502 | static void skb_release_data(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | { |
| 504 | if (!skb->cloned || |
| 505 | !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, |
| 506 | &skb_shinfo(skb)->dataref)) { |
| 507 | if (skb_shinfo(skb)->nr_frags) { |
| 508 | int i; |
| 509 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 510 | skb_frag_unref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 513 | /* |
| 514 | * If skb buf is from userspace, we need to notify the caller |
| 515 | * the lower device DMA has done; |
| 516 | */ |
| 517 | if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { |
| 518 | struct ubuf_info *uarg; |
| 519 | |
| 520 | uarg = skb_shinfo(skb)->destructor_arg; |
| 521 | if (uarg->callback) |
Michael S. Tsirkin | e19d676 | 2012-11-01 09:16:22 +0000 | [diff] [blame] | 522 | uarg->callback(uarg, true); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 523 | } |
| 524 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 525 | if (skb_has_frag_list(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | skb_drop_fraglist(skb); |
| 527 | |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 528 | skb_free_head(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Free an skbuff by memory without cleaning the state. |
| 534 | */ |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 535 | static void kfree_skbmem(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 537 | struct sk_buff *other; |
| 538 | atomic_t *fclone_ref; |
| 539 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 540 | switch (skb->fclone) { |
| 541 | case SKB_FCLONE_UNAVAILABLE: |
| 542 | kmem_cache_free(skbuff_head_cache, skb); |
| 543 | break; |
| 544 | |
| 545 | case SKB_FCLONE_ORIG: |
| 546 | fclone_ref = (atomic_t *) (skb + 2); |
| 547 | if (atomic_dec_and_test(fclone_ref)) |
| 548 | kmem_cache_free(skbuff_fclone_cache, skb); |
| 549 | break; |
| 550 | |
| 551 | case SKB_FCLONE_CLONE: |
| 552 | fclone_ref = (atomic_t *) (skb + 1); |
| 553 | other = skb - 1; |
| 554 | |
| 555 | /* The clone portion is available for |
| 556 | * fast-cloning again. |
| 557 | */ |
| 558 | skb->fclone = SKB_FCLONE_UNAVAILABLE; |
| 559 | |
| 560 | if (atomic_dec_and_test(fclone_ref)) |
| 561 | kmem_cache_free(skbuff_fclone_cache, other); |
| 562 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 563 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Lennert Buytenhek | 04a4bb5 | 2008-10-01 02:33:12 -0700 | [diff] [blame] | 566 | static void skb_release_head_state(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 568 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | #ifdef CONFIG_XFRM |
| 570 | secpath_put(skb->sp); |
| 571 | #endif |
Stephen Hemminger | 9c2b332 | 2005-04-19 22:39:42 -0700 | [diff] [blame] | 572 | if (skb->destructor) { |
| 573 | WARN_ON(in_irq()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | skb->destructor(skb); |
| 575 | } |
Igor Maravić | a3bf7ae | 2011-12-12 02:58:22 +0000 | [diff] [blame] | 576 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
Yasuyuki Kozakai | 5f79e0f | 2007-03-23 11:17:07 -0700 | [diff] [blame] | 577 | nf_conntrack_put(skb->nfct); |
KOVACS Krisztian | 2fc72c7 | 2011-01-12 20:25:08 +0100 | [diff] [blame] | 578 | #endif |
| 579 | #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 580 | nf_conntrack_put_reasm(skb->nfct_reasm); |
| 581 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 583 | nf_bridge_put(skb->nf_bridge); |
| 584 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | /* XXX: IS this still necessary? - JHS */ |
| 586 | #ifdef CONFIG_NET_SCHED |
| 587 | skb->tc_index = 0; |
| 588 | #ifdef CONFIG_NET_CLS_ACT |
| 589 | skb->tc_verd = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | #endif |
| 591 | #endif |
Lennert Buytenhek | 04a4bb5 | 2008-10-01 02:33:12 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* Free everything but the sk_buff shell. */ |
| 595 | static void skb_release_all(struct sk_buff *skb) |
| 596 | { |
| 597 | skb_release_head_state(skb); |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 598 | skb_release_data(skb); |
| 599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 601 | /** |
| 602 | * __kfree_skb - private function |
| 603 | * @skb: buffer |
| 604 | * |
| 605 | * Free an sk_buff. Release anything attached to the buffer. |
| 606 | * Clean the state. This is an internal helper function. Users should |
| 607 | * always call kfree_skb |
| 608 | */ |
| 609 | |
| 610 | void __kfree_skb(struct sk_buff *skb) |
| 611 | { |
| 612 | skb_release_all(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | kfree_skbmem(skb); |
| 614 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 615 | EXPORT_SYMBOL(__kfree_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
| 617 | /** |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 618 | * kfree_skb - free an sk_buff |
| 619 | * @skb: buffer to free |
| 620 | * |
| 621 | * Drop a reference to the buffer and free it if the usage count has |
| 622 | * hit zero. |
| 623 | */ |
| 624 | void kfree_skb(struct sk_buff *skb) |
| 625 | { |
| 626 | if (unlikely(!skb)) |
| 627 | return; |
| 628 | if (likely(atomic_read(&skb->users) == 1)) |
| 629 | smp_rmb(); |
| 630 | else if (likely(!atomic_dec_and_test(&skb->users))) |
| 631 | return; |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 632 | trace_kfree_skb(skb, __builtin_return_address(0)); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 633 | __kfree_skb(skb); |
| 634 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 635 | EXPORT_SYMBOL(kfree_skb); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 636 | |
Stephen Hemminger | d1a203e | 2008-11-01 21:01:09 -0700 | [diff] [blame] | 637 | /** |
Michael S. Tsirkin | 2512117 | 2012-11-01 09:16:28 +0000 | [diff] [blame] | 638 | * skb_tx_error - report an sk_buff xmit error |
| 639 | * @skb: buffer that triggered an error |
| 640 | * |
| 641 | * Report xmit error if a device callback is tracking this skb. |
| 642 | * skb must be freed afterwards. |
| 643 | */ |
| 644 | void skb_tx_error(struct sk_buff *skb) |
| 645 | { |
| 646 | if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { |
| 647 | struct ubuf_info *uarg; |
| 648 | |
| 649 | uarg = skb_shinfo(skb)->destructor_arg; |
| 650 | if (uarg->callback) |
| 651 | uarg->callback(uarg, false); |
| 652 | skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; |
| 653 | } |
| 654 | } |
| 655 | EXPORT_SYMBOL(skb_tx_error); |
| 656 | |
| 657 | /** |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 658 | * consume_skb - free an skbuff |
| 659 | * @skb: buffer to free |
| 660 | * |
| 661 | * Drop a ref to the buffer and free it if the usage count has hit zero |
| 662 | * Functions identically to kfree_skb, but kfree_skb assumes that the frame |
| 663 | * is being dropped after a failure and notes that |
| 664 | */ |
| 665 | void consume_skb(struct sk_buff *skb) |
| 666 | { |
| 667 | if (unlikely(!skb)) |
| 668 | return; |
| 669 | if (likely(atomic_read(&skb->users) == 1)) |
| 670 | smp_rmb(); |
| 671 | else if (likely(!atomic_dec_and_test(&skb->users))) |
| 672 | return; |
Koki Sanagi | 07dc22e | 2010-08-23 18:46:12 +0900 | [diff] [blame] | 673 | trace_consume_skb(skb); |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 674 | __kfree_skb(skb); |
| 675 | } |
| 676 | EXPORT_SYMBOL(consume_skb); |
| 677 | |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 678 | static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 679 | { |
| 680 | new->tstamp = old->tstamp; |
| 681 | new->dev = old->dev; |
| 682 | new->transport_header = old->transport_header; |
| 683 | new->network_header = old->network_header; |
| 684 | new->mac_header = old->mac_header; |
Joseph Gasparakis | 6a674e9 | 2012-12-07 14:14:14 +0000 | [diff] [blame] | 685 | new->inner_transport_header = old->inner_transport_header; |
| 686 | new->inner_network_header = old->inner_transport_header; |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 687 | skb_dst_copy(new, old); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 688 | new->rxhash = old->rxhash; |
Changli Gao | 6461be3 | 2011-08-19 04:44:18 +0000 | [diff] [blame] | 689 | new->ooo_okay = old->ooo_okay; |
Tom Herbert | bdeab99 | 2011-08-14 19:45:55 +0000 | [diff] [blame] | 690 | new->l4_rxhash = old->l4_rxhash; |
Ben Greear | 3bdc0eb | 2012-02-11 15:39:30 +0000 | [diff] [blame] | 691 | new->no_fcs = old->no_fcs; |
Joseph Gasparakis | 6a674e9 | 2012-12-07 14:14:14 +0000 | [diff] [blame] | 692 | new->encapsulation = old->encapsulation; |
Alexey Dobriyan | def8b4f | 2008-10-28 13:24:06 -0700 | [diff] [blame] | 693 | #ifdef CONFIG_XFRM |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 694 | new->sp = secpath_get(old->sp); |
| 695 | #endif |
| 696 | memcpy(new->cb, old->cb, sizeof(old->cb)); |
Herbert Xu | 9bcb97c | 2009-05-22 22:20:02 +0000 | [diff] [blame] | 697 | new->csum = old->csum; |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 698 | new->local_df = old->local_df; |
| 699 | new->pkt_type = old->pkt_type; |
| 700 | new->ip_summed = old->ip_summed; |
| 701 | skb_copy_queue_mapping(new, old); |
| 702 | new->priority = old->priority; |
Igor Maravić | a3bf7ae | 2011-12-12 02:58:22 +0000 | [diff] [blame] | 703 | #if IS_ENABLED(CONFIG_IP_VS) |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 704 | new->ipvs_property = old->ipvs_property; |
| 705 | #endif |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 706 | new->pfmemalloc = old->pfmemalloc; |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 707 | new->protocol = old->protocol; |
| 708 | new->mark = old->mark; |
Eric Dumazet | 8964be4 | 2009-11-20 15:35:04 -0800 | [diff] [blame] | 709 | new->skb_iif = old->skb_iif; |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 710 | __nf_copy(new, old); |
Igor Maravić | a3bf7ae | 2011-12-12 02:58:22 +0000 | [diff] [blame] | 711 | #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 712 | new->nf_trace = old->nf_trace; |
| 713 | #endif |
| 714 | #ifdef CONFIG_NET_SCHED |
| 715 | new->tc_index = old->tc_index; |
| 716 | #ifdef CONFIG_NET_CLS_ACT |
| 717 | new->tc_verd = old->tc_verd; |
| 718 | #endif |
| 719 | #endif |
Patrick McHardy | 6aa895b | 2008-07-14 22:49:06 -0700 | [diff] [blame] | 720 | new->vlan_tci = old->vlan_tci; |
| 721 | |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 722 | skb_copy_secmark(new, old); |
| 723 | } |
| 724 | |
Herbert Xu | 82c49a3 | 2009-05-22 22:11:37 +0000 | [diff] [blame] | 725 | /* |
| 726 | * You should not add any new code to this function. Add it to |
| 727 | * __copy_skb_header above instead. |
| 728 | */ |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 729 | static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | #define C(x) n->x = skb->x |
| 732 | |
| 733 | n->next = n->prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | n->sk = NULL; |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 735 | __copy_skb_header(n, skb); |
| 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | C(len); |
| 738 | C(data_len); |
Alexey Dobriyan | 3e6b3b2 | 2007-03-16 15:00:46 -0700 | [diff] [blame] | 739 | C(mac_len); |
Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 740 | n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 741 | n->cloned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | n->nohdr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | n->destructor = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | C(tail); |
| 745 | C(end); |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 746 | C(head); |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 747 | C(head_frag); |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 748 | C(data); |
| 749 | C(truesize); |
| 750 | atomic_set(&n->users, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
| 752 | atomic_inc(&(skb_shinfo(skb)->dataref)); |
| 753 | skb->cloned = 1; |
| 754 | |
| 755 | return n; |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 756 | #undef C |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * skb_morph - morph one skb into another |
| 761 | * @dst: the skb to receive the contents |
| 762 | * @src: the skb to supply the contents |
| 763 | * |
| 764 | * This is identical to skb_clone except that the target skb is |
| 765 | * supplied by the user. |
| 766 | * |
| 767 | * The target skb is returned upon exit. |
| 768 | */ |
| 769 | struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) |
| 770 | { |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 771 | skb_release_all(dst); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 772 | return __skb_clone(dst, src); |
| 773 | } |
| 774 | EXPORT_SYMBOL_GPL(skb_morph); |
| 775 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 776 | /** |
| 777 | * skb_copy_ubufs - copy userspace skb frags buffers to kernel |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 778 | * @skb: the skb to modify |
| 779 | * @gfp_mask: allocation priority |
| 780 | * |
| 781 | * This must be called on SKBTX_DEV_ZEROCOPY skb. |
| 782 | * It will copy all frags into kernel and drop the reference |
| 783 | * to userspace pages. |
| 784 | * |
| 785 | * If this function is called from an interrupt gfp_mask() must be |
| 786 | * %GFP_ATOMIC. |
| 787 | * |
| 788 | * Returns 0 on success or a negative error code on failure |
| 789 | * to allocate kernel memory to copy to. |
| 790 | */ |
| 791 | int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 792 | { |
| 793 | int i; |
| 794 | int num_frags = skb_shinfo(skb)->nr_frags; |
| 795 | struct page *page, *head = NULL; |
| 796 | struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; |
| 797 | |
| 798 | for (i = 0; i < num_frags; i++) { |
| 799 | u8 *vaddr; |
| 800 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
| 801 | |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 802 | page = alloc_page(gfp_mask); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 803 | if (!page) { |
| 804 | while (head) { |
| 805 | struct page *next = (struct page *)head->private; |
| 806 | put_page(head); |
| 807 | head = next; |
| 808 | } |
| 809 | return -ENOMEM; |
| 810 | } |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 811 | vaddr = kmap_atomic(skb_frag_page(f)); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 812 | memcpy(page_address(page), |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 813 | vaddr + f->page_offset, skb_frag_size(f)); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 814 | kunmap_atomic(vaddr); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 815 | page->private = (unsigned long)head; |
| 816 | head = page; |
| 817 | } |
| 818 | |
| 819 | /* skb frags release userspace buffers */ |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 820 | for (i = 0; i < num_frags; i++) |
Ian Campbell | a8605c6 | 2011-10-19 23:01:49 +0000 | [diff] [blame] | 821 | skb_frag_unref(skb, i); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 822 | |
Michael S. Tsirkin | e19d676 | 2012-11-01 09:16:22 +0000 | [diff] [blame] | 823 | uarg->callback(uarg, false); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 824 | |
| 825 | /* skb frags point to kernel buffers */ |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 826 | for (i = num_frags - 1; i >= 0; i--) { |
| 827 | __skb_fill_page_desc(skb, i, head, 0, |
| 828 | skb_shinfo(skb)->frags[i].size); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 829 | head = (struct page *)head->private; |
| 830 | } |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 831 | |
| 832 | skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 833 | return 0; |
| 834 | } |
Michael S. Tsirkin | dcc0fb7 | 2012-07-20 09:23:20 +0000 | [diff] [blame] | 835 | EXPORT_SYMBOL_GPL(skb_copy_ubufs); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 836 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 837 | /** |
| 838 | * skb_clone - duplicate an sk_buff |
| 839 | * @skb: buffer to clone |
| 840 | * @gfp_mask: allocation priority |
| 841 | * |
| 842 | * Duplicate an &sk_buff. The new one is not owned by a socket. Both |
| 843 | * copies share the same packet data but not structure. The new |
| 844 | * buffer has a reference count of 1. If the allocation fails the |
| 845 | * function returns %NULL otherwise the new buffer is returned. |
| 846 | * |
| 847 | * If this function is called from an interrupt gfp_mask() must be |
| 848 | * %GFP_ATOMIC. |
| 849 | */ |
| 850 | |
| 851 | struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) |
| 852 | { |
| 853 | struct sk_buff *n; |
| 854 | |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 855 | if (skb_orphan_frags(skb, gfp_mask)) |
| 856 | return NULL; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 857 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 858 | n = skb + 1; |
| 859 | if (skb->fclone == SKB_FCLONE_ORIG && |
| 860 | n->fclone == SKB_FCLONE_UNAVAILABLE) { |
| 861 | atomic_t *fclone_ref = (atomic_t *) (n + 1); |
| 862 | n->fclone = SKB_FCLONE_CLONE; |
| 863 | atomic_inc(fclone_ref); |
| 864 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 865 | if (skb_pfmemalloc(skb)) |
| 866 | gfp_mask |= __GFP_MEMALLOC; |
| 867 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 868 | n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); |
| 869 | if (!n) |
| 870 | return NULL; |
Vegard Nossum | fe55f6d | 2008-08-30 12:16:35 +0200 | [diff] [blame] | 871 | |
| 872 | kmemcheck_annotate_bitfield(n, flags1); |
| 873 | kmemcheck_annotate_bitfield(n, flags2); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 874 | n->fclone = SKB_FCLONE_UNAVAILABLE; |
| 875 | } |
| 876 | |
| 877 | return __skb_clone(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 879 | EXPORT_SYMBOL(skb_clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
| 881 | static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 882 | { |
Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 883 | #ifndef NET_SKBUFF_DATA_USES_OFFSET |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | /* |
| 885 | * Shift between the two data areas in bytes |
| 886 | */ |
| 887 | unsigned long offset = new->data - old->data; |
Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 888 | #endif |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 889 | |
| 890 | __copy_skb_header(new, old); |
| 891 | |
Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 892 | #ifndef NET_SKBUFF_DATA_USES_OFFSET |
| 893 | /* {transport,network,mac}_header are relative to skb->head */ |
| 894 | new->transport_header += offset; |
| 895 | new->network_header += offset; |
Stephen Hemminger | 603a8bb | 2009-06-17 12:17:34 +0000 | [diff] [blame] | 896 | if (skb_mac_header_was_set(new)) |
| 897 | new->mac_header += offset; |
Joseph Gasparakis | 6a674e9 | 2012-12-07 14:14:14 +0000 | [diff] [blame] | 898 | new->inner_transport_header += offset; |
| 899 | new->inner_network_header += offset; |
Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 900 | #endif |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 901 | skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; |
| 902 | skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; |
| 903 | skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 906 | static inline int skb_alloc_rx_flag(const struct sk_buff *skb) |
| 907 | { |
| 908 | if (skb_pfmemalloc(skb)) |
| 909 | return SKB_ALLOC_RX; |
| 910 | return 0; |
| 911 | } |
| 912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | /** |
| 914 | * skb_copy - create private copy of an sk_buff |
| 915 | * @skb: buffer to copy |
| 916 | * @gfp_mask: allocation priority |
| 917 | * |
| 918 | * Make a copy of both an &sk_buff and its data. This is used when the |
| 919 | * caller wishes to modify the data and needs a private copy of the |
| 920 | * data to alter. Returns %NULL on failure or the pointer to the buffer |
| 921 | * on success. The returned buffer has a reference count of 1. |
| 922 | * |
| 923 | * As by-product this function converts non-linear &sk_buff to linear |
| 924 | * one, so that &sk_buff becomes completely private and caller is allowed |
| 925 | * to modify all the data of returned buffer. This means that this |
| 926 | * function is not recommended for use in circumstances when only |
| 927 | * header is going to be modified. Use pskb_copy() instead. |
| 928 | */ |
| 929 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 930 | struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | { |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 932 | int headerlen = skb_headroom(skb); |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 933 | unsigned int size = skb_end_offset(skb) + skb->data_len; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 934 | struct sk_buff *n = __alloc_skb(size, gfp_mask, |
| 935 | skb_alloc_rx_flag(skb), NUMA_NO_NODE); |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | if (!n) |
| 938 | return NULL; |
| 939 | |
| 940 | /* Set the data pointer */ |
| 941 | skb_reserve(n, headerlen); |
| 942 | /* Set the tail pointer and length */ |
| 943 | skb_put(n, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
| 945 | if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) |
| 946 | BUG(); |
| 947 | |
| 948 | copy_skb_header(n, skb); |
| 949 | return n; |
| 950 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 951 | EXPORT_SYMBOL(skb_copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
| 953 | /** |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 954 | * __pskb_copy - create copy of an sk_buff with private head. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | * @skb: buffer to copy |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 956 | * @headroom: headroom of new skb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | * @gfp_mask: allocation priority |
| 958 | * |
| 959 | * Make a copy of both an &sk_buff and part of its data, located |
| 960 | * in header. Fragmented data remain shared. This is used when |
| 961 | * the caller wishes to modify only header of &sk_buff and needs |
| 962 | * private copy of the header to alter. Returns %NULL on failure |
| 963 | * or the pointer to the buffer on success. |
| 964 | * The returned buffer has a reference count of 1. |
| 965 | */ |
| 966 | |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 967 | struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | { |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 969 | unsigned int size = skb_headlen(skb) + headroom; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 970 | struct sk_buff *n = __alloc_skb(size, gfp_mask, |
| 971 | skb_alloc_rx_flag(skb), NUMA_NO_NODE); |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | if (!n) |
| 974 | goto out; |
| 975 | |
| 976 | /* Set the data pointer */ |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 977 | skb_reserve(n, headroom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | /* Set the tail pointer and length */ |
| 979 | skb_put(n, skb_headlen(skb)); |
| 980 | /* Copy the bytes */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 981 | skb_copy_from_linear_data(skb, n->data, n->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
Herbert Xu | 25f484a | 2006-11-07 14:57:15 -0800 | [diff] [blame] | 983 | n->truesize += skb->data_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | n->data_len = skb->data_len; |
| 985 | n->len = skb->len; |
| 986 | |
| 987 | if (skb_shinfo(skb)->nr_frags) { |
| 988 | int i; |
| 989 | |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 990 | if (skb_orphan_frags(skb, gfp_mask)) { |
| 991 | kfree_skb(n); |
| 992 | n = NULL; |
| 993 | goto out; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 994 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 996 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 997 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | } |
| 999 | skb_shinfo(n)->nr_frags = i; |
| 1000 | } |
| 1001 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1002 | if (skb_has_frag_list(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; |
| 1004 | skb_clone_fraglist(n); |
| 1005 | } |
| 1006 | |
| 1007 | copy_skb_header(n, skb); |
| 1008 | out: |
| 1009 | return n; |
| 1010 | } |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1011 | EXPORT_SYMBOL(__pskb_copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
| 1013 | /** |
| 1014 | * pskb_expand_head - reallocate header of &sk_buff |
| 1015 | * @skb: buffer to reallocate |
| 1016 | * @nhead: room to add at head |
| 1017 | * @ntail: room to add at tail |
| 1018 | * @gfp_mask: allocation priority |
| 1019 | * |
| 1020 | * Expands (or creates identical copy, if &nhead and &ntail are zero) |
| 1021 | * header of skb. &sk_buff itself is not changed. &sk_buff MUST have |
| 1022 | * reference count of 1. Returns zero in the case of success or error, |
| 1023 | * if expansion failed. In the last case, &sk_buff is not changed. |
| 1024 | * |
| 1025 | * All the pointers pointing into skb header may change and must be |
| 1026 | * reloaded after call to this function. |
| 1027 | */ |
| 1028 | |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 1029 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1030 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | { |
| 1032 | int i; |
| 1033 | u8 *data; |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 1034 | int size = nhead + skb_end_offset(skb) + ntail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | long off; |
| 1036 | |
Herbert Xu | 4edd87a | 2008-10-01 07:09:38 -0700 | [diff] [blame] | 1037 | BUG_ON(nhead < 0); |
| 1038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | if (skb_shared(skb)) |
| 1040 | BUG(); |
| 1041 | |
| 1042 | size = SKB_DATA_ALIGN(size); |
| 1043 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1044 | if (skb_pfmemalloc(skb)) |
| 1045 | gfp_mask |= __GFP_MEMALLOC; |
| 1046 | data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), |
| 1047 | gfp_mask, NUMA_NO_NODE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | if (!data) |
| 1049 | goto nodata; |
Eric Dumazet | 87151b8 | 2012-04-10 20:08:39 +0000 | [diff] [blame] | 1050 | size = SKB_WITH_OVERHEAD(ksize(data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | |
| 1052 | /* Copy only real data... and, alas, header. This should be |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1053 | * optimized for the cases when header is void. |
| 1054 | */ |
| 1055 | memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); |
| 1056 | |
| 1057 | memcpy((struct skb_shared_info *)(data + size), |
| 1058 | skb_shinfo(skb), |
Eric Dumazet | fed6638 | 2010-07-22 19:09:08 +0000 | [diff] [blame] | 1059 | offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | |
Alexander Duyck | 3e24591 | 2012-05-04 14:26:51 +0000 | [diff] [blame] | 1061 | /* |
| 1062 | * if shinfo is shared we must drop the old head gracefully, but if it |
| 1063 | * is not we can just drop the old head and let the existing refcount |
| 1064 | * be since all we did is relocate the values |
| 1065 | */ |
| 1066 | if (skb_cloned(skb)) { |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1067 | /* copy this zero copy skb frags */ |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1068 | if (skb_orphan_frags(skb, gfp_mask)) |
| 1069 | goto nofrags; |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1070 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1071 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1073 | if (skb_has_frag_list(skb)) |
| 1074 | skb_clone_fraglist(skb); |
| 1075 | |
| 1076 | skb_release_data(skb); |
Alexander Duyck | 3e24591 | 2012-05-04 14:26:51 +0000 | [diff] [blame] | 1077 | } else { |
| 1078 | skb_free_head(skb); |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | off = (data + nhead) - skb->head; |
| 1081 | |
| 1082 | skb->head = data; |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 1083 | skb->head_frag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | skb->data += off; |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1085 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 1086 | skb->end = size; |
Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 1087 | off = nhead; |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1088 | #else |
| 1089 | skb->end = skb->head + size; |
Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 1090 | #endif |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1091 | /* {transport,network,mac}_header and tail are relative to skb->head */ |
| 1092 | skb->tail += off; |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1093 | skb->transport_header += off; |
| 1094 | skb->network_header += off; |
Stephen Hemminger | 603a8bb | 2009-06-17 12:17:34 +0000 | [diff] [blame] | 1095 | if (skb_mac_header_was_set(skb)) |
| 1096 | skb->mac_header += off; |
Joseph Gasparakis | 6a674e9 | 2012-12-07 14:14:14 +0000 | [diff] [blame] | 1097 | skb->inner_transport_header += off; |
| 1098 | skb->inner_network_header += off; |
Andrea Shepard | 00c5a98 | 2010-07-22 09:12:35 +0000 | [diff] [blame] | 1099 | /* Only adjust this if it actually is csum_start rather than csum */ |
| 1100 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 1101 | skb->csum_start += nhead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | skb->cloned = 0; |
Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 1103 | skb->hdr_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | skb->nohdr = 0; |
| 1105 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 1106 | return 0; |
| 1107 | |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1108 | nofrags: |
| 1109 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | nodata: |
| 1111 | return -ENOMEM; |
| 1112 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1113 | EXPORT_SYMBOL(pskb_expand_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
| 1115 | /* Make private copy of skb with writable head and some headroom */ |
| 1116 | |
| 1117 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) |
| 1118 | { |
| 1119 | struct sk_buff *skb2; |
| 1120 | int delta = headroom - skb_headroom(skb); |
| 1121 | |
| 1122 | if (delta <= 0) |
| 1123 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
| 1124 | else { |
| 1125 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1126 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, |
| 1127 | GFP_ATOMIC)) { |
| 1128 | kfree_skb(skb2); |
| 1129 | skb2 = NULL; |
| 1130 | } |
| 1131 | } |
| 1132 | return skb2; |
| 1133 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1134 | EXPORT_SYMBOL(skb_realloc_headroom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
| 1136 | /** |
| 1137 | * skb_copy_expand - copy and expand sk_buff |
| 1138 | * @skb: buffer to copy |
| 1139 | * @newheadroom: new free bytes at head |
| 1140 | * @newtailroom: new free bytes at tail |
| 1141 | * @gfp_mask: allocation priority |
| 1142 | * |
| 1143 | * Make a copy of both an &sk_buff and its data and while doing so |
| 1144 | * allocate additional space. |
| 1145 | * |
| 1146 | * This is used when the caller wishes to modify the data and needs a |
| 1147 | * private copy of the data to alter as well as more space for new fields. |
| 1148 | * Returns %NULL on failure or the pointer to the buffer |
| 1149 | * on success. The returned buffer has a reference count of 1. |
| 1150 | * |
| 1151 | * You must pass %GFP_ATOMIC as the allocation priority if this function |
| 1152 | * is called from an interrupt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | */ |
| 1154 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 1155 | int newheadroom, int newtailroom, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1156 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | { |
| 1158 | /* |
| 1159 | * Allocate the copy buffer |
| 1160 | */ |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1161 | struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, |
| 1162 | gfp_mask, skb_alloc_rx_flag(skb), |
| 1163 | NUMA_NO_NODE); |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1164 | int oldheadroom = skb_headroom(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | int head_copy_len, head_copy_off; |
Herbert Xu | 5288605 | 2007-09-16 16:32:11 -0700 | [diff] [blame] | 1166 | int off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | |
| 1168 | if (!n) |
| 1169 | return NULL; |
| 1170 | |
| 1171 | skb_reserve(n, newheadroom); |
| 1172 | |
| 1173 | /* Set the tail pointer and length */ |
| 1174 | skb_put(n, skb->len); |
| 1175 | |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1176 | head_copy_len = oldheadroom; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | head_copy_off = 0; |
| 1178 | if (newheadroom <= head_copy_len) |
| 1179 | head_copy_len = newheadroom; |
| 1180 | else |
| 1181 | head_copy_off = newheadroom - head_copy_len; |
| 1182 | |
| 1183 | /* Copy the linear header and data. */ |
| 1184 | if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, |
| 1185 | skb->len + head_copy_len)) |
| 1186 | BUG(); |
| 1187 | |
| 1188 | copy_skb_header(n, skb); |
| 1189 | |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1190 | off = newheadroom - oldheadroom; |
David S. Miller | be2b6e6 | 2010-07-22 13:27:09 -0700 | [diff] [blame] | 1191 | if (n->ip_summed == CHECKSUM_PARTIAL) |
| 1192 | n->csum_start += off; |
Herbert Xu | 5288605 | 2007-09-16 16:32:11 -0700 | [diff] [blame] | 1193 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1194 | n->transport_header += off; |
| 1195 | n->network_header += off; |
Stephen Hemminger | 603a8bb | 2009-06-17 12:17:34 +0000 | [diff] [blame] | 1196 | if (skb_mac_header_was_set(skb)) |
| 1197 | n->mac_header += off; |
Joseph Gasparakis | 6a674e9 | 2012-12-07 14:14:14 +0000 | [diff] [blame] | 1198 | n->inner_transport_header += off; |
| 1199 | n->inner_network_header += off; |
Herbert Xu | 5288605 | 2007-09-16 16:32:11 -0700 | [diff] [blame] | 1200 | #endif |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | return n; |
| 1203 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1204 | EXPORT_SYMBOL(skb_copy_expand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
| 1206 | /** |
| 1207 | * skb_pad - zero pad the tail of an skb |
| 1208 | * @skb: buffer to pad |
| 1209 | * @pad: space to pad |
| 1210 | * |
| 1211 | * Ensure that a buffer is followed by a padding area that is zero |
| 1212 | * filled. Used by network drivers which may DMA or transfer data |
| 1213 | * beyond the buffer end onto the wire. |
| 1214 | * |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1215 | * May return error in out of memory cases. The skb is freed on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1217 | |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1218 | int skb_pad(struct sk_buff *skb, int pad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | { |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1220 | int err; |
| 1221 | int ntail; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | /* If the skbuff is non linear tailroom is always zero.. */ |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1224 | if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | memset(skb->data+skb->len, 0, pad); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1226 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | } |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1228 | |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1229 | ntail = skb->data_len + pad - (skb->end - skb->tail); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1230 | if (likely(skb_cloned(skb) || ntail > 0)) { |
| 1231 | err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); |
| 1232 | if (unlikely(err)) |
| 1233 | goto free_skb; |
| 1234 | } |
| 1235 | |
| 1236 | /* FIXME: The use of this function with non-linear skb's really needs |
| 1237 | * to be audited. |
| 1238 | */ |
| 1239 | err = skb_linearize(skb); |
| 1240 | if (unlikely(err)) |
| 1241 | goto free_skb; |
| 1242 | |
| 1243 | memset(skb->data + skb->len, 0, pad); |
| 1244 | return 0; |
| 1245 | |
| 1246 | free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | kfree_skb(skb); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1248 | return err; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1249 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1250 | EXPORT_SYMBOL(skb_pad); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1251 | |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1252 | /** |
| 1253 | * skb_put - add data to a buffer |
| 1254 | * @skb: buffer to use |
| 1255 | * @len: amount of data to add |
| 1256 | * |
| 1257 | * This function extends the used data area of the buffer. If this would |
| 1258 | * exceed the total buffer size the kernel will panic. A pointer to the |
| 1259 | * first byte of the extra data is returned. |
| 1260 | */ |
| 1261 | unsigned char *skb_put(struct sk_buff *skb, unsigned int len) |
| 1262 | { |
| 1263 | unsigned char *tmp = skb_tail_pointer(skb); |
| 1264 | SKB_LINEAR_ASSERT(skb); |
| 1265 | skb->tail += len; |
| 1266 | skb->len += len; |
| 1267 | if (unlikely(skb->tail > skb->end)) |
| 1268 | skb_over_panic(skb, len, __builtin_return_address(0)); |
| 1269 | return tmp; |
| 1270 | } |
| 1271 | EXPORT_SYMBOL(skb_put); |
| 1272 | |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1273 | /** |
Ilpo Järvinen | c2aa270 | 2008-03-27 17:52:40 -0700 | [diff] [blame] | 1274 | * skb_push - add data to the start of a buffer |
| 1275 | * @skb: buffer to use |
| 1276 | * @len: amount of data to add |
| 1277 | * |
| 1278 | * This function extends the used data area of the buffer at the buffer |
| 1279 | * start. If this would exceed the total buffer headroom the kernel will |
| 1280 | * panic. A pointer to the first byte of the extra data is returned. |
| 1281 | */ |
| 1282 | unsigned char *skb_push(struct sk_buff *skb, unsigned int len) |
| 1283 | { |
| 1284 | skb->data -= len; |
| 1285 | skb->len += len; |
| 1286 | if (unlikely(skb->data<skb->head)) |
| 1287 | skb_under_panic(skb, len, __builtin_return_address(0)); |
| 1288 | return skb->data; |
| 1289 | } |
| 1290 | EXPORT_SYMBOL(skb_push); |
| 1291 | |
| 1292 | /** |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1293 | * skb_pull - remove data from the start of a buffer |
| 1294 | * @skb: buffer to use |
| 1295 | * @len: amount of data to remove |
| 1296 | * |
| 1297 | * This function removes data from the start of a buffer, returning |
| 1298 | * the memory to the headroom. A pointer to the next data in the buffer |
| 1299 | * is returned. Once the data has been pulled future pushes will overwrite |
| 1300 | * the old data. |
| 1301 | */ |
| 1302 | unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) |
| 1303 | { |
David S. Miller | 47d2964 | 2010-05-02 02:21:44 -0700 | [diff] [blame] | 1304 | return skb_pull_inline(skb, len); |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1305 | } |
| 1306 | EXPORT_SYMBOL(skb_pull); |
| 1307 | |
Ilpo Järvinen | 419ae74 | 2008-03-27 17:54:01 -0700 | [diff] [blame] | 1308 | /** |
| 1309 | * skb_trim - remove end from a buffer |
| 1310 | * @skb: buffer to alter |
| 1311 | * @len: new length |
| 1312 | * |
| 1313 | * Cut the length of a buffer down by removing data from the tail. If |
| 1314 | * the buffer is already under the length specified it is not modified. |
| 1315 | * The skb must be linear. |
| 1316 | */ |
| 1317 | void skb_trim(struct sk_buff *skb, unsigned int len) |
| 1318 | { |
| 1319 | if (skb->len > len) |
| 1320 | __skb_trim(skb, len); |
| 1321 | } |
| 1322 | EXPORT_SYMBOL(skb_trim); |
| 1323 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 1324 | /* Trims skb to length len. It can change skb pointers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | */ |
| 1326 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 1327 | int ___pskb_trim(struct sk_buff *skb, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1329 | struct sk_buff **fragp; |
| 1330 | struct sk_buff *frag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | int offset = skb_headlen(skb); |
| 1332 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 1333 | int i; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1334 | int err; |
| 1335 | |
| 1336 | if (skb_cloned(skb) && |
| 1337 | unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) |
| 1338 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1340 | i = 0; |
| 1341 | if (offset >= len) |
| 1342 | goto drop_pages; |
| 1343 | |
| 1344 | for (; i < nfrags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1345 | int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1346 | |
| 1347 | if (end < len) { |
| 1348 | offset = end; |
| 1349 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | } |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1351 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1352 | skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1353 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1354 | drop_pages: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1355 | skb_shinfo(skb)->nr_frags = i; |
| 1356 | |
| 1357 | for (; i < nfrags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1358 | skb_frag_unref(skb, i); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1359 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1360 | if (skb_has_frag_list(skb)) |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1361 | skb_drop_fraglist(skb); |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1362 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1365 | for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); |
| 1366 | fragp = &frag->next) { |
| 1367 | int end = offset + frag->len; |
| 1368 | |
| 1369 | if (skb_shared(frag)) { |
| 1370 | struct sk_buff *nfrag; |
| 1371 | |
| 1372 | nfrag = skb_clone(frag, GFP_ATOMIC); |
| 1373 | if (unlikely(!nfrag)) |
| 1374 | return -ENOMEM; |
| 1375 | |
| 1376 | nfrag->next = frag->next; |
Eric Dumazet | 85bb2a6 | 2012-04-19 02:24:53 +0000 | [diff] [blame] | 1377 | consume_skb(frag); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1378 | frag = nfrag; |
| 1379 | *fragp = frag; |
| 1380 | } |
| 1381 | |
| 1382 | if (end < len) { |
| 1383 | offset = end; |
| 1384 | continue; |
| 1385 | } |
| 1386 | |
| 1387 | if (end > len && |
| 1388 | unlikely((err = pskb_trim(frag, len - offset)))) |
| 1389 | return err; |
| 1390 | |
| 1391 | if (frag->next) |
| 1392 | skb_drop_list(&frag->next); |
| 1393 | break; |
| 1394 | } |
| 1395 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1396 | done: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1397 | if (len > skb_headlen(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | skb->data_len -= skb->len - len; |
| 1399 | skb->len = len; |
| 1400 | } else { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1401 | skb->len = len; |
| 1402 | skb->data_len = 0; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1403 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | return 0; |
| 1407 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1408 | EXPORT_SYMBOL(___pskb_trim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
| 1410 | /** |
| 1411 | * __pskb_pull_tail - advance tail of skb header |
| 1412 | * @skb: buffer to reallocate |
| 1413 | * @delta: number of bytes to advance tail |
| 1414 | * |
| 1415 | * The function makes a sense only on a fragmented &sk_buff, |
| 1416 | * it expands header moving its tail forward and copying necessary |
| 1417 | * data from fragmented part. |
| 1418 | * |
| 1419 | * &sk_buff MUST have reference count of 1. |
| 1420 | * |
| 1421 | * Returns %NULL (and &sk_buff does not change) if pull failed |
| 1422 | * or value of new tail of skb in the case of success. |
| 1423 | * |
| 1424 | * All the pointers pointing into skb header may change and must be |
| 1425 | * reloaded after call to this function. |
| 1426 | */ |
| 1427 | |
| 1428 | /* Moves tail of skb head forward, copying data from fragmented part, |
| 1429 | * when it is necessary. |
| 1430 | * 1. It may fail due to malloc failure. |
| 1431 | * 2. It may change skb pointers. |
| 1432 | * |
| 1433 | * It is pretty complicated. Luckily, it is called only in exceptional cases. |
| 1434 | */ |
| 1435 | unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) |
| 1436 | { |
| 1437 | /* If skb has not enough free space at tail, get new one |
| 1438 | * plus 128 bytes for future expansions. If we have enough |
| 1439 | * room at tail, reallocate without expansion only if skb is cloned. |
| 1440 | */ |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1441 | int i, k, eat = (skb->tail + delta) - skb->end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | |
| 1443 | if (eat > 0 || skb_cloned(skb)) { |
| 1444 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, |
| 1445 | GFP_ATOMIC)) |
| 1446 | return NULL; |
| 1447 | } |
| 1448 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1449 | if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | BUG(); |
| 1451 | |
| 1452 | /* Optimization: no fragments, no reasons to preestimate |
| 1453 | * size of pulled pages. Superb. |
| 1454 | */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1455 | if (!skb_has_frag_list(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | goto pull_pages; |
| 1457 | |
| 1458 | /* Estimate size of pulled pages. */ |
| 1459 | eat = delta; |
| 1460 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1461 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 1462 | |
| 1463 | if (size >= eat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | goto pull_pages; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1465 | eat -= size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | /* If we need update frag list, we are in troubles. |
| 1469 | * Certainly, it possible to add an offset to skb data, |
| 1470 | * but taking into account that pulling is expected to |
| 1471 | * be very rare operation, it is worth to fight against |
| 1472 | * further bloating skb head and crucify ourselves here instead. |
| 1473 | * Pure masohism, indeed. 8)8) |
| 1474 | */ |
| 1475 | if (eat) { |
| 1476 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1477 | struct sk_buff *clone = NULL; |
| 1478 | struct sk_buff *insp = NULL; |
| 1479 | |
| 1480 | do { |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1481 | BUG_ON(!list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
| 1483 | if (list->len <= eat) { |
| 1484 | /* Eaten as whole. */ |
| 1485 | eat -= list->len; |
| 1486 | list = list->next; |
| 1487 | insp = list; |
| 1488 | } else { |
| 1489 | /* Eaten partially. */ |
| 1490 | |
| 1491 | if (skb_shared(list)) { |
| 1492 | /* Sucks! We need to fork list. :-( */ |
| 1493 | clone = skb_clone(list, GFP_ATOMIC); |
| 1494 | if (!clone) |
| 1495 | return NULL; |
| 1496 | insp = list->next; |
| 1497 | list = clone; |
| 1498 | } else { |
| 1499 | /* This may be pulled without |
| 1500 | * problems. */ |
| 1501 | insp = list; |
| 1502 | } |
| 1503 | if (!pskb_pull(list, eat)) { |
Wei Yongjun | f3fbbe0 | 2009-02-25 00:37:32 +0000 | [diff] [blame] | 1504 | kfree_skb(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | return NULL; |
| 1506 | } |
| 1507 | break; |
| 1508 | } |
| 1509 | } while (eat); |
| 1510 | |
| 1511 | /* Free pulled out fragments. */ |
| 1512 | while ((list = skb_shinfo(skb)->frag_list) != insp) { |
| 1513 | skb_shinfo(skb)->frag_list = list->next; |
| 1514 | kfree_skb(list); |
| 1515 | } |
| 1516 | /* And insert new clone at head. */ |
| 1517 | if (clone) { |
| 1518 | clone->next = list; |
| 1519 | skb_shinfo(skb)->frag_list = clone; |
| 1520 | } |
| 1521 | } |
| 1522 | /* Success! Now we may commit changes to skb data. */ |
| 1523 | |
| 1524 | pull_pages: |
| 1525 | eat = delta; |
| 1526 | k = 0; |
| 1527 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1528 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 1529 | |
| 1530 | if (size <= eat) { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1531 | skb_frag_unref(skb, i); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1532 | eat -= size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | } else { |
| 1534 | skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 1535 | if (eat) { |
| 1536 | skb_shinfo(skb)->frags[k].page_offset += eat; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1537 | skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | eat = 0; |
| 1539 | } |
| 1540 | k++; |
| 1541 | } |
| 1542 | } |
| 1543 | skb_shinfo(skb)->nr_frags = k; |
| 1544 | |
| 1545 | skb->tail += delta; |
| 1546 | skb->data_len -= delta; |
| 1547 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1548 | return skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1550 | EXPORT_SYMBOL(__pskb_pull_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
Eric Dumazet | 22019b1 | 2011-07-29 18:37:31 +0000 | [diff] [blame] | 1552 | /** |
| 1553 | * skb_copy_bits - copy bits from skb to kernel buffer |
| 1554 | * @skb: source skb |
| 1555 | * @offset: offset in source |
| 1556 | * @to: destination buffer |
| 1557 | * @len: number of bytes to copy |
| 1558 | * |
| 1559 | * Copy the specified number of bytes from the source skb to the |
| 1560 | * destination buffer. |
| 1561 | * |
| 1562 | * CAUTION ! : |
| 1563 | * If its prototype is ever changed, |
| 1564 | * check arch/{*}/net/{*}.S files, |
| 1565 | * since it is called from BPF assembly code. |
| 1566 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) |
| 1568 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1569 | int start = skb_headlen(skb); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1570 | struct sk_buff *frag_iter; |
| 1571 | int i, copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | |
| 1573 | if (offset > (int)skb->len - len) |
| 1574 | goto fault; |
| 1575 | |
| 1576 | /* Copy header. */ |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1577 | if ((copy = start - offset) > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | if (copy > len) |
| 1579 | copy = len; |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1580 | skb_copy_from_linear_data_offset(skb, offset, to, copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | if ((len -= copy) == 0) |
| 1582 | return 0; |
| 1583 | offset += copy; |
| 1584 | to += copy; |
| 1585 | } |
| 1586 | |
| 1587 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1588 | int end; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1589 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1591 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1592 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1593 | end = start + skb_frag_size(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | if ((copy = end - offset) > 0) { |
| 1595 | u8 *vaddr; |
| 1596 | |
| 1597 | if (copy > len) |
| 1598 | copy = len; |
| 1599 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1600 | vaddr = kmap_atomic(skb_frag_page(f)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | memcpy(to, |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1602 | vaddr + f->page_offset + offset - start, |
| 1603 | copy); |
| 1604 | kunmap_atomic(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
| 1606 | if ((len -= copy) == 0) |
| 1607 | return 0; |
| 1608 | offset += copy; |
| 1609 | to += copy; |
| 1610 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1611 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1614 | skb_walk_frags(skb, frag_iter) { |
| 1615 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1617 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1619 | end = start + frag_iter->len; |
| 1620 | if ((copy = end - offset) > 0) { |
| 1621 | if (copy > len) |
| 1622 | copy = len; |
| 1623 | if (skb_copy_bits(frag_iter, offset - start, to, copy)) |
| 1624 | goto fault; |
| 1625 | if ((len -= copy) == 0) |
| 1626 | return 0; |
| 1627 | offset += copy; |
| 1628 | to += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1630 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | } |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | if (!len) |
| 1634 | return 0; |
| 1635 | |
| 1636 | fault: |
| 1637 | return -EFAULT; |
| 1638 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1639 | EXPORT_SYMBOL(skb_copy_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1641 | /* |
| 1642 | * Callback from splice_to_pipe(), if we need to release some pages |
| 1643 | * at the end of the spd in case we error'ed out in filling the pipe. |
| 1644 | */ |
| 1645 | static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) |
| 1646 | { |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1647 | put_page(spd->pages[i]); |
| 1648 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1649 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1650 | static struct page *linear_to_page(struct page *page, unsigned int *len, |
| 1651 | unsigned int *offset, |
| 1652 | struct sk_buff *skb, struct sock *sk) |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1653 | { |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1654 | struct page_frag *pfrag = sk_page_frag(sk); |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1655 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1656 | if (!sk_page_frag_refill(sk, pfrag)) |
| 1657 | return NULL; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1658 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1659 | *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1660 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1661 | memcpy(page_address(pfrag->page) + pfrag->offset, |
| 1662 | page_address(page) + *offset, *len); |
| 1663 | *offset = pfrag->offset; |
| 1664 | pfrag->offset += *len; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1665 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1666 | return pfrag->page; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1667 | } |
| 1668 | |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1669 | static bool spd_can_coalesce(const struct splice_pipe_desc *spd, |
| 1670 | struct page *page, |
| 1671 | unsigned int offset) |
| 1672 | { |
| 1673 | return spd->nr_pages && |
| 1674 | spd->pages[spd->nr_pages - 1] == page && |
| 1675 | (spd->partial[spd->nr_pages - 1].offset + |
| 1676 | spd->partial[spd->nr_pages - 1].len == offset); |
| 1677 | } |
| 1678 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1679 | /* |
| 1680 | * Fill page/offset/length into spd, if it can hold more pages. |
| 1681 | */ |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1682 | static bool spd_fill_page(struct splice_pipe_desc *spd, |
| 1683 | struct pipe_inode_info *pipe, struct page *page, |
| 1684 | unsigned int *len, unsigned int offset, |
Eric Dumazet | d7ccf7c | 2012-04-23 23:35:04 -0400 | [diff] [blame] | 1685 | struct sk_buff *skb, bool linear, |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1686 | struct sock *sk) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1687 | { |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1688 | if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1689 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1690 | |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1691 | if (linear) { |
Jarek Poplawski | 7a67e56 | 2009-04-30 05:41:19 -0700 | [diff] [blame] | 1692 | page = linear_to_page(page, len, &offset, skb, sk); |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1693 | if (!page) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1694 | return true; |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1695 | } |
| 1696 | if (spd_can_coalesce(spd, page, offset)) { |
| 1697 | spd->partial[spd->nr_pages - 1].len += *len; |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1698 | return false; |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1699 | } |
| 1700 | get_page(page); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1701 | spd->pages[spd->nr_pages] = page; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1702 | spd->partial[spd->nr_pages].len = *len; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1703 | spd->partial[spd->nr_pages].offset = offset; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1704 | spd->nr_pages++; |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1705 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1706 | return false; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1707 | } |
| 1708 | |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1709 | static inline void __segment_seek(struct page **page, unsigned int *poff, |
| 1710 | unsigned int *plen, unsigned int off) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1711 | { |
Jarek Poplawski | ce3dd39 | 2009-02-12 16:51:43 -0800 | [diff] [blame] | 1712 | unsigned long n; |
| 1713 | |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1714 | *poff += off; |
Jarek Poplawski | ce3dd39 | 2009-02-12 16:51:43 -0800 | [diff] [blame] | 1715 | n = *poff / PAGE_SIZE; |
| 1716 | if (n) |
| 1717 | *page = nth_page(*page, n); |
| 1718 | |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1719 | *poff = *poff % PAGE_SIZE; |
| 1720 | *plen -= off; |
| 1721 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1722 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1723 | static bool __splice_segment(struct page *page, unsigned int poff, |
| 1724 | unsigned int plen, unsigned int *off, |
| 1725 | unsigned int *len, struct sk_buff *skb, |
Eric Dumazet | d7ccf7c | 2012-04-23 23:35:04 -0400 | [diff] [blame] | 1726 | struct splice_pipe_desc *spd, bool linear, |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1727 | struct sock *sk, |
| 1728 | struct pipe_inode_info *pipe) |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1729 | { |
| 1730 | if (!*len) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1731 | return true; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1732 | |
| 1733 | /* skip this segment if already processed */ |
| 1734 | if (*off >= plen) { |
| 1735 | *off -= plen; |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1736 | return false; |
Octavian Purdila | db43a28 | 2008-06-27 17:27:21 -0700 | [diff] [blame] | 1737 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1738 | |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1739 | /* ignore any bits we already processed */ |
| 1740 | if (*off) { |
| 1741 | __segment_seek(&page, &poff, &plen, *off); |
| 1742 | *off = 0; |
| 1743 | } |
| 1744 | |
| 1745 | do { |
| 1746 | unsigned int flen = min(*len, plen); |
| 1747 | |
| 1748 | /* the linear region may spread across several pages */ |
| 1749 | flen = min_t(unsigned int, flen, PAGE_SIZE - poff); |
| 1750 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1751 | if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1752 | return true; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1753 | |
| 1754 | __segment_seek(&page, &poff, &plen, flen); |
| 1755 | *len -= flen; |
| 1756 | |
| 1757 | } while (*len && plen); |
| 1758 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1759 | return false; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | /* |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1763 | * Map linear and fragment data from the skb to spd. It reports true if the |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1764 | * pipe is full or if we already spliced the requested length. |
| 1765 | */ |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1766 | static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, |
| 1767 | unsigned int *offset, unsigned int *len, |
| 1768 | struct splice_pipe_desc *spd, struct sock *sk) |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1769 | { |
| 1770 | int seg; |
| 1771 | |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 1772 | /* map the linear part : |
Alexander Duyck | 2996d31 | 2012-05-02 18:18:42 +0000 | [diff] [blame] | 1773 | * If skb->head_frag is set, this 'linear' part is backed by a |
| 1774 | * fragment, and if the head is not shared with any clones then |
| 1775 | * we can avoid a copy since we own the head portion of this page. |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1776 | */ |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1777 | if (__splice_segment(virt_to_page(skb->data), |
| 1778 | (unsigned long) skb->data & (PAGE_SIZE - 1), |
| 1779 | skb_headlen(skb), |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 1780 | offset, len, skb, spd, |
Alexander Duyck | 3a7c1ee4 | 2012-05-03 01:09:42 +0000 | [diff] [blame] | 1781 | skb_head_is_locked(skb), |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 1782 | sk, pipe)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1783 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1784 | |
| 1785 | /* |
| 1786 | * then map the fragments |
| 1787 | */ |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1788 | for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { |
| 1789 | const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; |
| 1790 | |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1791 | if (__splice_segment(skb_frag_page(f), |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1792 | f->page_offset, skb_frag_size(f), |
Eric Dumazet | d7ccf7c | 2012-04-23 23:35:04 -0400 | [diff] [blame] | 1793 | offset, len, skb, spd, false, sk, pipe)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1794 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1795 | } |
| 1796 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1797 | return false; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | /* |
| 1801 | * Map data from the skb to a pipe. Should handle both the linear part, |
| 1802 | * the fragments, and the frag list. It does NOT handle frag lists within |
| 1803 | * the frag list, if such a thing exists. We'd probably need to recurse to |
| 1804 | * handle that cleanly. |
| 1805 | */ |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1806 | int skb_splice_bits(struct sk_buff *skb, unsigned int offset, |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1807 | struct pipe_inode_info *pipe, unsigned int tlen, |
| 1808 | unsigned int flags) |
| 1809 | { |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1810 | struct partial_page partial[MAX_SKB_FRAGS]; |
| 1811 | struct page *pages[MAX_SKB_FRAGS]; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1812 | struct splice_pipe_desc spd = { |
| 1813 | .pages = pages, |
| 1814 | .partial = partial, |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 1815 | .nr_pages_max = MAX_SKB_FRAGS, |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1816 | .flags = flags, |
| 1817 | .ops = &sock_pipe_buf_ops, |
| 1818 | .spd_release = sock_spd_release, |
| 1819 | }; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1820 | struct sk_buff *frag_iter; |
Jarek Poplawski | 7a67e56 | 2009-04-30 05:41:19 -0700 | [diff] [blame] | 1821 | struct sock *sk = skb->sk; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1822 | int ret = 0; |
| 1823 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1824 | /* |
| 1825 | * __skb_splice_bits() only fails if the output has no room left, |
| 1826 | * so no point in going over the frag_list for the error case. |
| 1827 | */ |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1828 | if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1829 | goto done; |
| 1830 | else if (!tlen) |
| 1831 | goto done; |
| 1832 | |
| 1833 | /* |
| 1834 | * now see if we have a frag_list to map |
| 1835 | */ |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1836 | skb_walk_frags(skb, frag_iter) { |
| 1837 | if (!tlen) |
| 1838 | break; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1839 | if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1840 | break; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | done: |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1844 | if (spd.nr_pages) { |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1845 | /* |
| 1846 | * Drop the socket lock, otherwise we have reverse |
| 1847 | * locking dependencies between sk_lock and i_mutex |
| 1848 | * here as compared to sendfile(). We enter here |
| 1849 | * with the socket lock held, and splice_to_pipe() will |
| 1850 | * grab the pipe inode lock. For sendfile() emulation, |
| 1851 | * we call into ->sendpage() with the i_mutex lock held |
| 1852 | * and networking will grab the socket lock. |
| 1853 | */ |
Octavian Purdila | 293ad60 | 2008-06-04 15:45:58 -0700 | [diff] [blame] | 1854 | release_sock(sk); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1855 | ret = splice_to_pipe(pipe, &spd); |
Octavian Purdila | 293ad60 | 2008-06-04 15:45:58 -0700 | [diff] [blame] | 1856 | lock_sock(sk); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1859 | return ret; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1860 | } |
| 1861 | |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1862 | /** |
| 1863 | * skb_store_bits - store bits from kernel buffer to skb |
| 1864 | * @skb: destination buffer |
| 1865 | * @offset: offset in destination |
| 1866 | * @from: source buffer |
| 1867 | * @len: number of bytes to copy |
| 1868 | * |
| 1869 | * Copy the specified number of bytes from the source buffer to the |
| 1870 | * destination skb. This function handles all the messy bits of |
| 1871 | * traversing fragment lists and such. |
| 1872 | */ |
| 1873 | |
Stephen Hemminger | 0c6fcc8 | 2007-04-20 16:40:01 -0700 | [diff] [blame] | 1874 | int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1875 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1876 | int start = skb_headlen(skb); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1877 | struct sk_buff *frag_iter; |
| 1878 | int i, copy; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1879 | |
| 1880 | if (offset > (int)skb->len - len) |
| 1881 | goto fault; |
| 1882 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1883 | if ((copy = start - offset) > 0) { |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1884 | if (copy > len) |
| 1885 | copy = len; |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 1886 | skb_copy_to_linear_data_offset(skb, offset, from, copy); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1887 | if ((len -= copy) == 0) |
| 1888 | return 0; |
| 1889 | offset += copy; |
| 1890 | from += copy; |
| 1891 | } |
| 1892 | |
| 1893 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1894 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1895 | int end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1896 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1897 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1898 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1899 | end = start + skb_frag_size(frag); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1900 | if ((copy = end - offset) > 0) { |
| 1901 | u8 *vaddr; |
| 1902 | |
| 1903 | if (copy > len) |
| 1904 | copy = len; |
| 1905 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1906 | vaddr = kmap_atomic(skb_frag_page(frag)); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1907 | memcpy(vaddr + frag->page_offset + offset - start, |
| 1908 | from, copy); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1909 | kunmap_atomic(vaddr); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1910 | |
| 1911 | if ((len -= copy) == 0) |
| 1912 | return 0; |
| 1913 | offset += copy; |
| 1914 | from += copy; |
| 1915 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1916 | start = end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1919 | skb_walk_frags(skb, frag_iter) { |
| 1920 | int end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1921 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1922 | WARN_ON(start > offset + len); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1923 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1924 | end = start + frag_iter->len; |
| 1925 | if ((copy = end - offset) > 0) { |
| 1926 | if (copy > len) |
| 1927 | copy = len; |
| 1928 | if (skb_store_bits(frag_iter, offset - start, |
| 1929 | from, copy)) |
| 1930 | goto fault; |
| 1931 | if ((len -= copy) == 0) |
| 1932 | return 0; |
| 1933 | offset += copy; |
| 1934 | from += copy; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1935 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1936 | start = end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1937 | } |
| 1938 | if (!len) |
| 1939 | return 0; |
| 1940 | |
| 1941 | fault: |
| 1942 | return -EFAULT; |
| 1943 | } |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1944 | EXPORT_SYMBOL(skb_store_bits); |
| 1945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | /* Checksum skb data. */ |
| 1947 | |
Al Viro | 2bbbc86 | 2006-11-14 21:37:14 -0800 | [diff] [blame] | 1948 | __wsum skb_checksum(const struct sk_buff *skb, int offset, |
| 1949 | int len, __wsum csum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1951 | int start = skb_headlen(skb); |
| 1952 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1953 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | int pos = 0; |
| 1955 | |
| 1956 | /* Checksum header. */ |
| 1957 | if (copy > 0) { |
| 1958 | if (copy > len) |
| 1959 | copy = len; |
| 1960 | csum = csum_partial(skb->data + offset, copy, csum); |
| 1961 | if ((len -= copy) == 0) |
| 1962 | return csum; |
| 1963 | offset += copy; |
| 1964 | pos = copy; |
| 1965 | } |
| 1966 | |
| 1967 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1968 | int end; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1969 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1971 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1972 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1973 | end = start + skb_frag_size(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | if ((copy = end - offset) > 0) { |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 1975 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | |
| 1978 | if (copy > len) |
| 1979 | copy = len; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1980 | vaddr = kmap_atomic(skb_frag_page(frag)); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1981 | csum2 = csum_partial(vaddr + frag->page_offset + |
| 1982 | offset - start, copy, 0); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1983 | kunmap_atomic(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | csum = csum_block_add(csum, csum2, pos); |
| 1985 | if (!(len -= copy)) |
| 1986 | return csum; |
| 1987 | offset += copy; |
| 1988 | pos += copy; |
| 1989 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1990 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1993 | skb_walk_frags(skb, frag_iter) { |
| 1994 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1996 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1998 | end = start + frag_iter->len; |
| 1999 | if ((copy = end - offset) > 0) { |
| 2000 | __wsum csum2; |
| 2001 | if (copy > len) |
| 2002 | copy = len; |
| 2003 | csum2 = skb_checksum(frag_iter, offset - start, |
| 2004 | copy, 0); |
| 2005 | csum = csum_block_add(csum, csum2, pos); |
| 2006 | if ((len -= copy) == 0) |
| 2007 | return csum; |
| 2008 | offset += copy; |
| 2009 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2011 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2013 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | |
| 2015 | return csum; |
| 2016 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2017 | EXPORT_SYMBOL(skb_checksum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
| 2019 | /* Both of above in one bottle. */ |
| 2020 | |
Al Viro | 81d7766 | 2006-11-14 21:37:33 -0800 | [diff] [blame] | 2021 | __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, |
| 2022 | u8 *to, int len, __wsum csum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2024 | int start = skb_headlen(skb); |
| 2025 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2026 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | int pos = 0; |
| 2028 | |
| 2029 | /* Copy header. */ |
| 2030 | if (copy > 0) { |
| 2031 | if (copy > len) |
| 2032 | copy = len; |
| 2033 | csum = csum_partial_copy_nocheck(skb->data + offset, to, |
| 2034 | copy, csum); |
| 2035 | if ((len -= copy) == 0) |
| 2036 | return csum; |
| 2037 | offset += copy; |
| 2038 | to += copy; |
| 2039 | pos = copy; |
| 2040 | } |
| 2041 | |
| 2042 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2043 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2045 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2046 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2047 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | if ((copy = end - offset) > 0) { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 2049 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | u8 *vaddr; |
| 2051 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 2052 | |
| 2053 | if (copy > len) |
| 2054 | copy = len; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2055 | vaddr = kmap_atomic(skb_frag_page(frag)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | csum2 = csum_partial_copy_nocheck(vaddr + |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2057 | frag->page_offset + |
| 2058 | offset - start, to, |
| 2059 | copy, 0); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2060 | kunmap_atomic(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | csum = csum_block_add(csum, csum2, pos); |
| 2062 | if (!(len -= copy)) |
| 2063 | return csum; |
| 2064 | offset += copy; |
| 2065 | to += copy; |
| 2066 | pos += copy; |
| 2067 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2068 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2071 | skb_walk_frags(skb, frag_iter) { |
| 2072 | __wsum csum2; |
| 2073 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2075 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2077 | end = start + frag_iter->len; |
| 2078 | if ((copy = end - offset) > 0) { |
| 2079 | if (copy > len) |
| 2080 | copy = len; |
| 2081 | csum2 = skb_copy_and_csum_bits(frag_iter, |
| 2082 | offset - start, |
| 2083 | to, copy, 0); |
| 2084 | csum = csum_block_add(csum, csum2, pos); |
| 2085 | if ((len -= copy) == 0) |
| 2086 | return csum; |
| 2087 | offset += copy; |
| 2088 | to += copy; |
| 2089 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2091 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2093 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | return csum; |
| 2095 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2096 | EXPORT_SYMBOL(skb_copy_and_csum_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | |
| 2098 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) |
| 2099 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 2100 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | long csstart; |
| 2102 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2103 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
Michał Mirosław | 55508d6 | 2010-12-14 15:24:08 +0000 | [diff] [blame] | 2104 | csstart = skb_checksum_start_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | else |
| 2106 | csstart = skb_headlen(skb); |
| 2107 | |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2108 | BUG_ON(csstart > skb_headlen(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2110 | skb_copy_from_linear_data(skb, to, csstart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | |
| 2112 | csum = 0; |
| 2113 | if (csstart != skb->len) |
| 2114 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, |
| 2115 | skb->len - csstart, 0); |
| 2116 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2117 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Al Viro | ff1dcad | 2006-11-20 18:07:29 -0800 | [diff] [blame] | 2118 | long csstuff = csstart + skb->csum_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 2120 | *((__sum16 *)(to + csstuff)) = csum_fold(csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | } |
| 2122 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2123 | EXPORT_SYMBOL(skb_copy_and_csum_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | |
| 2125 | /** |
| 2126 | * skb_dequeue - remove from the head of the queue |
| 2127 | * @list: list to dequeue from |
| 2128 | * |
| 2129 | * Remove the head of the list. The list lock is taken so the function |
| 2130 | * may be used safely with other locking list functions. The head item is |
| 2131 | * returned or %NULL if the list is empty. |
| 2132 | */ |
| 2133 | |
| 2134 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) |
| 2135 | { |
| 2136 | unsigned long flags; |
| 2137 | struct sk_buff *result; |
| 2138 | |
| 2139 | spin_lock_irqsave(&list->lock, flags); |
| 2140 | result = __skb_dequeue(list); |
| 2141 | spin_unlock_irqrestore(&list->lock, flags); |
| 2142 | return result; |
| 2143 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2144 | EXPORT_SYMBOL(skb_dequeue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | |
| 2146 | /** |
| 2147 | * skb_dequeue_tail - remove from the tail of the queue |
| 2148 | * @list: list to dequeue from |
| 2149 | * |
| 2150 | * Remove the tail of the list. The list lock is taken so the function |
| 2151 | * may be used safely with other locking list functions. The tail item is |
| 2152 | * returned or %NULL if the list is empty. |
| 2153 | */ |
| 2154 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) |
| 2155 | { |
| 2156 | unsigned long flags; |
| 2157 | struct sk_buff *result; |
| 2158 | |
| 2159 | spin_lock_irqsave(&list->lock, flags); |
| 2160 | result = __skb_dequeue_tail(list); |
| 2161 | spin_unlock_irqrestore(&list->lock, flags); |
| 2162 | return result; |
| 2163 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2164 | EXPORT_SYMBOL(skb_dequeue_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | |
| 2166 | /** |
| 2167 | * skb_queue_purge - empty a list |
| 2168 | * @list: list to empty |
| 2169 | * |
| 2170 | * Delete all buffers on an &sk_buff list. Each buffer is removed from |
| 2171 | * the list and one reference dropped. This function takes the list |
| 2172 | * lock and is atomic with respect to other list locking functions. |
| 2173 | */ |
| 2174 | void skb_queue_purge(struct sk_buff_head *list) |
| 2175 | { |
| 2176 | struct sk_buff *skb; |
| 2177 | while ((skb = skb_dequeue(list)) != NULL) |
| 2178 | kfree_skb(skb); |
| 2179 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2180 | EXPORT_SYMBOL(skb_queue_purge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | |
| 2182 | /** |
| 2183 | * skb_queue_head - queue a buffer at the list head |
| 2184 | * @list: list to use |
| 2185 | * @newsk: buffer to queue |
| 2186 | * |
| 2187 | * Queue a buffer at the start of the list. This function takes the |
| 2188 | * list lock and can be used safely with other locking &sk_buff functions |
| 2189 | * safely. |
| 2190 | * |
| 2191 | * A buffer cannot be placed on two lists at the same time. |
| 2192 | */ |
| 2193 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) |
| 2194 | { |
| 2195 | unsigned long flags; |
| 2196 | |
| 2197 | spin_lock_irqsave(&list->lock, flags); |
| 2198 | __skb_queue_head(list, newsk); |
| 2199 | spin_unlock_irqrestore(&list->lock, flags); |
| 2200 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2201 | EXPORT_SYMBOL(skb_queue_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
| 2203 | /** |
| 2204 | * skb_queue_tail - queue a buffer at the list tail |
| 2205 | * @list: list to use |
| 2206 | * @newsk: buffer to queue |
| 2207 | * |
| 2208 | * Queue a buffer at the tail of the list. This function takes the |
| 2209 | * list lock and can be used safely with other locking &sk_buff functions |
| 2210 | * safely. |
| 2211 | * |
| 2212 | * A buffer cannot be placed on two lists at the same time. |
| 2213 | */ |
| 2214 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) |
| 2215 | { |
| 2216 | unsigned long flags; |
| 2217 | |
| 2218 | spin_lock_irqsave(&list->lock, flags); |
| 2219 | __skb_queue_tail(list, newsk); |
| 2220 | spin_unlock_irqrestore(&list->lock, flags); |
| 2221 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2222 | EXPORT_SYMBOL(skb_queue_tail); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | /** |
| 2225 | * skb_unlink - remove a buffer from a list |
| 2226 | * @skb: buffer to remove |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2227 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2229 | * Remove a packet from a list. The list locks are taken and this |
| 2230 | * function is atomic with respect to other list locked calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2232 | * You must know what list the SKB is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2234 | void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2236 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2238 | spin_lock_irqsave(&list->lock, flags); |
| 2239 | __skb_unlink(skb, list); |
| 2240 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2242 | EXPORT_SYMBOL(skb_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | /** |
| 2245 | * skb_append - append a buffer |
| 2246 | * @old: buffer to insert after |
| 2247 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2248 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | * |
| 2250 | * Place a packet after a given packet in a list. The list locks are taken |
| 2251 | * and this function is atomic with respect to other list locked calls. |
| 2252 | * A buffer cannot be placed on two lists at the same time. |
| 2253 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2254 | void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | { |
| 2256 | unsigned long flags; |
| 2257 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2258 | spin_lock_irqsave(&list->lock, flags); |
Gerrit Renker | 7de6c03 | 2008-04-14 00:05:09 -0700 | [diff] [blame] | 2259 | __skb_queue_after(list, old, newsk); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2260 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2262 | EXPORT_SYMBOL(skb_append); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | |
| 2264 | /** |
| 2265 | * skb_insert - insert a buffer |
| 2266 | * @old: buffer to insert before |
| 2267 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2268 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2270 | * Place a packet before a given packet in a list. The list locks are |
| 2271 | * taken and this function is atomic with respect to other list locked |
| 2272 | * calls. |
| 2273 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | * A buffer cannot be placed on two lists at the same time. |
| 2275 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2276 | void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | { |
| 2278 | unsigned long flags; |
| 2279 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2280 | spin_lock_irqsave(&list->lock, flags); |
| 2281 | __skb_insert(newsk, old->prev, old, list); |
| 2282 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2284 | EXPORT_SYMBOL(skb_insert); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | static inline void skb_split_inside_header(struct sk_buff *skb, |
| 2287 | struct sk_buff* skb1, |
| 2288 | const u32 len, const int pos) |
| 2289 | { |
| 2290 | int i; |
| 2291 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2292 | skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), |
| 2293 | pos - len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | /* And move data appendix as is. */ |
| 2295 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 2296 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 2297 | |
| 2298 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; |
| 2299 | skb_shinfo(skb)->nr_frags = 0; |
| 2300 | skb1->data_len = skb->data_len; |
| 2301 | skb1->len += skb1->data_len; |
| 2302 | skb->data_len = 0; |
| 2303 | skb->len = len; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 2304 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | static inline void skb_split_no_header(struct sk_buff *skb, |
| 2308 | struct sk_buff* skb1, |
| 2309 | const u32 len, int pos) |
| 2310 | { |
| 2311 | int i, k = 0; |
| 2312 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 2313 | |
| 2314 | skb_shinfo(skb)->nr_frags = 0; |
| 2315 | skb1->len = skb1->data_len = skb->len - len; |
| 2316 | skb->len = len; |
| 2317 | skb->data_len = len - pos; |
| 2318 | |
| 2319 | for (i = 0; i < nfrags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2320 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | |
| 2322 | if (pos + size > len) { |
| 2323 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 2324 | |
| 2325 | if (pos < len) { |
| 2326 | /* Split frag. |
| 2327 | * We have two variants in this case: |
| 2328 | * 1. Move all the frag to the second |
| 2329 | * part, if it is possible. F.e. |
| 2330 | * this approach is mandatory for TUX, |
| 2331 | * where splitting is expensive. |
| 2332 | * 2. Split is accurately. We make this. |
| 2333 | */ |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2334 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | skb_shinfo(skb1)->frags[0].page_offset += len - pos; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2336 | skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); |
| 2337 | skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | skb_shinfo(skb)->nr_frags++; |
| 2339 | } |
| 2340 | k++; |
| 2341 | } else |
| 2342 | skb_shinfo(skb)->nr_frags++; |
| 2343 | pos += size; |
| 2344 | } |
| 2345 | skb_shinfo(skb1)->nr_frags = k; |
| 2346 | } |
| 2347 | |
| 2348 | /** |
| 2349 | * skb_split - Split fragmented skb to two parts at length len. |
| 2350 | * @skb: the buffer to split |
| 2351 | * @skb1: the buffer to receive the second part |
| 2352 | * @len: new length for skb |
| 2353 | */ |
| 2354 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) |
| 2355 | { |
| 2356 | int pos = skb_headlen(skb); |
| 2357 | |
| 2358 | if (len < pos) /* Split line is inside header. */ |
| 2359 | skb_split_inside_header(skb, skb1, len, pos); |
| 2360 | else /* Second chunk has no header, nothing to copy. */ |
| 2361 | skb_split_no_header(skb, skb1, len, pos); |
| 2362 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2363 | EXPORT_SYMBOL(skb_split); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | |
Ilpo Järvinen | 9f782db | 2008-11-25 13:57:01 -0800 | [diff] [blame] | 2365 | /* Shifting from/to a cloned skb is a no-go. |
| 2366 | * |
| 2367 | * Caller cannot keep skb_shinfo related pointers past calling here! |
| 2368 | */ |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2369 | static int skb_prepare_for_shift(struct sk_buff *skb) |
| 2370 | { |
Ilpo Järvinen | 0ace285 | 2008-11-24 21:30:21 -0800 | [diff] [blame] | 2371 | return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | /** |
| 2375 | * skb_shift - Shifts paged data partially from skb to another |
| 2376 | * @tgt: buffer into which tail data gets added |
| 2377 | * @skb: buffer from which the paged data comes from |
| 2378 | * @shiftlen: shift up to this many bytes |
| 2379 | * |
| 2380 | * Attempts to shift up to shiftlen worth of bytes, which may be less than |
Feng King | 20e994a | 2011-11-21 01:47:11 +0000 | [diff] [blame] | 2381 | * the length of the skb, from skb to tgt. Returns number bytes shifted. |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2382 | * It's up to caller to free skb if everything was shifted. |
| 2383 | * |
| 2384 | * If @tgt runs out of frags, the whole operation is aborted. |
| 2385 | * |
| 2386 | * Skb cannot include anything else but paged data while tgt is allowed |
| 2387 | * to have non-paged data as well. |
| 2388 | * |
| 2389 | * TODO: full sized shift could be optimized but that would need |
| 2390 | * specialized skb free'er to handle frags without up-to-date nr_frags. |
| 2391 | */ |
| 2392 | int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) |
| 2393 | { |
| 2394 | int from, to, merge, todo; |
| 2395 | struct skb_frag_struct *fragfrom, *fragto; |
| 2396 | |
| 2397 | BUG_ON(shiftlen > skb->len); |
| 2398 | BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ |
| 2399 | |
| 2400 | todo = shiftlen; |
| 2401 | from = 0; |
| 2402 | to = skb_shinfo(tgt)->nr_frags; |
| 2403 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 2404 | |
| 2405 | /* Actual merge is delayed until the point when we know we can |
| 2406 | * commit all, so that we don't have to undo partial changes |
| 2407 | */ |
| 2408 | if (!to || |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2409 | !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), |
| 2410 | fragfrom->page_offset)) { |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2411 | merge = -1; |
| 2412 | } else { |
| 2413 | merge = to - 1; |
| 2414 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2415 | todo -= skb_frag_size(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2416 | if (todo < 0) { |
| 2417 | if (skb_prepare_for_shift(skb) || |
| 2418 | skb_prepare_for_shift(tgt)) |
| 2419 | return 0; |
| 2420 | |
Ilpo Järvinen | 9f782db | 2008-11-25 13:57:01 -0800 | [diff] [blame] | 2421 | /* All previous frag pointers might be stale! */ |
| 2422 | fragfrom = &skb_shinfo(skb)->frags[from]; |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2423 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 2424 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2425 | skb_frag_size_add(fragto, shiftlen); |
| 2426 | skb_frag_size_sub(fragfrom, shiftlen); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2427 | fragfrom->page_offset += shiftlen; |
| 2428 | |
| 2429 | goto onlymerged; |
| 2430 | } |
| 2431 | |
| 2432 | from++; |
| 2433 | } |
| 2434 | |
| 2435 | /* Skip full, not-fitting skb to avoid expensive operations */ |
| 2436 | if ((shiftlen == skb->len) && |
| 2437 | (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) |
| 2438 | return 0; |
| 2439 | |
| 2440 | if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) |
| 2441 | return 0; |
| 2442 | |
| 2443 | while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { |
| 2444 | if (to == MAX_SKB_FRAGS) |
| 2445 | return 0; |
| 2446 | |
| 2447 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 2448 | fragto = &skb_shinfo(tgt)->frags[to]; |
| 2449 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2450 | if (todo >= skb_frag_size(fragfrom)) { |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2451 | *fragto = *fragfrom; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2452 | todo -= skb_frag_size(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2453 | from++; |
| 2454 | to++; |
| 2455 | |
| 2456 | } else { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2457 | __skb_frag_ref(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2458 | fragto->page = fragfrom->page; |
| 2459 | fragto->page_offset = fragfrom->page_offset; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2460 | skb_frag_size_set(fragto, todo); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2461 | |
| 2462 | fragfrom->page_offset += todo; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2463 | skb_frag_size_sub(fragfrom, todo); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2464 | todo = 0; |
| 2465 | |
| 2466 | to++; |
| 2467 | break; |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | /* Ready to "commit" this state change to tgt */ |
| 2472 | skb_shinfo(tgt)->nr_frags = to; |
| 2473 | |
| 2474 | if (merge >= 0) { |
| 2475 | fragfrom = &skb_shinfo(skb)->frags[0]; |
| 2476 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 2477 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2478 | skb_frag_size_add(fragto, skb_frag_size(fragfrom)); |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2479 | __skb_frag_unref(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | /* Reposition in the original skb */ |
| 2483 | to = 0; |
| 2484 | while (from < skb_shinfo(skb)->nr_frags) |
| 2485 | skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; |
| 2486 | skb_shinfo(skb)->nr_frags = to; |
| 2487 | |
| 2488 | BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); |
| 2489 | |
| 2490 | onlymerged: |
| 2491 | /* Most likely the tgt won't ever need its checksum anymore, skb on |
| 2492 | * the other hand might need it if it needs to be resent |
| 2493 | */ |
| 2494 | tgt->ip_summed = CHECKSUM_PARTIAL; |
| 2495 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 2496 | |
| 2497 | /* Yak, is it really working this way? Some helper please? */ |
| 2498 | skb->len -= shiftlen; |
| 2499 | skb->data_len -= shiftlen; |
| 2500 | skb->truesize -= shiftlen; |
| 2501 | tgt->len += shiftlen; |
| 2502 | tgt->data_len += shiftlen; |
| 2503 | tgt->truesize += shiftlen; |
| 2504 | |
| 2505 | return shiftlen; |
| 2506 | } |
| 2507 | |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2508 | /** |
| 2509 | * skb_prepare_seq_read - Prepare a sequential read of skb data |
| 2510 | * @skb: the buffer to read |
| 2511 | * @from: lower offset of data to be read |
| 2512 | * @to: upper offset of data to be read |
| 2513 | * @st: state variable |
| 2514 | * |
| 2515 | * Initializes the specified state variable. Must be called before |
| 2516 | * invoking skb_seq_read() for the first time. |
| 2517 | */ |
| 2518 | void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, |
| 2519 | unsigned int to, struct skb_seq_state *st) |
| 2520 | { |
| 2521 | st->lower_offset = from; |
| 2522 | st->upper_offset = to; |
| 2523 | st->root_skb = st->cur_skb = skb; |
| 2524 | st->frag_idx = st->stepped_offset = 0; |
| 2525 | st->frag_data = NULL; |
| 2526 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2527 | EXPORT_SYMBOL(skb_prepare_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2528 | |
| 2529 | /** |
| 2530 | * skb_seq_read - Sequentially read skb data |
| 2531 | * @consumed: number of bytes consumed by the caller so far |
| 2532 | * @data: destination pointer for data to be returned |
| 2533 | * @st: state variable |
| 2534 | * |
| 2535 | * Reads a block of skb data at &consumed relative to the |
| 2536 | * lower offset specified to skb_prepare_seq_read(). Assigns |
| 2537 | * the head of the data block to &data and returns the length |
| 2538 | * of the block or 0 if the end of the skb data or the upper |
| 2539 | * offset has been reached. |
| 2540 | * |
| 2541 | * The caller is not required to consume all of the data |
| 2542 | * returned, i.e. &consumed is typically set to the number |
| 2543 | * of bytes already consumed and the next call to |
| 2544 | * skb_seq_read() will return the remaining part of the block. |
| 2545 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2546 | * Note 1: The size of each block of data returned can be arbitrary, |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2547 | * this limitation is the cost for zerocopy seqeuental |
| 2548 | * reads of potentially non linear data. |
| 2549 | * |
Randy Dunlap | bc2cda1 | 2008-02-13 15:03:25 -0800 | [diff] [blame] | 2550 | * Note 2: Fragment lists within fragments are not implemented |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2551 | * at the moment, state->root_skb could be replaced with |
| 2552 | * a stack for this purpose. |
| 2553 | */ |
| 2554 | unsigned int skb_seq_read(unsigned int consumed, const u8 **data, |
| 2555 | struct skb_seq_state *st) |
| 2556 | { |
| 2557 | unsigned int block_limit, abs_offset = consumed + st->lower_offset; |
| 2558 | skb_frag_t *frag; |
| 2559 | |
| 2560 | if (unlikely(abs_offset >= st->upper_offset)) |
| 2561 | return 0; |
| 2562 | |
| 2563 | next_skb: |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 2564 | block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2565 | |
Thomas Chenault | 995b337 | 2009-05-18 21:43:27 -0700 | [diff] [blame] | 2566 | if (abs_offset < block_limit && !st->frag_data) { |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 2567 | *data = st->cur_skb->data + (abs_offset - st->stepped_offset); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2568 | return block_limit - abs_offset; |
| 2569 | } |
| 2570 | |
| 2571 | if (st->frag_idx == 0 && !st->frag_data) |
| 2572 | st->stepped_offset += skb_headlen(st->cur_skb); |
| 2573 | |
| 2574 | while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { |
| 2575 | frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2576 | block_limit = skb_frag_size(frag) + st->stepped_offset; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2577 | |
| 2578 | if (abs_offset < block_limit) { |
| 2579 | if (!st->frag_data) |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2580 | st->frag_data = kmap_atomic(skb_frag_page(frag)); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2581 | |
| 2582 | *data = (u8 *) st->frag_data + frag->page_offset + |
| 2583 | (abs_offset - st->stepped_offset); |
| 2584 | |
| 2585 | return block_limit - abs_offset; |
| 2586 | } |
| 2587 | |
| 2588 | if (st->frag_data) { |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2589 | kunmap_atomic(st->frag_data); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2590 | st->frag_data = NULL; |
| 2591 | } |
| 2592 | |
| 2593 | st->frag_idx++; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2594 | st->stepped_offset += skb_frag_size(frag); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2595 | } |
| 2596 | |
Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 2597 | if (st->frag_data) { |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2598 | kunmap_atomic(st->frag_data); |
Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 2599 | st->frag_data = NULL; |
| 2600 | } |
| 2601 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 2602 | if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { |
Shyam Iyer | 71b3346 | 2009-01-29 16:12:42 -0800 | [diff] [blame] | 2603 | st->cur_skb = skb_shinfo(st->root_skb)->frag_list; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2604 | st->frag_idx = 0; |
| 2605 | goto next_skb; |
Shyam Iyer | 71b3346 | 2009-01-29 16:12:42 -0800 | [diff] [blame] | 2606 | } else if (st->cur_skb->next) { |
| 2607 | st->cur_skb = st->cur_skb->next; |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 2608 | st->frag_idx = 0; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2609 | goto next_skb; |
| 2610 | } |
| 2611 | |
| 2612 | return 0; |
| 2613 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2614 | EXPORT_SYMBOL(skb_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2615 | |
| 2616 | /** |
| 2617 | * skb_abort_seq_read - Abort a sequential read of skb data |
| 2618 | * @st: state variable |
| 2619 | * |
| 2620 | * Must be called if skb_seq_read() was not called until it |
| 2621 | * returned 0. |
| 2622 | */ |
| 2623 | void skb_abort_seq_read(struct skb_seq_state *st) |
| 2624 | { |
| 2625 | if (st->frag_data) |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2626 | kunmap_atomic(st->frag_data); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2627 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2628 | EXPORT_SYMBOL(skb_abort_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2629 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2630 | #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) |
| 2631 | |
| 2632 | static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, |
| 2633 | struct ts_config *conf, |
| 2634 | struct ts_state *state) |
| 2635 | { |
| 2636 | return skb_seq_read(offset, text, TS_SKB_CB(state)); |
| 2637 | } |
| 2638 | |
| 2639 | static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) |
| 2640 | { |
| 2641 | skb_abort_seq_read(TS_SKB_CB(state)); |
| 2642 | } |
| 2643 | |
| 2644 | /** |
| 2645 | * skb_find_text - Find a text pattern in skb data |
| 2646 | * @skb: the buffer to look in |
| 2647 | * @from: search offset |
| 2648 | * @to: search limit |
| 2649 | * @config: textsearch configuration |
| 2650 | * @state: uninitialized textsearch state variable |
| 2651 | * |
| 2652 | * Finds a pattern in the skb data according to the specified |
| 2653 | * textsearch configuration. Use textsearch_next() to retrieve |
| 2654 | * subsequent occurrences of the pattern. Returns the offset |
| 2655 | * to the first occurrence or UINT_MAX if no match was found. |
| 2656 | */ |
| 2657 | unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, |
| 2658 | unsigned int to, struct ts_config *config, |
| 2659 | struct ts_state *state) |
| 2660 | { |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 2661 | unsigned int ret; |
| 2662 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2663 | config->get_next_block = skb_ts_get_next_block; |
| 2664 | config->finish = skb_ts_finish; |
| 2665 | |
| 2666 | skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); |
| 2667 | |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 2668 | ret = textsearch_find(config, state); |
| 2669 | return (ret <= to - from ? ret : UINT_MAX); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2670 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2671 | EXPORT_SYMBOL(skb_find_text); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2672 | |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2673 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 2674 | * skb_append_datato_frags - append the user data to a skb |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2675 | * @sk: sock structure |
| 2676 | * @skb: skb structure to be appened with user data. |
| 2677 | * @getfrag: call back function to be used for getting the user data |
| 2678 | * @from: pointer to user message iov |
| 2679 | * @length: length of the iov message |
| 2680 | * |
| 2681 | * Description: This procedure append the user data in the fragment part |
| 2682 | * of the skb if any page alloc fails user this procedure returns -ENOMEM |
| 2683 | */ |
| 2684 | int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, |
Martin Waitz | dab9630 | 2005-12-05 13:40:12 -0800 | [diff] [blame] | 2685 | int (*getfrag)(void *from, char *to, int offset, |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2686 | int len, int odd, struct sk_buff *skb), |
| 2687 | void *from, int length) |
| 2688 | { |
| 2689 | int frg_cnt = 0; |
| 2690 | skb_frag_t *frag = NULL; |
| 2691 | struct page *page = NULL; |
| 2692 | int copy, left; |
| 2693 | int offset = 0; |
| 2694 | int ret; |
| 2695 | |
| 2696 | do { |
| 2697 | /* Return error if we don't have space for new frag */ |
| 2698 | frg_cnt = skb_shinfo(skb)->nr_frags; |
| 2699 | if (frg_cnt >= MAX_SKB_FRAGS) |
| 2700 | return -EFAULT; |
| 2701 | |
| 2702 | /* allocate a new page for next frag */ |
| 2703 | page = alloc_pages(sk->sk_allocation, 0); |
| 2704 | |
| 2705 | /* If alloc_page fails just return failure and caller will |
| 2706 | * free previous allocated pages by doing kfree_skb() |
| 2707 | */ |
| 2708 | if (page == NULL) |
| 2709 | return -ENOMEM; |
| 2710 | |
| 2711 | /* initialize the next frag */ |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2712 | skb_fill_page_desc(skb, frg_cnt, page, 0, 0); |
| 2713 | skb->truesize += PAGE_SIZE; |
| 2714 | atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); |
| 2715 | |
| 2716 | /* get the new initialized frag */ |
| 2717 | frg_cnt = skb_shinfo(skb)->nr_frags; |
| 2718 | frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; |
| 2719 | |
| 2720 | /* copy the user data to page */ |
| 2721 | left = PAGE_SIZE - frag->page_offset; |
| 2722 | copy = (length > left)? left : length; |
| 2723 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2724 | ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag), |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2725 | offset, copy, 0, skb); |
| 2726 | if (ret < 0) |
| 2727 | return -EFAULT; |
| 2728 | |
| 2729 | /* copy was successful so update the size parameters */ |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2730 | skb_frag_size_add(frag, copy); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2731 | skb->len += copy; |
| 2732 | skb->data_len += copy; |
| 2733 | offset += copy; |
| 2734 | length -= copy; |
| 2735 | |
| 2736 | } while (length > 0); |
| 2737 | |
| 2738 | return 0; |
| 2739 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2740 | EXPORT_SYMBOL(skb_append_datato_frags); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2741 | |
Herbert Xu | cbb042f9 | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2742 | /** |
| 2743 | * skb_pull_rcsum - pull skb and update receive checksum |
| 2744 | * @skb: buffer to update |
Herbert Xu | cbb042f9 | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2745 | * @len: length of data pulled |
| 2746 | * |
| 2747 | * This function performs an skb_pull on the packet and updates |
Urs Thuermann | fee54fa | 2008-02-12 22:03:25 -0800 | [diff] [blame] | 2748 | * the CHECKSUM_COMPLETE checksum. It should be used on |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2749 | * receive path processing instead of skb_pull unless you know |
| 2750 | * that the checksum difference is zero (e.g., a valid IP header) |
| 2751 | * or you are setting ip_summed to CHECKSUM_NONE. |
Herbert Xu | cbb042f9 | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2752 | */ |
| 2753 | unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) |
| 2754 | { |
| 2755 | BUG_ON(len > skb->len); |
| 2756 | skb->len -= len; |
| 2757 | BUG_ON(skb->len < skb->data_len); |
| 2758 | skb_postpull_rcsum(skb, skb->data, len); |
| 2759 | return skb->data += len; |
| 2760 | } |
Arnaldo Carvalho de Melo | f94691a | 2006-03-20 22:47:55 -0800 | [diff] [blame] | 2761 | EXPORT_SYMBOL_GPL(skb_pull_rcsum); |
| 2762 | |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2763 | /** |
| 2764 | * skb_segment - Perform protocol segmentation on skb. |
| 2765 | * @skb: buffer to segment |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 2766 | * @features: features for the output path (see dev->features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2767 | * |
| 2768 | * This function performs segmentation on the given skb. It returns |
Ben Hutchings | 4c821d7 | 2008-04-13 21:52:48 -0700 | [diff] [blame] | 2769 | * a pointer to the first in a list of new skbs for the segments. |
| 2770 | * In case of error it returns ERR_PTR(err). |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2771 | */ |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2772 | struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2773 | { |
| 2774 | struct sk_buff *segs = NULL; |
| 2775 | struct sk_buff *tail = NULL; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2776 | struct sk_buff *fskb = skb_shinfo(skb)->frag_list; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2777 | unsigned int mss = skb_shinfo(skb)->gso_size; |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 2778 | unsigned int doffset = skb->data - skb_mac_header(skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2779 | unsigned int offset = doffset; |
| 2780 | unsigned int headroom; |
| 2781 | unsigned int len; |
Michał Mirosław | 04ed3e7 | 2011-01-24 15:32:47 -0800 | [diff] [blame] | 2782 | int sg = !!(features & NETIF_F_SG); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2783 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 2784 | int err = -ENOMEM; |
| 2785 | int i = 0; |
| 2786 | int pos; |
| 2787 | |
| 2788 | __skb_push(skb, doffset); |
| 2789 | headroom = skb_headroom(skb); |
| 2790 | pos = skb_headlen(skb); |
| 2791 | |
| 2792 | do { |
| 2793 | struct sk_buff *nskb; |
| 2794 | skb_frag_t *frag; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 2795 | int hsize; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2796 | int size; |
| 2797 | |
| 2798 | len = skb->len - offset; |
| 2799 | if (len > mss) |
| 2800 | len = mss; |
| 2801 | |
| 2802 | hsize = skb_headlen(skb) - offset; |
| 2803 | if (hsize < 0) |
| 2804 | hsize = 0; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 2805 | if (hsize > len || !sg) |
| 2806 | hsize = len; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2807 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2808 | if (!hsize && i >= nfrags) { |
| 2809 | BUG_ON(fskb->len != len); |
| 2810 | |
| 2811 | pos += len; |
| 2812 | nskb = skb_clone(fskb, GFP_ATOMIC); |
| 2813 | fskb = fskb->next; |
| 2814 | |
| 2815 | if (unlikely(!nskb)) |
| 2816 | goto err; |
| 2817 | |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 2818 | hsize = skb_end_offset(nskb); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2819 | if (skb_cow_head(nskb, doffset + headroom)) { |
| 2820 | kfree_skb(nskb); |
| 2821 | goto err; |
| 2822 | } |
| 2823 | |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 2824 | nskb->truesize += skb_end_offset(nskb) - hsize; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2825 | skb_release_head_state(nskb); |
| 2826 | __skb_push(nskb, doffset); |
| 2827 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 2828 | nskb = __alloc_skb(hsize + doffset + headroom, |
| 2829 | GFP_ATOMIC, skb_alloc_rx_flag(skb), |
| 2830 | NUMA_NO_NODE); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2831 | |
| 2832 | if (unlikely(!nskb)) |
| 2833 | goto err; |
| 2834 | |
| 2835 | skb_reserve(nskb, headroom); |
| 2836 | __skb_put(nskb, doffset); |
| 2837 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2838 | |
| 2839 | if (segs) |
| 2840 | tail->next = nskb; |
| 2841 | else |
| 2842 | segs = nskb; |
| 2843 | tail = nskb; |
| 2844 | |
Herbert Xu | 6f85a12 | 2008-08-15 14:55:02 -0700 | [diff] [blame] | 2845 | __copy_skb_header(nskb, skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2846 | nskb->mac_len = skb->mac_len; |
| 2847 | |
Eric Dumazet | 3d3be43 | 2010-09-01 00:50:51 +0000 | [diff] [blame] | 2848 | /* nskb and skb might have different headroom */ |
| 2849 | if (nskb->ip_summed == CHECKSUM_PARTIAL) |
| 2850 | nskb->csum_start += skb_headroom(nskb) - headroom; |
| 2851 | |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 2852 | skb_reset_mac_header(nskb); |
Arnaldo Carvalho de Melo | ddc7b8e | 2007-03-15 21:42:27 -0300 | [diff] [blame] | 2853 | skb_set_network_header(nskb, skb->mac_len); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 2854 | nskb->transport_header = (nskb->network_header + |
| 2855 | skb_network_header_len(skb)); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2856 | skb_copy_from_linear_data(skb, nskb->data, doffset); |
| 2857 | |
Herbert Xu | 2f18185 | 2009-03-28 23:39:18 -0700 | [diff] [blame] | 2858 | if (fskb != skb_shinfo(skb)->frag_list) |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2859 | continue; |
| 2860 | |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2861 | if (!sg) { |
Herbert Xu | 6f85a12 | 2008-08-15 14:55:02 -0700 | [diff] [blame] | 2862 | nskb->ip_summed = CHECKSUM_NONE; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2863 | nskb->csum = skb_copy_and_csum_bits(skb, offset, |
| 2864 | skb_put(nskb, len), |
| 2865 | len, 0); |
| 2866 | continue; |
| 2867 | } |
| 2868 | |
| 2869 | frag = skb_shinfo(nskb)->frags; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2870 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2871 | skb_copy_from_linear_data_offset(skb, offset, |
| 2872 | skb_put(nskb, hsize), hsize); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2873 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2874 | while (pos < offset + len && i < nfrags) { |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2875 | *frag = skb_shinfo(skb)->frags[i]; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2876 | __skb_frag_ref(frag); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2877 | size = skb_frag_size(frag); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2878 | |
| 2879 | if (pos < offset) { |
| 2880 | frag->page_offset += offset - pos; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2881 | skb_frag_size_sub(frag, offset - pos); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2882 | } |
| 2883 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2884 | skb_shinfo(nskb)->nr_frags++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2885 | |
| 2886 | if (pos + size <= offset + len) { |
| 2887 | i++; |
| 2888 | pos += size; |
| 2889 | } else { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2890 | skb_frag_size_sub(frag, pos + size - (offset + len)); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2891 | goto skip_fraglist; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | frag++; |
| 2895 | } |
| 2896 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2897 | if (pos < offset + len) { |
| 2898 | struct sk_buff *fskb2 = fskb; |
| 2899 | |
| 2900 | BUG_ON(pos + fskb->len != offset + len); |
| 2901 | |
| 2902 | pos += fskb->len; |
| 2903 | fskb = fskb->next; |
| 2904 | |
| 2905 | if (fskb2->next) { |
| 2906 | fskb2 = skb_clone(fskb2, GFP_ATOMIC); |
| 2907 | if (!fskb2) |
| 2908 | goto err; |
| 2909 | } else |
| 2910 | skb_get(fskb2); |
| 2911 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2912 | SKB_FRAG_ASSERT(nskb); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2913 | skb_shinfo(nskb)->frag_list = fskb2; |
| 2914 | } |
| 2915 | |
| 2916 | skip_fraglist: |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2917 | nskb->data_len = len - hsize; |
| 2918 | nskb->len += nskb->data_len; |
| 2919 | nskb->truesize += nskb->data_len; |
| 2920 | } while ((offset += len) < skb->len); |
| 2921 | |
| 2922 | return segs; |
| 2923 | |
| 2924 | err: |
| 2925 | while ((skb = segs)) { |
| 2926 | segs = skb->next; |
Patrick McHardy | b08d584 | 2007-02-27 09:57:37 -0800 | [diff] [blame] | 2927 | kfree_skb(skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2928 | } |
| 2929 | return ERR_PTR(err); |
| 2930 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2931 | EXPORT_SYMBOL_GPL(skb_segment); |
| 2932 | |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 2933 | int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) |
| 2934 | { |
| 2935 | struct sk_buff *p = *head; |
| 2936 | struct sk_buff *nskb; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 2937 | struct skb_shared_info *skbinfo = skb_shinfo(skb); |
| 2938 | struct skb_shared_info *pinfo = skb_shinfo(p); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 2939 | unsigned int headroom; |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 2940 | unsigned int len = skb_gro_len(skb); |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 2941 | unsigned int offset = skb_gro_offset(skb); |
| 2942 | unsigned int headlen = skb_headlen(skb); |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 2943 | unsigned int delta_truesize; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 2944 | |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 2945 | if (p->len + len >= 65536) |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 2946 | return -E2BIG; |
| 2947 | |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 2948 | if (pinfo->frag_list) |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 2949 | goto merge; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 2950 | else if (headlen <= offset) { |
Herbert Xu | 42da699 | 2009-05-26 18:50:19 +0000 | [diff] [blame] | 2951 | skb_frag_t *frag; |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 2952 | skb_frag_t *frag2; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 2953 | int i = skbinfo->nr_frags; |
| 2954 | int nr_frags = pinfo->nr_frags + i; |
Herbert Xu | 42da699 | 2009-05-26 18:50:19 +0000 | [diff] [blame] | 2955 | |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 2956 | offset -= headlen; |
| 2957 | |
| 2958 | if (nr_frags > MAX_SKB_FRAGS) |
Herbert Xu | 81705ad | 2009-01-29 14:19:51 +0000 | [diff] [blame] | 2959 | return -E2BIG; |
| 2960 | |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 2961 | pinfo->nr_frags = nr_frags; |
| 2962 | skbinfo->nr_frags = 0; |
Herbert Xu | f557206 | 2009-01-14 20:40:03 -0800 | [diff] [blame] | 2963 | |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 2964 | frag = pinfo->frags + nr_frags; |
| 2965 | frag2 = skbinfo->frags + i; |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 2966 | do { |
| 2967 | *--frag = *--frag2; |
| 2968 | } while (--i); |
| 2969 | |
| 2970 | frag->page_offset += offset; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2971 | skb_frag_size_sub(frag, offset); |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 2972 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 2973 | /* all fragments truesize : remove (head size + sk_buff) */ |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 2974 | delta_truesize = skb->truesize - |
| 2975 | SKB_TRUESIZE(skb_end_offset(skb)); |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 2976 | |
Herbert Xu | f557206 | 2009-01-14 20:40:03 -0800 | [diff] [blame] | 2977 | skb->truesize -= skb->data_len; |
| 2978 | skb->len -= skb->data_len; |
| 2979 | skb->data_len = 0; |
| 2980 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 2981 | NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 2982 | goto done; |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 2983 | } else if (skb->head_frag) { |
| 2984 | int nr_frags = pinfo->nr_frags; |
| 2985 | skb_frag_t *frag = pinfo->frags + nr_frags; |
| 2986 | struct page *page = virt_to_head_page(skb->head); |
| 2987 | unsigned int first_size = headlen - offset; |
| 2988 | unsigned int first_offset; |
| 2989 | |
| 2990 | if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) |
| 2991 | return -E2BIG; |
| 2992 | |
| 2993 | first_offset = skb->data - |
| 2994 | (unsigned char *)page_address(page) + |
| 2995 | offset; |
| 2996 | |
| 2997 | pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; |
| 2998 | |
| 2999 | frag->page.p = page; |
| 3000 | frag->page_offset = first_offset; |
| 3001 | skb_frag_size_set(frag, first_size); |
| 3002 | |
| 3003 | memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); |
| 3004 | /* We dont need to clear skbinfo->nr_frags here */ |
| 3005 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3006 | delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 3007 | NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; |
| 3008 | goto done; |
Herbert Xu | 69c0cab | 2009-11-17 05:18:18 -0800 | [diff] [blame] | 3009 | } else if (skb_gro_len(p) != pinfo->gso_size) |
| 3010 | return -E2BIG; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3011 | |
| 3012 | headroom = skb_headroom(p); |
Eric Dumazet | 3d3be43 | 2010-09-01 00:50:51 +0000 | [diff] [blame] | 3013 | nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3014 | if (unlikely(!nskb)) |
| 3015 | return -ENOMEM; |
| 3016 | |
| 3017 | __copy_skb_header(nskb, p); |
| 3018 | nskb->mac_len = p->mac_len; |
| 3019 | |
| 3020 | skb_reserve(nskb, headroom); |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3021 | __skb_put(nskb, skb_gro_offset(p)); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3022 | |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3023 | skb_set_mac_header(nskb, skb_mac_header(p) - p->data); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3024 | skb_set_network_header(nskb, skb_network_offset(p)); |
| 3025 | skb_set_transport_header(nskb, skb_transport_offset(p)); |
| 3026 | |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3027 | __skb_pull(p, skb_gro_offset(p)); |
| 3028 | memcpy(skb_mac_header(nskb), skb_mac_header(p), |
| 3029 | p->data - skb_mac_header(p)); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3030 | |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3031 | skb_shinfo(nskb)->frag_list = p; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3032 | skb_shinfo(nskb)->gso_size = pinfo->gso_size; |
Herbert Xu | 622e0ca | 2010-05-20 23:07:56 -0700 | [diff] [blame] | 3033 | pinfo->gso_size = 0; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3034 | skb_header_release(p); |
Eric Dumazet | c3c7c25 | 2012-12-06 13:54:59 +0000 | [diff] [blame] | 3035 | NAPI_GRO_CB(nskb)->last = p; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3036 | |
| 3037 | nskb->data_len += p->len; |
Eric Dumazet | de8261c | 2012-02-13 04:09:20 +0000 | [diff] [blame] | 3038 | nskb->truesize += p->truesize; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3039 | nskb->len += p->len; |
| 3040 | |
| 3041 | *head = nskb; |
| 3042 | nskb->next = p->next; |
| 3043 | p->next = NULL; |
| 3044 | |
| 3045 | p = nskb; |
| 3046 | |
| 3047 | merge: |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3048 | delta_truesize = skb->truesize; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3049 | if (offset > headlen) { |
Michal Schmidt | d1dc7ab | 2011-01-24 12:08:48 +0000 | [diff] [blame] | 3050 | unsigned int eat = offset - headlen; |
| 3051 | |
| 3052 | skbinfo->frags[0].page_offset += eat; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3053 | skb_frag_size_sub(&skbinfo->frags[0], eat); |
Michal Schmidt | d1dc7ab | 2011-01-24 12:08:48 +0000 | [diff] [blame] | 3054 | skb->data_len -= eat; |
| 3055 | skb->len -= eat; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3056 | offset = headlen; |
Herbert Xu | 5603502 | 2009-02-05 21:26:52 -0800 | [diff] [blame] | 3057 | } |
| 3058 | |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3059 | __skb_pull(skb, offset); |
Herbert Xu | 5603502 | 2009-02-05 21:26:52 -0800 | [diff] [blame] | 3060 | |
Eric Dumazet | c3c7c25 | 2012-12-06 13:54:59 +0000 | [diff] [blame] | 3061 | NAPI_GRO_CB(p)->last->next = skb; |
| 3062 | NAPI_GRO_CB(p)->last = skb; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3063 | skb_header_release(skb); |
| 3064 | |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3065 | done: |
| 3066 | NAPI_GRO_CB(p)->count++; |
Herbert Xu | 37fe473 | 2009-01-17 19:48:13 +0000 | [diff] [blame] | 3067 | p->data_len += len; |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3068 | p->truesize += delta_truesize; |
Herbert Xu | 37fe473 | 2009-01-17 19:48:13 +0000 | [diff] [blame] | 3069 | p->len += len; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3070 | |
| 3071 | NAPI_GRO_CB(skb)->same_flow = 1; |
| 3072 | return 0; |
| 3073 | } |
| 3074 | EXPORT_SYMBOL_GPL(skb_gro_receive); |
| 3075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | void __init skb_init(void) |
| 3077 | { |
| 3078 | skbuff_head_cache = kmem_cache_create("skbuff_head_cache", |
| 3079 | sizeof(struct sk_buff), |
| 3080 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 3081 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3082 | NULL); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 3083 | skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", |
| 3084 | (2*sizeof(struct sk_buff)) + |
| 3085 | sizeof(atomic_t), |
| 3086 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 3087 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3088 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | } |
| 3090 | |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3091 | /** |
| 3092 | * skb_to_sgvec - Fill a scatter-gather list from a socket buffer |
| 3093 | * @skb: Socket buffer containing the buffers to be mapped |
| 3094 | * @sg: The scatter-gather list to map into |
| 3095 | * @offset: The offset into the buffer's contents to start mapping |
| 3096 | * @len: Length of buffer space to be mapped |
| 3097 | * |
| 3098 | * Fill the specified scatter-gather list with mappings/pointers into a |
| 3099 | * region of the buffer space attached to a socket buffer. |
| 3100 | */ |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3101 | static int |
| 3102 | __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3103 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3104 | int start = skb_headlen(skb); |
| 3105 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3106 | struct sk_buff *frag_iter; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3107 | int elt = 0; |
| 3108 | |
| 3109 | if (copy > 0) { |
| 3110 | if (copy > len) |
| 3111 | copy = len; |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 3112 | sg_set_buf(sg, skb->data + offset, copy); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3113 | elt++; |
| 3114 | if ((len -= copy) == 0) |
| 3115 | return elt; |
| 3116 | offset += copy; |
| 3117 | } |
| 3118 | |
| 3119 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3120 | int end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3121 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 3122 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3123 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3124 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3125 | if ((copy = end - offset) > 0) { |
| 3126 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 3127 | |
| 3128 | if (copy > len) |
| 3129 | copy = len; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3130 | sg_set_page(&sg[elt], skb_frag_page(frag), copy, |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 3131 | frag->page_offset+offset-start); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3132 | elt++; |
| 3133 | if (!(len -= copy)) |
| 3134 | return elt; |
| 3135 | offset += copy; |
| 3136 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3137 | start = end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3138 | } |
| 3139 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3140 | skb_walk_frags(skb, frag_iter) { |
| 3141 | int end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3142 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3143 | WARN_ON(start > offset + len); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3144 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3145 | end = start + frag_iter->len; |
| 3146 | if ((copy = end - offset) > 0) { |
| 3147 | if (copy > len) |
| 3148 | copy = len; |
| 3149 | elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, |
| 3150 | copy); |
| 3151 | if ((len -= copy) == 0) |
| 3152 | return elt; |
| 3153 | offset += copy; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3154 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3155 | start = end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3156 | } |
| 3157 | BUG_ON(len); |
| 3158 | return elt; |
| 3159 | } |
| 3160 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3161 | int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) |
| 3162 | { |
| 3163 | int nsg = __skb_to_sgvec(skb, sg, offset, len); |
| 3164 | |
Jens Axboe | c46f233 | 2007-10-31 12:06:37 +0100 | [diff] [blame] | 3165 | sg_mark_end(&sg[nsg - 1]); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3166 | |
| 3167 | return nsg; |
| 3168 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3169 | EXPORT_SYMBOL_GPL(skb_to_sgvec); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3170 | |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3171 | /** |
| 3172 | * skb_cow_data - Check that a socket buffer's data buffers are writable |
| 3173 | * @skb: The socket buffer to check. |
| 3174 | * @tailbits: Amount of trailing space to be added |
| 3175 | * @trailer: Returned pointer to the skb where the @tailbits space begins |
| 3176 | * |
| 3177 | * Make sure that the data buffers attached to a socket buffer are |
| 3178 | * writable. If they are not, private copies are made of the data buffers |
| 3179 | * and the socket buffer is set to use these instead. |
| 3180 | * |
| 3181 | * If @tailbits is given, make sure that there is space to write @tailbits |
| 3182 | * bytes of data beyond current end of socket buffer. @trailer will be |
| 3183 | * set to point to the skb in which this space begins. |
| 3184 | * |
| 3185 | * The number of scatterlist elements required to completely map the |
| 3186 | * COW'd and extended socket buffer will be returned. |
| 3187 | */ |
| 3188 | int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) |
| 3189 | { |
| 3190 | int copyflag; |
| 3191 | int elt; |
| 3192 | struct sk_buff *skb1, **skb_p; |
| 3193 | |
| 3194 | /* If skb is cloned or its head is paged, reallocate |
| 3195 | * head pulling out all the pages (pages are considered not writable |
| 3196 | * at the moment even if they are anonymous). |
| 3197 | */ |
| 3198 | if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && |
| 3199 | __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) |
| 3200 | return -ENOMEM; |
| 3201 | |
| 3202 | /* Easy case. Most of packets will go this way. */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3203 | if (!skb_has_frag_list(skb)) { |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3204 | /* A little of trouble, not enough of space for trailer. |
| 3205 | * This should not happen, when stack is tuned to generate |
| 3206 | * good frames. OK, on miss we reallocate and reserve even more |
| 3207 | * space, 128 bytes is fair. */ |
| 3208 | |
| 3209 | if (skb_tailroom(skb) < tailbits && |
| 3210 | pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) |
| 3211 | return -ENOMEM; |
| 3212 | |
| 3213 | /* Voila! */ |
| 3214 | *trailer = skb; |
| 3215 | return 1; |
| 3216 | } |
| 3217 | |
| 3218 | /* Misery. We are in troubles, going to mincer fragments... */ |
| 3219 | |
| 3220 | elt = 1; |
| 3221 | skb_p = &skb_shinfo(skb)->frag_list; |
| 3222 | copyflag = 0; |
| 3223 | |
| 3224 | while ((skb1 = *skb_p) != NULL) { |
| 3225 | int ntail = 0; |
| 3226 | |
| 3227 | /* The fragment is partially pulled by someone, |
| 3228 | * this can happen on input. Copy it and everything |
| 3229 | * after it. */ |
| 3230 | |
| 3231 | if (skb_shared(skb1)) |
| 3232 | copyflag = 1; |
| 3233 | |
| 3234 | /* If the skb is the last, worry about trailer. */ |
| 3235 | |
| 3236 | if (skb1->next == NULL && tailbits) { |
| 3237 | if (skb_shinfo(skb1)->nr_frags || |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3238 | skb_has_frag_list(skb1) || |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3239 | skb_tailroom(skb1) < tailbits) |
| 3240 | ntail = tailbits + 128; |
| 3241 | } |
| 3242 | |
| 3243 | if (copyflag || |
| 3244 | skb_cloned(skb1) || |
| 3245 | ntail || |
| 3246 | skb_shinfo(skb1)->nr_frags || |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3247 | skb_has_frag_list(skb1)) { |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3248 | struct sk_buff *skb2; |
| 3249 | |
| 3250 | /* Fuck, we are miserable poor guys... */ |
| 3251 | if (ntail == 0) |
| 3252 | skb2 = skb_copy(skb1, GFP_ATOMIC); |
| 3253 | else |
| 3254 | skb2 = skb_copy_expand(skb1, |
| 3255 | skb_headroom(skb1), |
| 3256 | ntail, |
| 3257 | GFP_ATOMIC); |
| 3258 | if (unlikely(skb2 == NULL)) |
| 3259 | return -ENOMEM; |
| 3260 | |
| 3261 | if (skb1->sk) |
| 3262 | skb_set_owner_w(skb2, skb1->sk); |
| 3263 | |
| 3264 | /* Looking around. Are we still alive? |
| 3265 | * OK, link new skb, drop old one */ |
| 3266 | |
| 3267 | skb2->next = skb1->next; |
| 3268 | *skb_p = skb2; |
| 3269 | kfree_skb(skb1); |
| 3270 | skb1 = skb2; |
| 3271 | } |
| 3272 | elt++; |
| 3273 | *trailer = skb1; |
| 3274 | skb_p = &skb1->next; |
| 3275 | } |
| 3276 | |
| 3277 | return elt; |
| 3278 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3279 | EXPORT_SYMBOL_GPL(skb_cow_data); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3280 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3281 | static void sock_rmem_free(struct sk_buff *skb) |
| 3282 | { |
| 3283 | struct sock *sk = skb->sk; |
| 3284 | |
| 3285 | atomic_sub(skb->truesize, &sk->sk_rmem_alloc); |
| 3286 | } |
| 3287 | |
| 3288 | /* |
| 3289 | * Note: We dont mem charge error packets (no sk_forward_alloc changes) |
| 3290 | */ |
| 3291 | int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) |
| 3292 | { |
Eric Dumazet | 110c433 | 2012-04-06 10:49:10 +0200 | [diff] [blame] | 3293 | int len = skb->len; |
| 3294 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3295 | if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3296 | (unsigned int)sk->sk_rcvbuf) |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3297 | return -ENOMEM; |
| 3298 | |
| 3299 | skb_orphan(skb); |
| 3300 | skb->sk = sk; |
| 3301 | skb->destructor = sock_rmem_free; |
| 3302 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); |
| 3303 | |
Eric Dumazet | abb57ea | 2011-05-18 02:21:31 -0400 | [diff] [blame] | 3304 | /* before exiting rcu section, make sure dst is refcounted */ |
| 3305 | skb_dst_force(skb); |
| 3306 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3307 | skb_queue_tail(&sk->sk_error_queue, skb); |
| 3308 | if (!sock_flag(sk, SOCK_DEAD)) |
Eric Dumazet | 110c433 | 2012-04-06 10:49:10 +0200 | [diff] [blame] | 3309 | sk->sk_data_ready(sk, len); |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3310 | return 0; |
| 3311 | } |
| 3312 | EXPORT_SYMBOL(sock_queue_err_skb); |
| 3313 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3314 | void skb_tstamp_tx(struct sk_buff *orig_skb, |
| 3315 | struct skb_shared_hwtstamps *hwtstamps) |
| 3316 | { |
| 3317 | struct sock *sk = orig_skb->sk; |
| 3318 | struct sock_exterr_skb *serr; |
| 3319 | struct sk_buff *skb; |
| 3320 | int err; |
| 3321 | |
| 3322 | if (!sk) |
| 3323 | return; |
| 3324 | |
| 3325 | skb = skb_clone(orig_skb, GFP_ATOMIC); |
| 3326 | if (!skb) |
| 3327 | return; |
| 3328 | |
| 3329 | if (hwtstamps) { |
| 3330 | *skb_hwtstamps(skb) = |
| 3331 | *hwtstamps; |
| 3332 | } else { |
| 3333 | /* |
| 3334 | * no hardware time stamps available, |
Oliver Hartkopp | 2244d07 | 2010-08-17 08:59:14 +0000 | [diff] [blame] | 3335 | * so keep the shared tx_flags and only |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3336 | * store software time stamp |
| 3337 | */ |
| 3338 | skb->tstamp = ktime_get_real(); |
| 3339 | } |
| 3340 | |
| 3341 | serr = SKB_EXT_ERR(skb); |
| 3342 | memset(serr, 0, sizeof(*serr)); |
| 3343 | serr->ee.ee_errno = ENOMSG; |
| 3344 | serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; |
Eric Dumazet | 2903037 | 2010-05-29 00:20:48 -0700 | [diff] [blame] | 3345 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3346 | err = sock_queue_err_skb(sk, skb); |
Eric Dumazet | 2903037 | 2010-05-29 00:20:48 -0700 | [diff] [blame] | 3347 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3348 | if (err) |
| 3349 | kfree_skb(skb); |
| 3350 | } |
| 3351 | EXPORT_SYMBOL_GPL(skb_tstamp_tx); |
| 3352 | |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 3353 | void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) |
| 3354 | { |
| 3355 | struct sock *sk = skb->sk; |
| 3356 | struct sock_exterr_skb *serr; |
| 3357 | int err; |
| 3358 | |
| 3359 | skb->wifi_acked_valid = 1; |
| 3360 | skb->wifi_acked = acked; |
| 3361 | |
| 3362 | serr = SKB_EXT_ERR(skb); |
| 3363 | memset(serr, 0, sizeof(*serr)); |
| 3364 | serr->ee.ee_errno = ENOMSG; |
| 3365 | serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; |
| 3366 | |
| 3367 | err = sock_queue_err_skb(sk, skb); |
| 3368 | if (err) |
| 3369 | kfree_skb(skb); |
| 3370 | } |
| 3371 | EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); |
| 3372 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3373 | |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3374 | /** |
| 3375 | * skb_partial_csum_set - set up and verify partial csum values for packet |
| 3376 | * @skb: the skb to set |
| 3377 | * @start: the number of bytes after skb->data to start checksumming. |
| 3378 | * @off: the offset from start to place the checksum. |
| 3379 | * |
| 3380 | * For untrusted partially-checksummed packets, we need to make sure the values |
| 3381 | * for skb->csum_start and skb->csum_offset are valid so we don't oops. |
| 3382 | * |
| 3383 | * This function checks and sets those values and skb->ip_summed: if this |
| 3384 | * returns false you should drop the packet. |
| 3385 | */ |
| 3386 | bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) |
| 3387 | { |
Herbert Xu | 5ff8dda | 2009-06-04 01:22:01 +0000 | [diff] [blame] | 3388 | if (unlikely(start > skb_headlen(skb)) || |
| 3389 | unlikely((int)start + off > skb_headlen(skb) - 2)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 3390 | net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", |
| 3391 | start, off, skb_headlen(skb)); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3392 | return false; |
| 3393 | } |
| 3394 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 3395 | skb->csum_start = skb_headroom(skb) + start; |
| 3396 | skb->csum_offset = off; |
| 3397 | return true; |
| 3398 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3399 | EXPORT_SYMBOL_GPL(skb_partial_csum_set); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3400 | |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 3401 | void __skb_warn_lro_forwarding(const struct sk_buff *skb) |
| 3402 | { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 3403 | net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", |
| 3404 | skb->dev->name); |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 3405 | } |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 3406 | EXPORT_SYMBOL(__skb_warn_lro_forwarding); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3407 | |
| 3408 | void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) |
| 3409 | { |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 3410 | if (head_stolen) { |
| 3411 | skb_release_head_state(skb); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3412 | kmem_cache_free(skbuff_head_cache, skb); |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 3413 | } else { |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3414 | __kfree_skb(skb); |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 3415 | } |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3416 | } |
| 3417 | EXPORT_SYMBOL(kfree_skb_partial); |
| 3418 | |
| 3419 | /** |
| 3420 | * skb_try_coalesce - try to merge skb to prior one |
| 3421 | * @to: prior buffer |
| 3422 | * @from: buffer to add |
| 3423 | * @fragstolen: pointer to boolean |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 3424 | * @delta_truesize: how much more was allocated than was requested |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3425 | */ |
| 3426 | bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, |
| 3427 | bool *fragstolen, int *delta_truesize) |
| 3428 | { |
| 3429 | int i, delta, len = from->len; |
| 3430 | |
| 3431 | *fragstolen = false; |
| 3432 | |
| 3433 | if (skb_cloned(to)) |
| 3434 | return false; |
| 3435 | |
| 3436 | if (len <= skb_tailroom(to)) { |
| 3437 | BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); |
| 3438 | *delta_truesize = 0; |
| 3439 | return true; |
| 3440 | } |
| 3441 | |
| 3442 | if (skb_has_frag_list(to) || skb_has_frag_list(from)) |
| 3443 | return false; |
| 3444 | |
| 3445 | if (skb_headlen(from) != 0) { |
| 3446 | struct page *page; |
| 3447 | unsigned int offset; |
| 3448 | |
| 3449 | if (skb_shinfo(to)->nr_frags + |
| 3450 | skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) |
| 3451 | return false; |
| 3452 | |
| 3453 | if (skb_head_is_locked(from)) |
| 3454 | return false; |
| 3455 | |
| 3456 | delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
| 3457 | |
| 3458 | page = virt_to_head_page(from->head); |
| 3459 | offset = from->data - (unsigned char *)page_address(page); |
| 3460 | |
| 3461 | skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, |
| 3462 | page, offset, skb_headlen(from)); |
| 3463 | *fragstolen = true; |
| 3464 | } else { |
| 3465 | if (skb_shinfo(to)->nr_frags + |
| 3466 | skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) |
| 3467 | return false; |
| 3468 | |
Weiping Pan | f4b549a | 2012-09-28 20:15:30 +0000 | [diff] [blame] | 3469 | delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | WARN_ON_ONCE(delta < len); |
| 3473 | |
| 3474 | memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, |
| 3475 | skb_shinfo(from)->frags, |
| 3476 | skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); |
| 3477 | skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; |
| 3478 | |
| 3479 | if (!skb_cloned(from)) |
| 3480 | skb_shinfo(from)->nr_frags = 0; |
| 3481 | |
Li RongQing | 8ea853f | 2012-09-18 16:53:21 +0000 | [diff] [blame] | 3482 | /* if the skb is not cloned this does nothing |
| 3483 | * since we set nr_frags to 0. |
| 3484 | */ |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3485 | for (i = 0; i < skb_shinfo(from)->nr_frags; i++) |
| 3486 | skb_frag_ref(from, i); |
| 3487 | |
| 3488 | to->truesize += delta; |
| 3489 | to->len += len; |
| 3490 | to->data_len += len; |
| 3491 | |
| 3492 | *delta_truesize = delta; |
| 3493 | return true; |
| 3494 | } |
| 3495 | EXPORT_SYMBOL(skb_try_coalesce); |