2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * @(#)unpcb.h 8.1 (Berkeley) 6/2/93
38 typedef uint64_t unp_gen_t;
40 #if defined(_KERNEL) || defined(_WANT_UNPCB)
41 #include <sys/queue.h>
42 #include <sys/ucred.h>
45 * Protocol control block for an active
46 * instance of a UNIX internal protocol.
48 * A socket may be associated with a vnode in the
49 * filesystem. If so, the unp_vnode pointer holds
50 * a reference count to this vnode, which should be irele'd
51 * when the socket goes away.
53 * A socket may be connected to another socket, in which
54 * case the control block of the socket to which it is connected
55 * is given by unp_conn.
57 * A socket may be referenced by a number of sockets (e.g. several
58 * sockets may be connected to a datagram socket.) These sockets
59 * are in a linked list starting with unp_refs, linked through
60 * unp_nextref and null-terminated. Note that a socket may be referenced
61 * by a number of other sockets and may also reference a socket (not
62 * necessarily one which is referencing it). This generates
63 * the need for unp_refs and unp_nextref to be separate fields.
65 * Stream sockets keep copies of receive sockbuf sb_cc and sb_mbcnt
66 * so that changes in the sockbuf may be computed to modify
67 * back pressure on the sender accordingly.
72 * (g) Locked using linkage lock
73 * (l) Locked using list lock
74 * (p) Locked using pcb lock
76 LIST_HEAD(unp_head, unpcb);
80 struct mtx unp_mtx; /* PCB mutex */
81 struct unpcb *unp_conn; /* (p) connected socket */
82 volatile u_int unp_refcount; /* (a, p) atomic refcount */
83 short unp_flags; /* (p) PCB flags */
84 short unp_gcflag; /* (g) Garbage collector flags */
85 struct sockaddr_un *unp_addr; /* (p) bound address of socket */
86 struct socket *unp_socket; /* (c) pointer back to socket */
88 u_int unp_pairbusy; /* (p) threads acquiring peer locks */
89 struct vnode *unp_vnode; /* (p) associated file if applicable */
90 struct xucred unp_peercred; /* (p) peer credentials if applicable */
91 LIST_ENTRY(unpcb) unp_reflink; /* (l) link in unp_refs list */
92 LIST_ENTRY(unpcb) unp_link; /* (g) glue on list of all PCBs */
93 struct unp_head unp_refs; /* (l) referencing socket linked list */
94 unp_gen_t unp_gencnt; /* (g) generation count of this item */
95 struct file *unp_file; /* (g) back-pointer to file for gc */
96 u_int unp_msgcount; /* (g) references from message queue */
97 u_int unp_gcrefs; /* (g) garbage collector refcount */
98 ino_t unp_ino; /* (g) fake inode number */
99 LIST_ENTRY(unpcb) unp_dead; /* (g) link in dead list */
100 } __aligned(CACHE_LINE_SIZE);
103 * Flags in unp_flags.
105 * UNP_HAVEPC - indicates that the unp_peercred member is filled in
106 * and is really the credentials of the connected peer. This is used
107 * to determine whether the contents should be sent to the user or
110 #define UNP_HAVEPC 0x001
111 #define UNP_WANTCRED_ALWAYS 0x002 /* credentials wanted always */
112 #define UNP_WANTCRED_ONESHOT 0x004 /* credentials wanted once */
113 #define UNP_CONNWAIT 0x008 /* connect blocks until accepted */
115 #define UNP_WANTCRED_MASK (UNP_WANTCRED_ONESHOT | UNP_WANTCRED_ALWAYS)
118 * These flags are used to handle non-atomicity in connect() and bind()
119 * operations on a socket: in particular, to avoid races between multiple
120 * threads or processes operating simultaneously on the same socket.
122 #define UNP_CONNECTING 0x010 /* Currently connecting. */
123 #define UNP_BINDING 0x020 /* Currently binding. */
124 #define UNP_WAITING 0x040 /* Peer state is changing. */
127 * Flags in unp_gcflag.
129 #define UNPGC_DEAD 0x1 /* unpcb might be dead. */
130 #define UNPGC_IGNORE_RIGHTS 0x2 /* Attached rights are freed */
132 #define sotounpcb(so) ((struct unpcb *)((so)->so_pcb))
134 #endif /* _KERNEL || _WANT_UNPCB */
137 * UNPCB structure exported to user-land via sysctl(3).
139 * Fields prefixed with "xu_" are unique to the export structure, and fields
140 * with "unp_" or other prefixes match corresponding fields of 'struct unpcb'.
143 * (s) - used by userland utilities in src
144 * (p) - used by utilities in ports
145 * (3) - is known to be used by third party software not in ports
146 * (n) - no known usage
148 * Evil hack: declare only if sys/socketvar.h have been included.
150 #ifdef _SYS_SOCKETVAR_H_
152 ksize_t xu_len; /* length of this structure */
153 kvaddr_t xu_unpp; /* to help netstat, fstat */
154 kvaddr_t unp_vnode; /* (s) */
155 kvaddr_t unp_conn; /* (s) */
156 kvaddr_t xu_firstref; /* (s) */
157 kvaddr_t xu_nextref; /* (s) */
158 unp_gen_t unp_gencnt; /* (s) */
159 int64_t xu_spare64[8];
160 int32_t xu_spare32[8];
162 struct sockaddr_un xu_addr; /* our bound address */
166 struct sockaddr_un xu_caddr; /* their bound address */
169 struct xsocket xu_socket;
170 } __aligned(MAX(8, sizeof(void *)));
178 #endif /* _SYS_SOCKETVAR_H_ */
183 /* In uipc_userreq.c */
185 unp_copy_peercred(struct thread *td, struct unpcb *client_unp,
186 struct unpcb *server_unp, struct unpcb *listen_unp);
189 #endif /* _SYS_UNPCB_H_ */