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_ROUTE))
387 so->so_fibnum = td->td_proc->p_fibnum;
392 mac_socket_create(cred, so);
394 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
395 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
398 * Auto-sizing of socket buffers is managed by the protocols and
399 * the appropriate flags must be set in the pru_attach function.
401 CURVNET_SET(so->so_vnet);
402 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
405 KASSERT(so->so_count == 1, ("socreate: so_count %d",
416 static int regression_sonewconn_earlytest = 1;
417 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
418 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
422 * When an attempt at a new connection is noted on a socket which accepts
423 * connections, sonewconn is called. If the connection is possible (subject
424 * to space constraints, etc.) then we allocate a new structure, propoerly
425 * linked into the data structure of the original socket, and return this.
426 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
428 * Note: the ref count on the socket is 0 on return.
431 sonewconn(struct socket *head, int connstatus)
437 over = (head->so_qlen > 3 * head->so_qlimit / 2);
440 if (regression_sonewconn_earlytest && over)
445 VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
446 __func__, __LINE__, head));
447 so = soalloc(head->so_vnet);
450 if ((head->so_options & SO_ACCEPTFILTER) != 0)
453 so->so_type = head->so_type;
454 so->so_options = head->so_options &~ SO_ACCEPTCONN;
455 so->so_linger = head->so_linger;
456 so->so_state = head->so_state | SS_NOFDREF;
457 so->so_fibnum = head->so_fibnum;
458 so->so_proto = head->so_proto;
459 so->so_cred = crhold(head->so_cred);
461 mac_socket_newconn(head, so);
463 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
464 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
465 VNET_SO_ASSERT(head);
466 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
467 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
471 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
472 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
473 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
474 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
475 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
476 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
477 so->so_state |= connstatus;
480 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
481 so->so_qstate |= SQ_COMP;
485 * Keep removing sockets from the head until there's room for
486 * us to insert on the tail. In pre-locking revisions, this
487 * was a simple if(), but as we could be racing with other
488 * threads and soabort() requires dropping locks, we must
489 * loop waiting for the condition to be true.
491 while (head->so_incqlen > head->so_qlimit) {
493 sp = TAILQ_FIRST(&head->so_incomp);
494 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
496 sp->so_qstate &= ~SQ_INCOMP;
502 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
503 so->so_qstate |= SQ_INCOMP;
509 wakeup_one(&head->so_timeo);
515 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
519 CURVNET_SET(so->so_vnet);
520 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
526 * solisten() transitions a socket from a non-listening state to a listening
527 * state, but can also be used to update the listen queue depth on an
528 * existing listen socket. The protocol will call back into the sockets
529 * layer using solisten_proto_check() and solisten_proto() to check and set
530 * socket-layer listen state. Call backs are used so that the protocol can
531 * acquire both protocol and socket layer locks in whatever order is required
534 * Protocol implementors are advised to hold the socket lock across the
535 * socket-layer test and set to avoid races at the socket layer.
538 solisten(struct socket *so, int backlog, struct thread *td)
542 CURVNET_SET(so->so_vnet);
543 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td);
549 solisten_proto_check(struct socket *so)
552 SOCK_LOCK_ASSERT(so);
554 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
561 solisten_proto(struct socket *so, int backlog)
564 SOCK_LOCK_ASSERT(so);
566 if (backlog < 0 || backlog > somaxconn)
568 so->so_qlimit = backlog;
569 so->so_options |= SO_ACCEPTCONN;
573 * Attempt to free a socket. This should really be sotryfree().
575 * sofree() will succeed if:
577 * - There are no outstanding file descriptor references or related consumers
580 * - The socket has been closed by user space, if ever open (SS_NOFDREF).
582 * - The protocol does not have an outstanding strong reference on the socket
585 * - The socket is not in a completed connection queue, so a process has been
586 * notified that it is present. If it is removed, the user process may
587 * block in accept() despite select() saying the socket was ready.
589 * Otherwise, it will quietly abort so that a future call to sofree(), when
590 * conditions are right, can succeed.
593 sofree(struct socket *so)
595 struct protosw *pr = so->so_proto;
598 ACCEPT_LOCK_ASSERT();
599 SOCK_LOCK_ASSERT(so);
601 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
602 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
610 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
611 (so->so_qstate & SQ_INCOMP) != 0,
612 ("sofree: so_head != NULL, but neither SQ_COMP nor "
614 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
615 (so->so_qstate & SQ_INCOMP) == 0,
616 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
617 TAILQ_REMOVE(&head->so_incomp, so, so_list);
619 so->so_qstate &= ~SQ_INCOMP;
622 KASSERT((so->so_qstate & SQ_COMP) == 0 &&
623 (so->so_qstate & SQ_INCOMP) == 0,
624 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
625 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
626 if (so->so_options & SO_ACCEPTCONN) {
627 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
628 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated"));
634 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
635 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
636 if (pr->pr_usrreqs->pru_detach != NULL)
637 (*pr->pr_usrreqs->pru_detach)(so);
640 * From this point on, we assume that no other references to this
641 * socket exist anywhere else in the stack. Therefore, no locks need
642 * to be acquired or held.
644 * We used to do a lot of socket buffer and socket locking here, as
645 * well as invoke sorflush() and perform wakeups. The direct call to
646 * dom_dispose() and sbrelease_internal() are an inlining of what was
647 * necessary from sorflush().
649 * Notice that the socket buffer and kqueue state are torn down
650 * before calling pru_detach. This means that protocols shold not
651 * assume they can perform socket wakeups, etc, in their detach code.
653 sbdestroy(&so->so_snd, so);
654 sbdestroy(&so->so_rcv, so);
655 seldrain(&so->so_snd.sb_sel);
656 seldrain(&so->so_rcv.sb_sel);
657 knlist_destroy(&so->so_rcv.sb_sel.si_note);
658 knlist_destroy(&so->so_snd.sb_sel.si_note);
663 * Close a socket on last file table reference removal. Initiate disconnect
664 * if connected. Free socket when disconnect complete.
666 * This function will sorele() the socket. Note that soclose() may be called
667 * prior to the ref count reaching zero. The actual socket structure will
668 * not be freed until the ref count reaches zero.
671 soclose(struct socket *so)
675 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
677 CURVNET_SET(so->so_vnet);
678 funsetown(&so->so_sigio);
679 if (so->so_state & SS_ISCONNECTED) {
680 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
681 error = sodisconnect(so);
683 if (error == ENOTCONN)
688 if (so->so_options & SO_LINGER) {
689 if ((so->so_state & SS_ISDISCONNECTING) &&
690 (so->so_state & SS_NBIO))
692 while (so->so_state & SS_ISCONNECTED) {
693 error = tsleep(&so->so_timeo,
694 PSOCK | PCATCH, "soclos", so->so_linger * hz);
702 if (so->so_proto->pr_usrreqs->pru_close != NULL)
703 (*so->so_proto->pr_usrreqs->pru_close)(so);
704 if (so->so_options & SO_ACCEPTCONN) {
707 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
708 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
710 sp->so_qstate &= ~SQ_INCOMP;
716 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
717 TAILQ_REMOVE(&so->so_comp, sp, so_list);
719 sp->so_qstate &= ~SQ_COMP;
729 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
730 so->so_state |= SS_NOFDREF;
737 * soabort() is used to abruptly tear down a connection, such as when a
738 * resource limit is reached (listen queue depth exceeded), or if a listen
739 * socket is closed while there are sockets waiting to be accepted.
741 * This interface is tricky, because it is called on an unreferenced socket,
742 * and must be called only by a thread that has actually removed the socket
743 * from the listen queue it was on, or races with other threads are risked.
745 * This interface will call into the protocol code, so must not be called
746 * with any socket locks held. Protocols do call it while holding their own
747 * recursible protocol mutexes, but this is something that should be subject
748 * to review in the future.
751 soabort(struct socket *so)
755 * In as much as is possible, assert that no references to this
756 * socket are held. This is not quite the same as asserting that the
757 * current thread is responsible for arranging for no references, but
758 * is as close as we can get for now.
760 KASSERT(so->so_count == 0, ("soabort: so_count"));
761 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
762 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
763 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
764 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
767 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
768 (*so->so_proto->pr_usrreqs->pru_abort)(so);
775 soaccept(struct socket *so, struct sockaddr **nam)
780 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
781 so->so_state &= ~SS_NOFDREF;
784 CURVNET_SET(so->so_vnet);
785 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
791 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
795 if (so->so_options & SO_ACCEPTCONN)
798 CURVNET_SET(so->so_vnet);
800 * If protocol is connection-based, can only connect once.
801 * Otherwise, if connected, try to disconnect first. This allows
802 * user to disconnect by connecting to, e.g., a null address.
804 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
805 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
806 (error = sodisconnect(so)))) {
810 * Prevent accumulated error from previous connection from
814 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
822 soconnect2(struct socket *so1, struct socket *so2)
826 CURVNET_SET(so1->so_vnet);
827 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
833 sodisconnect(struct socket *so)
837 if ((so->so_state & SS_ISCONNECTED) == 0)
839 if (so->so_state & SS_ISDISCONNECTING)
842 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
846 #ifdef ZERO_COPY_SOCKETS
847 struct so_zerocopy_stats{
852 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
853 #include <netinet/in.h>
854 #include <net/route.h>
855 #include <netinet/in_pcb.h>
857 #include <vm/vm_page.h>
858 #include <vm/vm_object.h>
861 * sosend_copyin() is only used if zero copy sockets are enabled. Otherwise
862 * sosend_dgram() and sosend_generic() use m_uiotombuf().
864 * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or
865 * all of the data referenced by the uio. If desired, it uses zero-copy.
866 * *space will be updated to reflect data copied in.
868 * NB: If atomic I/O is requested, the caller must already have checked that
869 * space can hold resid bytes.
871 * NB: In the event of an error, the caller may need to free the partial
872 * chain pointed to by *mpp. The contents of both *uio and *space may be
873 * modified even in the case of an error.
876 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space,
879 struct mbuf *m, **mp, *top;
882 #ifdef ZERO_COPY_SOCKETS
889 resid = uio->uio_resid;
892 #ifdef ZERO_COPY_SOCKETS
894 #endif /* ZERO_COPY_SOCKETS */
895 if (resid >= MINCLSIZE) {
896 #ifdef ZERO_COPY_SOCKETS
898 m = m_gethdr(M_WAITOK, MT_DATA);
900 m->m_pkthdr.rcvif = NULL;
902 m = m_get(M_WAITOK, MT_DATA);
903 if (so_zero_copy_send &&
906 uio->uio_iov->iov_len>=PAGE_SIZE) {
907 so_zerocp_stats.size_ok++;
908 so_zerocp_stats.align_ok++;
909 cow_send = socow_setup(m, uio);
913 m_clget(m, M_WAITOK);
914 len = min(min(MCLBYTES, resid), *space);
916 #else /* ZERO_COPY_SOCKETS */
918 m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
920 m->m_pkthdr.rcvif = NULL;
922 m = m_getcl(M_WAIT, MT_DATA, 0);
923 len = min(min(MCLBYTES, resid), *space);
924 #endif /* ZERO_COPY_SOCKETS */
927 m = m_gethdr(M_WAIT, MT_DATA);
929 m->m_pkthdr.rcvif = NULL;
931 len = min(min(MHLEN, resid), *space);
933 * For datagram protocols, leave room
934 * for protocol headers in first mbuf.
936 if (atomic && m && len < MHLEN)
939 m = m_get(M_WAIT, MT_DATA);
940 len = min(min(MLEN, resid), *space);
949 #ifdef ZERO_COPY_SOCKETS
953 #endif /* ZERO_COPY_SOCKETS */
954 error = uiomove(mtod(m, void *), (int)len, uio);
955 resid = uio->uio_resid;
958 top->m_pkthdr.len += len;
964 top->m_flags |= M_EOR;
967 } while (*space > 0 && atomic);
972 #endif /*ZERO_COPY_SOCKETS*/
974 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
977 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
978 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
981 int clen = 0, error, dontroute;
982 #ifdef ZERO_COPY_SOCKETS
983 int atomic = sosendallatonce(so) || top;
986 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
987 KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
988 ("sodgram_send: !PR_ATOMIC"));
991 resid = uio->uio_resid;
993 resid = top->m_pkthdr.len;
995 * In theory resid should be unsigned. However, space must be
996 * signed, as it might be less than 0 if we over-committed, and we
997 * must use a signed comparison of space and resid. On the other
998 * hand, a negative resid causes us to loop sending 0-length
999 * segments to the protocol.
1007 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
1009 td->td_ru.ru_msgsnd++;
1010 if (control != NULL)
1011 clen = control->m_len;
1013 SOCKBUF_LOCK(&so->so_snd);
1014 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1015 SOCKBUF_UNLOCK(&so->so_snd);
1020 error = so->so_error;
1022 SOCKBUF_UNLOCK(&so->so_snd);
1025 if ((so->so_state & SS_ISCONNECTED) == 0) {
1027 * `sendto' and `sendmsg' is allowed on a connection-based
1028 * socket if it supports implied connect. Return ENOTCONN if
1029 * not connected and no address is supplied.
1031 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1032 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1033 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1034 !(resid == 0 && clen != 0)) {
1035 SOCKBUF_UNLOCK(&so->so_snd);
1039 } else if (addr == NULL) {
1040 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1043 error = EDESTADDRREQ;
1044 SOCKBUF_UNLOCK(&so->so_snd);
1050 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
1051 * problem and need fixing.
1053 space = sbspace(&so->so_snd);
1054 if (flags & MSG_OOB)
1057 SOCKBUF_UNLOCK(&so->so_snd);
1058 if (resid > space) {
1064 if (flags & MSG_EOR)
1065 top->m_flags |= M_EOR;
1067 #ifdef ZERO_COPY_SOCKETS
1068 error = sosend_copyin(uio, &top, atomic, &space, flags);
1073 * Copy the data from userland into a mbuf chain.
1074 * If no data is to be copied in, a single empty mbuf
1077 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1078 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1080 error = EFAULT; /* only possible error */
1083 space -= resid - uio->uio_resid;
1085 resid = uio->uio_resid;
1087 KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1089 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1094 so->so_options |= SO_DONTROUTE;
1098 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1099 * of date. We could have recieved a reset packet in an interrupt or
1100 * maybe we slept while doing page faults in uiomove() etc. We could
1101 * probably recheck again inside the locking protection here, but
1102 * there are probably other places that this also happens. We must
1106 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1107 (flags & MSG_OOB) ? PRUS_OOB :
1109 * If the user set MSG_EOF, the protocol understands this flag and
1110 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1112 ((flags & MSG_EOF) &&
1113 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1116 /* If there is more to send set PRUS_MORETOCOME */
1117 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1118 top, addr, control, td);
1121 so->so_options &= ~SO_DONTROUTE;
1130 if (control != NULL)
1136 * Send on a socket. If send must go all at once and message is larger than
1137 * send buffering, then hard error. Lock against other senders. If must go
1138 * all at once and not enough room now, then inform user that this would
1139 * block and do nothing. Otherwise, if nonblocking, send as much as
1140 * possible. The data to be sent is described by "uio" if nonzero, otherwise
1141 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1142 * in mbuf chain must be small enough to send all at once.
1144 * Returns nonzero on error, timeout or signal; callers must check for short
1145 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1149 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1150 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1153 int clen = 0, error, dontroute;
1154 int atomic = sosendallatonce(so) || top;
1157 resid = uio->uio_resid;
1159 resid = top->m_pkthdr.len;
1161 * In theory resid should be unsigned. However, space must be
1162 * signed, as it might be less than 0 if we over-committed, and we
1163 * must use a signed comparison of space and resid. On the other
1164 * hand, a negative resid causes us to loop sending 0-length
1165 * segments to the protocol.
1167 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1168 * type sockets since that's an error.
1170 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1176 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1177 (so->so_proto->pr_flags & PR_ATOMIC);
1179 td->td_ru.ru_msgsnd++;
1180 if (control != NULL)
1181 clen = control->m_len;
1183 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1189 SOCKBUF_LOCK(&so->so_snd);
1190 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1191 SOCKBUF_UNLOCK(&so->so_snd);
1196 error = so->so_error;
1198 SOCKBUF_UNLOCK(&so->so_snd);
1201 if ((so->so_state & SS_ISCONNECTED) == 0) {
1203 * `sendto' and `sendmsg' is allowed on a connection-
1204 * based socket if it supports implied connect.
1205 * Return ENOTCONN if not connected and no address is
1208 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1209 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1210 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1211 !(resid == 0 && clen != 0)) {
1212 SOCKBUF_UNLOCK(&so->so_snd);
1216 } else if (addr == NULL) {
1217 SOCKBUF_UNLOCK(&so->so_snd);
1218 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1221 error = EDESTADDRREQ;
1225 space = sbspace(&so->so_snd);
1226 if (flags & MSG_OOB)
1228 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1229 clen > so->so_snd.sb_hiwat) {
1230 SOCKBUF_UNLOCK(&so->so_snd);
1234 if (space < resid + clen &&
1235 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1236 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
1237 SOCKBUF_UNLOCK(&so->so_snd);
1238 error = EWOULDBLOCK;
1241 error = sbwait(&so->so_snd);
1242 SOCKBUF_UNLOCK(&so->so_snd);
1247 SOCKBUF_UNLOCK(&so->so_snd);
1252 if (flags & MSG_EOR)
1253 top->m_flags |= M_EOR;
1255 #ifdef ZERO_COPY_SOCKETS
1256 error = sosend_copyin(uio, &top, atomic,
1262 * Copy the data from userland into a mbuf
1263 * chain. If no data is to be copied in,
1264 * a single empty mbuf is returned.
1266 top = m_uiotombuf(uio, M_WAITOK, space,
1267 (atomic ? max_hdr : 0),
1268 (atomic ? M_PKTHDR : 0) |
1269 ((flags & MSG_EOR) ? M_EOR : 0));
1271 error = EFAULT; /* only possible error */
1274 space -= resid - uio->uio_resid;
1276 resid = uio->uio_resid;
1280 so->so_options |= SO_DONTROUTE;
1284 * XXX all the SBS_CANTSENDMORE checks previously
1285 * done could be out of date. We could have recieved
1286 * a reset packet in an interrupt or maybe we slept
1287 * while doing page faults in uiomove() etc. We
1288 * could probably recheck again inside the locking
1289 * protection here, but there are probably other
1290 * places that this also happens. We must rethink
1294 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1295 (flags & MSG_OOB) ? PRUS_OOB :
1297 * If the user set MSG_EOF, the protocol understands
1298 * this flag and nothing left to send then use
1299 * PRU_SEND_EOF instead of PRU_SEND.
1301 ((flags & MSG_EOF) &&
1302 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1305 /* If there is more to send set PRUS_MORETOCOME. */
1306 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1307 top, addr, control, td);
1310 so->so_options &= ~SO_DONTROUTE;
1318 } while (resid && space > 0);
1322 sbunlock(&so->so_snd);
1326 if (control != NULL)
1332 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1333 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1337 CURVNET_SET(so->so_vnet);
1338 error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1339 control, flags, td);
1345 * The part of soreceive() that implements reading non-inline out-of-band
1346 * data from a socket. For more complete comments, see soreceive(), from
1347 * which this code originated.
1349 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1350 * unable to return an mbuf chain to the caller.
1353 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1355 struct protosw *pr = so->so_proto;
1359 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1362 m = m_get(M_WAIT, MT_DATA);
1363 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1367 #ifdef ZERO_COPY_SOCKETS
1368 if (so_zero_copy_receive) {
1371 if ((m->m_flags & M_EXT)
1372 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1377 error = uiomoveco(mtod(m, void *),
1378 min(uio->uio_resid, m->m_len),
1381 #endif /* ZERO_COPY_SOCKETS */
1382 error = uiomove(mtod(m, void *),
1383 (int) min(uio->uio_resid, m->m_len), uio);
1385 } while (uio->uio_resid && error == 0 && m);
1393 * Following replacement or removal of the first mbuf on the first mbuf chain
1394 * of a socket buffer, push necessary state changes back into the socket
1395 * buffer so that other consumers see the values consistently. 'nextrecord'
1396 * is the callers locally stored value of the original value of
1397 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1398 * NOTE: 'nextrecord' may be NULL.
1400 static __inline void
1401 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1404 SOCKBUF_LOCK_ASSERT(sb);
1406 * First, update for the new value of nextrecord. If necessary, make
1407 * it the first record.
1409 if (sb->sb_mb != NULL)
1410 sb->sb_mb->m_nextpkt = nextrecord;
1412 sb->sb_mb = nextrecord;
1415 * Now update any dependent socket buffer fields to reflect the new
1416 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
1417 * addition of a second clause that takes care of the case where
1418 * sb_mb has been updated, but remains the last record.
1420 if (sb->sb_mb == NULL) {
1421 sb->sb_mbtail = NULL;
1422 sb->sb_lastrecord = NULL;
1423 } else if (sb->sb_mb->m_nextpkt == NULL)
1424 sb->sb_lastrecord = sb->sb_mb;
1429 * Implement receive operations on a socket. We depend on the way that
1430 * records are added to the sockbuf by sbappend. In particular, each record
1431 * (mbufs linked through m_next) must begin with an address if the protocol
1432 * so specifies, followed by an optional mbuf or mbufs containing ancillary
1433 * data, and then zero or more mbufs of data. In order to allow parallelism
1434 * between network receive and copying to user space, as well as avoid
1435 * sleeping with a mutex held, we release the socket buffer mutex during the
1436 * user space copy. Although the sockbuf is locked, new data may still be
1437 * appended, and thus we must maintain consistency of the sockbuf during that
1440 * The caller may receive the data as a single mbuf chain by supplying an
1441 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1442 * the count in uio_resid.
1445 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1446 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1448 struct mbuf *m, **mp;
1449 int flags, len, error, offset;
1450 struct protosw *pr = so->so_proto;
1451 struct mbuf *nextrecord;
1453 int orig_resid = uio->uio_resid;
1458 if (controlp != NULL)
1461 flags = *flagsp &~ MSG_EOR;
1464 if (flags & MSG_OOB)
1465 return (soreceive_rcvoob(so, uio, flags));
1468 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1469 && uio->uio_resid) {
1471 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1474 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1479 SOCKBUF_LOCK(&so->so_rcv);
1480 m = so->so_rcv.sb_mb;
1482 * If we have less data than requested, block awaiting more (subject
1483 * to any timeout) if:
1484 * 1. the current count is less than the low water mark, or
1485 * 2. MSG_WAITALL is set, and it is possible to do the entire
1486 * receive operation at once if we block (resid <= hiwat).
1487 * 3. MSG_DONTWAIT is not set
1488 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1489 * we have to do the receive in sections, and thus risk returning a
1490 * short count if a timeout or signal occurs after we start.
1492 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1493 so->so_rcv.sb_cc < uio->uio_resid) &&
1494 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1495 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1496 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1497 KASSERT(m != NULL || !so->so_rcv.sb_cc,
1498 ("receive: m == %p so->so_rcv.sb_cc == %u",
1499 m, so->so_rcv.sb_cc));
1503 error = so->so_error;
1504 if ((flags & MSG_PEEK) == 0)
1506 SOCKBUF_UNLOCK(&so->so_rcv);
1509 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1510 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1512 SOCKBUF_UNLOCK(&so->so_rcv);
1517 for (; m != NULL; m = m->m_next)
1518 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1519 m = so->so_rcv.sb_mb;
1522 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1523 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1524 SOCKBUF_UNLOCK(&so->so_rcv);
1528 if (uio->uio_resid == 0) {
1529 SOCKBUF_UNLOCK(&so->so_rcv);
1532 if ((so->so_state & SS_NBIO) ||
1533 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1534 SOCKBUF_UNLOCK(&so->so_rcv);
1535 error = EWOULDBLOCK;
1538 SBLASTRECORDCHK(&so->so_rcv);
1539 SBLASTMBUFCHK(&so->so_rcv);
1540 error = sbwait(&so->so_rcv);
1541 SOCKBUF_UNLOCK(&so->so_rcv);
1548 * From this point onward, we maintain 'nextrecord' as a cache of the
1549 * pointer to the next record in the socket buffer. We must keep the
1550 * various socket buffer pointers and local stack versions of the
1551 * pointers in sync, pushing out modifications before dropping the
1552 * socket buffer mutex, and re-reading them when picking it up.
1554 * Otherwise, we will race with the network stack appending new data
1555 * or records onto the socket buffer by using inconsistent/stale
1556 * versions of the field, possibly resulting in socket buffer
1559 * By holding the high-level sblock(), we prevent simultaneous
1560 * readers from pulling off the front of the socket buffer.
1562 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1564 uio->uio_td->td_ru.ru_msgrcv++;
1565 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1566 SBLASTRECORDCHK(&so->so_rcv);
1567 SBLASTMBUFCHK(&so->so_rcv);
1568 nextrecord = m->m_nextpkt;
1569 if (pr->pr_flags & PR_ADDR) {
1570 KASSERT(m->m_type == MT_SONAME,
1571 ("m->m_type == %d", m->m_type));
1574 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1576 if (flags & MSG_PEEK) {
1579 sbfree(&so->so_rcv, m);
1580 so->so_rcv.sb_mb = m_free(m);
1581 m = so->so_rcv.sb_mb;
1582 sockbuf_pushsync(&so->so_rcv, nextrecord);
1587 * Process one or more MT_CONTROL mbufs present before any data mbufs
1588 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1589 * just copy the data; if !MSG_PEEK, we call into the protocol to
1590 * perform externalization (or freeing if controlp == NULL).
1592 if (m != NULL && m->m_type == MT_CONTROL) {
1593 struct mbuf *cm = NULL, *cmn;
1594 struct mbuf **cme = &cm;
1597 if (flags & MSG_PEEK) {
1598 if (controlp != NULL) {
1599 *controlp = m_copy(m, 0, m->m_len);
1600 controlp = &(*controlp)->m_next;
1604 sbfree(&so->so_rcv, m);
1605 so->so_rcv.sb_mb = m->m_next;
1608 cme = &(*cme)->m_next;
1609 m = so->so_rcv.sb_mb;
1611 } while (m != NULL && m->m_type == MT_CONTROL);
1612 if ((flags & MSG_PEEK) == 0)
1613 sockbuf_pushsync(&so->so_rcv, nextrecord);
1614 while (cm != NULL) {
1617 if (pr->pr_domain->dom_externalize != NULL) {
1618 SOCKBUF_UNLOCK(&so->so_rcv);
1620 error = (*pr->pr_domain->dom_externalize)
1622 SOCKBUF_LOCK(&so->so_rcv);
1623 } else if (controlp != NULL)
1627 if (controlp != NULL) {
1629 while (*controlp != NULL)
1630 controlp = &(*controlp)->m_next;
1635 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1637 nextrecord = so->so_rcv.sb_mb;
1641 if ((flags & MSG_PEEK) == 0) {
1642 KASSERT(m->m_nextpkt == nextrecord,
1643 ("soreceive: post-control, nextrecord !sync"));
1644 if (nextrecord == NULL) {
1645 KASSERT(so->so_rcv.sb_mb == m,
1646 ("soreceive: post-control, sb_mb!=m"));
1647 KASSERT(so->so_rcv.sb_lastrecord == m,
1648 ("soreceive: post-control, lastrecord!=m"));
1652 if (type == MT_OOBDATA)
1655 if ((flags & MSG_PEEK) == 0) {
1656 KASSERT(so->so_rcv.sb_mb == nextrecord,
1657 ("soreceive: sb_mb != nextrecord"));
1658 if (so->so_rcv.sb_mb == NULL) {
1659 KASSERT(so->so_rcv.sb_lastrecord == NULL,
1660 ("soreceive: sb_lastercord != NULL"));
1664 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1665 SBLASTRECORDCHK(&so->so_rcv);
1666 SBLASTMBUFCHK(&so->so_rcv);
1669 * Now continue to read any data mbufs off of the head of the socket
1670 * buffer until the read request is satisfied. Note that 'type' is
1671 * used to store the type of any mbuf reads that have happened so far
1672 * such that soreceive() can stop reading if the type changes, which
1673 * causes soreceive() to return only one of regular data and inline
1674 * out-of-band data in a single socket receive operation.
1678 while (m != NULL && uio->uio_resid > 0 && error == 0) {
1680 * If the type of mbuf has changed since the last mbuf
1681 * examined ('type'), end the receive operation.
1683 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1684 if (m->m_type == MT_OOBDATA) {
1685 if (type != MT_OOBDATA)
1687 } else if (type == MT_OOBDATA)
1690 KASSERT(m->m_type == MT_DATA,
1691 ("m->m_type == %d", m->m_type));
1692 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1693 len = uio->uio_resid;
1694 if (so->so_oobmark && len > so->so_oobmark - offset)
1695 len = so->so_oobmark - offset;
1696 if (len > m->m_len - moff)
1697 len = m->m_len - moff;
1699 * If mp is set, just pass back the mbufs. Otherwise copy
1700 * them out via the uio, then free. Sockbuf must be
1701 * consistent here (points to current mbuf, it points to next
1702 * record) when we drop priority; we must note any additions
1703 * to the sockbuf when we block interrupts again.
1706 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1707 SBLASTRECORDCHK(&so->so_rcv);
1708 SBLASTMBUFCHK(&so->so_rcv);
1709 SOCKBUF_UNLOCK(&so->so_rcv);
1710 #ifdef ZERO_COPY_SOCKETS
1711 if (so_zero_copy_receive) {
1714 if ((m->m_flags & M_EXT)
1715 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1720 error = uiomoveco(mtod(m, char *) + moff,
1724 #endif /* ZERO_COPY_SOCKETS */
1725 error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1726 SOCKBUF_LOCK(&so->so_rcv);
1729 * The MT_SONAME mbuf has already been removed
1730 * from the record, so it is necessary to
1731 * remove the data mbufs, if any, to preserve
1732 * the invariant in the case of PR_ADDR that
1733 * requires MT_SONAME mbufs at the head of
1736 if (m && pr->pr_flags & PR_ATOMIC &&
1737 ((flags & MSG_PEEK) == 0))
1738 (void)sbdroprecord_locked(&so->so_rcv);
1739 SOCKBUF_UNLOCK(&so->so_rcv);
1743 uio->uio_resid -= len;
1744 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1745 if (len == m->m_len - moff) {
1746 if (m->m_flags & M_EOR)
1748 if (flags & MSG_PEEK) {
1752 nextrecord = m->m_nextpkt;
1753 sbfree(&so->so_rcv, m);
1757 so->so_rcv.sb_mb = m = m->m_next;
1760 so->so_rcv.sb_mb = m_free(m);
1761 m = so->so_rcv.sb_mb;
1763 sockbuf_pushsync(&so->so_rcv, nextrecord);
1764 SBLASTRECORDCHK(&so->so_rcv);
1765 SBLASTMBUFCHK(&so->so_rcv);
1768 if (flags & MSG_PEEK)
1774 if (flags & MSG_DONTWAIT)
1775 copy_flag = M_DONTWAIT;
1778 if (copy_flag == M_WAIT)
1779 SOCKBUF_UNLOCK(&so->so_rcv);
1780 *mp = m_copym(m, 0, len, copy_flag);
1781 if (copy_flag == M_WAIT)
1782 SOCKBUF_LOCK(&so->so_rcv);
1785 * m_copym() couldn't
1786 * allocate an mbuf. Adjust
1787 * uio_resid back (it was
1788 * adjusted down by len
1789 * bytes, which we didn't end
1790 * up "copying" over).
1792 uio->uio_resid += len;
1798 so->so_rcv.sb_cc -= len;
1801 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1802 if (so->so_oobmark) {
1803 if ((flags & MSG_PEEK) == 0) {
1804 so->so_oobmark -= len;
1805 if (so->so_oobmark == 0) {
1806 so->so_rcv.sb_state |= SBS_RCVATMARK;
1811 if (offset == so->so_oobmark)
1815 if (flags & MSG_EOR)
1818 * If the MSG_WAITALL flag is set (for non-atomic socket), we
1819 * must not quit until "uio->uio_resid == 0" or an error
1820 * termination. If a signal/timeout occurs, return with a
1821 * short count but without error. Keep sockbuf locked
1822 * against other readers.
1824 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1825 !sosendallatonce(so) && nextrecord == NULL) {
1826 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1827 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
1830 * Notify the protocol that some data has been
1831 * drained before blocking.
1833 if (pr->pr_flags & PR_WANTRCVD) {
1834 SOCKBUF_UNLOCK(&so->so_rcv);
1836 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1837 SOCKBUF_LOCK(&so->so_rcv);
1839 SBLASTRECORDCHK(&so->so_rcv);
1840 SBLASTMBUFCHK(&so->so_rcv);
1842 * We could receive some data while was notifying
1843 * the protocol. Skip blocking in this case.
1845 if (so->so_rcv.sb_mb == NULL) {
1846 error = sbwait(&so->so_rcv);
1848 SOCKBUF_UNLOCK(&so->so_rcv);
1852 m = so->so_rcv.sb_mb;
1854 nextrecord = m->m_nextpkt;
1858 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1859 if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1861 if ((flags & MSG_PEEK) == 0)
1862 (void) sbdroprecord_locked(&so->so_rcv);
1864 if ((flags & MSG_PEEK) == 0) {
1867 * First part is an inline SB_EMPTY_FIXUP(). Second
1868 * part makes sure sb_lastrecord is up-to-date if
1869 * there is still data in the socket buffer.
1871 so->so_rcv.sb_mb = nextrecord;
1872 if (so->so_rcv.sb_mb == NULL) {
1873 so->so_rcv.sb_mbtail = NULL;
1874 so->so_rcv.sb_lastrecord = NULL;
1875 } else if (nextrecord->m_nextpkt == NULL)
1876 so->so_rcv.sb_lastrecord = nextrecord;
1878 SBLASTRECORDCHK(&so->so_rcv);
1879 SBLASTMBUFCHK(&so->so_rcv);
1881 * If soreceive() is being done from the socket callback,
1882 * then don't need to generate ACK to peer to update window,
1883 * since ACK will be generated on return to TCP.
1885 if (!(flags & MSG_SOCALLBCK) &&
1886 (pr->pr_flags & PR_WANTRCVD)) {
1887 SOCKBUF_UNLOCK(&so->so_rcv);
1889 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1890 SOCKBUF_LOCK(&so->so_rcv);
1893 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1894 if (orig_resid == uio->uio_resid && orig_resid &&
1895 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1896 SOCKBUF_UNLOCK(&so->so_rcv);
1899 SOCKBUF_UNLOCK(&so->so_rcv);
1904 sbunlock(&so->so_rcv);
1909 * Optimized version of soreceive() for stream (TCP) sockets.
1912 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
1913 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1915 int len = 0, error = 0, flags, oresid;
1917 struct mbuf *m, *n = NULL;
1919 /* We only do stream sockets. */
1920 if (so->so_type != SOCK_STREAM)
1924 if (controlp != NULL)
1927 flags = *flagsp &~ MSG_EOR;
1930 if (flags & MSG_OOB)
1931 return (soreceive_rcvoob(so, uio, flags));
1937 /* Prevent other readers from entering the socket. */
1938 error = sblock(sb, SBLOCKWAIT(flags));
1943 /* Easy one, no space to copyout anything. */
1944 if (uio->uio_resid == 0) {
1948 oresid = uio->uio_resid;
1950 /* We will never ever get anything unless we are connected. */
1951 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1952 /* When disconnecting there may be still some data left. */
1955 if (!(so->so_state & SS_ISDISCONNECTED))
1960 /* Socket buffer is empty and we shall not block. */
1961 if (sb->sb_cc == 0 &&
1962 ((sb->sb_flags & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
1968 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1970 /* Abort if socket has reported problems. */
1974 if (oresid > uio->uio_resid)
1976 error = so->so_error;
1977 if (!(flags & MSG_PEEK))
1982 /* Door is closed. Deliver what is left, if any. */
1983 if (sb->sb_state & SBS_CANTRCVMORE) {
1990 /* Socket buffer got some data that we shall deliver now. */
1991 if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) &&
1992 ((sb->sb_flags & SS_NBIO) ||
1993 (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
1994 sb->sb_cc >= sb->sb_lowat ||
1995 sb->sb_cc >= uio->uio_resid ||
1996 sb->sb_cc >= sb->sb_hiwat) ) {
2000 /* On MSG_WAITALL we must wait until all data or error arrives. */
2001 if ((flags & MSG_WAITALL) &&
2002 (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_lowat))
2006 * Wait and block until (more) data comes in.
2007 * NB: Drops the sockbuf lock during wait.
2015 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2016 KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__));
2017 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
2021 uio->uio_td->td_ru.ru_msgrcv++;
2023 /* Fill uio until full or current end of socket buffer is reached. */
2024 len = min(uio->uio_resid, sb->sb_cc);
2026 /* Dequeue as many mbufs as possible. */
2027 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
2028 for (*mp0 = m = sb->sb_mb;
2029 m != NULL && m->m_len <= len;
2032 uio->uio_resid -= m->m_len;
2037 if (sb->sb_mb == NULL)
2041 /* Copy the remainder. */
2043 KASSERT(sb->sb_mb != NULL,
2044 ("%s: len > 0 && sb->sb_mb empty", __func__));
2046 m = m_copym(sb->sb_mb, 0, len, M_DONTWAIT);
2048 len = 0; /* Don't flush data from sockbuf. */
2050 uio->uio_resid -= m->m_len;
2061 /* NB: Must unlock socket buffer as uiomove may sleep. */
2063 error = m_mbuftouio(uio, sb->sb_mb, len);
2068 SBLASTRECORDCHK(sb);
2072 * Remove the delivered data from the socket buffer unless we
2073 * were only peeking.
2075 if (!(flags & MSG_PEEK)) {
2077 sbdrop_locked(sb, len);
2079 /* Notify protocol that we drained some data. */
2080 if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
2081 (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
2082 !(flags & MSG_SOCALLBCK))) {
2085 (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
2091 * For MSG_WAITALL we may have to loop again and wait for
2092 * more data to come in.
2094 if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
2097 SOCKBUF_LOCK_ASSERT(sb);
2098 SBLASTRECORDCHK(sb);
2106 * Optimized version of soreceive() for simple datagram cases from userspace.
2107 * Unlike in the stream case, we're able to drop a datagram if copyout()
2108 * fails, and because we handle datagrams atomically, we don't need to use a
2109 * sleep lock to prevent I/O interlacing.
2112 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
2113 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2115 struct mbuf *m, *m2;
2116 int flags, len, error;
2117 struct protosw *pr = so->so_proto;
2118 struct mbuf *nextrecord;
2122 if (controlp != NULL)
2125 flags = *flagsp &~ MSG_EOR;
2130 * For any complicated cases, fall back to the full
2131 * soreceive_generic().
2133 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
2134 return (soreceive_generic(so, psa, uio, mp0, controlp,
2138 * Enforce restrictions on use.
2140 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
2141 ("soreceive_dgram: wantrcvd"));
2142 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
2143 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
2144 ("soreceive_dgram: SBS_RCVATMARK"));
2145 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
2146 ("soreceive_dgram: P_CONNREQUIRED"));
2149 * Loop blocking while waiting for a datagram.
2151 SOCKBUF_LOCK(&so->so_rcv);
2152 while ((m = so->so_rcv.sb_mb) == NULL) {
2153 KASSERT(so->so_rcv.sb_cc == 0,
2154 ("soreceive_dgram: sb_mb NULL but sb_cc %u",
2157 error = so->so_error;
2159 SOCKBUF_UNLOCK(&so->so_rcv);
2162 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
2163 uio->uio_resid == 0) {
2164 SOCKBUF_UNLOCK(&so->so_rcv);
2167 if ((so->so_state & SS_NBIO) ||
2168 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2169 SOCKBUF_UNLOCK(&so->so_rcv);
2170 return (EWOULDBLOCK);
2172 SBLASTRECORDCHK(&so->so_rcv);
2173 SBLASTMBUFCHK(&so->so_rcv);
2174 error = sbwait(&so->so_rcv);
2176 SOCKBUF_UNLOCK(&so->so_rcv);
2180 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2183 uio->uio_td->td_ru.ru_msgrcv++;
2184 SBLASTRECORDCHK(&so->so_rcv);
2185 SBLASTMBUFCHK(&so->so_rcv);
2186 nextrecord = m->m_nextpkt;
2187 if (nextrecord == NULL) {
2188 KASSERT(so->so_rcv.sb_lastrecord == m,
2189 ("soreceive_dgram: lastrecord != m"));
2192 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
2193 ("soreceive_dgram: m_nextpkt != nextrecord"));
2196 * Pull 'm' and its chain off the front of the packet queue.
2198 so->so_rcv.sb_mb = NULL;
2199 sockbuf_pushsync(&so->so_rcv, nextrecord);
2202 * Walk 'm's chain and free that many bytes from the socket buffer.
2204 for (m2 = m; m2 != NULL; m2 = m2->m_next)
2205 sbfree(&so->so_rcv, m2);
2208 * Do a few last checks before we let go of the lock.
2210 SBLASTRECORDCHK(&so->so_rcv);
2211 SBLASTMBUFCHK(&so->so_rcv);
2212 SOCKBUF_UNLOCK(&so->so_rcv);
2214 if (pr->pr_flags & PR_ADDR) {
2215 KASSERT(m->m_type == MT_SONAME,
2216 ("m->m_type == %d", m->m_type));
2218 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
2223 /* XXXRW: Can this happen? */
2228 * Packet to copyout() is now in 'm' and it is disconnected from the
2231 * Process one or more MT_CONTROL mbufs present before any data mbufs
2232 * in the first mbuf chain on the socket buffer. We call into the
2233 * protocol to perform externalization (or freeing if controlp ==
2236 if (m->m_type == MT_CONTROL) {
2237 struct mbuf *cm = NULL, *cmn;
2238 struct mbuf **cme = &cm;
2244 cme = &(*cme)->m_next;
2246 } while (m != NULL && m->m_type == MT_CONTROL);
2247 while (cm != NULL) {
2250 if (pr->pr_domain->dom_externalize != NULL) {
2251 error = (*pr->pr_domain->dom_externalize)
2253 } else if (controlp != NULL)
2257 if (controlp != NULL) {
2258 while (*controlp != NULL)
2259 controlp = &(*controlp)->m_next;
2264 KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
2266 while (m != NULL && uio->uio_resid > 0) {
2267 len = uio->uio_resid;
2270 error = uiomove(mtod(m, char *), (int)len, uio);
2275 if (len == m->m_len)
2291 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2292 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2296 CURVNET_SET(so->so_vnet);
2297 error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
2304 soshutdown(struct socket *so, int how)
2306 struct protosw *pr = so->so_proto;
2309 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2312 CURVNET_SET(so->so_vnet);
2313 if (pr->pr_usrreqs->pru_flush != NULL) {
2314 (*pr->pr_usrreqs->pru_flush)(so, how);
2318 if (how != SHUT_RD) {
2319 error = (*pr->pr_usrreqs->pru_shutdown)(so);
2328 sorflush(struct socket *so)
2330 struct sockbuf *sb = &so->so_rcv;
2331 struct protosw *pr = so->so_proto;
2337 * In order to avoid calling dom_dispose with the socket buffer mutex
2338 * held, and in order to generally avoid holding the lock for a long
2339 * time, we make a copy of the socket buffer and clear the original
2340 * (except locks, state). The new socket buffer copy won't have
2341 * initialized locks so we can only call routines that won't use or
2342 * assert those locks.
2344 * Dislodge threads currently blocked in receive and wait to acquire
2345 * a lock against other simultaneous readers before clearing the
2346 * socket buffer. Don't let our acquire be interrupted by a signal
2347 * despite any existing socket disposition on interruptable waiting.
2350 (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2353 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2354 * and mutex data unchanged.
2357 bzero(&asb, offsetof(struct sockbuf, sb_startzero));
2358 bcopy(&sb->sb_startzero, &asb.sb_startzero,
2359 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2360 bzero(&sb->sb_startzero,
2361 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2366 * Dispose of special rights and flush the socket buffer. Don't call
2367 * any unsafe routines (that rely on locks being initialized) on asb.
2369 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2370 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
2371 sbrelease_internal(&asb, so);
2375 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2376 * additional variant to handle the case where the option value needs to be
2377 * some kind of integer, but not a specific size. In addition to their use
2378 * here, these functions are also called by the protocol-level pr_ctloutput()
2382 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2387 * If the user gives us more than we wanted, we ignore it, but if we
2388 * don't get the minimum length the caller wants, we return EINVAL.
2389 * On success, sopt->sopt_valsize is set to however much we actually
2392 if ((valsize = sopt->sopt_valsize) < minlen)
2395 sopt->sopt_valsize = valsize = len;
2397 if (sopt->sopt_td != NULL)
2398 return (copyin(sopt->sopt_val, buf, valsize));
2400 bcopy(sopt->sopt_val, buf, valsize);
2405 * Kernel version of setsockopt(2).
2407 * XXX: optlen is size_t, not socklen_t
2410 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2413 struct sockopt sopt;
2415 sopt.sopt_level = level;
2416 sopt.sopt_name = optname;
2417 sopt.sopt_dir = SOPT_SET;
2418 sopt.sopt_val = optval;
2419 sopt.sopt_valsize = optlen;
2420 sopt.sopt_td = NULL;
2421 return (sosetopt(so, &sopt));
2425 sosetopt(struct socket *so, struct sockopt *sopt)
2435 CURVNET_SET(so->so_vnet);
2437 if (sopt->sopt_level != SOL_SOCKET) {
2438 if (so->so_proto && so->so_proto->pr_ctloutput) {
2439 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2443 error = ENOPROTOOPT;
2445 switch (sopt->sopt_name) {
2447 case SO_ACCEPTFILTER:
2448 error = do_setopt_accept_filter(so, sopt);
2454 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2459 so->so_linger = l.l_linger;
2461 so->so_options |= SO_LINGER;
2463 so->so_options &= ~SO_LINGER;
2470 case SO_USELOOPBACK:
2480 error = sooptcopyin(sopt, &optval, sizeof optval,
2486 so->so_options |= sopt->sopt_name;
2488 so->so_options &= ~sopt->sopt_name;
2493 error = sooptcopyin(sopt, &optval, sizeof optval,
2495 if (optval < 0 || optval > rt_numfibs) {
2499 if (so->so_proto != NULL &&
2500 ((so->so_proto->pr_domain->dom_family == PF_INET) ||
2501 (so->so_proto->pr_domain->dom_family == PF_ROUTE))) {
2502 so->so_fibnum = optval;
2503 /* Note: ignore error */
2504 if (so->so_proto->pr_ctloutput)
2505 (*so->so_proto->pr_ctloutput)(so, sopt);
2514 error = sooptcopyin(sopt, &optval, sizeof optval,
2520 * Values < 1 make no sense for any of these options,
2528 switch (sopt->sopt_name) {
2531 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
2532 &so->so_snd : &so->so_rcv, (u_long)optval,
2533 so, curthread) == 0) {
2537 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
2538 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
2542 * Make sure the low-water is never greater than the
2546 SOCKBUF_LOCK(&so->so_snd);
2547 so->so_snd.sb_lowat =
2548 (optval > so->so_snd.sb_hiwat) ?
2549 so->so_snd.sb_hiwat : optval;
2550 SOCKBUF_UNLOCK(&so->so_snd);
2553 SOCKBUF_LOCK(&so->so_rcv);
2554 so->so_rcv.sb_lowat =
2555 (optval > so->so_rcv.sb_hiwat) ?
2556 so->so_rcv.sb_hiwat : optval;
2557 SOCKBUF_UNLOCK(&so->so_rcv);
2564 #ifdef COMPAT_FREEBSD32
2565 if (SV_CURPROC_FLAG(SV_ILP32)) {
2566 struct timeval32 tv32;
2568 error = sooptcopyin(sopt, &tv32, sizeof tv32,
2570 CP(tv32, tv, tv_sec);
2571 CP(tv32, tv, tv_usec);
2574 error = sooptcopyin(sopt, &tv, sizeof tv,
2579 /* assert(hz > 0); */
2580 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
2581 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
2585 /* assert(tick > 0); */
2586 /* assert(ULONG_MAX - INT_MAX >= 1000000); */
2587 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
2588 if (val > INT_MAX) {
2592 if (val == 0 && tv.tv_usec != 0)
2595 switch (sopt->sopt_name) {
2597 so->so_snd.sb_timeo = val;
2600 so->so_rcv.sb_timeo = val;
2607 error = sooptcopyin(sopt, &extmac, sizeof extmac,
2611 error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2619 error = ENOPROTOOPT;
2622 if (error == 0 && so->so_proto != NULL &&
2623 so->so_proto->pr_ctloutput != NULL) {
2624 (void) ((*so->so_proto->pr_ctloutput)
2634 * Helper routine for getsockopt.
2637 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2645 * Documented get behavior is that we always return a value, possibly
2646 * truncated to fit in the user's buffer. Traditional behavior is
2647 * that we always tell the user precisely how much we copied, rather
2648 * than something useful like the total amount we had available for
2649 * her. Note that this interface is not idempotent; the entire
2650 * answer must generated ahead of time.
2652 valsize = min(len, sopt->sopt_valsize);
2653 sopt->sopt_valsize = valsize;
2654 if (sopt->sopt_val != NULL) {
2655 if (sopt->sopt_td != NULL)
2656 error = copyout(buf, sopt->sopt_val, valsize);
2658 bcopy(buf, sopt->sopt_val, valsize);
2664 sogetopt(struct socket *so, struct sockopt *sopt)
2673 CURVNET_SET(so->so_vnet);
2675 if (sopt->sopt_level != SOL_SOCKET) {
2676 if (so->so_proto && so->so_proto->pr_ctloutput)
2677 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2679 error = ENOPROTOOPT;
2683 switch (sopt->sopt_name) {
2685 case SO_ACCEPTFILTER:
2686 error = do_getopt_accept_filter(so, sopt);
2691 l.l_onoff = so->so_options & SO_LINGER;
2692 l.l_linger = so->so_linger;
2694 error = sooptcopyout(sopt, &l, sizeof l);
2697 case SO_USELOOPBACK:
2709 optval = so->so_options & sopt->sopt_name;
2711 error = sooptcopyout(sopt, &optval, sizeof optval);
2715 optval = so->so_type;
2720 optval = so->so_error;
2726 optval = so->so_snd.sb_hiwat;
2730 optval = so->so_rcv.sb_hiwat;
2734 optval = so->so_snd.sb_lowat;
2738 optval = so->so_rcv.sb_lowat;
2743 optval = (sopt->sopt_name == SO_SNDTIMEO ?
2744 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2746 tv.tv_sec = optval / hz;
2747 tv.tv_usec = (optval % hz) * tick;
2748 #ifdef COMPAT_FREEBSD32
2749 if (SV_CURPROC_FLAG(SV_ILP32)) {
2750 struct timeval32 tv32;
2752 CP(tv, tv32, tv_sec);
2753 CP(tv, tv32, tv_usec);
2754 error = sooptcopyout(sopt, &tv32, sizeof tv32);
2757 error = sooptcopyout(sopt, &tv, sizeof tv);
2762 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2766 error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
2770 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2778 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2782 error = mac_getsockopt_peerlabel(
2783 sopt->sopt_td->td_ucred, so, &extmac);
2786 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2792 case SO_LISTENQLIMIT:
2793 optval = so->so_qlimit;
2797 optval = so->so_qlen;
2800 case SO_LISTENINCQLEN:
2801 optval = so->so_incqlen;
2805 error = ENOPROTOOPT;
2816 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2818 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2820 struct mbuf *m, *m_prev;
2821 int sopt_size = sopt->sopt_valsize;
2823 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2826 if (sopt_size > MLEN) {
2827 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
2828 if ((m->m_flags & M_EXT) == 0) {
2832 m->m_len = min(MCLBYTES, sopt_size);
2834 m->m_len = min(MLEN, sopt_size);
2836 sopt_size -= m->m_len;
2841 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2846 if (sopt_size > MLEN) {
2847 MCLGET(m, sopt->sopt_td != NULL ? M_WAIT :
2849 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;
2865 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2867 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2869 struct mbuf *m0 = m;
2871 if (sopt->sopt_val == NULL)
2873 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2874 if (sopt->sopt_td != NULL) {
2877 error = copyin(sopt->sopt_val, mtod(m, char *),
2884 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2885 sopt->sopt_valsize -= m->m_len;
2886 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2889 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2890 panic("ip6_sooptmcopyin");
2894 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2896 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2898 struct mbuf *m0 = m;
2901 if (sopt->sopt_val == NULL)
2903 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2904 if (sopt->sopt_td != NULL) {
2907 error = copyout(mtod(m, char *), sopt->sopt_val,
2914 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2915 sopt->sopt_valsize -= m->m_len;
2916 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2917 valsize += m->m_len;
2921 /* enough soopt buffer should be given from user-land */
2925 sopt->sopt_valsize = valsize;
2930 * sohasoutofband(): protocol notifies socket layer of the arrival of new
2931 * out-of-band data, which will then notify socket consumers.
2934 sohasoutofband(struct socket *so)
2937 if (so->so_sigio != NULL)
2938 pgsigio(&so->so_sigio, SIGURG, 0);
2939 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
2943 sopoll(struct socket *so, int events, struct ucred *active_cred,
2948 * We do not need to set or assert curvnet as long as everyone uses
2951 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
2956 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
2961 SOCKBUF_LOCK(&so->so_snd);
2962 SOCKBUF_LOCK(&so->so_rcv);
2963 if (events & (POLLIN | POLLRDNORM))
2964 if (soreadabledata(so))
2965 revents |= events & (POLLIN | POLLRDNORM);
2967 if (events & (POLLOUT | POLLWRNORM))
2968 if (sowriteable(so))
2969 revents |= events & (POLLOUT | POLLWRNORM);
2971 if (events & (POLLPRI | POLLRDBAND))
2972 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
2973 revents |= events & (POLLPRI | POLLRDBAND);
2975 if ((events & POLLINIGNEOF) == 0) {
2976 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2977 revents |= events & (POLLIN | POLLRDNORM);
2978 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
2984 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
2985 selrecord(td, &so->so_rcv.sb_sel);
2986 so->so_rcv.sb_flags |= SB_SEL;
2989 if (events & (POLLOUT | POLLWRNORM)) {
2990 selrecord(td, &so->so_snd.sb_sel);
2991 so->so_snd.sb_flags |= SB_SEL;
2995 SOCKBUF_UNLOCK(&so->so_rcv);
2996 SOCKBUF_UNLOCK(&so->so_snd);
3001 soo_kqfilter(struct file *fp, struct knote *kn)
3003 struct socket *so = kn->kn_fp->f_data;
3006 switch (kn->kn_filter) {
3008 if (so->so_options & SO_ACCEPTCONN)
3009 kn->kn_fop = &solisten_filtops;
3011 kn->kn_fop = &soread_filtops;
3015 kn->kn_fop = &sowrite_filtops;
3023 knlist_add(&sb->sb_sel.si_note, kn, 1);
3024 sb->sb_flags |= SB_KNOTE;
3030 * Some routines that return EOPNOTSUPP for entry points that are not
3031 * supported by a protocol. Fill in as needed.
3034 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
3041 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
3048 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3055 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3062 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
3069 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
3070 struct ifnet *ifp, struct thread *td)
3077 pru_disconnect_notsupp(struct socket *so)
3084 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
3091 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
3098 pru_rcvd_notsupp(struct socket *so, int flags)
3105 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
3112 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
3113 struct sockaddr *addr, struct mbuf *control, struct thread *td)
3120 * This isn't really a ``null'' operation, but it's the default one and
3121 * doesn't do anything destructive.
3124 pru_sense_null(struct socket *so, struct stat *sb)
3127 sb->st_blksize = so->so_snd.sb_hiwat;
3132 pru_shutdown_notsupp(struct socket *so)
3139 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
3146 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
3147 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
3154 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
3155 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
3162 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
3170 filt_sordetach(struct knote *kn)
3172 struct socket *so = kn->kn_fp->f_data;
3174 SOCKBUF_LOCK(&so->so_rcv);
3175 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
3176 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
3177 so->so_rcv.sb_flags &= ~SB_KNOTE;
3178 SOCKBUF_UNLOCK(&so->so_rcv);
3183 filt_soread(struct knote *kn, long hint)
3187 so = kn->kn_fp->f_data;
3188 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
3190 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
3191 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3192 kn->kn_flags |= EV_EOF;
3193 kn->kn_fflags = so->so_error;
3195 } else if (so->so_error) /* temporary udp error */
3197 else if (kn->kn_sfflags & NOTE_LOWAT)
3198 return (kn->kn_data >= kn->kn_sdata);
3200 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
3204 filt_sowdetach(struct knote *kn)
3206 struct socket *so = kn->kn_fp->f_data;
3208 SOCKBUF_LOCK(&so->so_snd);
3209 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
3210 if (knlist_empty(&so->so_snd.sb_sel.si_note))
3211 so->so_snd.sb_flags &= ~SB_KNOTE;
3212 SOCKBUF_UNLOCK(&so->so_snd);
3217 filt_sowrite(struct knote *kn, long hint)
3221 so = kn->kn_fp->f_data;
3222 SOCKBUF_LOCK_ASSERT(&so->so_snd);
3223 kn->kn_data = sbspace(&so->so_snd);
3224 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
3225 kn->kn_flags |= EV_EOF;
3226 kn->kn_fflags = so->so_error;
3228 } else if (so->so_error) /* temporary udp error */
3230 else if (((so->so_state & SS_ISCONNECTED) == 0) &&
3231 (so->so_proto->pr_flags & PR_CONNREQUIRED))
3233 else if (kn->kn_sfflags & NOTE_LOWAT)
3234 return (kn->kn_data >= kn->kn_sdata);
3236 return (kn->kn_data >= so->so_snd.sb_lowat);
3241 filt_solisten(struct knote *kn, long hint)
3243 struct socket *so = kn->kn_fp->f_data;
3245 kn->kn_data = so->so_qlen;
3246 return (! TAILQ_EMPTY(&so->so_comp));
3250 socheckuid(struct socket *so, uid_t uid)
3255 if (so->so_cred->cr_uid != uid)
3261 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
3267 error = sysctl_handle_int(oidp, &val, 0, req);
3268 if (error || !req->newptr )
3271 if (val < 1 || val > USHRT_MAX)
3279 * These functions are used by protocols to notify the socket layer (and its
3280 * consumers) of state changes in the sockets driven by protocol-side events.
3284 * Procedures to manipulate state flags of socket and do appropriate wakeups.
3286 * Normal sequence from the active (originating) side is that
3287 * soisconnecting() is called during processing of connect() call, resulting
3288 * in an eventual call to soisconnected() if/when the connection is
3289 * established. When the connection is torn down soisdisconnecting() is
3290 * called during processing of disconnect() call, and soisdisconnected() is
3291 * called when the connection to the peer is totally severed. The semantics
3292 * of these routines are such that connectionless protocols can call
3293 * soisconnected() and soisdisconnected() only, bypassing the in-progress
3294 * calls when setting up a ``connection'' takes no time.
3296 * From the passive side, a socket is created with two queues of sockets:
3297 * so_incomp for connections in progress and so_comp for connections already
3298 * made and awaiting user acceptance. As a protocol is preparing incoming
3299 * connections, it creates a socket structure queued on so_incomp by calling
3300 * sonewconn(). When the connection is established, soisconnected() is
3301 * called, and transfers the socket structure to so_comp, making it available
3304 * If a socket is closed with sockets on either so_incomp or so_comp, these
3305 * sockets are dropped.
3307 * If higher-level protocols are implemented in the kernel, the wakeups done
3308 * here will sometimes cause software-interrupt process scheduling.
3311 soisconnecting(struct socket *so)
3315 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3316 so->so_state |= SS_ISCONNECTING;
3321 soisconnected(struct socket *so)
3323 struct socket *head;
3329 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3330 so->so_state |= SS_ISCONNECTED;
3332 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
3333 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3335 TAILQ_REMOVE(&head->so_incomp, so, so_list);
3337 so->so_qstate &= ~SQ_INCOMP;
3338 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
3340 so->so_qstate |= SQ_COMP;
3343 wakeup_one(&head->so_timeo);
3346 soupcall_set(so, SO_RCV,
3347 head->so_accf->so_accept_filter->accf_callback,
3348 head->so_accf->so_accept_filter_arg);
3349 so->so_options &= ~SO_ACCEPTFILTER;
3350 ret = head->so_accf->so_accept_filter->accf_callback(so,
3351 head->so_accf->so_accept_filter_arg, M_DONTWAIT);
3352 if (ret == SU_ISCONNECTED)
3353 soupcall_clear(so, SO_RCV);
3355 if (ret == SU_ISCONNECTED)
3362 wakeup(&so->so_timeo);
3368 soisdisconnecting(struct socket *so)
3372 * Note: This code assumes that SOCK_LOCK(so) and
3373 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3375 SOCKBUF_LOCK(&so->so_rcv);
3376 so->so_state &= ~SS_ISCONNECTING;
3377 so->so_state |= SS_ISDISCONNECTING;
3378 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3379 sorwakeup_locked(so);
3380 SOCKBUF_LOCK(&so->so_snd);
3381 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3382 sowwakeup_locked(so);
3383 wakeup(&so->so_timeo);
3387 soisdisconnected(struct socket *so)
3391 * Note: This code assumes that SOCK_LOCK(so) and
3392 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3394 SOCKBUF_LOCK(&so->so_rcv);
3395 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3396 so->so_state |= SS_ISDISCONNECTED;
3397 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3398 sorwakeup_locked(so);
3399 SOCKBUF_LOCK(&so->so_snd);
3400 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3401 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
3402 sowwakeup_locked(so);
3403 wakeup(&so->so_timeo);
3407 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3410 sodupsockaddr(const struct sockaddr *sa, int mflags)
3412 struct sockaddr *sa2;
3414 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3416 bcopy(sa, sa2, sa->sa_len);
3421 * Register per-socket buffer upcalls.
3424 soupcall_set(struct socket *so, int which,
3425 int (*func)(struct socket *, void *, int), void *arg)
3437 panic("soupcall_set: bad which");
3439 SOCKBUF_LOCK_ASSERT(sb);
3441 /* XXX: accf_http actually wants to do this on purpose. */
3442 KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall"));
3444 sb->sb_upcall = func;
3445 sb->sb_upcallarg = arg;
3446 sb->sb_flags |= SB_UPCALL;
3450 soupcall_clear(struct socket *so, int which)
3462 panic("soupcall_clear: bad which");
3464 SOCKBUF_LOCK_ASSERT(sb);
3465 KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear"));
3466 sb->sb_upcall = NULL;
3467 sb->sb_upcallarg = NULL;
3468 sb->sb_flags &= ~SB_UPCALL;
3472 * Create an external-format (``xsocket'') structure using the information in
3473 * the kernel-format socket structure pointed to by so. This is done to
3474 * reduce the spew of irrelevant information over this interface, to isolate
3475 * user code from changes in the kernel structure, and potentially to provide
3476 * information-hiding if we decide that some of this information should be
3477 * hidden from users.
3480 sotoxsocket(struct socket *so, struct xsocket *xso)
3483 xso->xso_len = sizeof *xso;
3485 xso->so_type = so->so_type;
3486 xso->so_options = so->so_options;
3487 xso->so_linger = so->so_linger;
3488 xso->so_state = so->so_state;
3489 xso->so_pcb = so->so_pcb;
3490 xso->xso_protocol = so->so_proto->pr_protocol;
3491 xso->xso_family = so->so_proto->pr_domain->dom_family;
3492 xso->so_qlen = so->so_qlen;
3493 xso->so_incqlen = so->so_incqlen;
3494 xso->so_qlimit = so->so_qlimit;
3495 xso->so_timeo = so->so_timeo;
3496 xso->so_error = so->so_error;
3497 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
3498 xso->so_oobmark = so->so_oobmark;
3499 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
3500 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
3501 xso->so_uid = so->so_cred->cr_uid;
3506 * Socket accessor functions to provide external consumers with
3507 * a safe interface to socket state
3512 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg)
3515 TAILQ_FOREACH(so, &so->so_comp, so_list)
3520 so_sockbuf_rcv(struct socket *so)
3523 return (&so->so_rcv);
3527 so_sockbuf_snd(struct socket *so)
3530 return (&so->so_snd);
3534 so_state_get(const struct socket *so)
3537 return (so->so_state);
3541 so_state_set(struct socket *so, int val)
3548 so_options_get(const struct socket *so)
3551 return (so->so_options);
3555 so_options_set(struct socket *so, int val)
3558 so->so_options = val;
3562 so_error_get(const struct socket *so)
3565 return (so->so_error);
3569 so_error_set(struct socket *so, int val)
3576 so_linger_get(const struct socket *so)
3579 return (so->so_linger);
3583 so_linger_set(struct socket *so, int val)
3586 so->so_linger = val;
3590 so_protosw_get(const struct socket *so)
3593 return (so->so_proto);
3597 so_protosw_set(struct socket *so, struct protosw *val)
3604 so_sorwakeup(struct socket *so)
3611 so_sowwakeup(struct socket *so)
3618 so_sorwakeup_locked(struct socket *so)
3621 sorwakeup_locked(so);
3625 so_sowwakeup_locked(struct socket *so)
3628 sowwakeup_locked(so);
3632 so_lock(struct socket *so)
3638 so_unlock(struct socket *so)