uipc_usrreq.c revision 133803
1/* 2 * Copyright 2004 Robert N. M. Watson 3 * Copyright (c) 1982, 1986, 1989, 1991, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 4. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/kern/uipc_usrreq.c 133803 2004-08-16 04:41:03Z rwatson $"); 35 36#include "opt_mac.h" 37 38#include <sys/param.h> 39#include <sys/domain.h> 40#include <sys/fcntl.h> 41#include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 42#include <sys/file.h> 43#include <sys/filedesc.h> 44#include <sys/jail.h> 45#include <sys/kernel.h> 46#include <sys/lock.h> 47#include <sys/mac.h> 48#include <sys/mbuf.h> 49#include <sys/mutex.h> 50#include <sys/namei.h> 51#include <sys/proc.h> 52#include <sys/protosw.h> 53#include <sys/resourcevar.h> 54#include <sys/socket.h> 55#include <sys/socketvar.h> 56#include <sys/signalvar.h> 57#include <sys/stat.h> 58#include <sys/sx.h> 59#include <sys/sysctl.h> 60#include <sys/systm.h> 61#include <sys/un.h> 62#include <sys/unpcb.h> 63#include <sys/vnode.h> 64 65#include <vm/uma.h> 66 67static uma_zone_t unp_zone; 68static unp_gen_t unp_gencnt; 69static u_int unp_count; 70 71static struct unp_head unp_shead, unp_dhead; 72 73/* 74 * Unix communications domain. 75 * 76 * TODO: 77 * SEQPACKET, RDM 78 * rethink name space problems 79 * need a proper out-of-band 80 * lock pushdown 81 */ 82static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 83static ino_t unp_ino; /* prototype for fake inode numbers */ 84 85/* 86 * Currently, UNIX domain sockets are protected by a single subsystem lock, 87 * which covers global data structures and variables, the contents of each 88 * per-socket unpcb structure, and the so_pcb field in sockets attached to 89 * the UNIX domain. This provides for a moderate degree of paralellism, as 90 * receive operations on UNIX domain sockets do not need to acquire the 91 * subsystem lock. Finer grained locking to permit send() without acquiring 92 * a global lock would be a logical next step. 93 * 94 * The UNIX domain socket lock preceds all socket layer locks, including the 95 * socket lock and socket buffer lock, permitting UNIX domain socket code to 96 * call into socket support routines without releasing its locks. 97 * 98 * Some caution is required in areas where the UNIX domain socket code enters 99 * VFS in order to create or find rendezvous points. This results in 100 * dropping of the UNIX domain socket subsystem lock, acquisition of the 101 * Giant lock, and potential sleeping. This increases the chances of races, 102 * and exposes weaknesses in the socket->protocol API by offering poor 103 * failure modes. 104 */ 105static struct mtx unp_mtx; 106#define UNP_LOCK_INIT() \ 107 mtx_init(&unp_mtx, "unp", NULL, MTX_DEF) 108#define UNP_LOCK() mtx_lock(&unp_mtx) 109#define UNP_UNLOCK() mtx_unlock(&unp_mtx) 110#define UNP_LOCK_ASSERT() mtx_assert(&unp_mtx, MA_OWNED) 111 112static int unp_attach(struct socket *); 113static void unp_detach(struct unpcb *); 114static int unp_bind(struct unpcb *,struct sockaddr *, struct thread *); 115static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 116static int unp_connect2(struct socket *so, struct socket *so2); 117static void unp_disconnect(struct unpcb *); 118static void unp_shutdown(struct unpcb *); 119static void unp_drop(struct unpcb *, int); 120static void unp_gc(void); 121static void unp_scan(struct mbuf *, void (*)(struct file *)); 122static void unp_mark(struct file *); 123static void unp_discard(struct file *); 124static void unp_freerights(struct file **, int); 125static int unp_internalize(struct mbuf **, struct thread *); 126static int unp_listen(struct unpcb *, struct thread *); 127 128static int 129uipc_abort(struct socket *so) 130{ 131 struct unpcb *unp; 132 133 UNP_LOCK(); 134 unp = sotounpcb(so); 135 if (unp == NULL) { 136 UNP_UNLOCK(); 137 return (EINVAL); 138 } 139 unp_drop(unp, ECONNABORTED); 140 unp_detach(unp); /* NB: unlocks */ 141 SOCK_LOCK(so); 142 sotryfree(so); 143 return (0); 144} 145 146static int 147uipc_accept(struct socket *so, struct sockaddr **nam) 148{ 149 struct unpcb *unp; 150 const struct sockaddr *sa; 151 152 /* 153 * Pass back name of connected socket, 154 * if it was bound and we are still connected 155 * (our peer may have closed already!). 156 */ 157 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 158 UNP_LOCK(); 159 unp = sotounpcb(so); 160 if (unp == NULL) { 161 UNP_UNLOCK(); 162 free(*nam, M_SONAME); 163 *nam = NULL; 164 return (EINVAL); 165 } 166 if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 167 sa = (struct sockaddr *) unp->unp_conn->unp_addr; 168 else 169 sa = &sun_noname; 170 bcopy(sa, *nam, sa->sa_len); 171 UNP_UNLOCK(); 172 return (0); 173} 174 175static int 176uipc_attach(struct socket *so, int proto, struct thread *td) 177{ 178 struct unpcb *unp = sotounpcb(so); 179 180 if (unp != NULL) 181 return (EISCONN); 182 return (unp_attach(so)); 183} 184 185static int 186uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 187{ 188 struct unpcb *unp; 189 int error; 190 191 UNP_LOCK(); 192 unp = sotounpcb(so); 193 if (unp == NULL) { 194 UNP_UNLOCK(); 195 return (EINVAL); 196 } 197 error = unp_bind(unp, nam, td); 198 UNP_UNLOCK(); 199 return (error); 200} 201 202static int 203uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 204{ 205 struct unpcb *unp; 206 int error; 207 208 KASSERT(td == curthread, ("uipc_connect: td != curthread")); 209 210 UNP_LOCK(); 211 unp = sotounpcb(so); 212 if (unp == NULL) { 213 UNP_UNLOCK(); 214 return (EINVAL); 215 } 216 error = unp_connect(so, nam, td); 217 UNP_UNLOCK(); 218 return (error); 219} 220 221int 222uipc_connect2(struct socket *so1, struct socket *so2) 223{ 224 struct unpcb *unp; 225 int error; 226 227 UNP_LOCK(); 228 unp = sotounpcb(so1); 229 if (unp == NULL) { 230 UNP_UNLOCK(); 231 return (EINVAL); 232 } 233 error = unp_connect2(so1, so2); 234 UNP_UNLOCK(); 235 return (error); 236} 237 238/* control is EOPNOTSUPP */ 239 240static int 241uipc_detach(struct socket *so) 242{ 243 struct unpcb *unp; 244 245 UNP_LOCK(); 246 unp = sotounpcb(so); 247 if (unp == NULL) { 248 UNP_UNLOCK(); 249 return (EINVAL); 250 } 251 unp_detach(unp); /* NB: unlocks unp */ 252 return (0); 253} 254 255static int 256uipc_disconnect(struct socket *so) 257{ 258 struct unpcb *unp; 259 260 UNP_LOCK(); 261 unp = sotounpcb(so); 262 if (unp == NULL) { 263 UNP_UNLOCK(); 264 return (EINVAL); 265 } 266 unp_disconnect(unp); 267 UNP_UNLOCK(); 268 return (0); 269} 270 271static int 272uipc_listen(struct socket *so, struct thread *td) 273{ 274 struct unpcb *unp; 275 int error; 276 277 UNP_LOCK(); 278 unp = sotounpcb(so); 279 if (unp == NULL || unp->unp_vnode == NULL) { 280 UNP_UNLOCK(); 281 return (EINVAL); 282 } 283 error = unp_listen(unp, td); 284 UNP_UNLOCK(); 285 return (error); 286} 287 288static int 289uipc_peeraddr(struct socket *so, struct sockaddr **nam) 290{ 291 struct unpcb *unp; 292 const struct sockaddr *sa; 293 294 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 295 UNP_LOCK(); 296 unp = sotounpcb(so); 297 if (unp == NULL) { 298 UNP_UNLOCK(); 299 free(*nam, M_SONAME); 300 *nam = NULL; 301 return (EINVAL); 302 } 303 if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 304 sa = (struct sockaddr *) unp->unp_conn->unp_addr; 305 else { 306 /* 307 * XXX: It seems that this test always fails even when 308 * connection is established. So, this else clause is 309 * added as workaround to return PF_LOCAL sockaddr. 310 */ 311 sa = &sun_noname; 312 } 313 bcopy(sa, *nam, sa->sa_len); 314 UNP_UNLOCK(); 315 return (0); 316} 317 318static int 319uipc_rcvd(struct socket *so, int flags) 320{ 321 struct unpcb *unp; 322 struct socket *so2; 323 u_long newhiwat; 324 325 UNP_LOCK(); 326 unp = sotounpcb(so); 327 if (unp == NULL) { 328 UNP_UNLOCK(); 329 return (EINVAL); 330 } 331 switch (so->so_type) { 332 case SOCK_DGRAM: 333 panic("uipc_rcvd DGRAM?"); 334 /*NOTREACHED*/ 335 336 case SOCK_STREAM: 337 if (unp->unp_conn == NULL) 338 break; 339 so2 = unp->unp_conn->unp_socket; 340 SOCKBUF_LOCK(&so2->so_snd); 341 SOCKBUF_LOCK(&so->so_rcv); 342 /* 343 * Adjust backpressure on sender 344 * and wakeup any waiting to write. 345 */ 346 so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 347 unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 348 newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 349 so->so_rcv.sb_cc; 350 (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 351 newhiwat, RLIM_INFINITY); 352 unp->unp_cc = so->so_rcv.sb_cc; 353 SOCKBUF_UNLOCK(&so->so_rcv); 354 sowwakeup_locked(so2); 355 break; 356 357 default: 358 panic("uipc_rcvd unknown socktype"); 359 } 360 UNP_UNLOCK(); 361 return (0); 362} 363 364/* pru_rcvoob is EOPNOTSUPP */ 365 366static int 367uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 368 struct mbuf *control, struct thread *td) 369{ 370 int error = 0; 371 struct unpcb *unp; 372 struct socket *so2; 373 u_long newhiwat; 374 375 unp = sotounpcb(so); 376 if (unp == NULL) { 377 error = EINVAL; 378 goto release; 379 } 380 if (flags & PRUS_OOB) { 381 error = EOPNOTSUPP; 382 goto release; 383 } 384 385 if (control != NULL && (error = unp_internalize(&control, td))) 386 goto release; 387 388 UNP_LOCK(); 389 unp = sotounpcb(so); 390 if (unp == NULL) { 391 UNP_UNLOCK(); 392 error = EINVAL; 393 goto dispose_release; 394 } 395 396 switch (so->so_type) { 397 case SOCK_DGRAM: 398 { 399 const struct sockaddr *from; 400 401 if (nam != NULL) { 402 if (unp->unp_conn != NULL) { 403 error = EISCONN; 404 break; 405 } 406 error = unp_connect(so, nam, td); 407 if (error) 408 break; 409 } else { 410 if (unp->unp_conn == NULL) { 411 error = ENOTCONN; 412 break; 413 } 414 } 415 so2 = unp->unp_conn->unp_socket; 416 if (unp->unp_addr != NULL) 417 from = (struct sockaddr *)unp->unp_addr; 418 else 419 from = &sun_noname; 420 SOCKBUF_LOCK(&so2->so_rcv); 421 if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 422 sorwakeup_locked(so2); 423 m = NULL; 424 control = NULL; 425 } else { 426 SOCKBUF_UNLOCK(&so2->so_rcv); 427 error = ENOBUFS; 428 } 429 if (nam != NULL) 430 unp_disconnect(unp); 431 break; 432 } 433 434 case SOCK_STREAM: 435 /* Connect if not connected yet. */ 436 /* 437 * Note: A better implementation would complain 438 * if not equal to the peer's address. 439 */ 440 if ((so->so_state & SS_ISCONNECTED) == 0) { 441 if (nam != NULL) { 442 error = unp_connect(so, nam, td); 443 if (error) 444 break; /* XXX */ 445 } else { 446 error = ENOTCONN; 447 break; 448 } 449 } 450 451 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 452 error = EPIPE; 453 break; 454 } 455 if (unp->unp_conn == NULL) 456 panic("uipc_send connected but no connection?"); 457 so2 = unp->unp_conn->unp_socket; 458 SOCKBUF_LOCK(&so2->so_rcv); 459 /* 460 * Send to paired receive port, and then reduce 461 * send buffer hiwater marks to maintain backpressure. 462 * Wake up readers. 463 */ 464 if (control != NULL) { 465 if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 466 control = NULL; 467 } else { 468 sbappend_locked(&so2->so_rcv, m); 469 } 470 so->so_snd.sb_mbmax -= 471 so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 472 unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 473 newhiwat = so->so_snd.sb_hiwat - 474 (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 475 (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 476 newhiwat, RLIM_INFINITY); 477 unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 478 sorwakeup_locked(so2); 479 m = NULL; 480 break; 481 482 default: 483 panic("uipc_send unknown socktype"); 484 } 485 486 /* 487 * SEND_EOF is equivalent to a SEND followed by 488 * a SHUTDOWN. 489 */ 490 if (flags & PRUS_EOF) { 491 socantsendmore(so); 492 unp_shutdown(unp); 493 } 494 UNP_UNLOCK(); 495 496dispose_release: 497 if (control != NULL && error != 0) 498 unp_dispose(control); 499 500release: 501 if (control != NULL) 502 m_freem(control); 503 if (m != NULL) 504 m_freem(m); 505 return (error); 506} 507 508static int 509uipc_sense(struct socket *so, struct stat *sb) 510{ 511 struct unpcb *unp; 512 struct socket *so2; 513 514 UNP_LOCK(); 515 unp = sotounpcb(so); 516 if (unp == NULL) { 517 UNP_UNLOCK(); 518 return (EINVAL); 519 } 520 sb->st_blksize = so->so_snd.sb_hiwat; 521 if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 522 so2 = unp->unp_conn->unp_socket; 523 sb->st_blksize += so2->so_rcv.sb_cc; 524 } 525 sb->st_dev = NODEV; 526 if (unp->unp_ino == 0) 527 unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 528 sb->st_ino = unp->unp_ino; 529 UNP_UNLOCK(); 530 return (0); 531} 532 533static int 534uipc_shutdown(struct socket *so) 535{ 536 struct unpcb *unp; 537 538 UNP_LOCK(); 539 unp = sotounpcb(so); 540 if (unp == NULL) { 541 UNP_UNLOCK(); 542 return (EINVAL); 543 } 544 socantsendmore(so); 545 unp_shutdown(unp); 546 UNP_UNLOCK(); 547 return (0); 548} 549 550static int 551uipc_sockaddr(struct socket *so, struct sockaddr **nam) 552{ 553 struct unpcb *unp; 554 const struct sockaddr *sa; 555 556 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 557 UNP_LOCK(); 558 unp = sotounpcb(so); 559 if (unp == NULL) { 560 UNP_UNLOCK(); 561 free(*nam, M_SONAME); 562 *nam = NULL; 563 return (EINVAL); 564 } 565 if (unp->unp_addr != NULL) 566 sa = (struct sockaddr *) unp->unp_addr; 567 else 568 sa = &sun_noname; 569 bcopy(sa, *nam, sa->sa_len); 570 UNP_UNLOCK(); 571 return (0); 572} 573 574struct pr_usrreqs uipc_usrreqs = { 575 uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 576 uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 577 uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 578 uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 579 sosend, soreceive, sopoll, pru_sosetlabel_null 580}; 581 582int 583uipc_ctloutput(so, sopt) 584 struct socket *so; 585 struct sockopt *sopt; 586{ 587 struct unpcb *unp; 588 struct xucred xu; 589 int error; 590 591 switch (sopt->sopt_dir) { 592 case SOPT_GET: 593 switch (sopt->sopt_name) { 594 case LOCAL_PEERCRED: 595 error = 0; 596 UNP_LOCK(); 597 unp = sotounpcb(so); 598 if (unp == NULL) { 599 UNP_UNLOCK(); 600 error = EINVAL; 601 break; 602 } 603 if (unp->unp_flags & UNP_HAVEPC) 604 xu = unp->unp_peercred; 605 else { 606 if (so->so_type == SOCK_STREAM) 607 error = ENOTCONN; 608 else 609 error = EINVAL; 610 } 611 UNP_UNLOCK(); 612 if (error == 0) 613 error = sooptcopyout(sopt, &xu, sizeof(xu)); 614 break; 615 default: 616 error = EOPNOTSUPP; 617 break; 618 } 619 break; 620 case SOPT_SET: 621 default: 622 error = EOPNOTSUPP; 623 break; 624 } 625 return (error); 626} 627 628/* 629 * Both send and receive buffers are allocated PIPSIZ bytes of buffering 630 * for stream sockets, although the total for sender and receiver is 631 * actually only PIPSIZ. 632 * Datagram sockets really use the sendspace as the maximum datagram size, 633 * and don't really want to reserve the sendspace. Their recvspace should 634 * be large enough for at least one max-size datagram plus address. 635 */ 636#ifndef PIPSIZ 637#define PIPSIZ 8192 638#endif 639static u_long unpst_sendspace = PIPSIZ; 640static u_long unpst_recvspace = PIPSIZ; 641static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 642static u_long unpdg_recvspace = 4*1024; 643 644static int unp_rights; /* file descriptors in flight */ 645 646SYSCTL_DECL(_net_local_stream); 647SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 648 &unpst_sendspace, 0, ""); 649SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 650 &unpst_recvspace, 0, ""); 651SYSCTL_DECL(_net_local_dgram); 652SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 653 &unpdg_sendspace, 0, ""); 654SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 655 &unpdg_recvspace, 0, ""); 656SYSCTL_DECL(_net_local); 657SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 658 659static int 660unp_attach(so) 661 struct socket *so; 662{ 663 register struct unpcb *unp; 664 int error; 665 666 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 667 switch (so->so_type) { 668 669 case SOCK_STREAM: 670 error = soreserve(so, unpst_sendspace, unpst_recvspace); 671 break; 672 673 case SOCK_DGRAM: 674 error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 675 break; 676 677 default: 678 panic("unp_attach"); 679 } 680 if (error) 681 return (error); 682 } 683 unp = uma_zalloc(unp_zone, M_WAITOK); 684 if (unp == NULL) 685 return (ENOBUFS); 686 bzero(unp, sizeof *unp); 687 LIST_INIT(&unp->unp_refs); 688 unp->unp_socket = so; 689 690 UNP_LOCK(); 691 unp->unp_gencnt = ++unp_gencnt; 692 unp_count++; 693 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 694 : &unp_shead, unp, unp_link); 695 so->so_pcb = unp; 696 UNP_UNLOCK(); 697 698 return (0); 699} 700 701static void 702unp_detach(unp) 703 register struct unpcb *unp; 704{ 705 struct vnode *vp; 706 707 UNP_LOCK_ASSERT(); 708 709 LIST_REMOVE(unp, unp_link); 710 unp->unp_gencnt = ++unp_gencnt; 711 --unp_count; 712 if ((vp = unp->unp_vnode) != NULL) { 713 /* 714 * XXXRW: should v_socket be frobbed only while holding 715 * Giant? 716 */ 717 unp->unp_vnode->v_socket = NULL; 718 unp->unp_vnode = NULL; 719 } 720 if (unp->unp_conn != NULL) 721 unp_disconnect(unp); 722 while (!LIST_EMPTY(&unp->unp_refs)) { 723 struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 724 unp_drop(ref, ECONNRESET); 725 } 726 soisdisconnected(unp->unp_socket); 727 unp->unp_socket->so_pcb = NULL; 728 if (unp_rights) { 729 /* 730 * Normally the receive buffer is flushed later, 731 * in sofree, but if our receive buffer holds references 732 * to descriptors that are now garbage, we will dispose 733 * of those descriptor references after the garbage collector 734 * gets them (resulting in a "panic: closef: count < 0"). 735 */ 736 sorflush(unp->unp_socket); 737 unp_gc(); 738 } 739 UNP_UNLOCK(); 740 if (unp->unp_addr != NULL) 741 FREE(unp->unp_addr, M_SONAME); 742 uma_zfree(unp_zone, unp); 743 if (vp) { 744 mtx_lock(&Giant); 745 vrele(vp); 746 mtx_unlock(&Giant); 747 } 748} 749 750static int 751unp_bind(unp, nam, td) 752 struct unpcb *unp; 753 struct sockaddr *nam; 754 struct thread *td; 755{ 756 struct sockaddr_un *soun = (struct sockaddr_un *)nam; 757 struct vnode *vp; 758 struct mount *mp; 759 struct vattr vattr; 760 int error, namelen; 761 struct nameidata nd; 762 char *buf; 763 764 UNP_LOCK_ASSERT(); 765 766 /* 767 * XXXRW: This test-and-set of unp_vnode is non-atomic; the 768 * unlocked read here is fine, but the value of unp_vnode needs 769 * to be tested again after we do all the lookups to see if the 770 * pcb is still unbound? 771 */ 772 if (unp->unp_vnode != NULL) 773 return (EINVAL); 774 775 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 776 if (namelen <= 0) 777 return (EINVAL); 778 779 UNP_UNLOCK(); 780 781 buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 782 strlcpy(buf, soun->sun_path, namelen + 1); 783 784 mtx_lock(&Giant); 785restart: 786 mtx_assert(&Giant, MA_OWNED); 787 NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 788 buf, td); 789/* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 790 error = namei(&nd); 791 if (error) 792 goto done; 793 vp = nd.ni_vp; 794 if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 795 NDFREE(&nd, NDF_ONLY_PNBUF); 796 if (nd.ni_dvp == vp) 797 vrele(nd.ni_dvp); 798 else 799 vput(nd.ni_dvp); 800 if (vp != NULL) { 801 vrele(vp); 802 error = EADDRINUSE; 803 goto done; 804 } 805 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 806 if (error) 807 goto done; 808 goto restart; 809 } 810 VATTR_NULL(&vattr); 811 vattr.va_type = VSOCK; 812 vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 813#ifdef MAC 814 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 815 &vattr); 816#endif 817 if (error == 0) { 818 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 819 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 820 } 821 NDFREE(&nd, NDF_ONLY_PNBUF); 822 vput(nd.ni_dvp); 823 if (error) 824 goto done; 825 vp = nd.ni_vp; 826 ASSERT_VOP_LOCKED(vp, "unp_bind"); 827 soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 828 UNP_LOCK(); 829 vp->v_socket = unp->unp_socket; 830 unp->unp_vnode = vp; 831 unp->unp_addr = soun; 832 UNP_UNLOCK(); 833 VOP_UNLOCK(vp, 0, td); 834 vn_finished_write(mp); 835done: 836 mtx_unlock(&Giant); 837 free(buf, M_TEMP); 838 UNP_LOCK(); 839 return (error); 840} 841 842static int 843unp_connect(so, nam, td) 844 struct socket *so; 845 struct sockaddr *nam; 846 struct thread *td; 847{ 848 register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 849 register struct vnode *vp; 850 register struct socket *so2, *so3; 851 struct unpcb *unp, *unp2, *unp3; 852 int error, len; 853 struct nameidata nd; 854 char buf[SOCK_MAXADDRLEN]; 855 struct sockaddr *sa; 856 857 UNP_LOCK_ASSERT(); 858 unp = sotounpcb(so); 859 860 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 861 if (len <= 0) 862 return (EINVAL); 863 strlcpy(buf, soun->sun_path, len + 1); 864 UNP_UNLOCK(); 865 sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 866 mtx_lock(&Giant); 867 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 868 error = namei(&nd); 869 if (error) 870 vp = NULL; 871 else 872 vp = nd.ni_vp; 873 ASSERT_VOP_LOCKED(vp, "unp_connect"); 874 NDFREE(&nd, NDF_ONLY_PNBUF); 875 if (error) 876 goto bad; 877 878 if (vp->v_type != VSOCK) { 879 error = ENOTSOCK; 880 goto bad; 881 } 882 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 883 if (error) 884 goto bad; 885 mtx_unlock(&Giant); 886 UNP_LOCK(); 887 unp = sotounpcb(so); 888 if (unp == NULL) { 889 /* 890 * XXXRW: Temporary debugging printf. 891 */ 892 printf("unp_connect(): lost race to another thread\n"); 893 error = EINVAL; 894 goto bad2; 895 } 896 so2 = vp->v_socket; 897 if (so2 == NULL) { 898 error = ECONNREFUSED; 899 goto bad2; 900 } 901 if (so->so_type != so2->so_type) { 902 error = EPROTOTYPE; 903 goto bad2; 904 } 905 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 906 if (so2->so_options & SO_ACCEPTCONN) { 907 /* 908 * NB: drop locks here so unp_attach is entered 909 * w/o locks; this avoids a recursive lock 910 * of the head and holding sleep locks across 911 * a (potentially) blocking malloc. 912 */ 913 UNP_UNLOCK(); 914 so3 = sonewconn(so2, 0); 915 UNP_LOCK(); 916 } else 917 so3 = NULL; 918 if (so3 == NULL) { 919 error = ECONNREFUSED; 920 goto bad2; 921 } 922 unp = sotounpcb(so); 923 unp2 = sotounpcb(so2); 924 unp3 = sotounpcb(so3); 925 if (unp2->unp_addr != NULL) { 926 bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 927 unp3->unp_addr = (struct sockaddr_un *) sa; 928 sa = NULL; 929 } 930 /* 931 * unp_peercred management: 932 * 933 * The connecter's (client's) credentials are copied 934 * from its process structure at the time of connect() 935 * (which is now). 936 */ 937 cru2x(td->td_ucred, &unp3->unp_peercred); 938 unp3->unp_flags |= UNP_HAVEPC; 939 /* 940 * The receiver's (server's) credentials are copied 941 * from the unp_peercred member of socket on which the 942 * former called listen(); unp_listen() cached that 943 * process's credentials at that time so we can use 944 * them now. 945 */ 946 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 947 ("unp_connect: listener without cached peercred")); 948 memcpy(&unp->unp_peercred, &unp2->unp_peercred, 949 sizeof(unp->unp_peercred)); 950 unp->unp_flags |= UNP_HAVEPC; 951#ifdef MAC 952 SOCK_LOCK(so); 953 mac_set_socket_peer_from_socket(so, so3); 954 mac_set_socket_peer_from_socket(so3, so); 955 SOCK_UNLOCK(so); 956#endif 957 958 so2 = so3; 959 } 960 error = unp_connect2(so, so2); 961bad2: 962 UNP_UNLOCK(); 963 mtx_lock(&Giant); 964bad: 965 mtx_assert(&Giant, MA_OWNED); 966 if (vp != NULL) 967 vput(vp); 968 mtx_unlock(&Giant); 969 free(sa, M_SONAME); 970 UNP_LOCK(); 971 return (error); 972} 973 974static int 975unp_connect2(so, so2) 976 register struct socket *so; 977 register struct socket *so2; 978{ 979 register struct unpcb *unp = sotounpcb(so); 980 register struct unpcb *unp2; 981 982 UNP_LOCK_ASSERT(); 983 984 if (so2->so_type != so->so_type) 985 return (EPROTOTYPE); 986 unp2 = sotounpcb(so2); 987 unp->unp_conn = unp2; 988 switch (so->so_type) { 989 990 case SOCK_DGRAM: 991 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 992 soisconnected(so); 993 break; 994 995 case SOCK_STREAM: 996 unp2->unp_conn = unp; 997 soisconnected(so); 998 soisconnected(so2); 999 break; 1000 1001 default: 1002 panic("unp_connect2"); 1003 } 1004 return (0); 1005} 1006 1007static void 1008unp_disconnect(unp) 1009 struct unpcb *unp; 1010{ 1011 register struct unpcb *unp2 = unp->unp_conn; 1012 struct socket *so; 1013 1014 UNP_LOCK_ASSERT(); 1015 1016 if (unp2 == NULL) 1017 return; 1018 unp->unp_conn = NULL; 1019 switch (unp->unp_socket->so_type) { 1020 1021 case SOCK_DGRAM: 1022 LIST_REMOVE(unp, unp_reflink); 1023 so = unp->unp_socket; 1024 SOCK_LOCK(so); 1025 so->so_state &= ~SS_ISCONNECTED; 1026 SOCK_UNLOCK(so); 1027 break; 1028 1029 case SOCK_STREAM: 1030 soisdisconnected(unp->unp_socket); 1031 unp2->unp_conn = NULL; 1032 soisdisconnected(unp2->unp_socket); 1033 break; 1034 } 1035} 1036 1037#ifdef notdef 1038void 1039unp_abort(unp) 1040 struct unpcb *unp; 1041{ 1042 1043 unp_detach(unp); 1044} 1045#endif 1046 1047/* 1048 * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 1049 * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 1050 * are safe to reference. It first scans the list of struct unpcb's to 1051 * generate a pointer list, then it rescans its list one entry at a time to 1052 * externalize and copyout. It checks the generation number to see if a 1053 * struct unpcb has been reused, and will skip it if so. 1054 */ 1055static int 1056unp_pcblist(SYSCTL_HANDLER_ARGS) 1057{ 1058 int error, i, n; 1059 struct unpcb *unp, **unp_list; 1060 unp_gen_t gencnt; 1061 struct xunpgen *xug; 1062 struct unp_head *head; 1063 struct xunpcb *xu; 1064 1065 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 1066 1067 /* 1068 * The process of preparing the PCB list is too time-consuming and 1069 * resource-intensive to repeat twice on every request. 1070 */ 1071 if (req->oldptr == NULL) { 1072 n = unp_count; 1073 req->oldidx = 2 * (sizeof *xug) 1074 + (n + n/8) * sizeof(struct xunpcb); 1075 return (0); 1076 } 1077 1078 if (req->newptr != NULL) 1079 return (EPERM); 1080 1081 /* 1082 * OK, now we're committed to doing something. 1083 */ 1084 xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 1085 UNP_LOCK(); 1086 gencnt = unp_gencnt; 1087 n = unp_count; 1088 UNP_UNLOCK(); 1089 1090 xug->xug_len = sizeof *xug; 1091 xug->xug_count = n; 1092 xug->xug_gen = gencnt; 1093 xug->xug_sogen = so_gencnt; 1094 error = SYSCTL_OUT(req, xug, sizeof *xug); 1095 if (error) { 1096 free(xug, M_TEMP); 1097 return (error); 1098 } 1099 1100 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 1101 1102 UNP_LOCK(); 1103 for (unp = LIST_FIRST(head), i = 0; unp && i < n; 1104 unp = LIST_NEXT(unp, unp_link)) { 1105 if (unp->unp_gencnt <= gencnt) { 1106 if (cr_cansee(req->td->td_ucred, 1107 unp->unp_socket->so_cred)) 1108 continue; 1109 unp_list[i++] = unp; 1110 } 1111 } 1112 UNP_UNLOCK(); 1113 n = i; /* in case we lost some during malloc */ 1114 1115 error = 0; 1116 xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 1117 for (i = 0; i < n; i++) { 1118 unp = unp_list[i]; 1119 if (unp->unp_gencnt <= gencnt) { 1120 xu->xu_len = sizeof *xu; 1121 xu->xu_unpp = unp; 1122 /* 1123 * XXX - need more locking here to protect against 1124 * connect/disconnect races for SMP. 1125 */ 1126 if (unp->unp_addr != NULL) 1127 bcopy(unp->unp_addr, &xu->xu_addr, 1128 unp->unp_addr->sun_len); 1129 if (unp->unp_conn != NULL && 1130 unp->unp_conn->unp_addr != NULL) 1131 bcopy(unp->unp_conn->unp_addr, 1132 &xu->xu_caddr, 1133 unp->unp_conn->unp_addr->sun_len); 1134 bcopy(unp, &xu->xu_unp, sizeof *unp); 1135 sotoxsocket(unp->unp_socket, &xu->xu_socket); 1136 error = SYSCTL_OUT(req, xu, sizeof *xu); 1137 } 1138 } 1139 free(xu, M_TEMP); 1140 if (!error) { 1141 /* 1142 * Give the user an updated idea of our state. 1143 * If the generation differs from what we told 1144 * her before, she knows that something happened 1145 * while we were processing this request, and it 1146 * might be necessary to retry. 1147 */ 1148 xug->xug_gen = unp_gencnt; 1149 xug->xug_sogen = so_gencnt; 1150 xug->xug_count = unp_count; 1151 error = SYSCTL_OUT(req, xug, sizeof *xug); 1152 } 1153 free(unp_list, M_TEMP); 1154 free(xug, M_TEMP); 1155 return (error); 1156} 1157 1158SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 1159 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 1160 "List of active local datagram sockets"); 1161SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 1162 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 1163 "List of active local stream sockets"); 1164 1165static void 1166unp_shutdown(unp) 1167 struct unpcb *unp; 1168{ 1169 struct socket *so; 1170 1171 UNP_LOCK_ASSERT(); 1172 1173 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1174 (so = unp->unp_conn->unp_socket)) 1175 socantrcvmore(so); 1176} 1177 1178static void 1179unp_drop(unp, errno) 1180 struct unpcb *unp; 1181 int errno; 1182{ 1183 struct socket *so = unp->unp_socket; 1184 1185 UNP_LOCK_ASSERT(); 1186 1187 so->so_error = errno; 1188 unp_disconnect(unp); 1189} 1190 1191#ifdef notdef 1192void 1193unp_drain() 1194{ 1195 1196} 1197#endif 1198 1199static void 1200unp_freerights(rp, fdcount) 1201 struct file **rp; 1202 int fdcount; 1203{ 1204 int i; 1205 struct file *fp; 1206 1207 for (i = 0; i < fdcount; i++) { 1208 fp = *rp; 1209 /* 1210 * zero the pointer before calling 1211 * unp_discard since it may end up 1212 * in unp_gc().. 1213 */ 1214 *rp++ = 0; 1215 unp_discard(fp); 1216 } 1217} 1218 1219int 1220unp_externalize(control, controlp) 1221 struct mbuf *control, **controlp; 1222{ 1223 struct thread *td = curthread; /* XXX */ 1224 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1225 int i; 1226 int *fdp; 1227 struct file **rp; 1228 struct file *fp; 1229 void *data; 1230 socklen_t clen = control->m_len, datalen; 1231 int error, newfds; 1232 int f; 1233 u_int newlen; 1234 1235 error = 0; 1236 if (controlp != NULL) /* controlp == NULL => free control messages */ 1237 *controlp = NULL; 1238 1239 while (cm != NULL) { 1240 if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 1241 error = EINVAL; 1242 break; 1243 } 1244 1245 data = CMSG_DATA(cm); 1246 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1247 1248 if (cm->cmsg_level == SOL_SOCKET 1249 && cm->cmsg_type == SCM_RIGHTS) { 1250 newfds = datalen / sizeof(struct file *); 1251 rp = data; 1252 1253 /* If we're not outputting the descriptors free them. */ 1254 if (error || controlp == NULL) { 1255 unp_freerights(rp, newfds); 1256 goto next; 1257 } 1258 FILEDESC_LOCK(td->td_proc->p_fd); 1259 /* if the new FD's will not fit free them. */ 1260 if (!fdavail(td, newfds)) { 1261 FILEDESC_UNLOCK(td->td_proc->p_fd); 1262 error = EMSGSIZE; 1263 unp_freerights(rp, newfds); 1264 goto next; 1265 } 1266 /* 1267 * now change each pointer to an fd in the global 1268 * table to an integer that is the index to the 1269 * local fd table entry that we set up to point 1270 * to the global one we are transferring. 1271 */ 1272 newlen = newfds * sizeof(int); 1273 *controlp = sbcreatecontrol(NULL, newlen, 1274 SCM_RIGHTS, SOL_SOCKET); 1275 if (*controlp == NULL) { 1276 FILEDESC_UNLOCK(td->td_proc->p_fd); 1277 error = E2BIG; 1278 unp_freerights(rp, newfds); 1279 goto next; 1280 } 1281 1282 fdp = (int *) 1283 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1284 for (i = 0; i < newfds; i++) { 1285 if (fdalloc(td, 0, &f)) 1286 panic("unp_externalize fdalloc failed"); 1287 fp = *rp++; 1288 td->td_proc->p_fd->fd_ofiles[f] = fp; 1289 FILE_LOCK(fp); 1290 fp->f_msgcount--; 1291 FILE_UNLOCK(fp); 1292 unp_rights--; 1293 *fdp++ = f; 1294 } 1295 FILEDESC_UNLOCK(td->td_proc->p_fd); 1296 } else { /* We can just copy anything else across */ 1297 if (error || controlp == NULL) 1298 goto next; 1299 *controlp = sbcreatecontrol(NULL, datalen, 1300 cm->cmsg_type, cm->cmsg_level); 1301 if (*controlp == NULL) { 1302 error = ENOBUFS; 1303 goto next; 1304 } 1305 bcopy(data, 1306 CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 1307 datalen); 1308 } 1309 1310 controlp = &(*controlp)->m_next; 1311 1312next: 1313 if (CMSG_SPACE(datalen) < clen) { 1314 clen -= CMSG_SPACE(datalen); 1315 cm = (struct cmsghdr *) 1316 ((caddr_t)cm + CMSG_SPACE(datalen)); 1317 } else { 1318 clen = 0; 1319 cm = NULL; 1320 } 1321 } 1322 1323 m_freem(control); 1324 1325 return (error); 1326} 1327 1328void 1329unp_init(void) 1330{ 1331 unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 1332 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1333 if (unp_zone == NULL) 1334 panic("unp_init"); 1335 uma_zone_set_max(unp_zone, nmbclusters); 1336 LIST_INIT(&unp_dhead); 1337 LIST_INIT(&unp_shead); 1338 1339 UNP_LOCK_INIT(); 1340} 1341 1342static int 1343unp_internalize(controlp, td) 1344 struct mbuf **controlp; 1345 struct thread *td; 1346{ 1347 struct mbuf *control = *controlp; 1348 struct proc *p = td->td_proc; 1349 struct filedesc *fdescp = p->p_fd; 1350 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1351 struct cmsgcred *cmcred; 1352 struct file **rp; 1353 struct file *fp; 1354 struct timeval *tv; 1355 int i, fd, *fdp; 1356 void *data; 1357 socklen_t clen = control->m_len, datalen; 1358 int error, oldfds; 1359 u_int newlen; 1360 1361 error = 0; 1362 *controlp = NULL; 1363 1364 while (cm != NULL) { 1365 if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 1366 || cm->cmsg_len > clen) { 1367 error = EINVAL; 1368 goto out; 1369 } 1370 1371 data = CMSG_DATA(cm); 1372 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1373 1374 switch (cm->cmsg_type) { 1375 /* 1376 * Fill in credential information. 1377 */ 1378 case SCM_CREDS: 1379 *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 1380 SCM_CREDS, SOL_SOCKET); 1381 if (*controlp == NULL) { 1382 error = ENOBUFS; 1383 goto out; 1384 } 1385 1386 cmcred = (struct cmsgcred *) 1387 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1388 cmcred->cmcred_pid = p->p_pid; 1389 cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1390 cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1391 cmcred->cmcred_euid = td->td_ucred->cr_uid; 1392 cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 1393 CMGROUP_MAX); 1394 for (i = 0; i < cmcred->cmcred_ngroups; i++) 1395 cmcred->cmcred_groups[i] = 1396 td->td_ucred->cr_groups[i]; 1397 break; 1398 1399 case SCM_RIGHTS: 1400 oldfds = datalen / sizeof (int); 1401 /* 1402 * check that all the FDs passed in refer to legal files 1403 * If not, reject the entire operation. 1404 */ 1405 fdp = data; 1406 FILEDESC_LOCK(fdescp); 1407 for (i = 0; i < oldfds; i++) { 1408 fd = *fdp++; 1409 if ((unsigned)fd >= fdescp->fd_nfiles || 1410 fdescp->fd_ofiles[fd] == NULL) { 1411 FILEDESC_UNLOCK(fdescp); 1412 error = EBADF; 1413 goto out; 1414 } 1415 fp = fdescp->fd_ofiles[fd]; 1416 if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1417 FILEDESC_UNLOCK(fdescp); 1418 error = EOPNOTSUPP; 1419 goto out; 1420 } 1421 1422 } 1423 /* 1424 * Now replace the integer FDs with pointers to 1425 * the associated global file table entry.. 1426 */ 1427 newlen = oldfds * sizeof(struct file *); 1428 *controlp = sbcreatecontrol(NULL, newlen, 1429 SCM_RIGHTS, SOL_SOCKET); 1430 if (*controlp == NULL) { 1431 FILEDESC_UNLOCK(fdescp); 1432 error = E2BIG; 1433 goto out; 1434 } 1435 1436 fdp = data; 1437 rp = (struct file **) 1438 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1439 for (i = 0; i < oldfds; i++) { 1440 fp = fdescp->fd_ofiles[*fdp++]; 1441 *rp++ = fp; 1442 FILE_LOCK(fp); 1443 fp->f_count++; 1444 fp->f_msgcount++; 1445 FILE_UNLOCK(fp); 1446 unp_rights++; 1447 } 1448 FILEDESC_UNLOCK(fdescp); 1449 break; 1450 1451 case SCM_TIMESTAMP: 1452 *controlp = sbcreatecontrol(NULL, sizeof(*tv), 1453 SCM_TIMESTAMP, SOL_SOCKET); 1454 if (*controlp == NULL) { 1455 error = ENOBUFS; 1456 goto out; 1457 } 1458 tv = (struct timeval *) 1459 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1460 microtime(tv); 1461 break; 1462 1463 default: 1464 error = EINVAL; 1465 goto out; 1466 } 1467 1468 controlp = &(*controlp)->m_next; 1469 1470 if (CMSG_SPACE(datalen) < clen) { 1471 clen -= CMSG_SPACE(datalen); 1472 cm = (struct cmsghdr *) 1473 ((caddr_t)cm + CMSG_SPACE(datalen)); 1474 } else { 1475 clen = 0; 1476 cm = NULL; 1477 } 1478 } 1479 1480out: 1481 m_freem(control); 1482 1483 return (error); 1484} 1485 1486static int unp_defer, unp_gcing; 1487 1488static void 1489unp_gc() 1490{ 1491 register struct file *fp, *nextfp; 1492 register struct socket *so; 1493 struct file **extra_ref, **fpp; 1494 int nunref, i; 1495 int nfiles_snap; 1496 int nfiles_slack = 20; 1497 1498 UNP_LOCK_ASSERT(); 1499 1500 if (unp_gcing) 1501 return; 1502 unp_gcing = 1; 1503 unp_defer = 0; 1504 /* 1505 * before going through all this, set all FDs to 1506 * be NOT defered and NOT externally accessible 1507 */ 1508 /* 1509 * XXXRW: Acquiring a sleep lock while holding UNP 1510 * mutex cannot be a good thing. 1511 */ 1512 sx_slock(&filelist_lock); 1513 LIST_FOREACH(fp, &filehead, f_list) 1514 fp->f_gcflag &= ~(FMARK|FDEFER); 1515 do { 1516 LIST_FOREACH(fp, &filehead, f_list) { 1517 FILE_LOCK(fp); 1518 /* 1519 * If the file is not open, skip it 1520 */ 1521 if (fp->f_count == 0) { 1522 FILE_UNLOCK(fp); 1523 continue; 1524 } 1525 /* 1526 * If we already marked it as 'defer' in a 1527 * previous pass, then try process it this time 1528 * and un-mark it 1529 */ 1530 if (fp->f_gcflag & FDEFER) { 1531 fp->f_gcflag &= ~FDEFER; 1532 unp_defer--; 1533 } else { 1534 /* 1535 * if it's not defered, then check if it's 1536 * already marked.. if so skip it 1537 */ 1538 if (fp->f_gcflag & FMARK) { 1539 FILE_UNLOCK(fp); 1540 continue; 1541 } 1542 /* 1543 * If all references are from messages 1544 * in transit, then skip it. it's not 1545 * externally accessible. 1546 */ 1547 if (fp->f_count == fp->f_msgcount) { 1548 FILE_UNLOCK(fp); 1549 continue; 1550 } 1551 /* 1552 * If it got this far then it must be 1553 * externally accessible. 1554 */ 1555 fp->f_gcflag |= FMARK; 1556 } 1557 /* 1558 * either it was defered, or it is externally 1559 * accessible and not already marked so. 1560 * Now check if it is possibly one of OUR sockets. 1561 */ 1562 if (fp->f_type != DTYPE_SOCKET || 1563 (so = fp->f_data) == NULL) { 1564 FILE_UNLOCK(fp); 1565 continue; 1566 } 1567 FILE_UNLOCK(fp); 1568 if (so->so_proto->pr_domain != &localdomain || 1569 (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1570 continue; 1571#ifdef notdef 1572 if (so->so_rcv.sb_flags & SB_LOCK) { 1573 /* 1574 * This is problematical; it's not clear 1575 * we need to wait for the sockbuf to be 1576 * unlocked (on a uniprocessor, at least), 1577 * and it's also not clear what to do 1578 * if sbwait returns an error due to receipt 1579 * of a signal. If sbwait does return 1580 * an error, we'll go into an infinite 1581 * loop. Delete all of this for now. 1582 */ 1583 (void) sbwait(&so->so_rcv); 1584 goto restart; 1585 } 1586#endif 1587 /* 1588 * So, Ok, it's one of our sockets and it IS externally 1589 * accessible (or was defered). Now we look 1590 * to see if we hold any file descriptors in its 1591 * message buffers. Follow those links and mark them 1592 * as accessible too. 1593 */ 1594 SOCKBUF_LOCK(&so->so_rcv); 1595 unp_scan(so->so_rcv.sb_mb, unp_mark); 1596 SOCKBUF_UNLOCK(&so->so_rcv); 1597 } 1598 } while (unp_defer); 1599 sx_sunlock(&filelist_lock); 1600 /* 1601 * We grab an extra reference to each of the file table entries 1602 * that are not otherwise accessible and then free the rights 1603 * that are stored in messages on them. 1604 * 1605 * The bug in the orginal code is a little tricky, so I'll describe 1606 * what's wrong with it here. 1607 * 1608 * It is incorrect to simply unp_discard each entry for f_msgcount 1609 * times -- consider the case of sockets A and B that contain 1610 * references to each other. On a last close of some other socket, 1611 * we trigger a gc since the number of outstanding rights (unp_rights) 1612 * is non-zero. If during the sweep phase the gc code un_discards, 1613 * we end up doing a (full) closef on the descriptor. A closef on A 1614 * results in the following chain. Closef calls soo_close, which 1615 * calls soclose. Soclose calls first (through the switch 1616 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1617 * returns because the previous instance had set unp_gcing, and 1618 * we return all the way back to soclose, which marks the socket 1619 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1620 * to free up the rights that are queued in messages on the socket A, 1621 * i.e., the reference on B. The sorflush calls via the dom_dispose 1622 * switch unp_dispose, which unp_scans with unp_discard. This second 1623 * instance of unp_discard just calls closef on B. 1624 * 1625 * Well, a similar chain occurs on B, resulting in a sorflush on B, 1626 * which results in another closef on A. Unfortunately, A is already 1627 * being closed, and the descriptor has already been marked with 1628 * SS_NOFDREF, and soclose panics at this point. 1629 * 1630 * Here, we first take an extra reference to each inaccessible 1631 * descriptor. Then, we call sorflush ourself, since we know 1632 * it is a Unix domain socket anyhow. After we destroy all the 1633 * rights carried in messages, we do a last closef to get rid 1634 * of our extra reference. This is the last close, and the 1635 * unp_detach etc will shut down the socket. 1636 * 1637 * 91/09/19, bsy@cs.cmu.edu 1638 */ 1639again: 1640 nfiles_snap = nfiles + nfiles_slack; /* some slack */ 1641 extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 1642 M_WAITOK); 1643 sx_slock(&filelist_lock); 1644 if (nfiles_snap < nfiles) { 1645 sx_sunlock(&filelist_lock); 1646 free(extra_ref, M_TEMP); 1647 nfiles_slack += 20; 1648 goto again; 1649 } 1650 for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1651 fp != NULL; fp = nextfp) { 1652 nextfp = LIST_NEXT(fp, f_list); 1653 FILE_LOCK(fp); 1654 /* 1655 * If it's not open, skip it 1656 */ 1657 if (fp->f_count == 0) { 1658 FILE_UNLOCK(fp); 1659 continue; 1660 } 1661 /* 1662 * If all refs are from msgs, and it's not marked accessible 1663 * then it must be referenced from some unreachable cycle 1664 * of (shut-down) FDs, so include it in our 1665 * list of FDs to remove 1666 */ 1667 if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1668 *fpp++ = fp; 1669 nunref++; 1670 fp->f_count++; 1671 } 1672 FILE_UNLOCK(fp); 1673 } 1674 sx_sunlock(&filelist_lock); 1675 /* 1676 * for each FD on our hit list, do the following two things 1677 */ 1678 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 1679 struct file *tfp = *fpp; 1680 FILE_LOCK(tfp); 1681 if (tfp->f_type == DTYPE_SOCKET && 1682 tfp->f_data != NULL) { 1683 FILE_UNLOCK(tfp); 1684 sorflush(tfp->f_data); 1685 } else { 1686 FILE_UNLOCK(tfp); 1687 } 1688 } 1689 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1690 closef(*fpp, (struct thread *) NULL); 1691 free(extra_ref, M_TEMP); 1692 unp_gcing = 0; 1693} 1694 1695void 1696unp_dispose(m) 1697 struct mbuf *m; 1698{ 1699 1700 if (m) 1701 unp_scan(m, unp_discard); 1702} 1703 1704static int 1705unp_listen(unp, td) 1706 struct unpcb *unp; 1707 struct thread *td; 1708{ 1709 UNP_LOCK_ASSERT(); 1710 1711 /* 1712 * XXXRW: Why populate the local peer cred with our own credential? 1713 */ 1714 cru2x(td->td_ucred, &unp->unp_peercred); 1715 unp->unp_flags |= UNP_HAVEPCCACHED; 1716 return (0); 1717} 1718 1719static void 1720unp_scan(m0, op) 1721 register struct mbuf *m0; 1722 void (*op)(struct file *); 1723{ 1724 struct mbuf *m; 1725 struct file **rp; 1726 struct cmsghdr *cm; 1727 void *data; 1728 int i; 1729 socklen_t clen, datalen; 1730 int qfds; 1731 1732 while (m0 != NULL) { 1733 for (m = m0; m; m = m->m_next) { 1734 if (m->m_type != MT_CONTROL) 1735 continue; 1736 1737 cm = mtod(m, struct cmsghdr *); 1738 clen = m->m_len; 1739 1740 while (cm != NULL) { 1741 if (sizeof(*cm) > clen || cm->cmsg_len > clen) 1742 break; 1743 1744 data = CMSG_DATA(cm); 1745 datalen = (caddr_t)cm + cm->cmsg_len 1746 - (caddr_t)data; 1747 1748 if (cm->cmsg_level == SOL_SOCKET && 1749 cm->cmsg_type == SCM_RIGHTS) { 1750 qfds = datalen / sizeof (struct file *); 1751 rp = data; 1752 for (i = 0; i < qfds; i++) 1753 (*op)(*rp++); 1754 } 1755 1756 if (CMSG_SPACE(datalen) < clen) { 1757 clen -= CMSG_SPACE(datalen); 1758 cm = (struct cmsghdr *) 1759 ((caddr_t)cm + CMSG_SPACE(datalen)); 1760 } else { 1761 clen = 0; 1762 cm = NULL; 1763 } 1764 } 1765 } 1766 m0 = m0->m_act; 1767 } 1768} 1769 1770static void 1771unp_mark(fp) 1772 struct file *fp; 1773{ 1774 if (fp->f_gcflag & FMARK) 1775 return; 1776 unp_defer++; 1777 fp->f_gcflag |= (FMARK|FDEFER); 1778} 1779 1780static void 1781unp_discard(fp) 1782 struct file *fp; 1783{ 1784 FILE_LOCK(fp); 1785 fp->f_msgcount--; 1786 unp_rights--; 1787 FILE_UNLOCK(fp); 1788 (void) closef(fp, (struct thread *)NULL); 1789} 1790