1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 3 #ifndef _NET_GRO_H 4 #define _NET_GRO_H 5 6 #include <linux/indirect_call_wrapper.h> 7 #include <linux/ip.h> 8 #include <linux/ipv6.h> 9 #include <net/ip6_checksum.h> 10 #include <linux/skbuff.h> 11 #include <net/udp.h> 12 #include <net/hotdata.h> 13 14 /* This should be increased if a protocol with a bigger head is added. */ 15 #define GRO_MAX_HEAD (MAX_HEADER + 128) 16 17 struct napi_gro_cb { 18 union { 19 struct { 20 /* Virtual address of skb_shinfo(skb)->frags[0].page + offset. */ 21 void *frag0; 22 23 /* Length of frag0. */ 24 unsigned int frag0_len; 25 }; 26 27 struct { 28 /* used in skb_gro_receive() slow path */ 29 struct sk_buff *last; 30 31 /* jiffies when first packet was created/queued */ 32 unsigned long age; 33 }; 34 }; 35 36 /* This indicates where we are processing relative to skb->data. */ 37 int data_offset; 38 39 /* This is non-zero if the packet cannot be merged with the new skb. */ 40 u16 flush; 41 42 /* Number of segments aggregated. */ 43 u16 count; 44 45 /* Used in ipv6_gro_receive() and foo-over-udp and esp-in-udp */ 46 u16 proto; 47 48 u16 pad; 49 50 /* Used in napi_gro_cb::free */ 51 #define NAPI_GRO_FREE 1 52 #define NAPI_GRO_FREE_STOLEN_HEAD 2 53 /* portion of the cb set to zero at every gro iteration */ 54 struct_group(zeroed, 55 56 /* Start offset for remote checksum offload */ 57 u16 gro_remcsum_start; 58 59 /* This is non-zero if the packet may be of the same flow. */ 60 u8 same_flow:1; 61 62 /* Used in tunnel GRO receive */ 63 u8 encap_mark:1; 64 65 /* GRO checksum is valid */ 66 u8 csum_valid:1; 67 68 /* Number of checksums via CHECKSUM_UNNECESSARY */ 69 u8 csum_cnt:3; 70 71 /* Free the skb? */ 72 u8 free:2; 73 74 /* Used in GRE, set in fou/gue_gro_receive */ 75 u8 is_fou:1; 76 77 /* Used to determine if ipid_offset can be ignored */ 78 u8 ip_fixedid:2; 79 80 /* Number of gro_receive callbacks this packet already went through */ 81 u8 recursion_counter:4; 82 83 /* GRO is done by frag_list pointer chaining. */ 84 u8 is_flist:1; 85 ); 86 87 /* used to support CHECKSUM_COMPLETE for tunneling protocols */ 88 __wsum csum; 89 90 /* L3 offsets */ 91 union { 92 struct { 93 u16 network_offset; 94 u16 inner_network_offset; 95 }; 96 u16 network_offsets[2]; 97 }; 98 }; 99 100 #define NAPI_GRO_CB(skb) ((struct napi_gro_cb *)(skb)->cb) 101 102 #define GRO_RECURSION_LIMIT 15 103 static inline int gro_recursion_inc_test(struct sk_buff *skb) 104 { 105 return ++NAPI_GRO_CB(skb)->recursion_counter == GRO_RECURSION_LIMIT; 106 } 107 108 typedef struct sk_buff *(*gro_receive_t)(struct list_head *, struct sk_buff *); 109 static inline struct sk_buff *call_gro_receive(gro_receive_t cb, 110 struct list_head *head, 111 struct sk_buff *skb) 112 { 113 if (unlikely(gro_recursion_inc_test(skb))) { 114 NAPI_GRO_CB(skb)->flush |= 1; 115 return NULL; 116 } 117 118 return cb(head, skb); 119 } 120 121 typedef struct sk_buff *(*gro_receive_sk_t)(struct sock *, struct list_head *, 122 struct sk_buff *); 123 static inline struct sk_buff *call_gro_receive_sk(gro_receive_sk_t cb, 124 struct sock *sk, 125 struct list_head *head, 126 struct sk_buff *skb) 127 { 128 if (unlikely(gro_recursion_inc_test(skb))) { 129 NAPI_GRO_CB(skb)->flush |= 1; 130 return NULL; 131 } 132 133 return cb(sk, head, skb); 134 } 135 136 static inline unsigned int skb_gro_offset(const struct sk_buff *skb) 137 { 138 return NAPI_GRO_CB(skb)->data_offset; 139 } 140 141 static inline unsigned int skb_gro_len(const struct sk_buff *skb) 142 { 143 return skb->len - NAPI_GRO_CB(skb)->data_offset; 144 } 145 146 static inline void skb_gro_pull(struct sk_buff *skb, unsigned int len) 147 { 148 NAPI_GRO_CB(skb)->data_offset += len; 149 } 150 151 static inline void *skb_gro_header_fast(const struct sk_buff *skb, 152 unsigned int offset) 153 { 154 return NAPI_GRO_CB(skb)->frag0 + offset; 155 } 156 157 static inline bool skb_gro_may_pull(const struct sk_buff *skb, 158 unsigned int hlen) 159 { 160 return likely(hlen <= NAPI_GRO_CB(skb)->frag0_len); 161 } 162 163 /* A pull can release the page fragment frag0 points to, or reallocate 164 * skb->head, even if it fails. Call this before the pull, to make sure later 165 * skb_gro_header() and skb_gro_network_header() calls no longer use frag0. 166 */ 167 static inline void skb_gro_frag0_invalidate(struct sk_buff *skb) 168 { 169 NAPI_GRO_CB(skb)->frag0_len = 0; 170 } 171 172 static inline void *skb_gro_header_slow(struct sk_buff *skb, unsigned int hlen, 173 unsigned int offset) 174 { 175 skb_gro_frag0_invalidate(skb); 176 if (!pskb_may_pull(skb, hlen)) 177 return NULL; 178 179 return skb->data + offset; 180 } 181 182 static inline void *skb_gro_header(struct sk_buff *skb, unsigned int hlen, 183 unsigned int offset) 184 { 185 void *ptr; 186 187 ptr = skb_gro_header_fast(skb, offset); 188 if (!skb_gro_may_pull(skb, hlen)) 189 ptr = skb_gro_header_slow(skb, hlen, offset); 190 return ptr; 191 } 192 193 static inline int skb_gro_receive_network_offset(const struct sk_buff *skb) 194 { 195 return NAPI_GRO_CB(skb)->network_offsets[NAPI_GRO_CB(skb)->encap_mark]; 196 } 197 198 static inline void *skb_gro_network_header(const struct sk_buff *skb) 199 { 200 if (skb_gro_may_pull(skb, skb_gro_offset(skb))) 201 return skb_gro_header_fast(skb, skb_gro_receive_network_offset(skb)); 202 203 return skb->data + skb_gro_receive_network_offset(skb); 204 } 205 206 static inline __wsum inet_gro_compute_pseudo(const struct sk_buff *skb, 207 int proto) 208 { 209 const struct iphdr *iph = skb_gro_network_header(skb); 210 211 return csum_tcpudp_nofold(iph->saddr, iph->daddr, 212 skb_gro_len(skb), proto, 0); 213 } 214 215 static inline void skb_gro_postpull_rcsum(struct sk_buff *skb, 216 const void *start, unsigned int len) 217 { 218 if (NAPI_GRO_CB(skb)->csum_valid) 219 NAPI_GRO_CB(skb)->csum = wsum_negate(csum_partial(start, len, 220 wsum_negate(NAPI_GRO_CB(skb)->csum))); 221 } 222 223 /* GRO checksum functions. These are logical equivalents of the normal 224 * checksum functions (in skbuff.h) except that they operate on the GRO 225 * offsets and fields in sk_buff. 226 */ 227 228 __sum16 __skb_gro_checksum_complete(struct sk_buff *skb); 229 230 static inline bool skb_at_gro_remcsum_start(struct sk_buff *skb) 231 { 232 return (NAPI_GRO_CB(skb)->gro_remcsum_start == skb_gro_offset(skb)); 233 } 234 235 static inline bool __skb_gro_checksum_validate_needed(struct sk_buff *skb, 236 bool zero_okay, 237 __sum16 check) 238 { 239 return ((skb->ip_summed != CHECKSUM_PARTIAL || 240 skb_checksum_start_offset(skb) < 241 skb_gro_offset(skb)) && 242 !skb_at_gro_remcsum_start(skb) && 243 NAPI_GRO_CB(skb)->csum_cnt == 0 && 244 (!zero_okay || check)); 245 } 246 247 static inline __sum16 __skb_gro_checksum_validate_complete(struct sk_buff *skb, 248 __wsum psum) 249 { 250 if (NAPI_GRO_CB(skb)->csum_valid && 251 !csum_fold(csum_add(psum, NAPI_GRO_CB(skb)->csum))) 252 return 0; 253 254 NAPI_GRO_CB(skb)->csum = psum; 255 256 return __skb_gro_checksum_complete(skb); 257 } 258 259 static inline void skb_gro_incr_csum_unnecessary(struct sk_buff *skb) 260 { 261 if (NAPI_GRO_CB(skb)->csum_cnt > 0) { 262 /* Consume a checksum from CHECKSUM_UNNECESSARY */ 263 NAPI_GRO_CB(skb)->csum_cnt--; 264 } else { 265 /* Update skb for CHECKSUM_UNNECESSARY and csum_level when we 266 * verified a new top level checksum or an encapsulated one 267 * during GRO. This saves work if we fallback to normal path. 268 */ 269 __skb_incr_checksum_unnecessary(skb); 270 } 271 } 272 273 #define __skb_gro_checksum_validate(skb, proto, zero_okay, check, \ 274 compute_pseudo) \ 275 ({ \ 276 __sum16 __ret = 0; \ 277 if (__skb_gro_checksum_validate_needed(skb, zero_okay, check)) \ 278 __ret = __skb_gro_checksum_validate_complete(skb, \ 279 compute_pseudo(skb, proto)); \ 280 if (!__ret) \ 281 skb_gro_incr_csum_unnecessary(skb); \ 282 __ret; \ 283 }) 284 285 #define skb_gro_checksum_validate(skb, proto, compute_pseudo) \ 286 __skb_gro_checksum_validate(skb, proto, false, 0, compute_pseudo) 287 288 #define skb_gro_checksum_validate_zero_check(skb, proto, check, \ 289 compute_pseudo) \ 290 __skb_gro_checksum_validate(skb, proto, true, check, compute_pseudo) 291 292 #define skb_gro_checksum_simple_validate(skb) \ 293 __skb_gro_checksum_validate(skb, 0, false, 0, null_compute_pseudo) 294 295 static inline bool __skb_gro_checksum_convert_check(struct sk_buff *skb) 296 { 297 return (NAPI_GRO_CB(skb)->csum_cnt == 0 && 298 !NAPI_GRO_CB(skb)->csum_valid); 299 } 300 301 static inline void __skb_gro_checksum_convert(struct sk_buff *skb, 302 __wsum pseudo) 303 { 304 NAPI_GRO_CB(skb)->csum = ~pseudo; 305 NAPI_GRO_CB(skb)->csum_valid = 1; 306 } 307 308 #define skb_gro_checksum_try_convert(skb, proto, compute_pseudo) \ 309 do { \ 310 if (__skb_gro_checksum_convert_check(skb)) \ 311 __skb_gro_checksum_convert(skb, \ 312 compute_pseudo(skb, proto)); \ 313 } while (0) 314 315 struct gro_remcsum { 316 int offset; 317 __wsum delta; 318 }; 319 320 static inline void skb_gro_remcsum_init(struct gro_remcsum *grc) 321 { 322 grc->offset = 0; 323 grc->delta = 0; 324 } 325 326 static inline void *skb_gro_remcsum_process(struct sk_buff *skb, void *ptr, 327 unsigned int off, size_t hdrlen, 328 int start, int offset, 329 struct gro_remcsum *grc, 330 bool nopartial) 331 { 332 __wsum delta; 333 size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start); 334 335 BUG_ON(!NAPI_GRO_CB(skb)->csum_valid); 336 337 if (!nopartial) { 338 NAPI_GRO_CB(skb)->gro_remcsum_start = off + hdrlen + start; 339 return ptr; 340 } 341 342 ptr = skb_gro_header(skb, off + plen, off); 343 if (!ptr) 344 return NULL; 345 346 delta = remcsum_adjust(ptr + hdrlen, NAPI_GRO_CB(skb)->csum, 347 start, offset); 348 349 /* Adjust skb->csum since we changed the packet */ 350 NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta); 351 352 grc->offset = off + hdrlen + offset; 353 grc->delta = delta; 354 355 return ptr; 356 } 357 358 static inline void skb_gro_remcsum_cleanup(struct sk_buff *skb, 359 struct gro_remcsum *grc) 360 { 361 void *ptr; 362 size_t plen = grc->offset + sizeof(u16); 363 364 /* Once merged, grc->offset can not be trusted: the frags or head 365 * of this skb now belong to the GRO packet, or skb->data was pulled 366 * past the headers before adding it to a frag_list. 367 * skb_gro_receive_list() does not merge packets with an adjusted 368 * checksum, other merged packets do not use their headers anymore. 369 */ 370 if (!grc->delta || NAPI_GRO_CB(skb)->same_flow) 371 return; 372 373 ptr = skb_gro_header(skb, plen, grc->offset); 374 if (!ptr) 375 return; 376 377 remcsum_unadjust((__sum16 *)ptr, grc->delta); 378 } 379 380 #ifdef CONFIG_XFRM_OFFLOAD 381 static inline void skb_gro_flush_final(struct sk_buff *skb, struct sk_buff *pp, int flush) 382 { 383 if (PTR_ERR(pp) != -EINPROGRESS) 384 NAPI_GRO_CB(skb)->flush |= flush; 385 } 386 static inline void skb_gro_flush_final_remcsum(struct sk_buff *skb, 387 struct sk_buff *pp, 388 int flush, 389 struct gro_remcsum *grc) 390 { 391 if (PTR_ERR(pp) != -EINPROGRESS) { 392 NAPI_GRO_CB(skb)->flush |= flush; 393 skb_gro_remcsum_cleanup(skb, grc); 394 skb->remcsum_offload = 0; 395 } 396 } 397 #else 398 static inline void skb_gro_flush_final(struct sk_buff *skb, struct sk_buff *pp, int flush) 399 { 400 NAPI_GRO_CB(skb)->flush |= flush; 401 } 402 static inline void skb_gro_flush_final_remcsum(struct sk_buff *skb, 403 struct sk_buff *pp, 404 int flush, 405 struct gro_remcsum *grc) 406 { 407 NAPI_GRO_CB(skb)->flush |= flush; 408 skb_gro_remcsum_cleanup(skb, grc); 409 skb->remcsum_offload = 0; 410 } 411 #endif 412 413 INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *, 414 struct sk_buff *)); 415 INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int)); 416 INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *, 417 struct sk_buff *)); 418 INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int)); 419 420 INDIRECT_CALLABLE_DECLARE(struct sk_buff *udp4_gro_receive(struct list_head *, 421 struct sk_buff *)); 422 INDIRECT_CALLABLE_DECLARE(int udp4_gro_complete(struct sk_buff *, int)); 423 424 struct sk_buff *udp6_gro_receive(struct list_head *, struct sk_buff *); 425 int udp6_gro_complete(struct sk_buff *, int); 426 427 #define indirect_call_gro_receive_inet(cb, f2, f1, head, skb) \ 428 ({ \ 429 unlikely(gro_recursion_inc_test(skb)) ? \ 430 NAPI_GRO_CB(skb)->flush |= 1, NULL : \ 431 INDIRECT_CALL_INET(cb, f2, f1, head, skb); \ 432 }) 433 434 struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb, 435 struct udphdr *uh, struct sock *sk); 436 int udp_gro_complete(struct sk_buff *skb, int nhoff, udp_lookup_t lookup); 437 438 static inline struct udphdr *udp_gro_udphdr(struct sk_buff *skb) 439 { 440 struct udphdr *uh; 441 unsigned int hlen, off; 442 443 off = skb_gro_offset(skb); 444 hlen = off + sizeof(*uh); 445 uh = skb_gro_header(skb, hlen, off); 446 447 return uh; 448 } 449 450 static inline __wsum ip6_gro_compute_pseudo(const struct sk_buff *skb, 451 int proto) 452 { 453 const struct ipv6hdr *iph = skb_gro_network_header(skb); 454 455 return ~csum_unfold(csum_ipv6_magic(&iph->saddr, &iph->daddr, 456 skb_gro_len(skb), proto, 0)); 457 } 458 459 static inline int inet_gro_flush(const struct iphdr *iph, const struct iphdr *iph2, 460 struct sk_buff *p, bool inner) 461 { 462 const u32 id = ntohl(*(__be32 *)&iph->id); 463 const u32 id2 = ntohl(*(__be32 *)&iph2->id); 464 const u16 ipid_offset = (id >> 16) - (id2 >> 16); 465 const u16 count = NAPI_GRO_CB(p)->count; 466 467 /* All fields must match except length and checksum. */ 468 if ((iph->ttl ^ iph2->ttl) | (iph->tos ^ iph2->tos) | ((id ^ id2) & IP_DF)) 469 return true; 470 471 /* When we receive our second frame we can make a decision on if we 472 * continue this flow as an atomic flow with a fixed ID or if we use 473 * an incrementing ID. 474 */ 475 if (count == 1 && !ipid_offset) 476 NAPI_GRO_CB(p)->ip_fixedid |= 1 << inner; 477 478 return ipid_offset ^ (count * !(NAPI_GRO_CB(p)->ip_fixedid & (1 << inner))); 479 } 480 481 static inline int ipv6_gro_flush(const struct ipv6hdr *iph, const struct ipv6hdr *iph2) 482 { 483 /* <Version:4><Traffic_Class:8><Flow_Label:20> */ 484 __be32 first_word = *(__be32 *)iph ^ *(__be32 *)iph2; 485 486 /* Flush if Traffic Class fields are different. */ 487 return !!((first_word & htonl(0x0FF00000)) | 488 (__force __be32)(iph->hop_limit ^ iph2->hop_limit)); 489 } 490 491 static inline int __gro_receive_network_flush(const void *th, const void *th2, 492 struct sk_buff *p, const u16 diff, 493 bool inner) 494 { 495 const void *nh = th - diff; 496 const void *nh2 = th2 - diff; 497 498 if (((struct iphdr *)nh)->version == 6) 499 return ipv6_gro_flush(nh, nh2); 500 else 501 return inet_gro_flush(nh, nh2, p, inner); 502 } 503 504 static inline int gro_receive_network_flush(const void *th, const void *th2, 505 struct sk_buff *p) 506 { 507 int off = skb_transport_offset(p); 508 int flush; 509 510 flush = __gro_receive_network_flush(th, th2, p, off - NAPI_GRO_CB(p)->network_offset, false); 511 if (NAPI_GRO_CB(p)->encap_mark) 512 flush |= __gro_receive_network_flush(th, th2, p, off - NAPI_GRO_CB(p)->inner_network_offset, true); 513 514 return flush; 515 } 516 517 int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb); 518 int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb); 519 void __gro_flush(struct gro_node *gro, bool flush_old); 520 521 static inline void gro_flush(struct gro_node *gro, bool flush_old) 522 { 523 if (!gro->bitmask) 524 return; 525 526 __gro_flush(gro, flush_old); 527 } 528 529 static inline void napi_gro_flush(struct napi_struct *napi, bool flush_old) 530 { 531 gro_flush(&napi->gro, flush_old); 532 } 533 534 /* Pass the currently batched GRO_NORMAL SKBs up to the stack. */ 535 static inline void gro_normal_list(struct gro_node *gro) 536 { 537 if (!gro->rx_count) 538 return; 539 netif_receive_skb_list_internal(&gro->rx_list); 540 INIT_LIST_HEAD(&gro->rx_list); 541 gro->rx_count = 0; 542 } 543 544 static inline void gro_flush_normal(struct gro_node *gro, bool flush_old) 545 { 546 gro_flush(gro, flush_old); 547 gro_normal_list(gro); 548 } 549 550 /* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded, 551 * pass the whole batch up to the stack. 552 */ 553 static inline void gro_normal_one(struct gro_node *gro, struct sk_buff *skb, 554 int segs) 555 { 556 list_add_tail(&skb->list, &gro->rx_list); 557 gro->rx_count += segs; 558 if (gro->rx_count >= READ_ONCE(net_hotdata.gro_normal_batch)) 559 gro_normal_list(gro); 560 } 561 562 void gro_init(struct gro_node *gro); 563 void gro_cleanup(struct gro_node *gro); 564 565 /* This function is the alternative of 'inet_iif' and 'inet_sdif' 566 * functions in case we can not rely on fields of IPCB. 567 * 568 * The caller must verify skb_valid_dst(skb) is false and skb->dev is initialized. 569 * The caller must hold the RCU read lock. 570 */ 571 static inline void inet_get_iif_sdif(const struct sk_buff *skb, int *iif, int *sdif) 572 { 573 *iif = inet_iif(skb) ?: skb->dev->ifindex; 574 *sdif = 0; 575 576 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) 577 if (netif_is_l3_slave(skb->dev)) { 578 struct net_device *master = netdev_master_upper_dev_get_rcu(skb->dev); 579 580 *sdif = *iif; 581 *iif = master ? master->ifindex : 0; 582 } 583 #endif 584 } 585 586 /* This function is the alternative of 'inet6_iif' and 'inet6_sdif' 587 * functions in case we can not rely on fields of IP6CB. 588 * 589 * The caller must verify skb_valid_dst(skb) is false and skb->dev is initialized. 590 * The caller must hold the RCU read lock. 591 */ 592 static inline void inet6_get_iif_sdif(const struct sk_buff *skb, int *iif, int *sdif) 593 { 594 /* using skb->dev->ifindex because skb_dst(skb) is not initialized */ 595 *iif = skb->dev->ifindex; 596 *sdif = 0; 597 598 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) 599 if (netif_is_l3_slave(skb->dev)) { 600 struct net_device *master = netdev_master_upper_dev_get_rcu(skb->dev); 601 602 *sdif = *iif; 603 *iif = master ? master->ifindex : 0; 604 } 605 #endif 606 } 607 608 struct packet_offload *gro_find_receive_by_type(__be16 type); 609 struct packet_offload *gro_find_complete_by_type(__be16 type); 610 611 static inline struct tcphdr *tcp_gro_pull_header(struct sk_buff *skb) 612 { 613 unsigned int thlen, hlen, off; 614 struct tcphdr *th; 615 616 off = skb_gro_offset(skb); 617 hlen = off + sizeof(*th); 618 th = skb_gro_header(skb, hlen, off); 619 if (unlikely(!th)) 620 return NULL; 621 622 thlen = th->doff * 4; 623 if (unlikely(thlen < sizeof(*th))) 624 return NULL; 625 626 hlen = off + thlen; 627 if (!skb_gro_may_pull(skb, hlen)) { 628 th = skb_gro_header_slow(skb, hlen, off); 629 if (unlikely(!th)) 630 return NULL; 631 } 632 633 skb_gro_pull(skb, thlen); 634 635 return th; 636 } 637 638 #endif /* _NET_GRO_H */ 639