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