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> |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 50 | #include <linux/tcp.h> |
| 51 | #include <linux/udp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/netdevice.h> |
| 53 | #ifdef CONFIG_NET_CLS_ACT |
| 54 | #include <net/pkt_sched.h> |
| 55 | #endif |
| 56 | #include <linux/string.h> |
| 57 | #include <linux/skbuff.h> |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 58 | #include <linux/splice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/cache.h> |
| 60 | #include <linux/rtnetlink.h> |
| 61 | #include <linux/init.h> |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 62 | #include <linux/scatterlist.h> |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 63 | #include <linux/errqueue.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 64 | #include <linux/prefetch.h> |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 65 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | #include <net/protocol.h> |
| 68 | #include <net/dst.h> |
| 69 | #include <net/sock.h> |
| 70 | #include <net/checksum.h> |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 71 | #include <net/ip6_checksum.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <net/xfrm.h> |
| 73 | |
| 74 | #include <asm/uaccess.h> |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 75 | #include <trace/events/skb.h> |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 76 | #include <linux/highmem.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 77 | |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 78 | struct kmem_cache *skbuff_head_cache __read_mostly; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 79 | static struct kmem_cache *skbuff_fclone_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /** |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 82 | * skb_panic - private function for out-of-line support |
| 83 | * @skb: buffer |
| 84 | * @sz: size |
| 85 | * @addr: address |
James Hogan | 99d5851 | 2013-02-13 11:20:27 +0000 | [diff] [blame] | 86 | * @msg: skb_over_panic or skb_under_panic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 88 | * Out-of-line support for skb_put() and skb_push(). |
| 89 | * Called via the wrapper skb_over_panic() or skb_under_panic(). |
| 90 | * Keep out of line to prevent kernel bloat. |
| 91 | * __builtin_return_address is not used because it is not always reliable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 93 | static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, |
James Hogan | 99d5851 | 2013-02-13 11:20:27 +0000 | [diff] [blame] | 94 | const char msg[]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 96 | pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", |
James Hogan | 99d5851 | 2013-02-13 11:20:27 +0000 | [diff] [blame] | 97 | msg, addr, skb->len, sz, skb->head, skb->data, |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 98 | (unsigned long)skb->tail, (unsigned long)skb->end, |
| 99 | skb->dev ? skb->dev->name : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | BUG(); |
| 101 | } |
| 102 | |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 103 | static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 105 | skb_panic(skb, sz, addr, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 108 | static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
| 109 | { |
| 110 | skb_panic(skb, sz, addr, __func__); |
| 111 | } |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells |
| 115 | * the caller if emergency pfmemalloc reserves are being used. If it is and |
| 116 | * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves |
| 117 | * may be used. Otherwise, the packet data may be discarded until enough |
| 118 | * memory is free |
| 119 | */ |
| 120 | #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ |
| 121 | __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) |
stephen hemminger | 61c5e88 | 2012-12-28 18:24:28 +0000 | [diff] [blame] | 122 | |
| 123 | static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, |
| 124 | unsigned long ip, bool *pfmemalloc) |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 125 | { |
| 126 | void *obj; |
| 127 | bool ret_pfmemalloc = false; |
| 128 | |
| 129 | /* |
| 130 | * Try a regular allocation, when that fails and we're not entitled |
| 131 | * to the reserves, fail. |
| 132 | */ |
| 133 | obj = kmalloc_node_track_caller(size, |
| 134 | flags | __GFP_NOMEMALLOC | __GFP_NOWARN, |
| 135 | node); |
| 136 | if (obj || !(gfp_pfmemalloc_allowed(flags))) |
| 137 | goto out; |
| 138 | |
| 139 | /* Try again but now we are using pfmemalloc reserves */ |
| 140 | ret_pfmemalloc = true; |
| 141 | obj = kmalloc_node_track_caller(size, flags, node); |
| 142 | |
| 143 | out: |
| 144 | if (pfmemalloc) |
| 145 | *pfmemalloc = ret_pfmemalloc; |
| 146 | |
| 147 | return obj; |
| 148 | } |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
| 151 | * 'private' fields and also do memory statistics to find all the |
| 152 | * [BEEP] leaks. |
| 153 | * |
| 154 | */ |
| 155 | |
Patrick McHardy | 0ebd0ac | 2013-04-17 06:46:58 +0000 | [diff] [blame] | 156 | struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node) |
| 157 | { |
| 158 | struct sk_buff *skb; |
| 159 | |
| 160 | /* Get the HEAD */ |
| 161 | skb = kmem_cache_alloc_node(skbuff_head_cache, |
| 162 | gfp_mask & ~__GFP_DMA, node); |
| 163 | if (!skb) |
| 164 | goto out; |
| 165 | |
| 166 | /* |
| 167 | * Only clear those fields we need to clear, not those that we will |
| 168 | * actually initialise below. Hence, don't put any more fields after |
| 169 | * the tail pointer in struct sk_buff! |
| 170 | */ |
| 171 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
Pablo Neira | 5e71d9d | 2013-06-03 09:28:43 +0000 | [diff] [blame] | 172 | skb->head = NULL; |
Patrick McHardy | 0ebd0ac | 2013-04-17 06:46:58 +0000 | [diff] [blame] | 173 | skb->truesize = sizeof(struct sk_buff); |
| 174 | atomic_set(&skb->users, 1); |
| 175 | |
Cong Wang | 35d0461 | 2013-05-29 15:16:05 +0800 | [diff] [blame] | 176 | skb->mac_header = (typeof(skb->mac_header))~0U; |
Patrick McHardy | 0ebd0ac | 2013-04-17 06:46:58 +0000 | [diff] [blame] | 177 | out: |
| 178 | return skb; |
| 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /** |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 182 | * __alloc_skb - allocate a network buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | * @size: size to allocate |
| 184 | * @gfp_mask: allocation mask |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 185 | * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache |
| 186 | * instead of head cache and allocate a cloned (child) skb. |
| 187 | * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for |
| 188 | * allocations in case the data is required for writeback |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 189 | * @node: numa node to allocate memory on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | * |
| 191 | * 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] | 192 | * tail room of at least size bytes. The object has a reference count |
| 193 | * 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] | 194 | * |
| 195 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 196 | * %GFP_ATOMIC. |
| 197 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 198 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 199 | int flags, int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 201 | struct kmem_cache *cache; |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 202 | struct skb_shared_info *shinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | struct sk_buff *skb; |
| 204 | u8 *data; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 205 | bool pfmemalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 207 | cache = (flags & SKB_ALLOC_FCLONE) |
| 208 | ? skbuff_fclone_cache : skbuff_head_cache; |
| 209 | |
| 210 | if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) |
| 211 | gfp_mask |= __GFP_MEMALLOC; |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | /* Get the HEAD */ |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 214 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | if (!skb) |
| 216 | goto out; |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 217 | prefetchw(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 219 | /* We do our best to align skb_shared_info on a separate cache |
| 220 | * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives |
| 221 | * aligned memory blocks, unless SLUB/SLAB debug is enabled. |
| 222 | * Both skb->head and skb_shared_info are cache line aligned. |
| 223 | */ |
Tony Lindgren | bc417e3 | 2011-11-02 13:40:28 +0000 | [diff] [blame] | 224 | size = SKB_DATA_ALIGN(size); |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 225 | size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 226 | data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | if (!data) |
| 228 | goto nodata; |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 229 | /* kmalloc(size) might give us more room than requested. |
| 230 | * Put skb_shared_info exactly at the end of allocated zone, |
| 231 | * to allow max possible filling before reallocation. |
| 232 | */ |
| 233 | size = SKB_WITH_OVERHEAD(ksize(data)); |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 234 | prefetchw(data + size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Arnaldo Carvalho de Melo | ca0605a | 2007-03-19 10:48:59 -0300 | [diff] [blame] | 236 | /* |
Johannes Berg | c800578 | 2008-05-03 20:56:42 -0700 | [diff] [blame] | 237 | * Only clear those fields we need to clear, not those that we will |
| 238 | * actually initialise below. Hence, don't put any more fields after |
| 239 | * the tail pointer in struct sk_buff! |
Arnaldo Carvalho de Melo | ca0605a | 2007-03-19 10:48:59 -0300 | [diff] [blame] | 240 | */ |
| 241 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 242 | /* Account for allocated memory : skb + skb->head */ |
| 243 | skb->truesize = SKB_TRUESIZE(size); |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 244 | skb->pfmemalloc = pfmemalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | atomic_set(&skb->users, 1); |
| 246 | skb->head = data; |
| 247 | skb->data = data; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 248 | skb_reset_tail_pointer(skb); |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 249 | skb->end = skb->tail + size; |
Cong Wang | 35d0461 | 2013-05-29 15:16:05 +0800 | [diff] [blame] | 250 | skb->mac_header = (typeof(skb->mac_header))~0U; |
| 251 | skb->transport_header = (typeof(skb->transport_header))~0U; |
Stephen Hemminger | 19633e1 | 2009-06-17 05:23:27 +0000 | [diff] [blame] | 252 | |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 253 | /* make sure we initialize shinfo sequentially */ |
| 254 | shinfo = skb_shinfo(skb); |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 255 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 256 | atomic_set(&shinfo->dataref, 1); |
Eric Dumazet | c2aa366 | 2011-01-25 23:18:38 +0000 | [diff] [blame] | 257 | kmemcheck_annotate_variable(shinfo->destructor_arg); |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 258 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 259 | if (flags & SKB_ALLOC_FCLONE) { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 260 | struct sk_buff_fclones *fclones; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 262 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
| 263 | |
| 264 | kmemcheck_annotate_bitfield(&fclones->skb2, flags1); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 265 | skb->fclone = SKB_FCLONE_ORIG; |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 266 | atomic_set(&fclones->fclone_ref, 1); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 267 | |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 268 | fclones->skb2.fclone = SKB_FCLONE_CLONE; |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 269 | fclones->skb2.pfmemalloc = pfmemalloc; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | out: |
| 272 | return skb; |
| 273 | nodata: |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 274 | kmem_cache_free(cache, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | skb = NULL; |
| 276 | goto out; |
| 277 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 278 | EXPORT_SYMBOL(__alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | /** |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 281 | * build_skb - build a network buffer |
| 282 | * @data: data buffer provided by caller |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 283 | * @frag_size: size of fragment, or 0 if head was kmalloced |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 284 | * |
| 285 | * Allocate a new &sk_buff. Caller provides space holding head and |
Florian Fainelli | deceb4c | 2013-07-23 20:22:39 +0100 | [diff] [blame] | 286 | * skb_shared_info. @data must have been allocated by kmalloc() only if |
| 287 | * @frag_size is 0, otherwise data should come from the page allocator. |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 288 | * The return is the new skb buffer. |
| 289 | * On a failure the return is %NULL, and @data is not freed. |
| 290 | * Notes : |
| 291 | * Before IO, driver allocates only data buffer where NIC put incoming frame |
| 292 | * Driver should add room at head (NET_SKB_PAD) and |
| 293 | * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) |
| 294 | * After IO, driver calls build_skb(), to allocate sk_buff and populate it |
| 295 | * before giving packet to stack. |
| 296 | * RX rings only contains data buffers, not full skbs. |
| 297 | */ |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 298 | struct sk_buff *build_skb(void *data, unsigned int frag_size) |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 299 | { |
| 300 | struct skb_shared_info *shinfo; |
| 301 | struct sk_buff *skb; |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 302 | unsigned int size = frag_size ? : ksize(data); |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 303 | |
| 304 | skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); |
| 305 | if (!skb) |
| 306 | return NULL; |
| 307 | |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 308 | size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 309 | |
| 310 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
| 311 | skb->truesize = SKB_TRUESIZE(size); |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 312 | skb->head_frag = frag_size != 0; |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 313 | atomic_set(&skb->users, 1); |
| 314 | skb->head = data; |
| 315 | skb->data = data; |
| 316 | skb_reset_tail_pointer(skb); |
| 317 | skb->end = skb->tail + size; |
Cong Wang | 35d0461 | 2013-05-29 15:16:05 +0800 | [diff] [blame] | 318 | skb->mac_header = (typeof(skb->mac_header))~0U; |
| 319 | skb->transport_header = (typeof(skb->transport_header))~0U; |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 320 | |
| 321 | /* make sure we initialize shinfo sequentially */ |
| 322 | shinfo = skb_shinfo(skb); |
| 323 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
| 324 | atomic_set(&shinfo->dataref, 1); |
| 325 | kmemcheck_annotate_variable(shinfo->destructor_arg); |
| 326 | |
| 327 | return skb; |
| 328 | } |
| 329 | EXPORT_SYMBOL(build_skb); |
| 330 | |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 331 | struct netdev_alloc_cache { |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 332 | struct page_frag frag; |
| 333 | /* we maintain a pagecount bias, so that we dont dirty cache line |
| 334 | * containing page->_count every time we allocate a fragment. |
| 335 | */ |
| 336 | unsigned int pagecnt_bias; |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 337 | }; |
| 338 | static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache); |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame^] | 339 | static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 340 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame^] | 341 | static struct page *__page_frag_refill(struct netdev_alloc_cache *nc, |
| 342 | gfp_t gfp_mask) |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 343 | { |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame^] | 344 | const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER; |
| 345 | struct page *page = NULL; |
| 346 | gfp_t gfp = gfp_mask; |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 347 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame^] | 348 | if (order) { |
| 349 | gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY; |
| 350 | page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order); |
| 351 | nc->frag.size = PAGE_SIZE << (page ? order : 0); |
| 352 | } |
| 353 | |
| 354 | if (unlikely(!page)) |
| 355 | page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); |
| 356 | |
| 357 | nc->frag.page = page; |
| 358 | |
| 359 | return page; |
| 360 | } |
| 361 | |
| 362 | static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache, |
| 363 | unsigned int fragsz, gfp_t gfp_mask) |
| 364 | { |
| 365 | struct netdev_alloc_cache *nc = this_cpu_ptr(cache); |
| 366 | struct page *page = nc->frag.page; |
| 367 | unsigned int size; |
| 368 | int offset; |
| 369 | |
| 370 | if (unlikely(!page)) { |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 371 | refill: |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame^] | 372 | page = __page_frag_refill(nc, gfp_mask); |
| 373 | if (!page) |
| 374 | return NULL; |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 375 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame^] | 376 | /* if size can vary use frag.size else just use PAGE_SIZE */ |
| 377 | size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE; |
| 378 | |
Eric Dumazet | 4c45058 | 2014-10-10 04:48:18 -0700 | [diff] [blame] | 379 | /* Even if we own the page, we do not use atomic_set(). |
| 380 | * This would break get_page_unless_zero() users. |
| 381 | */ |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame^] | 382 | atomic_add(size - 1, &page->_count); |
| 383 | |
| 384 | /* reset page count bias and offset to start of new frag */ |
| 385 | nc->pagecnt_bias = size; |
| 386 | nc->frag.offset = size; |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 387 | } |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 388 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame^] | 389 | offset = nc->frag.offset - fragsz; |
| 390 | if (unlikely(offset < 0)) { |
| 391 | if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count)) |
| 392 | goto refill; |
| 393 | |
| 394 | /* if size can vary use frag.size else just use PAGE_SIZE */ |
| 395 | size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE; |
| 396 | |
| 397 | /* OK, page count is 0, we can safely set it */ |
| 398 | atomic_set(&page->_count, size); |
| 399 | |
| 400 | /* reset page count bias and offset to start of new frag */ |
| 401 | nc->pagecnt_bias = size; |
| 402 | offset = size - fragsz; |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 403 | } |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 404 | |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 405 | nc->pagecnt_bias--; |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame^] | 406 | nc->frag.offset = offset; |
| 407 | |
| 408 | return page_address(page) + offset; |
| 409 | } |
| 410 | |
| 411 | static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) |
| 412 | { |
| 413 | unsigned long flags; |
| 414 | void *data; |
| 415 | |
| 416 | local_irq_save(flags); |
| 417 | data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask); |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 418 | local_irq_restore(flags); |
| 419 | return data; |
| 420 | } |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 421 | |
| 422 | /** |
| 423 | * netdev_alloc_frag - allocate a page fragment |
| 424 | * @fragsz: fragment size |
| 425 | * |
| 426 | * Allocates a frag from a page for receive buffer. |
| 427 | * Uses GFP_ATOMIC allocations. |
| 428 | */ |
| 429 | void *netdev_alloc_frag(unsigned int fragsz) |
| 430 | { |
| 431 | return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); |
| 432 | } |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 433 | EXPORT_SYMBOL(netdev_alloc_frag); |
| 434 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame^] | 435 | static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) |
| 436 | { |
| 437 | return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask); |
| 438 | } |
| 439 | |
| 440 | void *napi_alloc_frag(unsigned int fragsz) |
| 441 | { |
| 442 | return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); |
| 443 | } |
| 444 | EXPORT_SYMBOL(napi_alloc_frag); |
| 445 | |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 446 | /** |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 447 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device |
| 448 | * @dev: network device to receive on |
| 449 | * @length: length to allocate |
| 450 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
| 451 | * |
| 452 | * Allocate a new &sk_buff and assign it a usage count of one. The |
| 453 | * buffer has unspecified headroom built in. Users should allocate |
| 454 | * the headroom they think they need without accounting for the |
| 455 | * built in space. The built in space is used for optimisations. |
| 456 | * |
| 457 | * %NULL is returned if there is no free memory. |
| 458 | */ |
| 459 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 460 | unsigned int length, gfp_t gfp_mask) |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 461 | { |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 462 | struct sk_buff *skb = NULL; |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 463 | unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) + |
| 464 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 465 | |
Eric Dumazet | 310e158 | 2012-07-16 13:15:52 +0200 | [diff] [blame] | 466 | if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 467 | void *data; |
| 468 | |
| 469 | if (sk_memalloc_socks()) |
| 470 | gfp_mask |= __GFP_MEMALLOC; |
| 471 | |
| 472 | data = __netdev_alloc_frag(fragsz, gfp_mask); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 473 | |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 474 | if (likely(data)) { |
| 475 | skb = build_skb(data, fragsz); |
| 476 | if (unlikely(!skb)) |
| 477 | put_page(virt_to_head_page(data)); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 478 | } |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 479 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 480 | skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, |
| 481 | SKB_ALLOC_RX, NUMA_NO_NODE); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 482 | } |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 483 | if (likely(skb)) { |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 484 | skb_reserve(skb, NET_SKB_PAD); |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 485 | skb->dev = dev; |
| 486 | } |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 487 | return skb; |
| 488 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 489 | EXPORT_SYMBOL(__netdev_alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 491 | 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] | 492 | int size, unsigned int truesize) |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 493 | { |
| 494 | skb_fill_page_desc(skb, i, page, off, size); |
| 495 | skb->len += size; |
| 496 | skb->data_len += size; |
Eric Dumazet | 50269e1 | 2012-03-23 23:59:33 +0000 | [diff] [blame] | 497 | skb->truesize += truesize; |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 498 | } |
| 499 | EXPORT_SYMBOL(skb_add_rx_frag); |
| 500 | |
Jason Wang | f8e617e | 2013-11-01 14:07:47 +0800 | [diff] [blame] | 501 | void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, |
| 502 | unsigned int truesize) |
| 503 | { |
| 504 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 505 | |
| 506 | skb_frag_size_add(frag, size); |
| 507 | skb->len += size; |
| 508 | skb->data_len += size; |
| 509 | skb->truesize += truesize; |
| 510 | } |
| 511 | EXPORT_SYMBOL(skb_coalesce_rx_frag); |
| 512 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 513 | static void skb_drop_list(struct sk_buff **listp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | { |
Eric Dumazet | bd8a703 | 2013-06-24 06:26:00 -0700 | [diff] [blame] | 515 | kfree_skb_list(*listp); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 516 | *listp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 519 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
| 520 | { |
| 521 | skb_drop_list(&skb_shinfo(skb)->frag_list); |
| 522 | } |
| 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | static void skb_clone_fraglist(struct sk_buff *skb) |
| 525 | { |
| 526 | struct sk_buff *list; |
| 527 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 528 | skb_walk_frags(skb, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | skb_get(list); |
| 530 | } |
| 531 | |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 532 | static void skb_free_head(struct sk_buff *skb) |
| 533 | { |
| 534 | if (skb->head_frag) |
| 535 | put_page(virt_to_head_page(skb->head)); |
| 536 | else |
| 537 | kfree(skb->head); |
| 538 | } |
| 539 | |
Adrian Bunk | 5bba171 | 2006-06-29 13:02:35 -0700 | [diff] [blame] | 540 | static void skb_release_data(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 542 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 543 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 545 | if (skb->cloned && |
| 546 | atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, |
| 547 | &shinfo->dataref)) |
| 548 | return; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 549 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 550 | for (i = 0; i < shinfo->nr_frags; i++) |
| 551 | __skb_frag_unref(&shinfo->frags[i]); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 552 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 553 | /* |
| 554 | * If skb buf is from userspace, we need to notify the caller |
| 555 | * the lower device DMA has done; |
| 556 | */ |
| 557 | if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) { |
| 558 | struct ubuf_info *uarg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 560 | uarg = shinfo->destructor_arg; |
| 561 | if (uarg->callback) |
| 562 | uarg->callback(uarg, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 564 | |
| 565 | if (shinfo->frag_list) |
| 566 | kfree_skb_list(shinfo->frag_list); |
| 567 | |
| 568 | skb_free_head(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Free an skbuff by memory without cleaning the state. |
| 573 | */ |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 574 | static void kfree_skbmem(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 576 | struct sk_buff_fclones *fclones; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 577 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 578 | switch (skb->fclone) { |
| 579 | case SKB_FCLONE_UNAVAILABLE: |
| 580 | kmem_cache_free(skbuff_head_cache, skb); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 581 | return; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 582 | |
| 583 | case SKB_FCLONE_ORIG: |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 584 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 585 | |
| 586 | /* We usually free the clone (TX completion) before original skb |
| 587 | * This test would have no chance to be true for the clone, |
| 588 | * while here, branch prediction will be good. |
| 589 | */ |
| 590 | if (atomic_read(&fclones->fclone_ref) == 1) |
| 591 | goto fastpath; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 592 | break; |
| 593 | |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 594 | default: /* SKB_FCLONE_CLONE */ |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 595 | fclones = container_of(skb, struct sk_buff_fclones, skb2); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 596 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 597 | } |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 598 | if (!atomic_dec_and_test(&fclones->fclone_ref)) |
| 599 | return; |
| 600 | fastpath: |
| 601 | kmem_cache_free(skbuff_fclone_cache, fclones); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Lennert Buytenhek | 04a4bb5 | 2008-10-01 02:33:12 -0700 | [diff] [blame] | 604 | static void skb_release_head_state(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 606 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | #ifdef CONFIG_XFRM |
| 608 | secpath_put(skb->sp); |
| 609 | #endif |
Stephen Hemminger | 9c2b332 | 2005-04-19 22:39:42 -0700 | [diff] [blame] | 610 | if (skb->destructor) { |
| 611 | WARN_ON(in_irq()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | skb->destructor(skb); |
| 613 | } |
Igor Maravić | a3bf7ae | 2011-12-12 02:58:22 +0000 | [diff] [blame] | 614 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
Yasuyuki Kozakai | 5f79e0f | 2007-03-23 11:17:07 -0700 | [diff] [blame] | 615 | nf_conntrack_put(skb->nfct); |
KOVACS Krisztian | 2fc72c7 | 2011-01-12 20:25:08 +0100 | [diff] [blame] | 616 | #endif |
Pablo Neira Ayuso | 1109a90 | 2014-10-01 11:19:17 +0200 | [diff] [blame] | 617 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | nf_bridge_put(skb->nf_bridge); |
| 619 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | /* XXX: IS this still necessary? - JHS */ |
| 621 | #ifdef CONFIG_NET_SCHED |
| 622 | skb->tc_index = 0; |
| 623 | #ifdef CONFIG_NET_CLS_ACT |
| 624 | skb->tc_verd = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | #endif |
| 626 | #endif |
Lennert Buytenhek | 04a4bb5 | 2008-10-01 02:33:12 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /* Free everything but the sk_buff shell. */ |
| 630 | static void skb_release_all(struct sk_buff *skb) |
| 631 | { |
| 632 | skb_release_head_state(skb); |
Pablo Neira | 5e71d9d | 2013-06-03 09:28:43 +0000 | [diff] [blame] | 633 | if (likely(skb->head)) |
Patrick McHardy | 0ebd0ac | 2013-04-17 06:46:58 +0000 | [diff] [blame] | 634 | skb_release_data(skb); |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 637 | /** |
| 638 | * __kfree_skb - private function |
| 639 | * @skb: buffer |
| 640 | * |
| 641 | * Free an sk_buff. Release anything attached to the buffer. |
| 642 | * Clean the state. This is an internal helper function. Users should |
| 643 | * always call kfree_skb |
| 644 | */ |
| 645 | |
| 646 | void __kfree_skb(struct sk_buff *skb) |
| 647 | { |
| 648 | skb_release_all(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | kfree_skbmem(skb); |
| 650 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 651 | EXPORT_SYMBOL(__kfree_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | /** |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 654 | * kfree_skb - free an sk_buff |
| 655 | * @skb: buffer to free |
| 656 | * |
| 657 | * Drop a reference to the buffer and free it if the usage count has |
| 658 | * hit zero. |
| 659 | */ |
| 660 | void kfree_skb(struct sk_buff *skb) |
| 661 | { |
| 662 | if (unlikely(!skb)) |
| 663 | return; |
| 664 | if (likely(atomic_read(&skb->users) == 1)) |
| 665 | smp_rmb(); |
| 666 | else if (likely(!atomic_dec_and_test(&skb->users))) |
| 667 | return; |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 668 | trace_kfree_skb(skb, __builtin_return_address(0)); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 669 | __kfree_skb(skb); |
| 670 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 671 | EXPORT_SYMBOL(kfree_skb); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 672 | |
Eric Dumazet | bd8a703 | 2013-06-24 06:26:00 -0700 | [diff] [blame] | 673 | void kfree_skb_list(struct sk_buff *segs) |
| 674 | { |
| 675 | while (segs) { |
| 676 | struct sk_buff *next = segs->next; |
| 677 | |
| 678 | kfree_skb(segs); |
| 679 | segs = next; |
| 680 | } |
| 681 | } |
| 682 | EXPORT_SYMBOL(kfree_skb_list); |
| 683 | |
Stephen Hemminger | d1a203e | 2008-11-01 21:01:09 -0700 | [diff] [blame] | 684 | /** |
Michael S. Tsirkin | 2512117 | 2012-11-01 09:16:28 +0000 | [diff] [blame] | 685 | * skb_tx_error - report an sk_buff xmit error |
| 686 | * @skb: buffer that triggered an error |
| 687 | * |
| 688 | * Report xmit error if a device callback is tracking this skb. |
| 689 | * skb must be freed afterwards. |
| 690 | */ |
| 691 | void skb_tx_error(struct sk_buff *skb) |
| 692 | { |
| 693 | if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { |
| 694 | struct ubuf_info *uarg; |
| 695 | |
| 696 | uarg = skb_shinfo(skb)->destructor_arg; |
| 697 | if (uarg->callback) |
| 698 | uarg->callback(uarg, false); |
| 699 | skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; |
| 700 | } |
| 701 | } |
| 702 | EXPORT_SYMBOL(skb_tx_error); |
| 703 | |
| 704 | /** |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 705 | * consume_skb - free an skbuff |
| 706 | * @skb: buffer to free |
| 707 | * |
| 708 | * Drop a ref to the buffer and free it if the usage count has hit zero |
| 709 | * Functions identically to kfree_skb, but kfree_skb assumes that the frame |
| 710 | * is being dropped after a failure and notes that |
| 711 | */ |
| 712 | void consume_skb(struct sk_buff *skb) |
| 713 | { |
| 714 | if (unlikely(!skb)) |
| 715 | return; |
| 716 | if (likely(atomic_read(&skb->users) == 1)) |
| 717 | smp_rmb(); |
| 718 | else if (likely(!atomic_dec_and_test(&skb->users))) |
| 719 | return; |
Koki Sanagi | 07dc22e | 2010-08-23 18:46:12 +0900 | [diff] [blame] | 720 | trace_consume_skb(skb); |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 721 | __kfree_skb(skb); |
| 722 | } |
| 723 | EXPORT_SYMBOL(consume_skb); |
| 724 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 725 | /* Make sure a field is enclosed inside headers_start/headers_end section */ |
| 726 | #define CHECK_SKB_FIELD(field) \ |
| 727 | BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ |
| 728 | offsetof(struct sk_buff, headers_start)); \ |
| 729 | BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ |
| 730 | offsetof(struct sk_buff, headers_end)); \ |
| 731 | |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 732 | static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 733 | { |
| 734 | new->tstamp = old->tstamp; |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 735 | /* We do not copy old->sk */ |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 736 | new->dev = old->dev; |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 737 | memcpy(new->cb, old->cb, sizeof(old->cb)); |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 738 | skb_dst_copy(new, old); |
Alexey Dobriyan | def8b4f | 2008-10-28 13:24:06 -0700 | [diff] [blame] | 739 | #ifdef CONFIG_XFRM |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 740 | new->sp = secpath_get(old->sp); |
| 741 | #endif |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 742 | __nf_copy(new, old, false); |
Patrick McHardy | 6aa895b | 2008-07-14 22:49:06 -0700 | [diff] [blame] | 743 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 744 | /* Note : this field could be in headers_start/headers_end section |
| 745 | * It is not yet because we do not want to have a 16 bit hole |
| 746 | */ |
| 747 | new->queue_mapping = old->queue_mapping; |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 748 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 749 | memcpy(&new->headers_start, &old->headers_start, |
| 750 | offsetof(struct sk_buff, headers_end) - |
| 751 | offsetof(struct sk_buff, headers_start)); |
| 752 | CHECK_SKB_FIELD(protocol); |
| 753 | CHECK_SKB_FIELD(csum); |
| 754 | CHECK_SKB_FIELD(hash); |
| 755 | CHECK_SKB_FIELD(priority); |
| 756 | CHECK_SKB_FIELD(skb_iif); |
| 757 | CHECK_SKB_FIELD(vlan_proto); |
| 758 | CHECK_SKB_FIELD(vlan_tci); |
| 759 | CHECK_SKB_FIELD(transport_header); |
| 760 | CHECK_SKB_FIELD(network_header); |
| 761 | CHECK_SKB_FIELD(mac_header); |
| 762 | CHECK_SKB_FIELD(inner_protocol); |
| 763 | CHECK_SKB_FIELD(inner_transport_header); |
| 764 | CHECK_SKB_FIELD(inner_network_header); |
| 765 | CHECK_SKB_FIELD(inner_mac_header); |
| 766 | CHECK_SKB_FIELD(mark); |
| 767 | #ifdef CONFIG_NETWORK_SECMARK |
| 768 | CHECK_SKB_FIELD(secmark); |
| 769 | #endif |
Cong Wang | e0d1095 | 2013-08-01 11:10:25 +0800 | [diff] [blame] | 770 | #ifdef CONFIG_NET_RX_BUSY_POLL |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 771 | CHECK_SKB_FIELD(napi_id); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 772 | #endif |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 773 | #ifdef CONFIG_NET_SCHED |
| 774 | CHECK_SKB_FIELD(tc_index); |
| 775 | #ifdef CONFIG_NET_CLS_ACT |
| 776 | CHECK_SKB_FIELD(tc_verd); |
| 777 | #endif |
| 778 | #endif |
| 779 | |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Herbert Xu | 82c49a3 | 2009-05-22 22:11:37 +0000 | [diff] [blame] | 782 | /* |
| 783 | * You should not add any new code to this function. Add it to |
| 784 | * __copy_skb_header above instead. |
| 785 | */ |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 786 | 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] | 787 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | #define C(x) n->x = skb->x |
| 789 | |
| 790 | n->next = n->prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | n->sk = NULL; |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 792 | __copy_skb_header(n, skb); |
| 793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | C(len); |
| 795 | C(data_len); |
Alexey Dobriyan | 3e6b3b2 | 2007-03-16 15:00:46 -0700 | [diff] [blame] | 796 | C(mac_len); |
Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 797 | n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 798 | n->cloned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | n->nohdr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | n->destructor = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | C(tail); |
| 802 | C(end); |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 803 | C(head); |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 804 | C(head_frag); |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 805 | C(data); |
| 806 | C(truesize); |
| 807 | atomic_set(&n->users, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
| 809 | atomic_inc(&(skb_shinfo(skb)->dataref)); |
| 810 | skb->cloned = 1; |
| 811 | |
| 812 | return n; |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 813 | #undef C |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * skb_morph - morph one skb into another |
| 818 | * @dst: the skb to receive the contents |
| 819 | * @src: the skb to supply the contents |
| 820 | * |
| 821 | * This is identical to skb_clone except that the target skb is |
| 822 | * supplied by the user. |
| 823 | * |
| 824 | * The target skb is returned upon exit. |
| 825 | */ |
| 826 | struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) |
| 827 | { |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 828 | skb_release_all(dst); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 829 | return __skb_clone(dst, src); |
| 830 | } |
| 831 | EXPORT_SYMBOL_GPL(skb_morph); |
| 832 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 833 | /** |
| 834 | * skb_copy_ubufs - copy userspace skb frags buffers to kernel |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 835 | * @skb: the skb to modify |
| 836 | * @gfp_mask: allocation priority |
| 837 | * |
| 838 | * This must be called on SKBTX_DEV_ZEROCOPY skb. |
| 839 | * It will copy all frags into kernel and drop the reference |
| 840 | * to userspace pages. |
| 841 | * |
| 842 | * If this function is called from an interrupt gfp_mask() must be |
| 843 | * %GFP_ATOMIC. |
| 844 | * |
| 845 | * Returns 0 on success or a negative error code on failure |
| 846 | * to allocate kernel memory to copy to. |
| 847 | */ |
| 848 | int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 849 | { |
| 850 | int i; |
| 851 | int num_frags = skb_shinfo(skb)->nr_frags; |
| 852 | struct page *page, *head = NULL; |
| 853 | struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; |
| 854 | |
| 855 | for (i = 0; i < num_frags; i++) { |
| 856 | u8 *vaddr; |
| 857 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
| 858 | |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 859 | page = alloc_page(gfp_mask); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 860 | if (!page) { |
| 861 | while (head) { |
Sunghan Suh | 40dadff | 2013-07-12 16:17:23 +0900 | [diff] [blame] | 862 | struct page *next = (struct page *)page_private(head); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 863 | put_page(head); |
| 864 | head = next; |
| 865 | } |
| 866 | return -ENOMEM; |
| 867 | } |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 868 | vaddr = kmap_atomic(skb_frag_page(f)); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 869 | memcpy(page_address(page), |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 870 | vaddr + f->page_offset, skb_frag_size(f)); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 871 | kunmap_atomic(vaddr); |
Sunghan Suh | 40dadff | 2013-07-12 16:17:23 +0900 | [diff] [blame] | 872 | set_page_private(page, (unsigned long)head); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 873 | head = page; |
| 874 | } |
| 875 | |
| 876 | /* skb frags release userspace buffers */ |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 877 | for (i = 0; i < num_frags; i++) |
Ian Campbell | a8605c6 | 2011-10-19 23:01:49 +0000 | [diff] [blame] | 878 | skb_frag_unref(skb, i); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 879 | |
Michael S. Tsirkin | e19d676 | 2012-11-01 09:16:22 +0000 | [diff] [blame] | 880 | uarg->callback(uarg, false); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 881 | |
| 882 | /* skb frags point to kernel buffers */ |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 883 | for (i = num_frags - 1; i >= 0; i--) { |
| 884 | __skb_fill_page_desc(skb, i, head, 0, |
| 885 | skb_shinfo(skb)->frags[i].size); |
Sunghan Suh | 40dadff | 2013-07-12 16:17:23 +0900 | [diff] [blame] | 886 | head = (struct page *)page_private(head); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 887 | } |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 888 | |
| 889 | skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 890 | return 0; |
| 891 | } |
Michael S. Tsirkin | dcc0fb7 | 2012-07-20 09:23:20 +0000 | [diff] [blame] | 892 | EXPORT_SYMBOL_GPL(skb_copy_ubufs); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 893 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 894 | /** |
| 895 | * skb_clone - duplicate an sk_buff |
| 896 | * @skb: buffer to clone |
| 897 | * @gfp_mask: allocation priority |
| 898 | * |
| 899 | * Duplicate an &sk_buff. The new one is not owned by a socket. Both |
| 900 | * copies share the same packet data but not structure. The new |
| 901 | * buffer has a reference count of 1. If the allocation fails the |
| 902 | * function returns %NULL otherwise the new buffer is returned. |
| 903 | * |
| 904 | * If this function is called from an interrupt gfp_mask() must be |
| 905 | * %GFP_ATOMIC. |
| 906 | */ |
| 907 | |
| 908 | struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) |
| 909 | { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 910 | struct sk_buff_fclones *fclones = container_of(skb, |
| 911 | struct sk_buff_fclones, |
| 912 | skb1); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 913 | struct sk_buff *n; |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 914 | |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 915 | if (skb_orphan_frags(skb, gfp_mask)) |
| 916 | return NULL; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 917 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 918 | if (skb->fclone == SKB_FCLONE_ORIG && |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 919 | atomic_read(&fclones->fclone_ref) == 1) { |
| 920 | n = &fclones->skb2; |
| 921 | atomic_set(&fclones->fclone_ref, 2); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 922 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 923 | if (skb_pfmemalloc(skb)) |
| 924 | gfp_mask |= __GFP_MEMALLOC; |
| 925 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 926 | n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); |
| 927 | if (!n) |
| 928 | return NULL; |
Vegard Nossum | fe55f6d | 2008-08-30 12:16:35 +0200 | [diff] [blame] | 929 | |
| 930 | kmemcheck_annotate_bitfield(n, flags1); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 931 | n->fclone = SKB_FCLONE_UNAVAILABLE; |
| 932 | } |
| 933 | |
| 934 | return __skb_clone(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 936 | EXPORT_SYMBOL(skb_clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 938 | static void skb_headers_offset_update(struct sk_buff *skb, int off) |
| 939 | { |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 940 | /* Only adjust this if it actually is csum_start rather than csum */ |
| 941 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 942 | skb->csum_start += off; |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 943 | /* {transport,network,mac}_header and tail are relative to skb->head */ |
| 944 | skb->transport_header += off; |
| 945 | skb->network_header += off; |
| 946 | if (skb_mac_header_was_set(skb)) |
| 947 | skb->mac_header += off; |
| 948 | skb->inner_transport_header += off; |
| 949 | skb->inner_network_header += off; |
Pravin B Shelar | aefbd2b | 2013-03-07 13:21:46 +0000 | [diff] [blame] | 950 | skb->inner_mac_header += off; |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 954 | { |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 955 | __copy_skb_header(new, old); |
| 956 | |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 957 | skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; |
| 958 | skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; |
| 959 | skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 962 | static inline int skb_alloc_rx_flag(const struct sk_buff *skb) |
| 963 | { |
| 964 | if (skb_pfmemalloc(skb)) |
| 965 | return SKB_ALLOC_RX; |
| 966 | return 0; |
| 967 | } |
| 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | /** |
| 970 | * skb_copy - create private copy of an sk_buff |
| 971 | * @skb: buffer to copy |
| 972 | * @gfp_mask: allocation priority |
| 973 | * |
| 974 | * Make a copy of both an &sk_buff and its data. This is used when the |
| 975 | * caller wishes to modify the data and needs a private copy of the |
| 976 | * data to alter. Returns %NULL on failure or the pointer to the buffer |
| 977 | * on success. The returned buffer has a reference count of 1. |
| 978 | * |
| 979 | * As by-product this function converts non-linear &sk_buff to linear |
| 980 | * one, so that &sk_buff becomes completely private and caller is allowed |
| 981 | * to modify all the data of returned buffer. This means that this |
| 982 | * function is not recommended for use in circumstances when only |
| 983 | * header is going to be modified. Use pskb_copy() instead. |
| 984 | */ |
| 985 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 986 | 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] | 987 | { |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 988 | int headerlen = skb_headroom(skb); |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 989 | unsigned int size = skb_end_offset(skb) + skb->data_len; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 990 | struct sk_buff *n = __alloc_skb(size, gfp_mask, |
| 991 | skb_alloc_rx_flag(skb), NUMA_NO_NODE); |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | if (!n) |
| 994 | return NULL; |
| 995 | |
| 996 | /* Set the data pointer */ |
| 997 | skb_reserve(n, headerlen); |
| 998 | /* Set the tail pointer and length */ |
| 999 | skb_put(n, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
| 1001 | if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) |
| 1002 | BUG(); |
| 1003 | |
| 1004 | copy_skb_header(n, skb); |
| 1005 | return n; |
| 1006 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1007 | EXPORT_SYMBOL(skb_copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | /** |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1010 | * __pskb_copy_fclone - create copy of an sk_buff with private head. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | * @skb: buffer to copy |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1012 | * @headroom: headroom of new skb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | * @gfp_mask: allocation priority |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1014 | * @fclone: if true allocate the copy of the skb from the fclone |
| 1015 | * cache instead of the head cache; it is recommended to set this |
| 1016 | * to true for the cases where the copy will likely be cloned |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | * |
| 1018 | * Make a copy of both an &sk_buff and part of its data, located |
| 1019 | * in header. Fragmented data remain shared. This is used when |
| 1020 | * the caller wishes to modify only header of &sk_buff and needs |
| 1021 | * private copy of the header to alter. Returns %NULL on failure |
| 1022 | * or the pointer to the buffer on success. |
| 1023 | * The returned buffer has a reference count of 1. |
| 1024 | */ |
| 1025 | |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1026 | struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, |
| 1027 | gfp_t gfp_mask, bool fclone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | { |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1029 | unsigned int size = skb_headlen(skb) + headroom; |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1030 | int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); |
| 1031 | struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | if (!n) |
| 1034 | goto out; |
| 1035 | |
| 1036 | /* Set the data pointer */ |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1037 | skb_reserve(n, headroom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | /* Set the tail pointer and length */ |
| 1039 | skb_put(n, skb_headlen(skb)); |
| 1040 | /* Copy the bytes */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1041 | skb_copy_from_linear_data(skb, n->data, n->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
Herbert Xu | 25f484a | 2006-11-07 14:57:15 -0800 | [diff] [blame] | 1043 | n->truesize += skb->data_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | n->data_len = skb->data_len; |
| 1045 | n->len = skb->len; |
| 1046 | |
| 1047 | if (skb_shinfo(skb)->nr_frags) { |
| 1048 | int i; |
| 1049 | |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1050 | if (skb_orphan_frags(skb, gfp_mask)) { |
| 1051 | kfree_skb(n); |
| 1052 | n = NULL; |
| 1053 | goto out; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1054 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1056 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1057 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
| 1059 | skb_shinfo(n)->nr_frags = i; |
| 1060 | } |
| 1061 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1062 | if (skb_has_frag_list(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; |
| 1064 | skb_clone_fraglist(n); |
| 1065 | } |
| 1066 | |
| 1067 | copy_skb_header(n, skb); |
| 1068 | out: |
| 1069 | return n; |
| 1070 | } |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1071 | EXPORT_SYMBOL(__pskb_copy_fclone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
| 1073 | /** |
| 1074 | * pskb_expand_head - reallocate header of &sk_buff |
| 1075 | * @skb: buffer to reallocate |
| 1076 | * @nhead: room to add at head |
| 1077 | * @ntail: room to add at tail |
| 1078 | * @gfp_mask: allocation priority |
| 1079 | * |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 1080 | * Expands (or creates identical copy, if @nhead and @ntail are zero) |
| 1081 | * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | * reference count of 1. Returns zero in the case of success or error, |
| 1083 | * if expansion failed. In the last case, &sk_buff is not changed. |
| 1084 | * |
| 1085 | * All the pointers pointing into skb header may change and must be |
| 1086 | * reloaded after call to this function. |
| 1087 | */ |
| 1088 | |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 1089 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1090 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { |
| 1092 | int i; |
| 1093 | u8 *data; |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 1094 | int size = nhead + skb_end_offset(skb) + ntail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | long off; |
| 1096 | |
Herbert Xu | 4edd87a | 2008-10-01 07:09:38 -0700 | [diff] [blame] | 1097 | BUG_ON(nhead < 0); |
| 1098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | if (skb_shared(skb)) |
| 1100 | BUG(); |
| 1101 | |
| 1102 | size = SKB_DATA_ALIGN(size); |
| 1103 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1104 | if (skb_pfmemalloc(skb)) |
| 1105 | gfp_mask |= __GFP_MEMALLOC; |
| 1106 | data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), |
| 1107 | gfp_mask, NUMA_NO_NODE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | if (!data) |
| 1109 | goto nodata; |
Eric Dumazet | 87151b8 | 2012-04-10 20:08:39 +0000 | [diff] [blame] | 1110 | size = SKB_WITH_OVERHEAD(ksize(data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
| 1112 | /* Copy only real data... and, alas, header. This should be |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1113 | * optimized for the cases when header is void. |
| 1114 | */ |
| 1115 | memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); |
| 1116 | |
| 1117 | memcpy((struct skb_shared_info *)(data + size), |
| 1118 | skb_shinfo(skb), |
Eric Dumazet | fed6638 | 2010-07-22 19:09:08 +0000 | [diff] [blame] | 1119 | offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
Alexander Duyck | 3e24591 | 2012-05-04 14:26:51 +0000 | [diff] [blame] | 1121 | /* |
| 1122 | * if shinfo is shared we must drop the old head gracefully, but if it |
| 1123 | * is not we can just drop the old head and let the existing refcount |
| 1124 | * be since all we did is relocate the values |
| 1125 | */ |
| 1126 | if (skb_cloned(skb)) { |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1127 | /* copy this zero copy skb frags */ |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1128 | if (skb_orphan_frags(skb, gfp_mask)) |
| 1129 | goto nofrags; |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1130 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1131 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1133 | if (skb_has_frag_list(skb)) |
| 1134 | skb_clone_fraglist(skb); |
| 1135 | |
| 1136 | skb_release_data(skb); |
Alexander Duyck | 3e24591 | 2012-05-04 14:26:51 +0000 | [diff] [blame] | 1137 | } else { |
| 1138 | skb_free_head(skb); |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | off = (data + nhead) - skb->head; |
| 1141 | |
| 1142 | skb->head = data; |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 1143 | skb->head_frag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | skb->data += off; |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1145 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 1146 | skb->end = size; |
Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 1147 | off = nhead; |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1148 | #else |
| 1149 | skb->end = skb->head + size; |
Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 1150 | #endif |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1151 | skb->tail += off; |
Peter Pan(潘卫平) | b41abb4 | 2013-06-06 21:27:21 +0800 | [diff] [blame] | 1152 | skb_headers_offset_update(skb, nhead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | skb->cloned = 0; |
Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 1154 | skb->hdr_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | skb->nohdr = 0; |
| 1156 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 1157 | return 0; |
| 1158 | |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1159 | nofrags: |
| 1160 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | nodata: |
| 1162 | return -ENOMEM; |
| 1163 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1164 | EXPORT_SYMBOL(pskb_expand_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
| 1166 | /* Make private copy of skb with writable head and some headroom */ |
| 1167 | |
| 1168 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) |
| 1169 | { |
| 1170 | struct sk_buff *skb2; |
| 1171 | int delta = headroom - skb_headroom(skb); |
| 1172 | |
| 1173 | if (delta <= 0) |
| 1174 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
| 1175 | else { |
| 1176 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1177 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, |
| 1178 | GFP_ATOMIC)) { |
| 1179 | kfree_skb(skb2); |
| 1180 | skb2 = NULL; |
| 1181 | } |
| 1182 | } |
| 1183 | return skb2; |
| 1184 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1185 | EXPORT_SYMBOL(skb_realloc_headroom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
| 1187 | /** |
| 1188 | * skb_copy_expand - copy and expand sk_buff |
| 1189 | * @skb: buffer to copy |
| 1190 | * @newheadroom: new free bytes at head |
| 1191 | * @newtailroom: new free bytes at tail |
| 1192 | * @gfp_mask: allocation priority |
| 1193 | * |
| 1194 | * Make a copy of both an &sk_buff and its data and while doing so |
| 1195 | * allocate additional space. |
| 1196 | * |
| 1197 | * This is used when the caller wishes to modify the data and needs a |
| 1198 | * private copy of the data to alter as well as more space for new fields. |
| 1199 | * Returns %NULL on failure or the pointer to the buffer |
| 1200 | * on success. The returned buffer has a reference count of 1. |
| 1201 | * |
| 1202 | * You must pass %GFP_ATOMIC as the allocation priority if this function |
| 1203 | * is called from an interrupt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | */ |
| 1205 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 1206 | int newheadroom, int newtailroom, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1207 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | { |
| 1209 | /* |
| 1210 | * Allocate the copy buffer |
| 1211 | */ |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1212 | struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, |
| 1213 | gfp_mask, skb_alloc_rx_flag(skb), |
| 1214 | NUMA_NO_NODE); |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1215 | int oldheadroom = skb_headroom(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | int head_copy_len, head_copy_off; |
| 1217 | |
| 1218 | if (!n) |
| 1219 | return NULL; |
| 1220 | |
| 1221 | skb_reserve(n, newheadroom); |
| 1222 | |
| 1223 | /* Set the tail pointer and length */ |
| 1224 | skb_put(n, skb->len); |
| 1225 | |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1226 | head_copy_len = oldheadroom; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | head_copy_off = 0; |
| 1228 | if (newheadroom <= head_copy_len) |
| 1229 | head_copy_len = newheadroom; |
| 1230 | else |
| 1231 | head_copy_off = newheadroom - head_copy_len; |
| 1232 | |
| 1233 | /* Copy the linear header and data. */ |
| 1234 | if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, |
| 1235 | skb->len + head_copy_len)) |
| 1236 | BUG(); |
| 1237 | |
| 1238 | copy_skb_header(n, skb); |
| 1239 | |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 1240 | skb_headers_offset_update(n, newheadroom - oldheadroom); |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | return n; |
| 1243 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1244 | EXPORT_SYMBOL(skb_copy_expand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
| 1246 | /** |
| 1247 | * skb_pad - zero pad the tail of an skb |
| 1248 | * @skb: buffer to pad |
| 1249 | * @pad: space to pad |
| 1250 | * |
| 1251 | * Ensure that a buffer is followed by a padding area that is zero |
| 1252 | * filled. Used by network drivers which may DMA or transfer data |
| 1253 | * beyond the buffer end onto the wire. |
| 1254 | * |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1255 | * 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] | 1256 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1257 | |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1258 | int skb_pad(struct sk_buff *skb, int pad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | { |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1260 | int err; |
| 1261 | int ntail; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | /* If the skbuff is non linear tailroom is always zero.. */ |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1264 | if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | memset(skb->data+skb->len, 0, pad); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1266 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | } |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1268 | |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1269 | ntail = skb->data_len + pad - (skb->end - skb->tail); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1270 | if (likely(skb_cloned(skb) || ntail > 0)) { |
| 1271 | err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); |
| 1272 | if (unlikely(err)) |
| 1273 | goto free_skb; |
| 1274 | } |
| 1275 | |
| 1276 | /* FIXME: The use of this function with non-linear skb's really needs |
| 1277 | * to be audited. |
| 1278 | */ |
| 1279 | err = skb_linearize(skb); |
| 1280 | if (unlikely(err)) |
| 1281 | goto free_skb; |
| 1282 | |
| 1283 | memset(skb->data + skb->len, 0, pad); |
| 1284 | return 0; |
| 1285 | |
| 1286 | free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | kfree_skb(skb); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1288 | return err; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1289 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1290 | EXPORT_SYMBOL(skb_pad); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1291 | |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1292 | /** |
Mathias Krause | 0c7ddf3 | 2013-11-07 14:18:24 +0100 | [diff] [blame] | 1293 | * pskb_put - add data to the tail of a potentially fragmented buffer |
| 1294 | * @skb: start of the buffer to use |
| 1295 | * @tail: tail fragment of the buffer to use |
| 1296 | * @len: amount of data to add |
| 1297 | * |
| 1298 | * This function extends the used data area of the potentially |
| 1299 | * fragmented buffer. @tail must be the last fragment of @skb -- or |
| 1300 | * @skb itself. If this would exceed the total buffer size the kernel |
| 1301 | * will panic. A pointer to the first byte of the extra data is |
| 1302 | * returned. |
| 1303 | */ |
| 1304 | |
| 1305 | unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) |
| 1306 | { |
| 1307 | if (tail != skb) { |
| 1308 | skb->data_len += len; |
| 1309 | skb->len += len; |
| 1310 | } |
| 1311 | return skb_put(tail, len); |
| 1312 | } |
| 1313 | EXPORT_SYMBOL_GPL(pskb_put); |
| 1314 | |
| 1315 | /** |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1316 | * skb_put - add data to a buffer |
| 1317 | * @skb: buffer to use |
| 1318 | * @len: amount of data to add |
| 1319 | * |
| 1320 | * This function extends the used data area of the buffer. If this would |
| 1321 | * exceed the total buffer size the kernel will panic. A pointer to the |
| 1322 | * first byte of the extra data is returned. |
| 1323 | */ |
| 1324 | unsigned char *skb_put(struct sk_buff *skb, unsigned int len) |
| 1325 | { |
| 1326 | unsigned char *tmp = skb_tail_pointer(skb); |
| 1327 | SKB_LINEAR_ASSERT(skb); |
| 1328 | skb->tail += len; |
| 1329 | skb->len += len; |
| 1330 | if (unlikely(skb->tail > skb->end)) |
| 1331 | skb_over_panic(skb, len, __builtin_return_address(0)); |
| 1332 | return tmp; |
| 1333 | } |
| 1334 | EXPORT_SYMBOL(skb_put); |
| 1335 | |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1336 | /** |
Ilpo Järvinen | c2aa270 | 2008-03-27 17:52:40 -0700 | [diff] [blame] | 1337 | * skb_push - add data to the start of a buffer |
| 1338 | * @skb: buffer to use |
| 1339 | * @len: amount of data to add |
| 1340 | * |
| 1341 | * This function extends the used data area of the buffer at the buffer |
| 1342 | * start. If this would exceed the total buffer headroom the kernel will |
| 1343 | * panic. A pointer to the first byte of the extra data is returned. |
| 1344 | */ |
| 1345 | unsigned char *skb_push(struct sk_buff *skb, unsigned int len) |
| 1346 | { |
| 1347 | skb->data -= len; |
| 1348 | skb->len += len; |
| 1349 | if (unlikely(skb->data<skb->head)) |
| 1350 | skb_under_panic(skb, len, __builtin_return_address(0)); |
| 1351 | return skb->data; |
| 1352 | } |
| 1353 | EXPORT_SYMBOL(skb_push); |
| 1354 | |
| 1355 | /** |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1356 | * skb_pull - remove data from the start of a buffer |
| 1357 | * @skb: buffer to use |
| 1358 | * @len: amount of data to remove |
| 1359 | * |
| 1360 | * This function removes data from the start of a buffer, returning |
| 1361 | * the memory to the headroom. A pointer to the next data in the buffer |
| 1362 | * is returned. Once the data has been pulled future pushes will overwrite |
| 1363 | * the old data. |
| 1364 | */ |
| 1365 | unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) |
| 1366 | { |
David S. Miller | 47d2964 | 2010-05-02 02:21:44 -0700 | [diff] [blame] | 1367 | return skb_pull_inline(skb, len); |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1368 | } |
| 1369 | EXPORT_SYMBOL(skb_pull); |
| 1370 | |
Ilpo Järvinen | 419ae74 | 2008-03-27 17:54:01 -0700 | [diff] [blame] | 1371 | /** |
| 1372 | * skb_trim - remove end from a buffer |
| 1373 | * @skb: buffer to alter |
| 1374 | * @len: new length |
| 1375 | * |
| 1376 | * Cut the length of a buffer down by removing data from the tail. If |
| 1377 | * the buffer is already under the length specified it is not modified. |
| 1378 | * The skb must be linear. |
| 1379 | */ |
| 1380 | void skb_trim(struct sk_buff *skb, unsigned int len) |
| 1381 | { |
| 1382 | if (skb->len > len) |
| 1383 | __skb_trim(skb, len); |
| 1384 | } |
| 1385 | EXPORT_SYMBOL(skb_trim); |
| 1386 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 1387 | /* Trims skb to length len. It can change skb pointers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | */ |
| 1389 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 1390 | int ___pskb_trim(struct sk_buff *skb, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1392 | struct sk_buff **fragp; |
| 1393 | struct sk_buff *frag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | int offset = skb_headlen(skb); |
| 1395 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 1396 | int i; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1397 | int err; |
| 1398 | |
| 1399 | if (skb_cloned(skb) && |
| 1400 | unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) |
| 1401 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1403 | i = 0; |
| 1404 | if (offset >= len) |
| 1405 | goto drop_pages; |
| 1406 | |
| 1407 | for (; i < nfrags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1408 | int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1409 | |
| 1410 | if (end < len) { |
| 1411 | offset = end; |
| 1412 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | } |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1414 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1415 | skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1416 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1417 | drop_pages: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1418 | skb_shinfo(skb)->nr_frags = i; |
| 1419 | |
| 1420 | for (; i < nfrags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1421 | skb_frag_unref(skb, i); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1422 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1423 | if (skb_has_frag_list(skb)) |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1424 | skb_drop_fraglist(skb); |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1425 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1428 | for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); |
| 1429 | fragp = &frag->next) { |
| 1430 | int end = offset + frag->len; |
| 1431 | |
| 1432 | if (skb_shared(frag)) { |
| 1433 | struct sk_buff *nfrag; |
| 1434 | |
| 1435 | nfrag = skb_clone(frag, GFP_ATOMIC); |
| 1436 | if (unlikely(!nfrag)) |
| 1437 | return -ENOMEM; |
| 1438 | |
| 1439 | nfrag->next = frag->next; |
Eric Dumazet | 85bb2a6 | 2012-04-19 02:24:53 +0000 | [diff] [blame] | 1440 | consume_skb(frag); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1441 | frag = nfrag; |
| 1442 | *fragp = frag; |
| 1443 | } |
| 1444 | |
| 1445 | if (end < len) { |
| 1446 | offset = end; |
| 1447 | continue; |
| 1448 | } |
| 1449 | |
| 1450 | if (end > len && |
| 1451 | unlikely((err = pskb_trim(frag, len - offset)))) |
| 1452 | return err; |
| 1453 | |
| 1454 | if (frag->next) |
| 1455 | skb_drop_list(&frag->next); |
| 1456 | break; |
| 1457 | } |
| 1458 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1459 | done: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1460 | if (len > skb_headlen(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | skb->data_len -= skb->len - len; |
| 1462 | skb->len = len; |
| 1463 | } else { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1464 | skb->len = len; |
| 1465 | skb->data_len = 0; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1466 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | return 0; |
| 1470 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1471 | EXPORT_SYMBOL(___pskb_trim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
| 1473 | /** |
| 1474 | * __pskb_pull_tail - advance tail of skb header |
| 1475 | * @skb: buffer to reallocate |
| 1476 | * @delta: number of bytes to advance tail |
| 1477 | * |
| 1478 | * The function makes a sense only on a fragmented &sk_buff, |
| 1479 | * it expands header moving its tail forward and copying necessary |
| 1480 | * data from fragmented part. |
| 1481 | * |
| 1482 | * &sk_buff MUST have reference count of 1. |
| 1483 | * |
| 1484 | * Returns %NULL (and &sk_buff does not change) if pull failed |
| 1485 | * or value of new tail of skb in the case of success. |
| 1486 | * |
| 1487 | * All the pointers pointing into skb header may change and must be |
| 1488 | * reloaded after call to this function. |
| 1489 | */ |
| 1490 | |
| 1491 | /* Moves tail of skb head forward, copying data from fragmented part, |
| 1492 | * when it is necessary. |
| 1493 | * 1. It may fail due to malloc failure. |
| 1494 | * 2. It may change skb pointers. |
| 1495 | * |
| 1496 | * It is pretty complicated. Luckily, it is called only in exceptional cases. |
| 1497 | */ |
| 1498 | unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) |
| 1499 | { |
| 1500 | /* If skb has not enough free space at tail, get new one |
| 1501 | * plus 128 bytes for future expansions. If we have enough |
| 1502 | * room at tail, reallocate without expansion only if skb is cloned. |
| 1503 | */ |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1504 | int i, k, eat = (skb->tail + delta) - skb->end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | |
| 1506 | if (eat > 0 || skb_cloned(skb)) { |
| 1507 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, |
| 1508 | GFP_ATOMIC)) |
| 1509 | return NULL; |
| 1510 | } |
| 1511 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1512 | 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] | 1513 | BUG(); |
| 1514 | |
| 1515 | /* Optimization: no fragments, no reasons to preestimate |
| 1516 | * size of pulled pages. Superb. |
| 1517 | */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1518 | if (!skb_has_frag_list(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | goto pull_pages; |
| 1520 | |
| 1521 | /* Estimate size of pulled pages. */ |
| 1522 | eat = delta; |
| 1523 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1524 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 1525 | |
| 1526 | if (size >= eat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | goto pull_pages; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1528 | eat -= size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | /* If we need update frag list, we are in troubles. |
| 1532 | * Certainly, it possible to add an offset to skb data, |
| 1533 | * but taking into account that pulling is expected to |
| 1534 | * be very rare operation, it is worth to fight against |
| 1535 | * further bloating skb head and crucify ourselves here instead. |
| 1536 | * Pure masohism, indeed. 8)8) |
| 1537 | */ |
| 1538 | if (eat) { |
| 1539 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1540 | struct sk_buff *clone = NULL; |
| 1541 | struct sk_buff *insp = NULL; |
| 1542 | |
| 1543 | do { |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1544 | BUG_ON(!list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | |
| 1546 | if (list->len <= eat) { |
| 1547 | /* Eaten as whole. */ |
| 1548 | eat -= list->len; |
| 1549 | list = list->next; |
| 1550 | insp = list; |
| 1551 | } else { |
| 1552 | /* Eaten partially. */ |
| 1553 | |
| 1554 | if (skb_shared(list)) { |
| 1555 | /* Sucks! We need to fork list. :-( */ |
| 1556 | clone = skb_clone(list, GFP_ATOMIC); |
| 1557 | if (!clone) |
| 1558 | return NULL; |
| 1559 | insp = list->next; |
| 1560 | list = clone; |
| 1561 | } else { |
| 1562 | /* This may be pulled without |
| 1563 | * problems. */ |
| 1564 | insp = list; |
| 1565 | } |
| 1566 | if (!pskb_pull(list, eat)) { |
Wei Yongjun | f3fbbe0 | 2009-02-25 00:37:32 +0000 | [diff] [blame] | 1567 | kfree_skb(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | return NULL; |
| 1569 | } |
| 1570 | break; |
| 1571 | } |
| 1572 | } while (eat); |
| 1573 | |
| 1574 | /* Free pulled out fragments. */ |
| 1575 | while ((list = skb_shinfo(skb)->frag_list) != insp) { |
| 1576 | skb_shinfo(skb)->frag_list = list->next; |
| 1577 | kfree_skb(list); |
| 1578 | } |
| 1579 | /* And insert new clone at head. */ |
| 1580 | if (clone) { |
| 1581 | clone->next = list; |
| 1582 | skb_shinfo(skb)->frag_list = clone; |
| 1583 | } |
| 1584 | } |
| 1585 | /* Success! Now we may commit changes to skb data. */ |
| 1586 | |
| 1587 | pull_pages: |
| 1588 | eat = delta; |
| 1589 | k = 0; |
| 1590 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1591 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 1592 | |
| 1593 | if (size <= eat) { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1594 | skb_frag_unref(skb, i); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1595 | eat -= size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | } else { |
| 1597 | skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 1598 | if (eat) { |
| 1599 | skb_shinfo(skb)->frags[k].page_offset += eat; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1600 | skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | eat = 0; |
| 1602 | } |
| 1603 | k++; |
| 1604 | } |
| 1605 | } |
| 1606 | skb_shinfo(skb)->nr_frags = k; |
| 1607 | |
| 1608 | skb->tail += delta; |
| 1609 | skb->data_len -= delta; |
| 1610 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1611 | return skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1613 | EXPORT_SYMBOL(__pskb_pull_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
Eric Dumazet | 22019b1 | 2011-07-29 18:37:31 +0000 | [diff] [blame] | 1615 | /** |
| 1616 | * skb_copy_bits - copy bits from skb to kernel buffer |
| 1617 | * @skb: source skb |
| 1618 | * @offset: offset in source |
| 1619 | * @to: destination buffer |
| 1620 | * @len: number of bytes to copy |
| 1621 | * |
| 1622 | * Copy the specified number of bytes from the source skb to the |
| 1623 | * destination buffer. |
| 1624 | * |
| 1625 | * CAUTION ! : |
| 1626 | * If its prototype is ever changed, |
| 1627 | * check arch/{*}/net/{*}.S files, |
| 1628 | * since it is called from BPF assembly code. |
| 1629 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) |
| 1631 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1632 | int start = skb_headlen(skb); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1633 | struct sk_buff *frag_iter; |
| 1634 | int i, copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | |
| 1636 | if (offset > (int)skb->len - len) |
| 1637 | goto fault; |
| 1638 | |
| 1639 | /* Copy header. */ |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1640 | if ((copy = start - offset) > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | if (copy > len) |
| 1642 | copy = len; |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1643 | skb_copy_from_linear_data_offset(skb, offset, to, copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | if ((len -= copy) == 0) |
| 1645 | return 0; |
| 1646 | offset += copy; |
| 1647 | to += copy; |
| 1648 | } |
| 1649 | |
| 1650 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1651 | int end; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1652 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1654 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1655 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1656 | end = start + skb_frag_size(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | if ((copy = end - offset) > 0) { |
| 1658 | u8 *vaddr; |
| 1659 | |
| 1660 | if (copy > len) |
| 1661 | copy = len; |
| 1662 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1663 | vaddr = kmap_atomic(skb_frag_page(f)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | memcpy(to, |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1665 | vaddr + f->page_offset + offset - start, |
| 1666 | copy); |
| 1667 | kunmap_atomic(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | |
| 1669 | if ((len -= copy) == 0) |
| 1670 | return 0; |
| 1671 | offset += copy; |
| 1672 | to += copy; |
| 1673 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1674 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | } |
| 1676 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1677 | skb_walk_frags(skb, frag_iter) { |
| 1678 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1680 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1682 | end = start + frag_iter->len; |
| 1683 | if ((copy = end - offset) > 0) { |
| 1684 | if (copy > len) |
| 1685 | copy = len; |
| 1686 | if (skb_copy_bits(frag_iter, offset - start, to, copy)) |
| 1687 | goto fault; |
| 1688 | if ((len -= copy) == 0) |
| 1689 | return 0; |
| 1690 | offset += copy; |
| 1691 | to += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1693 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | } |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | if (!len) |
| 1697 | return 0; |
| 1698 | |
| 1699 | fault: |
| 1700 | return -EFAULT; |
| 1701 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1702 | EXPORT_SYMBOL(skb_copy_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1704 | /* |
| 1705 | * Callback from splice_to_pipe(), if we need to release some pages |
| 1706 | * at the end of the spd in case we error'ed out in filling the pipe. |
| 1707 | */ |
| 1708 | static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) |
| 1709 | { |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1710 | put_page(spd->pages[i]); |
| 1711 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1712 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1713 | static struct page *linear_to_page(struct page *page, unsigned int *len, |
| 1714 | unsigned int *offset, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1715 | struct sock *sk) |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1716 | { |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1717 | struct page_frag *pfrag = sk_page_frag(sk); |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1718 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1719 | if (!sk_page_frag_refill(sk, pfrag)) |
| 1720 | return NULL; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1721 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1722 | *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1723 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1724 | memcpy(page_address(pfrag->page) + pfrag->offset, |
| 1725 | page_address(page) + *offset, *len); |
| 1726 | *offset = pfrag->offset; |
| 1727 | pfrag->offset += *len; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1728 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1729 | return pfrag->page; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1732 | static bool spd_can_coalesce(const struct splice_pipe_desc *spd, |
| 1733 | struct page *page, |
| 1734 | unsigned int offset) |
| 1735 | { |
| 1736 | return spd->nr_pages && |
| 1737 | spd->pages[spd->nr_pages - 1] == page && |
| 1738 | (spd->partial[spd->nr_pages - 1].offset + |
| 1739 | spd->partial[spd->nr_pages - 1].len == offset); |
| 1740 | } |
| 1741 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1742 | /* |
| 1743 | * Fill page/offset/length into spd, if it can hold more pages. |
| 1744 | */ |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1745 | static bool spd_fill_page(struct splice_pipe_desc *spd, |
| 1746 | struct pipe_inode_info *pipe, struct page *page, |
| 1747 | unsigned int *len, unsigned int offset, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1748 | bool linear, |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1749 | struct sock *sk) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1750 | { |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1751 | if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1752 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1753 | |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1754 | if (linear) { |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1755 | page = linear_to_page(page, len, &offset, sk); |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1756 | if (!page) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1757 | return true; |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1758 | } |
| 1759 | if (spd_can_coalesce(spd, page, offset)) { |
| 1760 | spd->partial[spd->nr_pages - 1].len += *len; |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1761 | return false; |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1762 | } |
| 1763 | get_page(page); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1764 | spd->pages[spd->nr_pages] = page; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1765 | spd->partial[spd->nr_pages].len = *len; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1766 | spd->partial[spd->nr_pages].offset = offset; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1767 | spd->nr_pages++; |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1768 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1769 | return false; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1770 | } |
| 1771 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1772 | static bool __splice_segment(struct page *page, unsigned int poff, |
| 1773 | unsigned int plen, unsigned int *off, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1774 | unsigned int *len, |
Eric Dumazet | d7ccf7c | 2012-04-23 23:35:04 -0400 | [diff] [blame] | 1775 | struct splice_pipe_desc *spd, bool linear, |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1776 | struct sock *sk, |
| 1777 | struct pipe_inode_info *pipe) |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1778 | { |
| 1779 | if (!*len) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1780 | return true; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1781 | |
| 1782 | /* skip this segment if already processed */ |
| 1783 | if (*off >= plen) { |
| 1784 | *off -= plen; |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1785 | return false; |
Octavian Purdila | db43a28 | 2008-06-27 17:27:21 -0700 | [diff] [blame] | 1786 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1787 | |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1788 | /* ignore any bits we already processed */ |
Eric Dumazet | 9ca1b22 | 2013-01-05 21:31:18 +0000 | [diff] [blame] | 1789 | poff += *off; |
| 1790 | plen -= *off; |
| 1791 | *off = 0; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1792 | |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1793 | do { |
| 1794 | unsigned int flen = min(*len, plen); |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1795 | |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1796 | if (spd_fill_page(spd, pipe, page, &flen, poff, |
| 1797 | linear, sk)) |
| 1798 | return true; |
| 1799 | poff += flen; |
| 1800 | plen -= flen; |
| 1801 | *len -= flen; |
| 1802 | } while (*len && plen); |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1803 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1804 | return false; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | /* |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1808 | * 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] | 1809 | * pipe is full or if we already spliced the requested length. |
| 1810 | */ |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1811 | static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, |
| 1812 | unsigned int *offset, unsigned int *len, |
| 1813 | struct splice_pipe_desc *spd, struct sock *sk) |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1814 | { |
| 1815 | int seg; |
| 1816 | |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 1817 | /* map the linear part : |
Alexander Duyck | 2996d31 | 2012-05-02 18:18:42 +0000 | [diff] [blame] | 1818 | * If skb->head_frag is set, this 'linear' part is backed by a |
| 1819 | * fragment, and if the head is not shared with any clones then |
| 1820 | * 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] | 1821 | */ |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1822 | if (__splice_segment(virt_to_page(skb->data), |
| 1823 | (unsigned long) skb->data & (PAGE_SIZE - 1), |
| 1824 | skb_headlen(skb), |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1825 | offset, len, spd, |
Alexander Duyck | 3a7c1ee4 | 2012-05-03 01:09:42 +0000 | [diff] [blame] | 1826 | skb_head_is_locked(skb), |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 1827 | sk, pipe)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1828 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1829 | |
| 1830 | /* |
| 1831 | * then map the fragments |
| 1832 | */ |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1833 | for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { |
| 1834 | const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; |
| 1835 | |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1836 | if (__splice_segment(skb_frag_page(f), |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1837 | f->page_offset, skb_frag_size(f), |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1838 | offset, len, spd, false, sk, pipe)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1839 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1840 | } |
| 1841 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1842 | return false; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | /* |
| 1846 | * Map data from the skb to a pipe. Should handle both the linear part, |
| 1847 | * the fragments, and the frag list. It does NOT handle frag lists within |
| 1848 | * the frag list, if such a thing exists. We'd probably need to recurse to |
| 1849 | * handle that cleanly. |
| 1850 | */ |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1851 | int skb_splice_bits(struct sk_buff *skb, unsigned int offset, |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1852 | struct pipe_inode_info *pipe, unsigned int tlen, |
| 1853 | unsigned int flags) |
| 1854 | { |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1855 | struct partial_page partial[MAX_SKB_FRAGS]; |
| 1856 | struct page *pages[MAX_SKB_FRAGS]; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1857 | struct splice_pipe_desc spd = { |
| 1858 | .pages = pages, |
| 1859 | .partial = partial, |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 1860 | .nr_pages_max = MAX_SKB_FRAGS, |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1861 | .flags = flags, |
Miklos Szeredi | 28a625c | 2014-01-22 19:36:57 +0100 | [diff] [blame] | 1862 | .ops = &nosteal_pipe_buf_ops, |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1863 | .spd_release = sock_spd_release, |
| 1864 | }; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1865 | struct sk_buff *frag_iter; |
Jarek Poplawski | 7a67e56 | 2009-04-30 05:41:19 -0700 | [diff] [blame] | 1866 | struct sock *sk = skb->sk; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1867 | int ret = 0; |
| 1868 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1869 | /* |
| 1870 | * __skb_splice_bits() only fails if the output has no room left, |
| 1871 | * so no point in going over the frag_list for the error case. |
| 1872 | */ |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1873 | if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1874 | goto done; |
| 1875 | else if (!tlen) |
| 1876 | goto done; |
| 1877 | |
| 1878 | /* |
| 1879 | * now see if we have a frag_list to map |
| 1880 | */ |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1881 | skb_walk_frags(skb, frag_iter) { |
| 1882 | if (!tlen) |
| 1883 | break; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1884 | if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1885 | break; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | done: |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1889 | if (spd.nr_pages) { |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1890 | /* |
| 1891 | * Drop the socket lock, otherwise we have reverse |
| 1892 | * locking dependencies between sk_lock and i_mutex |
| 1893 | * here as compared to sendfile(). We enter here |
| 1894 | * with the socket lock held, and splice_to_pipe() will |
| 1895 | * grab the pipe inode lock. For sendfile() emulation, |
| 1896 | * we call into ->sendpage() with the i_mutex lock held |
| 1897 | * and networking will grab the socket lock. |
| 1898 | */ |
Octavian Purdila | 293ad60 | 2008-06-04 15:45:58 -0700 | [diff] [blame] | 1899 | release_sock(sk); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1900 | ret = splice_to_pipe(pipe, &spd); |
Octavian Purdila | 293ad60 | 2008-06-04 15:45:58 -0700 | [diff] [blame] | 1901 | lock_sock(sk); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1902 | } |
| 1903 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1904 | return ret; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1905 | } |
| 1906 | |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1907 | /** |
| 1908 | * skb_store_bits - store bits from kernel buffer to skb |
| 1909 | * @skb: destination buffer |
| 1910 | * @offset: offset in destination |
| 1911 | * @from: source buffer |
| 1912 | * @len: number of bytes to copy |
| 1913 | * |
| 1914 | * Copy the specified number of bytes from the source buffer to the |
| 1915 | * destination skb. This function handles all the messy bits of |
| 1916 | * traversing fragment lists and such. |
| 1917 | */ |
| 1918 | |
Stephen Hemminger | 0c6fcc8 | 2007-04-20 16:40:01 -0700 | [diff] [blame] | 1919 | 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] | 1920 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1921 | int start = skb_headlen(skb); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1922 | struct sk_buff *frag_iter; |
| 1923 | int i, copy; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1924 | |
| 1925 | if (offset > (int)skb->len - len) |
| 1926 | goto fault; |
| 1927 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1928 | if ((copy = start - offset) > 0) { |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1929 | if (copy > len) |
| 1930 | copy = len; |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 1931 | skb_copy_to_linear_data_offset(skb, offset, from, copy); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1932 | if ((len -= copy) == 0) |
| 1933 | return 0; |
| 1934 | offset += copy; |
| 1935 | from += copy; |
| 1936 | } |
| 1937 | |
| 1938 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1939 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1940 | int end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1941 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1942 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1943 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1944 | end = start + skb_frag_size(frag); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1945 | if ((copy = end - offset) > 0) { |
| 1946 | u8 *vaddr; |
| 1947 | |
| 1948 | if (copy > len) |
| 1949 | copy = len; |
| 1950 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1951 | vaddr = kmap_atomic(skb_frag_page(frag)); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1952 | memcpy(vaddr + frag->page_offset + offset - start, |
| 1953 | from, copy); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1954 | kunmap_atomic(vaddr); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1955 | |
| 1956 | if ((len -= copy) == 0) |
| 1957 | return 0; |
| 1958 | offset += copy; |
| 1959 | from += copy; |
| 1960 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1961 | start = end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1964 | skb_walk_frags(skb, frag_iter) { |
| 1965 | int end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1966 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1967 | WARN_ON(start > offset + len); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1968 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1969 | end = start + frag_iter->len; |
| 1970 | if ((copy = end - offset) > 0) { |
| 1971 | if (copy > len) |
| 1972 | copy = len; |
| 1973 | if (skb_store_bits(frag_iter, offset - start, |
| 1974 | from, copy)) |
| 1975 | goto fault; |
| 1976 | if ((len -= copy) == 0) |
| 1977 | return 0; |
| 1978 | offset += copy; |
| 1979 | from += copy; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1980 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1981 | start = end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1982 | } |
| 1983 | if (!len) |
| 1984 | return 0; |
| 1985 | |
| 1986 | fault: |
| 1987 | return -EFAULT; |
| 1988 | } |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1989 | EXPORT_SYMBOL(skb_store_bits); |
| 1990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | /* Checksum skb data. */ |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 1992 | __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, |
| 1993 | __wsum csum, const struct skb_checksum_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1995 | int start = skb_headlen(skb); |
| 1996 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1997 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | int pos = 0; |
| 1999 | |
| 2000 | /* Checksum header. */ |
| 2001 | if (copy > 0) { |
| 2002 | if (copy > len) |
| 2003 | copy = len; |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2004 | csum = ops->update(skb->data + offset, copy, csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | if ((len -= copy) == 0) |
| 2006 | return csum; |
| 2007 | offset += copy; |
| 2008 | pos = copy; |
| 2009 | } |
| 2010 | |
| 2011 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2012 | int end; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2013 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2015 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2016 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2017 | end = start + skb_frag_size(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | if ((copy = end - offset) > 0) { |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 2019 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | |
| 2022 | if (copy > len) |
| 2023 | copy = len; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2024 | vaddr = kmap_atomic(skb_frag_page(frag)); |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2025 | csum2 = ops->update(vaddr + frag->page_offset + |
| 2026 | offset - start, copy, 0); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2027 | kunmap_atomic(vaddr); |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2028 | csum = ops->combine(csum, csum2, pos, copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | if (!(len -= copy)) |
| 2030 | return csum; |
| 2031 | offset += copy; |
| 2032 | pos += copy; |
| 2033 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2034 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2037 | skb_walk_frags(skb, frag_iter) { |
| 2038 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2040 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2042 | end = start + frag_iter->len; |
| 2043 | if ((copy = end - offset) > 0) { |
| 2044 | __wsum csum2; |
| 2045 | if (copy > len) |
| 2046 | copy = len; |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2047 | csum2 = __skb_checksum(frag_iter, offset - start, |
| 2048 | copy, 0, ops); |
| 2049 | csum = ops->combine(csum, csum2, pos, copy); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2050 | if ((len -= copy) == 0) |
| 2051 | return csum; |
| 2052 | offset += copy; |
| 2053 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2055 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2057 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | |
| 2059 | return csum; |
| 2060 | } |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2061 | EXPORT_SYMBOL(__skb_checksum); |
| 2062 | |
| 2063 | __wsum skb_checksum(const struct sk_buff *skb, int offset, |
| 2064 | int len, __wsum csum) |
| 2065 | { |
| 2066 | const struct skb_checksum_ops ops = { |
Daniel Borkmann | cea80ea | 2013-11-04 17:10:25 +0100 | [diff] [blame] | 2067 | .update = csum_partial_ext, |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2068 | .combine = csum_block_add_ext, |
| 2069 | }; |
| 2070 | |
| 2071 | return __skb_checksum(skb, offset, len, csum, &ops); |
| 2072 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2073 | EXPORT_SYMBOL(skb_checksum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
| 2075 | /* Both of above in one bottle. */ |
| 2076 | |
Al Viro | 81d7766 | 2006-11-14 21:37:33 -0800 | [diff] [blame] | 2077 | __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, |
| 2078 | u8 *to, int len, __wsum csum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2080 | int start = skb_headlen(skb); |
| 2081 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2082 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | int pos = 0; |
| 2084 | |
| 2085 | /* Copy header. */ |
| 2086 | if (copy > 0) { |
| 2087 | if (copy > len) |
| 2088 | copy = len; |
| 2089 | csum = csum_partial_copy_nocheck(skb->data + offset, to, |
| 2090 | copy, csum); |
| 2091 | if ((len -= copy) == 0) |
| 2092 | return csum; |
| 2093 | offset += copy; |
| 2094 | to += copy; |
| 2095 | pos = copy; |
| 2096 | } |
| 2097 | |
| 2098 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2099 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2101 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2102 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2103 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | if ((copy = end - offset) > 0) { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 2105 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | u8 *vaddr; |
| 2107 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 2108 | |
| 2109 | if (copy > len) |
| 2110 | copy = len; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2111 | vaddr = kmap_atomic(skb_frag_page(frag)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | csum2 = csum_partial_copy_nocheck(vaddr + |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2113 | frag->page_offset + |
| 2114 | offset - start, to, |
| 2115 | copy, 0); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2116 | kunmap_atomic(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | csum = csum_block_add(csum, csum2, pos); |
| 2118 | if (!(len -= copy)) |
| 2119 | return csum; |
| 2120 | offset += copy; |
| 2121 | to += copy; |
| 2122 | pos += copy; |
| 2123 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2124 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2127 | skb_walk_frags(skb, frag_iter) { |
| 2128 | __wsum csum2; |
| 2129 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2131 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2133 | end = start + frag_iter->len; |
| 2134 | if ((copy = end - offset) > 0) { |
| 2135 | if (copy > len) |
| 2136 | copy = len; |
| 2137 | csum2 = skb_copy_and_csum_bits(frag_iter, |
| 2138 | offset - start, |
| 2139 | to, copy, 0); |
| 2140 | csum = csum_block_add(csum, csum2, pos); |
| 2141 | if ((len -= copy) == 0) |
| 2142 | return csum; |
| 2143 | offset += copy; |
| 2144 | to += copy; |
| 2145 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2147 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2149 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | return csum; |
| 2151 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2152 | EXPORT_SYMBOL(skb_copy_and_csum_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2154 | /** |
| 2155 | * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() |
| 2156 | * @from: source buffer |
| 2157 | * |
| 2158 | * Calculates the amount of linear headroom needed in the 'to' skb passed |
| 2159 | * into skb_zerocopy(). |
| 2160 | */ |
| 2161 | unsigned int |
| 2162 | skb_zerocopy_headlen(const struct sk_buff *from) |
| 2163 | { |
| 2164 | unsigned int hlen = 0; |
| 2165 | |
| 2166 | if (!from->head_frag || |
| 2167 | skb_headlen(from) < L1_CACHE_BYTES || |
| 2168 | skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) |
| 2169 | hlen = skb_headlen(from); |
| 2170 | |
| 2171 | if (skb_has_frag_list(from)) |
| 2172 | hlen = from->len; |
| 2173 | |
| 2174 | return hlen; |
| 2175 | } |
| 2176 | EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); |
| 2177 | |
| 2178 | /** |
| 2179 | * skb_zerocopy - Zero copy skb to skb |
| 2180 | * @to: destination buffer |
Masanari Iida | 7fceb4d | 2014-01-29 01:05:28 +0900 | [diff] [blame] | 2181 | * @from: source buffer |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2182 | * @len: number of bytes to copy from source buffer |
| 2183 | * @hlen: size of linear headroom in destination buffer |
| 2184 | * |
| 2185 | * Copies up to `len` bytes from `from` to `to` by creating references |
| 2186 | * to the frags in the source buffer. |
| 2187 | * |
| 2188 | * The `hlen` as calculated by skb_zerocopy_headlen() specifies the |
| 2189 | * headroom in the `to` buffer. |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2190 | * |
| 2191 | * Return value: |
| 2192 | * 0: everything is OK |
| 2193 | * -ENOMEM: couldn't orphan frags of @from due to lack of memory |
| 2194 | * -EFAULT: skb_copy_bits() found some problem with skb geometry |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2195 | */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2196 | int |
| 2197 | skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2198 | { |
| 2199 | int i, j = 0; |
| 2200 | int plen = 0; /* length of skb->head fragment */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2201 | int ret; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2202 | struct page *page; |
| 2203 | unsigned int offset; |
| 2204 | |
| 2205 | BUG_ON(!from->head_frag && !hlen); |
| 2206 | |
| 2207 | /* dont bother with small payloads */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2208 | if (len <= skb_tailroom(to)) |
| 2209 | return skb_copy_bits(from, 0, skb_put(to, len), len); |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2210 | |
| 2211 | if (hlen) { |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2212 | ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); |
| 2213 | if (unlikely(ret)) |
| 2214 | return ret; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2215 | len -= hlen; |
| 2216 | } else { |
| 2217 | plen = min_t(int, skb_headlen(from), len); |
| 2218 | if (plen) { |
| 2219 | page = virt_to_head_page(from->head); |
| 2220 | offset = from->data - (unsigned char *)page_address(page); |
| 2221 | __skb_fill_page_desc(to, 0, page, offset, plen); |
| 2222 | get_page(page); |
| 2223 | j = 1; |
| 2224 | len -= plen; |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | to->truesize += len + plen; |
| 2229 | to->len += len + plen; |
| 2230 | to->data_len += len + plen; |
| 2231 | |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2232 | if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { |
| 2233 | skb_tx_error(from); |
| 2234 | return -ENOMEM; |
| 2235 | } |
| 2236 | |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2237 | for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { |
| 2238 | if (!len) |
| 2239 | break; |
| 2240 | skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; |
| 2241 | skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); |
| 2242 | len -= skb_shinfo(to)->frags[j].size; |
| 2243 | skb_frag_ref(to, j); |
| 2244 | j++; |
| 2245 | } |
| 2246 | skb_shinfo(to)->nr_frags = j; |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2247 | |
| 2248 | return 0; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2249 | } |
| 2250 | EXPORT_SYMBOL_GPL(skb_zerocopy); |
| 2251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) |
| 2253 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 2254 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | long csstart; |
| 2256 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2257 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
Michał Mirosław | 55508d6 | 2010-12-14 15:24:08 +0000 | [diff] [blame] | 2258 | csstart = skb_checksum_start_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | else |
| 2260 | csstart = skb_headlen(skb); |
| 2261 | |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2262 | BUG_ON(csstart > skb_headlen(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2264 | skb_copy_from_linear_data(skb, to, csstart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | |
| 2266 | csum = 0; |
| 2267 | if (csstart != skb->len) |
| 2268 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, |
| 2269 | skb->len - csstart, 0); |
| 2270 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2271 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Al Viro | ff1dcad | 2006-11-20 18:07:29 -0800 | [diff] [blame] | 2272 | long csstuff = csstart + skb->csum_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 2274 | *((__sum16 *)(to + csstuff)) = csum_fold(csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | } |
| 2276 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2277 | EXPORT_SYMBOL(skb_copy_and_csum_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | |
| 2279 | /** |
| 2280 | * skb_dequeue - remove from the head of the queue |
| 2281 | * @list: list to dequeue from |
| 2282 | * |
| 2283 | * Remove the head of the list. The list lock is taken so the function |
| 2284 | * may be used safely with other locking list functions. The head item is |
| 2285 | * returned or %NULL if the list is empty. |
| 2286 | */ |
| 2287 | |
| 2288 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) |
| 2289 | { |
| 2290 | unsigned long flags; |
| 2291 | struct sk_buff *result; |
| 2292 | |
| 2293 | spin_lock_irqsave(&list->lock, flags); |
| 2294 | result = __skb_dequeue(list); |
| 2295 | spin_unlock_irqrestore(&list->lock, flags); |
| 2296 | return result; |
| 2297 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2298 | EXPORT_SYMBOL(skb_dequeue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | |
| 2300 | /** |
| 2301 | * skb_dequeue_tail - remove from the tail of the queue |
| 2302 | * @list: list to dequeue from |
| 2303 | * |
| 2304 | * Remove the tail of the list. The list lock is taken so the function |
| 2305 | * may be used safely with other locking list functions. The tail item is |
| 2306 | * returned or %NULL if the list is empty. |
| 2307 | */ |
| 2308 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) |
| 2309 | { |
| 2310 | unsigned long flags; |
| 2311 | struct sk_buff *result; |
| 2312 | |
| 2313 | spin_lock_irqsave(&list->lock, flags); |
| 2314 | result = __skb_dequeue_tail(list); |
| 2315 | spin_unlock_irqrestore(&list->lock, flags); |
| 2316 | return result; |
| 2317 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2318 | EXPORT_SYMBOL(skb_dequeue_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | |
| 2320 | /** |
| 2321 | * skb_queue_purge - empty a list |
| 2322 | * @list: list to empty |
| 2323 | * |
| 2324 | * Delete all buffers on an &sk_buff list. Each buffer is removed from |
| 2325 | * the list and one reference dropped. This function takes the list |
| 2326 | * lock and is atomic with respect to other list locking functions. |
| 2327 | */ |
| 2328 | void skb_queue_purge(struct sk_buff_head *list) |
| 2329 | { |
| 2330 | struct sk_buff *skb; |
| 2331 | while ((skb = skb_dequeue(list)) != NULL) |
| 2332 | kfree_skb(skb); |
| 2333 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2334 | EXPORT_SYMBOL(skb_queue_purge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | |
| 2336 | /** |
| 2337 | * skb_queue_head - queue a buffer at the list head |
| 2338 | * @list: list to use |
| 2339 | * @newsk: buffer to queue |
| 2340 | * |
| 2341 | * Queue a buffer at the start of the list. This function takes the |
| 2342 | * list lock and can be used safely with other locking &sk_buff functions |
| 2343 | * safely. |
| 2344 | * |
| 2345 | * A buffer cannot be placed on two lists at the same time. |
| 2346 | */ |
| 2347 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) |
| 2348 | { |
| 2349 | unsigned long flags; |
| 2350 | |
| 2351 | spin_lock_irqsave(&list->lock, flags); |
| 2352 | __skb_queue_head(list, newsk); |
| 2353 | spin_unlock_irqrestore(&list->lock, flags); |
| 2354 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2355 | EXPORT_SYMBOL(skb_queue_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | |
| 2357 | /** |
| 2358 | * skb_queue_tail - queue a buffer at the list tail |
| 2359 | * @list: list to use |
| 2360 | * @newsk: buffer to queue |
| 2361 | * |
| 2362 | * Queue a buffer at the tail of the list. This function takes the |
| 2363 | * list lock and can be used safely with other locking &sk_buff functions |
| 2364 | * safely. |
| 2365 | * |
| 2366 | * A buffer cannot be placed on two lists at the same time. |
| 2367 | */ |
| 2368 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) |
| 2369 | { |
| 2370 | unsigned long flags; |
| 2371 | |
| 2372 | spin_lock_irqsave(&list->lock, flags); |
| 2373 | __skb_queue_tail(list, newsk); |
| 2374 | spin_unlock_irqrestore(&list->lock, flags); |
| 2375 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2376 | EXPORT_SYMBOL(skb_queue_tail); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | /** |
| 2379 | * skb_unlink - remove a buffer from a list |
| 2380 | * @skb: buffer to remove |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2381 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2383 | * Remove a packet from a list. The list locks are taken and this |
| 2384 | * function is atomic with respect to other list locked calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2386 | * You must know what list the SKB is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2388 | void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2390 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2392 | spin_lock_irqsave(&list->lock, flags); |
| 2393 | __skb_unlink(skb, list); |
| 2394 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2396 | EXPORT_SYMBOL(skb_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | /** |
| 2399 | * skb_append - append a buffer |
| 2400 | * @old: buffer to insert after |
| 2401 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2402 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | * |
| 2404 | * Place a packet after a given packet in a list. The list locks are taken |
| 2405 | * and this function is atomic with respect to other list locked calls. |
| 2406 | * A buffer cannot be placed on two lists at the same time. |
| 2407 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2408 | 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] | 2409 | { |
| 2410 | unsigned long flags; |
| 2411 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2412 | spin_lock_irqsave(&list->lock, flags); |
Gerrit Renker | 7de6c03 | 2008-04-14 00:05:09 -0700 | [diff] [blame] | 2413 | __skb_queue_after(list, old, newsk); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2414 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2416 | EXPORT_SYMBOL(skb_append); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | |
| 2418 | /** |
| 2419 | * skb_insert - insert a buffer |
| 2420 | * @old: buffer to insert before |
| 2421 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2422 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2424 | * Place a packet before a given packet in a list. The list locks are |
| 2425 | * taken and this function is atomic with respect to other list locked |
| 2426 | * calls. |
| 2427 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | * A buffer cannot be placed on two lists at the same time. |
| 2429 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2430 | 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] | 2431 | { |
| 2432 | unsigned long flags; |
| 2433 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2434 | spin_lock_irqsave(&list->lock, flags); |
| 2435 | __skb_insert(newsk, old->prev, old, list); |
| 2436 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2438 | EXPORT_SYMBOL(skb_insert); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | static inline void skb_split_inside_header(struct sk_buff *skb, |
| 2441 | struct sk_buff* skb1, |
| 2442 | const u32 len, const int pos) |
| 2443 | { |
| 2444 | int i; |
| 2445 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2446 | skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), |
| 2447 | pos - len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | /* And move data appendix as is. */ |
| 2449 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 2450 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 2451 | |
| 2452 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; |
| 2453 | skb_shinfo(skb)->nr_frags = 0; |
| 2454 | skb1->data_len = skb->data_len; |
| 2455 | skb1->len += skb1->data_len; |
| 2456 | skb->data_len = 0; |
| 2457 | skb->len = len; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 2458 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | static inline void skb_split_no_header(struct sk_buff *skb, |
| 2462 | struct sk_buff* skb1, |
| 2463 | const u32 len, int pos) |
| 2464 | { |
| 2465 | int i, k = 0; |
| 2466 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 2467 | |
| 2468 | skb_shinfo(skb)->nr_frags = 0; |
| 2469 | skb1->len = skb1->data_len = skb->len - len; |
| 2470 | skb->len = len; |
| 2471 | skb->data_len = len - pos; |
| 2472 | |
| 2473 | for (i = 0; i < nfrags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2474 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | |
| 2476 | if (pos + size > len) { |
| 2477 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 2478 | |
| 2479 | if (pos < len) { |
| 2480 | /* Split frag. |
| 2481 | * We have two variants in this case: |
| 2482 | * 1. Move all the frag to the second |
| 2483 | * part, if it is possible. F.e. |
| 2484 | * this approach is mandatory for TUX, |
| 2485 | * where splitting is expensive. |
| 2486 | * 2. Split is accurately. We make this. |
| 2487 | */ |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2488 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | skb_shinfo(skb1)->frags[0].page_offset += len - pos; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2490 | skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); |
| 2491 | skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | skb_shinfo(skb)->nr_frags++; |
| 2493 | } |
| 2494 | k++; |
| 2495 | } else |
| 2496 | skb_shinfo(skb)->nr_frags++; |
| 2497 | pos += size; |
| 2498 | } |
| 2499 | skb_shinfo(skb1)->nr_frags = k; |
| 2500 | } |
| 2501 | |
| 2502 | /** |
| 2503 | * skb_split - Split fragmented skb to two parts at length len. |
| 2504 | * @skb: the buffer to split |
| 2505 | * @skb1: the buffer to receive the second part |
| 2506 | * @len: new length for skb |
| 2507 | */ |
| 2508 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) |
| 2509 | { |
| 2510 | int pos = skb_headlen(skb); |
| 2511 | |
Amerigo Wang | 68534c6 | 2013-02-19 22:51:30 +0000 | [diff] [blame] | 2512 | skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | if (len < pos) /* Split line is inside header. */ |
| 2514 | skb_split_inside_header(skb, skb1, len, pos); |
| 2515 | else /* Second chunk has no header, nothing to copy. */ |
| 2516 | skb_split_no_header(skb, skb1, len, pos); |
| 2517 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2518 | EXPORT_SYMBOL(skb_split); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | |
Ilpo Järvinen | 9f782db | 2008-11-25 13:57:01 -0800 | [diff] [blame] | 2520 | /* Shifting from/to a cloned skb is a no-go. |
| 2521 | * |
| 2522 | * Caller cannot keep skb_shinfo related pointers past calling here! |
| 2523 | */ |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2524 | static int skb_prepare_for_shift(struct sk_buff *skb) |
| 2525 | { |
Ilpo Järvinen | 0ace285 | 2008-11-24 21:30:21 -0800 | [diff] [blame] | 2526 | 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] | 2527 | } |
| 2528 | |
| 2529 | /** |
| 2530 | * skb_shift - Shifts paged data partially from skb to another |
| 2531 | * @tgt: buffer into which tail data gets added |
| 2532 | * @skb: buffer from which the paged data comes from |
| 2533 | * @shiftlen: shift up to this many bytes |
| 2534 | * |
| 2535 | * 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] | 2536 | * 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] | 2537 | * It's up to caller to free skb if everything was shifted. |
| 2538 | * |
| 2539 | * If @tgt runs out of frags, the whole operation is aborted. |
| 2540 | * |
| 2541 | * Skb cannot include anything else but paged data while tgt is allowed |
| 2542 | * to have non-paged data as well. |
| 2543 | * |
| 2544 | * TODO: full sized shift could be optimized but that would need |
| 2545 | * specialized skb free'er to handle frags without up-to-date nr_frags. |
| 2546 | */ |
| 2547 | int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) |
| 2548 | { |
| 2549 | int from, to, merge, todo; |
| 2550 | struct skb_frag_struct *fragfrom, *fragto; |
| 2551 | |
| 2552 | BUG_ON(shiftlen > skb->len); |
| 2553 | BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ |
| 2554 | |
| 2555 | todo = shiftlen; |
| 2556 | from = 0; |
| 2557 | to = skb_shinfo(tgt)->nr_frags; |
| 2558 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 2559 | |
| 2560 | /* Actual merge is delayed until the point when we know we can |
| 2561 | * commit all, so that we don't have to undo partial changes |
| 2562 | */ |
| 2563 | if (!to || |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2564 | !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), |
| 2565 | fragfrom->page_offset)) { |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2566 | merge = -1; |
| 2567 | } else { |
| 2568 | merge = to - 1; |
| 2569 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2570 | todo -= skb_frag_size(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2571 | if (todo < 0) { |
| 2572 | if (skb_prepare_for_shift(skb) || |
| 2573 | skb_prepare_for_shift(tgt)) |
| 2574 | return 0; |
| 2575 | |
Ilpo Järvinen | 9f782db | 2008-11-25 13:57:01 -0800 | [diff] [blame] | 2576 | /* All previous frag pointers might be stale! */ |
| 2577 | fragfrom = &skb_shinfo(skb)->frags[from]; |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2578 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 2579 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2580 | skb_frag_size_add(fragto, shiftlen); |
| 2581 | skb_frag_size_sub(fragfrom, shiftlen); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2582 | fragfrom->page_offset += shiftlen; |
| 2583 | |
| 2584 | goto onlymerged; |
| 2585 | } |
| 2586 | |
| 2587 | from++; |
| 2588 | } |
| 2589 | |
| 2590 | /* Skip full, not-fitting skb to avoid expensive operations */ |
| 2591 | if ((shiftlen == skb->len) && |
| 2592 | (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) |
| 2593 | return 0; |
| 2594 | |
| 2595 | if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) |
| 2596 | return 0; |
| 2597 | |
| 2598 | while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { |
| 2599 | if (to == MAX_SKB_FRAGS) |
| 2600 | return 0; |
| 2601 | |
| 2602 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 2603 | fragto = &skb_shinfo(tgt)->frags[to]; |
| 2604 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2605 | if (todo >= skb_frag_size(fragfrom)) { |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2606 | *fragto = *fragfrom; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2607 | todo -= skb_frag_size(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2608 | from++; |
| 2609 | to++; |
| 2610 | |
| 2611 | } else { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2612 | __skb_frag_ref(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2613 | fragto->page = fragfrom->page; |
| 2614 | fragto->page_offset = fragfrom->page_offset; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2615 | skb_frag_size_set(fragto, todo); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2616 | |
| 2617 | fragfrom->page_offset += todo; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2618 | skb_frag_size_sub(fragfrom, todo); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2619 | todo = 0; |
| 2620 | |
| 2621 | to++; |
| 2622 | break; |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | /* Ready to "commit" this state change to tgt */ |
| 2627 | skb_shinfo(tgt)->nr_frags = to; |
| 2628 | |
| 2629 | if (merge >= 0) { |
| 2630 | fragfrom = &skb_shinfo(skb)->frags[0]; |
| 2631 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 2632 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2633 | skb_frag_size_add(fragto, skb_frag_size(fragfrom)); |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2634 | __skb_frag_unref(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | /* Reposition in the original skb */ |
| 2638 | to = 0; |
| 2639 | while (from < skb_shinfo(skb)->nr_frags) |
| 2640 | skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; |
| 2641 | skb_shinfo(skb)->nr_frags = to; |
| 2642 | |
| 2643 | BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); |
| 2644 | |
| 2645 | onlymerged: |
| 2646 | /* Most likely the tgt won't ever need its checksum anymore, skb on |
| 2647 | * the other hand might need it if it needs to be resent |
| 2648 | */ |
| 2649 | tgt->ip_summed = CHECKSUM_PARTIAL; |
| 2650 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 2651 | |
| 2652 | /* Yak, is it really working this way? Some helper please? */ |
| 2653 | skb->len -= shiftlen; |
| 2654 | skb->data_len -= shiftlen; |
| 2655 | skb->truesize -= shiftlen; |
| 2656 | tgt->len += shiftlen; |
| 2657 | tgt->data_len += shiftlen; |
| 2658 | tgt->truesize += shiftlen; |
| 2659 | |
| 2660 | return shiftlen; |
| 2661 | } |
| 2662 | |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2663 | /** |
| 2664 | * skb_prepare_seq_read - Prepare a sequential read of skb data |
| 2665 | * @skb: the buffer to read |
| 2666 | * @from: lower offset of data to be read |
| 2667 | * @to: upper offset of data to be read |
| 2668 | * @st: state variable |
| 2669 | * |
| 2670 | * Initializes the specified state variable. Must be called before |
| 2671 | * invoking skb_seq_read() for the first time. |
| 2672 | */ |
| 2673 | void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, |
| 2674 | unsigned int to, struct skb_seq_state *st) |
| 2675 | { |
| 2676 | st->lower_offset = from; |
| 2677 | st->upper_offset = to; |
| 2678 | st->root_skb = st->cur_skb = skb; |
| 2679 | st->frag_idx = st->stepped_offset = 0; |
| 2680 | st->frag_data = NULL; |
| 2681 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2682 | EXPORT_SYMBOL(skb_prepare_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2683 | |
| 2684 | /** |
| 2685 | * skb_seq_read - Sequentially read skb data |
| 2686 | * @consumed: number of bytes consumed by the caller so far |
| 2687 | * @data: destination pointer for data to be returned |
| 2688 | * @st: state variable |
| 2689 | * |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 2690 | * Reads a block of skb data at @consumed relative to the |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2691 | * lower offset specified to skb_prepare_seq_read(). Assigns |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 2692 | * the head of the data block to @data and returns the length |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2693 | * of the block or 0 if the end of the skb data or the upper |
| 2694 | * offset has been reached. |
| 2695 | * |
| 2696 | * The caller is not required to consume all of the data |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 2697 | * returned, i.e. @consumed is typically set to the number |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2698 | * of bytes already consumed and the next call to |
| 2699 | * skb_seq_read() will return the remaining part of the block. |
| 2700 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2701 | * Note 1: The size of each block of data returned can be arbitrary, |
Masanari Iida | e793c0f | 2014-09-04 23:44:36 +0900 | [diff] [blame] | 2702 | * this limitation is the cost for zerocopy sequential |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2703 | * reads of potentially non linear data. |
| 2704 | * |
Randy Dunlap | bc2cda1 | 2008-02-13 15:03:25 -0800 | [diff] [blame] | 2705 | * Note 2: Fragment lists within fragments are not implemented |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2706 | * at the moment, state->root_skb could be replaced with |
| 2707 | * a stack for this purpose. |
| 2708 | */ |
| 2709 | unsigned int skb_seq_read(unsigned int consumed, const u8 **data, |
| 2710 | struct skb_seq_state *st) |
| 2711 | { |
| 2712 | unsigned int block_limit, abs_offset = consumed + st->lower_offset; |
| 2713 | skb_frag_t *frag; |
| 2714 | |
Wedson Almeida Filho | aeb193e | 2013-06-23 23:33:48 -0700 | [diff] [blame] | 2715 | if (unlikely(abs_offset >= st->upper_offset)) { |
| 2716 | if (st->frag_data) { |
| 2717 | kunmap_atomic(st->frag_data); |
| 2718 | st->frag_data = NULL; |
| 2719 | } |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2720 | return 0; |
Wedson Almeida Filho | aeb193e | 2013-06-23 23:33:48 -0700 | [diff] [blame] | 2721 | } |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2722 | |
| 2723 | next_skb: |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 2724 | block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2725 | |
Thomas Chenault | 995b337 | 2009-05-18 21:43:27 -0700 | [diff] [blame] | 2726 | if (abs_offset < block_limit && !st->frag_data) { |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 2727 | *data = st->cur_skb->data + (abs_offset - st->stepped_offset); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2728 | return block_limit - abs_offset; |
| 2729 | } |
| 2730 | |
| 2731 | if (st->frag_idx == 0 && !st->frag_data) |
| 2732 | st->stepped_offset += skb_headlen(st->cur_skb); |
| 2733 | |
| 2734 | while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { |
| 2735 | frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2736 | block_limit = skb_frag_size(frag) + st->stepped_offset; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2737 | |
| 2738 | if (abs_offset < block_limit) { |
| 2739 | if (!st->frag_data) |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2740 | st->frag_data = kmap_atomic(skb_frag_page(frag)); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2741 | |
| 2742 | *data = (u8 *) st->frag_data + frag->page_offset + |
| 2743 | (abs_offset - st->stepped_offset); |
| 2744 | |
| 2745 | return block_limit - abs_offset; |
| 2746 | } |
| 2747 | |
| 2748 | if (st->frag_data) { |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2749 | kunmap_atomic(st->frag_data); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2750 | st->frag_data = NULL; |
| 2751 | } |
| 2752 | |
| 2753 | st->frag_idx++; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2754 | st->stepped_offset += skb_frag_size(frag); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2755 | } |
| 2756 | |
Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 2757 | if (st->frag_data) { |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2758 | kunmap_atomic(st->frag_data); |
Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 2759 | st->frag_data = NULL; |
| 2760 | } |
| 2761 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 2762 | 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] | 2763 | st->cur_skb = skb_shinfo(st->root_skb)->frag_list; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2764 | st->frag_idx = 0; |
| 2765 | goto next_skb; |
Shyam Iyer | 71b3346 | 2009-01-29 16:12:42 -0800 | [diff] [blame] | 2766 | } else if (st->cur_skb->next) { |
| 2767 | st->cur_skb = st->cur_skb->next; |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 2768 | st->frag_idx = 0; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2769 | goto next_skb; |
| 2770 | } |
| 2771 | |
| 2772 | return 0; |
| 2773 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2774 | EXPORT_SYMBOL(skb_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2775 | |
| 2776 | /** |
| 2777 | * skb_abort_seq_read - Abort a sequential read of skb data |
| 2778 | * @st: state variable |
| 2779 | * |
| 2780 | * Must be called if skb_seq_read() was not called until it |
| 2781 | * returned 0. |
| 2782 | */ |
| 2783 | void skb_abort_seq_read(struct skb_seq_state *st) |
| 2784 | { |
| 2785 | if (st->frag_data) |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2786 | kunmap_atomic(st->frag_data); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2787 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2788 | EXPORT_SYMBOL(skb_abort_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2789 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2790 | #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) |
| 2791 | |
| 2792 | static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, |
| 2793 | struct ts_config *conf, |
| 2794 | struct ts_state *state) |
| 2795 | { |
| 2796 | return skb_seq_read(offset, text, TS_SKB_CB(state)); |
| 2797 | } |
| 2798 | |
| 2799 | static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) |
| 2800 | { |
| 2801 | skb_abort_seq_read(TS_SKB_CB(state)); |
| 2802 | } |
| 2803 | |
| 2804 | /** |
| 2805 | * skb_find_text - Find a text pattern in skb data |
| 2806 | * @skb: the buffer to look in |
| 2807 | * @from: search offset |
| 2808 | * @to: search limit |
| 2809 | * @config: textsearch configuration |
| 2810 | * @state: uninitialized textsearch state variable |
| 2811 | * |
| 2812 | * Finds a pattern in the skb data according to the specified |
| 2813 | * textsearch configuration. Use textsearch_next() to retrieve |
| 2814 | * subsequent occurrences of the pattern. Returns the offset |
| 2815 | * to the first occurrence or UINT_MAX if no match was found. |
| 2816 | */ |
| 2817 | unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, |
| 2818 | unsigned int to, struct ts_config *config, |
| 2819 | struct ts_state *state) |
| 2820 | { |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 2821 | unsigned int ret; |
| 2822 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2823 | config->get_next_block = skb_ts_get_next_block; |
| 2824 | config->finish = skb_ts_finish; |
| 2825 | |
| 2826 | skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); |
| 2827 | |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 2828 | ret = textsearch_find(config, state); |
| 2829 | return (ret <= to - from ? ret : UINT_MAX); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2830 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2831 | EXPORT_SYMBOL(skb_find_text); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2832 | |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2833 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 2834 | * skb_append_datato_frags - append the user data to a skb |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2835 | * @sk: sock structure |
Masanari Iida | e793c0f | 2014-09-04 23:44:36 +0900 | [diff] [blame] | 2836 | * @skb: skb structure to be appended with user data. |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2837 | * @getfrag: call back function to be used for getting the user data |
| 2838 | * @from: pointer to user message iov |
| 2839 | * @length: length of the iov message |
| 2840 | * |
| 2841 | * Description: This procedure append the user data in the fragment part |
| 2842 | * of the skb if any page alloc fails user this procedure returns -ENOMEM |
| 2843 | */ |
| 2844 | int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, |
Martin Waitz | dab9630 | 2005-12-05 13:40:12 -0800 | [diff] [blame] | 2845 | int (*getfrag)(void *from, char *to, int offset, |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2846 | int len, int odd, struct sk_buff *skb), |
| 2847 | void *from, int length) |
| 2848 | { |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2849 | int frg_cnt = skb_shinfo(skb)->nr_frags; |
| 2850 | int copy; |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2851 | int offset = 0; |
| 2852 | int ret; |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2853 | struct page_frag *pfrag = ¤t->task_frag; |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2854 | |
| 2855 | do { |
| 2856 | /* Return error if we don't have space for new frag */ |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2857 | if (frg_cnt >= MAX_SKB_FRAGS) |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2858 | return -EMSGSIZE; |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2859 | |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2860 | if (!sk_page_frag_refill(sk, pfrag)) |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2861 | return -ENOMEM; |
| 2862 | |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2863 | /* copy the user data to page */ |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2864 | copy = min_t(int, length, pfrag->size - pfrag->offset); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2865 | |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2866 | ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, |
| 2867 | offset, copy, 0, skb); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2868 | if (ret < 0) |
| 2869 | return -EFAULT; |
| 2870 | |
| 2871 | /* copy was successful so update the size parameters */ |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2872 | skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, |
| 2873 | copy); |
| 2874 | frg_cnt++; |
| 2875 | pfrag->offset += copy; |
| 2876 | get_page(pfrag->page); |
| 2877 | |
| 2878 | skb->truesize += copy; |
| 2879 | atomic_add(copy, &sk->sk_wmem_alloc); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2880 | skb->len += copy; |
| 2881 | skb->data_len += copy; |
| 2882 | offset += copy; |
| 2883 | length -= copy; |
| 2884 | |
| 2885 | } while (length > 0); |
| 2886 | |
| 2887 | return 0; |
| 2888 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2889 | EXPORT_SYMBOL(skb_append_datato_frags); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2890 | |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2891 | /** |
| 2892 | * skb_pull_rcsum - pull skb and update receive checksum |
| 2893 | * @skb: buffer to update |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2894 | * @len: length of data pulled |
| 2895 | * |
| 2896 | * This function performs an skb_pull on the packet and updates |
Urs Thuermann | fee54fa | 2008-02-12 22:03:25 -0800 | [diff] [blame] | 2897 | * the CHECKSUM_COMPLETE checksum. It should be used on |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2898 | * receive path processing instead of skb_pull unless you know |
| 2899 | * that the checksum difference is zero (e.g., a valid IP header) |
| 2900 | * or you are setting ip_summed to CHECKSUM_NONE. |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2901 | */ |
| 2902 | unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) |
| 2903 | { |
| 2904 | BUG_ON(len > skb->len); |
| 2905 | skb->len -= len; |
| 2906 | BUG_ON(skb->len < skb->data_len); |
| 2907 | skb_postpull_rcsum(skb, skb->data, len); |
| 2908 | return skb->data += len; |
| 2909 | } |
Arnaldo Carvalho de Melo | f94691a | 2006-03-20 22:47:55 -0800 | [diff] [blame] | 2910 | EXPORT_SYMBOL_GPL(skb_pull_rcsum); |
| 2911 | |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2912 | /** |
| 2913 | * skb_segment - Perform protocol segmentation on skb. |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2914 | * @head_skb: buffer to segment |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 2915 | * @features: features for the output path (see dev->features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2916 | * |
| 2917 | * This function performs segmentation on the given skb. It returns |
Ben Hutchings | 4c821d7 | 2008-04-13 21:52:48 -0700 | [diff] [blame] | 2918 | * a pointer to the first in a list of new skbs for the segments. |
| 2919 | * In case of error it returns ERR_PTR(err). |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2920 | */ |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2921 | struct sk_buff *skb_segment(struct sk_buff *head_skb, |
| 2922 | netdev_features_t features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2923 | { |
| 2924 | struct sk_buff *segs = NULL; |
| 2925 | struct sk_buff *tail = NULL; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 2926 | struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2927 | skb_frag_t *frag = skb_shinfo(head_skb)->frags; |
| 2928 | unsigned int mss = skb_shinfo(head_skb)->gso_size; |
| 2929 | unsigned int doffset = head_skb->data - skb_mac_header(head_skb); |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 2930 | struct sk_buff *frag_skb = head_skb; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2931 | unsigned int offset = doffset; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2932 | unsigned int tnl_hlen = skb_tnl_header_len(head_skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2933 | unsigned int headroom; |
| 2934 | unsigned int len; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 2935 | __be16 proto; |
| 2936 | bool csum; |
Michał Mirosław | 04ed3e7 | 2011-01-24 15:32:47 -0800 | [diff] [blame] | 2937 | int sg = !!(features & NETIF_F_SG); |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2938 | int nfrags = skb_shinfo(head_skb)->nr_frags; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2939 | int err = -ENOMEM; |
| 2940 | int i = 0; |
| 2941 | int pos; |
Vlad Yasevich | 53d6471 | 2014-03-27 17:26:18 -0400 | [diff] [blame] | 2942 | int dummy; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2943 | |
Wei-Chun Chao | 5882a07 | 2014-06-08 23:48:54 -0700 | [diff] [blame] | 2944 | __skb_push(head_skb, doffset); |
Vlad Yasevich | 53d6471 | 2014-03-27 17:26:18 -0400 | [diff] [blame] | 2945 | proto = skb_network_protocol(head_skb, &dummy); |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 2946 | if (unlikely(!proto)) |
| 2947 | return ERR_PTR(-EINVAL); |
| 2948 | |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 2949 | csum = !head_skb->encap_hdr_csum && |
| 2950 | !!can_checksum_protocol(features, proto); |
| 2951 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2952 | headroom = skb_headroom(head_skb); |
| 2953 | pos = skb_headlen(head_skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2954 | |
| 2955 | do { |
| 2956 | struct sk_buff *nskb; |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 2957 | skb_frag_t *nskb_frag; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 2958 | int hsize; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2959 | int size; |
| 2960 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2961 | len = head_skb->len - offset; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2962 | if (len > mss) |
| 2963 | len = mss; |
| 2964 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2965 | hsize = skb_headlen(head_skb) - offset; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2966 | if (hsize < 0) |
| 2967 | hsize = 0; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 2968 | if (hsize > len || !sg) |
| 2969 | hsize = len; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2970 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 2971 | if (!hsize && i >= nfrags && skb_headlen(list_skb) && |
| 2972 | (skb_headlen(list_skb) == len || sg)) { |
| 2973 | BUG_ON(skb_headlen(list_skb) > len); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2974 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 2975 | i = 0; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 2976 | nfrags = skb_shinfo(list_skb)->nr_frags; |
| 2977 | frag = skb_shinfo(list_skb)->frags; |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 2978 | frag_skb = list_skb; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 2979 | pos += skb_headlen(list_skb); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 2980 | |
| 2981 | while (pos < offset + len) { |
| 2982 | BUG_ON(i >= nfrags); |
| 2983 | |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 2984 | size = skb_frag_size(frag); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 2985 | if (pos + size > offset + len) |
| 2986 | break; |
| 2987 | |
| 2988 | i++; |
| 2989 | pos += size; |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 2990 | frag++; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 2991 | } |
| 2992 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 2993 | nskb = skb_clone(list_skb, GFP_ATOMIC); |
| 2994 | list_skb = list_skb->next; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 2995 | |
| 2996 | if (unlikely(!nskb)) |
| 2997 | goto err; |
| 2998 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 2999 | if (unlikely(pskb_trim(nskb, len))) { |
| 3000 | kfree_skb(nskb); |
| 3001 | goto err; |
| 3002 | } |
| 3003 | |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 3004 | hsize = skb_end_offset(nskb); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3005 | if (skb_cow_head(nskb, doffset + headroom)) { |
| 3006 | kfree_skb(nskb); |
| 3007 | goto err; |
| 3008 | } |
| 3009 | |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 3010 | nskb->truesize += skb_end_offset(nskb) - hsize; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3011 | skb_release_head_state(nskb); |
| 3012 | __skb_push(nskb, doffset); |
| 3013 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 3014 | nskb = __alloc_skb(hsize + doffset + headroom, |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3015 | GFP_ATOMIC, skb_alloc_rx_flag(head_skb), |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 3016 | NUMA_NO_NODE); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3017 | |
| 3018 | if (unlikely(!nskb)) |
| 3019 | goto err; |
| 3020 | |
| 3021 | skb_reserve(nskb, headroom); |
| 3022 | __skb_put(nskb, doffset); |
| 3023 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3024 | |
| 3025 | if (segs) |
| 3026 | tail->next = nskb; |
| 3027 | else |
| 3028 | segs = nskb; |
| 3029 | tail = nskb; |
| 3030 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3031 | __copy_skb_header(nskb, head_skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3032 | |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 3033 | skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); |
Vlad Yasevich | fcdfe3a | 2014-07-31 10:33:06 -0400 | [diff] [blame] | 3034 | skb_reset_mac_len(nskb); |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 3035 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3036 | skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 3037 | nskb->data - tnl_hlen, |
| 3038 | doffset + tnl_hlen); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3039 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3040 | if (nskb->len == len + doffset) |
Simon Horman | 1cdbcb7 | 2013-05-19 15:46:49 +0000 | [diff] [blame] | 3041 | goto perform_csum_check; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3042 | |
Tom Herbert | e585f23 | 2014-11-04 09:06:54 -0800 | [diff] [blame] | 3043 | if (!sg && !nskb->remcsum_offload) { |
Herbert Xu | 6f85a12 | 2008-08-15 14:55:02 -0700 | [diff] [blame] | 3044 | nskb->ip_summed = CHECKSUM_NONE; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3045 | nskb->csum = skb_copy_and_csum_bits(head_skb, offset, |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3046 | skb_put(nskb, len), |
| 3047 | len, 0); |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 3048 | SKB_GSO_CB(nskb)->csum_start = |
Tom Herbert | de84372 | 2014-06-25 12:51:01 -0700 | [diff] [blame] | 3049 | skb_headroom(nskb) + doffset; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3050 | continue; |
| 3051 | } |
| 3052 | |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3053 | nskb_frag = skb_shinfo(nskb)->frags; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3054 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3055 | skb_copy_from_linear_data_offset(head_skb, offset, |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 3056 | skb_put(nskb, hsize), hsize); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3057 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3058 | skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags & |
| 3059 | SKBTX_SHARED_FRAG; |
Eric Dumazet | cef401d | 2013-01-25 20:34:37 +0000 | [diff] [blame] | 3060 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3061 | while (pos < offset + len) { |
| 3062 | if (i >= nfrags) { |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3063 | BUG_ON(skb_headlen(list_skb)); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3064 | |
| 3065 | i = 0; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3066 | nfrags = skb_shinfo(list_skb)->nr_frags; |
| 3067 | frag = skb_shinfo(list_skb)->frags; |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 3068 | frag_skb = list_skb; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3069 | |
| 3070 | BUG_ON(!nfrags); |
| 3071 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3072 | list_skb = list_skb->next; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | if (unlikely(skb_shinfo(nskb)->nr_frags >= |
| 3076 | MAX_SKB_FRAGS)) { |
| 3077 | net_warn_ratelimited( |
| 3078 | "skb_segment: too many frags: %u %u\n", |
| 3079 | pos, mss); |
| 3080 | goto err; |
| 3081 | } |
| 3082 | |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 3083 | if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) |
| 3084 | goto err; |
| 3085 | |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 3086 | *nskb_frag = *frag; |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3087 | __skb_frag_ref(nskb_frag); |
| 3088 | size = skb_frag_size(nskb_frag); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3089 | |
| 3090 | if (pos < offset) { |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3091 | nskb_frag->page_offset += offset - pos; |
| 3092 | skb_frag_size_sub(nskb_frag, offset - pos); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3093 | } |
| 3094 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3095 | skb_shinfo(nskb)->nr_frags++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3096 | |
| 3097 | if (pos + size <= offset + len) { |
| 3098 | i++; |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 3099 | frag++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3100 | pos += size; |
| 3101 | } else { |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3102 | skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3103 | goto skip_fraglist; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3104 | } |
| 3105 | |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3106 | nskb_frag++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3107 | } |
| 3108 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3109 | skip_fraglist: |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3110 | nskb->data_len = len - hsize; |
| 3111 | nskb->len += nskb->data_len; |
| 3112 | nskb->truesize += nskb->data_len; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3113 | |
Simon Horman | 1cdbcb7 | 2013-05-19 15:46:49 +0000 | [diff] [blame] | 3114 | perform_csum_check: |
Tom Herbert | e585f23 | 2014-11-04 09:06:54 -0800 | [diff] [blame] | 3115 | if (!csum && !nskb->remcsum_offload) { |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3116 | nskb->csum = skb_checksum(nskb, doffset, |
| 3117 | nskb->len - doffset, 0); |
| 3118 | nskb->ip_summed = CHECKSUM_NONE; |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 3119 | SKB_GSO_CB(nskb)->csum_start = |
| 3120 | skb_headroom(nskb) + doffset; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3121 | } |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3122 | } while ((offset += len) < head_skb->len); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3123 | |
Eric Dumazet | bec3cfd | 2014-10-03 20:59:19 -0700 | [diff] [blame] | 3124 | /* Some callers want to get the end of the list. |
| 3125 | * Put it in segs->prev to avoid walking the list. |
| 3126 | * (see validate_xmit_skb_list() for example) |
| 3127 | */ |
| 3128 | segs->prev = tail; |
Toshiaki Makita | 432c856 | 2014-10-27 10:30:51 -0700 | [diff] [blame] | 3129 | |
| 3130 | /* Following permits correct backpressure, for protocols |
| 3131 | * using skb_set_owner_w(). |
| 3132 | * Idea is to tranfert ownership from head_skb to last segment. |
| 3133 | */ |
| 3134 | if (head_skb->destructor == sock_wfree) { |
| 3135 | swap(tail->truesize, head_skb->truesize); |
| 3136 | swap(tail->destructor, head_skb->destructor); |
| 3137 | swap(tail->sk, head_skb->sk); |
| 3138 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3139 | return segs; |
| 3140 | |
| 3141 | err: |
Eric Dumazet | 289dccb | 2013-12-20 14:29:08 -0800 | [diff] [blame] | 3142 | kfree_skb_list(segs); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3143 | return ERR_PTR(err); |
| 3144 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3145 | EXPORT_SYMBOL_GPL(skb_segment); |
| 3146 | |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3147 | int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) |
| 3148 | { |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3149 | struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3150 | unsigned int offset = skb_gro_offset(skb); |
| 3151 | unsigned int headlen = skb_headlen(skb); |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3152 | struct sk_buff *nskb, *lp, *p = *head; |
| 3153 | unsigned int len = skb_gro_len(skb); |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3154 | unsigned int delta_truesize; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3155 | unsigned int headroom; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3156 | |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3157 | if (unlikely(p->len + len >= 65536)) |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3158 | return -E2BIG; |
| 3159 | |
Eric Dumazet | 29e9824 | 2014-05-16 11:34:37 -0700 | [diff] [blame] | 3160 | lp = NAPI_GRO_CB(p)->last; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3161 | pinfo = skb_shinfo(lp); |
| 3162 | |
| 3163 | if (headlen <= offset) { |
Herbert Xu | 42da699 | 2009-05-26 18:50:19 +0000 | [diff] [blame] | 3164 | skb_frag_t *frag; |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3165 | skb_frag_t *frag2; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3166 | int i = skbinfo->nr_frags; |
| 3167 | int nr_frags = pinfo->nr_frags + i; |
Herbert Xu | 42da699 | 2009-05-26 18:50:19 +0000 | [diff] [blame] | 3168 | |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3169 | if (nr_frags > MAX_SKB_FRAGS) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3170 | goto merge; |
Herbert Xu | 81705ad | 2009-01-29 14:19:51 +0000 | [diff] [blame] | 3171 | |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3172 | offset -= headlen; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3173 | pinfo->nr_frags = nr_frags; |
| 3174 | skbinfo->nr_frags = 0; |
Herbert Xu | f557206 | 2009-01-14 20:40:03 -0800 | [diff] [blame] | 3175 | |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3176 | frag = pinfo->frags + nr_frags; |
| 3177 | frag2 = skbinfo->frags + i; |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3178 | do { |
| 3179 | *--frag = *--frag2; |
| 3180 | } while (--i); |
| 3181 | |
| 3182 | frag->page_offset += offset; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3183 | skb_frag_size_sub(frag, offset); |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3184 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3185 | /* all fragments truesize : remove (head size + sk_buff) */ |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 3186 | delta_truesize = skb->truesize - |
| 3187 | SKB_TRUESIZE(skb_end_offset(skb)); |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3188 | |
Herbert Xu | f557206 | 2009-01-14 20:40:03 -0800 | [diff] [blame] | 3189 | skb->truesize -= skb->data_len; |
| 3190 | skb->len -= skb->data_len; |
| 3191 | skb->data_len = 0; |
| 3192 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3193 | NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3194 | goto done; |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 3195 | } else if (skb->head_frag) { |
| 3196 | int nr_frags = pinfo->nr_frags; |
| 3197 | skb_frag_t *frag = pinfo->frags + nr_frags; |
| 3198 | struct page *page = virt_to_head_page(skb->head); |
| 3199 | unsigned int first_size = headlen - offset; |
| 3200 | unsigned int first_offset; |
| 3201 | |
| 3202 | if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3203 | goto merge; |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 3204 | |
| 3205 | first_offset = skb->data - |
| 3206 | (unsigned char *)page_address(page) + |
| 3207 | offset; |
| 3208 | |
| 3209 | pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; |
| 3210 | |
| 3211 | frag->page.p = page; |
| 3212 | frag->page_offset = first_offset; |
| 3213 | skb_frag_size_set(frag, first_size); |
| 3214 | |
| 3215 | memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); |
| 3216 | /* We dont need to clear skbinfo->nr_frags here */ |
| 3217 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3218 | delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 3219 | NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; |
| 3220 | goto done; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3221 | } |
Eric Dumazet | 73d3fe6 | 2014-09-29 10:34:29 -0700 | [diff] [blame] | 3222 | /* switch back to head shinfo */ |
| 3223 | pinfo = skb_shinfo(p); |
| 3224 | |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3225 | if (pinfo->frag_list) |
| 3226 | goto merge; |
| 3227 | if (skb_gro_len(p) != pinfo->gso_size) |
Herbert Xu | 69c0cab | 2009-11-17 05:18:18 -0800 | [diff] [blame] | 3228 | return -E2BIG; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3229 | |
| 3230 | headroom = skb_headroom(p); |
Eric Dumazet | 3d3be43 | 2010-09-01 00:50:51 +0000 | [diff] [blame] | 3231 | nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3232 | if (unlikely(!nskb)) |
| 3233 | return -ENOMEM; |
| 3234 | |
| 3235 | __copy_skb_header(nskb, p); |
| 3236 | nskb->mac_len = p->mac_len; |
| 3237 | |
| 3238 | skb_reserve(nskb, headroom); |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3239 | __skb_put(nskb, skb_gro_offset(p)); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3240 | |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3241 | skb_set_mac_header(nskb, skb_mac_header(p) - p->data); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3242 | skb_set_network_header(nskb, skb_network_offset(p)); |
| 3243 | skb_set_transport_header(nskb, skb_transport_offset(p)); |
| 3244 | |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3245 | __skb_pull(p, skb_gro_offset(p)); |
| 3246 | memcpy(skb_mac_header(nskb), skb_mac_header(p), |
| 3247 | p->data - skb_mac_header(p)); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3248 | |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3249 | skb_shinfo(nskb)->frag_list = p; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3250 | skb_shinfo(nskb)->gso_size = pinfo->gso_size; |
Herbert Xu | 622e0ca | 2010-05-20 23:07:56 -0700 | [diff] [blame] | 3251 | pinfo->gso_size = 0; |
Eric Dumazet | f4a775d | 2014-09-22 16:29:32 -0700 | [diff] [blame] | 3252 | __skb_header_release(p); |
Eric Dumazet | c3c7c25 | 2012-12-06 13:54:59 +0000 | [diff] [blame] | 3253 | NAPI_GRO_CB(nskb)->last = p; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3254 | |
| 3255 | nskb->data_len += p->len; |
Eric Dumazet | de8261c | 2012-02-13 04:09:20 +0000 | [diff] [blame] | 3256 | nskb->truesize += p->truesize; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3257 | nskb->len += p->len; |
| 3258 | |
| 3259 | *head = nskb; |
| 3260 | nskb->next = p->next; |
| 3261 | p->next = NULL; |
| 3262 | |
| 3263 | p = nskb; |
| 3264 | |
| 3265 | merge: |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3266 | delta_truesize = skb->truesize; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3267 | if (offset > headlen) { |
Michal Schmidt | d1dc7ab | 2011-01-24 12:08:48 +0000 | [diff] [blame] | 3268 | unsigned int eat = offset - headlen; |
| 3269 | |
| 3270 | skbinfo->frags[0].page_offset += eat; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3271 | skb_frag_size_sub(&skbinfo->frags[0], eat); |
Michal Schmidt | d1dc7ab | 2011-01-24 12:08:48 +0000 | [diff] [blame] | 3272 | skb->data_len -= eat; |
| 3273 | skb->len -= eat; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3274 | offset = headlen; |
Herbert Xu | 5603502 | 2009-02-05 21:26:52 -0800 | [diff] [blame] | 3275 | } |
| 3276 | |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3277 | __skb_pull(skb, offset); |
Herbert Xu | 5603502 | 2009-02-05 21:26:52 -0800 | [diff] [blame] | 3278 | |
Eric Dumazet | 29e9824 | 2014-05-16 11:34:37 -0700 | [diff] [blame] | 3279 | if (NAPI_GRO_CB(p)->last == p) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3280 | skb_shinfo(p)->frag_list = skb; |
| 3281 | else |
| 3282 | NAPI_GRO_CB(p)->last->next = skb; |
Eric Dumazet | c3c7c25 | 2012-12-06 13:54:59 +0000 | [diff] [blame] | 3283 | NAPI_GRO_CB(p)->last = skb; |
Eric Dumazet | f4a775d | 2014-09-22 16:29:32 -0700 | [diff] [blame] | 3284 | __skb_header_release(skb); |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3285 | lp = p; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3286 | |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3287 | done: |
| 3288 | NAPI_GRO_CB(p)->count++; |
Herbert Xu | 37fe473 | 2009-01-17 19:48:13 +0000 | [diff] [blame] | 3289 | p->data_len += len; |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3290 | p->truesize += delta_truesize; |
Herbert Xu | 37fe473 | 2009-01-17 19:48:13 +0000 | [diff] [blame] | 3291 | p->len += len; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3292 | if (lp != p) { |
| 3293 | lp->data_len += len; |
| 3294 | lp->truesize += delta_truesize; |
| 3295 | lp->len += len; |
| 3296 | } |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3297 | NAPI_GRO_CB(skb)->same_flow = 1; |
| 3298 | return 0; |
| 3299 | } |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | void __init skb_init(void) |
| 3302 | { |
| 3303 | skbuff_head_cache = kmem_cache_create("skbuff_head_cache", |
| 3304 | sizeof(struct sk_buff), |
| 3305 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 3306 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3307 | NULL); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 3308 | skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 3309 | sizeof(struct sk_buff_fclones), |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 3310 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 3311 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3312 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | } |
| 3314 | |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3315 | /** |
| 3316 | * skb_to_sgvec - Fill a scatter-gather list from a socket buffer |
| 3317 | * @skb: Socket buffer containing the buffers to be mapped |
| 3318 | * @sg: The scatter-gather list to map into |
| 3319 | * @offset: The offset into the buffer's contents to start mapping |
| 3320 | * @len: Length of buffer space to be mapped |
| 3321 | * |
| 3322 | * Fill the specified scatter-gather list with mappings/pointers into a |
| 3323 | * region of the buffer space attached to a socket buffer. |
| 3324 | */ |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3325 | static int |
| 3326 | __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] | 3327 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3328 | int start = skb_headlen(skb); |
| 3329 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3330 | struct sk_buff *frag_iter; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3331 | int elt = 0; |
| 3332 | |
| 3333 | if (copy > 0) { |
| 3334 | if (copy > len) |
| 3335 | copy = len; |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 3336 | sg_set_buf(sg, skb->data + offset, copy); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3337 | elt++; |
| 3338 | if ((len -= copy) == 0) |
| 3339 | return elt; |
| 3340 | offset += copy; |
| 3341 | } |
| 3342 | |
| 3343 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3344 | int end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3345 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 3346 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3347 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3348 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3349 | if ((copy = end - offset) > 0) { |
| 3350 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 3351 | |
| 3352 | if (copy > len) |
| 3353 | copy = len; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3354 | sg_set_page(&sg[elt], skb_frag_page(frag), copy, |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 3355 | frag->page_offset+offset-start); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3356 | elt++; |
| 3357 | if (!(len -= copy)) |
| 3358 | return elt; |
| 3359 | offset += copy; |
| 3360 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3361 | start = end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3364 | skb_walk_frags(skb, frag_iter) { |
| 3365 | int end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3366 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3367 | WARN_ON(start > offset + len); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3368 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3369 | end = start + frag_iter->len; |
| 3370 | if ((copy = end - offset) > 0) { |
| 3371 | if (copy > len) |
| 3372 | copy = len; |
| 3373 | elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, |
| 3374 | copy); |
| 3375 | if ((len -= copy) == 0) |
| 3376 | return elt; |
| 3377 | offset += copy; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3378 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3379 | start = end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3380 | } |
| 3381 | BUG_ON(len); |
| 3382 | return elt; |
| 3383 | } |
| 3384 | |
Fan Du | 25a91d8 | 2014-01-18 09:54:23 +0800 | [diff] [blame] | 3385 | /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given |
| 3386 | * sglist without mark the sg which contain last skb data as the end. |
| 3387 | * So the caller can mannipulate sg list as will when padding new data after |
| 3388 | * the first call without calling sg_unmark_end to expend sg list. |
| 3389 | * |
| 3390 | * Scenario to use skb_to_sgvec_nomark: |
| 3391 | * 1. sg_init_table |
| 3392 | * 2. skb_to_sgvec_nomark(payload1) |
| 3393 | * 3. skb_to_sgvec_nomark(payload2) |
| 3394 | * |
| 3395 | * This is equivalent to: |
| 3396 | * 1. sg_init_table |
| 3397 | * 2. skb_to_sgvec(payload1) |
| 3398 | * 3. sg_unmark_end |
| 3399 | * 4. skb_to_sgvec(payload2) |
| 3400 | * |
| 3401 | * When mapping mutilple payload conditionally, skb_to_sgvec_nomark |
| 3402 | * is more preferable. |
| 3403 | */ |
| 3404 | int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, |
| 3405 | int offset, int len) |
| 3406 | { |
| 3407 | return __skb_to_sgvec(skb, sg, offset, len); |
| 3408 | } |
| 3409 | EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); |
| 3410 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3411 | int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) |
| 3412 | { |
| 3413 | int nsg = __skb_to_sgvec(skb, sg, offset, len); |
| 3414 | |
Jens Axboe | c46f233 | 2007-10-31 12:06:37 +0100 | [diff] [blame] | 3415 | sg_mark_end(&sg[nsg - 1]); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3416 | |
| 3417 | return nsg; |
| 3418 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3419 | EXPORT_SYMBOL_GPL(skb_to_sgvec); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3420 | |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3421 | /** |
| 3422 | * skb_cow_data - Check that a socket buffer's data buffers are writable |
| 3423 | * @skb: The socket buffer to check. |
| 3424 | * @tailbits: Amount of trailing space to be added |
| 3425 | * @trailer: Returned pointer to the skb where the @tailbits space begins |
| 3426 | * |
| 3427 | * Make sure that the data buffers attached to a socket buffer are |
| 3428 | * writable. If they are not, private copies are made of the data buffers |
| 3429 | * and the socket buffer is set to use these instead. |
| 3430 | * |
| 3431 | * If @tailbits is given, make sure that there is space to write @tailbits |
| 3432 | * bytes of data beyond current end of socket buffer. @trailer will be |
| 3433 | * set to point to the skb in which this space begins. |
| 3434 | * |
| 3435 | * The number of scatterlist elements required to completely map the |
| 3436 | * COW'd and extended socket buffer will be returned. |
| 3437 | */ |
| 3438 | int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) |
| 3439 | { |
| 3440 | int copyflag; |
| 3441 | int elt; |
| 3442 | struct sk_buff *skb1, **skb_p; |
| 3443 | |
| 3444 | /* If skb is cloned or its head is paged, reallocate |
| 3445 | * head pulling out all the pages (pages are considered not writable |
| 3446 | * at the moment even if they are anonymous). |
| 3447 | */ |
| 3448 | if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && |
| 3449 | __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) |
| 3450 | return -ENOMEM; |
| 3451 | |
| 3452 | /* Easy case. Most of packets will go this way. */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3453 | if (!skb_has_frag_list(skb)) { |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3454 | /* A little of trouble, not enough of space for trailer. |
| 3455 | * This should not happen, when stack is tuned to generate |
| 3456 | * good frames. OK, on miss we reallocate and reserve even more |
| 3457 | * space, 128 bytes is fair. */ |
| 3458 | |
| 3459 | if (skb_tailroom(skb) < tailbits && |
| 3460 | pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) |
| 3461 | return -ENOMEM; |
| 3462 | |
| 3463 | /* Voila! */ |
| 3464 | *trailer = skb; |
| 3465 | return 1; |
| 3466 | } |
| 3467 | |
| 3468 | /* Misery. We are in troubles, going to mincer fragments... */ |
| 3469 | |
| 3470 | elt = 1; |
| 3471 | skb_p = &skb_shinfo(skb)->frag_list; |
| 3472 | copyflag = 0; |
| 3473 | |
| 3474 | while ((skb1 = *skb_p) != NULL) { |
| 3475 | int ntail = 0; |
| 3476 | |
| 3477 | /* The fragment is partially pulled by someone, |
| 3478 | * this can happen on input. Copy it and everything |
| 3479 | * after it. */ |
| 3480 | |
| 3481 | if (skb_shared(skb1)) |
| 3482 | copyflag = 1; |
| 3483 | |
| 3484 | /* If the skb is the last, worry about trailer. */ |
| 3485 | |
| 3486 | if (skb1->next == NULL && tailbits) { |
| 3487 | if (skb_shinfo(skb1)->nr_frags || |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3488 | skb_has_frag_list(skb1) || |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3489 | skb_tailroom(skb1) < tailbits) |
| 3490 | ntail = tailbits + 128; |
| 3491 | } |
| 3492 | |
| 3493 | if (copyflag || |
| 3494 | skb_cloned(skb1) || |
| 3495 | ntail || |
| 3496 | skb_shinfo(skb1)->nr_frags || |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3497 | skb_has_frag_list(skb1)) { |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3498 | struct sk_buff *skb2; |
| 3499 | |
| 3500 | /* Fuck, we are miserable poor guys... */ |
| 3501 | if (ntail == 0) |
| 3502 | skb2 = skb_copy(skb1, GFP_ATOMIC); |
| 3503 | else |
| 3504 | skb2 = skb_copy_expand(skb1, |
| 3505 | skb_headroom(skb1), |
| 3506 | ntail, |
| 3507 | GFP_ATOMIC); |
| 3508 | if (unlikely(skb2 == NULL)) |
| 3509 | return -ENOMEM; |
| 3510 | |
| 3511 | if (skb1->sk) |
| 3512 | skb_set_owner_w(skb2, skb1->sk); |
| 3513 | |
| 3514 | /* Looking around. Are we still alive? |
| 3515 | * OK, link new skb, drop old one */ |
| 3516 | |
| 3517 | skb2->next = skb1->next; |
| 3518 | *skb_p = skb2; |
| 3519 | kfree_skb(skb1); |
| 3520 | skb1 = skb2; |
| 3521 | } |
| 3522 | elt++; |
| 3523 | *trailer = skb1; |
| 3524 | skb_p = &skb1->next; |
| 3525 | } |
| 3526 | |
| 3527 | return elt; |
| 3528 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3529 | EXPORT_SYMBOL_GPL(skb_cow_data); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3530 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3531 | static void sock_rmem_free(struct sk_buff *skb) |
| 3532 | { |
| 3533 | struct sock *sk = skb->sk; |
| 3534 | |
| 3535 | atomic_sub(skb->truesize, &sk->sk_rmem_alloc); |
| 3536 | } |
| 3537 | |
| 3538 | /* |
| 3539 | * Note: We dont mem charge error packets (no sk_forward_alloc changes) |
| 3540 | */ |
| 3541 | int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) |
| 3542 | { |
| 3543 | if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3544 | (unsigned int)sk->sk_rcvbuf) |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3545 | return -ENOMEM; |
| 3546 | |
| 3547 | skb_orphan(skb); |
| 3548 | skb->sk = sk; |
| 3549 | skb->destructor = sock_rmem_free; |
| 3550 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); |
| 3551 | |
Eric Dumazet | abb57ea | 2011-05-18 02:21:31 -0400 | [diff] [blame] | 3552 | /* before exiting rcu section, make sure dst is refcounted */ |
| 3553 | skb_dst_force(skb); |
| 3554 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3555 | skb_queue_tail(&sk->sk_error_queue, skb); |
| 3556 | if (!sock_flag(sk, SOCK_DEAD)) |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 3557 | sk->sk_data_ready(sk); |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3558 | return 0; |
| 3559 | } |
| 3560 | EXPORT_SYMBOL(sock_queue_err_skb); |
| 3561 | |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 3562 | struct sk_buff *sock_dequeue_err_skb(struct sock *sk) |
| 3563 | { |
| 3564 | struct sk_buff_head *q = &sk->sk_error_queue; |
| 3565 | struct sk_buff *skb, *skb_next; |
| 3566 | int err = 0; |
| 3567 | |
| 3568 | spin_lock_bh(&q->lock); |
| 3569 | skb = __skb_dequeue(q); |
| 3570 | if (skb && (skb_next = skb_peek(q))) |
| 3571 | err = SKB_EXT_ERR(skb_next)->ee.ee_errno; |
| 3572 | spin_unlock_bh(&q->lock); |
| 3573 | |
| 3574 | sk->sk_err = err; |
| 3575 | if (err) |
| 3576 | sk->sk_error_report(sk); |
| 3577 | |
| 3578 | return skb; |
| 3579 | } |
| 3580 | EXPORT_SYMBOL(sock_dequeue_err_skb); |
| 3581 | |
Alexander Duyck | cab41c4 | 2014-09-10 18:05:26 -0400 | [diff] [blame] | 3582 | /** |
| 3583 | * skb_clone_sk - create clone of skb, and take reference to socket |
| 3584 | * @skb: the skb to clone |
| 3585 | * |
| 3586 | * This function creates a clone of a buffer that holds a reference on |
| 3587 | * sk_refcnt. Buffers created via this function are meant to be |
| 3588 | * returned using sock_queue_err_skb, or free via kfree_skb. |
| 3589 | * |
| 3590 | * When passing buffers allocated with this function to sock_queue_err_skb |
| 3591 | * it is necessary to wrap the call with sock_hold/sock_put in order to |
| 3592 | * prevent the socket from being released prior to being enqueued on |
| 3593 | * the sk_error_queue. |
| 3594 | */ |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 3595 | struct sk_buff *skb_clone_sk(struct sk_buff *skb) |
| 3596 | { |
| 3597 | struct sock *sk = skb->sk; |
| 3598 | struct sk_buff *clone; |
| 3599 | |
| 3600 | if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt)) |
| 3601 | return NULL; |
| 3602 | |
| 3603 | clone = skb_clone(skb, GFP_ATOMIC); |
| 3604 | if (!clone) { |
| 3605 | sock_put(sk); |
| 3606 | return NULL; |
| 3607 | } |
| 3608 | |
| 3609 | clone->sk = sk; |
| 3610 | clone->destructor = sock_efree; |
| 3611 | |
| 3612 | return clone; |
| 3613 | } |
| 3614 | EXPORT_SYMBOL(skb_clone_sk); |
| 3615 | |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3616 | static void __skb_complete_tx_timestamp(struct sk_buff *skb, |
| 3617 | struct sock *sk, |
| 3618 | int tstype) |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3619 | { |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3620 | struct sock_exterr_skb *serr; |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3621 | int err; |
| 3622 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3623 | serr = SKB_EXT_ERR(skb); |
| 3624 | memset(serr, 0, sizeof(*serr)); |
| 3625 | serr->ee.ee_errno = ENOMSG; |
| 3626 | serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; |
Willem de Bruijn | e7fd288 | 2014-08-04 22:11:48 -0400 | [diff] [blame] | 3627 | serr->ee.ee_info = tstype; |
Willem de Bruijn | 4ed2d76 | 2014-08-04 22:11:49 -0400 | [diff] [blame] | 3628 | if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { |
Willem de Bruijn | 09c2d25 | 2014-08-04 22:11:47 -0400 | [diff] [blame] | 3629 | serr->ee.ee_data = skb_shinfo(skb)->tskey; |
Willem de Bruijn | 4ed2d76 | 2014-08-04 22:11:49 -0400 | [diff] [blame] | 3630 | if (sk->sk_protocol == IPPROTO_TCP) |
| 3631 | serr->ee.ee_data -= sk->sk_tskey; |
| 3632 | } |
Eric Dumazet | 2903037 | 2010-05-29 00:20:48 -0700 | [diff] [blame] | 3633 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3634 | err = sock_queue_err_skb(sk, skb); |
Eric Dumazet | 2903037 | 2010-05-29 00:20:48 -0700 | [diff] [blame] | 3635 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3636 | if (err) |
| 3637 | kfree_skb(skb); |
| 3638 | } |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3639 | |
| 3640 | void skb_complete_tx_timestamp(struct sk_buff *skb, |
| 3641 | struct skb_shared_hwtstamps *hwtstamps) |
| 3642 | { |
| 3643 | struct sock *sk = skb->sk; |
| 3644 | |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 3645 | /* take a reference to prevent skb_orphan() from freeing the socket */ |
| 3646 | sock_hold(sk); |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3647 | |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 3648 | *skb_hwtstamps(skb) = *hwtstamps; |
| 3649 | __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND); |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3650 | |
| 3651 | sock_put(sk); |
| 3652 | } |
| 3653 | EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); |
| 3654 | |
| 3655 | void __skb_tstamp_tx(struct sk_buff *orig_skb, |
| 3656 | struct skb_shared_hwtstamps *hwtstamps, |
| 3657 | struct sock *sk, int tstype) |
| 3658 | { |
| 3659 | struct sk_buff *skb; |
| 3660 | |
| 3661 | if (!sk) |
| 3662 | return; |
| 3663 | |
| 3664 | if (hwtstamps) |
| 3665 | *skb_hwtstamps(orig_skb) = *hwtstamps; |
| 3666 | else |
| 3667 | orig_skb->tstamp = ktime_get_real(); |
| 3668 | |
| 3669 | skb = skb_clone(orig_skb, GFP_ATOMIC); |
| 3670 | if (!skb) |
| 3671 | return; |
| 3672 | |
| 3673 | __skb_complete_tx_timestamp(skb, sk, tstype); |
| 3674 | } |
Willem de Bruijn | e7fd288 | 2014-08-04 22:11:48 -0400 | [diff] [blame] | 3675 | EXPORT_SYMBOL_GPL(__skb_tstamp_tx); |
| 3676 | |
| 3677 | void skb_tstamp_tx(struct sk_buff *orig_skb, |
| 3678 | struct skb_shared_hwtstamps *hwtstamps) |
| 3679 | { |
| 3680 | return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, |
| 3681 | SCM_TSTAMP_SND); |
| 3682 | } |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3683 | EXPORT_SYMBOL_GPL(skb_tstamp_tx); |
| 3684 | |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 3685 | void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) |
| 3686 | { |
| 3687 | struct sock *sk = skb->sk; |
| 3688 | struct sock_exterr_skb *serr; |
| 3689 | int err; |
| 3690 | |
| 3691 | skb->wifi_acked_valid = 1; |
| 3692 | skb->wifi_acked = acked; |
| 3693 | |
| 3694 | serr = SKB_EXT_ERR(skb); |
| 3695 | memset(serr, 0, sizeof(*serr)); |
| 3696 | serr->ee.ee_errno = ENOMSG; |
| 3697 | serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; |
| 3698 | |
Alexander Duyck | bf7fa55 | 2014-09-10 18:05:42 -0400 | [diff] [blame] | 3699 | /* take a reference to prevent skb_orphan() from freeing the socket */ |
| 3700 | sock_hold(sk); |
| 3701 | |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 3702 | err = sock_queue_err_skb(sk, skb); |
| 3703 | if (err) |
| 3704 | kfree_skb(skb); |
Alexander Duyck | bf7fa55 | 2014-09-10 18:05:42 -0400 | [diff] [blame] | 3705 | |
| 3706 | sock_put(sk); |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 3707 | } |
| 3708 | EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); |
| 3709 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3710 | |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3711 | /** |
| 3712 | * skb_partial_csum_set - set up and verify partial csum values for packet |
| 3713 | * @skb: the skb to set |
| 3714 | * @start: the number of bytes after skb->data to start checksumming. |
| 3715 | * @off: the offset from start to place the checksum. |
| 3716 | * |
| 3717 | * For untrusted partially-checksummed packets, we need to make sure the values |
| 3718 | * for skb->csum_start and skb->csum_offset are valid so we don't oops. |
| 3719 | * |
| 3720 | * This function checks and sets those values and skb->ip_summed: if this |
| 3721 | * returns false you should drop the packet. |
| 3722 | */ |
| 3723 | bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) |
| 3724 | { |
Herbert Xu | 5ff8dda | 2009-06-04 01:22:01 +0000 | [diff] [blame] | 3725 | if (unlikely(start > skb_headlen(skb)) || |
| 3726 | unlikely((int)start + off > skb_headlen(skb) - 2)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 3727 | net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", |
| 3728 | start, off, skb_headlen(skb)); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3729 | return false; |
| 3730 | } |
| 3731 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 3732 | skb->csum_start = skb_headroom(skb) + start; |
| 3733 | skb->csum_offset = off; |
Jason Wang | e5d5dec | 2013-03-26 23:11:20 +0000 | [diff] [blame] | 3734 | skb_set_transport_header(skb, start); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3735 | return true; |
| 3736 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3737 | EXPORT_SYMBOL_GPL(skb_partial_csum_set); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3738 | |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3739 | static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, |
| 3740 | unsigned int max) |
| 3741 | { |
| 3742 | if (skb_headlen(skb) >= len) |
| 3743 | return 0; |
| 3744 | |
| 3745 | /* If we need to pullup then pullup to the max, so we |
| 3746 | * won't need to do it again. |
| 3747 | */ |
| 3748 | if (max > skb->len) |
| 3749 | max = skb->len; |
| 3750 | |
| 3751 | if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) |
| 3752 | return -ENOMEM; |
| 3753 | |
| 3754 | if (skb_headlen(skb) < len) |
| 3755 | return -EPROTO; |
| 3756 | |
| 3757 | return 0; |
| 3758 | } |
| 3759 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3760 | #define MAX_TCP_HDR_LEN (15 * 4) |
| 3761 | |
| 3762 | static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, |
| 3763 | typeof(IPPROTO_IP) proto, |
| 3764 | unsigned int off) |
| 3765 | { |
| 3766 | switch (proto) { |
| 3767 | int err; |
| 3768 | |
| 3769 | case IPPROTO_TCP: |
| 3770 | err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), |
| 3771 | off + MAX_TCP_HDR_LEN); |
| 3772 | if (!err && !skb_partial_csum_set(skb, off, |
| 3773 | offsetof(struct tcphdr, |
| 3774 | check))) |
| 3775 | err = -EPROTO; |
| 3776 | return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; |
| 3777 | |
| 3778 | case IPPROTO_UDP: |
| 3779 | err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), |
| 3780 | off + sizeof(struct udphdr)); |
| 3781 | if (!err && !skb_partial_csum_set(skb, off, |
| 3782 | offsetof(struct udphdr, |
| 3783 | check))) |
| 3784 | err = -EPROTO; |
| 3785 | return err ? ERR_PTR(err) : &udp_hdr(skb)->check; |
| 3786 | } |
| 3787 | |
| 3788 | return ERR_PTR(-EPROTO); |
| 3789 | } |
| 3790 | |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3791 | /* This value should be large enough to cover a tagged ethernet header plus |
| 3792 | * maximally sized IP and TCP or UDP headers. |
| 3793 | */ |
| 3794 | #define MAX_IP_HDR_LEN 128 |
| 3795 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3796 | static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3797 | { |
| 3798 | unsigned int off; |
| 3799 | bool fragment; |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3800 | __sum16 *csum; |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3801 | int err; |
| 3802 | |
| 3803 | fragment = false; |
| 3804 | |
| 3805 | err = skb_maybe_pull_tail(skb, |
| 3806 | sizeof(struct iphdr), |
| 3807 | MAX_IP_HDR_LEN); |
| 3808 | if (err < 0) |
| 3809 | goto out; |
| 3810 | |
| 3811 | if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) |
| 3812 | fragment = true; |
| 3813 | |
| 3814 | off = ip_hdrlen(skb); |
| 3815 | |
| 3816 | err = -EPROTO; |
| 3817 | |
| 3818 | if (fragment) |
| 3819 | goto out; |
| 3820 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3821 | csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); |
| 3822 | if (IS_ERR(csum)) |
| 3823 | return PTR_ERR(csum); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3824 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3825 | if (recalculate) |
| 3826 | *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 3827 | ip_hdr(skb)->daddr, |
| 3828 | skb->len - off, |
| 3829 | ip_hdr(skb)->protocol, 0); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3830 | err = 0; |
| 3831 | |
| 3832 | out: |
| 3833 | return err; |
| 3834 | } |
| 3835 | |
| 3836 | /* This value should be large enough to cover a tagged ethernet header plus |
| 3837 | * an IPv6 header, all options, and a maximal TCP or UDP header. |
| 3838 | */ |
| 3839 | #define MAX_IPV6_HDR_LEN 256 |
| 3840 | |
| 3841 | #define OPT_HDR(type, skb, off) \ |
| 3842 | (type *)(skb_network_header(skb) + (off)) |
| 3843 | |
| 3844 | static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) |
| 3845 | { |
| 3846 | int err; |
| 3847 | u8 nexthdr; |
| 3848 | unsigned int off; |
| 3849 | unsigned int len; |
| 3850 | bool fragment; |
| 3851 | bool done; |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3852 | __sum16 *csum; |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3853 | |
| 3854 | fragment = false; |
| 3855 | done = false; |
| 3856 | |
| 3857 | off = sizeof(struct ipv6hdr); |
| 3858 | |
| 3859 | err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); |
| 3860 | if (err < 0) |
| 3861 | goto out; |
| 3862 | |
| 3863 | nexthdr = ipv6_hdr(skb)->nexthdr; |
| 3864 | |
| 3865 | len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); |
| 3866 | while (off <= len && !done) { |
| 3867 | switch (nexthdr) { |
| 3868 | case IPPROTO_DSTOPTS: |
| 3869 | case IPPROTO_HOPOPTS: |
| 3870 | case IPPROTO_ROUTING: { |
| 3871 | struct ipv6_opt_hdr *hp; |
| 3872 | |
| 3873 | err = skb_maybe_pull_tail(skb, |
| 3874 | off + |
| 3875 | sizeof(struct ipv6_opt_hdr), |
| 3876 | MAX_IPV6_HDR_LEN); |
| 3877 | if (err < 0) |
| 3878 | goto out; |
| 3879 | |
| 3880 | hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); |
| 3881 | nexthdr = hp->nexthdr; |
| 3882 | off += ipv6_optlen(hp); |
| 3883 | break; |
| 3884 | } |
| 3885 | case IPPROTO_AH: { |
| 3886 | struct ip_auth_hdr *hp; |
| 3887 | |
| 3888 | err = skb_maybe_pull_tail(skb, |
| 3889 | off + |
| 3890 | sizeof(struct ip_auth_hdr), |
| 3891 | MAX_IPV6_HDR_LEN); |
| 3892 | if (err < 0) |
| 3893 | goto out; |
| 3894 | |
| 3895 | hp = OPT_HDR(struct ip_auth_hdr, skb, off); |
| 3896 | nexthdr = hp->nexthdr; |
| 3897 | off += ipv6_authlen(hp); |
| 3898 | break; |
| 3899 | } |
| 3900 | case IPPROTO_FRAGMENT: { |
| 3901 | struct frag_hdr *hp; |
| 3902 | |
| 3903 | err = skb_maybe_pull_tail(skb, |
| 3904 | off + |
| 3905 | sizeof(struct frag_hdr), |
| 3906 | MAX_IPV6_HDR_LEN); |
| 3907 | if (err < 0) |
| 3908 | goto out; |
| 3909 | |
| 3910 | hp = OPT_HDR(struct frag_hdr, skb, off); |
| 3911 | |
| 3912 | if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) |
| 3913 | fragment = true; |
| 3914 | |
| 3915 | nexthdr = hp->nexthdr; |
| 3916 | off += sizeof(struct frag_hdr); |
| 3917 | break; |
| 3918 | } |
| 3919 | default: |
| 3920 | done = true; |
| 3921 | break; |
| 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | err = -EPROTO; |
| 3926 | |
| 3927 | if (!done || fragment) |
| 3928 | goto out; |
| 3929 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3930 | csum = skb_checksum_setup_ip(skb, nexthdr, off); |
| 3931 | if (IS_ERR(csum)) |
| 3932 | return PTR_ERR(csum); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3933 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3934 | if (recalculate) |
| 3935 | *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 3936 | &ipv6_hdr(skb)->daddr, |
| 3937 | skb->len - off, nexthdr, 0); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3938 | err = 0; |
| 3939 | |
| 3940 | out: |
| 3941 | return err; |
| 3942 | } |
| 3943 | |
| 3944 | /** |
| 3945 | * skb_checksum_setup - set up partial checksum offset |
| 3946 | * @skb: the skb to set up |
| 3947 | * @recalculate: if true the pseudo-header checksum will be recalculated |
| 3948 | */ |
| 3949 | int skb_checksum_setup(struct sk_buff *skb, bool recalculate) |
| 3950 | { |
| 3951 | int err; |
| 3952 | |
| 3953 | switch (skb->protocol) { |
| 3954 | case htons(ETH_P_IP): |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3955 | err = skb_checksum_setup_ipv4(skb, recalculate); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3956 | break; |
| 3957 | |
| 3958 | case htons(ETH_P_IPV6): |
| 3959 | err = skb_checksum_setup_ipv6(skb, recalculate); |
| 3960 | break; |
| 3961 | |
| 3962 | default: |
| 3963 | err = -EPROTO; |
| 3964 | break; |
| 3965 | } |
| 3966 | |
| 3967 | return err; |
| 3968 | } |
| 3969 | EXPORT_SYMBOL(skb_checksum_setup); |
| 3970 | |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 3971 | void __skb_warn_lro_forwarding(const struct sk_buff *skb) |
| 3972 | { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 3973 | net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", |
| 3974 | skb->dev->name); |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 3975 | } |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 3976 | EXPORT_SYMBOL(__skb_warn_lro_forwarding); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3977 | |
| 3978 | void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) |
| 3979 | { |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 3980 | if (head_stolen) { |
| 3981 | skb_release_head_state(skb); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3982 | kmem_cache_free(skbuff_head_cache, skb); |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 3983 | } else { |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3984 | __kfree_skb(skb); |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 3985 | } |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3986 | } |
| 3987 | EXPORT_SYMBOL(kfree_skb_partial); |
| 3988 | |
| 3989 | /** |
| 3990 | * skb_try_coalesce - try to merge skb to prior one |
| 3991 | * @to: prior buffer |
| 3992 | * @from: buffer to add |
| 3993 | * @fragstolen: pointer to boolean |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 3994 | * @delta_truesize: how much more was allocated than was requested |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 3995 | */ |
| 3996 | bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, |
| 3997 | bool *fragstolen, int *delta_truesize) |
| 3998 | { |
| 3999 | int i, delta, len = from->len; |
| 4000 | |
| 4001 | *fragstolen = false; |
| 4002 | |
| 4003 | if (skb_cloned(to)) |
| 4004 | return false; |
| 4005 | |
| 4006 | if (len <= skb_tailroom(to)) { |
Eric Dumazet | e93a043 | 2014-09-15 04:19:52 -0700 | [diff] [blame] | 4007 | if (len) |
| 4008 | BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4009 | *delta_truesize = 0; |
| 4010 | return true; |
| 4011 | } |
| 4012 | |
| 4013 | if (skb_has_frag_list(to) || skb_has_frag_list(from)) |
| 4014 | return false; |
| 4015 | |
| 4016 | if (skb_headlen(from) != 0) { |
| 4017 | struct page *page; |
| 4018 | unsigned int offset; |
| 4019 | |
| 4020 | if (skb_shinfo(to)->nr_frags + |
| 4021 | skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) |
| 4022 | return false; |
| 4023 | |
| 4024 | if (skb_head_is_locked(from)) |
| 4025 | return false; |
| 4026 | |
| 4027 | delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
| 4028 | |
| 4029 | page = virt_to_head_page(from->head); |
| 4030 | offset = from->data - (unsigned char *)page_address(page); |
| 4031 | |
| 4032 | skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, |
| 4033 | page, offset, skb_headlen(from)); |
| 4034 | *fragstolen = true; |
| 4035 | } else { |
| 4036 | if (skb_shinfo(to)->nr_frags + |
| 4037 | skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) |
| 4038 | return false; |
| 4039 | |
Weiping Pan | f4b549a | 2012-09-28 20:15:30 +0000 | [diff] [blame] | 4040 | delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | WARN_ON_ONCE(delta < len); |
| 4044 | |
| 4045 | memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, |
| 4046 | skb_shinfo(from)->frags, |
| 4047 | skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); |
| 4048 | skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; |
| 4049 | |
| 4050 | if (!skb_cloned(from)) |
| 4051 | skb_shinfo(from)->nr_frags = 0; |
| 4052 | |
Li RongQing | 8ea853f | 2012-09-18 16:53:21 +0000 | [diff] [blame] | 4053 | /* if the skb is not cloned this does nothing |
| 4054 | * since we set nr_frags to 0. |
| 4055 | */ |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4056 | for (i = 0; i < skb_shinfo(from)->nr_frags; i++) |
| 4057 | skb_frag_ref(from, i); |
| 4058 | |
| 4059 | to->truesize += delta; |
| 4060 | to->len += len; |
| 4061 | to->data_len += len; |
| 4062 | |
| 4063 | *delta_truesize = delta; |
| 4064 | return true; |
| 4065 | } |
| 4066 | EXPORT_SYMBOL(skb_try_coalesce); |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4067 | |
| 4068 | /** |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 4069 | * skb_scrub_packet - scrub an skb |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4070 | * |
| 4071 | * @skb: buffer to clean |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 4072 | * @xnet: packet is crossing netns |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4073 | * |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 4074 | * skb_scrub_packet can be used after encapsulating or decapsulting a packet |
| 4075 | * into/from a tunnel. Some information have to be cleared during these |
| 4076 | * operations. |
| 4077 | * skb_scrub_packet can also be used to clean a skb before injecting it in |
| 4078 | * another namespace (@xnet == true). We have to clear all information in the |
| 4079 | * skb that could impact namespace isolation. |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4080 | */ |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 4081 | void skb_scrub_packet(struct sk_buff *skb, bool xnet) |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4082 | { |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 4083 | if (xnet) |
| 4084 | skb_orphan(skb); |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4085 | skb->tstamp.tv64 = 0; |
| 4086 | skb->pkt_type = PACKET_HOST; |
| 4087 | skb->skb_iif = 0; |
WANG Cong | 60ff746 | 2014-05-04 16:39:18 -0700 | [diff] [blame] | 4088 | skb->ignore_df = 0; |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4089 | skb_dst_drop(skb); |
| 4090 | skb->mark = 0; |
| 4091 | secpath_reset(skb); |
| 4092 | nf_reset(skb); |
| 4093 | nf_reset_trace(skb); |
| 4094 | } |
| 4095 | EXPORT_SYMBOL_GPL(skb_scrub_packet); |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 4096 | |
| 4097 | /** |
| 4098 | * skb_gso_transport_seglen - Return length of individual segments of a gso packet |
| 4099 | * |
| 4100 | * @skb: GSO skb |
| 4101 | * |
| 4102 | * skb_gso_transport_seglen is used to determine the real size of the |
| 4103 | * individual segments, including Layer4 headers (TCP/UDP). |
| 4104 | * |
| 4105 | * The MAC/L2 or network (IP, IPv6) headers are not accounted for. |
| 4106 | */ |
| 4107 | unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) |
| 4108 | { |
| 4109 | const struct skb_shared_info *shinfo = skb_shinfo(skb); |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 4110 | unsigned int thlen = 0; |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 4111 | |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 4112 | if (skb->encapsulation) { |
| 4113 | thlen = skb_inner_transport_header(skb) - |
| 4114 | skb_transport_header(skb); |
Florian Westphal | 6d39d58 | 2014-04-09 10:28:50 +0200 | [diff] [blame] | 4115 | |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 4116 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) |
| 4117 | thlen += inner_tcp_hdrlen(skb); |
| 4118 | } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
| 4119 | thlen = tcp_hdrlen(skb); |
| 4120 | } |
Florian Westphal | 6d39d58 | 2014-04-09 10:28:50 +0200 | [diff] [blame] | 4121 | /* UFO sets gso_size to the size of the fragmentation |
| 4122 | * payload, i.e. the size of the L4 (UDP) header is already |
| 4123 | * accounted for. |
| 4124 | */ |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 4125 | return thlen + shinfo->gso_size; |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 4126 | } |
| 4127 | EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 4128 | |
| 4129 | static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) |
| 4130 | { |
| 4131 | if (skb_cow(skb, skb_headroom(skb)) < 0) { |
| 4132 | kfree_skb(skb); |
| 4133 | return NULL; |
| 4134 | } |
| 4135 | |
| 4136 | memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN); |
| 4137 | skb->mac_header += VLAN_HLEN; |
| 4138 | return skb; |
| 4139 | } |
| 4140 | |
| 4141 | struct sk_buff *skb_vlan_untag(struct sk_buff *skb) |
| 4142 | { |
| 4143 | struct vlan_hdr *vhdr; |
| 4144 | u16 vlan_tci; |
| 4145 | |
| 4146 | if (unlikely(vlan_tx_tag_present(skb))) { |
| 4147 | /* vlan_tci is already set-up so leave this for another time */ |
| 4148 | return skb; |
| 4149 | } |
| 4150 | |
| 4151 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 4152 | if (unlikely(!skb)) |
| 4153 | goto err_free; |
| 4154 | |
| 4155 | if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) |
| 4156 | goto err_free; |
| 4157 | |
| 4158 | vhdr = (struct vlan_hdr *)skb->data; |
| 4159 | vlan_tci = ntohs(vhdr->h_vlan_TCI); |
| 4160 | __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); |
| 4161 | |
| 4162 | skb_pull_rcsum(skb, VLAN_HLEN); |
| 4163 | vlan_set_encap_proto(skb, vhdr); |
| 4164 | |
| 4165 | skb = skb_reorder_vlan_header(skb); |
| 4166 | if (unlikely(!skb)) |
| 4167 | goto err_free; |
| 4168 | |
| 4169 | skb_reset_network_header(skb); |
| 4170 | skb_reset_transport_header(skb); |
| 4171 | skb_reset_mac_len(skb); |
| 4172 | |
| 4173 | return skb; |
| 4174 | |
| 4175 | err_free: |
| 4176 | kfree_skb(skb); |
| 4177 | return NULL; |
| 4178 | } |
| 4179 | EXPORT_SYMBOL(skb_vlan_untag); |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 4180 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 4181 | int skb_ensure_writable(struct sk_buff *skb, int write_len) |
| 4182 | { |
| 4183 | if (!pskb_may_pull(skb, write_len)) |
| 4184 | return -ENOMEM; |
| 4185 | |
| 4186 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
| 4187 | return 0; |
| 4188 | |
| 4189 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 4190 | } |
| 4191 | EXPORT_SYMBOL(skb_ensure_writable); |
| 4192 | |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 4193 | /* remove VLAN header from packet and update csum accordingly. */ |
| 4194 | static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) |
| 4195 | { |
| 4196 | struct vlan_hdr *vhdr; |
| 4197 | unsigned int offset = skb->data - skb_mac_header(skb); |
| 4198 | int err; |
| 4199 | |
| 4200 | __skb_push(skb, offset); |
| 4201 | err = skb_ensure_writable(skb, VLAN_ETH_HLEN); |
| 4202 | if (unlikely(err)) |
| 4203 | goto pull; |
| 4204 | |
| 4205 | skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); |
| 4206 | |
| 4207 | vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); |
| 4208 | *vlan_tci = ntohs(vhdr->h_vlan_TCI); |
| 4209 | |
| 4210 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
| 4211 | __skb_pull(skb, VLAN_HLEN); |
| 4212 | |
| 4213 | vlan_set_encap_proto(skb, vhdr); |
| 4214 | skb->mac_header += VLAN_HLEN; |
| 4215 | |
| 4216 | if (skb_network_offset(skb) < ETH_HLEN) |
| 4217 | skb_set_network_header(skb, ETH_HLEN); |
| 4218 | |
| 4219 | skb_reset_mac_len(skb); |
| 4220 | pull: |
| 4221 | __skb_pull(skb, offset); |
| 4222 | |
| 4223 | return err; |
| 4224 | } |
| 4225 | |
| 4226 | int skb_vlan_pop(struct sk_buff *skb) |
| 4227 | { |
| 4228 | u16 vlan_tci; |
| 4229 | __be16 vlan_proto; |
| 4230 | int err; |
| 4231 | |
| 4232 | if (likely(vlan_tx_tag_present(skb))) { |
| 4233 | skb->vlan_tci = 0; |
| 4234 | } else { |
| 4235 | if (unlikely((skb->protocol != htons(ETH_P_8021Q) && |
| 4236 | skb->protocol != htons(ETH_P_8021AD)) || |
| 4237 | skb->len < VLAN_ETH_HLEN)) |
| 4238 | return 0; |
| 4239 | |
| 4240 | err = __skb_vlan_pop(skb, &vlan_tci); |
| 4241 | if (err) |
| 4242 | return err; |
| 4243 | } |
| 4244 | /* move next vlan tag to hw accel tag */ |
| 4245 | if (likely((skb->protocol != htons(ETH_P_8021Q) && |
| 4246 | skb->protocol != htons(ETH_P_8021AD)) || |
| 4247 | skb->len < VLAN_ETH_HLEN)) |
| 4248 | return 0; |
| 4249 | |
| 4250 | vlan_proto = skb->protocol; |
| 4251 | err = __skb_vlan_pop(skb, &vlan_tci); |
| 4252 | if (unlikely(err)) |
| 4253 | return err; |
| 4254 | |
| 4255 | __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); |
| 4256 | return 0; |
| 4257 | } |
| 4258 | EXPORT_SYMBOL(skb_vlan_pop); |
| 4259 | |
| 4260 | int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) |
| 4261 | { |
| 4262 | if (vlan_tx_tag_present(skb)) { |
| 4263 | unsigned int offset = skb->data - skb_mac_header(skb); |
| 4264 | int err; |
| 4265 | |
| 4266 | /* __vlan_insert_tag expect skb->data pointing to mac header. |
| 4267 | * So change skb->data before calling it and change back to |
| 4268 | * original position later |
| 4269 | */ |
| 4270 | __skb_push(skb, offset); |
| 4271 | err = __vlan_insert_tag(skb, skb->vlan_proto, |
| 4272 | vlan_tx_tag_get(skb)); |
| 4273 | if (err) |
| 4274 | return err; |
| 4275 | skb->protocol = skb->vlan_proto; |
| 4276 | skb->mac_len += VLAN_HLEN; |
| 4277 | __skb_pull(skb, offset); |
| 4278 | |
| 4279 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 4280 | skb->csum = csum_add(skb->csum, csum_partial(skb->data |
| 4281 | + (2 * ETH_ALEN), VLAN_HLEN, 0)); |
| 4282 | } |
| 4283 | __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); |
| 4284 | return 0; |
| 4285 | } |
| 4286 | EXPORT_SYMBOL(skb_vlan_push); |
| 4287 | |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 4288 | /** |
| 4289 | * alloc_skb_with_frags - allocate skb with page frags |
| 4290 | * |
Masanari Iida | de3f0d0 | 2014-10-09 12:58:08 +0900 | [diff] [blame] | 4291 | * @header_len: size of linear part |
| 4292 | * @data_len: needed length in frags |
| 4293 | * @max_page_order: max page order desired. |
| 4294 | * @errcode: pointer to error code if any |
| 4295 | * @gfp_mask: allocation mask |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 4296 | * |
| 4297 | * This can be used to allocate a paged skb, given a maximal order for frags. |
| 4298 | */ |
| 4299 | struct sk_buff *alloc_skb_with_frags(unsigned long header_len, |
| 4300 | unsigned long data_len, |
| 4301 | int max_page_order, |
| 4302 | int *errcode, |
| 4303 | gfp_t gfp_mask) |
| 4304 | { |
| 4305 | int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; |
| 4306 | unsigned long chunk; |
| 4307 | struct sk_buff *skb; |
| 4308 | struct page *page; |
| 4309 | gfp_t gfp_head; |
| 4310 | int i; |
| 4311 | |
| 4312 | *errcode = -EMSGSIZE; |
| 4313 | /* Note this test could be relaxed, if we succeed to allocate |
| 4314 | * high order pages... |
| 4315 | */ |
| 4316 | if (npages > MAX_SKB_FRAGS) |
| 4317 | return NULL; |
| 4318 | |
| 4319 | gfp_head = gfp_mask; |
| 4320 | if (gfp_head & __GFP_WAIT) |
| 4321 | gfp_head |= __GFP_REPEAT; |
| 4322 | |
| 4323 | *errcode = -ENOBUFS; |
| 4324 | skb = alloc_skb(header_len, gfp_head); |
| 4325 | if (!skb) |
| 4326 | return NULL; |
| 4327 | |
| 4328 | skb->truesize += npages << PAGE_SHIFT; |
| 4329 | |
| 4330 | for (i = 0; npages > 0; i++) { |
| 4331 | int order = max_page_order; |
| 4332 | |
| 4333 | while (order) { |
| 4334 | if (npages >= 1 << order) { |
| 4335 | page = alloc_pages(gfp_mask | |
| 4336 | __GFP_COMP | |
| 4337 | __GFP_NOWARN | |
| 4338 | __GFP_NORETRY, |
| 4339 | order); |
| 4340 | if (page) |
| 4341 | goto fill_page; |
| 4342 | /* Do not retry other high order allocations */ |
| 4343 | order = 1; |
| 4344 | max_page_order = 0; |
| 4345 | } |
| 4346 | order--; |
| 4347 | } |
| 4348 | page = alloc_page(gfp_mask); |
| 4349 | if (!page) |
| 4350 | goto failure; |
| 4351 | fill_page: |
| 4352 | chunk = min_t(unsigned long, data_len, |
| 4353 | PAGE_SIZE << order); |
| 4354 | skb_fill_page_desc(skb, i, page, 0, chunk); |
| 4355 | data_len -= chunk; |
| 4356 | npages -= 1 << order; |
| 4357 | } |
| 4358 | return skb; |
| 4359 | |
| 4360 | failure: |
| 4361 | kfree_skb(skb); |
| 4362 | return NULL; |
| 4363 | } |
| 4364 | EXPORT_SYMBOL(alloc_skb_with_frags); |