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_
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_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
176 void soassertlocked(struct socket *);
177 void soassertlocked_readonly(struct socket *);
178
179 static inline void
soref(struct socket * so)180 soref(struct socket *so)
181 {
182 refcnt_take(&so->so_refcnt);
183 }
184
185 static inline void
sorele(struct socket * so)186 sorele(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
198 static inline void
sb_mtx_lock(struct sockbuf * sb)199 sb_mtx_lock(struct sockbuf *sb)
200 {
201 if (sb->sb_flags & SB_MTXLOCK)
202 mtx_enter(&sb->sb_mtx);
203 }
204
205 static inline void
sb_mtx_unlock(struct sockbuf * sb)206 sb_mtx_unlock(struct sockbuf *sb)
207 {
208 if (sb->sb_flags & SB_MTXLOCK)
209 mtx_leave(&sb->sb_mtx);
210 }
211
212 void sbmtxassertlocked(struct socket *so, struct sockbuf *);
213
214 /*
215 * Do we need to notify the other side when I/O is possible?
216 */
217 static inline int
sb_notify(struct socket * so,struct sockbuf * sb)218 sb_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
239 static inline long
sbspace(struct socket * so,struct sockbuf * sb)240 sbspace(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? */
259 static inline int
soreadable(struct socket * so)260 soreadable(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? */
270 static inline int
sowriteable(struct socket * so)271 sowriteable(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 */
281 static inline void
sballoc(struct socket * so,struct sockbuf * sb,struct mbuf * m)282 sballoc(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 */
293 static inline void
sbfree(struct socket * so,struct sockbuf * sb,struct mbuf * m)294 sbfree(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 */
316 int sblock(struct sockbuf *, int);
317
318 /* release lock on sockbuf sb */
319 void 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
328 extern u_long sb_max;
329
330 extern struct pool socket_pool;
331
332 struct mbuf;
333 struct sockaddr;
334 struct proc;
335 struct msghdr;
336 struct stat;
337 struct knote;
338
339 /*
340 * File operations on sockets.
341 */
342 int soo_read(struct file *, struct uio *, int);
343 int soo_write(struct file *, struct uio *, int);
344 int soo_ioctl(struct file *, u_long, caddr_t, struct proc *);
345 int soo_kqfilter(struct file *, struct knote *);
346 int soo_close(struct file *, struct proc *);
347 int soo_stat(struct file *, struct stat *, struct proc *);
348 void sbappend(struct socket *, struct sockbuf *, struct mbuf *);
349 void sbappendstream(struct socket *, struct sockbuf *, struct mbuf *);
350 int sbappendaddr(struct socket *, struct sockbuf *,
351 const struct sockaddr *, struct mbuf *, struct mbuf *);
352 int sbappendcontrol(struct socket *, struct sockbuf *, struct mbuf *,
353 struct mbuf *);
354 void sbappendrecord(struct socket *, struct sockbuf *, struct mbuf *);
355 void sbcompress(struct socket *, struct sockbuf *, struct mbuf *,
356 struct mbuf *);
357 struct mbuf *
358 sbcreatecontrol(const void *, size_t, int, int);
359 void sbdrop(struct socket *, struct sockbuf *, int);
360 void sbdroprecord(struct socket *, struct sockbuf *);
361 void sbflush(struct socket *, struct sockbuf *);
362 void sbrelease(struct socket *, struct sockbuf *);
363 int sbcheckreserve(u_long, u_long);
364 int sbchecklowmem(void);
365 int sbreserve(struct socket *, struct sockbuf *, u_long);
366 int sbwait(struct socket *, struct sockbuf *);
367 void soinit(void);
368 void soabort(struct socket *);
369 int soaccept(struct socket *, struct mbuf *);
370 int sobind(struct socket *, struct mbuf *, struct proc *);
371 void socantrcvmore(struct socket *);
372 void socantsendmore(struct socket *);
373 int soclose(struct socket *, int);
374 int soconnect(struct socket *, struct mbuf *);
375 int soconnect2(struct socket *, struct socket *);
376 int socreate(int, struct socket **, int, int);
377 int sodisconnect(struct socket *);
378 struct socket *soalloc(const struct protosw *, int);
379 void sofree(struct socket *, int);
380 int sogetopt(struct socket *, int, int, struct mbuf *);
381 void sohasoutofband(struct socket *);
382 void soisconnected(struct socket *);
383 void soisconnecting(struct socket *);
384 void soisdisconnected(struct socket *);
385 void soisdisconnecting(struct socket *);
386 int solisten(struct socket *, int);
387 struct socket *sonewconn(struct socket *, int, int);
388 void soqinsque(struct socket *, struct socket *, int);
389 int soqremque(struct socket *, int);
390 int soreceive(struct socket *, struct mbuf **, struct uio *,
391 struct mbuf **, struct mbuf **, int *, socklen_t);
392 int soreserve(struct socket *, u_long, u_long);
393 int sosend(struct socket *, struct mbuf *, struct uio *,
394 struct mbuf *, struct mbuf *, int);
395 int sosetopt(struct socket *, int, int, struct mbuf *);
396 int soshutdown(struct socket *, int);
397 void sowakeup(struct socket *, struct sockbuf *);
398 void sorwakeup(struct socket *);
399 void sowwakeup(struct socket *);
400 int sockargs(struct mbuf **, const void *, size_t, int);
401
402 int sosleep_nsec(struct socket *, void *, int, const char *, uint64_t);
403 void solock(struct socket *);
404 void solock_shared(struct socket *);
405 int solock_persocket(struct socket *);
406 void solock_pair(struct socket *, struct socket *);
407 void sounlock(struct socket *);
408 void sounlock_shared(struct socket *);
409
410 int sendit(struct proc *, int, struct msghdr *, int, register_t *);
411 int recvit(struct proc *, int, struct msghdr *, caddr_t, register_t *);
412 int doaccept(struct proc *, int, struct sockaddr *, socklen_t *, int,
413 register_t *);
414
415 #ifdef SOCKBUF_DEBUG
416 void sblastrecordchk(struct sockbuf *, const char *);
417 #define SBLASTRECORDCHK(sb, where) sblastrecordchk((sb), (where))
418
419 void sblastmbufchk(struct sockbuf *, const char *);
420 #define SBLASTMBUFCHK(sb, where) sblastmbufchk((sb), (where))
421 void 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