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.17 (Berkeley) 05/04/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 return (EADDRINUSE); 374 } 375 VATTR_NULL(&vattr); 376 vattr.va_type = VSOCK; 377 vattr.va_mode = 0777; 378 if (error = VOP_CREATE(ndp, &vattr)) 379 return (error); 380 vp = ndp->ni_vp; 381 vp->v_socket = unp->unp_socket; 382 unp->unp_vnode = vp; 383 unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL); 384 VOP_UNLOCK(vp); 385 return (0); 386 } 387 388 unp_connect(so, nam) 389 struct socket *so; 390 struct mbuf *nam; 391 { 392 register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 393 register struct vnode *vp; 394 register struct socket *so2, *so3; 395 register struct nameidata *ndp = &u.u_nd; 396 struct unpcb *unp2, *unp3; 397 int error; 398 399 ndp->ni_dirp = soun->sun_path; 400 if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) { /* XXX */ 401 if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 402 return (EMSGSIZE); 403 } else 404 *(mtod(nam, caddr_t) + nam->m_len) = 0; 405 ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF; 406 ndp->ni_segflg = UIO_SYSSPACE; 407 if (error = namei(ndp)) 408 return (error); 409 vp = ndp->ni_vp; 410 if (vp->v_type != VSOCK) { 411 error = ENOTSOCK; 412 goto bad; 413 } 414 if (error = VOP_ACCESS(vp, VWRITE, ndp->ni_cred)) 415 goto bad; 416 so2 = vp->v_socket; 417 if (so2 == 0) { 418 error = ECONNREFUSED; 419 goto bad; 420 } 421 if (so->so_type != so2->so_type) { 422 error = EPROTOTYPE; 423 goto bad; 424 } 425 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 426 if ((so2->so_options & SO_ACCEPTCONN) == 0 || 427 (so3 = sonewconn(so2, 0)) == 0) { 428 error = ECONNREFUSED; 429 goto bad; 430 } 431 unp2 = sotounpcb(so2); 432 unp3 = sotounpcb(so3); 433 if (unp2->unp_addr) 434 unp3->unp_addr = 435 m_copy(unp2->unp_addr, 0, (int)M_COPYALL); 436 so2 = so3; 437 } 438 error = unp_connect2(so, so2); 439 bad: 440 vput(vp); 441 return (error); 442 } 443 444 unp_connect2(so, so2) 445 register struct socket *so; 446 register struct socket *so2; 447 { 448 register struct unpcb *unp = sotounpcb(so); 449 register struct unpcb *unp2; 450 451 if (so2->so_type != so->so_type) 452 return (EPROTOTYPE); 453 unp2 = sotounpcb(so2); 454 unp->unp_conn = unp2; 455 switch (so->so_type) { 456 457 case SOCK_DGRAM: 458 unp->unp_nextref = unp2->unp_refs; 459 unp2->unp_refs = unp; 460 soisconnected(so); 461 break; 462 463 case SOCK_STREAM: 464 unp2->unp_conn = unp; 465 soisconnected(so); 466 soisconnected(so2); 467 break; 468 469 default: 470 panic("unp_connect2"); 471 } 472 return (0); 473 } 474 475 unp_disconnect(unp) 476 struct unpcb *unp; 477 { 478 register struct unpcb *unp2 = unp->unp_conn; 479 480 if (unp2 == 0) 481 return; 482 unp->unp_conn = 0; 483 switch (unp->unp_socket->so_type) { 484 485 case SOCK_DGRAM: 486 if (unp2->unp_refs == unp) 487 unp2->unp_refs = unp->unp_nextref; 488 else { 489 unp2 = unp2->unp_refs; 490 for (;;) { 491 if (unp2 == 0) 492 panic("unp_disconnect"); 493 if (unp2->unp_nextref == unp) 494 break; 495 unp2 = unp2->unp_nextref; 496 } 497 unp2->unp_nextref = unp->unp_nextref; 498 } 499 unp->unp_nextref = 0; 500 unp->unp_socket->so_state &= ~SS_ISCONNECTED; 501 break; 502 503 case SOCK_STREAM: 504 soisdisconnected(unp->unp_socket); 505 unp2->unp_conn = 0; 506 soisdisconnected(unp2->unp_socket); 507 break; 508 } 509 } 510 511 #ifdef notdef 512 unp_abort(unp) 513 struct unpcb *unp; 514 { 515 516 unp_detach(unp); 517 } 518 #endif 519 520 /*ARGSUSED*/ 521 unp_usrclosed(unp) 522 struct unpcb *unp; 523 { 524 525 } 526 527 unp_drop(unp, errno) 528 struct unpcb *unp; 529 int errno; 530 { 531 struct socket *so = unp->unp_socket; 532 533 so->so_error = errno; 534 unp_disconnect(unp); 535 if (so->so_head) { 536 so->so_pcb = (caddr_t) 0; 537 m_freem(unp->unp_addr); 538 (void) m_free(dtom(unp)); 539 sofree(so); 540 } 541 } 542 543 #ifdef notdef 544 unp_drain() 545 { 546 547 } 548 #endif 549 550 unp_externalize(rights) 551 struct mbuf *rights; 552 { 553 register int i; 554 register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 555 register struct file **rp = (struct file **)(cm + 1); 556 register struct file *fp; 557 int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int); 558 int f; 559 560 if (newfds > ufavail()) { 561 for (i = 0; i < newfds; i++) { 562 fp = *rp; 563 unp_discard(fp); 564 *rp++ = 0; 565 } 566 return (EMSGSIZE); 567 } 568 for (i = 0; i < newfds; i++) { 569 if (ufalloc(0, &f)) 570 panic("unp_externalize"); 571 fp = *rp; 572 u.u_ofile[f] = fp; 573 fp->f_msgcount--; 574 unp_rights--; 575 *(int *)rp++ = f; 576 } 577 return (0); 578 } 579 580 unp_internalize(control) 581 struct mbuf *control; 582 { 583 register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 584 register struct file **rp; 585 register struct file *fp; 586 register int i, fd; 587 int oldfds; 588 589 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 590 cm->cmsg_len != control->m_len) 591 return (EINVAL); 592 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 593 rp = (struct file **)(cm + 1); 594 for (i = 0; i < oldfds; i++) { 595 fd = *(int *)rp++; 596 if ((unsigned)fd >= NOFILE || u.u_ofile[fd] == NULL) 597 return (EBADF); 598 } 599 rp = (struct file **)(cm + 1); 600 for (i = 0; i < oldfds; i++) { 601 fp = u.u_ofile[*(int *)rp]; 602 *rp++ = fp; 603 fp->f_count++; 604 fp->f_msgcount++; 605 unp_rights++; 606 } 607 return (0); 608 } 609 610 int unp_defer, unp_gcing; 611 int unp_mark(); 612 extern struct domain unixdomain; 613 614 unp_gc() 615 { 616 register struct file *fp; 617 register struct socket *so; 618 619 if (unp_gcing) 620 return; 621 unp_gcing = 1; 622 restart: 623 unp_defer = 0; 624 for (fp = file; fp < fileNFILE; fp++) 625 fp->f_flag &= ~(FMARK|FDEFER); 626 do { 627 for (fp = file; fp < fileNFILE; fp++) { 628 if (fp->f_count == 0) 629 continue; 630 if (fp->f_flag & FDEFER) { 631 fp->f_flag &= ~FDEFER; 632 unp_defer--; 633 } else { 634 if (fp->f_flag & FMARK) 635 continue; 636 if (fp->f_count == fp->f_msgcount) 637 continue; 638 fp->f_flag |= FMARK; 639 } 640 if (fp->f_type != DTYPE_SOCKET || 641 (so = (struct socket *)fp->f_data) == 0) 642 continue; 643 if (so->so_proto->pr_domain != &unixdomain || 644 (so->so_proto->pr_flags&PR_RIGHTS) == 0) 645 continue; 646 if (so->so_rcv.sb_flags & SB_LOCK) { 647 sbwait(&so->so_rcv); 648 goto restart; 649 } 650 unp_scan(so->so_rcv.sb_mb, unp_mark); 651 } 652 } while (unp_defer); 653 for (fp = file; fp < fileNFILE; fp++) { 654 if (fp->f_count == 0) 655 continue; 656 if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0) 657 while (fp->f_msgcount) 658 unp_discard(fp); 659 } 660 unp_gcing = 0; 661 } 662 663 unp_dispose(m) 664 struct mbuf *m; 665 { 666 int unp_discard(); 667 668 if (m) 669 unp_scan(m, unp_discard); 670 } 671 672 unp_scan(m0, op) 673 register struct mbuf *m0; 674 int (*op)(); 675 { 676 register struct mbuf *m; 677 register struct file **rp; 678 register struct cmsghdr *cm; 679 register int i; 680 int qfds; 681 682 while (m0) { 683 for (m = m0; m; m = m->m_next) 684 if (m->m_type == MT_CONTROL && 685 m->m_len >= sizeof(*cm)) { 686 cm = mtod(m, struct cmsghdr *); 687 if (cm->cmsg_level != SOL_SOCKET || 688 cm->cmsg_type != SCM_RIGHTS) 689 continue; 690 qfds = (cm->cmsg_len - sizeof *cm) 691 / sizeof (struct file *); 692 rp = (struct file **)(cm + 1); 693 for (i = 0; i < qfds; i++) 694 (*op)(*rp++); 695 break; /* XXX, but saves time */ 696 } 697 m0 = m0->m_act; 698 } 699 } 700 701 unp_mark(fp) 702 struct file *fp; 703 { 704 705 if (fp->f_flag & FMARK) 706 return; 707 unp_defer++; 708 fp->f_flag |= (FMARK|FDEFER); 709 } 710 711 unp_discard(fp) 712 struct file *fp; 713 { 714 715 fp->f_msgcount--; 716 unp_rights--; 717 (void) closef(fp); 718 } 719