xref: /freebsd-14-stable/sys/sys/socketvar.h (revision f2214e48d02c2a251f4aa9e95683d2f5e66337c7)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)socketvar.h	8.3 (Berkeley) 2/19/95
32  */
33 
34 #ifndef _SYS_SOCKETVAR_H_
35 #define _SYS_SOCKETVAR_H_
36 
37 /*
38  * Socket generation count type.  Also used in xinpcb, xtcpcb, xunpcb.
39  */
40 typedef uint64_t so_gen_t;
41 
42 #if defined(_KERNEL) || defined(_WANT_SOCKET)
43 #include <sys/queue.h>			/* for TAILQ macros */
44 #include <sys/selinfo.h>		/* for struct selinfo */
45 #include <sys/_lock.h>
46 #include <sys/_mutex.h>
47 #include <sys/osd.h>
48 #include <sys/_sx.h>
49 #include <sys/sockbuf.h>
50 #include <sys/_task.h>
51 #ifdef _KERNEL
52 #include <sys/caprights.h>
53 #include <sys/sockopt.h>
54 #else
55 #include <stdbool.h>
56 #endif
57 
58 struct vnet;
59 
60 /*
61  * Kernel structure per socket.
62  * Contains send and receive buffer queues,
63  * handle on protocol and pointer to protocol
64  * private data and error information.
65  */
66 typedef	int so_upcall_t(struct socket *, void *, int);
67 typedef	void so_dtor_t(struct socket *);
68 
69 struct socket;
70 
71 enum socket_qstate {
72 	SQ_NONE = 0,
73 	SQ_INCOMP = 0x0800,	/* on sol_incomp */
74 	SQ_COMP = 0x1000,	/* on sol_comp */
75 };
76 
77 
78 struct so_splice {
79 	struct socket *src;
80 	struct socket *dst;
81 	off_t max;		/* maximum bytes to splice, or -1 */
82 	struct mtx mtx;
83 	unsigned int wq_index;
84 	enum so_splice_state {
85 		SPLICE_INIT,	/* embryonic state, don't queue work yet */
86 		SPLICE_IDLE,	/* waiting for work to arrive */
87 		SPLICE_QUEUED,	/* a wakeup has queued some work */
88 		SPLICE_RUNNING,	/* currently transferring data */
89 		SPLICE_CLOSING,	/* waiting for work to drain */
90 		SPLICE_CLOSED,	/* unsplicing, terminal state */
91 		SPLICE_EXCEPTION, /* I/O error or limit, implicit unsplice */
92 	} state;
93 	struct timeout_task timeout;
94 	STAILQ_ENTRY(so_splice) next;
95 };
96 
97 /*-
98  * Locking key to struct socket:
99  * (a) constant after allocation, no locking required.
100  * (b) locked by SOCK_LOCK(so).
101  * (cr) locked by SOCK_RECVBUF_LOCK(so)
102  * (cs) locked by SOCK_SENDBUF_LOCK(so)
103  * (e) locked by SOLISTEN_LOCK() of corresponding listening socket.
104  * (f) not locked since integer reads/writes are atomic.
105  * (g) used only as a sleep/wakeup address, no value.
106  * (h) locked by global mutex so_global_mtx.
107  * (ir,is) locked by recv or send I/O locks.
108  * (k) locked by KTLS workqueue mutex
109  */
110 TAILQ_HEAD(accept_queue, socket);
111 struct socket {
112 	struct mtx	so_lock;
113 	volatile u_int	so_count;	/* (b / refcount) */
114 	struct selinfo	so_rdsel;	/* (b/cr) for so_rcv/so_comp */
115 	struct selinfo	so_wrsel;	/* (b/cs) for so_snd */
116 	int	so_options;		/* (b) from socket call, see socket.h */
117 	short	so_type;		/* (a) generic type, see socket.h */
118 	short	so_state;		/* (b) internal state flags SS_* */
119 	void	*so_pcb;		/* protocol control block */
120 	struct	vnet *so_vnet;		/* (a) network stack instance */
121 	struct	protosw *so_proto;	/* (a) protocol handle */
122 	short	so_linger;		/* time to linger close(2) */
123 	short	so_timeo;		/* (g) connection timeout */
124 	u_short	so_error;		/* (f) error affecting connection */
125 	u_short so_rerror;		/* (f) error affecting connection */
126 	struct	sigio *so_sigio;	/* [sg] information for async I/O or
127 					   out of band data (SIGURG) */
128 	struct	ucred *so_cred;		/* (a) user credentials */
129 	struct	label *so_label;	/* (b) MAC label for socket */
130 	/* NB: generation count must not be first. */
131 	so_gen_t so_gencnt;		/* (h) generation count */
132 	void	*so_emuldata;		/* (b) private data for emulators */
133 	so_dtor_t *so_dtor;		/* (b) optional destructor */
134 	struct	osd	osd;		/* Object Specific extensions */
135 	/*
136 	 * so_fibnum, so_user_cookie and friends can be used to attach
137 	 * some user-specified metadata to a socket, which then can be
138 	 * used by the kernel for various actions.
139 	 * so_user_cookie is used by ipfw/dummynet.
140 	 */
141 	int so_fibnum;		/* routing domain for this socket */
142 	uint32_t so_user_cookie;
143 
144 	int so_ts_clock;	/* type of the clock used for timestamps */
145 	uint32_t so_max_pacing_rate;	/* (f) TX rate limit in bytes/s */
146 	struct so_splice *so_splice;	/* (b) splice state for sink */
147 	struct so_splice *so_splice_back; /* (b) splice state for source */
148 	off_t so_splice_sent;	/* (ir) splice bytes sent so far */
149 
150 	/*
151 	 * Mutexes to prevent interleaving of socket I/O.  These have to be
152 	 * outside of the socket buffers in order to interlock with listen(2).
153 	 */
154 	struct sx so_snd_sx __aligned(CACHE_LINE_SIZE);
155 	struct mtx so_snd_mtx;
156 
157 	struct sx so_rcv_sx __aligned(CACHE_LINE_SIZE);
158 	struct mtx so_rcv_mtx;
159 
160 	union {
161 		/* Regular (data flow) socket. */
162 		struct {
163 			/* (cr, cs) Receive and send buffers. */
164 			struct sockbuf		so_rcv, so_snd;
165 
166 			/* (e) Our place on accept queue. */
167 			TAILQ_ENTRY(socket)	so_list;
168 			struct socket		*so_listen;	/* (b) */
169 			enum socket_qstate so_qstate;		/* (b) */
170 			/* (b) cached MAC label for peer */
171 			struct	label		*so_peerlabel;
172 			u_long	so_oobmark;	/* chars to oob mark */
173 
174 			/* (k) Our place on KTLS RX work queue. */
175 			STAILQ_ENTRY(socket)	so_ktls_rx_list;
176 		};
177 		/*
178 		 * Listening socket, where accepts occur, is so_listen in all
179 		 * subsidiary sockets.  If so_listen is NULL, socket is not
180 		 * related to an accept.  For a listening socket itself
181 		 * sol_incomp queues partially completed connections, while
182 		 * sol_comp is a queue of connections ready to be accepted.
183 		 * If a connection is aborted and it has so_listen set, then
184 		 * it has to be pulled out of either sol_incomp or sol_comp.
185 		 * We allow connections to queue up based on current queue
186 		 * lengths and limit on number of queued connections for this
187 		 * socket.
188 		 */
189 		struct {
190 			/* (e) queue of partial unaccepted connections */
191 			struct accept_queue	sol_incomp;
192 			/* (e) queue of complete unaccepted connections */
193 			struct accept_queue	sol_comp;
194 			u_int	sol_qlen;    /* (e) sol_comp length */
195 			u_int	sol_incqlen; /* (e) sol_incomp length */
196 			u_int	sol_qlimit;  /* (e) queue limit */
197 
198 			/* accept_filter(9) optional data */
199 			struct	accept_filter	*sol_accept_filter;
200 			void	*sol_accept_filter_arg;	/* saved filter args */
201 			char	*sol_accept_filter_str;	/* saved user args */
202 
203 			/* Optional upcall, for kernel socket. */
204 			so_upcall_t	*sol_upcall;	/* (e) */
205 			void		*sol_upcallarg;	/* (e) */
206 
207 			/* Socket buffer parameters, to be copied to
208 			 * dataflow sockets, accepted from this one. */
209 			int		sol_sbrcv_lowat;
210 			int		sol_sbsnd_lowat;
211 			u_int		sol_sbrcv_hiwat;
212 			u_int		sol_sbsnd_hiwat;
213 			short		sol_sbrcv_flags;
214 			short		sol_sbsnd_flags;
215 			sbintime_t	sol_sbrcv_timeo;
216 			sbintime_t	sol_sbsnd_timeo;
217 
218 			/* Information tracking listen queue overflows. */
219 			struct timeval	sol_lastover;	/* (e) */
220 			int		sol_overcount;	/* (e) */
221 		};
222 	};
223 };
224 #endif	/* defined(_KERNEL) || defined(_WANT_SOCKET) */
225 
226 /*
227  * Socket state bits.
228  *
229  * Historically, these bits were all kept in the so_state field.
230  * They are now split into separate, lock-specific fields.
231  * so_state maintains basic socket state protected by the socket lock.
232  * so_qstate holds information about the socket accept queues.
233  * Each socket buffer also has a state field holding information
234  * relevant to that socket buffer (can't send, rcv).
235  * Many fields will be read without locks to improve performance and avoid
236  * lock order issues.  However, this approach must be used with caution.
237  */
238 #define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
239 #define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
240 #define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
241 #define	SS_NBIO			0x0100	/* non-blocking ops */
242 #define	SS_ASYNC		0x0200	/* async i/o notify */
243 #define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
244 #define	SS_ISDISCONNECTED	0x2000	/* socket disconnected from peer */
245 
246 #ifdef _KERNEL
247 
248 #define	SOCK_MTX(so)		(&(so)->so_lock)
249 #define	SOCK_LOCK(so)		mtx_lock(&(so)->so_lock)
250 #define	SOCK_OWNED(so)		mtx_owned(&(so)->so_lock)
251 #define	SOCK_UNLOCK(so)		mtx_unlock(&(so)->so_lock)
252 #define	SOCK_LOCK_ASSERT(so)	mtx_assert(&(so)->so_lock, MA_OWNED)
253 #define	SOCK_UNLOCK_ASSERT(so)	mtx_assert(&(so)->so_lock, MA_NOTOWNED)
254 
255 #define	SOLISTENING(sol)	(((sol)->so_options & SO_ACCEPTCONN) != 0)
256 #define	SOLISTEN_LOCK(sol)	do {					\
257 	mtx_lock(&(sol)->so_lock);					\
258 	KASSERT(SOLISTENING(sol),					\
259 	    ("%s: %p not listening", __func__, (sol)));			\
260 } while (0)
261 #define	SOLISTEN_TRYLOCK(sol)	mtx_trylock(&(sol)->so_lock)
262 #define	SOLISTEN_UNLOCK(sol)	do {					\
263 	KASSERT(SOLISTENING(sol),					\
264 	    ("%s: %p not listening", __func__, (sol)));			\
265 	mtx_unlock(&(sol)->so_lock);					\
266 } while (0)
267 #define	SOLISTEN_LOCK_ASSERT(sol)	do {				\
268 	mtx_assert(&(sol)->so_lock, MA_OWNED);				\
269 	KASSERT(SOLISTENING(sol),					\
270 	    ("%s: %p not listening", __func__, (sol)));			\
271 } while (0)
272 #define	SOLISTEN_UNLOCK_ASSERT(sol)	do {				\
273 	mtx_assert(&(sol)->so_lock, MA_NOTOWNED);			\
274 	KASSERT(SOLISTENING(sol),					\
275 	    ("%s: %p not listening", __func__, (sol)));			\
276 } while (0)
277 
278 /*
279  * Socket buffer locks.  These are strongly preferred over SOCKBUF_LOCK(sb)
280  * macros, as we are moving towards protocol specific socket buffers.
281  */
282 #define	SOCK_RECVBUF_MTX(so)						\
283 	(&(so)->so_rcv_mtx)
284 #define	SOCK_RECVBUF_LOCK(so)						\
285 	mtx_lock(SOCK_RECVBUF_MTX(so))
286 #define	SOCK_RECVBUF_UNLOCK(so)						\
287 	mtx_unlock(SOCK_RECVBUF_MTX(so))
288 #define	SOCK_RECVBUF_LOCK_ASSERT(so)					\
289 	mtx_assert(SOCK_RECVBUF_MTX(so), MA_OWNED)
290 #define	SOCK_RECVBUF_UNLOCK_ASSERT(so)					\
291 	mtx_assert(SOCK_RECVBUF_MTX(so), MA_NOTOWNED)
292 
293 #define	SOCK_SENDBUF_MTX(so)						\
294 	(&(so)->so_snd_mtx)
295 #define	SOCK_SENDBUF_LOCK(so)						\
296 	mtx_lock(SOCK_SENDBUF_MTX(so))
297 #define	SOCK_SENDBUF_UNLOCK(so)						\
298 	mtx_unlock(SOCK_SENDBUF_MTX(so))
299 #define	SOCK_SENDBUF_LOCK_ASSERT(so)					\
300 	mtx_assert(SOCK_SENDBUF_MTX(so), MA_OWNED)
301 #define	SOCK_SENDBUF_UNLOCK_ASSERT(so)					\
302 	mtx_assert(SOCK_SENDBUF_MTX(so), MA_NOTOWNED)
303 
304 #define	SOCK_BUF_LOCK(so, which)					\
305 	mtx_lock(soeventmtx(so, which))
306 #define	SOCK_BUF_UNLOCK(so, which)					\
307 	mtx_unlock(soeventmtx(so, which))
308 #define	SOCK_BUF_LOCK_ASSERT(so, which)					\
309 	mtx_assert(soeventmtx(so, which), MA_OWNED)
310 #define	SOCK_BUF_UNLOCK_ASSERT(so, which)				\
311 	mtx_assert(soeventmtx(so, which), MA_NOTOWNED)
312 
313 static inline struct sockbuf *
sobuf(struct socket * so,const sb_which which)314 sobuf(struct socket *so, const sb_which which)
315 {
316 	return (which == SO_RCV ? &so->so_rcv : &so->so_snd);
317 }
318 
319 static inline struct mtx *
soeventmtx(struct socket * so,const sb_which which)320 soeventmtx(struct socket *so, const sb_which which)
321 {
322 	return (which == SO_RCV ? SOCK_RECVBUF_MTX(so) : SOCK_SENDBUF_MTX(so));
323 }
324 
325 /*
326  * Macros for sockets and socket buffering.
327  */
328 
329 
330 #define	isspliced(so)		((so->so_splice != NULL &&		\
331 					so->so_splice->src != NULL))
332 #define	issplicedback(so)	((so->so_splice_back != NULL &&		\
333 					so->so_splice_back->dst != NULL))
334 /*
335  * Flags to soiolock().
336  */
337 #define	SBL_WAIT	0x00000001	/* Wait if not immediately available. */
338 #define	SBL_NOINTR	0x00000002	/* Force non-interruptible sleep. */
339 #define	SBL_VALID	(SBL_WAIT | SBL_NOINTR)
340 
341 #define	SBLOCKWAIT(f)	(((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
342 
343 #define	SOCK_IO_SEND_LOCK(so, flags)					\
344 	soiolock((so), &(so)->so_snd_sx, (flags))
345 #define	SOCK_IO_SEND_UNLOCK(so)						\
346 	soiounlock(&(so)->so_snd_sx)
347 #define	SOCK_IO_SEND_OWNED(so)	sx_xlocked(&(so)->so_snd_sx)
348 #define	SOCK_IO_SEND_ASSERT_LOCKED(so)					\
349 	sx_assert(&(so)->so_snd_sx, SA_XLOCKED)
350 #define	SOCK_IO_RECV_LOCK(so, flags)					\
351 	soiolock((so), &(so)->so_rcv_sx, (flags))
352 #define	SOCK_IO_RECV_UNLOCK(so)						\
353 	soiounlock(&(so)->so_rcv_sx)
354 #define	SOCK_IO_RECV_OWNED(so)	sx_xlocked(&(so)->so_rcv_sx)
355 #define	SOCK_IO_RECV_ASSERT_LOCKED(so)					\
356 	sx_assert(&(so)->so_rcv_sx, SA_XLOCKED)
357 
358 /* do we have to send all at once on a socket? */
359 #define	sosendallatonce(so) \
360     ((so)->so_proto->pr_flags & PR_ATOMIC)
361 
362 /* can we read something from so? */
363 #define	soreadabledata(so) \
364 	(sbavail(&(so)->so_rcv) >= (so)->so_rcv.sb_lowat || \
365 	(so)->so_error || (so)->so_rerror)
366 #define	_soreadable(so) \
367 	(soreadabledata(so) || ((so)->so_rcv.sb_state & SBS_CANTRCVMORE))
368 
369 static inline bool
soreadable(struct socket * so)370 soreadable(struct socket *so)
371 {
372        if (isspliced(so))
373                return (false);
374        return (_soreadable(so));
375 }
376 
377 /* can we write something to so? */
378 #define	sowriteable(so) \
379     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
380 	(((so)->so_state&SS_ISCONNECTED) || \
381 	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
382      ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
383      (so)->so_error)
384 
385 /*
386  * soref()/sorele() ref-count the socket structure.
387  * soref() may be called without owning socket lock, but in that case a
388  * caller must own something that holds socket, and so_count must be not 0.
389  * Note that you must still explicitly close the socket, but the last ref
390  * count will free the structure.
391  */
392 #define	soref(so)	refcount_acquire(&(so)->so_count)
393 #define	sorele(so) do {							\
394 	SOCK_UNLOCK_ASSERT(so);						\
395 	if (!refcount_release_if_not_last(&(so)->so_count)) {		\
396 		SOCK_LOCK(so);						\
397 		sorele_locked(so);					\
398 	}								\
399 } while (0)
400 
401 /*
402  * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to
403  * avoid a non-atomic test-and-wakeup.  However, sowakeup is
404  * responsible for releasing the lock if it is called.  We unlock only
405  * if we don't call into sowakeup.  If any code is introduced that
406  * directly invokes the underlying sowakeup() primitives, it must
407  * maintain the same semantics.
408  */
409 #define	sorwakeup(so) do {						\
410 	SOCK_RECVBUF_LOCK(so);						\
411 	sorwakeup_locked(so);						\
412 } while (0)
413 
414 #define	sowwakeup(so) do {						\
415 	SOCK_SENDBUF_LOCK(so);						\
416 	sowwakeup_locked(so);						\
417 } while (0)
418 
419 struct accept_filter {
420 	char	accf_name[16];
421 	int	(*accf_callback)
422 		(struct socket *so, void *arg, int waitflag);
423 	void *	(*accf_create)
424 		(struct socket *so, char *arg);
425 	void	(*accf_destroy)
426 		(struct socket *so);
427 	SLIST_ENTRY(accept_filter) accf_next;
428 };
429 
430 #define	ACCEPT_FILTER_DEFINE(modname, filtname, cb, create, destroy, ver) \
431 	static struct accept_filter modname##_filter = {		\
432 		.accf_name = filtname,					\
433 		.accf_callback = cb,					\
434 		.accf_create = create,					\
435 		.accf_destroy = destroy,				\
436 	};								\
437 	static moduledata_t modname##_mod = {				\
438 		.name = __XSTRING(modname),				\
439 		.evhand = accept_filt_generic_mod_event,		\
440 		.priv = &modname##_filter,				\
441 	};								\
442 	DECLARE_MODULE(modname, modname##_mod, SI_SUB_DRIVERS,		\
443 	    SI_ORDER_MIDDLE);						\
444 	MODULE_VERSION(modname, ver)
445 
446 #ifdef MALLOC_DECLARE
447 MALLOC_DECLARE(M_ACCF);
448 MALLOC_DECLARE(M_PCB);
449 MALLOC_DECLARE(M_SONAME);
450 #endif
451 
452 /*
453  * Socket specific helper hook point identifiers
454  * Do not leave holes in the sequence, hook registration is a loop.
455  */
456 #define HHOOK_SOCKET_OPT		0
457 #define HHOOK_SOCKET_CREATE		1
458 #define HHOOK_SOCKET_RCV 		2
459 #define HHOOK_SOCKET_SND		3
460 #define HHOOK_FILT_SOREAD		4
461 #define HHOOK_FILT_SOWRITE		5
462 #define HHOOK_SOCKET_CLOSE		6
463 #define HHOOK_SOCKET_LAST		HHOOK_SOCKET_CLOSE
464 
465 struct socket_hhook_data {
466 	struct socket	*so;
467 	struct mbuf	*m;
468 	void		*hctx;		/* hook point specific data*/
469 	int		status;
470 };
471 
472 extern int	maxsockets;
473 extern u_long	sb_max;
474 extern so_gen_t so_gencnt;
475 
476 struct file;
477 struct filecaps;
478 struct filedesc;
479 struct mbuf;
480 struct sockaddr;
481 struct ucred;
482 struct uio;
483 
484 /* Return values for socket upcalls. */
485 #define	SU_OK		0
486 #define	SU_ISCONNECTED	1
487 
488 /*
489  * From uipc_socket and friends
490  */
491 int	getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr,
492 	    size_t len);
493 int	getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
494 	    struct file **fpp, struct filecaps *havecaps);
495 int	getsock(struct thread *td, int fd, cap_rights_t *rightsp,
496 	    struct file **fpp);
497 void	soabort(struct socket *so);
498 int	soaccept(struct socket *so, struct sockaddr **nam);
499 void	soaio_enqueue(struct task *task);
500 void	soaio_rcv(void *context, int pending);
501 void	soaio_snd(void *context, int pending);
502 int	socheckuid(struct socket *so, uid_t uid);
503 int	sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
504 int	sobindat(int fd, struct socket *so, struct sockaddr *nam,
505 	    struct thread *td);
506 int	soclose(struct socket *so);
507 int	soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
508 int	soconnectat(int fd, struct socket *so, struct sockaddr *nam,
509 	    struct thread *td);
510 int	soconnect2(struct socket *so1, struct socket *so2);
511 int	socreate(int dom, struct socket **aso, int type, int proto,
512 	    struct ucred *cred, struct thread *td);
513 int	sodisconnect(struct socket *so);
514 void	sodtor_set(struct socket *, so_dtor_t *);
515 struct	sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags);
516 void	sohasoutofband(struct socket *so);
517 int	solisten(struct socket *so, int backlog, struct thread *td);
518 void	solisten_proto(struct socket *so, int backlog);
519 void	solisten_proto_abort(struct socket *so);
520 int	solisten_proto_check(struct socket *so);
521 bool	solisten_enqueue(struct socket *, int);
522 int	solisten_dequeue(struct socket *, struct socket **, int);
523 struct socket *
524 	solisten_clone(struct socket *);
525 struct socket *
526 	sonewconn(struct socket *head, int connstatus);
527 struct socket *
528 	sopeeloff(struct socket *);
529 int	sopoll(struct socket *so, int events, struct ucred *active_cred,
530 	    struct thread *td);
531 int	sopoll_generic(struct socket *so, int events,
532 	    struct ucred *active_cred, struct thread *td);
533 int	soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
534 	    struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
535 int	soreceive_stream(struct socket *so, struct sockaddr **paddr,
536 	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
537 	    int *flagsp);
538 int	soreceive_dgram(struct socket *so, struct sockaddr **paddr,
539 	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
540 	    int *flagsp);
541 int	soreceive_generic(struct socket *so, struct sockaddr **paddr,
542 	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
543 	    int *flagsp);
544 void	sorele_locked(struct socket *so);
545 void	sodealloc(struct socket *);
546 int	soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
547 void	sorflush(struct socket *so);
548 int	sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
549 	    struct mbuf *top, struct mbuf *control, int flags,
550 	    struct thread *td);
551 int	sousrsend(struct socket *so, struct sockaddr *addr, struct uio *uio,
552 	    struct mbuf *control, int flags, struct proc *);
553 int	sosend_dgram(struct socket *so, struct sockaddr *addr,
554 	    struct uio *uio, struct mbuf *top, struct mbuf *control,
555 	    int flags, struct thread *td);
556 int	sosend_generic(struct socket *so, struct sockaddr *addr,
557 	    struct uio *uio, struct mbuf *top, struct mbuf *control,
558 	    int flags, struct thread *td);
559 int	sosetfib(struct socket *so, int fibnum);
560 int	soshutdown(struct socket *so, int how);
561 void	soupcall_clear(struct socket *, sb_which);
562 void	soupcall_set(struct socket *, sb_which, so_upcall_t, void *);
563 void	solisten_upcall_set(struct socket *, so_upcall_t, void *);
564 void	sorwakeup_locked(struct socket *);
565 void	sowwakeup_locked(struct socket *);
566 void	sowakeup_aio(struct socket *, sb_which);
567 void	solisten_wakeup(struct socket *);
568 int	selsocket(struct socket *so, int events, struct timeval *tv,
569 	    struct thread *td);
570 void	soisconnected(struct socket *so);
571 void	soisconnecting(struct socket *so);
572 void	soisdisconnected(struct socket *so);
573 void	soisdisconnecting(struct socket *so);
574 void	socantrcvmore(struct socket *so);
575 void	socantrcvmore_locked(struct socket *so);
576 void	socantsendmore(struct socket *so);
577 void	socantsendmore_locked(struct socket *so);
578 void	soroverflow(struct socket *so);
579 void	soroverflow_locked(struct socket *so);
580 int	soiolock(struct socket *so, struct sx *sx, int flags);
581 void	soiounlock(struct sx *sx);
582 
583 /*
584  * Socket splicing routines.
585  */
586 void	so_splice_dispatch(struct so_splice *sp);
587 
588 /*
589  * Accept filter functions (duh).
590  */
591 int	accept_filt_add(struct accept_filter *filt);
592 int	accept_filt_del(char *name);
593 struct	accept_filter *accept_filt_get(char *name);
594 #ifdef ACCEPT_FILTER_MOD
595 #ifdef SYSCTL_DECL
596 SYSCTL_DECL(_net_inet_accf);
597 #endif
598 int	accept_filt_generic_mod_event(module_t mod, int event, void *data);
599 #endif
600 
601 #endif /* _KERNEL */
602 
603 /*
604  * Structure to export socket from kernel to utilities, via sysctl(3).
605  */
606 struct xsocket {
607 	ksize_t		xso_len;	/* length of this structure */
608 	kvaddr_t	xso_so;		/* kernel address of struct socket */
609 	kvaddr_t	so_pcb;		/* kernel address of struct inpcb */
610 	uint64_t	so_oobmark;
611 	kvaddr_t	so_splice_so;	/* kernel address of spliced socket */
612 	int64_t		so_spare64[7];
613 	int32_t		xso_protocol;
614 	int32_t		xso_family;
615 	uint32_t	so_qlen;
616 	uint32_t	so_incqlen;
617 	uint32_t	so_qlimit;
618 	pid_t		so_pgid;
619 	uid_t		so_uid;
620 	int32_t		so_fibnum;
621 	int32_t		so_spare32[7];
622 	int16_t		so_type;
623 	int16_t		so_options;
624 	int16_t		so_linger;
625 	int16_t		so_state;
626 	int16_t		so_timeo;
627 	uint16_t	so_error;
628 	struct xsockbuf {
629 		uint32_t	sb_cc;
630 		uint32_t	sb_hiwat;
631 		uint32_t	sb_mbcnt;
632 		uint32_t	sb_spare0;	/* was sb_mcnt */
633 		uint32_t	sb_spare1;	/* was sb_ccnt */
634 		uint32_t	sb_mbmax;
635 		int32_t		sb_lowat;
636 		int32_t		sb_timeo;
637 		int16_t		sb_flags;
638 	} so_rcv, so_snd;
639 };
640 
641 #ifdef _KERNEL
642 void	sotoxsocket(struct socket *so, struct xsocket *xso);
643 void	sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
644 #endif
645 
646 /*
647  * Socket buffer state bits.  Exported via libprocstat(3).
648  */
649 #define	SBS_CANTSENDMORE	0x0010	/* can't send more data to peer */
650 #define	SBS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
651 #define	SBS_RCVATMARK		0x0040	/* at mark on input */
652 
653 #endif /* !_SYS_SOCKETVAR_H_ */
654