socketvar.h revision 130492
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 * $FreeBSD: head/sys/sys/socketvar.h 130492 2004-06-14 22:03:14Z rwatson $
31 */
32
33#ifndef _SYS_SOCKETVAR_H_
34#define _SYS_SOCKETVAR_H_
35
36#include <sys/queue.h>			/* for TAILQ macros */
37#include <sys/selinfo.h>		/* for struct selinfo */
38#include <sys/_lock.h>
39#include <sys/_mutex.h>
40
41/*
42 * Kernel structure per socket.
43 * Contains send and receive buffer queues,
44 * handle on protocol and pointer to protocol
45 * private data and error information.
46 */
47typedef	u_quad_t so_gen_t;
48
49/*-
50 * Locking key to struct socket:
51 * (a) constant after allocation, no locking required.
52 * (b) locked by SOCK_LOCK(so).
53 * (c) locked by SOCKBUF_LOCK(&so->so_rcv).
54 * (d) locked by SOCKBUF_LOCK(&so->so_snd).
55 * (e) locked by ACCEPT_LOCK().
56 * (f) not locked since integer reads/writes are atomic.
57 * (g) used only as a sleep/wakeup address, no value.
58 */
59struct socket {
60	int	so_count;		/* (b) reference count */
61	short	so_type;		/* (a) generic type, see socket.h */
62	short	so_options;		/* from socket call, see socket.h */
63	short	so_linger;		/* time to linger while closing */
64	short	so_state;		/* internal state flags SS_* */
65	int	so_qstate;		/* (e) internal state flags SQ_* */
66	void	*so_pcb;		/* protocol control block */
67	struct	protosw *so_proto;	/* (a) protocol handle */
68/*
69 * Variables for connection queuing.
70 * Socket where accepts occur is so_head in all subsidiary sockets.
71 * If so_head is 0, socket is not related to an accept.
72 * For head socket so_incomp queues partially completed connections,
73 * while so_comp is a queue of connections ready to be accepted.
74 * If a connection is aborted and it has so_head set, then
75 * it has to be pulled out of either so_incomp or so_comp.
76 * We allow connections to queue up based on current queue lengths
77 * and limit on number of queued connections for this socket.
78 */
79	struct	socket *so_head;	/* (e) back pointer to accept socket */
80	TAILQ_HEAD(, socket) so_incomp;	/* (e) queue of partial unaccepted connections */
81	TAILQ_HEAD(, socket) so_comp;	/* (e) queue of complete unaccepted connections */
82	TAILQ_ENTRY(socket) so_list;	/* (e) list of unaccepted connections */
83	short	so_qlen;		/* (e) number of unaccepted connections */
84	short	so_incqlen;		/* (e) number of unaccepted incomplete
85					   connections */
86	short	so_qlimit;		/* (e) max number queued connections */
87	short	so_timeo;		/* connection timeout */
88	u_short	so_error;		/* error affecting connection */
89	struct	sigio *so_sigio;	/* [sg] information for async I/O or
90					   out of band data (SIGURG) */
91	u_long	so_oobmark;		/* chars to oob mark */
92	TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
93/*
94 * Variables for socket buffering.
95 */
96	struct sockbuf {
97		struct	selinfo sb_sel;	/* process selecting read/write */
98		struct	mtx sb_mtx;	/* sockbuf lock */
99#define	sb_startzero	sb_mb
100		struct	mbuf *sb_mb;	/* the mbuf chain */
101		struct	mbuf *sb_mbtail; /* the last mbuf in the chain */
102		struct	mbuf *sb_lastrecord;	/* first mbuf of last record in
103						 * socket buffer */
104		u_int	sb_cc;		/* actual chars in buffer */
105		u_int	sb_hiwat;	/* max actual char count */
106		u_int	sb_mbcnt;	/* chars of mbufs used */
107		u_int	sb_mbmax;	/* max chars of mbufs to use */
108		u_int	sb_ctl;		/* non-data chars in buffer */
109		int	sb_lowat;	/* low water mark */
110		int	sb_timeo;	/* timeout for read/write */
111		short	sb_flags;	/* flags, see below */
112		short	sb_state;	/* (c/d) socket state on sockbuf */
113	} so_rcv, so_snd;
114#define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
115#define	SB_LOCK		0x01		/* lock on data queue */
116#define	SB_WANT		0x02		/* someone is waiting to lock */
117#define	SB_WAIT		0x04		/* someone is waiting for data/space */
118#define	SB_SEL		0x08		/* someone is selecting */
119#define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
120#define	SB_UPCALL	0x20		/* someone wants an upcall */
121#define	SB_NOINTR	0x40		/* operations not interruptible */
122#define SB_AIO		0x80		/* AIO operations queued */
123#define SB_KNOTE	0x100		/* kernel note attached */
124
125	void	(*so_upcall)(struct socket *, void *, int);
126	void	*so_upcallarg;
127	struct	ucred *so_cred;		/* user credentials */
128	struct	label *so_label;	/* (b) MAC label for socket */
129	struct	label *so_peerlabel;	/* (b) cached MAC label for peer */
130	/* NB: generation count must not be first; easiest to make it last. */
131	so_gen_t so_gencnt;		/* generation count */
132	void	*so_emuldata;		/* private data for emulators */
133 	struct so_accf {
134		struct	accept_filter *so_accept_filter;
135		void	*so_accept_filter_arg;	/* saved filter args */
136		char	*so_accept_filter_str;	/* saved user args */
137	} *so_accf;
138};
139
140#define SB_EMPTY_FIXUP(sb) do {						\
141	if ((sb)->sb_mb == NULL) {					\
142		(sb)->sb_mbtail = NULL;					\
143		(sb)->sb_lastrecord = NULL;				\
144	}								\
145} while (/*CONSTCOND*/0)
146
147/*
148 * Global accept mutex to serialize access to accept queues and
149 * fields associated with multiple sockets.  This allows us to
150 * avoid defining a lock order between listen and accept sockets
151 * until such time as it proves to be a good idea.
152 */
153extern struct mtx accept_mtx;
154#define	ACCEPT_LOCK()			mtx_lock(&accept_mtx)
155#define	ACCEPT_UNLOCK()			mtx_unlock(&accept_mtx)
156
157/*
158 * Per-socket buffer mutex used to protect most fields in the socket
159 * buffer.
160 */
161#define	SOCKBUF_MTX(_sb)		(&(_sb)->sb_mtx)
162#define	SOCKBUF_LOCK_INIT(_sb, _name) \
163	mtx_init(SOCKBUF_MTX(_sb), _name, NULL, MTX_DEF)
164#define	SOCKBUF_LOCK_DESTROY(_sb)	mtx_destroy(SOCKBUF_MTX(_sb))
165#define	SOCKBUF_LOCK(_sb)		mtx_lock(SOCKBUF_MTX(_sb))
166#define	SOCKBUF_OWNED(_sb)		mtx_owned(SOCKBUF_MTX(_sb))
167#define	SOCKBUF_UNLOCK(_sb)		mtx_unlock(SOCKBUF_MTX(_sb))
168#define	SOCKBUF_LOCK_ASSERT(_sb)	mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED)
169
170/*
171 * Per-socket mutex: we reuse the receive socket buffer mutex for space
172 * efficiency.  This decision should probably be revisited as we optimize
173 * locking for the socket code.
174 */
175#define	SOCK_MTX(_so)			SOCKBUF_MTX(&(_so)->so_rcv)
176#define	SOCK_LOCK(_so)			SOCKBUF_LOCK(&(_so)->so_rcv)
177#define	SOCK_OWNED(_so)			SOCKBUF_OWNED(&(_so)->so_rcv)
178#define	SOCK_UNLOCK(_so)		SOCKBUF_UNLOCK(&(_so)->so_rcv)
179#define	SOCK_LOCK_ASSERT(_so)		SOCKBUF_LOCK_ASSERT(&(_so)->so_rcv)
180
181/*
182 * Socket state bits.
183 *
184 * Historically, this bits were all kept in the so_state field.  For
185 * locking reasons, they are now in multiple fields, as they are
186 * locked differently.  so_state maintains basic socket state protected
187 * by the socket lock.  so_qstate holds information about the socket
188 * accept queues.  Each socket buffer also has a state field holding
189 * information relevant to that socket buffer (can't send, rcv).  Many
190 * fields will be read without locks to improve performance and avoid
191 * lock order issues.  However, this approach must be used with caution.
192 */
193#define	SS_NOFDREF		0x0001	/* no file table ref any more */
194#define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
195#define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
196#define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
197#define	SS_NBIO			0x0100	/* non-blocking ops */
198#define	SS_ASYNC		0x0200	/* async i/o notify */
199#define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
200#define	SS_ISDISCONNECTED	0x2000	/* socket disconnected from peer */
201
202/*
203 * Socket state bits now stored in the socket buffer state field.
204 */
205#define	SBS_CANTSENDMORE	0x0010	/* can't send more data to peer */
206#define	SBS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
207#define	SBS_RCVATMARK		0x0040	/* at mark on input */
208
209/*
210 * Socket state bits stored in so_qstate.
211 */
212#define	SQ_INCOMP		0x0800	/* unaccepted, incomplete connection */
213#define	SQ_COMP			0x1000	/* unaccepted, complete connection */
214
215/*
216 * Externalized form of struct socket used by the sysctl(3) interface.
217 */
218struct xsocket {
219	size_t	xso_len;	/* length of this structure */
220	struct	socket *xso_so;	/* makes a convenient handle sometimes */
221	short	so_type;
222	short	so_options;
223	short	so_linger;
224	short	so_state;
225	caddr_t	so_pcb;		/* another convenient handle */
226	int	xso_protocol;
227	int	xso_family;
228	short	so_qlen;
229	short	so_incqlen;
230	short	so_qlimit;
231	short	so_timeo;
232	u_short	so_error;
233	pid_t	so_pgid;
234	u_long	so_oobmark;
235	struct xsockbuf {
236		u_int	sb_cc;
237		u_int	sb_hiwat;
238		u_int	sb_mbcnt;
239		u_int	sb_mbmax;
240		int	sb_lowat;
241		int	sb_timeo;
242		short	sb_flags;
243	} so_rcv, so_snd;
244	uid_t	so_uid;		/* XXX */
245};
246
247#ifdef _KERNEL
248
249/*
250 * Macros for sockets and socket buffering.
251 */
252
253/*
254 * Do we need to notify the other side when I/O is possible?
255 */
256#define	sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
257    SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
258
259/*
260 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
261 * This is problematical if the fields are unsigned, as the space might
262 * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
263 * overflow and return 0.  Should use "lmin" but it doesn't exist now.
264 */
265#define	sbspace(sb) \
266    ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
267	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
268
269/* do we have to send all at once on a socket? */
270#define	sosendallatonce(so) \
271    ((so)->so_proto->pr_flags & PR_ATOMIC)
272
273/* can we read something from so? */
274#define	soreadable(so) \
275    ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
276	((so)->so_rcv.sb_state & SBS_CANTRCVMORE) || \
277	!TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
278
279/* can we write something to so? */
280#define	sowriteable(so) \
281    ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
282	(((so)->so_state&SS_ISCONNECTED) || \
283	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
284     ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
285     (so)->so_error)
286
287/* adjust counters in sb reflecting allocation of m */
288#define	sballoc(sb, m) { \
289	(sb)->sb_cc += (m)->m_len; \
290	if ((m)->m_type != MT_DATA && (m)->m_type != MT_HEADER && \
291	    (m)->m_type != MT_OOBDATA) \
292		(sb)->sb_ctl += (m)->m_len; \
293	(sb)->sb_mbcnt += MSIZE; \
294	if ((m)->m_flags & M_EXT) \
295		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
296}
297
298/* adjust counters in sb reflecting freeing of m */
299#define	sbfree(sb, m) { \
300	(sb)->sb_cc -= (m)->m_len; \
301	if ((m)->m_type != MT_DATA && (m)->m_type != MT_HEADER && \
302	    (m)->m_type != MT_OOBDATA) \
303		(sb)->sb_ctl -= (m)->m_len; \
304	(sb)->sb_mbcnt -= MSIZE; \
305	if ((m)->m_flags & M_EXT) \
306		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
307}
308
309/*
310 * Set lock on sockbuf sb; sleep if lock is already held.
311 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
312 * Returns error without lock if sleep is interrupted.
313 */
314#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
315		(((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
316		((sb)->sb_flags |= SB_LOCK), 0)
317
318/* release lock on sockbuf sb */
319#define	sbunlock(sb) { \
320	(sb)->sb_flags &= ~SB_LOCK; \
321	if ((sb)->sb_flags & SB_WANT) { \
322		(sb)->sb_flags &= ~SB_WANT; \
323		wakeup(&(sb)->sb_flags); \
324	} \
325}
326
327/*
328 * soref()/sorele() ref-count the socket structure.  Note that you must
329 * still explicitly close the socket, but the last ref count will free
330 * the structure.
331 */
332#define	soref(so) do {							\
333	SOCK_LOCK_ASSERT(so);						\
334	++(so)->so_count;						\
335} while (0)
336
337#define	sorele(so) do {							\
338	SOCK_LOCK_ASSERT(so);						\
339	if ((so)->so_count <= 0)					\
340		panic("sorele");					\
341	if (--(so)->so_count == 0)					\
342		sofree(so);						\
343	else								\
344		SOCK_UNLOCK(so);					\
345} while (0)
346
347#define	sotryfree(so) do {						\
348	SOCK_LOCK_ASSERT(so);						\
349	if ((so)->so_count == 0)					\
350		sofree(so);						\
351	else								\
352		SOCK_UNLOCK(so);					\
353} while(0)
354
355#define	sorwakeup(so) do {						\
356	if (sb_notify(&(so)->so_rcv))					\
357		sowakeup((so), &(so)->so_rcv); 				\
358} while (0)
359
360#define	sowwakeup(so) do {						\
361	if (sb_notify(&(so)->so_snd))					\
362		sowakeup((so), &(so)->so_snd); 				\
363} while (0)
364
365/*
366 * Argument structure for sosetopt et seq.  This is in the KERNEL
367 * section because it will never be visible to user code.
368 */
369enum sopt_dir { SOPT_GET, SOPT_SET };
370struct sockopt {
371	enum	sopt_dir sopt_dir; /* is this a get or a set? */
372	int	sopt_level;	/* second arg of [gs]etsockopt */
373	int	sopt_name;	/* third arg of [gs]etsockopt */
374	void   *sopt_val;	/* fourth arg of [gs]etsockopt */
375	size_t	sopt_valsize;	/* (almost) fifth arg of [gs]etsockopt */
376	struct	thread *sopt_td; /* calling thread or null if kernel */
377};
378
379struct accept_filter {
380	char	accf_name[16];
381	void	(*accf_callback)
382		(struct socket *so, void *arg, int waitflag);
383	void *	(*accf_create)
384		(struct socket *so, char *arg);
385	void	(*accf_destroy)
386		(struct socket *so);
387	SLIST_ENTRY(accept_filter) accf_next;
388};
389
390#ifdef MALLOC_DECLARE
391MALLOC_DECLARE(M_ACCF);
392MALLOC_DECLARE(M_PCB);
393MALLOC_DECLARE(M_SONAME);
394#endif
395
396extern int	maxsockets;
397extern u_long	sb_max;
398extern struct uma_zone *socket_zone;
399extern so_gen_t so_gencnt;
400
401struct mbuf;
402struct sockaddr;
403struct ucred;
404struct uio;
405
406/*
407 * From uipc_socket and friends
408 */
409int	sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type);
410int	getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len);
411void	sbappend(struct sockbuf *sb, struct mbuf *m);
412void	sbappendstream(struct sockbuf *sb, struct mbuf *m);
413int	sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
414	    struct mbuf *m0, struct mbuf *control);
415int	sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
416	    struct mbuf *control);
417void	sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
418void	sbcheck(struct sockbuf *sb);
419void	sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
420struct mbuf *
421	sbcreatecontrol(caddr_t p, int size, int type, int level);
422void	sbdrop(struct sockbuf *sb, int len);
423void	sbdroprecord(struct sockbuf *sb);
424void	sbflush(struct sockbuf *sb);
425void	sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
426void	sbrelease(struct sockbuf *sb, struct socket *so);
427int	sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
428	    struct thread *td);
429void	sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
430int	sbwait(struct sockbuf *sb);
431int	sb_lock(struct sockbuf *sb);
432int	soabort(struct socket *so);
433int	soaccept(struct socket *so, struct sockaddr **nam);
434struct	socket *soalloc(int mflags);
435int	socheckuid(struct socket *so, uid_t uid);
436int	sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
437void	socantrcvmore(struct socket *so);
438void	socantsendmore(struct socket *so);
439int	soclose(struct socket *so);
440int	soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
441int	soconnect2(struct socket *so1, struct socket *so2);
442int	socow_setup(struct mbuf *m0, struct uio *uio);
443int	socreate(int dom, struct socket **aso, int type, int proto,
444	    struct ucred *cred, struct thread *td);
445void	sodealloc(struct socket *so);
446int	sodisconnect(struct socket *so);
447struct	sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags);
448void	sofree(struct socket *so);
449int	sogetopt(struct socket *so, struct sockopt *sopt);
450void	sohasoutofband(struct socket *so);
451void	soisconnected(struct socket *so);
452void	soisconnecting(struct socket *so);
453void	soisdisconnected(struct socket *so);
454void	soisdisconnecting(struct socket *so);
455int	solisten(struct socket *so, int backlog, struct thread *td);
456struct socket *
457	sonewconn(struct socket *head, int connstatus);
458int	sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
459int	sooptcopyout(struct sockopt *sopt, const void *buf, size_t len);
460
461/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
462int	soopt_getm(struct sockopt *sopt, struct mbuf **mp);
463int	soopt_mcopyin(struct sockopt *sopt, struct mbuf *m);
464int	soopt_mcopyout(struct sockopt *sopt, struct mbuf *m);
465
466int	sopoll(struct socket *so, int events, struct ucred *active_cred,
467	    struct thread *td);
468int	soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
469	    struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
470int	soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
471void	sorflush(struct socket *so);
472int	sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
473	    struct mbuf *top, struct mbuf *control, int flags,
474	    struct thread *td);
475int	sosetopt(struct socket *so, struct sockopt *sopt);
476int	soshutdown(struct socket *so, int how);
477void	sotoxsocket(struct socket *so, struct xsocket *xso);
478void	sowakeup(struct socket *so, struct sockbuf *sb);
479
480#ifdef SOCKBUF_DEBUG
481void	sblastrecordchk(struct sockbuf *, const char *, int);
482#define	SBLASTRECORDCHK(sb)	sblastrecordchk((sb), __FILE__, __LINE__)
483
484void	sblastmbufchk(struct sockbuf *, const char *, int);
485#define	SBLASTMBUFCHK(sb)	sblastmbufchk((sb), __FILE__, __LINE__)
486#else
487#define	SBLASTRECORDCHK(sb)      /* nothing */
488#define	SBLASTMBUFCHK(sb)        /* nothing */
489#endif /* SOCKBUF_DEBUG */
490
491/*
492 * Accept filter functions (duh).
493 */
494int	accept_filt_add(struct accept_filter *filt);
495int	accept_filt_del(char *name);
496struct	accept_filter *accept_filt_get(char *name);
497#ifdef ACCEPT_FILTER_MOD
498#ifdef SYSCTL_DECL
499SYSCTL_DECL(_net_inet_accf);
500#endif
501int	accept_filt_generic_mod_event(module_t mod, int event, void *data);
502#endif
503
504#endif /* _KERNEL */
505
506#endif /* !_SYS_SOCKETVAR_H_ */
507