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