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_INET6) ||
396 (prp->pr_domain->dom_family == PF_ROUTE))
397 so->so_fibnum = td->td_proc->p_fibnum;
402 mac_socket_create(cred, so);
404 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
405 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
408 * Auto-sizing of socket buffers is managed by the protocols and
409 * the appropriate flags must be set in the pru_attach function.
411 CURVNET_SET(so->so_vnet);
412 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
415 KASSERT(so->so_count == 1, ("socreate: so_count %d",
426 static int regression_sonewconn_earlytest = 1;
427 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
428 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
432 * When an attempt at a new connection is noted on a socket which accepts
433 * connections, sonewconn is called. If the connection is possible (subject
434 * to space constraints, etc.) then we allocate a new structure, propoerly
435 * linked into the data structure of the original socket, and return this.
436 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
438 * Note: the ref count on the socket is 0 on return.
441 sonewconn(struct socket *head, int connstatus)
447 over = (head->so_qlen > 3 * head->so_qlimit / 2);
450 if (regression_sonewconn_earlytest && over)
455 VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
456 __func__, __LINE__, head));
457 so = soalloc(head->so_vnet);
460 if ((head->so_options & SO_ACCEPTFILTER) != 0)
463 so->so_type = head->so_type;
464 so->so_options = head->so_options &~ SO_ACCEPTCONN;
465 so->so_linger = head->so_linger;
466 so->so_state = head->so_state | SS_NOFDREF;
467 so->so_fibnum = head->so_fibnum;
468 so->so_proto = head->so_proto;
469 so->so_cred = crhold(head->so_cred);
471 mac_socket_newconn(head, so);
473 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
474 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
475 VNET_SO_ASSERT(head);
476 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
477 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
481 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
482 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
483 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
484 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
485 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
486 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
487 so->so_state |= connstatus;
490 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
491 so->so_qstate |= SQ_COMP;
495 * Keep removing sockets from the head until there's room for
496 * us to insert on the tail. In pre-locking revisions, this
497 * was a simple if(), but as we could be racing with other
498 * threads and soabort() requires dropping locks, we must
499 * loop waiting for the condition to be true.
501 while (head->so_incqlen > head->so_qlimit) {
503 sp = TAILQ_FIRST(&head->so_incomp);
504 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
506 sp->so_qstate &= ~SQ_INCOMP;
512 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
513 so->so_qstate |= SQ_INCOMP;
519 wakeup_one(&head->so_timeo);
525 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
529 CURVNET_SET(so->so_vnet);
530 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
536 * solisten() transitions a socket from a non-listening state to a listening
537 * state, but can also be used to update the listen queue depth on an
538 * existing listen socket. The protocol will call back into the sockets
539 * layer using solisten_proto_check() and solisten_proto() to check and set
540 * socket-layer listen state. Call backs are used so that the protocol can
541 * acquire both protocol and socket layer locks in whatever order is required
544 * Protocol implementors are advised to hold the socket lock across the
545 * socket-layer test and set to avoid races at the socket layer.
548 solisten(struct socket *so, int backlog, struct thread *td)
552 CURVNET_SET(so->so_vnet);
553 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td);
559 solisten_proto_check(struct socket *so)
562 SOCK_LOCK_ASSERT(so);
564 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
571 solisten_proto(struct socket *so, int backlog)
574 SOCK_LOCK_ASSERT(so);
576 if (backlog < 0 || backlog > somaxconn)
578 so->so_qlimit = backlog;
579 so->so_options |= SO_ACCEPTCONN;
583 * Evaluate the reference count and named references on a socket; if no
584 * references remain, free it. This should be called whenever a reference is
585 * released, such as in sorele(), but also when named reference flags are
586 * cleared in socket or protocol code.
588 * sofree() will free the socket if:
590 * - There are no outstanding file descriptor references or related consumers
593 * - The socket has been closed by user space, if ever open (SS_NOFDREF).
595 * - The protocol does not have an outstanding strong reference on the socket
598 * - The socket is not in a completed connection queue, so a process has been
599 * notified that it is present. If it is removed, the user process may
600 * block in accept() despite select() saying the socket was ready.
603 sofree(struct socket *so)
605 struct protosw *pr = so->so_proto;
608 ACCEPT_LOCK_ASSERT();
609 SOCK_LOCK_ASSERT(so);
611 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
612 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
620 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
621 (so->so_qstate & SQ_INCOMP) != 0,
622 ("sofree: so_head != NULL, but neither SQ_COMP nor "
624 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
625 (so->so_qstate & SQ_INCOMP) == 0,
626 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
627 TAILQ_REMOVE(&head->so_incomp, so, so_list);
629 so->so_qstate &= ~SQ_INCOMP;
632 KASSERT((so->so_qstate & SQ_COMP) == 0 &&
633 (so->so_qstate & SQ_INCOMP) == 0,
634 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
635 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
636 if (so->so_options & SO_ACCEPTCONN) {
637 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
638 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated"));
644 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
645 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
646 if (pr->pr_usrreqs->pru_detach != NULL)
647 (*pr->pr_usrreqs->pru_detach)(so);
650 * From this point on, we assume that no other references to this
651 * socket exist anywhere else in the stack. Therefore, no locks need
652 * to be acquired or held.
654 * We used to do a lot of socket buffer and socket locking here, as
655 * well as invoke sorflush() and perform wakeups. The direct call to
656 * dom_dispose() and sbrelease_internal() are an inlining of what was
657 * necessary from sorflush().
659 * Notice that the socket buffer and kqueue state are torn down
660 * before calling pru_detach. This means that protocols shold not
661 * assume they can perform socket wakeups, etc, in their detach code.
663 sbdestroy(&so->so_snd, so);
664 sbdestroy(&so->so_rcv, so);
665 seldrain(&so->so_snd.sb_sel);
666 seldrain(&so->so_rcv.sb_sel);
667 knlist_destroy(&so->so_rcv.sb_sel.si_note);
668 knlist_destroy(&so->so_snd.sb_sel.si_note);
673 * Close a socket on last file table reference removal. Initiate disconnect
674 * if connected. Free socket when disconnect complete.
676 * This function will sorele() the socket. Note that soclose() may be called
677 * prior to the ref count reaching zero. The actual socket structure will
678 * not be freed until the ref count reaches zero.
681 soclose(struct socket *so)
685 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
687 CURVNET_SET(so->so_vnet);
688 funsetown(&so->so_sigio);
689 if (so->so_state & SS_ISCONNECTED) {
690 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
691 error = sodisconnect(so);
693 if (error == ENOTCONN)
698 if (so->so_options & SO_LINGER) {
699 if ((so->so_state & SS_ISDISCONNECTING) &&
700 (so->so_state & SS_NBIO))
702 while (so->so_state & SS_ISCONNECTED) {
703 error = tsleep(&so->so_timeo,
704 PSOCK | PCATCH, "soclos", so->so_linger * hz);
712 if (so->so_proto->pr_usrreqs->pru_close != NULL)
713 (*so->so_proto->pr_usrreqs->pru_close)(so);
714 if (so->so_options & SO_ACCEPTCONN) {
717 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
718 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
720 sp->so_qstate &= ~SQ_INCOMP;
726 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
727 TAILQ_REMOVE(&so->so_comp, sp, so_list);
729 sp->so_qstate &= ~SQ_COMP;
739 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
740 so->so_state |= SS_NOFDREF;
747 * soabort() is used to abruptly tear down a connection, such as when a
748 * resource limit is reached (listen queue depth exceeded), or if a listen
749 * socket is closed while there are sockets waiting to be accepted.
751 * This interface is tricky, because it is called on an unreferenced socket,
752 * and must be called only by a thread that has actually removed the socket
753 * from the listen queue it was on, or races with other threads are risked.
755 * This interface will call into the protocol code, so must not be called
756 * with any socket locks held. Protocols do call it while holding their own
757 * recursible protocol mutexes, but this is something that should be subject
758 * to review in the future.
761 soabort(struct socket *so)
765 * In as much as is possible, assert that no references to this
766 * socket are held. This is not quite the same as asserting that the
767 * current thread is responsible for arranging for no references, but
768 * is as close as we can get for now.
770 KASSERT(so->so_count == 0, ("soabort: so_count"));
771 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
772 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
773 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
774 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
777 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
778 (*so->so_proto->pr_usrreqs->pru_abort)(so);
785 soaccept(struct socket *so, struct sockaddr **nam)
790 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
791 so->so_state &= ~SS_NOFDREF;
794 CURVNET_SET(so->so_vnet);
795 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
801 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
805 if (so->so_options & SO_ACCEPTCONN)
808 CURVNET_SET(so->so_vnet);
810 * If protocol is connection-based, can only connect once.
811 * Otherwise, if connected, try to disconnect first. This allows
812 * user to disconnect by connecting to, e.g., a null address.
814 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
815 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
816 (error = sodisconnect(so)))) {
820 * Prevent accumulated error from previous connection from
824 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
832 soconnect2(struct socket *so1, struct socket *so2)
836 CURVNET_SET(so1->so_vnet);
837 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
843 sodisconnect(struct socket *so)
847 if ((so->so_state & SS_ISCONNECTED) == 0)
849 if (so->so_state & SS_ISDISCONNECTING)
852 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
856 #ifdef ZERO_COPY_SOCKETS
857 struct so_zerocopy_stats{
862 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
863 #include <netinet/in.h>
864 #include <net/route.h>
865 #include <netinet/in_pcb.h>
867 #include <vm/vm_page.h>
868 #include <vm/vm_object.h>
871 * sosend_copyin() is only used if zero copy sockets are enabled. Otherwise
872 * sosend_dgram() and sosend_generic() use m_uiotombuf().
874 * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or
875 * all of the data referenced by the uio. If desired, it uses zero-copy.
876 * *space will be updated to reflect data copied in.
878 * NB: If atomic I/O is requested, the caller must already have checked that
879 * space can hold resid bytes.
881 * NB: In the event of an error, the caller may need to free the partial
882 * chain pointed to by *mpp. The contents of both *uio and *space may be
883 * modified even in the case of an error.
886 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space,
889 struct mbuf *m, **mp, *top;
893 #ifdef ZERO_COPY_SOCKETS
900 resid = uio->uio_resid;
903 #ifdef ZERO_COPY_SOCKETS
905 #endif /* ZERO_COPY_SOCKETS */
906 if (resid >= MINCLSIZE) {
907 #ifdef ZERO_COPY_SOCKETS
909 m = m_gethdr(M_WAITOK, MT_DATA);
911 m->m_pkthdr.rcvif = NULL;
913 m = m_get(M_WAITOK, MT_DATA);
914 if (so_zero_copy_send &&
917 uio->uio_iov->iov_len>=PAGE_SIZE) {
918 so_zerocp_stats.size_ok++;
919 so_zerocp_stats.align_ok++;
920 cow_send = socow_setup(m, uio);
924 m_clget(m, M_WAITOK);
925 len = min(min(MCLBYTES, resid), *space);
927 #else /* ZERO_COPY_SOCKETS */
929 m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
931 m->m_pkthdr.rcvif = NULL;
933 m = m_getcl(M_WAIT, MT_DATA, 0);
934 len = min(min(MCLBYTES, resid), *space);
935 #endif /* ZERO_COPY_SOCKETS */
938 m = m_gethdr(M_WAIT, MT_DATA);
940 m->m_pkthdr.rcvif = NULL;
942 len = min(min(MHLEN, resid), *space);
944 * For datagram protocols, leave room
945 * for protocol headers in first mbuf.
947 if (atomic && m && len < MHLEN)
950 m = m_get(M_WAIT, MT_DATA);
951 len = min(min(MLEN, resid), *space);
960 #ifdef ZERO_COPY_SOCKETS
964 #endif /* ZERO_COPY_SOCKETS */
965 error = uiomove(mtod(m, void *), (int)len, uio);
966 resid = uio->uio_resid;
969 top->m_pkthdr.len += len;
975 top->m_flags |= M_EOR;
978 } while (*space > 0 && atomic);
983 #endif /*ZERO_COPY_SOCKETS*/
985 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
988 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
989 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
993 int clen = 0, error, dontroute;
994 #ifdef ZERO_COPY_SOCKETS
995 int atomic = sosendallatonce(so) || top;
998 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
999 KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
1000 ("sodgram_send: !PR_ATOMIC"));
1003 resid = uio->uio_resid;
1005 resid = top->m_pkthdr.len;
1007 * In theory resid should be unsigned. However, space must be
1008 * signed, as it might be less than 0 if we over-committed, and we
1009 * must use a signed comparison of space and resid. On the other
1010 * hand, a negative resid causes us to loop sending 0-length
1011 * segments to the protocol.
1019 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
1021 td->td_ru.ru_msgsnd++;
1022 if (control != NULL)
1023 clen = control->m_len;
1025 SOCKBUF_LOCK(&so->so_snd);
1026 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1027 SOCKBUF_UNLOCK(&so->so_snd);
1032 error = so->so_error;
1034 SOCKBUF_UNLOCK(&so->so_snd);
1037 if ((so->so_state & SS_ISCONNECTED) == 0) {
1039 * `sendto' and `sendmsg' is allowed on a connection-based
1040 * socket if it supports implied connect. Return ENOTCONN if
1041 * not connected and no address is supplied.
1043 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1044 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1045 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1046 !(resid == 0 && clen != 0)) {
1047 SOCKBUF_UNLOCK(&so->so_snd);
1051 } else if (addr == NULL) {
1052 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1055 error = EDESTADDRREQ;
1056 SOCKBUF_UNLOCK(&so->so_snd);
1062 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
1063 * problem and need fixing.
1065 space = sbspace(&so->so_snd);
1066 if (flags & MSG_OOB)
1069 SOCKBUF_UNLOCK(&so->so_snd);
1070 if (resid > space) {
1076 if (flags & MSG_EOR)
1077 top->m_flags |= M_EOR;
1079 #ifdef ZERO_COPY_SOCKETS
1080 error = sosend_copyin(uio, &top, atomic, &space, flags);
1085 * Copy the data from userland into a mbuf chain.
1086 * If no data is to be copied in, a single empty mbuf
1089 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1090 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1092 error = EFAULT; /* only possible error */
1095 space -= resid - uio->uio_resid;
1097 resid = uio->uio_resid;
1099 KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1101 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1106 so->so_options |= SO_DONTROUTE;
1110 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1111 * of date. We could have recieved a reset packet in an interrupt or
1112 * maybe we slept while doing page faults in uiomove() etc. We could
1113 * probably recheck again inside the locking protection here, but
1114 * there are probably other places that this also happens. We must
1118 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1119 (flags & MSG_OOB) ? PRUS_OOB :
1121 * If the user set MSG_EOF, the protocol understands this flag and
1122 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1124 ((flags & MSG_EOF) &&
1125 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1128 /* If there is more to send set PRUS_MORETOCOME */
1129 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1130 top, addr, control, td);
1133 so->so_options &= ~SO_DONTROUTE;
1142 if (control != NULL)
1148 * Send on a socket. If send must go all at once and message is larger than
1149 * send buffering, then hard error. Lock against other senders. If must go
1150 * all at once and not enough room now, then inform user that this would
1151 * block and do nothing. Otherwise, if nonblocking, send as much as
1152 * possible. The data to be sent is described by "uio" if nonzero, otherwise
1153 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1154 * in mbuf chain must be small enough to send all at once.
1156 * Returns nonzero on error, timeout or signal; callers must check for short
1157 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1161 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1162 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1166 int clen = 0, error, dontroute;
1167 int atomic = sosendallatonce(so) || top;
1170 resid = uio->uio_resid;
1172 resid = top->m_pkthdr.len;
1174 * In theory resid should be unsigned. However, space must be
1175 * signed, as it might be less than 0 if we over-committed, and we
1176 * must use a signed comparison of space and resid. On the other
1177 * hand, a negative resid causes us to loop sending 0-length
1178 * segments to the protocol.
1180 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1181 * type sockets since that's an error.
1183 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1189 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1190 (so->so_proto->pr_flags & PR_ATOMIC);
1192 td->td_ru.ru_msgsnd++;
1193 if (control != NULL)
1194 clen = control->m_len;
1196 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1202 SOCKBUF_LOCK(&so->so_snd);
1203 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1204 SOCKBUF_UNLOCK(&so->so_snd);
1209 error = so->so_error;
1211 SOCKBUF_UNLOCK(&so->so_snd);
1214 if ((so->so_state & SS_ISCONNECTED) == 0) {
1216 * `sendto' and `sendmsg' is allowed on a connection-
1217 * based socket if it supports implied connect.
1218 * Return ENOTCONN if not connected and no address is
1221 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1222 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1223 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1224 !(resid == 0 && clen != 0)) {
1225 SOCKBUF_UNLOCK(&so->so_snd);
1229 } else if (addr == NULL) {
1230 SOCKBUF_UNLOCK(&so->so_snd);
1231 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1234 error = EDESTADDRREQ;
1238 space = sbspace(&so->so_snd);
1239 if (flags & MSG_OOB)
1241 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1242 clen > so->so_snd.sb_hiwat) {
1243 SOCKBUF_UNLOCK(&so->so_snd);
1247 if (space < resid + clen &&
1248 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1249 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
1250 SOCKBUF_UNLOCK(&so->so_snd);
1251 error = EWOULDBLOCK;
1254 error = sbwait(&so->so_snd);
1255 SOCKBUF_UNLOCK(&so->so_snd);
1260 SOCKBUF_UNLOCK(&so->so_snd);
1265 if (flags & MSG_EOR)
1266 top->m_flags |= M_EOR;
1268 #ifdef ZERO_COPY_SOCKETS
1269 error = sosend_copyin(uio, &top, atomic,
1275 * Copy the data from userland into a mbuf
1276 * chain. If no data is to be copied in,
1277 * a single empty mbuf is returned.
1279 top = m_uiotombuf(uio, M_WAITOK, space,
1280 (atomic ? max_hdr : 0),
1281 (atomic ? M_PKTHDR : 0) |
1282 ((flags & MSG_EOR) ? M_EOR : 0));
1284 error = EFAULT; /* only possible error */
1287 space -= resid - uio->uio_resid;
1289 resid = uio->uio_resid;
1293 so->so_options |= SO_DONTROUTE;
1297 * XXX all the SBS_CANTSENDMORE checks previously
1298 * done could be out of date. We could have recieved
1299 * a reset packet in an interrupt or maybe we slept
1300 * while doing page faults in uiomove() etc. We
1301 * could probably recheck again inside the locking
1302 * protection here, but there are probably other
1303 * places that this also happens. We must rethink
1307 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1308 (flags & MSG_OOB) ? PRUS_OOB :
1310 * If the user set MSG_EOF, the protocol understands
1311 * this flag and nothing left to send then use
1312 * PRU_SEND_EOF instead of PRU_SEND.
1314 ((flags & MSG_EOF) &&
1315 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1318 /* If there is more to send set PRUS_MORETOCOME. */
1319 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1320 top, addr, control, td);
1323 so->so_options &= ~SO_DONTROUTE;
1331 } while (resid && space > 0);
1335 sbunlock(&so->so_snd);
1339 if (control != NULL)
1345 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1346 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1350 CURVNET_SET(so->so_vnet);
1351 error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1352 control, flags, td);
1358 * The part of soreceive() that implements reading non-inline out-of-band
1359 * data from a socket. For more complete comments, see soreceive(), from
1360 * which this code originated.
1362 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1363 * unable to return an mbuf chain to the caller.
1366 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1368 struct protosw *pr = so->so_proto;
1372 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1375 m = m_get(M_WAIT, MT_DATA);
1376 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1380 #ifdef ZERO_COPY_SOCKETS
1381 if (so_zero_copy_receive) {
1384 if ((m->m_flags & M_EXT)
1385 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1390 error = uiomoveco(mtod(m, void *),
1391 min(uio->uio_resid, m->m_len),
1394 #endif /* ZERO_COPY_SOCKETS */
1395 error = uiomove(mtod(m, void *),
1396 (int) min(uio->uio_resid, m->m_len), uio);
1398 } while (uio->uio_resid && error == 0 && m);
1406 * Following replacement or removal of the first mbuf on the first mbuf chain
1407 * of a socket buffer, push necessary state changes back into the socket
1408 * buffer so that other consumers see the values consistently. 'nextrecord'
1409 * is the callers locally stored value of the original value of
1410 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1411 * NOTE: 'nextrecord' may be NULL.
1413 static __inline void
1414 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1417 SOCKBUF_LOCK_ASSERT(sb);
1419 * First, update for the new value of nextrecord. If necessary, make
1420 * it the first record.
1422 if (sb->sb_mb != NULL)
1423 sb->sb_mb->m_nextpkt = nextrecord;
1425 sb->sb_mb = nextrecord;
1428 * Now update any dependent socket buffer fields to reflect the new
1429 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
1430 * addition of a second clause that takes care of the case where
1431 * sb_mb has been updated, but remains the last record.
1433 if (sb->sb_mb == NULL) {
1434 sb->sb_mbtail = NULL;
1435 sb->sb_lastrecord = NULL;
1436 } else if (sb->sb_mb->m_nextpkt == NULL)
1437 sb->sb_lastrecord = sb->sb_mb;
1442 * Implement receive operations on a socket. We depend on the way that
1443 * records are added to the sockbuf by sbappend. In particular, each record
1444 * (mbufs linked through m_next) must begin with an address if the protocol
1445 * so specifies, followed by an optional mbuf or mbufs containing ancillary
1446 * data, and then zero or more mbufs of data. In order to allow parallelism
1447 * between network receive and copying to user space, as well as avoid
1448 * sleeping with a mutex held, we release the socket buffer mutex during the
1449 * user space copy. Although the sockbuf is locked, new data may still be
1450 * appended, and thus we must maintain consistency of the sockbuf during that
1453 * The caller may receive the data as a single mbuf chain by supplying an
1454 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1455 * the count in uio_resid.
1458 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1459 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1461 struct mbuf *m, **mp;
1462 int flags, error, offset;
1464 struct protosw *pr = so->so_proto;
1465 struct mbuf *nextrecord;
1467 ssize_t orig_resid = uio->uio_resid;
1472 if (controlp != NULL)
1475 flags = *flagsp &~ MSG_EOR;
1478 if (flags & MSG_OOB)
1479 return (soreceive_rcvoob(so, uio, flags));
1482 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1483 && uio->uio_resid) {
1485 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1488 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1493 SOCKBUF_LOCK(&so->so_rcv);
1494 m = so->so_rcv.sb_mb;
1496 * If we have less data than requested, block awaiting more (subject
1497 * to any timeout) if:
1498 * 1. the current count is less than the low water mark, or
1499 * 2. MSG_WAITALL is set, and it is possible to do the entire
1500 * receive operation at once if we block (resid <= hiwat).
1501 * 3. MSG_DONTWAIT is not set
1502 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1503 * we have to do the receive in sections, and thus risk returning a
1504 * short count if a timeout or signal occurs after we start.
1506 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1507 so->so_rcv.sb_cc < uio->uio_resid) &&
1508 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1509 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1510 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1511 KASSERT(m != NULL || !so->so_rcv.sb_cc,
1512 ("receive: m == %p so->so_rcv.sb_cc == %u",
1513 m, so->so_rcv.sb_cc));
1517 error = so->so_error;
1518 if ((flags & MSG_PEEK) == 0)
1520 SOCKBUF_UNLOCK(&so->so_rcv);
1523 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1524 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1526 SOCKBUF_UNLOCK(&so->so_rcv);
1531 for (; m != NULL; m = m->m_next)
1532 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1533 m = so->so_rcv.sb_mb;
1536 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1537 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1538 SOCKBUF_UNLOCK(&so->so_rcv);
1542 if (uio->uio_resid == 0) {
1543 SOCKBUF_UNLOCK(&so->so_rcv);
1546 if ((so->so_state & SS_NBIO) ||
1547 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1548 SOCKBUF_UNLOCK(&so->so_rcv);
1549 error = EWOULDBLOCK;
1552 SBLASTRECORDCHK(&so->so_rcv);
1553 SBLASTMBUFCHK(&so->so_rcv);
1554 error = sbwait(&so->so_rcv);
1555 SOCKBUF_UNLOCK(&so->so_rcv);
1562 * From this point onward, we maintain 'nextrecord' as a cache of the
1563 * pointer to the next record in the socket buffer. We must keep the
1564 * various socket buffer pointers and local stack versions of the
1565 * pointers in sync, pushing out modifications before dropping the
1566 * socket buffer mutex, and re-reading them when picking it up.
1568 * Otherwise, we will race with the network stack appending new data
1569 * or records onto the socket buffer by using inconsistent/stale
1570 * versions of the field, possibly resulting in socket buffer
1573 * By holding the high-level sblock(), we prevent simultaneous
1574 * readers from pulling off the front of the socket buffer.
1576 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1578 uio->uio_td->td_ru.ru_msgrcv++;
1579 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1580 SBLASTRECORDCHK(&so->so_rcv);
1581 SBLASTMBUFCHK(&so->so_rcv);
1582 nextrecord = m->m_nextpkt;
1583 if (pr->pr_flags & PR_ADDR) {
1584 KASSERT(m->m_type == MT_SONAME,
1585 ("m->m_type == %d", m->m_type));
1588 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1590 if (flags & MSG_PEEK) {
1593 sbfree(&so->so_rcv, m);
1594 so->so_rcv.sb_mb = m_free(m);
1595 m = so->so_rcv.sb_mb;
1596 sockbuf_pushsync(&so->so_rcv, nextrecord);
1601 * Process one or more MT_CONTROL mbufs present before any data mbufs
1602 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1603 * just copy the data; if !MSG_PEEK, we call into the protocol to
1604 * perform externalization (or freeing if controlp == NULL).
1606 if (m != NULL && m->m_type == MT_CONTROL) {
1607 struct mbuf *cm = NULL, *cmn;
1608 struct mbuf **cme = &cm;
1611 if (flags & MSG_PEEK) {
1612 if (controlp != NULL) {
1613 *controlp = m_copy(m, 0, m->m_len);
1614 controlp = &(*controlp)->m_next;
1618 sbfree(&so->so_rcv, m);
1619 so->so_rcv.sb_mb = m->m_next;
1622 cme = &(*cme)->m_next;
1623 m = so->so_rcv.sb_mb;
1625 } while (m != NULL && m->m_type == MT_CONTROL);
1626 if ((flags & MSG_PEEK) == 0)
1627 sockbuf_pushsync(&so->so_rcv, nextrecord);
1628 while (cm != NULL) {
1631 if (pr->pr_domain->dom_externalize != NULL) {
1632 SOCKBUF_UNLOCK(&so->so_rcv);
1634 error = (*pr->pr_domain->dom_externalize)
1636 SOCKBUF_LOCK(&so->so_rcv);
1637 } else if (controlp != NULL)
1641 if (controlp != NULL) {
1643 while (*controlp != NULL)
1644 controlp = &(*controlp)->m_next;
1649 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1651 nextrecord = so->so_rcv.sb_mb;
1655 if ((flags & MSG_PEEK) == 0) {
1656 KASSERT(m->m_nextpkt == nextrecord,
1657 ("soreceive: post-control, nextrecord !sync"));
1658 if (nextrecord == NULL) {
1659 KASSERT(so->so_rcv.sb_mb == m,
1660 ("soreceive: post-control, sb_mb!=m"));
1661 KASSERT(so->so_rcv.sb_lastrecord == m,
1662 ("soreceive: post-control, lastrecord!=m"));
1666 if (type == MT_OOBDATA)
1669 if ((flags & MSG_PEEK) == 0) {
1670 KASSERT(so->so_rcv.sb_mb == nextrecord,
1671 ("soreceive: sb_mb != nextrecord"));
1672 if (so->so_rcv.sb_mb == NULL) {
1673 KASSERT(so->so_rcv.sb_lastrecord == NULL,
1674 ("soreceive: sb_lastercord != NULL"));
1678 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1679 SBLASTRECORDCHK(&so->so_rcv);
1680 SBLASTMBUFCHK(&so->so_rcv);
1683 * Now continue to read any data mbufs off of the head of the socket
1684 * buffer until the read request is satisfied. Note that 'type' is
1685 * used to store the type of any mbuf reads that have happened so far
1686 * such that soreceive() can stop reading if the type changes, which
1687 * causes soreceive() to return only one of regular data and inline
1688 * out-of-band data in a single socket receive operation.
1692 while (m != NULL && uio->uio_resid > 0 && error == 0) {
1694 * If the type of mbuf has changed since the last mbuf
1695 * examined ('type'), end the receive operation.
1697 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1698 if (m->m_type == MT_OOBDATA) {
1699 if (type != MT_OOBDATA)
1701 } else if (type == MT_OOBDATA)
1704 KASSERT(m->m_type == MT_DATA,
1705 ("m->m_type == %d", m->m_type));
1706 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1707 len = uio->uio_resid;
1708 if (so->so_oobmark && len > so->so_oobmark - offset)
1709 len = so->so_oobmark - offset;
1710 if (len > m->m_len - moff)
1711 len = m->m_len - moff;
1713 * If mp is set, just pass back the mbufs. Otherwise copy
1714 * them out via the uio, then free. Sockbuf must be
1715 * consistent here (points to current mbuf, it points to next
1716 * record) when we drop priority; we must note any additions
1717 * to the sockbuf when we block interrupts again.
1720 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1721 SBLASTRECORDCHK(&so->so_rcv);
1722 SBLASTMBUFCHK(&so->so_rcv);
1723 SOCKBUF_UNLOCK(&so->so_rcv);
1724 #ifdef ZERO_COPY_SOCKETS
1725 if (so_zero_copy_receive) {
1728 if ((m->m_flags & M_EXT)
1729 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1734 error = uiomoveco(mtod(m, char *) + moff,
1738 #endif /* ZERO_COPY_SOCKETS */
1739 error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1740 SOCKBUF_LOCK(&so->so_rcv);
1743 * The MT_SONAME mbuf has already been removed
1744 * from the record, so it is necessary to
1745 * remove the data mbufs, if any, to preserve
1746 * the invariant in the case of PR_ADDR that
1747 * requires MT_SONAME mbufs at the head of
1750 if (m && pr->pr_flags & PR_ATOMIC &&
1751 ((flags & MSG_PEEK) == 0))
1752 (void)sbdroprecord_locked(&so->so_rcv);
1753 SOCKBUF_UNLOCK(&so->so_rcv);
1757 uio->uio_resid -= len;
1758 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1759 if (len == m->m_len - moff) {
1760 if (m->m_flags & M_EOR)
1762 if (flags & MSG_PEEK) {
1766 nextrecord = m->m_nextpkt;
1767 sbfree(&so->so_rcv, m);
1771 so->so_rcv.sb_mb = m = m->m_next;
1774 so->so_rcv.sb_mb = m_free(m);
1775 m = so->so_rcv.sb_mb;
1777 sockbuf_pushsync(&so->so_rcv, nextrecord);
1778 SBLASTRECORDCHK(&so->so_rcv);
1779 SBLASTMBUFCHK(&so->so_rcv);
1782 if (flags & MSG_PEEK)
1788 if (flags & MSG_DONTWAIT)
1789 copy_flag = M_DONTWAIT;
1792 if (copy_flag == M_WAIT)
1793 SOCKBUF_UNLOCK(&so->so_rcv);
1794 *mp = m_copym(m, 0, len, copy_flag);
1795 if (copy_flag == M_WAIT)
1796 SOCKBUF_LOCK(&so->so_rcv);
1799 * m_copym() couldn't
1800 * allocate an mbuf. Adjust
1801 * uio_resid back (it was
1802 * adjusted down by len
1803 * bytes, which we didn't end
1804 * up "copying" over).
1806 uio->uio_resid += len;
1812 so->so_rcv.sb_cc -= len;
1815 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1816 if (so->so_oobmark) {
1817 if ((flags & MSG_PEEK) == 0) {
1818 so->so_oobmark -= len;
1819 if (so->so_oobmark == 0) {
1820 so->so_rcv.sb_state |= SBS_RCVATMARK;
1825 if (offset == so->so_oobmark)
1829 if (flags & MSG_EOR)
1832 * If the MSG_WAITALL flag is set (for non-atomic socket), we
1833 * must not quit until "uio->uio_resid == 0" or an error
1834 * termination. If a signal/timeout occurs, return with a
1835 * short count but without error. Keep sockbuf locked
1836 * against other readers.
1838 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1839 !sosendallatonce(so) && nextrecord == NULL) {
1840 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1841 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
1844 * Notify the protocol that some data has been
1845 * drained before blocking.
1847 if (pr->pr_flags & PR_WANTRCVD) {
1848 SOCKBUF_UNLOCK(&so->so_rcv);
1850 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1851 SOCKBUF_LOCK(&so->so_rcv);
1853 SBLASTRECORDCHK(&so->so_rcv);
1854 SBLASTMBUFCHK(&so->so_rcv);
1856 * We could receive some data while was notifying
1857 * the protocol. Skip blocking in this case.
1859 if (so->so_rcv.sb_mb == NULL) {
1860 error = sbwait(&so->so_rcv);
1862 SOCKBUF_UNLOCK(&so->so_rcv);
1866 m = so->so_rcv.sb_mb;
1868 nextrecord = m->m_nextpkt;
1872 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1873 if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1875 if ((flags & MSG_PEEK) == 0)
1876 (void) sbdroprecord_locked(&so->so_rcv);
1878 if ((flags & MSG_PEEK) == 0) {
1881 * First part is an inline SB_EMPTY_FIXUP(). Second
1882 * part makes sure sb_lastrecord is up-to-date if
1883 * there is still data in the socket buffer.
1885 so->so_rcv.sb_mb = nextrecord;
1886 if (so->so_rcv.sb_mb == NULL) {
1887 so->so_rcv.sb_mbtail = NULL;
1888 so->so_rcv.sb_lastrecord = NULL;
1889 } else if (nextrecord->m_nextpkt == NULL)
1890 so->so_rcv.sb_lastrecord = nextrecord;
1892 SBLASTRECORDCHK(&so->so_rcv);
1893 SBLASTMBUFCHK(&so->so_rcv);
1895 * If soreceive() is being done from the socket callback,
1896 * then don't need to generate ACK to peer to update window,
1897 * since ACK will be generated on return to TCP.
1899 if (!(flags & MSG_SOCALLBCK) &&
1900 (pr->pr_flags & PR_WANTRCVD)) {
1901 SOCKBUF_UNLOCK(&so->so_rcv);
1903 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1904 SOCKBUF_LOCK(&so->so_rcv);
1907 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1908 if (orig_resid == uio->uio_resid && orig_resid &&
1909 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1910 SOCKBUF_UNLOCK(&so->so_rcv);
1913 SOCKBUF_UNLOCK(&so->so_rcv);
1918 sbunlock(&so->so_rcv);
1923 * Optimized version of soreceive() for stream (TCP) sockets.
1926 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
1927 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1929 int len = 0, error = 0, flags, oresid;
1931 struct mbuf *m, *n = NULL;
1933 /* We only do stream sockets. */
1934 if (so->so_type != SOCK_STREAM)
1938 if (controlp != NULL)
1941 flags = *flagsp &~ MSG_EOR;
1944 if (flags & MSG_OOB)
1945 return (soreceive_rcvoob(so, uio, flags));
1951 /* Prevent other readers from entering the socket. */
1952 error = sblock(sb, SBLOCKWAIT(flags));
1957 /* Easy one, no space to copyout anything. */
1958 if (uio->uio_resid == 0) {
1962 oresid = uio->uio_resid;
1964 /* We will never ever get anything unless we are or were connected. */
1965 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1971 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1973 /* Abort if socket has reported problems. */
1977 if (oresid > uio->uio_resid)
1979 error = so->so_error;
1980 if (!(flags & MSG_PEEK))
1985 /* Door is closed. Deliver what is left, if any. */
1986 if (sb->sb_state & SBS_CANTRCVMORE) {
1993 /* Socket buffer is empty and we shall not block. */
1994 if (sb->sb_cc == 0 &&
1995 ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
2000 /* Socket buffer got some data that we shall deliver now. */
2001 if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) &&
2002 ((sb->sb_flags & SS_NBIO) ||
2003 (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
2004 sb->sb_cc >= sb->sb_lowat ||
2005 sb->sb_cc >= uio->uio_resid ||
2006 sb->sb_cc >= sb->sb_hiwat) ) {
2010 /* On MSG_WAITALL we must wait until all data or error arrives. */
2011 if ((flags & MSG_WAITALL) &&
2012 (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_lowat))
2016 * Wait and block until (more) data comes in.
2017 * NB: Drops the sockbuf lock during wait.
2025 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2026 KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__));
2027 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
2031 uio->uio_td->td_ru.ru_msgrcv++;
2033 /* Fill uio until full or current end of socket buffer is reached. */
2034 len = min(uio->uio_resid, sb->sb_cc);
2036 /* Dequeue as many mbufs as possible. */
2037 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
2038 for (*mp0 = m = sb->sb_mb;
2039 m != NULL && m->m_len <= len;
2042 uio->uio_resid -= m->m_len;
2047 if (sb->sb_mb == NULL)
2051 /* Copy the remainder. */
2053 KASSERT(sb->sb_mb != NULL,
2054 ("%s: len > 0 && sb->sb_mb empty", __func__));
2056 m = m_copym(sb->sb_mb, 0, len, M_DONTWAIT);
2058 len = 0; /* Don't flush data from sockbuf. */
2060 uio->uio_resid -= m->m_len;
2071 /* NB: Must unlock socket buffer as uiomove may sleep. */
2073 error = m_mbuftouio(uio, sb->sb_mb, len);
2078 SBLASTRECORDCHK(sb);
2082 * Remove the delivered data from the socket buffer unless we
2083 * were only peeking.
2085 if (!(flags & MSG_PEEK)) {
2087 sbdrop_locked(sb, len);
2089 /* Notify protocol that we drained some data. */
2090 if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
2091 (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
2092 !(flags & MSG_SOCALLBCK))) {
2095 (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
2101 * For MSG_WAITALL we may have to loop again and wait for
2102 * more data to come in.
2104 if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
2107 SOCKBUF_LOCK_ASSERT(sb);
2108 SBLASTRECORDCHK(sb);
2116 * Optimized version of soreceive() for simple datagram cases from userspace.
2117 * Unlike in the stream case, we're able to drop a datagram if copyout()
2118 * fails, and because we handle datagrams atomically, we don't need to use a
2119 * sleep lock to prevent I/O interlacing.
2122 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
2123 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2125 struct mbuf *m, *m2;
2128 struct protosw *pr = so->so_proto;
2129 struct mbuf *nextrecord;
2133 if (controlp != NULL)
2136 flags = *flagsp &~ MSG_EOR;
2141 * For any complicated cases, fall back to the full
2142 * soreceive_generic().
2144 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
2145 return (soreceive_generic(so, psa, uio, mp0, controlp,
2149 * Enforce restrictions on use.
2151 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
2152 ("soreceive_dgram: wantrcvd"));
2153 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
2154 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
2155 ("soreceive_dgram: SBS_RCVATMARK"));
2156 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
2157 ("soreceive_dgram: P_CONNREQUIRED"));
2160 * Loop blocking while waiting for a datagram.
2162 SOCKBUF_LOCK(&so->so_rcv);
2163 while ((m = so->so_rcv.sb_mb) == NULL) {
2164 KASSERT(so->so_rcv.sb_cc == 0,
2165 ("soreceive_dgram: sb_mb NULL but sb_cc %u",
2168 error = so->so_error;
2170 SOCKBUF_UNLOCK(&so->so_rcv);
2173 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
2174 uio->uio_resid == 0) {
2175 SOCKBUF_UNLOCK(&so->so_rcv);
2178 if ((so->so_state & SS_NBIO) ||
2179 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2180 SOCKBUF_UNLOCK(&so->so_rcv);
2181 return (EWOULDBLOCK);
2183 SBLASTRECORDCHK(&so->so_rcv);
2184 SBLASTMBUFCHK(&so->so_rcv);
2185 error = sbwait(&so->so_rcv);
2187 SOCKBUF_UNLOCK(&so->so_rcv);
2191 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2194 uio->uio_td->td_ru.ru_msgrcv++;
2195 SBLASTRECORDCHK(&so->so_rcv);
2196 SBLASTMBUFCHK(&so->so_rcv);
2197 nextrecord = m->m_nextpkt;
2198 if (nextrecord == NULL) {
2199 KASSERT(so->so_rcv.sb_lastrecord == m,
2200 ("soreceive_dgram: lastrecord != m"));
2203 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
2204 ("soreceive_dgram: m_nextpkt != nextrecord"));
2207 * Pull 'm' and its chain off the front of the packet queue.
2209 so->so_rcv.sb_mb = NULL;
2210 sockbuf_pushsync(&so->so_rcv, nextrecord);
2213 * Walk 'm's chain and free that many bytes from the socket buffer.
2215 for (m2 = m; m2 != NULL; m2 = m2->m_next)
2216 sbfree(&so->so_rcv, m2);
2219 * Do a few last checks before we let go of the lock.
2221 SBLASTRECORDCHK(&so->so_rcv);
2222 SBLASTMBUFCHK(&so->so_rcv);
2223 SOCKBUF_UNLOCK(&so->so_rcv);
2225 if (pr->pr_flags & PR_ADDR) {
2226 KASSERT(m->m_type == MT_SONAME,
2227 ("m->m_type == %d", m->m_type));
2229 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
2234 /* XXXRW: Can this happen? */
2239 * Packet to copyout() is now in 'm' and it is disconnected from the
2242 * Process one or more MT_CONTROL mbufs present before any data mbufs
2243 * in the first mbuf chain on the socket buffer. We call into the
2244 * protocol to perform externalization (or freeing if controlp ==
2247 if (m->m_type == MT_CONTROL) {
2248 struct mbuf *cm = NULL, *cmn;
2249 struct mbuf **cme = &cm;
2255 cme = &(*cme)->m_next;
2257 } while (m != NULL && m->m_type == MT_CONTROL);
2258 while (cm != NULL) {
2261 if (pr->pr_domain->dom_externalize != NULL) {
2262 error = (*pr->pr_domain->dom_externalize)
2264 } else if (controlp != NULL)
2268 if (controlp != NULL) {
2269 while (*controlp != NULL)
2270 controlp = &(*controlp)->m_next;
2275 KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
2277 while (m != NULL && uio->uio_resid > 0) {
2278 len = uio->uio_resid;
2281 error = uiomove(mtod(m, char *), (int)len, uio);
2286 if (len == m->m_len)
2302 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2303 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2307 CURVNET_SET(so->so_vnet);
2308 error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
2315 soshutdown(struct socket *so, int how)
2317 struct protosw *pr = so->so_proto;
2320 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2323 CURVNET_SET(so->so_vnet);
2324 if (pr->pr_usrreqs->pru_flush != NULL) {
2325 (*pr->pr_usrreqs->pru_flush)(so, how);
2329 if (how != SHUT_RD) {
2330 error = (*pr->pr_usrreqs->pru_shutdown)(so);
2339 sorflush(struct socket *so)
2341 struct sockbuf *sb = &so->so_rcv;
2342 struct protosw *pr = so->so_proto;
2348 * In order to avoid calling dom_dispose with the socket buffer mutex
2349 * held, and in order to generally avoid holding the lock for a long
2350 * time, we make a copy of the socket buffer and clear the original
2351 * (except locks, state). The new socket buffer copy won't have
2352 * initialized locks so we can only call routines that won't use or
2353 * assert those locks.
2355 * Dislodge threads currently blocked in receive and wait to acquire
2356 * a lock against other simultaneous readers before clearing the
2357 * socket buffer. Don't let our acquire be interrupted by a signal
2358 * despite any existing socket disposition on interruptable waiting.
2361 (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2364 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2365 * and mutex data unchanged.
2368 bzero(&asb, offsetof(struct sockbuf, sb_startzero));
2369 bcopy(&sb->sb_startzero, &asb.sb_startzero,
2370 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2371 bzero(&sb->sb_startzero,
2372 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2377 * Dispose of special rights and flush the socket buffer. Don't call
2378 * any unsafe routines (that rely on locks being initialized) on asb.
2380 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2381 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
2382 sbrelease_internal(&asb, so);
2386 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2387 * additional variant to handle the case where the option value needs to be
2388 * some kind of integer, but not a specific size. In addition to their use
2389 * here, these functions are also called by the protocol-level pr_ctloutput()
2393 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2398 * If the user gives us more than we wanted, we ignore it, but if we
2399 * don't get the minimum length the caller wants, we return EINVAL.
2400 * On success, sopt->sopt_valsize is set to however much we actually
2403 if ((valsize = sopt->sopt_valsize) < minlen)
2406 sopt->sopt_valsize = valsize = len;
2408 if (sopt->sopt_td != NULL)
2409 return (copyin(sopt->sopt_val, buf, valsize));
2411 bcopy(sopt->sopt_val, buf, valsize);
2416 * Kernel version of setsockopt(2).
2418 * XXX: optlen is size_t, not socklen_t
2421 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2424 struct sockopt sopt;
2426 sopt.sopt_level = level;
2427 sopt.sopt_name = optname;
2428 sopt.sopt_dir = SOPT_SET;
2429 sopt.sopt_val = optval;
2430 sopt.sopt_valsize = optlen;
2431 sopt.sopt_td = NULL;
2432 return (sosetopt(so, &sopt));
2436 sosetopt(struct socket *so, struct sockopt *sopt)
2447 CURVNET_SET(so->so_vnet);
2449 if (sopt->sopt_level != SOL_SOCKET) {
2450 if (so->so_proto->pr_ctloutput != NULL) {
2451 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2455 error = ENOPROTOOPT;
2457 switch (sopt->sopt_name) {
2459 case SO_ACCEPTFILTER:
2460 error = do_setopt_accept_filter(so, sopt);
2466 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2471 so->so_linger = l.l_linger;
2473 so->so_options |= SO_LINGER;
2475 so->so_options &= ~SO_LINGER;
2482 case SO_USELOOPBACK:
2492 error = sooptcopyin(sopt, &optval, sizeof optval,
2498 so->so_options |= sopt->sopt_name;
2500 so->so_options &= ~sopt->sopt_name;
2505 error = sooptcopyin(sopt, &optval, sizeof optval,
2507 if (optval < 0 || optval >= rt_numfibs) {
2511 if (((so->so_proto->pr_domain->dom_family == PF_INET) ||
2512 (so->so_proto->pr_domain->dom_family == PF_INET6) ||
2513 (so->so_proto->pr_domain->dom_family == PF_ROUTE))) {
2514 so->so_fibnum = optval;
2515 /* Note: ignore error */
2516 if (so->so_proto->pr_ctloutput)
2517 (*so->so_proto->pr_ctloutput)(so, sopt);
2523 case SO_USER_COOKIE:
2524 error = sooptcopyin(sopt, &val32, sizeof val32,
2528 so->so_user_cookie = val32;
2535 error = sooptcopyin(sopt, &optval, sizeof optval,
2541 * Values < 1 make no sense for any of these options,
2549 switch (sopt->sopt_name) {
2552 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
2553 &so->so_snd : &so->so_rcv, (u_long)optval,
2554 so, curthread) == 0) {
2558 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
2559 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
2563 * Make sure the low-water is never greater than the
2567 SOCKBUF_LOCK(&so->so_snd);
2568 so->so_snd.sb_lowat =
2569 (optval > so->so_snd.sb_hiwat) ?
2570 so->so_snd.sb_hiwat : optval;
2571 SOCKBUF_UNLOCK(&so->so_snd);
2574 SOCKBUF_LOCK(&so->so_rcv);
2575 so->so_rcv.sb_lowat =
2576 (optval > so->so_rcv.sb_hiwat) ?
2577 so->so_rcv.sb_hiwat : optval;
2578 SOCKBUF_UNLOCK(&so->so_rcv);
2585 #ifdef COMPAT_FREEBSD32
2586 if (SV_CURPROC_FLAG(SV_ILP32)) {
2587 struct timeval32 tv32;
2589 error = sooptcopyin(sopt, &tv32, sizeof tv32,
2591 CP(tv32, tv, tv_sec);
2592 CP(tv32, tv, tv_usec);
2595 error = sooptcopyin(sopt, &tv, sizeof tv,
2600 /* assert(hz > 0); */
2601 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
2602 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
2606 /* assert(tick > 0); */
2607 /* assert(ULONG_MAX - INT_MAX >= 1000000); */
2608 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
2609 if (val > INT_MAX) {
2613 if (val == 0 && tv.tv_usec != 0)
2616 switch (sopt->sopt_name) {
2618 so->so_snd.sb_timeo = val;
2621 so->so_rcv.sb_timeo = val;
2628 error = sooptcopyin(sopt, &extmac, sizeof extmac,
2632 error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2640 error = ENOPROTOOPT;
2643 if (error == 0 && so->so_proto->pr_ctloutput != NULL)
2644 (void)(*so->so_proto->pr_ctloutput)(so, sopt);
2652 * Helper routine for getsockopt.
2655 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2663 * Documented get behavior is that we always return a value, possibly
2664 * truncated to fit in the user's buffer. Traditional behavior is
2665 * that we always tell the user precisely how much we copied, rather
2666 * than something useful like the total amount we had available for
2667 * her. Note that this interface is not idempotent; the entire
2668 * answer must generated ahead of time.
2670 valsize = min(len, sopt->sopt_valsize);
2671 sopt->sopt_valsize = valsize;
2672 if (sopt->sopt_val != NULL) {
2673 if (sopt->sopt_td != NULL)
2674 error = copyout(buf, sopt->sopt_val, valsize);
2676 bcopy(buf, sopt->sopt_val, valsize);
2682 sogetopt(struct socket *so, struct sockopt *sopt)
2691 CURVNET_SET(so->so_vnet);
2693 if (sopt->sopt_level != SOL_SOCKET) {
2694 if (so->so_proto->pr_ctloutput != NULL)
2695 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2697 error = ENOPROTOOPT;
2701 switch (sopt->sopt_name) {
2703 case SO_ACCEPTFILTER:
2704 error = do_getopt_accept_filter(so, sopt);
2709 l.l_onoff = so->so_options & SO_LINGER;
2710 l.l_linger = so->so_linger;
2712 error = sooptcopyout(sopt, &l, sizeof l);
2715 case SO_USELOOPBACK:
2727 optval = so->so_options & sopt->sopt_name;
2729 error = sooptcopyout(sopt, &optval, sizeof optval);
2733 optval = so->so_type;
2737 optval = so->so_proto->pr_protocol;
2742 optval = so->so_error;
2748 optval = so->so_snd.sb_hiwat;
2752 optval = so->so_rcv.sb_hiwat;
2756 optval = so->so_snd.sb_lowat;
2760 optval = so->so_rcv.sb_lowat;
2765 optval = (sopt->sopt_name == SO_SNDTIMEO ?
2766 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2768 tv.tv_sec = optval / hz;
2769 tv.tv_usec = (optval % hz) * tick;
2770 #ifdef COMPAT_FREEBSD32
2771 if (SV_CURPROC_FLAG(SV_ILP32)) {
2772 struct timeval32 tv32;
2774 CP(tv, tv32, tv_sec);
2775 CP(tv, tv32, tv_usec);
2776 error = sooptcopyout(sopt, &tv32, sizeof tv32);
2779 error = sooptcopyout(sopt, &tv, sizeof tv);
2784 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2788 error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
2792 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2800 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2804 error = mac_getsockopt_peerlabel(
2805 sopt->sopt_td->td_ucred, so, &extmac);
2808 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2814 case SO_LISTENQLIMIT:
2815 optval = so->so_qlimit;
2819 optval = so->so_qlen;
2822 case SO_LISTENINCQLEN:
2823 optval = so->so_incqlen;
2827 error = ENOPROTOOPT;
2838 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2840 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2842 struct mbuf *m, *m_prev;
2843 int sopt_size = sopt->sopt_valsize;
2845 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2848 if (sopt_size > MLEN) {
2849 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
2850 if ((m->m_flags & M_EXT) == 0) {
2854 m->m_len = min(MCLBYTES, sopt_size);
2856 m->m_len = min(MLEN, sopt_size);
2858 sopt_size -= m->m_len;
2863 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2868 if (sopt_size > MLEN) {
2869 MCLGET(m, sopt->sopt_td != NULL ? M_WAIT :
2871 if ((m->m_flags & M_EXT) == 0) {
2876 m->m_len = min(MCLBYTES, sopt_size);
2878 m->m_len = min(MLEN, sopt_size);
2880 sopt_size -= m->m_len;
2887 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2889 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2891 struct mbuf *m0 = m;
2893 if (sopt->sopt_val == NULL)
2895 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2896 if (sopt->sopt_td != NULL) {
2899 error = copyin(sopt->sopt_val, mtod(m, char *),
2906 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2907 sopt->sopt_valsize -= m->m_len;
2908 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2911 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2912 panic("ip6_sooptmcopyin");
2916 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2918 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2920 struct mbuf *m0 = m;
2923 if (sopt->sopt_val == NULL)
2925 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2926 if (sopt->sopt_td != NULL) {
2929 error = copyout(mtod(m, char *), sopt->sopt_val,
2936 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2937 sopt->sopt_valsize -= m->m_len;
2938 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2939 valsize += m->m_len;
2943 /* enough soopt buffer should be given from user-land */
2947 sopt->sopt_valsize = valsize;
2952 * sohasoutofband(): protocol notifies socket layer of the arrival of new
2953 * out-of-band data, which will then notify socket consumers.
2956 sohasoutofband(struct socket *so)
2959 if (so->so_sigio != NULL)
2960 pgsigio(&so->so_sigio, SIGURG, 0);
2961 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
2965 sopoll(struct socket *so, int events, struct ucred *active_cred,
2970 * We do not need to set or assert curvnet as long as everyone uses
2973 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
2978 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
2983 SOCKBUF_LOCK(&so->so_snd);
2984 SOCKBUF_LOCK(&so->so_rcv);
2985 if (events & (POLLIN | POLLRDNORM))
2986 if (soreadabledata(so))
2987 revents |= events & (POLLIN | POLLRDNORM);
2989 if (events & (POLLOUT | POLLWRNORM))
2990 if (sowriteable(so))
2991 revents |= events & (POLLOUT | POLLWRNORM);
2993 if (events & (POLLPRI | POLLRDBAND))
2994 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
2995 revents |= events & (POLLPRI | POLLRDBAND);
2997 if ((events & POLLINIGNEOF) == 0) {
2998 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2999 revents |= events & (POLLIN | POLLRDNORM);
3000 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
3006 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
3007 selrecord(td, &so->so_rcv.sb_sel);
3008 so->so_rcv.sb_flags |= SB_SEL;
3011 if (events & (POLLOUT | POLLWRNORM)) {
3012 selrecord(td, &so->so_snd.sb_sel);
3013 so->so_snd.sb_flags |= SB_SEL;
3017 SOCKBUF_UNLOCK(&so->so_rcv);
3018 SOCKBUF_UNLOCK(&so->so_snd);
3023 soo_kqfilter(struct file *fp, struct knote *kn)
3025 struct socket *so = kn->kn_fp->f_data;
3028 switch (kn->kn_filter) {
3030 if (so->so_options & SO_ACCEPTCONN)
3031 kn->kn_fop = &solisten_filtops;
3033 kn->kn_fop = &soread_filtops;
3037 kn->kn_fop = &sowrite_filtops;
3045 knlist_add(&sb->sb_sel.si_note, kn, 1);
3046 sb->sb_flags |= SB_KNOTE;
3052 * Some routines that return EOPNOTSUPP for entry points that are not
3053 * supported by a protocol. Fill in as needed.
3056 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
3063 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
3070 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3077 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3084 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
3091 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
3092 struct ifnet *ifp, struct thread *td)
3099 pru_disconnect_notsupp(struct socket *so)
3106 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
3113 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
3120 pru_rcvd_notsupp(struct socket *so, int flags)
3127 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
3134 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
3135 struct sockaddr *addr, struct mbuf *control, struct thread *td)
3142 * This isn't really a ``null'' operation, but it's the default one and
3143 * doesn't do anything destructive.
3146 pru_sense_null(struct socket *so, struct stat *sb)
3149 sb->st_blksize = so->so_snd.sb_hiwat;
3154 pru_shutdown_notsupp(struct socket *so)
3161 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
3168 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
3169 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
3176 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
3177 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
3184 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
3192 filt_sordetach(struct knote *kn)
3194 struct socket *so = kn->kn_fp->f_data;
3196 SOCKBUF_LOCK(&so->so_rcv);
3197 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
3198 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
3199 so->so_rcv.sb_flags &= ~SB_KNOTE;
3200 SOCKBUF_UNLOCK(&so->so_rcv);
3205 filt_soread(struct knote *kn, long hint)
3209 so = kn->kn_fp->f_data;
3210 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
3212 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
3213 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3214 kn->kn_flags |= EV_EOF;
3215 kn->kn_fflags = so->so_error;
3217 } else if (so->so_error) /* temporary udp error */
3219 else if (kn->kn_sfflags & NOTE_LOWAT)
3220 return (kn->kn_data >= kn->kn_sdata);
3222 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
3226 filt_sowdetach(struct knote *kn)
3228 struct socket *so = kn->kn_fp->f_data;
3230 SOCKBUF_LOCK(&so->so_snd);
3231 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
3232 if (knlist_empty(&so->so_snd.sb_sel.si_note))
3233 so->so_snd.sb_flags &= ~SB_KNOTE;
3234 SOCKBUF_UNLOCK(&so->so_snd);
3239 filt_sowrite(struct knote *kn, long hint)
3243 so = kn->kn_fp->f_data;
3244 SOCKBUF_LOCK_ASSERT(&so->so_snd);
3245 kn->kn_data = sbspace(&so->so_snd);
3246 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
3247 kn->kn_flags |= EV_EOF;
3248 kn->kn_fflags = so->so_error;
3250 } else if (so->so_error) /* temporary udp error */
3252 else if (((so->so_state & SS_ISCONNECTED) == 0) &&
3253 (so->so_proto->pr_flags & PR_CONNREQUIRED))
3255 else if (kn->kn_sfflags & NOTE_LOWAT)
3256 return (kn->kn_data >= kn->kn_sdata);
3258 return (kn->kn_data >= so->so_snd.sb_lowat);
3263 filt_solisten(struct knote *kn, long hint)
3265 struct socket *so = kn->kn_fp->f_data;
3267 kn->kn_data = so->so_qlen;
3268 return (! TAILQ_EMPTY(&so->so_comp));
3272 socheckuid(struct socket *so, uid_t uid)
3277 if (so->so_cred->cr_uid != uid)
3283 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
3289 error = sysctl_handle_int(oidp, &val, 0, req);
3290 if (error || !req->newptr )
3293 if (val < 1 || val > USHRT_MAX)
3301 * These functions are used by protocols to notify the socket layer (and its
3302 * consumers) of state changes in the sockets driven by protocol-side events.
3306 * Procedures to manipulate state flags of socket and do appropriate wakeups.
3308 * Normal sequence from the active (originating) side is that
3309 * soisconnecting() is called during processing of connect() call, resulting
3310 * in an eventual call to soisconnected() if/when the connection is
3311 * established. When the connection is torn down soisdisconnecting() is
3312 * called during processing of disconnect() call, and soisdisconnected() is
3313 * called when the connection to the peer is totally severed. The semantics
3314 * of these routines are such that connectionless protocols can call
3315 * soisconnected() and soisdisconnected() only, bypassing the in-progress
3316 * calls when setting up a ``connection'' takes no time.
3318 * From the passive side, a socket is created with two queues of sockets:
3319 * so_incomp for connections in progress and so_comp for connections already
3320 * made and awaiting user acceptance. As a protocol is preparing incoming
3321 * connections, it creates a socket structure queued on so_incomp by calling
3322 * sonewconn(). When the connection is established, soisconnected() is
3323 * called, and transfers the socket structure to so_comp, making it available
3326 * If a socket is closed with sockets on either so_incomp or so_comp, these
3327 * sockets are dropped.
3329 * If higher-level protocols are implemented in the kernel, the wakeups done
3330 * here will sometimes cause software-interrupt process scheduling.
3333 soisconnecting(struct socket *so)
3337 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3338 so->so_state |= SS_ISCONNECTING;
3343 soisconnected(struct socket *so)
3345 struct socket *head;
3351 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3352 so->so_state |= SS_ISCONNECTED;
3354 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
3355 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3357 TAILQ_REMOVE(&head->so_incomp, so, so_list);
3359 so->so_qstate &= ~SQ_INCOMP;
3360 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
3362 so->so_qstate |= SQ_COMP;
3365 wakeup_one(&head->so_timeo);
3368 soupcall_set(so, SO_RCV,
3369 head->so_accf->so_accept_filter->accf_callback,
3370 head->so_accf->so_accept_filter_arg);
3371 so->so_options &= ~SO_ACCEPTFILTER;
3372 ret = head->so_accf->so_accept_filter->accf_callback(so,
3373 head->so_accf->so_accept_filter_arg, M_DONTWAIT);
3374 if (ret == SU_ISCONNECTED)
3375 soupcall_clear(so, SO_RCV);
3377 if (ret == SU_ISCONNECTED)
3384 wakeup(&so->so_timeo);
3390 soisdisconnecting(struct socket *so)
3394 * Note: This code assumes that SOCK_LOCK(so) and
3395 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3397 SOCKBUF_LOCK(&so->so_rcv);
3398 so->so_state &= ~SS_ISCONNECTING;
3399 so->so_state |= SS_ISDISCONNECTING;
3400 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3401 sorwakeup_locked(so);
3402 SOCKBUF_LOCK(&so->so_snd);
3403 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3404 sowwakeup_locked(so);
3405 wakeup(&so->so_timeo);
3409 soisdisconnected(struct socket *so)
3413 * Note: This code assumes that SOCK_LOCK(so) and
3414 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3416 SOCKBUF_LOCK(&so->so_rcv);
3417 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3418 so->so_state |= SS_ISDISCONNECTED;
3419 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3420 sorwakeup_locked(so);
3421 SOCKBUF_LOCK(&so->so_snd);
3422 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3423 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
3424 sowwakeup_locked(so);
3425 wakeup(&so->so_timeo);
3429 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3432 sodupsockaddr(const struct sockaddr *sa, int mflags)
3434 struct sockaddr *sa2;
3436 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3438 bcopy(sa, sa2, sa->sa_len);
3443 * Register per-socket buffer upcalls.
3446 soupcall_set(struct socket *so, int which,
3447 int (*func)(struct socket *, void *, int), void *arg)
3459 panic("soupcall_set: bad which");
3461 SOCKBUF_LOCK_ASSERT(sb);
3463 /* XXX: accf_http actually wants to do this on purpose. */
3464 KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall"));
3466 sb->sb_upcall = func;
3467 sb->sb_upcallarg = arg;
3468 sb->sb_flags |= SB_UPCALL;
3472 soupcall_clear(struct socket *so, int which)
3484 panic("soupcall_clear: bad which");
3486 SOCKBUF_LOCK_ASSERT(sb);
3487 KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear"));
3488 sb->sb_upcall = NULL;
3489 sb->sb_upcallarg = NULL;
3490 sb->sb_flags &= ~SB_UPCALL;
3494 * Create an external-format (``xsocket'') structure using the information in
3495 * the kernel-format socket structure pointed to by so. This is done to
3496 * reduce the spew of irrelevant information over this interface, to isolate
3497 * user code from changes in the kernel structure, and potentially to provide
3498 * information-hiding if we decide that some of this information should be
3499 * hidden from users.
3502 sotoxsocket(struct socket *so, struct xsocket *xso)
3505 xso->xso_len = sizeof *xso;
3507 xso->so_type = so->so_type;
3508 xso->so_options = so->so_options;
3509 xso->so_linger = so->so_linger;
3510 xso->so_state = so->so_state;
3511 xso->so_pcb = so->so_pcb;
3512 xso->xso_protocol = so->so_proto->pr_protocol;
3513 xso->xso_family = so->so_proto->pr_domain->dom_family;
3514 xso->so_qlen = so->so_qlen;
3515 xso->so_incqlen = so->so_incqlen;
3516 xso->so_qlimit = so->so_qlimit;
3517 xso->so_timeo = so->so_timeo;
3518 xso->so_error = so->so_error;
3519 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
3520 xso->so_oobmark = so->so_oobmark;
3521 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
3522 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
3523 xso->so_uid = so->so_cred->cr_uid;
3528 * Socket accessor functions to provide external consumers with
3529 * a safe interface to socket state
3534 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg)
3537 TAILQ_FOREACH(so, &so->so_comp, so_list)
3542 so_sockbuf_rcv(struct socket *so)
3545 return (&so->so_rcv);
3549 so_sockbuf_snd(struct socket *so)
3552 return (&so->so_snd);
3556 so_state_get(const struct socket *so)
3559 return (so->so_state);
3563 so_state_set(struct socket *so, int val)
3570 so_options_get(const struct socket *so)
3573 return (so->so_options);
3577 so_options_set(struct socket *so, int val)
3580 so->so_options = val;
3584 so_error_get(const struct socket *so)
3587 return (so->so_error);
3591 so_error_set(struct socket *so, int val)
3598 so_linger_get(const struct socket *so)
3601 return (so->so_linger);
3605 so_linger_set(struct socket *so, int val)
3608 so->so_linger = val;
3612 so_protosw_get(const struct socket *so)
3615 return (so->so_proto);
3619 so_protosw_set(struct socket *so, struct protosw *val)
3626 so_sorwakeup(struct socket *so)
3633 so_sowwakeup(struct socket *so)
3640 so_sorwakeup_locked(struct socket *so)
3643 sorwakeup_locked(so);
3647 so_sowwakeup_locked(struct socket *so)
3650 sowwakeup_locked(so);
3654 so_lock(struct socket *so)
3660 so_unlock(struct socket *so)