1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 5 * The Regents of the University of California. 6 * Copyright (c) 2008 Robert N. M. Watson 7 * Copyright (c) 2010-2011 Juniper Networks, Inc. 8 * Copyright (c) 2014 Kevin Lo 9 * All rights reserved. 10 * 11 * Portions of this software were developed by Robert N. M. Watson under 12 * contract to Juniper Networks, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include "opt_inet.h" 45 #include "opt_inet6.h" 46 #include "opt_ipsec.h" 47 #include "opt_rss.h" 48 49 #include <sys/param.h> 50 #include <sys/domain.h> 51 #include <sys/eventhandler.h> 52 #include <sys/jail.h> 53 #include <sys/kernel.h> 54 #include <sys/lock.h> 55 #include <sys/malloc.h> 56 #include <sys/mbuf.h> 57 #include <sys/priv.h> 58 #include <sys/proc.h> 59 #include <sys/protosw.h> 60 #include <sys/sdt.h> 61 #include <sys/signalvar.h> 62 #include <sys/socket.h> 63 #include <sys/socketvar.h> 64 #include <sys/sx.h> 65 #include <sys/sysctl.h> 66 #include <sys/syslog.h> 67 #include <sys/systm.h> 68 69 #include <vm/uma.h> 70 71 #include <net/if.h> 72 #include <net/if_var.h> 73 #include <net/route.h> 74 #include <net/rss_config.h> 75 76 #include <netinet/in.h> 77 #include <netinet/in_kdtrace.h> 78 #include <netinet/in_pcb.h> 79 #include <netinet/in_systm.h> 80 #include <netinet/in_var.h> 81 #include <netinet/ip.h> 82 #ifdef INET6 83 #include <netinet/ip6.h> 84 #endif 85 #include <netinet/ip_icmp.h> 86 #include <netinet/icmp_var.h> 87 #include <netinet/ip_var.h> 88 #include <netinet/ip_options.h> 89 #ifdef INET6 90 #include <netinet6/ip6_var.h> 91 #endif 92 #include <netinet/udp.h> 93 #include <netinet/udp_var.h> 94 #include <netinet/udplite.h> 95 #include <netinet/in_rss.h> 96 97 #include <netipsec/ipsec_support.h> 98 99 #include <machine/in_cksum.h> 100 101 #include <security/mac/mac_framework.h> 102 103 /* 104 * UDP and UDP-Lite protocols implementation. 105 * Per RFC 768, August, 1980. 106 * Per RFC 3828, July, 2004. 107 */ 108 109 /* 110 * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums 111 * removes the only data integrity mechanism for packets and malformed 112 * packets that would otherwise be discarded due to bad checksums, and may 113 * cause problems (especially for NFS data blocks). 114 */ 115 VNET_DEFINE(int, udp_cksum) = 1; 116 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW, 117 &VNET_NAME(udp_cksum), 0, "compute udp checksum"); 118 119 int udp_log_in_vain = 0; 120 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 121 &udp_log_in_vain, 0, "Log all incoming UDP packets"); 122 123 VNET_DEFINE(int, udp_blackhole) = 0; 124 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 125 &VNET_NAME(udp_blackhole), 0, 126 "Do not send port unreachables for refused connects"); 127 128 u_long udp_sendspace = 9216; /* really max datagram size */ 129 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 130 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 131 132 u_long udp_recvspace = 40 * (1024 + 133 #ifdef INET6 134 sizeof(struct sockaddr_in6) 135 #else 136 sizeof(struct sockaddr_in) 137 #endif 138 ); /* 40 1K datagrams */ 139 140 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 141 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 142 143 VNET_DEFINE(struct inpcbhead, udb); /* from udp_var.h */ 144 VNET_DEFINE(struct inpcbinfo, udbinfo); 145 VNET_DEFINE(struct inpcbhead, ulitecb); 146 VNET_DEFINE(struct inpcbinfo, ulitecbinfo); 147 static VNET_DEFINE(uma_zone_t, udpcb_zone); 148 #define V_udpcb_zone VNET(udpcb_zone) 149 150 #ifndef UDBHASHSIZE 151 #define UDBHASHSIZE 128 152 #endif 153 154 VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat); /* from udp_var.h */ 155 VNET_PCPUSTAT_SYSINIT(udpstat); 156 SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat, 157 udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 158 159 #ifdef VIMAGE 160 VNET_PCPUSTAT_SYSUNINIT(udpstat); 161 #endif /* VIMAGE */ 162 #ifdef INET 163 static void udp_detach(struct socket *so); 164 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 165 struct mbuf *, struct thread *); 166 #endif 167 168 static void 169 udp_zone_change(void *tag) 170 { 171 172 uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 173 uma_zone_set_max(V_udpcb_zone, maxsockets); 174 } 175 176 static int 177 udp_inpcb_init(void *mem, int size, int flags) 178 { 179 struct inpcb *inp; 180 181 inp = mem; 182 INP_LOCK_INIT(inp, "inp", "udpinp"); 183 return (0); 184 } 185 186 static int 187 udplite_inpcb_init(void *mem, int size, int flags) 188 { 189 struct inpcb *inp; 190 191 inp = mem; 192 INP_LOCK_INIT(inp, "inp", "udpliteinp"); 193 return (0); 194 } 195 196 void 197 udp_init(void) 198 { 199 200 /* 201 * For now default to 2-tuple UDP hashing - until the fragment 202 * reassembly code can also update the flowid. 203 * 204 * Once we can calculate the flowid that way and re-establish 205 * a 4-tuple, flip this to 4-tuple. 206 */ 207 in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE, 208 "udp_inpcb", udp_inpcb_init, IPI_HASHFIELDS_2TUPLE); 209 V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb), 210 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 211 uma_zone_set_max(V_udpcb_zone, maxsockets); 212 uma_zone_set_warning(V_udpcb_zone, "kern.ipc.maxsockets limit reached"); 213 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL, 214 EVENTHANDLER_PRI_ANY); 215 } 216 217 void 218 udplite_init(void) 219 { 220 221 in_pcbinfo_init(&V_ulitecbinfo, "udplite", &V_ulitecb, UDBHASHSIZE, 222 UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init, 223 IPI_HASHFIELDS_2TUPLE); 224 } 225 226 /* 227 * Kernel module interface for updating udpstat. The argument is an index 228 * into udpstat treated as an array of u_long. While this encodes the 229 * general layout of udpstat into the caller, it doesn't encode its location, 230 * so that future changes to add, for example, per-CPU stats support won't 231 * cause binary compatibility problems for kernel modules. 232 */ 233 void 234 kmod_udpstat_inc(int statnum) 235 { 236 237 counter_u64_add(VNET(udpstat)[statnum], 1); 238 } 239 240 int 241 udp_newudpcb(struct inpcb *inp) 242 { 243 struct udpcb *up; 244 245 up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO); 246 if (up == NULL) 247 return (ENOBUFS); 248 inp->inp_ppcb = up; 249 return (0); 250 } 251 252 void 253 udp_discardcb(struct udpcb *up) 254 { 255 256 uma_zfree(V_udpcb_zone, up); 257 } 258 259 #ifdef VIMAGE 260 static void 261 udp_destroy(void *unused __unused) 262 { 263 264 in_pcbinfo_destroy(&V_udbinfo); 265 uma_zdestroy(V_udpcb_zone); 266 } 267 VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL); 268 269 static void 270 udplite_destroy(void *unused __unused) 271 { 272 273 in_pcbinfo_destroy(&V_ulitecbinfo); 274 } 275 VNET_SYSUNINIT(udplite, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udplite_destroy, 276 NULL); 277 #endif 278 279 #ifdef INET 280 /* 281 * Subroutine of udp_input(), which appends the provided mbuf chain to the 282 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 283 * contains the source address. If the socket ends up being an IPv6 socket, 284 * udp_append() will convert to a sockaddr_in6 before passing the address 285 * into the socket code. 286 * 287 * In the normal case udp_append() will return 0, indicating that you 288 * must unlock the inp. However if a tunneling protocol is in place we increment 289 * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we 290 * then decrement the reference count. If the inp_rele returns 1, indicating the 291 * inp is gone, we return that to the caller to tell them *not* to unlock 292 * the inp. In the case of multi-cast this will cause the distribution 293 * to stop (though most tunneling protocols known currently do *not* use 294 * multicast). 295 */ 296 static int 297 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off, 298 struct sockaddr_in *udp_in) 299 { 300 struct sockaddr *append_sa; 301 struct socket *so; 302 struct mbuf *tmpopts, *opts = NULL; 303 #ifdef INET6 304 struct sockaddr_in6 udp_in6; 305 #endif 306 struct udpcb *up; 307 308 INP_LOCK_ASSERT(inp); 309 310 /* 311 * Engage the tunneling protocol. 312 */ 313 up = intoudpcb(inp); 314 if (up->u_tun_func != NULL) { 315 in_pcbref(inp); 316 INP_RUNLOCK(inp); 317 (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&udp_in[0], 318 up->u_tun_ctx); 319 INP_RLOCK(inp); 320 return (in_pcbrele_rlocked(inp)); 321 } 322 323 off += sizeof(struct udphdr); 324 325 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 326 /* Check AH/ESP integrity. */ 327 if (IPSEC_ENABLED(ipv4) && 328 IPSEC_CHECK_POLICY(ipv4, n, inp) != 0) { 329 m_freem(n); 330 return (0); 331 } 332 if (up->u_flags & UF_ESPINUDP) {/* IPSec UDP encaps. */ 333 if (IPSEC_ENABLED(ipv4) && 334 UDPENCAP_INPUT(n, off, AF_INET) != 0) 335 return (0); /* Consumed. */ 336 } 337 #endif /* IPSEC */ 338 #ifdef MAC 339 if (mac_inpcb_check_deliver(inp, n) != 0) { 340 m_freem(n); 341 return (0); 342 } 343 #endif /* MAC */ 344 if (inp->inp_flags & INP_CONTROLOPTS || 345 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 346 #ifdef INET6 347 if (inp->inp_vflag & INP_IPV6) 348 (void)ip6_savecontrol_v4(inp, n, &opts, NULL); 349 else 350 #endif /* INET6 */ 351 ip_savecontrol(inp, &opts, ip, n); 352 } 353 if ((inp->inp_vflag & INP_IPV4) && (inp->inp_flags2 & INP_ORIGDSTADDR)) { 354 tmpopts = sbcreatecontrol((caddr_t)&udp_in[1], 355 sizeof(struct sockaddr_in), IP_ORIGDSTADDR, IPPROTO_IP); 356 if (tmpopts) { 357 if (opts) { 358 tmpopts->m_next = opts; 359 opts = tmpopts; 360 } else 361 opts = tmpopts; 362 } 363 } 364 #ifdef INET6 365 if (inp->inp_vflag & INP_IPV6) { 366 bzero(&udp_in6, sizeof(udp_in6)); 367 udp_in6.sin6_len = sizeof(udp_in6); 368 udp_in6.sin6_family = AF_INET6; 369 in6_sin_2_v4mapsin6(&udp_in[0], &udp_in6); 370 append_sa = (struct sockaddr *)&udp_in6; 371 } else 372 #endif /* INET6 */ 373 append_sa = (struct sockaddr *)&udp_in[0]; 374 m_adj(n, off); 375 376 so = inp->inp_socket; 377 SOCKBUF_LOCK(&so->so_rcv); 378 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 379 SOCKBUF_UNLOCK(&so->so_rcv); 380 m_freem(n); 381 if (opts) 382 m_freem(opts); 383 UDPSTAT_INC(udps_fullsock); 384 } else 385 sorwakeup_locked(so); 386 return (0); 387 } 388 389 int 390 udp_input(struct mbuf **mp, int *offp, int proto) 391 { 392 struct ip *ip; 393 struct udphdr *uh; 394 struct ifnet *ifp; 395 struct inpcb *inp; 396 uint16_t len, ip_len; 397 struct inpcbinfo *pcbinfo; 398 struct ip save_ip; 399 struct sockaddr_in udp_in[2]; 400 struct mbuf *m; 401 struct m_tag *fwd_tag; 402 int cscov_partial, iphlen; 403 404 m = *mp; 405 iphlen = *offp; 406 ifp = m->m_pkthdr.rcvif; 407 *mp = NULL; 408 UDPSTAT_INC(udps_ipackets); 409 410 /* 411 * Strip IP options, if any; should skip this, make available to 412 * user, and use on returned packets, but we don't yet have a way to 413 * check the checksum with options still present. 414 */ 415 if (iphlen > sizeof (struct ip)) { 416 ip_stripoptions(m); 417 iphlen = sizeof(struct ip); 418 } 419 420 /* 421 * Get IP and UDP header together in first mbuf. 422 */ 423 ip = mtod(m, struct ip *); 424 if (m->m_len < iphlen + sizeof(struct udphdr)) { 425 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) { 426 UDPSTAT_INC(udps_hdrops); 427 return (IPPROTO_DONE); 428 } 429 ip = mtod(m, struct ip *); 430 } 431 uh = (struct udphdr *)((caddr_t)ip + iphlen); 432 cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0; 433 434 /* 435 * Destination port of 0 is illegal, based on RFC768. 436 */ 437 if (uh->uh_dport == 0) 438 goto badunlocked; 439 440 /* 441 * Construct sockaddr format source address. Stuff source address 442 * and datagram in user buffer. 443 */ 444 bzero(&udp_in[0], sizeof(struct sockaddr_in) * 2); 445 udp_in[0].sin_len = sizeof(struct sockaddr_in); 446 udp_in[0].sin_family = AF_INET; 447 udp_in[0].sin_port = uh->uh_sport; 448 udp_in[0].sin_addr = ip->ip_src; 449 udp_in[1].sin_len = sizeof(struct sockaddr_in); 450 udp_in[1].sin_family = AF_INET; 451 udp_in[1].sin_port = uh->uh_dport; 452 udp_in[1].sin_addr = ip->ip_dst; 453 454 /* 455 * Make mbuf data length reflect UDP length. If not enough data to 456 * reflect UDP length, drop. 457 */ 458 len = ntohs((u_short)uh->uh_ulen); 459 ip_len = ntohs(ip->ip_len) - iphlen; 460 if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) { 461 /* Zero means checksum over the complete packet. */ 462 if (len == 0) 463 len = ip_len; 464 cscov_partial = 0; 465 } 466 if (ip_len != len) { 467 if (len > ip_len || len < sizeof(struct udphdr)) { 468 UDPSTAT_INC(udps_badlen); 469 goto badunlocked; 470 } 471 if (proto == IPPROTO_UDP) 472 m_adj(m, len - ip_len); 473 } 474 475 /* 476 * Save a copy of the IP header in case we want restore it for 477 * sending an ICMP error message in response. 478 */ 479 if (!V_udp_blackhole) 480 save_ip = *ip; 481 else 482 memset(&save_ip, 0, sizeof(save_ip)); 483 484 /* 485 * Checksum extended UDP header and data. 486 */ 487 if (uh->uh_sum) { 488 u_short uh_sum; 489 490 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) && 491 !cscov_partial) { 492 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 493 uh_sum = m->m_pkthdr.csum_data; 494 else 495 uh_sum = in_pseudo(ip->ip_src.s_addr, 496 ip->ip_dst.s_addr, htonl((u_short)len + 497 m->m_pkthdr.csum_data + proto)); 498 uh_sum ^= 0xffff; 499 } else { 500 char b[9]; 501 502 bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 503 bzero(((struct ipovly *)ip)->ih_x1, 9); 504 ((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ? 505 uh->uh_ulen : htons(ip_len); 506 uh_sum = in_cksum(m, len + sizeof (struct ip)); 507 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 508 } 509 if (uh_sum) { 510 UDPSTAT_INC(udps_badsum); 511 m_freem(m); 512 return (IPPROTO_DONE); 513 } 514 } else { 515 if (proto == IPPROTO_UDP) { 516 UDPSTAT_INC(udps_nosum); 517 } else { 518 /* UDPLite requires a checksum */ 519 /* XXX: What is the right UDPLite MIB counter here? */ 520 m_freem(m); 521 return (IPPROTO_DONE); 522 } 523 } 524 525 pcbinfo = udp_get_inpcbinfo(proto); 526 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 527 in_broadcast(ip->ip_dst, ifp)) { 528 struct inpcb *last; 529 struct inpcbhead *pcblist; 530 struct ip_moptions *imo; 531 532 INP_INFO_RLOCK(pcbinfo); 533 pcblist = udp_get_pcblist(proto); 534 last = NULL; 535 CK_LIST_FOREACH(inp, pcblist, inp_list) { 536 if (inp->inp_lport != uh->uh_dport) 537 continue; 538 #ifdef INET6 539 if ((inp->inp_vflag & INP_IPV4) == 0) 540 continue; 541 #endif 542 if (inp->inp_laddr.s_addr != INADDR_ANY && 543 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 544 continue; 545 if (inp->inp_faddr.s_addr != INADDR_ANY && 546 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 547 continue; 548 if (inp->inp_fport != 0 && 549 inp->inp_fport != uh->uh_sport) 550 continue; 551 552 INP_RLOCK(inp); 553 554 /* 555 * XXXRW: Because we weren't holding either the inpcb 556 * or the hash lock when we checked for a match 557 * before, we should probably recheck now that the 558 * inpcb lock is held. 559 */ 560 561 /* 562 * Handle socket delivery policy for any-source 563 * and source-specific multicast. [RFC3678] 564 */ 565 imo = inp->inp_moptions; 566 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 567 struct sockaddr_in group; 568 int blocked; 569 if (imo == NULL) { 570 INP_RUNLOCK(inp); 571 continue; 572 } 573 bzero(&group, sizeof(struct sockaddr_in)); 574 group.sin_len = sizeof(struct sockaddr_in); 575 group.sin_family = AF_INET; 576 group.sin_addr = ip->ip_dst; 577 578 blocked = imo_multi_filter(imo, ifp, 579 (struct sockaddr *)&group, 580 (struct sockaddr *)&udp_in[0]); 581 if (blocked != MCAST_PASS) { 582 if (blocked == MCAST_NOTGMEMBER) 583 IPSTAT_INC(ips_notmember); 584 if (blocked == MCAST_NOTSMEMBER || 585 blocked == MCAST_MUTED) 586 UDPSTAT_INC(udps_filtermcast); 587 INP_RUNLOCK(inp); 588 continue; 589 } 590 } 591 if (last != NULL) { 592 struct mbuf *n; 593 594 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != 595 NULL) { 596 UDP_PROBE(receive, NULL, last, ip, 597 last, uh); 598 if (udp_append(last, ip, n, iphlen, 599 udp_in)) { 600 goto inp_lost; 601 } 602 } 603 INP_RUNLOCK(last); 604 } 605 last = inp; 606 /* 607 * Don't look for additional matches if this one does 608 * not have either the SO_REUSEPORT or SO_REUSEADDR 609 * socket options set. This heuristic avoids 610 * searching through all pcbs in the common case of a 611 * non-shared port. It assumes that an application 612 * will never clear these options after setting them. 613 */ 614 if ((last->inp_socket->so_options & 615 (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) 616 break; 617 } 618 619 if (last == NULL) { 620 /* 621 * No matching pcb found; discard datagram. (No need 622 * to send an ICMP Port Unreachable for a broadcast 623 * or multicast datgram.) 624 */ 625 UDPSTAT_INC(udps_noportbcast); 626 if (inp) 627 INP_RUNLOCK(inp); 628 INP_INFO_RUNLOCK(pcbinfo); 629 goto badunlocked; 630 } 631 UDP_PROBE(receive, NULL, last, ip, last, uh); 632 if (udp_append(last, ip, m, iphlen, udp_in) == 0) 633 INP_RUNLOCK(last); 634 inp_lost: 635 INP_INFO_RUNLOCK(pcbinfo); 636 return (IPPROTO_DONE); 637 } 638 639 /* 640 * Locate pcb for datagram. 641 */ 642 643 /* 644 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 645 */ 646 if ((m->m_flags & M_IP_NEXTHOP) && 647 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 648 struct sockaddr_in *next_hop; 649 650 next_hop = (struct sockaddr_in *)(fwd_tag + 1); 651 652 /* 653 * Transparently forwarded. Pretend to be the destination. 654 * Already got one like this? 655 */ 656 inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 657 ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m); 658 if (!inp) { 659 /* 660 * It's new. Try to find the ambushing socket. 661 * Because we've rewritten the destination address, 662 * any hardware-generated hash is ignored. 663 */ 664 inp = in_pcblookup(pcbinfo, ip->ip_src, 665 uh->uh_sport, next_hop->sin_addr, 666 next_hop->sin_port ? htons(next_hop->sin_port) : 667 uh->uh_dport, INPLOOKUP_WILDCARD | 668 INPLOOKUP_RLOCKPCB, ifp); 669 } 670 /* Remove the tag from the packet. We don't need it anymore. */ 671 m_tag_delete(m, fwd_tag); 672 m->m_flags &= ~M_IP_NEXTHOP; 673 } else 674 inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 675 ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD | 676 INPLOOKUP_RLOCKPCB, ifp, m); 677 if (inp == NULL) { 678 if (udp_log_in_vain) { 679 char src[INET_ADDRSTRLEN]; 680 char dst[INET_ADDRSTRLEN]; 681 682 log(LOG_INFO, 683 "Connection attempt to UDP %s:%d from %s:%d\n", 684 inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport), 685 inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport)); 686 } 687 UDPSTAT_INC(udps_noport); 688 if (m->m_flags & (M_BCAST | M_MCAST)) { 689 UDPSTAT_INC(udps_noportbcast); 690 goto badunlocked; 691 } 692 if (V_udp_blackhole) 693 goto badunlocked; 694 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 695 goto badunlocked; 696 *ip = save_ip; 697 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 698 return (IPPROTO_DONE); 699 } 700 701 /* 702 * Check the minimum TTL for socket. 703 */ 704 INP_RLOCK_ASSERT(inp); 705 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) { 706 INP_RUNLOCK(inp); 707 m_freem(m); 708 return (IPPROTO_DONE); 709 } 710 if (cscov_partial) { 711 struct udpcb *up; 712 713 up = intoudpcb(inp); 714 if (up->u_rxcslen == 0 || up->u_rxcslen > len) { 715 INP_RUNLOCK(inp); 716 m_freem(m); 717 return (IPPROTO_DONE); 718 } 719 } 720 721 UDP_PROBE(receive, NULL, inp, ip, inp, uh); 722 if (udp_append(inp, ip, m, iphlen, udp_in) == 0) 723 INP_RUNLOCK(inp); 724 return (IPPROTO_DONE); 725 726 badunlocked: 727 m_freem(m); 728 return (IPPROTO_DONE); 729 } 730 #endif /* INET */ 731 732 /* 733 * Notify a udp user of an asynchronous error; just wake up so that they can 734 * collect error status. 735 */ 736 struct inpcb * 737 udp_notify(struct inpcb *inp, int errno) 738 { 739 740 /* 741 * While udp_ctlinput() always calls udp_notify() with a read lock 742 * when invoking it directly, in_pcbnotifyall() currently uses write 743 * locks due to sharing code with TCP. For now, accept either a read 744 * or a write lock, but a read lock is sufficient. 745 */ 746 INP_LOCK_ASSERT(inp); 747 if ((errno == EHOSTUNREACH || errno == ENETUNREACH || 748 errno == EHOSTDOWN) && inp->inp_route.ro_rt) { 749 RTFREE(inp->inp_route.ro_rt); 750 inp->inp_route.ro_rt = (struct rtentry *)NULL; 751 } 752 753 inp->inp_socket->so_error = errno; 754 sorwakeup(inp->inp_socket); 755 sowwakeup(inp->inp_socket); 756 return (inp); 757 } 758 759 #ifdef INET 760 static void 761 udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip, 762 struct inpcbinfo *pcbinfo) 763 { 764 struct ip *ip = vip; 765 struct udphdr *uh; 766 struct in_addr faddr; 767 struct inpcb *inp; 768 769 faddr = ((struct sockaddr_in *)sa)->sin_addr; 770 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 771 return; 772 773 if (PRC_IS_REDIRECT(cmd)) { 774 /* signal EHOSTDOWN, as it flushes the cached route */ 775 in_pcbnotifyall(&V_udbinfo, faddr, EHOSTDOWN, udp_notify); 776 return; 777 } 778 779 /* 780 * Hostdead is ugly because it goes linearly through all PCBs. 781 * 782 * XXX: We never get this from ICMP, otherwise it makes an excellent 783 * DoS attack on machines with many connections. 784 */ 785 if (cmd == PRC_HOSTDEAD) 786 ip = NULL; 787 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 788 return; 789 if (ip != NULL) { 790 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 791 inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 792 ip->ip_src, uh->uh_sport, INPLOOKUP_RLOCKPCB, NULL); 793 if (inp != NULL) { 794 INP_RLOCK_ASSERT(inp); 795 if (inp->inp_socket != NULL) { 796 udp_notify(inp, inetctlerrmap[cmd]); 797 } 798 INP_RUNLOCK(inp); 799 } else { 800 inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 801 ip->ip_src, uh->uh_sport, 802 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 803 if (inp != NULL) { 804 struct udpcb *up; 805 void *ctx; 806 udp_tun_icmp_t func; 807 808 up = intoudpcb(inp); 809 ctx = up->u_tun_ctx; 810 func = up->u_icmp_func; 811 INP_RUNLOCK(inp); 812 if (func != NULL) 813 (*func)(cmd, sa, vip, ctx); 814 } 815 } 816 } else 817 in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd], 818 udp_notify); 819 } 820 void 821 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 822 { 823 824 return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo)); 825 } 826 827 void 828 udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip) 829 { 830 831 return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo)); 832 } 833 #endif /* INET */ 834 835 static int 836 udp_pcblist(SYSCTL_HANDLER_ARGS) 837 { 838 int error, i, n; 839 struct inpcb *inp, **inp_list; 840 struct in_pcblist *il; 841 inp_gen_t gencnt; 842 struct xinpgen xig; 843 844 /* 845 * The process of preparing the PCB list is too time-consuming and 846 * resource-intensive to repeat twice on every request. 847 */ 848 if (req->oldptr == 0) { 849 n = V_udbinfo.ipi_count; 850 n += imax(n / 8, 10); 851 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb); 852 return (0); 853 } 854 855 if (req->newptr != 0) 856 return (EPERM); 857 858 /* 859 * OK, now we're committed to doing something. 860 */ 861 INP_INFO_RLOCK(&V_udbinfo); 862 gencnt = V_udbinfo.ipi_gencnt; 863 n = V_udbinfo.ipi_count; 864 INP_INFO_RUNLOCK(&V_udbinfo); 865 866 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 867 + n * sizeof(struct xinpcb)); 868 if (error != 0) 869 return (error); 870 871 xig.xig_len = sizeof xig; 872 xig.xig_count = n; 873 xig.xig_gen = gencnt; 874 xig.xig_sogen = so_gencnt; 875 error = SYSCTL_OUT(req, &xig, sizeof xig); 876 if (error) 877 return (error); 878 il = malloc(sizeof(struct in_pcblist) + n * sizeof(struct inpcb *), M_TEMP, M_WAITOK|M_ZERO_INVARIANTS); 879 inp_list = il->il_inp_list; 880 881 INP_INFO_RLOCK(&V_udbinfo); 882 for (inp = CK_LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n; 883 inp = CK_LIST_NEXT(inp, inp_list)) { 884 INP_WLOCK(inp); 885 if (inp->inp_gencnt <= gencnt && 886 cr_canseeinpcb(req->td->td_ucred, inp) == 0) { 887 in_pcbref(inp); 888 inp_list[i++] = inp; 889 } 890 INP_WUNLOCK(inp); 891 } 892 INP_INFO_RUNLOCK(&V_udbinfo); 893 n = i; 894 895 error = 0; 896 for (i = 0; i < n; i++) { 897 inp = inp_list[i]; 898 INP_RLOCK(inp); 899 if (inp->inp_gencnt <= gencnt) { 900 struct xinpcb xi; 901 902 in_pcbtoxinpcb(inp, &xi); 903 INP_RUNLOCK(inp); 904 error = SYSCTL_OUT(req, &xi, sizeof xi); 905 } else 906 INP_RUNLOCK(inp); 907 } 908 il->il_count = n; 909 il->il_pcbinfo = &V_udbinfo; 910 epoch_call(net_epoch_preempt, &il->il_epoch_ctx, in_pcblist_rele_rlocked); 911 912 if (!error) { 913 /* 914 * Give the user an updated idea of our state. If the 915 * generation differs from what we told her before, she knows 916 * that something happened while we were processing this 917 * request, and it might be necessary to retry. 918 */ 919 INP_INFO_RLOCK(&V_udbinfo); 920 xig.xig_gen = V_udbinfo.ipi_gencnt; 921 xig.xig_sogen = so_gencnt; 922 xig.xig_count = V_udbinfo.ipi_count; 923 INP_INFO_RUNLOCK(&V_udbinfo); 924 error = SYSCTL_OUT(req, &xig, sizeof xig); 925 } 926 return (error); 927 } 928 929 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, 930 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0, 931 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 932 933 #ifdef INET 934 static int 935 udp_getcred(SYSCTL_HANDLER_ARGS) 936 { 937 struct xucred xuc; 938 struct sockaddr_in addrs[2]; 939 struct inpcb *inp; 940 int error; 941 942 error = priv_check(req->td, PRIV_NETINET_GETCRED); 943 if (error) 944 return (error); 945 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 946 if (error) 947 return (error); 948 inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 949 addrs[0].sin_addr, addrs[0].sin_port, 950 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 951 if (inp != NULL) { 952 INP_RLOCK_ASSERT(inp); 953 if (inp->inp_socket == NULL) 954 error = ENOENT; 955 if (error == 0) 956 error = cr_canseeinpcb(req->td->td_ucred, inp); 957 if (error == 0) 958 cru2x(inp->inp_cred, &xuc); 959 INP_RUNLOCK(inp); 960 } else 961 error = ENOENT; 962 if (error == 0) 963 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 964 return (error); 965 } 966 967 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 968 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 969 udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 970 #endif /* INET */ 971 972 int 973 udp_ctloutput(struct socket *so, struct sockopt *sopt) 974 { 975 struct inpcb *inp; 976 struct udpcb *up; 977 int isudplite, error, optval; 978 979 error = 0; 980 isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0; 981 inp = sotoinpcb(so); 982 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 983 INP_WLOCK(inp); 984 if (sopt->sopt_level != so->so_proto->pr_protocol) { 985 #ifdef INET6 986 if (INP_CHECK_SOCKAF(so, AF_INET6)) { 987 INP_WUNLOCK(inp); 988 error = ip6_ctloutput(so, sopt); 989 } 990 #endif 991 #if defined(INET) && defined(INET6) 992 else 993 #endif 994 #ifdef INET 995 { 996 INP_WUNLOCK(inp); 997 error = ip_ctloutput(so, sopt); 998 } 999 #endif 1000 return (error); 1001 } 1002 1003 switch (sopt->sopt_dir) { 1004 case SOPT_SET: 1005 switch (sopt->sopt_name) { 1006 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1007 #ifdef INET 1008 case UDP_ENCAP: 1009 if (!IPSEC_ENABLED(ipv4)) { 1010 INP_WUNLOCK(inp); 1011 return (ENOPROTOOPT); 1012 } 1013 error = UDPENCAP_PCBCTL(inp, sopt); 1014 break; 1015 #endif /* INET */ 1016 #endif /* IPSEC */ 1017 case UDPLITE_SEND_CSCOV: 1018 case UDPLITE_RECV_CSCOV: 1019 if (!isudplite) { 1020 INP_WUNLOCK(inp); 1021 error = ENOPROTOOPT; 1022 break; 1023 } 1024 INP_WUNLOCK(inp); 1025 error = sooptcopyin(sopt, &optval, sizeof(optval), 1026 sizeof(optval)); 1027 if (error != 0) 1028 break; 1029 inp = sotoinpcb(so); 1030 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1031 INP_WLOCK(inp); 1032 up = intoudpcb(inp); 1033 KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1034 if ((optval != 0 && optval < 8) || (optval > 65535)) { 1035 INP_WUNLOCK(inp); 1036 error = EINVAL; 1037 break; 1038 } 1039 if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1040 up->u_txcslen = optval; 1041 else 1042 up->u_rxcslen = optval; 1043 INP_WUNLOCK(inp); 1044 break; 1045 default: 1046 INP_WUNLOCK(inp); 1047 error = ENOPROTOOPT; 1048 break; 1049 } 1050 break; 1051 case SOPT_GET: 1052 switch (sopt->sopt_name) { 1053 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1054 #ifdef INET 1055 case UDP_ENCAP: 1056 if (!IPSEC_ENABLED(ipv4)) { 1057 INP_WUNLOCK(inp); 1058 return (ENOPROTOOPT); 1059 } 1060 error = UDPENCAP_PCBCTL(inp, sopt); 1061 break; 1062 #endif /* INET */ 1063 #endif /* IPSEC */ 1064 case UDPLITE_SEND_CSCOV: 1065 case UDPLITE_RECV_CSCOV: 1066 if (!isudplite) { 1067 INP_WUNLOCK(inp); 1068 error = ENOPROTOOPT; 1069 break; 1070 } 1071 up = intoudpcb(inp); 1072 KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1073 if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1074 optval = up->u_txcslen; 1075 else 1076 optval = up->u_rxcslen; 1077 INP_WUNLOCK(inp); 1078 error = sooptcopyout(sopt, &optval, sizeof(optval)); 1079 break; 1080 default: 1081 INP_WUNLOCK(inp); 1082 error = ENOPROTOOPT; 1083 break; 1084 } 1085 break; 1086 } 1087 return (error); 1088 } 1089 1090 #ifdef INET 1091 #define UH_WLOCKED 2 1092 #define UH_RLOCKED 1 1093 #define UH_UNLOCKED 0 1094 static int 1095 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, 1096 struct mbuf *control, struct thread *td) 1097 { 1098 struct udpiphdr *ui; 1099 int len = m->m_pkthdr.len; 1100 struct in_addr faddr, laddr; 1101 struct cmsghdr *cm; 1102 struct inpcbinfo *pcbinfo; 1103 struct sockaddr_in *sin, src; 1104 int cscov_partial = 0; 1105 int error = 0; 1106 int ipflags; 1107 u_short fport, lport; 1108 int unlock_udbinfo, unlock_inp; 1109 u_char tos; 1110 uint8_t pr; 1111 uint16_t cscov = 0; 1112 uint32_t flowid = 0; 1113 uint8_t flowtype = M_HASHTYPE_NONE; 1114 1115 /* 1116 * udp_output() may need to temporarily bind or connect the current 1117 * inpcb. As such, we don't know up front whether we will need the 1118 * pcbinfo lock or not. Do any work to decide what is needed up 1119 * front before acquiring any locks. 1120 */ 1121 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 1122 if (control) 1123 m_freem(control); 1124 m_freem(m); 1125 return (EMSGSIZE); 1126 } 1127 1128 src.sin_family = 0; 1129 sin = (struct sockaddr_in *)addr; 1130 if (sin == NULL || 1131 (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 1132 INP_WLOCK(inp); 1133 unlock_inp = UH_WLOCKED; 1134 } else { 1135 INP_RLOCK(inp); 1136 unlock_inp = UH_RLOCKED; 1137 } 1138 tos = inp->inp_ip_tos; 1139 if (control != NULL) { 1140 /* 1141 * XXX: Currently, we assume all the optional information is 1142 * stored in a single mbuf. 1143 */ 1144 if (control->m_next) { 1145 if (unlock_inp == UH_WLOCKED) 1146 INP_WUNLOCK(inp); 1147 else 1148 INP_RUNLOCK(inp); 1149 m_freem(control); 1150 m_freem(m); 1151 return (EINVAL); 1152 } 1153 for (; control->m_len > 0; 1154 control->m_data += CMSG_ALIGN(cm->cmsg_len), 1155 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 1156 cm = mtod(control, struct cmsghdr *); 1157 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 1158 || cm->cmsg_len > control->m_len) { 1159 error = EINVAL; 1160 break; 1161 } 1162 if (cm->cmsg_level != IPPROTO_IP) 1163 continue; 1164 1165 switch (cm->cmsg_type) { 1166 case IP_SENDSRCADDR: 1167 if (cm->cmsg_len != 1168 CMSG_LEN(sizeof(struct in_addr))) { 1169 error = EINVAL; 1170 break; 1171 } 1172 bzero(&src, sizeof(src)); 1173 src.sin_family = AF_INET; 1174 src.sin_len = sizeof(src); 1175 src.sin_port = inp->inp_lport; 1176 src.sin_addr = 1177 *(struct in_addr *)CMSG_DATA(cm); 1178 break; 1179 1180 case IP_TOS: 1181 if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) { 1182 error = EINVAL; 1183 break; 1184 } 1185 tos = *(u_char *)CMSG_DATA(cm); 1186 break; 1187 1188 case IP_FLOWID: 1189 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 1190 error = EINVAL; 1191 break; 1192 } 1193 flowid = *(uint32_t *) CMSG_DATA(cm); 1194 break; 1195 1196 case IP_FLOWTYPE: 1197 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 1198 error = EINVAL; 1199 break; 1200 } 1201 flowtype = *(uint32_t *) CMSG_DATA(cm); 1202 break; 1203 1204 #ifdef RSS 1205 case IP_RSSBUCKETID: 1206 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 1207 error = EINVAL; 1208 break; 1209 } 1210 /* This is just a placeholder for now */ 1211 break; 1212 #endif /* RSS */ 1213 default: 1214 error = ENOPROTOOPT; 1215 break; 1216 } 1217 if (error) 1218 break; 1219 } 1220 m_freem(control); 1221 } 1222 if (error) { 1223 if (unlock_inp == UH_WLOCKED) 1224 INP_WUNLOCK(inp); 1225 else 1226 INP_RUNLOCK(inp); 1227 m_freem(m); 1228 return (error); 1229 } 1230 1231 /* 1232 * Depending on whether or not the application has bound or connected 1233 * the socket, we may have to do varying levels of work. The optimal 1234 * case is for a connected UDP socket, as a global lock isn't 1235 * required at all. 1236 * 1237 * In order to decide which we need, we require stability of the 1238 * inpcb binding, which we ensure by acquiring a read lock on the 1239 * inpcb. This doesn't strictly follow the lock order, so we play 1240 * the trylock and retry game; note that we may end up with more 1241 * conservative locks than required the second time around, so later 1242 * assertions have to accept that. Further analysis of the number of 1243 * misses under contention is required. 1244 * 1245 * XXXRW: Check that hash locking update here is correct. 1246 */ 1247 pr = inp->inp_socket->so_proto->pr_protocol; 1248 pcbinfo = udp_get_inpcbinfo(pr); 1249 sin = (struct sockaddr_in *)addr; 1250 if (sin != NULL && 1251 (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 1252 INP_HASH_WLOCK(pcbinfo); 1253 unlock_udbinfo = UH_WLOCKED; 1254 } else if ((sin != NULL && ( 1255 (sin->sin_addr.s_addr == INADDR_ANY) || 1256 (sin->sin_addr.s_addr == INADDR_BROADCAST) || 1257 (inp->inp_laddr.s_addr == INADDR_ANY) || 1258 (inp->inp_lport == 0))) || 1259 (src.sin_family == AF_INET)) { 1260 INP_HASH_RLOCK(pcbinfo); 1261 unlock_udbinfo = UH_RLOCKED; 1262 } else 1263 unlock_udbinfo = UH_UNLOCKED; 1264 1265 /* 1266 * If the IP_SENDSRCADDR control message was specified, override the 1267 * source address for this datagram. Its use is invalidated if the 1268 * address thus specified is incomplete or clobbers other inpcbs. 1269 */ 1270 laddr = inp->inp_laddr; 1271 lport = inp->inp_lport; 1272 if (src.sin_family == AF_INET) { 1273 INP_HASH_LOCK_ASSERT(pcbinfo); 1274 if ((lport == 0) || 1275 (laddr.s_addr == INADDR_ANY && 1276 src.sin_addr.s_addr == INADDR_ANY)) { 1277 error = EINVAL; 1278 goto release; 1279 } 1280 error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 1281 &laddr.s_addr, &lport, td->td_ucred); 1282 if (error) 1283 goto release; 1284 } 1285 1286 /* 1287 * If a UDP socket has been connected, then a local address/port will 1288 * have been selected and bound. 1289 * 1290 * If a UDP socket has not been connected to, then an explicit 1291 * destination address must be used, in which case a local 1292 * address/port may not have been selected and bound. 1293 */ 1294 if (sin != NULL) { 1295 INP_LOCK_ASSERT(inp); 1296 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1297 error = EISCONN; 1298 goto release; 1299 } 1300 1301 /* 1302 * Jail may rewrite the destination address, so let it do 1303 * that before we use it. 1304 */ 1305 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1306 if (error) 1307 goto release; 1308 1309 /* 1310 * If a local address or port hasn't yet been selected, or if 1311 * the destination address needs to be rewritten due to using 1312 * a special INADDR_ constant, invoke in_pcbconnect_setup() 1313 * to do the heavy lifting. Once a port is selected, we 1314 * commit the binding back to the socket; we also commit the 1315 * binding of the address if in jail. 1316 * 1317 * If we already have a valid binding and we're not 1318 * requesting a destination address rewrite, use a fast path. 1319 */ 1320 if (inp->inp_laddr.s_addr == INADDR_ANY || 1321 inp->inp_lport == 0 || 1322 sin->sin_addr.s_addr == INADDR_ANY || 1323 sin->sin_addr.s_addr == INADDR_BROADCAST) { 1324 INP_HASH_LOCK_ASSERT(pcbinfo); 1325 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, 1326 &lport, &faddr.s_addr, &fport, NULL, 1327 td->td_ucred); 1328 if (error) 1329 goto release; 1330 1331 /* 1332 * XXXRW: Why not commit the port if the address is 1333 * !INADDR_ANY? 1334 */ 1335 /* Commit the local port if newly assigned. */ 1336 if (inp->inp_laddr.s_addr == INADDR_ANY && 1337 inp->inp_lport == 0) { 1338 INP_WLOCK_ASSERT(inp); 1339 INP_HASH_WLOCK_ASSERT(pcbinfo); 1340 /* 1341 * Remember addr if jailed, to prevent 1342 * rebinding. 1343 */ 1344 if (prison_flag(td->td_ucred, PR_IP4)) 1345 inp->inp_laddr = laddr; 1346 inp->inp_lport = lport; 1347 if (in_pcbinshash(inp) != 0) { 1348 inp->inp_lport = 0; 1349 error = EAGAIN; 1350 goto release; 1351 } 1352 inp->inp_flags |= INP_ANONPORT; 1353 } 1354 } else { 1355 faddr = sin->sin_addr; 1356 fport = sin->sin_port; 1357 } 1358 } else { 1359 INP_LOCK_ASSERT(inp); 1360 faddr = inp->inp_faddr; 1361 fport = inp->inp_fport; 1362 if (faddr.s_addr == INADDR_ANY) { 1363 error = ENOTCONN; 1364 goto release; 1365 } 1366 } 1367 1368 /* 1369 * Calculate data length and get a mbuf for UDP, IP, and possible 1370 * link-layer headers. Immediate slide the data pointer back forward 1371 * since we won't use that space at this layer. 1372 */ 1373 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT); 1374 if (m == NULL) { 1375 error = ENOBUFS; 1376 goto release; 1377 } 1378 m->m_data += max_linkhdr; 1379 m->m_len -= max_linkhdr; 1380 m->m_pkthdr.len -= max_linkhdr; 1381 1382 /* 1383 * Fill in mbuf with extended UDP header and addresses and length put 1384 * into network format. 1385 */ 1386 ui = mtod(m, struct udpiphdr *); 1387 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 1388 ui->ui_pr = pr; 1389 ui->ui_src = laddr; 1390 ui->ui_dst = faddr; 1391 ui->ui_sport = lport; 1392 ui->ui_dport = fport; 1393 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 1394 if (pr == IPPROTO_UDPLITE) { 1395 struct udpcb *up; 1396 uint16_t plen; 1397 1398 up = intoudpcb(inp); 1399 cscov = up->u_txcslen; 1400 plen = (u_short)len + sizeof(struct udphdr); 1401 if (cscov >= plen) 1402 cscov = 0; 1403 ui->ui_len = htons(plen); 1404 ui->ui_ulen = htons(cscov); 1405 /* 1406 * For UDP-Lite, checksum coverage length of zero means 1407 * the entire UDPLite packet is covered by the checksum. 1408 */ 1409 cscov_partial = (cscov == 0) ? 0 : 1; 1410 } else 1411 ui->ui_v = IPVERSION << 4; 1412 1413 /* 1414 * Set the Don't Fragment bit in the IP header. 1415 */ 1416 if (inp->inp_flags & INP_DONTFRAG) { 1417 struct ip *ip; 1418 1419 ip = (struct ip *)&ui->ui_i; 1420 ip->ip_off |= htons(IP_DF); 1421 } 1422 1423 ipflags = 0; 1424 if (inp->inp_socket->so_options & SO_DONTROUTE) 1425 ipflags |= IP_ROUTETOIF; 1426 if (inp->inp_socket->so_options & SO_BROADCAST) 1427 ipflags |= IP_ALLOWBROADCAST; 1428 if (inp->inp_flags & INP_ONESBCAST) 1429 ipflags |= IP_SENDONES; 1430 1431 #ifdef MAC 1432 mac_inpcb_create_mbuf(inp, m); 1433 #endif 1434 1435 /* 1436 * Set up checksum and output datagram. 1437 */ 1438 ui->ui_sum = 0; 1439 if (pr == IPPROTO_UDPLITE) { 1440 if (inp->inp_flags & INP_ONESBCAST) 1441 faddr.s_addr = INADDR_BROADCAST; 1442 if (cscov_partial) { 1443 if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0) 1444 ui->ui_sum = 0xffff; 1445 } else { 1446 if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0) 1447 ui->ui_sum = 0xffff; 1448 } 1449 } else if (V_udp_cksum) { 1450 if (inp->inp_flags & INP_ONESBCAST) 1451 faddr.s_addr = INADDR_BROADCAST; 1452 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 1453 htons((u_short)len + sizeof(struct udphdr) + pr)); 1454 m->m_pkthdr.csum_flags = CSUM_UDP; 1455 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 1456 } 1457 ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len); 1458 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 1459 ((struct ip *)ui)->ip_tos = tos; /* XXX */ 1460 UDPSTAT_INC(udps_opackets); 1461 1462 /* 1463 * Setup flowid / RSS information for outbound socket. 1464 * 1465 * Once the UDP code decides to set a flowid some other way, 1466 * this allows the flowid to be overridden by userland. 1467 */ 1468 if (flowtype != M_HASHTYPE_NONE) { 1469 m->m_pkthdr.flowid = flowid; 1470 M_HASHTYPE_SET(m, flowtype); 1471 #ifdef RSS 1472 } else { 1473 uint32_t hash_val, hash_type; 1474 /* 1475 * Calculate an appropriate RSS hash for UDP and 1476 * UDP Lite. 1477 * 1478 * The called function will take care of figuring out 1479 * whether a 2-tuple or 4-tuple hash is required based 1480 * on the currently configured scheme. 1481 * 1482 * Later later on connected socket values should be 1483 * cached in the inpcb and reused, rather than constantly 1484 * re-calculating it. 1485 * 1486 * UDP Lite is a different protocol number and will 1487 * likely end up being hashed as a 2-tuple until 1488 * RSS / NICs grow UDP Lite protocol awareness. 1489 */ 1490 if (rss_proto_software_hash_v4(faddr, laddr, fport, lport, 1491 pr, &hash_val, &hash_type) == 0) { 1492 m->m_pkthdr.flowid = hash_val; 1493 M_HASHTYPE_SET(m, hash_type); 1494 } 1495 #endif 1496 } 1497 1498 #ifdef RSS 1499 /* 1500 * Don't override with the inp cached flowid value. 1501 * 1502 * Depending upon the kind of send being done, the inp 1503 * flowid/flowtype values may actually not be appropriate 1504 * for this particular socket send. 1505 * 1506 * We should either leave the flowid at zero (which is what is 1507 * currently done) or set it to some software generated 1508 * hash value based on the packet contents. 1509 */ 1510 ipflags |= IP_NODEFAULTFLOWID; 1511 #endif /* RSS */ 1512 1513 if (unlock_udbinfo == UH_WLOCKED) 1514 INP_HASH_WUNLOCK(pcbinfo); 1515 else if (unlock_udbinfo == UH_RLOCKED) 1516 INP_HASH_RUNLOCK(pcbinfo); 1517 UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u); 1518 error = ip_output(m, inp->inp_options, 1519 (unlock_inp == UH_WLOCKED ? &inp->inp_route : NULL), ipflags, 1520 inp->inp_moptions, inp); 1521 if (unlock_inp == UH_WLOCKED) 1522 INP_WUNLOCK(inp); 1523 else 1524 INP_RUNLOCK(inp); 1525 return (error); 1526 1527 release: 1528 if (unlock_udbinfo == UH_WLOCKED) { 1529 KASSERT(unlock_inp == UH_WLOCKED, 1530 ("%s: excl udbinfo lock, shared inp lock", __func__)); 1531 INP_HASH_WUNLOCK(pcbinfo); 1532 INP_WUNLOCK(inp); 1533 } else if (unlock_udbinfo == UH_RLOCKED) { 1534 KASSERT(unlock_inp == UH_RLOCKED, 1535 ("%s: shared udbinfo lock, excl inp lock", __func__)); 1536 INP_HASH_RUNLOCK(pcbinfo); 1537 INP_RUNLOCK(inp); 1538 } else if (unlock_inp == UH_WLOCKED) 1539 INP_WUNLOCK(inp); 1540 else 1541 INP_RUNLOCK(inp); 1542 m_freem(m); 1543 return (error); 1544 } 1545 1546 static void 1547 udp_abort(struct socket *so) 1548 { 1549 struct inpcb *inp; 1550 struct inpcbinfo *pcbinfo; 1551 1552 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1553 inp = sotoinpcb(so); 1554 KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 1555 INP_WLOCK(inp); 1556 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1557 INP_HASH_WLOCK(pcbinfo); 1558 in_pcbdisconnect(inp); 1559 inp->inp_laddr.s_addr = INADDR_ANY; 1560 INP_HASH_WUNLOCK(pcbinfo); 1561 soisdisconnected(so); 1562 } 1563 INP_WUNLOCK(inp); 1564 } 1565 1566 static int 1567 udp_attach(struct socket *so, int proto, struct thread *td) 1568 { 1569 static uint32_t udp_flowid; 1570 struct inpcb *inp; 1571 struct inpcbinfo *pcbinfo; 1572 int error; 1573 1574 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1575 inp = sotoinpcb(so); 1576 KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 1577 error = soreserve(so, udp_sendspace, udp_recvspace); 1578 if (error) 1579 return (error); 1580 INP_INFO_WLOCK(pcbinfo); 1581 error = in_pcballoc(so, pcbinfo); 1582 if (error) { 1583 INP_INFO_WUNLOCK(pcbinfo); 1584 return (error); 1585 } 1586 1587 inp = sotoinpcb(so); 1588 inp->inp_vflag |= INP_IPV4; 1589 inp->inp_ip_ttl = V_ip_defttl; 1590 inp->inp_flowid = atomic_fetchadd_int(&udp_flowid, 1); 1591 inp->inp_flowtype = M_HASHTYPE_OPAQUE; 1592 1593 error = udp_newudpcb(inp); 1594 if (error) { 1595 in_pcbdetach(inp); 1596 in_pcbfree(inp); 1597 INP_INFO_WUNLOCK(pcbinfo); 1598 return (error); 1599 } 1600 1601 INP_WUNLOCK(inp); 1602 INP_INFO_WUNLOCK(pcbinfo); 1603 return (0); 1604 } 1605 #endif /* INET */ 1606 1607 int 1608 udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx) 1609 { 1610 struct inpcb *inp; 1611 struct udpcb *up; 1612 1613 KASSERT(so->so_type == SOCK_DGRAM, 1614 ("udp_set_kernel_tunneling: !dgram")); 1615 inp = sotoinpcb(so); 1616 KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL")); 1617 INP_WLOCK(inp); 1618 up = intoudpcb(inp); 1619 if ((up->u_tun_func != NULL) || 1620 (up->u_icmp_func != NULL)) { 1621 INP_WUNLOCK(inp); 1622 return (EBUSY); 1623 } 1624 up->u_tun_func = f; 1625 up->u_icmp_func = i; 1626 up->u_tun_ctx = ctx; 1627 INP_WUNLOCK(inp); 1628 return (0); 1629 } 1630 1631 #ifdef INET 1632 static int 1633 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1634 { 1635 struct inpcb *inp; 1636 struct inpcbinfo *pcbinfo; 1637 int error; 1638 1639 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1640 inp = sotoinpcb(so); 1641 KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 1642 INP_WLOCK(inp); 1643 INP_HASH_WLOCK(pcbinfo); 1644 error = in_pcbbind(inp, nam, td->td_ucred); 1645 INP_HASH_WUNLOCK(pcbinfo); 1646 INP_WUNLOCK(inp); 1647 return (error); 1648 } 1649 1650 static void 1651 udp_close(struct socket *so) 1652 { 1653 struct inpcb *inp; 1654 struct inpcbinfo *pcbinfo; 1655 1656 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1657 inp = sotoinpcb(so); 1658 KASSERT(inp != NULL, ("udp_close: inp == NULL")); 1659 INP_WLOCK(inp); 1660 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1661 INP_HASH_WLOCK(pcbinfo); 1662 in_pcbdisconnect(inp); 1663 inp->inp_laddr.s_addr = INADDR_ANY; 1664 INP_HASH_WUNLOCK(pcbinfo); 1665 soisdisconnected(so); 1666 } 1667 INP_WUNLOCK(inp); 1668 } 1669 1670 static int 1671 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1672 { 1673 struct inpcb *inp; 1674 struct inpcbinfo *pcbinfo; 1675 struct sockaddr_in *sin; 1676 int error; 1677 1678 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1679 inp = sotoinpcb(so); 1680 KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 1681 INP_WLOCK(inp); 1682 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1683 INP_WUNLOCK(inp); 1684 return (EISCONN); 1685 } 1686 sin = (struct sockaddr_in *)nam; 1687 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1688 if (error != 0) { 1689 INP_WUNLOCK(inp); 1690 return (error); 1691 } 1692 INP_HASH_WLOCK(pcbinfo); 1693 error = in_pcbconnect(inp, nam, td->td_ucred); 1694 INP_HASH_WUNLOCK(pcbinfo); 1695 if (error == 0) 1696 soisconnected(so); 1697 INP_WUNLOCK(inp); 1698 return (error); 1699 } 1700 1701 static void 1702 udp_detach(struct socket *so) 1703 { 1704 struct inpcb *inp; 1705 struct inpcbinfo *pcbinfo; 1706 struct udpcb *up; 1707 1708 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1709 inp = sotoinpcb(so); 1710 KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1711 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1712 ("udp_detach: not disconnected")); 1713 INP_INFO_WLOCK(pcbinfo); 1714 INP_WLOCK(inp); 1715 up = intoudpcb(inp); 1716 KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1717 /* XXX defer to epoch_call */ 1718 inp->inp_ppcb = NULL; 1719 in_pcbdetach(inp); 1720 in_pcbfree(inp); 1721 INP_INFO_WUNLOCK(pcbinfo); 1722 udp_discardcb(up); 1723 } 1724 1725 static int 1726 udp_disconnect(struct socket *so) 1727 { 1728 struct inpcb *inp; 1729 struct inpcbinfo *pcbinfo; 1730 1731 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1732 inp = sotoinpcb(so); 1733 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 1734 INP_WLOCK(inp); 1735 if (inp->inp_faddr.s_addr == INADDR_ANY) { 1736 INP_WUNLOCK(inp); 1737 return (ENOTCONN); 1738 } 1739 INP_HASH_WLOCK(pcbinfo); 1740 in_pcbdisconnect(inp); 1741 inp->inp_laddr.s_addr = INADDR_ANY; 1742 INP_HASH_WUNLOCK(pcbinfo); 1743 SOCK_LOCK(so); 1744 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1745 SOCK_UNLOCK(so); 1746 INP_WUNLOCK(inp); 1747 return (0); 1748 } 1749 1750 static int 1751 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1752 struct mbuf *control, struct thread *td) 1753 { 1754 struct inpcb *inp; 1755 1756 inp = sotoinpcb(so); 1757 KASSERT(inp != NULL, ("udp_send: inp == NULL")); 1758 return (udp_output(inp, m, addr, control, td)); 1759 } 1760 #endif /* INET */ 1761 1762 int 1763 udp_shutdown(struct socket *so) 1764 { 1765 struct inpcb *inp; 1766 1767 inp = sotoinpcb(so); 1768 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 1769 INP_WLOCK(inp); 1770 socantsendmore(so); 1771 INP_WUNLOCK(inp); 1772 return (0); 1773 } 1774 1775 #ifdef INET 1776 struct pr_usrreqs udp_usrreqs = { 1777 .pru_abort = udp_abort, 1778 .pru_attach = udp_attach, 1779 .pru_bind = udp_bind, 1780 .pru_connect = udp_connect, 1781 .pru_control = in_control, 1782 .pru_detach = udp_detach, 1783 .pru_disconnect = udp_disconnect, 1784 .pru_peeraddr = in_getpeeraddr, 1785 .pru_send = udp_send, 1786 .pru_soreceive = soreceive_dgram, 1787 .pru_sosend = sosend_dgram, 1788 .pru_shutdown = udp_shutdown, 1789 .pru_sockaddr = in_getsockaddr, 1790 .pru_sosetlabel = in_pcbsosetlabel, 1791 .pru_close = udp_close, 1792 }; 1793 #endif /* INET */ 1794