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