Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Routines having to do with the 'struct sk_buff' memory handlers. |
| 4 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 5 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Florian La Roche <rzsfl@rz.uni-sb.de> |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Fixes: |
| 9 | * Alan Cox : Fixed the worst of the load |
| 10 | * balancer bugs. |
| 11 | * Dave Platt : Interrupt stacking fix. |
| 12 | * Richard Kooijman : Timestamp fixes. |
| 13 | * Alan Cox : Changed buffer format. |
| 14 | * Alan Cox : destructor hook for AF_UNIX etc. |
| 15 | * Linus Torvalds : Better skb_clone. |
| 16 | * Alan Cox : Added skb_copy. |
| 17 | * Alan Cox : Added all the changed routines Linus |
| 18 | * only put in the headers |
| 19 | * Ray VanTassle : Fixed --skb->lock in free |
| 20 | * Alan Cox : skb_copy copy arp field |
| 21 | * Andi Kleen : slabified it. |
| 22 | * Robert Olsson : Removed skb_head_pool |
| 23 | * |
| 24 | * NOTE: |
| 25 | * The __skb_ routines should be called with interrupts |
| 26 | * disabled, or you better be *real* sure that the operation is atomic |
| 27 | * with respect to whatever list is being frobbed (e.g. via lock_sock() |
| 28 | * or via disabling bottom half handlers, etc). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * The functions in this file will not compile correctly with gcc 2.4.x |
| 33 | */ |
| 34 | |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 35 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/module.h> |
| 38 | #include <linux/types.h> |
| 39 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/mm.h> |
| 41 | #include <linux/interrupt.h> |
| 42 | #include <linux/in.h> |
| 43 | #include <linux/inet.h> |
| 44 | #include <linux/slab.h> |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 45 | #include <linux/tcp.h> |
| 46 | #include <linux/udp.h> |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 47 | #include <linux/sctp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/netdevice.h> |
| 49 | #ifdef CONFIG_NET_CLS_ACT |
| 50 | #include <net/pkt_sched.h> |
| 51 | #endif |
| 52 | #include <linux/string.h> |
| 53 | #include <linux/skbuff.h> |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 54 | #include <linux/splice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/cache.h> |
| 56 | #include <linux/rtnetlink.h> |
| 57 | #include <linux/init.h> |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 58 | #include <linux/scatterlist.h> |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 59 | #include <linux/errqueue.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 60 | #include <linux/prefetch.h> |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 61 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | #include <net/protocol.h> |
| 64 | #include <net/dst.h> |
| 65 | #include <net/sock.h> |
| 66 | #include <net/checksum.h> |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 67 | #include <net/ip6_checksum.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <net/xfrm.h> |
| 69 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 70 | #include <linux/uaccess.h> |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 71 | #include <trace/events/skb.h> |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 72 | #include <linux/highmem.h> |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 73 | #include <linux/capability.h> |
| 74 | #include <linux/user_namespace.h> |
Matteo Croce | 2544af0 | 2019-05-29 17:13:48 +0200 | [diff] [blame] | 75 | #include <linux/indirect_call_wrapper.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 76 | |
Bart Van Assche | 7b7ed88 | 2019-03-25 09:17:23 -0700 | [diff] [blame] | 77 | #include "datagram.h" |
| 78 | |
Alexey Dobriyan | 08009a7 | 2018-02-24 21:20:33 +0300 | [diff] [blame] | 79 | struct kmem_cache *skbuff_head_cache __ro_after_init; |
| 80 | static struct kmem_cache *skbuff_fclone_cache __ro_after_init; |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 81 | #ifdef CONFIG_SKB_EXTENSIONS |
| 82 | static struct kmem_cache *skbuff_ext_cache __ro_after_init; |
| 83 | #endif |
Hans Westgaard Ry | 5f74f82e | 2016-02-03 09:26:57 +0100 | [diff] [blame] | 84 | int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; |
| 85 | EXPORT_SYMBOL(sysctl_max_skb_frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /** |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 88 | * skb_panic - private function for out-of-line support |
| 89 | * @skb: buffer |
| 90 | * @sz: size |
| 91 | * @addr: address |
James Hogan | 99d5851 | 2013-02-13 11:20:27 +0000 | [diff] [blame] | 92 | * @msg: skb_over_panic or skb_under_panic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 94 | * Out-of-line support for skb_put() and skb_push(). |
| 95 | * Called via the wrapper skb_over_panic() or skb_under_panic(). |
| 96 | * Keep out of line to prevent kernel bloat. |
| 97 | * __builtin_return_address is not used because it is not always reliable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | */ |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 99 | 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] | 100 | const char msg[]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 102 | 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] | 103 | msg, addr, skb->len, sz, skb->head, skb->data, |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 104 | (unsigned long)skb->tail, (unsigned long)skb->end, |
| 105 | skb->dev ? skb->dev->name : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | BUG(); |
| 107 | } |
| 108 | |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 109 | 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] | 110 | { |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 111 | skb_panic(skb, sz, addr, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 114 | static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
| 115 | { |
| 116 | skb_panic(skb, sz, addr, __func__); |
| 117 | } |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells |
| 121 | * the caller if emergency pfmemalloc reserves are being used. If it is and |
| 122 | * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves |
| 123 | * may be used. Otherwise, the packet data may be discarded until enough |
| 124 | * memory is free |
| 125 | */ |
| 126 | #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ |
| 127 | __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) |
stephen hemminger | 61c5e88 | 2012-12-28 18:24:28 +0000 | [diff] [blame] | 128 | |
| 129 | static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, |
| 130 | unsigned long ip, bool *pfmemalloc) |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 131 | { |
| 132 | void *obj; |
| 133 | bool ret_pfmemalloc = false; |
| 134 | |
| 135 | /* |
| 136 | * Try a regular allocation, when that fails and we're not entitled |
| 137 | * to the reserves, fail. |
| 138 | */ |
| 139 | obj = kmalloc_node_track_caller(size, |
| 140 | flags | __GFP_NOMEMALLOC | __GFP_NOWARN, |
| 141 | node); |
| 142 | if (obj || !(gfp_pfmemalloc_allowed(flags))) |
| 143 | goto out; |
| 144 | |
| 145 | /* Try again but now we are using pfmemalloc reserves */ |
| 146 | ret_pfmemalloc = true; |
| 147 | obj = kmalloc_node_track_caller(size, flags, node); |
| 148 | |
| 149 | out: |
| 150 | if (pfmemalloc) |
| 151 | *pfmemalloc = ret_pfmemalloc; |
| 152 | |
| 153 | return obj; |
| 154 | } |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
| 157 | * 'private' fields and also do memory statistics to find all the |
| 158 | * [BEEP] leaks. |
| 159 | * |
| 160 | */ |
| 161 | |
| 162 | /** |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 163 | * __alloc_skb - allocate a network buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | * @size: size to allocate |
| 165 | * @gfp_mask: allocation mask |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 166 | * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache |
| 167 | * instead of head cache and allocate a cloned (child) skb. |
| 168 | * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for |
| 169 | * allocations in case the data is required for writeback |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 170 | * @node: numa node to allocate memory on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | * |
| 172 | * 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] | 173 | * tail room of at least size bytes. The object has a reference count |
| 174 | * 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] | 175 | * |
| 176 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 177 | * %GFP_ATOMIC. |
| 178 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 179 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 180 | int flags, int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 182 | struct kmem_cache *cache; |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 183 | struct skb_shared_info *shinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | struct sk_buff *skb; |
| 185 | u8 *data; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 186 | bool pfmemalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 188 | cache = (flags & SKB_ALLOC_FCLONE) |
| 189 | ? skbuff_fclone_cache : skbuff_head_cache; |
| 190 | |
| 191 | if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) |
| 192 | gfp_mask |= __GFP_MEMALLOC; |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | /* Get the HEAD */ |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 195 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (!skb) |
| 197 | goto out; |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 198 | prefetchw(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 200 | /* We do our best to align skb_shared_info on a separate cache |
| 201 | * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives |
| 202 | * aligned memory blocks, unless SLUB/SLAB debug is enabled. |
| 203 | * Both skb->head and skb_shared_info are cache line aligned. |
| 204 | */ |
Tony Lindgren | bc417e3 | 2011-11-02 13:40:28 +0000 | [diff] [blame] | 205 | size = SKB_DATA_ALIGN(size); |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 206 | size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 207 | data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | if (!data) |
| 209 | goto nodata; |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 210 | /* kmalloc(size) might give us more room than requested. |
| 211 | * Put skb_shared_info exactly at the end of allocated zone, |
| 212 | * to allow max possible filling before reallocation. |
| 213 | */ |
| 214 | size = SKB_WITH_OVERHEAD(ksize(data)); |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 215 | prefetchw(data + size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Arnaldo Carvalho de Melo | ca0605a | 2007-03-19 10:48:59 -0300 | [diff] [blame] | 217 | /* |
Johannes Berg | c800578 | 2008-05-03 20:56:42 -0700 | [diff] [blame] | 218 | * Only clear those fields we need to clear, not those that we will |
| 219 | * actually initialise below. Hence, don't put any more fields after |
| 220 | * the tail pointer in struct sk_buff! |
Arnaldo Carvalho de Melo | ca0605a | 2007-03-19 10:48:59 -0300 | [diff] [blame] | 221 | */ |
| 222 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 223 | /* Account for allocated memory : skb + skb->head */ |
| 224 | skb->truesize = SKB_TRUESIZE(size); |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 225 | skb->pfmemalloc = pfmemalloc; |
Reshetova, Elena | 6335479 | 2017-06-30 13:07:58 +0300 | [diff] [blame] | 226 | refcount_set(&skb->users, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | skb->head = data; |
| 228 | skb->data = data; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 229 | skb_reset_tail_pointer(skb); |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 230 | skb->end = skb->tail + size; |
Cong Wang | 35d0461 | 2013-05-29 15:16:05 +0800 | [diff] [blame] | 231 | skb->mac_header = (typeof(skb->mac_header))~0U; |
| 232 | skb->transport_header = (typeof(skb->transport_header))~0U; |
Stephen Hemminger | 19633e1 | 2009-06-17 05:23:27 +0000 | [diff] [blame] | 233 | |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 234 | /* make sure we initialize shinfo sequentially */ |
| 235 | shinfo = skb_shinfo(skb); |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 236 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 237 | atomic_set(&shinfo->dataref, 1); |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 238 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 239 | if (flags & SKB_ALLOC_FCLONE) { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 240 | struct sk_buff_fclones *fclones; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 242 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
| 243 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 244 | skb->fclone = SKB_FCLONE_ORIG; |
Reshetova, Elena | 2638595 | 2017-06-30 13:07:59 +0300 | [diff] [blame] | 245 | refcount_set(&fclones->fclone_ref, 1); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 246 | |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 247 | fclones->skb2.fclone = SKB_FCLONE_CLONE; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | out: |
| 250 | return skb; |
| 251 | nodata: |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 252 | kmem_cache_free(cache, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | skb = NULL; |
| 254 | goto out; |
| 255 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 256 | EXPORT_SYMBOL(__alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 258 | /* Caller must provide SKB that is memset cleared */ |
| 259 | static struct sk_buff *__build_skb_around(struct sk_buff *skb, |
| 260 | void *data, unsigned int frag_size) |
| 261 | { |
| 262 | struct skb_shared_info *shinfo; |
| 263 | unsigned int size = frag_size ? : ksize(data); |
| 264 | |
| 265 | size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 266 | |
| 267 | /* Assumes caller memset cleared SKB */ |
| 268 | skb->truesize = SKB_TRUESIZE(size); |
| 269 | refcount_set(&skb->users, 1); |
| 270 | skb->head = data; |
| 271 | skb->data = data; |
| 272 | skb_reset_tail_pointer(skb); |
| 273 | skb->end = skb->tail + size; |
| 274 | skb->mac_header = (typeof(skb->mac_header))~0U; |
| 275 | skb->transport_header = (typeof(skb->transport_header))~0U; |
| 276 | |
| 277 | /* make sure we initialize shinfo sequentially */ |
| 278 | shinfo = skb_shinfo(skb); |
| 279 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
| 280 | atomic_set(&shinfo->dataref, 1); |
| 281 | |
| 282 | return skb; |
| 283 | } |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | /** |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 286 | * __build_skb - build a network buffer |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 287 | * @data: data buffer provided by caller |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 288 | * @frag_size: size of data, or 0 if head was kmalloced |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 289 | * |
| 290 | * Allocate a new &sk_buff. Caller provides space holding head and |
Florian Fainelli | deceb4c | 2013-07-23 20:22:39 +0100 | [diff] [blame] | 291 | * skb_shared_info. @data must have been allocated by kmalloc() only if |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 292 | * @frag_size is 0, otherwise data should come from the page allocator |
| 293 | * or vmalloc() |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 294 | * The return is the new skb buffer. |
| 295 | * On a failure the return is %NULL, and @data is not freed. |
| 296 | * Notes : |
| 297 | * Before IO, driver allocates only data buffer where NIC put incoming frame |
| 298 | * Driver should add room at head (NET_SKB_PAD) and |
| 299 | * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) |
| 300 | * After IO, driver calls build_skb(), to allocate sk_buff and populate it |
| 301 | * before giving packet to stack. |
| 302 | * RX rings only contains data buffers, not full skbs. |
| 303 | */ |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 304 | struct sk_buff *__build_skb(void *data, unsigned int frag_size) |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 305 | { |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 306 | struct sk_buff *skb; |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 307 | |
| 308 | skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 309 | if (unlikely(!skb)) |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 310 | return NULL; |
| 311 | |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 312 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 313 | |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 314 | return __build_skb_around(skb, data, frag_size); |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 315 | } |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 316 | |
| 317 | /* build_skb() is wrapper over __build_skb(), that specifically |
| 318 | * takes care of skb->head and skb->pfmemalloc |
| 319 | * This means that if @frag_size is not zero, then @data must be backed |
| 320 | * by a page fragment, not kmalloc() or vmalloc() |
| 321 | */ |
| 322 | struct sk_buff *build_skb(void *data, unsigned int frag_size) |
| 323 | { |
| 324 | struct sk_buff *skb = __build_skb(data, frag_size); |
| 325 | |
| 326 | if (skb && frag_size) { |
| 327 | skb->head_frag = 1; |
Michal Hocko | 2f064f3 | 2015-08-21 14:11:51 -0700 | [diff] [blame] | 328 | if (page_is_pfmemalloc(virt_to_head_page(data))) |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 329 | skb->pfmemalloc = 1; |
| 330 | } |
| 331 | return skb; |
| 332 | } |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 333 | EXPORT_SYMBOL(build_skb); |
| 334 | |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 335 | /** |
| 336 | * build_skb_around - build a network buffer around provided skb |
| 337 | * @skb: sk_buff provide by caller, must be memset cleared |
| 338 | * @data: data buffer provided by caller |
| 339 | * @frag_size: size of data, or 0 if head was kmalloced |
| 340 | */ |
| 341 | struct sk_buff *build_skb_around(struct sk_buff *skb, |
| 342 | void *data, unsigned int frag_size) |
| 343 | { |
| 344 | if (unlikely(!skb)) |
| 345 | return NULL; |
| 346 | |
| 347 | skb = __build_skb_around(skb, data, frag_size); |
| 348 | |
| 349 | if (skb && frag_size) { |
| 350 | skb->head_frag = 1; |
| 351 | if (page_is_pfmemalloc(virt_to_head_page(data))) |
| 352 | skb->pfmemalloc = 1; |
| 353 | } |
| 354 | return skb; |
| 355 | } |
| 356 | EXPORT_SYMBOL(build_skb_around); |
| 357 | |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 358 | #define NAPI_SKB_CACHE_SIZE 64 |
| 359 | |
| 360 | struct napi_alloc_cache { |
| 361 | struct page_frag_cache page; |
Alexey Dobriyan | e0d7924 | 2016-11-19 03:47:56 +0300 | [diff] [blame] | 362 | unsigned int skb_count; |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 363 | void *skb_cache[NAPI_SKB_CACHE_SIZE]; |
| 364 | }; |
| 365 | |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 366 | static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 367 | static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 368 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 369 | static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) |
| 370 | { |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 371 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 372 | |
Alexander Duyck | 8c2dd3e | 2017-01-10 16:58:06 -0800 | [diff] [blame] | 373 | return page_frag_alloc(&nc->page, fragsz, gfp_mask); |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | void *napi_alloc_frag(unsigned int fragsz) |
| 377 | { |
Alexander Duyck | 3bed3cc | 2019-02-15 14:44:18 -0800 | [diff] [blame] | 378 | fragsz = SKB_DATA_ALIGN(fragsz); |
| 379 | |
Mel Gorman | 453f85d | 2017-11-15 17:38:03 -0800 | [diff] [blame] | 380 | return __napi_alloc_frag(fragsz, GFP_ATOMIC); |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 381 | } |
| 382 | EXPORT_SYMBOL(napi_alloc_frag); |
| 383 | |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 384 | /** |
Sebastian Andrzej Siewior | 7ba7aea | 2019-06-07 21:20:34 +0200 | [diff] [blame] | 385 | * netdev_alloc_frag - allocate a page fragment |
| 386 | * @fragsz: fragment size |
| 387 | * |
| 388 | * Allocates a frag from a page for receive buffer. |
| 389 | * Uses GFP_ATOMIC allocations. |
| 390 | */ |
| 391 | void *netdev_alloc_frag(unsigned int fragsz) |
| 392 | { |
| 393 | struct page_frag_cache *nc; |
| 394 | void *data; |
| 395 | |
| 396 | fragsz = SKB_DATA_ALIGN(fragsz); |
| 397 | if (in_irq() || irqs_disabled()) { |
| 398 | nc = this_cpu_ptr(&netdev_alloc_cache); |
| 399 | data = page_frag_alloc(nc, fragsz, GFP_ATOMIC); |
| 400 | } else { |
| 401 | local_bh_disable(); |
| 402 | data = __napi_alloc_frag(fragsz, GFP_ATOMIC); |
| 403 | local_bh_enable(); |
| 404 | } |
| 405 | return data; |
| 406 | } |
| 407 | EXPORT_SYMBOL(netdev_alloc_frag); |
| 408 | |
| 409 | /** |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 410 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device |
| 411 | * @dev: network device to receive on |
Masanari Iida | d749916 | 2015-08-24 22:56:54 +0900 | [diff] [blame] | 412 | * @len: length to allocate |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 413 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
| 414 | * |
| 415 | * Allocate a new &sk_buff and assign it a usage count of one. The |
| 416 | * buffer has NET_SKB_PAD headroom built in. Users should allocate |
| 417 | * the headroom they think they need without accounting for the |
| 418 | * built in space. The built in space is used for optimisations. |
| 419 | * |
| 420 | * %NULL is returned if there is no free memory. |
| 421 | */ |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 422 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, |
| 423 | gfp_t gfp_mask) |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 424 | { |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 425 | struct page_frag_cache *nc; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 426 | struct sk_buff *skb; |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 427 | bool pfmemalloc; |
| 428 | void *data; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 429 | |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 430 | len += NET_SKB_PAD; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 431 | |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 432 | if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 433 | (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 434 | skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); |
| 435 | if (!skb) |
| 436 | goto skb_fail; |
| 437 | goto skb_success; |
| 438 | } |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 439 | |
| 440 | len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 441 | len = SKB_DATA_ALIGN(len); |
| 442 | |
| 443 | if (sk_memalloc_socks()) |
| 444 | gfp_mask |= __GFP_MEMALLOC; |
| 445 | |
Sebastian Andrzej Siewior | 92dcabd | 2019-06-07 21:20:35 +0200 | [diff] [blame] | 446 | if (in_irq() || irqs_disabled()) { |
| 447 | nc = this_cpu_ptr(&netdev_alloc_cache); |
| 448 | data = page_frag_alloc(nc, len, gfp_mask); |
| 449 | pfmemalloc = nc->pfmemalloc; |
| 450 | } else { |
| 451 | local_bh_disable(); |
| 452 | nc = this_cpu_ptr(&napi_alloc_cache.page); |
| 453 | data = page_frag_alloc(nc, len, gfp_mask); |
| 454 | pfmemalloc = nc->pfmemalloc; |
| 455 | local_bh_enable(); |
| 456 | } |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 457 | |
| 458 | if (unlikely(!data)) |
| 459 | return NULL; |
| 460 | |
| 461 | skb = __build_skb(data, len); |
| 462 | if (unlikely(!skb)) { |
Alexander Duyck | 181edb2 | 2015-05-06 21:12:03 -0700 | [diff] [blame] | 463 | skb_free_frag(data); |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 464 | return NULL; |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 465 | } |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 466 | |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 467 | /* use OR instead of assignment to avoid clearing of bits in mask */ |
| 468 | if (pfmemalloc) |
| 469 | skb->pfmemalloc = 1; |
| 470 | skb->head_frag = 1; |
| 471 | |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 472 | skb_success: |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 473 | skb_reserve(skb, NET_SKB_PAD); |
| 474 | skb->dev = dev; |
| 475 | |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 476 | skb_fail: |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 477 | return skb; |
| 478 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 479 | EXPORT_SYMBOL(__netdev_alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 481 | /** |
| 482 | * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance |
| 483 | * @napi: napi instance this buffer was allocated for |
Masanari Iida | d749916 | 2015-08-24 22:56:54 +0900 | [diff] [blame] | 484 | * @len: length to allocate |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 485 | * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages |
| 486 | * |
| 487 | * Allocate a new sk_buff for use in NAPI receive. This buffer will |
| 488 | * attempt to allocate the head from a special reserved region used |
| 489 | * only for NAPI Rx allocation. By doing this we can save several |
| 490 | * CPU cycles by avoiding having to disable and re-enable IRQs. |
| 491 | * |
| 492 | * %NULL is returned if there is no free memory. |
| 493 | */ |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 494 | struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, |
| 495 | gfp_t gfp_mask) |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 496 | { |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 497 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 498 | struct sk_buff *skb; |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 499 | void *data; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 500 | |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 501 | len += NET_SKB_PAD + NET_IP_ALIGN; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 502 | |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 503 | if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 504 | (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 505 | skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); |
| 506 | if (!skb) |
| 507 | goto skb_fail; |
| 508 | goto skb_success; |
| 509 | } |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 510 | |
| 511 | len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 512 | len = SKB_DATA_ALIGN(len); |
| 513 | |
| 514 | if (sk_memalloc_socks()) |
| 515 | gfp_mask |= __GFP_MEMALLOC; |
| 516 | |
Alexander Duyck | 8c2dd3e | 2017-01-10 16:58:06 -0800 | [diff] [blame] | 517 | data = page_frag_alloc(&nc->page, len, gfp_mask); |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 518 | if (unlikely(!data)) |
| 519 | return NULL; |
| 520 | |
| 521 | skb = __build_skb(data, len); |
| 522 | if (unlikely(!skb)) { |
Alexander Duyck | 181edb2 | 2015-05-06 21:12:03 -0700 | [diff] [blame] | 523 | skb_free_frag(data); |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 524 | return NULL; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 527 | /* use OR instead of assignment to avoid clearing of bits in mask */ |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 528 | if (nc->page.pfmemalloc) |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 529 | skb->pfmemalloc = 1; |
| 530 | skb->head_frag = 1; |
| 531 | |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 532 | skb_success: |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 533 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
| 534 | skb->dev = napi->dev; |
| 535 | |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 536 | skb_fail: |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 537 | return skb; |
| 538 | } |
| 539 | EXPORT_SYMBOL(__napi_alloc_skb); |
| 540 | |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 541 | 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] | 542 | int size, unsigned int truesize) |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 543 | { |
| 544 | skb_fill_page_desc(skb, i, page, off, size); |
| 545 | skb->len += size; |
| 546 | skb->data_len += size; |
Eric Dumazet | 50269e1 | 2012-03-23 23:59:33 +0000 | [diff] [blame] | 547 | skb->truesize += truesize; |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 548 | } |
| 549 | EXPORT_SYMBOL(skb_add_rx_frag); |
| 550 | |
Jason Wang | f8e617e | 2013-11-01 14:07:47 +0800 | [diff] [blame] | 551 | void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, |
| 552 | unsigned int truesize) |
| 553 | { |
| 554 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 555 | |
| 556 | skb_frag_size_add(frag, size); |
| 557 | skb->len += size; |
| 558 | skb->data_len += size; |
| 559 | skb->truesize += truesize; |
| 560 | } |
| 561 | EXPORT_SYMBOL(skb_coalesce_rx_frag); |
| 562 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 563 | static void skb_drop_list(struct sk_buff **listp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Eric Dumazet | bd8a703 | 2013-06-24 06:26:00 -0700 | [diff] [blame] | 565 | kfree_skb_list(*listp); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 566 | *listp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 569 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
| 570 | { |
| 571 | skb_drop_list(&skb_shinfo(skb)->frag_list); |
| 572 | } |
| 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | static void skb_clone_fraglist(struct sk_buff *skb) |
| 575 | { |
| 576 | struct sk_buff *list; |
| 577 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 578 | skb_walk_frags(skb, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | skb_get(list); |
| 580 | } |
| 581 | |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 582 | static void skb_free_head(struct sk_buff *skb) |
| 583 | { |
Alexander Duyck | 181edb2 | 2015-05-06 21:12:03 -0700 | [diff] [blame] | 584 | unsigned char *head = skb->head; |
| 585 | |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 586 | if (skb->head_frag) |
Alexander Duyck | 181edb2 | 2015-05-06 21:12:03 -0700 | [diff] [blame] | 587 | skb_free_frag(head); |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 588 | else |
Alexander Duyck | 181edb2 | 2015-05-06 21:12:03 -0700 | [diff] [blame] | 589 | kfree(head); |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Adrian Bunk | 5bba171 | 2006-06-29 13:02:35 -0700 | [diff] [blame] | 592 | static void skb_release_data(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | { |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 594 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 595 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 597 | if (skb->cloned && |
| 598 | atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, |
| 599 | &shinfo->dataref)) |
| 600 | return; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 601 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 602 | for (i = 0; i < shinfo->nr_frags; i++) |
| 603 | __skb_frag_unref(&shinfo->frags[i]); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 604 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 605 | if (shinfo->frag_list) |
| 606 | kfree_skb_list(shinfo->frag_list); |
| 607 | |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 608 | skb_zcopy_clear(skb, true); |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 609 | skb_free_head(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Free an skbuff by memory without cleaning the state. |
| 614 | */ |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 615 | static void kfree_skbmem(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 617 | struct sk_buff_fclones *fclones; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 618 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 619 | switch (skb->fclone) { |
| 620 | case SKB_FCLONE_UNAVAILABLE: |
| 621 | kmem_cache_free(skbuff_head_cache, skb); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 622 | return; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 623 | |
| 624 | case SKB_FCLONE_ORIG: |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 625 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 626 | |
| 627 | /* We usually free the clone (TX completion) before original skb |
| 628 | * This test would have no chance to be true for the clone, |
| 629 | * while here, branch prediction will be good. |
| 630 | */ |
Reshetova, Elena | 2638595 | 2017-06-30 13:07:59 +0300 | [diff] [blame] | 631 | if (refcount_read(&fclones->fclone_ref) == 1) |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 632 | goto fastpath; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 633 | break; |
| 634 | |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 635 | default: /* SKB_FCLONE_CLONE */ |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 636 | fclones = container_of(skb, struct sk_buff_fclones, skb2); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 637 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 638 | } |
Reshetova, Elena | 2638595 | 2017-06-30 13:07:59 +0300 | [diff] [blame] | 639 | if (!refcount_dec_and_test(&fclones->fclone_ref)) |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 640 | return; |
| 641 | fastpath: |
| 642 | kmem_cache_free(skbuff_fclone_cache, fclones); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 645 | void skb_release_head_state(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 647 | skb_dst_drop(skb); |
Stephen Hemminger | 9c2b332 | 2005-04-19 22:39:42 -0700 | [diff] [blame] | 648 | if (skb->destructor) { |
| 649 | WARN_ON(in_irq()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | skb->destructor(skb); |
| 651 | } |
Igor Maravić | a3bf7ae | 2011-12-12 02:58:22 +0000 | [diff] [blame] | 652 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
Florian Westphal | cb9c683 | 2017-01-23 18:21:56 +0100 | [diff] [blame] | 653 | nf_conntrack_put(skb_nfct(skb)); |
KOVACS Krisztian | 2fc72c7 | 2011-01-12 20:25:08 +0100 | [diff] [blame] | 654 | #endif |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 655 | skb_ext_put(skb); |
Lennert Buytenhek | 04a4bb5 | 2008-10-01 02:33:12 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /* Free everything but the sk_buff shell. */ |
| 659 | static void skb_release_all(struct sk_buff *skb) |
| 660 | { |
| 661 | skb_release_head_state(skb); |
Florian Westphal | a28b1b9 | 2017-07-23 19:54:47 +0200 | [diff] [blame] | 662 | if (likely(skb->head)) |
| 663 | skb_release_data(skb); |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 664 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 666 | /** |
| 667 | * __kfree_skb - private function |
| 668 | * @skb: buffer |
| 669 | * |
| 670 | * Free an sk_buff. Release anything attached to the buffer. |
| 671 | * Clean the state. This is an internal helper function. Users should |
| 672 | * always call kfree_skb |
| 673 | */ |
| 674 | |
| 675 | void __kfree_skb(struct sk_buff *skb) |
| 676 | { |
| 677 | skb_release_all(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | kfree_skbmem(skb); |
| 679 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 680 | EXPORT_SYMBOL(__kfree_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
| 682 | /** |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 683 | * kfree_skb - free an sk_buff |
| 684 | * @skb: buffer to free |
| 685 | * |
| 686 | * Drop a reference to the buffer and free it if the usage count has |
| 687 | * hit zero. |
| 688 | */ |
| 689 | void kfree_skb(struct sk_buff *skb) |
| 690 | { |
Paolo Abeni | 3889a803 | 2017-06-12 11:23:41 +0200 | [diff] [blame] | 691 | if (!skb_unref(skb)) |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 692 | return; |
Paolo Abeni | 3889a803 | 2017-06-12 11:23:41 +0200 | [diff] [blame] | 693 | |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 694 | trace_kfree_skb(skb, __builtin_return_address(0)); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 695 | __kfree_skb(skb); |
| 696 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 697 | EXPORT_SYMBOL(kfree_skb); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 698 | |
Eric Dumazet | bd8a703 | 2013-06-24 06:26:00 -0700 | [diff] [blame] | 699 | void kfree_skb_list(struct sk_buff *segs) |
| 700 | { |
| 701 | while (segs) { |
| 702 | struct sk_buff *next = segs->next; |
| 703 | |
| 704 | kfree_skb(segs); |
| 705 | segs = next; |
| 706 | } |
| 707 | } |
| 708 | EXPORT_SYMBOL(kfree_skb_list); |
| 709 | |
Willem de Bruijn | 6413139 | 2019-07-07 05:51:55 -0400 | [diff] [blame^] | 710 | /* Dump skb information and contents. |
| 711 | * |
| 712 | * Must only be called from net_ratelimit()-ed paths. |
| 713 | * |
| 714 | * Dumps up to can_dump_full whole packets if full_pkt, headers otherwise. |
| 715 | */ |
| 716 | void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) |
| 717 | { |
| 718 | static atomic_t can_dump_full = ATOMIC_INIT(5); |
| 719 | struct skb_shared_info *sh = skb_shinfo(skb); |
| 720 | struct net_device *dev = skb->dev; |
| 721 | struct sock *sk = skb->sk; |
| 722 | struct sk_buff *list_skb; |
| 723 | bool has_mac, has_trans; |
| 724 | int headroom, tailroom; |
| 725 | int i, len, seg_len; |
| 726 | |
| 727 | if (full_pkt) |
| 728 | full_pkt = atomic_dec_if_positive(&can_dump_full) >= 0; |
| 729 | |
| 730 | if (full_pkt) |
| 731 | len = skb->len; |
| 732 | else |
| 733 | len = min_t(int, skb->len, MAX_HEADER + 128); |
| 734 | |
| 735 | headroom = skb_headroom(skb); |
| 736 | tailroom = skb_tailroom(skb); |
| 737 | |
| 738 | has_mac = skb_mac_header_was_set(skb); |
| 739 | has_trans = skb_transport_header_was_set(skb); |
| 740 | |
| 741 | printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" |
| 742 | "mac=(%d,%d) net=(%d,%d) trans=%d\n" |
| 743 | "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" |
| 744 | "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" |
| 745 | "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", |
| 746 | level, skb->len, headroom, skb_headlen(skb), tailroom, |
| 747 | has_mac ? skb->mac_header : -1, |
| 748 | has_mac ? skb_mac_header_len(skb) : -1, |
| 749 | skb->network_header, |
| 750 | has_trans ? skb_network_header_len(skb) : -1, |
| 751 | has_trans ? skb->transport_header : -1, |
| 752 | sh->tx_flags, sh->nr_frags, |
| 753 | sh->gso_size, sh->gso_type, sh->gso_segs, |
| 754 | skb->csum, skb->ip_summed, skb->csum_complete_sw, |
| 755 | skb->csum_valid, skb->csum_level, |
| 756 | skb->hash, skb->sw_hash, skb->l4_hash, |
| 757 | ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); |
| 758 | |
| 759 | if (dev) |
| 760 | printk("%sdev name=%s feat=0x%pNF\n", |
| 761 | level, dev->name, &dev->features); |
| 762 | if (sk) |
| 763 | printk("%ssk family=%hu type=%hu proto=%hu\n", |
| 764 | level, sk->sk_family, sk->sk_type, sk->sk_protocol); |
| 765 | |
| 766 | if (full_pkt && headroom) |
| 767 | print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, |
| 768 | 16, 1, skb->head, headroom, false); |
| 769 | |
| 770 | seg_len = min_t(int, skb_headlen(skb), len); |
| 771 | if (seg_len) |
| 772 | print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, |
| 773 | 16, 1, skb->data, seg_len, false); |
| 774 | len -= seg_len; |
| 775 | |
| 776 | if (full_pkt && tailroom) |
| 777 | print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, |
| 778 | 16, 1, skb_tail_pointer(skb), tailroom, false); |
| 779 | |
| 780 | for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { |
| 781 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 782 | u32 p_off, p_len, copied; |
| 783 | struct page *p; |
| 784 | u8 *vaddr; |
| 785 | |
| 786 | skb_frag_foreach_page(frag, frag->page_offset, |
| 787 | skb_frag_size(frag), p, p_off, p_len, |
| 788 | copied) { |
| 789 | seg_len = min_t(int, p_len, len); |
| 790 | vaddr = kmap_atomic(p); |
| 791 | print_hex_dump(level, "skb frag: ", |
| 792 | DUMP_PREFIX_OFFSET, |
| 793 | 16, 1, vaddr + p_off, seg_len, false); |
| 794 | kunmap_atomic(vaddr); |
| 795 | len -= seg_len; |
| 796 | if (!len) |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | if (full_pkt && skb_has_frag_list(skb)) { |
| 802 | printk("skb fraglist:\n"); |
| 803 | skb_walk_frags(skb, list_skb) |
| 804 | skb_dump(level, list_skb, true); |
| 805 | } |
| 806 | } |
| 807 | EXPORT_SYMBOL(skb_dump); |
| 808 | |
Stephen Hemminger | d1a203e | 2008-11-01 21:01:09 -0700 | [diff] [blame] | 809 | /** |
Michael S. Tsirkin | 2512117 | 2012-11-01 09:16:28 +0000 | [diff] [blame] | 810 | * skb_tx_error - report an sk_buff xmit error |
| 811 | * @skb: buffer that triggered an error |
| 812 | * |
| 813 | * Report xmit error if a device callback is tracking this skb. |
| 814 | * skb must be freed afterwards. |
| 815 | */ |
| 816 | void skb_tx_error(struct sk_buff *skb) |
| 817 | { |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 818 | skb_zcopy_clear(skb, true); |
Michael S. Tsirkin | 2512117 | 2012-11-01 09:16:28 +0000 | [diff] [blame] | 819 | } |
| 820 | EXPORT_SYMBOL(skb_tx_error); |
| 821 | |
| 822 | /** |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 823 | * consume_skb - free an skbuff |
| 824 | * @skb: buffer to free |
| 825 | * |
| 826 | * Drop a ref to the buffer and free it if the usage count has hit zero |
| 827 | * Functions identically to kfree_skb, but kfree_skb assumes that the frame |
| 828 | * is being dropped after a failure and notes that |
| 829 | */ |
| 830 | void consume_skb(struct sk_buff *skb) |
| 831 | { |
Paolo Abeni | 3889a803 | 2017-06-12 11:23:41 +0200 | [diff] [blame] | 832 | if (!skb_unref(skb)) |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 833 | return; |
Paolo Abeni | 3889a803 | 2017-06-12 11:23:41 +0200 | [diff] [blame] | 834 | |
Koki Sanagi | 07dc22e | 2010-08-23 18:46:12 +0900 | [diff] [blame] | 835 | trace_consume_skb(skb); |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 836 | __kfree_skb(skb); |
| 837 | } |
| 838 | EXPORT_SYMBOL(consume_skb); |
| 839 | |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 840 | /** |
| 841 | * consume_stateless_skb - free an skbuff, assuming it is stateless |
| 842 | * @skb: buffer to free |
| 843 | * |
Paolo Abeni | ca2c141 | 2017-09-06 14:44:36 +0200 | [diff] [blame] | 844 | * Alike consume_skb(), but this variant assumes that this is the last |
| 845 | * skb reference and all the head states have been already dropped |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 846 | */ |
Paolo Abeni | ca2c141 | 2017-09-06 14:44:36 +0200 | [diff] [blame] | 847 | void __consume_stateless_skb(struct sk_buff *skb) |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 848 | { |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 849 | trace_consume_skb(skb); |
Florian Westphal | 06dc75a | 2017-07-17 18:56:54 +0200 | [diff] [blame] | 850 | skb_release_data(skb); |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 851 | kfree_skbmem(skb); |
| 852 | } |
| 853 | |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 854 | void __kfree_skb_flush(void) |
| 855 | { |
| 856 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
| 857 | |
| 858 | /* flush skb_cache if containing objects */ |
| 859 | if (nc->skb_count) { |
| 860 | kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count, |
| 861 | nc->skb_cache); |
| 862 | nc->skb_count = 0; |
| 863 | } |
| 864 | } |
| 865 | |
Jesper Dangaard Brouer | 15fad71 | 2016-02-08 13:15:04 +0100 | [diff] [blame] | 866 | static inline void _kfree_skb_defer(struct sk_buff *skb) |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 867 | { |
| 868 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
| 869 | |
| 870 | /* drop skb->head and call any destructors for packet */ |
| 871 | skb_release_all(skb); |
| 872 | |
| 873 | /* record skb to CPU local list */ |
| 874 | nc->skb_cache[nc->skb_count++] = skb; |
| 875 | |
| 876 | #ifdef CONFIG_SLUB |
| 877 | /* SLUB writes into objects when freeing */ |
| 878 | prefetchw(skb); |
| 879 | #endif |
| 880 | |
| 881 | /* flush skb_cache if it is filled */ |
| 882 | if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { |
| 883 | kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE, |
| 884 | nc->skb_cache); |
| 885 | nc->skb_count = 0; |
| 886 | } |
| 887 | } |
Jesper Dangaard Brouer | 15fad71 | 2016-02-08 13:15:04 +0100 | [diff] [blame] | 888 | void __kfree_skb_defer(struct sk_buff *skb) |
| 889 | { |
| 890 | _kfree_skb_defer(skb); |
| 891 | } |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 892 | |
| 893 | void napi_consume_skb(struct sk_buff *skb, int budget) |
| 894 | { |
| 895 | if (unlikely(!skb)) |
| 896 | return; |
| 897 | |
Jesper Dangaard Brouer | 885eb0a | 2016-03-11 09:43:58 +0100 | [diff] [blame] | 898 | /* Zero budget indicate non-NAPI context called us, like netpoll */ |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 899 | if (unlikely(!budget)) { |
Jesper Dangaard Brouer | 885eb0a | 2016-03-11 09:43:58 +0100 | [diff] [blame] | 900 | dev_consume_skb_any(skb); |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 901 | return; |
| 902 | } |
| 903 | |
Paolo Abeni | 7608894 | 2017-06-14 11:48:48 +0200 | [diff] [blame] | 904 | if (!skb_unref(skb)) |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 905 | return; |
Paolo Abeni | 7608894 | 2017-06-14 11:48:48 +0200 | [diff] [blame] | 906 | |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 907 | /* if reaching here SKB is ready to free */ |
| 908 | trace_consume_skb(skb); |
| 909 | |
| 910 | /* if SKB is a clone, don't handle this case */ |
Eric Dumazet | abbdb5a | 2016-03-20 11:27:47 -0700 | [diff] [blame] | 911 | if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 912 | __kfree_skb(skb); |
| 913 | return; |
| 914 | } |
| 915 | |
Jesper Dangaard Brouer | 15fad71 | 2016-02-08 13:15:04 +0100 | [diff] [blame] | 916 | _kfree_skb_defer(skb); |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 917 | } |
| 918 | EXPORT_SYMBOL(napi_consume_skb); |
| 919 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 920 | /* Make sure a field is enclosed inside headers_start/headers_end section */ |
| 921 | #define CHECK_SKB_FIELD(field) \ |
| 922 | BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ |
| 923 | offsetof(struct sk_buff, headers_start)); \ |
| 924 | BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ |
| 925 | offsetof(struct sk_buff, headers_end)); \ |
| 926 | |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 927 | static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 928 | { |
| 929 | new->tstamp = old->tstamp; |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 930 | /* We do not copy old->sk */ |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 931 | new->dev = old->dev; |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 932 | memcpy(new->cb, old->cb, sizeof(old->cb)); |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 933 | skb_dst_copy(new, old); |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 934 | __skb_ext_copy(new, old); |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 935 | __nf_copy(new, old, false); |
Patrick McHardy | 6aa895b | 2008-07-14 22:49:06 -0700 | [diff] [blame] | 936 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 937 | /* Note : this field could be in headers_start/headers_end section |
| 938 | * It is not yet because we do not want to have a 16 bit hole |
| 939 | */ |
| 940 | new->queue_mapping = old->queue_mapping; |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 941 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 942 | memcpy(&new->headers_start, &old->headers_start, |
| 943 | offsetof(struct sk_buff, headers_end) - |
| 944 | offsetof(struct sk_buff, headers_start)); |
| 945 | CHECK_SKB_FIELD(protocol); |
| 946 | CHECK_SKB_FIELD(csum); |
| 947 | CHECK_SKB_FIELD(hash); |
| 948 | CHECK_SKB_FIELD(priority); |
| 949 | CHECK_SKB_FIELD(skb_iif); |
| 950 | CHECK_SKB_FIELD(vlan_proto); |
| 951 | CHECK_SKB_FIELD(vlan_tci); |
| 952 | CHECK_SKB_FIELD(transport_header); |
| 953 | CHECK_SKB_FIELD(network_header); |
| 954 | CHECK_SKB_FIELD(mac_header); |
| 955 | CHECK_SKB_FIELD(inner_protocol); |
| 956 | CHECK_SKB_FIELD(inner_transport_header); |
| 957 | CHECK_SKB_FIELD(inner_network_header); |
| 958 | CHECK_SKB_FIELD(inner_mac_header); |
| 959 | CHECK_SKB_FIELD(mark); |
| 960 | #ifdef CONFIG_NETWORK_SECMARK |
| 961 | CHECK_SKB_FIELD(secmark); |
| 962 | #endif |
Cong Wang | e0d1095 | 2013-08-01 11:10:25 +0800 | [diff] [blame] | 963 | #ifdef CONFIG_NET_RX_BUSY_POLL |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 964 | CHECK_SKB_FIELD(napi_id); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 965 | #endif |
Eric Dumazet | 2bd8248 | 2015-02-03 23:48:24 -0800 | [diff] [blame] | 966 | #ifdef CONFIG_XPS |
| 967 | CHECK_SKB_FIELD(sender_cpu); |
| 968 | #endif |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 969 | #ifdef CONFIG_NET_SCHED |
| 970 | CHECK_SKB_FIELD(tc_index); |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 971 | #endif |
| 972 | |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Herbert Xu | 82c49a3 | 2009-05-22 22:11:37 +0000 | [diff] [blame] | 975 | /* |
| 976 | * You should not add any new code to this function. Add it to |
| 977 | * __copy_skb_header above instead. |
| 978 | */ |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 979 | 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] | 980 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | #define C(x) n->x = skb->x |
| 982 | |
| 983 | n->next = n->prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | n->sk = NULL; |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 985 | __copy_skb_header(n, skb); |
| 986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | C(len); |
| 988 | C(data_len); |
Alexey Dobriyan | 3e6b3b2 | 2007-03-16 15:00:46 -0700 | [diff] [blame] | 989 | C(mac_len); |
Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 990 | n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 991 | n->cloned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | n->nohdr = 0; |
Eric Dumazet | b13dda9 | 2018-04-07 13:42:39 -0700 | [diff] [blame] | 993 | n->peeked = 0; |
Stefano Brivio | e78bfb0 | 2018-07-13 13:21:07 +0200 | [diff] [blame] | 994 | C(pfmemalloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | n->destructor = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | C(tail); |
| 997 | C(end); |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 998 | C(head); |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 999 | C(head_frag); |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 1000 | C(data); |
| 1001 | C(truesize); |
Reshetova, Elena | 6335479 | 2017-06-30 13:07:58 +0300 | [diff] [blame] | 1002 | refcount_set(&n->users, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
| 1004 | atomic_inc(&(skb_shinfo(skb)->dataref)); |
| 1005 | skb->cloned = 1; |
| 1006 | |
| 1007 | return n; |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1008 | #undef C |
| 1009 | } |
| 1010 | |
| 1011 | /** |
Jakub Kicinski | da29e4b | 2019-06-03 15:16:58 -0700 | [diff] [blame] | 1012 | * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg |
| 1013 | * @first: first sk_buff of the msg |
| 1014 | */ |
| 1015 | struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) |
| 1016 | { |
| 1017 | struct sk_buff *n; |
| 1018 | |
| 1019 | n = alloc_skb(0, GFP_ATOMIC); |
| 1020 | if (!n) |
| 1021 | return NULL; |
| 1022 | |
| 1023 | n->len = first->len; |
| 1024 | n->data_len = first->len; |
| 1025 | n->truesize = first->truesize; |
| 1026 | |
| 1027 | skb_shinfo(n)->frag_list = first; |
| 1028 | |
| 1029 | __copy_skb_header(n, first); |
| 1030 | n->destructor = NULL; |
| 1031 | |
| 1032 | return n; |
| 1033 | } |
| 1034 | EXPORT_SYMBOL_GPL(alloc_skb_for_msg); |
| 1035 | |
| 1036 | /** |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1037 | * skb_morph - morph one skb into another |
| 1038 | * @dst: the skb to receive the contents |
| 1039 | * @src: the skb to supply the contents |
| 1040 | * |
| 1041 | * This is identical to skb_clone except that the target skb is |
| 1042 | * supplied by the user. |
| 1043 | * |
| 1044 | * The target skb is returned upon exit. |
| 1045 | */ |
| 1046 | struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) |
| 1047 | { |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 1048 | skb_release_all(dst); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1049 | return __skb_clone(dst, src); |
| 1050 | } |
| 1051 | EXPORT_SYMBOL_GPL(skb_morph); |
| 1052 | |
Sowmini Varadhan | 6f89dbc | 2018-02-15 10:49:32 -0800 | [diff] [blame] | 1053 | int mm_account_pinned_pages(struct mmpin *mmp, size_t size) |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1054 | { |
| 1055 | unsigned long max_pg, num_pg, new_pg, old_pg; |
| 1056 | struct user_struct *user; |
| 1057 | |
| 1058 | if (capable(CAP_IPC_LOCK) || !size) |
| 1059 | return 0; |
| 1060 | |
| 1061 | num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ |
| 1062 | max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 1063 | user = mmp->user ? : current_user(); |
| 1064 | |
| 1065 | do { |
| 1066 | old_pg = atomic_long_read(&user->locked_vm); |
| 1067 | new_pg = old_pg + num_pg; |
| 1068 | if (new_pg > max_pg) |
| 1069 | return -ENOBUFS; |
| 1070 | } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != |
| 1071 | old_pg); |
| 1072 | |
| 1073 | if (!mmp->user) { |
| 1074 | mmp->user = get_uid(user); |
| 1075 | mmp->num_pg = num_pg; |
| 1076 | } else { |
| 1077 | mmp->num_pg += num_pg; |
| 1078 | } |
| 1079 | |
| 1080 | return 0; |
| 1081 | } |
Sowmini Varadhan | 6f89dbc | 2018-02-15 10:49:32 -0800 | [diff] [blame] | 1082 | EXPORT_SYMBOL_GPL(mm_account_pinned_pages); |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1083 | |
Sowmini Varadhan | 6f89dbc | 2018-02-15 10:49:32 -0800 | [diff] [blame] | 1084 | void mm_unaccount_pinned_pages(struct mmpin *mmp) |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1085 | { |
| 1086 | if (mmp->user) { |
| 1087 | atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); |
| 1088 | free_uid(mmp->user); |
| 1089 | } |
| 1090 | } |
Sowmini Varadhan | 6f89dbc | 2018-02-15 10:49:32 -0800 | [diff] [blame] | 1091 | EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1092 | |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1093 | struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size) |
| 1094 | { |
| 1095 | struct ubuf_info *uarg; |
| 1096 | struct sk_buff *skb; |
| 1097 | |
| 1098 | WARN_ON_ONCE(!in_task()); |
| 1099 | |
| 1100 | skb = sock_omalloc(sk, 0, GFP_KERNEL); |
| 1101 | if (!skb) |
| 1102 | return NULL; |
| 1103 | |
| 1104 | BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); |
| 1105 | uarg = (void *)skb->cb; |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1106 | uarg->mmp.user = NULL; |
| 1107 | |
| 1108 | if (mm_account_pinned_pages(&uarg->mmp, size)) { |
| 1109 | kfree_skb(skb); |
| 1110 | return NULL; |
| 1111 | } |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1112 | |
| 1113 | uarg->callback = sock_zerocopy_callback; |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1114 | uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; |
| 1115 | uarg->len = 1; |
| 1116 | uarg->bytelen = size; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1117 | uarg->zerocopy = 1; |
Eric Dumazet | c1d1b43 | 2017-08-31 16:48:22 -0700 | [diff] [blame] | 1118 | refcount_set(&uarg->refcnt, 1); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1119 | sock_hold(sk); |
| 1120 | |
| 1121 | return uarg; |
| 1122 | } |
| 1123 | EXPORT_SYMBOL_GPL(sock_zerocopy_alloc); |
| 1124 | |
| 1125 | static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg) |
| 1126 | { |
| 1127 | return container_of((void *)uarg, struct sk_buff, cb); |
| 1128 | } |
| 1129 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1130 | struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size, |
| 1131 | struct ubuf_info *uarg) |
| 1132 | { |
| 1133 | if (uarg) { |
| 1134 | const u32 byte_limit = 1 << 19; /* limit to a few TSO */ |
| 1135 | u32 bytelen, next; |
| 1136 | |
| 1137 | /* realloc only when socket is locked (TCP, UDP cork), |
| 1138 | * so uarg->len and sk_zckey access is serialized |
| 1139 | */ |
| 1140 | if (!sock_owned_by_user(sk)) { |
| 1141 | WARN_ON_ONCE(1); |
| 1142 | return NULL; |
| 1143 | } |
| 1144 | |
| 1145 | bytelen = uarg->bytelen + size; |
| 1146 | if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) { |
| 1147 | /* TCP can create new skb to attach new uarg */ |
| 1148 | if (sk->sk_type == SOCK_STREAM) |
| 1149 | goto new_alloc; |
| 1150 | return NULL; |
| 1151 | } |
| 1152 | |
| 1153 | next = (u32)atomic_read(&sk->sk_zckey); |
| 1154 | if ((u32)(uarg->id + uarg->len) == next) { |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1155 | if (mm_account_pinned_pages(&uarg->mmp, size)) |
| 1156 | return NULL; |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1157 | uarg->len++; |
| 1158 | uarg->bytelen = bytelen; |
| 1159 | atomic_set(&sk->sk_zckey, ++next); |
Willem de Bruijn | 100f6d8 | 2019-05-30 18:01:21 -0400 | [diff] [blame] | 1160 | |
| 1161 | /* no extra ref when appending to datagram (MSG_MORE) */ |
| 1162 | if (sk->sk_type == SOCK_STREAM) |
| 1163 | sock_zerocopy_get(uarg); |
| 1164 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1165 | return uarg; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | new_alloc: |
| 1170 | return sock_zerocopy_alloc(sk, size); |
| 1171 | } |
| 1172 | EXPORT_SYMBOL_GPL(sock_zerocopy_realloc); |
| 1173 | |
| 1174 | static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) |
| 1175 | { |
| 1176 | struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); |
| 1177 | u32 old_lo, old_hi; |
| 1178 | u64 sum_len; |
| 1179 | |
| 1180 | old_lo = serr->ee.ee_info; |
| 1181 | old_hi = serr->ee.ee_data; |
| 1182 | sum_len = old_hi - old_lo + 1ULL + len; |
| 1183 | |
| 1184 | if (sum_len >= (1ULL << 32)) |
| 1185 | return false; |
| 1186 | |
| 1187 | if (lo != old_hi + 1) |
| 1188 | return false; |
| 1189 | |
| 1190 | serr->ee.ee_data += len; |
| 1191 | return true; |
| 1192 | } |
| 1193 | |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1194 | void sock_zerocopy_callback(struct ubuf_info *uarg, bool success) |
| 1195 | { |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1196 | struct sk_buff *tail, *skb = skb_from_uarg(uarg); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1197 | struct sock_exterr_skb *serr; |
| 1198 | struct sock *sk = skb->sk; |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1199 | struct sk_buff_head *q; |
| 1200 | unsigned long flags; |
| 1201 | u32 lo, hi; |
| 1202 | u16 len; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1203 | |
Willem de Bruijn | ccaffff | 2017-08-09 19:09:43 -0400 | [diff] [blame] | 1204 | mm_unaccount_pinned_pages(&uarg->mmp); |
| 1205 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1206 | /* if !len, there was only 1 call, and it was aborted |
| 1207 | * so do not queue a completion notification |
| 1208 | */ |
| 1209 | if (!uarg->len || sock_flag(sk, SOCK_DEAD)) |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1210 | goto release; |
| 1211 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1212 | len = uarg->len; |
| 1213 | lo = uarg->id; |
| 1214 | hi = uarg->id + len - 1; |
| 1215 | |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1216 | serr = SKB_EXT_ERR(skb); |
| 1217 | memset(serr, 0, sizeof(*serr)); |
| 1218 | serr->ee.ee_errno = 0; |
| 1219 | serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1220 | serr->ee.ee_data = hi; |
| 1221 | serr->ee.ee_info = lo; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1222 | if (!success) |
| 1223 | serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; |
| 1224 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1225 | q = &sk->sk_error_queue; |
| 1226 | spin_lock_irqsave(&q->lock, flags); |
| 1227 | tail = skb_peek_tail(q); |
| 1228 | if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || |
| 1229 | !skb_zerocopy_notify_extend(tail, lo, len)) { |
| 1230 | __skb_queue_tail(q, skb); |
| 1231 | skb = NULL; |
| 1232 | } |
| 1233 | spin_unlock_irqrestore(&q->lock, flags); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1234 | |
| 1235 | sk->sk_error_report(sk); |
| 1236 | |
| 1237 | release: |
| 1238 | consume_skb(skb); |
| 1239 | sock_put(sk); |
| 1240 | } |
| 1241 | EXPORT_SYMBOL_GPL(sock_zerocopy_callback); |
| 1242 | |
| 1243 | void sock_zerocopy_put(struct ubuf_info *uarg) |
| 1244 | { |
Eric Dumazet | c1d1b43 | 2017-08-31 16:48:22 -0700 | [diff] [blame] | 1245 | if (uarg && refcount_dec_and_test(&uarg->refcnt)) { |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1246 | if (uarg->callback) |
| 1247 | uarg->callback(uarg, uarg->zerocopy); |
| 1248 | else |
| 1249 | consume_skb(skb_from_uarg(uarg)); |
| 1250 | } |
| 1251 | } |
| 1252 | EXPORT_SYMBOL_GPL(sock_zerocopy_put); |
| 1253 | |
Willem de Bruijn | 52900d2 | 2018-11-30 15:32:40 -0500 | [diff] [blame] | 1254 | void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1255 | { |
| 1256 | if (uarg) { |
| 1257 | struct sock *sk = skb_from_uarg(uarg)->sk; |
| 1258 | |
| 1259 | atomic_dec(&sk->sk_zckey); |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1260 | uarg->len--; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1261 | |
Willem de Bruijn | 52900d2 | 2018-11-30 15:32:40 -0500 | [diff] [blame] | 1262 | if (have_uref) |
| 1263 | sock_zerocopy_put(uarg); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort); |
| 1267 | |
Willem de Bruijn | b5947e5 | 2018-11-30 15:32:39 -0500 | [diff] [blame] | 1268 | int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len) |
| 1269 | { |
| 1270 | return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len); |
| 1271 | } |
| 1272 | EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram); |
| 1273 | |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1274 | int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, |
| 1275 | struct msghdr *msg, int len, |
| 1276 | struct ubuf_info *uarg) |
| 1277 | { |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1278 | struct ubuf_info *orig_uarg = skb_zcopy(skb); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1279 | struct iov_iter orig_iter = msg->msg_iter; |
| 1280 | int err, orig_len = skb->len; |
| 1281 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1282 | /* An skb can only point to one uarg. This edge case happens when |
| 1283 | * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. |
| 1284 | */ |
| 1285 | if (orig_uarg && uarg != orig_uarg) |
| 1286 | return -EEXIST; |
| 1287 | |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1288 | err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len); |
| 1289 | if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { |
Willem de Bruijn | 54d43117 | 2017-10-19 12:40:39 -0400 | [diff] [blame] | 1290 | struct sock *save_sk = skb->sk; |
| 1291 | |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1292 | /* Streams do not free skb on error. Reset to prev state. */ |
| 1293 | msg->msg_iter = orig_iter; |
Willem de Bruijn | 54d43117 | 2017-10-19 12:40:39 -0400 | [diff] [blame] | 1294 | skb->sk = sk; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1295 | ___pskb_trim(skb, orig_len); |
Willem de Bruijn | 54d43117 | 2017-10-19 12:40:39 -0400 | [diff] [blame] | 1296 | skb->sk = save_sk; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1297 | return err; |
| 1298 | } |
| 1299 | |
Willem de Bruijn | 52900d2 | 2018-11-30 15:32:40 -0500 | [diff] [blame] | 1300 | skb_zcopy_set(skb, uarg, NULL); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1301 | return skb->len - orig_len; |
| 1302 | } |
| 1303 | EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); |
| 1304 | |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 1305 | static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1306 | gfp_t gfp_mask) |
| 1307 | { |
| 1308 | if (skb_zcopy(orig)) { |
| 1309 | if (skb_zcopy(nskb)) { |
| 1310 | /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ |
| 1311 | if (!gfp_mask) { |
| 1312 | WARN_ON_ONCE(1); |
| 1313 | return -ENOMEM; |
| 1314 | } |
| 1315 | if (skb_uarg(nskb) == skb_uarg(orig)) |
| 1316 | return 0; |
| 1317 | if (skb_copy_ubufs(nskb, GFP_ATOMIC)) |
| 1318 | return -EIO; |
| 1319 | } |
Willem de Bruijn | 52900d2 | 2018-11-30 15:32:40 -0500 | [diff] [blame] | 1320 | skb_zcopy_set(nskb, skb_uarg(orig), NULL); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1321 | } |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 1325 | /** |
| 1326 | * skb_copy_ubufs - copy userspace skb frags buffers to kernel |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 1327 | * @skb: the skb to modify |
| 1328 | * @gfp_mask: allocation priority |
| 1329 | * |
| 1330 | * This must be called on SKBTX_DEV_ZEROCOPY skb. |
| 1331 | * It will copy all frags into kernel and drop the reference |
| 1332 | * to userspace pages. |
| 1333 | * |
| 1334 | * If this function is called from an interrupt gfp_mask() must be |
| 1335 | * %GFP_ATOMIC. |
| 1336 | * |
| 1337 | * Returns 0 on success or a negative error code on failure |
| 1338 | * to allocate kernel memory to copy to. |
| 1339 | */ |
| 1340 | int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1341 | { |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1342 | int num_frags = skb_shinfo(skb)->nr_frags; |
| 1343 | struct page *page, *head = NULL; |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1344 | int i, new_frags; |
| 1345 | u32 d_off; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1346 | |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1347 | if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) |
| 1348 | return -EINVAL; |
| 1349 | |
Willem de Bruijn | f72c4ac | 2017-12-28 12:38:13 -0500 | [diff] [blame] | 1350 | if (!num_frags) |
| 1351 | goto release; |
| 1352 | |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1353 | new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1354 | for (i = 0; i < new_frags; i++) { |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 1355 | page = alloc_page(gfp_mask); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1356 | if (!page) { |
| 1357 | while (head) { |
Sunghan Suh | 40dadff | 2013-07-12 16:17:23 +0900 | [diff] [blame] | 1358 | struct page *next = (struct page *)page_private(head); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1359 | put_page(head); |
| 1360 | head = next; |
| 1361 | } |
| 1362 | return -ENOMEM; |
| 1363 | } |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1364 | set_page_private(page, (unsigned long)head); |
| 1365 | head = page; |
| 1366 | } |
| 1367 | |
| 1368 | page = head; |
| 1369 | d_off = 0; |
| 1370 | for (i = 0; i < num_frags; i++) { |
| 1371 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
| 1372 | u32 p_off, p_len, copied; |
| 1373 | struct page *p; |
| 1374 | u8 *vaddr; |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 1375 | |
| 1376 | skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f), |
| 1377 | p, p_off, p_len, copied) { |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1378 | u32 copy, done = 0; |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 1379 | vaddr = kmap_atomic(p); |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1380 | |
| 1381 | while (done < p_len) { |
| 1382 | if (d_off == PAGE_SIZE) { |
| 1383 | d_off = 0; |
| 1384 | page = (struct page *)page_private(page); |
| 1385 | } |
| 1386 | copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); |
| 1387 | memcpy(page_address(page) + d_off, |
| 1388 | vaddr + p_off + done, copy); |
| 1389 | done += copy; |
| 1390 | d_off += copy; |
| 1391 | } |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 1392 | kunmap_atomic(vaddr); |
| 1393 | } |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | /* skb frags release userspace buffers */ |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 1397 | for (i = 0; i < num_frags; i++) |
Ian Campbell | a8605c6 | 2011-10-19 23:01:49 +0000 | [diff] [blame] | 1398 | skb_frag_unref(skb, i); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1399 | |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1400 | /* skb frags point to kernel buffers */ |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1401 | for (i = 0; i < new_frags - 1; i++) { |
| 1402 | __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); |
Sunghan Suh | 40dadff | 2013-07-12 16:17:23 +0900 | [diff] [blame] | 1403 | head = (struct page *)page_private(head); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1404 | } |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1405 | __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); |
| 1406 | skb_shinfo(skb)->nr_frags = new_frags; |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 1407 | |
Willem de Bruijn | b90ddd5 | 2017-12-20 17:37:50 -0500 | [diff] [blame] | 1408 | release: |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 1409 | skb_zcopy_clear(skb, false); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1410 | return 0; |
| 1411 | } |
Michael S. Tsirkin | dcc0fb7 | 2012-07-20 09:23:20 +0000 | [diff] [blame] | 1412 | EXPORT_SYMBOL_GPL(skb_copy_ubufs); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1413 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1414 | /** |
| 1415 | * skb_clone - duplicate an sk_buff |
| 1416 | * @skb: buffer to clone |
| 1417 | * @gfp_mask: allocation priority |
| 1418 | * |
| 1419 | * Duplicate an &sk_buff. The new one is not owned by a socket. Both |
| 1420 | * copies share the same packet data but not structure. The new |
| 1421 | * buffer has a reference count of 1. If the allocation fails the |
| 1422 | * function returns %NULL otherwise the new buffer is returned. |
| 1423 | * |
| 1424 | * If this function is called from an interrupt gfp_mask() must be |
| 1425 | * %GFP_ATOMIC. |
| 1426 | */ |
| 1427 | |
| 1428 | struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) |
| 1429 | { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 1430 | struct sk_buff_fclones *fclones = container_of(skb, |
| 1431 | struct sk_buff_fclones, |
| 1432 | skb1); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 1433 | struct sk_buff *n; |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1434 | |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1435 | if (skb_orphan_frags(skb, gfp_mask)) |
| 1436 | return NULL; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1437 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1438 | if (skb->fclone == SKB_FCLONE_ORIG && |
Reshetova, Elena | 2638595 | 2017-06-30 13:07:59 +0300 | [diff] [blame] | 1439 | refcount_read(&fclones->fclone_ref) == 1) { |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 1440 | n = &fclones->skb2; |
Reshetova, Elena | 2638595 | 2017-06-30 13:07:59 +0300 | [diff] [blame] | 1441 | refcount_set(&fclones->fclone_ref, 2); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1442 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1443 | if (skb_pfmemalloc(skb)) |
| 1444 | gfp_mask |= __GFP_MEMALLOC; |
| 1445 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1446 | n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); |
| 1447 | if (!n) |
| 1448 | return NULL; |
Vegard Nossum | fe55f6d | 2008-08-30 12:16:35 +0200 | [diff] [blame] | 1449 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1450 | n->fclone = SKB_FCLONE_UNAVAILABLE; |
| 1451 | } |
| 1452 | |
| 1453 | return __skb_clone(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1455 | EXPORT_SYMBOL(skb_clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
Toshiaki Makita | b0768a8 | 2018-08-03 16:58:09 +0900 | [diff] [blame] | 1457 | void skb_headers_offset_update(struct sk_buff *skb, int off) |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 1458 | { |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 1459 | /* Only adjust this if it actually is csum_start rather than csum */ |
| 1460 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 1461 | skb->csum_start += off; |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 1462 | /* {transport,network,mac}_header and tail are relative to skb->head */ |
| 1463 | skb->transport_header += off; |
| 1464 | skb->network_header += off; |
| 1465 | if (skb_mac_header_was_set(skb)) |
| 1466 | skb->mac_header += off; |
| 1467 | skb->inner_transport_header += off; |
| 1468 | skb->inner_network_header += off; |
Pravin B Shelar | aefbd2b | 2013-03-07 13:21:46 +0000 | [diff] [blame] | 1469 | skb->inner_mac_header += off; |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 1470 | } |
Toshiaki Makita | b0768a8 | 2018-08-03 16:58:09 +0900 | [diff] [blame] | 1471 | EXPORT_SYMBOL(skb_headers_offset_update); |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 1472 | |
Ilya Lesokhin | 08303c1 | 2018-04-30 10:16:11 +0300 | [diff] [blame] | 1473 | void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | { |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 1475 | __copy_skb_header(new, old); |
| 1476 | |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 1477 | skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; |
| 1478 | skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; |
| 1479 | skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | } |
Ilya Lesokhin | 08303c1 | 2018-04-30 10:16:11 +0300 | [diff] [blame] | 1481 | EXPORT_SYMBOL(skb_copy_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1483 | static inline int skb_alloc_rx_flag(const struct sk_buff *skb) |
| 1484 | { |
| 1485 | if (skb_pfmemalloc(skb)) |
| 1486 | return SKB_ALLOC_RX; |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | /** |
| 1491 | * skb_copy - create private copy of an sk_buff |
| 1492 | * @skb: buffer to copy |
| 1493 | * @gfp_mask: allocation priority |
| 1494 | * |
| 1495 | * Make a copy of both an &sk_buff and its data. This is used when the |
| 1496 | * caller wishes to modify the data and needs a private copy of the |
| 1497 | * data to alter. Returns %NULL on failure or the pointer to the buffer |
| 1498 | * on success. The returned buffer has a reference count of 1. |
| 1499 | * |
| 1500 | * As by-product this function converts non-linear &sk_buff to linear |
| 1501 | * one, so that &sk_buff becomes completely private and caller is allowed |
| 1502 | * to modify all the data of returned buffer. This means that this |
| 1503 | * function is not recommended for use in circumstances when only |
| 1504 | * header is going to be modified. Use pskb_copy() instead. |
| 1505 | */ |
| 1506 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1507 | 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] | 1508 | { |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1509 | int headerlen = skb_headroom(skb); |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 1510 | unsigned int size = skb_end_offset(skb) + skb->data_len; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1511 | struct sk_buff *n = __alloc_skb(size, gfp_mask, |
| 1512 | skb_alloc_rx_flag(skb), NUMA_NO_NODE); |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | if (!n) |
| 1515 | return NULL; |
| 1516 | |
| 1517 | /* Set the data pointer */ |
| 1518 | skb_reserve(n, headerlen); |
| 1519 | /* Set the tail pointer and length */ |
| 1520 | skb_put(n, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
Tim Hansen | 9f77fad | 2017-10-09 11:37:59 -0400 | [diff] [blame] | 1522 | BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | |
Ilya Lesokhin | 08303c1 | 2018-04-30 10:16:11 +0300 | [diff] [blame] | 1524 | skb_copy_header(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | return n; |
| 1526 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1527 | EXPORT_SYMBOL(skb_copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
| 1529 | /** |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1530 | * __pskb_copy_fclone - create copy of an sk_buff with private head. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | * @skb: buffer to copy |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1532 | * @headroom: headroom of new skb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | * @gfp_mask: allocation priority |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1534 | * @fclone: if true allocate the copy of the skb from the fclone |
| 1535 | * cache instead of the head cache; it is recommended to set this |
| 1536 | * to true for the cases where the copy will likely be cloned |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | * |
| 1538 | * Make a copy of both an &sk_buff and part of its data, located |
| 1539 | * in header. Fragmented data remain shared. This is used when |
| 1540 | * the caller wishes to modify only header of &sk_buff and needs |
| 1541 | * private copy of the header to alter. Returns %NULL on failure |
| 1542 | * or the pointer to the buffer on success. |
| 1543 | * The returned buffer has a reference count of 1. |
| 1544 | */ |
| 1545 | |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1546 | struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, |
| 1547 | gfp_t gfp_mask, bool fclone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | { |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1549 | unsigned int size = skb_headlen(skb) + headroom; |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1550 | int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); |
| 1551 | 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] | 1552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | if (!n) |
| 1554 | goto out; |
| 1555 | |
| 1556 | /* Set the data pointer */ |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1557 | skb_reserve(n, headroom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | /* Set the tail pointer and length */ |
| 1559 | skb_put(n, skb_headlen(skb)); |
| 1560 | /* Copy the bytes */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1561 | skb_copy_from_linear_data(skb, n->data, n->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | |
Herbert Xu | 25f484a | 2006-11-07 14:57:15 -0800 | [diff] [blame] | 1563 | n->truesize += skb->data_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | n->data_len = skb->data_len; |
| 1565 | n->len = skb->len; |
| 1566 | |
| 1567 | if (skb_shinfo(skb)->nr_frags) { |
| 1568 | int i; |
| 1569 | |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 1570 | if (skb_orphan_frags(skb, gfp_mask) || |
| 1571 | skb_zerocopy_clone(n, skb, gfp_mask)) { |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1572 | kfree_skb(n); |
| 1573 | n = NULL; |
| 1574 | goto out; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1577 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1578 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | } |
| 1580 | skb_shinfo(n)->nr_frags = i; |
| 1581 | } |
| 1582 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1583 | if (skb_has_frag_list(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; |
| 1585 | skb_clone_fraglist(n); |
| 1586 | } |
| 1587 | |
Ilya Lesokhin | 08303c1 | 2018-04-30 10:16:11 +0300 | [diff] [blame] | 1588 | skb_copy_header(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | out: |
| 1590 | return n; |
| 1591 | } |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1592 | EXPORT_SYMBOL(__pskb_copy_fclone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
| 1594 | /** |
| 1595 | * pskb_expand_head - reallocate header of &sk_buff |
| 1596 | * @skb: buffer to reallocate |
| 1597 | * @nhead: room to add at head |
| 1598 | * @ntail: room to add at tail |
| 1599 | * @gfp_mask: allocation priority |
| 1600 | * |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 1601 | * Expands (or creates identical copy, if @nhead and @ntail are zero) |
| 1602 | * 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] | 1603 | * reference count of 1. Returns zero in the case of success or error, |
| 1604 | * if expansion failed. In the last case, &sk_buff is not changed. |
| 1605 | * |
| 1606 | * All the pointers pointing into skb header may change and must be |
| 1607 | * reloaded after call to this function. |
| 1608 | */ |
| 1609 | |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 1610 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1611 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | { |
Eric Dumazet | 158f323 | 2017-01-27 07:11:27 -0800 | [diff] [blame] | 1613 | int i, osize = skb_end_offset(skb); |
| 1614 | int size = osize + nhead + ntail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | long off; |
Eric Dumazet | 158f323 | 2017-01-27 07:11:27 -0800 | [diff] [blame] | 1616 | u8 *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | |
Herbert Xu | 4edd87a | 2008-10-01 07:09:38 -0700 | [diff] [blame] | 1618 | BUG_ON(nhead < 0); |
| 1619 | |
Tim Hansen | 9f77fad | 2017-10-09 11:37:59 -0400 | [diff] [blame] | 1620 | BUG_ON(skb_shared(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | |
| 1622 | size = SKB_DATA_ALIGN(size); |
| 1623 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1624 | if (skb_pfmemalloc(skb)) |
| 1625 | gfp_mask |= __GFP_MEMALLOC; |
| 1626 | data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), |
| 1627 | gfp_mask, NUMA_NO_NODE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | if (!data) |
| 1629 | goto nodata; |
Eric Dumazet | 87151b8 | 2012-04-10 20:08:39 +0000 | [diff] [blame] | 1630 | size = SKB_WITH_OVERHEAD(ksize(data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
| 1632 | /* Copy only real data... and, alas, header. This should be |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1633 | * optimized for the cases when header is void. |
| 1634 | */ |
| 1635 | memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); |
| 1636 | |
| 1637 | memcpy((struct skb_shared_info *)(data + size), |
| 1638 | skb_shinfo(skb), |
Eric Dumazet | fed6638 | 2010-07-22 19:09:08 +0000 | [diff] [blame] | 1639 | offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | |
Alexander Duyck | 3e24591 | 2012-05-04 14:26:51 +0000 | [diff] [blame] | 1641 | /* |
| 1642 | * if shinfo is shared we must drop the old head gracefully, but if it |
| 1643 | * is not we can just drop the old head and let the existing refcount |
| 1644 | * be since all we did is relocate the values |
| 1645 | */ |
| 1646 | if (skb_cloned(skb)) { |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1647 | if (skb_orphan_frags(skb, gfp_mask)) |
| 1648 | goto nofrags; |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 1649 | if (skb_zcopy(skb)) |
Eric Dumazet | c1d1b43 | 2017-08-31 16:48:22 -0700 | [diff] [blame] | 1650 | refcount_inc(&skb_uarg(skb)->refcnt); |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1651 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1652 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1654 | if (skb_has_frag_list(skb)) |
| 1655 | skb_clone_fraglist(skb); |
| 1656 | |
| 1657 | skb_release_data(skb); |
Alexander Duyck | 3e24591 | 2012-05-04 14:26:51 +0000 | [diff] [blame] | 1658 | } else { |
| 1659 | skb_free_head(skb); |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | off = (data + nhead) - skb->head; |
| 1662 | |
| 1663 | skb->head = data; |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 1664 | skb->head_frag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | skb->data += off; |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1666 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 1667 | skb->end = size; |
Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 1668 | off = nhead; |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1669 | #else |
| 1670 | skb->end = skb->head + size; |
Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 1671 | #endif |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1672 | skb->tail += off; |
Peter Pan(潘卫平) | b41abb4 | 2013-06-06 21:27:21 +0800 | [diff] [blame] | 1673 | skb_headers_offset_update(skb, nhead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | skb->cloned = 0; |
Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 1675 | skb->hdr_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | skb->nohdr = 0; |
| 1677 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
Eric Dumazet | 158f323 | 2017-01-27 07:11:27 -0800 | [diff] [blame] | 1678 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1679 | skb_metadata_clear(skb); |
| 1680 | |
Eric Dumazet | 158f323 | 2017-01-27 07:11:27 -0800 | [diff] [blame] | 1681 | /* It is not generally safe to change skb->truesize. |
| 1682 | * For the moment, we really care of rx path, or |
| 1683 | * when skb is orphaned (not attached to a socket). |
| 1684 | */ |
| 1685 | if (!skb->sk || skb->destructor == sock_edemux) |
| 1686 | skb->truesize += size - osize; |
| 1687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | return 0; |
| 1689 | |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1690 | nofrags: |
| 1691 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | nodata: |
| 1693 | return -ENOMEM; |
| 1694 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1695 | EXPORT_SYMBOL(pskb_expand_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | |
| 1697 | /* Make private copy of skb with writable head and some headroom */ |
| 1698 | |
| 1699 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) |
| 1700 | { |
| 1701 | struct sk_buff *skb2; |
| 1702 | int delta = headroom - skb_headroom(skb); |
| 1703 | |
| 1704 | if (delta <= 0) |
| 1705 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
| 1706 | else { |
| 1707 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1708 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, |
| 1709 | GFP_ATOMIC)) { |
| 1710 | kfree_skb(skb2); |
| 1711 | skb2 = NULL; |
| 1712 | } |
| 1713 | } |
| 1714 | return skb2; |
| 1715 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1716 | EXPORT_SYMBOL(skb_realloc_headroom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | |
| 1718 | /** |
| 1719 | * skb_copy_expand - copy and expand sk_buff |
| 1720 | * @skb: buffer to copy |
| 1721 | * @newheadroom: new free bytes at head |
| 1722 | * @newtailroom: new free bytes at tail |
| 1723 | * @gfp_mask: allocation priority |
| 1724 | * |
| 1725 | * Make a copy of both an &sk_buff and its data and while doing so |
| 1726 | * allocate additional space. |
| 1727 | * |
| 1728 | * This is used when the caller wishes to modify the data and needs a |
| 1729 | * private copy of the data to alter as well as more space for new fields. |
| 1730 | * Returns %NULL on failure or the pointer to the buffer |
| 1731 | * on success. The returned buffer has a reference count of 1. |
| 1732 | * |
| 1733 | * You must pass %GFP_ATOMIC as the allocation priority if this function |
| 1734 | * is called from an interrupt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | */ |
| 1736 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 1737 | int newheadroom, int newtailroom, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1738 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | { |
| 1740 | /* |
| 1741 | * Allocate the copy buffer |
| 1742 | */ |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1743 | struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, |
| 1744 | gfp_mask, skb_alloc_rx_flag(skb), |
| 1745 | NUMA_NO_NODE); |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1746 | int oldheadroom = skb_headroom(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | int head_copy_len, head_copy_off; |
| 1748 | |
| 1749 | if (!n) |
| 1750 | return NULL; |
| 1751 | |
| 1752 | skb_reserve(n, newheadroom); |
| 1753 | |
| 1754 | /* Set the tail pointer and length */ |
| 1755 | skb_put(n, skb->len); |
| 1756 | |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1757 | head_copy_len = oldheadroom; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | head_copy_off = 0; |
| 1759 | if (newheadroom <= head_copy_len) |
| 1760 | head_copy_len = newheadroom; |
| 1761 | else |
| 1762 | head_copy_off = newheadroom - head_copy_len; |
| 1763 | |
| 1764 | /* Copy the linear header and data. */ |
Tim Hansen | 9f77fad | 2017-10-09 11:37:59 -0400 | [diff] [blame] | 1765 | BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, |
| 1766 | skb->len + head_copy_len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
Ilya Lesokhin | 08303c1 | 2018-04-30 10:16:11 +0300 | [diff] [blame] | 1768 | skb_copy_header(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 1770 | skb_headers_offset_update(n, newheadroom - oldheadroom); |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | return n; |
| 1773 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1774 | EXPORT_SYMBOL(skb_copy_expand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
| 1776 | /** |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1777 | * __skb_pad - zero pad the tail of an skb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | * @skb: buffer to pad |
| 1779 | * @pad: space to pad |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1780 | * @free_on_error: free buffer on error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | * |
| 1782 | * Ensure that a buffer is followed by a padding area that is zero |
| 1783 | * filled. Used by network drivers which may DMA or transfer data |
| 1784 | * beyond the buffer end onto the wire. |
| 1785 | * |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1786 | * May return error in out of memory cases. The skb is freed on error |
| 1787 | * if @free_on_error is true. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1789 | |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1790 | int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | { |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1792 | int err; |
| 1793 | int ntail; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | /* If the skbuff is non linear tailroom is always zero.. */ |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1796 | if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | memset(skb->data+skb->len, 0, pad); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1798 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | } |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1800 | |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1801 | ntail = skb->data_len + pad - (skb->end - skb->tail); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1802 | if (likely(skb_cloned(skb) || ntail > 0)) { |
| 1803 | err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); |
| 1804 | if (unlikely(err)) |
| 1805 | goto free_skb; |
| 1806 | } |
| 1807 | |
| 1808 | /* FIXME: The use of this function with non-linear skb's really needs |
| 1809 | * to be audited. |
| 1810 | */ |
| 1811 | err = skb_linearize(skb); |
| 1812 | if (unlikely(err)) |
| 1813 | goto free_skb; |
| 1814 | |
| 1815 | memset(skb->data + skb->len, 0, pad); |
| 1816 | return 0; |
| 1817 | |
| 1818 | free_skb: |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1819 | if (free_on_error) |
| 1820 | kfree_skb(skb); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1821 | return err; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1822 | } |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1823 | EXPORT_SYMBOL(__skb_pad); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1824 | |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1825 | /** |
Mathias Krause | 0c7ddf3 | 2013-11-07 14:18:24 +0100 | [diff] [blame] | 1826 | * pskb_put - add data to the tail of a potentially fragmented buffer |
| 1827 | * @skb: start of the buffer to use |
| 1828 | * @tail: tail fragment of the buffer to use |
| 1829 | * @len: amount of data to add |
| 1830 | * |
| 1831 | * This function extends the used data area of the potentially |
| 1832 | * fragmented buffer. @tail must be the last fragment of @skb -- or |
| 1833 | * @skb itself. If this would exceed the total buffer size the kernel |
| 1834 | * will panic. A pointer to the first byte of the extra data is |
| 1835 | * returned. |
| 1836 | */ |
| 1837 | |
Johannes Berg | 4df864c | 2017-06-16 14:29:21 +0200 | [diff] [blame] | 1838 | void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) |
Mathias Krause | 0c7ddf3 | 2013-11-07 14:18:24 +0100 | [diff] [blame] | 1839 | { |
| 1840 | if (tail != skb) { |
| 1841 | skb->data_len += len; |
| 1842 | skb->len += len; |
| 1843 | } |
| 1844 | return skb_put(tail, len); |
| 1845 | } |
| 1846 | EXPORT_SYMBOL_GPL(pskb_put); |
| 1847 | |
| 1848 | /** |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1849 | * skb_put - add data to a buffer |
| 1850 | * @skb: buffer to use |
| 1851 | * @len: amount of data to add |
| 1852 | * |
| 1853 | * This function extends the used data area of the buffer. If this would |
| 1854 | * exceed the total buffer size the kernel will panic. A pointer to the |
| 1855 | * first byte of the extra data is returned. |
| 1856 | */ |
Johannes Berg | 4df864c | 2017-06-16 14:29:21 +0200 | [diff] [blame] | 1857 | void *skb_put(struct sk_buff *skb, unsigned int len) |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1858 | { |
Johannes Berg | 4df864c | 2017-06-16 14:29:21 +0200 | [diff] [blame] | 1859 | void *tmp = skb_tail_pointer(skb); |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1860 | SKB_LINEAR_ASSERT(skb); |
| 1861 | skb->tail += len; |
| 1862 | skb->len += len; |
| 1863 | if (unlikely(skb->tail > skb->end)) |
| 1864 | skb_over_panic(skb, len, __builtin_return_address(0)); |
| 1865 | return tmp; |
| 1866 | } |
| 1867 | EXPORT_SYMBOL(skb_put); |
| 1868 | |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1869 | /** |
Ilpo Järvinen | c2aa270 | 2008-03-27 17:52:40 -0700 | [diff] [blame] | 1870 | * skb_push - add data to the start of a buffer |
| 1871 | * @skb: buffer to use |
| 1872 | * @len: amount of data to add |
| 1873 | * |
| 1874 | * This function extends the used data area of the buffer at the buffer |
| 1875 | * start. If this would exceed the total buffer headroom the kernel will |
| 1876 | * panic. A pointer to the first byte of the extra data is returned. |
| 1877 | */ |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 1878 | void *skb_push(struct sk_buff *skb, unsigned int len) |
Ilpo Järvinen | c2aa270 | 2008-03-27 17:52:40 -0700 | [diff] [blame] | 1879 | { |
| 1880 | skb->data -= len; |
| 1881 | skb->len += len; |
Ganesh Goudar | 9aba2f8 | 2018-08-02 15:34:52 +0530 | [diff] [blame] | 1882 | if (unlikely(skb->data < skb->head)) |
Ilpo Järvinen | c2aa270 | 2008-03-27 17:52:40 -0700 | [diff] [blame] | 1883 | skb_under_panic(skb, len, __builtin_return_address(0)); |
| 1884 | return skb->data; |
| 1885 | } |
| 1886 | EXPORT_SYMBOL(skb_push); |
| 1887 | |
| 1888 | /** |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1889 | * skb_pull - remove data from the start of a buffer |
| 1890 | * @skb: buffer to use |
| 1891 | * @len: amount of data to remove |
| 1892 | * |
| 1893 | * This function removes data from the start of a buffer, returning |
| 1894 | * the memory to the headroom. A pointer to the next data in the buffer |
| 1895 | * is returned. Once the data has been pulled future pushes will overwrite |
| 1896 | * the old data. |
| 1897 | */ |
Johannes Berg | af72868 | 2017-06-16 14:29:22 +0200 | [diff] [blame] | 1898 | void *skb_pull(struct sk_buff *skb, unsigned int len) |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1899 | { |
David S. Miller | 47d2964 | 2010-05-02 02:21:44 -0700 | [diff] [blame] | 1900 | return skb_pull_inline(skb, len); |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1901 | } |
| 1902 | EXPORT_SYMBOL(skb_pull); |
| 1903 | |
Ilpo Järvinen | 419ae74 | 2008-03-27 17:54:01 -0700 | [diff] [blame] | 1904 | /** |
| 1905 | * skb_trim - remove end from a buffer |
| 1906 | * @skb: buffer to alter |
| 1907 | * @len: new length |
| 1908 | * |
| 1909 | * Cut the length of a buffer down by removing data from the tail. If |
| 1910 | * the buffer is already under the length specified it is not modified. |
| 1911 | * The skb must be linear. |
| 1912 | */ |
| 1913 | void skb_trim(struct sk_buff *skb, unsigned int len) |
| 1914 | { |
| 1915 | if (skb->len > len) |
| 1916 | __skb_trim(skb, len); |
| 1917 | } |
| 1918 | EXPORT_SYMBOL(skb_trim); |
| 1919 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 1920 | /* Trims skb to length len. It can change skb pointers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | */ |
| 1922 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 1923 | int ___pskb_trim(struct sk_buff *skb, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1925 | struct sk_buff **fragp; |
| 1926 | struct sk_buff *frag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | int offset = skb_headlen(skb); |
| 1928 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 1929 | int i; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1930 | int err; |
| 1931 | |
| 1932 | if (skb_cloned(skb) && |
| 1933 | unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) |
| 1934 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1936 | i = 0; |
| 1937 | if (offset >= len) |
| 1938 | goto drop_pages; |
| 1939 | |
| 1940 | for (; i < nfrags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1941 | int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1942 | |
| 1943 | if (end < len) { |
| 1944 | offset = end; |
| 1945 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | } |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1947 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1948 | skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1949 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1950 | drop_pages: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1951 | skb_shinfo(skb)->nr_frags = i; |
| 1952 | |
| 1953 | for (; i < nfrags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1954 | skb_frag_unref(skb, i); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1955 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1956 | if (skb_has_frag_list(skb)) |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1957 | skb_drop_fraglist(skb); |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1958 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1961 | for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); |
| 1962 | fragp = &frag->next) { |
| 1963 | int end = offset + frag->len; |
| 1964 | |
| 1965 | if (skb_shared(frag)) { |
| 1966 | struct sk_buff *nfrag; |
| 1967 | |
| 1968 | nfrag = skb_clone(frag, GFP_ATOMIC); |
| 1969 | if (unlikely(!nfrag)) |
| 1970 | return -ENOMEM; |
| 1971 | |
| 1972 | nfrag->next = frag->next; |
Eric Dumazet | 85bb2a6 | 2012-04-19 02:24:53 +0000 | [diff] [blame] | 1973 | consume_skb(frag); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1974 | frag = nfrag; |
| 1975 | *fragp = frag; |
| 1976 | } |
| 1977 | |
| 1978 | if (end < len) { |
| 1979 | offset = end; |
| 1980 | continue; |
| 1981 | } |
| 1982 | |
| 1983 | if (end > len && |
| 1984 | unlikely((err = pskb_trim(frag, len - offset)))) |
| 1985 | return err; |
| 1986 | |
| 1987 | if (frag->next) |
| 1988 | skb_drop_list(&frag->next); |
| 1989 | break; |
| 1990 | } |
| 1991 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1992 | done: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1993 | if (len > skb_headlen(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | skb->data_len -= skb->len - len; |
| 1995 | skb->len = len; |
| 1996 | } else { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1997 | skb->len = len; |
| 1998 | skb->data_len = 0; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1999 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
Eric Dumazet | c21b48c | 2017-04-26 09:07:46 -0700 | [diff] [blame] | 2002 | if (!skb->sk || skb->destructor == sock_edemux) |
| 2003 | skb_condense(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | return 0; |
| 2005 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2006 | EXPORT_SYMBOL(___pskb_trim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | |
Eric Dumazet | 88078d9 | 2018-04-18 11:43:15 -0700 | [diff] [blame] | 2008 | /* Note : use pskb_trim_rcsum() instead of calling this directly |
| 2009 | */ |
| 2010 | int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) |
| 2011 | { |
| 2012 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 2013 | int delta = skb->len - len; |
| 2014 | |
Dimitris Michailidis | d55bef50 | 2018-10-19 17:07:13 -0700 | [diff] [blame] | 2015 | skb->csum = csum_block_sub(skb->csum, |
| 2016 | skb_checksum(skb, len, delta, 0), |
| 2017 | len); |
Eric Dumazet | 88078d9 | 2018-04-18 11:43:15 -0700 | [diff] [blame] | 2018 | } |
| 2019 | return __pskb_trim(skb, len); |
| 2020 | } |
| 2021 | EXPORT_SYMBOL(pskb_trim_rcsum_slow); |
| 2022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | /** |
| 2024 | * __pskb_pull_tail - advance tail of skb header |
| 2025 | * @skb: buffer to reallocate |
| 2026 | * @delta: number of bytes to advance tail |
| 2027 | * |
| 2028 | * The function makes a sense only on a fragmented &sk_buff, |
| 2029 | * it expands header moving its tail forward and copying necessary |
| 2030 | * data from fragmented part. |
| 2031 | * |
| 2032 | * &sk_buff MUST have reference count of 1. |
| 2033 | * |
| 2034 | * Returns %NULL (and &sk_buff does not change) if pull failed |
| 2035 | * or value of new tail of skb in the case of success. |
| 2036 | * |
| 2037 | * All the pointers pointing into skb header may change and must be |
| 2038 | * reloaded after call to this function. |
| 2039 | */ |
| 2040 | |
| 2041 | /* Moves tail of skb head forward, copying data from fragmented part, |
| 2042 | * when it is necessary. |
| 2043 | * 1. It may fail due to malloc failure. |
| 2044 | * 2. It may change skb pointers. |
| 2045 | * |
| 2046 | * It is pretty complicated. Luckily, it is called only in exceptional cases. |
| 2047 | */ |
Johannes Berg | af72868 | 2017-06-16 14:29:22 +0200 | [diff] [blame] | 2048 | void *__pskb_pull_tail(struct sk_buff *skb, int delta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | { |
| 2050 | /* If skb has not enough free space at tail, get new one |
| 2051 | * plus 128 bytes for future expansions. If we have enough |
| 2052 | * room at tail, reallocate without expansion only if skb is cloned. |
| 2053 | */ |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 2054 | int i, k, eat = (skb->tail + delta) - skb->end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | |
| 2056 | if (eat > 0 || skb_cloned(skb)) { |
| 2057 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, |
| 2058 | GFP_ATOMIC)) |
| 2059 | return NULL; |
| 2060 | } |
| 2061 | |
Tim Hansen | 9f77fad | 2017-10-09 11:37:59 -0400 | [diff] [blame] | 2062 | BUG_ON(skb_copy_bits(skb, skb_headlen(skb), |
| 2063 | skb_tail_pointer(skb), delta)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | |
| 2065 | /* Optimization: no fragments, no reasons to preestimate |
| 2066 | * size of pulled pages. Superb. |
| 2067 | */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 2068 | if (!skb_has_frag_list(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | goto pull_pages; |
| 2070 | |
| 2071 | /* Estimate size of pulled pages. */ |
| 2072 | eat = delta; |
| 2073 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2074 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 2075 | |
| 2076 | if (size >= eat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | goto pull_pages; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2078 | eat -= size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | /* If we need update frag list, we are in troubles. |
Wenhua Shi | 09001b0 | 2017-10-14 18:51:36 +0200 | [diff] [blame] | 2082 | * Certainly, it is possible to add an offset to skb data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | * but taking into account that pulling is expected to |
| 2084 | * be very rare operation, it is worth to fight against |
| 2085 | * further bloating skb head and crucify ourselves here instead. |
| 2086 | * Pure masohism, indeed. 8)8) |
| 2087 | */ |
| 2088 | if (eat) { |
| 2089 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 2090 | struct sk_buff *clone = NULL; |
| 2091 | struct sk_buff *insp = NULL; |
| 2092 | |
| 2093 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | if (list->len <= eat) { |
| 2095 | /* Eaten as whole. */ |
| 2096 | eat -= list->len; |
| 2097 | list = list->next; |
| 2098 | insp = list; |
| 2099 | } else { |
| 2100 | /* Eaten partially. */ |
| 2101 | |
| 2102 | if (skb_shared(list)) { |
| 2103 | /* Sucks! We need to fork list. :-( */ |
| 2104 | clone = skb_clone(list, GFP_ATOMIC); |
| 2105 | if (!clone) |
| 2106 | return NULL; |
| 2107 | insp = list->next; |
| 2108 | list = clone; |
| 2109 | } else { |
| 2110 | /* This may be pulled without |
| 2111 | * problems. */ |
| 2112 | insp = list; |
| 2113 | } |
| 2114 | if (!pskb_pull(list, eat)) { |
Wei Yongjun | f3fbbe0 | 2009-02-25 00:37:32 +0000 | [diff] [blame] | 2115 | kfree_skb(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | return NULL; |
| 2117 | } |
| 2118 | break; |
| 2119 | } |
| 2120 | } while (eat); |
| 2121 | |
| 2122 | /* Free pulled out fragments. */ |
| 2123 | while ((list = skb_shinfo(skb)->frag_list) != insp) { |
| 2124 | skb_shinfo(skb)->frag_list = list->next; |
| 2125 | kfree_skb(list); |
| 2126 | } |
| 2127 | /* And insert new clone at head. */ |
| 2128 | if (clone) { |
| 2129 | clone->next = list; |
| 2130 | skb_shinfo(skb)->frag_list = clone; |
| 2131 | } |
| 2132 | } |
| 2133 | /* Success! Now we may commit changes to skb data. */ |
| 2134 | |
| 2135 | pull_pages: |
| 2136 | eat = delta; |
| 2137 | k = 0; |
| 2138 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2139 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 2140 | |
| 2141 | if (size <= eat) { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2142 | skb_frag_unref(skb, i); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2143 | eat -= size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | } else { |
| 2145 | skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 2146 | if (eat) { |
| 2147 | skb_shinfo(skb)->frags[k].page_offset += eat; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2148 | skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); |
linzhang | 3ccc6c6 | 2017-07-17 17:25:02 +0800 | [diff] [blame] | 2149 | if (!i) |
| 2150 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | eat = 0; |
| 2152 | } |
| 2153 | k++; |
| 2154 | } |
| 2155 | } |
| 2156 | skb_shinfo(skb)->nr_frags = k; |
| 2157 | |
linzhang | 3ccc6c6 | 2017-07-17 17:25:02 +0800 | [diff] [blame] | 2158 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | skb->tail += delta; |
| 2160 | skb->data_len -= delta; |
| 2161 | |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 2162 | if (!skb->data_len) |
| 2163 | skb_zcopy_clear(skb, false); |
| 2164 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 2165 | return skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2167 | EXPORT_SYMBOL(__pskb_pull_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | |
Eric Dumazet | 22019b1 | 2011-07-29 18:37:31 +0000 | [diff] [blame] | 2169 | /** |
| 2170 | * skb_copy_bits - copy bits from skb to kernel buffer |
| 2171 | * @skb: source skb |
| 2172 | * @offset: offset in source |
| 2173 | * @to: destination buffer |
| 2174 | * @len: number of bytes to copy |
| 2175 | * |
| 2176 | * Copy the specified number of bytes from the source skb to the |
| 2177 | * destination buffer. |
| 2178 | * |
| 2179 | * CAUTION ! : |
| 2180 | * If its prototype is ever changed, |
| 2181 | * check arch/{*}/net/{*}.S files, |
| 2182 | * since it is called from BPF assembly code. |
| 2183 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) |
| 2185 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2186 | int start = skb_headlen(skb); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2187 | struct sk_buff *frag_iter; |
| 2188 | int i, copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | |
| 2190 | if (offset > (int)skb->len - len) |
| 2191 | goto fault; |
| 2192 | |
| 2193 | /* Copy header. */ |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2194 | if ((copy = start - offset) > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | if (copy > len) |
| 2196 | copy = len; |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2197 | skb_copy_from_linear_data_offset(skb, offset, to, copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | if ((len -= copy) == 0) |
| 2199 | return 0; |
| 2200 | offset += copy; |
| 2201 | to += copy; |
| 2202 | } |
| 2203 | |
| 2204 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2205 | int end; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2206 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2208 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2209 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2210 | end = start + skb_frag_size(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | if ((copy = end - offset) > 0) { |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2212 | u32 p_off, p_len, copied; |
| 2213 | struct page *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | u8 *vaddr; |
| 2215 | |
| 2216 | if (copy > len) |
| 2217 | copy = len; |
| 2218 | |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2219 | skb_frag_foreach_page(f, |
| 2220 | f->page_offset + offset - start, |
| 2221 | copy, p, p_off, p_len, copied) { |
| 2222 | vaddr = kmap_atomic(p); |
| 2223 | memcpy(to + copied, vaddr + p_off, p_len); |
| 2224 | kunmap_atomic(vaddr); |
| 2225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | |
| 2227 | if ((len -= copy) == 0) |
| 2228 | return 0; |
| 2229 | offset += copy; |
| 2230 | to += copy; |
| 2231 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2232 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2235 | skb_walk_frags(skb, frag_iter) { |
| 2236 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2238 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2240 | end = start + frag_iter->len; |
| 2241 | if ((copy = end - offset) > 0) { |
| 2242 | if (copy > len) |
| 2243 | copy = len; |
| 2244 | if (skb_copy_bits(frag_iter, offset - start, to, copy)) |
| 2245 | goto fault; |
| 2246 | if ((len -= copy) == 0) |
| 2247 | return 0; |
| 2248 | offset += copy; |
| 2249 | to += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2251 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | } |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 2253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | if (!len) |
| 2255 | return 0; |
| 2256 | |
| 2257 | fault: |
| 2258 | return -EFAULT; |
| 2259 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2260 | EXPORT_SYMBOL(skb_copy_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2262 | /* |
| 2263 | * Callback from splice_to_pipe(), if we need to release some pages |
| 2264 | * at the end of the spd in case we error'ed out in filling the pipe. |
| 2265 | */ |
| 2266 | static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) |
| 2267 | { |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2268 | put_page(spd->pages[i]); |
| 2269 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2270 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2271 | static struct page *linear_to_page(struct page *page, unsigned int *len, |
| 2272 | unsigned int *offset, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2273 | struct sock *sk) |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2274 | { |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 2275 | struct page_frag *pfrag = sk_page_frag(sk); |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2276 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 2277 | if (!sk_page_frag_refill(sk, pfrag)) |
| 2278 | return NULL; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 2279 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 2280 | *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 2281 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 2282 | memcpy(page_address(pfrag->page) + pfrag->offset, |
| 2283 | page_address(page) + *offset, *len); |
| 2284 | *offset = pfrag->offset; |
| 2285 | pfrag->offset += *len; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 2286 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 2287 | return pfrag->page; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2288 | } |
| 2289 | |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 2290 | static bool spd_can_coalesce(const struct splice_pipe_desc *spd, |
| 2291 | struct page *page, |
| 2292 | unsigned int offset) |
| 2293 | { |
| 2294 | return spd->nr_pages && |
| 2295 | spd->pages[spd->nr_pages - 1] == page && |
| 2296 | (spd->partial[spd->nr_pages - 1].offset + |
| 2297 | spd->partial[spd->nr_pages - 1].len == offset); |
| 2298 | } |
| 2299 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2300 | /* |
| 2301 | * Fill page/offset/length into spd, if it can hold more pages. |
| 2302 | */ |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2303 | static bool spd_fill_page(struct splice_pipe_desc *spd, |
| 2304 | struct pipe_inode_info *pipe, struct page *page, |
| 2305 | unsigned int *len, unsigned int offset, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2306 | bool linear, |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2307 | struct sock *sk) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2308 | { |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 2309 | if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2310 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2311 | |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2312 | if (linear) { |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2313 | page = linear_to_page(page, len, &offset, sk); |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2314 | if (!page) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2315 | return true; |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 2316 | } |
| 2317 | if (spd_can_coalesce(spd, page, offset)) { |
| 2318 | spd->partial[spd->nr_pages - 1].len += *len; |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2319 | return false; |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 2320 | } |
| 2321 | get_page(page); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2322 | spd->pages[spd->nr_pages] = page; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 2323 | spd->partial[spd->nr_pages].len = *len; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2324 | spd->partial[spd->nr_pages].offset = offset; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2325 | spd->nr_pages++; |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2326 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2327 | return false; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2328 | } |
| 2329 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2330 | static bool __splice_segment(struct page *page, unsigned int poff, |
| 2331 | unsigned int plen, unsigned int *off, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2332 | unsigned int *len, |
Eric Dumazet | d7ccf7c | 2012-04-23 23:35:04 -0400 | [diff] [blame] | 2333 | struct splice_pipe_desc *spd, bool linear, |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2334 | struct sock *sk, |
| 2335 | struct pipe_inode_info *pipe) |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2336 | { |
| 2337 | if (!*len) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2338 | return true; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2339 | |
| 2340 | /* skip this segment if already processed */ |
| 2341 | if (*off >= plen) { |
| 2342 | *off -= plen; |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2343 | return false; |
Octavian Purdila | db43a28 | 2008-06-27 17:27:21 -0700 | [diff] [blame] | 2344 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2345 | |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2346 | /* ignore any bits we already processed */ |
Eric Dumazet | 9ca1b22 | 2013-01-05 21:31:18 +0000 | [diff] [blame] | 2347 | poff += *off; |
| 2348 | plen -= *off; |
| 2349 | *off = 0; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2350 | |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2351 | do { |
| 2352 | unsigned int flen = min(*len, plen); |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2353 | |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2354 | if (spd_fill_page(spd, pipe, page, &flen, poff, |
| 2355 | linear, sk)) |
| 2356 | return true; |
| 2357 | poff += flen; |
| 2358 | plen -= flen; |
| 2359 | *len -= flen; |
| 2360 | } while (*len && plen); |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2361 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2362 | return false; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | /* |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2366 | * 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] | 2367 | * pipe is full or if we already spliced the requested length. |
| 2368 | */ |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2369 | static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, |
| 2370 | unsigned int *offset, unsigned int *len, |
| 2371 | struct splice_pipe_desc *spd, struct sock *sk) |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2372 | { |
| 2373 | int seg; |
Tom Herbert | fa9835e | 2016-03-07 14:11:04 -0800 | [diff] [blame] | 2374 | struct sk_buff *iter; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2375 | |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 2376 | /* map the linear part : |
Alexander Duyck | 2996d31 | 2012-05-02 18:18:42 +0000 | [diff] [blame] | 2377 | * If skb->head_frag is set, this 'linear' part is backed by a |
| 2378 | * fragment, and if the head is not shared with any clones then |
| 2379 | * 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] | 2380 | */ |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2381 | if (__splice_segment(virt_to_page(skb->data), |
| 2382 | (unsigned long) skb->data & (PAGE_SIZE - 1), |
| 2383 | skb_headlen(skb), |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2384 | offset, len, spd, |
Alexander Duyck | 3a7c1ee4 | 2012-05-03 01:09:42 +0000 | [diff] [blame] | 2385 | skb_head_is_locked(skb), |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 2386 | sk, pipe)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2387 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2388 | |
| 2389 | /* |
| 2390 | * then map the fragments |
| 2391 | */ |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2392 | for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { |
| 2393 | const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; |
| 2394 | |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2395 | if (__splice_segment(skb_frag_page(f), |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2396 | f->page_offset, skb_frag_size(f), |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2397 | offset, len, spd, false, sk, pipe)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2398 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2399 | } |
| 2400 | |
Tom Herbert | fa9835e | 2016-03-07 14:11:04 -0800 | [diff] [blame] | 2401 | skb_walk_frags(skb, iter) { |
| 2402 | if (*offset >= iter->len) { |
| 2403 | *offset -= iter->len; |
| 2404 | continue; |
| 2405 | } |
| 2406 | /* __skb_splice_bits() only fails if the output has no room |
| 2407 | * left, so no point in going over the frag_list for the error |
| 2408 | * case. |
| 2409 | */ |
| 2410 | if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) |
| 2411 | return true; |
| 2412 | } |
| 2413 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2414 | return false; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | /* |
| 2418 | * Map data from the skb to a pipe. Should handle both the linear part, |
Tom Herbert | fa9835e | 2016-03-07 14:11:04 -0800 | [diff] [blame] | 2419 | * the fragments, and the frag list. |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2420 | */ |
Hannes Frederic Sowa | a60e3cc | 2015-05-21 17:00:00 +0200 | [diff] [blame] | 2421 | 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] | 2422 | struct pipe_inode_info *pipe, unsigned int tlen, |
Al Viro | 2586926 | 2016-09-17 21:02:10 -0400 | [diff] [blame] | 2423 | unsigned int flags) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2424 | { |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 2425 | struct partial_page partial[MAX_SKB_FRAGS]; |
| 2426 | struct page *pages[MAX_SKB_FRAGS]; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2427 | struct splice_pipe_desc spd = { |
| 2428 | .pages = pages, |
| 2429 | .partial = partial, |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 2430 | .nr_pages_max = MAX_SKB_FRAGS, |
Miklos Szeredi | 28a625c | 2014-01-22 19:36:57 +0100 | [diff] [blame] | 2431 | .ops = &nosteal_pipe_buf_ops, |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2432 | .spd_release = sock_spd_release, |
| 2433 | }; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 2434 | int ret = 0; |
| 2435 | |
Tom Herbert | fa9835e | 2016-03-07 14:11:04 -0800 | [diff] [blame] | 2436 | __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2437 | |
Hannes Frederic Sowa | a60e3cc | 2015-05-21 17:00:00 +0200 | [diff] [blame] | 2438 | if (spd.nr_pages) |
Al Viro | 2586926 | 2016-09-17 21:02:10 -0400 | [diff] [blame] | 2439 | ret = splice_to_pipe(pipe, &spd); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2440 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 2441 | return ret; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2442 | } |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2443 | EXPORT_SYMBOL_GPL(skb_splice_bits); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2444 | |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2445 | /* Send skb data on a socket. Socket must be locked. */ |
| 2446 | int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, |
| 2447 | int len) |
| 2448 | { |
| 2449 | unsigned int orig_len = len; |
| 2450 | struct sk_buff *head = skb; |
| 2451 | unsigned short fragidx; |
| 2452 | int slen, ret; |
| 2453 | |
| 2454 | do_frag_list: |
| 2455 | |
| 2456 | /* Deal with head data */ |
| 2457 | while (offset < skb_headlen(skb) && len) { |
| 2458 | struct kvec kv; |
| 2459 | struct msghdr msg; |
| 2460 | |
| 2461 | slen = min_t(int, len, skb_headlen(skb) - offset); |
| 2462 | kv.iov_base = skb->data + offset; |
John Fastabend | db5980d | 2017-08-15 22:31:34 -0700 | [diff] [blame] | 2463 | kv.iov_len = slen; |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2464 | memset(&msg, 0, sizeof(msg)); |
John Fastabend | bd95e678 | 2019-05-24 08:01:00 -0700 | [diff] [blame] | 2465 | msg.msg_flags = MSG_DONTWAIT; |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2466 | |
| 2467 | ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen); |
| 2468 | if (ret <= 0) |
| 2469 | goto error; |
| 2470 | |
| 2471 | offset += ret; |
| 2472 | len -= ret; |
| 2473 | } |
| 2474 | |
| 2475 | /* All the data was skb head? */ |
| 2476 | if (!len) |
| 2477 | goto out; |
| 2478 | |
| 2479 | /* Make offset relative to start of frags */ |
| 2480 | offset -= skb_headlen(skb); |
| 2481 | |
| 2482 | /* Find where we are in frag list */ |
| 2483 | for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { |
| 2484 | skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; |
| 2485 | |
| 2486 | if (offset < frag->size) |
| 2487 | break; |
| 2488 | |
| 2489 | offset -= frag->size; |
| 2490 | } |
| 2491 | |
| 2492 | for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { |
| 2493 | skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; |
| 2494 | |
| 2495 | slen = min_t(size_t, len, frag->size - offset); |
| 2496 | |
| 2497 | while (slen) { |
| 2498 | ret = kernel_sendpage_locked(sk, frag->page.p, |
| 2499 | frag->page_offset + offset, |
| 2500 | slen, MSG_DONTWAIT); |
| 2501 | if (ret <= 0) |
| 2502 | goto error; |
| 2503 | |
| 2504 | len -= ret; |
| 2505 | offset += ret; |
| 2506 | slen -= ret; |
| 2507 | } |
| 2508 | |
| 2509 | offset = 0; |
| 2510 | } |
| 2511 | |
| 2512 | if (len) { |
| 2513 | /* Process any frag lists */ |
| 2514 | |
| 2515 | if (skb == head) { |
| 2516 | if (skb_has_frag_list(skb)) { |
| 2517 | skb = skb_shinfo(skb)->frag_list; |
| 2518 | goto do_frag_list; |
| 2519 | } |
| 2520 | } else if (skb->next) { |
| 2521 | skb = skb->next; |
| 2522 | goto do_frag_list; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | out: |
| 2527 | return orig_len - len; |
| 2528 | |
| 2529 | error: |
| 2530 | return orig_len == len ? ret : orig_len - len; |
| 2531 | } |
| 2532 | EXPORT_SYMBOL_GPL(skb_send_sock_locked); |
| 2533 | |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2534 | /** |
| 2535 | * skb_store_bits - store bits from kernel buffer to skb |
| 2536 | * @skb: destination buffer |
| 2537 | * @offset: offset in destination |
| 2538 | * @from: source buffer |
| 2539 | * @len: number of bytes to copy |
| 2540 | * |
| 2541 | * Copy the specified number of bytes from the source buffer to the |
| 2542 | * destination skb. This function handles all the messy bits of |
| 2543 | * traversing fragment lists and such. |
| 2544 | */ |
| 2545 | |
Stephen Hemminger | 0c6fcc8 | 2007-04-20 16:40:01 -0700 | [diff] [blame] | 2546 | 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] | 2547 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2548 | int start = skb_headlen(skb); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2549 | struct sk_buff *frag_iter; |
| 2550 | int i, copy; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2551 | |
| 2552 | if (offset > (int)skb->len - len) |
| 2553 | goto fault; |
| 2554 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2555 | if ((copy = start - offset) > 0) { |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2556 | if (copy > len) |
| 2557 | copy = len; |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 2558 | skb_copy_to_linear_data_offset(skb, offset, from, copy); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2559 | if ((len -= copy) == 0) |
| 2560 | return 0; |
| 2561 | offset += copy; |
| 2562 | from += copy; |
| 2563 | } |
| 2564 | |
| 2565 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 2566 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2567 | int end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2568 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2569 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2570 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2571 | end = start + skb_frag_size(frag); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2572 | if ((copy = end - offset) > 0) { |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2573 | u32 p_off, p_len, copied; |
| 2574 | struct page *p; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2575 | u8 *vaddr; |
| 2576 | |
| 2577 | if (copy > len) |
| 2578 | copy = len; |
| 2579 | |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2580 | skb_frag_foreach_page(frag, |
| 2581 | frag->page_offset + offset - start, |
| 2582 | copy, p, p_off, p_len, copied) { |
| 2583 | vaddr = kmap_atomic(p); |
| 2584 | memcpy(vaddr + p_off, from + copied, p_len); |
| 2585 | kunmap_atomic(vaddr); |
| 2586 | } |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2587 | |
| 2588 | if ((len -= copy) == 0) |
| 2589 | return 0; |
| 2590 | offset += copy; |
| 2591 | from += copy; |
| 2592 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2593 | start = end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2596 | skb_walk_frags(skb, frag_iter) { |
| 2597 | int end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2598 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2599 | WARN_ON(start > offset + len); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2600 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2601 | end = start + frag_iter->len; |
| 2602 | if ((copy = end - offset) > 0) { |
| 2603 | if (copy > len) |
| 2604 | copy = len; |
| 2605 | if (skb_store_bits(frag_iter, offset - start, |
| 2606 | from, copy)) |
| 2607 | goto fault; |
| 2608 | if ((len -= copy) == 0) |
| 2609 | return 0; |
| 2610 | offset += copy; |
| 2611 | from += copy; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2612 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2613 | start = end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2614 | } |
| 2615 | if (!len) |
| 2616 | return 0; |
| 2617 | |
| 2618 | fault: |
| 2619 | return -EFAULT; |
| 2620 | } |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2621 | EXPORT_SYMBOL(skb_store_bits); |
| 2622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | /* Checksum skb data. */ |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2624 | __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, |
| 2625 | __wsum csum, const struct skb_checksum_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2627 | int start = skb_headlen(skb); |
| 2628 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2629 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | int pos = 0; |
| 2631 | |
| 2632 | /* Checksum header. */ |
| 2633 | if (copy > 0) { |
| 2634 | if (copy > len) |
| 2635 | copy = len; |
Matteo Croce | 2544af0 | 2019-05-29 17:13:48 +0200 | [diff] [blame] | 2636 | csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, |
| 2637 | skb->data + offset, copy, csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | if ((len -= copy) == 0) |
| 2639 | return csum; |
| 2640 | offset += copy; |
| 2641 | pos = copy; |
| 2642 | } |
| 2643 | |
| 2644 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2645 | int end; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2646 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2648 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2649 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2650 | end = start + skb_frag_size(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | if ((copy = end - offset) > 0) { |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2652 | u32 p_off, p_len, copied; |
| 2653 | struct page *p; |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 2654 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | |
| 2657 | if (copy > len) |
| 2658 | copy = len; |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2659 | |
| 2660 | skb_frag_foreach_page(frag, |
| 2661 | frag->page_offset + offset - start, |
| 2662 | copy, p, p_off, p_len, copied) { |
| 2663 | vaddr = kmap_atomic(p); |
Matteo Croce | 2544af0 | 2019-05-29 17:13:48 +0200 | [diff] [blame] | 2664 | csum2 = INDIRECT_CALL_1(ops->update, |
| 2665 | csum_partial_ext, |
| 2666 | vaddr + p_off, p_len, 0); |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2667 | kunmap_atomic(vaddr); |
Matteo Croce | 2544af0 | 2019-05-29 17:13:48 +0200 | [diff] [blame] | 2668 | csum = INDIRECT_CALL_1(ops->combine, |
| 2669 | csum_block_add_ext, csum, |
| 2670 | csum2, pos, p_len); |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2671 | pos += p_len; |
| 2672 | } |
| 2673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2674 | if (!(len -= copy)) |
| 2675 | return csum; |
| 2676 | offset += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2678 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | } |
| 2680 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2681 | skb_walk_frags(skb, frag_iter) { |
| 2682 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2684 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2686 | end = start + frag_iter->len; |
| 2687 | if ((copy = end - offset) > 0) { |
| 2688 | __wsum csum2; |
| 2689 | if (copy > len) |
| 2690 | copy = len; |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2691 | csum2 = __skb_checksum(frag_iter, offset - start, |
| 2692 | copy, 0, ops); |
Matteo Croce | 2544af0 | 2019-05-29 17:13:48 +0200 | [diff] [blame] | 2693 | csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, |
| 2694 | csum, csum2, pos, copy); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2695 | if ((len -= copy) == 0) |
| 2696 | return csum; |
| 2697 | offset += copy; |
| 2698 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2700 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2702 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | |
| 2704 | return csum; |
| 2705 | } |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2706 | EXPORT_SYMBOL(__skb_checksum); |
| 2707 | |
| 2708 | __wsum skb_checksum(const struct sk_buff *skb, int offset, |
| 2709 | int len, __wsum csum) |
| 2710 | { |
| 2711 | const struct skb_checksum_ops ops = { |
Daniel Borkmann | cea80ea | 2013-11-04 17:10:25 +0100 | [diff] [blame] | 2712 | .update = csum_partial_ext, |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2713 | .combine = csum_block_add_ext, |
| 2714 | }; |
| 2715 | |
| 2716 | return __skb_checksum(skb, offset, len, csum, &ops); |
| 2717 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2718 | EXPORT_SYMBOL(skb_checksum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | |
| 2720 | /* Both of above in one bottle. */ |
| 2721 | |
Al Viro | 81d7766 | 2006-11-14 21:37:33 -0800 | [diff] [blame] | 2722 | __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, |
| 2723 | u8 *to, int len, __wsum csum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2725 | int start = skb_headlen(skb); |
| 2726 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2727 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | int pos = 0; |
| 2729 | |
| 2730 | /* Copy header. */ |
| 2731 | if (copy > 0) { |
| 2732 | if (copy > len) |
| 2733 | copy = len; |
| 2734 | csum = csum_partial_copy_nocheck(skb->data + offset, to, |
| 2735 | copy, csum); |
| 2736 | if ((len -= copy) == 0) |
| 2737 | return csum; |
| 2738 | offset += copy; |
| 2739 | to += copy; |
| 2740 | pos = copy; |
| 2741 | } |
| 2742 | |
| 2743 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2744 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2746 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2747 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2748 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | if ((copy = end - offset) > 0) { |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2750 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 2751 | u32 p_off, p_len, copied; |
| 2752 | struct page *p; |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 2753 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | |
| 2756 | if (copy > len) |
| 2757 | copy = len; |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2758 | |
| 2759 | skb_frag_foreach_page(frag, |
| 2760 | frag->page_offset + offset - start, |
| 2761 | copy, p, p_off, p_len, copied) { |
| 2762 | vaddr = kmap_atomic(p); |
| 2763 | csum2 = csum_partial_copy_nocheck(vaddr + p_off, |
| 2764 | to + copied, |
| 2765 | p_len, 0); |
| 2766 | kunmap_atomic(vaddr); |
| 2767 | csum = csum_block_add(csum, csum2, pos); |
| 2768 | pos += p_len; |
| 2769 | } |
| 2770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | if (!(len -= copy)) |
| 2772 | return csum; |
| 2773 | offset += copy; |
| 2774 | to += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2776 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | } |
| 2778 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2779 | skb_walk_frags(skb, frag_iter) { |
| 2780 | __wsum csum2; |
| 2781 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2783 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2785 | end = start + frag_iter->len; |
| 2786 | if ((copy = end - offset) > 0) { |
| 2787 | if (copy > len) |
| 2788 | copy = len; |
| 2789 | csum2 = skb_copy_and_csum_bits(frag_iter, |
| 2790 | offset - start, |
| 2791 | to, copy, 0); |
| 2792 | csum = csum_block_add(csum, csum2, pos); |
| 2793 | if ((len -= copy) == 0) |
| 2794 | return csum; |
| 2795 | offset += copy; |
| 2796 | to += copy; |
| 2797 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2799 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2801 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | return csum; |
| 2803 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2804 | EXPORT_SYMBOL(skb_copy_and_csum_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2806 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) |
| 2807 | { |
| 2808 | __sum16 sum; |
| 2809 | |
| 2810 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); |
Cong Wang | 1464193 | 2018-11-26 09:31:26 -0800 | [diff] [blame] | 2811 | /* See comments in __skb_checksum_complete(). */ |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2812 | if (likely(!sum)) { |
| 2813 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && |
| 2814 | !skb->csum_complete_sw) |
Cong Wang | 7fe50ac | 2018-11-12 14:47:18 -0800 | [diff] [blame] | 2815 | netdev_rx_csum_fault(skb->dev, skb); |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2816 | } |
| 2817 | if (!skb_shared(skb)) |
| 2818 | skb->csum_valid = !sum; |
| 2819 | return sum; |
| 2820 | } |
| 2821 | EXPORT_SYMBOL(__skb_checksum_complete_head); |
| 2822 | |
Cong Wang | 1464193 | 2018-11-26 09:31:26 -0800 | [diff] [blame] | 2823 | /* This function assumes skb->csum already holds pseudo header's checksum, |
| 2824 | * which has been changed from the hardware checksum, for example, by |
| 2825 | * __skb_checksum_validate_complete(). And, the original skb->csum must |
| 2826 | * have been validated unsuccessfully for CHECKSUM_COMPLETE case. |
| 2827 | * |
| 2828 | * It returns non-zero if the recomputed checksum is still invalid, otherwise |
| 2829 | * zero. The new checksum is stored back into skb->csum unless the skb is |
| 2830 | * shared. |
| 2831 | */ |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2832 | __sum16 __skb_checksum_complete(struct sk_buff *skb) |
| 2833 | { |
| 2834 | __wsum csum; |
| 2835 | __sum16 sum; |
| 2836 | |
| 2837 | csum = skb_checksum(skb, 0, skb->len, 0); |
| 2838 | |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2839 | sum = csum_fold(csum_add(skb->csum, csum)); |
Cong Wang | 1464193 | 2018-11-26 09:31:26 -0800 | [diff] [blame] | 2840 | /* This check is inverted, because we already knew the hardware |
| 2841 | * checksum is invalid before calling this function. So, if the |
| 2842 | * re-computed checksum is valid instead, then we have a mismatch |
| 2843 | * between the original skb->csum and skb_checksum(). This means either |
| 2844 | * the original hardware checksum is incorrect or we screw up skb->csum |
| 2845 | * when moving skb->data around. |
| 2846 | */ |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2847 | if (likely(!sum)) { |
| 2848 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && |
| 2849 | !skb->csum_complete_sw) |
Cong Wang | 7fe50ac | 2018-11-12 14:47:18 -0800 | [diff] [blame] | 2850 | netdev_rx_csum_fault(skb->dev, skb); |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2851 | } |
| 2852 | |
| 2853 | if (!skb_shared(skb)) { |
| 2854 | /* Save full packet checksum */ |
| 2855 | skb->csum = csum; |
| 2856 | skb->ip_summed = CHECKSUM_COMPLETE; |
| 2857 | skb->csum_complete_sw = 1; |
| 2858 | skb->csum_valid = !sum; |
| 2859 | } |
| 2860 | |
| 2861 | return sum; |
| 2862 | } |
| 2863 | EXPORT_SYMBOL(__skb_checksum_complete); |
| 2864 | |
Davide Caratti | 9617813 | 2017-05-18 15:44:37 +0200 | [diff] [blame] | 2865 | static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) |
| 2866 | { |
| 2867 | net_warn_ratelimited( |
| 2868 | "%s: attempt to compute crc32c without libcrc32c.ko\n", |
| 2869 | __func__); |
| 2870 | return 0; |
| 2871 | } |
| 2872 | |
| 2873 | static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, |
| 2874 | int offset, int len) |
| 2875 | { |
| 2876 | net_warn_ratelimited( |
| 2877 | "%s: attempt to compute crc32c without libcrc32c.ko\n", |
| 2878 | __func__); |
| 2879 | return 0; |
| 2880 | } |
| 2881 | |
| 2882 | static const struct skb_checksum_ops default_crc32c_ops = { |
| 2883 | .update = warn_crc32c_csum_update, |
| 2884 | .combine = warn_crc32c_csum_combine, |
| 2885 | }; |
| 2886 | |
| 2887 | const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = |
| 2888 | &default_crc32c_ops; |
| 2889 | EXPORT_SYMBOL(crc32c_csum_stub); |
| 2890 | |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2891 | /** |
| 2892 | * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() |
| 2893 | * @from: source buffer |
| 2894 | * |
| 2895 | * Calculates the amount of linear headroom needed in the 'to' skb passed |
| 2896 | * into skb_zerocopy(). |
| 2897 | */ |
| 2898 | unsigned int |
| 2899 | skb_zerocopy_headlen(const struct sk_buff *from) |
| 2900 | { |
| 2901 | unsigned int hlen = 0; |
| 2902 | |
| 2903 | if (!from->head_frag || |
| 2904 | skb_headlen(from) < L1_CACHE_BYTES || |
| 2905 | skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) |
| 2906 | hlen = skb_headlen(from); |
| 2907 | |
| 2908 | if (skb_has_frag_list(from)) |
| 2909 | hlen = from->len; |
| 2910 | |
| 2911 | return hlen; |
| 2912 | } |
| 2913 | EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); |
| 2914 | |
| 2915 | /** |
| 2916 | * skb_zerocopy - Zero copy skb to skb |
| 2917 | * @to: destination buffer |
Masanari Iida | 7fceb4d | 2014-01-29 01:05:28 +0900 | [diff] [blame] | 2918 | * @from: source buffer |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2919 | * @len: number of bytes to copy from source buffer |
| 2920 | * @hlen: size of linear headroom in destination buffer |
| 2921 | * |
| 2922 | * Copies up to `len` bytes from `from` to `to` by creating references |
| 2923 | * to the frags in the source buffer. |
| 2924 | * |
| 2925 | * The `hlen` as calculated by skb_zerocopy_headlen() specifies the |
| 2926 | * headroom in the `to` buffer. |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2927 | * |
| 2928 | * Return value: |
| 2929 | * 0: everything is OK |
| 2930 | * -ENOMEM: couldn't orphan frags of @from due to lack of memory |
| 2931 | * -EFAULT: skb_copy_bits() found some problem with skb geometry |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2932 | */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2933 | int |
| 2934 | 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] | 2935 | { |
| 2936 | int i, j = 0; |
| 2937 | int plen = 0; /* length of skb->head fragment */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2938 | int ret; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2939 | struct page *page; |
| 2940 | unsigned int offset; |
| 2941 | |
| 2942 | BUG_ON(!from->head_frag && !hlen); |
| 2943 | |
| 2944 | /* dont bother with small payloads */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2945 | if (len <= skb_tailroom(to)) |
| 2946 | return skb_copy_bits(from, 0, skb_put(to, len), len); |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2947 | |
| 2948 | if (hlen) { |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2949 | ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); |
| 2950 | if (unlikely(ret)) |
| 2951 | return ret; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2952 | len -= hlen; |
| 2953 | } else { |
| 2954 | plen = min_t(int, skb_headlen(from), len); |
| 2955 | if (plen) { |
| 2956 | page = virt_to_head_page(from->head); |
| 2957 | offset = from->data - (unsigned char *)page_address(page); |
| 2958 | __skb_fill_page_desc(to, 0, page, offset, plen); |
| 2959 | get_page(page); |
| 2960 | j = 1; |
| 2961 | len -= plen; |
| 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | to->truesize += len + plen; |
| 2966 | to->len += len + plen; |
| 2967 | to->data_len += len + plen; |
| 2968 | |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2969 | if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { |
| 2970 | skb_tx_error(from); |
| 2971 | return -ENOMEM; |
| 2972 | } |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 2973 | skb_zerocopy_clone(to, from, GFP_ATOMIC); |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2974 | |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2975 | for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { |
| 2976 | if (!len) |
| 2977 | break; |
| 2978 | skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; |
| 2979 | skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); |
| 2980 | len -= skb_shinfo(to)->frags[j].size; |
| 2981 | skb_frag_ref(to, j); |
| 2982 | j++; |
| 2983 | } |
| 2984 | skb_shinfo(to)->nr_frags = j; |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2985 | |
| 2986 | return 0; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2987 | } |
| 2988 | EXPORT_SYMBOL_GPL(skb_zerocopy); |
| 2989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) |
| 2991 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 2992 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | long csstart; |
| 2994 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2995 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
Michał Mirosław | 55508d6 | 2010-12-14 15:24:08 +0000 | [diff] [blame] | 2996 | csstart = skb_checksum_start_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | else |
| 2998 | csstart = skb_headlen(skb); |
| 2999 | |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 3000 | BUG_ON(csstart > skb_headlen(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 3002 | skb_copy_from_linear_data(skb, to, csstart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | |
| 3004 | csum = 0; |
| 3005 | if (csstart != skb->len) |
| 3006 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, |
| 3007 | skb->len - csstart, 0); |
| 3008 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 3009 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Al Viro | ff1dcad | 2006-11-20 18:07:29 -0800 | [diff] [blame] | 3010 | long csstuff = csstart + skb->csum_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 3012 | *((__sum16 *)(to + csstuff)) = csum_fold(csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | } |
| 3014 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3015 | EXPORT_SYMBOL(skb_copy_and_csum_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | |
| 3017 | /** |
| 3018 | * skb_dequeue - remove from the head of the queue |
| 3019 | * @list: list to dequeue from |
| 3020 | * |
| 3021 | * Remove the head of the list. The list lock is taken so the function |
| 3022 | * may be used safely with other locking list functions. The head item is |
| 3023 | * returned or %NULL if the list is empty. |
| 3024 | */ |
| 3025 | |
| 3026 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) |
| 3027 | { |
| 3028 | unsigned long flags; |
| 3029 | struct sk_buff *result; |
| 3030 | |
| 3031 | spin_lock_irqsave(&list->lock, flags); |
| 3032 | result = __skb_dequeue(list); |
| 3033 | spin_unlock_irqrestore(&list->lock, flags); |
| 3034 | return result; |
| 3035 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3036 | EXPORT_SYMBOL(skb_dequeue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | |
| 3038 | /** |
| 3039 | * skb_dequeue_tail - remove from the tail of the queue |
| 3040 | * @list: list to dequeue from |
| 3041 | * |
| 3042 | * Remove the tail of the list. The list lock is taken so the function |
| 3043 | * may be used safely with other locking list functions. The tail item is |
| 3044 | * returned or %NULL if the list is empty. |
| 3045 | */ |
| 3046 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) |
| 3047 | { |
| 3048 | unsigned long flags; |
| 3049 | struct sk_buff *result; |
| 3050 | |
| 3051 | spin_lock_irqsave(&list->lock, flags); |
| 3052 | result = __skb_dequeue_tail(list); |
| 3053 | spin_unlock_irqrestore(&list->lock, flags); |
| 3054 | return result; |
| 3055 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3056 | EXPORT_SYMBOL(skb_dequeue_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | |
| 3058 | /** |
| 3059 | * skb_queue_purge - empty a list |
| 3060 | * @list: list to empty |
| 3061 | * |
| 3062 | * Delete all buffers on an &sk_buff list. Each buffer is removed from |
| 3063 | * the list and one reference dropped. This function takes the list |
| 3064 | * lock and is atomic with respect to other list locking functions. |
| 3065 | */ |
| 3066 | void skb_queue_purge(struct sk_buff_head *list) |
| 3067 | { |
| 3068 | struct sk_buff *skb; |
| 3069 | while ((skb = skb_dequeue(list)) != NULL) |
| 3070 | kfree_skb(skb); |
| 3071 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3072 | EXPORT_SYMBOL(skb_queue_purge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | |
| 3074 | /** |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3075 | * skb_rbtree_purge - empty a skb rbtree |
| 3076 | * @root: root of the rbtree to empty |
Peter Oskolkov | 385114d | 2018-08-02 23:34:38 +0000 | [diff] [blame] | 3077 | * Return value: the sum of truesizes of all purged skbs. |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3078 | * |
| 3079 | * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from |
| 3080 | * the list and one reference dropped. This function does not take |
| 3081 | * any lock. Synchronization should be handled by the caller (e.g., TCP |
| 3082 | * out-of-order queue is protected by the socket lock). |
| 3083 | */ |
Peter Oskolkov | 385114d | 2018-08-02 23:34:38 +0000 | [diff] [blame] | 3084 | unsigned int skb_rbtree_purge(struct rb_root *root) |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3085 | { |
Eric Dumazet | 7c90584 | 2017-09-23 12:39:12 -0700 | [diff] [blame] | 3086 | struct rb_node *p = rb_first(root); |
Peter Oskolkov | 385114d | 2018-08-02 23:34:38 +0000 | [diff] [blame] | 3087 | unsigned int sum = 0; |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3088 | |
Eric Dumazet | 7c90584 | 2017-09-23 12:39:12 -0700 | [diff] [blame] | 3089 | while (p) { |
| 3090 | struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); |
| 3091 | |
| 3092 | p = rb_next(p); |
| 3093 | rb_erase(&skb->rbnode, root); |
Peter Oskolkov | 385114d | 2018-08-02 23:34:38 +0000 | [diff] [blame] | 3094 | sum += skb->truesize; |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3095 | kfree_skb(skb); |
Eric Dumazet | 7c90584 | 2017-09-23 12:39:12 -0700 | [diff] [blame] | 3096 | } |
Peter Oskolkov | 385114d | 2018-08-02 23:34:38 +0000 | [diff] [blame] | 3097 | return sum; |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | * skb_queue_head - queue a buffer at the list head |
| 3102 | * @list: list to use |
| 3103 | * @newsk: buffer to queue |
| 3104 | * |
| 3105 | * Queue a buffer at the start of the list. This function takes the |
| 3106 | * list lock and can be used safely with other locking &sk_buff functions |
| 3107 | * safely. |
| 3108 | * |
| 3109 | * A buffer cannot be placed on two lists at the same time. |
| 3110 | */ |
| 3111 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) |
| 3112 | { |
| 3113 | unsigned long flags; |
| 3114 | |
| 3115 | spin_lock_irqsave(&list->lock, flags); |
| 3116 | __skb_queue_head(list, newsk); |
| 3117 | spin_unlock_irqrestore(&list->lock, flags); |
| 3118 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3119 | EXPORT_SYMBOL(skb_queue_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | |
| 3121 | /** |
| 3122 | * skb_queue_tail - queue a buffer at the list tail |
| 3123 | * @list: list to use |
| 3124 | * @newsk: buffer to queue |
| 3125 | * |
| 3126 | * Queue a buffer at the tail of the list. This function takes the |
| 3127 | * list lock and can be used safely with other locking &sk_buff functions |
| 3128 | * safely. |
| 3129 | * |
| 3130 | * A buffer cannot be placed on two lists at the same time. |
| 3131 | */ |
| 3132 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) |
| 3133 | { |
| 3134 | unsigned long flags; |
| 3135 | |
| 3136 | spin_lock_irqsave(&list->lock, flags); |
| 3137 | __skb_queue_tail(list, newsk); |
| 3138 | spin_unlock_irqrestore(&list->lock, flags); |
| 3139 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3140 | EXPORT_SYMBOL(skb_queue_tail); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | /** |
| 3143 | * skb_unlink - remove a buffer from a list |
| 3144 | * @skb: buffer to remove |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3145 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3147 | * Remove a packet from a list. The list locks are taken and this |
| 3148 | * function is atomic with respect to other list locked calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3150 | * You must know what list the SKB is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3152 | void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3154 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3156 | spin_lock_irqsave(&list->lock, flags); |
| 3157 | __skb_unlink(skb, list); |
| 3158 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3160 | EXPORT_SYMBOL(skb_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | /** |
| 3163 | * skb_append - append a buffer |
| 3164 | * @old: buffer to insert after |
| 3165 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3166 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | * |
| 3168 | * Place a packet after a given packet in a list. The list locks are taken |
| 3169 | * and this function is atomic with respect to other list locked calls. |
| 3170 | * A buffer cannot be placed on two lists at the same time. |
| 3171 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3172 | 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] | 3173 | { |
| 3174 | unsigned long flags; |
| 3175 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3176 | spin_lock_irqsave(&list->lock, flags); |
Gerrit Renker | 7de6c03 | 2008-04-14 00:05:09 -0700 | [diff] [blame] | 3177 | __skb_queue_after(list, old, newsk); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3178 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3180 | EXPORT_SYMBOL(skb_append); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3182 | static inline void skb_split_inside_header(struct sk_buff *skb, |
| 3183 | struct sk_buff* skb1, |
| 3184 | const u32 len, const int pos) |
| 3185 | { |
| 3186 | int i; |
| 3187 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 3188 | skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), |
| 3189 | pos - len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | /* And move data appendix as is. */ |
| 3191 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 3192 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 3193 | |
| 3194 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; |
| 3195 | skb_shinfo(skb)->nr_frags = 0; |
| 3196 | skb1->data_len = skb->data_len; |
| 3197 | skb1->len += skb1->data_len; |
| 3198 | skb->data_len = 0; |
| 3199 | skb->len = len; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 3200 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | static inline void skb_split_no_header(struct sk_buff *skb, |
| 3204 | struct sk_buff* skb1, |
| 3205 | const u32 len, int pos) |
| 3206 | { |
| 3207 | int i, k = 0; |
| 3208 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 3209 | |
| 3210 | skb_shinfo(skb)->nr_frags = 0; |
| 3211 | skb1->len = skb1->data_len = skb->len - len; |
| 3212 | skb->len = len; |
| 3213 | skb->data_len = len - pos; |
| 3214 | |
| 3215 | for (i = 0; i < nfrags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3216 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | |
| 3218 | if (pos + size > len) { |
| 3219 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 3220 | |
| 3221 | if (pos < len) { |
| 3222 | /* Split frag. |
| 3223 | * We have two variants in this case: |
| 3224 | * 1. Move all the frag to the second |
| 3225 | * part, if it is possible. F.e. |
| 3226 | * this approach is mandatory for TUX, |
| 3227 | * where splitting is expensive. |
| 3228 | * 2. Split is accurately. We make this. |
| 3229 | */ |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3230 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | skb_shinfo(skb1)->frags[0].page_offset += len - pos; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3232 | skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); |
| 3233 | skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | skb_shinfo(skb)->nr_frags++; |
| 3235 | } |
| 3236 | k++; |
| 3237 | } else |
| 3238 | skb_shinfo(skb)->nr_frags++; |
| 3239 | pos += size; |
| 3240 | } |
| 3241 | skb_shinfo(skb1)->nr_frags = k; |
| 3242 | } |
| 3243 | |
| 3244 | /** |
| 3245 | * skb_split - Split fragmented skb to two parts at length len. |
| 3246 | * @skb: the buffer to split |
| 3247 | * @skb1: the buffer to receive the second part |
| 3248 | * @len: new length for skb |
| 3249 | */ |
| 3250 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) |
| 3251 | { |
| 3252 | int pos = skb_headlen(skb); |
| 3253 | |
Willem de Bruijn | fff8803 | 2017-06-08 11:35:03 -0400 | [diff] [blame] | 3254 | skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags & |
| 3255 | SKBTX_SHARED_FRAG; |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 3256 | skb_zerocopy_clone(skb1, skb, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | if (len < pos) /* Split line is inside header. */ |
| 3258 | skb_split_inside_header(skb, skb1, len, pos); |
| 3259 | else /* Second chunk has no header, nothing to copy. */ |
| 3260 | skb_split_no_header(skb, skb1, len, pos); |
| 3261 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3262 | EXPORT_SYMBOL(skb_split); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | |
Ilpo Järvinen | 9f782db | 2008-11-25 13:57:01 -0800 | [diff] [blame] | 3264 | /* Shifting from/to a cloned skb is a no-go. |
| 3265 | * |
| 3266 | * Caller cannot keep skb_shinfo related pointers past calling here! |
| 3267 | */ |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3268 | static int skb_prepare_for_shift(struct sk_buff *skb) |
| 3269 | { |
Ilpo Järvinen | 0ace285 | 2008-11-24 21:30:21 -0800 | [diff] [blame] | 3270 | 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] | 3271 | } |
| 3272 | |
| 3273 | /** |
| 3274 | * skb_shift - Shifts paged data partially from skb to another |
| 3275 | * @tgt: buffer into which tail data gets added |
| 3276 | * @skb: buffer from which the paged data comes from |
| 3277 | * @shiftlen: shift up to this many bytes |
| 3278 | * |
| 3279 | * 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] | 3280 | * 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] | 3281 | * It's up to caller to free skb if everything was shifted. |
| 3282 | * |
| 3283 | * If @tgt runs out of frags, the whole operation is aborted. |
| 3284 | * |
| 3285 | * Skb cannot include anything else but paged data while tgt is allowed |
| 3286 | * to have non-paged data as well. |
| 3287 | * |
| 3288 | * TODO: full sized shift could be optimized but that would need |
| 3289 | * specialized skb free'er to handle frags without up-to-date nr_frags. |
| 3290 | */ |
| 3291 | int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) |
| 3292 | { |
| 3293 | int from, to, merge, todo; |
| 3294 | struct skb_frag_struct *fragfrom, *fragto; |
| 3295 | |
| 3296 | BUG_ON(shiftlen > skb->len); |
Eric Dumazet | f8071cd | 2016-11-15 12:51:50 -0800 | [diff] [blame] | 3297 | |
| 3298 | if (skb_headlen(skb)) |
| 3299 | return 0; |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 3300 | if (skb_zcopy(tgt) || skb_zcopy(skb)) |
| 3301 | return 0; |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3302 | |
| 3303 | todo = shiftlen; |
| 3304 | from = 0; |
| 3305 | to = skb_shinfo(tgt)->nr_frags; |
| 3306 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 3307 | |
| 3308 | /* Actual merge is delayed until the point when we know we can |
| 3309 | * commit all, so that we don't have to undo partial changes |
| 3310 | */ |
| 3311 | if (!to || |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3312 | !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), |
| 3313 | fragfrom->page_offset)) { |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3314 | merge = -1; |
| 3315 | } else { |
| 3316 | merge = to - 1; |
| 3317 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3318 | todo -= skb_frag_size(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3319 | if (todo < 0) { |
| 3320 | if (skb_prepare_for_shift(skb) || |
| 3321 | skb_prepare_for_shift(tgt)) |
| 3322 | return 0; |
| 3323 | |
Ilpo Järvinen | 9f782db | 2008-11-25 13:57:01 -0800 | [diff] [blame] | 3324 | /* All previous frag pointers might be stale! */ |
| 3325 | fragfrom = &skb_shinfo(skb)->frags[from]; |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3326 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 3327 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3328 | skb_frag_size_add(fragto, shiftlen); |
| 3329 | skb_frag_size_sub(fragfrom, shiftlen); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3330 | fragfrom->page_offset += shiftlen; |
| 3331 | |
| 3332 | goto onlymerged; |
| 3333 | } |
| 3334 | |
| 3335 | from++; |
| 3336 | } |
| 3337 | |
| 3338 | /* Skip full, not-fitting skb to avoid expensive operations */ |
| 3339 | if ((shiftlen == skb->len) && |
| 3340 | (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) |
| 3341 | return 0; |
| 3342 | |
| 3343 | if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) |
| 3344 | return 0; |
| 3345 | |
| 3346 | while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { |
| 3347 | if (to == MAX_SKB_FRAGS) |
| 3348 | return 0; |
| 3349 | |
| 3350 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 3351 | fragto = &skb_shinfo(tgt)->frags[to]; |
| 3352 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3353 | if (todo >= skb_frag_size(fragfrom)) { |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3354 | *fragto = *fragfrom; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3355 | todo -= skb_frag_size(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3356 | from++; |
| 3357 | to++; |
| 3358 | |
| 3359 | } else { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3360 | __skb_frag_ref(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3361 | fragto->page = fragfrom->page; |
| 3362 | fragto->page_offset = fragfrom->page_offset; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3363 | skb_frag_size_set(fragto, todo); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3364 | |
| 3365 | fragfrom->page_offset += todo; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3366 | skb_frag_size_sub(fragfrom, todo); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3367 | todo = 0; |
| 3368 | |
| 3369 | to++; |
| 3370 | break; |
| 3371 | } |
| 3372 | } |
| 3373 | |
| 3374 | /* Ready to "commit" this state change to tgt */ |
| 3375 | skb_shinfo(tgt)->nr_frags = to; |
| 3376 | |
| 3377 | if (merge >= 0) { |
| 3378 | fragfrom = &skb_shinfo(skb)->frags[0]; |
| 3379 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 3380 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3381 | skb_frag_size_add(fragto, skb_frag_size(fragfrom)); |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3382 | __skb_frag_unref(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3383 | } |
| 3384 | |
| 3385 | /* Reposition in the original skb */ |
| 3386 | to = 0; |
| 3387 | while (from < skb_shinfo(skb)->nr_frags) |
| 3388 | skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; |
| 3389 | skb_shinfo(skb)->nr_frags = to; |
| 3390 | |
| 3391 | BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); |
| 3392 | |
| 3393 | onlymerged: |
| 3394 | /* Most likely the tgt won't ever need its checksum anymore, skb on |
| 3395 | * the other hand might need it if it needs to be resent |
| 3396 | */ |
| 3397 | tgt->ip_summed = CHECKSUM_PARTIAL; |
| 3398 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 3399 | |
| 3400 | /* Yak, is it really working this way? Some helper please? */ |
| 3401 | skb->len -= shiftlen; |
| 3402 | skb->data_len -= shiftlen; |
| 3403 | skb->truesize -= shiftlen; |
| 3404 | tgt->len += shiftlen; |
| 3405 | tgt->data_len += shiftlen; |
| 3406 | tgt->truesize += shiftlen; |
| 3407 | |
| 3408 | return shiftlen; |
| 3409 | } |
| 3410 | |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3411 | /** |
| 3412 | * skb_prepare_seq_read - Prepare a sequential read of skb data |
| 3413 | * @skb: the buffer to read |
| 3414 | * @from: lower offset of data to be read |
| 3415 | * @to: upper offset of data to be read |
| 3416 | * @st: state variable |
| 3417 | * |
| 3418 | * Initializes the specified state variable. Must be called before |
| 3419 | * invoking skb_seq_read() for the first time. |
| 3420 | */ |
| 3421 | void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, |
| 3422 | unsigned int to, struct skb_seq_state *st) |
| 3423 | { |
| 3424 | st->lower_offset = from; |
| 3425 | st->upper_offset = to; |
| 3426 | st->root_skb = st->cur_skb = skb; |
| 3427 | st->frag_idx = st->stepped_offset = 0; |
| 3428 | st->frag_data = NULL; |
| 3429 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3430 | EXPORT_SYMBOL(skb_prepare_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3431 | |
| 3432 | /** |
| 3433 | * skb_seq_read - Sequentially read skb data |
| 3434 | * @consumed: number of bytes consumed by the caller so far |
| 3435 | * @data: destination pointer for data to be returned |
| 3436 | * @st: state variable |
| 3437 | * |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 3438 | * Reads a block of skb data at @consumed relative to the |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3439 | * lower offset specified to skb_prepare_seq_read(). Assigns |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 3440 | * the head of the data block to @data and returns the length |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3441 | * of the block or 0 if the end of the skb data or the upper |
| 3442 | * offset has been reached. |
| 3443 | * |
| 3444 | * The caller is not required to consume all of the data |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 3445 | * returned, i.e. @consumed is typically set to the number |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3446 | * of bytes already consumed and the next call to |
| 3447 | * skb_seq_read() will return the remaining part of the block. |
| 3448 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3449 | * 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] | 3450 | * this limitation is the cost for zerocopy sequential |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3451 | * reads of potentially non linear data. |
| 3452 | * |
Randy Dunlap | bc2cda1 | 2008-02-13 15:03:25 -0800 | [diff] [blame] | 3453 | * Note 2: Fragment lists within fragments are not implemented |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3454 | * at the moment, state->root_skb could be replaced with |
| 3455 | * a stack for this purpose. |
| 3456 | */ |
| 3457 | unsigned int skb_seq_read(unsigned int consumed, const u8 **data, |
| 3458 | struct skb_seq_state *st) |
| 3459 | { |
| 3460 | unsigned int block_limit, abs_offset = consumed + st->lower_offset; |
| 3461 | skb_frag_t *frag; |
| 3462 | |
Wedson Almeida Filho | aeb193e | 2013-06-23 23:33:48 -0700 | [diff] [blame] | 3463 | if (unlikely(abs_offset >= st->upper_offset)) { |
| 3464 | if (st->frag_data) { |
| 3465 | kunmap_atomic(st->frag_data); |
| 3466 | st->frag_data = NULL; |
| 3467 | } |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3468 | return 0; |
Wedson Almeida Filho | aeb193e | 2013-06-23 23:33:48 -0700 | [diff] [blame] | 3469 | } |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3470 | |
| 3471 | next_skb: |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 3472 | block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3473 | |
Thomas Chenault | 995b337 | 2009-05-18 21:43:27 -0700 | [diff] [blame] | 3474 | if (abs_offset < block_limit && !st->frag_data) { |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 3475 | *data = st->cur_skb->data + (abs_offset - st->stepped_offset); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3476 | return block_limit - abs_offset; |
| 3477 | } |
| 3478 | |
| 3479 | if (st->frag_idx == 0 && !st->frag_data) |
| 3480 | st->stepped_offset += skb_headlen(st->cur_skb); |
| 3481 | |
| 3482 | while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { |
| 3483 | frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3484 | block_limit = skb_frag_size(frag) + st->stepped_offset; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3485 | |
| 3486 | if (abs_offset < block_limit) { |
| 3487 | if (!st->frag_data) |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 3488 | st->frag_data = kmap_atomic(skb_frag_page(frag)); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3489 | |
| 3490 | *data = (u8 *) st->frag_data + frag->page_offset + |
| 3491 | (abs_offset - st->stepped_offset); |
| 3492 | |
| 3493 | return block_limit - abs_offset; |
| 3494 | } |
| 3495 | |
| 3496 | if (st->frag_data) { |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 3497 | kunmap_atomic(st->frag_data); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3498 | st->frag_data = NULL; |
| 3499 | } |
| 3500 | |
| 3501 | st->frag_idx++; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3502 | st->stepped_offset += skb_frag_size(frag); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3503 | } |
| 3504 | |
Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 3505 | if (st->frag_data) { |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 3506 | kunmap_atomic(st->frag_data); |
Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 3507 | st->frag_data = NULL; |
| 3508 | } |
| 3509 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3510 | 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] | 3511 | st->cur_skb = skb_shinfo(st->root_skb)->frag_list; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3512 | st->frag_idx = 0; |
| 3513 | goto next_skb; |
Shyam Iyer | 71b3346 | 2009-01-29 16:12:42 -0800 | [diff] [blame] | 3514 | } else if (st->cur_skb->next) { |
| 3515 | st->cur_skb = st->cur_skb->next; |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 3516 | st->frag_idx = 0; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3517 | goto next_skb; |
| 3518 | } |
| 3519 | |
| 3520 | return 0; |
| 3521 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3522 | EXPORT_SYMBOL(skb_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3523 | |
| 3524 | /** |
| 3525 | * skb_abort_seq_read - Abort a sequential read of skb data |
| 3526 | * @st: state variable |
| 3527 | * |
| 3528 | * Must be called if skb_seq_read() was not called until it |
| 3529 | * returned 0. |
| 3530 | */ |
| 3531 | void skb_abort_seq_read(struct skb_seq_state *st) |
| 3532 | { |
| 3533 | if (st->frag_data) |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 3534 | kunmap_atomic(st->frag_data); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3535 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3536 | EXPORT_SYMBOL(skb_abort_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3537 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3538 | #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) |
| 3539 | |
| 3540 | static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, |
| 3541 | struct ts_config *conf, |
| 3542 | struct ts_state *state) |
| 3543 | { |
| 3544 | return skb_seq_read(offset, text, TS_SKB_CB(state)); |
| 3545 | } |
| 3546 | |
| 3547 | static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) |
| 3548 | { |
| 3549 | skb_abort_seq_read(TS_SKB_CB(state)); |
| 3550 | } |
| 3551 | |
| 3552 | /** |
| 3553 | * skb_find_text - Find a text pattern in skb data |
| 3554 | * @skb: the buffer to look in |
| 3555 | * @from: search offset |
| 3556 | * @to: search limit |
| 3557 | * @config: textsearch configuration |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3558 | * |
| 3559 | * Finds a pattern in the skb data according to the specified |
| 3560 | * textsearch configuration. Use textsearch_next() to retrieve |
| 3561 | * subsequent occurrences of the pattern. Returns the offset |
| 3562 | * to the first occurrence or UINT_MAX if no match was found. |
| 3563 | */ |
| 3564 | unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame] | 3565 | unsigned int to, struct ts_config *config) |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3566 | { |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame] | 3567 | struct ts_state state; |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 3568 | unsigned int ret; |
| 3569 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3570 | config->get_next_block = skb_ts_get_next_block; |
| 3571 | config->finish = skb_ts_finish; |
| 3572 | |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame] | 3573 | skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3574 | |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame] | 3575 | ret = textsearch_find(config, &state); |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 3576 | return (ret <= to - from ? ret : UINT_MAX); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3577 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3578 | EXPORT_SYMBOL(skb_find_text); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3579 | |
Hannes Frederic Sowa | be12a1f | 2015-05-21 16:59:58 +0200 | [diff] [blame] | 3580 | int skb_append_pagefrags(struct sk_buff *skb, struct page *page, |
| 3581 | int offset, size_t size) |
| 3582 | { |
| 3583 | int i = skb_shinfo(skb)->nr_frags; |
| 3584 | |
| 3585 | if (skb_can_coalesce(skb, i, page, offset)) { |
| 3586 | skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); |
| 3587 | } else if (i < MAX_SKB_FRAGS) { |
| 3588 | get_page(page); |
| 3589 | skb_fill_page_desc(skb, i, page, offset, size); |
| 3590 | } else { |
| 3591 | return -EMSGSIZE; |
| 3592 | } |
| 3593 | |
| 3594 | return 0; |
| 3595 | } |
| 3596 | EXPORT_SYMBOL_GPL(skb_append_pagefrags); |
| 3597 | |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3598 | /** |
| 3599 | * skb_pull_rcsum - pull skb and update receive checksum |
| 3600 | * @skb: buffer to update |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3601 | * @len: length of data pulled |
| 3602 | * |
| 3603 | * This function performs an skb_pull on the packet and updates |
Urs Thuermann | fee54fa | 2008-02-12 22:03:25 -0800 | [diff] [blame] | 3604 | * the CHECKSUM_COMPLETE checksum. It should be used on |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 3605 | * receive path processing instead of skb_pull unless you know |
| 3606 | * that the checksum difference is zero (e.g., a valid IP header) |
| 3607 | * or you are setting ip_summed to CHECKSUM_NONE. |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3608 | */ |
Johannes Berg | af72868 | 2017-06-16 14:29:22 +0200 | [diff] [blame] | 3609 | void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3610 | { |
Pravin B Shelar | 31b33df | 2015-09-28 17:24:25 -0700 | [diff] [blame] | 3611 | unsigned char *data = skb->data; |
| 3612 | |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3613 | BUG_ON(len > skb->len); |
Pravin B Shelar | 31b33df | 2015-09-28 17:24:25 -0700 | [diff] [blame] | 3614 | __skb_pull(skb, len); |
| 3615 | skb_postpull_rcsum(skb, data, len); |
| 3616 | return skb->data; |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3617 | } |
Arnaldo Carvalho de Melo | f94691a | 2006-03-20 22:47:55 -0800 | [diff] [blame] | 3618 | EXPORT_SYMBOL_GPL(skb_pull_rcsum); |
| 3619 | |
Yonghong Song | 13acc94 | 2018-03-21 16:31:03 -0700 | [diff] [blame] | 3620 | static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) |
| 3621 | { |
| 3622 | skb_frag_t head_frag; |
| 3623 | struct page *page; |
| 3624 | |
| 3625 | page = virt_to_head_page(frag_skb->head); |
| 3626 | head_frag.page.p = page; |
| 3627 | head_frag.page_offset = frag_skb->data - |
| 3628 | (unsigned char *)page_address(page); |
| 3629 | head_frag.size = skb_headlen(frag_skb); |
| 3630 | return head_frag; |
| 3631 | } |
| 3632 | |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3633 | /** |
| 3634 | * skb_segment - Perform protocol segmentation on skb. |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3635 | * @head_skb: buffer to segment |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 3636 | * @features: features for the output path (see dev->features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3637 | * |
| 3638 | * This function performs segmentation on the given skb. It returns |
Ben Hutchings | 4c821d7 | 2008-04-13 21:52:48 -0700 | [diff] [blame] | 3639 | * a pointer to the first in a list of new skbs for the segments. |
| 3640 | * In case of error it returns ERR_PTR(err). |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3641 | */ |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3642 | struct sk_buff *skb_segment(struct sk_buff *head_skb, |
| 3643 | netdev_features_t features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3644 | { |
| 3645 | struct sk_buff *segs = NULL; |
| 3646 | struct sk_buff *tail = NULL; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3647 | struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3648 | skb_frag_t *frag = skb_shinfo(head_skb)->frags; |
| 3649 | unsigned int mss = skb_shinfo(head_skb)->gso_size; |
| 3650 | unsigned int doffset = head_skb->data - skb_mac_header(head_skb); |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 3651 | struct sk_buff *frag_skb = head_skb; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3652 | unsigned int offset = doffset; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3653 | unsigned int tnl_hlen = skb_tnl_header_len(head_skb); |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3654 | unsigned int partial_segs = 0; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3655 | unsigned int headroom; |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3656 | unsigned int len = head_skb->len; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3657 | __be16 proto; |
Alexander Duyck | 36c9838 | 2016-05-02 09:38:18 -0700 | [diff] [blame] | 3658 | bool csum, sg; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3659 | int nfrags = skb_shinfo(head_skb)->nr_frags; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3660 | int err = -ENOMEM; |
| 3661 | int i = 0; |
| 3662 | int pos; |
Vlad Yasevich | 53d6471 | 2014-03-27 17:26:18 -0400 | [diff] [blame] | 3663 | int dummy; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3664 | |
Wei-Chun Chao | 5882a07 | 2014-06-08 23:48:54 -0700 | [diff] [blame] | 3665 | __skb_push(head_skb, doffset); |
Vlad Yasevich | 53d6471 | 2014-03-27 17:26:18 -0400 | [diff] [blame] | 3666 | proto = skb_network_protocol(head_skb, &dummy); |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3667 | if (unlikely(!proto)) |
| 3668 | return ERR_PTR(-EINVAL); |
| 3669 | |
Alexander Duyck | 36c9838 | 2016-05-02 09:38:18 -0700 | [diff] [blame] | 3670 | sg = !!(features & NETIF_F_SG); |
Alexander Duyck | f245d07 | 2016-02-05 15:28:26 -0800 | [diff] [blame] | 3671 | csum = !!can_checksum_protocol(features, proto); |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 3672 | |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3673 | if (sg && csum && (mss != GSO_BY_FRAGS)) { |
| 3674 | if (!(features & NETIF_F_GSO_PARTIAL)) { |
| 3675 | struct sk_buff *iter; |
Ilan Tayari | 43170c4 | 2017-04-19 21:26:07 +0300 | [diff] [blame] | 3676 | unsigned int frag_len; |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3677 | |
| 3678 | if (!list_skb || |
| 3679 | !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) |
| 3680 | goto normal; |
| 3681 | |
Ilan Tayari | 43170c4 | 2017-04-19 21:26:07 +0300 | [diff] [blame] | 3682 | /* If we get here then all the required |
| 3683 | * GSO features except frag_list are supported. |
| 3684 | * Try to split the SKB to multiple GSO SKBs |
| 3685 | * with no frag_list. |
| 3686 | * Currently we can do that only when the buffers don't |
| 3687 | * have a linear part and all the buffers except |
| 3688 | * the last are of the same length. |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3689 | */ |
Ilan Tayari | 43170c4 | 2017-04-19 21:26:07 +0300 | [diff] [blame] | 3690 | frag_len = list_skb->len; |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3691 | skb_walk_frags(head_skb, iter) { |
Ilan Tayari | 43170c4 | 2017-04-19 21:26:07 +0300 | [diff] [blame] | 3692 | if (frag_len != iter->len && iter->next) |
| 3693 | goto normal; |
Ilan Tayari | eaffadb | 2017-04-08 02:07:08 +0300 | [diff] [blame] | 3694 | if (skb_headlen(iter) && !iter->head_frag) |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3695 | goto normal; |
| 3696 | |
| 3697 | len -= iter->len; |
| 3698 | } |
Ilan Tayari | 43170c4 | 2017-04-19 21:26:07 +0300 | [diff] [blame] | 3699 | |
| 3700 | if (len != frag_len) |
| 3701 | goto normal; |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | /* GSO partial only requires that we trim off any excess that |
| 3705 | * doesn't fit into an MSS sized block, so take care of that |
| 3706 | * now. |
| 3707 | */ |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3708 | partial_segs = len / mss; |
Alexander Duyck | d7fb5a8 | 2016-05-02 09:38:12 -0700 | [diff] [blame] | 3709 | if (partial_segs > 1) |
| 3710 | mss *= partial_segs; |
| 3711 | else |
| 3712 | partial_segs = 0; |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3713 | } |
| 3714 | |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3715 | normal: |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3716 | headroom = skb_headroom(head_skb); |
| 3717 | pos = skb_headlen(head_skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3718 | |
| 3719 | do { |
| 3720 | struct sk_buff *nskb; |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3721 | skb_frag_t *nskb_frag; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 3722 | int hsize; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3723 | int size; |
| 3724 | |
Marcelo Ricardo Leitner | 3953c46 | 2016-06-02 15:05:40 -0300 | [diff] [blame] | 3725 | if (unlikely(mss == GSO_BY_FRAGS)) { |
| 3726 | len = list_skb->len; |
| 3727 | } else { |
| 3728 | len = head_skb->len - offset; |
| 3729 | if (len > mss) |
| 3730 | len = mss; |
| 3731 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3732 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3733 | hsize = skb_headlen(head_skb) - offset; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3734 | if (hsize < 0) |
| 3735 | hsize = 0; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 3736 | if (hsize > len || !sg) |
| 3737 | hsize = len; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3738 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3739 | if (!hsize && i >= nfrags && skb_headlen(list_skb) && |
| 3740 | (skb_headlen(list_skb) == len || sg)) { |
| 3741 | BUG_ON(skb_headlen(list_skb) > len); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3742 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3743 | i = 0; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3744 | nfrags = skb_shinfo(list_skb)->nr_frags; |
| 3745 | frag = skb_shinfo(list_skb)->frags; |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 3746 | frag_skb = list_skb; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3747 | pos += skb_headlen(list_skb); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3748 | |
| 3749 | while (pos < offset + len) { |
| 3750 | BUG_ON(i >= nfrags); |
| 3751 | |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 3752 | size = skb_frag_size(frag); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3753 | if (pos + size > offset + len) |
| 3754 | break; |
| 3755 | |
| 3756 | i++; |
| 3757 | pos += size; |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 3758 | frag++; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3759 | } |
| 3760 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3761 | nskb = skb_clone(list_skb, GFP_ATOMIC); |
| 3762 | list_skb = list_skb->next; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3763 | |
| 3764 | if (unlikely(!nskb)) |
| 3765 | goto err; |
| 3766 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3767 | if (unlikely(pskb_trim(nskb, len))) { |
| 3768 | kfree_skb(nskb); |
| 3769 | goto err; |
| 3770 | } |
| 3771 | |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 3772 | hsize = skb_end_offset(nskb); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3773 | if (skb_cow_head(nskb, doffset + headroom)) { |
| 3774 | kfree_skb(nskb); |
| 3775 | goto err; |
| 3776 | } |
| 3777 | |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 3778 | nskb->truesize += skb_end_offset(nskb) - hsize; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3779 | skb_release_head_state(nskb); |
| 3780 | __skb_push(nskb, doffset); |
| 3781 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 3782 | nskb = __alloc_skb(hsize + doffset + headroom, |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3783 | GFP_ATOMIC, skb_alloc_rx_flag(head_skb), |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 3784 | NUMA_NO_NODE); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3785 | |
| 3786 | if (unlikely(!nskb)) |
| 3787 | goto err; |
| 3788 | |
| 3789 | skb_reserve(nskb, headroom); |
| 3790 | __skb_put(nskb, doffset); |
| 3791 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3792 | |
| 3793 | if (segs) |
| 3794 | tail->next = nskb; |
| 3795 | else |
| 3796 | segs = nskb; |
| 3797 | tail = nskb; |
| 3798 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3799 | __copy_skb_header(nskb, head_skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3800 | |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 3801 | skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); |
Vlad Yasevich | fcdfe3a | 2014-07-31 10:33:06 -0400 | [diff] [blame] | 3802 | skb_reset_mac_len(nskb); |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 3803 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3804 | skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 3805 | nskb->data - tnl_hlen, |
| 3806 | doffset + tnl_hlen); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3807 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3808 | if (nskb->len == len + doffset) |
Simon Horman | 1cdbcb7 | 2013-05-19 15:46:49 +0000 | [diff] [blame] | 3809 | goto perform_csum_check; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3810 | |
Alexander Duyck | 7fbeffe | 2016-02-05 15:27:43 -0800 | [diff] [blame] | 3811 | if (!sg) { |
| 3812 | if (!nskb->remcsum_offload) |
| 3813 | nskb->ip_summed = CHECKSUM_NONE; |
Alexander Duyck | 7644345 | 2016-02-05 15:27:37 -0800 | [diff] [blame] | 3814 | SKB_GSO_CB(nskb)->csum = |
| 3815 | skb_copy_and_csum_bits(head_skb, offset, |
| 3816 | skb_put(nskb, len), |
| 3817 | len, 0); |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 3818 | SKB_GSO_CB(nskb)->csum_start = |
Alexander Duyck | 7644345 | 2016-02-05 15:27:37 -0800 | [diff] [blame] | 3819 | skb_headroom(nskb) + doffset; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3820 | continue; |
| 3821 | } |
| 3822 | |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3823 | nskb_frag = skb_shinfo(nskb)->frags; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3824 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3825 | skb_copy_from_linear_data_offset(head_skb, offset, |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 3826 | skb_put(nskb, hsize), hsize); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3827 | |
Willem de Bruijn | fff8803 | 2017-06-08 11:35:03 -0400 | [diff] [blame] | 3828 | skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags & |
| 3829 | SKBTX_SHARED_FRAG; |
Eric Dumazet | cef401d | 2013-01-25 20:34:37 +0000 | [diff] [blame] | 3830 | |
Willem de Bruijn | bf5c25d | 2017-12-22 19:00:17 -0500 | [diff] [blame] | 3831 | if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || |
| 3832 | skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) |
| 3833 | goto err; |
| 3834 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3835 | while (pos < offset + len) { |
| 3836 | if (i >= nfrags) { |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3837 | i = 0; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3838 | nfrags = skb_shinfo(list_skb)->nr_frags; |
| 3839 | frag = skb_shinfo(list_skb)->frags; |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 3840 | frag_skb = list_skb; |
Yonghong Song | 13acc94 | 2018-03-21 16:31:03 -0700 | [diff] [blame] | 3841 | if (!skb_headlen(list_skb)) { |
| 3842 | BUG_ON(!nfrags); |
| 3843 | } else { |
| 3844 | BUG_ON(!list_skb->head_frag); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3845 | |
Yonghong Song | 13acc94 | 2018-03-21 16:31:03 -0700 | [diff] [blame] | 3846 | /* to make room for head_frag. */ |
| 3847 | i--; |
| 3848 | frag--; |
| 3849 | } |
Willem de Bruijn | bf5c25d | 2017-12-22 19:00:17 -0500 | [diff] [blame] | 3850 | if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || |
| 3851 | skb_zerocopy_clone(nskb, frag_skb, |
| 3852 | GFP_ATOMIC)) |
| 3853 | goto err; |
| 3854 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3855 | list_skb = list_skb->next; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | if (unlikely(skb_shinfo(nskb)->nr_frags >= |
| 3859 | MAX_SKB_FRAGS)) { |
| 3860 | net_warn_ratelimited( |
| 3861 | "skb_segment: too many frags: %u %u\n", |
| 3862 | pos, mss); |
Eric Dumazet | ff907a1 | 2018-07-19 16:04:38 -0700 | [diff] [blame] | 3863 | err = -EINVAL; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3864 | goto err; |
| 3865 | } |
| 3866 | |
Yonghong Song | 13acc94 | 2018-03-21 16:31:03 -0700 | [diff] [blame] | 3867 | *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3868 | __skb_frag_ref(nskb_frag); |
| 3869 | size = skb_frag_size(nskb_frag); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3870 | |
| 3871 | if (pos < offset) { |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3872 | nskb_frag->page_offset += offset - pos; |
| 3873 | skb_frag_size_sub(nskb_frag, offset - pos); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3874 | } |
| 3875 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3876 | skb_shinfo(nskb)->nr_frags++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3877 | |
| 3878 | if (pos + size <= offset + len) { |
| 3879 | i++; |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 3880 | frag++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3881 | pos += size; |
| 3882 | } else { |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3883 | skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3884 | goto skip_fraglist; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3885 | } |
| 3886 | |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3887 | nskb_frag++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3888 | } |
| 3889 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3890 | skip_fraglist: |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3891 | nskb->data_len = len - hsize; |
| 3892 | nskb->len += nskb->data_len; |
| 3893 | nskb->truesize += nskb->data_len; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3894 | |
Simon Horman | 1cdbcb7 | 2013-05-19 15:46:49 +0000 | [diff] [blame] | 3895 | perform_csum_check: |
Alexander Duyck | 7fbeffe | 2016-02-05 15:27:43 -0800 | [diff] [blame] | 3896 | if (!csum) { |
Eric Dumazet | ff907a1 | 2018-07-19 16:04:38 -0700 | [diff] [blame] | 3897 | if (skb_has_shared_frag(nskb) && |
| 3898 | __skb_linearize(nskb)) |
| 3899 | goto err; |
| 3900 | |
Alexander Duyck | 7fbeffe | 2016-02-05 15:27:43 -0800 | [diff] [blame] | 3901 | if (!nskb->remcsum_offload) |
| 3902 | nskb->ip_summed = CHECKSUM_NONE; |
Alexander Duyck | 7644345 | 2016-02-05 15:27:37 -0800 | [diff] [blame] | 3903 | SKB_GSO_CB(nskb)->csum = |
| 3904 | skb_checksum(nskb, doffset, |
| 3905 | nskb->len - doffset, 0); |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 3906 | SKB_GSO_CB(nskb)->csum_start = |
Alexander Duyck | 7644345 | 2016-02-05 15:27:37 -0800 | [diff] [blame] | 3907 | skb_headroom(nskb) + doffset; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3908 | } |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3909 | } while ((offset += len) < head_skb->len); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3910 | |
Eric Dumazet | bec3cfd | 2014-10-03 20:59:19 -0700 | [diff] [blame] | 3911 | /* Some callers want to get the end of the list. |
| 3912 | * Put it in segs->prev to avoid walking the list. |
| 3913 | * (see validate_xmit_skb_list() for example) |
| 3914 | */ |
| 3915 | segs->prev = tail; |
Toshiaki Makita | 432c856 | 2014-10-27 10:30:51 -0700 | [diff] [blame] | 3916 | |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3917 | if (partial_segs) { |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3918 | struct sk_buff *iter; |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3919 | int type = skb_shinfo(head_skb)->gso_type; |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3920 | unsigned short gso_size = skb_shinfo(head_skb)->gso_size; |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3921 | |
| 3922 | /* Update type to add partial and then remove dodgy if set */ |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3923 | type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3924 | type &= ~SKB_GSO_DODGY; |
| 3925 | |
| 3926 | /* Update GSO info and prepare to start updating headers on |
| 3927 | * our way back down the stack of protocols. |
| 3928 | */ |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3929 | for (iter = segs; iter; iter = iter->next) { |
| 3930 | skb_shinfo(iter)->gso_size = gso_size; |
| 3931 | skb_shinfo(iter)->gso_segs = partial_segs; |
| 3932 | skb_shinfo(iter)->gso_type = type; |
| 3933 | SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; |
| 3934 | } |
| 3935 | |
| 3936 | if (tail->len - doffset <= gso_size) |
| 3937 | skb_shinfo(tail)->gso_size = 0; |
| 3938 | else if (tail != segs) |
| 3939 | skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3940 | } |
| 3941 | |
Toshiaki Makita | 432c856 | 2014-10-27 10:30:51 -0700 | [diff] [blame] | 3942 | /* Following permits correct backpressure, for protocols |
| 3943 | * using skb_set_owner_w(). |
| 3944 | * Idea is to tranfert ownership from head_skb to last segment. |
| 3945 | */ |
| 3946 | if (head_skb->destructor == sock_wfree) { |
| 3947 | swap(tail->truesize, head_skb->truesize); |
| 3948 | swap(tail->destructor, head_skb->destructor); |
| 3949 | swap(tail->sk, head_skb->sk); |
| 3950 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3951 | return segs; |
| 3952 | |
| 3953 | err: |
Eric Dumazet | 289dccb | 2013-12-20 14:29:08 -0800 | [diff] [blame] | 3954 | kfree_skb_list(segs); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3955 | return ERR_PTR(err); |
| 3956 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3957 | EXPORT_SYMBOL_GPL(skb_segment); |
| 3958 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 3959 | int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb) |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3960 | { |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3961 | struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3962 | unsigned int offset = skb_gro_offset(skb); |
| 3963 | unsigned int headlen = skb_headlen(skb); |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3964 | unsigned int len = skb_gro_len(skb); |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3965 | unsigned int delta_truesize; |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 3966 | struct sk_buff *lp; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3967 | |
Steffen Klassert | 0ab03f3 | 2019-04-02 08:16:03 +0200 | [diff] [blame] | 3968 | if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush)) |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3969 | return -E2BIG; |
| 3970 | |
Eric Dumazet | 29e9824 | 2014-05-16 11:34:37 -0700 | [diff] [blame] | 3971 | lp = NAPI_GRO_CB(p)->last; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3972 | pinfo = skb_shinfo(lp); |
| 3973 | |
| 3974 | if (headlen <= offset) { |
Herbert Xu | 42da699 | 2009-05-26 18:50:19 +0000 | [diff] [blame] | 3975 | skb_frag_t *frag; |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3976 | skb_frag_t *frag2; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3977 | int i = skbinfo->nr_frags; |
| 3978 | int nr_frags = pinfo->nr_frags + i; |
Herbert Xu | 42da699 | 2009-05-26 18:50:19 +0000 | [diff] [blame] | 3979 | |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3980 | if (nr_frags > MAX_SKB_FRAGS) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3981 | goto merge; |
Herbert Xu | 81705ad | 2009-01-29 14:19:51 +0000 | [diff] [blame] | 3982 | |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3983 | offset -= headlen; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3984 | pinfo->nr_frags = nr_frags; |
| 3985 | skbinfo->nr_frags = 0; |
Herbert Xu | f557206 | 2009-01-14 20:40:03 -0800 | [diff] [blame] | 3986 | |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3987 | frag = pinfo->frags + nr_frags; |
| 3988 | frag2 = skbinfo->frags + i; |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3989 | do { |
| 3990 | *--frag = *--frag2; |
| 3991 | } while (--i); |
| 3992 | |
| 3993 | frag->page_offset += offset; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3994 | skb_frag_size_sub(frag, offset); |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3995 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3996 | /* all fragments truesize : remove (head size + sk_buff) */ |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 3997 | delta_truesize = skb->truesize - |
| 3998 | SKB_TRUESIZE(skb_end_offset(skb)); |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3999 | |
Herbert Xu | f557206 | 2009-01-14 20:40:03 -0800 | [diff] [blame] | 4000 | skb->truesize -= skb->data_len; |
| 4001 | skb->len -= skb->data_len; |
| 4002 | skb->data_len = 0; |
| 4003 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4004 | NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 4005 | goto done; |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 4006 | } else if (skb->head_frag) { |
| 4007 | int nr_frags = pinfo->nr_frags; |
| 4008 | skb_frag_t *frag = pinfo->frags + nr_frags; |
| 4009 | struct page *page = virt_to_head_page(skb->head); |
| 4010 | unsigned int first_size = headlen - offset; |
| 4011 | unsigned int first_offset; |
| 4012 | |
| 4013 | if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4014 | goto merge; |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 4015 | |
| 4016 | first_offset = skb->data - |
| 4017 | (unsigned char *)page_address(page) + |
| 4018 | offset; |
| 4019 | |
| 4020 | pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; |
| 4021 | |
| 4022 | frag->page.p = page; |
| 4023 | frag->page_offset = first_offset; |
| 4024 | skb_frag_size_set(frag, first_size); |
| 4025 | |
| 4026 | memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); |
| 4027 | /* We dont need to clear skbinfo->nr_frags here */ |
| 4028 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4029 | delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 4030 | NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; |
| 4031 | goto done; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4032 | } |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4033 | |
| 4034 | merge: |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4035 | delta_truesize = skb->truesize; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 4036 | if (offset > headlen) { |
Michal Schmidt | d1dc7ab | 2011-01-24 12:08:48 +0000 | [diff] [blame] | 4037 | unsigned int eat = offset - headlen; |
| 4038 | |
| 4039 | skbinfo->frags[0].page_offset += eat; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 4040 | skb_frag_size_sub(&skbinfo->frags[0], eat); |
Michal Schmidt | d1dc7ab | 2011-01-24 12:08:48 +0000 | [diff] [blame] | 4041 | skb->data_len -= eat; |
| 4042 | skb->len -= eat; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 4043 | offset = headlen; |
Herbert Xu | 5603502 | 2009-02-05 21:26:52 -0800 | [diff] [blame] | 4044 | } |
| 4045 | |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 4046 | __skb_pull(skb, offset); |
Herbert Xu | 5603502 | 2009-02-05 21:26:52 -0800 | [diff] [blame] | 4047 | |
Eric Dumazet | 29e9824 | 2014-05-16 11:34:37 -0700 | [diff] [blame] | 4048 | if (NAPI_GRO_CB(p)->last == p) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4049 | skb_shinfo(p)->frag_list = skb; |
| 4050 | else |
| 4051 | NAPI_GRO_CB(p)->last->next = skb; |
Eric Dumazet | c3c7c25 | 2012-12-06 13:54:59 +0000 | [diff] [blame] | 4052 | NAPI_GRO_CB(p)->last = skb; |
Eric Dumazet | f4a775d | 2014-09-22 16:29:32 -0700 | [diff] [blame] | 4053 | __skb_header_release(skb); |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4054 | lp = p; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4055 | |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 4056 | done: |
| 4057 | NAPI_GRO_CB(p)->count++; |
Herbert Xu | 37fe473 | 2009-01-17 19:48:13 +0000 | [diff] [blame] | 4058 | p->data_len += len; |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4059 | p->truesize += delta_truesize; |
Herbert Xu | 37fe473 | 2009-01-17 19:48:13 +0000 | [diff] [blame] | 4060 | p->len += len; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4061 | if (lp != p) { |
| 4062 | lp->data_len += len; |
| 4063 | lp->truesize += delta_truesize; |
| 4064 | lp->len += len; |
| 4065 | } |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4066 | NAPI_GRO_CB(skb)->same_flow = 1; |
| 4067 | return 0; |
| 4068 | } |
Marcelo Ricardo Leitner | 57c05650 | 2016-06-02 15:05:39 -0300 | [diff] [blame] | 4069 | EXPORT_SYMBOL_GPL(skb_gro_receive); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4070 | |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 4071 | #ifdef CONFIG_SKB_EXTENSIONS |
| 4072 | #define SKB_EXT_ALIGN_VALUE 8 |
| 4073 | #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) |
| 4074 | |
| 4075 | static const u8 skb_ext_type_len[] = { |
| 4076 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
| 4077 | [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), |
| 4078 | #endif |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 4079 | #ifdef CONFIG_XFRM |
| 4080 | [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), |
| 4081 | #endif |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 4082 | }; |
| 4083 | |
| 4084 | static __always_inline unsigned int skb_ext_total_length(void) |
| 4085 | { |
| 4086 | return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + |
| 4087 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
| 4088 | skb_ext_type_len[SKB_EXT_BRIDGE_NF] + |
| 4089 | #endif |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 4090 | #ifdef CONFIG_XFRM |
| 4091 | skb_ext_type_len[SKB_EXT_SEC_PATH] + |
| 4092 | #endif |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 4093 | 0; |
| 4094 | } |
| 4095 | |
| 4096 | static void skb_extensions_init(void) |
| 4097 | { |
| 4098 | BUILD_BUG_ON(SKB_EXT_NUM >= 8); |
| 4099 | BUILD_BUG_ON(skb_ext_total_length() > 255); |
| 4100 | |
| 4101 | skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", |
| 4102 | SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), |
| 4103 | 0, |
| 4104 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
| 4105 | NULL); |
| 4106 | } |
| 4107 | #else |
| 4108 | static void skb_extensions_init(void) {} |
| 4109 | #endif |
| 4110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | void __init skb_init(void) |
| 4112 | { |
Kees Cook | 79a8a64 | 2018-02-07 17:44:38 -0800 | [diff] [blame] | 4113 | skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | sizeof(struct sk_buff), |
| 4115 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 4116 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Kees Cook | 79a8a64 | 2018-02-07 17:44:38 -0800 | [diff] [blame] | 4117 | offsetof(struct sk_buff, cb), |
| 4118 | sizeof_field(struct sk_buff, cb), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4119 | NULL); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 4120 | skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 4121 | sizeof(struct sk_buff_fclones), |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 4122 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 4123 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4124 | NULL); |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 4125 | skb_extensions_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | } |
| 4127 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 4128 | static int |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4129 | __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, |
| 4130 | unsigned int recursion_level) |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4131 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 4132 | int start = skb_headlen(skb); |
| 4133 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4134 | struct sk_buff *frag_iter; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4135 | int elt = 0; |
| 4136 | |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4137 | if (unlikely(recursion_level >= 24)) |
| 4138 | return -EMSGSIZE; |
| 4139 | |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4140 | if (copy > 0) { |
| 4141 | if (copy > len) |
| 4142 | copy = len; |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 4143 | sg_set_buf(sg, skb->data + offset, copy); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4144 | elt++; |
| 4145 | if ((len -= copy) == 0) |
| 4146 | return elt; |
| 4147 | offset += copy; |
| 4148 | } |
| 4149 | |
| 4150 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 4151 | int end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4152 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 4153 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 4154 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 4155 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4156 | if ((copy = end - offset) > 0) { |
| 4157 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4158 | if (unlikely(elt && sg_is_last(&sg[elt - 1]))) |
| 4159 | return -EMSGSIZE; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4160 | |
| 4161 | if (copy > len) |
| 4162 | copy = len; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 4163 | sg_set_page(&sg[elt], skb_frag_page(frag), copy, |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 4164 | frag->page_offset+offset-start); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4165 | elt++; |
| 4166 | if (!(len -= copy)) |
| 4167 | return elt; |
| 4168 | offset += copy; |
| 4169 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 4170 | start = end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4171 | } |
| 4172 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4173 | skb_walk_frags(skb, frag_iter) { |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4174 | int end, ret; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4175 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4176 | WARN_ON(start > offset + len); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4177 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4178 | end = start + frag_iter->len; |
| 4179 | if ((copy = end - offset) > 0) { |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4180 | if (unlikely(elt && sg_is_last(&sg[elt - 1]))) |
| 4181 | return -EMSGSIZE; |
| 4182 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4183 | if (copy > len) |
| 4184 | copy = len; |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4185 | ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, |
| 4186 | copy, recursion_level + 1); |
| 4187 | if (unlikely(ret < 0)) |
| 4188 | return ret; |
| 4189 | elt += ret; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4190 | if ((len -= copy) == 0) |
| 4191 | return elt; |
| 4192 | offset += copy; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4193 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4194 | start = end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4195 | } |
| 4196 | BUG_ON(len); |
| 4197 | return elt; |
| 4198 | } |
| 4199 | |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4200 | /** |
| 4201 | * skb_to_sgvec - Fill a scatter-gather list from a socket buffer |
| 4202 | * @skb: Socket buffer containing the buffers to be mapped |
| 4203 | * @sg: The scatter-gather list to map into |
| 4204 | * @offset: The offset into the buffer's contents to start mapping |
| 4205 | * @len: Length of buffer space to be mapped |
| 4206 | * |
| 4207 | * Fill the specified scatter-gather list with mappings/pointers into a |
| 4208 | * region of the buffer space attached to a socket buffer. Returns either |
| 4209 | * the number of scatterlist items used, or -EMSGSIZE if the contents |
| 4210 | * could not fit. |
| 4211 | */ |
| 4212 | int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) |
| 4213 | { |
| 4214 | int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); |
| 4215 | |
| 4216 | if (nsg <= 0) |
| 4217 | return nsg; |
| 4218 | |
| 4219 | sg_mark_end(&sg[nsg - 1]); |
| 4220 | |
| 4221 | return nsg; |
| 4222 | } |
| 4223 | EXPORT_SYMBOL_GPL(skb_to_sgvec); |
| 4224 | |
Fan Du | 25a91d8 | 2014-01-18 09:54:23 +0800 | [diff] [blame] | 4225 | /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given |
| 4226 | * sglist without mark the sg which contain last skb data as the end. |
| 4227 | * So the caller can mannipulate sg list as will when padding new data after |
| 4228 | * the first call without calling sg_unmark_end to expend sg list. |
| 4229 | * |
| 4230 | * Scenario to use skb_to_sgvec_nomark: |
| 4231 | * 1. sg_init_table |
| 4232 | * 2. skb_to_sgvec_nomark(payload1) |
| 4233 | * 3. skb_to_sgvec_nomark(payload2) |
| 4234 | * |
| 4235 | * This is equivalent to: |
| 4236 | * 1. sg_init_table |
| 4237 | * 2. skb_to_sgvec(payload1) |
| 4238 | * 3. sg_unmark_end |
| 4239 | * 4. skb_to_sgvec(payload2) |
| 4240 | * |
| 4241 | * When mapping mutilple payload conditionally, skb_to_sgvec_nomark |
| 4242 | * is more preferable. |
| 4243 | */ |
| 4244 | int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, |
| 4245 | int offset, int len) |
| 4246 | { |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4247 | return __skb_to_sgvec(skb, sg, offset, len, 0); |
Fan Du | 25a91d8 | 2014-01-18 09:54:23 +0800 | [diff] [blame] | 4248 | } |
| 4249 | EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); |
| 4250 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 4251 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 4252 | |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4253 | /** |
| 4254 | * skb_cow_data - Check that a socket buffer's data buffers are writable |
| 4255 | * @skb: The socket buffer to check. |
| 4256 | * @tailbits: Amount of trailing space to be added |
| 4257 | * @trailer: Returned pointer to the skb where the @tailbits space begins |
| 4258 | * |
| 4259 | * Make sure that the data buffers attached to a socket buffer are |
| 4260 | * writable. If they are not, private copies are made of the data buffers |
| 4261 | * and the socket buffer is set to use these instead. |
| 4262 | * |
| 4263 | * If @tailbits is given, make sure that there is space to write @tailbits |
| 4264 | * bytes of data beyond current end of socket buffer. @trailer will be |
| 4265 | * set to point to the skb in which this space begins. |
| 4266 | * |
| 4267 | * The number of scatterlist elements required to completely map the |
| 4268 | * COW'd and extended socket buffer will be returned. |
| 4269 | */ |
| 4270 | int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) |
| 4271 | { |
| 4272 | int copyflag; |
| 4273 | int elt; |
| 4274 | struct sk_buff *skb1, **skb_p; |
| 4275 | |
| 4276 | /* If skb is cloned or its head is paged, reallocate |
| 4277 | * head pulling out all the pages (pages are considered not writable |
| 4278 | * at the moment even if they are anonymous). |
| 4279 | */ |
| 4280 | if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && |
| 4281 | __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) |
| 4282 | return -ENOMEM; |
| 4283 | |
| 4284 | /* Easy case. Most of packets will go this way. */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 4285 | if (!skb_has_frag_list(skb)) { |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4286 | /* A little of trouble, not enough of space for trailer. |
| 4287 | * This should not happen, when stack is tuned to generate |
| 4288 | * good frames. OK, on miss we reallocate and reserve even more |
| 4289 | * space, 128 bytes is fair. */ |
| 4290 | |
| 4291 | if (skb_tailroom(skb) < tailbits && |
| 4292 | pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) |
| 4293 | return -ENOMEM; |
| 4294 | |
| 4295 | /* Voila! */ |
| 4296 | *trailer = skb; |
| 4297 | return 1; |
| 4298 | } |
| 4299 | |
| 4300 | /* Misery. We are in troubles, going to mincer fragments... */ |
| 4301 | |
| 4302 | elt = 1; |
| 4303 | skb_p = &skb_shinfo(skb)->frag_list; |
| 4304 | copyflag = 0; |
| 4305 | |
| 4306 | while ((skb1 = *skb_p) != NULL) { |
| 4307 | int ntail = 0; |
| 4308 | |
| 4309 | /* The fragment is partially pulled by someone, |
| 4310 | * this can happen on input. Copy it and everything |
| 4311 | * after it. */ |
| 4312 | |
| 4313 | if (skb_shared(skb1)) |
| 4314 | copyflag = 1; |
| 4315 | |
| 4316 | /* If the skb is the last, worry about trailer. */ |
| 4317 | |
| 4318 | if (skb1->next == NULL && tailbits) { |
| 4319 | if (skb_shinfo(skb1)->nr_frags || |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 4320 | skb_has_frag_list(skb1) || |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4321 | skb_tailroom(skb1) < tailbits) |
| 4322 | ntail = tailbits + 128; |
| 4323 | } |
| 4324 | |
| 4325 | if (copyflag || |
| 4326 | skb_cloned(skb1) || |
| 4327 | ntail || |
| 4328 | skb_shinfo(skb1)->nr_frags || |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 4329 | skb_has_frag_list(skb1)) { |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4330 | struct sk_buff *skb2; |
| 4331 | |
| 4332 | /* Fuck, we are miserable poor guys... */ |
| 4333 | if (ntail == 0) |
| 4334 | skb2 = skb_copy(skb1, GFP_ATOMIC); |
| 4335 | else |
| 4336 | skb2 = skb_copy_expand(skb1, |
| 4337 | skb_headroom(skb1), |
| 4338 | ntail, |
| 4339 | GFP_ATOMIC); |
| 4340 | if (unlikely(skb2 == NULL)) |
| 4341 | return -ENOMEM; |
| 4342 | |
| 4343 | if (skb1->sk) |
| 4344 | skb_set_owner_w(skb2, skb1->sk); |
| 4345 | |
| 4346 | /* Looking around. Are we still alive? |
| 4347 | * OK, link new skb, drop old one */ |
| 4348 | |
| 4349 | skb2->next = skb1->next; |
| 4350 | *skb_p = skb2; |
| 4351 | kfree_skb(skb1); |
| 4352 | skb1 = skb2; |
| 4353 | } |
| 4354 | elt++; |
| 4355 | *trailer = skb1; |
| 4356 | skb_p = &skb1->next; |
| 4357 | } |
| 4358 | |
| 4359 | return elt; |
| 4360 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 4361 | EXPORT_SYMBOL_GPL(skb_cow_data); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4362 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4363 | static void sock_rmem_free(struct sk_buff *skb) |
| 4364 | { |
| 4365 | struct sock *sk = skb->sk; |
| 4366 | |
| 4367 | atomic_sub(skb->truesize, &sk->sk_rmem_alloc); |
| 4368 | } |
| 4369 | |
Soheil Hassas Yeganeh | 8605330a | 2017-03-18 17:02:59 -0400 | [diff] [blame] | 4370 | static void skb_set_err_queue(struct sk_buff *skb) |
| 4371 | { |
| 4372 | /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. |
| 4373 | * So, it is safe to (mis)use it to mark skbs on the error queue. |
| 4374 | */ |
| 4375 | skb->pkt_type = PACKET_OUTGOING; |
| 4376 | BUILD_BUG_ON(PACKET_OUTGOING == 0); |
| 4377 | } |
| 4378 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4379 | /* |
| 4380 | * Note: We dont mem charge error packets (no sk_forward_alloc changes) |
| 4381 | */ |
| 4382 | int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) |
| 4383 | { |
| 4384 | if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4385 | (unsigned int)sk->sk_rcvbuf) |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4386 | return -ENOMEM; |
| 4387 | |
| 4388 | skb_orphan(skb); |
| 4389 | skb->sk = sk; |
| 4390 | skb->destructor = sock_rmem_free; |
| 4391 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); |
Soheil Hassas Yeganeh | 8605330a | 2017-03-18 17:02:59 -0400 | [diff] [blame] | 4392 | skb_set_err_queue(skb); |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4393 | |
Eric Dumazet | abb57ea | 2011-05-18 02:21:31 -0400 | [diff] [blame] | 4394 | /* before exiting rcu section, make sure dst is refcounted */ |
| 4395 | skb_dst_force(skb); |
| 4396 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4397 | skb_queue_tail(&sk->sk_error_queue, skb); |
| 4398 | if (!sock_flag(sk, SOCK_DEAD)) |
Vinicius Costa Gomes | 6e5d58f | 2018-03-14 13:32:09 -0700 | [diff] [blame] | 4399 | sk->sk_error_report(sk); |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4400 | return 0; |
| 4401 | } |
| 4402 | EXPORT_SYMBOL(sock_queue_err_skb); |
| 4403 | |
Soheil Hassas Yeganeh | 83a1a1a | 2016-11-30 14:01:08 -0500 | [diff] [blame] | 4404 | static bool is_icmp_err_skb(const struct sk_buff *skb) |
| 4405 | { |
| 4406 | return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || |
| 4407 | SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); |
| 4408 | } |
| 4409 | |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 4410 | struct sk_buff *sock_dequeue_err_skb(struct sock *sk) |
| 4411 | { |
| 4412 | struct sk_buff_head *q = &sk->sk_error_queue; |
Soheil Hassas Yeganeh | 83a1a1a | 2016-11-30 14:01:08 -0500 | [diff] [blame] | 4413 | struct sk_buff *skb, *skb_next = NULL; |
| 4414 | bool icmp_next = false; |
Eric Dumazet | 997d5c3 | 2015-02-18 05:47:55 -0800 | [diff] [blame] | 4415 | unsigned long flags; |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 4416 | |
Eric Dumazet | 997d5c3 | 2015-02-18 05:47:55 -0800 | [diff] [blame] | 4417 | spin_lock_irqsave(&q->lock, flags); |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 4418 | skb = __skb_dequeue(q); |
Soheil Hassas Yeganeh | 38b2579 | 2017-06-02 12:38:22 -0400 | [diff] [blame] | 4419 | if (skb && (skb_next = skb_peek(q))) { |
Soheil Hassas Yeganeh | 83a1a1a | 2016-11-30 14:01:08 -0500 | [diff] [blame] | 4420 | icmp_next = is_icmp_err_skb(skb_next); |
Soheil Hassas Yeganeh | 38b2579 | 2017-06-02 12:38:22 -0400 | [diff] [blame] | 4421 | if (icmp_next) |
| 4422 | sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin; |
| 4423 | } |
Eric Dumazet | 997d5c3 | 2015-02-18 05:47:55 -0800 | [diff] [blame] | 4424 | spin_unlock_irqrestore(&q->lock, flags); |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 4425 | |
Soheil Hassas Yeganeh | 83a1a1a | 2016-11-30 14:01:08 -0500 | [diff] [blame] | 4426 | if (is_icmp_err_skb(skb) && !icmp_next) |
| 4427 | sk->sk_err = 0; |
| 4428 | |
| 4429 | if (skb_next) |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 4430 | sk->sk_error_report(sk); |
| 4431 | |
| 4432 | return skb; |
| 4433 | } |
| 4434 | EXPORT_SYMBOL(sock_dequeue_err_skb); |
| 4435 | |
Alexander Duyck | cab41c4 | 2014-09-10 18:05:26 -0400 | [diff] [blame] | 4436 | /** |
| 4437 | * skb_clone_sk - create clone of skb, and take reference to socket |
| 4438 | * @skb: the skb to clone |
| 4439 | * |
| 4440 | * This function creates a clone of a buffer that holds a reference on |
| 4441 | * sk_refcnt. Buffers created via this function are meant to be |
| 4442 | * returned using sock_queue_err_skb, or free via kfree_skb. |
| 4443 | * |
| 4444 | * When passing buffers allocated with this function to sock_queue_err_skb |
| 4445 | * it is necessary to wrap the call with sock_hold/sock_put in order to |
| 4446 | * prevent the socket from being released prior to being enqueued on |
| 4447 | * the sk_error_queue. |
| 4448 | */ |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 4449 | struct sk_buff *skb_clone_sk(struct sk_buff *skb) |
| 4450 | { |
| 4451 | struct sock *sk = skb->sk; |
| 4452 | struct sk_buff *clone; |
| 4453 | |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 4454 | if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 4455 | return NULL; |
| 4456 | |
| 4457 | clone = skb_clone(skb, GFP_ATOMIC); |
| 4458 | if (!clone) { |
| 4459 | sock_put(sk); |
| 4460 | return NULL; |
| 4461 | } |
| 4462 | |
| 4463 | clone->sk = sk; |
| 4464 | clone->destructor = sock_efree; |
| 4465 | |
| 4466 | return clone; |
| 4467 | } |
| 4468 | EXPORT_SYMBOL(skb_clone_sk); |
| 4469 | |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4470 | static void __skb_complete_tx_timestamp(struct sk_buff *skb, |
| 4471 | struct sock *sk, |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4472 | int tstype, |
| 4473 | bool opt_stats) |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4474 | { |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4475 | struct sock_exterr_skb *serr; |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4476 | int err; |
| 4477 | |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4478 | BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); |
| 4479 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4480 | serr = SKB_EXT_ERR(skb); |
| 4481 | memset(serr, 0, sizeof(*serr)); |
| 4482 | serr->ee.ee_errno = ENOMSG; |
| 4483 | serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; |
Willem de Bruijn | e7fd288 | 2014-08-04 22:11:48 -0400 | [diff] [blame] | 4484 | serr->ee.ee_info = tstype; |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4485 | serr->opt_stats = opt_stats; |
Willem de Bruijn | 1862d62 | 2017-04-12 19:24:35 -0400 | [diff] [blame] | 4486 | serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; |
Willem de Bruijn | 4ed2d76 | 2014-08-04 22:11:49 -0400 | [diff] [blame] | 4487 | if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { |
Willem de Bruijn | 09c2d25 | 2014-08-04 22:11:47 -0400 | [diff] [blame] | 4488 | serr->ee.ee_data = skb_shinfo(skb)->tskey; |
WANG Cong | ac5cc97 | 2015-12-16 23:39:04 -0800 | [diff] [blame] | 4489 | if (sk->sk_protocol == IPPROTO_TCP && |
| 4490 | sk->sk_type == SOCK_STREAM) |
Willem de Bruijn | 4ed2d76 | 2014-08-04 22:11:49 -0400 | [diff] [blame] | 4491 | serr->ee.ee_data -= sk->sk_tskey; |
| 4492 | } |
Eric Dumazet | 2903037 | 2010-05-29 00:20:48 -0700 | [diff] [blame] | 4493 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4494 | err = sock_queue_err_skb(sk, skb); |
Eric Dumazet | 2903037 | 2010-05-29 00:20:48 -0700 | [diff] [blame] | 4495 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4496 | if (err) |
| 4497 | kfree_skb(skb); |
| 4498 | } |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4499 | |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 4500 | static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) |
| 4501 | { |
| 4502 | bool ret; |
| 4503 | |
| 4504 | if (likely(sysctl_tstamp_allow_data || tsonly)) |
| 4505 | return true; |
| 4506 | |
| 4507 | read_lock_bh(&sk->sk_callback_lock); |
| 4508 | ret = sk->sk_socket && sk->sk_socket->file && |
| 4509 | file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); |
| 4510 | read_unlock_bh(&sk->sk_callback_lock); |
| 4511 | return ret; |
| 4512 | } |
| 4513 | |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4514 | void skb_complete_tx_timestamp(struct sk_buff *skb, |
| 4515 | struct skb_shared_hwtstamps *hwtstamps) |
| 4516 | { |
| 4517 | struct sock *sk = skb->sk; |
| 4518 | |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 4519 | if (!skb_may_tx_timestamp(sk, false)) |
Willem de Bruijn | 35b99df | 2017-12-13 14:41:06 -0500 | [diff] [blame] | 4520 | goto err; |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 4521 | |
Eric Dumazet | 9ac25fc | 2017-03-03 21:01:03 -0800 | [diff] [blame] | 4522 | /* Take a reference to prevent skb_orphan() from freeing the socket, |
| 4523 | * but only if the socket refcount is not zero. |
| 4524 | */ |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 4525 | if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { |
Eric Dumazet | 9ac25fc | 2017-03-03 21:01:03 -0800 | [diff] [blame] | 4526 | *skb_hwtstamps(skb) = *hwtstamps; |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4527 | __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); |
Eric Dumazet | 9ac25fc | 2017-03-03 21:01:03 -0800 | [diff] [blame] | 4528 | sock_put(sk); |
Willem de Bruijn | 35b99df | 2017-12-13 14:41:06 -0500 | [diff] [blame] | 4529 | return; |
Eric Dumazet | 9ac25fc | 2017-03-03 21:01:03 -0800 | [diff] [blame] | 4530 | } |
Willem de Bruijn | 35b99df | 2017-12-13 14:41:06 -0500 | [diff] [blame] | 4531 | |
| 4532 | err: |
| 4533 | kfree_skb(skb); |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4534 | } |
| 4535 | EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); |
| 4536 | |
| 4537 | void __skb_tstamp_tx(struct sk_buff *orig_skb, |
| 4538 | struct skb_shared_hwtstamps *hwtstamps, |
| 4539 | struct sock *sk, int tstype) |
| 4540 | { |
| 4541 | struct sk_buff *skb; |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4542 | bool tsonly, opt_stats = false; |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4543 | |
Willem de Bruijn | 3a8dd97 | 2015-03-11 15:43:55 -0400 | [diff] [blame] | 4544 | if (!sk) |
| 4545 | return; |
| 4546 | |
Miroslav Lichvar | b50a5c7 | 2017-05-19 17:52:40 +0200 | [diff] [blame] | 4547 | if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && |
| 4548 | skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) |
| 4549 | return; |
| 4550 | |
Willem de Bruijn | 3a8dd97 | 2015-03-11 15:43:55 -0400 | [diff] [blame] | 4551 | tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; |
| 4552 | if (!skb_may_tx_timestamp(sk, tsonly)) |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4553 | return; |
| 4554 | |
Francis Yan | 1c88580 | 2016-11-27 23:07:18 -0800 | [diff] [blame] | 4555 | if (tsonly) { |
| 4556 | #ifdef CONFIG_INET |
| 4557 | if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && |
| 4558 | sk->sk_protocol == IPPROTO_TCP && |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4559 | sk->sk_type == SOCK_STREAM) { |
Francis Yan | 1c88580 | 2016-11-27 23:07:18 -0800 | [diff] [blame] | 4560 | skb = tcp_get_timestamping_opt_stats(sk); |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4561 | opt_stats = true; |
| 4562 | } else |
Francis Yan | 1c88580 | 2016-11-27 23:07:18 -0800 | [diff] [blame] | 4563 | #endif |
| 4564 | skb = alloc_skb(0, GFP_ATOMIC); |
| 4565 | } else { |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 4566 | skb = skb_clone(orig_skb, GFP_ATOMIC); |
Francis Yan | 1c88580 | 2016-11-27 23:07:18 -0800 | [diff] [blame] | 4567 | } |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4568 | if (!skb) |
| 4569 | return; |
| 4570 | |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 4571 | if (tsonly) { |
Willem de Bruijn | fff8803 | 2017-06-08 11:35:03 -0400 | [diff] [blame] | 4572 | skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & |
| 4573 | SKBTX_ANY_TSTAMP; |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 4574 | skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; |
| 4575 | } |
| 4576 | |
| 4577 | if (hwtstamps) |
| 4578 | *skb_hwtstamps(skb) = *hwtstamps; |
| 4579 | else |
| 4580 | skb->tstamp = ktime_get_real(); |
| 4581 | |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4582 | __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4583 | } |
Willem de Bruijn | e7fd288 | 2014-08-04 22:11:48 -0400 | [diff] [blame] | 4584 | EXPORT_SYMBOL_GPL(__skb_tstamp_tx); |
| 4585 | |
| 4586 | void skb_tstamp_tx(struct sk_buff *orig_skb, |
| 4587 | struct skb_shared_hwtstamps *hwtstamps) |
| 4588 | { |
| 4589 | return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, |
| 4590 | SCM_TSTAMP_SND); |
| 4591 | } |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4592 | EXPORT_SYMBOL_GPL(skb_tstamp_tx); |
| 4593 | |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 4594 | void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) |
| 4595 | { |
| 4596 | struct sock *sk = skb->sk; |
| 4597 | struct sock_exterr_skb *serr; |
Eric Dumazet | dd4f107 | 2017-03-03 21:01:02 -0800 | [diff] [blame] | 4598 | int err = 1; |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 4599 | |
| 4600 | skb->wifi_acked_valid = 1; |
| 4601 | skb->wifi_acked = acked; |
| 4602 | |
| 4603 | serr = SKB_EXT_ERR(skb); |
| 4604 | memset(serr, 0, sizeof(*serr)); |
| 4605 | serr->ee.ee_errno = ENOMSG; |
| 4606 | serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; |
| 4607 | |
Eric Dumazet | dd4f107 | 2017-03-03 21:01:02 -0800 | [diff] [blame] | 4608 | /* Take a reference to prevent skb_orphan() from freeing the socket, |
| 4609 | * but only if the socket refcount is not zero. |
| 4610 | */ |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 4611 | if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { |
Eric Dumazet | dd4f107 | 2017-03-03 21:01:02 -0800 | [diff] [blame] | 4612 | err = sock_queue_err_skb(sk, skb); |
| 4613 | sock_put(sk); |
| 4614 | } |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 4615 | if (err) |
| 4616 | kfree_skb(skb); |
| 4617 | } |
| 4618 | EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); |
| 4619 | |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 4620 | /** |
| 4621 | * skb_partial_csum_set - set up and verify partial csum values for packet |
| 4622 | * @skb: the skb to set |
| 4623 | * @start: the number of bytes after skb->data to start checksumming. |
| 4624 | * @off: the offset from start to place the checksum. |
| 4625 | * |
| 4626 | * For untrusted partially-checksummed packets, we need to make sure the values |
| 4627 | * for skb->csum_start and skb->csum_offset are valid so we don't oops. |
| 4628 | * |
| 4629 | * This function checks and sets those values and skb->ip_summed: if this |
| 4630 | * returns false you should drop the packet. |
| 4631 | */ |
| 4632 | bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) |
| 4633 | { |
Eric Dumazet | 52b5d6f | 2018-10-10 06:59:35 -0700 | [diff] [blame] | 4634 | u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); |
| 4635 | u32 csum_start = skb_headroom(skb) + (u32)start; |
| 4636 | |
| 4637 | if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { |
| 4638 | net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", |
| 4639 | start, off, skb_headroom(skb), skb_headlen(skb)); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 4640 | return false; |
| 4641 | } |
| 4642 | skb->ip_summed = CHECKSUM_PARTIAL; |
Eric Dumazet | 52b5d6f | 2018-10-10 06:59:35 -0700 | [diff] [blame] | 4643 | skb->csum_start = csum_start; |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 4644 | skb->csum_offset = off; |
Jason Wang | e5d5dec | 2013-03-26 23:11:20 +0000 | [diff] [blame] | 4645 | skb_set_transport_header(skb, start); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 4646 | return true; |
| 4647 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 4648 | EXPORT_SYMBOL_GPL(skb_partial_csum_set); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 4649 | |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4650 | static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, |
| 4651 | unsigned int max) |
| 4652 | { |
| 4653 | if (skb_headlen(skb) >= len) |
| 4654 | return 0; |
| 4655 | |
| 4656 | /* If we need to pullup then pullup to the max, so we |
| 4657 | * won't need to do it again. |
| 4658 | */ |
| 4659 | if (max > skb->len) |
| 4660 | max = skb->len; |
| 4661 | |
| 4662 | if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) |
| 4663 | return -ENOMEM; |
| 4664 | |
| 4665 | if (skb_headlen(skb) < len) |
| 4666 | return -EPROTO; |
| 4667 | |
| 4668 | return 0; |
| 4669 | } |
| 4670 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4671 | #define MAX_TCP_HDR_LEN (15 * 4) |
| 4672 | |
| 4673 | static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, |
| 4674 | typeof(IPPROTO_IP) proto, |
| 4675 | unsigned int off) |
| 4676 | { |
| 4677 | switch (proto) { |
| 4678 | int err; |
| 4679 | |
| 4680 | case IPPROTO_TCP: |
| 4681 | err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), |
| 4682 | off + MAX_TCP_HDR_LEN); |
| 4683 | if (!err && !skb_partial_csum_set(skb, off, |
| 4684 | offsetof(struct tcphdr, |
| 4685 | check))) |
| 4686 | err = -EPROTO; |
| 4687 | return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; |
| 4688 | |
| 4689 | case IPPROTO_UDP: |
| 4690 | err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), |
| 4691 | off + sizeof(struct udphdr)); |
| 4692 | if (!err && !skb_partial_csum_set(skb, off, |
| 4693 | offsetof(struct udphdr, |
| 4694 | check))) |
| 4695 | err = -EPROTO; |
| 4696 | return err ? ERR_PTR(err) : &udp_hdr(skb)->check; |
| 4697 | } |
| 4698 | |
| 4699 | return ERR_PTR(-EPROTO); |
| 4700 | } |
| 4701 | |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4702 | /* This value should be large enough to cover a tagged ethernet header plus |
| 4703 | * maximally sized IP and TCP or UDP headers. |
| 4704 | */ |
| 4705 | #define MAX_IP_HDR_LEN 128 |
| 4706 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4707 | static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4708 | { |
| 4709 | unsigned int off; |
| 4710 | bool fragment; |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4711 | __sum16 *csum; |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4712 | int err; |
| 4713 | |
| 4714 | fragment = false; |
| 4715 | |
| 4716 | err = skb_maybe_pull_tail(skb, |
| 4717 | sizeof(struct iphdr), |
| 4718 | MAX_IP_HDR_LEN); |
| 4719 | if (err < 0) |
| 4720 | goto out; |
| 4721 | |
| 4722 | if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) |
| 4723 | fragment = true; |
| 4724 | |
| 4725 | off = ip_hdrlen(skb); |
| 4726 | |
| 4727 | err = -EPROTO; |
| 4728 | |
| 4729 | if (fragment) |
| 4730 | goto out; |
| 4731 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4732 | csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); |
| 4733 | if (IS_ERR(csum)) |
| 4734 | return PTR_ERR(csum); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4735 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4736 | if (recalculate) |
| 4737 | *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 4738 | ip_hdr(skb)->daddr, |
| 4739 | skb->len - off, |
| 4740 | ip_hdr(skb)->protocol, 0); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4741 | err = 0; |
| 4742 | |
| 4743 | out: |
| 4744 | return err; |
| 4745 | } |
| 4746 | |
| 4747 | /* This value should be large enough to cover a tagged ethernet header plus |
| 4748 | * an IPv6 header, all options, and a maximal TCP or UDP header. |
| 4749 | */ |
| 4750 | #define MAX_IPV6_HDR_LEN 256 |
| 4751 | |
| 4752 | #define OPT_HDR(type, skb, off) \ |
| 4753 | (type *)(skb_network_header(skb) + (off)) |
| 4754 | |
| 4755 | static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) |
| 4756 | { |
| 4757 | int err; |
| 4758 | u8 nexthdr; |
| 4759 | unsigned int off; |
| 4760 | unsigned int len; |
| 4761 | bool fragment; |
| 4762 | bool done; |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4763 | __sum16 *csum; |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4764 | |
| 4765 | fragment = false; |
| 4766 | done = false; |
| 4767 | |
| 4768 | off = sizeof(struct ipv6hdr); |
| 4769 | |
| 4770 | err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); |
| 4771 | if (err < 0) |
| 4772 | goto out; |
| 4773 | |
| 4774 | nexthdr = ipv6_hdr(skb)->nexthdr; |
| 4775 | |
| 4776 | len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); |
| 4777 | while (off <= len && !done) { |
| 4778 | switch (nexthdr) { |
| 4779 | case IPPROTO_DSTOPTS: |
| 4780 | case IPPROTO_HOPOPTS: |
| 4781 | case IPPROTO_ROUTING: { |
| 4782 | struct ipv6_opt_hdr *hp; |
| 4783 | |
| 4784 | err = skb_maybe_pull_tail(skb, |
| 4785 | off + |
| 4786 | sizeof(struct ipv6_opt_hdr), |
| 4787 | MAX_IPV6_HDR_LEN); |
| 4788 | if (err < 0) |
| 4789 | goto out; |
| 4790 | |
| 4791 | hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); |
| 4792 | nexthdr = hp->nexthdr; |
| 4793 | off += ipv6_optlen(hp); |
| 4794 | break; |
| 4795 | } |
| 4796 | case IPPROTO_AH: { |
| 4797 | struct ip_auth_hdr *hp; |
| 4798 | |
| 4799 | err = skb_maybe_pull_tail(skb, |
| 4800 | off + |
| 4801 | sizeof(struct ip_auth_hdr), |
| 4802 | MAX_IPV6_HDR_LEN); |
| 4803 | if (err < 0) |
| 4804 | goto out; |
| 4805 | |
| 4806 | hp = OPT_HDR(struct ip_auth_hdr, skb, off); |
| 4807 | nexthdr = hp->nexthdr; |
| 4808 | off += ipv6_authlen(hp); |
| 4809 | break; |
| 4810 | } |
| 4811 | case IPPROTO_FRAGMENT: { |
| 4812 | struct frag_hdr *hp; |
| 4813 | |
| 4814 | err = skb_maybe_pull_tail(skb, |
| 4815 | off + |
| 4816 | sizeof(struct frag_hdr), |
| 4817 | MAX_IPV6_HDR_LEN); |
| 4818 | if (err < 0) |
| 4819 | goto out; |
| 4820 | |
| 4821 | hp = OPT_HDR(struct frag_hdr, skb, off); |
| 4822 | |
| 4823 | if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) |
| 4824 | fragment = true; |
| 4825 | |
| 4826 | nexthdr = hp->nexthdr; |
| 4827 | off += sizeof(struct frag_hdr); |
| 4828 | break; |
| 4829 | } |
| 4830 | default: |
| 4831 | done = true; |
| 4832 | break; |
| 4833 | } |
| 4834 | } |
| 4835 | |
| 4836 | err = -EPROTO; |
| 4837 | |
| 4838 | if (!done || fragment) |
| 4839 | goto out; |
| 4840 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4841 | csum = skb_checksum_setup_ip(skb, nexthdr, off); |
| 4842 | if (IS_ERR(csum)) |
| 4843 | return PTR_ERR(csum); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4844 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4845 | if (recalculate) |
| 4846 | *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 4847 | &ipv6_hdr(skb)->daddr, |
| 4848 | skb->len - off, nexthdr, 0); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4849 | err = 0; |
| 4850 | |
| 4851 | out: |
| 4852 | return err; |
| 4853 | } |
| 4854 | |
| 4855 | /** |
| 4856 | * skb_checksum_setup - set up partial checksum offset |
| 4857 | * @skb: the skb to set up |
| 4858 | * @recalculate: if true the pseudo-header checksum will be recalculated |
| 4859 | */ |
| 4860 | int skb_checksum_setup(struct sk_buff *skb, bool recalculate) |
| 4861 | { |
| 4862 | int err; |
| 4863 | |
| 4864 | switch (skb->protocol) { |
| 4865 | case htons(ETH_P_IP): |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4866 | err = skb_checksum_setup_ipv4(skb, recalculate); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4867 | break; |
| 4868 | |
| 4869 | case htons(ETH_P_IPV6): |
| 4870 | err = skb_checksum_setup_ipv6(skb, recalculate); |
| 4871 | break; |
| 4872 | |
| 4873 | default: |
| 4874 | err = -EPROTO; |
| 4875 | break; |
| 4876 | } |
| 4877 | |
| 4878 | return err; |
| 4879 | } |
| 4880 | EXPORT_SYMBOL(skb_checksum_setup); |
| 4881 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4882 | /** |
| 4883 | * skb_checksum_maybe_trim - maybe trims the given skb |
| 4884 | * @skb: the skb to check |
| 4885 | * @transport_len: the data length beyond the network header |
| 4886 | * |
| 4887 | * Checks whether the given skb has data beyond the given transport length. |
| 4888 | * If so, returns a cloned skb trimmed to this transport length. |
| 4889 | * Otherwise returns the provided skb. Returns NULL in error cases |
| 4890 | * (e.g. transport_len exceeds skb length or out-of-memory). |
| 4891 | * |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 4892 | * Caller needs to set the skb transport header and free any returned skb if it |
| 4893 | * differs from the provided skb. |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4894 | */ |
| 4895 | static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, |
| 4896 | unsigned int transport_len) |
| 4897 | { |
| 4898 | struct sk_buff *skb_chk; |
| 4899 | unsigned int len = skb_transport_offset(skb) + transport_len; |
| 4900 | int ret; |
| 4901 | |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 4902 | if (skb->len < len) |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4903 | return NULL; |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 4904 | else if (skb->len == len) |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4905 | return skb; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4906 | |
| 4907 | skb_chk = skb_clone(skb, GFP_ATOMIC); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4908 | if (!skb_chk) |
| 4909 | return NULL; |
| 4910 | |
| 4911 | ret = pskb_trim_rcsum(skb_chk, len); |
| 4912 | if (ret) { |
| 4913 | kfree_skb(skb_chk); |
| 4914 | return NULL; |
| 4915 | } |
| 4916 | |
| 4917 | return skb_chk; |
| 4918 | } |
| 4919 | |
| 4920 | /** |
| 4921 | * skb_checksum_trimmed - validate checksum of an skb |
| 4922 | * @skb: the skb to check |
| 4923 | * @transport_len: the data length beyond the network header |
| 4924 | * @skb_chkf: checksum function to use |
| 4925 | * |
| 4926 | * Applies the given checksum function skb_chkf to the provided skb. |
| 4927 | * Returns a checked and maybe trimmed skb. Returns NULL on error. |
| 4928 | * |
| 4929 | * If the skb has data beyond the given transport length, then a |
| 4930 | * trimmed & cloned skb is checked and returned. |
| 4931 | * |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 4932 | * Caller needs to set the skb transport header and free any returned skb if it |
| 4933 | * differs from the provided skb. |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4934 | */ |
| 4935 | struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, |
| 4936 | unsigned int transport_len, |
| 4937 | __sum16(*skb_chkf)(struct sk_buff *skb)) |
| 4938 | { |
| 4939 | struct sk_buff *skb_chk; |
| 4940 | unsigned int offset = skb_transport_offset(skb); |
Linus Lüssing | fcba67c | 2015-05-05 00:19:35 +0200 | [diff] [blame] | 4941 | __sum16 ret; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4942 | |
| 4943 | skb_chk = skb_checksum_maybe_trim(skb, transport_len); |
| 4944 | if (!skb_chk) |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 4945 | goto err; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4946 | |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 4947 | if (!pskb_may_pull(skb_chk, offset)) |
| 4948 | goto err; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4949 | |
Linus Lüssing | 9b36881 | 2016-02-24 04:21:42 +0100 | [diff] [blame] | 4950 | skb_pull_rcsum(skb_chk, offset); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4951 | ret = skb_chkf(skb_chk); |
Linus Lüssing | 9b36881 | 2016-02-24 04:21:42 +0100 | [diff] [blame] | 4952 | skb_push_rcsum(skb_chk, offset); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4953 | |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 4954 | if (ret) |
| 4955 | goto err; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4956 | |
| 4957 | return skb_chk; |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 4958 | |
| 4959 | err: |
| 4960 | if (skb_chk && skb_chk != skb) |
| 4961 | kfree_skb(skb_chk); |
| 4962 | |
| 4963 | return NULL; |
| 4964 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 4965 | } |
| 4966 | EXPORT_SYMBOL(skb_checksum_trimmed); |
| 4967 | |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 4968 | void __skb_warn_lro_forwarding(const struct sk_buff *skb) |
| 4969 | { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 4970 | net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", |
| 4971 | skb->dev->name); |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 4972 | } |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 4973 | EXPORT_SYMBOL(__skb_warn_lro_forwarding); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4974 | |
| 4975 | void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) |
| 4976 | { |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 4977 | if (head_stolen) { |
| 4978 | skb_release_head_state(skb); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4979 | kmem_cache_free(skbuff_head_cache, skb); |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 4980 | } else { |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4981 | __kfree_skb(skb); |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 4982 | } |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4983 | } |
| 4984 | EXPORT_SYMBOL(kfree_skb_partial); |
| 4985 | |
| 4986 | /** |
| 4987 | * skb_try_coalesce - try to merge skb to prior one |
| 4988 | * @to: prior buffer |
| 4989 | * @from: buffer to add |
| 4990 | * @fragstolen: pointer to boolean |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 4991 | * @delta_truesize: how much more was allocated than was requested |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4992 | */ |
| 4993 | bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, |
| 4994 | bool *fragstolen, int *delta_truesize) |
| 4995 | { |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 4996 | struct skb_shared_info *to_shinfo, *from_shinfo; |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4997 | int i, delta, len = from->len; |
| 4998 | |
| 4999 | *fragstolen = false; |
| 5000 | |
| 5001 | if (skb_cloned(to)) |
| 5002 | return false; |
| 5003 | |
| 5004 | if (len <= skb_tailroom(to)) { |
Eric Dumazet | e93a043 | 2014-09-15 04:19:52 -0700 | [diff] [blame] | 5005 | if (len) |
| 5006 | BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5007 | *delta_truesize = 0; |
| 5008 | return true; |
| 5009 | } |
| 5010 | |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5011 | to_shinfo = skb_shinfo(to); |
| 5012 | from_shinfo = skb_shinfo(from); |
| 5013 | if (to_shinfo->frag_list || from_shinfo->frag_list) |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5014 | return false; |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 5015 | if (skb_zcopy(to) || skb_zcopy(from)) |
| 5016 | return false; |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5017 | |
| 5018 | if (skb_headlen(from) != 0) { |
| 5019 | struct page *page; |
| 5020 | unsigned int offset; |
| 5021 | |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5022 | if (to_shinfo->nr_frags + |
| 5023 | from_shinfo->nr_frags >= MAX_SKB_FRAGS) |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5024 | return false; |
| 5025 | |
| 5026 | if (skb_head_is_locked(from)) |
| 5027 | return false; |
| 5028 | |
| 5029 | delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
| 5030 | |
| 5031 | page = virt_to_head_page(from->head); |
| 5032 | offset = from->data - (unsigned char *)page_address(page); |
| 5033 | |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5034 | skb_fill_page_desc(to, to_shinfo->nr_frags, |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5035 | page, offset, skb_headlen(from)); |
| 5036 | *fragstolen = true; |
| 5037 | } else { |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5038 | if (to_shinfo->nr_frags + |
| 5039 | from_shinfo->nr_frags > MAX_SKB_FRAGS) |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5040 | return false; |
| 5041 | |
Weiping Pan | f4b549a | 2012-09-28 20:15:30 +0000 | [diff] [blame] | 5042 | delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | WARN_ON_ONCE(delta < len); |
| 5046 | |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5047 | memcpy(to_shinfo->frags + to_shinfo->nr_frags, |
| 5048 | from_shinfo->frags, |
| 5049 | from_shinfo->nr_frags * sizeof(skb_frag_t)); |
| 5050 | to_shinfo->nr_frags += from_shinfo->nr_frags; |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5051 | |
| 5052 | if (!skb_cloned(from)) |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5053 | from_shinfo->nr_frags = 0; |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5054 | |
Li RongQing | 8ea853f | 2012-09-18 16:53:21 +0000 | [diff] [blame] | 5055 | /* if the skb is not cloned this does nothing |
| 5056 | * since we set nr_frags to 0. |
| 5057 | */ |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5058 | for (i = 0; i < from_shinfo->nr_frags; i++) |
| 5059 | __skb_frag_ref(&from_shinfo->frags[i]); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5060 | |
| 5061 | to->truesize += delta; |
| 5062 | to->len += len; |
| 5063 | to->data_len += len; |
| 5064 | |
| 5065 | *delta_truesize = delta; |
| 5066 | return true; |
| 5067 | } |
| 5068 | EXPORT_SYMBOL(skb_try_coalesce); |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5069 | |
| 5070 | /** |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 5071 | * skb_scrub_packet - scrub an skb |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5072 | * |
| 5073 | * @skb: buffer to clean |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 5074 | * @xnet: packet is crossing netns |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5075 | * |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 5076 | * skb_scrub_packet can be used after encapsulating or decapsulting a packet |
| 5077 | * into/from a tunnel. Some information have to be cleared during these |
| 5078 | * operations. |
| 5079 | * skb_scrub_packet can also be used to clean a skb before injecting it in |
| 5080 | * another namespace (@xnet == true). We have to clear all information in the |
| 5081 | * skb that could impact namespace isolation. |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5082 | */ |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 5083 | void skb_scrub_packet(struct sk_buff *skb, bool xnet) |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5084 | { |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5085 | skb->pkt_type = PACKET_HOST; |
| 5086 | skb->skb_iif = 0; |
WANG Cong | 60ff746 | 2014-05-04 16:39:18 -0700 | [diff] [blame] | 5087 | skb->ignore_df = 0; |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5088 | skb_dst_drop(skb); |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5089 | secpath_reset(skb); |
| 5090 | nf_reset(skb); |
| 5091 | nf_reset_trace(skb); |
Herbert Xu | 213dd74 | 2015-04-16 09:03:27 +0800 | [diff] [blame] | 5092 | |
Petr Machata | 6f9a506 | 2018-11-19 16:11:07 +0000 | [diff] [blame] | 5093 | #ifdef CONFIG_NET_SWITCHDEV |
| 5094 | skb->offload_fwd_mark = 0; |
Ido Schimmel | 875e893 | 2018-12-04 08:15:10 +0000 | [diff] [blame] | 5095 | skb->offload_l3_fwd_mark = 0; |
Petr Machata | 6f9a506 | 2018-11-19 16:11:07 +0000 | [diff] [blame] | 5096 | #endif |
| 5097 | |
Herbert Xu | 213dd74 | 2015-04-16 09:03:27 +0800 | [diff] [blame] | 5098 | if (!xnet) |
| 5099 | return; |
| 5100 | |
Ye Yin | 2b5ec1a | 2017-10-26 16:57:05 +0800 | [diff] [blame] | 5101 | ipvs_reset(skb); |
Herbert Xu | 213dd74 | 2015-04-16 09:03:27 +0800 | [diff] [blame] | 5102 | skb->mark = 0; |
Jesus Sanchez-Palencia | c47d8c2 | 2018-07-03 15:42:47 -0700 | [diff] [blame] | 5103 | skb->tstamp = 0; |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5104 | } |
| 5105 | EXPORT_SYMBOL_GPL(skb_scrub_packet); |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 5106 | |
| 5107 | /** |
| 5108 | * skb_gso_transport_seglen - Return length of individual segments of a gso packet |
| 5109 | * |
| 5110 | * @skb: GSO skb |
| 5111 | * |
| 5112 | * skb_gso_transport_seglen is used to determine the real size of the |
| 5113 | * individual segments, including Layer4 headers (TCP/UDP). |
| 5114 | * |
| 5115 | * The MAC/L2 or network (IP, IPv6) headers are not accounted for. |
| 5116 | */ |
Daniel Axtens | a4a7771 | 2018-03-01 17:13:40 +1100 | [diff] [blame] | 5117 | static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 5118 | { |
| 5119 | const struct skb_shared_info *shinfo = skb_shinfo(skb); |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 5120 | unsigned int thlen = 0; |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 5121 | |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 5122 | if (skb->encapsulation) { |
| 5123 | thlen = skb_inner_transport_header(skb) - |
| 5124 | skb_transport_header(skb); |
Florian Westphal | 6d39d58 | 2014-04-09 10:28:50 +0200 | [diff] [blame] | 5125 | |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 5126 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) |
| 5127 | thlen += inner_tcp_hdrlen(skb); |
| 5128 | } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
| 5129 | thlen = tcp_hdrlen(skb); |
Daniel Axtens | 1dd27cd | 2018-03-09 14:06:09 +1100 | [diff] [blame] | 5130 | } else if (unlikely(skb_is_gso_sctp(skb))) { |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 5131 | thlen = sizeof(struct sctphdr); |
Willem de Bruijn | ee80d1e | 2018-04-26 13:42:16 -0400 | [diff] [blame] | 5132 | } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { |
| 5133 | thlen = sizeof(struct udphdr); |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 5134 | } |
Florian Westphal | 6d39d58 | 2014-04-09 10:28:50 +0200 | [diff] [blame] | 5135 | /* UFO sets gso_size to the size of the fragmentation |
| 5136 | * payload, i.e. the size of the L4 (UDP) header is already |
| 5137 | * accounted for. |
| 5138 | */ |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 5139 | return thlen + shinfo->gso_size; |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 5140 | } |
Daniel Axtens | a4a7771 | 2018-03-01 17:13:40 +1100 | [diff] [blame] | 5141 | |
| 5142 | /** |
| 5143 | * skb_gso_network_seglen - Return length of individual segments of a gso packet |
| 5144 | * |
| 5145 | * @skb: GSO skb |
| 5146 | * |
| 5147 | * skb_gso_network_seglen is used to determine the real size of the |
| 5148 | * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). |
| 5149 | * |
| 5150 | * The MAC/L2 header is not accounted for. |
| 5151 | */ |
| 5152 | static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) |
| 5153 | { |
| 5154 | unsigned int hdr_len = skb_transport_header(skb) - |
| 5155 | skb_network_header(skb); |
| 5156 | |
| 5157 | return hdr_len + skb_gso_transport_seglen(skb); |
| 5158 | } |
| 5159 | |
| 5160 | /** |
| 5161 | * skb_gso_mac_seglen - Return length of individual segments of a gso packet |
| 5162 | * |
| 5163 | * @skb: GSO skb |
| 5164 | * |
| 5165 | * skb_gso_mac_seglen is used to determine the real size of the |
| 5166 | * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 |
| 5167 | * headers (TCP/UDP). |
| 5168 | */ |
| 5169 | static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) |
| 5170 | { |
| 5171 | unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); |
| 5172 | |
| 5173 | return hdr_len + skb_gso_transport_seglen(skb); |
| 5174 | } |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5175 | |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5176 | /** |
Daniel Axtens | 2b16f04 | 2018-01-31 14:15:33 +1100 | [diff] [blame] | 5177 | * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS |
| 5178 | * |
| 5179 | * There are a couple of instances where we have a GSO skb, and we |
| 5180 | * want to determine what size it would be after it is segmented. |
| 5181 | * |
| 5182 | * We might want to check: |
| 5183 | * - L3+L4+payload size (e.g. IP forwarding) |
| 5184 | * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) |
| 5185 | * |
| 5186 | * This is a helper to do that correctly considering GSO_BY_FRAGS. |
| 5187 | * |
Mathieu Malaterre | 49682bf | 2018-10-31 13:16:58 +0100 | [diff] [blame] | 5188 | * @skb: GSO skb |
| 5189 | * |
Daniel Axtens | 2b16f04 | 2018-01-31 14:15:33 +1100 | [diff] [blame] | 5190 | * @seg_len: The segmented length (from skb_gso_*_seglen). In the |
| 5191 | * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. |
| 5192 | * |
| 5193 | * @max_len: The maximum permissible length. |
| 5194 | * |
| 5195 | * Returns true if the segmented length <= max length. |
| 5196 | */ |
| 5197 | static inline bool skb_gso_size_check(const struct sk_buff *skb, |
| 5198 | unsigned int seg_len, |
| 5199 | unsigned int max_len) { |
| 5200 | const struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 5201 | const struct sk_buff *iter; |
| 5202 | |
| 5203 | if (shinfo->gso_size != GSO_BY_FRAGS) |
| 5204 | return seg_len <= max_len; |
| 5205 | |
| 5206 | /* Undo this so we can re-use header sizes */ |
| 5207 | seg_len -= GSO_BY_FRAGS; |
| 5208 | |
| 5209 | skb_walk_frags(skb, iter) { |
| 5210 | if (seg_len + skb_headlen(iter) > max_len) |
| 5211 | return false; |
| 5212 | } |
| 5213 | |
| 5214 | return true; |
| 5215 | } |
| 5216 | |
| 5217 | /** |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 5218 | * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5219 | * |
| 5220 | * @skb: GSO skb |
David S. Miller | 76f21b9 | 2016-06-03 22:56:28 -0700 | [diff] [blame] | 5221 | * @mtu: MTU to validate against |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5222 | * |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 5223 | * skb_gso_validate_network_len validates if a given skb will fit a |
| 5224 | * wanted MTU once split. It considers L3 headers, L4 headers, and the |
| 5225 | * payload. |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5226 | */ |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 5227 | bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5228 | { |
Daniel Axtens | 2b16f04 | 2018-01-31 14:15:33 +1100 | [diff] [blame] | 5229 | return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5230 | } |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 5231 | EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5232 | |
Daniel Axtens | 2b16f04 | 2018-01-31 14:15:33 +1100 | [diff] [blame] | 5233 | /** |
| 5234 | * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? |
| 5235 | * |
| 5236 | * @skb: GSO skb |
| 5237 | * @len: length to validate against |
| 5238 | * |
| 5239 | * skb_gso_validate_mac_len validates if a given skb will fit a wanted |
| 5240 | * length once split, including L2, L3 and L4 headers and the payload. |
| 5241 | */ |
| 5242 | bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) |
| 5243 | { |
| 5244 | return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); |
| 5245 | } |
| 5246 | EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); |
| 5247 | |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5248 | static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) |
| 5249 | { |
Yuya Kusakabe | d85e8be | 2019-04-16 10:22:28 +0900 | [diff] [blame] | 5250 | int mac_len, meta_len; |
| 5251 | void *meta; |
Toshiaki Makita | 4bbb3e0 | 2018-03-13 14:51:27 +0900 | [diff] [blame] | 5252 | |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5253 | if (skb_cow(skb, skb_headroom(skb)) < 0) { |
| 5254 | kfree_skb(skb); |
| 5255 | return NULL; |
| 5256 | } |
| 5257 | |
Toshiaki Makita | 4bbb3e0 | 2018-03-13 14:51:27 +0900 | [diff] [blame] | 5258 | mac_len = skb->data - skb_mac_header(skb); |
Toshiaki Makita | ae47457 | 2018-03-29 19:05:29 +0900 | [diff] [blame] | 5259 | if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { |
| 5260 | memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), |
| 5261 | mac_len - VLAN_HLEN - ETH_TLEN); |
| 5262 | } |
Yuya Kusakabe | d85e8be | 2019-04-16 10:22:28 +0900 | [diff] [blame] | 5263 | |
| 5264 | meta_len = skb_metadata_len(skb); |
| 5265 | if (meta_len) { |
| 5266 | meta = skb_metadata_end(skb) - meta_len; |
| 5267 | memmove(meta + VLAN_HLEN, meta, meta_len); |
| 5268 | } |
| 5269 | |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5270 | skb->mac_header += VLAN_HLEN; |
| 5271 | return skb; |
| 5272 | } |
| 5273 | |
| 5274 | struct sk_buff *skb_vlan_untag(struct sk_buff *skb) |
| 5275 | { |
| 5276 | struct vlan_hdr *vhdr; |
| 5277 | u16 vlan_tci; |
| 5278 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 5279 | if (unlikely(skb_vlan_tag_present(skb))) { |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5280 | /* vlan_tci is already set-up so leave this for another time */ |
| 5281 | return skb; |
| 5282 | } |
| 5283 | |
| 5284 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 5285 | if (unlikely(!skb)) |
| 5286 | goto err_free; |
| 5287 | |
| 5288 | if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) |
| 5289 | goto err_free; |
| 5290 | |
| 5291 | vhdr = (struct vlan_hdr *)skb->data; |
| 5292 | vlan_tci = ntohs(vhdr->h_vlan_TCI); |
| 5293 | __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); |
| 5294 | |
| 5295 | skb_pull_rcsum(skb, VLAN_HLEN); |
| 5296 | vlan_set_encap_proto(skb, vhdr); |
| 5297 | |
| 5298 | skb = skb_reorder_vlan_header(skb); |
| 5299 | if (unlikely(!skb)) |
| 5300 | goto err_free; |
| 5301 | |
| 5302 | skb_reset_network_header(skb); |
| 5303 | skb_reset_transport_header(skb); |
| 5304 | skb_reset_mac_len(skb); |
| 5305 | |
| 5306 | return skb; |
| 5307 | |
| 5308 | err_free: |
| 5309 | kfree_skb(skb); |
| 5310 | return NULL; |
| 5311 | } |
| 5312 | EXPORT_SYMBOL(skb_vlan_untag); |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5313 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 5314 | int skb_ensure_writable(struct sk_buff *skb, int write_len) |
| 5315 | { |
| 5316 | if (!pskb_may_pull(skb, write_len)) |
| 5317 | return -ENOMEM; |
| 5318 | |
| 5319 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
| 5320 | return 0; |
| 5321 | |
| 5322 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 5323 | } |
| 5324 | EXPORT_SYMBOL(skb_ensure_writable); |
| 5325 | |
Shmulik Ladkani | bfca4c5 | 2016-09-19 19:11:09 +0300 | [diff] [blame] | 5326 | /* remove VLAN header from packet and update csum accordingly. |
| 5327 | * expects a non skb_vlan_tag_present skb with a vlan tag payload |
| 5328 | */ |
| 5329 | int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5330 | { |
| 5331 | struct vlan_hdr *vhdr; |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5332 | int offset = skb->data - skb_mac_header(skb); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5333 | int err; |
| 5334 | |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5335 | if (WARN_ONCE(offset, |
| 5336 | "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", |
| 5337 | offset)) { |
| 5338 | return -EINVAL; |
| 5339 | } |
| 5340 | |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5341 | err = skb_ensure_writable(skb, VLAN_ETH_HLEN); |
| 5342 | if (unlikely(err)) |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5343 | return err; |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5344 | |
| 5345 | skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); |
| 5346 | |
| 5347 | vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); |
| 5348 | *vlan_tci = ntohs(vhdr->h_vlan_TCI); |
| 5349 | |
| 5350 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
| 5351 | __skb_pull(skb, VLAN_HLEN); |
| 5352 | |
| 5353 | vlan_set_encap_proto(skb, vhdr); |
| 5354 | skb->mac_header += VLAN_HLEN; |
| 5355 | |
| 5356 | if (skb_network_offset(skb) < ETH_HLEN) |
| 5357 | skb_set_network_header(skb, ETH_HLEN); |
| 5358 | |
| 5359 | skb_reset_mac_len(skb); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5360 | |
| 5361 | return err; |
| 5362 | } |
Shmulik Ladkani | bfca4c5 | 2016-09-19 19:11:09 +0300 | [diff] [blame] | 5363 | EXPORT_SYMBOL(__skb_vlan_pop); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5364 | |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5365 | /* Pop a vlan tag either from hwaccel or from payload. |
| 5366 | * Expects skb->data at mac header. |
| 5367 | */ |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5368 | int skb_vlan_pop(struct sk_buff *skb) |
| 5369 | { |
| 5370 | u16 vlan_tci; |
| 5371 | __be16 vlan_proto; |
| 5372 | int err; |
| 5373 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 5374 | if (likely(skb_vlan_tag_present(skb))) { |
Michał Mirosław | b1817524 | 2018-11-09 00:18:02 +0100 | [diff] [blame] | 5375 | __vlan_hwaccel_clear_tag(skb); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5376 | } else { |
Shmulik Ladkani | ecf4ee4 | 2016-09-20 12:48:37 +0300 | [diff] [blame] | 5377 | if (unlikely(!eth_type_vlan(skb->protocol))) |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5378 | return 0; |
| 5379 | |
| 5380 | err = __skb_vlan_pop(skb, &vlan_tci); |
| 5381 | if (err) |
| 5382 | return err; |
| 5383 | } |
| 5384 | /* move next vlan tag to hw accel tag */ |
Shmulik Ladkani | ecf4ee4 | 2016-09-20 12:48:37 +0300 | [diff] [blame] | 5385 | if (likely(!eth_type_vlan(skb->protocol))) |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5386 | return 0; |
| 5387 | |
| 5388 | vlan_proto = skb->protocol; |
| 5389 | err = __skb_vlan_pop(skb, &vlan_tci); |
| 5390 | if (unlikely(err)) |
| 5391 | return err; |
| 5392 | |
| 5393 | __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); |
| 5394 | return 0; |
| 5395 | } |
| 5396 | EXPORT_SYMBOL(skb_vlan_pop); |
| 5397 | |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5398 | /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). |
| 5399 | * Expects skb->data at mac header. |
| 5400 | */ |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5401 | int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) |
| 5402 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 5403 | if (skb_vlan_tag_present(skb)) { |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5404 | int offset = skb->data - skb_mac_header(skb); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5405 | int err; |
| 5406 | |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5407 | if (WARN_ONCE(offset, |
| 5408 | "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", |
| 5409 | offset)) { |
| 5410 | return -EINVAL; |
| 5411 | } |
| 5412 | |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5413 | err = __vlan_insert_tag(skb, skb->vlan_proto, |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 5414 | skb_vlan_tag_get(skb)); |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5415 | if (err) |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5416 | return err; |
Daniel Borkmann | 9241e2d | 2016-04-16 02:27:58 +0200 | [diff] [blame] | 5417 | |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5418 | skb->protocol = skb->vlan_proto; |
| 5419 | skb->mac_len += VLAN_HLEN; |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5420 | |
Daniel Borkmann | 6b83d28 | 2016-02-20 00:29:30 +0100 | [diff] [blame] | 5421 | skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5422 | } |
| 5423 | __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); |
| 5424 | return 0; |
| 5425 | } |
| 5426 | EXPORT_SYMBOL(skb_vlan_push); |
| 5427 | |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5428 | /** |
| 5429 | * alloc_skb_with_frags - allocate skb with page frags |
| 5430 | * |
Masanari Iida | de3f0d0 | 2014-10-09 12:58:08 +0900 | [diff] [blame] | 5431 | * @header_len: size of linear part |
| 5432 | * @data_len: needed length in frags |
| 5433 | * @max_page_order: max page order desired. |
| 5434 | * @errcode: pointer to error code if any |
| 5435 | * @gfp_mask: allocation mask |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5436 | * |
| 5437 | * This can be used to allocate a paged skb, given a maximal order for frags. |
| 5438 | */ |
| 5439 | struct sk_buff *alloc_skb_with_frags(unsigned long header_len, |
| 5440 | unsigned long data_len, |
| 5441 | int max_page_order, |
| 5442 | int *errcode, |
| 5443 | gfp_t gfp_mask) |
| 5444 | { |
| 5445 | int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; |
| 5446 | unsigned long chunk; |
| 5447 | struct sk_buff *skb; |
| 5448 | struct page *page; |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5449 | int i; |
| 5450 | |
| 5451 | *errcode = -EMSGSIZE; |
| 5452 | /* Note this test could be relaxed, if we succeed to allocate |
| 5453 | * high order pages... |
| 5454 | */ |
| 5455 | if (npages > MAX_SKB_FRAGS) |
| 5456 | return NULL; |
| 5457 | |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5458 | *errcode = -ENOBUFS; |
David Rientjes | f8c468e | 2019-01-02 13:01:43 -0800 | [diff] [blame] | 5459 | skb = alloc_skb(header_len, gfp_mask); |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5460 | if (!skb) |
| 5461 | return NULL; |
| 5462 | |
| 5463 | skb->truesize += npages << PAGE_SHIFT; |
| 5464 | |
| 5465 | for (i = 0; npages > 0; i++) { |
| 5466 | int order = max_page_order; |
| 5467 | |
| 5468 | while (order) { |
| 5469 | if (npages >= 1 << order) { |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 5470 | page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5471 | __GFP_COMP | |
Michal Hocko | d14b56f | 2018-06-28 17:53:06 +0200 | [diff] [blame] | 5472 | __GFP_NOWARN, |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5473 | order); |
| 5474 | if (page) |
| 5475 | goto fill_page; |
| 5476 | /* Do not retry other high order allocations */ |
| 5477 | order = 1; |
| 5478 | max_page_order = 0; |
| 5479 | } |
| 5480 | order--; |
| 5481 | } |
| 5482 | page = alloc_page(gfp_mask); |
| 5483 | if (!page) |
| 5484 | goto failure; |
| 5485 | fill_page: |
| 5486 | chunk = min_t(unsigned long, data_len, |
| 5487 | PAGE_SIZE << order); |
| 5488 | skb_fill_page_desc(skb, i, page, 0, chunk); |
| 5489 | data_len -= chunk; |
| 5490 | npages -= 1 << order; |
| 5491 | } |
| 5492 | return skb; |
| 5493 | |
| 5494 | failure: |
| 5495 | kfree_skb(skb); |
| 5496 | return NULL; |
| 5497 | } |
| 5498 | EXPORT_SYMBOL(alloc_skb_with_frags); |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 5499 | |
| 5500 | /* carve out the first off bytes from skb when off < headlen */ |
| 5501 | static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, |
| 5502 | const int headlen, gfp_t gfp_mask) |
| 5503 | { |
| 5504 | int i; |
| 5505 | int size = skb_end_offset(skb); |
| 5506 | int new_hlen = headlen - off; |
| 5507 | u8 *data; |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 5508 | |
| 5509 | size = SKB_DATA_ALIGN(size); |
| 5510 | |
| 5511 | if (skb_pfmemalloc(skb)) |
| 5512 | gfp_mask |= __GFP_MEMALLOC; |
| 5513 | data = kmalloc_reserve(size + |
| 5514 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), |
| 5515 | gfp_mask, NUMA_NO_NODE, NULL); |
| 5516 | if (!data) |
| 5517 | return -ENOMEM; |
| 5518 | |
| 5519 | size = SKB_WITH_OVERHEAD(ksize(data)); |
| 5520 | |
| 5521 | /* Copy real data, and all frags */ |
| 5522 | skb_copy_from_linear_data_offset(skb, off, data, new_hlen); |
| 5523 | skb->len -= off; |
| 5524 | |
| 5525 | memcpy((struct skb_shared_info *)(data + size), |
| 5526 | skb_shinfo(skb), |
| 5527 | offsetof(struct skb_shared_info, |
| 5528 | frags[skb_shinfo(skb)->nr_frags])); |
| 5529 | if (skb_cloned(skb)) { |
| 5530 | /* drop the old head gracefully */ |
| 5531 | if (skb_orphan_frags(skb, gfp_mask)) { |
| 5532 | kfree(data); |
| 5533 | return -ENOMEM; |
| 5534 | } |
| 5535 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 5536 | skb_frag_ref(skb, i); |
| 5537 | if (skb_has_frag_list(skb)) |
| 5538 | skb_clone_fraglist(skb); |
| 5539 | skb_release_data(skb); |
| 5540 | } else { |
| 5541 | /* we can reuse existing recount- all we did was |
| 5542 | * relocate values |
| 5543 | */ |
| 5544 | skb_free_head(skb); |
| 5545 | } |
| 5546 | |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 5547 | skb->head = data; |
| 5548 | skb->data = data; |
| 5549 | skb->head_frag = 0; |
| 5550 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 5551 | skb->end = size; |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 5552 | #else |
| 5553 | skb->end = skb->head + size; |
| 5554 | #endif |
| 5555 | skb_set_tail_pointer(skb, skb_headlen(skb)); |
| 5556 | skb_headers_offset_update(skb, 0); |
| 5557 | skb->cloned = 0; |
| 5558 | skb->hdr_len = 0; |
| 5559 | skb->nohdr = 0; |
| 5560 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 5561 | |
| 5562 | return 0; |
| 5563 | } |
| 5564 | |
| 5565 | static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); |
| 5566 | |
| 5567 | /* carve out the first eat bytes from skb's frag_list. May recurse into |
| 5568 | * pskb_carve() |
| 5569 | */ |
| 5570 | static int pskb_carve_frag_list(struct sk_buff *skb, |
| 5571 | struct skb_shared_info *shinfo, int eat, |
| 5572 | gfp_t gfp_mask) |
| 5573 | { |
| 5574 | struct sk_buff *list = shinfo->frag_list; |
| 5575 | struct sk_buff *clone = NULL; |
| 5576 | struct sk_buff *insp = NULL; |
| 5577 | |
| 5578 | do { |
| 5579 | if (!list) { |
| 5580 | pr_err("Not enough bytes to eat. Want %d\n", eat); |
| 5581 | return -EFAULT; |
| 5582 | } |
| 5583 | if (list->len <= eat) { |
| 5584 | /* Eaten as whole. */ |
| 5585 | eat -= list->len; |
| 5586 | list = list->next; |
| 5587 | insp = list; |
| 5588 | } else { |
| 5589 | /* Eaten partially. */ |
| 5590 | if (skb_shared(list)) { |
| 5591 | clone = skb_clone(list, gfp_mask); |
| 5592 | if (!clone) |
| 5593 | return -ENOMEM; |
| 5594 | insp = list->next; |
| 5595 | list = clone; |
| 5596 | } else { |
| 5597 | /* This may be pulled without problems. */ |
| 5598 | insp = list; |
| 5599 | } |
| 5600 | if (pskb_carve(list, eat, gfp_mask) < 0) { |
| 5601 | kfree_skb(clone); |
| 5602 | return -ENOMEM; |
| 5603 | } |
| 5604 | break; |
| 5605 | } |
| 5606 | } while (eat); |
| 5607 | |
| 5608 | /* Free pulled out fragments. */ |
| 5609 | while ((list = shinfo->frag_list) != insp) { |
| 5610 | shinfo->frag_list = list->next; |
| 5611 | kfree_skb(list); |
| 5612 | } |
| 5613 | /* And insert new clone at head. */ |
| 5614 | if (clone) { |
| 5615 | clone->next = list; |
| 5616 | shinfo->frag_list = clone; |
| 5617 | } |
| 5618 | return 0; |
| 5619 | } |
| 5620 | |
| 5621 | /* carve off first len bytes from skb. Split line (off) is in the |
| 5622 | * non-linear part of skb |
| 5623 | */ |
| 5624 | static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, |
| 5625 | int pos, gfp_t gfp_mask) |
| 5626 | { |
| 5627 | int i, k = 0; |
| 5628 | int size = skb_end_offset(skb); |
| 5629 | u8 *data; |
| 5630 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 5631 | struct skb_shared_info *shinfo; |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 5632 | |
| 5633 | size = SKB_DATA_ALIGN(size); |
| 5634 | |
| 5635 | if (skb_pfmemalloc(skb)) |
| 5636 | gfp_mask |= __GFP_MEMALLOC; |
| 5637 | data = kmalloc_reserve(size + |
| 5638 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), |
| 5639 | gfp_mask, NUMA_NO_NODE, NULL); |
| 5640 | if (!data) |
| 5641 | return -ENOMEM; |
| 5642 | |
| 5643 | size = SKB_WITH_OVERHEAD(ksize(data)); |
| 5644 | |
| 5645 | memcpy((struct skb_shared_info *)(data + size), |
| 5646 | skb_shinfo(skb), offsetof(struct skb_shared_info, |
| 5647 | frags[skb_shinfo(skb)->nr_frags])); |
| 5648 | if (skb_orphan_frags(skb, gfp_mask)) { |
| 5649 | kfree(data); |
| 5650 | return -ENOMEM; |
| 5651 | } |
| 5652 | shinfo = (struct skb_shared_info *)(data + size); |
| 5653 | for (i = 0; i < nfrags; i++) { |
| 5654 | int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 5655 | |
| 5656 | if (pos + fsize > off) { |
| 5657 | shinfo->frags[k] = skb_shinfo(skb)->frags[i]; |
| 5658 | |
| 5659 | if (pos < off) { |
| 5660 | /* Split frag. |
| 5661 | * We have two variants in this case: |
| 5662 | * 1. Move all the frag to the second |
| 5663 | * part, if it is possible. F.e. |
| 5664 | * this approach is mandatory for TUX, |
| 5665 | * where splitting is expensive. |
| 5666 | * 2. Split is accurately. We make this. |
| 5667 | */ |
| 5668 | shinfo->frags[0].page_offset += off - pos; |
| 5669 | skb_frag_size_sub(&shinfo->frags[0], off - pos); |
| 5670 | } |
| 5671 | skb_frag_ref(skb, i); |
| 5672 | k++; |
| 5673 | } |
| 5674 | pos += fsize; |
| 5675 | } |
| 5676 | shinfo->nr_frags = k; |
| 5677 | if (skb_has_frag_list(skb)) |
| 5678 | skb_clone_fraglist(skb); |
| 5679 | |
| 5680 | if (k == 0) { |
| 5681 | /* split line is in frag list */ |
| 5682 | pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask); |
| 5683 | } |
| 5684 | skb_release_data(skb); |
| 5685 | |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 5686 | skb->head = data; |
| 5687 | skb->head_frag = 0; |
| 5688 | skb->data = data; |
| 5689 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 5690 | skb->end = size; |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 5691 | #else |
| 5692 | skb->end = skb->head + size; |
| 5693 | #endif |
| 5694 | skb_reset_tail_pointer(skb); |
| 5695 | skb_headers_offset_update(skb, 0); |
| 5696 | skb->cloned = 0; |
| 5697 | skb->hdr_len = 0; |
| 5698 | skb->nohdr = 0; |
| 5699 | skb->len -= off; |
| 5700 | skb->data_len = skb->len; |
| 5701 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 5702 | return 0; |
| 5703 | } |
| 5704 | |
| 5705 | /* remove len bytes from the beginning of the skb */ |
| 5706 | static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) |
| 5707 | { |
| 5708 | int headlen = skb_headlen(skb); |
| 5709 | |
| 5710 | if (len < headlen) |
| 5711 | return pskb_carve_inside_header(skb, len, headlen, gfp); |
| 5712 | else |
| 5713 | return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); |
| 5714 | } |
| 5715 | |
| 5716 | /* Extract to_copy bytes starting at off from skb, and return this in |
| 5717 | * a new skb |
| 5718 | */ |
| 5719 | struct sk_buff *pskb_extract(struct sk_buff *skb, int off, |
| 5720 | int to_copy, gfp_t gfp) |
| 5721 | { |
| 5722 | struct sk_buff *clone = skb_clone(skb, gfp); |
| 5723 | |
| 5724 | if (!clone) |
| 5725 | return NULL; |
| 5726 | |
| 5727 | if (pskb_carve(clone, off, gfp) < 0 || |
| 5728 | pskb_trim(clone, to_copy)) { |
| 5729 | kfree_skb(clone); |
| 5730 | return NULL; |
| 5731 | } |
| 5732 | return clone; |
| 5733 | } |
| 5734 | EXPORT_SYMBOL(pskb_extract); |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 5735 | |
| 5736 | /** |
| 5737 | * skb_condense - try to get rid of fragments/frag_list if possible |
| 5738 | * @skb: buffer |
| 5739 | * |
| 5740 | * Can be used to save memory before skb is added to a busy queue. |
| 5741 | * If packet has bytes in frags and enough tail room in skb->head, |
| 5742 | * pull all of them, so that we can free the frags right now and adjust |
| 5743 | * truesize. |
| 5744 | * Notes: |
| 5745 | * We do not reallocate skb->head thus can not fail. |
| 5746 | * Caller must re-evaluate skb->truesize if needed. |
| 5747 | */ |
| 5748 | void skb_condense(struct sk_buff *skb) |
| 5749 | { |
Eric Dumazet | 3174fed | 2016-12-09 08:02:05 -0800 | [diff] [blame] | 5750 | if (skb->data_len) { |
| 5751 | if (skb->data_len > skb->end - skb->tail || |
| 5752 | skb_cloned(skb)) |
| 5753 | return; |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 5754 | |
Eric Dumazet | 3174fed | 2016-12-09 08:02:05 -0800 | [diff] [blame] | 5755 | /* Nice, we can free page frag(s) right now */ |
| 5756 | __pskb_pull_tail(skb, skb->data_len); |
| 5757 | } |
| 5758 | /* At this point, skb->truesize might be over estimated, |
| 5759 | * because skb had a fragment, and fragments do not tell |
| 5760 | * their truesize. |
| 5761 | * When we pulled its content into skb->head, fragment |
| 5762 | * was freed, but __pskb_pull_tail() could not possibly |
| 5763 | * adjust skb->truesize, not knowing the frag truesize. |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 5764 | */ |
| 5765 | skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); |
| 5766 | } |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5767 | |
| 5768 | #ifdef CONFIG_SKB_EXTENSIONS |
| 5769 | static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) |
| 5770 | { |
| 5771 | return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); |
| 5772 | } |
| 5773 | |
| 5774 | static struct skb_ext *skb_ext_alloc(void) |
| 5775 | { |
| 5776 | struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); |
| 5777 | |
| 5778 | if (new) { |
| 5779 | memset(new->offset, 0, sizeof(new->offset)); |
| 5780 | refcount_set(&new->refcnt, 1); |
| 5781 | } |
| 5782 | |
| 5783 | return new; |
| 5784 | } |
| 5785 | |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 5786 | static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, |
| 5787 | unsigned int old_active) |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5788 | { |
| 5789 | struct skb_ext *new; |
| 5790 | |
| 5791 | if (refcount_read(&old->refcnt) == 1) |
| 5792 | return old; |
| 5793 | |
| 5794 | new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); |
| 5795 | if (!new) |
| 5796 | return NULL; |
| 5797 | |
| 5798 | memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); |
| 5799 | refcount_set(&new->refcnt, 1); |
| 5800 | |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 5801 | #ifdef CONFIG_XFRM |
| 5802 | if (old_active & (1 << SKB_EXT_SEC_PATH)) { |
| 5803 | struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); |
| 5804 | unsigned int i; |
| 5805 | |
| 5806 | for (i = 0; i < sp->len; i++) |
| 5807 | xfrm_state_hold(sp->xvec[i]); |
| 5808 | } |
| 5809 | #endif |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5810 | __skb_ext_put(old); |
| 5811 | return new; |
| 5812 | } |
| 5813 | |
| 5814 | /** |
| 5815 | * skb_ext_add - allocate space for given extension, COW if needed |
| 5816 | * @skb: buffer |
| 5817 | * @id: extension to allocate space for |
| 5818 | * |
| 5819 | * Allocates enough space for the given extension. |
| 5820 | * If the extension is already present, a pointer to that extension |
| 5821 | * is returned. |
| 5822 | * |
| 5823 | * If the skb was cloned, COW applies and the returned memory can be |
| 5824 | * modified without changing the extension space of clones buffers. |
| 5825 | * |
| 5826 | * Returns pointer to the extension or NULL on allocation failure. |
| 5827 | */ |
| 5828 | void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) |
| 5829 | { |
| 5830 | struct skb_ext *new, *old = NULL; |
| 5831 | unsigned int newlen, newoff; |
| 5832 | |
| 5833 | if (skb->active_extensions) { |
| 5834 | old = skb->extensions; |
| 5835 | |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 5836 | new = skb_ext_maybe_cow(old, skb->active_extensions); |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5837 | if (!new) |
| 5838 | return NULL; |
| 5839 | |
Paolo Abeni | 682ec85 | 2018-12-21 19:03:15 +0100 | [diff] [blame] | 5840 | if (__skb_ext_exist(new, id)) |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5841 | goto set_active; |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5842 | |
Paolo Abeni | e94e50b | 2018-12-21 19:03:13 +0100 | [diff] [blame] | 5843 | newoff = new->chunks; |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5844 | } else { |
| 5845 | newoff = SKB_EXT_CHUNKSIZEOF(*new); |
| 5846 | |
| 5847 | new = skb_ext_alloc(); |
| 5848 | if (!new) |
| 5849 | return NULL; |
| 5850 | } |
| 5851 | |
| 5852 | newlen = newoff + skb_ext_type_len[id]; |
| 5853 | new->chunks = newlen; |
| 5854 | new->offset[id] = newoff; |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5855 | set_active: |
Paolo Abeni | 682ec85 | 2018-12-21 19:03:15 +0100 | [diff] [blame] | 5856 | skb->extensions = new; |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5857 | skb->active_extensions |= 1 << id; |
| 5858 | return skb_ext_get_ptr(new, id); |
| 5859 | } |
| 5860 | EXPORT_SYMBOL(skb_ext_add); |
| 5861 | |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 5862 | #ifdef CONFIG_XFRM |
| 5863 | static void skb_ext_put_sp(struct sec_path *sp) |
| 5864 | { |
| 5865 | unsigned int i; |
| 5866 | |
| 5867 | for (i = 0; i < sp->len; i++) |
| 5868 | xfrm_state_put(sp->xvec[i]); |
| 5869 | } |
| 5870 | #endif |
| 5871 | |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5872 | void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) |
| 5873 | { |
| 5874 | struct skb_ext *ext = skb->extensions; |
| 5875 | |
| 5876 | skb->active_extensions &= ~(1 << id); |
| 5877 | if (skb->active_extensions == 0) { |
| 5878 | skb->extensions = NULL; |
| 5879 | __skb_ext_put(ext); |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 5880 | #ifdef CONFIG_XFRM |
| 5881 | } else if (id == SKB_EXT_SEC_PATH && |
| 5882 | refcount_read(&ext->refcnt) == 1) { |
| 5883 | struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); |
| 5884 | |
| 5885 | skb_ext_put_sp(sp); |
| 5886 | sp->len = 0; |
| 5887 | #endif |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5888 | } |
| 5889 | } |
| 5890 | EXPORT_SYMBOL(__skb_ext_del); |
| 5891 | |
| 5892 | void __skb_ext_put(struct skb_ext *ext) |
| 5893 | { |
| 5894 | /* If this is last clone, nothing can increment |
| 5895 | * it after check passes. Avoids one atomic op. |
| 5896 | */ |
| 5897 | if (refcount_read(&ext->refcnt) == 1) |
| 5898 | goto free_now; |
| 5899 | |
| 5900 | if (!refcount_dec_and_test(&ext->refcnt)) |
| 5901 | return; |
| 5902 | free_now: |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 5903 | #ifdef CONFIG_XFRM |
| 5904 | if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) |
| 5905 | skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); |
| 5906 | #endif |
| 5907 | |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 5908 | kmem_cache_free(skbuff_ext_cache, ext); |
| 5909 | } |
| 5910 | EXPORT_SYMBOL(__skb_ext_put); |
| 5911 | #endif /* CONFIG_SKB_EXTENSIONS */ |