1 /*	$OpenBSD: socketvar.h,v 1.34 2004/04/19 22:38:37 deraadt Exp $	*/
2 /*	$NetBSD: socketvar.h,v 1.18 1996/02/09 18:25:38 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1982, 1986, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)socketvar.h	8.1 (Berkeley) 6/2/93
33  */
34 
35 #include <sys/select.h>			/* for struct selinfo */
36 #include <sys/queue.h>
37 
38 TAILQ_HEAD(soqhead, socket);
39 
40 /*
41  * Kernel structure per socket.
42  * Contains send and receive buffer queues,
43  * handle on protocol and pointer to protocol
44  * private data and error information.
45  */
46 struct socket {
47 	short	so_type;		/* generic type, see socket.h */
48 	short	so_options;		/* from socket call, see socket.h */
49 	short	so_linger;		/* time to linger while closing */
50 	short	so_state;		/* internal state flags SS_*, below */
51 	void	*so_pcb;		/* protocol control block */
52 	struct	protosw *so_proto;	/* protocol handle */
53 /*
54  * Variables for connection queueing.
55  * Socket where accepts occur is so_head in all subsidiary sockets.
56  * If so_head is 0, socket is not related to an accept.
57  * For head socket so_q0 queues partially completed connections,
58  * while so_q is a queue of connections ready to be accepted.
59  * If a connection is aborted and it has so_head set, then
60  * it has to be pulled out of either so_q0 or so_q.
61  * We allow connections to queue up based on current queue lengths
62  * and limit on number of queued connections for this socket.
63  */
64 	struct	socket	*so_head;	/* back pointer to accept socket */
65 	struct	soqhead	*so_onq;	/* queue (q or q0) that we're on */
66 	struct	soqhead	so_q0;		/* queue of partial connections */
67 	struct	soqhead	so_q;		/* queue of incoming connections */
68 	TAILQ_ENTRY(socket) so_qe;	/* our queue entry (q or q0) */
69 	short	so_q0len;		/* partials on so_q0 */
70 	short	so_qlen;		/* number of connections on so_q */
71 	short	so_qlimit;		/* max number queued connections */
72 	short	so_timeo;		/* connection timeout */
73 	u_short	so_error;		/* error affecting connection */
74 	pid_t	so_pgid;		/* pgid for signals */
75 	uid_t	so_siguid;		/* uid of process who set so_pgid */
76 	uid_t	so_sigeuid;		/* euid of process who set so_pgid */
77 	u_long	so_oobmark;		/* chars to oob mark */
78 /*
79  * Variables for socket buffering.
80  */
81 	struct	sockbuf {
82 		u_long	sb_cc;		/* actual chars in buffer */
83 		u_long	sb_hiwat;	/* max actual char count */
84 		u_long	sb_mbcnt;	/* chars of mbufs used */
85 		u_long	sb_mbmax;	/* max chars of mbufs to use */
86 		long	sb_lowat;	/* low water mark */
87 		struct mbuf *sb_mb;	/* the mbuf chain */
88 		struct mbuf *sb_mbtail;	/* the last mbuf in the chain */
89 		struct mbuf *sb_lastrecord;/* first mbuf of last record in
90 					      socket buffer */
91 		struct	selinfo sb_sel;	/* process selecting read/write */
92 		short	sb_flags;	/* flags, see below */
93 		short	sb_timeo;	/* timeout for read/write */
94 	} so_rcv, so_snd;
95 #define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
96 #define	SB_LOCK		0x01		/* lock on data queue */
97 #define	SB_WANT		0x02		/* someone is waiting to lock */
98 #define	SB_WAIT		0x04		/* someone is waiting for data/space */
99 #define	SB_SEL		0x08		/* someone is selecting */
100 #define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
101 #define	SB_NOINTR	0x40		/* operations not interruptible */
102 #define	SB_KNOTE	0x80		/* kernel note attached */
103 
104 	void	*so_internal;		/* Space for svr4 stream data */
105 	void	(*so_upcall)(struct socket *so, caddr_t arg, int waitf);
106 	caddr_t	so_upcallarg;		/* Arg for above */
107 	uid_t	so_euid, so_ruid;	/* who opened the socket */
108 	gid_t	so_egid, so_rgid;
109 };
110 
111 #define	SB_EMPTY_FIXUP(sb)						\
112 do {									\
113 	if ((sb)->sb_mb == NULL) {					\
114 		(sb)->sb_mbtail = NULL;					\
115 		(sb)->sb_lastrecord = NULL;				\
116 	}								\
117 } while (/*CONSTCOND*/0)
118 
119 /*
120  * Socket state bits.
121  */
122 #define	SS_NOFDREF		0x001	/* no file table ref any more */
123 #define	SS_ISCONNECTED		0x002	/* socket connected to a peer */
124 #define	SS_ISCONNECTING		0x004	/* in process of connecting to peer */
125 #define	SS_ISDISCONNECTING	0x008	/* in process of disconnecting */
126 #define	SS_CANTSENDMORE		0x010	/* can't send more data to peer */
127 #define	SS_CANTRCVMORE		0x020	/* can't receive more data from peer */
128 #define	SS_RCVATMARK		0x040	/* at mark on input */
129 #define	SS_ISDISCONNECTED	0x800	/* socket disconnected from peer */
130 
131 #define	SS_PRIV			0x080	/* privileged for broadcast, raw... */
132 #define	SS_NBIO			0x100	/* non-blocking ops */
133 #define	SS_ASYNC		0x200	/* async i/o notify */
134 #define	SS_ISCONFIRMING		0x400	/* deciding to accept connection req */
135 #define	SS_CONNECTOUT		0x1000	/* connect, not accept, at this end */
136 
137 /*
138  * Macros for sockets and socket buffering.
139  */
140 
141 /*
142  * Do we need to notify the other side when I/O is possible?
143  */
144 #define	sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC| \
145     SB_KNOTE)) != 0)
146 
147 /*
148  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
149  * This is problematical if the fields are unsigned, as the space might
150  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
151  * overflow and return 0.  Should use "lmin" but it doesn't exist now.
152  */
153 #define	sbspace(sb) \
154     ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
155 	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
156 
157 /* do we have to send all at once on a socket? */
158 #define	sosendallatonce(so) \
159     ((so)->so_proto->pr_flags & PR_ATOMIC)
160 
161 /* can we read something from so? */
162 #define	soreadable(so) \
163     ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
164 	((so)->so_state & SS_CANTRCVMORE) || \
165 	(so)->so_qlen || (so)->so_error)
166 
167 /* can we write something to so? */
168 #define	sowriteable(so) \
169     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
170 	(((so)->so_state&SS_ISCONNECTED) || \
171 	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
172     ((so)->so_state & SS_CANTSENDMORE) || (so)->so_error)
173 
174 /* adjust counters in sb reflecting allocation of m */
175 #define	sballoc(sb, m) { \
176 	(sb)->sb_cc += (m)->m_len; \
177 	(sb)->sb_mbcnt += MSIZE; \
178 	if ((m)->m_flags & M_EXT) \
179 		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
180 }
181 
182 /* adjust counters in sb reflecting freeing of m */
183 #define	sbfree(sb, m) { \
184 	(sb)->sb_cc -= (m)->m_len; \
185 	(sb)->sb_mbcnt -= MSIZE; \
186 	if ((m)->m_flags & M_EXT) \
187 		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
188 }
189 
190 /*
191  * Set lock on sockbuf sb; sleep if lock is already held.
192  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
193  * Returns error without lock if sleep is interrupted.
194  */
195 #define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
196 		(((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
197 		((sb)->sb_flags |= SB_LOCK), 0)
198 
199 /* release lock on sockbuf sb */
200 #define	sbunlock(sb) { \
201 	(sb)->sb_flags &= ~SB_LOCK; \
202 	if ((sb)->sb_flags & SB_WANT) { \
203 		(sb)->sb_flags &= ~SB_WANT; \
204 		wakeup((caddr_t)&(sb)->sb_flags); \
205 	} \
206 }
207 
208 #define	sorwakeup(so)	{ sowakeup((so), &(so)->so_rcv); \
209 			  if ((so)->so_upcall) \
210 			    (*((so)->so_upcall))((so), (so)->so_upcallarg, M_DONTWAIT); \
211 			}
212 
213 #define	sowwakeup(so)	sowakeup((so), &(so)->so_snd)
214 
215 #ifdef _KERNEL
216 extern u_long sb_max;
217 struct	socket *sonewconn(struct socket *head, int connstatus);
218 
219 /* strings for sleep message: */
220 extern	const char netio[], netcon[], netcls[];
221 
222 extern struct pool	socket_pool;
223 
224 struct mbuf;
225 struct sockaddr;
226 struct proc;
227 struct msghdr;
228 struct stat;
229 struct knote;
230 
231 /*
232  * File operations on sockets.
233  */
234 int	soo_read(struct file *fp, off_t *, struct uio *uio,
235 	    struct ucred *cred);
236 int	soo_write(struct file *fp, off_t *, struct uio *uio,
237 	    struct ucred *cred);
238 int	soo_ioctl(struct file *fp, u_long cmd, caddr_t data,
239 	    struct proc *p);
240 int	soo_poll(struct file *fp, int events, struct proc *p);
241 int	soo_kqfilter(struct file *fp, struct knote *kn);
242 int 	soo_close(struct file *fp, struct proc *p);
243 int	soo_stat(struct file *, struct stat *, struct proc *);
244 int	uipc_usrreq(struct socket *, int , struct mbuf *,
245 			 struct mbuf *, struct mbuf *);
246 void	sbappend(struct sockbuf *sb, struct mbuf *m);
247 void	sbappendstream(struct sockbuf *sb, struct mbuf *m);
248 int	sbappendaddr(struct sockbuf *sb, struct sockaddr *asa,
249 	    struct mbuf *m0, struct mbuf *control);
250 int	sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
251 	    struct mbuf *control);
252 void	sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
253 void	sbcheck(struct sockbuf *sb);
254 void	sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
255 struct mbuf *
256 	sbcreatecontrol(caddr_t p, int size, int type, int level);
257 void	sbdrop(struct sockbuf *sb, int len);
258 void	sbdroprecord(struct sockbuf *sb);
259 void	sbflush(struct sockbuf *sb);
260 void	sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
261 void	sbrelease(struct sockbuf *sb);
262 int	sbcheckreserve(u_long cnt, u_long defcnt);
263 int	sbreserve(struct sockbuf *sb, u_long cc);
264 int	sbwait(struct sockbuf *sb);
265 int	sb_lock(struct sockbuf *sb);
266 void	soinit(void);
267 int	soabort(struct socket *so);
268 int	soaccept(struct socket *so, struct mbuf *nam);
269 int	sobind(struct socket *so, struct mbuf *nam);
270 void	socantrcvmore(struct socket *so);
271 void	socantsendmore(struct socket *so);
272 int	soclose(struct socket *so);
273 int	soconnect(struct socket *so, struct mbuf *nam);
274 int	soconnect2(struct socket *so1, struct socket *so2);
275 int	socreate(int dom, struct socket **aso, int type, int proto);
276 int	sodisconnect(struct socket *so);
277 void	sofree(struct socket *so);
278 int	sogetopt(struct socket *so, int level, int optname,
279 	    struct mbuf **mp);
280 void	sohasoutofband(struct socket *so);
281 void	soisconnected(struct socket *so);
282 void	soisconnecting(struct socket *so);
283 void	soisdisconnected(struct socket *so);
284 void	soisdisconnecting(struct socket *so);
285 int	solisten(struct socket *so, int backlog);
286 struct socket *sonewconn(struct socket *head, int connstatus);
287 void	soqinsque(struct socket *head, struct socket *so, int q);
288 int	soqremque(struct socket *so, int q);
289 int	soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
290 	    struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
291 int	soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
292 void	sorflush(struct socket *so);
293 int	sosend(struct socket *so, struct mbuf *addr, struct uio *uio,
294 	    struct mbuf *top, struct mbuf *control, int flags);
295 int	sosetopt(struct socket *so, int level, int optname,
296 	    struct mbuf *m0);
297 int	soshutdown(struct socket *so, int how);
298 void	sowakeup(struct socket *so, struct sockbuf *sb);
299 int	sockargs(struct mbuf **, const void *, size_t, int);
300 
301 int	sendit(struct proc *, int, struct msghdr *, int, register_t *);
302 int	recvit(struct proc *, int, struct msghdr *, caddr_t,
303 		    register_t *);
304 
305 #ifdef SOCKBUF_DEBUG
306 void	sblastrecordchk(struct sockbuf *, const char *);
307 #define	SBLASTRECORDCHK(sb, where)	sblastrecordchk((sb), (where))
308 
309 void	sblastmbufchk(struct sockbuf *, const char *);
310 #define	SBLASTMBUFCHK(sb, where)	sblastmbufchk((sb), (where))
311 #else
312 #define	SBLASTRECORDCHK(sb, where)	/* nothing */
313 #define	SBLASTMBUFCHK(sb, where)	/* nothing */
314 #endif /* SOCKBUF_DEBUG */
315 
316 #endif /* _KERNEL */
317