2 * Copyright (c) 1982, 1986, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
37 #ifndef _SYS_SOCKETVAR_H_
38 #define _SYS_SOCKETVAR_H_
40 #include <sys/queue.h> /* for TAILQ macros */
41 #include <sys/sx.h> /* SX locks */
42 #include <sys/selinfo.h> /* for struct selinfo */
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.
50 typedef u_quad_t so_gen_t;
55 struct vm_zone *so_zone; /* zone we were allocated from */
56 int so_count; /* reference count */
57 short so_type; /* generic type, see socket.h */
58 short so_options; /* from socket call, see socket.h */
59 short so_linger; /* time to linger while closing */
60 short so_state; /* internal state flags SS_*, below */
61 caddr_t so_pcb; /* protocol control block */
62 struct protosw *so_proto; /* protocol handle */
64 * Variables for connection queuing.
65 * Socket where accepts occur is so_head in all subsidiary sockets.
66 * If so_head is 0, socket is not related to an accept.
67 * For head socket so_incomp queues partially completed connections,
68 * while so_comp is a queue of connections ready to be accepted.
69 * If a connection is aborted and it has so_head set, then
70 * it has to be pulled out of either so_incomp or so_comp.
71 * We allow connections to queue up based on current queue lengths
72 * and limit on number of queued connections for this socket.
74 struct socket *so_head; /* back pointer to accept socket */
75 TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */
76 TAILQ_HEAD(, socket) so_comp; /* queue of complete unaccepted connections */
77 TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */
78 short so_qlen; /* number of unaccepted connections */
79 short so_incqlen; /* number of unaccepted incomplete
81 short so_qlimit; /* max number queued connections */
82 short so_timeo; /* connection timeout */
83 u_short so_error; /* error affecting connection */
84 struct sigio *so_sigio; /* information for async I/O or
85 out of band data (SIGURG) */
86 u_long so_oobmark; /* chars to oob mark */
87 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
89 * Variables for socket buffering.
92 u_long sb_cc; /* actual chars in buffer */
93 u_long sb_hiwat; /* max actual char count */
94 u_long sb_mbcnt; /* chars of mbufs used */
95 u_long sb_mbmax; /* max chars of mbufs to use */
96 long sb_lowat; /* low water mark */
97 struct mbuf *sb_mb; /* the mbuf chain */
98 struct selinfo sb_sel; /* process selecting read/write */
99 short sb_flags; /* flags, see below */
100 short sb_timeo; /* timeout for read/write */
102 #define SB_MAX (256*1024) /* default for max chars in sockbuf */
103 #define SB_LOCK 0x01 /* lock on data queue */
104 #define SB_WANT 0x02 /* someone is waiting to lock */
105 #define SB_WAIT 0x04 /* someone is waiting for data/space */
106 #define SB_SEL 0x08 /* someone is selecting */
107 #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
108 #define SB_UPCALL 0x20 /* someone wants an upcall */
109 #define SB_NOINTR 0x40 /* operations not interruptible */
110 #define SB_AIO 0x80 /* AIO operations queued */
111 #define SB_KNOTE 0x100 /* kernel note attached */
113 void (*so_upcall) __P((struct socket *, void *, int));
115 struct ucred *so_cred; /* user credentials */
116 /* NB: generation count must not be first; easiest to make it last. */
117 so_gen_t so_gencnt; /* generation count */
118 void *so_emuldata; /* private data for emulators */
120 struct accept_filter *so_accept_filter;
121 void *so_accept_filter_arg; /* saved filter args */
122 char *so_accept_filter_str; /* saved user args */
129 #define SS_NOFDREF 0x0001 /* no file table ref any more */
130 #define SS_ISCONNECTED 0x0002 /* socket connected to a peer */
131 #define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */
132 #define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */
133 #define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */
134 #define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */
135 #define SS_RCVATMARK 0x0040 /* at mark on input */
137 #define SS_NBIO 0x0100 /* non-blocking ops */
138 #define SS_ASYNC 0x0200 /* async i/o notify */
139 #define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */
141 #define SS_INCOMP 0x0800 /* unaccepted, incomplete connection */
142 #define SS_COMP 0x1000 /* unaccepted, complete connection */
143 #define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */
146 * Externalized form of struct socket used by the sysctl(3) interface.
149 size_t xso_len; /* length of this structure */
150 struct socket *xso_so; /* makes a convenient handle sometimes */
155 caddr_t so_pcb; /* another convenient handle */
174 uid_t so_uid; /* XXX */
178 * Macros for sockets and socket buffering.
182 * Do we need to notify the other side when I/O is possible?
184 #define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
185 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
188 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
189 * This is problematical if the fields are unsigned, as the space might
190 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
191 * overflow and return 0. Should use "lmin" but it doesn't exist now.
193 #define sbspace(sb) \
194 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
195 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
197 /* do we have to send all at once on a socket? */
198 #define sosendallatonce(so) \
199 ((so)->so_proto->pr_flags & PR_ATOMIC)
201 /* can we read something from so? */
202 #define soreadable(so) \
203 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
204 ((so)->so_state & SS_CANTRCVMORE) || \
205 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
207 /* can we write something to so? */
208 #define sowriteable(so) \
209 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
210 (((so)->so_state&SS_ISCONNECTED) || \
211 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
212 ((so)->so_state & SS_CANTSENDMORE) || \
215 /* adjust counters in sb reflecting allocation of m */
216 #define sballoc(sb, m) { \
217 (sb)->sb_cc += (m)->m_len; \
218 (sb)->sb_mbcnt += MSIZE; \
219 if ((m)->m_flags & M_EXT) \
220 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
223 /* adjust counters in sb reflecting freeing of m */
224 #define sbfree(sb, m) { \
225 (sb)->sb_cc -= (m)->m_len; \
226 (sb)->sb_mbcnt -= MSIZE; \
227 if ((m)->m_flags & M_EXT) \
228 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
232 * Set lock on sockbuf sb; sleep if lock is already held.
233 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
234 * Returns error without lock if sleep is interrupted.
236 #define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
237 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
238 ((sb)->sb_flags |= SB_LOCK), 0)
240 /* release lock on sockbuf sb */
241 #define sbunlock(sb) { \
242 (sb)->sb_flags &= ~SB_LOCK; \
243 if ((sb)->sb_flags & SB_WANT) { \
244 (sb)->sb_flags &= ~SB_WANT; \
245 wakeup((caddr_t)&(sb)->sb_flags); \
250 * soref()/sorele() ref-count the socket structure. Note that you must
251 * still explicitly close the socket, but the last ref count will free
255 #define soref(so) do { \
259 #define sorele(so) do { \
260 if ((so)->so_count <= 0) \
262 if (--(so)->so_count == 0)\
266 #define sotryfree(so) do { \
267 if ((so)->so_count == 0) \
271 #define sorwakeup(so) do { \
272 if (sb_notify(&(so)->so_rcv)) \
273 sowakeup((so), &(so)->so_rcv); \
276 #define sowwakeup(so) do { \
277 if (sb_notify(&(so)->so_snd)) \
278 sowakeup((so), &(so)->so_snd); \
284 * Argument structure for sosetopt et seq. This is in the KERNEL
285 * section because it will never be visible to user code.
287 enum sopt_dir { SOPT_GET, SOPT_SET };
289 enum sopt_dir sopt_dir; /* is this a get or a set? */
290 int sopt_level; /* second arg of [gs]etsockopt */
291 int sopt_name; /* third arg of [gs]etsockopt */
292 void *sopt_val; /* fourth arg of [gs]etsockopt */
293 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */
294 struct thread *sopt_td; /* calling thread or null if kernel */
298 SLIST_ENTRY(sf_buf) free_list; /* list of free buffer slots */
299 struct vm_page *m; /* currently mapped page */
300 vm_offset_t kva; /* va of mapping */
303 struct accept_filter {
305 void (*accf_callback)
306 __P((struct socket *so, void *arg, int waitflag));
307 void * (*accf_create)
308 __P((struct socket *so, char *arg));
310 __P((struct socket *so));
311 SLIST_ENTRY(accept_filter) accf_next; /* next on the list */
314 #ifdef MALLOC_DECLARE
315 MALLOC_DECLARE(M_PCB);
316 MALLOC_DECLARE(M_SONAME);
317 MALLOC_DECLARE(M_ACCF);
320 extern int maxsockets;
321 extern u_long sb_max;
322 extern struct vm_zone *socket_zone;
323 extern so_gen_t so_gencnt;
335 * File operations on sockets.
337 int soo_read __P((struct file *fp, struct uio *uio, struct ucred *cred,
338 int flags, struct thread *td));
339 int soo_write __P((struct file *fp, struct uio *uio, struct ucred *cred,
340 int flags, struct thread *td));
341 int soo_close __P((struct file *fp, struct thread *td));
342 int soo_ioctl __P((struct file *fp, u_long cmd, caddr_t data,
344 int soo_poll __P((struct file *fp, int events, struct ucred *cred,
346 int soo_stat __P((struct file *fp, struct stat *ub, struct thread *td));
347 int sokqfilter __P((struct file *fp, struct knote *kn));
350 * From uipc_socket and friends
352 struct sockaddr *dup_sockaddr __P((struct sockaddr *sa, int canwait));
353 int sockargs __P((struct mbuf **mp, caddr_t buf, int buflen, int type));
354 int getsockaddr __P((struct sockaddr **namp, caddr_t uaddr, size_t len));
355 void sbappend __P((struct sockbuf *sb, struct mbuf *m));
356 int sbappendaddr __P((struct sockbuf *sb, struct sockaddr *asa,
357 struct mbuf *m0, struct mbuf *control));
358 int sbappendcontrol __P((struct sockbuf *sb, struct mbuf *m0,
359 struct mbuf *control));
360 void sbappendrecord __P((struct sockbuf *sb, struct mbuf *m0));
361 void sbcheck __P((struct sockbuf *sb));
362 void sbcompress __P((struct sockbuf *sb, struct mbuf *m, struct mbuf *n));
364 sbcreatecontrol __P((caddr_t p, int size, int type, int level));
365 void sbdrop __P((struct sockbuf *sb, int len));
366 void sbdroprecord __P((struct sockbuf *sb));
367 void sbflush __P((struct sockbuf *sb));
368 void sbinsertoob __P((struct sockbuf *sb, struct mbuf *m0));
369 void sbrelease __P((struct sockbuf *sb, struct socket *so));
370 int sbreserve __P((struct sockbuf *sb, u_long cc, struct socket *so,
372 void sbtoxsockbuf __P((struct sockbuf *sb, struct xsockbuf *xsb));
373 int sbwait __P((struct sockbuf *sb));
374 int sb_lock __P((struct sockbuf *sb));
375 int soabort __P((struct socket *so));
376 int soaccept __P((struct socket *so, struct sockaddr **nam));
377 struct socket *soalloc __P((int waitok));
378 int sobind __P((struct socket *so, struct sockaddr *nam, struct thread *td));
379 void socantrcvmore __P((struct socket *so));
380 void socantsendmore __P((struct socket *so));
381 int soclose __P((struct socket *so));
382 int soconnect __P((struct socket *so, struct sockaddr *nam, struct thread *td));
383 int soconnect2 __P((struct socket *so1, struct socket *so2));
384 int socreate __P((int dom, struct socket **aso, int type, int proto,
385 struct ucred *cred, struct thread *td));
386 int sodisconnect __P((struct socket *so));
387 void sofree __P((struct socket *so));
388 int sogetopt __P((struct socket *so, struct sockopt *sopt));
389 void sohasoutofband __P((struct socket *so));
390 void soisconnected __P((struct socket *so));
391 void soisconnecting __P((struct socket *so));
392 void soisdisconnected __P((struct socket *so));
393 void soisdisconnecting __P((struct socket *so));
394 int solisten __P((struct socket *so, int backlog, struct thread *td));
396 sodropablereq __P((struct socket *head));
398 sonewconn __P((struct socket *head, int connstatus));
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 */
432 int socheckuid __P((struct socket *so, uid_t uid));
433 int socheckproc __P((struct socket *so, struct proc *p));
437 #endif /* !_SYS_SOCKETVAR_H_ */