xref: /openbsd/sys/sys/socketvar.h (revision 621c49c5)
1 /*	$OpenBSD: socketvar.h,v 1.108 2022/08/21 16:22:18 mvs Exp $	*/
2 /*	$NetBSD: socketvar.h,v 1.18 1996/02/09 18:25:38 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1982, 1986, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)socketvar.h	8.1 (Berkeley) 6/2/93
33  */
34 
35 #ifndef _SYS_SOCKETVAR_H_
36 #define _SYS_SOCKETVAR_H_
37 
38 #include <sys/selinfo.h>			/* for struct selinfo */
39 #include <sys/queue.h>
40 #include <sys/sigio.h>				/* for struct sigio_ref */
41 #include <sys/task.h>
42 #include <sys/timeout.h>
43 #include <sys/rwlock.h>
44 #include <sys/refcnt.h>
45 
46 #ifndef	_SOCKLEN_T_DEFINED_
47 #define	_SOCKLEN_T_DEFINED_
48 typedef	__socklen_t	socklen_t;	/* length type for network syscalls */
49 #endif
50 
51 TAILQ_HEAD(soqhead, socket);
52 
53 /*
54  * Kernel structure per socket.
55  * Contains send and receive buffer queues,
56  * handle on protocol and pointer to protocol
57  * private data and error information.
58  */
59 struct socket {
60 	const struct protosw *so_proto;	/* protocol handle */
61 	struct rwlock so_lock;		/* this socket lock */
62 	struct refcnt so_refcnt;	/* references to this socket */
63 	void	*so_pcb;		/* protocol control block */
64 	u_int	so_state;		/* internal state flags SS_*, below */
65 	short	so_type;		/* generic type, see socket.h */
66 	short	so_options;		/* from socket call, see socket.h */
67 	short	so_linger;		/* time to linger while closing */
68 /*
69  * Variables for connection queueing.
70  * Socket where accepts occur is so_head in all subsidiary sockets.
71  * If so_head is 0, socket is not related to an accept.
72  * For head socket so_q0 queues partially completed connections,
73  * while so_q is a queue of connections ready to be accepted.
74  * If a connection is aborted and it has so_head set, then
75  * it has to be pulled out of either so_q0 or so_q.
76  * We allow connections to queue up based on current queue lengths
77  * and limit on number of queued connections for this socket.
78  */
79 	struct	socket	*so_head;	/* back pointer to accept socket */
80 	struct	soqhead	*so_onq;	/* queue (q or q0) that we're on */
81 	struct	soqhead	so_q0;		/* queue of partial connections */
82 	struct	soqhead	so_q;		/* queue of incoming connections */
83 	struct	sigio_ref so_sigio;	/* async I/O registration */
84 	TAILQ_ENTRY(socket) so_qe;	/* our queue entry (q or q0) */
85 	short	so_q0len;		/* partials on so_q0 */
86 	short	so_qlen;		/* number of connections on so_q */
87 	short	so_qlimit;		/* max number queued connections */
88 	u_long	so_newconn;		/* # of pending sonewconn() threads */
89 	short	so_timeo;		/* connection timeout */
90 	u_long	so_oobmark;		/* chars to oob mark */
91 	u_int	so_error;		/* error affecting connection */
92 /*
93  * Variables for socket splicing, allocated only when needed.
94  */
95 	struct sosplice {
96 		struct	socket *ssp_socket;	/* send data to drain socket */
97 		struct	socket *ssp_soback;	/* back ref to source socket */
98 		off_t	ssp_len;		/* number of bytes spliced */
99 		off_t	ssp_max;		/* maximum number of bytes */
100 		struct	timeval ssp_idletv;	/* idle timeout */
101 		struct	timeout ssp_idleto;
102 		struct	task ssp_task;		/* task for somove */
103 	} *so_sp;
104 /*
105  * Variables for socket buffering.
106  */
107 	struct	sockbuf {
108 /* The following fields are all zeroed on flush. */
109 #define	sb_startzero	sb_cc
110 		u_long	sb_cc;		/* actual chars in buffer */
111 		u_long	sb_datacc;	/* data only chars in buffer */
112 		u_long	sb_hiwat;	/* max actual char count */
113 		u_long  sb_wat;		/* default watermark */
114 		u_long	sb_mbcnt;	/* chars of mbufs used */
115 		u_long	sb_mbmax;	/* max chars of mbufs to use */
116 		long	sb_lowat;	/* low water mark */
117 		struct mbuf *sb_mb;	/* the mbuf chain */
118 		struct mbuf *sb_mbtail;	/* the last mbuf in the chain */
119 		struct mbuf *sb_lastrecord;/* first mbuf of last record in
120 					      socket buffer */
121 		short	sb_flags;	/* flags, see below */
122 /* End area that is zeroed on flush. */
123 #define	sb_endzero	sb_flags
124 		uint64_t sb_timeo_nsecs;/* timeout for read/write */
125 		struct	selinfo sb_sel;	/* process selecting read/write */
126 	} so_rcv, so_snd;
127 #define	SB_MAX		(2*1024*1024)	/* default for max chars in sockbuf */
128 #define	SB_LOCK		0x01		/* lock on data queue */
129 #define	SB_WANT		0x02		/* someone is waiting to lock */
130 #define	SB_WAIT		0x04		/* someone is waiting for data/space */
131 #define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
132 #define	SB_SPLICE	0x20		/* buffer is splice source or drain */
133 #define	SB_NOINTR	0x40		/* operations not interruptible */
134 
135 	void	(*so_upcall)(struct socket *so, caddr_t arg, int waitf);
136 	caddr_t	so_upcallarg;		/* Arg for above */
137 	uid_t	so_euid, so_ruid;	/* who opened the socket */
138 	gid_t	so_egid, so_rgid;
139 	pid_t	so_cpid;		/* pid of process that opened socket */
140 };
141 
142 /*
143  * Socket state bits.
144  */
145 #define	SS_NOFDREF		0x001	/* no file table ref any more */
146 #define	SS_ISCONNECTED		0x002	/* socket connected to a peer */
147 #define	SS_ISCONNECTING		0x004	/* in process of connecting to peer */
148 #define	SS_ISDISCONNECTING	0x008	/* in process of disconnecting */
149 #define	SS_CANTSENDMORE		0x010	/* can't send more data to peer */
150 #define	SS_CANTRCVMORE		0x020	/* can't receive more data from peer */
151 #define	SS_RCVATMARK		0x040	/* at mark on input */
152 #define	SS_ISDISCONNECTED	0x800	/* socket disconnected from peer */
153 
154 #define	SS_PRIV			0x080	/* privileged for broadcast, raw... */
155 #define	SS_CONNECTOUT		0x1000	/* connect, not accept, at this end */
156 #define	SS_ISSENDING		0x2000	/* hint for lower layer */
157 #define	SS_DNS			0x4000	/* created using SOCK_DNS socket(2) */
158 #define	SS_NEWCONN_WAIT		0x8000	/* waiting sonewconn() relock */
159 #define	SS_YP			0x10000	/* created using ypconnect(2) */
160 
161 #ifdef _KERNEL
162 
163 #include <lib/libkern/libkern.h>
164 
165 void	soassertlocked(struct socket *);
166 
167 static inline void
168 soref(struct socket *so)
169 {
170 	refcnt_take(&so->so_refcnt);
171 }
172 
173 static inline void
174 sorele(struct socket *so)
175 {
176 	refcnt_rele_wake(&so->so_refcnt);
177 }
178 
179 /*
180  * Macros for sockets and socket buffering.
181  */
182 
183 #define isspliced(so)		((so)->so_sp && (so)->so_sp->ssp_socket)
184 #define issplicedback(so)	((so)->so_sp && (so)->so_sp->ssp_soback)
185 
186 /*
187  * Do we need to notify the other side when I/O is possible?
188  */
189 static inline int
190 sb_notify(struct socket *so, struct sockbuf *sb)
191 {
192 	KASSERT(sb == &so->so_rcv || sb == &so->so_snd);
193 	soassertlocked(so);
194 	return ((sb->sb_flags & (SB_WAIT|SB_ASYNC|SB_SPLICE)) != 0 ||
195 	    !klist_empty(&sb->sb_sel.si_note));
196 }
197 
198 /*
199  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
200  * This is problematical if the fields are unsigned, as the space might
201  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
202  * overflow and return 0.
203  */
204 static inline long
205 sbspace(struct socket *so, struct sockbuf *sb)
206 {
207 	KASSERT(sb == &so->so_rcv || sb == &so->so_snd);
208 	soassertlocked(so);
209 	return lmin(sb->sb_hiwat - sb->sb_cc, sb->sb_mbmax - sb->sb_mbcnt);
210 }
211 
212 /* do we have to send all at once on a socket? */
213 #define	sosendallatonce(so) \
214     ((so)->so_proto->pr_flags & PR_ATOMIC)
215 
216 /* are we sending on this socket? */
217 #define	soissending(so) \
218     ((so)->so_state & SS_ISSENDING)
219 
220 /* can we read something from so? */
221 static inline int
222 soreadable(struct socket *so)
223 {
224 	soassertlocked(so);
225 	if (isspliced(so))
226 		return 0;
227 	return (so->so_state & SS_CANTRCVMORE) || so->so_qlen || so->so_error ||
228 	    so->so_rcv.sb_cc >= so->so_rcv.sb_lowat;
229 }
230 
231 /* can we write something to so? */
232 #define	sowriteable(so) \
233     ((sbspace((so), &(so)->so_snd) >= (so)->so_snd.sb_lowat && \
234 	(((so)->so_state & SS_ISCONNECTED) || \
235 	  ((so)->so_proto->pr_flags & PR_CONNREQUIRED)==0)) || \
236     ((so)->so_state & SS_CANTSENDMORE) || (so)->so_error)
237 
238 /* adjust counters in sb reflecting allocation of m */
239 #define	sballoc(so, sb, m) do {						\
240 	(sb)->sb_cc += (m)->m_len;					\
241 	if ((m)->m_type != MT_CONTROL && (m)->m_type != MT_SONAME)	\
242 		(sb)->sb_datacc += (m)->m_len;				\
243 	(sb)->sb_mbcnt += MSIZE;					\
244 	if ((m)->m_flags & M_EXT)					\
245 		(sb)->sb_mbcnt += (m)->m_ext.ext_size;			\
246 } while (/* CONSTCOND */ 0)
247 
248 /* adjust counters in sb reflecting freeing of m */
249 #define	sbfree(so, sb, m) do {						\
250 	(sb)->sb_cc -= (m)->m_len;					\
251 	if ((m)->m_type != MT_CONTROL && (m)->m_type != MT_SONAME)	\
252 		(sb)->sb_datacc -= (m)->m_len;				\
253 	(sb)->sb_mbcnt -= MSIZE;					\
254 	if ((m)->m_flags & M_EXT)					\
255 		(sb)->sb_mbcnt -= (m)->m_ext.ext_size;			\
256 } while (/* CONSTCOND */ 0)
257 
258 /*
259  * Set lock on sockbuf sb; sleep if lock is already held.
260  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
261  * Returns error without lock if sleep is interrupted.
262  */
263 int sblock(struct socket *, struct sockbuf *, int);
264 
265 /* release lock on sockbuf sb */
266 void sbunlock(struct socket *, struct sockbuf *);
267 
268 #define	SB_EMPTY_FIXUP(sb) do {						\
269 	if ((sb)->sb_mb == NULL) {					\
270 		(sb)->sb_mbtail = NULL;					\
271 		(sb)->sb_lastrecord = NULL;				\
272 	}								\
273 } while (/*CONSTCOND*/0)
274 
275 extern u_long sb_max;
276 
277 extern struct pool	socket_pool;
278 
279 struct mbuf;
280 struct sockaddr;
281 struct proc;
282 struct msghdr;
283 struct stat;
284 struct knote;
285 
286 /*
287  * File operations on sockets.
288  */
289 int	soo_read(struct file *, struct uio *, int);
290 int	soo_write(struct file *, struct uio *, int);
291 int	soo_ioctl(struct file *, u_long, caddr_t, struct proc *);
292 int	soo_kqfilter(struct file *, struct knote *);
293 int 	soo_close(struct file *, struct proc *);
294 int	soo_stat(struct file *, struct stat *, struct proc *);
295 void	sbappend(struct socket *, struct sockbuf *, struct mbuf *);
296 void	sbappendstream(struct socket *, struct sockbuf *, struct mbuf *);
297 int	sbappendaddr(struct socket *, struct sockbuf *,
298 	    const struct sockaddr *, struct mbuf *, struct mbuf *);
299 int	sbappendcontrol(struct socket *, struct sockbuf *, struct mbuf *,
300 	    struct mbuf *);
301 void	sbappendrecord(struct socket *, struct sockbuf *, struct mbuf *);
302 void	sbcompress(struct socket *, struct sockbuf *, struct mbuf *,
303 	    struct mbuf *);
304 struct mbuf *
305 	sbcreatecontrol(const void *, size_t, int, int);
306 void	sbdrop(struct socket *, struct sockbuf *, int);
307 void	sbdroprecord(struct socket *, struct sockbuf *);
308 void	sbflush(struct socket *, struct sockbuf *);
309 void	sbrelease(struct socket *, struct sockbuf *);
310 int	sbcheckreserve(u_long, u_long);
311 int	sbchecklowmem(void);
312 int	sbreserve(struct socket *, struct sockbuf *, u_long);
313 int	sbwait(struct socket *, struct sockbuf *);
314 int	sb_lock(struct sockbuf *);
315 void	soinit(void);
316 void	soabort(struct socket *);
317 int	soaccept(struct socket *, struct mbuf *);
318 int	sobind(struct socket *, struct mbuf *, struct proc *);
319 void	socantrcvmore(struct socket *);
320 void	socantsendmore(struct socket *);
321 int	soclose(struct socket *, int);
322 int	soconnect(struct socket *, struct mbuf *);
323 int	soconnect2(struct socket *, struct socket *);
324 int	socreate(int, struct socket **, int, int);
325 int	sodisconnect(struct socket *);
326 struct socket *soalloc(int);
327 void	sofree(struct socket *, int);
328 int	sogetopt(struct socket *, int, int, struct mbuf *);
329 void	sohasoutofband(struct socket *);
330 void	soisconnected(struct socket *);
331 void	soisconnecting(struct socket *);
332 void	soisdisconnected(struct socket *);
333 void	soisdisconnecting(struct socket *);
334 int	solisten(struct socket *, int);
335 struct socket *sonewconn(struct socket *, int);
336 void	soqinsque(struct socket *, struct socket *, int);
337 int	soqremque(struct socket *, int);
338 int	soreceive(struct socket *, struct mbuf **, struct uio *,
339 	    struct mbuf **, struct mbuf **, int *, socklen_t);
340 int	soreserve(struct socket *, u_long, u_long);
341 int	sosend(struct socket *, struct mbuf *, struct uio *,
342 	    struct mbuf *, struct mbuf *, int);
343 int	sosetopt(struct socket *, int, int, struct mbuf *);
344 int	soshutdown(struct socket *, int);
345 void	sowakeup(struct socket *, struct sockbuf *);
346 void	sorwakeup(struct socket *);
347 void	sowwakeup(struct socket *);
348 int	sockargs(struct mbuf **, const void *, size_t, int);
349 
350 int	sosleep_nsec(struct socket *, void *, int, const char *, uint64_t);
351 void	solock(struct socket *);
352 int	solock_persocket(struct socket *);
353 void	solock_pair(struct socket *, struct socket *);
354 void	sounlock(struct socket *);
355 
356 int	sendit(struct proc *, int, struct msghdr *, int, register_t *);
357 int	recvit(struct proc *, int, struct msghdr *, caddr_t, register_t *);
358 int	doaccept(struct proc *, int, struct sockaddr *, socklen_t *, int,
359 	    register_t *);
360 
361 #ifdef SOCKBUF_DEBUG
362 void	sblastrecordchk(struct sockbuf *, const char *);
363 #define	SBLASTRECORDCHK(sb, where)	sblastrecordchk((sb), (where))
364 
365 void	sblastmbufchk(struct sockbuf *, const char *);
366 #define	SBLASTMBUFCHK(sb, where)	sblastmbufchk((sb), (where))
367 void	sbcheck(struct socket *, struct sockbuf *);
368 #define	SBCHECK(so, sb)			sbcheck((so), (sb))
369 #else
370 #define	SBLASTRECORDCHK(sb, where)	/* nothing */
371 #define	SBLASTMBUFCHK(sb, where)	/* nothing */
372 #define	SBCHECK(so, sb)			/* nothing */
373 #endif /* SOCKBUF_DEBUG */
374 
375 #endif /* _KERNEL */
376 
377 #endif /* _SYS_SOCKETVAR_H_ */
378