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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
34 #ifndef _SYS_SOCKETVAR_H_
35 #define _SYS_SOCKETVAR_H_
37 #include <sys/queue.h> /* for TAILQ macros */
38 #include <sys/selinfo.h> /* for struct selinfo */
39 #include <sys/_lock.h>
40 #include <sys/_mutex.h>
43 #include <sys/sockbuf.h>
44 #include <sys/sockstate.h>
46 #include <sys/caprights.h>
47 #include <sys/sockopt.h>
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.
58 typedef u_quad_t so_gen_t;
63 * Locking key to struct socket:
64 * (a) constant after allocation, no locking required.
65 * (b) locked by SOCK_LOCK(so).
66 * (c) locked by SOCKBUF_LOCK(&so->so_rcv).
67 * (d) locked by SOCKBUF_LOCK(&so->so_snd).
68 * (e) locked by ACCEPT_LOCK().
69 * (f) not locked since integer reads/writes are atomic.
70 * (g) used only as a sleep/wakeup address, no value.
71 * (h) locked by global mutex so_global_mtx.
74 int so_count; /* (b) reference count */
75 short so_type; /* (a) 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; /* (b) internal state flags SS_* */
79 int so_qstate; /* (e) internal state flags SQ_* */
80 void *so_pcb; /* protocol control block */
81 struct vnet *so_vnet; /* network stack instance */
82 struct protosw *so_proto; /* (a) protocol handle */
84 * Variables for connection queuing.
85 * Socket where accepts occur is so_head in all subsidiary sockets.
86 * If so_head is 0, socket is not related to an accept.
87 * For head socket so_incomp queues partially completed connections,
88 * while so_comp is a queue of connections ready to be accepted.
89 * If a connection is aborted and it has so_head set, then
90 * it has to be pulled out of either so_incomp or so_comp.
91 * We allow connections to queue up based on current queue lengths
92 * and limit on number of queued connections for this socket.
94 struct socket *so_head; /* (e) back pointer to listen socket */
95 TAILQ_HEAD(, socket) so_incomp; /* (e) queue of partial unaccepted connections */
96 TAILQ_HEAD(, socket) so_comp; /* (e) queue of complete unaccepted connections */
97 TAILQ_ENTRY(socket) so_list; /* (e) list of unaccepted connections */
98 u_short so_qlen; /* (e) number of unaccepted connections */
99 u_short so_incqlen; /* (e) number of unaccepted incomplete
101 u_short so_qlimit; /* (e) max number queued connections */
102 short so_timeo; /* (g) connection timeout */
103 u_short so_error; /* (f) error affecting connection */
104 struct sigio *so_sigio; /* [sg] information for async I/O or
105 out of band data (SIGURG) */
106 u_long so_oobmark; /* (c) chars to oob mark */
107 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
109 struct sockbuf so_rcv, so_snd;
111 struct ucred *so_cred; /* (a) user credentials */
112 struct label *so_label; /* (b) MAC label for socket */
113 struct label *so_peerlabel; /* (b) cached MAC label for peer */
114 /* NB: generation count must not be first. */
115 so_gen_t so_gencnt; /* (h) generation count */
116 void *so_emuldata; /* (b) private data for emulators */
118 struct accept_filter *so_accept_filter;
119 void *so_accept_filter_arg; /* saved filter args */
120 char *so_accept_filter_str; /* saved user args */
122 struct osd osd; /* Object Specific extensions */
124 * so_fibnum, so_user_cookie and friends can be used to attach
125 * some user-specified metadata to a socket, which then can be
126 * used by the kernel for various actions.
127 * so_user_cookie is used by ipfw/dummynet.
129 int so_fibnum; /* routing domain for this socket */
130 uint32_t so_user_cookie;
134 * Global accept mutex to serialize access to accept queues and
135 * fields associated with multiple sockets. This allows us to
136 * avoid defining a lock order between listen and accept sockets
137 * until such time as it proves to be a good idea.
139 extern struct mtx accept_mtx;
140 #define ACCEPT_LOCK_ASSERT() mtx_assert(&accept_mtx, MA_OWNED)
141 #define ACCEPT_UNLOCK_ASSERT() mtx_assert(&accept_mtx, MA_NOTOWNED)
142 #define ACCEPT_LOCK() mtx_lock(&accept_mtx)
143 #define ACCEPT_UNLOCK() mtx_unlock(&accept_mtx)
146 * Per-socket mutex: we reuse the receive socket buffer mutex for space
147 * efficiency. This decision should probably be revisited as we optimize
148 * locking for the socket code.
150 #define SOCK_MTX(_so) SOCKBUF_MTX(&(_so)->so_rcv)
151 #define SOCK_LOCK(_so) SOCKBUF_LOCK(&(_so)->so_rcv)
152 #define SOCK_OWNED(_so) SOCKBUF_OWNED(&(_so)->so_rcv)
153 #define SOCK_UNLOCK(_so) SOCKBUF_UNLOCK(&(_so)->so_rcv)
154 #define SOCK_LOCK_ASSERT(_so) SOCKBUF_LOCK_ASSERT(&(_so)->so_rcv)
157 * Socket state bits stored in so_qstate.
159 #define SQ_INCOMP 0x0800 /* unaccepted, incomplete connection */
160 #define SQ_COMP 0x1000 /* unaccepted, complete connection */
163 * Externalized form of struct socket used by the sysctl(3) interface.
166 size_t xso_len; /* length of this structure */
167 struct socket *xso_so; /* makes a convenient handle sometimes */
172 caddr_t so_pcb; /* another convenient handle */
182 struct xsockbuf so_rcv, so_snd;
183 uid_t so_uid; /* XXX */
189 * Macros for sockets and socket buffering.
195 #define SBL_WAIT 0x00000001 /* Wait if not immediately available. */
196 #define SBL_NOINTR 0x00000002 /* Force non-interruptible sleep. */
197 #define SBL_VALID (SBL_WAIT | SBL_NOINTR)
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)
205 /* do we have to send all at once on a socket? */
206 #define sosendallatonce(so) \
207 ((so)->so_proto->pr_flags & PR_ATOMIC)
209 /* can we read something from so? */
210 #define soreadabledata(so) \
211 (sbavail(&(so)->so_rcv) >= (so)->so_rcv.sb_lowat || \
212 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
213 #define soreadable(so) \
214 (soreadabledata(so) || ((so)->so_rcv.sb_state & SBS_CANTRCVMORE))
216 /* can we write something to so? */
217 #define sowriteable(so) \
218 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
219 (((so)->so_state&SS_ISCONNECTED) || \
220 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
221 ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
225 * soref()/sorele() ref-count the socket structure. Note that you must
226 * still explicitly close the socket, but the last ref count will free
229 #define soref(so) do { \
230 SOCK_LOCK_ASSERT(so); \
234 #define sorele(so) do { \
235 ACCEPT_LOCK_ASSERT(); \
236 SOCK_LOCK_ASSERT(so); \
237 if ((so)->so_count <= 0) \
239 if (--(so)->so_count == 0) \
248 * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to
249 * avoid a non-atomic test-and-wakeup. However, sowakeup is
250 * responsible for releasing the lock if it is called. We unlock only
251 * if we don't call into sowakeup. If any code is introduced that
252 * directly invokes the underlying sowakeup() primitives, it must
253 * maintain the same semantics.
255 #define sorwakeup_locked(so) do { \
256 SOCKBUF_LOCK_ASSERT(&(so)->so_rcv); \
257 if (sb_notify(&(so)->so_rcv)) \
258 sowakeup((so), &(so)->so_rcv); \
260 SOCKBUF_UNLOCK(&(so)->so_rcv); \
263 #define sorwakeup(so) do { \
264 SOCKBUF_LOCK(&(so)->so_rcv); \
265 sorwakeup_locked(so); \
268 #define sowwakeup_locked(so) do { \
269 SOCKBUF_LOCK_ASSERT(&(so)->so_snd); \
270 if (sb_notify(&(so)->so_snd)) \
271 sowakeup((so), &(so)->so_snd); \
273 SOCKBUF_UNLOCK(&(so)->so_snd); \
276 #define sowwakeup(so) do { \
277 SOCKBUF_LOCK(&(so)->so_snd); \
278 sowwakeup_locked(so); \
281 struct accept_filter {
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;
292 #ifdef MALLOC_DECLARE
293 MALLOC_DECLARE(M_ACCF);
294 MALLOC_DECLARE(M_PCB);
295 MALLOC_DECLARE(M_SONAME);
299 * Socket specific helper hook point identifiers
300 * Do not leave holes in the sequence, hook registration is a loop.
302 #define HHOOK_SOCKET_OPT 0
303 #define HHOOK_SOCKET_CREATE 1
304 #define HHOOK_SOCKET_RCV 2
305 #define HHOOK_SOCKET_SND 3
306 #define HHOOK_FILT_SOREAD 4
307 #define HHOOK_FILT_SOWRITE 5
308 #define HHOOK_SOCKET_CLOSE 6
309 #define HHOOK_SOCKET_LAST HHOOK_SOCKET_CLOSE
311 struct socket_hhook_data {
314 void *hctx; /* hook point specific data*/
318 extern int maxsockets;
319 extern u_long sb_max;
320 extern so_gen_t so_gencnt;
329 /* 'which' values for socket upcalls. */
333 /* Return values for socket upcalls. */
335 #define SU_ISCONNECTED 1
338 * From uipc_socket and friends
340 int sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type);
341 int getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len);
342 int getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
343 struct file **fpp, u_int *fflagp);
344 void soabort(struct socket *so);
345 int soaccept(struct socket *so, struct sockaddr **nam);
346 int socheckuid(struct socket *so, uid_t uid);
347 int sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
348 int sobindat(int fd, struct socket *so, struct sockaddr *nam,
350 int soclose(struct socket *so);
351 int soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
352 int soconnectat(int fd, struct socket *so, struct sockaddr *nam,
354 int soconnect2(struct socket *so1, struct socket *so2);
355 int socreate(int dom, struct socket **aso, int type, int proto,
356 struct ucred *cred, struct thread *td);
357 int sodisconnect(struct socket *so);
358 struct sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags);
359 void sofree(struct socket *so);
360 void sohasoutofband(struct socket *so);
361 int solisten(struct socket *so, int backlog, struct thread *td);
362 void solisten_proto(struct socket *so, int backlog);
363 int solisten_proto_check(struct socket *so);
365 sonewconn(struct socket *head, int connstatus);
368 int sopoll(struct socket *so, int events, struct ucred *active_cred,
370 int sopoll_generic(struct socket *so, int events,
371 struct ucred *active_cred, struct thread *td);
372 int soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
373 struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
374 int soreceive_stream(struct socket *so, struct sockaddr **paddr,
375 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
377 int soreceive_dgram(struct socket *so, struct sockaddr **paddr,
378 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
380 int soreceive_generic(struct socket *so, struct sockaddr **paddr,
381 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
383 int soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
384 void sorflush(struct socket *so);
385 int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
386 struct mbuf *top, struct mbuf *control, int flags,
388 int sosend_dgram(struct socket *so, struct sockaddr *addr,
389 struct uio *uio, struct mbuf *top, struct mbuf *control,
390 int flags, struct thread *td);
391 int sosend_generic(struct socket *so, struct sockaddr *addr,
392 struct uio *uio, struct mbuf *top, struct mbuf *control,
393 int flags, struct thread *td);
394 int soshutdown(struct socket *so, int how);
395 void sotoxsocket(struct socket *so, struct xsocket *xso);
396 void soupcall_clear(struct socket *so, int which);
397 void soupcall_set(struct socket *so, int which,
398 int (*func)(struct socket *, void *, int), void *arg);
399 void sowakeup(struct socket *so, struct sockbuf *sb);
400 int selsocket(struct socket *so, int events, struct timeval *tv,
404 * Accept filter functions (duh).
406 int accept_filt_add(struct accept_filter *filt);
407 int accept_filt_del(char *name);
408 struct accept_filter *accept_filt_get(char *name);
409 #ifdef ACCEPT_FILTER_MOD
411 SYSCTL_DECL(_net_inet_accf);
413 int accept_filt_generic_mod_event(module_t mod, int event, void *data);
418 #endif /* !_SYS_SOCKETVAR_H_ */