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