socketvar.h revision 95883
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 95883 2002-05-01 20:44:46Z 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/_lock.h>
42#include <sys/_mutex.h>
43#include <sys/selinfo.h>		/* for struct selinfo */
44#include <vm/uma.h>
45
46/*
47 * Kernel structure per socket.
48 * Contains send and receive buffer queues,
49 * handle on protocol and pointer to protocol
50 * private data and error information.
51 */
52typedef	u_quad_t so_gen_t;
53
54struct accept_filter;
55
56/*
57 * List of locks:
58 * (c)	const, inited in either socreate() or sonewconn()
59 * (m)	sb_mtx mutex
60 * (mr)	so_rcv.sb_mtx mutex
61 * (sg)	sigio_lock sx
62 * (sh)	sohead_lock sx
63 *
64 * Lock of so_rcv.sb_mtx can duplicate, provided that sohead_lock
65 * is exclusively locked.
66 *
67 * Brackets mean that this data is not protected yet.
68 */
69struct socket {
70	int	so_count;		/* reference count */
71	short	so_type;		/* generic type, see socket.h */
72	short	so_options;		/* from socket call, see socket.h */
73	short	so_linger;		/* time to linger while closing */
74	short	so_state;		/* internal state flags SS_*, below */
75	caddr_t	so_pcb;			/* protocol control block */
76	struct	protosw *so_proto;	/* protocol handle */
77/*
78 * Variables for connection queuing.
79 * Socket where accepts occur is so_head in all subsidiary sockets.
80 * If so_head is 0, socket is not related to an accept.
81 * For head socket so_incomp queues partially completed connections,
82 * while so_comp is a queue of connections ready to be accepted.
83 * If a connection is aborted and it has so_head set, then
84 * it has to be pulled out of either so_incomp or so_comp.
85 * We allow connections to queue up based on current queue lengths
86 * and limit on number of queued connections for this socket.
87 */
88	struct	socket *so_head;	/* back pointer to accept socket */
89	TAILQ_HEAD(, socket) so_incomp;	/* queue of partial unaccepted connections */
90	TAILQ_HEAD(, socket) so_comp;	/* queue of complete unaccepted connections */
91	TAILQ_ENTRY(socket) so_list;	/* list of unaccepted connections */
92	short	so_qlen;		/* number of unaccepted connections */
93	short	so_incqlen;		/* number of unaccepted incomplete
94					   connections */
95	short	so_qlimit;		/* max number queued connections */
96	short	so_timeo;		/* connection timeout */
97	u_short	so_error;		/* error affecting connection */
98	struct  sigio *so_sigio;	/* [sg]	information for async I/O or
99					   out of band data (SIGURG) */
100	u_long	so_oobmark;		/* chars to oob mark */
101	TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
102/*
103 * Variables for socket buffering.
104 */
105	struct sockbuf {
106		u_long	sb_cc;		/* actual chars in buffer */
107		u_long	sb_hiwat;	/* max actual char count */
108		u_long	sb_mbcnt;	/* chars of mbufs used */
109		u_long	sb_mbmax;	/* max chars of mbufs to use */
110		long	sb_lowat;	/* low water mark */
111		struct	mbuf *sb_mb;	/* the mbuf chain */
112		struct	selinfo sb_sel;	/* process selecting read/write */
113		short	sb_flags;	/* flags, see below */
114		short	sb_timeo;	/* timeout for read/write */
115	} so_rcv, so_snd;
116#define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
117#define	SB_LOCK		0x01		/* lock on data queue */
118#define	SB_WANT		0x02		/* someone is waiting to lock */
119#define	SB_WAIT		0x04		/* someone is waiting for data/space */
120#define	SB_SEL		0x08		/* someone is selecting */
121#define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
122#define	SB_UPCALL	0x20		/* someone wants an upcall */
123#define	SB_NOINTR	0x40		/* operations not interruptible */
124#define SB_AIO		0x80		/* AIO operations queued */
125#define SB_KNOTE	0x100		/* kernel note attached */
126
127	void	(*so_upcall)(struct socket *, void *, int);
128	void	*so_upcallarg;
129	struct	ucred *so_cred;		/* user credentials */
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/*
141 * Socket state bits.
142 */
143#define	SS_NOFDREF		0x0001	/* no file table ref any more */
144#define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
145#define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
146#define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
147#define	SS_CANTSENDMORE		0x0010	/* can't send more data to peer */
148#define	SS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
149#define	SS_RCVATMARK		0x0040	/* at mark on input */
150
151#define	SS_NBIO			0x0100	/* non-blocking ops */
152#define	SS_ASYNC		0x0200	/* async i/o notify */
153#define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
154
155#define	SS_INCOMP		0x0800	/* unaccepted, incomplete connection */
156#define	SS_COMP			0x1000	/* unaccepted, complete connection */
157#define	SS_ISDISCONNECTED	0x2000	/* socket disconnected from peer */
158
159/*
160 * Externalized form of struct socket used by the sysctl(3) interface.
161 */
162struct xsocket {
163	size_t	xso_len;	/* length of this structure */
164	struct	socket *xso_so;	/* makes a convenient handle sometimes */
165	short	so_type;
166	short	so_options;
167	short	so_linger;
168	short	so_state;
169	caddr_t	so_pcb;		/* another convenient handle */
170	int	xso_protocol;
171	int	xso_family;
172	short	so_qlen;
173	short	so_incqlen;
174	short	so_qlimit;
175	short	so_timeo;
176	u_short	so_error;
177	pid_t	so_pgid;
178	u_long	so_oobmark;
179	struct xsockbuf {
180		u_long	sb_cc;
181		u_long	sb_hiwat;
182		u_long	sb_mbcnt;
183		u_long	sb_mbmax;
184		long	sb_lowat;
185		short	sb_flags;
186		short	sb_timeo;
187	} so_rcv, so_snd;
188	uid_t	so_uid;		/* XXX */
189};
190
191/*
192 * Macros for sockets and socket buffering.
193 */
194
195/*
196 * Do we need to notify the other side when I/O is possible?
197 */
198#define	sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
199    SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
200
201/*
202 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
203 * This is problematical if the fields are unsigned, as the space might
204 * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
205 * overflow and return 0.  Should use "lmin" but it doesn't exist now.
206 */
207#define	sbspace(sb) \
208    ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
209	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
210
211/* do we have to send all at once on a socket? */
212#define	sosendallatonce(so) \
213    ((so)->so_proto->pr_flags & PR_ATOMIC)
214
215/* can we read something from so? */
216#define	soreadable(so) \
217    ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
218	((so)->so_state & SS_CANTRCVMORE) || \
219	!TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
220
221/* can we write something to so? */
222#define	sowriteable(so) \
223    ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
224	(((so)->so_state&SS_ISCONNECTED) || \
225	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
226     ((so)->so_state & SS_CANTSENDMORE) || \
227     (so)->so_error)
228
229/* adjust counters in sb reflecting allocation of m */
230#define	sballoc(sb, m) { \
231	(sb)->sb_cc += (m)->m_len; \
232	(sb)->sb_mbcnt += MSIZE; \
233	if ((m)->m_flags & M_EXT) \
234		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
235}
236
237/* adjust counters in sb reflecting freeing of m */
238#define	sbfree(sb, m) { \
239	(sb)->sb_cc -= (m)->m_len; \
240	(sb)->sb_mbcnt -= MSIZE; \
241	if ((m)->m_flags & M_EXT) \
242		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
243}
244
245/*
246 * Set lock on sockbuf sb; sleep if lock is already held.
247 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
248 * Returns error without lock if sleep is interrupted.
249 */
250#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
251		(((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
252		((sb)->sb_flags |= SB_LOCK), 0)
253
254/* release lock on sockbuf sb */
255#define	sbunlock(sb) { \
256	(sb)->sb_flags &= ~SB_LOCK; \
257	if ((sb)->sb_flags & SB_WANT) { \
258		(sb)->sb_flags &= ~SB_WANT; \
259		wakeup((caddr_t)&(sb)->sb_flags); \
260	} \
261}
262
263/*
264 * soref()/sorele() ref-count the socket structure.  Note that you must
265 * still explicitly close the socket, but the last ref count will free
266 * the structure.
267 */
268
269#define soref(so)	do {			\
270				++(so)->so_count; \
271			} while (0)
272
273#define sorele(so)	do {				\
274				if ((so)->so_count <= 0)	\
275					panic("sorele");\
276				if (--(so)->so_count == 0)\
277					sofree(so);	\
278			} while (0)
279
280#define sotryfree(so)	do {				\
281				if ((so)->so_count == 0)	\
282					sofree(so);	\
283			} while(0)
284
285#define	sorwakeup_locked(so)	do {						\
286					if (sb_notify(&(so)->so_rcv))		\
287						sowakeup((so), &(so)->so_rcv);	\
288				} while (0)
289
290#define	sorwakeup(so)		do {						\
291					sorwakeup_locked(so);			\
292				} while (0)
293
294#define	sowwakeup_locked(so)	do {						\
295					if (sb_notify(&(so)->so_snd))		\
296						sowakeup((so), &(so)->so_snd);	\
297				} while (0)
298
299#define	sowwakeup(so)		do {						\
300					sowwakeup_locked(so);			\
301				} while (0)
302
303#ifdef _KERNEL
304
305/*
306 * Argument structure for sosetopt et seq.  This is in the KERNEL
307 * section because it will never be visible to user code.
308 */
309enum sopt_dir { SOPT_GET, SOPT_SET };
310struct sockopt {
311	enum	sopt_dir sopt_dir; /* is this a get or a set? */
312	int	sopt_level;	/* second arg of [gs]etsockopt */
313	int	sopt_name;	/* third arg of [gs]etsockopt */
314	void   *sopt_val;	/* fourth arg of [gs]etsockopt */
315	size_t	sopt_valsize;	/* (almost) fifth arg of [gs]etsockopt */
316	struct	thread *sopt_td;	/* calling thread or null if kernel */
317};
318
319struct sf_buf {
320	SLIST_ENTRY(sf_buf) free_list;	/* list of free buffer slots */
321	struct		vm_page *m;	/* currently mapped page */
322	vm_offset_t	kva;		/* va of mapping */
323};
324
325struct accept_filter {
326	char	accf_name[16];
327	void	(*accf_callback)
328		(struct socket *so, void *arg, int waitflag);
329	void *	(*accf_create)
330		(struct socket *so, char *arg);
331	void	(*accf_destroy)
332		(struct socket *so);
333	SLIST_ENTRY(accept_filter) accf_next;	/* next on the list */
334};
335
336#ifdef MALLOC_DECLARE
337MALLOC_DECLARE(M_PCB);
338MALLOC_DECLARE(M_SONAME);
339MALLOC_DECLARE(M_ACCF);
340#endif
341
342extern int    maxsockets;
343extern u_long	sb_max;
344extern uma_zone_t	socket_zone;
345extern so_gen_t so_gencnt;
346
347struct file;
348struct filedesc;
349struct mbuf;
350struct sockaddr;
351struct stat;
352struct ucred;
353struct uio;
354struct knote;
355
356/*
357 * File operations on sockets.
358 */
359int	soo_read(struct file *fp, struct uio *uio, struct ucred *cred,
360	    int flags, struct thread *td);
361int	soo_write(struct file *fp, struct uio *uio, struct ucred *cred,
362	    int flags, struct thread *td);
363int	soo_close(struct file *fp, struct thread *td);
364int	soo_ioctl(struct file *fp, u_long cmd, caddr_t data,
365	    struct thread *td);
366int	soo_poll(struct file *fp, int events, struct ucred *cred,
367	    struct thread *td);
368int	soo_stat(struct file *fp, struct stat *ub, struct thread *td);
369int	sokqfilter(struct file *fp, struct knote *kn);
370
371/*
372 * From uipc_socket and friends
373 */
374struct	sockaddr *dup_sockaddr(struct sockaddr *sa, int canwait);
375int	sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type);
376int	getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len);
377void	sbappend(struct sockbuf *sb, struct mbuf *m);
378int	sbappendaddr(struct sockbuf *sb, struct sockaddr *asa,
379	    struct mbuf *m0, struct mbuf *control);
380int	sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
381	    struct mbuf *control);
382void	sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
383void	sbcheck(struct sockbuf *sb);
384void	sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
385struct mbuf *
386	sbcreatecontrol(caddr_t p, int size, int type, int level);
387void	sbdrop(struct sockbuf *sb, int len);
388void	sbdroprecord(struct sockbuf *sb);
389void	sbflush(struct sockbuf *sb);
390void	sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
391void	sbrelease(struct sockbuf *sb, struct socket *so);
392int	sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
393	    struct thread *td);
394void	sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
395int	sbwait(struct sockbuf *sb);
396int	sb_lock(struct sockbuf *sb);
397int	soabort(struct socket *so);
398int	soaccept(struct socket *so, struct sockaddr **nam);
399struct	socket *soalloc(int waitok);
400int	sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
401void	socantrcvmore(struct socket *so);
402void	socantsendmore(struct socket *so);
403int	soclose(struct socket *so);
404int	soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
405int	soconnect2(struct socket *so1, struct socket *so2);
406int	socreate(int dom, struct socket **aso, int type, int proto,
407	    struct ucred *cred, struct thread *td);
408int	sodisconnect(struct socket *so);
409void	sofree(struct socket *so);
410int	sogetopt(struct socket *so, struct sockopt *sopt);
411void	sohasoutofband(struct socket *so);
412void	soisconnected(struct socket *so);
413void	soisconnected_locked(struct socket *so);
414void	soisconnecting(struct socket *so);
415void	soisdisconnected(struct socket *so);
416void	soisdisconnected_locked(struct socket *so);
417void	soisdisconnecting(struct socket *so);
418int	solisten(struct socket *so, int backlog, struct thread *td);
419struct socket *
420	sonewconn(struct socket *head, int connstatus);
421int	sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
422int	sooptcopyout(struct sockopt *sopt, void *buf, size_t len);
423
424/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
425int	soopt_getm(struct sockopt *sopt, struct mbuf **mp);
426int	soopt_mcopyin(struct sockopt *sopt, struct mbuf *m);
427int	soopt_mcopyout(struct sockopt *sopt, struct mbuf *m);
428
429int	sopoll(struct socket *so, int events, struct ucred *cred,
430	    struct thread *td);
431int	soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
432	    struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
433int	soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
434void	sorflush(struct socket *so);
435int	sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
436	    struct mbuf *top, struct mbuf *control, int flags,
437	    struct thread *td);
438int	sosetopt(struct socket *so, struct sockopt *sopt);
439int	soshutdown(struct socket *so, int how);
440void	sotoxsocket(struct socket *so, struct xsocket *xso);
441void	sowakeup(struct socket *so, struct sockbuf *sb);
442
443/* accept filter functions */
444int	accept_filt_add(struct accept_filter *filt);
445int	accept_filt_del(char *name);
446struct accept_filter *	accept_filt_get(char *name);
447#ifdef ACCEPT_FILTER_MOD
448int accept_filt_generic_mod_event(module_t mod, int event, void *data);
449SYSCTL_DECL(_net_inet_accf);
450#endif /* ACCEPT_FILTER_MOD */
451
452int	socheckuid(struct socket *so, uid_t uid);
453int	socheckproc(struct socket *so, struct proc *p);
454
455#endif /* _KERNEL */
456
457#endif /* !_SYS_SOCKETVAR_H_ */
458