1 //==========================================================================
3 // include/sys/socketvar.h
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
23 * Copyright (c) 1982, 1986, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
55 * $FreeBSD: src/sys/sys/socketvar.h,v 1.46.2.6 2001/08/31 13:45:49 jlemon Exp $
58 #ifndef _SYS_SOCKETVAR_H_
59 #define _SYS_SOCKETVAR_H_
61 #include <sys/queue.h> /* for TAILQ macros */
64 * Kernel structure per socket.
65 * Contains send and receive buffer queues,
66 * handle on protocol and pointer to protocol
67 * private data and error information.
69 typedef u_quad_t so_gen_t;
74 struct vm_zone *so_zone; /* zone we were allocated from */
75 short so_type; /* generic type, see socket.h */
76 short so_options; /* from socket call, see socket.h */
77 short so_linger; /* time to linger while closing */
78 short so_state; /* internal state flags SS_*, below */
79 caddr_t so_pcb; /* protocol control block */
80 struct protosw *so_proto; /* protocol handle */
82 * Variables for connection queuing.
83 * Socket where accepts occur is so_head in all subsidiary sockets.
84 * If so_head is 0, socket is not related to an accept.
85 * For head socket so_incomp queues partially completed connections,
86 * while so_comp is a queue of connections ready to be accepted.
87 * If a connection is aborted and it has so_head set, then
88 * it has to be pulled out of either so_incomp or so_comp.
89 * We allow connections to queue up based on current queue lengths
90 * and limit on number of queued connections for this socket.
92 struct socket *so_head; /* back pointer to accept socket */
93 TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */
94 TAILQ_HEAD(, socket) so_comp; /* queue of complete unaccepted connections */
95 TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */
96 short so_qlen; /* number of unaccepted connections */
97 short so_incqlen; /* number of unaccepted incomplete
99 short so_qlimit; /* max number queued connections */
100 short so_timeo; /* connection timeout */
101 u_short so_error; /* error affecting connection */
102 struct sigio *so_sigio; /* information for async I/O or
103 out of band data (SIGURG) */
104 u_long so_oobmark; /* chars to oob mark */
105 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
107 * Variables for socket buffering.
110 u_long sb_cc; /* actual chars in buffer */
111 u_long sb_hiwat; /* max actual char count */
112 u_long sb_mbcnt; /* chars of mbufs used */
113 u_long sb_mbmax; /* max chars of mbufs to use */
114 long sb_lowat; /* low water mark */
115 struct mbuf *sb_mb; /* the mbuf chain */
116 struct selinfo sb_sel; /* process selecting read/write */
117 short sb_flags; /* flags, see below */
118 short sb_timeo; /* timeout for read/write */
120 #define SB_MAX (256*1024) /* default for max chars in sockbuf */
121 #define SB_LOCK 0x01 /* lock on data queue */
122 #define SB_WANT 0x02 /* someone is waiting to lock */
123 #define SB_WAIT 0x04 /* someone is waiting for data/space */
124 #define SB_SEL 0x08 /* someone is selecting */
125 #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
126 #define SB_UPCALL 0x20 /* someone wants an upcall */
127 #define SB_NOINTR 0x40 /* operations not interruptible */
128 #define SB_AIO 0x80 /* AIO operations queued */
129 #define SB_KNOTE 0x100 /* kernel note attached */
131 void (*so_upcall) __P((struct socket *, void *, int));
133 struct ucred *so_cred; /* user credentials */
134 /* NB: generation count must not be first; easiest to make it last. */
135 so_gen_t so_gencnt; /* generation count */
136 void *so_emuldata; /* private data for emulators */
138 struct accept_filter *so_accept_filter;
139 void *so_accept_filter_arg; /* saved filter args */
140 char *so_accept_filter_str; /* saved user args */
147 #define SS_NOFDREF 0x0001 /* no file table ref any more */
148 #define SS_ISCONNECTED 0x0002 /* socket connected to a peer */
149 #define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */
150 #define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */
151 #define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */
152 #define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */
153 #define SS_RCVATMARK 0x0040 /* at mark on input */
155 #define SS_NBIO 0x0100 /* non-blocking ops */
156 #define SS_ASYNC 0x0200 /* async i/o notify */
157 #define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */
159 #define SS_INCOMP 0x0800 /* unaccepted, incomplete connection */
160 #define SS_COMP 0x1000 /* unaccepted, complete connection */
161 #define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */
164 * Externalized form of struct socket used by the sysctl(3) interface.
167 size_t xso_len; /* length of this structure */
168 struct socket *xso_so; /* makes a convenient handle sometimes */
173 caddr_t so_pcb; /* another convenient handle */
192 uid_t so_uid; /* XXX */
196 * Macros for sockets and socket buffering.
200 * Do we need to notify the other side when I/O is possible?
202 #define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
203 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
206 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
207 * This is problematical if the fields are unsigned, as the space might
208 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
209 * overflow and return 0. Should use "lmin" but it doesn't exist now.
211 #define sbspace(sb) \
212 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
213 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
215 /* do we have to send all at once on a socket? */
216 #define sosendallatonce(so) \
217 ((so)->so_proto->pr_flags & PR_ATOMIC)
219 /* can we read something from so? */
220 #define soreadable(so) \
221 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
222 ((so)->so_state & SS_CANTRCVMORE) || \
223 (so)->so_comp.tqh_first || (so)->so_error)
225 /* can we write something to so? */
226 #define sowriteable(so) \
227 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
228 (((so)->so_state&SS_ISCONNECTED) || \
229 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
230 ((so)->so_state & SS_CANTSENDMORE) || \
233 /* adjust counters in sb reflecting allocation of m */
234 #define sballoc(sb, m) { \
235 (sb)->sb_cc += (m)->m_len; \
236 (sb)->sb_mbcnt += MSIZE; \
237 if ((m)->m_flags & M_EXT) \
238 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
241 /* adjust counters in sb reflecting freeing of m */
242 #define sbfree(sb, m) { \
243 (sb)->sb_cc -= (m)->m_len; \
244 (sb)->sb_mbcnt -= MSIZE; \
245 if ((m)->m_flags & M_EXT) \
246 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
250 * Set lock on sockbuf sb; sleep if lock is already held.
251 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
252 * Returns error without lock if sleep is interrupted.
254 #define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
255 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
256 ((sb)->sb_flags |= SB_LOCK), 0)
258 /* release lock on sockbuf sb */
259 #define sbunlock(sb) { \
260 (sb)->sb_flags &= ~SB_LOCK; \
261 if ((sb)->sb_flags & SB_WANT) { \
262 (sb)->sb_flags &= ~SB_WANT; \
263 wakeup((caddr_t)&(sb)->sb_flags); \
267 #define sorwakeup(so) do { \
268 if (sb_notify(&(so)->so_rcv)) \
269 sowakeup((so), &(so)->so_rcv); \
272 #define sowwakeup(so) do { \
273 if (sb_notify(&(so)->so_snd)) \
274 sowakeup((so), &(so)->so_snd); \
280 * Argument structure for sosetopt et seq. This is in the KERNEL
281 * section because it will never be visible to user code.
283 enum sopt_dir { SOPT_GET, SOPT_SET };
285 enum sopt_dir sopt_dir; /* is this a get or a set? */
286 int sopt_level; /* second arg of [gs]etsockopt */
287 int sopt_name; /* third arg of [gs]etsockopt */
288 void *sopt_val; /* fourth arg of [gs]etsockopt */
289 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */
290 struct proc *sopt_p; /* calling process or null if kernel */
294 SLIST_ENTRY(sf_buf) free_list; /* list of free buffer slots */
295 int refcnt; /* reference count */
296 struct vm_page *m; /* currently mapped page */
297 vm_offset_t kva; /* va of mapping */
300 struct accept_filter {
302 void (*accf_callback)
303 __P((struct socket *so, void *arg, int waitflag));
304 void * (*accf_create)
305 __P((struct socket *so, char *arg));
307 __P((struct socket *so));
308 SLIST_ENTRY(accept_filter) accf_next; /* next on the list */
311 #ifdef MALLOC_DECLARE
312 MALLOC_DECLARE(M_PCB);
313 MALLOC_DECLARE(M_SONAME);
314 MALLOC_DECLARE(M_ACCF);
317 extern int maxsockets;
318 extern u_long sb_max;
319 extern struct vm_zone *socket_zone;
320 extern so_gen_t so_gencnt;
332 * File operations on sockets.
334 int soo_read __P((struct file *fp, struct uio *uio, struct ucred *cred,
335 int flags, struct proc *p));
336 int soo_write __P((struct file *fp, struct uio *uio, struct ucred *cred,
337 int flags, struct proc *p));
338 int soo_close __P((struct file *fp, struct proc *p));
339 int soo_ioctl __P((struct file *fp, u_long cmd, caddr_t data,
341 int soo_poll __P((struct file *fp, int events, struct ucred *cred,
343 int soo_stat __P((struct file *fp, struct stat *ub, struct proc *p));
344 int sokqfilter __P((struct file *fp, struct knote *kn));
347 * From uipc_socket and friends
349 struct sockaddr *dup_sockaddr __P((struct sockaddr *sa, int canwait));
350 int holdsock __P((struct filedesc *fdp, int fdes, struct file **fpp));
351 int sockargs __P((struct mbuf **mp, caddr_t buf, int buflen, int type));
352 void sbappend __P((struct sockbuf *sb, struct mbuf *m));
353 int sbappendaddr __P((struct sockbuf *sb, struct sockaddr *asa,
354 struct mbuf *m0, struct mbuf *control));
355 int sbappendcontrol __P((struct sockbuf *sb, struct mbuf *m0,
356 struct mbuf *control));
357 void sbappendrecord __P((struct sockbuf *sb, struct mbuf *m0));
358 void sbcheck __P((struct sockbuf *sb));
359 void sbcompress __P((struct sockbuf *sb, struct mbuf *m, struct mbuf *n));
361 sbcreatecontrol __P((caddr_t p, int size, int type, int level));
362 void sbdrop __P((struct sockbuf *sb, int len));
363 void sbdroprecord __P((struct sockbuf *sb));
364 void sbflush __P((struct sockbuf *sb));
365 void sbinsertoob __P((struct sockbuf *sb, struct mbuf *m0));
366 void sbrelease __P((struct sockbuf *sb, struct socket *so));
367 int sbreserve __P((struct sockbuf *sb, u_long cc, struct socket *so,
369 void sbtoxsockbuf __P((struct sockbuf *sb, struct xsockbuf *xsb));
370 int sbwait __P((struct sockbuf *sb));
371 int sb_lock __P((struct sockbuf *sb));
372 int soabort __P((struct socket *so));
373 int soaccept __P((struct socket *so, struct sockaddr **nam));
374 struct socket *soalloc __P((int waitok));
375 int sobind __P((struct socket *so, struct sockaddr *nam, struct proc *p));
376 void socantrcvmore __P((struct socket *so));
377 void socantsendmore __P((struct socket *so));
378 int soclose __P((struct socket *so));
379 int soconnect __P((struct socket *so, struct sockaddr *nam, struct proc *p));
380 int soconnect2 __P((struct socket *so1, struct socket *so2));
381 int socreate __P((int dom, struct socket **aso, int type, int proto,
383 void sodealloc __P((struct socket *so));
384 int sodisconnect __P((struct socket *so));
385 void sofree __P((struct socket *so));
386 int sogetopt __P((struct socket *so, struct sockopt *sopt));
387 void sohasoutofband __P((struct socket *so));
388 void soisconnected __P((struct socket *so));
389 void soisconnecting __P((struct socket *so));
390 void soisdisconnected __P((struct socket *so));
391 void soisdisconnecting __P((struct socket *so));
392 int solisten __P((struct socket *so, int backlog, struct proc *p));
394 sodropablereq __P((struct socket *head));
396 sonewconn __P((struct socket *head, int connstatus));
398 sonewconn3 __P((struct socket *head, int connstatus, struct proc *p));
399 int sooptcopyin __P((struct sockopt *sopt, void *buf, size_t len,
401 int sooptcopyout __P((struct sockopt *sopt, void *buf, size_t len));
403 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
404 int soopt_getm __P((struct sockopt *sopt, struct mbuf **mp));
405 int soopt_mcopyin __P((struct sockopt *sopt, struct mbuf *m));
406 int soopt_mcopyout __P((struct sockopt *sopt, struct mbuf *m));
408 int sopoll __P((struct socket *so, int events, struct ucred *cred,
410 int soreceive __P((struct socket *so, struct sockaddr **paddr,
411 struct uio *uio, struct mbuf **mp0,
412 struct mbuf **controlp, int *flagsp));
413 int soreserve __P((struct socket *so, u_long sndcc, u_long rcvcc));
414 void sorflush __P((struct socket *so));
415 int sosend __P((struct socket *so, struct sockaddr *addr, struct uio *uio,
416 struct mbuf *top, struct mbuf *control, int flags,
418 int sosetopt __P((struct socket *so, struct sockopt *sopt));
419 int soshutdown __P((struct socket *so, int how));
420 void sotoxsocket __P((struct socket *so, struct xsocket *xso));
421 void sowakeup __P((struct socket *so, struct sockbuf *sb));
423 /* accept filter functions */
424 int accept_filt_add __P((struct accept_filter *filt));
425 int accept_filt_del __P((char *name));
426 struct accept_filter * accept_filt_get __P((char *name));
427 #ifdef ACCEPT_FILTER_MOD
428 int accept_filt_generic_mod_event __P((module_t mod, int event, void *data));
429 SYSCTL_DECL(_net_inet_accf);
430 #endif /* ACCEPT_FILTER_MOD */
434 #endif /* !_SYS_SOCKETVAR_H_ */