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