xref: /openbsd/sys/sys/socketvar.h (revision bb0cd11a)
1 /*	$OpenBSD: socketvar.h,v 1.130 2024/05/03 17:43:09 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/event.h>
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/mutex.h>
44 #include <sys/rwlock.h>
45 #include <sys/refcnt.h>
46 
47 #ifndef	_SOCKLEN_T_DEFINED_
48 #define	_SOCKLEN_T_DEFINED_
49 typedef	__socklen_t	socklen_t;	/* length type for network syscalls */
50 #endif
51 
52 TAILQ_HEAD(soqhead, socket);
53 
54 /*
55  * Kernel structure per socket.
56  * Contains send and receive buffer queues,
57  * handle on protocol and pointer to protocol
58  * private data and error information.
59  */
60 struct socket {
61 	const struct protosw *so_proto;	/* protocol handle */
62 	struct rwlock so_lock;		/* this socket lock */
63 	struct refcnt so_refcnt;	/* references to this socket */
64 	void	*so_pcb;		/* protocol control block */
65 	u_int	so_state;		/* internal state flags SS_*, below */
66 	short	so_type;		/* generic type, see socket.h */
67 	short	so_options;		/* from socket call, see socket.h */
68 	short	so_linger;		/* time to linger while closing */
69 /*
70  * Variables for connection queueing.
71  * Socket where accepts occur is so_head in all subsidiary sockets.
72  * If so_head is 0, socket is not related to an accept.
73  * For head socket so_q0 queues partially completed connections,
74  * while so_q is a queue of connections ready to be accepted.
75  * If a connection is aborted and it has so_head set, then
76  * it has to be pulled out of either so_q0 or so_q.
77  * We allow connections to queue up based on current queue lengths
78  * and limit on number of queued connections for this socket.
79  */
80 	struct	socket	*so_head;	/* back pointer to accept socket */
81 	struct	soqhead	*so_onq;	/* queue (q or q0) that we're on */
82 	struct	soqhead	so_q0;		/* queue of partial connections */
83 	struct	soqhead	so_q;		/* queue of incoming connections */
84 	struct	sigio_ref so_sigio;	/* async I/O registration */
85 	TAILQ_ENTRY(socket) so_qe;	/* our queue entry (q or q0) */
86 	short	so_q0len;		/* partials on so_q0 */
87 	short	so_qlen;		/* number of connections on so_q */
88 	short	so_qlimit;		/* max number queued connections */
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 		struct rwlock sb_lock;
109 		struct mutex  sb_mtx;
110 /* The following fields are all zeroed on flush. */
111 #define	sb_startzero	sb_cc
112 		u_long	sb_cc;		/* actual chars in buffer */
113 		u_long	sb_datacc;	/* data only chars in buffer */
114 		u_long	sb_hiwat;	/* max actual char count */
115 		u_long  sb_wat;		/* default watermark */
116 		u_long	sb_mbcnt;	/* chars of mbufs used */
117 		u_long	sb_mbmax;	/* max chars of mbufs to use */
118 		long	sb_lowat;	/* low water mark */
119 		struct mbuf *sb_mb;	/* the mbuf chain */
120 		struct mbuf *sb_mbtail;	/* the last mbuf in the chain */
121 		struct mbuf *sb_lastrecord;/* first mbuf of last record in
122 					      socket buffer */
123 		short	sb_flags;	/* flags, see below */
124 /* End area that is zeroed on flush. */
125 #define	sb_endzero	sb_flags
126 		short	sb_state;	/* socket state on sockbuf */
127 		uint64_t sb_timeo_nsecs;/* timeout for read/write */
128 		struct klist sb_klist;	/* process selecting read/write */
129 	} so_rcv, so_snd;
130 #define SB_MAX		(2*1024*1024)	/* default for max chars in sockbuf */
131 #define SB_LOCK		0x0001		/* lock on data queue */
132 #define SB_WANT		0x0002		/* someone is waiting to lock */
133 #define SB_WAIT		0x0004		/* someone is waiting for data/space */
134 #define SB_ASYNC	0x0010		/* ASYNC I/O, need signals */
135 #define SB_SPLICE	0x0020		/* buffer is splice source or drain */
136 #define SB_NOINTR	0x0040		/* operations not interruptible */
137 #define SB_MTXLOCK	0x0080		/* sblock() doesn't need solock() */
138 
139 	void	(*so_upcall)(struct socket *so, caddr_t arg, int waitf);
140 	caddr_t	so_upcallarg;		/* Arg for above */
141 	uid_t	so_euid, so_ruid;	/* who opened the socket */
142 	gid_t	so_egid, so_rgid;
143 	pid_t	so_cpid;		/* pid of process that opened socket */
144 };
145 
146 /*
147  * Socket state bits.
148  *
149  * NOTE: The following states should be used with corresponding socket's
150  * buffer `sb_state' only:
151  *
152  *	SS_CANTSENDMORE		with `so_snd'
153  *	SS_ISSENDING		with `so_snd'
154  *	SS_CANTRCVMORE		with `so_rcv'
155  *	SS_RCVATMARK		with `so_rcv'
156  */
157 
158 #define	SS_NOFDREF		0x001	/* no file table ref any more */
159 #define	SS_ISCONNECTED		0x002	/* socket connected to a peer */
160 #define	SS_ISCONNECTING		0x004	/* in process of connecting to peer */
161 #define	SS_ISDISCONNECTING	0x008	/* in process of disconnecting */
162 #define	SS_CANTSENDMORE		0x010	/* can't send more data to peer */
163 #define	SS_CANTRCVMORE		0x020	/* can't receive more data from peer */
164 #define	SS_RCVATMARK		0x040	/* at mark on input */
165 #define	SS_ISDISCONNECTED	0x800	/* socket disconnected from peer */
166 
167 #define	SS_PRIV			0x080	/* privileged for broadcast, raw... */
168 #define	SS_CONNECTOUT		0x1000	/* connect, not accept, at this end */
169 #define	SS_ISSENDING		0x2000	/* hint for lower layer */
170 #define	SS_DNS			0x4000	/* created using SOCK_DNS socket(2) */
171 #define	SS_YP			0x8000	/* created using ypconnect(2) */
172 
173 #ifdef _KERNEL
174 
175 #include <sys/protosw.h>
176 #include <lib/libkern/libkern.h>
177 
178 void	soassertlocked(struct socket *);
179 void	soassertlocked_readonly(struct socket *);
180 
181 static inline void
soref(struct socket * so)182 soref(struct socket *so)
183 {
184 	refcnt_take(&so->so_refcnt);
185 }
186 
187 static inline void
sorele(struct socket * so)188 sorele(struct socket *so)
189 {
190 	refcnt_rele_wake(&so->so_refcnt);
191 }
192 
193 /*
194  * Macros for sockets and socket buffering.
195  */
196 
197 #define isspliced(so)		((so)->so_sp && (so)->so_sp->ssp_socket)
198 #define issplicedback(so)	((so)->so_sp && (so)->so_sp->ssp_soback)
199 
200 static inline void
sb_mtx_lock(struct sockbuf * sb)201 sb_mtx_lock(struct sockbuf *sb)
202 {
203 	if (sb->sb_flags & SB_MTXLOCK)
204 		mtx_enter(&sb->sb_mtx);
205 }
206 
207 static inline void
sb_mtx_unlock(struct sockbuf * sb)208 sb_mtx_unlock(struct sockbuf *sb)
209 {
210 	if (sb->sb_flags & SB_MTXLOCK)
211 		mtx_leave(&sb->sb_mtx);
212 }
213 
214 void	sbmtxassertlocked(struct socket *so, struct sockbuf *);
215 
216 /*
217  * Do we need to notify the other side when I/O is possible?
218  */
219 static inline int
sb_notify(struct socket * so,struct sockbuf * sb)220 sb_notify(struct socket *so, struct sockbuf *sb)
221 {
222 	int rv;
223 
224 	soassertlocked(so);
225 
226 	mtx_enter(&sb->sb_mtx);
227 	rv = ((sb->sb_flags & (SB_WAIT|SB_ASYNC|SB_SPLICE)) != 0 ||
228 	    !klist_empty(&sb->sb_klist));
229 	mtx_leave(&sb->sb_mtx);
230 
231 	return rv;
232 }
233 
234 /*
235  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
236  * This is problematical if the fields are unsigned, as the space might
237  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
238  * overflow and return 0.
239  */
240 
241 static inline long
sbspace(struct socket * so,struct sockbuf * sb)242 sbspace(struct socket *so, struct sockbuf *sb)
243 {
244 	if (sb->sb_flags & SB_MTXLOCK)
245 		sbmtxassertlocked(so, sb);
246 	else
247 		soassertlocked_readonly(so);
248 
249 	return lmin(sb->sb_hiwat - sb->sb_cc, sb->sb_mbmax - sb->sb_mbcnt);
250 }
251 
252 /* do we have to send all at once on a socket? */
253 #define	sosendallatonce(so) \
254     ((so)->so_proto->pr_flags & PR_ATOMIC)
255 
256 /* are we sending on this socket? */
257 #define	soissending(so) \
258     ((so)->so_snd.sb_state & SS_ISSENDING)
259 
260 /* can we read something from so? */
261 static inline int
soreadable(struct socket * so)262 soreadable(struct socket *so)
263 {
264 	soassertlocked_readonly(so);
265 	if (isspliced(so))
266 		return 0;
267 	return (so->so_rcv.sb_state & SS_CANTRCVMORE) || so->so_qlen ||
268 	    so->so_error || so->so_rcv.sb_cc >= so->so_rcv.sb_lowat;
269 }
270 
271 /* can we write something to so? */
272 static inline int
sowriteable(struct socket * so)273 sowriteable(struct socket *so)
274 {
275 	soassertlocked_readonly(so);
276 	return ((sbspace(so, &so->so_snd) >= so->so_snd.sb_lowat &&
277 	    ((so->so_state & SS_ISCONNECTED) ||
278 	    (so->so_proto->pr_flags & PR_CONNREQUIRED)==0)) ||
279 	    (so->so_snd.sb_state & SS_CANTSENDMORE) || so->so_error);
280 }
281 
282 /* adjust counters in sb reflecting allocation of m */
283 static inline void
sballoc(struct socket * so,struct sockbuf * sb,struct mbuf * m)284 sballoc(struct socket *so, struct sockbuf *sb, struct mbuf *m)
285 {
286 	sb->sb_cc += m->m_len;
287 	if (m->m_type != MT_CONTROL && m->m_type != MT_SONAME)
288 		sb->sb_datacc += m->m_len;
289 	sb->sb_mbcnt += MSIZE;
290 	if (m->m_flags & M_EXT)
291 		sb->sb_mbcnt += m->m_ext.ext_size;
292 }
293 
294 /* adjust counters in sb reflecting freeing of m */
295 static inline void
sbfree(struct socket * so,struct sockbuf * sb,struct mbuf * m)296 sbfree(struct socket *so, struct sockbuf *sb, struct mbuf *m)
297 {
298 	sb->sb_cc -= m->m_len;
299 	if (m->m_type != MT_CONTROL && m->m_type != MT_SONAME)
300 		sb->sb_datacc -= m->m_len;
301 	sb->sb_mbcnt -= MSIZE;
302 	if (m->m_flags & M_EXT)
303 		sb->sb_mbcnt -= m->m_ext.ext_size;
304 }
305 
306 /*
307  * Flags to sblock()
308  */
309 #define SBL_WAIT	0x01	/* Wait if lock not immediately available. */
310 #define SBL_NOINTR	0x02	/* Enforce non-interruptible sleep. */
311 
312 /*
313  * Set lock on sockbuf sb; sleep if lock is already held.
314  * Unless SB_NOINTR is set on sockbuf or SBL_NOINTR passed,
315  * sleep is interruptible. Returns error without lock if
316  * sleep is interrupted.
317  */
318 int sblock(struct socket *, struct sockbuf *, int);
319 
320 /* release lock on sockbuf sb */
321 void sbunlock(struct socket *, struct sockbuf *);
322 void sbunlock_locked(struct socket *, struct sockbuf *);
323 
324 #define	SB_EMPTY_FIXUP(sb) do {						\
325 	if ((sb)->sb_mb == NULL) {					\
326 		(sb)->sb_mbtail = NULL;					\
327 		(sb)->sb_lastrecord = NULL;				\
328 	}								\
329 } while (/*CONSTCOND*/0)
330 
331 extern u_long sb_max;
332 
333 extern struct pool	socket_pool;
334 
335 struct mbuf;
336 struct sockaddr;
337 struct proc;
338 struct msghdr;
339 struct stat;
340 struct knote;
341 
342 /*
343  * File operations on sockets.
344  */
345 int	soo_read(struct file *, struct uio *, int);
346 int	soo_write(struct file *, struct uio *, int);
347 int	soo_ioctl(struct file *, u_long, caddr_t, struct proc *);
348 int	soo_kqfilter(struct file *, struct knote *);
349 int 	soo_close(struct file *, struct proc *);
350 int	soo_stat(struct file *, struct stat *, struct proc *);
351 void	sbappend(struct socket *, struct sockbuf *, struct mbuf *);
352 void	sbappendstream(struct socket *, struct sockbuf *, struct mbuf *);
353 int	sbappendaddr(struct socket *, struct sockbuf *,
354 	    const struct sockaddr *, struct mbuf *, struct mbuf *);
355 int	sbappendcontrol(struct socket *, struct sockbuf *, struct mbuf *,
356 	    struct mbuf *);
357 void	sbappendrecord(struct socket *, struct sockbuf *, struct mbuf *);
358 void	sbcompress(struct socket *, struct sockbuf *, struct mbuf *,
359 	    struct mbuf *);
360 struct mbuf *
361 	sbcreatecontrol(const void *, size_t, int, int);
362 void	sbdrop(struct socket *, struct sockbuf *, int);
363 void	sbdroprecord(struct socket *, struct sockbuf *);
364 void	sbflush(struct socket *, struct sockbuf *);
365 void	sbrelease(struct socket *, struct sockbuf *);
366 int	sbcheckreserve(u_long, u_long);
367 int	sbchecklowmem(void);
368 int	sbreserve(struct socket *, struct sockbuf *, u_long);
369 int	sbwait(struct socket *, struct sockbuf *);
370 int	sbwait_locked(struct socket *, struct sockbuf *);
371 void	soinit(void);
372 void	soabort(struct socket *);
373 int	soaccept(struct socket *, struct mbuf *);
374 int	sobind(struct socket *, struct mbuf *, struct proc *);
375 void	socantrcvmore(struct socket *);
376 void	socantsendmore(struct socket *);
377 int	soclose(struct socket *, int);
378 int	soconnect(struct socket *, struct mbuf *);
379 int	soconnect2(struct socket *, struct socket *);
380 int	socreate(int, struct socket **, int, int);
381 int	sodisconnect(struct socket *);
382 struct socket *soalloc(const struct protosw *, int);
383 void	sofree(struct socket *, int);
384 int	sogetopt(struct socket *, int, int, struct mbuf *);
385 void	sohasoutofband(struct socket *);
386 void	soisconnected(struct socket *);
387 void	soisconnecting(struct socket *);
388 void	soisdisconnected(struct socket *);
389 void	soisdisconnecting(struct socket *);
390 int	solisten(struct socket *, int);
391 struct socket *sonewconn(struct socket *, int, int);
392 void	soqinsque(struct socket *, struct socket *, int);
393 int	soqremque(struct socket *, int);
394 int	soreceive(struct socket *, struct mbuf **, struct uio *,
395 	    struct mbuf **, struct mbuf **, int *, socklen_t);
396 int	soreserve(struct socket *, u_long, u_long);
397 int	sosend(struct socket *, struct mbuf *, struct uio *,
398 	    struct mbuf *, struct mbuf *, int);
399 int	sosetopt(struct socket *, int, int, struct mbuf *);
400 int	soshutdown(struct socket *, int);
401 void	sowakeup(struct socket *, struct sockbuf *);
402 void	sorwakeup(struct socket *);
403 void	sowwakeup(struct socket *);
404 int	sockargs(struct mbuf **, const void *, size_t, int);
405 
406 int	sosleep_nsec(struct socket *, void *, int, const char *, uint64_t);
407 void	solock(struct socket *);
408 void	solock_shared(struct socket *);
409 int	solock_persocket(struct socket *);
410 void	solock_pair(struct socket *, struct socket *);
411 void	sounlock(struct socket *);
412 void	sounlock_shared(struct socket *);
413 
414 int	sendit(struct proc *, int, struct msghdr *, int, register_t *);
415 int	recvit(struct proc *, int, struct msghdr *, caddr_t, register_t *);
416 int	doaccept(struct proc *, int, struct sockaddr *, socklen_t *, int,
417 	    register_t *);
418 
419 #ifdef SOCKBUF_DEBUG
420 void	sblastrecordchk(struct sockbuf *, const char *);
421 #define	SBLASTRECORDCHK(sb, where)	sblastrecordchk((sb), (where))
422 
423 void	sblastmbufchk(struct sockbuf *, const char *);
424 #define	SBLASTMBUFCHK(sb, where)	sblastmbufchk((sb), (where))
425 void	sbcheck(struct socket *, struct sockbuf *);
426 #define	SBCHECK(so, sb)			sbcheck((so), (sb))
427 #else
428 #define	SBLASTRECORDCHK(sb, where)	/* nothing */
429 #define	SBLASTMBUFCHK(sb, where)	/* nothing */
430 #define	SBCHECK(so, sb)			/* nothing */
431 #endif /* SOCKBUF_DEBUG */
432 
433 #endif /* _KERNEL */
434 
435 #endif /* _SYS_SOCKETVAR_H_ */
436