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