1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All Rights Reserved. 6 * Copyright (c) 2004-2009 Robert N. M. Watson All Rights Reserved. 7 * Copyright (c) 2018 Matthew Macy 8 * Copyright (c) 2022 Gleb Smirnoff <glebius@FreeBSD.org> 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 35 */ 36 37 /* 38 * UNIX Domain (Local) Sockets 39 * 40 * This is an implementation of UNIX (local) domain sockets. Each socket has 41 * an associated struct unpcb (UNIX protocol control block). Stream sockets 42 * may be connected to 0 or 1 other socket. Datagram sockets may be 43 * connected to 0, 1, or many other sockets. Sockets may be created and 44 * connected in pairs (socketpair(2)), or bound/connected to using the file 45 * system name space. For most purposes, only the receive socket buffer is 46 * used, as sending on one socket delivers directly to the receive socket 47 * buffer of a second socket. 48 * 49 * The implementation is substantially complicated by the fact that 50 * "ancillary data", such as file descriptors or credentials, may be passed 51 * across UNIX domain sockets. The potential for passing UNIX domain sockets 52 * over other UNIX domain sockets requires the implementation of a simple 53 * garbage collector to find and tear down cycles of disconnected sockets. 54 * 55 * TODO: 56 * RDM 57 * rethink name space problems 58 * need a proper out-of-band 59 */ 60 61 #include <sys/cdefs.h> 62 #include "opt_ddb.h" 63 64 #include <sys/param.h> 65 #include <sys/capsicum.h> 66 #include <sys/domain.h> 67 #include <sys/eventhandler.h> 68 #include <sys/fcntl.h> 69 #include <sys/file.h> 70 #include <sys/filedesc.h> 71 #include <sys/kernel.h> 72 #include <sys/lock.h> 73 #include <sys/malloc.h> 74 #include <sys/mbuf.h> 75 #include <sys/mount.h> 76 #include <sys/mutex.h> 77 #include <sys/namei.h> 78 #include <sys/proc.h> 79 #include <sys/protosw.h> 80 #include <sys/queue.h> 81 #include <sys/resourcevar.h> 82 #include <sys/rwlock.h> 83 #include <sys/socket.h> 84 #include <sys/socketvar.h> 85 #include <sys/signalvar.h> 86 #include <sys/stat.h> 87 #include <sys/sx.h> 88 #include <sys/sysctl.h> 89 #include <sys/systm.h> 90 #include <sys/taskqueue.h> 91 #include <sys/un.h> 92 #include <sys/unpcb.h> 93 #include <sys/vnode.h> 94 95 #include <net/vnet.h> 96 97 #ifdef DDB 98 #include <ddb/ddb.h> 99 #endif 100 101 #include <security/mac/mac_framework.h> 102 103 #include <vm/uma.h> 104 105 MALLOC_DECLARE(M_FILECAPS); 106 107 static struct domain localdomain; 108 109 static uma_zone_t unp_zone; 110 static unp_gen_t unp_gencnt; /* (l) */ 111 static u_int unp_count; /* (l) Count of local sockets. */ 112 static ino_t unp_ino; /* Prototype for fake inode numbers. */ 113 static int unp_rights; /* (g) File descriptors in flight. */ 114 static struct unp_head unp_shead; /* (l) List of stream sockets. */ 115 static struct unp_head unp_dhead; /* (l) List of datagram sockets. */ 116 static struct unp_head unp_sphead; /* (l) List of seqpacket sockets. */ 117 118 struct unp_defer { 119 SLIST_ENTRY(unp_defer) ud_link; 120 struct file *ud_fp; 121 }; 122 static SLIST_HEAD(, unp_defer) unp_defers; 123 static int unp_defers_count; 124 125 static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 126 127 /* 128 * Garbage collection of cyclic file descriptor/socket references occurs 129 * asynchronously in a taskqueue context in order to avoid recursion and 130 * reentrance in the UNIX domain socket, file descriptor, and socket layer 131 * code. See unp_gc() for a full description. 132 */ 133 static struct timeout_task unp_gc_task; 134 135 /* 136 * The close of unix domain sockets attached as SCM_RIGHTS is 137 * postponed to the taskqueue, to avoid arbitrary recursion depth. 138 * The attached sockets might have another sockets attached. 139 */ 140 static struct task unp_defer_task; 141 142 /* 143 * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 144 * stream sockets, although the total for sender and receiver is actually 145 * only PIPSIZ. 146 * 147 * Datagram sockets really use the sendspace as the maximum datagram size, 148 * and don't really want to reserve the sendspace. Their recvspace should be 149 * large enough for at least one max-size datagram plus address. 150 */ 151 #ifndef PIPSIZ 152 #define PIPSIZ 8192 153 #endif 154 static u_long unpst_sendspace = PIPSIZ; 155 static u_long unpst_recvspace = PIPSIZ; 156 static u_long unpdg_maxdgram = 2*1024; 157 static u_long unpdg_recvspace = 16*1024; /* support 8KB syslog msgs */ 158 static u_long unpsp_sendspace = PIPSIZ; /* really max datagram size */ 159 static u_long unpsp_recvspace = PIPSIZ; 160 161 static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 162 "Local domain"); 163 static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, 164 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 165 "SOCK_STREAM"); 166 static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, 167 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 168 "SOCK_DGRAM"); 169 static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, 170 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 171 "SOCK_SEQPACKET"); 172 173 SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 174 &unpst_sendspace, 0, "Default stream send space."); 175 SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 176 &unpst_recvspace, 0, "Default stream receive space."); 177 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 178 &unpdg_maxdgram, 0, "Maximum datagram size."); 179 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 180 &unpdg_recvspace, 0, "Default datagram receive space."); 181 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW, 182 &unpsp_sendspace, 0, "Default seqpacket send space."); 183 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW, 184 &unpsp_recvspace, 0, "Default seqpacket receive space."); 185 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, 186 "File descriptors in flight."); 187 SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD, 188 &unp_defers_count, 0, 189 "File descriptors deferred to taskqueue for close."); 190 191 /* 192 * Locking and synchronization: 193 * 194 * Several types of locks exist in the local domain socket implementation: 195 * - a global linkage lock 196 * - a global connection list lock 197 * - the mtxpool lock 198 * - per-unpcb mutexes 199 * 200 * The linkage lock protects the global socket lists, the generation number 201 * counter and garbage collector state. 202 * 203 * The connection list lock protects the list of referring sockets in a datagram 204 * socket PCB. This lock is also overloaded to protect a global list of 205 * sockets whose buffers contain socket references in the form of SCM_RIGHTS 206 * messages. To avoid recursion, such references are released by a dedicated 207 * thread. 208 * 209 * The mtxpool lock protects the vnode from being modified while referenced. 210 * Lock ordering rules require that it be acquired before any PCB locks. 211 * 212 * The unpcb lock (unp_mtx) protects the most commonly referenced fields in the 213 * unpcb. This includes the unp_conn field, which either links two connected 214 * PCBs together (for connected socket types) or points at the destination 215 * socket (for connectionless socket types). The operations of creating or 216 * destroying a connection therefore involve locking multiple PCBs. To avoid 217 * lock order reversals, in some cases this involves dropping a PCB lock and 218 * using a reference counter to maintain liveness. 219 * 220 * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer, 221 * allocated in pr_attach() and freed in pr_detach(). The validity of that 222 * pointer is an invariant, so no lock is required to dereference the so_pcb 223 * pointer if a valid socket reference is held by the caller. In practice, 224 * this is always true during operations performed on a socket. Each unpcb 225 * has a back-pointer to its socket, unp_socket, which will be stable under 226 * the same circumstances. 227 * 228 * This pointer may only be safely dereferenced as long as a valid reference 229 * to the unpcb is held. Typically, this reference will be from the socket, 230 * or from another unpcb when the referring unpcb's lock is held (in order 231 * that the reference not be invalidated during use). For example, to follow 232 * unp->unp_conn->unp_socket, you need to hold a lock on unp_conn to guarantee 233 * that detach is not run clearing unp_socket. 234 * 235 * Blocking with UNIX domain sockets is a tricky issue: unlike most network 236 * protocols, bind() is a non-atomic operation, and connect() requires 237 * potential sleeping in the protocol, due to potentially waiting on local or 238 * distributed file systems. We try to separate "lookup" operations, which 239 * may sleep, and the IPC operations themselves, which typically can occur 240 * with relative atomicity as locks can be held over the entire operation. 241 * 242 * Another tricky issue is simultaneous multi-threaded or multi-process 243 * access to a single UNIX domain socket. These are handled by the flags 244 * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or 245 * binding, both of which involve dropping UNIX domain socket locks in order 246 * to perform namei() and other file system operations. 247 */ 248 static struct rwlock unp_link_rwlock; 249 static struct mtx unp_defers_lock; 250 251 #define UNP_LINK_LOCK_INIT() rw_init(&unp_link_rwlock, \ 252 "unp_link_rwlock") 253 254 #define UNP_LINK_LOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 255 RA_LOCKED) 256 #define UNP_LINK_UNLOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 257 RA_UNLOCKED) 258 259 #define UNP_LINK_RLOCK() rw_rlock(&unp_link_rwlock) 260 #define UNP_LINK_RUNLOCK() rw_runlock(&unp_link_rwlock) 261 #define UNP_LINK_WLOCK() rw_wlock(&unp_link_rwlock) 262 #define UNP_LINK_WUNLOCK() rw_wunlock(&unp_link_rwlock) 263 #define UNP_LINK_WLOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 264 RA_WLOCKED) 265 #define UNP_LINK_WOWNED() rw_wowned(&unp_link_rwlock) 266 267 #define UNP_DEFERRED_LOCK_INIT() mtx_init(&unp_defers_lock, \ 268 "unp_defer", NULL, MTX_DEF) 269 #define UNP_DEFERRED_LOCK() mtx_lock(&unp_defers_lock) 270 #define UNP_DEFERRED_UNLOCK() mtx_unlock(&unp_defers_lock) 271 272 #define UNP_REF_LIST_LOCK() UNP_DEFERRED_LOCK(); 273 #define UNP_REF_LIST_UNLOCK() UNP_DEFERRED_UNLOCK(); 274 275 #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \ 276 "unp", "unp", \ 277 MTX_DUPOK|MTX_DEF) 278 #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx) 279 #define UNP_PCB_LOCKPTR(unp) (&(unp)->unp_mtx) 280 #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx) 281 #define UNP_PCB_TRYLOCK(unp) mtx_trylock(&(unp)->unp_mtx) 282 #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx) 283 #define UNP_PCB_OWNED(unp) mtx_owned(&(unp)->unp_mtx) 284 #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED) 285 #define UNP_PCB_UNLOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_NOTOWNED) 286 287 static int uipc_connect2(struct socket *, struct socket *); 288 static int uipc_ctloutput(struct socket *, struct sockopt *); 289 static int unp_connect(struct socket *, struct sockaddr *, 290 struct thread *); 291 static int unp_connectat(int, struct socket *, struct sockaddr *, 292 struct thread *, bool); 293 typedef enum { PRU_CONNECT, PRU_CONNECT2 } conn2_how; 294 static void unp_connect2(struct socket *so, struct socket *so2, conn2_how); 295 static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); 296 static void unp_dispose(struct socket *so); 297 static void unp_shutdown(struct unpcb *); 298 static void unp_drop(struct unpcb *); 299 static void unp_gc(__unused void *, int); 300 static void unp_scan(struct mbuf *, void (*)(struct filedescent **, int)); 301 static void unp_discard(struct file *); 302 static void unp_freerights(struct filedescent **, int); 303 static int unp_internalize(struct mbuf **, struct thread *, 304 struct mbuf **, u_int *, u_int *); 305 static void unp_internalize_fp(struct file *); 306 static int unp_externalize(struct mbuf *, struct mbuf **, int); 307 static int unp_externalize_fp(struct file *); 308 static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *, 309 int, struct mbuf **, u_int *, u_int *); 310 static void unp_process_defers(void * __unused, int); 311 312 static void 313 unp_pcb_hold(struct unpcb *unp) 314 { 315 u_int old __unused; 316 317 old = refcount_acquire(&unp->unp_refcount); 318 KASSERT(old > 0, ("%s: unpcb %p has no references", __func__, unp)); 319 } 320 321 static __result_use_check bool 322 unp_pcb_rele(struct unpcb *unp) 323 { 324 bool ret; 325 326 UNP_PCB_LOCK_ASSERT(unp); 327 328 if ((ret = refcount_release(&unp->unp_refcount))) { 329 UNP_PCB_UNLOCK(unp); 330 UNP_PCB_LOCK_DESTROY(unp); 331 uma_zfree(unp_zone, unp); 332 } 333 return (ret); 334 } 335 336 static void 337 unp_pcb_rele_notlast(struct unpcb *unp) 338 { 339 bool ret __unused; 340 341 ret = refcount_release(&unp->unp_refcount); 342 KASSERT(!ret, ("%s: unpcb %p has no references", __func__, unp)); 343 } 344 345 static void 346 unp_pcb_lock_pair(struct unpcb *unp, struct unpcb *unp2) 347 { 348 UNP_PCB_UNLOCK_ASSERT(unp); 349 UNP_PCB_UNLOCK_ASSERT(unp2); 350 351 if (unp == unp2) { 352 UNP_PCB_LOCK(unp); 353 } else if ((uintptr_t)unp2 > (uintptr_t)unp) { 354 UNP_PCB_LOCK(unp); 355 UNP_PCB_LOCK(unp2); 356 } else { 357 UNP_PCB_LOCK(unp2); 358 UNP_PCB_LOCK(unp); 359 } 360 } 361 362 static void 363 unp_pcb_unlock_pair(struct unpcb *unp, struct unpcb *unp2) 364 { 365 UNP_PCB_UNLOCK(unp); 366 if (unp != unp2) 367 UNP_PCB_UNLOCK(unp2); 368 } 369 370 /* 371 * Try to lock the connected peer of an already locked socket. In some cases 372 * this requires that we unlock the current socket. The pairbusy counter is 373 * used to block concurrent connection attempts while the lock is dropped. The 374 * caller must be careful to revalidate PCB state. 375 */ 376 static struct unpcb * 377 unp_pcb_lock_peer(struct unpcb *unp) 378 { 379 struct unpcb *unp2; 380 381 UNP_PCB_LOCK_ASSERT(unp); 382 unp2 = unp->unp_conn; 383 if (unp2 == NULL) 384 return (NULL); 385 if (__predict_false(unp == unp2)) 386 return (unp); 387 388 UNP_PCB_UNLOCK_ASSERT(unp2); 389 390 if (__predict_true(UNP_PCB_TRYLOCK(unp2))) 391 return (unp2); 392 if ((uintptr_t)unp2 > (uintptr_t)unp) { 393 UNP_PCB_LOCK(unp2); 394 return (unp2); 395 } 396 unp->unp_pairbusy++; 397 unp_pcb_hold(unp2); 398 UNP_PCB_UNLOCK(unp); 399 400 UNP_PCB_LOCK(unp2); 401 UNP_PCB_LOCK(unp); 402 KASSERT(unp->unp_conn == unp2 || unp->unp_conn == NULL, 403 ("%s: socket %p was reconnected", __func__, unp)); 404 if (--unp->unp_pairbusy == 0 && (unp->unp_flags & UNP_WAITING) != 0) { 405 unp->unp_flags &= ~UNP_WAITING; 406 wakeup(unp); 407 } 408 if (unp_pcb_rele(unp2)) { 409 /* unp2 is unlocked. */ 410 return (NULL); 411 } 412 if (unp->unp_conn == NULL) { 413 UNP_PCB_UNLOCK(unp2); 414 return (NULL); 415 } 416 return (unp2); 417 } 418 419 static void 420 uipc_abort(struct socket *so) 421 { 422 struct unpcb *unp, *unp2; 423 424 unp = sotounpcb(so); 425 KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 426 UNP_PCB_UNLOCK_ASSERT(unp); 427 428 UNP_PCB_LOCK(unp); 429 unp2 = unp->unp_conn; 430 if (unp2 != NULL) { 431 unp_pcb_hold(unp2); 432 UNP_PCB_UNLOCK(unp); 433 unp_drop(unp2); 434 } else 435 UNP_PCB_UNLOCK(unp); 436 } 437 438 static int 439 uipc_accept(struct socket *so, struct sockaddr **nam) 440 { 441 struct unpcb *unp, *unp2; 442 const struct sockaddr *sa; 443 444 /* 445 * Pass back name of connected socket, if it was bound and we are 446 * still connected (our peer may have closed already!). 447 */ 448 unp = sotounpcb(so); 449 KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 450 451 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 452 UNP_PCB_LOCK(unp); 453 unp2 = unp_pcb_lock_peer(unp); 454 if (unp2 != NULL && unp2->unp_addr != NULL) 455 sa = (struct sockaddr *)unp2->unp_addr; 456 else 457 sa = &sun_noname; 458 bcopy(sa, *nam, sa->sa_len); 459 if (unp2 != NULL) 460 unp_pcb_unlock_pair(unp, unp2); 461 else 462 UNP_PCB_UNLOCK(unp); 463 return (0); 464 } 465 466 static int 467 uipc_attach(struct socket *so, int proto, struct thread *td) 468 { 469 u_long sendspace, recvspace; 470 struct unpcb *unp; 471 int error; 472 bool locked; 473 474 KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 475 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 476 switch (so->so_type) { 477 case SOCK_STREAM: 478 sendspace = unpst_sendspace; 479 recvspace = unpst_recvspace; 480 break; 481 482 case SOCK_DGRAM: 483 STAILQ_INIT(&so->so_rcv.uxdg_mb); 484 STAILQ_INIT(&so->so_snd.uxdg_mb); 485 TAILQ_INIT(&so->so_rcv.uxdg_conns); 486 /* 487 * Since send buffer is either bypassed or is a part 488 * of one-to-many receive buffer, we assign both space 489 * limits to unpdg_recvspace. 490 */ 491 sendspace = recvspace = unpdg_recvspace; 492 break; 493 494 case SOCK_SEQPACKET: 495 sendspace = unpsp_sendspace; 496 recvspace = unpsp_recvspace; 497 break; 498 499 default: 500 panic("uipc_attach"); 501 } 502 error = soreserve(so, sendspace, recvspace); 503 if (error) 504 return (error); 505 } 506 unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO); 507 if (unp == NULL) 508 return (ENOBUFS); 509 LIST_INIT(&unp->unp_refs); 510 UNP_PCB_LOCK_INIT(unp); 511 unp->unp_socket = so; 512 so->so_pcb = unp; 513 refcount_init(&unp->unp_refcount, 1); 514 515 if ((locked = UNP_LINK_WOWNED()) == false) 516 UNP_LINK_WLOCK(); 517 518 unp->unp_gencnt = ++unp_gencnt; 519 unp->unp_ino = ++unp_ino; 520 unp_count++; 521 switch (so->so_type) { 522 case SOCK_STREAM: 523 LIST_INSERT_HEAD(&unp_shead, unp, unp_link); 524 break; 525 526 case SOCK_DGRAM: 527 LIST_INSERT_HEAD(&unp_dhead, unp, unp_link); 528 break; 529 530 case SOCK_SEQPACKET: 531 LIST_INSERT_HEAD(&unp_sphead, unp, unp_link); 532 break; 533 534 default: 535 panic("uipc_attach"); 536 } 537 538 if (locked == false) 539 UNP_LINK_WUNLOCK(); 540 541 return (0); 542 } 543 544 static int 545 uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td) 546 { 547 struct sockaddr_un *soun = (struct sockaddr_un *)nam; 548 struct vattr vattr; 549 int error, namelen; 550 struct nameidata nd; 551 struct unpcb *unp; 552 struct vnode *vp; 553 struct mount *mp; 554 cap_rights_t rights; 555 char *buf; 556 557 if (nam->sa_family != AF_UNIX) 558 return (EAFNOSUPPORT); 559 560 unp = sotounpcb(so); 561 KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 562 563 if (soun->sun_len > sizeof(struct sockaddr_un)) 564 return (EINVAL); 565 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 566 if (namelen <= 0) 567 return (EINVAL); 568 569 /* 570 * We don't allow simultaneous bind() calls on a single UNIX domain 571 * socket, so flag in-progress operations, and return an error if an 572 * operation is already in progress. 573 * 574 * Historically, we have not allowed a socket to be rebound, so this 575 * also returns an error. Not allowing re-binding simplifies the 576 * implementation and avoids a great many possible failure modes. 577 */ 578 UNP_PCB_LOCK(unp); 579 if (unp->unp_vnode != NULL) { 580 UNP_PCB_UNLOCK(unp); 581 return (EINVAL); 582 } 583 if (unp->unp_flags & UNP_BINDING) { 584 UNP_PCB_UNLOCK(unp); 585 return (EALREADY); 586 } 587 unp->unp_flags |= UNP_BINDING; 588 UNP_PCB_UNLOCK(unp); 589 590 buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 591 bcopy(soun->sun_path, buf, namelen); 592 buf[namelen] = 0; 593 594 restart: 595 NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | NOCACHE, 596 UIO_SYSSPACE, buf, fd, cap_rights_init_one(&rights, CAP_BINDAT)); 597 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 598 error = namei(&nd); 599 if (error) 600 goto error; 601 vp = nd.ni_vp; 602 if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 603 NDFREE_PNBUF(&nd); 604 if (nd.ni_dvp == vp) 605 vrele(nd.ni_dvp); 606 else 607 vput(nd.ni_dvp); 608 if (vp != NULL) { 609 vrele(vp); 610 error = EADDRINUSE; 611 goto error; 612 } 613 error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH); 614 if (error) 615 goto error; 616 goto restart; 617 } 618 VATTR_NULL(&vattr); 619 vattr.va_type = VSOCK; 620 vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_pd->pd_cmask); 621 #ifdef MAC 622 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 623 &vattr); 624 #endif 625 if (error == 0) 626 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 627 NDFREE_PNBUF(&nd); 628 if (error) { 629 VOP_VPUT_PAIR(nd.ni_dvp, NULL, true); 630 vn_finished_write(mp); 631 if (error == ERELOOKUP) 632 goto restart; 633 goto error; 634 } 635 vp = nd.ni_vp; 636 ASSERT_VOP_ELOCKED(vp, "uipc_bind"); 637 soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 638 639 UNP_PCB_LOCK(unp); 640 VOP_UNP_BIND(vp, unp); 641 unp->unp_vnode = vp; 642 unp->unp_addr = soun; 643 unp->unp_flags &= ~UNP_BINDING; 644 UNP_PCB_UNLOCK(unp); 645 vref(vp); 646 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true); 647 vn_finished_write(mp); 648 free(buf, M_TEMP); 649 return (0); 650 651 error: 652 UNP_PCB_LOCK(unp); 653 unp->unp_flags &= ~UNP_BINDING; 654 UNP_PCB_UNLOCK(unp); 655 free(buf, M_TEMP); 656 return (error); 657 } 658 659 static int 660 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 661 { 662 663 return (uipc_bindat(AT_FDCWD, so, nam, td)); 664 } 665 666 static int 667 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 668 { 669 int error; 670 671 KASSERT(td == curthread, ("uipc_connect: td != curthread")); 672 error = unp_connect(so, nam, td); 673 return (error); 674 } 675 676 static int 677 uipc_connectat(int fd, struct socket *so, struct sockaddr *nam, 678 struct thread *td) 679 { 680 int error; 681 682 KASSERT(td == curthread, ("uipc_connectat: td != curthread")); 683 error = unp_connectat(fd, so, nam, td, false); 684 return (error); 685 } 686 687 static void 688 uipc_close(struct socket *so) 689 { 690 struct unpcb *unp, *unp2; 691 struct vnode *vp = NULL; 692 struct mtx *vplock; 693 694 unp = sotounpcb(so); 695 KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 696 697 vplock = NULL; 698 if ((vp = unp->unp_vnode) != NULL) { 699 vplock = mtx_pool_find(mtxpool_sleep, vp); 700 mtx_lock(vplock); 701 } 702 UNP_PCB_LOCK(unp); 703 if (vp && unp->unp_vnode == NULL) { 704 mtx_unlock(vplock); 705 vp = NULL; 706 } 707 if (vp != NULL) { 708 VOP_UNP_DETACH(vp); 709 unp->unp_vnode = NULL; 710 } 711 if ((unp2 = unp_pcb_lock_peer(unp)) != NULL) 712 unp_disconnect(unp, unp2); 713 else 714 UNP_PCB_UNLOCK(unp); 715 if (vp) { 716 mtx_unlock(vplock); 717 vrele(vp); 718 } 719 } 720 721 static int 722 uipc_connect2(struct socket *so1, struct socket *so2) 723 { 724 struct unpcb *unp, *unp2; 725 726 if (so1->so_type != so2->so_type) 727 return (EPROTOTYPE); 728 729 unp = so1->so_pcb; 730 KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 731 unp2 = so2->so_pcb; 732 KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL")); 733 unp_pcb_lock_pair(unp, unp2); 734 unp_connect2(so1, so2, PRU_CONNECT2); 735 unp_pcb_unlock_pair(unp, unp2); 736 737 return (0); 738 } 739 740 static void 741 uipc_detach(struct socket *so) 742 { 743 struct unpcb *unp, *unp2; 744 struct mtx *vplock; 745 struct vnode *vp; 746 int local_unp_rights; 747 748 unp = sotounpcb(so); 749 KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 750 751 vp = NULL; 752 vplock = NULL; 753 754 UNP_LINK_WLOCK(); 755 LIST_REMOVE(unp, unp_link); 756 if (unp->unp_gcflag & UNPGC_DEAD) 757 LIST_REMOVE(unp, unp_dead); 758 unp->unp_gencnt = ++unp_gencnt; 759 --unp_count; 760 UNP_LINK_WUNLOCK(); 761 762 UNP_PCB_UNLOCK_ASSERT(unp); 763 restart: 764 if ((vp = unp->unp_vnode) != NULL) { 765 vplock = mtx_pool_find(mtxpool_sleep, vp); 766 mtx_lock(vplock); 767 } 768 UNP_PCB_LOCK(unp); 769 if (unp->unp_vnode != vp && unp->unp_vnode != NULL) { 770 if (vplock) 771 mtx_unlock(vplock); 772 UNP_PCB_UNLOCK(unp); 773 goto restart; 774 } 775 if ((vp = unp->unp_vnode) != NULL) { 776 VOP_UNP_DETACH(vp); 777 unp->unp_vnode = NULL; 778 } 779 if ((unp2 = unp_pcb_lock_peer(unp)) != NULL) 780 unp_disconnect(unp, unp2); 781 else 782 UNP_PCB_UNLOCK(unp); 783 784 UNP_REF_LIST_LOCK(); 785 while (!LIST_EMPTY(&unp->unp_refs)) { 786 struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 787 788 unp_pcb_hold(ref); 789 UNP_REF_LIST_UNLOCK(); 790 791 MPASS(ref != unp); 792 UNP_PCB_UNLOCK_ASSERT(ref); 793 unp_drop(ref); 794 UNP_REF_LIST_LOCK(); 795 } 796 UNP_REF_LIST_UNLOCK(); 797 798 UNP_PCB_LOCK(unp); 799 local_unp_rights = unp_rights; 800 unp->unp_socket->so_pcb = NULL; 801 unp->unp_socket = NULL; 802 free(unp->unp_addr, M_SONAME); 803 unp->unp_addr = NULL; 804 if (!unp_pcb_rele(unp)) 805 UNP_PCB_UNLOCK(unp); 806 if (vp) { 807 mtx_unlock(vplock); 808 vrele(vp); 809 } 810 if (local_unp_rights) 811 taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1); 812 813 switch (so->so_type) { 814 case SOCK_DGRAM: 815 /* 816 * Everything should have been unlinked/freed by unp_dispose() 817 * and/or unp_disconnect(). 818 */ 819 MPASS(so->so_rcv.uxdg_peeked == NULL); 820 MPASS(STAILQ_EMPTY(&so->so_rcv.uxdg_mb)); 821 MPASS(TAILQ_EMPTY(&so->so_rcv.uxdg_conns)); 822 MPASS(STAILQ_EMPTY(&so->so_snd.uxdg_mb)); 823 } 824 } 825 826 static int 827 uipc_disconnect(struct socket *so) 828 { 829 struct unpcb *unp, *unp2; 830 831 unp = sotounpcb(so); 832 KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 833 834 UNP_PCB_LOCK(unp); 835 if ((unp2 = unp_pcb_lock_peer(unp)) != NULL) 836 unp_disconnect(unp, unp2); 837 else 838 UNP_PCB_UNLOCK(unp); 839 return (0); 840 } 841 842 static int 843 uipc_listen(struct socket *so, int backlog, struct thread *td) 844 { 845 struct unpcb *unp; 846 int error; 847 848 MPASS(so->so_type != SOCK_DGRAM); 849 850 /* 851 * Synchronize with concurrent connection attempts. 852 */ 853 error = 0; 854 unp = sotounpcb(so); 855 UNP_PCB_LOCK(unp); 856 if (unp->unp_conn != NULL || (unp->unp_flags & UNP_CONNECTING) != 0) 857 error = EINVAL; 858 else if (unp->unp_vnode == NULL) 859 error = EDESTADDRREQ; 860 if (error != 0) { 861 UNP_PCB_UNLOCK(unp); 862 return (error); 863 } 864 865 SOCK_LOCK(so); 866 error = solisten_proto_check(so); 867 if (error == 0) { 868 cru2xt(td, &unp->unp_peercred); 869 solisten_proto(so, backlog); 870 } 871 SOCK_UNLOCK(so); 872 UNP_PCB_UNLOCK(unp); 873 return (error); 874 } 875 876 static int 877 uipc_peeraddr(struct socket *so, struct sockaddr **nam) 878 { 879 struct unpcb *unp, *unp2; 880 const struct sockaddr *sa; 881 882 unp = sotounpcb(so); 883 KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 884 885 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 886 887 UNP_PCB_LOCK(unp); 888 unp2 = unp_pcb_lock_peer(unp); 889 if (unp2 != NULL) { 890 if (unp2->unp_addr != NULL) 891 sa = (struct sockaddr *)unp2->unp_addr; 892 else 893 sa = &sun_noname; 894 bcopy(sa, *nam, sa->sa_len); 895 unp_pcb_unlock_pair(unp, unp2); 896 } else { 897 sa = &sun_noname; 898 bcopy(sa, *nam, sa->sa_len); 899 UNP_PCB_UNLOCK(unp); 900 } 901 return (0); 902 } 903 904 static int 905 uipc_rcvd(struct socket *so, int flags) 906 { 907 struct unpcb *unp, *unp2; 908 struct socket *so2; 909 u_int mbcnt, sbcc; 910 911 unp = sotounpcb(so); 912 KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 913 KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET, 914 ("%s: socktype %d", __func__, so->so_type)); 915 916 /* 917 * Adjust backpressure on sender and wakeup any waiting to write. 918 * 919 * The unp lock is acquired to maintain the validity of the unp_conn 920 * pointer; no lock on unp2 is required as unp2->unp_socket will be 921 * static as long as we don't permit unp2 to disconnect from unp, 922 * which is prevented by the lock on unp. We cache values from 923 * so_rcv to avoid holding the so_rcv lock over the entire 924 * transaction on the remote so_snd. 925 */ 926 SOCKBUF_LOCK(&so->so_rcv); 927 mbcnt = so->so_rcv.sb_mbcnt; 928 sbcc = sbavail(&so->so_rcv); 929 SOCKBUF_UNLOCK(&so->so_rcv); 930 /* 931 * There is a benign race condition at this point. If we're planning to 932 * clear SB_STOP, but uipc_send is called on the connected socket at 933 * this instant, it might add data to the sockbuf and set SB_STOP. Then 934 * we would erroneously clear SB_STOP below, even though the sockbuf is 935 * full. The race is benign because the only ill effect is to allow the 936 * sockbuf to exceed its size limit, and the size limits are not 937 * strictly guaranteed anyway. 938 */ 939 UNP_PCB_LOCK(unp); 940 unp2 = unp->unp_conn; 941 if (unp2 == NULL) { 942 UNP_PCB_UNLOCK(unp); 943 return (0); 944 } 945 so2 = unp2->unp_socket; 946 SOCKBUF_LOCK(&so2->so_snd); 947 if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax) 948 so2->so_snd.sb_flags &= ~SB_STOP; 949 sowwakeup_locked(so2); 950 UNP_PCB_UNLOCK(unp); 951 return (0); 952 } 953 954 static int 955 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 956 struct mbuf *control, struct thread *td) 957 { 958 struct unpcb *unp, *unp2; 959 struct socket *so2; 960 u_int mbcnt, sbcc; 961 int error; 962 963 unp = sotounpcb(so); 964 KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 965 KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET, 966 ("%s: socktype %d", __func__, so->so_type)); 967 968 error = 0; 969 if (flags & PRUS_OOB) { 970 error = EOPNOTSUPP; 971 goto release; 972 } 973 if (control != NULL && 974 (error = unp_internalize(&control, td, NULL, NULL, NULL))) 975 goto release; 976 977 unp2 = NULL; 978 if ((so->so_state & SS_ISCONNECTED) == 0) { 979 if (nam != NULL) { 980 if ((error = unp_connect(so, nam, td)) != 0) 981 goto out; 982 } else { 983 error = ENOTCONN; 984 goto out; 985 } 986 } 987 988 UNP_PCB_LOCK(unp); 989 if ((unp2 = unp_pcb_lock_peer(unp)) == NULL) { 990 UNP_PCB_UNLOCK(unp); 991 error = ENOTCONN; 992 goto out; 993 } else if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 994 unp_pcb_unlock_pair(unp, unp2); 995 error = EPIPE; 996 goto out; 997 } 998 UNP_PCB_UNLOCK(unp); 999 if ((so2 = unp2->unp_socket) == NULL) { 1000 UNP_PCB_UNLOCK(unp2); 1001 error = ENOTCONN; 1002 goto out; 1003 } 1004 SOCKBUF_LOCK(&so2->so_rcv); 1005 if (unp2->unp_flags & UNP_WANTCRED_MASK) { 1006 /* 1007 * Credentials are passed only once on SOCK_STREAM and 1008 * SOCK_SEQPACKET (LOCAL_CREDS => WANTCRED_ONESHOT), or 1009 * forever (LOCAL_CREDS_PERSISTENT => WANTCRED_ALWAYS). 1010 */ 1011 control = unp_addsockcred(td, control, unp2->unp_flags, NULL, 1012 NULL, NULL); 1013 unp2->unp_flags &= ~UNP_WANTCRED_ONESHOT; 1014 } 1015 1016 /* 1017 * Send to paired receive port and wake up readers. Don't 1018 * check for space available in the receive buffer if we're 1019 * attaching ancillary data; Unix domain sockets only check 1020 * for space in the sending sockbuf, and that check is 1021 * performed one level up the stack. At that level we cannot 1022 * precisely account for the amount of buffer space used 1023 * (e.g., because control messages are not yet internalized). 1024 */ 1025 switch (so->so_type) { 1026 case SOCK_STREAM: 1027 if (control != NULL) { 1028 sbappendcontrol_locked(&so2->so_rcv, m, 1029 control, flags); 1030 control = NULL; 1031 } else 1032 sbappend_locked(&so2->so_rcv, m, flags); 1033 break; 1034 1035 case SOCK_SEQPACKET: 1036 if (sbappendaddr_nospacecheck_locked(&so2->so_rcv, 1037 &sun_noname, m, control)) 1038 control = NULL; 1039 break; 1040 } 1041 1042 mbcnt = so2->so_rcv.sb_mbcnt; 1043 sbcc = sbavail(&so2->so_rcv); 1044 if (sbcc) 1045 sorwakeup_locked(so2); 1046 else 1047 SOCKBUF_UNLOCK(&so2->so_rcv); 1048 1049 /* 1050 * The PCB lock on unp2 protects the SB_STOP flag. Without it, 1051 * it would be possible for uipc_rcvd to be called at this 1052 * point, drain the receiving sockbuf, clear SB_STOP, and then 1053 * we would set SB_STOP below. That could lead to an empty 1054 * sockbuf having SB_STOP set 1055 */ 1056 SOCKBUF_LOCK(&so->so_snd); 1057 if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax) 1058 so->so_snd.sb_flags |= SB_STOP; 1059 SOCKBUF_UNLOCK(&so->so_snd); 1060 UNP_PCB_UNLOCK(unp2); 1061 m = NULL; 1062 out: 1063 /* 1064 * PRUS_EOF is equivalent to pr_send followed by pr_shutdown. 1065 */ 1066 if (flags & PRUS_EOF) { 1067 UNP_PCB_LOCK(unp); 1068 socantsendmore(so); 1069 unp_shutdown(unp); 1070 UNP_PCB_UNLOCK(unp); 1071 } 1072 if (control != NULL && error != 0) 1073 unp_scan(control, unp_freerights); 1074 1075 release: 1076 if (control != NULL) 1077 m_freem(control); 1078 /* 1079 * In case of PRUS_NOTREADY, uipc_ready() is responsible 1080 * for freeing memory. 1081 */ 1082 if (m != NULL && (flags & PRUS_NOTREADY) == 0) 1083 m_freem(m); 1084 return (error); 1085 } 1086 1087 /* PF_UNIX/SOCK_DGRAM version of sbspace() */ 1088 static inline bool 1089 uipc_dgram_sbspace(struct sockbuf *sb, u_int cc, u_int mbcnt) 1090 { 1091 u_int bleft, mleft; 1092 1093 /* 1094 * Negative space may happen if send(2) is followed by 1095 * setsockopt(SO_SNDBUF/SO_RCVBUF) that shrinks maximum. 1096 */ 1097 if (__predict_false(sb->sb_hiwat < sb->uxdg_cc || 1098 sb->sb_mbmax < sb->uxdg_mbcnt)) 1099 return (false); 1100 1101 if (__predict_false(sb->sb_state & SBS_CANTRCVMORE)) 1102 return (false); 1103 1104 bleft = sb->sb_hiwat - sb->uxdg_cc; 1105 mleft = sb->sb_mbmax - sb->uxdg_mbcnt; 1106 1107 return (bleft >= cc && mleft >= mbcnt); 1108 } 1109 1110 /* 1111 * PF_UNIX/SOCK_DGRAM send 1112 * 1113 * Allocate a record consisting of 3 mbufs in the sequence of 1114 * from -> control -> data and append it to the socket buffer. 1115 * 1116 * The first mbuf carries sender's name and is a pkthdr that stores 1117 * overall length of datagram, its memory consumption and control length. 1118 */ 1119 #define ctllen PH_loc.thirtytwo[1] 1120 _Static_assert(offsetof(struct pkthdr, memlen) + sizeof(u_int) <= 1121 offsetof(struct pkthdr, ctllen), "unix/dgram can not store ctllen"); 1122 static int 1123 uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio, 1124 struct mbuf *m, struct mbuf *c, int flags, struct thread *td) 1125 { 1126 struct unpcb *unp, *unp2; 1127 const struct sockaddr *from; 1128 struct socket *so2; 1129 struct sockbuf *sb; 1130 struct mbuf *f, *clast; 1131 u_int cc, ctl, mbcnt; 1132 u_int dcc __diagused, dctl __diagused, dmbcnt __diagused; 1133 int error; 1134 1135 MPASS((uio != NULL && m == NULL) || (m != NULL && uio == NULL)); 1136 1137 error = 0; 1138 f = NULL; 1139 ctl = 0; 1140 1141 if (__predict_false(flags & MSG_OOB)) { 1142 error = EOPNOTSUPP; 1143 goto out; 1144 } 1145 if (m == NULL) { 1146 if (__predict_false(uio->uio_resid > unpdg_maxdgram)) { 1147 error = EMSGSIZE; 1148 goto out; 1149 } 1150 m = m_uiotombuf(uio, M_WAITOK, 0, max_hdr, M_PKTHDR); 1151 if (__predict_false(m == NULL)) { 1152 error = EFAULT; 1153 goto out; 1154 } 1155 f = m_gethdr(M_WAITOK, MT_SONAME); 1156 cc = m->m_pkthdr.len; 1157 mbcnt = MSIZE + m->m_pkthdr.memlen; 1158 if (c != NULL && 1159 (error = unp_internalize(&c, td, &clast, &ctl, &mbcnt))) 1160 goto out; 1161 } else { 1162 /* pr_sosend() with mbuf usually is a kernel thread. */ 1163 1164 M_ASSERTPKTHDR(m); 1165 if (__predict_false(c != NULL)) 1166 panic("%s: control from a kernel thread", __func__); 1167 1168 if (__predict_false(m->m_pkthdr.len > unpdg_maxdgram)) { 1169 error = EMSGSIZE; 1170 goto out; 1171 } 1172 if ((f = m_gethdr(M_NOWAIT, MT_SONAME)) == NULL) { 1173 error = ENOBUFS; 1174 goto out; 1175 } 1176 /* Condition the foreign mbuf to our standards. */ 1177 m_clrprotoflags(m); 1178 m_tag_delete_chain(m, NULL); 1179 m->m_pkthdr.rcvif = NULL; 1180 m->m_pkthdr.flowid = 0; 1181 m->m_pkthdr.csum_flags = 0; 1182 m->m_pkthdr.fibnum = 0; 1183 m->m_pkthdr.rsstype = 0; 1184 1185 cc = m->m_pkthdr.len; 1186 mbcnt = MSIZE; 1187 for (struct mbuf *mb = m; mb != NULL; mb = mb->m_next) { 1188 mbcnt += MSIZE; 1189 if (mb->m_flags & M_EXT) 1190 mbcnt += mb->m_ext.ext_size; 1191 } 1192 } 1193 1194 unp = sotounpcb(so); 1195 MPASS(unp); 1196 1197 /* 1198 * XXXGL: would be cool to fully remove so_snd out of the equation 1199 * and avoid this lock, which is not only extraneous, but also being 1200 * released, thus still leaving possibility for a race. We can easily 1201 * handle SBS_CANTSENDMORE/SS_ISCONNECTED complement in unpcb, but it 1202 * is more difficult to invent something to handle so_error. 1203 */ 1204 error = SOCK_IO_SEND_LOCK(so, SBLOCKWAIT(flags)); 1205 if (error) 1206 goto out2; 1207 SOCK_SENDBUF_LOCK(so); 1208 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1209 SOCK_SENDBUF_UNLOCK(so); 1210 error = EPIPE; 1211 goto out3; 1212 } 1213 if (so->so_error != 0) { 1214 error = so->so_error; 1215 so->so_error = 0; 1216 SOCK_SENDBUF_UNLOCK(so); 1217 goto out3; 1218 } 1219 if (((so->so_state & SS_ISCONNECTED) == 0) && addr == NULL) { 1220 SOCK_SENDBUF_UNLOCK(so); 1221 error = EDESTADDRREQ; 1222 goto out3; 1223 } 1224 SOCK_SENDBUF_UNLOCK(so); 1225 1226 if (addr != NULL) { 1227 if ((error = unp_connectat(AT_FDCWD, so, addr, td, true))) 1228 goto out3; 1229 UNP_PCB_LOCK_ASSERT(unp); 1230 unp2 = unp->unp_conn; 1231 UNP_PCB_LOCK_ASSERT(unp2); 1232 } else { 1233 UNP_PCB_LOCK(unp); 1234 unp2 = unp_pcb_lock_peer(unp); 1235 if (unp2 == NULL) { 1236 UNP_PCB_UNLOCK(unp); 1237 error = ENOTCONN; 1238 goto out3; 1239 } 1240 } 1241 1242 if (unp2->unp_flags & UNP_WANTCRED_MASK) 1243 c = unp_addsockcred(td, c, unp2->unp_flags, &clast, &ctl, 1244 &mbcnt); 1245 if (unp->unp_addr != NULL) 1246 from = (struct sockaddr *)unp->unp_addr; 1247 else 1248 from = &sun_noname; 1249 f->m_len = from->sa_len; 1250 MPASS(from->sa_len <= MLEN); 1251 bcopy(from, mtod(f, void *), from->sa_len); 1252 ctl += f->m_len; 1253 1254 /* 1255 * Concatenate mbufs: from -> control -> data. 1256 * Save overall cc and mbcnt in "from" mbuf. 1257 */ 1258 if (c != NULL) { 1259 #ifdef INVARIANTS 1260 struct mbuf *mc; 1261 1262 for (mc = c; mc->m_next != NULL; mc = mc->m_next); 1263 MPASS(mc == clast); 1264 #endif 1265 f->m_next = c; 1266 clast->m_next = m; 1267 c = NULL; 1268 } else 1269 f->m_next = m; 1270 m = NULL; 1271 #ifdef INVARIANTS 1272 dcc = dctl = dmbcnt = 0; 1273 for (struct mbuf *mb = f; mb != NULL; mb = mb->m_next) { 1274 if (mb->m_type == MT_DATA) 1275 dcc += mb->m_len; 1276 else 1277 dctl += mb->m_len; 1278 dmbcnt += MSIZE; 1279 if (mb->m_flags & M_EXT) 1280 dmbcnt += mb->m_ext.ext_size; 1281 } 1282 MPASS(dcc == cc); 1283 MPASS(dctl == ctl); 1284 MPASS(dmbcnt == mbcnt); 1285 #endif 1286 f->m_pkthdr.len = cc + ctl; 1287 f->m_pkthdr.memlen = mbcnt; 1288 f->m_pkthdr.ctllen = ctl; 1289 1290 /* 1291 * Destination socket buffer selection. 1292 * 1293 * Unconnected sends, when !(so->so_state & SS_ISCONNECTED) and the 1294 * destination address is supplied, create a temporary connection for 1295 * the run time of the function (see call to unp_connectat() above and 1296 * to unp_disconnect() below). We distinguish them by condition of 1297 * (addr != NULL). We intentionally avoid adding 'bool connected' for 1298 * that condition, since, again, through the run time of this code we 1299 * are always connected. For such "unconnected" sends, the destination 1300 * buffer would be the receive buffer of destination socket so2. 1301 * 1302 * For connected sends, data lands on the send buffer of the sender's 1303 * socket "so". Then, if we just added the very first datagram 1304 * on this send buffer, we need to add the send buffer on to the 1305 * receiving socket's buffer list. We put ourselves on top of the 1306 * list. Such logic gives infrequent senders priority over frequent 1307 * senders. 1308 * 1309 * Note on byte count management. As long as event methods kevent(2), 1310 * select(2) are not protocol specific (yet), we need to maintain 1311 * meaningful values on the receive buffer. So, the receive buffer 1312 * would accumulate counters from all connected buffers potentially 1313 * having sb_ccc > sb_hiwat or sb_mbcnt > sb_mbmax. 1314 */ 1315 so2 = unp2->unp_socket; 1316 sb = (addr == NULL) ? &so->so_snd : &so2->so_rcv; 1317 SOCK_RECVBUF_LOCK(so2); 1318 if (uipc_dgram_sbspace(sb, cc + ctl, mbcnt)) { 1319 if (addr == NULL && STAILQ_EMPTY(&sb->uxdg_mb)) 1320 TAILQ_INSERT_HEAD(&so2->so_rcv.uxdg_conns, &so->so_snd, 1321 uxdg_clist); 1322 STAILQ_INSERT_TAIL(&sb->uxdg_mb, f, m_stailqpkt); 1323 sb->uxdg_cc += cc + ctl; 1324 sb->uxdg_ctl += ctl; 1325 sb->uxdg_mbcnt += mbcnt; 1326 so2->so_rcv.sb_acc += cc + ctl; 1327 so2->so_rcv.sb_ccc += cc + ctl; 1328 so2->so_rcv.sb_ctl += ctl; 1329 so2->so_rcv.sb_mbcnt += mbcnt; 1330 sorwakeup_locked(so2); 1331 f = NULL; 1332 } else { 1333 soroverflow_locked(so2); 1334 error = ENOBUFS; 1335 if (f->m_next->m_type == MT_CONTROL) { 1336 c = f->m_next; 1337 f->m_next = NULL; 1338 } 1339 } 1340 1341 if (addr != NULL) 1342 unp_disconnect(unp, unp2); 1343 else 1344 unp_pcb_unlock_pair(unp, unp2); 1345 1346 td->td_ru.ru_msgsnd++; 1347 1348 out3: 1349 SOCK_IO_SEND_UNLOCK(so); 1350 out2: 1351 if (c) 1352 unp_scan(c, unp_freerights); 1353 out: 1354 if (f) 1355 m_freem(f); 1356 if (c) 1357 m_freem(c); 1358 if (m) 1359 m_freem(m); 1360 1361 return (error); 1362 } 1363 1364 /* 1365 * PF_UNIX/SOCK_DGRAM receive with MSG_PEEK. 1366 * The mbuf has already been unlinked from the uxdg_mb of socket buffer 1367 * and needs to be linked onto uxdg_peeked of receive socket buffer. 1368 */ 1369 static int 1370 uipc_peek_dgram(struct socket *so, struct mbuf *m, struct sockaddr **psa, 1371 struct uio *uio, struct mbuf **controlp, int *flagsp) 1372 { 1373 ssize_t len = 0; 1374 int error; 1375 1376 so->so_rcv.uxdg_peeked = m; 1377 so->so_rcv.uxdg_cc += m->m_pkthdr.len; 1378 so->so_rcv.uxdg_ctl += m->m_pkthdr.ctllen; 1379 so->so_rcv.uxdg_mbcnt += m->m_pkthdr.memlen; 1380 SOCK_RECVBUF_UNLOCK(so); 1381 1382 KASSERT(m->m_type == MT_SONAME, ("m->m_type == %d", m->m_type)); 1383 if (psa != NULL) 1384 *psa = sodupsockaddr(mtod(m, struct sockaddr *), M_WAITOK); 1385 1386 m = m->m_next; 1387 KASSERT(m, ("%s: no data or control after soname", __func__)); 1388 1389 /* 1390 * With MSG_PEEK the control isn't executed, just copied. 1391 */ 1392 while (m != NULL && m->m_type == MT_CONTROL) { 1393 if (controlp != NULL) { 1394 *controlp = m_copym(m, 0, m->m_len, M_WAITOK); 1395 controlp = &(*controlp)->m_next; 1396 } 1397 m = m->m_next; 1398 } 1399 KASSERT(m == NULL || m->m_type == MT_DATA, 1400 ("%s: not MT_DATA mbuf %p", __func__, m)); 1401 while (m != NULL && uio->uio_resid > 0) { 1402 len = uio->uio_resid; 1403 if (len > m->m_len) 1404 len = m->m_len; 1405 error = uiomove(mtod(m, char *), (int)len, uio); 1406 if (error) { 1407 SOCK_IO_RECV_UNLOCK(so); 1408 return (error); 1409 } 1410 if (len == m->m_len) 1411 m = m->m_next; 1412 } 1413 SOCK_IO_RECV_UNLOCK(so); 1414 1415 if (flagsp != NULL) { 1416 if (m != NULL) { 1417 if (*flagsp & MSG_TRUNC) { 1418 /* Report real length of the packet */ 1419 uio->uio_resid -= m_length(m, NULL) - len; 1420 } 1421 *flagsp |= MSG_TRUNC; 1422 } else 1423 *flagsp &= ~MSG_TRUNC; 1424 } 1425 1426 return (0); 1427 } 1428 1429 /* 1430 * PF_UNIX/SOCK_DGRAM receive 1431 */ 1432 static int 1433 uipc_soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio, 1434 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 1435 { 1436 struct sockbuf *sb = NULL; 1437 struct mbuf *m; 1438 int flags, error; 1439 ssize_t len = 0; 1440 bool nonblock; 1441 1442 MPASS(mp0 == NULL); 1443 1444 if (psa != NULL) 1445 *psa = NULL; 1446 if (controlp != NULL) 1447 *controlp = NULL; 1448 1449 flags = flagsp != NULL ? *flagsp : 0; 1450 nonblock = (so->so_state & SS_NBIO) || 1451 (flags & (MSG_DONTWAIT | MSG_NBIO)); 1452 1453 error = SOCK_IO_RECV_LOCK(so, SBLOCKWAIT(flags)); 1454 if (__predict_false(error)) 1455 return (error); 1456 1457 /* 1458 * Loop blocking while waiting for a datagram. Prioritize connected 1459 * peers over unconnected sends. Set sb to selected socket buffer 1460 * containing an mbuf on exit from the wait loop. A datagram that 1461 * had already been peeked at has top priority. 1462 */ 1463 SOCK_RECVBUF_LOCK(so); 1464 while ((m = so->so_rcv.uxdg_peeked) == NULL && 1465 (sb = TAILQ_FIRST(&so->so_rcv.uxdg_conns)) == NULL && 1466 (m = STAILQ_FIRST(&so->so_rcv.uxdg_mb)) == NULL) { 1467 if (so->so_error) { 1468 error = so->so_error; 1469 so->so_error = 0; 1470 SOCK_RECVBUF_UNLOCK(so); 1471 SOCK_IO_RECV_UNLOCK(so); 1472 return (error); 1473 } 1474 if (so->so_rcv.sb_state & SBS_CANTRCVMORE || 1475 uio->uio_resid == 0) { 1476 SOCK_RECVBUF_UNLOCK(so); 1477 SOCK_IO_RECV_UNLOCK(so); 1478 return (0); 1479 } 1480 if (nonblock) { 1481 SOCK_RECVBUF_UNLOCK(so); 1482 SOCK_IO_RECV_UNLOCK(so); 1483 return (EWOULDBLOCK); 1484 } 1485 error = sbwait(so, SO_RCV); 1486 if (error) { 1487 SOCK_RECVBUF_UNLOCK(so); 1488 SOCK_IO_RECV_UNLOCK(so); 1489 return (error); 1490 } 1491 } 1492 1493 if (sb == NULL) 1494 sb = &so->so_rcv; 1495 else if (m == NULL) 1496 m = STAILQ_FIRST(&sb->uxdg_mb); 1497 else 1498 MPASS(m == so->so_rcv.uxdg_peeked); 1499 1500 MPASS(sb->uxdg_cc > 0); 1501 M_ASSERTPKTHDR(m); 1502 KASSERT(m->m_type == MT_SONAME, ("m->m_type == %d", m->m_type)); 1503 1504 if (uio->uio_td) 1505 uio->uio_td->td_ru.ru_msgrcv++; 1506 1507 if (__predict_true(m != so->so_rcv.uxdg_peeked)) { 1508 STAILQ_REMOVE_HEAD(&sb->uxdg_mb, m_stailqpkt); 1509 if (STAILQ_EMPTY(&sb->uxdg_mb) && sb != &so->so_rcv) 1510 TAILQ_REMOVE(&so->so_rcv.uxdg_conns, sb, uxdg_clist); 1511 } else 1512 so->so_rcv.uxdg_peeked = NULL; 1513 1514 sb->uxdg_cc -= m->m_pkthdr.len; 1515 sb->uxdg_ctl -= m->m_pkthdr.ctllen; 1516 sb->uxdg_mbcnt -= m->m_pkthdr.memlen; 1517 1518 if (__predict_false(flags & MSG_PEEK)) 1519 return (uipc_peek_dgram(so, m, psa, uio, controlp, flagsp)); 1520 1521 so->so_rcv.sb_acc -= m->m_pkthdr.len; 1522 so->so_rcv.sb_ccc -= m->m_pkthdr.len; 1523 so->so_rcv.sb_ctl -= m->m_pkthdr.ctllen; 1524 so->so_rcv.sb_mbcnt -= m->m_pkthdr.memlen; 1525 SOCK_RECVBUF_UNLOCK(so); 1526 1527 if (psa != NULL) 1528 *psa = sodupsockaddr(mtod(m, struct sockaddr *), M_WAITOK); 1529 m = m_free(m); 1530 KASSERT(m, ("%s: no data or control after soname", __func__)); 1531 1532 /* 1533 * Packet to copyout() is now in 'm' and it is disconnected from the 1534 * queue. 1535 * 1536 * Process one or more MT_CONTROL mbufs present before any data mbufs 1537 * in the first mbuf chain on the socket buffer. We call into the 1538 * unp_externalize() to perform externalization (or freeing if 1539 * controlp == NULL). In some cases there can be only MT_CONTROL mbufs 1540 * without MT_DATA mbufs. 1541 */ 1542 while (m != NULL && m->m_type == MT_CONTROL) { 1543 struct mbuf *cm; 1544 1545 /* XXXGL: unp_externalize() is also dom_externalize() KBI and 1546 * it frees whole chain, so we must disconnect the mbuf. 1547 */ 1548 cm = m; m = m->m_next; cm->m_next = NULL; 1549 error = unp_externalize(cm, controlp, flags); 1550 if (error != 0) { 1551 SOCK_IO_RECV_UNLOCK(so); 1552 unp_scan(m, unp_freerights); 1553 m_freem(m); 1554 return (error); 1555 } 1556 if (controlp != NULL) { 1557 while (*controlp != NULL) 1558 controlp = &(*controlp)->m_next; 1559 } 1560 } 1561 KASSERT(m == NULL || m->m_type == MT_DATA, 1562 ("%s: not MT_DATA mbuf %p", __func__, m)); 1563 while (m != NULL && uio->uio_resid > 0) { 1564 len = uio->uio_resid; 1565 if (len > m->m_len) 1566 len = m->m_len; 1567 error = uiomove(mtod(m, char *), (int)len, uio); 1568 if (error) { 1569 SOCK_IO_RECV_UNLOCK(so); 1570 m_freem(m); 1571 return (error); 1572 } 1573 if (len == m->m_len) 1574 m = m_free(m); 1575 else { 1576 m->m_data += len; 1577 m->m_len -= len; 1578 } 1579 } 1580 SOCK_IO_RECV_UNLOCK(so); 1581 1582 if (m != NULL) { 1583 if (flagsp != NULL) { 1584 if (flags & MSG_TRUNC) { 1585 /* Report real length of the packet */ 1586 uio->uio_resid -= m_length(m, NULL); 1587 } 1588 *flagsp |= MSG_TRUNC; 1589 } 1590 m_freem(m); 1591 } else if (flagsp != NULL) 1592 *flagsp &= ~MSG_TRUNC; 1593 1594 return (0); 1595 } 1596 1597 static bool 1598 uipc_ready_scan(struct socket *so, struct mbuf *m, int count, int *errorp) 1599 { 1600 struct mbuf *mb, *n; 1601 struct sockbuf *sb; 1602 1603 SOCK_LOCK(so); 1604 if (SOLISTENING(so)) { 1605 SOCK_UNLOCK(so); 1606 return (false); 1607 } 1608 mb = NULL; 1609 sb = &so->so_rcv; 1610 SOCKBUF_LOCK(sb); 1611 if (sb->sb_fnrdy != NULL) { 1612 for (mb = sb->sb_mb, n = mb->m_nextpkt; mb != NULL;) { 1613 if (mb == m) { 1614 *errorp = sbready(sb, m, count); 1615 break; 1616 } 1617 mb = mb->m_next; 1618 if (mb == NULL) { 1619 mb = n; 1620 if (mb != NULL) 1621 n = mb->m_nextpkt; 1622 } 1623 } 1624 } 1625 SOCKBUF_UNLOCK(sb); 1626 SOCK_UNLOCK(so); 1627 return (mb != NULL); 1628 } 1629 1630 static int 1631 uipc_ready(struct socket *so, struct mbuf *m, int count) 1632 { 1633 struct unpcb *unp, *unp2; 1634 struct socket *so2; 1635 int error, i; 1636 1637 unp = sotounpcb(so); 1638 1639 KASSERT(so->so_type == SOCK_STREAM, 1640 ("%s: unexpected socket type for %p", __func__, so)); 1641 1642 UNP_PCB_LOCK(unp); 1643 if ((unp2 = unp_pcb_lock_peer(unp)) != NULL) { 1644 UNP_PCB_UNLOCK(unp); 1645 so2 = unp2->unp_socket; 1646 SOCKBUF_LOCK(&so2->so_rcv); 1647 if ((error = sbready(&so2->so_rcv, m, count)) == 0) 1648 sorwakeup_locked(so2); 1649 else 1650 SOCKBUF_UNLOCK(&so2->so_rcv); 1651 UNP_PCB_UNLOCK(unp2); 1652 return (error); 1653 } 1654 UNP_PCB_UNLOCK(unp); 1655 1656 /* 1657 * The receiving socket has been disconnected, but may still be valid. 1658 * In this case, the now-ready mbufs are still present in its socket 1659 * buffer, so perform an exhaustive search before giving up and freeing 1660 * the mbufs. 1661 */ 1662 UNP_LINK_RLOCK(); 1663 LIST_FOREACH(unp, &unp_shead, unp_link) { 1664 if (uipc_ready_scan(unp->unp_socket, m, count, &error)) 1665 break; 1666 } 1667 UNP_LINK_RUNLOCK(); 1668 1669 if (unp == NULL) { 1670 for (i = 0; i < count; i++) 1671 m = m_free(m); 1672 error = ECONNRESET; 1673 } 1674 return (error); 1675 } 1676 1677 static int 1678 uipc_sense(struct socket *so, struct stat *sb) 1679 { 1680 struct unpcb *unp; 1681 1682 unp = sotounpcb(so); 1683 KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 1684 1685 sb->st_blksize = so->so_snd.sb_hiwat; 1686 sb->st_dev = NODEV; 1687 sb->st_ino = unp->unp_ino; 1688 return (0); 1689 } 1690 1691 static int 1692 uipc_shutdown(struct socket *so) 1693 { 1694 struct unpcb *unp; 1695 1696 unp = sotounpcb(so); 1697 KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 1698 1699 UNP_PCB_LOCK(unp); 1700 socantsendmore(so); 1701 unp_shutdown(unp); 1702 UNP_PCB_UNLOCK(unp); 1703 return (0); 1704 } 1705 1706 static int 1707 uipc_sockaddr(struct socket *so, struct sockaddr **nam) 1708 { 1709 struct unpcb *unp; 1710 const struct sockaddr *sa; 1711 1712 unp = sotounpcb(so); 1713 KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 1714 1715 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1716 UNP_PCB_LOCK(unp); 1717 if (unp->unp_addr != NULL) 1718 sa = (struct sockaddr *) unp->unp_addr; 1719 else 1720 sa = &sun_noname; 1721 bcopy(sa, *nam, sa->sa_len); 1722 UNP_PCB_UNLOCK(unp); 1723 return (0); 1724 } 1725 1726 static int 1727 uipc_ctloutput(struct socket *so, struct sockopt *sopt) 1728 { 1729 struct unpcb *unp; 1730 struct xucred xu; 1731 int error, optval; 1732 1733 if (sopt->sopt_level != SOL_LOCAL) 1734 return (EINVAL); 1735 1736 unp = sotounpcb(so); 1737 KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 1738 error = 0; 1739 switch (sopt->sopt_dir) { 1740 case SOPT_GET: 1741 switch (sopt->sopt_name) { 1742 case LOCAL_PEERCRED: 1743 UNP_PCB_LOCK(unp); 1744 if (unp->unp_flags & UNP_HAVEPC) 1745 xu = unp->unp_peercred; 1746 else { 1747 if (so->so_type == SOCK_STREAM) 1748 error = ENOTCONN; 1749 else 1750 error = EINVAL; 1751 } 1752 UNP_PCB_UNLOCK(unp); 1753 if (error == 0) 1754 error = sooptcopyout(sopt, &xu, sizeof(xu)); 1755 break; 1756 1757 case LOCAL_CREDS: 1758 /* Unlocked read. */ 1759 optval = unp->unp_flags & UNP_WANTCRED_ONESHOT ? 1 : 0; 1760 error = sooptcopyout(sopt, &optval, sizeof(optval)); 1761 break; 1762 1763 case LOCAL_CREDS_PERSISTENT: 1764 /* Unlocked read. */ 1765 optval = unp->unp_flags & UNP_WANTCRED_ALWAYS ? 1 : 0; 1766 error = sooptcopyout(sopt, &optval, sizeof(optval)); 1767 break; 1768 1769 case LOCAL_CONNWAIT: 1770 /* Unlocked read. */ 1771 optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 1772 error = sooptcopyout(sopt, &optval, sizeof(optval)); 1773 break; 1774 1775 default: 1776 error = EOPNOTSUPP; 1777 break; 1778 } 1779 break; 1780 1781 case SOPT_SET: 1782 switch (sopt->sopt_name) { 1783 case LOCAL_CREDS: 1784 case LOCAL_CREDS_PERSISTENT: 1785 case LOCAL_CONNWAIT: 1786 error = sooptcopyin(sopt, &optval, sizeof(optval), 1787 sizeof(optval)); 1788 if (error) 1789 break; 1790 1791 #define OPTSET(bit, exclusive) do { \ 1792 UNP_PCB_LOCK(unp); \ 1793 if (optval) { \ 1794 if ((unp->unp_flags & (exclusive)) != 0) { \ 1795 UNP_PCB_UNLOCK(unp); \ 1796 error = EINVAL; \ 1797 break; \ 1798 } \ 1799 unp->unp_flags |= (bit); \ 1800 } else \ 1801 unp->unp_flags &= ~(bit); \ 1802 UNP_PCB_UNLOCK(unp); \ 1803 } while (0) 1804 1805 switch (sopt->sopt_name) { 1806 case LOCAL_CREDS: 1807 OPTSET(UNP_WANTCRED_ONESHOT, UNP_WANTCRED_ALWAYS); 1808 break; 1809 1810 case LOCAL_CREDS_PERSISTENT: 1811 OPTSET(UNP_WANTCRED_ALWAYS, UNP_WANTCRED_ONESHOT); 1812 break; 1813 1814 case LOCAL_CONNWAIT: 1815 OPTSET(UNP_CONNWAIT, 0); 1816 break; 1817 1818 default: 1819 break; 1820 } 1821 break; 1822 #undef OPTSET 1823 default: 1824 error = ENOPROTOOPT; 1825 break; 1826 } 1827 break; 1828 1829 default: 1830 error = EOPNOTSUPP; 1831 break; 1832 } 1833 return (error); 1834 } 1835 1836 static int 1837 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1838 { 1839 1840 return (unp_connectat(AT_FDCWD, so, nam, td, false)); 1841 } 1842 1843 static int 1844 unp_connectat(int fd, struct socket *so, struct sockaddr *nam, 1845 struct thread *td, bool return_locked) 1846 { 1847 struct mtx *vplock; 1848 struct sockaddr_un *soun; 1849 struct vnode *vp; 1850 struct socket *so2; 1851 struct unpcb *unp, *unp2, *unp3; 1852 struct nameidata nd; 1853 char buf[SOCK_MAXADDRLEN]; 1854 struct sockaddr *sa; 1855 cap_rights_t rights; 1856 int error, len; 1857 bool connreq; 1858 1859 if (nam->sa_family != AF_UNIX) 1860 return (EAFNOSUPPORT); 1861 if (nam->sa_len > sizeof(struct sockaddr_un)) 1862 return (EINVAL); 1863 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 1864 if (len <= 0) 1865 return (EINVAL); 1866 soun = (struct sockaddr_un *)nam; 1867 bcopy(soun->sun_path, buf, len); 1868 buf[len] = 0; 1869 1870 error = 0; 1871 unp = sotounpcb(so); 1872 UNP_PCB_LOCK(unp); 1873 for (;;) { 1874 /* 1875 * Wait for connection state to stabilize. If a connection 1876 * already exists, give up. For datagram sockets, which permit 1877 * multiple consecutive connect(2) calls, upper layers are 1878 * responsible for disconnecting in advance of a subsequent 1879 * connect(2), but this is not synchronized with PCB connection 1880 * state. 1881 * 1882 * Also make sure that no threads are currently attempting to 1883 * lock the peer socket, to ensure that unp_conn cannot 1884 * transition between two valid sockets while locks are dropped. 1885 */ 1886 if (SOLISTENING(so)) 1887 error = EOPNOTSUPP; 1888 else if (unp->unp_conn != NULL) 1889 error = EISCONN; 1890 else if ((unp->unp_flags & UNP_CONNECTING) != 0) { 1891 error = EALREADY; 1892 } 1893 if (error != 0) { 1894 UNP_PCB_UNLOCK(unp); 1895 return (error); 1896 } 1897 if (unp->unp_pairbusy > 0) { 1898 unp->unp_flags |= UNP_WAITING; 1899 mtx_sleep(unp, UNP_PCB_LOCKPTR(unp), 0, "unpeer", 0); 1900 continue; 1901 } 1902 break; 1903 } 1904 unp->unp_flags |= UNP_CONNECTING; 1905 UNP_PCB_UNLOCK(unp); 1906 1907 connreq = (so->so_proto->pr_flags & PR_CONNREQUIRED) != 0; 1908 if (connreq) 1909 sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1910 else 1911 sa = NULL; 1912 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 1913 UIO_SYSSPACE, buf, fd, cap_rights_init_one(&rights, CAP_CONNECTAT)); 1914 error = namei(&nd); 1915 if (error) 1916 vp = NULL; 1917 else 1918 vp = nd.ni_vp; 1919 ASSERT_VOP_LOCKED(vp, "unp_connect"); 1920 if (error) 1921 goto bad; 1922 NDFREE_PNBUF(&nd); 1923 1924 if (vp->v_type != VSOCK) { 1925 error = ENOTSOCK; 1926 goto bad; 1927 } 1928 #ifdef MAC 1929 error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD); 1930 if (error) 1931 goto bad; 1932 #endif 1933 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1934 if (error) 1935 goto bad; 1936 1937 unp = sotounpcb(so); 1938 KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1939 1940 vplock = mtx_pool_find(mtxpool_sleep, vp); 1941 mtx_lock(vplock); 1942 VOP_UNP_CONNECT(vp, &unp2); 1943 if (unp2 == NULL) { 1944 error = ECONNREFUSED; 1945 goto bad2; 1946 } 1947 so2 = unp2->unp_socket; 1948 if (so->so_type != so2->so_type) { 1949 error = EPROTOTYPE; 1950 goto bad2; 1951 } 1952 if (connreq) { 1953 if (SOLISTENING(so2)) { 1954 CURVNET_SET(so2->so_vnet); 1955 so2 = sonewconn(so2, 0); 1956 CURVNET_RESTORE(); 1957 } else 1958 so2 = NULL; 1959 if (so2 == NULL) { 1960 error = ECONNREFUSED; 1961 goto bad2; 1962 } 1963 unp3 = sotounpcb(so2); 1964 unp_pcb_lock_pair(unp2, unp3); 1965 if (unp2->unp_addr != NULL) { 1966 bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 1967 unp3->unp_addr = (struct sockaddr_un *) sa; 1968 sa = NULL; 1969 } 1970 1971 unp_copy_peercred(td, unp3, unp, unp2); 1972 1973 UNP_PCB_UNLOCK(unp2); 1974 unp2 = unp3; 1975 1976 /* 1977 * It is safe to block on the PCB lock here since unp2 is 1978 * nascent and cannot be connected to any other sockets. 1979 */ 1980 UNP_PCB_LOCK(unp); 1981 #ifdef MAC 1982 mac_socketpeer_set_from_socket(so, so2); 1983 mac_socketpeer_set_from_socket(so2, so); 1984 #endif 1985 } else { 1986 unp_pcb_lock_pair(unp, unp2); 1987 } 1988 KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 && 1989 sotounpcb(so2) == unp2, 1990 ("%s: unp2 %p so2 %p", __func__, unp2, so2)); 1991 unp_connect2(so, so2, PRU_CONNECT); 1992 KASSERT((unp->unp_flags & UNP_CONNECTING) != 0, 1993 ("%s: unp %p has UNP_CONNECTING clear", __func__, unp)); 1994 unp->unp_flags &= ~UNP_CONNECTING; 1995 if (!return_locked) 1996 unp_pcb_unlock_pair(unp, unp2); 1997 bad2: 1998 mtx_unlock(vplock); 1999 bad: 2000 if (vp != NULL) { 2001 /* 2002 * If we are returning locked (called via uipc_sosend_dgram()), 2003 * we need to be sure that vput() won't sleep. This is 2004 * guaranteed by VOP_UNP_CONNECT() call above and unp2 lock. 2005 * SOCK_STREAM/SEQPACKET can't request return_locked (yet). 2006 */ 2007 MPASS(!(return_locked && connreq)); 2008 vput(vp); 2009 } 2010 free(sa, M_SONAME); 2011 if (__predict_false(error)) { 2012 UNP_PCB_LOCK(unp); 2013 KASSERT((unp->unp_flags & UNP_CONNECTING) != 0, 2014 ("%s: unp %p has UNP_CONNECTING clear", __func__, unp)); 2015 unp->unp_flags &= ~UNP_CONNECTING; 2016 UNP_PCB_UNLOCK(unp); 2017 } 2018 return (error); 2019 } 2020 2021 /* 2022 * Set socket peer credentials at connection time. 2023 * 2024 * The client's PCB credentials are copied from its process structure. The 2025 * server's PCB credentials are copied from the socket on which it called 2026 * listen(2). uipc_listen cached that process's credentials at the time. 2027 */ 2028 void 2029 unp_copy_peercred(struct thread *td, struct unpcb *client_unp, 2030 struct unpcb *server_unp, struct unpcb *listen_unp) 2031 { 2032 cru2xt(td, &client_unp->unp_peercred); 2033 client_unp->unp_flags |= UNP_HAVEPC; 2034 2035 memcpy(&server_unp->unp_peercred, &listen_unp->unp_peercred, 2036 sizeof(server_unp->unp_peercred)); 2037 server_unp->unp_flags |= UNP_HAVEPC; 2038 client_unp->unp_flags |= (listen_unp->unp_flags & UNP_WANTCRED_MASK); 2039 } 2040 2041 static void 2042 unp_connect2(struct socket *so, struct socket *so2, conn2_how req) 2043 { 2044 struct unpcb *unp; 2045 struct unpcb *unp2; 2046 2047 MPASS(so2->so_type == so->so_type); 2048 unp = sotounpcb(so); 2049 KASSERT(unp != NULL, ("unp_connect2: unp == NULL")); 2050 unp2 = sotounpcb(so2); 2051 KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 2052 2053 UNP_PCB_LOCK_ASSERT(unp); 2054 UNP_PCB_LOCK_ASSERT(unp2); 2055 KASSERT(unp->unp_conn == NULL, 2056 ("%s: socket %p is already connected", __func__, unp)); 2057 2058 unp->unp_conn = unp2; 2059 unp_pcb_hold(unp2); 2060 unp_pcb_hold(unp); 2061 switch (so->so_type) { 2062 case SOCK_DGRAM: 2063 UNP_REF_LIST_LOCK(); 2064 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 2065 UNP_REF_LIST_UNLOCK(); 2066 soisconnected(so); 2067 break; 2068 2069 case SOCK_STREAM: 2070 case SOCK_SEQPACKET: 2071 KASSERT(unp2->unp_conn == NULL, 2072 ("%s: socket %p is already connected", __func__, unp2)); 2073 unp2->unp_conn = unp; 2074 if (req == PRU_CONNECT && 2075 ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 2076 soisconnecting(so); 2077 else 2078 soisconnected(so); 2079 soisconnected(so2); 2080 break; 2081 2082 default: 2083 panic("unp_connect2"); 2084 } 2085 } 2086 2087 static void 2088 unp_disconnect(struct unpcb *unp, struct unpcb *unp2) 2089 { 2090 struct socket *so, *so2; 2091 struct mbuf *m = NULL; 2092 #ifdef INVARIANTS 2093 struct unpcb *unptmp; 2094 #endif 2095 2096 UNP_PCB_LOCK_ASSERT(unp); 2097 UNP_PCB_LOCK_ASSERT(unp2); 2098 KASSERT(unp->unp_conn == unp2, 2099 ("%s: unpcb %p is not connected to %p", __func__, unp, unp2)); 2100 2101 unp->unp_conn = NULL; 2102 so = unp->unp_socket; 2103 so2 = unp2->unp_socket; 2104 switch (unp->unp_socket->so_type) { 2105 case SOCK_DGRAM: 2106 /* 2107 * Remove our send socket buffer from the peer's receive buffer. 2108 * Move the data to the receive buffer only if it is empty. 2109 * This is a protection against a scenario where a peer 2110 * connects, floods and disconnects, effectively blocking 2111 * sendto() from unconnected sockets. 2112 */ 2113 SOCK_RECVBUF_LOCK(so2); 2114 if (!STAILQ_EMPTY(&so->so_snd.uxdg_mb)) { 2115 TAILQ_REMOVE(&so2->so_rcv.uxdg_conns, &so->so_snd, 2116 uxdg_clist); 2117 if (__predict_true((so2->so_rcv.sb_state & 2118 SBS_CANTRCVMORE) == 0) && 2119 STAILQ_EMPTY(&so2->so_rcv.uxdg_mb)) { 2120 STAILQ_CONCAT(&so2->so_rcv.uxdg_mb, 2121 &so->so_snd.uxdg_mb); 2122 so2->so_rcv.uxdg_cc += so->so_snd.uxdg_cc; 2123 so2->so_rcv.uxdg_ctl += so->so_snd.uxdg_ctl; 2124 so2->so_rcv.uxdg_mbcnt += so->so_snd.uxdg_mbcnt; 2125 } else { 2126 m = STAILQ_FIRST(&so->so_snd.uxdg_mb); 2127 STAILQ_INIT(&so->so_snd.uxdg_mb); 2128 so2->so_rcv.sb_acc -= so->so_snd.uxdg_cc; 2129 so2->so_rcv.sb_ccc -= so->so_snd.uxdg_cc; 2130 so2->so_rcv.sb_ctl -= so->so_snd.uxdg_ctl; 2131 so2->so_rcv.sb_mbcnt -= so->so_snd.uxdg_mbcnt; 2132 } 2133 /* Note: so may reconnect. */ 2134 so->so_snd.uxdg_cc = 0; 2135 so->so_snd.uxdg_ctl = 0; 2136 so->so_snd.uxdg_mbcnt = 0; 2137 } 2138 SOCK_RECVBUF_UNLOCK(so2); 2139 UNP_REF_LIST_LOCK(); 2140 #ifdef INVARIANTS 2141 LIST_FOREACH(unptmp, &unp2->unp_refs, unp_reflink) { 2142 if (unptmp == unp) 2143 break; 2144 } 2145 KASSERT(unptmp != NULL, 2146 ("%s: %p not found in reflist of %p", __func__, unp, unp2)); 2147 #endif 2148 LIST_REMOVE(unp, unp_reflink); 2149 UNP_REF_LIST_UNLOCK(); 2150 if (so) { 2151 SOCK_LOCK(so); 2152 so->so_state &= ~SS_ISCONNECTED; 2153 SOCK_UNLOCK(so); 2154 } 2155 break; 2156 2157 case SOCK_STREAM: 2158 case SOCK_SEQPACKET: 2159 if (so) 2160 soisdisconnected(so); 2161 MPASS(unp2->unp_conn == unp); 2162 unp2->unp_conn = NULL; 2163 if (so2) 2164 soisdisconnected(so2); 2165 break; 2166 } 2167 2168 if (unp == unp2) { 2169 unp_pcb_rele_notlast(unp); 2170 if (!unp_pcb_rele(unp)) 2171 UNP_PCB_UNLOCK(unp); 2172 } else { 2173 if (!unp_pcb_rele(unp)) 2174 UNP_PCB_UNLOCK(unp); 2175 if (!unp_pcb_rele(unp2)) 2176 UNP_PCB_UNLOCK(unp2); 2177 } 2178 2179 if (m != NULL) { 2180 unp_scan(m, unp_freerights); 2181 m_freem(m); 2182 } 2183 } 2184 2185 /* 2186 * unp_pcblist() walks the global list of struct unpcb's to generate a 2187 * pointer list, bumping the refcount on each unpcb. It then copies them out 2188 * sequentially, validating the generation number on each to see if it has 2189 * been detached. All of this is necessary because copyout() may sleep on 2190 * disk I/O. 2191 */ 2192 static int 2193 unp_pcblist(SYSCTL_HANDLER_ARGS) 2194 { 2195 struct unpcb *unp, **unp_list; 2196 unp_gen_t gencnt; 2197 struct xunpgen *xug; 2198 struct unp_head *head; 2199 struct xunpcb *xu; 2200 u_int i; 2201 int error, n; 2202 2203 switch ((intptr_t)arg1) { 2204 case SOCK_STREAM: 2205 head = &unp_shead; 2206 break; 2207 2208 case SOCK_DGRAM: 2209 head = &unp_dhead; 2210 break; 2211 2212 case SOCK_SEQPACKET: 2213 head = &unp_sphead; 2214 break; 2215 2216 default: 2217 panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1); 2218 } 2219 2220 /* 2221 * The process of preparing the PCB list is too time-consuming and 2222 * resource-intensive to repeat twice on every request. 2223 */ 2224 if (req->oldptr == NULL) { 2225 n = unp_count; 2226 req->oldidx = 2 * (sizeof *xug) 2227 + (n + n/8) * sizeof(struct xunpcb); 2228 return (0); 2229 } 2230 2231 if (req->newptr != NULL) 2232 return (EPERM); 2233 2234 /* 2235 * OK, now we're committed to doing something. 2236 */ 2237 xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK | M_ZERO); 2238 UNP_LINK_RLOCK(); 2239 gencnt = unp_gencnt; 2240 n = unp_count; 2241 UNP_LINK_RUNLOCK(); 2242 2243 xug->xug_len = sizeof *xug; 2244 xug->xug_count = n; 2245 xug->xug_gen = gencnt; 2246 xug->xug_sogen = so_gencnt; 2247 error = SYSCTL_OUT(req, xug, sizeof *xug); 2248 if (error) { 2249 free(xug, M_TEMP); 2250 return (error); 2251 } 2252 2253 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 2254 2255 UNP_LINK_RLOCK(); 2256 for (unp = LIST_FIRST(head), i = 0; unp && i < n; 2257 unp = LIST_NEXT(unp, unp_link)) { 2258 UNP_PCB_LOCK(unp); 2259 if (unp->unp_gencnt <= gencnt) { 2260 if (cr_cansee(req->td->td_ucred, 2261 unp->unp_socket->so_cred)) { 2262 UNP_PCB_UNLOCK(unp); 2263 continue; 2264 } 2265 unp_list[i++] = unp; 2266 unp_pcb_hold(unp); 2267 } 2268 UNP_PCB_UNLOCK(unp); 2269 } 2270 UNP_LINK_RUNLOCK(); 2271 n = i; /* In case we lost some during malloc. */ 2272 2273 error = 0; 2274 xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 2275 for (i = 0; i < n; i++) { 2276 unp = unp_list[i]; 2277 UNP_PCB_LOCK(unp); 2278 if (unp_pcb_rele(unp)) 2279 continue; 2280 2281 if (unp->unp_gencnt <= gencnt) { 2282 xu->xu_len = sizeof *xu; 2283 xu->xu_unpp = (uintptr_t)unp; 2284 /* 2285 * XXX - need more locking here to protect against 2286 * connect/disconnect races for SMP. 2287 */ 2288 if (unp->unp_addr != NULL) 2289 bcopy(unp->unp_addr, &xu->xu_addr, 2290 unp->unp_addr->sun_len); 2291 else 2292 bzero(&xu->xu_addr, sizeof(xu->xu_addr)); 2293 if (unp->unp_conn != NULL && 2294 unp->unp_conn->unp_addr != NULL) 2295 bcopy(unp->unp_conn->unp_addr, 2296 &xu->xu_caddr, 2297 unp->unp_conn->unp_addr->sun_len); 2298 else 2299 bzero(&xu->xu_caddr, sizeof(xu->xu_caddr)); 2300 xu->unp_vnode = (uintptr_t)unp->unp_vnode; 2301 xu->unp_conn = (uintptr_t)unp->unp_conn; 2302 xu->xu_firstref = (uintptr_t)LIST_FIRST(&unp->unp_refs); 2303 xu->xu_nextref = (uintptr_t)LIST_NEXT(unp, unp_reflink); 2304 xu->unp_gencnt = unp->unp_gencnt; 2305 sotoxsocket(unp->unp_socket, &xu->xu_socket); 2306 UNP_PCB_UNLOCK(unp); 2307 error = SYSCTL_OUT(req, xu, sizeof *xu); 2308 } else { 2309 UNP_PCB_UNLOCK(unp); 2310 } 2311 } 2312 free(xu, M_TEMP); 2313 if (!error) { 2314 /* 2315 * Give the user an updated idea of our state. If the 2316 * generation differs from what we told her before, she knows 2317 * that something happened while we were processing this 2318 * request, and it might be necessary to retry. 2319 */ 2320 xug->xug_gen = unp_gencnt; 2321 xug->xug_sogen = so_gencnt; 2322 xug->xug_count = unp_count; 2323 error = SYSCTL_OUT(req, xug, sizeof *xug); 2324 } 2325 free(unp_list, M_TEMP); 2326 free(xug, M_TEMP); 2327 return (error); 2328 } 2329 2330 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, 2331 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 2332 (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 2333 "List of active local datagram sockets"); 2334 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, 2335 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 2336 (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 2337 "List of active local stream sockets"); 2338 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, 2339 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 2340 (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb", 2341 "List of active local seqpacket sockets"); 2342 2343 static void 2344 unp_shutdown(struct unpcb *unp) 2345 { 2346 struct unpcb *unp2; 2347 struct socket *so; 2348 2349 UNP_PCB_LOCK_ASSERT(unp); 2350 2351 unp2 = unp->unp_conn; 2352 if ((unp->unp_socket->so_type == SOCK_STREAM || 2353 (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) { 2354 so = unp2->unp_socket; 2355 if (so != NULL) 2356 socantrcvmore(so); 2357 } 2358 } 2359 2360 static void 2361 unp_drop(struct unpcb *unp) 2362 { 2363 struct socket *so; 2364 struct unpcb *unp2; 2365 2366 /* 2367 * Regardless of whether the socket's peer dropped the connection 2368 * with this socket by aborting or disconnecting, POSIX requires 2369 * that ECONNRESET is returned. 2370 */ 2371 2372 UNP_PCB_LOCK(unp); 2373 so = unp->unp_socket; 2374 if (so) 2375 so->so_error = ECONNRESET; 2376 if ((unp2 = unp_pcb_lock_peer(unp)) != NULL) { 2377 /* Last reference dropped in unp_disconnect(). */ 2378 unp_pcb_rele_notlast(unp); 2379 unp_disconnect(unp, unp2); 2380 } else if (!unp_pcb_rele(unp)) { 2381 UNP_PCB_UNLOCK(unp); 2382 } 2383 } 2384 2385 static void 2386 unp_freerights(struct filedescent **fdep, int fdcount) 2387 { 2388 struct file *fp; 2389 int i; 2390 2391 KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount)); 2392 2393 for (i = 0; i < fdcount; i++) { 2394 fp = fdep[i]->fde_file; 2395 filecaps_free(&fdep[i]->fde_caps); 2396 unp_discard(fp); 2397 } 2398 free(fdep[0], M_FILECAPS); 2399 } 2400 2401 static int 2402 unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags) 2403 { 2404 struct thread *td = curthread; /* XXX */ 2405 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 2406 int i; 2407 int *fdp; 2408 struct filedesc *fdesc = td->td_proc->p_fd; 2409 struct filedescent **fdep; 2410 void *data; 2411 socklen_t clen = control->m_len, datalen; 2412 int error, newfds; 2413 u_int newlen; 2414 2415 UNP_LINK_UNLOCK_ASSERT(); 2416 2417 error = 0; 2418 if (controlp != NULL) /* controlp == NULL => free control messages */ 2419 *controlp = NULL; 2420 while (cm != NULL) { 2421 MPASS(clen >= sizeof(*cm) && clen >= cm->cmsg_len); 2422 2423 data = CMSG_DATA(cm); 2424 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 2425 if (cm->cmsg_level == SOL_SOCKET 2426 && cm->cmsg_type == SCM_RIGHTS) { 2427 newfds = datalen / sizeof(*fdep); 2428 if (newfds == 0) 2429 goto next; 2430 fdep = data; 2431 2432 /* If we're not outputting the descriptors free them. */ 2433 if (error || controlp == NULL) { 2434 unp_freerights(fdep, newfds); 2435 goto next; 2436 } 2437 FILEDESC_XLOCK(fdesc); 2438 2439 /* 2440 * Now change each pointer to an fd in the global 2441 * table to an integer that is the index to the local 2442 * fd table entry that we set up to point to the 2443 * global one we are transferring. 2444 */ 2445 newlen = newfds * sizeof(int); 2446 *controlp = sbcreatecontrol(NULL, newlen, 2447 SCM_RIGHTS, SOL_SOCKET, M_WAITOK); 2448 2449 fdp = (int *) 2450 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2451 if ((error = fdallocn(td, 0, fdp, newfds))) { 2452 FILEDESC_XUNLOCK(fdesc); 2453 unp_freerights(fdep, newfds); 2454 m_freem(*controlp); 2455 *controlp = NULL; 2456 goto next; 2457 } 2458 for (i = 0; i < newfds; i++, fdp++) { 2459 _finstall(fdesc, fdep[i]->fde_file, *fdp, 2460 (flags & MSG_CMSG_CLOEXEC) != 0 ? O_CLOEXEC : 0, 2461 &fdep[i]->fde_caps); 2462 unp_externalize_fp(fdep[i]->fde_file); 2463 } 2464 2465 /* 2466 * The new type indicates that the mbuf data refers to 2467 * kernel resources that may need to be released before 2468 * the mbuf is freed. 2469 */ 2470 m_chtype(*controlp, MT_EXTCONTROL); 2471 FILEDESC_XUNLOCK(fdesc); 2472 free(fdep[0], M_FILECAPS); 2473 } else { 2474 /* We can just copy anything else across. */ 2475 if (error || controlp == NULL) 2476 goto next; 2477 *controlp = sbcreatecontrol(NULL, datalen, 2478 cm->cmsg_type, cm->cmsg_level, M_WAITOK); 2479 bcopy(data, 2480 CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 2481 datalen); 2482 } 2483 controlp = &(*controlp)->m_next; 2484 2485 next: 2486 if (CMSG_SPACE(datalen) < clen) { 2487 clen -= CMSG_SPACE(datalen); 2488 cm = (struct cmsghdr *) 2489 ((caddr_t)cm + CMSG_SPACE(datalen)); 2490 } else { 2491 clen = 0; 2492 cm = NULL; 2493 } 2494 } 2495 2496 m_freem(control); 2497 return (error); 2498 } 2499 2500 static void 2501 unp_zone_change(void *tag) 2502 { 2503 2504 uma_zone_set_max(unp_zone, maxsockets); 2505 } 2506 2507 #ifdef INVARIANTS 2508 static void 2509 unp_zdtor(void *mem, int size __unused, void *arg __unused) 2510 { 2511 struct unpcb *unp; 2512 2513 unp = mem; 2514 2515 KASSERT(LIST_EMPTY(&unp->unp_refs), 2516 ("%s: unpcb %p has lingering refs", __func__, unp)); 2517 KASSERT(unp->unp_socket == NULL, 2518 ("%s: unpcb %p has socket backpointer", __func__, unp)); 2519 KASSERT(unp->unp_vnode == NULL, 2520 ("%s: unpcb %p has vnode references", __func__, unp)); 2521 KASSERT(unp->unp_conn == NULL, 2522 ("%s: unpcb %p is still connected", __func__, unp)); 2523 KASSERT(unp->unp_addr == NULL, 2524 ("%s: unpcb %p has leaked addr", __func__, unp)); 2525 } 2526 #endif 2527 2528 static void 2529 unp_init(void *arg __unused) 2530 { 2531 uma_dtor dtor; 2532 2533 #ifdef INVARIANTS 2534 dtor = unp_zdtor; 2535 #else 2536 dtor = NULL; 2537 #endif 2538 unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, dtor, 2539 NULL, NULL, UMA_ALIGN_CACHE, 0); 2540 uma_zone_set_max(unp_zone, maxsockets); 2541 uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached"); 2542 EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 2543 NULL, EVENTHANDLER_PRI_ANY); 2544 LIST_INIT(&unp_dhead); 2545 LIST_INIT(&unp_shead); 2546 LIST_INIT(&unp_sphead); 2547 SLIST_INIT(&unp_defers); 2548 TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL); 2549 TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL); 2550 UNP_LINK_LOCK_INIT(); 2551 UNP_DEFERRED_LOCK_INIT(); 2552 } 2553 SYSINIT(unp_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_SECOND, unp_init, NULL); 2554 2555 static void 2556 unp_internalize_cleanup_rights(struct mbuf *control) 2557 { 2558 struct cmsghdr *cp; 2559 struct mbuf *m; 2560 void *data; 2561 socklen_t datalen; 2562 2563 for (m = control; m != NULL; m = m->m_next) { 2564 cp = mtod(m, struct cmsghdr *); 2565 if (cp->cmsg_level != SOL_SOCKET || 2566 cp->cmsg_type != SCM_RIGHTS) 2567 continue; 2568 data = CMSG_DATA(cp); 2569 datalen = (caddr_t)cp + cp->cmsg_len - (caddr_t)data; 2570 unp_freerights(data, datalen / sizeof(struct filedesc *)); 2571 } 2572 } 2573 2574 static int 2575 unp_internalize(struct mbuf **controlp, struct thread *td, 2576 struct mbuf **clast, u_int *space, u_int *mbcnt) 2577 { 2578 struct mbuf *control, **initial_controlp; 2579 struct proc *p; 2580 struct filedesc *fdesc; 2581 struct bintime *bt; 2582 struct cmsghdr *cm; 2583 struct cmsgcred *cmcred; 2584 struct filedescent *fde, **fdep, *fdev; 2585 struct file *fp; 2586 struct timeval *tv; 2587 struct timespec *ts; 2588 void *data; 2589 socklen_t clen, datalen; 2590 int i, j, error, *fdp, oldfds; 2591 u_int newlen; 2592 2593 MPASS((*controlp)->m_next == NULL); /* COMPAT_OLDSOCK may violate */ 2594 UNP_LINK_UNLOCK_ASSERT(); 2595 2596 p = td->td_proc; 2597 fdesc = p->p_fd; 2598 error = 0; 2599 control = *controlp; 2600 *controlp = NULL; 2601 initial_controlp = controlp; 2602 for (clen = control->m_len, cm = mtod(control, struct cmsghdr *), 2603 data = CMSG_DATA(cm); 2604 2605 clen >= sizeof(*cm) && cm->cmsg_level == SOL_SOCKET && 2606 clen >= cm->cmsg_len && cm->cmsg_len >= sizeof(*cm) && 2607 (char *)cm + cm->cmsg_len >= (char *)data; 2608 2609 clen -= min(CMSG_SPACE(datalen), clen), 2610 cm = (struct cmsghdr *) ((char *)cm + CMSG_SPACE(datalen)), 2611 data = CMSG_DATA(cm)) { 2612 datalen = (char *)cm + cm->cmsg_len - (char *)data; 2613 switch (cm->cmsg_type) { 2614 case SCM_CREDS: 2615 *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 2616 SCM_CREDS, SOL_SOCKET, M_WAITOK); 2617 cmcred = (struct cmsgcred *) 2618 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2619 cmcred->cmcred_pid = p->p_pid; 2620 cmcred->cmcred_uid = td->td_ucred->cr_ruid; 2621 cmcred->cmcred_gid = td->td_ucred->cr_rgid; 2622 cmcred->cmcred_euid = td->td_ucred->cr_uid; 2623 cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 2624 CMGROUP_MAX); 2625 for (i = 0; i < cmcred->cmcred_ngroups; i++) 2626 cmcred->cmcred_groups[i] = 2627 td->td_ucred->cr_groups[i]; 2628 break; 2629 2630 case SCM_RIGHTS: 2631 oldfds = datalen / sizeof (int); 2632 if (oldfds == 0) 2633 continue; 2634 /* On some machines sizeof pointer is bigger than 2635 * sizeof int, so we need to check if data fits into 2636 * single mbuf. We could allocate several mbufs, and 2637 * unp_externalize() should even properly handle that. 2638 * But it is not worth to complicate the code for an 2639 * insane scenario of passing over 200 file descriptors 2640 * at once. 2641 */ 2642 newlen = oldfds * sizeof(fdep[0]); 2643 if (CMSG_SPACE(newlen) > MCLBYTES) { 2644 error = EMSGSIZE; 2645 goto out; 2646 } 2647 /* 2648 * Check that all the FDs passed in refer to legal 2649 * files. If not, reject the entire operation. 2650 */ 2651 fdp = data; 2652 FILEDESC_SLOCK(fdesc); 2653 for (i = 0; i < oldfds; i++, fdp++) { 2654 fp = fget_noref(fdesc, *fdp); 2655 if (fp == NULL) { 2656 FILEDESC_SUNLOCK(fdesc); 2657 error = EBADF; 2658 goto out; 2659 } 2660 if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 2661 FILEDESC_SUNLOCK(fdesc); 2662 error = EOPNOTSUPP; 2663 goto out; 2664 } 2665 } 2666 2667 /* 2668 * Now replace the integer FDs with pointers to the 2669 * file structure and capability rights. 2670 */ 2671 *controlp = sbcreatecontrol(NULL, newlen, 2672 SCM_RIGHTS, SOL_SOCKET, M_WAITOK); 2673 fdp = data; 2674 for (i = 0; i < oldfds; i++, fdp++) { 2675 if (!fhold(fdesc->fd_ofiles[*fdp].fde_file)) { 2676 fdp = data; 2677 for (j = 0; j < i; j++, fdp++) { 2678 fdrop(fdesc->fd_ofiles[*fdp]. 2679 fde_file, td); 2680 } 2681 FILEDESC_SUNLOCK(fdesc); 2682 error = EBADF; 2683 goto out; 2684 } 2685 } 2686 fdp = data; 2687 fdep = (struct filedescent **) 2688 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2689 fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS, 2690 M_WAITOK); 2691 for (i = 0; i < oldfds; i++, fdev++, fdp++) { 2692 fde = &fdesc->fd_ofiles[*fdp]; 2693 fdep[i] = fdev; 2694 fdep[i]->fde_file = fde->fde_file; 2695 filecaps_copy(&fde->fde_caps, 2696 &fdep[i]->fde_caps, true); 2697 unp_internalize_fp(fdep[i]->fde_file); 2698 } 2699 FILEDESC_SUNLOCK(fdesc); 2700 break; 2701 2702 case SCM_TIMESTAMP: 2703 *controlp = sbcreatecontrol(NULL, sizeof(*tv), 2704 SCM_TIMESTAMP, SOL_SOCKET, M_WAITOK); 2705 tv = (struct timeval *) 2706 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2707 microtime(tv); 2708 break; 2709 2710 case SCM_BINTIME: 2711 *controlp = sbcreatecontrol(NULL, sizeof(*bt), 2712 SCM_BINTIME, SOL_SOCKET, M_WAITOK); 2713 bt = (struct bintime *) 2714 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2715 bintime(bt); 2716 break; 2717 2718 case SCM_REALTIME: 2719 *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2720 SCM_REALTIME, SOL_SOCKET, M_WAITOK); 2721 ts = (struct timespec *) 2722 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2723 nanotime(ts); 2724 break; 2725 2726 case SCM_MONOTONIC: 2727 *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2728 SCM_MONOTONIC, SOL_SOCKET, M_WAITOK); 2729 ts = (struct timespec *) 2730 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2731 nanouptime(ts); 2732 break; 2733 2734 default: 2735 error = EINVAL; 2736 goto out; 2737 } 2738 2739 if (space != NULL) { 2740 *space += (*controlp)->m_len; 2741 *mbcnt += MSIZE; 2742 if ((*controlp)->m_flags & M_EXT) 2743 *mbcnt += (*controlp)->m_ext.ext_size; 2744 *clast = *controlp; 2745 } 2746 controlp = &(*controlp)->m_next; 2747 } 2748 if (clen > 0) 2749 error = EINVAL; 2750 2751 out: 2752 if (error != 0 && initial_controlp != NULL) 2753 unp_internalize_cleanup_rights(*initial_controlp); 2754 m_freem(control); 2755 return (error); 2756 } 2757 2758 static struct mbuf * 2759 unp_addsockcred(struct thread *td, struct mbuf *control, int mode, 2760 struct mbuf **clast, u_int *space, u_int *mbcnt) 2761 { 2762 struct mbuf *m, *n, *n_prev; 2763 const struct cmsghdr *cm; 2764 int ngroups, i, cmsgtype; 2765 size_t ctrlsz; 2766 2767 ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 2768 if (mode & UNP_WANTCRED_ALWAYS) { 2769 ctrlsz = SOCKCRED2SIZE(ngroups); 2770 cmsgtype = SCM_CREDS2; 2771 } else { 2772 ctrlsz = SOCKCREDSIZE(ngroups); 2773 cmsgtype = SCM_CREDS; 2774 } 2775 2776 m = sbcreatecontrol(NULL, ctrlsz, cmsgtype, SOL_SOCKET, M_NOWAIT); 2777 if (m == NULL) 2778 return (control); 2779 MPASS((m->m_flags & M_EXT) == 0 && m->m_next == NULL); 2780 2781 if (mode & UNP_WANTCRED_ALWAYS) { 2782 struct sockcred2 *sc; 2783 2784 sc = (void *)CMSG_DATA(mtod(m, struct cmsghdr *)); 2785 sc->sc_version = 0; 2786 sc->sc_pid = td->td_proc->p_pid; 2787 sc->sc_uid = td->td_ucred->cr_ruid; 2788 sc->sc_euid = td->td_ucred->cr_uid; 2789 sc->sc_gid = td->td_ucred->cr_rgid; 2790 sc->sc_egid = td->td_ucred->cr_gid; 2791 sc->sc_ngroups = ngroups; 2792 for (i = 0; i < sc->sc_ngroups; i++) 2793 sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 2794 } else { 2795 struct sockcred *sc; 2796 2797 sc = (void *)CMSG_DATA(mtod(m, struct cmsghdr *)); 2798 sc->sc_uid = td->td_ucred->cr_ruid; 2799 sc->sc_euid = td->td_ucred->cr_uid; 2800 sc->sc_gid = td->td_ucred->cr_rgid; 2801 sc->sc_egid = td->td_ucred->cr_gid; 2802 sc->sc_ngroups = ngroups; 2803 for (i = 0; i < sc->sc_ngroups; i++) 2804 sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 2805 } 2806 2807 /* 2808 * Unlink SCM_CREDS control messages (struct cmsgcred), since just 2809 * created SCM_CREDS control message (struct sockcred) has another 2810 * format. 2811 */ 2812 if (control != NULL && cmsgtype == SCM_CREDS) 2813 for (n = control, n_prev = NULL; n != NULL;) { 2814 cm = mtod(n, struct cmsghdr *); 2815 if (cm->cmsg_level == SOL_SOCKET && 2816 cm->cmsg_type == SCM_CREDS) { 2817 if (n_prev == NULL) 2818 control = n->m_next; 2819 else 2820 n_prev->m_next = n->m_next; 2821 if (space != NULL) { 2822 MPASS(*space >= n->m_len); 2823 *space -= n->m_len; 2824 MPASS(*mbcnt >= MSIZE); 2825 *mbcnt -= MSIZE; 2826 if (n->m_flags & M_EXT) { 2827 MPASS(*mbcnt >= 2828 n->m_ext.ext_size); 2829 *mbcnt -= n->m_ext.ext_size; 2830 } 2831 MPASS(clast); 2832 if (*clast == n) { 2833 MPASS(n->m_next == NULL); 2834 if (n_prev == NULL) 2835 *clast = m; 2836 else 2837 *clast = n_prev; 2838 } 2839 } 2840 n = m_free(n); 2841 } else { 2842 n_prev = n; 2843 n = n->m_next; 2844 } 2845 } 2846 2847 /* Prepend it to the head. */ 2848 m->m_next = control; 2849 if (space != NULL) { 2850 *space += m->m_len; 2851 *mbcnt += MSIZE; 2852 if (control == NULL) 2853 *clast = m; 2854 } 2855 return (m); 2856 } 2857 2858 static struct unpcb * 2859 fptounp(struct file *fp) 2860 { 2861 struct socket *so; 2862 2863 if (fp->f_type != DTYPE_SOCKET) 2864 return (NULL); 2865 if ((so = fp->f_data) == NULL) 2866 return (NULL); 2867 if (so->so_proto->pr_domain != &localdomain) 2868 return (NULL); 2869 return sotounpcb(so); 2870 } 2871 2872 static void 2873 unp_discard(struct file *fp) 2874 { 2875 struct unp_defer *dr; 2876 2877 if (unp_externalize_fp(fp)) { 2878 dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK); 2879 dr->ud_fp = fp; 2880 UNP_DEFERRED_LOCK(); 2881 SLIST_INSERT_HEAD(&unp_defers, dr, ud_link); 2882 UNP_DEFERRED_UNLOCK(); 2883 atomic_add_int(&unp_defers_count, 1); 2884 taskqueue_enqueue(taskqueue_thread, &unp_defer_task); 2885 } else 2886 closef_nothread(fp); 2887 } 2888 2889 static void 2890 unp_process_defers(void *arg __unused, int pending) 2891 { 2892 struct unp_defer *dr; 2893 SLIST_HEAD(, unp_defer) drl; 2894 int count; 2895 2896 SLIST_INIT(&drl); 2897 for (;;) { 2898 UNP_DEFERRED_LOCK(); 2899 if (SLIST_FIRST(&unp_defers) == NULL) { 2900 UNP_DEFERRED_UNLOCK(); 2901 break; 2902 } 2903 SLIST_SWAP(&unp_defers, &drl, unp_defer); 2904 UNP_DEFERRED_UNLOCK(); 2905 count = 0; 2906 while ((dr = SLIST_FIRST(&drl)) != NULL) { 2907 SLIST_REMOVE_HEAD(&drl, ud_link); 2908 closef_nothread(dr->ud_fp); 2909 free(dr, M_TEMP); 2910 count++; 2911 } 2912 atomic_add_int(&unp_defers_count, -count); 2913 } 2914 } 2915 2916 static void 2917 unp_internalize_fp(struct file *fp) 2918 { 2919 struct unpcb *unp; 2920 2921 UNP_LINK_WLOCK(); 2922 if ((unp = fptounp(fp)) != NULL) { 2923 unp->unp_file = fp; 2924 unp->unp_msgcount++; 2925 } 2926 unp_rights++; 2927 UNP_LINK_WUNLOCK(); 2928 } 2929 2930 static int 2931 unp_externalize_fp(struct file *fp) 2932 { 2933 struct unpcb *unp; 2934 int ret; 2935 2936 UNP_LINK_WLOCK(); 2937 if ((unp = fptounp(fp)) != NULL) { 2938 unp->unp_msgcount--; 2939 ret = 1; 2940 } else 2941 ret = 0; 2942 unp_rights--; 2943 UNP_LINK_WUNLOCK(); 2944 return (ret); 2945 } 2946 2947 /* 2948 * unp_defer indicates whether additional work has been defered for a future 2949 * pass through unp_gc(). It is thread local and does not require explicit 2950 * synchronization. 2951 */ 2952 static int unp_marked; 2953 2954 static void 2955 unp_remove_dead_ref(struct filedescent **fdep, int fdcount) 2956 { 2957 struct unpcb *unp; 2958 struct file *fp; 2959 int i; 2960 2961 /* 2962 * This function can only be called from the gc task. 2963 */ 2964 KASSERT(taskqueue_member(taskqueue_thread, curthread) != 0, 2965 ("%s: not on gc callout", __func__)); 2966 UNP_LINK_LOCK_ASSERT(); 2967 2968 for (i = 0; i < fdcount; i++) { 2969 fp = fdep[i]->fde_file; 2970 if ((unp = fptounp(fp)) == NULL) 2971 continue; 2972 if ((unp->unp_gcflag & UNPGC_DEAD) == 0) 2973 continue; 2974 unp->unp_gcrefs--; 2975 } 2976 } 2977 2978 static void 2979 unp_restore_undead_ref(struct filedescent **fdep, int fdcount) 2980 { 2981 struct unpcb *unp; 2982 struct file *fp; 2983 int i; 2984 2985 /* 2986 * This function can only be called from the gc task. 2987 */ 2988 KASSERT(taskqueue_member(taskqueue_thread, curthread) != 0, 2989 ("%s: not on gc callout", __func__)); 2990 UNP_LINK_LOCK_ASSERT(); 2991 2992 for (i = 0; i < fdcount; i++) { 2993 fp = fdep[i]->fde_file; 2994 if ((unp = fptounp(fp)) == NULL) 2995 continue; 2996 if ((unp->unp_gcflag & UNPGC_DEAD) == 0) 2997 continue; 2998 unp->unp_gcrefs++; 2999 unp_marked++; 3000 } 3001 } 3002 3003 static void 3004 unp_scan_socket(struct socket *so, void (*op)(struct filedescent **, int)) 3005 { 3006 struct sockbuf *sb; 3007 3008 SOCK_LOCK_ASSERT(so); 3009 3010 if (sotounpcb(so)->unp_gcflag & UNPGC_IGNORE_RIGHTS) 3011 return; 3012 3013 SOCK_RECVBUF_LOCK(so); 3014 switch (so->so_type) { 3015 case SOCK_DGRAM: 3016 unp_scan(STAILQ_FIRST(&so->so_rcv.uxdg_mb), op); 3017 unp_scan(so->so_rcv.uxdg_peeked, op); 3018 TAILQ_FOREACH(sb, &so->so_rcv.uxdg_conns, uxdg_clist) 3019 unp_scan(STAILQ_FIRST(&sb->uxdg_mb), op); 3020 break; 3021 case SOCK_STREAM: 3022 case SOCK_SEQPACKET: 3023 unp_scan(so->so_rcv.sb_mb, op); 3024 break; 3025 } 3026 SOCK_RECVBUF_UNLOCK(so); 3027 } 3028 3029 static void 3030 unp_gc_scan(struct unpcb *unp, void (*op)(struct filedescent **, int)) 3031 { 3032 struct socket *so, *soa; 3033 3034 so = unp->unp_socket; 3035 SOCK_LOCK(so); 3036 if (SOLISTENING(so)) { 3037 /* 3038 * Mark all sockets in our accept queue. 3039 */ 3040 TAILQ_FOREACH(soa, &so->sol_comp, so_list) 3041 unp_scan_socket(soa, op); 3042 } else { 3043 /* 3044 * Mark all sockets we reference with RIGHTS. 3045 */ 3046 unp_scan_socket(so, op); 3047 } 3048 SOCK_UNLOCK(so); 3049 } 3050 3051 static int unp_recycled; 3052 SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, 3053 "Number of unreachable sockets claimed by the garbage collector."); 3054 3055 static int unp_taskcount; 3056 SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, 3057 "Number of times the garbage collector has run."); 3058 3059 SYSCTL_UINT(_net_local, OID_AUTO, sockcount, CTLFLAG_RD, &unp_count, 0, 3060 "Number of active local sockets."); 3061 3062 static void 3063 unp_gc(__unused void *arg, int pending) 3064 { 3065 struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead, 3066 NULL }; 3067 struct unp_head **head; 3068 struct unp_head unp_deadhead; /* List of potentially-dead sockets. */ 3069 struct file *f, **unref; 3070 struct unpcb *unp, *unptmp; 3071 int i, total, unp_unreachable; 3072 3073 LIST_INIT(&unp_deadhead); 3074 unp_taskcount++; 3075 UNP_LINK_RLOCK(); 3076 /* 3077 * First determine which sockets may be in cycles. 3078 */ 3079 unp_unreachable = 0; 3080 3081 for (head = heads; *head != NULL; head++) 3082 LIST_FOREACH(unp, *head, unp_link) { 3083 KASSERT((unp->unp_gcflag & ~UNPGC_IGNORE_RIGHTS) == 0, 3084 ("%s: unp %p has unexpected gc flags 0x%x", 3085 __func__, unp, (unsigned int)unp->unp_gcflag)); 3086 3087 f = unp->unp_file; 3088 3089 /* 3090 * Check for an unreachable socket potentially in a 3091 * cycle. It must be in a queue as indicated by 3092 * msgcount, and this must equal the file reference 3093 * count. Note that when msgcount is 0 the file is 3094 * NULL. 3095 */ 3096 if (f != NULL && unp->unp_msgcount != 0 && 3097 refcount_load(&f->f_count) == unp->unp_msgcount) { 3098 LIST_INSERT_HEAD(&unp_deadhead, unp, unp_dead); 3099 unp->unp_gcflag |= UNPGC_DEAD; 3100 unp->unp_gcrefs = unp->unp_msgcount; 3101 unp_unreachable++; 3102 } 3103 } 3104 3105 /* 3106 * Scan all sockets previously marked as potentially being in a cycle 3107 * and remove the references each socket holds on any UNPGC_DEAD 3108 * sockets in its queue. After this step, all remaining references on 3109 * sockets marked UNPGC_DEAD should not be part of any cycle. 3110 */ 3111 LIST_FOREACH(unp, &unp_deadhead, unp_dead) 3112 unp_gc_scan(unp, unp_remove_dead_ref); 3113 3114 /* 3115 * If a socket still has a non-negative refcount, it cannot be in a 3116 * cycle. In this case increment refcount of all children iteratively. 3117 * Stop the scan once we do a complete loop without discovering 3118 * a new reachable socket. 3119 */ 3120 do { 3121 unp_marked = 0; 3122 LIST_FOREACH_SAFE(unp, &unp_deadhead, unp_dead, unptmp) 3123 if (unp->unp_gcrefs > 0) { 3124 unp->unp_gcflag &= ~UNPGC_DEAD; 3125 LIST_REMOVE(unp, unp_dead); 3126 KASSERT(unp_unreachable > 0, 3127 ("%s: unp_unreachable underflow.", 3128 __func__)); 3129 unp_unreachable--; 3130 unp_gc_scan(unp, unp_restore_undead_ref); 3131 } 3132 } while (unp_marked); 3133 3134 UNP_LINK_RUNLOCK(); 3135 3136 if (unp_unreachable == 0) 3137 return; 3138 3139 /* 3140 * Allocate space for a local array of dead unpcbs. 3141 * TODO: can this path be simplified by instead using the local 3142 * dead list at unp_deadhead, after taking out references 3143 * on the file object and/or unpcb and dropping the link lock? 3144 */ 3145 unref = malloc(unp_unreachable * sizeof(struct file *), 3146 M_TEMP, M_WAITOK); 3147 3148 /* 3149 * Iterate looking for sockets which have been specifically marked 3150 * as unreachable and store them locally. 3151 */ 3152 UNP_LINK_RLOCK(); 3153 total = 0; 3154 LIST_FOREACH(unp, &unp_deadhead, unp_dead) { 3155 KASSERT((unp->unp_gcflag & UNPGC_DEAD) != 0, 3156 ("%s: unp %p not marked UNPGC_DEAD", __func__, unp)); 3157 unp->unp_gcflag &= ~UNPGC_DEAD; 3158 f = unp->unp_file; 3159 if (unp->unp_msgcount == 0 || f == NULL || 3160 refcount_load(&f->f_count) != unp->unp_msgcount || 3161 !fhold(f)) 3162 continue; 3163 unref[total++] = f; 3164 KASSERT(total <= unp_unreachable, 3165 ("%s: incorrect unreachable count.", __func__)); 3166 } 3167 UNP_LINK_RUNLOCK(); 3168 3169 /* 3170 * Now flush all sockets, free'ing rights. This will free the 3171 * struct files associated with these sockets but leave each socket 3172 * with one remaining ref. 3173 */ 3174 for (i = 0; i < total; i++) { 3175 struct socket *so; 3176 3177 so = unref[i]->f_data; 3178 CURVNET_SET(so->so_vnet); 3179 sorflush(so); 3180 CURVNET_RESTORE(); 3181 } 3182 3183 /* 3184 * And finally release the sockets so they can be reclaimed. 3185 */ 3186 for (i = 0; i < total; i++) 3187 fdrop(unref[i], NULL); 3188 unp_recycled += total; 3189 free(unref, M_TEMP); 3190 } 3191 3192 /* 3193 * Synchronize against unp_gc, which can trip over data as we are freeing it. 3194 */ 3195 static void 3196 unp_dispose(struct socket *so) 3197 { 3198 struct sockbuf *sb; 3199 struct unpcb *unp; 3200 struct mbuf *m; 3201 3202 MPASS(!SOLISTENING(so)); 3203 3204 unp = sotounpcb(so); 3205 UNP_LINK_WLOCK(); 3206 unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS; 3207 UNP_LINK_WUNLOCK(); 3208 3209 /* 3210 * Grab our special mbufs before calling sbrelease(). 3211 */ 3212 SOCK_RECVBUF_LOCK(so); 3213 switch (so->so_type) { 3214 case SOCK_DGRAM: 3215 while ((sb = TAILQ_FIRST(&so->so_rcv.uxdg_conns)) != NULL) { 3216 STAILQ_CONCAT(&so->so_rcv.uxdg_mb, &sb->uxdg_mb); 3217 TAILQ_REMOVE(&so->so_rcv.uxdg_conns, sb, uxdg_clist); 3218 /* Note: socket of sb may reconnect. */ 3219 sb->uxdg_cc = sb->uxdg_ctl = sb->uxdg_mbcnt = 0; 3220 } 3221 sb = &so->so_rcv; 3222 if (sb->uxdg_peeked != NULL) { 3223 STAILQ_INSERT_HEAD(&sb->uxdg_mb, sb->uxdg_peeked, 3224 m_stailqpkt); 3225 sb->uxdg_peeked = NULL; 3226 } 3227 m = STAILQ_FIRST(&sb->uxdg_mb); 3228 STAILQ_INIT(&sb->uxdg_mb); 3229 /* XXX: our shortened sbrelease() */ 3230 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 3231 RLIM_INFINITY); 3232 /* 3233 * XXXGL Mark sb with SBS_CANTRCVMORE. This is needed to 3234 * prevent uipc_sosend_dgram() or unp_disconnect() adding more 3235 * data to the socket. 3236 * We are now in dom_dispose and it could be a call from 3237 * soshutdown() or from the final sofree(). The sofree() case 3238 * is simple as it guarantees that no more sends will happen, 3239 * however we can race with unp_disconnect() from our peer. 3240 * The shutdown(2) case is more exotic. It would call into 3241 * dom_dispose() only if socket is SS_ISCONNECTED. This is 3242 * possible if we did connect(2) on this socket and we also 3243 * had it bound with bind(2) and receive connections from other 3244 * sockets. Because soshutdown() violates POSIX (see comment 3245 * there) we will end up here shutting down our receive side. 3246 * Of course this will have affect not only on the peer we 3247 * connect(2)ed to, but also on all of the peers who had 3248 * connect(2)ed to us. Their sends would end up with ENOBUFS. 3249 */ 3250 sb->sb_state |= SBS_CANTRCVMORE; 3251 break; 3252 case SOCK_STREAM: 3253 case SOCK_SEQPACKET: 3254 sb = &so->so_rcv; 3255 m = sbcut_locked(sb, sb->sb_ccc); 3256 KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0, 3257 ("%s: ccc %u mb %p mbcnt %u", __func__, 3258 sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt)); 3259 sbrelease_locked(so, SO_RCV); 3260 break; 3261 } 3262 SOCK_RECVBUF_UNLOCK(so); 3263 if (SOCK_IO_RECV_OWNED(so)) 3264 SOCK_IO_RECV_UNLOCK(so); 3265 3266 if (m != NULL) { 3267 unp_scan(m, unp_freerights); 3268 m_freem(m); 3269 } 3270 } 3271 3272 static void 3273 unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int)) 3274 { 3275 struct mbuf *m; 3276 struct cmsghdr *cm; 3277 void *data; 3278 socklen_t clen, datalen; 3279 3280 while (m0 != NULL) { 3281 for (m = m0; m; m = m->m_next) { 3282 if (m->m_type != MT_CONTROL) 3283 continue; 3284 3285 cm = mtod(m, struct cmsghdr *); 3286 clen = m->m_len; 3287 3288 while (cm != NULL) { 3289 if (sizeof(*cm) > clen || cm->cmsg_len > clen) 3290 break; 3291 3292 data = CMSG_DATA(cm); 3293 datalen = (caddr_t)cm + cm->cmsg_len 3294 - (caddr_t)data; 3295 3296 if (cm->cmsg_level == SOL_SOCKET && 3297 cm->cmsg_type == SCM_RIGHTS) { 3298 (*op)(data, datalen / 3299 sizeof(struct filedescent *)); 3300 } 3301 3302 if (CMSG_SPACE(datalen) < clen) { 3303 clen -= CMSG_SPACE(datalen); 3304 cm = (struct cmsghdr *) 3305 ((caddr_t)cm + CMSG_SPACE(datalen)); 3306 } else { 3307 clen = 0; 3308 cm = NULL; 3309 } 3310 } 3311 } 3312 m0 = m0->m_nextpkt; 3313 } 3314 } 3315 3316 /* 3317 * Definitions of protocols supported in the LOCAL domain. 3318 */ 3319 static struct protosw streamproto = { 3320 .pr_type = SOCK_STREAM, 3321 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS| 3322 PR_CAPATTACH, 3323 .pr_ctloutput = &uipc_ctloutput, 3324 .pr_abort = uipc_abort, 3325 .pr_accept = uipc_accept, 3326 .pr_attach = uipc_attach, 3327 .pr_bind = uipc_bind, 3328 .pr_bindat = uipc_bindat, 3329 .pr_connect = uipc_connect, 3330 .pr_connectat = uipc_connectat, 3331 .pr_connect2 = uipc_connect2, 3332 .pr_detach = uipc_detach, 3333 .pr_disconnect = uipc_disconnect, 3334 .pr_listen = uipc_listen, 3335 .pr_peeraddr = uipc_peeraddr, 3336 .pr_rcvd = uipc_rcvd, 3337 .pr_send = uipc_send, 3338 .pr_ready = uipc_ready, 3339 .pr_sense = uipc_sense, 3340 .pr_shutdown = uipc_shutdown, 3341 .pr_sockaddr = uipc_sockaddr, 3342 .pr_soreceive = soreceive_generic, 3343 .pr_close = uipc_close, 3344 }; 3345 3346 static struct protosw dgramproto = { 3347 .pr_type = SOCK_DGRAM, 3348 .pr_flags = PR_ATOMIC | PR_ADDR |PR_RIGHTS | PR_CAPATTACH | 3349 PR_SOCKBUF, 3350 .pr_ctloutput = &uipc_ctloutput, 3351 .pr_abort = uipc_abort, 3352 .pr_accept = uipc_accept, 3353 .pr_attach = uipc_attach, 3354 .pr_bind = uipc_bind, 3355 .pr_bindat = uipc_bindat, 3356 .pr_connect = uipc_connect, 3357 .pr_connectat = uipc_connectat, 3358 .pr_connect2 = uipc_connect2, 3359 .pr_detach = uipc_detach, 3360 .pr_disconnect = uipc_disconnect, 3361 .pr_peeraddr = uipc_peeraddr, 3362 .pr_sosend = uipc_sosend_dgram, 3363 .pr_sense = uipc_sense, 3364 .pr_shutdown = uipc_shutdown, 3365 .pr_sockaddr = uipc_sockaddr, 3366 .pr_soreceive = uipc_soreceive_dgram, 3367 .pr_close = uipc_close, 3368 }; 3369 3370 static struct protosw seqpacketproto = { 3371 .pr_type = SOCK_SEQPACKET, 3372 /* 3373 * XXXRW: For now, PR_ADDR because soreceive will bump into them 3374 * due to our use of sbappendaddr. A new sbappend variants is needed 3375 * that supports both atomic record writes and control data. 3376 */ 3377 .pr_flags = PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED| 3378 PR_WANTRCVD|PR_RIGHTS|PR_CAPATTACH, 3379 .pr_ctloutput = &uipc_ctloutput, 3380 .pr_abort = uipc_abort, 3381 .pr_accept = uipc_accept, 3382 .pr_attach = uipc_attach, 3383 .pr_bind = uipc_bind, 3384 .pr_bindat = uipc_bindat, 3385 .pr_connect = uipc_connect, 3386 .pr_connectat = uipc_connectat, 3387 .pr_connect2 = uipc_connect2, 3388 .pr_detach = uipc_detach, 3389 .pr_disconnect = uipc_disconnect, 3390 .pr_listen = uipc_listen, 3391 .pr_peeraddr = uipc_peeraddr, 3392 .pr_rcvd = uipc_rcvd, 3393 .pr_send = uipc_send, 3394 .pr_sense = uipc_sense, 3395 .pr_shutdown = uipc_shutdown, 3396 .pr_sockaddr = uipc_sockaddr, 3397 .pr_soreceive = soreceive_generic, /* XXX: or...? */ 3398 .pr_close = uipc_close, 3399 }; 3400 3401 static struct domain localdomain = { 3402 .dom_family = AF_LOCAL, 3403 .dom_name = "local", 3404 .dom_externalize = unp_externalize, 3405 .dom_dispose = unp_dispose, 3406 .dom_nprotosw = 3, 3407 .dom_protosw = { 3408 &streamproto, 3409 &dgramproto, 3410 &seqpacketproto, 3411 } 3412 }; 3413 DOMAIN_SET(local); 3414 3415 /* 3416 * A helper function called by VFS before socket-type vnode reclamation. 3417 * For an active vnode it clears unp_vnode pointer and decrements unp_vnode 3418 * use count. 3419 */ 3420 void 3421 vfs_unp_reclaim(struct vnode *vp) 3422 { 3423 struct unpcb *unp; 3424 int active; 3425 struct mtx *vplock; 3426 3427 ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim"); 3428 KASSERT(vp->v_type == VSOCK, 3429 ("vfs_unp_reclaim: vp->v_type != VSOCK")); 3430 3431 active = 0; 3432 vplock = mtx_pool_find(mtxpool_sleep, vp); 3433 mtx_lock(vplock); 3434 VOP_UNP_CONNECT(vp, &unp); 3435 if (unp == NULL) 3436 goto done; 3437 UNP_PCB_LOCK(unp); 3438 if (unp->unp_vnode == vp) { 3439 VOP_UNP_DETACH(vp); 3440 unp->unp_vnode = NULL; 3441 active = 1; 3442 } 3443 UNP_PCB_UNLOCK(unp); 3444 done: 3445 mtx_unlock(vplock); 3446 if (active) 3447 vunref(vp); 3448 } 3449 3450 #ifdef DDB 3451 static void 3452 db_print_indent(int indent) 3453 { 3454 int i; 3455 3456 for (i = 0; i < indent; i++) 3457 db_printf(" "); 3458 } 3459 3460 static void 3461 db_print_unpflags(int unp_flags) 3462 { 3463 int comma; 3464 3465 comma = 0; 3466 if (unp_flags & UNP_HAVEPC) { 3467 db_printf("%sUNP_HAVEPC", comma ? ", " : ""); 3468 comma = 1; 3469 } 3470 if (unp_flags & UNP_WANTCRED_ALWAYS) { 3471 db_printf("%sUNP_WANTCRED_ALWAYS", comma ? ", " : ""); 3472 comma = 1; 3473 } 3474 if (unp_flags & UNP_WANTCRED_ONESHOT) { 3475 db_printf("%sUNP_WANTCRED_ONESHOT", comma ? ", " : ""); 3476 comma = 1; 3477 } 3478 if (unp_flags & UNP_CONNWAIT) { 3479 db_printf("%sUNP_CONNWAIT", comma ? ", " : ""); 3480 comma = 1; 3481 } 3482 if (unp_flags & UNP_CONNECTING) { 3483 db_printf("%sUNP_CONNECTING", comma ? ", " : ""); 3484 comma = 1; 3485 } 3486 if (unp_flags & UNP_BINDING) { 3487 db_printf("%sUNP_BINDING", comma ? ", " : ""); 3488 comma = 1; 3489 } 3490 } 3491 3492 static void 3493 db_print_xucred(int indent, struct xucred *xu) 3494 { 3495 int comma, i; 3496 3497 db_print_indent(indent); 3498 db_printf("cr_version: %u cr_uid: %u cr_pid: %d cr_ngroups: %d\n", 3499 xu->cr_version, xu->cr_uid, xu->cr_pid, xu->cr_ngroups); 3500 db_print_indent(indent); 3501 db_printf("cr_groups: "); 3502 comma = 0; 3503 for (i = 0; i < xu->cr_ngroups; i++) { 3504 db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]); 3505 comma = 1; 3506 } 3507 db_printf("\n"); 3508 } 3509 3510 static void 3511 db_print_unprefs(int indent, struct unp_head *uh) 3512 { 3513 struct unpcb *unp; 3514 int counter; 3515 3516 counter = 0; 3517 LIST_FOREACH(unp, uh, unp_reflink) { 3518 if (counter % 4 == 0) 3519 db_print_indent(indent); 3520 db_printf("%p ", unp); 3521 if (counter % 4 == 3) 3522 db_printf("\n"); 3523 counter++; 3524 } 3525 if (counter != 0 && counter % 4 != 0) 3526 db_printf("\n"); 3527 } 3528 3529 DB_SHOW_COMMAND(unpcb, db_show_unpcb) 3530 { 3531 struct unpcb *unp; 3532 3533 if (!have_addr) { 3534 db_printf("usage: show unpcb <addr>\n"); 3535 return; 3536 } 3537 unp = (struct unpcb *)addr; 3538 3539 db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket, 3540 unp->unp_vnode); 3541 3542 db_printf("unp_ino: %ju unp_conn: %p\n", (uintmax_t)unp->unp_ino, 3543 unp->unp_conn); 3544 3545 db_printf("unp_refs:\n"); 3546 db_print_unprefs(2, &unp->unp_refs); 3547 3548 /* XXXRW: Would be nice to print the full address, if any. */ 3549 db_printf("unp_addr: %p\n", unp->unp_addr); 3550 3551 db_printf("unp_gencnt: %llu\n", 3552 (unsigned long long)unp->unp_gencnt); 3553 3554 db_printf("unp_flags: %x (", unp->unp_flags); 3555 db_print_unpflags(unp->unp_flags); 3556 db_printf(")\n"); 3557 3558 db_printf("unp_peercred:\n"); 3559 db_print_xucred(2, &unp->unp_peercred); 3560 3561 db_printf("unp_refcount: %u\n", unp->unp_refcount); 3562 } 3563 #endif 3564