xref: /f-stack/tools/compat/include/sys/socketvar.h (revision 616851da)
11eaf0ac3Slogwang /*-
2*616851daSagerguo  * SPDX-License-Identifier: BSD-3-Clause
3*616851daSagerguo  *
41eaf0ac3Slogwang  * Copyright (c) 1982, 1986, 1990, 1993
51eaf0ac3Slogwang  *	The Regents of the University of California.  All rights reserved.
61eaf0ac3Slogwang  *
71eaf0ac3Slogwang  * Redistribution and use in source and binary forms, with or without
81eaf0ac3Slogwang  * modification, are permitted provided that the following conditions
91eaf0ac3Slogwang  * are met:
101eaf0ac3Slogwang  * 1. Redistributions of source code must retain the above copyright
111eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer.
121eaf0ac3Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
131eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer in the
141eaf0ac3Slogwang  *    documentation and/or other materials provided with the distribution.
15*616851daSagerguo  * 3. Neither the name of the University nor the names of its contributors
161eaf0ac3Slogwang  *    may be used to endorse or promote products derived from this software
171eaf0ac3Slogwang  *    without specific prior written permission.
181eaf0ac3Slogwang  *
191eaf0ac3Slogwang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201eaf0ac3Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211eaf0ac3Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221eaf0ac3Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231eaf0ac3Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241eaf0ac3Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251eaf0ac3Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261eaf0ac3Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271eaf0ac3Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281eaf0ac3Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291eaf0ac3Slogwang  * SUCH DAMAGE.
301eaf0ac3Slogwang  *
311eaf0ac3Slogwang  *	@(#)socketvar.h	8.3 (Berkeley) 2/19/95
321eaf0ac3Slogwang  *
331eaf0ac3Slogwang  * $FreeBSD$
341eaf0ac3Slogwang  */
351eaf0ac3Slogwang 
361eaf0ac3Slogwang #ifndef _SYS_SOCKETVAR_H_
371eaf0ac3Slogwang #define _SYS_SOCKETVAR_H_
381eaf0ac3Slogwang 
39*616851daSagerguo /*
40*616851daSagerguo  * Socket generation count type.  Also used in xinpcb, xtcpcb, xunpcb.
41*616851daSagerguo  */
42*616851daSagerguo typedef uint64_t so_gen_t;
43*616851daSagerguo 
44*616851daSagerguo #if defined(_KERNEL) || defined(_WANT_SOCKET)
451eaf0ac3Slogwang #include <sys/queue.h>			/* for TAILQ macros */
461eaf0ac3Slogwang #include <sys/selinfo.h>		/* for struct selinfo */
471eaf0ac3Slogwang #include <sys/_lock.h>
481eaf0ac3Slogwang #include <sys/_mutex.h>
491eaf0ac3Slogwang #include <sys/osd.h>
501eaf0ac3Slogwang #include <sys/_sx.h>
511eaf0ac3Slogwang #include <sys/sockbuf.h>
521eaf0ac3Slogwang #ifdef _KERNEL
531eaf0ac3Slogwang #include <sys/caprights.h>
541eaf0ac3Slogwang #include <sys/sockopt.h>
551eaf0ac3Slogwang #endif
561eaf0ac3Slogwang 
571eaf0ac3Slogwang struct vnet;
581eaf0ac3Slogwang 
591eaf0ac3Slogwang /*
601eaf0ac3Slogwang  * Kernel structure per socket.
611eaf0ac3Slogwang  * Contains send and receive buffer queues,
621eaf0ac3Slogwang  * handle on protocol and pointer to protocol
631eaf0ac3Slogwang  * private data and error information.
641eaf0ac3Slogwang  */
65*616851daSagerguo typedef	int so_upcall_t(struct socket *, void *, int);
66*616851daSagerguo typedef	void so_dtor_t(struct socket *);
671eaf0ac3Slogwang 
681eaf0ac3Slogwang struct socket;
691eaf0ac3Slogwang 
70*616851daSagerguo enum socket_qstate {
71*616851daSagerguo 	SQ_NONE = 0,
72*616851daSagerguo 	SQ_INCOMP = 0x0800,	/* on sol_incomp */
73*616851daSagerguo 	SQ_COMP = 0x1000,	/* on sol_comp */
74*616851daSagerguo };
75*616851daSagerguo 
761eaf0ac3Slogwang /*-
771eaf0ac3Slogwang  * Locking key to struct socket:
781eaf0ac3Slogwang  * (a) constant after allocation, no locking required.
791eaf0ac3Slogwang  * (b) locked by SOCK_LOCK(so).
80*616851daSagerguo  * (cr) locked by SOCKBUF_LOCK(&so->so_rcv).
81*616851daSagerguo  * (cs) locked by SOCKBUF_LOCK(&so->so_snd).
82*616851daSagerguo  * (e) locked by SOLISTEN_LOCK() of corresponding listening socket.
831eaf0ac3Slogwang  * (f) not locked since integer reads/writes are atomic.
841eaf0ac3Slogwang  * (g) used only as a sleep/wakeup address, no value.
851eaf0ac3Slogwang  * (h) locked by global mutex so_global_mtx.
86*616851daSagerguo  * (k) locked by KTLS workqueue mutex
871eaf0ac3Slogwang  */
88*616851daSagerguo TAILQ_HEAD(accept_queue, socket);
891eaf0ac3Slogwang struct socket {
90*616851daSagerguo 	struct mtx	so_lock;
91*616851daSagerguo 	volatile u_int	so_count;	/* (b / refcount) */
92*616851daSagerguo 	struct selinfo	so_rdsel;	/* (b/cr) for so_rcv/so_comp */
93*616851daSagerguo 	struct selinfo	so_wrsel;	/* (b/cs) for so_snd */
941eaf0ac3Slogwang 	short	so_type;		/* (a) generic type, see socket.h */
95*616851daSagerguo 	int	so_options;		/* (b) from socket call, see socket.h */
96*616851daSagerguo 	short	so_linger;		/* time to linger close(2) */
971eaf0ac3Slogwang 	short	so_state;		/* (b) internal state flags SS_* */
981eaf0ac3Slogwang 	void	*so_pcb;		/* protocol control block */
991eaf0ac3Slogwang 	struct	vnet *so_vnet;		/* (a) network stack instance */
1001eaf0ac3Slogwang 	struct	protosw *so_proto;	/* (a) protocol handle */
1011eaf0ac3Slogwang 	short	so_timeo;		/* (g) connection timeout */
1021eaf0ac3Slogwang 	u_short	so_error;		/* (f) error affecting connection */
1031eaf0ac3Slogwang 	struct	sigio *so_sigio;	/* [sg] information for async I/O or
1041eaf0ac3Slogwang 					   out of band data (SIGURG) */
1051eaf0ac3Slogwang 	struct	ucred *so_cred;		/* (a) user credentials */
1061eaf0ac3Slogwang 	struct	label *so_label;	/* (b) MAC label for socket */
1071eaf0ac3Slogwang 	/* NB: generation count must not be first. */
1081eaf0ac3Slogwang 	so_gen_t so_gencnt;		/* (h) generation count */
1091eaf0ac3Slogwang 	void	*so_emuldata;		/* (b) private data for emulators */
110*616851daSagerguo 	so_dtor_t *so_dtor;		/* (b) optional destructor */
1111eaf0ac3Slogwang 	struct	osd	osd;		/* Object Specific extensions */
1121eaf0ac3Slogwang 	/*
1131eaf0ac3Slogwang 	 * so_fibnum, so_user_cookie and friends can be used to attach
1141eaf0ac3Slogwang 	 * some user-specified metadata to a socket, which then can be
1151eaf0ac3Slogwang 	 * used by the kernel for various actions.
1161eaf0ac3Slogwang 	 * so_user_cookie is used by ipfw/dummynet.
1171eaf0ac3Slogwang 	 */
1181eaf0ac3Slogwang 	int so_fibnum;		/* routing domain for this socket */
1191eaf0ac3Slogwang 	uint32_t so_user_cookie;
1201eaf0ac3Slogwang 
121*616851daSagerguo 	int so_ts_clock;	/* type of the clock used for timestamps */
122*616851daSagerguo 	uint32_t so_max_pacing_rate;	/* (f) TX rate limit in bytes/s */
123*616851daSagerguo 	union {
124*616851daSagerguo 		/* Regular (data flow) socket. */
125*616851daSagerguo 		struct {
126*616851daSagerguo 			/* (cr, cs) Receive and send buffers. */
127*616851daSagerguo 			struct sockbuf		so_rcv, so_snd;
128*616851daSagerguo 
129*616851daSagerguo 			/* (e) Our place on accept queue. */
130*616851daSagerguo 			TAILQ_ENTRY(socket)	so_list;
131*616851daSagerguo 			struct socket		*so_listen;	/* (b) */
132*616851daSagerguo 			enum socket_qstate so_qstate;		/* (b) */
133*616851daSagerguo 			/* (b) cached MAC label for peer */
134*616851daSagerguo 			struct	label		*so_peerlabel;
135*616851daSagerguo 			u_long	so_oobmark;	/* chars to oob mark */
136*616851daSagerguo 
137*616851daSagerguo 			/* (k) Our place on KTLS RX work queue. */
138*616851daSagerguo 			STAILQ_ENTRY(socket)	so_ktls_rx_list;
1391eaf0ac3Slogwang 		};
1401eaf0ac3Slogwang 		/*
141*616851daSagerguo 		 * Listening socket, where accepts occur, is so_listen in all
142*616851daSagerguo 		 * subsidiary sockets.  If so_listen is NULL, socket is not
143*616851daSagerguo 		 * related to an accept.  For a listening socket itself
144*616851daSagerguo 		 * sol_incomp queues partially completed connections, while
145*616851daSagerguo 		 * sol_comp is a queue of connections ready to be accepted.
146*616851daSagerguo 		 * If a connection is aborted and it has so_listen set, then
147*616851daSagerguo 		 * it has to be pulled out of either sol_incomp or sol_comp.
148*616851daSagerguo 		 * We allow connections to queue up based on current queue
149*616851daSagerguo 		 * lengths and limit on number of queued connections for this
150*616851daSagerguo 		 * socket.
1511eaf0ac3Slogwang 		 */
152*616851daSagerguo 		struct {
153*616851daSagerguo 			/* (e) queue of partial unaccepted connections */
154*616851daSagerguo 			struct accept_queue	sol_incomp;
155*616851daSagerguo 			/* (e) queue of complete unaccepted connections */
156*616851daSagerguo 			struct accept_queue	sol_comp;
157*616851daSagerguo 			u_int	sol_qlen;    /* (e) sol_comp length */
158*616851daSagerguo 			u_int	sol_incqlen; /* (e) sol_incomp length */
159*616851daSagerguo 			u_int	sol_qlimit;  /* (e) queue limit */
1601eaf0ac3Slogwang 
161*616851daSagerguo 			/* accept_filter(9) optional data */
162*616851daSagerguo 			struct	accept_filter	*sol_accept_filter;
163*616851daSagerguo 			void	*sol_accept_filter_arg;	/* saved filter args */
164*616851daSagerguo 			char	*sol_accept_filter_str;	/* saved user args */
1651eaf0ac3Slogwang 
166*616851daSagerguo 			/* Optional upcall, for kernel socket. */
167*616851daSagerguo 			so_upcall_t	*sol_upcall;	/* (e) */
168*616851daSagerguo 			void		*sol_upcallarg;	/* (e) */
1691eaf0ac3Slogwang 
170*616851daSagerguo 			/* Socket buffer parameters, to be copied to
171*616851daSagerguo 			 * dataflow sockets, accepted from this one. */
172*616851daSagerguo 			int		sol_sbrcv_lowat;
173*616851daSagerguo 			int		sol_sbsnd_lowat;
174*616851daSagerguo 			u_int		sol_sbrcv_hiwat;
175*616851daSagerguo 			u_int		sol_sbsnd_hiwat;
176*616851daSagerguo 			short		sol_sbrcv_flags;
177*616851daSagerguo 			short		sol_sbsnd_flags;
178*616851daSagerguo 			sbintime_t	sol_sbrcv_timeo;
179*616851daSagerguo 			sbintime_t	sol_sbsnd_timeo;
180*616851daSagerguo 
181*616851daSagerguo 			/* Information tracking listen queue overflows. */
182*616851daSagerguo 			struct timeval	sol_lastover;	/* (e) */
183*616851daSagerguo 			int		sol_overcount;	/* (e) */
1841eaf0ac3Slogwang 		};
185*616851daSagerguo 	};
186*616851daSagerguo #ifdef LVS_TCPOPT_TOA
187*616851daSagerguo 	uint8_t so_toa[8];  /* lvs toa option */
188*616851daSagerguo #endif
189*616851daSagerguo };
190*616851daSagerguo #endif	/* defined(_KERNEL) || defined(_WANT_SOCKET) */
191*616851daSagerguo 
192*616851daSagerguo /*
193*616851daSagerguo  * Socket state bits.
194*616851daSagerguo  *
195*616851daSagerguo  * Historically, these bits were all kept in the so_state field.
196*616851daSagerguo  * They are now split into separate, lock-specific fields.
197*616851daSagerguo  * so_state maintains basic socket state protected by the socket lock.
198*616851daSagerguo  * so_qstate holds information about the socket accept queues.
199*616851daSagerguo  * Each socket buffer also has a state field holding information
200*616851daSagerguo  * relevant to that socket buffer (can't send, rcv).
201*616851daSagerguo  * Many fields will be read without locks to improve performance and avoid
202*616851daSagerguo  * lock order issues.  However, this approach must be used with caution.
203*616851daSagerguo  */
204*616851daSagerguo #define	SS_NOFDREF		0x0001	/* no file table ref any more */
205*616851daSagerguo #define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
206*616851daSagerguo #define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
207*616851daSagerguo #define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
208*616851daSagerguo #define	SS_NBIO			0x0100	/* non-blocking ops */
209*616851daSagerguo #define	SS_ASYNC		0x0200	/* async i/o notify */
210*616851daSagerguo #define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
211*616851daSagerguo #define	SS_ISDISCONNECTED	0x2000	/* socket disconnected from peer */
212*616851daSagerguo 
213*616851daSagerguo /*
214*616851daSagerguo  * Protocols can mark a socket as SS_PROTOREF to indicate that, following
215*616851daSagerguo  * pru_detach, they still want the socket to persist, and will free it
216*616851daSagerguo  * themselves when they are done.  Protocols should only ever call sofree()
217*616851daSagerguo  * following setting this flag in pru_detach(), and never otherwise, as
218*616851daSagerguo  * sofree() bypasses socket reference counting.
219*616851daSagerguo  */
220*616851daSagerguo #define	SS_PROTOREF		0x4000	/* strong protocol reference */
2211eaf0ac3Slogwang 
2221eaf0ac3Slogwang #ifdef _KERNEL
2231eaf0ac3Slogwang 
224*616851daSagerguo #define	SOCK_MTX(so)		&(so)->so_lock
225*616851daSagerguo #define	SOCK_LOCK(so)		mtx_lock(&(so)->so_lock)
226*616851daSagerguo #define	SOCK_OWNED(so)		mtx_owned(&(so)->so_lock)
227*616851daSagerguo #define	SOCK_UNLOCK(so)		mtx_unlock(&(so)->so_lock)
228*616851daSagerguo #define	SOCK_LOCK_ASSERT(so)	mtx_assert(&(so)->so_lock, MA_OWNED)
229*616851daSagerguo #define	SOCK_UNLOCK_ASSERT(so)	mtx_assert(&(so)->so_lock, MA_NOTOWNED)
230*616851daSagerguo 
231*616851daSagerguo #define	SOLISTENING(sol)	(((sol)->so_options & SO_ACCEPTCONN) != 0)
232*616851daSagerguo #define	SOLISTEN_LOCK(sol)	do {					\
233*616851daSagerguo 	mtx_lock(&(sol)->so_lock);					\
234*616851daSagerguo 	KASSERT(SOLISTENING(sol),					\
235*616851daSagerguo 	    ("%s: %p not listening", __func__, (sol)));			\
236*616851daSagerguo } while (0)
237*616851daSagerguo #define	SOLISTEN_TRYLOCK(sol)	mtx_trylock(&(sol)->so_lock)
238*616851daSagerguo #define	SOLISTEN_UNLOCK(sol)	do {					\
239*616851daSagerguo 	KASSERT(SOLISTENING(sol),					\
240*616851daSagerguo 	    ("%s: %p not listening", __func__, (sol)));			\
241*616851daSagerguo 	mtx_unlock(&(sol)->so_lock);					\
242*616851daSagerguo } while (0)
243*616851daSagerguo #define	SOLISTEN_LOCK_ASSERT(sol)	do {				\
244*616851daSagerguo 	mtx_assert(&(sol)->so_lock, MA_OWNED);				\
245*616851daSagerguo 	KASSERT(SOLISTENING(sol),					\
246*616851daSagerguo 	    ("%s: %p not listening", __func__, (sol)));			\
247*616851daSagerguo } while (0)
248*616851daSagerguo 
2491eaf0ac3Slogwang /*
2501eaf0ac3Slogwang  * Macros for sockets and socket buffering.
2511eaf0ac3Slogwang  */
2521eaf0ac3Slogwang 
2531eaf0ac3Slogwang /*
2541eaf0ac3Slogwang  * Flags to sblock().
2551eaf0ac3Slogwang  */
2561eaf0ac3Slogwang #define	SBL_WAIT	0x00000001	/* Wait if not immediately available. */
2571eaf0ac3Slogwang #define	SBL_NOINTR	0x00000002	/* Force non-interruptible sleep. */
2581eaf0ac3Slogwang #define	SBL_VALID	(SBL_WAIT | SBL_NOINTR)
2591eaf0ac3Slogwang 
2601eaf0ac3Slogwang /*
2611eaf0ac3Slogwang  * Do we need to notify the other side when I/O is possible?
2621eaf0ac3Slogwang  */
2631eaf0ac3Slogwang #define	sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
2641eaf0ac3Slogwang     SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
2651eaf0ac3Slogwang 
2661eaf0ac3Slogwang /* do we have to send all at once on a socket? */
2671eaf0ac3Slogwang #define	sosendallatonce(so) \
2681eaf0ac3Slogwang     ((so)->so_proto->pr_flags & PR_ATOMIC)
2691eaf0ac3Slogwang 
2701eaf0ac3Slogwang /* can we read something from so? */
2711eaf0ac3Slogwang #define	soreadabledata(so) \
272*616851daSagerguo 	(sbavail(&(so)->so_rcv) >= (so)->so_rcv.sb_lowat ||  (so)->so_error)
2731eaf0ac3Slogwang #define	soreadable(so) \
2741eaf0ac3Slogwang 	(soreadabledata(so) || ((so)->so_rcv.sb_state & SBS_CANTRCVMORE))
2751eaf0ac3Slogwang 
2761eaf0ac3Slogwang /* can we write something to so? */
2771eaf0ac3Slogwang #define	sowriteable(so) \
2781eaf0ac3Slogwang     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
2791eaf0ac3Slogwang 	(((so)->so_state&SS_ISCONNECTED) || \
2801eaf0ac3Slogwang 	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
2811eaf0ac3Slogwang      ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
2821eaf0ac3Slogwang      (so)->so_error)
2831eaf0ac3Slogwang 
2841eaf0ac3Slogwang /*
285*616851daSagerguo  * soref()/sorele() ref-count the socket structure.
286*616851daSagerguo  * soref() may be called without owning socket lock, but in that case a
287*616851daSagerguo  * caller must own something that holds socket, and so_count must be not 0.
288*616851daSagerguo  * Note that you must still explicitly close the socket, but the last ref
289*616851daSagerguo  * count will free the structure.
2901eaf0ac3Slogwang  */
291*616851daSagerguo #define	soref(so)	refcount_acquire(&(so)->so_count)
2921eaf0ac3Slogwang #define	sorele(so) do {							\
2931eaf0ac3Slogwang 	SOCK_LOCK_ASSERT(so);						\
294*616851daSagerguo 	if (refcount_release(&(so)->so_count))				\
2951eaf0ac3Slogwang 		sofree(so);						\
296*616851daSagerguo 	else								\
2971eaf0ac3Slogwang 		SOCK_UNLOCK(so);					\
2981eaf0ac3Slogwang } while (0)
2991eaf0ac3Slogwang 
3001eaf0ac3Slogwang /*
3011eaf0ac3Slogwang  * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to
3021eaf0ac3Slogwang  * avoid a non-atomic test-and-wakeup.  However, sowakeup is
3031eaf0ac3Slogwang  * responsible for releasing the lock if it is called.  We unlock only
3041eaf0ac3Slogwang  * if we don't call into sowakeup.  If any code is introduced that
3051eaf0ac3Slogwang  * directly invokes the underlying sowakeup() primitives, it must
3061eaf0ac3Slogwang  * maintain the same semantics.
3071eaf0ac3Slogwang  */
3081eaf0ac3Slogwang #define	sorwakeup_locked(so) do {					\
3091eaf0ac3Slogwang 	SOCKBUF_LOCK_ASSERT(&(so)->so_rcv);				\
3101eaf0ac3Slogwang 	if (sb_notify(&(so)->so_rcv))					\
3111eaf0ac3Slogwang 		sowakeup((so), &(so)->so_rcv);	 			\
3121eaf0ac3Slogwang 	else								\
3131eaf0ac3Slogwang 		SOCKBUF_UNLOCK(&(so)->so_rcv);				\
3141eaf0ac3Slogwang } while (0)
3151eaf0ac3Slogwang 
3161eaf0ac3Slogwang #define	sorwakeup(so) do {						\
3171eaf0ac3Slogwang 	SOCKBUF_LOCK(&(so)->so_rcv);					\
3181eaf0ac3Slogwang 	sorwakeup_locked(so);						\
3191eaf0ac3Slogwang } while (0)
3201eaf0ac3Slogwang 
3211eaf0ac3Slogwang #define	sowwakeup_locked(so) do {					\
3221eaf0ac3Slogwang 	SOCKBUF_LOCK_ASSERT(&(so)->so_snd);				\
3231eaf0ac3Slogwang 	if (sb_notify(&(so)->so_snd))					\
3241eaf0ac3Slogwang 		sowakeup((so), &(so)->so_snd); 				\
3251eaf0ac3Slogwang 	else								\
3261eaf0ac3Slogwang 		SOCKBUF_UNLOCK(&(so)->so_snd);				\
3271eaf0ac3Slogwang } while (0)
3281eaf0ac3Slogwang 
3291eaf0ac3Slogwang #define	sowwakeup(so) do {						\
3301eaf0ac3Slogwang 	SOCKBUF_LOCK(&(so)->so_snd);					\
3311eaf0ac3Slogwang 	sowwakeup_locked(so);						\
3321eaf0ac3Slogwang } while (0)
3331eaf0ac3Slogwang 
3341eaf0ac3Slogwang struct accept_filter {
3351eaf0ac3Slogwang 	char	accf_name[16];
3361eaf0ac3Slogwang 	int	(*accf_callback)
3371eaf0ac3Slogwang 		(struct socket *so, void *arg, int waitflag);
3381eaf0ac3Slogwang 	void *	(*accf_create)
3391eaf0ac3Slogwang 		(struct socket *so, char *arg);
3401eaf0ac3Slogwang 	void	(*accf_destroy)
3411eaf0ac3Slogwang 		(struct socket *so);
3421eaf0ac3Slogwang 	SLIST_ENTRY(accept_filter) accf_next;
3431eaf0ac3Slogwang };
3441eaf0ac3Slogwang 
345*616851daSagerguo #define	ACCEPT_FILTER_DEFINE(modname, filtname, cb, create, destroy, ver) \
346*616851daSagerguo 	static struct accept_filter modname##_filter = {		\
347*616851daSagerguo 		.accf_name = filtname,					\
348*616851daSagerguo 		.accf_callback = cb,					\
349*616851daSagerguo 		.accf_create = create,					\
350*616851daSagerguo 		.accf_destroy = destroy,				\
351*616851daSagerguo 	};								\
352*616851daSagerguo 	static moduledata_t modname##_mod = {				\
353*616851daSagerguo 		.name = __XSTRING(modname),				\
354*616851daSagerguo 		.evhand = accept_filt_generic_mod_event,		\
355*616851daSagerguo 		.priv = &modname##_filter,				\
356*616851daSagerguo 	};								\
357*616851daSagerguo 	DECLARE_MODULE(modname, modname##_mod, SI_SUB_DRIVERS,		\
358*616851daSagerguo 	    SI_ORDER_MIDDLE);						\
359*616851daSagerguo 	MODULE_VERSION(modname, ver)
360*616851daSagerguo 
3611eaf0ac3Slogwang #ifdef MALLOC_DECLARE
3621eaf0ac3Slogwang MALLOC_DECLARE(M_ACCF);
3631eaf0ac3Slogwang MALLOC_DECLARE(M_PCB);
3641eaf0ac3Slogwang MALLOC_DECLARE(M_SONAME);
3651eaf0ac3Slogwang #endif
3661eaf0ac3Slogwang 
3671eaf0ac3Slogwang /*
3681eaf0ac3Slogwang  * Socket specific helper hook point identifiers
3691eaf0ac3Slogwang  * Do not leave holes in the sequence, hook registration is a loop.
3701eaf0ac3Slogwang  */
3711eaf0ac3Slogwang #define HHOOK_SOCKET_OPT		0
3721eaf0ac3Slogwang #define HHOOK_SOCKET_CREATE		1
3731eaf0ac3Slogwang #define HHOOK_SOCKET_RCV 		2
3741eaf0ac3Slogwang #define HHOOK_SOCKET_SND		3
3751eaf0ac3Slogwang #define HHOOK_FILT_SOREAD		4
3761eaf0ac3Slogwang #define HHOOK_FILT_SOWRITE		5
3771eaf0ac3Slogwang #define HHOOK_SOCKET_CLOSE		6
3781eaf0ac3Slogwang #define HHOOK_SOCKET_LAST		HHOOK_SOCKET_CLOSE
3791eaf0ac3Slogwang 
3801eaf0ac3Slogwang struct socket_hhook_data {
3811eaf0ac3Slogwang 	struct socket	*so;
3821eaf0ac3Slogwang 	struct mbuf	*m;
3831eaf0ac3Slogwang 	void		*hctx;		/* hook point specific data*/
3841eaf0ac3Slogwang 	int		status;
3851eaf0ac3Slogwang };
3861eaf0ac3Slogwang 
3871eaf0ac3Slogwang extern int	maxsockets;
3881eaf0ac3Slogwang extern u_long	sb_max;
3891eaf0ac3Slogwang extern so_gen_t so_gencnt;
3901eaf0ac3Slogwang 
3911eaf0ac3Slogwang struct file;
392*616851daSagerguo struct filecaps;
3931eaf0ac3Slogwang struct filedesc;
3941eaf0ac3Slogwang struct mbuf;
3951eaf0ac3Slogwang struct sockaddr;
3961eaf0ac3Slogwang struct ucred;
3971eaf0ac3Slogwang struct uio;
3981eaf0ac3Slogwang 
3991eaf0ac3Slogwang /* 'which' values for socket upcalls. */
4001eaf0ac3Slogwang #define	SO_RCV		1
4011eaf0ac3Slogwang #define	SO_SND		2
4021eaf0ac3Slogwang 
4031eaf0ac3Slogwang /* Return values for socket upcalls. */
4041eaf0ac3Slogwang #define	SU_OK		0
4051eaf0ac3Slogwang #define	SU_ISCONNECTED	1
4061eaf0ac3Slogwang 
4071eaf0ac3Slogwang /*
4081eaf0ac3Slogwang  * From uipc_socket and friends
4091eaf0ac3Slogwang  */
410*616851daSagerguo int	getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr,
411*616851daSagerguo 	    size_t len);
4121eaf0ac3Slogwang int	getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
413*616851daSagerguo 	    struct file **fpp, u_int *fflagp, struct filecaps *havecaps);
4141eaf0ac3Slogwang void	soabort(struct socket *so);
4151eaf0ac3Slogwang int	soaccept(struct socket *so, struct sockaddr **nam);
4161eaf0ac3Slogwang void	soaio_enqueue(struct task *task);
4171eaf0ac3Slogwang void	soaio_rcv(void *context, int pending);
4181eaf0ac3Slogwang void	soaio_snd(void *context, int pending);
4191eaf0ac3Slogwang int	socheckuid(struct socket *so, uid_t uid);
4201eaf0ac3Slogwang int	sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
4211eaf0ac3Slogwang int	sobindat(int fd, struct socket *so, struct sockaddr *nam,
4221eaf0ac3Slogwang 	    struct thread *td);
4231eaf0ac3Slogwang int	soclose(struct socket *so);
4241eaf0ac3Slogwang int	soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
4251eaf0ac3Slogwang int	soconnectat(int fd, struct socket *so, struct sockaddr *nam,
4261eaf0ac3Slogwang 	    struct thread *td);
4271eaf0ac3Slogwang int	soconnect2(struct socket *so1, struct socket *so2);
4281eaf0ac3Slogwang int	socreate(int dom, struct socket **aso, int type, int proto,
4291eaf0ac3Slogwang 	    struct ucred *cred, struct thread *td);
4301eaf0ac3Slogwang int	sodisconnect(struct socket *so);
431*616851daSagerguo void	sodtor_set(struct socket *, so_dtor_t *);
4321eaf0ac3Slogwang struct	sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags);
4331eaf0ac3Slogwang void	sofree(struct socket *so);
4341eaf0ac3Slogwang void	sohasoutofband(struct socket *so);
4351eaf0ac3Slogwang int	solisten(struct socket *so, int backlog, struct thread *td);
4361eaf0ac3Slogwang void	solisten_proto(struct socket *so, int backlog);
4371eaf0ac3Slogwang int	solisten_proto_check(struct socket *so);
438*616851daSagerguo int	solisten_dequeue(struct socket *, struct socket **, int);
4391eaf0ac3Slogwang struct socket *
4401eaf0ac3Slogwang 	sonewconn(struct socket *head, int connstatus);
441*616851daSagerguo struct socket *
442*616851daSagerguo 	sopeeloff(struct socket *);
4431eaf0ac3Slogwang int	sopoll(struct socket *so, int events, struct ucred *active_cred,
4441eaf0ac3Slogwang 	    struct thread *td);
4451eaf0ac3Slogwang int	sopoll_generic(struct socket *so, int events,
4461eaf0ac3Slogwang 	    struct ucred *active_cred, struct thread *td);
4471eaf0ac3Slogwang int	soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
4481eaf0ac3Slogwang 	    struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
4491eaf0ac3Slogwang int	soreceive_stream(struct socket *so, struct sockaddr **paddr,
4501eaf0ac3Slogwang 	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
4511eaf0ac3Slogwang 	    int *flagsp);
4521eaf0ac3Slogwang int	soreceive_dgram(struct socket *so, struct sockaddr **paddr,
4531eaf0ac3Slogwang 	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
4541eaf0ac3Slogwang 	    int *flagsp);
4551eaf0ac3Slogwang int	soreceive_generic(struct socket *so, struct sockaddr **paddr,
4561eaf0ac3Slogwang 	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
4571eaf0ac3Slogwang 	    int *flagsp);
4581eaf0ac3Slogwang int	soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
4591eaf0ac3Slogwang void	sorflush(struct socket *so);
4601eaf0ac3Slogwang int	sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
4611eaf0ac3Slogwang 	    struct mbuf *top, struct mbuf *control, int flags,
4621eaf0ac3Slogwang 	    struct thread *td);
4631eaf0ac3Slogwang int	sosend_dgram(struct socket *so, struct sockaddr *addr,
4641eaf0ac3Slogwang 	    struct uio *uio, struct mbuf *top, struct mbuf *control,
4651eaf0ac3Slogwang 	    int flags, struct thread *td);
4661eaf0ac3Slogwang int	sosend_generic(struct socket *so, struct sockaddr *addr,
4671eaf0ac3Slogwang 	    struct uio *uio, struct mbuf *top, struct mbuf *control,
4681eaf0ac3Slogwang 	    int flags, struct thread *td);
4691eaf0ac3Slogwang int	soshutdown(struct socket *so, int how);
470*616851daSagerguo void	soupcall_clear(struct socket *, int);
471*616851daSagerguo void	soupcall_set(struct socket *, int, so_upcall_t, void *);
472*616851daSagerguo void	solisten_upcall_set(struct socket *, so_upcall_t, void *);
4731eaf0ac3Slogwang void	sowakeup(struct socket *so, struct sockbuf *sb);
4741eaf0ac3Slogwang void	sowakeup_aio(struct socket *so, struct sockbuf *sb);
475*616851daSagerguo void	solisten_wakeup(struct socket *);
4761eaf0ac3Slogwang int	selsocket(struct socket *so, int events, struct timeval *tv,
4771eaf0ac3Slogwang 	    struct thread *td);
478*616851daSagerguo void	soisconnected(struct socket *so);
479*616851daSagerguo void	soisconnecting(struct socket *so);
480*616851daSagerguo void	soisdisconnected(struct socket *so);
481*616851daSagerguo void	soisdisconnecting(struct socket *so);
482*616851daSagerguo void	socantrcvmore(struct socket *so);
483*616851daSagerguo void	socantrcvmore_locked(struct socket *so);
484*616851daSagerguo void	socantsendmore(struct socket *so);
485*616851daSagerguo void	socantsendmore_locked(struct socket *so);
4861eaf0ac3Slogwang 
4871eaf0ac3Slogwang /*
4881eaf0ac3Slogwang  * Accept filter functions (duh).
4891eaf0ac3Slogwang  */
4901eaf0ac3Slogwang int	accept_filt_add(struct accept_filter *filt);
4911eaf0ac3Slogwang int	accept_filt_del(char *name);
4921eaf0ac3Slogwang struct	accept_filter *accept_filt_get(char *name);
4931eaf0ac3Slogwang #ifdef ACCEPT_FILTER_MOD
4941eaf0ac3Slogwang #ifdef SYSCTL_DECL
4951eaf0ac3Slogwang SYSCTL_DECL(_net_inet_accf);
4961eaf0ac3Slogwang #endif
4971eaf0ac3Slogwang int	accept_filt_generic_mod_event(module_t mod, int event, void *data);
4981eaf0ac3Slogwang #endif
4991eaf0ac3Slogwang 
5001eaf0ac3Slogwang #endif /* _KERNEL */
5011eaf0ac3Slogwang 
502*616851daSagerguo /*
503*616851daSagerguo  * Structure to export socket from kernel to utilities, via sysctl(3).
504*616851daSagerguo  */
505*616851daSagerguo struct xsocket {
506*616851daSagerguo 	ksize_t		xso_len;	/* length of this structure */
507*616851daSagerguo 	kvaddr_t	xso_so;		/* kernel address of struct socket */
508*616851daSagerguo 	kvaddr_t	so_pcb;		/* kernel address of struct inpcb */
509*616851daSagerguo 	uint64_t	so_oobmark;
510*616851daSagerguo 	int64_t		so_spare64[8];
511*616851daSagerguo 	int32_t		xso_protocol;
512*616851daSagerguo 	int32_t		xso_family;
513*616851daSagerguo 	uint32_t	so_qlen;
514*616851daSagerguo 	uint32_t	so_incqlen;
515*616851daSagerguo 	uint32_t	so_qlimit;
516*616851daSagerguo 	pid_t		so_pgid;
517*616851daSagerguo 	uid_t		so_uid;
518*616851daSagerguo 	int32_t		so_spare32[8];
519*616851daSagerguo 	int16_t		so_type;
520*616851daSagerguo 	int16_t		so_options;
521*616851daSagerguo 	int16_t		so_linger;
522*616851daSagerguo 	int16_t		so_state;
523*616851daSagerguo 	int16_t		so_timeo;
524*616851daSagerguo 	uint16_t	so_error;
525*616851daSagerguo 	struct xsockbuf {
526*616851daSagerguo 		uint32_t	sb_cc;
527*616851daSagerguo 		uint32_t	sb_hiwat;
528*616851daSagerguo 		uint32_t	sb_mbcnt;
529*616851daSagerguo 		uint32_t	sb_mcnt;
530*616851daSagerguo 		uint32_t	sb_ccnt;
531*616851daSagerguo 		uint32_t	sb_mbmax;
532*616851daSagerguo 		int32_t		sb_lowat;
533*616851daSagerguo 		int32_t		sb_timeo;
534*616851daSagerguo 		int16_t		sb_flags;
535*616851daSagerguo 	} so_rcv, so_snd;
536*616851daSagerguo };
537*616851daSagerguo 
538*616851daSagerguo #ifdef _KERNEL
539*616851daSagerguo void	sotoxsocket(struct socket *so, struct xsocket *xso);
540*616851daSagerguo void	sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
541*616851daSagerguo #endif
542*616851daSagerguo 
543*616851daSagerguo /*
544*616851daSagerguo  * Socket buffer state bits.  Exported via libprocstat(3).
545*616851daSagerguo  */
546*616851daSagerguo #define	SBS_CANTSENDMORE	0x0010	/* can't send more data to peer */
547*616851daSagerguo #define	SBS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
548*616851daSagerguo #define	SBS_RCVATMARK		0x0040	/* at mark on input */
549*616851daSagerguo 
5501eaf0ac3Slogwang #endif /* !_SYS_SOCKETVAR_H_ */
551