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