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