xref: /freebsd-13-stable/sys/sys/socketvar.h (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
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 #ifdef _KERNEL
51 #include <sys/caprights.h>
52 #include <sys/sockopt.h>
53 #endif
54 
55 struct vnet;
56 
57 /*
58  * Kernel structure per socket.
59  * Contains send and receive buffer queues,
60  * handle on protocol and pointer to protocol
61  * private data and error information.
62  */
63 typedef	int so_upcall_t(struct socket *, void *, int);
64 typedef	void so_dtor_t(struct socket *);
65 
66 struct socket;
67 
68 enum socket_qstate {
69 	SQ_NONE = 0,
70 	SQ_INCOMP = 0x0800,	/* on sol_incomp */
71 	SQ_COMP = 0x1000,	/* on sol_comp */
72 };
73 
74 /*-
75  * Locking key to struct socket:
76  * (a) constant after allocation, no locking required.
77  * (b) locked by SOCK_LOCK(so).
78  * (cr) locked by SOCKBUF_LOCK(&so->so_rcv).
79  * (cs) locked by SOCKBUF_LOCK(&so->so_snd).
80  * (e) locked by SOLISTEN_LOCK() of corresponding listening socket.
81  * (f) not locked since integer reads/writes are atomic.
82  * (g) used only as a sleep/wakeup address, no value.
83  * (h) locked by global mutex so_global_mtx.
84  * (k) locked by KTLS workqueue mutex
85  */
86 TAILQ_HEAD(accept_queue, socket);
87 struct socket {
88 	struct mtx	so_lock;
89 	volatile u_int	so_count;	/* (b / refcount) */
90 	struct selinfo	so_rdsel;	/* (b/cr) for so_rcv/so_comp */
91 	struct selinfo	so_wrsel;	/* (b/cs) for so_snd */
92 	short	so_type;		/* (a) generic type, see socket.h */
93 	int	so_options;		/* (b) from socket call, see socket.h */
94 	short	so_linger;		/* time to linger close(2) */
95 	short	so_state;		/* (b) internal state flags SS_* */
96 	void	*so_pcb;		/* protocol control block */
97 	struct	vnet *so_vnet;		/* (a) network stack instance */
98 	struct	protosw *so_proto;	/* (a) protocol handle */
99 	short	so_timeo;		/* (g) connection timeout */
100 	u_short	so_error;		/* (f) error affecting connection */
101 	u_short so_rerror;		/* (f) error affecting connection */
102 	struct	sigio *so_sigio;	/* [sg] information for async I/O or
103 					   out of band data (SIGURG) */
104 	struct	ucred *so_cred;		/* (a) user credentials */
105 	struct	label *so_label;	/* (b) MAC label for socket */
106 	/* NB: generation count must not be first. */
107 	so_gen_t so_gencnt;		/* (h) generation count */
108 	void	*so_emuldata;		/* (b) private data for emulators */
109 	so_dtor_t *so_dtor;		/* (b) optional destructor */
110 	struct	osd	osd;		/* Object Specific extensions */
111 	/*
112 	 * so_fibnum, so_user_cookie and friends can be used to attach
113 	 * some user-specified metadata to a socket, which then can be
114 	 * used by the kernel for various actions.
115 	 * so_user_cookie is used by ipfw/dummynet.
116 	 */
117 	int so_fibnum;		/* routing domain for this socket */
118 	uint32_t so_user_cookie;
119 
120 	int so_ts_clock;	/* type of the clock used for timestamps */
121 	uint32_t so_max_pacing_rate;	/* (f) TX rate limit in bytes/s */
122 	union {
123 		/* Regular (data flow) socket. */
124 		struct {
125 			/* (cr, cs) Receive and send buffers. */
126 			struct sockbuf		so_rcv, so_snd;
127 
128 			/* (e) Our place on accept queue. */
129 			TAILQ_ENTRY(socket)	so_list;
130 			struct socket		*so_listen;	/* (b) */
131 			enum socket_qstate so_qstate;		/* (b) */
132 			/* (b) cached MAC label for peer */
133 			struct	label		*so_peerlabel;
134 			u_long	so_oobmark;	/* chars to oob mark */
135 
136 			/* (k) Our place on KTLS RX work queue. */
137 			STAILQ_ENTRY(socket)	so_ktls_rx_list;
138 		};
139 		/*
140 		 * Listening socket, where accepts occur, is so_listen in all
141 		 * subsidiary sockets.  If so_listen is NULL, socket is not
142 		 * related to an accept.  For a listening socket itself
143 		 * sol_incomp queues partially completed connections, while
144 		 * sol_comp is a queue of connections ready to be accepted.
145 		 * If a connection is aborted and it has so_listen set, then
146 		 * it has to be pulled out of either sol_incomp or sol_comp.
147 		 * We allow connections to queue up based on current queue
148 		 * lengths and limit on number of queued connections for this
149 		 * socket.
150 		 */
151 		struct {
152 			/* (e) queue of partial unaccepted connections */
153 			struct accept_queue	sol_incomp;
154 			/* (e) queue of complete unaccepted connections */
155 			struct accept_queue	sol_comp;
156 			u_int	sol_qlen;    /* (e) sol_comp length */
157 			u_int	sol_incqlen; /* (e) sol_incomp length */
158 			u_int	sol_qlimit;  /* (e) queue limit */
159 
160 			/* accept_filter(9) optional data */
161 			struct	accept_filter	*sol_accept_filter;
162 			void	*sol_accept_filter_arg;	/* saved filter args */
163 			char	*sol_accept_filter_str;	/* saved user args */
164 
165 			/* Optional upcall, for kernel socket. */
166 			so_upcall_t	*sol_upcall;	/* (e) */
167 			void		*sol_upcallarg;	/* (e) */
168 
169 			/* Socket buffer parameters, to be copied to
170 			 * dataflow sockets, accepted from this one. */
171 			int		sol_sbrcv_lowat;
172 			int		sol_sbsnd_lowat;
173 			u_int		sol_sbrcv_hiwat;
174 			u_int		sol_sbsnd_hiwat;
175 			short		sol_sbrcv_flags;
176 			short		sol_sbsnd_flags;
177 			sbintime_t	sol_sbrcv_timeo;
178 			sbintime_t	sol_sbsnd_timeo;
179 
180 			/* Information tracking listen queue overflows. */
181 			struct timeval	sol_lastover;	/* (e) */
182 			int		sol_overcount;	/* (e) */
183 		};
184 	};
185 };
186 #endif	/* defined(_KERNEL) || defined(_WANT_SOCKET) */
187 
188 /*
189  * Socket state bits.
190  *
191  * Historically, these bits were all kept in the so_state field.
192  * They are now split into separate, lock-specific fields.
193  * so_state maintains basic socket state protected by the socket lock.
194  * so_qstate holds information about the socket accept queues.
195  * Each socket buffer also has a state field holding information
196  * relevant to that socket buffer (can't send, rcv).
197  * Many fields will be read without locks to improve performance and avoid
198  * lock order issues.  However, this approach must be used with caution.
199  */
200 #define	SS_NOFDREF		0x0001	/* no file table ref any more */
201 #define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
202 #define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
203 #define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
204 #define	SS_NBIO			0x0100	/* non-blocking ops */
205 #define	SS_ASYNC		0x0200	/* async i/o notify */
206 #define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
207 #define	SS_ISDISCONNECTED	0x2000	/* socket disconnected from peer */
208 
209 /*
210  * Protocols can mark a socket as SS_PROTOREF to indicate that, following
211  * pru_detach, they still want the socket to persist, and will free it
212  * themselves when they are done.  Protocols should only ever call sofree()
213  * following setting this flag in pru_detach(), and never otherwise, as
214  * sofree() bypasses socket reference counting.
215  */
216 #define	SS_PROTOREF		0x4000	/* strong protocol reference */
217 
218 #ifdef _KERNEL
219 
220 #define	SOCK_MTX(so)		(&(so)->so_lock)
221 #define	SOCK_LOCK(so)		mtx_lock(&(so)->so_lock)
222 #define	SOCK_OWNED(so)		mtx_owned(&(so)->so_lock)
223 #define	SOCK_UNLOCK(so)		mtx_unlock(&(so)->so_lock)
224 #define	SOCK_LOCK_ASSERT(so)	mtx_assert(&(so)->so_lock, MA_OWNED)
225 #define	SOCK_UNLOCK_ASSERT(so)	mtx_assert(&(so)->so_lock, MA_NOTOWNED)
226 
227 #define	SOLISTENING(sol)	(((sol)->so_options & SO_ACCEPTCONN) != 0)
228 #define	SOLISTEN_LOCK(sol)	do {					\
229 	mtx_lock(&(sol)->so_lock);					\
230 	KASSERT(SOLISTENING(sol),					\
231 	    ("%s: %p not listening", __func__, (sol)));			\
232 } while (0)
233 #define	SOLISTEN_TRYLOCK(sol)	mtx_trylock(&(sol)->so_lock)
234 #define	SOLISTEN_UNLOCK(sol)	do {					\
235 	KASSERT(SOLISTENING(sol),					\
236 	    ("%s: %p not listening", __func__, (sol)));			\
237 	mtx_unlock(&(sol)->so_lock);					\
238 } while (0)
239 #define	SOLISTEN_LOCK_ASSERT(sol)	do {				\
240 	mtx_assert(&(sol)->so_lock, MA_OWNED);				\
241 	KASSERT(SOLISTENING(sol),					\
242 	    ("%s: %p not listening", __func__, (sol)));			\
243 } while (0)
244 
245 /*
246  * Macros for sockets and socket buffering.
247  */
248 
249 /*
250  * Flags to soiolock().
251  */
252 #define	SBL_WAIT	0x00000001	/* Wait if not immediately available. */
253 #define	SBL_NOINTR	0x00000002	/* Force non-interruptible sleep. */
254 #define	SBL_VALID	(SBL_WAIT | SBL_NOINTR)
255 
256 
257 #define	SBLOCKWAIT(f)	(((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
258 
259 #define	SOCK_IO_SEND_LOCK(so, flags)					\
260 	soiolock((so), &(so)->so_snd.sb_sx, (flags))
261 #define	SOCK_IO_SEND_UNLOCK(so)						\
262 	soiounlock(&(so)->so_snd.sb_sx)
263 #define	SOCK_IO_RECV_LOCK(so, flags)					\
264 	soiolock((so), &(so)->so_rcv.sb_sx, (flags))
265 #define	SOCK_IO_RECV_UNLOCK(so)						\
266 	soiounlock(&(so)->so_rcv.sb_sx)
267 
268 /*
269  * Do we need to notify the other side when I/O is possible?
270  */
271 #define	sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
272     SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
273 
274 /* do we have to send all at once on a socket? */
275 #define	sosendallatonce(so) \
276     ((so)->so_proto->pr_flags & PR_ATOMIC)
277 
278 /* can we read something from so? */
279 #define	soreadabledata(so) \
280 	(sbavail(&(so)->so_rcv) >= (so)->so_rcv.sb_lowat || \
281 	(so)->so_error || (so)->so_rerror)
282 #define	soreadable(so) \
283 	(soreadabledata(so) || ((so)->so_rcv.sb_state & SBS_CANTRCVMORE))
284 
285 /* can we write something to so? */
286 #define	sowriteable(so) \
287     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
288 	(((so)->so_state&SS_ISCONNECTED) || \
289 	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
290      ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
291      (so)->so_error)
292 
293 /*
294  * soref()/sorele() ref-count the socket structure.
295  * soref() may be called without owning socket lock, but in that case a
296  * caller must own something that holds socket, and so_count must be not 0.
297  * Note that you must still explicitly close the socket, but the last ref
298  * count will free the structure.
299  */
300 #define	soref(so)	refcount_acquire(&(so)->so_count)
301 #define	sorele(so) do {							\
302 	SOCK_LOCK_ASSERT(so);						\
303 	if (refcount_release(&(so)->so_count))				\
304 		sofree(so);						\
305 	else								\
306 		SOCK_UNLOCK(so);					\
307 } while (0)
308 
309 /*
310  * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to
311  * avoid a non-atomic test-and-wakeup.  However, sowakeup is
312  * responsible for releasing the lock if it is called.  We unlock only
313  * if we don't call into sowakeup.  If any code is introduced that
314  * directly invokes the underlying sowakeup() primitives, it must
315  * maintain the same semantics.
316  */
317 #define	sorwakeup_locked(so) do {					\
318 	SOCKBUF_LOCK_ASSERT(&(so)->so_rcv);				\
319 	if (sb_notify(&(so)->so_rcv))					\
320 		sowakeup((so), &(so)->so_rcv);	 			\
321 	else								\
322 		SOCKBUF_UNLOCK(&(so)->so_rcv);				\
323 } while (0)
324 
325 #define	sorwakeup(so) do {						\
326 	SOCKBUF_LOCK(&(so)->so_rcv);					\
327 	sorwakeup_locked(so);						\
328 } while (0)
329 
330 #define	sowwakeup_locked(so) do {					\
331 	SOCKBUF_LOCK_ASSERT(&(so)->so_snd);				\
332 	if (sb_notify(&(so)->so_snd))					\
333 		sowakeup((so), &(so)->so_snd); 				\
334 	else								\
335 		SOCKBUF_UNLOCK(&(so)->so_snd);				\
336 } while (0)
337 
338 #define	sowwakeup(so) do {						\
339 	SOCKBUF_LOCK(&(so)->so_snd);					\
340 	sowwakeup_locked(so);						\
341 } while (0)
342 
343 struct accept_filter {
344 	char	accf_name[16];
345 	int	(*accf_callback)
346 		(struct socket *so, void *arg, int waitflag);
347 	void *	(*accf_create)
348 		(struct socket *so, char *arg);
349 	void	(*accf_destroy)
350 		(struct socket *so);
351 	SLIST_ENTRY(accept_filter) accf_next;
352 };
353 
354 #define	ACCEPT_FILTER_DEFINE(modname, filtname, cb, create, destroy, ver) \
355 	static struct accept_filter modname##_filter = {		\
356 		.accf_name = filtname,					\
357 		.accf_callback = cb,					\
358 		.accf_create = create,					\
359 		.accf_destroy = destroy,				\
360 	};								\
361 	static moduledata_t modname##_mod = {				\
362 		.name = __XSTRING(modname),				\
363 		.evhand = accept_filt_generic_mod_event,		\
364 		.priv = &modname##_filter,				\
365 	};								\
366 	DECLARE_MODULE(modname, modname##_mod, SI_SUB_DRIVERS,		\
367 	    SI_ORDER_MIDDLE);						\
368 	MODULE_VERSION(modname, ver)
369 
370 #ifdef MALLOC_DECLARE
371 MALLOC_DECLARE(M_ACCF);
372 MALLOC_DECLARE(M_PCB);
373 MALLOC_DECLARE(M_SONAME);
374 #endif
375 
376 /*
377  * Socket specific helper hook point identifiers
378  * Do not leave holes in the sequence, hook registration is a loop.
379  */
380 #define HHOOK_SOCKET_OPT		0
381 #define HHOOK_SOCKET_CREATE		1
382 #define HHOOK_SOCKET_RCV 		2
383 #define HHOOK_SOCKET_SND		3
384 #define HHOOK_FILT_SOREAD		4
385 #define HHOOK_FILT_SOWRITE		5
386 #define HHOOK_SOCKET_CLOSE		6
387 #define HHOOK_SOCKET_LAST		HHOOK_SOCKET_CLOSE
388 
389 struct socket_hhook_data {
390 	struct socket	*so;
391 	struct mbuf	*m;
392 	void		*hctx;		/* hook point specific data*/
393 	int		status;
394 };
395 
396 extern int	maxsockets;
397 extern u_long	sb_max;
398 extern so_gen_t so_gencnt;
399 
400 struct file;
401 struct filecaps;
402 struct filedesc;
403 struct mbuf;
404 struct sockaddr;
405 struct ucred;
406 struct uio;
407 
408 /* 'which' values for socket upcalls. */
409 #define	SO_RCV		1
410 #define	SO_SND		2
411 
412 /* Return values for socket upcalls. */
413 #define	SU_OK		0
414 #define	SU_ISCONNECTED	1
415 
416 /*
417  * From uipc_socket and friends
418  */
419 int	getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr,
420 	    size_t len);
421 int	getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
422 	    struct file **fpp, u_int *fflagp, struct filecaps *havecaps);
423 void	soabort(struct socket *so);
424 int	soaccept(struct socket *so, struct sockaddr **nam);
425 void	soaio_enqueue(struct task *task);
426 void	soaio_rcv(void *context, int pending);
427 void	soaio_snd(void *context, int pending);
428 int	socheckuid(struct socket *so, uid_t uid);
429 int	sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
430 int	sobindat(int fd, struct socket *so, struct sockaddr *nam,
431 	    struct thread *td);
432 int	soclose(struct socket *so);
433 int	soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
434 int	soconnectat(int fd, struct socket *so, struct sockaddr *nam,
435 	    struct thread *td);
436 int	soconnect2(struct socket *so1, struct socket *so2);
437 int	socreate(int dom, struct socket **aso, int type, int proto,
438 	    struct ucred *cred, struct thread *td);
439 int	sodisconnect(struct socket *so);
440 void	sodtor_set(struct socket *, so_dtor_t *);
441 struct	sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags);
442 void	sofree(struct socket *so);
443 void	sohasoutofband(struct socket *so);
444 int	solisten(struct socket *so, int backlog, struct thread *td);
445 void	solisten_proto(struct socket *so, int backlog);
446 int	solisten_proto_check(struct socket *so);
447 int	solisten_dequeue(struct socket *, struct socket **, int);
448 struct socket *
449 	sonewconn(struct socket *head, int connstatus);
450 struct socket *
451 	sopeeloff(struct socket *);
452 int	sopoll(struct socket *so, int events, struct ucred *active_cred,
453 	    struct thread *td);
454 int	sopoll_generic(struct socket *so, int events,
455 	    struct ucred *active_cred, struct thread *td);
456 int	soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
457 	    struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
458 int	soreceive_stream(struct socket *so, struct sockaddr **paddr,
459 	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
460 	    int *flagsp);
461 int	soreceive_dgram(struct socket *so, struct sockaddr **paddr,
462 	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
463 	    int *flagsp);
464 int	soreceive_generic(struct socket *so, struct sockaddr **paddr,
465 	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
466 	    int *flagsp);
467 int	soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
468 void	sorflush(struct socket *so);
469 int	sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
470 	    struct mbuf *top, struct mbuf *control, int flags,
471 	    struct thread *td);
472 int	sosend_dgram(struct socket *so, struct sockaddr *addr,
473 	    struct uio *uio, struct mbuf *top, struct mbuf *control,
474 	    int flags, struct thread *td);
475 int	sosend_generic(struct socket *so, struct sockaddr *addr,
476 	    struct uio *uio, struct mbuf *top, struct mbuf *control,
477 	    int flags, struct thread *td);
478 int	soshutdown(struct socket *so, int how);
479 void	soupcall_clear(struct socket *, int);
480 void	soupcall_set(struct socket *, int, so_upcall_t, void *);
481 void	solisten_upcall_set(struct socket *, so_upcall_t, void *);
482 void	sowakeup(struct socket *so, struct sockbuf *sb);
483 void	sowakeup_aio(struct socket *so, struct sockbuf *sb);
484 void	solisten_wakeup(struct socket *);
485 int	selsocket(struct socket *so, int events, struct timeval *tv,
486 	    struct thread *td);
487 void	soisconnected(struct socket *so);
488 void	soisconnecting(struct socket *so);
489 void	soisdisconnected(struct socket *so);
490 void	soisdisconnecting(struct socket *so);
491 void	socantrcvmore(struct socket *so);
492 void	socantrcvmore_locked(struct socket *so);
493 void	socantsendmore(struct socket *so);
494 void	socantsendmore_locked(struct socket *so);
495 void	soroverflow(struct socket *so);
496 void	soroverflow_locked(struct socket *so);
497 int	soiolock(struct socket *so, struct sx *sx, int flags);
498 void	soiounlock(struct sx *sx);
499 
500 /*
501  * Accept filter functions (duh).
502  */
503 int	accept_filt_add(struct accept_filter *filt);
504 int	accept_filt_del(char *name);
505 struct	accept_filter *accept_filt_get(char *name);
506 #ifdef ACCEPT_FILTER_MOD
507 #ifdef SYSCTL_DECL
508 SYSCTL_DECL(_net_inet_accf);
509 #endif
510 int	accept_filt_generic_mod_event(module_t mod, int event, void *data);
511 #endif
512 
513 #endif /* _KERNEL */
514 
515 /*
516  * Structure to export socket from kernel to utilities, via sysctl(3).
517  */
518 struct xsocket {
519 	ksize_t		xso_len;	/* length of this structure */
520 	kvaddr_t	xso_so;		/* kernel address of struct socket */
521 	kvaddr_t	so_pcb;		/* kernel address of struct inpcb */
522 	uint64_t	so_oobmark;
523 	int64_t		so_spare64[8];
524 	int32_t		xso_protocol;
525 	int32_t		xso_family;
526 	uint32_t	so_qlen;
527 	uint32_t	so_incqlen;
528 	uint32_t	so_qlimit;
529 	pid_t		so_pgid;
530 	uid_t		so_uid;
531 	int32_t		so_spare32[8];
532 	int16_t		so_type;
533 	int16_t		so_options;
534 	int16_t		so_linger;
535 	int16_t		so_state;
536 	int16_t		so_timeo;
537 	uint16_t	so_error;
538 	struct xsockbuf {
539 		uint32_t	sb_cc;
540 		uint32_t	sb_hiwat;
541 		uint32_t	sb_mbcnt;
542 		uint32_t	sb_mcnt;
543 		uint32_t	sb_ccnt;
544 		uint32_t	sb_mbmax;
545 		int32_t		sb_lowat;
546 		int32_t		sb_timeo;
547 		int16_t		sb_flags;
548 	} so_rcv, so_snd;
549 };
550 
551 #ifdef _KERNEL
552 void	sotoxsocket(struct socket *so, struct xsocket *xso);
553 void	sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
554 #endif
555 
556 /*
557  * Socket buffer state bits.  Exported via libprocstat(3).
558  */
559 #define	SBS_CANTSENDMORE	0x0010	/* can't send more data to peer */
560 #define	SBS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
561 #define	SBS_RCVATMARK		0x0040	/* at mark on input */
562 
563 #endif /* !_SYS_SOCKETVAR_H_ */
564