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