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