1 /*- 2 * Copyright 2004-2005 Robert N. M. Watson 3 * Copyright (c) 1982, 1986, 1989, 1991, 1993 4 * The Regents of the University of California. 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 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_mac.h" 37 38 #include <sys/param.h> 39 #include <sys/domain.h> 40 #include <sys/fcntl.h> 41 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 42 #include <sys/file.h> 43 #include <sys/filedesc.h> 44 #include <sys/jail.h> 45 #include <sys/kernel.h> 46 #include <sys/lock.h> 47 #include <sys/mac.h> 48 #include <sys/mbuf.h> 49 #include <sys/mutex.h> 50 #include <sys/namei.h> 51 #include <sys/proc.h> 52 #include <sys/protosw.h> 53 #include <sys/resourcevar.h> 54 #include <sys/socket.h> 55 #include <sys/socketvar.h> 56 #include <sys/signalvar.h> 57 #include <sys/stat.h> 58 #include <sys/sx.h> 59 #include <sys/sysctl.h> 60 #include <sys/systm.h> 61 #include <sys/un.h> 62 #include <sys/unpcb.h> 63 #include <sys/vnode.h> 64 65 #include <vm/uma.h> 66 67 static uma_zone_t unp_zone; 68 static unp_gen_t unp_gencnt; 69 static u_int unp_count; 70 71 static struct unp_head unp_shead, unp_dhead; 72 73 /* 74 * Unix communications domain. 75 * 76 * TODO: 77 * SEQPACKET, RDM 78 * rethink name space problems 79 * need a proper out-of-band 80 * lock pushdown 81 */ 82 static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 83 static ino_t unp_ino; /* prototype for fake inode numbers */ 84 struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 85 86 /* 87 * Currently, UNIX domain sockets are protected by a single subsystem lock, 88 * which covers global data structures and variables, the contents of each 89 * per-socket unpcb structure, and the so_pcb field in sockets attached to 90 * the UNIX domain. This provides for a moderate degree of paralellism, as 91 * receive operations on UNIX domain sockets do not need to acquire the 92 * subsystem lock. Finer grained locking to permit send() without acquiring 93 * a global lock would be a logical next step. 94 * 95 * The UNIX domain socket lock preceds all socket layer locks, including the 96 * socket lock and socket buffer lock, permitting UNIX domain socket code to 97 * call into socket support routines without releasing its locks. 98 * 99 * Some caution is required in areas where the UNIX domain socket code enters 100 * VFS in order to create or find rendezvous points. This results in 101 * dropping of the UNIX domain socket subsystem lock, acquisition of the 102 * Giant lock, and potential sleeping. This increases the chances of races, 103 * and exposes weaknesses in the socket->protocol API by offering poor 104 * failure modes. 105 */ 106 static struct mtx unp_mtx; 107 #define UNP_LOCK_INIT() \ 108 mtx_init(&unp_mtx, "unp", NULL, MTX_DEF) 109 #define UNP_LOCK() mtx_lock(&unp_mtx) 110 #define UNP_UNLOCK() mtx_unlock(&unp_mtx) 111 #define UNP_LOCK_ASSERT() mtx_assert(&unp_mtx, MA_OWNED) 112 #define UNP_UNLOCK_ASSERT() mtx_assert(&unp_mtx, MA_NOTOWNED) 113 114 static int unp_attach(struct socket *); 115 static void unp_detach(struct unpcb *); 116 static int unp_bind(struct unpcb *,struct sockaddr *, struct thread *); 117 static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 118 static int unp_connect2(struct socket *so, struct socket *so2, int); 119 static void unp_disconnect(struct unpcb *); 120 static void unp_shutdown(struct unpcb *); 121 static void unp_drop(struct unpcb *, int); 122 static void unp_gc(void); 123 static void unp_scan(struct mbuf *, void (*)(struct file *)); 124 static void unp_mark(struct file *); 125 static void unp_discard(struct file *); 126 static void unp_freerights(struct file **, int); 127 static int unp_internalize(struct mbuf **, struct thread *); 128 static int unp_listen(struct socket *, struct unpcb *, struct thread *); 129 130 static int 131 uipc_abort(struct socket *so) 132 { 133 struct unpcb *unp; 134 135 UNP_LOCK(); 136 unp = sotounpcb(so); 137 if (unp == NULL) { 138 UNP_UNLOCK(); 139 return (EINVAL); 140 } 141 unp_drop(unp, ECONNABORTED); 142 unp_detach(unp); 143 UNP_UNLOCK_ASSERT(); 144 ACCEPT_LOCK(); 145 SOCK_LOCK(so); 146 sotryfree(so); 147 return (0); 148 } 149 150 static int 151 uipc_accept(struct socket *so, struct sockaddr **nam) 152 { 153 struct unpcb *unp; 154 const struct sockaddr *sa; 155 156 /* 157 * Pass back name of connected socket, 158 * if it was bound and we are still connected 159 * (our peer may have closed already!). 160 */ 161 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 162 UNP_LOCK(); 163 unp = sotounpcb(so); 164 if (unp == NULL) { 165 UNP_UNLOCK(); 166 free(*nam, M_SONAME); 167 *nam = NULL; 168 return (EINVAL); 169 } 170 if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 171 sa = (struct sockaddr *) unp->unp_conn->unp_addr; 172 else 173 sa = &sun_noname; 174 bcopy(sa, *nam, sa->sa_len); 175 UNP_UNLOCK(); 176 return (0); 177 } 178 179 static int 180 uipc_attach(struct socket *so, int proto, struct thread *td) 181 { 182 struct unpcb *unp = sotounpcb(so); 183 184 if (unp != NULL) 185 return (EISCONN); 186 return (unp_attach(so)); 187 } 188 189 static int 190 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 191 { 192 struct unpcb *unp; 193 int error; 194 195 UNP_LOCK(); 196 unp = sotounpcb(so); 197 if (unp == NULL) { 198 UNP_UNLOCK(); 199 return (EINVAL); 200 } 201 error = unp_bind(unp, nam, td); 202 UNP_UNLOCK(); 203 return (error); 204 } 205 206 static int 207 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 208 { 209 struct unpcb *unp; 210 int error; 211 212 KASSERT(td == curthread, ("uipc_connect: td != curthread")); 213 214 UNP_LOCK(); 215 unp = sotounpcb(so); 216 if (unp == NULL) { 217 UNP_UNLOCK(); 218 return (EINVAL); 219 } 220 error = unp_connect(so, nam, td); 221 UNP_UNLOCK(); 222 return (error); 223 } 224 225 int 226 uipc_connect2(struct socket *so1, struct socket *so2) 227 { 228 struct unpcb *unp; 229 int error; 230 231 UNP_LOCK(); 232 unp = sotounpcb(so1); 233 if (unp == NULL) { 234 UNP_UNLOCK(); 235 return (EINVAL); 236 } 237 error = unp_connect2(so1, so2, PRU_CONNECT2); 238 UNP_UNLOCK(); 239 return (error); 240 } 241 242 /* control is EOPNOTSUPP */ 243 244 static int 245 uipc_detach(struct socket *so) 246 { 247 struct unpcb *unp; 248 249 UNP_LOCK(); 250 unp = sotounpcb(so); 251 if (unp == NULL) { 252 UNP_UNLOCK(); 253 return (EINVAL); 254 } 255 unp_detach(unp); 256 UNP_UNLOCK_ASSERT(); 257 return (0); 258 } 259 260 static int 261 uipc_disconnect(struct socket *so) 262 { 263 struct unpcb *unp; 264 265 UNP_LOCK(); 266 unp = sotounpcb(so); 267 if (unp == NULL) { 268 UNP_UNLOCK(); 269 return (EINVAL); 270 } 271 unp_disconnect(unp); 272 UNP_UNLOCK(); 273 return (0); 274 } 275 276 static int 277 uipc_listen(struct socket *so, struct thread *td) 278 { 279 struct unpcb *unp; 280 int error; 281 282 UNP_LOCK(); 283 unp = sotounpcb(so); 284 if (unp == NULL || unp->unp_vnode == NULL) { 285 UNP_UNLOCK(); 286 return (EINVAL); 287 } 288 error = unp_listen(so, unp, td); 289 UNP_UNLOCK(); 290 return (error); 291 } 292 293 static int 294 uipc_peeraddr(struct socket *so, struct sockaddr **nam) 295 { 296 struct unpcb *unp; 297 const struct sockaddr *sa; 298 299 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 300 UNP_LOCK(); 301 unp = sotounpcb(so); 302 if (unp == NULL) { 303 UNP_UNLOCK(); 304 free(*nam, M_SONAME); 305 *nam = NULL; 306 return (EINVAL); 307 } 308 if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 309 sa = (struct sockaddr *) unp->unp_conn->unp_addr; 310 else { 311 /* 312 * XXX: It seems that this test always fails even when 313 * connection is established. So, this else clause is 314 * added as workaround to return PF_LOCAL sockaddr. 315 */ 316 sa = &sun_noname; 317 } 318 bcopy(sa, *nam, sa->sa_len); 319 UNP_UNLOCK(); 320 return (0); 321 } 322 323 static int 324 uipc_rcvd(struct socket *so, int flags) 325 { 326 struct unpcb *unp; 327 struct socket *so2; 328 u_long newhiwat; 329 330 UNP_LOCK(); 331 unp = sotounpcb(so); 332 if (unp == NULL) { 333 UNP_UNLOCK(); 334 return (EINVAL); 335 } 336 switch (so->so_type) { 337 case SOCK_DGRAM: 338 panic("uipc_rcvd DGRAM?"); 339 /*NOTREACHED*/ 340 341 case SOCK_STREAM: 342 if (unp->unp_conn == NULL) 343 break; 344 so2 = unp->unp_conn->unp_socket; 345 SOCKBUF_LOCK(&so2->so_snd); 346 SOCKBUF_LOCK(&so->so_rcv); 347 /* 348 * Adjust backpressure on sender 349 * and wakeup any waiting to write. 350 */ 351 so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 352 unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 353 newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 354 so->so_rcv.sb_cc; 355 (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 356 newhiwat, RLIM_INFINITY); 357 unp->unp_cc = so->so_rcv.sb_cc; 358 SOCKBUF_UNLOCK(&so->so_rcv); 359 sowwakeup_locked(so2); 360 break; 361 362 default: 363 panic("uipc_rcvd unknown socktype"); 364 } 365 UNP_UNLOCK(); 366 return (0); 367 } 368 369 /* pru_rcvoob is EOPNOTSUPP */ 370 371 static int 372 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 373 struct mbuf *control, struct thread *td) 374 { 375 int error = 0; 376 struct unpcb *unp; 377 struct socket *so2; 378 u_long newhiwat; 379 380 unp = sotounpcb(so); 381 if (unp == NULL) { 382 error = EINVAL; 383 goto release; 384 } 385 if (flags & PRUS_OOB) { 386 error = EOPNOTSUPP; 387 goto release; 388 } 389 390 if (control != NULL && (error = unp_internalize(&control, td))) 391 goto release; 392 393 UNP_LOCK(); 394 unp = sotounpcb(so); 395 if (unp == NULL) { 396 UNP_UNLOCK(); 397 error = EINVAL; 398 goto dispose_release; 399 } 400 401 switch (so->so_type) { 402 case SOCK_DGRAM: 403 { 404 const struct sockaddr *from; 405 406 if (nam != NULL) { 407 if (unp->unp_conn != NULL) { 408 error = EISCONN; 409 break; 410 } 411 error = unp_connect(so, nam, td); 412 if (error) 413 break; 414 } else { 415 if (unp->unp_conn == NULL) { 416 error = ENOTCONN; 417 break; 418 } 419 } 420 so2 = unp->unp_conn->unp_socket; 421 if (unp->unp_addr != NULL) 422 from = (struct sockaddr *)unp->unp_addr; 423 else 424 from = &sun_noname; 425 if (unp->unp_conn->unp_flags & UNP_WANTCRED) 426 control = unp_addsockcred(td, control); 427 SOCKBUF_LOCK(&so2->so_rcv); 428 if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 429 sorwakeup_locked(so2); 430 m = NULL; 431 control = NULL; 432 } else { 433 SOCKBUF_UNLOCK(&so2->so_rcv); 434 error = ENOBUFS; 435 } 436 if (nam != NULL) 437 unp_disconnect(unp); 438 break; 439 } 440 441 case SOCK_STREAM: 442 /* Connect if not connected yet. */ 443 /* 444 * Note: A better implementation would complain 445 * if not equal to the peer's address. 446 */ 447 if ((so->so_state & SS_ISCONNECTED) == 0) { 448 if (nam != NULL) { 449 error = unp_connect(so, nam, td); 450 if (error) 451 break; /* XXX */ 452 } else { 453 error = ENOTCONN; 454 break; 455 } 456 } 457 458 SOCKBUF_LOCK(&so->so_snd); 459 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 460 SOCKBUF_UNLOCK(&so->so_snd); 461 error = EPIPE; 462 break; 463 } 464 if (unp->unp_conn == NULL) 465 panic("uipc_send connected but no connection?"); 466 so2 = unp->unp_conn->unp_socket; 467 SOCKBUF_LOCK(&so2->so_rcv); 468 if (unp->unp_conn->unp_flags & UNP_WANTCRED) { 469 /* 470 * Credentials are passed only once on 471 * SOCK_STREAM. 472 */ 473 unp->unp_conn->unp_flags &= ~UNP_WANTCRED; 474 control = unp_addsockcred(td, control); 475 } 476 /* 477 * Send to paired receive port, and then reduce 478 * send buffer hiwater marks to maintain backpressure. 479 * Wake up readers. 480 */ 481 if (control != NULL) { 482 if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 483 control = NULL; 484 } else { 485 sbappend_locked(&so2->so_rcv, m); 486 } 487 so->so_snd.sb_mbmax -= 488 so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 489 unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 490 newhiwat = so->so_snd.sb_hiwat - 491 (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 492 (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 493 newhiwat, RLIM_INFINITY); 494 SOCKBUF_UNLOCK(&so->so_snd); 495 unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 496 sorwakeup_locked(so2); 497 m = NULL; 498 break; 499 500 default: 501 panic("uipc_send unknown socktype"); 502 } 503 504 /* 505 * SEND_EOF is equivalent to a SEND followed by 506 * a SHUTDOWN. 507 */ 508 if (flags & PRUS_EOF) { 509 socantsendmore(so); 510 unp_shutdown(unp); 511 } 512 UNP_UNLOCK(); 513 514 dispose_release: 515 if (control != NULL && error != 0) 516 unp_dispose(control); 517 518 release: 519 if (control != NULL) 520 m_freem(control); 521 if (m != NULL) 522 m_freem(m); 523 return (error); 524 } 525 526 static int 527 uipc_sense(struct socket *so, struct stat *sb) 528 { 529 struct unpcb *unp; 530 struct socket *so2; 531 532 UNP_LOCK(); 533 unp = sotounpcb(so); 534 if (unp == NULL) { 535 UNP_UNLOCK(); 536 return (EINVAL); 537 } 538 sb->st_blksize = so->so_snd.sb_hiwat; 539 if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 540 so2 = unp->unp_conn->unp_socket; 541 sb->st_blksize += so2->so_rcv.sb_cc; 542 } 543 sb->st_dev = NODEV; 544 if (unp->unp_ino == 0) 545 unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 546 sb->st_ino = unp->unp_ino; 547 UNP_UNLOCK(); 548 return (0); 549 } 550 551 static int 552 uipc_shutdown(struct socket *so) 553 { 554 struct unpcb *unp; 555 556 UNP_LOCK(); 557 unp = sotounpcb(so); 558 if (unp == NULL) { 559 UNP_UNLOCK(); 560 return (EINVAL); 561 } 562 socantsendmore(so); 563 unp_shutdown(unp); 564 UNP_UNLOCK(); 565 return (0); 566 } 567 568 static int 569 uipc_sockaddr(struct socket *so, struct sockaddr **nam) 570 { 571 struct unpcb *unp; 572 const struct sockaddr *sa; 573 574 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 575 UNP_LOCK(); 576 unp = sotounpcb(so); 577 if (unp == NULL) { 578 UNP_UNLOCK(); 579 free(*nam, M_SONAME); 580 *nam = NULL; 581 return (EINVAL); 582 } 583 if (unp->unp_addr != NULL) 584 sa = (struct sockaddr *) unp->unp_addr; 585 else 586 sa = &sun_noname; 587 bcopy(sa, *nam, sa->sa_len); 588 UNP_UNLOCK(); 589 return (0); 590 } 591 592 struct pr_usrreqs uipc_usrreqs = { 593 .pru_abort = uipc_abort, 594 .pru_accept = uipc_accept, 595 .pru_attach = uipc_attach, 596 .pru_bind = uipc_bind, 597 .pru_connect = uipc_connect, 598 .pru_connect2 = uipc_connect2, 599 .pru_detach = uipc_detach, 600 .pru_disconnect = uipc_disconnect, 601 .pru_listen = uipc_listen, 602 .pru_peeraddr = uipc_peeraddr, 603 .pru_rcvd = uipc_rcvd, 604 .pru_send = uipc_send, 605 .pru_sense = uipc_sense, 606 .pru_shutdown = uipc_shutdown, 607 .pru_sockaddr = uipc_sockaddr, 608 .pru_sosend = sosend, 609 .pru_soreceive = soreceive, 610 .pru_sopoll = sopoll, 611 }; 612 613 int 614 uipc_ctloutput(struct socket *so, struct sockopt *sopt) 615 { 616 struct unpcb *unp; 617 struct xucred xu; 618 int error, optval; 619 620 UNP_LOCK(); 621 unp = sotounpcb(so); 622 if (unp == NULL) { 623 UNP_UNLOCK(); 624 return (EINVAL); 625 } 626 error = 0; 627 628 switch (sopt->sopt_dir) { 629 case SOPT_GET: 630 switch (sopt->sopt_name) { 631 case LOCAL_PEERCRED: 632 if (unp->unp_flags & UNP_HAVEPC) 633 xu = unp->unp_peercred; 634 else { 635 if (so->so_type == SOCK_STREAM) 636 error = ENOTCONN; 637 else 638 error = EINVAL; 639 } 640 if (error == 0) 641 error = sooptcopyout(sopt, &xu, sizeof(xu)); 642 break; 643 case LOCAL_CREDS: 644 optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 645 error = sooptcopyout(sopt, &optval, sizeof(optval)); 646 break; 647 case LOCAL_CONNWAIT: 648 optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 649 error = sooptcopyout(sopt, &optval, sizeof(optval)); 650 break; 651 default: 652 error = EOPNOTSUPP; 653 break; 654 } 655 break; 656 case SOPT_SET: 657 switch (sopt->sopt_name) { 658 case LOCAL_CREDS: 659 case LOCAL_CONNWAIT: 660 error = sooptcopyin(sopt, &optval, sizeof(optval), 661 sizeof(optval)); 662 if (error) 663 break; 664 665 #define OPTSET(bit) \ 666 if (optval) \ 667 unp->unp_flags |= bit; \ 668 else \ 669 unp->unp_flags &= ~bit; 670 671 switch (sopt->sopt_name) { 672 case LOCAL_CREDS: 673 OPTSET(UNP_WANTCRED); 674 break; 675 case LOCAL_CONNWAIT: 676 OPTSET(UNP_CONNWAIT); 677 break; 678 default: 679 break; 680 } 681 break; 682 #undef OPTSET 683 default: 684 error = ENOPROTOOPT; 685 break; 686 } 687 default: 688 error = EOPNOTSUPP; 689 break; 690 } 691 UNP_UNLOCK(); 692 return (error); 693 } 694 695 /* 696 * Both send and receive buffers are allocated PIPSIZ bytes of buffering 697 * for stream sockets, although the total for sender and receiver is 698 * actually only PIPSIZ. 699 * Datagram sockets really use the sendspace as the maximum datagram size, 700 * and don't really want to reserve the sendspace. Their recvspace should 701 * be large enough for at least one max-size datagram plus address. 702 */ 703 #ifndef PIPSIZ 704 #define PIPSIZ 8192 705 #endif 706 static u_long unpst_sendspace = PIPSIZ; 707 static u_long unpst_recvspace = PIPSIZ; 708 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 709 static u_long unpdg_recvspace = 4*1024; 710 711 static int unp_rights; /* file descriptors in flight */ 712 713 SYSCTL_DECL(_net_local_stream); 714 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 715 &unpst_sendspace, 0, ""); 716 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 717 &unpst_recvspace, 0, ""); 718 SYSCTL_DECL(_net_local_dgram); 719 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 720 &unpdg_sendspace, 0, ""); 721 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 722 &unpdg_recvspace, 0, ""); 723 SYSCTL_DECL(_net_local); 724 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 725 726 static int 727 unp_attach(struct socket *so) 728 { 729 struct unpcb *unp; 730 int error; 731 732 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 733 switch (so->so_type) { 734 735 case SOCK_STREAM: 736 error = soreserve(so, unpst_sendspace, unpst_recvspace); 737 break; 738 739 case SOCK_DGRAM: 740 error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 741 break; 742 743 default: 744 panic("unp_attach"); 745 } 746 if (error) 747 return (error); 748 } 749 unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO); 750 if (unp == NULL) 751 return (ENOBUFS); 752 LIST_INIT(&unp->unp_refs); 753 unp->unp_socket = so; 754 so->so_pcb = unp; 755 756 UNP_LOCK(); 757 unp->unp_gencnt = ++unp_gencnt; 758 unp_count++; 759 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 760 : &unp_shead, unp, unp_link); 761 UNP_UNLOCK(); 762 763 return (0); 764 } 765 766 static void 767 unp_detach(struct unpcb *unp) 768 { 769 struct vnode *vp; 770 771 UNP_LOCK_ASSERT(); 772 773 LIST_REMOVE(unp, unp_link); 774 unp->unp_gencnt = ++unp_gencnt; 775 --unp_count; 776 if ((vp = unp->unp_vnode) != NULL) { 777 /* 778 * XXXRW: should v_socket be frobbed only while holding 779 * Giant? 780 */ 781 unp->unp_vnode->v_socket = NULL; 782 unp->unp_vnode = NULL; 783 } 784 if (unp->unp_conn != NULL) 785 unp_disconnect(unp); 786 while (!LIST_EMPTY(&unp->unp_refs)) { 787 struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 788 unp_drop(ref, ECONNRESET); 789 } 790 soisdisconnected(unp->unp_socket); 791 unp->unp_socket->so_pcb = NULL; 792 if (unp_rights) { 793 /* 794 * Normally the receive buffer is flushed later, 795 * in sofree, but if our receive buffer holds references 796 * to descriptors that are now garbage, we will dispose 797 * of those descriptor references after the garbage collector 798 * gets them (resulting in a "panic: closef: count < 0"). 799 */ 800 sorflush(unp->unp_socket); 801 unp_gc(); /* Will unlock UNP. */ 802 } else 803 UNP_UNLOCK(); 804 UNP_UNLOCK_ASSERT(); 805 if (unp->unp_addr != NULL) 806 FREE(unp->unp_addr, M_SONAME); 807 uma_zfree(unp_zone, unp); 808 if (vp) { 809 mtx_lock(&Giant); 810 vrele(vp); 811 mtx_unlock(&Giant); 812 } 813 } 814 815 static int 816 unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td) 817 { 818 struct sockaddr_un *soun = (struct sockaddr_un *)nam; 819 struct vnode *vp; 820 struct mount *mp; 821 struct vattr vattr; 822 int error, namelen; 823 struct nameidata nd; 824 char *buf; 825 826 UNP_LOCK_ASSERT(); 827 828 /* 829 * XXXRW: This test-and-set of unp_vnode is non-atomic; the 830 * unlocked read here is fine, but the value of unp_vnode needs 831 * to be tested again after we do all the lookups to see if the 832 * pcb is still unbound? 833 */ 834 if (unp->unp_vnode != NULL) 835 return (EINVAL); 836 837 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 838 if (namelen <= 0) 839 return (EINVAL); 840 841 UNP_UNLOCK(); 842 843 buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 844 strlcpy(buf, soun->sun_path, namelen + 1); 845 846 mtx_lock(&Giant); 847 restart: 848 mtx_assert(&Giant, MA_OWNED); 849 NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 850 buf, td); 851 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 852 error = namei(&nd); 853 if (error) 854 goto done; 855 vp = nd.ni_vp; 856 if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 857 NDFREE(&nd, NDF_ONLY_PNBUF); 858 if (nd.ni_dvp == vp) 859 vrele(nd.ni_dvp); 860 else 861 vput(nd.ni_dvp); 862 if (vp != NULL) { 863 vrele(vp); 864 error = EADDRINUSE; 865 goto done; 866 } 867 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 868 if (error) 869 goto done; 870 goto restart; 871 } 872 VATTR_NULL(&vattr); 873 vattr.va_type = VSOCK; 874 vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 875 #ifdef MAC 876 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 877 &vattr); 878 #endif 879 if (error == 0) { 880 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 881 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 882 } 883 NDFREE(&nd, NDF_ONLY_PNBUF); 884 vput(nd.ni_dvp); 885 if (error) { 886 vn_finished_write(mp); 887 goto done; 888 } 889 vp = nd.ni_vp; 890 ASSERT_VOP_LOCKED(vp, "unp_bind"); 891 soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 892 UNP_LOCK(); 893 vp->v_socket = unp->unp_socket; 894 unp->unp_vnode = vp; 895 unp->unp_addr = soun; 896 UNP_UNLOCK(); 897 VOP_UNLOCK(vp, 0, td); 898 vn_finished_write(mp); 899 done: 900 mtx_unlock(&Giant); 901 free(buf, M_TEMP); 902 UNP_LOCK(); 903 return (error); 904 } 905 906 static int 907 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 908 { 909 struct sockaddr_un *soun = (struct sockaddr_un *)nam; 910 struct vnode *vp; 911 struct socket *so2, *so3; 912 struct unpcb *unp, *unp2, *unp3; 913 int error, len; 914 struct nameidata nd; 915 char buf[SOCK_MAXADDRLEN]; 916 struct sockaddr *sa; 917 918 UNP_LOCK_ASSERT(); 919 unp = sotounpcb(so); 920 921 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 922 if (len <= 0) 923 return (EINVAL); 924 strlcpy(buf, soun->sun_path, len + 1); 925 UNP_UNLOCK(); 926 sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 927 mtx_lock(&Giant); 928 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 929 error = namei(&nd); 930 if (error) 931 vp = NULL; 932 else 933 vp = nd.ni_vp; 934 ASSERT_VOP_LOCKED(vp, "unp_connect"); 935 NDFREE(&nd, NDF_ONLY_PNBUF); 936 if (error) 937 goto bad; 938 939 if (vp->v_type != VSOCK) { 940 error = ENOTSOCK; 941 goto bad; 942 } 943 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 944 if (error) 945 goto bad; 946 mtx_unlock(&Giant); 947 UNP_LOCK(); 948 unp = sotounpcb(so); 949 if (unp == NULL) { 950 error = EINVAL; 951 goto bad2; 952 } 953 so2 = vp->v_socket; 954 if (so2 == NULL) { 955 error = ECONNREFUSED; 956 goto bad2; 957 } 958 if (so->so_type != so2->so_type) { 959 error = EPROTOTYPE; 960 goto bad2; 961 } 962 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 963 if (so2->so_options & SO_ACCEPTCONN) { 964 /* 965 * NB: drop locks here so unp_attach is entered 966 * w/o locks; this avoids a recursive lock 967 * of the head and holding sleep locks across 968 * a (potentially) blocking malloc. 969 */ 970 UNP_UNLOCK(); 971 so3 = sonewconn(so2, 0); 972 UNP_LOCK(); 973 } else 974 so3 = NULL; 975 if (so3 == NULL) { 976 error = ECONNREFUSED; 977 goto bad2; 978 } 979 unp = sotounpcb(so); 980 unp2 = sotounpcb(so2); 981 unp3 = sotounpcb(so3); 982 if (unp2->unp_addr != NULL) { 983 bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 984 unp3->unp_addr = (struct sockaddr_un *) sa; 985 sa = NULL; 986 } 987 /* 988 * unp_peercred management: 989 * 990 * The connecter's (client's) credentials are copied 991 * from its process structure at the time of connect() 992 * (which is now). 993 */ 994 cru2x(td->td_ucred, &unp3->unp_peercred); 995 unp3->unp_flags |= UNP_HAVEPC; 996 /* 997 * The receiver's (server's) credentials are copied 998 * from the unp_peercred member of socket on which the 999 * former called listen(); unp_listen() cached that 1000 * process's credentials at that time so we can use 1001 * them now. 1002 */ 1003 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 1004 ("unp_connect: listener without cached peercred")); 1005 memcpy(&unp->unp_peercred, &unp2->unp_peercred, 1006 sizeof(unp->unp_peercred)); 1007 unp->unp_flags |= UNP_HAVEPC; 1008 #ifdef MAC 1009 SOCK_LOCK(so); 1010 mac_set_socket_peer_from_socket(so, so3); 1011 mac_set_socket_peer_from_socket(so3, so); 1012 SOCK_UNLOCK(so); 1013 #endif 1014 1015 so2 = so3; 1016 } 1017 error = unp_connect2(so, so2, PRU_CONNECT); 1018 bad2: 1019 UNP_UNLOCK(); 1020 mtx_lock(&Giant); 1021 bad: 1022 mtx_assert(&Giant, MA_OWNED); 1023 if (vp != NULL) 1024 vput(vp); 1025 mtx_unlock(&Giant); 1026 free(sa, M_SONAME); 1027 UNP_LOCK(); 1028 return (error); 1029 } 1030 1031 static int 1032 unp_connect2(struct socket *so, struct socket *so2, int req) 1033 { 1034 struct unpcb *unp = sotounpcb(so); 1035 struct unpcb *unp2; 1036 1037 UNP_LOCK_ASSERT(); 1038 1039 if (so2->so_type != so->so_type) 1040 return (EPROTOTYPE); 1041 unp2 = sotounpcb(so2); 1042 unp->unp_conn = unp2; 1043 switch (so->so_type) { 1044 1045 case SOCK_DGRAM: 1046 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 1047 soisconnected(so); 1048 break; 1049 1050 case SOCK_STREAM: 1051 unp2->unp_conn = unp; 1052 if (req == PRU_CONNECT && 1053 ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 1054 soisconnecting(so); 1055 else 1056 soisconnected(so); 1057 soisconnected(so2); 1058 break; 1059 1060 default: 1061 panic("unp_connect2"); 1062 } 1063 return (0); 1064 } 1065 1066 static void 1067 unp_disconnect(struct unpcb *unp) 1068 { 1069 struct unpcb *unp2 = unp->unp_conn; 1070 struct socket *so; 1071 1072 UNP_LOCK_ASSERT(); 1073 1074 if (unp2 == NULL) 1075 return; 1076 unp->unp_conn = NULL; 1077 switch (unp->unp_socket->so_type) { 1078 1079 case SOCK_DGRAM: 1080 LIST_REMOVE(unp, unp_reflink); 1081 so = unp->unp_socket; 1082 SOCK_LOCK(so); 1083 so->so_state &= ~SS_ISCONNECTED; 1084 SOCK_UNLOCK(so); 1085 break; 1086 1087 case SOCK_STREAM: 1088 soisdisconnected(unp->unp_socket); 1089 unp2->unp_conn = NULL; 1090 soisdisconnected(unp2->unp_socket); 1091 break; 1092 } 1093 } 1094 1095 #ifdef notdef 1096 void 1097 unp_abort(struct unpcb *unp) 1098 { 1099 1100 unp_detach(unp); 1101 UNP_UNLOCK_ASSERT(); 1102 } 1103 #endif 1104 1105 /* 1106 * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 1107 * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 1108 * are safe to reference. It first scans the list of struct unpcb's to 1109 * generate a pointer list, then it rescans its list one entry at a time to 1110 * externalize and copyout. It checks the generation number to see if a 1111 * struct unpcb has been reused, and will skip it if so. 1112 */ 1113 static int 1114 unp_pcblist(SYSCTL_HANDLER_ARGS) 1115 { 1116 int error, i, n; 1117 struct unpcb *unp, **unp_list; 1118 unp_gen_t gencnt; 1119 struct xunpgen *xug; 1120 struct unp_head *head; 1121 struct xunpcb *xu; 1122 1123 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 1124 1125 /* 1126 * The process of preparing the PCB list is too time-consuming and 1127 * resource-intensive to repeat twice on every request. 1128 */ 1129 if (req->oldptr == NULL) { 1130 n = unp_count; 1131 req->oldidx = 2 * (sizeof *xug) 1132 + (n + n/8) * sizeof(struct xunpcb); 1133 return (0); 1134 } 1135 1136 if (req->newptr != NULL) 1137 return (EPERM); 1138 1139 /* 1140 * OK, now we're committed to doing something. 1141 */ 1142 xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 1143 UNP_LOCK(); 1144 gencnt = unp_gencnt; 1145 n = unp_count; 1146 UNP_UNLOCK(); 1147 1148 xug->xug_len = sizeof *xug; 1149 xug->xug_count = n; 1150 xug->xug_gen = gencnt; 1151 xug->xug_sogen = so_gencnt; 1152 error = SYSCTL_OUT(req, xug, sizeof *xug); 1153 if (error) { 1154 free(xug, M_TEMP); 1155 return (error); 1156 } 1157 1158 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 1159 1160 UNP_LOCK(); 1161 for (unp = LIST_FIRST(head), i = 0; unp && i < n; 1162 unp = LIST_NEXT(unp, unp_link)) { 1163 if (unp->unp_gencnt <= gencnt) { 1164 if (cr_cansee(req->td->td_ucred, 1165 unp->unp_socket->so_cred)) 1166 continue; 1167 unp_list[i++] = unp; 1168 } 1169 } 1170 UNP_UNLOCK(); 1171 n = i; /* in case we lost some during malloc */ 1172 1173 error = 0; 1174 xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 1175 for (i = 0; i < n; i++) { 1176 unp = unp_list[i]; 1177 if (unp->unp_gencnt <= gencnt) { 1178 xu->xu_len = sizeof *xu; 1179 xu->xu_unpp = unp; 1180 /* 1181 * XXX - need more locking here to protect against 1182 * connect/disconnect races for SMP. 1183 */ 1184 if (unp->unp_addr != NULL) 1185 bcopy(unp->unp_addr, &xu->xu_addr, 1186 unp->unp_addr->sun_len); 1187 if (unp->unp_conn != NULL && 1188 unp->unp_conn->unp_addr != NULL) 1189 bcopy(unp->unp_conn->unp_addr, 1190 &xu->xu_caddr, 1191 unp->unp_conn->unp_addr->sun_len); 1192 bcopy(unp, &xu->xu_unp, sizeof *unp); 1193 sotoxsocket(unp->unp_socket, &xu->xu_socket); 1194 error = SYSCTL_OUT(req, xu, sizeof *xu); 1195 } 1196 } 1197 free(xu, M_TEMP); 1198 if (!error) { 1199 /* 1200 * Give the user an updated idea of our state. 1201 * If the generation differs from what we told 1202 * her before, she knows that something happened 1203 * while we were processing this request, and it 1204 * might be necessary to retry. 1205 */ 1206 xug->xug_gen = unp_gencnt; 1207 xug->xug_sogen = so_gencnt; 1208 xug->xug_count = unp_count; 1209 error = SYSCTL_OUT(req, xug, sizeof *xug); 1210 } 1211 free(unp_list, M_TEMP); 1212 free(xug, M_TEMP); 1213 return (error); 1214 } 1215 1216 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 1217 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 1218 "List of active local datagram sockets"); 1219 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 1220 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 1221 "List of active local stream sockets"); 1222 1223 static void 1224 unp_shutdown(struct unpcb *unp) 1225 { 1226 struct socket *so; 1227 1228 UNP_LOCK_ASSERT(); 1229 1230 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1231 (so = unp->unp_conn->unp_socket)) 1232 socantrcvmore(so); 1233 } 1234 1235 static void 1236 unp_drop(struct unpcb *unp, int errno) 1237 { 1238 struct socket *so = unp->unp_socket; 1239 1240 UNP_LOCK_ASSERT(); 1241 1242 so->so_error = errno; 1243 unp_disconnect(unp); 1244 } 1245 1246 #ifdef notdef 1247 void 1248 unp_drain(void) 1249 { 1250 1251 } 1252 #endif 1253 1254 static void 1255 unp_freerights(struct file **rp, int fdcount) 1256 { 1257 int i; 1258 struct file *fp; 1259 1260 for (i = 0; i < fdcount; i++) { 1261 fp = *rp; 1262 /* 1263 * zero the pointer before calling 1264 * unp_discard since it may end up 1265 * in unp_gc().. 1266 */ 1267 *rp++ = 0; 1268 unp_discard(fp); 1269 } 1270 } 1271 1272 int 1273 unp_externalize(struct mbuf *control, struct mbuf **controlp) 1274 { 1275 struct thread *td = curthread; /* XXX */ 1276 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1277 int i; 1278 int *fdp; 1279 struct file **rp; 1280 struct file *fp; 1281 void *data; 1282 socklen_t clen = control->m_len, datalen; 1283 int error, newfds; 1284 int f; 1285 u_int newlen; 1286 1287 UNP_UNLOCK_ASSERT(); 1288 1289 error = 0; 1290 if (controlp != NULL) /* controlp == NULL => free control messages */ 1291 *controlp = NULL; 1292 1293 while (cm != NULL) { 1294 if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 1295 error = EINVAL; 1296 break; 1297 } 1298 1299 data = CMSG_DATA(cm); 1300 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1301 1302 if (cm->cmsg_level == SOL_SOCKET 1303 && cm->cmsg_type == SCM_RIGHTS) { 1304 newfds = datalen / sizeof(struct file *); 1305 rp = data; 1306 1307 /* If we're not outputting the descriptors free them. */ 1308 if (error || controlp == NULL) { 1309 unp_freerights(rp, newfds); 1310 goto next; 1311 } 1312 FILEDESC_LOCK(td->td_proc->p_fd); 1313 /* if the new FD's will not fit free them. */ 1314 if (!fdavail(td, newfds)) { 1315 FILEDESC_UNLOCK(td->td_proc->p_fd); 1316 error = EMSGSIZE; 1317 unp_freerights(rp, newfds); 1318 goto next; 1319 } 1320 /* 1321 * now change each pointer to an fd in the global 1322 * table to an integer that is the index to the 1323 * local fd table entry that we set up to point 1324 * to the global one we are transferring. 1325 */ 1326 newlen = newfds * sizeof(int); 1327 *controlp = sbcreatecontrol(NULL, newlen, 1328 SCM_RIGHTS, SOL_SOCKET); 1329 if (*controlp == NULL) { 1330 FILEDESC_UNLOCK(td->td_proc->p_fd); 1331 error = E2BIG; 1332 unp_freerights(rp, newfds); 1333 goto next; 1334 } 1335 1336 fdp = (int *) 1337 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1338 for (i = 0; i < newfds; i++) { 1339 if (fdalloc(td, 0, &f)) 1340 panic("unp_externalize fdalloc failed"); 1341 fp = *rp++; 1342 td->td_proc->p_fd->fd_ofiles[f] = fp; 1343 FILE_LOCK(fp); 1344 fp->f_msgcount--; 1345 FILE_UNLOCK(fp); 1346 unp_rights--; 1347 *fdp++ = f; 1348 } 1349 FILEDESC_UNLOCK(td->td_proc->p_fd); 1350 } else { /* We can just copy anything else across */ 1351 if (error || controlp == NULL) 1352 goto next; 1353 *controlp = sbcreatecontrol(NULL, datalen, 1354 cm->cmsg_type, cm->cmsg_level); 1355 if (*controlp == NULL) { 1356 error = ENOBUFS; 1357 goto next; 1358 } 1359 bcopy(data, 1360 CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 1361 datalen); 1362 } 1363 1364 controlp = &(*controlp)->m_next; 1365 1366 next: 1367 if (CMSG_SPACE(datalen) < clen) { 1368 clen -= CMSG_SPACE(datalen); 1369 cm = (struct cmsghdr *) 1370 ((caddr_t)cm + CMSG_SPACE(datalen)); 1371 } else { 1372 clen = 0; 1373 cm = NULL; 1374 } 1375 } 1376 1377 m_freem(control); 1378 1379 return (error); 1380 } 1381 1382 void 1383 unp_init(void) 1384 { 1385 unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 1386 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1387 if (unp_zone == NULL) 1388 panic("unp_init"); 1389 uma_zone_set_max(unp_zone, nmbclusters); 1390 LIST_INIT(&unp_dhead); 1391 LIST_INIT(&unp_shead); 1392 1393 UNP_LOCK_INIT(); 1394 } 1395 1396 static int 1397 unp_internalize(struct mbuf **controlp, struct thread *td) 1398 { 1399 struct mbuf *control = *controlp; 1400 struct proc *p = td->td_proc; 1401 struct filedesc *fdescp = p->p_fd; 1402 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1403 struct cmsgcred *cmcred; 1404 struct file **rp; 1405 struct file *fp; 1406 struct timeval *tv; 1407 int i, fd, *fdp; 1408 void *data; 1409 socklen_t clen = control->m_len, datalen; 1410 int error, oldfds; 1411 u_int newlen; 1412 1413 UNP_UNLOCK_ASSERT(); 1414 1415 error = 0; 1416 *controlp = NULL; 1417 1418 while (cm != NULL) { 1419 if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 1420 || cm->cmsg_len > clen) { 1421 error = EINVAL; 1422 goto out; 1423 } 1424 1425 data = CMSG_DATA(cm); 1426 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1427 1428 switch (cm->cmsg_type) { 1429 /* 1430 * Fill in credential information. 1431 */ 1432 case SCM_CREDS: 1433 *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 1434 SCM_CREDS, SOL_SOCKET); 1435 if (*controlp == NULL) { 1436 error = ENOBUFS; 1437 goto out; 1438 } 1439 1440 cmcred = (struct cmsgcred *) 1441 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1442 cmcred->cmcred_pid = p->p_pid; 1443 cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1444 cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1445 cmcred->cmcred_euid = td->td_ucred->cr_uid; 1446 cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 1447 CMGROUP_MAX); 1448 for (i = 0; i < cmcred->cmcred_ngroups; i++) 1449 cmcred->cmcred_groups[i] = 1450 td->td_ucred->cr_groups[i]; 1451 break; 1452 1453 case SCM_RIGHTS: 1454 oldfds = datalen / sizeof (int); 1455 /* 1456 * check that all the FDs passed in refer to legal files 1457 * If not, reject the entire operation. 1458 */ 1459 fdp = data; 1460 FILEDESC_LOCK(fdescp); 1461 for (i = 0; i < oldfds; i++) { 1462 fd = *fdp++; 1463 if ((unsigned)fd >= fdescp->fd_nfiles || 1464 fdescp->fd_ofiles[fd] == NULL) { 1465 FILEDESC_UNLOCK(fdescp); 1466 error = EBADF; 1467 goto out; 1468 } 1469 fp = fdescp->fd_ofiles[fd]; 1470 if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1471 FILEDESC_UNLOCK(fdescp); 1472 error = EOPNOTSUPP; 1473 goto out; 1474 } 1475 1476 } 1477 /* 1478 * Now replace the integer FDs with pointers to 1479 * the associated global file table entry.. 1480 */ 1481 newlen = oldfds * sizeof(struct file *); 1482 *controlp = sbcreatecontrol(NULL, newlen, 1483 SCM_RIGHTS, SOL_SOCKET); 1484 if (*controlp == NULL) { 1485 FILEDESC_UNLOCK(fdescp); 1486 error = E2BIG; 1487 goto out; 1488 } 1489 1490 fdp = data; 1491 rp = (struct file **) 1492 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1493 for (i = 0; i < oldfds; i++) { 1494 fp = fdescp->fd_ofiles[*fdp++]; 1495 *rp++ = fp; 1496 FILE_LOCK(fp); 1497 fp->f_count++; 1498 fp->f_msgcount++; 1499 FILE_UNLOCK(fp); 1500 unp_rights++; 1501 } 1502 FILEDESC_UNLOCK(fdescp); 1503 break; 1504 1505 case SCM_TIMESTAMP: 1506 *controlp = sbcreatecontrol(NULL, sizeof(*tv), 1507 SCM_TIMESTAMP, SOL_SOCKET); 1508 if (*controlp == NULL) { 1509 error = ENOBUFS; 1510 goto out; 1511 } 1512 tv = (struct timeval *) 1513 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1514 microtime(tv); 1515 break; 1516 1517 default: 1518 error = EINVAL; 1519 goto out; 1520 } 1521 1522 controlp = &(*controlp)->m_next; 1523 1524 if (CMSG_SPACE(datalen) < clen) { 1525 clen -= CMSG_SPACE(datalen); 1526 cm = (struct cmsghdr *) 1527 ((caddr_t)cm + CMSG_SPACE(datalen)); 1528 } else { 1529 clen = 0; 1530 cm = NULL; 1531 } 1532 } 1533 1534 out: 1535 m_freem(control); 1536 1537 return (error); 1538 } 1539 1540 struct mbuf * 1541 unp_addsockcred(struct thread *td, struct mbuf *control) 1542 { 1543 struct mbuf *m, *n; 1544 struct sockcred *sc; 1545 int ngroups; 1546 int i; 1547 1548 ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 1549 1550 m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 1551 if (m == NULL) 1552 return (control); 1553 m->m_next = NULL; 1554 1555 sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 1556 sc->sc_uid = td->td_ucred->cr_ruid; 1557 sc->sc_euid = td->td_ucred->cr_uid; 1558 sc->sc_gid = td->td_ucred->cr_rgid; 1559 sc->sc_egid = td->td_ucred->cr_gid; 1560 sc->sc_ngroups = ngroups; 1561 for (i = 0; i < sc->sc_ngroups; i++) 1562 sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 1563 1564 /* 1565 * If a control message already exists, append us to the end. 1566 */ 1567 if (control != NULL) { 1568 for (n = control; n->m_next != NULL; n = n->m_next) 1569 ; 1570 n->m_next = m; 1571 } else 1572 control = m; 1573 1574 return (control); 1575 } 1576 1577 /* 1578 * unp_defer is thread-local during garbage collection, and does not require 1579 * explicit synchronization. unp_gcing prevents other threads from entering 1580 * garbage collection, and perhaps should be an sx lock instead. 1581 */ 1582 static int unp_defer, unp_gcing; 1583 1584 static void 1585 unp_gc(void) 1586 { 1587 struct file *fp, *nextfp; 1588 struct socket *so; 1589 struct file **extra_ref, **fpp; 1590 int nunref, i; 1591 int nfiles_snap; 1592 int nfiles_slack = 20; 1593 1594 UNP_LOCK_ASSERT(); 1595 1596 if (unp_gcing) { 1597 UNP_UNLOCK(); 1598 return; 1599 } 1600 unp_gcing = 1; 1601 unp_defer = 0; 1602 UNP_UNLOCK(); 1603 /* 1604 * before going through all this, set all FDs to 1605 * be NOT defered and NOT externally accessible 1606 */ 1607 sx_slock(&filelist_lock); 1608 LIST_FOREACH(fp, &filehead, f_list) 1609 fp->f_gcflag &= ~(FMARK|FDEFER); 1610 do { 1611 LIST_FOREACH(fp, &filehead, f_list) { 1612 FILE_LOCK(fp); 1613 /* 1614 * If the file is not open, skip it 1615 */ 1616 if (fp->f_count == 0) { 1617 FILE_UNLOCK(fp); 1618 continue; 1619 } 1620 /* 1621 * If we already marked it as 'defer' in a 1622 * previous pass, then try process it this time 1623 * and un-mark it 1624 */ 1625 if (fp->f_gcflag & FDEFER) { 1626 fp->f_gcflag &= ~FDEFER; 1627 unp_defer--; 1628 } else { 1629 /* 1630 * if it's not defered, then check if it's 1631 * already marked.. if so skip it 1632 */ 1633 if (fp->f_gcflag & FMARK) { 1634 FILE_UNLOCK(fp); 1635 continue; 1636 } 1637 /* 1638 * If all references are from messages 1639 * in transit, then skip it. it's not 1640 * externally accessible. 1641 */ 1642 if (fp->f_count == fp->f_msgcount) { 1643 FILE_UNLOCK(fp); 1644 continue; 1645 } 1646 /* 1647 * If it got this far then it must be 1648 * externally accessible. 1649 */ 1650 fp->f_gcflag |= FMARK; 1651 } 1652 /* 1653 * either it was defered, or it is externally 1654 * accessible and not already marked so. 1655 * Now check if it is possibly one of OUR sockets. 1656 */ 1657 if (fp->f_type != DTYPE_SOCKET || 1658 (so = fp->f_data) == NULL) { 1659 FILE_UNLOCK(fp); 1660 continue; 1661 } 1662 FILE_UNLOCK(fp); 1663 if (so->so_proto->pr_domain != &localdomain || 1664 (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1665 continue; 1666 #ifdef notdef 1667 if (so->so_rcv.sb_flags & SB_LOCK) { 1668 /* 1669 * This is problematical; it's not clear 1670 * we need to wait for the sockbuf to be 1671 * unlocked (on a uniprocessor, at least), 1672 * and it's also not clear what to do 1673 * if sbwait returns an error due to receipt 1674 * of a signal. If sbwait does return 1675 * an error, we'll go into an infinite 1676 * loop. Delete all of this for now. 1677 */ 1678 (void) sbwait(&so->so_rcv); 1679 goto restart; 1680 } 1681 #endif 1682 /* 1683 * So, Ok, it's one of our sockets and it IS externally 1684 * accessible (or was defered). Now we look 1685 * to see if we hold any file descriptors in its 1686 * message buffers. Follow those links and mark them 1687 * as accessible too. 1688 */ 1689 SOCKBUF_LOCK(&so->so_rcv); 1690 unp_scan(so->so_rcv.sb_mb, unp_mark); 1691 SOCKBUF_UNLOCK(&so->so_rcv); 1692 } 1693 } while (unp_defer); 1694 sx_sunlock(&filelist_lock); 1695 /* 1696 * We grab an extra reference to each of the file table entries 1697 * that are not otherwise accessible and then free the rights 1698 * that are stored in messages on them. 1699 * 1700 * The bug in the orginal code is a little tricky, so I'll describe 1701 * what's wrong with it here. 1702 * 1703 * It is incorrect to simply unp_discard each entry for f_msgcount 1704 * times -- consider the case of sockets A and B that contain 1705 * references to each other. On a last close of some other socket, 1706 * we trigger a gc since the number of outstanding rights (unp_rights) 1707 * is non-zero. If during the sweep phase the gc code un_discards, 1708 * we end up doing a (full) closef on the descriptor. A closef on A 1709 * results in the following chain. Closef calls soo_close, which 1710 * calls soclose. Soclose calls first (through the switch 1711 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1712 * returns because the previous instance had set unp_gcing, and 1713 * we return all the way back to soclose, which marks the socket 1714 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1715 * to free up the rights that are queued in messages on the socket A, 1716 * i.e., the reference on B. The sorflush calls via the dom_dispose 1717 * switch unp_dispose, which unp_scans with unp_discard. This second 1718 * instance of unp_discard just calls closef on B. 1719 * 1720 * Well, a similar chain occurs on B, resulting in a sorflush on B, 1721 * which results in another closef on A. Unfortunately, A is already 1722 * being closed, and the descriptor has already been marked with 1723 * SS_NOFDREF, and soclose panics at this point. 1724 * 1725 * Here, we first take an extra reference to each inaccessible 1726 * descriptor. Then, we call sorflush ourself, since we know 1727 * it is a Unix domain socket anyhow. After we destroy all the 1728 * rights carried in messages, we do a last closef to get rid 1729 * of our extra reference. This is the last close, and the 1730 * unp_detach etc will shut down the socket. 1731 * 1732 * 91/09/19, bsy@cs.cmu.edu 1733 */ 1734 again: 1735 nfiles_snap = openfiles + nfiles_slack; /* some slack */ 1736 extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 1737 M_WAITOK); 1738 sx_slock(&filelist_lock); 1739 if (nfiles_snap < openfiles) { 1740 sx_sunlock(&filelist_lock); 1741 free(extra_ref, M_TEMP); 1742 nfiles_slack += 20; 1743 goto again; 1744 } 1745 for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1746 fp != NULL; fp = nextfp) { 1747 nextfp = LIST_NEXT(fp, f_list); 1748 FILE_LOCK(fp); 1749 /* 1750 * If it's not open, skip it 1751 */ 1752 if (fp->f_count == 0) { 1753 FILE_UNLOCK(fp); 1754 continue; 1755 } 1756 /* 1757 * If all refs are from msgs, and it's not marked accessible 1758 * then it must be referenced from some unreachable cycle 1759 * of (shut-down) FDs, so include it in our 1760 * list of FDs to remove 1761 */ 1762 if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1763 *fpp++ = fp; 1764 nunref++; 1765 fp->f_count++; 1766 } 1767 FILE_UNLOCK(fp); 1768 } 1769 sx_sunlock(&filelist_lock); 1770 /* 1771 * for each FD on our hit list, do the following two things 1772 */ 1773 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 1774 struct file *tfp = *fpp; 1775 FILE_LOCK(tfp); 1776 if (tfp->f_type == DTYPE_SOCKET && 1777 tfp->f_data != NULL) { 1778 FILE_UNLOCK(tfp); 1779 sorflush(tfp->f_data); 1780 } else { 1781 FILE_UNLOCK(tfp); 1782 } 1783 } 1784 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1785 closef(*fpp, (struct thread *) NULL); 1786 free(extra_ref, M_TEMP); 1787 unp_gcing = 0; 1788 1789 UNP_UNLOCK_ASSERT(); 1790 } 1791 1792 void 1793 unp_dispose(struct mbuf *m) 1794 { 1795 1796 if (m) 1797 unp_scan(m, unp_discard); 1798 } 1799 1800 static int 1801 unp_listen(struct socket *so, struct unpcb *unp, struct thread *td) 1802 { 1803 int error; 1804 1805 UNP_LOCK_ASSERT(); 1806 1807 SOCK_LOCK(so); 1808 error = solisten_proto_check(so); 1809 if (error == 0) { 1810 cru2x(td->td_ucred, &unp->unp_peercred); 1811 unp->unp_flags |= UNP_HAVEPCCACHED; 1812 solisten_proto(so); 1813 } 1814 SOCK_UNLOCK(so); 1815 return (error); 1816 } 1817 1818 static void 1819 unp_scan(struct mbuf *m0, void (*op)(struct file *)) 1820 { 1821 struct mbuf *m; 1822 struct file **rp; 1823 struct cmsghdr *cm; 1824 void *data; 1825 int i; 1826 socklen_t clen, datalen; 1827 int qfds; 1828 1829 while (m0 != NULL) { 1830 for (m = m0; m; m = m->m_next) { 1831 if (m->m_type != MT_CONTROL) 1832 continue; 1833 1834 cm = mtod(m, struct cmsghdr *); 1835 clen = m->m_len; 1836 1837 while (cm != NULL) { 1838 if (sizeof(*cm) > clen || cm->cmsg_len > clen) 1839 break; 1840 1841 data = CMSG_DATA(cm); 1842 datalen = (caddr_t)cm + cm->cmsg_len 1843 - (caddr_t)data; 1844 1845 if (cm->cmsg_level == SOL_SOCKET && 1846 cm->cmsg_type == SCM_RIGHTS) { 1847 qfds = datalen / sizeof (struct file *); 1848 rp = data; 1849 for (i = 0; i < qfds; i++) 1850 (*op)(*rp++); 1851 } 1852 1853 if (CMSG_SPACE(datalen) < clen) { 1854 clen -= CMSG_SPACE(datalen); 1855 cm = (struct cmsghdr *) 1856 ((caddr_t)cm + CMSG_SPACE(datalen)); 1857 } else { 1858 clen = 0; 1859 cm = NULL; 1860 } 1861 } 1862 } 1863 m0 = m0->m_act; 1864 } 1865 } 1866 1867 static void 1868 unp_mark(struct file *fp) 1869 { 1870 if (fp->f_gcflag & FMARK) 1871 return; 1872 unp_defer++; 1873 fp->f_gcflag |= (FMARK|FDEFER); 1874 } 1875 1876 static void 1877 unp_discard(struct file *fp) 1878 { 1879 FILE_LOCK(fp); 1880 fp->f_msgcount--; 1881 unp_rights--; 1882 FILE_UNLOCK(fp); 1883 (void) closef(fp, (struct thread *)NULL); 1884 } 1885