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