1 /* $NetBSD: uipc_usrreq.c,v 1.212 2026/10/02 01:33:44 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2000, 2004, 2008, 2009, 2020 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center, and by Andrew Doran. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1989, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)uipc_usrreq.c 8.9 (Berkeley) 5/14/95 62 */ 63 64 /* 65 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved. 66 * 67 * Redistribution and use in source and binary forms, with or without 68 * modification, are permitted provided that the following conditions 69 * are met: 70 * 1. Redistributions of source code must retain the above copyright 71 * notice, this list of conditions and the following disclaimer. 72 * 2. Redistributions in binary form must reproduce the above copyright 73 * notice, this list of conditions and the following disclaimer in the 74 * documentation and/or other materials provided with the distribution. 75 * 3. All advertising materials mentioning features or use of this software 76 * must display the following acknowledgement: 77 * This product includes software developed by the University of 78 * California, Berkeley and its contributors. 79 * 4. Neither the name of the University nor the names of its contributors 80 * may be used to endorse or promote products derived from this software 81 * without specific prior written permission. 82 * 83 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 84 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 85 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 86 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 87 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 88 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 89 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 90 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 91 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 92 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 93 * SUCH DAMAGE. 94 * 95 * @(#)uipc_usrreq.c 8.9 (Berkeley) 5/14/95 96 */ 97 98 #include <sys/cdefs.h> 99 __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.212 2026/10/02 01:33:44 riastradh Exp $"); 100 101 #ifdef _KERNEL_OPT 102 #include "opt_compat_netbsd.h" 103 #endif 104 105 #include <sys/param.h> 106 #include <sys/types.h> 107 108 #include <sys/atomic.h> 109 #include <sys/compat_stub.h> 110 #include <sys/domain.h> 111 #include <sys/file.h> 112 #include <sys/filedesc.h> 113 #include <sys/kauth.h> 114 #include <sys/kernel.h> 115 #include <sys/kmem.h> 116 #include <sys/kthread.h> 117 #include <sys/mbuf.h> 118 #include <sys/namei.h> 119 #include <sys/proc.h> 120 #include <sys/protosw.h> 121 #include <sys/sdt.h> 122 #include <sys/socket.h> 123 #include <sys/socketvar.h> 124 #include <sys/stat.h> 125 #include <sys/systm.h> 126 #include <sys/uidinfo.h> 127 #include <sys/un.h> 128 #include <sys/unpcb.h> 129 #include <sys/vnode.h> 130 131 #include <compat/net/route_70.h> 132 #include <compat/sys/socket.h> 133 134 /* 135 * Unix communications domain. 136 * 137 * TODO: 138 * RDM 139 * rethink name space problems 140 * need a proper out-of-band 141 * 142 * Notes on locking: 143 * 144 * The generic rules noted in uipc_socket2.c apply. In addition: 145 * 146 * o We have a global lock, uipc_lock. 147 * 148 * o All datagram sockets are locked by uipc_lock. 149 * 150 * o For stream socketpairs, the two endpoints are created sharing the same 151 * independent lock. Sockets presented to PRU_CONNECT2 must already have 152 * matching locks. 153 * 154 * o Stream sockets created via socket() start life with their own 155 * independent lock. 156 * 157 * o Stream connections to a named endpoint are slightly more complicated. 158 * Sockets that have called listen() have their lock pointer mutated to 159 * the global uipc_lock. When establishing a connection, the connecting 160 * socket also has its lock mutated to uipc_lock, which matches the head 161 * (listening socket). We create a new socket for accept() to return, and 162 * that also shares the head's lock. Until the connection is completely 163 * done on both ends, all three sockets are locked by uipc_lock. Once the 164 * connection is complete, the association with the head's lock is broken. 165 * The connecting socket and the socket returned from accept() have their 166 * lock pointers mutated away from uipc_lock, and back to the connecting 167 * socket's original, independent lock. The head continues to be locked 168 * by uipc_lock. 169 * 170 * o If uipc_lock is determined to be a significant source of contention, 171 * it could easily be hashed out. It is difficult to simply make it an 172 * independent lock because of visibility / garbage collection issues: 173 * if a socket has been associated with a lock at any point, that lock 174 * must remain valid until the socket is no longer visible in the system. 175 * The lock must not be freed or otherwise destroyed until any sockets 176 * that had referenced it have also been destroyed. 177 */ 178 const struct sockaddr_un sun_noname = { 179 .sun_len = offsetof(struct sockaddr_un, sun_path), 180 .sun_family = AF_LOCAL, 181 }; 182 183 /* prototype for fake inode numbers */ 184 #ifdef __HAVE_ATOMIC64_OPS 185 volatile uint64_t unp_ino __cacheline_aligned; 186 #else 187 struct { 188 uint64_t num; 189 kmutex_t lock; 190 } unp_ino __cacheline_aligned; 191 #endif 192 193 static struct mbuf * unp_addsockcred(struct lwp *, struct mbuf *); 194 static void unp_discard_later(file_t *); 195 static void unp_discard_now(file_t *); 196 static void unp_disconnect1(struct unpcb *); 197 static bool unp_drop(struct unpcb *, int); 198 static int unp_internalize(struct mbuf **); 199 static void unp_mark(file_t *); 200 static void unp_scan(struct mbuf *, void (*)(file_t *), int); 201 static void unp_shutdown1(struct unpcb *); 202 static void unp_thread(void *); 203 static void unp_thread_kick(void); 204 205 static kmutex_t *uipc_lock; 206 207 static kcondvar_t unp_thread_cv; 208 static lwp_t *unp_thread_lwp; 209 static SLIST_HEAD(,file) unp_thread_discard; 210 static int unp_defer; 211 static struct sysctllog *usrreq_sysctllog; 212 static void unp_sysctl_create(void); 213 214 /* Compat interface */ 215 216 struct mbuf * stub_compat_70_unp_addsockcred(lwp_t *, struct mbuf *); 217 218 struct mbuf * stub_compat_70_unp_addsockcred(struct lwp *lwp, 219 struct mbuf *control) 220 { 221 222 /* just copy our initial argument */ 223 return control; 224 } 225 226 bool compat70_ocreds_valid = false; 227 228 /* 229 * Initialize Unix protocols. 230 */ 231 void 232 uipc_init(void) 233 { 234 int error; 235 236 unp_sysctl_create(); 237 238 uipc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 239 cv_init(&unp_thread_cv, "unpgc"); 240 241 error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, unp_thread, 242 NULL, &unp_thread_lwp, "unpgc"); 243 if (error != 0) 244 panic("uipc_init %d", error); 245 246 #ifndef __HAVE_ATOMIC64_OPS 247 mutex_init(&unp_ino.lock, MUTEX_DEFAULT, IPL_NONE); 248 #endif 249 } 250 251 static void 252 unp_connid(struct lwp *l, struct unpcb *unp, int flags) 253 { 254 unp->unp_connid.unp_pid = l->l_proc->p_pid; 255 unp->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred); 256 unp->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred); 257 unp->unp_flags |= flags; 258 } 259 260 /* 261 * A connection succeeded: disassociate both endpoints from the head's 262 * lock, and make them share their own lock. There is a race here: for 263 * a very brief time one endpoint will be locked by a different lock 264 * than the other end. However, since the current thread holds the old 265 * lock (the listening socket's lock, the head) access can still only be 266 * made to one side of the connection. 267 */ 268 static void 269 unp_setpeerlocks(struct socket *so, struct socket *so2) 270 { 271 struct unpcb *unp; 272 kmutex_t *lock; 273 274 KASSERT(solocked2(so, so2)); 275 276 /* 277 * Bail out if either end of the socket is not yet fully 278 * connected or accepted. We only break the lock association 279 * with the head when the pair of sockets stand completely 280 * on their own. 281 */ 282 KASSERT(so->so_head == NULL); 283 if (so2->so_head != NULL) 284 return; 285 286 /* 287 * Drop references to old lock. A third reference (from the 288 * queue head) must be held as we still hold its lock. Bonus: 289 * we don't need to worry about garbage collecting the lock. 290 */ 291 lock = so->so_lock; 292 KASSERT(lock == uipc_lock); 293 mutex_obj_free(lock); 294 mutex_obj_free(lock); 295 296 /* 297 * Grab stream lock from the initiator and share between the two 298 * endpoints. Issue memory barrier to ensure all modifications 299 * become globally visible before the lock change. so2 is 300 * assumed not to have a stream lock, because it was created 301 * purely for the server side to accept this connection and 302 * started out life using the domain-wide lock. 303 */ 304 unp = sotounpcb(so); 305 KASSERT(unp->unp_streamlock != NULL); 306 KASSERT(sotounpcb(so2)->unp_streamlock == NULL); 307 lock = unp->unp_streamlock; 308 unp->unp_streamlock = NULL; 309 mutex_obj_hold(lock); 310 /* 311 * Ensure lock is initialized before publishing it with 312 * solockreset. Pairs with atomic_load_consume in solock and 313 * various loops to reacquire lock after wakeup. 314 */ 315 membar_release(); 316 /* 317 * possible race if lock is not held - see comment in 318 * uipc_usrreq(PRU_ACCEPT). 319 */ 320 KASSERT(mutex_owned(lock)); 321 solockreset(so, lock); 322 solockreset(so2, lock); 323 } 324 325 /* 326 * Reset a socket's lock back to the domain-wide lock. 327 */ 328 static void 329 unp_resetlock(struct socket *so) 330 { 331 kmutex_t *olock, *nlock; 332 struct unpcb *unp; 333 334 KASSERT(solocked(so)); 335 336 olock = so->so_lock; 337 nlock = uipc_lock; 338 if (olock == nlock) 339 return; 340 unp = sotounpcb(so); 341 KASSERT(unp->unp_streamlock == NULL); 342 unp->unp_streamlock = olock; 343 mutex_obj_hold(nlock); 344 mutex_enter(nlock); 345 solockreset(so, nlock); 346 mutex_exit(olock); 347 } 348 349 static void 350 unp_free(struct unpcb *unp) 351 { 352 if (unp->unp_addr) 353 free(unp->unp_addr, M_SONAME); 354 if (unp->unp_streamlock != NULL) 355 mutex_obj_free(unp->unp_streamlock); 356 kmem_free(unp, sizeof(*unp)); 357 } 358 359 static int 360 unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp) 361 { 362 struct socket *so2; 363 const struct sockaddr_un *sun; 364 365 /* XXX: server side closed the socket */ 366 if (unp->unp_conn == NULL) 367 return SET_ERROR(ECONNREFUSED); 368 so2 = unp->unp_conn->unp_socket; 369 370 KASSERT(solocked(so2)); 371 372 if (unp->unp_addr) 373 sun = unp->unp_addr; 374 else 375 sun = &sun_noname; 376 if (unp->unp_conn->unp_flags & UNP_WANTCRED) 377 control = unp_addsockcred(curlwp, control); 378 if (unp->unp_conn->unp_flags & UNP_OWANTCRED) 379 MODULE_HOOK_CALL(uipc_unp_70_hook, (curlwp, control), 380 stub_compat_70_unp_addsockcred(curlwp, control), control); 381 if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m, 382 control) == 0) { 383 unp_dispose(control); 384 m_freem(control); 385 m_freem(m); 386 /* Don't call soroverflow because we're returning this 387 * error directly to the sender. */ 388 so2->so_rcv.sb_overflowed++; 389 return SET_ERROR(ENOBUFS); 390 } else { 391 sorwakeup(so2); 392 return 0; 393 } 394 } 395 396 static void 397 unp_setaddr(struct socket *so, struct sockaddr *nam, bool peeraddr) 398 { 399 const struct sockaddr_un *sun = NULL; 400 struct unpcb *unp; 401 402 KASSERT(solocked(so)); 403 unp = sotounpcb(so); 404 405 if (peeraddr) { 406 if (unp->unp_conn && unp->unp_conn->unp_addr) 407 sun = unp->unp_conn->unp_addr; 408 } else { 409 if (unp->unp_addr) 410 sun = unp->unp_addr; 411 } 412 if (sun == NULL) 413 sun = &sun_noname; 414 415 memcpy(nam, sun, sun->sun_len); 416 } 417 418 static int 419 unp_rcvd(struct socket *so, int flags, struct lwp *l) 420 { 421 struct unpcb *unp = sotounpcb(so); 422 struct socket *so2; 423 u_int newhiwat; 424 425 KASSERT(solocked(so)); 426 KASSERT(unp != NULL); 427 428 switch (so->so_type) { 429 430 case SOCK_DGRAM: 431 panic("uipc 1"); 432 /*NOTREACHED*/ 433 434 case SOCK_SEQPACKET: /* FALLTHROUGH */ 435 case SOCK_STREAM: 436 #define rcv (&so->so_rcv) 437 #define snd (&so2->so_snd) 438 if (unp->unp_conn == 0) 439 break; 440 so2 = unp->unp_conn->unp_socket; 441 KASSERT(solocked2(so, so2)); 442 /* 443 * Adjust backpressure on sender 444 * and wakeup any waiting to write. 445 */ 446 snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; 447 unp->unp_mbcnt = rcv->sb_mbcnt; 448 newhiwat = snd->sb_hiwat + unp->unp_cc - rcv->sb_cc; 449 (void)chgsbsize(so2->so_uidinfo, 450 &snd->sb_hiwat, newhiwat, RLIM_INFINITY); 451 unp->unp_cc = rcv->sb_cc; 452 sowwakeup(so2); 453 #undef snd 454 #undef rcv 455 break; 456 457 default: 458 panic("uipc 2"); 459 } 460 461 return 0; 462 } 463 464 static int 465 unp_recvoob(struct socket *so, struct mbuf *m, int flags) 466 { 467 KASSERT(solocked(so)); 468 469 return SET_ERROR(EOPNOTSUPP); 470 } 471 472 static int 473 unp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam, 474 struct mbuf *control, struct lwp *l) 475 { 476 struct unpcb *unp = sotounpcb(so); 477 int error = 0; 478 u_int newhiwat; 479 struct socket *so2; 480 481 KASSERT(solocked(so)); 482 KASSERT(unp != NULL); 483 KASSERT(m != NULL); 484 485 /* 486 * Note: unp_internalize() rejects any control message 487 * other than SCM_RIGHTS, and only allows one. This 488 * has the side-effect of preventing a caller from 489 * forging SCM_CREDS. 490 */ 491 if (control) { 492 sounlock(so); 493 error = unp_internalize(&control); 494 solock(so); 495 if (error != 0) { 496 m_freem(control); 497 m_freem(m); 498 return error; 499 } 500 } 501 502 switch (so->so_type) { 503 504 case SOCK_DGRAM: { 505 KASSERT(so->so_lock == uipc_lock); 506 if (nam) { 507 if ((so->so_state & SS_ISCONNECTED) != 0) 508 error = SET_ERROR(EISCONN); 509 else { 510 /* 511 * Note: once connected, the 512 * socket's lock must not be 513 * dropped until we have sent 514 * the message and disconnected. 515 * This is necessary to prevent 516 * intervening control ops, like 517 * another connection. 518 */ 519 error = unp_connect(so, nam, l); 520 } 521 } else { 522 if ((so->so_state & SS_ISCONNECTED) == 0) 523 error = SET_ERROR(ENOTCONN); 524 } 525 if (error) { 526 unp_dispose(control); 527 m_freem(control); 528 m_freem(m); 529 return error; 530 } 531 error = unp_output(m, control, unp); 532 if (nam) 533 unp_disconnect1(unp); 534 break; 535 } 536 537 case SOCK_SEQPACKET: /* FALLTHROUGH */ 538 case SOCK_STREAM: 539 #define rcv (&so2->so_rcv) 540 #define snd (&so->so_snd) 541 if (unp->unp_conn == NULL) { 542 error = SET_ERROR(ENOTCONN); 543 break; 544 } 545 so2 = unp->unp_conn->unp_socket; 546 KASSERT(solocked2(so, so2)); 547 if (unp->unp_conn->unp_flags & UNP_WANTCRED) { 548 /* 549 * Credentials are passed only once on 550 * SOCK_STREAM and SOCK_SEQPACKET. 551 */ 552 unp->unp_conn->unp_flags &= ~UNP_WANTCRED; 553 control = unp_addsockcred(l, control); 554 } 555 if (unp->unp_conn->unp_flags & UNP_OWANTCRED) { 556 /* 557 * Credentials are passed only once on 558 * SOCK_STREAM and SOCK_SEQPACKET. 559 */ 560 unp->unp_conn->unp_flags &= ~UNP_OWANTCRED; 561 MODULE_HOOK_CALL(uipc_unp_70_hook, (curlwp, control), 562 stub_compat_70_unp_addsockcred(curlwp, control), 563 control); 564 } 565 /* 566 * Send to paired receive port, and then reduce 567 * send buffer hiwater marks to maintain backpressure. 568 * Wake up readers. 569 */ 570 if (control) { 571 sbappendcontrol(rcv, m, control); 572 control = NULL; 573 } else { 574 switch(so->so_type) { 575 case SOCK_SEQPACKET: 576 sbappendrecord(rcv, m); 577 break; 578 case SOCK_STREAM: 579 sbappend(rcv, m); 580 break; 581 default: 582 panic("uipc_usrreq"); 583 break; 584 } 585 } 586 snd->sb_mbmax -= 587 rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; 588 unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; 589 newhiwat = snd->sb_hiwat - 590 (rcv->sb_cc - unp->unp_conn->unp_cc); 591 (void)chgsbsize(so->so_uidinfo, 592 &snd->sb_hiwat, newhiwat, RLIM_INFINITY); 593 unp->unp_conn->unp_cc = rcv->sb_cc; 594 sorwakeup(so2); 595 #undef snd 596 #undef rcv 597 if (control != NULL) { 598 unp_dispose(control); 599 m_freem(control); 600 } 601 break; 602 603 default: 604 panic("uipc 4"); 605 } 606 607 return error; 608 } 609 610 static int 611 unp_sendoob(struct socket *so, struct mbuf *m, struct mbuf * control) 612 { 613 KASSERT(solocked(so)); 614 615 m_freem(m); 616 m_freem(control); 617 618 return SET_ERROR(EOPNOTSUPP); 619 } 620 621 /* 622 * Unix domain socket option processing. 623 */ 624 int 625 uipc_ctloutput(int op, struct socket *so, struct sockopt *sopt) 626 { 627 struct unpcb *unp = sotounpcb(so); 628 int optval = 0, error = 0; 629 630 KASSERT(solocked(so)); 631 632 if (sopt->sopt_level != SOL_LOCAL) { 633 error = SET_ERROR(ENOPROTOOPT); 634 } else switch (op) { 635 636 case PRCO_SETOPT: 637 switch (sopt->sopt_name) { 638 case LOCAL_OCREDS: 639 if (!compat70_ocreds_valid) { 640 error = SET_ERROR(ENOPROTOOPT); 641 break; 642 } 643 /* FALLTHROUGH */ 644 case LOCAL_CREDS: 645 case LOCAL_CONNWAIT: 646 error = sockopt_getint(sopt, &optval); 647 if (error) 648 break; 649 switch (sopt->sopt_name) { 650 #define OPTSET(bit) \ 651 if (optval) \ 652 unp->unp_flags |= (bit); \ 653 else \ 654 unp->unp_flags &= ~(bit); 655 656 case LOCAL_CREDS: 657 OPTSET(UNP_WANTCRED); 658 break; 659 case LOCAL_CONNWAIT: 660 OPTSET(UNP_CONNWAIT); 661 break; 662 case LOCAL_OCREDS: 663 OPTSET(UNP_OWANTCRED); 664 break; 665 } 666 break; 667 #undef OPTSET 668 669 default: 670 error = SET_ERROR(ENOPROTOOPT); 671 break; 672 } 673 break; 674 675 case PRCO_GETOPT: 676 sounlock(so); 677 switch (sopt->sopt_name) { 678 case LOCAL_PEEREID: 679 if (unp->unp_flags & UNP_EIDSVALID) { 680 error = sockopt_set(sopt, &unp->unp_connid, 681 sizeof(unp->unp_connid)); 682 } else { 683 error = SET_ERROR(EINVAL); 684 } 685 break; 686 case LOCAL_CREDS: 687 #define OPTBIT(bit) (unp->unp_flags & (bit) ? 1 : 0) 688 689 optval = OPTBIT(UNP_WANTCRED); 690 error = sockopt_setint(sopt, optval); 691 break; 692 case LOCAL_OCREDS: 693 if (compat70_ocreds_valid) { 694 optval = OPTBIT(UNP_OWANTCRED); 695 error = sockopt_setint(sopt, optval); 696 break; 697 } 698 #undef OPTBIT 699 /* FALLTHROUGH */ 700 default: 701 error = SET_ERROR(ENOPROTOOPT); 702 break; 703 } 704 solock(so); 705 break; 706 } 707 return (error); 708 } 709 710 /* 711 * Both send and receive buffers are allocated PIPSIZ bytes of buffering 712 * for stream sockets, although the total for sender and receiver is 713 * actually only PIPSIZ. 714 * Datagram sockets really use the sendspace as the maximum datagram size, 715 * and don't really want to reserve the sendspace. Their recvspace should 716 * be large enough for at least one max-size datagram plus address. 717 */ 718 #ifndef PIPSIZ 719 #define PIPSIZ 8192 720 #endif 721 u_long unpst_sendspace = PIPSIZ; 722 u_long unpst_recvspace = PIPSIZ; 723 u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 724 u_long unpdg_recvspace = 16*1024; 725 726 u_int unp_rights; /* files in flight */ 727 u_int unp_rights_ratio = 2; /* limit, fraction of maxfiles */ 728 729 static int 730 unp_attach(struct socket *so, int proto) 731 { 732 struct unpcb *unp = sotounpcb(so); 733 u_long sndspc, rcvspc; 734 int error; 735 736 KASSERT(unp == NULL); 737 738 switch (so->so_type) { 739 case SOCK_SEQPACKET: 740 /* FALLTHROUGH */ 741 case SOCK_STREAM: 742 if (so->so_lock == NULL) { 743 so->so_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 744 solock(so); 745 } 746 sndspc = unpst_sendspace; 747 rcvspc = unpst_recvspace; 748 break; 749 750 case SOCK_DGRAM: 751 if (so->so_lock == NULL) { 752 mutex_obj_hold(uipc_lock); 753 so->so_lock = uipc_lock; 754 solock(so); 755 } 756 sndspc = unpdg_sendspace; 757 rcvspc = unpdg_recvspace; 758 break; 759 760 default: 761 panic("unp_attach"); 762 } 763 764 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 765 error = soreserve(so, sndspc, rcvspc); 766 if (error) { 767 return error; 768 } 769 } 770 771 unp = kmem_zalloc(sizeof(*unp), KM_SLEEP); 772 nanotime(&unp->unp_ctime); 773 unp->unp_socket = so; 774 so->so_pcb = unp; 775 776 KASSERT(solocked(so)); 777 return 0; 778 } 779 780 static void 781 unp_detach(struct socket *so) 782 { 783 struct unpcb *unp; 784 vnode_t *vp; 785 786 unp = sotounpcb(so); 787 KASSERT(unp != NULL); 788 KASSERT(solocked(so)); 789 retry: 790 if ((vp = unp->unp_vnode) != NULL) { 791 sounlock(so); 792 /* Acquire v_interlock to protect against unp_connect(). */ 793 /* XXXAD racy */ 794 mutex_enter(vp->v_interlock); 795 vp->v_socket = NULL; 796 mutex_exit(vp->v_interlock); 797 vrele(vp); 798 solock(so); 799 unp->unp_vnode = NULL; 800 } 801 if (unp->unp_conn) 802 unp_disconnect1(unp); 803 while (unp->unp_refs) { 804 KASSERT(solocked2(so, unp->unp_refs->unp_socket)); 805 if (unp_drop(unp->unp_refs, SET_ERROR(ECONNRESET))) { 806 solock(so); 807 goto retry; 808 } 809 } 810 soisdisconnected(so); 811 so->so_pcb = NULL; 812 if (unp_rights) { 813 /* 814 * Normally the receive buffer is flushed later, in sofree, 815 * but if our receive buffer holds references to files that 816 * are now garbage, we will enqueue those file references to 817 * the garbage collector and kick it into action. 818 */ 819 sorflush(so); 820 unp_free(unp); 821 unp_thread_kick(); 822 } else 823 unp_free(unp); 824 } 825 826 static int 827 unp_accept(struct socket *so, struct sockaddr *nam) 828 { 829 struct unpcb *unp = sotounpcb(so); 830 struct socket *so2; 831 832 KASSERT(solocked(so)); 833 KASSERT(nam != NULL); 834 835 /* XXX code review required to determine if unp can ever be NULL */ 836 if (unp == NULL) 837 return SET_ERROR(EINVAL); 838 839 KASSERT(so->so_lock == uipc_lock); 840 /* 841 * Mark the initiating STREAM socket as connected *ONLY* 842 * after it's been accepted. This prevents a client from 843 * overrunning a server and receiving ECONNREFUSED. 844 */ 845 if (unp->unp_conn == NULL) { 846 /* 847 * This will use the empty socket and will not 848 * allocate. 849 */ 850 unp_setaddr(so, nam, true); 851 return 0; 852 } 853 so2 = unp->unp_conn->unp_socket; 854 if (so2->so_state & SS_ISCONNECTING) { 855 KASSERT(so->so_head == NULL || solocked2(so, so->so_head)); 856 KASSERT(so->so_head == NULL || solocked2(so2, so->so_head)); 857 soisconnected(so2); 858 } 859 /* 860 * If the connection is fully established, break the 861 * association with uipc_lock and give the connected 862 * pair a separate lock to share. 863 * There is a race here: sotounpcb(so2)->unp_streamlock 864 * is not locked, so when changing so2->so_lock 865 * another thread can grab it while so->so_lock is still 866 * pointing to the (locked) uipc_lock. 867 * this should be harmless, except that this makes 868 * solocked2() and solocked() unreliable. 869 * Another problem is that unp_setaddr() expects the 870 * the socket locked. Grabbing sotounpcb(so2)->unp_streamlock 871 * fixes both issues. 872 */ 873 mutex_enter(sotounpcb(so2)->unp_streamlock); 874 unp_setpeerlocks(so2, so); 875 /* 876 * Only now return peer's address, as we may need to 877 * block in order to allocate memory. 878 * 879 * XXX Minor race: connection can be broken while 880 * lock is dropped in unp_setaddr(). We will return 881 * error == 0 and sun_noname as the peer address. 882 */ 883 unp_setaddr(so, nam, true); 884 /* so_lock now points to unp_streamlock */ 885 mutex_exit(so2->so_lock); 886 return 0; 887 } 888 889 static int 890 unp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp) 891 { 892 return SET_ERROR(EOPNOTSUPP); 893 } 894 895 static int 896 unp_stat(struct socket *so, struct stat *ub) 897 { 898 struct unpcb *unp; 899 struct socket *so2; 900 901 KASSERT(solocked(so)); 902 903 unp = sotounpcb(so); 904 if (unp == NULL) 905 return SET_ERROR(EINVAL); 906 907 ub->st_blksize = so->so_snd.sb_hiwat; 908 switch (so->so_type) { 909 case SOCK_SEQPACKET: /* FALLTHROUGH */ 910 case SOCK_STREAM: 911 if (unp->unp_conn == 0) 912 break; 913 914 so2 = unp->unp_conn->unp_socket; 915 KASSERT(solocked2(so, so2)); 916 ub->st_blksize += so2->so_rcv.sb_cc; 917 break; 918 default: 919 break; 920 } 921 ub->st_dev = NODEV; 922 if (unp->unp_ino == 0) { 923 #ifdef __HAVE_ATOMIC64_OPS 924 unp->unp_ino = atomic_inc_64_nv(&unp_ino); 925 #else 926 mutex_enter(&unp_ino.lock); 927 unp->unp_ino = ++unp_ino.num; 928 mutex_exit(&unp_ino.lock); 929 #endif 930 } 931 ub->st_atimespec = ub->st_mtimespec = ub->st_ctimespec = unp->unp_ctime; 932 ub->st_ino = unp->unp_ino; 933 ub->st_uid = so->so_uidinfo->ui_uid; 934 ub->st_gid = so->so_egid; 935 return (0); 936 } 937 938 static int 939 unp_peeraddr(struct socket *so, struct sockaddr *nam) 940 { 941 KASSERT(solocked(so)); 942 KASSERT(sotounpcb(so) != NULL); 943 KASSERT(nam != NULL); 944 945 unp_setaddr(so, nam, true); 946 return 0; 947 } 948 949 static int 950 unp_sockaddr(struct socket *so, struct sockaddr *nam) 951 { 952 KASSERT(solocked(so)); 953 KASSERT(sotounpcb(so) != NULL); 954 KASSERT(nam != NULL); 955 956 unp_setaddr(so, nam, false); 957 return 0; 958 } 959 960 /* 961 * we only need to perform this allocation until syscalls other than 962 * bind are adjusted to use sockaddr_big. 963 */ 964 static struct sockaddr_un * 965 makeun_sb(struct sockaddr *nam, size_t *addrlen) 966 { 967 struct sockaddr_un *sun; 968 969 *addrlen = nam->sa_len + 1; 970 sun = malloc(*addrlen, M_SONAME, M_WAITOK); 971 memcpy(sun, nam, nam->sa_len); 972 *(((char *)sun) + nam->sa_len) = '\0'; 973 return sun; 974 } 975 976 static int 977 unp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l) 978 { 979 struct sockaddr_un *sun; 980 struct unpcb *unp; 981 vnode_t *vp; 982 struct vattr vattr; 983 size_t addrlen; 984 int error; 985 struct pathbuf *pb; 986 struct nameidata nd; 987 proc_t *p; 988 989 unp = sotounpcb(so); 990 991 KASSERT(solocked(so)); 992 KASSERT(unp != NULL); 993 KASSERT(nam != NULL); 994 995 if (unp->unp_vnode != NULL) 996 return SET_ERROR(EINVAL); 997 if ((unp->unp_flags & UNP_BUSY) != 0) { 998 /* 999 * EALREADY may not be strictly accurate, but since this 1000 * is a major application error it's hardly a big deal. 1001 */ 1002 return SET_ERROR(EALREADY); 1003 } 1004 unp->unp_flags |= UNP_BUSY; 1005 sounlock(so); 1006 1007 p = l->l_proc; 1008 sun = makeun_sb(nam, &addrlen); 1009 1010 pb = pathbuf_create(sun->sun_path); 1011 if (pb == NULL) { 1012 error = SET_ERROR(ENOMEM); 1013 goto bad; 1014 } 1015 NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT | TRYEMULROOT, pb); 1016 1017 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 1018 if ((error = namei(&nd)) != 0) { 1019 pathbuf_destroy(pb); 1020 goto bad; 1021 } 1022 vp = nd.ni_vp; 1023 if (vp != NULL) { 1024 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1025 if (nd.ni_dvp == vp) 1026 vrele(nd.ni_dvp); 1027 else 1028 vput(nd.ni_dvp); 1029 vrele(vp); 1030 pathbuf_destroy(pb); 1031 error = SET_ERROR(EADDRINUSE); 1032 goto bad; 1033 } 1034 vattr_null(&vattr); 1035 vattr.va_type = VSOCK; 1036 vattr.va_mode = ACCESSPERMS & ~(p->p_cwdi->cwdi_cmask); 1037 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 1038 if (error) { 1039 vput(nd.ni_dvp); 1040 pathbuf_destroy(pb); 1041 goto bad; 1042 } 1043 vp = nd.ni_vp; 1044 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1045 solock(so); 1046 vp->v_socket = unp->unp_socket; 1047 unp->unp_vnode = vp; 1048 unp->unp_addrlen = addrlen; 1049 unp->unp_addr = sun; 1050 VOP_UNLOCK(vp); 1051 vput(nd.ni_dvp); 1052 unp->unp_flags &= ~UNP_BUSY; 1053 pathbuf_destroy(pb); 1054 return (0); 1055 1056 bad: 1057 free(sun, M_SONAME); 1058 solock(so); 1059 unp->unp_flags &= ~UNP_BUSY; 1060 return (error); 1061 } 1062 1063 static int 1064 unp_listen(struct socket *so, struct lwp *l) 1065 { 1066 struct unpcb *unp = sotounpcb(so); 1067 1068 KASSERT(solocked(so)); 1069 KASSERT(unp != NULL); 1070 1071 /* 1072 * If the socket can accept a connection, it must be 1073 * locked by uipc_lock. 1074 */ 1075 unp_resetlock(so); 1076 if (unp->unp_vnode == NULL) 1077 return SET_ERROR(EINVAL); 1078 1079 unp_connid(l, unp, UNP_EIDSBIND); 1080 return 0; 1081 } 1082 1083 static int 1084 unp_disconnect(struct socket *so) 1085 { 1086 KASSERT(solocked(so)); 1087 KASSERT(sotounpcb(so) != NULL); 1088 1089 unp_disconnect1(sotounpcb(so)); 1090 return 0; 1091 } 1092 1093 static int 1094 unp_shutdown(struct socket *so) 1095 { 1096 KASSERT(solocked(so)); 1097 KASSERT(sotounpcb(so) != NULL); 1098 1099 socantsendmore(so); 1100 unp_shutdown1(sotounpcb(so)); 1101 return 0; 1102 } 1103 1104 static int 1105 unp_abort(struct socket *so) 1106 { 1107 KASSERT(solocked(so)); 1108 KASSERT(sotounpcb(so) != NULL); 1109 1110 (void)unp_drop(sotounpcb(so), SET_ERROR(ECONNABORTED)); 1111 KASSERT(so->so_head == NULL); 1112 KASSERT(so->so_pcb != NULL); 1113 unp_detach(so); 1114 return 0; 1115 } 1116 1117 static int 1118 unp_connect1(struct socket *so, struct socket *so2, struct lwp *l) 1119 { 1120 struct unpcb *unp = sotounpcb(so); 1121 struct unpcb *unp2; 1122 1123 if (so2->so_type != so->so_type) 1124 return SET_ERROR(EPROTOTYPE); 1125 1126 /* 1127 * All three sockets involved must be locked by same lock: 1128 * 1129 * local endpoint (so) 1130 * remote endpoint (so2) 1131 * queue head (so2->so_head, only if PR_CONNREQUIRED) 1132 */ 1133 KASSERT(solocked2(so, so2)); 1134 KASSERT(so->so_head == NULL); 1135 if (so2->so_head != NULL) { 1136 KASSERT(so2->so_lock == uipc_lock); 1137 KASSERT(solocked2(so2, so2->so_head)); 1138 } 1139 1140 unp2 = sotounpcb(so2); 1141 unp->unp_conn = unp2; 1142 1143 switch (so->so_type) { 1144 1145 case SOCK_DGRAM: 1146 unp->unp_nextref = unp2->unp_refs; 1147 unp2->unp_refs = unp; 1148 soisconnected(so); 1149 break; 1150 1151 case SOCK_SEQPACKET: /* FALLTHROUGH */ 1152 case SOCK_STREAM: 1153 1154 /* 1155 * SOCK_SEQPACKET and SOCK_STREAM cases are handled by callers 1156 * which are unp_connect() or unp_connect2(). 1157 */ 1158 1159 break; 1160 1161 default: 1162 panic("unp_connect1"); 1163 } 1164 1165 return 0; 1166 } 1167 1168 int 1169 unp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l) 1170 { 1171 struct sockaddr_un *sun; 1172 vnode_t *vp; 1173 struct socket *so2, *so3; 1174 struct unpcb *unp, *unp2, *unp3; 1175 size_t addrlen; 1176 int error; 1177 struct pathbuf *pb; 1178 struct nameidata nd; 1179 1180 unp = sotounpcb(so); 1181 if ((unp->unp_flags & UNP_BUSY) != 0) { 1182 /* 1183 * EALREADY may not be strictly accurate, but since this 1184 * is a major application error it's hardly a big deal. 1185 */ 1186 return SET_ERROR(EALREADY); 1187 } 1188 unp->unp_flags |= UNP_BUSY; 1189 sounlock(so); 1190 1191 sun = makeun_sb(nam, &addrlen); 1192 pb = pathbuf_create(sun->sun_path); 1193 if (pb == NULL) { 1194 error = SET_ERROR(ENOMEM); 1195 goto bad2; 1196 } 1197 1198 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb); 1199 1200 if ((error = namei(&nd)) != 0) { 1201 pathbuf_destroy(pb); 1202 goto bad2; 1203 } 1204 vp = nd.ni_vp; 1205 pathbuf_destroy(pb); 1206 if (vp->v_type != VSOCK) { 1207 error = SET_ERROR(ENOTSOCK); 1208 goto bad; 1209 } 1210 if ((error = VOP_ACCESS(vp, VWRITE, l->l_cred)) != 0) 1211 goto bad; 1212 /* Acquire v_interlock to protect against unp_detach(). */ 1213 mutex_enter(vp->v_interlock); 1214 so2 = vp->v_socket; 1215 if (so2 == NULL) { 1216 mutex_exit(vp->v_interlock); 1217 error = SET_ERROR(ECONNREFUSED); 1218 goto bad; 1219 } 1220 if (so->so_type != so2->so_type) { 1221 mutex_exit(vp->v_interlock); 1222 error = SET_ERROR(EPROTOTYPE); 1223 goto bad; 1224 } 1225 solock(so); 1226 unp_resetlock(so); 1227 mutex_exit(vp->v_interlock); 1228 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) { 1229 /* 1230 * This may seem somewhat fragile but is OK: if we can 1231 * see SO_ACCEPTCONN set on the endpoint, then it must 1232 * be locked by the domain-wide uipc_lock. 1233 */ 1234 KASSERT((so2->so_options & SO_ACCEPTCONN) == 0 || 1235 so2->so_lock == uipc_lock); 1236 if ((so2->so_options & SO_ACCEPTCONN) == 0 || 1237 (so3 = sonewconn(so2, false)) == NULL) { 1238 error = SET_ERROR(ECONNREFUSED); 1239 sounlock(so); 1240 goto bad; 1241 } 1242 unp2 = sotounpcb(so2); 1243 unp3 = sotounpcb(so3); 1244 if (unp2->unp_addr) { 1245 unp3->unp_addr = malloc(unp2->unp_addrlen, 1246 M_SONAME, M_WAITOK); 1247 memcpy(unp3->unp_addr, unp2->unp_addr, 1248 unp2->unp_addrlen); 1249 unp3->unp_addrlen = unp2->unp_addrlen; 1250 } 1251 unp3->unp_flags = unp2->unp_flags; 1252 so2 = so3; 1253 /* 1254 * The connector's (client's) credentials are copied from its 1255 * process structure at the time of connect() (which is now). 1256 */ 1257 unp_connid(l, unp3, UNP_EIDSVALID); 1258 /* 1259 * The receiver's (server's) credentials are copied from the 1260 * unp_peercred member of socket on which the former called 1261 * listen(); unp_listen() cached that process's credentials 1262 * at that time so we can use them now. 1263 */ 1264 if (unp2->unp_flags & UNP_EIDSBIND) { 1265 memcpy(&unp->unp_connid, &unp2->unp_connid, 1266 sizeof(unp->unp_connid)); 1267 unp->unp_flags |= UNP_EIDSVALID; 1268 } 1269 } 1270 error = unp_connect1(so, so2, l); 1271 if (error) { 1272 sounlock(so); 1273 goto bad; 1274 } 1275 unp2 = sotounpcb(so2); 1276 switch (so->so_type) { 1277 1278 /* 1279 * SOCK_DGRAM and default cases are handled in prior call to 1280 * unp_connect1(), do not add a default case without fixing 1281 * unp_connect1(). 1282 */ 1283 1284 case SOCK_SEQPACKET: /* FALLTHROUGH */ 1285 case SOCK_STREAM: 1286 unp2->unp_conn = unp; 1287 if ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT) 1288 soisconnecting(so); 1289 else 1290 soisconnected(so); 1291 soisconnected(so2); 1292 /* 1293 * If the connection is fully established, break the 1294 * association with uipc_lock and give the connected 1295 * pair a separate lock to share. 1296 */ 1297 KASSERT(so2->so_head != NULL); 1298 unp_setpeerlocks(so, so2); 1299 break; 1300 1301 } 1302 sounlock(so); 1303 bad: 1304 vput(vp); 1305 bad2: 1306 free(sun, M_SONAME); 1307 solock(so); 1308 unp->unp_flags &= ~UNP_BUSY; 1309 return (error); 1310 } 1311 1312 int 1313 unp_connect2(struct socket *so, struct socket *so2) 1314 { 1315 struct unpcb *unp = sotounpcb(so); 1316 struct unpcb *unp2; 1317 int error = 0; 1318 1319 KASSERT(solocked2(so, so2)); 1320 1321 error = unp_connect1(so, so2, curlwp); 1322 if (error) 1323 return error; 1324 1325 unp2 = sotounpcb(so2); 1326 switch (so->so_type) { 1327 1328 /* 1329 * SOCK_DGRAM and default cases are handled in prior call to 1330 * unp_connect1(), do not add a default case without fixing 1331 * unp_connect1(). 1332 */ 1333 1334 case SOCK_SEQPACKET: /* FALLTHROUGH */ 1335 case SOCK_STREAM: 1336 unp2->unp_conn = unp; 1337 soisconnected(so); 1338 soisconnected(so2); 1339 break; 1340 1341 } 1342 return error; 1343 } 1344 1345 static void 1346 unp_disconnect1(struct unpcb *unp) 1347 { 1348 struct unpcb *unp2 = unp->unp_conn; 1349 struct socket *so; 1350 1351 if (unp2 == 0) 1352 return; 1353 unp->unp_conn = 0; 1354 so = unp->unp_socket; 1355 switch (so->so_type) { 1356 case SOCK_DGRAM: 1357 if (unp2->unp_refs == unp) 1358 unp2->unp_refs = unp->unp_nextref; 1359 else { 1360 unp2 = unp2->unp_refs; 1361 for (;;) { 1362 KASSERT(solocked2(so, unp2->unp_socket)); 1363 if (unp2 == 0) 1364 panic("unp_disconnect1"); 1365 if (unp2->unp_nextref == unp) 1366 break; 1367 unp2 = unp2->unp_nextref; 1368 } 1369 unp2->unp_nextref = unp->unp_nextref; 1370 } 1371 unp->unp_nextref = 0; 1372 so->so_state &= ~SS_ISCONNECTED; 1373 break; 1374 1375 case SOCK_SEQPACKET: /* FALLTHROUGH */ 1376 case SOCK_STREAM: 1377 KASSERT(solocked2(so, unp2->unp_socket)); 1378 soisdisconnected(so); 1379 unp2->unp_conn = 0; 1380 soisdisconnected(unp2->unp_socket); 1381 break; 1382 } 1383 } 1384 1385 static void 1386 unp_shutdown1(struct unpcb *unp) 1387 { 1388 struct socket *so; 1389 1390 switch(unp->unp_socket->so_type) { 1391 case SOCK_SEQPACKET: /* FALLTHROUGH */ 1392 case SOCK_STREAM: 1393 if (unp->unp_conn && (so = unp->unp_conn->unp_socket)) 1394 socantrcvmore(so); 1395 break; 1396 default: 1397 break; 1398 } 1399 } 1400 1401 static bool 1402 unp_drop(struct unpcb *unp, int errno) 1403 { 1404 struct socket *so = unp->unp_socket; 1405 1406 KASSERT(solocked(so)); 1407 1408 so->so_error = errno; 1409 unp_disconnect1(unp); 1410 if (so->so_head) { 1411 so->so_pcb = NULL; 1412 /* sofree() drops the socket lock */ 1413 sofree(so); 1414 unp_free(unp); 1415 return true; 1416 } 1417 return false; 1418 } 1419 1420 #ifdef notdef 1421 unp_drain(void) 1422 { 1423 1424 } 1425 #endif 1426 1427 int 1428 unp_externalize(struct mbuf *rights, struct lwp *l, int flags) 1429 { 1430 struct cmsghdr * const cm = mtod(rights, struct cmsghdr *); 1431 struct proc * const p = l->l_proc; 1432 file_t **rp; 1433 int error = 0; 1434 1435 const size_t nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / 1436 sizeof(file_t *); 1437 if (nfds == 0) 1438 goto noop; 1439 1440 int * const fdp = kmem_alloc(nfds * sizeof(int), KM_SLEEP); 1441 rw_enter(&p->p_cwdi->cwdi_lock, RW_READER); 1442 1443 /* Make sure the recipient should be able to see the files.. */ 1444 rp = (file_t **)CMSG_DATA(cm); 1445 for (size_t i = 0; i < nfds; i++) { 1446 file_t * const fp = *rp++; 1447 if (fp == NULL) { 1448 error = SET_ERROR(EINVAL); 1449 goto out; 1450 } 1451 /* 1452 * If we are in a chroot'ed directory, and 1453 * someone wants to pass us a directory, make 1454 * sure it's inside the subtree we're allowed 1455 * to access. 1456 */ 1457 if (p->p_cwdi->cwdi_rdir != NULL && fp->f_type == DTYPE_VNODE) { 1458 vnode_t *vp = fp->f_vnode; 1459 if ((vp->v_type == VDIR) && 1460 !vn_isunder(vp, p->p_cwdi->cwdi_rdir, l)) { 1461 error = SET_ERROR(EPERM); 1462 goto out; 1463 } 1464 } 1465 } 1466 1467 restart: 1468 /* 1469 * First loop -- allocate file descriptor table slots for the 1470 * new files. 1471 */ 1472 for (size_t i = 0; i < nfds; i++) { 1473 if ((error = fd_alloc(p, 0, &fdp[i])) != 0) { 1474 /* 1475 * Back out what we've done so far. 1476 */ 1477 while (i-- > 0) { 1478 fd_abort(p, NULL, fdp[i]); 1479 } 1480 if (error == ENOSPC) { 1481 fd_tryexpand(p); 1482 error = 0; 1483 goto restart; 1484 } 1485 /* 1486 * This is the error that has historically 1487 * been returned, and some callers may 1488 * expect it. 1489 */ 1490 error = SET_ERROR(EMSGSIZE); 1491 goto out; 1492 } 1493 } 1494 1495 /* 1496 * Now that adding them has succeeded, update all of the 1497 * file passing state and affix the descriptors. 1498 */ 1499 rp = (file_t **)CMSG_DATA(cm); 1500 int *ofdp = (int *)CMSG_DATA(cm); 1501 for (size_t i = 0; i < nfds; i++) { 1502 file_t * const fp = *rp++; 1503 const int fd = fdp[i]; 1504 atomic_dec_uint(&unp_rights); 1505 fd_set_exclose(l, fd, (flags & O_CLOEXEC) != 0); 1506 fd_set_foclose(l, fd, (flags & O_CLOFORK) != 0); 1507 fd_affix(p, fp, fd); 1508 /* 1509 * Done with this file pointer, replace it with a fd; 1510 */ 1511 *ofdp++ = fd; 1512 mutex_enter(&fp->f_lock); 1513 fp->f_msgcount--; 1514 mutex_exit(&fp->f_lock); 1515 /* 1516 * Note that fd_affix() adds a reference to the file. 1517 * The file may already have been closed by another 1518 * LWP in the process, so we must drop the reference 1519 * added by unp_internalize() with closef(). 1520 */ 1521 closef(fp); 1522 } 1523 1524 /* 1525 * Adjust length, in case of transition from large file_t 1526 * pointers to ints. 1527 */ 1528 if (sizeof(file_t *) != sizeof(int)) { 1529 cm->cmsg_len = CMSG_LEN(nfds * sizeof(int)); 1530 rights->m_len = CMSG_SPACE(nfds * sizeof(int)); 1531 } 1532 out: 1533 if (__predict_false(error != 0)) { 1534 file_t **const fpp = (file_t **)CMSG_DATA(cm); 1535 for (size_t i = 0; i < nfds; i++) 1536 unp_discard_now(fpp[i]); 1537 /* 1538 * Truncate the array so that nobody will try to interpret 1539 * what is now garbage in it. 1540 */ 1541 cm->cmsg_len = CMSG_LEN(0); 1542 rights->m_len = CMSG_SPACE(0); 1543 } 1544 rw_exit(&p->p_cwdi->cwdi_lock); 1545 kmem_free(fdp, nfds * sizeof(int)); 1546 1547 noop: 1548 /* 1549 * Don't disclose kernel memory in the alignment space. 1550 */ 1551 KASSERT(cm->cmsg_len <= rights->m_len); 1552 memset(&mtod(rights, char *)[cm->cmsg_len], 0, rights->m_len - 1553 cm->cmsg_len); 1554 return error; 1555 } 1556 1557 static int 1558 unp_internalize(struct mbuf **controlp) 1559 { 1560 filedesc_t *fdescp = curlwp->l_fd; 1561 fdtab_t *dt; 1562 struct mbuf *control = *controlp; 1563 struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *); 1564 file_t **rp, **files; 1565 file_t *fp; 1566 int i, fd, *fdp; 1567 int nfds, error; 1568 u_int maxmsg; 1569 1570 error = 0; 1571 newcm = NULL; 1572 1573 /* Sanity check the control message header. */ 1574 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 1575 cm->cmsg_len > control->m_len || 1576 cm->cmsg_len < CMSG_ALIGN(sizeof(*cm))) 1577 return SET_ERROR(EINVAL); 1578 1579 /* 1580 * Verify that the file descriptors are valid, and acquire 1581 * a reference to each. 1582 */ 1583 nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int); 1584 fdp = (int *)CMSG_DATA(cm); 1585 maxmsg = maxfiles / unp_rights_ratio; 1586 for (i = 0; i < nfds; i++) { 1587 fd = *fdp++; 1588 if (atomic_inc_uint_nv(&unp_rights) > maxmsg) { 1589 atomic_dec_uint(&unp_rights); 1590 nfds = i; 1591 error = SET_ERROR(EAGAIN); 1592 goto out; 1593 } 1594 if ((fp = fd_getfile(fd)) == NULL 1595 || fp->f_type == DTYPE_KQUEUE) { 1596 if (fp) 1597 fd_putfile(fd); 1598 atomic_dec_uint(&unp_rights); 1599 nfds = i; 1600 error = SET_ERROR(EBADF); 1601 goto out; 1602 } 1603 } 1604 1605 /* Allocate new space and copy header into it. */ 1606 newcm = malloc(CMSG_SPACE(nfds * sizeof(file_t *)), M_MBUF, M_WAITOK); 1607 if (newcm == NULL) { 1608 error = SET_ERROR(E2BIG); 1609 goto out; 1610 } 1611 memcpy(newcm, cm, sizeof(struct cmsghdr)); 1612 memset(newcm + 1, 0, CMSG_LEN(0) - sizeof(struct cmsghdr)); 1613 files = (file_t **)CMSG_DATA(newcm); 1614 1615 /* 1616 * Transform the file descriptors into file_t pointers, in 1617 * reverse order so that if pointers are bigger than ints, the 1618 * int won't get until we're done. No need to lock, as we have 1619 * already validated the descriptors with fd_getfile(). 1620 */ 1621 fdp = (int *)CMSG_DATA(cm) + nfds; 1622 rp = files + nfds; 1623 for (i = 0; i < nfds; i++) { 1624 dt = atomic_load_consume(&fdescp->fd_dt); 1625 fp = atomic_load_consume(&dt->dt_ff[*--fdp]->ff_file); 1626 KASSERT(fp != NULL); 1627 mutex_enter(&fp->f_lock); 1628 *--rp = fp; 1629 fp->f_count++; 1630 fp->f_msgcount++; 1631 mutex_exit(&fp->f_lock); 1632 } 1633 1634 out: 1635 /* Release descriptor references. */ 1636 fdp = (int *)CMSG_DATA(cm); 1637 for (i = 0; i < nfds; i++) { 1638 fd_putfile(*fdp++); 1639 if (error != 0) { 1640 atomic_dec_uint(&unp_rights); 1641 } 1642 } 1643 1644 if (error == 0) { 1645 if (control->m_flags & M_EXT) { 1646 m_freem(control); 1647 *controlp = control = m_get(M_WAIT, MT_CONTROL); 1648 } 1649 MEXTADD(control, newcm, CMSG_SPACE(nfds * sizeof(file_t *)), 1650 M_MBUF, NULL, NULL); 1651 cm = newcm; 1652 /* 1653 * Adjust message & mbuf to note amount of space 1654 * actually used. 1655 */ 1656 cm->cmsg_len = CMSG_LEN(nfds * sizeof(file_t *)); 1657 control->m_len = CMSG_SPACE(nfds * sizeof(file_t *)); 1658 } 1659 1660 return error; 1661 } 1662 1663 struct mbuf * 1664 unp_addsockcred(struct lwp *l, struct mbuf *control) 1665 { 1666 struct sockcred *sc; 1667 struct mbuf *m; 1668 void *p; 1669 1670 m = sbcreatecontrol1(&p, SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)), 1671 SCM_CREDS, SOL_SOCKET, M_WAITOK); 1672 if (m == NULL) 1673 return control; 1674 1675 sc = p; 1676 sc->sc_pid = l->l_proc->p_pid; 1677 sc->sc_uid = kauth_cred_getuid(l->l_cred); 1678 sc->sc_euid = kauth_cred_geteuid(l->l_cred); 1679 sc->sc_gid = kauth_cred_getgid(l->l_cred); 1680 sc->sc_egid = kauth_cred_getegid(l->l_cred); 1681 sc->sc_ngroups = kauth_cred_ngroups(l->l_cred); 1682 1683 for (int i = 0; i < sc->sc_ngroups; i++) 1684 sc->sc_groups[i] = kauth_cred_group(l->l_cred, i); 1685 1686 return m_add(control, m); 1687 } 1688 1689 /* 1690 * Do a mark-sweep GC of files in the system, to free up any which are 1691 * caught in flight to an about-to-be-closed socket. Additionally, 1692 * process deferred file closures. 1693 */ 1694 static void 1695 unp_gc(file_t *dp) 1696 { 1697 extern struct domain unixdomain; 1698 file_t *fp, *np; 1699 struct socket *so, *so1; 1700 u_int i, oflags, rflags; 1701 bool didwork; 1702 1703 KASSERT(curlwp == unp_thread_lwp); 1704 KASSERT(mutex_owned(&filelist_lock)); 1705 1706 /* 1707 * First, process deferred file closures. 1708 */ 1709 while (!SLIST_EMPTY(&unp_thread_discard)) { 1710 fp = SLIST_FIRST(&unp_thread_discard); 1711 KASSERT(fp->f_unpcount > 0); 1712 KASSERT(fp->f_count > 0); 1713 KASSERT(fp->f_msgcount > 0); 1714 KASSERT(fp->f_count >= fp->f_unpcount); 1715 KASSERT(fp->f_count >= fp->f_msgcount); 1716 KASSERT(fp->f_msgcount >= fp->f_unpcount); 1717 SLIST_REMOVE_HEAD(&unp_thread_discard, f_unplist); 1718 i = fp->f_unpcount; 1719 fp->f_unpcount = 0; 1720 mutex_exit(&filelist_lock); 1721 for (; i != 0; i--) { 1722 unp_discard_now(fp); 1723 } 1724 mutex_enter(&filelist_lock); 1725 } 1726 1727 /* 1728 * Clear mark bits. Ensure that we don't consider new files 1729 * entering the file table during this loop (they will not have 1730 * FSCAN set). 1731 */ 1732 unp_defer = 0; 1733 LIST_FOREACH(fp, &filehead, f_list) { 1734 for (oflags = fp->f_flag;; oflags = rflags) { 1735 rflags = atomic_cas_uint(&fp->f_flag, oflags, 1736 (oflags | FSCAN) & ~(FMARK|FDEFER)); 1737 if (__predict_true(oflags == rflags)) { 1738 break; 1739 } 1740 } 1741 } 1742 1743 /* 1744 * Iterate over the set of sockets, marking ones believed (based on 1745 * refcount) to be referenced from a process, and marking for rescan 1746 * sockets which are queued on a socket. Recan continues descending 1747 * and searching for sockets referenced by sockets (FDEFER), until 1748 * there are no more socket->socket references to be discovered. 1749 */ 1750 do { 1751 didwork = false; 1752 for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) { 1753 KASSERT(mutex_owned(&filelist_lock)); 1754 np = LIST_NEXT(fp, f_list); 1755 mutex_enter(&fp->f_lock); 1756 if ((fp->f_flag & FDEFER) != 0) { 1757 atomic_and_uint(&fp->f_flag, ~FDEFER); 1758 unp_defer--; 1759 if (fp->f_count == 0) { 1760 /* 1761 * XXX: closef() doesn't pay attention 1762 * to FDEFER 1763 */ 1764 mutex_exit(&fp->f_lock); 1765 continue; 1766 } 1767 } else { 1768 if (fp->f_count == 0 || 1769 (fp->f_flag & FMARK) != 0 || 1770 fp->f_count == fp->f_msgcount || 1771 fp->f_unpcount != 0) { 1772 mutex_exit(&fp->f_lock); 1773 continue; 1774 } 1775 } 1776 atomic_or_uint(&fp->f_flag, FMARK); 1777 1778 if (fp->f_type != DTYPE_SOCKET || 1779 (so = fp->f_socket) == NULL || 1780 so->so_proto->pr_domain != &unixdomain || 1781 (so->so_proto->pr_flags & PR_RIGHTS) == 0) { 1782 mutex_exit(&fp->f_lock); 1783 continue; 1784 } 1785 1786 /* Gain file ref, mark our position, and unlock. */ 1787 didwork = true; 1788 LIST_INSERT_AFTER(fp, dp, f_list); 1789 fp->f_count++; 1790 mutex_exit(&fp->f_lock); 1791 mutex_exit(&filelist_lock); 1792 1793 /* 1794 * Mark files referenced from sockets queued on the 1795 * accept queue as well. 1796 */ 1797 solock(so); 1798 unp_scan(so->so_rcv.sb_mb, unp_mark, 0); 1799 if ((so->so_options & SO_ACCEPTCONN) != 0) { 1800 TAILQ_FOREACH(so1, &so->so_q0, so_qe) { 1801 unp_scan(so1->so_rcv.sb_mb, unp_mark, 0); 1802 } 1803 TAILQ_FOREACH(so1, &so->so_q, so_qe) { 1804 unp_scan(so1->so_rcv.sb_mb, unp_mark, 0); 1805 } 1806 } 1807 sounlock(so); 1808 1809 /* Re-lock and restart from where we left off. */ 1810 closef(fp); 1811 mutex_enter(&filelist_lock); 1812 np = LIST_NEXT(dp, f_list); 1813 LIST_REMOVE(dp, f_list); 1814 } 1815 /* 1816 * Bail early if we did nothing in the loop above. Could 1817 * happen because of concurrent activity causing unp_defer 1818 * to get out of sync. 1819 */ 1820 } while (unp_defer != 0 && didwork); 1821 1822 /* 1823 * Sweep pass. 1824 * 1825 * We grab an extra reference to each of the files that are 1826 * not otherwise accessible and then free the rights that are 1827 * stored in messages on them. 1828 */ 1829 for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) { 1830 KASSERT(mutex_owned(&filelist_lock)); 1831 np = LIST_NEXT(fp, f_list); 1832 mutex_enter(&fp->f_lock); 1833 1834 /* 1835 * Ignore non-sockets. 1836 * Ignore dead sockets, or sockets with pending close. 1837 * Ignore sockets obviously referenced elsewhere. 1838 * Ignore sockets marked as referenced by our scan. 1839 * Ignore new sockets that did not exist during the scan. 1840 */ 1841 if (fp->f_type != DTYPE_SOCKET || 1842 fp->f_count == 0 || fp->f_unpcount != 0 || 1843 fp->f_count != fp->f_msgcount || 1844 (fp->f_flag & (FMARK | FSCAN)) != FSCAN) { 1845 mutex_exit(&fp->f_lock); 1846 continue; 1847 } 1848 1849 /* Gain file ref, mark our position, and unlock. */ 1850 LIST_INSERT_AFTER(fp, dp, f_list); 1851 fp->f_count++; 1852 mutex_exit(&fp->f_lock); 1853 mutex_exit(&filelist_lock); 1854 1855 /* 1856 * Flush all data from the socket's receive buffer. 1857 * This will cause files referenced only by the 1858 * socket to be queued for close. 1859 */ 1860 so = fp->f_socket; 1861 solock(so); 1862 sorflush(so); 1863 sounlock(so); 1864 1865 /* Re-lock and restart from where we left off. */ 1866 closef(fp); 1867 mutex_enter(&filelist_lock); 1868 np = LIST_NEXT(dp, f_list); 1869 LIST_REMOVE(dp, f_list); 1870 } 1871 } 1872 1873 /* 1874 * Garbage collector thread. While SCM_RIGHTS messages are in transit, 1875 * wake once per second to garbage collect. Run continually while we 1876 * have deferred closes to process. 1877 */ 1878 static void 1879 unp_thread(void *cookie) 1880 { 1881 file_t *dp; 1882 1883 /* Allocate a dummy file for our scans. */ 1884 if ((dp = fgetdummy()) == NULL) { 1885 panic("unp_thread"); 1886 } 1887 1888 mutex_enter(&filelist_lock); 1889 for (;;) { 1890 KASSERT(mutex_owned(&filelist_lock)); 1891 if (SLIST_EMPTY(&unp_thread_discard)) { 1892 if (unp_rights != 0) { 1893 (void)cv_timedwait(&unp_thread_cv, 1894 &filelist_lock, hz); 1895 } else { 1896 cv_wait(&unp_thread_cv, &filelist_lock); 1897 } 1898 } 1899 unp_gc(dp); 1900 } 1901 /* NOTREACHED */ 1902 } 1903 1904 /* 1905 * Kick the garbage collector into action if there is something for 1906 * it to process. 1907 */ 1908 static void 1909 unp_thread_kick(void) 1910 { 1911 1912 if (!SLIST_EMPTY(&unp_thread_discard) || unp_rights != 0) { 1913 mutex_enter(&filelist_lock); 1914 cv_signal(&unp_thread_cv); 1915 mutex_exit(&filelist_lock); 1916 } 1917 } 1918 1919 void 1920 unp_dispose(struct mbuf *m) 1921 { 1922 1923 if (m) 1924 unp_scan(m, unp_discard_later, 1); 1925 } 1926 1927 void 1928 unp_scan(struct mbuf *m0, void (*op)(file_t *), int discard) 1929 { 1930 struct mbuf *m; 1931 file_t **rp, *fp; 1932 struct cmsghdr *cm; 1933 int i, qfds; 1934 1935 while (m0) { 1936 for (m = m0; m; m = m->m_next) { 1937 if (m->m_type != MT_CONTROL || 1938 m->m_len < sizeof(*cm)) { 1939 continue; 1940 } 1941 cm = mtod(m, struct cmsghdr *); 1942 if (cm->cmsg_level != SOL_SOCKET || 1943 cm->cmsg_type != SCM_RIGHTS) 1944 continue; 1945 qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) 1946 / sizeof(file_t *); 1947 rp = (file_t **)CMSG_DATA(cm); 1948 for (i = 0; i < qfds; i++) { 1949 fp = *rp; 1950 if (discard) { 1951 *rp = 0; 1952 } 1953 (*op)(fp); 1954 rp++; 1955 } 1956 } 1957 m0 = m0->m_nextpkt; 1958 } 1959 } 1960 1961 void 1962 unp_mark(file_t *fp) 1963 { 1964 1965 if (fp == NULL) 1966 return; 1967 1968 /* If we're already deferred, don't screw up the defer count */ 1969 mutex_enter(&fp->f_lock); 1970 if (fp->f_flag & (FMARK | FDEFER)) { 1971 mutex_exit(&fp->f_lock); 1972 return; 1973 } 1974 1975 /* 1976 * Minimize the number of deferrals... Sockets are the only type of 1977 * file which can hold references to another file, so just mark 1978 * other files, and defer unmarked sockets for the next pass. 1979 */ 1980 if (fp->f_type == DTYPE_SOCKET) { 1981 unp_defer++; 1982 KASSERT(fp->f_count != 0); 1983 atomic_or_uint(&fp->f_flag, FDEFER); 1984 } else { 1985 atomic_or_uint(&fp->f_flag, FMARK); 1986 } 1987 mutex_exit(&fp->f_lock); 1988 } 1989 1990 static void 1991 unp_discard_now(file_t *fp) 1992 { 1993 1994 if (fp == NULL) 1995 return; 1996 1997 KASSERT(fp->f_count > 0); 1998 KASSERT(fp->f_msgcount > 0); 1999 2000 mutex_enter(&fp->f_lock); 2001 fp->f_msgcount--; 2002 mutex_exit(&fp->f_lock); 2003 atomic_dec_uint(&unp_rights); 2004 (void)closef(fp); 2005 } 2006 2007 static void 2008 unp_discard_later(file_t *fp) 2009 { 2010 2011 if (fp == NULL) 2012 return; 2013 2014 KASSERT(fp->f_count > 0); 2015 KASSERT(fp->f_msgcount > 0); 2016 2017 mutex_enter(&filelist_lock); 2018 if (fp->f_unpcount++ == 0) { 2019 SLIST_INSERT_HEAD(&unp_thread_discard, fp, f_unplist); 2020 } 2021 mutex_exit(&filelist_lock); 2022 } 2023 2024 static void 2025 unp_sysctl_create(void) 2026 { 2027 2028 KASSERT(usrreq_sysctllog == NULL); 2029 sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL, 2030 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2031 CTLTYPE_LONG, "sendspace", 2032 SYSCTL_DESCR("Default stream send space"), 2033 NULL, 0, &unpst_sendspace, 0, 2034 CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL); 2035 sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL, 2036 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2037 CTLTYPE_LONG, "recvspace", 2038 SYSCTL_DESCR("Default stream recv space"), 2039 NULL, 0, &unpst_recvspace, 0, 2040 CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL); 2041 sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL, 2042 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2043 CTLTYPE_LONG, "sendspace", 2044 SYSCTL_DESCR("Default datagram send space"), 2045 NULL, 0, &unpdg_sendspace, 0, 2046 CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL); 2047 sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL, 2048 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2049 CTLTYPE_LONG, "recvspace", 2050 SYSCTL_DESCR("Default datagram recv space"), 2051 NULL, 0, &unpdg_recvspace, 0, 2052 CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL); 2053 sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL, 2054 CTLFLAG_PERMANENT|CTLFLAG_READONLY, 2055 CTLTYPE_INT, "inflight", 2056 SYSCTL_DESCR("File descriptors in flight"), 2057 NULL, 0, &unp_rights, 0, 2058 CTL_NET, PF_LOCAL, CTL_CREATE, CTL_EOL); 2059 sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL, 2060 CTLFLAG_PERMANENT|CTLFLAG_READONLY, 2061 CTLTYPE_INT, "deferred", 2062 SYSCTL_DESCR("File descriptors deferred for close"), 2063 NULL, 0, &unp_defer, 0, 2064 CTL_NET, PF_LOCAL, CTL_CREATE, CTL_EOL); 2065 } 2066 2067 const struct pr_usrreqs unp_usrreqs = { 2068 .pr_attach = unp_attach, 2069 .pr_detach = unp_detach, 2070 .pr_accept = unp_accept, 2071 .pr_bind = unp_bind, 2072 .pr_listen = unp_listen, 2073 .pr_connect = unp_connect, 2074 .pr_connect2 = unp_connect2, 2075 .pr_disconnect = unp_disconnect, 2076 .pr_shutdown = unp_shutdown, 2077 .pr_abort = unp_abort, 2078 .pr_ioctl = unp_ioctl, 2079 .pr_stat = unp_stat, 2080 .pr_peeraddr = unp_peeraddr, 2081 .pr_sockaddr = unp_sockaddr, 2082 .pr_rcvd = unp_rcvd, 2083 .pr_recvoob = unp_recvoob, 2084 .pr_send = unp_send, 2085 .pr_sendoob = unp_sendoob, 2086 }; 2087