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. 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 uint64_t so_gen_t;
59 typedef int so_upcall_t(struct socket *, void *, int);
64 * Locking key to struct socket:
65 * (a) constant after allocation, no locking required.
66 * (b) locked by SOCK_LOCK(so).
67 * (cr) locked by SOCKBUF_LOCK(&so->so_rcv).
68 * (cs) locked by SOCKBUF_LOCK(&so->so_rcv).
69 * (e) locked by SOLISTEN_LOCK() of corresponding listening socket.
70 * (f) not locked since integer reads/writes are atomic.
71 * (g) used only as a sleep/wakeup address, no value.
72 * (h) locked by global mutex so_global_mtx.
74 TAILQ_HEAD(accept_queue, socket);
77 volatile u_int so_count; /* (b / refcount) */
78 struct selinfo so_rdsel; /* (b/cr) for so_rcv/so_comp */
79 struct selinfo so_wrsel; /* (b/cs) for so_snd */
80 short so_type; /* (a) generic type, see socket.h */
81 short so_options; /* (b) from socket call, see socket.h */
82 short so_linger; /* time to linger close(2) */
83 short so_state; /* (b) internal state flags SS_* */
84 void *so_pcb; /* protocol control block */
85 struct vnet *so_vnet; /* (a) network stack instance */
86 struct protosw *so_proto; /* (a) protocol handle */
87 short so_timeo; /* (g) connection timeout */
88 u_short so_error; /* (f) error affecting connection */
89 struct sigio *so_sigio; /* [sg] information for async I/O or
90 out of band data (SIGURG) */
91 struct ucred *so_cred; /* (a) user credentials */
92 struct label *so_label; /* (b) MAC label for socket */
93 /* NB: generation count must not be first. */
94 so_gen_t so_gencnt; /* (h) generation count */
95 void *so_emuldata; /* (b) private data for emulators */
96 struct osd osd; /* Object Specific extensions */
98 * so_fibnum, so_user_cookie and friends can be used to attach
99 * some user-specified metadata to a socket, which then can be
100 * used by the kernel for various actions.
101 * so_user_cookie is used by ipfw/dummynet.
103 int so_fibnum; /* routing domain for this socket */
104 uint32_t so_user_cookie;
106 int so_ts_clock; /* type of the clock used for timestamps */
107 uint32_t so_max_pacing_rate; /* (f) TX rate limit in bytes/s */
109 /* Regular (data flow) socket. */
111 /* (cr, cs) Receive and send buffers. */
112 struct sockbuf so_rcv, so_snd;
114 /* (e) Our place on accept queue. */
115 TAILQ_ENTRY(socket) so_list;
116 struct socket *so_listen; /* (b) */
119 SQ_INCOMP = 0x0800, /* on sol_incomp */
120 SQ_COMP = 0x1000, /* on sol_comp */
121 } so_qstate; /* (b) */
123 /* (b) cached MAC label for peer */
124 struct label *so_peerlabel;
125 u_long so_oobmark; /* chars to oob mark */
128 * Listening socket, where accepts occur, is so_listen in all
129 * subsidiary sockets. If so_listen is NULL, socket is not
130 * related to an accept. For a listening socket itself
131 * sol_incomp queues partially completed connections, while
132 * sol_comp is a queue of connections ready to be accepted.
133 * If a connection is aborted and it has so_listen set, then
134 * it has to be pulled out of either sol_incomp or sol_comp.
135 * We allow connections to queue up based on current queue
136 * lengths and limit on number of queued connections for this
140 /* (e) queue of partial unaccepted connections */
141 struct accept_queue sol_incomp;
142 /* (e) queue of complete unaccepted connections */
143 struct accept_queue sol_comp;
144 u_int sol_qlen; /* (e) sol_comp length */
145 u_int sol_incqlen; /* (e) sol_incomp length */
146 u_int sol_qlimit; /* (e) queue limit */
148 /* accept_filter(9) optional data */
149 struct accept_filter *sol_accept_filter;
150 void *sol_accept_filter_arg; /* saved filter args */
151 char *sol_accept_filter_str; /* saved user args */
153 /* Optional upcall, for kernel socket. */
154 so_upcall_t *sol_upcall; /* (e) */
155 void *sol_upcallarg; /* (e) */
157 /* Socket buffer parameters, to be copied to
158 * dataflow sockets, accepted from this one. */
161 u_int sol_sbrcv_hiwat;
162 u_int sol_sbsnd_hiwat;
163 short sol_sbrcv_flags;
164 short sol_sbsnd_flags;
165 sbintime_t sol_sbrcv_timeo;
166 sbintime_t sol_sbsnd_timeo;
171 #define SOCK_MTX(so) &(so)->so_lock
172 #define SOCK_LOCK(so) mtx_lock(&(so)->so_lock)
173 #define SOCK_OWNED(so) mtx_owned(&(so)->so_lock)
174 #define SOCK_UNLOCK(so) mtx_unlock(&(so)->so_lock)
175 #define SOCK_LOCK_ASSERT(so) mtx_assert(&(so)->so_lock, MA_OWNED)
176 #define SOCK_UNLOCK_ASSERT(so) mtx_assert(&(so)->so_lock, MA_NOTOWNED)
178 #define SOLISTENING(sol) (((sol)->so_options & SO_ACCEPTCONN) != 0)
179 #define SOLISTEN_LOCK(sol) do { \
180 mtx_lock(&(sol)->so_lock); \
181 KASSERT(SOLISTENING(sol), \
182 ("%s: %p not listening", __func__, (sol))); \
184 #define SOLISTEN_TRYLOCK(sol) mtx_trylock(&(sol)->so_lock)
185 #define SOLISTEN_UNLOCK(sol) do { \
186 KASSERT(SOLISTENING(sol), \
187 ("%s: %p not listening", __func__, (sol))); \
188 mtx_unlock(&(sol)->so_lock); \
190 #define SOLISTEN_LOCK_ASSERT(sol) do { \
191 mtx_assert(&(sol)->so_lock, MA_OWNED); \
192 KASSERT(SOLISTENING(sol), \
193 ("%s: %p not listening", __func__, (sol))); \
197 * Externalized form of struct socket used by the sysctl(3) interface.
200 size_t xso_len; /* length of this structure */
201 struct socket *xso_so; /* makes a convenient handle sometimes */
206 caddr_t so_pcb; /* another convenient handle */
216 struct xsockbuf so_rcv, so_snd;
217 uid_t so_uid; /* XXX */
223 * Macros for sockets and socket buffering.
229 #define SBL_WAIT 0x00000001 /* Wait if not immediately available. */
230 #define SBL_NOINTR 0x00000002 /* Force non-interruptible sleep. */
231 #define SBL_VALID (SBL_WAIT | SBL_NOINTR)
234 * Do we need to notify the other side when I/O is possible?
236 #define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
237 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
239 /* do we have to send all at once on a socket? */
240 #define sosendallatonce(so) \
241 ((so)->so_proto->pr_flags & PR_ATOMIC)
243 /* can we read something from so? */
244 #define soreadabledata(so) \
245 (sbavail(&(so)->so_rcv) >= (so)->so_rcv.sb_lowat || (so)->so_error)
246 #define soreadable(so) \
247 (soreadabledata(so) || ((so)->so_rcv.sb_state & SBS_CANTRCVMORE))
249 /* can we write something to so? */
250 #define sowriteable(so) \
251 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
252 (((so)->so_state&SS_ISCONNECTED) || \
253 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
254 ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
258 * soref()/sorele() ref-count the socket structure.
259 * soref() may be called without owning socket lock, but in that case a
260 * caller must own something that holds socket, and so_count must be not 0.
261 * Note that you must still explicitly close the socket, but the last ref
262 * count will free the structure.
264 #define soref(so) refcount_acquire(&(so)->so_count)
265 #define sorele(so) do { \
266 SOCK_LOCK_ASSERT(so); \
267 if (refcount_release(&(so)->so_count)) \
274 * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to
275 * avoid a non-atomic test-and-wakeup. However, sowakeup is
276 * responsible for releasing the lock if it is called. We unlock only
277 * if we don't call into sowakeup. If any code is introduced that
278 * directly invokes the underlying sowakeup() primitives, it must
279 * maintain the same semantics.
281 #define sorwakeup_locked(so) do { \
282 SOCKBUF_LOCK_ASSERT(&(so)->so_rcv); \
283 if (sb_notify(&(so)->so_rcv)) \
284 sowakeup((so), &(so)->so_rcv); \
286 SOCKBUF_UNLOCK(&(so)->so_rcv); \
289 #define sorwakeup(so) do { \
290 SOCKBUF_LOCK(&(so)->so_rcv); \
291 sorwakeup_locked(so); \
294 #define sowwakeup_locked(so) do { \
295 SOCKBUF_LOCK_ASSERT(&(so)->so_snd); \
296 if (sb_notify(&(so)->so_snd)) \
297 sowakeup((so), &(so)->so_snd); \
299 SOCKBUF_UNLOCK(&(so)->so_snd); \
302 #define sowwakeup(so) do { \
303 SOCKBUF_LOCK(&(so)->so_snd); \
304 sowwakeup_locked(so); \
307 struct accept_filter {
310 (struct socket *so, void *arg, int waitflag);
311 void * (*accf_create)
312 (struct socket *so, char *arg);
315 SLIST_ENTRY(accept_filter) accf_next;
318 #ifdef MALLOC_DECLARE
319 MALLOC_DECLARE(M_ACCF);
320 MALLOC_DECLARE(M_PCB);
321 MALLOC_DECLARE(M_SONAME);
325 * Socket specific helper hook point identifiers
326 * Do not leave holes in the sequence, hook registration is a loop.
328 #define HHOOK_SOCKET_OPT 0
329 #define HHOOK_SOCKET_CREATE 1
330 #define HHOOK_SOCKET_RCV 2
331 #define HHOOK_SOCKET_SND 3
332 #define HHOOK_FILT_SOREAD 4
333 #define HHOOK_FILT_SOWRITE 5
334 #define HHOOK_SOCKET_CLOSE 6
335 #define HHOOK_SOCKET_LAST HHOOK_SOCKET_CLOSE
337 struct socket_hhook_data {
340 void *hctx; /* hook point specific data*/
344 extern int maxsockets;
345 extern u_long sb_max;
346 extern so_gen_t so_gencnt;
356 /* 'which' values for socket upcalls. */
360 /* Return values for socket upcalls. */
362 #define SU_ISCONNECTED 1
365 * From uipc_socket and friends
367 int getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len);
368 int getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
369 struct file **fpp, u_int *fflagp, struct filecaps *havecaps);
370 void soabort(struct socket *so);
371 int soaccept(struct socket *so, struct sockaddr **nam);
372 void soaio_enqueue(struct task *task);
373 void soaio_rcv(void *context, int pending);
374 void soaio_snd(void *context, int pending);
375 int socheckuid(struct socket *so, uid_t uid);
376 int sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
377 int sobindat(int fd, struct socket *so, struct sockaddr *nam,
379 int soclose(struct socket *so);
380 int soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
381 int soconnectat(int fd, struct socket *so, struct sockaddr *nam,
383 int soconnect2(struct socket *so1, struct socket *so2);
384 int socreate(int dom, struct socket **aso, int type, int proto,
385 struct ucred *cred, struct thread *td);
386 int sodisconnect(struct socket *so);
387 struct sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags);
388 void sofree(struct socket *so);
389 void sohasoutofband(struct socket *so);
390 int solisten(struct socket *so, int backlog, struct thread *td);
391 void solisten_proto(struct socket *so, int backlog);
392 int solisten_proto_check(struct socket *so);
393 int solisten_dequeue(struct socket *, struct socket **, int);
395 sonewconn(struct socket *head, int connstatus);
397 sopeeloff(struct socket *);
398 int sopoll(struct socket *so, int events, struct ucred *active_cred,
400 int sopoll_generic(struct socket *so, int events,
401 struct ucred *active_cred, struct thread *td);
402 int soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
403 struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
404 int soreceive_stream(struct socket *so, struct sockaddr **paddr,
405 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
407 int soreceive_dgram(struct socket *so, struct sockaddr **paddr,
408 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
410 int soreceive_generic(struct socket *so, struct sockaddr **paddr,
411 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
413 int soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
414 void sorflush(struct socket *so);
415 int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
416 struct mbuf *top, struct mbuf *control, int flags,
418 int sosend_dgram(struct socket *so, struct sockaddr *addr,
419 struct uio *uio, struct mbuf *top, struct mbuf *control,
420 int flags, struct thread *td);
421 int sosend_generic(struct socket *so, struct sockaddr *addr,
422 struct uio *uio, struct mbuf *top, struct mbuf *control,
423 int flags, struct thread *td);
424 int soshutdown(struct socket *so, int how);
425 void sotoxsocket(struct socket *so, struct xsocket *xso);
426 void soupcall_clear(struct socket *, int);
427 void soupcall_set(struct socket *, int, so_upcall_t, void *);
428 void solisten_upcall_set(struct socket *, so_upcall_t, void *);
429 void sowakeup(struct socket *so, struct sockbuf *sb);
430 void sowakeup_aio(struct socket *so, struct sockbuf *sb);
431 void solisten_wakeup(struct socket *);
432 int selsocket(struct socket *so, int events, struct timeval *tv,
436 * Accept filter functions (duh).
438 int accept_filt_add(struct accept_filter *filt);
439 int accept_filt_del(char *name);
440 struct accept_filter *accept_filt_get(char *name);
441 #ifdef ACCEPT_FILTER_MOD
443 SYSCTL_DECL(_net_inet_accf);
445 int accept_filt_generic_mod_event(module_t mod, int event, void *data);
450 #endif /* !_SYS_SOCKETVAR_H_ */