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 =
161 { 1, NULL, filt_sordetach, filt_solisten };
162 static struct filterops soread_filtops =
163 { 1, NULL, filt_sordetach, filt_soread };
164 static struct filterops sowrite_filtops =
165 { 1, NULL, filt_sowdetach, filt_sowrite };
167 uma_zone_t socket_zone;
168 so_gen_t so_gencnt; /* generation count for sockets */
172 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
173 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
175 #define VNET_SO_ASSERT(so) \
176 VNET_ASSERT(curvnet != NULL, \
177 ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so)));
179 static int somaxconn = SOMAXCONN;
180 static int sysctl_somaxconn(SYSCTL_HANDLER_ARGS);
181 /* XXX: we dont have SYSCTL_USHORT */
182 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLTYPE_UINT | CTLFLAG_RW,
183 0, sizeof(int), sysctl_somaxconn, "I", "Maximum pending socket connection "
185 static int numopensockets;
186 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
187 &numopensockets, 0, "Number of open sockets");
188 #ifdef ZERO_COPY_SOCKETS
189 /* These aren't static because they're used in other files. */
190 int so_zero_copy_send = 1;
191 int so_zero_copy_receive = 1;
192 SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0,
193 "Zero copy controls");
194 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW,
195 &so_zero_copy_receive, 0, "Enable zero copy receive");
196 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW,
197 &so_zero_copy_send, 0, "Enable zero copy send");
198 #endif /* ZERO_COPY_SOCKETS */
201 * accept_mtx locks down per-socket fields relating to accept queues. See
202 * socketvar.h for an annotation of the protected fields of struct socket.
204 struct mtx accept_mtx;
205 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
208 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
211 static struct mtx so_global_mtx;
212 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
215 * General IPC sysctl name space, used by sockets and a variety of other IPC
218 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
221 * Sysctl to get and set the maximum global sockets limit. Notify protocols
222 * of the change so that they can update their dependent limits as required.
225 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
227 int error, newmaxsockets;
229 newmaxsockets = maxsockets;
230 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
231 if (error == 0 && req->newptr) {
232 if (newmaxsockets > maxsockets) {
233 maxsockets = newmaxsockets;
234 if (maxsockets > ((maxfiles / 4) * 3)) {
235 maxfiles = (maxsockets * 5) / 4;
236 maxfilesperproc = (maxfiles * 9) / 10;
238 EVENTHANDLER_INVOKE(maxsockets_change);
245 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
246 &maxsockets, 0, sysctl_maxsockets, "IU",
247 "Maximum number of sockets avaliable");
250 * Initialise maxsockets. This SYSINIT must be run after
254 init_maxsockets(void *ignored)
257 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
258 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
260 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
263 * Socket operation routines. These routines are called by the routines in
264 * sys_socket.c or from a system process, and implement the semantics of
265 * socket operations by switching out to the protocol specific routines.
269 * Get a socket structure from our zone, and initialize it. Note that it
270 * would probably be better to allocate socket and PCB at the same time, but
271 * I'm not convinced that all the protocols can be easily modified to do
274 * soalloc() returns a socket with a ref count of 0.
276 static struct socket *
277 soalloc(struct vnet *vnet)
281 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
285 if (mac_socket_init(so, M_NOWAIT) != 0) {
286 uma_zfree(socket_zone, so);
290 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
291 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
292 sx_init(&so->so_snd.sb_sx, "so_snd_sx");
293 sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
294 TAILQ_INIT(&so->so_aiojobq);
295 mtx_lock(&so_global_mtx);
296 so->so_gencnt = ++so_gencnt;
299 VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p",
300 __func__, __LINE__, so));
301 vnet->vnet_sockcnt++;
304 mtx_unlock(&so_global_mtx);
309 * Free the storage associated with a socket at the socket layer, tear down
310 * locks, labels, etc. All protocol state is assumed already to have been
311 * torn down (and possibly never set up) by the caller.
314 sodealloc(struct socket *so)
317 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
318 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
320 mtx_lock(&so_global_mtx);
321 so->so_gencnt = ++so_gencnt;
322 --numopensockets; /* Could be below, but faster here. */
324 VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p",
325 __func__, __LINE__, so));
326 so->so_vnet->vnet_sockcnt--;
328 mtx_unlock(&so_global_mtx);
329 if (so->so_rcv.sb_hiwat)
330 (void)chgsbsize(so->so_cred->cr_uidinfo,
331 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
332 if (so->so_snd.sb_hiwat)
333 (void)chgsbsize(so->so_cred->cr_uidinfo,
334 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
336 /* remove acccept filter if one is present. */
337 if (so->so_accf != NULL)
338 do_setopt_accept_filter(so, NULL);
341 mac_socket_destroy(so);
344 sx_destroy(&so->so_snd.sb_sx);
345 sx_destroy(&so->so_rcv.sb_sx);
346 SOCKBUF_LOCK_DESTROY(&so->so_snd);
347 SOCKBUF_LOCK_DESTROY(&so->so_rcv);
348 uma_zfree(socket_zone, so);
352 * socreate returns a socket with a ref count of 1. The socket should be
353 * closed with soclose().
356 socreate(int dom, struct socket **aso, int type, int proto,
357 struct ucred *cred, struct thread *td)
364 prp = pffindproto(dom, proto, type);
366 prp = pffindtype(dom, type);
368 if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
369 prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
370 return (EPROTONOSUPPORT);
372 if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
373 return (EPROTONOSUPPORT);
375 if (prp->pr_type != type)
377 so = soalloc(CRED_TO_VNET(cred));
381 TAILQ_INIT(&so->so_incomp);
382 TAILQ_INIT(&so->so_comp);
384 so->so_cred = crhold(cred);
385 if ((prp->pr_domain->dom_family == PF_INET) ||
386 (prp->pr_domain->dom_family == PF_INET6) ||
387 (prp->pr_domain->dom_family == PF_ROUTE))
388 so->so_fibnum = td->td_proc->p_fibnum;
393 mac_socket_create(cred, so);
395 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
396 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
399 * Auto-sizing of socket buffers is managed by the protocols and
400 * the appropriate flags must be set in the pru_attach function.
402 CURVNET_SET(so->so_vnet);
403 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
406 KASSERT(so->so_count == 1, ("socreate: so_count %d",
417 static int regression_sonewconn_earlytest = 1;
418 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
419 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
423 * When an attempt at a new connection is noted on a socket which accepts
424 * connections, sonewconn is called. If the connection is possible (subject
425 * to space constraints, etc.) then we allocate a new structure, propoerly
426 * linked into the data structure of the original socket, and return this.
427 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
429 * Note: the ref count on the socket is 0 on return.
432 sonewconn(struct socket *head, int connstatus)
438 over = (head->so_qlen > 3 * head->so_qlimit / 2);
441 if (regression_sonewconn_earlytest && over)
446 VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
447 __func__, __LINE__, head));
448 so = soalloc(head->so_vnet);
451 if ((head->so_options & SO_ACCEPTFILTER) != 0)
454 so->so_type = head->so_type;
455 so->so_options = head->so_options &~ SO_ACCEPTCONN;
456 so->so_linger = head->so_linger;
457 so->so_state = head->so_state | SS_NOFDREF;
458 so->so_fibnum = head->so_fibnum;
459 so->so_proto = head->so_proto;
460 so->so_cred = crhold(head->so_cred);
462 mac_socket_newconn(head, so);
464 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
465 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
466 VNET_SO_ASSERT(head);
467 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
468 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
472 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
473 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
474 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
475 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
476 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
477 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
478 so->so_state |= connstatus;
481 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
482 so->so_qstate |= SQ_COMP;
486 * Keep removing sockets from the head until there's room for
487 * us to insert on the tail. In pre-locking revisions, this
488 * was a simple if(), but as we could be racing with other
489 * threads and soabort() requires dropping locks, we must
490 * loop waiting for the condition to be true.
492 while (head->so_incqlen > head->so_qlimit) {
494 sp = TAILQ_FIRST(&head->so_incomp);
495 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
497 sp->so_qstate &= ~SQ_INCOMP;
503 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
504 so->so_qstate |= SQ_INCOMP;
510 wakeup_one(&head->so_timeo);
516 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
520 CURVNET_SET(so->so_vnet);
521 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
527 * solisten() transitions a socket from a non-listening state to a listening
528 * state, but can also be used to update the listen queue depth on an
529 * existing listen socket. The protocol will call back into the sockets
530 * layer using solisten_proto_check() and solisten_proto() to check and set
531 * socket-layer listen state. Call backs are used so that the protocol can
532 * acquire both protocol and socket layer locks in whatever order is required
535 * Protocol implementors are advised to hold the socket lock across the
536 * socket-layer test and set to avoid races at the socket layer.
539 solisten(struct socket *so, int backlog, struct thread *td)
543 CURVNET_SET(so->so_vnet);
544 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td);
550 solisten_proto_check(struct socket *so)
553 SOCK_LOCK_ASSERT(so);
555 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
562 solisten_proto(struct socket *so, int backlog)
565 SOCK_LOCK_ASSERT(so);
567 if (backlog < 0 || backlog > somaxconn)
569 so->so_qlimit = backlog;
570 so->so_options |= SO_ACCEPTCONN;
574 * Attempt to free a socket. This should really be sotryfree().
576 * sofree() will succeed if:
578 * - There are no outstanding file descriptor references or related consumers
581 * - The socket has been closed by user space, if ever open (SS_NOFDREF).
583 * - The protocol does not have an outstanding strong reference on the socket
586 * - The socket is not in a completed connection queue, so a process has been
587 * notified that it is present. If it is removed, the user process may
588 * block in accept() despite select() saying the socket was ready.
590 * Otherwise, it will quietly abort so that a future call to sofree(), when
591 * conditions are right, can succeed.
594 sofree(struct socket *so)
596 struct protosw *pr = so->so_proto;
599 ACCEPT_LOCK_ASSERT();
600 SOCK_LOCK_ASSERT(so);
602 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
603 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
611 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
612 (so->so_qstate & SQ_INCOMP) != 0,
613 ("sofree: so_head != NULL, but neither SQ_COMP nor "
615 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
616 (so->so_qstate & SQ_INCOMP) == 0,
617 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
618 TAILQ_REMOVE(&head->so_incomp, so, so_list);
620 so->so_qstate &= ~SQ_INCOMP;
623 KASSERT((so->so_qstate & SQ_COMP) == 0 &&
624 (so->so_qstate & SQ_INCOMP) == 0,
625 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
626 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
627 if (so->so_options & SO_ACCEPTCONN) {
628 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
629 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_incomp populated"));
635 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
636 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
637 if (pr->pr_usrreqs->pru_detach != NULL)
638 (*pr->pr_usrreqs->pru_detach)(so);
641 * From this point on, we assume that no other references to this
642 * socket exist anywhere else in the stack. Therefore, no locks need
643 * to be acquired or held.
645 * We used to do a lot of socket buffer and socket locking here, as
646 * well as invoke sorflush() and perform wakeups. The direct call to
647 * dom_dispose() and sbrelease_internal() are an inlining of what was
648 * necessary from sorflush().
650 * Notice that the socket buffer and kqueue state are torn down
651 * before calling pru_detach. This means that protocols shold not
652 * assume they can perform socket wakeups, etc, in their detach code.
654 sbdestroy(&so->so_snd, so);
655 sbdestroy(&so->so_rcv, so);
656 seldrain(&so->so_snd.sb_sel);
657 seldrain(&so->so_rcv.sb_sel);
658 knlist_destroy(&so->so_rcv.sb_sel.si_note);
659 knlist_destroy(&so->so_snd.sb_sel.si_note);
664 * Close a socket on last file table reference removal. Initiate disconnect
665 * if connected. Free socket when disconnect complete.
667 * This function will sorele() the socket. Note that soclose() may be called
668 * prior to the ref count reaching zero. The actual socket structure will
669 * not be freed until the ref count reaches zero.
672 soclose(struct socket *so)
676 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
678 CURVNET_SET(so->so_vnet);
679 funsetown(&so->so_sigio);
680 if (so->so_state & SS_ISCONNECTED) {
681 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
682 error = sodisconnect(so);
684 if (error == ENOTCONN)
689 if (so->so_options & SO_LINGER) {
690 if ((so->so_state & SS_ISDISCONNECTING) &&
691 (so->so_state & SS_NBIO))
693 while (so->so_state & SS_ISCONNECTED) {
694 error = tsleep(&so->so_timeo,
695 PSOCK | PCATCH, "soclos", so->so_linger * hz);
703 if (so->so_proto->pr_usrreqs->pru_close != NULL)
704 (*so->so_proto->pr_usrreqs->pru_close)(so);
705 if (so->so_options & SO_ACCEPTCONN) {
708 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
709 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
711 sp->so_qstate &= ~SQ_INCOMP;
717 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
718 TAILQ_REMOVE(&so->so_comp, sp, so_list);
720 sp->so_qstate &= ~SQ_COMP;
730 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
731 so->so_state |= SS_NOFDREF;
738 * soabort() is used to abruptly tear down a connection, such as when a
739 * resource limit is reached (listen queue depth exceeded), or if a listen
740 * socket is closed while there are sockets waiting to be accepted.
742 * This interface is tricky, because it is called on an unreferenced socket,
743 * and must be called only by a thread that has actually removed the socket
744 * from the listen queue it was on, or races with other threads are risked.
746 * This interface will call into the protocol code, so must not be called
747 * with any socket locks held. Protocols do call it while holding their own
748 * recursible protocol mutexes, but this is something that should be subject
749 * to review in the future.
752 soabort(struct socket *so)
756 * In as much as is possible, assert that no references to this
757 * socket are held. This is not quite the same as asserting that the
758 * current thread is responsible for arranging for no references, but
759 * is as close as we can get for now.
761 KASSERT(so->so_count == 0, ("soabort: so_count"));
762 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
763 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
764 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
765 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
768 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
769 (*so->so_proto->pr_usrreqs->pru_abort)(so);
776 soaccept(struct socket *so, struct sockaddr **nam)
781 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
782 so->so_state &= ~SS_NOFDREF;
785 CURVNET_SET(so->so_vnet);
786 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
792 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
796 if (so->so_options & SO_ACCEPTCONN)
799 CURVNET_SET(so->so_vnet);
801 * If protocol is connection-based, can only connect once.
802 * Otherwise, if connected, try to disconnect first. This allows
803 * user to disconnect by connecting to, e.g., a null address.
805 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
806 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
807 (error = sodisconnect(so)))) {
811 * Prevent accumulated error from previous connection from
815 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
823 soconnect2(struct socket *so1, struct socket *so2)
827 CURVNET_SET(so1->so_vnet);
828 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
834 sodisconnect(struct socket *so)
838 if ((so->so_state & SS_ISCONNECTED) == 0)
840 if (so->so_state & SS_ISDISCONNECTING)
843 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
847 #ifdef ZERO_COPY_SOCKETS
848 struct so_zerocopy_stats{
853 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
854 #include <netinet/in.h>
855 #include <net/route.h>
856 #include <netinet/in_pcb.h>
858 #include <vm/vm_page.h>
859 #include <vm/vm_object.h>
862 * sosend_copyin() is only used if zero copy sockets are enabled. Otherwise
863 * sosend_dgram() and sosend_generic() use m_uiotombuf().
865 * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or
866 * all of the data referenced by the uio. If desired, it uses zero-copy.
867 * *space will be updated to reflect data copied in.
869 * NB: If atomic I/O is requested, the caller must already have checked that
870 * space can hold resid bytes.
872 * NB: In the event of an error, the caller may need to free the partial
873 * chain pointed to by *mpp. The contents of both *uio and *space may be
874 * modified even in the case of an error.
877 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space,
880 struct mbuf *m, **mp, *top;
883 #ifdef ZERO_COPY_SOCKETS
890 resid = uio->uio_resid;
893 #ifdef ZERO_COPY_SOCKETS
895 #endif /* ZERO_COPY_SOCKETS */
896 if (resid >= MINCLSIZE) {
897 #ifdef ZERO_COPY_SOCKETS
899 m = m_gethdr(M_WAITOK, MT_DATA);
901 m->m_pkthdr.rcvif = NULL;
903 m = m_get(M_WAITOK, MT_DATA);
904 if (so_zero_copy_send &&
907 uio->uio_iov->iov_len>=PAGE_SIZE) {
908 so_zerocp_stats.size_ok++;
909 so_zerocp_stats.align_ok++;
910 cow_send = socow_setup(m, uio);
914 m_clget(m, M_WAITOK);
915 len = min(min(MCLBYTES, resid), *space);
917 #else /* ZERO_COPY_SOCKETS */
919 m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
921 m->m_pkthdr.rcvif = NULL;
923 m = m_getcl(M_WAIT, MT_DATA, 0);
924 len = min(min(MCLBYTES, resid), *space);
925 #endif /* ZERO_COPY_SOCKETS */
928 m = m_gethdr(M_WAIT, MT_DATA);
930 m->m_pkthdr.rcvif = NULL;
932 len = min(min(MHLEN, resid), *space);
934 * For datagram protocols, leave room
935 * for protocol headers in first mbuf.
937 if (atomic && m && len < MHLEN)
940 m = m_get(M_WAIT, MT_DATA);
941 len = min(min(MLEN, resid), *space);
950 #ifdef ZERO_COPY_SOCKETS
954 #endif /* ZERO_COPY_SOCKETS */
955 error = uiomove(mtod(m, void *), (int)len, uio);
956 resid = uio->uio_resid;
959 top->m_pkthdr.len += len;
965 top->m_flags |= M_EOR;
968 } while (*space > 0 && atomic);
973 #endif /*ZERO_COPY_SOCKETS*/
975 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
978 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
979 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
982 int clen = 0, error, dontroute;
983 #ifdef ZERO_COPY_SOCKETS
984 int atomic = sosendallatonce(so) || top;
987 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
988 KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
989 ("sodgram_send: !PR_ATOMIC"));
992 resid = uio->uio_resid;
994 resid = top->m_pkthdr.len;
996 * In theory resid should be unsigned. However, space must be
997 * signed, as it might be less than 0 if we over-committed, and we
998 * must use a signed comparison of space and resid. On the other
999 * hand, a negative resid causes us to loop sending 0-length
1000 * segments to the protocol.
1008 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
1010 td->td_ru.ru_msgsnd++;
1011 if (control != NULL)
1012 clen = control->m_len;
1014 SOCKBUF_LOCK(&so->so_snd);
1015 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1016 SOCKBUF_UNLOCK(&so->so_snd);
1021 error = so->so_error;
1023 SOCKBUF_UNLOCK(&so->so_snd);
1026 if ((so->so_state & SS_ISCONNECTED) == 0) {
1028 * `sendto' and `sendmsg' is allowed on a connection-based
1029 * socket if it supports implied connect. Return ENOTCONN if
1030 * not connected and no address is supplied.
1032 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1033 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1034 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1035 !(resid == 0 && clen != 0)) {
1036 SOCKBUF_UNLOCK(&so->so_snd);
1040 } else if (addr == NULL) {
1041 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1044 error = EDESTADDRREQ;
1045 SOCKBUF_UNLOCK(&so->so_snd);
1051 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
1052 * problem and need fixing.
1054 space = sbspace(&so->so_snd);
1055 if (flags & MSG_OOB)
1058 SOCKBUF_UNLOCK(&so->so_snd);
1059 if (resid > space) {
1065 if (flags & MSG_EOR)
1066 top->m_flags |= M_EOR;
1068 #ifdef ZERO_COPY_SOCKETS
1069 error = sosend_copyin(uio, &top, atomic, &space, flags);
1074 * Copy the data from userland into a mbuf chain.
1075 * If no data is to be copied in, a single empty mbuf
1078 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1079 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1081 error = EFAULT; /* only possible error */
1084 space -= resid - uio->uio_resid;
1086 resid = uio->uio_resid;
1088 KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1090 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1095 so->so_options |= SO_DONTROUTE;
1099 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1100 * of date. We could have recieved a reset packet in an interrupt or
1101 * maybe we slept while doing page faults in uiomove() etc. We could
1102 * probably recheck again inside the locking protection here, but
1103 * there are probably other places that this also happens. We must
1107 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1108 (flags & MSG_OOB) ? PRUS_OOB :
1110 * If the user set MSG_EOF, the protocol understands this flag and
1111 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1113 ((flags & MSG_EOF) &&
1114 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1117 /* If there is more to send set PRUS_MORETOCOME */
1118 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1119 top, addr, control, td);
1122 so->so_options &= ~SO_DONTROUTE;
1131 if (control != NULL)
1137 * Send on a socket. If send must go all at once and message is larger than
1138 * send buffering, then hard error. Lock against other senders. If must go
1139 * all at once and not enough room now, then inform user that this would
1140 * block and do nothing. Otherwise, if nonblocking, send as much as
1141 * possible. The data to be sent is described by "uio" if nonzero, otherwise
1142 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1143 * in mbuf chain must be small enough to send all at once.
1145 * Returns nonzero on error, timeout or signal; callers must check for short
1146 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1150 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1151 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1154 int clen = 0, error, dontroute;
1155 int atomic = sosendallatonce(so) || top;
1158 resid = uio->uio_resid;
1160 resid = top->m_pkthdr.len;
1162 * In theory resid should be unsigned. However, space must be
1163 * signed, as it might be less than 0 if we over-committed, and we
1164 * must use a signed comparison of space and resid. On the other
1165 * hand, a negative resid causes us to loop sending 0-length
1166 * segments to the protocol.
1168 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1169 * type sockets since that's an error.
1171 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1177 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1178 (so->so_proto->pr_flags & PR_ATOMIC);
1180 td->td_ru.ru_msgsnd++;
1181 if (control != NULL)
1182 clen = control->m_len;
1184 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1190 SOCKBUF_LOCK(&so->so_snd);
1191 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1192 SOCKBUF_UNLOCK(&so->so_snd);
1197 error = so->so_error;
1199 SOCKBUF_UNLOCK(&so->so_snd);
1202 if ((so->so_state & SS_ISCONNECTED) == 0) {
1204 * `sendto' and `sendmsg' is allowed on a connection-
1205 * based socket if it supports implied connect.
1206 * Return ENOTCONN if not connected and no address is
1209 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1210 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1211 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1212 !(resid == 0 && clen != 0)) {
1213 SOCKBUF_UNLOCK(&so->so_snd);
1217 } else if (addr == NULL) {
1218 SOCKBUF_UNLOCK(&so->so_snd);
1219 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1222 error = EDESTADDRREQ;
1226 space = sbspace(&so->so_snd);
1227 if (flags & MSG_OOB)
1229 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1230 clen > so->so_snd.sb_hiwat) {
1231 SOCKBUF_UNLOCK(&so->so_snd);
1235 if (space < resid + clen &&
1236 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1237 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
1238 SOCKBUF_UNLOCK(&so->so_snd);
1239 error = EWOULDBLOCK;
1242 error = sbwait(&so->so_snd);
1243 SOCKBUF_UNLOCK(&so->so_snd);
1248 SOCKBUF_UNLOCK(&so->so_snd);
1253 if (flags & MSG_EOR)
1254 top->m_flags |= M_EOR;
1256 #ifdef ZERO_COPY_SOCKETS
1257 error = sosend_copyin(uio, &top, atomic,
1263 * Copy the data from userland into a mbuf
1264 * chain. If no data is to be copied in,
1265 * a single empty mbuf is returned.
1267 top = m_uiotombuf(uio, M_WAITOK, space,
1268 (atomic ? max_hdr : 0),
1269 (atomic ? M_PKTHDR : 0) |
1270 ((flags & MSG_EOR) ? M_EOR : 0));
1272 error = EFAULT; /* only possible error */
1275 space -= resid - uio->uio_resid;
1277 resid = uio->uio_resid;
1281 so->so_options |= SO_DONTROUTE;
1285 * XXX all the SBS_CANTSENDMORE checks previously
1286 * done could be out of date. We could have recieved
1287 * a reset packet in an interrupt or maybe we slept
1288 * while doing page faults in uiomove() etc. We
1289 * could probably recheck again inside the locking
1290 * protection here, but there are probably other
1291 * places that this also happens. We must rethink
1295 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1296 (flags & MSG_OOB) ? PRUS_OOB :
1298 * If the user set MSG_EOF, the protocol understands
1299 * this flag and nothing left to send then use
1300 * PRU_SEND_EOF instead of PRU_SEND.
1302 ((flags & MSG_EOF) &&
1303 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1306 /* If there is more to send set PRUS_MORETOCOME. */
1307 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1308 top, addr, control, td);
1311 so->so_options &= ~SO_DONTROUTE;
1319 } while (resid && space > 0);
1323 sbunlock(&so->so_snd);
1327 if (control != NULL)
1333 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1334 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1338 CURVNET_SET(so->so_vnet);
1339 error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1340 control, flags, td);
1346 * The part of soreceive() that implements reading non-inline out-of-band
1347 * data from a socket. For more complete comments, see soreceive(), from
1348 * which this code originated.
1350 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1351 * unable to return an mbuf chain to the caller.
1354 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1356 struct protosw *pr = so->so_proto;
1360 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1363 m = m_get(M_WAIT, MT_DATA);
1364 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1368 #ifdef ZERO_COPY_SOCKETS
1369 if (so_zero_copy_receive) {
1372 if ((m->m_flags & M_EXT)
1373 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1378 error = uiomoveco(mtod(m, void *),
1379 min(uio->uio_resid, m->m_len),
1382 #endif /* ZERO_COPY_SOCKETS */
1383 error = uiomove(mtod(m, void *),
1384 (int) min(uio->uio_resid, m->m_len), uio);
1386 } while (uio->uio_resid && error == 0 && m);
1394 * Following replacement or removal of the first mbuf on the first mbuf chain
1395 * of a socket buffer, push necessary state changes back into the socket
1396 * buffer so that other consumers see the values consistently. 'nextrecord'
1397 * is the callers locally stored value of the original value of
1398 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1399 * NOTE: 'nextrecord' may be NULL.
1401 static __inline void
1402 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1405 SOCKBUF_LOCK_ASSERT(sb);
1407 * First, update for the new value of nextrecord. If necessary, make
1408 * it the first record.
1410 if (sb->sb_mb != NULL)
1411 sb->sb_mb->m_nextpkt = nextrecord;
1413 sb->sb_mb = nextrecord;
1416 * Now update any dependent socket buffer fields to reflect the new
1417 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
1418 * addition of a second clause that takes care of the case where
1419 * sb_mb has been updated, but remains the last record.
1421 if (sb->sb_mb == NULL) {
1422 sb->sb_mbtail = NULL;
1423 sb->sb_lastrecord = NULL;
1424 } else if (sb->sb_mb->m_nextpkt == NULL)
1425 sb->sb_lastrecord = sb->sb_mb;
1430 * Implement receive operations on a socket. We depend on the way that
1431 * records are added to the sockbuf by sbappend. In particular, each record
1432 * (mbufs linked through m_next) must begin with an address if the protocol
1433 * so specifies, followed by an optional mbuf or mbufs containing ancillary
1434 * data, and then zero or more mbufs of data. In order to allow parallelism
1435 * between network receive and copying to user space, as well as avoid
1436 * sleeping with a mutex held, we release the socket buffer mutex during the
1437 * user space copy. Although the sockbuf is locked, new data may still be
1438 * appended, and thus we must maintain consistency of the sockbuf during that
1441 * The caller may receive the data as a single mbuf chain by supplying an
1442 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1443 * the count in uio_resid.
1446 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1447 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1449 struct mbuf *m, **mp;
1450 int flags, len, error, offset;
1451 struct protosw *pr = so->so_proto;
1452 struct mbuf *nextrecord;
1454 int orig_resid = uio->uio_resid;
1459 if (controlp != NULL)
1462 flags = *flagsp &~ MSG_EOR;
1465 if (flags & MSG_OOB)
1466 return (soreceive_rcvoob(so, uio, flags));
1469 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1470 && uio->uio_resid) {
1472 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1475 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1480 SOCKBUF_LOCK(&so->so_rcv);
1481 m = so->so_rcv.sb_mb;
1483 * If we have less data than requested, block awaiting more (subject
1484 * to any timeout) if:
1485 * 1. the current count is less than the low water mark, or
1486 * 2. MSG_DONTWAIT is not set
1488 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1489 so->so_rcv.sb_cc < uio->uio_resid) &&
1490 so->so_rcv.sb_cc < so->so_rcv.sb_lowat &&
1491 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1492 KASSERT(m != NULL || !so->so_rcv.sb_cc,
1493 ("receive: m == %p so->so_rcv.sb_cc == %u",
1494 m, so->so_rcv.sb_cc));
1498 error = so->so_error;
1499 if ((flags & MSG_PEEK) == 0)
1501 SOCKBUF_UNLOCK(&so->so_rcv);
1504 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1505 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1507 SOCKBUF_UNLOCK(&so->so_rcv);
1512 for (; m != NULL; m = m->m_next)
1513 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1514 m = so->so_rcv.sb_mb;
1517 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1518 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1519 SOCKBUF_UNLOCK(&so->so_rcv);
1523 if (uio->uio_resid == 0) {
1524 SOCKBUF_UNLOCK(&so->so_rcv);
1527 if ((so->so_state & SS_NBIO) ||
1528 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1529 SOCKBUF_UNLOCK(&so->so_rcv);
1530 error = EWOULDBLOCK;
1533 SBLASTRECORDCHK(&so->so_rcv);
1534 SBLASTMBUFCHK(&so->so_rcv);
1535 error = sbwait(&so->so_rcv);
1536 SOCKBUF_UNLOCK(&so->so_rcv);
1543 * From this point onward, we maintain 'nextrecord' as a cache of the
1544 * pointer to the next record in the socket buffer. We must keep the
1545 * various socket buffer pointers and local stack versions of the
1546 * pointers in sync, pushing out modifications before dropping the
1547 * socket buffer mutex, and re-reading them when picking it up.
1549 * Otherwise, we will race with the network stack appending new data
1550 * or records onto the socket buffer by using inconsistent/stale
1551 * versions of the field, possibly resulting in socket buffer
1554 * By holding the high-level sblock(), we prevent simultaneous
1555 * readers from pulling off the front of the socket buffer.
1557 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1559 uio->uio_td->td_ru.ru_msgrcv++;
1560 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1561 SBLASTRECORDCHK(&so->so_rcv);
1562 SBLASTMBUFCHK(&so->so_rcv);
1563 nextrecord = m->m_nextpkt;
1564 if (pr->pr_flags & PR_ADDR) {
1565 KASSERT(m->m_type == MT_SONAME,
1566 ("m->m_type == %d", m->m_type));
1569 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1571 if (flags & MSG_PEEK) {
1574 sbfree(&so->so_rcv, m);
1575 so->so_rcv.sb_mb = m_free(m);
1576 m = so->so_rcv.sb_mb;
1577 sockbuf_pushsync(&so->so_rcv, nextrecord);
1582 * Process one or more MT_CONTROL mbufs present before any data mbufs
1583 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1584 * just copy the data; if !MSG_PEEK, we call into the protocol to
1585 * perform externalization (or freeing if controlp == NULL).
1587 if (m != NULL && m->m_type == MT_CONTROL) {
1588 struct mbuf *cm = NULL, *cmn;
1589 struct mbuf **cme = &cm;
1592 if (flags & MSG_PEEK) {
1593 if (controlp != NULL) {
1594 *controlp = m_copy(m, 0, m->m_len);
1595 controlp = &(*controlp)->m_next;
1599 sbfree(&so->so_rcv, m);
1600 so->so_rcv.sb_mb = m->m_next;
1603 cme = &(*cme)->m_next;
1604 m = so->so_rcv.sb_mb;
1606 } while (m != NULL && m->m_type == MT_CONTROL);
1607 if ((flags & MSG_PEEK) == 0)
1608 sockbuf_pushsync(&so->so_rcv, nextrecord);
1609 while (cm != NULL) {
1612 if (pr->pr_domain->dom_externalize != NULL) {
1613 SOCKBUF_UNLOCK(&so->so_rcv);
1615 error = (*pr->pr_domain->dom_externalize)
1617 SOCKBUF_LOCK(&so->so_rcv);
1618 } else if (controlp != NULL)
1622 if (controlp != NULL) {
1624 while (*controlp != NULL)
1625 controlp = &(*controlp)->m_next;
1630 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1632 nextrecord = so->so_rcv.sb_mb;
1636 if ((flags & MSG_PEEK) == 0) {
1637 KASSERT(m->m_nextpkt == nextrecord,
1638 ("soreceive: post-control, nextrecord !sync"));
1639 if (nextrecord == NULL) {
1640 KASSERT(so->so_rcv.sb_mb == m,
1641 ("soreceive: post-control, sb_mb!=m"));
1642 KASSERT(so->so_rcv.sb_lastrecord == m,
1643 ("soreceive: post-control, lastrecord!=m"));
1647 if (type == MT_OOBDATA)
1650 if ((flags & MSG_PEEK) == 0) {
1651 KASSERT(so->so_rcv.sb_mb == nextrecord,
1652 ("soreceive: sb_mb != nextrecord"));
1653 if (so->so_rcv.sb_mb == NULL) {
1654 KASSERT(so->so_rcv.sb_lastrecord == NULL,
1655 ("soreceive: sb_lastercord != NULL"));
1659 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1660 SBLASTRECORDCHK(&so->so_rcv);
1661 SBLASTMBUFCHK(&so->so_rcv);
1664 * Now continue to read any data mbufs off of the head of the socket
1665 * buffer until the read request is satisfied. Note that 'type' is
1666 * used to store the type of any mbuf reads that have happened so far
1667 * such that soreceive() can stop reading if the type changes, which
1668 * causes soreceive() to return only one of regular data and inline
1669 * out-of-band data in a single socket receive operation.
1673 while (m != NULL && uio->uio_resid > 0 && error == 0) {
1675 * If the type of mbuf has changed since the last mbuf
1676 * examined ('type'), end the receive operation.
1678 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1679 if (m->m_type == MT_OOBDATA || m->m_type == MT_CONTROL) {
1680 if (type != m->m_type)
1682 } else if (type == MT_OOBDATA)
1685 KASSERT(m->m_type == MT_DATA,
1686 ("m->m_type == %d", m->m_type));
1687 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1688 len = uio->uio_resid;
1689 if (so->so_oobmark && len > so->so_oobmark - offset)
1690 len = so->so_oobmark - offset;
1691 if (len > m->m_len - moff)
1692 len = m->m_len - moff;
1694 * If mp is set, just pass back the mbufs. Otherwise copy
1695 * them out via the uio, then free. Sockbuf must be
1696 * consistent here (points to current mbuf, it points to next
1697 * record) when we drop priority; we must note any additions
1698 * to the sockbuf when we block interrupts again.
1701 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1702 SBLASTRECORDCHK(&so->so_rcv);
1703 SBLASTMBUFCHK(&so->so_rcv);
1704 SOCKBUF_UNLOCK(&so->so_rcv);
1705 #ifdef ZERO_COPY_SOCKETS
1706 if (so_zero_copy_receive) {
1709 if ((m->m_flags & M_EXT)
1710 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1715 error = uiomoveco(mtod(m, char *) + moff,
1719 #endif /* ZERO_COPY_SOCKETS */
1720 error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1721 SOCKBUF_LOCK(&so->so_rcv);
1724 * The MT_SONAME mbuf has already been removed
1725 * from the record, so it is necessary to
1726 * remove the data mbufs, if any, to preserve
1727 * the invariant in the case of PR_ADDR that
1728 * requires MT_SONAME mbufs at the head of
1731 if (m && pr->pr_flags & PR_ATOMIC &&
1732 ((flags & MSG_PEEK) == 0))
1733 (void)sbdroprecord_locked(&so->so_rcv);
1734 SOCKBUF_UNLOCK(&so->so_rcv);
1738 uio->uio_resid -= len;
1739 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1740 if (len == m->m_len - moff) {
1741 if (m->m_flags & M_EOR)
1743 if (flags & MSG_PEEK) {
1747 nextrecord = m->m_nextpkt;
1748 sbfree(&so->so_rcv, m);
1752 so->so_rcv.sb_mb = m = m->m_next;
1755 so->so_rcv.sb_mb = m_free(m);
1756 m = so->so_rcv.sb_mb;
1758 sockbuf_pushsync(&so->so_rcv, nextrecord);
1759 SBLASTRECORDCHK(&so->so_rcv);
1760 SBLASTMBUFCHK(&so->so_rcv);
1763 if (flags & MSG_PEEK)
1769 if (flags & MSG_DONTWAIT)
1770 copy_flag = M_DONTWAIT;
1773 if (copy_flag == M_WAIT)
1774 SOCKBUF_UNLOCK(&so->so_rcv);
1775 *mp = m_copym(m, 0, len, copy_flag);
1776 if (copy_flag == M_WAIT)
1777 SOCKBUF_LOCK(&so->so_rcv);
1780 * m_copym() couldn't
1781 * allocate an mbuf. Adjust
1782 * uio_resid back (it was
1783 * adjusted down by len
1784 * bytes, which we didn't end
1785 * up "copying" over).
1787 uio->uio_resid += len;
1793 so->so_rcv.sb_cc -= len;
1796 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1797 if (so->so_oobmark) {
1798 if ((flags & MSG_PEEK) == 0) {
1799 so->so_oobmark -= len;
1800 if (so->so_oobmark == 0) {
1801 so->so_rcv.sb_state |= SBS_RCVATMARK;
1806 if (offset == so->so_oobmark)
1810 if (flags & MSG_EOR)
1813 * If the MSG_WAITALL flag is set (for non-atomic socket), we
1814 * must not quit until "uio->uio_resid == 0" or an error
1815 * termination. If a signal/timeout occurs, return with a
1816 * short count but without error. Keep sockbuf locked
1817 * against other readers.
1819 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1820 !sosendallatonce(so) && nextrecord == NULL) {
1821 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1822 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
1825 * Notify the protocol that some data has been
1826 * drained before blocking.
1828 if (pr->pr_flags & PR_WANTRCVD) {
1829 SOCKBUF_UNLOCK(&so->so_rcv);
1831 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1832 SOCKBUF_LOCK(&so->so_rcv);
1834 SBLASTRECORDCHK(&so->so_rcv);
1835 SBLASTMBUFCHK(&so->so_rcv);
1837 * We could receive some data while was notifying
1838 * the protocol. Skip blocking in this case.
1840 if (so->so_rcv.sb_mb == NULL) {
1841 error = sbwait(&so->so_rcv);
1843 SOCKBUF_UNLOCK(&so->so_rcv);
1847 m = so->so_rcv.sb_mb;
1849 nextrecord = m->m_nextpkt;
1853 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1854 if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1856 if ((flags & MSG_PEEK) == 0)
1857 (void) sbdroprecord_locked(&so->so_rcv);
1859 if ((flags & MSG_PEEK) == 0) {
1862 * First part is an inline SB_EMPTY_FIXUP(). Second
1863 * part makes sure sb_lastrecord is up-to-date if
1864 * there is still data in the socket buffer.
1866 so->so_rcv.sb_mb = nextrecord;
1867 if (so->so_rcv.sb_mb == NULL) {
1868 so->so_rcv.sb_mbtail = NULL;
1869 so->so_rcv.sb_lastrecord = NULL;
1870 } else if (nextrecord->m_nextpkt == NULL)
1871 so->so_rcv.sb_lastrecord = nextrecord;
1873 SBLASTRECORDCHK(&so->so_rcv);
1874 SBLASTMBUFCHK(&so->so_rcv);
1876 * If soreceive() is being done from the socket callback,
1877 * then don't need to generate ACK to peer to update window,
1878 * since ACK will be generated on return to TCP.
1880 if (!(flags & MSG_SOCALLBCK) &&
1881 (pr->pr_flags & PR_WANTRCVD)) {
1882 SOCKBUF_UNLOCK(&so->so_rcv);
1884 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1885 SOCKBUF_LOCK(&so->so_rcv);
1888 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1889 if (orig_resid == uio->uio_resid && orig_resid &&
1890 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1891 SOCKBUF_UNLOCK(&so->so_rcv);
1894 SOCKBUF_UNLOCK(&so->so_rcv);
1899 sbunlock(&so->so_rcv);
1904 * Optimized version of soreceive() for stream (TCP) sockets.
1907 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
1908 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1910 int len = 0, error = 0, flags, oresid;
1912 struct mbuf *m, *n = NULL;
1914 /* We only do stream sockets. */
1915 if (so->so_type != SOCK_STREAM)
1919 if (controlp != NULL)
1922 flags = *flagsp &~ MSG_EOR;
1925 if (flags & MSG_OOB)
1926 return (soreceive_rcvoob(so, uio, flags));
1932 /* Prevent other readers from entering the socket. */
1933 error = sblock(sb, SBLOCKWAIT(flags));
1938 /* Easy one, no space to copyout anything. */
1939 if (uio->uio_resid == 0) {
1943 oresid = uio->uio_resid;
1945 /* We will never ever get anything unless we are connected. */
1946 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1947 /* When disconnecting there may be still some data left. */
1950 if (!(so->so_state & SS_ISDISCONNECTED))
1955 /* Socket buffer is empty and we shall not block. */
1956 if (sb->sb_cc == 0 &&
1957 ((sb->sb_flags & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
1963 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1965 /* Abort if socket has reported problems. */
1969 if (oresid > uio->uio_resid)
1971 error = so->so_error;
1972 if (!(flags & MSG_PEEK))
1977 /* Door is closed. Deliver what is left, if any. */
1978 if (sb->sb_state & SBS_CANTRCVMORE) {
1985 /* Socket buffer got some data that we shall deliver now. */
1986 if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) &&
1987 ((sb->sb_flags & SS_NBIO) ||
1988 (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
1989 sb->sb_cc >= sb->sb_lowat ||
1990 sb->sb_cc >= uio->uio_resid ||
1991 sb->sb_cc >= sb->sb_hiwat) ) {
1995 /* On MSG_WAITALL we must wait until all data or error arrives. */
1996 if ((flags & MSG_WAITALL) &&
1997 (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_lowat))
2001 * Wait and block until (more) data comes in.
2002 * NB: Drops the sockbuf lock during wait.
2010 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2011 KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__));
2012 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
2016 uio->uio_td->td_ru.ru_msgrcv++;
2018 /* Fill uio until full or current end of socket buffer is reached. */
2019 len = min(uio->uio_resid, sb->sb_cc);
2021 /* Dequeue as many mbufs as possible. */
2022 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
2023 for (*mp0 = m = sb->sb_mb;
2024 m != NULL && m->m_len <= len;
2027 uio->uio_resid -= m->m_len;
2032 if (sb->sb_mb == NULL)
2036 /* Copy the remainder. */
2038 KASSERT(sb->sb_mb != NULL,
2039 ("%s: len > 0 && sb->sb_mb empty", __func__));
2041 m = m_copym(sb->sb_mb, 0, len, M_DONTWAIT);
2043 len = 0; /* Don't flush data from sockbuf. */
2045 uio->uio_resid -= m->m_len;
2056 /* NB: Must unlock socket buffer as uiomove may sleep. */
2058 error = m_mbuftouio(uio, sb->sb_mb, len);
2063 SBLASTRECORDCHK(sb);
2067 * Remove the delivered data from the socket buffer unless we
2068 * were only peeking.
2070 if (!(flags & MSG_PEEK)) {
2072 sbdrop_locked(sb, len);
2074 /* Notify protocol that we drained some data. */
2075 if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
2076 (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
2077 !(flags & MSG_SOCALLBCK))) {
2080 (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
2086 * For MSG_WAITALL we may have to loop again and wait for
2087 * more data to come in.
2089 if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
2092 SOCKBUF_LOCK_ASSERT(sb);
2093 SBLASTRECORDCHK(sb);
2101 * Optimized version of soreceive() for simple datagram cases from userspace.
2102 * Unlike in the stream case, we're able to drop a datagram if copyout()
2103 * fails, and because we handle datagrams atomically, we don't need to use a
2104 * sleep lock to prevent I/O interlacing.
2107 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
2108 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2110 struct mbuf *m, *m2;
2111 int flags, len, error;
2112 struct protosw *pr = so->so_proto;
2113 struct mbuf *nextrecord;
2117 if (controlp != NULL)
2120 flags = *flagsp &~ MSG_EOR;
2125 * For any complicated cases, fall back to the full
2126 * soreceive_generic().
2128 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
2129 return (soreceive_generic(so, psa, uio, mp0, controlp,
2133 * Enforce restrictions on use.
2135 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
2136 ("soreceive_dgram: wantrcvd"));
2137 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
2138 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
2139 ("soreceive_dgram: SBS_RCVATMARK"));
2140 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
2141 ("soreceive_dgram: P_CONNREQUIRED"));
2144 * Loop blocking while waiting for a datagram.
2146 SOCKBUF_LOCK(&so->so_rcv);
2147 while ((m = so->so_rcv.sb_mb) == NULL) {
2148 KASSERT(so->so_rcv.sb_cc == 0,
2149 ("soreceive_dgram: sb_mb NULL but sb_cc %u",
2152 error = so->so_error;
2154 SOCKBUF_UNLOCK(&so->so_rcv);
2157 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
2158 uio->uio_resid == 0) {
2159 SOCKBUF_UNLOCK(&so->so_rcv);
2162 if ((so->so_state & SS_NBIO) ||
2163 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2164 SOCKBUF_UNLOCK(&so->so_rcv);
2165 return (EWOULDBLOCK);
2167 SBLASTRECORDCHK(&so->so_rcv);
2168 SBLASTMBUFCHK(&so->so_rcv);
2169 error = sbwait(&so->so_rcv);
2171 SOCKBUF_UNLOCK(&so->so_rcv);
2175 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2178 uio->uio_td->td_ru.ru_msgrcv++;
2179 SBLASTRECORDCHK(&so->so_rcv);
2180 SBLASTMBUFCHK(&so->so_rcv);
2181 nextrecord = m->m_nextpkt;
2182 if (nextrecord == NULL) {
2183 KASSERT(so->so_rcv.sb_lastrecord == m,
2184 ("soreceive_dgram: lastrecord != m"));
2187 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
2188 ("soreceive_dgram: m_nextpkt != nextrecord"));
2191 * Pull 'm' and its chain off the front of the packet queue.
2193 so->so_rcv.sb_mb = NULL;
2194 sockbuf_pushsync(&so->so_rcv, nextrecord);
2197 * Walk 'm's chain and free that many bytes from the socket buffer.
2199 for (m2 = m; m2 != NULL; m2 = m2->m_next)
2200 sbfree(&so->so_rcv, m2);
2203 * Do a few last checks before we let go of the lock.
2205 SBLASTRECORDCHK(&so->so_rcv);
2206 SBLASTMBUFCHK(&so->so_rcv);
2207 SOCKBUF_UNLOCK(&so->so_rcv);
2209 if (pr->pr_flags & PR_ADDR) {
2210 KASSERT(m->m_type == MT_SONAME,
2211 ("m->m_type == %d", m->m_type));
2213 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
2218 /* XXXRW: Can this happen? */
2223 * Packet to copyout() is now in 'm' and it is disconnected from the
2226 * Process one or more MT_CONTROL mbufs present before any data mbufs
2227 * in the first mbuf chain on the socket buffer. We call into the
2228 * protocol to perform externalization (or freeing if controlp ==
2231 if (m->m_type == MT_CONTROL) {
2232 struct mbuf *cm = NULL, *cmn;
2233 struct mbuf **cme = &cm;
2239 cme = &(*cme)->m_next;
2241 } while (m != NULL && m->m_type == MT_CONTROL);
2242 while (cm != NULL) {
2245 if (pr->pr_domain->dom_externalize != NULL) {
2246 error = (*pr->pr_domain->dom_externalize)
2248 } else if (controlp != NULL)
2252 if (controlp != NULL) {
2253 while (*controlp != NULL)
2254 controlp = &(*controlp)->m_next;
2259 KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
2261 while (m != NULL && uio->uio_resid > 0) {
2262 len = uio->uio_resid;
2265 error = uiomove(mtod(m, char *), (int)len, uio);
2270 if (len == m->m_len)
2286 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2287 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2291 CURVNET_SET(so->so_vnet);
2292 error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
2299 soshutdown(struct socket *so, int how)
2301 struct protosw *pr = so->so_proto;
2304 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2307 CURVNET_SET(so->so_vnet);
2308 if (pr->pr_usrreqs->pru_flush != NULL) {
2309 (*pr->pr_usrreqs->pru_flush)(so, how);
2313 if (how != SHUT_RD) {
2314 error = (*pr->pr_usrreqs->pru_shutdown)(so);
2323 sorflush(struct socket *so)
2325 struct sockbuf *sb = &so->so_rcv;
2326 struct protosw *pr = so->so_proto;
2332 * In order to avoid calling dom_dispose with the socket buffer mutex
2333 * held, and in order to generally avoid holding the lock for a long
2334 * time, we make a copy of the socket buffer and clear the original
2335 * (except locks, state). The new socket buffer copy won't have
2336 * initialized locks so we can only call routines that won't use or
2337 * assert those locks.
2339 * Dislodge threads currently blocked in receive and wait to acquire
2340 * a lock against other simultaneous readers before clearing the
2341 * socket buffer. Don't let our acquire be interrupted by a signal
2342 * despite any existing socket disposition on interruptable waiting.
2345 (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2348 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2349 * and mutex data unchanged.
2352 bzero(&asb, offsetof(struct sockbuf, sb_startzero));
2353 bcopy(&sb->sb_startzero, &asb.sb_startzero,
2354 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2355 bzero(&sb->sb_startzero,
2356 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2361 * Dispose of special rights and flush the socket buffer. Don't call
2362 * any unsafe routines (that rely on locks being initialized) on asb.
2364 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2365 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
2366 sbrelease_internal(&asb, so);
2370 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2371 * additional variant to handle the case where the option value needs to be
2372 * some kind of integer, but not a specific size. In addition to their use
2373 * here, these functions are also called by the protocol-level pr_ctloutput()
2377 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2382 * If the user gives us more than we wanted, we ignore it, but if we
2383 * don't get the minimum length the caller wants, we return EINVAL.
2384 * On success, sopt->sopt_valsize is set to however much we actually
2387 if ((valsize = sopt->sopt_valsize) < minlen)
2390 sopt->sopt_valsize = valsize = len;
2392 if (sopt->sopt_td != NULL)
2393 return (copyin(sopt->sopt_val, buf, valsize));
2395 bcopy(sopt->sopt_val, buf, valsize);
2400 * Kernel version of setsockopt(2).
2402 * XXX: optlen is size_t, not socklen_t
2405 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2408 struct sockopt sopt;
2410 sopt.sopt_level = level;
2411 sopt.sopt_name = optname;
2412 sopt.sopt_dir = SOPT_SET;
2413 sopt.sopt_val = optval;
2414 sopt.sopt_valsize = optlen;
2415 sopt.sopt_td = NULL;
2416 return (sosetopt(so, &sopt));
2420 sosetopt(struct socket *so, struct sockopt *sopt)
2430 CURVNET_SET(so->so_vnet);
2432 if (sopt->sopt_level != SOL_SOCKET) {
2433 if (so->so_proto->pr_ctloutput != NULL) {
2434 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2438 error = ENOPROTOOPT;
2440 switch (sopt->sopt_name) {
2442 case SO_ACCEPTFILTER:
2443 error = do_setopt_accept_filter(so, sopt);
2449 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2454 so->so_linger = l.l_linger;
2456 so->so_options |= SO_LINGER;
2458 so->so_options &= ~SO_LINGER;
2465 case SO_USELOOPBACK:
2475 error = sooptcopyin(sopt, &optval, sizeof optval,
2481 so->so_options |= sopt->sopt_name;
2483 so->so_options &= ~sopt->sopt_name;
2488 error = sooptcopyin(sopt, &optval, sizeof optval,
2490 if (optval < 0 || optval >= rt_numfibs) {
2494 if (((so->so_proto->pr_domain->dom_family == PF_INET) ||
2495 (so->so_proto->pr_domain->dom_family == PF_INET6) ||
2496 (so->so_proto->pr_domain->dom_family == PF_ROUTE))) {
2497 so->so_fibnum = optval;
2498 /* Note: ignore error */
2499 if (so->so_proto->pr_ctloutput)
2500 (*so->so_proto->pr_ctloutput)(so, sopt);
2509 error = sooptcopyin(sopt, &optval, sizeof optval,
2515 * Values < 1 make no sense for any of these options,
2523 switch (sopt->sopt_name) {
2526 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
2527 &so->so_snd : &so->so_rcv, (u_long)optval,
2528 so, curthread) == 0) {
2532 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
2533 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
2537 * Make sure the low-water is never greater than the
2541 SOCKBUF_LOCK(&so->so_snd);
2542 so->so_snd.sb_lowat =
2543 (optval > so->so_snd.sb_hiwat) ?
2544 so->so_snd.sb_hiwat : optval;
2545 SOCKBUF_UNLOCK(&so->so_snd);
2548 SOCKBUF_LOCK(&so->so_rcv);
2549 so->so_rcv.sb_lowat =
2550 (optval > so->so_rcv.sb_hiwat) ?
2551 so->so_rcv.sb_hiwat : optval;
2552 SOCKBUF_UNLOCK(&so->so_rcv);
2559 #ifdef COMPAT_FREEBSD32
2560 if (SV_CURPROC_FLAG(SV_ILP32)) {
2561 struct timeval32 tv32;
2563 error = sooptcopyin(sopt, &tv32, sizeof tv32,
2565 CP(tv32, tv, tv_sec);
2566 CP(tv32, tv, tv_usec);
2569 error = sooptcopyin(sopt, &tv, sizeof tv,
2573 if (tv.tv_sec < 0 || tv.tv_usec < 0 ||
2574 tv.tv_usec >= 1000000) {
2580 switch (sopt->sopt_name) {
2582 so->so_snd.sb_timeo = val;
2585 so->so_rcv.sb_timeo = val;
2592 error = sooptcopyin(sopt, &extmac, sizeof extmac,
2596 error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2604 error = ENOPROTOOPT;
2607 if (error == 0 && so->so_proto->pr_ctloutput != NULL)
2608 (void)(*so->so_proto->pr_ctloutput)(so, sopt);
2616 * Helper routine for getsockopt.
2619 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2627 * Documented get behavior is that we always return a value, possibly
2628 * truncated to fit in the user's buffer. Traditional behavior is
2629 * that we always tell the user precisely how much we copied, rather
2630 * than something useful like the total amount we had available for
2631 * her. Note that this interface is not idempotent; the entire
2632 * answer must generated ahead of time.
2634 valsize = min(len, sopt->sopt_valsize);
2635 sopt->sopt_valsize = valsize;
2636 if (sopt->sopt_val != NULL) {
2637 if (sopt->sopt_td != NULL)
2638 error = copyout(buf, sopt->sopt_val, valsize);
2640 bcopy(buf, sopt->sopt_val, valsize);
2646 sogetopt(struct socket *so, struct sockopt *sopt)
2655 CURVNET_SET(so->so_vnet);
2657 if (sopt->sopt_level != SOL_SOCKET) {
2658 if (so->so_proto->pr_ctloutput != NULL)
2659 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2661 error = ENOPROTOOPT;
2665 switch (sopt->sopt_name) {
2667 case SO_ACCEPTFILTER:
2668 error = do_getopt_accept_filter(so, sopt);
2673 l.l_onoff = so->so_options & SO_LINGER;
2674 l.l_linger = so->so_linger;
2676 error = sooptcopyout(sopt, &l, sizeof l);
2679 case SO_USELOOPBACK:
2691 optval = so->so_options & sopt->sopt_name;
2693 error = sooptcopyout(sopt, &optval, sizeof optval);
2697 optval = so->so_type;
2701 optval = so->so_proto->pr_protocol;
2706 optval = so->so_error;
2712 optval = so->so_snd.sb_hiwat;
2716 optval = so->so_rcv.sb_hiwat;
2720 optval = so->so_snd.sb_lowat;
2724 optval = so->so_rcv.sb_lowat;
2729 optval = (sopt->sopt_name == SO_SNDTIMEO ?
2730 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2732 tv.tv_sec = optval / hz;
2733 tv.tv_usec = (optval % hz) * tick;
2734 #ifdef COMPAT_FREEBSD32
2735 if (SV_CURPROC_FLAG(SV_ILP32)) {
2736 struct timeval32 tv32;
2738 CP(tv, tv32, tv_sec);
2739 CP(tv, tv32, tv_usec);
2740 error = sooptcopyout(sopt, &tv32, sizeof tv32);
2743 error = sooptcopyout(sopt, &tv, sizeof tv);
2748 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2752 error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
2756 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2764 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2768 error = mac_getsockopt_peerlabel(
2769 sopt->sopt_td->td_ucred, so, &extmac);
2772 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2778 case SO_LISTENQLIMIT:
2779 optval = so->so_qlimit;
2783 optval = so->so_qlen;
2786 case SO_LISTENINCQLEN:
2787 optval = so->so_incqlen;
2791 error = ENOPROTOOPT;
2802 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2804 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2806 struct mbuf *m, *m_prev;
2807 int sopt_size = sopt->sopt_valsize;
2809 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2812 if (sopt_size > MLEN) {
2813 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
2814 if ((m->m_flags & M_EXT) == 0) {
2818 m->m_len = min(MCLBYTES, sopt_size);
2820 m->m_len = min(MLEN, sopt_size);
2822 sopt_size -= m->m_len;
2827 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2832 if (sopt_size > MLEN) {
2833 MCLGET(m, sopt->sopt_td != NULL ? M_WAIT :
2835 if ((m->m_flags & M_EXT) == 0) {
2840 m->m_len = min(MCLBYTES, sopt_size);
2842 m->m_len = min(MLEN, sopt_size);
2844 sopt_size -= m->m_len;
2851 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2853 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2855 struct mbuf *m0 = m;
2857 if (sopt->sopt_val == NULL)
2859 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2860 if (sopt->sopt_td != NULL) {
2863 error = copyin(sopt->sopt_val, mtod(m, char *),
2870 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2871 sopt->sopt_valsize -= m->m_len;
2872 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2875 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2876 panic("ip6_sooptmcopyin");
2880 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2882 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2884 struct mbuf *m0 = m;
2887 if (sopt->sopt_val == NULL)
2889 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2890 if (sopt->sopt_td != NULL) {
2893 error = copyout(mtod(m, char *), sopt->sopt_val,
2900 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2901 sopt->sopt_valsize -= m->m_len;
2902 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2903 valsize += m->m_len;
2907 /* enough soopt buffer should be given from user-land */
2911 sopt->sopt_valsize = valsize;
2916 * sohasoutofband(): protocol notifies socket layer of the arrival of new
2917 * out-of-band data, which will then notify socket consumers.
2920 sohasoutofband(struct socket *so)
2923 if (so->so_sigio != NULL)
2924 pgsigio(&so->so_sigio, SIGURG, 0);
2925 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
2929 sopoll(struct socket *so, int events, struct ucred *active_cred,
2934 * We do not need to set or assert curvnet as long as everyone uses
2937 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
2942 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
2947 SOCKBUF_LOCK(&so->so_snd);
2948 SOCKBUF_LOCK(&so->so_rcv);
2949 if (events & (POLLIN | POLLRDNORM))
2950 if (soreadabledata(so))
2951 revents |= events & (POLLIN | POLLRDNORM);
2953 if (events & (POLLOUT | POLLWRNORM))
2954 if (sowriteable(so))
2955 revents |= events & (POLLOUT | POLLWRNORM);
2957 if (events & (POLLPRI | POLLRDBAND))
2958 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
2959 revents |= events & (POLLPRI | POLLRDBAND);
2961 if ((events & POLLINIGNEOF) == 0) {
2962 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2963 revents |= events & (POLLIN | POLLRDNORM);
2964 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
2970 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
2971 selrecord(td, &so->so_rcv.sb_sel);
2972 so->so_rcv.sb_flags |= SB_SEL;
2975 if (events & (POLLOUT | POLLWRNORM)) {
2976 selrecord(td, &so->so_snd.sb_sel);
2977 so->so_snd.sb_flags |= SB_SEL;
2981 SOCKBUF_UNLOCK(&so->so_rcv);
2982 SOCKBUF_UNLOCK(&so->so_snd);
2987 soo_kqfilter(struct file *fp, struct knote *kn)
2989 struct socket *so = kn->kn_fp->f_data;
2992 switch (kn->kn_filter) {
2994 if (so->so_options & SO_ACCEPTCONN)
2995 kn->kn_fop = &solisten_filtops;
2997 kn->kn_fop = &soread_filtops;
3001 kn->kn_fop = &sowrite_filtops;
3009 knlist_add(&sb->sb_sel.si_note, kn, 1);
3010 sb->sb_flags |= SB_KNOTE;
3016 * Some routines that return EOPNOTSUPP for entry points that are not
3017 * supported by a protocol. Fill in as needed.
3020 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
3027 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
3034 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3041 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3048 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
3055 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
3056 struct ifnet *ifp, struct thread *td)
3063 pru_disconnect_notsupp(struct socket *so)
3070 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
3077 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
3084 pru_rcvd_notsupp(struct socket *so, int flags)
3091 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
3098 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
3099 struct sockaddr *addr, struct mbuf *control, struct thread *td)
3106 * This isn't really a ``null'' operation, but it's the default one and
3107 * doesn't do anything destructive.
3110 pru_sense_null(struct socket *so, struct stat *sb)
3113 sb->st_blksize = so->so_snd.sb_hiwat;
3118 pru_shutdown_notsupp(struct socket *so)
3125 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
3132 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
3133 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
3140 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
3141 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
3148 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
3156 filt_sordetach(struct knote *kn)
3158 struct socket *so = kn->kn_fp->f_data;
3160 SOCKBUF_LOCK(&so->so_rcv);
3161 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
3162 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
3163 so->so_rcv.sb_flags &= ~SB_KNOTE;
3164 SOCKBUF_UNLOCK(&so->so_rcv);
3169 filt_soread(struct knote *kn, long hint)
3173 so = kn->kn_fp->f_data;
3174 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
3176 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
3177 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3178 kn->kn_flags |= EV_EOF;
3179 kn->kn_fflags = so->so_error;
3181 } else if (so->so_error) /* temporary udp error */
3183 else if (kn->kn_sfflags & NOTE_LOWAT)
3184 return (kn->kn_data >= kn->kn_sdata);
3186 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
3190 filt_sowdetach(struct knote *kn)
3192 struct socket *so = kn->kn_fp->f_data;
3194 SOCKBUF_LOCK(&so->so_snd);
3195 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
3196 if (knlist_empty(&so->so_snd.sb_sel.si_note))
3197 so->so_snd.sb_flags &= ~SB_KNOTE;
3198 SOCKBUF_UNLOCK(&so->so_snd);
3203 filt_sowrite(struct knote *kn, long hint)
3207 so = kn->kn_fp->f_data;
3208 SOCKBUF_LOCK_ASSERT(&so->so_snd);
3209 kn->kn_data = sbspace(&so->so_snd);
3210 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
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 (((so->so_state & SS_ISCONNECTED) == 0) &&
3217 (so->so_proto->pr_flags & PR_CONNREQUIRED))
3219 else if (kn->kn_sfflags & NOTE_LOWAT)
3220 return (kn->kn_data >= kn->kn_sdata);
3222 return (kn->kn_data >= so->so_snd.sb_lowat);
3227 filt_solisten(struct knote *kn, long hint)
3229 struct socket *so = kn->kn_fp->f_data;
3231 kn->kn_data = so->so_qlen;
3232 return (! TAILQ_EMPTY(&so->so_comp));
3236 socheckuid(struct socket *so, uid_t uid)
3241 if (so->so_cred->cr_uid != uid)
3247 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
3253 error = sysctl_handle_int(oidp, &val, 0, req);
3254 if (error || !req->newptr )
3257 if (val < 1 || val > USHRT_MAX)
3265 * These functions are used by protocols to notify the socket layer (and its
3266 * consumers) of state changes in the sockets driven by protocol-side events.
3270 * Procedures to manipulate state flags of socket and do appropriate wakeups.
3272 * Normal sequence from the active (originating) side is that
3273 * soisconnecting() is called during processing of connect() call, resulting
3274 * in an eventual call to soisconnected() if/when the connection is
3275 * established. When the connection is torn down soisdisconnecting() is
3276 * called during processing of disconnect() call, and soisdisconnected() is
3277 * called when the connection to the peer is totally severed. The semantics
3278 * of these routines are such that connectionless protocols can call
3279 * soisconnected() and soisdisconnected() only, bypassing the in-progress
3280 * calls when setting up a ``connection'' takes no time.
3282 * From the passive side, a socket is created with two queues of sockets:
3283 * so_incomp for connections in progress and so_comp for connections already
3284 * made and awaiting user acceptance. As a protocol is preparing incoming
3285 * connections, it creates a socket structure queued on so_incomp by calling
3286 * sonewconn(). When the connection is established, soisconnected() is
3287 * called, and transfers the socket structure to so_comp, making it available
3290 * If a socket is closed with sockets on either so_incomp or so_comp, these
3291 * sockets are dropped.
3293 * If higher-level protocols are implemented in the kernel, the wakeups done
3294 * here will sometimes cause software-interrupt process scheduling.
3297 soisconnecting(struct socket *so)
3301 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3302 so->so_state |= SS_ISCONNECTING;
3307 soisconnected(struct socket *so)
3309 struct socket *head;
3315 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3316 so->so_state |= SS_ISCONNECTED;
3318 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
3319 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3321 TAILQ_REMOVE(&head->so_incomp, so, so_list);
3323 so->so_qstate &= ~SQ_INCOMP;
3324 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
3326 so->so_qstate |= SQ_COMP;
3329 wakeup_one(&head->so_timeo);
3332 soupcall_set(so, SO_RCV,
3333 head->so_accf->so_accept_filter->accf_callback,
3334 head->so_accf->so_accept_filter_arg);
3335 so->so_options &= ~SO_ACCEPTFILTER;
3336 ret = head->so_accf->so_accept_filter->accf_callback(so,
3337 head->so_accf->so_accept_filter_arg, M_DONTWAIT);
3338 if (ret == SU_ISCONNECTED)
3339 soupcall_clear(so, SO_RCV);
3341 if (ret == SU_ISCONNECTED)
3348 wakeup(&so->so_timeo);
3354 soisdisconnecting(struct socket *so)
3358 * Note: This code assumes that SOCK_LOCK(so) and
3359 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3361 SOCKBUF_LOCK(&so->so_rcv);
3362 so->so_state &= ~SS_ISCONNECTING;
3363 so->so_state |= SS_ISDISCONNECTING;
3364 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3365 sorwakeup_locked(so);
3366 SOCKBUF_LOCK(&so->so_snd);
3367 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3368 sowwakeup_locked(so);
3369 wakeup(&so->so_timeo);
3373 soisdisconnected(struct socket *so)
3377 * Note: This code assumes that SOCK_LOCK(so) and
3378 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3380 SOCKBUF_LOCK(&so->so_rcv);
3381 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3382 so->so_state |= SS_ISDISCONNECTED;
3383 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3384 sorwakeup_locked(so);
3385 SOCKBUF_LOCK(&so->so_snd);
3386 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3387 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
3388 sowwakeup_locked(so);
3389 wakeup(&so->so_timeo);
3393 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3396 sodupsockaddr(const struct sockaddr *sa, int mflags)
3398 struct sockaddr *sa2;
3400 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3402 bcopy(sa, sa2, sa->sa_len);
3407 * Register per-socket buffer upcalls.
3410 soupcall_set(struct socket *so, int which,
3411 int (*func)(struct socket *, void *, int), void *arg)
3423 panic("soupcall_set: bad which");
3425 SOCKBUF_LOCK_ASSERT(sb);
3427 /* XXX: accf_http actually wants to do this on purpose. */
3428 KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall"));
3430 sb->sb_upcall = func;
3431 sb->sb_upcallarg = arg;
3432 sb->sb_flags |= SB_UPCALL;
3436 soupcall_clear(struct socket *so, int which)
3448 panic("soupcall_clear: bad which");
3450 SOCKBUF_LOCK_ASSERT(sb);
3451 KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear"));
3452 sb->sb_upcall = NULL;
3453 sb->sb_upcallarg = NULL;
3454 sb->sb_flags &= ~SB_UPCALL;
3458 * Create an external-format (``xsocket'') structure using the information in
3459 * the kernel-format socket structure pointed to by so. This is done to
3460 * reduce the spew of irrelevant information over this interface, to isolate
3461 * user code from changes in the kernel structure, and potentially to provide
3462 * information-hiding if we decide that some of this information should be
3463 * hidden from users.
3466 sotoxsocket(struct socket *so, struct xsocket *xso)
3469 xso->xso_len = sizeof *xso;
3471 xso->so_type = so->so_type;
3472 xso->so_options = so->so_options;
3473 xso->so_linger = so->so_linger;
3474 xso->so_state = so->so_state;
3475 xso->so_pcb = so->so_pcb;
3476 xso->xso_protocol = so->so_proto->pr_protocol;
3477 xso->xso_family = so->so_proto->pr_domain->dom_family;
3478 xso->so_qlen = so->so_qlen;
3479 xso->so_incqlen = so->so_incqlen;
3480 xso->so_qlimit = so->so_qlimit;
3481 xso->so_timeo = so->so_timeo;
3482 xso->so_error = so->so_error;
3483 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
3484 xso->so_oobmark = so->so_oobmark;
3485 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
3486 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
3487 xso->so_uid = so->so_cred->cr_uid;
3492 * Socket accessor functions to provide external consumers with
3493 * a safe interface to socket state
3498 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg)
3501 TAILQ_FOREACH(so, &so->so_comp, so_list)
3506 so_sockbuf_rcv(struct socket *so)
3509 return (&so->so_rcv);
3513 so_sockbuf_snd(struct socket *so)
3516 return (&so->so_snd);
3520 so_state_get(const struct socket *so)
3523 return (so->so_state);
3527 so_state_set(struct socket *so, int val)
3534 so_options_get(const struct socket *so)
3537 return (so->so_options);
3541 so_options_set(struct socket *so, int val)
3544 so->so_options = val;
3548 so_error_get(const struct socket *so)
3551 return (so->so_error);
3555 so_error_set(struct socket *so, int val)
3562 so_linger_get(const struct socket *so)
3565 return (so->so_linger);
3569 so_linger_set(struct socket *so, int val)
3572 so->so_linger = val;
3576 so_protosw_get(const struct socket *so)
3579 return (so->so_proto);
3583 so_protosw_set(struct socket *so, struct protosw *val)
3590 so_sorwakeup(struct socket *so)
3597 so_sowwakeup(struct socket *so)
3604 so_sorwakeup_locked(struct socket *so)
3607 sorwakeup_locked(so);
3611 so_sowwakeup_locked(struct socket *so)
3614 sowwakeup_locked(so);
3618 so_lock(struct socket *so)
3624 so_unlock(struct socket *so)