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