2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2004 The FreeBSD Foundation
5 * Copyright (c) 2004-2008 Robert N. M. Watson
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
36 * Comments on the socket life cycle:
38 * soalloc() sets of socket layer state for a socket, called only by
39 * socreate() and sonewconn(). Socket layer private.
41 * sodealloc() tears down socket layer state for a socket, called only by
42 * sofree() and sonewconn(). Socket layer private.
44 * pru_attach() associates protocol layer state with an allocated socket;
45 * called only once, may fail, aborting socket allocation. This is called
46 * from socreate() and sonewconn(). Socket layer private.
48 * pru_detach() disassociates protocol layer state from an attached socket,
49 * and will be called exactly once for sockets in which pru_attach() has
50 * been successfully called. If pru_attach() returned an error,
51 * pru_detach() will not be called. Socket layer private.
53 * pru_abort() and pru_close() notify the protocol layer that the last
54 * consumer of a socket is starting to tear down the socket, and that the
55 * protocol should terminate the connection. Historically, pru_abort() also
56 * detached protocol state from the socket state, but this is no longer the
59 * socreate() creates a socket and attaches protocol state. This is a public
60 * interface that may be used by socket layer consumers to create new
63 * sonewconn() creates a socket and attaches protocol state. This is a
64 * public interface that may be used by protocols to create new sockets when
65 * a new connection is received and will be available for accept() on a
68 * soclose() destroys a socket after possibly waiting for it to disconnect.
69 * This is a public interface that socket consumers should use to close and
70 * release a socket when done with it.
72 * soabort() destroys a socket without waiting for it to disconnect (used
73 * only for incoming connections that are already partially or fully
74 * connected). This is used internally by the socket layer when clearing
75 * listen socket queues (due to overflow or close on the listen socket), but
76 * is also a public interface protocols may use to abort connections in
77 * their incomplete listen queues should they no longer be required. Sockets
78 * placed in completed connection listen queues should not be aborted for
79 * reasons described in the comment above the soclose() implementation. This
80 * is not a general purpose close routine, and except in the specific
81 * circumstances described here, should not be used.
83 * sofree() will free a socket and its protocol state if all references on
84 * the socket have been released, and is the public interface to attempt to
85 * free a socket when a reference is removed. This is a socket layer private
88 * NOTE: In addition to socreate() and soclose(), which provide a single
89 * socket reference to the consumer to be managed as required, there are two
90 * calls to explicitly manage socket references, soref(), and sorele().
91 * Currently, these are generally required only when transitioning a socket
92 * from a listen queue to a file descriptor, in order to prevent garbage
93 * collection of the socket at an untimely moment. For a number of reasons,
94 * these interfaces are not preferred, and should be avoided.
96 * NOTE: With regard to VNETs the general rule is that callers do not set
97 * curvnet. Exceptions to this rule include soabort(), sodisconnect(),
98 * sofree() (and with that sorele(), sotryfree()), as well as sonewconn()
99 * and sorflush(), which are usually called from a pre-set VNET context.
100 * sopoll() currently does not need a VNET context to be set.
103 #include <sys/cdefs.h>
104 __FBSDID("$FreeBSD$");
106 #include "opt_inet.h"
107 #include "opt_inet6.h"
108 #include "opt_zero.h"
109 #include "opt_compat.h"
111 #include <sys/param.h>
112 #include <sys/systm.h>
113 #include <sys/fcntl.h>
114 #include <sys/limits.h>
115 #include <sys/lock.h>
117 #include <sys/malloc.h>
118 #include <sys/mbuf.h>
119 #include <sys/mutex.h>
120 #include <sys/domain.h>
121 #include <sys/file.h> /* for struct knote */
122 #include <sys/kernel.h>
123 #include <sys/event.h>
124 #include <sys/eventhandler.h>
125 #include <sys/poll.h>
126 #include <sys/proc.h>
127 #include <sys/protosw.h>
128 #include <sys/socket.h>
129 #include <sys/socketvar.h>
130 #include <sys/resourcevar.h>
131 #include <net/route.h>
132 #include <sys/signalvar.h>
133 #include <sys/stat.h>
135 #include <sys/sysctl.h>
137 #include <sys/jail.h>
139 #include <net/vnet.h>
141 #include <security/mac/mac_framework.h>
145 #ifdef COMPAT_FREEBSD32
146 #include <sys/mount.h>
147 #include <sys/sysent.h>
148 #include <compat/freebsd32/freebsd32.h>
151 static int soreceive_rcvoob(struct socket *so, struct uio *uio,
154 static void filt_sordetach(struct knote *kn);
155 static int filt_soread(struct knote *kn, long hint);
156 static void filt_sowdetach(struct knote *kn);
157 static int filt_sowrite(struct knote *kn, long hint);
158 static int filt_solisten(struct knote *kn, long hint);
160 static struct filterops solisten_filtops = {
162 .f_detach = filt_sordetach,
163 .f_event = filt_solisten,
165 static struct filterops soread_filtops = {
167 .f_detach = filt_sordetach,
168 .f_event = filt_soread,
170 static struct filterops sowrite_filtops = {
172 .f_detach = filt_sowdetach,
173 .f_event = filt_sowrite,
176 uma_zone_t socket_zone;
177 so_gen_t so_gencnt; /* generation count for sockets */
181 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
182 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
184 #define VNET_SO_ASSERT(so) \
185 VNET_ASSERT(curvnet != NULL, \
186 ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so)));
188 static int somaxconn = SOMAXCONN;
189 static int sysctl_somaxconn(SYSCTL_HANDLER_ARGS);
190 /* XXX: we dont have SYSCTL_USHORT */
191 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLTYPE_UINT | CTLFLAG_RW,
192 0, sizeof(int), sysctl_somaxconn, "I", "Maximum pending socket connection "
194 static int numopensockets;
195 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
196 &numopensockets, 0, "Number of open sockets");
197 #ifdef ZERO_COPY_SOCKETS
198 /* These aren't static because they're used in other files. */
199 int so_zero_copy_send = 1;
200 int so_zero_copy_receive = 1;
201 SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0,
202 "Zero copy controls");
203 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW,
204 &so_zero_copy_receive, 0, "Enable zero copy receive");
205 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW,
206 &so_zero_copy_send, 0, "Enable zero copy send");
207 #endif /* ZERO_COPY_SOCKETS */
210 * accept_mtx locks down per-socket fields relating to accept queues. See
211 * socketvar.h for an annotation of the protected fields of struct socket.
213 struct mtx accept_mtx;
214 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
217 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
220 static struct mtx so_global_mtx;
221 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
224 * General IPC sysctl name space, used by sockets and a variety of other IPC
227 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
230 * Sysctl to get and set the maximum global sockets limit. Notify protocols
231 * of the change so that they can update their dependent limits as required.
234 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
236 int error, newmaxsockets;
238 newmaxsockets = maxsockets;
239 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
240 if (error == 0 && req->newptr) {
241 if (newmaxsockets > maxsockets) {
242 maxsockets = newmaxsockets;
243 if (maxsockets > ((maxfiles / 4) * 3)) {
244 maxfiles = (maxsockets * 5) / 4;
245 maxfilesperproc = (maxfiles * 9) / 10;
247 EVENTHANDLER_INVOKE(maxsockets_change);
254 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
255 &maxsockets, 0, sysctl_maxsockets, "IU",
256 "Maximum number of sockets avaliable");
259 * Initialise maxsockets. This SYSINIT must be run after
263 init_maxsockets(void *ignored)
266 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
267 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
269 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
272 * Socket operation routines. These routines are called by the routines in
273 * sys_socket.c or from a system process, and implement the semantics of
274 * socket operations by switching out to the protocol specific routines.
278 * Get a socket structure from our zone, and initialize it. Note that it
279 * would probably be better to allocate socket and PCB at the same time, but
280 * I'm not convinced that all the protocols can be easily modified to do
283 * soalloc() returns a socket with a ref count of 0.
285 static struct socket *
286 soalloc(struct vnet *vnet)
290 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
294 if (mac_socket_init(so, M_NOWAIT) != 0) {
295 uma_zfree(socket_zone, so);
299 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
300 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
301 sx_init(&so->so_snd.sb_sx, "so_snd_sx");
302 sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
303 TAILQ_INIT(&so->so_aiojobq);
304 mtx_lock(&so_global_mtx);
305 so->so_gencnt = ++so_gencnt;
308 VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p",
309 __func__, __LINE__, so));
310 vnet->vnet_sockcnt++;
313 mtx_unlock(&so_global_mtx);
318 * Free the storage associated with a socket at the socket layer, tear down
319 * locks, labels, etc. All protocol state is assumed already to have been
320 * torn down (and possibly never set up) by the caller.
323 sodealloc(struct socket *so)
326 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
327 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
329 mtx_lock(&so_global_mtx);
330 so->so_gencnt = ++so_gencnt;
331 --numopensockets; /* Could be below, but faster here. */
333 VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p",
334 __func__, __LINE__, so));
335 so->so_vnet->vnet_sockcnt--;
337 mtx_unlock(&so_global_mtx);
338 if (so->so_rcv.sb_hiwat)
339 (void)chgsbsize(so->so_cred->cr_uidinfo,
340 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
341 if (so->so_snd.sb_hiwat)
342 (void)chgsbsize(so->so_cred->cr_uidinfo,
343 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
345 /* remove acccept filter if one is present. */
346 if (so->so_accf != NULL)
347 do_setopt_accept_filter(so, NULL);
350 mac_socket_destroy(so);
353 sx_destroy(&so->so_snd.sb_sx);
354 sx_destroy(&so->so_rcv.sb_sx);
355 SOCKBUF_LOCK_DESTROY(&so->so_snd);
356 SOCKBUF_LOCK_DESTROY(&so->so_rcv);
357 uma_zfree(socket_zone, so);
361 * socreate returns a socket with a ref count of 1. The socket should be
362 * closed with soclose().
365 socreate(int dom, struct socket **aso, int type, int proto,
366 struct ucred *cred, struct thread *td)
373 prp = pffindproto(dom, proto, type);
375 prp = pffindtype(dom, type);
377 if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
378 prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
379 return (EPROTONOSUPPORT);
381 if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
382 return (EPROTONOSUPPORT);
384 if (prp->pr_type != type)
386 so = soalloc(CRED_TO_VNET(cred));
390 TAILQ_INIT(&so->so_incomp);
391 TAILQ_INIT(&so->so_comp);
393 so->so_cred = crhold(cred);
394 if ((prp->pr_domain->dom_family == PF_INET) ||
395 (prp->pr_domain->dom_family == PF_ROUTE))
396 so->so_fibnum = td->td_proc->p_fibnum;
401 mac_socket_create(cred, so);
403 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
404 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
407 * Auto-sizing of socket buffers is managed by the protocols and
408 * the appropriate flags must be set in the pru_attach function.
410 CURVNET_SET(so->so_vnet);
411 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
414 KASSERT(so->so_count == 1, ("socreate: so_count %d",
425 static int regression_sonewconn_earlytest = 1;
426 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
427 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
431 * When an attempt at a new connection is noted on a socket which accepts
432 * connections, sonewconn is called. If the connection is possible (subject
433 * to space constraints, etc.) then we allocate a new structure, propoerly
434 * linked into the data structure of the original socket, and return this.
435 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
437 * Note: the ref count on the socket is 0 on return.
440 sonewconn(struct socket *head, int connstatus)
446 over = (head->so_qlen > 3 * head->so_qlimit / 2);
449 if (regression_sonewconn_earlytest && over)
454 VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
455 __func__, __LINE__, head));
456 so = soalloc(head->so_vnet);
459 if ((head->so_options & SO_ACCEPTFILTER) != 0)
462 so->so_type = head->so_type;
463 so->so_options = head->so_options &~ SO_ACCEPTCONN;
464 so->so_linger = head->so_linger;
465 so->so_state = head->so_state | SS_NOFDREF;
466 so->so_fibnum = head->so_fibnum;
467 so->so_proto = head->so_proto;
468 so->so_cred = crhold(head->so_cred);
470 mac_socket_newconn(head, so);
472 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
473 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
474 VNET_SO_ASSERT(head);
475 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
476 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
480 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
481 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
482 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
483 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
484 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
485 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
486 so->so_state |= connstatus;
489 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
490 so->so_qstate |= SQ_COMP;
494 * Keep removing sockets from the head until there's room for
495 * us to insert on the tail. In pre-locking revisions, this
496 * was a simple if(), but as we could be racing with other
497 * threads and soabort() requires dropping locks, we must
498 * loop waiting for the condition to be true.
500 while (head->so_incqlen > head->so_qlimit) {
502 sp = TAILQ_FIRST(&head->so_incomp);
503 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
505 sp->so_qstate &= ~SQ_INCOMP;
511 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
512 so->so_qstate |= SQ_INCOMP;
518 wakeup_one(&head->so_timeo);
524 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
528 CURVNET_SET(so->so_vnet);
529 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
535 * solisten() transitions a socket from a non-listening state to a listening
536 * state, but can also be used to update the listen queue depth on an
537 * existing listen socket. The protocol will call back into the sockets
538 * layer using solisten_proto_check() and solisten_proto() to check and set
539 * socket-layer listen state. Call backs are used so that the protocol can
540 * acquire both protocol and socket layer locks in whatever order is required
543 * Protocol implementors are advised to hold the socket lock across the
544 * socket-layer test and set to avoid races at the socket layer.
547 solisten(struct socket *so, int backlog, struct thread *td)
551 CURVNET_SET(so->so_vnet);
552 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td);
558 solisten_proto_check(struct socket *so)
561 SOCK_LOCK_ASSERT(so);
563 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
570 solisten_proto(struct socket *so, int backlog)
573 SOCK_LOCK_ASSERT(so);
575 if (backlog < 0 || backlog > somaxconn)
577 so->so_qlimit = backlog;
578 so->so_options |= SO_ACCEPTCONN;
582 * Evaluate the reference count and named references on a socket; if no
583 * references remain, free it. This should be called whenever a reference is
584 * released, such as in sorele(), but also when named reference flags are
585 * cleared in socket or protocol code.
587 * sofree() will free the socket if:
589 * - There are no outstanding file descriptor references or related consumers
592 * - The socket has been closed by user space, if ever open (SS_NOFDREF).
594 * - The protocol does not have an outstanding strong reference on the socket
597 * - The socket is not in a completed connection queue, so a process has been
598 * notified that it is present. If it is removed, the user process may
599 * block in accept() despite select() saying the socket was ready.
602 sofree(struct socket *so)
604 struct protosw *pr = so->so_proto;
607 ACCEPT_LOCK_ASSERT();
608 SOCK_LOCK_ASSERT(so);
610 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
611 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
619 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
620 (so->so_qstate & SQ_INCOMP) != 0,
621 ("sofree: so_head != NULL, but neither SQ_COMP nor "
623 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
624 (so->so_qstate & SQ_INCOMP) == 0,
625 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
626 TAILQ_REMOVE(&head->so_incomp, so, so_list);
628 so->so_qstate &= ~SQ_INCOMP;
631 KASSERT((so->so_qstate & SQ_COMP) == 0 &&
632 (so->so_qstate & SQ_INCOMP) == 0,
633 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
634 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
635 if (so->so_options & SO_ACCEPTCONN) {
636 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
637 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated"));
643 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
644 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
645 if (pr->pr_usrreqs->pru_detach != NULL)
646 (*pr->pr_usrreqs->pru_detach)(so);
649 * From this point on, we assume that no other references to this
650 * socket exist anywhere else in the stack. Therefore, no locks need
651 * to be acquired or held.
653 * We used to do a lot of socket buffer and socket locking here, as
654 * well as invoke sorflush() and perform wakeups. The direct call to
655 * dom_dispose() and sbrelease_internal() are an inlining of what was
656 * necessary from sorflush().
658 * Notice that the socket buffer and kqueue state are torn down
659 * before calling pru_detach. This means that protocols shold not
660 * assume they can perform socket wakeups, etc, in their detach code.
662 sbdestroy(&so->so_snd, so);
663 sbdestroy(&so->so_rcv, so);
664 knlist_destroy(&so->so_rcv.sb_sel.si_note);
665 knlist_destroy(&so->so_snd.sb_sel.si_note);
670 * Close a socket on last file table reference removal. Initiate disconnect
671 * if connected. Free socket when disconnect complete.
673 * This function will sorele() the socket. Note that soclose() may be called
674 * prior to the ref count reaching zero. The actual socket structure will
675 * not be freed until the ref count reaches zero.
678 soclose(struct socket *so)
682 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
684 CURVNET_SET(so->so_vnet);
685 funsetown(&so->so_sigio);
686 if (so->so_state & SS_ISCONNECTED) {
687 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
688 error = sodisconnect(so);
690 if (error == ENOTCONN)
695 if (so->so_options & SO_LINGER) {
696 if ((so->so_state & SS_ISDISCONNECTING) &&
697 (so->so_state & SS_NBIO))
699 while (so->so_state & SS_ISCONNECTED) {
700 error = tsleep(&so->so_timeo,
701 PSOCK | PCATCH, "soclos", so->so_linger * hz);
709 if (so->so_proto->pr_usrreqs->pru_close != NULL)
710 (*so->so_proto->pr_usrreqs->pru_close)(so);
711 if (so->so_options & SO_ACCEPTCONN) {
714 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
715 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
717 sp->so_qstate &= ~SQ_INCOMP;
723 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
724 TAILQ_REMOVE(&so->so_comp, sp, so_list);
726 sp->so_qstate &= ~SQ_COMP;
736 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
737 so->so_state |= SS_NOFDREF;
744 * soabort() is used to abruptly tear down a connection, such as when a
745 * resource limit is reached (listen queue depth exceeded), or if a listen
746 * socket is closed while there are sockets waiting to be accepted.
748 * This interface is tricky, because it is called on an unreferenced socket,
749 * and must be called only by a thread that has actually removed the socket
750 * from the listen queue it was on, or races with other threads are risked.
752 * This interface will call into the protocol code, so must not be called
753 * with any socket locks held. Protocols do call it while holding their own
754 * recursible protocol mutexes, but this is something that should be subject
755 * to review in the future.
758 soabort(struct socket *so)
762 * In as much as is possible, assert that no references to this
763 * socket are held. This is not quite the same as asserting that the
764 * current thread is responsible for arranging for no references, but
765 * is as close as we can get for now.
767 KASSERT(so->so_count == 0, ("soabort: so_count"));
768 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
769 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
770 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
771 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
774 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
775 (*so->so_proto->pr_usrreqs->pru_abort)(so);
782 soaccept(struct socket *so, struct sockaddr **nam)
787 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
788 so->so_state &= ~SS_NOFDREF;
791 CURVNET_SET(so->so_vnet);
792 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
798 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
802 if (so->so_options & SO_ACCEPTCONN)
805 CURVNET_SET(so->so_vnet);
807 * If protocol is connection-based, can only connect once.
808 * Otherwise, if connected, try to disconnect first. This allows
809 * user to disconnect by connecting to, e.g., a null address.
811 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
812 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
813 (error = sodisconnect(so)))) {
817 * Prevent accumulated error from previous connection from
821 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
829 soconnect2(struct socket *so1, struct socket *so2)
833 CURVNET_SET(so1->so_vnet);
834 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
840 sodisconnect(struct socket *so)
844 if ((so->so_state & SS_ISCONNECTED) == 0)
846 if (so->so_state & SS_ISDISCONNECTING)
849 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
853 #ifdef ZERO_COPY_SOCKETS
854 struct so_zerocopy_stats{
859 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
860 #include <netinet/in.h>
861 #include <net/route.h>
862 #include <netinet/in_pcb.h>
864 #include <vm/vm_page.h>
865 #include <vm/vm_object.h>
868 * sosend_copyin() is only used if zero copy sockets are enabled. Otherwise
869 * sosend_dgram() and sosend_generic() use m_uiotombuf().
871 * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or
872 * all of the data referenced by the uio. If desired, it uses zero-copy.
873 * *space will be updated to reflect data copied in.
875 * NB: If atomic I/O is requested, the caller must already have checked that
876 * space can hold resid bytes.
878 * NB: In the event of an error, the caller may need to free the partial
879 * chain pointed to by *mpp. The contents of both *uio and *space may be
880 * modified even in the case of an error.
883 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space,
886 struct mbuf *m, **mp, *top;
889 #ifdef ZERO_COPY_SOCKETS
896 resid = uio->uio_resid;
899 #ifdef ZERO_COPY_SOCKETS
901 #endif /* ZERO_COPY_SOCKETS */
902 if (resid >= MINCLSIZE) {
903 #ifdef ZERO_COPY_SOCKETS
905 m = m_gethdr(M_WAITOK, MT_DATA);
907 m->m_pkthdr.rcvif = NULL;
909 m = m_get(M_WAITOK, MT_DATA);
910 if (so_zero_copy_send &&
913 uio->uio_iov->iov_len>=PAGE_SIZE) {
914 so_zerocp_stats.size_ok++;
915 so_zerocp_stats.align_ok++;
916 cow_send = socow_setup(m, uio);
920 m_clget(m, M_WAITOK);
921 len = min(min(MCLBYTES, resid), *space);
923 #else /* ZERO_COPY_SOCKETS */
925 m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
927 m->m_pkthdr.rcvif = NULL;
929 m = m_getcl(M_WAIT, MT_DATA, 0);
930 len = min(min(MCLBYTES, resid), *space);
931 #endif /* ZERO_COPY_SOCKETS */
934 m = m_gethdr(M_WAIT, MT_DATA);
936 m->m_pkthdr.rcvif = NULL;
938 len = min(min(MHLEN, resid), *space);
940 * For datagram protocols, leave room
941 * for protocol headers in first mbuf.
943 if (atomic && m && len < MHLEN)
946 m = m_get(M_WAIT, MT_DATA);
947 len = min(min(MLEN, resid), *space);
956 #ifdef ZERO_COPY_SOCKETS
960 #endif /* ZERO_COPY_SOCKETS */
961 error = uiomove(mtod(m, void *), (int)len, uio);
962 resid = uio->uio_resid;
965 top->m_pkthdr.len += len;
971 top->m_flags |= M_EOR;
974 } while (*space > 0 && atomic);
979 #endif /*ZERO_COPY_SOCKETS*/
981 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
984 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
985 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
988 int clen = 0, error, dontroute;
989 #ifdef ZERO_COPY_SOCKETS
990 int atomic = sosendallatonce(so) || top;
993 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
994 KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
995 ("sodgram_send: !PR_ATOMIC"));
998 resid = uio->uio_resid;
1000 resid = top->m_pkthdr.len;
1002 * In theory resid should be unsigned. However, space must be
1003 * signed, as it might be less than 0 if we over-committed, and we
1004 * must use a signed comparison of space and resid. On the other
1005 * hand, a negative resid causes us to loop sending 0-length
1006 * segments to the protocol.
1014 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
1016 td->td_ru.ru_msgsnd++;
1017 if (control != NULL)
1018 clen = control->m_len;
1020 SOCKBUF_LOCK(&so->so_snd);
1021 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1022 SOCKBUF_UNLOCK(&so->so_snd);
1027 error = so->so_error;
1029 SOCKBUF_UNLOCK(&so->so_snd);
1032 if ((so->so_state & SS_ISCONNECTED) == 0) {
1034 * `sendto' and `sendmsg' is allowed on a connection-based
1035 * socket if it supports implied connect. Return ENOTCONN if
1036 * not connected and no address is supplied.
1038 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1039 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1040 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1041 !(resid == 0 && clen != 0)) {
1042 SOCKBUF_UNLOCK(&so->so_snd);
1046 } else if (addr == NULL) {
1047 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1050 error = EDESTADDRREQ;
1051 SOCKBUF_UNLOCK(&so->so_snd);
1057 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
1058 * problem and need fixing.
1060 space = sbspace(&so->so_snd);
1061 if (flags & MSG_OOB)
1064 SOCKBUF_UNLOCK(&so->so_snd);
1065 if (resid > space) {
1071 if (flags & MSG_EOR)
1072 top->m_flags |= M_EOR;
1074 #ifdef ZERO_COPY_SOCKETS
1075 error = sosend_copyin(uio, &top, atomic, &space, flags);
1080 * Copy the data from userland into a mbuf chain.
1081 * If no data is to be copied in, a single empty mbuf
1084 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1085 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1087 error = EFAULT; /* only possible error */
1090 space -= resid - uio->uio_resid;
1092 resid = uio->uio_resid;
1094 KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1096 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1101 so->so_options |= SO_DONTROUTE;
1105 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1106 * of date. We could have recieved a reset packet in an interrupt or
1107 * maybe we slept while doing page faults in uiomove() etc. We could
1108 * probably recheck again inside the locking protection here, but
1109 * there are probably other places that this also happens. We must
1113 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1114 (flags & MSG_OOB) ? PRUS_OOB :
1116 * If the user set MSG_EOF, the protocol understands this flag and
1117 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1119 ((flags & MSG_EOF) &&
1120 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1123 /* If there is more to send set PRUS_MORETOCOME */
1124 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1125 top, addr, control, td);
1128 so->so_options &= ~SO_DONTROUTE;
1137 if (control != NULL)
1143 * Send on a socket. If send must go all at once and message is larger than
1144 * send buffering, then hard error. Lock against other senders. If must go
1145 * all at once and not enough room now, then inform user that this would
1146 * block and do nothing. Otherwise, if nonblocking, send as much as
1147 * possible. The data to be sent is described by "uio" if nonzero, otherwise
1148 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1149 * in mbuf chain must be small enough to send all at once.
1151 * Returns nonzero on error, timeout or signal; callers must check for short
1152 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1156 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1157 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1160 int clen = 0, error, dontroute;
1161 int atomic = sosendallatonce(so) || top;
1164 resid = uio->uio_resid;
1166 resid = top->m_pkthdr.len;
1168 * In theory resid should be unsigned. However, space must be
1169 * signed, as it might be less than 0 if we over-committed, and we
1170 * must use a signed comparison of space and resid. On the other
1171 * hand, a negative resid causes us to loop sending 0-length
1172 * segments to the protocol.
1174 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1175 * type sockets since that's an error.
1177 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1183 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1184 (so->so_proto->pr_flags & PR_ATOMIC);
1186 td->td_ru.ru_msgsnd++;
1187 if (control != NULL)
1188 clen = control->m_len;
1190 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1196 SOCKBUF_LOCK(&so->so_snd);
1197 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1198 SOCKBUF_UNLOCK(&so->so_snd);
1203 error = so->so_error;
1205 SOCKBUF_UNLOCK(&so->so_snd);
1208 if ((so->so_state & SS_ISCONNECTED) == 0) {
1210 * `sendto' and `sendmsg' is allowed on a connection-
1211 * based socket if it supports implied connect.
1212 * Return ENOTCONN if not connected and no address is
1215 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1216 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1217 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1218 !(resid == 0 && clen != 0)) {
1219 SOCKBUF_UNLOCK(&so->so_snd);
1223 } else if (addr == NULL) {
1224 SOCKBUF_UNLOCK(&so->so_snd);
1225 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1228 error = EDESTADDRREQ;
1232 space = sbspace(&so->so_snd);
1233 if (flags & MSG_OOB)
1235 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1236 clen > so->so_snd.sb_hiwat) {
1237 SOCKBUF_UNLOCK(&so->so_snd);
1241 if (space < resid + clen &&
1242 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1243 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
1244 SOCKBUF_UNLOCK(&so->so_snd);
1245 error = EWOULDBLOCK;
1248 error = sbwait(&so->so_snd);
1249 SOCKBUF_UNLOCK(&so->so_snd);
1254 SOCKBUF_UNLOCK(&so->so_snd);
1259 if (flags & MSG_EOR)
1260 top->m_flags |= M_EOR;
1262 #ifdef ZERO_COPY_SOCKETS
1263 error = sosend_copyin(uio, &top, atomic,
1269 * Copy the data from userland into a mbuf
1270 * chain. If no data is to be copied in,
1271 * a single empty mbuf is returned.
1273 top = m_uiotombuf(uio, M_WAITOK, space,
1274 (atomic ? max_hdr : 0),
1275 (atomic ? M_PKTHDR : 0) |
1276 ((flags & MSG_EOR) ? M_EOR : 0));
1278 error = EFAULT; /* only possible error */
1281 space -= resid - uio->uio_resid;
1283 resid = uio->uio_resid;
1287 so->so_options |= SO_DONTROUTE;
1291 * XXX all the SBS_CANTSENDMORE checks previously
1292 * done could be out of date. We could have recieved
1293 * a reset packet in an interrupt or maybe we slept
1294 * while doing page faults in uiomove() etc. We
1295 * could probably recheck again inside the locking
1296 * protection here, but there are probably other
1297 * places that this also happens. We must rethink
1301 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1302 (flags & MSG_OOB) ? PRUS_OOB :
1304 * If the user set MSG_EOF, the protocol understands
1305 * this flag and nothing left to send then use
1306 * PRU_SEND_EOF instead of PRU_SEND.
1308 ((flags & MSG_EOF) &&
1309 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1312 /* If there is more to send set PRUS_MORETOCOME. */
1313 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1314 top, addr, control, td);
1317 so->so_options &= ~SO_DONTROUTE;
1325 } while (resid && space > 0);
1329 sbunlock(&so->so_snd);
1333 if (control != NULL)
1339 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1340 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1344 CURVNET_SET(so->so_vnet);
1345 error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1346 control, flags, td);
1352 * The part of soreceive() that implements reading non-inline out-of-band
1353 * data from a socket. For more complete comments, see soreceive(), from
1354 * which this code originated.
1356 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1357 * unable to return an mbuf chain to the caller.
1360 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1362 struct protosw *pr = so->so_proto;
1366 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1369 m = m_get(M_WAIT, MT_DATA);
1370 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1374 #ifdef ZERO_COPY_SOCKETS
1375 if (so_zero_copy_receive) {
1378 if ((m->m_flags & M_EXT)
1379 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1384 error = uiomoveco(mtod(m, void *),
1385 min(uio->uio_resid, m->m_len),
1388 #endif /* ZERO_COPY_SOCKETS */
1389 error = uiomove(mtod(m, void *),
1390 (int) min(uio->uio_resid, m->m_len), uio);
1392 } while (uio->uio_resid && error == 0 && m);
1400 * Following replacement or removal of the first mbuf on the first mbuf chain
1401 * of a socket buffer, push necessary state changes back into the socket
1402 * buffer so that other consumers see the values consistently. 'nextrecord'
1403 * is the callers locally stored value of the original value of
1404 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1405 * NOTE: 'nextrecord' may be NULL.
1407 static __inline void
1408 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1411 SOCKBUF_LOCK_ASSERT(sb);
1413 * First, update for the new value of nextrecord. If necessary, make
1414 * it the first record.
1416 if (sb->sb_mb != NULL)
1417 sb->sb_mb->m_nextpkt = nextrecord;
1419 sb->sb_mb = nextrecord;
1422 * Now update any dependent socket buffer fields to reflect the new
1423 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
1424 * addition of a second clause that takes care of the case where
1425 * sb_mb has been updated, but remains the last record.
1427 if (sb->sb_mb == NULL) {
1428 sb->sb_mbtail = NULL;
1429 sb->sb_lastrecord = NULL;
1430 } else if (sb->sb_mb->m_nextpkt == NULL)
1431 sb->sb_lastrecord = sb->sb_mb;
1436 * Implement receive operations on a socket. We depend on the way that
1437 * records are added to the sockbuf by sbappend. In particular, each record
1438 * (mbufs linked through m_next) must begin with an address if the protocol
1439 * so specifies, followed by an optional mbuf or mbufs containing ancillary
1440 * data, and then zero or more mbufs of data. In order to allow parallelism
1441 * between network receive and copying to user space, as well as avoid
1442 * sleeping with a mutex held, we release the socket buffer mutex during the
1443 * user space copy. Although the sockbuf is locked, new data may still be
1444 * appended, and thus we must maintain consistency of the sockbuf during that
1447 * The caller may receive the data as a single mbuf chain by supplying an
1448 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1449 * the count in uio_resid.
1452 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1453 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1455 struct mbuf *m, **mp;
1456 int flags, len, error, offset;
1457 struct protosw *pr = so->so_proto;
1458 struct mbuf *nextrecord;
1460 int orig_resid = uio->uio_resid;
1465 if (controlp != NULL)
1468 flags = *flagsp &~ MSG_EOR;
1471 if (flags & MSG_OOB)
1472 return (soreceive_rcvoob(so, uio, flags));
1475 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1476 && uio->uio_resid) {
1478 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1481 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1486 SOCKBUF_LOCK(&so->so_rcv);
1487 m = so->so_rcv.sb_mb;
1489 * If we have less data than requested, block awaiting more (subject
1490 * to any timeout) if:
1491 * 1. the current count is less than the low water mark, or
1492 * 2. MSG_WAITALL is set, and it is possible to do the entire
1493 * receive operation at once if we block (resid <= hiwat).
1494 * 3. MSG_DONTWAIT is not set
1495 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1496 * we have to do the receive in sections, and thus risk returning a
1497 * short count if a timeout or signal occurs after we start.
1499 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1500 so->so_rcv.sb_cc < uio->uio_resid) &&
1501 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1502 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1503 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1504 KASSERT(m != NULL || !so->so_rcv.sb_cc,
1505 ("receive: m == %p so->so_rcv.sb_cc == %u",
1506 m, so->so_rcv.sb_cc));
1510 error = so->so_error;
1511 if ((flags & MSG_PEEK) == 0)
1513 SOCKBUF_UNLOCK(&so->so_rcv);
1516 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1517 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1519 SOCKBUF_UNLOCK(&so->so_rcv);
1524 for (; m != NULL; m = m->m_next)
1525 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1526 m = so->so_rcv.sb_mb;
1529 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1530 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1531 SOCKBUF_UNLOCK(&so->so_rcv);
1535 if (uio->uio_resid == 0) {
1536 SOCKBUF_UNLOCK(&so->so_rcv);
1539 if ((so->so_state & SS_NBIO) ||
1540 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1541 SOCKBUF_UNLOCK(&so->so_rcv);
1542 error = EWOULDBLOCK;
1545 SBLASTRECORDCHK(&so->so_rcv);
1546 SBLASTMBUFCHK(&so->so_rcv);
1547 error = sbwait(&so->so_rcv);
1548 SOCKBUF_UNLOCK(&so->so_rcv);
1555 * From this point onward, we maintain 'nextrecord' as a cache of the
1556 * pointer to the next record in the socket buffer. We must keep the
1557 * various socket buffer pointers and local stack versions of the
1558 * pointers in sync, pushing out modifications before dropping the
1559 * socket buffer mutex, and re-reading them when picking it up.
1561 * Otherwise, we will race with the network stack appending new data
1562 * or records onto the socket buffer by using inconsistent/stale
1563 * versions of the field, possibly resulting in socket buffer
1566 * By holding the high-level sblock(), we prevent simultaneous
1567 * readers from pulling off the front of the socket buffer.
1569 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1571 uio->uio_td->td_ru.ru_msgrcv++;
1572 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1573 SBLASTRECORDCHK(&so->so_rcv);
1574 SBLASTMBUFCHK(&so->so_rcv);
1575 nextrecord = m->m_nextpkt;
1576 if (pr->pr_flags & PR_ADDR) {
1577 KASSERT(m->m_type == MT_SONAME,
1578 ("m->m_type == %d", m->m_type));
1581 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1583 if (flags & MSG_PEEK) {
1586 sbfree(&so->so_rcv, m);
1587 so->so_rcv.sb_mb = m_free(m);
1588 m = so->so_rcv.sb_mb;
1589 sockbuf_pushsync(&so->so_rcv, nextrecord);
1594 * Process one or more MT_CONTROL mbufs present before any data mbufs
1595 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1596 * just copy the data; if !MSG_PEEK, we call into the protocol to
1597 * perform externalization (or freeing if controlp == NULL).
1599 if (m != NULL && m->m_type == MT_CONTROL) {
1600 struct mbuf *cm = NULL, *cmn;
1601 struct mbuf **cme = &cm;
1604 if (flags & MSG_PEEK) {
1605 if (controlp != NULL) {
1606 *controlp = m_copy(m, 0, m->m_len);
1607 controlp = &(*controlp)->m_next;
1611 sbfree(&so->so_rcv, m);
1612 so->so_rcv.sb_mb = m->m_next;
1615 cme = &(*cme)->m_next;
1616 m = so->so_rcv.sb_mb;
1618 } while (m != NULL && m->m_type == MT_CONTROL);
1619 if ((flags & MSG_PEEK) == 0)
1620 sockbuf_pushsync(&so->so_rcv, nextrecord);
1621 while (cm != NULL) {
1624 if (pr->pr_domain->dom_externalize != NULL) {
1625 SOCKBUF_UNLOCK(&so->so_rcv);
1627 error = (*pr->pr_domain->dom_externalize)
1629 SOCKBUF_LOCK(&so->so_rcv);
1630 } else if (controlp != NULL)
1634 if (controlp != NULL) {
1636 while (*controlp != NULL)
1637 controlp = &(*controlp)->m_next;
1642 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1644 nextrecord = so->so_rcv.sb_mb;
1648 if ((flags & MSG_PEEK) == 0) {
1649 KASSERT(m->m_nextpkt == nextrecord,
1650 ("soreceive: post-control, nextrecord !sync"));
1651 if (nextrecord == NULL) {
1652 KASSERT(so->so_rcv.sb_mb == m,
1653 ("soreceive: post-control, sb_mb!=m"));
1654 KASSERT(so->so_rcv.sb_lastrecord == m,
1655 ("soreceive: post-control, lastrecord!=m"));
1659 if (type == MT_OOBDATA)
1662 if ((flags & MSG_PEEK) == 0) {
1663 KASSERT(so->so_rcv.sb_mb == nextrecord,
1664 ("soreceive: sb_mb != nextrecord"));
1665 if (so->so_rcv.sb_mb == NULL) {
1666 KASSERT(so->so_rcv.sb_lastrecord == NULL,
1667 ("soreceive: sb_lastercord != NULL"));
1671 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1672 SBLASTRECORDCHK(&so->so_rcv);
1673 SBLASTMBUFCHK(&so->so_rcv);
1676 * Now continue to read any data mbufs off of the head of the socket
1677 * buffer until the read request is satisfied. Note that 'type' is
1678 * used to store the type of any mbuf reads that have happened so far
1679 * such that soreceive() can stop reading if the type changes, which
1680 * causes soreceive() to return only one of regular data and inline
1681 * out-of-band data in a single socket receive operation.
1685 while (m != NULL && uio->uio_resid > 0 && error == 0) {
1687 * If the type of mbuf has changed since the last mbuf
1688 * examined ('type'), end the receive operation.
1690 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1691 if (m->m_type == MT_OOBDATA) {
1692 if (type != MT_OOBDATA)
1694 } else if (type == MT_OOBDATA)
1697 KASSERT(m->m_type == MT_DATA,
1698 ("m->m_type == %d", m->m_type));
1699 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1700 len = uio->uio_resid;
1701 if (so->so_oobmark && len > so->so_oobmark - offset)
1702 len = so->so_oobmark - offset;
1703 if (len > m->m_len - moff)
1704 len = m->m_len - moff;
1706 * If mp is set, just pass back the mbufs. Otherwise copy
1707 * them out via the uio, then free. Sockbuf must be
1708 * consistent here (points to current mbuf, it points to next
1709 * record) when we drop priority; we must note any additions
1710 * to the sockbuf when we block interrupts again.
1713 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1714 SBLASTRECORDCHK(&so->so_rcv);
1715 SBLASTMBUFCHK(&so->so_rcv);
1716 SOCKBUF_UNLOCK(&so->so_rcv);
1717 #ifdef ZERO_COPY_SOCKETS
1718 if (so_zero_copy_receive) {
1721 if ((m->m_flags & M_EXT)
1722 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1727 error = uiomoveco(mtod(m, char *) + moff,
1731 #endif /* ZERO_COPY_SOCKETS */
1732 error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1733 SOCKBUF_LOCK(&so->so_rcv);
1736 * The MT_SONAME mbuf has already been removed
1737 * from the record, so it is necessary to
1738 * remove the data mbufs, if any, to preserve
1739 * the invariant in the case of PR_ADDR that
1740 * requires MT_SONAME mbufs at the head of
1743 if (m && pr->pr_flags & PR_ATOMIC &&
1744 ((flags & MSG_PEEK) == 0))
1745 (void)sbdroprecord_locked(&so->so_rcv);
1746 SOCKBUF_UNLOCK(&so->so_rcv);
1750 uio->uio_resid -= len;
1751 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1752 if (len == m->m_len - moff) {
1753 if (m->m_flags & M_EOR)
1755 if (flags & MSG_PEEK) {
1759 nextrecord = m->m_nextpkt;
1760 sbfree(&so->so_rcv, m);
1764 so->so_rcv.sb_mb = m = m->m_next;
1767 so->so_rcv.sb_mb = m_free(m);
1768 m = so->so_rcv.sb_mb;
1770 sockbuf_pushsync(&so->so_rcv, nextrecord);
1771 SBLASTRECORDCHK(&so->so_rcv);
1772 SBLASTMBUFCHK(&so->so_rcv);
1775 if (flags & MSG_PEEK)
1781 if (flags & MSG_DONTWAIT)
1782 copy_flag = M_DONTWAIT;
1785 if (copy_flag == M_WAIT)
1786 SOCKBUF_UNLOCK(&so->so_rcv);
1787 *mp = m_copym(m, 0, len, copy_flag);
1788 if (copy_flag == M_WAIT)
1789 SOCKBUF_LOCK(&so->so_rcv);
1792 * m_copym() couldn't
1793 * allocate an mbuf. Adjust
1794 * uio_resid back (it was
1795 * adjusted down by len
1796 * bytes, which we didn't end
1797 * up "copying" over).
1799 uio->uio_resid += len;
1805 so->so_rcv.sb_cc -= len;
1808 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1809 if (so->so_oobmark) {
1810 if ((flags & MSG_PEEK) == 0) {
1811 so->so_oobmark -= len;
1812 if (so->so_oobmark == 0) {
1813 so->so_rcv.sb_state |= SBS_RCVATMARK;
1818 if (offset == so->so_oobmark)
1822 if (flags & MSG_EOR)
1825 * If the MSG_WAITALL flag is set (for non-atomic socket), we
1826 * must not quit until "uio->uio_resid == 0" or an error
1827 * termination. If a signal/timeout occurs, return with a
1828 * short count but without error. Keep sockbuf locked
1829 * against other readers.
1831 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1832 !sosendallatonce(so) && nextrecord == NULL) {
1833 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1834 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
1837 * Notify the protocol that some data has been
1838 * drained before blocking.
1840 if (pr->pr_flags & PR_WANTRCVD) {
1841 SOCKBUF_UNLOCK(&so->so_rcv);
1843 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1844 SOCKBUF_LOCK(&so->so_rcv);
1846 SBLASTRECORDCHK(&so->so_rcv);
1847 SBLASTMBUFCHK(&so->so_rcv);
1849 * We could receive some data while was notifying
1850 * the protocol. Skip blocking in this case.
1852 if (so->so_rcv.sb_mb == NULL) {
1853 error = sbwait(&so->so_rcv);
1855 SOCKBUF_UNLOCK(&so->so_rcv);
1859 m = so->so_rcv.sb_mb;
1861 nextrecord = m->m_nextpkt;
1865 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1866 if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1868 if ((flags & MSG_PEEK) == 0)
1869 (void) sbdroprecord_locked(&so->so_rcv);
1871 if ((flags & MSG_PEEK) == 0) {
1874 * First part is an inline SB_EMPTY_FIXUP(). Second
1875 * part makes sure sb_lastrecord is up-to-date if
1876 * there is still data in the socket buffer.
1878 so->so_rcv.sb_mb = nextrecord;
1879 if (so->so_rcv.sb_mb == NULL) {
1880 so->so_rcv.sb_mbtail = NULL;
1881 so->so_rcv.sb_lastrecord = NULL;
1882 } else if (nextrecord->m_nextpkt == NULL)
1883 so->so_rcv.sb_lastrecord = nextrecord;
1885 SBLASTRECORDCHK(&so->so_rcv);
1886 SBLASTMBUFCHK(&so->so_rcv);
1888 * If soreceive() is being done from the socket callback,
1889 * then don't need to generate ACK to peer to update window,
1890 * since ACK will be generated on return to TCP.
1892 if (!(flags & MSG_SOCALLBCK) &&
1893 (pr->pr_flags & PR_WANTRCVD)) {
1894 SOCKBUF_UNLOCK(&so->so_rcv);
1896 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1897 SOCKBUF_LOCK(&so->so_rcv);
1900 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1901 if (orig_resid == uio->uio_resid && orig_resid &&
1902 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1903 SOCKBUF_UNLOCK(&so->so_rcv);
1906 SOCKBUF_UNLOCK(&so->so_rcv);
1911 sbunlock(&so->so_rcv);
1916 * Optimized version of soreceive() for stream (TCP) sockets.
1918 #ifdef TCP_SORECEIVE_STREAM
1920 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
1921 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1923 int len = 0, error = 0, flags, oresid;
1925 struct mbuf *m, *n = NULL;
1927 /* We only do stream sockets. */
1928 if (so->so_type != SOCK_STREAM)
1932 if (controlp != NULL)
1935 flags = *flagsp &~ MSG_EOR;
1938 if (flags & MSG_OOB)
1939 return (soreceive_rcvoob(so, uio, flags));
1945 /* Prevent other readers from entering the socket. */
1946 error = sblock(sb, SBLOCKWAIT(flags));
1951 /* Easy one, no space to copyout anything. */
1952 if (uio->uio_resid == 0) {
1956 oresid = uio->uio_resid;
1958 /* We will never ever get anything unless we are connected. */
1959 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1960 /* When disconnecting there may be still some data left. */
1963 if (!(so->so_state & SS_ISDISCONNECTED))
1968 /* Socket buffer is empty and we shall not block. */
1969 if (sb->sb_cc == 0 &&
1970 ((sb->sb_flags & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
1976 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1978 /* Abort if socket has reported problems. */
1982 if (oresid > uio->uio_resid)
1984 error = so->so_error;
1985 if (!(flags & MSG_PEEK))
1990 /* Door is closed. Deliver what is left, if any. */
1991 if (sb->sb_state & SBS_CANTRCVMORE) {
1998 /* Socket buffer got some data that we shall deliver now. */
1999 if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) &&
2000 ((sb->sb_flags & SS_NBIO) ||
2001 (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
2002 sb->sb_cc >= sb->sb_lowat ||
2003 sb->sb_cc >= uio->uio_resid ||
2004 sb->sb_cc >= sb->sb_hiwat) ) {
2008 /* On MSG_WAITALL we must wait until all data or error arrives. */
2009 if ((flags & MSG_WAITALL) &&
2010 (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_lowat))
2014 * Wait and block until (more) data comes in.
2015 * NB: Drops the sockbuf lock during wait.
2023 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2024 KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__));
2025 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
2029 uio->uio_td->td_ru.ru_msgrcv++;
2031 /* Fill uio until full or current end of socket buffer is reached. */
2032 len = min(uio->uio_resid, sb->sb_cc);
2034 /* Dequeue as many mbufs as possible. */
2035 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
2036 for (*mp0 = m = sb->sb_mb;
2037 m != NULL && m->m_len <= len;
2040 uio->uio_resid -= m->m_len;
2045 if (sb->sb_mb == NULL)
2049 /* Copy the remainder. */
2051 KASSERT(sb->sb_mb != NULL,
2052 ("%s: len > 0 && sb->sb_mb empty", __func__));
2054 m = m_copym(sb->sb_mb, 0, len, M_DONTWAIT);
2056 len = 0; /* Don't flush data from sockbuf. */
2058 uio->uio_resid -= m->m_len;
2069 /* NB: Must unlock socket buffer as uiomove may sleep. */
2071 error = m_mbuftouio(uio, sb->sb_mb, len);
2076 SBLASTRECORDCHK(sb);
2080 * Remove the delivered data from the socket buffer unless we
2081 * were only peeking.
2083 if (!(flags & MSG_PEEK)) {
2085 sbdrop_locked(sb, len);
2087 /* Notify protocol that we drained some data. */
2088 if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
2089 (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
2090 !(flags & MSG_SOCALLBCK))) {
2093 (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
2099 * For MSG_WAITALL we may have to loop again and wait for
2100 * more data to come in.
2102 if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
2105 SOCKBUF_LOCK_ASSERT(sb);
2106 SBLASTRECORDCHK(sb);
2112 #endif /* TCP_SORECEIVE_STREAM */
2115 * Optimized version of soreceive() for simple datagram cases from userspace.
2116 * Unlike in the stream case, we're able to drop a datagram if copyout()
2117 * fails, and because we handle datagrams atomically, we don't need to use a
2118 * sleep lock to prevent I/O interlacing.
2121 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
2122 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2124 struct mbuf *m, *m2;
2125 int flags, len, error;
2126 struct protosw *pr = so->so_proto;
2127 struct mbuf *nextrecord;
2131 if (controlp != NULL)
2134 flags = *flagsp &~ MSG_EOR;
2139 * For any complicated cases, fall back to the full
2140 * soreceive_generic().
2142 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
2143 return (soreceive_generic(so, psa, uio, mp0, controlp,
2147 * Enforce restrictions on use.
2149 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
2150 ("soreceive_dgram: wantrcvd"));
2151 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
2152 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
2153 ("soreceive_dgram: SBS_RCVATMARK"));
2154 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
2155 ("soreceive_dgram: P_CONNREQUIRED"));
2158 * Loop blocking while waiting for a datagram.
2160 SOCKBUF_LOCK(&so->so_rcv);
2161 while ((m = so->so_rcv.sb_mb) == NULL) {
2162 KASSERT(so->so_rcv.sb_cc == 0,
2163 ("soreceive_dgram: sb_mb NULL but sb_cc %u",
2166 error = so->so_error;
2168 SOCKBUF_UNLOCK(&so->so_rcv);
2171 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
2172 uio->uio_resid == 0) {
2173 SOCKBUF_UNLOCK(&so->so_rcv);
2176 if ((so->so_state & SS_NBIO) ||
2177 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2178 SOCKBUF_UNLOCK(&so->so_rcv);
2179 return (EWOULDBLOCK);
2181 SBLASTRECORDCHK(&so->so_rcv);
2182 SBLASTMBUFCHK(&so->so_rcv);
2183 error = sbwait(&so->so_rcv);
2185 SOCKBUF_UNLOCK(&so->so_rcv);
2189 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2192 uio->uio_td->td_ru.ru_msgrcv++;
2193 SBLASTRECORDCHK(&so->so_rcv);
2194 SBLASTMBUFCHK(&so->so_rcv);
2195 nextrecord = m->m_nextpkt;
2196 if (nextrecord == NULL) {
2197 KASSERT(so->so_rcv.sb_lastrecord == m,
2198 ("soreceive_dgram: lastrecord != m"));
2201 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
2202 ("soreceive_dgram: m_nextpkt != nextrecord"));
2205 * Pull 'm' and its chain off the front of the packet queue.
2207 so->so_rcv.sb_mb = NULL;
2208 sockbuf_pushsync(&so->so_rcv, nextrecord);
2211 * Walk 'm's chain and free that many bytes from the socket buffer.
2213 for (m2 = m; m2 != NULL; m2 = m2->m_next)
2214 sbfree(&so->so_rcv, m2);
2217 * Do a few last checks before we let go of the lock.
2219 SBLASTRECORDCHK(&so->so_rcv);
2220 SBLASTMBUFCHK(&so->so_rcv);
2221 SOCKBUF_UNLOCK(&so->so_rcv);
2223 if (pr->pr_flags & PR_ADDR) {
2224 KASSERT(m->m_type == MT_SONAME,
2225 ("m->m_type == %d", m->m_type));
2227 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
2232 /* XXXRW: Can this happen? */
2237 * Packet to copyout() is now in 'm' and it is disconnected from the
2240 * Process one or more MT_CONTROL mbufs present before any data mbufs
2241 * in the first mbuf chain on the socket buffer. We call into the
2242 * protocol to perform externalization (or freeing if controlp ==
2245 if (m->m_type == MT_CONTROL) {
2246 struct mbuf *cm = NULL, *cmn;
2247 struct mbuf **cme = &cm;
2253 cme = &(*cme)->m_next;
2255 } while (m != NULL && m->m_type == MT_CONTROL);
2256 while (cm != NULL) {
2259 if (pr->pr_domain->dom_externalize != NULL) {
2260 error = (*pr->pr_domain->dom_externalize)
2262 } else if (controlp != NULL)
2266 if (controlp != NULL) {
2267 while (*controlp != NULL)
2268 controlp = &(*controlp)->m_next;
2273 KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
2275 while (m != NULL && uio->uio_resid > 0) {
2276 len = uio->uio_resid;
2279 error = uiomove(mtod(m, char *), (int)len, uio);
2284 if (len == m->m_len)
2300 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2301 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2305 CURVNET_SET(so->so_vnet);
2306 error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
2313 soshutdown(struct socket *so, int how)
2315 struct protosw *pr = so->so_proto;
2318 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2321 CURVNET_SET(so->so_vnet);
2322 if (pr->pr_usrreqs->pru_flush != NULL) {
2323 (*pr->pr_usrreqs->pru_flush)(so, how);
2327 if (how != SHUT_RD) {
2328 error = (*pr->pr_usrreqs->pru_shutdown)(so);
2337 sorflush(struct socket *so)
2339 struct sockbuf *sb = &so->so_rcv;
2340 struct protosw *pr = so->so_proto;
2346 * In order to avoid calling dom_dispose with the socket buffer mutex
2347 * held, and in order to generally avoid holding the lock for a long
2348 * time, we make a copy of the socket buffer and clear the original
2349 * (except locks, state). The new socket buffer copy won't have
2350 * initialized locks so we can only call routines that won't use or
2351 * assert those locks.
2353 * Dislodge threads currently blocked in receive and wait to acquire
2354 * a lock against other simultaneous readers before clearing the
2355 * socket buffer. Don't let our acquire be interrupted by a signal
2356 * despite any existing socket disposition on interruptable waiting.
2359 (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2362 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2363 * and mutex data unchanged.
2366 bzero(&asb, offsetof(struct sockbuf, sb_startzero));
2367 bcopy(&sb->sb_startzero, &asb.sb_startzero,
2368 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2369 bzero(&sb->sb_startzero,
2370 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2375 * Dispose of special rights and flush the socket buffer. Don't call
2376 * any unsafe routines (that rely on locks being initialized) on asb.
2378 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2379 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
2380 sbrelease_internal(&asb, so);
2384 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2385 * additional variant to handle the case where the option value needs to be
2386 * some kind of integer, but not a specific size. In addition to their use
2387 * here, these functions are also called by the protocol-level pr_ctloutput()
2391 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2396 * If the user gives us more than we wanted, we ignore it, but if we
2397 * don't get the minimum length the caller wants, we return EINVAL.
2398 * On success, sopt->sopt_valsize is set to however much we actually
2401 if ((valsize = sopt->sopt_valsize) < minlen)
2404 sopt->sopt_valsize = valsize = len;
2406 if (sopt->sopt_td != NULL)
2407 return (copyin(sopt->sopt_val, buf, valsize));
2409 bcopy(sopt->sopt_val, buf, valsize);
2414 * Kernel version of setsockopt(2).
2416 * XXX: optlen is size_t, not socklen_t
2419 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2422 struct sockopt sopt;
2424 sopt.sopt_level = level;
2425 sopt.sopt_name = optname;
2426 sopt.sopt_dir = SOPT_SET;
2427 sopt.sopt_val = optval;
2428 sopt.sopt_valsize = optlen;
2429 sopt.sopt_td = NULL;
2430 return (sosetopt(so, &sopt));
2434 sosetopt(struct socket *so, struct sockopt *sopt)
2445 CURVNET_SET(so->so_vnet);
2447 if (sopt->sopt_level != SOL_SOCKET) {
2448 if (so->so_proto && so->so_proto->pr_ctloutput) {
2449 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2453 error = ENOPROTOOPT;
2455 switch (sopt->sopt_name) {
2457 case SO_ACCEPTFILTER:
2458 error = do_setopt_accept_filter(so, sopt);
2464 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2469 so->so_linger = l.l_linger;
2471 so->so_options |= SO_LINGER;
2473 so->so_options &= ~SO_LINGER;
2480 case SO_USELOOPBACK:
2490 error = sooptcopyin(sopt, &optval, sizeof optval,
2496 so->so_options |= sopt->sopt_name;
2498 so->so_options &= ~sopt->sopt_name;
2503 error = sooptcopyin(sopt, &optval, sizeof optval,
2505 if (optval < 0 || optval > rt_numfibs) {
2509 if (so->so_proto != NULL &&
2510 ((so->so_proto->pr_domain->dom_family == PF_INET) ||
2511 (so->so_proto->pr_domain->dom_family == PF_ROUTE))) {
2512 so->so_fibnum = optval;
2513 /* Note: ignore error */
2514 if (so->so_proto->pr_ctloutput)
2515 (*so->so_proto->pr_ctloutput)(so, sopt);
2521 case SO_USER_COOKIE:
2522 error = sooptcopyin(sopt, &val32, sizeof val32,
2526 so->so_user_cookie = val32;
2533 error = sooptcopyin(sopt, &optval, sizeof optval,
2539 * Values < 1 make no sense for any of these options,
2547 switch (sopt->sopt_name) {
2550 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
2551 &so->so_snd : &so->so_rcv, (u_long)optval,
2552 so, curthread) == 0) {
2556 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
2557 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
2561 * Make sure the low-water is never greater than the
2565 SOCKBUF_LOCK(&so->so_snd);
2566 so->so_snd.sb_lowat =
2567 (optval > so->so_snd.sb_hiwat) ?
2568 so->so_snd.sb_hiwat : optval;
2569 SOCKBUF_UNLOCK(&so->so_snd);
2572 SOCKBUF_LOCK(&so->so_rcv);
2573 so->so_rcv.sb_lowat =
2574 (optval > so->so_rcv.sb_hiwat) ?
2575 so->so_rcv.sb_hiwat : optval;
2576 SOCKBUF_UNLOCK(&so->so_rcv);
2583 #ifdef COMPAT_FREEBSD32
2584 if (SV_CURPROC_FLAG(SV_ILP32)) {
2585 struct timeval32 tv32;
2587 error = sooptcopyin(sopt, &tv32, sizeof tv32,
2589 CP(tv32, tv, tv_sec);
2590 CP(tv32, tv, tv_usec);
2593 error = sooptcopyin(sopt, &tv, sizeof tv,
2598 /* assert(hz > 0); */
2599 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
2600 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
2604 /* assert(tick > 0); */
2605 /* assert(ULONG_MAX - INT_MAX >= 1000000); */
2606 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
2607 if (val > INT_MAX) {
2611 if (val == 0 && tv.tv_usec != 0)
2614 switch (sopt->sopt_name) {
2616 so->so_snd.sb_timeo = val;
2619 so->so_rcv.sb_timeo = val;
2626 error = sooptcopyin(sopt, &extmac, sizeof extmac,
2630 error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2638 error = ENOPROTOOPT;
2641 if (error == 0 && so->so_proto != NULL &&
2642 so->so_proto->pr_ctloutput != NULL) {
2643 (void) ((*so->so_proto->pr_ctloutput)
2653 * Helper routine for getsockopt.
2656 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2664 * Documented get behavior is that we always return a value, possibly
2665 * truncated to fit in the user's buffer. Traditional behavior is
2666 * that we always tell the user precisely how much we copied, rather
2667 * than something useful like the total amount we had available for
2668 * her. Note that this interface is not idempotent; the entire
2669 * answer must generated ahead of time.
2671 valsize = min(len, sopt->sopt_valsize);
2672 sopt->sopt_valsize = valsize;
2673 if (sopt->sopt_val != NULL) {
2674 if (sopt->sopt_td != NULL)
2675 error = copyout(buf, sopt->sopt_val, valsize);
2677 bcopy(buf, sopt->sopt_val, valsize);
2683 sogetopt(struct socket *so, struct sockopt *sopt)
2692 CURVNET_SET(so->so_vnet);
2694 if (sopt->sopt_level != SOL_SOCKET) {
2695 if (so->so_proto && so->so_proto->pr_ctloutput)
2696 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2698 error = ENOPROTOOPT;
2702 switch (sopt->sopt_name) {
2704 case SO_ACCEPTFILTER:
2705 error = do_getopt_accept_filter(so, sopt);
2710 l.l_onoff = so->so_options & SO_LINGER;
2711 l.l_linger = so->so_linger;
2713 error = sooptcopyout(sopt, &l, sizeof l);
2716 case SO_USELOOPBACK:
2728 optval = so->so_options & sopt->sopt_name;
2730 error = sooptcopyout(sopt, &optval, sizeof optval);
2734 optval = so->so_type;
2739 optval = so->so_error;
2745 optval = so->so_snd.sb_hiwat;
2749 optval = so->so_rcv.sb_hiwat;
2753 optval = so->so_snd.sb_lowat;
2757 optval = so->so_rcv.sb_lowat;
2762 optval = (sopt->sopt_name == SO_SNDTIMEO ?
2763 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2765 tv.tv_sec = optval / hz;
2766 tv.tv_usec = (optval % hz) * tick;
2767 #ifdef COMPAT_FREEBSD32
2768 if (SV_CURPROC_FLAG(SV_ILP32)) {
2769 struct timeval32 tv32;
2771 CP(tv, tv32, tv_sec);
2772 CP(tv, tv32, tv_usec);
2773 error = sooptcopyout(sopt, &tv32, sizeof tv32);
2776 error = sooptcopyout(sopt, &tv, sizeof tv);
2781 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2785 error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
2789 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2797 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2801 error = mac_getsockopt_peerlabel(
2802 sopt->sopt_td->td_ucred, so, &extmac);
2805 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2811 case SO_LISTENQLIMIT:
2812 optval = so->so_qlimit;
2816 optval = so->so_qlen;
2819 case SO_LISTENINCQLEN:
2820 optval = so->so_incqlen;
2824 error = ENOPROTOOPT;
2835 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2837 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2839 struct mbuf *m, *m_prev;
2840 int sopt_size = sopt->sopt_valsize;
2842 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2845 if (sopt_size > MLEN) {
2846 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
2847 if ((m->m_flags & M_EXT) == 0) {
2851 m->m_len = min(MCLBYTES, sopt_size);
2853 m->m_len = min(MLEN, sopt_size);
2855 sopt_size -= m->m_len;
2860 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2865 if (sopt_size > MLEN) {
2866 MCLGET(m, sopt->sopt_td != NULL ? M_WAIT :
2868 if ((m->m_flags & M_EXT) == 0) {
2873 m->m_len = min(MCLBYTES, sopt_size);
2875 m->m_len = min(MLEN, sopt_size);
2877 sopt_size -= m->m_len;
2884 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2886 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2888 struct mbuf *m0 = m;
2890 if (sopt->sopt_val == NULL)
2892 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2893 if (sopt->sopt_td != NULL) {
2896 error = copyin(sopt->sopt_val, mtod(m, char *),
2903 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2904 sopt->sopt_valsize -= m->m_len;
2905 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2908 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2909 panic("ip6_sooptmcopyin");
2913 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2915 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2917 struct mbuf *m0 = m;
2920 if (sopt->sopt_val == NULL)
2922 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2923 if (sopt->sopt_td != NULL) {
2926 error = copyout(mtod(m, char *), sopt->sopt_val,
2933 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2934 sopt->sopt_valsize -= m->m_len;
2935 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2936 valsize += m->m_len;
2940 /* enough soopt buffer should be given from user-land */
2944 sopt->sopt_valsize = valsize;
2949 * sohasoutofband(): protocol notifies socket layer of the arrival of new
2950 * out-of-band data, which will then notify socket consumers.
2953 sohasoutofband(struct socket *so)
2956 if (so->so_sigio != NULL)
2957 pgsigio(&so->so_sigio, SIGURG, 0);
2958 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
2962 sopoll(struct socket *so, int events, struct ucred *active_cred,
2967 * We do not need to set or assert curvnet as long as everyone uses
2970 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
2975 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
2980 SOCKBUF_LOCK(&so->so_snd);
2981 SOCKBUF_LOCK(&so->so_rcv);
2982 if (events & (POLLIN | POLLRDNORM))
2983 if (soreadabledata(so))
2984 revents |= events & (POLLIN | POLLRDNORM);
2986 if (events & (POLLOUT | POLLWRNORM))
2987 if (sowriteable(so))
2988 revents |= events & (POLLOUT | POLLWRNORM);
2990 if (events & (POLLPRI | POLLRDBAND))
2991 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
2992 revents |= events & (POLLPRI | POLLRDBAND);
2994 if ((events & POLLINIGNEOF) == 0) {
2995 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2996 revents |= events & (POLLIN | POLLRDNORM);
2997 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
3003 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
3004 selrecord(td, &so->so_rcv.sb_sel);
3005 so->so_rcv.sb_flags |= SB_SEL;
3008 if (events & (POLLOUT | POLLWRNORM)) {
3009 selrecord(td, &so->so_snd.sb_sel);
3010 so->so_snd.sb_flags |= SB_SEL;
3014 SOCKBUF_UNLOCK(&so->so_rcv);
3015 SOCKBUF_UNLOCK(&so->so_snd);
3020 soo_kqfilter(struct file *fp, struct knote *kn)
3022 struct socket *so = kn->kn_fp->f_data;
3025 switch (kn->kn_filter) {
3027 if (so->so_options & SO_ACCEPTCONN)
3028 kn->kn_fop = &solisten_filtops;
3030 kn->kn_fop = &soread_filtops;
3034 kn->kn_fop = &sowrite_filtops;
3042 knlist_add(&sb->sb_sel.si_note, kn, 1);
3043 sb->sb_flags |= SB_KNOTE;
3049 * Some routines that return EOPNOTSUPP for entry points that are not
3050 * supported by a protocol. Fill in as needed.
3053 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
3060 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
3067 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3074 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3081 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
3088 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
3089 struct ifnet *ifp, struct thread *td)
3096 pru_disconnect_notsupp(struct socket *so)
3103 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
3110 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
3117 pru_rcvd_notsupp(struct socket *so, int flags)
3124 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
3131 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
3132 struct sockaddr *addr, struct mbuf *control, struct thread *td)
3139 * This isn't really a ``null'' operation, but it's the default one and
3140 * doesn't do anything destructive.
3143 pru_sense_null(struct socket *so, struct stat *sb)
3146 sb->st_blksize = so->so_snd.sb_hiwat;
3151 pru_shutdown_notsupp(struct socket *so)
3158 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
3165 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
3166 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
3173 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
3174 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
3181 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
3189 filt_sordetach(struct knote *kn)
3191 struct socket *so = kn->kn_fp->f_data;
3193 SOCKBUF_LOCK(&so->so_rcv);
3194 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
3195 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
3196 so->so_rcv.sb_flags &= ~SB_KNOTE;
3197 SOCKBUF_UNLOCK(&so->so_rcv);
3202 filt_soread(struct knote *kn, long hint)
3206 so = kn->kn_fp->f_data;
3207 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
3209 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
3210 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3211 kn->kn_flags |= EV_EOF;
3212 kn->kn_fflags = so->so_error;
3214 } else if (so->so_error) /* temporary udp error */
3216 else if (kn->kn_sfflags & NOTE_LOWAT)
3217 return (kn->kn_data >= kn->kn_sdata);
3219 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
3223 filt_sowdetach(struct knote *kn)
3225 struct socket *so = kn->kn_fp->f_data;
3227 SOCKBUF_LOCK(&so->so_snd);
3228 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
3229 if (knlist_empty(&so->so_snd.sb_sel.si_note))
3230 so->so_snd.sb_flags &= ~SB_KNOTE;
3231 SOCKBUF_UNLOCK(&so->so_snd);
3236 filt_sowrite(struct knote *kn, long hint)
3240 so = kn->kn_fp->f_data;
3241 SOCKBUF_LOCK_ASSERT(&so->so_snd);
3242 kn->kn_data = sbspace(&so->so_snd);
3243 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
3244 kn->kn_flags |= EV_EOF;
3245 kn->kn_fflags = so->so_error;
3247 } else if (so->so_error) /* temporary udp error */
3249 else if (((so->so_state & SS_ISCONNECTED) == 0) &&
3250 (so->so_proto->pr_flags & PR_CONNREQUIRED))
3252 else if (kn->kn_sfflags & NOTE_LOWAT)
3253 return (kn->kn_data >= kn->kn_sdata);
3255 return (kn->kn_data >= so->so_snd.sb_lowat);
3260 filt_solisten(struct knote *kn, long hint)
3262 struct socket *so = kn->kn_fp->f_data;
3264 kn->kn_data = so->so_qlen;
3265 return (! TAILQ_EMPTY(&so->so_comp));
3269 socheckuid(struct socket *so, uid_t uid)
3274 if (so->so_cred->cr_uid != uid)
3280 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
3286 error = sysctl_handle_int(oidp, &val, 0, req);
3287 if (error || !req->newptr )
3290 if (val < 1 || val > USHRT_MAX)
3298 * These functions are used by protocols to notify the socket layer (and its
3299 * consumers) of state changes in the sockets driven by protocol-side events.
3303 * Procedures to manipulate state flags of socket and do appropriate wakeups.
3305 * Normal sequence from the active (originating) side is that
3306 * soisconnecting() is called during processing of connect() call, resulting
3307 * in an eventual call to soisconnected() if/when the connection is
3308 * established. When the connection is torn down soisdisconnecting() is
3309 * called during processing of disconnect() call, and soisdisconnected() is
3310 * called when the connection to the peer is totally severed. The semantics
3311 * of these routines are such that connectionless protocols can call
3312 * soisconnected() and soisdisconnected() only, bypassing the in-progress
3313 * calls when setting up a ``connection'' takes no time.
3315 * From the passive side, a socket is created with two queues of sockets:
3316 * so_incomp for connections in progress and so_comp for connections already
3317 * made and awaiting user acceptance. As a protocol is preparing incoming
3318 * connections, it creates a socket structure queued on so_incomp by calling
3319 * sonewconn(). When the connection is established, soisconnected() is
3320 * called, and transfers the socket structure to so_comp, making it available
3323 * If a socket is closed with sockets on either so_incomp or so_comp, these
3324 * sockets are dropped.
3326 * If higher-level protocols are implemented in the kernel, the wakeups done
3327 * here will sometimes cause software-interrupt process scheduling.
3330 soisconnecting(struct socket *so)
3334 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3335 so->so_state |= SS_ISCONNECTING;
3340 soisconnected(struct socket *so)
3342 struct socket *head;
3348 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3349 so->so_state |= SS_ISCONNECTED;
3351 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
3352 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3354 TAILQ_REMOVE(&head->so_incomp, so, so_list);
3356 so->so_qstate &= ~SQ_INCOMP;
3357 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
3359 so->so_qstate |= SQ_COMP;
3362 wakeup_one(&head->so_timeo);
3365 soupcall_set(so, SO_RCV,
3366 head->so_accf->so_accept_filter->accf_callback,
3367 head->so_accf->so_accept_filter_arg);
3368 so->so_options &= ~SO_ACCEPTFILTER;
3369 ret = head->so_accf->so_accept_filter->accf_callback(so,
3370 head->so_accf->so_accept_filter_arg, M_DONTWAIT);
3371 if (ret == SU_ISCONNECTED)
3372 soupcall_clear(so, SO_RCV);
3374 if (ret == SU_ISCONNECTED)
3381 wakeup(&so->so_timeo);
3387 soisdisconnecting(struct socket *so)
3391 * Note: This code assumes that SOCK_LOCK(so) and
3392 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3394 SOCKBUF_LOCK(&so->so_rcv);
3395 so->so_state &= ~SS_ISCONNECTING;
3396 so->so_state |= SS_ISDISCONNECTING;
3397 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3398 sorwakeup_locked(so);
3399 SOCKBUF_LOCK(&so->so_snd);
3400 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3401 sowwakeup_locked(so);
3402 wakeup(&so->so_timeo);
3406 soisdisconnected(struct socket *so)
3410 * Note: This code assumes that SOCK_LOCK(so) and
3411 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3413 SOCKBUF_LOCK(&so->so_rcv);
3414 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3415 so->so_state |= SS_ISDISCONNECTED;
3416 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3417 sorwakeup_locked(so);
3418 SOCKBUF_LOCK(&so->so_snd);
3419 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3420 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
3421 sowwakeup_locked(so);
3422 wakeup(&so->so_timeo);
3426 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3429 sodupsockaddr(const struct sockaddr *sa, int mflags)
3431 struct sockaddr *sa2;
3433 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3435 bcopy(sa, sa2, sa->sa_len);
3440 * Register per-socket buffer upcalls.
3443 soupcall_set(struct socket *so, int which,
3444 int (*func)(struct socket *, void *, int), void *arg)
3456 panic("soupcall_set: bad which");
3458 SOCKBUF_LOCK_ASSERT(sb);
3460 /* XXX: accf_http actually wants to do this on purpose. */
3461 KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall"));
3463 sb->sb_upcall = func;
3464 sb->sb_upcallarg = arg;
3465 sb->sb_flags |= SB_UPCALL;
3469 soupcall_clear(struct socket *so, int which)
3481 panic("soupcall_clear: bad which");
3483 SOCKBUF_LOCK_ASSERT(sb);
3484 KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear"));
3485 sb->sb_upcall = NULL;
3486 sb->sb_upcallarg = NULL;
3487 sb->sb_flags &= ~SB_UPCALL;
3491 * Create an external-format (``xsocket'') structure using the information in
3492 * the kernel-format socket structure pointed to by so. This is done to
3493 * reduce the spew of irrelevant information over this interface, to isolate
3494 * user code from changes in the kernel structure, and potentially to provide
3495 * information-hiding if we decide that some of this information should be
3496 * hidden from users.
3499 sotoxsocket(struct socket *so, struct xsocket *xso)
3502 xso->xso_len = sizeof *xso;
3504 xso->so_type = so->so_type;
3505 xso->so_options = so->so_options;
3506 xso->so_linger = so->so_linger;
3507 xso->so_state = so->so_state;
3508 xso->so_pcb = so->so_pcb;
3509 xso->xso_protocol = so->so_proto->pr_protocol;
3510 xso->xso_family = so->so_proto->pr_domain->dom_family;
3511 xso->so_qlen = so->so_qlen;
3512 xso->so_incqlen = so->so_incqlen;
3513 xso->so_qlimit = so->so_qlimit;
3514 xso->so_timeo = so->so_timeo;
3515 xso->so_error = so->so_error;
3516 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
3517 xso->so_oobmark = so->so_oobmark;
3518 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
3519 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
3520 xso->so_uid = so->so_cred->cr_uid;
3525 * Socket accessor functions to provide external consumers with
3526 * a safe interface to socket state
3531 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg)
3534 TAILQ_FOREACH(so, &so->so_comp, so_list)
3539 so_sockbuf_rcv(struct socket *so)
3542 return (&so->so_rcv);
3546 so_sockbuf_snd(struct socket *so)
3549 return (&so->so_snd);
3553 so_state_get(const struct socket *so)
3556 return (so->so_state);
3560 so_state_set(struct socket *so, int val)
3567 so_options_get(const struct socket *so)
3570 return (so->so_options);
3574 so_options_set(struct socket *so, int val)
3577 so->so_options = val;
3581 so_error_get(const struct socket *so)
3584 return (so->so_error);
3588 so_error_set(struct socket *so, int val)
3595 so_linger_get(const struct socket *so)
3598 return (so->so_linger);
3602 so_linger_set(struct socket *so, int val)
3605 so->so_linger = val;
3609 so_protosw_get(const struct socket *so)
3612 return (so->so_proto);
3616 so_protosw_set(struct socket *so, struct protosw *val)
3623 so_sorwakeup(struct socket *so)
3630 so_sowwakeup(struct socket *so)
3637 so_sorwakeup_locked(struct socket *so)
3640 sorwakeup_locked(so);
3644 so_sowwakeup_locked(struct socket *so)
3647 sowwakeup_locked(so);
3651 so_lock(struct socket *so)
3657 so_unlock(struct socket *so)