xref: /dflybsd-src/sys/sys/socketvar.h (revision b5b0912b1891e95ccc48cad83f09239ccb7ffc16)
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: src/sys/sys/socketvar.h,v 1.46.2.10 2003/08/24 08:24:39 hsu Exp $
35  * $DragonFly: src/sys/sys/socketvar.h,v 1.18 2005/03/28 19:53:30 hsu Exp $
36  */
37 
38 #ifndef _SYS_SOCKETVAR_H_
39 #define _SYS_SOCKETVAR_H_
40 
41 #include <sys/queue.h>			/* for TAILQ macros */
42 #include <sys/select.h>			/* for struct selinfo */
43 
44 /*
45  * Kernel structure per socket.
46  * Contains send and receive buffer queues,
47  * handle on protocol and pointer to protocol
48  * private data and error information.
49  */
50 typedef	u_quad_t so_gen_t;
51 
52 struct accept_filter;
53 
54 struct socket {
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 	void	*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_incomp queues partially completed connections,
66  * while so_comp 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_incomp or so_comp.
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	mbuf *sb_lastmbuf;	/* last mbuf in sb_mb */
97 		struct	mbuf *sb_lastrecord;	/* last record in sb_mb
98 						 * valid <=> sb_mb non-NULL */
99 		struct	selinfo sb_sel;	/* process selecting read/write */
100 		short	sb_flags;	/* flags, see below */
101 		short	sb_timeo;	/* timeout for read/write */
102 	} so_rcv, so_snd;
103 #define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
104 #define	SB_LOCK		0x01		/* lock on data queue */
105 #define	SB_WANT		0x02		/* someone is waiting to lock */
106 #define	SB_WAIT		0x04		/* someone is waiting for data/space */
107 #define	SB_SEL		0x08		/* someone is selecting */
108 #define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
109 #define	SB_UPCALL	0x20		/* someone wants an upcall */
110 #define	SB_NOINTR	0x40		/* operations not interruptible */
111 #define SB_AIO		0x80		/* AIO operations queued */
112 #define SB_KNOTE	0x100		/* kernel note attached */
113 #define SB_MEVENT	0x200		/* need message event notification */
114 
115 	void	(*so_upcall) (struct socket *, void *, int);
116 	void	*so_upcallarg;
117 	struct	ucred *so_cred;		/* user credentials */
118 	/* NB: generation count must not be first; easiest to make it last. */
119 	so_gen_t so_gencnt;		/* generation count */
120 	void	*so_emuldata;		/* private data for emulators */
121 	struct	so_accf {
122 		struct	accept_filter *so_accept_filter;
123 		void	*so_accept_filter_arg;	/* saved filter args */
124 		char	*so_accept_filter_str;	/* saved user args */
125 	} *so_accf;
126 };
127 
128 /*
129  * Socket state bits.
130  */
131 #define	SS_NOFDREF		0x0001	/* no file table ref any more */
132 #define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
133 #define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
134 #define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
135 #define	SS_CANTSENDMORE		0x0010	/* can't send more data to peer */
136 #define	SS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
137 #define	SS_RCVATMARK		0x0040	/* at mark on input */
138 
139 #define	SS_NBIO			0x0100	/* non-blocking ops */
140 #define	SS_ASYNC		0x0200	/* async i/o notify */
141 #define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
142 
143 #define	SS_INCOMP		0x0800	/* unaccepted, incomplete connection */
144 #define	SS_COMP			0x1000	/* unaccepted, complete connection */
145 #define	SS_ISDISCONNECTED	0x2000	/* socket disconnected from peer */
146 
147 /*
148  * Externalized form of struct socket used by the sysctl(3) interface.
149  */
150 struct	xsocket {
151 	size_t	xso_len;	/* length of this structure */
152 	struct	socket *xso_so;	/* makes a convenient handle sometimes */
153 	short	so_type;
154 	short	so_options;
155 	short	so_linger;
156 	short	so_state;
157 	void	*so_pcb;		/* another convenient handle */
158 	int	xso_protocol;
159 	int	xso_family;
160 	short	so_qlen;
161 	short	so_incqlen;
162 	short	so_qlimit;
163 	short	so_timeo;
164 	u_short	so_error;
165 	pid_t	so_pgid;
166 	u_long	so_oobmark;
167 	struct	xsockbuf {
168 		u_long	sb_cc;
169 		u_long	sb_hiwat;
170 		u_long	sb_mbcnt;
171 		u_long	sb_mbmax;
172 		long	sb_lowat;
173 		short	sb_flags;
174 		short	sb_timeo;
175 	} so_rcv, so_snd;
176 	uid_t	so_uid;		/* XXX */
177 };
178 
179 /*
180  * Macros for sockets and socket buffering.
181  */
182 
183 /*
184  * Do we need to notify the other side when I/O is possible?
185  */
186 #define	sb_notify(sb)						\
187 (((sb)->sb_flags &						\
188  (SB_WAIT | SB_SEL | SB_ASYNC | SB_UPCALL | SB_AIO | SB_KNOTE | SB_MEVENT)))
189 
190 /*
191  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
192  * This is problematical if the fields are unsigned, as the space might
193  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
194  * overflow and return 0.  Should use "lmin" but it doesn't exist now.
195  */
196 #define	sbspace(sb) \
197     ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
198 	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
199 
200 /* do we have to send all at once on a socket? */
201 #define	sosendallatonce(so) \
202     ((so)->so_proto->pr_flags & PR_ATOMIC)
203 
204 /* can we read something from so? */
205 #define	soreadable(so) \
206     ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
207 	((so)->so_state & SS_CANTRCVMORE) || \
208 	!TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
209 
210 /* can we write something to so? */
211 #define	sowriteable(so) \
212     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
213 	(((so)->so_state&SS_ISCONNECTED) || \
214 	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
215      ((so)->so_state & SS_CANTSENDMORE) || \
216      (so)->so_error)
217 
218 /* adjust counters in sb reflecting allocation of m */
219 #define	sballoc(sb, m) { \
220 	(sb)->sb_cc += (m)->m_len; \
221 	(sb)->sb_mbcnt += MSIZE; \
222 	if ((m)->m_flags & M_EXT) \
223 		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
224 }
225 
226 /* adjust counters in sb reflecting freeing of m */
227 #define	sbfree(sb, m) { \
228 	(sb)->sb_cc -= (m)->m_len; \
229 	(sb)->sb_mbcnt -= MSIZE; \
230 	if ((m)->m_flags & M_EXT) \
231 		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
232 }
233 
234 /*
235  * Set lock on sockbuf sb; sleep if lock is already held.
236  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
237  * Returns error without lock if sleep is interrupted.
238  */
239 #define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
240 		(((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
241 		((sb)->sb_flags |= SB_LOCK), 0)
242 
243 /* release lock on sockbuf sb */
244 #define	sbunlock(sb) { \
245 	(sb)->sb_flags &= ~SB_LOCK; \
246 	if ((sb)->sb_flags & SB_WANT) { \
247 		(sb)->sb_flags &= ~SB_WANT; \
248 		wakeup((caddr_t)&(sb)->sb_flags); \
249 	} \
250 }
251 
252 #define	sorwakeup(so)	do { \
253 			  if (sb_notify(&(so)->so_rcv)) \
254 			    sowakeup((so), &(so)->so_rcv); \
255 			} while (0)
256 
257 #define	sowwakeup(so)	do { \
258 			  if (sb_notify(&(so)->so_snd)) \
259 			    sowakeup((so), &(so)->so_snd); \
260 			} while (0)
261 
262 #ifdef _KERNEL
263 
264 /*
265  * Argument structure for sosetopt et seq.  This is in the KERNEL
266  * section because it will never be visible to user code.
267  */
268 enum sopt_dir { SOPT_GET, SOPT_SET };
269 struct sockopt {
270 	enum	sopt_dir sopt_dir; /* is this a get or a set? */
271 	int	sopt_level;	/* second arg of [gs]etsockopt */
272 	int	sopt_name;	/* third arg of [gs]etsockopt */
273 	void   *sopt_val;	/* fourth arg of [gs]etsockopt */
274 	size_t	sopt_valsize;	/* (almost) fifth arg of [gs]etsockopt */
275 	struct	thread *sopt_td; /* calling thread or null if kernel */
276 };
277 
278 struct accept_filter {
279 	char	accf_name[16];
280 	void	(*accf_callback)
281 		(struct socket *so, void *arg, int waitflag);
282 	void *	(*accf_create)
283 		(struct socket *so, char *arg);
284 	void	(*accf_destroy)
285 		(struct socket *so);
286 	SLIST_ENTRY(accept_filter) accf_next;	/* next on the list */
287 };
288 
289 #ifdef MALLOC_DECLARE
290 MALLOC_DECLARE(M_PCB);
291 MALLOC_DECLARE(M_SONAME);
292 MALLOC_DECLARE(M_ACCF);
293 #endif
294 
295 extern int	maxsockets;
296 extern u_long	sb_max;		/* nominal limit */
297 extern u_long	sb_max_adj;	/* actual limit used by sbreserve() */
298 extern struct	vm_zone *socket_zone;
299 extern so_gen_t so_gencnt;
300 
301 struct file;
302 struct filedesc;
303 struct mbuf;
304 struct rlimit;
305 struct sockaddr;
306 struct stat;
307 struct ucred;
308 struct uio;
309 struct knote;
310 
311 /*
312  * File operations on sockets.
313  */
314 int	soo_read (struct file *fp, struct uio *uio, struct ucred *cred,
315 	    int flags, struct thread *td);
316 int	soo_write (struct file *fp, struct uio *uio, struct ucred *cred,
317 	    int flags, struct thread *td);
318 int	soo_close (struct file *fp, struct thread *td);
319 int	soo_ioctl (struct file *fp, u_long cmd, caddr_t data,
320 	    struct thread *td);
321 int	soo_poll (struct file *fp, int events, struct ucred *cred,
322 	    struct thread *td);
323 int	soo_stat (struct file *fp, struct stat *ub, struct thread *td);
324 int	sokqfilter (struct file *fp, struct knote *kn);
325 
326 /*
327  * From uipc_socket and friends
328  */
329 struct	sockaddr *dup_sockaddr (const struct sockaddr *sa);
330 int	holdsock (struct filedesc *fdp, int fdes, struct file **fpp);
331 int	getsockaddr (struct sockaddr **namp, caddr_t uaddr, size_t len);
332 void	sbappend (struct sockbuf *sb, struct mbuf *m);
333 int	sbappendaddr (struct sockbuf *sb, const struct sockaddr *asa,
334 	    struct mbuf *m0, struct mbuf *control);
335 int	sbappendcontrol (struct sockbuf *sb, struct mbuf *m0,
336 	    struct mbuf *control);
337 void	sbappendrecord (struct sockbuf *sb, struct mbuf *m0);
338 void	sbappendstream (struct sockbuf *sb, struct mbuf *m);
339 void	sbcheck (struct sockbuf *sb);
340 void	sbcompress (struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
341 struct mbuf *
342 	sbcreatecontrol (caddr_t p, int size, int type, int level);
343 void	sbdrop (struct sockbuf *sb, int len);
344 void	sbdroprecord (struct sockbuf *sb);
345 void	sbflush (struct sockbuf *sb);
346 void	sbinsertoob (struct sockbuf *sb, struct mbuf *m0);
347 void	sbrelease (struct sockbuf *sb, struct socket *so);
348 int	sbreserve (struct sockbuf *sb, u_long cc, struct socket *so,
349 		   struct rlimit *rl);
350 void	sbtoxsockbuf (struct sockbuf *sb, struct xsockbuf *xsb);
351 int	sbwait (struct sockbuf *sb);
352 int	sb_lock (struct sockbuf *sb);
353 int	soabort (struct socket *so);
354 int	soaccept (struct socket *so, struct sockaddr **nam);
355 struct	socket *soalloc (int waitok);
356 int	sobind (struct socket *so, struct sockaddr *nam, struct thread *td);
357 void	socantrcvmore (struct socket *so);
358 void	socantsendmore (struct socket *so);
359 int	soclose (struct socket *so);
360 int	soconnect (struct socket *so, struct sockaddr *nam, struct thread *td);
361 int	soconnect2 (struct socket *so1, struct socket *so2);
362 int	socreate (int dom, struct socket **aso, int type, int proto,
363 	    struct thread *td);
364 void	sodealloc (struct socket *so);
365 int	sodisconnect (struct socket *so);
366 void	sofree (struct socket *so);
367 int	sogetopt (struct socket *so, struct sockopt *sopt);
368 void	sohasoutofband (struct socket *so);
369 void	soisconnected (struct socket *so);
370 void	soisconnecting (struct socket *so);
371 void	soisdisconnected (struct socket *so);
372 void	soisdisconnecting (struct socket *so);
373 int	solisten (struct socket *so, int backlog, struct thread *td);
374 struct socket *sonewconn (struct socket *head, int connstatus);
375 int	sooptcopyin (struct sockopt *sopt, void *buf, size_t len,
376 			 size_t minlen);
377 int	sooptcopyout (struct sockopt *sopt, const void *buf, size_t len);
378 
379 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
380 int	soopt_getm (struct sockopt *sopt, struct mbuf **mp);
381 int	soopt_mcopyin (struct sockopt *sopt, struct mbuf *m);
382 int	soopt_mcopyout (struct sockopt *sopt, struct mbuf *m);
383 
384 int	sopoll (struct socket *so, int events, struct ucred *cred,
385 		    struct thread *td);
386 int	soreceive (struct socket *so, struct sockaddr **paddr,
387 		       struct uio *uio, struct mbuf **mp0,
388 		       struct mbuf **controlp, int *flagsp);
389 int	soreserve (struct socket *so, u_long sndcc, u_long rcvcc,
390 		   struct rlimit *rl);
391 void	sorflush (struct socket *so);
392 int	sosend (struct socket *so, struct sockaddr *addr, struct uio *uio,
393 		    struct mbuf *top, struct mbuf *control, int flags,
394 		    struct thread *td);
395 int	sosendudp (struct socket *so, struct sockaddr *addr, struct uio *uio,
396 		    struct mbuf *top, struct mbuf *control, int flags,
397 		    struct thread *td);
398 int	sosetopt (struct socket *so, struct sockopt *sopt);
399 int	soshutdown (struct socket *so, int how);
400 void	sotoxsocket (struct socket *so, struct xsocket *xso);
401 void	sowakeup (struct socket *so, struct sockbuf *sb);
402 
403 /* accept filter functions */
404 int	accept_filt_add (struct accept_filter *filt);
405 int	accept_filt_del (char *name);
406 struct accept_filter *	accept_filt_get (char *name);
407 #ifdef ACCEPT_FILTER_MOD
408 int accept_filt_generic_mod_event (module_t mod, int event, void *data);
409 SYSCTL_DECL(_net_inet_accf);
410 #endif /* ACCEPT_FILTER_MOD */
411 
412 #endif /* _KERNEL */
413 
414 #endif /* !_SYS_SOCKETVAR_H_ */
415