socketvar.h revision 36735
1178476Sjb/*-
2178476Sjb * Copyright (c) 1982, 1986, 1990, 1993
3178476Sjb *	The Regents of the University of California.  All rights reserved.
4178476Sjb *
5178476Sjb * Redistribution and use in source and binary forms, with or without
6178476Sjb * modification, are permitted provided that the following conditions
7178476Sjb * are met:
8178476Sjb * 1. Redistributions of source code must retain the above copyright
9178476Sjb *    notice, this list of conditions and the following disclaimer.
10178476Sjb * 2. Redistributions in binary form must reproduce the above copyright
11178476Sjb *    notice, this list of conditions and the following disclaimer in the
12178476Sjb *    documentation and/or other materials provided with the distribution.
13178476Sjb * 3. All advertising materials mentioning features or use of this software
14178476Sjb *    must display the following acknowledgement:
15178476Sjb *	This product includes software developed by the University of
16178476Sjb *	California, Berkeley and its contributors.
17178476Sjb * 4. Neither the name of the University nor the names of its contributors
18178476Sjb *    may be used to endorse or promote products derived from this software
19178476Sjb *    without specific prior written permission.
20178476Sjb *
21178476Sjb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22178476Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178476Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24178476Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25178476Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26178476Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27178476Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28178476Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29178476Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30178476Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31178476Sjb * SUCH DAMAGE.
32178476Sjb *
33178476Sjb *	@(#)socketvar.h	8.3 (Berkeley) 2/19/95
34178476Sjb *	$Id: socketvar.h,v 1.27 1998/05/31 18:37:22 peter Exp $
35178476Sjb */
36178476Sjb
37178476Sjb#ifndef _SYS_SOCKETVAR_H_
38178476Sjb#define _SYS_SOCKETVAR_H_
39178476Sjb
40178476Sjb#include <sys/queue.h>			/* for TAILQ macros */
41178476Sjb#include <sys/select.h>			/* for struct selinfo */
42178476Sjb
43178476Sjb/*
44178476Sjb * Kernel structure per socket.
45178476Sjb * Contains send and receive buffer queues,
46178476Sjb * handle on protocol and pointer to protocol
47178476Sjb * private data and error information.
48178476Sjb */
49178476Sjbtypedef	u_quad_t so_gen_t;
50178476Sjb
51178476Sjbstruct socket {
52178476Sjb	struct	vm_zone *so_zone;	/* zone we were allocated from */
53178476Sjb	short	so_type;		/* generic type, see socket.h */
54178476Sjb	short	so_options;		/* from socket call, see socket.h */
55	short	so_linger;		/* time to linger while closing */
56	short	so_state;		/* internal state flags SS_*, below */
57	caddr_t	so_pcb;			/* protocol control block */
58	struct	protosw *so_proto;	/* protocol handle */
59/*
60 * Variables for connection queuing.
61 * Socket where accepts occur is so_head in all subsidiary sockets.
62 * If so_head is 0, socket is not related to an accept.
63 * For head socket so_q0 queues partially completed connections,
64 * while so_q is a queue of connections ready to be accepted.
65 * If a connection is aborted and it has so_head set, then
66 * it has to be pulled out of either so_q0 or so_q.
67 * We allow connections to queue up based on current queue lengths
68 * and limit on number of queued connections for this socket.
69 */
70	struct	socket *so_head;	/* back pointer to accept socket */
71	TAILQ_HEAD(, socket) so_incomp;	/* queue of partial unaccepted connections */
72	TAILQ_HEAD(, socket) so_comp;	/* queue of complete unaccepted connections */
73	TAILQ_ENTRY(socket) so_list;	/* list of unaccepted connections */
74	short	so_qlen;		/* number of unaccepted connections */
75	short	so_incqlen;		/* number of unaccepted incomplete
76					   connections */
77	short	so_qlimit;		/* max number queued connections */
78	short	so_timeo;		/* connection timeout */
79	u_short	so_error;		/* error affecting connection */
80	pid_t	so_pgid;		/* pgid for signals */
81	u_long	so_oobmark;		/* chars to oob mark */
82/*
83 * Variables for socket buffering.
84 */
85	struct	sockbuf {
86		u_long	sb_cc;		/* actual chars in buffer */
87		u_long	sb_hiwat;	/* max actual char count */
88		u_long	sb_mbcnt;	/* chars of mbufs used */
89		u_long	sb_mbmax;	/* max chars of mbufs to use */
90		long	sb_lowat;	/* low water mark */
91		struct	mbuf *sb_mb;	/* the mbuf chain */
92		struct	selinfo sb_sel;	/* process selecting read/write */
93		short	sb_flags;	/* flags, see below */
94		short	sb_timeo;	/* timeout for read/write */
95	} so_rcv, so_snd;
96#define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
97#define	SB_LOCK		0x01		/* lock on data queue */
98#define	SB_WANT		0x02		/* someone is waiting to lock */
99#define	SB_WAIT		0x04		/* someone is waiting for data/space */
100#define	SB_SEL		0x08		/* someone is selecting */
101#define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
102#define	SB_UPCALL	0x20		/* someone wants an upcall */
103#define	SB_NOINTR	0x40		/* operations not interruptible */
104
105	void	(*so_upcall) __P((struct socket *so, caddr_t arg, int waitf));
106	caddr_t	so_upcallarg;		/* Arg for above */
107	uid_t	so_uid;			/* who opened the socket */
108	so_gen_t so_gencnt;		/* generation count */
109};
110
111/*
112 * Socket state bits.
113 */
114#define	SS_NOFDREF		0x0001	/* no file table ref any more */
115#define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
116#define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
117#define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
118#define	SS_CANTSENDMORE		0x0010	/* can't send more data to peer */
119#define	SS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
120#define	SS_RCVATMARK		0x0040	/* at mark on input */
121
122/*efine	SS_PRIV			0x0080	   privileged for broadcast, raw... */
123#define	SS_NBIO			0x0100	/* non-blocking ops */
124#define	SS_ASYNC		0x0200	/* async i/o notify */
125#define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
126
127#define	SS_INCOMP		0x0800	/* unaccepted, incomplete connection */
128#define	SS_COMP			0x1000	/* unaccepted, complete connection */
129
130/*
131 * Externalized form of struct socket used by the sysctl(3) interface.
132 */
133struct	xsocket {
134	size_t	xso_len;	/* length of this structure */
135	struct	socket *xso_so;	/* makes a convenient handle sometimes */
136	short	so_type;
137	short	so_options;
138	short	so_linger;
139	short	so_state;
140	caddr_t	so_pcb;		/* another convenient handle */
141	int	xso_protocol;
142	int	xso_family;
143	short	so_qlen;
144	short	so_incqlen;
145	short	so_qlimit;
146	short	so_timeo;
147	u_short	so_error;
148	pid_t	so_pgid;
149	u_long	so_oobmark;
150	struct	xsockbuf {
151		u_long	sb_cc;
152		u_long	sb_hiwat;
153		u_long	sb_mbcnt;
154		u_long	sb_mbmax;
155		long	sb_lowat;
156		short	sb_flags;
157		short	sb_timeo;
158	} so_rcv, so_snd;
159	uid_t	so_uid;		/* XXX */
160};
161
162/*
163 * Macros for sockets and socket buffering.
164 */
165
166/*
167 * Do we need to notify the other side when I/O is possible?
168 */
169#define sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC|SB_UPCALL)) != 0)
170
171/*
172 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
173 * This is problematical if the fields are unsigned, as the space might
174 * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
175 * overflow and return 0.  Should use "lmin" but it doesn't exist now.
176 */
177#define	sbspace(sb) \
178    ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
179	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
180
181/* do we have to send all at once on a socket? */
182#define	sosendallatonce(so) \
183    ((so)->so_proto->pr_flags & PR_ATOMIC)
184
185/* can we read something from so? */
186#define	soreadable(so) \
187    ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
188	((so)->so_state & SS_CANTRCVMORE) || \
189	(so)->so_comp.tqh_first || (so)->so_error)
190
191/* can we write something to so? */
192#define	sowriteable(so) \
193    ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
194	(((so)->so_state&SS_ISCONNECTED) || \
195	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
196     ((so)->so_state & SS_CANTSENDMORE) || \
197     (so)->so_error)
198
199/* adjust counters in sb reflecting allocation of m */
200#define	sballoc(sb, m) { \
201	(sb)->sb_cc += (m)->m_len; \
202	(sb)->sb_mbcnt += MSIZE; \
203	if ((m)->m_flags & M_EXT) \
204		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
205}
206
207/* adjust counters in sb reflecting freeing of m */
208#define	sbfree(sb, m) { \
209	(sb)->sb_cc -= (m)->m_len; \
210	(sb)->sb_mbcnt -= MSIZE; \
211	if ((m)->m_flags & M_EXT) \
212		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
213}
214
215/*
216 * Set lock on sockbuf sb; sleep if lock is already held.
217 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
218 * Returns error without lock if sleep is interrupted.
219 */
220#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
221		(((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
222		((sb)->sb_flags |= SB_LOCK), 0)
223
224/* release lock on sockbuf sb */
225#define	sbunlock(sb) { \
226	(sb)->sb_flags &= ~SB_LOCK; \
227	if ((sb)->sb_flags & SB_WANT) { \
228		(sb)->sb_flags &= ~SB_WANT; \
229		wakeup((caddr_t)&(sb)->sb_flags); \
230	} \
231}
232
233#define	sorwakeup(so)	do { \
234			  if (sb_notify(&(so)->so_rcv)) \
235			    sowakeup((so), &(so)->so_rcv); \
236			} while (0)
237
238#define	sowwakeup(so)	do { \
239			  if (sb_notify(&(so)->so_snd)) \
240			    sowakeup((so), &(so)->so_snd); \
241			} while (0)
242
243#ifdef KERNEL
244
245#ifdef MALLOC_DECLARE
246MALLOC_DECLARE(M_PCB);
247MALLOC_DECLARE(M_SONAME);
248#endif
249
250extern int	maxsockets;
251extern u_long	sb_max;
252extern struct	vm_zone *socket_zone;
253extern so_gen_t so_gencnt;
254
255struct file;
256struct filedesc;
257struct mbuf;
258struct sockaddr;
259struct stat;
260struct ucred;
261struct uio;
262
263/*
264 * File operations on sockets.
265 */
266int	soo_ioctl __P((struct file *fp, u_long cmd, caddr_t data,
267	    struct proc *p));
268int	soo_poll __P((struct file *fp, int events, struct ucred *cred,
269	    struct proc *p));
270int	soo_stat __P((struct socket *so, struct stat *ub));
271
272/*
273 * From uipc_socket and friends
274 */
275struct	sockaddr *dup_sockaddr __P((struct sockaddr *sa, int canwait));
276int	getsock __P((struct filedesc *fdp, int fdes, struct file **fpp));
277int	sockargs __P((struct mbuf **mp, caddr_t buf, int buflen, int type));
278int	getsockaddr __P((struct sockaddr **namp, caddr_t uaddr, size_t len));
279void	sbappend __P((struct sockbuf *sb, struct mbuf *m));
280int	sbappendaddr __P((struct sockbuf *sb, struct sockaddr *asa,
281	    struct mbuf *m0, struct mbuf *control));
282int	sbappendcontrol __P((struct sockbuf *sb, struct mbuf *m0,
283	    struct mbuf *control));
284void	sbappendrecord __P((struct sockbuf *sb, struct mbuf *m0));
285void	sbcheck __P((struct sockbuf *sb));
286void	sbcompress __P((struct sockbuf *sb, struct mbuf *m, struct mbuf *n));
287struct mbuf *
288	sbcreatecontrol __P((caddr_t p, int size, int type, int level));
289void	sbdrop __P((struct sockbuf *sb, int len));
290void	sbdroprecord __P((struct sockbuf *sb));
291void	sbflush __P((struct sockbuf *sb));
292void	sbinsertoob __P((struct sockbuf *sb, struct mbuf *m0));
293void	sbrelease __P((struct sockbuf *sb));
294int	sbreserve __P((struct sockbuf *sb, u_long cc));
295void	sbtoxsockbuf __P((struct sockbuf *sb, struct xsockbuf *xsb));
296int	sbwait __P((struct sockbuf *sb));
297int	sb_lock __P((struct sockbuf *sb));
298int	soabort __P((struct socket *so));
299int	soaccept __P((struct socket *so, struct sockaddr **nam));
300struct	socket *soalloc __P((int waitok));
301int	sobind __P((struct socket *so, struct sockaddr *nam, struct proc *p));
302void	socantrcvmore __P((struct socket *so));
303void	socantsendmore __P((struct socket *so));
304int	soclose __P((struct socket *so));
305int	soconnect __P((struct socket *so, struct sockaddr *nam, struct proc *p));
306int	soconnect2 __P((struct socket *so1, struct socket *so2));
307int	socreate __P((int dom, struct socket **aso, int type, int proto,
308	    struct proc *p));
309void	sodealloc __P((struct socket *so));
310int	sodisconnect __P((struct socket *so));
311void	sofree __P((struct socket *so));
312int	sogetopt __P((struct socket *so, int level, int optname,
313	    struct mbuf **mp, struct proc *p));
314void	sohasoutofband __P((struct socket *so));
315void	soisconnected __P((struct socket *so));
316void	soisconnecting __P((struct socket *so));
317void	soisdisconnected __P((struct socket *so));
318void	soisdisconnecting __P((struct socket *so));
319int	solisten __P((struct socket *so, int backlog, struct proc *p));
320struct socket *
321	sodropablereq __P((struct socket *head));
322struct socket *
323	sonewconn __P((struct socket *head, int connstatus));
324int	sopoll __P((struct socket *so, int events, struct ucred *cred,
325		    struct proc *p));
326int	soreceive __P((struct socket *so, struct sockaddr **paddr,
327		       struct uio *uio, struct mbuf **mp0,
328		       struct mbuf **controlp, int *flagsp));
329int	soreserve __P((struct socket *so, u_long sndcc, u_long rcvcc));
330void	sorflush __P((struct socket *so));
331int	sosend __P((struct socket *so, struct sockaddr *addr, struct uio *uio,
332		    struct mbuf *top, struct mbuf *control, int flags,
333		    struct proc *p));
334int	sosetopt __P((struct socket *so, int level, int optname,
335	    struct mbuf *m0, struct proc *p));
336int	soshutdown __P((struct socket *so, int how));
337void	sotoxsocket __P((struct socket *so, struct xsocket *xso));
338void	sowakeup __P((struct socket *so, struct sockbuf *sb));
339
340#endif /* KERNEL */
341
342#endif /* !_SYS_SOCKETVAR_H_ */
343