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