socketvar.h revision 43196
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1990, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
3314505Shsu *	@(#)socketvar.h	8.3 (Berkeley) 2/19/95
3443196Sfenner *	$Id: socketvar.h,v 1.32 1998/11/11 10:56:07 truckman Exp $
351541Srgrimes */
361541Srgrimes
372165Spaul#ifndef _SYS_SOCKETVAR_H_
382165Spaul#define _SYS_SOCKETVAR_H_
392165Spaul
4015492Sbde#include <sys/queue.h>			/* for TAILQ macros */
411541Srgrimes#include <sys/select.h>			/* for struct selinfo */
421541Srgrimes
431541Srgrimes/*
441541Srgrimes * Kernel structure per socket.
451541Srgrimes * Contains send and receive buffer queues,
461541Srgrimes * handle on protocol and pointer to protocol
471541Srgrimes * private data and error information.
481541Srgrimes */
4936079Swollmantypedef	u_quad_t so_gen_t;
5036079Swollman
511541Srgrimesstruct socket {
5236079Swollman	struct	vm_zone *so_zone;	/* zone we were allocated from */
531541Srgrimes	short	so_type;		/* generic type, see socket.h */
541541Srgrimes	short	so_options;		/* from socket call, see socket.h */
551541Srgrimes	short	so_linger;		/* time to linger while closing */
561541Srgrimes	short	so_state;		/* internal state flags SS_*, below */
571541Srgrimes	caddr_t	so_pcb;			/* protocol control block */
581541Srgrimes	struct	protosw *so_proto;	/* protocol handle */
591541Srgrimes/*
6013765Smpp * Variables for connection queuing.
611541Srgrimes * Socket where accepts occur is so_head in all subsidiary sockets.
621541Srgrimes * If so_head is 0, socket is not related to an accept.
631541Srgrimes * For head socket so_q0 queues partially completed connections,
641541Srgrimes * while so_q is a queue of connections ready to be accepted.
651541Srgrimes * If a connection is aborted and it has so_head set, then
661541Srgrimes * it has to be pulled out of either so_q0 or so_q.
671541Srgrimes * We allow connections to queue up based on current queue lengths
681541Srgrimes * and limit on number of queued connections for this socket.
691541Srgrimes */
701541Srgrimes	struct	socket *so_head;	/* back pointer to accept socket */
7114547Sdg	TAILQ_HEAD(, socket) so_incomp;	/* queue of partial unaccepted connections */
7214547Sdg	TAILQ_HEAD(, socket) so_comp;	/* queue of complete unaccepted connections */
7314547Sdg	TAILQ_ENTRY(socket) so_list;	/* list of unaccepted connections */
7414547Sdg	short	so_qlen;		/* number of unaccepted connections */
7518787Spst	short	so_incqlen;		/* number of unaccepted incomplete
7618787Spst					   connections */
771541Srgrimes	short	so_qlimit;		/* max number queued connections */
781541Srgrimes	short	so_timeo;		/* connection timeout */
791541Srgrimes	u_short	so_error;		/* error affecting connection */
8041087Struckman	struct  sigio *so_sigio;	/* information for async I/O or
8141087Struckman					   out of band data (SIGURG) */
821541Srgrimes	u_long	so_oobmark;		/* chars to oob mark */
831541Srgrimes/*
841541Srgrimes * Variables for socket buffering.
851541Srgrimes */
861541Srgrimes	struct	sockbuf {
871541Srgrimes		u_long	sb_cc;		/* actual chars in buffer */
881541Srgrimes		u_long	sb_hiwat;	/* max actual char count */
891541Srgrimes		u_long	sb_mbcnt;	/* chars of mbufs used */
901541Srgrimes		u_long	sb_mbmax;	/* max chars of mbufs to use */
911541Srgrimes		long	sb_lowat;	/* low water mark */
921541Srgrimes		struct	mbuf *sb_mb;	/* the mbuf chain */
931541Srgrimes		struct	selinfo sb_sel;	/* process selecting read/write */
941541Srgrimes		short	sb_flags;	/* flags, see below */
951541Srgrimes		short	sb_timeo;	/* timeout for read/write */
961541Srgrimes	} so_rcv, so_snd;
971541Srgrimes#define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
981541Srgrimes#define	SB_LOCK		0x01		/* lock on data queue */
991541Srgrimes#define	SB_WANT		0x02		/* someone is waiting to lock */
1001541Srgrimes#define	SB_WAIT		0x04		/* someone is waiting for data/space */
1011541Srgrimes#define	SB_SEL		0x08		/* someone is selecting */
1021541Srgrimes#define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
10336527Speter#define	SB_UPCALL	0x20		/* someone wants an upcall */
1041541Srgrimes#define	SB_NOINTR	0x40		/* operations not interruptible */
1051541Srgrimes
10638482Swollman	void	(*so_upcall) __P((struct socket *, void *, int));
10738482Swollman	void	*so_upcallarg;
10833955Sguido	uid_t	so_uid;			/* who opened the socket */
10938482Swollman	/* NB: generation count must not be first; easiest to make it last. */
11036079Swollman	so_gen_t so_gencnt;		/* generation count */
1111541Srgrimes};
1121541Srgrimes
1131541Srgrimes/*
1141541Srgrimes * Socket state bits.
1151541Srgrimes */
11614547Sdg#define	SS_NOFDREF		0x0001	/* no file table ref any more */
11714547Sdg#define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
11814547Sdg#define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
11914547Sdg#define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
12014547Sdg#define	SS_CANTSENDMORE		0x0010	/* can't send more data to peer */
12114547Sdg#define	SS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
12214547Sdg#define	SS_RCVATMARK		0x0040	/* at mark on input */
1231541Srgrimes
12414547Sdg#define	SS_NBIO			0x0100	/* non-blocking ops */
12514547Sdg#define	SS_ASYNC		0x0200	/* async i/o notify */
12614547Sdg#define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
1271541Srgrimes
12814547Sdg#define	SS_INCOMP		0x0800	/* unaccepted, incomplete connection */
12914547Sdg#define	SS_COMP			0x1000	/* unaccepted, complete connection */
13043196Sfenner#define	SS_ISDISCONNECTED	0x2000	/* socket disconnected from peer */
1311541Srgrimes
13236079Swollman/*
13336079Swollman * Externalized form of struct socket used by the sysctl(3) interface.
13436079Swollman */
13536079Swollmanstruct	xsocket {
13636079Swollman	size_t	xso_len;	/* length of this structure */
13736079Swollman	struct	socket *xso_so;	/* makes a convenient handle sometimes */
13836079Swollman	short	so_type;
13936079Swollman	short	so_options;
14036079Swollman	short	so_linger;
14136079Swollman	short	so_state;
14236079Swollman	caddr_t	so_pcb;		/* another convenient handle */
14336079Swollman	int	xso_protocol;
14436079Swollman	int	xso_family;
14536079Swollman	short	so_qlen;
14636079Swollman	short	so_incqlen;
14736079Swollman	short	so_qlimit;
14836079Swollman	short	so_timeo;
14936079Swollman	u_short	so_error;
15036079Swollman	pid_t	so_pgid;
15136079Swollman	u_long	so_oobmark;
15236079Swollman	struct	xsockbuf {
15336079Swollman		u_long	sb_cc;
15436079Swollman		u_long	sb_hiwat;
15536079Swollman		u_long	sb_mbcnt;
15636079Swollman		u_long	sb_mbmax;
15736079Swollman		long	sb_lowat;
15836079Swollman		short	sb_flags;
15936079Swollman		short	sb_timeo;
16036079Swollman	} so_rcv, so_snd;
16136079Swollman	uid_t	so_uid;		/* XXX */
16236079Swollman};
16314547Sdg
1641541Srgrimes/*
1651541Srgrimes * Macros for sockets and socket buffering.
1661541Srgrimes */
1671541Srgrimes
1681541Srgrimes/*
16936527Speter * Do we need to notify the other side when I/O is possible?
17036527Speter */
17136527Speter#define sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC|SB_UPCALL)) != 0)
17236527Speter
17336527Speter/*
1741541Srgrimes * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
1751541Srgrimes * This is problematical if the fields are unsigned, as the space might
1761541Srgrimes * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
1771541Srgrimes * overflow and return 0.  Should use "lmin" but it doesn't exist now.
1781541Srgrimes */
1791541Srgrimes#define	sbspace(sb) \
1801541Srgrimes    ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
1811541Srgrimes	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
1821541Srgrimes
1831541Srgrimes/* do we have to send all at once on a socket? */
1841541Srgrimes#define	sosendallatonce(so) \
1851541Srgrimes    ((so)->so_proto->pr_flags & PR_ATOMIC)
1861541Srgrimes
1871541Srgrimes/* can we read something from so? */
1881541Srgrimes#define	soreadable(so) \
1891541Srgrimes    ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
1901541Srgrimes	((so)->so_state & SS_CANTRCVMORE) || \
19114547Sdg	(so)->so_comp.tqh_first || (so)->so_error)
1921541Srgrimes
1931541Srgrimes/* can we write something to so? */
1941541Srgrimes#define	sowriteable(so) \
1953304Sphk    ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
1961541Srgrimes	(((so)->so_state&SS_ISCONNECTED) || \
1973304Sphk	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
1981541Srgrimes     ((so)->so_state & SS_CANTSENDMORE) || \
1991541Srgrimes     (so)->so_error)
2001541Srgrimes
2011541Srgrimes/* adjust counters in sb reflecting allocation of m */
2021541Srgrimes#define	sballoc(sb, m) { \
2031541Srgrimes	(sb)->sb_cc += (m)->m_len; \
2041541Srgrimes	(sb)->sb_mbcnt += MSIZE; \
2051541Srgrimes	if ((m)->m_flags & M_EXT) \
2061541Srgrimes		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
2071541Srgrimes}
2081541Srgrimes
2091541Srgrimes/* adjust counters in sb reflecting freeing of m */
2101541Srgrimes#define	sbfree(sb, m) { \
2111541Srgrimes	(sb)->sb_cc -= (m)->m_len; \
2121541Srgrimes	(sb)->sb_mbcnt -= MSIZE; \
2131541Srgrimes	if ((m)->m_flags & M_EXT) \
2141541Srgrimes		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
2151541Srgrimes}
2161541Srgrimes
2171541Srgrimes/*
2181541Srgrimes * Set lock on sockbuf sb; sleep if lock is already held.
2191541Srgrimes * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
2201541Srgrimes * Returns error without lock if sleep is interrupted.
2211541Srgrimes */
2221541Srgrimes#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
2231541Srgrimes		(((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
2241541Srgrimes		((sb)->sb_flags |= SB_LOCK), 0)
2251541Srgrimes
2261541Srgrimes/* release lock on sockbuf sb */
2271541Srgrimes#define	sbunlock(sb) { \
2281541Srgrimes	(sb)->sb_flags &= ~SB_LOCK; \
2291541Srgrimes	if ((sb)->sb_flags & SB_WANT) { \
2301541Srgrimes		(sb)->sb_flags &= ~SB_WANT; \
2311541Srgrimes		wakeup((caddr_t)&(sb)->sb_flags); \
2321541Srgrimes	} \
2331541Srgrimes}
2341541Srgrimes
23536527Speter#define	sorwakeup(so)	do { \
23636527Speter			  if (sb_notify(&(so)->so_rcv)) \
23736527Speter			    sowakeup((so), &(so)->so_rcv); \
23836527Speter			} while (0)
2391541Srgrimes
24036527Speter#define	sowwakeup(so)	do { \
24136527Speter			  if (sb_notify(&(so)->so_snd)) \
24236527Speter			    sowakeup((so), &(so)->so_snd); \
24336527Speter			} while (0)
2441541Srgrimes
2451541Srgrimes#ifdef KERNEL
24631927Sbde
24738482Swollman/*
24838482Swollman * Argument structure for sosetopt et seq.  This is in the KERNEL
24938482Swollman * section because it will never be visible to user code.
25038482Swollman */
25138482Swollmanenum sopt_dir { SOPT_GET, SOPT_SET };
25238482Swollmanstruct sockopt {
25338482Swollman	enum	sopt_dir sopt_dir; /* is this a get or a set? */
25438482Swollman	int	sopt_level;	/* second arg of [gs]etsockopt */
25538482Swollman	int	sopt_name;	/* third arg of [gs]etsockopt */
25638482Swollman	void   *sopt_val;	/* fourth arg of [gs]etsockopt */
25738482Swollman	size_t	sopt_valsize;	/* (almost) fifth arg of [gs]etsockopt */
25838482Swollman	struct	proc *sopt_p;	/* calling process or null if kernel */
25938482Swollman};
26038482Swollman
26140931Sdgstruct sf_buf {
26240931Sdg	SLIST_ENTRY(sf_buf) free_list;	/* list of free buffer slots */
26340931Sdg	int		refcnt;		/* reference count */
26440931Sdg	struct		vm_page *m;	/* currently mapped page */
26540931Sdg	vm_offset_t	kva;		/* va of mapping */
26640931Sdg};
26740931Sdg
26831927Sbde#ifdef MALLOC_DECLARE
26931927SbdeMALLOC_DECLARE(M_PCB);
27031927SbdeMALLOC_DECLARE(M_SONAME);
27131927Sbde#endif
27231927Sbde
27336079Swollmanextern int	maxsockets;
2742112Swollmanextern u_long	sb_max;
27536079Swollmanextern struct	vm_zone *socket_zone;
27636079Swollmanextern so_gen_t so_gencnt;
27714505Shsu
27832995Sbdestruct file;
27915492Sbdestruct filedesc;
28015492Sbdestruct mbuf;
28115492Sbdestruct sockaddr;
28215492Sbdestruct stat;
28332995Sbdestruct ucred;
28432995Sbdestruct uio;
28515492Sbde
2861541Srgrimes/*
2871541Srgrimes * File operations on sockets.
2881541Srgrimes */
28936735Sdfrint	soo_ioctl __P((struct file *fp, u_long cmd, caddr_t data,
29014505Shsu	    struct proc *p));
29129350Speterint	soo_poll __P((struct file *fp, int events, struct ucred *cred,
29229350Speter	    struct proc *p));
29314505Shsuint	soo_stat __P((struct socket *so, struct stat *ub));
2943304Sphk
2953304Sphk/*
2963304Sphk * From uipc_socket and friends
2973304Sphk */
29828270Swollmanstruct	sockaddr *dup_sockaddr __P((struct sockaddr *sa, int canwait));
29914505Shsuint	getsock __P((struct filedesc *fdp, int fdes, struct file **fpp));
30014505Shsuint	sockargs __P((struct mbuf **mp, caddr_t buf, int buflen, int type));
30128270Swollmanint	getsockaddr __P((struct sockaddr **namp, caddr_t uaddr, size_t len));
30214505Shsuvoid	sbappend __P((struct sockbuf *sb, struct mbuf *m));
30314505Shsuint	sbappendaddr __P((struct sockbuf *sb, struct sockaddr *asa,
30414505Shsu	    struct mbuf *m0, struct mbuf *control));
30514505Shsuint	sbappendcontrol __P((struct sockbuf *sb, struct mbuf *m0,
30614505Shsu	    struct mbuf *control));
30714505Shsuvoid	sbappendrecord __P((struct sockbuf *sb, struct mbuf *m0));
30814505Shsuvoid	sbcheck __P((struct sockbuf *sb));
30914505Shsuvoid	sbcompress __P((struct sockbuf *sb, struct mbuf *m, struct mbuf *n));
31019670Sbdestruct mbuf *
31119670Sbde	sbcreatecontrol __P((caddr_t p, int size, int type, int level));
31214505Shsuvoid	sbdrop __P((struct sockbuf *sb, int len));
31314505Shsuvoid	sbdroprecord __P((struct sockbuf *sb));
31414505Shsuvoid	sbflush __P((struct sockbuf *sb));
31514505Shsuvoid	sbinsertoob __P((struct sockbuf *sb, struct mbuf *m0));
31614505Shsuvoid	sbrelease __P((struct sockbuf *sb));
31714505Shsuint	sbreserve __P((struct sockbuf *sb, u_long cc));
31836079Swollmanvoid	sbtoxsockbuf __P((struct sockbuf *sb, struct xsockbuf *xsb));
31914505Shsuint	sbwait __P((struct sockbuf *sb));
32014505Shsuint	sb_lock __P((struct sockbuf *sb));
32114505Shsuint	soabort __P((struct socket *so));
32228270Swollmanint	soaccept __P((struct socket *so, struct sockaddr **nam));
32336079Swollmanstruct	socket *soalloc __P((int waitok));
32428270Swollmanint	sobind __P((struct socket *so, struct sockaddr *nam, struct proc *p));
32514505Shsuvoid	socantrcvmore __P((struct socket *so));
32614505Shsuvoid	socantsendmore __P((struct socket *so));
32714505Shsuint	soclose __P((struct socket *so));
32828270Swollmanint	soconnect __P((struct socket *so, struct sockaddr *nam, struct proc *p));
32914505Shsuint	soconnect2 __P((struct socket *so1, struct socket *so2));
33014505Shsuint	socreate __P((int dom, struct socket **aso, int type, int proto,
33114505Shsu	    struct proc *p));
33236079Swollmanvoid	sodealloc __P((struct socket *so));
33314505Shsuint	sodisconnect __P((struct socket *so));
33414505Shsuvoid	sofree __P((struct socket *so));
33538482Swollmanint	sogetopt __P((struct socket *so, struct sockopt *sopt));
33614505Shsuvoid	sohasoutofband __P((struct socket *so));
33714505Shsuvoid	soisconnected __P((struct socket *so));
33814505Shsuvoid	soisconnecting __P((struct socket *so));
33914505Shsuvoid	soisdisconnected __P((struct socket *so));
34014505Shsuvoid	soisdisconnecting __P((struct socket *so));
34125201Swollmanint	solisten __P((struct socket *so, int backlog, struct proc *p));
34214505Shsustruct socket *
34318787Spst	sodropablereq __P((struct socket *head));
34418787Spststruct socket *
34527531Sfenner	sonewconn __P((struct socket *head, int connstatus));
34638482Swollmanint	sooptcopyin __P((struct sockopt *sopt, void *buf, size_t len,
34738482Swollman			 size_t minlen));
34838482Swollmanint	sooptcopyout __P((struct sockopt *sopt, void *buf, size_t len));
34929350Speterint	sopoll __P((struct socket *so, int events, struct ucred *cred,
35029350Speter		    struct proc *p));
35128270Swollmanint	soreceive __P((struct socket *so, struct sockaddr **paddr,
35228270Swollman		       struct uio *uio, struct mbuf **mp0,
35328270Swollman		       struct mbuf **controlp, int *flagsp));
35414505Shsuint	soreserve __P((struct socket *so, u_long sndcc, u_long rcvcc));
35514505Shsuvoid	sorflush __P((struct socket *so));
35628270Swollmanint	sosend __P((struct socket *so, struct sockaddr *addr, struct uio *uio,
35728270Swollman		    struct mbuf *top, struct mbuf *control, int flags,
35828270Swollman		    struct proc *p));
35938482Swollmanint	sosetopt __P((struct socket *so, struct sockopt *sopt));
36014505Shsuint	soshutdown __P((struct socket *so, int how));
36136079Swollmanvoid	sotoxsocket __P((struct socket *so, struct xsocket *xso));
36214505Shsuvoid	sowakeup __P((struct socket *so, struct sockbuf *sb));
36331927Sbde
36414505Shsu#endif /* KERNEL */
3652165Spaul
36614505Shsu#endif /* !_SYS_SOCKETVAR_H_ */
367