xref: /freebsd/sys/sys/unpcb.h (revision bfd03046d18776ea70785ca1ef36dfc60822de3b)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  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 
32 #ifndef _SYS_UNPCB_H_
33 #define _SYS_UNPCB_H_
34 
35 typedef uint64_t unp_gen_t;
36 
37 #if defined(_KERNEL) || defined(_WANT_UNPCB)
38 #include <sys/queue.h>
39 #include <sys/ucred.h>
40 
41 /*
42  * Protocol control block for an active
43  * instance of a UNIX internal protocol.
44  *
45  * A socket may be associated with a vnode in the
46  * filesystem.  If so, the unp_vnode pointer holds
47  * a reference count to this vnode, which should be irele'd
48  * when the socket goes away.
49  *
50  * A socket may be connected to another socket, in which
51  * case the control block of the socket to which it is connected
52  * is given by unp_conn.
53  *
54  * A socket may be referenced by a number of sockets (e.g. several
55  * sockets may be connected to a datagram socket.)  These sockets
56  * are in a linked list starting with unp_refs, linked through
57  * unp_nextref and null-terminated.  Note that a socket may be referenced
58  * by a number of other sockets and may also reference a socket (not
59  * necessarily one which is referencing it).  This generates
60  * the need for unp_refs and unp_nextref to be separate fields.
61  *
62  * Stream sockets keep copies of receive sockbuf sb_cc and sb_mbcnt
63  * so that changes in the sockbuf may be computed to modify
64  * back pressure on the sender accordingly.
65  *
66  * Locking key:
67  * (a) Atomic
68  * (c) Constant
69  * (g) Locked using linkage lock
70  * (l) Locked using list lock
71  * (p) Locked using pcb lock
72  */
73 LIST_HEAD(unp_head, unpcb);
74 
75 struct unpcb {
76 	/* Cache line 1 */
77 	struct	mtx unp_mtx;		/* PCB mutex */
78 	struct	unpcb *unp_conn;	/* (p) connected socket */
79 	volatile u_int unp_refcount;	/* (a, p) atomic refcount */
80 	short	unp_flags;		/* (p) PCB flags */
81 	short	unp_gcflag;		/* (g) Garbage collector flags */
82 	struct	sockaddr_un *unp_addr;	/* (p) bound address of socket */
83 	struct	socket *unp_socket;	/* (c) pointer back to socket */
84 	/* Cache line 2 */
85 	u_int	unp_pairbusy;		/* (p) threads acquiring peer locks */
86 	struct	vnode *unp_vnode;	/* (p) associated file if applicable */
87 	struct	xucred unp_peercred;	/* (p) peer credentials if applicable */
88 	LIST_ENTRY(unpcb) unp_reflink;	/* (l) link in unp_refs list */
89 	LIST_ENTRY(unpcb) unp_link; 	/* (g) glue on list of all PCBs */
90 	struct	unp_head unp_refs;	/* (l) referencing socket linked list */
91 	unp_gen_t unp_gencnt;		/* (g) generation count of this item */
92 	struct	file *unp_file;		/* (g) back-pointer to file for gc */
93 	u_int	unp_msgcount;		/* (g) references from message queue */
94 	u_int	unp_gcrefs;		/* (g) garbage collector refcount */
95 	ino_t	unp_ino;		/* (g) fake inode number */
96 	mode_t  unp_mode;		/* (g) initial pre-bind() mode */
97 	LIST_ENTRY(unpcb) unp_dead;	/* (g) link in dead list */
98 } __aligned(CACHE_LINE_SIZE);
99 
100 /*
101  * Flags in unp_flags.
102  *
103  * UNP_HAVEPC - indicates that the unp_peercred member is filled in
104  * and is really the credentials of the connected peer.  This is used
105  * to determine whether the contents should be sent to the user or
106  * not.
107  */
108 #define	UNP_HAVEPC			0x001
109 #define	UNP_WANTCRED_ALWAYS		0x002	/* credentials wanted always */
110 #define	UNP_WANTCRED_ONESHOT		0x004	/* credentials wanted once */
111 #define	UNP_WANTCRED_MASK	(UNP_WANTCRED_ONESHOT | UNP_WANTCRED_ALWAYS)
112 
113 /*
114  * These flags are used to handle non-atomicity in connect() and bind()
115  * operations on a socket: in particular, to avoid races between multiple
116  * threads or processes operating simultaneously on the same socket.
117  */
118 #define	UNP_CONNECTING			0x010	/* Currently connecting. */
119 #define	UNP_BINDING			0x020	/* Currently binding. */
120 #define	UNP_WAITING			0x040	/* Peer state is changing. */
121 
122 /*
123  * Flags in unp_gcflag.
124  */
125 #define	UNPGC_DEAD			0x1	/* unpcb might be dead. */
126 #define	UNPGC_IGNORE_RIGHTS		0x2	/* Attached rights are freed */
127 
128 #define	sotounpcb(so)	((struct unpcb *)((so)->so_pcb))
129 
130 #endif	/* _KERNEL || _WANT_UNPCB */
131 
132 /*
133  * UNPCB structure exported to user-land via sysctl(3).
134  *
135  * Fields prefixed with "xu_" are unique to the export structure, and fields
136  * with "unp_" or other prefixes match corresponding fields of 'struct unpcb'.
137  *
138  * Legend:
139  * (s) - used by userland utilities in src
140  * (p) - used by utilities in ports
141  * (3) - is known to be used by third party software not in ports
142  * (n) - no known usage
143  *
144  * Evil hack: declare only if sys/socketvar.h have been included.
145  */
146 #ifdef	_SYS_SOCKETVAR_H_
147 struct xunpcb {
148 	ksize_t		xu_len;			/* length of this structure */
149 	kvaddr_t	xu_unpp;		/* to help netstat, fstat */
150 	kvaddr_t	unp_vnode;		/* (s) */
151 	kvaddr_t	unp_conn;		/* (s) */
152 	kvaddr_t	xu_firstref;		/* (s) */
153 	kvaddr_t	xu_nextref;		/* (s) */
154 	unp_gen_t	unp_gencnt;		/* (s) */
155 	int64_t		xu_spare64[8];
156 	int32_t		xu_spare32[8];
157 	union {
158 		struct	sockaddr_un xu_addr;	/* our bound address */
159 		char	xu_dummy1[256];
160 	};
161 	union {
162 		struct	sockaddr_un xu_caddr;	/* their bound address */
163 		char	xu_dummy2[256];
164 	};
165 	struct xsocket	xu_socket;
166 } __aligned(MAX(8, sizeof(void *)));
167 
168 struct xunpgen {
169 	ksize_t	xug_len;
170 	u_int	xug_count;
171 	unp_gen_t xug_gen;
172 	so_gen_t xug_sogen;
173 } __aligned(8);
174 #endif /* _SYS_SOCKETVAR_H_ */
175 
176 #if defined(_KERNEL)
177 struct thread;
178 
179 /* In uipc_userreq.c */
180 void
181 unp_copy_peercred(struct thread *td, struct unpcb *client_unp,
182     struct unpcb *server_unp, struct unpcb *listen_unp);
183 #endif
184 
185 #endif /* _SYS_UNPCB_H_ */
186