Changeset View
Changeset View
Standalone View
Standalone View
sys/kern/uipc_usrreq.c
| Show First 20 Lines • Show All 290 Lines • ▼ Show 20 Lines | |||||
| static int uipc_connect2(struct socket *, struct socket *); | static int uipc_connect2(struct socket *, struct socket *); | ||||
| static int uipc_ctloutput(struct socket *, struct sockopt *); | static int uipc_ctloutput(struct socket *, struct sockopt *); | ||||
| static int unp_connect(struct socket *, struct sockaddr *, | static int unp_connect(struct socket *, struct sockaddr *, | ||||
| struct thread *); | struct thread *); | ||||
| static int unp_connectat(int, struct socket *, struct sockaddr *, | static int unp_connectat(int, struct socket *, struct sockaddr *, | ||||
| struct thread *, struct socket **); | struct thread *, struct socket **); | ||||
| static int unp_connect_peer(struct socket *, struct unpcb *, | static int unp_connect_peer(struct socket *, struct unpcb *, | ||||
| struct sockaddr **, struct thread *, bool); | struct sockaddr **, struct thread *, bool); | ||||
| static int unp_vnode_peer(struct vnode *, struct thread *, | |||||
| struct socket **); | |||||
| static void unp_connect2(struct socket *, struct socket *, bool); | static void unp_connect2(struct socket *, struct socket *, bool); | ||||
| static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); | static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); | ||||
| static void unp_dispose(struct socket *so); | static void unp_dispose(struct socket *so); | ||||
| static void unp_drop(struct unpcb *); | static void unp_drop(struct unpcb *); | ||||
| static void unp_gc(__unused void *, int); | static void unp_gc(__unused void *, int); | ||||
| static void unp_scan(struct mbuf *, void (*)(struct filedescent **, int)); | static void unp_scan(struct mbuf *, void (*)(struct filedescent **, int)); | ||||
| static void unp_discard(struct file *); | static void unp_discard(struct file *); | ||||
| static void unp_freerights(struct filedescent **, int); | static void unp_freerights(struct filedescent **, int); | ||||
| ▲ Show 20 Lines • Show All 2,618 Lines • ▼ Show 20 Lines | |||||
| * | * | ||||
| * Note: the referenced_peerp mechanism is here only for the datagram fast-send | * Note: the referenced_peerp mechanism is here only for the datagram fast-send | ||||
| * path, which enqueues under the peer's PCB lock. | * path, which enqueues under the peer's PCB lock. | ||||
| */ | */ | ||||
| static int | static int | ||||
| unp_connectat(int fd, struct socket *so, struct sockaddr *nam, | unp_connectat(int fd, struct socket *so, struct sockaddr *nam, | ||||
| struct thread *td, struct socket **referenced_peerp) | struct thread *td, struct socket **referenced_peerp) | ||||
| { | { | ||||
| struct mtx *vplock; | |||||
| struct socket *so2; | struct socket *so2; | ||||
| struct vnode *vp; | struct unpcb *unp; | ||||
| struct unpcb *unp, *unp2; | |||||
| struct nameidata nd; | struct nameidata nd; | ||||
| char buf[SOCK_MAXADDRLEN]; | char buf[SOCK_MAXADDRLEN]; | ||||
| struct sockaddr *sa; | struct sockaddr *sa; | ||||
| cap_rights_t rights; | cap_rights_t rights; | ||||
| const char *path; | const char *path; | ||||
| int error, len; | int error, len; | ||||
| bool connreq; | bool connreq; | ||||
| ▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | else | ||||
| sa = NULL; | sa = NULL; | ||||
| NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | | NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | | ||||
| (fd == AT_FDCWD ? 0 : EMPTYPATH), UIO_SYSSPACE, buf, fd, | (fd == AT_FDCWD ? 0 : EMPTYPATH), UIO_SYSSPACE, buf, fd, | ||||
| cap_rights_init_one(&rights, CAP_CONNECTAT)); | cap_rights_init_one(&rights, CAP_CONNECTAT)); | ||||
| error = namei(&nd); | error = namei(&nd); | ||||
| if (error) | if (error) | ||||
| goto out; | goto out; | ||||
| NDFREE_PNBUF(&nd); | NDFREE_PNBUF(&nd); | ||||
| vp = nd.ni_vp; | |||||
| ASSERT_VOP_LOCKED(vp, "unp_connect"); | |||||
| /* | /* | ||||
| * Resolve the vnode to a referenced peer socket, then drop the vnode | * Resolve the vnode to a referenced peer socket and drop the vnode | ||||
| * before connecting. Holding a reference on the peer keeps it stable | * before connecting: the reference keeps the peer stable, so no vnode | ||||
| * in place of the per-vnode unp_vp_mtxpool lock, so no vnode lock is | * lock is held across unp_connect_peer() (which matters for the | ||||
| * held across unp_connect_peer() -- which is what the return_locked | * return_locked datagram fast path). | ||||
| * datagram fast path needs, since vput() must not sleep while the peer | |||||
| * is locked. | |||||
| */ | */ | ||||
| if (vp->v_type != VSOCK) { | error = unp_vnode_peer(nd.ni_vp, td, &so2); | ||||
| error = ENOTSOCK; | vput(nd.ni_vp); | ||||
| goto drop_vp; | |||||
| } | |||||
| #ifdef MAC | |||||
| error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD); | |||||
| if (error) | |||||
| goto drop_vp; | |||||
| #endif | |||||
| error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); | |||||
| if (error) | |||||
| goto drop_vp; | |||||
| vplock = mtx_pool_find(unp_vp_mtxpool, vp); | |||||
| mtx_lock(vplock); | |||||
| VOP_UNP_CONNECT(vp, &unp2); | |||||
| if (unp2 == NULL) | |||||
| error = ECONNREFUSED; | |||||
| else | |||||
| soref(so2 = unp2->unp_socket); | |||||
| mtx_unlock(vplock); | |||||
| drop_vp: | |||||
| vput(vp); | |||||
| if (error != 0) | if (error != 0) | ||||
| goto out; | goto out; | ||||
| error = unp_connect_peer(so, sotounpcb(so2), &sa, td, | error = unp_connect_peer(so, sotounpcb(so2), &sa, td, | ||||
| referenced_peerp != NULL); | referenced_peerp != NULL); | ||||
| /* Transfer the reference; the caller releases it after unlocking. */ | /* Transfer the reference; the caller releases it after unlocking. */ | ||||
| if (error == 0 && referenced_peerp != NULL) | if (error == 0 && referenced_peerp != NULL) | ||||
| *referenced_peerp = so2; | *referenced_peerp = so2; | ||||
| else | else | ||||
| sorele(so2); | sorele(so2); | ||||
| out: | out: | ||||
| free(sa, M_SONAME); | free(sa, M_SONAME); | ||||
| if (__predict_false(error)) { | if (__predict_false(error)) { | ||||
| UNP_PCB_LOCK(unp); | UNP_PCB_LOCK(unp); | ||||
| KASSERT((unp->unp_flags & UNP_CONNECTING) != 0, | KASSERT((unp->unp_flags & UNP_CONNECTING) != 0, | ||||
| ("%s: unp %p has UNP_CONNECTING clear", __func__, unp)); | ("%s: unp %p has UNP_CONNECTING clear", __func__, unp)); | ||||
| unp->unp_flags &= ~UNP_CONNECTING; | unp->unp_flags &= ~UNP_CONNECTING; | ||||
| UNP_PCB_UNLOCK(unp); | UNP_PCB_UNLOCK(unp); | ||||
| } | } | ||||
| return (error); | return (error); | ||||
| } | } | ||||
| /* | /* | ||||
| * Resolve locked vnode 'vp' to the unix-domain socket it names and return a | |||||
| * referenced peer socket in '*so2p'. As the connect(2)-time resolution, this | |||||
| * enforces the caller's authorization to reach the socket -- filesystem | |||||
| * permission (VOP_ACCESS) and MAC (mac_vnode_check_open) -- which bare readers | |||||
| * of the vnode->pcb binding, such as vfs_unp_reclaim(), deliberately skip. | |||||
| * | |||||
| * The returned reference keeps the peer stable for unp_connect_peer() once vp's | |||||
| * per-vnode binding lock is dropped, so the caller must release it with | |||||
| * sorele(). Does not consume 'vp'. | |||||
markj: These comments (here and elsewhere in the series) are somewhat verbose for my taste, they're… | |||||
| */ | |||||
| static int | |||||
| unp_vnode_peer(struct vnode *vp, struct thread *td, struct socket **so2p) | |||||
| { | |||||
| struct mtx *vplock; | |||||
| struct unpcb *unp2; | |||||
| int error; | |||||
| ASSERT_VOP_LOCKED(vp, __func__); | |||||
| if (vp->v_type != VSOCK) | |||||
| return (ENOTSOCK); | |||||
| #ifdef MAC | |||||
| error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD); | |||||
| if (error != 0) | |||||
| return (error); | |||||
| #endif | |||||
| error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); | |||||
| if (error != 0) | |||||
| return (error); | |||||
| vplock = mtx_pool_find(unp_vp_mtxpool, vp); | |||||
| mtx_lock(vplock); | |||||
| VOP_UNP_CONNECT(vp, &unp2); | |||||
| if (unp2 == NULL) | |||||
| error = ECONNREFUSED; | |||||
| else | |||||
| soref(*so2p = unp2->unp_socket); | |||||
| mtx_unlock(vplock); | |||||
| return (error); | |||||
| } | |||||
| /* | |||||
| * Second half of connecting a unix socket: 'so' is our connecting socket, | * Second half of connecting a unix socket: 'so' is our connecting socket, | ||||
| * with UNP_CONNECTING set, and 'unp2' is the PCB of the peer named by the | * with UNP_CONNECTING set, and 'unp2' is the PCB of the peer named by the | ||||
| * caller, which must guarantee its stability (by holding a reference on the | * caller, which must guarantee its stability (by holding a reference on the | ||||
| * peer socket, or the vnode lock plus unp_vp_mtxpool lock for a peer found | * peer socket, or the vnode lock plus unp_vp_mtxpool lock for a peer found | ||||
| * via VOP_UNP_CONNECT()). | * via VOP_UNP_CONNECT()). | ||||
| * | * | ||||
| * For connection-oriented sockets '*sap' points to a buffer to hold the | * For connection-oriented sockets '*sap' points to a buffer to hold the | ||||
| * listener's address; it is consumed (set to NULL) if used. On success | * listener's address; it is consumed (set to NULL) if used. On success | ||||
| ▲ Show 20 Lines • Show All 1,693 Lines • Show Last 20 Lines | |||||
These comments (here and elsewhere in the series) are somewhat verbose for my taste, they're just restating what the code does (or isn't doing).