1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * UDP over IPv6 4 * Linux INET6 implementation 5 * 6 * Authors: 7 * Pedro Roque <roque@di.fc.ul.pt> 8 * 9 * Based on linux/ipv4/udp.c 10 * 11 * Fixes: 12 * Hideaki YOSHIFUJI : sin6_scope_id support 13 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 14 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind 15 * a single port at the same time. 16 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data 17 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file. 18 */ 19 20 #include <linux/errno.h> 21 #include <linux/types.h> 22 #include <linux/socket.h> 23 #include <linux/sockios.h> 24 #include <linux/net.h> 25 #include <linux/in6.h> 26 #include <linux/netdevice.h> 27 #include <linux/if_arp.h> 28 #include <linux/ipv6.h> 29 #include <linux/icmpv6.h> 30 #include <linux/init.h> 31 #include <linux/module.h> 32 #include <linux/skbuff.h> 33 #include <linux/slab.h> 34 #include <linux/uaccess.h> 35 #include <linux/indirect_call_wrapper.h> 36 37 #include <net/addrconf.h> 38 #include <net/ndisc.h> 39 #include <net/protocol.h> 40 #include <net/transp_v6.h> 41 #include <net/ip6_route.h> 42 #include <net/raw.h> 43 #include <net/seg6.h> 44 #include <net/tcp_states.h> 45 #include <net/ip6_checksum.h> 46 #include <net/ip6_tunnel.h> 47 #include <net/xfrm.h> 48 #include <net/inet_hashtables.h> 49 #include <net/inet6_hashtables.h> 50 #include <net/busy_poll.h> 51 #include <net/sock_reuseport.h> 52 53 #include <linux/proc_fs.h> 54 #include <linux/seq_file.h> 55 #include <trace/events/skb.h> 56 #include "udp_impl.h" 57 58 static u32 udp6_ehashfn(const struct net *net, 59 const struct in6_addr *laddr, 60 const u16 lport, 61 const struct in6_addr *faddr, 62 const __be16 fport) 63 { 64 static u32 udp6_ehash_secret __read_mostly; 65 static u32 udp_ipv6_hash_secret __read_mostly; 66 67 u32 lhash, fhash; 68 69 net_get_random_once(&udp6_ehash_secret, 70 sizeof(udp6_ehash_secret)); 71 net_get_random_once(&udp_ipv6_hash_secret, 72 sizeof(udp_ipv6_hash_secret)); 73 74 lhash = (__force u32)laddr->s6_addr32[3]; 75 fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret); 76 77 return __inet6_ehashfn(lhash, lport, fhash, fport, 78 udp_ipv6_hash_secret + net_hash_mix(net)); 79 } 80 81 int udp_v6_get_port(struct sock *sk, unsigned short snum) 82 { 83 unsigned int hash2_nulladdr = 84 ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum); 85 unsigned int hash2_partial = 86 ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0); 87 88 /* precompute partial secondary hash */ 89 udp_sk(sk)->udp_portaddr_hash = hash2_partial; 90 return udp_lib_get_port(sk, snum, hash2_nulladdr); 91 } 92 93 void udp_v6_rehash(struct sock *sk) 94 { 95 u16 new_hash = ipv6_portaddr_hash(sock_net(sk), 96 &sk->sk_v6_rcv_saddr, 97 inet_sk(sk)->inet_num); 98 99 udp_lib_rehash(sk, new_hash); 100 } 101 102 static int compute_score(struct sock *sk, struct net *net, 103 const struct in6_addr *saddr, __be16 sport, 104 const struct in6_addr *daddr, unsigned short hnum, 105 int dif, int sdif) 106 { 107 int score; 108 struct inet_sock *inet; 109 bool dev_match; 110 111 if (!net_eq(sock_net(sk), net) || 112 udp_sk(sk)->udp_port_hash != hnum || 113 sk->sk_family != PF_INET6) 114 return -1; 115 116 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) 117 return -1; 118 119 score = 0; 120 inet = inet_sk(sk); 121 122 if (inet->inet_dport) { 123 if (inet->inet_dport != sport) 124 return -1; 125 score++; 126 } 127 128 if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 129 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr)) 130 return -1; 131 score++; 132 } 133 134 dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif); 135 if (!dev_match) 136 return -1; 137 if (sk->sk_bound_dev_if) 138 score++; 139 140 if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 141 score++; 142 143 return score; 144 } 145 146 static struct sock *lookup_reuseport(struct net *net, struct sock *sk, 147 struct sk_buff *skb, 148 const struct in6_addr *saddr, 149 __be16 sport, 150 const struct in6_addr *daddr, 151 unsigned int hnum) 152 { 153 struct sock *reuse_sk = NULL; 154 u32 hash; 155 156 if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) { 157 hash = udp6_ehashfn(net, daddr, hnum, saddr, sport); 158 reuse_sk = reuseport_select_sock(sk, hash, skb, 159 sizeof(struct udphdr)); 160 } 161 return reuse_sk; 162 } 163 164 /* called with rcu_read_lock() */ 165 static struct sock *udp6_lib_lookup2(struct net *net, 166 const struct in6_addr *saddr, __be16 sport, 167 const struct in6_addr *daddr, unsigned int hnum, 168 int dif, int sdif, struct udp_hslot *hslot2, 169 struct sk_buff *skb) 170 { 171 struct sock *sk, *result; 172 int score, badness; 173 174 result = NULL; 175 badness = -1; 176 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 177 score = compute_score(sk, net, saddr, sport, 178 daddr, hnum, dif, sdif); 179 if (score > badness) { 180 result = lookup_reuseport(net, sk, skb, 181 saddr, sport, daddr, hnum); 182 /* Fall back to scoring if group has connections */ 183 if (result && !reuseport_has_conns(sk, false)) 184 return result; 185 186 result = result ? : sk; 187 badness = score; 188 } 189 } 190 return result; 191 } 192 193 static inline struct sock *udp6_lookup_run_bpf(struct net *net, 194 struct udp_table *udptable, 195 struct sk_buff *skb, 196 const struct in6_addr *saddr, 197 __be16 sport, 198 const struct in6_addr *daddr, 199 u16 hnum) 200 { 201 struct sock *sk, *reuse_sk; 202 bool no_reuseport; 203 204 if (udptable != &udp_table) 205 return NULL; /* only UDP is supported */ 206 207 no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP, 208 saddr, sport, daddr, hnum, &sk); 209 if (no_reuseport || IS_ERR_OR_NULL(sk)) 210 return sk; 211 212 reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum); 213 if (reuse_sk) 214 sk = reuse_sk; 215 return sk; 216 } 217 218 /* rcu_read_lock() must be held */ 219 struct sock *__udp6_lib_lookup(struct net *net, 220 const struct in6_addr *saddr, __be16 sport, 221 const struct in6_addr *daddr, __be16 dport, 222 int dif, int sdif, struct udp_table *udptable, 223 struct sk_buff *skb) 224 { 225 unsigned short hnum = ntohs(dport); 226 unsigned int hash2, slot2; 227 struct udp_hslot *hslot2; 228 struct sock *result, *sk; 229 230 hash2 = ipv6_portaddr_hash(net, daddr, hnum); 231 slot2 = hash2 & udptable->mask; 232 hslot2 = &udptable->hash2[slot2]; 233 234 /* Lookup connected or non-wildcard sockets */ 235 result = udp6_lib_lookup2(net, saddr, sport, 236 daddr, hnum, dif, sdif, 237 hslot2, skb); 238 if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED) 239 goto done; 240 241 /* Lookup redirect from BPF */ 242 if (static_branch_unlikely(&bpf_sk_lookup_enabled)) { 243 sk = udp6_lookup_run_bpf(net, udptable, skb, 244 saddr, sport, daddr, hnum); 245 if (sk) { 246 result = sk; 247 goto done; 248 } 249 } 250 251 /* Got non-wildcard socket or error on first lookup */ 252 if (result) 253 goto done; 254 255 /* Lookup wildcard sockets */ 256 hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum); 257 slot2 = hash2 & udptable->mask; 258 hslot2 = &udptable->hash2[slot2]; 259 260 result = udp6_lib_lookup2(net, saddr, sport, 261 &in6addr_any, hnum, dif, sdif, 262 hslot2, skb); 263 done: 264 if (IS_ERR(result)) 265 return NULL; 266 return result; 267 } 268 EXPORT_SYMBOL_GPL(__udp6_lib_lookup); 269 270 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, 271 __be16 sport, __be16 dport, 272 struct udp_table *udptable) 273 { 274 const struct ipv6hdr *iph = ipv6_hdr(skb); 275 276 return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 277 &iph->daddr, dport, inet6_iif(skb), 278 inet6_sdif(skb), udptable, skb); 279 } 280 281 struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb, 282 __be16 sport, __be16 dport) 283 { 284 const struct ipv6hdr *iph = ipv6_hdr(skb); 285 286 return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 287 &iph->daddr, dport, inet6_iif(skb), 288 inet6_sdif(skb), &udp_table, NULL); 289 } 290 291 /* Must be called under rcu_read_lock(). 292 * Does increment socket refcount. 293 */ 294 #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6) 295 struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport, 296 const struct in6_addr *daddr, __be16 dport, int dif) 297 { 298 struct sock *sk; 299 300 sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport, 301 dif, 0, &udp_table, NULL); 302 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 303 sk = NULL; 304 return sk; 305 } 306 EXPORT_SYMBOL_GPL(udp6_lib_lookup); 307 #endif 308 309 /* do not use the scratch area len for jumbogram: their length execeeds the 310 * scratch area space; note that the IP6CB flags is still in the first 311 * cacheline, so checking for jumbograms is cheap 312 */ 313 static int udp6_skb_len(struct sk_buff *skb) 314 { 315 return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb); 316 } 317 318 /* 319 * This should be easy, if there is something there we 320 * return it, otherwise we block. 321 */ 322 323 int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 324 int noblock, int flags, int *addr_len) 325 { 326 struct ipv6_pinfo *np = inet6_sk(sk); 327 struct inet_sock *inet = inet_sk(sk); 328 struct sk_buff *skb; 329 unsigned int ulen, copied; 330 int off, err, peeking = flags & MSG_PEEK; 331 int is_udplite = IS_UDPLITE(sk); 332 struct udp_mib __percpu *mib; 333 bool checksum_valid = false; 334 int is_udp4; 335 336 if (flags & MSG_ERRQUEUE) 337 return ipv6_recv_error(sk, msg, len, addr_len); 338 339 if (np->rxpmtu && np->rxopt.bits.rxpmtu) 340 return ipv6_recv_rxpmtu(sk, msg, len, addr_len); 341 342 try_again: 343 off = sk_peek_offset(sk, flags); 344 skb = __skb_recv_udp(sk, flags, noblock, &off, &err); 345 if (!skb) 346 return err; 347 if (ccs_socket_post_recvmsg_permission(sk, skb, flags)) 348 return -EAGAIN; /* Hope less harmful than -EPERM. */ 349 350 ulen = udp6_skb_len(skb); 351 copied = len; 352 if (copied > ulen - off) 353 copied = ulen - off; 354 else if (copied < ulen) 355 msg->msg_flags |= MSG_TRUNC; 356 357 is_udp4 = (skb->protocol == htons(ETH_P_IP)); 358 mib = __UDPX_MIB(sk, is_udp4); 359 360 /* 361 * If checksum is needed at all, try to do it while copying the 362 * data. If the data is truncated, or if we only want a partial 363 * coverage checksum (UDP-Lite), do it before the copy. 364 */ 365 366 if (copied < ulen || peeking || 367 (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 368 checksum_valid = udp_skb_csum_unnecessary(skb) || 369 !__udp_lib_checksum_complete(skb); 370 if (!checksum_valid) 371 goto csum_copy_err; 372 } 373 374 if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 375 if (udp_skb_is_linear(skb)) 376 err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 377 else 378 err = skb_copy_datagram_msg(skb, off, msg, copied); 379 } else { 380 err = skb_copy_and_csum_datagram_msg(skb, off, msg); 381 if (err == -EINVAL) 382 goto csum_copy_err; 383 } 384 if (unlikely(err)) { 385 if (!peeking) { 386 atomic_inc(&sk->sk_drops); 387 SNMP_INC_STATS(mib, UDP_MIB_INERRORS); 388 } 389 kfree_skb(skb); 390 return err; 391 } 392 if (!peeking) 393 SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS); 394 395 sock_recv_ts_and_drops(msg, sk, skb); 396 397 /* Copy the address. */ 398 if (msg->msg_name) { 399 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 400 sin6->sin6_family = AF_INET6; 401 sin6->sin6_port = udp_hdr(skb)->source; 402 sin6->sin6_flowinfo = 0; 403 404 if (is_udp4) { 405 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, 406 &sin6->sin6_addr); 407 sin6->sin6_scope_id = 0; 408 } else { 409 sin6->sin6_addr = ipv6_hdr(skb)->saddr; 410 sin6->sin6_scope_id = 411 ipv6_iface_scope_id(&sin6->sin6_addr, 412 inet6_iif(skb)); 413 } 414 *addr_len = sizeof(*sin6); 415 416 BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, 417 (struct sockaddr *)sin6); 418 } 419 420 if (udp_sk(sk)->gro_enabled) 421 udp_cmsg_recv(msg, sk, skb); 422 423 if (np->rxopt.all) 424 ip6_datagram_recv_common_ctl(sk, msg, skb); 425 426 if (is_udp4) { 427 if (inet->cmsg_flags) 428 ip_cmsg_recv_offset(msg, sk, skb, 429 sizeof(struct udphdr), off); 430 } else { 431 if (np->rxopt.all) 432 ip6_datagram_recv_specific_ctl(sk, msg, skb); 433 } 434 435 err = copied; 436 if (flags & MSG_TRUNC) 437 err = ulen; 438 439 skb_consume_udp(sk, skb, peeking ? -err : err); 440 return err; 441 442 csum_copy_err: 443 if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 444 udp_skb_destructor)) { 445 SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS); 446 SNMP_INC_STATS(mib, UDP_MIB_INERRORS); 447 } 448 kfree_skb(skb); 449 450 /* starting over for a new packet, but check if we need to yield */ 451 cond_resched(); 452 msg->msg_flags &= ~MSG_TRUNC; 453 goto try_again; 454 } 455 456 DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key); 457 void udpv6_encap_enable(void) 458 { 459 static_branch_inc(&udpv6_encap_needed_key); 460 } 461 EXPORT_SYMBOL(udpv6_encap_enable); 462 463 /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 464 * through error handlers in encapsulations looking for a match. 465 */ 466 static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb, 467 struct inet6_skb_parm *opt, 468 u8 type, u8 code, int offset, __be32 info) 469 { 470 int i; 471 472 for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 473 int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, 474 u8 type, u8 code, int offset, __be32 info); 475 const struct ip6_tnl_encap_ops *encap; 476 477 encap = rcu_dereference(ip6tun_encaps[i]); 478 if (!encap) 479 continue; 480 handler = encap->err_handler; 481 if (handler && !handler(skb, opt, type, code, offset, info)) 482 return 0; 483 } 484 485 return -ENOENT; 486 } 487 488 /* Try to match ICMP errors to UDP tunnels by looking up a socket without 489 * reversing source and destination port: this will match tunnels that force the 490 * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 491 * lwtunnels might actually break this assumption by being configured with 492 * different destination ports on endpoints, in this case we won't be able to 493 * trace ICMP messages back to them. 494 * 495 * If this doesn't match any socket, probe tunnels with arbitrary destination 496 * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 497 * we've sent packets to won't necessarily match the local destination port. 498 * 499 * Then ask the tunnel implementation to match the error against a valid 500 * association. 501 * 502 * Return an error if we can't find a match, the socket if we need further 503 * processing, zero otherwise. 504 */ 505 static struct sock *__udp6_lib_err_encap(struct net *net, 506 const struct ipv6hdr *hdr, int offset, 507 struct udphdr *uh, 508 struct udp_table *udptable, 509 struct sock *sk, 510 struct sk_buff *skb, 511 struct inet6_skb_parm *opt, 512 u8 type, u8 code, __be32 info) 513 { 514 int (*lookup)(struct sock *sk, struct sk_buff *skb); 515 int network_offset, transport_offset; 516 struct udp_sock *up; 517 518 network_offset = skb_network_offset(skb); 519 transport_offset = skb_transport_offset(skb); 520 521 /* Network header needs to point to the outer IPv6 header inside ICMP */ 522 skb_reset_network_header(skb); 523 524 /* Transport header needs to point to the UDP header */ 525 skb_set_transport_header(skb, offset); 526 527 if (sk) { 528 up = udp_sk(sk); 529 530 lookup = READ_ONCE(up->encap_err_lookup); 531 if (lookup && lookup(sk, skb)) 532 sk = NULL; 533 534 goto out; 535 } 536 537 sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source, 538 &hdr->saddr, uh->dest, 539 inet6_iif(skb), 0, udptable, skb); 540 if (sk) { 541 up = udp_sk(sk); 542 543 lookup = READ_ONCE(up->encap_err_lookup); 544 if (!lookup || lookup(sk, skb)) 545 sk = NULL; 546 } 547 548 out: 549 if (!sk) { 550 sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code, 551 offset, info)); 552 } 553 554 skb_set_transport_header(skb, transport_offset); 555 skb_set_network_header(skb, network_offset); 556 557 return sk; 558 } 559 560 int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 561 u8 type, u8 code, int offset, __be32 info, 562 struct udp_table *udptable) 563 { 564 struct ipv6_pinfo *np; 565 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; 566 const struct in6_addr *saddr = &hdr->saddr; 567 const struct in6_addr *daddr = seg6_get_daddr(skb, opt) ? : &hdr->daddr; 568 struct udphdr *uh = (struct udphdr *)(skb->data+offset); 569 bool tunnel = false; 570 struct sock *sk; 571 int harderr; 572 int err; 573 struct net *net = dev_net(skb->dev); 574 575 sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source, 576 inet6_iif(skb), inet6_sdif(skb), udptable, NULL); 577 578 if (!sk || udp_sk(sk)->encap_type) { 579 /* No socket for error: try tunnels before discarding */ 580 if (static_branch_unlikely(&udpv6_encap_needed_key)) { 581 sk = __udp6_lib_err_encap(net, hdr, offset, uh, 582 udptable, sk, skb, 583 opt, type, code, info); 584 if (!sk) 585 return 0; 586 } else 587 sk = ERR_PTR(-ENOENT); 588 589 if (IS_ERR(sk)) { 590 __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), 591 ICMP6_MIB_INERRORS); 592 return PTR_ERR(sk); 593 } 594 595 tunnel = true; 596 } 597 598 harderr = icmpv6_err_convert(type, code, &err); 599 np = inet6_sk(sk); 600 601 if (type == ICMPV6_PKT_TOOBIG) { 602 if (!ip6_sk_accept_pmtu(sk)) 603 goto out; 604 ip6_sk_update_pmtu(skb, sk, info); 605 if (np->pmtudisc != IPV6_PMTUDISC_DONT) 606 harderr = 1; 607 } 608 if (type == NDISC_REDIRECT) { 609 if (tunnel) { 610 ip6_redirect(skb, sock_net(sk), inet6_iif(skb), 611 sk->sk_mark, sk->sk_uid); 612 } else { 613 ip6_sk_redirect(skb, sk); 614 } 615 goto out; 616 } 617 618 /* Tunnels don't have an application socket: don't pass errors back */ 619 if (tunnel) 620 goto out; 621 622 if (!np->recverr) { 623 if (!harderr || sk->sk_state != TCP_ESTABLISHED) 624 goto out; 625 } else { 626 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); 627 } 628 629 sk->sk_err = err; 630 sk_error_report(sk); 631 out: 632 return 0; 633 } 634 635 static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 636 { 637 int rc; 638 639 if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 640 sock_rps_save_rxhash(sk, skb); 641 sk_mark_napi_id(sk, skb); 642 sk_incoming_cpu_update(sk); 643 } else { 644 sk_mark_napi_id_once(sk, skb); 645 } 646 647 rc = __udp_enqueue_schedule_skb(sk, skb); 648 if (rc < 0) { 649 int is_udplite = IS_UDPLITE(sk); 650 651 /* Note that an ENOMEM error is charged twice */ 652 if (rc == -ENOMEM) 653 UDP6_INC_STATS(sock_net(sk), 654 UDP_MIB_RCVBUFERRORS, is_udplite); 655 else 656 UDP6_INC_STATS(sock_net(sk), 657 UDP_MIB_MEMERRORS, is_udplite); 658 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 659 kfree_skb(skb); 660 return -1; 661 } 662 663 return 0; 664 } 665 666 static __inline__ int udpv6_err(struct sk_buff *skb, 667 struct inet6_skb_parm *opt, u8 type, 668 u8 code, int offset, __be32 info) 669 { 670 return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table); 671 } 672 673 static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 674 { 675 struct udp_sock *up = udp_sk(sk); 676 int is_udplite = IS_UDPLITE(sk); 677 678 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) 679 goto drop; 680 681 if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) { 682 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 683 684 /* 685 * This is an encapsulation socket so pass the skb to 686 * the socket's udp_encap_rcv() hook. Otherwise, just 687 * fall through and pass this up the UDP socket. 688 * up->encap_rcv() returns the following value: 689 * =0 if skb was successfully passed to the encap 690 * handler or was discarded by it. 691 * >0 if skb should be passed on to UDP. 692 * <0 if skb should be resubmitted as proto -N 693 */ 694 695 /* if we're overly short, let UDP handle it */ 696 encap_rcv = READ_ONCE(up->encap_rcv); 697 if (encap_rcv) { 698 int ret; 699 700 /* Verify checksum before giving to encap */ 701 if (udp_lib_checksum_complete(skb)) 702 goto csum_error; 703 704 ret = encap_rcv(sk, skb); 705 if (ret <= 0) { 706 __UDP6_INC_STATS(sock_net(sk), 707 UDP_MIB_INDATAGRAMS, 708 is_udplite); 709 return -ret; 710 } 711 } 712 713 /* FALLTHROUGH -- it's a UDP Packet */ 714 } 715 716 /* 717 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). 718 */ 719 if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 720 721 if (up->pcrlen == 0) { /* full coverage was set */ 722 net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n", 723 UDP_SKB_CB(skb)->cscov, skb->len); 724 goto drop; 725 } 726 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 727 net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n", 728 UDP_SKB_CB(skb)->cscov, up->pcrlen); 729 goto drop; 730 } 731 } 732 733 prefetch(&sk->sk_rmem_alloc); 734 if (rcu_access_pointer(sk->sk_filter) && 735 udp_lib_checksum_complete(skb)) 736 goto csum_error; 737 738 if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) 739 goto drop; 740 741 udp_csum_pull_header(skb); 742 743 skb_dst_drop(skb); 744 745 return __udpv6_queue_rcv_skb(sk, skb); 746 747 csum_error: 748 __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 749 drop: 750 __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 751 atomic_inc(&sk->sk_drops); 752 kfree_skb(skb); 753 return -1; 754 } 755 756 static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 757 { 758 struct sk_buff *next, *segs; 759 int ret; 760 761 if (likely(!udp_unexpected_gso(sk, skb))) 762 return udpv6_queue_rcv_one_skb(sk, skb); 763 764 __skb_push(skb, -skb_mac_offset(skb)); 765 segs = udp_rcv_segment(sk, skb, false); 766 skb_list_walk_safe(segs, skb, next) { 767 __skb_pull(skb, skb_transport_offset(skb)); 768 769 udp_post_segment_fix_csum(skb); 770 ret = udpv6_queue_rcv_one_skb(sk, skb); 771 if (ret > 0) 772 ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret, 773 true); 774 } 775 return 0; 776 } 777 778 static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk, 779 __be16 loc_port, const struct in6_addr *loc_addr, 780 __be16 rmt_port, const struct in6_addr *rmt_addr, 781 int dif, int sdif, unsigned short hnum) 782 { 783 struct inet_sock *inet = inet_sk(sk); 784 785 if (!net_eq(sock_net(sk), net)) 786 return false; 787 788 if (udp_sk(sk)->udp_port_hash != hnum || 789 sk->sk_family != PF_INET6 || 790 (inet->inet_dport && inet->inet_dport != rmt_port) || 791 (!ipv6_addr_any(&sk->sk_v6_daddr) && 792 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || 793 !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) || 794 (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) && 795 !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))) 796 return false; 797 if (!inet6_mc_check(sk, loc_addr, rmt_addr)) 798 return false; 799 return true; 800 } 801 802 static void udp6_csum_zero_error(struct sk_buff *skb) 803 { 804 /* RFC 2460 section 8.1 says that we SHOULD log 805 * this error. Well, it is reasonable. 806 */ 807 net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n", 808 &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source), 809 &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest)); 810 } 811 812 /* 813 * Note: called only from the BH handler context, 814 * so we don't need to lock the hashes. 815 */ 816 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 817 const struct in6_addr *saddr, const struct in6_addr *daddr, 818 struct udp_table *udptable, int proto) 819 { 820 struct sock *sk, *first = NULL; 821 const struct udphdr *uh = udp_hdr(skb); 822 unsigned short hnum = ntohs(uh->dest); 823 struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 824 unsigned int offset = offsetof(typeof(*sk), sk_node); 825 unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 826 int dif = inet6_iif(skb); 827 int sdif = inet6_sdif(skb); 828 struct hlist_node *node; 829 struct sk_buff *nskb; 830 831 if (use_hash2) { 832 hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) & 833 udptable->mask; 834 hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask; 835 start_lookup: 836 hslot = &udptable->hash2[hash2]; 837 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 838 } 839 840 sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 841 if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr, 842 uh->source, saddr, dif, sdif, 843 hnum)) 844 continue; 845 /* If zero checksum and no_check is not on for 846 * the socket then skip it. 847 */ 848 if (!uh->check && !udp_sk(sk)->no_check6_rx) 849 continue; 850 if (!first) { 851 first = sk; 852 continue; 853 } 854 nskb = skb_clone(skb, GFP_ATOMIC); 855 if (unlikely(!nskb)) { 856 atomic_inc(&sk->sk_drops); 857 __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 858 IS_UDPLITE(sk)); 859 __UDP6_INC_STATS(net, UDP_MIB_INERRORS, 860 IS_UDPLITE(sk)); 861 continue; 862 } 863 864 if (udpv6_queue_rcv_skb(sk, nskb) > 0) 865 consume_skb(nskb); 866 } 867 868 /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 869 if (use_hash2 && hash2 != hash2_any) { 870 hash2 = hash2_any; 871 goto start_lookup; 872 } 873 874 if (first) { 875 if (udpv6_queue_rcv_skb(first, skb) > 0) 876 consume_skb(skb); 877 } else { 878 kfree_skb(skb); 879 __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 880 proto == IPPROTO_UDPLITE); 881 } 882 return 0; 883 } 884 885 static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 886 { 887 if (udp_sk_rx_dst_set(sk, dst)) { 888 const struct rt6_info *rt = (const struct rt6_info *)dst; 889 890 sk->sk_rx_dst_cookie = rt6_get_cookie(rt); 891 } 892 } 893 894 /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 895 * return code conversion for ip layer consumption 896 */ 897 static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 898 struct udphdr *uh) 899 { 900 int ret; 901 902 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 903 skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo); 904 905 ret = udpv6_queue_rcv_skb(sk, skb); 906 907 /* a return value > 0 means to resubmit the input */ 908 if (ret > 0) 909 return ret; 910 return 0; 911 } 912 913 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 914 int proto) 915 { 916 const struct in6_addr *saddr, *daddr; 917 struct net *net = dev_net(skb->dev); 918 struct udphdr *uh; 919 struct sock *sk; 920 bool refcounted; 921 u32 ulen = 0; 922 923 if (!pskb_may_pull(skb, sizeof(struct udphdr))) 924 goto discard; 925 926 saddr = &ipv6_hdr(skb)->saddr; 927 daddr = &ipv6_hdr(skb)->daddr; 928 uh = udp_hdr(skb); 929 930 ulen = ntohs(uh->len); 931 if (ulen > skb->len) 932 goto short_packet; 933 934 if (proto == IPPROTO_UDP) { 935 /* UDP validates ulen. */ 936 937 /* Check for jumbo payload */ 938 if (ulen == 0) 939 ulen = skb->len; 940 941 if (ulen < sizeof(*uh)) 942 goto short_packet; 943 944 if (ulen < skb->len) { 945 if (pskb_trim_rcsum(skb, ulen)) 946 goto short_packet; 947 saddr = &ipv6_hdr(skb)->saddr; 948 daddr = &ipv6_hdr(skb)->daddr; 949 uh = udp_hdr(skb); 950 } 951 } 952 953 if (udp6_csum_init(skb, uh, proto)) 954 goto csum_error; 955 956 /* Check if the socket is already available, e.g. due to early demux */ 957 sk = skb_steal_sock(skb, &refcounted); 958 if (sk) { 959 struct dst_entry *dst = skb_dst(skb); 960 int ret; 961 962 if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst)) 963 udp6_sk_rx_dst_set(sk, dst); 964 965 if (!uh->check && !udp_sk(sk)->no_check6_rx) { 966 if (refcounted) 967 sock_put(sk); 968 goto report_csum_error; 969 } 970 971 ret = udp6_unicast_rcv_skb(sk, skb, uh); 972 if (refcounted) 973 sock_put(sk); 974 return ret; 975 } 976 977 /* 978 * Multicast receive code 979 */ 980 if (ipv6_addr_is_multicast(daddr)) 981 return __udp6_lib_mcast_deliver(net, skb, 982 saddr, daddr, udptable, proto); 983 984 /* Unicast */ 985 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 986 if (sk) { 987 if (!uh->check && !udp_sk(sk)->no_check6_rx) 988 goto report_csum_error; 989 return udp6_unicast_rcv_skb(sk, skb, uh); 990 } 991 992 if (!uh->check) 993 goto report_csum_error; 994 995 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 996 goto discard; 997 998 if (udp_lib_checksum_complete(skb)) 999 goto csum_error; 1000 1001 __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 1002 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); 1003 1004 kfree_skb(skb); 1005 return 0; 1006 1007 short_packet: 1008 net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n", 1009 proto == IPPROTO_UDPLITE ? "-Lite" : "", 1010 saddr, ntohs(uh->source), 1011 ulen, skb->len, 1012 daddr, ntohs(uh->dest)); 1013 goto discard; 1014 1015 report_csum_error: 1016 udp6_csum_zero_error(skb); 1017 csum_error: 1018 __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 1019 discard: 1020 __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 1021 kfree_skb(skb); 1022 return 0; 1023 } 1024 1025 1026 static struct sock *__udp6_lib_demux_lookup(struct net *net, 1027 __be16 loc_port, const struct in6_addr *loc_addr, 1028 __be16 rmt_port, const struct in6_addr *rmt_addr, 1029 int dif, int sdif) 1030 { 1031 unsigned short hnum = ntohs(loc_port); 1032 unsigned int hash2 = ipv6_portaddr_hash(net, loc_addr, hnum); 1033 unsigned int slot2 = hash2 & udp_table.mask; 1034 struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; 1035 const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); 1036 struct sock *sk; 1037 1038 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 1039 if (sk->sk_state == TCP_ESTABLISHED && 1040 INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif)) 1041 return sk; 1042 /* Only check first socket in chain */ 1043 break; 1044 } 1045 return NULL; 1046 } 1047 1048 INDIRECT_CALLABLE_SCOPE void udp_v6_early_demux(struct sk_buff *skb) 1049 { 1050 struct net *net = dev_net(skb->dev); 1051 const struct udphdr *uh; 1052 struct sock *sk; 1053 struct dst_entry *dst; 1054 int dif = skb->dev->ifindex; 1055 int sdif = inet6_sdif(skb); 1056 1057 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 1058 sizeof(struct udphdr))) 1059 return; 1060 1061 uh = udp_hdr(skb); 1062 1063 if (skb->pkt_type == PACKET_HOST) 1064 sk = __udp6_lib_demux_lookup(net, uh->dest, 1065 &ipv6_hdr(skb)->daddr, 1066 uh->source, &ipv6_hdr(skb)->saddr, 1067 dif, sdif); 1068 else 1069 return; 1070 1071 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 1072 return; 1073 1074 skb->sk = sk; 1075 skb->destructor = sock_efree; 1076 dst = rcu_dereference(sk->sk_rx_dst); 1077 1078 if (dst) 1079 dst = dst_check(dst, sk->sk_rx_dst_cookie); 1080 if (dst) { 1081 /* set noref for now. 1082 * any place which wants to hold dst has to call 1083 * dst_hold_safe() 1084 */ 1085 skb_dst_set_noref(skb, dst); 1086 } 1087 } 1088 1089 INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb) 1090 { 1091 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP); 1092 } 1093 1094 /* 1095 * Throw away all pending data and cancel the corking. Socket is locked. 1096 */ 1097 static void udp_v6_flush_pending_frames(struct sock *sk) 1098 { 1099 struct udp_sock *up = udp_sk(sk); 1100 1101 if (up->pending == AF_INET) 1102 udp_flush_pending_frames(sk); 1103 else if (up->pending) { 1104 up->len = 0; 1105 up->pending = 0; 1106 ip6_flush_pending_frames(sk); 1107 } 1108 } 1109 1110 static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr, 1111 int addr_len) 1112 { 1113 if (addr_len < offsetofend(struct sockaddr, sa_family)) 1114 return -EINVAL; 1115 /* The following checks are replicated from __ip6_datagram_connect() 1116 * and intended to prevent BPF program called below from accessing 1117 * bytes that are out of the bound specified by user in addr_len. 1118 */ 1119 if (uaddr->sa_family == AF_INET) { 1120 if (__ipv6_only_sock(sk)) 1121 return -EAFNOSUPPORT; 1122 return udp_pre_connect(sk, uaddr, addr_len); 1123 } 1124 1125 if (addr_len < SIN6_LEN_RFC2133) 1126 return -EINVAL; 1127 1128 return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr); 1129 } 1130 1131 /** 1132 * udp6_hwcsum_outgoing - handle outgoing HW checksumming 1133 * @sk: socket we are sending on 1134 * @skb: sk_buff containing the filled-in UDP header 1135 * (checksum field must be zeroed out) 1136 * @saddr: source address 1137 * @daddr: destination address 1138 * @len: length of packet 1139 */ 1140 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 1141 const struct in6_addr *saddr, 1142 const struct in6_addr *daddr, int len) 1143 { 1144 unsigned int offset; 1145 struct udphdr *uh = udp_hdr(skb); 1146 struct sk_buff *frags = skb_shinfo(skb)->frag_list; 1147 __wsum csum = 0; 1148 1149 if (!frags) { 1150 /* Only one fragment on the socket. */ 1151 skb->csum_start = skb_transport_header(skb) - skb->head; 1152 skb->csum_offset = offsetof(struct udphdr, check); 1153 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0); 1154 } else { 1155 /* 1156 * HW-checksum won't work as there are two or more 1157 * fragments on the socket so that all csums of sk_buffs 1158 * should be together 1159 */ 1160 offset = skb_transport_offset(skb); 1161 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 1162 csum = skb->csum; 1163 1164 skb->ip_summed = CHECKSUM_NONE; 1165 1166 do { 1167 csum = csum_add(csum, frags->csum); 1168 } while ((frags = frags->next)); 1169 1170 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 1171 csum); 1172 if (uh->check == 0) 1173 uh->check = CSUM_MANGLED_0; 1174 } 1175 } 1176 1177 /* 1178 * Sending 1179 */ 1180 1181 static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6, 1182 struct inet_cork *cork) 1183 { 1184 struct sock *sk = skb->sk; 1185 struct udphdr *uh; 1186 int err = 0; 1187 int is_udplite = IS_UDPLITE(sk); 1188 __wsum csum = 0; 1189 int offset = skb_transport_offset(skb); 1190 int len = skb->len - offset; 1191 int datalen = len - sizeof(*uh); 1192 1193 /* 1194 * Create a UDP header 1195 */ 1196 uh = udp_hdr(skb); 1197 uh->source = fl6->fl6_sport; 1198 uh->dest = fl6->fl6_dport; 1199 uh->len = htons(len); 1200 uh->check = 0; 1201 1202 if (cork->gso_size) { 1203 const int hlen = skb_network_header_len(skb) + 1204 sizeof(struct udphdr); 1205 1206 if (hlen + cork->gso_size > cork->fragsize) { 1207 kfree_skb(skb); 1208 return -EINVAL; 1209 } 1210 if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) { 1211 kfree_skb(skb); 1212 return -EINVAL; 1213 } 1214 if (udp_sk(sk)->no_check6_tx) { 1215 kfree_skb(skb); 1216 return -EINVAL; 1217 } 1218 if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 1219 dst_xfrm(skb_dst(skb))) { 1220 kfree_skb(skb); 1221 return -EIO; 1222 } 1223 1224 if (datalen > cork->gso_size) { 1225 skb_shinfo(skb)->gso_size = cork->gso_size; 1226 skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 1227 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, 1228 cork->gso_size); 1229 } 1230 goto csum_partial; 1231 } 1232 1233 if (is_udplite) 1234 csum = udplite_csum(skb); 1235 else if (udp_sk(sk)->no_check6_tx) { /* UDP csum disabled */ 1236 skb->ip_summed = CHECKSUM_NONE; 1237 goto send; 1238 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 1239 csum_partial: 1240 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len); 1241 goto send; 1242 } else 1243 csum = udp_csum(skb); 1244 1245 /* add protocol-dependent pseudo-header */ 1246 uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 1247 len, fl6->flowi6_proto, csum); 1248 if (uh->check == 0) 1249 uh->check = CSUM_MANGLED_0; 1250 1251 send: 1252 err = ip6_send_skb(skb); 1253 if (err) { 1254 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) { 1255 UDP6_INC_STATS(sock_net(sk), 1256 UDP_MIB_SNDBUFERRORS, is_udplite); 1257 err = 0; 1258 } 1259 } else { 1260 UDP6_INC_STATS(sock_net(sk), 1261 UDP_MIB_OUTDATAGRAMS, is_udplite); 1262 } 1263 return err; 1264 } 1265 1266 static int udp_v6_push_pending_frames(struct sock *sk) 1267 { 1268 struct sk_buff *skb; 1269 struct udp_sock *up = udp_sk(sk); 1270 struct flowi6 fl6; 1271 int err = 0; 1272 1273 if (up->pending == AF_INET) 1274 return udp_push_pending_frames(sk); 1275 1276 /* ip6_finish_skb will release the cork, so make a copy of 1277 * fl6 here. 1278 */ 1279 fl6 = inet_sk(sk)->cork.fl.u.ip6; 1280 1281 skb = ip6_finish_skb(sk); 1282 if (!skb) 1283 goto out; 1284 1285 err = udp_v6_send_skb(skb, &fl6, &inet_sk(sk)->cork.base); 1286 1287 out: 1288 up->len = 0; 1289 up->pending = 0; 1290 return err; 1291 } 1292 1293 int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1294 { 1295 struct ipv6_txoptions opt_space; 1296 struct udp_sock *up = udp_sk(sk); 1297 struct inet_sock *inet = inet_sk(sk); 1298 struct ipv6_pinfo *np = inet6_sk(sk); 1299 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 1300 struct in6_addr *daddr, *final_p, final; 1301 struct ipv6_txoptions *opt = NULL; 1302 struct ipv6_txoptions *opt_to_free = NULL; 1303 struct ip6_flowlabel *flowlabel = NULL; 1304 struct flowi6 fl6; 1305 struct dst_entry *dst; 1306 struct ipcm6_cookie ipc6; 1307 int addr_len = msg->msg_namelen; 1308 bool connected = false; 1309 int ulen = len; 1310 int corkreq = READ_ONCE(up->corkflag) || msg->msg_flags&MSG_MORE; 1311 int err; 1312 int is_udplite = IS_UDPLITE(sk); 1313 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1314 1315 ipcm6_init(&ipc6); 1316 ipc6.gso_size = READ_ONCE(up->gso_size); 1317 ipc6.sockc.tsflags = sk->sk_tsflags; 1318 ipc6.sockc.mark = sk->sk_mark; 1319 1320 /* destination address check */ 1321 if (sin6) { 1322 if (addr_len < offsetof(struct sockaddr, sa_data)) 1323 return -EINVAL; 1324 1325 switch (sin6->sin6_family) { 1326 case AF_INET6: 1327 if (addr_len < SIN6_LEN_RFC2133) 1328 return -EINVAL; 1329 daddr = &sin6->sin6_addr; 1330 if (ipv6_addr_any(daddr) && 1331 ipv6_addr_v4mapped(&np->saddr)) 1332 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK), 1333 daddr); 1334 break; 1335 case AF_INET: 1336 goto do_udp_sendmsg; 1337 case AF_UNSPEC: 1338 msg->msg_name = sin6 = NULL; 1339 msg->msg_namelen = addr_len = 0; 1340 daddr = NULL; 1341 break; 1342 default: 1343 return -EINVAL; 1344 } 1345 } else if (!up->pending) { 1346 if (sk->sk_state != TCP_ESTABLISHED) 1347 return -EDESTADDRREQ; 1348 daddr = &sk->sk_v6_daddr; 1349 } else 1350 daddr = NULL; 1351 1352 if (daddr) { 1353 if (ipv6_addr_v4mapped(daddr)) { 1354 struct sockaddr_in sin; 1355 sin.sin_family = AF_INET; 1356 sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport; 1357 sin.sin_addr.s_addr = daddr->s6_addr32[3]; 1358 msg->msg_name = &sin; 1359 msg->msg_namelen = sizeof(sin); 1360 do_udp_sendmsg: 1361 if (__ipv6_only_sock(sk)) 1362 return -ENETUNREACH; 1363 return udp_sendmsg(sk, msg, len); 1364 } 1365 } 1366 1367 if (up->pending == AF_INET) 1368 return udp_sendmsg(sk, msg, len); 1369 1370 /* Rough check on arithmetic overflow, 1371 better check is made in ip6_append_data(). 1372 */ 1373 if (len > INT_MAX - sizeof(struct udphdr)) 1374 return -EMSGSIZE; 1375 1376 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1377 if (up->pending) { 1378 /* 1379 * There are pending frames. 1380 * The socket lock must be held while it's corked. 1381 */ 1382 lock_sock(sk); 1383 if (likely(up->pending)) { 1384 if (unlikely(up->pending != AF_INET6)) { 1385 release_sock(sk); 1386 return -EAFNOSUPPORT; 1387 } 1388 dst = NULL; 1389 goto do_append_data; 1390 } 1391 release_sock(sk); 1392 } 1393 ulen += sizeof(struct udphdr); 1394 1395 memset(&fl6, 0, sizeof(fl6)); 1396 1397 if (sin6) { 1398 if (sin6->sin6_port == 0) 1399 return -EINVAL; 1400 1401 fl6.fl6_dport = sin6->sin6_port; 1402 daddr = &sin6->sin6_addr; 1403 1404 if (np->sndflow) { 1405 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 1406 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 1407 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 1408 if (IS_ERR(flowlabel)) 1409 return -EINVAL; 1410 } 1411 } 1412 1413 /* 1414 * Otherwise it will be difficult to maintain 1415 * sk->sk_dst_cache. 1416 */ 1417 if (sk->sk_state == TCP_ESTABLISHED && 1418 ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 1419 daddr = &sk->sk_v6_daddr; 1420 1421 if (addr_len >= sizeof(struct sockaddr_in6) && 1422 sin6->sin6_scope_id && 1423 __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 1424 fl6.flowi6_oif = sin6->sin6_scope_id; 1425 } else { 1426 if (sk->sk_state != TCP_ESTABLISHED) 1427 return -EDESTADDRREQ; 1428 1429 fl6.fl6_dport = inet->inet_dport; 1430 daddr = &sk->sk_v6_daddr; 1431 fl6.flowlabel = np->flow_label; 1432 connected = true; 1433 } 1434 1435 if (!fl6.flowi6_oif) 1436 fl6.flowi6_oif = sk->sk_bound_dev_if; 1437 1438 if (!fl6.flowi6_oif) 1439 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; 1440 1441 fl6.flowi6_uid = sk->sk_uid; 1442 1443 if (msg->msg_controllen) { 1444 opt = &opt_space; 1445 memset(opt, 0, sizeof(struct ipv6_txoptions)); 1446 opt->tot_len = sizeof(*opt); 1447 ipc6.opt = opt; 1448 1449 err = udp_cmsg_send(sk, msg, &ipc6.gso_size); 1450 if (err > 0) 1451 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, 1452 &ipc6); 1453 if (err < 0) { 1454 fl6_sock_release(flowlabel); 1455 return err; 1456 } 1457 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 1458 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 1459 if (IS_ERR(flowlabel)) 1460 return -EINVAL; 1461 } 1462 if (!(opt->opt_nflen|opt->opt_flen)) 1463 opt = NULL; 1464 connected = false; 1465 } 1466 if (!opt) { 1467 opt = txopt_get(np); 1468 opt_to_free = opt; 1469 } 1470 if (flowlabel) 1471 opt = fl6_merge_options(&opt_space, flowlabel, opt); 1472 opt = ipv6_fixup_options(&opt_space, opt); 1473 ipc6.opt = opt; 1474 1475 fl6.flowi6_proto = sk->sk_protocol; 1476 fl6.flowi6_mark = ipc6.sockc.mark; 1477 fl6.daddr = *daddr; 1478 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 1479 fl6.saddr = np->saddr; 1480 fl6.fl6_sport = inet->inet_sport; 1481 1482 if (cgroup_bpf_enabled(CGROUP_UDP6_SENDMSG) && !connected) { 1483 err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, 1484 (struct sockaddr *)sin6, &fl6.saddr); 1485 if (err) 1486 goto out_no_dst; 1487 if (sin6) { 1488 if (ipv6_addr_v4mapped(&sin6->sin6_addr)) { 1489 /* BPF program rewrote IPv6-only by IPv4-mapped 1490 * IPv6. It's currently unsupported. 1491 */ 1492 err = -ENOTSUPP; 1493 goto out_no_dst; 1494 } 1495 if (sin6->sin6_port == 0) { 1496 /* BPF program set invalid port. Reject it. */ 1497 err = -EINVAL; 1498 goto out_no_dst; 1499 } 1500 fl6.fl6_dport = sin6->sin6_port; 1501 fl6.daddr = sin6->sin6_addr; 1502 } 1503 } 1504 1505 if (ipv6_addr_any(&fl6.daddr)) 1506 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 1507 1508 final_p = fl6_update_dst(&fl6, opt, &final); 1509 if (final_p) 1510 connected = false; 1511 1512 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) { 1513 fl6.flowi6_oif = np->mcast_oif; 1514 connected = false; 1515 } else if (!fl6.flowi6_oif) 1516 fl6.flowi6_oif = np->ucast_oif; 1517 1518 security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6)); 1519 1520 if (ipc6.tclass < 0) 1521 ipc6.tclass = np->tclass; 1522 1523 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 1524 1525 dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, connected); 1526 if (IS_ERR(dst)) { 1527 err = PTR_ERR(dst); 1528 dst = NULL; 1529 goto out; 1530 } 1531 1532 if (ipc6.hlimit < 0) 1533 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 1534 1535 if (msg->msg_flags&MSG_CONFIRM) 1536 goto do_confirm; 1537 back_from_confirm: 1538 1539 /* Lockless fast path for the non-corking case */ 1540 if (!corkreq) { 1541 struct inet_cork_full cork; 1542 struct sk_buff *skb; 1543 1544 skb = ip6_make_skb(sk, getfrag, msg, ulen, 1545 sizeof(struct udphdr), &ipc6, 1546 &fl6, (struct rt6_info *)dst, 1547 msg->msg_flags, &cork); 1548 err = PTR_ERR(skb); 1549 if (!IS_ERR_OR_NULL(skb)) 1550 err = udp_v6_send_skb(skb, &fl6, &cork.base); 1551 goto out; 1552 } 1553 1554 lock_sock(sk); 1555 if (unlikely(up->pending)) { 1556 /* The socket is already corked while preparing it. */ 1557 /* ... which is an evident application bug. --ANK */ 1558 release_sock(sk); 1559 1560 net_dbg_ratelimited("udp cork app bug 2\n"); 1561 err = -EINVAL; 1562 goto out; 1563 } 1564 1565 up->pending = AF_INET6; 1566 1567 do_append_data: 1568 if (ipc6.dontfrag < 0) 1569 ipc6.dontfrag = np->dontfrag; 1570 up->len += ulen; 1571 err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr), 1572 &ipc6, &fl6, (struct rt6_info *)dst, 1573 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1574 if (err) 1575 udp_v6_flush_pending_frames(sk); 1576 else if (!corkreq) 1577 err = udp_v6_push_pending_frames(sk); 1578 else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1579 up->pending = 0; 1580 1581 if (err > 0) 1582 err = np->recverr ? net_xmit_errno(err) : 0; 1583 release_sock(sk); 1584 1585 out: 1586 dst_release(dst); 1587 out_no_dst: 1588 fl6_sock_release(flowlabel); 1589 txopt_put(opt_to_free); 1590 if (!err) 1591 return len; 1592 /* 1593 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1594 * ENOBUFS might not be good (it's not tunable per se), but otherwise 1595 * we don't have a good statistic (IpOutDiscards but it can be too many 1596 * things). We could add another new stat but at least for now that 1597 * seems like overkill. 1598 */ 1599 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 1600 UDP6_INC_STATS(sock_net(sk), 1601 UDP_MIB_SNDBUFERRORS, is_udplite); 1602 } 1603 return err; 1604 1605 do_confirm: 1606 if (msg->msg_flags & MSG_PROBE) 1607 dst_confirm_neigh(dst, &fl6.daddr); 1608 if (!(msg->msg_flags&MSG_PROBE) || len) 1609 goto back_from_confirm; 1610 err = 0; 1611 goto out; 1612 } 1613 1614 void udpv6_destroy_sock(struct sock *sk) 1615 { 1616 struct udp_sock *up = udp_sk(sk); 1617 lock_sock(sk); 1618 1619 /* protects from races with udp_abort() */ 1620 sock_set_flag(sk, SOCK_DEAD); 1621 udp_v6_flush_pending_frames(sk); 1622 release_sock(sk); 1623 1624 if (static_branch_unlikely(&udpv6_encap_needed_key)) { 1625 if (up->encap_type) { 1626 void (*encap_destroy)(struct sock *sk); 1627 encap_destroy = READ_ONCE(up->encap_destroy); 1628 if (encap_destroy) 1629 encap_destroy(sk); 1630 } 1631 if (up->encap_enabled) { 1632 static_branch_dec(&udpv6_encap_needed_key); 1633 udp_encap_disable(); 1634 } 1635 } 1636 1637 inet6_destroy_sock(sk); 1638 } 1639 1640 /* 1641 * Socket option code for UDP 1642 */ 1643 int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 1644 unsigned int optlen) 1645 { 1646 if (level == SOL_UDP || level == SOL_UDPLITE) 1647 return udp_lib_setsockopt(sk, level, optname, 1648 optval, optlen, 1649 udp_v6_push_pending_frames); 1650 return ipv6_setsockopt(sk, level, optname, optval, optlen); 1651 } 1652 1653 int udpv6_getsockopt(struct sock *sk, int level, int optname, 1654 char __user *optval, int __user *optlen) 1655 { 1656 if (level == SOL_UDP || level == SOL_UDPLITE) 1657 return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1658 return ipv6_getsockopt(sk, level, optname, optval, optlen); 1659 } 1660 1661 /* thinking of making this const? Don't. 1662 * early_demux can change based on sysctl. 1663 */ 1664 static struct inet6_protocol udpv6_protocol = { 1665 .early_demux = udp_v6_early_demux, 1666 .early_demux_handler = udp_v6_early_demux, 1667 .handler = udpv6_rcv, 1668 .err_handler = udpv6_err, 1669 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1670 }; 1671 1672 /* ------------------------------------------------------------------------ */ 1673 #ifdef CONFIG_PROC_FS 1674 int udp6_seq_show(struct seq_file *seq, void *v) 1675 { 1676 if (v == SEQ_START_TOKEN) { 1677 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 1678 } else { 1679 int bucket = ((struct udp_iter_state *)seq->private)->bucket; 1680 struct inet_sock *inet = inet_sk(v); 1681 __u16 srcp = ntohs(inet->inet_sport); 1682 __u16 destp = ntohs(inet->inet_dport); 1683 __ip6_dgram_sock_seq_show(seq, v, srcp, destp, 1684 udp_rqueue_get(v), bucket); 1685 } 1686 return 0; 1687 } 1688 1689 const struct seq_operations udp6_seq_ops = { 1690 .start = udp_seq_start, 1691 .next = udp_seq_next, 1692 .stop = udp_seq_stop, 1693 .show = udp6_seq_show, 1694 }; 1695 EXPORT_SYMBOL(udp6_seq_ops); 1696 1697 static struct udp_seq_afinfo udp6_seq_afinfo = { 1698 .family = AF_INET6, 1699 .udp_table = &udp_table, 1700 }; 1701 1702 int __net_init udp6_proc_init(struct net *net) 1703 { 1704 if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops, 1705 sizeof(struct udp_iter_state), &udp6_seq_afinfo)) 1706 return -ENOMEM; 1707 return 0; 1708 } 1709 1710 void udp6_proc_exit(struct net *net) 1711 { 1712 remove_proc_entry("udp6", net->proc_net); 1713 } 1714 #endif /* CONFIG_PROC_FS */ 1715 1716 /* ------------------------------------------------------------------------ */ 1717 1718 struct proto udpv6_prot = { 1719 .name = "UDPv6", 1720 .owner = THIS_MODULE, 1721 .close = udp_lib_close, 1722 .pre_connect = udpv6_pre_connect, 1723 .connect = ip6_datagram_connect, 1724 .disconnect = udp_disconnect, 1725 .ioctl = udp_ioctl, 1726 .init = udp_init_sock, 1727 .destroy = udpv6_destroy_sock, 1728 .setsockopt = udpv6_setsockopt, 1729 .getsockopt = udpv6_getsockopt, 1730 .sendmsg = udpv6_sendmsg, 1731 .recvmsg = udpv6_recvmsg, 1732 .release_cb = ip6_datagram_release_cb, 1733 .hash = udp_lib_hash, 1734 .unhash = udp_lib_unhash, 1735 .rehash = udp_v6_rehash, 1736 .get_port = udp_v6_get_port, 1737 #ifdef CONFIG_BPF_SYSCALL 1738 .psock_update_sk_prot = udp_bpf_update_proto, 1739 #endif 1740 .memory_allocated = &udp_memory_allocated, 1741 .sysctl_mem = sysctl_udp_mem, 1742 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 1743 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 1744 .obj_size = sizeof(struct udp6_sock), 1745 .h.udp_table = &udp_table, 1746 .diag_destroy = udp_abort, 1747 }; 1748 1749 static struct inet_protosw udpv6_protosw = { 1750 .type = SOCK_DGRAM, 1751 .protocol = IPPROTO_UDP, 1752 .prot = &udpv6_prot, 1753 .ops = &inet6_dgram_ops, 1754 .flags = INET_PROTOSW_PERMANENT, 1755 }; 1756 1757 int __init udpv6_init(void) 1758 { 1759 int ret; 1760 1761 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); 1762 if (ret) 1763 goto out; 1764 1765 ret = inet6_register_protosw(&udpv6_protosw); 1766 if (ret) 1767 goto out_udpv6_protocol; 1768 out: 1769 return ret; 1770 1771 out_udpv6_protocol: 1772 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1773 goto out; 1774 } 1775 1776 void udpv6_exit(void) 1777 { 1778 inet6_unregister_protosw(&udpv6_protosw); 1779 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1780 } 1781
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.