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