1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)uipc_usrreq.c 7.34 (Berkeley) 05/14/92 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "proc.h" 13 #include "filedesc.h" 14 #include "domain.h" 15 #include "protosw.h" 16 #include "socket.h" 17 #include "socketvar.h" 18 #include "unpcb.h" 19 #include "un.h" 20 #include "namei.h" 21 #include "vnode.h" 22 #include "file.h" 23 #include "stat.h" 24 #include "mbuf.h" 25 26 /* 27 * Unix communications domain. 28 * 29 * TODO: 30 * SEQPACKET, RDM 31 * rethink name space problems 32 * need a proper out-of-band 33 */ 34 struct sockaddr sun_noname = { sizeof(sun_noname), AF_UNIX }; 35 ino_t unp_ino; /* prototype for fake inode numbers */ 36 37 /*ARGSUSED*/ 38 uipc_usrreq(so, req, m, nam, control) 39 struct socket *so; 40 int req; 41 struct mbuf *m, *nam, *control; 42 { 43 struct unpcb *unp = sotounpcb(so); 44 register struct socket *so2; 45 register int error = 0; 46 struct proc *p = curproc; /* XXX */ 47 48 if (req == PRU_CONTROL) 49 return (EOPNOTSUPP); 50 if (req != PRU_SEND && control && control->m_len) { 51 error = EOPNOTSUPP; 52 goto release; 53 } 54 if (unp == 0 && req != PRU_ATTACH) { 55 error = EINVAL; 56 goto release; 57 } 58 switch (req) { 59 60 case PRU_ATTACH: 61 if (unp) { 62 error = EISCONN; 63 break; 64 } 65 error = unp_attach(so); 66 break; 67 68 case PRU_DETACH: 69 unp_detach(unp); 70 break; 71 72 case PRU_BIND: 73 error = unp_bind(unp, nam, p); 74 break; 75 76 case PRU_LISTEN: 77 if (unp->unp_vnode == 0) 78 error = EINVAL; 79 break; 80 81 case PRU_CONNECT: 82 error = unp_connect(so, nam, p); 83 break; 84 85 case PRU_CONNECT2: 86 error = unp_connect2(so, (struct socket *)nam); 87 break; 88 89 case PRU_DISCONNECT: 90 unp_disconnect(unp); 91 break; 92 93 case PRU_ACCEPT: 94 /* 95 * Pass back name of connected socket, 96 * if it was bound and we are still connected 97 * (our peer may have closed already!). 98 */ 99 if (unp->unp_conn && unp->unp_conn->unp_addr) { 100 nam->m_len = unp->unp_conn->unp_addr->m_len; 101 bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 102 mtod(nam, caddr_t), (unsigned)nam->m_len); 103 } else { 104 nam->m_len = sizeof(sun_noname); 105 *(mtod(nam, struct sockaddr *)) = sun_noname; 106 } 107 break; 108 109 case PRU_SHUTDOWN: 110 socantsendmore(so); 111 unp_shutdown(unp); 112 break; 113 114 case PRU_RCVD: 115 switch (so->so_type) { 116 117 case SOCK_DGRAM: 118 panic("uipc 1"); 119 /*NOTREACHED*/ 120 121 case SOCK_STREAM: 122 #define rcv (&so->so_rcv) 123 #define snd (&so2->so_snd) 124 if (unp->unp_conn == 0) 125 break; 126 so2 = unp->unp_conn->unp_socket; 127 /* 128 * Adjust backpressure on sender 129 * and wakeup any waiting to write. 130 */ 131 snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; 132 unp->unp_mbcnt = rcv->sb_mbcnt; 133 snd->sb_hiwat += unp->unp_cc - rcv->sb_cc; 134 unp->unp_cc = rcv->sb_cc; 135 sowwakeup(so2); 136 #undef snd 137 #undef rcv 138 break; 139 140 default: 141 panic("uipc 2"); 142 } 143 break; 144 145 case PRU_SEND: 146 if (control && (error = unp_internalize(control, p))) 147 break; 148 switch (so->so_type) { 149 150 case SOCK_DGRAM: { 151 struct sockaddr *from; 152 153 if (nam) { 154 if (unp->unp_conn) { 155 error = EISCONN; 156 break; 157 } 158 error = unp_connect(so, nam, p); 159 if (error) 160 break; 161 } else { 162 if (unp->unp_conn == 0) { 163 error = ENOTCONN; 164 break; 165 } 166 } 167 so2 = unp->unp_conn->unp_socket; 168 if (unp->unp_addr) 169 from = mtod(unp->unp_addr, struct sockaddr *); 170 else 171 from = &sun_noname; 172 if (sbappendaddr(&so2->so_rcv, from, m, control)) { 173 sorwakeup(so2); 174 m = 0; 175 control = 0; 176 } else 177 error = ENOBUFS; 178 if (nam) 179 unp_disconnect(unp); 180 break; 181 } 182 183 case SOCK_STREAM: 184 #define rcv (&so2->so_rcv) 185 #define snd (&so->so_snd) 186 if (so->so_state & SS_CANTSENDMORE) { 187 error = EPIPE; 188 break; 189 } 190 if (unp->unp_conn == 0) 191 panic("uipc 3"); 192 so2 = unp->unp_conn->unp_socket; 193 /* 194 * Send to paired receive port, and then reduce 195 * send buffer hiwater marks to maintain backpressure. 196 * Wake up readers. 197 */ 198 if (control) { 199 if (sbappendcontrol(rcv, m, control)) 200 control = 0; 201 } else 202 sbappend(rcv, m); 203 snd->sb_mbmax -= 204 rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; 205 unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; 206 snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc; 207 unp->unp_conn->unp_cc = rcv->sb_cc; 208 sorwakeup(so2); 209 m = 0; 210 #undef snd 211 #undef rcv 212 break; 213 214 default: 215 panic("uipc 4"); 216 } 217 break; 218 219 case PRU_ABORT: 220 unp_drop(unp, ECONNABORTED); 221 break; 222 223 case PRU_SENSE: 224 ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 225 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 226 so2 = unp->unp_conn->unp_socket; 227 ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc; 228 } 229 ((struct stat *) m)->st_dev = NODEV; 230 if (unp->unp_ino == 0) 231 unp->unp_ino = unp_ino++; 232 ((struct stat *) m)->st_ino = unp->unp_ino; 233 return (0); 234 235 case PRU_RCVOOB: 236 return (EOPNOTSUPP); 237 238 case PRU_SENDOOB: 239 error = EOPNOTSUPP; 240 break; 241 242 case PRU_SOCKADDR: 243 if (unp->unp_addr) { 244 nam->m_len = unp->unp_addr->m_len; 245 bcopy(mtod(unp->unp_addr, caddr_t), 246 mtod(nam, caddr_t), (unsigned)nam->m_len); 247 } else 248 nam->m_len = 0; 249 break; 250 251 case PRU_PEERADDR: 252 if (unp->unp_conn && unp->unp_conn->unp_addr) { 253 nam->m_len = unp->unp_conn->unp_addr->m_len; 254 bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 255 mtod(nam, caddr_t), (unsigned)nam->m_len); 256 } else 257 nam->m_len = 0; 258 break; 259 260 case PRU_SLOWTIMO: 261 break; 262 263 default: 264 panic("piusrreq"); 265 } 266 release: 267 if (control) 268 m_freem(control); 269 if (m) 270 m_freem(m); 271 return (error); 272 } 273 274 /* 275 * Both send and receive buffers are allocated PIPSIZ bytes of buffering 276 * for stream sockets, although the total for sender and receiver is 277 * actually only PIPSIZ. 278 * Datagram sockets really use the sendspace as the maximum datagram size, 279 * and don't really want to reserve the sendspace. Their recvspace should 280 * be large enough for at least one max-size datagram plus address. 281 */ 282 #define PIPSIZ 4096 283 u_long unpst_sendspace = PIPSIZ; 284 u_long unpst_recvspace = PIPSIZ; 285 u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 286 u_long unpdg_recvspace = 4*1024; 287 288 int unp_rights; /* file descriptors in flight */ 289 290 unp_attach(so) 291 struct socket *so; 292 { 293 register struct mbuf *m; 294 register struct unpcb *unp; 295 int error; 296 297 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 298 switch (so->so_type) { 299 300 case SOCK_STREAM: 301 error = soreserve(so, unpst_sendspace, unpst_recvspace); 302 break; 303 304 case SOCK_DGRAM: 305 error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 306 break; 307 308 default: 309 panic("unp_attach"); 310 } 311 if (error) 312 return (error); 313 } 314 m = m_getclr(M_DONTWAIT, MT_PCB); 315 if (m == NULL) 316 return (ENOBUFS); 317 unp = mtod(m, struct unpcb *); 318 so->so_pcb = (caddr_t)unp; 319 unp->unp_socket = so; 320 return (0); 321 } 322 323 unp_detach(unp) 324 register struct unpcb *unp; 325 { 326 327 if (unp->unp_vnode) { 328 unp->unp_vnode->v_socket = 0; 329 vrele(unp->unp_vnode); 330 unp->unp_vnode = 0; 331 } 332 if (unp->unp_conn) 333 unp_disconnect(unp); 334 while (unp->unp_refs) 335 unp_drop(unp->unp_refs, ECONNRESET); 336 soisdisconnected(unp->unp_socket); 337 unp->unp_socket->so_pcb = 0; 338 m_freem(unp->unp_addr); 339 (void) m_free(dtom(unp)); 340 if (unp_rights) 341 unp_gc(); 342 } 343 344 unp_bind(unp, nam, p) 345 struct unpcb *unp; 346 struct mbuf *nam; 347 struct proc *p; 348 { 349 USES_VOP_ABORTOP; 350 USES_VOP_CREATE; 351 USES_VOP_UNLOCK; 352 struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 353 register struct vnode *vp; 354 struct vattr vattr; 355 int error; 356 struct nameidata nd; 357 358 NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE, 359 soun->sun_path, p); 360 if (unp->unp_vnode != NULL) 361 return (EINVAL); 362 if (nam->m_len == MLEN) { 363 if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 364 return (EINVAL); 365 } else 366 *(mtod(nam, caddr_t) + nam->m_len) = 0; 367 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 368 if (error = namei(&nd)) 369 return (error); 370 vp = nd.ni_vp; 371 if (vp != NULL) { 372 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 373 if (nd.ni_dvp == vp) 374 vrele(nd.ni_dvp); 375 else 376 vput(nd.ni_dvp); 377 vrele(vp); 378 return (EADDRINUSE); 379 } 380 VATTR_NULL(&vattr); 381 vattr.va_type = VSOCK; 382 vattr.va_mode = 0777; 383 LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 384 if (error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr)) 385 return (error); 386 vp = nd.ni_vp; 387 vp->v_socket = unp->unp_socket; 388 unp->unp_vnode = vp; 389 unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL); 390 VOP_UNLOCK(vp); 391 return (0); 392 } 393 394 unp_connect(so, nam, p) 395 struct socket *so; 396 struct mbuf *nam; 397 struct proc *p; 398 { 399 USES_VOP_ACCESS; 400 register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 401 register struct vnode *vp; 402 register struct socket *so2, *so3; 403 struct unpcb *unp2, *unp3; 404 int error; 405 struct nameidata nd; 406 407 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, soun->sun_path, p); 408 if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) { /* XXX */ 409 if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 410 return (EMSGSIZE); 411 } else 412 *(mtod(nam, caddr_t) + nam->m_len) = 0; 413 if (error = namei(&nd)) 414 return (error); 415 vp = nd.ni_vp; 416 if (vp->v_type != VSOCK) { 417 error = ENOTSOCK; 418 goto bad; 419 } 420 if (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) 421 goto bad; 422 so2 = vp->v_socket; 423 if (so2 == 0) { 424 error = ECONNREFUSED; 425 goto bad; 426 } 427 if (so->so_type != so2->so_type) { 428 error = EPROTOTYPE; 429 goto bad; 430 } 431 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 432 if ((so2->so_options & SO_ACCEPTCONN) == 0 || 433 (so3 = sonewconn(so2, 0)) == 0) { 434 error = ECONNREFUSED; 435 goto bad; 436 } 437 unp2 = sotounpcb(so2); 438 unp3 = sotounpcb(so3); 439 if (unp2->unp_addr) 440 unp3->unp_addr = 441 m_copy(unp2->unp_addr, 0, (int)M_COPYALL); 442 so2 = so3; 443 } 444 error = unp_connect2(so, so2); 445 bad: 446 vput(vp); 447 return (error); 448 } 449 450 unp_connect2(so, so2) 451 register struct socket *so; 452 register struct socket *so2; 453 { 454 register struct unpcb *unp = sotounpcb(so); 455 register struct unpcb *unp2; 456 457 if (so2->so_type != so->so_type) 458 return (EPROTOTYPE); 459 unp2 = sotounpcb(so2); 460 unp->unp_conn = unp2; 461 switch (so->so_type) { 462 463 case SOCK_DGRAM: 464 unp->unp_nextref = unp2->unp_refs; 465 unp2->unp_refs = unp; 466 soisconnected(so); 467 break; 468 469 case SOCK_STREAM: 470 unp2->unp_conn = unp; 471 soisconnected(so); 472 soisconnected(so2); 473 break; 474 475 default: 476 panic("unp_connect2"); 477 } 478 return (0); 479 } 480 481 unp_disconnect(unp) 482 struct unpcb *unp; 483 { 484 register struct unpcb *unp2 = unp->unp_conn; 485 486 if (unp2 == 0) 487 return; 488 unp->unp_conn = 0; 489 switch (unp->unp_socket->so_type) { 490 491 case SOCK_DGRAM: 492 if (unp2->unp_refs == unp) 493 unp2->unp_refs = unp->unp_nextref; 494 else { 495 unp2 = unp2->unp_refs; 496 for (;;) { 497 if (unp2 == 0) 498 panic("unp_disconnect"); 499 if (unp2->unp_nextref == unp) 500 break; 501 unp2 = unp2->unp_nextref; 502 } 503 unp2->unp_nextref = unp->unp_nextref; 504 } 505 unp->unp_nextref = 0; 506 unp->unp_socket->so_state &= ~SS_ISCONNECTED; 507 break; 508 509 case SOCK_STREAM: 510 soisdisconnected(unp->unp_socket); 511 unp2->unp_conn = 0; 512 soisdisconnected(unp2->unp_socket); 513 break; 514 } 515 } 516 517 #ifdef notdef 518 unp_abort(unp) 519 struct unpcb *unp; 520 { 521 522 unp_detach(unp); 523 } 524 #endif 525 526 unp_shutdown(unp) 527 struct unpcb *unp; 528 { 529 struct socket *so; 530 531 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 532 (so = unp->unp_conn->unp_socket)) 533 socantrcvmore(so); 534 } 535 536 unp_drop(unp, errno) 537 struct unpcb *unp; 538 int errno; 539 { 540 struct socket *so = unp->unp_socket; 541 542 so->so_error = errno; 543 unp_disconnect(unp); 544 if (so->so_head) { 545 so->so_pcb = (caddr_t) 0; 546 m_freem(unp->unp_addr); 547 (void) m_free(dtom(unp)); 548 sofree(so); 549 } 550 } 551 552 #ifdef notdef 553 unp_drain() 554 { 555 556 } 557 #endif 558 559 unp_externalize(rights) 560 struct mbuf *rights; 561 { 562 struct proc *p = curproc; /* XXX */ 563 register int i; 564 register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 565 register struct file **rp = (struct file **)(cm + 1); 566 register struct file *fp; 567 int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int); 568 int f; 569 570 if (fdavail(p, newfds)) { 571 for (i = 0; i < newfds; i++) { 572 fp = *rp; 573 unp_discard(fp); 574 *rp++ = 0; 575 } 576 return (EMSGSIZE); 577 } 578 for (i = 0; i < newfds; i++) { 579 if (fdalloc(p, 0, &f)) 580 panic("unp_externalize"); 581 fp = *rp; 582 p->p_fd->fd_ofiles[f] = fp; 583 fp->f_msgcount--; 584 unp_rights--; 585 *(int *)rp++ = f; 586 } 587 return (0); 588 } 589 590 unp_internalize(control, p) 591 struct mbuf *control; 592 struct proc *p; 593 { 594 struct filedesc *fdp = p->p_fd; 595 register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 596 register struct file **rp; 597 register struct file *fp; 598 register int i, fd; 599 int oldfds; 600 601 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 602 cm->cmsg_len != control->m_len) 603 return (EINVAL); 604 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 605 rp = (struct file **)(cm + 1); 606 for (i = 0; i < oldfds; i++) { 607 fd = *(int *)rp++; 608 if ((unsigned)fd >= fdp->fd_nfiles || 609 fdp->fd_ofiles[fd] == NULL) 610 return (EBADF); 611 } 612 rp = (struct file **)(cm + 1); 613 for (i = 0; i < oldfds; i++) { 614 fp = fdp->fd_ofiles[*(int *)rp]; 615 *rp++ = fp; 616 fp->f_count++; 617 fp->f_msgcount++; 618 unp_rights++; 619 } 620 return (0); 621 } 622 623 int unp_defer, unp_gcing; 624 int unp_mark(); 625 extern struct domain unixdomain; 626 627 unp_gc() 628 { 629 register struct file *fp, *nextfp; 630 register struct socket *so; 631 632 if (unp_gcing) 633 return; 634 unp_gcing = 1; 635 restart: 636 unp_defer = 0; 637 for (fp = filehead; fp; fp = fp->f_filef) 638 fp->f_flag &= ~(FMARK|FDEFER); 639 do { 640 for (fp = filehead; fp; fp = fp->f_filef) { 641 if (fp->f_count == 0) 642 continue; 643 if (fp->f_flag & FDEFER) { 644 fp->f_flag &= ~FDEFER; 645 unp_defer--; 646 } else { 647 if (fp->f_flag & FMARK) 648 continue; 649 if (fp->f_count == fp->f_msgcount) 650 continue; 651 fp->f_flag |= FMARK; 652 } 653 if (fp->f_type != DTYPE_SOCKET || 654 (so = (struct socket *)fp->f_data) == 0) 655 continue; 656 if (so->so_proto->pr_domain != &unixdomain || 657 (so->so_proto->pr_flags&PR_RIGHTS) == 0) 658 continue; 659 #ifdef notdef 660 if (so->so_rcv.sb_flags & SB_LOCK) { 661 /* 662 * This is problematical; it's not clear 663 * we need to wait for the sockbuf to be 664 * unlocked (on a uniprocessor, at least), 665 * and it's also not clear what to do 666 * if sbwait returns an error due to receipt 667 * of a signal. If sbwait does return 668 * an error, we'll go into an infinite 669 * loop. Delete all of this for now. 670 */ 671 (void) sbwait(&so->so_rcv); 672 goto restart; 673 } 674 #endif 675 unp_scan(so->so_rcv.sb_mb, unp_mark); 676 } 677 } while (unp_defer); 678 for (fp = filehead; fp; fp = nextfp) { 679 nextfp = fp->f_filef; 680 if (fp->f_count == 0) 681 continue; 682 if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0) 683 while (fp->f_msgcount) 684 unp_discard(fp); 685 } 686 unp_gcing = 0; 687 } 688 689 unp_dispose(m) 690 struct mbuf *m; 691 { 692 int unp_discard(); 693 694 if (m) 695 unp_scan(m, unp_discard); 696 } 697 698 unp_scan(m0, op) 699 register struct mbuf *m0; 700 int (*op)(); 701 { 702 register struct mbuf *m; 703 register struct file **rp; 704 register struct cmsghdr *cm; 705 register int i; 706 int qfds; 707 708 while (m0) { 709 for (m = m0; m; m = m->m_next) 710 if (m->m_type == MT_CONTROL && 711 m->m_len >= sizeof(*cm)) { 712 cm = mtod(m, struct cmsghdr *); 713 if (cm->cmsg_level != SOL_SOCKET || 714 cm->cmsg_type != SCM_RIGHTS) 715 continue; 716 qfds = (cm->cmsg_len - sizeof *cm) 717 / sizeof (struct file *); 718 rp = (struct file **)(cm + 1); 719 for (i = 0; i < qfds; i++) 720 (*op)(*rp++); 721 break; /* XXX, but saves time */ 722 } 723 m0 = m0->m_act; 724 } 725 } 726 727 unp_mark(fp) 728 struct file *fp; 729 { 730 731 if (fp->f_flag & FMARK) 732 return; 733 unp_defer++; 734 fp->f_flag |= (FMARK|FDEFER); 735 } 736 737 unp_discard(fp) 738 struct file *fp; 739 { 740 741 fp->f_msgcount--; 742 unp_rights--; 743 (void) closef(fp, (struct proc *)NULL); 744 } 745