1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 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 * 3. 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 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 30 * $FreeBSD: src/sys/kern/uipc_usrreq.c,v 1.54.2.10 2003/03/04 17:28:09 nectar Exp $ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/domain.h> 37 #include <sys/fcntl.h> 38 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 39 #include <sys/proc.h> 40 #include <sys/file.h> 41 #include <sys/filedesc.h> 42 #include <sys/mbuf.h> 43 #include <sys/nlookup.h> 44 #include <sys/protosw.h> 45 #include <sys/socket.h> 46 #include <sys/socketvar.h> 47 #include <sys/resourcevar.h> 48 #include <sys/stat.h> 49 #include <sys/mount.h> 50 #include <sys/sysctl.h> 51 #include <sys/un.h> 52 #include <sys/unpcb.h> 53 #include <sys/vnode.h> 54 55 #include <sys/file2.h> 56 #include <sys/spinlock2.h> 57 #include <sys/socketvar2.h> 58 #include <sys/msgport2.h> 59 60 #define UNP_DETACHED UNP_PRIVATE1 61 #define UNP_CONNECTING UNP_PRIVATE2 62 #define UNP_DROPPED UNP_PRIVATE3 63 64 #define UNP_ISATTACHED(unp) \ 65 ((unp) != NULL && ((unp)->unp_flags & UNP_DETACHED) == 0) 66 67 #ifdef INVARIANTS 68 #define UNP_ASSERT_TOKEN_HELD(unp) \ 69 ASSERT_LWKT_TOKEN_HELD(lwkt_token_pool_lookup((unp))) 70 #else /* !INVARIANTS */ 71 #define UNP_ASSERT_TOKEN_HELD(unp) 72 #endif /* INVARIANTS */ 73 74 typedef struct unp_defdiscard { 75 struct unp_defdiscard *next; 76 struct file *fp; 77 } *unp_defdiscard_t; 78 79 static MALLOC_DEFINE(M_UNPCB, "unpcb", "unpcb struct"); 80 static unp_gen_t unp_gencnt; 81 static u_int unp_count; 82 83 static struct unp_head unp_shead, unp_dhead; 84 85 static struct lwkt_token unp_token = LWKT_TOKEN_INITIALIZER(unp_token); 86 static int unp_defdiscard_nest; 87 static unp_defdiscard_t unp_defdiscard_base; 88 89 /* 90 * Unix communications domain. 91 * 92 * TODO: 93 * RDM 94 * rethink name space problems 95 * need a proper out-of-band 96 * lock pushdown 97 */ 98 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 99 static ino_t unp_ino = 1; /* prototype for fake inode numbers */ 100 static struct spinlock unp_ino_spin = SPINLOCK_INITIALIZER(&unp_ino_spin, "unp_ino_spin"); 101 102 static int unp_attach (struct socket *, struct pru_attach_info *); 103 static void unp_detach (struct unpcb *); 104 static int unp_bind (struct unpcb *,struct sockaddr *, struct thread *); 105 static int unp_connect (struct socket *,struct sockaddr *, 106 struct thread *); 107 static void unp_disconnect(struct unpcb *, int); 108 static void unp_shutdown (struct unpcb *); 109 static void unp_gc (void); 110 static int unp_gc_clearmarks(struct file *, void *); 111 static int unp_gc_checkmarks(struct file *, void *); 112 static int unp_gc_checkrefs(struct file *, void *); 113 static int unp_revoke_gc_check(struct file *, void *); 114 static void unp_scan (struct mbuf *, void (*)(struct file *, void *), 115 void *data); 116 static void unp_mark (struct file *, void *data); 117 static void unp_discard (struct file *, void *); 118 static int unp_internalize (struct mbuf *, struct thread *); 119 static int unp_listen (struct unpcb *, struct thread *); 120 static void unp_fp_externalize(struct lwp *lp, struct file *fp, int fd); 121 static int unp_find_lockref(struct sockaddr *nam, struct thread *td, 122 short type, struct unpcb **unp_ret); 123 static int unp_connect_pair(struct unpcb *unp, struct unpcb *unp2); 124 static void unp_drop(struct unpcb *unp, int error); 125 126 /* 127 * SMP Considerations: 128 * 129 * Since unp_token will be automaticly released upon execution of 130 * blocking code, we need to reference unp_conn before any possible 131 * blocking code to prevent it from being ripped behind our back. 132 * 133 * Any adjustment to unp->unp_conn requires both the global unp_token 134 * AND the per-unp token (lwkt_token_pool_lookup(unp)) to be held. 135 * 136 * Any access to so_pcb to obtain unp requires the pool token for 137 * unp to be held. 138 */ 139 140 static __inline void 141 unp_reference(struct unpcb *unp) 142 { 143 /* 0->1 transition will not work */ 144 KKASSERT(unp->unp_refcnt > 0); 145 atomic_add_int(&unp->unp_refcnt, 1); 146 } 147 148 static __inline void 149 unp_free(struct unpcb *unp) 150 { 151 KKASSERT(unp->unp_refcnt > 0); 152 if (atomic_fetchadd_int(&unp->unp_refcnt, -1) == 1) 153 unp_detach(unp); 154 } 155 156 static __inline struct unpcb * 157 unp_getsocktoken(struct socket *so) 158 { 159 struct unpcb *unp; 160 161 /* 162 * The unp pointer is invalid until we verify that it is 163 * good by re-checking so_pcb AFTER obtaining the token. 164 */ 165 while ((unp = so->so_pcb) != NULL) { 166 lwkt_getpooltoken(unp); 167 if (unp == so->so_pcb) 168 break; 169 lwkt_relpooltoken(unp); 170 } 171 return unp; 172 } 173 174 static __inline void 175 unp_reltoken(struct unpcb *unp) 176 { 177 if (unp != NULL) 178 lwkt_relpooltoken(unp); 179 } 180 181 static __inline void 182 unp_setflags(struct unpcb *unp, int flags) 183 { 184 atomic_set_int(&unp->unp_flags, flags); 185 } 186 187 static __inline void 188 unp_clrflags(struct unpcb *unp, int flags) 189 { 190 atomic_clear_int(&unp->unp_flags, flags); 191 } 192 193 /* 194 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort() 195 * will sofree() it when we return. 196 */ 197 static void 198 uipc_abort(netmsg_t msg) 199 { 200 struct unpcb *unp; 201 int error; 202 203 lwkt_gettoken(&unp_token); 204 unp = unp_getsocktoken(msg->base.nm_so); 205 206 if (UNP_ISATTACHED(unp)) { 207 unp_setflags(unp, UNP_DETACHED); 208 unp_drop(unp, ECONNABORTED); 209 unp_free(unp); 210 error = 0; 211 } else { 212 error = EINVAL; 213 } 214 215 unp_reltoken(unp); 216 lwkt_reltoken(&unp_token); 217 218 lwkt_replymsg(&msg->lmsg, error); 219 } 220 221 static void 222 uipc_accept(netmsg_t msg) 223 { 224 struct unpcb *unp; 225 int error; 226 227 lwkt_gettoken(&unp_token); 228 unp = unp_getsocktoken(msg->base.nm_so); 229 230 if (!UNP_ISATTACHED(unp)) { 231 error = EINVAL; 232 } else { 233 struct unpcb *unp2 = unp->unp_conn; 234 235 /* 236 * Pass back name of connected socket, 237 * if it was bound and we are still connected 238 * (our peer may have closed already!). 239 */ 240 if (unp2 && unp2->unp_addr) { 241 unp_reference(unp2); 242 *msg->accept.nm_nam = dup_sockaddr( 243 (struct sockaddr *)unp2->unp_addr); 244 unp_free(unp2); 245 } else { 246 *msg->accept.nm_nam = dup_sockaddr(&sun_noname); 247 } 248 error = 0; 249 } 250 251 unp_reltoken(unp); 252 lwkt_reltoken(&unp_token); 253 254 lwkt_replymsg(&msg->lmsg, error); 255 } 256 257 static void 258 uipc_attach(netmsg_t msg) 259 { 260 int error; 261 262 lwkt_gettoken(&unp_token); 263 264 KASSERT(msg->base.nm_so->so_pcb == NULL, ("double unp attach")); 265 error = unp_attach(msg->base.nm_so, msg->attach.nm_ai); 266 267 lwkt_reltoken(&unp_token); 268 lwkt_replymsg(&msg->lmsg, error); 269 } 270 271 static void 272 uipc_bind(netmsg_t msg) 273 { 274 struct unpcb *unp; 275 int error; 276 277 lwkt_gettoken(&unp_token); 278 unp = unp_getsocktoken(msg->base.nm_so); 279 280 if (UNP_ISATTACHED(unp)) 281 error = unp_bind(unp, msg->bind.nm_nam, msg->bind.nm_td); 282 else 283 error = EINVAL; 284 285 unp_reltoken(unp); 286 lwkt_reltoken(&unp_token); 287 288 lwkt_replymsg(&msg->lmsg, error); 289 } 290 291 static void 292 uipc_connect(netmsg_t msg) 293 { 294 int error; 295 296 error = unp_connect(msg->base.nm_so, msg->connect.nm_nam, 297 msg->connect.nm_td); 298 lwkt_replymsg(&msg->lmsg, error); 299 } 300 301 static void 302 uipc_connect2(netmsg_t msg) 303 { 304 int error; 305 306 error = unp_connect2(msg->connect2.nm_so1, msg->connect2.nm_so2); 307 lwkt_replymsg(&msg->lmsg, error); 308 } 309 310 /* control is EOPNOTSUPP */ 311 312 static void 313 uipc_detach(netmsg_t msg) 314 { 315 struct unpcb *unp; 316 int error; 317 318 lwkt_gettoken(&unp_token); 319 unp = unp_getsocktoken(msg->base.nm_so); 320 321 if (UNP_ISATTACHED(unp)) { 322 unp_setflags(unp, UNP_DETACHED); 323 unp_drop(unp, 0); 324 unp_free(unp); 325 error = 0; 326 } else { 327 error = EINVAL; 328 } 329 330 unp_reltoken(unp); 331 lwkt_reltoken(&unp_token); 332 333 lwkt_replymsg(&msg->lmsg, error); 334 } 335 336 static void 337 uipc_disconnect(netmsg_t msg) 338 { 339 struct unpcb *unp; 340 int error; 341 342 lwkt_gettoken(&unp_token); 343 unp = unp_getsocktoken(msg->base.nm_so); 344 345 if (UNP_ISATTACHED(unp)) { 346 unp_disconnect(unp, 0); 347 error = 0; 348 } else { 349 error = EINVAL; 350 } 351 352 unp_reltoken(unp); 353 lwkt_reltoken(&unp_token); 354 355 lwkt_replymsg(&msg->lmsg, error); 356 } 357 358 static void 359 uipc_listen(netmsg_t msg) 360 { 361 struct unpcb *unp; 362 int error; 363 364 lwkt_gettoken(&unp_token); 365 unp = unp_getsocktoken(msg->base.nm_so); 366 367 if (!UNP_ISATTACHED(unp) || unp->unp_vnode == NULL) 368 error = EINVAL; 369 else 370 error = unp_listen(unp, msg->listen.nm_td); 371 372 unp_reltoken(unp); 373 lwkt_reltoken(&unp_token); 374 375 lwkt_replymsg(&msg->lmsg, error); 376 } 377 378 static void 379 uipc_peeraddr(netmsg_t msg) 380 { 381 struct unpcb *unp; 382 int error; 383 384 lwkt_gettoken(&unp_token); 385 unp = unp_getsocktoken(msg->base.nm_so); 386 387 if (!UNP_ISATTACHED(unp)) { 388 error = EINVAL; 389 } else if (unp->unp_conn && unp->unp_conn->unp_addr) { 390 struct unpcb *unp2 = unp->unp_conn; 391 392 unp_reference(unp2); 393 *msg->peeraddr.nm_nam = dup_sockaddr( 394 (struct sockaddr *)unp2->unp_addr); 395 unp_free(unp2); 396 error = 0; 397 } else { 398 /* 399 * XXX: It seems that this test always fails even when 400 * connection is established. So, this else clause is 401 * added as workaround to return PF_LOCAL sockaddr. 402 */ 403 *msg->peeraddr.nm_nam = dup_sockaddr(&sun_noname); 404 error = 0; 405 } 406 407 unp_reltoken(unp); 408 lwkt_reltoken(&unp_token); 409 410 lwkt_replymsg(&msg->lmsg, error); 411 } 412 413 static void 414 uipc_rcvd(netmsg_t msg) 415 { 416 struct unpcb *unp, *unp2; 417 struct socket *so; 418 struct socket *so2; 419 int error; 420 421 /* 422 * so_pcb is only modified with both the global and the unp 423 * pool token held. 424 */ 425 so = msg->base.nm_so; 426 unp = unp_getsocktoken(so); 427 428 if (!UNP_ISATTACHED(unp)) { 429 error = EINVAL; 430 goto done; 431 } 432 433 switch (so->so_type) { 434 case SOCK_DGRAM: 435 panic("uipc_rcvd DGRAM?"); 436 /*NOTREACHED*/ 437 case SOCK_STREAM: 438 case SOCK_SEQPACKET: 439 if (unp->unp_conn == NULL) 440 break; 441 unp2 = unp->unp_conn; /* protected by pool token */ 442 443 /* 444 * Because we are transfering mbufs directly to the 445 * peer socket we have to use SSB_STOP on the sender 446 * to prevent it from building up infinite mbufs. 447 * 448 * As in several places in this module w ehave to ref unp2 449 * to ensure that it does not get ripped out from under us 450 * if we block on the so2 token or in sowwakeup(). 451 */ 452 so2 = unp2->unp_socket; 453 unp_reference(unp2); 454 lwkt_gettoken(&so2->so_rcv.ssb_token); 455 if (so->so_rcv.ssb_cc < so2->so_snd.ssb_hiwat && 456 so->so_rcv.ssb_mbcnt < so2->so_snd.ssb_mbmax 457 ) { 458 atomic_clear_int(&so2->so_snd.ssb_flags, SSB_STOP); 459 460 sowwakeup(so2); 461 } 462 lwkt_reltoken(&so2->so_rcv.ssb_token); 463 unp_free(unp2); 464 break; 465 default: 466 panic("uipc_rcvd unknown socktype"); 467 /*NOTREACHED*/ 468 } 469 error = 0; 470 done: 471 unp_reltoken(unp); 472 lwkt_replymsg(&msg->lmsg, error); 473 } 474 475 /* pru_rcvoob is EOPNOTSUPP */ 476 477 static void 478 uipc_send(netmsg_t msg) 479 { 480 struct unpcb *unp, *unp2; 481 struct socket *so; 482 struct socket *so2; 483 struct mbuf *control; 484 struct mbuf *m; 485 int error = 0; 486 487 so = msg->base.nm_so; 488 control = msg->send.nm_control; 489 m = msg->send.nm_m; 490 491 /* 492 * so_pcb is only modified with both the global and the unp 493 * pool token held. 494 */ 495 so = msg->base.nm_so; 496 unp = unp_getsocktoken(so); 497 498 if (!UNP_ISATTACHED(unp)) { 499 error = EINVAL; 500 goto release; 501 } 502 503 if (msg->send.nm_flags & PRUS_OOB) { 504 error = EOPNOTSUPP; 505 goto release; 506 } 507 508 wakeup_start_delayed(); 509 510 if (control && (error = unp_internalize(control, msg->send.nm_td))) 511 goto release; 512 513 switch (so->so_type) { 514 case SOCK_DGRAM: 515 { 516 struct sockaddr *from; 517 518 if (msg->send.nm_addr) { 519 if (unp->unp_conn) { 520 error = EISCONN; 521 break; 522 } 523 error = unp_find_lockref(msg->send.nm_addr, 524 msg->send.nm_td, so->so_type, &unp2); 525 if (error) 526 break; 527 /* 528 * NOTE: 529 * unp2 is locked and referenced. 530 * 531 * We could unlock unp2 now, since it was checked 532 * and referenced. 533 */ 534 unp_reltoken(unp2); 535 } else { 536 if (unp->unp_conn == NULL) { 537 error = ENOTCONN; 538 break; 539 } 540 unp2 = unp->unp_conn; 541 unp_reference(unp2); 542 } 543 /* NOTE: unp2 is referenced. */ 544 so2 = unp2->unp_socket; 545 546 if (unp->unp_addr) 547 from = (struct sockaddr *)unp->unp_addr; 548 else 549 from = &sun_noname; 550 551 lwkt_gettoken(&so2->so_rcv.ssb_token); 552 if (ssb_appendaddr(&so2->so_rcv, from, m, control)) { 553 sorwakeup(so2); 554 m = NULL; 555 control = NULL; 556 } else { 557 error = ENOBUFS; 558 } 559 lwkt_reltoken(&so2->so_rcv.ssb_token); 560 561 unp_free(unp2); 562 break; 563 } 564 565 case SOCK_STREAM: 566 case SOCK_SEQPACKET: 567 /* Connect if not connected yet. */ 568 /* 569 * Note: A better implementation would complain 570 * if not equal to the peer's address. 571 */ 572 if (unp->unp_conn == NULL) { 573 if (msg->send.nm_addr) { 574 error = unp_connect(so, 575 msg->send.nm_addr, 576 msg->send.nm_td); 577 if (error) 578 break; /* XXX */ 579 } 580 /* 581 * NOTE: 582 * unp_conn still could be NULL, even if the 583 * above unp_connect() succeeds; since the 584 * current unp's token could be released due 585 * to blocking operations after unp_conn is 586 * assigned. 587 */ 588 if (unp->unp_conn == NULL) { 589 error = ENOTCONN; 590 break; 591 } 592 } 593 if (so->so_state & SS_CANTSENDMORE) { 594 error = EPIPE; 595 break; 596 } 597 598 unp2 = unp->unp_conn; 599 KASSERT(unp2 != NULL, ("unp is not connected")); 600 so2 = unp2->unp_socket; 601 602 unp_reference(unp2); 603 604 /* 605 * Send to paired receive port, and then reduce 606 * send buffer hiwater marks to maintain backpressure. 607 * Wake up readers. 608 */ 609 lwkt_gettoken(&so2->so_rcv.ssb_token); 610 if (control) { 611 if (ssb_appendcontrol(&so2->so_rcv, m, control)) { 612 control = NULL; 613 m = NULL; 614 } 615 } else if (so->so_type == SOCK_SEQPACKET) { 616 sbappendrecord(&so2->so_rcv.sb, m); 617 m = NULL; 618 } else { 619 sbappend(&so2->so_rcv.sb, m); 620 m = NULL; 621 } 622 623 /* 624 * Because we are transfering mbufs directly to the 625 * peer socket we have to use SSB_STOP on the sender 626 * to prevent it from building up infinite mbufs. 627 */ 628 if (so2->so_rcv.ssb_cc >= so->so_snd.ssb_hiwat || 629 so2->so_rcv.ssb_mbcnt >= so->so_snd.ssb_mbmax 630 ) { 631 atomic_set_int(&so->so_snd.ssb_flags, SSB_STOP); 632 } 633 lwkt_reltoken(&so2->so_rcv.ssb_token); 634 sorwakeup(so2); 635 636 unp_free(unp2); 637 break; 638 639 default: 640 panic("uipc_send unknown socktype"); 641 } 642 643 /* 644 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN. 645 */ 646 if (msg->send.nm_flags & PRUS_EOF) { 647 socantsendmore(so); 648 unp_shutdown(unp); 649 } 650 651 if (control && error != 0) 652 unp_dispose(control); 653 release: 654 unp_reltoken(unp); 655 wakeup_end_delayed(); 656 657 if (control) 658 m_freem(control); 659 if (m) 660 m_freem(m); 661 lwkt_replymsg(&msg->lmsg, error); 662 } 663 664 /* 665 * MPSAFE 666 */ 667 static void 668 uipc_sense(netmsg_t msg) 669 { 670 struct unpcb *unp; 671 struct socket *so; 672 struct stat *sb; 673 int error; 674 675 so = msg->base.nm_so; 676 sb = msg->sense.nm_stat; 677 678 /* 679 * so_pcb is only modified with both the global and the unp 680 * pool token held. 681 */ 682 unp = unp_getsocktoken(so); 683 684 if (!UNP_ISATTACHED(unp)) { 685 error = EINVAL; 686 goto done; 687 } 688 689 sb->st_blksize = so->so_snd.ssb_hiwat; 690 sb->st_dev = NOUDEV; 691 if (unp->unp_ino == 0) { /* make up a non-zero inode number */ 692 spin_lock(&unp_ino_spin); 693 unp->unp_ino = unp_ino++; 694 spin_unlock(&unp_ino_spin); 695 } 696 sb->st_ino = unp->unp_ino; 697 error = 0; 698 done: 699 unp_reltoken(unp); 700 lwkt_replymsg(&msg->lmsg, error); 701 } 702 703 static void 704 uipc_shutdown(netmsg_t msg) 705 { 706 struct socket *so; 707 struct unpcb *unp; 708 int error; 709 710 /* 711 * so_pcb is only modified with both the global and the unp 712 * pool token held. 713 */ 714 so = msg->base.nm_so; 715 unp = unp_getsocktoken(so); 716 717 if (UNP_ISATTACHED(unp)) { 718 socantsendmore(so); 719 unp_shutdown(unp); 720 error = 0; 721 } else { 722 error = EINVAL; 723 } 724 725 unp_reltoken(unp); 726 lwkt_replymsg(&msg->lmsg, error); 727 } 728 729 static void 730 uipc_sockaddr(netmsg_t msg) 731 { 732 struct unpcb *unp; 733 int error; 734 735 /* 736 * so_pcb is only modified with both the global and the unp 737 * pool token held. 738 */ 739 unp = unp_getsocktoken(msg->base.nm_so); 740 741 if (UNP_ISATTACHED(unp)) { 742 if (unp->unp_addr) { 743 *msg->sockaddr.nm_nam = 744 dup_sockaddr((struct sockaddr *)unp->unp_addr); 745 } 746 error = 0; 747 } else { 748 error = EINVAL; 749 } 750 751 unp_reltoken(unp); 752 lwkt_replymsg(&msg->lmsg, error); 753 } 754 755 struct pr_usrreqs uipc_usrreqs = { 756 .pru_abort = uipc_abort, 757 .pru_accept = uipc_accept, 758 .pru_attach = uipc_attach, 759 .pru_bind = uipc_bind, 760 .pru_connect = uipc_connect, 761 .pru_connect2 = uipc_connect2, 762 .pru_control = pr_generic_notsupp, 763 .pru_detach = uipc_detach, 764 .pru_disconnect = uipc_disconnect, 765 .pru_listen = uipc_listen, 766 .pru_peeraddr = uipc_peeraddr, 767 .pru_rcvd = uipc_rcvd, 768 .pru_rcvoob = pr_generic_notsupp, 769 .pru_send = uipc_send, 770 .pru_sense = uipc_sense, 771 .pru_shutdown = uipc_shutdown, 772 .pru_sockaddr = uipc_sockaddr, 773 .pru_sosend = sosend, 774 .pru_soreceive = soreceive 775 }; 776 777 void 778 uipc_ctloutput(netmsg_t msg) 779 { 780 struct socket *so; 781 struct sockopt *sopt; 782 struct unpcb *unp; 783 int error = 0; 784 785 so = msg->base.nm_so; 786 sopt = msg->ctloutput.nm_sopt; 787 788 lwkt_gettoken(&unp_token); 789 unp = unp_getsocktoken(so); 790 791 if (!UNP_ISATTACHED(unp)) { 792 error = EINVAL; 793 goto done; 794 } 795 796 switch (sopt->sopt_dir) { 797 case SOPT_GET: 798 switch (sopt->sopt_name) { 799 case LOCAL_PEERCRED: 800 if (unp->unp_flags & UNP_HAVEPC) 801 soopt_from_kbuf(sopt, &unp->unp_peercred, 802 sizeof(unp->unp_peercred)); 803 else { 804 if (so->so_type == SOCK_STREAM) 805 error = ENOTCONN; 806 else if (so->so_type == SOCK_SEQPACKET) 807 error = ENOTCONN; 808 else 809 error = EINVAL; 810 } 811 break; 812 default: 813 error = EOPNOTSUPP; 814 break; 815 } 816 break; 817 case SOPT_SET: 818 default: 819 error = EOPNOTSUPP; 820 break; 821 } 822 823 done: 824 unp_reltoken(unp); 825 lwkt_reltoken(&unp_token); 826 827 lwkt_replymsg(&msg->lmsg, error); 828 } 829 830 /* 831 * Both send and receive buffers are allocated PIPSIZ bytes of buffering 832 * for stream sockets, although the total for sender and receiver is 833 * actually only PIPSIZ. 834 * 835 * Datagram sockets really use the sendspace as the maximum datagram size, 836 * and don't really want to reserve the sendspace. Their recvspace should 837 * be large enough for at least one max-size datagram plus address. 838 * 839 * We want the local send/recv space to be significant larger then lo0's 840 * mtu of 16384. 841 */ 842 #ifndef PIPSIZ 843 #define PIPSIZ 57344 844 #endif 845 static u_long unpst_sendspace = PIPSIZ; 846 static u_long unpst_recvspace = PIPSIZ; 847 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 848 static u_long unpdg_recvspace = 4*1024; 849 850 static int unp_rights; /* file descriptors in flight */ 851 static struct spinlock unp_spin = SPINLOCK_INITIALIZER(&unp_spin, "unp_spin"); 852 853 SYSCTL_DECL(_net_local_seqpacket); 854 SYSCTL_DECL(_net_local_stream); 855 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 856 &unpst_sendspace, 0, "Size of stream socket send buffer"); 857 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 858 &unpst_recvspace, 0, "Size of stream socket receive buffer"); 859 860 SYSCTL_DECL(_net_local_dgram); 861 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 862 &unpdg_sendspace, 0, "Max datagram socket size"); 863 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 864 &unpdg_recvspace, 0, "Size of datagram socket receive buffer"); 865 866 SYSCTL_DECL(_net_local); 867 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, 868 "File descriptors in flight"); 869 870 static int 871 unp_attach(struct socket *so, struct pru_attach_info *ai) 872 { 873 struct unpcb *unp; 874 int error; 875 876 lwkt_gettoken(&unp_token); 877 878 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) { 879 switch (so->so_type) { 880 case SOCK_STREAM: 881 case SOCK_SEQPACKET: 882 error = soreserve(so, unpst_sendspace, unpst_recvspace, 883 ai->sb_rlimit); 884 break; 885 886 case SOCK_DGRAM: 887 error = soreserve(so, unpdg_sendspace, unpdg_recvspace, 888 ai->sb_rlimit); 889 break; 890 891 default: 892 panic("unp_attach"); 893 } 894 if (error) 895 goto failed; 896 } 897 898 /* 899 * In order to support sendfile we have to set either SSB_STOPSUPP 900 * or SSB_PREALLOC. Unix domain sockets use the SSB_STOP flow 901 * control mechanism. 902 */ 903 if (so->so_type == SOCK_STREAM) { 904 atomic_set_int(&so->so_rcv.ssb_flags, SSB_STOPSUPP); 905 atomic_set_int(&so->so_snd.ssb_flags, SSB_STOPSUPP); 906 } 907 908 unp = kmalloc(sizeof(*unp), M_UNPCB, M_WAITOK | M_ZERO | M_NULLOK); 909 if (unp == NULL) { 910 error = ENOBUFS; 911 goto failed; 912 } 913 unp->unp_refcnt = 1; 914 unp->unp_gencnt = ++unp_gencnt; 915 unp_count++; 916 LIST_INIT(&unp->unp_refs); 917 unp->unp_socket = so; 918 unp->unp_rvnode = ai->fd_rdir; /* jail cruft XXX JH */ 919 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 920 : &unp_shead, unp, unp_link); 921 so->so_pcb = (caddr_t)unp; 922 soreference(so); 923 error = 0; 924 failed: 925 lwkt_reltoken(&unp_token); 926 return error; 927 } 928 929 static void 930 unp_detach(struct unpcb *unp) 931 { 932 struct socket *so; 933 934 lwkt_gettoken(&unp_token); 935 lwkt_getpooltoken(unp); 936 937 LIST_REMOVE(unp, unp_link); /* both tokens required */ 938 unp->unp_gencnt = ++unp_gencnt; 939 --unp_count; 940 if (unp->unp_vnode) { 941 unp->unp_vnode->v_socket = NULL; 942 vrele(unp->unp_vnode); 943 unp->unp_vnode = NULL; 944 } 945 soisdisconnected(unp->unp_socket); 946 so = unp->unp_socket; 947 soreference(so); /* for delayed sorflush */ 948 KKASSERT(so->so_pcb == unp); 949 so->so_pcb = NULL; /* both tokens required */ 950 unp->unp_socket = NULL; 951 sofree(so); /* remove pcb ref */ 952 953 if (unp_rights) { 954 /* 955 * Normally the receive buffer is flushed later, 956 * in sofree, but if our receive buffer holds references 957 * to descriptors that are now garbage, we will dispose 958 * of those descriptor references after the garbage collector 959 * gets them (resulting in a "panic: closef: count < 0"). 960 */ 961 sorflush(so); 962 unp_gc(); 963 } 964 sofree(so); 965 lwkt_relpooltoken(unp); 966 lwkt_reltoken(&unp_token); 967 968 KASSERT(unp->unp_conn == NULL, ("unp is still connected")); 969 KASSERT(LIST_EMPTY(&unp->unp_refs), ("unp still has references")); 970 971 if (unp->unp_addr) 972 kfree(unp->unp_addr, M_SONAME); 973 kfree(unp, M_UNPCB); 974 } 975 976 static int 977 unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td) 978 { 979 struct proc *p = td->td_proc; 980 struct sockaddr_un *soun = (struct sockaddr_un *)nam; 981 struct vnode *vp; 982 struct vattr vattr; 983 int error, namelen; 984 struct nlookupdata nd; 985 char buf[SOCK_MAXADDRLEN]; 986 987 ASSERT_LWKT_TOKEN_HELD(&unp_token); 988 UNP_ASSERT_TOKEN_HELD(unp); 989 990 if (unp->unp_vnode != NULL) 991 return EINVAL; 992 993 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 994 if (namelen <= 0) 995 return EINVAL; 996 strncpy(buf, soun->sun_path, namelen); 997 buf[namelen] = 0; /* null-terminate the string */ 998 error = nlookup_init(&nd, buf, UIO_SYSSPACE, 999 NLC_LOCKVP | NLC_CREATE | NLC_REFDVP); 1000 if (error == 0) 1001 error = nlookup(&nd); 1002 if (error == 0 && nd.nl_nch.ncp->nc_vp != NULL) 1003 error = EADDRINUSE; 1004 if (error) 1005 goto done; 1006 1007 VATTR_NULL(&vattr); 1008 vattr.va_type = VSOCK; 1009 vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask); 1010 error = VOP_NCREATE(&nd.nl_nch, nd.nl_dvp, &vp, nd.nl_cred, &vattr); 1011 if (error == 0) { 1012 if (unp->unp_vnode == NULL) { 1013 vp->v_socket = unp->unp_socket; 1014 unp->unp_vnode = vp; 1015 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam); 1016 vn_unlock(vp); 1017 } else { 1018 vput(vp); /* late race */ 1019 error = EINVAL; 1020 } 1021 } 1022 done: 1023 nlookup_done(&nd); 1024 return (error); 1025 } 1026 1027 static int 1028 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1029 { 1030 struct unpcb *unp, *unp2; 1031 int error, flags = 0; 1032 1033 lwkt_gettoken(&unp_token); 1034 1035 unp = unp_getsocktoken(so); 1036 if (!UNP_ISATTACHED(unp)) { 1037 error = EINVAL; 1038 goto failed; 1039 } 1040 1041 if ((unp->unp_flags & UNP_CONNECTING) || unp->unp_conn != NULL) { 1042 error = EISCONN; 1043 goto failed; 1044 } 1045 1046 flags = UNP_CONNECTING; 1047 unp_setflags(unp, flags); 1048 1049 error = unp_find_lockref(nam, td, so->so_type, &unp2); 1050 if (error) 1051 goto failed; 1052 /* 1053 * NOTE: 1054 * unp2 is locked and referenced. 1055 */ 1056 1057 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1058 struct socket *so2, *so3; 1059 struct unpcb *unp3; 1060 1061 so2 = unp2->unp_socket; 1062 if (!(so2->so_options & SO_ACCEPTCONN) || 1063 (so3 = sonewconn_faddr(so2, 0, NULL, 1064 TRUE /* keep ref */)) == NULL) { 1065 error = ECONNREFUSED; 1066 goto done; 1067 } 1068 /* so3 has a socket reference. */ 1069 1070 unp3 = unp_getsocktoken(so3); 1071 if (!UNP_ISATTACHED(unp3)) { 1072 unp_reltoken(unp3); 1073 /* 1074 * Already aborted; we only need to drop the 1075 * socket reference held by sonewconn_faddr(). 1076 */ 1077 sofree(so3); 1078 error = ECONNREFUSED; 1079 goto done; 1080 } 1081 unp_reference(unp3); 1082 /* 1083 * NOTE: 1084 * unp3 is locked and referenced. 1085 */ 1086 1087 /* 1088 * Release so3 socket reference held by sonewconn_faddr(). 1089 * Since we have referenced unp3, neither unp3 nor so3 will 1090 * be destroyed here. 1091 */ 1092 sofree(so3); 1093 1094 if (unp2->unp_addr != NULL) { 1095 unp3->unp_addr = (struct sockaddr_un *) 1096 dup_sockaddr((struct sockaddr *)unp2->unp_addr); 1097 } 1098 1099 /* 1100 * unp_peercred management: 1101 * 1102 * The connecter's (client's) credentials are copied 1103 * from its process structure at the time of connect() 1104 * (which is now). 1105 */ 1106 cru2x(td->td_proc->p_ucred, &unp3->unp_peercred); 1107 unp_setflags(unp3, UNP_HAVEPC); 1108 /* 1109 * The receiver's (server's) credentials are copied 1110 * from the unp_peercred member of socket on which the 1111 * former called listen(); unp_listen() cached that 1112 * process's credentials at that time so we can use 1113 * them now. 1114 */ 1115 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 1116 ("unp_connect: listener without cached peercred")); 1117 memcpy(&unp->unp_peercred, &unp2->unp_peercred, 1118 sizeof(unp->unp_peercred)); 1119 unp_setflags(unp, UNP_HAVEPC); 1120 1121 error = unp_connect_pair(unp, unp3); 1122 if (error) { 1123 /* XXX we need a better name */ 1124 soabort_oncpu(so3); 1125 } 1126 1127 /* Done with unp3 */ 1128 unp_free(unp3); 1129 unp_reltoken(unp3); 1130 } else { 1131 error = unp_connect_pair(unp, unp2); 1132 } 1133 done: 1134 unp_free(unp2); 1135 unp_reltoken(unp2); 1136 failed: 1137 if (flags) 1138 unp_clrflags(unp, flags); 1139 unp_reltoken(unp); 1140 1141 lwkt_reltoken(&unp_token); 1142 return (error); 1143 } 1144 1145 /* 1146 * Connect two unix domain sockets together. 1147 * 1148 * NOTE: Semantics for any change to unp_conn requires that the per-unp 1149 * pool token also be held. 1150 */ 1151 int 1152 unp_connect2(struct socket *so, struct socket *so2) 1153 { 1154 struct unpcb *unp, *unp2; 1155 int error; 1156 1157 lwkt_gettoken(&unp_token); 1158 if (so2->so_type != so->so_type) { 1159 lwkt_reltoken(&unp_token); 1160 return (EPROTOTYPE); 1161 } 1162 unp = unp_getsocktoken(so); 1163 unp2 = unp_getsocktoken(so2); 1164 1165 if (!UNP_ISATTACHED(unp)) { 1166 error = EINVAL; 1167 goto done; 1168 } 1169 if (!UNP_ISATTACHED(unp2)) { 1170 error = ECONNREFUSED; 1171 goto done; 1172 } 1173 1174 if (unp->unp_conn != NULL) { 1175 error = EISCONN; 1176 goto done; 1177 } 1178 if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) && 1179 unp2->unp_conn != NULL) { 1180 error = EISCONN; 1181 goto done; 1182 } 1183 1184 error = unp_connect_pair(unp, unp2); 1185 done: 1186 unp_reltoken(unp2); 1187 unp_reltoken(unp); 1188 lwkt_reltoken(&unp_token); 1189 return (error); 1190 } 1191 1192 /* 1193 * Disconnect a unix domain socket pair. 1194 * 1195 * NOTE: Semantics for any change to unp_conn requires that the per-unp 1196 * pool token also be held. 1197 */ 1198 static void 1199 unp_disconnect(struct unpcb *unp, int error) 1200 { 1201 struct socket *so = unp->unp_socket; 1202 struct unpcb *unp2; 1203 1204 ASSERT_LWKT_TOKEN_HELD(&unp_token); 1205 UNP_ASSERT_TOKEN_HELD(unp); 1206 1207 if (error) 1208 so->so_error = error; 1209 1210 while ((unp2 = unp->unp_conn) != NULL) { 1211 lwkt_getpooltoken(unp2); 1212 if (unp2 == unp->unp_conn) 1213 break; 1214 lwkt_relpooltoken(unp2); 1215 } 1216 if (unp2 == NULL) 1217 return; 1218 /* unp2 is locked. */ 1219 1220 KASSERT((unp2->unp_flags & UNP_DROPPED) == 0, ("unp2 was dropped")); 1221 1222 unp->unp_conn = NULL; 1223 1224 switch (so->so_type) { 1225 case SOCK_DGRAM: 1226 LIST_REMOVE(unp, unp_reflink); 1227 soclrstate(so, SS_ISCONNECTED); 1228 break; 1229 1230 case SOCK_STREAM: 1231 case SOCK_SEQPACKET: 1232 /* 1233 * Keep a reference before clearing the unp_conn 1234 * to avoid racing uipc_detach()/uipc_abort() in 1235 * other thread. 1236 */ 1237 unp_reference(unp2); 1238 KASSERT(unp2->unp_conn == unp, ("unp_conn mismatch")); 1239 unp2->unp_conn = NULL; 1240 1241 soisdisconnected(so); 1242 soisdisconnected(unp2->unp_socket); 1243 1244 unp_free(unp2); 1245 break; 1246 } 1247 1248 lwkt_relpooltoken(unp2); 1249 } 1250 1251 #ifdef notdef 1252 void 1253 unp_abort(struct unpcb *unp) 1254 { 1255 lwkt_gettoken(&unp_token); 1256 unp_free(unp); 1257 lwkt_reltoken(&unp_token); 1258 } 1259 #endif 1260 1261 static int 1262 prison_unpcb(struct thread *td, struct unpcb *unp) 1263 { 1264 struct proc *p; 1265 1266 if (td == NULL) 1267 return (0); 1268 if ((p = td->td_proc) == NULL) 1269 return (0); 1270 if (!p->p_ucred->cr_prison) 1271 return (0); 1272 if (p->p_fd->fd_rdir == unp->unp_rvnode) 1273 return (0); 1274 return (1); 1275 } 1276 1277 static int 1278 unp_pcblist(SYSCTL_HANDLER_ARGS) 1279 { 1280 int error, i, n; 1281 struct unpcb *unp, **unp_list; 1282 unp_gen_t gencnt; 1283 struct unp_head *head; 1284 1285 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 1286 1287 KKASSERT(curproc != NULL); 1288 1289 /* 1290 * The process of preparing the PCB list is too time-consuming and 1291 * resource-intensive to repeat twice on every request. 1292 */ 1293 if (req->oldptr == NULL) { 1294 n = unp_count; 1295 req->oldidx = (n + n/8) * sizeof(struct xunpcb); 1296 return 0; 1297 } 1298 1299 if (req->newptr != NULL) 1300 return EPERM; 1301 1302 lwkt_gettoken(&unp_token); 1303 1304 /* 1305 * OK, now we're committed to doing something. 1306 */ 1307 gencnt = unp_gencnt; 1308 n = unp_count; 1309 1310 unp_list = kmalloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 1311 1312 for (unp = LIST_FIRST(head), i = 0; unp && i < n; 1313 unp = LIST_NEXT(unp, unp_link)) { 1314 if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp)) 1315 unp_list[i++] = unp; 1316 } 1317 n = i; /* in case we lost some during malloc */ 1318 1319 error = 0; 1320 for (i = 0; i < n; i++) { 1321 unp = unp_list[i]; 1322 if (unp->unp_gencnt <= gencnt) { 1323 struct xunpcb xu; 1324 xu.xu_len = sizeof xu; 1325 xu.xu_unpp = unp; 1326 /* 1327 * XXX - need more locking here to protect against 1328 * connect/disconnect races for SMP. 1329 */ 1330 if (unp->unp_addr) 1331 bcopy(unp->unp_addr, &xu.xu_addr, 1332 unp->unp_addr->sun_len); 1333 if (unp->unp_conn && unp->unp_conn->unp_addr) 1334 bcopy(unp->unp_conn->unp_addr, 1335 &xu.xu_caddr, 1336 unp->unp_conn->unp_addr->sun_len); 1337 bcopy(unp, &xu.xu_unp, sizeof *unp); 1338 sotoxsocket(unp->unp_socket, &xu.xu_socket); 1339 error = SYSCTL_OUT(req, &xu, sizeof xu); 1340 } 1341 } 1342 lwkt_reltoken(&unp_token); 1343 kfree(unp_list, M_TEMP); 1344 1345 return error; 1346 } 1347 1348 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 1349 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 1350 "List of active local datagram sockets"); 1351 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 1352 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 1353 "List of active local stream sockets"); 1354 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, CTLFLAG_RD, 1355 (caddr_t)(long)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb", 1356 "List of active local seqpacket stream sockets"); 1357 1358 static void 1359 unp_shutdown(struct unpcb *unp) 1360 { 1361 struct socket *so; 1362 1363 if ((unp->unp_socket->so_type == SOCK_STREAM || 1364 unp->unp_socket->so_type == SOCK_SEQPACKET) && 1365 unp->unp_conn != NULL && (so = unp->unp_conn->unp_socket)) { 1366 socantrcvmore(so); 1367 } 1368 } 1369 1370 #ifdef notdef 1371 void 1372 unp_drain(void) 1373 { 1374 lwkt_gettoken(&unp_token); 1375 lwkt_reltoken(&unp_token); 1376 } 1377 #endif 1378 1379 int 1380 unp_externalize(struct mbuf *rights) 1381 { 1382 struct thread *td = curthread; 1383 struct proc *p = td->td_proc; /* XXX */ 1384 struct lwp *lp = td->td_lwp; 1385 struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 1386 int *fdp; 1387 int i; 1388 struct file **rp; 1389 struct file *fp; 1390 int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm)) 1391 / sizeof (struct file *); 1392 int f; 1393 1394 lwkt_gettoken(&unp_token); 1395 1396 /* 1397 * if the new FD's will not fit, then we free them all 1398 */ 1399 if (!fdavail(p, newfds)) { 1400 rp = (struct file **)CMSG_DATA(cm); 1401 for (i = 0; i < newfds; i++) { 1402 fp = *rp; 1403 /* 1404 * zero the pointer before calling unp_discard, 1405 * since it may end up in unp_gc().. 1406 */ 1407 *rp++ = NULL; 1408 unp_discard(fp, NULL); 1409 } 1410 lwkt_reltoken(&unp_token); 1411 return (EMSGSIZE); 1412 } 1413 1414 /* 1415 * now change each pointer to an fd in the global table to 1416 * an integer that is the index to the local fd table entry 1417 * that we set up to point to the global one we are transferring. 1418 * If sizeof (struct file *) is bigger than or equal to sizeof int, 1419 * then do it in forward order. In that case, an integer will 1420 * always come in the same place or before its corresponding 1421 * struct file pointer. 1422 * If sizeof (struct file *) is smaller than sizeof int, then 1423 * do it in reverse order. 1424 */ 1425 if (sizeof (struct file *) >= sizeof (int)) { 1426 fdp = (int *)CMSG_DATA(cm); 1427 rp = (struct file **)CMSG_DATA(cm); 1428 for (i = 0; i < newfds; i++) { 1429 if (fdalloc(p, 0, &f)) 1430 panic("unp_externalize"); 1431 fp = *rp++; 1432 unp_fp_externalize(lp, fp, f); 1433 *fdp++ = f; 1434 } 1435 } else { 1436 fdp = (int *)CMSG_DATA(cm) + newfds - 1; 1437 rp = (struct file **)CMSG_DATA(cm) + newfds - 1; 1438 for (i = 0; i < newfds; i++) { 1439 if (fdalloc(p, 0, &f)) 1440 panic("unp_externalize"); 1441 fp = *rp--; 1442 unp_fp_externalize(lp, fp, f); 1443 *fdp-- = f; 1444 } 1445 } 1446 1447 /* 1448 * Adjust length, in case sizeof(struct file *) and sizeof(int) 1449 * differs. 1450 */ 1451 cm->cmsg_len = CMSG_LEN(newfds * sizeof(int)); 1452 rights->m_len = cm->cmsg_len; 1453 1454 lwkt_reltoken(&unp_token); 1455 return (0); 1456 } 1457 1458 static void 1459 unp_fp_externalize(struct lwp *lp, struct file *fp, int fd) 1460 { 1461 struct file *fx; 1462 int error; 1463 1464 lwkt_gettoken(&unp_token); 1465 1466 if (lp) { 1467 KKASSERT(fd >= 0); 1468 if (fp->f_flag & FREVOKED) { 1469 kprintf("Warning: revoked fp exiting unix socket\n"); 1470 fx = NULL; 1471 error = falloc(lp, &fx, NULL); 1472 if (error == 0) 1473 fsetfd(lp->lwp_proc->p_fd, fx, fd); 1474 else 1475 fsetfd(lp->lwp_proc->p_fd, NULL, fd); 1476 fdrop(fx); 1477 } else { 1478 fsetfd(lp->lwp_proc->p_fd, fp, fd); 1479 } 1480 } 1481 spin_lock(&unp_spin); 1482 fp->f_msgcount--; 1483 unp_rights--; 1484 spin_unlock(&unp_spin); 1485 fdrop(fp); 1486 1487 lwkt_reltoken(&unp_token); 1488 } 1489 1490 1491 void 1492 unp_init(void) 1493 { 1494 LIST_INIT(&unp_dhead); 1495 LIST_INIT(&unp_shead); 1496 spin_init(&unp_spin, "unpinit"); 1497 } 1498 1499 static int 1500 unp_internalize(struct mbuf *control, struct thread *td) 1501 { 1502 struct proc *p = td->td_proc; 1503 struct filedesc *fdescp; 1504 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1505 struct file **rp; 1506 struct file *fp; 1507 int i, fd, *fdp; 1508 struct cmsgcred *cmcred; 1509 int oldfds; 1510 u_int newlen; 1511 int error; 1512 1513 KKASSERT(p); 1514 lwkt_gettoken(&unp_token); 1515 1516 fdescp = p->p_fd; 1517 if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) || 1518 cm->cmsg_level != SOL_SOCKET || 1519 CMSG_ALIGN(cm->cmsg_len) != control->m_len) { 1520 error = EINVAL; 1521 goto done; 1522 } 1523 1524 /* 1525 * Fill in credential information. 1526 */ 1527 if (cm->cmsg_type == SCM_CREDS) { 1528 cmcred = (struct cmsgcred *)CMSG_DATA(cm); 1529 cmcred->cmcred_pid = p->p_pid; 1530 cmcred->cmcred_uid = p->p_ucred->cr_ruid; 1531 cmcred->cmcred_gid = p->p_ucred->cr_rgid; 1532 cmcred->cmcred_euid = p->p_ucred->cr_uid; 1533 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups, 1534 CMGROUP_MAX); 1535 for (i = 0; i < cmcred->cmcred_ngroups; i++) 1536 cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i]; 1537 error = 0; 1538 goto done; 1539 } 1540 1541 /* 1542 * cmsghdr may not be aligned, do not allow calculation(s) to 1543 * go negative. 1544 */ 1545 if (cm->cmsg_len < CMSG_LEN(0)) { 1546 error = EINVAL; 1547 goto done; 1548 } 1549 1550 oldfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof (int); 1551 1552 /* 1553 * check that all the FDs passed in refer to legal OPEN files 1554 * If not, reject the entire operation. 1555 */ 1556 fdp = (int *)CMSG_DATA(cm); 1557 for (i = 0; i < oldfds; i++) { 1558 fd = *fdp++; 1559 if ((unsigned)fd >= fdescp->fd_nfiles || 1560 fdescp->fd_files[fd].fp == NULL) { 1561 error = EBADF; 1562 goto done; 1563 } 1564 if (fdescp->fd_files[fd].fp->f_type == DTYPE_KQUEUE) { 1565 error = EOPNOTSUPP; 1566 goto done; 1567 } 1568 } 1569 /* 1570 * Now replace the integer FDs with pointers to 1571 * the associated global file table entry.. 1572 * Allocate a bigger buffer as necessary. But if an cluster is not 1573 * enough, return E2BIG. 1574 */ 1575 newlen = CMSG_LEN(oldfds * sizeof(struct file *)); 1576 if (newlen > MCLBYTES) { 1577 error = E2BIG; 1578 goto done; 1579 } 1580 if (newlen - control->m_len > M_TRAILINGSPACE(control)) { 1581 if (control->m_flags & M_EXT) { 1582 error = E2BIG; 1583 goto done; 1584 } 1585 MCLGET(control, M_WAITOK); 1586 if (!(control->m_flags & M_EXT)) { 1587 error = ENOBUFS; 1588 goto done; 1589 } 1590 1591 /* copy the data to the cluster */ 1592 memcpy(mtod(control, char *), cm, cm->cmsg_len); 1593 cm = mtod(control, struct cmsghdr *); 1594 } 1595 1596 /* 1597 * Adjust length, in case sizeof(struct file *) and sizeof(int) 1598 * differs. 1599 */ 1600 cm->cmsg_len = newlen; 1601 control->m_len = CMSG_ALIGN(newlen); 1602 1603 /* 1604 * Transform the file descriptors into struct file pointers. 1605 * If sizeof (struct file *) is bigger than or equal to sizeof int, 1606 * then do it in reverse order so that the int won't get until 1607 * we're done. 1608 * If sizeof (struct file *) is smaller than sizeof int, then 1609 * do it in forward order. 1610 */ 1611 if (sizeof (struct file *) >= sizeof (int)) { 1612 fdp = (int *)CMSG_DATA(cm) + oldfds - 1; 1613 rp = (struct file **)CMSG_DATA(cm) + oldfds - 1; 1614 for (i = 0; i < oldfds; i++) { 1615 fp = fdescp->fd_files[*fdp--].fp; 1616 *rp-- = fp; 1617 fhold(fp); 1618 spin_lock(&unp_spin); 1619 fp->f_msgcount++; 1620 unp_rights++; 1621 spin_unlock(&unp_spin); 1622 } 1623 } else { 1624 fdp = (int *)CMSG_DATA(cm); 1625 rp = (struct file **)CMSG_DATA(cm); 1626 for (i = 0; i < oldfds; i++) { 1627 fp = fdescp->fd_files[*fdp++].fp; 1628 *rp++ = fp; 1629 fhold(fp); 1630 spin_lock(&unp_spin); 1631 fp->f_msgcount++; 1632 unp_rights++; 1633 spin_unlock(&unp_spin); 1634 } 1635 } 1636 error = 0; 1637 done: 1638 lwkt_reltoken(&unp_token); 1639 return error; 1640 } 1641 1642 /* 1643 * Garbage collect in-transit file descriptors that get lost due to 1644 * loops (i.e. when a socket is sent to another process over itself, 1645 * and more complex situations). 1646 * 1647 * NOT MPSAFE - TODO socket flush code and maybe closef. Rest is MPSAFE. 1648 */ 1649 1650 struct unp_gc_info { 1651 struct file **extra_ref; 1652 struct file *locked_fp; 1653 int defer; 1654 int index; 1655 int maxindex; 1656 }; 1657 1658 static void 1659 unp_gc(void) 1660 { 1661 struct unp_gc_info info; 1662 static boolean_t unp_gcing; 1663 struct file **fpp; 1664 int i; 1665 1666 /* 1667 * Only one gc can be in-progress at any given moment 1668 */ 1669 spin_lock(&unp_spin); 1670 if (unp_gcing) { 1671 spin_unlock(&unp_spin); 1672 return; 1673 } 1674 unp_gcing = TRUE; 1675 spin_unlock(&unp_spin); 1676 1677 lwkt_gettoken(&unp_token); 1678 1679 /* 1680 * Before going through all this, set all FDs to be NOT defered 1681 * and NOT externally accessible (not marked). During the scan 1682 * a fd can be marked externally accessible but we may or may not 1683 * be able to immediately process it (controlled by FDEFER). 1684 * 1685 * If we loop sleep a bit. The complexity of the topology can cause 1686 * multiple loops. Also failure to acquire the socket's so_rcv 1687 * token can cause us to loop. 1688 */ 1689 allfiles_scan_exclusive(unp_gc_clearmarks, NULL); 1690 do { 1691 info.defer = 0; 1692 allfiles_scan_exclusive(unp_gc_checkmarks, &info); 1693 if (info.defer) 1694 tsleep(&info, 0, "gcagain", 1); 1695 } while (info.defer); 1696 1697 /* 1698 * We grab an extra reference to each of the file table entries 1699 * that are not otherwise accessible and then free the rights 1700 * that are stored in messages on them. 1701 * 1702 * The bug in the orginal code is a little tricky, so I'll describe 1703 * what's wrong with it here. 1704 * 1705 * It is incorrect to simply unp_discard each entry for f_msgcount 1706 * times -- consider the case of sockets A and B that contain 1707 * references to each other. On a last close of some other socket, 1708 * we trigger a gc since the number of outstanding rights (unp_rights) 1709 * is non-zero. If during the sweep phase the gc code un_discards, 1710 * we end up doing a (full) closef on the descriptor. A closef on A 1711 * results in the following chain. Closef calls soo_close, which 1712 * calls soclose. Soclose calls first (through the switch 1713 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1714 * returns because the previous instance had set unp_gcing, and 1715 * we return all the way back to soclose, which marks the socket 1716 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1717 * to free up the rights that are queued in messages on the socket A, 1718 * i.e., the reference on B. The sorflush calls via the dom_dispose 1719 * switch unp_dispose, which unp_scans with unp_discard. This second 1720 * instance of unp_discard just calls closef on B. 1721 * 1722 * Well, a similar chain occurs on B, resulting in a sorflush on B, 1723 * which results in another closef on A. Unfortunately, A is already 1724 * being closed, and the descriptor has already been marked with 1725 * SS_NOFDREF, and soclose panics at this point. 1726 * 1727 * Here, we first take an extra reference to each inaccessible 1728 * descriptor. Then, we call sorflush ourself, since we know 1729 * it is a Unix domain socket anyhow. After we destroy all the 1730 * rights carried in messages, we do a last closef to get rid 1731 * of our extra reference. This is the last close, and the 1732 * unp_detach etc will shut down the socket. 1733 * 1734 * 91/09/19, bsy@cs.cmu.edu 1735 */ 1736 info.extra_ref = kmalloc(256 * sizeof(struct file *), M_FILE, M_WAITOK); 1737 info.maxindex = 256; 1738 1739 do { 1740 /* 1741 * Look for matches 1742 */ 1743 info.index = 0; 1744 allfiles_scan_exclusive(unp_gc_checkrefs, &info); 1745 1746 /* 1747 * For each FD on our hit list, do the following two things 1748 */ 1749 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) { 1750 struct file *tfp = *fpp; 1751 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) 1752 sorflush((struct socket *)(tfp->f_data)); 1753 } 1754 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) 1755 closef(*fpp, NULL); 1756 } while (info.index == info.maxindex); 1757 1758 lwkt_reltoken(&unp_token); 1759 1760 kfree((caddr_t)info.extra_ref, M_FILE); 1761 unp_gcing = FALSE; 1762 } 1763 1764 /* 1765 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry 1766 */ 1767 static int 1768 unp_gc_checkrefs(struct file *fp, void *data) 1769 { 1770 struct unp_gc_info *info = data; 1771 1772 if (fp->f_count == 0) 1773 return(0); 1774 if (info->index == info->maxindex) 1775 return(-1); 1776 1777 /* 1778 * If all refs are from msgs, and it's not marked accessible 1779 * then it must be referenced from some unreachable cycle 1780 * of (shut-down) FDs, so include it in our 1781 * list of FDs to remove 1782 */ 1783 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) { 1784 info->extra_ref[info->index++] = fp; 1785 fhold(fp); 1786 } 1787 return(0); 1788 } 1789 1790 /* 1791 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry 1792 */ 1793 static int 1794 unp_gc_clearmarks(struct file *fp, void *data __unused) 1795 { 1796 atomic_clear_int(&fp->f_flag, FMARK | FDEFER); 1797 return(0); 1798 } 1799 1800 /* 1801 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry 1802 */ 1803 static int 1804 unp_gc_checkmarks(struct file *fp, void *data) 1805 { 1806 struct unp_gc_info *info = data; 1807 struct socket *so; 1808 1809 /* 1810 * If the file is not open, skip it. Make sure it isn't marked 1811 * defered or we could loop forever, in case we somehow race 1812 * something. 1813 */ 1814 if (fp->f_count == 0) { 1815 if (fp->f_flag & FDEFER) 1816 atomic_clear_int(&fp->f_flag, FDEFER); 1817 return(0); 1818 } 1819 /* 1820 * If we already marked it as 'defer' in a 1821 * previous pass, then try process it this time 1822 * and un-mark it 1823 */ 1824 if (fp->f_flag & FDEFER) { 1825 atomic_clear_int(&fp->f_flag, FDEFER); 1826 } else { 1827 /* 1828 * if it's not defered, then check if it's 1829 * already marked.. if so skip it 1830 */ 1831 if (fp->f_flag & FMARK) 1832 return(0); 1833 /* 1834 * If all references are from messages 1835 * in transit, then skip it. it's not 1836 * externally accessible. 1837 */ 1838 if (fp->f_count == fp->f_msgcount) 1839 return(0); 1840 /* 1841 * If it got this far then it must be 1842 * externally accessible. 1843 */ 1844 atomic_set_int(&fp->f_flag, FMARK); 1845 } 1846 1847 /* 1848 * either it was defered, or it is externally 1849 * accessible and not already marked so. 1850 * Now check if it is possibly one of OUR sockets. 1851 */ 1852 if (fp->f_type != DTYPE_SOCKET || 1853 (so = (struct socket *)fp->f_data) == NULL) { 1854 return(0); 1855 } 1856 if (so->so_proto->pr_domain != &localdomain || 1857 !(so->so_proto->pr_flags & PR_RIGHTS)) { 1858 return(0); 1859 } 1860 1861 /* 1862 * So, Ok, it's one of our sockets and it IS externally accessible 1863 * (or was defered). Now we look to see if we hold any file 1864 * descriptors in its message buffers. Follow those links and mark 1865 * them as accessible too. 1866 * 1867 * We are holding multiple spinlocks here, if we cannot get the 1868 * token non-blocking defer until the next loop. 1869 */ 1870 info->locked_fp = fp; 1871 if (lwkt_trytoken(&so->so_rcv.ssb_token)) { 1872 unp_scan(so->so_rcv.ssb_mb, unp_mark, info); 1873 lwkt_reltoken(&so->so_rcv.ssb_token); 1874 } else { 1875 atomic_set_int(&fp->f_flag, FDEFER); 1876 ++info->defer; 1877 } 1878 return (0); 1879 } 1880 1881 /* 1882 * Scan all unix domain sockets and replace any revoked file pointers 1883 * found with the dummy file pointer fx. We don't worry about races 1884 * against file pointers being read out as those are handled in the 1885 * externalize code. 1886 */ 1887 1888 #define REVOKE_GC_MAXFILES 32 1889 1890 struct unp_revoke_gc_info { 1891 struct file *fx; 1892 struct file *fary[REVOKE_GC_MAXFILES]; 1893 int fcount; 1894 }; 1895 1896 void 1897 unp_revoke_gc(struct file *fx) 1898 { 1899 struct unp_revoke_gc_info info; 1900 int i; 1901 1902 lwkt_gettoken(&unp_token); 1903 info.fx = fx; 1904 do { 1905 info.fcount = 0; 1906 allfiles_scan_exclusive(unp_revoke_gc_check, &info); 1907 for (i = 0; i < info.fcount; ++i) 1908 unp_fp_externalize(NULL, info.fary[i], -1); 1909 } while (info.fcount == REVOKE_GC_MAXFILES); 1910 lwkt_reltoken(&unp_token); 1911 } 1912 1913 /* 1914 * Check for and replace revoked descriptors. 1915 * 1916 * WARNING: This routine is not allowed to block. 1917 */ 1918 static int 1919 unp_revoke_gc_check(struct file *fps, void *vinfo) 1920 { 1921 struct unp_revoke_gc_info *info = vinfo; 1922 struct file *fp; 1923 struct socket *so; 1924 struct mbuf *m0; 1925 struct mbuf *m; 1926 struct file **rp; 1927 struct cmsghdr *cm; 1928 int i; 1929 int qfds; 1930 1931 /* 1932 * Is this a unix domain socket with rights-passing abilities? 1933 */ 1934 if (fps->f_type != DTYPE_SOCKET) 1935 return (0); 1936 if ((so = (struct socket *)fps->f_data) == NULL) 1937 return(0); 1938 if (so->so_proto->pr_domain != &localdomain) 1939 return(0); 1940 if ((so->so_proto->pr_flags & PR_RIGHTS) == 0) 1941 return(0); 1942 1943 /* 1944 * Scan the mbufs for control messages and replace any revoked 1945 * descriptors we find. 1946 */ 1947 lwkt_gettoken(&so->so_rcv.ssb_token); 1948 m0 = so->so_rcv.ssb_mb; 1949 while (m0) { 1950 for (m = m0; m; m = m->m_next) { 1951 if (m->m_type != MT_CONTROL) 1952 continue; 1953 if (m->m_len < sizeof(*cm)) 1954 continue; 1955 cm = mtod(m, struct cmsghdr *); 1956 if (cm->cmsg_level != SOL_SOCKET || 1957 cm->cmsg_type != SCM_RIGHTS) { 1958 continue; 1959 } 1960 qfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(void *); 1961 rp = (struct file **)CMSG_DATA(cm); 1962 for (i = 0; i < qfds; i++) { 1963 fp = rp[i]; 1964 if (fp->f_flag & FREVOKED) { 1965 kprintf("Warning: Removing revoked fp from unix domain socket queue\n"); 1966 fhold(info->fx); 1967 info->fx->f_msgcount++; 1968 unp_rights++; 1969 rp[i] = info->fx; 1970 info->fary[info->fcount++] = fp; 1971 } 1972 if (info->fcount == REVOKE_GC_MAXFILES) 1973 break; 1974 } 1975 if (info->fcount == REVOKE_GC_MAXFILES) 1976 break; 1977 } 1978 m0 = m0->m_nextpkt; 1979 if (info->fcount == REVOKE_GC_MAXFILES) 1980 break; 1981 } 1982 lwkt_reltoken(&so->so_rcv.ssb_token); 1983 1984 /* 1985 * Stop the scan if we filled up our array. 1986 */ 1987 if (info->fcount == REVOKE_GC_MAXFILES) 1988 return(-1); 1989 return(0); 1990 } 1991 1992 /* 1993 * Dispose of the fp's stored in a mbuf. 1994 * 1995 * The dds loop can cause additional fps to be entered onto the 1996 * list while it is running, flattening out the operation and avoiding 1997 * a deep kernel stack recursion. 1998 */ 1999 void 2000 unp_dispose(struct mbuf *m) 2001 { 2002 unp_defdiscard_t dds; 2003 2004 lwkt_gettoken(&unp_token); 2005 ++unp_defdiscard_nest; 2006 if (m) { 2007 unp_scan(m, unp_discard, NULL); 2008 } 2009 if (unp_defdiscard_nest == 1) { 2010 while ((dds = unp_defdiscard_base) != NULL) { 2011 unp_defdiscard_base = dds->next; 2012 closef(dds->fp, NULL); 2013 kfree(dds, M_UNPCB); 2014 } 2015 } 2016 --unp_defdiscard_nest; 2017 lwkt_reltoken(&unp_token); 2018 } 2019 2020 static int 2021 unp_listen(struct unpcb *unp, struct thread *td) 2022 { 2023 struct proc *p = td->td_proc; 2024 2025 ASSERT_LWKT_TOKEN_HELD(&unp_token); 2026 UNP_ASSERT_TOKEN_HELD(unp); 2027 2028 KKASSERT(p); 2029 cru2x(p->p_ucred, &unp->unp_peercred); 2030 unp_setflags(unp, UNP_HAVEPCCACHED); 2031 return (0); 2032 } 2033 2034 static void 2035 unp_scan(struct mbuf *m0, void (*op)(struct file *, void *), void *data) 2036 { 2037 struct mbuf *m; 2038 struct file **rp; 2039 struct cmsghdr *cm; 2040 int i; 2041 int qfds; 2042 2043 while (m0) { 2044 for (m = m0; m; m = m->m_next) { 2045 if (m->m_type == MT_CONTROL && 2046 m->m_len >= sizeof(*cm)) { 2047 cm = mtod(m, struct cmsghdr *); 2048 if (cm->cmsg_level != SOL_SOCKET || 2049 cm->cmsg_type != SCM_RIGHTS) 2050 continue; 2051 qfds = (cm->cmsg_len - CMSG_LEN(0)) / 2052 sizeof(void *); 2053 rp = (struct file **)CMSG_DATA(cm); 2054 for (i = 0; i < qfds; i++) 2055 (*op)(*rp++, data); 2056 break; /* XXX, but saves time */ 2057 } 2058 } 2059 m0 = m0->m_nextpkt; 2060 } 2061 } 2062 2063 /* 2064 * Mark visibility. info->defer is recalculated on every pass. 2065 */ 2066 static void 2067 unp_mark(struct file *fp, void *data) 2068 { 2069 struct unp_gc_info *info = data; 2070 2071 if ((fp->f_flag & FMARK) == 0) { 2072 ++info->defer; 2073 atomic_set_int(&fp->f_flag, FMARK | FDEFER); 2074 } else if (fp->f_flag & FDEFER) { 2075 ++info->defer; 2076 } 2077 } 2078 2079 /* 2080 * Discard a fp previously held in a unix domain socket mbuf. To 2081 * avoid blowing out the kernel stack due to contrived chain-reactions 2082 * we may have to defer the operation to a higher procedural level. 2083 * 2084 * Caller holds unp_token 2085 */ 2086 static void 2087 unp_discard(struct file *fp, void *data __unused) 2088 { 2089 unp_defdiscard_t dds; 2090 2091 spin_lock(&unp_spin); 2092 fp->f_msgcount--; 2093 unp_rights--; 2094 spin_unlock(&unp_spin); 2095 2096 if (unp_defdiscard_nest) { 2097 dds = kmalloc(sizeof(*dds), M_UNPCB, M_WAITOK|M_ZERO); 2098 dds->fp = fp; 2099 dds->next = unp_defdiscard_base; 2100 unp_defdiscard_base = dds; 2101 } else { 2102 closef(fp, NULL); 2103 } 2104 } 2105 2106 static int 2107 unp_find_lockref(struct sockaddr *nam, struct thread *td, short type, 2108 struct unpcb **unp_ret) 2109 { 2110 struct proc *p = td->td_proc; 2111 struct sockaddr_un *soun = (struct sockaddr_un *)nam; 2112 struct vnode *vp = NULL; 2113 struct socket *so; 2114 struct unpcb *unp; 2115 int error, len; 2116 struct nlookupdata nd; 2117 char buf[SOCK_MAXADDRLEN]; 2118 2119 *unp_ret = NULL; 2120 2121 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 2122 if (len <= 0) { 2123 error = EINVAL; 2124 goto failed; 2125 } 2126 strncpy(buf, soun->sun_path, len); 2127 buf[len] = 0; 2128 2129 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW); 2130 if (error == 0) 2131 error = nlookup(&nd); 2132 if (error == 0) 2133 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); 2134 nlookup_done(&nd); 2135 if (error) { 2136 vp = NULL; 2137 goto failed; 2138 } 2139 2140 if (vp->v_type != VSOCK) { 2141 error = ENOTSOCK; 2142 goto failed; 2143 } 2144 error = VOP_EACCESS(vp, VWRITE, p->p_ucred); 2145 if (error) 2146 goto failed; 2147 so = vp->v_socket; 2148 if (so == NULL) { 2149 error = ECONNREFUSED; 2150 goto failed; 2151 } 2152 if (so->so_type != type) { 2153 error = EPROTOTYPE; 2154 goto failed; 2155 } 2156 2157 /* Lock this unp. */ 2158 unp = unp_getsocktoken(so); 2159 if (!UNP_ISATTACHED(unp)) { 2160 unp_reltoken(unp); 2161 error = ECONNREFUSED; 2162 goto failed; 2163 } 2164 /* And keep this unp referenced. */ 2165 unp_reference(unp); 2166 2167 /* Done! */ 2168 *unp_ret = unp; 2169 error = 0; 2170 failed: 2171 if (vp != NULL) 2172 vput(vp); 2173 return error; 2174 } 2175 2176 static int 2177 unp_connect_pair(struct unpcb *unp, struct unpcb *unp2) 2178 { 2179 struct socket *so = unp->unp_socket; 2180 struct socket *so2 = unp2->unp_socket; 2181 2182 ASSERT_LWKT_TOKEN_HELD(&unp_token); 2183 UNP_ASSERT_TOKEN_HELD(unp); 2184 UNP_ASSERT_TOKEN_HELD(unp2); 2185 2186 KASSERT(so->so_type == so2->so_type, 2187 ("socket type mismatch, so %d, so2 %d", so->so_type, so2->so_type)); 2188 2189 if (!UNP_ISATTACHED(unp)) 2190 return EINVAL; 2191 if (!UNP_ISATTACHED(unp2)) 2192 return ECONNREFUSED; 2193 2194 KASSERT(unp->unp_conn == NULL, ("unp is already connected")); 2195 unp->unp_conn = unp2; 2196 2197 switch (so->so_type) { 2198 case SOCK_DGRAM: 2199 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 2200 soisconnected(so); 2201 break; 2202 2203 case SOCK_STREAM: 2204 case SOCK_SEQPACKET: 2205 KASSERT(unp2->unp_conn == NULL, ("unp2 is already connected")); 2206 unp2->unp_conn = unp; 2207 soisconnected(so); 2208 soisconnected(so2); 2209 break; 2210 2211 default: 2212 panic("unp_connect_pair: unknown socket type %d", so->so_type); 2213 } 2214 return 0; 2215 } 2216 2217 static void 2218 unp_drop(struct unpcb *unp, int error) 2219 { 2220 struct unpcb *unp2; 2221 2222 ASSERT_LWKT_TOKEN_HELD(&unp_token); 2223 UNP_ASSERT_TOKEN_HELD(unp); 2224 KASSERT(unp->unp_flags & UNP_DETACHED, ("unp is not detached")); 2225 2226 unp_disconnect(unp, error); 2227 2228 while ((unp2 = LIST_FIRST(&unp->unp_refs)) != NULL) { 2229 lwkt_getpooltoken(unp2); 2230 unp_disconnect(unp2, ECONNRESET); 2231 lwkt_relpooltoken(unp2); 2232 } 2233 unp_setflags(unp, UNP_DROPPED); 2234 } 2235