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>
138 #include <sys/syslog.h>
140 #include <net/vnet.h>
142 #include <security/mac/mac_framework.h>
146 #ifdef COMPAT_FREEBSD32
147 #include <sys/mount.h>
148 #include <sys/sysent.h>
149 #include <compat/freebsd32/freebsd32.h>
152 static int soreceive_rcvoob(struct socket *so, struct uio *uio,
155 static void filt_sordetach(struct knote *kn);
156 static int filt_soread(struct knote *kn, long hint);
157 static void filt_sowdetach(struct knote *kn);
158 static int filt_sowrite(struct knote *kn, long hint);
159 static int filt_solisten(struct knote *kn, long hint);
161 static struct filterops solisten_filtops = {
163 .f_detach = filt_sordetach,
164 .f_event = filt_solisten,
166 static struct filterops soread_filtops = {
168 .f_detach = filt_sordetach,
169 .f_event = filt_soread,
171 static struct filterops sowrite_filtops = {
173 .f_detach = filt_sowdetach,
174 .f_event = filt_sowrite,
177 so_gen_t so_gencnt; /* generation count for sockets */
179 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
180 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
182 #define VNET_SO_ASSERT(so) \
183 VNET_ASSERT(curvnet != NULL, \
184 ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so)));
187 * Limit on the number of connections in the listen queue waiting
189 * NB: The orginal sysctl somaxconn is still available but hidden
190 * to prevent confusion about the actual purpose of this number.
192 static int somaxconn = SOMAXCONN;
195 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
201 error = sysctl_handle_int(oidp, &val, 0, req);
202 if (error || !req->newptr )
205 if (val < 1 || val > USHRT_MAX)
211 SYSCTL_PROC(_kern_ipc, OID_AUTO, soacceptqueue, CTLTYPE_UINT | CTLFLAG_RW,
212 0, sizeof(int), sysctl_somaxconn, "I",
213 "Maximum listen socket pending connection accept queue size");
214 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn,
215 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_SKIP,
216 0, sizeof(int), sysctl_somaxconn, "I",
217 "Maximum listen socket pending connection accept queue size (compat)");
219 static int numopensockets;
220 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
221 &numopensockets, 0, "Number of open sockets");
223 #if defined(SOCKET_SEND_COW) || defined(SOCKET_RECV_PFLIP)
224 SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0,
225 "Zero copy controls");
226 #ifdef SOCKET_RECV_PFLIP
227 int so_zero_copy_receive = 1;
228 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW,
229 &so_zero_copy_receive, 0, "Enable zero copy receive");
231 #ifdef SOCKET_SEND_COW
232 int so_zero_copy_send = 1;
233 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW,
234 &so_zero_copy_send, 0, "Enable zero copy send");
235 #endif /* SOCKET_SEND_COW */
236 #endif /* SOCKET_SEND_COW || SOCKET_RECV_PFLIP */
239 * accept_mtx locks down per-socket fields relating to accept queues. See
240 * socketvar.h for an annotation of the protected fields of struct socket.
242 struct mtx accept_mtx;
243 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
246 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
249 static struct mtx so_global_mtx;
250 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
253 * General IPC sysctl name space, used by sockets and a variety of other IPC
256 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
259 * Initialize the socket subsystem and set up the socket
262 uma_zone_t socket_zone;
266 socket_zone_change(void *tag)
269 uma_zone_set_max(socket_zone, maxsockets);
273 socket_init(void *tag)
276 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
277 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
278 uma_zone_set_max(socket_zone, maxsockets);
279 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
280 EVENTHANDLER_PRI_FIRST);
282 SYSINIT(socket, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, socket_init, NULL);
285 * Initialise maxsockets. This SYSINIT must be run after
289 init_maxsockets(void *ignored)
292 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
293 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
295 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
298 * Sysctl to get and set the maximum global sockets limit. Notify protocols
299 * of the change so that they can update their dependent limits as required.
302 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
304 int error, newmaxsockets;
306 newmaxsockets = maxsockets;
307 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
308 if (error == 0 && req->newptr) {
309 if (newmaxsockets > maxsockets) {
310 maxsockets = newmaxsockets;
311 if (maxsockets > ((maxfiles / 4) * 3)) {
312 maxfiles = (maxsockets * 5) / 4;
313 maxfilesperproc = (maxfiles * 9) / 10;
315 EVENTHANDLER_INVOKE(maxsockets_change);
321 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
322 &maxsockets, 0, sysctl_maxsockets, "IU",
323 "Maximum number of sockets avaliable");
326 * Socket operation routines. These routines are called by the routines in
327 * sys_socket.c or from a system process, and implement the semantics of
328 * socket operations by switching out to the protocol specific routines.
332 * Get a socket structure from our zone, and initialize it. Note that it
333 * would probably be better to allocate socket and PCB at the same time, but
334 * I'm not convinced that all the protocols can be easily modified to do
337 * soalloc() returns a socket with a ref count of 0.
339 static struct socket *
340 soalloc(struct vnet *vnet)
344 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
348 if (mac_socket_init(so, M_NOWAIT) != 0) {
349 uma_zfree(socket_zone, so);
353 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
354 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
355 sx_init(&so->so_snd.sb_sx, "so_snd_sx");
356 sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
357 TAILQ_INIT(&so->so_aiojobq);
358 mtx_lock(&so_global_mtx);
359 so->so_gencnt = ++so_gencnt;
362 VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p",
363 __func__, __LINE__, so));
364 vnet->vnet_sockcnt++;
367 mtx_unlock(&so_global_mtx);
372 * Free the storage associated with a socket at the socket layer, tear down
373 * locks, labels, etc. All protocol state is assumed already to have been
374 * torn down (and possibly never set up) by the caller.
377 sodealloc(struct socket *so)
380 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
381 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
383 mtx_lock(&so_global_mtx);
384 so->so_gencnt = ++so_gencnt;
385 --numopensockets; /* Could be below, but faster here. */
387 VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p",
388 __func__, __LINE__, so));
389 so->so_vnet->vnet_sockcnt--;
391 mtx_unlock(&so_global_mtx);
392 if (so->so_rcv.sb_hiwat)
393 (void)chgsbsize(so->so_cred->cr_uidinfo,
394 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
395 if (so->so_snd.sb_hiwat)
396 (void)chgsbsize(so->so_cred->cr_uidinfo,
397 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
399 /* remove acccept filter if one is present. */
400 if (so->so_accf != NULL)
401 do_setopt_accept_filter(so, NULL);
404 mac_socket_destroy(so);
407 sx_destroy(&so->so_snd.sb_sx);
408 sx_destroy(&so->so_rcv.sb_sx);
409 SOCKBUF_LOCK_DESTROY(&so->so_snd);
410 SOCKBUF_LOCK_DESTROY(&so->so_rcv);
411 uma_zfree(socket_zone, so);
415 * socreate returns a socket with a ref count of 1. The socket should be
416 * closed with soclose().
419 socreate(int dom, struct socket **aso, int type, int proto,
420 struct ucred *cred, struct thread *td)
427 prp = pffindproto(dom, proto, type);
429 prp = pffindtype(dom, type);
431 if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
432 prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
433 return (EPROTONOSUPPORT);
435 if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
436 return (EPROTONOSUPPORT);
438 if (prp->pr_type != type)
440 so = soalloc(CRED_TO_VNET(cred));
444 TAILQ_INIT(&so->so_incomp);
445 TAILQ_INIT(&so->so_comp);
447 so->so_cred = crhold(cred);
448 if ((prp->pr_domain->dom_family == PF_INET) ||
449 (prp->pr_domain->dom_family == PF_INET6) ||
450 (prp->pr_domain->dom_family == PF_ROUTE))
451 so->so_fibnum = td->td_proc->p_fibnum;
456 mac_socket_create(cred, so);
458 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
459 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
462 * Auto-sizing of socket buffers is managed by the protocols and
463 * the appropriate flags must be set in the pru_attach function.
465 CURVNET_SET(so->so_vnet);
466 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
469 KASSERT(so->so_count == 1, ("socreate: so_count %d",
480 static int regression_sonewconn_earlytest = 1;
481 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
482 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
486 * When an attempt at a new connection is noted on a socket which accepts
487 * connections, sonewconn is called. If the connection is possible (subject
488 * to space constraints, etc.) then we allocate a new structure, propoerly
489 * linked into the data structure of the original socket, and return this.
490 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
492 * Note: the ref count on the socket is 0 on return.
495 sonewconn(struct socket *head, int connstatus)
501 over = (head->so_qlen > 3 * head->so_qlimit / 2);
504 if (regression_sonewconn_earlytest && over) {
508 log(LOG_DEBUG, "%s: pcb %p: Listen queue overflow: "
509 "%i already in queue awaiting acceptance\n",
510 __func__, head->so_pcb, over);
513 VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
514 __func__, __LINE__, head));
515 so = soalloc(head->so_vnet);
517 log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: "
518 "limit reached or out of memory\n",
519 __func__, head->so_pcb);
522 if ((head->so_options & SO_ACCEPTFILTER) != 0)
525 so->so_type = head->so_type;
526 so->so_options = head->so_options &~ SO_ACCEPTCONN;
527 so->so_linger = head->so_linger;
528 so->so_state = head->so_state | SS_NOFDREF;
529 so->so_fibnum = head->so_fibnum;
530 so->so_proto = head->so_proto;
531 so->so_cred = crhold(head->so_cred);
533 mac_socket_newconn(head, so);
535 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
536 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
537 VNET_SO_ASSERT(head);
538 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
540 log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n",
541 __func__, head->so_pcb);
544 if ((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
546 log(LOG_DEBUG, "%s: pcb %p: pru_attach() failed\n",
547 __func__, head->so_pcb);
550 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
551 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
552 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
553 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
554 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
555 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
556 so->so_state |= connstatus;
559 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
560 so->so_qstate |= SQ_COMP;
564 * Keep removing sockets from the head until there's room for
565 * us to insert on the tail. In pre-locking revisions, this
566 * was a simple if(), but as we could be racing with other
567 * threads and soabort() requires dropping locks, we must
568 * loop waiting for the condition to be true.
570 while (head->so_incqlen > head->so_qlimit) {
572 sp = TAILQ_FIRST(&head->so_incomp);
573 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
575 sp->so_qstate &= ~SQ_INCOMP;
581 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
582 so->so_qstate |= SQ_INCOMP;
588 wakeup_one(&head->so_timeo);
594 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
598 CURVNET_SET(so->so_vnet);
599 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
605 * solisten() transitions a socket from a non-listening state to a listening
606 * state, but can also be used to update the listen queue depth on an
607 * existing listen socket. The protocol will call back into the sockets
608 * layer using solisten_proto_check() and solisten_proto() to check and set
609 * socket-layer listen state. Call backs are used so that the protocol can
610 * acquire both protocol and socket layer locks in whatever order is required
613 * Protocol implementors are advised to hold the socket lock across the
614 * socket-layer test and set to avoid races at the socket layer.
617 solisten(struct socket *so, int backlog, struct thread *td)
621 CURVNET_SET(so->so_vnet);
622 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td);
628 solisten_proto_check(struct socket *so)
631 SOCK_LOCK_ASSERT(so);
633 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
640 solisten_proto(struct socket *so, int backlog)
643 SOCK_LOCK_ASSERT(so);
645 if (backlog < 0 || backlog > somaxconn)
647 so->so_qlimit = backlog;
648 so->so_options |= SO_ACCEPTCONN;
652 * Evaluate the reference count and named references on a socket; if no
653 * references remain, free it. This should be called whenever a reference is
654 * released, such as in sorele(), but also when named reference flags are
655 * cleared in socket or protocol code.
657 * sofree() will free the socket if:
659 * - There are no outstanding file descriptor references or related consumers
662 * - The socket has been closed by user space, if ever open (SS_NOFDREF).
664 * - The protocol does not have an outstanding strong reference on the socket
667 * - The socket is not in a completed connection queue, so a process has been
668 * notified that it is present. If it is removed, the user process may
669 * block in accept() despite select() saying the socket was ready.
672 sofree(struct socket *so)
674 struct protosw *pr = so->so_proto;
677 ACCEPT_LOCK_ASSERT();
678 SOCK_LOCK_ASSERT(so);
680 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
681 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
689 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
690 (so->so_qstate & SQ_INCOMP) != 0,
691 ("sofree: so_head != NULL, but neither SQ_COMP nor "
693 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
694 (so->so_qstate & SQ_INCOMP) == 0,
695 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
696 TAILQ_REMOVE(&head->so_incomp, so, so_list);
698 so->so_qstate &= ~SQ_INCOMP;
701 KASSERT((so->so_qstate & SQ_COMP) == 0 &&
702 (so->so_qstate & SQ_INCOMP) == 0,
703 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
704 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
705 if (so->so_options & SO_ACCEPTCONN) {
706 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
707 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_incomp populated"));
713 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
714 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
715 if (pr->pr_usrreqs->pru_detach != NULL)
716 (*pr->pr_usrreqs->pru_detach)(so);
719 * From this point on, we assume that no other references to this
720 * socket exist anywhere else in the stack. Therefore, no locks need
721 * to be acquired or held.
723 * We used to do a lot of socket buffer and socket locking here, as
724 * well as invoke sorflush() and perform wakeups. The direct call to
725 * dom_dispose() and sbrelease_internal() are an inlining of what was
726 * necessary from sorflush().
728 * Notice that the socket buffer and kqueue state are torn down
729 * before calling pru_detach. This means that protocols shold not
730 * assume they can perform socket wakeups, etc, in their detach code.
732 sbdestroy(&so->so_snd, so);
733 sbdestroy(&so->so_rcv, so);
734 seldrain(&so->so_snd.sb_sel);
735 seldrain(&so->so_rcv.sb_sel);
736 knlist_destroy(&so->so_rcv.sb_sel.si_note);
737 knlist_destroy(&so->so_snd.sb_sel.si_note);
742 * Close a socket on last file table reference removal. Initiate disconnect
743 * if connected. Free socket when disconnect complete.
745 * This function will sorele() the socket. Note that soclose() may be called
746 * prior to the ref count reaching zero. The actual socket structure will
747 * not be freed until the ref count reaches zero.
750 soclose(struct socket *so)
754 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
756 CURVNET_SET(so->so_vnet);
757 funsetown(&so->so_sigio);
758 if (so->so_state & SS_ISCONNECTED) {
759 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
760 error = sodisconnect(so);
762 if (error == ENOTCONN)
767 if (so->so_options & SO_LINGER) {
768 if ((so->so_state & SS_ISDISCONNECTING) &&
769 (so->so_state & SS_NBIO))
771 while (so->so_state & SS_ISCONNECTED) {
772 error = tsleep(&so->so_timeo,
773 PSOCK | PCATCH, "soclos", so->so_linger * hz);
781 if (so->so_proto->pr_usrreqs->pru_close != NULL)
782 (*so->so_proto->pr_usrreqs->pru_close)(so);
783 if (so->so_options & SO_ACCEPTCONN) {
786 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
787 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
789 sp->so_qstate &= ~SQ_INCOMP;
795 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
796 TAILQ_REMOVE(&so->so_comp, sp, so_list);
798 sp->so_qstate &= ~SQ_COMP;
808 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
809 so->so_state |= SS_NOFDREF;
816 * soabort() is used to abruptly tear down a connection, such as when a
817 * resource limit is reached (listen queue depth exceeded), or if a listen
818 * socket is closed while there are sockets waiting to be accepted.
820 * This interface is tricky, because it is called on an unreferenced socket,
821 * and must be called only by a thread that has actually removed the socket
822 * from the listen queue it was on, or races with other threads are risked.
824 * This interface will call into the protocol code, so must not be called
825 * with any socket locks held. Protocols do call it while holding their own
826 * recursible protocol mutexes, but this is something that should be subject
827 * to review in the future.
830 soabort(struct socket *so)
834 * In as much as is possible, assert that no references to this
835 * socket are held. This is not quite the same as asserting that the
836 * current thread is responsible for arranging for no references, but
837 * is as close as we can get for now.
839 KASSERT(so->so_count == 0, ("soabort: so_count"));
840 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
841 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
842 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
843 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
846 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
847 (*so->so_proto->pr_usrreqs->pru_abort)(so);
854 soaccept(struct socket *so, struct sockaddr **nam)
859 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
860 so->so_state &= ~SS_NOFDREF;
863 CURVNET_SET(so->so_vnet);
864 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
870 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
874 if (so->so_options & SO_ACCEPTCONN)
877 CURVNET_SET(so->so_vnet);
879 * If protocol is connection-based, can only connect once.
880 * Otherwise, if connected, try to disconnect first. This allows
881 * user to disconnect by connecting to, e.g., a null address.
883 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
884 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
885 (error = sodisconnect(so)))) {
889 * Prevent accumulated error from previous connection from
893 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
901 soconnect2(struct socket *so1, struct socket *so2)
905 CURVNET_SET(so1->so_vnet);
906 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
912 sodisconnect(struct socket *so)
916 if ((so->so_state & SS_ISCONNECTED) == 0)
918 if (so->so_state & SS_ISDISCONNECTING)
921 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
925 #ifdef SOCKET_SEND_COW
926 struct so_zerocopy_stats{
931 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
934 * sosend_copyin() is only used if zero copy sockets are enabled. Otherwise
935 * sosend_dgram() and sosend_generic() use m_uiotombuf().
937 * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or
938 * all of the data referenced by the uio. If desired, it uses zero-copy.
939 * *space will be updated to reflect data copied in.
941 * NB: If atomic I/O is requested, the caller must already have checked that
942 * space can hold resid bytes.
944 * NB: In the event of an error, the caller may need to free the partial
945 * chain pointed to by *mpp. The contents of both *uio and *space may be
946 * modified even in the case of an error.
949 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space,
952 struct mbuf *m, **mp, *top;
961 resid = uio->uio_resid;
965 if (resid >= MINCLSIZE) {
967 m = m_gethdr(M_WAITOK, MT_DATA);
969 m->m_pkthdr.rcvif = NULL;
971 m = m_get(M_WAITOK, MT_DATA);
972 if (so_zero_copy_send &&
973 resid >= PAGE_SIZE &&
974 *space >= PAGE_SIZE &&
975 uio->uio_iov->iov_len >= PAGE_SIZE) {
976 so_zerocp_stats.size_ok++;
977 so_zerocp_stats.align_ok++;
978 cow_send = socow_setup(m, uio);
982 m_clget(m, M_WAITOK);
983 len = min(min(MCLBYTES, resid), *space);
987 m = m_gethdr(M_WAIT, MT_DATA);
989 m->m_pkthdr.rcvif = NULL;
991 len = min(min(MHLEN, resid), *space);
993 * For datagram protocols, leave room
994 * for protocol headers in first mbuf.
996 if (atomic && m && len < MHLEN)
999 m = m_get(M_WAIT, MT_DATA);
1000 len = min(min(MLEN, resid), *space);
1012 error = uiomove(mtod(m, void *), (int)len, uio);
1013 resid = uio->uio_resid;
1016 top->m_pkthdr.len += len;
1021 if (flags & MSG_EOR)
1022 top->m_flags |= M_EOR;
1025 } while (*space > 0 && atomic);
1030 #endif /* SOCKET_SEND_COW */
1032 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
1035 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
1036 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1040 int clen = 0, error, dontroute;
1041 #ifdef SOCKET_SEND_COW
1042 int atomic = sosendallatonce(so) || top;
1045 KASSERT(so->so_type == SOCK_DGRAM, ("sosend_dgram: !SOCK_DGRAM"));
1046 KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
1047 ("sosend_dgram: !PR_ATOMIC"));
1050 resid = uio->uio_resid;
1052 resid = top->m_pkthdr.len;
1054 * In theory resid should be unsigned. However, space must be
1055 * signed, as it might be less than 0 if we over-committed, and we
1056 * must use a signed comparison of space and resid. On the other
1057 * hand, a negative resid causes us to loop sending 0-length
1058 * segments to the protocol.
1066 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
1068 td->td_ru.ru_msgsnd++;
1069 if (control != NULL)
1070 clen = control->m_len;
1072 SOCKBUF_LOCK(&so->so_snd);
1073 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1074 SOCKBUF_UNLOCK(&so->so_snd);
1079 error = so->so_error;
1081 SOCKBUF_UNLOCK(&so->so_snd);
1084 if ((so->so_state & SS_ISCONNECTED) == 0) {
1086 * `sendto' and `sendmsg' is allowed on a connection-based
1087 * socket if it supports implied connect. Return ENOTCONN if
1088 * not connected and no address is supplied.
1090 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1091 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1092 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1093 !(resid == 0 && clen != 0)) {
1094 SOCKBUF_UNLOCK(&so->so_snd);
1098 } else if (addr == NULL) {
1099 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1102 error = EDESTADDRREQ;
1103 SOCKBUF_UNLOCK(&so->so_snd);
1109 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
1110 * problem and need fixing.
1112 space = sbspace(&so->so_snd);
1113 if (flags & MSG_OOB)
1116 SOCKBUF_UNLOCK(&so->so_snd);
1117 if (resid > space) {
1123 if (flags & MSG_EOR)
1124 top->m_flags |= M_EOR;
1126 #ifdef SOCKET_SEND_COW
1127 error = sosend_copyin(uio, &top, atomic, &space, flags);
1132 * Copy the data from userland into a mbuf chain.
1133 * If no data is to be copied in, a single empty mbuf
1136 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1137 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1139 error = EFAULT; /* only possible error */
1142 space -= resid - uio->uio_resid;
1143 #endif /* SOCKET_SEND_COW */
1144 resid = uio->uio_resid;
1146 KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1148 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1153 so->so_options |= SO_DONTROUTE;
1157 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1158 * of date. We could have recieved a reset packet in an interrupt or
1159 * maybe we slept while doing page faults in uiomove() etc. We could
1160 * probably recheck again inside the locking protection here, but
1161 * there are probably other places that this also happens. We must
1165 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1166 (flags & MSG_OOB) ? PRUS_OOB :
1168 * If the user set MSG_EOF, the protocol understands this flag and
1169 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1171 ((flags & MSG_EOF) &&
1172 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1175 /* If there is more to send set PRUS_MORETOCOME */
1176 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1177 top, addr, control, td);
1180 so->so_options &= ~SO_DONTROUTE;
1189 if (control != NULL)
1195 * Send on a socket. If send must go all at once and message is larger than
1196 * send buffering, then hard error. Lock against other senders. If must go
1197 * all at once and not enough room now, then inform user that this would
1198 * block and do nothing. Otherwise, if nonblocking, send as much as
1199 * possible. The data to be sent is described by "uio" if nonzero, otherwise
1200 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1201 * in mbuf chain must be small enough to send all at once.
1203 * Returns nonzero on error, timeout or signal; callers must check for short
1204 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1208 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1209 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1213 int clen = 0, error, dontroute;
1214 int atomic = sosendallatonce(so) || top;
1217 resid = uio->uio_resid;
1219 resid = top->m_pkthdr.len;
1221 * In theory resid should be unsigned. However, space must be
1222 * signed, as it might be less than 0 if we over-committed, and we
1223 * must use a signed comparison of space and resid. On the other
1224 * hand, a negative resid causes us to loop sending 0-length
1225 * segments to the protocol.
1227 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1228 * type sockets since that's an error.
1230 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1236 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1237 (so->so_proto->pr_flags & PR_ATOMIC);
1239 td->td_ru.ru_msgsnd++;
1240 if (control != NULL)
1241 clen = control->m_len;
1243 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1249 SOCKBUF_LOCK(&so->so_snd);
1250 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1251 SOCKBUF_UNLOCK(&so->so_snd);
1256 error = so->so_error;
1258 SOCKBUF_UNLOCK(&so->so_snd);
1261 if ((so->so_state & SS_ISCONNECTED) == 0) {
1263 * `sendto' and `sendmsg' is allowed on a connection-
1264 * based socket if it supports implied connect.
1265 * Return ENOTCONN if not connected and no address is
1268 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1269 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1270 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1271 !(resid == 0 && clen != 0)) {
1272 SOCKBUF_UNLOCK(&so->so_snd);
1276 } else if (addr == NULL) {
1277 SOCKBUF_UNLOCK(&so->so_snd);
1278 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1281 error = EDESTADDRREQ;
1285 space = sbspace(&so->so_snd);
1286 if (flags & MSG_OOB)
1288 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1289 clen > so->so_snd.sb_hiwat) {
1290 SOCKBUF_UNLOCK(&so->so_snd);
1294 if (space < resid + clen &&
1295 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1296 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
1297 SOCKBUF_UNLOCK(&so->so_snd);
1298 error = EWOULDBLOCK;
1301 error = sbwait(&so->so_snd);
1302 SOCKBUF_UNLOCK(&so->so_snd);
1307 SOCKBUF_UNLOCK(&so->so_snd);
1312 if (flags & MSG_EOR)
1313 top->m_flags |= M_EOR;
1315 #ifdef SOCKET_SEND_COW
1316 error = sosend_copyin(uio, &top, atomic,
1322 * Copy the data from userland into a mbuf
1323 * chain. If no data is to be copied in,
1324 * a single empty mbuf is returned.
1326 top = m_uiotombuf(uio, M_WAITOK, space,
1327 (atomic ? max_hdr : 0),
1328 (atomic ? M_PKTHDR : 0) |
1329 ((flags & MSG_EOR) ? M_EOR : 0));
1331 error = EFAULT; /* only possible error */
1334 space -= resid - uio->uio_resid;
1335 #endif /* SOCKET_SEND_COW */
1336 resid = uio->uio_resid;
1340 so->so_options |= SO_DONTROUTE;
1344 * XXX all the SBS_CANTSENDMORE checks previously
1345 * done could be out of date. We could have recieved
1346 * a reset packet in an interrupt or maybe we slept
1347 * while doing page faults in uiomove() etc. We
1348 * could probably recheck again inside the locking
1349 * protection here, but there are probably other
1350 * places that this also happens. We must rethink
1354 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1355 (flags & MSG_OOB) ? PRUS_OOB :
1357 * If the user set MSG_EOF, the protocol understands
1358 * this flag and nothing left to send then use
1359 * PRU_SEND_EOF instead of PRU_SEND.
1361 ((flags & MSG_EOF) &&
1362 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1365 /* If there is more to send set PRUS_MORETOCOME. */
1366 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1367 top, addr, control, td);
1370 so->so_options &= ~SO_DONTROUTE;
1378 } while (resid && space > 0);
1382 sbunlock(&so->so_snd);
1386 if (control != NULL)
1392 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1393 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1397 CURVNET_SET(so->so_vnet);
1398 error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1399 control, flags, td);
1405 * The part of soreceive() that implements reading non-inline out-of-band
1406 * data from a socket. For more complete comments, see soreceive(), from
1407 * which this code originated.
1409 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1410 * unable to return an mbuf chain to the caller.
1413 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1415 struct protosw *pr = so->so_proto;
1419 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1422 m = m_get(M_WAIT, MT_DATA);
1423 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1427 #ifdef SOCKET_RECV_PFLIP
1428 if (so_zero_copy_receive) {
1431 if ((m->m_flags & M_EXT)
1432 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1437 error = uiomoveco(mtod(m, void *),
1438 min(uio->uio_resid, m->m_len),
1441 #endif /* SOCKET_RECV_PFLIP */
1442 error = uiomove(mtod(m, void *),
1443 (int) min(uio->uio_resid, m->m_len), uio);
1445 } while (uio->uio_resid && error == 0 && m);
1453 * Following replacement or removal of the first mbuf on the first mbuf chain
1454 * of a socket buffer, push necessary state changes back into the socket
1455 * buffer so that other consumers see the values consistently. 'nextrecord'
1456 * is the callers locally stored value of the original value of
1457 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1458 * NOTE: 'nextrecord' may be NULL.
1460 static __inline void
1461 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1464 SOCKBUF_LOCK_ASSERT(sb);
1466 * First, update for the new value of nextrecord. If necessary, make
1467 * it the first record.
1469 if (sb->sb_mb != NULL)
1470 sb->sb_mb->m_nextpkt = nextrecord;
1472 sb->sb_mb = nextrecord;
1475 * Now update any dependent socket buffer fields to reflect the new
1476 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
1477 * addition of a second clause that takes care of the case where
1478 * sb_mb has been updated, but remains the last record.
1480 if (sb->sb_mb == NULL) {
1481 sb->sb_mbtail = NULL;
1482 sb->sb_lastrecord = NULL;
1483 } else if (sb->sb_mb->m_nextpkt == NULL)
1484 sb->sb_lastrecord = sb->sb_mb;
1489 * Implement receive operations on a socket. We depend on the way that
1490 * records are added to the sockbuf by sbappend. In particular, each record
1491 * (mbufs linked through m_next) must begin with an address if the protocol
1492 * so specifies, followed by an optional mbuf or mbufs containing ancillary
1493 * data, and then zero or more mbufs of data. In order to allow parallelism
1494 * between network receive and copying to user space, as well as avoid
1495 * sleeping with a mutex held, we release the socket buffer mutex during the
1496 * user space copy. Although the sockbuf is locked, new data may still be
1497 * appended, and thus we must maintain consistency of the sockbuf during that
1500 * The caller may receive the data as a single mbuf chain by supplying an
1501 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1502 * the count in uio_resid.
1505 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1506 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1508 struct mbuf *m, **mp;
1509 int flags, error, offset;
1511 struct protosw *pr = so->so_proto;
1512 struct mbuf *nextrecord;
1514 ssize_t orig_resid = uio->uio_resid;
1519 if (controlp != NULL)
1522 flags = *flagsp &~ MSG_EOR;
1525 if (flags & MSG_OOB)
1526 return (soreceive_rcvoob(so, uio, flags));
1529 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1530 && uio->uio_resid) {
1532 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1535 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1540 SOCKBUF_LOCK(&so->so_rcv);
1541 m = so->so_rcv.sb_mb;
1543 * If we have less data than requested, block awaiting more (subject
1544 * to any timeout) if:
1545 * 1. the current count is less than the low water mark, or
1546 * 2. MSG_DONTWAIT is not set
1548 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1549 so->so_rcv.sb_cc < uio->uio_resid) &&
1550 so->so_rcv.sb_cc < so->so_rcv.sb_lowat &&
1551 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1552 KASSERT(m != NULL || !so->so_rcv.sb_cc,
1553 ("receive: m == %p so->so_rcv.sb_cc == %u",
1554 m, so->so_rcv.sb_cc));
1558 error = so->so_error;
1559 if ((flags & MSG_PEEK) == 0)
1561 SOCKBUF_UNLOCK(&so->so_rcv);
1564 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1565 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1567 SOCKBUF_UNLOCK(&so->so_rcv);
1572 for (; m != NULL; m = m->m_next)
1573 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1574 m = so->so_rcv.sb_mb;
1577 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1578 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1579 SOCKBUF_UNLOCK(&so->so_rcv);
1583 if (uio->uio_resid == 0) {
1584 SOCKBUF_UNLOCK(&so->so_rcv);
1587 if ((so->so_state & SS_NBIO) ||
1588 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1589 SOCKBUF_UNLOCK(&so->so_rcv);
1590 error = EWOULDBLOCK;
1593 SBLASTRECORDCHK(&so->so_rcv);
1594 SBLASTMBUFCHK(&so->so_rcv);
1595 error = sbwait(&so->so_rcv);
1596 SOCKBUF_UNLOCK(&so->so_rcv);
1603 * From this point onward, we maintain 'nextrecord' as a cache of the
1604 * pointer to the next record in the socket buffer. We must keep the
1605 * various socket buffer pointers and local stack versions of the
1606 * pointers in sync, pushing out modifications before dropping the
1607 * socket buffer mutex, and re-reading them when picking it up.
1609 * Otherwise, we will race with the network stack appending new data
1610 * or records onto the socket buffer by using inconsistent/stale
1611 * versions of the field, possibly resulting in socket buffer
1614 * By holding the high-level sblock(), we prevent simultaneous
1615 * readers from pulling off the front of the socket buffer.
1617 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1619 uio->uio_td->td_ru.ru_msgrcv++;
1620 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1621 SBLASTRECORDCHK(&so->so_rcv);
1622 SBLASTMBUFCHK(&so->so_rcv);
1623 nextrecord = m->m_nextpkt;
1624 if (pr->pr_flags & PR_ADDR) {
1625 KASSERT(m->m_type == MT_SONAME,
1626 ("m->m_type == %d", m->m_type));
1629 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1631 if (flags & MSG_PEEK) {
1634 sbfree(&so->so_rcv, m);
1635 so->so_rcv.sb_mb = m_free(m);
1636 m = so->so_rcv.sb_mb;
1637 sockbuf_pushsync(&so->so_rcv, nextrecord);
1642 * Process one or more MT_CONTROL mbufs present before any data mbufs
1643 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1644 * just copy the data; if !MSG_PEEK, we call into the protocol to
1645 * perform externalization (or freeing if controlp == NULL).
1647 if (m != NULL && m->m_type == MT_CONTROL) {
1648 struct mbuf *cm = NULL, *cmn;
1649 struct mbuf **cme = &cm;
1652 if (flags & MSG_PEEK) {
1653 if (controlp != NULL) {
1654 *controlp = m_copy(m, 0, m->m_len);
1655 controlp = &(*controlp)->m_next;
1659 sbfree(&so->so_rcv, m);
1660 so->so_rcv.sb_mb = m->m_next;
1663 cme = &(*cme)->m_next;
1664 m = so->so_rcv.sb_mb;
1666 } while (m != NULL && m->m_type == MT_CONTROL);
1667 if ((flags & MSG_PEEK) == 0)
1668 sockbuf_pushsync(&so->so_rcv, nextrecord);
1669 while (cm != NULL) {
1672 if (pr->pr_domain->dom_externalize != NULL) {
1673 SOCKBUF_UNLOCK(&so->so_rcv);
1675 error = (*pr->pr_domain->dom_externalize)
1677 SOCKBUF_LOCK(&so->so_rcv);
1678 } else if (controlp != NULL)
1682 if (controlp != NULL) {
1684 while (*controlp != NULL)
1685 controlp = &(*controlp)->m_next;
1690 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1692 nextrecord = so->so_rcv.sb_mb;
1696 if ((flags & MSG_PEEK) == 0) {
1697 KASSERT(m->m_nextpkt == nextrecord,
1698 ("soreceive: post-control, nextrecord !sync"));
1699 if (nextrecord == NULL) {
1700 KASSERT(so->so_rcv.sb_mb == m,
1701 ("soreceive: post-control, sb_mb!=m"));
1702 KASSERT(so->so_rcv.sb_lastrecord == m,
1703 ("soreceive: post-control, lastrecord!=m"));
1707 if (type == MT_OOBDATA)
1710 if ((flags & MSG_PEEK) == 0) {
1711 KASSERT(so->so_rcv.sb_mb == nextrecord,
1712 ("soreceive: sb_mb != nextrecord"));
1713 if (so->so_rcv.sb_mb == NULL) {
1714 KASSERT(so->so_rcv.sb_lastrecord == NULL,
1715 ("soreceive: sb_lastercord != NULL"));
1719 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1720 SBLASTRECORDCHK(&so->so_rcv);
1721 SBLASTMBUFCHK(&so->so_rcv);
1724 * Now continue to read any data mbufs off of the head of the socket
1725 * buffer until the read request is satisfied. Note that 'type' is
1726 * used to store the type of any mbuf reads that have happened so far
1727 * such that soreceive() can stop reading if the type changes, which
1728 * causes soreceive() to return only one of regular data and inline
1729 * out-of-band data in a single socket receive operation.
1733 while (m != NULL && uio->uio_resid > 0 && error == 0) {
1735 * If the type of mbuf has changed since the last mbuf
1736 * examined ('type'), end the receive operation.
1738 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1739 if (m->m_type == MT_OOBDATA || m->m_type == MT_CONTROL) {
1740 if (type != m->m_type)
1742 } else if (type == MT_OOBDATA)
1745 KASSERT(m->m_type == MT_DATA,
1746 ("m->m_type == %d", m->m_type));
1747 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1748 len = uio->uio_resid;
1749 if (so->so_oobmark && len > so->so_oobmark - offset)
1750 len = so->so_oobmark - offset;
1751 if (len > m->m_len - moff)
1752 len = m->m_len - moff;
1754 * If mp is set, just pass back the mbufs. Otherwise copy
1755 * them out via the uio, then free. Sockbuf must be
1756 * consistent here (points to current mbuf, it points to next
1757 * record) when we drop priority; we must note any additions
1758 * to the sockbuf when we block interrupts again.
1761 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1762 SBLASTRECORDCHK(&so->so_rcv);
1763 SBLASTMBUFCHK(&so->so_rcv);
1764 SOCKBUF_UNLOCK(&so->so_rcv);
1765 #ifdef SOCKET_RECV_PFLIP
1766 if (so_zero_copy_receive) {
1769 if ((m->m_flags & M_EXT)
1770 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1775 error = uiomoveco(mtod(m, char *) + moff,
1779 #endif /* SOCKET_RECV_PFLIP */
1780 error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1781 SOCKBUF_LOCK(&so->so_rcv);
1784 * The MT_SONAME mbuf has already been removed
1785 * from the record, so it is necessary to
1786 * remove the data mbufs, if any, to preserve
1787 * the invariant in the case of PR_ADDR that
1788 * requires MT_SONAME mbufs at the head of
1791 if (m && pr->pr_flags & PR_ATOMIC &&
1792 ((flags & MSG_PEEK) == 0))
1793 (void)sbdroprecord_locked(&so->so_rcv);
1794 SOCKBUF_UNLOCK(&so->so_rcv);
1798 uio->uio_resid -= len;
1799 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1800 if (len == m->m_len - moff) {
1801 if (m->m_flags & M_EOR)
1803 if (flags & MSG_PEEK) {
1807 nextrecord = m->m_nextpkt;
1808 sbfree(&so->so_rcv, m);
1812 so->so_rcv.sb_mb = m = m->m_next;
1815 so->so_rcv.sb_mb = m_free(m);
1816 m = so->so_rcv.sb_mb;
1818 sockbuf_pushsync(&so->so_rcv, nextrecord);
1819 SBLASTRECORDCHK(&so->so_rcv);
1820 SBLASTMBUFCHK(&so->so_rcv);
1823 if (flags & MSG_PEEK)
1829 if (flags & MSG_DONTWAIT)
1830 copy_flag = M_DONTWAIT;
1833 if (copy_flag == M_WAIT)
1834 SOCKBUF_UNLOCK(&so->so_rcv);
1835 *mp = m_copym(m, 0, len, copy_flag);
1836 if (copy_flag == M_WAIT)
1837 SOCKBUF_LOCK(&so->so_rcv);
1840 * m_copym() couldn't
1841 * allocate an mbuf. Adjust
1842 * uio_resid back (it was
1843 * adjusted down by len
1844 * bytes, which we didn't end
1845 * up "copying" over).
1847 uio->uio_resid += len;
1853 so->so_rcv.sb_cc -= len;
1856 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1857 if (so->so_oobmark) {
1858 if ((flags & MSG_PEEK) == 0) {
1859 so->so_oobmark -= len;
1860 if (so->so_oobmark == 0) {
1861 so->so_rcv.sb_state |= SBS_RCVATMARK;
1866 if (offset == so->so_oobmark)
1870 if (flags & MSG_EOR)
1873 * If the MSG_WAITALL flag is set (for non-atomic socket), we
1874 * must not quit until "uio->uio_resid == 0" or an error
1875 * termination. If a signal/timeout occurs, return with a
1876 * short count but without error. Keep sockbuf locked
1877 * against other readers.
1879 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1880 !sosendallatonce(so) && nextrecord == NULL) {
1881 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1882 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
1885 * Notify the protocol that some data has been
1886 * drained before blocking.
1888 if (pr->pr_flags & PR_WANTRCVD) {
1889 SOCKBUF_UNLOCK(&so->so_rcv);
1891 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1892 SOCKBUF_LOCK(&so->so_rcv);
1894 SBLASTRECORDCHK(&so->so_rcv);
1895 SBLASTMBUFCHK(&so->so_rcv);
1897 * We could receive some data while was notifying
1898 * the protocol. Skip blocking in this case.
1900 if (so->so_rcv.sb_mb == NULL) {
1901 error = sbwait(&so->so_rcv);
1903 SOCKBUF_UNLOCK(&so->so_rcv);
1907 m = so->so_rcv.sb_mb;
1909 nextrecord = m->m_nextpkt;
1913 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1914 if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1916 if ((flags & MSG_PEEK) == 0)
1917 (void) sbdroprecord_locked(&so->so_rcv);
1919 if ((flags & MSG_PEEK) == 0) {
1922 * First part is an inline SB_EMPTY_FIXUP(). Second
1923 * part makes sure sb_lastrecord is up-to-date if
1924 * there is still data in the socket buffer.
1926 so->so_rcv.sb_mb = nextrecord;
1927 if (so->so_rcv.sb_mb == NULL) {
1928 so->so_rcv.sb_mbtail = NULL;
1929 so->so_rcv.sb_lastrecord = NULL;
1930 } else if (nextrecord->m_nextpkt == NULL)
1931 so->so_rcv.sb_lastrecord = nextrecord;
1933 SBLASTRECORDCHK(&so->so_rcv);
1934 SBLASTMBUFCHK(&so->so_rcv);
1936 * If soreceive() is being done from the socket callback,
1937 * then don't need to generate ACK to peer to update window,
1938 * since ACK will be generated on return to TCP.
1940 if (!(flags & MSG_SOCALLBCK) &&
1941 (pr->pr_flags & PR_WANTRCVD)) {
1942 SOCKBUF_UNLOCK(&so->so_rcv);
1944 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1945 SOCKBUF_LOCK(&so->so_rcv);
1948 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1949 if (orig_resid == uio->uio_resid && orig_resid &&
1950 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1951 SOCKBUF_UNLOCK(&so->so_rcv);
1954 SOCKBUF_UNLOCK(&so->so_rcv);
1959 sbunlock(&so->so_rcv);
1964 * Optimized version of soreceive() for stream (TCP) sockets.
1965 * XXXAO: (MSG_WAITALL | MSG_PEEK) isn't properly handled.
1968 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
1969 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1971 int len = 0, error = 0, flags, oresid;
1973 struct mbuf *m, *n = NULL;
1975 /* We only do stream sockets. */
1976 if (so->so_type != SOCK_STREAM)
1980 if (controlp != NULL)
1983 flags = *flagsp &~ MSG_EOR;
1986 if (flags & MSG_OOB)
1987 return (soreceive_rcvoob(so, uio, flags));
1993 /* Prevent other readers from entering the socket. */
1994 error = sblock(sb, SBLOCKWAIT(flags));
1999 /* Easy one, no space to copyout anything. */
2000 if (uio->uio_resid == 0) {
2004 oresid = uio->uio_resid;
2006 /* We will never ever get anything unless we are or were connected. */
2007 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
2013 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2015 /* Abort if socket has reported problems. */
2019 if (oresid > uio->uio_resid)
2021 error = so->so_error;
2022 if (!(flags & MSG_PEEK))
2027 /* Door is closed. Deliver what is left, if any. */
2028 if (sb->sb_state & SBS_CANTRCVMORE) {
2035 /* Socket buffer is empty and we shall not block. */
2036 if (sb->sb_cc == 0 &&
2037 ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
2042 /* Socket buffer got some data that we shall deliver now. */
2043 if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) &&
2044 ((sb->sb_flags & SS_NBIO) ||
2045 (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
2046 sb->sb_cc >= sb->sb_lowat ||
2047 sb->sb_cc >= uio->uio_resid ||
2048 sb->sb_cc >= sb->sb_hiwat) ) {
2052 /* On MSG_WAITALL we must wait until all data or error arrives. */
2053 if ((flags & MSG_WAITALL) &&
2054 (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_hiwat))
2058 * Wait and block until (more) data comes in.
2059 * NB: Drops the sockbuf lock during wait.
2067 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2068 KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__));
2069 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
2073 uio->uio_td->td_ru.ru_msgrcv++;
2075 /* Fill uio until full or current end of socket buffer is reached. */
2076 len = min(uio->uio_resid, sb->sb_cc);
2078 /* Dequeue as many mbufs as possible. */
2079 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
2083 m_cat(*mp0, sb->sb_mb);
2085 m != NULL && m->m_len <= len;
2088 uio->uio_resid -= m->m_len;
2094 sb->sb_lastrecord = sb->sb_mb;
2095 if (sb->sb_mb == NULL)
2098 /* Copy the remainder. */
2100 KASSERT(sb->sb_mb != NULL,
2101 ("%s: len > 0 && sb->sb_mb empty", __func__));
2103 m = m_copym(sb->sb_mb, 0, len, M_DONTWAIT);
2105 len = 0; /* Don't flush data from sockbuf. */
2107 uio->uio_resid -= len;
2118 /* NB: Must unlock socket buffer as uiomove may sleep. */
2120 error = m_mbuftouio(uio, sb->sb_mb, len);
2125 SBLASTRECORDCHK(sb);
2129 * Remove the delivered data from the socket buffer unless we
2130 * were only peeking.
2132 if (!(flags & MSG_PEEK)) {
2134 sbdrop_locked(sb, len);
2136 /* Notify protocol that we drained some data. */
2137 if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
2138 (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
2139 !(flags & MSG_SOCALLBCK))) {
2142 (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
2148 * For MSG_WAITALL we may have to loop again and wait for
2149 * more data to come in.
2151 if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
2154 SOCKBUF_LOCK_ASSERT(sb);
2155 SBLASTRECORDCHK(sb);
2163 * Optimized version of soreceive() for simple datagram cases from userspace.
2164 * Unlike in the stream case, we're able to drop a datagram if copyout()
2165 * fails, and because we handle datagrams atomically, we don't need to use a
2166 * sleep lock to prevent I/O interlacing.
2169 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
2170 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2172 struct mbuf *m, *m2;
2175 struct protosw *pr = so->so_proto;
2176 struct mbuf *nextrecord;
2180 if (controlp != NULL)
2183 flags = *flagsp &~ MSG_EOR;
2188 * For any complicated cases, fall back to the full
2189 * soreceive_generic().
2191 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
2192 return (soreceive_generic(so, psa, uio, mp0, controlp,
2196 * Enforce restrictions on use.
2198 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
2199 ("soreceive_dgram: wantrcvd"));
2200 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
2201 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
2202 ("soreceive_dgram: SBS_RCVATMARK"));
2203 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
2204 ("soreceive_dgram: P_CONNREQUIRED"));
2207 * Loop blocking while waiting for a datagram.
2209 SOCKBUF_LOCK(&so->so_rcv);
2210 while ((m = so->so_rcv.sb_mb) == NULL) {
2211 KASSERT(so->so_rcv.sb_cc == 0,
2212 ("soreceive_dgram: sb_mb NULL but sb_cc %u",
2215 error = so->so_error;
2217 SOCKBUF_UNLOCK(&so->so_rcv);
2220 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
2221 uio->uio_resid == 0) {
2222 SOCKBUF_UNLOCK(&so->so_rcv);
2225 if ((so->so_state & SS_NBIO) ||
2226 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2227 SOCKBUF_UNLOCK(&so->so_rcv);
2228 return (EWOULDBLOCK);
2230 SBLASTRECORDCHK(&so->so_rcv);
2231 SBLASTMBUFCHK(&so->so_rcv);
2232 error = sbwait(&so->so_rcv);
2234 SOCKBUF_UNLOCK(&so->so_rcv);
2238 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2241 uio->uio_td->td_ru.ru_msgrcv++;
2242 SBLASTRECORDCHK(&so->so_rcv);
2243 SBLASTMBUFCHK(&so->so_rcv);
2244 nextrecord = m->m_nextpkt;
2245 if (nextrecord == NULL) {
2246 KASSERT(so->so_rcv.sb_lastrecord == m,
2247 ("soreceive_dgram: lastrecord != m"));
2250 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
2251 ("soreceive_dgram: m_nextpkt != nextrecord"));
2254 * Pull 'm' and its chain off the front of the packet queue.
2256 so->so_rcv.sb_mb = NULL;
2257 sockbuf_pushsync(&so->so_rcv, nextrecord);
2260 * Walk 'm's chain and free that many bytes from the socket buffer.
2262 for (m2 = m; m2 != NULL; m2 = m2->m_next)
2263 sbfree(&so->so_rcv, m2);
2266 * Do a few last checks before we let go of the lock.
2268 SBLASTRECORDCHK(&so->so_rcv);
2269 SBLASTMBUFCHK(&so->so_rcv);
2270 SOCKBUF_UNLOCK(&so->so_rcv);
2272 if (pr->pr_flags & PR_ADDR) {
2273 KASSERT(m->m_type == MT_SONAME,
2274 ("m->m_type == %d", m->m_type));
2276 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
2281 /* XXXRW: Can this happen? */
2286 * Packet to copyout() is now in 'm' and it is disconnected from the
2289 * Process one or more MT_CONTROL mbufs present before any data mbufs
2290 * in the first mbuf chain on the socket buffer. We call into the
2291 * protocol to perform externalization (or freeing if controlp ==
2294 if (m->m_type == MT_CONTROL) {
2295 struct mbuf *cm = NULL, *cmn;
2296 struct mbuf **cme = &cm;
2302 cme = &(*cme)->m_next;
2304 } while (m != NULL && m->m_type == MT_CONTROL);
2305 while (cm != NULL) {
2308 if (pr->pr_domain->dom_externalize != NULL) {
2309 error = (*pr->pr_domain->dom_externalize)
2311 } else if (controlp != NULL)
2315 if (controlp != NULL) {
2316 while (*controlp != NULL)
2317 controlp = &(*controlp)->m_next;
2322 KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
2324 while (m != NULL && uio->uio_resid > 0) {
2325 len = uio->uio_resid;
2328 error = uiomove(mtod(m, char *), (int)len, uio);
2333 if (len == m->m_len)
2349 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2350 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2354 CURVNET_SET(so->so_vnet);
2355 error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
2362 soshutdown(struct socket *so, int how)
2364 struct protosw *pr = so->so_proto;
2367 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2370 CURVNET_SET(so->so_vnet);
2371 if (pr->pr_usrreqs->pru_flush != NULL) {
2372 (*pr->pr_usrreqs->pru_flush)(so, how);
2376 if (how != SHUT_RD) {
2377 error = (*pr->pr_usrreqs->pru_shutdown)(so);
2386 sorflush(struct socket *so)
2388 struct sockbuf *sb = &so->so_rcv;
2389 struct protosw *pr = so->so_proto;
2395 * In order to avoid calling dom_dispose with the socket buffer mutex
2396 * held, and in order to generally avoid holding the lock for a long
2397 * time, we make a copy of the socket buffer and clear the original
2398 * (except locks, state). The new socket buffer copy won't have
2399 * initialized locks so we can only call routines that won't use or
2400 * assert those locks.
2402 * Dislodge threads currently blocked in receive and wait to acquire
2403 * a lock against other simultaneous readers before clearing the
2404 * socket buffer. Don't let our acquire be interrupted by a signal
2405 * despite any existing socket disposition on interruptable waiting.
2408 (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2411 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2412 * and mutex data unchanged.
2415 bzero(&asb, offsetof(struct sockbuf, sb_startzero));
2416 bcopy(&sb->sb_startzero, &asb.sb_startzero,
2417 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2418 bzero(&sb->sb_startzero,
2419 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2424 * Dispose of special rights and flush the socket buffer. Don't call
2425 * any unsafe routines (that rely on locks being initialized) on asb.
2427 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2428 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
2429 sbrelease_internal(&asb, so);
2433 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2434 * additional variant to handle the case where the option value needs to be
2435 * some kind of integer, but not a specific size. In addition to their use
2436 * here, these functions are also called by the protocol-level pr_ctloutput()
2440 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2445 * If the user gives us more than we wanted, we ignore it, but if we
2446 * don't get the minimum length the caller wants, we return EINVAL.
2447 * On success, sopt->sopt_valsize is set to however much we actually
2450 if ((valsize = sopt->sopt_valsize) < minlen)
2453 sopt->sopt_valsize = valsize = len;
2455 if (sopt->sopt_td != NULL)
2456 return (copyin(sopt->sopt_val, buf, valsize));
2458 bcopy(sopt->sopt_val, buf, valsize);
2463 * Kernel version of setsockopt(2).
2465 * XXX: optlen is size_t, not socklen_t
2468 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2471 struct sockopt sopt;
2473 sopt.sopt_level = level;
2474 sopt.sopt_name = optname;
2475 sopt.sopt_dir = SOPT_SET;
2476 sopt.sopt_val = optval;
2477 sopt.sopt_valsize = optlen;
2478 sopt.sopt_td = NULL;
2479 return (sosetopt(so, &sopt));
2483 sosetopt(struct socket *so, struct sockopt *sopt)
2494 CURVNET_SET(so->so_vnet);
2496 if (sopt->sopt_level != SOL_SOCKET) {
2497 if (so->so_proto->pr_ctloutput != NULL) {
2498 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2502 error = ENOPROTOOPT;
2504 switch (sopt->sopt_name) {
2506 case SO_ACCEPTFILTER:
2507 error = do_setopt_accept_filter(so, sopt);
2513 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2518 so->so_linger = l.l_linger;
2520 so->so_options |= SO_LINGER;
2522 so->so_options &= ~SO_LINGER;
2529 case SO_USELOOPBACK:
2539 error = sooptcopyin(sopt, &optval, sizeof optval,
2545 so->so_options |= sopt->sopt_name;
2547 so->so_options &= ~sopt->sopt_name;
2552 error = sooptcopyin(sopt, &optval, sizeof optval,
2557 if (optval < 0 || optval >= rt_numfibs) {
2561 if (((so->so_proto->pr_domain->dom_family == PF_INET) ||
2562 (so->so_proto->pr_domain->dom_family == PF_INET6) ||
2563 (so->so_proto->pr_domain->dom_family == PF_ROUTE)))
2564 so->so_fibnum = optval;
2569 case SO_USER_COOKIE:
2570 error = sooptcopyin(sopt, &val32, sizeof val32,
2574 so->so_user_cookie = val32;
2581 error = sooptcopyin(sopt, &optval, sizeof optval,
2587 * Values < 1 make no sense for any of these options,
2595 switch (sopt->sopt_name) {
2598 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
2599 &so->so_snd : &so->so_rcv, (u_long)optval,
2600 so, curthread) == 0) {
2604 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
2605 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
2609 * Make sure the low-water is never greater than the
2613 SOCKBUF_LOCK(&so->so_snd);
2614 so->so_snd.sb_lowat =
2615 (optval > so->so_snd.sb_hiwat) ?
2616 so->so_snd.sb_hiwat : optval;
2617 SOCKBUF_UNLOCK(&so->so_snd);
2620 SOCKBUF_LOCK(&so->so_rcv);
2621 so->so_rcv.sb_lowat =
2622 (optval > so->so_rcv.sb_hiwat) ?
2623 so->so_rcv.sb_hiwat : optval;
2624 SOCKBUF_UNLOCK(&so->so_rcv);
2631 #ifdef COMPAT_FREEBSD32
2632 if (SV_CURPROC_FLAG(SV_ILP32)) {
2633 struct timeval32 tv32;
2635 error = sooptcopyin(sopt, &tv32, sizeof tv32,
2637 CP(tv32, tv, tv_sec);
2638 CP(tv32, tv, tv_usec);
2641 error = sooptcopyin(sopt, &tv, sizeof tv,
2646 /* assert(hz > 0); */
2647 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
2648 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
2652 /* assert(tick > 0); */
2653 /* assert(ULONG_MAX - INT_MAX >= 1000000); */
2654 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
2655 if (val > INT_MAX) {
2659 if (val == 0 && tv.tv_usec != 0)
2662 switch (sopt->sopt_name) {
2664 so->so_snd.sb_timeo = val;
2667 so->so_rcv.sb_timeo = val;
2674 error = sooptcopyin(sopt, &extmac, sizeof extmac,
2678 error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2686 error = ENOPROTOOPT;
2689 if (error == 0 && so->so_proto->pr_ctloutput != NULL)
2690 (void)(*so->so_proto->pr_ctloutput)(so, sopt);
2698 * Helper routine for getsockopt.
2701 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2709 * Documented get behavior is that we always return a value, possibly
2710 * truncated to fit in the user's buffer. Traditional behavior is
2711 * that we always tell the user precisely how much we copied, rather
2712 * than something useful like the total amount we had available for
2713 * her. Note that this interface is not idempotent; the entire
2714 * answer must generated ahead of time.
2716 valsize = min(len, sopt->sopt_valsize);
2717 sopt->sopt_valsize = valsize;
2718 if (sopt->sopt_val != NULL) {
2719 if (sopt->sopt_td != NULL)
2720 error = copyout(buf, sopt->sopt_val, valsize);
2722 bcopy(buf, sopt->sopt_val, valsize);
2728 sogetopt(struct socket *so, struct sockopt *sopt)
2737 CURVNET_SET(so->so_vnet);
2739 if (sopt->sopt_level != SOL_SOCKET) {
2740 if (so->so_proto->pr_ctloutput != NULL)
2741 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2743 error = ENOPROTOOPT;
2747 switch (sopt->sopt_name) {
2749 case SO_ACCEPTFILTER:
2750 error = do_getopt_accept_filter(so, sopt);
2755 l.l_onoff = so->so_options & SO_LINGER;
2756 l.l_linger = so->so_linger;
2758 error = sooptcopyout(sopt, &l, sizeof l);
2761 case SO_USELOOPBACK:
2773 optval = so->so_options & sopt->sopt_name;
2775 error = sooptcopyout(sopt, &optval, sizeof optval);
2779 optval = so->so_type;
2783 optval = so->so_proto->pr_protocol;
2788 optval = so->so_error;
2794 optval = so->so_snd.sb_hiwat;
2798 optval = so->so_rcv.sb_hiwat;
2802 optval = so->so_snd.sb_lowat;
2806 optval = so->so_rcv.sb_lowat;
2811 optval = (sopt->sopt_name == SO_SNDTIMEO ?
2812 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2814 tv.tv_sec = optval / hz;
2815 tv.tv_usec = (optval % hz) * tick;
2816 #ifdef COMPAT_FREEBSD32
2817 if (SV_CURPROC_FLAG(SV_ILP32)) {
2818 struct timeval32 tv32;
2820 CP(tv, tv32, tv_sec);
2821 CP(tv, tv32, tv_usec);
2822 error = sooptcopyout(sopt, &tv32, sizeof tv32);
2825 error = sooptcopyout(sopt, &tv, sizeof tv);
2830 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2834 error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
2838 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2846 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2850 error = mac_getsockopt_peerlabel(
2851 sopt->sopt_td->td_ucred, so, &extmac);
2854 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2860 case SO_LISTENQLIMIT:
2861 optval = so->so_qlimit;
2865 optval = so->so_qlen;
2868 case SO_LISTENINCQLEN:
2869 optval = so->so_incqlen;
2873 error = ENOPROTOOPT;
2885 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2887 struct mbuf *m, *m_prev;
2888 int sopt_size = sopt->sopt_valsize;
2890 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2893 if (sopt_size > MLEN) {
2894 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
2895 if ((m->m_flags & M_EXT) == 0) {
2899 m->m_len = min(MCLBYTES, sopt_size);
2901 m->m_len = min(MLEN, sopt_size);
2903 sopt_size -= m->m_len;
2908 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2913 if (sopt_size > MLEN) {
2914 MCLGET(m, sopt->sopt_td != NULL ? M_WAIT :
2916 if ((m->m_flags & M_EXT) == 0) {
2921 m->m_len = min(MCLBYTES, sopt_size);
2923 m->m_len = min(MLEN, sopt_size);
2925 sopt_size -= m->m_len;
2933 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2935 struct mbuf *m0 = m;
2937 if (sopt->sopt_val == NULL)
2939 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2940 if (sopt->sopt_td != NULL) {
2943 error = copyin(sopt->sopt_val, mtod(m, char *),
2950 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2951 sopt->sopt_valsize -= m->m_len;
2952 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2955 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2956 panic("ip6_sooptmcopyin");
2961 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2963 struct mbuf *m0 = m;
2966 if (sopt->sopt_val == NULL)
2968 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2969 if (sopt->sopt_td != NULL) {
2972 error = copyout(mtod(m, char *), sopt->sopt_val,
2979 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2980 sopt->sopt_valsize -= m->m_len;
2981 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2982 valsize += m->m_len;
2986 /* enough soopt buffer should be given from user-land */
2990 sopt->sopt_valsize = valsize;
2995 * sohasoutofband(): protocol notifies socket layer of the arrival of new
2996 * out-of-band data, which will then notify socket consumers.
2999 sohasoutofband(struct socket *so)
3002 if (so->so_sigio != NULL)
3003 pgsigio(&so->so_sigio, SIGURG, 0);
3004 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
3008 sopoll(struct socket *so, int events, struct ucred *active_cred,
3013 * We do not need to set or assert curvnet as long as everyone uses
3016 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
3021 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
3026 SOCKBUF_LOCK(&so->so_snd);
3027 SOCKBUF_LOCK(&so->so_rcv);
3028 if (events & (POLLIN | POLLRDNORM))
3029 if (soreadabledata(so))
3030 revents |= events & (POLLIN | POLLRDNORM);
3032 if (events & (POLLOUT | POLLWRNORM))
3033 if (sowriteable(so))
3034 revents |= events & (POLLOUT | POLLWRNORM);
3036 if (events & (POLLPRI | POLLRDBAND))
3037 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
3038 revents |= events & (POLLPRI | POLLRDBAND);
3040 if ((events & POLLINIGNEOF) == 0) {
3041 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3042 revents |= events & (POLLIN | POLLRDNORM);
3043 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
3049 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
3050 selrecord(td, &so->so_rcv.sb_sel);
3051 so->so_rcv.sb_flags |= SB_SEL;
3054 if (events & (POLLOUT | POLLWRNORM)) {
3055 selrecord(td, &so->so_snd.sb_sel);
3056 so->so_snd.sb_flags |= SB_SEL;
3060 SOCKBUF_UNLOCK(&so->so_rcv);
3061 SOCKBUF_UNLOCK(&so->so_snd);
3066 soo_kqfilter(struct file *fp, struct knote *kn)
3068 struct socket *so = kn->kn_fp->f_data;
3071 switch (kn->kn_filter) {
3073 if (so->so_options & SO_ACCEPTCONN)
3074 kn->kn_fop = &solisten_filtops;
3076 kn->kn_fop = &soread_filtops;
3080 kn->kn_fop = &sowrite_filtops;
3088 knlist_add(&sb->sb_sel.si_note, kn, 1);
3089 sb->sb_flags |= SB_KNOTE;
3095 * Some routines that return EOPNOTSUPP for entry points that are not
3096 * supported by a protocol. Fill in as needed.
3099 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
3106 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
3113 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3120 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3127 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
3134 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
3135 struct ifnet *ifp, struct thread *td)
3142 pru_disconnect_notsupp(struct socket *so)
3149 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
3156 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
3163 pru_rcvd_notsupp(struct socket *so, int flags)
3170 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
3177 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
3178 struct sockaddr *addr, struct mbuf *control, struct thread *td)
3185 * This isn't really a ``null'' operation, but it's the default one and
3186 * doesn't do anything destructive.
3189 pru_sense_null(struct socket *so, struct stat *sb)
3192 sb->st_blksize = so->so_snd.sb_hiwat;
3197 pru_shutdown_notsupp(struct socket *so)
3204 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
3211 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
3212 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
3219 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
3220 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
3227 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
3235 filt_sordetach(struct knote *kn)
3237 struct socket *so = kn->kn_fp->f_data;
3239 SOCKBUF_LOCK(&so->so_rcv);
3240 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
3241 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
3242 so->so_rcv.sb_flags &= ~SB_KNOTE;
3243 SOCKBUF_UNLOCK(&so->so_rcv);
3248 filt_soread(struct knote *kn, long hint)
3252 so = kn->kn_fp->f_data;
3253 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
3255 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
3256 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3257 kn->kn_flags |= EV_EOF;
3258 kn->kn_fflags = so->so_error;
3260 } else if (so->so_error) /* temporary udp error */
3262 else if (kn->kn_sfflags & NOTE_LOWAT)
3263 return (kn->kn_data >= kn->kn_sdata);
3265 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
3269 filt_sowdetach(struct knote *kn)
3271 struct socket *so = kn->kn_fp->f_data;
3273 SOCKBUF_LOCK(&so->so_snd);
3274 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
3275 if (knlist_empty(&so->so_snd.sb_sel.si_note))
3276 so->so_snd.sb_flags &= ~SB_KNOTE;
3277 SOCKBUF_UNLOCK(&so->so_snd);
3282 filt_sowrite(struct knote *kn, long hint)
3286 so = kn->kn_fp->f_data;
3287 SOCKBUF_LOCK_ASSERT(&so->so_snd);
3288 kn->kn_data = sbspace(&so->so_snd);
3289 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
3290 kn->kn_flags |= EV_EOF;
3291 kn->kn_fflags = so->so_error;
3293 } else if (so->so_error) /* temporary udp error */
3295 else if (((so->so_state & SS_ISCONNECTED) == 0) &&
3296 (so->so_proto->pr_flags & PR_CONNREQUIRED))
3298 else if (kn->kn_sfflags & NOTE_LOWAT)
3299 return (kn->kn_data >= kn->kn_sdata);
3301 return (kn->kn_data >= so->so_snd.sb_lowat);
3306 filt_solisten(struct knote *kn, long hint)
3308 struct socket *so = kn->kn_fp->f_data;
3310 kn->kn_data = so->so_qlen;
3311 return (! TAILQ_EMPTY(&so->so_comp));
3315 socheckuid(struct socket *so, uid_t uid)
3320 if (so->so_cred->cr_uid != uid)
3326 * These functions are used by protocols to notify the socket layer (and its
3327 * consumers) of state changes in the sockets driven by protocol-side events.
3331 * Procedures to manipulate state flags of socket and do appropriate wakeups.
3333 * Normal sequence from the active (originating) side is that
3334 * soisconnecting() is called during processing of connect() call, resulting
3335 * in an eventual call to soisconnected() if/when the connection is
3336 * established. When the connection is torn down soisdisconnecting() is
3337 * called during processing of disconnect() call, and soisdisconnected() is
3338 * called when the connection to the peer is totally severed. The semantics
3339 * of these routines are such that connectionless protocols can call
3340 * soisconnected() and soisdisconnected() only, bypassing the in-progress
3341 * calls when setting up a ``connection'' takes no time.
3343 * From the passive side, a socket is created with two queues of sockets:
3344 * so_incomp for connections in progress and so_comp for connections already
3345 * made and awaiting user acceptance. As a protocol is preparing incoming
3346 * connections, it creates a socket structure queued on so_incomp by calling
3347 * sonewconn(). When the connection is established, soisconnected() is
3348 * called, and transfers the socket structure to so_comp, making it available
3351 * If a socket is closed with sockets on either so_incomp or so_comp, these
3352 * sockets are dropped.
3354 * If higher-level protocols are implemented in the kernel, the wakeups done
3355 * here will sometimes cause software-interrupt process scheduling.
3358 soisconnecting(struct socket *so)
3362 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3363 so->so_state |= SS_ISCONNECTING;
3368 soisconnected(struct socket *so)
3370 struct socket *head;
3376 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3377 so->so_state |= SS_ISCONNECTED;
3379 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
3380 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3382 TAILQ_REMOVE(&head->so_incomp, so, so_list);
3384 so->so_qstate &= ~SQ_INCOMP;
3385 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
3387 so->so_qstate |= SQ_COMP;
3390 wakeup_one(&head->so_timeo);
3393 soupcall_set(so, SO_RCV,
3394 head->so_accf->so_accept_filter->accf_callback,
3395 head->so_accf->so_accept_filter_arg);
3396 so->so_options &= ~SO_ACCEPTFILTER;
3397 ret = head->so_accf->so_accept_filter->accf_callback(so,
3398 head->so_accf->so_accept_filter_arg, M_DONTWAIT);
3399 if (ret == SU_ISCONNECTED)
3400 soupcall_clear(so, SO_RCV);
3402 if (ret == SU_ISCONNECTED)
3409 wakeup(&so->so_timeo);
3415 soisdisconnecting(struct socket *so)
3419 * Note: This code assumes that SOCK_LOCK(so) and
3420 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3422 SOCKBUF_LOCK(&so->so_rcv);
3423 so->so_state &= ~SS_ISCONNECTING;
3424 so->so_state |= SS_ISDISCONNECTING;
3425 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3426 sorwakeup_locked(so);
3427 SOCKBUF_LOCK(&so->so_snd);
3428 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3429 sowwakeup_locked(so);
3430 wakeup(&so->so_timeo);
3434 soisdisconnected(struct socket *so)
3438 * Note: This code assumes that SOCK_LOCK(so) and
3439 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3441 SOCKBUF_LOCK(&so->so_rcv);
3442 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3443 so->so_state |= SS_ISDISCONNECTED;
3444 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3445 sorwakeup_locked(so);
3446 SOCKBUF_LOCK(&so->so_snd);
3447 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3448 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
3449 sowwakeup_locked(so);
3450 wakeup(&so->so_timeo);
3454 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3457 sodupsockaddr(const struct sockaddr *sa, int mflags)
3459 struct sockaddr *sa2;
3461 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3463 bcopy(sa, sa2, sa->sa_len);
3468 * Register per-socket buffer upcalls.
3471 soupcall_set(struct socket *so, int which,
3472 int (*func)(struct socket *, void *, int), void *arg)
3484 panic("soupcall_set: bad which");
3486 SOCKBUF_LOCK_ASSERT(sb);
3488 /* XXX: accf_http actually wants to do this on purpose. */
3489 KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall"));
3491 sb->sb_upcall = func;
3492 sb->sb_upcallarg = arg;
3493 sb->sb_flags |= SB_UPCALL;
3497 soupcall_clear(struct socket *so, int which)
3509 panic("soupcall_clear: bad which");
3511 SOCKBUF_LOCK_ASSERT(sb);
3512 KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear"));
3513 sb->sb_upcall = NULL;
3514 sb->sb_upcallarg = NULL;
3515 sb->sb_flags &= ~SB_UPCALL;
3519 * Create an external-format (``xsocket'') structure using the information in
3520 * the kernel-format socket structure pointed to by so. This is done to
3521 * reduce the spew of irrelevant information over this interface, to isolate
3522 * user code from changes in the kernel structure, and potentially to provide
3523 * information-hiding if we decide that some of this information should be
3524 * hidden from users.
3527 sotoxsocket(struct socket *so, struct xsocket *xso)
3530 xso->xso_len = sizeof *xso;
3532 xso->so_type = so->so_type;
3533 xso->so_options = so->so_options;
3534 xso->so_linger = so->so_linger;
3535 xso->so_state = so->so_state;
3536 xso->so_pcb = so->so_pcb;
3537 xso->xso_protocol = so->so_proto->pr_protocol;
3538 xso->xso_family = so->so_proto->pr_domain->dom_family;
3539 xso->so_qlen = so->so_qlen;
3540 xso->so_incqlen = so->so_incqlen;
3541 xso->so_qlimit = so->so_qlimit;
3542 xso->so_timeo = so->so_timeo;
3543 xso->so_error = so->so_error;
3544 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
3545 xso->so_oobmark = so->so_oobmark;
3546 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
3547 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
3548 xso->so_uid = so->so_cred->cr_uid;
3553 * Socket accessor functions to provide external consumers with
3554 * a safe interface to socket state
3559 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg)
3562 TAILQ_FOREACH(so, &so->so_comp, so_list)
3567 so_sockbuf_rcv(struct socket *so)
3570 return (&so->so_rcv);
3574 so_sockbuf_snd(struct socket *so)
3577 return (&so->so_snd);
3581 so_state_get(const struct socket *so)
3584 return (so->so_state);
3588 so_state_set(struct socket *so, int val)
3595 so_options_get(const struct socket *so)
3598 return (so->so_options);
3602 so_options_set(struct socket *so, int val)
3605 so->so_options = val;
3609 so_error_get(const struct socket *so)
3612 return (so->so_error);
3616 so_error_set(struct socket *so, int val)
3623 so_linger_get(const struct socket *so)
3626 return (so->so_linger);
3630 so_linger_set(struct socket *so, int val)
3633 so->so_linger = val;
3637 so_protosw_get(const struct socket *so)
3640 return (so->so_proto);
3644 so_protosw_set(struct socket *so, struct protosw *val)
3651 so_sorwakeup(struct socket *so)
3658 so_sowwakeup(struct socket *so)
3665 so_sorwakeup_locked(struct socket *so)
3668 sorwakeup_locked(so);
3672 so_sowwakeup_locked(struct socket *so)
3675 sowwakeup_locked(so);
3679 so_lock(struct socket *so)
3685 so_unlock(struct socket *so)