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