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