| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Routines having to do with the 'struct sk_buff' memory handlers. |
| 4 | * |
| 5 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> |
| 6 | * Florian La Roche <rzsfl@rz.uni-sb.de> |
| 7 | * |
| 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). |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * The functions in this file will not compile correctly with gcc 2.4.x |
| 33 | */ |
| 34 | |
| 35 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 36 | |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/types.h> |
| 39 | #include <linux/kernel.h> |
| 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> |
| 45 | #include <linux/tcp.h> |
| 46 | #include <linux/udp.h> |
| 47 | #include <linux/sctp.h> |
| 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> |
| 54 | #include <linux/splice.h> |
| 55 | #include <linux/cache.h> |
| 56 | #include <linux/rtnetlink.h> |
| 57 | #include <linux/init.h> |
| 58 | #include <linux/scatterlist.h> |
| 59 | #include <linux/errqueue.h> |
| 60 | #include <linux/prefetch.h> |
| 61 | #include <linux/bitfield.h> |
| 62 | #include <linux/if_vlan.h> |
| 63 | #include <linux/mpls.h> |
| 64 | #include <linux/kcov.h> |
| 65 | #include <linux/iov_iter.h> |
| 66 | |
| 67 | #include <net/protocol.h> |
| 68 | #include <net/dst.h> |
| 69 | #include <net/sock.h> |
| 70 | #include <net/checksum.h> |
| 71 | #include <net/gso.h> |
| 72 | #include <net/ip6_checksum.h> |
| 73 | #include <net/xfrm.h> |
| 74 | #include <net/mpls.h> |
| 75 | #include <net/mptcp.h> |
| 76 | #include <net/mctp.h> |
| 77 | #include <net/page_pool/helpers.h> |
| 78 | #include <net/dropreason.h> |
| 79 | |
| 80 | #include <linux/uaccess.h> |
| 81 | #include <trace/events/skb.h> |
| 82 | #include <linux/highmem.h> |
| 83 | #include <linux/capability.h> |
| 84 | #include <linux/user_namespace.h> |
| 85 | #include <linux/indirect_call_wrapper.h> |
| 86 | #include <linux/textsearch.h> |
| 87 | |
| 88 | #include "dev.h" |
| 89 | #include "sock_destructor.h" |
| 90 | |
| 91 | struct kmem_cache *skbuff_cache __ro_after_init; |
| 92 | static struct kmem_cache *skbuff_fclone_cache __ro_after_init; |
| 93 | #ifdef CONFIG_SKB_EXTENSIONS |
| 94 | static struct kmem_cache *skbuff_ext_cache __ro_after_init; |
| 95 | #endif |
| 96 | |
| 97 | |
| 98 | static struct kmem_cache *skb_small_head_cache __ro_after_init; |
| 99 | |
| 100 | #define SKB_SMALL_HEAD_SIZE SKB_HEAD_ALIGN(MAX_TCP_HEADER) |
| 101 | |
| 102 | /* We want SKB_SMALL_HEAD_CACHE_SIZE to not be a power of two. |
| 103 | * This should ensure that SKB_SMALL_HEAD_HEADROOM is a unique |
| 104 | * size, and we can differentiate heads from skb_small_head_cache |
| 105 | * vs system slabs by looking at their size (skb_end_offset()). |
| 106 | */ |
| 107 | #define SKB_SMALL_HEAD_CACHE_SIZE \ |
| 108 | (is_power_of_2(SKB_SMALL_HEAD_SIZE) ? \ |
| 109 | (SKB_SMALL_HEAD_SIZE + L1_CACHE_BYTES) : \ |
| 110 | SKB_SMALL_HEAD_SIZE) |
| 111 | |
| 112 | #define SKB_SMALL_HEAD_HEADROOM \ |
| 113 | SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE) |
| 114 | |
| 115 | int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; |
| 116 | EXPORT_SYMBOL(sysctl_max_skb_frags); |
| 117 | |
| 118 | #undef FN |
| 119 | #define FN(reason) [SKB_DROP_REASON_##reason] = #reason, |
| 120 | static const char * const drop_reasons[] = { |
| 121 | [SKB_CONSUMED] = "CONSUMED", |
| 122 | DEFINE_DROP_REASON(FN, FN) |
| 123 | }; |
| 124 | |
| 125 | static const struct drop_reason_list drop_reasons_core = { |
| 126 | .reasons = drop_reasons, |
| 127 | .n_reasons = ARRAY_SIZE(drop_reasons), |
| 128 | }; |
| 129 | |
| 130 | const struct drop_reason_list __rcu * |
| 131 | drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_NUM] = { |
| 132 | [SKB_DROP_REASON_SUBSYS_CORE] = RCU_INITIALIZER(&drop_reasons_core), |
| 133 | }; |
| 134 | EXPORT_SYMBOL(drop_reasons_by_subsys); |
| 135 | |
| 136 | /** |
| 137 | * drop_reasons_register_subsys - register another drop reason subsystem |
| 138 | * @subsys: the subsystem to register, must not be the core |
| 139 | * @list: the list of drop reasons within the subsystem, must point to |
| 140 | * a statically initialized list |
| 141 | */ |
| 142 | void drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys, |
| 143 | const struct drop_reason_list *list) |
| 144 | { |
| 145 | if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE || |
| 146 | subsys >= ARRAY_SIZE(drop_reasons_by_subsys), |
| 147 | "invalid subsystem %d\n", subsys)) |
| 148 | return; |
| 149 | |
| 150 | /* must point to statically allocated memory, so INIT is OK */ |
| 151 | RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], list); |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(drop_reasons_register_subsys); |
| 154 | |
| 155 | /** |
| 156 | * drop_reasons_unregister_subsys - unregister a drop reason subsystem |
| 157 | * @subsys: the subsystem to remove, must not be the core |
| 158 | * |
| 159 | * Note: This will synchronize_rcu() to ensure no users when it returns. |
| 160 | */ |
| 161 | void drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys) |
| 162 | { |
| 163 | if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE || |
| 164 | subsys >= ARRAY_SIZE(drop_reasons_by_subsys), |
| 165 | "invalid subsystem %d\n", subsys)) |
| 166 | return; |
| 167 | |
| 168 | RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], NULL); |
| 169 | |
| 170 | synchronize_rcu(); |
| 171 | } |
| 172 | EXPORT_SYMBOL_GPL(drop_reasons_unregister_subsys); |
| 173 | |
| 174 | /** |
| 175 | * skb_panic - private function for out-of-line support |
| 176 | * @skb: buffer |
| 177 | * @sz: size |
| 178 | * @addr: address |
| 179 | * @msg: skb_over_panic or skb_under_panic |
| 180 | * |
| 181 | * Out-of-line support for skb_put() and skb_push(). |
| 182 | * Called via the wrapper skb_over_panic() or skb_under_panic(). |
| 183 | * Keep out of line to prevent kernel bloat. |
| 184 | * __builtin_return_address is not used because it is not always reliable. |
| 185 | */ |
| 186 | static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, |
| 187 | const char msg[]) |
| 188 | { |
| 189 | pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", |
| 190 | msg, addr, skb->len, sz, skb->head, skb->data, |
| 191 | (unsigned long)skb->tail, (unsigned long)skb->end, |
| 192 | skb->dev ? skb->dev->name : "<NULL>"); |
| 193 | BUG(); |
| 194 | } |
| 195 | |
| 196 | static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
| 197 | { |
| 198 | skb_panic(skb, sz, addr, __func__); |
| 199 | } |
| 200 | |
| 201 | static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
| 202 | { |
| 203 | skb_panic(skb, sz, addr, __func__); |
| 204 | } |
| 205 | |
| 206 | #define NAPI_SKB_CACHE_SIZE 64 |
| 207 | #define NAPI_SKB_CACHE_BULK 16 |
| 208 | #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) |
| 209 | |
| 210 | #if PAGE_SIZE == SZ_4K |
| 211 | |
| 212 | #define NAPI_HAS_SMALL_PAGE_FRAG 1 |
| 213 | #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc) |
| 214 | |
| 215 | /* specialized page frag allocator using a single order 0 page |
| 216 | * and slicing it into 1K sized fragment. Constrained to systems |
| 217 | * with a very limited amount of 1K fragments fitting a single |
| 218 | * page - to avoid excessive truesize underestimation |
| 219 | */ |
| 220 | |
| 221 | struct page_frag_1k { |
| 222 | void *va; |
| 223 | u16 offset; |
| 224 | bool pfmemalloc; |
| 225 | }; |
| 226 | |
| 227 | static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp) |
| 228 | { |
| 229 | struct page *page; |
| 230 | int offset; |
| 231 | |
| 232 | offset = nc->offset - SZ_1K; |
| 233 | if (likely(offset >= 0)) |
| 234 | goto use_frag; |
| 235 | |
| 236 | page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); |
| 237 | if (!page) |
| 238 | return NULL; |
| 239 | |
| 240 | nc->va = page_address(page); |
| 241 | nc->pfmemalloc = page_is_pfmemalloc(page); |
| 242 | offset = PAGE_SIZE - SZ_1K; |
| 243 | page_ref_add(page, offset / SZ_1K); |
| 244 | |
| 245 | use_frag: |
| 246 | nc->offset = offset; |
| 247 | return nc->va + offset; |
| 248 | } |
| 249 | #else |
| 250 | |
| 251 | /* the small page is actually unused in this build; add dummy helpers |
| 252 | * to please the compiler and avoid later preprocessor's conditionals |
| 253 | */ |
| 254 | #define NAPI_HAS_SMALL_PAGE_FRAG 0 |
| 255 | #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false |
| 256 | |
| 257 | struct page_frag_1k { |
| 258 | }; |
| 259 | |
| 260 | static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask) |
| 261 | { |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | #endif |
| 266 | |
| 267 | struct napi_alloc_cache { |
| 268 | struct page_frag_cache page; |
| 269 | struct page_frag_1k page_small; |
| 270 | unsigned int skb_count; |
| 271 | void *skb_cache[NAPI_SKB_CACHE_SIZE]; |
| 272 | }; |
| 273 | |
| 274 | static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); |
| 275 | static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); |
| 276 | |
| 277 | /* Double check that napi_get_frags() allocates skbs with |
| 278 | * skb->head being backed by slab, not a page fragment. |
| 279 | * This is to make sure bug fixed in 3226b158e67c |
| 280 | * ("net: avoid 32 x truesize under-estimation for tiny skbs") |
| 281 | * does not accidentally come back. |
| 282 | */ |
| 283 | void napi_get_frags_check(struct napi_struct *napi) |
| 284 | { |
| 285 | struct sk_buff *skb; |
| 286 | |
| 287 | local_bh_disable(); |
| 288 | skb = napi_get_frags(napi); |
| 289 | WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag); |
| 290 | napi_free_frags(napi); |
| 291 | local_bh_enable(); |
| 292 | } |
| 293 | |
| 294 | void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) |
| 295 | { |
| 296 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
| 297 | |
| 298 | fragsz = SKB_DATA_ALIGN(fragsz); |
| 299 | |
| 300 | return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); |
| 301 | } |
| 302 | EXPORT_SYMBOL(__napi_alloc_frag_align); |
| 303 | |
| 304 | void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) |
| 305 | { |
| 306 | void *data; |
| 307 | |
| 308 | fragsz = SKB_DATA_ALIGN(fragsz); |
| 309 | if (in_hardirq() || irqs_disabled()) { |
| 310 | struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); |
| 311 | |
| 312 | data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); |
| 313 | } else { |
| 314 | struct napi_alloc_cache *nc; |
| 315 | |
| 316 | local_bh_disable(); |
| 317 | nc = this_cpu_ptr(&napi_alloc_cache); |
| 318 | data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); |
| 319 | local_bh_enable(); |
| 320 | } |
| 321 | return data; |
| 322 | } |
| 323 | EXPORT_SYMBOL(__netdev_alloc_frag_align); |
| 324 | |
| 325 | static struct sk_buff *napi_skb_cache_get(void) |
| 326 | { |
| 327 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
| 328 | struct sk_buff *skb; |
| 329 | |
| 330 | if (unlikely(!nc->skb_count)) { |
| 331 | nc->skb_count = kmem_cache_alloc_bulk(skbuff_cache, |
| 332 | GFP_ATOMIC, |
| 333 | NAPI_SKB_CACHE_BULK, |
| 334 | nc->skb_cache); |
| 335 | if (unlikely(!nc->skb_count)) |
| 336 | return NULL; |
| 337 | } |
| 338 | |
| 339 | skb = nc->skb_cache[--nc->skb_count]; |
| 340 | kasan_unpoison_object_data(skbuff_cache, skb); |
| 341 | |
| 342 | return skb; |
| 343 | } |
| 344 | |
| 345 | static inline void __finalize_skb_around(struct sk_buff *skb, void *data, |
| 346 | unsigned int size) |
| 347 | { |
| 348 | struct skb_shared_info *shinfo; |
| 349 | |
| 350 | size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 351 | |
| 352 | /* Assumes caller memset cleared SKB */ |
| 353 | skb->truesize = SKB_TRUESIZE(size); |
| 354 | refcount_set(&skb->users, 1); |
| 355 | skb->head = data; |
| 356 | skb->data = data; |
| 357 | skb_reset_tail_pointer(skb); |
| 358 | skb_set_end_offset(skb, size); |
| 359 | skb->mac_header = (typeof(skb->mac_header))~0U; |
| 360 | skb->transport_header = (typeof(skb->transport_header))~0U; |
| 361 | skb->alloc_cpu = raw_smp_processor_id(); |
| 362 | /* make sure we initialize shinfo sequentially */ |
| 363 | shinfo = skb_shinfo(skb); |
| 364 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
| 365 | atomic_set(&shinfo->dataref, 1); |
| 366 | |
| 367 | skb_set_kcov_handle(skb, kcov_common_handle()); |
| 368 | } |
| 369 | |
| 370 | static inline void *__slab_build_skb(struct sk_buff *skb, void *data, |
| 371 | unsigned int *size) |
| 372 | { |
| 373 | void *resized; |
| 374 | |
| 375 | /* Must find the allocation size (and grow it to match). */ |
| 376 | *size = ksize(data); |
| 377 | /* krealloc() will immediately return "data" when |
| 378 | * "ksize(data)" is requested: it is the existing upper |
| 379 | * bounds. As a result, GFP_ATOMIC will be ignored. Note |
| 380 | * that this "new" pointer needs to be passed back to the |
| 381 | * caller for use so the __alloc_size hinting will be |
| 382 | * tracked correctly. |
| 383 | */ |
| 384 | resized = krealloc(data, *size, GFP_ATOMIC); |
| 385 | WARN_ON_ONCE(resized != data); |
| 386 | return resized; |
| 387 | } |
| 388 | |
| 389 | /* build_skb() variant which can operate on slab buffers. |
| 390 | * Note that this should be used sparingly as slab buffers |
| 391 | * cannot be combined efficiently by GRO! |
| 392 | */ |
| 393 | struct sk_buff *slab_build_skb(void *data) |
| 394 | { |
| 395 | struct sk_buff *skb; |
| 396 | unsigned int size; |
| 397 | |
| 398 | skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC); |
| 399 | if (unlikely(!skb)) |
| 400 | return NULL; |
| 401 | |
| 402 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
| 403 | data = __slab_build_skb(skb, data, &size); |
| 404 | __finalize_skb_around(skb, data, size); |
| 405 | |
| 406 | return skb; |
| 407 | } |
| 408 | EXPORT_SYMBOL(slab_build_skb); |
| 409 | |
| 410 | /* Caller must provide SKB that is memset cleared */ |
| 411 | static void __build_skb_around(struct sk_buff *skb, void *data, |
| 412 | unsigned int frag_size) |
| 413 | { |
| 414 | unsigned int size = frag_size; |
| 415 | |
| 416 | /* frag_size == 0 is considered deprecated now. Callers |
| 417 | * using slab buffer should use slab_build_skb() instead. |
| 418 | */ |
| 419 | if (WARN_ONCE(size == 0, "Use slab_build_skb() instead")) |
| 420 | data = __slab_build_skb(skb, data, &size); |
| 421 | |
| 422 | __finalize_skb_around(skb, data, size); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * __build_skb - build a network buffer |
| 427 | * @data: data buffer provided by caller |
| 428 | * @frag_size: size of data (must not be 0) |
| 429 | * |
| 430 | * Allocate a new &sk_buff. Caller provides space holding head and |
| 431 | * skb_shared_info. @data must have been allocated from the page |
| 432 | * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc() |
| 433 | * allocation is deprecated, and callers should use slab_build_skb() |
| 434 | * instead.) |
| 435 | * The return is the new skb buffer. |
| 436 | * On a failure the return is %NULL, and @data is not freed. |
| 437 | * Notes : |
| 438 | * Before IO, driver allocates only data buffer where NIC put incoming frame |
| 439 | * Driver should add room at head (NET_SKB_PAD) and |
| 440 | * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) |
| 441 | * After IO, driver calls build_skb(), to allocate sk_buff and populate it |
| 442 | * before giving packet to stack. |
| 443 | * RX rings only contains data buffers, not full skbs. |
| 444 | */ |
| 445 | struct sk_buff *__build_skb(void *data, unsigned int frag_size) |
| 446 | { |
| 447 | struct sk_buff *skb; |
| 448 | |
| 449 | skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC); |
| 450 | if (unlikely(!skb)) |
| 451 | return NULL; |
| 452 | |
| 453 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
| 454 | __build_skb_around(skb, data, frag_size); |
| 455 | |
| 456 | return skb; |
| 457 | } |
| 458 | |
| 459 | /* build_skb() is wrapper over __build_skb(), that specifically |
| 460 | * takes care of skb->head and skb->pfmemalloc |
| 461 | */ |
| 462 | struct sk_buff *build_skb(void *data, unsigned int frag_size) |
| 463 | { |
| 464 | struct sk_buff *skb = __build_skb(data, frag_size); |
| 465 | |
| 466 | if (likely(skb && frag_size)) { |
| 467 | skb->head_frag = 1; |
| 468 | skb_propagate_pfmemalloc(virt_to_head_page(data), skb); |
| 469 | } |
| 470 | return skb; |
| 471 | } |
| 472 | EXPORT_SYMBOL(build_skb); |
| 473 | |
| 474 | /** |
| 475 | * build_skb_around - build a network buffer around provided skb |
| 476 | * @skb: sk_buff provide by caller, must be memset cleared |
| 477 | * @data: data buffer provided by caller |
| 478 | * @frag_size: size of data |
| 479 | */ |
| 480 | struct sk_buff *build_skb_around(struct sk_buff *skb, |
| 481 | void *data, unsigned int frag_size) |
| 482 | { |
| 483 | if (unlikely(!skb)) |
| 484 | return NULL; |
| 485 | |
| 486 | __build_skb_around(skb, data, frag_size); |
| 487 | |
| 488 | if (frag_size) { |
| 489 | skb->head_frag = 1; |
| 490 | skb_propagate_pfmemalloc(virt_to_head_page(data), skb); |
| 491 | } |
| 492 | return skb; |
| 493 | } |
| 494 | EXPORT_SYMBOL(build_skb_around); |
| 495 | |
| 496 | /** |
| 497 | * __napi_build_skb - build a network buffer |
| 498 | * @data: data buffer provided by caller |
| 499 | * @frag_size: size of data |
| 500 | * |
| 501 | * Version of __build_skb() that uses NAPI percpu caches to obtain |
| 502 | * skbuff_head instead of inplace allocation. |
| 503 | * |
| 504 | * Returns a new &sk_buff on success, %NULL on allocation failure. |
| 505 | */ |
| 506 | static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) |
| 507 | { |
| 508 | struct sk_buff *skb; |
| 509 | |
| 510 | skb = napi_skb_cache_get(); |
| 511 | if (unlikely(!skb)) |
| 512 | return NULL; |
| 513 | |
| 514 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
| 515 | __build_skb_around(skb, data, frag_size); |
| 516 | |
| 517 | return skb; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * napi_build_skb - build a network buffer |
| 522 | * @data: data buffer provided by caller |
| 523 | * @frag_size: size of data |
| 524 | * |
| 525 | * Version of __napi_build_skb() that takes care of skb->head_frag |
| 526 | * and skb->pfmemalloc when the data is a page or page fragment. |
| 527 | * |
| 528 | * Returns a new &sk_buff on success, %NULL on allocation failure. |
| 529 | */ |
| 530 | struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) |
| 531 | { |
| 532 | struct sk_buff *skb = __napi_build_skb(data, frag_size); |
| 533 | |
| 534 | if (likely(skb) && frag_size) { |
| 535 | skb->head_frag = 1; |
| 536 | skb_propagate_pfmemalloc(virt_to_head_page(data), skb); |
| 537 | } |
| 538 | |
| 539 | return skb; |
| 540 | } |
| 541 | EXPORT_SYMBOL(napi_build_skb); |
| 542 | |
| 543 | /* |
| 544 | * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells |
| 545 | * the caller if emergency pfmemalloc reserves are being used. If it is and |
| 546 | * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves |
| 547 | * may be used. Otherwise, the packet data may be discarded until enough |
| 548 | * memory is free |
| 549 | */ |
| 550 | static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node, |
| 551 | bool *pfmemalloc) |
| 552 | { |
| 553 | bool ret_pfmemalloc = false; |
| 554 | size_t obj_size; |
| 555 | void *obj; |
| 556 | |
| 557 | obj_size = SKB_HEAD_ALIGN(*size); |
| 558 | if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE && |
| 559 | !(flags & KMALLOC_NOT_NORMAL_BITS)) { |
| 560 | obj = kmem_cache_alloc_node(skb_small_head_cache, |
| 561 | flags | __GFP_NOMEMALLOC | __GFP_NOWARN, |
| 562 | node); |
| 563 | *size = SKB_SMALL_HEAD_CACHE_SIZE; |
| 564 | if (obj || !(gfp_pfmemalloc_allowed(flags))) |
| 565 | goto out; |
| 566 | /* Try again but now we are using pfmemalloc reserves */ |
| 567 | ret_pfmemalloc = true; |
| 568 | obj = kmem_cache_alloc_node(skb_small_head_cache, flags, node); |
| 569 | goto out; |
| 570 | } |
| 571 | |
| 572 | obj_size = kmalloc_size_roundup(obj_size); |
| 573 | /* The following cast might truncate high-order bits of obj_size, this |
| 574 | * is harmless because kmalloc(obj_size >= 2^32) will fail anyway. |
| 575 | */ |
| 576 | *size = (unsigned int)obj_size; |
| 577 | |
| 578 | /* |
| 579 | * Try a regular allocation, when that fails and we're not entitled |
| 580 | * to the reserves, fail. |
| 581 | */ |
| 582 | obj = kmalloc_node_track_caller(obj_size, |
| 583 | flags | __GFP_NOMEMALLOC | __GFP_NOWARN, |
| 584 | node); |
| 585 | if (obj || !(gfp_pfmemalloc_allowed(flags))) |
| 586 | goto out; |
| 587 | |
| 588 | /* Try again but now we are using pfmemalloc reserves */ |
| 589 | ret_pfmemalloc = true; |
| 590 | obj = kmalloc_node_track_caller(obj_size, flags, node); |
| 591 | |
| 592 | out: |
| 593 | if (pfmemalloc) |
| 594 | *pfmemalloc = ret_pfmemalloc; |
| 595 | |
| 596 | return obj; |
| 597 | } |
| 598 | |
| 599 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
| 600 | * 'private' fields and also do memory statistics to find all the |
| 601 | * [BEEP] leaks. |
| 602 | * |
| 603 | */ |
| 604 | |
| 605 | /** |
| 606 | * __alloc_skb - allocate a network buffer |
| 607 | * @size: size to allocate |
| 608 | * @gfp_mask: allocation mask |
| 609 | * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache |
| 610 | * instead of head cache and allocate a cloned (child) skb. |
| 611 | * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for |
| 612 | * allocations in case the data is required for writeback |
| 613 | * @node: numa node to allocate memory on |
| 614 | * |
| 615 | * Allocate a new &sk_buff. The returned buffer has no headroom and a |
| 616 | * tail room of at least size bytes. The object has a reference count |
| 617 | * of one. The return is the buffer. On a failure the return is %NULL. |
| 618 | * |
| 619 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 620 | * %GFP_ATOMIC. |
| 621 | */ |
| 622 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
| 623 | int flags, int node) |
| 624 | { |
| 625 | struct kmem_cache *cache; |
| 626 | struct sk_buff *skb; |
| 627 | bool pfmemalloc; |
| 628 | u8 *data; |
| 629 | |
| 630 | cache = (flags & SKB_ALLOC_FCLONE) |
| 631 | ? skbuff_fclone_cache : skbuff_cache; |
| 632 | |
| 633 | if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) |
| 634 | gfp_mask |= __GFP_MEMALLOC; |
| 635 | |
| 636 | /* Get the HEAD */ |
| 637 | if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && |
| 638 | likely(node == NUMA_NO_NODE || node == numa_mem_id())) |
| 639 | skb = napi_skb_cache_get(); |
| 640 | else |
| 641 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); |
| 642 | if (unlikely(!skb)) |
| 643 | return NULL; |
| 644 | prefetchw(skb); |
| 645 | |
| 646 | /* We do our best to align skb_shared_info on a separate cache |
| 647 | * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives |
| 648 | * aligned memory blocks, unless SLUB/SLAB debug is enabled. |
| 649 | * Both skb->head and skb_shared_info are cache line aligned. |
| 650 | */ |
| 651 | data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc); |
| 652 | if (unlikely(!data)) |
| 653 | goto nodata; |
| 654 | /* kmalloc_size_roundup() might give us more room than requested. |
| 655 | * Put skb_shared_info exactly at the end of allocated zone, |
| 656 | * to allow max possible filling before reallocation. |
| 657 | */ |
| 658 | prefetchw(data + SKB_WITH_OVERHEAD(size)); |
| 659 | |
| 660 | /* |
| 661 | * Only clear those fields we need to clear, not those that we will |
| 662 | * actually initialise below. Hence, don't put any more fields after |
| 663 | * the tail pointer in struct sk_buff! |
| 664 | */ |
| 665 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
| 666 | __build_skb_around(skb, data, size); |
| 667 | skb->pfmemalloc = pfmemalloc; |
| 668 | |
| 669 | if (flags & SKB_ALLOC_FCLONE) { |
| 670 | struct sk_buff_fclones *fclones; |
| 671 | |
| 672 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
| 673 | |
| 674 | skb->fclone = SKB_FCLONE_ORIG; |
| 675 | refcount_set(&fclones->fclone_ref, 1); |
| 676 | } |
| 677 | |
| 678 | return skb; |
| 679 | |
| 680 | nodata: |
| 681 | kmem_cache_free(cache, skb); |
| 682 | return NULL; |
| 683 | } |
| 684 | EXPORT_SYMBOL(__alloc_skb); |
| 685 | |
| 686 | /** |
| 687 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device |
| 688 | * @dev: network device to receive on |
| 689 | * @len: length to allocate |
| 690 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
| 691 | * |
| 692 | * Allocate a new &sk_buff and assign it a usage count of one. The |
| 693 | * buffer has NET_SKB_PAD headroom built in. Users should allocate |
| 694 | * the headroom they think they need without accounting for the |
| 695 | * built in space. The built in space is used for optimisations. |
| 696 | * |
| 697 | * %NULL is returned if there is no free memory. |
| 698 | */ |
| 699 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, |
| 700 | gfp_t gfp_mask) |
| 701 | { |
| 702 | struct page_frag_cache *nc; |
| 703 | struct sk_buff *skb; |
| 704 | bool pfmemalloc; |
| 705 | void *data; |
| 706 | |
| 707 | len += NET_SKB_PAD; |
| 708 | |
| 709 | /* If requested length is either too small or too big, |
| 710 | * we use kmalloc() for skb->head allocation. |
| 711 | */ |
| 712 | if (len <= SKB_WITH_OVERHEAD(1024) || |
| 713 | len > SKB_WITH_OVERHEAD(PAGE_SIZE) || |
| 714 | (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { |
| 715 | skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); |
| 716 | if (!skb) |
| 717 | goto skb_fail; |
| 718 | goto skb_success; |
| 719 | } |
| 720 | |
| 721 | len = SKB_HEAD_ALIGN(len); |
| 722 | |
| 723 | if (sk_memalloc_socks()) |
| 724 | gfp_mask |= __GFP_MEMALLOC; |
| 725 | |
| 726 | if (in_hardirq() || irqs_disabled()) { |
| 727 | nc = this_cpu_ptr(&netdev_alloc_cache); |
| 728 | data = page_frag_alloc(nc, len, gfp_mask); |
| 729 | pfmemalloc = nc->pfmemalloc; |
| 730 | } else { |
| 731 | local_bh_disable(); |
| 732 | nc = this_cpu_ptr(&napi_alloc_cache.page); |
| 733 | data = page_frag_alloc(nc, len, gfp_mask); |
| 734 | pfmemalloc = nc->pfmemalloc; |
| 735 | local_bh_enable(); |
| 736 | } |
| 737 | |
| 738 | if (unlikely(!data)) |
| 739 | return NULL; |
| 740 | |
| 741 | skb = __build_skb(data, len); |
| 742 | if (unlikely(!skb)) { |
| 743 | skb_free_frag(data); |
| 744 | return NULL; |
| 745 | } |
| 746 | |
| 747 | if (pfmemalloc) |
| 748 | skb->pfmemalloc = 1; |
| 749 | skb->head_frag = 1; |
| 750 | |
| 751 | skb_success: |
| 752 | skb_reserve(skb, NET_SKB_PAD); |
| 753 | skb->dev = dev; |
| 754 | |
| 755 | skb_fail: |
| 756 | return skb; |
| 757 | } |
| 758 | EXPORT_SYMBOL(__netdev_alloc_skb); |
| 759 | |
| 760 | /** |
| 761 | * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance |
| 762 | * @napi: napi instance this buffer was allocated for |
| 763 | * @len: length to allocate |
| 764 | * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages |
| 765 | * |
| 766 | * Allocate a new sk_buff for use in NAPI receive. This buffer will |
| 767 | * attempt to allocate the head from a special reserved region used |
| 768 | * only for NAPI Rx allocation. By doing this we can save several |
| 769 | * CPU cycles by avoiding having to disable and re-enable IRQs. |
| 770 | * |
| 771 | * %NULL is returned if there is no free memory. |
| 772 | */ |
| 773 | struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, |
| 774 | gfp_t gfp_mask) |
| 775 | { |
| 776 | struct napi_alloc_cache *nc; |
| 777 | struct sk_buff *skb; |
| 778 | bool pfmemalloc; |
| 779 | void *data; |
| 780 | |
| 781 | DEBUG_NET_WARN_ON_ONCE(!in_softirq()); |
| 782 | len += NET_SKB_PAD + NET_IP_ALIGN; |
| 783 | |
| 784 | /* If requested length is either too small or too big, |
| 785 | * we use kmalloc() for skb->head allocation. |
| 786 | * When the small frag allocator is available, prefer it over kmalloc |
| 787 | * for small fragments |
| 788 | */ |
| 789 | if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) || |
| 790 | len > SKB_WITH_OVERHEAD(PAGE_SIZE) || |
| 791 | (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { |
| 792 | skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, |
| 793 | NUMA_NO_NODE); |
| 794 | if (!skb) |
| 795 | goto skb_fail; |
| 796 | goto skb_success; |
| 797 | } |
| 798 | |
| 799 | nc = this_cpu_ptr(&napi_alloc_cache); |
| 800 | |
| 801 | if (sk_memalloc_socks()) |
| 802 | gfp_mask |= __GFP_MEMALLOC; |
| 803 | |
| 804 | if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) { |
| 805 | /* we are artificially inflating the allocation size, but |
| 806 | * that is not as bad as it may look like, as: |
| 807 | * - 'len' less than GRO_MAX_HEAD makes little sense |
| 808 | * - On most systems, larger 'len' values lead to fragment |
| 809 | * size above 512 bytes |
| 810 | * - kmalloc would use the kmalloc-1k slab for such values |
| 811 | * - Builds with smaller GRO_MAX_HEAD will very likely do |
| 812 | * little networking, as that implies no WiFi and no |
| 813 | * tunnels support, and 32 bits arches. |
| 814 | */ |
| 815 | len = SZ_1K; |
| 816 | |
| 817 | data = page_frag_alloc_1k(&nc->page_small, gfp_mask); |
| 818 | pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small); |
| 819 | } else { |
| 820 | len = SKB_HEAD_ALIGN(len); |
| 821 | |
| 822 | data = page_frag_alloc(&nc->page, len, gfp_mask); |
| 823 | pfmemalloc = nc->page.pfmemalloc; |
| 824 | } |
| 825 | |
| 826 | if (unlikely(!data)) |
| 827 | return NULL; |
| 828 | |
| 829 | skb = __napi_build_skb(data, len); |
| 830 | if (unlikely(!skb)) { |
| 831 | skb_free_frag(data); |
| 832 | return NULL; |
| 833 | } |
| 834 | |
| 835 | if (pfmemalloc) |
| 836 | skb->pfmemalloc = 1; |
| 837 | skb->head_frag = 1; |
| 838 | |
| 839 | skb_success: |
| 840 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
| 841 | skb->dev = napi->dev; |
| 842 | |
| 843 | skb_fail: |
| 844 | return skb; |
| 845 | } |
| 846 | EXPORT_SYMBOL(__napi_alloc_skb); |
| 847 | |
| 848 | void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, |
| 849 | int size, unsigned int truesize) |
| 850 | { |
| 851 | DEBUG_NET_WARN_ON_ONCE(size > truesize); |
| 852 | |
| 853 | skb_fill_page_desc(skb, i, page, off, size); |
| 854 | skb->len += size; |
| 855 | skb->data_len += size; |
| 856 | skb->truesize += truesize; |
| 857 | } |
| 858 | EXPORT_SYMBOL(skb_add_rx_frag); |
| 859 | |
| 860 | void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, |
| 861 | unsigned int truesize) |
| 862 | { |
| 863 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 864 | |
| 865 | DEBUG_NET_WARN_ON_ONCE(size > truesize); |
| 866 | |
| 867 | skb_frag_size_add(frag, size); |
| 868 | skb->len += size; |
| 869 | skb->data_len += size; |
| 870 | skb->truesize += truesize; |
| 871 | } |
| 872 | EXPORT_SYMBOL(skb_coalesce_rx_frag); |
| 873 | |
| 874 | static void skb_drop_list(struct sk_buff **listp) |
| 875 | { |
| 876 | kfree_skb_list(*listp); |
| 877 | *listp = NULL; |
| 878 | } |
| 879 | |
| 880 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
| 881 | { |
| 882 | skb_drop_list(&skb_shinfo(skb)->frag_list); |
| 883 | } |
| 884 | |
| 885 | static void skb_clone_fraglist(struct sk_buff *skb) |
| 886 | { |
| 887 | struct sk_buff *list; |
| 888 | |
| 889 | skb_walk_frags(skb, list) |
| 890 | skb_get(list); |
| 891 | } |
| 892 | |
| 893 | static bool is_pp_page(struct page *page) |
| 894 | { |
| 895 | return (page->pp_magic & ~0x3UL) == PP_SIGNATURE; |
| 896 | } |
| 897 | |
| 898 | #if IS_ENABLED(CONFIG_PAGE_POOL) |
| 899 | bool napi_pp_put_page(struct page *page, bool napi_safe) |
| 900 | { |
| 901 | bool allow_direct = false; |
| 902 | struct page_pool *pp; |
| 903 | |
| 904 | page = compound_head(page); |
| 905 | |
| 906 | /* page->pp_magic is OR'ed with PP_SIGNATURE after the allocation |
| 907 | * in order to preserve any existing bits, such as bit 0 for the |
| 908 | * head page of compound page and bit 1 for pfmemalloc page, so |
| 909 | * mask those bits for freeing side when doing below checking, |
| 910 | * and page_is_pfmemalloc() is checked in __page_pool_put_page() |
| 911 | * to avoid recycling the pfmemalloc page. |
| 912 | */ |
| 913 | if (unlikely(!is_pp_page(page))) |
| 914 | return false; |
| 915 | |
| 916 | pp = page->pp; |
| 917 | |
| 918 | /* Allow direct recycle if we have reasons to believe that we are |
| 919 | * in the same context as the consumer would run, so there's |
| 920 | * no possible race. |
| 921 | * __page_pool_put_page() makes sure we're not in hardirq context |
| 922 | * and interrupts are enabled prior to accessing the cache. |
| 923 | */ |
| 924 | if (napi_safe || in_softirq()) { |
| 925 | const struct napi_struct *napi = READ_ONCE(pp->p.napi); |
| 926 | |
| 927 | allow_direct = napi && |
| 928 | READ_ONCE(napi->list_owner) == smp_processor_id(); |
| 929 | } |
| 930 | |
| 931 | /* Driver set this to memory recycling info. Reset it on recycle. |
| 932 | * This will *not* work for NIC using a split-page memory model. |
| 933 | * The page will be returned to the pool here regardless of the |
| 934 | * 'flipped' fragment being in use or not. |
| 935 | */ |
| 936 | page_pool_put_full_page(pp, page, allow_direct); |
| 937 | |
| 938 | return true; |
| 939 | } |
| 940 | EXPORT_SYMBOL(napi_pp_put_page); |
| 941 | #endif |
| 942 | |
| 943 | static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe) |
| 944 | { |
| 945 | if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) |
| 946 | return false; |
| 947 | return napi_pp_put_page(virt_to_page(data), napi_safe); |
| 948 | } |
| 949 | |
| 950 | static void skb_kfree_head(void *head, unsigned int end_offset) |
| 951 | { |
| 952 | if (end_offset == SKB_SMALL_HEAD_HEADROOM) |
| 953 | kmem_cache_free(skb_small_head_cache, head); |
| 954 | else |
| 955 | kfree(head); |
| 956 | } |
| 957 | |
| 958 | static void skb_free_head(struct sk_buff *skb, bool napi_safe) |
| 959 | { |
| 960 | unsigned char *head = skb->head; |
| 961 | |
| 962 | if (skb->head_frag) { |
| 963 | if (skb_pp_recycle(skb, head, napi_safe)) |
| 964 | return; |
| 965 | skb_free_frag(head); |
| 966 | } else { |
| 967 | skb_kfree_head(head, skb_end_offset(skb)); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason, |
| 972 | bool napi_safe) |
| 973 | { |
| 974 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 975 | int i; |
| 976 | |
| 977 | if (skb->cloned && |
| 978 | atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, |
| 979 | &shinfo->dataref)) |
| 980 | goto exit; |
| 981 | |
| 982 | if (skb_zcopy(skb)) { |
| 983 | bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS; |
| 984 | |
| 985 | skb_zcopy_clear(skb, true); |
| 986 | if (skip_unref) |
| 987 | goto free_head; |
| 988 | } |
| 989 | |
| 990 | for (i = 0; i < shinfo->nr_frags; i++) |
| 991 | napi_frag_unref(&shinfo->frags[i], skb->pp_recycle, napi_safe); |
| 992 | |
| 993 | free_head: |
| 994 | if (shinfo->frag_list) |
| 995 | kfree_skb_list_reason(shinfo->frag_list, reason); |
| 996 | |
| 997 | skb_free_head(skb, napi_safe); |
| 998 | exit: |
| 999 | /* When we clone an SKB we copy the reycling bit. The pp_recycle |
| 1000 | * bit is only set on the head though, so in order to avoid races |
| 1001 | * while trying to recycle fragments on __skb_frag_unref() we need |
| 1002 | * to make one SKB responsible for triggering the recycle path. |
| 1003 | * So disable the recycling bit if an SKB is cloned and we have |
| 1004 | * additional references to the fragmented part of the SKB. |
| 1005 | * Eventually the last SKB will have the recycling bit set and it's |
| 1006 | * dataref set to 0, which will trigger the recycling |
| 1007 | */ |
| 1008 | skb->pp_recycle = 0; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * Free an skbuff by memory without cleaning the state. |
| 1013 | */ |
| 1014 | static void kfree_skbmem(struct sk_buff *skb) |
| 1015 | { |
| 1016 | struct sk_buff_fclones *fclones; |
| 1017 | |
| 1018 | switch (skb->fclone) { |
| 1019 | case SKB_FCLONE_UNAVAILABLE: |
| 1020 | kmem_cache_free(skbuff_cache, skb); |
| 1021 | return; |
| 1022 | |
| 1023 | case SKB_FCLONE_ORIG: |
| 1024 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
| 1025 | |
| 1026 | /* We usually free the clone (TX completion) before original skb |
| 1027 | * This test would have no chance to be true for the clone, |
| 1028 | * while here, branch prediction will be good. |
| 1029 | */ |
| 1030 | if (refcount_read(&fclones->fclone_ref) == 1) |
| 1031 | goto fastpath; |
| 1032 | break; |
| 1033 | |
| 1034 | default: /* SKB_FCLONE_CLONE */ |
| 1035 | fclones = container_of(skb, struct sk_buff_fclones, skb2); |
| 1036 | break; |
| 1037 | } |
| 1038 | if (!refcount_dec_and_test(&fclones->fclone_ref)) |
| 1039 | return; |
| 1040 | fastpath: |
| 1041 | kmem_cache_free(skbuff_fclone_cache, fclones); |
| 1042 | } |
| 1043 | |
| 1044 | void skb_release_head_state(struct sk_buff *skb) |
| 1045 | { |
| 1046 | skb_dst_drop(skb); |
| 1047 | if (skb->destructor) { |
| 1048 | DEBUG_NET_WARN_ON_ONCE(in_hardirq()); |
| 1049 | skb->destructor(skb); |
| 1050 | } |
| 1051 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
| 1052 | nf_conntrack_put(skb_nfct(skb)); |
| 1053 | #endif |
| 1054 | skb_ext_put(skb); |
| 1055 | } |
| 1056 | |
| 1057 | /* Free everything but the sk_buff shell. */ |
| 1058 | static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason, |
| 1059 | bool napi_safe) |
| 1060 | { |
| 1061 | skb_release_head_state(skb); |
| 1062 | if (likely(skb->head)) |
| 1063 | skb_release_data(skb, reason, napi_safe); |
| 1064 | } |
| 1065 | |
| 1066 | /** |
| 1067 | * __kfree_skb - private function |
| 1068 | * @skb: buffer |
| 1069 | * |
| 1070 | * Free an sk_buff. Release anything attached to the buffer. |
| 1071 | * Clean the state. This is an internal helper function. Users should |
| 1072 | * always call kfree_skb |
| 1073 | */ |
| 1074 | |
| 1075 | void __kfree_skb(struct sk_buff *skb) |
| 1076 | { |
| 1077 | skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED, false); |
| 1078 | kfree_skbmem(skb); |
| 1079 | } |
| 1080 | EXPORT_SYMBOL(__kfree_skb); |
| 1081 | |
| 1082 | static __always_inline |
| 1083 | bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) |
| 1084 | { |
| 1085 | if (unlikely(!skb_unref(skb))) |
| 1086 | return false; |
| 1087 | |
| 1088 | DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET || |
| 1089 | u32_get_bits(reason, |
| 1090 | SKB_DROP_REASON_SUBSYS_MASK) >= |
| 1091 | SKB_DROP_REASON_SUBSYS_NUM); |
| 1092 | |
| 1093 | if (reason == SKB_CONSUMED) |
| 1094 | trace_consume_skb(skb, __builtin_return_address(0)); |
| 1095 | else |
| 1096 | trace_kfree_skb(skb, __builtin_return_address(0), reason); |
| 1097 | return true; |
| 1098 | } |
| 1099 | |
| 1100 | /** |
| 1101 | * kfree_skb_reason - free an sk_buff with special reason |
| 1102 | * @skb: buffer to free |
| 1103 | * @reason: reason why this skb is dropped |
| 1104 | * |
| 1105 | * Drop a reference to the buffer and free it if the usage count has |
| 1106 | * hit zero. Meanwhile, pass the drop reason to 'kfree_skb' |
| 1107 | * tracepoint. |
| 1108 | */ |
| 1109 | void __fix_address |
| 1110 | kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) |
| 1111 | { |
| 1112 | if (__kfree_skb_reason(skb, reason)) |
| 1113 | __kfree_skb(skb); |
| 1114 | } |
| 1115 | EXPORT_SYMBOL(kfree_skb_reason); |
| 1116 | |
| 1117 | #define KFREE_SKB_BULK_SIZE 16 |
| 1118 | |
| 1119 | struct skb_free_array { |
| 1120 | unsigned int skb_count; |
| 1121 | void *skb_array[KFREE_SKB_BULK_SIZE]; |
| 1122 | }; |
| 1123 | |
| 1124 | static void kfree_skb_add_bulk(struct sk_buff *skb, |
| 1125 | struct skb_free_array *sa, |
| 1126 | enum skb_drop_reason reason) |
| 1127 | { |
| 1128 | /* if SKB is a clone, don't handle this case */ |
| 1129 | if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) { |
| 1130 | __kfree_skb(skb); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | skb_release_all(skb, reason, false); |
| 1135 | sa->skb_array[sa->skb_count++] = skb; |
| 1136 | |
| 1137 | if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) { |
| 1138 | kmem_cache_free_bulk(skbuff_cache, KFREE_SKB_BULK_SIZE, |
| 1139 | sa->skb_array); |
| 1140 | sa->skb_count = 0; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | void __fix_address |
| 1145 | kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason) |
| 1146 | { |
| 1147 | struct skb_free_array sa; |
| 1148 | |
| 1149 | sa.skb_count = 0; |
| 1150 | |
| 1151 | while (segs) { |
| 1152 | struct sk_buff *next = segs->next; |
| 1153 | |
| 1154 | if (__kfree_skb_reason(segs, reason)) { |
| 1155 | skb_poison_list(segs); |
| 1156 | kfree_skb_add_bulk(segs, &sa, reason); |
| 1157 | } |
| 1158 | |
| 1159 | segs = next; |
| 1160 | } |
| 1161 | |
| 1162 | if (sa.skb_count) |
| 1163 | kmem_cache_free_bulk(skbuff_cache, sa.skb_count, sa.skb_array); |
| 1164 | } |
| 1165 | EXPORT_SYMBOL(kfree_skb_list_reason); |
| 1166 | |
| 1167 | /* Dump skb information and contents. |
| 1168 | * |
| 1169 | * Must only be called from net_ratelimit()-ed paths. |
| 1170 | * |
| 1171 | * Dumps whole packets if full_pkt, only headers otherwise. |
| 1172 | */ |
| 1173 | void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) |
| 1174 | { |
| 1175 | struct skb_shared_info *sh = skb_shinfo(skb); |
| 1176 | struct net_device *dev = skb->dev; |
| 1177 | struct sock *sk = skb->sk; |
| 1178 | struct sk_buff *list_skb; |
| 1179 | bool has_mac, has_trans; |
| 1180 | int headroom, tailroom; |
| 1181 | int i, len, seg_len; |
| 1182 | |
| 1183 | if (full_pkt) |
| 1184 | len = skb->len; |
| 1185 | else |
| 1186 | len = min_t(int, skb->len, MAX_HEADER + 128); |
| 1187 | |
| 1188 | headroom = skb_headroom(skb); |
| 1189 | tailroom = skb_tailroom(skb); |
| 1190 | |
| 1191 | has_mac = skb_mac_header_was_set(skb); |
| 1192 | has_trans = skb_transport_header_was_set(skb); |
| 1193 | |
| 1194 | printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" |
| 1195 | "mac=(%d,%d) net=(%d,%d) trans=%d\n" |
| 1196 | "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" |
| 1197 | "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" |
| 1198 | "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", |
| 1199 | level, skb->len, headroom, skb_headlen(skb), tailroom, |
| 1200 | has_mac ? skb->mac_header : -1, |
| 1201 | has_mac ? skb_mac_header_len(skb) : -1, |
| 1202 | skb->network_header, |
| 1203 | has_trans ? skb_network_header_len(skb) : -1, |
| 1204 | has_trans ? skb->transport_header : -1, |
| 1205 | sh->tx_flags, sh->nr_frags, |
| 1206 | sh->gso_size, sh->gso_type, sh->gso_segs, |
| 1207 | skb->csum, skb->ip_summed, skb->csum_complete_sw, |
| 1208 | skb->csum_valid, skb->csum_level, |
| 1209 | skb->hash, skb->sw_hash, skb->l4_hash, |
| 1210 | ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); |
| 1211 | |
| 1212 | if (dev) |
| 1213 | printk("%sdev name=%s feat=%pNF\n", |
| 1214 | level, dev->name, &dev->features); |
| 1215 | if (sk) |
| 1216 | printk("%ssk family=%hu type=%u proto=%u\n", |
| 1217 | level, sk->sk_family, sk->sk_type, sk->sk_protocol); |
| 1218 | |
| 1219 | if (full_pkt && headroom) |
| 1220 | print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, |
| 1221 | 16, 1, skb->head, headroom, false); |
| 1222 | |
| 1223 | seg_len = min_t(int, skb_headlen(skb), len); |
| 1224 | if (seg_len) |
| 1225 | print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, |
| 1226 | 16, 1, skb->data, seg_len, false); |
| 1227 | len -= seg_len; |
| 1228 | |
| 1229 | if (full_pkt && tailroom) |
| 1230 | print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, |
| 1231 | 16, 1, skb_tail_pointer(skb), tailroom, false); |
| 1232 | |
| 1233 | for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { |
| 1234 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1235 | u32 p_off, p_len, copied; |
| 1236 | struct page *p; |
| 1237 | u8 *vaddr; |
| 1238 | |
| 1239 | skb_frag_foreach_page(frag, skb_frag_off(frag), |
| 1240 | skb_frag_size(frag), p, p_off, p_len, |
| 1241 | copied) { |
| 1242 | seg_len = min_t(int, p_len, len); |
| 1243 | vaddr = kmap_atomic(p); |
| 1244 | print_hex_dump(level, "skb frag: ", |
| 1245 | DUMP_PREFIX_OFFSET, |
| 1246 | 16, 1, vaddr + p_off, seg_len, false); |
| 1247 | kunmap_atomic(vaddr); |
| 1248 | len -= seg_len; |
| 1249 | if (!len) |
| 1250 | break; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | if (full_pkt && skb_has_frag_list(skb)) { |
| 1255 | printk("skb fraglist:\n"); |
| 1256 | skb_walk_frags(skb, list_skb) |
| 1257 | skb_dump(level, list_skb, true); |
| 1258 | } |
| 1259 | } |
| 1260 | EXPORT_SYMBOL(skb_dump); |
| 1261 | |
| 1262 | /** |
| 1263 | * skb_tx_error - report an sk_buff xmit error |
| 1264 | * @skb: buffer that triggered an error |
| 1265 | * |
| 1266 | * Report xmit error if a device callback is tracking this skb. |
| 1267 | * skb must be freed afterwards. |
| 1268 | */ |
| 1269 | void skb_tx_error(struct sk_buff *skb) |
| 1270 | { |
| 1271 | if (skb) { |
| 1272 | skb_zcopy_downgrade_managed(skb); |
| 1273 | skb_zcopy_clear(skb, true); |
| 1274 | } |
| 1275 | } |
| 1276 | EXPORT_SYMBOL(skb_tx_error); |
| 1277 | |
| 1278 | #ifdef CONFIG_TRACEPOINTS |
| 1279 | /** |
| 1280 | * consume_skb - free an skbuff |
| 1281 | * @skb: buffer to free |
| 1282 | * |
| 1283 | * Drop a ref to the buffer and free it if the usage count has hit zero |
| 1284 | * Functions identically to kfree_skb, but kfree_skb assumes that the frame |
| 1285 | * is being dropped after a failure and notes that |
| 1286 | */ |
| 1287 | void consume_skb(struct sk_buff *skb) |
| 1288 | { |
| 1289 | if (!skb_unref(skb)) |
| 1290 | return; |
| 1291 | |
| 1292 | trace_consume_skb(skb, __builtin_return_address(0)); |
| 1293 | __kfree_skb(skb); |
| 1294 | } |
| 1295 | EXPORT_SYMBOL(consume_skb); |
| 1296 | #endif |
| 1297 | |
| 1298 | /** |
| 1299 | * __consume_stateless_skb - free an skbuff, assuming it is stateless |
| 1300 | * @skb: buffer to free |
| 1301 | * |
| 1302 | * Alike consume_skb(), but this variant assumes that this is the last |
| 1303 | * skb reference and all the head states have been already dropped |
| 1304 | */ |
| 1305 | void __consume_stateless_skb(struct sk_buff *skb) |
| 1306 | { |
| 1307 | trace_consume_skb(skb, __builtin_return_address(0)); |
| 1308 | skb_release_data(skb, SKB_CONSUMED, false); |
| 1309 | kfree_skbmem(skb); |
| 1310 | } |
| 1311 | |
| 1312 | static void napi_skb_cache_put(struct sk_buff *skb) |
| 1313 | { |
| 1314 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
| 1315 | u32 i; |
| 1316 | |
| 1317 | kasan_poison_object_data(skbuff_cache, skb); |
| 1318 | nc->skb_cache[nc->skb_count++] = skb; |
| 1319 | |
| 1320 | if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { |
| 1321 | for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) |
| 1322 | kasan_unpoison_object_data(skbuff_cache, |
| 1323 | nc->skb_cache[i]); |
| 1324 | |
| 1325 | kmem_cache_free_bulk(skbuff_cache, NAPI_SKB_CACHE_HALF, |
| 1326 | nc->skb_cache + NAPI_SKB_CACHE_HALF); |
| 1327 | nc->skb_count = NAPI_SKB_CACHE_HALF; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason) |
| 1332 | { |
| 1333 | skb_release_all(skb, reason, true); |
| 1334 | napi_skb_cache_put(skb); |
| 1335 | } |
| 1336 | |
| 1337 | void napi_skb_free_stolen_head(struct sk_buff *skb) |
| 1338 | { |
| 1339 | if (unlikely(skb->slow_gro)) { |
| 1340 | nf_reset_ct(skb); |
| 1341 | skb_dst_drop(skb); |
| 1342 | skb_ext_put(skb); |
| 1343 | skb_orphan(skb); |
| 1344 | skb->slow_gro = 0; |
| 1345 | } |
| 1346 | napi_skb_cache_put(skb); |
| 1347 | } |
| 1348 | |
| 1349 | void napi_consume_skb(struct sk_buff *skb, int budget) |
| 1350 | { |
| 1351 | /* Zero budget indicate non-NAPI context called us, like netpoll */ |
| 1352 | if (unlikely(!budget)) { |
| 1353 | dev_consume_skb_any(skb); |
| 1354 | return; |
| 1355 | } |
| 1356 | |
| 1357 | DEBUG_NET_WARN_ON_ONCE(!in_softirq()); |
| 1358 | |
| 1359 | if (!skb_unref(skb)) |
| 1360 | return; |
| 1361 | |
| 1362 | /* if reaching here SKB is ready to free */ |
| 1363 | trace_consume_skb(skb, __builtin_return_address(0)); |
| 1364 | |
| 1365 | /* if SKB is a clone, don't handle this case */ |
| 1366 | if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { |
| 1367 | __kfree_skb(skb); |
| 1368 | return; |
| 1369 | } |
| 1370 | |
| 1371 | skb_release_all(skb, SKB_CONSUMED, !!budget); |
| 1372 | napi_skb_cache_put(skb); |
| 1373 | } |
| 1374 | EXPORT_SYMBOL(napi_consume_skb); |
| 1375 | |
| 1376 | /* Make sure a field is contained by headers group */ |
| 1377 | #define CHECK_SKB_FIELD(field) \ |
| 1378 | BUILD_BUG_ON(offsetof(struct sk_buff, field) != \ |
| 1379 | offsetof(struct sk_buff, headers.field)); \ |
| 1380 | |
| 1381 | static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 1382 | { |
| 1383 | new->tstamp = old->tstamp; |
| 1384 | /* We do not copy old->sk */ |
| 1385 | new->dev = old->dev; |
| 1386 | memcpy(new->cb, old->cb, sizeof(old->cb)); |
| 1387 | skb_dst_copy(new, old); |
| 1388 | __skb_ext_copy(new, old); |
| 1389 | __nf_copy(new, old, false); |
| 1390 | |
| 1391 | /* Note : this field could be in the headers group. |
| 1392 | * It is not yet because we do not want to have a 16 bit hole |
| 1393 | */ |
| 1394 | new->queue_mapping = old->queue_mapping; |
| 1395 | |
| 1396 | memcpy(&new->headers, &old->headers, sizeof(new->headers)); |
| 1397 | CHECK_SKB_FIELD(protocol); |
| 1398 | CHECK_SKB_FIELD(csum); |
| 1399 | CHECK_SKB_FIELD(hash); |
| 1400 | CHECK_SKB_FIELD(priority); |
| 1401 | CHECK_SKB_FIELD(skb_iif); |
| 1402 | CHECK_SKB_FIELD(vlan_proto); |
| 1403 | CHECK_SKB_FIELD(vlan_tci); |
| 1404 | CHECK_SKB_FIELD(transport_header); |
| 1405 | CHECK_SKB_FIELD(network_header); |
| 1406 | CHECK_SKB_FIELD(mac_header); |
| 1407 | CHECK_SKB_FIELD(inner_protocol); |
| 1408 | CHECK_SKB_FIELD(inner_transport_header); |
| 1409 | CHECK_SKB_FIELD(inner_network_header); |
| 1410 | CHECK_SKB_FIELD(inner_mac_header); |
| 1411 | CHECK_SKB_FIELD(mark); |
| 1412 | #ifdef CONFIG_NETWORK_SECMARK |
| 1413 | CHECK_SKB_FIELD(secmark); |
| 1414 | #endif |
| 1415 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 1416 | CHECK_SKB_FIELD(napi_id); |
| 1417 | #endif |
| 1418 | CHECK_SKB_FIELD(alloc_cpu); |
| 1419 | #ifdef CONFIG_XPS |
| 1420 | CHECK_SKB_FIELD(sender_cpu); |
| 1421 | #endif |
| 1422 | #ifdef CONFIG_NET_SCHED |
| 1423 | CHECK_SKB_FIELD(tc_index); |
| 1424 | #endif |
| 1425 | |
| 1426 | } |
| 1427 | |
| 1428 | /* |
| 1429 | * You should not add any new code to this function. Add it to |
| 1430 | * __copy_skb_header above instead. |
| 1431 | */ |
| 1432 | static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) |
| 1433 | { |
| 1434 | #define C(x) n->x = skb->x |
| 1435 | |
| 1436 | n->next = n->prev = NULL; |
| 1437 | n->sk = NULL; |
| 1438 | __copy_skb_header(n, skb); |
| 1439 | |
| 1440 | C(len); |
| 1441 | C(data_len); |
| 1442 | C(mac_len); |
| 1443 | n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; |
| 1444 | n->cloned = 1; |
| 1445 | n->nohdr = 0; |
| 1446 | n->peeked = 0; |
| 1447 | C(pfmemalloc); |
| 1448 | C(pp_recycle); |
| 1449 | n->destructor = NULL; |
| 1450 | C(tail); |
| 1451 | C(end); |
| 1452 | C(head); |
| 1453 | C(head_frag); |
| 1454 | C(data); |
| 1455 | C(truesize); |
| 1456 | refcount_set(&n->users, 1); |
| 1457 | |
| 1458 | atomic_inc(&(skb_shinfo(skb)->dataref)); |
| 1459 | skb->cloned = 1; |
| 1460 | |
| 1461 | return n; |
| 1462 | #undef C |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg |
| 1467 | * @first: first sk_buff of the msg |
| 1468 | */ |
| 1469 | struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) |
| 1470 | { |
| 1471 | struct sk_buff *n; |
| 1472 | |
| 1473 | n = alloc_skb(0, GFP_ATOMIC); |
| 1474 | if (!n) |
| 1475 | return NULL; |
| 1476 | |
| 1477 | n->len = first->len; |
| 1478 | n->data_len = first->len; |
| 1479 | n->truesize = first->truesize; |
| 1480 | |
| 1481 | skb_shinfo(n)->frag_list = first; |
| 1482 | |
| 1483 | __copy_skb_header(n, first); |
| 1484 | n->destructor = NULL; |
| 1485 | |
| 1486 | return n; |
| 1487 | } |
| 1488 | EXPORT_SYMBOL_GPL(alloc_skb_for_msg); |
| 1489 | |
| 1490 | /** |
| 1491 | * skb_morph - morph one skb into another |
| 1492 | * @dst: the skb to receive the contents |
| 1493 | * @src: the skb to supply the contents |
| 1494 | * |
| 1495 | * This is identical to skb_clone except that the target skb is |
| 1496 | * supplied by the user. |
| 1497 | * |
| 1498 | * The target skb is returned upon exit. |
| 1499 | */ |
| 1500 | struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) |
| 1501 | { |
| 1502 | skb_release_all(dst, SKB_CONSUMED, false); |
| 1503 | return __skb_clone(dst, src); |
| 1504 | } |
| 1505 | EXPORT_SYMBOL_GPL(skb_morph); |
| 1506 | |
| 1507 | int mm_account_pinned_pages(struct mmpin *mmp, size_t size) |
| 1508 | { |
| 1509 | unsigned long max_pg, num_pg, new_pg, old_pg, rlim; |
| 1510 | struct user_struct *user; |
| 1511 | |
| 1512 | if (capable(CAP_IPC_LOCK) || !size) |
| 1513 | return 0; |
| 1514 | |
| 1515 | rlim = rlimit(RLIMIT_MEMLOCK); |
| 1516 | if (rlim == RLIM_INFINITY) |
| 1517 | return 0; |
| 1518 | |
| 1519 | num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ |
| 1520 | max_pg = rlim >> PAGE_SHIFT; |
| 1521 | user = mmp->user ? : current_user(); |
| 1522 | |
| 1523 | old_pg = atomic_long_read(&user->locked_vm); |
| 1524 | do { |
| 1525 | new_pg = old_pg + num_pg; |
| 1526 | if (new_pg > max_pg) |
| 1527 | return -ENOBUFS; |
| 1528 | } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg)); |
| 1529 | |
| 1530 | if (!mmp->user) { |
| 1531 | mmp->user = get_uid(user); |
| 1532 | mmp->num_pg = num_pg; |
| 1533 | } else { |
| 1534 | mmp->num_pg += num_pg; |
| 1535 | } |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | EXPORT_SYMBOL_GPL(mm_account_pinned_pages); |
| 1540 | |
| 1541 | void mm_unaccount_pinned_pages(struct mmpin *mmp) |
| 1542 | { |
| 1543 | if (mmp->user) { |
| 1544 | atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); |
| 1545 | free_uid(mmp->user); |
| 1546 | } |
| 1547 | } |
| 1548 | EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); |
| 1549 | |
| 1550 | static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) |
| 1551 | { |
| 1552 | struct ubuf_info_msgzc *uarg; |
| 1553 | struct sk_buff *skb; |
| 1554 | |
| 1555 | WARN_ON_ONCE(!in_task()); |
| 1556 | |
| 1557 | skb = sock_omalloc(sk, 0, GFP_KERNEL); |
| 1558 | if (!skb) |
| 1559 | return NULL; |
| 1560 | |
| 1561 | BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); |
| 1562 | uarg = (void *)skb->cb; |
| 1563 | uarg->mmp.user = NULL; |
| 1564 | |
| 1565 | if (mm_account_pinned_pages(&uarg->mmp, size)) { |
| 1566 | kfree_skb(skb); |
| 1567 | return NULL; |
| 1568 | } |
| 1569 | |
| 1570 | uarg->ubuf.callback = msg_zerocopy_callback; |
| 1571 | uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; |
| 1572 | uarg->len = 1; |
| 1573 | uarg->bytelen = size; |
| 1574 | uarg->zerocopy = 1; |
| 1575 | uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN; |
| 1576 | refcount_set(&uarg->ubuf.refcnt, 1); |
| 1577 | sock_hold(sk); |
| 1578 | |
| 1579 | return &uarg->ubuf; |
| 1580 | } |
| 1581 | |
| 1582 | static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg) |
| 1583 | { |
| 1584 | return container_of((void *)uarg, struct sk_buff, cb); |
| 1585 | } |
| 1586 | |
| 1587 | struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, |
| 1588 | struct ubuf_info *uarg) |
| 1589 | { |
| 1590 | if (uarg) { |
| 1591 | struct ubuf_info_msgzc *uarg_zc; |
| 1592 | const u32 byte_limit = 1 << 19; /* limit to a few TSO */ |
| 1593 | u32 bytelen, next; |
| 1594 | |
| 1595 | /* there might be non MSG_ZEROCOPY users */ |
| 1596 | if (uarg->callback != msg_zerocopy_callback) |
| 1597 | return NULL; |
| 1598 | |
| 1599 | /* realloc only when socket is locked (TCP, UDP cork), |
| 1600 | * so uarg->len and sk_zckey access is serialized |
| 1601 | */ |
| 1602 | if (!sock_owned_by_user(sk)) { |
| 1603 | WARN_ON_ONCE(1); |
| 1604 | return NULL; |
| 1605 | } |
| 1606 | |
| 1607 | uarg_zc = uarg_to_msgzc(uarg); |
| 1608 | bytelen = uarg_zc->bytelen + size; |
| 1609 | if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) { |
| 1610 | /* TCP can create new skb to attach new uarg */ |
| 1611 | if (sk->sk_type == SOCK_STREAM) |
| 1612 | goto new_alloc; |
| 1613 | return NULL; |
| 1614 | } |
| 1615 | |
| 1616 | next = (u32)atomic_read(&sk->sk_zckey); |
| 1617 | if ((u32)(uarg_zc->id + uarg_zc->len) == next) { |
| 1618 | if (mm_account_pinned_pages(&uarg_zc->mmp, size)) |
| 1619 | return NULL; |
| 1620 | uarg_zc->len++; |
| 1621 | uarg_zc->bytelen = bytelen; |
| 1622 | atomic_set(&sk->sk_zckey, ++next); |
| 1623 | |
| 1624 | /* no extra ref when appending to datagram (MSG_MORE) */ |
| 1625 | if (sk->sk_type == SOCK_STREAM) |
| 1626 | net_zcopy_get(uarg); |
| 1627 | |
| 1628 | return uarg; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | new_alloc: |
| 1633 | return msg_zerocopy_alloc(sk, size); |
| 1634 | } |
| 1635 | EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); |
| 1636 | |
| 1637 | static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) |
| 1638 | { |
| 1639 | struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); |
| 1640 | u32 old_lo, old_hi; |
| 1641 | u64 sum_len; |
| 1642 | |
| 1643 | old_lo = serr->ee.ee_info; |
| 1644 | old_hi = serr->ee.ee_data; |
| 1645 | sum_len = old_hi - old_lo + 1ULL + len; |
| 1646 | |
| 1647 | if (sum_len >= (1ULL << 32)) |
| 1648 | return false; |
| 1649 | |
| 1650 | if (lo != old_hi + 1) |
| 1651 | return false; |
| 1652 | |
| 1653 | serr->ee.ee_data += len; |
| 1654 | return true; |
| 1655 | } |
| 1656 | |
| 1657 | static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg) |
| 1658 | { |
| 1659 | struct sk_buff *tail, *skb = skb_from_uarg(uarg); |
| 1660 | struct sock_exterr_skb *serr; |
| 1661 | struct sock *sk = skb->sk; |
| 1662 | struct sk_buff_head *q; |
| 1663 | unsigned long flags; |
| 1664 | bool is_zerocopy; |
| 1665 | u32 lo, hi; |
| 1666 | u16 len; |
| 1667 | |
| 1668 | mm_unaccount_pinned_pages(&uarg->mmp); |
| 1669 | |
| 1670 | /* if !len, there was only 1 call, and it was aborted |
| 1671 | * so do not queue a completion notification |
| 1672 | */ |
| 1673 | if (!uarg->len || sock_flag(sk, SOCK_DEAD)) |
| 1674 | goto release; |
| 1675 | |
| 1676 | len = uarg->len; |
| 1677 | lo = uarg->id; |
| 1678 | hi = uarg->id + len - 1; |
| 1679 | is_zerocopy = uarg->zerocopy; |
| 1680 | |
| 1681 | serr = SKB_EXT_ERR(skb); |
| 1682 | memset(serr, 0, sizeof(*serr)); |
| 1683 | serr->ee.ee_errno = 0; |
| 1684 | serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; |
| 1685 | serr->ee.ee_data = hi; |
| 1686 | serr->ee.ee_info = lo; |
| 1687 | if (!is_zerocopy) |
| 1688 | serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; |
| 1689 | |
| 1690 | q = &sk->sk_error_queue; |
| 1691 | spin_lock_irqsave(&q->lock, flags); |
| 1692 | tail = skb_peek_tail(q); |
| 1693 | if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || |
| 1694 | !skb_zerocopy_notify_extend(tail, lo, len)) { |
| 1695 | __skb_queue_tail(q, skb); |
| 1696 | skb = NULL; |
| 1697 | } |
| 1698 | spin_unlock_irqrestore(&q->lock, flags); |
| 1699 | |
| 1700 | sk_error_report(sk); |
| 1701 | |
| 1702 | release: |
| 1703 | consume_skb(skb); |
| 1704 | sock_put(sk); |
| 1705 | } |
| 1706 | |
| 1707 | void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, |
| 1708 | bool success) |
| 1709 | { |
| 1710 | struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg); |
| 1711 | |
| 1712 | uarg_zc->zerocopy = uarg_zc->zerocopy & success; |
| 1713 | |
| 1714 | if (refcount_dec_and_test(&uarg->refcnt)) |
| 1715 | __msg_zerocopy_callback(uarg_zc); |
| 1716 | } |
| 1717 | EXPORT_SYMBOL_GPL(msg_zerocopy_callback); |
| 1718 | |
| 1719 | void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) |
| 1720 | { |
| 1721 | struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk; |
| 1722 | |
| 1723 | atomic_dec(&sk->sk_zckey); |
| 1724 | uarg_to_msgzc(uarg)->len--; |
| 1725 | |
| 1726 | if (have_uref) |
| 1727 | msg_zerocopy_callback(NULL, uarg, true); |
| 1728 | } |
| 1729 | EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); |
| 1730 | |
| 1731 | int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, |
| 1732 | struct msghdr *msg, int len, |
| 1733 | struct ubuf_info *uarg) |
| 1734 | { |
| 1735 | struct ubuf_info *orig_uarg = skb_zcopy(skb); |
| 1736 | int err, orig_len = skb->len; |
| 1737 | |
| 1738 | /* An skb can only point to one uarg. This edge case happens when |
| 1739 | * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. |
| 1740 | */ |
| 1741 | if (orig_uarg && uarg != orig_uarg) |
| 1742 | return -EEXIST; |
| 1743 | |
| 1744 | err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len); |
| 1745 | if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { |
| 1746 | struct sock *save_sk = skb->sk; |
| 1747 | |
| 1748 | /* Streams do not free skb on error. Reset to prev state. */ |
| 1749 | iov_iter_revert(&msg->msg_iter, skb->len - orig_len); |
| 1750 | skb->sk = sk; |
| 1751 | ___pskb_trim(skb, orig_len); |
| 1752 | skb->sk = save_sk; |
| 1753 | return err; |
| 1754 | } |
| 1755 | |
| 1756 | skb_zcopy_set(skb, uarg, NULL); |
| 1757 | return skb->len - orig_len; |
| 1758 | } |
| 1759 | EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); |
| 1760 | |
| 1761 | void __skb_zcopy_downgrade_managed(struct sk_buff *skb) |
| 1762 | { |
| 1763 | int i; |
| 1764 | |
| 1765 | skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS; |
| 1766 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 1767 | skb_frag_ref(skb, i); |
| 1768 | } |
| 1769 | EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed); |
| 1770 | |
| 1771 | static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, |
| 1772 | gfp_t gfp_mask) |
| 1773 | { |
| 1774 | if (skb_zcopy(orig)) { |
| 1775 | if (skb_zcopy(nskb)) { |
| 1776 | /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ |
| 1777 | if (!gfp_mask) { |
| 1778 | WARN_ON_ONCE(1); |
| 1779 | return -ENOMEM; |
| 1780 | } |
| 1781 | if (skb_uarg(nskb) == skb_uarg(orig)) |
| 1782 | return 0; |
| 1783 | if (skb_copy_ubufs(nskb, GFP_ATOMIC)) |
| 1784 | return -EIO; |
| 1785 | } |
| 1786 | skb_zcopy_set(nskb, skb_uarg(orig), NULL); |
| 1787 | } |
| 1788 | return 0; |
| 1789 | } |
| 1790 | |
| 1791 | /** |
| 1792 | * skb_copy_ubufs - copy userspace skb frags buffers to kernel |
| 1793 | * @skb: the skb to modify |
| 1794 | * @gfp_mask: allocation priority |
| 1795 | * |
| 1796 | * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. |
| 1797 | * It will copy all frags into kernel and drop the reference |
| 1798 | * to userspace pages. |
| 1799 | * |
| 1800 | * If this function is called from an interrupt gfp_mask() must be |
| 1801 | * %GFP_ATOMIC. |
| 1802 | * |
| 1803 | * Returns 0 on success or a negative error code on failure |
| 1804 | * to allocate kernel memory to copy to. |
| 1805 | */ |
| 1806 | int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) |
| 1807 | { |
| 1808 | int num_frags = skb_shinfo(skb)->nr_frags; |
| 1809 | struct page *page, *head = NULL; |
| 1810 | int i, order, psize, new_frags; |
| 1811 | u32 d_off; |
| 1812 | |
| 1813 | if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) |
| 1814 | return -EINVAL; |
| 1815 | |
| 1816 | if (!num_frags) |
| 1817 | goto release; |
| 1818 | |
| 1819 | /* We might have to allocate high order pages, so compute what minimum |
| 1820 | * page order is needed. |
| 1821 | */ |
| 1822 | order = 0; |
| 1823 | while ((PAGE_SIZE << order) * MAX_SKB_FRAGS < __skb_pagelen(skb)) |
| 1824 | order++; |
| 1825 | psize = (PAGE_SIZE << order); |
| 1826 | |
| 1827 | new_frags = (__skb_pagelen(skb) + psize - 1) >> (PAGE_SHIFT + order); |
| 1828 | for (i = 0; i < new_frags; i++) { |
| 1829 | page = alloc_pages(gfp_mask | __GFP_COMP, order); |
| 1830 | if (!page) { |
| 1831 | while (head) { |
| 1832 | struct page *next = (struct page *)page_private(head); |
| 1833 | put_page(head); |
| 1834 | head = next; |
| 1835 | } |
| 1836 | return -ENOMEM; |
| 1837 | } |
| 1838 | set_page_private(page, (unsigned long)head); |
| 1839 | head = page; |
| 1840 | } |
| 1841 | |
| 1842 | page = head; |
| 1843 | d_off = 0; |
| 1844 | for (i = 0; i < num_frags; i++) { |
| 1845 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
| 1846 | u32 p_off, p_len, copied; |
| 1847 | struct page *p; |
| 1848 | u8 *vaddr; |
| 1849 | |
| 1850 | skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), |
| 1851 | p, p_off, p_len, copied) { |
| 1852 | u32 copy, done = 0; |
| 1853 | vaddr = kmap_atomic(p); |
| 1854 | |
| 1855 | while (done < p_len) { |
| 1856 | if (d_off == psize) { |
| 1857 | d_off = 0; |
| 1858 | page = (struct page *)page_private(page); |
| 1859 | } |
| 1860 | copy = min_t(u32, psize - d_off, p_len - done); |
| 1861 | memcpy(page_address(page) + d_off, |
| 1862 | vaddr + p_off + done, copy); |
| 1863 | done += copy; |
| 1864 | d_off += copy; |
| 1865 | } |
| 1866 | kunmap_atomic(vaddr); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | /* skb frags release userspace buffers */ |
| 1871 | for (i = 0; i < num_frags; i++) |
| 1872 | skb_frag_unref(skb, i); |
| 1873 | |
| 1874 | /* skb frags point to kernel buffers */ |
| 1875 | for (i = 0; i < new_frags - 1; i++) { |
| 1876 | __skb_fill_page_desc(skb, i, head, 0, psize); |
| 1877 | head = (struct page *)page_private(head); |
| 1878 | } |
| 1879 | __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); |
| 1880 | skb_shinfo(skb)->nr_frags = new_frags; |
| 1881 | |
| 1882 | release: |
| 1883 | skb_zcopy_clear(skb, false); |
| 1884 | return 0; |
| 1885 | } |
| 1886 | EXPORT_SYMBOL_GPL(skb_copy_ubufs); |
| 1887 | |
| 1888 | /** |
| 1889 | * skb_clone - duplicate an sk_buff |
| 1890 | * @skb: buffer to clone |
| 1891 | * @gfp_mask: allocation priority |
| 1892 | * |
| 1893 | * Duplicate an &sk_buff. The new one is not owned by a socket. Both |
| 1894 | * copies share the same packet data but not structure. The new |
| 1895 | * buffer has a reference count of 1. If the allocation fails the |
| 1896 | * function returns %NULL otherwise the new buffer is returned. |
| 1897 | * |
| 1898 | * If this function is called from an interrupt gfp_mask() must be |
| 1899 | * %GFP_ATOMIC. |
| 1900 | */ |
| 1901 | |
| 1902 | struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) |
| 1903 | { |
| 1904 | struct sk_buff_fclones *fclones = container_of(skb, |
| 1905 | struct sk_buff_fclones, |
| 1906 | skb1); |
| 1907 | struct sk_buff *n; |
| 1908 | |
| 1909 | if (skb_orphan_frags(skb, gfp_mask)) |
| 1910 | return NULL; |
| 1911 | |
| 1912 | if (skb->fclone == SKB_FCLONE_ORIG && |
| 1913 | refcount_read(&fclones->fclone_ref) == 1) { |
| 1914 | n = &fclones->skb2; |
| 1915 | refcount_set(&fclones->fclone_ref, 2); |
| 1916 | n->fclone = SKB_FCLONE_CLONE; |
| 1917 | } else { |
| 1918 | if (skb_pfmemalloc(skb)) |
| 1919 | gfp_mask |= __GFP_MEMALLOC; |
| 1920 | |
| 1921 | n = kmem_cache_alloc(skbuff_cache, gfp_mask); |
| 1922 | if (!n) |
| 1923 | return NULL; |
| 1924 | |
| 1925 | n->fclone = SKB_FCLONE_UNAVAILABLE; |
| 1926 | } |
| 1927 | |
| 1928 | return __skb_clone(n, skb); |
| 1929 | } |
| 1930 | EXPORT_SYMBOL(skb_clone); |
| 1931 | |
| 1932 | void skb_headers_offset_update(struct sk_buff *skb, int off) |
| 1933 | { |
| 1934 | /* Only adjust this if it actually is csum_start rather than csum */ |
| 1935 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 1936 | skb->csum_start += off; |
| 1937 | /* {transport,network,mac}_header and tail are relative to skb->head */ |
| 1938 | skb->transport_header += off; |
| 1939 | skb->network_header += off; |
| 1940 | if (skb_mac_header_was_set(skb)) |
| 1941 | skb->mac_header += off; |
| 1942 | skb->inner_transport_header += off; |
| 1943 | skb->inner_network_header += off; |
| 1944 | skb->inner_mac_header += off; |
| 1945 | } |
| 1946 | EXPORT_SYMBOL(skb_headers_offset_update); |
| 1947 | |
| 1948 | void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) |
| 1949 | { |
| 1950 | __copy_skb_header(new, old); |
| 1951 | |
| 1952 | skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; |
| 1953 | skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; |
| 1954 | skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; |
| 1955 | } |
| 1956 | EXPORT_SYMBOL(skb_copy_header); |
| 1957 | |
| 1958 | static inline int skb_alloc_rx_flag(const struct sk_buff *skb) |
| 1959 | { |
| 1960 | if (skb_pfmemalloc(skb)) |
| 1961 | return SKB_ALLOC_RX; |
| 1962 | return 0; |
| 1963 | } |
| 1964 | |
| 1965 | /** |
| 1966 | * skb_copy - create private copy of an sk_buff |
| 1967 | * @skb: buffer to copy |
| 1968 | * @gfp_mask: allocation priority |
| 1969 | * |
| 1970 | * Make a copy of both an &sk_buff and its data. This is used when the |
| 1971 | * caller wishes to modify the data and needs a private copy of the |
| 1972 | * data to alter. Returns %NULL on failure or the pointer to the buffer |
| 1973 | * on success. The returned buffer has a reference count of 1. |
| 1974 | * |
| 1975 | * As by-product this function converts non-linear &sk_buff to linear |
| 1976 | * one, so that &sk_buff becomes completely private and caller is allowed |
| 1977 | * to modify all the data of returned buffer. This means that this |
| 1978 | * function is not recommended for use in circumstances when only |
| 1979 | * header is going to be modified. Use pskb_copy() instead. |
| 1980 | */ |
| 1981 | |
| 1982 | struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) |
| 1983 | { |
| 1984 | int headerlen = skb_headroom(skb); |
| 1985 | unsigned int size = skb_end_offset(skb) + skb->data_len; |
| 1986 | struct sk_buff *n = __alloc_skb(size, gfp_mask, |
| 1987 | skb_alloc_rx_flag(skb), NUMA_NO_NODE); |
| 1988 | |
| 1989 | if (!n) |
| 1990 | return NULL; |
| 1991 | |
| 1992 | /* Set the data pointer */ |
| 1993 | skb_reserve(n, headerlen); |
| 1994 | /* Set the tail pointer and length */ |
| 1995 | skb_put(n, skb->len); |
| 1996 | |
| 1997 | BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); |
| 1998 | |
| 1999 | skb_copy_header(n, skb); |
| 2000 | return n; |
| 2001 | } |
| 2002 | EXPORT_SYMBOL(skb_copy); |
| 2003 | |
| 2004 | /** |
| 2005 | * __pskb_copy_fclone - create copy of an sk_buff with private head. |
| 2006 | * @skb: buffer to copy |
| 2007 | * @headroom: headroom of new skb |
| 2008 | * @gfp_mask: allocation priority |
| 2009 | * @fclone: if true allocate the copy of the skb from the fclone |
| 2010 | * cache instead of the head cache; it is recommended to set this |
| 2011 | * to true for the cases where the copy will likely be cloned |
| 2012 | * |
| 2013 | * Make a copy of both an &sk_buff and part of its data, located |
| 2014 | * in header. Fragmented data remain shared. This is used when |
| 2015 | * the caller wishes to modify only header of &sk_buff and needs |
| 2016 | * private copy of the header to alter. Returns %NULL on failure |
| 2017 | * or the pointer to the buffer on success. |
| 2018 | * The returned buffer has a reference count of 1. |
| 2019 | */ |
| 2020 | |
| 2021 | struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, |
| 2022 | gfp_t gfp_mask, bool fclone) |
| 2023 | { |
| 2024 | unsigned int size = skb_headlen(skb) + headroom; |
| 2025 | int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); |
| 2026 | struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); |
| 2027 | |
| 2028 | if (!n) |
| 2029 | goto out; |
| 2030 | |
| 2031 | /* Set the data pointer */ |
| 2032 | skb_reserve(n, headroom); |
| 2033 | /* Set the tail pointer and length */ |
| 2034 | skb_put(n, skb_headlen(skb)); |
| 2035 | /* Copy the bytes */ |
| 2036 | skb_copy_from_linear_data(skb, n->data, n->len); |
| 2037 | |
| 2038 | n->truesize += skb->data_len; |
| 2039 | n->data_len = skb->data_len; |
| 2040 | n->len = skb->len; |
| 2041 | |
| 2042 | if (skb_shinfo(skb)->nr_frags) { |
| 2043 | int i; |
| 2044 | |
| 2045 | if (skb_orphan_frags(skb, gfp_mask) || |
| 2046 | skb_zerocopy_clone(n, skb, gfp_mask)) { |
| 2047 | kfree_skb(n); |
| 2048 | n = NULL; |
| 2049 | goto out; |
| 2050 | } |
| 2051 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 2052 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 2053 | skb_frag_ref(skb, i); |
| 2054 | } |
| 2055 | skb_shinfo(n)->nr_frags = i; |
| 2056 | } |
| 2057 | |
| 2058 | if (skb_has_frag_list(skb)) { |
| 2059 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; |
| 2060 | skb_clone_fraglist(n); |
| 2061 | } |
| 2062 | |
| 2063 | skb_copy_header(n, skb); |
| 2064 | out: |
| 2065 | return n; |
| 2066 | } |
| 2067 | EXPORT_SYMBOL(__pskb_copy_fclone); |
| 2068 | |
| 2069 | /** |
| 2070 | * pskb_expand_head - reallocate header of &sk_buff |
| 2071 | * @skb: buffer to reallocate |
| 2072 | * @nhead: room to add at head |
| 2073 | * @ntail: room to add at tail |
| 2074 | * @gfp_mask: allocation priority |
| 2075 | * |
| 2076 | * Expands (or creates identical copy, if @nhead and @ntail are zero) |
| 2077 | * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have |
| 2078 | * reference count of 1. Returns zero in the case of success or error, |
| 2079 | * if expansion failed. In the last case, &sk_buff is not changed. |
| 2080 | * |
| 2081 | * All the pointers pointing into skb header may change and must be |
| 2082 | * reloaded after call to this function. |
| 2083 | */ |
| 2084 | |
| 2085 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, |
| 2086 | gfp_t gfp_mask) |
| 2087 | { |
| 2088 | unsigned int osize = skb_end_offset(skb); |
| 2089 | unsigned int size = osize + nhead + ntail; |
| 2090 | long off; |
| 2091 | u8 *data; |
| 2092 | int i; |
| 2093 | |
| 2094 | BUG_ON(nhead < 0); |
| 2095 | |
| 2096 | BUG_ON(skb_shared(skb)); |
| 2097 | |
| 2098 | skb_zcopy_downgrade_managed(skb); |
| 2099 | |
| 2100 | if (skb_pfmemalloc(skb)) |
| 2101 | gfp_mask |= __GFP_MEMALLOC; |
| 2102 | |
| 2103 | data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); |
| 2104 | if (!data) |
| 2105 | goto nodata; |
| 2106 | size = SKB_WITH_OVERHEAD(size); |
| 2107 | |
| 2108 | /* Copy only real data... and, alas, header. This should be |
| 2109 | * optimized for the cases when header is void. |
| 2110 | */ |
| 2111 | memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); |
| 2112 | |
| 2113 | memcpy((struct skb_shared_info *)(data + size), |
| 2114 | skb_shinfo(skb), |
| 2115 | offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); |
| 2116 | |
| 2117 | /* |
| 2118 | * if shinfo is shared we must drop the old head gracefully, but if it |
| 2119 | * is not we can just drop the old head and let the existing refcount |
| 2120 | * be since all we did is relocate the values |
| 2121 | */ |
| 2122 | if (skb_cloned(skb)) { |
| 2123 | if (skb_orphan_frags(skb, gfp_mask)) |
| 2124 | goto nofrags; |
| 2125 | if (skb_zcopy(skb)) |
| 2126 | refcount_inc(&skb_uarg(skb)->refcnt); |
| 2127 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 2128 | skb_frag_ref(skb, i); |
| 2129 | |
| 2130 | if (skb_has_frag_list(skb)) |
| 2131 | skb_clone_fraglist(skb); |
| 2132 | |
| 2133 | skb_release_data(skb, SKB_CONSUMED, false); |
| 2134 | } else { |
| 2135 | skb_free_head(skb, false); |
| 2136 | } |
| 2137 | off = (data + nhead) - skb->head; |
| 2138 | |
| 2139 | skb->head = data; |
| 2140 | skb->head_frag = 0; |
| 2141 | skb->data += off; |
| 2142 | |
| 2143 | skb_set_end_offset(skb, size); |
| 2144 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 2145 | off = nhead; |
| 2146 | #endif |
| 2147 | skb->tail += off; |
| 2148 | skb_headers_offset_update(skb, nhead); |
| 2149 | skb->cloned = 0; |
| 2150 | skb->hdr_len = 0; |
| 2151 | skb->nohdr = 0; |
| 2152 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 2153 | |
| 2154 | skb_metadata_clear(skb); |
| 2155 | |
| 2156 | /* It is not generally safe to change skb->truesize. |
| 2157 | * For the moment, we really care of rx path, or |
| 2158 | * when skb is orphaned (not attached to a socket). |
| 2159 | */ |
| 2160 | if (!skb->sk || skb->destructor == sock_edemux) |
| 2161 | skb->truesize += size - osize; |
| 2162 | |
| 2163 | return 0; |
| 2164 | |
| 2165 | nofrags: |
| 2166 | skb_kfree_head(data, size); |
| 2167 | nodata: |
| 2168 | return -ENOMEM; |
| 2169 | } |
| 2170 | EXPORT_SYMBOL(pskb_expand_head); |
| 2171 | |
| 2172 | /* Make private copy of skb with writable head and some headroom */ |
| 2173 | |
| 2174 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) |
| 2175 | { |
| 2176 | struct sk_buff *skb2; |
| 2177 | int delta = headroom - skb_headroom(skb); |
| 2178 | |
| 2179 | if (delta <= 0) |
| 2180 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
| 2181 | else { |
| 2182 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 2183 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, |
| 2184 | GFP_ATOMIC)) { |
| 2185 | kfree_skb(skb2); |
| 2186 | skb2 = NULL; |
| 2187 | } |
| 2188 | } |
| 2189 | return skb2; |
| 2190 | } |
| 2191 | EXPORT_SYMBOL(skb_realloc_headroom); |
| 2192 | |
| 2193 | /* Note: We plan to rework this in linux-6.4 */ |
| 2194 | int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) |
| 2195 | { |
| 2196 | unsigned int saved_end_offset, saved_truesize; |
| 2197 | struct skb_shared_info *shinfo; |
| 2198 | int res; |
| 2199 | |
| 2200 | saved_end_offset = skb_end_offset(skb); |
| 2201 | saved_truesize = skb->truesize; |
| 2202 | |
| 2203 | res = pskb_expand_head(skb, 0, 0, pri); |
| 2204 | if (res) |
| 2205 | return res; |
| 2206 | |
| 2207 | skb->truesize = saved_truesize; |
| 2208 | |
| 2209 | if (likely(skb_end_offset(skb) == saved_end_offset)) |
| 2210 | return 0; |
| 2211 | |
| 2212 | /* We can not change skb->end if the original or new value |
| 2213 | * is SKB_SMALL_HEAD_HEADROOM, as it might break skb_kfree_head(). |
| 2214 | */ |
| 2215 | if (saved_end_offset == SKB_SMALL_HEAD_HEADROOM || |
| 2216 | skb_end_offset(skb) == SKB_SMALL_HEAD_HEADROOM) { |
| 2217 | /* We think this path should not be taken. |
| 2218 | * Add a temporary trace to warn us just in case. |
| 2219 | */ |
| 2220 | pr_err_once("__skb_unclone_keeptruesize() skb_end_offset() %u -> %u\n", |
| 2221 | saved_end_offset, skb_end_offset(skb)); |
| 2222 | WARN_ON_ONCE(1); |
| 2223 | return 0; |
| 2224 | } |
| 2225 | |
| 2226 | shinfo = skb_shinfo(skb); |
| 2227 | |
| 2228 | /* We are about to change back skb->end, |
| 2229 | * we need to move skb_shinfo() to its new location. |
| 2230 | */ |
| 2231 | memmove(skb->head + saved_end_offset, |
| 2232 | shinfo, |
| 2233 | offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); |
| 2234 | |
| 2235 | skb_set_end_offset(skb, saved_end_offset); |
| 2236 | |
| 2237 | return 0; |
| 2238 | } |
| 2239 | |
| 2240 | /** |
| 2241 | * skb_expand_head - reallocate header of &sk_buff |
| 2242 | * @skb: buffer to reallocate |
| 2243 | * @headroom: needed headroom |
| 2244 | * |
| 2245 | * Unlike skb_realloc_headroom, this one does not allocate a new skb |
| 2246 | * if possible; copies skb->sk to new skb as needed |
| 2247 | * and frees original skb in case of failures. |
| 2248 | * |
| 2249 | * It expect increased headroom and generates warning otherwise. |
| 2250 | */ |
| 2251 | |
| 2252 | struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) |
| 2253 | { |
| 2254 | int delta = headroom - skb_headroom(skb); |
| 2255 | int osize = skb_end_offset(skb); |
| 2256 | struct sock *sk = skb->sk; |
| 2257 | |
| 2258 | if (WARN_ONCE(delta <= 0, |
| 2259 | "%s is expecting an increase in the headroom", __func__)) |
| 2260 | return skb; |
| 2261 | |
| 2262 | delta = SKB_DATA_ALIGN(delta); |
| 2263 | /* pskb_expand_head() might crash, if skb is shared. */ |
| 2264 | if (skb_shared(skb) || !is_skb_wmem(skb)) { |
| 2265 | struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); |
| 2266 | |
| 2267 | if (unlikely(!nskb)) |
| 2268 | goto fail; |
| 2269 | |
| 2270 | if (sk) |
| 2271 | skb_set_owner_w(nskb, sk); |
| 2272 | consume_skb(skb); |
| 2273 | skb = nskb; |
| 2274 | } |
| 2275 | if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) |
| 2276 | goto fail; |
| 2277 | |
| 2278 | if (sk && is_skb_wmem(skb)) { |
| 2279 | delta = skb_end_offset(skb) - osize; |
| 2280 | refcount_add(delta, &sk->sk_wmem_alloc); |
| 2281 | skb->truesize += delta; |
| 2282 | } |
| 2283 | return skb; |
| 2284 | |
| 2285 | fail: |
| 2286 | kfree_skb(skb); |
| 2287 | return NULL; |
| 2288 | } |
| 2289 | EXPORT_SYMBOL(skb_expand_head); |
| 2290 | |
| 2291 | /** |
| 2292 | * skb_copy_expand - copy and expand sk_buff |
| 2293 | * @skb: buffer to copy |
| 2294 | * @newheadroom: new free bytes at head |
| 2295 | * @newtailroom: new free bytes at tail |
| 2296 | * @gfp_mask: allocation priority |
| 2297 | * |
| 2298 | * Make a copy of both an &sk_buff and its data and while doing so |
| 2299 | * allocate additional space. |
| 2300 | * |
| 2301 | * This is used when the caller wishes to modify the data and needs a |
| 2302 | * private copy of the data to alter as well as more space for new fields. |
| 2303 | * Returns %NULL on failure or the pointer to the buffer |
| 2304 | * on success. The returned buffer has a reference count of 1. |
| 2305 | * |
| 2306 | * You must pass %GFP_ATOMIC as the allocation priority if this function |
| 2307 | * is called from an interrupt. |
| 2308 | */ |
| 2309 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, |
| 2310 | int newheadroom, int newtailroom, |
| 2311 | gfp_t gfp_mask) |
| 2312 | { |
| 2313 | /* |
| 2314 | * Allocate the copy buffer |
| 2315 | */ |
| 2316 | struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, |
| 2317 | gfp_mask, skb_alloc_rx_flag(skb), |
| 2318 | NUMA_NO_NODE); |
| 2319 | int oldheadroom = skb_headroom(skb); |
| 2320 | int head_copy_len, head_copy_off; |
| 2321 | |
| 2322 | if (!n) |
| 2323 | return NULL; |
| 2324 | |
| 2325 | skb_reserve(n, newheadroom); |
| 2326 | |
| 2327 | /* Set the tail pointer and length */ |
| 2328 | skb_put(n, skb->len); |
| 2329 | |
| 2330 | head_copy_len = oldheadroom; |
| 2331 | head_copy_off = 0; |
| 2332 | if (newheadroom <= head_copy_len) |
| 2333 | head_copy_len = newheadroom; |
| 2334 | else |
| 2335 | head_copy_off = newheadroom - head_copy_len; |
| 2336 | |
| 2337 | /* Copy the linear header and data. */ |
| 2338 | BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, |
| 2339 | skb->len + head_copy_len)); |
| 2340 | |
| 2341 | skb_copy_header(n, skb); |
| 2342 | |
| 2343 | skb_headers_offset_update(n, newheadroom - oldheadroom); |
| 2344 | |
| 2345 | return n; |
| 2346 | } |
| 2347 | EXPORT_SYMBOL(skb_copy_expand); |
| 2348 | |
| 2349 | /** |
| 2350 | * __skb_pad - zero pad the tail of an skb |
| 2351 | * @skb: buffer to pad |
| 2352 | * @pad: space to pad |
| 2353 | * @free_on_error: free buffer on error |
| 2354 | * |
| 2355 | * Ensure that a buffer is followed by a padding area that is zero |
| 2356 | * filled. Used by network drivers which may DMA or transfer data |
| 2357 | * beyond the buffer end onto the wire. |
| 2358 | * |
| 2359 | * May return error in out of memory cases. The skb is freed on error |
| 2360 | * if @free_on_error is true. |
| 2361 | */ |
| 2362 | |
| 2363 | int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) |
| 2364 | { |
| 2365 | int err; |
| 2366 | int ntail; |
| 2367 | |
| 2368 | /* If the skbuff is non linear tailroom is always zero.. */ |
| 2369 | if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { |
| 2370 | memset(skb->data+skb->len, 0, pad); |
| 2371 | return 0; |
| 2372 | } |
| 2373 | |
| 2374 | ntail = skb->data_len + pad - (skb->end - skb->tail); |
| 2375 | if (likely(skb_cloned(skb) || ntail > 0)) { |
| 2376 | err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); |
| 2377 | if (unlikely(err)) |
| 2378 | goto free_skb; |
| 2379 | } |
| 2380 | |
| 2381 | /* FIXME: The use of this function with non-linear skb's really needs |
| 2382 | * to be audited. |
| 2383 | */ |
| 2384 | err = skb_linearize(skb); |
| 2385 | if (unlikely(err)) |
| 2386 | goto free_skb; |
| 2387 | |
| 2388 | memset(skb->data + skb->len, 0, pad); |
| 2389 | return 0; |
| 2390 | |
| 2391 | free_skb: |
| 2392 | if (free_on_error) |
| 2393 | kfree_skb(skb); |
| 2394 | return err; |
| 2395 | } |
| 2396 | EXPORT_SYMBOL(__skb_pad); |
| 2397 | |
| 2398 | /** |
| 2399 | * pskb_put - add data to the tail of a potentially fragmented buffer |
| 2400 | * @skb: start of the buffer to use |
| 2401 | * @tail: tail fragment of the buffer to use |
| 2402 | * @len: amount of data to add |
| 2403 | * |
| 2404 | * This function extends the used data area of the potentially |
| 2405 | * fragmented buffer. @tail must be the last fragment of @skb -- or |
| 2406 | * @skb itself. If this would exceed the total buffer size the kernel |
| 2407 | * will panic. A pointer to the first byte of the extra data is |
| 2408 | * returned. |
| 2409 | */ |
| 2410 | |
| 2411 | void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) |
| 2412 | { |
| 2413 | if (tail != skb) { |
| 2414 | skb->data_len += len; |
| 2415 | skb->len += len; |
| 2416 | } |
| 2417 | return skb_put(tail, len); |
| 2418 | } |
| 2419 | EXPORT_SYMBOL_GPL(pskb_put); |
| 2420 | |
| 2421 | /** |
| 2422 | * skb_put - add data to a buffer |
| 2423 | * @skb: buffer to use |
| 2424 | * @len: amount of data to add |
| 2425 | * |
| 2426 | * This function extends the used data area of the buffer. If this would |
| 2427 | * exceed the total buffer size the kernel will panic. A pointer to the |
| 2428 | * first byte of the extra data is returned. |
| 2429 | */ |
| 2430 | void *skb_put(struct sk_buff *skb, unsigned int len) |
| 2431 | { |
| 2432 | void *tmp = skb_tail_pointer(skb); |
| 2433 | SKB_LINEAR_ASSERT(skb); |
| 2434 | skb->tail += len; |
| 2435 | skb->len += len; |
| 2436 | if (unlikely(skb->tail > skb->end)) |
| 2437 | skb_over_panic(skb, len, __builtin_return_address(0)); |
| 2438 | return tmp; |
| 2439 | } |
| 2440 | EXPORT_SYMBOL(skb_put); |
| 2441 | |
| 2442 | /** |
| 2443 | * skb_push - add data to the start of a buffer |
| 2444 | * @skb: buffer to use |
| 2445 | * @len: amount of data to add |
| 2446 | * |
| 2447 | * This function extends the used data area of the buffer at the buffer |
| 2448 | * start. If this would exceed the total buffer headroom the kernel will |
| 2449 | * panic. A pointer to the first byte of the extra data is returned. |
| 2450 | */ |
| 2451 | void *skb_push(struct sk_buff *skb, unsigned int len) |
| 2452 | { |
| 2453 | skb->data -= len; |
| 2454 | skb->len += len; |
| 2455 | if (unlikely(skb->data < skb->head)) |
| 2456 | skb_under_panic(skb, len, __builtin_return_address(0)); |
| 2457 | return skb->data; |
| 2458 | } |
| 2459 | EXPORT_SYMBOL(skb_push); |
| 2460 | |
| 2461 | /** |
| 2462 | * skb_pull - remove data from the start of a buffer |
| 2463 | * @skb: buffer to use |
| 2464 | * @len: amount of data to remove |
| 2465 | * |
| 2466 | * This function removes data from the start of a buffer, returning |
| 2467 | * the memory to the headroom. A pointer to the next data in the buffer |
| 2468 | * is returned. Once the data has been pulled future pushes will overwrite |
| 2469 | * the old data. |
| 2470 | */ |
| 2471 | void *skb_pull(struct sk_buff *skb, unsigned int len) |
| 2472 | { |
| 2473 | return skb_pull_inline(skb, len); |
| 2474 | } |
| 2475 | EXPORT_SYMBOL(skb_pull); |
| 2476 | |
| 2477 | /** |
| 2478 | * skb_pull_data - remove data from the start of a buffer returning its |
| 2479 | * original position. |
| 2480 | * @skb: buffer to use |
| 2481 | * @len: amount of data to remove |
| 2482 | * |
| 2483 | * This function removes data from the start of a buffer, returning |
| 2484 | * the memory to the headroom. A pointer to the original data in the buffer |
| 2485 | * is returned after checking if there is enough data to pull. Once the |
| 2486 | * data has been pulled future pushes will overwrite the old data. |
| 2487 | */ |
| 2488 | void *skb_pull_data(struct sk_buff *skb, size_t len) |
| 2489 | { |
| 2490 | void *data = skb->data; |
| 2491 | |
| 2492 | if (skb->len < len) |
| 2493 | return NULL; |
| 2494 | |
| 2495 | skb_pull(skb, len); |
| 2496 | |
| 2497 | return data; |
| 2498 | } |
| 2499 | EXPORT_SYMBOL(skb_pull_data); |
| 2500 | |
| 2501 | /** |
| 2502 | * skb_trim - remove end from a buffer |
| 2503 | * @skb: buffer to alter |
| 2504 | * @len: new length |
| 2505 | * |
| 2506 | * Cut the length of a buffer down by removing data from the tail. If |
| 2507 | * the buffer is already under the length specified it is not modified. |
| 2508 | * The skb must be linear. |
| 2509 | */ |
| 2510 | void skb_trim(struct sk_buff *skb, unsigned int len) |
| 2511 | { |
| 2512 | if (skb->len > len) |
| 2513 | __skb_trim(skb, len); |
| 2514 | } |
| 2515 | EXPORT_SYMBOL(skb_trim); |
| 2516 | |
| 2517 | /* Trims skb to length len. It can change skb pointers. |
| 2518 | */ |
| 2519 | |
| 2520 | int ___pskb_trim(struct sk_buff *skb, unsigned int len) |
| 2521 | { |
| 2522 | struct sk_buff **fragp; |
| 2523 | struct sk_buff *frag; |
| 2524 | int offset = skb_headlen(skb); |
| 2525 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 2526 | int i; |
| 2527 | int err; |
| 2528 | |
| 2529 | if (skb_cloned(skb) && |
| 2530 | unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) |
| 2531 | return err; |
| 2532 | |
| 2533 | i = 0; |
| 2534 | if (offset >= len) |
| 2535 | goto drop_pages; |
| 2536 | |
| 2537 | for (; i < nfrags; i++) { |
| 2538 | int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 2539 | |
| 2540 | if (end < len) { |
| 2541 | offset = end; |
| 2542 | continue; |
| 2543 | } |
| 2544 | |
| 2545 | skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); |
| 2546 | |
| 2547 | drop_pages: |
| 2548 | skb_shinfo(skb)->nr_frags = i; |
| 2549 | |
| 2550 | for (; i < nfrags; i++) |
| 2551 | skb_frag_unref(skb, i); |
| 2552 | |
| 2553 | if (skb_has_frag_list(skb)) |
| 2554 | skb_drop_fraglist(skb); |
| 2555 | goto done; |
| 2556 | } |
| 2557 | |
| 2558 | for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); |
| 2559 | fragp = &frag->next) { |
| 2560 | int end = offset + frag->len; |
| 2561 | |
| 2562 | if (skb_shared(frag)) { |
| 2563 | struct sk_buff *nfrag; |
| 2564 | |
| 2565 | nfrag = skb_clone(frag, GFP_ATOMIC); |
| 2566 | if (unlikely(!nfrag)) |
| 2567 | return -ENOMEM; |
| 2568 | |
| 2569 | nfrag->next = frag->next; |
| 2570 | consume_skb(frag); |
| 2571 | frag = nfrag; |
| 2572 | *fragp = frag; |
| 2573 | } |
| 2574 | |
| 2575 | if (end < len) { |
| 2576 | offset = end; |
| 2577 | continue; |
| 2578 | } |
| 2579 | |
| 2580 | if (end > len && |
| 2581 | unlikely((err = pskb_trim(frag, len - offset)))) |
| 2582 | return err; |
| 2583 | |
| 2584 | if (frag->next) |
| 2585 | skb_drop_list(&frag->next); |
| 2586 | break; |
| 2587 | } |
| 2588 | |
| 2589 | done: |
| 2590 | if (len > skb_headlen(skb)) { |
| 2591 | skb->data_len -= skb->len - len; |
| 2592 | skb->len = len; |
| 2593 | } else { |
| 2594 | skb->len = len; |
| 2595 | skb->data_len = 0; |
| 2596 | skb_set_tail_pointer(skb, len); |
| 2597 | } |
| 2598 | |
| 2599 | if (!skb->sk || skb->destructor == sock_edemux) |
| 2600 | skb_condense(skb); |
| 2601 | return 0; |
| 2602 | } |
| 2603 | EXPORT_SYMBOL(___pskb_trim); |
| 2604 | |
| 2605 | /* Note : use pskb_trim_rcsum() instead of calling this directly |
| 2606 | */ |
| 2607 | int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) |
| 2608 | { |
| 2609 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 2610 | int delta = skb->len - len; |
| 2611 | |
| 2612 | skb->csum = csum_block_sub(skb->csum, |
| 2613 | skb_checksum(skb, len, delta, 0), |
| 2614 | len); |
| 2615 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 2616 | int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; |
| 2617 | int offset = skb_checksum_start_offset(skb) + skb->csum_offset; |
| 2618 | |
| 2619 | if (offset + sizeof(__sum16) > hdlen) |
| 2620 | return -EINVAL; |
| 2621 | } |
| 2622 | return __pskb_trim(skb, len); |
| 2623 | } |
| 2624 | EXPORT_SYMBOL(pskb_trim_rcsum_slow); |
| 2625 | |
| 2626 | /** |
| 2627 | * __pskb_pull_tail - advance tail of skb header |
| 2628 | * @skb: buffer to reallocate |
| 2629 | * @delta: number of bytes to advance tail |
| 2630 | * |
| 2631 | * The function makes a sense only on a fragmented &sk_buff, |
| 2632 | * it expands header moving its tail forward and copying necessary |
| 2633 | * data from fragmented part. |
| 2634 | * |
| 2635 | * &sk_buff MUST have reference count of 1. |
| 2636 | * |
| 2637 | * Returns %NULL (and &sk_buff does not change) if pull failed |
| 2638 | * or value of new tail of skb in the case of success. |
| 2639 | * |
| 2640 | * All the pointers pointing into skb header may change and must be |
| 2641 | * reloaded after call to this function. |
| 2642 | */ |
| 2643 | |
| 2644 | /* Moves tail of skb head forward, copying data from fragmented part, |
| 2645 | * when it is necessary. |
| 2646 | * 1. It may fail due to malloc failure. |
| 2647 | * 2. It may change skb pointers. |
| 2648 | * |
| 2649 | * It is pretty complicated. Luckily, it is called only in exceptional cases. |
| 2650 | */ |
| 2651 | void *__pskb_pull_tail(struct sk_buff *skb, int delta) |
| 2652 | { |
| 2653 | /* If skb has not enough free space at tail, get new one |
| 2654 | * plus 128 bytes for future expansions. If we have enough |
| 2655 | * room at tail, reallocate without expansion only if skb is cloned. |
| 2656 | */ |
| 2657 | int i, k, eat = (skb->tail + delta) - skb->end; |
| 2658 | |
| 2659 | if (eat > 0 || skb_cloned(skb)) { |
| 2660 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, |
| 2661 | GFP_ATOMIC)) |
| 2662 | return NULL; |
| 2663 | } |
| 2664 | |
| 2665 | BUG_ON(skb_copy_bits(skb, skb_headlen(skb), |
| 2666 | skb_tail_pointer(skb), delta)); |
| 2667 | |
| 2668 | /* Optimization: no fragments, no reasons to preestimate |
| 2669 | * size of pulled pages. Superb. |
| 2670 | */ |
| 2671 | if (!skb_has_frag_list(skb)) |
| 2672 | goto pull_pages; |
| 2673 | |
| 2674 | /* Estimate size of pulled pages. */ |
| 2675 | eat = delta; |
| 2676 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 2677 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 2678 | |
| 2679 | if (size >= eat) |
| 2680 | goto pull_pages; |
| 2681 | eat -= size; |
| 2682 | } |
| 2683 | |
| 2684 | /* If we need update frag list, we are in troubles. |
| 2685 | * Certainly, it is possible to add an offset to skb data, |
| 2686 | * but taking into account that pulling is expected to |
| 2687 | * be very rare operation, it is worth to fight against |
| 2688 | * further bloating skb head and crucify ourselves here instead. |
| 2689 | * Pure masohism, indeed. 8)8) |
| 2690 | */ |
| 2691 | if (eat) { |
| 2692 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 2693 | struct sk_buff *clone = NULL; |
| 2694 | struct sk_buff *insp = NULL; |
| 2695 | |
| 2696 | do { |
| 2697 | if (list->len <= eat) { |
| 2698 | /* Eaten as whole. */ |
| 2699 | eat -= list->len; |
| 2700 | list = list->next; |
| 2701 | insp = list; |
| 2702 | } else { |
| 2703 | /* Eaten partially. */ |
| 2704 | if (skb_is_gso(skb) && !list->head_frag && |
| 2705 | skb_headlen(list)) |
| 2706 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 2707 | |
| 2708 | if (skb_shared(list)) { |
| 2709 | /* Sucks! We need to fork list. :-( */ |
| 2710 | clone = skb_clone(list, GFP_ATOMIC); |
| 2711 | if (!clone) |
| 2712 | return NULL; |
| 2713 | insp = list->next; |
| 2714 | list = clone; |
| 2715 | } else { |
| 2716 | /* This may be pulled without |
| 2717 | * problems. */ |
| 2718 | insp = list; |
| 2719 | } |
| 2720 | if (!pskb_pull(list, eat)) { |
| 2721 | kfree_skb(clone); |
| 2722 | return NULL; |
| 2723 | } |
| 2724 | break; |
| 2725 | } |
| 2726 | } while (eat); |
| 2727 | |
| 2728 | /* Free pulled out fragments. */ |
| 2729 | while ((list = skb_shinfo(skb)->frag_list) != insp) { |
| 2730 | skb_shinfo(skb)->frag_list = list->next; |
| 2731 | consume_skb(list); |
| 2732 | } |
| 2733 | /* And insert new clone at head. */ |
| 2734 | if (clone) { |
| 2735 | clone->next = list; |
| 2736 | skb_shinfo(skb)->frag_list = clone; |
| 2737 | } |
| 2738 | } |
| 2739 | /* Success! Now we may commit changes to skb data. */ |
| 2740 | |
| 2741 | pull_pages: |
| 2742 | eat = delta; |
| 2743 | k = 0; |
| 2744 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 2745 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 2746 | |
| 2747 | if (size <= eat) { |
| 2748 | skb_frag_unref(skb, i); |
| 2749 | eat -= size; |
| 2750 | } else { |
| 2751 | skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; |
| 2752 | |
| 2753 | *frag = skb_shinfo(skb)->frags[i]; |
| 2754 | if (eat) { |
| 2755 | skb_frag_off_add(frag, eat); |
| 2756 | skb_frag_size_sub(frag, eat); |
| 2757 | if (!i) |
| 2758 | goto end; |
| 2759 | eat = 0; |
| 2760 | } |
| 2761 | k++; |
| 2762 | } |
| 2763 | } |
| 2764 | skb_shinfo(skb)->nr_frags = k; |
| 2765 | |
| 2766 | end: |
| 2767 | skb->tail += delta; |
| 2768 | skb->data_len -= delta; |
| 2769 | |
| 2770 | if (!skb->data_len) |
| 2771 | skb_zcopy_clear(skb, false); |
| 2772 | |
| 2773 | return skb_tail_pointer(skb); |
| 2774 | } |
| 2775 | EXPORT_SYMBOL(__pskb_pull_tail); |
| 2776 | |
| 2777 | /** |
| 2778 | * skb_copy_bits - copy bits from skb to kernel buffer |
| 2779 | * @skb: source skb |
| 2780 | * @offset: offset in source |
| 2781 | * @to: destination buffer |
| 2782 | * @len: number of bytes to copy |
| 2783 | * |
| 2784 | * Copy the specified number of bytes from the source skb to the |
| 2785 | * destination buffer. |
| 2786 | * |
| 2787 | * CAUTION ! : |
| 2788 | * If its prototype is ever changed, |
| 2789 | * check arch/{*}/net/{*}.S files, |
| 2790 | * since it is called from BPF assembly code. |
| 2791 | */ |
| 2792 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) |
| 2793 | { |
| 2794 | int start = skb_headlen(skb); |
| 2795 | struct sk_buff *frag_iter; |
| 2796 | int i, copy; |
| 2797 | |
| 2798 | if (offset > (int)skb->len - len) |
| 2799 | goto fault; |
| 2800 | |
| 2801 | /* Copy header. */ |
| 2802 | if ((copy = start - offset) > 0) { |
| 2803 | if (copy > len) |
| 2804 | copy = len; |
| 2805 | skb_copy_from_linear_data_offset(skb, offset, to, copy); |
| 2806 | if ((len -= copy) == 0) |
| 2807 | return 0; |
| 2808 | offset += copy; |
| 2809 | to += copy; |
| 2810 | } |
| 2811 | |
| 2812 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 2813 | int end; |
| 2814 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
| 2815 | |
| 2816 | WARN_ON(start > offset + len); |
| 2817 | |
| 2818 | end = start + skb_frag_size(f); |
| 2819 | if ((copy = end - offset) > 0) { |
| 2820 | u32 p_off, p_len, copied; |
| 2821 | struct page *p; |
| 2822 | u8 *vaddr; |
| 2823 | |
| 2824 | if (copy > len) |
| 2825 | copy = len; |
| 2826 | |
| 2827 | skb_frag_foreach_page(f, |
| 2828 | skb_frag_off(f) + offset - start, |
| 2829 | copy, p, p_off, p_len, copied) { |
| 2830 | vaddr = kmap_atomic(p); |
| 2831 | memcpy(to + copied, vaddr + p_off, p_len); |
| 2832 | kunmap_atomic(vaddr); |
| 2833 | } |
| 2834 | |
| 2835 | if ((len -= copy) == 0) |
| 2836 | return 0; |
| 2837 | offset += copy; |
| 2838 | to += copy; |
| 2839 | } |
| 2840 | start = end; |
| 2841 | } |
| 2842 | |
| 2843 | skb_walk_frags(skb, frag_iter) { |
| 2844 | int end; |
| 2845 | |
| 2846 | WARN_ON(start > offset + len); |
| 2847 | |
| 2848 | end = start + frag_iter->len; |
| 2849 | if ((copy = end - offset) > 0) { |
| 2850 | if (copy > len) |
| 2851 | copy = len; |
| 2852 | if (skb_copy_bits(frag_iter, offset - start, to, copy)) |
| 2853 | goto fault; |
| 2854 | if ((len -= copy) == 0) |
| 2855 | return 0; |
| 2856 | offset += copy; |
| 2857 | to += copy; |
| 2858 | } |
| 2859 | start = end; |
| 2860 | } |
| 2861 | |
| 2862 | if (!len) |
| 2863 | return 0; |
| 2864 | |
| 2865 | fault: |
| 2866 | return -EFAULT; |
| 2867 | } |
| 2868 | EXPORT_SYMBOL(skb_copy_bits); |
| 2869 | |
| 2870 | /* |
| 2871 | * Callback from splice_to_pipe(), if we need to release some pages |
| 2872 | * at the end of the spd in case we error'ed out in filling the pipe. |
| 2873 | */ |
| 2874 | static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) |
| 2875 | { |
| 2876 | put_page(spd->pages[i]); |
| 2877 | } |
| 2878 | |
| 2879 | static struct page *linear_to_page(struct page *page, unsigned int *len, |
| 2880 | unsigned int *offset, |
| 2881 | struct sock *sk) |
| 2882 | { |
| 2883 | struct page_frag *pfrag = sk_page_frag(sk); |
| 2884 | |
| 2885 | if (!sk_page_frag_refill(sk, pfrag)) |
| 2886 | return NULL; |
| 2887 | |
| 2888 | *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); |
| 2889 | |
| 2890 | memcpy(page_address(pfrag->page) + pfrag->offset, |
| 2891 | page_address(page) + *offset, *len); |
| 2892 | *offset = pfrag->offset; |
| 2893 | pfrag->offset += *len; |
| 2894 | |
| 2895 | return pfrag->page; |
| 2896 | } |
| 2897 | |
| 2898 | static bool spd_can_coalesce(const struct splice_pipe_desc *spd, |
| 2899 | struct page *page, |
| 2900 | unsigned int offset) |
| 2901 | { |
| 2902 | return spd->nr_pages && |
| 2903 | spd->pages[spd->nr_pages - 1] == page && |
| 2904 | (spd->partial[spd->nr_pages - 1].offset + |
| 2905 | spd->partial[spd->nr_pages - 1].len == offset); |
| 2906 | } |
| 2907 | |
| 2908 | /* |
| 2909 | * Fill page/offset/length into spd, if it can hold more pages. |
| 2910 | */ |
| 2911 | static bool spd_fill_page(struct splice_pipe_desc *spd, |
| 2912 | struct pipe_inode_info *pipe, struct page *page, |
| 2913 | unsigned int *len, unsigned int offset, |
| 2914 | bool linear, |
| 2915 | struct sock *sk) |
| 2916 | { |
| 2917 | if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) |
| 2918 | return true; |
| 2919 | |
| 2920 | if (linear) { |
| 2921 | page = linear_to_page(page, len, &offset, sk); |
| 2922 | if (!page) |
| 2923 | return true; |
| 2924 | } |
| 2925 | if (spd_can_coalesce(spd, page, offset)) { |
| 2926 | spd->partial[spd->nr_pages - 1].len += *len; |
| 2927 | return false; |
| 2928 | } |
| 2929 | get_page(page); |
| 2930 | spd->pages[spd->nr_pages] = page; |
| 2931 | spd->partial[spd->nr_pages].len = *len; |
| 2932 | spd->partial[spd->nr_pages].offset = offset; |
| 2933 | spd->nr_pages++; |
| 2934 | |
| 2935 | return false; |
| 2936 | } |
| 2937 | |
| 2938 | static bool __splice_segment(struct page *page, unsigned int poff, |
| 2939 | unsigned int plen, unsigned int *off, |
| 2940 | unsigned int *len, |
| 2941 | struct splice_pipe_desc *spd, bool linear, |
| 2942 | struct sock *sk, |
| 2943 | struct pipe_inode_info *pipe) |
| 2944 | { |
| 2945 | if (!*len) |
| 2946 | return true; |
| 2947 | |
| 2948 | /* skip this segment if already processed */ |
| 2949 | if (*off >= plen) { |
| 2950 | *off -= plen; |
| 2951 | return false; |
| 2952 | } |
| 2953 | |
| 2954 | /* ignore any bits we already processed */ |
| 2955 | poff += *off; |
| 2956 | plen -= *off; |
| 2957 | *off = 0; |
| 2958 | |
| 2959 | do { |
| 2960 | unsigned int flen = min(*len, plen); |
| 2961 | |
| 2962 | if (spd_fill_page(spd, pipe, page, &flen, poff, |
| 2963 | linear, sk)) |
| 2964 | return true; |
| 2965 | poff += flen; |
| 2966 | plen -= flen; |
| 2967 | *len -= flen; |
| 2968 | } while (*len && plen); |
| 2969 | |
| 2970 | return false; |
| 2971 | } |
| 2972 | |
| 2973 | /* |
| 2974 | * Map linear and fragment data from the skb to spd. It reports true if the |
| 2975 | * pipe is full or if we already spliced the requested length. |
| 2976 | */ |
| 2977 | static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, |
| 2978 | unsigned int *offset, unsigned int *len, |
| 2979 | struct splice_pipe_desc *spd, struct sock *sk) |
| 2980 | { |
| 2981 | int seg; |
| 2982 | struct sk_buff *iter; |
| 2983 | |
| 2984 | /* map the linear part : |
| 2985 | * If skb->head_frag is set, this 'linear' part is backed by a |
| 2986 | * fragment, and if the head is not shared with any clones then |
| 2987 | * we can avoid a copy since we own the head portion of this page. |
| 2988 | */ |
| 2989 | if (__splice_segment(virt_to_page(skb->data), |
| 2990 | (unsigned long) skb->data & (PAGE_SIZE - 1), |
| 2991 | skb_headlen(skb), |
| 2992 | offset, len, spd, |
| 2993 | skb_head_is_locked(skb), |
| 2994 | sk, pipe)) |
| 2995 | return true; |
| 2996 | |
| 2997 | /* |
| 2998 | * then map the fragments |
| 2999 | */ |
| 3000 | for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { |
| 3001 | const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; |
| 3002 | |
| 3003 | if (__splice_segment(skb_frag_page(f), |
| 3004 | skb_frag_off(f), skb_frag_size(f), |
| 3005 | offset, len, spd, false, sk, pipe)) |
| 3006 | return true; |
| 3007 | } |
| 3008 | |
| 3009 | skb_walk_frags(skb, iter) { |
| 3010 | if (*offset >= iter->len) { |
| 3011 | *offset -= iter->len; |
| 3012 | continue; |
| 3013 | } |
| 3014 | /* __skb_splice_bits() only fails if the output has no room |
| 3015 | * left, so no point in going over the frag_list for the error |
| 3016 | * case. |
| 3017 | */ |
| 3018 | if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) |
| 3019 | return true; |
| 3020 | } |
| 3021 | |
| 3022 | return false; |
| 3023 | } |
| 3024 | |
| 3025 | /* |
| 3026 | * Map data from the skb to a pipe. Should handle both the linear part, |
| 3027 | * the fragments, and the frag list. |
| 3028 | */ |
| 3029 | int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, |
| 3030 | struct pipe_inode_info *pipe, unsigned int tlen, |
| 3031 | unsigned int flags) |
| 3032 | { |
| 3033 | struct partial_page partial[MAX_SKB_FRAGS]; |
| 3034 | struct page *pages[MAX_SKB_FRAGS]; |
| 3035 | struct splice_pipe_desc spd = { |
| 3036 | .pages = pages, |
| 3037 | .partial = partial, |
| 3038 | .nr_pages_max = MAX_SKB_FRAGS, |
| 3039 | .ops = &nosteal_pipe_buf_ops, |
| 3040 | .spd_release = sock_spd_release, |
| 3041 | }; |
| 3042 | int ret = 0; |
| 3043 | |
| 3044 | __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); |
| 3045 | |
| 3046 | if (spd.nr_pages) |
| 3047 | ret = splice_to_pipe(pipe, &spd); |
| 3048 | |
| 3049 | return ret; |
| 3050 | } |
| 3051 | EXPORT_SYMBOL_GPL(skb_splice_bits); |
| 3052 | |
| 3053 | static int sendmsg_locked(struct sock *sk, struct msghdr *msg) |
| 3054 | { |
| 3055 | struct socket *sock = sk->sk_socket; |
| 3056 | size_t size = msg_data_left(msg); |
| 3057 | |
| 3058 | if (!sock) |
| 3059 | return -EINVAL; |
| 3060 | |
| 3061 | if (!sock->ops->sendmsg_locked) |
| 3062 | return sock_no_sendmsg_locked(sk, msg, size); |
| 3063 | |
| 3064 | return sock->ops->sendmsg_locked(sk, msg, size); |
| 3065 | } |
| 3066 | |
| 3067 | static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg) |
| 3068 | { |
| 3069 | struct socket *sock = sk->sk_socket; |
| 3070 | |
| 3071 | if (!sock) |
| 3072 | return -EINVAL; |
| 3073 | return sock_sendmsg(sock, msg); |
| 3074 | } |
| 3075 | |
| 3076 | typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg); |
| 3077 | static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, |
| 3078 | int len, sendmsg_func sendmsg) |
| 3079 | { |
| 3080 | unsigned int orig_len = len; |
| 3081 | struct sk_buff *head = skb; |
| 3082 | unsigned short fragidx; |
| 3083 | int slen, ret; |
| 3084 | |
| 3085 | do_frag_list: |
| 3086 | |
| 3087 | /* Deal with head data */ |
| 3088 | while (offset < skb_headlen(skb) && len) { |
| 3089 | struct kvec kv; |
| 3090 | struct msghdr msg; |
| 3091 | |
| 3092 | slen = min_t(int, len, skb_headlen(skb) - offset); |
| 3093 | kv.iov_base = skb->data + offset; |
| 3094 | kv.iov_len = slen; |
| 3095 | memset(&msg, 0, sizeof(msg)); |
| 3096 | msg.msg_flags = MSG_DONTWAIT; |
| 3097 | |
| 3098 | iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &kv, 1, slen); |
| 3099 | ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked, |
| 3100 | sendmsg_unlocked, sk, &msg); |
| 3101 | if (ret <= 0) |
| 3102 | goto error; |
| 3103 | |
| 3104 | offset += ret; |
| 3105 | len -= ret; |
| 3106 | } |
| 3107 | |
| 3108 | /* All the data was skb head? */ |
| 3109 | if (!len) |
| 3110 | goto out; |
| 3111 | |
| 3112 | /* Make offset relative to start of frags */ |
| 3113 | offset -= skb_headlen(skb); |
| 3114 | |
| 3115 | /* Find where we are in frag list */ |
| 3116 | for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { |
| 3117 | skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; |
| 3118 | |
| 3119 | if (offset < skb_frag_size(frag)) |
| 3120 | break; |
| 3121 | |
| 3122 | offset -= skb_frag_size(frag); |
| 3123 | } |
| 3124 | |
| 3125 | for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { |
| 3126 | skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; |
| 3127 | |
| 3128 | slen = min_t(size_t, len, skb_frag_size(frag) - offset); |
| 3129 | |
| 3130 | while (slen) { |
| 3131 | struct bio_vec bvec; |
| 3132 | struct msghdr msg = { |
| 3133 | .msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT, |
| 3134 | }; |
| 3135 | |
| 3136 | bvec_set_page(&bvec, skb_frag_page(frag), slen, |
| 3137 | skb_frag_off(frag) + offset); |
| 3138 | iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, |
| 3139 | slen); |
| 3140 | |
| 3141 | ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked, |
| 3142 | sendmsg_unlocked, sk, &msg); |
| 3143 | if (ret <= 0) |
| 3144 | goto error; |
| 3145 | |
| 3146 | len -= ret; |
| 3147 | offset += ret; |
| 3148 | slen -= ret; |
| 3149 | } |
| 3150 | |
| 3151 | offset = 0; |
| 3152 | } |
| 3153 | |
| 3154 | if (len) { |
| 3155 | /* Process any frag lists */ |
| 3156 | |
| 3157 | if (skb == head) { |
| 3158 | if (skb_has_frag_list(skb)) { |
| 3159 | skb = skb_shinfo(skb)->frag_list; |
| 3160 | goto do_frag_list; |
| 3161 | } |
| 3162 | } else if (skb->next) { |
| 3163 | skb = skb->next; |
| 3164 | goto do_frag_list; |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | out: |
| 3169 | return orig_len - len; |
| 3170 | |
| 3171 | error: |
| 3172 | return orig_len == len ? ret : orig_len - len; |
| 3173 | } |
| 3174 | |
| 3175 | /* Send skb data on a socket. Socket must be locked. */ |
| 3176 | int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, |
| 3177 | int len) |
| 3178 | { |
| 3179 | return __skb_send_sock(sk, skb, offset, len, sendmsg_locked); |
| 3180 | } |
| 3181 | EXPORT_SYMBOL_GPL(skb_send_sock_locked); |
| 3182 | |
| 3183 | /* Send skb data on a socket. Socket must be unlocked. */ |
| 3184 | int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) |
| 3185 | { |
| 3186 | return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked); |
| 3187 | } |
| 3188 | |
| 3189 | /** |
| 3190 | * skb_store_bits - store bits from kernel buffer to skb |
| 3191 | * @skb: destination buffer |
| 3192 | * @offset: offset in destination |
| 3193 | * @from: source buffer |
| 3194 | * @len: number of bytes to copy |
| 3195 | * |
| 3196 | * Copy the specified number of bytes from the source buffer to the |
| 3197 | * destination skb. This function handles all the messy bits of |
| 3198 | * traversing fragment lists and such. |
| 3199 | */ |
| 3200 | |
| 3201 | int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) |
| 3202 | { |
| 3203 | int start = skb_headlen(skb); |
| 3204 | struct sk_buff *frag_iter; |
| 3205 | int i, copy; |
| 3206 | |
| 3207 | if (offset > (int)skb->len - len) |
| 3208 | goto fault; |
| 3209 | |
| 3210 | if ((copy = start - offset) > 0) { |
| 3211 | if (copy > len) |
| 3212 | copy = len; |
| 3213 | skb_copy_to_linear_data_offset(skb, offset, from, copy); |
| 3214 | if ((len -= copy) == 0) |
| 3215 | return 0; |
| 3216 | offset += copy; |
| 3217 | from += copy; |
| 3218 | } |
| 3219 | |
| 3220 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 3221 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 3222 | int end; |
| 3223 | |
| 3224 | WARN_ON(start > offset + len); |
| 3225 | |
| 3226 | end = start + skb_frag_size(frag); |
| 3227 | if ((copy = end - offset) > 0) { |
| 3228 | u32 p_off, p_len, copied; |
| 3229 | struct page *p; |
| 3230 | u8 *vaddr; |
| 3231 | |
| 3232 | if (copy > len) |
| 3233 | copy = len; |
| 3234 | |
| 3235 | skb_frag_foreach_page(frag, |
| 3236 | skb_frag_off(frag) + offset - start, |
| 3237 | copy, p, p_off, p_len, copied) { |
| 3238 | vaddr = kmap_atomic(p); |
| 3239 | memcpy(vaddr + p_off, from + copied, p_len); |
| 3240 | kunmap_atomic(vaddr); |
| 3241 | } |
| 3242 | |
| 3243 | if ((len -= copy) == 0) |
| 3244 | return 0; |
| 3245 | offset += copy; |
| 3246 | from += copy; |
| 3247 | } |
| 3248 | start = end; |
| 3249 | } |
| 3250 | |
| 3251 | skb_walk_frags(skb, frag_iter) { |
| 3252 | int end; |
| 3253 | |
| 3254 | WARN_ON(start > offset + len); |
| 3255 | |
| 3256 | end = start + frag_iter->len; |
| 3257 | if ((copy = end - offset) > 0) { |
| 3258 | if (copy > len) |
| 3259 | copy = len; |
| 3260 | if (skb_store_bits(frag_iter, offset - start, |
| 3261 | from, copy)) |
| 3262 | goto fault; |
| 3263 | if ((len -= copy) == 0) |
| 3264 | return 0; |
| 3265 | offset += copy; |
| 3266 | from += copy; |
| 3267 | } |
| 3268 | start = end; |
| 3269 | } |
| 3270 | if (!len) |
| 3271 | return 0; |
| 3272 | |
| 3273 | fault: |
| 3274 | return -EFAULT; |
| 3275 | } |
| 3276 | EXPORT_SYMBOL(skb_store_bits); |
| 3277 | |
| 3278 | /* Checksum skb data. */ |
| 3279 | __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, |
| 3280 | __wsum csum, const struct skb_checksum_ops *ops) |
| 3281 | { |
| 3282 | int start = skb_headlen(skb); |
| 3283 | int i, copy = start - offset; |
| 3284 | struct sk_buff *frag_iter; |
| 3285 | int pos = 0; |
| 3286 | |
| 3287 | /* Checksum header. */ |
| 3288 | if (copy > 0) { |
| 3289 | if (copy > len) |
| 3290 | copy = len; |
| 3291 | csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, |
| 3292 | skb->data + offset, copy, csum); |
| 3293 | if ((len -= copy) == 0) |
| 3294 | return csum; |
| 3295 | offset += copy; |
| 3296 | pos = copy; |
| 3297 | } |
| 3298 | |
| 3299 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 3300 | int end; |
| 3301 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 3302 | |
| 3303 | WARN_ON(start > offset + len); |
| 3304 | |
| 3305 | end = start + skb_frag_size(frag); |
| 3306 | if ((copy = end - offset) > 0) { |
| 3307 | u32 p_off, p_len, copied; |
| 3308 | struct page *p; |
| 3309 | __wsum csum2; |
| 3310 | u8 *vaddr; |
| 3311 | |
| 3312 | if (copy > len) |
| 3313 | copy = len; |
| 3314 | |
| 3315 | skb_frag_foreach_page(frag, |
| 3316 | skb_frag_off(frag) + offset - start, |
| 3317 | copy, p, p_off, p_len, copied) { |
| 3318 | vaddr = kmap_atomic(p); |
| 3319 | csum2 = INDIRECT_CALL_1(ops->update, |
| 3320 | csum_partial_ext, |
| 3321 | vaddr + p_off, p_len, 0); |
| 3322 | kunmap_atomic(vaddr); |
| 3323 | csum = INDIRECT_CALL_1(ops->combine, |
| 3324 | csum_block_add_ext, csum, |
| 3325 | csum2, pos, p_len); |
| 3326 | pos += p_len; |
| 3327 | } |
| 3328 | |
| 3329 | if (!(len -= copy)) |
| 3330 | return csum; |
| 3331 | offset += copy; |
| 3332 | } |
| 3333 | start = end; |
| 3334 | } |
| 3335 | |
| 3336 | skb_walk_frags(skb, frag_iter) { |
| 3337 | int end; |
| 3338 | |
| 3339 | WARN_ON(start > offset + len); |
| 3340 | |
| 3341 | end = start + frag_iter->len; |
| 3342 | if ((copy = end - offset) > 0) { |
| 3343 | __wsum csum2; |
| 3344 | if (copy > len) |
| 3345 | copy = len; |
| 3346 | csum2 = __skb_checksum(frag_iter, offset - start, |
| 3347 | copy, 0, ops); |
| 3348 | csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, |
| 3349 | csum, csum2, pos, copy); |
| 3350 | if ((len -= copy) == 0) |
| 3351 | return csum; |
| 3352 | offset += copy; |
| 3353 | pos += copy; |
| 3354 | } |
| 3355 | start = end; |
| 3356 | } |
| 3357 | BUG_ON(len); |
| 3358 | |
| 3359 | return csum; |
| 3360 | } |
| 3361 | EXPORT_SYMBOL(__skb_checksum); |
| 3362 | |
| 3363 | __wsum skb_checksum(const struct sk_buff *skb, int offset, |
| 3364 | int len, __wsum csum) |
| 3365 | { |
| 3366 | const struct skb_checksum_ops ops = { |
| 3367 | .update = csum_partial_ext, |
| 3368 | .combine = csum_block_add_ext, |
| 3369 | }; |
| 3370 | |
| 3371 | return __skb_checksum(skb, offset, len, csum, &ops); |
| 3372 | } |
| 3373 | EXPORT_SYMBOL(skb_checksum); |
| 3374 | |
| 3375 | /* Both of above in one bottle. */ |
| 3376 | |
| 3377 | __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, |
| 3378 | u8 *to, int len) |
| 3379 | { |
| 3380 | int start = skb_headlen(skb); |
| 3381 | int i, copy = start - offset; |
| 3382 | struct sk_buff *frag_iter; |
| 3383 | int pos = 0; |
| 3384 | __wsum csum = 0; |
| 3385 | |
| 3386 | /* Copy header. */ |
| 3387 | if (copy > 0) { |
| 3388 | if (copy > len) |
| 3389 | copy = len; |
| 3390 | csum = csum_partial_copy_nocheck(skb->data + offset, to, |
| 3391 | copy); |
| 3392 | if ((len -= copy) == 0) |
| 3393 | return csum; |
| 3394 | offset += copy; |
| 3395 | to += copy; |
| 3396 | pos = copy; |
| 3397 | } |
| 3398 | |
| 3399 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 3400 | int end; |
| 3401 | |
| 3402 | WARN_ON(start > offset + len); |
| 3403 | |
| 3404 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 3405 | if ((copy = end - offset) > 0) { |
| 3406 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 3407 | u32 p_off, p_len, copied; |
| 3408 | struct page *p; |
| 3409 | __wsum csum2; |
| 3410 | u8 *vaddr; |
| 3411 | |
| 3412 | if (copy > len) |
| 3413 | copy = len; |
| 3414 | |
| 3415 | skb_frag_foreach_page(frag, |
| 3416 | skb_frag_off(frag) + offset - start, |
| 3417 | copy, p, p_off, p_len, copied) { |
| 3418 | vaddr = kmap_atomic(p); |
| 3419 | csum2 = csum_partial_copy_nocheck(vaddr + p_off, |
| 3420 | to + copied, |
| 3421 | p_len); |
| 3422 | kunmap_atomic(vaddr); |
| 3423 | csum = csum_block_add(csum, csum2, pos); |
| 3424 | pos += p_len; |
| 3425 | } |
| 3426 | |
| 3427 | if (!(len -= copy)) |
| 3428 | return csum; |
| 3429 | offset += copy; |
| 3430 | to += copy; |
| 3431 | } |
| 3432 | start = end; |
| 3433 | } |
| 3434 | |
| 3435 | skb_walk_frags(skb, frag_iter) { |
| 3436 | __wsum csum2; |
| 3437 | int end; |
| 3438 | |
| 3439 | WARN_ON(start > offset + len); |
| 3440 | |
| 3441 | end = start + frag_iter->len; |
| 3442 | if ((copy = end - offset) > 0) { |
| 3443 | if (copy > len) |
| 3444 | copy = len; |
| 3445 | csum2 = skb_copy_and_csum_bits(frag_iter, |
| 3446 | offset - start, |
| 3447 | to, copy); |
| 3448 | csum = csum_block_add(csum, csum2, pos); |
| 3449 | if ((len -= copy) == 0) |
| 3450 | return csum; |
| 3451 | offset += copy; |
| 3452 | to += copy; |
| 3453 | pos += copy; |
| 3454 | } |
| 3455 | start = end; |
| 3456 | } |
| 3457 | BUG_ON(len); |
| 3458 | return csum; |
| 3459 | } |
| 3460 | EXPORT_SYMBOL(skb_copy_and_csum_bits); |
| 3461 | |
| 3462 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) |
| 3463 | { |
| 3464 | __sum16 sum; |
| 3465 | |
| 3466 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); |
| 3467 | /* See comments in __skb_checksum_complete(). */ |
| 3468 | if (likely(!sum)) { |
| 3469 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && |
| 3470 | !skb->csum_complete_sw) |
| 3471 | netdev_rx_csum_fault(skb->dev, skb); |
| 3472 | } |
| 3473 | if (!skb_shared(skb)) |
| 3474 | skb->csum_valid = !sum; |
| 3475 | return sum; |
| 3476 | } |
| 3477 | EXPORT_SYMBOL(__skb_checksum_complete_head); |
| 3478 | |
| 3479 | /* This function assumes skb->csum already holds pseudo header's checksum, |
| 3480 | * which has been changed from the hardware checksum, for example, by |
| 3481 | * __skb_checksum_validate_complete(). And, the original skb->csum must |
| 3482 | * have been validated unsuccessfully for CHECKSUM_COMPLETE case. |
| 3483 | * |
| 3484 | * It returns non-zero if the recomputed checksum is still invalid, otherwise |
| 3485 | * zero. The new checksum is stored back into skb->csum unless the skb is |
| 3486 | * shared. |
| 3487 | */ |
| 3488 | __sum16 __skb_checksum_complete(struct sk_buff *skb) |
| 3489 | { |
| 3490 | __wsum csum; |
| 3491 | __sum16 sum; |
| 3492 | |
| 3493 | csum = skb_checksum(skb, 0, skb->len, 0); |
| 3494 | |
| 3495 | sum = csum_fold(csum_add(skb->csum, csum)); |
| 3496 | /* This check is inverted, because we already knew the hardware |
| 3497 | * checksum is invalid before calling this function. So, if the |
| 3498 | * re-computed checksum is valid instead, then we have a mismatch |
| 3499 | * between the original skb->csum and skb_checksum(). This means either |
| 3500 | * the original hardware checksum is incorrect or we screw up skb->csum |
| 3501 | * when moving skb->data around. |
| 3502 | */ |
| 3503 | if (likely(!sum)) { |
| 3504 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && |
| 3505 | !skb->csum_complete_sw) |
| 3506 | netdev_rx_csum_fault(skb->dev, skb); |
| 3507 | } |
| 3508 | |
| 3509 | if (!skb_shared(skb)) { |
| 3510 | /* Save full packet checksum */ |
| 3511 | skb->csum = csum; |
| 3512 | skb->ip_summed = CHECKSUM_COMPLETE; |
| 3513 | skb->csum_complete_sw = 1; |
| 3514 | skb->csum_valid = !sum; |
| 3515 | } |
| 3516 | |
| 3517 | return sum; |
| 3518 | } |
| 3519 | EXPORT_SYMBOL(__skb_checksum_complete); |
| 3520 | |
| 3521 | static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) |
| 3522 | { |
| 3523 | net_warn_ratelimited( |
| 3524 | "%s: attempt to compute crc32c without libcrc32c.ko\n", |
| 3525 | __func__); |
| 3526 | return 0; |
| 3527 | } |
| 3528 | |
| 3529 | static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, |
| 3530 | int offset, int len) |
| 3531 | { |
| 3532 | net_warn_ratelimited( |
| 3533 | "%s: attempt to compute crc32c without libcrc32c.ko\n", |
| 3534 | __func__); |
| 3535 | return 0; |
| 3536 | } |
| 3537 | |
| 3538 | static const struct skb_checksum_ops default_crc32c_ops = { |
| 3539 | .update = warn_crc32c_csum_update, |
| 3540 | .combine = warn_crc32c_csum_combine, |
| 3541 | }; |
| 3542 | |
| 3543 | const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = |
| 3544 | &default_crc32c_ops; |
| 3545 | EXPORT_SYMBOL(crc32c_csum_stub); |
| 3546 | |
| 3547 | /** |
| 3548 | * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() |
| 3549 | * @from: source buffer |
| 3550 | * |
| 3551 | * Calculates the amount of linear headroom needed in the 'to' skb passed |
| 3552 | * into skb_zerocopy(). |
| 3553 | */ |
| 3554 | unsigned int |
| 3555 | skb_zerocopy_headlen(const struct sk_buff *from) |
| 3556 | { |
| 3557 | unsigned int hlen = 0; |
| 3558 | |
| 3559 | if (!from->head_frag || |
| 3560 | skb_headlen(from) < L1_CACHE_BYTES || |
| 3561 | skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { |
| 3562 | hlen = skb_headlen(from); |
| 3563 | if (!hlen) |
| 3564 | hlen = from->len; |
| 3565 | } |
| 3566 | |
| 3567 | if (skb_has_frag_list(from)) |
| 3568 | hlen = from->len; |
| 3569 | |
| 3570 | return hlen; |
| 3571 | } |
| 3572 | EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); |
| 3573 | |
| 3574 | /** |
| 3575 | * skb_zerocopy - Zero copy skb to skb |
| 3576 | * @to: destination buffer |
| 3577 | * @from: source buffer |
| 3578 | * @len: number of bytes to copy from source buffer |
| 3579 | * @hlen: size of linear headroom in destination buffer |
| 3580 | * |
| 3581 | * Copies up to `len` bytes from `from` to `to` by creating references |
| 3582 | * to the frags in the source buffer. |
| 3583 | * |
| 3584 | * The `hlen` as calculated by skb_zerocopy_headlen() specifies the |
| 3585 | * headroom in the `to` buffer. |
| 3586 | * |
| 3587 | * Return value: |
| 3588 | * 0: everything is OK |
| 3589 | * -ENOMEM: couldn't orphan frags of @from due to lack of memory |
| 3590 | * -EFAULT: skb_copy_bits() found some problem with skb geometry |
| 3591 | */ |
| 3592 | int |
| 3593 | skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) |
| 3594 | { |
| 3595 | int i, j = 0; |
| 3596 | int plen = 0; /* length of skb->head fragment */ |
| 3597 | int ret; |
| 3598 | struct page *page; |
| 3599 | unsigned int offset; |
| 3600 | |
| 3601 | BUG_ON(!from->head_frag && !hlen); |
| 3602 | |
| 3603 | /* dont bother with small payloads */ |
| 3604 | if (len <= skb_tailroom(to)) |
| 3605 | return skb_copy_bits(from, 0, skb_put(to, len), len); |
| 3606 | |
| 3607 | if (hlen) { |
| 3608 | ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); |
| 3609 | if (unlikely(ret)) |
| 3610 | return ret; |
| 3611 | len -= hlen; |
| 3612 | } else { |
| 3613 | plen = min_t(int, skb_headlen(from), len); |
| 3614 | if (plen) { |
| 3615 | page = virt_to_head_page(from->head); |
| 3616 | offset = from->data - (unsigned char *)page_address(page); |
| 3617 | __skb_fill_page_desc(to, 0, page, offset, plen); |
| 3618 | get_page(page); |
| 3619 | j = 1; |
| 3620 | len -= plen; |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | skb_len_add(to, len + plen); |
| 3625 | |
| 3626 | if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { |
| 3627 | skb_tx_error(from); |
| 3628 | return -ENOMEM; |
| 3629 | } |
| 3630 | skb_zerocopy_clone(to, from, GFP_ATOMIC); |
| 3631 | |
| 3632 | for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { |
| 3633 | int size; |
| 3634 | |
| 3635 | if (!len) |
| 3636 | break; |
| 3637 | skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; |
| 3638 | size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), |
| 3639 | len); |
| 3640 | skb_frag_size_set(&skb_shinfo(to)->frags[j], size); |
| 3641 | len -= size; |
| 3642 | skb_frag_ref(to, j); |
| 3643 | j++; |
| 3644 | } |
| 3645 | skb_shinfo(to)->nr_frags = j; |
| 3646 | |
| 3647 | return 0; |
| 3648 | } |
| 3649 | EXPORT_SYMBOL_GPL(skb_zerocopy); |
| 3650 | |
| 3651 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) |
| 3652 | { |
| 3653 | __wsum csum; |
| 3654 | long csstart; |
| 3655 | |
| 3656 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 3657 | csstart = skb_checksum_start_offset(skb); |
| 3658 | else |
| 3659 | csstart = skb_headlen(skb); |
| 3660 | |
| 3661 | BUG_ON(csstart > skb_headlen(skb)); |
| 3662 | |
| 3663 | skb_copy_from_linear_data(skb, to, csstart); |
| 3664 | |
| 3665 | csum = 0; |
| 3666 | if (csstart != skb->len) |
| 3667 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, |
| 3668 | skb->len - csstart); |
| 3669 | |
| 3670 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 3671 | long csstuff = csstart + skb->csum_offset; |
| 3672 | |
| 3673 | *((__sum16 *)(to + csstuff)) = csum_fold(csum); |
| 3674 | } |
| 3675 | } |
| 3676 | EXPORT_SYMBOL(skb_copy_and_csum_dev); |
| 3677 | |
| 3678 | /** |
| 3679 | * skb_dequeue - remove from the head of the queue |
| 3680 | * @list: list to dequeue from |
| 3681 | * |
| 3682 | * Remove the head of the list. The list lock is taken so the function |
| 3683 | * may be used safely with other locking list functions. The head item is |
| 3684 | * returned or %NULL if the list is empty. |
| 3685 | */ |
| 3686 | |
| 3687 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) |
| 3688 | { |
| 3689 | unsigned long flags; |
| 3690 | struct sk_buff *result; |
| 3691 | |
| 3692 | spin_lock_irqsave(&list->lock, flags); |
| 3693 | result = __skb_dequeue(list); |
| 3694 | spin_unlock_irqrestore(&list->lock, flags); |
| 3695 | return result; |
| 3696 | } |
| 3697 | EXPORT_SYMBOL(skb_dequeue); |
| 3698 | |
| 3699 | /** |
| 3700 | * skb_dequeue_tail - remove from the tail of the queue |
| 3701 | * @list: list to dequeue from |
| 3702 | * |
| 3703 | * Remove the tail of the list. The list lock is taken so the function |
| 3704 | * may be used safely with other locking list functions. The tail item is |
| 3705 | * returned or %NULL if the list is empty. |
| 3706 | */ |
| 3707 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) |
| 3708 | { |
| 3709 | unsigned long flags; |
| 3710 | struct sk_buff *result; |
| 3711 | |
| 3712 | spin_lock_irqsave(&list->lock, flags); |
| 3713 | result = __skb_dequeue_tail(list); |
| 3714 | spin_unlock_irqrestore(&list->lock, flags); |
| 3715 | return result; |
| 3716 | } |
| 3717 | EXPORT_SYMBOL(skb_dequeue_tail); |
| 3718 | |
| 3719 | /** |
| 3720 | * skb_queue_purge_reason - empty a list |
| 3721 | * @list: list to empty |
| 3722 | * @reason: drop reason |
| 3723 | * |
| 3724 | * Delete all buffers on an &sk_buff list. Each buffer is removed from |
| 3725 | * the list and one reference dropped. This function takes the list |
| 3726 | * lock and is atomic with respect to other list locking functions. |
| 3727 | */ |
| 3728 | void skb_queue_purge_reason(struct sk_buff_head *list, |
| 3729 | enum skb_drop_reason reason) |
| 3730 | { |
| 3731 | struct sk_buff_head tmp; |
| 3732 | unsigned long flags; |
| 3733 | |
| 3734 | if (skb_queue_empty_lockless(list)) |
| 3735 | return; |
| 3736 | |
| 3737 | __skb_queue_head_init(&tmp); |
| 3738 | |
| 3739 | spin_lock_irqsave(&list->lock, flags); |
| 3740 | skb_queue_splice_init(list, &tmp); |
| 3741 | spin_unlock_irqrestore(&list->lock, flags); |
| 3742 | |
| 3743 | __skb_queue_purge_reason(&tmp, reason); |
| 3744 | } |
| 3745 | EXPORT_SYMBOL(skb_queue_purge_reason); |
| 3746 | |
| 3747 | /** |
| 3748 | * skb_rbtree_purge - empty a skb rbtree |
| 3749 | * @root: root of the rbtree to empty |
| 3750 | * Return value: the sum of truesizes of all purged skbs. |
| 3751 | * |
| 3752 | * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from |
| 3753 | * the list and one reference dropped. This function does not take |
| 3754 | * any lock. Synchronization should be handled by the caller (e.g., TCP |
| 3755 | * out-of-order queue is protected by the socket lock). |
| 3756 | */ |
| 3757 | unsigned int skb_rbtree_purge(struct rb_root *root) |
| 3758 | { |
| 3759 | struct rb_node *p = rb_first(root); |
| 3760 | unsigned int sum = 0; |
| 3761 | |
| 3762 | while (p) { |
| 3763 | struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); |
| 3764 | |
| 3765 | p = rb_next(p); |
| 3766 | rb_erase(&skb->rbnode, root); |
| 3767 | sum += skb->truesize; |
| 3768 | kfree_skb(skb); |
| 3769 | } |
| 3770 | return sum; |
| 3771 | } |
| 3772 | |
| 3773 | void skb_errqueue_purge(struct sk_buff_head *list) |
| 3774 | { |
| 3775 | struct sk_buff *skb, *next; |
| 3776 | struct sk_buff_head kill; |
| 3777 | unsigned long flags; |
| 3778 | |
| 3779 | __skb_queue_head_init(&kill); |
| 3780 | |
| 3781 | spin_lock_irqsave(&list->lock, flags); |
| 3782 | skb_queue_walk_safe(list, skb, next) { |
| 3783 | if (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ZEROCOPY || |
| 3784 | SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) |
| 3785 | continue; |
| 3786 | __skb_unlink(skb, list); |
| 3787 | __skb_queue_tail(&kill, skb); |
| 3788 | } |
| 3789 | spin_unlock_irqrestore(&list->lock, flags); |
| 3790 | __skb_queue_purge(&kill); |
| 3791 | } |
| 3792 | EXPORT_SYMBOL(skb_errqueue_purge); |
| 3793 | |
| 3794 | /** |
| 3795 | * skb_queue_head - queue a buffer at the list head |
| 3796 | * @list: list to use |
| 3797 | * @newsk: buffer to queue |
| 3798 | * |
| 3799 | * Queue a buffer at the start of the list. This function takes the |
| 3800 | * list lock and can be used safely with other locking &sk_buff functions |
| 3801 | * safely. |
| 3802 | * |
| 3803 | * A buffer cannot be placed on two lists at the same time. |
| 3804 | */ |
| 3805 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) |
| 3806 | { |
| 3807 | unsigned long flags; |
| 3808 | |
| 3809 | spin_lock_irqsave(&list->lock, flags); |
| 3810 | __skb_queue_head(list, newsk); |
| 3811 | spin_unlock_irqrestore(&list->lock, flags); |
| 3812 | } |
| 3813 | EXPORT_SYMBOL(skb_queue_head); |
| 3814 | |
| 3815 | /** |
| 3816 | * skb_queue_tail - queue a buffer at the list tail |
| 3817 | * @list: list to use |
| 3818 | * @newsk: buffer to queue |
| 3819 | * |
| 3820 | * Queue a buffer at the tail of the list. This function takes the |
| 3821 | * list lock and can be used safely with other locking &sk_buff functions |
| 3822 | * safely. |
| 3823 | * |
| 3824 | * A buffer cannot be placed on two lists at the same time. |
| 3825 | */ |
| 3826 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) |
| 3827 | { |
| 3828 | unsigned long flags; |
| 3829 | |
| 3830 | spin_lock_irqsave(&list->lock, flags); |
| 3831 | __skb_queue_tail(list, newsk); |
| 3832 | spin_unlock_irqrestore(&list->lock, flags); |
| 3833 | } |
| 3834 | EXPORT_SYMBOL(skb_queue_tail); |
| 3835 | |
| 3836 | /** |
| 3837 | * skb_unlink - remove a buffer from a list |
| 3838 | * @skb: buffer to remove |
| 3839 | * @list: list to use |
| 3840 | * |
| 3841 | * Remove a packet from a list. The list locks are taken and this |
| 3842 | * function is atomic with respect to other list locked calls |
| 3843 | * |
| 3844 | * You must know what list the SKB is on. |
| 3845 | */ |
| 3846 | void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) |
| 3847 | { |
| 3848 | unsigned long flags; |
| 3849 | |
| 3850 | spin_lock_irqsave(&list->lock, flags); |
| 3851 | __skb_unlink(skb, list); |
| 3852 | spin_unlock_irqrestore(&list->lock, flags); |
| 3853 | } |
| 3854 | EXPORT_SYMBOL(skb_unlink); |
| 3855 | |
| 3856 | /** |
| 3857 | * skb_append - append a buffer |
| 3858 | * @old: buffer to insert after |
| 3859 | * @newsk: buffer to insert |
| 3860 | * @list: list to use |
| 3861 | * |
| 3862 | * Place a packet after a given packet in a list. The list locks are taken |
| 3863 | * and this function is atomic with respect to other list locked calls. |
| 3864 | * A buffer cannot be placed on two lists at the same time. |
| 3865 | */ |
| 3866 | void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) |
| 3867 | { |
| 3868 | unsigned long flags; |
| 3869 | |
| 3870 | spin_lock_irqsave(&list->lock, flags); |
| 3871 | __skb_queue_after(list, old, newsk); |
| 3872 | spin_unlock_irqrestore(&list->lock, flags); |
| 3873 | } |
| 3874 | EXPORT_SYMBOL(skb_append); |
| 3875 | |
| 3876 | static inline void skb_split_inside_header(struct sk_buff *skb, |
| 3877 | struct sk_buff* skb1, |
| 3878 | const u32 len, const int pos) |
| 3879 | { |
| 3880 | int i; |
| 3881 | |
| 3882 | skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), |
| 3883 | pos - len); |
| 3884 | /* And move data appendix as is. */ |
| 3885 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 3886 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 3887 | |
| 3888 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; |
| 3889 | skb_shinfo(skb)->nr_frags = 0; |
| 3890 | skb1->data_len = skb->data_len; |
| 3891 | skb1->len += skb1->data_len; |
| 3892 | skb->data_len = 0; |
| 3893 | skb->len = len; |
| 3894 | skb_set_tail_pointer(skb, len); |
| 3895 | } |
| 3896 | |
| 3897 | static inline void skb_split_no_header(struct sk_buff *skb, |
| 3898 | struct sk_buff* skb1, |
| 3899 | const u32 len, int pos) |
| 3900 | { |
| 3901 | int i, k = 0; |
| 3902 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 3903 | |
| 3904 | skb_shinfo(skb)->nr_frags = 0; |
| 3905 | skb1->len = skb1->data_len = skb->len - len; |
| 3906 | skb->len = len; |
| 3907 | skb->data_len = len - pos; |
| 3908 | |
| 3909 | for (i = 0; i < nfrags; i++) { |
| 3910 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 3911 | |
| 3912 | if (pos + size > len) { |
| 3913 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 3914 | |
| 3915 | if (pos < len) { |
| 3916 | /* Split frag. |
| 3917 | * We have two variants in this case: |
| 3918 | * 1. Move all the frag to the second |
| 3919 | * part, if it is possible. F.e. |
| 3920 | * this approach is mandatory for TUX, |
| 3921 | * where splitting is expensive. |
| 3922 | * 2. Split is accurately. We make this. |
| 3923 | */ |
| 3924 | skb_frag_ref(skb, i); |
| 3925 | skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); |
| 3926 | skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); |
| 3927 | skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); |
| 3928 | skb_shinfo(skb)->nr_frags++; |
| 3929 | } |
| 3930 | k++; |
| 3931 | } else |
| 3932 | skb_shinfo(skb)->nr_frags++; |
| 3933 | pos += size; |
| 3934 | } |
| 3935 | skb_shinfo(skb1)->nr_frags = k; |
| 3936 | } |
| 3937 | |
| 3938 | /** |
| 3939 | * skb_split - Split fragmented skb to two parts at length len. |
| 3940 | * @skb: the buffer to split |
| 3941 | * @skb1: the buffer to receive the second part |
| 3942 | * @len: new length for skb |
| 3943 | */ |
| 3944 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) |
| 3945 | { |
| 3946 | int pos = skb_headlen(skb); |
| 3947 | const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; |
| 3948 | |
| 3949 | skb_zcopy_downgrade_managed(skb); |
| 3950 | |
| 3951 | skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; |
| 3952 | skb_zerocopy_clone(skb1, skb, 0); |
| 3953 | if (len < pos) /* Split line is inside header. */ |
| 3954 | skb_split_inside_header(skb, skb1, len, pos); |
| 3955 | else /* Second chunk has no header, nothing to copy. */ |
| 3956 | skb_split_no_header(skb, skb1, len, pos); |
| 3957 | } |
| 3958 | EXPORT_SYMBOL(skb_split); |
| 3959 | |
| 3960 | /* Shifting from/to a cloned skb is a no-go. |
| 3961 | * |
| 3962 | * Caller cannot keep skb_shinfo related pointers past calling here! |
| 3963 | */ |
| 3964 | static int skb_prepare_for_shift(struct sk_buff *skb) |
| 3965 | { |
| 3966 | return skb_unclone_keeptruesize(skb, GFP_ATOMIC); |
| 3967 | } |
| 3968 | |
| 3969 | /** |
| 3970 | * skb_shift - Shifts paged data partially from skb to another |
| 3971 | * @tgt: buffer into which tail data gets added |
| 3972 | * @skb: buffer from which the paged data comes from |
| 3973 | * @shiftlen: shift up to this many bytes |
| 3974 | * |
| 3975 | * Attempts to shift up to shiftlen worth of bytes, which may be less than |
| 3976 | * the length of the skb, from skb to tgt. Returns number bytes shifted. |
| 3977 | * It's up to caller to free skb if everything was shifted. |
| 3978 | * |
| 3979 | * If @tgt runs out of frags, the whole operation is aborted. |
| 3980 | * |
| 3981 | * Skb cannot include anything else but paged data while tgt is allowed |
| 3982 | * to have non-paged data as well. |
| 3983 | * |
| 3984 | * TODO: full sized shift could be optimized but that would need |
| 3985 | * specialized skb free'er to handle frags without up-to-date nr_frags. |
| 3986 | */ |
| 3987 | int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) |
| 3988 | { |
| 3989 | int from, to, merge, todo; |
| 3990 | skb_frag_t *fragfrom, *fragto; |
| 3991 | |
| 3992 | BUG_ON(shiftlen > skb->len); |
| 3993 | |
| 3994 | if (skb_headlen(skb)) |
| 3995 | return 0; |
| 3996 | if (skb_zcopy(tgt) || skb_zcopy(skb)) |
| 3997 | return 0; |
| 3998 | |
| 3999 | todo = shiftlen; |
| 4000 | from = 0; |
| 4001 | to = skb_shinfo(tgt)->nr_frags; |
| 4002 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 4003 | |
| 4004 | /* Actual merge is delayed until the point when we know we can |
| 4005 | * commit all, so that we don't have to undo partial changes |
| 4006 | */ |
| 4007 | if (!to || |
| 4008 | !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), |
| 4009 | skb_frag_off(fragfrom))) { |
| 4010 | merge = -1; |
| 4011 | } else { |
| 4012 | merge = to - 1; |
| 4013 | |
| 4014 | todo -= skb_frag_size(fragfrom); |
| 4015 | if (todo < 0) { |
| 4016 | if (skb_prepare_for_shift(skb) || |
| 4017 | skb_prepare_for_shift(tgt)) |
| 4018 | return 0; |
| 4019 | |
| 4020 | /* All previous frag pointers might be stale! */ |
| 4021 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 4022 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 4023 | |
| 4024 | skb_frag_size_add(fragto, shiftlen); |
| 4025 | skb_frag_size_sub(fragfrom, shiftlen); |
| 4026 | skb_frag_off_add(fragfrom, shiftlen); |
| 4027 | |
| 4028 | goto onlymerged; |
| 4029 | } |
| 4030 | |
| 4031 | from++; |
| 4032 | } |
| 4033 | |
| 4034 | /* Skip full, not-fitting skb to avoid expensive operations */ |
| 4035 | if ((shiftlen == skb->len) && |
| 4036 | (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) |
| 4037 | return 0; |
| 4038 | |
| 4039 | if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) |
| 4040 | return 0; |
| 4041 | |
| 4042 | while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { |
| 4043 | if (to == MAX_SKB_FRAGS) |
| 4044 | return 0; |
| 4045 | |
| 4046 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 4047 | fragto = &skb_shinfo(tgt)->frags[to]; |
| 4048 | |
| 4049 | if (todo >= skb_frag_size(fragfrom)) { |
| 4050 | *fragto = *fragfrom; |
| 4051 | todo -= skb_frag_size(fragfrom); |
| 4052 | from++; |
| 4053 | to++; |
| 4054 | |
| 4055 | } else { |
| 4056 | __skb_frag_ref(fragfrom); |
| 4057 | skb_frag_page_copy(fragto, fragfrom); |
| 4058 | skb_frag_off_copy(fragto, fragfrom); |
| 4059 | skb_frag_size_set(fragto, todo); |
| 4060 | |
| 4061 | skb_frag_off_add(fragfrom, todo); |
| 4062 | skb_frag_size_sub(fragfrom, todo); |
| 4063 | todo = 0; |
| 4064 | |
| 4065 | to++; |
| 4066 | break; |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | /* Ready to "commit" this state change to tgt */ |
| 4071 | skb_shinfo(tgt)->nr_frags = to; |
| 4072 | |
| 4073 | if (merge >= 0) { |
| 4074 | fragfrom = &skb_shinfo(skb)->frags[0]; |
| 4075 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 4076 | |
| 4077 | skb_frag_size_add(fragto, skb_frag_size(fragfrom)); |
| 4078 | __skb_frag_unref(fragfrom, skb->pp_recycle); |
| 4079 | } |
| 4080 | |
| 4081 | /* Reposition in the original skb */ |
| 4082 | to = 0; |
| 4083 | while (from < skb_shinfo(skb)->nr_frags) |
| 4084 | skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; |
| 4085 | skb_shinfo(skb)->nr_frags = to; |
| 4086 | |
| 4087 | BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); |
| 4088 | |
| 4089 | onlymerged: |
| 4090 | /* Most likely the tgt won't ever need its checksum anymore, skb on |
| 4091 | * the other hand might need it if it needs to be resent |
| 4092 | */ |
| 4093 | tgt->ip_summed = CHECKSUM_PARTIAL; |
| 4094 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 4095 | |
| 4096 | skb_len_add(skb, -shiftlen); |
| 4097 | skb_len_add(tgt, shiftlen); |
| 4098 | |
| 4099 | return shiftlen; |
| 4100 | } |
| 4101 | |
| 4102 | /** |
| 4103 | * skb_prepare_seq_read - Prepare a sequential read of skb data |
| 4104 | * @skb: the buffer to read |
| 4105 | * @from: lower offset of data to be read |
| 4106 | * @to: upper offset of data to be read |
| 4107 | * @st: state variable |
| 4108 | * |
| 4109 | * Initializes the specified state variable. Must be called before |
| 4110 | * invoking skb_seq_read() for the first time. |
| 4111 | */ |
| 4112 | void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, |
| 4113 | unsigned int to, struct skb_seq_state *st) |
| 4114 | { |
| 4115 | st->lower_offset = from; |
| 4116 | st->upper_offset = to; |
| 4117 | st->root_skb = st->cur_skb = skb; |
| 4118 | st->frag_idx = st->stepped_offset = 0; |
| 4119 | st->frag_data = NULL; |
| 4120 | st->frag_off = 0; |
| 4121 | } |
| 4122 | EXPORT_SYMBOL(skb_prepare_seq_read); |
| 4123 | |
| 4124 | /** |
| 4125 | * skb_seq_read - Sequentially read skb data |
| 4126 | * @consumed: number of bytes consumed by the caller so far |
| 4127 | * @data: destination pointer for data to be returned |
| 4128 | * @st: state variable |
| 4129 | * |
| 4130 | * Reads a block of skb data at @consumed relative to the |
| 4131 | * lower offset specified to skb_prepare_seq_read(). Assigns |
| 4132 | * the head of the data block to @data and returns the length |
| 4133 | * of the block or 0 if the end of the skb data or the upper |
| 4134 | * offset has been reached. |
| 4135 | * |
| 4136 | * The caller is not required to consume all of the data |
| 4137 | * returned, i.e. @consumed is typically set to the number |
| 4138 | * of bytes already consumed and the next call to |
| 4139 | * skb_seq_read() will return the remaining part of the block. |
| 4140 | * |
| 4141 | * Note 1: The size of each block of data returned can be arbitrary, |
| 4142 | * this limitation is the cost for zerocopy sequential |
| 4143 | * reads of potentially non linear data. |
| 4144 | * |
| 4145 | * Note 2: Fragment lists within fragments are not implemented |
| 4146 | * at the moment, state->root_skb could be replaced with |
| 4147 | * a stack for this purpose. |
| 4148 | */ |
| 4149 | unsigned int skb_seq_read(unsigned int consumed, const u8 **data, |
| 4150 | struct skb_seq_state *st) |
| 4151 | { |
| 4152 | unsigned int block_limit, abs_offset = consumed + st->lower_offset; |
| 4153 | skb_frag_t *frag; |
| 4154 | |
| 4155 | if (unlikely(abs_offset >= st->upper_offset)) { |
| 4156 | if (st->frag_data) { |
| 4157 | kunmap_atomic(st->frag_data); |
| 4158 | st->frag_data = NULL; |
| 4159 | } |
| 4160 | return 0; |
| 4161 | } |
| 4162 | |
| 4163 | next_skb: |
| 4164 | block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; |
| 4165 | |
| 4166 | if (abs_offset < block_limit && !st->frag_data) { |
| 4167 | *data = st->cur_skb->data + (abs_offset - st->stepped_offset); |
| 4168 | return block_limit - abs_offset; |
| 4169 | } |
| 4170 | |
| 4171 | if (st->frag_idx == 0 && !st->frag_data) |
| 4172 | st->stepped_offset += skb_headlen(st->cur_skb); |
| 4173 | |
| 4174 | while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { |
| 4175 | unsigned int pg_idx, pg_off, pg_sz; |
| 4176 | |
| 4177 | frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; |
| 4178 | |
| 4179 | pg_idx = 0; |
| 4180 | pg_off = skb_frag_off(frag); |
| 4181 | pg_sz = skb_frag_size(frag); |
| 4182 | |
| 4183 | if (skb_frag_must_loop(skb_frag_page(frag))) { |
| 4184 | pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; |
| 4185 | pg_off = offset_in_page(pg_off + st->frag_off); |
| 4186 | pg_sz = min_t(unsigned int, pg_sz - st->frag_off, |
| 4187 | PAGE_SIZE - pg_off); |
| 4188 | } |
| 4189 | |
| 4190 | block_limit = pg_sz + st->stepped_offset; |
| 4191 | if (abs_offset < block_limit) { |
| 4192 | if (!st->frag_data) |
| 4193 | st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); |
| 4194 | |
| 4195 | *data = (u8 *)st->frag_data + pg_off + |
| 4196 | (abs_offset - st->stepped_offset); |
| 4197 | |
| 4198 | return block_limit - abs_offset; |
| 4199 | } |
| 4200 | |
| 4201 | if (st->frag_data) { |
| 4202 | kunmap_atomic(st->frag_data); |
| 4203 | st->frag_data = NULL; |
| 4204 | } |
| 4205 | |
| 4206 | st->stepped_offset += pg_sz; |
| 4207 | st->frag_off += pg_sz; |
| 4208 | if (st->frag_off == skb_frag_size(frag)) { |
| 4209 | st->frag_off = 0; |
| 4210 | st->frag_idx++; |
| 4211 | } |
| 4212 | } |
| 4213 | |
| 4214 | if (st->frag_data) { |
| 4215 | kunmap_atomic(st->frag_data); |
| 4216 | st->frag_data = NULL; |
| 4217 | } |
| 4218 | |
| 4219 | if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { |
| 4220 | st->cur_skb = skb_shinfo(st->root_skb)->frag_list; |
| 4221 | st->frag_idx = 0; |
| 4222 | goto next_skb; |
| 4223 | } else if (st->cur_skb->next) { |
| 4224 | st->cur_skb = st->cur_skb->next; |
| 4225 | st->frag_idx = 0; |
| 4226 | goto next_skb; |
| 4227 | } |
| 4228 | |
| 4229 | return 0; |
| 4230 | } |
| 4231 | EXPORT_SYMBOL(skb_seq_read); |
| 4232 | |
| 4233 | /** |
| 4234 | * skb_abort_seq_read - Abort a sequential read of skb data |
| 4235 | * @st: state variable |
| 4236 | * |
| 4237 | * Must be called if skb_seq_read() was not called until it |
| 4238 | * returned 0. |
| 4239 | */ |
| 4240 | void skb_abort_seq_read(struct skb_seq_state *st) |
| 4241 | { |
| 4242 | if (st->frag_data) |
| 4243 | kunmap_atomic(st->frag_data); |
| 4244 | } |
| 4245 | EXPORT_SYMBOL(skb_abort_seq_read); |
| 4246 | |
| 4247 | #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) |
| 4248 | |
| 4249 | static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, |
| 4250 | struct ts_config *conf, |
| 4251 | struct ts_state *state) |
| 4252 | { |
| 4253 | return skb_seq_read(offset, text, TS_SKB_CB(state)); |
| 4254 | } |
| 4255 | |
| 4256 | static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) |
| 4257 | { |
| 4258 | skb_abort_seq_read(TS_SKB_CB(state)); |
| 4259 | } |
| 4260 | |
| 4261 | /** |
| 4262 | * skb_find_text - Find a text pattern in skb data |
| 4263 | * @skb: the buffer to look in |
| 4264 | * @from: search offset |
| 4265 | * @to: search limit |
| 4266 | * @config: textsearch configuration |
| 4267 | * |
| 4268 | * Finds a pattern in the skb data according to the specified |
| 4269 | * textsearch configuration. Use textsearch_next() to retrieve |
| 4270 | * subsequent occurrences of the pattern. Returns the offset |
| 4271 | * to the first occurrence or UINT_MAX if no match was found. |
| 4272 | */ |
| 4273 | unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, |
| 4274 | unsigned int to, struct ts_config *config) |
| 4275 | { |
| 4276 | unsigned int patlen = config->ops->get_pattern_len(config); |
| 4277 | struct ts_state state; |
| 4278 | unsigned int ret; |
| 4279 | |
| 4280 | BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); |
| 4281 | |
| 4282 | config->get_next_block = skb_ts_get_next_block; |
| 4283 | config->finish = skb_ts_finish; |
| 4284 | |
| 4285 | skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); |
| 4286 | |
| 4287 | ret = textsearch_find(config, &state); |
| 4288 | return (ret + patlen <= to - from ? ret : UINT_MAX); |
| 4289 | } |
| 4290 | EXPORT_SYMBOL(skb_find_text); |
| 4291 | |
| 4292 | int skb_append_pagefrags(struct sk_buff *skb, struct page *page, |
| 4293 | int offset, size_t size, size_t max_frags) |
| 4294 | { |
| 4295 | int i = skb_shinfo(skb)->nr_frags; |
| 4296 | |
| 4297 | if (skb_can_coalesce(skb, i, page, offset)) { |
| 4298 | skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); |
| 4299 | } else if (i < max_frags) { |
| 4300 | skb_zcopy_downgrade_managed(skb); |
| 4301 | get_page(page); |
| 4302 | skb_fill_page_desc_noacc(skb, i, page, offset, size); |
| 4303 | } else { |
| 4304 | return -EMSGSIZE; |
| 4305 | } |
| 4306 | |
| 4307 | return 0; |
| 4308 | } |
| 4309 | EXPORT_SYMBOL_GPL(skb_append_pagefrags); |
| 4310 | |
| 4311 | /** |
| 4312 | * skb_pull_rcsum - pull skb and update receive checksum |
| 4313 | * @skb: buffer to update |
| 4314 | * @len: length of data pulled |
| 4315 | * |
| 4316 | * This function performs an skb_pull on the packet and updates |
| 4317 | * the CHECKSUM_COMPLETE checksum. It should be used on |
| 4318 | * receive path processing instead of skb_pull unless you know |
| 4319 | * that the checksum difference is zero (e.g., a valid IP header) |
| 4320 | * or you are setting ip_summed to CHECKSUM_NONE. |
| 4321 | */ |
| 4322 | void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) |
| 4323 | { |
| 4324 | unsigned char *data = skb->data; |
| 4325 | |
| 4326 | BUG_ON(len > skb->len); |
| 4327 | __skb_pull(skb, len); |
| 4328 | skb_postpull_rcsum(skb, data, len); |
| 4329 | return skb->data; |
| 4330 | } |
| 4331 | EXPORT_SYMBOL_GPL(skb_pull_rcsum); |
| 4332 | |
| 4333 | static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) |
| 4334 | { |
| 4335 | skb_frag_t head_frag; |
| 4336 | struct page *page; |
| 4337 | |
| 4338 | page = virt_to_head_page(frag_skb->head); |
| 4339 | skb_frag_fill_page_desc(&head_frag, page, frag_skb->data - |
| 4340 | (unsigned char *)page_address(page), |
| 4341 | skb_headlen(frag_skb)); |
| 4342 | return head_frag; |
| 4343 | } |
| 4344 | |
| 4345 | struct sk_buff *skb_segment_list(struct sk_buff *skb, |
| 4346 | netdev_features_t features, |
| 4347 | unsigned int offset) |
| 4348 | { |
| 4349 | struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; |
| 4350 | unsigned int tnl_hlen = skb_tnl_header_len(skb); |
| 4351 | unsigned int delta_truesize = 0; |
| 4352 | unsigned int delta_len = 0; |
| 4353 | struct sk_buff *tail = NULL; |
| 4354 | struct sk_buff *nskb, *tmp; |
| 4355 | int len_diff, err; |
| 4356 | |
| 4357 | skb_push(skb, -skb_network_offset(skb) + offset); |
| 4358 | |
| 4359 | /* Ensure the head is writeable before touching the shared info */ |
| 4360 | err = skb_unclone(skb, GFP_ATOMIC); |
| 4361 | if (err) |
| 4362 | goto err_linearize; |
| 4363 | |
| 4364 | skb_shinfo(skb)->frag_list = NULL; |
| 4365 | |
| 4366 | while (list_skb) { |
| 4367 | nskb = list_skb; |
| 4368 | list_skb = list_skb->next; |
| 4369 | |
| 4370 | err = 0; |
| 4371 | delta_truesize += nskb->truesize; |
| 4372 | if (skb_shared(nskb)) { |
| 4373 | tmp = skb_clone(nskb, GFP_ATOMIC); |
| 4374 | if (tmp) { |
| 4375 | consume_skb(nskb); |
| 4376 | nskb = tmp; |
| 4377 | err = skb_unclone(nskb, GFP_ATOMIC); |
| 4378 | } else { |
| 4379 | err = -ENOMEM; |
| 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | if (!tail) |
| 4384 | skb->next = nskb; |
| 4385 | else |
| 4386 | tail->next = nskb; |
| 4387 | |
| 4388 | if (unlikely(err)) { |
| 4389 | nskb->next = list_skb; |
| 4390 | goto err_linearize; |
| 4391 | } |
| 4392 | |
| 4393 | tail = nskb; |
| 4394 | |
| 4395 | delta_len += nskb->len; |
| 4396 | |
| 4397 | skb_push(nskb, -skb_network_offset(nskb) + offset); |
| 4398 | |
| 4399 | skb_release_head_state(nskb); |
| 4400 | len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); |
| 4401 | __copy_skb_header(nskb, skb); |
| 4402 | |
| 4403 | skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); |
| 4404 | nskb->transport_header += len_diff; |
| 4405 | skb_copy_from_linear_data_offset(skb, -tnl_hlen, |
| 4406 | nskb->data - tnl_hlen, |
| 4407 | offset + tnl_hlen); |
| 4408 | |
| 4409 | if (skb_needs_linearize(nskb, features) && |
| 4410 | __skb_linearize(nskb)) |
| 4411 | goto err_linearize; |
| 4412 | } |
| 4413 | |
| 4414 | skb->truesize = skb->truesize - delta_truesize; |
| 4415 | skb->data_len = skb->data_len - delta_len; |
| 4416 | skb->len = skb->len - delta_len; |
| 4417 | |
| 4418 | skb_gso_reset(skb); |
| 4419 | |
| 4420 | skb->prev = tail; |
| 4421 | |
| 4422 | if (skb_needs_linearize(skb, features) && |
| 4423 | __skb_linearize(skb)) |
| 4424 | goto err_linearize; |
| 4425 | |
| 4426 | skb_get(skb); |
| 4427 | |
| 4428 | return skb; |
| 4429 | |
| 4430 | err_linearize: |
| 4431 | kfree_skb_list(skb->next); |
| 4432 | skb->next = NULL; |
| 4433 | return ERR_PTR(-ENOMEM); |
| 4434 | } |
| 4435 | EXPORT_SYMBOL_GPL(skb_segment_list); |
| 4436 | |
| 4437 | /** |
| 4438 | * skb_segment - Perform protocol segmentation on skb. |
| 4439 | * @head_skb: buffer to segment |
| 4440 | * @features: features for the output path (see dev->features) |
| 4441 | * |
| 4442 | * This function performs segmentation on the given skb. It returns |
| 4443 | * a pointer to the first in a list of new skbs for the segments. |
| 4444 | * In case of error it returns ERR_PTR(err). |
| 4445 | */ |
| 4446 | struct sk_buff *skb_segment(struct sk_buff *head_skb, |
| 4447 | netdev_features_t features) |
| 4448 | { |
| 4449 | struct sk_buff *segs = NULL; |
| 4450 | struct sk_buff *tail = NULL; |
| 4451 | struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; |
| 4452 | unsigned int mss = skb_shinfo(head_skb)->gso_size; |
| 4453 | unsigned int doffset = head_skb->data - skb_mac_header(head_skb); |
| 4454 | unsigned int offset = doffset; |
| 4455 | unsigned int tnl_hlen = skb_tnl_header_len(head_skb); |
| 4456 | unsigned int partial_segs = 0; |
| 4457 | unsigned int headroom; |
| 4458 | unsigned int len = head_skb->len; |
| 4459 | struct sk_buff *frag_skb; |
| 4460 | skb_frag_t *frag; |
| 4461 | __be16 proto; |
| 4462 | bool csum, sg; |
| 4463 | int err = -ENOMEM; |
| 4464 | int i = 0; |
| 4465 | int nfrags, pos; |
| 4466 | |
| 4467 | if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) && |
| 4468 | mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) { |
| 4469 | struct sk_buff *check_skb; |
| 4470 | |
| 4471 | for (check_skb = list_skb; check_skb; check_skb = check_skb->next) { |
| 4472 | if (skb_headlen(check_skb) && !check_skb->head_frag) { |
| 4473 | /* gso_size is untrusted, and we have a frag_list with |
| 4474 | * a linear non head_frag item. |
| 4475 | * |
| 4476 | * If head_skb's headlen does not fit requested gso_size, |
| 4477 | * it means that the frag_list members do NOT terminate |
| 4478 | * on exact gso_size boundaries. Hence we cannot perform |
| 4479 | * skb_frag_t page sharing. Therefore we must fallback to |
| 4480 | * copying the frag_list skbs; we do so by disabling SG. |
| 4481 | */ |
| 4482 | features &= ~NETIF_F_SG; |
| 4483 | break; |
| 4484 | } |
| 4485 | } |
| 4486 | } |
| 4487 | |
| 4488 | __skb_push(head_skb, doffset); |
| 4489 | proto = skb_network_protocol(head_skb, NULL); |
| 4490 | if (unlikely(!proto)) |
| 4491 | return ERR_PTR(-EINVAL); |
| 4492 | |
| 4493 | sg = !!(features & NETIF_F_SG); |
| 4494 | csum = !!can_checksum_protocol(features, proto); |
| 4495 | |
| 4496 | if (sg && csum && (mss != GSO_BY_FRAGS)) { |
| 4497 | if (!(features & NETIF_F_GSO_PARTIAL)) { |
| 4498 | struct sk_buff *iter; |
| 4499 | unsigned int frag_len; |
| 4500 | |
| 4501 | if (!list_skb || |
| 4502 | !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) |
| 4503 | goto normal; |
| 4504 | |
| 4505 | /* If we get here then all the required |
| 4506 | * GSO features except frag_list are supported. |
| 4507 | * Try to split the SKB to multiple GSO SKBs |
| 4508 | * with no frag_list. |
| 4509 | * Currently we can do that only when the buffers don't |
| 4510 | * have a linear part and all the buffers except |
| 4511 | * the last are of the same length. |
| 4512 | */ |
| 4513 | frag_len = list_skb->len; |
| 4514 | skb_walk_frags(head_skb, iter) { |
| 4515 | if (frag_len != iter->len && iter->next) |
| 4516 | goto normal; |
| 4517 | if (skb_headlen(iter) && !iter->head_frag) |
| 4518 | goto normal; |
| 4519 | |
| 4520 | len -= iter->len; |
| 4521 | } |
| 4522 | |
| 4523 | if (len != frag_len) |
| 4524 | goto normal; |
| 4525 | } |
| 4526 | |
| 4527 | /* GSO partial only requires that we trim off any excess that |
| 4528 | * doesn't fit into an MSS sized block, so take care of that |
| 4529 | * now. |
| 4530 | * Cap len to not accidentally hit GSO_BY_FRAGS. |
| 4531 | */ |
| 4532 | partial_segs = min(len, GSO_BY_FRAGS - 1) / mss; |
| 4533 | if (partial_segs > 1) |
| 4534 | mss *= partial_segs; |
| 4535 | else |
| 4536 | partial_segs = 0; |
| 4537 | } |
| 4538 | |
| 4539 | normal: |
| 4540 | headroom = skb_headroom(head_skb); |
| 4541 | pos = skb_headlen(head_skb); |
| 4542 | |
| 4543 | if (skb_orphan_frags(head_skb, GFP_ATOMIC)) |
| 4544 | return ERR_PTR(-ENOMEM); |
| 4545 | |
| 4546 | nfrags = skb_shinfo(head_skb)->nr_frags; |
| 4547 | frag = skb_shinfo(head_skb)->frags; |
| 4548 | frag_skb = head_skb; |
| 4549 | |
| 4550 | do { |
| 4551 | struct sk_buff *nskb; |
| 4552 | skb_frag_t *nskb_frag; |
| 4553 | int hsize; |
| 4554 | int size; |
| 4555 | |
| 4556 | if (unlikely(mss == GSO_BY_FRAGS)) { |
| 4557 | len = list_skb->len; |
| 4558 | } else { |
| 4559 | len = head_skb->len - offset; |
| 4560 | if (len > mss) |
| 4561 | len = mss; |
| 4562 | } |
| 4563 | |
| 4564 | hsize = skb_headlen(head_skb) - offset; |
| 4565 | |
| 4566 | if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && |
| 4567 | (skb_headlen(list_skb) == len || sg)) { |
| 4568 | BUG_ON(skb_headlen(list_skb) > len); |
| 4569 | |
| 4570 | nskb = skb_clone(list_skb, GFP_ATOMIC); |
| 4571 | if (unlikely(!nskb)) |
| 4572 | goto err; |
| 4573 | |
| 4574 | i = 0; |
| 4575 | nfrags = skb_shinfo(list_skb)->nr_frags; |
| 4576 | frag = skb_shinfo(list_skb)->frags; |
| 4577 | frag_skb = list_skb; |
| 4578 | pos += skb_headlen(list_skb); |
| 4579 | |
| 4580 | while (pos < offset + len) { |
| 4581 | BUG_ON(i >= nfrags); |
| 4582 | |
| 4583 | size = skb_frag_size(frag); |
| 4584 | if (pos + size > offset + len) |
| 4585 | break; |
| 4586 | |
| 4587 | i++; |
| 4588 | pos += size; |
| 4589 | frag++; |
| 4590 | } |
| 4591 | |
| 4592 | list_skb = list_skb->next; |
| 4593 | |
| 4594 | if (unlikely(pskb_trim(nskb, len))) { |
| 4595 | kfree_skb(nskb); |
| 4596 | goto err; |
| 4597 | } |
| 4598 | |
| 4599 | hsize = skb_end_offset(nskb); |
| 4600 | if (skb_cow_head(nskb, doffset + headroom)) { |
| 4601 | kfree_skb(nskb); |
| 4602 | goto err; |
| 4603 | } |
| 4604 | |
| 4605 | nskb->truesize += skb_end_offset(nskb) - hsize; |
| 4606 | skb_release_head_state(nskb); |
| 4607 | __skb_push(nskb, doffset); |
| 4608 | } else { |
| 4609 | if (hsize < 0) |
| 4610 | hsize = 0; |
| 4611 | if (hsize > len || !sg) |
| 4612 | hsize = len; |
| 4613 | |
| 4614 | nskb = __alloc_skb(hsize + doffset + headroom, |
| 4615 | GFP_ATOMIC, skb_alloc_rx_flag(head_skb), |
| 4616 | NUMA_NO_NODE); |
| 4617 | |
| 4618 | if (unlikely(!nskb)) |
| 4619 | goto err; |
| 4620 | |
| 4621 | skb_reserve(nskb, headroom); |
| 4622 | __skb_put(nskb, doffset); |
| 4623 | } |
| 4624 | |
| 4625 | if (segs) |
| 4626 | tail->next = nskb; |
| 4627 | else |
| 4628 | segs = nskb; |
| 4629 | tail = nskb; |
| 4630 | |
| 4631 | __copy_skb_header(nskb, head_skb); |
| 4632 | |
| 4633 | skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); |
| 4634 | skb_reset_mac_len(nskb); |
| 4635 | |
| 4636 | skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, |
| 4637 | nskb->data - tnl_hlen, |
| 4638 | doffset + tnl_hlen); |
| 4639 | |
| 4640 | if (nskb->len == len + doffset) |
| 4641 | goto perform_csum_check; |
| 4642 | |
| 4643 | if (!sg) { |
| 4644 | if (!csum) { |
| 4645 | if (!nskb->remcsum_offload) |
| 4646 | nskb->ip_summed = CHECKSUM_NONE; |
| 4647 | SKB_GSO_CB(nskb)->csum = |
| 4648 | skb_copy_and_csum_bits(head_skb, offset, |
| 4649 | skb_put(nskb, |
| 4650 | len), |
| 4651 | len); |
| 4652 | SKB_GSO_CB(nskb)->csum_start = |
| 4653 | skb_headroom(nskb) + doffset; |
| 4654 | } else { |
| 4655 | if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len)) |
| 4656 | goto err; |
| 4657 | } |
| 4658 | continue; |
| 4659 | } |
| 4660 | |
| 4661 | nskb_frag = skb_shinfo(nskb)->frags; |
| 4662 | |
| 4663 | skb_copy_from_linear_data_offset(head_skb, offset, |
| 4664 | skb_put(nskb, hsize), hsize); |
| 4665 | |
| 4666 | skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & |
| 4667 | SKBFL_SHARED_FRAG; |
| 4668 | |
| 4669 | if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) |
| 4670 | goto err; |
| 4671 | |
| 4672 | while (pos < offset + len) { |
| 4673 | if (i >= nfrags) { |
| 4674 | if (skb_orphan_frags(list_skb, GFP_ATOMIC) || |
| 4675 | skb_zerocopy_clone(nskb, list_skb, |
| 4676 | GFP_ATOMIC)) |
| 4677 | goto err; |
| 4678 | |
| 4679 | i = 0; |
| 4680 | nfrags = skb_shinfo(list_skb)->nr_frags; |
| 4681 | frag = skb_shinfo(list_skb)->frags; |
| 4682 | frag_skb = list_skb; |
| 4683 | if (!skb_headlen(list_skb)) { |
| 4684 | BUG_ON(!nfrags); |
| 4685 | } else { |
| 4686 | BUG_ON(!list_skb->head_frag); |
| 4687 | |
| 4688 | /* to make room for head_frag. */ |
| 4689 | i--; |
| 4690 | frag--; |
| 4691 | } |
| 4692 | |
| 4693 | list_skb = list_skb->next; |
| 4694 | } |
| 4695 | |
| 4696 | if (unlikely(skb_shinfo(nskb)->nr_frags >= |
| 4697 | MAX_SKB_FRAGS)) { |
| 4698 | net_warn_ratelimited( |
| 4699 | "skb_segment: too many frags: %u %u\n", |
| 4700 | pos, mss); |
| 4701 | err = -EINVAL; |
| 4702 | goto err; |
| 4703 | } |
| 4704 | |
| 4705 | *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; |
| 4706 | __skb_frag_ref(nskb_frag); |
| 4707 | size = skb_frag_size(nskb_frag); |
| 4708 | |
| 4709 | if (pos < offset) { |
| 4710 | skb_frag_off_add(nskb_frag, offset - pos); |
| 4711 | skb_frag_size_sub(nskb_frag, offset - pos); |
| 4712 | } |
| 4713 | |
| 4714 | skb_shinfo(nskb)->nr_frags++; |
| 4715 | |
| 4716 | if (pos + size <= offset + len) { |
| 4717 | i++; |
| 4718 | frag++; |
| 4719 | pos += size; |
| 4720 | } else { |
| 4721 | skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); |
| 4722 | goto skip_fraglist; |
| 4723 | } |
| 4724 | |
| 4725 | nskb_frag++; |
| 4726 | } |
| 4727 | |
| 4728 | skip_fraglist: |
| 4729 | nskb->data_len = len - hsize; |
| 4730 | nskb->len += nskb->data_len; |
| 4731 | nskb->truesize += nskb->data_len; |
| 4732 | |
| 4733 | perform_csum_check: |
| 4734 | if (!csum) { |
| 4735 | if (skb_has_shared_frag(nskb) && |
| 4736 | __skb_linearize(nskb)) |
| 4737 | goto err; |
| 4738 | |
| 4739 | if (!nskb->remcsum_offload) |
| 4740 | nskb->ip_summed = CHECKSUM_NONE; |
| 4741 | SKB_GSO_CB(nskb)->csum = |
| 4742 | skb_checksum(nskb, doffset, |
| 4743 | nskb->len - doffset, 0); |
| 4744 | SKB_GSO_CB(nskb)->csum_start = |
| 4745 | skb_headroom(nskb) + doffset; |
| 4746 | } |
| 4747 | } while ((offset += len) < head_skb->len); |
| 4748 | |
| 4749 | /* Some callers want to get the end of the list. |
| 4750 | * Put it in segs->prev to avoid walking the list. |
| 4751 | * (see validate_xmit_skb_list() for example) |
| 4752 | */ |
| 4753 | segs->prev = tail; |
| 4754 | |
| 4755 | if (partial_segs) { |
| 4756 | struct sk_buff *iter; |
| 4757 | int type = skb_shinfo(head_skb)->gso_type; |
| 4758 | unsigned short gso_size = skb_shinfo(head_skb)->gso_size; |
| 4759 | |
| 4760 | /* Update type to add partial and then remove dodgy if set */ |
| 4761 | type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; |
| 4762 | type &= ~SKB_GSO_DODGY; |
| 4763 | |
| 4764 | /* Update GSO info and prepare to start updating headers on |
| 4765 | * our way back down the stack of protocols. |
| 4766 | */ |
| 4767 | for (iter = segs; iter; iter = iter->next) { |
| 4768 | skb_shinfo(iter)->gso_size = gso_size; |
| 4769 | skb_shinfo(iter)->gso_segs = partial_segs; |
| 4770 | skb_shinfo(iter)->gso_type = type; |
| 4771 | SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; |
| 4772 | } |
| 4773 | |
| 4774 | if (tail->len - doffset <= gso_size) |
| 4775 | skb_shinfo(tail)->gso_size = 0; |
| 4776 | else if (tail != segs) |
| 4777 | skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); |
| 4778 | } |
| 4779 | |
| 4780 | /* Following permits correct backpressure, for protocols |
| 4781 | * using skb_set_owner_w(). |
| 4782 | * Idea is to tranfert ownership from head_skb to last segment. |
| 4783 | */ |
| 4784 | if (head_skb->destructor == sock_wfree) { |
| 4785 | swap(tail->truesize, head_skb->truesize); |
| 4786 | swap(tail->destructor, head_skb->destructor); |
| 4787 | swap(tail->sk, head_skb->sk); |
| 4788 | } |
| 4789 | return segs; |
| 4790 | |
| 4791 | err: |
| 4792 | kfree_skb_list(segs); |
| 4793 | return ERR_PTR(err); |
| 4794 | } |
| 4795 | EXPORT_SYMBOL_GPL(skb_segment); |
| 4796 | |
| 4797 | #ifdef CONFIG_SKB_EXTENSIONS |
| 4798 | #define SKB_EXT_ALIGN_VALUE 8 |
| 4799 | #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) |
| 4800 | |
| 4801 | static const u8 skb_ext_type_len[] = { |
| 4802 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
| 4803 | [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), |
| 4804 | #endif |
| 4805 | #ifdef CONFIG_XFRM |
| 4806 | [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), |
| 4807 | #endif |
| 4808 | #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) |
| 4809 | [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), |
| 4810 | #endif |
| 4811 | #if IS_ENABLED(CONFIG_MPTCP) |
| 4812 | [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), |
| 4813 | #endif |
| 4814 | #if IS_ENABLED(CONFIG_MCTP_FLOWS) |
| 4815 | [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), |
| 4816 | #endif |
| 4817 | }; |
| 4818 | |
| 4819 | static __always_inline unsigned int skb_ext_total_length(void) |
| 4820 | { |
| 4821 | unsigned int l = SKB_EXT_CHUNKSIZEOF(struct skb_ext); |
| 4822 | int i; |
| 4823 | |
| 4824 | for (i = 0; i < ARRAY_SIZE(skb_ext_type_len); i++) |
| 4825 | l += skb_ext_type_len[i]; |
| 4826 | |
| 4827 | return l; |
| 4828 | } |
| 4829 | |
| 4830 | static void skb_extensions_init(void) |
| 4831 | { |
| 4832 | BUILD_BUG_ON(SKB_EXT_NUM >= 8); |
| 4833 | BUILD_BUG_ON(skb_ext_total_length() > 255); |
| 4834 | |
| 4835 | skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", |
| 4836 | SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), |
| 4837 | 0, |
| 4838 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
| 4839 | NULL); |
| 4840 | } |
| 4841 | #else |
| 4842 | static void skb_extensions_init(void) {} |
| 4843 | #endif |
| 4844 | |
| 4845 | /* The SKB kmem_cache slab is critical for network performance. Never |
| 4846 | * merge/alias the slab with similar sized objects. This avoids fragmentation |
| 4847 | * that hurts performance of kmem_cache_{alloc,free}_bulk APIs. |
| 4848 | */ |
| 4849 | #ifndef CONFIG_SLUB_TINY |
| 4850 | #define FLAG_SKB_NO_MERGE SLAB_NO_MERGE |
| 4851 | #else /* CONFIG_SLUB_TINY - simple loop in kmem_cache_alloc_bulk */ |
| 4852 | #define FLAG_SKB_NO_MERGE 0 |
| 4853 | #endif |
| 4854 | |
| 4855 | void __init skb_init(void) |
| 4856 | { |
| 4857 | skbuff_cache = kmem_cache_create_usercopy("skbuff_head_cache", |
| 4858 | sizeof(struct sk_buff), |
| 4859 | 0, |
| 4860 | SLAB_HWCACHE_ALIGN|SLAB_PANIC| |
| 4861 | FLAG_SKB_NO_MERGE, |
| 4862 | offsetof(struct sk_buff, cb), |
| 4863 | sizeof_field(struct sk_buff, cb), |
| 4864 | NULL); |
| 4865 | skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", |
| 4866 | sizeof(struct sk_buff_fclones), |
| 4867 | 0, |
| 4868 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
| 4869 | NULL); |
| 4870 | /* usercopy should only access first SKB_SMALL_HEAD_HEADROOM bytes. |
| 4871 | * struct skb_shared_info is located at the end of skb->head, |
| 4872 | * and should not be copied to/from user. |
| 4873 | */ |
| 4874 | skb_small_head_cache = kmem_cache_create_usercopy("skbuff_small_head", |
| 4875 | SKB_SMALL_HEAD_CACHE_SIZE, |
| 4876 | 0, |
| 4877 | SLAB_HWCACHE_ALIGN | SLAB_PANIC, |
| 4878 | 0, |
| 4879 | SKB_SMALL_HEAD_HEADROOM, |
| 4880 | NULL); |
| 4881 | skb_extensions_init(); |
| 4882 | } |
| 4883 | |
| 4884 | static int |
| 4885 | __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, |
| 4886 | unsigned int recursion_level) |
| 4887 | { |
| 4888 | int start = skb_headlen(skb); |
| 4889 | int i, copy = start - offset; |
| 4890 | struct sk_buff *frag_iter; |
| 4891 | int elt = 0; |
| 4892 | |
| 4893 | if (unlikely(recursion_level >= 24)) |
| 4894 | return -EMSGSIZE; |
| 4895 | |
| 4896 | if (copy > 0) { |
| 4897 | if (copy > len) |
| 4898 | copy = len; |
| 4899 | sg_set_buf(sg, skb->data + offset, copy); |
| 4900 | elt++; |
| 4901 | if ((len -= copy) == 0) |
| 4902 | return elt; |
| 4903 | offset += copy; |
| 4904 | } |
| 4905 | |
| 4906 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 4907 | int end; |
| 4908 | |
| 4909 | WARN_ON(start > offset + len); |
| 4910 | |
| 4911 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 4912 | if ((copy = end - offset) > 0) { |
| 4913 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 4914 | if (unlikely(elt && sg_is_last(&sg[elt - 1]))) |
| 4915 | return -EMSGSIZE; |
| 4916 | |
| 4917 | if (copy > len) |
| 4918 | copy = len; |
| 4919 | sg_set_page(&sg[elt], skb_frag_page(frag), copy, |
| 4920 | skb_frag_off(frag) + offset - start); |
| 4921 | elt++; |
| 4922 | if (!(len -= copy)) |
| 4923 | return elt; |
| 4924 | offset += copy; |
| 4925 | } |
| 4926 | start = end; |
| 4927 | } |
| 4928 | |
| 4929 | skb_walk_frags(skb, frag_iter) { |
| 4930 | int end, ret; |
| 4931 | |
| 4932 | WARN_ON(start > offset + len); |
| 4933 | |
| 4934 | end = start + frag_iter->len; |
| 4935 | if ((copy = end - offset) > 0) { |
| 4936 | if (unlikely(elt && sg_is_last(&sg[elt - 1]))) |
| 4937 | return -EMSGSIZE; |
| 4938 | |
| 4939 | if (copy > len) |
| 4940 | copy = len; |
| 4941 | ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, |
| 4942 | copy, recursion_level + 1); |
| 4943 | if (unlikely(ret < 0)) |
| 4944 | return ret; |
| 4945 | elt += ret; |
| 4946 | if ((len -= copy) == 0) |
| 4947 | return elt; |
| 4948 | offset += copy; |
| 4949 | } |
| 4950 | start = end; |
| 4951 | } |
| 4952 | BUG_ON(len); |
| 4953 | return elt; |
| 4954 | } |
| 4955 | |
| 4956 | /** |
| 4957 | * skb_to_sgvec - Fill a scatter-gather list from a socket buffer |
| 4958 | * @skb: Socket buffer containing the buffers to be mapped |
| 4959 | * @sg: The scatter-gather list to map into |
| 4960 | * @offset: The offset into the buffer's contents to start mapping |
| 4961 | * @len: Length of buffer space to be mapped |
| 4962 | * |
| 4963 | * Fill the specified scatter-gather list with mappings/pointers into a |
| 4964 | * region of the buffer space attached to a socket buffer. Returns either |
| 4965 | * the number of scatterlist items used, or -EMSGSIZE if the contents |
| 4966 | * could not fit. |
| 4967 | */ |
| 4968 | int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) |
| 4969 | { |
| 4970 | int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); |
| 4971 | |
| 4972 | if (nsg <= 0) |
| 4973 | return nsg; |
| 4974 | |
| 4975 | sg_mark_end(&sg[nsg - 1]); |
| 4976 | |
| 4977 | return nsg; |
| 4978 | } |
| 4979 | EXPORT_SYMBOL_GPL(skb_to_sgvec); |
| 4980 | |
| 4981 | /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given |
| 4982 | * sglist without mark the sg which contain last skb data as the end. |
| 4983 | * So the caller can mannipulate sg list as will when padding new data after |
| 4984 | * the first call without calling sg_unmark_end to expend sg list. |
| 4985 | * |
| 4986 | * Scenario to use skb_to_sgvec_nomark: |
| 4987 | * 1. sg_init_table |
| 4988 | * 2. skb_to_sgvec_nomark(payload1) |
| 4989 | * 3. skb_to_sgvec_nomark(payload2) |
| 4990 | * |
| 4991 | * This is equivalent to: |
| 4992 | * 1. sg_init_table |
| 4993 | * 2. skb_to_sgvec(payload1) |
| 4994 | * 3. sg_unmark_end |
| 4995 | * 4. skb_to_sgvec(payload2) |
| 4996 | * |
| 4997 | * When mapping mutilple payload conditionally, skb_to_sgvec_nomark |
| 4998 | * is more preferable. |
| 4999 | */ |
| 5000 | int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, |
| 5001 | int offset, int len) |
| 5002 | { |
| 5003 | return __skb_to_sgvec(skb, sg, offset, len, 0); |
| 5004 | } |
| 5005 | EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); |
| 5006 | |
| 5007 | |
| 5008 | |
| 5009 | /** |
| 5010 | * skb_cow_data - Check that a socket buffer's data buffers are writable |
| 5011 | * @skb: The socket buffer to check. |
| 5012 | * @tailbits: Amount of trailing space to be added |
| 5013 | * @trailer: Returned pointer to the skb where the @tailbits space begins |
| 5014 | * |
| 5015 | * Make sure that the data buffers attached to a socket buffer are |
| 5016 | * writable. If they are not, private copies are made of the data buffers |
| 5017 | * and the socket buffer is set to use these instead. |
| 5018 | * |
| 5019 | * If @tailbits is given, make sure that there is space to write @tailbits |
| 5020 | * bytes of data beyond current end of socket buffer. @trailer will be |
| 5021 | * set to point to the skb in which this space begins. |
| 5022 | * |
| 5023 | * The number of scatterlist elements required to completely map the |
| 5024 | * COW'd and extended socket buffer will be returned. |
| 5025 | */ |
| 5026 | int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) |
| 5027 | { |
| 5028 | int copyflag; |
| 5029 | int elt; |
| 5030 | struct sk_buff *skb1, **skb_p; |
| 5031 | |
| 5032 | /* If skb is cloned or its head is paged, reallocate |
| 5033 | * head pulling out all the pages (pages are considered not writable |
| 5034 | * at the moment even if they are anonymous). |
| 5035 | */ |
| 5036 | if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && |
| 5037 | !__pskb_pull_tail(skb, __skb_pagelen(skb))) |
| 5038 | return -ENOMEM; |
| 5039 | |
| 5040 | /* Easy case. Most of packets will go this way. */ |
| 5041 | if (!skb_has_frag_list(skb)) { |
| 5042 | /* A little of trouble, not enough of space for trailer. |
| 5043 | * This should not happen, when stack is tuned to generate |
| 5044 | * good frames. OK, on miss we reallocate and reserve even more |
| 5045 | * space, 128 bytes is fair. */ |
| 5046 | |
| 5047 | if (skb_tailroom(skb) < tailbits && |
| 5048 | pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) |
| 5049 | return -ENOMEM; |
| 5050 | |
| 5051 | /* Voila! */ |
| 5052 | *trailer = skb; |
| 5053 | return 1; |
| 5054 | } |
| 5055 | |
| 5056 | /* Misery. We are in troubles, going to mincer fragments... */ |
| 5057 | |
| 5058 | elt = 1; |
| 5059 | skb_p = &skb_shinfo(skb)->frag_list; |
| 5060 | copyflag = 0; |
| 5061 | |
| 5062 | while ((skb1 = *skb_p) != NULL) { |
| 5063 | int ntail = 0; |
| 5064 | |
| 5065 | /* The fragment is partially pulled by someone, |
| 5066 | * this can happen on input. Copy it and everything |
| 5067 | * after it. */ |
| 5068 | |
| 5069 | if (skb_shared(skb1)) |
| 5070 | copyflag = 1; |
| 5071 | |
| 5072 | /* If the skb is the last, worry about trailer. */ |
| 5073 | |
| 5074 | if (skb1->next == NULL && tailbits) { |
| 5075 | if (skb_shinfo(skb1)->nr_frags || |
| 5076 | skb_has_frag_list(skb1) || |
| 5077 | skb_tailroom(skb1) < tailbits) |
| 5078 | ntail = tailbits + 128; |
| 5079 | } |
| 5080 | |
| 5081 | if (copyflag || |
| 5082 | skb_cloned(skb1) || |
| 5083 | ntail || |
| 5084 | skb_shinfo(skb1)->nr_frags || |
| 5085 | skb_has_frag_list(skb1)) { |
| 5086 | struct sk_buff *skb2; |
| 5087 | |
| 5088 | /* Fuck, we are miserable poor guys... */ |
| 5089 | if (ntail == 0) |
| 5090 | skb2 = skb_copy(skb1, GFP_ATOMIC); |
| 5091 | else |
| 5092 | skb2 = skb_copy_expand(skb1, |
| 5093 | skb_headroom(skb1), |
| 5094 | ntail, |
| 5095 | GFP_ATOMIC); |
| 5096 | if (unlikely(skb2 == NULL)) |
| 5097 | return -ENOMEM; |
| 5098 | |
| 5099 | if (skb1->sk) |
| 5100 | skb_set_owner_w(skb2, skb1->sk); |
| 5101 | |
| 5102 | /* Looking around. Are we still alive? |
| 5103 | * OK, link new skb, drop old one */ |
| 5104 | |
| 5105 | skb2->next = skb1->next; |
| 5106 | *skb_p = skb2; |
| 5107 | kfree_skb(skb1); |
| 5108 | skb1 = skb2; |
| 5109 | } |
| 5110 | elt++; |
| 5111 | *trailer = skb1; |
| 5112 | skb_p = &skb1->next; |
| 5113 | } |
| 5114 | |
| 5115 | return elt; |
| 5116 | } |
| 5117 | EXPORT_SYMBOL_GPL(skb_cow_data); |
| 5118 | |
| 5119 | static void sock_rmem_free(struct sk_buff *skb) |
| 5120 | { |
| 5121 | struct sock *sk = skb->sk; |
| 5122 | |
| 5123 | atomic_sub(skb->truesize, &sk->sk_rmem_alloc); |
| 5124 | } |
| 5125 | |
| 5126 | static void skb_set_err_queue(struct sk_buff *skb) |
| 5127 | { |
| 5128 | /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. |
| 5129 | * So, it is safe to (mis)use it to mark skbs on the error queue. |
| 5130 | */ |
| 5131 | skb->pkt_type = PACKET_OUTGOING; |
| 5132 | BUILD_BUG_ON(PACKET_OUTGOING == 0); |
| 5133 | } |
| 5134 | |
| 5135 | /* |
| 5136 | * Note: We dont mem charge error packets (no sk_forward_alloc changes) |
| 5137 | */ |
| 5138 | int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) |
| 5139 | { |
| 5140 | if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= |
| 5141 | (unsigned int)READ_ONCE(sk->sk_rcvbuf)) |
| 5142 | return -ENOMEM; |
| 5143 | |
| 5144 | skb_orphan(skb); |
| 5145 | skb->sk = sk; |
| 5146 | skb->destructor = sock_rmem_free; |
| 5147 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); |
| 5148 | skb_set_err_queue(skb); |
| 5149 | |
| 5150 | /* before exiting rcu section, make sure dst is refcounted */ |
| 5151 | skb_dst_force(skb); |
| 5152 | |
| 5153 | skb_queue_tail(&sk->sk_error_queue, skb); |
| 5154 | if (!sock_flag(sk, SOCK_DEAD)) |
| 5155 | sk_error_report(sk); |
| 5156 | return 0; |
| 5157 | } |
| 5158 | EXPORT_SYMBOL(sock_queue_err_skb); |
| 5159 | |
| 5160 | static bool is_icmp_err_skb(const struct sk_buff *skb) |
| 5161 | { |
| 5162 | return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || |
| 5163 | SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); |
| 5164 | } |
| 5165 | |
| 5166 | struct sk_buff *sock_dequeue_err_skb(struct sock *sk) |
| 5167 | { |
| 5168 | struct sk_buff_head *q = &sk->sk_error_queue; |
| 5169 | struct sk_buff *skb, *skb_next = NULL; |
| 5170 | bool icmp_next = false; |
| 5171 | unsigned long flags; |
| 5172 | |
| 5173 | if (skb_queue_empty_lockless(q)) |
| 5174 | return NULL; |
| 5175 | |
| 5176 | spin_lock_irqsave(&q->lock, flags); |
| 5177 | skb = __skb_dequeue(q); |
| 5178 | if (skb && (skb_next = skb_peek(q))) { |
| 5179 | icmp_next = is_icmp_err_skb(skb_next); |
| 5180 | if (icmp_next) |
| 5181 | sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; |
| 5182 | } |
| 5183 | spin_unlock_irqrestore(&q->lock, flags); |
| 5184 | |
| 5185 | if (is_icmp_err_skb(skb) && !icmp_next) |
| 5186 | sk->sk_err = 0; |
| 5187 | |
| 5188 | if (skb_next) |
| 5189 | sk_error_report(sk); |
| 5190 | |
| 5191 | return skb; |
| 5192 | } |
| 5193 | EXPORT_SYMBOL(sock_dequeue_err_skb); |
| 5194 | |
| 5195 | /** |
| 5196 | * skb_clone_sk - create clone of skb, and take reference to socket |
| 5197 | * @skb: the skb to clone |
| 5198 | * |
| 5199 | * This function creates a clone of a buffer that holds a reference on |
| 5200 | * sk_refcnt. Buffers created via this function are meant to be |
| 5201 | * returned using sock_queue_err_skb, or free via kfree_skb. |
| 5202 | * |
| 5203 | * When passing buffers allocated with this function to sock_queue_err_skb |
| 5204 | * it is necessary to wrap the call with sock_hold/sock_put in order to |
| 5205 | * prevent the socket from being released prior to being enqueued on |
| 5206 | * the sk_error_queue. |
| 5207 | */ |
| 5208 | struct sk_buff *skb_clone_sk(struct sk_buff *skb) |
| 5209 | { |
| 5210 | struct sock *sk = skb->sk; |
| 5211 | struct sk_buff *clone; |
| 5212 | |
| 5213 | if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) |
| 5214 | return NULL; |
| 5215 | |
| 5216 | clone = skb_clone(skb, GFP_ATOMIC); |
| 5217 | if (!clone) { |
| 5218 | sock_put(sk); |
| 5219 | return NULL; |
| 5220 | } |
| 5221 | |
| 5222 | clone->sk = sk; |
| 5223 | clone->destructor = sock_efree; |
| 5224 | |
| 5225 | return clone; |
| 5226 | } |
| 5227 | EXPORT_SYMBOL(skb_clone_sk); |
| 5228 | |
| 5229 | static void __skb_complete_tx_timestamp(struct sk_buff *skb, |
| 5230 | struct sock *sk, |
| 5231 | int tstype, |
| 5232 | bool opt_stats) |
| 5233 | { |
| 5234 | struct sock_exterr_skb *serr; |
| 5235 | int err; |
| 5236 | |
| 5237 | BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); |
| 5238 | |
| 5239 | serr = SKB_EXT_ERR(skb); |
| 5240 | memset(serr, 0, sizeof(*serr)); |
| 5241 | serr->ee.ee_errno = ENOMSG; |
| 5242 | serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; |
| 5243 | serr->ee.ee_info = tstype; |
| 5244 | serr->opt_stats = opt_stats; |
| 5245 | serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; |
| 5246 | if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID) { |
| 5247 | serr->ee.ee_data = skb_shinfo(skb)->tskey; |
| 5248 | if (sk_is_tcp(sk)) |
| 5249 | serr->ee.ee_data -= atomic_read(&sk->sk_tskey); |
| 5250 | } |
| 5251 | |
| 5252 | err = sock_queue_err_skb(sk, skb); |
| 5253 | |
| 5254 | if (err) |
| 5255 | kfree_skb(skb); |
| 5256 | } |
| 5257 | |
| 5258 | static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) |
| 5259 | { |
| 5260 | bool ret; |
| 5261 | |
| 5262 | if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly)) |
| 5263 | return true; |
| 5264 | |
| 5265 | read_lock_bh(&sk->sk_callback_lock); |
| 5266 | ret = sk->sk_socket && sk->sk_socket->file && |
| 5267 | file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); |
| 5268 | read_unlock_bh(&sk->sk_callback_lock); |
| 5269 | return ret; |
| 5270 | } |
| 5271 | |
| 5272 | void skb_complete_tx_timestamp(struct sk_buff *skb, |
| 5273 | struct skb_shared_hwtstamps *hwtstamps) |
| 5274 | { |
| 5275 | struct sock *sk = skb->sk; |
| 5276 | |
| 5277 | if (!skb_may_tx_timestamp(sk, false)) |
| 5278 | goto err; |
| 5279 | |
| 5280 | /* Take a reference to prevent skb_orphan() from freeing the socket, |
| 5281 | * but only if the socket refcount is not zero. |
| 5282 | */ |
| 5283 | if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { |
| 5284 | *skb_hwtstamps(skb) = *hwtstamps; |
| 5285 | __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); |
| 5286 | sock_put(sk); |
| 5287 | return; |
| 5288 | } |
| 5289 | |
| 5290 | err: |
| 5291 | kfree_skb(skb); |
| 5292 | } |
| 5293 | EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); |
| 5294 | |
| 5295 | void __skb_tstamp_tx(struct sk_buff *orig_skb, |
| 5296 | const struct sk_buff *ack_skb, |
| 5297 | struct skb_shared_hwtstamps *hwtstamps, |
| 5298 | struct sock *sk, int tstype) |
| 5299 | { |
| 5300 | struct sk_buff *skb; |
| 5301 | bool tsonly, opt_stats = false; |
| 5302 | u32 tsflags; |
| 5303 | |
| 5304 | if (!sk) |
| 5305 | return; |
| 5306 | |
| 5307 | tsflags = READ_ONCE(sk->sk_tsflags); |
| 5308 | if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && |
| 5309 | skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) |
| 5310 | return; |
| 5311 | |
| 5312 | tsonly = tsflags & SOF_TIMESTAMPING_OPT_TSONLY; |
| 5313 | if (!skb_may_tx_timestamp(sk, tsonly)) |
| 5314 | return; |
| 5315 | |
| 5316 | if (tsonly) { |
| 5317 | #ifdef CONFIG_INET |
| 5318 | if ((tsflags & SOF_TIMESTAMPING_OPT_STATS) && |
| 5319 | sk_is_tcp(sk)) { |
| 5320 | skb = tcp_get_timestamping_opt_stats(sk, orig_skb, |
| 5321 | ack_skb); |
| 5322 | opt_stats = true; |
| 5323 | } else |
| 5324 | #endif |
| 5325 | skb = alloc_skb(0, GFP_ATOMIC); |
| 5326 | } else { |
| 5327 | skb = skb_clone(orig_skb, GFP_ATOMIC); |
| 5328 | |
| 5329 | if (skb_orphan_frags_rx(skb, GFP_ATOMIC)) { |
| 5330 | kfree_skb(skb); |
| 5331 | return; |
| 5332 | } |
| 5333 | } |
| 5334 | if (!skb) |
| 5335 | return; |
| 5336 | |
| 5337 | if (tsonly) { |
| 5338 | skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & |
| 5339 | SKBTX_ANY_TSTAMP; |
| 5340 | skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; |
| 5341 | } |
| 5342 | |
| 5343 | if (hwtstamps) |
| 5344 | *skb_hwtstamps(skb) = *hwtstamps; |
| 5345 | else |
| 5346 | __net_timestamp(skb); |
| 5347 | |
| 5348 | __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); |
| 5349 | } |
| 5350 | EXPORT_SYMBOL_GPL(__skb_tstamp_tx); |
| 5351 | |
| 5352 | void skb_tstamp_tx(struct sk_buff *orig_skb, |
| 5353 | struct skb_shared_hwtstamps *hwtstamps) |
| 5354 | { |
| 5355 | return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, |
| 5356 | SCM_TSTAMP_SND); |
| 5357 | } |
| 5358 | EXPORT_SYMBOL_GPL(skb_tstamp_tx); |
| 5359 | |
| 5360 | #ifdef CONFIG_WIRELESS |
| 5361 | void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) |
| 5362 | { |
| 5363 | struct sock *sk = skb->sk; |
| 5364 | struct sock_exterr_skb *serr; |
| 5365 | int err = 1; |
| 5366 | |
| 5367 | skb->wifi_acked_valid = 1; |
| 5368 | skb->wifi_acked = acked; |
| 5369 | |
| 5370 | serr = SKB_EXT_ERR(skb); |
| 5371 | memset(serr, 0, sizeof(*serr)); |
| 5372 | serr->ee.ee_errno = ENOMSG; |
| 5373 | serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; |
| 5374 | |
| 5375 | /* Take a reference to prevent skb_orphan() from freeing the socket, |
| 5376 | * but only if the socket refcount is not zero. |
| 5377 | */ |
| 5378 | if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { |
| 5379 | err = sock_queue_err_skb(sk, skb); |
| 5380 | sock_put(sk); |
| 5381 | } |
| 5382 | if (err) |
| 5383 | kfree_skb(skb); |
| 5384 | } |
| 5385 | EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); |
| 5386 | #endif /* CONFIG_WIRELESS */ |
| 5387 | |
| 5388 | /** |
| 5389 | * skb_partial_csum_set - set up and verify partial csum values for packet |
| 5390 | * @skb: the skb to set |
| 5391 | * @start: the number of bytes after skb->data to start checksumming. |
| 5392 | * @off: the offset from start to place the checksum. |
| 5393 | * |
| 5394 | * For untrusted partially-checksummed packets, we need to make sure the values |
| 5395 | * for skb->csum_start and skb->csum_offset are valid so we don't oops. |
| 5396 | * |
| 5397 | * This function checks and sets those values and skb->ip_summed: if this |
| 5398 | * returns false you should drop the packet. |
| 5399 | */ |
| 5400 | bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) |
| 5401 | { |
| 5402 | u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); |
| 5403 | u32 csum_start = skb_headroom(skb) + (u32)start; |
| 5404 | |
| 5405 | if (unlikely(csum_start >= U16_MAX || csum_end > skb_headlen(skb))) { |
| 5406 | net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", |
| 5407 | start, off, skb_headroom(skb), skb_headlen(skb)); |
| 5408 | return false; |
| 5409 | } |
| 5410 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 5411 | skb->csum_start = csum_start; |
| 5412 | skb->csum_offset = off; |
| 5413 | skb->transport_header = csum_start; |
| 5414 | return true; |
| 5415 | } |
| 5416 | EXPORT_SYMBOL_GPL(skb_partial_csum_set); |
| 5417 | |
| 5418 | static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, |
| 5419 | unsigned int max) |
| 5420 | { |
| 5421 | if (skb_headlen(skb) >= len) |
| 5422 | return 0; |
| 5423 | |
| 5424 | /* If we need to pullup then pullup to the max, so we |
| 5425 | * won't need to do it again. |
| 5426 | */ |
| 5427 | if (max > skb->len) |
| 5428 | max = skb->len; |
| 5429 | |
| 5430 | if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) |
| 5431 | return -ENOMEM; |
| 5432 | |
| 5433 | if (skb_headlen(skb) < len) |
| 5434 | return -EPROTO; |
| 5435 | |
| 5436 | return 0; |
| 5437 | } |
| 5438 | |
| 5439 | #define MAX_TCP_HDR_LEN (15 * 4) |
| 5440 | |
| 5441 | static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, |
| 5442 | typeof(IPPROTO_IP) proto, |
| 5443 | unsigned int off) |
| 5444 | { |
| 5445 | int err; |
| 5446 | |
| 5447 | switch (proto) { |
| 5448 | case IPPROTO_TCP: |
| 5449 | err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), |
| 5450 | off + MAX_TCP_HDR_LEN); |
| 5451 | if (!err && !skb_partial_csum_set(skb, off, |
| 5452 | offsetof(struct tcphdr, |
| 5453 | check))) |
| 5454 | err = -EPROTO; |
| 5455 | return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; |
| 5456 | |
| 5457 | case IPPROTO_UDP: |
| 5458 | err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), |
| 5459 | off + sizeof(struct udphdr)); |
| 5460 | if (!err && !skb_partial_csum_set(skb, off, |
| 5461 | offsetof(struct udphdr, |
| 5462 | check))) |
| 5463 | err = -EPROTO; |
| 5464 | return err ? ERR_PTR(err) : &udp_hdr(skb)->check; |
| 5465 | } |
| 5466 | |
| 5467 | return ERR_PTR(-EPROTO); |
| 5468 | } |
| 5469 | |
| 5470 | /* This value should be large enough to cover a tagged ethernet header plus |
| 5471 | * maximally sized IP and TCP or UDP headers. |
| 5472 | */ |
| 5473 | #define MAX_IP_HDR_LEN 128 |
| 5474 | |
| 5475 | static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) |
| 5476 | { |
| 5477 | unsigned int off; |
| 5478 | bool fragment; |
| 5479 | __sum16 *csum; |
| 5480 | int err; |
| 5481 | |
| 5482 | fragment = false; |
| 5483 | |
| 5484 | err = skb_maybe_pull_tail(skb, |
| 5485 | sizeof(struct iphdr), |
| 5486 | MAX_IP_HDR_LEN); |
| 5487 | if (err < 0) |
| 5488 | goto out; |
| 5489 | |
| 5490 | if (ip_is_fragment(ip_hdr(skb))) |
| 5491 | fragment = true; |
| 5492 | |
| 5493 | off = ip_hdrlen(skb); |
| 5494 | |
| 5495 | err = -EPROTO; |
| 5496 | |
| 5497 | if (fragment) |
| 5498 | goto out; |
| 5499 | |
| 5500 | csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); |
| 5501 | if (IS_ERR(csum)) |
| 5502 | return PTR_ERR(csum); |
| 5503 | |
| 5504 | if (recalculate) |
| 5505 | *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 5506 | ip_hdr(skb)->daddr, |
| 5507 | skb->len - off, |
| 5508 | ip_hdr(skb)->protocol, 0); |
| 5509 | err = 0; |
| 5510 | |
| 5511 | out: |
| 5512 | return err; |
| 5513 | } |
| 5514 | |
| 5515 | /* This value should be large enough to cover a tagged ethernet header plus |
| 5516 | * an IPv6 header, all options, and a maximal TCP or UDP header. |
| 5517 | */ |
| 5518 | #define MAX_IPV6_HDR_LEN 256 |
| 5519 | |
| 5520 | #define OPT_HDR(type, skb, off) \ |
| 5521 | (type *)(skb_network_header(skb) + (off)) |
| 5522 | |
| 5523 | static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) |
| 5524 | { |
| 5525 | int err; |
| 5526 | u8 nexthdr; |
| 5527 | unsigned int off; |
| 5528 | unsigned int len; |
| 5529 | bool fragment; |
| 5530 | bool done; |
| 5531 | __sum16 *csum; |
| 5532 | |
| 5533 | fragment = false; |
| 5534 | done = false; |
| 5535 | |
| 5536 | off = sizeof(struct ipv6hdr); |
| 5537 | |
| 5538 | err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); |
| 5539 | if (err < 0) |
| 5540 | goto out; |
| 5541 | |
| 5542 | nexthdr = ipv6_hdr(skb)->nexthdr; |
| 5543 | |
| 5544 | len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); |
| 5545 | while (off <= len && !done) { |
| 5546 | switch (nexthdr) { |
| 5547 | case IPPROTO_DSTOPTS: |
| 5548 | case IPPROTO_HOPOPTS: |
| 5549 | case IPPROTO_ROUTING: { |
| 5550 | struct ipv6_opt_hdr *hp; |
| 5551 | |
| 5552 | err = skb_maybe_pull_tail(skb, |
| 5553 | off + |
| 5554 | sizeof(struct ipv6_opt_hdr), |
| 5555 | MAX_IPV6_HDR_LEN); |
| 5556 | if (err < 0) |
| 5557 | goto out; |
| 5558 | |
| 5559 | hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); |
| 5560 | nexthdr = hp->nexthdr; |
| 5561 | off += ipv6_optlen(hp); |
| 5562 | break; |
| 5563 | } |
| 5564 | case IPPROTO_AH: { |
| 5565 | struct ip_auth_hdr *hp; |
| 5566 | |
| 5567 | err = skb_maybe_pull_tail(skb, |
| 5568 | off + |
| 5569 | sizeof(struct ip_auth_hdr), |
| 5570 | MAX_IPV6_HDR_LEN); |
| 5571 | if (err < 0) |
| 5572 | goto out; |
| 5573 | |
| 5574 | hp = OPT_HDR(struct ip_auth_hdr, skb, off); |
| 5575 | nexthdr = hp->nexthdr; |
| 5576 | off += ipv6_authlen(hp); |
| 5577 | break; |
| 5578 | } |
| 5579 | case IPPROTO_FRAGMENT: { |
| 5580 | struct frag_hdr *hp; |
| 5581 | |
| 5582 | err = skb_maybe_pull_tail(skb, |
| 5583 | off + |
| 5584 | sizeof(struct frag_hdr), |
| 5585 | MAX_IPV6_HDR_LEN); |
| 5586 | if (err < 0) |
| 5587 | goto out; |
| 5588 | |
| 5589 | hp = OPT_HDR(struct frag_hdr, skb, off); |
| 5590 | |
| 5591 | if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) |
| 5592 | fragment = true; |
| 5593 | |
| 5594 | nexthdr = hp->nexthdr; |
| 5595 | off += sizeof(struct frag_hdr); |
| 5596 | break; |
| 5597 | } |
| 5598 | default: |
| 5599 | done = true; |
| 5600 | break; |
| 5601 | } |
| 5602 | } |
| 5603 | |
| 5604 | err = -EPROTO; |
| 5605 | |
| 5606 | if (!done || fragment) |
| 5607 | goto out; |
| 5608 | |
| 5609 | csum = skb_checksum_setup_ip(skb, nexthdr, off); |
| 5610 | if (IS_ERR(csum)) |
| 5611 | return PTR_ERR(csum); |
| 5612 | |
| 5613 | if (recalculate) |
| 5614 | *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 5615 | &ipv6_hdr(skb)->daddr, |
| 5616 | skb->len - off, nexthdr, 0); |
| 5617 | err = 0; |
| 5618 | |
| 5619 | out: |
| 5620 | return err; |
| 5621 | } |
| 5622 | |
| 5623 | /** |
| 5624 | * skb_checksum_setup - set up partial checksum offset |
| 5625 | * @skb: the skb to set up |
| 5626 | * @recalculate: if true the pseudo-header checksum will be recalculated |
| 5627 | */ |
| 5628 | int skb_checksum_setup(struct sk_buff *skb, bool recalculate) |
| 5629 | { |
| 5630 | int err; |
| 5631 | |
| 5632 | switch (skb->protocol) { |
| 5633 | case htons(ETH_P_IP): |
| 5634 | err = skb_checksum_setup_ipv4(skb, recalculate); |
| 5635 | break; |
| 5636 | |
| 5637 | case htons(ETH_P_IPV6): |
| 5638 | err = skb_checksum_setup_ipv6(skb, recalculate); |
| 5639 | break; |
| 5640 | |
| 5641 | default: |
| 5642 | err = -EPROTO; |
| 5643 | break; |
| 5644 | } |
| 5645 | |
| 5646 | return err; |
| 5647 | } |
| 5648 | EXPORT_SYMBOL(skb_checksum_setup); |
| 5649 | |
| 5650 | /** |
| 5651 | * skb_checksum_maybe_trim - maybe trims the given skb |
| 5652 | * @skb: the skb to check |
| 5653 | * @transport_len: the data length beyond the network header |
| 5654 | * |
| 5655 | * Checks whether the given skb has data beyond the given transport length. |
| 5656 | * If so, returns a cloned skb trimmed to this transport length. |
| 5657 | * Otherwise returns the provided skb. Returns NULL in error cases |
| 5658 | * (e.g. transport_len exceeds skb length or out-of-memory). |
| 5659 | * |
| 5660 | * Caller needs to set the skb transport header and free any returned skb if it |
| 5661 | * differs from the provided skb. |
| 5662 | */ |
| 5663 | static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, |
| 5664 | unsigned int transport_len) |
| 5665 | { |
| 5666 | struct sk_buff *skb_chk; |
| 5667 | unsigned int len = skb_transport_offset(skb) + transport_len; |
| 5668 | int ret; |
| 5669 | |
| 5670 | if (skb->len < len) |
| 5671 | return NULL; |
| 5672 | else if (skb->len == len) |
| 5673 | return skb; |
| 5674 | |
| 5675 | skb_chk = skb_clone(skb, GFP_ATOMIC); |
| 5676 | if (!skb_chk) |
| 5677 | return NULL; |
| 5678 | |
| 5679 | ret = pskb_trim_rcsum(skb_chk, len); |
| 5680 | if (ret) { |
| 5681 | kfree_skb(skb_chk); |
| 5682 | return NULL; |
| 5683 | } |
| 5684 | |
| 5685 | return skb_chk; |
| 5686 | } |
| 5687 | |
| 5688 | /** |
| 5689 | * skb_checksum_trimmed - validate checksum of an skb |
| 5690 | * @skb: the skb to check |
| 5691 | * @transport_len: the data length beyond the network header |
| 5692 | * @skb_chkf: checksum function to use |
| 5693 | * |
| 5694 | * Applies the given checksum function skb_chkf to the provided skb. |
| 5695 | * Returns a checked and maybe trimmed skb. Returns NULL on error. |
| 5696 | * |
| 5697 | * If the skb has data beyond the given transport length, then a |
| 5698 | * trimmed & cloned skb is checked and returned. |
| 5699 | * |
| 5700 | * Caller needs to set the skb transport header and free any returned skb if it |
| 5701 | * differs from the provided skb. |
| 5702 | */ |
| 5703 | struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, |
| 5704 | unsigned int transport_len, |
| 5705 | __sum16(*skb_chkf)(struct sk_buff *skb)) |
| 5706 | { |
| 5707 | struct sk_buff *skb_chk; |
| 5708 | unsigned int offset = skb_transport_offset(skb); |
| 5709 | __sum16 ret; |
| 5710 | |
| 5711 | skb_chk = skb_checksum_maybe_trim(skb, transport_len); |
| 5712 | if (!skb_chk) |
| 5713 | goto err; |
| 5714 | |
| 5715 | if (!pskb_may_pull(skb_chk, offset)) |
| 5716 | goto err; |
| 5717 | |
| 5718 | skb_pull_rcsum(skb_chk, offset); |
| 5719 | ret = skb_chkf(skb_chk); |
| 5720 | skb_push_rcsum(skb_chk, offset); |
| 5721 | |
| 5722 | if (ret) |
| 5723 | goto err; |
| 5724 | |
| 5725 | return skb_chk; |
| 5726 | |
| 5727 | err: |
| 5728 | if (skb_chk && skb_chk != skb) |
| 5729 | kfree_skb(skb_chk); |
| 5730 | |
| 5731 | return NULL; |
| 5732 | |
| 5733 | } |
| 5734 | EXPORT_SYMBOL(skb_checksum_trimmed); |
| 5735 | |
| 5736 | void __skb_warn_lro_forwarding(const struct sk_buff *skb) |
| 5737 | { |
| 5738 | net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", |
| 5739 | skb->dev->name); |
| 5740 | } |
| 5741 | EXPORT_SYMBOL(__skb_warn_lro_forwarding); |
| 5742 | |
| 5743 | void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) |
| 5744 | { |
| 5745 | if (head_stolen) { |
| 5746 | skb_release_head_state(skb); |
| 5747 | kmem_cache_free(skbuff_cache, skb); |
| 5748 | } else { |
| 5749 | __kfree_skb(skb); |
| 5750 | } |
| 5751 | } |
| 5752 | EXPORT_SYMBOL(kfree_skb_partial); |
| 5753 | |
| 5754 | /** |
| 5755 | * skb_try_coalesce - try to merge skb to prior one |
| 5756 | * @to: prior buffer |
| 5757 | * @from: buffer to add |
| 5758 | * @fragstolen: pointer to boolean |
| 5759 | * @delta_truesize: how much more was allocated than was requested |
| 5760 | */ |
| 5761 | bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, |
| 5762 | bool *fragstolen, int *delta_truesize) |
| 5763 | { |
| 5764 | struct skb_shared_info *to_shinfo, *from_shinfo; |
| 5765 | int i, delta, len = from->len; |
| 5766 | |
| 5767 | *fragstolen = false; |
| 5768 | |
| 5769 | if (skb_cloned(to)) |
| 5770 | return false; |
| 5771 | |
| 5772 | /* In general, avoid mixing page_pool and non-page_pool allocated |
| 5773 | * pages within the same SKB. Additionally avoid dealing with clones |
| 5774 | * with page_pool pages, in case the SKB is using page_pool fragment |
| 5775 | * references (page_pool_alloc_frag()). Since we only take full page |
| 5776 | * references for cloned SKBs at the moment that would result in |
| 5777 | * inconsistent reference counts. |
| 5778 | * In theory we could take full references if @from is cloned and |
| 5779 | * !@to->pp_recycle but its tricky (due to potential race with |
| 5780 | * the clone disappearing) and rare, so not worth dealing with. |
| 5781 | */ |
| 5782 | if (to->pp_recycle != from->pp_recycle || |
| 5783 | (from->pp_recycle && skb_cloned(from))) |
| 5784 | return false; |
| 5785 | |
| 5786 | if (len <= skb_tailroom(to)) { |
| 5787 | if (len) |
| 5788 | BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); |
| 5789 | *delta_truesize = 0; |
| 5790 | return true; |
| 5791 | } |
| 5792 | |
| 5793 | to_shinfo = skb_shinfo(to); |
| 5794 | from_shinfo = skb_shinfo(from); |
| 5795 | if (to_shinfo->frag_list || from_shinfo->frag_list) |
| 5796 | return false; |
| 5797 | if (skb_zcopy(to) || skb_zcopy(from)) |
| 5798 | return false; |
| 5799 | |
| 5800 | if (skb_headlen(from) != 0) { |
| 5801 | struct page *page; |
| 5802 | unsigned int offset; |
| 5803 | |
| 5804 | if (to_shinfo->nr_frags + |
| 5805 | from_shinfo->nr_frags >= MAX_SKB_FRAGS) |
| 5806 | return false; |
| 5807 | |
| 5808 | if (skb_head_is_locked(from)) |
| 5809 | return false; |
| 5810 | |
| 5811 | delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
| 5812 | |
| 5813 | page = virt_to_head_page(from->head); |
| 5814 | offset = from->data - (unsigned char *)page_address(page); |
| 5815 | |
| 5816 | skb_fill_page_desc(to, to_shinfo->nr_frags, |
| 5817 | page, offset, skb_headlen(from)); |
| 5818 | *fragstolen = true; |
| 5819 | } else { |
| 5820 | if (to_shinfo->nr_frags + |
| 5821 | from_shinfo->nr_frags > MAX_SKB_FRAGS) |
| 5822 | return false; |
| 5823 | |
| 5824 | delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); |
| 5825 | } |
| 5826 | |
| 5827 | WARN_ON_ONCE(delta < len); |
| 5828 | |
| 5829 | memcpy(to_shinfo->frags + to_shinfo->nr_frags, |
| 5830 | from_shinfo->frags, |
| 5831 | from_shinfo->nr_frags * sizeof(skb_frag_t)); |
| 5832 | to_shinfo->nr_frags += from_shinfo->nr_frags; |
| 5833 | |
| 5834 | if (!skb_cloned(from)) |
| 5835 | from_shinfo->nr_frags = 0; |
| 5836 | |
| 5837 | /* if the skb is not cloned this does nothing |
| 5838 | * since we set nr_frags to 0. |
| 5839 | */ |
| 5840 | for (i = 0; i < from_shinfo->nr_frags; i++) |
| 5841 | __skb_frag_ref(&from_shinfo->frags[i]); |
| 5842 | |
| 5843 | to->truesize += delta; |
| 5844 | to->len += len; |
| 5845 | to->data_len += len; |
| 5846 | |
| 5847 | *delta_truesize = delta; |
| 5848 | return true; |
| 5849 | } |
| 5850 | EXPORT_SYMBOL(skb_try_coalesce); |
| 5851 | |
| 5852 | /** |
| 5853 | * skb_scrub_packet - scrub an skb |
| 5854 | * |
| 5855 | * @skb: buffer to clean |
| 5856 | * @xnet: packet is crossing netns |
| 5857 | * |
| 5858 | * skb_scrub_packet can be used after encapsulating or decapsulting a packet |
| 5859 | * into/from a tunnel. Some information have to be cleared during these |
| 5860 | * operations. |
| 5861 | * skb_scrub_packet can also be used to clean a skb before injecting it in |
| 5862 | * another namespace (@xnet == true). We have to clear all information in the |
| 5863 | * skb that could impact namespace isolation. |
| 5864 | */ |
| 5865 | void skb_scrub_packet(struct sk_buff *skb, bool xnet) |
| 5866 | { |
| 5867 | skb->pkt_type = PACKET_HOST; |
| 5868 | skb->skb_iif = 0; |
| 5869 | skb->ignore_df = 0; |
| 5870 | skb_dst_drop(skb); |
| 5871 | skb_ext_reset(skb); |
| 5872 | nf_reset_ct(skb); |
| 5873 | nf_reset_trace(skb); |
| 5874 | |
| 5875 | #ifdef CONFIG_NET_SWITCHDEV |
| 5876 | skb->offload_fwd_mark = 0; |
| 5877 | skb->offload_l3_fwd_mark = 0; |
| 5878 | #endif |
| 5879 | |
| 5880 | if (!xnet) |
| 5881 | return; |
| 5882 | |
| 5883 | ipvs_reset(skb); |
| 5884 | skb->mark = 0; |
| 5885 | skb_clear_tstamp(skb); |
| 5886 | } |
| 5887 | EXPORT_SYMBOL_GPL(skb_scrub_packet); |
| 5888 | |
| 5889 | static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) |
| 5890 | { |
| 5891 | int mac_len, meta_len; |
| 5892 | void *meta; |
| 5893 | |
| 5894 | if (skb_cow(skb, skb_headroom(skb)) < 0) { |
| 5895 | kfree_skb(skb); |
| 5896 | return NULL; |
| 5897 | } |
| 5898 | |
| 5899 | mac_len = skb->data - skb_mac_header(skb); |
| 5900 | if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { |
| 5901 | memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), |
| 5902 | mac_len - VLAN_HLEN - ETH_TLEN); |
| 5903 | } |
| 5904 | |
| 5905 | meta_len = skb_metadata_len(skb); |
| 5906 | if (meta_len) { |
| 5907 | meta = skb_metadata_end(skb) - meta_len; |
| 5908 | memmove(meta + VLAN_HLEN, meta, meta_len); |
| 5909 | } |
| 5910 | |
| 5911 | skb->mac_header += VLAN_HLEN; |
| 5912 | return skb; |
| 5913 | } |
| 5914 | |
| 5915 | struct sk_buff *skb_vlan_untag(struct sk_buff *skb) |
| 5916 | { |
| 5917 | struct vlan_hdr *vhdr; |
| 5918 | u16 vlan_tci; |
| 5919 | |
| 5920 | if (unlikely(skb_vlan_tag_present(skb))) { |
| 5921 | /* vlan_tci is already set-up so leave this for another time */ |
| 5922 | return skb; |
| 5923 | } |
| 5924 | |
| 5925 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 5926 | if (unlikely(!skb)) |
| 5927 | goto err_free; |
| 5928 | /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ |
| 5929 | if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) |
| 5930 | goto err_free; |
| 5931 | |
| 5932 | vhdr = (struct vlan_hdr *)skb->data; |
| 5933 | vlan_tci = ntohs(vhdr->h_vlan_TCI); |
| 5934 | __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); |
| 5935 | |
| 5936 | skb_pull_rcsum(skb, VLAN_HLEN); |
| 5937 | vlan_set_encap_proto(skb, vhdr); |
| 5938 | |
| 5939 | skb = skb_reorder_vlan_header(skb); |
| 5940 | if (unlikely(!skb)) |
| 5941 | goto err_free; |
| 5942 | |
| 5943 | skb_reset_network_header(skb); |
| 5944 | if (!skb_transport_header_was_set(skb)) |
| 5945 | skb_reset_transport_header(skb); |
| 5946 | skb_reset_mac_len(skb); |
| 5947 | |
| 5948 | return skb; |
| 5949 | |
| 5950 | err_free: |
| 5951 | kfree_skb(skb); |
| 5952 | return NULL; |
| 5953 | } |
| 5954 | EXPORT_SYMBOL(skb_vlan_untag); |
| 5955 | |
| 5956 | int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len) |
| 5957 | { |
| 5958 | if (!pskb_may_pull(skb, write_len)) |
| 5959 | return -ENOMEM; |
| 5960 | |
| 5961 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
| 5962 | return 0; |
| 5963 | |
| 5964 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 5965 | } |
| 5966 | EXPORT_SYMBOL(skb_ensure_writable); |
| 5967 | |
| 5968 | /* remove VLAN header from packet and update csum accordingly. |
| 5969 | * expects a non skb_vlan_tag_present skb with a vlan tag payload |
| 5970 | */ |
| 5971 | int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) |
| 5972 | { |
| 5973 | int offset = skb->data - skb_mac_header(skb); |
| 5974 | int err; |
| 5975 | |
| 5976 | if (WARN_ONCE(offset, |
| 5977 | "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", |
| 5978 | offset)) { |
| 5979 | return -EINVAL; |
| 5980 | } |
| 5981 | |
| 5982 | err = skb_ensure_writable(skb, VLAN_ETH_HLEN); |
| 5983 | if (unlikely(err)) |
| 5984 | return err; |
| 5985 | |
| 5986 | skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); |
| 5987 | |
| 5988 | vlan_remove_tag(skb, vlan_tci); |
| 5989 | |
| 5990 | skb->mac_header += VLAN_HLEN; |
| 5991 | |
| 5992 | if (skb_network_offset(skb) < ETH_HLEN) |
| 5993 | skb_set_network_header(skb, ETH_HLEN); |
| 5994 | |
| 5995 | skb_reset_mac_len(skb); |
| 5996 | |
| 5997 | return err; |
| 5998 | } |
| 5999 | EXPORT_SYMBOL(__skb_vlan_pop); |
| 6000 | |
| 6001 | /* Pop a vlan tag either from hwaccel or from payload. |
| 6002 | * Expects skb->data at mac header. |
| 6003 | */ |
| 6004 | int skb_vlan_pop(struct sk_buff *skb) |
| 6005 | { |
| 6006 | u16 vlan_tci; |
| 6007 | __be16 vlan_proto; |
| 6008 | int err; |
| 6009 | |
| 6010 | if (likely(skb_vlan_tag_present(skb))) { |
| 6011 | __vlan_hwaccel_clear_tag(skb); |
| 6012 | } else { |
| 6013 | if (unlikely(!eth_type_vlan(skb->protocol))) |
| 6014 | return 0; |
| 6015 | |
| 6016 | err = __skb_vlan_pop(skb, &vlan_tci); |
| 6017 | if (err) |
| 6018 | return err; |
| 6019 | } |
| 6020 | /* move next vlan tag to hw accel tag */ |
| 6021 | if (likely(!eth_type_vlan(skb->protocol))) |
| 6022 | return 0; |
| 6023 | |
| 6024 | vlan_proto = skb->protocol; |
| 6025 | err = __skb_vlan_pop(skb, &vlan_tci); |
| 6026 | if (unlikely(err)) |
| 6027 | return err; |
| 6028 | |
| 6029 | __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); |
| 6030 | return 0; |
| 6031 | } |
| 6032 | EXPORT_SYMBOL(skb_vlan_pop); |
| 6033 | |
| 6034 | /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). |
| 6035 | * Expects skb->data at mac header. |
| 6036 | */ |
| 6037 | int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) |
| 6038 | { |
| 6039 | if (skb_vlan_tag_present(skb)) { |
| 6040 | int offset = skb->data - skb_mac_header(skb); |
| 6041 | int err; |
| 6042 | |
| 6043 | if (WARN_ONCE(offset, |
| 6044 | "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", |
| 6045 | offset)) { |
| 6046 | return -EINVAL; |
| 6047 | } |
| 6048 | |
| 6049 | err = __vlan_insert_tag(skb, skb->vlan_proto, |
| 6050 | skb_vlan_tag_get(skb)); |
| 6051 | if (err) |
| 6052 | return err; |
| 6053 | |
| 6054 | skb->protocol = skb->vlan_proto; |
| 6055 | skb->mac_len += VLAN_HLEN; |
| 6056 | |
| 6057 | skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); |
| 6058 | } |
| 6059 | __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); |
| 6060 | return 0; |
| 6061 | } |
| 6062 | EXPORT_SYMBOL(skb_vlan_push); |
| 6063 | |
| 6064 | /** |
| 6065 | * skb_eth_pop() - Drop the Ethernet header at the head of a packet |
| 6066 | * |
| 6067 | * @skb: Socket buffer to modify |
| 6068 | * |
| 6069 | * Drop the Ethernet header of @skb. |
| 6070 | * |
| 6071 | * Expects that skb->data points to the mac header and that no VLAN tags are |
| 6072 | * present. |
| 6073 | * |
| 6074 | * Returns 0 on success, -errno otherwise. |
| 6075 | */ |
| 6076 | int skb_eth_pop(struct sk_buff *skb) |
| 6077 | { |
| 6078 | if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || |
| 6079 | skb_network_offset(skb) < ETH_HLEN) |
| 6080 | return -EPROTO; |
| 6081 | |
| 6082 | skb_pull_rcsum(skb, ETH_HLEN); |
| 6083 | skb_reset_mac_header(skb); |
| 6084 | skb_reset_mac_len(skb); |
| 6085 | |
| 6086 | return 0; |
| 6087 | } |
| 6088 | EXPORT_SYMBOL(skb_eth_pop); |
| 6089 | |
| 6090 | /** |
| 6091 | * skb_eth_push() - Add a new Ethernet header at the head of a packet |
| 6092 | * |
| 6093 | * @skb: Socket buffer to modify |
| 6094 | * @dst: Destination MAC address of the new header |
| 6095 | * @src: Source MAC address of the new header |
| 6096 | * |
| 6097 | * Prepend @skb with a new Ethernet header. |
| 6098 | * |
| 6099 | * Expects that skb->data points to the mac header, which must be empty. |
| 6100 | * |
| 6101 | * Returns 0 on success, -errno otherwise. |
| 6102 | */ |
| 6103 | int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, |
| 6104 | const unsigned char *src) |
| 6105 | { |
| 6106 | struct ethhdr *eth; |
| 6107 | int err; |
| 6108 | |
| 6109 | if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) |
| 6110 | return -EPROTO; |
| 6111 | |
| 6112 | err = skb_cow_head(skb, sizeof(*eth)); |
| 6113 | if (err < 0) |
| 6114 | return err; |
| 6115 | |
| 6116 | skb_push(skb, sizeof(*eth)); |
| 6117 | skb_reset_mac_header(skb); |
| 6118 | skb_reset_mac_len(skb); |
| 6119 | |
| 6120 | eth = eth_hdr(skb); |
| 6121 | ether_addr_copy(eth->h_dest, dst); |
| 6122 | ether_addr_copy(eth->h_source, src); |
| 6123 | eth->h_proto = skb->protocol; |
| 6124 | |
| 6125 | skb_postpush_rcsum(skb, eth, sizeof(*eth)); |
| 6126 | |
| 6127 | return 0; |
| 6128 | } |
| 6129 | EXPORT_SYMBOL(skb_eth_push); |
| 6130 | |
| 6131 | /* Update the ethertype of hdr and the skb csum value if required. */ |
| 6132 | static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, |
| 6133 | __be16 ethertype) |
| 6134 | { |
| 6135 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 6136 | __be16 diff[] = { ~hdr->h_proto, ethertype }; |
| 6137 | |
| 6138 | skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); |
| 6139 | } |
| 6140 | |
| 6141 | hdr->h_proto = ethertype; |
| 6142 | } |
| 6143 | |
| 6144 | /** |
| 6145 | * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of |
| 6146 | * the packet |
| 6147 | * |
| 6148 | * @skb: buffer |
| 6149 | * @mpls_lse: MPLS label stack entry to push |
| 6150 | * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) |
| 6151 | * @mac_len: length of the MAC header |
| 6152 | * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is |
| 6153 | * ethernet |
| 6154 | * |
| 6155 | * Expects skb->data at mac header. |
| 6156 | * |
| 6157 | * Returns 0 on success, -errno otherwise. |
| 6158 | */ |
| 6159 | int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, |
| 6160 | int mac_len, bool ethernet) |
| 6161 | { |
| 6162 | struct mpls_shim_hdr *lse; |
| 6163 | int err; |
| 6164 | |
| 6165 | if (unlikely(!eth_p_mpls(mpls_proto))) |
| 6166 | return -EINVAL; |
| 6167 | |
| 6168 | /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ |
| 6169 | if (skb->encapsulation) |
| 6170 | return -EINVAL; |
| 6171 | |
| 6172 | err = skb_cow_head(skb, MPLS_HLEN); |
| 6173 | if (unlikely(err)) |
| 6174 | return err; |
| 6175 | |
| 6176 | if (!skb->inner_protocol) { |
| 6177 | skb_set_inner_network_header(skb, skb_network_offset(skb)); |
| 6178 | skb_set_inner_protocol(skb, skb->protocol); |
| 6179 | } |
| 6180 | |
| 6181 | skb_push(skb, MPLS_HLEN); |
| 6182 | memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), |
| 6183 | mac_len); |
| 6184 | skb_reset_mac_header(skb); |
| 6185 | skb_set_network_header(skb, mac_len); |
| 6186 | skb_reset_mac_len(skb); |
| 6187 | |
| 6188 | lse = mpls_hdr(skb); |
| 6189 | lse->label_stack_entry = mpls_lse; |
| 6190 | skb_postpush_rcsum(skb, lse, MPLS_HLEN); |
| 6191 | |
| 6192 | if (ethernet && mac_len >= ETH_HLEN) |
| 6193 | skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); |
| 6194 | skb->protocol = mpls_proto; |
| 6195 | |
| 6196 | return 0; |
| 6197 | } |
| 6198 | EXPORT_SYMBOL_GPL(skb_mpls_push); |
| 6199 | |
| 6200 | /** |
| 6201 | * skb_mpls_pop() - pop the outermost MPLS header |
| 6202 | * |
| 6203 | * @skb: buffer |
| 6204 | * @next_proto: ethertype of header after popped MPLS header |
| 6205 | * @mac_len: length of the MAC header |
| 6206 | * @ethernet: flag to indicate if the packet is ethernet |
| 6207 | * |
| 6208 | * Expects skb->data at mac header. |
| 6209 | * |
| 6210 | * Returns 0 on success, -errno otherwise. |
| 6211 | */ |
| 6212 | int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, |
| 6213 | bool ethernet) |
| 6214 | { |
| 6215 | int err; |
| 6216 | |
| 6217 | if (unlikely(!eth_p_mpls(skb->protocol))) |
| 6218 | return 0; |
| 6219 | |
| 6220 | err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); |
| 6221 | if (unlikely(err)) |
| 6222 | return err; |
| 6223 | |
| 6224 | skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); |
| 6225 | memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), |
| 6226 | mac_len); |
| 6227 | |
| 6228 | __skb_pull(skb, MPLS_HLEN); |
| 6229 | skb_reset_mac_header(skb); |
| 6230 | skb_set_network_header(skb, mac_len); |
| 6231 | |
| 6232 | if (ethernet && mac_len >= ETH_HLEN) { |
| 6233 | struct ethhdr *hdr; |
| 6234 | |
| 6235 | /* use mpls_hdr() to get ethertype to account for VLANs. */ |
| 6236 | hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); |
| 6237 | skb_mod_eth_type(skb, hdr, next_proto); |
| 6238 | } |
| 6239 | skb->protocol = next_proto; |
| 6240 | |
| 6241 | return 0; |
| 6242 | } |
| 6243 | EXPORT_SYMBOL_GPL(skb_mpls_pop); |
| 6244 | |
| 6245 | /** |
| 6246 | * skb_mpls_update_lse() - modify outermost MPLS header and update csum |
| 6247 | * |
| 6248 | * @skb: buffer |
| 6249 | * @mpls_lse: new MPLS label stack entry to update to |
| 6250 | * |
| 6251 | * Expects skb->data at mac header. |
| 6252 | * |
| 6253 | * Returns 0 on success, -errno otherwise. |
| 6254 | */ |
| 6255 | int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) |
| 6256 | { |
| 6257 | int err; |
| 6258 | |
| 6259 | if (unlikely(!eth_p_mpls(skb->protocol))) |
| 6260 | return -EINVAL; |
| 6261 | |
| 6262 | err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); |
| 6263 | if (unlikely(err)) |
| 6264 | return err; |
| 6265 | |
| 6266 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 6267 | __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; |
| 6268 | |
| 6269 | skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); |
| 6270 | } |
| 6271 | |
| 6272 | mpls_hdr(skb)->label_stack_entry = mpls_lse; |
| 6273 | |
| 6274 | return 0; |
| 6275 | } |
| 6276 | EXPORT_SYMBOL_GPL(skb_mpls_update_lse); |
| 6277 | |
| 6278 | /** |
| 6279 | * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header |
| 6280 | * |
| 6281 | * @skb: buffer |
| 6282 | * |
| 6283 | * Expects skb->data at mac header. |
| 6284 | * |
| 6285 | * Returns 0 on success, -errno otherwise. |
| 6286 | */ |
| 6287 | int skb_mpls_dec_ttl(struct sk_buff *skb) |
| 6288 | { |
| 6289 | u32 lse; |
| 6290 | u8 ttl; |
| 6291 | |
| 6292 | if (unlikely(!eth_p_mpls(skb->protocol))) |
| 6293 | return -EINVAL; |
| 6294 | |
| 6295 | if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) |
| 6296 | return -ENOMEM; |
| 6297 | |
| 6298 | lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); |
| 6299 | ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; |
| 6300 | if (!--ttl) |
| 6301 | return -EINVAL; |
| 6302 | |
| 6303 | lse &= ~MPLS_LS_TTL_MASK; |
| 6304 | lse |= ttl << MPLS_LS_TTL_SHIFT; |
| 6305 | |
| 6306 | return skb_mpls_update_lse(skb, cpu_to_be32(lse)); |
| 6307 | } |
| 6308 | EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); |
| 6309 | |
| 6310 | /** |
| 6311 | * alloc_skb_with_frags - allocate skb with page frags |
| 6312 | * |
| 6313 | * @header_len: size of linear part |
| 6314 | * @data_len: needed length in frags |
| 6315 | * @order: max page order desired. |
| 6316 | * @errcode: pointer to error code if any |
| 6317 | * @gfp_mask: allocation mask |
| 6318 | * |
| 6319 | * This can be used to allocate a paged skb, given a maximal order for frags. |
| 6320 | */ |
| 6321 | struct sk_buff *alloc_skb_with_frags(unsigned long header_len, |
| 6322 | unsigned long data_len, |
| 6323 | int order, |
| 6324 | int *errcode, |
| 6325 | gfp_t gfp_mask) |
| 6326 | { |
| 6327 | unsigned long chunk; |
| 6328 | struct sk_buff *skb; |
| 6329 | struct page *page; |
| 6330 | int nr_frags = 0; |
| 6331 | |
| 6332 | *errcode = -EMSGSIZE; |
| 6333 | if (unlikely(data_len > MAX_SKB_FRAGS * (PAGE_SIZE << order))) |
| 6334 | return NULL; |
| 6335 | |
| 6336 | *errcode = -ENOBUFS; |
| 6337 | skb = alloc_skb(header_len, gfp_mask); |
| 6338 | if (!skb) |
| 6339 | return NULL; |
| 6340 | |
| 6341 | while (data_len) { |
| 6342 | if (nr_frags == MAX_SKB_FRAGS - 1) |
| 6343 | goto failure; |
| 6344 | while (order && PAGE_ALIGN(data_len) < (PAGE_SIZE << order)) |
| 6345 | order--; |
| 6346 | |
| 6347 | if (order) { |
| 6348 | page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | |
| 6349 | __GFP_COMP | |
| 6350 | __GFP_NOWARN, |
| 6351 | order); |
| 6352 | if (!page) { |
| 6353 | order--; |
| 6354 | continue; |
| 6355 | } |
| 6356 | } else { |
| 6357 | page = alloc_page(gfp_mask); |
| 6358 | if (!page) |
| 6359 | goto failure; |
| 6360 | } |
| 6361 | chunk = min_t(unsigned long, data_len, |
| 6362 | PAGE_SIZE << order); |
| 6363 | skb_fill_page_desc(skb, nr_frags, page, 0, chunk); |
| 6364 | nr_frags++; |
| 6365 | skb->truesize += (PAGE_SIZE << order); |
| 6366 | data_len -= chunk; |
| 6367 | } |
| 6368 | return skb; |
| 6369 | |
| 6370 | failure: |
| 6371 | kfree_skb(skb); |
| 6372 | return NULL; |
| 6373 | } |
| 6374 | EXPORT_SYMBOL(alloc_skb_with_frags); |
| 6375 | |
| 6376 | /* carve out the first off bytes from skb when off < headlen */ |
| 6377 | static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, |
| 6378 | const int headlen, gfp_t gfp_mask) |
| 6379 | { |
| 6380 | int i; |
| 6381 | unsigned int size = skb_end_offset(skb); |
| 6382 | int new_hlen = headlen - off; |
| 6383 | u8 *data; |
| 6384 | |
| 6385 | if (skb_pfmemalloc(skb)) |
| 6386 | gfp_mask |= __GFP_MEMALLOC; |
| 6387 | |
| 6388 | data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); |
| 6389 | if (!data) |
| 6390 | return -ENOMEM; |
| 6391 | size = SKB_WITH_OVERHEAD(size); |
| 6392 | |
| 6393 | /* Copy real data, and all frags */ |
| 6394 | skb_copy_from_linear_data_offset(skb, off, data, new_hlen); |
| 6395 | skb->len -= off; |
| 6396 | |
| 6397 | memcpy((struct skb_shared_info *)(data + size), |
| 6398 | skb_shinfo(skb), |
| 6399 | offsetof(struct skb_shared_info, |
| 6400 | frags[skb_shinfo(skb)->nr_frags])); |
| 6401 | if (skb_cloned(skb)) { |
| 6402 | /* drop the old head gracefully */ |
| 6403 | if (skb_orphan_frags(skb, gfp_mask)) { |
| 6404 | skb_kfree_head(data, size); |
| 6405 | return -ENOMEM; |
| 6406 | } |
| 6407 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 6408 | skb_frag_ref(skb, i); |
| 6409 | if (skb_has_frag_list(skb)) |
| 6410 | skb_clone_fraglist(skb); |
| 6411 | skb_release_data(skb, SKB_CONSUMED, false); |
| 6412 | } else { |
| 6413 | /* we can reuse existing recount- all we did was |
| 6414 | * relocate values |
| 6415 | */ |
| 6416 | skb_free_head(skb, false); |
| 6417 | } |
| 6418 | |
| 6419 | skb->head = data; |
| 6420 | skb->data = data; |
| 6421 | skb->head_frag = 0; |
| 6422 | skb_set_end_offset(skb, size); |
| 6423 | skb_set_tail_pointer(skb, skb_headlen(skb)); |
| 6424 | skb_headers_offset_update(skb, 0); |
| 6425 | skb->cloned = 0; |
| 6426 | skb->hdr_len = 0; |
| 6427 | skb->nohdr = 0; |
| 6428 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 6429 | |
| 6430 | return 0; |
| 6431 | } |
| 6432 | |
| 6433 | static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); |
| 6434 | |
| 6435 | /* carve out the first eat bytes from skb's frag_list. May recurse into |
| 6436 | * pskb_carve() |
| 6437 | */ |
| 6438 | static int pskb_carve_frag_list(struct sk_buff *skb, |
| 6439 | struct skb_shared_info *shinfo, int eat, |
| 6440 | gfp_t gfp_mask) |
| 6441 | { |
| 6442 | struct sk_buff *list = shinfo->frag_list; |
| 6443 | struct sk_buff *clone = NULL; |
| 6444 | struct sk_buff *insp = NULL; |
| 6445 | |
| 6446 | do { |
| 6447 | if (!list) { |
| 6448 | pr_err("Not enough bytes to eat. Want %d\n", eat); |
| 6449 | return -EFAULT; |
| 6450 | } |
| 6451 | if (list->len <= eat) { |
| 6452 | /* Eaten as whole. */ |
| 6453 | eat -= list->len; |
| 6454 | list = list->next; |
| 6455 | insp = list; |
| 6456 | } else { |
| 6457 | /* Eaten partially. */ |
| 6458 | if (skb_shared(list)) { |
| 6459 | clone = skb_clone(list, gfp_mask); |
| 6460 | if (!clone) |
| 6461 | return -ENOMEM; |
| 6462 | insp = list->next; |
| 6463 | list = clone; |
| 6464 | } else { |
| 6465 | /* This may be pulled without problems. */ |
| 6466 | insp = list; |
| 6467 | } |
| 6468 | if (pskb_carve(list, eat, gfp_mask) < 0) { |
| 6469 | kfree_skb(clone); |
| 6470 | return -ENOMEM; |
| 6471 | } |
| 6472 | break; |
| 6473 | } |
| 6474 | } while (eat); |
| 6475 | |
| 6476 | /* Free pulled out fragments. */ |
| 6477 | while ((list = shinfo->frag_list) != insp) { |
| 6478 | shinfo->frag_list = list->next; |
| 6479 | consume_skb(list); |
| 6480 | } |
| 6481 | /* And insert new clone at head. */ |
| 6482 | if (clone) { |
| 6483 | clone->next = list; |
| 6484 | shinfo->frag_list = clone; |
| 6485 | } |
| 6486 | return 0; |
| 6487 | } |
| 6488 | |
| 6489 | /* carve off first len bytes from skb. Split line (off) is in the |
| 6490 | * non-linear part of skb |
| 6491 | */ |
| 6492 | static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, |
| 6493 | int pos, gfp_t gfp_mask) |
| 6494 | { |
| 6495 | int i, k = 0; |
| 6496 | unsigned int size = skb_end_offset(skb); |
| 6497 | u8 *data; |
| 6498 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 6499 | struct skb_shared_info *shinfo; |
| 6500 | |
| 6501 | if (skb_pfmemalloc(skb)) |
| 6502 | gfp_mask |= __GFP_MEMALLOC; |
| 6503 | |
| 6504 | data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); |
| 6505 | if (!data) |
| 6506 | return -ENOMEM; |
| 6507 | size = SKB_WITH_OVERHEAD(size); |
| 6508 | |
| 6509 | memcpy((struct skb_shared_info *)(data + size), |
| 6510 | skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); |
| 6511 | if (skb_orphan_frags(skb, gfp_mask)) { |
| 6512 | skb_kfree_head(data, size); |
| 6513 | return -ENOMEM; |
| 6514 | } |
| 6515 | shinfo = (struct skb_shared_info *)(data + size); |
| 6516 | for (i = 0; i < nfrags; i++) { |
| 6517 | int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 6518 | |
| 6519 | if (pos + fsize > off) { |
| 6520 | shinfo->frags[k] = skb_shinfo(skb)->frags[i]; |
| 6521 | |
| 6522 | if (pos < off) { |
| 6523 | /* Split frag. |
| 6524 | * We have two variants in this case: |
| 6525 | * 1. Move all the frag to the second |
| 6526 | * part, if it is possible. F.e. |
| 6527 | * this approach is mandatory for TUX, |
| 6528 | * where splitting is expensive. |
| 6529 | * 2. Split is accurately. We make this. |
| 6530 | */ |
| 6531 | skb_frag_off_add(&shinfo->frags[0], off - pos); |
| 6532 | skb_frag_size_sub(&shinfo->frags[0], off - pos); |
| 6533 | } |
| 6534 | skb_frag_ref(skb, i); |
| 6535 | k++; |
| 6536 | } |
| 6537 | pos += fsize; |
| 6538 | } |
| 6539 | shinfo->nr_frags = k; |
| 6540 | if (skb_has_frag_list(skb)) |
| 6541 | skb_clone_fraglist(skb); |
| 6542 | |
| 6543 | /* split line is in frag list */ |
| 6544 | if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { |
| 6545 | /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ |
| 6546 | if (skb_has_frag_list(skb)) |
| 6547 | kfree_skb_list(skb_shinfo(skb)->frag_list); |
| 6548 | skb_kfree_head(data, size); |
| 6549 | return -ENOMEM; |
| 6550 | } |
| 6551 | skb_release_data(skb, SKB_CONSUMED, false); |
| 6552 | |
| 6553 | skb->head = data; |
| 6554 | skb->head_frag = 0; |
| 6555 | skb->data = data; |
| 6556 | skb_set_end_offset(skb, size); |
| 6557 | skb_reset_tail_pointer(skb); |
| 6558 | skb_headers_offset_update(skb, 0); |
| 6559 | skb->cloned = 0; |
| 6560 | skb->hdr_len = 0; |
| 6561 | skb->nohdr = 0; |
| 6562 | skb->len -= off; |
| 6563 | skb->data_len = skb->len; |
| 6564 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 6565 | return 0; |
| 6566 | } |
| 6567 | |
| 6568 | /* remove len bytes from the beginning of the skb */ |
| 6569 | static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) |
| 6570 | { |
| 6571 | int headlen = skb_headlen(skb); |
| 6572 | |
| 6573 | if (len < headlen) |
| 6574 | return pskb_carve_inside_header(skb, len, headlen, gfp); |
| 6575 | else |
| 6576 | return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); |
| 6577 | } |
| 6578 | |
| 6579 | /* Extract to_copy bytes starting at off from skb, and return this in |
| 6580 | * a new skb |
| 6581 | */ |
| 6582 | struct sk_buff *pskb_extract(struct sk_buff *skb, int off, |
| 6583 | int to_copy, gfp_t gfp) |
| 6584 | { |
| 6585 | struct sk_buff *clone = skb_clone(skb, gfp); |
| 6586 | |
| 6587 | if (!clone) |
| 6588 | return NULL; |
| 6589 | |
| 6590 | if (pskb_carve(clone, off, gfp) < 0 || |
| 6591 | pskb_trim(clone, to_copy)) { |
| 6592 | kfree_skb(clone); |
| 6593 | return NULL; |
| 6594 | } |
| 6595 | return clone; |
| 6596 | } |
| 6597 | EXPORT_SYMBOL(pskb_extract); |
| 6598 | |
| 6599 | /** |
| 6600 | * skb_condense - try to get rid of fragments/frag_list if possible |
| 6601 | * @skb: buffer |
| 6602 | * |
| 6603 | * Can be used to save memory before skb is added to a busy queue. |
| 6604 | * If packet has bytes in frags and enough tail room in skb->head, |
| 6605 | * pull all of them, so that we can free the frags right now and adjust |
| 6606 | * truesize. |
| 6607 | * Notes: |
| 6608 | * We do not reallocate skb->head thus can not fail. |
| 6609 | * Caller must re-evaluate skb->truesize if needed. |
| 6610 | */ |
| 6611 | void skb_condense(struct sk_buff *skb) |
| 6612 | { |
| 6613 | if (skb->data_len) { |
| 6614 | if (skb->data_len > skb->end - skb->tail || |
| 6615 | skb_cloned(skb)) |
| 6616 | return; |
| 6617 | |
| 6618 | /* Nice, we can free page frag(s) right now */ |
| 6619 | __pskb_pull_tail(skb, skb->data_len); |
| 6620 | } |
| 6621 | /* At this point, skb->truesize might be over estimated, |
| 6622 | * because skb had a fragment, and fragments do not tell |
| 6623 | * their truesize. |
| 6624 | * When we pulled its content into skb->head, fragment |
| 6625 | * was freed, but __pskb_pull_tail() could not possibly |
| 6626 | * adjust skb->truesize, not knowing the frag truesize. |
| 6627 | */ |
| 6628 | skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); |
| 6629 | } |
| 6630 | EXPORT_SYMBOL(skb_condense); |
| 6631 | |
| 6632 | #ifdef CONFIG_SKB_EXTENSIONS |
| 6633 | static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) |
| 6634 | { |
| 6635 | return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); |
| 6636 | } |
| 6637 | |
| 6638 | /** |
| 6639 | * __skb_ext_alloc - allocate a new skb extensions storage |
| 6640 | * |
| 6641 | * @flags: See kmalloc(). |
| 6642 | * |
| 6643 | * Returns the newly allocated pointer. The pointer can later attached to a |
| 6644 | * skb via __skb_ext_set(). |
| 6645 | * Note: caller must handle the skb_ext as an opaque data. |
| 6646 | */ |
| 6647 | struct skb_ext *__skb_ext_alloc(gfp_t flags) |
| 6648 | { |
| 6649 | struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); |
| 6650 | |
| 6651 | if (new) { |
| 6652 | memset(new->offset, 0, sizeof(new->offset)); |
| 6653 | refcount_set(&new->refcnt, 1); |
| 6654 | } |
| 6655 | |
| 6656 | return new; |
| 6657 | } |
| 6658 | |
| 6659 | static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, |
| 6660 | unsigned int old_active) |
| 6661 | { |
| 6662 | struct skb_ext *new; |
| 6663 | |
| 6664 | if (refcount_read(&old->refcnt) == 1) |
| 6665 | return old; |
| 6666 | |
| 6667 | new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); |
| 6668 | if (!new) |
| 6669 | return NULL; |
| 6670 | |
| 6671 | memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); |
| 6672 | refcount_set(&new->refcnt, 1); |
| 6673 | |
| 6674 | #ifdef CONFIG_XFRM |
| 6675 | if (old_active & (1 << SKB_EXT_SEC_PATH)) { |
| 6676 | struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); |
| 6677 | unsigned int i; |
| 6678 | |
| 6679 | for (i = 0; i < sp->len; i++) |
| 6680 | xfrm_state_hold(sp->xvec[i]); |
| 6681 | } |
| 6682 | #endif |
| 6683 | __skb_ext_put(old); |
| 6684 | return new; |
| 6685 | } |
| 6686 | |
| 6687 | /** |
| 6688 | * __skb_ext_set - attach the specified extension storage to this skb |
| 6689 | * @skb: buffer |
| 6690 | * @id: extension id |
| 6691 | * @ext: extension storage previously allocated via __skb_ext_alloc() |
| 6692 | * |
| 6693 | * Existing extensions, if any, are cleared. |
| 6694 | * |
| 6695 | * Returns the pointer to the extension. |
| 6696 | */ |
| 6697 | void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, |
| 6698 | struct skb_ext *ext) |
| 6699 | { |
| 6700 | unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); |
| 6701 | |
| 6702 | skb_ext_put(skb); |
| 6703 | newlen = newoff + skb_ext_type_len[id]; |
| 6704 | ext->chunks = newlen; |
| 6705 | ext->offset[id] = newoff; |
| 6706 | skb->extensions = ext; |
| 6707 | skb->active_extensions = 1 << id; |
| 6708 | return skb_ext_get_ptr(ext, id); |
| 6709 | } |
| 6710 | |
| 6711 | /** |
| 6712 | * skb_ext_add - allocate space for given extension, COW if needed |
| 6713 | * @skb: buffer |
| 6714 | * @id: extension to allocate space for |
| 6715 | * |
| 6716 | * Allocates enough space for the given extension. |
| 6717 | * If the extension is already present, a pointer to that extension |
| 6718 | * is returned. |
| 6719 | * |
| 6720 | * If the skb was cloned, COW applies and the returned memory can be |
| 6721 | * modified without changing the extension space of clones buffers. |
| 6722 | * |
| 6723 | * Returns pointer to the extension or NULL on allocation failure. |
| 6724 | */ |
| 6725 | void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) |
| 6726 | { |
| 6727 | struct skb_ext *new, *old = NULL; |
| 6728 | unsigned int newlen, newoff; |
| 6729 | |
| 6730 | if (skb->active_extensions) { |
| 6731 | old = skb->extensions; |
| 6732 | |
| 6733 | new = skb_ext_maybe_cow(old, skb->active_extensions); |
| 6734 | if (!new) |
| 6735 | return NULL; |
| 6736 | |
| 6737 | if (__skb_ext_exist(new, id)) |
| 6738 | goto set_active; |
| 6739 | |
| 6740 | newoff = new->chunks; |
| 6741 | } else { |
| 6742 | newoff = SKB_EXT_CHUNKSIZEOF(*new); |
| 6743 | |
| 6744 | new = __skb_ext_alloc(GFP_ATOMIC); |
| 6745 | if (!new) |
| 6746 | return NULL; |
| 6747 | } |
| 6748 | |
| 6749 | newlen = newoff + skb_ext_type_len[id]; |
| 6750 | new->chunks = newlen; |
| 6751 | new->offset[id] = newoff; |
| 6752 | set_active: |
| 6753 | skb->slow_gro = 1; |
| 6754 | skb->extensions = new; |
| 6755 | skb->active_extensions |= 1 << id; |
| 6756 | return skb_ext_get_ptr(new, id); |
| 6757 | } |
| 6758 | EXPORT_SYMBOL(skb_ext_add); |
| 6759 | |
| 6760 | #ifdef CONFIG_XFRM |
| 6761 | static void skb_ext_put_sp(struct sec_path *sp) |
| 6762 | { |
| 6763 | unsigned int i; |
| 6764 | |
| 6765 | for (i = 0; i < sp->len; i++) |
| 6766 | xfrm_state_put(sp->xvec[i]); |
| 6767 | } |
| 6768 | #endif |
| 6769 | |
| 6770 | #ifdef CONFIG_MCTP_FLOWS |
| 6771 | static void skb_ext_put_mctp(struct mctp_flow *flow) |
| 6772 | { |
| 6773 | if (flow->key) |
| 6774 | mctp_key_unref(flow->key); |
| 6775 | } |
| 6776 | #endif |
| 6777 | |
| 6778 | void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) |
| 6779 | { |
| 6780 | struct skb_ext *ext = skb->extensions; |
| 6781 | |
| 6782 | skb->active_extensions &= ~(1 << id); |
| 6783 | if (skb->active_extensions == 0) { |
| 6784 | skb->extensions = NULL; |
| 6785 | __skb_ext_put(ext); |
| 6786 | #ifdef CONFIG_XFRM |
| 6787 | } else if (id == SKB_EXT_SEC_PATH && |
| 6788 | refcount_read(&ext->refcnt) == 1) { |
| 6789 | struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); |
| 6790 | |
| 6791 | skb_ext_put_sp(sp); |
| 6792 | sp->len = 0; |
| 6793 | #endif |
| 6794 | } |
| 6795 | } |
| 6796 | EXPORT_SYMBOL(__skb_ext_del); |
| 6797 | |
| 6798 | void __skb_ext_put(struct skb_ext *ext) |
| 6799 | { |
| 6800 | /* If this is last clone, nothing can increment |
| 6801 | * it after check passes. Avoids one atomic op. |
| 6802 | */ |
| 6803 | if (refcount_read(&ext->refcnt) == 1) |
| 6804 | goto free_now; |
| 6805 | |
| 6806 | if (!refcount_dec_and_test(&ext->refcnt)) |
| 6807 | return; |
| 6808 | free_now: |
| 6809 | #ifdef CONFIG_XFRM |
| 6810 | if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) |
| 6811 | skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); |
| 6812 | #endif |
| 6813 | #ifdef CONFIG_MCTP_FLOWS |
| 6814 | if (__skb_ext_exist(ext, SKB_EXT_MCTP)) |
| 6815 | skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); |
| 6816 | #endif |
| 6817 | |
| 6818 | kmem_cache_free(skbuff_ext_cache, ext); |
| 6819 | } |
| 6820 | EXPORT_SYMBOL(__skb_ext_put); |
| 6821 | #endif /* CONFIG_SKB_EXTENSIONS */ |
| 6822 | |
| 6823 | /** |
| 6824 | * skb_attempt_defer_free - queue skb for remote freeing |
| 6825 | * @skb: buffer |
| 6826 | * |
| 6827 | * Put @skb in a per-cpu list, using the cpu which |
| 6828 | * allocated the skb/pages to reduce false sharing |
| 6829 | * and memory zone spinlock contention. |
| 6830 | */ |
| 6831 | void skb_attempt_defer_free(struct sk_buff *skb) |
| 6832 | { |
| 6833 | int cpu = skb->alloc_cpu; |
| 6834 | struct softnet_data *sd; |
| 6835 | unsigned int defer_max; |
| 6836 | bool kick; |
| 6837 | |
| 6838 | if (WARN_ON_ONCE(cpu >= nr_cpu_ids) || |
| 6839 | !cpu_online(cpu) || |
| 6840 | cpu == raw_smp_processor_id()) { |
| 6841 | nodefer: __kfree_skb(skb); |
| 6842 | return; |
| 6843 | } |
| 6844 | |
| 6845 | DEBUG_NET_WARN_ON_ONCE(skb_dst(skb)); |
| 6846 | DEBUG_NET_WARN_ON_ONCE(skb->destructor); |
| 6847 | |
| 6848 | sd = &per_cpu(softnet_data, cpu); |
| 6849 | defer_max = READ_ONCE(sysctl_skb_defer_max); |
| 6850 | if (READ_ONCE(sd->defer_count) >= defer_max) |
| 6851 | goto nodefer; |
| 6852 | |
| 6853 | spin_lock_bh(&sd->defer_lock); |
| 6854 | /* Send an IPI every time queue reaches half capacity. */ |
| 6855 | kick = sd->defer_count == (defer_max >> 1); |
| 6856 | /* Paired with the READ_ONCE() few lines above */ |
| 6857 | WRITE_ONCE(sd->defer_count, sd->defer_count + 1); |
| 6858 | |
| 6859 | skb->next = sd->defer_list; |
| 6860 | /* Paired with READ_ONCE() in skb_defer_free_flush() */ |
| 6861 | WRITE_ONCE(sd->defer_list, skb); |
| 6862 | spin_unlock_bh(&sd->defer_lock); |
| 6863 | |
| 6864 | /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU |
| 6865 | * if we are unlucky enough (this seems very unlikely). |
| 6866 | */ |
| 6867 | if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) |
| 6868 | smp_call_function_single_async(cpu, &sd->defer_csd); |
| 6869 | } |
| 6870 | |
| 6871 | static void skb_splice_csum_page(struct sk_buff *skb, struct page *page, |
| 6872 | size_t offset, size_t len) |
| 6873 | { |
| 6874 | const char *kaddr; |
| 6875 | __wsum csum; |
| 6876 | |
| 6877 | kaddr = kmap_local_page(page); |
| 6878 | csum = csum_partial(kaddr + offset, len, 0); |
| 6879 | kunmap_local(kaddr); |
| 6880 | skb->csum = csum_block_add(skb->csum, csum, skb->len); |
| 6881 | } |
| 6882 | |
| 6883 | /** |
| 6884 | * skb_splice_from_iter - Splice (or copy) pages to skbuff |
| 6885 | * @skb: The buffer to add pages to |
| 6886 | * @iter: Iterator representing the pages to be added |
| 6887 | * @maxsize: Maximum amount of pages to be added |
| 6888 | * @gfp: Allocation flags |
| 6889 | * |
| 6890 | * This is a common helper function for supporting MSG_SPLICE_PAGES. It |
| 6891 | * extracts pages from an iterator and adds them to the socket buffer if |
| 6892 | * possible, copying them to fragments if not possible (such as if they're slab |
| 6893 | * pages). |
| 6894 | * |
| 6895 | * Returns the amount of data spliced/copied or -EMSGSIZE if there's |
| 6896 | * insufficient space in the buffer to transfer anything. |
| 6897 | */ |
| 6898 | ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter, |
| 6899 | ssize_t maxsize, gfp_t gfp) |
| 6900 | { |
| 6901 | size_t frag_limit = READ_ONCE(sysctl_max_skb_frags); |
| 6902 | struct page *pages[8], **ppages = pages; |
| 6903 | ssize_t spliced = 0, ret = 0; |
| 6904 | unsigned int i; |
| 6905 | |
| 6906 | while (iter->count > 0) { |
| 6907 | ssize_t space, nr, len; |
| 6908 | size_t off; |
| 6909 | |
| 6910 | ret = -EMSGSIZE; |
| 6911 | space = frag_limit - skb_shinfo(skb)->nr_frags; |
| 6912 | if (space < 0) |
| 6913 | break; |
| 6914 | |
| 6915 | /* We might be able to coalesce without increasing nr_frags */ |
| 6916 | nr = clamp_t(size_t, space, 1, ARRAY_SIZE(pages)); |
| 6917 | |
| 6918 | len = iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off); |
| 6919 | if (len <= 0) { |
| 6920 | ret = len ?: -EIO; |
| 6921 | break; |
| 6922 | } |
| 6923 | |
| 6924 | i = 0; |
| 6925 | do { |
| 6926 | struct page *page = pages[i++]; |
| 6927 | size_t part = min_t(size_t, PAGE_SIZE - off, len); |
| 6928 | |
| 6929 | ret = -EIO; |
| 6930 | if (WARN_ON_ONCE(!sendpage_ok(page))) |
| 6931 | goto out; |
| 6932 | |
| 6933 | ret = skb_append_pagefrags(skb, page, off, part, |
| 6934 | frag_limit); |
| 6935 | if (ret < 0) { |
| 6936 | iov_iter_revert(iter, len); |
| 6937 | goto out; |
| 6938 | } |
| 6939 | |
| 6940 | if (skb->ip_summed == CHECKSUM_NONE) |
| 6941 | skb_splice_csum_page(skb, page, off, part); |
| 6942 | |
| 6943 | off = 0; |
| 6944 | spliced += part; |
| 6945 | maxsize -= part; |
| 6946 | len -= part; |
| 6947 | } while (len > 0); |
| 6948 | |
| 6949 | if (maxsize <= 0) |
| 6950 | break; |
| 6951 | } |
| 6952 | |
| 6953 | out: |
| 6954 | skb_len_add(skb, spliced); |
| 6955 | return spliced ?: ret; |
| 6956 | } |
| 6957 | EXPORT_SYMBOL(skb_splice_from_iter); |
| 6958 | |
| 6959 | static __always_inline |
| 6960 | size_t memcpy_from_iter_csum(void *iter_from, size_t progress, |
| 6961 | size_t len, void *to, void *priv2) |
| 6962 | { |
| 6963 | __wsum *csum = priv2; |
| 6964 | __wsum next = csum_partial_copy_nocheck(iter_from, to + progress, len); |
| 6965 | |
| 6966 | *csum = csum_block_add(*csum, next, progress); |
| 6967 | return 0; |
| 6968 | } |
| 6969 | |
| 6970 | static __always_inline |
| 6971 | size_t copy_from_user_iter_csum(void __user *iter_from, size_t progress, |
| 6972 | size_t len, void *to, void *priv2) |
| 6973 | { |
| 6974 | __wsum next, *csum = priv2; |
| 6975 | |
| 6976 | next = csum_and_copy_from_user(iter_from, to + progress, len); |
| 6977 | *csum = csum_block_add(*csum, next, progress); |
| 6978 | return next ? 0 : len; |
| 6979 | } |
| 6980 | |
| 6981 | bool csum_and_copy_from_iter_full(void *addr, size_t bytes, |
| 6982 | __wsum *csum, struct iov_iter *i) |
| 6983 | { |
| 6984 | size_t copied; |
| 6985 | |
| 6986 | if (WARN_ON_ONCE(!i->data_source)) |
| 6987 | return false; |
| 6988 | copied = iterate_and_advance2(i, bytes, addr, csum, |
| 6989 | copy_from_user_iter_csum, |
| 6990 | memcpy_from_iter_csum); |
| 6991 | if (likely(copied == bytes)) |
| 6992 | return true; |
| 6993 | iov_iter_revert(i, copied); |
| 6994 | return false; |
| 6995 | } |
| 6996 | EXPORT_SYMBOL(csum_and_copy_from_iter_full); |