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