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.18 (Berkeley) 05/30/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 vput(ndp->ni_dvp); 374 vrele(vp); 375 return (EADDRINUSE); 376 } 377 VATTR_NULL(&vattr); 378 vattr.va_type = VSOCK; 379 vattr.va_mode = 0777; 380 if (error = VOP_CREATE(ndp, &vattr)) 381 return (error); 382 vp = ndp->ni_vp; 383 vp->v_socket = unp->unp_socket; 384 unp->unp_vnode = vp; 385 unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL); 386 VOP_UNLOCK(vp); 387 return (0); 388 } 389 390 unp_connect(so, nam) 391 struct socket *so; 392 struct mbuf *nam; 393 { 394 register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 395 register struct vnode *vp; 396 register struct socket *so2, *so3; 397 register struct nameidata *ndp = &u.u_nd; 398 struct unpcb *unp2, *unp3; 399 int error; 400 401 ndp->ni_dirp = soun->sun_path; 402 if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) { /* XXX */ 403 if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 404 return (EMSGSIZE); 405 } else 406 *(mtod(nam, caddr_t) + nam->m_len) = 0; 407 ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF; 408 ndp->ni_segflg = UIO_SYSSPACE; 409 if (error = namei(ndp)) 410 return (error); 411 vp = ndp->ni_vp; 412 if (vp->v_type != VSOCK) { 413 error = ENOTSOCK; 414 goto bad; 415 } 416 if (error = VOP_ACCESS(vp, VWRITE, ndp->ni_cred)) 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 /*ARGSUSED*/ 523 unp_usrclosed(unp) 524 struct unpcb *unp; 525 { 526 527 } 528 529 unp_drop(unp, errno) 530 struct unpcb *unp; 531 int errno; 532 { 533 struct socket *so = unp->unp_socket; 534 535 so->so_error = errno; 536 unp_disconnect(unp); 537 if (so->so_head) { 538 so->so_pcb = (caddr_t) 0; 539 m_freem(unp->unp_addr); 540 (void) m_free(dtom(unp)); 541 sofree(so); 542 } 543 } 544 545 #ifdef notdef 546 unp_drain() 547 { 548 549 } 550 #endif 551 552 unp_externalize(rights) 553 struct mbuf *rights; 554 { 555 register int i; 556 register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 557 register struct file **rp = (struct file **)(cm + 1); 558 register struct file *fp; 559 int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int); 560 int f; 561 562 if (newfds > ufavail()) { 563 for (i = 0; i < newfds; i++) { 564 fp = *rp; 565 unp_discard(fp); 566 *rp++ = 0; 567 } 568 return (EMSGSIZE); 569 } 570 for (i = 0; i < newfds; i++) { 571 if (ufalloc(0, &f)) 572 panic("unp_externalize"); 573 fp = *rp; 574 u.u_ofile[f] = fp; 575 fp->f_msgcount--; 576 unp_rights--; 577 *(int *)rp++ = f; 578 } 579 return (0); 580 } 581 582 unp_internalize(control) 583 struct mbuf *control; 584 { 585 register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 586 register struct file **rp; 587 register struct file *fp; 588 register int i, fd; 589 int oldfds; 590 591 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 592 cm->cmsg_len != control->m_len) 593 return (EINVAL); 594 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 595 rp = (struct file **)(cm + 1); 596 for (i = 0; i < oldfds; i++) { 597 fd = *(int *)rp++; 598 if ((unsigned)fd >= NOFILE || u.u_ofile[fd] == NULL) 599 return (EBADF); 600 } 601 rp = (struct file **)(cm + 1); 602 for (i = 0; i < oldfds; i++) { 603 fp = u.u_ofile[*(int *)rp]; 604 *rp++ = fp; 605 fp->f_count++; 606 fp->f_msgcount++; 607 unp_rights++; 608 } 609 return (0); 610 } 611 612 int unp_defer, unp_gcing; 613 int unp_mark(); 614 extern struct domain unixdomain; 615 616 unp_gc() 617 { 618 register struct file *fp; 619 register struct socket *so; 620 621 if (unp_gcing) 622 return; 623 unp_gcing = 1; 624 restart: 625 unp_defer = 0; 626 for (fp = file; fp < fileNFILE; fp++) 627 fp->f_flag &= ~(FMARK|FDEFER); 628 do { 629 for (fp = file; fp < fileNFILE; fp++) { 630 if (fp->f_count == 0) 631 continue; 632 if (fp->f_flag & FDEFER) { 633 fp->f_flag &= ~FDEFER; 634 unp_defer--; 635 } else { 636 if (fp->f_flag & FMARK) 637 continue; 638 if (fp->f_count == fp->f_msgcount) 639 continue; 640 fp->f_flag |= FMARK; 641 } 642 if (fp->f_type != DTYPE_SOCKET || 643 (so = (struct socket *)fp->f_data) == 0) 644 continue; 645 if (so->so_proto->pr_domain != &unixdomain || 646 (so->so_proto->pr_flags&PR_RIGHTS) == 0) 647 continue; 648 if (so->so_rcv.sb_flags & SB_LOCK) { 649 sbwait(&so->so_rcv); 650 goto restart; 651 } 652 unp_scan(so->so_rcv.sb_mb, unp_mark); 653 } 654 } while (unp_defer); 655 for (fp = file; fp < fileNFILE; fp++) { 656 if (fp->f_count == 0) 657 continue; 658 if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0) 659 while (fp->f_msgcount) 660 unp_discard(fp); 661 } 662 unp_gcing = 0; 663 } 664 665 unp_dispose(m) 666 struct mbuf *m; 667 { 668 int unp_discard(); 669 670 if (m) 671 unp_scan(m, unp_discard); 672 } 673 674 unp_scan(m0, op) 675 register struct mbuf *m0; 676 int (*op)(); 677 { 678 register struct mbuf *m; 679 register struct file **rp; 680 register struct cmsghdr *cm; 681 register int i; 682 int qfds; 683 684 while (m0) { 685 for (m = m0; m; m = m->m_next) 686 if (m->m_type == MT_CONTROL && 687 m->m_len >= sizeof(*cm)) { 688 cm = mtod(m, struct cmsghdr *); 689 if (cm->cmsg_level != SOL_SOCKET || 690 cm->cmsg_type != SCM_RIGHTS) 691 continue; 692 qfds = (cm->cmsg_len - sizeof *cm) 693 / sizeof (struct file *); 694 rp = (struct file **)(cm + 1); 695 for (i = 0; i < qfds; i++) 696 (*op)(*rp++); 697 break; /* XXX, but saves time */ 698 } 699 m0 = m0->m_act; 700 } 701 } 702 703 unp_mark(fp) 704 struct file *fp; 705 { 706 707 if (fp->f_flag & FMARK) 708 return; 709 unp_defer++; 710 fp->f_flag |= (FMARK|FDEFER); 711 } 712 713 unp_discard(fp) 714 struct file *fp; 715 { 716 717 fp->f_msgcount--; 718 unp_rights--; 719 (void) closef(fp); 720 } 721