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