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