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