socketvar.h revision 65534
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 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)socketvar.h	8.3 (Berkeley) 2/19/95
34 * $FreeBSD: head/sys/sys/socketvar.h 65534 2000-09-06 18:49:13Z alfred $
35 */
36
37#ifndef _SYS_SOCKETVAR_H_
38#define _SYS_SOCKETVAR_H_
39
40#include <sys/queue.h>			/* for TAILQ macros */
41#include <sys/select.h>			/* for struct selinfo */
42
43/*
44 * Kernel structure per socket.
45 * Contains send and receive buffer queues,
46 * handle on protocol and pointer to protocol
47 * private data and error information.
48 */
49typedef	u_quad_t so_gen_t;
50
51struct accept_filter;
52
53struct socket {
54	struct	vm_zone *so_zone;	/* zone we were allocated from */
55	short	so_type;		/* generic type, see socket.h */
56	short	so_options;		/* from socket call, see socket.h */
57	short	so_linger;		/* time to linger while closing */
58	short	so_state;		/* internal state flags SS_*, below */
59	caddr_t	so_pcb;			/* protocol control block */
60	struct	protosw *so_proto;	/* protocol handle */
61/*
62 * Variables for connection queuing.
63 * Socket where accepts occur is so_head in all subsidiary sockets.
64 * If so_head is 0, socket is not related to an accept.
65 * For head socket so_q0 queues partially completed connections,
66 * while so_q is a queue of connections ready to be accepted.
67 * If a connection is aborted and it has so_head set, then
68 * it has to be pulled out of either so_q0 or so_q.
69 * We allow connections to queue up based on current queue lengths
70 * and limit on number of queued connections for this socket.
71 */
72	struct	socket *so_head;	/* back pointer to accept socket */
73	TAILQ_HEAD(, socket) so_incomp;	/* queue of partial unaccepted connections */
74	TAILQ_HEAD(, socket) so_comp;	/* queue of complete unaccepted connections */
75	TAILQ_ENTRY(socket) so_list;	/* list of unaccepted connections */
76	short	so_qlen;		/* number of unaccepted connections */
77	short	so_incqlen;		/* number of unaccepted incomplete
78					   connections */
79	short	so_qlimit;		/* max number queued connections */
80	short	so_timeo;		/* connection timeout */
81	u_short	so_error;		/* error affecting connection */
82	struct  sigio *so_sigio;	/* information for async I/O or
83					   out of band data (SIGURG) */
84	u_long	so_oobmark;		/* chars to oob mark */
85	TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
86/*
87 * Variables for socket buffering.
88 */
89	struct	sockbuf {
90		u_long	sb_cc;		/* actual chars in buffer */
91		u_long	sb_hiwat;	/* max actual char count */
92		u_long	sb_mbcnt;	/* chars of mbufs used */
93		u_long	sb_mbmax;	/* max chars of mbufs to use */
94		long	sb_lowat;	/* low water mark */
95		struct	mbuf *sb_mb;	/* the mbuf chain */
96		struct	selinfo sb_sel;	/* process selecting read/write */
97		short	sb_flags;	/* flags, see below */
98		short	sb_timeo;	/* timeout for read/write */
99	} so_rcv, so_snd;
100#define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
101#define	SB_LOCK		0x01		/* lock on data queue */
102#define	SB_WANT		0x02		/* someone is waiting to lock */
103#define	SB_WAIT		0x04		/* someone is waiting for data/space */
104#define	SB_SEL		0x08		/* someone is selecting */
105#define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
106#define	SB_UPCALL	0x20		/* someone wants an upcall */
107#define	SB_NOINTR	0x40		/* operations not interruptible */
108#define SB_AIO		0x80		/* AIO operations queued */
109#define SB_KNOTE	0x100		/* kernel note attached */
110
111	void	(*so_upcall) __P((struct socket *, void *, int));
112	void	*so_upcallarg;
113	struct	ucred *so_cred;		/* user credentials */
114	/* NB: generation count must not be first; easiest to make it last. */
115	so_gen_t so_gencnt;		/* generation count */
116	void	*so_emuldata;		/* private data for emulators */
117	struct	so_accf {
118		struct	accept_filter *so_accept_filter;
119		void	*so_accept_filter_arg;	/* saved filter args */
120		char	*so_accept_filter_str;	/* saved user args */
121	} *so_accf;
122};
123
124/*
125 * Socket state bits.
126 */
127#define	SS_NOFDREF		0x0001	/* no file table ref any more */
128#define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
129#define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
130#define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
131#define	SS_CANTSENDMORE		0x0010	/* can't send more data to peer */
132#define	SS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
133#define	SS_RCVATMARK		0x0040	/* at mark on input */
134
135#define	SS_NBIO			0x0100	/* non-blocking ops */
136#define	SS_ASYNC		0x0200	/* async i/o notify */
137#define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
138
139#define	SS_INCOMP		0x0800	/* unaccepted, incomplete connection */
140#define	SS_COMP			0x1000	/* unaccepted, complete connection */
141#define	SS_ISDISCONNECTED	0x2000	/* socket disconnected from peer */
142
143/*
144 * Externalized form of struct socket used by the sysctl(3) interface.
145 */
146struct	xsocket {
147	size_t	xso_len;	/* length of this structure */
148	struct	socket *xso_so;	/* makes a convenient handle sometimes */
149	short	so_type;
150	short	so_options;
151	short	so_linger;
152	short	so_state;
153	caddr_t	so_pcb;		/* another convenient handle */
154	int	xso_protocol;
155	int	xso_family;
156	short	so_qlen;
157	short	so_incqlen;
158	short	so_qlimit;
159	short	so_timeo;
160	u_short	so_error;
161	pid_t	so_pgid;
162	u_long	so_oobmark;
163	struct	xsockbuf {
164		u_long	sb_cc;
165		u_long	sb_hiwat;
166		u_long	sb_mbcnt;
167		u_long	sb_mbmax;
168		long	sb_lowat;
169		short	sb_flags;
170		short	sb_timeo;
171	} so_rcv, so_snd;
172	uid_t	so_uid;		/* XXX */
173};
174
175/*
176 * Macros for sockets and socket buffering.
177 */
178
179/*
180 * Do we need to notify the other side when I/O is possible?
181 */
182#define	sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
183    SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
184
185/*
186 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
187 * This is problematical if the fields are unsigned, as the space might
188 * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
189 * overflow and return 0.  Should use "lmin" but it doesn't exist now.
190 */
191#define	sbspace(sb) \
192    ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
193	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
194
195/* do we have to send all at once on a socket? */
196#define	sosendallatonce(so) \
197    ((so)->so_proto->pr_flags & PR_ATOMIC)
198
199/* can we read something from so? */
200#define	soreadable(so) \
201    ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
202	((so)->so_state & SS_CANTRCVMORE) || \
203	(so)->so_comp.tqh_first || (so)->so_error)
204
205/* can we write something to so? */
206#define	sowriteable(so) \
207    ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
208	(((so)->so_state&SS_ISCONNECTED) || \
209	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
210     ((so)->so_state & SS_CANTSENDMORE) || \
211     (so)->so_error)
212
213/* adjust counters in sb reflecting allocation of m */
214#define	sballoc(sb, m) { \
215	(sb)->sb_cc += (m)->m_len; \
216	(sb)->sb_mbcnt += MSIZE; \
217	if ((m)->m_flags & M_EXT) \
218		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
219}
220
221/* adjust counters in sb reflecting freeing of m */
222#define	sbfree(sb, m) { \
223	(sb)->sb_cc -= (m)->m_len; \
224	(sb)->sb_mbcnt -= MSIZE; \
225	if ((m)->m_flags & M_EXT) \
226		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
227}
228
229/*
230 * Set lock on sockbuf sb; sleep if lock is already held.
231 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
232 * Returns error without lock if sleep is interrupted.
233 */
234#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
235		(((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
236		((sb)->sb_flags |= SB_LOCK), 0)
237
238/* release lock on sockbuf sb */
239#define	sbunlock(sb) { \
240	(sb)->sb_flags &= ~SB_LOCK; \
241	if ((sb)->sb_flags & SB_WANT) { \
242		(sb)->sb_flags &= ~SB_WANT; \
243		wakeup((caddr_t)&(sb)->sb_flags); \
244	} \
245}
246
247#define	sorwakeup(so)	do { \
248			  if (sb_notify(&(so)->so_rcv)) \
249			    sowakeup((so), &(so)->so_rcv); \
250			} while (0)
251
252#define	sowwakeup(so)	do { \
253			  if (sb_notify(&(so)->so_snd)) \
254			    sowakeup((so), &(so)->so_snd); \
255			} while (0)
256
257#ifdef _KERNEL
258
259/*
260 * Argument structure for sosetopt et seq.  This is in the KERNEL
261 * section because it will never be visible to user code.
262 */
263enum sopt_dir { SOPT_GET, SOPT_SET };
264struct sockopt {
265	enum	sopt_dir sopt_dir; /* is this a get or a set? */
266	int	sopt_level;	/* second arg of [gs]etsockopt */
267	int	sopt_name;	/* third arg of [gs]etsockopt */
268	void   *sopt_val;	/* fourth arg of [gs]etsockopt */
269	size_t	sopt_valsize;	/* (almost) fifth arg of [gs]etsockopt */
270	struct	proc *sopt_p;	/* calling process or null if kernel */
271};
272
273struct sf_buf {
274	SLIST_ENTRY(sf_buf) free_list;	/* list of free buffer slots */
275	struct		vm_page *m;	/* currently mapped page */
276	vm_offset_t	kva;		/* va of mapping */
277};
278
279struct accept_filter {
280	char	accf_name[16];
281	void	(*accf_callback)
282		__P((struct socket *so, void *arg, int waitflag));
283	void *	(*accf_create)
284		__P((struct socket *so, char *arg));
285	void	(*accf_destroy)
286		__P((struct socket *so));
287	SLIST_ENTRY(accept_filter) accf_next;	/* next on the list */
288};
289
290#ifdef MALLOC_DECLARE
291MALLOC_DECLARE(M_PCB);
292MALLOC_DECLARE(M_SONAME);
293MALLOC_DECLARE(M_ACCF);
294#endif
295
296extern int	maxsockets;
297extern u_long	sb_max;
298extern struct	vm_zone *socket_zone;
299extern so_gen_t so_gencnt;
300
301struct file;
302struct filedesc;
303struct mbuf;
304struct sockaddr;
305struct stat;
306struct ucred;
307struct uio;
308
309/*
310 * File operations on sockets.
311 */
312int	soo_read __P((struct file *fp, struct uio *uio, struct ucred *cred,
313	    int flags, struct proc *p));
314int	soo_write __P((struct file *fp, struct uio *uio, struct ucred *cred,
315	    int flags, struct proc *p));
316int	soo_close __P((struct file *fp, struct proc *p));
317int	soo_ioctl __P((struct file *fp, u_long cmd, caddr_t data,
318	    struct proc *p));
319int	soo_poll __P((struct file *fp, int events, struct ucred *cred,
320	    struct proc *p));
321int	soo_stat __P((struct file *fp, struct stat *ub, struct proc *p));
322
323/*
324 * From uipc_socket and friends
325 */
326struct	sockaddr *dup_sockaddr __P((struct sockaddr *sa, int canwait));
327int	getsock __P((struct filedesc *fdp, int fdes, struct file **fpp));
328int	sockargs __P((struct mbuf **mp, caddr_t buf, int buflen, int type));
329int	getsockaddr __P((struct sockaddr **namp, caddr_t uaddr, size_t len));
330void	sbappend __P((struct sockbuf *sb, struct mbuf *m));
331int	sbappendaddr __P((struct sockbuf *sb, struct sockaddr *asa,
332	    struct mbuf *m0, struct mbuf *control));
333int	sbappendcontrol __P((struct sockbuf *sb, struct mbuf *m0,
334	    struct mbuf *control));
335void	sbappendrecord __P((struct sockbuf *sb, struct mbuf *m0));
336void	sbcheck __P((struct sockbuf *sb));
337void	sbcompress __P((struct sockbuf *sb, struct mbuf *m, struct mbuf *n));
338struct mbuf *
339	sbcreatecontrol __P((caddr_t p, int size, int type, int level));
340void	sbdrop __P((struct sockbuf *sb, int len));
341void	sbdroprecord __P((struct sockbuf *sb));
342void	sbflush __P((struct sockbuf *sb));
343void	sbinsertoob __P((struct sockbuf *sb, struct mbuf *m0));
344void	sbrelease __P((struct sockbuf *sb, struct socket *so));
345int	sbreserve __P((struct sockbuf *sb, u_long cc, struct socket *so,
346		       struct proc *p));
347void	sbtoxsockbuf __P((struct sockbuf *sb, struct xsockbuf *xsb));
348int	sbwait __P((struct sockbuf *sb));
349int	sb_lock __P((struct sockbuf *sb));
350int	soabort __P((struct socket *so));
351int	soaccept __P((struct socket *so, struct sockaddr **nam));
352struct	socket *soalloc __P((int waitok));
353int	sobind __P((struct socket *so, struct sockaddr *nam, struct proc *p));
354void	socantrcvmore __P((struct socket *so));
355void	socantsendmore __P((struct socket *so));
356int	soclose __P((struct socket *so));
357int	soconnect __P((struct socket *so, struct sockaddr *nam, struct proc *p));
358int	soconnect2 __P((struct socket *so1, struct socket *so2));
359int	socreate __P((int dom, struct socket **aso, int type, int proto,
360	    struct proc *p));
361void	sodealloc __P((struct socket *so));
362int	sodisconnect __P((struct socket *so));
363void	sofree __P((struct socket *so));
364int	sogetopt __P((struct socket *so, struct sockopt *sopt));
365void	sohasoutofband __P((struct socket *so));
366void	soisconnected __P((struct socket *so));
367void	soisconnecting __P((struct socket *so));
368void	soisdisconnected __P((struct socket *so));
369void	soisdisconnecting __P((struct socket *so));
370int	solisten __P((struct socket *so, int backlog, struct proc *p));
371struct socket *
372	sodropablereq __P((struct socket *head));
373struct socket *
374	sonewconn __P((struct socket *head, int connstatus));
375struct socket *
376	sonewconn3 __P((struct socket *head, int connstatus, struct proc *p));
377int	sooptcopyin __P((struct sockopt *sopt, void *buf, size_t len,
378			 size_t minlen));
379int	sooptcopyout __P((struct sockopt *sopt, void *buf, size_t len));
380
381/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
382int	soopt_getm __P((struct sockopt *sopt, struct mbuf **mp));
383int	soopt_mcopyin __P((struct sockopt *sopt, struct mbuf *m));
384int	soopt_mcopyout __P((struct sockopt *sopt, struct mbuf *m));
385
386int	sopoll __P((struct socket *so, int events, struct ucred *cred,
387		    struct proc *p));
388int	soreceive __P((struct socket *so, struct sockaddr **paddr,
389		       struct uio *uio, struct mbuf **mp0,
390		       struct mbuf **controlp, int *flagsp));
391int	soreserve __P((struct socket *so, u_long sndcc, u_long rcvcc));
392void	sorflush __P((struct socket *so));
393int	sosend __P((struct socket *so, struct sockaddr *addr, struct uio *uio,
394		    struct mbuf *top, struct mbuf *control, int flags,
395		    struct proc *p));
396int	sosetopt __P((struct socket *so, struct sockopt *sopt));
397int	soshutdown __P((struct socket *so, int how));
398void	sotoxsocket __P((struct socket *so, struct xsocket *xso));
399void	sowakeup __P((struct socket *so, struct sockbuf *sb));
400
401/* accept filter functions */
402int	accept_filt_add __P((struct accept_filter *filt));
403int	accept_filt_del __P((char *name));
404struct accept_filter *	accept_filt_get __P((char *name));
405#ifdef ACCEPT_FILTER_MOD
406int accept_filt_generic_mod_event __P((module_t mod, int event, void *data));
407SYSCTL_DECL(_net_inet_accf);
408#endif /* ACCEPT_FILTER_MOD */
409
410#endif /* _KERNEL */
411
412#endif /* !_SYS_SOCKETVAR_H_ */
413