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
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.30 2007/11/07 18:24:04 dillon Exp $
38 #ifndef _SYS_SOCKETVAR_H_
39 #define _SYS_SOCKETVAR_H_
42 #include <sys/types.h>
45 #include <sys/queue.h> /* for TAILQ macros */
47 #ifndef _SYS_SELINFO_H_
48 #include <sys/selinfo.h> /* for struct selinfo */
50 #ifndef _SYS_SOCKBUF_H_
51 #include <sys/sockbuf.h>
54 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
59 * Signaling socket buffers contain additional elements for locking
60 * and signaling conditions. These are used primarily by sockets.
62 struct signalsockbuf
{
64 struct selinfo ssb_sel
; /* process selecting read/write */
65 short ssb_flags
; /* flags, see below */
66 short ssb_timeo
; /* timeout for read/write */
67 long ssb_lowat
; /* low water mark */
68 u_long ssb_hiwat
; /* high water mark / max actual char count */
69 u_long ssb_mbmax
; /* max chars of mbufs to use */
72 #define ssb_cc sb.sb_cc /* commonly used fields */
73 #define ssb_mb sb.sb_mb /* commonly used fields */
74 #define ssb_mbcnt sb.sb_mbcnt /* commonly used fields */
76 #define SSB_LOCK 0x01 /* lock on data queue */
77 #define SSB_WANT 0x02 /* someone is waiting to lock */
78 #define SSB_WAIT 0x04 /* someone is waiting for data/space */
79 #define SSB_SEL 0x08 /* someone is selecting */
80 #define SSB_ASYNC 0x10 /* ASYNC I/O, need signals */
81 #define SSB_UPCALL 0x20 /* someone wants an upcall */
82 #define SSB_NOINTR 0x40 /* operations not interruptible */
83 #define SSB_AIO 0x80 /* AIO operations queued */
84 #define SSB_KNOTE 0x100 /* kernel note attached */
85 #define SSB_MEVENT 0x200 /* need message event notification */
88 * Per-socket kernel structure. Contains universal send and receive queues,
89 * protocol control handle, and error information.
92 short so_type
; /* generic type, see socket.h */
93 short so_options
; /* from socket call, see socket.h */
94 short so_linger
; /* time to linger while closing */
95 short so_state
; /* internal state flags SS_*, below */
96 void *so_pcb
; /* protocol control block */
97 struct protosw
*so_proto
; /* protocol handle */
98 struct socket
*so_head
; /* back pointer to accept socket */
101 * These fields are used to manage sockets capable of accepting
104 TAILQ_HEAD(, socket
) so_incomp
; /* in-progress, incomplete */
105 TAILQ_HEAD(, socket
) so_comp
; /* completed but not yet accepted */
106 TAILQ_ENTRY(socket
) so_list
; /* list of unaccepted connections */
107 short so_qlen
; /* so_comp count */
108 short so_incqlen
; /* so_incomp count */
109 short so_qlimit
; /* max number queued connections */
112 * Misc socket support
114 short so_timeo
; /* connection timeout */
115 u_short so_error
; /* error affecting connection */
116 struct sigio
*so_sigio
; /* information for async I/O or
117 out of band data (SIGURG) */
118 u_long so_oobmark
; /* chars to oob mark */
119 TAILQ_HEAD(, aiocblist
) so_aiojobq
; /* AIO ops waiting on socket */
120 struct signalsockbuf so_rcv
;
121 struct signalsockbuf so_snd
;
123 void (*so_upcall
) (struct socket
*, void *, int);
125 struct ucred
*so_cred
; /* user credentials */
126 /* NB: generation count must not be first; easiest to make it last. */
127 void *so_emuldata
; /* private data for emulators */
129 struct accept_filter
*so_accept_filter
;
130 void *so_accept_filter_arg
; /* saved filter args */
131 char *so_accept_filter_str
; /* saved user args */
140 #define SS_NOFDREF 0x0001 /* no file table ref any more */
141 #define SS_ISCONNECTED 0x0002 /* socket connected to a peer */
142 #define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */
143 #define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */
144 #define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */
145 #define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */
146 #define SS_RCVATMARK 0x0040 /* at mark on input */
148 #define SS_UNUSED0100 0x0100
149 #define SS_ASYNC 0x0200 /* async i/o notify */
150 #define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */
152 #define SS_INCOMP 0x0800 /* unaccepted, incomplete connection */
153 #define SS_COMP 0x1000 /* unaccepted, complete connection */
154 #define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */
157 * Externalized form of struct socket used by the sysctl(3) interface.
160 size_t xso_len
; /* length of this structure */
161 struct socket
*xso_so
; /* makes a convenient handle sometimes */
166 void *so_pcb
; /* another convenient handle */
185 uid_t so_uid
; /* XXX */
189 * Macros for sockets and socket buffering.
192 #define sosendallatonce(so) \
193 ((so)->so_proto->pr_flags & PR_ATOMIC)
195 /* can we read something from so? */
196 #define soreadable(so) \
197 ((so)->so_rcv.ssb_cc >= (so)->so_rcv.ssb_lowat || \
198 ((so)->so_state & SS_CANTRCVMORE) || \
199 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
201 /* can we write something to so? */
202 #define sowriteable(so) \
203 ((ssb_space(&(so)->so_snd) >= (so)->so_snd.ssb_lowat && \
204 (((so)->so_state&SS_ISCONNECTED) || \
205 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
206 ((so)->so_state & SS_CANTSENDMORE) || \
210 * Do we need to notify the other side when I/O is possible?
212 #define ssb_notify(ssb) \
213 (((ssb)->ssb_flags & \
214 (SSB_WAIT | SSB_SEL | SSB_ASYNC | SSB_UPCALL | \
215 SSB_AIO | SSB_KNOTE | SSB_MEVENT)))
217 /* do we have to send all at once on a socket? */
220 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
221 * This is problematical if the fields are unsigned, as the space might
222 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
223 * overflow and return 0. Should use "lmin" but it doesn't exist now.
225 #define ssb_space(ssb) \
226 ((long)imin((int)((ssb)->ssb_hiwat - (ssb)->ssb_cc), \
227 (int)((ssb)->ssb_mbmax - (ssb)->ssb_mbcnt)))
229 #define ssb_append(ssb, m) \
230 sbappend(&(ssb)->sb, m)
232 #define ssb_appendstream(ssb, m) \
233 sbappendstream(&(ssb)->sb, m)
235 #define ssb_appendrecord(ssb, m) \
236 sbappendrecord(&(ssb)->sb, m)
238 #define ssb_appendaddr(ssb, src, m, control) \
239 ((ssb_space(ssb) <= 0) ? 0 : sbappendaddr(&(ssb)->sb, src, m, control))
241 #define ssb_appendcontrol(ssb, m, control) \
242 ((ssb_space(ssb) <= 0) ? 0 : sbappendcontrol(&(ssb)->sb, m, control))
244 #define ssb_insert_knote(ssb, kn) { \
245 SLIST_INSERT_HEAD(&(ssb)->ssb_sel.si_note, kn, kn_selnext); \
246 (ssb)->ssb_flags |= SSB_KNOTE; \
249 #define ssb_remove_knote(ssb, kn) { \
250 SLIST_REMOVE(&(ssb)->ssb_sel.si_note, kn, knote, kn_selnext); \
251 if (SLIST_EMPTY(&(ssb)->ssb_sel.si_note)) \
252 (ssb)->ssb_flags &= ~SSB_KNOTE; \
255 #define sorwakeup(so) do { \
256 if (ssb_notify(&(so)->so_rcv)) \
257 sowakeup((so), &(so)->so_rcv); \
260 #define sowwakeup(so) do { \
261 if (ssb_notify(&(so)->so_snd)) \
262 sowakeup((so), &(so)->so_snd); \
268 * Argument structure for sosetopt et seq. This is in the KERNEL
269 * section because it will never be visible to user code.
271 enum sopt_dir
{ SOPT_GET
, SOPT_SET
};
273 enum sopt_dir sopt_dir
; /* is this a get or a set? */
274 int sopt_level
; /* second arg of [gs]etsockopt */
275 int sopt_name
; /* third arg of [gs]etsockopt */
276 void *sopt_val
; /* fourth arg of [gs]etsockopt */
277 size_t sopt_valsize
; /* (almost) fifth arg of [gs]etsockopt */
278 struct thread
*sopt_td
; /* calling thread or null if kernel */
281 struct accept_filter
{
283 void (*accf_callback
)
284 (struct socket
*so
, void *arg
, int waitflag
);
285 void * (*accf_create
)
286 (struct socket
*so
, char *arg
);
289 SLIST_ENTRY(accept_filter
) accf_next
; /* next on the list */
292 #ifdef MALLOC_DECLARE
293 MALLOC_DECLARE(M_PCB
);
294 MALLOC_DECLARE(M_SONAME
);
295 MALLOC_DECLARE(M_ACCF
);
298 extern int maxsockets
;
299 extern u_long sb_max
; /* nominal limit */
300 extern u_long sb_max_adj
; /* actual limit used by sbreserve() */
301 extern struct vm_zone
*socket_zone
;
314 * File operations on sockets.
316 int soo_read (struct file
*fp
, struct uio
*uio
, struct ucred
*cred
,
318 int soo_write (struct file
*fp
, struct uio
*uio
, struct ucred
*cred
,
320 int soo_close (struct file
*fp
);
321 int soo_shutdown (struct file
*fp
, int how
);
322 int soo_ioctl (struct file
*fp
, u_long cmd
, caddr_t data
,
324 int soo_poll (struct file
*fp
, int events
, struct ucred
*cred
);
325 int soo_stat (struct file
*fp
, struct stat
*ub
, struct ucred
*cred
);
326 int sokqfilter (struct file
*fp
, struct knote
*kn
);
329 * From uipc_socket and friends
331 struct sockaddr
*dup_sockaddr (const struct sockaddr
*sa
);
332 int getsockaddr (struct sockaddr
**namp
, caddr_t uaddr
, size_t len
);
334 void ssb_release (struct signalsockbuf
*ssb
, struct socket
*so
);
335 int ssb_reserve (struct signalsockbuf
*ssb
, u_long cc
, struct socket
*so
,
337 void ssbtoxsockbuf (struct signalsockbuf
*sb
, struct xsockbuf
*xsb
);
338 int ssb_wait (struct signalsockbuf
*sb
);
339 int _ssb_lock (struct signalsockbuf
*sb
);
341 int soabort (struct socket
*so
);
342 int soaccept (struct socket
*so
, struct sockaddr
**nam
);
343 struct socket
*soalloc (int waitok
);
344 int sobind (struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
);
345 void socantrcvmore (struct socket
*so
);
346 void socantsendmore (struct socket
*so
);
347 int soclose (struct socket
*so
, int fflags
);
348 int soconnect (struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
);
349 int soconnect2 (struct socket
*so1
, struct socket
*so2
);
350 int socreate (int dom
, struct socket
**aso
, int type
, int proto
,
352 void sodealloc (struct socket
*so
);
353 int sodisconnect (struct socket
*so
);
354 void sofree (struct socket
*so
);
355 int sogetopt (struct socket
*so
, struct sockopt
*sopt
);
356 void sohasoutofband (struct socket
*so
);
357 void soisconnected (struct socket
*so
);
358 void soisconnecting (struct socket
*so
);
359 void soisdisconnected (struct socket
*so
);
360 void soisdisconnecting (struct socket
*so
);
361 int solisten (struct socket
*so
, int backlog
, struct thread
*td
);
362 struct socket
*sonewconn (struct socket
*head
, int connstatus
);
363 int sooptcopyin (struct sockopt
*sopt
, void *buf
, size_t len
,
365 int sooptcopyout (struct sockopt
*sopt
, const void *buf
, size_t len
);
367 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
368 int soopt_getm (struct sockopt
*sopt
, struct mbuf
**mp
);
369 int soopt_mcopyin (struct sockopt
*sopt
, struct mbuf
*m
);
370 int soopt_mcopyout (struct sockopt
*sopt
, struct mbuf
*m
);
372 int sopoll (struct socket
*so
, int events
, struct ucred
*cred
,
374 int soreceive (struct socket
*so
, struct sockaddr
**paddr
,
375 struct uio
*uio
, struct sockbuf
*sio
,
376 struct mbuf
**controlp
, int *flagsp
);
377 int soreserve (struct socket
*so
, u_long sndcc
, u_long rcvcc
,
379 void sorflush (struct socket
*so
);
380 int sosend (struct socket
*so
, struct sockaddr
*addr
, struct uio
*uio
,
381 struct mbuf
*top
, struct mbuf
*control
, int flags
,
383 int sosendudp (struct socket
*so
, struct sockaddr
*addr
, struct uio
*uio
,
384 struct mbuf
*top
, struct mbuf
*control
, int flags
,
386 int sosetopt (struct socket
*so
, struct sockopt
*sopt
);
387 int soshutdown (struct socket
*so
, int how
);
388 void sotoxsocket (struct socket
*so
, struct xsocket
*xso
);
389 void sowakeup (struct socket
*so
, struct signalsockbuf
*sb
);
391 /* accept filter functions */
392 int accept_filt_add (struct accept_filter
*filt
);
393 int accept_filt_del (char *name
);
394 struct accept_filter
* accept_filt_get (char *name
);
395 #ifdef ACCEPT_FILTER_MOD
396 int accept_filt_generic_mod_event (module_t mod
, int event
, void *data
);
397 SYSCTL_DECL(_net_inet_accf
);
398 #endif /* ACCEPT_FILTER_MOD */
402 #endif /* !_SYS_SOCKETVAR_H_ */