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