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