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.28 (Berkeley) 01/14/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 register struct nameidata *ndp; 348 struct vattr vattr; 349 int error; 350 struct nameidata nd; 351 352 ndp = &nd; 353 ndp->ni_dirp = soun->sun_path; 354 if (unp->unp_vnode != NULL) 355 return (EINVAL); 356 if (nam->m_len == MLEN) { 357 if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 358 return (EINVAL); 359 } else 360 *(mtod(nam, caddr_t) + nam->m_len) = 0; 361 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 362 ndp->ni_nameiop = CREATE | FOLLOW | LOCKPARENT; 363 ndp->ni_segflg = UIO_SYSSPACE; 364 if (error = namei(ndp, p)) 365 return (error); 366 vp = ndp->ni_vp; 367 if (vp != NULL) { 368 VOP_ABORTOP(ndp); 369 if (ndp->ni_dvp == vp) 370 vrele(ndp->ni_dvp); 371 else 372 vput(ndp->ni_dvp); 373 vrele(vp); 374 return (EADDRINUSE); 375 } 376 VATTR_NULL(&vattr); 377 vattr.va_type = VSOCK; 378 vattr.va_mode = 0777; 379 LEASE_CHECK(ndp->ni_dvp, p, p->p_ucred, LEASE_WRITE); 380 if (error = VOP_CREATE(ndp, &vattr, p)) 381 return (error); 382 vp = ndp->ni_vp; 383 vp->v_socket = unp->unp_socket; 384 unp->unp_vnode = vp; 385 unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL); 386 VOP_UNLOCK(vp); 387 return (0); 388 } 389 390 unp_connect(so, nam, p) 391 struct socket *so; 392 struct mbuf *nam; 393 struct proc *p; 394 { 395 register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 396 register struct vnode *vp; 397 register struct socket *so2, *so3; 398 register struct nameidata *ndp; 399 struct unpcb *unp2, *unp3; 400 int error; 401 struct nameidata nd; 402 403 ndp = &nd; 404 ndp->ni_dirp = soun->sun_path; 405 if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) { /* XXX */ 406 if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 407 return (EMSGSIZE); 408 } else 409 *(mtod(nam, caddr_t) + nam->m_len) = 0; 410 ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF; 411 ndp->ni_segflg = UIO_SYSSPACE; 412 if (error = namei(ndp, p)) 413 return (error); 414 vp = ndp->ni_vp; 415 if (vp->v_type != VSOCK) { 416 error = ENOTSOCK; 417 goto bad; 418 } 419 if (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) 420 goto bad; 421 so2 = vp->v_socket; 422 if (so2 == 0) { 423 error = ECONNREFUSED; 424 goto bad; 425 } 426 if (so->so_type != so2->so_type) { 427 error = EPROTOTYPE; 428 goto bad; 429 } 430 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 431 if ((so2->so_options & SO_ACCEPTCONN) == 0 || 432 (so3 = sonewconn(so2, 0)) == 0) { 433 error = ECONNREFUSED; 434 goto bad; 435 } 436 unp2 = sotounpcb(so2); 437 unp3 = sotounpcb(so3); 438 if (unp2->unp_addr) 439 unp3->unp_addr = 440 m_copy(unp2->unp_addr, 0, (int)M_COPYALL); 441 so2 = so3; 442 } 443 error = unp_connect2(so, so2); 444 bad: 445 vput(vp); 446 return (error); 447 } 448 449 unp_connect2(so, so2) 450 register struct socket *so; 451 register struct socket *so2; 452 { 453 register struct unpcb *unp = sotounpcb(so); 454 register struct unpcb *unp2; 455 456 if (so2->so_type != so->so_type) 457 return (EPROTOTYPE); 458 unp2 = sotounpcb(so2); 459 unp->unp_conn = unp2; 460 switch (so->so_type) { 461 462 case SOCK_DGRAM: 463 unp->unp_nextref = unp2->unp_refs; 464 unp2->unp_refs = unp; 465 soisconnected(so); 466 break; 467 468 case SOCK_STREAM: 469 unp2->unp_conn = unp; 470 soisconnected(so); 471 soisconnected(so2); 472 break; 473 474 default: 475 panic("unp_connect2"); 476 } 477 return (0); 478 } 479 480 unp_disconnect(unp) 481 struct unpcb *unp; 482 { 483 register struct unpcb *unp2 = unp->unp_conn; 484 485 if (unp2 == 0) 486 return; 487 unp->unp_conn = 0; 488 switch (unp->unp_socket->so_type) { 489 490 case SOCK_DGRAM: 491 if (unp2->unp_refs == unp) 492 unp2->unp_refs = unp->unp_nextref; 493 else { 494 unp2 = unp2->unp_refs; 495 for (;;) { 496 if (unp2 == 0) 497 panic("unp_disconnect"); 498 if (unp2->unp_nextref == unp) 499 break; 500 unp2 = unp2->unp_nextref; 501 } 502 unp2->unp_nextref = unp->unp_nextref; 503 } 504 unp->unp_nextref = 0; 505 unp->unp_socket->so_state &= ~SS_ISCONNECTED; 506 break; 507 508 case SOCK_STREAM: 509 soisdisconnected(unp->unp_socket); 510 unp2->unp_conn = 0; 511 soisdisconnected(unp2->unp_socket); 512 break; 513 } 514 } 515 516 #ifdef notdef 517 unp_abort(unp) 518 struct unpcb *unp; 519 { 520 521 unp_detach(unp); 522 } 523 #endif 524 525 unp_shutdown(unp) 526 struct unpcb *unp; 527 { 528 struct socket *so; 529 530 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 531 (so = unp->unp_conn->unp_socket)) 532 socantrcvmore(so); 533 } 534 535 unp_drop(unp, errno) 536 struct unpcb *unp; 537 int errno; 538 { 539 struct socket *so = unp->unp_socket; 540 541 so->so_error = errno; 542 unp_disconnect(unp); 543 if (so->so_head) { 544 so->so_pcb = (caddr_t) 0; 545 m_freem(unp->unp_addr); 546 (void) m_free(dtom(unp)); 547 sofree(so); 548 } 549 } 550 551 #ifdef notdef 552 unp_drain() 553 { 554 555 } 556 #endif 557 558 unp_externalize(rights) 559 struct mbuf *rights; 560 { 561 struct proc *p = curproc; /* XXX */ 562 register int i; 563 register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 564 register struct file **rp = (struct file **)(cm + 1); 565 register struct file *fp; 566 int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int); 567 int f; 568 569 if (fdavail(p, newfds)) { 570 for (i = 0; i < newfds; i++) { 571 fp = *rp; 572 unp_discard(fp); 573 *rp++ = 0; 574 } 575 return (EMSGSIZE); 576 } 577 for (i = 0; i < newfds; i++) { 578 if (fdalloc(p, 0, &f)) 579 panic("unp_externalize"); 580 fp = *rp; 581 p->p_fd->fd_ofiles[f] = fp; 582 fp->f_msgcount--; 583 unp_rights--; 584 *(int *)rp++ = f; 585 } 586 return (0); 587 } 588 589 unp_internalize(control, p) 590 struct mbuf *control; 591 struct proc *p; 592 { 593 struct filedesc *fdp = p->p_fd; 594 register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 595 register struct file **rp; 596 register struct file *fp; 597 register int i, fd; 598 int oldfds; 599 600 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 601 cm->cmsg_len != control->m_len) 602 return (EINVAL); 603 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 604 rp = (struct file **)(cm + 1); 605 for (i = 0; i < oldfds; i++) { 606 fd = *(int *)rp++; 607 if ((unsigned)fd >= fdp->fd_nfiles || 608 fdp->fd_ofiles[fd] == NULL) 609 return (EBADF); 610 } 611 rp = (struct file **)(cm + 1); 612 for (i = 0; i < oldfds; i++) { 613 fp = fdp->fd_ofiles[*(int *)rp]; 614 *rp++ = fp; 615 fp->f_count++; 616 fp->f_msgcount++; 617 unp_rights++; 618 } 619 return (0); 620 } 621 622 int unp_defer, unp_gcing; 623 int unp_mark(); 624 extern struct domain unixdomain; 625 626 unp_gc() 627 { 628 register struct file *fp; 629 register struct socket *so; 630 631 if (unp_gcing) 632 return; 633 unp_gcing = 1; 634 restart: 635 unp_defer = 0; 636 for (fp = filehead; fp; fp = fp->f_filef) 637 fp->f_flag &= ~(FMARK|FDEFER); 638 do { 639 for (fp = filehead; fp; fp = fp->f_filef) { 640 if (fp->f_count == 0) 641 continue; 642 if (fp->f_flag & FDEFER) { 643 fp->f_flag &= ~FDEFER; 644 unp_defer--; 645 } else { 646 if (fp->f_flag & FMARK) 647 continue; 648 if (fp->f_count == fp->f_msgcount) 649 continue; 650 fp->f_flag |= FMARK; 651 } 652 if (fp->f_type != DTYPE_SOCKET || 653 (so = (struct socket *)fp->f_data) == 0) 654 continue; 655 if (so->so_proto->pr_domain != &unixdomain || 656 (so->so_proto->pr_flags&PR_RIGHTS) == 0) 657 continue; 658 #ifdef notdef 659 if (so->so_rcv.sb_flags & SB_LOCK) { 660 /* 661 * This is problematical; it's not clear 662 * we need to wait for the sockbuf to be 663 * unlocked (on a uniprocessor, at least), 664 * and it's also not clear what to do 665 * if sbwait returns an error due to receipt 666 * of a signal. If sbwait does return 667 * an error, we'll go into an infinite 668 * loop. Delete all of this for now. 669 */ 670 (void) sbwait(&so->so_rcv); 671 goto restart; 672 } 673 #endif 674 unp_scan(so->so_rcv.sb_mb, unp_mark); 675 } 676 } while (unp_defer); 677 for (fp = filehead; fp; fp = fp->f_filef) { 678 if (fp->f_count == 0) 679 continue; 680 if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0) 681 while (fp->f_msgcount) 682 unp_discard(fp); 683 } 684 unp_gcing = 0; 685 } 686 687 unp_dispose(m) 688 struct mbuf *m; 689 { 690 int unp_discard(); 691 692 if (m) 693 unp_scan(m, unp_discard); 694 } 695 696 unp_scan(m0, op) 697 register struct mbuf *m0; 698 int (*op)(); 699 { 700 register struct mbuf *m; 701 register struct file **rp; 702 register struct cmsghdr *cm; 703 register int i; 704 int qfds; 705 706 while (m0) { 707 for (m = m0; m; m = m->m_next) 708 if (m->m_type == MT_CONTROL && 709 m->m_len >= sizeof(*cm)) { 710 cm = mtod(m, struct cmsghdr *); 711 if (cm->cmsg_level != SOL_SOCKET || 712 cm->cmsg_type != SCM_RIGHTS) 713 continue; 714 qfds = (cm->cmsg_len - sizeof *cm) 715 / sizeof (struct file *); 716 rp = (struct file **)(cm + 1); 717 for (i = 0; i < qfds; i++) 718 (*op)(*rp++); 719 break; /* XXX, but saves time */ 720 } 721 m0 = m0->m_act; 722 } 723 } 724 725 unp_mark(fp) 726 struct file *fp; 727 { 728 729 if (fp->f_flag & FMARK) 730 return; 731 unp_defer++; 732 fp->f_flag |= (FMARK|FDEFER); 733 } 734 735 unp_discard(fp) 736 struct file *fp; 737 { 738 739 fp->f_msgcount--; 740 unp_rights--; 741 (void) closef(fp, (struct proc *)NULL); 742 } 743