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