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