1/*	$OpenBSD: socketvar.h,v 1.131 2024/05/17 19:11:14 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_
49typedef	__socklen_t	socklen_t;	/* length type for network syscalls */
50#endif
51
52TAILQ_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 */
60struct 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_WAIT		0x0001		/* someone is waiting for data/space */
132#define SB_ASYNC	0x0002		/* ASYNC I/O, need signals */
133#define SB_SPLICE	0x0004		/* buffer is splice source or drain */
134#define SB_NOINTR	0x0008		/* operations not interruptible */
135#define SB_MTXLOCK	0x0010		/* sblock() doesn't need solock() */
136
137	void	(*so_upcall)(struct socket *so, caddr_t arg, int waitf);
138	caddr_t	so_upcallarg;		/* Arg for above */
139	uid_t	so_euid, so_ruid;	/* who opened the socket */
140	gid_t	so_egid, so_rgid;
141	pid_t	so_cpid;		/* pid of process that opened socket */
142};
143
144/*
145 * Socket state bits.
146 *
147 * NOTE: The following states should be used with corresponding socket's
148 * buffer `sb_state' only:
149 *
150 *	SS_CANTSENDMORE		with `so_snd'
151 *	SS_ISSENDING		with `so_snd'
152 *	SS_CANTRCVMORE		with `so_rcv'
153 *	SS_RCVATMARK		with `so_rcv'
154 */
155
156#define	SS_NOFDREF		0x001	/* no file table ref any more */
157#define	SS_ISCONNECTED		0x002	/* socket connected to a peer */
158#define	SS_ISCONNECTING		0x004	/* in process of connecting to peer */
159#define	SS_ISDISCONNECTING	0x008	/* in process of disconnecting */
160#define	SS_CANTSENDMORE		0x010	/* can't send more data to peer */
161#define	SS_CANTRCVMORE		0x020	/* can't receive more data from peer */
162#define	SS_RCVATMARK		0x040	/* at mark on input */
163#define	SS_ISDISCONNECTED	0x800	/* socket disconnected from peer */
164
165#define	SS_PRIV			0x080	/* privileged for broadcast, raw... */
166#define	SS_CONNECTOUT		0x1000	/* connect, not accept, at this end */
167#define	SS_ISSENDING		0x2000	/* hint for lower layer */
168#define	SS_DNS			0x4000	/* created using SOCK_DNS socket(2) */
169#define	SS_YP			0x8000	/* created using ypconnect(2) */
170
171#ifdef _KERNEL
172
173#include <sys/protosw.h>
174#include <lib/libkern/libkern.h>
175
176void	soassertlocked(struct socket *);
177void	soassertlocked_readonly(struct socket *);
178
179static inline void
180soref(struct socket *so)
181{
182	refcnt_take(&so->so_refcnt);
183}
184
185static inline void
186sorele(struct socket *so)
187{
188	refcnt_rele_wake(&so->so_refcnt);
189}
190
191/*
192 * Macros for sockets and socket buffering.
193 */
194
195#define isspliced(so)		((so)->so_sp && (so)->so_sp->ssp_socket)
196#define issplicedback(so)	((so)->so_sp && (so)->so_sp->ssp_soback)
197
198static inline void
199sb_mtx_lock(struct sockbuf *sb)
200{
201	if (sb->sb_flags & SB_MTXLOCK)
202		mtx_enter(&sb->sb_mtx);
203}
204
205static inline void
206sb_mtx_unlock(struct sockbuf *sb)
207{
208	if (sb->sb_flags & SB_MTXLOCK)
209		mtx_leave(&sb->sb_mtx);
210}
211
212void	sbmtxassertlocked(struct socket *so, struct sockbuf *);
213
214/*
215 * Do we need to notify the other side when I/O is possible?
216 */
217static inline int
218sb_notify(struct socket *so, struct sockbuf *sb)
219{
220	int rv;
221
222	soassertlocked(so);
223
224	mtx_enter(&sb->sb_mtx);
225	rv = ((sb->sb_flags & (SB_WAIT|SB_ASYNC|SB_SPLICE)) != 0 ||
226	    !klist_empty(&sb->sb_klist));
227	mtx_leave(&sb->sb_mtx);
228
229	return rv;
230}
231
232/*
233 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
234 * This is problematical if the fields are unsigned, as the space might
235 * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
236 * overflow and return 0.
237 */
238
239static inline long
240sbspace(struct socket *so, struct sockbuf *sb)
241{
242	if (sb->sb_flags & SB_MTXLOCK)
243		sbmtxassertlocked(so, sb);
244	else
245		soassertlocked_readonly(so);
246
247	return lmin(sb->sb_hiwat - sb->sb_cc, sb->sb_mbmax - sb->sb_mbcnt);
248}
249
250/* do we have to send all at once on a socket? */
251#define	sosendallatonce(so) \
252    ((so)->so_proto->pr_flags & PR_ATOMIC)
253
254/* are we sending on this socket? */
255#define	soissending(so) \
256    ((so)->so_snd.sb_state & SS_ISSENDING)
257
258/* can we read something from so? */
259static inline int
260soreadable(struct socket *so)
261{
262	soassertlocked_readonly(so);
263	if (isspliced(so))
264		return 0;
265	return (so->so_rcv.sb_state & SS_CANTRCVMORE) || so->so_qlen ||
266	    so->so_error || so->so_rcv.sb_cc >= so->so_rcv.sb_lowat;
267}
268
269/* can we write something to so? */
270static inline int
271sowriteable(struct socket *so)
272{
273	soassertlocked_readonly(so);
274	return ((sbspace(so, &so->so_snd) >= so->so_snd.sb_lowat &&
275	    ((so->so_state & SS_ISCONNECTED) ||
276	    (so->so_proto->pr_flags & PR_CONNREQUIRED)==0)) ||
277	    (so->so_snd.sb_state & SS_CANTSENDMORE) || so->so_error);
278}
279
280/* adjust counters in sb reflecting allocation of m */
281static inline void
282sballoc(struct socket *so, struct sockbuf *sb, struct mbuf *m)
283{
284	sb->sb_cc += m->m_len;
285	if (m->m_type != MT_CONTROL && m->m_type != MT_SONAME)
286		sb->sb_datacc += m->m_len;
287	sb->sb_mbcnt += MSIZE;
288	if (m->m_flags & M_EXT)
289		sb->sb_mbcnt += m->m_ext.ext_size;
290}
291
292/* adjust counters in sb reflecting freeing of m */
293static inline void
294sbfree(struct socket *so, struct sockbuf *sb, struct mbuf *m)
295{
296	sb->sb_cc -= m->m_len;
297	if (m->m_type != MT_CONTROL && m->m_type != MT_SONAME)
298		sb->sb_datacc -= m->m_len;
299	sb->sb_mbcnt -= MSIZE;
300	if (m->m_flags & M_EXT)
301		sb->sb_mbcnt -= m->m_ext.ext_size;
302}
303
304/*
305 * Flags to sblock()
306 */
307#define SBL_WAIT	0x01	/* Wait if lock not immediately available. */
308#define SBL_NOINTR	0x02	/* Enforce non-interruptible sleep. */
309
310/*
311 * Set lock on sockbuf sb; sleep if lock is already held.
312 * Unless SB_NOINTR is set on sockbuf or SBL_NOINTR passed,
313 * sleep is interruptible. Returns error without lock if
314 * sleep is interrupted.
315 */
316int sblock(struct sockbuf *, int);
317
318/* release lock on sockbuf sb */
319void sbunlock(struct sockbuf *);
320
321#define	SB_EMPTY_FIXUP(sb) do {						\
322	if ((sb)->sb_mb == NULL) {					\
323		(sb)->sb_mbtail = NULL;					\
324		(sb)->sb_lastrecord = NULL;				\
325	}								\
326} while (/*CONSTCOND*/0)
327
328extern u_long sb_max;
329
330extern struct pool	socket_pool;
331
332struct mbuf;
333struct sockaddr;
334struct proc;
335struct msghdr;
336struct stat;
337struct knote;
338
339/*
340 * File operations on sockets.
341 */
342int	soo_read(struct file *, struct uio *, int);
343int	soo_write(struct file *, struct uio *, int);
344int	soo_ioctl(struct file *, u_long, caddr_t, struct proc *);
345int	soo_kqfilter(struct file *, struct knote *);
346int 	soo_close(struct file *, struct proc *);
347int	soo_stat(struct file *, struct stat *, struct proc *);
348void	sbappend(struct socket *, struct sockbuf *, struct mbuf *);
349void	sbappendstream(struct socket *, struct sockbuf *, struct mbuf *);
350int	sbappendaddr(struct socket *, struct sockbuf *,
351	    const struct sockaddr *, struct mbuf *, struct mbuf *);
352int	sbappendcontrol(struct socket *, struct sockbuf *, struct mbuf *,
353	    struct mbuf *);
354void	sbappendrecord(struct socket *, struct sockbuf *, struct mbuf *);
355void	sbcompress(struct socket *, struct sockbuf *, struct mbuf *,
356	    struct mbuf *);
357struct mbuf *
358	sbcreatecontrol(const void *, size_t, int, int);
359void	sbdrop(struct socket *, struct sockbuf *, int);
360void	sbdroprecord(struct socket *, struct sockbuf *);
361void	sbflush(struct socket *, struct sockbuf *);
362void	sbrelease(struct socket *, struct sockbuf *);
363int	sbcheckreserve(u_long, u_long);
364int	sbchecklowmem(void);
365int	sbreserve(struct socket *, struct sockbuf *, u_long);
366int	sbwait(struct socket *, struct sockbuf *);
367void	soinit(void);
368void	soabort(struct socket *);
369int	soaccept(struct socket *, struct mbuf *);
370int	sobind(struct socket *, struct mbuf *, struct proc *);
371void	socantrcvmore(struct socket *);
372void	socantsendmore(struct socket *);
373int	soclose(struct socket *, int);
374int	soconnect(struct socket *, struct mbuf *);
375int	soconnect2(struct socket *, struct socket *);
376int	socreate(int, struct socket **, int, int);
377int	sodisconnect(struct socket *);
378struct socket *soalloc(const struct protosw *, int);
379void	sofree(struct socket *, int);
380int	sogetopt(struct socket *, int, int, struct mbuf *);
381void	sohasoutofband(struct socket *);
382void	soisconnected(struct socket *);
383void	soisconnecting(struct socket *);
384void	soisdisconnected(struct socket *);
385void	soisdisconnecting(struct socket *);
386int	solisten(struct socket *, int);
387struct socket *sonewconn(struct socket *, int, int);
388void	soqinsque(struct socket *, struct socket *, int);
389int	soqremque(struct socket *, int);
390int	soreceive(struct socket *, struct mbuf **, struct uio *,
391	    struct mbuf **, struct mbuf **, int *, socklen_t);
392int	soreserve(struct socket *, u_long, u_long);
393int	sosend(struct socket *, struct mbuf *, struct uio *,
394	    struct mbuf *, struct mbuf *, int);
395int	sosetopt(struct socket *, int, int, struct mbuf *);
396int	soshutdown(struct socket *, int);
397void	sowakeup(struct socket *, struct sockbuf *);
398void	sorwakeup(struct socket *);
399void	sowwakeup(struct socket *);
400int	sockargs(struct mbuf **, const void *, size_t, int);
401
402int	sosleep_nsec(struct socket *, void *, int, const char *, uint64_t);
403void	solock(struct socket *);
404void	solock_shared(struct socket *);
405int	solock_persocket(struct socket *);
406void	solock_pair(struct socket *, struct socket *);
407void	sounlock(struct socket *);
408void	sounlock_shared(struct socket *);
409
410int	sendit(struct proc *, int, struct msghdr *, int, register_t *);
411int	recvit(struct proc *, int, struct msghdr *, caddr_t, register_t *);
412int	doaccept(struct proc *, int, struct sockaddr *, socklen_t *, int,
413	    register_t *);
414
415#ifdef SOCKBUF_DEBUG
416void	sblastrecordchk(struct sockbuf *, const char *);
417#define	SBLASTRECORDCHK(sb, where)	sblastrecordchk((sb), (where))
418
419void	sblastmbufchk(struct sockbuf *, const char *);
420#define	SBLASTMBUFCHK(sb, where)	sblastmbufchk((sb), (where))
421void	sbcheck(struct socket *, struct sockbuf *);
422#define	SBCHECK(so, sb)			sbcheck((so), (sb))
423#else
424#define	SBLASTRECORDCHK(sb, where)	/* nothing */
425#define	SBLASTMBUFCHK(sb, where)	/* nothing */
426#define	SBCHECK(so, sb)			/* nothing */
427#endif /* SOCKBUF_DEBUG */
428
429#endif /* _KERNEL */
430
431#endif /* _SYS_SOCKETVAR_H_ */
432