socketvar.h revision 321021
1/*-
2 * Copyright (c) 1982, 1986, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)socketvar.h	8.3 (Berkeley) 2/19/95
30 *
31 * $FreeBSD: stable/10/sys/sys/socketvar.h 321021 2017-07-15 17:28:03Z dchagin $
32 */
33
34#ifndef _SYS_SOCKETVAR_H_
35#define _SYS_SOCKETVAR_H_
36
37#include <sys/queue.h>			/* for TAILQ macros */
38#include <sys/selinfo.h>		/* for struct selinfo */
39#include <sys/_lock.h>
40#include <sys/_mutex.h>
41#include <sys/_sx.h>
42#include <sys/sockbuf.h>
43#include <sys/sockstate.h>
44#ifdef _KERNEL
45#include <sys/caprights.h>
46#include <sys/sockopt.h>
47#endif
48
49struct vnet;
50
51/*
52 * Kernel structure per socket.
53 * Contains send and receive buffer queues,
54 * handle on protocol and pointer to protocol
55 * private data and error information.
56 */
57typedef	u_quad_t so_gen_t;
58
59struct socket;
60
61/*-
62 * Locking key to struct socket:
63 * (a) constant after allocation, no locking required.
64 * (b) locked by SOCK_LOCK(so).
65 * (c) locked by SOCKBUF_LOCK(&so->so_rcv).
66 * (d) locked by SOCKBUF_LOCK(&so->so_snd).
67 * (e) locked by ACCEPT_LOCK().
68 * (f) not locked since integer reads/writes are atomic.
69 * (g) used only as a sleep/wakeup address, no value.
70 * (h) locked by global mutex so_global_mtx.
71 */
72struct socket {
73	int	so_count;		/* (b) reference count */
74	short	so_type;		/* (a) generic type, see socket.h */
75	short	so_options;		/* from socket call, see socket.h */
76	short	so_linger;		/* time to linger while closing */
77	short	so_state;		/* (b) internal state flags SS_* */
78	int	so_qstate;		/* (e) internal state flags SQ_* */
79	void	*so_pcb;		/* protocol control block */
80	struct	vnet *so_vnet;		/* (a) network stack instance */
81	struct	protosw *so_proto;	/* (a) protocol handle */
82/*
83 * Variables for connection queuing.
84 * Socket where accepts occur is so_head in all subsidiary sockets.
85 * If so_head is 0, socket is not related to an accept.
86 * For head socket so_incomp queues partially completed connections,
87 * while so_comp is a queue of connections ready to be accepted.
88 * If a connection is aborted and it has so_head set, then
89 * it has to be pulled out of either so_incomp or so_comp.
90 * We allow connections to queue up based on current queue lengths
91 * and limit on number of queued connections for this socket.
92 */
93	struct	socket *so_head;	/* (e) back pointer to listen socket */
94	TAILQ_HEAD(, socket) so_incomp;	/* (e) queue of partial unaccepted connections */
95	TAILQ_HEAD(, socket) so_comp;	/* (e) queue of complete unaccepted connections */
96	TAILQ_ENTRY(socket) so_list;	/* (e) list of unaccepted connections */
97	u_short	so_qlen;		/* (e) number of unaccepted connections */
98	u_short	so_incqlen;		/* (e) number of unaccepted incomplete
99					   connections */
100	u_short	so_qlimit;		/* (e) max number queued connections */
101	short	so_timeo;		/* (g) connection timeout */
102	u_short	so_error;		/* (f) error affecting connection */
103	struct	sigio *so_sigio;	/* [sg] information for async I/O or
104					   out of band data (SIGURG) */
105	u_long	so_oobmark;		/* (c) chars to oob mark */
106	TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
107
108	struct sockbuf so_rcv, so_snd;
109
110	struct	ucred *so_cred;		/* (a) user credentials */
111	struct	label *so_label;	/* (b) MAC label for socket */
112	struct	label *so_peerlabel;	/* (b) cached MAC label for peer */
113	/* NB: generation count must not be first. */
114	so_gen_t so_gencnt;		/* (h) generation count */
115	void	*so_emuldata;		/* (b) private data for emulators */
116 	struct so_accf {
117		struct	accept_filter *so_accept_filter;
118		void	*so_accept_filter_arg;	/* saved filter args */
119		char	*so_accept_filter_str;	/* saved user args */
120	} *so_accf;
121	/*
122	 * so_fibnum, so_user_cookie and friends can be used to attach
123	 * some user-specified metadata to a socket, which then can be
124	 * used by the kernel for various actions.
125	 * so_user_cookie is used by ipfw/dummynet.
126	 */
127	int so_fibnum;		/* routing domain for this socket */
128	uint32_t so_user_cookie;
129};
130
131/*
132 * Global accept mutex to serialize access to accept queues and
133 * fields associated with multiple sockets.  This allows us to
134 * avoid defining a lock order between listen and accept sockets
135 * until such time as it proves to be a good idea.
136 */
137extern struct mtx accept_mtx;
138#define	ACCEPT_LOCK_ASSERT()		mtx_assert(&accept_mtx, MA_OWNED)
139#define	ACCEPT_UNLOCK_ASSERT()		mtx_assert(&accept_mtx, MA_NOTOWNED)
140#define	ACCEPT_LOCK()			mtx_lock(&accept_mtx)
141#define	ACCEPT_UNLOCK()			mtx_unlock(&accept_mtx)
142
143/*
144 * Per-socket mutex: we reuse the receive socket buffer mutex for space
145 * efficiency.  This decision should probably be revisited as we optimize
146 * locking for the socket code.
147 */
148#define	SOCK_MTX(_so)			SOCKBUF_MTX(&(_so)->so_rcv)
149#define	SOCK_LOCK(_so)			SOCKBUF_LOCK(&(_so)->so_rcv)
150#define	SOCK_OWNED(_so)			SOCKBUF_OWNED(&(_so)->so_rcv)
151#define	SOCK_UNLOCK(_so)		SOCKBUF_UNLOCK(&(_so)->so_rcv)
152#define	SOCK_LOCK_ASSERT(_so)		SOCKBUF_LOCK_ASSERT(&(_so)->so_rcv)
153
154/*
155 * Socket state bits stored in so_qstate.
156 */
157#define	SQ_INCOMP		0x0800	/* unaccepted, incomplete connection */
158#define	SQ_COMP			0x1000	/* unaccepted, complete connection */
159
160/*
161 * Externalized form of struct socket used by the sysctl(3) interface.
162 */
163struct xsocket {
164	size_t	xso_len;	/* length of this structure */
165	struct	socket *xso_so;	/* makes a convenient handle sometimes */
166	short	so_type;
167	short	so_options;
168	short	so_linger;
169	short	so_state;
170	caddr_t	so_pcb;		/* another convenient handle */
171	int	xso_protocol;
172	int	xso_family;
173	u_short	so_qlen;
174	u_short	so_incqlen;
175	u_short	so_qlimit;
176	short	so_timeo;
177	u_short	so_error;
178	pid_t	so_pgid;
179	u_long	so_oobmark;
180	struct xsockbuf so_rcv, so_snd;
181	uid_t	so_uid;		/* XXX */
182};
183
184#ifdef _KERNEL
185
186/*
187 * Macros for sockets and socket buffering.
188 */
189
190/*
191 * Flags to sblock().
192 */
193#define	SBL_WAIT	0x00000001	/* Wait if not immediately available. */
194#define	SBL_NOINTR	0x00000002	/* Force non-interruptible sleep. */
195#define	SBL_VALID	(SBL_WAIT | SBL_NOINTR)
196
197/*
198 * Do we need to notify the other side when I/O is possible?
199 */
200#define	sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
201    SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
202
203/* do we have to send all at once on a socket? */
204#define	sosendallatonce(so) \
205    ((so)->so_proto->pr_flags & PR_ATOMIC)
206
207/* can we read something from so? */
208#define	soreadabledata(so) \
209    ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
210	!TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
211#define	soreadable(so) \
212	(soreadabledata(so) || ((so)->so_rcv.sb_state & SBS_CANTRCVMORE))
213
214/* can we write something to so? */
215#define	sowriteable(so) \
216    ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
217	(((so)->so_state&SS_ISCONNECTED) || \
218	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
219     ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
220     (so)->so_error)
221
222/*
223 * soref()/sorele() ref-count the socket structure.  Note that you must
224 * still explicitly close the socket, but the last ref count will free
225 * the structure.
226 */
227#define	soref(so) do {							\
228	SOCK_LOCK_ASSERT(so);						\
229	++(so)->so_count;						\
230} while (0)
231
232#define	sorele(so) do {							\
233	ACCEPT_LOCK_ASSERT();						\
234	SOCK_LOCK_ASSERT(so);						\
235	if ((so)->so_count <= 0)					\
236		panic("sorele");					\
237	if (--(so)->so_count == 0)					\
238		sofree(so);						\
239	else {								\
240		SOCK_UNLOCK(so);					\
241		ACCEPT_UNLOCK();					\
242	}								\
243} while (0)
244
245/*
246 * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to
247 * avoid a non-atomic test-and-wakeup.  However, sowakeup is
248 * responsible for releasing the lock if it is called.  We unlock only
249 * if we don't call into sowakeup.  If any code is introduced that
250 * directly invokes the underlying sowakeup() primitives, it must
251 * maintain the same semantics.
252 */
253#define	sorwakeup_locked(so) do {					\
254	SOCKBUF_LOCK_ASSERT(&(so)->so_rcv);				\
255	if (sb_notify(&(so)->so_rcv))					\
256		sowakeup((so), &(so)->so_rcv);	 			\
257	else								\
258		SOCKBUF_UNLOCK(&(so)->so_rcv);				\
259} while (0)
260
261#define	sorwakeup(so) do {						\
262	SOCKBUF_LOCK(&(so)->so_rcv);					\
263	sorwakeup_locked(so);						\
264} while (0)
265
266#define	sowwakeup_locked(so) do {					\
267	SOCKBUF_LOCK_ASSERT(&(so)->so_snd);				\
268	if (sb_notify(&(so)->so_snd))					\
269		sowakeup((so), &(so)->so_snd); 				\
270	else								\
271		SOCKBUF_UNLOCK(&(so)->so_snd);				\
272} while (0)
273
274#define	sowwakeup(so) do {						\
275	SOCKBUF_LOCK(&(so)->so_snd);					\
276	sowwakeup_locked(so);						\
277} while (0)
278
279struct accept_filter {
280	char	accf_name[16];
281	int	(*accf_callback)
282		(struct socket *so, void *arg, int waitflag);
283	void *	(*accf_create)
284		(struct socket *so, char *arg);
285	void	(*accf_destroy)
286		(struct socket *so);
287	SLIST_ENTRY(accept_filter) accf_next;
288};
289
290#ifdef MALLOC_DECLARE
291MALLOC_DECLARE(M_ACCF);
292MALLOC_DECLARE(M_PCB);
293MALLOC_DECLARE(M_SONAME);
294#endif
295
296extern int	maxsockets;
297extern u_long	sb_max;
298extern so_gen_t so_gencnt;
299
300struct file;
301struct filedesc;
302struct mbuf;
303struct sockaddr;
304struct ucred;
305struct uio;
306
307/* 'which' values for socket upcalls. */
308#define	SO_RCV		1
309#define	SO_SND		2
310
311/* Return values for socket upcalls. */
312#define	SU_OK		0
313#define	SU_ISCONNECTED	1
314
315/*
316 * From uipc_socket and friends
317 */
318int	sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type);
319int	getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len);
320int	getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
321	    struct file **fpp, u_int *fflagp);
322void	soabort(struct socket *so);
323int	soaccept(struct socket *so, struct sockaddr **nam);
324int	socheckuid(struct socket *so, uid_t uid);
325int	sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
326int	sobindat(int fd, struct socket *so, struct sockaddr *nam,
327	    struct thread *td);
328int	soclose(struct socket *so);
329int	soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
330int	soconnectat(int fd, struct socket *so, struct sockaddr *nam,
331	    struct thread *td);
332int	soconnect2(struct socket *so1, struct socket *so2);
333int	socreate(int dom, struct socket **aso, int type, int proto,
334	    struct ucred *cred, struct thread *td);
335int	sodisconnect(struct socket *so);
336struct	sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags);
337void	sofree(struct socket *so);
338void	sohasoutofband(struct socket *so);
339int	solisten(struct socket *so, int backlog, struct thread *td);
340void	solisten_proto(struct socket *so, int backlog);
341int	solisten_proto_check(struct socket *so);
342struct socket *
343	sonewconn(struct socket *head, int connstatus);
344
345
346int	sopoll(struct socket *so, int events, struct ucred *active_cred,
347	    struct thread *td);
348int	sopoll_generic(struct socket *so, int events,
349	    struct ucred *active_cred, struct thread *td);
350int	soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
351	    struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
352int	soreceive_stream(struct socket *so, struct sockaddr **paddr,
353	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
354	    int *flagsp);
355int	soreceive_dgram(struct socket *so, struct sockaddr **paddr,
356	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
357	    int *flagsp);
358int	soreceive_generic(struct socket *so, struct sockaddr **paddr,
359	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
360	    int *flagsp);
361int	soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
362void	sorflush(struct socket *so);
363int	sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
364	    struct mbuf *top, struct mbuf *control, int flags,
365	    struct thread *td);
366int	sosend_dgram(struct socket *so, struct sockaddr *addr,
367	    struct uio *uio, struct mbuf *top, struct mbuf *control,
368	    int flags, struct thread *td);
369int	sosend_generic(struct socket *so, struct sockaddr *addr,
370	    struct uio *uio, struct mbuf *top, struct mbuf *control,
371	    int flags, struct thread *td);
372int	soshutdown(struct socket *so, int how);
373void	sotoxsocket(struct socket *so, struct xsocket *xso);
374void	soupcall_clear(struct socket *so, int which);
375void	soupcall_set(struct socket *so, int which,
376	    int (*func)(struct socket *, void *, int), void *arg);
377void	sowakeup(struct socket *so, struct sockbuf *sb);
378int	selsocket(struct socket *so, int events, struct timeval *tv,
379	    struct thread *td);
380
381/*
382 * Accept filter functions (duh).
383 */
384int	accept_filt_add(struct accept_filter *filt);
385int	accept_filt_del(char *name);
386struct	accept_filter *accept_filt_get(char *name);
387#ifdef ACCEPT_FILTER_MOD
388#ifdef SYSCTL_DECL
389SYSCTL_DECL(_net_inet_accf);
390#endif
391int	accept_filt_generic_mod_event(module_t mod, int event, void *data);
392#endif
393
394#endif /* _KERNEL */
395
396#endif /* !_SYS_SOCKETVAR_H_ */
397