1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3 * The Regents of the University of California. 4 * Copyright (c) 2008 Robert N. M. Watson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 4. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_ipfw.h" 38 #include "opt_inet6.h" 39 #include "opt_ipsec.h" 40 #include "opt_mac.h" 41 42 #include <sys/param.h> 43 #include <sys/domain.h> 44 #include <sys/eventhandler.h> 45 #include <sys/jail.h> 46 #include <sys/kernel.h> 47 #include <sys/lock.h> 48 #include <sys/malloc.h> 49 #include <sys/mbuf.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/protosw.h> 53 #include <sys/signalvar.h> 54 #include <sys/socket.h> 55 #include <sys/socketvar.h> 56 #include <sys/sx.h> 57 #include <sys/sysctl.h> 58 #include <sys/syslog.h> 59 #include <sys/systm.h> 60 61 #include <vm/uma.h> 62 63 #include <net/if.h> 64 #include <net/route.h> 65 66 #include <netinet/in.h> 67 #include <netinet/in_pcb.h> 68 #include <netinet/in_systm.h> 69 #include <netinet/in_var.h> 70 #include <netinet/ip.h> 71 #ifdef INET6 72 #include <netinet/ip6.h> 73 #endif 74 #include <netinet/ip_icmp.h> 75 #include <netinet/icmp_var.h> 76 #include <netinet/ip_var.h> 77 #include <netinet/ip_options.h> 78 #ifdef INET6 79 #include <netinet6/ip6_var.h> 80 #endif 81 #include <netinet/udp.h> 82 #include <netinet/udp_var.h> 83 84 #ifdef IPSEC 85 #include <netipsec/ipsec.h> 86 #endif 87 88 #include <machine/in_cksum.h> 89 90 #include <security/mac/mac_framework.h> 91 92 /* 93 * UDP protocol implementation. 94 * Per RFC 768, August, 1980. 95 */ 96 97 /* 98 * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums 99 * removes the only data integrity mechanism for packets and malformed 100 * packets that would otherwise be discarded due to bad checksums, and may 101 * cause problems (especially for NFS data blocks). 102 */ 103 static int udp_cksum = 1; 104 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum, 105 0, "compute udp checksum"); 106 107 int udp_log_in_vain = 0; 108 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 109 &udp_log_in_vain, 0, "Log all incoming UDP packets"); 110 111 int udp_blackhole = 0; 112 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, &udp_blackhole, 0, 113 "Do not send port unreachables for refused connects"); 114 115 u_long udp_sendspace = 9216; /* really max datagram size */ 116 /* 40 1K datagrams */ 117 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 118 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 119 120 u_long udp_recvspace = 40 * (1024 + 121 #ifdef INET6 122 sizeof(struct sockaddr_in6) 123 #else 124 sizeof(struct sockaddr_in) 125 #endif 126 ); 127 128 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 129 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 130 131 struct inpcbhead udb; /* from udp_var.h */ 132 struct inpcbinfo udbinfo; 133 134 #ifndef UDBHASHSIZE 135 #define UDBHASHSIZE 128 136 #endif 137 138 struct udpstat udpstat; /* from udp_var.h */ 139 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, &udpstat, 140 udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 141 142 static void udp_detach(struct socket *so); 143 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 144 struct mbuf *, struct thread *); 145 146 static void 147 udp_zone_change(void *tag) 148 { 149 150 uma_zone_set_max(udbinfo.ipi_zone, maxsockets); 151 } 152 153 static int 154 udp_inpcb_init(void *mem, int size, int flags) 155 { 156 struct inpcb *inp; 157 158 inp = mem; 159 INP_LOCK_INIT(inp, "inp", "udpinp"); 160 return (0); 161 } 162 163 void 164 udp_init(void) 165 { 166 167 INP_INFO_LOCK_INIT(&udbinfo, "udp"); 168 LIST_INIT(&udb); 169 udbinfo.ipi_listhead = &udb; 170 udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB, 171 &udbinfo.ipi_hashmask); 172 udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB, 173 &udbinfo.ipi_porthashmask); 174 udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL, 175 NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 176 uma_zone_set_max(udbinfo.ipi_zone, maxsockets); 177 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL, 178 EVENTHANDLER_PRI_ANY); 179 } 180 181 /* 182 * Subroutine of udp_input(), which appends the provided mbuf chain to the 183 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 184 * contains the source address. If the socket ends up being an IPv6 socket, 185 * udp_append() will convert to a sockaddr_in6 before passing the address 186 * into the socket code. 187 */ 188 static void 189 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off, 190 struct sockaddr_in *udp_in) 191 { 192 struct sockaddr *append_sa; 193 struct socket *so; 194 struct mbuf *opts = 0; 195 #ifdef INET6 196 struct sockaddr_in6 udp_in6; 197 #endif 198 199 INP_RLOCK_ASSERT(inp); 200 201 #ifdef IPSEC 202 /* Check AH/ESP integrity. */ 203 if (ipsec4_in_reject(n, inp)) { 204 m_freem(n); 205 ipsec4stat.in_polvio++; 206 return; 207 } 208 #endif /* IPSEC */ 209 #ifdef MAC 210 if (mac_inpcb_check_deliver(inp, n) != 0) { 211 m_freem(n); 212 return; 213 } 214 #endif 215 if (inp->inp_flags & INP_CONTROLOPTS || 216 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 217 #ifdef INET6 218 if (inp->inp_vflag & INP_IPV6) 219 ip6_savecontrol_v4(inp, n, &opts); 220 else 221 #endif 222 ip_savecontrol(inp, &opts, ip, n); 223 } 224 #ifdef INET6 225 if (inp->inp_vflag & INP_IPV6) { 226 bzero(&udp_in6, sizeof(udp_in6)); 227 udp_in6.sin6_len = sizeof(udp_in6); 228 udp_in6.sin6_family = AF_INET6; 229 in6_sin_2_v4mapsin6(udp_in, &udp_in6); 230 append_sa = (struct sockaddr *)&udp_in6; 231 } else 232 #endif 233 append_sa = (struct sockaddr *)udp_in; 234 m_adj(n, off); 235 236 so = inp->inp_socket; 237 SOCKBUF_LOCK(&so->so_rcv); 238 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 239 SOCKBUF_UNLOCK(&so->so_rcv); 240 m_freem(n); 241 if (opts) 242 m_freem(opts); 243 udpstat.udps_fullsock++; 244 } else 245 sorwakeup_locked(so); 246 } 247 248 void 249 udp_input(struct mbuf *m, int off) 250 { 251 int iphlen = off; 252 struct ip *ip; 253 struct udphdr *uh; 254 struct ifnet *ifp; 255 struct inpcb *inp; 256 int len; 257 struct ip save_ip; 258 struct sockaddr_in udp_in; 259 #ifdef IPFIREWALL_FORWARD 260 struct m_tag *fwd_tag; 261 #endif 262 263 ifp = m->m_pkthdr.rcvif; 264 udpstat.udps_ipackets++; 265 266 /* 267 * Strip IP options, if any; should skip this, make available to 268 * user, and use on returned packets, but we don't yet have a way to 269 * check the checksum with options still present. 270 */ 271 if (iphlen > sizeof (struct ip)) { 272 ip_stripoptions(m, (struct mbuf *)0); 273 iphlen = sizeof(struct ip); 274 } 275 276 /* 277 * Get IP and UDP header together in first mbuf. 278 */ 279 ip = mtod(m, struct ip *); 280 if (m->m_len < iphlen + sizeof(struct udphdr)) { 281 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 282 udpstat.udps_hdrops++; 283 return; 284 } 285 ip = mtod(m, struct ip *); 286 } 287 uh = (struct udphdr *)((caddr_t)ip + iphlen); 288 289 /* 290 * Destination port of 0 is illegal, based on RFC768. 291 */ 292 if (uh->uh_dport == 0) 293 goto badunlocked; 294 295 /* 296 * Construct sockaddr format source address. Stuff source address 297 * and datagram in user buffer. 298 */ 299 bzero(&udp_in, sizeof(udp_in)); 300 udp_in.sin_len = sizeof(udp_in); 301 udp_in.sin_family = AF_INET; 302 udp_in.sin_port = uh->uh_sport; 303 udp_in.sin_addr = ip->ip_src; 304 305 /* 306 * Make mbuf data length reflect UDP length. If not enough data to 307 * reflect UDP length, drop. 308 */ 309 len = ntohs((u_short)uh->uh_ulen); 310 if (ip->ip_len != len) { 311 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 312 udpstat.udps_badlen++; 313 goto badunlocked; 314 } 315 m_adj(m, len - ip->ip_len); 316 /* ip->ip_len = len; */ 317 } 318 319 /* 320 * Save a copy of the IP header in case we want restore it for 321 * sending an ICMP error message in response. 322 */ 323 if (!udp_blackhole) 324 save_ip = *ip; 325 else 326 memset(&save_ip, 0, sizeof(save_ip)); 327 328 /* 329 * Checksum extended UDP header and data. 330 */ 331 if (uh->uh_sum) { 332 u_short uh_sum; 333 334 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 335 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 336 uh_sum = m->m_pkthdr.csum_data; 337 else 338 uh_sum = in_pseudo(ip->ip_src.s_addr, 339 ip->ip_dst.s_addr, htonl((u_short)len + 340 m->m_pkthdr.csum_data + IPPROTO_UDP)); 341 uh_sum ^= 0xffff; 342 } else { 343 char b[9]; 344 345 bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 346 bzero(((struct ipovly *)ip)->ih_x1, 9); 347 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 348 uh_sum = in_cksum(m, len + sizeof (struct ip)); 349 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 350 } 351 if (uh_sum) { 352 udpstat.udps_badsum++; 353 m_freem(m); 354 return; 355 } 356 } else 357 udpstat.udps_nosum++; 358 359 #ifdef IPFIREWALL_FORWARD 360 /* 361 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 362 */ 363 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 364 if (fwd_tag != NULL) { 365 struct sockaddr_in *next_hop; 366 367 /* 368 * Do the hack. 369 */ 370 next_hop = (struct sockaddr_in *)(fwd_tag + 1); 371 ip->ip_dst = next_hop->sin_addr; 372 uh->uh_dport = ntohs(next_hop->sin_port); 373 374 /* 375 * Remove the tag from the packet. We don't need it anymore. 376 */ 377 m_tag_delete(m, fwd_tag); 378 } 379 #endif 380 381 INP_INFO_RLOCK(&udbinfo); 382 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 383 in_broadcast(ip->ip_dst, ifp)) { 384 struct inpcb *last; 385 struct ip_moptions *imo; 386 387 last = NULL; 388 LIST_FOREACH(inp, &udb, inp_list) { 389 if (inp->inp_lport != uh->uh_dport) 390 continue; 391 #ifdef INET6 392 if ((inp->inp_vflag & INP_IPV4) == 0) 393 continue; 394 #endif 395 if (inp->inp_laddr.s_addr != INADDR_ANY && 396 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 397 continue; 398 if (inp->inp_faddr.s_addr != INADDR_ANY && 399 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 400 continue; 401 /* 402 * XXX: Do not check source port of incoming datagram 403 * unless inp_connect() has been called to bind the 404 * fport part of the 4-tuple; the source could be 405 * trying to talk to us with an ephemeral port. 406 */ 407 if (inp->inp_fport != 0 && 408 inp->inp_fport != uh->uh_sport) 409 continue; 410 411 INP_RLOCK(inp); 412 413 /* 414 * Handle socket delivery policy for any-source 415 * and source-specific multicast. [RFC3678] 416 */ 417 imo = inp->inp_moptions; 418 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 419 imo != NULL) { 420 struct sockaddr_in sin; 421 struct in_msource *ims; 422 int blocked, mode; 423 size_t idx; 424 425 bzero(&sin, sizeof(struct sockaddr_in)); 426 sin.sin_len = sizeof(struct sockaddr_in); 427 sin.sin_family = AF_INET; 428 sin.sin_addr = ip->ip_dst; 429 430 blocked = 0; 431 idx = imo_match_group(imo, ifp, 432 (struct sockaddr *)&sin); 433 if (idx == -1) { 434 /* 435 * No group membership for this socket. 436 * Do not bump udps_noportbcast, as 437 * this will happen further down. 438 */ 439 blocked++; 440 } else { 441 /* 442 * Check for a multicast source filter 443 * entry on this socket for this group. 444 * MCAST_EXCLUDE is the default 445 * behaviour. It means default accept; 446 * entries, if present, denote sources 447 * to be excluded from delivery. 448 */ 449 ims = imo_match_source(imo, idx, 450 (struct sockaddr *)&udp_in); 451 mode = imo->imo_mfilters[idx].imf_fmode; 452 if ((ims != NULL && 453 mode == MCAST_EXCLUDE) || 454 (ims == NULL && 455 mode == MCAST_INCLUDE)) { 456 #ifdef DIAGNOSTIC 457 if (bootverbose) { 458 printf("%s: blocked by" 459 " source filter\n", 460 __func__); 461 } 462 #endif 463 udpstat.udps_filtermcast++; 464 blocked++; 465 } 466 } 467 if (blocked != 0) { 468 INP_RUNLOCK(inp); 469 continue; 470 } 471 } 472 if (last != NULL) { 473 struct mbuf *n; 474 475 n = m_copy(m, 0, M_COPYALL); 476 if (n != NULL) 477 udp_append(last, ip, n, iphlen + 478 sizeof(struct udphdr), &udp_in); 479 INP_RUNLOCK(last); 480 } 481 last = inp; 482 /* 483 * Don't look for additional matches if this one does 484 * not have either the SO_REUSEPORT or SO_REUSEADDR 485 * socket options set. This heuristic avoids 486 * searching through all pcbs in the common case of a 487 * non-shared port. It assumes that an application 488 * will never clear these options after setting them. 489 */ 490 if ((last->inp_socket->so_options & 491 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 492 break; 493 } 494 495 if (last == NULL) { 496 /* 497 * No matching pcb found; discard datagram. (No need 498 * to send an ICMP Port Unreachable for a broadcast 499 * or multicast datgram.) 500 */ 501 udpstat.udps_noportbcast++; 502 goto badheadlocked; 503 } 504 udp_append(last, ip, m, iphlen + sizeof(struct udphdr), 505 &udp_in); 506 INP_RUNLOCK(last); 507 INP_INFO_RUNLOCK(&udbinfo); 508 return; 509 } 510 511 /* 512 * Locate pcb for datagram. 513 */ 514 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 515 ip->ip_dst, uh->uh_dport, 1, ifp); 516 if (inp == NULL) { 517 if (udp_log_in_vain) { 518 char buf[4*sizeof "123"]; 519 520 strcpy(buf, inet_ntoa(ip->ip_dst)); 521 log(LOG_INFO, 522 "Connection attempt to UDP %s:%d from %s:%d\n", 523 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 524 ntohs(uh->uh_sport)); 525 } 526 udpstat.udps_noport++; 527 if (m->m_flags & (M_BCAST | M_MCAST)) { 528 udpstat.udps_noportbcast++; 529 goto badheadlocked; 530 } 531 if (udp_blackhole) 532 goto badheadlocked; 533 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 534 goto badheadlocked; 535 *ip = save_ip; 536 ip->ip_len += iphlen; 537 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 538 INP_INFO_RUNLOCK(&udbinfo); 539 return; 540 } 541 542 /* 543 * Check the minimum TTL for socket. 544 */ 545 INP_RLOCK(inp); 546 INP_INFO_RUNLOCK(&udbinfo); 547 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) { 548 INP_RUNLOCK(inp); 549 goto badunlocked; 550 } 551 udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in); 552 INP_RUNLOCK(inp); 553 return; 554 555 badheadlocked: 556 if (inp) 557 INP_RUNLOCK(inp); 558 INP_INFO_RUNLOCK(&udbinfo); 559 badunlocked: 560 m_freem(m); 561 } 562 563 /* 564 * Notify a udp user of an asynchronous error; just wake up so that they can 565 * collect error status. 566 */ 567 struct inpcb * 568 udp_notify(struct inpcb *inp, int errno) 569 { 570 571 /* 572 * While udp_ctlinput() always calls udp_notify() with a read lock 573 * when invoking it directly, in_pcbnotifyall() currently uses write 574 * locks due to sharing code with TCP. For now, accept either a read 575 * or a write lock, but a read lock is sufficient. 576 */ 577 INP_LOCK_ASSERT(inp); 578 579 inp->inp_socket->so_error = errno; 580 sorwakeup(inp->inp_socket); 581 sowwakeup(inp->inp_socket); 582 return (inp); 583 } 584 585 void 586 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 587 { 588 struct ip *ip = vip; 589 struct udphdr *uh; 590 struct in_addr faddr; 591 struct inpcb *inp; 592 593 faddr = ((struct sockaddr_in *)sa)->sin_addr; 594 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 595 return; 596 597 /* 598 * Redirects don't need to be handled up here. 599 */ 600 if (PRC_IS_REDIRECT(cmd)) 601 return; 602 603 /* 604 * Hostdead is ugly because it goes linearly through all PCBs. 605 * 606 * XXX: We never get this from ICMP, otherwise it makes an excellent 607 * DoS attack on machines with many connections. 608 */ 609 if (cmd == PRC_HOSTDEAD) 610 ip = NULL; 611 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 612 return; 613 if (ip != NULL) { 614 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 615 INP_INFO_RLOCK(&udbinfo); 616 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, 617 ip->ip_src, uh->uh_sport, 0, NULL); 618 if (inp != NULL) { 619 INP_RLOCK(inp); 620 if (inp->inp_socket != NULL) { 621 udp_notify(inp, inetctlerrmap[cmd]); 622 } 623 INP_RUNLOCK(inp); 624 } 625 INP_INFO_RUNLOCK(&udbinfo); 626 } else 627 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], 628 udp_notify); 629 } 630 631 static int 632 udp_pcblist(SYSCTL_HANDLER_ARGS) 633 { 634 int error, i, n; 635 struct inpcb *inp, **inp_list; 636 inp_gen_t gencnt; 637 struct xinpgen xig; 638 639 /* 640 * The process of preparing the PCB list is too time-consuming and 641 * resource-intensive to repeat twice on every request. 642 */ 643 if (req->oldptr == 0) { 644 n = udbinfo.ipi_count; 645 req->oldidx = 2 * (sizeof xig) 646 + (n + n/8) * sizeof(struct xinpcb); 647 return (0); 648 } 649 650 if (req->newptr != 0) 651 return (EPERM); 652 653 /* 654 * OK, now we're committed to doing something. 655 */ 656 INP_INFO_RLOCK(&udbinfo); 657 gencnt = udbinfo.ipi_gencnt; 658 n = udbinfo.ipi_count; 659 INP_INFO_RUNLOCK(&udbinfo); 660 661 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 662 + n * sizeof(struct xinpcb)); 663 if (error != 0) 664 return (error); 665 666 xig.xig_len = sizeof xig; 667 xig.xig_count = n; 668 xig.xig_gen = gencnt; 669 xig.xig_sogen = so_gencnt; 670 error = SYSCTL_OUT(req, &xig, sizeof xig); 671 if (error) 672 return (error); 673 674 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 675 if (inp_list == 0) 676 return (ENOMEM); 677 678 INP_INFO_RLOCK(&udbinfo); 679 for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n; 680 inp = LIST_NEXT(inp, inp_list)) { 681 INP_RLOCK(inp); 682 if (inp->inp_gencnt <= gencnt && 683 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 684 inp_list[i++] = inp; 685 INP_RUNLOCK(inp); 686 } 687 INP_INFO_RUNLOCK(&udbinfo); 688 n = i; 689 690 error = 0; 691 for (i = 0; i < n; i++) { 692 inp = inp_list[i]; 693 INP_RLOCK(inp); 694 if (inp->inp_gencnt <= gencnt) { 695 struct xinpcb xi; 696 bzero(&xi, sizeof(xi)); 697 xi.xi_len = sizeof xi; 698 /* XXX should avoid extra copy */ 699 bcopy(inp, &xi.xi_inp, sizeof *inp); 700 if (inp->inp_socket) 701 sotoxsocket(inp->inp_socket, &xi.xi_socket); 702 xi.xi_inp.inp_gencnt = inp->inp_gencnt; 703 INP_RUNLOCK(inp); 704 error = SYSCTL_OUT(req, &xi, sizeof xi); 705 } else 706 INP_RUNLOCK(inp); 707 } 708 if (!error) { 709 /* 710 * Give the user an updated idea of our state. If the 711 * generation differs from what we told her before, she knows 712 * that something happened while we were processing this 713 * request, and it might be necessary to retry. 714 */ 715 INP_INFO_RLOCK(&udbinfo); 716 xig.xig_gen = udbinfo.ipi_gencnt; 717 xig.xig_sogen = so_gencnt; 718 xig.xig_count = udbinfo.ipi_count; 719 INP_INFO_RUNLOCK(&udbinfo); 720 error = SYSCTL_OUT(req, &xig, sizeof xig); 721 } 722 free(inp_list, M_TEMP); 723 return (error); 724 } 725 726 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 727 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 728 729 static int 730 udp_getcred(SYSCTL_HANDLER_ARGS) 731 { 732 struct xucred xuc; 733 struct sockaddr_in addrs[2]; 734 struct inpcb *inp; 735 int error; 736 737 error = priv_check(req->td, PRIV_NETINET_GETCRED); 738 if (error) 739 return (error); 740 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 741 if (error) 742 return (error); 743 INP_INFO_RLOCK(&udbinfo); 744 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 745 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 746 if (inp != NULL) { 747 INP_RLOCK(inp); 748 INP_INFO_RUNLOCK(&udbinfo); 749 if (inp->inp_socket == NULL) 750 error = ENOENT; 751 if (error == 0) 752 error = cr_canseesocket(req->td->td_ucred, 753 inp->inp_socket); 754 if (error == 0) 755 cru2x(inp->inp_socket->so_cred, &xuc); 756 INP_RUNLOCK(inp); 757 } else { 758 INP_INFO_RUNLOCK(&udbinfo); 759 error = ENOENT; 760 } 761 if (error == 0) 762 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 763 return (error); 764 } 765 766 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 767 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 768 udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 769 770 static int 771 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, 772 struct mbuf *control, struct thread *td) 773 { 774 struct udpiphdr *ui; 775 int len = m->m_pkthdr.len; 776 struct in_addr faddr, laddr; 777 struct cmsghdr *cm; 778 struct sockaddr_in *sin, src; 779 int error = 0; 780 int ipflags; 781 u_short fport, lport; 782 int unlock_udbinfo; 783 784 /* 785 * udp_output() may need to temporarily bind or connect the current 786 * inpcb. As such, we don't know up front whether we will need the 787 * pcbinfo lock or not. Do any work to decide what is needed up 788 * front before acquiring any locks. 789 */ 790 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 791 if (control) 792 m_freem(control); 793 m_freem(m); 794 return (EMSGSIZE); 795 } 796 797 src.sin_family = 0; 798 if (control != NULL) { 799 /* 800 * XXX: Currently, we assume all the optional information is 801 * stored in a single mbuf. 802 */ 803 if (control->m_next) { 804 m_freem(control); 805 m_freem(m); 806 return (EINVAL); 807 } 808 for (; control->m_len > 0; 809 control->m_data += CMSG_ALIGN(cm->cmsg_len), 810 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 811 cm = mtod(control, struct cmsghdr *); 812 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 813 || cm->cmsg_len > control->m_len) { 814 error = EINVAL; 815 break; 816 } 817 if (cm->cmsg_level != IPPROTO_IP) 818 continue; 819 820 switch (cm->cmsg_type) { 821 case IP_SENDSRCADDR: 822 if (cm->cmsg_len != 823 CMSG_LEN(sizeof(struct in_addr))) { 824 error = EINVAL; 825 break; 826 } 827 bzero(&src, sizeof(src)); 828 src.sin_family = AF_INET; 829 src.sin_len = sizeof(src); 830 src.sin_port = inp->inp_lport; 831 src.sin_addr = 832 *(struct in_addr *)CMSG_DATA(cm); 833 break; 834 835 default: 836 error = ENOPROTOOPT; 837 break; 838 } 839 if (error) 840 break; 841 } 842 m_freem(control); 843 } 844 if (error) { 845 m_freem(m); 846 return (error); 847 } 848 849 /* 850 * Depending on whether or not the application has bound or connected 851 * the socket, we may have to do varying levels of work. The optimal 852 * case is for a connected UDP socket, as a global lock isn't 853 * required at all. 854 * 855 * In order to decide which we need, we require stability of the 856 * inpcb binding, which we ensure by acquiring a read lock on the 857 * inpcb. This doesn't strictly follow the lock order, so we play 858 * the trylock and retry game; note that we may end up with more 859 * conservative locks than required the second time around, so later 860 * assertions have to accept that. Further analysis of the number of 861 * misses under contention is required. 862 */ 863 sin = (struct sockaddr_in *)addr; 864 INP_RLOCK(inp); 865 if (sin != NULL && 866 (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 867 INP_RUNLOCK(inp); 868 INP_INFO_WLOCK(&udbinfo); 869 INP_WLOCK(inp); 870 unlock_udbinfo = 2; 871 } else if ((sin != NULL && ( 872 (sin->sin_addr.s_addr == INADDR_ANY) || 873 (sin->sin_addr.s_addr == INADDR_BROADCAST) || 874 (inp->inp_laddr.s_addr == INADDR_ANY) || 875 (inp->inp_lport == 0))) || 876 (src.sin_family == AF_INET)) { 877 if (!INP_INFO_TRY_RLOCK(&udbinfo)) { 878 INP_RUNLOCK(inp); 879 INP_INFO_RLOCK(&udbinfo); 880 INP_RLOCK(inp); 881 } 882 unlock_udbinfo = 1; 883 } else 884 unlock_udbinfo = 0; 885 886 /* 887 * If the IP_SENDSRCADDR control message was specified, override the 888 * source address for this datagram. Its use is invalidated if the 889 * address thus specified is incomplete or clobbers other inpcbs. 890 */ 891 laddr = inp->inp_laddr; 892 lport = inp->inp_lport; 893 if (src.sin_family == AF_INET) { 894 INP_INFO_LOCK_ASSERT(&udbinfo); 895 if ((lport == 0) || 896 (laddr.s_addr == INADDR_ANY && 897 src.sin_addr.s_addr == INADDR_ANY)) { 898 error = EINVAL; 899 goto release; 900 } 901 error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 902 &laddr.s_addr, &lport, td->td_ucred); 903 if (error) 904 goto release; 905 } 906 907 /* 908 * If a UDP socket has been connected, then a local address/port will 909 * have been selected and bound. 910 * 911 * If a UDP socket has not been connected to, then an explicit 912 * destination address must be used, in which case a local 913 * address/port may not have been selected and bound. 914 */ 915 if (sin != NULL) { 916 INP_LOCK_ASSERT(inp); 917 if (inp->inp_faddr.s_addr != INADDR_ANY) { 918 error = EISCONN; 919 goto release; 920 } 921 922 /* 923 * Jail may rewrite the destination address, so let it do 924 * that before we use it. 925 */ 926 if (jailed(td->td_ucred)) 927 prison_remote_ip(td->td_ucred, 0, 928 &sin->sin_addr.s_addr); 929 930 /* 931 * If a local address or port hasn't yet been selected, or if 932 * the destination address needs to be rewritten due to using 933 * a special INADDR_ constant, invoke in_pcbconnect_setup() 934 * to do the heavy lifting. Once a port is selected, we 935 * commit the binding back to the socket; we also commit the 936 * binding of the address if in jail. 937 * 938 * If we already have a valid binding and we're not 939 * requesting a destination address rewrite, use a fast path. 940 */ 941 if (inp->inp_laddr.s_addr == INADDR_ANY || 942 inp->inp_lport == 0 || 943 sin->sin_addr.s_addr == INADDR_ANY || 944 sin->sin_addr.s_addr == INADDR_BROADCAST) { 945 INP_INFO_LOCK_ASSERT(&udbinfo); 946 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, 947 &lport, &faddr.s_addr, &fport, NULL, 948 td->td_ucred); 949 if (error) 950 goto release; 951 952 /* 953 * XXXRW: Why not commit the port if the address is 954 * !INADDR_ANY? 955 */ 956 /* Commit the local port if newly assigned. */ 957 if (inp->inp_laddr.s_addr == INADDR_ANY && 958 inp->inp_lport == 0) { 959 INP_INFO_WLOCK_ASSERT(&udbinfo); 960 INP_WLOCK_ASSERT(inp); 961 /* 962 * Remember addr if jailed, to prevent 963 * rebinding. 964 */ 965 if (jailed(td->td_ucred)) 966 inp->inp_laddr = laddr; 967 inp->inp_lport = lport; 968 if (in_pcbinshash(inp) != 0) { 969 inp->inp_lport = 0; 970 error = EAGAIN; 971 goto release; 972 } 973 inp->inp_flags |= INP_ANONPORT; 974 } 975 } else { 976 faddr = sin->sin_addr; 977 fport = sin->sin_port; 978 } 979 } else { 980 INP_LOCK_ASSERT(inp); 981 faddr = inp->inp_faddr; 982 fport = inp->inp_fport; 983 if (faddr.s_addr == INADDR_ANY) { 984 error = ENOTCONN; 985 goto release; 986 } 987 } 988 989 /* 990 * Calculate data length and get a mbuf for UDP, IP, and possible 991 * link-layer headers. Immediate slide the data pointer back forward 992 * since we won't use that space at this layer. 993 */ 994 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT); 995 if (m == NULL) { 996 error = ENOBUFS; 997 goto release; 998 } 999 m->m_data += max_linkhdr; 1000 m->m_len -= max_linkhdr; 1001 m->m_pkthdr.len -= max_linkhdr; 1002 1003 /* 1004 * Fill in mbuf with extended UDP header and addresses and length put 1005 * into network format. 1006 */ 1007 ui = mtod(m, struct udpiphdr *); 1008 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 1009 ui->ui_pr = IPPROTO_UDP; 1010 ui->ui_src = laddr; 1011 ui->ui_dst = faddr; 1012 ui->ui_sport = lport; 1013 ui->ui_dport = fport; 1014 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 1015 1016 /* 1017 * Set the Don't Fragment bit in the IP header. 1018 */ 1019 if (inp->inp_flags & INP_DONTFRAG) { 1020 struct ip *ip; 1021 1022 ip = (struct ip *)&ui->ui_i; 1023 ip->ip_off |= IP_DF; 1024 } 1025 1026 ipflags = 0; 1027 if (inp->inp_socket->so_options & SO_DONTROUTE) 1028 ipflags |= IP_ROUTETOIF; 1029 if (inp->inp_socket->so_options & SO_BROADCAST) 1030 ipflags |= IP_ALLOWBROADCAST; 1031 if (inp->inp_flags & INP_ONESBCAST) 1032 ipflags |= IP_SENDONES; 1033 1034 #ifdef MAC 1035 mac_inpcb_create_mbuf(inp, m); 1036 #endif 1037 1038 /* 1039 * Set up checksum and output datagram. 1040 */ 1041 if (udp_cksum) { 1042 if (inp->inp_flags & INP_ONESBCAST) 1043 faddr.s_addr = INADDR_BROADCAST; 1044 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 1045 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 1046 m->m_pkthdr.csum_flags = CSUM_UDP; 1047 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 1048 } else 1049 ui->ui_sum = 0; 1050 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 1051 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 1052 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 1053 udpstat.udps_opackets++; 1054 1055 if (unlock_udbinfo == 2) 1056 INP_INFO_WUNLOCK(&udbinfo); 1057 else if (unlock_udbinfo == 1) 1058 INP_INFO_RUNLOCK(&udbinfo); 1059 error = ip_output(m, inp->inp_options, NULL, ipflags, 1060 inp->inp_moptions, inp); 1061 if (unlock_udbinfo == 2) 1062 INP_WUNLOCK(inp); 1063 else 1064 INP_RUNLOCK(inp); 1065 return (error); 1066 1067 release: 1068 if (unlock_udbinfo == 2) { 1069 INP_WUNLOCK(inp); 1070 INP_INFO_WUNLOCK(&udbinfo); 1071 } else if (unlock_udbinfo == 1) { 1072 INP_RUNLOCK(inp); 1073 INP_INFO_RUNLOCK(&udbinfo); 1074 } else 1075 INP_RUNLOCK(inp); 1076 m_freem(m); 1077 return (error); 1078 } 1079 1080 static void 1081 udp_abort(struct socket *so) 1082 { 1083 struct inpcb *inp; 1084 1085 inp = sotoinpcb(so); 1086 KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 1087 INP_INFO_WLOCK(&udbinfo); 1088 INP_WLOCK(inp); 1089 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1090 in_pcbdisconnect(inp); 1091 inp->inp_laddr.s_addr = INADDR_ANY; 1092 soisdisconnected(so); 1093 } 1094 INP_WUNLOCK(inp); 1095 INP_INFO_WUNLOCK(&udbinfo); 1096 } 1097 1098 static int 1099 udp_attach(struct socket *so, int proto, struct thread *td) 1100 { 1101 struct inpcb *inp; 1102 int error; 1103 1104 inp = sotoinpcb(so); 1105 KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 1106 error = soreserve(so, udp_sendspace, udp_recvspace); 1107 if (error) 1108 return (error); 1109 INP_INFO_WLOCK(&udbinfo); 1110 error = in_pcballoc(so, &udbinfo); 1111 if (error) { 1112 INP_INFO_WUNLOCK(&udbinfo); 1113 return (error); 1114 } 1115 1116 inp = (struct inpcb *)so->so_pcb; 1117 INP_INFO_WUNLOCK(&udbinfo); 1118 inp->inp_vflag |= INP_IPV4; 1119 inp->inp_ip_ttl = ip_defttl; 1120 INP_WUNLOCK(inp); 1121 return (0); 1122 } 1123 1124 static int 1125 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1126 { 1127 struct inpcb *inp; 1128 int error; 1129 1130 inp = sotoinpcb(so); 1131 KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 1132 INP_INFO_WLOCK(&udbinfo); 1133 INP_WLOCK(inp); 1134 error = in_pcbbind(inp, nam, td->td_ucred); 1135 INP_WUNLOCK(inp); 1136 INP_INFO_WUNLOCK(&udbinfo); 1137 return (error); 1138 } 1139 1140 static void 1141 udp_close(struct socket *so) 1142 { 1143 struct inpcb *inp; 1144 1145 inp = sotoinpcb(so); 1146 KASSERT(inp != NULL, ("udp_close: inp == NULL")); 1147 INP_INFO_WLOCK(&udbinfo); 1148 INP_WLOCK(inp); 1149 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1150 in_pcbdisconnect(inp); 1151 inp->inp_laddr.s_addr = INADDR_ANY; 1152 soisdisconnected(so); 1153 } 1154 INP_WUNLOCK(inp); 1155 INP_INFO_WUNLOCK(&udbinfo); 1156 } 1157 1158 static int 1159 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1160 { 1161 struct inpcb *inp; 1162 int error; 1163 struct sockaddr_in *sin; 1164 1165 inp = sotoinpcb(so); 1166 KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 1167 INP_INFO_WLOCK(&udbinfo); 1168 INP_WLOCK(inp); 1169 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1170 INP_WUNLOCK(inp); 1171 INP_INFO_WUNLOCK(&udbinfo); 1172 return (EISCONN); 1173 } 1174 sin = (struct sockaddr_in *)nam; 1175 if (jailed(td->td_ucred)) 1176 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr); 1177 error = in_pcbconnect(inp, nam, td->td_ucred); 1178 if (error == 0) 1179 soisconnected(so); 1180 INP_WUNLOCK(inp); 1181 INP_INFO_WUNLOCK(&udbinfo); 1182 return (error); 1183 } 1184 1185 static void 1186 udp_detach(struct socket *so) 1187 { 1188 struct inpcb *inp; 1189 1190 inp = sotoinpcb(so); 1191 KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1192 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1193 ("udp_detach: not disconnected")); 1194 INP_INFO_WLOCK(&udbinfo); 1195 INP_WLOCK(inp); 1196 in_pcbdetach(inp); 1197 in_pcbfree(inp); 1198 INP_INFO_WUNLOCK(&udbinfo); 1199 } 1200 1201 static int 1202 udp_disconnect(struct socket *so) 1203 { 1204 struct inpcb *inp; 1205 1206 inp = sotoinpcb(so); 1207 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 1208 INP_INFO_WLOCK(&udbinfo); 1209 INP_WLOCK(inp); 1210 if (inp->inp_faddr.s_addr == INADDR_ANY) { 1211 INP_WUNLOCK(inp); 1212 INP_INFO_WUNLOCK(&udbinfo); 1213 return (ENOTCONN); 1214 } 1215 1216 in_pcbdisconnect(inp); 1217 inp->inp_laddr.s_addr = INADDR_ANY; 1218 SOCK_LOCK(so); 1219 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1220 SOCK_UNLOCK(so); 1221 INP_WUNLOCK(inp); 1222 INP_INFO_WUNLOCK(&udbinfo); 1223 return (0); 1224 } 1225 1226 static int 1227 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1228 struct mbuf *control, struct thread *td) 1229 { 1230 struct inpcb *inp; 1231 1232 inp = sotoinpcb(so); 1233 KASSERT(inp != NULL, ("udp_send: inp == NULL")); 1234 return (udp_output(inp, m, addr, control, td)); 1235 } 1236 1237 int 1238 udp_shutdown(struct socket *so) 1239 { 1240 struct inpcb *inp; 1241 1242 inp = sotoinpcb(so); 1243 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 1244 INP_WLOCK(inp); 1245 socantsendmore(so); 1246 INP_WUNLOCK(inp); 1247 return (0); 1248 } 1249 1250 struct pr_usrreqs udp_usrreqs = { 1251 .pru_abort = udp_abort, 1252 .pru_attach = udp_attach, 1253 .pru_bind = udp_bind, 1254 .pru_connect = udp_connect, 1255 .pru_control = in_control, 1256 .pru_detach = udp_detach, 1257 .pru_disconnect = udp_disconnect, 1258 .pru_peeraddr = in_getpeeraddr, 1259 .pru_send = udp_send, 1260 .pru_soreceive = soreceive_dgram, 1261 .pru_sosend = sosend_dgram, 1262 .pru_shutdown = udp_shutdown, 1263 .pru_sockaddr = in_getsockaddr, 1264 .pru_sosetlabel = in_pcbsosetlabel, 1265 .pru_close = udp_close, 1266 }; 1267