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