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