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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 34 * $Id: uipc_usrreq.c,v 1.44 1999/05/10 18:09:39 truckman Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/domain.h> 41 #include <sys/fcntl.h> 42 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 43 #include <sys/file.h> 44 #include <sys/filedesc.h> 45 #include <sys/lock.h> 46 #include <sys/mbuf.h> 47 #include <sys/namei.h> 48 #include <sys/proc.h> 49 #include <sys/protosw.h> 50 #include <sys/socket.h> 51 #include <sys/socketvar.h> 52 #include <sys/stat.h> 53 #include <sys/sysctl.h> 54 #include <sys/un.h> 55 #include <sys/unpcb.h> 56 #include <sys/vnode.h> 57 58 #include <vm/vm_zone.h> 59 60 static struct vm_zone *unp_zone; 61 static unp_gen_t unp_gencnt; 62 static u_int unp_count; 63 64 static struct unp_head unp_shead, unp_dhead; 65 66 /* 67 * Unix communications domain. 68 * 69 * TODO: 70 * SEQPACKET, RDM 71 * rethink name space problems 72 * need a proper out-of-band 73 * lock pushdown 74 */ 75 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 76 static ino_t unp_ino; /* prototype for fake inode numbers */ 77 78 static int unp_attach __P((struct socket *)); 79 static void unp_detach __P((struct unpcb *)); 80 static int unp_bind __P((struct unpcb *,struct sockaddr *, struct proc *)); 81 static int unp_connect __P((struct socket *,struct sockaddr *, 82 struct proc *)); 83 static void unp_disconnect __P((struct unpcb *)); 84 static void unp_shutdown __P((struct unpcb *)); 85 static void unp_drop __P((struct unpcb *, int)); 86 static void unp_gc __P((void)); 87 static void unp_scan __P((struct mbuf *, void (*)(struct file *))); 88 static void unp_mark __P((struct file *)); 89 static void unp_discard __P((struct file *)); 90 static int unp_internalize __P((struct mbuf *, struct proc *)); 91 92 static int 93 uipc_abort(struct socket *so) 94 { 95 struct unpcb *unp = sotounpcb(so); 96 97 if (unp == 0) 98 return EINVAL; 99 unp_drop(unp, ECONNABORTED); 100 return 0; 101 } 102 103 static int 104 uipc_accept(struct socket *so, struct sockaddr **nam) 105 { 106 struct unpcb *unp = sotounpcb(so); 107 108 if (unp == 0) 109 return EINVAL; 110 111 /* 112 * Pass back name of connected socket, 113 * if it was bound and we are still connected 114 * (our peer may have closed already!). 115 */ 116 if (unp->unp_conn && unp->unp_conn->unp_addr) { 117 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, 118 1); 119 } else { 120 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); 121 } 122 return 0; 123 } 124 125 static int 126 uipc_attach(struct socket *so, int proto, struct proc *p) 127 { 128 struct unpcb *unp = sotounpcb(so); 129 130 if (unp != 0) 131 return EISCONN; 132 return unp_attach(so); 133 } 134 135 static int 136 uipc_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 137 { 138 struct unpcb *unp = sotounpcb(so); 139 140 if (unp == 0) 141 return EINVAL; 142 143 return unp_bind(unp, nam, p); 144 } 145 146 static int 147 uipc_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 148 { 149 struct unpcb *unp = sotounpcb(so); 150 151 if (unp == 0) 152 return EINVAL; 153 return unp_connect(so, nam, curproc); 154 } 155 156 static int 157 uipc_connect2(struct socket *so1, struct socket *so2) 158 { 159 struct unpcb *unp = sotounpcb(so1); 160 161 if (unp == 0) 162 return EINVAL; 163 164 return unp_connect2(so1, so2); 165 } 166 167 /* control is EOPNOTSUPP */ 168 169 static int 170 uipc_detach(struct socket *so) 171 { 172 struct unpcb *unp = sotounpcb(so); 173 174 if (unp == 0) 175 return EINVAL; 176 177 unp_detach(unp); 178 return 0; 179 } 180 181 static int 182 uipc_disconnect(struct socket *so) 183 { 184 struct unpcb *unp = sotounpcb(so); 185 186 if (unp == 0) 187 return EINVAL; 188 unp_disconnect(unp); 189 return 0; 190 } 191 192 static int 193 uipc_listen(struct socket *so, struct proc *p) 194 { 195 struct unpcb *unp = sotounpcb(so); 196 197 if (unp == 0 || unp->unp_vnode == 0) 198 return EINVAL; 199 return 0; 200 } 201 202 static int 203 uipc_peeraddr(struct socket *so, struct sockaddr **nam) 204 { 205 struct unpcb *unp = sotounpcb(so); 206 207 if (unp == 0) 208 return EINVAL; 209 if (unp->unp_conn && unp->unp_conn->unp_addr) 210 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, 211 1); 212 return 0; 213 } 214 215 static int 216 uipc_rcvd(struct socket *so, int flags) 217 { 218 struct unpcb *unp = sotounpcb(so); 219 struct socket *so2; 220 221 if (unp == 0) 222 return EINVAL; 223 switch (so->so_type) { 224 case SOCK_DGRAM: 225 panic("uipc_rcvd DGRAM?"); 226 /*NOTREACHED*/ 227 228 case SOCK_STREAM: 229 #define rcv (&so->so_rcv) 230 #define snd (&so2->so_snd) 231 if (unp->unp_conn == 0) 232 break; 233 so2 = unp->unp_conn->unp_socket; 234 /* 235 * Adjust backpressure on sender 236 * and wakeup any waiting to write. 237 */ 238 snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; 239 unp->unp_mbcnt = rcv->sb_mbcnt; 240 snd->sb_hiwat += unp->unp_cc - rcv->sb_cc; 241 unp->unp_cc = rcv->sb_cc; 242 sowwakeup(so2); 243 #undef snd 244 #undef rcv 245 break; 246 247 default: 248 panic("uipc_rcvd unknown socktype"); 249 } 250 return 0; 251 } 252 253 /* pru_rcvoob is EOPNOTSUPP */ 254 255 static int 256 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 257 struct mbuf *control, struct proc *p) 258 { 259 int error = 0; 260 struct unpcb *unp = sotounpcb(so); 261 struct socket *so2; 262 263 if (unp == 0) { 264 error = EINVAL; 265 goto release; 266 } 267 if (flags & PRUS_OOB) { 268 error = EOPNOTSUPP; 269 goto release; 270 } 271 272 if (control && (error = unp_internalize(control, p))) 273 goto release; 274 275 switch (so->so_type) { 276 case SOCK_DGRAM: 277 { 278 struct sockaddr *from; 279 280 if (nam) { 281 if (unp->unp_conn) { 282 error = EISCONN; 283 break; 284 } 285 error = unp_connect(so, nam, p); 286 if (error) 287 break; 288 } else { 289 if (unp->unp_conn == 0) { 290 error = ENOTCONN; 291 break; 292 } 293 } 294 so2 = unp->unp_conn->unp_socket; 295 if (unp->unp_addr) 296 from = (struct sockaddr *)unp->unp_addr; 297 else 298 from = &sun_noname; 299 if (sbappendaddr(&so2->so_rcv, from, m, control)) { 300 sorwakeup(so2); 301 m = 0; 302 control = 0; 303 } else 304 error = ENOBUFS; 305 if (nam) 306 unp_disconnect(unp); 307 break; 308 } 309 310 case SOCK_STREAM: 311 #define rcv (&so2->so_rcv) 312 #define snd (&so->so_snd) 313 /* Connect if not connected yet. */ 314 /* 315 * Note: A better implementation would complain 316 * if not equal to the peer's address. 317 */ 318 if ((so->so_state & SS_ISCONNECTED) == 0) { 319 if (nam) { 320 error = unp_connect(so, nam, p); 321 if (error) 322 break; /* XXX */ 323 } else { 324 error = ENOTCONN; 325 break; 326 } 327 } 328 329 if (so->so_state & SS_CANTSENDMORE) { 330 error = EPIPE; 331 break; 332 } 333 if (unp->unp_conn == 0) 334 panic("uipc_send connected but no connection?"); 335 so2 = unp->unp_conn->unp_socket; 336 /* 337 * Send to paired receive port, and then reduce 338 * send buffer hiwater marks to maintain backpressure. 339 * Wake up readers. 340 */ 341 if (control) { 342 if (sbappendcontrol(rcv, m, control)) 343 control = 0; 344 } else 345 sbappend(rcv, m); 346 snd->sb_mbmax -= 347 rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; 348 unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; 349 snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc; 350 unp->unp_conn->unp_cc = rcv->sb_cc; 351 sorwakeup(so2); 352 m = 0; 353 #undef snd 354 #undef rcv 355 break; 356 357 default: 358 panic("uipc_send unknown socktype"); 359 } 360 361 /* 362 * SEND_EOF is equivalent to a SEND followed by 363 * a SHUTDOWN. 364 */ 365 if (flags & PRUS_EOF) { 366 socantsendmore(so); 367 unp_shutdown(unp); 368 } 369 370 if (control && error != 0) 371 unp_dispose(control); 372 373 release: 374 if (control) 375 m_freem(control); 376 if (m) 377 m_freem(m); 378 return error; 379 } 380 381 static int 382 uipc_sense(struct socket *so, struct stat *sb) 383 { 384 struct unpcb *unp = sotounpcb(so); 385 struct socket *so2; 386 387 if (unp == 0) 388 return EINVAL; 389 sb->st_blksize = so->so_snd.sb_hiwat; 390 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 391 so2 = unp->unp_conn->unp_socket; 392 sb->st_blksize += so2->so_rcv.sb_cc; 393 } 394 sb->st_dev = NOUDEV; 395 if (unp->unp_ino == 0) 396 unp->unp_ino = unp_ino++; 397 sb->st_ino = unp->unp_ino; 398 return (0); 399 } 400 401 static int 402 uipc_shutdown(struct socket *so) 403 { 404 struct unpcb *unp = sotounpcb(so); 405 406 if (unp == 0) 407 return EINVAL; 408 socantsendmore(so); 409 unp_shutdown(unp); 410 return 0; 411 } 412 413 static int 414 uipc_sockaddr(struct socket *so, struct sockaddr **nam) 415 { 416 struct unpcb *unp = sotounpcb(so); 417 418 if (unp == 0) 419 return EINVAL; 420 if (unp->unp_addr) 421 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1); 422 return 0; 423 } 424 425 struct pr_usrreqs uipc_usrreqs = { 426 uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 427 uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 428 uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 429 uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 430 sosend, soreceive, sopoll 431 }; 432 433 /* 434 * Both send and receive buffers are allocated PIPSIZ bytes of buffering 435 * for stream sockets, although the total for sender and receiver is 436 * actually only PIPSIZ. 437 * Datagram sockets really use the sendspace as the maximum datagram size, 438 * and don't really want to reserve the sendspace. Their recvspace should 439 * be large enough for at least one max-size datagram plus address. 440 */ 441 #ifndef PIPSIZ 442 #define PIPSIZ 8192 443 #endif 444 static u_long unpst_sendspace = PIPSIZ; 445 static u_long unpst_recvspace = PIPSIZ; 446 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 447 static u_long unpdg_recvspace = 4*1024; 448 449 static int unp_rights; /* file descriptors in flight */ 450 451 SYSCTL_DECL(_net_local_stream); 452 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 453 &unpst_sendspace, 0, ""); 454 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 455 &unpst_recvspace, 0, ""); 456 SYSCTL_DECL(_net_local_dgram); 457 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 458 &unpdg_sendspace, 0, ""); 459 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 460 &unpdg_recvspace, 0, ""); 461 SYSCTL_DECL(_net_local); 462 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 463 464 static int 465 unp_attach(so) 466 struct socket *so; 467 { 468 register struct unpcb *unp; 469 int error; 470 471 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 472 switch (so->so_type) { 473 474 case SOCK_STREAM: 475 error = soreserve(so, unpst_sendspace, unpst_recvspace); 476 break; 477 478 case SOCK_DGRAM: 479 error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 480 break; 481 482 default: 483 panic("unp_attach"); 484 } 485 if (error) 486 return (error); 487 } 488 unp = zalloc(unp_zone); 489 if (unp == NULL) 490 return (ENOBUFS); 491 bzero(unp, sizeof *unp); 492 unp->unp_gencnt = ++unp_gencnt; 493 unp_count++; 494 LIST_INIT(&unp->unp_refs); 495 unp->unp_socket = so; 496 unp->unp_rvnode = curproc->p_fd->fd_rdir; 497 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 498 : &unp_shead, unp, unp_link); 499 so->so_pcb = (caddr_t)unp; 500 return (0); 501 } 502 503 static void 504 unp_detach(unp) 505 register struct unpcb *unp; 506 { 507 LIST_REMOVE(unp, unp_link); 508 unp->unp_gencnt = ++unp_gencnt; 509 --unp_count; 510 if (unp->unp_vnode) { 511 unp->unp_vnode->v_socket = 0; 512 vrele(unp->unp_vnode); 513 unp->unp_vnode = 0; 514 } 515 if (unp->unp_conn) 516 unp_disconnect(unp); 517 while (unp->unp_refs.lh_first) 518 unp_drop(unp->unp_refs.lh_first, ECONNRESET); 519 soisdisconnected(unp->unp_socket); 520 unp->unp_socket->so_pcb = 0; 521 if (unp_rights) { 522 /* 523 * Normally the receive buffer is flushed later, 524 * in sofree, but if our receive buffer holds references 525 * to descriptors that are now garbage, we will dispose 526 * of those descriptor references after the garbage collector 527 * gets them (resulting in a "panic: closef: count < 0"). 528 */ 529 sorflush(unp->unp_socket); 530 unp_gc(); 531 } 532 if (unp->unp_addr) 533 FREE(unp->unp_addr, M_SONAME); 534 zfree(unp_zone, unp); 535 } 536 537 static int 538 unp_bind(unp, nam, p) 539 struct unpcb *unp; 540 struct sockaddr *nam; 541 struct proc *p; 542 { 543 struct sockaddr_un *soun = (struct sockaddr_un *)nam; 544 register struct vnode *vp; 545 struct vattr vattr; 546 int error, namelen; 547 struct nameidata nd; 548 char buf[SOCK_MAXADDRLEN]; 549 550 if (unp->unp_vnode != NULL) 551 return (EINVAL); 552 #define offsetof(s, e) ((char *)&((s *)0)->e - (char *)((s *)0)) 553 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 554 if (namelen <= 0) 555 return EINVAL; 556 strncpy(buf, soun->sun_path, namelen); 557 buf[namelen] = 0; /* null-terminate the string */ 558 NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE, 559 buf, p); 560 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 561 error = namei(&nd); 562 if (error) 563 return (error); 564 vp = nd.ni_vp; 565 if (vp != NULL) { 566 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 567 if (nd.ni_dvp == vp) 568 vrele(nd.ni_dvp); 569 else 570 vput(nd.ni_dvp); 571 vrele(vp); 572 return (EADDRINUSE); 573 } 574 VATTR_NULL(&vattr); 575 vattr.va_type = VSOCK; 576 vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask); 577 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 578 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 579 vput(nd.ni_dvp); 580 if (error) 581 return (error); 582 vp = nd.ni_vp; 583 vp->v_socket = unp->unp_socket; 584 unp->unp_vnode = vp; 585 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1); 586 VOP_UNLOCK(vp, 0, p); 587 return (0); 588 } 589 590 static int 591 unp_connect(so, nam, p) 592 struct socket *so; 593 struct sockaddr *nam; 594 struct proc *p; 595 { 596 register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 597 register struct vnode *vp; 598 register struct socket *so2, *so3; 599 struct unpcb *unp2, *unp3; 600 int error, len; 601 struct nameidata nd; 602 char buf[SOCK_MAXADDRLEN]; 603 604 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 605 if (len <= 0) 606 return EINVAL; 607 strncpy(buf, soun->sun_path, len); 608 buf[len] = 0; 609 610 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, p); 611 error = namei(&nd); 612 if (error) 613 return (error); 614 vp = nd.ni_vp; 615 if (vp->v_type != VSOCK) { 616 error = ENOTSOCK; 617 goto bad; 618 } 619 error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p); 620 if (error) 621 goto bad; 622 so2 = vp->v_socket; 623 if (so2 == 0) { 624 error = ECONNREFUSED; 625 goto bad; 626 } 627 if (so->so_type != so2->so_type) { 628 error = EPROTOTYPE; 629 goto bad; 630 } 631 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 632 if ((so2->so_options & SO_ACCEPTCONN) == 0 || 633 (so3 = sonewconn(so2, 0)) == 0) { 634 error = ECONNREFUSED; 635 goto bad; 636 } 637 unp2 = sotounpcb(so2); 638 unp3 = sotounpcb(so3); 639 if (unp2->unp_addr) 640 unp3->unp_addr = (struct sockaddr_un *) 641 dup_sockaddr((struct sockaddr *) 642 unp2->unp_addr, 1); 643 so2 = so3; 644 } 645 error = unp_connect2(so, so2); 646 bad: 647 vput(vp); 648 return (error); 649 } 650 651 int 652 unp_connect2(so, so2) 653 register struct socket *so; 654 register struct socket *so2; 655 { 656 register struct unpcb *unp = sotounpcb(so); 657 register struct unpcb *unp2; 658 659 if (so2->so_type != so->so_type) 660 return (EPROTOTYPE); 661 unp2 = sotounpcb(so2); 662 unp->unp_conn = unp2; 663 switch (so->so_type) { 664 665 case SOCK_DGRAM: 666 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 667 soisconnected(so); 668 break; 669 670 case SOCK_STREAM: 671 unp2->unp_conn = unp; 672 soisconnected(so); 673 soisconnected(so2); 674 break; 675 676 default: 677 panic("unp_connect2"); 678 } 679 return (0); 680 } 681 682 static void 683 unp_disconnect(unp) 684 struct unpcb *unp; 685 { 686 register struct unpcb *unp2 = unp->unp_conn; 687 688 if (unp2 == 0) 689 return; 690 unp->unp_conn = 0; 691 switch (unp->unp_socket->so_type) { 692 693 case SOCK_DGRAM: 694 LIST_REMOVE(unp, unp_reflink); 695 unp->unp_socket->so_state &= ~SS_ISCONNECTED; 696 break; 697 698 case SOCK_STREAM: 699 soisdisconnected(unp->unp_socket); 700 unp2->unp_conn = 0; 701 soisdisconnected(unp2->unp_socket); 702 break; 703 } 704 } 705 706 #ifdef notdef 707 void 708 unp_abort(unp) 709 struct unpcb *unp; 710 { 711 712 unp_detach(unp); 713 } 714 #endif 715 716 static int 717 prison_unpcb(struct proc *p, struct unpcb *unp) 718 { 719 if (!p->p_prison) 720 return (0); 721 if (p->p_fd->fd_rdir == unp->unp_rvnode) 722 return (0); 723 return (1); 724 } 725 726 static int 727 unp_pcblist SYSCTL_HANDLER_ARGS 728 { 729 int error, i, n; 730 struct unpcb *unp, **unp_list; 731 unp_gen_t gencnt; 732 struct xunpgen xug; 733 struct unp_head *head; 734 735 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 736 737 /* 738 * The process of preparing the PCB list is too time-consuming and 739 * resource-intensive to repeat twice on every request. 740 */ 741 if (req->oldptr == 0) { 742 n = unp_count; 743 req->oldidx = 2 * (sizeof xug) 744 + (n + n/8) * sizeof(struct xunpcb); 745 return 0; 746 } 747 748 if (req->newptr != 0) 749 return EPERM; 750 751 /* 752 * OK, now we're committed to doing something. 753 */ 754 gencnt = unp_gencnt; 755 n = unp_count; 756 757 xug.xug_len = sizeof xug; 758 xug.xug_count = n; 759 xug.xug_gen = gencnt; 760 xug.xug_sogen = so_gencnt; 761 error = SYSCTL_OUT(req, &xug, sizeof xug); 762 if (error) 763 return error; 764 765 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 766 if (unp_list == 0) 767 return ENOMEM; 768 769 for (unp = head->lh_first, i = 0; unp && i < n; 770 unp = unp->unp_link.le_next) { 771 if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->p, unp)) 772 unp_list[i++] = unp; 773 } 774 n = i; /* in case we lost some during malloc */ 775 776 error = 0; 777 for (i = 0; i < n; i++) { 778 unp = unp_list[i]; 779 if (unp->unp_gencnt <= gencnt) { 780 struct xunpcb xu; 781 xu.xu_len = sizeof xu; 782 xu.xu_unpp = unp; 783 /* 784 * XXX - need more locking here to protect against 785 * connect/disconnect races for SMP. 786 */ 787 if (unp->unp_addr) 788 bcopy(unp->unp_addr, &xu.xu_addr, 789 unp->unp_addr->sun_len); 790 if (unp->unp_conn && unp->unp_conn->unp_addr) 791 bcopy(unp->unp_conn->unp_addr, 792 &xu.xu_caddr, 793 unp->unp_conn->unp_addr->sun_len); 794 bcopy(unp, &xu.xu_unp, sizeof *unp); 795 sotoxsocket(unp->unp_socket, &xu.xu_socket); 796 error = SYSCTL_OUT(req, &xu, sizeof xu); 797 } 798 } 799 if (!error) { 800 /* 801 * Give the user an updated idea of our state. 802 * If the generation differs from what we told 803 * her before, she knows that something happened 804 * while we were processing this request, and it 805 * might be necessary to retry. 806 */ 807 xug.xug_gen = unp_gencnt; 808 xug.xug_sogen = so_gencnt; 809 xug.xug_count = unp_count; 810 error = SYSCTL_OUT(req, &xug, sizeof xug); 811 } 812 free(unp_list, M_TEMP); 813 return error; 814 } 815 816 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 817 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 818 "List of active local datagram sockets"); 819 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 820 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 821 "List of active local stream sockets"); 822 823 static void 824 unp_shutdown(unp) 825 struct unpcb *unp; 826 { 827 struct socket *so; 828 829 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 830 (so = unp->unp_conn->unp_socket)) 831 socantrcvmore(so); 832 } 833 834 static void 835 unp_drop(unp, errno) 836 struct unpcb *unp; 837 int errno; 838 { 839 struct socket *so = unp->unp_socket; 840 841 so->so_error = errno; 842 unp_disconnect(unp); 843 if (so->so_head) { 844 LIST_REMOVE(unp, unp_link); 845 unp->unp_gencnt = ++unp_gencnt; 846 unp_count--; 847 so->so_pcb = (caddr_t) 0; 848 if (unp->unp_addr) 849 FREE(unp->unp_addr, M_SONAME); 850 zfree(unp_zone, unp); 851 sofree(so); 852 } 853 } 854 855 #ifdef notdef 856 void 857 unp_drain() 858 { 859 860 } 861 #endif 862 863 int 864 unp_externalize(rights) 865 struct mbuf *rights; 866 { 867 struct proc *p = curproc; /* XXX */ 868 register int i; 869 register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 870 register struct file **rp = (struct file **)(cm + 1); 871 register struct file *fp; 872 int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int); 873 int f; 874 875 /* 876 * if the new FD's will not fit, then we free them all 877 */ 878 if (!fdavail(p, newfds)) { 879 for (i = 0; i < newfds; i++) { 880 fp = *rp; 881 unp_discard(fp); 882 *rp++ = 0; 883 } 884 return (EMSGSIZE); 885 } 886 /* 887 * now change each pointer to an fd in the global table to 888 * an integer that is the index to the local fd table entry 889 * that we set up to point to the global one we are transferring. 890 * XXX this assumes a pointer and int are the same size...! 891 */ 892 for (i = 0; i < newfds; i++) { 893 if (fdalloc(p, 0, &f)) 894 panic("unp_externalize"); 895 fp = *rp; 896 p->p_fd->fd_ofiles[f] = fp; 897 fp->f_msgcount--; 898 unp_rights--; 899 *(int *)rp++ = f; 900 } 901 return (0); 902 } 903 904 void 905 unp_init(void) 906 { 907 unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0); 908 if (unp_zone == 0) 909 panic("unp_init"); 910 LIST_INIT(&unp_dhead); 911 LIST_INIT(&unp_shead); 912 } 913 914 #ifndef MIN 915 #define MIN(a,b) (((a)<(b))?(a):(b)) 916 #endif 917 918 static int 919 unp_internalize(control, p) 920 struct mbuf *control; 921 struct proc *p; 922 { 923 struct filedesc *fdp = p->p_fd; 924 register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 925 register struct file **rp; 926 register struct file *fp; 927 register int i, fd; 928 register struct cmsgcred *cmcred; 929 int oldfds; 930 931 if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) || 932 cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len) 933 return (EINVAL); 934 935 /* 936 * Fill in credential information. 937 */ 938 if (cm->cmsg_type == SCM_CREDS) { 939 cmcred = (struct cmsgcred *)(cm + 1); 940 cmcred->cmcred_pid = p->p_pid; 941 cmcred->cmcred_uid = p->p_cred->p_ruid; 942 cmcred->cmcred_gid = p->p_cred->p_rgid; 943 cmcred->cmcred_euid = p->p_ucred->cr_uid; 944 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups, 945 CMGROUP_MAX); 946 for (i = 0; i < cmcred->cmcred_ngroups; i++) 947 cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i]; 948 return(0); 949 } 950 951 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 952 /* 953 * check that all the FDs passed in refer to legal OPEN files 954 * If not, reject the entire operation. 955 */ 956 rp = (struct file **)(cm + 1); 957 for (i = 0; i < oldfds; i++) { 958 fd = *(int *)rp++; 959 if ((unsigned)fd >= fdp->fd_nfiles || 960 fdp->fd_ofiles[fd] == NULL) 961 return (EBADF); 962 } 963 /* 964 * Now replace the integer FDs with pointers to 965 * the associated global file table entry.. 966 * XXX this assumes a pointer and an int are the same size! 967 */ 968 rp = (struct file **)(cm + 1); 969 for (i = 0; i < oldfds; i++) { 970 fp = fdp->fd_ofiles[*(int *)rp]; 971 *rp++ = fp; 972 fp->f_count++; 973 fp->f_msgcount++; 974 unp_rights++; 975 } 976 return (0); 977 } 978 979 static int unp_defer, unp_gcing; 980 981 static void 982 unp_gc() 983 { 984 register struct file *fp, *nextfp; 985 register struct socket *so; 986 struct file **extra_ref, **fpp; 987 int nunref, i; 988 989 if (unp_gcing) 990 return; 991 unp_gcing = 1; 992 unp_defer = 0; 993 /* 994 * before going through all this, set all FDs to 995 * be NOT defered and NOT externally accessible 996 */ 997 for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) 998 fp->f_flag &= ~(FMARK|FDEFER); 999 do { 1000 for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) { 1001 /* 1002 * If the file is not open, skip it 1003 */ 1004 if (fp->f_count == 0) 1005 continue; 1006 /* 1007 * If we already marked it as 'defer' in a 1008 * previous pass, then try process it this time 1009 * and un-mark it 1010 */ 1011 if (fp->f_flag & FDEFER) { 1012 fp->f_flag &= ~FDEFER; 1013 unp_defer--; 1014 } else { 1015 /* 1016 * if it's not defered, then check if it's 1017 * already marked.. if so skip it 1018 */ 1019 if (fp->f_flag & FMARK) 1020 continue; 1021 /* 1022 * If all references are from messages 1023 * in transit, then skip it. it's not 1024 * externally accessible. 1025 */ 1026 if (fp->f_count == fp->f_msgcount) 1027 continue; 1028 /* 1029 * If it got this far then it must be 1030 * externally accessible. 1031 */ 1032 fp->f_flag |= FMARK; 1033 } 1034 /* 1035 * either it was defered, or it is externally 1036 * accessible and not already marked so. 1037 * Now check if it is possibly one of OUR sockets. 1038 */ 1039 if (fp->f_type != DTYPE_SOCKET || 1040 (so = (struct socket *)fp->f_data) == 0) 1041 continue; 1042 if (so->so_proto->pr_domain != &localdomain || 1043 (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1044 continue; 1045 #ifdef notdef 1046 if (so->so_rcv.sb_flags & SB_LOCK) { 1047 /* 1048 * This is problematical; it's not clear 1049 * we need to wait for the sockbuf to be 1050 * unlocked (on a uniprocessor, at least), 1051 * and it's also not clear what to do 1052 * if sbwait returns an error due to receipt 1053 * of a signal. If sbwait does return 1054 * an error, we'll go into an infinite 1055 * loop. Delete all of this for now. 1056 */ 1057 (void) sbwait(&so->so_rcv); 1058 goto restart; 1059 } 1060 #endif 1061 /* 1062 * So, Ok, it's one of our sockets and it IS externally 1063 * accessible (or was defered). Now we look 1064 * to see if we hold any file descriptors in its 1065 * message buffers. Follow those links and mark them 1066 * as accessible too. 1067 */ 1068 unp_scan(so->so_rcv.sb_mb, unp_mark); 1069 } 1070 } while (unp_defer); 1071 /* 1072 * We grab an extra reference to each of the file table entries 1073 * that are not otherwise accessible and then free the rights 1074 * that are stored in messages on them. 1075 * 1076 * The bug in the orginal code is a little tricky, so I'll describe 1077 * what's wrong with it here. 1078 * 1079 * It is incorrect to simply unp_discard each entry for f_msgcount 1080 * times -- consider the case of sockets A and B that contain 1081 * references to each other. On a last close of some other socket, 1082 * we trigger a gc since the number of outstanding rights (unp_rights) 1083 * is non-zero. If during the sweep phase the gc code un_discards, 1084 * we end up doing a (full) closef on the descriptor. A closef on A 1085 * results in the following chain. Closef calls soo_close, which 1086 * calls soclose. Soclose calls first (through the switch 1087 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1088 * returns because the previous instance had set unp_gcing, and 1089 * we return all the way back to soclose, which marks the socket 1090 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1091 * to free up the rights that are queued in messages on the socket A, 1092 * i.e., the reference on B. The sorflush calls via the dom_dispose 1093 * switch unp_dispose, which unp_scans with unp_discard. This second 1094 * instance of unp_discard just calls closef on B. 1095 * 1096 * Well, a similar chain occurs on B, resulting in a sorflush on B, 1097 * which results in another closef on A. Unfortunately, A is already 1098 * being closed, and the descriptor has already been marked with 1099 * SS_NOFDREF, and soclose panics at this point. 1100 * 1101 * Here, we first take an extra reference to each inaccessible 1102 * descriptor. Then, we call sorflush ourself, since we know 1103 * it is a Unix domain socket anyhow. After we destroy all the 1104 * rights carried in messages, we do a last closef to get rid 1105 * of our extra reference. This is the last close, and the 1106 * unp_detach etc will shut down the socket. 1107 * 1108 * 91/09/19, bsy@cs.cmu.edu 1109 */ 1110 extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK); 1111 for (nunref = 0, fp = filehead.lh_first, fpp = extra_ref; fp != 0; 1112 fp = nextfp) { 1113 nextfp = fp->f_list.le_next; 1114 /* 1115 * If it's not open, skip it 1116 */ 1117 if (fp->f_count == 0) 1118 continue; 1119 /* 1120 * If all refs are from msgs, and it's not marked accessible 1121 * then it must be referenced from some unreachable cycle 1122 * of (shut-down) FDs, so include it in our 1123 * list of FDs to remove 1124 */ 1125 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) { 1126 *fpp++ = fp; 1127 nunref++; 1128 fp->f_count++; 1129 } 1130 } 1131 /* 1132 * for each FD on our hit list, do the following two things 1133 */ 1134 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 1135 struct file *tfp = *fpp; 1136 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) 1137 sorflush((struct socket *)(tfp->f_data)); 1138 } 1139 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1140 closef(*fpp, (struct proc *) NULL); 1141 free((caddr_t)extra_ref, M_FILE); 1142 unp_gcing = 0; 1143 } 1144 1145 void 1146 unp_dispose(m) 1147 struct mbuf *m; 1148 { 1149 1150 if (m) 1151 unp_scan(m, unp_discard); 1152 } 1153 1154 static void 1155 unp_scan(m0, op) 1156 register struct mbuf *m0; 1157 void (*op) __P((struct file *)); 1158 { 1159 register struct mbuf *m; 1160 register struct file **rp; 1161 register struct cmsghdr *cm; 1162 register int i; 1163 int qfds; 1164 1165 while (m0) { 1166 for (m = m0; m; m = m->m_next) 1167 if (m->m_type == MT_CONTROL && 1168 m->m_len >= sizeof(*cm)) { 1169 cm = mtod(m, struct cmsghdr *); 1170 if (cm->cmsg_level != SOL_SOCKET || 1171 cm->cmsg_type != SCM_RIGHTS) 1172 continue; 1173 qfds = (cm->cmsg_len - sizeof *cm) 1174 / sizeof (struct file *); 1175 rp = (struct file **)(cm + 1); 1176 for (i = 0; i < qfds; i++) 1177 (*op)(*rp++); 1178 break; /* XXX, but saves time */ 1179 } 1180 m0 = m0->m_act; 1181 } 1182 } 1183 1184 static void 1185 unp_mark(fp) 1186 struct file *fp; 1187 { 1188 1189 if (fp->f_flag & FMARK) 1190 return; 1191 unp_defer++; 1192 fp->f_flag |= (FMARK|FDEFER); 1193 } 1194 1195 static void 1196 unp_discard(fp) 1197 struct file *fp; 1198 { 1199 1200 fp->f_msgcount--; 1201 unp_rights--; 1202 (void) closef(fp, (struct proc *)NULL); 1203 } 1204