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> |
John Hurley | 2a2ea50 | 2019-07-07 15:01:57 +0100 | [diff] [blame] | 62 | #include <linux/mpls.h> |
Sebastian Andrzej Siewior | 183f47f | 2021-02-18 18:31:24 +0100 | [diff] [blame] | 63 | #include <linux/kcov.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | #include <net/protocol.h> |
| 66 | #include <net/dst.h> |
| 67 | #include <net/sock.h> |
| 68 | #include <net/checksum.h> |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 69 | #include <net/ip6_checksum.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #include <net/xfrm.h> |
John Hurley | 8822e27 | 2019-07-07 15:01:54 +0100 | [diff] [blame] | 71 | #include <net/mpls.h> |
Mat Martineau | 3ee17bc | 2020-01-09 07:59:19 -0800 | [diff] [blame] | 72 | #include <net/mptcp.h> |
Ilias Apalodimas | 6a5bcd8 | 2021-06-07 21:02:38 +0200 | [diff] [blame] | 73 | #include <net/page_pool.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 75 | #include <linux/uaccess.h> |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 76 | #include <trace/events/skb.h> |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 77 | #include <linux/highmem.h> |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 78 | #include <linux/capability.h> |
| 79 | #include <linux/user_namespace.h> |
Matteo Croce | 2544af0 | 2019-05-29 17:13:48 +0200 | [diff] [blame] | 80 | #include <linux/indirect_call_wrapper.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 81 | |
Bart Van Assche | 7b7ed88 | 2019-03-25 09:17:23 -0700 | [diff] [blame] | 82 | #include "datagram.h" |
| 83 | |
Alexey Dobriyan | 08009a7 | 2018-02-24 21:20:33 +0300 | [diff] [blame] | 84 | struct kmem_cache *skbuff_head_cache __ro_after_init; |
| 85 | static struct kmem_cache *skbuff_fclone_cache __ro_after_init; |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 86 | #ifdef CONFIG_SKB_EXTENSIONS |
| 87 | static struct kmem_cache *skbuff_ext_cache __ro_after_init; |
| 88 | #endif |
Hans Westgaard Ry | 5f74f82e | 2016-02-03 09:26:57 +0100 | [diff] [blame] | 89 | int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; |
| 90 | EXPORT_SYMBOL(sysctl_max_skb_frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /** |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 93 | * skb_panic - private function for out-of-line support |
| 94 | * @skb: buffer |
| 95 | * @sz: size |
| 96 | * @addr: address |
James Hogan | 99d5851 | 2013-02-13 11:20:27 +0000 | [diff] [blame] | 97 | * @msg: skb_over_panic or skb_under_panic |
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 | * Out-of-line support for skb_put() and skb_push(). |
| 100 | * Called via the wrapper skb_over_panic() or skb_under_panic(). |
| 101 | * Keep out of line to prevent kernel bloat. |
| 102 | * __builtin_return_address is not used because it is not always reliable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | */ |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 104 | 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] | 105 | const char msg[]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Jesper Dangaard Brouer | 41a4691 | 2020-04-27 18:37:43 +0200 | [diff] [blame] | 107 | pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", |
James Hogan | 99d5851 | 2013-02-13 11:20:27 +0000 | [diff] [blame] | 108 | msg, addr, skb->len, sz, skb->head, skb->data, |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 109 | (unsigned long)skb->tail, (unsigned long)skb->end, |
| 110 | skb->dev ? skb->dev->name : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | BUG(); |
| 112 | } |
| 113 | |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 114 | 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] | 115 | { |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 116 | skb_panic(skb, sz, addr, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 119 | static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
| 120 | { |
| 121 | skb_panic(skb, sz, addr, __func__); |
| 122 | } |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 123 | |
Alexander Lobakin | 50fad4b5 | 2021-02-13 14:12:13 +0000 | [diff] [blame] | 124 | #define NAPI_SKB_CACHE_SIZE 64 |
Alexander Lobakin | f450d53 | 2021-02-13 14:12:25 +0000 | [diff] [blame] | 125 | #define NAPI_SKB_CACHE_BULK 16 |
| 126 | #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) |
Alexander Lobakin | 50fad4b5 | 2021-02-13 14:12:13 +0000 | [diff] [blame] | 127 | |
| 128 | struct napi_alloc_cache { |
| 129 | struct page_frag_cache page; |
| 130 | unsigned int skb_count; |
| 131 | void *skb_cache[NAPI_SKB_CACHE_SIZE]; |
| 132 | }; |
| 133 | |
| 134 | static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); |
| 135 | static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); |
| 136 | |
| 137 | static void *__alloc_frag_align(unsigned int fragsz, gfp_t gfp_mask, |
| 138 | unsigned int align_mask) |
| 139 | { |
| 140 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
| 141 | |
| 142 | return page_frag_alloc_align(&nc->page, fragsz, gfp_mask, align_mask); |
| 143 | } |
| 144 | |
| 145 | void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) |
| 146 | { |
| 147 | fragsz = SKB_DATA_ALIGN(fragsz); |
| 148 | |
| 149 | return __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask); |
| 150 | } |
| 151 | EXPORT_SYMBOL(__napi_alloc_frag_align); |
| 152 | |
| 153 | void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) |
| 154 | { |
| 155 | struct page_frag_cache *nc; |
| 156 | void *data; |
| 157 | |
| 158 | fragsz = SKB_DATA_ALIGN(fragsz); |
| 159 | if (in_irq() || irqs_disabled()) { |
| 160 | nc = this_cpu_ptr(&netdev_alloc_cache); |
| 161 | data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); |
| 162 | } else { |
| 163 | local_bh_disable(); |
| 164 | data = __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask); |
| 165 | local_bh_enable(); |
| 166 | } |
| 167 | return data; |
| 168 | } |
| 169 | EXPORT_SYMBOL(__netdev_alloc_frag_align); |
| 170 | |
Alexander Lobakin | f450d53 | 2021-02-13 14:12:25 +0000 | [diff] [blame] | 171 | static struct sk_buff *napi_skb_cache_get(void) |
| 172 | { |
| 173 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
| 174 | struct sk_buff *skb; |
| 175 | |
| 176 | if (unlikely(!nc->skb_count)) |
| 177 | nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache, |
| 178 | GFP_ATOMIC, |
| 179 | NAPI_SKB_CACHE_BULK, |
| 180 | nc->skb_cache); |
| 181 | if (unlikely(!nc->skb_count)) |
| 182 | return NULL; |
| 183 | |
| 184 | skb = nc->skb_cache[--nc->skb_count]; |
| 185 | kasan_unpoison_object_data(skbuff_head_cache, skb); |
| 186 | |
| 187 | return skb; |
| 188 | } |
| 189 | |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 190 | /* Caller must provide SKB that is memset cleared */ |
Alexander Lobakin | 483126b | 2021-02-13 14:11:26 +0000 | [diff] [blame] | 191 | static void __build_skb_around(struct sk_buff *skb, void *data, |
| 192 | unsigned int frag_size) |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 193 | { |
| 194 | struct skb_shared_info *shinfo; |
| 195 | unsigned int size = frag_size ? : ksize(data); |
| 196 | |
| 197 | size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 198 | |
| 199 | /* Assumes caller memset cleared SKB */ |
| 200 | skb->truesize = SKB_TRUESIZE(size); |
| 201 | refcount_set(&skb->users, 1); |
| 202 | skb->head = data; |
| 203 | skb->data = data; |
| 204 | skb_reset_tail_pointer(skb); |
| 205 | skb->end = skb->tail + size; |
| 206 | skb->mac_header = (typeof(skb->mac_header))~0U; |
| 207 | skb->transport_header = (typeof(skb->transport_header))~0U; |
| 208 | |
| 209 | /* make sure we initialize shinfo sequentially */ |
| 210 | shinfo = skb_shinfo(skb); |
| 211 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
| 212 | atomic_set(&shinfo->dataref, 1); |
| 213 | |
Aleksandr Nogikh | 6370cc3 | 2020-10-29 17:36:19 +0000 | [diff] [blame] | 214 | skb_set_kcov_handle(skb, kcov_common_handle()); |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /** |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 218 | * __build_skb - build a network buffer |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 219 | * @data: data buffer provided by caller |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 220 | * @frag_size: size of data, or 0 if head was kmalloced |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 221 | * |
| 222 | * Allocate a new &sk_buff. Caller provides space holding head and |
Florian Fainelli | deceb4c | 2013-07-23 20:22:39 +0100 | [diff] [blame] | 223 | * skb_shared_info. @data must have been allocated by kmalloc() only if |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 224 | * @frag_size is 0, otherwise data should come from the page allocator |
| 225 | * or vmalloc() |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 226 | * The return is the new skb buffer. |
| 227 | * On a failure the return is %NULL, and @data is not freed. |
| 228 | * Notes : |
| 229 | * Before IO, driver allocates only data buffer where NIC put incoming frame |
| 230 | * Driver should add room at head (NET_SKB_PAD) and |
| 231 | * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) |
| 232 | * After IO, driver calls build_skb(), to allocate sk_buff and populate it |
| 233 | * before giving packet to stack. |
| 234 | * RX rings only contains data buffers, not full skbs. |
| 235 | */ |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 236 | struct sk_buff *__build_skb(void *data, unsigned int frag_size) |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 237 | { |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 238 | struct sk_buff *skb; |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 239 | |
| 240 | skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 241 | if (unlikely(!skb)) |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 242 | return NULL; |
| 243 | |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 244 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
Alexander Lobakin | 483126b | 2021-02-13 14:11:26 +0000 | [diff] [blame] | 245 | __build_skb_around(skb, data, frag_size); |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 246 | |
Alexander Lobakin | 483126b | 2021-02-13 14:11:26 +0000 | [diff] [blame] | 247 | return skb; |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 248 | } |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 249 | |
| 250 | /* build_skb() is wrapper over __build_skb(), that specifically |
| 251 | * takes care of skb->head and skb->pfmemalloc |
| 252 | * This means that if @frag_size is not zero, then @data must be backed |
| 253 | * by a page fragment, not kmalloc() or vmalloc() |
| 254 | */ |
| 255 | struct sk_buff *build_skb(void *data, unsigned int frag_size) |
| 256 | { |
| 257 | struct sk_buff *skb = __build_skb(data, frag_size); |
| 258 | |
| 259 | if (skb && frag_size) { |
| 260 | skb->head_frag = 1; |
Michal Hocko | 2f064f3 | 2015-08-21 14:11:51 -0700 | [diff] [blame] | 261 | if (page_is_pfmemalloc(virt_to_head_page(data))) |
Eric Dumazet | 2ea2f62 | 2015-04-24 16:05:01 -0700 | [diff] [blame] | 262 | skb->pfmemalloc = 1; |
| 263 | } |
| 264 | return skb; |
| 265 | } |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 266 | EXPORT_SYMBOL(build_skb); |
| 267 | |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 268 | /** |
| 269 | * build_skb_around - build a network buffer around provided skb |
| 270 | * @skb: sk_buff provide by caller, must be memset cleared |
| 271 | * @data: data buffer provided by caller |
| 272 | * @frag_size: size of data, or 0 if head was kmalloced |
| 273 | */ |
| 274 | struct sk_buff *build_skb_around(struct sk_buff *skb, |
| 275 | void *data, unsigned int frag_size) |
| 276 | { |
| 277 | if (unlikely(!skb)) |
| 278 | return NULL; |
| 279 | |
Alexander Lobakin | 483126b | 2021-02-13 14:11:26 +0000 | [diff] [blame] | 280 | __build_skb_around(skb, data, frag_size); |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 281 | |
Alexander Lobakin | 483126b | 2021-02-13 14:11:26 +0000 | [diff] [blame] | 282 | if (frag_size) { |
Jesper Dangaard Brouer | ba0509b | 2019-04-12 17:07:37 +0200 | [diff] [blame] | 283 | skb->head_frag = 1; |
| 284 | if (page_is_pfmemalloc(virt_to_head_page(data))) |
| 285 | skb->pfmemalloc = 1; |
| 286 | } |
| 287 | return skb; |
| 288 | } |
| 289 | EXPORT_SYMBOL(build_skb_around); |
| 290 | |
Alexander Lobakin | f450d53 | 2021-02-13 14:12:25 +0000 | [diff] [blame] | 291 | /** |
| 292 | * __napi_build_skb - build a network buffer |
| 293 | * @data: data buffer provided by caller |
| 294 | * @frag_size: size of data, or 0 if head was kmalloced |
| 295 | * |
| 296 | * Version of __build_skb() that uses NAPI percpu caches to obtain |
| 297 | * skbuff_head instead of inplace allocation. |
| 298 | * |
| 299 | * Returns a new &sk_buff on success, %NULL on allocation failure. |
| 300 | */ |
| 301 | static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) |
| 302 | { |
| 303 | struct sk_buff *skb; |
| 304 | |
| 305 | skb = napi_skb_cache_get(); |
| 306 | if (unlikely(!skb)) |
| 307 | return NULL; |
| 308 | |
| 309 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
| 310 | __build_skb_around(skb, data, frag_size); |
| 311 | |
| 312 | return skb; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * napi_build_skb - build a network buffer |
| 317 | * @data: data buffer provided by caller |
| 318 | * @frag_size: size of data, or 0 if head was kmalloced |
| 319 | * |
| 320 | * Version of __napi_build_skb() that takes care of skb->head_frag |
| 321 | * and skb->pfmemalloc when the data is a page or page fragment. |
| 322 | * |
| 323 | * Returns a new &sk_buff on success, %NULL on allocation failure. |
| 324 | */ |
| 325 | struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) |
| 326 | { |
| 327 | struct sk_buff *skb = __napi_build_skb(data, frag_size); |
| 328 | |
| 329 | if (likely(skb) && frag_size) { |
| 330 | skb->head_frag = 1; |
| 331 | skb_propagate_pfmemalloc(virt_to_head_page(data), skb); |
| 332 | } |
| 333 | |
| 334 | return skb; |
| 335 | } |
| 336 | EXPORT_SYMBOL(napi_build_skb); |
| 337 | |
Alexander Lobakin | 5381b23 | 2021-02-13 14:11:00 +0000 | [diff] [blame] | 338 | /* |
| 339 | * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells |
| 340 | * the caller if emergency pfmemalloc reserves are being used. If it is and |
| 341 | * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves |
| 342 | * may be used. Otherwise, the packet data may be discarded until enough |
| 343 | * memory is free |
| 344 | */ |
Alexander Lobakin | ef28095 | 2021-02-13 14:11:11 +0000 | [diff] [blame] | 345 | static void *kmalloc_reserve(size_t size, gfp_t flags, int node, |
| 346 | bool *pfmemalloc) |
Alexander Lobakin | 5381b23 | 2021-02-13 14:11:00 +0000 | [diff] [blame] | 347 | { |
| 348 | void *obj; |
| 349 | bool ret_pfmemalloc = false; |
| 350 | |
| 351 | /* |
| 352 | * Try a regular allocation, when that fails and we're not entitled |
| 353 | * to the reserves, fail. |
| 354 | */ |
| 355 | obj = kmalloc_node_track_caller(size, |
| 356 | flags | __GFP_NOMEMALLOC | __GFP_NOWARN, |
| 357 | node); |
| 358 | if (obj || !(gfp_pfmemalloc_allowed(flags))) |
| 359 | goto out; |
| 360 | |
| 361 | /* Try again but now we are using pfmemalloc reserves */ |
| 362 | ret_pfmemalloc = true; |
| 363 | obj = kmalloc_node_track_caller(size, flags, node); |
| 364 | |
| 365 | out: |
| 366 | if (pfmemalloc) |
| 367 | *pfmemalloc = ret_pfmemalloc; |
| 368 | |
| 369 | return obj; |
| 370 | } |
| 371 | |
| 372 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
| 373 | * 'private' fields and also do memory statistics to find all the |
| 374 | * [BEEP] leaks. |
| 375 | * |
| 376 | */ |
| 377 | |
| 378 | /** |
| 379 | * __alloc_skb - allocate a network buffer |
| 380 | * @size: size to allocate |
| 381 | * @gfp_mask: allocation mask |
| 382 | * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache |
| 383 | * instead of head cache and allocate a cloned (child) skb. |
| 384 | * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for |
| 385 | * allocations in case the data is required for writeback |
| 386 | * @node: numa node to allocate memory on |
| 387 | * |
| 388 | * Allocate a new &sk_buff. The returned buffer has no headroom and a |
| 389 | * tail room of at least size bytes. The object has a reference count |
| 390 | * of one. The return is the buffer. On a failure the return is %NULL. |
| 391 | * |
| 392 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 393 | * %GFP_ATOMIC. |
| 394 | */ |
| 395 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
| 396 | int flags, int node) |
| 397 | { |
| 398 | struct kmem_cache *cache; |
Alexander Lobakin | 5381b23 | 2021-02-13 14:11:00 +0000 | [diff] [blame] | 399 | struct sk_buff *skb; |
| 400 | u8 *data; |
| 401 | bool pfmemalloc; |
| 402 | |
| 403 | cache = (flags & SKB_ALLOC_FCLONE) |
| 404 | ? skbuff_fclone_cache : skbuff_head_cache; |
| 405 | |
| 406 | if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) |
| 407 | gfp_mask |= __GFP_MEMALLOC; |
| 408 | |
| 409 | /* Get the HEAD */ |
Alexander Lobakin | d13612b | 2021-02-13 14:12:38 +0000 | [diff] [blame] | 410 | if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && |
| 411 | likely(node == NUMA_NO_NODE || node == numa_mem_id())) |
| 412 | skb = napi_skb_cache_get(); |
| 413 | else |
| 414 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); |
Alexander Lobakin | df1ae02 | 2021-02-13 14:11:39 +0000 | [diff] [blame] | 415 | if (unlikely(!skb)) |
| 416 | return NULL; |
Alexander Lobakin | 5381b23 | 2021-02-13 14:11:00 +0000 | [diff] [blame] | 417 | prefetchw(skb); |
| 418 | |
| 419 | /* We do our best to align skb_shared_info on a separate cache |
| 420 | * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives |
| 421 | * aligned memory blocks, unless SLUB/SLAB debug is enabled. |
| 422 | * Both skb->head and skb_shared_info are cache line aligned. |
| 423 | */ |
| 424 | size = SKB_DATA_ALIGN(size); |
| 425 | size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 426 | data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); |
Alexander Lobakin | df1ae02 | 2021-02-13 14:11:39 +0000 | [diff] [blame] | 427 | if (unlikely(!data)) |
Alexander Lobakin | 5381b23 | 2021-02-13 14:11:00 +0000 | [diff] [blame] | 428 | goto nodata; |
| 429 | /* kmalloc(size) might give us more room than requested. |
| 430 | * Put skb_shared_info exactly at the end of allocated zone, |
| 431 | * to allow max possible filling before reallocation. |
| 432 | */ |
| 433 | size = SKB_WITH_OVERHEAD(ksize(data)); |
| 434 | prefetchw(data + size); |
| 435 | |
| 436 | /* |
| 437 | * Only clear those fields we need to clear, not those that we will |
| 438 | * actually initialise below. Hence, don't put any more fields after |
| 439 | * the tail pointer in struct sk_buff! |
| 440 | */ |
| 441 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
Alexander Lobakin | f9d6725 | 2021-02-13 14:11:50 +0000 | [diff] [blame] | 442 | __build_skb_around(skb, data, 0); |
Alexander Lobakin | 5381b23 | 2021-02-13 14:11:00 +0000 | [diff] [blame] | 443 | skb->pfmemalloc = pfmemalloc; |
Alexander Lobakin | 5381b23 | 2021-02-13 14:11:00 +0000 | [diff] [blame] | 444 | |
| 445 | if (flags & SKB_ALLOC_FCLONE) { |
| 446 | struct sk_buff_fclones *fclones; |
| 447 | |
| 448 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
| 449 | |
| 450 | skb->fclone = SKB_FCLONE_ORIG; |
| 451 | refcount_set(&fclones->fclone_ref, 1); |
| 452 | |
| 453 | fclones->skb2.fclone = SKB_FCLONE_CLONE; |
| 454 | } |
| 455 | |
Alexander Lobakin | 5381b23 | 2021-02-13 14:11:00 +0000 | [diff] [blame] | 456 | return skb; |
Alexander Lobakin | df1ae02 | 2021-02-13 14:11:39 +0000 | [diff] [blame] | 457 | |
Alexander Lobakin | 5381b23 | 2021-02-13 14:11:00 +0000 | [diff] [blame] | 458 | nodata: |
| 459 | kmem_cache_free(cache, skb); |
Alexander Lobakin | df1ae02 | 2021-02-13 14:11:39 +0000 | [diff] [blame] | 460 | return NULL; |
Alexander Lobakin | 5381b23 | 2021-02-13 14:11:00 +0000 | [diff] [blame] | 461 | } |
| 462 | EXPORT_SYMBOL(__alloc_skb); |
| 463 | |
Sebastian Andrzej Siewior | 7ba7aea | 2019-06-07 21:20:34 +0200 | [diff] [blame] | 464 | /** |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 465 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device |
| 466 | * @dev: network device to receive on |
Masanari Iida | d749916 | 2015-08-24 22:56:54 +0900 | [diff] [blame] | 467 | * @len: length to allocate |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 468 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
| 469 | * |
| 470 | * Allocate a new &sk_buff and assign it a usage count of one. The |
| 471 | * buffer has NET_SKB_PAD headroom built in. Users should allocate |
| 472 | * the headroom they think they need without accounting for the |
| 473 | * built in space. The built in space is used for optimisations. |
| 474 | * |
| 475 | * %NULL is returned if there is no free memory. |
| 476 | */ |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 477 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, |
| 478 | gfp_t gfp_mask) |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 479 | { |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 480 | struct page_frag_cache *nc; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 481 | struct sk_buff *skb; |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 482 | bool pfmemalloc; |
| 483 | void *data; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 484 | |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 485 | len += NET_SKB_PAD; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 486 | |
Alexander Lobakin | 66c5560 | 2021-01-15 15:04:40 +0000 | [diff] [blame] | 487 | /* If requested length is either too small or too big, |
| 488 | * we use kmalloc() for skb->head allocation. |
| 489 | */ |
| 490 | if (len <= SKB_WITH_OVERHEAD(1024) || |
| 491 | len > SKB_WITH_OVERHEAD(PAGE_SIZE) || |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 492 | (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 493 | skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); |
| 494 | if (!skb) |
| 495 | goto skb_fail; |
| 496 | goto skb_success; |
| 497 | } |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 498 | |
| 499 | len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 500 | len = SKB_DATA_ALIGN(len); |
| 501 | |
| 502 | if (sk_memalloc_socks()) |
| 503 | gfp_mask |= __GFP_MEMALLOC; |
| 504 | |
Sebastian Andrzej Siewior | 92dcabd | 2019-06-07 21:20:35 +0200 | [diff] [blame] | 505 | if (in_irq() || irqs_disabled()) { |
| 506 | nc = this_cpu_ptr(&netdev_alloc_cache); |
| 507 | data = page_frag_alloc(nc, len, gfp_mask); |
| 508 | pfmemalloc = nc->pfmemalloc; |
| 509 | } else { |
| 510 | local_bh_disable(); |
| 511 | nc = this_cpu_ptr(&napi_alloc_cache.page); |
| 512 | data = page_frag_alloc(nc, len, gfp_mask); |
| 513 | pfmemalloc = nc->pfmemalloc; |
| 514 | local_bh_enable(); |
| 515 | } |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 516 | |
| 517 | if (unlikely(!data)) |
| 518 | return NULL; |
| 519 | |
| 520 | skb = __build_skb(data, len); |
| 521 | if (unlikely(!skb)) { |
Alexander Duyck | 181edb2 | 2015-05-06 21:12:03 -0700 | [diff] [blame] | 522 | skb_free_frag(data); |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 523 | return NULL; |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 524 | } |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 525 | |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 526 | if (pfmemalloc) |
| 527 | skb->pfmemalloc = 1; |
| 528 | skb->head_frag = 1; |
| 529 | |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 530 | skb_success: |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 531 | skb_reserve(skb, NET_SKB_PAD); |
| 532 | skb->dev = dev; |
| 533 | |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 534 | skb_fail: |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 535 | return skb; |
| 536 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 537 | EXPORT_SYMBOL(__netdev_alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 539 | /** |
| 540 | * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance |
| 541 | * @napi: napi instance this buffer was allocated for |
Masanari Iida | d749916 | 2015-08-24 22:56:54 +0900 | [diff] [blame] | 542 | * @len: length to allocate |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 543 | * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages |
| 544 | * |
| 545 | * Allocate a new sk_buff for use in NAPI receive. This buffer will |
| 546 | * attempt to allocate the head from a special reserved region used |
| 547 | * only for NAPI Rx allocation. By doing this we can save several |
| 548 | * CPU cycles by avoiding having to disable and re-enable IRQs. |
| 549 | * |
| 550 | * %NULL is returned if there is no free memory. |
| 551 | */ |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 552 | struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, |
| 553 | gfp_t gfp_mask) |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 554 | { |
Eric Dumazet | 3226b15 | 2021-01-13 08:18:19 -0800 | [diff] [blame] | 555 | struct napi_alloc_cache *nc; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 556 | struct sk_buff *skb; |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 557 | void *data; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 558 | |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 559 | len += NET_SKB_PAD + NET_IP_ALIGN; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 560 | |
Eric Dumazet | 3226b15 | 2021-01-13 08:18:19 -0800 | [diff] [blame] | 561 | /* If requested length is either too small or too big, |
| 562 | * we use kmalloc() for skb->head allocation. |
| 563 | */ |
| 564 | if (len <= SKB_WITH_OVERHEAD(1024) || |
| 565 | len > SKB_WITH_OVERHEAD(PAGE_SIZE) || |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 566 | (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { |
Alexander Lobakin | cfb8ec6 | 2021-02-13 14:12:49 +0000 | [diff] [blame] | 567 | skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, |
| 568 | NUMA_NO_NODE); |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 569 | if (!skb) |
| 570 | goto skb_fail; |
| 571 | goto skb_success; |
| 572 | } |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 573 | |
Eric Dumazet | 3226b15 | 2021-01-13 08:18:19 -0800 | [diff] [blame] | 574 | nc = this_cpu_ptr(&napi_alloc_cache); |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 575 | len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 576 | len = SKB_DATA_ALIGN(len); |
| 577 | |
| 578 | if (sk_memalloc_socks()) |
| 579 | gfp_mask |= __GFP_MEMALLOC; |
| 580 | |
Alexander Duyck | 8c2dd3e | 2017-01-10 16:58:06 -0800 | [diff] [blame] | 581 | data = page_frag_alloc(&nc->page, len, gfp_mask); |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 582 | if (unlikely(!data)) |
| 583 | return NULL; |
| 584 | |
Alexander Lobakin | cfb8ec6 | 2021-02-13 14:12:49 +0000 | [diff] [blame] | 585 | skb = __napi_build_skb(data, len); |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 586 | if (unlikely(!skb)) { |
Alexander Duyck | 181edb2 | 2015-05-06 21:12:03 -0700 | [diff] [blame] | 587 | skb_free_frag(data); |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 588 | return NULL; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 591 | if (nc->page.pfmemalloc) |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 592 | skb->pfmemalloc = 1; |
| 593 | skb->head_frag = 1; |
| 594 | |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 595 | skb_success: |
Alexander Duyck | 9451980 | 2015-05-06 21:11:40 -0700 | [diff] [blame] | 596 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
| 597 | skb->dev = napi->dev; |
| 598 | |
Alexander Duyck | a080e7b | 2015-05-13 13:34:13 -0700 | [diff] [blame] | 599 | skb_fail: |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 600 | return skb; |
| 601 | } |
| 602 | EXPORT_SYMBOL(__napi_alloc_skb); |
| 603 | |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 604 | 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] | 605 | int size, unsigned int truesize) |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 606 | { |
| 607 | skb_fill_page_desc(skb, i, page, off, size); |
| 608 | skb->len += size; |
| 609 | skb->data_len += size; |
Eric Dumazet | 50269e1 | 2012-03-23 23:59:33 +0000 | [diff] [blame] | 610 | skb->truesize += truesize; |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 611 | } |
| 612 | EXPORT_SYMBOL(skb_add_rx_frag); |
| 613 | |
Jason Wang | f8e617e | 2013-11-01 14:07:47 +0800 | [diff] [blame] | 614 | void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, |
| 615 | unsigned int truesize) |
| 616 | { |
| 617 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 618 | |
| 619 | skb_frag_size_add(frag, size); |
| 620 | skb->len += size; |
| 621 | skb->data_len += size; |
| 622 | skb->truesize += truesize; |
| 623 | } |
| 624 | EXPORT_SYMBOL(skb_coalesce_rx_frag); |
| 625 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 626 | static void skb_drop_list(struct sk_buff **listp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { |
Eric Dumazet | bd8a703 | 2013-06-24 06:26:00 -0700 | [diff] [blame] | 628 | kfree_skb_list(*listp); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 629 | *listp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 632 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
| 633 | { |
| 634 | skb_drop_list(&skb_shinfo(skb)->frag_list); |
| 635 | } |
| 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | static void skb_clone_fraglist(struct sk_buff *skb) |
| 638 | { |
| 639 | struct sk_buff *list; |
| 640 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 641 | skb_walk_frags(skb, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | skb_get(list); |
| 643 | } |
| 644 | |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 645 | static void skb_free_head(struct sk_buff *skb) |
| 646 | { |
Alexander Duyck | 181edb2 | 2015-05-06 21:12:03 -0700 | [diff] [blame] | 647 | unsigned char *head = skb->head; |
| 648 | |
Ilias Apalodimas | 6a5bcd8 | 2021-06-07 21:02:38 +0200 | [diff] [blame] | 649 | if (skb->head_frag) { |
| 650 | if (skb_pp_recycle(skb, head)) |
| 651 | return; |
Alexander Duyck | 181edb2 | 2015-05-06 21:12:03 -0700 | [diff] [blame] | 652 | skb_free_frag(head); |
Ilias Apalodimas | 6a5bcd8 | 2021-06-07 21:02:38 +0200 | [diff] [blame] | 653 | } else { |
Alexander Duyck | 181edb2 | 2015-05-06 21:12:03 -0700 | [diff] [blame] | 654 | kfree(head); |
Ilias Apalodimas | 6a5bcd8 | 2021-06-07 21:02:38 +0200 | [diff] [blame] | 655 | } |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Adrian Bunk | 5bba171 | 2006-06-29 13:02:35 -0700 | [diff] [blame] | 658 | static void skb_release_data(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 660 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 661 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 663 | if (skb->cloned && |
| 664 | atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, |
| 665 | &shinfo->dataref)) |
| 666 | return; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 667 | |
Jonathan Lemon | 70c4316 | 2021-01-06 14:18:36 -0800 | [diff] [blame] | 668 | skb_zcopy_clear(skb, true); |
| 669 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 670 | for (i = 0; i < shinfo->nr_frags; i++) |
Ilias Apalodimas | 6a5bcd8 | 2021-06-07 21:02:38 +0200 | [diff] [blame] | 671 | __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 672 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 673 | if (shinfo->frag_list) |
| 674 | kfree_skb_list(shinfo->frag_list); |
| 675 | |
| 676 | skb_free_head(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Free an skbuff by memory without cleaning the state. |
| 681 | */ |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 682 | static void kfree_skbmem(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 684 | struct sk_buff_fclones *fclones; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 685 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 686 | switch (skb->fclone) { |
| 687 | case SKB_FCLONE_UNAVAILABLE: |
| 688 | kmem_cache_free(skbuff_head_cache, skb); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 689 | return; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 690 | |
| 691 | case SKB_FCLONE_ORIG: |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 692 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 693 | |
| 694 | /* We usually free the clone (TX completion) before original skb |
| 695 | * This test would have no chance to be true for the clone, |
| 696 | * while here, branch prediction will be good. |
| 697 | */ |
Reshetova, Elena | 2638595 | 2017-06-30 13:07:59 +0300 | [diff] [blame] | 698 | if (refcount_read(&fclones->fclone_ref) == 1) |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 699 | goto fastpath; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 700 | break; |
| 701 | |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 702 | default: /* SKB_FCLONE_CLONE */ |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 703 | fclones = container_of(skb, struct sk_buff_fclones, skb2); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 704 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 705 | } |
Reshetova, Elena | 2638595 | 2017-06-30 13:07:59 +0300 | [diff] [blame] | 706 | if (!refcount_dec_and_test(&fclones->fclone_ref)) |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 707 | return; |
| 708 | fastpath: |
| 709 | kmem_cache_free(skbuff_fclone_cache, fclones); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 712 | void skb_release_head_state(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 714 | skb_dst_drop(skb); |
Stephen Hemminger | 9c2b332 | 2005-04-19 22:39:42 -0700 | [diff] [blame] | 715 | if (skb->destructor) { |
| 716 | WARN_ON(in_irq()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | skb->destructor(skb); |
| 718 | } |
Igor Maravić | a3bf7ae | 2011-12-12 02:58:22 +0000 | [diff] [blame] | 719 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
Florian Westphal | cb9c683 | 2017-01-23 18:21:56 +0100 | [diff] [blame] | 720 | nf_conntrack_put(skb_nfct(skb)); |
KOVACS Krisztian | 2fc72c7 | 2011-01-12 20:25:08 +0100 | [diff] [blame] | 721 | #endif |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 722 | skb_ext_put(skb); |
Lennert Buytenhek | 04a4bb5 | 2008-10-01 02:33:12 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /* Free everything but the sk_buff shell. */ |
| 726 | static void skb_release_all(struct sk_buff *skb) |
| 727 | { |
| 728 | skb_release_head_state(skb); |
Florian Westphal | a28b1b9 | 2017-07-23 19:54:47 +0200 | [diff] [blame] | 729 | if (likely(skb->head)) |
| 730 | skb_release_data(skb); |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 733 | /** |
| 734 | * __kfree_skb - private function |
| 735 | * @skb: buffer |
| 736 | * |
| 737 | * Free an sk_buff. Release anything attached to the buffer. |
| 738 | * Clean the state. This is an internal helper function. Users should |
| 739 | * always call kfree_skb |
| 740 | */ |
| 741 | |
| 742 | void __kfree_skb(struct sk_buff *skb) |
| 743 | { |
| 744 | skb_release_all(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | kfree_skbmem(skb); |
| 746 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 747 | EXPORT_SYMBOL(__kfree_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
| 749 | /** |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 750 | * kfree_skb - free an sk_buff |
| 751 | * @skb: buffer to free |
| 752 | * |
| 753 | * Drop a reference to the buffer and free it if the usage count has |
| 754 | * hit zero. |
| 755 | */ |
| 756 | void kfree_skb(struct sk_buff *skb) |
| 757 | { |
Paolo Abeni | 3889a803 | 2017-06-12 11:23:41 +0200 | [diff] [blame] | 758 | if (!skb_unref(skb)) |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 759 | return; |
Paolo Abeni | 3889a803 | 2017-06-12 11:23:41 +0200 | [diff] [blame] | 760 | |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 761 | trace_kfree_skb(skb, __builtin_return_address(0)); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 762 | __kfree_skb(skb); |
| 763 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 764 | EXPORT_SYMBOL(kfree_skb); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 765 | |
Eric Dumazet | bd8a703 | 2013-06-24 06:26:00 -0700 | [diff] [blame] | 766 | void kfree_skb_list(struct sk_buff *segs) |
| 767 | { |
| 768 | while (segs) { |
| 769 | struct sk_buff *next = segs->next; |
| 770 | |
| 771 | kfree_skb(segs); |
| 772 | segs = next; |
| 773 | } |
| 774 | } |
| 775 | EXPORT_SYMBOL(kfree_skb_list); |
| 776 | |
Willem de Bruijn | 6413139 | 2019-07-07 05:51:55 -0400 | [diff] [blame] | 777 | /* Dump skb information and contents. |
| 778 | * |
| 779 | * Must only be called from net_ratelimit()-ed paths. |
| 780 | * |
Vladimir Oltean | 302af7c | 2020-10-05 17:48:38 +0300 | [diff] [blame] | 781 | * Dumps whole packets if full_pkt, only headers otherwise. |
Willem de Bruijn | 6413139 | 2019-07-07 05:51:55 -0400 | [diff] [blame] | 782 | */ |
| 783 | void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) |
| 784 | { |
Willem de Bruijn | 6413139 | 2019-07-07 05:51:55 -0400 | [diff] [blame] | 785 | struct skb_shared_info *sh = skb_shinfo(skb); |
| 786 | struct net_device *dev = skb->dev; |
| 787 | struct sock *sk = skb->sk; |
| 788 | struct sk_buff *list_skb; |
| 789 | bool has_mac, has_trans; |
| 790 | int headroom, tailroom; |
| 791 | int i, len, seg_len; |
| 792 | |
| 793 | if (full_pkt) |
Willem de Bruijn | 6413139 | 2019-07-07 05:51:55 -0400 | [diff] [blame] | 794 | len = skb->len; |
| 795 | else |
| 796 | len = min_t(int, skb->len, MAX_HEADER + 128); |
| 797 | |
| 798 | headroom = skb_headroom(skb); |
| 799 | tailroom = skb_tailroom(skb); |
| 800 | |
| 801 | has_mac = skb_mac_header_was_set(skb); |
| 802 | has_trans = skb_transport_header_was_set(skb); |
| 803 | |
| 804 | printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" |
| 805 | "mac=(%d,%d) net=(%d,%d) trans=%d\n" |
| 806 | "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" |
| 807 | "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" |
| 808 | "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", |
| 809 | level, skb->len, headroom, skb_headlen(skb), tailroom, |
| 810 | has_mac ? skb->mac_header : -1, |
| 811 | has_mac ? skb_mac_header_len(skb) : -1, |
| 812 | skb->network_header, |
| 813 | has_trans ? skb_network_header_len(skb) : -1, |
| 814 | has_trans ? skb->transport_header : -1, |
| 815 | sh->tx_flags, sh->nr_frags, |
| 816 | sh->gso_size, sh->gso_type, sh->gso_segs, |
| 817 | skb->csum, skb->ip_summed, skb->csum_complete_sw, |
| 818 | skb->csum_valid, skb->csum_level, |
| 819 | skb->hash, skb->sw_hash, skb->l4_hash, |
| 820 | ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); |
| 821 | |
| 822 | if (dev) |
| 823 | printk("%sdev name=%s feat=0x%pNF\n", |
| 824 | level, dev->name, &dev->features); |
| 825 | if (sk) |
Qian Cai | db8051f | 2019-07-16 11:43:05 -0400 | [diff] [blame] | 826 | printk("%ssk family=%hu type=%u proto=%u\n", |
Willem de Bruijn | 6413139 | 2019-07-07 05:51:55 -0400 | [diff] [blame] | 827 | level, sk->sk_family, sk->sk_type, sk->sk_protocol); |
| 828 | |
| 829 | if (full_pkt && headroom) |
| 830 | print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, |
| 831 | 16, 1, skb->head, headroom, false); |
| 832 | |
| 833 | seg_len = min_t(int, skb_headlen(skb), len); |
| 834 | if (seg_len) |
| 835 | print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, |
| 836 | 16, 1, skb->data, seg_len, false); |
| 837 | len -= seg_len; |
| 838 | |
| 839 | if (full_pkt && tailroom) |
| 840 | print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, |
| 841 | 16, 1, skb_tail_pointer(skb), tailroom, false); |
| 842 | |
| 843 | for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { |
| 844 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 845 | u32 p_off, p_len, copied; |
| 846 | struct page *p; |
| 847 | u8 *vaddr; |
| 848 | |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 849 | skb_frag_foreach_page(frag, skb_frag_off(frag), |
Willem de Bruijn | 6413139 | 2019-07-07 05:51:55 -0400 | [diff] [blame] | 850 | skb_frag_size(frag), p, p_off, p_len, |
| 851 | copied) { |
| 852 | seg_len = min_t(int, p_len, len); |
| 853 | vaddr = kmap_atomic(p); |
| 854 | print_hex_dump(level, "skb frag: ", |
| 855 | DUMP_PREFIX_OFFSET, |
| 856 | 16, 1, vaddr + p_off, seg_len, false); |
| 857 | kunmap_atomic(vaddr); |
| 858 | len -= seg_len; |
| 859 | if (!len) |
| 860 | break; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | if (full_pkt && skb_has_frag_list(skb)) { |
| 865 | printk("skb fraglist:\n"); |
| 866 | skb_walk_frags(skb, list_skb) |
| 867 | skb_dump(level, list_skb, true); |
| 868 | } |
| 869 | } |
| 870 | EXPORT_SYMBOL(skb_dump); |
| 871 | |
Stephen Hemminger | d1a203e | 2008-11-01 21:01:09 -0700 | [diff] [blame] | 872 | /** |
Michael S. Tsirkin | 2512117 | 2012-11-01 09:16:28 +0000 | [diff] [blame] | 873 | * skb_tx_error - report an sk_buff xmit error |
| 874 | * @skb: buffer that triggered an error |
| 875 | * |
| 876 | * Report xmit error if a device callback is tracking this skb. |
| 877 | * skb must be freed afterwards. |
| 878 | */ |
| 879 | void skb_tx_error(struct sk_buff *skb) |
| 880 | { |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 881 | skb_zcopy_clear(skb, true); |
Michael S. Tsirkin | 2512117 | 2012-11-01 09:16:28 +0000 | [diff] [blame] | 882 | } |
| 883 | EXPORT_SYMBOL(skb_tx_error); |
| 884 | |
Herbert Xu | be769db | 2020-08-22 08:23:29 +1000 | [diff] [blame] | 885 | #ifdef CONFIG_TRACEPOINTS |
Michael S. Tsirkin | 2512117 | 2012-11-01 09:16:28 +0000 | [diff] [blame] | 886 | /** |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 887 | * consume_skb - free an skbuff |
| 888 | * @skb: buffer to free |
| 889 | * |
| 890 | * Drop a ref to the buffer and free it if the usage count has hit zero |
| 891 | * Functions identically to kfree_skb, but kfree_skb assumes that the frame |
| 892 | * is being dropped after a failure and notes that |
| 893 | */ |
| 894 | void consume_skb(struct sk_buff *skb) |
| 895 | { |
Paolo Abeni | 3889a803 | 2017-06-12 11:23:41 +0200 | [diff] [blame] | 896 | if (!skb_unref(skb)) |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 897 | return; |
Paolo Abeni | 3889a803 | 2017-06-12 11:23:41 +0200 | [diff] [blame] | 898 | |
Koki Sanagi | 07dc22e | 2010-08-23 18:46:12 +0900 | [diff] [blame] | 899 | trace_consume_skb(skb); |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 900 | __kfree_skb(skb); |
| 901 | } |
| 902 | EXPORT_SYMBOL(consume_skb); |
Herbert Xu | be769db | 2020-08-22 08:23:29 +1000 | [diff] [blame] | 903 | #endif |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 904 | |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 905 | /** |
Mauro Carvalho Chehab | c1639be | 2020-11-16 11:17:58 +0100 | [diff] [blame] | 906 | * __consume_stateless_skb - free an skbuff, assuming it is stateless |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 907 | * @skb: buffer to free |
| 908 | * |
Paolo Abeni | ca2c141 | 2017-09-06 14:44:36 +0200 | [diff] [blame] | 909 | * Alike consume_skb(), but this variant assumes that this is the last |
| 910 | * skb reference and all the head states have been already dropped |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 911 | */ |
Paolo Abeni | ca2c141 | 2017-09-06 14:44:36 +0200 | [diff] [blame] | 912 | void __consume_stateless_skb(struct sk_buff *skb) |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 913 | { |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 914 | trace_consume_skb(skb); |
Florian Westphal | 06dc75a | 2017-07-17 18:56:54 +0200 | [diff] [blame] | 915 | skb_release_data(skb); |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 916 | kfree_skbmem(skb); |
| 917 | } |
| 918 | |
Alexander Lobakin | f450d53 | 2021-02-13 14:12:25 +0000 | [diff] [blame] | 919 | static void napi_skb_cache_put(struct sk_buff *skb) |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 920 | { |
| 921 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
Alexander Lobakin | f450d53 | 2021-02-13 14:12:25 +0000 | [diff] [blame] | 922 | u32 i; |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 923 | |
Alexander Lobakin | f450d53 | 2021-02-13 14:12:25 +0000 | [diff] [blame] | 924 | kasan_poison_object_data(skbuff_head_cache, skb); |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 925 | nc->skb_cache[nc->skb_count++] = skb; |
| 926 | |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 927 | if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { |
Alexander Lobakin | f450d53 | 2021-02-13 14:12:25 +0000 | [diff] [blame] | 928 | for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) |
| 929 | kasan_unpoison_object_data(skbuff_head_cache, |
| 930 | nc->skb_cache[i]); |
| 931 | |
| 932 | kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF, |
| 933 | nc->skb_cache + NAPI_SKB_CACHE_HALF); |
| 934 | nc->skb_count = NAPI_SKB_CACHE_HALF; |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 935 | } |
| 936 | } |
Alexander Lobakin | f450d53 | 2021-02-13 14:12:25 +0000 | [diff] [blame] | 937 | |
Jesper Dangaard Brouer | 15fad71 | 2016-02-08 13:15:04 +0100 | [diff] [blame] | 938 | void __kfree_skb_defer(struct sk_buff *skb) |
| 939 | { |
Alexander Lobakin | 9243adf | 2021-02-13 14:13:09 +0000 | [diff] [blame] | 940 | skb_release_all(skb); |
| 941 | napi_skb_cache_put(skb); |
| 942 | } |
| 943 | |
| 944 | void napi_skb_free_stolen_head(struct sk_buff *skb) |
| 945 | { |
| 946 | skb_dst_drop(skb); |
| 947 | skb_ext_put(skb); |
Alexander Lobakin | f450d53 | 2021-02-13 14:12:25 +0000 | [diff] [blame] | 948 | napi_skb_cache_put(skb); |
Jesper Dangaard Brouer | 15fad71 | 2016-02-08 13:15:04 +0100 | [diff] [blame] | 949 | } |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 950 | |
| 951 | void napi_consume_skb(struct sk_buff *skb, int budget) |
| 952 | { |
Jesper Dangaard Brouer | 885eb0a | 2016-03-11 09:43:58 +0100 | [diff] [blame] | 953 | /* Zero budget indicate non-NAPI context called us, like netpoll */ |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 954 | if (unlikely(!budget)) { |
Jesper Dangaard Brouer | 885eb0a | 2016-03-11 09:43:58 +0100 | [diff] [blame] | 955 | dev_consume_skb_any(skb); |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 956 | return; |
| 957 | } |
| 958 | |
Yunsheng Lin | 6454eca | 2020-11-24 18:49:29 +0800 | [diff] [blame] | 959 | lockdep_assert_in_softirq(); |
| 960 | |
Paolo Abeni | 7608894 | 2017-06-14 11:48:48 +0200 | [diff] [blame] | 961 | if (!skb_unref(skb)) |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 962 | return; |
Paolo Abeni | 7608894 | 2017-06-14 11:48:48 +0200 | [diff] [blame] | 963 | |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 964 | /* if reaching here SKB is ready to free */ |
| 965 | trace_consume_skb(skb); |
| 966 | |
| 967 | /* if SKB is a clone, don't handle this case */ |
Eric Dumazet | abbdb5a | 2016-03-20 11:27:47 -0700 | [diff] [blame] | 968 | if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 969 | __kfree_skb(skb); |
| 970 | return; |
| 971 | } |
| 972 | |
Alexander Lobakin | 9243adf | 2021-02-13 14:13:09 +0000 | [diff] [blame] | 973 | skb_release_all(skb); |
Alexander Lobakin | f450d53 | 2021-02-13 14:12:25 +0000 | [diff] [blame] | 974 | napi_skb_cache_put(skb); |
Jesper Dangaard Brouer | 795bb1c | 2016-02-08 13:14:59 +0100 | [diff] [blame] | 975 | } |
| 976 | EXPORT_SYMBOL(napi_consume_skb); |
| 977 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 978 | /* Make sure a field is enclosed inside headers_start/headers_end section */ |
| 979 | #define CHECK_SKB_FIELD(field) \ |
| 980 | BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ |
| 981 | offsetof(struct sk_buff, headers_start)); \ |
| 982 | BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ |
| 983 | offsetof(struct sk_buff, headers_end)); \ |
| 984 | |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 985 | static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 986 | { |
| 987 | new->tstamp = old->tstamp; |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 988 | /* We do not copy old->sk */ |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 989 | new->dev = old->dev; |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 990 | memcpy(new->cb, old->cb, sizeof(old->cb)); |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 991 | skb_dst_copy(new, old); |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 992 | __skb_ext_copy(new, old); |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 993 | __nf_copy(new, old, false); |
Patrick McHardy | 6aa895b | 2008-07-14 22:49:06 -0700 | [diff] [blame] | 994 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 995 | /* Note : this field could be in headers_start/headers_end section |
| 996 | * It is not yet because we do not want to have a 16 bit hole |
| 997 | */ |
| 998 | new->queue_mapping = old->queue_mapping; |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 999 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 1000 | memcpy(&new->headers_start, &old->headers_start, |
| 1001 | offsetof(struct sk_buff, headers_end) - |
| 1002 | offsetof(struct sk_buff, headers_start)); |
| 1003 | CHECK_SKB_FIELD(protocol); |
| 1004 | CHECK_SKB_FIELD(csum); |
| 1005 | CHECK_SKB_FIELD(hash); |
| 1006 | CHECK_SKB_FIELD(priority); |
| 1007 | CHECK_SKB_FIELD(skb_iif); |
| 1008 | CHECK_SKB_FIELD(vlan_proto); |
| 1009 | CHECK_SKB_FIELD(vlan_tci); |
| 1010 | CHECK_SKB_FIELD(transport_header); |
| 1011 | CHECK_SKB_FIELD(network_header); |
| 1012 | CHECK_SKB_FIELD(mac_header); |
| 1013 | CHECK_SKB_FIELD(inner_protocol); |
| 1014 | CHECK_SKB_FIELD(inner_transport_header); |
| 1015 | CHECK_SKB_FIELD(inner_network_header); |
| 1016 | CHECK_SKB_FIELD(inner_mac_header); |
| 1017 | CHECK_SKB_FIELD(mark); |
| 1018 | #ifdef CONFIG_NETWORK_SECMARK |
| 1019 | CHECK_SKB_FIELD(secmark); |
| 1020 | #endif |
Cong Wang | e0d1095 | 2013-08-01 11:10:25 +0800 | [diff] [blame] | 1021 | #ifdef CONFIG_NET_RX_BUSY_POLL |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 1022 | CHECK_SKB_FIELD(napi_id); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 1023 | #endif |
Eric Dumazet | 2bd8248 | 2015-02-03 23:48:24 -0800 | [diff] [blame] | 1024 | #ifdef CONFIG_XPS |
| 1025 | CHECK_SKB_FIELD(sender_cpu); |
| 1026 | #endif |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 1027 | #ifdef CONFIG_NET_SCHED |
| 1028 | CHECK_SKB_FIELD(tc_index); |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 1029 | #endif |
| 1030 | |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Herbert Xu | 82c49a3 | 2009-05-22 22:11:37 +0000 | [diff] [blame] | 1033 | /* |
| 1034 | * You should not add any new code to this function. Add it to |
| 1035 | * __copy_skb_header above instead. |
| 1036 | */ |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1037 | 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] | 1038 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | #define C(x) n->x = skb->x |
| 1040 | |
| 1041 | n->next = n->prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | n->sk = NULL; |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 1043 | __copy_skb_header(n, skb); |
| 1044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | C(len); |
| 1046 | C(data_len); |
Alexey Dobriyan | 3e6b3b2 | 2007-03-16 15:00:46 -0700 | [diff] [blame] | 1047 | C(mac_len); |
Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 1048 | n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 1049 | n->cloned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | n->nohdr = 0; |
Eric Dumazet | b13dda9 | 2018-04-07 13:42:39 -0700 | [diff] [blame] | 1051 | n->peeked = 0; |
Stefano Brivio | e78bfb0 | 2018-07-13 13:21:07 +0200 | [diff] [blame] | 1052 | C(pfmemalloc); |
Ilias Apalodimas | 6a5bcd8 | 2021-06-07 21:02:38 +0200 | [diff] [blame] | 1053 | C(pp_recycle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | n->destructor = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | C(tail); |
| 1056 | C(end); |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 1057 | C(head); |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 1058 | C(head_frag); |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 1059 | C(data); |
| 1060 | C(truesize); |
Reshetova, Elena | 6335479 | 2017-06-30 13:07:58 +0300 | [diff] [blame] | 1061 | refcount_set(&n->users, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
| 1063 | atomic_inc(&(skb_shinfo(skb)->dataref)); |
| 1064 | skb->cloned = 1; |
| 1065 | |
| 1066 | return n; |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1067 | #undef C |
| 1068 | } |
| 1069 | |
| 1070 | /** |
Jakub Kicinski | da29e4b | 2019-06-03 15:16:58 -0700 | [diff] [blame] | 1071 | * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg |
| 1072 | * @first: first sk_buff of the msg |
| 1073 | */ |
| 1074 | struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) |
| 1075 | { |
| 1076 | struct sk_buff *n; |
| 1077 | |
| 1078 | n = alloc_skb(0, GFP_ATOMIC); |
| 1079 | if (!n) |
| 1080 | return NULL; |
| 1081 | |
| 1082 | n->len = first->len; |
| 1083 | n->data_len = first->len; |
| 1084 | n->truesize = first->truesize; |
| 1085 | |
| 1086 | skb_shinfo(n)->frag_list = first; |
| 1087 | |
| 1088 | __copy_skb_header(n, first); |
| 1089 | n->destructor = NULL; |
| 1090 | |
| 1091 | return n; |
| 1092 | } |
| 1093 | EXPORT_SYMBOL_GPL(alloc_skb_for_msg); |
| 1094 | |
| 1095 | /** |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1096 | * skb_morph - morph one skb into another |
| 1097 | * @dst: the skb to receive the contents |
| 1098 | * @src: the skb to supply the contents |
| 1099 | * |
| 1100 | * This is identical to skb_clone except that the target skb is |
| 1101 | * supplied by the user. |
| 1102 | * |
| 1103 | * The target skb is returned upon exit. |
| 1104 | */ |
| 1105 | struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) |
| 1106 | { |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 1107 | skb_release_all(dst); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1108 | return __skb_clone(dst, src); |
| 1109 | } |
| 1110 | EXPORT_SYMBOL_GPL(skb_morph); |
| 1111 | |
Sowmini Varadhan | 6f89dbc | 2018-02-15 10:49:32 -0800 | [diff] [blame] | 1112 | int mm_account_pinned_pages(struct mmpin *mmp, size_t size) |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1113 | { |
| 1114 | unsigned long max_pg, num_pg, new_pg, old_pg; |
| 1115 | struct user_struct *user; |
| 1116 | |
| 1117 | if (capable(CAP_IPC_LOCK) || !size) |
| 1118 | return 0; |
| 1119 | |
| 1120 | num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ |
| 1121 | max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 1122 | user = mmp->user ? : current_user(); |
| 1123 | |
| 1124 | do { |
| 1125 | old_pg = atomic_long_read(&user->locked_vm); |
| 1126 | new_pg = old_pg + num_pg; |
| 1127 | if (new_pg > max_pg) |
| 1128 | return -ENOBUFS; |
| 1129 | } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != |
| 1130 | old_pg); |
| 1131 | |
| 1132 | if (!mmp->user) { |
| 1133 | mmp->user = get_uid(user); |
| 1134 | mmp->num_pg = num_pg; |
| 1135 | } else { |
| 1136 | mmp->num_pg += num_pg; |
| 1137 | } |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
Sowmini Varadhan | 6f89dbc | 2018-02-15 10:49:32 -0800 | [diff] [blame] | 1141 | EXPORT_SYMBOL_GPL(mm_account_pinned_pages); |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1142 | |
Sowmini Varadhan | 6f89dbc | 2018-02-15 10:49:32 -0800 | [diff] [blame] | 1143 | void mm_unaccount_pinned_pages(struct mmpin *mmp) |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1144 | { |
| 1145 | if (mmp->user) { |
| 1146 | atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); |
| 1147 | free_uid(mmp->user); |
| 1148 | } |
| 1149 | } |
Sowmini Varadhan | 6f89dbc | 2018-02-15 10:49:32 -0800 | [diff] [blame] | 1150 | EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1151 | |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1152 | struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1153 | { |
| 1154 | struct ubuf_info *uarg; |
| 1155 | struct sk_buff *skb; |
| 1156 | |
| 1157 | WARN_ON_ONCE(!in_task()); |
| 1158 | |
| 1159 | skb = sock_omalloc(sk, 0, GFP_KERNEL); |
| 1160 | if (!skb) |
| 1161 | return NULL; |
| 1162 | |
| 1163 | BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); |
| 1164 | uarg = (void *)skb->cb; |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1165 | uarg->mmp.user = NULL; |
| 1166 | |
| 1167 | if (mm_account_pinned_pages(&uarg->mmp, size)) { |
| 1168 | kfree_skb(skb); |
| 1169 | return NULL; |
| 1170 | } |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1171 | |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1172 | uarg->callback = msg_zerocopy_callback; |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1173 | uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; |
| 1174 | uarg->len = 1; |
| 1175 | uarg->bytelen = size; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1176 | uarg->zerocopy = 1; |
Jonathan Lemon | 04c2d33 | 2021-01-06 14:18:39 -0800 | [diff] [blame] | 1177 | uarg->flags = SKBFL_ZEROCOPY_FRAG; |
Eric Dumazet | c1d1b43 | 2017-08-31 16:48:22 -0700 | [diff] [blame] | 1178 | refcount_set(&uarg->refcnt, 1); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1179 | sock_hold(sk); |
| 1180 | |
| 1181 | return uarg; |
| 1182 | } |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1183 | EXPORT_SYMBOL_GPL(msg_zerocopy_alloc); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1184 | |
| 1185 | static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg) |
| 1186 | { |
| 1187 | return container_of((void *)uarg, struct sk_buff, cb); |
| 1188 | } |
| 1189 | |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1190 | struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, |
| 1191 | struct ubuf_info *uarg) |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1192 | { |
| 1193 | if (uarg) { |
| 1194 | const u32 byte_limit = 1 << 19; /* limit to a few TSO */ |
| 1195 | u32 bytelen, next; |
| 1196 | |
| 1197 | /* realloc only when socket is locked (TCP, UDP cork), |
| 1198 | * so uarg->len and sk_zckey access is serialized |
| 1199 | */ |
| 1200 | if (!sock_owned_by_user(sk)) { |
| 1201 | WARN_ON_ONCE(1); |
| 1202 | return NULL; |
| 1203 | } |
| 1204 | |
| 1205 | bytelen = uarg->bytelen + size; |
| 1206 | if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) { |
| 1207 | /* TCP can create new skb to attach new uarg */ |
| 1208 | if (sk->sk_type == SOCK_STREAM) |
| 1209 | goto new_alloc; |
| 1210 | return NULL; |
| 1211 | } |
| 1212 | |
| 1213 | next = (u32)atomic_read(&sk->sk_zckey); |
| 1214 | if ((u32)(uarg->id + uarg->len) == next) { |
Willem de Bruijn | a91dbff | 2017-08-03 16:29:43 -0400 | [diff] [blame] | 1215 | if (mm_account_pinned_pages(&uarg->mmp, size)) |
| 1216 | return NULL; |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1217 | uarg->len++; |
| 1218 | uarg->bytelen = bytelen; |
| 1219 | atomic_set(&sk->sk_zckey, ++next); |
Willem de Bruijn | 100f6d8 | 2019-05-30 18:01:21 -0400 | [diff] [blame] | 1220 | |
| 1221 | /* no extra ref when appending to datagram (MSG_MORE) */ |
| 1222 | if (sk->sk_type == SOCK_STREAM) |
Jonathan Lemon | 8e04491 | 2021-01-06 14:18:41 -0800 | [diff] [blame] | 1223 | net_zcopy_get(uarg); |
Willem de Bruijn | 100f6d8 | 2019-05-30 18:01:21 -0400 | [diff] [blame] | 1224 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1225 | return uarg; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | new_alloc: |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1230 | return msg_zerocopy_alloc(sk, size); |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1231 | } |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1232 | EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1233 | |
| 1234 | static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) |
| 1235 | { |
| 1236 | struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); |
| 1237 | u32 old_lo, old_hi; |
| 1238 | u64 sum_len; |
| 1239 | |
| 1240 | old_lo = serr->ee.ee_info; |
| 1241 | old_hi = serr->ee.ee_data; |
| 1242 | sum_len = old_hi - old_lo + 1ULL + len; |
| 1243 | |
| 1244 | if (sum_len >= (1ULL << 32)) |
| 1245 | return false; |
| 1246 | |
| 1247 | if (lo != old_hi + 1) |
| 1248 | return false; |
| 1249 | |
| 1250 | serr->ee.ee_data += len; |
| 1251 | return true; |
| 1252 | } |
| 1253 | |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1254 | static void __msg_zerocopy_callback(struct ubuf_info *uarg) |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1255 | { |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1256 | struct sk_buff *tail, *skb = skb_from_uarg(uarg); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1257 | struct sock_exterr_skb *serr; |
| 1258 | struct sock *sk = skb->sk; |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1259 | struct sk_buff_head *q; |
| 1260 | unsigned long flags; |
Willem de Bruijn | 3bdd5ee | 2021-06-09 18:41:57 -0400 | [diff] [blame] | 1261 | bool is_zerocopy; |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1262 | u32 lo, hi; |
| 1263 | u16 len; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1264 | |
Willem de Bruijn | ccaffff | 2017-08-09 19:09:43 -0400 | [diff] [blame] | 1265 | mm_unaccount_pinned_pages(&uarg->mmp); |
| 1266 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1267 | /* if !len, there was only 1 call, and it was aborted |
| 1268 | * so do not queue a completion notification |
| 1269 | */ |
| 1270 | if (!uarg->len || sock_flag(sk, SOCK_DEAD)) |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1271 | goto release; |
| 1272 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1273 | len = uarg->len; |
| 1274 | lo = uarg->id; |
| 1275 | hi = uarg->id + len - 1; |
Willem de Bruijn | 3bdd5ee | 2021-06-09 18:41:57 -0400 | [diff] [blame] | 1276 | is_zerocopy = uarg->zerocopy; |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1277 | |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1278 | serr = SKB_EXT_ERR(skb); |
| 1279 | memset(serr, 0, sizeof(*serr)); |
| 1280 | serr->ee.ee_errno = 0; |
| 1281 | serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1282 | serr->ee.ee_data = hi; |
| 1283 | serr->ee.ee_info = lo; |
Willem de Bruijn | 3bdd5ee | 2021-06-09 18:41:57 -0400 | [diff] [blame] | 1284 | if (!is_zerocopy) |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1285 | serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; |
| 1286 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1287 | q = &sk->sk_error_queue; |
| 1288 | spin_lock_irqsave(&q->lock, flags); |
| 1289 | tail = skb_peek_tail(q); |
| 1290 | if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || |
| 1291 | !skb_zerocopy_notify_extend(tail, lo, len)) { |
| 1292 | __skb_queue_tail(q, skb); |
| 1293 | skb = NULL; |
| 1294 | } |
| 1295 | spin_unlock_irqrestore(&q->lock, flags); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1296 | |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame^] | 1297 | sk_error_report(sk); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1298 | |
| 1299 | release: |
| 1300 | consume_skb(skb); |
| 1301 | sock_put(sk); |
| 1302 | } |
Jonathan Lemon | 7551885 | 2021-01-06 14:18:31 -0800 | [diff] [blame] | 1303 | |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1304 | void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, |
| 1305 | bool success) |
Jonathan Lemon | 7551885 | 2021-01-06 14:18:31 -0800 | [diff] [blame] | 1306 | { |
| 1307 | uarg->zerocopy = uarg->zerocopy & success; |
| 1308 | |
| 1309 | if (refcount_dec_and_test(&uarg->refcnt)) |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1310 | __msg_zerocopy_callback(uarg); |
Jonathan Lemon | 7551885 | 2021-01-06 14:18:31 -0800 | [diff] [blame] | 1311 | } |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1312 | EXPORT_SYMBOL_GPL(msg_zerocopy_callback); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1313 | |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1314 | void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1315 | { |
Jonathan Lemon | 236a6b1 | 2021-01-06 14:18:35 -0800 | [diff] [blame] | 1316 | struct sock *sk = skb_from_uarg(uarg)->sk; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1317 | |
Jonathan Lemon | 236a6b1 | 2021-01-06 14:18:35 -0800 | [diff] [blame] | 1318 | atomic_dec(&sk->sk_zckey); |
| 1319 | uarg->len--; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1320 | |
Jonathan Lemon | 236a6b1 | 2021-01-06 14:18:35 -0800 | [diff] [blame] | 1321 | if (have_uref) |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1322 | msg_zerocopy_callback(NULL, uarg, true); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1323 | } |
Jonathan Lemon | 8c79382 | 2021-01-06 14:18:37 -0800 | [diff] [blame] | 1324 | EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1325 | |
Willem de Bruijn | b5947e5 | 2018-11-30 15:32:39 -0500 | [diff] [blame] | 1326 | int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len) |
| 1327 | { |
| 1328 | return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len); |
| 1329 | } |
| 1330 | EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram); |
| 1331 | |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1332 | int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, |
| 1333 | struct msghdr *msg, int len, |
| 1334 | struct ubuf_info *uarg) |
| 1335 | { |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1336 | struct ubuf_info *orig_uarg = skb_zcopy(skb); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1337 | struct iov_iter orig_iter = msg->msg_iter; |
| 1338 | int err, orig_len = skb->len; |
| 1339 | |
Willem de Bruijn | 4ab6c99 | 2017-08-03 16:29:42 -0400 | [diff] [blame] | 1340 | /* An skb can only point to one uarg. This edge case happens when |
| 1341 | * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. |
| 1342 | */ |
| 1343 | if (orig_uarg && uarg != orig_uarg) |
| 1344 | return -EEXIST; |
| 1345 | |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1346 | err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len); |
| 1347 | if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { |
Willem de Bruijn | 54d43117 | 2017-10-19 12:40:39 -0400 | [diff] [blame] | 1348 | struct sock *save_sk = skb->sk; |
| 1349 | |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1350 | /* Streams do not free skb on error. Reset to prev state. */ |
| 1351 | msg->msg_iter = orig_iter; |
Willem de Bruijn | 54d43117 | 2017-10-19 12:40:39 -0400 | [diff] [blame] | 1352 | skb->sk = sk; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1353 | ___pskb_trim(skb, orig_len); |
Willem de Bruijn | 54d43117 | 2017-10-19 12:40:39 -0400 | [diff] [blame] | 1354 | skb->sk = save_sk; |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1355 | return err; |
| 1356 | } |
| 1357 | |
Willem de Bruijn | 52900d2 | 2018-11-30 15:32:40 -0500 | [diff] [blame] | 1358 | skb_zcopy_set(skb, uarg, NULL); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1359 | return skb->len - orig_len; |
| 1360 | } |
| 1361 | EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); |
| 1362 | |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 1363 | 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] | 1364 | gfp_t gfp_mask) |
| 1365 | { |
| 1366 | if (skb_zcopy(orig)) { |
| 1367 | if (skb_zcopy(nskb)) { |
| 1368 | /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ |
| 1369 | if (!gfp_mask) { |
| 1370 | WARN_ON_ONCE(1); |
| 1371 | return -ENOMEM; |
| 1372 | } |
| 1373 | if (skb_uarg(nskb) == skb_uarg(orig)) |
| 1374 | return 0; |
| 1375 | if (skb_copy_ubufs(nskb, GFP_ATOMIC)) |
| 1376 | return -EIO; |
| 1377 | } |
Willem de Bruijn | 52900d2 | 2018-11-30 15:32:40 -0500 | [diff] [blame] | 1378 | skb_zcopy_set(nskb, skb_uarg(orig), NULL); |
Willem de Bruijn | 5226779 | 2017-08-03 16:29:39 -0400 | [diff] [blame] | 1379 | } |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 1383 | /** |
| 1384 | * skb_copy_ubufs - copy userspace skb frags buffers to kernel |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 1385 | * @skb: the skb to modify |
| 1386 | * @gfp_mask: allocation priority |
| 1387 | * |
Jonathan Lemon | 06b4feb | 2021-01-06 14:18:38 -0800 | [diff] [blame] | 1388 | * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 1389 | * It will copy all frags into kernel and drop the reference |
| 1390 | * to userspace pages. |
| 1391 | * |
| 1392 | * If this function is called from an interrupt gfp_mask() must be |
| 1393 | * %GFP_ATOMIC. |
| 1394 | * |
| 1395 | * Returns 0 on success or a negative error code on failure |
| 1396 | * to allocate kernel memory to copy to. |
| 1397 | */ |
| 1398 | int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) |
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 | int num_frags = skb_shinfo(skb)->nr_frags; |
| 1401 | struct page *page, *head = NULL; |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1402 | int i, new_frags; |
| 1403 | u32 d_off; |
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 | if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) |
| 1406 | return -EINVAL; |
| 1407 | |
Willem de Bruijn | f72c4ac | 2017-12-28 12:38:13 -0500 | [diff] [blame] | 1408 | if (!num_frags) |
| 1409 | goto release; |
| 1410 | |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1411 | new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1412 | for (i = 0; i < new_frags; i++) { |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 1413 | page = alloc_page(gfp_mask); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1414 | if (!page) { |
| 1415 | while (head) { |
Sunghan Suh | 40dadff | 2013-07-12 16:17:23 +0900 | [diff] [blame] | 1416 | struct page *next = (struct page *)page_private(head); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1417 | put_page(head); |
| 1418 | head = next; |
| 1419 | } |
| 1420 | return -ENOMEM; |
| 1421 | } |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1422 | set_page_private(page, (unsigned long)head); |
| 1423 | head = page; |
| 1424 | } |
| 1425 | |
| 1426 | page = head; |
| 1427 | d_off = 0; |
| 1428 | for (i = 0; i < num_frags; i++) { |
| 1429 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
| 1430 | u32 p_off, p_len, copied; |
| 1431 | struct page *p; |
| 1432 | u8 *vaddr; |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 1433 | |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 1434 | skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 1435 | p, p_off, p_len, copied) { |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1436 | u32 copy, done = 0; |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 1437 | vaddr = kmap_atomic(p); |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1438 | |
| 1439 | while (done < p_len) { |
| 1440 | if (d_off == PAGE_SIZE) { |
| 1441 | d_off = 0; |
| 1442 | page = (struct page *)page_private(page); |
| 1443 | } |
| 1444 | copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); |
| 1445 | memcpy(page_address(page) + d_off, |
| 1446 | vaddr + p_off + done, copy); |
| 1447 | done += copy; |
| 1448 | d_off += copy; |
| 1449 | } |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 1450 | kunmap_atomic(vaddr); |
| 1451 | } |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | /* skb frags release userspace buffers */ |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 1455 | for (i = 0; i < num_frags; i++) |
Ian Campbell | a8605c6 | 2011-10-19 23:01:49 +0000 | [diff] [blame] | 1456 | skb_frag_unref(skb, i); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1457 | |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1458 | /* skb frags point to kernel buffers */ |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1459 | for (i = 0; i < new_frags - 1; i++) { |
| 1460 | __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); |
Sunghan Suh | 40dadff | 2013-07-12 16:17:23 +0900 | [diff] [blame] | 1461 | head = (struct page *)page_private(head); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1462 | } |
Willem de Bruijn | 3ece782 | 2017-08-03 16:29:38 -0400 | [diff] [blame] | 1463 | __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); |
| 1464 | skb_shinfo(skb)->nr_frags = new_frags; |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 1465 | |
Willem de Bruijn | b90ddd5 | 2017-12-20 17:37:50 -0500 | [diff] [blame] | 1466 | release: |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 1467 | skb_zcopy_clear(skb, false); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1468 | return 0; |
| 1469 | } |
Michael S. Tsirkin | dcc0fb7 | 2012-07-20 09:23:20 +0000 | [diff] [blame] | 1470 | EXPORT_SYMBOL_GPL(skb_copy_ubufs); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1471 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1472 | /** |
| 1473 | * skb_clone - duplicate an sk_buff |
| 1474 | * @skb: buffer to clone |
| 1475 | * @gfp_mask: allocation priority |
| 1476 | * |
| 1477 | * Duplicate an &sk_buff. The new one is not owned by a socket. Both |
| 1478 | * copies share the same packet data but not structure. The new |
| 1479 | * buffer has a reference count of 1. If the allocation fails the |
| 1480 | * function returns %NULL otherwise the new buffer is returned. |
| 1481 | * |
| 1482 | * If this function is called from an interrupt gfp_mask() must be |
| 1483 | * %GFP_ATOMIC. |
| 1484 | */ |
| 1485 | |
| 1486 | struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) |
| 1487 | { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 1488 | struct sk_buff_fclones *fclones = container_of(skb, |
| 1489 | struct sk_buff_fclones, |
| 1490 | skb1); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 1491 | struct sk_buff *n; |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1492 | |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1493 | if (skb_orphan_frags(skb, gfp_mask)) |
| 1494 | return NULL; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1495 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1496 | if (skb->fclone == SKB_FCLONE_ORIG && |
Reshetova, Elena | 2638595 | 2017-06-30 13:07:59 +0300 | [diff] [blame] | 1497 | refcount_read(&fclones->fclone_ref) == 1) { |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 1498 | n = &fclones->skb2; |
Reshetova, Elena | 2638595 | 2017-06-30 13:07:59 +0300 | [diff] [blame] | 1499 | refcount_set(&fclones->fclone_ref, 2); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1500 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1501 | if (skb_pfmemalloc(skb)) |
| 1502 | gfp_mask |= __GFP_MEMALLOC; |
| 1503 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1504 | n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); |
| 1505 | if (!n) |
| 1506 | return NULL; |
Vegard Nossum | fe55f6d | 2008-08-30 12:16:35 +0200 | [diff] [blame] | 1507 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 1508 | n->fclone = SKB_FCLONE_UNAVAILABLE; |
| 1509 | } |
| 1510 | |
| 1511 | return __skb_clone(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1513 | EXPORT_SYMBOL(skb_clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
Toshiaki Makita | b0768a8 | 2018-08-03 16:58:09 +0900 | [diff] [blame] | 1515 | void skb_headers_offset_update(struct sk_buff *skb, int off) |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 1516 | { |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 1517 | /* Only adjust this if it actually is csum_start rather than csum */ |
| 1518 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 1519 | skb->csum_start += off; |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 1520 | /* {transport,network,mac}_header and tail are relative to skb->head */ |
| 1521 | skb->transport_header += off; |
| 1522 | skb->network_header += off; |
| 1523 | if (skb_mac_header_was_set(skb)) |
| 1524 | skb->mac_header += off; |
| 1525 | skb->inner_transport_header += off; |
| 1526 | skb->inner_network_header += off; |
Pravin B Shelar | aefbd2b | 2013-03-07 13:21:46 +0000 | [diff] [blame] | 1527 | skb->inner_mac_header += off; |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 1528 | } |
Toshiaki Makita | b0768a8 | 2018-08-03 16:58:09 +0900 | [diff] [blame] | 1529 | EXPORT_SYMBOL(skb_headers_offset_update); |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 1530 | |
Ilya Lesokhin | 08303c1 | 2018-04-30 10:16:11 +0300 | [diff] [blame] | 1531 | 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] | 1532 | { |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 1533 | __copy_skb_header(new, old); |
| 1534 | |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 1535 | skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; |
| 1536 | skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; |
| 1537 | skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | } |
Ilya Lesokhin | 08303c1 | 2018-04-30 10:16:11 +0300 | [diff] [blame] | 1539 | EXPORT_SYMBOL(skb_copy_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1541 | static inline int skb_alloc_rx_flag(const struct sk_buff *skb) |
| 1542 | { |
| 1543 | if (skb_pfmemalloc(skb)) |
| 1544 | return SKB_ALLOC_RX; |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | /** |
| 1549 | * skb_copy - create private copy of an sk_buff |
| 1550 | * @skb: buffer to copy |
| 1551 | * @gfp_mask: allocation priority |
| 1552 | * |
| 1553 | * Make a copy of both an &sk_buff and its data. This is used when the |
| 1554 | * caller wishes to modify the data and needs a private copy of the |
| 1555 | * data to alter. Returns %NULL on failure or the pointer to the buffer |
| 1556 | * on success. The returned buffer has a reference count of 1. |
| 1557 | * |
| 1558 | * As by-product this function converts non-linear &sk_buff to linear |
| 1559 | * one, so that &sk_buff becomes completely private and caller is allowed |
| 1560 | * to modify all the data of returned buffer. This means that this |
| 1561 | * function is not recommended for use in circumstances when only |
| 1562 | * header is going to be modified. Use pskb_copy() instead. |
| 1563 | */ |
| 1564 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1565 | 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] | 1566 | { |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1567 | int headerlen = skb_headroom(skb); |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 1568 | unsigned int size = skb_end_offset(skb) + skb->data_len; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1569 | struct sk_buff *n = __alloc_skb(size, gfp_mask, |
| 1570 | skb_alloc_rx_flag(skb), NUMA_NO_NODE); |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | if (!n) |
| 1573 | return NULL; |
| 1574 | |
| 1575 | /* Set the data pointer */ |
| 1576 | skb_reserve(n, headerlen); |
| 1577 | /* Set the tail pointer and length */ |
| 1578 | skb_put(n, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
Tim Hansen | 9f77fad | 2017-10-09 11:37:59 -0400 | [diff] [blame] | 1580 | BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | |
Ilya Lesokhin | 08303c1 | 2018-04-30 10:16:11 +0300 | [diff] [blame] | 1582 | skb_copy_header(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | return n; |
| 1584 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1585 | EXPORT_SYMBOL(skb_copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | |
| 1587 | /** |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1588 | * __pskb_copy_fclone - create copy of an sk_buff with private head. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | * @skb: buffer to copy |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1590 | * @headroom: headroom of new skb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | * @gfp_mask: allocation priority |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1592 | * @fclone: if true allocate the copy of the skb from the fclone |
| 1593 | * cache instead of the head cache; it is recommended to set this |
| 1594 | * to true for the cases where the copy will likely be cloned |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | * |
| 1596 | * Make a copy of both an &sk_buff and part of its data, located |
| 1597 | * in header. Fragmented data remain shared. This is used when |
| 1598 | * the caller wishes to modify only header of &sk_buff and needs |
| 1599 | * private copy of the header to alter. Returns %NULL on failure |
| 1600 | * or the pointer to the buffer on success. |
| 1601 | * The returned buffer has a reference count of 1. |
| 1602 | */ |
| 1603 | |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1604 | struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, |
| 1605 | gfp_t gfp_mask, bool fclone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | { |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1607 | unsigned int size = skb_headlen(skb) + headroom; |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1608 | int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); |
| 1609 | 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] | 1610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | if (!n) |
| 1612 | goto out; |
| 1613 | |
| 1614 | /* Set the data pointer */ |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1615 | skb_reserve(n, headroom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | /* Set the tail pointer and length */ |
| 1617 | skb_put(n, skb_headlen(skb)); |
| 1618 | /* Copy the bytes */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1619 | skb_copy_from_linear_data(skb, n->data, n->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | |
Herbert Xu | 25f484a | 2006-11-07 14:57:15 -0800 | [diff] [blame] | 1621 | n->truesize += skb->data_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | n->data_len = skb->data_len; |
| 1623 | n->len = skb->len; |
| 1624 | |
| 1625 | if (skb_shinfo(skb)->nr_frags) { |
| 1626 | int i; |
| 1627 | |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 1628 | if (skb_orphan_frags(skb, gfp_mask) || |
| 1629 | skb_zerocopy_clone(n, skb, gfp_mask)) { |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1630 | kfree_skb(n); |
| 1631 | n = NULL; |
| 1632 | goto out; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1633 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1635 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1636 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | } |
| 1638 | skb_shinfo(n)->nr_frags = i; |
| 1639 | } |
| 1640 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1641 | if (skb_has_frag_list(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; |
| 1643 | skb_clone_fraglist(n); |
| 1644 | } |
| 1645 | |
Ilya Lesokhin | 08303c1 | 2018-04-30 10:16:11 +0300 | [diff] [blame] | 1646 | skb_copy_header(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | out: |
| 1648 | return n; |
| 1649 | } |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1650 | EXPORT_SYMBOL(__pskb_copy_fclone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | |
| 1652 | /** |
| 1653 | * pskb_expand_head - reallocate header of &sk_buff |
| 1654 | * @skb: buffer to reallocate |
| 1655 | * @nhead: room to add at head |
| 1656 | * @ntail: room to add at tail |
| 1657 | * @gfp_mask: allocation priority |
| 1658 | * |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 1659 | * Expands (or creates identical copy, if @nhead and @ntail are zero) |
| 1660 | * 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] | 1661 | * reference count of 1. Returns zero in the case of success or error, |
| 1662 | * if expansion failed. In the last case, &sk_buff is not changed. |
| 1663 | * |
| 1664 | * All the pointers pointing into skb header may change and must be |
| 1665 | * reloaded after call to this function. |
| 1666 | */ |
| 1667 | |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 1668 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1669 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | { |
Eric Dumazet | 158f323 | 2017-01-27 07:11:27 -0800 | [diff] [blame] | 1671 | int i, osize = skb_end_offset(skb); |
| 1672 | int size = osize + nhead + ntail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | long off; |
Eric Dumazet | 158f323 | 2017-01-27 07:11:27 -0800 | [diff] [blame] | 1674 | u8 *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | |
Herbert Xu | 4edd87a | 2008-10-01 07:09:38 -0700 | [diff] [blame] | 1676 | BUG_ON(nhead < 0); |
| 1677 | |
Tim Hansen | 9f77fad | 2017-10-09 11:37:59 -0400 | [diff] [blame] | 1678 | BUG_ON(skb_shared(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
| 1680 | size = SKB_DATA_ALIGN(size); |
| 1681 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1682 | if (skb_pfmemalloc(skb)) |
| 1683 | gfp_mask |= __GFP_MEMALLOC; |
| 1684 | data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), |
| 1685 | gfp_mask, NUMA_NO_NODE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | if (!data) |
| 1687 | goto nodata; |
Eric Dumazet | 87151b8 | 2012-04-10 20:08:39 +0000 | [diff] [blame] | 1688 | size = SKB_WITH_OVERHEAD(ksize(data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | |
| 1690 | /* Copy only real data... and, alas, header. This should be |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1691 | * optimized for the cases when header is void. |
| 1692 | */ |
| 1693 | memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); |
| 1694 | |
| 1695 | memcpy((struct skb_shared_info *)(data + size), |
| 1696 | skb_shinfo(skb), |
Eric Dumazet | fed6638 | 2010-07-22 19:09:08 +0000 | [diff] [blame] | 1697 | offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | |
Alexander Duyck | 3e24591 | 2012-05-04 14:26:51 +0000 | [diff] [blame] | 1699 | /* |
| 1700 | * if shinfo is shared we must drop the old head gracefully, but if it |
| 1701 | * is not we can just drop the old head and let the existing refcount |
| 1702 | * be since all we did is relocate the values |
| 1703 | */ |
| 1704 | if (skb_cloned(skb)) { |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1705 | if (skb_orphan_frags(skb, gfp_mask)) |
| 1706 | goto nofrags; |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 1707 | if (skb_zcopy(skb)) |
Eric Dumazet | c1d1b43 | 2017-08-31 16:48:22 -0700 | [diff] [blame] | 1708 | refcount_inc(&skb_uarg(skb)->refcnt); |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1709 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1710 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1712 | if (skb_has_frag_list(skb)) |
| 1713 | skb_clone_fraglist(skb); |
| 1714 | |
| 1715 | skb_release_data(skb); |
Alexander Duyck | 3e24591 | 2012-05-04 14:26:51 +0000 | [diff] [blame] | 1716 | } else { |
| 1717 | skb_free_head(skb); |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | off = (data + nhead) - skb->head; |
| 1720 | |
| 1721 | skb->head = data; |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 1722 | skb->head_frag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | skb->data += off; |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1724 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 1725 | skb->end = size; |
Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 1726 | off = nhead; |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1727 | #else |
| 1728 | skb->end = skb->head + size; |
Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 1729 | #endif |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1730 | skb->tail += off; |
Peter Pan(潘卫平) | b41abb4 | 2013-06-06 21:27:21 +0800 | [diff] [blame] | 1731 | skb_headers_offset_update(skb, nhead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | skb->cloned = 0; |
Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 1733 | skb->hdr_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | skb->nohdr = 0; |
| 1735 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
Eric Dumazet | 158f323 | 2017-01-27 07:11:27 -0800 | [diff] [blame] | 1736 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1737 | skb_metadata_clear(skb); |
| 1738 | |
Eric Dumazet | 158f323 | 2017-01-27 07:11:27 -0800 | [diff] [blame] | 1739 | /* It is not generally safe to change skb->truesize. |
| 1740 | * For the moment, we really care of rx path, or |
| 1741 | * when skb is orphaned (not attached to a socket). |
| 1742 | */ |
| 1743 | if (!skb->sk || skb->destructor == sock_edemux) |
| 1744 | skb->truesize += size - osize; |
| 1745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | return 0; |
| 1747 | |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1748 | nofrags: |
| 1749 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | nodata: |
| 1751 | return -ENOMEM; |
| 1752 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1753 | EXPORT_SYMBOL(pskb_expand_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
| 1755 | /* Make private copy of skb with writable head and some headroom */ |
| 1756 | |
| 1757 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) |
| 1758 | { |
| 1759 | struct sk_buff *skb2; |
| 1760 | int delta = headroom - skb_headroom(skb); |
| 1761 | |
| 1762 | if (delta <= 0) |
| 1763 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
| 1764 | else { |
| 1765 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1766 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, |
| 1767 | GFP_ATOMIC)) { |
| 1768 | kfree_skb(skb2); |
| 1769 | skb2 = NULL; |
| 1770 | } |
| 1771 | } |
| 1772 | return skb2; |
| 1773 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1774 | EXPORT_SYMBOL(skb_realloc_headroom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
| 1776 | /** |
| 1777 | * skb_copy_expand - copy and expand sk_buff |
| 1778 | * @skb: buffer to copy |
| 1779 | * @newheadroom: new free bytes at head |
| 1780 | * @newtailroom: new free bytes at tail |
| 1781 | * @gfp_mask: allocation priority |
| 1782 | * |
| 1783 | * Make a copy of both an &sk_buff and its data and while doing so |
| 1784 | * allocate additional space. |
| 1785 | * |
| 1786 | * This is used when the caller wishes to modify the data and needs a |
| 1787 | * private copy of the data to alter as well as more space for new fields. |
| 1788 | * Returns %NULL on failure or the pointer to the buffer |
| 1789 | * on success. The returned buffer has a reference count of 1. |
| 1790 | * |
| 1791 | * You must pass %GFP_ATOMIC as the allocation priority if this function |
| 1792 | * is called from an interrupt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | */ |
| 1794 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 1795 | int newheadroom, int newtailroom, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1796 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | { |
| 1798 | /* |
| 1799 | * Allocate the copy buffer |
| 1800 | */ |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1801 | struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, |
| 1802 | gfp_mask, skb_alloc_rx_flag(skb), |
| 1803 | NUMA_NO_NODE); |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1804 | int oldheadroom = skb_headroom(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | int head_copy_len, head_copy_off; |
| 1806 | |
| 1807 | if (!n) |
| 1808 | return NULL; |
| 1809 | |
| 1810 | skb_reserve(n, newheadroom); |
| 1811 | |
| 1812 | /* Set the tail pointer and length */ |
| 1813 | skb_put(n, skb->len); |
| 1814 | |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1815 | head_copy_len = oldheadroom; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | head_copy_off = 0; |
| 1817 | if (newheadroom <= head_copy_len) |
| 1818 | head_copy_len = newheadroom; |
| 1819 | else |
| 1820 | head_copy_off = newheadroom - head_copy_len; |
| 1821 | |
| 1822 | /* Copy the linear header and data. */ |
Tim Hansen | 9f77fad | 2017-10-09 11:37:59 -0400 | [diff] [blame] | 1823 | BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, |
| 1824 | skb->len + head_copy_len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | |
Ilya Lesokhin | 08303c1 | 2018-04-30 10:16:11 +0300 | [diff] [blame] | 1826 | skb_copy_header(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 1828 | skb_headers_offset_update(n, newheadroom - oldheadroom); |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | return n; |
| 1831 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1832 | EXPORT_SYMBOL(skb_copy_expand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | |
| 1834 | /** |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1835 | * __skb_pad - zero pad the tail of an skb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | * @skb: buffer to pad |
| 1837 | * @pad: space to pad |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1838 | * @free_on_error: free buffer on error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | * |
| 1840 | * Ensure that a buffer is followed by a padding area that is zero |
| 1841 | * filled. Used by network drivers which may DMA or transfer data |
| 1842 | * beyond the buffer end onto the wire. |
| 1843 | * |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1844 | * May return error in out of memory cases. The skb is freed on error |
| 1845 | * if @free_on_error is true. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1847 | |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1848 | 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] | 1849 | { |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1850 | int err; |
| 1851 | int ntail; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | /* If the skbuff is non linear tailroom is always zero.. */ |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1854 | if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | memset(skb->data+skb->len, 0, pad); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1856 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | } |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1858 | |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1859 | ntail = skb->data_len + pad - (skb->end - skb->tail); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1860 | if (likely(skb_cloned(skb) || ntail > 0)) { |
| 1861 | err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); |
| 1862 | if (unlikely(err)) |
| 1863 | goto free_skb; |
| 1864 | } |
| 1865 | |
| 1866 | /* FIXME: The use of this function with non-linear skb's really needs |
| 1867 | * to be audited. |
| 1868 | */ |
| 1869 | err = skb_linearize(skb); |
| 1870 | if (unlikely(err)) |
| 1871 | goto free_skb; |
| 1872 | |
| 1873 | memset(skb->data + skb->len, 0, pad); |
| 1874 | return 0; |
| 1875 | |
| 1876 | free_skb: |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1877 | if (free_on_error) |
| 1878 | kfree_skb(skb); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1879 | return err; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1880 | } |
Florian Fainelli | cd0a137 | 2017-08-22 15:12:14 -0700 | [diff] [blame] | 1881 | EXPORT_SYMBOL(__skb_pad); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1882 | |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1883 | /** |
Mathias Krause | 0c7ddf3 | 2013-11-07 14:18:24 +0100 | [diff] [blame] | 1884 | * pskb_put - add data to the tail of a potentially fragmented buffer |
| 1885 | * @skb: start of the buffer to use |
| 1886 | * @tail: tail fragment of the buffer to use |
| 1887 | * @len: amount of data to add |
| 1888 | * |
| 1889 | * This function extends the used data area of the potentially |
| 1890 | * fragmented buffer. @tail must be the last fragment of @skb -- or |
| 1891 | * @skb itself. If this would exceed the total buffer size the kernel |
| 1892 | * will panic. A pointer to the first byte of the extra data is |
| 1893 | * returned. |
| 1894 | */ |
| 1895 | |
Johannes Berg | 4df864c | 2017-06-16 14:29:21 +0200 | [diff] [blame] | 1896 | 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] | 1897 | { |
| 1898 | if (tail != skb) { |
| 1899 | skb->data_len += len; |
| 1900 | skb->len += len; |
| 1901 | } |
| 1902 | return skb_put(tail, len); |
| 1903 | } |
| 1904 | EXPORT_SYMBOL_GPL(pskb_put); |
| 1905 | |
| 1906 | /** |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1907 | * skb_put - add data to a buffer |
| 1908 | * @skb: buffer to use |
| 1909 | * @len: amount of data to add |
| 1910 | * |
| 1911 | * This function extends the used data area of the buffer. If this would |
| 1912 | * exceed the total buffer size the kernel will panic. A pointer to the |
| 1913 | * first byte of the extra data is returned. |
| 1914 | */ |
Johannes Berg | 4df864c | 2017-06-16 14:29:21 +0200 | [diff] [blame] | 1915 | void *skb_put(struct sk_buff *skb, unsigned int len) |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1916 | { |
Johannes Berg | 4df864c | 2017-06-16 14:29:21 +0200 | [diff] [blame] | 1917 | void *tmp = skb_tail_pointer(skb); |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1918 | SKB_LINEAR_ASSERT(skb); |
| 1919 | skb->tail += len; |
| 1920 | skb->len += len; |
| 1921 | if (unlikely(skb->tail > skb->end)) |
| 1922 | skb_over_panic(skb, len, __builtin_return_address(0)); |
| 1923 | return tmp; |
| 1924 | } |
| 1925 | EXPORT_SYMBOL(skb_put); |
| 1926 | |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1927 | /** |
Ilpo Järvinen | c2aa270 | 2008-03-27 17:52:40 -0700 | [diff] [blame] | 1928 | * skb_push - add data to the start of a buffer |
| 1929 | * @skb: buffer to use |
| 1930 | * @len: amount of data to add |
| 1931 | * |
| 1932 | * This function extends the used data area of the buffer at the buffer |
| 1933 | * start. If this would exceed the total buffer headroom the kernel will |
| 1934 | * panic. A pointer to the first byte of the extra data is returned. |
| 1935 | */ |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 1936 | void *skb_push(struct sk_buff *skb, unsigned int len) |
Ilpo Järvinen | c2aa270 | 2008-03-27 17:52:40 -0700 | [diff] [blame] | 1937 | { |
| 1938 | skb->data -= len; |
| 1939 | skb->len += len; |
Ganesh Goudar | 9aba2f8 | 2018-08-02 15:34:52 +0530 | [diff] [blame] | 1940 | if (unlikely(skb->data < skb->head)) |
Ilpo Järvinen | c2aa270 | 2008-03-27 17:52:40 -0700 | [diff] [blame] | 1941 | skb_under_panic(skb, len, __builtin_return_address(0)); |
| 1942 | return skb->data; |
| 1943 | } |
| 1944 | EXPORT_SYMBOL(skb_push); |
| 1945 | |
| 1946 | /** |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1947 | * skb_pull - remove data from the start of a buffer |
| 1948 | * @skb: buffer to use |
| 1949 | * @len: amount of data to remove |
| 1950 | * |
| 1951 | * This function removes data from the start of a buffer, returning |
| 1952 | * the memory to the headroom. A pointer to the next data in the buffer |
| 1953 | * is returned. Once the data has been pulled future pushes will overwrite |
| 1954 | * the old data. |
| 1955 | */ |
Johannes Berg | af72868 | 2017-06-16 14:29:22 +0200 | [diff] [blame] | 1956 | void *skb_pull(struct sk_buff *skb, unsigned int len) |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1957 | { |
David S. Miller | 47d2964 | 2010-05-02 02:21:44 -0700 | [diff] [blame] | 1958 | return skb_pull_inline(skb, len); |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1959 | } |
| 1960 | EXPORT_SYMBOL(skb_pull); |
| 1961 | |
Ilpo Järvinen | 419ae74 | 2008-03-27 17:54:01 -0700 | [diff] [blame] | 1962 | /** |
| 1963 | * skb_trim - remove end from a buffer |
| 1964 | * @skb: buffer to alter |
| 1965 | * @len: new length |
| 1966 | * |
| 1967 | * Cut the length of a buffer down by removing data from the tail. If |
| 1968 | * the buffer is already under the length specified it is not modified. |
| 1969 | * The skb must be linear. |
| 1970 | */ |
| 1971 | void skb_trim(struct sk_buff *skb, unsigned int len) |
| 1972 | { |
| 1973 | if (skb->len > len) |
| 1974 | __skb_trim(skb, len); |
| 1975 | } |
| 1976 | EXPORT_SYMBOL(skb_trim); |
| 1977 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 1978 | /* Trims skb to length len. It can change skb pointers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | */ |
| 1980 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 1981 | int ___pskb_trim(struct sk_buff *skb, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1983 | struct sk_buff **fragp; |
| 1984 | struct sk_buff *frag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | int offset = skb_headlen(skb); |
| 1986 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 1987 | int i; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1988 | int err; |
| 1989 | |
| 1990 | if (skb_cloned(skb) && |
| 1991 | unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) |
| 1992 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1994 | i = 0; |
| 1995 | if (offset >= len) |
| 1996 | goto drop_pages; |
| 1997 | |
| 1998 | for (; i < nfrags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1999 | int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 2000 | |
| 2001 | if (end < len) { |
| 2002 | offset = end; |
| 2003 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | } |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 2005 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2006 | skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 2007 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 2008 | drop_pages: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 2009 | skb_shinfo(skb)->nr_frags = i; |
| 2010 | |
| 2011 | for (; i < nfrags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2012 | skb_frag_unref(skb, i); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 2013 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 2014 | if (skb_has_frag_list(skb)) |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 2015 | skb_drop_fraglist(skb); |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 2016 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | } |
| 2018 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 2019 | for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); |
| 2020 | fragp = &frag->next) { |
| 2021 | int end = offset + frag->len; |
| 2022 | |
| 2023 | if (skb_shared(frag)) { |
| 2024 | struct sk_buff *nfrag; |
| 2025 | |
| 2026 | nfrag = skb_clone(frag, GFP_ATOMIC); |
| 2027 | if (unlikely(!nfrag)) |
| 2028 | return -ENOMEM; |
| 2029 | |
| 2030 | nfrag->next = frag->next; |
Eric Dumazet | 85bb2a6 | 2012-04-19 02:24:53 +0000 | [diff] [blame] | 2031 | consume_skb(frag); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 2032 | frag = nfrag; |
| 2033 | *fragp = frag; |
| 2034 | } |
| 2035 | |
| 2036 | if (end < len) { |
| 2037 | offset = end; |
| 2038 | continue; |
| 2039 | } |
| 2040 | |
| 2041 | if (end > len && |
| 2042 | unlikely((err = pskb_trim(frag, len - offset)))) |
| 2043 | return err; |
| 2044 | |
| 2045 | if (frag->next) |
| 2046 | skb_drop_list(&frag->next); |
| 2047 | break; |
| 2048 | } |
| 2049 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 2050 | done: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 2051 | if (len > skb_headlen(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | skb->data_len -= skb->len - len; |
| 2053 | skb->len = len; |
| 2054 | } else { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 2055 | skb->len = len; |
| 2056 | skb->data_len = 0; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 2057 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
Eric Dumazet | c21b48c | 2017-04-26 09:07:46 -0700 | [diff] [blame] | 2060 | if (!skb->sk || skb->destructor == sock_edemux) |
| 2061 | skb_condense(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | return 0; |
| 2063 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2064 | EXPORT_SYMBOL(___pskb_trim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
Eric Dumazet | 88078d9 | 2018-04-18 11:43:15 -0700 | [diff] [blame] | 2066 | /* Note : use pskb_trim_rcsum() instead of calling this directly |
| 2067 | */ |
| 2068 | int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) |
| 2069 | { |
| 2070 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 2071 | int delta = skb->len - len; |
| 2072 | |
Dimitris Michailidis | d55bef50 | 2018-10-19 17:07:13 -0700 | [diff] [blame] | 2073 | skb->csum = csum_block_sub(skb->csum, |
| 2074 | skb_checksum(skb, len, delta, 0), |
| 2075 | len); |
Vasily Averin | 54970a2 | 2020-12-14 22:07:39 +0300 | [diff] [blame] | 2076 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 2077 | int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; |
| 2078 | int offset = skb_checksum_start_offset(skb) + skb->csum_offset; |
| 2079 | |
| 2080 | if (offset + sizeof(__sum16) > hdlen) |
| 2081 | return -EINVAL; |
Eric Dumazet | 88078d9 | 2018-04-18 11:43:15 -0700 | [diff] [blame] | 2082 | } |
| 2083 | return __pskb_trim(skb, len); |
| 2084 | } |
| 2085 | EXPORT_SYMBOL(pskb_trim_rcsum_slow); |
| 2086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | /** |
| 2088 | * __pskb_pull_tail - advance tail of skb header |
| 2089 | * @skb: buffer to reallocate |
| 2090 | * @delta: number of bytes to advance tail |
| 2091 | * |
| 2092 | * The function makes a sense only on a fragmented &sk_buff, |
| 2093 | * it expands header moving its tail forward and copying necessary |
| 2094 | * data from fragmented part. |
| 2095 | * |
| 2096 | * &sk_buff MUST have reference count of 1. |
| 2097 | * |
| 2098 | * Returns %NULL (and &sk_buff does not change) if pull failed |
| 2099 | * or value of new tail of skb in the case of success. |
| 2100 | * |
| 2101 | * All the pointers pointing into skb header may change and must be |
| 2102 | * reloaded after call to this function. |
| 2103 | */ |
| 2104 | |
| 2105 | /* Moves tail of skb head forward, copying data from fragmented part, |
| 2106 | * when it is necessary. |
| 2107 | * 1. It may fail due to malloc failure. |
| 2108 | * 2. It may change skb pointers. |
| 2109 | * |
| 2110 | * It is pretty complicated. Luckily, it is called only in exceptional cases. |
| 2111 | */ |
Johannes Berg | af72868 | 2017-06-16 14:29:22 +0200 | [diff] [blame] | 2112 | void *__pskb_pull_tail(struct sk_buff *skb, int delta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | { |
| 2114 | /* If skb has not enough free space at tail, get new one |
| 2115 | * plus 128 bytes for future expansions. If we have enough |
| 2116 | * room at tail, reallocate without expansion only if skb is cloned. |
| 2117 | */ |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 2118 | int i, k, eat = (skb->tail + delta) - skb->end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
| 2120 | if (eat > 0 || skb_cloned(skb)) { |
| 2121 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, |
| 2122 | GFP_ATOMIC)) |
| 2123 | return NULL; |
| 2124 | } |
| 2125 | |
Tim Hansen | 9f77fad | 2017-10-09 11:37:59 -0400 | [diff] [blame] | 2126 | BUG_ON(skb_copy_bits(skb, skb_headlen(skb), |
| 2127 | skb_tail_pointer(skb), delta)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | |
| 2129 | /* Optimization: no fragments, no reasons to preestimate |
| 2130 | * size of pulled pages. Superb. |
| 2131 | */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 2132 | if (!skb_has_frag_list(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | goto pull_pages; |
| 2134 | |
| 2135 | /* Estimate size of pulled pages. */ |
| 2136 | eat = delta; |
| 2137 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2138 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 2139 | |
| 2140 | if (size >= eat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | goto pull_pages; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2142 | eat -= size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | /* If we need update frag list, we are in troubles. |
Wenhua Shi | 09001b0 | 2017-10-14 18:51:36 +0200 | [diff] [blame] | 2146 | * Certainly, it is possible to add an offset to skb data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | * but taking into account that pulling is expected to |
| 2148 | * be very rare operation, it is worth to fight against |
| 2149 | * further bloating skb head and crucify ourselves here instead. |
| 2150 | * Pure masohism, indeed. 8)8) |
| 2151 | */ |
| 2152 | if (eat) { |
| 2153 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 2154 | struct sk_buff *clone = NULL; |
| 2155 | struct sk_buff *insp = NULL; |
| 2156 | |
| 2157 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | if (list->len <= eat) { |
| 2159 | /* Eaten as whole. */ |
| 2160 | eat -= list->len; |
| 2161 | list = list->next; |
| 2162 | insp = list; |
| 2163 | } else { |
| 2164 | /* Eaten partially. */ |
| 2165 | |
| 2166 | if (skb_shared(list)) { |
| 2167 | /* Sucks! We need to fork list. :-( */ |
| 2168 | clone = skb_clone(list, GFP_ATOMIC); |
| 2169 | if (!clone) |
| 2170 | return NULL; |
| 2171 | insp = list->next; |
| 2172 | list = clone; |
| 2173 | } else { |
| 2174 | /* This may be pulled without |
| 2175 | * problems. */ |
| 2176 | insp = list; |
| 2177 | } |
| 2178 | if (!pskb_pull(list, eat)) { |
Wei Yongjun | f3fbbe0 | 2009-02-25 00:37:32 +0000 | [diff] [blame] | 2179 | kfree_skb(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | return NULL; |
| 2181 | } |
| 2182 | break; |
| 2183 | } |
| 2184 | } while (eat); |
| 2185 | |
| 2186 | /* Free pulled out fragments. */ |
| 2187 | while ((list = skb_shinfo(skb)->frag_list) != insp) { |
| 2188 | skb_shinfo(skb)->frag_list = list->next; |
| 2189 | kfree_skb(list); |
| 2190 | } |
| 2191 | /* And insert new clone at head. */ |
| 2192 | if (clone) { |
| 2193 | clone->next = list; |
| 2194 | skb_shinfo(skb)->frag_list = clone; |
| 2195 | } |
| 2196 | } |
| 2197 | /* Success! Now we may commit changes to skb data. */ |
| 2198 | |
| 2199 | pull_pages: |
| 2200 | eat = delta; |
| 2201 | k = 0; |
| 2202 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2203 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 2204 | |
| 2205 | if (size <= eat) { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2206 | skb_frag_unref(skb, i); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2207 | eat -= size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | } else { |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 2209 | skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; |
| 2210 | |
| 2211 | *frag = skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | if (eat) { |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 2213 | skb_frag_off_add(frag, eat); |
| 2214 | skb_frag_size_sub(frag, eat); |
linzhang | 3ccc6c6 | 2017-07-17 17:25:02 +0800 | [diff] [blame] | 2215 | if (!i) |
| 2216 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | eat = 0; |
| 2218 | } |
| 2219 | k++; |
| 2220 | } |
| 2221 | } |
| 2222 | skb_shinfo(skb)->nr_frags = k; |
| 2223 | |
linzhang | 3ccc6c6 | 2017-07-17 17:25:02 +0800 | [diff] [blame] | 2224 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | skb->tail += delta; |
| 2226 | skb->data_len -= delta; |
| 2227 | |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 2228 | if (!skb->data_len) |
| 2229 | skb_zcopy_clear(skb, false); |
| 2230 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 2231 | return skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2233 | EXPORT_SYMBOL(__pskb_pull_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | |
Eric Dumazet | 22019b1 | 2011-07-29 18:37:31 +0000 | [diff] [blame] | 2235 | /** |
| 2236 | * skb_copy_bits - copy bits from skb to kernel buffer |
| 2237 | * @skb: source skb |
| 2238 | * @offset: offset in source |
| 2239 | * @to: destination buffer |
| 2240 | * @len: number of bytes to copy |
| 2241 | * |
| 2242 | * Copy the specified number of bytes from the source skb to the |
| 2243 | * destination buffer. |
| 2244 | * |
| 2245 | * CAUTION ! : |
| 2246 | * If its prototype is ever changed, |
| 2247 | * check arch/{*}/net/{*}.S files, |
| 2248 | * since it is called from BPF assembly code. |
| 2249 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) |
| 2251 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2252 | int start = skb_headlen(skb); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2253 | struct sk_buff *frag_iter; |
| 2254 | int i, copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | |
| 2256 | if (offset > (int)skb->len - len) |
| 2257 | goto fault; |
| 2258 | |
| 2259 | /* Copy header. */ |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2260 | if ((copy = start - offset) > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | if (copy > len) |
| 2262 | copy = len; |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2263 | skb_copy_from_linear_data_offset(skb, offset, to, copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | if ((len -= copy) == 0) |
| 2265 | return 0; |
| 2266 | offset += copy; |
| 2267 | to += copy; |
| 2268 | } |
| 2269 | |
| 2270 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2271 | int end; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2272 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2274 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2275 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2276 | end = start + skb_frag_size(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | if ((copy = end - offset) > 0) { |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2278 | u32 p_off, p_len, copied; |
| 2279 | struct page *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | u8 *vaddr; |
| 2281 | |
| 2282 | if (copy > len) |
| 2283 | copy = len; |
| 2284 | |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2285 | skb_frag_foreach_page(f, |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 2286 | skb_frag_off(f) + offset - start, |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2287 | copy, p, p_off, p_len, copied) { |
| 2288 | vaddr = kmap_atomic(p); |
| 2289 | memcpy(to + copied, vaddr + p_off, p_len); |
| 2290 | kunmap_atomic(vaddr); |
| 2291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | |
| 2293 | if ((len -= copy) == 0) |
| 2294 | return 0; |
| 2295 | offset += copy; |
| 2296 | to += copy; |
| 2297 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2298 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | } |
| 2300 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2301 | skb_walk_frags(skb, frag_iter) { |
| 2302 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2304 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2306 | end = start + frag_iter->len; |
| 2307 | if ((copy = end - offset) > 0) { |
| 2308 | if (copy > len) |
| 2309 | copy = len; |
| 2310 | if (skb_copy_bits(frag_iter, offset - start, to, copy)) |
| 2311 | goto fault; |
| 2312 | if ((len -= copy) == 0) |
| 2313 | return 0; |
| 2314 | offset += copy; |
| 2315 | to += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2317 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | } |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 2319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | if (!len) |
| 2321 | return 0; |
| 2322 | |
| 2323 | fault: |
| 2324 | return -EFAULT; |
| 2325 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2326 | EXPORT_SYMBOL(skb_copy_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2328 | /* |
| 2329 | * Callback from splice_to_pipe(), if we need to release some pages |
| 2330 | * at the end of the spd in case we error'ed out in filling the pipe. |
| 2331 | */ |
| 2332 | static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) |
| 2333 | { |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2334 | put_page(spd->pages[i]); |
| 2335 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2336 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2337 | static struct page *linear_to_page(struct page *page, unsigned int *len, |
| 2338 | unsigned int *offset, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2339 | struct sock *sk) |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2340 | { |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 2341 | struct page_frag *pfrag = sk_page_frag(sk); |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2342 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 2343 | if (!sk_page_frag_refill(sk, pfrag)) |
| 2344 | return NULL; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 2345 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 2346 | *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 2347 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 2348 | memcpy(page_address(pfrag->page) + pfrag->offset, |
| 2349 | page_address(page) + *offset, *len); |
| 2350 | *offset = pfrag->offset; |
| 2351 | pfrag->offset += *len; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 2352 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 2353 | return pfrag->page; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2354 | } |
| 2355 | |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 2356 | static bool spd_can_coalesce(const struct splice_pipe_desc *spd, |
| 2357 | struct page *page, |
| 2358 | unsigned int offset) |
| 2359 | { |
| 2360 | return spd->nr_pages && |
| 2361 | spd->pages[spd->nr_pages - 1] == page && |
| 2362 | (spd->partial[spd->nr_pages - 1].offset + |
| 2363 | spd->partial[spd->nr_pages - 1].len == offset); |
| 2364 | } |
| 2365 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2366 | /* |
| 2367 | * Fill page/offset/length into spd, if it can hold more pages. |
| 2368 | */ |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2369 | static bool spd_fill_page(struct splice_pipe_desc *spd, |
| 2370 | struct pipe_inode_info *pipe, struct page *page, |
| 2371 | unsigned int *len, unsigned int offset, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2372 | bool linear, |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2373 | struct sock *sk) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2374 | { |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 2375 | if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2376 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2377 | |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2378 | if (linear) { |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2379 | page = linear_to_page(page, len, &offset, sk); |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2380 | if (!page) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2381 | return true; |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 2382 | } |
| 2383 | if (spd_can_coalesce(spd, page, offset)) { |
| 2384 | spd->partial[spd->nr_pages - 1].len += *len; |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2385 | return false; |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 2386 | } |
| 2387 | get_page(page); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2388 | spd->pages[spd->nr_pages] = page; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 2389 | spd->partial[spd->nr_pages].len = *len; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2390 | spd->partial[spd->nr_pages].offset = offset; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2391 | spd->nr_pages++; |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 2392 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2393 | return false; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2394 | } |
| 2395 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2396 | static bool __splice_segment(struct page *page, unsigned int poff, |
| 2397 | unsigned int plen, unsigned int *off, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2398 | unsigned int *len, |
Eric Dumazet | d7ccf7c | 2012-04-23 23:35:04 -0400 | [diff] [blame] | 2399 | struct splice_pipe_desc *spd, bool linear, |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2400 | struct sock *sk, |
| 2401 | struct pipe_inode_info *pipe) |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2402 | { |
| 2403 | if (!*len) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2404 | return true; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2405 | |
| 2406 | /* skip this segment if already processed */ |
| 2407 | if (*off >= plen) { |
| 2408 | *off -= plen; |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2409 | return false; |
Octavian Purdila | db43a28 | 2008-06-27 17:27:21 -0700 | [diff] [blame] | 2410 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2411 | |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2412 | /* ignore any bits we already processed */ |
Eric Dumazet | 9ca1b22 | 2013-01-05 21:31:18 +0000 | [diff] [blame] | 2413 | poff += *off; |
| 2414 | plen -= *off; |
| 2415 | *off = 0; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2416 | |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2417 | do { |
| 2418 | unsigned int flen = min(*len, plen); |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2419 | |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2420 | if (spd_fill_page(spd, pipe, page, &flen, poff, |
| 2421 | linear, sk)) |
| 2422 | return true; |
| 2423 | poff += flen; |
| 2424 | plen -= flen; |
| 2425 | *len -= flen; |
| 2426 | } while (*len && plen); |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2427 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2428 | return false; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | /* |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2432 | * 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] | 2433 | * pipe is full or if we already spliced the requested length. |
| 2434 | */ |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2435 | static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, |
| 2436 | unsigned int *offset, unsigned int *len, |
| 2437 | struct splice_pipe_desc *spd, struct sock *sk) |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2438 | { |
| 2439 | int seg; |
Tom Herbert | fa9835e | 2016-03-07 14:11:04 -0800 | [diff] [blame] | 2440 | struct sk_buff *iter; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2441 | |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 2442 | /* map the linear part : |
Alexander Duyck | 2996d31 | 2012-05-02 18:18:42 +0000 | [diff] [blame] | 2443 | * If skb->head_frag is set, this 'linear' part is backed by a |
| 2444 | * fragment, and if the head is not shared with any clones then |
| 2445 | * 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] | 2446 | */ |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 2447 | if (__splice_segment(virt_to_page(skb->data), |
| 2448 | (unsigned long) skb->data & (PAGE_SIZE - 1), |
| 2449 | skb_headlen(skb), |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2450 | offset, len, spd, |
Alexander Duyck | 3a7c1ee4 | 2012-05-03 01:09:42 +0000 | [diff] [blame] | 2451 | skb_head_is_locked(skb), |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 2452 | sk, pipe)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2453 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2454 | |
| 2455 | /* |
| 2456 | * then map the fragments |
| 2457 | */ |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2458 | for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { |
| 2459 | const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; |
| 2460 | |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2461 | if (__splice_segment(skb_frag_page(f), |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 2462 | skb_frag_off(f), skb_frag_size(f), |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 2463 | offset, len, spd, false, sk, pipe)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2464 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2465 | } |
| 2466 | |
Tom Herbert | fa9835e | 2016-03-07 14:11:04 -0800 | [diff] [blame] | 2467 | skb_walk_frags(skb, iter) { |
| 2468 | if (*offset >= iter->len) { |
| 2469 | *offset -= iter->len; |
| 2470 | continue; |
| 2471 | } |
| 2472 | /* __skb_splice_bits() only fails if the output has no room |
| 2473 | * left, so no point in going over the frag_list for the error |
| 2474 | * case. |
| 2475 | */ |
| 2476 | if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) |
| 2477 | return true; |
| 2478 | } |
| 2479 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 2480 | return false; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | /* |
| 2484 | * 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] | 2485 | * the fragments, and the frag list. |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2486 | */ |
Hannes Frederic Sowa | a60e3cc | 2015-05-21 17:00:00 +0200 | [diff] [blame] | 2487 | 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] | 2488 | struct pipe_inode_info *pipe, unsigned int tlen, |
Al Viro | 2586926 | 2016-09-17 21:02:10 -0400 | [diff] [blame] | 2489 | unsigned int flags) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2490 | { |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 2491 | struct partial_page partial[MAX_SKB_FRAGS]; |
| 2492 | struct page *pages[MAX_SKB_FRAGS]; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2493 | struct splice_pipe_desc spd = { |
| 2494 | .pages = pages, |
| 2495 | .partial = partial, |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 2496 | .nr_pages_max = MAX_SKB_FRAGS, |
Miklos Szeredi | 28a625c | 2014-01-22 19:36:57 +0100 | [diff] [blame] | 2497 | .ops = &nosteal_pipe_buf_ops, |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2498 | .spd_release = sock_spd_release, |
| 2499 | }; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 2500 | int ret = 0; |
| 2501 | |
Tom Herbert | fa9835e | 2016-03-07 14:11:04 -0800 | [diff] [blame] | 2502 | __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2503 | |
Hannes Frederic Sowa | a60e3cc | 2015-05-21 17:00:00 +0200 | [diff] [blame] | 2504 | if (spd.nr_pages) |
Al Viro | 2586926 | 2016-09-17 21:02:10 -0400 | [diff] [blame] | 2505 | ret = splice_to_pipe(pipe, &spd); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2506 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 2507 | return ret; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2508 | } |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2509 | EXPORT_SYMBOL_GPL(skb_splice_bits); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 2510 | |
Cong Wang | 0739cd2 | 2021-03-30 19:32:24 -0700 | [diff] [blame] | 2511 | static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg, |
| 2512 | struct kvec *vec, size_t num, size_t size) |
| 2513 | { |
| 2514 | struct socket *sock = sk->sk_socket; |
| 2515 | |
| 2516 | if (!sock) |
| 2517 | return -EINVAL; |
| 2518 | return kernel_sendmsg(sock, msg, vec, num, size); |
| 2519 | } |
| 2520 | |
| 2521 | static int sendpage_unlocked(struct sock *sk, struct page *page, int offset, |
| 2522 | size_t size, int flags) |
| 2523 | { |
| 2524 | struct socket *sock = sk->sk_socket; |
| 2525 | |
| 2526 | if (!sock) |
| 2527 | return -EINVAL; |
| 2528 | return kernel_sendpage(sock, page, offset, size, flags); |
| 2529 | } |
| 2530 | |
| 2531 | typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg, |
| 2532 | struct kvec *vec, size_t num, size_t size); |
| 2533 | typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset, |
| 2534 | size_t size, int flags); |
| 2535 | static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, |
| 2536 | int len, sendmsg_func sendmsg, sendpage_func sendpage) |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2537 | { |
| 2538 | unsigned int orig_len = len; |
| 2539 | struct sk_buff *head = skb; |
| 2540 | unsigned short fragidx; |
| 2541 | int slen, ret; |
| 2542 | |
| 2543 | do_frag_list: |
| 2544 | |
| 2545 | /* Deal with head data */ |
| 2546 | while (offset < skb_headlen(skb) && len) { |
| 2547 | struct kvec kv; |
| 2548 | struct msghdr msg; |
| 2549 | |
| 2550 | slen = min_t(int, len, skb_headlen(skb) - offset); |
| 2551 | kv.iov_base = skb->data + offset; |
John Fastabend | db5980d | 2017-08-15 22:31:34 -0700 | [diff] [blame] | 2552 | kv.iov_len = slen; |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2553 | memset(&msg, 0, sizeof(msg)); |
John Fastabend | bd95e678 | 2019-05-24 08:01:00 -0700 | [diff] [blame] | 2554 | msg.msg_flags = MSG_DONTWAIT; |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2555 | |
Cong Wang | 0739cd2 | 2021-03-30 19:32:24 -0700 | [diff] [blame] | 2556 | ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked, |
| 2557 | sendmsg_unlocked, sk, &msg, &kv, 1, slen); |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2558 | if (ret <= 0) |
| 2559 | goto error; |
| 2560 | |
| 2561 | offset += ret; |
| 2562 | len -= ret; |
| 2563 | } |
| 2564 | |
| 2565 | /* All the data was skb head? */ |
| 2566 | if (!len) |
| 2567 | goto out; |
| 2568 | |
| 2569 | /* Make offset relative to start of frags */ |
| 2570 | offset -= skb_headlen(skb); |
| 2571 | |
| 2572 | /* Find where we are in frag list */ |
| 2573 | for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { |
| 2574 | skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; |
| 2575 | |
Matthew Wilcox (Oracle) | d8e18a5 | 2019-07-22 20:08:26 -0700 | [diff] [blame] | 2576 | if (offset < skb_frag_size(frag)) |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2577 | break; |
| 2578 | |
Matthew Wilcox (Oracle) | d8e18a5 | 2019-07-22 20:08:26 -0700 | [diff] [blame] | 2579 | offset -= skb_frag_size(frag); |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { |
| 2583 | skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; |
| 2584 | |
Matthew Wilcox (Oracle) | d8e18a5 | 2019-07-22 20:08:26 -0700 | [diff] [blame] | 2585 | slen = min_t(size_t, len, skb_frag_size(frag) - offset); |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2586 | |
| 2587 | while (slen) { |
Cong Wang | 0739cd2 | 2021-03-30 19:32:24 -0700 | [diff] [blame] | 2588 | ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked, |
| 2589 | sendpage_unlocked, sk, |
| 2590 | skb_frag_page(frag), |
| 2591 | skb_frag_off(frag) + offset, |
| 2592 | slen, MSG_DONTWAIT); |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2593 | if (ret <= 0) |
| 2594 | goto error; |
| 2595 | |
| 2596 | len -= ret; |
| 2597 | offset += ret; |
| 2598 | slen -= ret; |
| 2599 | } |
| 2600 | |
| 2601 | offset = 0; |
| 2602 | } |
| 2603 | |
| 2604 | if (len) { |
| 2605 | /* Process any frag lists */ |
| 2606 | |
| 2607 | if (skb == head) { |
| 2608 | if (skb_has_frag_list(skb)) { |
| 2609 | skb = skb_shinfo(skb)->frag_list; |
| 2610 | goto do_frag_list; |
| 2611 | } |
| 2612 | } else if (skb->next) { |
| 2613 | skb = skb->next; |
| 2614 | goto do_frag_list; |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | out: |
| 2619 | return orig_len - len; |
| 2620 | |
| 2621 | error: |
| 2622 | return orig_len == len ? ret : orig_len - len; |
| 2623 | } |
Cong Wang | 0739cd2 | 2021-03-30 19:32:24 -0700 | [diff] [blame] | 2624 | |
| 2625 | /* Send skb data on a socket. Socket must be locked. */ |
| 2626 | int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, |
| 2627 | int len) |
| 2628 | { |
| 2629 | return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked, |
| 2630 | kernel_sendpage_locked); |
| 2631 | } |
Tom Herbert | 20bf50d | 2017-07-28 16:22:42 -0700 | [diff] [blame] | 2632 | EXPORT_SYMBOL_GPL(skb_send_sock_locked); |
| 2633 | |
Cong Wang | 0739cd2 | 2021-03-30 19:32:24 -0700 | [diff] [blame] | 2634 | /* Send skb data on a socket. Socket must be unlocked. */ |
| 2635 | int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) |
| 2636 | { |
| 2637 | return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked, |
| 2638 | sendpage_unlocked); |
| 2639 | } |
| 2640 | |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2641 | /** |
| 2642 | * skb_store_bits - store bits from kernel buffer to skb |
| 2643 | * @skb: destination buffer |
| 2644 | * @offset: offset in destination |
| 2645 | * @from: source buffer |
| 2646 | * @len: number of bytes to copy |
| 2647 | * |
| 2648 | * Copy the specified number of bytes from the source buffer to the |
| 2649 | * destination skb. This function handles all the messy bits of |
| 2650 | * traversing fragment lists and such. |
| 2651 | */ |
| 2652 | |
Stephen Hemminger | 0c6fcc8 | 2007-04-20 16:40:01 -0700 | [diff] [blame] | 2653 | 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] | 2654 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2655 | int start = skb_headlen(skb); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2656 | struct sk_buff *frag_iter; |
| 2657 | int i, copy; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2658 | |
| 2659 | if (offset > (int)skb->len - len) |
| 2660 | goto fault; |
| 2661 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2662 | if ((copy = start - offset) > 0) { |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2663 | if (copy > len) |
| 2664 | copy = len; |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 2665 | skb_copy_to_linear_data_offset(skb, offset, from, copy); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2666 | if ((len -= copy) == 0) |
| 2667 | return 0; |
| 2668 | offset += copy; |
| 2669 | from += copy; |
| 2670 | } |
| 2671 | |
| 2672 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 2673 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2674 | int end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2675 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2676 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2677 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2678 | end = start + skb_frag_size(frag); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2679 | if ((copy = end - offset) > 0) { |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2680 | u32 p_off, p_len, copied; |
| 2681 | struct page *p; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2682 | u8 *vaddr; |
| 2683 | |
| 2684 | if (copy > len) |
| 2685 | copy = len; |
| 2686 | |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2687 | skb_frag_foreach_page(frag, |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 2688 | skb_frag_off(frag) + offset - start, |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2689 | copy, p, p_off, p_len, copied) { |
| 2690 | vaddr = kmap_atomic(p); |
| 2691 | memcpy(vaddr + p_off, from + copied, p_len); |
| 2692 | kunmap_atomic(vaddr); |
| 2693 | } |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2694 | |
| 2695 | if ((len -= copy) == 0) |
| 2696 | return 0; |
| 2697 | offset += copy; |
| 2698 | from += copy; |
| 2699 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2700 | start = end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2703 | skb_walk_frags(skb, frag_iter) { |
| 2704 | int end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2705 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2706 | WARN_ON(start > offset + len); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2707 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2708 | end = start + frag_iter->len; |
| 2709 | if ((copy = end - offset) > 0) { |
| 2710 | if (copy > len) |
| 2711 | copy = len; |
| 2712 | if (skb_store_bits(frag_iter, offset - start, |
| 2713 | from, copy)) |
| 2714 | goto fault; |
| 2715 | if ((len -= copy) == 0) |
| 2716 | return 0; |
| 2717 | offset += copy; |
| 2718 | from += copy; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2719 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2720 | start = end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2721 | } |
| 2722 | if (!len) |
| 2723 | return 0; |
| 2724 | |
| 2725 | fault: |
| 2726 | return -EFAULT; |
| 2727 | } |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2728 | EXPORT_SYMBOL(skb_store_bits); |
| 2729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | /* Checksum skb data. */ |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2731 | __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, |
| 2732 | __wsum csum, const struct skb_checksum_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2734 | int start = skb_headlen(skb); |
| 2735 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2736 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | int pos = 0; |
| 2738 | |
| 2739 | /* Checksum header. */ |
| 2740 | if (copy > 0) { |
| 2741 | if (copy > len) |
| 2742 | copy = len; |
Matteo Croce | 2544af0 | 2019-05-29 17:13:48 +0200 | [diff] [blame] | 2743 | csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, |
| 2744 | skb->data + offset, copy, csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | if ((len -= copy) == 0) |
| 2746 | return csum; |
| 2747 | offset += copy; |
| 2748 | pos = copy; |
| 2749 | } |
| 2750 | |
| 2751 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2752 | int end; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2753 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2755 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2756 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2757 | end = start + skb_frag_size(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | if ((copy = end - offset) > 0) { |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2759 | u32 p_off, p_len, copied; |
| 2760 | struct page *p; |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 2761 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | |
| 2764 | if (copy > len) |
| 2765 | copy = len; |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2766 | |
| 2767 | skb_frag_foreach_page(frag, |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 2768 | skb_frag_off(frag) + offset - start, |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2769 | copy, p, p_off, p_len, copied) { |
| 2770 | vaddr = kmap_atomic(p); |
Matteo Croce | 2544af0 | 2019-05-29 17:13:48 +0200 | [diff] [blame] | 2771 | csum2 = INDIRECT_CALL_1(ops->update, |
| 2772 | csum_partial_ext, |
| 2773 | vaddr + p_off, p_len, 0); |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2774 | kunmap_atomic(vaddr); |
Matteo Croce | 2544af0 | 2019-05-29 17:13:48 +0200 | [diff] [blame] | 2775 | csum = INDIRECT_CALL_1(ops->combine, |
| 2776 | csum_block_add_ext, csum, |
| 2777 | csum2, pos, p_len); |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2778 | pos += p_len; |
| 2779 | } |
| 2780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | if (!(len -= copy)) |
| 2782 | return csum; |
| 2783 | offset += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2785 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | } |
| 2787 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2788 | skb_walk_frags(skb, frag_iter) { |
| 2789 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2791 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2793 | end = start + frag_iter->len; |
| 2794 | if ((copy = end - offset) > 0) { |
| 2795 | __wsum csum2; |
| 2796 | if (copy > len) |
| 2797 | copy = len; |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2798 | csum2 = __skb_checksum(frag_iter, offset - start, |
| 2799 | copy, 0, ops); |
Matteo Croce | 2544af0 | 2019-05-29 17:13:48 +0200 | [diff] [blame] | 2800 | csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, |
| 2801 | csum, csum2, pos, copy); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2802 | if ((len -= copy) == 0) |
| 2803 | return csum; |
| 2804 | offset += copy; |
| 2805 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2807 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2809 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | |
| 2811 | return csum; |
| 2812 | } |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2813 | EXPORT_SYMBOL(__skb_checksum); |
| 2814 | |
| 2815 | __wsum skb_checksum(const struct sk_buff *skb, int offset, |
| 2816 | int len, __wsum csum) |
| 2817 | { |
| 2818 | const struct skb_checksum_ops ops = { |
Daniel Borkmann | cea80ea | 2013-11-04 17:10:25 +0100 | [diff] [blame] | 2819 | .update = csum_partial_ext, |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2820 | .combine = csum_block_add_ext, |
| 2821 | }; |
| 2822 | |
| 2823 | return __skb_checksum(skb, offset, len, csum, &ops); |
| 2824 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2825 | EXPORT_SYMBOL(skb_checksum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | |
| 2827 | /* Both of above in one bottle. */ |
| 2828 | |
Al Viro | 81d7766 | 2006-11-14 21:37:33 -0800 | [diff] [blame] | 2829 | __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, |
Al Viro | 8d5930d | 2020-07-10 20:07:10 -0400 | [diff] [blame] | 2830 | u8 *to, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2832 | int start = skb_headlen(skb); |
| 2833 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2834 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | int pos = 0; |
Al Viro | 8d5930d | 2020-07-10 20:07:10 -0400 | [diff] [blame] | 2836 | __wsum csum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | |
| 2838 | /* Copy header. */ |
| 2839 | if (copy > 0) { |
| 2840 | if (copy > len) |
| 2841 | copy = len; |
| 2842 | csum = csum_partial_copy_nocheck(skb->data + offset, to, |
Al Viro | cc44c17 | 2020-07-11 00:12:07 -0400 | [diff] [blame] | 2843 | copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | if ((len -= copy) == 0) |
| 2845 | return csum; |
| 2846 | offset += copy; |
| 2847 | to += copy; |
| 2848 | pos = copy; |
| 2849 | } |
| 2850 | |
| 2851 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2852 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2854 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2855 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2856 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | if ((copy = end - offset) > 0) { |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2858 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 2859 | u32 p_off, p_len, copied; |
| 2860 | struct page *p; |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 2861 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | |
| 2864 | if (copy > len) |
| 2865 | copy = len; |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2866 | |
| 2867 | skb_frag_foreach_page(frag, |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 2868 | skb_frag_off(frag) + offset - start, |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2869 | copy, p, p_off, p_len, copied) { |
| 2870 | vaddr = kmap_atomic(p); |
| 2871 | csum2 = csum_partial_copy_nocheck(vaddr + p_off, |
| 2872 | to + copied, |
Al Viro | cc44c17 | 2020-07-11 00:12:07 -0400 | [diff] [blame] | 2873 | p_len); |
Willem de Bruijn | c613c20 | 2017-07-31 08:15:47 -0400 | [diff] [blame] | 2874 | kunmap_atomic(vaddr); |
| 2875 | csum = csum_block_add(csum, csum2, pos); |
| 2876 | pos += p_len; |
| 2877 | } |
| 2878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | if (!(len -= copy)) |
| 2880 | return csum; |
| 2881 | offset += copy; |
| 2882 | to += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2884 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | } |
| 2886 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2887 | skb_walk_frags(skb, frag_iter) { |
| 2888 | __wsum csum2; |
| 2889 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2891 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2893 | end = start + frag_iter->len; |
| 2894 | if ((copy = end - offset) > 0) { |
| 2895 | if (copy > len) |
| 2896 | copy = len; |
| 2897 | csum2 = skb_copy_and_csum_bits(frag_iter, |
| 2898 | offset - start, |
Al Viro | 8d5930d | 2020-07-10 20:07:10 -0400 | [diff] [blame] | 2899 | to, copy); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2900 | csum = csum_block_add(csum, csum2, pos); |
| 2901 | if ((len -= copy) == 0) |
| 2902 | return csum; |
| 2903 | offset += copy; |
| 2904 | to += copy; |
| 2905 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2907 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2909 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | return csum; |
| 2911 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2912 | EXPORT_SYMBOL(skb_copy_and_csum_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2914 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) |
| 2915 | { |
| 2916 | __sum16 sum; |
| 2917 | |
| 2918 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); |
Cong Wang | 1464193 | 2018-11-26 09:31:26 -0800 | [diff] [blame] | 2919 | /* See comments in __skb_checksum_complete(). */ |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2920 | if (likely(!sum)) { |
| 2921 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && |
| 2922 | !skb->csum_complete_sw) |
Cong Wang | 7fe50ac | 2018-11-12 14:47:18 -0800 | [diff] [blame] | 2923 | netdev_rx_csum_fault(skb->dev, skb); |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2924 | } |
| 2925 | if (!skb_shared(skb)) |
| 2926 | skb->csum_valid = !sum; |
| 2927 | return sum; |
| 2928 | } |
| 2929 | EXPORT_SYMBOL(__skb_checksum_complete_head); |
| 2930 | |
Cong Wang | 1464193 | 2018-11-26 09:31:26 -0800 | [diff] [blame] | 2931 | /* This function assumes skb->csum already holds pseudo header's checksum, |
| 2932 | * which has been changed from the hardware checksum, for example, by |
| 2933 | * __skb_checksum_validate_complete(). And, the original skb->csum must |
| 2934 | * have been validated unsuccessfully for CHECKSUM_COMPLETE case. |
| 2935 | * |
| 2936 | * It returns non-zero if the recomputed checksum is still invalid, otherwise |
| 2937 | * zero. The new checksum is stored back into skb->csum unless the skb is |
| 2938 | * shared. |
| 2939 | */ |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2940 | __sum16 __skb_checksum_complete(struct sk_buff *skb) |
| 2941 | { |
| 2942 | __wsum csum; |
| 2943 | __sum16 sum; |
| 2944 | |
| 2945 | csum = skb_checksum(skb, 0, skb->len, 0); |
| 2946 | |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2947 | sum = csum_fold(csum_add(skb->csum, csum)); |
Cong Wang | 1464193 | 2018-11-26 09:31:26 -0800 | [diff] [blame] | 2948 | /* This check is inverted, because we already knew the hardware |
| 2949 | * checksum is invalid before calling this function. So, if the |
| 2950 | * re-computed checksum is valid instead, then we have a mismatch |
| 2951 | * between the original skb->csum and skb_checksum(). This means either |
| 2952 | * the original hardware checksum is incorrect or we screw up skb->csum |
| 2953 | * when moving skb->data around. |
| 2954 | */ |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2955 | if (likely(!sum)) { |
| 2956 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && |
| 2957 | !skb->csum_complete_sw) |
Cong Wang | 7fe50ac | 2018-11-12 14:47:18 -0800 | [diff] [blame] | 2958 | netdev_rx_csum_fault(skb->dev, skb); |
Cong Wang | 49f8e83 | 2018-11-08 14:05:42 -0800 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | if (!skb_shared(skb)) { |
| 2962 | /* Save full packet checksum */ |
| 2963 | skb->csum = csum; |
| 2964 | skb->ip_summed = CHECKSUM_COMPLETE; |
| 2965 | skb->csum_complete_sw = 1; |
| 2966 | skb->csum_valid = !sum; |
| 2967 | } |
| 2968 | |
| 2969 | return sum; |
| 2970 | } |
| 2971 | EXPORT_SYMBOL(__skb_checksum_complete); |
| 2972 | |
Davide Caratti | 9617813 | 2017-05-18 15:44:37 +0200 | [diff] [blame] | 2973 | static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) |
| 2974 | { |
| 2975 | net_warn_ratelimited( |
| 2976 | "%s: attempt to compute crc32c without libcrc32c.ko\n", |
| 2977 | __func__); |
| 2978 | return 0; |
| 2979 | } |
| 2980 | |
| 2981 | static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, |
| 2982 | int offset, int len) |
| 2983 | { |
| 2984 | net_warn_ratelimited( |
| 2985 | "%s: attempt to compute crc32c without libcrc32c.ko\n", |
| 2986 | __func__); |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | static const struct skb_checksum_ops default_crc32c_ops = { |
| 2991 | .update = warn_crc32c_csum_update, |
| 2992 | .combine = warn_crc32c_csum_combine, |
| 2993 | }; |
| 2994 | |
| 2995 | const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = |
| 2996 | &default_crc32c_ops; |
| 2997 | EXPORT_SYMBOL(crc32c_csum_stub); |
| 2998 | |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2999 | /** |
| 3000 | * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() |
| 3001 | * @from: source buffer |
| 3002 | * |
| 3003 | * Calculates the amount of linear headroom needed in the 'to' skb passed |
| 3004 | * into skb_zerocopy(). |
| 3005 | */ |
| 3006 | unsigned int |
| 3007 | skb_zerocopy_headlen(const struct sk_buff *from) |
| 3008 | { |
| 3009 | unsigned int hlen = 0; |
| 3010 | |
| 3011 | if (!from->head_frag || |
| 3012 | skb_headlen(from) < L1_CACHE_BYTES || |
| 3013 | skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) |
| 3014 | hlen = skb_headlen(from); |
| 3015 | |
| 3016 | if (skb_has_frag_list(from)) |
| 3017 | hlen = from->len; |
| 3018 | |
| 3019 | return hlen; |
| 3020 | } |
| 3021 | EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); |
| 3022 | |
| 3023 | /** |
| 3024 | * skb_zerocopy - Zero copy skb to skb |
| 3025 | * @to: destination buffer |
Masanari Iida | 7fceb4d | 2014-01-29 01:05:28 +0900 | [diff] [blame] | 3026 | * @from: source buffer |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 3027 | * @len: number of bytes to copy from source buffer |
| 3028 | * @hlen: size of linear headroom in destination buffer |
| 3029 | * |
| 3030 | * Copies up to `len` bytes from `from` to `to` by creating references |
| 3031 | * to the frags in the source buffer. |
| 3032 | * |
| 3033 | * The `hlen` as calculated by skb_zerocopy_headlen() specifies the |
| 3034 | * headroom in the `to` buffer. |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 3035 | * |
| 3036 | * Return value: |
| 3037 | * 0: everything is OK |
| 3038 | * -ENOMEM: couldn't orphan frags of @from due to lack of memory |
| 3039 | * -EFAULT: skb_copy_bits() found some problem with skb geometry |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 3040 | */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 3041 | int |
| 3042 | 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] | 3043 | { |
| 3044 | int i, j = 0; |
| 3045 | int plen = 0; /* length of skb->head fragment */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 3046 | int ret; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 3047 | struct page *page; |
| 3048 | unsigned int offset; |
| 3049 | |
| 3050 | BUG_ON(!from->head_frag && !hlen); |
| 3051 | |
| 3052 | /* dont bother with small payloads */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 3053 | if (len <= skb_tailroom(to)) |
| 3054 | return skb_copy_bits(from, 0, skb_put(to, len), len); |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 3055 | |
| 3056 | if (hlen) { |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 3057 | ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); |
| 3058 | if (unlikely(ret)) |
| 3059 | return ret; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 3060 | len -= hlen; |
| 3061 | } else { |
| 3062 | plen = min_t(int, skb_headlen(from), len); |
| 3063 | if (plen) { |
| 3064 | page = virt_to_head_page(from->head); |
| 3065 | offset = from->data - (unsigned char *)page_address(page); |
| 3066 | __skb_fill_page_desc(to, 0, page, offset, plen); |
| 3067 | get_page(page); |
| 3068 | j = 1; |
| 3069 | len -= plen; |
| 3070 | } |
| 3071 | } |
| 3072 | |
| 3073 | to->truesize += len + plen; |
| 3074 | to->len += len + plen; |
| 3075 | to->data_len += len + plen; |
| 3076 | |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 3077 | if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { |
| 3078 | skb_tx_error(from); |
| 3079 | return -ENOMEM; |
| 3080 | } |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 3081 | skb_zerocopy_clone(to, from, GFP_ATOMIC); |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 3082 | |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 3083 | for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { |
Matthew Wilcox (Oracle) | d8e18a5 | 2019-07-22 20:08:26 -0700 | [diff] [blame] | 3084 | int size; |
| 3085 | |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 3086 | if (!len) |
| 3087 | break; |
| 3088 | skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; |
Matthew Wilcox (Oracle) | d8e18a5 | 2019-07-22 20:08:26 -0700 | [diff] [blame] | 3089 | size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), |
| 3090 | len); |
| 3091 | skb_frag_size_set(&skb_shinfo(to)->frags[j], size); |
| 3092 | len -= size; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 3093 | skb_frag_ref(to, j); |
| 3094 | j++; |
| 3095 | } |
| 3096 | skb_shinfo(to)->nr_frags = j; |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 3097 | |
| 3098 | return 0; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 3099 | } |
| 3100 | EXPORT_SYMBOL_GPL(skb_zerocopy); |
| 3101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) |
| 3103 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 3104 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | long csstart; |
| 3106 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 3107 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
Michał Mirosław | 55508d6 | 2010-12-14 15:24:08 +0000 | [diff] [blame] | 3108 | csstart = skb_checksum_start_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | else |
| 3110 | csstart = skb_headlen(skb); |
| 3111 | |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 3112 | BUG_ON(csstart > skb_headlen(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 3114 | skb_copy_from_linear_data(skb, to, csstart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | |
| 3116 | csum = 0; |
| 3117 | if (csstart != skb->len) |
| 3118 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, |
Al Viro | 8d5930d | 2020-07-10 20:07:10 -0400 | [diff] [blame] | 3119 | skb->len - csstart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 3121 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Al Viro | ff1dcad | 2006-11-20 18:07:29 -0800 | [diff] [blame] | 3122 | long csstuff = csstart + skb->csum_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 3124 | *((__sum16 *)(to + csstuff)) = csum_fold(csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | } |
| 3126 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3127 | EXPORT_SYMBOL(skb_copy_and_csum_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | |
| 3129 | /** |
| 3130 | * skb_dequeue - remove from the head of the queue |
| 3131 | * @list: list to dequeue from |
| 3132 | * |
| 3133 | * Remove the head of the list. The list lock is taken so the function |
| 3134 | * may be used safely with other locking list functions. The head item is |
| 3135 | * returned or %NULL if the list is empty. |
| 3136 | */ |
| 3137 | |
| 3138 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) |
| 3139 | { |
| 3140 | unsigned long flags; |
| 3141 | struct sk_buff *result; |
| 3142 | |
| 3143 | spin_lock_irqsave(&list->lock, flags); |
| 3144 | result = __skb_dequeue(list); |
| 3145 | spin_unlock_irqrestore(&list->lock, flags); |
| 3146 | return result; |
| 3147 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3148 | EXPORT_SYMBOL(skb_dequeue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | |
| 3150 | /** |
| 3151 | * skb_dequeue_tail - remove from the tail of the queue |
| 3152 | * @list: list to dequeue from |
| 3153 | * |
| 3154 | * Remove the tail of the list. The list lock is taken so the function |
| 3155 | * may be used safely with other locking list functions. The tail item is |
| 3156 | * returned or %NULL if the list is empty. |
| 3157 | */ |
| 3158 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) |
| 3159 | { |
| 3160 | unsigned long flags; |
| 3161 | struct sk_buff *result; |
| 3162 | |
| 3163 | spin_lock_irqsave(&list->lock, flags); |
| 3164 | result = __skb_dequeue_tail(list); |
| 3165 | spin_unlock_irqrestore(&list->lock, flags); |
| 3166 | return result; |
| 3167 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3168 | EXPORT_SYMBOL(skb_dequeue_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | |
| 3170 | /** |
| 3171 | * skb_queue_purge - empty a list |
| 3172 | * @list: list to empty |
| 3173 | * |
| 3174 | * Delete all buffers on an &sk_buff list. Each buffer is removed from |
| 3175 | * the list and one reference dropped. This function takes the list |
| 3176 | * lock and is atomic with respect to other list locking functions. |
| 3177 | */ |
| 3178 | void skb_queue_purge(struct sk_buff_head *list) |
| 3179 | { |
| 3180 | struct sk_buff *skb; |
| 3181 | while ((skb = skb_dequeue(list)) != NULL) |
| 3182 | kfree_skb(skb); |
| 3183 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3184 | EXPORT_SYMBOL(skb_queue_purge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | |
| 3186 | /** |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3187 | * skb_rbtree_purge - empty a skb rbtree |
| 3188 | * @root: root of the rbtree to empty |
Peter Oskolkov | 385114d | 2018-08-02 23:34:38 +0000 | [diff] [blame] | 3189 | * Return value: the sum of truesizes of all purged skbs. |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3190 | * |
| 3191 | * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from |
| 3192 | * the list and one reference dropped. This function does not take |
| 3193 | * any lock. Synchronization should be handled by the caller (e.g., TCP |
| 3194 | * out-of-order queue is protected by the socket lock). |
| 3195 | */ |
Peter Oskolkov | 385114d | 2018-08-02 23:34:38 +0000 | [diff] [blame] | 3196 | unsigned int skb_rbtree_purge(struct rb_root *root) |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3197 | { |
Eric Dumazet | 7c90584 | 2017-09-23 12:39:12 -0700 | [diff] [blame] | 3198 | struct rb_node *p = rb_first(root); |
Peter Oskolkov | 385114d | 2018-08-02 23:34:38 +0000 | [diff] [blame] | 3199 | unsigned int sum = 0; |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3200 | |
Eric Dumazet | 7c90584 | 2017-09-23 12:39:12 -0700 | [diff] [blame] | 3201 | while (p) { |
| 3202 | struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); |
| 3203 | |
| 3204 | p = rb_next(p); |
| 3205 | rb_erase(&skb->rbnode, root); |
Peter Oskolkov | 385114d | 2018-08-02 23:34:38 +0000 | [diff] [blame] | 3206 | sum += skb->truesize; |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3207 | kfree_skb(skb); |
Eric Dumazet | 7c90584 | 2017-09-23 12:39:12 -0700 | [diff] [blame] | 3208 | } |
Peter Oskolkov | 385114d | 2018-08-02 23:34:38 +0000 | [diff] [blame] | 3209 | return sum; |
Yaogong Wang | 9f5afea | 2016-09-07 14:49:28 -0700 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3213 | * skb_queue_head - queue a buffer at the list head |
| 3214 | * @list: list to use |
| 3215 | * @newsk: buffer to queue |
| 3216 | * |
| 3217 | * Queue a buffer at the start of the list. This function takes the |
| 3218 | * list lock and can be used safely with other locking &sk_buff functions |
| 3219 | * safely. |
| 3220 | * |
| 3221 | * A buffer cannot be placed on two lists at the same time. |
| 3222 | */ |
| 3223 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) |
| 3224 | { |
| 3225 | unsigned long flags; |
| 3226 | |
| 3227 | spin_lock_irqsave(&list->lock, flags); |
| 3228 | __skb_queue_head(list, newsk); |
| 3229 | spin_unlock_irqrestore(&list->lock, flags); |
| 3230 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3231 | EXPORT_SYMBOL(skb_queue_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | |
| 3233 | /** |
| 3234 | * skb_queue_tail - queue a buffer at the list tail |
| 3235 | * @list: list to use |
| 3236 | * @newsk: buffer to queue |
| 3237 | * |
| 3238 | * Queue a buffer at the tail of the list. This function takes the |
| 3239 | * list lock and can be used safely with other locking &sk_buff functions |
| 3240 | * safely. |
| 3241 | * |
| 3242 | * A buffer cannot be placed on two lists at the same time. |
| 3243 | */ |
| 3244 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) |
| 3245 | { |
| 3246 | unsigned long flags; |
| 3247 | |
| 3248 | spin_lock_irqsave(&list->lock, flags); |
| 3249 | __skb_queue_tail(list, newsk); |
| 3250 | spin_unlock_irqrestore(&list->lock, flags); |
| 3251 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3252 | EXPORT_SYMBOL(skb_queue_tail); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | /** |
| 3255 | * skb_unlink - remove a buffer from a list |
| 3256 | * @skb: buffer to remove |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3257 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3259 | * Remove a packet from a list. The list locks are taken and this |
| 3260 | * function is atomic with respect to other list locked calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3262 | * You must know what list the SKB is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3264 | void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3266 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3268 | spin_lock_irqsave(&list->lock, flags); |
| 3269 | __skb_unlink(skb, list); |
| 3270 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3272 | EXPORT_SYMBOL(skb_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | /** |
| 3275 | * skb_append - append a buffer |
| 3276 | * @old: buffer to insert after |
| 3277 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3278 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | * |
| 3280 | * Place a packet after a given packet in a list. The list locks are taken |
| 3281 | * and this function is atomic with respect to other list locked calls. |
| 3282 | * A buffer cannot be placed on two lists at the same time. |
| 3283 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3284 | 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] | 3285 | { |
| 3286 | unsigned long flags; |
| 3287 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3288 | spin_lock_irqsave(&list->lock, flags); |
Gerrit Renker | 7de6c03 | 2008-04-14 00:05:09 -0700 | [diff] [blame] | 3289 | __skb_queue_after(list, old, newsk); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3290 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3292 | EXPORT_SYMBOL(skb_append); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | static inline void skb_split_inside_header(struct sk_buff *skb, |
| 3295 | struct sk_buff* skb1, |
| 3296 | const u32 len, const int pos) |
| 3297 | { |
| 3298 | int i; |
| 3299 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 3300 | skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), |
| 3301 | pos - len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | /* And move data appendix as is. */ |
| 3303 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 3304 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 3305 | |
| 3306 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; |
| 3307 | skb_shinfo(skb)->nr_frags = 0; |
| 3308 | skb1->data_len = skb->data_len; |
| 3309 | skb1->len += skb1->data_len; |
| 3310 | skb->data_len = 0; |
| 3311 | skb->len = len; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 3312 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | static inline void skb_split_no_header(struct sk_buff *skb, |
| 3316 | struct sk_buff* skb1, |
| 3317 | const u32 len, int pos) |
| 3318 | { |
| 3319 | int i, k = 0; |
| 3320 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 3321 | |
| 3322 | skb_shinfo(skb)->nr_frags = 0; |
| 3323 | skb1->len = skb1->data_len = skb->len - len; |
| 3324 | skb->len = len; |
| 3325 | skb->data_len = len - pos; |
| 3326 | |
| 3327 | for (i = 0; i < nfrags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3328 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | |
| 3330 | if (pos + size > len) { |
| 3331 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 3332 | |
| 3333 | if (pos < len) { |
| 3334 | /* Split frag. |
| 3335 | * We have two variants in this case: |
| 3336 | * 1. Move all the frag to the second |
| 3337 | * part, if it is possible. F.e. |
| 3338 | * this approach is mandatory for TUX, |
| 3339 | * where splitting is expensive. |
| 3340 | * 2. Split is accurately. We make this. |
| 3341 | */ |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3342 | skb_frag_ref(skb, i); |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 3343 | skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3344 | skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); |
| 3345 | skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | skb_shinfo(skb)->nr_frags++; |
| 3347 | } |
| 3348 | k++; |
| 3349 | } else |
| 3350 | skb_shinfo(skb)->nr_frags++; |
| 3351 | pos += size; |
| 3352 | } |
| 3353 | skb_shinfo(skb1)->nr_frags = k; |
| 3354 | } |
| 3355 | |
| 3356 | /** |
| 3357 | * skb_split - Split fragmented skb to two parts at length len. |
| 3358 | * @skb: the buffer to split |
| 3359 | * @skb1: the buffer to receive the second part |
| 3360 | * @len: new length for skb |
| 3361 | */ |
| 3362 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) |
| 3363 | { |
| 3364 | int pos = skb_headlen(skb); |
| 3365 | |
Jonathan Lemon | 06b4feb | 2021-01-06 14:18:38 -0800 | [diff] [blame] | 3366 | skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG; |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 3367 | skb_zerocopy_clone(skb1, skb, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | if (len < pos) /* Split line is inside header. */ |
| 3369 | skb_split_inside_header(skb, skb1, len, pos); |
| 3370 | else /* Second chunk has no header, nothing to copy. */ |
| 3371 | skb_split_no_header(skb, skb1, len, pos); |
| 3372 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3373 | EXPORT_SYMBOL(skb_split); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3374 | |
Ilpo Järvinen | 9f782db | 2008-11-25 13:57:01 -0800 | [diff] [blame] | 3375 | /* Shifting from/to a cloned skb is a no-go. |
| 3376 | * |
| 3377 | * Caller cannot keep skb_shinfo related pointers past calling here! |
| 3378 | */ |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3379 | static int skb_prepare_for_shift(struct sk_buff *skb) |
| 3380 | { |
Marco Elver | 097b914 | 2021-02-01 17:04:20 +0100 | [diff] [blame] | 3381 | int ret = 0; |
| 3382 | |
| 3383 | if (skb_cloned(skb)) { |
| 3384 | /* Save and restore truesize: pskb_expand_head() may reallocate |
| 3385 | * memory where ksize(kmalloc(S)) != ksize(kmalloc(S)), but we |
| 3386 | * cannot change truesize at this point. |
| 3387 | */ |
| 3388 | unsigned int save_truesize = skb->truesize; |
| 3389 | |
| 3390 | ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 3391 | skb->truesize = save_truesize; |
| 3392 | } |
| 3393 | return ret; |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3394 | } |
| 3395 | |
| 3396 | /** |
| 3397 | * skb_shift - Shifts paged data partially from skb to another |
| 3398 | * @tgt: buffer into which tail data gets added |
| 3399 | * @skb: buffer from which the paged data comes from |
| 3400 | * @shiftlen: shift up to this many bytes |
| 3401 | * |
| 3402 | * 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] | 3403 | * 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] | 3404 | * It's up to caller to free skb if everything was shifted. |
| 3405 | * |
| 3406 | * If @tgt runs out of frags, the whole operation is aborted. |
| 3407 | * |
| 3408 | * Skb cannot include anything else but paged data while tgt is allowed |
| 3409 | * to have non-paged data as well. |
| 3410 | * |
| 3411 | * TODO: full sized shift could be optimized but that would need |
| 3412 | * specialized skb free'er to handle frags without up-to-date nr_frags. |
| 3413 | */ |
| 3414 | int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) |
| 3415 | { |
| 3416 | int from, to, merge, todo; |
Matthew Wilcox (Oracle) | d8e18a5 | 2019-07-22 20:08:26 -0700 | [diff] [blame] | 3417 | skb_frag_t *fragfrom, *fragto; |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3418 | |
| 3419 | BUG_ON(shiftlen > skb->len); |
Eric Dumazet | f8071cd | 2016-11-15 12:51:50 -0800 | [diff] [blame] | 3420 | |
| 3421 | if (skb_headlen(skb)) |
| 3422 | return 0; |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 3423 | if (skb_zcopy(tgt) || skb_zcopy(skb)) |
| 3424 | return 0; |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3425 | |
| 3426 | todo = shiftlen; |
| 3427 | from = 0; |
| 3428 | to = skb_shinfo(tgt)->nr_frags; |
| 3429 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 3430 | |
| 3431 | /* Actual merge is delayed until the point when we know we can |
| 3432 | * commit all, so that we don't have to undo partial changes |
| 3433 | */ |
| 3434 | if (!to || |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3435 | !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 3436 | skb_frag_off(fragfrom))) { |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3437 | merge = -1; |
| 3438 | } else { |
| 3439 | merge = to - 1; |
| 3440 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3441 | todo -= skb_frag_size(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3442 | if (todo < 0) { |
| 3443 | if (skb_prepare_for_shift(skb) || |
| 3444 | skb_prepare_for_shift(tgt)) |
| 3445 | return 0; |
| 3446 | |
Ilpo Järvinen | 9f782db | 2008-11-25 13:57:01 -0800 | [diff] [blame] | 3447 | /* All previous frag pointers might be stale! */ |
| 3448 | fragfrom = &skb_shinfo(skb)->frags[from]; |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3449 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 3450 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3451 | skb_frag_size_add(fragto, shiftlen); |
| 3452 | skb_frag_size_sub(fragfrom, shiftlen); |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 3453 | skb_frag_off_add(fragfrom, shiftlen); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3454 | |
| 3455 | goto onlymerged; |
| 3456 | } |
| 3457 | |
| 3458 | from++; |
| 3459 | } |
| 3460 | |
| 3461 | /* Skip full, not-fitting skb to avoid expensive operations */ |
| 3462 | if ((shiftlen == skb->len) && |
| 3463 | (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) |
| 3464 | return 0; |
| 3465 | |
| 3466 | if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) |
| 3467 | return 0; |
| 3468 | |
| 3469 | while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { |
| 3470 | if (to == MAX_SKB_FRAGS) |
| 3471 | return 0; |
| 3472 | |
| 3473 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 3474 | fragto = &skb_shinfo(tgt)->frags[to]; |
| 3475 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3476 | if (todo >= skb_frag_size(fragfrom)) { |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3477 | *fragto = *fragfrom; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3478 | todo -= skb_frag_size(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3479 | from++; |
| 3480 | to++; |
| 3481 | |
| 3482 | } else { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3483 | __skb_frag_ref(fragfrom); |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 3484 | skb_frag_page_copy(fragto, fragfrom); |
| 3485 | skb_frag_off_copy(fragto, fragfrom); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3486 | skb_frag_size_set(fragto, todo); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3487 | |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 3488 | skb_frag_off_add(fragfrom, todo); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3489 | skb_frag_size_sub(fragfrom, todo); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3490 | todo = 0; |
| 3491 | |
| 3492 | to++; |
| 3493 | break; |
| 3494 | } |
| 3495 | } |
| 3496 | |
| 3497 | /* Ready to "commit" this state change to tgt */ |
| 3498 | skb_shinfo(tgt)->nr_frags = to; |
| 3499 | |
| 3500 | if (merge >= 0) { |
| 3501 | fragfrom = &skb_shinfo(skb)->frags[0]; |
| 3502 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 3503 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3504 | skb_frag_size_add(fragto, skb_frag_size(fragfrom)); |
Ilias Apalodimas | 6a5bcd8 | 2021-06-07 21:02:38 +0200 | [diff] [blame] | 3505 | __skb_frag_unref(fragfrom, skb->pp_recycle); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | /* Reposition in the original skb */ |
| 3509 | to = 0; |
| 3510 | while (from < skb_shinfo(skb)->nr_frags) |
| 3511 | skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; |
| 3512 | skb_shinfo(skb)->nr_frags = to; |
| 3513 | |
| 3514 | BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); |
| 3515 | |
| 3516 | onlymerged: |
| 3517 | /* Most likely the tgt won't ever need its checksum anymore, skb on |
| 3518 | * the other hand might need it if it needs to be resent |
| 3519 | */ |
| 3520 | tgt->ip_summed = CHECKSUM_PARTIAL; |
| 3521 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 3522 | |
| 3523 | /* Yak, is it really working this way? Some helper please? */ |
| 3524 | skb->len -= shiftlen; |
| 3525 | skb->data_len -= shiftlen; |
| 3526 | skb->truesize -= shiftlen; |
| 3527 | tgt->len += shiftlen; |
| 3528 | tgt->data_len += shiftlen; |
| 3529 | tgt->truesize += shiftlen; |
| 3530 | |
| 3531 | return shiftlen; |
| 3532 | } |
| 3533 | |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3534 | /** |
| 3535 | * skb_prepare_seq_read - Prepare a sequential read of skb data |
| 3536 | * @skb: the buffer to read |
| 3537 | * @from: lower offset of data to be read |
| 3538 | * @to: upper offset of data to be read |
| 3539 | * @st: state variable |
| 3540 | * |
| 3541 | * Initializes the specified state variable. Must be called before |
| 3542 | * invoking skb_seq_read() for the first time. |
| 3543 | */ |
| 3544 | void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, |
| 3545 | unsigned int to, struct skb_seq_state *st) |
| 3546 | { |
| 3547 | st->lower_offset = from; |
| 3548 | st->upper_offset = to; |
| 3549 | st->root_skb = st->cur_skb = skb; |
| 3550 | st->frag_idx = st->stepped_offset = 0; |
| 3551 | st->frag_data = NULL; |
Willem de Bruijn | 97550f6 | 2021-01-09 17:18:33 -0500 | [diff] [blame] | 3552 | st->frag_off = 0; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3553 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3554 | EXPORT_SYMBOL(skb_prepare_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3555 | |
| 3556 | /** |
| 3557 | * skb_seq_read - Sequentially read skb data |
| 3558 | * @consumed: number of bytes consumed by the caller so far |
| 3559 | * @data: destination pointer for data to be returned |
| 3560 | * @st: state variable |
| 3561 | * |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 3562 | * Reads a block of skb data at @consumed relative to the |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3563 | * lower offset specified to skb_prepare_seq_read(). Assigns |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 3564 | * the head of the data block to @data and returns the length |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3565 | * of the block or 0 if the end of the skb data or the upper |
| 3566 | * offset has been reached. |
| 3567 | * |
| 3568 | * The caller is not required to consume all of the data |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 3569 | * returned, i.e. @consumed is typically set to the number |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3570 | * of bytes already consumed and the next call to |
| 3571 | * skb_seq_read() will return the remaining part of the block. |
| 3572 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3573 | * 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] | 3574 | * this limitation is the cost for zerocopy sequential |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3575 | * reads of potentially non linear data. |
| 3576 | * |
Randy Dunlap | bc2cda1 | 2008-02-13 15:03:25 -0800 | [diff] [blame] | 3577 | * Note 2: Fragment lists within fragments are not implemented |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3578 | * at the moment, state->root_skb could be replaced with |
| 3579 | * a stack for this purpose. |
| 3580 | */ |
| 3581 | unsigned int skb_seq_read(unsigned int consumed, const u8 **data, |
| 3582 | struct skb_seq_state *st) |
| 3583 | { |
| 3584 | unsigned int block_limit, abs_offset = consumed + st->lower_offset; |
| 3585 | skb_frag_t *frag; |
| 3586 | |
Wedson Almeida Filho | aeb193e | 2013-06-23 23:33:48 -0700 | [diff] [blame] | 3587 | if (unlikely(abs_offset >= st->upper_offset)) { |
| 3588 | if (st->frag_data) { |
| 3589 | kunmap_atomic(st->frag_data); |
| 3590 | st->frag_data = NULL; |
| 3591 | } |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3592 | return 0; |
Wedson Almeida Filho | aeb193e | 2013-06-23 23:33:48 -0700 | [diff] [blame] | 3593 | } |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3594 | |
| 3595 | next_skb: |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 3596 | block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3597 | |
Thomas Chenault | 995b337 | 2009-05-18 21:43:27 -0700 | [diff] [blame] | 3598 | if (abs_offset < block_limit && !st->frag_data) { |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 3599 | *data = st->cur_skb->data + (abs_offset - st->stepped_offset); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3600 | return block_limit - abs_offset; |
| 3601 | } |
| 3602 | |
| 3603 | if (st->frag_idx == 0 && !st->frag_data) |
| 3604 | st->stepped_offset += skb_headlen(st->cur_skb); |
| 3605 | |
| 3606 | while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { |
Willem de Bruijn | 97550f6 | 2021-01-09 17:18:33 -0500 | [diff] [blame] | 3607 | unsigned int pg_idx, pg_off, pg_sz; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3608 | |
Willem de Bruijn | 97550f6 | 2021-01-09 17:18:33 -0500 | [diff] [blame] | 3609 | frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; |
| 3610 | |
| 3611 | pg_idx = 0; |
| 3612 | pg_off = skb_frag_off(frag); |
| 3613 | pg_sz = skb_frag_size(frag); |
| 3614 | |
| 3615 | if (skb_frag_must_loop(skb_frag_page(frag))) { |
| 3616 | pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; |
| 3617 | pg_off = offset_in_page(pg_off + st->frag_off); |
| 3618 | pg_sz = min_t(unsigned int, pg_sz - st->frag_off, |
| 3619 | PAGE_SIZE - pg_off); |
| 3620 | } |
| 3621 | |
| 3622 | block_limit = pg_sz + st->stepped_offset; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3623 | if (abs_offset < block_limit) { |
| 3624 | if (!st->frag_data) |
Willem de Bruijn | 97550f6 | 2021-01-09 17:18:33 -0500 | [diff] [blame] | 3625 | st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3626 | |
Willem de Bruijn | 97550f6 | 2021-01-09 17:18:33 -0500 | [diff] [blame] | 3627 | *data = (u8 *)st->frag_data + pg_off + |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3628 | (abs_offset - st->stepped_offset); |
| 3629 | |
| 3630 | return block_limit - abs_offset; |
| 3631 | } |
| 3632 | |
| 3633 | if (st->frag_data) { |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 3634 | kunmap_atomic(st->frag_data); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3635 | st->frag_data = NULL; |
| 3636 | } |
| 3637 | |
Willem de Bruijn | 97550f6 | 2021-01-09 17:18:33 -0500 | [diff] [blame] | 3638 | st->stepped_offset += pg_sz; |
| 3639 | st->frag_off += pg_sz; |
| 3640 | if (st->frag_off == skb_frag_size(frag)) { |
| 3641 | st->frag_off = 0; |
| 3642 | st->frag_idx++; |
| 3643 | } |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3644 | } |
| 3645 | |
Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 3646 | if (st->frag_data) { |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 3647 | kunmap_atomic(st->frag_data); |
Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 3648 | st->frag_data = NULL; |
| 3649 | } |
| 3650 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3651 | 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] | 3652 | st->cur_skb = skb_shinfo(st->root_skb)->frag_list; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3653 | st->frag_idx = 0; |
| 3654 | goto next_skb; |
Shyam Iyer | 71b3346 | 2009-01-29 16:12:42 -0800 | [diff] [blame] | 3655 | } else if (st->cur_skb->next) { |
| 3656 | st->cur_skb = st->cur_skb->next; |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 3657 | st->frag_idx = 0; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3658 | goto next_skb; |
| 3659 | } |
| 3660 | |
| 3661 | return 0; |
| 3662 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3663 | EXPORT_SYMBOL(skb_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3664 | |
| 3665 | /** |
| 3666 | * skb_abort_seq_read - Abort a sequential read of skb data |
| 3667 | * @st: state variable |
| 3668 | * |
| 3669 | * Must be called if skb_seq_read() was not called until it |
| 3670 | * returned 0. |
| 3671 | */ |
| 3672 | void skb_abort_seq_read(struct skb_seq_state *st) |
| 3673 | { |
| 3674 | if (st->frag_data) |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 3675 | kunmap_atomic(st->frag_data); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3676 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3677 | EXPORT_SYMBOL(skb_abort_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 3678 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3679 | #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) |
| 3680 | |
| 3681 | static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, |
| 3682 | struct ts_config *conf, |
| 3683 | struct ts_state *state) |
| 3684 | { |
| 3685 | return skb_seq_read(offset, text, TS_SKB_CB(state)); |
| 3686 | } |
| 3687 | |
| 3688 | static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) |
| 3689 | { |
| 3690 | skb_abort_seq_read(TS_SKB_CB(state)); |
| 3691 | } |
| 3692 | |
| 3693 | /** |
| 3694 | * skb_find_text - Find a text pattern in skb data |
| 3695 | * @skb: the buffer to look in |
| 3696 | * @from: search offset |
| 3697 | * @to: search limit |
| 3698 | * @config: textsearch configuration |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3699 | * |
| 3700 | * Finds a pattern in the skb data according to the specified |
| 3701 | * textsearch configuration. Use textsearch_next() to retrieve |
| 3702 | * subsequent occurrences of the pattern. Returns the offset |
| 3703 | * to the first occurrence or UINT_MAX if no match was found. |
| 3704 | */ |
| 3705 | unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame] | 3706 | unsigned int to, struct ts_config *config) |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3707 | { |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame] | 3708 | struct ts_state state; |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 3709 | unsigned int ret; |
| 3710 | |
Willem de Bruijn | b228c9b | 2021-03-01 15:09:44 +0000 | [diff] [blame] | 3711 | BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); |
| 3712 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3713 | config->get_next_block = skb_ts_get_next_block; |
| 3714 | config->finish = skb_ts_finish; |
| 3715 | |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame] | 3716 | skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3717 | |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame] | 3718 | ret = textsearch_find(config, &state); |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 3719 | return (ret <= to - from ? ret : UINT_MAX); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3720 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3721 | EXPORT_SYMBOL(skb_find_text); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 3722 | |
Hannes Frederic Sowa | be12a1f | 2015-05-21 16:59:58 +0200 | [diff] [blame] | 3723 | int skb_append_pagefrags(struct sk_buff *skb, struct page *page, |
| 3724 | int offset, size_t size) |
| 3725 | { |
| 3726 | int i = skb_shinfo(skb)->nr_frags; |
| 3727 | |
| 3728 | if (skb_can_coalesce(skb, i, page, offset)) { |
| 3729 | skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); |
| 3730 | } else if (i < MAX_SKB_FRAGS) { |
| 3731 | get_page(page); |
| 3732 | skb_fill_page_desc(skb, i, page, offset, size); |
| 3733 | } else { |
| 3734 | return -EMSGSIZE; |
| 3735 | } |
| 3736 | |
| 3737 | return 0; |
| 3738 | } |
| 3739 | EXPORT_SYMBOL_GPL(skb_append_pagefrags); |
| 3740 | |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3741 | /** |
| 3742 | * skb_pull_rcsum - pull skb and update receive checksum |
| 3743 | * @skb: buffer to update |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3744 | * @len: length of data pulled |
| 3745 | * |
| 3746 | * This function performs an skb_pull on the packet and updates |
Urs Thuermann | fee54fa | 2008-02-12 22:03:25 -0800 | [diff] [blame] | 3747 | * the CHECKSUM_COMPLETE checksum. It should be used on |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 3748 | * receive path processing instead of skb_pull unless you know |
| 3749 | * that the checksum difference is zero (e.g., a valid IP header) |
| 3750 | * or you are setting ip_summed to CHECKSUM_NONE. |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3751 | */ |
Johannes Berg | af72868 | 2017-06-16 14:29:22 +0200 | [diff] [blame] | 3752 | void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3753 | { |
Pravin B Shelar | 31b33df | 2015-09-28 17:24:25 -0700 | [diff] [blame] | 3754 | unsigned char *data = skb->data; |
| 3755 | |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3756 | BUG_ON(len > skb->len); |
Pravin B Shelar | 31b33df | 2015-09-28 17:24:25 -0700 | [diff] [blame] | 3757 | __skb_pull(skb, len); |
| 3758 | skb_postpull_rcsum(skb, data, len); |
| 3759 | return skb->data; |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 3760 | } |
Arnaldo Carvalho de Melo | f94691a | 2006-03-20 22:47:55 -0800 | [diff] [blame] | 3761 | EXPORT_SYMBOL_GPL(skb_pull_rcsum); |
| 3762 | |
Yonghong Song | 13acc94 | 2018-03-21 16:31:03 -0700 | [diff] [blame] | 3763 | static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) |
| 3764 | { |
| 3765 | skb_frag_t head_frag; |
| 3766 | struct page *page; |
| 3767 | |
| 3768 | page = virt_to_head_page(frag_skb->head); |
Matthew Wilcox (Oracle) | d8e18a5 | 2019-07-22 20:08:26 -0700 | [diff] [blame] | 3769 | __skb_frag_set_page(&head_frag, page); |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 3770 | skb_frag_off_set(&head_frag, frag_skb->data - |
| 3771 | (unsigned char *)page_address(page)); |
Matthew Wilcox (Oracle) | d8e18a5 | 2019-07-22 20:08:26 -0700 | [diff] [blame] | 3772 | skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); |
Yonghong Song | 13acc94 | 2018-03-21 16:31:03 -0700 | [diff] [blame] | 3773 | return head_frag; |
| 3774 | } |
| 3775 | |
Steffen Klassert | 3a1296a | 2020-01-25 11:26:44 +0100 | [diff] [blame] | 3776 | struct sk_buff *skb_segment_list(struct sk_buff *skb, |
| 3777 | netdev_features_t features, |
| 3778 | unsigned int offset) |
| 3779 | { |
| 3780 | struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; |
| 3781 | unsigned int tnl_hlen = skb_tnl_header_len(skb); |
| 3782 | unsigned int delta_truesize = 0; |
| 3783 | unsigned int delta_len = 0; |
| 3784 | struct sk_buff *tail = NULL; |
Dongseok Yi | 53475c5 | 2021-01-08 11:28:38 +0900 | [diff] [blame] | 3785 | struct sk_buff *nskb, *tmp; |
| 3786 | int err; |
Steffen Klassert | 3a1296a | 2020-01-25 11:26:44 +0100 | [diff] [blame] | 3787 | |
| 3788 | skb_push(skb, -skb_network_offset(skb) + offset); |
| 3789 | |
| 3790 | skb_shinfo(skb)->frag_list = NULL; |
| 3791 | |
| 3792 | do { |
| 3793 | nskb = list_skb; |
| 3794 | list_skb = list_skb->next; |
| 3795 | |
Dongseok Yi | 53475c5 | 2021-01-08 11:28:38 +0900 | [diff] [blame] | 3796 | err = 0; |
| 3797 | if (skb_shared(nskb)) { |
| 3798 | tmp = skb_clone(nskb, GFP_ATOMIC); |
| 3799 | if (tmp) { |
| 3800 | consume_skb(nskb); |
| 3801 | nskb = tmp; |
| 3802 | err = skb_unclone(nskb, GFP_ATOMIC); |
| 3803 | } else { |
| 3804 | err = -ENOMEM; |
| 3805 | } |
| 3806 | } |
| 3807 | |
Steffen Klassert | 3a1296a | 2020-01-25 11:26:44 +0100 | [diff] [blame] | 3808 | if (!tail) |
| 3809 | skb->next = nskb; |
| 3810 | else |
| 3811 | tail->next = nskb; |
| 3812 | |
Dongseok Yi | 53475c5 | 2021-01-08 11:28:38 +0900 | [diff] [blame] | 3813 | if (unlikely(err)) { |
| 3814 | nskb->next = list_skb; |
| 3815 | goto err_linearize; |
| 3816 | } |
| 3817 | |
Steffen Klassert | 3a1296a | 2020-01-25 11:26:44 +0100 | [diff] [blame] | 3818 | tail = nskb; |
| 3819 | |
| 3820 | delta_len += nskb->len; |
| 3821 | delta_truesize += nskb->truesize; |
| 3822 | |
| 3823 | skb_push(nskb, -skb_network_offset(nskb) + offset); |
| 3824 | |
Florian Westphal | cf673ed | 2020-03-30 18:51:29 +0200 | [diff] [blame] | 3825 | skb_release_head_state(nskb); |
Steffen Klassert | 3a1296a | 2020-01-25 11:26:44 +0100 | [diff] [blame] | 3826 | __copy_skb_header(nskb, skb); |
| 3827 | |
| 3828 | skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); |
| 3829 | skb_copy_from_linear_data_offset(skb, -tnl_hlen, |
| 3830 | nskb->data - tnl_hlen, |
| 3831 | offset + tnl_hlen); |
| 3832 | |
| 3833 | if (skb_needs_linearize(nskb, features) && |
| 3834 | __skb_linearize(nskb)) |
| 3835 | goto err_linearize; |
| 3836 | |
| 3837 | } while (list_skb); |
| 3838 | |
| 3839 | skb->truesize = skb->truesize - delta_truesize; |
| 3840 | skb->data_len = skb->data_len - delta_len; |
| 3841 | skb->len = skb->len - delta_len; |
| 3842 | |
| 3843 | skb_gso_reset(skb); |
| 3844 | |
| 3845 | skb->prev = tail; |
| 3846 | |
| 3847 | if (skb_needs_linearize(skb, features) && |
| 3848 | __skb_linearize(skb)) |
| 3849 | goto err_linearize; |
| 3850 | |
| 3851 | skb_get(skb); |
| 3852 | |
| 3853 | return skb; |
| 3854 | |
| 3855 | err_linearize: |
| 3856 | kfree_skb_list(skb->next); |
| 3857 | skb->next = NULL; |
| 3858 | return ERR_PTR(-ENOMEM); |
| 3859 | } |
| 3860 | EXPORT_SYMBOL_GPL(skb_segment_list); |
| 3861 | |
| 3862 | int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb) |
| 3863 | { |
| 3864 | if (unlikely(p->len + skb->len >= 65536)) |
| 3865 | return -E2BIG; |
| 3866 | |
| 3867 | if (NAPI_GRO_CB(p)->last == p) |
| 3868 | skb_shinfo(p)->frag_list = skb; |
| 3869 | else |
| 3870 | NAPI_GRO_CB(p)->last->next = skb; |
| 3871 | |
| 3872 | skb_pull(skb, skb_gro_offset(skb)); |
| 3873 | |
| 3874 | NAPI_GRO_CB(p)->last = skb; |
| 3875 | NAPI_GRO_CB(p)->count++; |
| 3876 | p->data_len += skb->len; |
| 3877 | p->truesize += skb->truesize; |
| 3878 | p->len += skb->len; |
| 3879 | |
| 3880 | NAPI_GRO_CB(skb)->same_flow = 1; |
| 3881 | |
| 3882 | return 0; |
| 3883 | } |
Steffen Klassert | 3a1296a | 2020-01-25 11:26:44 +0100 | [diff] [blame] | 3884 | |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3885 | /** |
| 3886 | * skb_segment - Perform protocol segmentation on skb. |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3887 | * @head_skb: buffer to segment |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 3888 | * @features: features for the output path (see dev->features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3889 | * |
| 3890 | * This function performs segmentation on the given skb. It returns |
Ben Hutchings | 4c821d7 | 2008-04-13 21:52:48 -0700 | [diff] [blame] | 3891 | * a pointer to the first in a list of new skbs for the segments. |
| 3892 | * In case of error it returns ERR_PTR(err). |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3893 | */ |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3894 | struct sk_buff *skb_segment(struct sk_buff *head_skb, |
| 3895 | netdev_features_t features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3896 | { |
| 3897 | struct sk_buff *segs = NULL; |
| 3898 | struct sk_buff *tail = NULL; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3899 | struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3900 | skb_frag_t *frag = skb_shinfo(head_skb)->frags; |
| 3901 | unsigned int mss = skb_shinfo(head_skb)->gso_size; |
| 3902 | unsigned int doffset = head_skb->data - skb_mac_header(head_skb); |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 3903 | struct sk_buff *frag_skb = head_skb; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3904 | unsigned int offset = doffset; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3905 | unsigned int tnl_hlen = skb_tnl_header_len(head_skb); |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3906 | unsigned int partial_segs = 0; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3907 | unsigned int headroom; |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3908 | unsigned int len = head_skb->len; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3909 | __be16 proto; |
Alexander Duyck | 36c9838 | 2016-05-02 09:38:18 -0700 | [diff] [blame] | 3910 | bool csum, sg; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3911 | int nfrags = skb_shinfo(head_skb)->nr_frags; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3912 | int err = -ENOMEM; |
| 3913 | int i = 0; |
| 3914 | int pos; |
| 3915 | |
Shmulik Ladkani | 3dcbdb1 | 2019-09-06 12:23:50 +0300 | [diff] [blame] | 3916 | if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) && |
| 3917 | (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) { |
| 3918 | /* gso_size is untrusted, and we have a frag_list with a linear |
| 3919 | * non head_frag head. |
| 3920 | * |
| 3921 | * (we assume checking the first list_skb member suffices; |
| 3922 | * i.e if either of the list_skb members have non head_frag |
| 3923 | * head, then the first one has too). |
| 3924 | * |
| 3925 | * If head_skb's headlen does not fit requested gso_size, it |
| 3926 | * means that the frag_list members do NOT terminate on exact |
| 3927 | * gso_size boundaries. Hence we cannot perform skb_frag_t page |
| 3928 | * sharing. Therefore we must fallback to copying the frag_list |
| 3929 | * skbs; we do so by disabling SG. |
| 3930 | */ |
| 3931 | if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) |
| 3932 | features &= ~NETIF_F_SG; |
| 3933 | } |
| 3934 | |
Wei-Chun Chao | 5882a07 | 2014-06-08 23:48:54 -0700 | [diff] [blame] | 3935 | __skb_push(head_skb, doffset); |
Miaohe Lin | 2f63113 | 2020-08-01 17:36:05 +0800 | [diff] [blame] | 3936 | proto = skb_network_protocol(head_skb, NULL); |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3937 | if (unlikely(!proto)) |
| 3938 | return ERR_PTR(-EINVAL); |
| 3939 | |
Alexander Duyck | 36c9838 | 2016-05-02 09:38:18 -0700 | [diff] [blame] | 3940 | sg = !!(features & NETIF_F_SG); |
Alexander Duyck | f245d07 | 2016-02-05 15:28:26 -0800 | [diff] [blame] | 3941 | csum = !!can_checksum_protocol(features, proto); |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 3942 | |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3943 | if (sg && csum && (mss != GSO_BY_FRAGS)) { |
| 3944 | if (!(features & NETIF_F_GSO_PARTIAL)) { |
| 3945 | struct sk_buff *iter; |
Ilan Tayari | 43170c4 | 2017-04-19 21:26:07 +0300 | [diff] [blame] | 3946 | unsigned int frag_len; |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3947 | |
| 3948 | if (!list_skb || |
| 3949 | !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) |
| 3950 | goto normal; |
| 3951 | |
Ilan Tayari | 43170c4 | 2017-04-19 21:26:07 +0300 | [diff] [blame] | 3952 | /* If we get here then all the required |
| 3953 | * GSO features except frag_list are supported. |
| 3954 | * Try to split the SKB to multiple GSO SKBs |
| 3955 | * with no frag_list. |
| 3956 | * Currently we can do that only when the buffers don't |
| 3957 | * have a linear part and all the buffers except |
| 3958 | * the last are of the same length. |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3959 | */ |
Ilan Tayari | 43170c4 | 2017-04-19 21:26:07 +0300 | [diff] [blame] | 3960 | frag_len = list_skb->len; |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3961 | skb_walk_frags(head_skb, iter) { |
Ilan Tayari | 43170c4 | 2017-04-19 21:26:07 +0300 | [diff] [blame] | 3962 | if (frag_len != iter->len && iter->next) |
| 3963 | goto normal; |
Ilan Tayari | eaffadb | 2017-04-08 02:07:08 +0300 | [diff] [blame] | 3964 | if (skb_headlen(iter) && !iter->head_frag) |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3965 | goto normal; |
| 3966 | |
| 3967 | len -= iter->len; |
| 3968 | } |
Ilan Tayari | 43170c4 | 2017-04-19 21:26:07 +0300 | [diff] [blame] | 3969 | |
| 3970 | if (len != frag_len) |
| 3971 | goto normal; |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | /* GSO partial only requires that we trim off any excess that |
| 3975 | * doesn't fit into an MSS sized block, so take care of that |
| 3976 | * now. |
| 3977 | */ |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3978 | partial_segs = len / mss; |
Alexander Duyck | d7fb5a8 | 2016-05-02 09:38:12 -0700 | [diff] [blame] | 3979 | if (partial_segs > 1) |
| 3980 | mss *= partial_segs; |
| 3981 | else |
| 3982 | partial_segs = 0; |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 3983 | } |
| 3984 | |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 3985 | normal: |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3986 | headroom = skb_headroom(head_skb); |
| 3987 | pos = skb_headlen(head_skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3988 | |
| 3989 | do { |
| 3990 | struct sk_buff *nskb; |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3991 | skb_frag_t *nskb_frag; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 3992 | int hsize; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3993 | int size; |
| 3994 | |
Marcelo Ricardo Leitner | 3953c46 | 2016-06-02 15:05:40 -0300 | [diff] [blame] | 3995 | if (unlikely(mss == GSO_BY_FRAGS)) { |
| 3996 | len = list_skb->len; |
| 3997 | } else { |
| 3998 | len = head_skb->len - offset; |
| 3999 | if (len > mss) |
| 4000 | len = mss; |
| 4001 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4002 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 4003 | hsize = skb_headlen(head_skb) - offset; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4004 | |
Xin Long | dbd50f2 | 2021-01-15 17:36:38 +0800 | [diff] [blame] | 4005 | if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 4006 | (skb_headlen(list_skb) == len || sg)) { |
| 4007 | BUG_ON(skb_headlen(list_skb) > len); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 4008 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4009 | i = 0; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 4010 | nfrags = skb_shinfo(list_skb)->nr_frags; |
| 4011 | frag = skb_shinfo(list_skb)->frags; |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 4012 | frag_skb = list_skb; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 4013 | pos += skb_headlen(list_skb); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4014 | |
| 4015 | while (pos < offset + len) { |
| 4016 | BUG_ON(i >= nfrags); |
| 4017 | |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 4018 | size = skb_frag_size(frag); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4019 | if (pos + size > offset + len) |
| 4020 | break; |
| 4021 | |
| 4022 | i++; |
| 4023 | pos += size; |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 4024 | frag++; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4025 | } |
| 4026 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 4027 | nskb = skb_clone(list_skb, GFP_ATOMIC); |
| 4028 | list_skb = list_skb->next; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 4029 | |
| 4030 | if (unlikely(!nskb)) |
| 4031 | goto err; |
| 4032 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4033 | if (unlikely(pskb_trim(nskb, len))) { |
| 4034 | kfree_skb(nskb); |
| 4035 | goto err; |
| 4036 | } |
| 4037 | |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 4038 | hsize = skb_end_offset(nskb); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 4039 | if (skb_cow_head(nskb, doffset + headroom)) { |
| 4040 | kfree_skb(nskb); |
| 4041 | goto err; |
| 4042 | } |
| 4043 | |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 4044 | nskb->truesize += skb_end_offset(nskb) - hsize; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 4045 | skb_release_head_state(nskb); |
| 4046 | __skb_push(nskb, doffset); |
| 4047 | } else { |
Paolo Abeni | 00b229f | 2021-01-19 17:56:56 +0100 | [diff] [blame] | 4048 | if (hsize < 0) |
| 4049 | hsize = 0; |
Xin Long | dbd50f2 | 2021-01-15 17:36:38 +0800 | [diff] [blame] | 4050 | if (hsize > len || !sg) |
| 4051 | hsize = len; |
Xin Long | dbd50f2 | 2021-01-15 17:36:38 +0800 | [diff] [blame] | 4052 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 4053 | nskb = __alloc_skb(hsize + doffset + headroom, |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 4054 | GFP_ATOMIC, skb_alloc_rx_flag(head_skb), |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 4055 | NUMA_NO_NODE); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 4056 | |
| 4057 | if (unlikely(!nskb)) |
| 4058 | goto err; |
| 4059 | |
| 4060 | skb_reserve(nskb, headroom); |
| 4061 | __skb_put(nskb, doffset); |
| 4062 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4063 | |
| 4064 | if (segs) |
| 4065 | tail->next = nskb; |
| 4066 | else |
| 4067 | segs = nskb; |
| 4068 | tail = nskb; |
| 4069 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 4070 | __copy_skb_header(nskb, head_skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4071 | |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 4072 | skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); |
Vlad Yasevich | fcdfe3a | 2014-07-31 10:33:06 -0400 | [diff] [blame] | 4073 | skb_reset_mac_len(nskb); |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 4074 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 4075 | skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 4076 | nskb->data - tnl_hlen, |
| 4077 | doffset + tnl_hlen); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 4078 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4079 | if (nskb->len == len + doffset) |
Simon Horman | 1cdbcb7 | 2013-05-19 15:46:49 +0000 | [diff] [blame] | 4080 | goto perform_csum_check; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 4081 | |
Alexander Duyck | 7fbeffe | 2016-02-05 15:27:43 -0800 | [diff] [blame] | 4082 | if (!sg) { |
Yadu Kishore | 1454c9f | 2020-03-17 14:08:38 +0530 | [diff] [blame] | 4083 | if (!csum) { |
| 4084 | if (!nskb->remcsum_offload) |
| 4085 | nskb->ip_summed = CHECKSUM_NONE; |
| 4086 | SKB_GSO_CB(nskb)->csum = |
| 4087 | skb_copy_and_csum_bits(head_skb, offset, |
| 4088 | skb_put(nskb, |
| 4089 | len), |
Al Viro | 8d5930d | 2020-07-10 20:07:10 -0400 | [diff] [blame] | 4090 | len); |
Yadu Kishore | 1454c9f | 2020-03-17 14:08:38 +0530 | [diff] [blame] | 4091 | SKB_GSO_CB(nskb)->csum_start = |
| 4092 | skb_headroom(nskb) + doffset; |
| 4093 | } else { |
| 4094 | skb_copy_bits(head_skb, offset, |
| 4095 | skb_put(nskb, len), |
| 4096 | len); |
| 4097 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4098 | continue; |
| 4099 | } |
| 4100 | |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 4101 | nskb_frag = skb_shinfo(nskb)->frags; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4102 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 4103 | skb_copy_from_linear_data_offset(head_skb, offset, |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4104 | skb_put(nskb, hsize), hsize); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4105 | |
Jonathan Lemon | 06b4feb | 2021-01-06 14:18:38 -0800 | [diff] [blame] | 4106 | skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & |
| 4107 | SKBFL_SHARED_FRAG; |
Eric Dumazet | cef401d | 2013-01-25 20:34:37 +0000 | [diff] [blame] | 4108 | |
Willem de Bruijn | bf5c25d | 2017-12-22 19:00:17 -0500 | [diff] [blame] | 4109 | if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || |
| 4110 | skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) |
| 4111 | goto err; |
| 4112 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4113 | while (pos < offset + len) { |
| 4114 | if (i >= nfrags) { |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4115 | i = 0; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 4116 | nfrags = skb_shinfo(list_skb)->nr_frags; |
| 4117 | frag = skb_shinfo(list_skb)->frags; |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 4118 | frag_skb = list_skb; |
Yonghong Song | 13acc94 | 2018-03-21 16:31:03 -0700 | [diff] [blame] | 4119 | if (!skb_headlen(list_skb)) { |
| 4120 | BUG_ON(!nfrags); |
| 4121 | } else { |
| 4122 | BUG_ON(!list_skb->head_frag); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4123 | |
Yonghong Song | 13acc94 | 2018-03-21 16:31:03 -0700 | [diff] [blame] | 4124 | /* to make room for head_frag. */ |
| 4125 | i--; |
| 4126 | frag--; |
| 4127 | } |
Willem de Bruijn | bf5c25d | 2017-12-22 19:00:17 -0500 | [diff] [blame] | 4128 | if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || |
| 4129 | skb_zerocopy_clone(nskb, frag_skb, |
| 4130 | GFP_ATOMIC)) |
| 4131 | goto err; |
| 4132 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 4133 | list_skb = list_skb->next; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | if (unlikely(skb_shinfo(nskb)->nr_frags >= |
| 4137 | MAX_SKB_FRAGS)) { |
| 4138 | net_warn_ratelimited( |
| 4139 | "skb_segment: too many frags: %u %u\n", |
| 4140 | pos, mss); |
Eric Dumazet | ff907a1 | 2018-07-19 16:04:38 -0700 | [diff] [blame] | 4141 | err = -EINVAL; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 4142 | goto err; |
| 4143 | } |
| 4144 | |
Yonghong Song | 13acc94 | 2018-03-21 16:31:03 -0700 | [diff] [blame] | 4145 | *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] | 4146 | __skb_frag_ref(nskb_frag); |
| 4147 | size = skb_frag_size(nskb_frag); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4148 | |
| 4149 | if (pos < offset) { |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 4150 | skb_frag_off_add(nskb_frag, offset - pos); |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 4151 | skb_frag_size_sub(nskb_frag, offset - pos); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4152 | } |
| 4153 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 4154 | skb_shinfo(nskb)->nr_frags++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4155 | |
| 4156 | if (pos + size <= offset + len) { |
| 4157 | i++; |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 4158 | frag++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4159 | pos += size; |
| 4160 | } else { |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 4161 | skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 4162 | goto skip_fraglist; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4163 | } |
| 4164 | |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 4165 | nskb_frag++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4166 | } |
| 4167 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 4168 | skip_fraglist: |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4169 | nskb->data_len = len - hsize; |
| 4170 | nskb->len += nskb->data_len; |
| 4171 | nskb->truesize += nskb->data_len; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 4172 | |
Simon Horman | 1cdbcb7 | 2013-05-19 15:46:49 +0000 | [diff] [blame] | 4173 | perform_csum_check: |
Alexander Duyck | 7fbeffe | 2016-02-05 15:27:43 -0800 | [diff] [blame] | 4174 | if (!csum) { |
Eric Dumazet | ff907a1 | 2018-07-19 16:04:38 -0700 | [diff] [blame] | 4175 | if (skb_has_shared_frag(nskb) && |
| 4176 | __skb_linearize(nskb)) |
| 4177 | goto err; |
| 4178 | |
Alexander Duyck | 7fbeffe | 2016-02-05 15:27:43 -0800 | [diff] [blame] | 4179 | if (!nskb->remcsum_offload) |
| 4180 | nskb->ip_summed = CHECKSUM_NONE; |
Alexander Duyck | 7644345 | 2016-02-05 15:27:37 -0800 | [diff] [blame] | 4181 | SKB_GSO_CB(nskb)->csum = |
| 4182 | skb_checksum(nskb, doffset, |
| 4183 | nskb->len - doffset, 0); |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 4184 | SKB_GSO_CB(nskb)->csum_start = |
Alexander Duyck | 7644345 | 2016-02-05 15:27:37 -0800 | [diff] [blame] | 4185 | skb_headroom(nskb) + doffset; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 4186 | } |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 4187 | } while ((offset += len) < head_skb->len); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4188 | |
Eric Dumazet | bec3cfd | 2014-10-03 20:59:19 -0700 | [diff] [blame] | 4189 | /* Some callers want to get the end of the list. |
| 4190 | * Put it in segs->prev to avoid walking the list. |
| 4191 | * (see validate_xmit_skb_list() for example) |
| 4192 | */ |
| 4193 | segs->prev = tail; |
Toshiaki Makita | 432c856 | 2014-10-27 10:30:51 -0700 | [diff] [blame] | 4194 | |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 4195 | if (partial_segs) { |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 4196 | struct sk_buff *iter; |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 4197 | int type = skb_shinfo(head_skb)->gso_type; |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 4198 | unsigned short gso_size = skb_shinfo(head_skb)->gso_size; |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 4199 | |
| 4200 | /* Update type to add partial and then remove dodgy if set */ |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 4201 | 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] | 4202 | type &= ~SKB_GSO_DODGY; |
| 4203 | |
| 4204 | /* Update GSO info and prepare to start updating headers on |
| 4205 | * our way back down the stack of protocols. |
| 4206 | */ |
Steffen Klassert | 07b26c9 | 2016-09-19 12:58:47 +0200 | [diff] [blame] | 4207 | for (iter = segs; iter; iter = iter->next) { |
| 4208 | skb_shinfo(iter)->gso_size = gso_size; |
| 4209 | skb_shinfo(iter)->gso_segs = partial_segs; |
| 4210 | skb_shinfo(iter)->gso_type = type; |
| 4211 | SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; |
| 4212 | } |
| 4213 | |
| 4214 | if (tail->len - doffset <= gso_size) |
| 4215 | skb_shinfo(tail)->gso_size = 0; |
| 4216 | else if (tail != segs) |
| 4217 | 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] | 4218 | } |
| 4219 | |
Toshiaki Makita | 432c856 | 2014-10-27 10:30:51 -0700 | [diff] [blame] | 4220 | /* Following permits correct backpressure, for protocols |
| 4221 | * using skb_set_owner_w(). |
| 4222 | * Idea is to tranfert ownership from head_skb to last segment. |
| 4223 | */ |
| 4224 | if (head_skb->destructor == sock_wfree) { |
| 4225 | swap(tail->truesize, head_skb->truesize); |
| 4226 | swap(tail->destructor, head_skb->destructor); |
| 4227 | swap(tail->sk, head_skb->sk); |
| 4228 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4229 | return segs; |
| 4230 | |
| 4231 | err: |
Eric Dumazet | 289dccb | 2013-12-20 14:29:08 -0800 | [diff] [blame] | 4232 | kfree_skb_list(segs); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4233 | return ERR_PTR(err); |
| 4234 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 4235 | EXPORT_SYMBOL_GPL(skb_segment); |
| 4236 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 4237 | int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb) |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4238 | { |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4239 | struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 4240 | unsigned int offset = skb_gro_offset(skb); |
| 4241 | unsigned int headlen = skb_headlen(skb); |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4242 | unsigned int len = skb_gro_len(skb); |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4243 | unsigned int delta_truesize; |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 4244 | struct sk_buff *lp; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4245 | |
Steffen Klassert | 0ab03f3 | 2019-04-02 08:16:03 +0200 | [diff] [blame] | 4246 | if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush)) |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4247 | return -E2BIG; |
| 4248 | |
Eric Dumazet | 29e9824 | 2014-05-16 11:34:37 -0700 | [diff] [blame] | 4249 | lp = NAPI_GRO_CB(p)->last; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4250 | pinfo = skb_shinfo(lp); |
| 4251 | |
| 4252 | if (headlen <= offset) { |
Herbert Xu | 42da699 | 2009-05-26 18:50:19 +0000 | [diff] [blame] | 4253 | skb_frag_t *frag; |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 4254 | skb_frag_t *frag2; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 4255 | int i = skbinfo->nr_frags; |
| 4256 | int nr_frags = pinfo->nr_frags + i; |
Herbert Xu | 42da699 | 2009-05-26 18:50:19 +0000 | [diff] [blame] | 4257 | |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 4258 | if (nr_frags > MAX_SKB_FRAGS) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4259 | goto merge; |
Herbert Xu | 81705ad | 2009-01-29 14:19:51 +0000 | [diff] [blame] | 4260 | |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4261 | offset -= headlen; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 4262 | pinfo->nr_frags = nr_frags; |
| 4263 | skbinfo->nr_frags = 0; |
Herbert Xu | f557206 | 2009-01-14 20:40:03 -0800 | [diff] [blame] | 4264 | |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 4265 | frag = pinfo->frags + nr_frags; |
| 4266 | frag2 = skbinfo->frags + i; |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 4267 | do { |
| 4268 | *--frag = *--frag2; |
| 4269 | } while (--i); |
| 4270 | |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 4271 | skb_frag_off_add(frag, offset); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 4272 | skb_frag_size_sub(frag, offset); |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 4273 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4274 | /* all fragments truesize : remove (head size + sk_buff) */ |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 4275 | delta_truesize = skb->truesize - |
| 4276 | SKB_TRUESIZE(skb_end_offset(skb)); |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4277 | |
Herbert Xu | f557206 | 2009-01-14 20:40:03 -0800 | [diff] [blame] | 4278 | skb->truesize -= skb->data_len; |
| 4279 | skb->len -= skb->data_len; |
| 4280 | skb->data_len = 0; |
| 4281 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4282 | NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 4283 | goto done; |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 4284 | } else if (skb->head_frag) { |
| 4285 | int nr_frags = pinfo->nr_frags; |
| 4286 | skb_frag_t *frag = pinfo->frags + nr_frags; |
| 4287 | struct page *page = virt_to_head_page(skb->head); |
| 4288 | unsigned int first_size = headlen - offset; |
| 4289 | unsigned int first_offset; |
| 4290 | |
| 4291 | if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4292 | goto merge; |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 4293 | |
| 4294 | first_offset = skb->data - |
| 4295 | (unsigned char *)page_address(page) + |
| 4296 | offset; |
| 4297 | |
| 4298 | pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; |
| 4299 | |
Matthew Wilcox (Oracle) | d8e18a5 | 2019-07-22 20:08:26 -0700 | [diff] [blame] | 4300 | __skb_frag_set_page(frag, page); |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 4301 | skb_frag_off_set(frag, first_offset); |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 4302 | skb_frag_size_set(frag, first_size); |
| 4303 | |
| 4304 | memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); |
| 4305 | /* We dont need to clear skbinfo->nr_frags here */ |
| 4306 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4307 | delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 4308 | NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; |
| 4309 | goto done; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4310 | } |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4311 | |
| 4312 | merge: |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4313 | delta_truesize = skb->truesize; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 4314 | if (offset > headlen) { |
Michal Schmidt | d1dc7ab | 2011-01-24 12:08:48 +0000 | [diff] [blame] | 4315 | unsigned int eat = offset - headlen; |
| 4316 | |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 4317 | skb_frag_off_add(&skbinfo->frags[0], eat); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 4318 | skb_frag_size_sub(&skbinfo->frags[0], eat); |
Michal Schmidt | d1dc7ab | 2011-01-24 12:08:48 +0000 | [diff] [blame] | 4319 | skb->data_len -= eat; |
| 4320 | skb->len -= eat; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 4321 | offset = headlen; |
Herbert Xu | 5603502 | 2009-02-05 21:26:52 -0800 | [diff] [blame] | 4322 | } |
| 4323 | |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 4324 | __skb_pull(skb, offset); |
Herbert Xu | 5603502 | 2009-02-05 21:26:52 -0800 | [diff] [blame] | 4325 | |
Eric Dumazet | 29e9824 | 2014-05-16 11:34:37 -0700 | [diff] [blame] | 4326 | if (NAPI_GRO_CB(p)->last == p) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4327 | skb_shinfo(p)->frag_list = skb; |
| 4328 | else |
| 4329 | NAPI_GRO_CB(p)->last->next = skb; |
Eric Dumazet | c3c7c25 | 2012-12-06 13:54:59 +0000 | [diff] [blame] | 4330 | NAPI_GRO_CB(p)->last = skb; |
Eric Dumazet | f4a775d | 2014-09-22 16:29:32 -0700 | [diff] [blame] | 4331 | __skb_header_release(skb); |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4332 | lp = p; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4333 | |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 4334 | done: |
| 4335 | NAPI_GRO_CB(p)->count++; |
Herbert Xu | 37fe473 | 2009-01-17 19:48:13 +0000 | [diff] [blame] | 4336 | p->data_len += len; |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 4337 | p->truesize += delta_truesize; |
Herbert Xu | 37fe473 | 2009-01-17 19:48:13 +0000 | [diff] [blame] | 4338 | p->len += len; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 4339 | if (lp != p) { |
| 4340 | lp->data_len += len; |
| 4341 | lp->truesize += delta_truesize; |
| 4342 | lp->len += len; |
| 4343 | } |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4344 | NAPI_GRO_CB(skb)->same_flow = 1; |
| 4345 | return 0; |
| 4346 | } |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 4347 | |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 4348 | #ifdef CONFIG_SKB_EXTENSIONS |
| 4349 | #define SKB_EXT_ALIGN_VALUE 8 |
| 4350 | #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) |
| 4351 | |
| 4352 | static const u8 skb_ext_type_len[] = { |
| 4353 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
| 4354 | [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), |
| 4355 | #endif |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 4356 | #ifdef CONFIG_XFRM |
| 4357 | [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), |
| 4358 | #endif |
Paul Blakey | 95a7233 | 2019-09-04 16:56:37 +0300 | [diff] [blame] | 4359 | #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) |
| 4360 | [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), |
| 4361 | #endif |
Mat Martineau | 3ee17bc | 2020-01-09 07:59:19 -0800 | [diff] [blame] | 4362 | #if IS_ENABLED(CONFIG_MPTCP) |
| 4363 | [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), |
| 4364 | #endif |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 4365 | }; |
| 4366 | |
| 4367 | static __always_inline unsigned int skb_ext_total_length(void) |
| 4368 | { |
| 4369 | return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + |
| 4370 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
| 4371 | skb_ext_type_len[SKB_EXT_BRIDGE_NF] + |
| 4372 | #endif |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 4373 | #ifdef CONFIG_XFRM |
| 4374 | skb_ext_type_len[SKB_EXT_SEC_PATH] + |
| 4375 | #endif |
Paul Blakey | 95a7233 | 2019-09-04 16:56:37 +0300 | [diff] [blame] | 4376 | #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) |
| 4377 | skb_ext_type_len[TC_SKB_EXT] + |
| 4378 | #endif |
Mat Martineau | 3ee17bc | 2020-01-09 07:59:19 -0800 | [diff] [blame] | 4379 | #if IS_ENABLED(CONFIG_MPTCP) |
| 4380 | skb_ext_type_len[SKB_EXT_MPTCP] + |
| 4381 | #endif |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 4382 | 0; |
| 4383 | } |
| 4384 | |
| 4385 | static void skb_extensions_init(void) |
| 4386 | { |
| 4387 | BUILD_BUG_ON(SKB_EXT_NUM >= 8); |
| 4388 | BUILD_BUG_ON(skb_ext_total_length() > 255); |
| 4389 | |
| 4390 | skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", |
| 4391 | SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), |
| 4392 | 0, |
| 4393 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
| 4394 | NULL); |
| 4395 | } |
| 4396 | #else |
| 4397 | static void skb_extensions_init(void) {} |
| 4398 | #endif |
| 4399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | void __init skb_init(void) |
| 4401 | { |
Kees Cook | 79a8a64 | 2018-02-07 17:44:38 -0800 | [diff] [blame] | 4402 | skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | sizeof(struct sk_buff), |
| 4404 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 4405 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Kees Cook | 79a8a64 | 2018-02-07 17:44:38 -0800 | [diff] [blame] | 4406 | offsetof(struct sk_buff, cb), |
| 4407 | sizeof_field(struct sk_buff, cb), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4408 | NULL); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 4409 | skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 4410 | sizeof(struct sk_buff_fclones), |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 4411 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 4412 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4413 | NULL); |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 4414 | skb_extensions_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | } |
| 4416 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 4417 | static int |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4418 | __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, |
| 4419 | unsigned int recursion_level) |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4420 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 4421 | int start = skb_headlen(skb); |
| 4422 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4423 | struct sk_buff *frag_iter; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4424 | int elt = 0; |
| 4425 | |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4426 | if (unlikely(recursion_level >= 24)) |
| 4427 | return -EMSGSIZE; |
| 4428 | |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4429 | if (copy > 0) { |
| 4430 | if (copy > len) |
| 4431 | copy = len; |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 4432 | sg_set_buf(sg, skb->data + offset, copy); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4433 | elt++; |
| 4434 | if ((len -= copy) == 0) |
| 4435 | return elt; |
| 4436 | offset += copy; |
| 4437 | } |
| 4438 | |
| 4439 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 4440 | int end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4441 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 4442 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 4443 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 4444 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4445 | if ((copy = end - offset) > 0) { |
| 4446 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4447 | if (unlikely(elt && sg_is_last(&sg[elt - 1]))) |
| 4448 | return -EMSGSIZE; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4449 | |
| 4450 | if (copy > len) |
| 4451 | copy = len; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 4452 | sg_set_page(&sg[elt], skb_frag_page(frag), copy, |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 4453 | skb_frag_off(frag) + offset - start); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4454 | elt++; |
| 4455 | if (!(len -= copy)) |
| 4456 | return elt; |
| 4457 | offset += copy; |
| 4458 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 4459 | start = end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4460 | } |
| 4461 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4462 | skb_walk_frags(skb, frag_iter) { |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4463 | int end, ret; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4464 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4465 | WARN_ON(start > offset + len); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4466 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4467 | end = start + frag_iter->len; |
| 4468 | if ((copy = end - offset) > 0) { |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4469 | if (unlikely(elt && sg_is_last(&sg[elt - 1]))) |
| 4470 | return -EMSGSIZE; |
| 4471 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4472 | if (copy > len) |
| 4473 | copy = len; |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4474 | ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, |
| 4475 | copy, recursion_level + 1); |
| 4476 | if (unlikely(ret < 0)) |
| 4477 | return ret; |
| 4478 | elt += ret; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4479 | if ((len -= copy) == 0) |
| 4480 | return elt; |
| 4481 | offset += copy; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4482 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 4483 | start = end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4484 | } |
| 4485 | BUG_ON(len); |
| 4486 | return elt; |
| 4487 | } |
| 4488 | |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4489 | /** |
| 4490 | * skb_to_sgvec - Fill a scatter-gather list from a socket buffer |
| 4491 | * @skb: Socket buffer containing the buffers to be mapped |
| 4492 | * @sg: The scatter-gather list to map into |
| 4493 | * @offset: The offset into the buffer's contents to start mapping |
| 4494 | * @len: Length of buffer space to be mapped |
| 4495 | * |
| 4496 | * Fill the specified scatter-gather list with mappings/pointers into a |
| 4497 | * region of the buffer space attached to a socket buffer. Returns either |
| 4498 | * the number of scatterlist items used, or -EMSGSIZE if the contents |
| 4499 | * could not fit. |
| 4500 | */ |
| 4501 | int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) |
| 4502 | { |
| 4503 | int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); |
| 4504 | |
| 4505 | if (nsg <= 0) |
| 4506 | return nsg; |
| 4507 | |
| 4508 | sg_mark_end(&sg[nsg - 1]); |
| 4509 | |
| 4510 | return nsg; |
| 4511 | } |
| 4512 | EXPORT_SYMBOL_GPL(skb_to_sgvec); |
| 4513 | |
Fan Du | 25a91d8 | 2014-01-18 09:54:23 +0800 | [diff] [blame] | 4514 | /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given |
| 4515 | * sglist without mark the sg which contain last skb data as the end. |
| 4516 | * So the caller can mannipulate sg list as will when padding new data after |
| 4517 | * the first call without calling sg_unmark_end to expend sg list. |
| 4518 | * |
| 4519 | * Scenario to use skb_to_sgvec_nomark: |
| 4520 | * 1. sg_init_table |
| 4521 | * 2. skb_to_sgvec_nomark(payload1) |
| 4522 | * 3. skb_to_sgvec_nomark(payload2) |
| 4523 | * |
| 4524 | * This is equivalent to: |
| 4525 | * 1. sg_init_table |
| 4526 | * 2. skb_to_sgvec(payload1) |
| 4527 | * 3. sg_unmark_end |
| 4528 | * 4. skb_to_sgvec(payload2) |
| 4529 | * |
| 4530 | * When mapping mutilple payload conditionally, skb_to_sgvec_nomark |
| 4531 | * is more preferable. |
| 4532 | */ |
| 4533 | int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, |
| 4534 | int offset, int len) |
| 4535 | { |
Jason A. Donenfeld | 48a1df6 | 2017-06-04 04:16:22 +0200 | [diff] [blame] | 4536 | return __skb_to_sgvec(skb, sg, offset, len, 0); |
Fan Du | 25a91d8 | 2014-01-18 09:54:23 +0800 | [diff] [blame] | 4537 | } |
| 4538 | EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); |
| 4539 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 4540 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 4541 | |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4542 | /** |
| 4543 | * skb_cow_data - Check that a socket buffer's data buffers are writable |
| 4544 | * @skb: The socket buffer to check. |
| 4545 | * @tailbits: Amount of trailing space to be added |
| 4546 | * @trailer: Returned pointer to the skb where the @tailbits space begins |
| 4547 | * |
| 4548 | * Make sure that the data buffers attached to a socket buffer are |
| 4549 | * writable. If they are not, private copies are made of the data buffers |
| 4550 | * and the socket buffer is set to use these instead. |
| 4551 | * |
| 4552 | * If @tailbits is given, make sure that there is space to write @tailbits |
| 4553 | * bytes of data beyond current end of socket buffer. @trailer will be |
| 4554 | * set to point to the skb in which this space begins. |
| 4555 | * |
| 4556 | * The number of scatterlist elements required to completely map the |
| 4557 | * COW'd and extended socket buffer will be returned. |
| 4558 | */ |
| 4559 | int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) |
| 4560 | { |
| 4561 | int copyflag; |
| 4562 | int elt; |
| 4563 | struct sk_buff *skb1, **skb_p; |
| 4564 | |
| 4565 | /* If skb is cloned or its head is paged, reallocate |
| 4566 | * head pulling out all the pages (pages are considered not writable |
| 4567 | * at the moment even if they are anonymous). |
| 4568 | */ |
| 4569 | if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && |
Miaohe Lin | c15fc19 | 2020-08-01 17:30:23 +0800 | [diff] [blame] | 4570 | !__pskb_pull_tail(skb, __skb_pagelen(skb))) |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4571 | return -ENOMEM; |
| 4572 | |
| 4573 | /* Easy case. Most of packets will go this way. */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 4574 | if (!skb_has_frag_list(skb)) { |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4575 | /* A little of trouble, not enough of space for trailer. |
| 4576 | * This should not happen, when stack is tuned to generate |
| 4577 | * good frames. OK, on miss we reallocate and reserve even more |
| 4578 | * space, 128 bytes is fair. */ |
| 4579 | |
| 4580 | if (skb_tailroom(skb) < tailbits && |
| 4581 | pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) |
| 4582 | return -ENOMEM; |
| 4583 | |
| 4584 | /* Voila! */ |
| 4585 | *trailer = skb; |
| 4586 | return 1; |
| 4587 | } |
| 4588 | |
| 4589 | /* Misery. We are in troubles, going to mincer fragments... */ |
| 4590 | |
| 4591 | elt = 1; |
| 4592 | skb_p = &skb_shinfo(skb)->frag_list; |
| 4593 | copyflag = 0; |
| 4594 | |
| 4595 | while ((skb1 = *skb_p) != NULL) { |
| 4596 | int ntail = 0; |
| 4597 | |
| 4598 | /* The fragment is partially pulled by someone, |
| 4599 | * this can happen on input. Copy it and everything |
| 4600 | * after it. */ |
| 4601 | |
| 4602 | if (skb_shared(skb1)) |
| 4603 | copyflag = 1; |
| 4604 | |
| 4605 | /* If the skb is the last, worry about trailer. */ |
| 4606 | |
| 4607 | if (skb1->next == NULL && tailbits) { |
| 4608 | if (skb_shinfo(skb1)->nr_frags || |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 4609 | skb_has_frag_list(skb1) || |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4610 | skb_tailroom(skb1) < tailbits) |
| 4611 | ntail = tailbits + 128; |
| 4612 | } |
| 4613 | |
| 4614 | if (copyflag || |
| 4615 | skb_cloned(skb1) || |
| 4616 | ntail || |
| 4617 | skb_shinfo(skb1)->nr_frags || |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 4618 | skb_has_frag_list(skb1)) { |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4619 | struct sk_buff *skb2; |
| 4620 | |
| 4621 | /* Fuck, we are miserable poor guys... */ |
| 4622 | if (ntail == 0) |
| 4623 | skb2 = skb_copy(skb1, GFP_ATOMIC); |
| 4624 | else |
| 4625 | skb2 = skb_copy_expand(skb1, |
| 4626 | skb_headroom(skb1), |
| 4627 | ntail, |
| 4628 | GFP_ATOMIC); |
| 4629 | if (unlikely(skb2 == NULL)) |
| 4630 | return -ENOMEM; |
| 4631 | |
| 4632 | if (skb1->sk) |
| 4633 | skb_set_owner_w(skb2, skb1->sk); |
| 4634 | |
| 4635 | /* Looking around. Are we still alive? |
| 4636 | * OK, link new skb, drop old one */ |
| 4637 | |
| 4638 | skb2->next = skb1->next; |
| 4639 | *skb_p = skb2; |
| 4640 | kfree_skb(skb1); |
| 4641 | skb1 = skb2; |
| 4642 | } |
| 4643 | elt++; |
| 4644 | *trailer = skb1; |
| 4645 | skb_p = &skb1->next; |
| 4646 | } |
| 4647 | |
| 4648 | return elt; |
| 4649 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 4650 | EXPORT_SYMBOL_GPL(skb_cow_data); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 4651 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4652 | static void sock_rmem_free(struct sk_buff *skb) |
| 4653 | { |
| 4654 | struct sock *sk = skb->sk; |
| 4655 | |
| 4656 | atomic_sub(skb->truesize, &sk->sk_rmem_alloc); |
| 4657 | } |
| 4658 | |
Soheil Hassas Yeganeh | 8605330a | 2017-03-18 17:02:59 -0400 | [diff] [blame] | 4659 | static void skb_set_err_queue(struct sk_buff *skb) |
| 4660 | { |
| 4661 | /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. |
| 4662 | * So, it is safe to (mis)use it to mark skbs on the error queue. |
| 4663 | */ |
| 4664 | skb->pkt_type = PACKET_OUTGOING; |
| 4665 | BUILD_BUG_ON(PACKET_OUTGOING == 0); |
| 4666 | } |
| 4667 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4668 | /* |
| 4669 | * Note: We dont mem charge error packets (no sk_forward_alloc changes) |
| 4670 | */ |
| 4671 | int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) |
| 4672 | { |
| 4673 | if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= |
Eric Dumazet | ebb3b78 | 2019-10-10 20:17:44 -0700 | [diff] [blame] | 4674 | (unsigned int)READ_ONCE(sk->sk_rcvbuf)) |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4675 | return -ENOMEM; |
| 4676 | |
| 4677 | skb_orphan(skb); |
| 4678 | skb->sk = sk; |
| 4679 | skb->destructor = sock_rmem_free; |
| 4680 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); |
Soheil Hassas Yeganeh | 8605330a | 2017-03-18 17:02:59 -0400 | [diff] [blame] | 4681 | skb_set_err_queue(skb); |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4682 | |
Eric Dumazet | abb57ea | 2011-05-18 02:21:31 -0400 | [diff] [blame] | 4683 | /* before exiting rcu section, make sure dst is refcounted */ |
| 4684 | skb_dst_force(skb); |
| 4685 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4686 | skb_queue_tail(&sk->sk_error_queue, skb); |
| 4687 | if (!sock_flag(sk, SOCK_DEAD)) |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame^] | 4688 | sk_error_report(sk); |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 4689 | return 0; |
| 4690 | } |
| 4691 | EXPORT_SYMBOL(sock_queue_err_skb); |
| 4692 | |
Soheil Hassas Yeganeh | 83a1a1a | 2016-11-30 14:01:08 -0500 | [diff] [blame] | 4693 | static bool is_icmp_err_skb(const struct sk_buff *skb) |
| 4694 | { |
| 4695 | return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || |
| 4696 | SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); |
| 4697 | } |
| 4698 | |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 4699 | struct sk_buff *sock_dequeue_err_skb(struct sock *sk) |
| 4700 | { |
| 4701 | struct sk_buff_head *q = &sk->sk_error_queue; |
Soheil Hassas Yeganeh | 83a1a1a | 2016-11-30 14:01:08 -0500 | [diff] [blame] | 4702 | struct sk_buff *skb, *skb_next = NULL; |
| 4703 | bool icmp_next = false; |
Eric Dumazet | 997d5c3 | 2015-02-18 05:47:55 -0800 | [diff] [blame] | 4704 | unsigned long flags; |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 4705 | |
Eric Dumazet | 997d5c3 | 2015-02-18 05:47:55 -0800 | [diff] [blame] | 4706 | spin_lock_irqsave(&q->lock, flags); |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 4707 | skb = __skb_dequeue(q); |
Soheil Hassas Yeganeh | 38b2579 | 2017-06-02 12:38:22 -0400 | [diff] [blame] | 4708 | if (skb && (skb_next = skb_peek(q))) { |
Soheil Hassas Yeganeh | 83a1a1a | 2016-11-30 14:01:08 -0500 | [diff] [blame] | 4709 | icmp_next = is_icmp_err_skb(skb_next); |
Soheil Hassas Yeganeh | 38b2579 | 2017-06-02 12:38:22 -0400 | [diff] [blame] | 4710 | if (icmp_next) |
Willem de Bruijn | 985f733 | 2020-11-26 10:12:20 -0500 | [diff] [blame] | 4711 | sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; |
Soheil Hassas Yeganeh | 38b2579 | 2017-06-02 12:38:22 -0400 | [diff] [blame] | 4712 | } |
Eric Dumazet | 997d5c3 | 2015-02-18 05:47:55 -0800 | [diff] [blame] | 4713 | spin_unlock_irqrestore(&q->lock, flags); |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 4714 | |
Soheil Hassas Yeganeh | 83a1a1a | 2016-11-30 14:01:08 -0500 | [diff] [blame] | 4715 | if (is_icmp_err_skb(skb) && !icmp_next) |
| 4716 | sk->sk_err = 0; |
| 4717 | |
| 4718 | if (skb_next) |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame^] | 4719 | sk_error_report(sk); |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 4720 | |
| 4721 | return skb; |
| 4722 | } |
| 4723 | EXPORT_SYMBOL(sock_dequeue_err_skb); |
| 4724 | |
Alexander Duyck | cab41c4 | 2014-09-10 18:05:26 -0400 | [diff] [blame] | 4725 | /** |
| 4726 | * skb_clone_sk - create clone of skb, and take reference to socket |
| 4727 | * @skb: the skb to clone |
| 4728 | * |
| 4729 | * This function creates a clone of a buffer that holds a reference on |
| 4730 | * sk_refcnt. Buffers created via this function are meant to be |
| 4731 | * returned using sock_queue_err_skb, or free via kfree_skb. |
| 4732 | * |
| 4733 | * When passing buffers allocated with this function to sock_queue_err_skb |
| 4734 | * it is necessary to wrap the call with sock_hold/sock_put in order to |
| 4735 | * prevent the socket from being released prior to being enqueued on |
| 4736 | * the sk_error_queue. |
| 4737 | */ |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 4738 | struct sk_buff *skb_clone_sk(struct sk_buff *skb) |
| 4739 | { |
| 4740 | struct sock *sk = skb->sk; |
| 4741 | struct sk_buff *clone; |
| 4742 | |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 4743 | if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 4744 | return NULL; |
| 4745 | |
| 4746 | clone = skb_clone(skb, GFP_ATOMIC); |
| 4747 | if (!clone) { |
| 4748 | sock_put(sk); |
| 4749 | return NULL; |
| 4750 | } |
| 4751 | |
| 4752 | clone->sk = sk; |
| 4753 | clone->destructor = sock_efree; |
| 4754 | |
| 4755 | return clone; |
| 4756 | } |
| 4757 | EXPORT_SYMBOL(skb_clone_sk); |
| 4758 | |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4759 | static void __skb_complete_tx_timestamp(struct sk_buff *skb, |
| 4760 | struct sock *sk, |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4761 | int tstype, |
| 4762 | bool opt_stats) |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4763 | { |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4764 | struct sock_exterr_skb *serr; |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4765 | int err; |
| 4766 | |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4767 | BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); |
| 4768 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4769 | serr = SKB_EXT_ERR(skb); |
| 4770 | memset(serr, 0, sizeof(*serr)); |
| 4771 | serr->ee.ee_errno = ENOMSG; |
| 4772 | serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; |
Willem de Bruijn | e7fd288 | 2014-08-04 22:11:48 -0400 | [diff] [blame] | 4773 | serr->ee.ee_info = tstype; |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4774 | serr->opt_stats = opt_stats; |
Willem de Bruijn | 1862d62 | 2017-04-12 19:24:35 -0400 | [diff] [blame] | 4775 | serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; |
Willem de Bruijn | 4ed2d76 | 2014-08-04 22:11:49 -0400 | [diff] [blame] | 4776 | if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { |
Willem de Bruijn | 09c2d25 | 2014-08-04 22:11:47 -0400 | [diff] [blame] | 4777 | serr->ee.ee_data = skb_shinfo(skb)->tskey; |
WANG Cong | ac5cc97 | 2015-12-16 23:39:04 -0800 | [diff] [blame] | 4778 | if (sk->sk_protocol == IPPROTO_TCP && |
| 4779 | sk->sk_type == SOCK_STREAM) |
Willem de Bruijn | 4ed2d76 | 2014-08-04 22:11:49 -0400 | [diff] [blame] | 4780 | serr->ee.ee_data -= sk->sk_tskey; |
| 4781 | } |
Eric Dumazet | 2903037 | 2010-05-29 00:20:48 -0700 | [diff] [blame] | 4782 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4783 | err = sock_queue_err_skb(sk, skb); |
Eric Dumazet | 2903037 | 2010-05-29 00:20:48 -0700 | [diff] [blame] | 4784 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4785 | if (err) |
| 4786 | kfree_skb(skb); |
| 4787 | } |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4788 | |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 4789 | static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) |
| 4790 | { |
| 4791 | bool ret; |
| 4792 | |
| 4793 | if (likely(sysctl_tstamp_allow_data || tsonly)) |
| 4794 | return true; |
| 4795 | |
| 4796 | read_lock_bh(&sk->sk_callback_lock); |
| 4797 | ret = sk->sk_socket && sk->sk_socket->file && |
| 4798 | file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); |
| 4799 | read_unlock_bh(&sk->sk_callback_lock); |
| 4800 | return ret; |
| 4801 | } |
| 4802 | |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4803 | void skb_complete_tx_timestamp(struct sk_buff *skb, |
| 4804 | struct skb_shared_hwtstamps *hwtstamps) |
| 4805 | { |
| 4806 | struct sock *sk = skb->sk; |
| 4807 | |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 4808 | if (!skb_may_tx_timestamp(sk, false)) |
Willem de Bruijn | 35b99df | 2017-12-13 14:41:06 -0500 | [diff] [blame] | 4809 | goto err; |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 4810 | |
Eric Dumazet | 9ac25fc | 2017-03-03 21:01:03 -0800 | [diff] [blame] | 4811 | /* Take a reference to prevent skb_orphan() from freeing the socket, |
| 4812 | * but only if the socket refcount is not zero. |
| 4813 | */ |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 4814 | if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { |
Eric Dumazet | 9ac25fc | 2017-03-03 21:01:03 -0800 | [diff] [blame] | 4815 | *skb_hwtstamps(skb) = *hwtstamps; |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4816 | __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); |
Eric Dumazet | 9ac25fc | 2017-03-03 21:01:03 -0800 | [diff] [blame] | 4817 | sock_put(sk); |
Willem de Bruijn | 35b99df | 2017-12-13 14:41:06 -0500 | [diff] [blame] | 4818 | return; |
Eric Dumazet | 9ac25fc | 2017-03-03 21:01:03 -0800 | [diff] [blame] | 4819 | } |
Willem de Bruijn | 35b99df | 2017-12-13 14:41:06 -0500 | [diff] [blame] | 4820 | |
| 4821 | err: |
| 4822 | kfree_skb(skb); |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4823 | } |
| 4824 | EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); |
| 4825 | |
| 4826 | void __skb_tstamp_tx(struct sk_buff *orig_skb, |
Yousuk Seung | e7ed11e | 2021-01-20 12:41:55 -0800 | [diff] [blame] | 4827 | const struct sk_buff *ack_skb, |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4828 | struct skb_shared_hwtstamps *hwtstamps, |
| 4829 | struct sock *sk, int tstype) |
| 4830 | { |
| 4831 | struct sk_buff *skb; |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4832 | bool tsonly, opt_stats = false; |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4833 | |
Willem de Bruijn | 3a8dd97 | 2015-03-11 15:43:55 -0400 | [diff] [blame] | 4834 | if (!sk) |
| 4835 | return; |
| 4836 | |
Miroslav Lichvar | b50a5c7 | 2017-05-19 17:52:40 +0200 | [diff] [blame] | 4837 | if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && |
| 4838 | skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) |
| 4839 | return; |
| 4840 | |
Willem de Bruijn | 3a8dd97 | 2015-03-11 15:43:55 -0400 | [diff] [blame] | 4841 | tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; |
| 4842 | if (!skb_may_tx_timestamp(sk, tsonly)) |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4843 | return; |
| 4844 | |
Francis Yan | 1c88580 | 2016-11-27 23:07:18 -0800 | [diff] [blame] | 4845 | if (tsonly) { |
| 4846 | #ifdef CONFIG_INET |
| 4847 | if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && |
| 4848 | sk->sk_protocol == IPPROTO_TCP && |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4849 | sk->sk_type == SOCK_STREAM) { |
Yousuk Seung | e7ed11e | 2021-01-20 12:41:55 -0800 | [diff] [blame] | 4850 | skb = tcp_get_timestamping_opt_stats(sk, orig_skb, |
| 4851 | ack_skb); |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4852 | opt_stats = true; |
| 4853 | } else |
Francis Yan | 1c88580 | 2016-11-27 23:07:18 -0800 | [diff] [blame] | 4854 | #endif |
| 4855 | skb = alloc_skb(0, GFP_ATOMIC); |
| 4856 | } else { |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 4857 | skb = skb_clone(orig_skb, GFP_ATOMIC); |
Francis Yan | 1c88580 | 2016-11-27 23:07:18 -0800 | [diff] [blame] | 4858 | } |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4859 | if (!skb) |
| 4860 | return; |
| 4861 | |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 4862 | if (tsonly) { |
Willem de Bruijn | fff8803 | 2017-06-08 11:35:03 -0400 | [diff] [blame] | 4863 | skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & |
| 4864 | SKBTX_ANY_TSTAMP; |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 4865 | skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; |
| 4866 | } |
| 4867 | |
| 4868 | if (hwtstamps) |
| 4869 | *skb_hwtstamps(skb) = *hwtstamps; |
| 4870 | else |
| 4871 | skb->tstamp = ktime_get_real(); |
| 4872 | |
Soheil Hassas Yeganeh | 4ef1b28 | 2017-03-18 17:03:00 -0400 | [diff] [blame] | 4873 | __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 4874 | } |
Willem de Bruijn | e7fd288 | 2014-08-04 22:11:48 -0400 | [diff] [blame] | 4875 | EXPORT_SYMBOL_GPL(__skb_tstamp_tx); |
| 4876 | |
| 4877 | void skb_tstamp_tx(struct sk_buff *orig_skb, |
| 4878 | struct skb_shared_hwtstamps *hwtstamps) |
| 4879 | { |
Yousuk Seung | e7ed11e | 2021-01-20 12:41:55 -0800 | [diff] [blame] | 4880 | return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, |
Willem de Bruijn | e7fd288 | 2014-08-04 22:11:48 -0400 | [diff] [blame] | 4881 | SCM_TSTAMP_SND); |
| 4882 | } |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 4883 | EXPORT_SYMBOL_GPL(skb_tstamp_tx); |
| 4884 | |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 4885 | void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) |
| 4886 | { |
| 4887 | struct sock *sk = skb->sk; |
| 4888 | struct sock_exterr_skb *serr; |
Eric Dumazet | dd4f107 | 2017-03-03 21:01:02 -0800 | [diff] [blame] | 4889 | int err = 1; |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 4890 | |
| 4891 | skb->wifi_acked_valid = 1; |
| 4892 | skb->wifi_acked = acked; |
| 4893 | |
| 4894 | serr = SKB_EXT_ERR(skb); |
| 4895 | memset(serr, 0, sizeof(*serr)); |
| 4896 | serr->ee.ee_errno = ENOMSG; |
| 4897 | serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; |
| 4898 | |
Eric Dumazet | dd4f107 | 2017-03-03 21:01:02 -0800 | [diff] [blame] | 4899 | /* Take a reference to prevent skb_orphan() from freeing the socket, |
| 4900 | * but only if the socket refcount is not zero. |
| 4901 | */ |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 4902 | if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { |
Eric Dumazet | dd4f107 | 2017-03-03 21:01:02 -0800 | [diff] [blame] | 4903 | err = sock_queue_err_skb(sk, skb); |
| 4904 | sock_put(sk); |
| 4905 | } |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 4906 | if (err) |
| 4907 | kfree_skb(skb); |
| 4908 | } |
| 4909 | EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); |
| 4910 | |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 4911 | /** |
| 4912 | * skb_partial_csum_set - set up and verify partial csum values for packet |
| 4913 | * @skb: the skb to set |
| 4914 | * @start: the number of bytes after skb->data to start checksumming. |
| 4915 | * @off: the offset from start to place the checksum. |
| 4916 | * |
| 4917 | * For untrusted partially-checksummed packets, we need to make sure the values |
| 4918 | * for skb->csum_start and skb->csum_offset are valid so we don't oops. |
| 4919 | * |
| 4920 | * This function checks and sets those values and skb->ip_summed: if this |
| 4921 | * returns false you should drop the packet. |
| 4922 | */ |
| 4923 | bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) |
| 4924 | { |
Eric Dumazet | 52b5d6f | 2018-10-10 06:59:35 -0700 | [diff] [blame] | 4925 | u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); |
| 4926 | u32 csum_start = skb_headroom(skb) + (u32)start; |
| 4927 | |
| 4928 | if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { |
| 4929 | net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", |
| 4930 | start, off, skb_headroom(skb), skb_headlen(skb)); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 4931 | return false; |
| 4932 | } |
| 4933 | skb->ip_summed = CHECKSUM_PARTIAL; |
Eric Dumazet | 52b5d6f | 2018-10-10 06:59:35 -0700 | [diff] [blame] | 4934 | skb->csum_start = csum_start; |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 4935 | skb->csum_offset = off; |
Jason Wang | e5d5dec | 2013-03-26 23:11:20 +0000 | [diff] [blame] | 4936 | skb_set_transport_header(skb, start); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 4937 | return true; |
| 4938 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 4939 | EXPORT_SYMBOL_GPL(skb_partial_csum_set); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 4940 | |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4941 | static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, |
| 4942 | unsigned int max) |
| 4943 | { |
| 4944 | if (skb_headlen(skb) >= len) |
| 4945 | return 0; |
| 4946 | |
| 4947 | /* If we need to pullup then pullup to the max, so we |
| 4948 | * won't need to do it again. |
| 4949 | */ |
| 4950 | if (max > skb->len) |
| 4951 | max = skb->len; |
| 4952 | |
| 4953 | if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) |
| 4954 | return -ENOMEM; |
| 4955 | |
| 4956 | if (skb_headlen(skb) < len) |
| 4957 | return -EPROTO; |
| 4958 | |
| 4959 | return 0; |
| 4960 | } |
| 4961 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4962 | #define MAX_TCP_HDR_LEN (15 * 4) |
| 4963 | |
| 4964 | static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, |
| 4965 | typeof(IPPROTO_IP) proto, |
| 4966 | unsigned int off) |
| 4967 | { |
Kees Cook | 161d179 | 2020-02-19 22:23:04 -0800 | [diff] [blame] | 4968 | int err; |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4969 | |
Kees Cook | 161d179 | 2020-02-19 22:23:04 -0800 | [diff] [blame] | 4970 | switch (proto) { |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4971 | case IPPROTO_TCP: |
| 4972 | err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), |
| 4973 | off + MAX_TCP_HDR_LEN); |
| 4974 | if (!err && !skb_partial_csum_set(skb, off, |
| 4975 | offsetof(struct tcphdr, |
| 4976 | check))) |
| 4977 | err = -EPROTO; |
| 4978 | return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; |
| 4979 | |
| 4980 | case IPPROTO_UDP: |
| 4981 | err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), |
| 4982 | off + sizeof(struct udphdr)); |
| 4983 | if (!err && !skb_partial_csum_set(skb, off, |
| 4984 | offsetof(struct udphdr, |
| 4985 | check))) |
| 4986 | err = -EPROTO; |
| 4987 | return err ? ERR_PTR(err) : &udp_hdr(skb)->check; |
| 4988 | } |
| 4989 | |
| 4990 | return ERR_PTR(-EPROTO); |
| 4991 | } |
| 4992 | |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4993 | /* This value should be large enough to cover a tagged ethernet header plus |
| 4994 | * maximally sized IP and TCP or UDP headers. |
| 4995 | */ |
| 4996 | #define MAX_IP_HDR_LEN 128 |
| 4997 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4998 | static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4999 | { |
| 5000 | unsigned int off; |
| 5001 | bool fragment; |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 5002 | __sum16 *csum; |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 5003 | int err; |
| 5004 | |
| 5005 | fragment = false; |
| 5006 | |
| 5007 | err = skb_maybe_pull_tail(skb, |
| 5008 | sizeof(struct iphdr), |
| 5009 | MAX_IP_HDR_LEN); |
| 5010 | if (err < 0) |
| 5011 | goto out; |
| 5012 | |
Miaohe Lin | 11f920d | 2020-08-06 19:57:18 +0800 | [diff] [blame] | 5013 | if (ip_is_fragment(ip_hdr(skb))) |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 5014 | fragment = true; |
| 5015 | |
| 5016 | off = ip_hdrlen(skb); |
| 5017 | |
| 5018 | err = -EPROTO; |
| 5019 | |
| 5020 | if (fragment) |
| 5021 | goto out; |
| 5022 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 5023 | csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); |
| 5024 | if (IS_ERR(csum)) |
| 5025 | return PTR_ERR(csum); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 5026 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 5027 | if (recalculate) |
| 5028 | *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 5029 | ip_hdr(skb)->daddr, |
| 5030 | skb->len - off, |
| 5031 | ip_hdr(skb)->protocol, 0); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 5032 | err = 0; |
| 5033 | |
| 5034 | out: |
| 5035 | return err; |
| 5036 | } |
| 5037 | |
| 5038 | /* This value should be large enough to cover a tagged ethernet header plus |
| 5039 | * an IPv6 header, all options, and a maximal TCP or UDP header. |
| 5040 | */ |
| 5041 | #define MAX_IPV6_HDR_LEN 256 |
| 5042 | |
| 5043 | #define OPT_HDR(type, skb, off) \ |
| 5044 | (type *)(skb_network_header(skb) + (off)) |
| 5045 | |
| 5046 | static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) |
| 5047 | { |
| 5048 | int err; |
| 5049 | u8 nexthdr; |
| 5050 | unsigned int off; |
| 5051 | unsigned int len; |
| 5052 | bool fragment; |
| 5053 | bool done; |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 5054 | __sum16 *csum; |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 5055 | |
| 5056 | fragment = false; |
| 5057 | done = false; |
| 5058 | |
| 5059 | off = sizeof(struct ipv6hdr); |
| 5060 | |
| 5061 | err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); |
| 5062 | if (err < 0) |
| 5063 | goto out; |
| 5064 | |
| 5065 | nexthdr = ipv6_hdr(skb)->nexthdr; |
| 5066 | |
| 5067 | len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); |
| 5068 | while (off <= len && !done) { |
| 5069 | switch (nexthdr) { |
| 5070 | case IPPROTO_DSTOPTS: |
| 5071 | case IPPROTO_HOPOPTS: |
| 5072 | case IPPROTO_ROUTING: { |
| 5073 | struct ipv6_opt_hdr *hp; |
| 5074 | |
| 5075 | err = skb_maybe_pull_tail(skb, |
| 5076 | off + |
| 5077 | sizeof(struct ipv6_opt_hdr), |
| 5078 | MAX_IPV6_HDR_LEN); |
| 5079 | if (err < 0) |
| 5080 | goto out; |
| 5081 | |
| 5082 | hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); |
| 5083 | nexthdr = hp->nexthdr; |
| 5084 | off += ipv6_optlen(hp); |
| 5085 | break; |
| 5086 | } |
| 5087 | case IPPROTO_AH: { |
| 5088 | struct ip_auth_hdr *hp; |
| 5089 | |
| 5090 | err = skb_maybe_pull_tail(skb, |
| 5091 | off + |
| 5092 | sizeof(struct ip_auth_hdr), |
| 5093 | MAX_IPV6_HDR_LEN); |
| 5094 | if (err < 0) |
| 5095 | goto out; |
| 5096 | |
| 5097 | hp = OPT_HDR(struct ip_auth_hdr, skb, off); |
| 5098 | nexthdr = hp->nexthdr; |
| 5099 | off += ipv6_authlen(hp); |
| 5100 | break; |
| 5101 | } |
| 5102 | case IPPROTO_FRAGMENT: { |
| 5103 | struct frag_hdr *hp; |
| 5104 | |
| 5105 | err = skb_maybe_pull_tail(skb, |
| 5106 | off + |
| 5107 | sizeof(struct frag_hdr), |
| 5108 | MAX_IPV6_HDR_LEN); |
| 5109 | if (err < 0) |
| 5110 | goto out; |
| 5111 | |
| 5112 | hp = OPT_HDR(struct frag_hdr, skb, off); |
| 5113 | |
| 5114 | if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) |
| 5115 | fragment = true; |
| 5116 | |
| 5117 | nexthdr = hp->nexthdr; |
| 5118 | off += sizeof(struct frag_hdr); |
| 5119 | break; |
| 5120 | } |
| 5121 | default: |
| 5122 | done = true; |
| 5123 | break; |
| 5124 | } |
| 5125 | } |
| 5126 | |
| 5127 | err = -EPROTO; |
| 5128 | |
| 5129 | if (!done || fragment) |
| 5130 | goto out; |
| 5131 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 5132 | csum = skb_checksum_setup_ip(skb, nexthdr, off); |
| 5133 | if (IS_ERR(csum)) |
| 5134 | return PTR_ERR(csum); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 5135 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 5136 | if (recalculate) |
| 5137 | *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 5138 | &ipv6_hdr(skb)->daddr, |
| 5139 | skb->len - off, nexthdr, 0); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 5140 | err = 0; |
| 5141 | |
| 5142 | out: |
| 5143 | return err; |
| 5144 | } |
| 5145 | |
| 5146 | /** |
| 5147 | * skb_checksum_setup - set up partial checksum offset |
| 5148 | * @skb: the skb to set up |
| 5149 | * @recalculate: if true the pseudo-header checksum will be recalculated |
| 5150 | */ |
| 5151 | int skb_checksum_setup(struct sk_buff *skb, bool recalculate) |
| 5152 | { |
| 5153 | int err; |
| 5154 | |
| 5155 | switch (skb->protocol) { |
| 5156 | case htons(ETH_P_IP): |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 5157 | err = skb_checksum_setup_ipv4(skb, recalculate); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 5158 | break; |
| 5159 | |
| 5160 | case htons(ETH_P_IPV6): |
| 5161 | err = skb_checksum_setup_ipv6(skb, recalculate); |
| 5162 | break; |
| 5163 | |
| 5164 | default: |
| 5165 | err = -EPROTO; |
| 5166 | break; |
| 5167 | } |
| 5168 | |
| 5169 | return err; |
| 5170 | } |
| 5171 | EXPORT_SYMBOL(skb_checksum_setup); |
| 5172 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5173 | /** |
| 5174 | * skb_checksum_maybe_trim - maybe trims the given skb |
| 5175 | * @skb: the skb to check |
| 5176 | * @transport_len: the data length beyond the network header |
| 5177 | * |
| 5178 | * Checks whether the given skb has data beyond the given transport length. |
| 5179 | * If so, returns a cloned skb trimmed to this transport length. |
| 5180 | * Otherwise returns the provided skb. Returns NULL in error cases |
| 5181 | * (e.g. transport_len exceeds skb length or out-of-memory). |
| 5182 | * |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 5183 | * Caller needs to set the skb transport header and free any returned skb if it |
| 5184 | * differs from the provided skb. |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5185 | */ |
| 5186 | static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, |
| 5187 | unsigned int transport_len) |
| 5188 | { |
| 5189 | struct sk_buff *skb_chk; |
| 5190 | unsigned int len = skb_transport_offset(skb) + transport_len; |
| 5191 | int ret; |
| 5192 | |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 5193 | if (skb->len < len) |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5194 | return NULL; |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 5195 | else if (skb->len == len) |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5196 | return skb; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5197 | |
| 5198 | skb_chk = skb_clone(skb, GFP_ATOMIC); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5199 | if (!skb_chk) |
| 5200 | return NULL; |
| 5201 | |
| 5202 | ret = pskb_trim_rcsum(skb_chk, len); |
| 5203 | if (ret) { |
| 5204 | kfree_skb(skb_chk); |
| 5205 | return NULL; |
| 5206 | } |
| 5207 | |
| 5208 | return skb_chk; |
| 5209 | } |
| 5210 | |
| 5211 | /** |
| 5212 | * skb_checksum_trimmed - validate checksum of an skb |
| 5213 | * @skb: the skb to check |
| 5214 | * @transport_len: the data length beyond the network header |
| 5215 | * @skb_chkf: checksum function to use |
| 5216 | * |
| 5217 | * Applies the given checksum function skb_chkf to the provided skb. |
| 5218 | * Returns a checked and maybe trimmed skb. Returns NULL on error. |
| 5219 | * |
| 5220 | * If the skb has data beyond the given transport length, then a |
| 5221 | * trimmed & cloned skb is checked and returned. |
| 5222 | * |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 5223 | * Caller needs to set the skb transport header and free any returned skb if it |
| 5224 | * differs from the provided skb. |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5225 | */ |
| 5226 | struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, |
| 5227 | unsigned int transport_len, |
| 5228 | __sum16(*skb_chkf)(struct sk_buff *skb)) |
| 5229 | { |
| 5230 | struct sk_buff *skb_chk; |
| 5231 | unsigned int offset = skb_transport_offset(skb); |
Linus Lüssing | fcba67c | 2015-05-05 00:19:35 +0200 | [diff] [blame] | 5232 | __sum16 ret; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5233 | |
| 5234 | skb_chk = skb_checksum_maybe_trim(skb, transport_len); |
| 5235 | if (!skb_chk) |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 5236 | goto err; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5237 | |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 5238 | if (!pskb_may_pull(skb_chk, offset)) |
| 5239 | goto err; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5240 | |
Linus Lüssing | 9b36881 | 2016-02-24 04:21:42 +0100 | [diff] [blame] | 5241 | skb_pull_rcsum(skb_chk, offset); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5242 | ret = skb_chkf(skb_chk); |
Linus Lüssing | 9b36881 | 2016-02-24 04:21:42 +0100 | [diff] [blame] | 5243 | skb_push_rcsum(skb_chk, offset); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5244 | |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 5245 | if (ret) |
| 5246 | goto err; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5247 | |
| 5248 | return skb_chk; |
Linus Lüssing | a516993 | 2015-08-13 05:54:07 +0200 | [diff] [blame] | 5249 | |
| 5250 | err: |
| 5251 | if (skb_chk && skb_chk != skb) |
| 5252 | kfree_skb(skb_chk); |
| 5253 | |
| 5254 | return NULL; |
| 5255 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 5256 | } |
| 5257 | EXPORT_SYMBOL(skb_checksum_trimmed); |
| 5258 | |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 5259 | void __skb_warn_lro_forwarding(const struct sk_buff *skb) |
| 5260 | { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 5261 | net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", |
| 5262 | skb->dev->name); |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 5263 | } |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 5264 | EXPORT_SYMBOL(__skb_warn_lro_forwarding); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5265 | |
| 5266 | void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) |
| 5267 | { |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 5268 | if (head_stolen) { |
| 5269 | skb_release_head_state(skb); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5270 | kmem_cache_free(skbuff_head_cache, skb); |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 5271 | } else { |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5272 | __kfree_skb(skb); |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 5273 | } |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5274 | } |
| 5275 | EXPORT_SYMBOL(kfree_skb_partial); |
| 5276 | |
| 5277 | /** |
| 5278 | * skb_try_coalesce - try to merge skb to prior one |
| 5279 | * @to: prior buffer |
| 5280 | * @from: buffer to add |
| 5281 | * @fragstolen: pointer to boolean |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 5282 | * @delta_truesize: how much more was allocated than was requested |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5283 | */ |
| 5284 | bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, |
| 5285 | bool *fragstolen, int *delta_truesize) |
| 5286 | { |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5287 | struct skb_shared_info *to_shinfo, *from_shinfo; |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5288 | int i, delta, len = from->len; |
| 5289 | |
| 5290 | *fragstolen = false; |
| 5291 | |
| 5292 | if (skb_cloned(to)) |
| 5293 | return false; |
| 5294 | |
Ilias Apalodimas | 6a5bcd8 | 2021-06-07 21:02:38 +0200 | [diff] [blame] | 5295 | /* The page pool signature of struct page will eventually figure out |
| 5296 | * which pages can be recycled or not but for now let's prohibit slab |
| 5297 | * allocated and page_pool allocated SKBs from being coalesced. |
| 5298 | */ |
| 5299 | if (to->pp_recycle != from->pp_recycle) |
| 5300 | return false; |
| 5301 | |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5302 | if (len <= skb_tailroom(to)) { |
Eric Dumazet | e93a043 | 2014-09-15 04:19:52 -0700 | [diff] [blame] | 5303 | if (len) |
| 5304 | BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5305 | *delta_truesize = 0; |
| 5306 | return true; |
| 5307 | } |
| 5308 | |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5309 | to_shinfo = skb_shinfo(to); |
| 5310 | from_shinfo = skb_shinfo(from); |
| 5311 | if (to_shinfo->frag_list || from_shinfo->frag_list) |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5312 | return false; |
Willem de Bruijn | 1f8b977 | 2017-08-03 16:29:41 -0400 | [diff] [blame] | 5313 | if (skb_zcopy(to) || skb_zcopy(from)) |
| 5314 | return false; |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5315 | |
| 5316 | if (skb_headlen(from) != 0) { |
| 5317 | struct page *page; |
| 5318 | unsigned int offset; |
| 5319 | |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5320 | if (to_shinfo->nr_frags + |
| 5321 | from_shinfo->nr_frags >= MAX_SKB_FRAGS) |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5322 | return false; |
| 5323 | |
| 5324 | if (skb_head_is_locked(from)) |
| 5325 | return false; |
| 5326 | |
| 5327 | delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
| 5328 | |
| 5329 | page = virt_to_head_page(from->head); |
| 5330 | offset = from->data - (unsigned char *)page_address(page); |
| 5331 | |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5332 | skb_fill_page_desc(to, to_shinfo->nr_frags, |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5333 | page, offset, skb_headlen(from)); |
| 5334 | *fragstolen = true; |
| 5335 | } else { |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5336 | if (to_shinfo->nr_frags + |
| 5337 | from_shinfo->nr_frags > MAX_SKB_FRAGS) |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5338 | return false; |
| 5339 | |
Weiping Pan | f4b549a | 2012-09-28 20:15:30 +0000 | [diff] [blame] | 5340 | delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5341 | } |
| 5342 | |
| 5343 | WARN_ON_ONCE(delta < len); |
| 5344 | |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5345 | memcpy(to_shinfo->frags + to_shinfo->nr_frags, |
| 5346 | from_shinfo->frags, |
| 5347 | from_shinfo->nr_frags * sizeof(skb_frag_t)); |
| 5348 | to_shinfo->nr_frags += from_shinfo->nr_frags; |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5349 | |
| 5350 | if (!skb_cloned(from)) |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5351 | from_shinfo->nr_frags = 0; |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5352 | |
Li RongQing | 8ea853f | 2012-09-18 16:53:21 +0000 | [diff] [blame] | 5353 | /* if the skb is not cloned this does nothing |
| 5354 | * since we set nr_frags to 0. |
| 5355 | */ |
Eric Dumazet | c818fa9 | 2017-10-04 10:48:35 -0700 | [diff] [blame] | 5356 | for (i = 0; i < from_shinfo->nr_frags; i++) |
| 5357 | __skb_frag_ref(&from_shinfo->frags[i]); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 5358 | |
| 5359 | to->truesize += delta; |
| 5360 | to->len += len; |
| 5361 | to->data_len += len; |
| 5362 | |
| 5363 | *delta_truesize = delta; |
| 5364 | return true; |
| 5365 | } |
| 5366 | EXPORT_SYMBOL(skb_try_coalesce); |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5367 | |
| 5368 | /** |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 5369 | * skb_scrub_packet - scrub an skb |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5370 | * |
| 5371 | * @skb: buffer to clean |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 5372 | * @xnet: packet is crossing netns |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5373 | * |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 5374 | * skb_scrub_packet can be used after encapsulating or decapsulting a packet |
| 5375 | * into/from a tunnel. Some information have to be cleared during these |
| 5376 | * operations. |
| 5377 | * skb_scrub_packet can also be used to clean a skb before injecting it in |
| 5378 | * another namespace (@xnet == true). We have to clear all information in the |
| 5379 | * skb that could impact namespace isolation. |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5380 | */ |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 5381 | void skb_scrub_packet(struct sk_buff *skb, bool xnet) |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5382 | { |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5383 | skb->pkt_type = PACKET_HOST; |
| 5384 | skb->skb_iif = 0; |
WANG Cong | 60ff746 | 2014-05-04 16:39:18 -0700 | [diff] [blame] | 5385 | skb->ignore_df = 0; |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5386 | skb_dst_drop(skb); |
Florian Westphal | 174e238 | 2019-09-26 20:37:05 +0200 | [diff] [blame] | 5387 | skb_ext_reset(skb); |
Florian Westphal | 895b5c9 | 2019-09-29 20:54:03 +0200 | [diff] [blame] | 5388 | nf_reset_ct(skb); |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5389 | nf_reset_trace(skb); |
Herbert Xu | 213dd74 | 2015-04-16 09:03:27 +0800 | [diff] [blame] | 5390 | |
Petr Machata | 6f9a506 | 2018-11-19 16:11:07 +0000 | [diff] [blame] | 5391 | #ifdef CONFIG_NET_SWITCHDEV |
| 5392 | skb->offload_fwd_mark = 0; |
Ido Schimmel | 875e893 | 2018-12-04 08:15:10 +0000 | [diff] [blame] | 5393 | skb->offload_l3_fwd_mark = 0; |
Petr Machata | 6f9a506 | 2018-11-19 16:11:07 +0000 | [diff] [blame] | 5394 | #endif |
| 5395 | |
Herbert Xu | 213dd74 | 2015-04-16 09:03:27 +0800 | [diff] [blame] | 5396 | if (!xnet) |
| 5397 | return; |
| 5398 | |
Ye Yin | 2b5ec1a | 2017-10-26 16:57:05 +0800 | [diff] [blame] | 5399 | ipvs_reset(skb); |
Herbert Xu | 213dd74 | 2015-04-16 09:03:27 +0800 | [diff] [blame] | 5400 | skb->mark = 0; |
Jesus Sanchez-Palencia | c47d8c2 | 2018-07-03 15:42:47 -0700 | [diff] [blame] | 5401 | skb->tstamp = 0; |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 5402 | } |
| 5403 | EXPORT_SYMBOL_GPL(skb_scrub_packet); |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 5404 | |
| 5405 | /** |
| 5406 | * skb_gso_transport_seglen - Return length of individual segments of a gso packet |
| 5407 | * |
| 5408 | * @skb: GSO skb |
| 5409 | * |
| 5410 | * skb_gso_transport_seglen is used to determine the real size of the |
| 5411 | * individual segments, including Layer4 headers (TCP/UDP). |
| 5412 | * |
| 5413 | * The MAC/L2 or network (IP, IPv6) headers are not accounted for. |
| 5414 | */ |
Daniel Axtens | a4a7771 | 2018-03-01 17:13:40 +1100 | [diff] [blame] | 5415 | static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 5416 | { |
| 5417 | const struct skb_shared_info *shinfo = skb_shinfo(skb); |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 5418 | unsigned int thlen = 0; |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 5419 | |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 5420 | if (skb->encapsulation) { |
| 5421 | thlen = skb_inner_transport_header(skb) - |
| 5422 | skb_transport_header(skb); |
Florian Westphal | 6d39d58 | 2014-04-09 10:28:50 +0200 | [diff] [blame] | 5423 | |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 5424 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) |
| 5425 | thlen += inner_tcp_hdrlen(skb); |
| 5426 | } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
| 5427 | thlen = tcp_hdrlen(skb); |
Daniel Axtens | 1dd27cd | 2018-03-09 14:06:09 +1100 | [diff] [blame] | 5428 | } else if (unlikely(skb_is_gso_sctp(skb))) { |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 5429 | thlen = sizeof(struct sctphdr); |
Willem de Bruijn | ee80d1e | 2018-04-26 13:42:16 -0400 | [diff] [blame] | 5430 | } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { |
| 5431 | thlen = sizeof(struct udphdr); |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 5432 | } |
Florian Westphal | 6d39d58 | 2014-04-09 10:28:50 +0200 | [diff] [blame] | 5433 | /* UFO sets gso_size to the size of the fragmentation |
| 5434 | * payload, i.e. the size of the L4 (UDP) header is already |
| 5435 | * accounted for. |
| 5436 | */ |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 5437 | return thlen + shinfo->gso_size; |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 5438 | } |
Daniel Axtens | a4a7771 | 2018-03-01 17:13:40 +1100 | [diff] [blame] | 5439 | |
| 5440 | /** |
| 5441 | * skb_gso_network_seglen - Return length of individual segments of a gso packet |
| 5442 | * |
| 5443 | * @skb: GSO skb |
| 5444 | * |
| 5445 | * skb_gso_network_seglen is used to determine the real size of the |
| 5446 | * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). |
| 5447 | * |
| 5448 | * The MAC/L2 header is not accounted for. |
| 5449 | */ |
| 5450 | static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) |
| 5451 | { |
| 5452 | unsigned int hdr_len = skb_transport_header(skb) - |
| 5453 | skb_network_header(skb); |
| 5454 | |
| 5455 | return hdr_len + skb_gso_transport_seglen(skb); |
| 5456 | } |
| 5457 | |
| 5458 | /** |
| 5459 | * skb_gso_mac_seglen - Return length of individual segments of a gso packet |
| 5460 | * |
| 5461 | * @skb: GSO skb |
| 5462 | * |
| 5463 | * skb_gso_mac_seglen is used to determine the real size of the |
| 5464 | * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 |
| 5465 | * headers (TCP/UDP). |
| 5466 | */ |
| 5467 | static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) |
| 5468 | { |
| 5469 | unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); |
| 5470 | |
| 5471 | return hdr_len + skb_gso_transport_seglen(skb); |
| 5472 | } |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5473 | |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5474 | /** |
Daniel Axtens | 2b16f04 | 2018-01-31 14:15:33 +1100 | [diff] [blame] | 5475 | * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS |
| 5476 | * |
| 5477 | * There are a couple of instances where we have a GSO skb, and we |
| 5478 | * want to determine what size it would be after it is segmented. |
| 5479 | * |
| 5480 | * We might want to check: |
| 5481 | * - L3+L4+payload size (e.g. IP forwarding) |
| 5482 | * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) |
| 5483 | * |
| 5484 | * This is a helper to do that correctly considering GSO_BY_FRAGS. |
| 5485 | * |
Mathieu Malaterre | 49682bf | 2018-10-31 13:16:58 +0100 | [diff] [blame] | 5486 | * @skb: GSO skb |
| 5487 | * |
Daniel Axtens | 2b16f04 | 2018-01-31 14:15:33 +1100 | [diff] [blame] | 5488 | * @seg_len: The segmented length (from skb_gso_*_seglen). In the |
| 5489 | * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. |
| 5490 | * |
| 5491 | * @max_len: The maximum permissible length. |
| 5492 | * |
| 5493 | * Returns true if the segmented length <= max length. |
| 5494 | */ |
| 5495 | static inline bool skb_gso_size_check(const struct sk_buff *skb, |
| 5496 | unsigned int seg_len, |
| 5497 | unsigned int max_len) { |
| 5498 | const struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 5499 | const struct sk_buff *iter; |
| 5500 | |
| 5501 | if (shinfo->gso_size != GSO_BY_FRAGS) |
| 5502 | return seg_len <= max_len; |
| 5503 | |
| 5504 | /* Undo this so we can re-use header sizes */ |
| 5505 | seg_len -= GSO_BY_FRAGS; |
| 5506 | |
| 5507 | skb_walk_frags(skb, iter) { |
| 5508 | if (seg_len + skb_headlen(iter) > max_len) |
| 5509 | return false; |
| 5510 | } |
| 5511 | |
| 5512 | return true; |
| 5513 | } |
| 5514 | |
| 5515 | /** |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 5516 | * 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] | 5517 | * |
| 5518 | * @skb: GSO skb |
David S. Miller | 76f21b9 | 2016-06-03 22:56:28 -0700 | [diff] [blame] | 5519 | * @mtu: MTU to validate against |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5520 | * |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 5521 | * skb_gso_validate_network_len validates if a given skb will fit a |
| 5522 | * wanted MTU once split. It considers L3 headers, L4 headers, and the |
| 5523 | * payload. |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5524 | */ |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 5525 | 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] | 5526 | { |
Daniel Axtens | 2b16f04 | 2018-01-31 14:15:33 +1100 | [diff] [blame] | 5527 | 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] | 5528 | } |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 5529 | EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); |
Marcelo Ricardo Leitner | ae7ef81 | 2016-06-02 15:05:41 -0300 | [diff] [blame] | 5530 | |
Daniel Axtens | 2b16f04 | 2018-01-31 14:15:33 +1100 | [diff] [blame] | 5531 | /** |
| 5532 | * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? |
| 5533 | * |
| 5534 | * @skb: GSO skb |
| 5535 | * @len: length to validate against |
| 5536 | * |
| 5537 | * skb_gso_validate_mac_len validates if a given skb will fit a wanted |
| 5538 | * length once split, including L2, L3 and L4 headers and the payload. |
| 5539 | */ |
| 5540 | bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) |
| 5541 | { |
| 5542 | return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); |
| 5543 | } |
| 5544 | EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); |
| 5545 | |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5546 | static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) |
| 5547 | { |
Yuya Kusakabe | d85e8be | 2019-04-16 10:22:28 +0900 | [diff] [blame] | 5548 | int mac_len, meta_len; |
| 5549 | void *meta; |
Toshiaki Makita | 4bbb3e0 | 2018-03-13 14:51:27 +0900 | [diff] [blame] | 5550 | |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5551 | if (skb_cow(skb, skb_headroom(skb)) < 0) { |
| 5552 | kfree_skb(skb); |
| 5553 | return NULL; |
| 5554 | } |
| 5555 | |
Toshiaki Makita | 4bbb3e0 | 2018-03-13 14:51:27 +0900 | [diff] [blame] | 5556 | mac_len = skb->data - skb_mac_header(skb); |
Toshiaki Makita | ae47457 | 2018-03-29 19:05:29 +0900 | [diff] [blame] | 5557 | if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { |
| 5558 | memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), |
| 5559 | mac_len - VLAN_HLEN - ETH_TLEN); |
| 5560 | } |
Yuya Kusakabe | d85e8be | 2019-04-16 10:22:28 +0900 | [diff] [blame] | 5561 | |
| 5562 | meta_len = skb_metadata_len(skb); |
| 5563 | if (meta_len) { |
| 5564 | meta = skb_metadata_end(skb) - meta_len; |
| 5565 | memmove(meta + VLAN_HLEN, meta, meta_len); |
| 5566 | } |
| 5567 | |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5568 | skb->mac_header += VLAN_HLEN; |
| 5569 | return skb; |
| 5570 | } |
| 5571 | |
| 5572 | struct sk_buff *skb_vlan_untag(struct sk_buff *skb) |
| 5573 | { |
| 5574 | struct vlan_hdr *vhdr; |
| 5575 | u16 vlan_tci; |
| 5576 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 5577 | if (unlikely(skb_vlan_tag_present(skb))) { |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5578 | /* vlan_tci is already set-up so leave this for another time */ |
| 5579 | return skb; |
| 5580 | } |
| 5581 | |
| 5582 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 5583 | if (unlikely(!skb)) |
| 5584 | goto err_free; |
Miaohe Lin | 55eff0e | 2020-08-15 04:44:31 -0400 | [diff] [blame] | 5585 | /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ |
| 5586 | if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5587 | goto err_free; |
| 5588 | |
| 5589 | vhdr = (struct vlan_hdr *)skb->data; |
| 5590 | vlan_tci = ntohs(vhdr->h_vlan_TCI); |
| 5591 | __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); |
| 5592 | |
| 5593 | skb_pull_rcsum(skb, VLAN_HLEN); |
| 5594 | vlan_set_encap_proto(skb, vhdr); |
| 5595 | |
| 5596 | skb = skb_reorder_vlan_header(skb); |
| 5597 | if (unlikely(!skb)) |
| 5598 | goto err_free; |
| 5599 | |
| 5600 | skb_reset_network_header(skb); |
Alexander Lobakin | 8be33ec | 2020-11-09 23:47:23 +0000 | [diff] [blame] | 5601 | if (!skb_transport_header_was_set(skb)) |
| 5602 | skb_reset_transport_header(skb); |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 5603 | skb_reset_mac_len(skb); |
| 5604 | |
| 5605 | return skb; |
| 5606 | |
| 5607 | err_free: |
| 5608 | kfree_skb(skb); |
| 5609 | return NULL; |
| 5610 | } |
| 5611 | EXPORT_SYMBOL(skb_vlan_untag); |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5612 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 5613 | int skb_ensure_writable(struct sk_buff *skb, int write_len) |
| 5614 | { |
| 5615 | if (!pskb_may_pull(skb, write_len)) |
| 5616 | return -ENOMEM; |
| 5617 | |
| 5618 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
| 5619 | return 0; |
| 5620 | |
| 5621 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 5622 | } |
| 5623 | EXPORT_SYMBOL(skb_ensure_writable); |
| 5624 | |
Shmulik Ladkani | bfca4c5 | 2016-09-19 19:11:09 +0300 | [diff] [blame] | 5625 | /* remove VLAN header from packet and update csum accordingly. |
| 5626 | * expects a non skb_vlan_tag_present skb with a vlan tag payload |
| 5627 | */ |
| 5628 | int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5629 | { |
| 5630 | struct vlan_hdr *vhdr; |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5631 | int offset = skb->data - skb_mac_header(skb); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5632 | int err; |
| 5633 | |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5634 | if (WARN_ONCE(offset, |
| 5635 | "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", |
| 5636 | offset)) { |
| 5637 | return -EINVAL; |
| 5638 | } |
| 5639 | |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5640 | err = skb_ensure_writable(skb, VLAN_ETH_HLEN); |
| 5641 | if (unlikely(err)) |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5642 | return err; |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5643 | |
| 5644 | skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); |
| 5645 | |
| 5646 | vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); |
| 5647 | *vlan_tci = ntohs(vhdr->h_vlan_TCI); |
| 5648 | |
| 5649 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
| 5650 | __skb_pull(skb, VLAN_HLEN); |
| 5651 | |
| 5652 | vlan_set_encap_proto(skb, vhdr); |
| 5653 | skb->mac_header += VLAN_HLEN; |
| 5654 | |
| 5655 | if (skb_network_offset(skb) < ETH_HLEN) |
| 5656 | skb_set_network_header(skb, ETH_HLEN); |
| 5657 | |
| 5658 | skb_reset_mac_len(skb); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5659 | |
| 5660 | return err; |
| 5661 | } |
Shmulik Ladkani | bfca4c5 | 2016-09-19 19:11:09 +0300 | [diff] [blame] | 5662 | EXPORT_SYMBOL(__skb_vlan_pop); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5663 | |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5664 | /* Pop a vlan tag either from hwaccel or from payload. |
| 5665 | * Expects skb->data at mac header. |
| 5666 | */ |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5667 | int skb_vlan_pop(struct sk_buff *skb) |
| 5668 | { |
| 5669 | u16 vlan_tci; |
| 5670 | __be16 vlan_proto; |
| 5671 | int err; |
| 5672 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 5673 | if (likely(skb_vlan_tag_present(skb))) { |
Michał Mirosław | b1817524 | 2018-11-09 00:18:02 +0100 | [diff] [blame] | 5674 | __vlan_hwaccel_clear_tag(skb); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5675 | } else { |
Shmulik Ladkani | ecf4ee4 | 2016-09-20 12:48:37 +0300 | [diff] [blame] | 5676 | if (unlikely(!eth_type_vlan(skb->protocol))) |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5677 | return 0; |
| 5678 | |
| 5679 | err = __skb_vlan_pop(skb, &vlan_tci); |
| 5680 | if (err) |
| 5681 | return err; |
| 5682 | } |
| 5683 | /* move next vlan tag to hw accel tag */ |
Shmulik Ladkani | ecf4ee4 | 2016-09-20 12:48:37 +0300 | [diff] [blame] | 5684 | if (likely(!eth_type_vlan(skb->protocol))) |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5685 | return 0; |
| 5686 | |
| 5687 | vlan_proto = skb->protocol; |
| 5688 | err = __skb_vlan_pop(skb, &vlan_tci); |
| 5689 | if (unlikely(err)) |
| 5690 | return err; |
| 5691 | |
| 5692 | __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); |
| 5693 | return 0; |
| 5694 | } |
| 5695 | EXPORT_SYMBOL(skb_vlan_pop); |
| 5696 | |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5697 | /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). |
| 5698 | * Expects skb->data at mac header. |
| 5699 | */ |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5700 | int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) |
| 5701 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 5702 | if (skb_vlan_tag_present(skb)) { |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5703 | int offset = skb->data - skb_mac_header(skb); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5704 | int err; |
| 5705 | |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5706 | if (WARN_ONCE(offset, |
| 5707 | "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", |
| 5708 | offset)) { |
| 5709 | return -EINVAL; |
| 5710 | } |
| 5711 | |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5712 | err = __vlan_insert_tag(skb, skb->vlan_proto, |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 5713 | skb_vlan_tag_get(skb)); |
Shmulik Ladkani | b6a7920 | 2016-09-29 12:10:41 +0300 | [diff] [blame] | 5714 | if (err) |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5715 | return err; |
Daniel Borkmann | 9241e2d | 2016-04-16 02:27:58 +0200 | [diff] [blame] | 5716 | |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5717 | skb->protocol = skb->vlan_proto; |
| 5718 | skb->mac_len += VLAN_HLEN; |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5719 | |
Daniel Borkmann | 6b83d28 | 2016-02-20 00:29:30 +0100 | [diff] [blame] | 5720 | skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 5721 | } |
| 5722 | __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); |
| 5723 | return 0; |
| 5724 | } |
| 5725 | EXPORT_SYMBOL(skb_vlan_push); |
| 5726 | |
Guillaume Nault | 19fbcb3 | 2020-10-03 00:44:28 +0200 | [diff] [blame] | 5727 | /** |
| 5728 | * skb_eth_pop() - Drop the Ethernet header at the head of a packet |
| 5729 | * |
| 5730 | * @skb: Socket buffer to modify |
| 5731 | * |
| 5732 | * Drop the Ethernet header of @skb. |
| 5733 | * |
| 5734 | * Expects that skb->data points to the mac header and that no VLAN tags are |
| 5735 | * present. |
| 5736 | * |
| 5737 | * Returns 0 on success, -errno otherwise. |
| 5738 | */ |
| 5739 | int skb_eth_pop(struct sk_buff *skb) |
| 5740 | { |
| 5741 | if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || |
| 5742 | skb_network_offset(skb) < ETH_HLEN) |
| 5743 | return -EPROTO; |
| 5744 | |
| 5745 | skb_pull_rcsum(skb, ETH_HLEN); |
| 5746 | skb_reset_mac_header(skb); |
| 5747 | skb_reset_mac_len(skb); |
| 5748 | |
| 5749 | return 0; |
| 5750 | } |
| 5751 | EXPORT_SYMBOL(skb_eth_pop); |
| 5752 | |
| 5753 | /** |
| 5754 | * skb_eth_push() - Add a new Ethernet header at the head of a packet |
| 5755 | * |
| 5756 | * @skb: Socket buffer to modify |
| 5757 | * @dst: Destination MAC address of the new header |
| 5758 | * @src: Source MAC address of the new header |
| 5759 | * |
| 5760 | * Prepend @skb with a new Ethernet header. |
| 5761 | * |
| 5762 | * Expects that skb->data points to the mac header, which must be empty. |
| 5763 | * |
| 5764 | * Returns 0 on success, -errno otherwise. |
| 5765 | */ |
| 5766 | int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, |
| 5767 | const unsigned char *src) |
| 5768 | { |
| 5769 | struct ethhdr *eth; |
| 5770 | int err; |
| 5771 | |
| 5772 | if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) |
| 5773 | return -EPROTO; |
| 5774 | |
| 5775 | err = skb_cow_head(skb, sizeof(*eth)); |
| 5776 | if (err < 0) |
| 5777 | return err; |
| 5778 | |
| 5779 | skb_push(skb, sizeof(*eth)); |
| 5780 | skb_reset_mac_header(skb); |
| 5781 | skb_reset_mac_len(skb); |
| 5782 | |
| 5783 | eth = eth_hdr(skb); |
| 5784 | ether_addr_copy(eth->h_dest, dst); |
| 5785 | ether_addr_copy(eth->h_source, src); |
| 5786 | eth->h_proto = skb->protocol; |
| 5787 | |
| 5788 | skb_postpush_rcsum(skb, eth, sizeof(*eth)); |
| 5789 | |
| 5790 | return 0; |
| 5791 | } |
| 5792 | EXPORT_SYMBOL(skb_eth_push); |
| 5793 | |
John Hurley | 8822e27 | 2019-07-07 15:01:54 +0100 | [diff] [blame] | 5794 | /* Update the ethertype of hdr and the skb csum value if required. */ |
| 5795 | static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, |
| 5796 | __be16 ethertype) |
| 5797 | { |
| 5798 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 5799 | __be16 diff[] = { ~hdr->h_proto, ethertype }; |
| 5800 | |
| 5801 | skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); |
| 5802 | } |
| 5803 | |
| 5804 | hdr->h_proto = ethertype; |
| 5805 | } |
| 5806 | |
| 5807 | /** |
Martin Varghese | e7dbfed | 2019-12-21 08:50:01 +0530 | [diff] [blame] | 5808 | * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of |
| 5809 | * the packet |
John Hurley | 8822e27 | 2019-07-07 15:01:54 +0100 | [diff] [blame] | 5810 | * |
| 5811 | * @skb: buffer |
| 5812 | * @mpls_lse: MPLS label stack entry to push |
| 5813 | * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) |
Davide Caratti | fa4e0f8 | 2019-10-12 13:55:07 +0200 | [diff] [blame] | 5814 | * @mac_len: length of the MAC header |
Martin Varghese | e7dbfed | 2019-12-21 08:50:01 +0530 | [diff] [blame] | 5815 | * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is |
| 5816 | * ethernet |
John Hurley | 8822e27 | 2019-07-07 15:01:54 +0100 | [diff] [blame] | 5817 | * |
| 5818 | * Expects skb->data at mac header. |
| 5819 | * |
| 5820 | * Returns 0 on success, -errno otherwise. |
| 5821 | */ |
Davide Caratti | fa4e0f8 | 2019-10-12 13:55:07 +0200 | [diff] [blame] | 5822 | int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, |
Martin Varghese | d04ac22 | 2019-12-05 05:57:22 +0530 | [diff] [blame] | 5823 | int mac_len, bool ethernet) |
John Hurley | 8822e27 | 2019-07-07 15:01:54 +0100 | [diff] [blame] | 5824 | { |
| 5825 | struct mpls_shim_hdr *lse; |
| 5826 | int err; |
| 5827 | |
| 5828 | if (unlikely(!eth_p_mpls(mpls_proto))) |
| 5829 | return -EINVAL; |
| 5830 | |
| 5831 | /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ |
| 5832 | if (skb->encapsulation) |
| 5833 | return -EINVAL; |
| 5834 | |
| 5835 | err = skb_cow_head(skb, MPLS_HLEN); |
| 5836 | if (unlikely(err)) |
| 5837 | return err; |
| 5838 | |
| 5839 | if (!skb->inner_protocol) { |
Martin Varghese | e7dbfed | 2019-12-21 08:50:01 +0530 | [diff] [blame] | 5840 | skb_set_inner_network_header(skb, skb_network_offset(skb)); |
John Hurley | 8822e27 | 2019-07-07 15:01:54 +0100 | [diff] [blame] | 5841 | skb_set_inner_protocol(skb, skb->protocol); |
| 5842 | } |
| 5843 | |
| 5844 | skb_push(skb, MPLS_HLEN); |
| 5845 | memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), |
Davide Caratti | fa4e0f8 | 2019-10-12 13:55:07 +0200 | [diff] [blame] | 5846 | mac_len); |
John Hurley | 8822e27 | 2019-07-07 15:01:54 +0100 | [diff] [blame] | 5847 | skb_reset_mac_header(skb); |
Davide Caratti | fa4e0f8 | 2019-10-12 13:55:07 +0200 | [diff] [blame] | 5848 | skb_set_network_header(skb, mac_len); |
Martin Varghese | e7dbfed | 2019-12-21 08:50:01 +0530 | [diff] [blame] | 5849 | skb_reset_mac_len(skb); |
John Hurley | 8822e27 | 2019-07-07 15:01:54 +0100 | [diff] [blame] | 5850 | |
| 5851 | lse = mpls_hdr(skb); |
| 5852 | lse->label_stack_entry = mpls_lse; |
| 5853 | skb_postpush_rcsum(skb, lse, MPLS_HLEN); |
| 5854 | |
Guillaume Nault | 4296adc | 2020-10-02 21:53:08 +0200 | [diff] [blame] | 5855 | if (ethernet && mac_len >= ETH_HLEN) |
John Hurley | 8822e27 | 2019-07-07 15:01:54 +0100 | [diff] [blame] | 5856 | skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); |
| 5857 | skb->protocol = mpls_proto; |
| 5858 | |
| 5859 | return 0; |
| 5860 | } |
| 5861 | EXPORT_SYMBOL_GPL(skb_mpls_push); |
| 5862 | |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5863 | /** |
John Hurley | ed246ce | 2019-07-07 15:01:55 +0100 | [diff] [blame] | 5864 | * skb_mpls_pop() - pop the outermost MPLS header |
| 5865 | * |
| 5866 | * @skb: buffer |
| 5867 | * @next_proto: ethertype of header after popped MPLS header |
Davide Caratti | fa4e0f8 | 2019-10-12 13:55:07 +0200 | [diff] [blame] | 5868 | * @mac_len: length of the MAC header |
Martin Varghese | 76f99f9 | 2019-12-21 08:50:23 +0530 | [diff] [blame] | 5869 | * @ethernet: flag to indicate if the packet is ethernet |
John Hurley | ed246ce | 2019-07-07 15:01:55 +0100 | [diff] [blame] | 5870 | * |
| 5871 | * Expects skb->data at mac header. |
| 5872 | * |
| 5873 | * Returns 0 on success, -errno otherwise. |
| 5874 | */ |
Martin Varghese | 040b5cf | 2019-12-02 10:49:51 +0530 | [diff] [blame] | 5875 | int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, |
| 5876 | bool ethernet) |
John Hurley | ed246ce | 2019-07-07 15:01:55 +0100 | [diff] [blame] | 5877 | { |
| 5878 | int err; |
| 5879 | |
| 5880 | if (unlikely(!eth_p_mpls(skb->protocol))) |
Davide Caratti | dedc5a0 | 2019-10-12 13:55:06 +0200 | [diff] [blame] | 5881 | return 0; |
John Hurley | ed246ce | 2019-07-07 15:01:55 +0100 | [diff] [blame] | 5882 | |
Davide Caratti | fa4e0f8 | 2019-10-12 13:55:07 +0200 | [diff] [blame] | 5883 | err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); |
John Hurley | ed246ce | 2019-07-07 15:01:55 +0100 | [diff] [blame] | 5884 | if (unlikely(err)) |
| 5885 | return err; |
| 5886 | |
| 5887 | skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); |
| 5888 | memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), |
Davide Caratti | fa4e0f8 | 2019-10-12 13:55:07 +0200 | [diff] [blame] | 5889 | mac_len); |
John Hurley | ed246ce | 2019-07-07 15:01:55 +0100 | [diff] [blame] | 5890 | |
| 5891 | __skb_pull(skb, MPLS_HLEN); |
| 5892 | skb_reset_mac_header(skb); |
Davide Caratti | fa4e0f8 | 2019-10-12 13:55:07 +0200 | [diff] [blame] | 5893 | skb_set_network_header(skb, mac_len); |
John Hurley | ed246ce | 2019-07-07 15:01:55 +0100 | [diff] [blame] | 5894 | |
Guillaume Nault | 4296adc | 2020-10-02 21:53:08 +0200 | [diff] [blame] | 5895 | if (ethernet && mac_len >= ETH_HLEN) { |
John Hurley | ed246ce | 2019-07-07 15:01:55 +0100 | [diff] [blame] | 5896 | struct ethhdr *hdr; |
| 5897 | |
| 5898 | /* use mpls_hdr() to get ethertype to account for VLANs. */ |
| 5899 | hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); |
| 5900 | skb_mod_eth_type(skb, hdr, next_proto); |
| 5901 | } |
| 5902 | skb->protocol = next_proto; |
| 5903 | |
| 5904 | return 0; |
| 5905 | } |
| 5906 | EXPORT_SYMBOL_GPL(skb_mpls_pop); |
| 5907 | |
| 5908 | /** |
John Hurley | d27cf5c | 2019-07-07 15:01:56 +0100 | [diff] [blame] | 5909 | * skb_mpls_update_lse() - modify outermost MPLS header and update csum |
| 5910 | * |
| 5911 | * @skb: buffer |
| 5912 | * @mpls_lse: new MPLS label stack entry to update to |
| 5913 | * |
| 5914 | * Expects skb->data at mac header. |
| 5915 | * |
| 5916 | * Returns 0 on success, -errno otherwise. |
| 5917 | */ |
| 5918 | int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) |
| 5919 | { |
| 5920 | int err; |
| 5921 | |
| 5922 | if (unlikely(!eth_p_mpls(skb->protocol))) |
| 5923 | return -EINVAL; |
| 5924 | |
| 5925 | err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); |
| 5926 | if (unlikely(err)) |
| 5927 | return err; |
| 5928 | |
| 5929 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 5930 | __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; |
| 5931 | |
| 5932 | skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); |
| 5933 | } |
| 5934 | |
| 5935 | mpls_hdr(skb)->label_stack_entry = mpls_lse; |
| 5936 | |
| 5937 | return 0; |
| 5938 | } |
| 5939 | EXPORT_SYMBOL_GPL(skb_mpls_update_lse); |
| 5940 | |
| 5941 | /** |
John Hurley | 2a2ea50 | 2019-07-07 15:01:57 +0100 | [diff] [blame] | 5942 | * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header |
| 5943 | * |
| 5944 | * @skb: buffer |
| 5945 | * |
| 5946 | * Expects skb->data at mac header. |
| 5947 | * |
| 5948 | * Returns 0 on success, -errno otherwise. |
| 5949 | */ |
| 5950 | int skb_mpls_dec_ttl(struct sk_buff *skb) |
| 5951 | { |
| 5952 | u32 lse; |
| 5953 | u8 ttl; |
| 5954 | |
| 5955 | if (unlikely(!eth_p_mpls(skb->protocol))) |
| 5956 | return -EINVAL; |
| 5957 | |
Davide Caratti | 13de4ed | 2020-12-03 10:58:21 +0100 | [diff] [blame] | 5958 | if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) |
| 5959 | return -ENOMEM; |
| 5960 | |
John Hurley | 2a2ea50 | 2019-07-07 15:01:57 +0100 | [diff] [blame] | 5961 | lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); |
| 5962 | ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; |
| 5963 | if (!--ttl) |
| 5964 | return -EINVAL; |
| 5965 | |
| 5966 | lse &= ~MPLS_LS_TTL_MASK; |
| 5967 | lse |= ttl << MPLS_LS_TTL_SHIFT; |
| 5968 | |
| 5969 | return skb_mpls_update_lse(skb, cpu_to_be32(lse)); |
| 5970 | } |
| 5971 | EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); |
| 5972 | |
| 5973 | /** |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5974 | * alloc_skb_with_frags - allocate skb with page frags |
| 5975 | * |
Masanari Iida | de3f0d0 | 2014-10-09 12:58:08 +0900 | [diff] [blame] | 5976 | * @header_len: size of linear part |
| 5977 | * @data_len: needed length in frags |
| 5978 | * @max_page_order: max page order desired. |
| 5979 | * @errcode: pointer to error code if any |
| 5980 | * @gfp_mask: allocation mask |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5981 | * |
| 5982 | * This can be used to allocate a paged skb, given a maximal order for frags. |
| 5983 | */ |
| 5984 | struct sk_buff *alloc_skb_with_frags(unsigned long header_len, |
| 5985 | unsigned long data_len, |
| 5986 | int max_page_order, |
| 5987 | int *errcode, |
| 5988 | gfp_t gfp_mask) |
| 5989 | { |
| 5990 | int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; |
| 5991 | unsigned long chunk; |
| 5992 | struct sk_buff *skb; |
| 5993 | struct page *page; |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 5994 | int i; |
| 5995 | |
| 5996 | *errcode = -EMSGSIZE; |
| 5997 | /* Note this test could be relaxed, if we succeed to allocate |
| 5998 | * high order pages... |
| 5999 | */ |
| 6000 | if (npages > MAX_SKB_FRAGS) |
| 6001 | return NULL; |
| 6002 | |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 6003 | *errcode = -ENOBUFS; |
David Rientjes | f8c468e | 2019-01-02 13:01:43 -0800 | [diff] [blame] | 6004 | skb = alloc_skb(header_len, gfp_mask); |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 6005 | if (!skb) |
| 6006 | return NULL; |
| 6007 | |
| 6008 | skb->truesize += npages << PAGE_SHIFT; |
| 6009 | |
| 6010 | for (i = 0; npages > 0; i++) { |
| 6011 | int order = max_page_order; |
| 6012 | |
| 6013 | while (order) { |
| 6014 | if (npages >= 1 << order) { |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 6015 | page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 6016 | __GFP_COMP | |
Michal Hocko | d14b56f | 2018-06-28 17:53:06 +0200 | [diff] [blame] | 6017 | __GFP_NOWARN, |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 6018 | order); |
| 6019 | if (page) |
| 6020 | goto fill_page; |
| 6021 | /* Do not retry other high order allocations */ |
| 6022 | order = 1; |
| 6023 | max_page_order = 0; |
| 6024 | } |
| 6025 | order--; |
| 6026 | } |
| 6027 | page = alloc_page(gfp_mask); |
| 6028 | if (!page) |
| 6029 | goto failure; |
| 6030 | fill_page: |
| 6031 | chunk = min_t(unsigned long, data_len, |
| 6032 | PAGE_SIZE << order); |
| 6033 | skb_fill_page_desc(skb, i, page, 0, chunk); |
| 6034 | data_len -= chunk; |
| 6035 | npages -= 1 << order; |
| 6036 | } |
| 6037 | return skb; |
| 6038 | |
| 6039 | failure: |
| 6040 | kfree_skb(skb); |
| 6041 | return NULL; |
| 6042 | } |
| 6043 | EXPORT_SYMBOL(alloc_skb_with_frags); |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 6044 | |
| 6045 | /* carve out the first off bytes from skb when off < headlen */ |
| 6046 | static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, |
| 6047 | const int headlen, gfp_t gfp_mask) |
| 6048 | { |
| 6049 | int i; |
| 6050 | int size = skb_end_offset(skb); |
| 6051 | int new_hlen = headlen - off; |
| 6052 | u8 *data; |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 6053 | |
| 6054 | size = SKB_DATA_ALIGN(size); |
| 6055 | |
| 6056 | if (skb_pfmemalloc(skb)) |
| 6057 | gfp_mask |= __GFP_MEMALLOC; |
| 6058 | data = kmalloc_reserve(size + |
| 6059 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), |
| 6060 | gfp_mask, NUMA_NO_NODE, NULL); |
| 6061 | if (!data) |
| 6062 | return -ENOMEM; |
| 6063 | |
| 6064 | size = SKB_WITH_OVERHEAD(ksize(data)); |
| 6065 | |
| 6066 | /* Copy real data, and all frags */ |
| 6067 | skb_copy_from_linear_data_offset(skb, off, data, new_hlen); |
| 6068 | skb->len -= off; |
| 6069 | |
| 6070 | memcpy((struct skb_shared_info *)(data + size), |
| 6071 | skb_shinfo(skb), |
| 6072 | offsetof(struct skb_shared_info, |
| 6073 | frags[skb_shinfo(skb)->nr_frags])); |
| 6074 | if (skb_cloned(skb)) { |
| 6075 | /* drop the old head gracefully */ |
| 6076 | if (skb_orphan_frags(skb, gfp_mask)) { |
| 6077 | kfree(data); |
| 6078 | return -ENOMEM; |
| 6079 | } |
| 6080 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 6081 | skb_frag_ref(skb, i); |
| 6082 | if (skb_has_frag_list(skb)) |
| 6083 | skb_clone_fraglist(skb); |
| 6084 | skb_release_data(skb); |
| 6085 | } else { |
| 6086 | /* we can reuse existing recount- all we did was |
| 6087 | * relocate values |
| 6088 | */ |
| 6089 | skb_free_head(skb); |
| 6090 | } |
| 6091 | |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 6092 | skb->head = data; |
| 6093 | skb->data = data; |
| 6094 | skb->head_frag = 0; |
| 6095 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 6096 | skb->end = size; |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 6097 | #else |
| 6098 | skb->end = skb->head + size; |
| 6099 | #endif |
| 6100 | skb_set_tail_pointer(skb, skb_headlen(skb)); |
| 6101 | skb_headers_offset_update(skb, 0); |
| 6102 | skb->cloned = 0; |
| 6103 | skb->hdr_len = 0; |
| 6104 | skb->nohdr = 0; |
| 6105 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 6106 | |
| 6107 | return 0; |
| 6108 | } |
| 6109 | |
| 6110 | static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); |
| 6111 | |
| 6112 | /* carve out the first eat bytes from skb's frag_list. May recurse into |
| 6113 | * pskb_carve() |
| 6114 | */ |
| 6115 | static int pskb_carve_frag_list(struct sk_buff *skb, |
| 6116 | struct skb_shared_info *shinfo, int eat, |
| 6117 | gfp_t gfp_mask) |
| 6118 | { |
| 6119 | struct sk_buff *list = shinfo->frag_list; |
| 6120 | struct sk_buff *clone = NULL; |
| 6121 | struct sk_buff *insp = NULL; |
| 6122 | |
| 6123 | do { |
| 6124 | if (!list) { |
| 6125 | pr_err("Not enough bytes to eat. Want %d\n", eat); |
| 6126 | return -EFAULT; |
| 6127 | } |
| 6128 | if (list->len <= eat) { |
| 6129 | /* Eaten as whole. */ |
| 6130 | eat -= list->len; |
| 6131 | list = list->next; |
| 6132 | insp = list; |
| 6133 | } else { |
| 6134 | /* Eaten partially. */ |
| 6135 | if (skb_shared(list)) { |
| 6136 | clone = skb_clone(list, gfp_mask); |
| 6137 | if (!clone) |
| 6138 | return -ENOMEM; |
| 6139 | insp = list->next; |
| 6140 | list = clone; |
| 6141 | } else { |
| 6142 | /* This may be pulled without problems. */ |
| 6143 | insp = list; |
| 6144 | } |
| 6145 | if (pskb_carve(list, eat, gfp_mask) < 0) { |
| 6146 | kfree_skb(clone); |
| 6147 | return -ENOMEM; |
| 6148 | } |
| 6149 | break; |
| 6150 | } |
| 6151 | } while (eat); |
| 6152 | |
| 6153 | /* Free pulled out fragments. */ |
| 6154 | while ((list = shinfo->frag_list) != insp) { |
| 6155 | shinfo->frag_list = list->next; |
| 6156 | kfree_skb(list); |
| 6157 | } |
| 6158 | /* And insert new clone at head. */ |
| 6159 | if (clone) { |
| 6160 | clone->next = list; |
| 6161 | shinfo->frag_list = clone; |
| 6162 | } |
| 6163 | return 0; |
| 6164 | } |
| 6165 | |
| 6166 | /* carve off first len bytes from skb. Split line (off) is in the |
| 6167 | * non-linear part of skb |
| 6168 | */ |
| 6169 | static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, |
| 6170 | int pos, gfp_t gfp_mask) |
| 6171 | { |
| 6172 | int i, k = 0; |
| 6173 | int size = skb_end_offset(skb); |
| 6174 | u8 *data; |
| 6175 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 6176 | struct skb_shared_info *shinfo; |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 6177 | |
| 6178 | size = SKB_DATA_ALIGN(size); |
| 6179 | |
| 6180 | if (skb_pfmemalloc(skb)) |
| 6181 | gfp_mask |= __GFP_MEMALLOC; |
| 6182 | data = kmalloc_reserve(size + |
| 6183 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), |
| 6184 | gfp_mask, NUMA_NO_NODE, NULL); |
| 6185 | if (!data) |
| 6186 | return -ENOMEM; |
| 6187 | |
| 6188 | size = SKB_WITH_OVERHEAD(ksize(data)); |
| 6189 | |
| 6190 | memcpy((struct skb_shared_info *)(data + size), |
Miaohe Lin | e3ec1e8 | 2020-08-15 04:48:53 -0400 | [diff] [blame] | 6191 | skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 6192 | if (skb_orphan_frags(skb, gfp_mask)) { |
| 6193 | kfree(data); |
| 6194 | return -ENOMEM; |
| 6195 | } |
| 6196 | shinfo = (struct skb_shared_info *)(data + size); |
| 6197 | for (i = 0; i < nfrags; i++) { |
| 6198 | int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 6199 | |
| 6200 | if (pos + fsize > off) { |
| 6201 | shinfo->frags[k] = skb_shinfo(skb)->frags[i]; |
| 6202 | |
| 6203 | if (pos < off) { |
| 6204 | /* Split frag. |
| 6205 | * We have two variants in this case: |
| 6206 | * 1. Move all the frag to the second |
| 6207 | * part, if it is possible. F.e. |
| 6208 | * this approach is mandatory for TUX, |
| 6209 | * where splitting is expensive. |
| 6210 | * 2. Split is accurately. We make this. |
| 6211 | */ |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 6212 | skb_frag_off_add(&shinfo->frags[0], off - pos); |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 6213 | skb_frag_size_sub(&shinfo->frags[0], off - pos); |
| 6214 | } |
| 6215 | skb_frag_ref(skb, i); |
| 6216 | k++; |
| 6217 | } |
| 6218 | pos += fsize; |
| 6219 | } |
| 6220 | shinfo->nr_frags = k; |
| 6221 | if (skb_has_frag_list(skb)) |
| 6222 | skb_clone_fraglist(skb); |
| 6223 | |
Miaohe Lin | eabe861 | 2020-08-15 04:46:41 -0400 | [diff] [blame] | 6224 | /* split line is in frag list */ |
| 6225 | if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { |
| 6226 | /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ |
| 6227 | if (skb_has_frag_list(skb)) |
| 6228 | kfree_skb_list(skb_shinfo(skb)->frag_list); |
| 6229 | kfree(data); |
| 6230 | return -ENOMEM; |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 6231 | } |
| 6232 | skb_release_data(skb); |
| 6233 | |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 6234 | skb->head = data; |
| 6235 | skb->head_frag = 0; |
| 6236 | skb->data = data; |
| 6237 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 6238 | skb->end = size; |
Sowmini Varadhan | 6fa01cc | 2016-04-22 18:36:35 -0700 | [diff] [blame] | 6239 | #else |
| 6240 | skb->end = skb->head + size; |
| 6241 | #endif |
| 6242 | skb_reset_tail_pointer(skb); |
| 6243 | skb_headers_offset_update(skb, 0); |
| 6244 | skb->cloned = 0; |
| 6245 | skb->hdr_len = 0; |
| 6246 | skb->nohdr = 0; |
| 6247 | skb->len -= off; |
| 6248 | skb->data_len = skb->len; |
| 6249 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 6250 | return 0; |
| 6251 | } |
| 6252 | |
| 6253 | /* remove len bytes from the beginning of the skb */ |
| 6254 | static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) |
| 6255 | { |
| 6256 | int headlen = skb_headlen(skb); |
| 6257 | |
| 6258 | if (len < headlen) |
| 6259 | return pskb_carve_inside_header(skb, len, headlen, gfp); |
| 6260 | else |
| 6261 | return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); |
| 6262 | } |
| 6263 | |
| 6264 | /* Extract to_copy bytes starting at off from skb, and return this in |
| 6265 | * a new skb |
| 6266 | */ |
| 6267 | struct sk_buff *pskb_extract(struct sk_buff *skb, int off, |
| 6268 | int to_copy, gfp_t gfp) |
| 6269 | { |
| 6270 | struct sk_buff *clone = skb_clone(skb, gfp); |
| 6271 | |
| 6272 | if (!clone) |
| 6273 | return NULL; |
| 6274 | |
| 6275 | if (pskb_carve(clone, off, gfp) < 0 || |
| 6276 | pskb_trim(clone, to_copy)) { |
| 6277 | kfree_skb(clone); |
| 6278 | return NULL; |
| 6279 | } |
| 6280 | return clone; |
| 6281 | } |
| 6282 | EXPORT_SYMBOL(pskb_extract); |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 6283 | |
| 6284 | /** |
| 6285 | * skb_condense - try to get rid of fragments/frag_list if possible |
| 6286 | * @skb: buffer |
| 6287 | * |
| 6288 | * Can be used to save memory before skb is added to a busy queue. |
| 6289 | * If packet has bytes in frags and enough tail room in skb->head, |
| 6290 | * pull all of them, so that we can free the frags right now and adjust |
| 6291 | * truesize. |
| 6292 | * Notes: |
| 6293 | * We do not reallocate skb->head thus can not fail. |
| 6294 | * Caller must re-evaluate skb->truesize if needed. |
| 6295 | */ |
| 6296 | void skb_condense(struct sk_buff *skb) |
| 6297 | { |
Eric Dumazet | 3174fed | 2016-12-09 08:02:05 -0800 | [diff] [blame] | 6298 | if (skb->data_len) { |
| 6299 | if (skb->data_len > skb->end - skb->tail || |
| 6300 | skb_cloned(skb)) |
| 6301 | return; |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 6302 | |
Eric Dumazet | 3174fed | 2016-12-09 08:02:05 -0800 | [diff] [blame] | 6303 | /* Nice, we can free page frag(s) right now */ |
| 6304 | __pskb_pull_tail(skb, skb->data_len); |
| 6305 | } |
| 6306 | /* At this point, skb->truesize might be over estimated, |
| 6307 | * because skb had a fragment, and fragments do not tell |
| 6308 | * their truesize. |
| 6309 | * When we pulled its content into skb->head, fragment |
| 6310 | * was freed, but __pskb_pull_tail() could not possibly |
| 6311 | * adjust skb->truesize, not knowing the frag truesize. |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 6312 | */ |
| 6313 | skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); |
| 6314 | } |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6315 | |
| 6316 | #ifdef CONFIG_SKB_EXTENSIONS |
| 6317 | static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) |
| 6318 | { |
| 6319 | return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); |
| 6320 | } |
| 6321 | |
Paolo Abeni | 8b69a80 | 2020-01-09 07:59:24 -0800 | [diff] [blame] | 6322 | /** |
| 6323 | * __skb_ext_alloc - allocate a new skb extensions storage |
| 6324 | * |
Florian Westphal | 4930f48 | 2020-05-16 10:46:23 +0200 | [diff] [blame] | 6325 | * @flags: See kmalloc(). |
| 6326 | * |
Paolo Abeni | 8b69a80 | 2020-01-09 07:59:24 -0800 | [diff] [blame] | 6327 | * Returns the newly allocated pointer. The pointer can later attached to a |
| 6328 | * skb via __skb_ext_set(). |
| 6329 | * Note: caller must handle the skb_ext as an opaque data. |
| 6330 | */ |
Florian Westphal | 4930f48 | 2020-05-16 10:46:23 +0200 | [diff] [blame] | 6331 | struct skb_ext *__skb_ext_alloc(gfp_t flags) |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6332 | { |
Florian Westphal | 4930f48 | 2020-05-16 10:46:23 +0200 | [diff] [blame] | 6333 | struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6334 | |
| 6335 | if (new) { |
| 6336 | memset(new->offset, 0, sizeof(new->offset)); |
| 6337 | refcount_set(&new->refcnt, 1); |
| 6338 | } |
| 6339 | |
| 6340 | return new; |
| 6341 | } |
| 6342 | |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 6343 | static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, |
| 6344 | unsigned int old_active) |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6345 | { |
| 6346 | struct skb_ext *new; |
| 6347 | |
| 6348 | if (refcount_read(&old->refcnt) == 1) |
| 6349 | return old; |
| 6350 | |
| 6351 | new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); |
| 6352 | if (!new) |
| 6353 | return NULL; |
| 6354 | |
| 6355 | memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); |
| 6356 | refcount_set(&new->refcnt, 1); |
| 6357 | |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 6358 | #ifdef CONFIG_XFRM |
| 6359 | if (old_active & (1 << SKB_EXT_SEC_PATH)) { |
| 6360 | struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); |
| 6361 | unsigned int i; |
| 6362 | |
| 6363 | for (i = 0; i < sp->len; i++) |
| 6364 | xfrm_state_hold(sp->xvec[i]); |
| 6365 | } |
| 6366 | #endif |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6367 | __skb_ext_put(old); |
| 6368 | return new; |
| 6369 | } |
| 6370 | |
| 6371 | /** |
Paolo Abeni | 8b69a80 | 2020-01-09 07:59:24 -0800 | [diff] [blame] | 6372 | * __skb_ext_set - attach the specified extension storage to this skb |
| 6373 | * @skb: buffer |
| 6374 | * @id: extension id |
| 6375 | * @ext: extension storage previously allocated via __skb_ext_alloc() |
| 6376 | * |
| 6377 | * Existing extensions, if any, are cleared. |
| 6378 | * |
| 6379 | * Returns the pointer to the extension. |
| 6380 | */ |
| 6381 | void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, |
| 6382 | struct skb_ext *ext) |
| 6383 | { |
| 6384 | unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); |
| 6385 | |
| 6386 | skb_ext_put(skb); |
| 6387 | newlen = newoff + skb_ext_type_len[id]; |
| 6388 | ext->chunks = newlen; |
| 6389 | ext->offset[id] = newoff; |
| 6390 | skb->extensions = ext; |
| 6391 | skb->active_extensions = 1 << id; |
| 6392 | return skb_ext_get_ptr(ext, id); |
| 6393 | } |
| 6394 | |
| 6395 | /** |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6396 | * skb_ext_add - allocate space for given extension, COW if needed |
| 6397 | * @skb: buffer |
| 6398 | * @id: extension to allocate space for |
| 6399 | * |
| 6400 | * Allocates enough space for the given extension. |
| 6401 | * If the extension is already present, a pointer to that extension |
| 6402 | * is returned. |
| 6403 | * |
| 6404 | * If the skb was cloned, COW applies and the returned memory can be |
| 6405 | * modified without changing the extension space of clones buffers. |
| 6406 | * |
| 6407 | * Returns pointer to the extension or NULL on allocation failure. |
| 6408 | */ |
| 6409 | void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) |
| 6410 | { |
| 6411 | struct skb_ext *new, *old = NULL; |
| 6412 | unsigned int newlen, newoff; |
| 6413 | |
| 6414 | if (skb->active_extensions) { |
| 6415 | old = skb->extensions; |
| 6416 | |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 6417 | new = skb_ext_maybe_cow(old, skb->active_extensions); |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6418 | if (!new) |
| 6419 | return NULL; |
| 6420 | |
Paolo Abeni | 682ec85 | 2018-12-21 19:03:15 +0100 | [diff] [blame] | 6421 | if (__skb_ext_exist(new, id)) |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6422 | goto set_active; |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6423 | |
Paolo Abeni | e94e50b | 2018-12-21 19:03:13 +0100 | [diff] [blame] | 6424 | newoff = new->chunks; |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6425 | } else { |
| 6426 | newoff = SKB_EXT_CHUNKSIZEOF(*new); |
| 6427 | |
Florian Westphal | 4930f48 | 2020-05-16 10:46:23 +0200 | [diff] [blame] | 6428 | new = __skb_ext_alloc(GFP_ATOMIC); |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6429 | if (!new) |
| 6430 | return NULL; |
| 6431 | } |
| 6432 | |
| 6433 | newlen = newoff + skb_ext_type_len[id]; |
| 6434 | new->chunks = newlen; |
| 6435 | new->offset[id] = newoff; |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6436 | set_active: |
Paolo Abeni | 682ec85 | 2018-12-21 19:03:15 +0100 | [diff] [blame] | 6437 | skb->extensions = new; |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6438 | skb->active_extensions |= 1 << id; |
| 6439 | return skb_ext_get_ptr(new, id); |
| 6440 | } |
| 6441 | EXPORT_SYMBOL(skb_ext_add); |
| 6442 | |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 6443 | #ifdef CONFIG_XFRM |
| 6444 | static void skb_ext_put_sp(struct sec_path *sp) |
| 6445 | { |
| 6446 | unsigned int i; |
| 6447 | |
| 6448 | for (i = 0; i < sp->len; i++) |
| 6449 | xfrm_state_put(sp->xvec[i]); |
| 6450 | } |
| 6451 | #endif |
| 6452 | |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6453 | void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) |
| 6454 | { |
| 6455 | struct skb_ext *ext = skb->extensions; |
| 6456 | |
| 6457 | skb->active_extensions &= ~(1 << id); |
| 6458 | if (skb->active_extensions == 0) { |
| 6459 | skb->extensions = NULL; |
| 6460 | __skb_ext_put(ext); |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 6461 | #ifdef CONFIG_XFRM |
| 6462 | } else if (id == SKB_EXT_SEC_PATH && |
| 6463 | refcount_read(&ext->refcnt) == 1) { |
| 6464 | struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); |
| 6465 | |
| 6466 | skb_ext_put_sp(sp); |
| 6467 | sp->len = 0; |
| 6468 | #endif |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6469 | } |
| 6470 | } |
| 6471 | EXPORT_SYMBOL(__skb_ext_del); |
| 6472 | |
| 6473 | void __skb_ext_put(struct skb_ext *ext) |
| 6474 | { |
| 6475 | /* If this is last clone, nothing can increment |
| 6476 | * it after check passes. Avoids one atomic op. |
| 6477 | */ |
| 6478 | if (refcount_read(&ext->refcnt) == 1) |
| 6479 | goto free_now; |
| 6480 | |
| 6481 | if (!refcount_dec_and_test(&ext->refcnt)) |
| 6482 | return; |
| 6483 | free_now: |
Florian Westphal | 4165079 | 2018-12-18 17:15:27 +0100 | [diff] [blame] | 6484 | #ifdef CONFIG_XFRM |
| 6485 | if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) |
| 6486 | skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); |
| 6487 | #endif |
| 6488 | |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 6489 | kmem_cache_free(skbuff_ext_cache, ext); |
| 6490 | } |
| 6491 | EXPORT_SYMBOL(__skb_ext_put); |
| 6492 | #endif /* CONFIG_SKB_EXTENSIONS */ |