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.35 2008/08/28 23:15:45 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 */
48 #include <sys/event.h> /* for struct kqinfo */
50 #ifndef _SYS_THREAD_H_
51 #include <sys/thread.h> /* for struct lwkt_token */
53 #ifndef _SYS_SOCKBUF_H_
54 #include <sys/sockbuf.h>
57 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
59 #ifndef _NET_NETMSG_H_
60 #include <net/netmsg.h>
63 #ifndef _SYS_SPINLOCK_H_
64 #include <sys/spinlock.h>
70 * Signaling socket buffers contain additional elements for locking
71 * and signaling conditions. These are used primarily by sockets.
73 * WARNING: See partial clearing of fields in kern/uipc_socket.c
74 * sorflush() and sowflush().
76 struct signalsockbuf {
78 struct kqinfo ssb_kq; /* process selecting read/write */
79 uint32_t ssb_flags; /* flags, see below (use atomic ops) */
80 u_int ssb_timeo; /* timeout for read/write */
81 long ssb_lowat; /* low water mark */
82 u_long ssb_hiwat; /* high water mark / max actual char count */
83 u_long ssb_mbmax; /* max chars of mbufs to use */
84 struct lwkt_token ssb_token; /* frontend/backend serializer */
87 #define ssb_cc sb.sb_cc /* commonly used fields */
88 #define ssb_mb sb.sb_mb /* commonly used fields */
89 #define ssb_mbcnt sb.sb_mbcnt /* commonly used fields */
90 #define ssb_cc_prealloc sb.sb_cc_prealloc
91 #define ssb_mbcnt_prealloc sb.sb_mbcnt_prealloc
93 #define SSB_LOCK 0x0001 /* lock on data queue */
94 #define SSB_WANT 0x0002 /* someone is waiting to lock */
95 #define SSB_WAIT 0x0004 /* someone is waiting for data/space */
96 #define SSB_ASYNC 0x0010 /* ASYNC I/O, need signals */
97 #define SSB_UPCALL 0x0020 /* someone wants an upcall */
98 #define SSB_NOINTR 0x0040 /* operations not interruptible */
99 /*#define SSB_AIO 0x0080*/ /* AIO operations queued */
100 #define SSB_KNOTE 0x0100 /* kernel note attached */
101 #define SSB_MEVENT 0x0200 /* need message event notification */
102 #define SSB_STOP 0x0400 /* backpressure indicator */
103 #define SSB_AUTOSIZE 0x0800 /* automatically size socket buffer */
104 #define SSB_AUTOLOWAT 0x1000 /* automatically scale lowat */
105 #define SSB_WAKEUP 0x2000 /* wakeup event race */
106 #define SSB_PREALLOC 0x4000 /* prealloc supported */
107 #define SSB_STOPSUPP 0x8000 /* SSB_STOP supported */
109 #define SSB_CLEAR_MASK (SSB_ASYNC | SSB_UPCALL | SSB_STOP | \
110 SSB_AUTOSIZE | SSB_AUTOLOWAT)
112 #define SSB_NOTIFY_MASK (SSB_WAIT | SSB_ASYNC | SSB_UPCALL | \
113 SSB_KNOTE | SSB_MEVENT)
116 * Per-socket kernel structure. Contains universal send and receive queues,
117 * protocol control handle, and error information.
120 short so_type; /* generic type, see socket.h */
121 short so_options; /* from socket call, see socket.h */
122 short so_linger; /* time to linger while closing */
123 short so_state; /* internal state flags SS_*, below */
124 void *so_pcb; /* protocol control block */
125 struct protosw *so_proto; /* protocol handle */
126 struct socket *so_head; /* back pointer to accept socket */
127 lwkt_port_t so_port; /* message port */
130 * These fields are used to manage sockets capable of accepting
133 TAILQ_HEAD(, socket) so_incomp; /* in-progress, incomplete */
134 TAILQ_HEAD(, socket) so_comp; /* completed but not yet accepted */
135 TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */
136 short so_qlen; /* so_comp count */
137 short so_incqlen; /* so_incomp count */
138 short so_qlimit; /* max number queued connections */
141 * Misc socket support
143 short so_timeo; /* connection timeout */
144 u_short so_error; /* error affecting connection */
145 struct sigio *so_sigio; /* information for async I/O or
146 out of band data (SIGURG) */
147 u_long so_oobmark; /* chars to oob mark */
148 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
149 struct signalsockbuf so_rcv;
150 struct signalsockbuf so_snd;
152 void (*so_upcall) (struct socket *, void *, int);
154 struct ucred *so_cred; /* user credentials */
155 /* NB: generation count must not be first; easiest to make it last. */
156 void *so_emuldata; /* private data for emulators */
157 int so_refs; /* shutdown refs */
159 struct accept_filter *so_accept_filter;
160 void *so_accept_filter_arg; /* saved filter args */
161 char *so_accept_filter_str; /* saved user args */
164 struct netmsg_base so_clomsg;
165 struct sockaddr *so_faddr;
167 struct spinlock so_rcvd_spin;
168 struct netmsg_pru_rcvd so_rcvd_msg;
170 lwkt_port_t so_orig_port;
178 * NOTE: The following states are interlocked with so_refs:
180 * SS_NOFDREF so_refs while not set
181 * (so_pcb) so_refs while set
183 #define SS_NOFDREF 0x0001 /* no file table ref any more */
184 #define SS_ISCONNECTED 0x0002 /* socket connected to a peer */
185 #define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */
186 #define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */
187 #define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */
188 #define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */
189 #define SS_RCVATMARK 0x0040 /* at mark on input */
191 #define SS_ISCLOSING 0x0080 /* in process of closing */
192 #define SS_ASSERTINPROG 0x0100 /* sonewconn race debugging */
193 #define SS_ASYNC 0x0200 /* async i/o notify */
194 #define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */
196 #define SS_INCOMP 0x0800 /* unaccepted, incomplete connection */
197 #define SS_COMP 0x1000 /* unaccepted, complete connection */
198 #define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */
201 * Externalized form of struct socket used by the sysctl(3) interface.
204 size_t xso_len; /* length of this structure */
205 struct socket *xso_so; /* makes a convenient handle sometimes */
210 void *so_pcb; /* another convenient handle */
229 uid_t so_uid; /* XXX */
233 * Macros for sockets and socket buffering.
236 #define sosendallatonce(so) \
237 ((so)->so_proto->pr_flags & PR_ATOMIC)
239 /* can we read something from so? */
240 #define soreadable(so) \
241 ((so)->so_rcv.ssb_cc >= (so)->so_rcv.ssb_lowat || \
242 ((so)->so_state & SS_CANTRCVMORE) || \
243 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
245 /* can we write something to so? */
246 #define sowriteable(so) \
247 ((ssb_space(&(so)->so_snd) >= (so)->so_snd.ssb_lowat && \
248 (((so)->so_state&SS_ISCONNECTED) || \
249 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
250 ((so)->so_state & SS_CANTSENDMORE) || \
253 /* do we have to send all at once on a socket? */
258 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
259 * This is problematical if the fields are unsigned, as the space might
260 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
261 * overflow and return 0.
263 * SSB_STOP ignores cc/hiwat and returns 0. This is used by unix domain
264 * stream sockets to signal backpressure.
268 ssb_space(struct signalsockbuf *ssb)
273 if (ssb->ssb_flags & SSB_STOP)
275 bleft = ssb->ssb_hiwat - ssb->ssb_cc;
276 mleft = ssb->ssb_mbmax - ssb->ssb_mbcnt;
277 return((bleft < mleft) ? bleft : mleft);
281 ssb_space_prealloc(struct signalsockbuf *ssb)
283 long bleft, bleft_prealloc;
284 long mleft, mleft_prealloc;
286 if (ssb->ssb_flags & SSB_STOP)
289 bleft = ssb->ssb_hiwat - ssb->ssb_cc;
290 bleft_prealloc = ssb->ssb_hiwat - ssb->ssb_cc_prealloc;
291 if (bleft_prealloc < bleft)
292 bleft = bleft_prealloc;
294 mleft = ssb->ssb_mbmax - ssb->ssb_mbcnt;
295 mleft_prealloc = ssb->ssb_mbmax - ssb->ssb_mbcnt_prealloc;
296 if (mleft_prealloc < mleft)
297 mleft = mleft_prealloc;
299 return((bleft < mleft) ? bleft : mleft);
303 * NOTE: Only works w/ later ssb_appendstream() on m
306 ssb_preallocstream(struct signalsockbuf *ssb, struct mbuf *m)
310 sbprealloc(&ssb->sb, m);
315 #define ssb_append(ssb, m) \
316 sbappend(&(ssb)->sb, m)
318 #define ssb_appendstream(ssb, m) \
319 sbappendstream(&(ssb)->sb, m)
321 #define ssb_appendrecord(ssb, m) \
322 sbappendrecord(&(ssb)->sb, m)
324 #define ssb_appendaddr(ssb, src, m, control) \
325 ((ssb_space(ssb) <= 0) ? 0 : sbappendaddr(&(ssb)->sb, src, m, control))
327 #define ssb_appendcontrol(ssb, m, control) \
328 ((ssb_space(ssb) <= 0) ? 0 : sbappendcontrol(&(ssb)->sb, m, control))
330 #define ssb_insert_knote(ssb, kn) { \
331 knote_insert(&(ssb)->ssb_kq.ki_note, kn); \
332 atomic_set_int(&(ssb)->ssb_flags, SSB_KNOTE); \
335 #define ssb_remove_knote(ssb, kn) { \
336 knote_remove(&(ssb)->ssb_kq.ki_note, kn); \
337 if (SLIST_EMPTY(&(ssb)->ssb_kq.ki_note)) \
338 atomic_clear_int(&(ssb)->ssb_flags, SSB_KNOTE); \
341 #define sorwakeup(so) sowakeup((so), &(so)->so_rcv)
342 #define sowwakeup(so) sowakeup((so), &(so)->so_snd)
347 * Argument structure for sosetopt et seq. This is in the KERNEL
348 * section because it will never be visible to user code.
350 enum sopt_dir { SOPT_GET, SOPT_SET };
352 enum sopt_dir sopt_dir; /* is this a get or a set? */
353 int sopt_level; /* second arg of [gs]etsockopt */
354 int sopt_name; /* third arg of [gs]etsockopt */
355 void *sopt_val; /* fourth arg of [gs]etsockopt */
356 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */
357 struct thread *sopt_td; /* calling thread or null if kernel */
360 struct accept_filter {
362 void (*accf_callback)
363 (struct socket *so, void *arg, int waitflag);
364 void * (*accf_create)
365 (struct socket *so, char *arg);
368 SLIST_ENTRY(accept_filter) accf_next; /* next on the list */
371 #ifdef MALLOC_DECLARE
372 MALLOC_DECLARE(M_PCB);
373 MALLOC_DECLARE(M_SONAME);
374 MALLOC_DECLARE(M_ACCF);
377 extern int maxsockets;
378 extern u_long sb_max; /* nominal limit */
379 extern u_long sb_max_adj; /* actual limit used by sbreserve() */
393 * File operations on sockets.
395 int soo_read (struct file *fp, struct uio *uio, struct ucred *cred,
397 int soo_write (struct file *fp, struct uio *uio, struct ucred *cred,
399 int soo_close (struct file *fp);
400 int soo_shutdown (struct file *fp, int how);
401 int soo_ioctl (struct file *fp, u_long cmd, caddr_t data,
402 struct ucred *cred, struct sysmsg *msg);
403 int soo_stat (struct file *fp, struct stat *ub, struct ucred *cred);
404 int sokqfilter (struct file *fp, struct knote *kn);
407 * From uipc_socket and friends
409 struct sockaddr *dup_sockaddr (const struct sockaddr *sa);
410 int getsockaddr (struct sockaddr **namp, caddr_t uaddr, size_t len);
412 void ssb_release (struct signalsockbuf *ssb, struct socket *so);
413 int ssb_reserve (struct signalsockbuf *ssb, u_long cc, struct socket *so,
415 void ssbtoxsockbuf (struct signalsockbuf *sb, struct xsockbuf *xsb);
416 int ssb_wait (struct signalsockbuf *sb);
417 int _ssb_lock (struct signalsockbuf *sb);
419 void soabort_async (struct socket *so, boolean_t clr_head);
420 void soabort_direct (struct socket *so);
421 int soaccept (struct socket *so, struct sockaddr **nam);
422 void soaccept_generic (struct socket *so);
423 struct socket *soalloc (int waitok, struct protosw *);
424 int sobind (struct socket *so, struct sockaddr *nam, struct thread *td);
425 void socantrcvmore (struct socket *so);
426 void socantsendmore (struct socket *so);
427 int socket_wait (struct socket *so, struct timespec *ts, int *res);
428 int soclose (struct socket *so, int fflags);
429 int soconnect (struct socket *so, struct sockaddr *nam, struct thread *td,
431 int soconnect2 (struct socket *so1, struct socket *so2);
432 int socreate (int dom, struct socket **aso, int type, int proto,
434 int sodisconnect (struct socket *so);
435 void sodiscard (struct socket *so);
436 void sofree (struct socket *so);
437 int sogetopt (struct socket *so, struct sockopt *sopt);
438 void sohasoutofband (struct socket *so);
439 void soisconnected (struct socket *so);
440 void soisconnecting (struct socket *so);
441 void soisdisconnected (struct socket *so);
442 void soisdisconnecting (struct socket *so);
443 void soisreconnected (struct socket *so);
444 void soisreconnecting (struct socket *so);
445 void sosetport (struct socket *so, struct lwkt_port *port);
446 int solisten (struct socket *so, int backlog, struct thread *td);
447 struct socket *sonewconn (struct socket *head, int connstatus);
448 struct socket *sonewconn_faddr (struct socket *head, int connstatus,
449 const struct sockaddr *faddr, boolean_t keep_ref);
450 void soinherit(struct socket *so, struct socket *so_inh);
451 int sooptcopyin (struct sockopt *sopt, void *buf, size_t len,
453 int soopt_to_kbuf (struct sockopt *sopt, void *buf, size_t len,
455 int sooptcopyout (struct sockopt *sopt, const void *buf, size_t len);
456 void soopt_from_kbuf (struct sockopt *sopt, const void *buf, size_t len);
458 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
459 int soopt_getm (struct sockopt *sopt, struct mbuf **mp);
460 int soopt_mcopyin (struct sockopt *sopt, struct mbuf *m);
461 void soopt_to_mbuf (struct sockopt *sopt, struct mbuf *m);
462 int soopt_mcopyout (struct sockopt *sopt, struct mbuf *m);
463 int soopt_from_mbuf (struct sockopt *sopt, struct mbuf *m);
465 int soreceive (struct socket *so, struct sockaddr **paddr,
466 struct uio *uio, struct sockbuf *sio,
467 struct mbuf **controlp, int *flagsp);
468 int sorecvtcp (struct socket *so, struct sockaddr **paddr,
469 struct uio *uio, struct sockbuf *sio,
470 struct mbuf **controlp, int *flagsp);
471 int soreserve (struct socket *so, u_long sndcc, u_long rcvcc,
473 void sorflush (struct socket *so);
474 int sosend (struct socket *so, struct sockaddr *addr, struct uio *uio,
475 struct mbuf *top, struct mbuf *control, int flags,
477 int sosendudp (struct socket *so, struct sockaddr *addr, struct uio *uio,
478 struct mbuf *top, struct mbuf *control, int flags,
480 int sosendtcp (struct socket *so, struct sockaddr *addr, struct uio *uio,
481 struct mbuf *top, struct mbuf *control, int flags,
483 int sosetopt (struct socket *so, struct sockopt *sopt);
484 int soshutdown (struct socket *so, int how);
485 void sotoxsocket (struct socket *so, struct xsocket *xso);
486 void sowakeup (struct socket *so, struct signalsockbuf *sb);
488 /* accept filter functions */
489 int accept_filt_add (struct accept_filter *filt);
490 int accept_filt_del (char *name);
491 struct accept_filter * accept_filt_get (char *name);
492 #ifdef ACCEPT_FILTER_MOD
493 int accept_filt_generic_mod_event (module_t mod, int event, void *data);
494 SYSCTL_DECL(_net_inet_accf);
495 #endif /* ACCEPT_FILTER_MOD */
499 #endif /* !_SYS_SOCKETVAR_H_ */