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