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