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.9 2003/09/29 18:52:06 dillon 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 (struct socket *); 81 static void unp_detach (struct unpcb *); 82 static int unp_bind (struct unpcb *,struct sockaddr *, struct thread *); 83 static int unp_connect (struct socket *,struct sockaddr *, 84 struct thread *); 85 static void unp_disconnect (struct unpcb *); 86 static void unp_shutdown (struct unpcb *); 87 static void unp_drop (struct unpcb *, int); 88 static void unp_gc (void); 89 static void unp_scan (struct mbuf *, void (*)(struct file *)); 90 static void unp_mark (struct file *); 91 static void unp_discard (struct file *); 92 static int unp_internalize (struct mbuf *, struct thread *); 93 static int unp_listen (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, NAMEI_CREATE, CNP_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, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, 655 UIO_SYSSPACE, buf, td); 656 error = namei(&nd); 657 if (error) 658 return (error); 659 vp = nd.ni_vp; 660 NDFREE(&nd, NDF_ONLY_PNBUF); 661 if (vp->v_type != VSOCK) { 662 error = ENOTSOCK; 663 goto bad; 664 } 665 error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td); 666 if (error) 667 goto bad; 668 so2 = vp->v_socket; 669 if (so2 == 0) { 670 error = ECONNREFUSED; 671 goto bad; 672 } 673 if (so->so_type != so2->so_type) { 674 error = EPROTOTYPE; 675 goto bad; 676 } 677 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 678 if ((so2->so_options & SO_ACCEPTCONN) == 0 || 679 (so3 = sonewconn(so2, 0)) == 0) { 680 error = ECONNREFUSED; 681 goto bad; 682 } 683 unp = sotounpcb(so); 684 unp2 = sotounpcb(so2); 685 unp3 = sotounpcb(so3); 686 if (unp2->unp_addr) 687 unp3->unp_addr = (struct sockaddr_un *) 688 dup_sockaddr((struct sockaddr *) 689 unp2->unp_addr, 1); 690 691 /* 692 * unp_peercred management: 693 * 694 * The connecter's (client's) credentials are copied 695 * from its process structure at the time of connect() 696 * (which is now). 697 */ 698 cru2x(p->p_ucred, &unp3->unp_peercred); 699 unp3->unp_flags |= UNP_HAVEPC; 700 /* 701 * The receiver's (server's) credentials are copied 702 * from the unp_peercred member of socket on which the 703 * former called listen(); unp_listen() cached that 704 * process's credentials at that time so we can use 705 * them now. 706 */ 707 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 708 ("unp_connect: listener without cached peercred")); 709 memcpy(&unp->unp_peercred, &unp2->unp_peercred, 710 sizeof(unp->unp_peercred)); 711 unp->unp_flags |= UNP_HAVEPC; 712 713 so2 = so3; 714 } 715 error = unp_connect2(so, so2); 716 bad: 717 vput(vp); 718 return (error); 719 } 720 721 int 722 unp_connect2(so, so2) 723 struct socket *so; 724 struct socket *so2; 725 { 726 struct unpcb *unp = sotounpcb(so); 727 struct unpcb *unp2; 728 729 if (so2->so_type != so->so_type) 730 return (EPROTOTYPE); 731 unp2 = sotounpcb(so2); 732 unp->unp_conn = unp2; 733 switch (so->so_type) { 734 735 case SOCK_DGRAM: 736 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 737 soisconnected(so); 738 break; 739 740 case SOCK_STREAM: 741 unp2->unp_conn = unp; 742 soisconnected(so); 743 soisconnected(so2); 744 break; 745 746 default: 747 panic("unp_connect2"); 748 } 749 return (0); 750 } 751 752 static void 753 unp_disconnect(unp) 754 struct unpcb *unp; 755 { 756 struct unpcb *unp2 = unp->unp_conn; 757 758 if (unp2 == 0) 759 return; 760 unp->unp_conn = 0; 761 switch (unp->unp_socket->so_type) { 762 763 case SOCK_DGRAM: 764 LIST_REMOVE(unp, unp_reflink); 765 unp->unp_socket->so_state &= ~SS_ISCONNECTED; 766 break; 767 768 case SOCK_STREAM: 769 soisdisconnected(unp->unp_socket); 770 unp2->unp_conn = 0; 771 soisdisconnected(unp2->unp_socket); 772 break; 773 } 774 } 775 776 #ifdef notdef 777 void 778 unp_abort(unp) 779 struct unpcb *unp; 780 { 781 782 unp_detach(unp); 783 } 784 #endif 785 786 static int 787 prison_unpcb(struct thread *td, struct unpcb *unp) 788 { 789 struct proc *p; 790 791 if (td == NULL) 792 return (0); 793 if ((p = td->td_proc) == NULL) 794 return (0); 795 if (!p->p_ucred->cr_prison) 796 return (0); 797 if (p->p_fd->fd_rdir == unp->unp_rvnode) 798 return (0); 799 return (1); 800 } 801 802 static int 803 unp_pcblist(SYSCTL_HANDLER_ARGS) 804 { 805 int error, i, n; 806 struct unpcb *unp, **unp_list; 807 unp_gen_t gencnt; 808 struct xunpgen xug; 809 struct unp_head *head; 810 811 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 812 813 KKASSERT(curproc != NULL); 814 815 /* 816 * The process of preparing the PCB list is too time-consuming and 817 * resource-intensive to repeat twice on every request. 818 */ 819 if (req->oldptr == 0) { 820 n = unp_count; 821 req->oldidx = 2 * (sizeof xug) 822 + (n + n/8) * sizeof(struct xunpcb); 823 return 0; 824 } 825 826 if (req->newptr != 0) 827 return EPERM; 828 829 /* 830 * OK, now we're committed to doing something. 831 */ 832 gencnt = unp_gencnt; 833 n = unp_count; 834 835 xug.xug_len = sizeof xug; 836 xug.xug_count = n; 837 xug.xug_gen = gencnt; 838 xug.xug_sogen = so_gencnt; 839 error = SYSCTL_OUT(req, &xug, sizeof xug); 840 if (error) 841 return error; 842 843 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 844 if (unp_list == 0) 845 return ENOMEM; 846 847 for (unp = LIST_FIRST(head), i = 0; unp && i < n; 848 unp = LIST_NEXT(unp, unp_link)) { 849 if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp)) 850 unp_list[i++] = unp; 851 } 852 n = i; /* in case we lost some during malloc */ 853 854 error = 0; 855 for (i = 0; i < n; i++) { 856 unp = unp_list[i]; 857 if (unp->unp_gencnt <= gencnt) { 858 struct xunpcb xu; 859 xu.xu_len = sizeof xu; 860 xu.xu_unpp = unp; 861 /* 862 * XXX - need more locking here to protect against 863 * connect/disconnect races for SMP. 864 */ 865 if (unp->unp_addr) 866 bcopy(unp->unp_addr, &xu.xu_addr, 867 unp->unp_addr->sun_len); 868 if (unp->unp_conn && unp->unp_conn->unp_addr) 869 bcopy(unp->unp_conn->unp_addr, 870 &xu.xu_caddr, 871 unp->unp_conn->unp_addr->sun_len); 872 bcopy(unp, &xu.xu_unp, sizeof *unp); 873 sotoxsocket(unp->unp_socket, &xu.xu_socket); 874 error = SYSCTL_OUT(req, &xu, sizeof xu); 875 } 876 } 877 if (!error) { 878 /* 879 * Give the user an updated idea of our state. 880 * If the generation differs from what we told 881 * her before, she knows that something happened 882 * while we were processing this request, and it 883 * might be necessary to retry. 884 */ 885 xug.xug_gen = unp_gencnt; 886 xug.xug_sogen = so_gencnt; 887 xug.xug_count = unp_count; 888 error = SYSCTL_OUT(req, &xug, sizeof xug); 889 } 890 free(unp_list, M_TEMP); 891 return error; 892 } 893 894 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 895 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 896 "List of active local datagram sockets"); 897 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 898 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 899 "List of active local stream sockets"); 900 901 static void 902 unp_shutdown(unp) 903 struct unpcb *unp; 904 { 905 struct socket *so; 906 907 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 908 (so = unp->unp_conn->unp_socket)) 909 socantrcvmore(so); 910 } 911 912 static void 913 unp_drop(unp, errno) 914 struct unpcb *unp; 915 int errno; 916 { 917 struct socket *so = unp->unp_socket; 918 919 so->so_error = errno; 920 unp_disconnect(unp); 921 } 922 923 #ifdef notdef 924 void 925 unp_drain() 926 { 927 928 } 929 #endif 930 931 int 932 unp_externalize(struct mbuf *rights) 933 { 934 struct proc *p = curproc; /* XXX */ 935 int i; 936 struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 937 int *fdp; 938 struct file **rp; 939 struct file *fp; 940 int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm)) 941 / sizeof (struct file *); 942 int f; 943 944 /* 945 * if the new FD's will not fit, then we free them all 946 */ 947 if (!fdavail(p, newfds)) { 948 rp = (struct file **)CMSG_DATA(cm); 949 for (i = 0; i < newfds; i++) { 950 fp = *rp; 951 /* 952 * zero the pointer before calling unp_discard, 953 * since it may end up in unp_gc().. 954 */ 955 *rp++ = 0; 956 unp_discard(fp); 957 } 958 return (EMSGSIZE); 959 } 960 /* 961 * now change each pointer to an fd in the global table to 962 * an integer that is the index to the local fd table entry 963 * that we set up to point to the global one we are transferring. 964 * If sizeof (struct file *) is bigger than or equal to sizeof int, 965 * then do it in forward order. In that case, an integer will 966 * always come in the same place or before its corresponding 967 * struct file pointer. 968 * If sizeof (struct file *) is smaller than sizeof int, then 969 * do it in reverse order. 970 */ 971 if (sizeof (struct file *) >= sizeof (int)) { 972 fdp = (int *)(cm + 1); 973 rp = (struct file **)CMSG_DATA(cm); 974 for (i = 0; i < newfds; i++) { 975 if (fdalloc(p, 0, &f)) 976 panic("unp_externalize"); 977 fp = *rp++; 978 p->p_fd->fd_ofiles[f] = fp; 979 fp->f_msgcount--; 980 unp_rights--; 981 *fdp++ = f; 982 } 983 } else { 984 fdp = (int *)(cm + 1) + newfds - 1; 985 rp = (struct file **)CMSG_DATA(cm) + newfds - 1; 986 for (i = 0; i < newfds; i++) { 987 if (fdalloc(p, 0, &f)) 988 panic("unp_externalize"); 989 fp = *rp--; 990 p->p_fd->fd_ofiles[f] = fp; 991 fp->f_msgcount--; 992 unp_rights--; 993 *fdp-- = f; 994 } 995 } 996 997 /* 998 * Adjust length, in case sizeof(struct file *) and sizeof(int) 999 * differs. 1000 */ 1001 cm->cmsg_len = CMSG_LEN(newfds * sizeof(int)); 1002 rights->m_len = cm->cmsg_len; 1003 return (0); 1004 } 1005 1006 void 1007 unp_init(void) 1008 { 1009 unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0); 1010 if (unp_zone == 0) 1011 panic("unp_init"); 1012 LIST_INIT(&unp_dhead); 1013 LIST_INIT(&unp_shead); 1014 } 1015 1016 static int 1017 unp_internalize(struct mbuf *control, struct thread *td) 1018 { 1019 struct proc *p = td->td_proc; 1020 struct filedesc *fdescp; 1021 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1022 struct file **rp; 1023 struct file *fp; 1024 int i, fd, *fdp; 1025 struct cmsgcred *cmcred; 1026 int oldfds; 1027 u_int newlen; 1028 1029 KKASSERT(p); 1030 fdescp = p->p_fd; 1031 if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) || 1032 cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len) 1033 return (EINVAL); 1034 1035 /* 1036 * Fill in credential information. 1037 */ 1038 if (cm->cmsg_type == SCM_CREDS) { 1039 cmcred = (struct cmsgcred *)(cm + 1); 1040 cmcred->cmcred_pid = p->p_pid; 1041 cmcred->cmcred_uid = p->p_ucred->cr_ruid; 1042 cmcred->cmcred_gid = p->p_ucred->cr_rgid; 1043 cmcred->cmcred_euid = p->p_ucred->cr_uid; 1044 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups, 1045 CMGROUP_MAX); 1046 for (i = 0; i < cmcred->cmcred_ngroups; i++) 1047 cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i]; 1048 return(0); 1049 } 1050 1051 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 1052 /* 1053 * check that all the FDs passed in refer to legal OPEN files 1054 * If not, reject the entire operation. 1055 */ 1056 fdp = (int *)(cm + 1); 1057 for (i = 0; i < oldfds; i++) { 1058 fd = *fdp++; 1059 if ((unsigned)fd >= fdescp->fd_nfiles || 1060 fdescp->fd_ofiles[fd] == NULL) 1061 return (EBADF); 1062 if (fdescp->fd_ofiles[fd]->f_type == DTYPE_KQUEUE) 1063 return (EOPNOTSUPP); 1064 } 1065 /* 1066 * Now replace the integer FDs with pointers to 1067 * the associated global file table entry.. 1068 * Allocate a bigger buffer as necessary. But if an cluster is not 1069 * enough, return E2BIG. 1070 */ 1071 newlen = CMSG_LEN(oldfds * sizeof(struct file *)); 1072 if (newlen > MCLBYTES) 1073 return (E2BIG); 1074 if (newlen - control->m_len > M_TRAILINGSPACE(control)) { 1075 if (control->m_flags & M_EXT) 1076 return (E2BIG); 1077 MCLGET(control, M_WAIT); 1078 if ((control->m_flags & M_EXT) == 0) 1079 return (ENOBUFS); 1080 1081 /* copy the data to the cluster */ 1082 memcpy(mtod(control, char *), cm, cm->cmsg_len); 1083 cm = mtod(control, struct cmsghdr *); 1084 } 1085 1086 /* 1087 * Adjust length, in case sizeof(struct file *) and sizeof(int) 1088 * differs. 1089 */ 1090 control->m_len = cm->cmsg_len = newlen; 1091 1092 /* 1093 * Transform the file descriptors into struct file pointers. 1094 * If sizeof (struct file *) is bigger than or equal to sizeof int, 1095 * then do it in reverse order so that the int won't get until 1096 * we're done. 1097 * If sizeof (struct file *) is smaller than sizeof int, then 1098 * do it in forward order. 1099 */ 1100 if (sizeof (struct file *) >= sizeof (int)) { 1101 fdp = (int *)(cm + 1) + oldfds - 1; 1102 rp = (struct file **)CMSG_DATA(cm) + oldfds - 1; 1103 for (i = 0; i < oldfds; i++) { 1104 fp = fdescp->fd_ofiles[*fdp--]; 1105 *rp-- = fp; 1106 fp->f_count++; 1107 fp->f_msgcount++; 1108 unp_rights++; 1109 } 1110 } else { 1111 fdp = (int *)(cm + 1); 1112 rp = (struct file **)CMSG_DATA(cm); 1113 for (i = 0; i < oldfds; i++) { 1114 fp = fdescp->fd_ofiles[*fdp++]; 1115 *rp++ = fp; 1116 fp->f_count++; 1117 fp->f_msgcount++; 1118 unp_rights++; 1119 } 1120 } 1121 return (0); 1122 } 1123 1124 static int unp_defer, unp_gcing; 1125 1126 static void 1127 unp_gc() 1128 { 1129 struct file *fp, *nextfp; 1130 struct socket *so; 1131 struct file **extra_ref, **fpp; 1132 int nunref, i; 1133 1134 if (unp_gcing) 1135 return; 1136 unp_gcing = 1; 1137 unp_defer = 0; 1138 /* 1139 * before going through all this, set all FDs to 1140 * be NOT defered and NOT externally accessible 1141 */ 1142 LIST_FOREACH(fp, &filehead, f_list) 1143 fp->f_flag &= ~(FMARK|FDEFER); 1144 do { 1145 LIST_FOREACH(fp, &filehead, f_list) { 1146 /* 1147 * If the file is not open, skip it 1148 */ 1149 if (fp->f_count == 0) 1150 continue; 1151 /* 1152 * If we already marked it as 'defer' in a 1153 * previous pass, then try process it this time 1154 * and un-mark it 1155 */ 1156 if (fp->f_flag & FDEFER) { 1157 fp->f_flag &= ~FDEFER; 1158 unp_defer--; 1159 } else { 1160 /* 1161 * if it's not defered, then check if it's 1162 * already marked.. if so skip it 1163 */ 1164 if (fp->f_flag & FMARK) 1165 continue; 1166 /* 1167 * If all references are from messages 1168 * in transit, then skip it. it's not 1169 * externally accessible. 1170 */ 1171 if (fp->f_count == fp->f_msgcount) 1172 continue; 1173 /* 1174 * If it got this far then it must be 1175 * externally accessible. 1176 */ 1177 fp->f_flag |= FMARK; 1178 } 1179 /* 1180 * either it was defered, or it is externally 1181 * accessible and not already marked so. 1182 * Now check if it is possibly one of OUR sockets. 1183 */ 1184 if (fp->f_type != DTYPE_SOCKET || 1185 (so = (struct socket *)fp->f_data) == 0) 1186 continue; 1187 if (so->so_proto->pr_domain != &localdomain || 1188 (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1189 continue; 1190 #ifdef notdef 1191 if (so->so_rcv.sb_flags & SB_LOCK) { 1192 /* 1193 * This is problematical; it's not clear 1194 * we need to wait for the sockbuf to be 1195 * unlocked (on a uniprocessor, at least), 1196 * and it's also not clear what to do 1197 * if sbwait returns an error due to receipt 1198 * of a signal. If sbwait does return 1199 * an error, we'll go into an infinite 1200 * loop. Delete all of this for now. 1201 */ 1202 (void) sbwait(&so->so_rcv); 1203 goto restart; 1204 } 1205 #endif 1206 /* 1207 * So, Ok, it's one of our sockets and it IS externally 1208 * accessible (or was defered). Now we look 1209 * to see if we hold any file descriptors in its 1210 * message buffers. Follow those links and mark them 1211 * as accessible too. 1212 */ 1213 unp_scan(so->so_rcv.sb_mb, unp_mark); 1214 } 1215 } while (unp_defer); 1216 /* 1217 * We grab an extra reference to each of the file table entries 1218 * that are not otherwise accessible and then free the rights 1219 * that are stored in messages on them. 1220 * 1221 * The bug in the orginal code is a little tricky, so I'll describe 1222 * what's wrong with it here. 1223 * 1224 * It is incorrect to simply unp_discard each entry for f_msgcount 1225 * times -- consider the case of sockets A and B that contain 1226 * references to each other. On a last close of some other socket, 1227 * we trigger a gc since the number of outstanding rights (unp_rights) 1228 * is non-zero. If during the sweep phase the gc code un_discards, 1229 * we end up doing a (full) closef on the descriptor. A closef on A 1230 * results in the following chain. Closef calls soo_close, which 1231 * calls soclose. Soclose calls first (through the switch 1232 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1233 * returns because the previous instance had set unp_gcing, and 1234 * we return all the way back to soclose, which marks the socket 1235 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1236 * to free up the rights that are queued in messages on the socket A, 1237 * i.e., the reference on B. The sorflush calls via the dom_dispose 1238 * switch unp_dispose, which unp_scans with unp_discard. This second 1239 * instance of unp_discard just calls closef on B. 1240 * 1241 * Well, a similar chain occurs on B, resulting in a sorflush on B, 1242 * which results in another closef on A. Unfortunately, A is already 1243 * being closed, and the descriptor has already been marked with 1244 * SS_NOFDREF, and soclose panics at this point. 1245 * 1246 * Here, we first take an extra reference to each inaccessible 1247 * descriptor. Then, we call sorflush ourself, since we know 1248 * it is a Unix domain socket anyhow. After we destroy all the 1249 * rights carried in messages, we do a last closef to get rid 1250 * of our extra reference. This is the last close, and the 1251 * unp_detach etc will shut down the socket. 1252 * 1253 * 91/09/19, bsy@cs.cmu.edu 1254 */ 1255 extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK); 1256 for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0; 1257 fp = nextfp) { 1258 nextfp = LIST_NEXT(fp, f_list); 1259 /* 1260 * If it's not open, skip it 1261 */ 1262 if (fp->f_count == 0) 1263 continue; 1264 /* 1265 * If all refs are from msgs, and it's not marked accessible 1266 * then it must be referenced from some unreachable cycle 1267 * of (shut-down) FDs, so include it in our 1268 * list of FDs to remove 1269 */ 1270 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) { 1271 *fpp++ = fp; 1272 nunref++; 1273 fp->f_count++; 1274 } 1275 } 1276 /* 1277 * for each FD on our hit list, do the following two things 1278 */ 1279 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 1280 struct file *tfp = *fpp; 1281 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) 1282 sorflush((struct socket *)(tfp->f_data)); 1283 } 1284 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1285 closef(*fpp, NULL); 1286 free((caddr_t)extra_ref, M_FILE); 1287 unp_gcing = 0; 1288 } 1289 1290 void 1291 unp_dispose(struct mbuf *m) 1292 { 1293 if (m) 1294 unp_scan(m, unp_discard); 1295 } 1296 1297 static int 1298 unp_listen(struct unpcb *unp, struct thread *td) 1299 { 1300 struct proc *p = td->td_proc; 1301 1302 KKASSERT(p); 1303 cru2x(p->p_ucred, &unp->unp_peercred); 1304 unp->unp_flags |= UNP_HAVEPCCACHED; 1305 return (0); 1306 } 1307 1308 static void 1309 unp_scan(m0, op) 1310 struct mbuf *m0; 1311 void (*op) (struct file *); 1312 { 1313 struct mbuf *m; 1314 struct file **rp; 1315 struct cmsghdr *cm; 1316 int i; 1317 int qfds; 1318 1319 while (m0) { 1320 for (m = m0; m; m = m->m_next) 1321 if (m->m_type == MT_CONTROL && 1322 m->m_len >= sizeof(*cm)) { 1323 cm = mtod(m, struct cmsghdr *); 1324 if (cm->cmsg_level != SOL_SOCKET || 1325 cm->cmsg_type != SCM_RIGHTS) 1326 continue; 1327 qfds = (cm->cmsg_len - 1328 (CMSG_DATA(cm) - (u_char *)cm)) 1329 / sizeof (struct file *); 1330 rp = (struct file **)CMSG_DATA(cm); 1331 for (i = 0; i < qfds; i++) 1332 (*op)(*rp++); 1333 break; /* XXX, but saves time */ 1334 } 1335 m0 = m0->m_act; 1336 } 1337 } 1338 1339 static void 1340 unp_mark(fp) 1341 struct file *fp; 1342 { 1343 1344 if (fp->f_flag & FMARK) 1345 return; 1346 unp_defer++; 1347 fp->f_flag |= (FMARK|FDEFER); 1348 } 1349 1350 static void 1351 unp_discard(fp) 1352 struct file *fp; 1353 { 1354 1355 fp->f_msgcount--; 1356 unp_rights--; 1357 (void) closef(fp, NULL); 1358 } 1359