1 /*- 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. 4 * Copyright (c) 2004-2009 Robert N. M. Watson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 4. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 32 */ 33 34 /* 35 * UNIX Domain (Local) Sockets 36 * 37 * This is an implementation of UNIX (local) domain sockets. Each socket has 38 * an associated struct unpcb (UNIX protocol control block). Stream sockets 39 * may be connected to 0 or 1 other socket. Datagram sockets may be 40 * connected to 0, 1, or many other sockets. Sockets may be created and 41 * connected in pairs (socketpair(2)), or bound/connected to using the file 42 * system name space. For most purposes, only the receive socket buffer is 43 * used, as sending on one socket delivers directly to the receive socket 44 * buffer of a second socket. 45 * 46 * The implementation is substantially complicated by the fact that 47 * "ancillary data", such as file descriptors or credentials, may be passed 48 * across UNIX domain sockets. The potential for passing UNIX domain sockets 49 * over other UNIX domain sockets requires the implementation of a simple 50 * garbage collector to find and tear down cycles of disconnected sockets. 51 * 52 * TODO: 53 * RDM 54 * distinguish datagram size limits from flow control limits in SEQPACKET 55 * rethink name space problems 56 * need a proper out-of-band 57 */ 58 59 #include <sys/cdefs.h> 60 __FBSDID("$FreeBSD$"); 61 62 #include "opt_ddb.h" 63 64 #include <sys/param.h> 65 #include <sys/capability.h> 66 #include <sys/domain.h> 67 #include <sys/fcntl.h> 68 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 69 #include <sys/eventhandler.h> 70 #include <sys/file.h> 71 #include <sys/filedesc.h> 72 #include <sys/kernel.h> 73 #include <sys/lock.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 /* 108 * Locking key: 109 * (l) Locked using list lock 110 * (g) Locked using linkage lock 111 */ 112 113 static uma_zone_t unp_zone; 114 static unp_gen_t unp_gencnt; /* (l) */ 115 static u_int unp_count; /* (l) Count of local sockets. */ 116 static ino_t unp_ino; /* Prototype for fake inode numbers. */ 117 static int unp_rights; /* (g) File descriptors in flight. */ 118 static struct unp_head unp_shead; /* (l) List of stream sockets. */ 119 static struct unp_head unp_dhead; /* (l) List of datagram sockets. */ 120 static struct unp_head unp_sphead; /* (l) List of seqpacket sockets. */ 121 122 struct unp_defer { 123 SLIST_ENTRY(unp_defer) ud_link; 124 struct file *ud_fp; 125 }; 126 static SLIST_HEAD(, unp_defer) unp_defers; 127 static int unp_defers_count; 128 129 static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 130 131 /* 132 * Garbage collection of cyclic file descriptor/socket references occurs 133 * asynchronously in a taskqueue context in order to avoid recursion and 134 * reentrance in the UNIX domain socket, file descriptor, and socket layer 135 * code. See unp_gc() for a full description. 136 */ 137 static struct timeout_task unp_gc_task; 138 139 /* 140 * The close of unix domain sockets attached as SCM_RIGHTS is 141 * postponed to the taskqueue, to avoid arbitrary recursion depth. 142 * The attached sockets might have another sockets attached. 143 */ 144 static struct task unp_defer_task; 145 146 /* 147 * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 148 * stream sockets, although the total for sender and receiver is actually 149 * only PIPSIZ. 150 * 151 * Datagram sockets really use the sendspace as the maximum datagram size, 152 * and don't really want to reserve the sendspace. Their recvspace should be 153 * large enough for at least one max-size datagram plus address. 154 */ 155 #ifndef PIPSIZ 156 #define PIPSIZ 8192 157 #endif 158 static u_long unpst_sendspace = PIPSIZ; 159 static u_long unpst_recvspace = PIPSIZ; 160 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 161 static u_long unpdg_recvspace = 4*1024; 162 static u_long unpsp_sendspace = PIPSIZ; /* really max datagram size */ 163 static u_long unpsp_recvspace = PIPSIZ; 164 165 static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain"); 166 static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0, 167 "SOCK_STREAM"); 168 static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM"); 169 static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, CTLFLAG_RW, 0, 170 "SOCK_SEQPACKET"); 171 172 SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 173 &unpst_sendspace, 0, "Default stream send space."); 174 SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 175 &unpst_recvspace, 0, "Default stream receive space."); 176 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 177 &unpdg_sendspace, 0, "Default datagram send space."); 178 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 179 &unpdg_recvspace, 0, "Default datagram receive space."); 180 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW, 181 &unpsp_sendspace, 0, "Default seqpacket send space."); 182 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW, 183 &unpsp_recvspace, 0, "Default seqpacket receive space."); 184 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, 185 "File descriptors in flight."); 186 SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD, 187 &unp_defers_count, 0, 188 "File descriptors deferred to taskqueue for close."); 189 190 /* 191 * Locking and synchronization: 192 * 193 * Three types of locks exit in the local domain socket implementation: a 194 * global list mutex, a global linkage rwlock, and per-unpcb mutexes. Of the 195 * global locks, the list lock protects the socket count, global generation 196 * number, and stream/datagram global lists. The linkage lock protects the 197 * interconnection of unpcbs, the v_socket and unp_vnode pointers, and can be 198 * held exclusively over the acquisition of multiple unpcb locks to prevent 199 * deadlock. 200 * 201 * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer, 202 * allocated in pru_attach() and freed in pru_detach(). The validity of that 203 * pointer is an invariant, so no lock is required to dereference the so_pcb 204 * pointer if a valid socket reference is held by the caller. In practice, 205 * this is always true during operations performed on a socket. Each unpcb 206 * has a back-pointer to its socket, unp_socket, which will be stable under 207 * the same circumstances. 208 * 209 * This pointer may only be safely dereferenced as long as a valid reference 210 * to the unpcb is held. Typically, this reference will be from the socket, 211 * or from another unpcb when the referring unpcb's lock is held (in order 212 * that the reference not be invalidated during use). For example, to follow 213 * unp->unp_conn->unp_socket, you need unlock the lock on unp, not unp_conn, 214 * as unp_socket remains valid as long as the reference to unp_conn is valid. 215 * 216 * Fields of unpcbss are locked using a per-unpcb lock, unp_mtx. Individual 217 * atomic reads without the lock may be performed "lockless", but more 218 * complex reads and read-modify-writes require the mutex to be held. No 219 * lock order is defined between unpcb locks -- multiple unpcb locks may be 220 * acquired at the same time only when holding the linkage rwlock 221 * exclusively, which prevents deadlocks. 222 * 223 * Blocking with UNIX domain sockets is a tricky issue: unlike most network 224 * protocols, bind() is a non-atomic operation, and connect() requires 225 * potential sleeping in the protocol, due to potentially waiting on local or 226 * distributed file systems. We try to separate "lookup" operations, which 227 * may sleep, and the IPC operations themselves, which typically can occur 228 * with relative atomicity as locks can be held over the entire operation. 229 * 230 * Another tricky issue is simultaneous multi-threaded or multi-process 231 * access to a single UNIX domain socket. These are handled by the flags 232 * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or 233 * binding, both of which involve dropping UNIX domain socket locks in order 234 * to perform namei() and other file system operations. 235 */ 236 static struct rwlock unp_link_rwlock; 237 static struct mtx unp_list_lock; 238 static struct mtx unp_defers_lock; 239 240 #define UNP_LINK_LOCK_INIT() rw_init(&unp_link_rwlock, \ 241 "unp_link_rwlock") 242 243 #define UNP_LINK_LOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 244 RA_LOCKED) 245 #define UNP_LINK_UNLOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 246 RA_UNLOCKED) 247 248 #define UNP_LINK_RLOCK() rw_rlock(&unp_link_rwlock) 249 #define UNP_LINK_RUNLOCK() rw_runlock(&unp_link_rwlock) 250 #define UNP_LINK_WLOCK() rw_wlock(&unp_link_rwlock) 251 #define UNP_LINK_WUNLOCK() rw_wunlock(&unp_link_rwlock) 252 #define UNP_LINK_WLOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 253 RA_WLOCKED) 254 255 #define UNP_LIST_LOCK_INIT() mtx_init(&unp_list_lock, \ 256 "unp_list_lock", NULL, MTX_DEF) 257 #define UNP_LIST_LOCK() mtx_lock(&unp_list_lock) 258 #define UNP_LIST_UNLOCK() mtx_unlock(&unp_list_lock) 259 260 #define UNP_DEFERRED_LOCK_INIT() mtx_init(&unp_defers_lock, \ 261 "unp_defer", NULL, MTX_DEF) 262 #define UNP_DEFERRED_LOCK() mtx_lock(&unp_defers_lock) 263 #define UNP_DEFERRED_UNLOCK() mtx_unlock(&unp_defers_lock) 264 265 #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \ 266 "unp_mtx", "unp_mtx", \ 267 MTX_DUPOK|MTX_DEF|MTX_RECURSE) 268 #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx) 269 #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx) 270 #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx) 271 #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED) 272 273 static int uipc_connect2(struct socket *, struct socket *); 274 static int uipc_ctloutput(struct socket *, struct sockopt *); 275 static int unp_connect(struct socket *, struct sockaddr *, 276 struct thread *); 277 static int unp_connectat(int, struct socket *, struct sockaddr *, 278 struct thread *); 279 static int unp_connect2(struct socket *so, struct socket *so2, int); 280 static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); 281 static void unp_dispose(struct mbuf *); 282 static void unp_shutdown(struct unpcb *); 283 static void unp_drop(struct unpcb *, int); 284 static void unp_gc(__unused void *, int); 285 static void unp_scan(struct mbuf *, void (*)(struct filedescent **, int)); 286 static void unp_discard(struct file *); 287 static void unp_freerights(struct filedescent **, int); 288 static void unp_init(void); 289 static int unp_internalize(struct mbuf **, struct thread *); 290 static void unp_internalize_fp(struct file *); 291 static int unp_externalize(struct mbuf *, struct mbuf **, int); 292 static int unp_externalize_fp(struct file *); 293 static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 294 static void unp_process_defers(void * __unused, int); 295 296 /* 297 * Definitions of protocols supported in the LOCAL domain. 298 */ 299 static struct domain localdomain; 300 static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream; 301 static struct pr_usrreqs uipc_usrreqs_seqpacket; 302 static struct protosw localsw[] = { 303 { 304 .pr_type = SOCK_STREAM, 305 .pr_domain = &localdomain, 306 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS, 307 .pr_ctloutput = &uipc_ctloutput, 308 .pr_usrreqs = &uipc_usrreqs_stream 309 }, 310 { 311 .pr_type = SOCK_DGRAM, 312 .pr_domain = &localdomain, 313 .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS, 314 .pr_ctloutput = &uipc_ctloutput, 315 .pr_usrreqs = &uipc_usrreqs_dgram 316 }, 317 { 318 .pr_type = SOCK_SEQPACKET, 319 .pr_domain = &localdomain, 320 321 /* 322 * XXXRW: For now, PR_ADDR because soreceive will bump into them 323 * due to our use of sbappendaddr. A new sbappend variants is needed 324 * that supports both atomic record writes and control data. 325 */ 326 .pr_flags = PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD| 327 PR_RIGHTS, 328 .pr_usrreqs = &uipc_usrreqs_seqpacket, 329 }, 330 }; 331 332 static struct domain localdomain = { 333 .dom_family = AF_LOCAL, 334 .dom_name = "local", 335 .dom_init = unp_init, 336 .dom_externalize = unp_externalize, 337 .dom_dispose = unp_dispose, 338 .dom_protosw = localsw, 339 .dom_protoswNPROTOSW = &localsw[sizeof(localsw)/sizeof(localsw[0])] 340 }; 341 DOMAIN_SET(local); 342 343 static void 344 uipc_abort(struct socket *so) 345 { 346 struct unpcb *unp, *unp2; 347 348 unp = sotounpcb(so); 349 KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 350 351 UNP_LINK_WLOCK(); 352 UNP_PCB_LOCK(unp); 353 unp2 = unp->unp_conn; 354 if (unp2 != NULL) { 355 UNP_PCB_LOCK(unp2); 356 unp_drop(unp2, ECONNABORTED); 357 UNP_PCB_UNLOCK(unp2); 358 } 359 UNP_PCB_UNLOCK(unp); 360 UNP_LINK_WUNLOCK(); 361 } 362 363 static int 364 uipc_accept(struct socket *so, struct sockaddr **nam) 365 { 366 struct unpcb *unp, *unp2; 367 const struct sockaddr *sa; 368 369 /* 370 * Pass back name of connected socket, if it was bound and we are 371 * still connected (our peer may have closed already!). 372 */ 373 unp = sotounpcb(so); 374 KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 375 376 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 377 UNP_LINK_RLOCK(); 378 unp2 = unp->unp_conn; 379 if (unp2 != NULL && unp2->unp_addr != NULL) { 380 UNP_PCB_LOCK(unp2); 381 sa = (struct sockaddr *) unp2->unp_addr; 382 bcopy(sa, *nam, sa->sa_len); 383 UNP_PCB_UNLOCK(unp2); 384 } else { 385 sa = &sun_noname; 386 bcopy(sa, *nam, sa->sa_len); 387 } 388 UNP_LINK_RUNLOCK(); 389 return (0); 390 } 391 392 static int 393 uipc_attach(struct socket *so, int proto, struct thread *td) 394 { 395 u_long sendspace, recvspace; 396 struct unpcb *unp; 397 int error; 398 399 KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 400 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 401 switch (so->so_type) { 402 case SOCK_STREAM: 403 sendspace = unpst_sendspace; 404 recvspace = unpst_recvspace; 405 break; 406 407 case SOCK_DGRAM: 408 sendspace = unpdg_sendspace; 409 recvspace = unpdg_recvspace; 410 break; 411 412 case SOCK_SEQPACKET: 413 sendspace = unpsp_sendspace; 414 recvspace = unpsp_recvspace; 415 break; 416 417 default: 418 panic("uipc_attach"); 419 } 420 error = soreserve(so, sendspace, recvspace); 421 if (error) 422 return (error); 423 } 424 unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO); 425 if (unp == NULL) 426 return (ENOBUFS); 427 LIST_INIT(&unp->unp_refs); 428 UNP_PCB_LOCK_INIT(unp); 429 unp->unp_socket = so; 430 so->so_pcb = unp; 431 unp->unp_refcount = 1; 432 433 UNP_LIST_LOCK(); 434 unp->unp_gencnt = ++unp_gencnt; 435 unp_count++; 436 switch (so->so_type) { 437 case SOCK_STREAM: 438 LIST_INSERT_HEAD(&unp_shead, unp, unp_link); 439 break; 440 441 case SOCK_DGRAM: 442 LIST_INSERT_HEAD(&unp_dhead, unp, unp_link); 443 break; 444 445 case SOCK_SEQPACKET: 446 LIST_INSERT_HEAD(&unp_sphead, unp, unp_link); 447 break; 448 449 default: 450 panic("uipc_attach"); 451 } 452 UNP_LIST_UNLOCK(); 453 454 return (0); 455 } 456 457 static int 458 uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td) 459 { 460 struct sockaddr_un *soun = (struct sockaddr_un *)nam; 461 struct vattr vattr; 462 int error, namelen; 463 struct nameidata nd; 464 struct unpcb *unp; 465 struct vnode *vp; 466 struct mount *mp; 467 char *buf; 468 469 unp = sotounpcb(so); 470 KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 471 472 if (soun->sun_len > sizeof(struct sockaddr_un)) 473 return (EINVAL); 474 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 475 if (namelen <= 0) 476 return (EINVAL); 477 478 /* 479 * We don't allow simultaneous bind() calls on a single UNIX domain 480 * socket, so flag in-progress operations, and return an error if an 481 * operation is already in progress. 482 * 483 * Historically, we have not allowed a socket to be rebound, so this 484 * also returns an error. Not allowing re-binding simplifies the 485 * implementation and avoids a great many possible failure modes. 486 */ 487 UNP_PCB_LOCK(unp); 488 if (unp->unp_vnode != NULL) { 489 UNP_PCB_UNLOCK(unp); 490 return (EINVAL); 491 } 492 if (unp->unp_flags & UNP_BINDING) { 493 UNP_PCB_UNLOCK(unp); 494 return (EALREADY); 495 } 496 unp->unp_flags |= UNP_BINDING; 497 UNP_PCB_UNLOCK(unp); 498 499 buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 500 bcopy(soun->sun_path, buf, namelen); 501 buf[namelen] = 0; 502 503 restart: 504 NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, 505 UIO_SYSSPACE, buf, fd, CAP_BINDAT, td); 506 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 507 error = namei(&nd); 508 if (error) 509 goto error; 510 vp = nd.ni_vp; 511 if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 512 NDFREE(&nd, NDF_ONLY_PNBUF); 513 if (nd.ni_dvp == vp) 514 vrele(nd.ni_dvp); 515 else 516 vput(nd.ni_dvp); 517 if (vp != NULL) { 518 vrele(vp); 519 error = EADDRINUSE; 520 goto error; 521 } 522 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 523 if (error) 524 goto error; 525 goto restart; 526 } 527 VATTR_NULL(&vattr); 528 vattr.va_type = VSOCK; 529 vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 530 #ifdef MAC 531 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 532 &vattr); 533 #endif 534 if (error == 0) 535 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 536 NDFREE(&nd, NDF_ONLY_PNBUF); 537 vput(nd.ni_dvp); 538 if (error) { 539 vn_finished_write(mp); 540 goto error; 541 } 542 vp = nd.ni_vp; 543 ASSERT_VOP_ELOCKED(vp, "uipc_bind"); 544 soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 545 546 UNP_LINK_WLOCK(); 547 UNP_PCB_LOCK(unp); 548 VOP_UNP_BIND(vp, unp->unp_socket); 549 unp->unp_vnode = vp; 550 unp->unp_addr = soun; 551 unp->unp_flags &= ~UNP_BINDING; 552 UNP_PCB_UNLOCK(unp); 553 UNP_LINK_WUNLOCK(); 554 VOP_UNLOCK(vp, 0); 555 vn_finished_write(mp); 556 free(buf, M_TEMP); 557 return (0); 558 559 error: 560 UNP_PCB_LOCK(unp); 561 unp->unp_flags &= ~UNP_BINDING; 562 UNP_PCB_UNLOCK(unp); 563 free(buf, M_TEMP); 564 return (error); 565 } 566 567 static int 568 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 569 { 570 571 return (uipc_bindat(AT_FDCWD, so, nam, td)); 572 } 573 574 static int 575 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 576 { 577 int error; 578 579 KASSERT(td == curthread, ("uipc_connect: td != curthread")); 580 UNP_LINK_WLOCK(); 581 error = unp_connect(so, nam, td); 582 UNP_LINK_WUNLOCK(); 583 return (error); 584 } 585 586 static int 587 uipc_connectat(int fd, struct socket *so, struct sockaddr *nam, 588 struct thread *td) 589 { 590 int error; 591 592 KASSERT(td == curthread, ("uipc_connectat: td != curthread")); 593 UNP_LINK_WLOCK(); 594 error = unp_connectat(fd, so, nam, td); 595 UNP_LINK_WUNLOCK(); 596 return (error); 597 } 598 599 static void 600 uipc_close(struct socket *so) 601 { 602 struct unpcb *unp, *unp2; 603 604 unp = sotounpcb(so); 605 KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 606 607 UNP_LINK_WLOCK(); 608 UNP_PCB_LOCK(unp); 609 unp2 = unp->unp_conn; 610 if (unp2 != NULL) { 611 UNP_PCB_LOCK(unp2); 612 unp_disconnect(unp, unp2); 613 UNP_PCB_UNLOCK(unp2); 614 } 615 UNP_PCB_UNLOCK(unp); 616 UNP_LINK_WUNLOCK(); 617 } 618 619 static int 620 uipc_connect2(struct socket *so1, struct socket *so2) 621 { 622 struct unpcb *unp, *unp2; 623 int error; 624 625 UNP_LINK_WLOCK(); 626 unp = so1->so_pcb; 627 KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 628 UNP_PCB_LOCK(unp); 629 unp2 = so2->so_pcb; 630 KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL")); 631 UNP_PCB_LOCK(unp2); 632 error = unp_connect2(so1, so2, PRU_CONNECT2); 633 UNP_PCB_UNLOCK(unp2); 634 UNP_PCB_UNLOCK(unp); 635 UNP_LINK_WUNLOCK(); 636 return (error); 637 } 638 639 static void 640 uipc_detach(struct socket *so) 641 { 642 struct unpcb *unp, *unp2; 643 struct sockaddr_un *saved_unp_addr; 644 struct vnode *vp; 645 int freeunp, local_unp_rights; 646 647 unp = sotounpcb(so); 648 KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 649 650 UNP_LINK_WLOCK(); 651 UNP_LIST_LOCK(); 652 UNP_PCB_LOCK(unp); 653 LIST_REMOVE(unp, unp_link); 654 unp->unp_gencnt = ++unp_gencnt; 655 --unp_count; 656 UNP_LIST_UNLOCK(); 657 658 /* 659 * XXXRW: Should assert vp->v_socket == so. 660 */ 661 if ((vp = unp->unp_vnode) != NULL) { 662 VOP_UNP_DETACH(vp); 663 unp->unp_vnode = NULL; 664 } 665 unp2 = unp->unp_conn; 666 if (unp2 != NULL) { 667 UNP_PCB_LOCK(unp2); 668 unp_disconnect(unp, unp2); 669 UNP_PCB_UNLOCK(unp2); 670 } 671 672 /* 673 * We hold the linkage lock exclusively, so it's OK to acquire 674 * multiple pcb locks at a time. 675 */ 676 while (!LIST_EMPTY(&unp->unp_refs)) { 677 struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 678 679 UNP_PCB_LOCK(ref); 680 unp_drop(ref, ECONNRESET); 681 UNP_PCB_UNLOCK(ref); 682 } 683 local_unp_rights = unp_rights; 684 UNP_LINK_WUNLOCK(); 685 unp->unp_socket->so_pcb = NULL; 686 saved_unp_addr = unp->unp_addr; 687 unp->unp_addr = NULL; 688 unp->unp_refcount--; 689 freeunp = (unp->unp_refcount == 0); 690 if (saved_unp_addr != NULL) 691 free(saved_unp_addr, M_SONAME); 692 if (freeunp) { 693 UNP_PCB_LOCK_DESTROY(unp); 694 uma_zfree(unp_zone, unp); 695 } else 696 UNP_PCB_UNLOCK(unp); 697 if (vp) 698 vrele(vp); 699 if (local_unp_rights) 700 taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1); 701 } 702 703 static int 704 uipc_disconnect(struct socket *so) 705 { 706 struct unpcb *unp, *unp2; 707 708 unp = sotounpcb(so); 709 KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 710 711 UNP_LINK_WLOCK(); 712 UNP_PCB_LOCK(unp); 713 unp2 = unp->unp_conn; 714 if (unp2 != NULL) { 715 UNP_PCB_LOCK(unp2); 716 unp_disconnect(unp, unp2); 717 UNP_PCB_UNLOCK(unp2); 718 } 719 UNP_PCB_UNLOCK(unp); 720 UNP_LINK_WUNLOCK(); 721 return (0); 722 } 723 724 static int 725 uipc_listen(struct socket *so, int backlog, struct thread *td) 726 { 727 struct unpcb *unp; 728 int error; 729 730 unp = sotounpcb(so); 731 KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 732 733 UNP_PCB_LOCK(unp); 734 if (unp->unp_vnode == NULL) { 735 UNP_PCB_UNLOCK(unp); 736 return (EINVAL); 737 } 738 739 SOCK_LOCK(so); 740 error = solisten_proto_check(so); 741 if (error == 0) { 742 cru2x(td->td_ucred, &unp->unp_peercred); 743 unp->unp_flags |= UNP_HAVEPCCACHED; 744 solisten_proto(so, backlog); 745 } 746 SOCK_UNLOCK(so); 747 UNP_PCB_UNLOCK(unp); 748 return (error); 749 } 750 751 static int 752 uipc_peeraddr(struct socket *so, struct sockaddr **nam) 753 { 754 struct unpcb *unp, *unp2; 755 const struct sockaddr *sa; 756 757 unp = sotounpcb(so); 758 KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 759 760 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 761 UNP_LINK_RLOCK(); 762 /* 763 * XXX: It seems that this test always fails even when connection is 764 * established. So, this else clause is added as workaround to 765 * return PF_LOCAL sockaddr. 766 */ 767 unp2 = unp->unp_conn; 768 if (unp2 != NULL) { 769 UNP_PCB_LOCK(unp2); 770 if (unp2->unp_addr != NULL) 771 sa = (struct sockaddr *) unp2->unp_addr; 772 else 773 sa = &sun_noname; 774 bcopy(sa, *nam, sa->sa_len); 775 UNP_PCB_UNLOCK(unp2); 776 } else { 777 sa = &sun_noname; 778 bcopy(sa, *nam, sa->sa_len); 779 } 780 UNP_LINK_RUNLOCK(); 781 return (0); 782 } 783 784 static int 785 uipc_rcvd(struct socket *so, int flags) 786 { 787 struct unpcb *unp, *unp2; 788 struct socket *so2; 789 u_int mbcnt, sbcc; 790 u_long newhiwat; 791 792 unp = sotounpcb(so); 793 KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL")); 794 795 if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET) 796 panic("uipc_rcvd socktype %d", so->so_type); 797 798 /* 799 * Adjust backpressure on sender and wakeup any waiting to write. 800 * 801 * The unp lock is acquired to maintain the validity of the unp_conn 802 * pointer; no lock on unp2 is required as unp2->unp_socket will be 803 * static as long as we don't permit unp2 to disconnect from unp, 804 * which is prevented by the lock on unp. We cache values from 805 * so_rcv to avoid holding the so_rcv lock over the entire 806 * transaction on the remote so_snd. 807 */ 808 SOCKBUF_LOCK(&so->so_rcv); 809 mbcnt = so->so_rcv.sb_mbcnt; 810 sbcc = so->so_rcv.sb_cc; 811 SOCKBUF_UNLOCK(&so->so_rcv); 812 UNP_PCB_LOCK(unp); 813 unp2 = unp->unp_conn; 814 if (unp2 == NULL) { 815 UNP_PCB_UNLOCK(unp); 816 return (0); 817 } 818 so2 = unp2->unp_socket; 819 SOCKBUF_LOCK(&so2->so_snd); 820 so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt; 821 newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc; 822 (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 823 newhiwat, RLIM_INFINITY); 824 sowwakeup_locked(so2); 825 unp->unp_mbcnt = mbcnt; 826 unp->unp_cc = sbcc; 827 UNP_PCB_UNLOCK(unp); 828 return (0); 829 } 830 831 static int 832 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 833 struct mbuf *control, struct thread *td) 834 { 835 struct unpcb *unp, *unp2; 836 struct socket *so2; 837 u_int mbcnt_delta, sbcc; 838 u_int newhiwat; 839 int error = 0; 840 841 unp = sotounpcb(so); 842 KASSERT(unp != NULL, ("uipc_send: unp == NULL")); 843 844 if (flags & PRUS_OOB) { 845 error = EOPNOTSUPP; 846 goto release; 847 } 848 if (control != NULL && (error = unp_internalize(&control, td))) 849 goto release; 850 if ((nam != NULL) || (flags & PRUS_EOF)) 851 UNP_LINK_WLOCK(); 852 else 853 UNP_LINK_RLOCK(); 854 switch (so->so_type) { 855 case SOCK_DGRAM: 856 { 857 const struct sockaddr *from; 858 859 unp2 = unp->unp_conn; 860 if (nam != NULL) { 861 UNP_LINK_WLOCK_ASSERT(); 862 if (unp2 != NULL) { 863 error = EISCONN; 864 break; 865 } 866 error = unp_connect(so, nam, td); 867 if (error) 868 break; 869 unp2 = unp->unp_conn; 870 } 871 872 /* 873 * Because connect() and send() are non-atomic in a sendto() 874 * with a target address, it's possible that the socket will 875 * have disconnected before the send() can run. In that case 876 * return the slightly counter-intuitive but otherwise 877 * correct error that the socket is not connected. 878 */ 879 if (unp2 == NULL) { 880 error = ENOTCONN; 881 break; 882 } 883 /* Lockless read. */ 884 if (unp2->unp_flags & UNP_WANTCRED) 885 control = unp_addsockcred(td, control); 886 UNP_PCB_LOCK(unp); 887 if (unp->unp_addr != NULL) 888 from = (struct sockaddr *)unp->unp_addr; 889 else 890 from = &sun_noname; 891 so2 = unp2->unp_socket; 892 SOCKBUF_LOCK(&so2->so_rcv); 893 if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 894 sorwakeup_locked(so2); 895 m = NULL; 896 control = NULL; 897 } else { 898 SOCKBUF_UNLOCK(&so2->so_rcv); 899 error = ENOBUFS; 900 } 901 if (nam != NULL) { 902 UNP_LINK_WLOCK_ASSERT(); 903 UNP_PCB_LOCK(unp2); 904 unp_disconnect(unp, unp2); 905 UNP_PCB_UNLOCK(unp2); 906 } 907 UNP_PCB_UNLOCK(unp); 908 break; 909 } 910 911 case SOCK_SEQPACKET: 912 case SOCK_STREAM: 913 if ((so->so_state & SS_ISCONNECTED) == 0) { 914 if (nam != NULL) { 915 UNP_LINK_WLOCK_ASSERT(); 916 error = unp_connect(so, nam, td); 917 if (error) 918 break; /* XXX */ 919 } else { 920 error = ENOTCONN; 921 break; 922 } 923 } 924 925 /* Lockless read. */ 926 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 927 error = EPIPE; 928 break; 929 } 930 931 /* 932 * Because connect() and send() are non-atomic in a sendto() 933 * with a target address, it's possible that the socket will 934 * have disconnected before the send() can run. In that case 935 * return the slightly counter-intuitive but otherwise 936 * correct error that the socket is not connected. 937 * 938 * Locking here must be done carefully: the linkage lock 939 * prevents interconnections between unpcbs from changing, so 940 * we can traverse from unp to unp2 without acquiring unp's 941 * lock. Socket buffer locks follow unpcb locks, so we can 942 * acquire both remote and lock socket buffer locks. 943 */ 944 unp2 = unp->unp_conn; 945 if (unp2 == NULL) { 946 error = ENOTCONN; 947 break; 948 } 949 so2 = unp2->unp_socket; 950 UNP_PCB_LOCK(unp2); 951 SOCKBUF_LOCK(&so2->so_rcv); 952 if (unp2->unp_flags & UNP_WANTCRED) { 953 /* 954 * Credentials are passed only once on SOCK_STREAM 955 * and SOCK_SEQPACKET. 956 */ 957 unp2->unp_flags &= ~UNP_WANTCRED; 958 control = unp_addsockcred(td, control); 959 } 960 /* 961 * Send to paired receive port, and then reduce send buffer 962 * hiwater marks to maintain backpressure. Wake up readers. 963 */ 964 switch (so->so_type) { 965 case SOCK_STREAM: 966 if (control != NULL) { 967 if (sbappendcontrol_locked(&so2->so_rcv, m, 968 control)) 969 control = NULL; 970 } else 971 sbappend_locked(&so2->so_rcv, m); 972 break; 973 974 case SOCK_SEQPACKET: { 975 const struct sockaddr *from; 976 977 from = &sun_noname; 978 if (sbappendaddr_locked(&so2->so_rcv, from, m, 979 control)) 980 control = NULL; 981 break; 982 } 983 } 984 985 /* 986 * XXXRW: While fine for SOCK_STREAM, this conflates maximum 987 * datagram size and back-pressure for SOCK_SEQPACKET, which 988 * can lead to undesired return of EMSGSIZE on send instead 989 * of more desirable blocking. 990 */ 991 mbcnt_delta = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt; 992 unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt; 993 sbcc = so2->so_rcv.sb_cc; 994 sorwakeup_locked(so2); 995 996 SOCKBUF_LOCK(&so->so_snd); 997 if ((int)so->so_snd.sb_hiwat >= (int)(sbcc - unp2->unp_cc)) 998 newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc); 999 else 1000 newhiwat = 0; 1001 (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 1002 newhiwat, RLIM_INFINITY); 1003 so->so_snd.sb_mbmax -= mbcnt_delta; 1004 SOCKBUF_UNLOCK(&so->so_snd); 1005 unp2->unp_cc = sbcc; 1006 UNP_PCB_UNLOCK(unp2); 1007 m = NULL; 1008 break; 1009 1010 default: 1011 panic("uipc_send unknown socktype"); 1012 } 1013 1014 /* 1015 * PRUS_EOF is equivalent to pru_send followed by pru_shutdown. 1016 */ 1017 if (flags & PRUS_EOF) { 1018 UNP_PCB_LOCK(unp); 1019 socantsendmore(so); 1020 unp_shutdown(unp); 1021 UNP_PCB_UNLOCK(unp); 1022 } 1023 1024 if ((nam != NULL) || (flags & PRUS_EOF)) 1025 UNP_LINK_WUNLOCK(); 1026 else 1027 UNP_LINK_RUNLOCK(); 1028 1029 if (control != NULL && error != 0) 1030 unp_dispose(control); 1031 1032 release: 1033 if (control != NULL) 1034 m_freem(control); 1035 if (m != NULL) 1036 m_freem(m); 1037 return (error); 1038 } 1039 1040 static int 1041 uipc_sense(struct socket *so, struct stat *sb) 1042 { 1043 struct unpcb *unp, *unp2; 1044 struct socket *so2; 1045 1046 unp = sotounpcb(so); 1047 KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 1048 1049 sb->st_blksize = so->so_snd.sb_hiwat; 1050 UNP_LINK_RLOCK(); 1051 UNP_PCB_LOCK(unp); 1052 unp2 = unp->unp_conn; 1053 if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) && 1054 unp2 != NULL) { 1055 so2 = unp2->unp_socket; 1056 sb->st_blksize += so2->so_rcv.sb_cc; 1057 } 1058 sb->st_dev = NODEV; 1059 if (unp->unp_ino == 0) 1060 unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 1061 sb->st_ino = unp->unp_ino; 1062 UNP_PCB_UNLOCK(unp); 1063 UNP_LINK_RUNLOCK(); 1064 return (0); 1065 } 1066 1067 static int 1068 uipc_shutdown(struct socket *so) 1069 { 1070 struct unpcb *unp; 1071 1072 unp = sotounpcb(so); 1073 KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 1074 1075 UNP_LINK_WLOCK(); 1076 UNP_PCB_LOCK(unp); 1077 socantsendmore(so); 1078 unp_shutdown(unp); 1079 UNP_PCB_UNLOCK(unp); 1080 UNP_LINK_WUNLOCK(); 1081 return (0); 1082 } 1083 1084 static int 1085 uipc_sockaddr(struct socket *so, struct sockaddr **nam) 1086 { 1087 struct unpcb *unp; 1088 const struct sockaddr *sa; 1089 1090 unp = sotounpcb(so); 1091 KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 1092 1093 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1094 UNP_PCB_LOCK(unp); 1095 if (unp->unp_addr != NULL) 1096 sa = (struct sockaddr *) unp->unp_addr; 1097 else 1098 sa = &sun_noname; 1099 bcopy(sa, *nam, sa->sa_len); 1100 UNP_PCB_UNLOCK(unp); 1101 return (0); 1102 } 1103 1104 static struct pr_usrreqs uipc_usrreqs_dgram = { 1105 .pru_abort = uipc_abort, 1106 .pru_accept = uipc_accept, 1107 .pru_attach = uipc_attach, 1108 .pru_bind = uipc_bind, 1109 .pru_bindat = uipc_bindat, 1110 .pru_connect = uipc_connect, 1111 .pru_connectat = uipc_connectat, 1112 .pru_connect2 = uipc_connect2, 1113 .pru_detach = uipc_detach, 1114 .pru_disconnect = uipc_disconnect, 1115 .pru_listen = uipc_listen, 1116 .pru_peeraddr = uipc_peeraddr, 1117 .pru_rcvd = uipc_rcvd, 1118 .pru_send = uipc_send, 1119 .pru_sense = uipc_sense, 1120 .pru_shutdown = uipc_shutdown, 1121 .pru_sockaddr = uipc_sockaddr, 1122 .pru_soreceive = soreceive_dgram, 1123 .pru_close = uipc_close, 1124 }; 1125 1126 static struct pr_usrreqs uipc_usrreqs_seqpacket = { 1127 .pru_abort = uipc_abort, 1128 .pru_accept = uipc_accept, 1129 .pru_attach = uipc_attach, 1130 .pru_bind = uipc_bind, 1131 .pru_bindat = uipc_bindat, 1132 .pru_connect = uipc_connect, 1133 .pru_connectat = uipc_connectat, 1134 .pru_connect2 = uipc_connect2, 1135 .pru_detach = uipc_detach, 1136 .pru_disconnect = uipc_disconnect, 1137 .pru_listen = uipc_listen, 1138 .pru_peeraddr = uipc_peeraddr, 1139 .pru_rcvd = uipc_rcvd, 1140 .pru_send = uipc_send, 1141 .pru_sense = uipc_sense, 1142 .pru_shutdown = uipc_shutdown, 1143 .pru_sockaddr = uipc_sockaddr, 1144 .pru_soreceive = soreceive_generic, /* XXX: or...? */ 1145 .pru_close = uipc_close, 1146 }; 1147 1148 static struct pr_usrreqs uipc_usrreqs_stream = { 1149 .pru_abort = uipc_abort, 1150 .pru_accept = uipc_accept, 1151 .pru_attach = uipc_attach, 1152 .pru_bind = uipc_bind, 1153 .pru_bindat = uipc_bindat, 1154 .pru_connect = uipc_connect, 1155 .pru_connectat = uipc_connectat, 1156 .pru_connect2 = uipc_connect2, 1157 .pru_detach = uipc_detach, 1158 .pru_disconnect = uipc_disconnect, 1159 .pru_listen = uipc_listen, 1160 .pru_peeraddr = uipc_peeraddr, 1161 .pru_rcvd = uipc_rcvd, 1162 .pru_send = uipc_send, 1163 .pru_sense = uipc_sense, 1164 .pru_shutdown = uipc_shutdown, 1165 .pru_sockaddr = uipc_sockaddr, 1166 .pru_soreceive = soreceive_generic, 1167 .pru_close = uipc_close, 1168 }; 1169 1170 static int 1171 uipc_ctloutput(struct socket *so, struct sockopt *sopt) 1172 { 1173 struct unpcb *unp; 1174 struct xucred xu; 1175 int error, optval; 1176 1177 if (sopt->sopt_level != 0) 1178 return (EINVAL); 1179 1180 unp = sotounpcb(so); 1181 KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 1182 error = 0; 1183 switch (sopt->sopt_dir) { 1184 case SOPT_GET: 1185 switch (sopt->sopt_name) { 1186 case LOCAL_PEERCRED: 1187 UNP_PCB_LOCK(unp); 1188 if (unp->unp_flags & UNP_HAVEPC) 1189 xu = unp->unp_peercred; 1190 else { 1191 if (so->so_type == SOCK_STREAM) 1192 error = ENOTCONN; 1193 else 1194 error = EINVAL; 1195 } 1196 UNP_PCB_UNLOCK(unp); 1197 if (error == 0) 1198 error = sooptcopyout(sopt, &xu, sizeof(xu)); 1199 break; 1200 1201 case LOCAL_CREDS: 1202 /* Unlocked read. */ 1203 optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 1204 error = sooptcopyout(sopt, &optval, sizeof(optval)); 1205 break; 1206 1207 case LOCAL_CONNWAIT: 1208 /* Unlocked read. */ 1209 optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 1210 error = sooptcopyout(sopt, &optval, sizeof(optval)); 1211 break; 1212 1213 default: 1214 error = EOPNOTSUPP; 1215 break; 1216 } 1217 break; 1218 1219 case SOPT_SET: 1220 switch (sopt->sopt_name) { 1221 case LOCAL_CREDS: 1222 case LOCAL_CONNWAIT: 1223 error = sooptcopyin(sopt, &optval, sizeof(optval), 1224 sizeof(optval)); 1225 if (error) 1226 break; 1227 1228 #define OPTSET(bit) do { \ 1229 UNP_PCB_LOCK(unp); \ 1230 if (optval) \ 1231 unp->unp_flags |= bit; \ 1232 else \ 1233 unp->unp_flags &= ~bit; \ 1234 UNP_PCB_UNLOCK(unp); \ 1235 } while (0) 1236 1237 switch (sopt->sopt_name) { 1238 case LOCAL_CREDS: 1239 OPTSET(UNP_WANTCRED); 1240 break; 1241 1242 case LOCAL_CONNWAIT: 1243 OPTSET(UNP_CONNWAIT); 1244 break; 1245 1246 default: 1247 break; 1248 } 1249 break; 1250 #undef OPTSET 1251 default: 1252 error = ENOPROTOOPT; 1253 break; 1254 } 1255 break; 1256 1257 default: 1258 error = EOPNOTSUPP; 1259 break; 1260 } 1261 return (error); 1262 } 1263 1264 static int 1265 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1266 { 1267 1268 return (unp_connectat(AT_FDCWD, so, nam, td)); 1269 } 1270 1271 static int 1272 unp_connectat(int fd, struct socket *so, struct sockaddr *nam, 1273 struct thread *td) 1274 { 1275 struct sockaddr_un *soun = (struct sockaddr_un *)nam; 1276 struct vnode *vp; 1277 struct socket *so2, *so3; 1278 struct unpcb *unp, *unp2, *unp3; 1279 int error, len; 1280 struct nameidata nd; 1281 char buf[SOCK_MAXADDRLEN]; 1282 struct sockaddr *sa; 1283 1284 UNP_LINK_WLOCK_ASSERT(); 1285 1286 unp = sotounpcb(so); 1287 KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1288 1289 if (nam->sa_len > sizeof(struct sockaddr_un)) 1290 return (EINVAL); 1291 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 1292 if (len <= 0) 1293 return (EINVAL); 1294 bcopy(soun->sun_path, buf, len); 1295 buf[len] = 0; 1296 1297 UNP_PCB_LOCK(unp); 1298 if (unp->unp_flags & UNP_CONNECTING) { 1299 UNP_PCB_UNLOCK(unp); 1300 return (EALREADY); 1301 } 1302 UNP_LINK_WUNLOCK(); 1303 unp->unp_flags |= UNP_CONNECTING; 1304 UNP_PCB_UNLOCK(unp); 1305 1306 sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1307 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 1308 UIO_SYSSPACE, buf, fd, CAP_CONNECTAT, td); 1309 error = namei(&nd); 1310 if (error) 1311 vp = NULL; 1312 else 1313 vp = nd.ni_vp; 1314 ASSERT_VOP_LOCKED(vp, "unp_connect"); 1315 NDFREE(&nd, NDF_ONLY_PNBUF); 1316 if (error) 1317 goto bad; 1318 1319 if (vp->v_type != VSOCK) { 1320 error = ENOTSOCK; 1321 goto bad; 1322 } 1323 #ifdef MAC 1324 error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD); 1325 if (error) 1326 goto bad; 1327 #endif 1328 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1329 if (error) 1330 goto bad; 1331 1332 unp = sotounpcb(so); 1333 KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1334 1335 /* 1336 * Lock linkage lock for two reasons: make sure v_socket is stable, 1337 * and to protect simultaneous locking of multiple pcbs. 1338 */ 1339 UNP_LINK_WLOCK(); 1340 VOP_UNP_CONNECT(vp, &so2); 1341 if (so2 == NULL) { 1342 error = ECONNREFUSED; 1343 goto bad2; 1344 } 1345 if (so->so_type != so2->so_type) { 1346 error = EPROTOTYPE; 1347 goto bad2; 1348 } 1349 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1350 if (so2->so_options & SO_ACCEPTCONN) { 1351 CURVNET_SET(so2->so_vnet); 1352 so3 = sonewconn(so2, 0); 1353 CURVNET_RESTORE(); 1354 } else 1355 so3 = NULL; 1356 if (so3 == NULL) { 1357 error = ECONNREFUSED; 1358 goto bad2; 1359 } 1360 unp = sotounpcb(so); 1361 unp2 = sotounpcb(so2); 1362 unp3 = sotounpcb(so3); 1363 UNP_PCB_LOCK(unp); 1364 UNP_PCB_LOCK(unp2); 1365 UNP_PCB_LOCK(unp3); 1366 if (unp2->unp_addr != NULL) { 1367 bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 1368 unp3->unp_addr = (struct sockaddr_un *) sa; 1369 sa = NULL; 1370 } 1371 1372 /* 1373 * The connector's (client's) credentials are copied from its 1374 * process structure at the time of connect() (which is now). 1375 */ 1376 cru2x(td->td_ucred, &unp3->unp_peercred); 1377 unp3->unp_flags |= UNP_HAVEPC; 1378 1379 /* 1380 * The receiver's (server's) credentials are copied from the 1381 * unp_peercred member of socket on which the former called 1382 * listen(); uipc_listen() cached that process's credentials 1383 * at that time so we can use them now. 1384 */ 1385 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 1386 ("unp_connect: listener without cached peercred")); 1387 memcpy(&unp->unp_peercred, &unp2->unp_peercred, 1388 sizeof(unp->unp_peercred)); 1389 unp->unp_flags |= UNP_HAVEPC; 1390 if (unp2->unp_flags & UNP_WANTCRED) 1391 unp3->unp_flags |= UNP_WANTCRED; 1392 UNP_PCB_UNLOCK(unp3); 1393 UNP_PCB_UNLOCK(unp2); 1394 UNP_PCB_UNLOCK(unp); 1395 #ifdef MAC 1396 mac_socketpeer_set_from_socket(so, so3); 1397 mac_socketpeer_set_from_socket(so3, so); 1398 #endif 1399 1400 so2 = so3; 1401 } 1402 unp = sotounpcb(so); 1403 KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1404 unp2 = sotounpcb(so2); 1405 KASSERT(unp2 != NULL, ("unp_connect: unp2 == NULL")); 1406 UNP_PCB_LOCK(unp); 1407 UNP_PCB_LOCK(unp2); 1408 error = unp_connect2(so, so2, PRU_CONNECT); 1409 UNP_PCB_UNLOCK(unp2); 1410 UNP_PCB_UNLOCK(unp); 1411 bad2: 1412 UNP_LINK_WUNLOCK(); 1413 bad: 1414 if (vp != NULL) 1415 vput(vp); 1416 free(sa, M_SONAME); 1417 UNP_LINK_WLOCK(); 1418 UNP_PCB_LOCK(unp); 1419 unp->unp_flags &= ~UNP_CONNECTING; 1420 UNP_PCB_UNLOCK(unp); 1421 return (error); 1422 } 1423 1424 static int 1425 unp_connect2(struct socket *so, struct socket *so2, int req) 1426 { 1427 struct unpcb *unp; 1428 struct unpcb *unp2; 1429 1430 unp = sotounpcb(so); 1431 KASSERT(unp != NULL, ("unp_connect2: unp == NULL")); 1432 unp2 = sotounpcb(so2); 1433 KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1434 1435 UNP_LINK_WLOCK_ASSERT(); 1436 UNP_PCB_LOCK_ASSERT(unp); 1437 UNP_PCB_LOCK_ASSERT(unp2); 1438 1439 if (so2->so_type != so->so_type) 1440 return (EPROTOTYPE); 1441 unp->unp_conn = unp2; 1442 1443 switch (so->so_type) { 1444 case SOCK_DGRAM: 1445 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 1446 soisconnected(so); 1447 break; 1448 1449 case SOCK_STREAM: 1450 case SOCK_SEQPACKET: 1451 unp2->unp_conn = unp; 1452 if (req == PRU_CONNECT && 1453 ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 1454 soisconnecting(so); 1455 else 1456 soisconnected(so); 1457 soisconnected(so2); 1458 break; 1459 1460 default: 1461 panic("unp_connect2"); 1462 } 1463 return (0); 1464 } 1465 1466 static void 1467 unp_disconnect(struct unpcb *unp, struct unpcb *unp2) 1468 { 1469 struct socket *so; 1470 1471 KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL")); 1472 1473 UNP_LINK_WLOCK_ASSERT(); 1474 UNP_PCB_LOCK_ASSERT(unp); 1475 UNP_PCB_LOCK_ASSERT(unp2); 1476 1477 unp->unp_conn = NULL; 1478 switch (unp->unp_socket->so_type) { 1479 case SOCK_DGRAM: 1480 LIST_REMOVE(unp, unp_reflink); 1481 so = unp->unp_socket; 1482 SOCK_LOCK(so); 1483 so->so_state &= ~SS_ISCONNECTED; 1484 SOCK_UNLOCK(so); 1485 break; 1486 1487 case SOCK_STREAM: 1488 case SOCK_SEQPACKET: 1489 soisdisconnected(unp->unp_socket); 1490 unp2->unp_conn = NULL; 1491 soisdisconnected(unp2->unp_socket); 1492 break; 1493 } 1494 } 1495 1496 /* 1497 * unp_pcblist() walks the global list of struct unpcb's to generate a 1498 * pointer list, bumping the refcount on each unpcb. It then copies them out 1499 * sequentially, validating the generation number on each to see if it has 1500 * been detached. All of this is necessary because copyout() may sleep on 1501 * disk I/O. 1502 */ 1503 static int 1504 unp_pcblist(SYSCTL_HANDLER_ARGS) 1505 { 1506 int error, i, n; 1507 int freeunp; 1508 struct unpcb *unp, **unp_list; 1509 unp_gen_t gencnt; 1510 struct xunpgen *xug; 1511 struct unp_head *head; 1512 struct xunpcb *xu; 1513 1514 switch ((intptr_t)arg1) { 1515 case SOCK_STREAM: 1516 head = &unp_shead; 1517 break; 1518 1519 case SOCK_DGRAM: 1520 head = &unp_dhead; 1521 break; 1522 1523 case SOCK_SEQPACKET: 1524 head = &unp_sphead; 1525 break; 1526 1527 default: 1528 panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1); 1529 } 1530 1531 /* 1532 * The process of preparing the PCB list is too time-consuming and 1533 * resource-intensive to repeat twice on every request. 1534 */ 1535 if (req->oldptr == NULL) { 1536 n = unp_count; 1537 req->oldidx = 2 * (sizeof *xug) 1538 + (n + n/8) * sizeof(struct xunpcb); 1539 return (0); 1540 } 1541 1542 if (req->newptr != NULL) 1543 return (EPERM); 1544 1545 /* 1546 * OK, now we're committed to doing something. 1547 */ 1548 xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 1549 UNP_LIST_LOCK(); 1550 gencnt = unp_gencnt; 1551 n = unp_count; 1552 UNP_LIST_UNLOCK(); 1553 1554 xug->xug_len = sizeof *xug; 1555 xug->xug_count = n; 1556 xug->xug_gen = gencnt; 1557 xug->xug_sogen = so_gencnt; 1558 error = SYSCTL_OUT(req, xug, sizeof *xug); 1559 if (error) { 1560 free(xug, M_TEMP); 1561 return (error); 1562 } 1563 1564 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 1565 1566 UNP_LIST_LOCK(); 1567 for (unp = LIST_FIRST(head), i = 0; unp && i < n; 1568 unp = LIST_NEXT(unp, unp_link)) { 1569 UNP_PCB_LOCK(unp); 1570 if (unp->unp_gencnt <= gencnt) { 1571 if (cr_cansee(req->td->td_ucred, 1572 unp->unp_socket->so_cred)) { 1573 UNP_PCB_UNLOCK(unp); 1574 continue; 1575 } 1576 unp_list[i++] = unp; 1577 unp->unp_refcount++; 1578 } 1579 UNP_PCB_UNLOCK(unp); 1580 } 1581 UNP_LIST_UNLOCK(); 1582 n = i; /* In case we lost some during malloc. */ 1583 1584 error = 0; 1585 xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 1586 for (i = 0; i < n; i++) { 1587 unp = unp_list[i]; 1588 UNP_PCB_LOCK(unp); 1589 unp->unp_refcount--; 1590 if (unp->unp_refcount != 0 && unp->unp_gencnt <= gencnt) { 1591 xu->xu_len = sizeof *xu; 1592 xu->xu_unpp = unp; 1593 /* 1594 * XXX - need more locking here to protect against 1595 * connect/disconnect races for SMP. 1596 */ 1597 if (unp->unp_addr != NULL) 1598 bcopy(unp->unp_addr, &xu->xu_addr, 1599 unp->unp_addr->sun_len); 1600 if (unp->unp_conn != NULL && 1601 unp->unp_conn->unp_addr != NULL) 1602 bcopy(unp->unp_conn->unp_addr, 1603 &xu->xu_caddr, 1604 unp->unp_conn->unp_addr->sun_len); 1605 bcopy(unp, &xu->xu_unp, sizeof *unp); 1606 sotoxsocket(unp->unp_socket, &xu->xu_socket); 1607 UNP_PCB_UNLOCK(unp); 1608 error = SYSCTL_OUT(req, xu, sizeof *xu); 1609 } else { 1610 freeunp = (unp->unp_refcount == 0); 1611 UNP_PCB_UNLOCK(unp); 1612 if (freeunp) { 1613 UNP_PCB_LOCK_DESTROY(unp); 1614 uma_zfree(unp_zone, unp); 1615 } 1616 } 1617 } 1618 free(xu, M_TEMP); 1619 if (!error) { 1620 /* 1621 * Give the user an updated idea of our state. If the 1622 * generation differs from what we told her before, she knows 1623 * that something happened while we were processing this 1624 * request, and it might be necessary to retry. 1625 */ 1626 xug->xug_gen = unp_gencnt; 1627 xug->xug_sogen = so_gencnt; 1628 xug->xug_count = unp_count; 1629 error = SYSCTL_OUT(req, xug, sizeof *xug); 1630 } 1631 free(unp_list, M_TEMP); 1632 free(xug, M_TEMP); 1633 return (error); 1634 } 1635 1636 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD, 1637 (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 1638 "List of active local datagram sockets"); 1639 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD, 1640 (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 1641 "List of active local stream sockets"); 1642 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, 1643 CTLTYPE_OPAQUE | CTLFLAG_RD, 1644 (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb", 1645 "List of active local seqpacket sockets"); 1646 1647 static void 1648 unp_shutdown(struct unpcb *unp) 1649 { 1650 struct unpcb *unp2; 1651 struct socket *so; 1652 1653 UNP_LINK_WLOCK_ASSERT(); 1654 UNP_PCB_LOCK_ASSERT(unp); 1655 1656 unp2 = unp->unp_conn; 1657 if ((unp->unp_socket->so_type == SOCK_STREAM || 1658 (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) { 1659 so = unp2->unp_socket; 1660 if (so != NULL) 1661 socantrcvmore(so); 1662 } 1663 } 1664 1665 static void 1666 unp_drop(struct unpcb *unp, int errno) 1667 { 1668 struct socket *so = unp->unp_socket; 1669 struct unpcb *unp2; 1670 1671 UNP_LINK_WLOCK_ASSERT(); 1672 UNP_PCB_LOCK_ASSERT(unp); 1673 1674 so->so_error = errno; 1675 unp2 = unp->unp_conn; 1676 if (unp2 == NULL) 1677 return; 1678 UNP_PCB_LOCK(unp2); 1679 unp_disconnect(unp, unp2); 1680 UNP_PCB_UNLOCK(unp2); 1681 } 1682 1683 static void 1684 unp_freerights(struct filedescent **fdep, int fdcount) 1685 { 1686 struct file *fp; 1687 int i; 1688 1689 KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount)); 1690 1691 for (i = 0; i < fdcount; i++) { 1692 fp = fdep[i]->fde_file; 1693 filecaps_free(&fdep[i]->fde_caps); 1694 unp_discard(fp); 1695 } 1696 free(fdep[0], M_FILECAPS); 1697 } 1698 1699 static int 1700 unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags) 1701 { 1702 struct thread *td = curthread; /* XXX */ 1703 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1704 int i; 1705 int *fdp; 1706 struct filedesc *fdesc = td->td_proc->p_fd; 1707 struct filedescent *fde, **fdep; 1708 void *data; 1709 socklen_t clen = control->m_len, datalen; 1710 int error, newfds; 1711 u_int newlen; 1712 1713 UNP_LINK_UNLOCK_ASSERT(); 1714 1715 error = 0; 1716 if (controlp != NULL) /* controlp == NULL => free control messages */ 1717 *controlp = NULL; 1718 while (cm != NULL) { 1719 if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 1720 error = EINVAL; 1721 break; 1722 } 1723 data = CMSG_DATA(cm); 1724 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1725 if (cm->cmsg_level == SOL_SOCKET 1726 && cm->cmsg_type == SCM_RIGHTS) { 1727 newfds = datalen / sizeof(*fdep); 1728 if (newfds == 0) 1729 goto next; 1730 fdep = data; 1731 1732 /* If we're not outputting the descriptors free them. */ 1733 if (error || controlp == NULL) { 1734 unp_freerights(fdep, newfds); 1735 goto next; 1736 } 1737 FILEDESC_XLOCK(fdesc); 1738 1739 /* 1740 * Now change each pointer to an fd in the global 1741 * table to an integer that is the index to the local 1742 * fd table entry that we set up to point to the 1743 * global one we are transferring. 1744 */ 1745 newlen = newfds * sizeof(int); 1746 *controlp = sbcreatecontrol(NULL, newlen, 1747 SCM_RIGHTS, SOL_SOCKET); 1748 if (*controlp == NULL) { 1749 FILEDESC_XUNLOCK(fdesc); 1750 error = E2BIG; 1751 unp_freerights(fdep, newfds); 1752 goto next; 1753 } 1754 1755 fdp = (int *) 1756 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1757 if (fdallocn(td, 0, fdp, newfds) != 0) { 1758 FILEDESC_XUNLOCK(td->td_proc->p_fd); 1759 error = EMSGSIZE; 1760 unp_freerights(fdep, newfds); 1761 m_freem(*controlp); 1762 *controlp = NULL; 1763 goto next; 1764 } 1765 for (i = 0; i < newfds; i++, fdp++) { 1766 fde = &fdesc->fd_ofiles[*fdp]; 1767 fde->fde_file = fdep[i]->fde_file; 1768 filecaps_move(&fdep[i]->fde_caps, 1769 &fde->fde_caps); 1770 if ((flags & MSG_CMSG_CLOEXEC) != 0) 1771 fde->fde_flags |= UF_EXCLOSE; 1772 unp_externalize_fp(fde->fde_file); 1773 } 1774 FILEDESC_XUNLOCK(fdesc); 1775 free(fdep[0], M_FILECAPS); 1776 } else { 1777 /* We can just copy anything else across. */ 1778 if (error || controlp == NULL) 1779 goto next; 1780 *controlp = sbcreatecontrol(NULL, datalen, 1781 cm->cmsg_type, cm->cmsg_level); 1782 if (*controlp == NULL) { 1783 error = ENOBUFS; 1784 goto next; 1785 } 1786 bcopy(data, 1787 CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 1788 datalen); 1789 } 1790 controlp = &(*controlp)->m_next; 1791 1792 next: 1793 if (CMSG_SPACE(datalen) < clen) { 1794 clen -= CMSG_SPACE(datalen); 1795 cm = (struct cmsghdr *) 1796 ((caddr_t)cm + CMSG_SPACE(datalen)); 1797 } else { 1798 clen = 0; 1799 cm = NULL; 1800 } 1801 } 1802 1803 m_freem(control); 1804 return (error); 1805 } 1806 1807 static void 1808 unp_zone_change(void *tag) 1809 { 1810 1811 uma_zone_set_max(unp_zone, maxsockets); 1812 } 1813 1814 static void 1815 unp_init(void) 1816 { 1817 1818 #ifdef VIMAGE 1819 if (!IS_DEFAULT_VNET(curvnet)) 1820 return; 1821 #endif 1822 unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 1823 NULL, NULL, UMA_ALIGN_PTR, 0); 1824 if (unp_zone == NULL) 1825 panic("unp_init"); 1826 uma_zone_set_max(unp_zone, maxsockets); 1827 uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached"); 1828 EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 1829 NULL, EVENTHANDLER_PRI_ANY); 1830 LIST_INIT(&unp_dhead); 1831 LIST_INIT(&unp_shead); 1832 LIST_INIT(&unp_sphead); 1833 SLIST_INIT(&unp_defers); 1834 TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL); 1835 TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL); 1836 UNP_LINK_LOCK_INIT(); 1837 UNP_LIST_LOCK_INIT(); 1838 UNP_DEFERRED_LOCK_INIT(); 1839 } 1840 1841 static int 1842 unp_internalize(struct mbuf **controlp, struct thread *td) 1843 { 1844 struct mbuf *control = *controlp; 1845 struct proc *p = td->td_proc; 1846 struct filedesc *fdesc = p->p_fd; 1847 struct bintime *bt; 1848 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1849 struct cmsgcred *cmcred; 1850 struct filedescent *fde, **fdep, *fdev; 1851 struct file *fp; 1852 struct timeval *tv; 1853 int i, fd, *fdp; 1854 void *data; 1855 socklen_t clen = control->m_len, datalen; 1856 int error, oldfds; 1857 u_int newlen; 1858 1859 UNP_LINK_UNLOCK_ASSERT(); 1860 1861 error = 0; 1862 *controlp = NULL; 1863 while (cm != NULL) { 1864 if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 1865 || cm->cmsg_len > clen) { 1866 error = EINVAL; 1867 goto out; 1868 } 1869 data = CMSG_DATA(cm); 1870 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1871 1872 switch (cm->cmsg_type) { 1873 /* 1874 * Fill in credential information. 1875 */ 1876 case SCM_CREDS: 1877 *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 1878 SCM_CREDS, SOL_SOCKET); 1879 if (*controlp == NULL) { 1880 error = ENOBUFS; 1881 goto out; 1882 } 1883 cmcred = (struct cmsgcred *) 1884 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1885 cmcred->cmcred_pid = p->p_pid; 1886 cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1887 cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1888 cmcred->cmcred_euid = td->td_ucred->cr_uid; 1889 cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 1890 CMGROUP_MAX); 1891 for (i = 0; i < cmcred->cmcred_ngroups; i++) 1892 cmcred->cmcred_groups[i] = 1893 td->td_ucred->cr_groups[i]; 1894 break; 1895 1896 case SCM_RIGHTS: 1897 oldfds = datalen / sizeof (int); 1898 if (oldfds == 0) 1899 break; 1900 /* 1901 * Check that all the FDs passed in refer to legal 1902 * files. If not, reject the entire operation. 1903 */ 1904 fdp = data; 1905 FILEDESC_SLOCK(fdesc); 1906 for (i = 0; i < oldfds; i++) { 1907 fd = *fdp++; 1908 if (fget_locked(fdesc, fd) == NULL) { 1909 FILEDESC_SUNLOCK(fdesc); 1910 error = EBADF; 1911 goto out; 1912 } 1913 fp = fdesc->fd_ofiles[fd].fde_file; 1914 if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1915 FILEDESC_SUNLOCK(fdesc); 1916 error = EOPNOTSUPP; 1917 goto out; 1918 } 1919 1920 } 1921 1922 /* 1923 * Now replace the integer FDs with pointers to the 1924 * file structure and capability rights. 1925 */ 1926 newlen = oldfds * sizeof(fdep[0]); 1927 *controlp = sbcreatecontrol(NULL, newlen, 1928 SCM_RIGHTS, SOL_SOCKET); 1929 if (*controlp == NULL) { 1930 FILEDESC_SUNLOCK(fdesc); 1931 error = E2BIG; 1932 goto out; 1933 } 1934 fdp = data; 1935 fdep = (struct filedescent **) 1936 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1937 fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS, 1938 M_WAITOK); 1939 for (i = 0; i < oldfds; i++, fdev++, fdp++) { 1940 fde = &fdesc->fd_ofiles[*fdp]; 1941 fdep[i] = fdev; 1942 fdep[i]->fde_file = fde->fde_file; 1943 filecaps_copy(&fde->fde_caps, 1944 &fdep[i]->fde_caps); 1945 unp_internalize_fp(fdep[i]->fde_file); 1946 } 1947 FILEDESC_SUNLOCK(fdesc); 1948 break; 1949 1950 case SCM_TIMESTAMP: 1951 *controlp = sbcreatecontrol(NULL, sizeof(*tv), 1952 SCM_TIMESTAMP, SOL_SOCKET); 1953 if (*controlp == NULL) { 1954 error = ENOBUFS; 1955 goto out; 1956 } 1957 tv = (struct timeval *) 1958 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1959 microtime(tv); 1960 break; 1961 1962 case SCM_BINTIME: 1963 *controlp = sbcreatecontrol(NULL, sizeof(*bt), 1964 SCM_BINTIME, SOL_SOCKET); 1965 if (*controlp == NULL) { 1966 error = ENOBUFS; 1967 goto out; 1968 } 1969 bt = (struct bintime *) 1970 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1971 bintime(bt); 1972 break; 1973 1974 default: 1975 error = EINVAL; 1976 goto out; 1977 } 1978 1979 controlp = &(*controlp)->m_next; 1980 if (CMSG_SPACE(datalen) < clen) { 1981 clen -= CMSG_SPACE(datalen); 1982 cm = (struct cmsghdr *) 1983 ((caddr_t)cm + CMSG_SPACE(datalen)); 1984 } else { 1985 clen = 0; 1986 cm = NULL; 1987 } 1988 } 1989 1990 out: 1991 m_freem(control); 1992 return (error); 1993 } 1994 1995 static struct mbuf * 1996 unp_addsockcred(struct thread *td, struct mbuf *control) 1997 { 1998 struct mbuf *m, *n, *n_prev; 1999 struct sockcred *sc; 2000 const struct cmsghdr *cm; 2001 int ngroups; 2002 int i; 2003 2004 ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 2005 m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 2006 if (m == NULL) 2007 return (control); 2008 2009 sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 2010 sc->sc_uid = td->td_ucred->cr_ruid; 2011 sc->sc_euid = td->td_ucred->cr_uid; 2012 sc->sc_gid = td->td_ucred->cr_rgid; 2013 sc->sc_egid = td->td_ucred->cr_gid; 2014 sc->sc_ngroups = ngroups; 2015 for (i = 0; i < sc->sc_ngroups; i++) 2016 sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 2017 2018 /* 2019 * Unlink SCM_CREDS control messages (struct cmsgcred), since just 2020 * created SCM_CREDS control message (struct sockcred) has another 2021 * format. 2022 */ 2023 if (control != NULL) 2024 for (n = control, n_prev = NULL; n != NULL;) { 2025 cm = mtod(n, struct cmsghdr *); 2026 if (cm->cmsg_level == SOL_SOCKET && 2027 cm->cmsg_type == SCM_CREDS) { 2028 if (n_prev == NULL) 2029 control = n->m_next; 2030 else 2031 n_prev->m_next = n->m_next; 2032 n = m_free(n); 2033 } else { 2034 n_prev = n; 2035 n = n->m_next; 2036 } 2037 } 2038 2039 /* Prepend it to the head. */ 2040 m->m_next = control; 2041 return (m); 2042 } 2043 2044 static struct unpcb * 2045 fptounp(struct file *fp) 2046 { 2047 struct socket *so; 2048 2049 if (fp->f_type != DTYPE_SOCKET) 2050 return (NULL); 2051 if ((so = fp->f_data) == NULL) 2052 return (NULL); 2053 if (so->so_proto->pr_domain != &localdomain) 2054 return (NULL); 2055 return sotounpcb(so); 2056 } 2057 2058 static void 2059 unp_discard(struct file *fp) 2060 { 2061 struct unp_defer *dr; 2062 2063 if (unp_externalize_fp(fp)) { 2064 dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK); 2065 dr->ud_fp = fp; 2066 UNP_DEFERRED_LOCK(); 2067 SLIST_INSERT_HEAD(&unp_defers, dr, ud_link); 2068 UNP_DEFERRED_UNLOCK(); 2069 atomic_add_int(&unp_defers_count, 1); 2070 taskqueue_enqueue(taskqueue_thread, &unp_defer_task); 2071 } else 2072 (void) closef(fp, (struct thread *)NULL); 2073 } 2074 2075 static void 2076 unp_process_defers(void *arg __unused, int pending) 2077 { 2078 struct unp_defer *dr; 2079 SLIST_HEAD(, unp_defer) drl; 2080 int count; 2081 2082 SLIST_INIT(&drl); 2083 for (;;) { 2084 UNP_DEFERRED_LOCK(); 2085 if (SLIST_FIRST(&unp_defers) == NULL) { 2086 UNP_DEFERRED_UNLOCK(); 2087 break; 2088 } 2089 SLIST_SWAP(&unp_defers, &drl, unp_defer); 2090 UNP_DEFERRED_UNLOCK(); 2091 count = 0; 2092 while ((dr = SLIST_FIRST(&drl)) != NULL) { 2093 SLIST_REMOVE_HEAD(&drl, ud_link); 2094 closef(dr->ud_fp, NULL); 2095 free(dr, M_TEMP); 2096 count++; 2097 } 2098 atomic_add_int(&unp_defers_count, -count); 2099 } 2100 } 2101 2102 static void 2103 unp_internalize_fp(struct file *fp) 2104 { 2105 struct unpcb *unp; 2106 2107 UNP_LINK_WLOCK(); 2108 if ((unp = fptounp(fp)) != NULL) { 2109 unp->unp_file = fp; 2110 unp->unp_msgcount++; 2111 } 2112 fhold(fp); 2113 unp_rights++; 2114 UNP_LINK_WUNLOCK(); 2115 } 2116 2117 static int 2118 unp_externalize_fp(struct file *fp) 2119 { 2120 struct unpcb *unp; 2121 int ret; 2122 2123 UNP_LINK_WLOCK(); 2124 if ((unp = fptounp(fp)) != NULL) { 2125 unp->unp_msgcount--; 2126 ret = 1; 2127 } else 2128 ret = 0; 2129 unp_rights--; 2130 UNP_LINK_WUNLOCK(); 2131 return (ret); 2132 } 2133 2134 /* 2135 * unp_defer indicates whether additional work has been defered for a future 2136 * pass through unp_gc(). It is thread local and does not require explicit 2137 * synchronization. 2138 */ 2139 static int unp_marked; 2140 static int unp_unreachable; 2141 2142 static void 2143 unp_accessable(struct filedescent **fdep, int fdcount) 2144 { 2145 struct unpcb *unp; 2146 struct file *fp; 2147 int i; 2148 2149 for (i = 0; i < fdcount; i++) { 2150 fp = fdep[i]->fde_file; 2151 if ((unp = fptounp(fp)) == NULL) 2152 continue; 2153 if (unp->unp_gcflag & UNPGC_REF) 2154 continue; 2155 unp->unp_gcflag &= ~UNPGC_DEAD; 2156 unp->unp_gcflag |= UNPGC_REF; 2157 unp_marked++; 2158 } 2159 } 2160 2161 static void 2162 unp_gc_process(struct unpcb *unp) 2163 { 2164 struct socket *soa; 2165 struct socket *so; 2166 struct file *fp; 2167 2168 /* Already processed. */ 2169 if (unp->unp_gcflag & UNPGC_SCANNED) 2170 return; 2171 fp = unp->unp_file; 2172 2173 /* 2174 * Check for a socket potentially in a cycle. It must be in a 2175 * queue as indicated by msgcount, and this must equal the file 2176 * reference count. Note that when msgcount is 0 the file is NULL. 2177 */ 2178 if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp && 2179 unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) { 2180 unp->unp_gcflag |= UNPGC_DEAD; 2181 unp_unreachable++; 2182 return; 2183 } 2184 2185 /* 2186 * Mark all sockets we reference with RIGHTS. 2187 */ 2188 so = unp->unp_socket; 2189 SOCKBUF_LOCK(&so->so_rcv); 2190 unp_scan(so->so_rcv.sb_mb, unp_accessable); 2191 SOCKBUF_UNLOCK(&so->so_rcv); 2192 2193 /* 2194 * Mark all sockets in our accept queue. 2195 */ 2196 ACCEPT_LOCK(); 2197 TAILQ_FOREACH(soa, &so->so_comp, so_list) { 2198 SOCKBUF_LOCK(&soa->so_rcv); 2199 unp_scan(soa->so_rcv.sb_mb, unp_accessable); 2200 SOCKBUF_UNLOCK(&soa->so_rcv); 2201 } 2202 ACCEPT_UNLOCK(); 2203 unp->unp_gcflag |= UNPGC_SCANNED; 2204 } 2205 2206 static int unp_recycled; 2207 SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, 2208 "Number of unreachable sockets claimed by the garbage collector."); 2209 2210 static int unp_taskcount; 2211 SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, 2212 "Number of times the garbage collector has run."); 2213 2214 static void 2215 unp_gc(__unused void *arg, int pending) 2216 { 2217 struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead, 2218 NULL }; 2219 struct unp_head **head; 2220 struct file *f, **unref; 2221 struct unpcb *unp; 2222 int i, total; 2223 2224 unp_taskcount++; 2225 UNP_LIST_LOCK(); 2226 /* 2227 * First clear all gc flags from previous runs. 2228 */ 2229 for (head = heads; *head != NULL; head++) 2230 LIST_FOREACH(unp, *head, unp_link) 2231 unp->unp_gcflag = 0; 2232 2233 /* 2234 * Scan marking all reachable sockets with UNPGC_REF. Once a socket 2235 * is reachable all of the sockets it references are reachable. 2236 * Stop the scan once we do a complete loop without discovering 2237 * a new reachable socket. 2238 */ 2239 do { 2240 unp_unreachable = 0; 2241 unp_marked = 0; 2242 for (head = heads; *head != NULL; head++) 2243 LIST_FOREACH(unp, *head, unp_link) 2244 unp_gc_process(unp); 2245 } while (unp_marked); 2246 UNP_LIST_UNLOCK(); 2247 if (unp_unreachable == 0) 2248 return; 2249 2250 /* 2251 * Allocate space for a local list of dead unpcbs. 2252 */ 2253 unref = malloc(unp_unreachable * sizeof(struct file *), 2254 M_TEMP, M_WAITOK); 2255 2256 /* 2257 * Iterate looking for sockets which have been specifically marked 2258 * as as unreachable and store them locally. 2259 */ 2260 UNP_LINK_RLOCK(); 2261 UNP_LIST_LOCK(); 2262 for (total = 0, head = heads; *head != NULL; head++) 2263 LIST_FOREACH(unp, *head, unp_link) 2264 if ((unp->unp_gcflag & UNPGC_DEAD) != 0) { 2265 f = unp->unp_file; 2266 if (unp->unp_msgcount == 0 || f == NULL || 2267 f->f_count != unp->unp_msgcount) 2268 continue; 2269 unref[total++] = f; 2270 fhold(f); 2271 KASSERT(total <= unp_unreachable, 2272 ("unp_gc: incorrect unreachable count.")); 2273 } 2274 UNP_LIST_UNLOCK(); 2275 UNP_LINK_RUNLOCK(); 2276 2277 /* 2278 * Now flush all sockets, free'ing rights. This will free the 2279 * struct files associated with these sockets but leave each socket 2280 * with one remaining ref. 2281 */ 2282 for (i = 0; i < total; i++) { 2283 struct socket *so; 2284 2285 so = unref[i]->f_data; 2286 CURVNET_SET(so->so_vnet); 2287 sorflush(so); 2288 CURVNET_RESTORE(); 2289 } 2290 2291 /* 2292 * And finally release the sockets so they can be reclaimed. 2293 */ 2294 for (i = 0; i < total; i++) 2295 fdrop(unref[i], NULL); 2296 unp_recycled += total; 2297 free(unref, M_TEMP); 2298 } 2299 2300 static void 2301 unp_dispose(struct mbuf *m) 2302 { 2303 2304 if (m) 2305 unp_scan(m, unp_freerights); 2306 } 2307 2308 static void 2309 unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int)) 2310 { 2311 struct mbuf *m; 2312 struct cmsghdr *cm; 2313 void *data; 2314 socklen_t clen, datalen; 2315 2316 while (m0 != NULL) { 2317 for (m = m0; m; m = m->m_next) { 2318 if (m->m_type != MT_CONTROL) 2319 continue; 2320 2321 cm = mtod(m, struct cmsghdr *); 2322 clen = m->m_len; 2323 2324 while (cm != NULL) { 2325 if (sizeof(*cm) > clen || cm->cmsg_len > clen) 2326 break; 2327 2328 data = CMSG_DATA(cm); 2329 datalen = (caddr_t)cm + cm->cmsg_len 2330 - (caddr_t)data; 2331 2332 if (cm->cmsg_level == SOL_SOCKET && 2333 cm->cmsg_type == SCM_RIGHTS) { 2334 (*op)(data, datalen / 2335 sizeof(struct filedescent *)); 2336 } 2337 2338 if (CMSG_SPACE(datalen) < clen) { 2339 clen -= CMSG_SPACE(datalen); 2340 cm = (struct cmsghdr *) 2341 ((caddr_t)cm + CMSG_SPACE(datalen)); 2342 } else { 2343 clen = 0; 2344 cm = NULL; 2345 } 2346 } 2347 } 2348 m0 = m0->m_act; 2349 } 2350 } 2351 2352 /* 2353 * A helper function called by VFS before socket-type vnode reclamation. 2354 * For an active vnode it clears unp_vnode pointer and decrements unp_vnode 2355 * use count. 2356 */ 2357 void 2358 vfs_unp_reclaim(struct vnode *vp) 2359 { 2360 struct socket *so; 2361 struct unpcb *unp; 2362 int active; 2363 2364 ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim"); 2365 KASSERT(vp->v_type == VSOCK, 2366 ("vfs_unp_reclaim: vp->v_type != VSOCK")); 2367 2368 active = 0; 2369 UNP_LINK_WLOCK(); 2370 VOP_UNP_CONNECT(vp, &so); 2371 if (so == NULL) 2372 goto done; 2373 unp = sotounpcb(so); 2374 if (unp == NULL) 2375 goto done; 2376 UNP_PCB_LOCK(unp); 2377 if (unp->unp_vnode == vp) { 2378 VOP_UNP_DETACH(vp); 2379 unp->unp_vnode = NULL; 2380 active = 1; 2381 } 2382 UNP_PCB_UNLOCK(unp); 2383 done: 2384 UNP_LINK_WUNLOCK(); 2385 if (active) 2386 vunref(vp); 2387 } 2388 2389 #ifdef DDB 2390 static void 2391 db_print_indent(int indent) 2392 { 2393 int i; 2394 2395 for (i = 0; i < indent; i++) 2396 db_printf(" "); 2397 } 2398 2399 static void 2400 db_print_unpflags(int unp_flags) 2401 { 2402 int comma; 2403 2404 comma = 0; 2405 if (unp_flags & UNP_HAVEPC) { 2406 db_printf("%sUNP_HAVEPC", comma ? ", " : ""); 2407 comma = 1; 2408 } 2409 if (unp_flags & UNP_HAVEPCCACHED) { 2410 db_printf("%sUNP_HAVEPCCACHED", comma ? ", " : ""); 2411 comma = 1; 2412 } 2413 if (unp_flags & UNP_WANTCRED) { 2414 db_printf("%sUNP_WANTCRED", comma ? ", " : ""); 2415 comma = 1; 2416 } 2417 if (unp_flags & UNP_CONNWAIT) { 2418 db_printf("%sUNP_CONNWAIT", comma ? ", " : ""); 2419 comma = 1; 2420 } 2421 if (unp_flags & UNP_CONNECTING) { 2422 db_printf("%sUNP_CONNECTING", comma ? ", " : ""); 2423 comma = 1; 2424 } 2425 if (unp_flags & UNP_BINDING) { 2426 db_printf("%sUNP_BINDING", comma ? ", " : ""); 2427 comma = 1; 2428 } 2429 } 2430 2431 static void 2432 db_print_xucred(int indent, struct xucred *xu) 2433 { 2434 int comma, i; 2435 2436 db_print_indent(indent); 2437 db_printf("cr_version: %u cr_uid: %u cr_ngroups: %d\n", 2438 xu->cr_version, xu->cr_uid, xu->cr_ngroups); 2439 db_print_indent(indent); 2440 db_printf("cr_groups: "); 2441 comma = 0; 2442 for (i = 0; i < xu->cr_ngroups; i++) { 2443 db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]); 2444 comma = 1; 2445 } 2446 db_printf("\n"); 2447 } 2448 2449 static void 2450 db_print_unprefs(int indent, struct unp_head *uh) 2451 { 2452 struct unpcb *unp; 2453 int counter; 2454 2455 counter = 0; 2456 LIST_FOREACH(unp, uh, unp_reflink) { 2457 if (counter % 4 == 0) 2458 db_print_indent(indent); 2459 db_printf("%p ", unp); 2460 if (counter % 4 == 3) 2461 db_printf("\n"); 2462 counter++; 2463 } 2464 if (counter != 0 && counter % 4 != 0) 2465 db_printf("\n"); 2466 } 2467 2468 DB_SHOW_COMMAND(unpcb, db_show_unpcb) 2469 { 2470 struct unpcb *unp; 2471 2472 if (!have_addr) { 2473 db_printf("usage: show unpcb <addr>\n"); 2474 return; 2475 } 2476 unp = (struct unpcb *)addr; 2477 2478 db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket, 2479 unp->unp_vnode); 2480 2481 db_printf("unp_ino: %ju unp_conn: %p\n", (uintmax_t)unp->unp_ino, 2482 unp->unp_conn); 2483 2484 db_printf("unp_refs:\n"); 2485 db_print_unprefs(2, &unp->unp_refs); 2486 2487 /* XXXRW: Would be nice to print the full address, if any. */ 2488 db_printf("unp_addr: %p\n", unp->unp_addr); 2489 2490 db_printf("unp_cc: %d unp_mbcnt: %d unp_gencnt: %llu\n", 2491 unp->unp_cc, unp->unp_mbcnt, 2492 (unsigned long long)unp->unp_gencnt); 2493 2494 db_printf("unp_flags: %x (", unp->unp_flags); 2495 db_print_unpflags(unp->unp_flags); 2496 db_printf(")\n"); 2497 2498 db_printf("unp_peercred:\n"); 2499 db_print_xucred(2, &unp->unp_peercred); 2500 2501 db_printf("unp_refcount: %u\n", unp->unp_refcount); 2502 } 2503 #endif 2504