1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)uipc_usrreq.c 7.19 (Berkeley) 06/21/90 18 */ 19 20 #include "param.h" 21 #include "user.h" 22 #include "domain.h" 23 #include "protosw.h" 24 #include "socket.h" 25 #include "socketvar.h" 26 #include "unpcb.h" 27 #include "un.h" 28 #include "vnode.h" 29 #include "file.h" 30 #include "stat.h" 31 #include "mbuf.h" 32 33 /* 34 * Unix communications domain. 35 * 36 * TODO: 37 * SEQPACKET, RDM 38 * rethink name space problems 39 * need a proper out-of-band 40 */ 41 struct sockaddr sun_noname = { sizeof(sun_noname), AF_UNIX }; 42 ino_t unp_ino; /* prototype for fake inode numbers */ 43 44 /*ARGSUSED*/ 45 uipc_usrreq(so, req, m, nam, control) 46 struct socket *so; 47 int req; 48 struct mbuf *m, *nam, *control; 49 { 50 struct unpcb *unp = sotounpcb(so); 51 register struct socket *so2; 52 register int error = 0; 53 54 if (req == PRU_CONTROL) 55 return (EOPNOTSUPP); 56 if (req != PRU_SEND && control && control->m_len) { 57 error = EOPNOTSUPP; 58 goto release; 59 } 60 if (unp == 0 && req != PRU_ATTACH) { 61 error = EINVAL; 62 goto release; 63 } 64 switch (req) { 65 66 case PRU_ATTACH: 67 if (unp) { 68 error = EISCONN; 69 break; 70 } 71 error = unp_attach(so); 72 break; 73 74 case PRU_DETACH: 75 unp_detach(unp); 76 break; 77 78 case PRU_BIND: 79 error = unp_bind(unp, nam); 80 break; 81 82 case PRU_LISTEN: 83 if (unp->unp_vnode == 0) 84 error = EINVAL; 85 break; 86 87 case PRU_CONNECT: 88 error = unp_connect(so, nam); 89 break; 90 91 case PRU_CONNECT2: 92 error = unp_connect2(so, (struct socket *)nam); 93 break; 94 95 case PRU_DISCONNECT: 96 unp_disconnect(unp); 97 break; 98 99 case PRU_ACCEPT: 100 /* 101 * Pass back name of connected socket, 102 * if it was bound and we are still connected 103 * (our peer may have closed already!). 104 */ 105 if (unp->unp_conn && unp->unp_conn->unp_addr) { 106 nam->m_len = unp->unp_conn->unp_addr->m_len; 107 bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 108 mtod(nam, caddr_t), (unsigned)nam->m_len); 109 } else { 110 nam->m_len = sizeof(sun_noname); 111 *(mtod(nam, struct sockaddr *)) = sun_noname; 112 } 113 break; 114 115 case PRU_SHUTDOWN: 116 socantsendmore(so); 117 unp_usrclosed(unp); 118 break; 119 120 case PRU_RCVD: 121 switch (so->so_type) { 122 123 case SOCK_DGRAM: 124 panic("uipc 1"); 125 /*NOTREACHED*/ 126 127 case SOCK_STREAM: 128 #define rcv (&so->so_rcv) 129 #define snd (&so2->so_snd) 130 if (unp->unp_conn == 0) 131 break; 132 so2 = unp->unp_conn->unp_socket; 133 /* 134 * Adjust backpressure on sender 135 * and wakeup any waiting to write. 136 */ 137 snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; 138 unp->unp_mbcnt = rcv->sb_mbcnt; 139 snd->sb_hiwat += unp->unp_cc - rcv->sb_cc; 140 unp->unp_cc = rcv->sb_cc; 141 sowwakeup(so2); 142 #undef snd 143 #undef rcv 144 break; 145 146 default: 147 panic("uipc 2"); 148 } 149 break; 150 151 case PRU_SEND: 152 if (control && (error = unp_internalize(control))) 153 break; 154 switch (so->so_type) { 155 156 case SOCK_DGRAM: { 157 struct sockaddr *from; 158 159 if (nam) { 160 if (unp->unp_conn) { 161 error = EISCONN; 162 break; 163 } 164 error = unp_connect(so, nam); 165 if (error) 166 break; 167 } else { 168 if (unp->unp_conn == 0) { 169 error = ENOTCONN; 170 break; 171 } 172 } 173 so2 = unp->unp_conn->unp_socket; 174 if (unp->unp_addr) 175 from = mtod(unp->unp_addr, struct sockaddr *); 176 else 177 from = &sun_noname; 178 if (sbappendaddr(&so2->so_rcv, from, m, control)) { 179 sorwakeup(so2); 180 m = 0; 181 control = 0; 182 } else 183 error = ENOBUFS; 184 if (nam) 185 unp_disconnect(unp); 186 break; 187 } 188 189 case SOCK_STREAM: 190 #define rcv (&so2->so_rcv) 191 #define snd (&so->so_snd) 192 if (so->so_state & SS_CANTSENDMORE) { 193 error = EPIPE; 194 break; 195 } 196 if (unp->unp_conn == 0) 197 panic("uipc 3"); 198 so2 = unp->unp_conn->unp_socket; 199 /* 200 * Send to paired receive port, and then reduce 201 * send buffer hiwater marks to maintain backpressure. 202 * Wake up readers. 203 */ 204 if (control) { 205 (void)sbappendcontrol(rcv, m, control); 206 control = 0; 207 } else 208 sbappend(rcv, m); 209 snd->sb_mbmax -= 210 rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; 211 unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; 212 snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc; 213 unp->unp_conn->unp_cc = rcv->sb_cc; 214 sorwakeup(so2); 215 m = 0; 216 #undef snd 217 #undef rcv 218 break; 219 220 default: 221 panic("uipc 4"); 222 } 223 break; 224 225 case PRU_ABORT: 226 unp_drop(unp, ECONNABORTED); 227 break; 228 229 case PRU_SENSE: 230 ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 231 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 232 so2 = unp->unp_conn->unp_socket; 233 ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc; 234 } 235 ((struct stat *) m)->st_dev = NODEV; 236 if (unp->unp_ino == 0) 237 unp->unp_ino = unp_ino++; 238 ((struct stat *) m)->st_ino = unp->unp_ino; 239 return (0); 240 241 case PRU_RCVOOB: 242 return (EOPNOTSUPP); 243 244 case PRU_SENDOOB: 245 error = EOPNOTSUPP; 246 break; 247 248 case PRU_SOCKADDR: 249 if (unp->unp_addr) { 250 nam->m_len = unp->unp_addr->m_len; 251 bcopy(mtod(unp->unp_addr, caddr_t), 252 mtod(nam, caddr_t), (unsigned)nam->m_len); 253 } else 254 nam->m_len = 0; 255 break; 256 257 case PRU_PEERADDR: 258 if (unp->unp_conn && unp->unp_conn->unp_addr) { 259 nam->m_len = unp->unp_conn->unp_addr->m_len; 260 bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 261 mtod(nam, caddr_t), (unsigned)nam->m_len); 262 } else 263 nam->m_len = 0; 264 break; 265 266 case PRU_SLOWTIMO: 267 break; 268 269 default: 270 panic("piusrreq"); 271 } 272 release: 273 if (control) 274 m_freem(control); 275 if (m) 276 m_freem(m); 277 return (error); 278 } 279 280 /* 281 * Both send and receive buffers are allocated PIPSIZ bytes of buffering 282 * for stream sockets, although the total for sender and receiver is 283 * actually only PIPSIZ. 284 * Datagram sockets really use the sendspace as the maximum datagram size, 285 * and don't really want to reserve the sendspace. Their recvspace should 286 * be large enough for at least one max-size datagram plus address. 287 */ 288 #define PIPSIZ 4096 289 u_long unpst_sendspace = PIPSIZ; 290 u_long unpst_recvspace = PIPSIZ; 291 u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 292 u_long unpdg_recvspace = 4*1024; 293 294 int unp_rights; /* file descriptors in flight */ 295 296 unp_attach(so) 297 struct socket *so; 298 { 299 register struct mbuf *m; 300 register struct unpcb *unp; 301 int error; 302 303 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 304 switch (so->so_type) { 305 306 case SOCK_STREAM: 307 error = soreserve(so, unpst_sendspace, unpst_recvspace); 308 break; 309 310 case SOCK_DGRAM: 311 error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 312 break; 313 } 314 if (error) 315 return (error); 316 } 317 m = m_getclr(M_DONTWAIT, MT_PCB); 318 if (m == NULL) 319 return (ENOBUFS); 320 unp = mtod(m, struct unpcb *); 321 so->so_pcb = (caddr_t)unp; 322 unp->unp_socket = so; 323 return (0); 324 } 325 326 unp_detach(unp) 327 register struct unpcb *unp; 328 { 329 330 if (unp->unp_vnode) { 331 unp->unp_vnode->v_socket = 0; 332 vrele(unp->unp_vnode); 333 unp->unp_vnode = 0; 334 } 335 if (unp->unp_conn) 336 unp_disconnect(unp); 337 while (unp->unp_refs) 338 unp_drop(unp->unp_refs, ECONNRESET); 339 soisdisconnected(unp->unp_socket); 340 unp->unp_socket->so_pcb = 0; 341 m_freem(unp->unp_addr); 342 (void) m_free(dtom(unp)); 343 if (unp_rights) 344 unp_gc(); 345 } 346 347 unp_bind(unp, nam) 348 struct unpcb *unp; 349 struct mbuf *nam; 350 { 351 struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 352 register struct vnode *vp; 353 register struct nameidata *ndp = &u.u_nd; 354 struct vattr vattr; 355 int error; 356 357 ndp->ni_dirp = soun->sun_path; 358 if (unp->unp_vnode != NULL) 359 return (EINVAL); 360 if (nam->m_len == MLEN) { 361 if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 362 return (EINVAL); 363 } else 364 *(mtod(nam, caddr_t) + nam->m_len) = 0; 365 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 366 ndp->ni_nameiop = CREATE | FOLLOW | LOCKPARENT; 367 ndp->ni_segflg = UIO_SYSSPACE; 368 if (error = namei(ndp)) 369 return (error); 370 vp = ndp->ni_vp; 371 if (vp != NULL) { 372 VOP_ABORTOP(ndp); 373 if (ndp->ni_dvp == vp) 374 vrele(ndp->ni_dvp); 375 else 376 vput(ndp->ni_dvp); 377 vrele(vp); 378 return (EADDRINUSE); 379 } 380 VATTR_NULL(&vattr); 381 vattr.va_type = VSOCK; 382 vattr.va_mode = 0777; 383 if (error = VOP_CREATE(ndp, &vattr)) 384 return (error); 385 vp = ndp->ni_vp; 386 vp->v_socket = unp->unp_socket; 387 unp->unp_vnode = vp; 388 unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL); 389 VOP_UNLOCK(vp); 390 return (0); 391 } 392 393 unp_connect(so, nam) 394 struct socket *so; 395 struct mbuf *nam; 396 { 397 register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 398 register struct vnode *vp; 399 register struct socket *so2, *so3; 400 register struct nameidata *ndp = &u.u_nd; 401 struct unpcb *unp2, *unp3; 402 int error; 403 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)) 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, ndp->ni_cred)) 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 /*ARGSUSED*/ 526 unp_usrclosed(unp) 527 struct unpcb *unp; 528 { 529 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 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 (newfds > ufavail()) { 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 (ufalloc(0, &f)) 575 panic("unp_externalize"); 576 fp = *rp; 577 u.u_ofile[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 register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 589 register struct file **rp; 590 register struct file *fp; 591 register int i, fd; 592 int oldfds; 593 594 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 595 cm->cmsg_len != control->m_len) 596 return (EINVAL); 597 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 598 rp = (struct file **)(cm + 1); 599 for (i = 0; i < oldfds; i++) { 600 fd = *(int *)rp++; 601 if ((unsigned)fd >= NOFILE || u.u_ofile[fd] == NULL) 602 return (EBADF); 603 } 604 rp = (struct file **)(cm + 1); 605 for (i = 0; i < oldfds; i++) { 606 fp = u.u_ofile[*(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 = file; fp < fileNFILE; fp++) 630 fp->f_flag &= ~(FMARK|FDEFER); 631 do { 632 for (fp = file; fp < fileNFILE; fp++) { 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 if (so->so_rcv.sb_flags & SB_LOCK) { 652 sbwait(&so->so_rcv); 653 goto restart; 654 } 655 unp_scan(so->so_rcv.sb_mb, unp_mark); 656 } 657 } while (unp_defer); 658 for (fp = file; fp < fileNFILE; fp++) { 659 if (fp->f_count == 0) 660 continue; 661 if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0) 662 while (fp->f_msgcount) 663 unp_discard(fp); 664 } 665 unp_gcing = 0; 666 } 667 668 unp_dispose(m) 669 struct mbuf *m; 670 { 671 int unp_discard(); 672 673 if (m) 674 unp_scan(m, unp_discard); 675 } 676 677 unp_scan(m0, op) 678 register struct mbuf *m0; 679 int (*op)(); 680 { 681 register struct mbuf *m; 682 register struct file **rp; 683 register struct cmsghdr *cm; 684 register int i; 685 int qfds; 686 687 while (m0) { 688 for (m = m0; m; m = m->m_next) 689 if (m->m_type == MT_CONTROL && 690 m->m_len >= sizeof(*cm)) { 691 cm = mtod(m, struct cmsghdr *); 692 if (cm->cmsg_level != SOL_SOCKET || 693 cm->cmsg_type != SCM_RIGHTS) 694 continue; 695 qfds = (cm->cmsg_len - sizeof *cm) 696 / sizeof (struct file *); 697 rp = (struct file **)(cm + 1); 698 for (i = 0; i < qfds; i++) 699 (*op)(*rp++); 700 break; /* XXX, but saves time */ 701 } 702 m0 = m0->m_act; 703 } 704 } 705 706 unp_mark(fp) 707 struct file *fp; 708 { 709 710 if (fp->f_flag & FMARK) 711 return; 712 unp_defer++; 713 fp->f_flag |= (FMARK|FDEFER); 714 } 715 716 unp_discard(fp) 717 struct file *fp; 718 { 719 720 fp->f_msgcount--; 721 unp_rights--; 722 (void) closef(fp); 723 } 724