1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * IPV4 GSO/GRO offload support 4 * Linux INET implementation 5 * 6 * UDPv4 GSO support 7 */ 8 9 #include <linux/skbuff.h> 10 #include <net/gro.h> 11 #include <net/gso.h> 12 #include <net/udp.h> 13 #include <net/protocol.h> 14 #include <net/inet_common.h> 15 #include <net/udp_tunnel.h> 16 17 #if IS_ENABLED(CONFIG_NET_UDP_TUNNEL) 18 19 /* 20 * Dummy GRO tunnel callback, exists mainly to avoid dangling/NULL 21 * values for the udp tunnel static call. 22 */ 23 static struct sk_buff *dummy_gro_rcv(struct sock *sk, 24 struct list_head *head, 25 struct sk_buff *skb) 26 { 27 NAPI_GRO_CB(skb)->flush = 1; 28 return NULL; 29 } 30 31 typedef struct sk_buff *(*udp_tunnel_gro_rcv_t)(struct sock *sk, 32 struct list_head *head, 33 struct sk_buff *skb); 34 35 struct udp_tunnel_type_entry { 36 udp_tunnel_gro_rcv_t gro_receive; 37 refcount_t count; 38 }; 39 40 #define UDP_MAX_TUNNEL_TYPES (IS_ENABLED(CONFIG_GENEVE) + \ 41 IS_ENABLED(CONFIG_VXLAN) * 2 + \ 42 IS_ENABLED(CONFIG_NET_FOU) * 2 + \ 43 IS_ENABLED(CONFIG_XFRM) * 2) 44 45 DEFINE_STATIC_CALL(udp_tunnel_gro_rcv, dummy_gro_rcv); 46 static DEFINE_STATIC_KEY_FALSE(udp_tunnel_static_call); 47 static struct mutex udp_tunnel_gro_type_lock; 48 static struct udp_tunnel_type_entry udp_tunnel_gro_types[UDP_MAX_TUNNEL_TYPES]; 49 static unsigned int udp_tunnel_gro_type_nr; 50 static DEFINE_SPINLOCK(udp_tunnel_gro_lock); 51 52 void udp_tunnel_update_gro_lookup(struct net *net, struct sock *sk, bool add) 53 { 54 bool is_ipv6 = sk->sk_family == AF_INET6; 55 struct udp_sock *tup, *up = udp_sk(sk); 56 struct udp_tunnel_gro *udp_tunnel_gro; 57 58 spin_lock(&udp_tunnel_gro_lock); 59 udp_tunnel_gro = &net->ipv4.udp_tunnel_gro[is_ipv6]; 60 if (add) 61 hlist_add_head(&up->tunnel_list, &udp_tunnel_gro->list); 62 else if (up->tunnel_list.pprev) 63 hlist_del_init(&up->tunnel_list); 64 65 if (udp_tunnel_gro->list.first && 66 !udp_tunnel_gro->list.first->next) { 67 tup = hlist_entry(udp_tunnel_gro->list.first, struct udp_sock, 68 tunnel_list); 69 70 rcu_assign_pointer(udp_tunnel_gro->sk, (struct sock *)tup); 71 } else { 72 RCU_INIT_POINTER(udp_tunnel_gro->sk, NULL); 73 } 74 75 spin_unlock(&udp_tunnel_gro_lock); 76 } 77 EXPORT_SYMBOL_GPL(udp_tunnel_update_gro_lookup); 78 79 void udp_tunnel_update_gro_rcv(struct sock *sk, bool add) 80 { 81 struct udp_tunnel_type_entry *cur = NULL; 82 struct udp_sock *up = udp_sk(sk); 83 int i, old_gro_type_nr; 84 85 if (!UDP_MAX_TUNNEL_TYPES || !up->gro_receive) 86 return; 87 88 mutex_lock(&udp_tunnel_gro_type_lock); 89 90 /* Check if the static call is permanently disabled. */ 91 if (udp_tunnel_gro_type_nr > UDP_MAX_TUNNEL_TYPES) 92 goto out; 93 94 for (i = 0; i < udp_tunnel_gro_type_nr; i++) 95 if (udp_tunnel_gro_types[i].gro_receive == up->gro_receive) 96 cur = &udp_tunnel_gro_types[i]; 97 98 old_gro_type_nr = udp_tunnel_gro_type_nr; 99 if (add) { 100 /* 101 * Update the matching entry, if found, or add a new one 102 * if needed 103 */ 104 if (cur) { 105 refcount_inc(&cur->count); 106 goto out; 107 } 108 109 if (unlikely(udp_tunnel_gro_type_nr == UDP_MAX_TUNNEL_TYPES)) { 110 pr_err_once("Too many UDP tunnel types, please increase UDP_MAX_TUNNEL_TYPES\n"); 111 /* Ensure static call will never be enabled */ 112 udp_tunnel_gro_type_nr = UDP_MAX_TUNNEL_TYPES + 1; 113 } else { 114 cur = &udp_tunnel_gro_types[udp_tunnel_gro_type_nr++]; 115 refcount_set(&cur->count, 1); 116 cur->gro_receive = up->gro_receive; 117 } 118 } else { 119 /* 120 * The stack cleanups only successfully added tunnel, the 121 * lookup on removal should never fail. 122 */ 123 if (WARN_ON_ONCE(!cur)) 124 goto out; 125 126 if (!refcount_dec_and_test(&cur->count)) 127 goto out; 128 129 /* Avoid gaps, so that the enable tunnel has always id 0 */ 130 *cur = udp_tunnel_gro_types[--udp_tunnel_gro_type_nr]; 131 } 132 133 if (udp_tunnel_gro_type_nr == 1) { 134 static_call_update(udp_tunnel_gro_rcv, 135 udp_tunnel_gro_types[0].gro_receive); 136 static_branch_enable(&udp_tunnel_static_call); 137 } else if (old_gro_type_nr == 1) { 138 static_branch_disable(&udp_tunnel_static_call); 139 static_call_update(udp_tunnel_gro_rcv, dummy_gro_rcv); 140 } 141 142 out: 143 mutex_unlock(&udp_tunnel_gro_type_lock); 144 } 145 EXPORT_SYMBOL_GPL(udp_tunnel_update_gro_rcv); 146 147 static void udp_tunnel_gro_init(void) 148 { 149 mutex_init(&udp_tunnel_gro_type_lock); 150 } 151 152 static struct sk_buff *udp_tunnel_gro_rcv(struct sock *sk, 153 struct list_head *head, 154 struct sk_buff *skb) 155 { 156 if (static_branch_likely(&udp_tunnel_static_call)) { 157 if (unlikely(gro_recursion_inc_test(skb))) { 158 NAPI_GRO_CB(skb)->flush |= 1; 159 return NULL; 160 } 161 return static_call(udp_tunnel_gro_rcv)(sk, head, skb); 162 } 163 return call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb); 164 } 165 166 #else 167 168 static void udp_tunnel_gro_init(void) {} 169 170 static struct sk_buff *udp_tunnel_gro_rcv(struct sock *sk, 171 struct list_head *head, 172 struct sk_buff *skb) 173 { 174 return call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb); 175 } 176 177 #endif 178 179 static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb, 180 netdev_features_t features, 181 struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb, 182 netdev_features_t features), 183 __be16 new_protocol, bool is_ipv6) 184 { 185 int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb); 186 bool remcsum, need_csum, offload_csum, gso_partial; 187 struct sk_buff *segs = ERR_PTR(-EINVAL); 188 struct udphdr *uh = udp_hdr(skb); 189 u16 mac_offset = skb->mac_header; 190 __be16 protocol = skb->protocol; 191 u16 mac_len = skb->mac_len; 192 int udp_offset, outer_hlen; 193 __wsum partial; 194 bool need_ipsec; 195 196 if (unlikely(!pskb_may_pull(skb, tnl_hlen))) 197 goto out; 198 199 /* Adjust partial header checksum to negate old length. 200 * We cannot rely on the value contained in uh->len as it is 201 * possible that the actual value exceeds the boundaries of the 202 * 16 bit length field due to the header being added outside of an 203 * IP or IPv6 frame that was already limited to 64K - 1. 204 */ 205 if (skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) 206 partial = (__force __wsum)uh->len; 207 else 208 partial = (__force __wsum)htonl(skb->len); 209 partial = csum_sub(csum_unfold(uh->check), partial); 210 211 /* setup inner skb. */ 212 skb->encapsulation = 0; 213 SKB_GSO_CB(skb)->encap_level = 0; 214 __skb_pull(skb, tnl_hlen); 215 skb_reset_mac_header(skb); 216 skb_set_network_header(skb, skb_inner_network_offset(skb)); 217 skb_set_transport_header(skb, skb_inner_transport_offset(skb)); 218 skb->mac_len = skb_inner_network_offset(skb); 219 skb->protocol = new_protocol; 220 221 need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM); 222 skb->encap_hdr_csum = need_csum; 223 224 remcsum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TUNNEL_REMCSUM); 225 skb->remcsum_offload = remcsum; 226 227 need_ipsec = skb_dst(skb) && dst_xfrm(skb_dst(skb)); 228 /* Try to offload checksum if possible */ 229 offload_csum = !!(need_csum && 230 !need_ipsec && 231 (skb->dev->features & 232 (is_ipv6 ? (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM) : 233 (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)))); 234 235 features &= skb->dev->hw_enc_features; 236 if (need_csum) 237 features &= ~NETIF_F_SCTP_CRC; 238 239 /* The only checksum offload we care about from here on out is the 240 * outer one so strip the existing checksum feature flags and 241 * instead set the flag based on our outer checksum offload value. 242 */ 243 if (remcsum) { 244 features &= ~NETIF_F_CSUM_MASK; 245 if (!need_csum || offload_csum) 246 features |= NETIF_F_HW_CSUM; 247 } 248 249 /* segment inner packet. */ 250 segs = gso_inner_segment(skb, features); 251 if (IS_ERR_OR_NULL(segs)) { 252 skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset, 253 mac_len); 254 goto out; 255 } 256 257 gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL); 258 259 outer_hlen = skb_tnl_header_len(skb); 260 udp_offset = outer_hlen - tnl_hlen; 261 skb = segs; 262 do { 263 unsigned int len; 264 265 if (remcsum) 266 skb->ip_summed = CHECKSUM_NONE; 267 268 /* Set up inner headers if we are offloading inner checksum */ 269 if (skb->ip_summed == CHECKSUM_PARTIAL) { 270 skb_reset_inner_headers(skb); 271 skb->encapsulation = 1; 272 } 273 274 skb->mac_len = mac_len; 275 skb->protocol = protocol; 276 277 __skb_push(skb, outer_hlen); 278 skb_reset_mac_header(skb); 279 skb_set_network_header(skb, mac_len); 280 skb_set_transport_header(skb, udp_offset); 281 len = skb->len - udp_offset; 282 uh = udp_hdr(skb); 283 284 /* If we are only performing partial GSO the inner header 285 * will be using a length value equal to only one MSS sized 286 * segment instead of the entire frame. 287 */ 288 if (gso_partial && skb_is_gso(skb)) { 289 uh->len = htons(skb_shinfo(skb)->gso_size + 290 SKB_GSO_CB(skb)->data_offset + 291 skb->head - (unsigned char *)uh); 292 } else { 293 uh->len = htons(len); 294 } 295 296 if (!need_csum) 297 continue; 298 299 uh->check = ~csum_fold(csum_add(partial, 300 (__force __wsum)htonl(len))); 301 302 if (skb->encapsulation || !offload_csum) { 303 uh->check = gso_make_checksum(skb, ~uh->check); 304 if (uh->check == 0) 305 uh->check = CSUM_MANGLED_0; 306 } else { 307 skb->ip_summed = CHECKSUM_PARTIAL; 308 skb->csum_start = skb_transport_header(skb) - skb->head; 309 skb->csum_offset = offsetof(struct udphdr, check); 310 } 311 } while ((skb = skb->next)); 312 out: 313 return segs; 314 } 315 316 struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb, 317 netdev_features_t features, 318 bool is_ipv6) 319 { 320 const struct net_offload __rcu **offloads; 321 __be16 protocol = skb->protocol; 322 const struct net_offload *ops; 323 struct sk_buff *segs = ERR_PTR(-EINVAL); 324 struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb, 325 netdev_features_t features); 326 327 rcu_read_lock(); 328 329 switch (skb->inner_protocol_type) { 330 case ENCAP_TYPE_ETHER: 331 protocol = skb->inner_protocol; 332 gso_inner_segment = skb_mac_gso_segment; 333 break; 334 case ENCAP_TYPE_IPPROTO: 335 offloads = is_ipv6 ? inet6_offloads : inet_offloads; 336 ops = rcu_dereference(offloads[skb->inner_ipproto]); 337 if (!ops || !ops->callbacks.gso_segment) 338 goto out_unlock; 339 gso_inner_segment = ops->callbacks.gso_segment; 340 break; 341 default: 342 goto out_unlock; 343 } 344 345 segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment, 346 protocol, is_ipv6); 347 348 out_unlock: 349 rcu_read_unlock(); 350 351 return segs; 352 } 353 EXPORT_SYMBOL(skb_udp_tunnel_segment); 354 355 static void __udpv4_gso_segment_csum(struct sk_buff *seg, 356 __be32 *oldip, __be32 *newip, 357 __be16 *oldport, __be16 *newport) 358 { 359 struct udphdr *uh; 360 struct iphdr *iph; 361 362 if (*oldip == *newip && *oldport == *newport) 363 return; 364 365 uh = udp_hdr(seg); 366 iph = ip_hdr(seg); 367 368 if (uh->check) { 369 inet_proto_csum_replace4(&uh->check, seg, *oldip, *newip, 370 true); 371 inet_proto_csum_replace2(&uh->check, seg, *oldport, *newport, 372 false); 373 if (!uh->check) 374 uh->check = CSUM_MANGLED_0; 375 } 376 *oldport = *newport; 377 378 csum_replace4(&iph->check, *oldip, *newip); 379 *oldip = *newip; 380 } 381 382 static struct sk_buff *__udpv4_gso_segment_list_csum(struct sk_buff *segs) 383 { 384 struct sk_buff *seg; 385 struct udphdr *uh, *uh2; 386 struct iphdr *iph, *iph2; 387 388 seg = segs; 389 uh = udp_hdr(seg); 390 iph = ip_hdr(seg); 391 392 if ((udp_hdr(seg)->dest == udp_hdr(seg->next)->dest) && 393 (udp_hdr(seg)->source == udp_hdr(seg->next)->source) && 394 (ip_hdr(seg)->daddr == ip_hdr(seg->next)->daddr) && 395 (ip_hdr(seg)->saddr == ip_hdr(seg->next)->saddr)) 396 return segs; 397 398 while ((seg = seg->next)) { 399 uh2 = udp_hdr(seg); 400 iph2 = ip_hdr(seg); 401 402 __udpv4_gso_segment_csum(seg, 403 &iph2->saddr, &iph->saddr, 404 &uh2->source, &uh->source); 405 __udpv4_gso_segment_csum(seg, 406 &iph2->daddr, &iph->daddr, 407 &uh2->dest, &uh->dest); 408 } 409 410 return segs; 411 } 412 413 static void __udpv6_gso_segment_csum(struct sk_buff *seg, 414 struct in6_addr *oldip, 415 const struct in6_addr *newip, 416 __be16 *oldport, __be16 newport) 417 { 418 struct udphdr *uh = udp_hdr(seg); 419 420 if (ipv6_addr_equal(oldip, newip) && *oldport == newport) 421 return; 422 423 if (uh->check) { 424 inet_proto_csum_replace16(&uh->check, seg, oldip->s6_addr32, 425 newip->s6_addr32, true); 426 427 inet_proto_csum_replace2(&uh->check, seg, *oldport, newport, 428 false); 429 if (!uh->check) 430 uh->check = CSUM_MANGLED_0; 431 } 432 433 *oldip = *newip; 434 *oldport = newport; 435 } 436 437 static struct sk_buff *__udpv6_gso_segment_list_csum(struct sk_buff *segs) 438 { 439 const struct ipv6hdr *iph; 440 const struct udphdr *uh; 441 struct ipv6hdr *iph2; 442 struct sk_buff *seg; 443 struct udphdr *uh2; 444 445 seg = segs; 446 uh = udp_hdr(seg); 447 iph = ipv6_hdr(seg); 448 uh2 = udp_hdr(seg->next); 449 iph2 = ipv6_hdr(seg->next); 450 451 if (!(*(const u32 *)&uh->source ^ *(const u32 *)&uh2->source) && 452 ipv6_addr_equal(&iph->saddr, &iph2->saddr) && 453 ipv6_addr_equal(&iph->daddr, &iph2->daddr)) 454 return segs; 455 456 while ((seg = seg->next)) { 457 uh2 = udp_hdr(seg); 458 iph2 = ipv6_hdr(seg); 459 460 __udpv6_gso_segment_csum(seg, &iph2->saddr, &iph->saddr, 461 &uh2->source, uh->source); 462 __udpv6_gso_segment_csum(seg, &iph2->daddr, &iph->daddr, 463 &uh2->dest, uh->dest); 464 } 465 466 return segs; 467 } 468 469 static struct sk_buff *__udp_gso_segment_list(struct sk_buff *skb, 470 netdev_features_t features, 471 bool is_ipv6) 472 { 473 unsigned int mss = skb_shinfo(skb)->gso_size; 474 475 skb = skb_segment_list(skb, features, skb_mac_header_len(skb)); 476 if (IS_ERR(skb)) 477 return skb; 478 479 udp_hdr(skb)->len = htons(sizeof(struct udphdr) + mss); 480 481 if (is_ipv6) 482 return __udpv6_gso_segment_list_csum(skb); 483 else 484 return __udpv4_gso_segment_list_csum(skb); 485 } 486 487 struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb, 488 netdev_features_t features, bool is_ipv6) 489 { 490 struct sock *sk = gso_skb->sk; 491 unsigned int sum_truesize = 0; 492 struct sk_buff *segs, *seg; 493 struct udphdr *uh; 494 unsigned int mss; 495 bool copy_dtor; 496 __sum16 check; 497 __be16 newlen; 498 int ret = 0; 499 500 mss = skb_shinfo(gso_skb)->gso_size; 501 if (gso_skb->len <= sizeof(*uh) + mss) 502 return ERR_PTR(-EINVAL); 503 504 if (unlikely(skb_checksum_start(gso_skb) != 505 skb_transport_header(gso_skb) && 506 !(skb_shinfo(gso_skb)->gso_type & SKB_GSO_FRAGLIST))) 507 return ERR_PTR(-EINVAL); 508 509 /* We don't know if egress device can segment and checksum the packet 510 * when IPv6 extension headers are present. Fall back to software GSO. 511 */ 512 if (gso_skb->ip_summed != CHECKSUM_PARTIAL) 513 features &= ~(NETIF_F_GSO_UDP_L4 | NETIF_F_CSUM_MASK); 514 515 if (skb_gso_ok(gso_skb, features | NETIF_F_GSO_ROBUST)) { 516 /* Packet is from an untrusted source, reset gso_segs. */ 517 skb_shinfo(gso_skb)->gso_segs = DIV_ROUND_UP(gso_skb->len - sizeof(*uh), 518 mss); 519 return NULL; 520 } 521 522 if (skb_shinfo(gso_skb)->gso_type & SKB_GSO_FRAGLIST) { 523 /* Detect modified geometry and pass those to skb_segment. */ 524 if (skb_pagelen(gso_skb) - sizeof(*uh) == skb_shinfo(gso_skb)->gso_size) 525 return __udp_gso_segment_list(gso_skb, features, is_ipv6); 526 527 ret = __skb_linearize(gso_skb); 528 if (ret) 529 return ERR_PTR(ret); 530 531 /* Setup csum, as fraglist skips this in udp4_gro_receive. */ 532 gso_skb->csum_start = skb_transport_header(gso_skb) - gso_skb->head; 533 gso_skb->csum_offset = offsetof(struct udphdr, check); 534 gso_skb->ip_summed = CHECKSUM_PARTIAL; 535 536 uh = udp_hdr(gso_skb); 537 if (is_ipv6) 538 uh->check = ~udp_v6_check(gso_skb->len, 539 &ipv6_hdr(gso_skb)->saddr, 540 &ipv6_hdr(gso_skb)->daddr, 0); 541 else 542 uh->check = ~udp_v4_check(gso_skb->len, 543 ip_hdr(gso_skb)->saddr, 544 ip_hdr(gso_skb)->daddr, 0); 545 } 546 547 skb_pull(gso_skb, sizeof(*uh)); 548 549 /* clear destructor to avoid skb_segment assigning it to tail */ 550 copy_dtor = gso_skb->destructor == sock_wfree; 551 if (copy_dtor) { 552 gso_skb->destructor = NULL; 553 gso_skb->sk = NULL; 554 } 555 556 segs = skb_segment(gso_skb, features); 557 if (IS_ERR_OR_NULL(segs)) { 558 if (copy_dtor) { 559 gso_skb->destructor = sock_wfree; 560 gso_skb->sk = sk; 561 } 562 return segs; 563 } 564 565 /* GSO partial and frag_list segmentation only requires splitting 566 * the frame into an MSS multiple and possibly a remainder, both 567 * cases return a GSO skb. So update the mss now. 568 */ 569 if (skb_is_gso(segs)) 570 mss *= skb_shinfo(segs)->gso_segs; 571 572 seg = segs; 573 uh = udp_hdr(seg); 574 575 /* preserve TX timestamp flags and TS key for first segment */ 576 skb_shinfo(seg)->tskey = skb_shinfo(gso_skb)->tskey; 577 skb_shinfo(seg)->tx_flags |= 578 (skb_shinfo(gso_skb)->tx_flags & SKBTX_ANY_TSTAMP); 579 580 /* compute checksum adjustment based on old length versus new */ 581 newlen = htons(sizeof(*uh) + mss); 582 check = csum16_add(csum16_sub(uh->check, uh->len), newlen); 583 584 for (;;) { 585 if (copy_dtor) { 586 seg->destructor = sock_wfree; 587 seg->sk = sk; 588 sum_truesize += seg->truesize; 589 } 590 591 if (!seg->next) 592 break; 593 594 uh->len = newlen; 595 uh->check = check; 596 597 if (seg->ip_summed == CHECKSUM_PARTIAL) 598 gso_reset_checksum(seg, ~check); 599 else 600 uh->check = gso_make_checksum(seg, ~check) ? : 601 CSUM_MANGLED_0; 602 603 seg = seg->next; 604 uh = udp_hdr(seg); 605 } 606 607 /* last packet can be partial gso_size, account for that in checksum */ 608 newlen = htons(skb_tail_pointer(seg) - skb_transport_header(seg) + 609 seg->data_len); 610 check = csum16_add(csum16_sub(uh->check, uh->len), newlen); 611 612 uh->len = newlen; 613 uh->check = check; 614 615 if (seg->ip_summed == CHECKSUM_PARTIAL) 616 gso_reset_checksum(seg, ~check); 617 else 618 uh->check = gso_make_checksum(seg, ~check) ? : CSUM_MANGLED_0; 619 620 /* On the TX path, CHECKSUM_NONE and CHECKSUM_UNNECESSARY have the same 621 * meaning. However, check for bad offloads in the GSO stack expects the 622 * latter, if the checksum was calculated in software. To vouch for the 623 * segment skbs we actually need to set it on the gso_skb. 624 */ 625 if (gso_skb->ip_summed == CHECKSUM_NONE) 626 gso_skb->ip_summed = CHECKSUM_UNNECESSARY; 627 628 /* update refcount for the packet */ 629 if (copy_dtor) { 630 int delta = sum_truesize - gso_skb->truesize; 631 632 /* In some pathological cases, delta can be negative. 633 * We need to either use refcount_add() or refcount_sub_and_test() 634 */ 635 if (likely(delta >= 0)) 636 refcount_add(delta, &sk->sk_wmem_alloc); 637 else 638 WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc)); 639 } 640 return segs; 641 } 642 EXPORT_SYMBOL_GPL(__udp_gso_segment); 643 644 static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, 645 netdev_features_t features) 646 { 647 struct sk_buff *segs = ERR_PTR(-EINVAL); 648 unsigned int mss; 649 __wsum csum; 650 struct udphdr *uh; 651 struct iphdr *iph; 652 653 if (skb->encapsulation && 654 (skb_shinfo(skb)->gso_type & 655 (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) { 656 segs = skb_udp_tunnel_segment(skb, features, false); 657 goto out; 658 } 659 660 if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_UDP | SKB_GSO_UDP_L4))) 661 goto out; 662 663 if (!pskb_may_pull(skb, sizeof(struct udphdr))) 664 goto out; 665 666 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) 667 return __udp_gso_segment(skb, features, false); 668 669 mss = skb_shinfo(skb)->gso_size; 670 if (unlikely(skb->len <= mss)) 671 goto out; 672 673 /* Do software UFO. Complete and fill in the UDP checksum as 674 * HW cannot do checksum of UDP packets sent as multiple 675 * IP fragments. 676 */ 677 678 uh = udp_hdr(skb); 679 iph = ip_hdr(skb); 680 681 uh->check = 0; 682 csum = skb_checksum(skb, 0, skb->len, 0); 683 uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum); 684 if (uh->check == 0) 685 uh->check = CSUM_MANGLED_0; 686 687 skb->ip_summed = CHECKSUM_UNNECESSARY; 688 689 /* If there is no outer header we can fake a checksum offload 690 * due to the fact that we have already done the checksum in 691 * software prior to segmenting the frame. 692 */ 693 if (!skb->encap_hdr_csum) 694 features |= NETIF_F_HW_CSUM; 695 696 /* Fragment the skb. IP headers of the fragments are updated in 697 * inet_gso_segment() 698 */ 699 segs = skb_segment(skb, features); 700 out: 701 return segs; 702 } 703 704 705 #define UDP_GRO_CNT_MAX 64 706 static struct sk_buff *udp_gro_receive_segment(struct list_head *head, 707 struct sk_buff *skb) 708 { 709 struct udphdr *uh = udp_gro_udphdr(skb); 710 struct sk_buff *pp = NULL; 711 struct udphdr *uh2; 712 struct sk_buff *p; 713 unsigned int ulen; 714 int ret = 0; 715 int flush; 716 717 /* requires non zero csum, for symmetry with GSO */ 718 if (!uh->check) { 719 NAPI_GRO_CB(skb)->flush = 1; 720 return NULL; 721 } 722 723 /* Do not deal with padded or malicious packets, sorry ! */ 724 ulen = ntohs(uh->len); 725 if (ulen <= sizeof(*uh) || ulen != skb_gro_len(skb)) { 726 NAPI_GRO_CB(skb)->flush = 1; 727 return NULL; 728 } 729 /* pull encapsulating udp header */ 730 skb_gro_pull(skb, sizeof(struct udphdr)); 731 732 list_for_each_entry(p, head, list) { 733 if (!NAPI_GRO_CB(p)->same_flow) 734 continue; 735 736 uh2 = udp_hdr(p); 737 738 /* Match ports only, as csum is always non zero */ 739 if ((*(u32 *)&uh->source != *(u32 *)&uh2->source)) { 740 NAPI_GRO_CB(p)->same_flow = 0; 741 continue; 742 } 743 744 if (NAPI_GRO_CB(skb)->is_flist != NAPI_GRO_CB(p)->is_flist) { 745 NAPI_GRO_CB(skb)->flush = 1; 746 return p; 747 } 748 749 flush = gro_receive_network_flush(uh, uh2, p); 750 751 /* Terminate the flow on len mismatch or if it grow "too much". 752 * Under small packet flood GRO count could elsewhere grow a lot 753 * leading to excessive truesize values. 754 * On len mismatch merge the first packet shorter than gso_size, 755 * otherwise complete the GRO packet. 756 */ 757 if (ulen > ntohs(uh2->len) || flush) { 758 pp = p; 759 } else { 760 if (NAPI_GRO_CB(skb)->is_flist) { 761 if (!pskb_may_pull(skb, skb_gro_offset(skb))) { 762 NAPI_GRO_CB(skb)->flush = 1; 763 return NULL; 764 } 765 if ((skb->ip_summed != p->ip_summed) || 766 (skb->csum_level != p->csum_level)) { 767 NAPI_GRO_CB(skb)->flush = 1; 768 return NULL; 769 } 770 ret = skb_gro_receive_list(p, skb); 771 } else { 772 skb_gro_postpull_rcsum(skb, uh, 773 sizeof(struct udphdr)); 774 775 ret = skb_gro_receive(p, skb); 776 } 777 } 778 779 if (ret || ulen != ntohs(uh2->len) || 780 NAPI_GRO_CB(p)->count >= UDP_GRO_CNT_MAX) 781 pp = p; 782 783 return pp; 784 } 785 786 /* mismatch, but we never need to flush */ 787 return NULL; 788 } 789 790 struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb, 791 struct udphdr *uh, struct sock *sk) 792 { 793 struct sk_buff *pp = NULL; 794 struct sk_buff *p; 795 struct udphdr *uh2; 796 unsigned int off = skb_gro_offset(skb); 797 int flush = 1; 798 799 /* We can do L4 aggregation only if the packet can't land in a tunnel 800 * otherwise we could corrupt the inner stream. Detecting such packets 801 * cannot be foolproof and the aggregation might still happen in some 802 * cases. Such packets should be caught in udp_unexpected_gso later. 803 */ 804 NAPI_GRO_CB(skb)->is_flist = 0; 805 if (!sk || !udp_sk(sk)->gro_receive) { 806 /* If the packet was locally encapsulated in a UDP tunnel that 807 * wasn't detected above, do not GRO. 808 */ 809 if (skb->encapsulation) 810 goto out; 811 812 if (skb->dev->features & NETIF_F_GRO_FRAGLIST) 813 NAPI_GRO_CB(skb)->is_flist = sk ? !udp_test_bit(GRO_ENABLED, sk) : 1; 814 815 if ((!sk && (skb->dev->features & NETIF_F_GRO_UDP_FWD)) || 816 (sk && udp_test_bit(GRO_ENABLED, sk)) || NAPI_GRO_CB(skb)->is_flist) 817 return call_gro_receive(udp_gro_receive_segment, head, skb); 818 819 /* no GRO, be sure flush the current packet */ 820 goto out; 821 } 822 823 if (NAPI_GRO_CB(skb)->encap_mark || 824 (uh->check && skb->ip_summed != CHECKSUM_PARTIAL && 825 NAPI_GRO_CB(skb)->csum_cnt == 0 && 826 !NAPI_GRO_CB(skb)->csum_valid)) 827 goto out; 828 829 /* mark that this skb passed once through the tunnel gro layer */ 830 NAPI_GRO_CB(skb)->encap_mark = 1; 831 832 flush = 0; 833 834 list_for_each_entry(p, head, list) { 835 if (!NAPI_GRO_CB(p)->same_flow) 836 continue; 837 838 uh2 = (struct udphdr *)(p->data + off); 839 840 /* Match ports and either checksums are either both zero 841 * or nonzero. 842 */ 843 if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) || 844 (!uh->check ^ !uh2->check)) { 845 NAPI_GRO_CB(p)->same_flow = 0; 846 continue; 847 } 848 } 849 850 skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */ 851 skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr)); 852 pp = udp_tunnel_gro_rcv(sk, head, skb); 853 854 out: 855 skb_gro_flush_final(skb, pp, flush); 856 return pp; 857 } 858 EXPORT_SYMBOL(udp_gro_receive); 859 860 static struct sock *udp4_gro_lookup_skb(struct sk_buff *skb, __be16 sport, 861 __be16 dport) 862 { 863 const struct iphdr *iph = skb_gro_network_header(skb); 864 struct net *net = dev_net_rcu(skb->dev); 865 struct sock *sk; 866 int iif, sdif; 867 868 sk = udp_tunnel_sk(net, false); 869 if (sk && dport == htons(sk->sk_num)) 870 return sk; 871 872 inet_get_iif_sdif(skb, &iif, &sdif); 873 874 return __udp4_lib_lookup(net, iph->saddr, sport, 875 iph->daddr, dport, iif, 876 sdif, net->ipv4.udp_table, NULL); 877 } 878 879 INDIRECT_CALLABLE_SCOPE 880 struct sk_buff *udp4_gro_receive(struct list_head *head, struct sk_buff *skb) 881 { 882 struct udphdr *uh = udp_gro_udphdr(skb); 883 struct sock *sk = NULL; 884 struct sk_buff *pp; 885 886 if (unlikely(!uh)) 887 goto flush; 888 889 /* Don't bother verifying checksum if we're going to flush anyway. */ 890 if (NAPI_GRO_CB(skb)->flush) 891 goto skip; 892 893 if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check, 894 inet_gro_compute_pseudo)) 895 goto flush; 896 else if (uh->check) 897 skb_gro_checksum_try_convert(skb, IPPROTO_UDP, 898 inet_gro_compute_pseudo); 899 skip: 900 NAPI_GRO_CB(skb)->is_ipv6 = 0; 901 902 if (static_branch_unlikely(&udp_encap_needed_key)) 903 sk = udp4_gro_lookup_skb(skb, uh->source, uh->dest); 904 905 pp = udp_gro_receive(head, skb, uh, sk); 906 return pp; 907 908 flush: 909 NAPI_GRO_CB(skb)->flush = 1; 910 return NULL; 911 } 912 913 static int udp_gro_complete_segment(struct sk_buff *skb) 914 { 915 struct udphdr *uh = udp_hdr(skb); 916 917 skb->csum_start = (unsigned char *)uh - skb->head; 918 skb->csum_offset = offsetof(struct udphdr, check); 919 skb->ip_summed = CHECKSUM_PARTIAL; 920 921 skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count; 922 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_L4; 923 924 if (skb->encapsulation) 925 skb->inner_transport_header = skb->transport_header; 926 927 return 0; 928 } 929 930 int udp_gro_complete(struct sk_buff *skb, int nhoff, 931 udp_lookup_t lookup) 932 { 933 __be16 newlen = htons(skb->len - nhoff); 934 struct udphdr *uh = (struct udphdr *)(skb->data + nhoff); 935 struct sock *sk; 936 int err; 937 938 uh->len = newlen; 939 940 sk = INDIRECT_CALL_INET(lookup, udp6_lib_lookup_skb, 941 udp4_lib_lookup_skb, skb, uh->source, uh->dest); 942 if (sk && udp_sk(sk)->gro_complete) { 943 skb_shinfo(skb)->gso_type = uh->check ? SKB_GSO_UDP_TUNNEL_CSUM 944 : SKB_GSO_UDP_TUNNEL; 945 946 /* clear the encap mark, so that inner frag_list gro_complete 947 * can take place 948 */ 949 NAPI_GRO_CB(skb)->encap_mark = 0; 950 951 /* Set encapsulation before calling into inner gro_complete() 952 * functions to make them set up the inner offsets. 953 */ 954 skb->encapsulation = 1; 955 err = udp_sk(sk)->gro_complete(sk, skb, 956 nhoff + sizeof(struct udphdr)); 957 } else { 958 err = udp_gro_complete_segment(skb); 959 } 960 961 if (skb->remcsum_offload) 962 skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM; 963 964 return err; 965 } 966 EXPORT_SYMBOL(udp_gro_complete); 967 968 INDIRECT_CALLABLE_SCOPE int udp4_gro_complete(struct sk_buff *skb, int nhoff) 969 { 970 const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation]; 971 const struct iphdr *iph = (struct iphdr *)(skb->data + offset); 972 struct udphdr *uh = (struct udphdr *)(skb->data + nhoff); 973 974 /* do fraglist only if there is no outer UDP encap (or we already processed it) */ 975 if (NAPI_GRO_CB(skb)->is_flist && !NAPI_GRO_CB(skb)->encap_mark) { 976 uh->len = htons(skb->len - nhoff); 977 978 skb_shinfo(skb)->gso_type |= (SKB_GSO_FRAGLIST|SKB_GSO_UDP_L4); 979 skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count; 980 981 __skb_incr_checksum_unnecessary(skb); 982 983 return 0; 984 } 985 986 if (uh->check) 987 uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr, 988 iph->daddr, 0); 989 990 return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb); 991 } 992 993 int __init udpv4_offload_init(void) 994 { 995 net_hotdata.udpv4_offload = (struct net_offload) { 996 .callbacks = { 997 .gso_segment = udp4_ufo_fragment, 998 .gro_receive = udp4_gro_receive, 999 .gro_complete = udp4_gro_complete, 1000 }, 1001 }; 1002 1003 udp_tunnel_gro_init(); 1004 return inet_add_offload(&net_hotdata.udpv4_offload, IPPROTO_UDP); 1005 } 1006