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