1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Definitions for the 'struct sk_buff' memory handlers. 4 * 5 * Authors: 6 * Alan Cox, <[email protected]> 7 * Florian La Roche, <[email protected]> 8 */ 9 10 #ifndef _LINUX_SKBUFF_H 11 #define _LINUX_SKBUFF_H 12 13 #include <linux/kernel.h> 14 #include <linux/compiler.h> 15 #include <linux/time.h> 16 #include <linux/bug.h> 17 #include <linux/bvec.h> 18 #include <linux/cache.h> 19 #include <linux/rbtree.h> 20 #include <linux/socket.h> 21 #include <linux/refcount.h> 22 23 #include <linux/atomic.h> 24 #include <asm/types.h> 25 #include <linux/spinlock.h> 26 #include <linux/net.h> 27 #include <linux/textsearch.h> 28 #include <net/checksum.h> 29 #include <linux/rcupdate.h> 30 #include <linux/hrtimer.h> 31 #include <linux/dma-mapping.h> 32 #include <linux/netdev_features.h> 33 #include <linux/sched.h> 34 #include <linux/sched/clock.h> 35 #include <net/flow_dissector.h> 36 #include <linux/splice.h> 37 #include <linux/in6.h> 38 #include <linux/if_packet.h> 39 #include <linux/llist.h> 40 #include <net/flow.h> 41 #include <net/page_pool.h> 42 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 43 #include <linux/netfilter/nf_conntrack_common.h> 44 #endif 45 46 /* The interface for checksum offload between the stack and networking drivers 47 * is as follows... 48 * 49 * A. IP checksum related features 50 * 51 * Drivers advertise checksum offload capabilities in the features of a device. 52 * From the stack's point of view these are capabilities offered by the driver. 53 * A driver typically only advertises features that it is capable of offloading 54 * to its device. 55 * 56 * The checksum related features are: 57 * 58 * NETIF_F_HW_CSUM - The driver (or its device) is able to compute one 59 * IP (one's complement) checksum for any combination 60 * of protocols or protocol layering. The checksum is 61 * computed and set in a packet per the CHECKSUM_PARTIAL 62 * interface (see below). 63 * 64 * NETIF_F_IP_CSUM - Driver (device) is only able to checksum plain 65 * TCP or UDP packets over IPv4. These are specifically 66 * unencapsulated packets of the form IPv4|TCP or 67 * IPv4|UDP where the Protocol field in the IPv4 header 68 * is TCP or UDP. The IPv4 header may contain IP options. 69 * This feature cannot be set in features for a device 70 * with NETIF_F_HW_CSUM also set. This feature is being 71 * DEPRECATED (see below). 72 * 73 * NETIF_F_IPV6_CSUM - Driver (device) is only able to checksum plain 74 * TCP or UDP packets over IPv6. These are specifically 75 * unencapsulated packets of the form IPv6|TCP or 76 * IPv6|UDP where the Next Header field in the IPv6 77 * header is either TCP or UDP. IPv6 extension headers 78 * are not supported with this feature. This feature 79 * cannot be set in features for a device with 80 * NETIF_F_HW_CSUM also set. This feature is being 81 * DEPRECATED (see below). 82 * 83 * NETIF_F_RXCSUM - Driver (device) performs receive checksum offload. 84 * This flag is only used to disable the RX checksum 85 * feature for a device. The stack will accept receive 86 * checksum indication in packets received on a device 87 * regardless of whether NETIF_F_RXCSUM is set. 88 * 89 * B. Checksumming of received packets by device. Indication of checksum 90 * verification is set in skb->ip_summed. Possible values are: 91 * 92 * CHECKSUM_NONE: 93 * 94 * Device did not checksum this packet e.g. due to lack of capabilities. 95 * The packet contains full (though not verified) checksum in packet but 96 * not in skb->csum. Thus, skb->csum is undefined in this case. 97 * 98 * CHECKSUM_UNNECESSARY: 99 * 100 * The hardware you're dealing with doesn't calculate the full checksum 101 * (as in CHECKSUM_COMPLETE), but it does parse headers and verify checksums 102 * for specific protocols. For such packets it will set CHECKSUM_UNNECESSARY 103 * if their checksums are okay. skb->csum is still undefined in this case 104 * though. A driver or device must never modify the checksum field in the 105 * packet even if checksum is verified. 106 * 107 * CHECKSUM_UNNECESSARY is applicable to following protocols: 108 * TCP: IPv6 and IPv4. 109 * UDP: IPv4 and IPv6. A device may apply CHECKSUM_UNNECESSARY to a 110 * zero UDP checksum for either IPv4 or IPv6, the networking stack 111 * may perform further validation in this case. 112 * GRE: only if the checksum is present in the header. 113 * SCTP: indicates the CRC in SCTP header has been validated. 114 * FCOE: indicates the CRC in FC frame has been validated. 115 * 116 * skb->csum_level indicates the number of consecutive checksums found in 117 * the packet minus one that have been verified as CHECKSUM_UNNECESSARY. 118 * For instance if a device receives an IPv6->UDP->GRE->IPv4->TCP packet 119 * and a device is able to verify the checksums for UDP (possibly zero), 120 * GRE (checksum flag is set) and TCP, skb->csum_level would be set to 121 * two. If the device were only able to verify the UDP checksum and not 122 * GRE, either because it doesn't support GRE checksum or because GRE 123 * checksum is bad, skb->csum_level would be set to zero (TCP checksum is 124 * not considered in this case). 125 * 126 * CHECKSUM_COMPLETE: 127 * 128 * This is the most generic way. The device supplied checksum of the _whole_ 129 * packet as seen by netif_rx() and fills in skb->csum. This means the 130 * hardware doesn't need to parse L3/L4 headers to implement this. 131 * 132 * Notes: 133 * - Even if device supports only some protocols, but is able to produce 134 * skb->csum, it MUST use CHECKSUM_COMPLETE, not CHECKSUM_UNNECESSARY. 135 * - CHECKSUM_COMPLETE is not applicable to SCTP and FCoE protocols. 136 * 137 * CHECKSUM_PARTIAL: 138 * 139 * A checksum is set up to be offloaded to a device as described in the 140 * output description for CHECKSUM_PARTIAL. This may occur on a packet 141 * received directly from another Linux OS, e.g., a virtualized Linux kernel 142 * on the same host, or it may be set in the input path in GRO or remote 143 * checksum offload. For the purposes of checksum verification, the checksum 144 * referred to by skb->csum_start + skb->csum_offset and any preceding 145 * checksums in the packet are considered verified. Any checksums in the 146 * packet that are after the checksum being offloaded are not considered to 147 * be verified. 148 * 149 * C. Checksumming on transmit for non-GSO. The stack requests checksum offload 150 * in the skb->ip_summed for a packet. Values are: 151 * 152 * CHECKSUM_PARTIAL: 153 * 154 * The driver is required to checksum the packet as seen by hard_start_xmit() 155 * from skb->csum_start up to the end, and to record/write the checksum at 156 * offset skb->csum_start + skb->csum_offset. A driver may verify that the 157 * csum_start and csum_offset values are valid values given the length and 158 * offset of the packet, but it should not attempt to validate that the 159 * checksum refers to a legitimate transport layer checksum -- it is the 160 * purview of the stack to validate that csum_start and csum_offset are set 161 * correctly. 162 * 163 * When the stack requests checksum offload for a packet, the driver MUST 164 * ensure that the checksum is set correctly. A driver can either offload the 165 * checksum calculation to the device, or call skb_checksum_help (in the case 166 * that the device does not support offload for a particular checksum). 167 * 168 * NETIF_F_IP_CSUM and NETIF_F_IPV6_CSUM are being deprecated in favor of 169 * NETIF_F_HW_CSUM. New devices should use NETIF_F_HW_CSUM to indicate 170 * checksum offload capability. 171 * skb_csum_hwoffload_help() can be called to resolve CHECKSUM_PARTIAL based 172 * on network device checksumming capabilities: if a packet does not match 173 * them, skb_checksum_help or skb_crc32c_help (depending on the value of 174 * csum_not_inet, see item D.) is called to resolve the checksum. 175 * 176 * CHECKSUM_NONE: 177 * 178 * The skb was already checksummed by the protocol, or a checksum is not 179 * required. 180 * 181 * CHECKSUM_UNNECESSARY: 182 * 183 * This has the same meaning as CHECKSUM_NONE for checksum offload on 184 * output. 185 * 186 * CHECKSUM_COMPLETE: 187 * Not used in checksum output. If a driver observes a packet with this value 188 * set in skbuff, it should treat the packet as if CHECKSUM_NONE were set. 189 * 190 * D. Non-IP checksum (CRC) offloads 191 * 192 * NETIF_F_SCTP_CRC - This feature indicates that a device is capable of 193 * offloading the SCTP CRC in a packet. To perform this offload the stack 194 * will set csum_start and csum_offset accordingly, set ip_summed to 195 * CHECKSUM_PARTIAL and set csum_not_inet to 1, to provide an indication in 196 * the skbuff that the CHECKSUM_PARTIAL refers to CRC32c. 197 * A driver that supports both IP checksum offload and SCTP CRC32c offload 198 * must verify which offload is configured for a packet by testing the 199 * value of skb->csum_not_inet; skb_crc32c_csum_help is provided to resolve 200 * CHECKSUM_PARTIAL on skbs where csum_not_inet is set to 1. 201 * 202 * NETIF_F_FCOE_CRC - This feature indicates that a device is capable of 203 * offloading the FCOE CRC in a packet. To perform this offload the stack 204 * will set ip_summed to CHECKSUM_PARTIAL and set csum_start and csum_offset 205 * accordingly. Note that there is no indication in the skbuff that the 206 * CHECKSUM_PARTIAL refers to an FCOE checksum, so a driver that supports 207 * both IP checksum offload and FCOE CRC offload must verify which offload 208 * is configured for a packet, presumably by inspecting packet headers. 209 * 210 * E. Checksumming on output with GSO. 211 * 212 * In the case of a GSO packet (skb_is_gso(skb) is true), checksum offload 213 * is implied by the SKB_GSO_* flags in gso_type. Most obviously, if the 214 * gso_type is SKB_GSO_TCPV4 or SKB_GSO_TCPV6, TCP checksum offload as 215 * part of the GSO operation is implied. If a checksum is being offloaded 216 * with GSO then ip_summed is CHECKSUM_PARTIAL, and both csum_start and 217 * csum_offset are set to refer to the outermost checksum being offloaded 218 * (two offloaded checksums are possible with UDP encapsulation). 219 */ 220 221 /* Don't change this without changing skb_csum_unnecessary! */ 222 #define CHECKSUM_NONE 0 223 #define CHECKSUM_UNNECESSARY 1 224 #define CHECKSUM_COMPLETE 2 225 #define CHECKSUM_PARTIAL 3 226 227 /* Maximum value in skb->csum_level */ 228 #define SKB_MAX_CSUM_LEVEL 3 229 230 #define SKB_DATA_ALIGN(X) ALIGN(X, SMP_CACHE_BYTES) 231 #define SKB_WITH_OVERHEAD(X) \ 232 ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) 233 #define SKB_MAX_ORDER(X, ORDER) \ 234 SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X)) 235 #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0)) 236 #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2)) 237 238 /* return minimum truesize of one skb containing X bytes of data */ 239 #define SKB_TRUESIZE(X) ((X) + \ 240 SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \ 241 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) 242 243 struct ahash_request; 244 struct net_device; 245 struct scatterlist; 246 struct pipe_inode_info; 247 struct iov_iter; 248 struct napi_struct; 249 struct bpf_prog; 250 union bpf_attr; 251 struct skb_ext; 252 253 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 254 struct nf_bridge_info { 255 enum { 256 BRNF_PROTO_UNCHANGED, 257 BRNF_PROTO_8021Q, 258 BRNF_PROTO_PPPOE 259 } orig_proto:8; 260 u8 pkt_otherhost:1; 261 u8 in_prerouting:1; 262 u8 bridged_dnat:1; 263 __u16 frag_max_size; 264 struct net_device *physindev; 265 266 /* always valid & non-NULL from FORWARD on, for physdev match */ 267 struct net_device *physoutdev; 268 union { 269 /* prerouting: detect dnat in orig/reply direction */ 270 __be32 ipv4_daddr; 271 struct in6_addr ipv6_daddr; 272 273 /* after prerouting + nat detected: store original source 274 * mac since neigh resolution overwrites it, only used while 275 * skb is out in neigh layer. 276 */ 277 char neigh_header[8]; 278 }; 279 }; 280 #endif 281 282 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 283 /* Chain in tc_skb_ext will be used to share the tc chain with 284 * ovs recirc_id. It will be set to the current chain by tc 285 * and read by ovs to recirc_id. 286 */ 287 struct tc_skb_ext { 288 __u32 chain; 289 __u16 mru; 290 __u16 zone; 291 u8 post_ct:1; 292 u8 post_ct_snat:1; 293 u8 post_ct_dnat:1; 294 }; 295 #endif 296 297 struct sk_buff_head { 298 /* These two members must be first to match sk_buff. */ 299 struct_group_tagged(sk_buff_list, list, 300 struct sk_buff *next; 301 struct sk_buff *prev; 302 ); 303 304 __u32 qlen; 305 spinlock_t lock; 306 }; 307 308 struct sk_buff; 309 310 /* The reason of skb drop, which is used in kfree_skb_reason(). 311 * en...maybe they should be splited by group? 312 * 313 * Each item here should also be in 'TRACE_SKB_DROP_REASON', which is 314 * used to translate the reason to string. 315 */ 316 enum skb_drop_reason { 317 SKB_DROP_REASON_NOT_SPECIFIED, /* drop reason is not specified */ 318 SKB_DROP_REASON_NO_SOCKET, /* socket not found */ 319 SKB_DROP_REASON_PKT_TOO_SMALL, /* packet size is too small */ 320 SKB_DROP_REASON_TCP_CSUM, /* TCP checksum error */ 321 SKB_DROP_REASON_SOCKET_FILTER, /* dropped by socket filter */ 322 SKB_DROP_REASON_UDP_CSUM, /* UDP checksum error */ 323 SKB_DROP_REASON_NETFILTER_DROP, /* dropped by netfilter */ 324 SKB_DROP_REASON_OTHERHOST, /* packet don't belong to current 325 * host (interface is in promisc 326 * mode) 327 */ 328 SKB_DROP_REASON_IP_CSUM, /* IP checksum error */ 329 SKB_DROP_REASON_IP_INHDR, /* there is something wrong with 330 * IP header (see 331 * IPSTATS_MIB_INHDRERRORS) 332 */ 333 SKB_DROP_REASON_IP_RPFILTER, /* IP rpfilter validate failed. 334 * see the document for rp_filter 335 * in ip-sysctl.rst for more 336 * information 337 */ 338 SKB_DROP_REASON_UNICAST_IN_L2_MULTICAST, /* destination address of L2 339 * is multicast, but L3 is 340 * unicast. 341 */ 342 SKB_DROP_REASON_XFRM_POLICY, /* xfrm policy check failed */ 343 SKB_DROP_REASON_IP_NOPROTO, /* no support for IP protocol */ 344 SKB_DROP_REASON_SOCKET_RCVBUFF, /* socket receive buff is full */ 345 SKB_DROP_REASON_PROTO_MEM, /* proto memory limition, such as 346 * udp packet drop out of 347 * udp_memory_allocated. 348 */ 349 SKB_DROP_REASON_TCP_MD5NOTFOUND, /* no MD5 hash and one 350 * expected, corresponding 351 * to LINUX_MIB_TCPMD5NOTFOUND 352 */ 353 SKB_DROP_REASON_TCP_MD5UNEXPECTED, /* MD5 hash and we're not 354 * expecting one, corresponding 355 * to LINUX_MIB_TCPMD5UNEXPECTED 356 */ 357 SKB_DROP_REASON_TCP_MD5FAILURE, /* MD5 hash and its wrong, 358 * corresponding to 359 * LINUX_MIB_TCPMD5FAILURE 360 */ 361 SKB_DROP_REASON_SOCKET_BACKLOG, /* failed to add skb to socket 362 * backlog (see 363 * LINUX_MIB_TCPBACKLOGDROP) 364 */ 365 SKB_DROP_REASON_TCP_FLAGS, /* TCP flags invalid */ 366 SKB_DROP_REASON_TCP_ZEROWINDOW, /* TCP receive window size is zero, 367 * see LINUX_MIB_TCPZEROWINDOWDROP 368 */ 369 SKB_DROP_REASON_TCP_OLD_DATA, /* the TCP data reveived is already 370 * received before (spurious retrans 371 * may happened), see 372 * LINUX_MIB_DELAYEDACKLOST 373 */ 374 SKB_DROP_REASON_TCP_OVERWINDOW, /* the TCP data is out of window, 375 * the seq of the first byte exceed 376 * the right edges of receive 377 * window 378 */ 379 SKB_DROP_REASON_TCP_OFOMERGE, /* the data of skb is already in 380 * the ofo queue, corresponding to 381 * LINUX_MIB_TCPOFOMERGE 382 */ 383 SKB_DROP_REASON_MAX, 384 }; 385 386 /* To allow 64K frame to be packed as single skb without frag_list we 387 * require 64K/PAGE_SIZE pages plus 1 additional page to allow for 388 * buffers which do not start on a page boundary. 389 * 390 * Since GRO uses frags we allocate at least 16 regardless of page 391 * size. 392 */ 393 #if (65536/PAGE_SIZE + 1) < 16 394 #define MAX_SKB_FRAGS 16UL 395 #else 396 #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1) 397 #endif 398 extern int sysctl_max_skb_frags; 399 400 /* Set skb_shinfo(skb)->gso_size to this in case you want skb_segment to 401 * segment using its current segmentation instead. 402 */ 403 #define GSO_BY_FRAGS 0xFFFF 404 405 typedef struct bio_vec skb_frag_t; 406 407 /** 408 * skb_frag_size() - Returns the size of a skb fragment 409 * @frag: skb fragment 410 */ 411 static inline unsigned int skb_frag_size(const skb_frag_t *frag) 412 { 413 return frag->bv_len; 414 } 415 416 /** 417 * skb_frag_size_set() - Sets the size of a skb fragment 418 * @frag: skb fragment 419 * @size: size of fragment 420 */ 421 static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size) 422 { 423 frag->bv_len = size; 424 } 425 426 /** 427 * skb_frag_size_add() - Increments the size of a skb fragment by @delta 428 * @frag: skb fragment 429 * @delta: value to add 430 */ 431 static inline void skb_frag_size_add(skb_frag_t *frag, int delta) 432 { 433 frag->bv_len += delta; 434 } 435 436 /** 437 * skb_frag_size_sub() - Decrements the size of a skb fragment by @delta 438 * @frag: skb fragment 439 * @delta: value to subtract 440 */ 441 static inline void skb_frag_size_sub(skb_frag_t *frag, int delta) 442 { 443 frag->bv_len -= delta; 444 } 445 446 /** 447 * skb_frag_must_loop - Test if %p is a high memory page 448 * @p: fragment's page 449 */ 450 static inline bool skb_frag_must_loop(struct page *p) 451 { 452 #if defined(CONFIG_HIGHMEM) 453 if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP) || PageHighMem(p)) 454 return true; 455 #endif 456 return false; 457 } 458 459 /** 460 * skb_frag_foreach_page - loop over pages in a fragment 461 * 462 * @f: skb frag to operate on 463 * @f_off: offset from start of f->bv_page 464 * @f_len: length from f_off to loop over 465 * @p: (temp var) current page 466 * @p_off: (temp var) offset from start of current page, 467 * non-zero only on first page. 468 * @p_len: (temp var) length in current page, 469 * < PAGE_SIZE only on first and last page. 470 * @copied: (temp var) length so far, excluding current p_len. 471 * 472 * A fragment can hold a compound page, in which case per-page 473 * operations, notably kmap_atomic, must be called for each 474 * regular page. 475 */ 476 #define skb_frag_foreach_page(f, f_off, f_len, p, p_off, p_len, copied) \ 477 for (p = skb_frag_page(f) + ((f_off) >> PAGE_SHIFT), \ 478 p_off = (f_off) & (PAGE_SIZE - 1), \ 479 p_len = skb_frag_must_loop(p) ? \ 480 min_t(u32, f_len, PAGE_SIZE - p_off) : f_len, \ 481 copied = 0; \ 482 copied < f_len; \ 483 copied += p_len, p++, p_off = 0, \ 484 p_len = min_t(u32, f_len - copied, PAGE_SIZE)) \ 485 486 #define HAVE_HW_TIME_STAMP 487 488 /** 489 * struct skb_shared_hwtstamps - hardware time stamps 490 * @hwtstamp: hardware time stamp transformed into duration 491 * since arbitrary point in time 492 * 493 * Software time stamps generated by ktime_get_real() are stored in 494 * skb->tstamp. 495 * 496 * hwtstamps can only be compared against other hwtstamps from 497 * the same device. 498 * 499 * This structure is attached to packets as part of the 500 * &skb_shared_info. Use skb_hwtstamps() to get a pointer. 501 */ 502 struct skb_shared_hwtstamps { 503 ktime_t hwtstamp; 504 }; 505 506 /* Definitions for tx_flags in struct skb_shared_info */ 507 enum { 508 /* generate hardware time stamp */ 509 SKBTX_HW_TSTAMP = 1 << 0, 510 511 /* generate software time stamp when queueing packet to NIC */ 512 SKBTX_SW_TSTAMP = 1 << 1, 513 514 /* device driver is going to provide hardware time stamp */ 515 SKBTX_IN_PROGRESS = 1 << 2, 516 517 /* generate wifi status information (where possible) */ 518 SKBTX_WIFI_STATUS = 1 << 4, 519 520 /* generate software time stamp when entering packet scheduling */ 521 SKBTX_SCHED_TSTAMP = 1 << 6, 522 }; 523 524 #define SKBTX_ANY_SW_TSTAMP (SKBTX_SW_TSTAMP | \ 525 SKBTX_SCHED_TSTAMP) 526 #define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | SKBTX_ANY_SW_TSTAMP) 527 528 /* Definitions for flags in struct skb_shared_info */ 529 enum { 530 /* use zcopy routines */ 531 SKBFL_ZEROCOPY_ENABLE = BIT(0), 532 533 /* This indicates at least one fragment might be overwritten 534 * (as in vmsplice(), sendfile() ...) 535 * If we need to compute a TX checksum, we'll need to copy 536 * all frags to avoid possible bad checksum 537 */ 538 SKBFL_SHARED_FRAG = BIT(1), 539 540 /* segment contains only zerocopy data and should not be 541 * charged to the kernel memory. 542 */ 543 SKBFL_PURE_ZEROCOPY = BIT(2), 544 }; 545 546 #define SKBFL_ZEROCOPY_FRAG (SKBFL_ZEROCOPY_ENABLE | SKBFL_SHARED_FRAG) 547 #define SKBFL_ALL_ZEROCOPY (SKBFL_ZEROCOPY_FRAG | SKBFL_PURE_ZEROCOPY) 548 549 /* 550 * The callback notifies userspace to release buffers when skb DMA is done in 551 * lower device, the skb last reference should be 0 when calling this. 552 * The zerocopy_success argument is true if zero copy transmit occurred, 553 * false on data copy or out of memory error caused by data copy attempt. 554 * The ctx field is used to track device context. 555 * The desc field is used to track userspace buffer index. 556 */ 557 struct ubuf_info { 558 void (*callback)(struct sk_buff *, struct ubuf_info *, 559 bool zerocopy_success); 560 union { 561 struct { 562 unsigned long desc; 563 void *ctx; 564 }; 565 struct { 566 u32 id; 567 u16 len; 568 u16 zerocopy:1; 569 u32 bytelen; 570 }; 571 }; 572 refcount_t refcnt; 573 u8 flags; 574 575 struct mmpin { 576 struct user_struct *user; 577 unsigned int num_pg; 578 } mmp; 579 }; 580 581 #define skb_uarg(SKB) ((struct ubuf_info *)(skb_shinfo(SKB)->destructor_arg)) 582 583 int mm_account_pinned_pages(struct mmpin *mmp, size_t size); 584 void mm_unaccount_pinned_pages(struct mmpin *mmp); 585 586 struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size); 587 struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 588 struct ubuf_info *uarg); 589 590 void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref); 591 592 void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 593 bool success); 594 595 int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len); 596 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 597 struct msghdr *msg, int len, 598 struct ubuf_info *uarg); 599 600 /* This data is invariant across clones and lives at 601 * the end of the header data, ie. at skb->end. 602 */ 603 struct skb_shared_info { 604 __u8 flags; 605 __u8 meta_len; 606 __u8 nr_frags; 607 __u8 tx_flags; 608 unsigned short gso_size; 609 /* Warning: this field is not always filled in (UFO)! */ 610 unsigned short gso_segs; 611 struct sk_buff *frag_list; 612 struct skb_shared_hwtstamps hwtstamps; 613 unsigned int gso_type; 614 u32 tskey; 615 616 /* 617 * Warning : all fields before dataref are cleared in __alloc_skb() 618 */ 619 atomic_t dataref; 620 unsigned int xdp_frags_size; 621 622 /* Intermediate layers must ensure that destructor_arg 623 * remains valid until skb destructor */ 624 void * destructor_arg; 625 626 /* must be last field, see pskb_expand_head() */ 627 skb_frag_t frags[MAX_SKB_FRAGS]; 628 }; 629 630 /* We divide dataref into two halves. The higher 16 bits hold references 631 * to the payload part of skb->data. The lower 16 bits hold references to 632 * the entire skb->data. A clone of a headerless skb holds the length of 633 * the header in skb->hdr_len. 634 * 635 * All users must obey the rule that the skb->data reference count must be 636 * greater than or equal to the payload reference count. 637 * 638 * Holding a reference to the payload part means that the user does not 639 * care about modifications to the header part of skb->data. 640 */ 641 #define SKB_DATAREF_SHIFT 16 642 #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1) 643 644 645 enum { 646 SKB_FCLONE_UNAVAILABLE, /* skb has no fclone (from head_cache) */ 647 SKB_FCLONE_ORIG, /* orig skb (from fclone_cache) */ 648 SKB_FCLONE_CLONE, /* companion fclone skb (from fclone_cache) */ 649 }; 650 651 enum { 652 SKB_GSO_TCPV4 = 1 << 0, 653 654 /* This indicates the skb is from an untrusted source. */ 655 SKB_GSO_DODGY = 1 << 1, 656 657 /* This indicates the tcp segment has CWR set. */ 658 SKB_GSO_TCP_ECN = 1 << 2, 659 660 SKB_GSO_TCP_FIXEDID = 1 << 3, 661 662 SKB_GSO_TCPV6 = 1 << 4, 663 664 SKB_GSO_FCOE = 1 << 5, 665 666 SKB_GSO_GRE = 1 << 6, 667 668 SKB_GSO_GRE_CSUM = 1 << 7, 669 670 SKB_GSO_IPXIP4 = 1 << 8, 671 672 SKB_GSO_IPXIP6 = 1 << 9, 673 674 SKB_GSO_UDP_TUNNEL = 1 << 10, 675 676 SKB_GSO_UDP_TUNNEL_CSUM = 1 << 11, 677 678 SKB_GSO_PARTIAL = 1 << 12, 679 680 SKB_GSO_TUNNEL_REMCSUM = 1 << 13, 681 682 SKB_GSO_SCTP = 1 << 14, 683 684 SKB_GSO_ESP = 1 << 15, 685 686 SKB_GSO_UDP = 1 << 16, 687 688 SKB_GSO_UDP_L4 = 1 << 17, 689 690 SKB_GSO_FRAGLIST = 1 << 18, 691 }; 692 693 #if BITS_PER_LONG > 32 694 #define NET_SKBUFF_DATA_USES_OFFSET 1 695 #endif 696 697 #ifdef NET_SKBUFF_DATA_USES_OFFSET 698 typedef unsigned int sk_buff_data_t; 699 #else 700 typedef unsigned char *sk_buff_data_t; 701 #endif 702 703 /** 704 * struct sk_buff - socket buffer 705 * @next: Next buffer in list 706 * @prev: Previous buffer in list 707 * @tstamp: Time we arrived/left 708 * @skb_mstamp_ns: (aka @tstamp) earliest departure time; start point 709 * for retransmit timer 710 * @rbnode: RB tree node, alternative to next/prev for netem/tcp 711 * @list: queue head 712 * @ll_node: anchor in an llist (eg socket defer_list) 713 * @sk: Socket we are owned by 714 * @ip_defrag_offset: (aka @sk) alternate use of @sk, used in 715 * fragmentation management 716 * @dev: Device we arrived on/are leaving by 717 * @dev_scratch: (aka @dev) alternate use of @dev when @dev would be %NULL 718 * @cb: Control buffer. Free for use by every layer. Put private vars here 719 * @_skb_refdst: destination entry (with norefcount bit) 720 * @sp: the security path, used for xfrm 721 * @len: Length of actual data 722 * @data_len: Data length 723 * @mac_len: Length of link layer header 724 * @hdr_len: writable header length of cloned skb 725 * @csum: Checksum (must include start/offset pair) 726 * @csum_start: Offset from skb->head where checksumming should start 727 * @csum_offset: Offset from csum_start where checksum should be stored 728 * @priority: Packet queueing priority 729 * @ignore_df: allow local fragmentation 730 * @cloned: Head may be cloned (check refcnt to be sure) 731 * @ip_summed: Driver fed us an IP checksum 732 * @nohdr: Payload reference only, must not modify header 733 * @pkt_type: Packet class 734 * @fclone: skbuff clone status 735 * @ipvs_property: skbuff is owned by ipvs 736 * @inner_protocol_type: whether the inner protocol is 737 * ENCAP_TYPE_ETHER or ENCAP_TYPE_IPPROTO 738 * @remcsum_offload: remote checksum offload is enabled 739 * @offload_fwd_mark: Packet was L2-forwarded in hardware 740 * @offload_l3_fwd_mark: Packet was L3-forwarded in hardware 741 * @tc_skip_classify: do not classify packet. set by IFB device 742 * @tc_at_ingress: used within tc_classify to distinguish in/egress 743 * @redirected: packet was redirected by packet classifier 744 * @from_ingress: packet was redirected from the ingress path 745 * @nf_skip_egress: packet shall skip nf egress - see netfilter_netdev.h 746 * @peeked: this packet has been seen already, so stats have been 747 * done for it, don't do them again 748 * @nf_trace: netfilter packet trace flag 749 * @protocol: Packet protocol from driver 750 * @destructor: Destruct function 751 * @tcp_tsorted_anchor: list structure for TCP (tp->tsorted_sent_queue) 752 * @_sk_redir: socket redirection information for skmsg 753 * @_nfct: Associated connection, if any (with nfctinfo bits) 754 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c 755 * @skb_iif: ifindex of device we arrived on 756 * @tc_index: Traffic control index 757 * @hash: the packet hash 758 * @queue_mapping: Queue mapping for multiqueue devices 759 * @head_frag: skb was allocated from page fragments, 760 * not allocated by kmalloc() or vmalloc(). 761 * @pfmemalloc: skbuff was allocated from PFMEMALLOC reserves 762 * @pp_recycle: mark the packet for recycling instead of freeing (implies 763 * page_pool support on driver) 764 * @active_extensions: active extensions (skb_ext_id types) 765 * @ndisc_nodetype: router type (from link layer) 766 * @ooo_okay: allow the mapping of a socket to a queue to be changed 767 * @l4_hash: indicate hash is a canonical 4-tuple hash over transport 768 * ports. 769 * @sw_hash: indicates hash was computed in software stack 770 * @wifi_acked_valid: wifi_acked was set 771 * @wifi_acked: whether frame was acked on wifi or not 772 * @no_fcs: Request NIC to treat last 4 bytes as Ethernet FCS 773 * @encapsulation: indicates the inner headers in the skbuff are valid 774 * @encap_hdr_csum: software checksum is needed 775 * @csum_valid: checksum is already valid 776 * @csum_not_inet: use CRC32c to resolve CHECKSUM_PARTIAL 777 * @csum_complete_sw: checksum was completed by software 778 * @csum_level: indicates the number of consecutive checksums found in 779 * the packet minus one that have been verified as 780 * CHECKSUM_UNNECESSARY (max 3) 781 * @dst_pending_confirm: need to confirm neighbour 782 * @decrypted: Decrypted SKB 783 * @slow_gro: state present at GRO time, slower prepare step required 784 * @napi_id: id of the NAPI struct this skb came from 785 * @sender_cpu: (aka @napi_id) source CPU in XPS 786 * @secmark: security marking 787 * @mark: Generic packet mark 788 * @reserved_tailroom: (aka @mark) number of bytes of free space available 789 * at the tail of an sk_buff 790 * @vlan_present: VLAN tag is present 791 * @vlan_proto: vlan encapsulation protocol 792 * @vlan_tci: vlan tag control information 793 * @inner_protocol: Protocol (encapsulation) 794 * @inner_ipproto: (aka @inner_protocol) stores ipproto when 795 * skb->inner_protocol_type == ENCAP_TYPE_IPPROTO; 796 * @inner_transport_header: Inner transport layer header (encapsulation) 797 * @inner_network_header: Network layer header (encapsulation) 798 * @inner_mac_header: Link layer header (encapsulation) 799 * @transport_header: Transport layer header 800 * @network_header: Network layer header 801 * @mac_header: Link layer header 802 * @kcov_handle: KCOV remote handle for remote coverage collection 803 * @tail: Tail pointer 804 * @end: End pointer 805 * @head: Head of buffer 806 * @data: Data head pointer 807 * @truesize: Buffer size 808 * @users: User count - see {datagram,tcp}.c 809 * @extensions: allocated extensions, valid if active_extensions is nonzero 810 */ 811 812 struct sk_buff { 813 union { 814 struct { 815 /* These two members must be first to match sk_buff_head. */ 816 struct sk_buff *next; 817 struct sk_buff *prev; 818 819 union { 820 struct net_device *dev; 821 /* Some protocols might use this space to store information, 822 * while device pointer would be NULL. 823 * UDP receive path is one user. 824 */ 825 unsigned long dev_scratch; 826 }; 827 }; 828 struct rb_node rbnode; /* used in netem, ip4 defrag, and tcp stack */ 829 struct list_head list; 830 struct llist_node ll_node; 831 }; 832 833 union { 834 struct sock *sk; 835 int ip_defrag_offset; 836 }; 837 838 union { 839 ktime_t tstamp; 840 u64 skb_mstamp_ns; /* earliest departure time */ 841 }; 842 /* 843 * This is the control buffer. It is free to use for every 844 * layer. Please put your private variables there. If you 845 * want to keep them across layers you have to do a skb_clone() 846 * first. This is owned by whoever has the skb queued ATM. 847 */ 848 char cb[48] __aligned(8); 849 850 union { 851 struct { 852 unsigned long _skb_refdst; 853 void (*destructor)(struct sk_buff *skb); 854 }; 855 struct list_head tcp_tsorted_anchor; 856 #ifdef CONFIG_NET_SOCK_MSG 857 unsigned long _sk_redir; 858 #endif 859 }; 860 861 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 862 unsigned long _nfct; 863 #endif 864 unsigned int len, 865 data_len; 866 __u16 mac_len, 867 hdr_len; 868 869 /* Following fields are _not_ copied in __copy_skb_header() 870 * Note that queue_mapping is here mostly to fill a hole. 871 */ 872 __u16 queue_mapping; 873 874 /* if you move cloned around you also must adapt those constants */ 875 #ifdef __BIG_ENDIAN_BITFIELD 876 #define CLONED_MASK (1 << 7) 877 #else 878 #define CLONED_MASK 1 879 #endif 880 #define CLONED_OFFSET offsetof(struct sk_buff, __cloned_offset) 881 882 /* private: */ 883 __u8 __cloned_offset[0]; 884 /* public: */ 885 __u8 cloned:1, 886 nohdr:1, 887 fclone:2, 888 peeked:1, 889 head_frag:1, 890 pfmemalloc:1, 891 pp_recycle:1; /* page_pool recycle indicator */ 892 #ifdef CONFIG_SKB_EXTENSIONS 893 __u8 active_extensions; 894 #endif 895 896 /* Fields enclosed in headers group are copied 897 * using a single memcpy() in __copy_skb_header() 898 */ 899 struct_group(headers, 900 901 /* private: */ 902 __u8 __pkt_type_offset[0]; 903 /* public: */ 904 __u8 pkt_type:3; /* see PKT_TYPE_MAX */ 905 __u8 ignore_df:1; 906 __u8 nf_trace:1; 907 __u8 ip_summed:2; 908 __u8 ooo_okay:1; 909 910 __u8 l4_hash:1; 911 __u8 sw_hash:1; 912 __u8 wifi_acked_valid:1; 913 __u8 wifi_acked:1; 914 __u8 no_fcs:1; 915 /* Indicates the inner headers are valid in the skbuff. */ 916 __u8 encapsulation:1; 917 __u8 encap_hdr_csum:1; 918 __u8 csum_valid:1; 919 920 /* private: */ 921 __u8 __pkt_vlan_present_offset[0]; 922 /* public: */ 923 __u8 vlan_present:1; /* See PKT_VLAN_PRESENT_BIT */ 924 __u8 csum_complete_sw:1; 925 __u8 csum_level:2; 926 __u8 csum_not_inet:1; 927 __u8 dst_pending_confirm:1; 928 #ifdef CONFIG_IPV6_NDISC_NODETYPE 929 __u8 ndisc_nodetype:2; 930 #endif 931 932 __u8 ipvs_property:1; 933 __u8 inner_protocol_type:1; 934 __u8 remcsum_offload:1; 935 #ifdef CONFIG_NET_SWITCHDEV 936 __u8 offload_fwd_mark:1; 937 __u8 offload_l3_fwd_mark:1; 938 #endif 939 #ifdef CONFIG_NET_CLS_ACT 940 __u8 tc_skip_classify:1; 941 __u8 tc_at_ingress:1; 942 #endif 943 __u8 redirected:1; 944 #ifdef CONFIG_NET_REDIRECT 945 __u8 from_ingress:1; 946 #endif 947 #ifdef CONFIG_NETFILTER_SKIP_EGRESS 948 __u8 nf_skip_egress:1; 949 #endif 950 #ifdef CONFIG_TLS_DEVICE 951 __u8 decrypted:1; 952 #endif 953 __u8 slow_gro:1; 954 955 #ifdef CONFIG_NET_SCHED 956 __u16 tc_index; /* traffic control index */ 957 #endif 958 959 union { 960 __wsum csum; 961 struct { 962 __u16 csum_start; 963 __u16 csum_offset; 964 }; 965 }; 966 __u32 priority; 967 int skb_iif; 968 __u32 hash; 969 __be16 vlan_proto; 970 __u16 vlan_tci; 971 #if defined(CONFIG_NET_RX_BUSY_POLL) || defined(CONFIG_XPS) 972 union { 973 unsigned int napi_id; 974 unsigned int sender_cpu; 975 }; 976 #endif 977 #ifdef CONFIG_NETWORK_SECMARK 978 __u32 secmark; 979 #endif 980 981 union { 982 __u32 mark; 983 __u32 reserved_tailroom; 984 }; 985 986 union { 987 __be16 inner_protocol; 988 __u8 inner_ipproto; 989 }; 990 991 __u16 inner_transport_header; 992 __u16 inner_network_header; 993 __u16 inner_mac_header; 994 995 __be16 protocol; 996 __u16 transport_header; 997 __u16 network_header; 998 __u16 mac_header; 999 1000 #ifdef CONFIG_KCOV 1001 u64 kcov_handle; 1002 #endif 1003 1004 ); /* end headers group */ 1005 1006 /* These elements must be at the end, see alloc_skb() for details. */ 1007 sk_buff_data_t tail; 1008 sk_buff_data_t end; 1009 unsigned char *head, 1010 *data; 1011 unsigned int truesize; 1012 refcount_t users; 1013 1014 #ifdef CONFIG_SKB_EXTENSIONS 1015 /* only useable after checking ->active_extensions != 0 */ 1016 struct skb_ext *extensions; 1017 #endif 1018 }; 1019 1020 /* if you move pkt_type around you also must adapt those constants */ 1021 #ifdef __BIG_ENDIAN_BITFIELD 1022 #define PKT_TYPE_MAX (7 << 5) 1023 #else 1024 #define PKT_TYPE_MAX 7 1025 #endif 1026 #define PKT_TYPE_OFFSET offsetof(struct sk_buff, __pkt_type_offset) 1027 1028 /* if you move pkt_vlan_present around you also must adapt these constants */ 1029 #ifdef __BIG_ENDIAN_BITFIELD 1030 #define PKT_VLAN_PRESENT_BIT 7 1031 #else 1032 #define PKT_VLAN_PRESENT_BIT 0 1033 #endif 1034 #define PKT_VLAN_PRESENT_OFFSET offsetof(struct sk_buff, __pkt_vlan_present_offset) 1035 1036 #ifdef __KERNEL__ 1037 /* 1038 * Handling routines are only of interest to the kernel 1039 */ 1040 1041 #define SKB_ALLOC_FCLONE 0x01 1042 #define SKB_ALLOC_RX 0x02 1043 #define SKB_ALLOC_NAPI 0x04 1044 1045 /** 1046 * skb_pfmemalloc - Test if the skb was allocated from PFMEMALLOC reserves 1047 * @skb: buffer 1048 */ 1049 static inline bool skb_pfmemalloc(const struct sk_buff *skb) 1050 { 1051 return unlikely(skb->pfmemalloc); 1052 } 1053 1054 /* 1055 * skb might have a dst pointer attached, refcounted or not. 1056 * _skb_refdst low order bit is set if refcount was _not_ taken 1057 */ 1058 #define SKB_DST_NOREF 1UL 1059 #define SKB_DST_PTRMASK ~(SKB_DST_NOREF) 1060 1061 /** 1062 * skb_dst - returns skb dst_entry 1063 * @skb: buffer 1064 * 1065 * Returns skb dst_entry, regardless of reference taken or not. 1066 */ 1067 static inline struct dst_entry *skb_dst(const struct sk_buff *skb) 1068 { 1069 /* If refdst was not refcounted, check we still are in a 1070 * rcu_read_lock section 1071 */ 1072 WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) && 1073 !rcu_read_lock_held() && 1074 !rcu_read_lock_bh_held()); 1075 return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK); 1076 } 1077 1078 /** 1079 * skb_dst_set - sets skb dst 1080 * @skb: buffer 1081 * @dst: dst entry 1082 * 1083 * Sets skb dst, assuming a reference was taken on dst and should 1084 * be released by skb_dst_drop() 1085 */ 1086 static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst) 1087 { 1088 skb->slow_gro |= !!dst; 1089 skb->_skb_refdst = (unsigned long)dst; 1090 } 1091 1092 /** 1093 * skb_dst_set_noref - sets skb dst, hopefully, without taking reference 1094 * @skb: buffer 1095 * @dst: dst entry 1096 * 1097 * Sets skb dst, assuming a reference was not taken on dst. 1098 * If dst entry is cached, we do not take reference and dst_release 1099 * will be avoided by refdst_drop. If dst entry is not cached, we take 1100 * reference, so that last dst_release can destroy the dst immediately. 1101 */ 1102 static inline void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst) 1103 { 1104 WARN_ON(!rcu_read_lock_held() && !rcu_read_lock_bh_held()); 1105 skb->slow_gro |= !!dst; 1106 skb->_skb_refdst = (unsigned long)dst | SKB_DST_NOREF; 1107 } 1108 1109 /** 1110 * skb_dst_is_noref - Test if skb dst isn't refcounted 1111 * @skb: buffer 1112 */ 1113 static inline bool skb_dst_is_noref(const struct sk_buff *skb) 1114 { 1115 return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb); 1116 } 1117 1118 /** 1119 * skb_rtable - Returns the skb &rtable 1120 * @skb: buffer 1121 */ 1122 static inline struct rtable *skb_rtable(const struct sk_buff *skb) 1123 { 1124 return (struct rtable *)skb_dst(skb); 1125 } 1126 1127 /* For mangling skb->pkt_type from user space side from applications 1128 * such as nft, tc, etc, we only allow a conservative subset of 1129 * possible pkt_types to be set. 1130 */ 1131 static inline bool skb_pkt_type_ok(u32 ptype) 1132 { 1133 return ptype <= PACKET_OTHERHOST; 1134 } 1135 1136 /** 1137 * skb_napi_id - Returns the skb's NAPI id 1138 * @skb: buffer 1139 */ 1140 static inline unsigned int skb_napi_id(const struct sk_buff *skb) 1141 { 1142 #ifdef CONFIG_NET_RX_BUSY_POLL 1143 return skb->napi_id; 1144 #else 1145 return 0; 1146 #endif 1147 } 1148 1149 /** 1150 * skb_unref - decrement the skb's reference count 1151 * @skb: buffer 1152 * 1153 * Returns true if we can free the skb. 1154 */ 1155 static inline bool skb_unref(struct sk_buff *skb) 1156 { 1157 if (unlikely(!skb)) 1158 return false; 1159 if (likely(refcount_read(&skb->users) == 1)) 1160 smp_rmb(); 1161 else if (likely(!refcount_dec_and_test(&skb->users))) 1162 return false; 1163 1164 return true; 1165 } 1166 1167 void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason); 1168 1169 /** 1170 * kfree_skb - free an sk_buff with 'NOT_SPECIFIED' reason 1171 * @skb: buffer to free 1172 */ 1173 static inline void kfree_skb(struct sk_buff *skb) 1174 { 1175 kfree_skb_reason(skb, SKB_DROP_REASON_NOT_SPECIFIED); 1176 } 1177 1178 void skb_release_head_state(struct sk_buff *skb); 1179 void kfree_skb_list(struct sk_buff *segs); 1180 void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt); 1181 void skb_tx_error(struct sk_buff *skb); 1182 1183 #ifdef CONFIG_TRACEPOINTS 1184 void consume_skb(struct sk_buff *skb); 1185 #else 1186 static inline void consume_skb(struct sk_buff *skb) 1187 { 1188 return kfree_skb(skb); 1189 } 1190 #endif 1191 1192 void __consume_stateless_skb(struct sk_buff *skb); 1193 void __kfree_skb(struct sk_buff *skb); 1194 extern struct kmem_cache *skbuff_head_cache; 1195 1196 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen); 1197 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 1198 bool *fragstolen, int *delta_truesize); 1199 1200 struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags, 1201 int node); 1202 struct sk_buff *__build_skb(void *data, unsigned int frag_size); 1203 struct sk_buff *build_skb(void *data, unsigned int frag_size); 1204 struct sk_buff *build_skb_around(struct sk_buff *skb, 1205 void *data, unsigned int frag_size); 1206 1207 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size); 1208 1209 /** 1210 * alloc_skb - allocate a network buffer 1211 * @size: size to allocate 1212 * @priority: allocation mask 1213 * 1214 * This function is a convenient wrapper around __alloc_skb(). 1215 */ 1216 static inline struct sk_buff *alloc_skb(unsigned int size, 1217 gfp_t priority) 1218 { 1219 return __alloc_skb(size, priority, 0, NUMA_NO_NODE); 1220 } 1221 1222 struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 1223 unsigned long data_len, 1224 int max_page_order, 1225 int *errcode, 1226 gfp_t gfp_mask); 1227 struct sk_buff *alloc_skb_for_msg(struct sk_buff *first); 1228 1229 /* Layout of fast clones : [skb1][skb2][fclone_ref] */ 1230 struct sk_buff_fclones { 1231 struct sk_buff skb1; 1232 1233 struct sk_buff skb2; 1234 1235 refcount_t fclone_ref; 1236 }; 1237 1238 /** 1239 * skb_fclone_busy - check if fclone is busy 1240 * @sk: socket 1241 * @skb: buffer 1242 * 1243 * Returns true if skb is a fast clone, and its clone is not freed. 1244 * Some drivers call skb_orphan() in their ndo_start_xmit(), 1245 * so we also check that this didnt happen. 1246 */ 1247 static inline bool skb_fclone_busy(const struct sock *sk, 1248 const struct sk_buff *skb) 1249 { 1250 const struct sk_buff_fclones *fclones; 1251 1252 fclones = container_of(skb, struct sk_buff_fclones, skb1); 1253 1254 return skb->fclone == SKB_FCLONE_ORIG && 1255 refcount_read(&fclones->fclone_ref) > 1 && 1256 READ_ONCE(fclones->skb2.sk) == sk; 1257 } 1258 1259 /** 1260 * alloc_skb_fclone - allocate a network buffer from fclone cache 1261 * @size: size to allocate 1262 * @priority: allocation mask 1263 * 1264 * This function is a convenient wrapper around __alloc_skb(). 1265 */ 1266 static inline struct sk_buff *alloc_skb_fclone(unsigned int size, 1267 gfp_t priority) 1268 { 1269 return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE); 1270 } 1271 1272 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src); 1273 void skb_headers_offset_update(struct sk_buff *skb, int off); 1274 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask); 1275 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t priority); 1276 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old); 1277 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t priority); 1278 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1279 gfp_t gfp_mask, bool fclone); 1280 static inline struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, 1281 gfp_t gfp_mask) 1282 { 1283 return __pskb_copy_fclone(skb, headroom, gfp_mask, false); 1284 } 1285 1286 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, gfp_t gfp_mask); 1287 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, 1288 unsigned int headroom); 1289 struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom); 1290 struct sk_buff *skb_copy_expand(const struct sk_buff *skb, int newheadroom, 1291 int newtailroom, gfp_t priority); 1292 int __must_check skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 1293 int offset, int len); 1294 int __must_check skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, 1295 int offset, int len); 1296 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer); 1297 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error); 1298 1299 /** 1300 * skb_pad - zero pad the tail of an skb 1301 * @skb: buffer to pad 1302 * @pad: space to pad 1303 * 1304 * Ensure that a buffer is followed by a padding area that is zero 1305 * filled. Used by network drivers which may DMA or transfer data 1306 * beyond the buffer end onto the wire. 1307 * 1308 * May return error in out of memory cases. The skb is freed on error. 1309 */ 1310 static inline int skb_pad(struct sk_buff *skb, int pad) 1311 { 1312 return __skb_pad(skb, pad, true); 1313 } 1314 #define dev_kfree_skb(a) consume_skb(a) 1315 1316 int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 1317 int offset, size_t size); 1318 1319 struct skb_seq_state { 1320 __u32 lower_offset; 1321 __u32 upper_offset; 1322 __u32 frag_idx; 1323 __u32 stepped_offset; 1324 struct sk_buff *root_skb; 1325 struct sk_buff *cur_skb; 1326 __u8 *frag_data; 1327 __u32 frag_off; 1328 }; 1329 1330 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 1331 unsigned int to, struct skb_seq_state *st); 1332 unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 1333 struct skb_seq_state *st); 1334 void skb_abort_seq_read(struct skb_seq_state *st); 1335 1336 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 1337 unsigned int to, struct ts_config *config); 1338 1339 /* 1340 * Packet hash types specify the type of hash in skb_set_hash. 1341 * 1342 * Hash types refer to the protocol layer addresses which are used to 1343 * construct a packet's hash. The hashes are used to differentiate or identify 1344 * flows of the protocol layer for the hash type. Hash types are either 1345 * layer-2 (L2), layer-3 (L3), or layer-4 (L4). 1346 * 1347 * Properties of hashes: 1348 * 1349 * 1) Two packets in different flows have different hash values 1350 * 2) Two packets in the same flow should have the same hash value 1351 * 1352 * A hash at a higher layer is considered to be more specific. A driver should 1353 * set the most specific hash possible. 1354 * 1355 * A driver cannot indicate a more specific hash than the layer at which a hash 1356 * was computed. For instance an L3 hash cannot be set as an L4 hash. 1357 * 1358 * A driver may indicate a hash level which is less specific than the 1359 * actual layer the hash was computed on. For instance, a hash computed 1360 * at L4 may be considered an L3 hash. This should only be done if the 1361 * driver can't unambiguously determine that the HW computed the hash at 1362 * the higher layer. Note that the "should" in the second property above 1363 * permits this. 1364 */ 1365 enum pkt_hash_types { 1366 PKT_HASH_TYPE_NONE, /* Undefined type */ 1367 PKT_HASH_TYPE_L2, /* Input: src_MAC, dest_MAC */ 1368 PKT_HASH_TYPE_L3, /* Input: src_IP, dst_IP */ 1369 PKT_HASH_TYPE_L4, /* Input: src_IP, dst_IP, src_port, dst_port */ 1370 }; 1371 1372 static inline void skb_clear_hash(struct sk_buff *skb) 1373 { 1374 skb->hash = 0; 1375 skb->sw_hash = 0; 1376 skb->l4_hash = 0; 1377 } 1378 1379 static inline void skb_clear_hash_if_not_l4(struct sk_buff *skb) 1380 { 1381 if (!skb->l4_hash) 1382 skb_clear_hash(skb); 1383 } 1384 1385 static inline void 1386 __skb_set_hash(struct sk_buff *skb, __u32 hash, bool is_sw, bool is_l4) 1387 { 1388 skb->l4_hash = is_l4; 1389 skb->sw_hash = is_sw; 1390 skb->hash = hash; 1391 } 1392 1393 static inline void 1394 skb_set_hash(struct sk_buff *skb, __u32 hash, enum pkt_hash_types type) 1395 { 1396 /* Used by drivers to set hash from HW */ 1397 __skb_set_hash(skb, hash, false, type == PKT_HASH_TYPE_L4); 1398 } 1399 1400 static inline void 1401 __skb_set_sw_hash(struct sk_buff *skb, __u32 hash, bool is_l4) 1402 { 1403 __skb_set_hash(skb, hash, true, is_l4); 1404 } 1405 1406 void __skb_get_hash(struct sk_buff *skb); 1407 u32 __skb_get_hash_symmetric(const struct sk_buff *skb); 1408 u32 skb_get_poff(const struct sk_buff *skb); 1409 u32 __skb_get_poff(const struct sk_buff *skb, const void *data, 1410 const struct flow_keys_basic *keys, int hlen); 1411 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, 1412 const void *data, int hlen_proto); 1413 1414 static inline __be32 skb_flow_get_ports(const struct sk_buff *skb, 1415 int thoff, u8 ip_proto) 1416 { 1417 return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0); 1418 } 1419 1420 void skb_flow_dissector_init(struct flow_dissector *flow_dissector, 1421 const struct flow_dissector_key *key, 1422 unsigned int key_count); 1423 1424 struct bpf_flow_dissector; 1425 bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx, 1426 __be16 proto, int nhoff, int hlen, unsigned int flags); 1427 1428 bool __skb_flow_dissect(const struct net *net, 1429 const struct sk_buff *skb, 1430 struct flow_dissector *flow_dissector, 1431 void *target_container, const void *data, 1432 __be16 proto, int nhoff, int hlen, unsigned int flags); 1433 1434 static inline bool skb_flow_dissect(const struct sk_buff *skb, 1435 struct flow_dissector *flow_dissector, 1436 void *target_container, unsigned int flags) 1437 { 1438 return __skb_flow_dissect(NULL, skb, flow_dissector, 1439 target_container, NULL, 0, 0, 0, flags); 1440 } 1441 1442 static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb, 1443 struct flow_keys *flow, 1444 unsigned int flags) 1445 { 1446 memset(flow, 0, sizeof(*flow)); 1447 return __skb_flow_dissect(NULL, skb, &flow_keys_dissector, 1448 flow, NULL, 0, 0, 0, flags); 1449 } 1450 1451 static inline bool 1452 skb_flow_dissect_flow_keys_basic(const struct net *net, 1453 const struct sk_buff *skb, 1454 struct flow_keys_basic *flow, 1455 const void *data, __be16 proto, 1456 int nhoff, int hlen, unsigned int flags) 1457 { 1458 memset(flow, 0, sizeof(*flow)); 1459 return __skb_flow_dissect(net, skb, &flow_keys_basic_dissector, flow, 1460 data, proto, nhoff, hlen, flags); 1461 } 1462 1463 void skb_flow_dissect_meta(const struct sk_buff *skb, 1464 struct flow_dissector *flow_dissector, 1465 void *target_container); 1466 1467 /* Gets a skb connection tracking info, ctinfo map should be a 1468 * map of mapsize to translate enum ip_conntrack_info states 1469 * to user states. 1470 */ 1471 void 1472 skb_flow_dissect_ct(const struct sk_buff *skb, 1473 struct flow_dissector *flow_dissector, 1474 void *target_container, 1475 u16 *ctinfo_map, size_t mapsize, 1476 bool post_ct, u16 zone); 1477 void 1478 skb_flow_dissect_tunnel_info(const struct sk_buff *skb, 1479 struct flow_dissector *flow_dissector, 1480 void *target_container); 1481 1482 void skb_flow_dissect_hash(const struct sk_buff *skb, 1483 struct flow_dissector *flow_dissector, 1484 void *target_container); 1485 1486 static inline __u32 skb_get_hash(struct sk_buff *skb) 1487 { 1488 if (!skb->l4_hash && !skb->sw_hash) 1489 __skb_get_hash(skb); 1490 1491 return skb->hash; 1492 } 1493 1494 static inline __u32 skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6) 1495 { 1496 if (!skb->l4_hash && !skb->sw_hash) { 1497 struct flow_keys keys; 1498 __u32 hash = __get_hash_from_flowi6(fl6, &keys); 1499 1500 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys)); 1501 } 1502 1503 return skb->hash; 1504 } 1505 1506 __u32 skb_get_hash_perturb(const struct sk_buff *skb, 1507 const siphash_key_t *perturb); 1508 1509 static inline __u32 skb_get_hash_raw(const struct sk_buff *skb) 1510 { 1511 return skb->hash; 1512 } 1513 1514 static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from) 1515 { 1516 to->hash = from->hash; 1517 to->sw_hash = from->sw_hash; 1518 to->l4_hash = from->l4_hash; 1519 }; 1520 1521 static inline void skb_copy_decrypted(struct sk_buff *to, 1522 const struct sk_buff *from) 1523 { 1524 #ifdef CONFIG_TLS_DEVICE 1525 to->decrypted = from->decrypted; 1526 #endif 1527 } 1528 1529 #ifdef NET_SKBUFF_DATA_USES_OFFSET 1530 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb) 1531 { 1532 return skb->head + skb->end; 1533 } 1534 1535 static inline unsigned int skb_end_offset(const struct sk_buff *skb) 1536 { 1537 return skb->end; 1538 } 1539 #else 1540 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb) 1541 { 1542 return skb->end; 1543 } 1544 1545 static inline unsigned int skb_end_offset(const struct sk_buff *skb) 1546 { 1547 return skb->end - skb->head; 1548 } 1549 #endif 1550 1551 /* Internal */ 1552 #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB))) 1553 1554 static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb) 1555 { 1556 return &skb_shinfo(skb)->hwtstamps; 1557 } 1558 1559 static inline struct ubuf_info *skb_zcopy(struct sk_buff *skb) 1560 { 1561 bool is_zcopy = skb && skb_shinfo(skb)->flags & SKBFL_ZEROCOPY_ENABLE; 1562 1563 return is_zcopy ? skb_uarg(skb) : NULL; 1564 } 1565 1566 static inline bool skb_zcopy_pure(const struct sk_buff *skb) 1567 { 1568 return skb_shinfo(skb)->flags & SKBFL_PURE_ZEROCOPY; 1569 } 1570 1571 static inline bool skb_pure_zcopy_same(const struct sk_buff *skb1, 1572 const struct sk_buff *skb2) 1573 { 1574 return skb_zcopy_pure(skb1) == skb_zcopy_pure(skb2); 1575 } 1576 1577 static inline void net_zcopy_get(struct ubuf_info *uarg) 1578 { 1579 refcount_inc(&uarg->refcnt); 1580 } 1581 1582 static inline void skb_zcopy_init(struct sk_buff *skb, struct ubuf_info *uarg) 1583 { 1584 skb_shinfo(skb)->destructor_arg = uarg; 1585 skb_shinfo(skb)->flags |= uarg->flags; 1586 } 1587 1588 static inline void skb_zcopy_set(struct sk_buff *skb, struct ubuf_info *uarg, 1589 bool *have_ref) 1590 { 1591 if (skb && uarg && !skb_zcopy(skb)) { 1592 if (unlikely(have_ref && *have_ref)) 1593 *have_ref = false; 1594 else 1595 net_zcopy_get(uarg); 1596 skb_zcopy_init(skb, uarg); 1597 } 1598 } 1599 1600 static inline void skb_zcopy_set_nouarg(struct sk_buff *skb, void *val) 1601 { 1602 skb_shinfo(skb)->destructor_arg = (void *)((uintptr_t) val | 0x1UL); 1603 skb_shinfo(skb)->flags |= SKBFL_ZEROCOPY_FRAG; 1604 } 1605 1606 static inline bool skb_zcopy_is_nouarg(struct sk_buff *skb) 1607 { 1608 return (uintptr_t) skb_shinfo(skb)->destructor_arg & 0x1UL; 1609 } 1610 1611 static inline void *skb_zcopy_get_nouarg(struct sk_buff *skb) 1612 { 1613 return (void *)((uintptr_t) skb_shinfo(skb)->destructor_arg & ~0x1UL); 1614 } 1615 1616 static inline void net_zcopy_put(struct ubuf_info *uarg) 1617 { 1618 if (uarg) 1619 uarg->callback(NULL, uarg, true); 1620 } 1621 1622 static inline void net_zcopy_put_abort(struct ubuf_info *uarg, bool have_uref) 1623 { 1624 if (uarg) { 1625 if (uarg->callback == msg_zerocopy_callback) 1626 msg_zerocopy_put_abort(uarg, have_uref); 1627 else if (have_uref) 1628 net_zcopy_put(uarg); 1629 } 1630 } 1631 1632 /* Release a reference on a zerocopy structure */ 1633 static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy_success) 1634 { 1635 struct ubuf_info *uarg = skb_zcopy(skb); 1636 1637 if (uarg) { 1638 if (!skb_zcopy_is_nouarg(skb)) 1639 uarg->callback(skb, uarg, zerocopy_success); 1640 1641 skb_shinfo(skb)->flags &= ~SKBFL_ALL_ZEROCOPY; 1642 } 1643 } 1644 1645 static inline void skb_mark_not_on_list(struct sk_buff *skb) 1646 { 1647 skb->next = NULL; 1648 } 1649 1650 /* Iterate through singly-linked GSO fragments of an skb. */ 1651 #define skb_list_walk_safe(first, skb, next_skb) \ 1652 for ((skb) = (first), (next_skb) = (skb) ? (skb)->next : NULL; (skb); \ 1653 (skb) = (next_skb), (next_skb) = (skb) ? (skb)->next : NULL) 1654 1655 static inline void skb_list_del_init(struct sk_buff *skb) 1656 { 1657 __list_del_entry(&skb->list); 1658 skb_mark_not_on_list(skb); 1659 } 1660 1661 /** 1662 * skb_queue_empty - check if a queue is empty 1663 * @list: queue head 1664 * 1665 * Returns true if the queue is empty, false otherwise. 1666 */ 1667 static inline int skb_queue_empty(const struct sk_buff_head *list) 1668 { 1669 return list->next == (const struct sk_buff *) list; 1670 } 1671 1672 /** 1673 * skb_queue_empty_lockless - check if a queue is empty 1674 * @list: queue head 1675 * 1676 * Returns true if the queue is empty, false otherwise. 1677 * This variant can be used in lockless contexts. 1678 */ 1679 static inline bool skb_queue_empty_lockless(const struct sk_buff_head *list) 1680 { 1681 return READ_ONCE(list->next) == (const struct sk_buff *) list; 1682 } 1683 1684 1685 /** 1686 * skb_queue_is_last - check if skb is the last entry in the queue 1687 * @list: queue head 1688 * @skb: buffer 1689 * 1690 * Returns true if @skb is the last buffer on the list. 1691 */ 1692 static inline bool skb_queue_is_last(const struct sk_buff_head *list, 1693 const struct sk_buff *skb) 1694 { 1695 return skb->next == (const struct sk_buff *) list; 1696 } 1697 1698 /** 1699 * skb_queue_is_first - check if skb is the first entry in the queue 1700 * @list: queue head 1701 * @skb: buffer 1702 * 1703 * Returns true if @skb is the first buffer on the list. 1704 */ 1705 static inline bool skb_queue_is_first(const struct sk_buff_head *list, 1706 const struct sk_buff *skb) 1707 { 1708 return skb->prev == (const struct sk_buff *) list; 1709 } 1710 1711 /** 1712 * skb_queue_next - return the next packet in the queue 1713 * @list: queue head 1714 * @skb: current buffer 1715 * 1716 * Return the next packet in @list after @skb. It is only valid to 1717 * call this if skb_queue_is_last() evaluates to false. 1718 */ 1719 static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list, 1720 const struct sk_buff *skb) 1721 { 1722 /* This BUG_ON may seem severe, but if we just return then we 1723 * are going to dereference garbage. 1724 */ 1725 BUG_ON(skb_queue_is_last(list, skb)); 1726 return skb->next; 1727 } 1728 1729 /** 1730 * skb_queue_prev - return the prev packet in the queue 1731 * @list: queue head 1732 * @skb: current buffer 1733 * 1734 * Return the prev packet in @list before @skb. It is only valid to 1735 * call this if skb_queue_is_first() evaluates to false. 1736 */ 1737 static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list, 1738 const struct sk_buff *skb) 1739 { 1740 /* This BUG_ON may seem severe, but if we just return then we 1741 * are going to dereference garbage. 1742 */ 1743 BUG_ON(skb_queue_is_first(list, skb)); 1744 return skb->prev; 1745 } 1746 1747 /** 1748 * skb_get - reference buffer 1749 * @skb: buffer to reference 1750 * 1751 * Makes another reference to a socket buffer and returns a pointer 1752 * to the buffer. 1753 */ 1754 static inline struct sk_buff *skb_get(struct sk_buff *skb) 1755 { 1756 refcount_inc(&skb->users); 1757 return skb; 1758 } 1759 1760 /* 1761 * If users == 1, we are the only owner and can avoid redundant atomic changes. 1762 */ 1763 1764 /** 1765 * skb_cloned - is the buffer a clone 1766 * @skb: buffer to check 1767 * 1768 * Returns true if the buffer was generated with skb_clone() and is 1769 * one of multiple shared copies of the buffer. Cloned buffers are 1770 * shared data so must not be written to under normal circumstances. 1771 */ 1772 static inline int skb_cloned(const struct sk_buff *skb) 1773 { 1774 return skb->cloned && 1775 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1; 1776 } 1777 1778 static inline int skb_unclone(struct sk_buff *skb, gfp_t pri) 1779 { 1780 might_sleep_if(gfpflags_allow_blocking(pri)); 1781 1782 if (skb_cloned(skb)) 1783 return pskb_expand_head(skb, 0, 0, pri); 1784 1785 return 0; 1786 } 1787 1788 /* This variant of skb_unclone() makes sure skb->truesize is not changed */ 1789 static inline int skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 1790 { 1791 might_sleep_if(gfpflags_allow_blocking(pri)); 1792 1793 if (skb_cloned(skb)) { 1794 unsigned int save = skb->truesize; 1795 int res; 1796 1797 res = pskb_expand_head(skb, 0, 0, pri); 1798 skb->truesize = save; 1799 return res; 1800 } 1801 return 0; 1802 } 1803 1804 /** 1805 * skb_header_cloned - is the header a clone 1806 * @skb: buffer to check 1807 * 1808 * Returns true if modifying the header part of the buffer requires 1809 * the data to be copied. 1810 */ 1811 static inline int skb_header_cloned(const struct sk_buff *skb) 1812 { 1813 int dataref; 1814 1815 if (!skb->cloned) 1816 return 0; 1817 1818 dataref = atomic_read(&skb_shinfo(skb)->dataref); 1819 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT); 1820 return dataref != 1; 1821 } 1822 1823 static inline int skb_header_unclone(struct sk_buff *skb, gfp_t pri) 1824 { 1825 might_sleep_if(gfpflags_allow_blocking(pri)); 1826 1827 if (skb_header_cloned(skb)) 1828 return pskb_expand_head(skb, 0, 0, pri); 1829 1830 return 0; 1831 } 1832 1833 /** 1834 * __skb_header_release - release reference to header 1835 * @skb: buffer to operate on 1836 */ 1837 static inline void __skb_header_release(struct sk_buff *skb) 1838 { 1839 skb->nohdr = 1; 1840 atomic_set(&skb_shinfo(skb)->dataref, 1 + (1 << SKB_DATAREF_SHIFT)); 1841 } 1842 1843 1844 /** 1845 * skb_shared - is the buffer shared 1846 * @skb: buffer to check 1847 * 1848 * Returns true if more than one person has a reference to this 1849 * buffer. 1850 */ 1851 static inline int skb_shared(const struct sk_buff *skb) 1852 { 1853 return refcount_read(&skb->users) != 1; 1854 } 1855 1856 /** 1857 * skb_share_check - check if buffer is shared and if so clone it 1858 * @skb: buffer to check 1859 * @pri: priority for memory allocation 1860 * 1861 * If the buffer is shared the buffer is cloned and the old copy 1862 * drops a reference. A new clone with a single reference is returned. 1863 * If the buffer is not shared the original buffer is returned. When 1864 * being called from interrupt status or with spinlocks held pri must 1865 * be GFP_ATOMIC. 1866 * 1867 * NULL is returned on a memory allocation failure. 1868 */ 1869 static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri) 1870 { 1871 might_sleep_if(gfpflags_allow_blocking(pri)); 1872 if (skb_shared(skb)) { 1873 struct sk_buff *nskb = skb_clone(skb, pri); 1874 1875 if (likely(nskb)) 1876 consume_skb(skb); 1877 else 1878 kfree_skb(skb); 1879 skb = nskb; 1880 } 1881 return skb; 1882 } 1883 1884 /* 1885 * Copy shared buffers into a new sk_buff. We effectively do COW on 1886 * packets to handle cases where we have a local reader and forward 1887 * and a couple of other messy ones. The normal one is tcpdumping 1888 * a packet thats being forwarded. 1889 */ 1890 1891 /** 1892 * skb_unshare - make a copy of a shared buffer 1893 * @skb: buffer to check 1894 * @pri: priority for memory allocation 1895 * 1896 * If the socket buffer is a clone then this function creates a new 1897 * copy of the data, drops a reference count on the old copy and returns 1898 * the new copy with the reference count at 1. If the buffer is not a clone 1899 * the original buffer is returned. When called with a spinlock held or 1900 * from interrupt state @pri must be %GFP_ATOMIC 1901 * 1902 * %NULL is returned on a memory allocation failure. 1903 */ 1904 static inline struct sk_buff *skb_unshare(struct sk_buff *skb, 1905 gfp_t pri) 1906 { 1907 might_sleep_if(gfpflags_allow_blocking(pri)); 1908 if (skb_cloned(skb)) { 1909 struct sk_buff *nskb = skb_copy(skb, pri); 1910 1911 /* Free our shared copy */ 1912 if (likely(nskb)) 1913 consume_skb(skb); 1914 else 1915 kfree_skb(skb); 1916 skb = nskb; 1917 } 1918 return skb; 1919 } 1920 1921 /** 1922 * skb_peek - peek at the head of an &sk_buff_head 1923 * @list_: list to peek at 1924 * 1925 * Peek an &sk_buff. Unlike most other operations you _MUST_ 1926 * be careful with this one. A peek leaves the buffer on the 1927 * list and someone else may run off with it. You must hold 1928 * the appropriate locks or have a private queue to do this. 1929 * 1930 * Returns %NULL for an empty list or a pointer to the head element. 1931 * The reference count is not incremented and the reference is therefore 1932 * volatile. Use with caution. 1933 */ 1934 static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_) 1935 { 1936 struct sk_buff *skb = list_->next; 1937 1938 if (skb == (struct sk_buff *)list_) 1939 skb = NULL; 1940 return skb; 1941 } 1942 1943 /** 1944 * __skb_peek - peek at the head of a non-empty &sk_buff_head 1945 * @list_: list to peek at 1946 * 1947 * Like skb_peek(), but the caller knows that the list is not empty. 1948 */ 1949 static inline struct sk_buff *__skb_peek(const struct sk_buff_head *list_) 1950 { 1951 return list_->next; 1952 } 1953 1954 /** 1955 * skb_peek_next - peek skb following the given one from a queue 1956 * @skb: skb to start from 1957 * @list_: list to peek at 1958 * 1959 * Returns %NULL when the end of the list is met or a pointer to the 1960 * next element. The reference count is not incremented and the 1961 * reference is therefore volatile. Use with caution. 1962 */ 1963 static inline struct sk_buff *skb_peek_next(struct sk_buff *skb, 1964 const struct sk_buff_head *list_) 1965 { 1966 struct sk_buff *next = skb->next; 1967 1968 if (next == (struct sk_buff *)list_) 1969 next = NULL; 1970 return next; 1971 } 1972 1973 /** 1974 * skb_peek_tail - peek at the tail of an &sk_buff_head 1975 * @list_: list to peek at 1976 * 1977 * Peek an &sk_buff. Unlike most other operations you _MUST_ 1978 * be careful with this one. A peek leaves the buffer on the 1979 * list and someone else may run off with it. You must hold 1980 * the appropriate locks or have a private queue to do this. 1981 * 1982 * Returns %NULL for an empty list or a pointer to the tail element. 1983 * The reference count is not incremented and the reference is therefore 1984 * volatile. Use with caution. 1985 */ 1986 static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_) 1987 { 1988 struct sk_buff *skb = READ_ONCE(list_->prev); 1989 1990 if (skb == (struct sk_buff *)list_) 1991 skb = NULL; 1992 return skb; 1993 1994 } 1995 1996 /** 1997 * skb_queue_len - get queue length 1998 * @list_: list to measure 1999 * 2000 * Return the length of an &sk_buff queue. 2001 */ 2002 static inline __u32 skb_queue_len(const struct sk_buff_head *list_) 2003 { 2004 return list_->qlen; 2005 } 2006 2007 /** 2008 * skb_queue_len_lockless - get queue length 2009 * @list_: list to measure 2010 * 2011 * Return the length of an &sk_buff queue. 2012 * This variant can be used in lockless contexts. 2013 */ 2014 static inline __u32 skb_queue_len_lockless(const struct sk_buff_head *list_) 2015 { 2016 return READ_ONCE(list_->qlen); 2017 } 2018 2019 /** 2020 * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head 2021 * @list: queue to initialize 2022 * 2023 * This initializes only the list and queue length aspects of 2024 * an sk_buff_head object. This allows to initialize the list 2025 * aspects of an sk_buff_head without reinitializing things like 2026 * the spinlock. It can also be used for on-stack sk_buff_head 2027 * objects where the spinlock is known to not be used. 2028 */ 2029 static inline void __skb_queue_head_init(struct sk_buff_head *list) 2030 { 2031 list->prev = list->next = (struct sk_buff *)list; 2032 list->qlen = 0; 2033 } 2034 2035 /* 2036 * This function creates a split out lock class for each invocation; 2037 * this is needed for now since a whole lot of users of the skb-queue 2038 * infrastructure in drivers have different locking usage (in hardirq) 2039 * than the networking core (in softirq only). In the long run either the 2040 * network layer or drivers should need annotation to consolidate the 2041 * main types of usage into 3 classes. 2042 */ 2043 static inline void skb_queue_head_init(struct sk_buff_head *list) 2044 { 2045 spin_lock_init(&list->lock); 2046 __skb_queue_head_init(list); 2047 } 2048 2049 static inline void skb_queue_head_init_class(struct sk_buff_head *list, 2050 struct lock_class_key *class) 2051 { 2052 skb_queue_head_init(list); 2053 lockdep_set_class(&list->lock, class); 2054 } 2055 2056 /* 2057 * Insert an sk_buff on a list. 2058 * 2059 * The "__skb_xxxx()" functions are the non-atomic ones that 2060 * can only be called with interrupts disabled. 2061 */ 2062 static inline void __skb_insert(struct sk_buff *newsk, 2063 struct sk_buff *prev, struct sk_buff *next, 2064 struct sk_buff_head *list) 2065 { 2066 /* See skb_queue_empty_lockless() and skb_peek_tail() 2067 * for the opposite READ_ONCE() 2068 */ 2069 WRITE_ONCE(newsk->next, next); 2070 WRITE_ONCE(newsk->prev, prev); 2071 WRITE_ONCE(((struct sk_buff_list *)next)->prev, newsk); 2072 WRITE_ONCE(((struct sk_buff_list *)prev)->next, newsk); 2073 WRITE_ONCE(list->qlen, list->qlen + 1); 2074 } 2075 2076 static inline void __skb_queue_splice(const struct sk_buff_head *list, 2077 struct sk_buff *prev, 2078 struct sk_buff *next) 2079 { 2080 struct sk_buff *first = list->next; 2081 struct sk_buff *last = list->prev; 2082 2083 WRITE_ONCE(first->prev, prev); 2084 WRITE_ONCE(prev->next, first); 2085 2086 WRITE_ONCE(last->next, next); 2087 WRITE_ONCE(next->prev, last); 2088 } 2089 2090 /** 2091 * skb_queue_splice - join two skb lists, this is designed for stacks 2092 * @list: the new list to add 2093 * @head: the place to add it in the first list 2094 */ 2095 static inline void skb_queue_splice(const struct sk_buff_head *list, 2096 struct sk_buff_head *head) 2097 { 2098 if (!skb_queue_empty(list)) { 2099 __skb_queue_splice(list, (struct sk_buff *) head, head->next); 2100 head->qlen += list->qlen; 2101 } 2102 } 2103 2104 /** 2105 * skb_queue_splice_init - join two skb lists and reinitialise the emptied list 2106 * @list: the new list to add 2107 * @head: the place to add it in the first list 2108 * 2109 * The list at @list is reinitialised 2110 */ 2111 static inline void skb_queue_splice_init(struct sk_buff_head *list, 2112 struct sk_buff_head *head) 2113 { 2114 if (!skb_queue_empty(list)) { 2115 __skb_queue_splice(list, (struct sk_buff *) head, head->next); 2116 head->qlen += list->qlen; 2117 __skb_queue_head_init(list); 2118 } 2119 } 2120 2121 /** 2122 * skb_queue_splice_tail - join two skb lists, each list being a queue 2123 * @list: the new list to add 2124 * @head: the place to add it in the first list 2125 */ 2126 static inline void skb_queue_splice_tail(const struct sk_buff_head *list, 2127 struct sk_buff_head *head) 2128 { 2129 if (!skb_queue_empty(list)) { 2130 __skb_queue_splice(list, head->prev, (struct sk_buff *) head); 2131 head->qlen += list->qlen; 2132 } 2133 } 2134 2135 /** 2136 * skb_queue_splice_tail_init - join two skb lists and reinitialise the emptied list 2137 * @list: the new list to add 2138 * @head: the place to add it in the first list 2139 * 2140 * Each of the lists is a queue. 2141 * The list at @list is reinitialised 2142 */ 2143 static inline void skb_queue_splice_tail_init(struct sk_buff_head *list, 2144 struct sk_buff_head *head) 2145 { 2146 if (!skb_queue_empty(list)) { 2147 __skb_queue_splice(list, head->prev, (struct sk_buff *) head); 2148 head->qlen += list->qlen; 2149 __skb_queue_head_init(list); 2150 } 2151 } 2152 2153 /** 2154 * __skb_queue_after - queue a buffer at the list head 2155 * @list: list to use 2156 * @prev: place after this buffer 2157 * @newsk: buffer to queue 2158 * 2159 * Queue a buffer int the middle of a list. This function takes no locks 2160 * and you must therefore hold required locks before calling it. 2161 * 2162 * A buffer cannot be placed on two lists at the same time. 2163 */ 2164 static inline void __skb_queue_after(struct sk_buff_head *list, 2165 struct sk_buff *prev, 2166 struct sk_buff *newsk) 2167 { 2168 __skb_insert(newsk, prev, ((struct sk_buff_list *)prev)->next, list); 2169 } 2170 2171 void skb_append(struct sk_buff *old, struct sk_buff *newsk, 2172 struct sk_buff_head *list); 2173 2174 static inline void __skb_queue_before(struct sk_buff_head *list, 2175 struct sk_buff *next, 2176 struct sk_buff *newsk) 2177 { 2178 __skb_insert(newsk, ((struct sk_buff_list *)next)->prev, next, list); 2179 } 2180 2181 /** 2182 * __skb_queue_head - queue a buffer at the list head 2183 * @list: list to use 2184 * @newsk: buffer to queue 2185 * 2186 * Queue a buffer at the start of a list. This function takes no locks 2187 * and you must therefore hold required locks before calling it. 2188 * 2189 * A buffer cannot be placed on two lists at the same time. 2190 */ 2191 static inline void __skb_queue_head(struct sk_buff_head *list, 2192 struct sk_buff *newsk) 2193 { 2194 __skb_queue_after(list, (struct sk_buff *)list, newsk); 2195 } 2196 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk); 2197 2198 /** 2199 * __skb_queue_tail - queue a buffer at the list tail 2200 * @list: list to use 2201 * @newsk: buffer to queue 2202 * 2203 * Queue a buffer at the end of a list. This function takes no locks 2204 * and you must therefore hold required locks before calling it. 2205 * 2206 * A buffer cannot be placed on two lists at the same time. 2207 */ 2208 static inline void __skb_queue_tail(struct sk_buff_head *list, 2209 struct sk_buff *newsk) 2210 { 2211 __skb_queue_before(list, (struct sk_buff *)list, newsk); 2212 } 2213 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk); 2214 2215 /* 2216 * remove sk_buff from list. _Must_ be called atomically, and with 2217 * the list known.. 2218 */ 2219 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list); 2220 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 2221 { 2222 struct sk_buff *next, *prev; 2223 2224 WRITE_ONCE(list->qlen, list->qlen - 1); 2225 next = skb->next; 2226 prev = skb->prev; 2227 skb->next = skb->prev = NULL; 2228 WRITE_ONCE(next->prev, prev); 2229 WRITE_ONCE(prev->next, next); 2230 } 2231 2232 /** 2233 * __skb_dequeue - remove from the head of the queue 2234 * @list: list to dequeue from 2235 * 2236 * Remove the head of the list. This function does not take any locks 2237 * so must be used with appropriate locks held only. The head item is 2238 * returned or %NULL if the list is empty. 2239 */ 2240 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list) 2241 { 2242 struct sk_buff *skb = skb_peek(list); 2243 if (skb) 2244 __skb_unlink(skb, list); 2245 return skb; 2246 } 2247 struct sk_buff *skb_dequeue(struct sk_buff_head *list); 2248 2249 /** 2250 * __skb_dequeue_tail - remove from the tail of the queue 2251 * @list: list to dequeue from 2252 * 2253 * Remove the tail of the list. This function does not take any locks 2254 * so must be used with appropriate locks held only. The tail item is 2255 * returned or %NULL if the list is empty. 2256 */ 2257 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list) 2258 { 2259 struct sk_buff *skb = skb_peek_tail(list); 2260 if (skb) 2261 __skb_unlink(skb, list); 2262 return skb; 2263 } 2264 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list); 2265 2266 2267 static inline bool skb_is_nonlinear(const struct sk_buff *skb) 2268 { 2269 return skb->data_len; 2270 } 2271 2272 static inline unsigned int skb_headlen(const struct sk_buff *skb) 2273 { 2274 return skb->len - skb->data_len; 2275 } 2276 2277 static inline unsigned int __skb_pagelen(const struct sk_buff *skb) 2278 { 2279 unsigned int i, len = 0; 2280 2281 for (i = skb_shinfo(skb)->nr_frags - 1; (int)i >= 0; i--) 2282 len += skb_frag_size(&skb_shinfo(skb)->frags[i]); 2283 return len; 2284 } 2285 2286 static inline unsigned int skb_pagelen(const struct sk_buff *skb) 2287 { 2288 return skb_headlen(skb) + __skb_pagelen(skb); 2289 } 2290 2291 /** 2292 * __skb_fill_page_desc - initialise a paged fragment in an skb 2293 * @skb: buffer containing fragment to be initialised 2294 * @i: paged fragment index to initialise 2295 * @page: the page to use for this fragment 2296 * @off: the offset to the data with @page 2297 * @size: the length of the data 2298 * 2299 * Initialises the @i'th fragment of @skb to point to &size bytes at 2300 * offset @off within @page. 2301 * 2302 * Does not take any additional reference on the fragment. 2303 */ 2304 static inline void __skb_fill_page_desc(struct sk_buff *skb, int i, 2305 struct page *page, int off, int size) 2306 { 2307 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2308 2309 /* 2310 * Propagate page pfmemalloc to the skb if we can. The problem is 2311 * that not all callers have unique ownership of the page but rely 2312 * on page_is_pfmemalloc doing the right thing(tm). 2313 */ 2314 frag->bv_page = page; 2315 frag->bv_offset = off; 2316 skb_frag_size_set(frag, size); 2317 2318 page = compound_head(page); 2319 if (page_is_pfmemalloc(page)) 2320 skb->pfmemalloc = true; 2321 } 2322 2323 /** 2324 * skb_fill_page_desc - initialise a paged fragment in an skb 2325 * @skb: buffer containing fragment to be initialised 2326 * @i: paged fragment index to initialise 2327 * @page: the page to use for this fragment 2328 * @off: the offset to the data with @page 2329 * @size: the length of the data 2330 * 2331 * As per __skb_fill_page_desc() -- initialises the @i'th fragment of 2332 * @skb to point to @size bytes at offset @off within @page. In 2333 * addition updates @skb such that @i is the last fragment. 2334 * 2335 * Does not take any additional reference on the fragment. 2336 */ 2337 static inline void skb_fill_page_desc(struct sk_buff *skb, int i, 2338 struct page *page, int off, int size) 2339 { 2340 __skb_fill_page_desc(skb, i, page, off, size); 2341 skb_shinfo(skb)->nr_frags = i + 1; 2342 } 2343 2344 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 2345 int size, unsigned int truesize); 2346 2347 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 2348 unsigned int truesize); 2349 2350 #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb)) 2351 2352 #ifdef NET_SKBUFF_DATA_USES_OFFSET 2353 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb) 2354 { 2355 return skb->head + skb->tail; 2356 } 2357 2358 static inline void skb_reset_tail_pointer(struct sk_buff *skb) 2359 { 2360 skb->tail = skb->data - skb->head; 2361 } 2362 2363 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset) 2364 { 2365 skb_reset_tail_pointer(skb); 2366 skb->tail += offset; 2367 } 2368 2369 #else /* NET_SKBUFF_DATA_USES_OFFSET */ 2370 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb) 2371 { 2372 return skb->tail; 2373 } 2374 2375 static inline void skb_reset_tail_pointer(struct sk_buff *skb) 2376 { 2377 skb->tail = skb->data; 2378 } 2379 2380 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset) 2381 { 2382 skb->tail = skb->data + offset; 2383 } 2384 2385 #endif /* NET_SKBUFF_DATA_USES_OFFSET */ 2386 2387 /* 2388 * Add data to an sk_buff 2389 */ 2390 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len); 2391 void *skb_put(struct sk_buff *skb, unsigned int len); 2392 static inline void *__skb_put(struct sk_buff *skb, unsigned int len) 2393 { 2394 void *tmp = skb_tail_pointer(skb); 2395 SKB_LINEAR_ASSERT(skb); 2396 skb->tail += len; 2397 skb->len += len; 2398 return tmp; 2399 } 2400 2401 static inline void *__skb_put_zero(struct sk_buff *skb, unsigned int len) 2402 { 2403 void *tmp = __skb_put(skb, len); 2404 2405 memset(tmp, 0, len); 2406 return tmp; 2407 } 2408 2409 static inline void *__skb_put_data(struct sk_buff *skb, const void *data, 2410 unsigned int len) 2411 { 2412 void *tmp = __skb_put(skb, len); 2413 2414 memcpy(tmp, data, len); 2415 return tmp; 2416 } 2417 2418 static inline void __skb_put_u8(struct sk_buff *skb, u8 val) 2419 { 2420 *(u8 *)__skb_put(skb, 1) = val; 2421 } 2422 2423 static inline void *skb_put_zero(struct sk_buff *skb, unsigned int len) 2424 { 2425 void *tmp = skb_put(skb, len); 2426 2427 memset(tmp, 0, len); 2428 2429 return tmp; 2430 } 2431 2432 static inline void *skb_put_data(struct sk_buff *skb, const void *data, 2433 unsigned int len) 2434 { 2435 void *tmp = skb_put(skb, len); 2436 2437 memcpy(tmp, data, len); 2438 2439 return tmp; 2440 } 2441 2442 static inline void skb_put_u8(struct sk_buff *skb, u8 val) 2443 { 2444 *(u8 *)skb_put(skb, 1) = val; 2445 } 2446 2447 void *skb_push(struct sk_buff *skb, unsigned int len); 2448 static inline void *__skb_push(struct sk_buff *skb, unsigned int len) 2449 { 2450 skb->data -= len; 2451 skb->len += len; 2452 return skb->data; 2453 } 2454 2455 void *skb_pull(struct sk_buff *skb, unsigned int len); 2456 static inline void *__skb_pull(struct sk_buff *skb, unsigned int len) 2457 { 2458 skb->len -= len; 2459 BUG_ON(skb->len < skb->data_len); 2460 return skb->data += len; 2461 } 2462 2463 static inline void *skb_pull_inline(struct sk_buff *skb, unsigned int len) 2464 { 2465 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len); 2466 } 2467 2468 void *skb_pull_data(struct sk_buff *skb, size_t len); 2469 2470 void *__pskb_pull_tail(struct sk_buff *skb, int delta); 2471 2472 static inline void *__pskb_pull(struct sk_buff *skb, unsigned int len) 2473 { 2474 if (len > skb_headlen(skb) && 2475 !__pskb_pull_tail(skb, len - skb_headlen(skb))) 2476 return NULL; 2477 skb->len -= len; 2478 return skb->data += len; 2479 } 2480 2481 static inline void *pskb_pull(struct sk_buff *skb, unsigned int len) 2482 { 2483 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len); 2484 } 2485 2486 static inline bool pskb_may_pull(struct sk_buff *skb, unsigned int len) 2487 { 2488 if (likely(len <= skb_headlen(skb))) 2489 return true; 2490 if (unlikely(len > skb->len)) 2491 return false; 2492 return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL; 2493 } 2494 2495 void skb_condense(struct sk_buff *skb); 2496 2497 /** 2498 * skb_headroom - bytes at buffer head 2499 * @skb: buffer to check 2500 * 2501 * Return the number of bytes of free space at the head of an &sk_buff. 2502 */ 2503 static inline unsigned int skb_headroom(const struct sk_buff *skb) 2504 { 2505 return skb->data - skb->head; 2506 } 2507 2508 /** 2509 * skb_tailroom - bytes at buffer end 2510 * @skb: buffer to check 2511 * 2512 * Return the number of bytes of free space at the tail of an sk_buff 2513 */ 2514 static inline int skb_tailroom(const struct sk_buff *skb) 2515 { 2516 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail; 2517 } 2518 2519 /** 2520 * skb_availroom - bytes at buffer end 2521 * @skb: buffer to check 2522 * 2523 * Return the number of bytes of free space at the tail of an sk_buff 2524 * allocated by sk_stream_alloc() 2525 */ 2526 static inline int skb_availroom(const struct sk_buff *skb) 2527 { 2528 if (skb_is_nonlinear(skb)) 2529 return 0; 2530 2531 return skb->end - skb->tail - skb->reserved_tailroom; 2532 } 2533 2534 /** 2535 * skb_reserve - adjust headroom 2536 * @skb: buffer to alter 2537 * @len: bytes to move 2538 * 2539 * Increase the headroom of an empty &sk_buff by reducing the tail 2540 * room. This is only allowed for an empty buffer. 2541 */ 2542 static inline void skb_reserve(struct sk_buff *skb, int len) 2543 { 2544 skb->data += len; 2545 skb->tail += len; 2546 } 2547 2548 /** 2549 * skb_tailroom_reserve - adjust reserved_tailroom 2550 * @skb: buffer to alter 2551 * @mtu: maximum amount of headlen permitted 2552 * @needed_tailroom: minimum amount of reserved_tailroom 2553 * 2554 * Set reserved_tailroom so that headlen can be as large as possible but 2555 * not larger than mtu and tailroom cannot be smaller than 2556 * needed_tailroom. 2557 * The required headroom should already have been reserved before using 2558 * this function. 2559 */ 2560 static inline void skb_tailroom_reserve(struct sk_buff *skb, unsigned int mtu, 2561 unsigned int needed_tailroom) 2562 { 2563 SKB_LINEAR_ASSERT(skb); 2564 if (mtu < skb_tailroom(skb) - needed_tailroom) 2565 /* use at most mtu */ 2566 skb->reserved_tailroom = skb_tailroom(skb) - mtu; 2567 else 2568 /* use up to all available space */ 2569 skb->reserved_tailroom = needed_tailroom; 2570 } 2571 2572 #define ENCAP_TYPE_ETHER 0 2573 #define ENCAP_TYPE_IPPROTO 1 2574 2575 static inline void skb_set_inner_protocol(struct sk_buff *skb, 2576 __be16 protocol) 2577 { 2578 skb->inner_protocol = protocol; 2579 skb->inner_protocol_type = ENCAP_TYPE_ETHER; 2580 } 2581 2582 static inline void skb_set_inner_ipproto(struct sk_buff *skb, 2583 __u8 ipproto) 2584 { 2585 skb->inner_ipproto = ipproto; 2586 skb->inner_protocol_type = ENCAP_TYPE_IPPROTO; 2587 } 2588 2589 static inline void skb_reset_inner_headers(struct sk_buff *skb) 2590 { 2591 skb->inner_mac_header = skb->mac_header; 2592 skb->inner_network_header = skb->network_header; 2593 skb->inner_transport_header = skb->transport_header; 2594 } 2595 2596 static inline void skb_reset_mac_len(struct sk_buff *skb) 2597 { 2598 skb->mac_len = skb->network_header - skb->mac_header; 2599 } 2600 2601 static inline unsigned char *skb_inner_transport_header(const struct sk_buff 2602 *skb) 2603 { 2604 return skb->head + skb->inner_transport_header; 2605 } 2606 2607 static inline int skb_inner_transport_offset(const struct sk_buff *skb) 2608 { 2609 return skb_inner_transport_header(skb) - skb->data; 2610 } 2611 2612 static inline void skb_reset_inner_transport_header(struct sk_buff *skb) 2613 { 2614 skb->inner_transport_header = skb->data - skb->head; 2615 } 2616 2617 static inline void skb_set_inner_transport_header(struct sk_buff *skb, 2618 const int offset) 2619 { 2620 skb_reset_inner_transport_header(skb); 2621 skb->inner_transport_header += offset; 2622 } 2623 2624 static inline unsigned char *skb_inner_network_header(const struct sk_buff *skb) 2625 { 2626 return skb->head + skb->inner_network_header; 2627 } 2628 2629 static inline void skb_reset_inner_network_header(struct sk_buff *skb) 2630 { 2631 skb->inner_network_header = skb->data - skb->head; 2632 } 2633 2634 static inline void skb_set_inner_network_header(struct sk_buff *skb, 2635 const int offset) 2636 { 2637 skb_reset_inner_network_header(skb); 2638 skb->inner_network_header += offset; 2639 } 2640 2641 static inline unsigned char *skb_inner_mac_header(const struct sk_buff *skb) 2642 { 2643 return skb->head + skb->inner_mac_header; 2644 } 2645 2646 static inline void skb_reset_inner_mac_header(struct sk_buff *skb) 2647 { 2648 skb->inner_mac_header = skb->data - skb->head; 2649 } 2650 2651 static inline void skb_set_inner_mac_header(struct sk_buff *skb, 2652 const int offset) 2653 { 2654 skb_reset_inner_mac_header(skb); 2655 skb->inner_mac_header += offset; 2656 } 2657 static inline bool skb_transport_header_was_set(const struct sk_buff *skb) 2658 { 2659 return skb->transport_header != (typeof(skb->transport_header))~0U; 2660 } 2661 2662 static inline unsigned char *skb_transport_header(const struct sk_buff *skb) 2663 { 2664 return skb->head + skb->transport_header; 2665 } 2666 2667 static inline void skb_reset_transport_header(struct sk_buff *skb) 2668 { 2669 skb->transport_header = skb->data - skb->head; 2670 } 2671 2672 static inline void skb_set_transport_header(struct sk_buff *skb, 2673 const int offset) 2674 { 2675 skb_reset_transport_header(skb); 2676 skb->transport_header += offset; 2677 } 2678 2679 static inline unsigned char *skb_network_header(const struct sk_buff *skb) 2680 { 2681 return skb->head + skb->network_header; 2682 } 2683 2684 static inline void skb_reset_network_header(struct sk_buff *skb) 2685 { 2686 skb->network_header = skb->data - skb->head; 2687 } 2688 2689 static inline void skb_set_network_header(struct sk_buff *skb, const int offset) 2690 { 2691 skb_reset_network_header(skb); 2692 skb->network_header += offset; 2693 } 2694 2695 static inline unsigned char *skb_mac_header(const struct sk_buff *skb) 2696 { 2697 return skb->head + skb->mac_header; 2698 } 2699 2700 static inline int skb_mac_offset(const struct sk_buff *skb) 2701 { 2702 return skb_mac_header(skb) - skb->data; 2703 } 2704 2705 static inline u32 skb_mac_header_len(const struct sk_buff *skb) 2706 { 2707 return skb->network_header - skb->mac_header; 2708 } 2709 2710 static inline int skb_mac_header_was_set(const struct sk_buff *skb) 2711 { 2712 return skb->mac_header != (typeof(skb->mac_header))~0U; 2713 } 2714 2715 static inline void skb_unset_mac_header(struct sk_buff *skb) 2716 { 2717 skb->mac_header = (typeof(skb->mac_header))~0U; 2718 } 2719 2720 static inline void skb_reset_mac_header(struct sk_buff *skb) 2721 { 2722 skb->mac_header = skb->data - skb->head; 2723 } 2724 2725 static inline void skb_set_mac_header(struct sk_buff *skb, const int offset) 2726 { 2727 skb_reset_mac_header(skb); 2728 skb->mac_header += offset; 2729 } 2730 2731 static inline void skb_pop_mac_header(struct sk_buff *skb) 2732 { 2733 skb->mac_header = skb->network_header; 2734 } 2735 2736 static inline void skb_probe_transport_header(struct sk_buff *skb) 2737 { 2738 struct flow_keys_basic keys; 2739 2740 if (skb_transport_header_was_set(skb)) 2741 return; 2742 2743 if (skb_flow_dissect_flow_keys_basic(NULL, skb, &keys, 2744 NULL, 0, 0, 0, 0)) 2745 skb_set_transport_header(skb, keys.control.thoff); 2746 } 2747 2748 static inline void skb_mac_header_rebuild(struct sk_buff *skb) 2749 { 2750 if (skb_mac_header_was_set(skb)) { 2751 const unsigned char *old_mac = skb_mac_header(skb); 2752 2753 skb_set_mac_header(skb, -skb->mac_len); 2754 memmove(skb_mac_header(skb), old_mac, skb->mac_len); 2755 } 2756 } 2757 2758 static inline int skb_checksum_start_offset(const struct sk_buff *skb) 2759 { 2760 return skb->csum_start - skb_headroom(skb); 2761 } 2762 2763 static inline unsigned char *skb_checksum_start(const struct sk_buff *skb) 2764 { 2765 return skb->head + skb->csum_start; 2766 } 2767 2768 static inline int skb_transport_offset(const struct sk_buff *skb) 2769 { 2770 return skb_transport_header(skb) - skb->data; 2771 } 2772 2773 static inline u32 skb_network_header_len(const struct sk_buff *skb) 2774 { 2775 return skb->transport_header - skb->network_header; 2776 } 2777 2778 static inline u32 skb_inner_network_header_len(const struct sk_buff *skb) 2779 { 2780 return skb->inner_transport_header - skb->inner_network_header; 2781 } 2782 2783 static inline int skb_network_offset(const struct sk_buff *skb) 2784 { 2785 return skb_network_header(skb) - skb->data; 2786 } 2787 2788 static inline int skb_inner_network_offset(const struct sk_buff *skb) 2789 { 2790 return skb_inner_network_header(skb) - skb->data; 2791 } 2792 2793 static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len) 2794 { 2795 return pskb_may_pull(skb, skb_network_offset(skb) + len); 2796 } 2797 2798 /* 2799 * CPUs often take a performance hit when accessing unaligned memory 2800 * locations. The actual performance hit varies, it can be small if the 2801 * hardware handles it or large if we have to take an exception and fix it 2802 * in software. 2803 * 2804 * Since an ethernet header is 14 bytes network drivers often end up with 2805 * the IP header at an unaligned offset. The IP header can be aligned by 2806 * shifting the start of the packet by 2 bytes. Drivers should do this 2807 * with: 2808 * 2809 * skb_reserve(skb, NET_IP_ALIGN); 2810 * 2811 * The downside to this alignment of the IP header is that the DMA is now 2812 * unaligned. On some architectures the cost of an unaligned DMA is high 2813 * and this cost outweighs the gains made by aligning the IP header. 2814 * 2815 * Since this trade off varies between architectures, we allow NET_IP_ALIGN 2816 * to be overridden. 2817 */ 2818 #ifndef NET_IP_ALIGN 2819 #define NET_IP_ALIGN 2 2820 #endif 2821 2822 /* 2823 * The networking layer reserves some headroom in skb data (via 2824 * dev_alloc_skb). This is used to avoid having to reallocate skb data when 2825 * the header has to grow. In the default case, if the header has to grow 2826 * 32 bytes or less we avoid the reallocation. 2827 * 2828 * Unfortunately this headroom changes the DMA alignment of the resulting 2829 * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive 2830 * on some architectures. An architecture can override this value, 2831 * perhaps setting it to a cacheline in size (since that will maintain 2832 * cacheline alignment of the DMA). It must be a power of 2. 2833 * 2834 * Various parts of the networking layer expect at least 32 bytes of 2835 * headroom, you should not reduce this. 2836 * 2837 * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS) 2838 * to reduce average number of cache lines per packet. 2839 * get_rps_cpu() for example only access one 64 bytes aligned block : 2840 * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8) 2841 */ 2842 #ifndef NET_SKB_PAD 2843 #define NET_SKB_PAD max(32, L1_CACHE_BYTES) 2844 #endif 2845 2846 int ___pskb_trim(struct sk_buff *skb, unsigned int len); 2847 2848 static inline void __skb_set_length(struct sk_buff *skb, unsigned int len) 2849 { 2850 if (WARN_ON(skb_is_nonlinear(skb))) 2851 return; 2852 skb->len = len; 2853 skb_set_tail_pointer(skb, len); 2854 } 2855 2856 static inline void __skb_trim(struct sk_buff *skb, unsigned int len) 2857 { 2858 __skb_set_length(skb, len); 2859 } 2860 2861 void skb_trim(struct sk_buff *skb, unsigned int len); 2862 2863 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len) 2864 { 2865 if (skb->data_len) 2866 return ___pskb_trim(skb, len); 2867 __skb_trim(skb, len); 2868 return 0; 2869 } 2870 2871 static inline int pskb_trim(struct sk_buff *skb, unsigned int len) 2872 { 2873 return (len < skb->len) ? __pskb_trim(skb, len) : 0; 2874 } 2875 2876 /** 2877 * pskb_trim_unique - remove end from a paged unique (not cloned) buffer 2878 * @skb: buffer to alter 2879 * @len: new length 2880 * 2881 * This is identical to pskb_trim except that the caller knows that 2882 * the skb is not cloned so we should never get an error due to out- 2883 * of-memory. 2884 */ 2885 static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len) 2886 { 2887 int err = pskb_trim(skb, len); 2888 BUG_ON(err); 2889 } 2890 2891 static inline int __skb_grow(struct sk_buff *skb, unsigned int len) 2892 { 2893 unsigned int diff = len - skb->len; 2894 2895 if (skb_tailroom(skb) < diff) { 2896 int ret = pskb_expand_head(skb, 0, diff - skb_tailroom(skb), 2897 GFP_ATOMIC); 2898 if (ret) 2899 return ret; 2900 } 2901 __skb_set_length(skb, len); 2902 return 0; 2903 } 2904 2905 /** 2906 * skb_orphan - orphan a buffer 2907 * @skb: buffer to orphan 2908 * 2909 * If a buffer currently has an owner then we call the owner's 2910 * destructor function and make the @skb unowned. The buffer continues 2911 * to exist but is no longer charged to its former owner. 2912 */ 2913 static inline void skb_orphan(struct sk_buff *skb) 2914 { 2915 if (skb->destructor) { 2916 skb->destructor(skb); 2917 skb->destructor = NULL; 2918 skb->sk = NULL; 2919 } else { 2920 BUG_ON(skb->sk); 2921 } 2922 } 2923 2924 /** 2925 * skb_orphan_frags - orphan the frags contained in a buffer 2926 * @skb: buffer to orphan frags from 2927 * @gfp_mask: allocation mask for replacement pages 2928 * 2929 * For each frag in the SKB which needs a destructor (i.e. has an 2930 * owner) create a copy of that frag and release the original 2931 * page by calling the destructor. 2932 */ 2933 static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask) 2934 { 2935 if (likely(!skb_zcopy(skb))) 2936 return 0; 2937 if (!skb_zcopy_is_nouarg(skb) && 2938 skb_uarg(skb)->callback == msg_zerocopy_callback) 2939 return 0; 2940 return skb_copy_ubufs(skb, gfp_mask); 2941 } 2942 2943 /* Frags must be orphaned, even if refcounted, if skb might loop to rx path */ 2944 static inline int skb_orphan_frags_rx(struct sk_buff *skb, gfp_t gfp_mask) 2945 { 2946 if (likely(!skb_zcopy(skb))) 2947 return 0; 2948 return skb_copy_ubufs(skb, gfp_mask); 2949 } 2950 2951 /** 2952 * __skb_queue_purge - empty a list 2953 * @list: list to empty 2954 * 2955 * Delete all buffers on an &sk_buff list. Each buffer is removed from 2956 * the list and one reference dropped. This function does not take the 2957 * list lock and the caller must hold the relevant locks to use it. 2958 */ 2959 static inline void __skb_queue_purge(struct sk_buff_head *list) 2960 { 2961 struct sk_buff *skb; 2962 while ((skb = __skb_dequeue(list)) != NULL) 2963 kfree_skb(skb); 2964 } 2965 void skb_queue_purge(struct sk_buff_head *list); 2966 2967 unsigned int skb_rbtree_purge(struct rb_root *root); 2968 2969 void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask); 2970 2971 /** 2972 * netdev_alloc_frag - allocate a page fragment 2973 * @fragsz: fragment size 2974 * 2975 * Allocates a frag from a page for receive buffer. 2976 * Uses GFP_ATOMIC allocations. 2977 */ 2978 static inline void *netdev_alloc_frag(unsigned int fragsz) 2979 { 2980 return __netdev_alloc_frag_align(fragsz, ~0u); 2981 } 2982 2983 static inline void *netdev_alloc_frag_align(unsigned int fragsz, 2984 unsigned int align) 2985 { 2986 WARN_ON_ONCE(!is_power_of_2(align)); 2987 return __netdev_alloc_frag_align(fragsz, -align); 2988 } 2989 2990 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int length, 2991 gfp_t gfp_mask); 2992 2993 /** 2994 * netdev_alloc_skb - allocate an skbuff for rx on a specific device 2995 * @dev: network device to receive on 2996 * @length: length to allocate 2997 * 2998 * Allocate a new &sk_buff and assign it a usage count of one. The 2999 * buffer has unspecified headroom built in. Users should allocate 3000 * the headroom they think they need without accounting for the 3001 * built in space. The built in space is used for optimisations. 3002 * 3003 * %NULL is returned if there is no free memory. Although this function 3004 * allocates memory it can be called from an interrupt. 3005 */ 3006 static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev, 3007 unsigned int length) 3008 { 3009 return __netdev_alloc_skb(dev, length, GFP_ATOMIC); 3010 } 3011 3012 /* legacy helper around __netdev_alloc_skb() */ 3013 static inline struct sk_buff *__dev_alloc_skb(unsigned int length, 3014 gfp_t gfp_mask) 3015 { 3016 return __netdev_alloc_skb(NULL, length, gfp_mask); 3017 } 3018 3019 /* legacy helper around netdev_alloc_skb() */ 3020 static inline struct sk_buff *dev_alloc_skb(unsigned int length) 3021 { 3022 return netdev_alloc_skb(NULL, length); 3023 } 3024 3025 3026 static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev, 3027 unsigned int length, gfp_t gfp) 3028 { 3029 struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp); 3030 3031 if (NET_IP_ALIGN && skb) 3032 skb_reserve(skb, NET_IP_ALIGN); 3033 return skb; 3034 } 3035 3036 static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev, 3037 unsigned int length) 3038 { 3039 return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC); 3040 } 3041 3042 static inline void skb_free_frag(void *addr) 3043 { 3044 page_frag_free(addr); 3045 } 3046 3047 void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask); 3048 3049 static inline void *napi_alloc_frag(unsigned int fragsz) 3050 { 3051 return __napi_alloc_frag_align(fragsz, ~0u); 3052 } 3053 3054 static inline void *napi_alloc_frag_align(unsigned int fragsz, 3055 unsigned int align) 3056 { 3057 WARN_ON_ONCE(!is_power_of_2(align)); 3058 return __napi_alloc_frag_align(fragsz, -align); 3059 } 3060 3061 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, 3062 unsigned int length, gfp_t gfp_mask); 3063 static inline struct sk_buff *napi_alloc_skb(struct napi_struct *napi, 3064 unsigned int length) 3065 { 3066 return __napi_alloc_skb(napi, length, GFP_ATOMIC); 3067 } 3068 void napi_consume_skb(struct sk_buff *skb, int budget); 3069 3070 void napi_skb_free_stolen_head(struct sk_buff *skb); 3071 void __kfree_skb_defer(struct sk_buff *skb); 3072 3073 /** 3074 * __dev_alloc_pages - allocate page for network Rx 3075 * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx 3076 * @order: size of the allocation 3077 * 3078 * Allocate a new page. 3079 * 3080 * %NULL is returned if there is no free memory. 3081 */ 3082 static inline struct page *__dev_alloc_pages(gfp_t gfp_mask, 3083 unsigned int order) 3084 { 3085 /* This piece of code contains several assumptions. 3086 * 1. This is for device Rx, therefor a cold page is preferred. 3087 * 2. The expectation is the user wants a compound page. 3088 * 3. If requesting a order 0 page it will not be compound 3089 * due to the check to see if order has a value in prep_new_page 3090 * 4. __GFP_MEMALLOC is ignored if __GFP_NOMEMALLOC is set due to 3091 * code in gfp_to_alloc_flags that should be enforcing this. 3092 */ 3093 gfp_mask |= __GFP_COMP | __GFP_MEMALLOC; 3094 3095 return alloc_pages_node(NUMA_NO_NODE, gfp_mask, order); 3096 } 3097 3098 static inline struct page *dev_alloc_pages(unsigned int order) 3099 { 3100 return __dev_alloc_pages(GFP_ATOMIC | __GFP_NOWARN, order); 3101 } 3102 3103 /** 3104 * __dev_alloc_page - allocate a page for network Rx 3105 * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx 3106 * 3107 * Allocate a new page. 3108 * 3109 * %NULL is returned if there is no free memory. 3110 */ 3111 static inline struct page *__dev_alloc_page(gfp_t gfp_mask) 3112 { 3113 return __dev_alloc_pages(gfp_mask, 0); 3114 } 3115 3116 static inline struct page *dev_alloc_page(void) 3117 { 3118 return dev_alloc_pages(0); 3119 } 3120 3121 /** 3122 * dev_page_is_reusable - check whether a page can be reused for network Rx 3123 * @page: the page to test 3124 * 3125 * A page shouldn't be considered for reusing/recycling if it was allocated 3126 * under memory pressure or at a distant memory node. 3127 * 3128 * Returns false if this page should be returned to page allocator, true 3129 * otherwise. 3130 */ 3131 static inline bool dev_page_is_reusable(const struct page *page) 3132 { 3133 return likely(page_to_nid(page) == numa_mem_id() && 3134 !page_is_pfmemalloc(page)); 3135 } 3136 3137 /** 3138 * skb_propagate_pfmemalloc - Propagate pfmemalloc if skb is allocated after RX page 3139 * @page: The page that was allocated from skb_alloc_page 3140 * @skb: The skb that may need pfmemalloc set 3141 */ 3142 static inline void skb_propagate_pfmemalloc(const struct page *page, 3143 struct sk_buff *skb) 3144 { 3145 if (page_is_pfmemalloc(page)) 3146 skb->pfmemalloc = true; 3147 } 3148 3149 /** 3150 * skb_frag_off() - Returns the offset of a skb fragment 3151 * @frag: the paged fragment 3152 */ 3153 static inline unsigned int skb_frag_off(const skb_frag_t *frag) 3154 { 3155 return frag->bv_offset; 3156 } 3157 3158 /** 3159 * skb_frag_off_add() - Increments the offset of a skb fragment by @delta 3160 * @frag: skb fragment 3161 * @delta: value to add 3162 */ 3163 static inline void skb_frag_off_add(skb_frag_t *frag, int delta) 3164 { 3165 frag->bv_offset += delta; 3166 } 3167 3168 /** 3169 * skb_frag_off_set() - Sets the offset of a skb fragment 3170 * @frag: skb fragment 3171 * @offset: offset of fragment 3172 */ 3173 static inline void skb_frag_off_set(skb_frag_t *frag, unsigned int offset) 3174 { 3175 frag->bv_offset = offset; 3176 } 3177 3178 /** 3179 * skb_frag_off_copy() - Sets the offset of a skb fragment from another fragment 3180 * @fragto: skb fragment where offset is set 3181 * @fragfrom: skb fragment offset is copied from 3182 */ 3183 static inline void skb_frag_off_copy(skb_frag_t *fragto, 3184 const skb_frag_t *fragfrom) 3185 { 3186 fragto->bv_offset = fragfrom->bv_offset; 3187 } 3188 3189 /** 3190 * skb_frag_page - retrieve the page referred to by a paged fragment 3191 * @frag: the paged fragment 3192 * 3193 * Returns the &struct page associated with @frag. 3194 */ 3195 static inline struct page *skb_frag_page(const skb_frag_t *frag) 3196 { 3197 return frag->bv_page; 3198 } 3199 3200 /** 3201 * __skb_frag_ref - take an addition reference on a paged fragment. 3202 * @frag: the paged fragment 3203 * 3204 * Takes an additional reference on the paged fragment @frag. 3205 */ 3206 static inline void __skb_frag_ref(skb_frag_t *frag) 3207 { 3208 get_page(skb_frag_page(frag)); 3209 } 3210 3211 /** 3212 * skb_frag_ref - take an addition reference on a paged fragment of an skb. 3213 * @skb: the buffer 3214 * @f: the fragment offset. 3215 * 3216 * Takes an additional reference on the @f'th paged fragment of @skb. 3217 */ 3218 static inline void skb_frag_ref(struct sk_buff *skb, int f) 3219 { 3220 __skb_frag_ref(&skb_shinfo(skb)->frags[f]); 3221 } 3222 3223 /** 3224 * __skb_frag_unref - release a reference on a paged fragment. 3225 * @frag: the paged fragment 3226 * @recycle: recycle the page if allocated via page_pool 3227 * 3228 * Releases a reference on the paged fragment @frag 3229 * or recycles the page via the page_pool API. 3230 */ 3231 static inline void __skb_frag_unref(skb_frag_t *frag, bool recycle) 3232 { 3233 struct page *page = skb_frag_page(frag); 3234 3235 #ifdef CONFIG_PAGE_POOL 3236 if (recycle && page_pool_return_skb_page(page)) 3237 return; 3238 #endif 3239 put_page(page); 3240 } 3241 3242 /** 3243 * skb_frag_unref - release a reference on a paged fragment of an skb. 3244 * @skb: the buffer 3245 * @f: the fragment offset 3246 * 3247 * Releases a reference on the @f'th paged fragment of @skb. 3248 */ 3249 static inline void skb_frag_unref(struct sk_buff *skb, int f) 3250 { 3251 __skb_frag_unref(&skb_shinfo(skb)->frags[f], skb->pp_recycle); 3252 } 3253 3254 /** 3255 * skb_frag_address - gets the address of the data contained in a paged fragment 3256 * @frag: the paged fragment buffer 3257 * 3258 * Returns the address of the data within @frag. The page must already 3259 * be mapped. 3260 */ 3261 static inline void *skb_frag_address(const skb_frag_t *frag) 3262 { 3263 return page_address(skb_frag_page(frag)) + skb_frag_off(frag); 3264 } 3265 3266 /** 3267 * skb_frag_address_safe - gets the address of the data contained in a paged fragment 3268 * @frag: the paged fragment buffer 3269 * 3270 * Returns the address of the data within @frag. Checks that the page 3271 * is mapped and returns %NULL otherwise. 3272 */ 3273 static inline void *skb_frag_address_safe(const skb_frag_t *frag) 3274 { 3275 void *ptr = page_address(skb_frag_page(frag)); 3276 if (unlikely(!ptr)) 3277 return NULL; 3278 3279 return ptr + skb_frag_off(frag); 3280 } 3281 3282 /** 3283 * skb_frag_page_copy() - sets the page in a fragment from another fragment 3284 * @fragto: skb fragment where page is set 3285 * @fragfrom: skb fragment page is copied from 3286 */ 3287 static inline void skb_frag_page_copy(skb_frag_t *fragto, 3288 const skb_frag_t *fragfrom) 3289 { 3290 fragto->bv_page = fragfrom->bv_page; 3291 } 3292 3293 /** 3294 * __skb_frag_set_page - sets the page contained in a paged fragment 3295 * @frag: the paged fragment 3296 * @page: the page to set 3297 * 3298 * Sets the fragment @frag to contain @page. 3299 */ 3300 static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page) 3301 { 3302 frag->bv_page = page; 3303 } 3304 3305 /** 3306 * skb_frag_set_page - sets the page contained in a paged fragment of an skb 3307 * @skb: the buffer 3308 * @f: the fragment offset 3309 * @page: the page to set 3310 * 3311 * Sets the @f'th fragment of @skb to contain @page. 3312 */ 3313 static inline void skb_frag_set_page(struct sk_buff *skb, int f, 3314 struct page *page) 3315 { 3316 __skb_frag_set_page(&skb_shinfo(skb)->frags[f], page); 3317 } 3318 3319 bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio); 3320 3321 /** 3322 * skb_frag_dma_map - maps a paged fragment via the DMA API 3323 * @dev: the device to map the fragment to 3324 * @frag: the paged fragment to map 3325 * @offset: the offset within the fragment (starting at the 3326 * fragment's own offset) 3327 * @size: the number of bytes to map 3328 * @dir: the direction of the mapping (``PCI_DMA_*``) 3329 * 3330 * Maps the page associated with @frag to @device. 3331 */ 3332 static inline dma_addr_t skb_frag_dma_map(struct device *dev, 3333 const skb_frag_t *frag, 3334 size_t offset, size_t size, 3335 enum dma_data_direction dir) 3336 { 3337 return dma_map_page(dev, skb_frag_page(frag), 3338 skb_frag_off(frag) + offset, size, dir); 3339 } 3340 3341 static inline struct sk_buff *pskb_copy(struct sk_buff *skb, 3342 gfp_t gfp_mask) 3343 { 3344 return __pskb_copy(skb, skb_headroom(skb), gfp_mask); 3345 } 3346 3347 3348 static inline struct sk_buff *pskb_copy_for_clone(struct sk_buff *skb, 3349 gfp_t gfp_mask) 3350 { 3351 return __pskb_copy_fclone(skb, skb_headroom(skb), gfp_mask, true); 3352 } 3353 3354 3355 /** 3356 * skb_clone_writable - is the header of a clone writable 3357 * @skb: buffer to check 3358 * @len: length up to which to write 3359 * 3360 * Returns true if modifying the header part of the cloned buffer 3361 * does not requires the data to be copied. 3362 */ 3363 static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len) 3364 { 3365 return !skb_header_cloned(skb) && 3366 skb_headroom(skb) + len <= skb->hdr_len; 3367 } 3368 3369 static inline int skb_try_make_writable(struct sk_buff *skb, 3370 unsigned int write_len) 3371 { 3372 return skb_cloned(skb) && !skb_clone_writable(skb, write_len) && 3373 pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 3374 } 3375 3376 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom, 3377 int cloned) 3378 { 3379 int delta = 0; 3380 3381 if (headroom > skb_headroom(skb)) 3382 delta = headroom - skb_headroom(skb); 3383 3384 if (delta || cloned) 3385 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0, 3386 GFP_ATOMIC); 3387 return 0; 3388 } 3389 3390 /** 3391 * skb_cow - copy header of skb when it is required 3392 * @skb: buffer to cow 3393 * @headroom: needed headroom 3394 * 3395 * If the skb passed lacks sufficient headroom or its data part 3396 * is shared, data is reallocated. If reallocation fails, an error 3397 * is returned and original skb is not changed. 3398 * 3399 * The result is skb with writable area skb->head...skb->tail 3400 * and at least @headroom of space at head. 3401 */ 3402 static inline int skb_cow(struct sk_buff *skb, unsigned int headroom) 3403 { 3404 return __skb_cow(skb, headroom, skb_cloned(skb)); 3405 } 3406 3407 /** 3408 * skb_cow_head - skb_cow but only making the head writable 3409 * @skb: buffer to cow 3410 * @headroom: needed headroom 3411 * 3412 * This function is identical to skb_cow except that we replace the 3413 * skb_cloned check by skb_header_cloned. It should be used when 3414 * you only need to push on some header and do not need to modify 3415 * the data. 3416 */ 3417 static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom) 3418 { 3419 return __skb_cow(skb, headroom, skb_header_cloned(skb)); 3420 } 3421 3422 /** 3423 * skb_padto - pad an skbuff up to a minimal size 3424 * @skb: buffer to pad 3425 * @len: minimal length 3426 * 3427 * Pads up a buffer to ensure the trailing bytes exist and are 3428 * blanked. If the buffer already contains sufficient data it 3429 * is untouched. Otherwise it is extended. Returns zero on 3430 * success. The skb is freed on error. 3431 */ 3432 static inline int skb_padto(struct sk_buff *skb, unsigned int len) 3433 { 3434 unsigned int size = skb->len; 3435 if (likely(size >= len)) 3436 return 0; 3437 return skb_pad(skb, len - size); 3438 } 3439 3440 /** 3441 * __skb_put_padto - increase size and pad an skbuff up to a minimal size 3442 * @skb: buffer to pad 3443 * @len: minimal length 3444 * @free_on_error: free buffer on error 3445 * 3446 * Pads up a buffer to ensure the trailing bytes exist and are 3447 * blanked. If the buffer already contains sufficient data it 3448 * is untouched. Otherwise it is extended. Returns zero on 3449 * success. The skb is freed on error if @free_on_error is true. 3450 */ 3451 static inline int __must_check __skb_put_padto(struct sk_buff *skb, 3452 unsigned int len, 3453 bool free_on_error) 3454 { 3455 unsigned int size = skb->len; 3456 3457 if (unlikely(size < len)) { 3458 len -= size; 3459 if (__skb_pad(skb, len, free_on_error)) 3460 return -ENOMEM; 3461 __skb_put(skb, len); 3462 } 3463 return 0; 3464 } 3465 3466 /** 3467 * skb_put_padto - increase size and pad an skbuff up to a minimal size 3468 * @skb: buffer to pad 3469 * @len: minimal length 3470 * 3471 * Pads up a buffer to ensure the trailing bytes exist and are 3472 * blanked. If the buffer already contains sufficient data it 3473 * is untouched. Otherwise it is extended. Returns zero on 3474 * success. The skb is freed on error. 3475 */ 3476 static inline int __must_check skb_put_padto(struct sk_buff *skb, unsigned int len) 3477 { 3478 return __skb_put_padto(skb, len, true); 3479 } 3480 3481 static inline int skb_add_data(struct sk_buff *skb, 3482 struct iov_iter *from, int copy) 3483 { 3484 const int off = skb->len; 3485 3486 if (skb->ip_summed == CHECKSUM_NONE) { 3487 __wsum csum = 0; 3488 if (csum_and_copy_from_iter_full(skb_put(skb, copy), copy, 3489 &csum, from)) { 3490 skb->csum = csum_block_add(skb->csum, csum, off); 3491 return 0; 3492 } 3493 } else if (copy_from_iter_full(skb_put(skb, copy), copy, from)) 3494 return 0; 3495 3496 __skb_trim(skb, off); 3497 return -EFAULT; 3498 } 3499 3500 static inline bool skb_can_coalesce(struct sk_buff *skb, int i, 3501 const struct page *page, int off) 3502 { 3503 if (skb_zcopy(skb)) 3504 return false; 3505 if (i) { 3506 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1]; 3507 3508 return page == skb_frag_page(frag) && 3509 off == skb_frag_off(frag) + skb_frag_size(frag); 3510 } 3511 return false; 3512 } 3513 3514 static inline int __skb_linearize(struct sk_buff *skb) 3515 { 3516 return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM; 3517 } 3518 3519 /** 3520 * skb_linearize - convert paged skb to linear one 3521 * @skb: buffer to linarize 3522 * 3523 * If there is no free memory -ENOMEM is returned, otherwise zero 3524 * is returned and the old skb data released. 3525 */ 3526 static inline int skb_linearize(struct sk_buff *skb) 3527 { 3528 return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0; 3529 } 3530 3531 /** 3532 * skb_has_shared_frag - can any frag be overwritten 3533 * @skb: buffer to test 3534 * 3535 * Return true if the skb has at least one frag that might be modified 3536 * by an external entity (as in vmsplice()/sendfile()) 3537 */ 3538 static inline bool skb_has_shared_frag(const struct sk_buff *skb) 3539 { 3540 return skb_is_nonlinear(skb) && 3541 skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG; 3542 } 3543 3544 /** 3545 * skb_linearize_cow - make sure skb is linear and writable 3546 * @skb: buffer to process 3547 * 3548 * If there is no free memory -ENOMEM is returned, otherwise zero 3549 * is returned and the old skb data released. 3550 */ 3551 static inline int skb_linearize_cow(struct sk_buff *skb) 3552 { 3553 return skb_is_nonlinear(skb) || skb_cloned(skb) ? 3554 __skb_linearize(skb) : 0; 3555 } 3556 3557 static __always_inline void 3558 __skb_postpull_rcsum(struct sk_buff *skb, const void *start, unsigned int len, 3559 unsigned int off) 3560 { 3561 if (skb->ip_summed == CHECKSUM_COMPLETE) 3562 skb->csum = csum_block_sub(skb->csum, 3563 csum_partial(start, len, 0), off); 3564 else if (skb->ip_summed == CHECKSUM_PARTIAL && 3565 skb_checksum_start_offset(skb) < 0) 3566 skb->ip_summed = CHECKSUM_NONE; 3567 } 3568 3569 /** 3570 * skb_postpull_rcsum - update checksum for received skb after pull 3571 * @skb: buffer to update 3572 * @start: start of data before pull 3573 * @len: length of data pulled 3574 * 3575 * After doing a pull on a received packet, you need to call this to 3576 * update the CHECKSUM_COMPLETE checksum, or set ip_summed to 3577 * CHECKSUM_NONE so that it can be recomputed from scratch. 3578 */ 3579 static inline void skb_postpull_rcsum(struct sk_buff *skb, 3580 const void *start, unsigned int len) 3581 { 3582 if (skb->ip_summed == CHECKSUM_COMPLETE) 3583 skb->csum = wsum_negate(csum_partial(start, len, 3584 wsum_negate(skb->csum))); 3585 else if (skb->ip_summed == CHECKSUM_PARTIAL && 3586 skb_checksum_start_offset(skb) < 0) 3587 skb->ip_summed = CHECKSUM_NONE; 3588 } 3589 3590 static __always_inline void 3591 __skb_postpush_rcsum(struct sk_buff *skb, const void *start, unsigned int len, 3592 unsigned int off) 3593 { 3594 if (skb->ip_summed == CHECKSUM_COMPLETE) 3595 skb->csum = csum_block_add(skb->csum, 3596 csum_partial(start, len, 0), off); 3597 } 3598 3599 /** 3600 * skb_postpush_rcsum - update checksum for received skb after push 3601 * @skb: buffer to update 3602 * @start: start of data after push 3603 * @len: length of data pushed 3604 * 3605 * After doing a push on a received packet, you need to call this to 3606 * update the CHECKSUM_COMPLETE checksum. 3607 */ 3608 static inline void skb_postpush_rcsum(struct sk_buff *skb, 3609 const void *start, unsigned int len) 3610 { 3611 __skb_postpush_rcsum(skb, start, len, 0); 3612 } 3613 3614 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len); 3615 3616 /** 3617 * skb_push_rcsum - push skb and update receive checksum 3618 * @skb: buffer to update 3619 * @len: length of data pulled 3620 * 3621 * This function performs an skb_push on the packet and updates 3622 * the CHECKSUM_COMPLETE checksum. It should be used on 3623 * receive path processing instead of skb_push unless you know 3624 * that the checksum difference is zero (e.g., a valid IP header) 3625 * or you are setting ip_summed to CHECKSUM_NONE. 3626 */ 3627 static inline void *skb_push_rcsum(struct sk_buff *skb, unsigned int len) 3628 { 3629 skb_push(skb, len); 3630 skb_postpush_rcsum(skb, skb->data, len); 3631 return skb->data; 3632 } 3633 3634 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len); 3635 /** 3636 * pskb_trim_rcsum - trim received skb and update checksum 3637 * @skb: buffer to trim 3638 * @len: new length 3639 * 3640 * This is exactly the same as pskb_trim except that it ensures the 3641 * checksum of received packets are still valid after the operation. 3642 * It can change skb pointers. 3643 */ 3644 3645 static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len) 3646 { 3647 if (likely(len >= skb->len)) 3648 return 0; 3649 return pskb_trim_rcsum_slow(skb, len); 3650 } 3651 3652 static inline int __skb_trim_rcsum(struct sk_buff *skb, unsigned int len) 3653 { 3654 if (skb->ip_summed == CHECKSUM_COMPLETE) 3655 skb->ip_summed = CHECKSUM_NONE; 3656 __skb_trim(skb, len); 3657 return 0; 3658 } 3659 3660 static inline int __skb_grow_rcsum(struct sk_buff *skb, unsigned int len) 3661 { 3662 if (skb->ip_summed == CHECKSUM_COMPLETE) 3663 skb->ip_summed = CHECKSUM_NONE; 3664 return __skb_grow(skb, len); 3665 } 3666 3667 #define rb_to_skb(rb) rb_entry_safe(rb, struct sk_buff, rbnode) 3668 #define skb_rb_first(root) rb_to_skb(rb_first(root)) 3669 #define skb_rb_last(root) rb_to_skb(rb_last(root)) 3670 #define skb_rb_next(skb) rb_to_skb(rb_next(&(skb)->rbnode)) 3671 #define skb_rb_prev(skb) rb_to_skb(rb_prev(&(skb)->rbnode)) 3672 3673 #define skb_queue_walk(queue, skb) \ 3674 for (skb = (queue)->next; \ 3675 skb != (struct sk_buff *)(queue); \ 3676 skb = skb->next) 3677 3678 #define skb_queue_walk_safe(queue, skb, tmp) \ 3679 for (skb = (queue)->next, tmp = skb->next; \ 3680 skb != (struct sk_buff *)(queue); \ 3681 skb = tmp, tmp = skb->next) 3682 3683 #define skb_queue_walk_from(queue, skb) \ 3684 for (; skb != (struct sk_buff *)(queue); \ 3685 skb = skb->next) 3686 3687 #define skb_rbtree_walk(skb, root) \ 3688 for (skb = skb_rb_first(root); skb != NULL; \ 3689 skb = skb_rb_next(skb)) 3690 3691 #define skb_rbtree_walk_from(skb) \ 3692 for (; skb != NULL; \ 3693 skb = skb_rb_next(skb)) 3694 3695 #define skb_rbtree_walk_from_safe(skb, tmp) \ 3696 for (; tmp = skb ? skb_rb_next(skb) : NULL, (skb != NULL); \ 3697 skb = tmp) 3698 3699 #define skb_queue_walk_from_safe(queue, skb, tmp) \ 3700 for (tmp = skb->next; \ 3701 skb != (struct sk_buff *)(queue); \ 3702 skb = tmp, tmp = skb->next) 3703 3704 #define skb_queue_reverse_walk(queue, skb) \ 3705 for (skb = (queue)->prev; \ 3706 skb != (struct sk_buff *)(queue); \ 3707 skb = skb->prev) 3708 3709 #define skb_queue_reverse_walk_safe(queue, skb, tmp) \ 3710 for (skb = (queue)->prev, tmp = skb->prev; \ 3711 skb != (struct sk_buff *)(queue); \ 3712 skb = tmp, tmp = skb->prev) 3713 3714 #define skb_queue_reverse_walk_from_safe(queue, skb, tmp) \ 3715 for (tmp = skb->prev; \ 3716 skb != (struct sk_buff *)(queue); \ 3717 skb = tmp, tmp = skb->prev) 3718 3719 static inline bool skb_has_frag_list(const struct sk_buff *skb) 3720 { 3721 return skb_shinfo(skb)->frag_list != NULL; 3722 } 3723 3724 static inline void skb_frag_list_init(struct sk_buff *skb) 3725 { 3726 skb_shinfo(skb)->frag_list = NULL; 3727 } 3728 3729 #define skb_walk_frags(skb, iter) \ 3730 for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next) 3731 3732 3733 int __skb_wait_for_more_packets(struct sock *sk, struct sk_buff_head *queue, 3734 int *err, long *timeo_p, 3735 const struct sk_buff *skb); 3736 struct sk_buff *__skb_try_recv_from_queue(struct sock *sk, 3737 struct sk_buff_head *queue, 3738 unsigned int flags, 3739 int *off, int *err, 3740 struct sk_buff **last); 3741 struct sk_buff *__skb_try_recv_datagram(struct sock *sk, 3742 struct sk_buff_head *queue, 3743 unsigned int flags, int *off, int *err, 3744 struct sk_buff **last); 3745 struct sk_buff *__skb_recv_datagram(struct sock *sk, 3746 struct sk_buff_head *sk_queue, 3747 unsigned int flags, int *off, int *err); 3748 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, int noblock, 3749 int *err); 3750 __poll_t datagram_poll(struct file *file, struct socket *sock, 3751 struct poll_table_struct *wait); 3752 int skb_copy_datagram_iter(const struct sk_buff *from, int offset, 3753 struct iov_iter *to, int size); 3754 static inline int skb_copy_datagram_msg(const struct sk_buff *from, int offset, 3755 struct msghdr *msg, int size) 3756 { 3757 return skb_copy_datagram_iter(from, offset, &msg->msg_iter, size); 3758 } 3759 int skb_copy_and_csum_datagram_msg(struct sk_buff *skb, int hlen, 3760 struct msghdr *msg); 3761 int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset, 3762 struct iov_iter *to, int len, 3763 struct ahash_request *hash); 3764 int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset, 3765 struct iov_iter *from, int len); 3766 int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *frm); 3767 void skb_free_datagram(struct sock *sk, struct sk_buff *skb); 3768 void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len); 3769 static inline void skb_free_datagram_locked(struct sock *sk, 3770 struct sk_buff *skb) 3771 { 3772 __skb_free_datagram_locked(sk, skb, 0); 3773 } 3774 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags); 3775 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len); 3776 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len); 3777 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to, 3778 int len); 3779 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 3780 struct pipe_inode_info *pipe, unsigned int len, 3781 unsigned int flags); 3782 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 3783 int len); 3784 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len); 3785 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to); 3786 unsigned int skb_zerocopy_headlen(const struct sk_buff *from); 3787 int skb_zerocopy(struct sk_buff *to, struct sk_buff *from, 3788 int len, int hlen); 3789 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len); 3790 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen); 3791 void skb_scrub_packet(struct sk_buff *skb, bool xnet); 3792 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu); 3793 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len); 3794 struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features); 3795 struct sk_buff *skb_segment_list(struct sk_buff *skb, netdev_features_t features, 3796 unsigned int offset); 3797 struct sk_buff *skb_vlan_untag(struct sk_buff *skb); 3798 int skb_ensure_writable(struct sk_buff *skb, int write_len); 3799 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci); 3800 int skb_vlan_pop(struct sk_buff *skb); 3801 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci); 3802 int skb_eth_pop(struct sk_buff *skb); 3803 int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 3804 const unsigned char *src); 3805 int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 3806 int mac_len, bool ethernet); 3807 int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 3808 bool ethernet); 3809 int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse); 3810 int skb_mpls_dec_ttl(struct sk_buff *skb); 3811 struct sk_buff *pskb_extract(struct sk_buff *skb, int off, int to_copy, 3812 gfp_t gfp); 3813 3814 static inline int memcpy_from_msg(void *data, struct msghdr *msg, int len) 3815 { 3816 return copy_from_iter_full(data, len, &msg->msg_iter) ? 0 : -EFAULT; 3817 } 3818 3819 static inline int memcpy_to_msg(struct msghdr *msg, void *data, int len) 3820 { 3821 return copy_to_iter(data, len, &msg->msg_iter) == len ? 0 : -EFAULT; 3822 } 3823 3824 struct skb_checksum_ops { 3825 __wsum (*update)(const void *mem, int len, __wsum wsum); 3826 __wsum (*combine)(__wsum csum, __wsum csum2, int offset, int len); 3827 }; 3828 3829 extern const struct skb_checksum_ops *crc32c_csum_stub __read_mostly; 3830 3831 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 3832 __wsum csum, const struct skb_checksum_ops *ops); 3833 __wsum skb_checksum(const struct sk_buff *skb, int offset, int len, 3834 __wsum csum); 3835 3836 static inline void * __must_check 3837 __skb_header_pointer(const struct sk_buff *skb, int offset, int len, 3838 const void *data, int hlen, void *buffer) 3839 { 3840 if (likely(hlen - offset >= len)) 3841 return (void *)data + offset; 3842 3843 if (!skb || unlikely(skb_copy_bits(skb, offset, buffer, len) < 0)) 3844 return NULL; 3845 3846 return buffer; 3847 } 3848 3849 static inline void * __must_check 3850 skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer) 3851 { 3852 return __skb_header_pointer(skb, offset, len, skb->data, 3853 skb_headlen(skb), buffer); 3854 } 3855 3856 /** 3857 * skb_needs_linearize - check if we need to linearize a given skb 3858 * depending on the given device features. 3859 * @skb: socket buffer to check 3860 * @features: net device features 3861 * 3862 * Returns true if either: 3863 * 1. skb has frag_list and the device doesn't support FRAGLIST, or 3864 * 2. skb is fragmented and the device does not support SG. 3865 */ 3866 static inline bool skb_needs_linearize(struct sk_buff *skb, 3867 netdev_features_t features) 3868 { 3869 return skb_is_nonlinear(skb) && 3870 ((skb_has_frag_list(skb) && !(features & NETIF_F_FRAGLIST)) || 3871 (skb_shinfo(skb)->nr_frags && !(features & NETIF_F_SG))); 3872 } 3873 3874 static inline void skb_copy_from_linear_data(const struct sk_buff *skb, 3875 void *to, 3876 const unsigned int len) 3877 { 3878 memcpy(to, skb->data, len); 3879 } 3880 3881 static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb, 3882 const int offset, void *to, 3883 const unsigned int len) 3884 { 3885 memcpy(to, skb->data + offset, len); 3886 } 3887 3888 static inline void skb_copy_to_linear_data(struct sk_buff *skb, 3889 const void *from, 3890 const unsigned int len) 3891 { 3892 memcpy(skb->data, from, len); 3893 } 3894 3895 static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb, 3896 const int offset, 3897 const void *from, 3898 const unsigned int len) 3899 { 3900 memcpy(skb->data + offset, from, len); 3901 } 3902 3903 void skb_init(void); 3904 3905 static inline ktime_t skb_get_ktime(const struct sk_buff *skb) 3906 { 3907 return skb->tstamp; 3908 } 3909 3910 /** 3911 * skb_get_timestamp - get timestamp from a skb 3912 * @skb: skb to get stamp from 3913 * @stamp: pointer to struct __kernel_old_timeval to store stamp in 3914 * 3915 * Timestamps are stored in the skb as offsets to a base timestamp. 3916 * This function converts the offset back to a struct timeval and stores 3917 * it in stamp. 3918 */ 3919 static inline void skb_get_timestamp(const struct sk_buff *skb, 3920 struct __kernel_old_timeval *stamp) 3921 { 3922 *stamp = ns_to_kernel_old_timeval(skb->tstamp); 3923 } 3924 3925 static inline void skb_get_new_timestamp(const struct sk_buff *skb, 3926 struct __kernel_sock_timeval *stamp) 3927 { 3928 struct timespec64 ts = ktime_to_timespec64(skb->tstamp); 3929 3930 stamp->tv_sec = ts.tv_sec; 3931 stamp->tv_usec = ts.tv_nsec / 1000; 3932 } 3933 3934 static inline void skb_get_timestampns(const struct sk_buff *skb, 3935 struct __kernel_old_timespec *stamp) 3936 { 3937 struct timespec64 ts = ktime_to_timespec64(skb->tstamp); 3938 3939 stamp->tv_sec = ts.tv_sec; 3940 stamp->tv_nsec = ts.tv_nsec; 3941 } 3942 3943 static inline void skb_get_new_timestampns(const struct sk_buff *skb, 3944 struct __kernel_timespec *stamp) 3945 { 3946 struct timespec64 ts = ktime_to_timespec64(skb->tstamp); 3947 3948 stamp->tv_sec = ts.tv_sec; 3949 stamp->tv_nsec = ts.tv_nsec; 3950 } 3951 3952 static inline void __net_timestamp(struct sk_buff *skb) 3953 { 3954 skb->tstamp = ktime_get_real(); 3955 } 3956 3957 static inline ktime_t net_timedelta(ktime_t t) 3958 { 3959 return ktime_sub(ktime_get_real(), t); 3960 } 3961 3962 static inline u8 skb_metadata_len(const struct sk_buff *skb) 3963 { 3964 return skb_shinfo(skb)->meta_len; 3965 } 3966 3967 static inline void *skb_metadata_end(const struct sk_buff *skb) 3968 { 3969 return skb_mac_header(skb); 3970 } 3971 3972 static inline bool __skb_metadata_differs(const struct sk_buff *skb_a, 3973 const struct sk_buff *skb_b, 3974 u8 meta_len) 3975 { 3976 const void *a = skb_metadata_end(skb_a); 3977 const void *b = skb_metadata_end(skb_b); 3978 /* Using more efficient varaiant than plain call to memcmp(). */ 3979 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 3980 u64 diffs = 0; 3981 3982 switch (meta_len) { 3983 #define __it(x, op) (x -= sizeof(u##op)) 3984 #define __it_diff(a, b, op) (*(u##op *)__it(a, op)) ^ (*(u##op *)__it(b, op)) 3985 case 32: diffs |= __it_diff(a, b, 64); 3986 fallthrough; 3987 case 24: diffs |= __it_diff(a, b, 64); 3988 fallthrough; 3989 case 16: diffs |= __it_diff(a, b, 64); 3990 fallthrough; 3991 case 8: diffs |= __it_diff(a, b, 64); 3992 break; 3993 case 28: diffs |= __it_diff(a, b, 64); 3994 fallthrough; 3995 case 20: diffs |= __it_diff(a, b, 64); 3996 fallthrough; 3997 case 12: diffs |= __it_diff(a, b, 64); 3998 fallthrough; 3999 case 4: diffs |= __it_diff(a, b, 32); 4000 break; 4001 } 4002 return diffs; 4003 #else 4004 return memcmp(a - meta_len, b - meta_len, meta_len); 4005 #endif 4006 } 4007 4008 static inline bool skb_metadata_differs(const struct sk_buff *skb_a, 4009 const struct sk_buff *skb_b) 4010 { 4011 u8 len_a = skb_metadata_len(skb_a); 4012 u8 len_b = skb_metadata_len(skb_b); 4013 4014 if (!(len_a | len_b)) 4015 return false; 4016 4017 return len_a != len_b ? 4018 true : __skb_metadata_differs(skb_a, skb_b, len_a); 4019 } 4020 4021 static inline void skb_metadata_set(struct sk_buff *skb, u8 meta_len) 4022 { 4023 skb_shinfo(skb)->meta_len = meta_len; 4024 } 4025 4026 static inline void skb_metadata_clear(struct sk_buff *skb) 4027 { 4028 skb_metadata_set(skb, 0); 4029 } 4030 4031 struct sk_buff *skb_clone_sk(struct sk_buff *skb); 4032 4033 #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING 4034 4035 void skb_clone_tx_timestamp(struct sk_buff *skb); 4036 bool skb_defer_rx_timestamp(struct sk_buff *skb); 4037 4038 #else /* CONFIG_NETWORK_PHY_TIMESTAMPING */ 4039 4040 static inline void skb_clone_tx_timestamp(struct sk_buff *skb) 4041 { 4042 } 4043 4044 static inline bool skb_defer_rx_timestamp(struct sk_buff *skb) 4045 { 4046 return false; 4047 } 4048 4049 #endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */ 4050 4051 /** 4052 * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps 4053 * 4054 * PHY drivers may accept clones of transmitted packets for 4055 * timestamping via their phy_driver.txtstamp method. These drivers 4056 * must call this function to return the skb back to the stack with a 4057 * timestamp. 4058 * 4059 * @skb: clone of the original outgoing packet 4060 * @hwtstamps: hardware time stamps 4061 * 4062 */ 4063 void skb_complete_tx_timestamp(struct sk_buff *skb, 4064 struct skb_shared_hwtstamps *hwtstamps); 4065 4066 void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb, 4067 struct skb_shared_hwtstamps *hwtstamps, 4068 struct sock *sk, int tstype); 4069 4070 /** 4071 * skb_tstamp_tx - queue clone of skb with send time stamps 4072 * @orig_skb: the original outgoing packet 4073 * @hwtstamps: hardware time stamps, may be NULL if not available 4074 * 4075 * If the skb has a socket associated, then this function clones the 4076 * skb (thus sharing the actual data and optional structures), stores 4077 * the optional hardware time stamping information (if non NULL) or 4078 * generates a software time stamp (otherwise), then queues the clone 4079 * to the error queue of the socket. Errors are silently ignored. 4080 */ 4081 void skb_tstamp_tx(struct sk_buff *orig_skb, 4082 struct skb_shared_hwtstamps *hwtstamps); 4083 4084 /** 4085 * skb_tx_timestamp() - Driver hook for transmit timestamping 4086 * 4087 * Ethernet MAC Drivers should call this function in their hard_xmit() 4088 * function immediately before giving the sk_buff to the MAC hardware. 4089 * 4090 * Specifically, one should make absolutely sure that this function is 4091 * called before TX completion of this packet can trigger. Otherwise 4092 * the packet could potentially already be freed. 4093 * 4094 * @skb: A socket buffer. 4095 */ 4096 static inline void skb_tx_timestamp(struct sk_buff *skb) 4097 { 4098 skb_clone_tx_timestamp(skb); 4099 if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP) 4100 skb_tstamp_tx(skb, NULL); 4101 } 4102 4103 /** 4104 * skb_complete_wifi_ack - deliver skb with wifi status 4105 * 4106 * @skb: the original outgoing packet 4107 * @acked: ack status 4108 * 4109 */ 4110 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked); 4111 4112 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len); 4113 __sum16 __skb_checksum_complete(struct sk_buff *skb); 4114 4115 static inline int skb_csum_unnecessary(const struct sk_buff *skb) 4116 { 4117 return ((skb->ip_summed == CHECKSUM_UNNECESSARY) || 4118 skb->csum_valid || 4119 (skb->ip_summed == CHECKSUM_PARTIAL && 4120 skb_checksum_start_offset(skb) >= 0)); 4121 } 4122 4123 /** 4124 * skb_checksum_complete - Calculate checksum of an entire packet 4125 * @skb: packet to process 4126 * 4127 * This function calculates the checksum over the entire packet plus 4128 * the value of skb->csum. The latter can be used to supply the 4129 * checksum of a pseudo header as used by TCP/UDP. It returns the 4130 * checksum. 4131 * 4132 * For protocols that contain complete checksums such as ICMP/TCP/UDP, 4133 * this function can be used to verify that checksum on received 4134 * packets. In that case the function should return zero if the 4135 * checksum is correct. In particular, this function will return zero 4136 * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the 4137 * hardware has already verified the correctness of the checksum. 4138 */ 4139 static inline __sum16 skb_checksum_complete(struct sk_buff *skb) 4140 { 4141 return skb_csum_unnecessary(skb) ? 4142 0 : __skb_checksum_complete(skb); 4143 } 4144 4145 static inline void __skb_decr_checksum_unnecessary(struct sk_buff *skb) 4146 { 4147 if (skb->ip_summed == CHECKSUM_UNNECESSARY) { 4148 if (skb->csum_level == 0) 4149 skb->ip_summed = CHECKSUM_NONE; 4150 else 4151 skb->csum_level--; 4152 } 4153 } 4154 4155 static inline void __skb_incr_checksum_unnecessary(struct sk_buff *skb) 4156 { 4157 if (skb->ip_summed == CHECKSUM_UNNECESSARY) { 4158 if (skb->csum_level < SKB_MAX_CSUM_LEVEL) 4159 skb->csum_level++; 4160 } else if (skb->ip_summed == CHECKSUM_NONE) { 4161 skb->ip_summed = CHECKSUM_UNNECESSARY; 4162 skb->csum_level = 0; 4163 } 4164 } 4165 4166 static inline void __skb_reset_checksum_unnecessary(struct sk_buff *skb) 4167 { 4168 if (skb->ip_summed == CHECKSUM_UNNECESSARY) { 4169 skb->ip_summed = CHECKSUM_NONE; 4170 skb->csum_level = 0; 4171 } 4172 } 4173 4174 /* Check if we need to perform checksum complete validation. 4175 * 4176 * Returns true if checksum complete is needed, false otherwise 4177 * (either checksum is unnecessary or zero checksum is allowed). 4178 */ 4179 static inline bool __skb_checksum_validate_needed(struct sk_buff *skb, 4180 bool zero_okay, 4181 __sum16 check) 4182 { 4183 if (skb_csum_unnecessary(skb) || (zero_okay && !check)) { 4184 skb->csum_valid = 1; 4185 __skb_decr_checksum_unnecessary(skb); 4186 return false; 4187 } 4188 4189 return true; 4190 } 4191 4192 /* For small packets <= CHECKSUM_BREAK perform checksum complete directly 4193 * in checksum_init. 4194 */ 4195 #define CHECKSUM_BREAK 76 4196 4197 /* Unset checksum-complete 4198 * 4199 * Unset checksum complete can be done when packet is being modified 4200 * (uncompressed for instance) and checksum-complete value is 4201 * invalidated. 4202 */ 4203 static inline void skb_checksum_complete_unset(struct sk_buff *skb) 4204 { 4205 if (skb->ip_summed == CHECKSUM_COMPLETE) 4206 skb->ip_summed = CHECKSUM_NONE; 4207 } 4208 4209 /* Validate (init) checksum based on checksum complete. 4210 * 4211 * Return values: 4212 * 0: checksum is validated or try to in skb_checksum_complete. In the latter 4213 * case the ip_summed will not be CHECKSUM_UNNECESSARY and the pseudo 4214 * checksum is stored in skb->csum for use in __skb_checksum_complete 4215 * non-zero: value of invalid checksum 4216 * 4217 */ 4218 static inline __sum16 __skb_checksum_validate_complete(struct sk_buff *skb, 4219 bool complete, 4220 __wsum psum) 4221 { 4222 if (skb->ip_summed == CHECKSUM_COMPLETE) { 4223 if (!csum_fold(csum_add(psum, skb->csum))) { 4224 skb->csum_valid = 1; 4225 return 0; 4226 } 4227 } 4228 4229 skb->csum = psum; 4230 4231 if (complete || skb->len <= CHECKSUM_BREAK) { 4232 __sum16 csum; 4233 4234 csum = __skb_checksum_complete(skb); 4235 skb->csum_valid = !csum; 4236 return csum; 4237 } 4238 4239 return 0; 4240 } 4241 4242 static inline __wsum null_compute_pseudo(struct sk_buff *skb, int proto) 4243 { 4244 return 0; 4245 } 4246 4247 /* Perform checksum validate (init). Note that this is a macro since we only 4248 * want to calculate the pseudo header which is an input function if necessary. 4249 * First we try to validate without any computation (checksum unnecessary) and 4250 * then calculate based on checksum complete calling the function to compute 4251 * pseudo header. 4252 * 4253 * Return values: 4254 * 0: checksum is validated or try to in skb_checksum_complete 4255 * non-zero: value of invalid checksum 4256 */ 4257 #define __skb_checksum_validate(skb, proto, complete, \ 4258 zero_okay, check, compute_pseudo) \ 4259 ({ \ 4260 __sum16 __ret = 0; \ 4261 skb->csum_valid = 0; \ 4262 if (__skb_checksum_validate_needed(skb, zero_okay, check)) \ 4263 __ret = __skb_checksum_validate_complete(skb, \ 4264 complete, compute_pseudo(skb, proto)); \ 4265 __ret; \ 4266 }) 4267 4268 #define skb_checksum_init(skb, proto, compute_pseudo) \ 4269 __skb_checksum_validate(skb, proto, false, false, 0, compute_pseudo) 4270 4271 #define skb_checksum_init_zero_check(skb, proto, check, compute_pseudo) \ 4272 __skb_checksum_validate(skb, proto, false, true, check, compute_pseudo) 4273 4274 #define skb_checksum_validate(skb, proto, compute_pseudo) \ 4275 __skb_checksum_validate(skb, proto, true, false, 0, compute_pseudo) 4276 4277 #define skb_checksum_validate_zero_check(skb, proto, check, \ 4278 compute_pseudo) \ 4279 __skb_checksum_validate(skb, proto, true, true, check, compute_pseudo) 4280 4281 #define skb_checksum_simple_validate(skb) \ 4282 __skb_checksum_validate(skb, 0, true, false, 0, null_compute_pseudo) 4283 4284 static inline bool __skb_checksum_convert_check(struct sk_buff *skb) 4285 { 4286 return (skb->ip_summed == CHECKSUM_NONE && skb->csum_valid); 4287 } 4288 4289 static inline void __skb_checksum_convert(struct sk_buff *skb, __wsum pseudo) 4290 { 4291 skb->csum = ~pseudo; 4292 skb->ip_summed = CHECKSUM_COMPLETE; 4293 } 4294 4295 #define skb_checksum_try_convert(skb, proto, compute_pseudo) \ 4296 do { \ 4297 if (__skb_checksum_convert_check(skb)) \ 4298 __skb_checksum_convert(skb, compute_pseudo(skb, proto)); \ 4299 } while (0) 4300 4301 static inline void skb_remcsum_adjust_partial(struct sk_buff *skb, void *ptr, 4302 u16 start, u16 offset) 4303 { 4304 skb->ip_summed = CHECKSUM_PARTIAL; 4305 skb->csum_start = ((unsigned char *)ptr + start) - skb->head; 4306 skb->csum_offset = offset - start; 4307 } 4308 4309 /* Update skbuf and packet to reflect the remote checksum offload operation. 4310 * When called, ptr indicates the starting point for skb->csum when 4311 * ip_summed is CHECKSUM_COMPLETE. If we need create checksum complete 4312 * here, skb_postpull_rcsum is done so skb->csum start is ptr. 4313 */ 4314 static inline void skb_remcsum_process(struct sk_buff *skb, void *ptr, 4315 int start, int offset, bool nopartial) 4316 { 4317 __wsum delta; 4318 4319 if (!nopartial) { 4320 skb_remcsum_adjust_partial(skb, ptr, start, offset); 4321 return; 4322 } 4323 4324 if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE)) { 4325 __skb_checksum_complete(skb); 4326 skb_postpull_rcsum(skb, skb->data, ptr - (void *)skb->data); 4327 } 4328 4329 delta = remcsum_adjust(ptr, skb->csum, start, offset); 4330 4331 /* Adjust skb->csum since we changed the packet */ 4332 skb->csum = csum_add(skb->csum, delta); 4333 } 4334 4335 static inline struct nf_conntrack *skb_nfct(const struct sk_buff *skb) 4336 { 4337 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 4338 return (void *)(skb->_nfct & NFCT_PTRMASK); 4339 #else 4340 return NULL; 4341 #endif 4342 } 4343 4344 static inline unsigned long skb_get_nfct(const struct sk_buff *skb) 4345 { 4346 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 4347 return skb->_nfct; 4348 #else 4349 return 0UL; 4350 #endif 4351 } 4352 4353 static inline void skb_set_nfct(struct sk_buff *skb, unsigned long nfct) 4354 { 4355 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 4356 skb->slow_gro |= !!nfct; 4357 skb->_nfct = nfct; 4358 #endif 4359 } 4360 4361 #ifdef CONFIG_SKB_EXTENSIONS 4362 enum skb_ext_id { 4363 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4364 SKB_EXT_BRIDGE_NF, 4365 #endif 4366 #ifdef CONFIG_XFRM 4367 SKB_EXT_SEC_PATH, 4368 #endif 4369 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 4370 TC_SKB_EXT, 4371 #endif 4372 #if IS_ENABLED(CONFIG_MPTCP) 4373 SKB_EXT_MPTCP, 4374 #endif 4375 #if IS_ENABLED(CONFIG_MCTP_FLOWS) 4376 SKB_EXT_MCTP, 4377 #endif 4378 SKB_EXT_NUM, /* must be last */ 4379 }; 4380 4381 /** 4382 * struct skb_ext - sk_buff extensions 4383 * @refcnt: 1 on allocation, deallocated on 0 4384 * @offset: offset to add to @data to obtain extension address 4385 * @chunks: size currently allocated, stored in SKB_EXT_ALIGN_SHIFT units 4386 * @data: start of extension data, variable sized 4387 * 4388 * Note: offsets/lengths are stored in chunks of 8 bytes, this allows 4389 * to use 'u8' types while allowing up to 2kb worth of extension data. 4390 */ 4391 struct skb_ext { 4392 refcount_t refcnt; 4393 u8 offset[SKB_EXT_NUM]; /* in chunks of 8 bytes */ 4394 u8 chunks; /* same */ 4395 char data[] __aligned(8); 4396 }; 4397 4398 struct skb_ext *__skb_ext_alloc(gfp_t flags); 4399 void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 4400 struct skb_ext *ext); 4401 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id); 4402 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id); 4403 void __skb_ext_put(struct skb_ext *ext); 4404 4405 static inline void skb_ext_put(struct sk_buff *skb) 4406 { 4407 if (skb->active_extensions) 4408 __skb_ext_put(skb->extensions); 4409 } 4410 4411 static inline void __skb_ext_copy(struct sk_buff *dst, 4412 const struct sk_buff *src) 4413 { 4414 dst->active_extensions = src->active_extensions; 4415 4416 if (src->active_extensions) { 4417 struct skb_ext *ext = src->extensions; 4418 4419 refcount_inc(&ext->refcnt); 4420 dst->extensions = ext; 4421 } 4422 } 4423 4424 static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *src) 4425 { 4426 skb_ext_put(dst); 4427 __skb_ext_copy(dst, src); 4428 } 4429 4430 static inline bool __skb_ext_exist(const struct skb_ext *ext, enum skb_ext_id i) 4431 { 4432 return !!ext->offset[i]; 4433 } 4434 4435 static inline bool skb_ext_exist(const struct sk_buff *skb, enum skb_ext_id id) 4436 { 4437 return skb->active_extensions & (1 << id); 4438 } 4439 4440 static inline void skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 4441 { 4442 if (skb_ext_exist(skb, id)) 4443 __skb_ext_del(skb, id); 4444 } 4445 4446 static inline void *skb_ext_find(const struct sk_buff *skb, enum skb_ext_id id) 4447 { 4448 if (skb_ext_exist(skb, id)) { 4449 struct skb_ext *ext = skb->extensions; 4450 4451 return (void *)ext + (ext->offset[id] << 3); 4452 } 4453 4454 return NULL; 4455 } 4456 4457 static inline void skb_ext_reset(struct sk_buff *skb) 4458 { 4459 if (unlikely(skb->active_extensions)) { 4460 __skb_ext_put(skb->extensions); 4461 skb->active_extensions = 0; 4462 } 4463 } 4464 4465 static inline bool skb_has_extensions(struct sk_buff *skb) 4466 { 4467 return unlikely(skb->active_extensions); 4468 } 4469 #else 4470 static inline void skb_ext_put(struct sk_buff *skb) {} 4471 static inline void skb_ext_reset(struct sk_buff *skb) {} 4472 static inline void skb_ext_del(struct sk_buff *skb, int unused) {} 4473 static inline void __skb_ext_copy(struct sk_buff *d, const struct sk_buff *s) {} 4474 static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *s) {} 4475 static inline bool skb_has_extensions(struct sk_buff *skb) { return false; } 4476 #endif /* CONFIG_SKB_EXTENSIONS */ 4477 4478 static inline void nf_reset_ct(struct sk_buff *skb) 4479 { 4480 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 4481 nf_conntrack_put(skb_nfct(skb)); 4482 skb->_nfct = 0; 4483 #endif 4484 } 4485 4486 static inline void nf_reset_trace(struct sk_buff *skb) 4487 { 4488 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES) 4489 skb->nf_trace = 0; 4490 #endif 4491 } 4492 4493 static inline void ipvs_reset(struct sk_buff *skb) 4494 { 4495 #if IS_ENABLED(CONFIG_IP_VS) 4496 skb->ipvs_property = 0; 4497 #endif 4498 } 4499 4500 /* Note: This doesn't put any conntrack info in dst. */ 4501 static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src, 4502 bool copy) 4503 { 4504 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 4505 dst->_nfct = src->_nfct; 4506 nf_conntrack_get(skb_nfct(src)); 4507 #endif 4508 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES) 4509 if (copy) 4510 dst->nf_trace = src->nf_trace; 4511 #endif 4512 } 4513 4514 static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src) 4515 { 4516 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 4517 nf_conntrack_put(skb_nfct(dst)); 4518 #endif 4519 dst->slow_gro = src->slow_gro; 4520 __nf_copy(dst, src, true); 4521 } 4522 4523 #ifdef CONFIG_NETWORK_SECMARK 4524 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from) 4525 { 4526 to->secmark = from->secmark; 4527 } 4528 4529 static inline void skb_init_secmark(struct sk_buff *skb) 4530 { 4531 skb->secmark = 0; 4532 } 4533 #else 4534 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from) 4535 { } 4536 4537 static inline void skb_init_secmark(struct sk_buff *skb) 4538 { } 4539 #endif 4540 4541 static inline int secpath_exists(const struct sk_buff *skb) 4542 { 4543 #ifdef CONFIG_XFRM 4544 return skb_ext_exist(skb, SKB_EXT_SEC_PATH); 4545 #else 4546 return 0; 4547 #endif 4548 } 4549 4550 static inline bool skb_irq_freeable(const struct sk_buff *skb) 4551 { 4552 return !skb->destructor && 4553 !secpath_exists(skb) && 4554 !skb_nfct(skb) && 4555 !skb->_skb_refdst && 4556 !skb_has_frag_list(skb); 4557 } 4558 4559 static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping) 4560 { 4561 skb->queue_mapping = queue_mapping; 4562 } 4563 4564 static inline u16 skb_get_queue_mapping(const struct sk_buff *skb) 4565 { 4566 return skb->queue_mapping; 4567 } 4568 4569 static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from) 4570 { 4571 to->queue_mapping = from->queue_mapping; 4572 } 4573 4574 static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue) 4575 { 4576 skb->queue_mapping = rx_queue + 1; 4577 } 4578 4579 static inline u16 skb_get_rx_queue(const struct sk_buff *skb) 4580 { 4581 return skb->queue_mapping - 1; 4582 } 4583 4584 static inline bool skb_rx_queue_recorded(const struct sk_buff *skb) 4585 { 4586 return skb->queue_mapping != 0; 4587 } 4588 4589 static inline void skb_set_dst_pending_confirm(struct sk_buff *skb, u32 val) 4590 { 4591 skb->dst_pending_confirm = val; 4592 } 4593 4594 static inline bool skb_get_dst_pending_confirm(const struct sk_buff *skb) 4595 { 4596 return skb->dst_pending_confirm != 0; 4597 } 4598 4599 static inline struct sec_path *skb_sec_path(const struct sk_buff *skb) 4600 { 4601 #ifdef CONFIG_XFRM 4602 return skb_ext_find(skb, SKB_EXT_SEC_PATH); 4603 #else 4604 return NULL; 4605 #endif 4606 } 4607 4608 /* Keeps track of mac header offset relative to skb->head. 4609 * It is useful for TSO of Tunneling protocol. e.g. GRE. 4610 * For non-tunnel skb it points to skb_mac_header() and for 4611 * tunnel skb it points to outer mac header. 4612 * Keeps track of level of encapsulation of network headers. 4613 */ 4614 struct skb_gso_cb { 4615 union { 4616 int mac_offset; 4617 int data_offset; 4618 }; 4619 int encap_level; 4620 __wsum csum; 4621 __u16 csum_start; 4622 }; 4623 #define SKB_GSO_CB_OFFSET 32 4624 #define SKB_GSO_CB(skb) ((struct skb_gso_cb *)((skb)->cb + SKB_GSO_CB_OFFSET)) 4625 4626 static inline int skb_tnl_header_len(const struct sk_buff *inner_skb) 4627 { 4628 return (skb_mac_header(inner_skb) - inner_skb->head) - 4629 SKB_GSO_CB(inner_skb)->mac_offset; 4630 } 4631 4632 static inline int gso_pskb_expand_head(struct sk_buff *skb, int extra) 4633 { 4634 int new_headroom, headroom; 4635 int ret; 4636 4637 headroom = skb_headroom(skb); 4638 ret = pskb_expand_head(skb, extra, 0, GFP_ATOMIC); 4639 if (ret) 4640 return ret; 4641 4642 new_headroom = skb_headroom(skb); 4643 SKB_GSO_CB(skb)->mac_offset += (new_headroom - headroom); 4644 return 0; 4645 } 4646 4647 static inline void gso_reset_checksum(struct sk_buff *skb, __wsum res) 4648 { 4649 /* Do not update partial checksums if remote checksum is enabled. */ 4650 if (skb->remcsum_offload) 4651 return; 4652 4653 SKB_GSO_CB(skb)->csum = res; 4654 SKB_GSO_CB(skb)->csum_start = skb_checksum_start(skb) - skb->head; 4655 } 4656 4657 /* Compute the checksum for a gso segment. First compute the checksum value 4658 * from the start of transport header to SKB_GSO_CB(skb)->csum_start, and 4659 * then add in skb->csum (checksum from csum_start to end of packet). 4660 * skb->csum and csum_start are then updated to reflect the checksum of the 4661 * resultant packet starting from the transport header-- the resultant checksum 4662 * is in the res argument (i.e. normally zero or ~ of checksum of a pseudo 4663 * header. 4664 */ 4665 static inline __sum16 gso_make_checksum(struct sk_buff *skb, __wsum res) 4666 { 4667 unsigned char *csum_start = skb_transport_header(skb); 4668 int plen = (skb->head + SKB_GSO_CB(skb)->csum_start) - csum_start; 4669 __wsum partial = SKB_GSO_CB(skb)->csum; 4670 4671 SKB_GSO_CB(skb)->csum = res; 4672 SKB_GSO_CB(skb)->csum_start = csum_start - skb->head; 4673 4674 return csum_fold(csum_partial(csum_start, plen, partial)); 4675 } 4676 4677 static inline bool skb_is_gso(const struct sk_buff *skb) 4678 { 4679 return skb_shinfo(skb)->gso_size; 4680 } 4681 4682 /* Note: Should be called only if skb_is_gso(skb) is true */ 4683 static inline bool skb_is_gso_v6(const struct sk_buff *skb) 4684 { 4685 return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6; 4686 } 4687 4688 /* Note: Should be called only if skb_is_gso(skb) is true */ 4689 static inline bool skb_is_gso_sctp(const struct sk_buff *skb) 4690 { 4691 return skb_shinfo(skb)->gso_type & SKB_GSO_SCTP; 4692 } 4693 4694 /* Note: Should be called only if skb_is_gso(skb) is true */ 4695 static inline bool skb_is_gso_tcp(const struct sk_buff *skb) 4696 { 4697 return skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6); 4698 } 4699 4700 static inline void skb_gso_reset(struct sk_buff *skb) 4701 { 4702 skb_shinfo(skb)->gso_size = 0; 4703 skb_shinfo(skb)->gso_segs = 0; 4704 skb_shinfo(skb)->gso_type = 0; 4705 } 4706 4707 static inline void skb_increase_gso_size(struct skb_shared_info *shinfo, 4708 u16 increment) 4709 { 4710 if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS)) 4711 return; 4712 shinfo->gso_size += increment; 4713 } 4714 4715 static inline void skb_decrease_gso_size(struct skb_shared_info *shinfo, 4716 u16 decrement) 4717 { 4718 if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS)) 4719 return; 4720 shinfo->gso_size -= decrement; 4721 } 4722 4723 void __skb_warn_lro_forwarding(const struct sk_buff *skb); 4724 4725 static inline bool skb_warn_if_lro(const struct sk_buff *skb) 4726 { 4727 /* LRO sets gso_size but not gso_type, whereas if GSO is really 4728 * wanted then gso_type will be set. */ 4729 const struct skb_shared_info *shinfo = skb_shinfo(skb); 4730 4731 if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 && 4732 unlikely(shinfo->gso_type == 0)) { 4733 __skb_warn_lro_forwarding(skb); 4734 return true; 4735 } 4736 return false; 4737 } 4738 4739 static inline void skb_forward_csum(struct sk_buff *skb) 4740 { 4741 /* Unfortunately we don't support this one. Any brave souls? */ 4742 if (skb->ip_summed == CHECKSUM_COMPLETE) 4743 skb->ip_summed = CHECKSUM_NONE; 4744 } 4745 4746 /** 4747 * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE 4748 * @skb: skb to check 4749 * 4750 * fresh skbs have their ip_summed set to CHECKSUM_NONE. 4751 * Instead of forcing ip_summed to CHECKSUM_NONE, we can 4752 * use this helper, to document places where we make this assertion. 4753 */ 4754 static inline void skb_checksum_none_assert(const struct sk_buff *skb) 4755 { 4756 #ifdef DEBUG 4757 BUG_ON(skb->ip_summed != CHECKSUM_NONE); 4758 #endif 4759 } 4760 4761 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off); 4762 4763 int skb_checksum_setup(struct sk_buff *skb, bool recalculate); 4764 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 4765 unsigned int transport_len, 4766 __sum16(*skb_chkf)(struct sk_buff *skb)); 4767 4768 /** 4769 * skb_head_is_locked - Determine if the skb->head is locked down 4770 * @skb: skb to check 4771 * 4772 * The head on skbs build around a head frag can be removed if they are 4773 * not cloned. This function returns true if the skb head is locked down 4774 * due to either being allocated via kmalloc, or by being a clone with 4775 * multiple references to the head. 4776 */ 4777 static inline bool skb_head_is_locked(const struct sk_buff *skb) 4778 { 4779 return !skb->head_frag || skb_cloned(skb); 4780 } 4781 4782 /* Local Checksum Offload. 4783 * Compute outer checksum based on the assumption that the 4784 * inner checksum will be offloaded later. 4785 * See Documentation/networking/checksum-offloads.rst for 4786 * explanation of how this works. 4787 * Fill in outer checksum adjustment (e.g. with sum of outer 4788 * pseudo-header) before calling. 4789 * Also ensure that inner checksum is in linear data area. 4790 */ 4791 static inline __wsum lco_csum(struct sk_buff *skb) 4792 { 4793 unsigned char *csum_start = skb_checksum_start(skb); 4794 unsigned char *l4_hdr = skb_transport_header(skb); 4795 __wsum partial; 4796 4797 /* Start with complement of inner checksum adjustment */ 4798 partial = ~csum_unfold(*(__force __sum16 *)(csum_start + 4799 skb->csum_offset)); 4800 4801 /* Add in checksum of our headers (incl. outer checksum 4802 * adjustment filled in by caller) and return result. 4803 */ 4804 return csum_partial(l4_hdr, csum_start - l4_hdr, partial); 4805 } 4806 4807 static inline bool skb_is_redirected(const struct sk_buff *skb) 4808 { 4809 return skb->redirected; 4810 } 4811 4812 static inline void skb_set_redirected(struct sk_buff *skb, bool from_ingress) 4813 { 4814 skb->redirected = 1; 4815 #ifdef CONFIG_NET_REDIRECT 4816 skb->from_ingress = from_ingress; 4817 if (skb->from_ingress) 4818 skb->tstamp = 0; 4819 #endif 4820 } 4821 4822 static inline void skb_reset_redirect(struct sk_buff *skb) 4823 { 4824 skb->redirected = 0; 4825 } 4826 4827 static inline bool skb_csum_is_sctp(struct sk_buff *skb) 4828 { 4829 return skb->csum_not_inet; 4830 } 4831 4832 static inline void skb_set_kcov_handle(struct sk_buff *skb, 4833 const u64 kcov_handle) 4834 { 4835 #ifdef CONFIG_KCOV 4836 skb->kcov_handle = kcov_handle; 4837 #endif 4838 } 4839 4840 static inline u64 skb_get_kcov_handle(struct sk_buff *skb) 4841 { 4842 #ifdef CONFIG_KCOV 4843 return skb->kcov_handle; 4844 #else 4845 return 0; 4846 #endif 4847 } 4848 4849 #ifdef CONFIG_PAGE_POOL 4850 static inline void skb_mark_for_recycle(struct sk_buff *skb) 4851 { 4852 skb->pp_recycle = 1; 4853 } 4854 #endif 4855 4856 static inline bool skb_pp_recycle(struct sk_buff *skb, void *data) 4857 { 4858 if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) 4859 return false; 4860 return page_pool_return_skb_page(virt_to_page(data)); 4861 } 4862 4863 #endif /* __KERNEL__ */ 4864 #endif /* _LINUX_SKBUFF_H */ 4865