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>
139 #include <netinet/in.h>
141 #include <net/vnet.h>
143 #include <security/mac/mac_framework.h>
147 #ifdef COMPAT_FREEBSD32
148 #include <sys/mount.h>
149 #include <sys/sysent.h>
150 #include <compat/freebsd32/freebsd32.h>
153 static int soreceive_rcvoob(struct socket *so, struct uio *uio,
156 static void filt_sordetach(struct knote *kn);
157 static int filt_soread(struct knote *kn, long hint);
158 static void filt_sowdetach(struct knote *kn);
159 static int filt_sowrite(struct knote *kn, long hint);
160 static int filt_solisten(struct knote *kn, long hint);
162 static struct filterops solisten_filtops = {
164 .f_detach = filt_sordetach,
165 .f_event = filt_solisten,
167 static struct filterops soread_filtops = {
169 .f_detach = filt_sordetach,
170 .f_event = filt_soread,
172 static struct filterops sowrite_filtops = {
174 .f_detach = filt_sowdetach,
175 .f_event = filt_sowrite,
178 so_gen_t so_gencnt; /* generation count for sockets */
180 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
181 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
183 #define VNET_SO_ASSERT(so) \
184 VNET_ASSERT(curvnet != NULL, \
185 ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so)));
188 * Limit on the number of connections in the listen queue waiting
191 static int somaxconn = SOMAXCONN;
194 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
200 error = sysctl_handle_int(oidp, &val, 0, req);
201 if (error || !req->newptr )
204 if (val < 1 || val > USHRT_MAX)
210 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLTYPE_UINT | CTLFLAG_RW,
211 0, sizeof(int), sysctl_somaxconn, "I",
212 "Maximum listen socket pending connection accept queue size");
214 static int numopensockets;
215 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
216 &numopensockets, 0, "Number of open sockets");
218 #ifdef ZERO_COPY_SOCKETS
219 /* These aren't static because they're used in other files. */
220 int so_zero_copy_send = 1;
221 int so_zero_copy_receive = 1;
222 SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0,
223 "Zero copy controls");
224 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW,
225 &so_zero_copy_receive, 0, "Enable zero copy receive");
226 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW,
227 &so_zero_copy_send, 0, "Enable zero copy send");
228 #endif /* ZERO_COPY_SOCKETS */
231 * accept_mtx locks down per-socket fields relating to accept queues. See
232 * socketvar.h for an annotation of the protected fields of struct socket.
234 struct mtx accept_mtx;
235 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
238 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
241 static struct mtx so_global_mtx;
242 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
245 * General IPC sysctl name space, used by sockets and a variety of other IPC
248 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
251 * Initialize the socket subsystem and set up the socket
254 uma_zone_t socket_zone;
258 socket_zone_change(void *tag)
261 uma_zone_set_max(socket_zone, maxsockets);
265 socket_init(void *tag)
268 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
269 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
270 uma_zone_set_max(socket_zone, maxsockets);
271 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
272 EVENTHANDLER_PRI_FIRST);
274 SYSINIT(socket, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, socket_init, NULL);
277 * Initialise maxsockets. This SYSINIT must be run after
281 init_maxsockets(void *ignored)
284 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
285 maxsockets = imax(maxsockets, maxfiles);
287 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
290 * Sysctl to get and set the maximum global sockets limit. Notify protocols
291 * of the change so that they can update their dependent limits as required.
294 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
296 int error, newmaxsockets;
298 newmaxsockets = maxsockets;
299 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
300 if (error == 0 && req->newptr) {
301 if (newmaxsockets > maxsockets &&
302 newmaxsockets <= maxfiles) {
303 maxsockets = newmaxsockets;
304 EVENTHANDLER_INVOKE(maxsockets_change);
310 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
311 &maxsockets, 0, sysctl_maxsockets, "IU",
312 "Maximum number of sockets avaliable");
315 * Socket operation routines. These routines are called by the routines in
316 * sys_socket.c or from a system process, and implement the semantics of
317 * socket operations by switching out to the protocol specific routines.
321 * Get a socket structure from our zone, and initialize it. Note that it
322 * would probably be better to allocate socket and PCB at the same time, but
323 * I'm not convinced that all the protocols can be easily modified to do
326 * soalloc() returns a socket with a ref count of 0.
328 static struct socket *
329 soalloc(struct vnet *vnet)
333 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
337 if (mac_socket_init(so, M_NOWAIT) != 0) {
338 uma_zfree(socket_zone, so);
342 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
343 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
344 sx_init(&so->so_snd.sb_sx, "so_snd_sx");
345 sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
346 TAILQ_INIT(&so->so_aiojobq);
347 mtx_lock(&so_global_mtx);
348 so->so_gencnt = ++so_gencnt;
351 VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p",
352 __func__, __LINE__, so));
353 vnet->vnet_sockcnt++;
356 mtx_unlock(&so_global_mtx);
361 * Free the storage associated with a socket at the socket layer, tear down
362 * locks, labels, etc. All protocol state is assumed already to have been
363 * torn down (and possibly never set up) by the caller.
366 sodealloc(struct socket *so)
369 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
370 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
372 mtx_lock(&so_global_mtx);
373 so->so_gencnt = ++so_gencnt;
374 --numopensockets; /* Could be below, but faster here. */
376 VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p",
377 __func__, __LINE__, so));
378 so->so_vnet->vnet_sockcnt--;
380 mtx_unlock(&so_global_mtx);
381 if (so->so_rcv.sb_hiwat)
382 (void)chgsbsize(so->so_cred->cr_uidinfo,
383 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
384 if (so->so_snd.sb_hiwat)
385 (void)chgsbsize(so->so_cred->cr_uidinfo,
386 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
388 /* remove acccept filter if one is present. */
389 if (so->so_accf != NULL)
390 do_setopt_accept_filter(so, NULL);
393 mac_socket_destroy(so);
396 sx_destroy(&so->so_snd.sb_sx);
397 sx_destroy(&so->so_rcv.sb_sx);
398 SOCKBUF_LOCK_DESTROY(&so->so_snd);
399 SOCKBUF_LOCK_DESTROY(&so->so_rcv);
400 uma_zfree(socket_zone, so);
404 * socreate returns a socket with a ref count of 1. The socket should be
405 * closed with soclose().
408 socreate(int dom, struct socket **aso, int type, int proto,
409 struct ucred *cred, struct thread *td)
416 prp = pffindproto(dom, proto, type);
418 prp = pffindtype(dom, type);
420 if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
421 prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
422 return (EPROTONOSUPPORT);
424 if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
425 return (EPROTONOSUPPORT);
427 if (prp->pr_type != type)
429 so = soalloc(CRED_TO_VNET(cred));
433 TAILQ_INIT(&so->so_incomp);
434 TAILQ_INIT(&so->so_comp);
436 so->so_cred = crhold(cred);
437 if ((prp->pr_domain->dom_family == PF_INET) ||
438 (prp->pr_domain->dom_family == PF_INET6) ||
439 (prp->pr_domain->dom_family == PF_ROUTE))
440 so->so_fibnum = td->td_proc->p_fibnum;
445 mac_socket_create(cred, so);
447 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
448 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
451 * Auto-sizing of socket buffers is managed by the protocols and
452 * the appropriate flags must be set in the pru_attach function.
454 CURVNET_SET(so->so_vnet);
455 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
458 KASSERT(so->so_count == 1, ("socreate: so_count %d",
469 static int regression_sonewconn_earlytest = 1;
470 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
471 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
475 * When an attempt at a new connection is noted on a socket which accepts
476 * connections, sonewconn is called. If the connection is possible (subject
477 * to space constraints, etc.) then we allocate a new structure, propoerly
478 * linked into the data structure of the original socket, and return this.
479 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
481 * Note: the ref count on the socket is 0 on return.
484 sonewconn(struct socket *head, int connstatus)
486 static struct timeval lastover;
487 static struct timeval overinterval = { 60, 0 };
488 static int overcount;
494 over = (head->so_qlen > 3 * head->so_qlimit / 2);
497 if (regression_sonewconn_earlytest && over) {
503 if (ratecheck(&lastover, &overinterval)) {
504 log(LOG_DEBUG, "%s: pcb %p: Listen queue overflow: "
505 "%i already in queue awaiting acceptance "
506 "(%d occurrences)\n",
507 __func__, head->so_pcb, head->so_qlen, overcount);
514 VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
515 __func__, __LINE__, head));
516 so = soalloc(head->so_vnet);
518 log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: "
519 "limit reached or out of memory\n",
520 __func__, head->so_pcb);
523 if ((head->so_options & SO_ACCEPTFILTER) != 0)
526 so->so_type = head->so_type;
527 so->so_options = head->so_options &~ SO_ACCEPTCONN;
528 so->so_linger = head->so_linger;
529 so->so_state = head->so_state | SS_NOFDREF;
530 so->so_fibnum = head->so_fibnum;
531 so->so_proto = head->so_proto;
532 so->so_cred = crhold(head->so_cred);
534 mac_socket_newconn(head, so);
536 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
537 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
538 VNET_SO_ASSERT(head);
539 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
541 log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n",
542 __func__, head->so_pcb);
545 if ((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
547 log(LOG_DEBUG, "%s: pcb %p: pru_attach() failed\n",
548 __func__, head->so_pcb);
551 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
552 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
553 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
554 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
555 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
556 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
557 so->so_state |= connstatus;
560 * The accept socket may be tearing down but we just
561 * won a race on the ACCEPT_LOCK.
562 * However, if sctp_peeloff() is called on a 1-to-many
563 * style socket, the SO_ACCEPTCONN doesn't need to be set.
565 if (!(head->so_options & SO_ACCEPTCONN) &&
566 ((head->so_proto->pr_protocol != IPPROTO_SCTP) ||
567 (head->so_type != SOCK_SEQPACKET))) {
570 sofree(so); /* NB: returns ACCEPT_UNLOCK'ed. */
574 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
575 so->so_qstate |= SQ_COMP;
579 * Keep removing sockets from the head until there's room for
580 * us to insert on the tail. In pre-locking revisions, this
581 * was a simple if(), but as we could be racing with other
582 * threads and soabort() requires dropping locks, we must
583 * loop waiting for the condition to be true.
585 while (head->so_incqlen > head->so_qlimit) {
587 sp = TAILQ_FIRST(&head->so_incomp);
588 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
590 sp->so_qstate &= ~SQ_INCOMP;
596 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
597 so->so_qstate |= SQ_INCOMP;
603 wakeup_one(&head->so_timeo);
609 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
613 CURVNET_SET(so->so_vnet);
614 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
620 * solisten() transitions a socket from a non-listening state to a listening
621 * state, but can also be used to update the listen queue depth on an
622 * existing listen socket. The protocol will call back into the sockets
623 * layer using solisten_proto_check() and solisten_proto() to check and set
624 * socket-layer listen state. Call backs are used so that the protocol can
625 * acquire both protocol and socket layer locks in whatever order is required
628 * Protocol implementors are advised to hold the socket lock across the
629 * socket-layer test and set to avoid races at the socket layer.
632 solisten(struct socket *so, int backlog, struct thread *td)
636 CURVNET_SET(so->so_vnet);
637 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td);
643 solisten_proto_check(struct socket *so)
646 SOCK_LOCK_ASSERT(so);
648 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
655 solisten_proto(struct socket *so, int backlog)
658 SOCK_LOCK_ASSERT(so);
660 if (backlog < 0 || backlog > somaxconn)
662 so->so_qlimit = backlog;
663 so->so_options |= SO_ACCEPTCONN;
667 * Evaluate the reference count and named references on a socket; if no
668 * references remain, free it. This should be called whenever a reference is
669 * released, such as in sorele(), but also when named reference flags are
670 * cleared in socket or protocol code.
672 * sofree() will free the socket if:
674 * - There are no outstanding file descriptor references or related consumers
677 * - The socket has been closed by user space, if ever open (SS_NOFDREF).
679 * - The protocol does not have an outstanding strong reference on the socket
682 * - The socket is not in a completed connection queue, so a process has been
683 * notified that it is present. If it is removed, the user process may
684 * block in accept() despite select() saying the socket was ready.
687 sofree(struct socket *so)
689 struct protosw *pr = so->so_proto;
692 ACCEPT_LOCK_ASSERT();
693 SOCK_LOCK_ASSERT(so);
695 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
696 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
704 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
705 (so->so_qstate & SQ_INCOMP) != 0,
706 ("sofree: so_head != NULL, but neither SQ_COMP nor "
708 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
709 (so->so_qstate & SQ_INCOMP) == 0,
710 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
711 TAILQ_REMOVE(&head->so_incomp, so, so_list);
713 so->so_qstate &= ~SQ_INCOMP;
716 KASSERT((so->so_qstate & SQ_COMP) == 0 &&
717 (so->so_qstate & SQ_INCOMP) == 0,
718 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
719 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
720 if (so->so_options & SO_ACCEPTCONN) {
721 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
722 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_incomp populated"));
728 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
729 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
730 if (pr->pr_usrreqs->pru_detach != NULL)
731 (*pr->pr_usrreqs->pru_detach)(so);
734 * From this point on, we assume that no other references to this
735 * socket exist anywhere else in the stack. Therefore, no locks need
736 * to be acquired or held.
738 * We used to do a lot of socket buffer and socket locking here, as
739 * well as invoke sorflush() and perform wakeups. The direct call to
740 * dom_dispose() and sbrelease_internal() are an inlining of what was
741 * necessary from sorflush().
743 * Notice that the socket buffer and kqueue state are torn down
744 * before calling pru_detach. This means that protocols shold not
745 * assume they can perform socket wakeups, etc, in their detach code.
747 sbdestroy(&so->so_snd, so);
748 sbdestroy(&so->so_rcv, so);
749 seldrain(&so->so_snd.sb_sel);
750 seldrain(&so->so_rcv.sb_sel);
751 knlist_destroy(&so->so_rcv.sb_sel.si_note);
752 knlist_destroy(&so->so_snd.sb_sel.si_note);
757 * Close a socket on last file table reference removal. Initiate disconnect
758 * if connected. Free socket when disconnect complete.
760 * This function will sorele() the socket. Note that soclose() may be called
761 * prior to the ref count reaching zero. The actual socket structure will
762 * not be freed until the ref count reaches zero.
765 soclose(struct socket *so)
769 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
771 CURVNET_SET(so->so_vnet);
772 funsetown(&so->so_sigio);
773 if (so->so_state & SS_ISCONNECTED) {
774 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
775 error = sodisconnect(so);
777 if (error == ENOTCONN)
782 if (so->so_options & SO_LINGER) {
783 if ((so->so_state & SS_ISDISCONNECTING) &&
784 (so->so_state & SS_NBIO))
786 while (so->so_state & SS_ISCONNECTED) {
787 error = tsleep(&so->so_timeo,
788 PSOCK | PCATCH, "soclos", so->so_linger * hz);
796 if (so->so_proto->pr_usrreqs->pru_close != NULL)
797 (*so->so_proto->pr_usrreqs->pru_close)(so);
799 if (so->so_options & SO_ACCEPTCONN) {
802 * Prevent new additions to the accept queues due
803 * to ACCEPT_LOCK races while we are draining them.
805 so->so_options &= ~SO_ACCEPTCONN;
806 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
807 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
809 sp->so_qstate &= ~SQ_INCOMP;
815 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
816 TAILQ_REMOVE(&so->so_comp, sp, so_list);
818 sp->so_qstate &= ~SQ_COMP;
824 KASSERT((TAILQ_EMPTY(&so->so_comp)),
825 ("%s: so_comp populated", __func__));
826 KASSERT((TAILQ_EMPTY(&so->so_incomp)),
827 ("%s: so_incomp populated", __func__));
830 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
831 so->so_state |= SS_NOFDREF;
832 sorele(so); /* NB: Returns with ACCEPT_UNLOCK(). */
838 * soabort() is used to abruptly tear down a connection, such as when a
839 * resource limit is reached (listen queue depth exceeded), or if a listen
840 * socket is closed while there are sockets waiting to be accepted.
842 * This interface is tricky, because it is called on an unreferenced socket,
843 * and must be called only by a thread that has actually removed the socket
844 * from the listen queue it was on, or races with other threads are risked.
846 * This interface will call into the protocol code, so must not be called
847 * with any socket locks held. Protocols do call it while holding their own
848 * recursible protocol mutexes, but this is something that should be subject
849 * to review in the future.
852 soabort(struct socket *so)
856 * In as much as is possible, assert that no references to this
857 * socket are held. This is not quite the same as asserting that the
858 * current thread is responsible for arranging for no references, but
859 * is as close as we can get for now.
861 KASSERT(so->so_count == 0, ("soabort: so_count"));
862 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
863 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
864 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
865 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
868 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
869 (*so->so_proto->pr_usrreqs->pru_abort)(so);
876 soaccept(struct socket *so, struct sockaddr **nam)
881 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
882 so->so_state &= ~SS_NOFDREF;
885 CURVNET_SET(so->so_vnet);
886 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
892 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
896 if (so->so_options & SO_ACCEPTCONN)
899 CURVNET_SET(so->so_vnet);
901 * If protocol is connection-based, can only connect once.
902 * Otherwise, if connected, try to disconnect first. This allows
903 * user to disconnect by connecting to, e.g., a null address.
905 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
906 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
907 (error = sodisconnect(so)))) {
911 * Prevent accumulated error from previous connection from
915 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
923 soconnect2(struct socket *so1, struct socket *so2)
927 CURVNET_SET(so1->so_vnet);
928 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
934 sodisconnect(struct socket *so)
938 if ((so->so_state & SS_ISCONNECTED) == 0)
940 if (so->so_state & SS_ISDISCONNECTING)
943 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
947 #ifdef ZERO_COPY_SOCKETS
948 struct so_zerocopy_stats{
953 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
956 * sosend_copyin() is only used if zero copy sockets are enabled. Otherwise
957 * sosend_dgram() and sosend_generic() use m_uiotombuf().
959 * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or
960 * all of the data referenced by the uio. If desired, it uses zero-copy.
961 * *space will be updated to reflect data copied in.
963 * NB: If atomic I/O is requested, the caller must already have checked that
964 * space can hold resid bytes.
966 * NB: In the event of an error, the caller may need to free the partial
967 * chain pointed to by *mpp. The contents of both *uio and *space may be
968 * modified even in the case of an error.
971 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space,
974 struct mbuf *m, **mp, *top;
983 resid = uio->uio_resid;
987 if (resid >= MINCLSIZE) {
989 m = m_gethdr(M_WAITOK, MT_DATA);
991 m->m_pkthdr.rcvif = NULL;
993 m = m_get(M_WAITOK, MT_DATA);
994 if (so_zero_copy_send &&
995 resid >= PAGE_SIZE &&
996 *space >= PAGE_SIZE &&
997 uio->uio_iov->iov_len >= PAGE_SIZE) {
998 so_zerocp_stats.size_ok++;
999 so_zerocp_stats.align_ok++;
1000 cow_send = socow_setup(m, uio);
1004 m_clget(m, M_WAITOK);
1005 len = min(min(MCLBYTES, resid), *space);
1009 m = m_gethdr(M_WAIT, MT_DATA);
1010 m->m_pkthdr.len = 0;
1011 m->m_pkthdr.rcvif = NULL;
1013 len = min(min(MHLEN, resid), *space);
1015 * For datagram protocols, leave room
1016 * for protocol headers in first mbuf.
1018 if (atomic && m && len < MHLEN)
1021 m = m_get(M_WAIT, MT_DATA);
1022 len = min(min(MLEN, resid), *space);
1034 error = uiomove(mtod(m, void *), (int)len, uio);
1035 resid = uio->uio_resid;
1038 top->m_pkthdr.len += len;
1043 if (flags & MSG_EOR)
1044 top->m_flags |= M_EOR;
1047 } while (*space > 0 && atomic);
1052 #endif /* ZERO_COPY_SOCKETS */
1054 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
1057 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
1058 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1062 int clen = 0, error, dontroute;
1063 #ifdef ZERO_COPY_SOCKETS
1064 int atomic = sosendallatonce(so) || top;
1067 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
1068 KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
1069 ("sodgram_send: !PR_ATOMIC"));
1072 resid = uio->uio_resid;
1074 resid = top->m_pkthdr.len;
1076 * In theory resid should be unsigned. However, space must be
1077 * signed, as it might be less than 0 if we over-committed, and we
1078 * must use a signed comparison of space and resid. On the other
1079 * hand, a negative resid causes us to loop sending 0-length
1080 * segments to the protocol.
1088 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
1090 td->td_ru.ru_msgsnd++;
1091 if (control != NULL)
1092 clen = control->m_len;
1094 SOCKBUF_LOCK(&so->so_snd);
1095 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1096 SOCKBUF_UNLOCK(&so->so_snd);
1101 error = so->so_error;
1103 SOCKBUF_UNLOCK(&so->so_snd);
1106 if ((so->so_state & SS_ISCONNECTED) == 0) {
1108 * `sendto' and `sendmsg' is allowed on a connection-based
1109 * socket if it supports implied connect. Return ENOTCONN if
1110 * not connected and no address is supplied.
1112 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1113 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1114 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1115 !(resid == 0 && clen != 0)) {
1116 SOCKBUF_UNLOCK(&so->so_snd);
1120 } else if (addr == NULL) {
1121 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1124 error = EDESTADDRREQ;
1125 SOCKBUF_UNLOCK(&so->so_snd);
1131 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
1132 * problem and need fixing.
1134 space = sbspace(&so->so_snd);
1135 if (flags & MSG_OOB)
1138 SOCKBUF_UNLOCK(&so->so_snd);
1139 if (resid > space) {
1145 if (flags & MSG_EOR)
1146 top->m_flags |= M_EOR;
1148 #ifdef ZERO_COPY_SOCKETS
1149 error = sosend_copyin(uio, &top, atomic, &space, flags);
1154 * Copy the data from userland into a mbuf chain.
1155 * If no data is to be copied in, a single empty mbuf
1158 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1159 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1161 error = EFAULT; /* only possible error */
1164 space -= resid - uio->uio_resid;
1166 resid = uio->uio_resid;
1168 KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1170 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1175 so->so_options |= SO_DONTROUTE;
1179 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1180 * of date. We could have recieved a reset packet in an interrupt or
1181 * maybe we slept while doing page faults in uiomove() etc. We could
1182 * probably recheck again inside the locking protection here, but
1183 * there are probably other places that this also happens. We must
1187 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1188 (flags & MSG_OOB) ? PRUS_OOB :
1190 * If the user set MSG_EOF, the protocol understands this flag and
1191 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1193 ((flags & MSG_EOF) &&
1194 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1197 /* If there is more to send set PRUS_MORETOCOME */
1198 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1199 top, addr, control, td);
1202 so->so_options &= ~SO_DONTROUTE;
1211 if (control != NULL)
1217 * Send on a socket. If send must go all at once and message is larger than
1218 * send buffering, then hard error. Lock against other senders. If must go
1219 * all at once and not enough room now, then inform user that this would
1220 * block and do nothing. Otherwise, if nonblocking, send as much as
1221 * possible. The data to be sent is described by "uio" if nonzero, otherwise
1222 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1223 * in mbuf chain must be small enough to send all at once.
1225 * Returns nonzero on error, timeout or signal; callers must check for short
1226 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1230 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1231 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1235 int clen = 0, error, dontroute;
1236 int atomic = sosendallatonce(so) || top;
1239 resid = uio->uio_resid;
1241 resid = top->m_pkthdr.len;
1243 * In theory resid should be unsigned. However, space must be
1244 * signed, as it might be less than 0 if we over-committed, and we
1245 * must use a signed comparison of space and resid. On the other
1246 * hand, a negative resid causes us to loop sending 0-length
1247 * segments to the protocol.
1249 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1250 * type sockets since that's an error.
1252 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1258 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1259 (so->so_proto->pr_flags & PR_ATOMIC);
1261 td->td_ru.ru_msgsnd++;
1262 if (control != NULL)
1263 clen = control->m_len;
1265 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1271 SOCKBUF_LOCK(&so->so_snd);
1272 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1273 SOCKBUF_UNLOCK(&so->so_snd);
1278 error = so->so_error;
1280 SOCKBUF_UNLOCK(&so->so_snd);
1283 if ((so->so_state & SS_ISCONNECTED) == 0) {
1285 * `sendto' and `sendmsg' is allowed on a connection-
1286 * based socket if it supports implied connect.
1287 * Return ENOTCONN if not connected and no address is
1290 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1291 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1292 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1293 !(resid == 0 && clen != 0)) {
1294 SOCKBUF_UNLOCK(&so->so_snd);
1298 } else if (addr == NULL) {
1299 SOCKBUF_UNLOCK(&so->so_snd);
1300 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1303 error = EDESTADDRREQ;
1307 space = sbspace(&so->so_snd);
1308 if (flags & MSG_OOB)
1310 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1311 clen > so->so_snd.sb_hiwat) {
1312 SOCKBUF_UNLOCK(&so->so_snd);
1316 if (space < resid + clen &&
1317 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1318 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
1319 SOCKBUF_UNLOCK(&so->so_snd);
1320 error = EWOULDBLOCK;
1323 error = sbwait(&so->so_snd);
1324 SOCKBUF_UNLOCK(&so->so_snd);
1329 SOCKBUF_UNLOCK(&so->so_snd);
1334 if (flags & MSG_EOR)
1335 top->m_flags |= M_EOR;
1337 #ifdef ZERO_COPY_SOCKETS
1338 error = sosend_copyin(uio, &top, atomic,
1344 * Copy the data from userland into a mbuf
1345 * chain. If no data is to be copied in,
1346 * a single empty mbuf is returned.
1348 top = m_uiotombuf(uio, M_WAITOK, space,
1349 (atomic ? max_hdr : 0),
1350 (atomic ? M_PKTHDR : 0) |
1351 ((flags & MSG_EOR) ? M_EOR : 0));
1353 error = EFAULT; /* only possible error */
1356 space -= resid - uio->uio_resid;
1358 resid = uio->uio_resid;
1362 so->so_options |= SO_DONTROUTE;
1366 * XXX all the SBS_CANTSENDMORE checks previously
1367 * done could be out of date. We could have recieved
1368 * a reset packet in an interrupt or maybe we slept
1369 * while doing page faults in uiomove() etc. We
1370 * could probably recheck again inside the locking
1371 * protection here, but there are probably other
1372 * places that this also happens. We must rethink
1376 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1377 (flags & MSG_OOB) ? PRUS_OOB :
1379 * If the user set MSG_EOF, the protocol understands
1380 * this flag and nothing left to send then use
1381 * PRU_SEND_EOF instead of PRU_SEND.
1383 ((flags & MSG_EOF) &&
1384 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1387 /* If there is more to send set PRUS_MORETOCOME. */
1388 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1389 top, addr, control, td);
1392 so->so_options &= ~SO_DONTROUTE;
1400 } while (resid && space > 0);
1404 sbunlock(&so->so_snd);
1408 if (control != NULL)
1414 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1415 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1419 CURVNET_SET(so->so_vnet);
1420 error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1421 control, flags, td);
1427 * The part of soreceive() that implements reading non-inline out-of-band
1428 * data from a socket. For more complete comments, see soreceive(), from
1429 * which this code originated.
1431 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1432 * unable to return an mbuf chain to the caller.
1435 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1437 struct protosw *pr = so->so_proto;
1441 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1444 m = m_get(M_WAIT, MT_DATA);
1445 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1449 #ifdef ZERO_COPY_SOCKETS
1450 if (so_zero_copy_receive) {
1453 if ((m->m_flags & M_EXT)
1454 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1459 error = uiomoveco(mtod(m, void *),
1460 min(uio->uio_resid, m->m_len),
1463 #endif /* ZERO_COPY_SOCKETS */
1464 error = uiomove(mtod(m, void *),
1465 (int) min(uio->uio_resid, m->m_len), uio);
1467 } while (uio->uio_resid && error == 0 && m);
1475 * Following replacement or removal of the first mbuf on the first mbuf chain
1476 * of a socket buffer, push necessary state changes back into the socket
1477 * buffer so that other consumers see the values consistently. 'nextrecord'
1478 * is the callers locally stored value of the original value of
1479 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1480 * NOTE: 'nextrecord' may be NULL.
1482 static __inline void
1483 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1486 SOCKBUF_LOCK_ASSERT(sb);
1488 * First, update for the new value of nextrecord. If necessary, make
1489 * it the first record.
1491 if (sb->sb_mb != NULL)
1492 sb->sb_mb->m_nextpkt = nextrecord;
1494 sb->sb_mb = nextrecord;
1497 * Now update any dependent socket buffer fields to reflect the new
1498 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
1499 * addition of a second clause that takes care of the case where
1500 * sb_mb has been updated, but remains the last record.
1502 if (sb->sb_mb == NULL) {
1503 sb->sb_mbtail = NULL;
1504 sb->sb_lastrecord = NULL;
1505 } else if (sb->sb_mb->m_nextpkt == NULL)
1506 sb->sb_lastrecord = sb->sb_mb;
1511 * Implement receive operations on a socket. We depend on the way that
1512 * records are added to the sockbuf by sbappend. In particular, each record
1513 * (mbufs linked through m_next) must begin with an address if the protocol
1514 * so specifies, followed by an optional mbuf or mbufs containing ancillary
1515 * data, and then zero or more mbufs of data. In order to allow parallelism
1516 * between network receive and copying to user space, as well as avoid
1517 * sleeping with a mutex held, we release the socket buffer mutex during the
1518 * user space copy. Although the sockbuf is locked, new data may still be
1519 * appended, and thus we must maintain consistency of the sockbuf during that
1522 * The caller may receive the data as a single mbuf chain by supplying an
1523 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1524 * the count in uio_resid.
1527 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1528 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1530 struct mbuf *m, **mp;
1531 int flags, error, offset;
1533 struct protosw *pr = so->so_proto;
1534 struct mbuf *nextrecord;
1536 ssize_t orig_resid = uio->uio_resid;
1541 if (controlp != NULL)
1544 flags = *flagsp &~ MSG_EOR;
1547 if (flags & MSG_OOB)
1548 return (soreceive_rcvoob(so, uio, flags));
1551 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1552 && uio->uio_resid) {
1554 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1557 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1562 SOCKBUF_LOCK(&so->so_rcv);
1563 m = so->so_rcv.sb_mb;
1565 * If we have less data than requested, block awaiting more (subject
1566 * to any timeout) if:
1567 * 1. the current count is less than the low water mark, or
1568 * 2. MSG_DONTWAIT is not set
1570 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1571 so->so_rcv.sb_cc < uio->uio_resid) &&
1572 so->so_rcv.sb_cc < so->so_rcv.sb_lowat &&
1573 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1574 KASSERT(m != NULL || !so->so_rcv.sb_cc,
1575 ("receive: m == %p so->so_rcv.sb_cc == %u",
1576 m, so->so_rcv.sb_cc));
1580 error = so->so_error;
1581 if ((flags & MSG_PEEK) == 0)
1583 SOCKBUF_UNLOCK(&so->so_rcv);
1586 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1587 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1589 SOCKBUF_UNLOCK(&so->so_rcv);
1594 for (; m != NULL; m = m->m_next)
1595 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1596 m = so->so_rcv.sb_mb;
1599 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1600 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1601 SOCKBUF_UNLOCK(&so->so_rcv);
1605 if (uio->uio_resid == 0) {
1606 SOCKBUF_UNLOCK(&so->so_rcv);
1609 if ((so->so_state & SS_NBIO) ||
1610 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1611 SOCKBUF_UNLOCK(&so->so_rcv);
1612 error = EWOULDBLOCK;
1615 SBLASTRECORDCHK(&so->so_rcv);
1616 SBLASTMBUFCHK(&so->so_rcv);
1617 error = sbwait(&so->so_rcv);
1618 SOCKBUF_UNLOCK(&so->so_rcv);
1625 * From this point onward, we maintain 'nextrecord' as a cache of the
1626 * pointer to the next record in the socket buffer. We must keep the
1627 * various socket buffer pointers and local stack versions of the
1628 * pointers in sync, pushing out modifications before dropping the
1629 * socket buffer mutex, and re-reading them when picking it up.
1631 * Otherwise, we will race with the network stack appending new data
1632 * or records onto the socket buffer by using inconsistent/stale
1633 * versions of the field, possibly resulting in socket buffer
1636 * By holding the high-level sblock(), we prevent simultaneous
1637 * readers from pulling off the front of the socket buffer.
1639 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1641 uio->uio_td->td_ru.ru_msgrcv++;
1642 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1643 SBLASTRECORDCHK(&so->so_rcv);
1644 SBLASTMBUFCHK(&so->so_rcv);
1645 nextrecord = m->m_nextpkt;
1646 if (pr->pr_flags & PR_ADDR) {
1647 KASSERT(m->m_type == MT_SONAME,
1648 ("m->m_type == %d", m->m_type));
1651 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1653 if (flags & MSG_PEEK) {
1656 sbfree(&so->so_rcv, m);
1657 so->so_rcv.sb_mb = m_free(m);
1658 m = so->so_rcv.sb_mb;
1659 sockbuf_pushsync(&so->so_rcv, nextrecord);
1664 * Process one or more MT_CONTROL mbufs present before any data mbufs
1665 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1666 * just copy the data; if !MSG_PEEK, we call into the protocol to
1667 * perform externalization (or freeing if controlp == NULL).
1669 if (m != NULL && m->m_type == MT_CONTROL) {
1670 struct mbuf *cm = NULL, *cmn;
1671 struct mbuf **cme = &cm;
1674 if (flags & MSG_PEEK) {
1675 if (controlp != NULL) {
1676 *controlp = m_copy(m, 0, m->m_len);
1677 controlp = &(*controlp)->m_next;
1681 sbfree(&so->so_rcv, m);
1682 so->so_rcv.sb_mb = m->m_next;
1685 cme = &(*cme)->m_next;
1686 m = so->so_rcv.sb_mb;
1688 } while (m != NULL && m->m_type == MT_CONTROL);
1689 if ((flags & MSG_PEEK) == 0)
1690 sockbuf_pushsync(&so->so_rcv, nextrecord);
1691 while (cm != NULL) {
1694 if (pr->pr_domain->dom_externalize != NULL) {
1695 SOCKBUF_UNLOCK(&so->so_rcv);
1697 error = (*pr->pr_domain->dom_externalize)
1699 SOCKBUF_LOCK(&so->so_rcv);
1700 } else if (controlp != NULL)
1704 if (controlp != NULL) {
1706 while (*controlp != NULL)
1707 controlp = &(*controlp)->m_next;
1712 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1714 nextrecord = so->so_rcv.sb_mb;
1718 if ((flags & MSG_PEEK) == 0) {
1719 KASSERT(m->m_nextpkt == nextrecord,
1720 ("soreceive: post-control, nextrecord !sync"));
1721 if (nextrecord == NULL) {
1722 KASSERT(so->so_rcv.sb_mb == m,
1723 ("soreceive: post-control, sb_mb!=m"));
1724 KASSERT(so->so_rcv.sb_lastrecord == m,
1725 ("soreceive: post-control, lastrecord!=m"));
1729 if (type == MT_OOBDATA)
1732 if ((flags & MSG_PEEK) == 0) {
1733 KASSERT(so->so_rcv.sb_mb == nextrecord,
1734 ("soreceive: sb_mb != nextrecord"));
1735 if (so->so_rcv.sb_mb == NULL) {
1736 KASSERT(so->so_rcv.sb_lastrecord == NULL,
1737 ("soreceive: sb_lastercord != NULL"));
1741 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1742 SBLASTRECORDCHK(&so->so_rcv);
1743 SBLASTMBUFCHK(&so->so_rcv);
1746 * Now continue to read any data mbufs off of the head of the socket
1747 * buffer until the read request is satisfied. Note that 'type' is
1748 * used to store the type of any mbuf reads that have happened so far
1749 * such that soreceive() can stop reading if the type changes, which
1750 * causes soreceive() to return only one of regular data and inline
1751 * out-of-band data in a single socket receive operation.
1755 while (m != NULL && uio->uio_resid > 0 && error == 0) {
1757 * If the type of mbuf has changed since the last mbuf
1758 * examined ('type'), end the receive operation.
1760 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1761 if (m->m_type == MT_OOBDATA || m->m_type == MT_CONTROL) {
1762 if (type != m->m_type)
1764 } else if (type == MT_OOBDATA)
1767 KASSERT(m->m_type == MT_DATA,
1768 ("m->m_type == %d", m->m_type));
1769 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1770 len = uio->uio_resid;
1771 if (so->so_oobmark && len > so->so_oobmark - offset)
1772 len = so->so_oobmark - offset;
1773 if (len > m->m_len - moff)
1774 len = m->m_len - moff;
1776 * If mp is set, just pass back the mbufs. Otherwise copy
1777 * them out via the uio, then free. Sockbuf must be
1778 * consistent here (points to current mbuf, it points to next
1779 * record) when we drop priority; we must note any additions
1780 * to the sockbuf when we block interrupts again.
1783 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1784 SBLASTRECORDCHK(&so->so_rcv);
1785 SBLASTMBUFCHK(&so->so_rcv);
1786 SOCKBUF_UNLOCK(&so->so_rcv);
1787 #ifdef ZERO_COPY_SOCKETS
1788 if (so_zero_copy_receive) {
1791 if ((m->m_flags & M_EXT)
1792 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1797 error = uiomoveco(mtod(m, char *) + moff,
1801 #endif /* ZERO_COPY_SOCKETS */
1802 error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1803 SOCKBUF_LOCK(&so->so_rcv);
1806 * The MT_SONAME mbuf has already been removed
1807 * from the record, so it is necessary to
1808 * remove the data mbufs, if any, to preserve
1809 * the invariant in the case of PR_ADDR that
1810 * requires MT_SONAME mbufs at the head of
1813 if (m && pr->pr_flags & PR_ATOMIC &&
1814 ((flags & MSG_PEEK) == 0))
1815 (void)sbdroprecord_locked(&so->so_rcv);
1816 SOCKBUF_UNLOCK(&so->so_rcv);
1820 uio->uio_resid -= len;
1821 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1822 if (len == m->m_len - moff) {
1823 if (m->m_flags & M_EOR)
1825 if (flags & MSG_PEEK) {
1829 nextrecord = m->m_nextpkt;
1830 sbfree(&so->so_rcv, m);
1834 so->so_rcv.sb_mb = m = m->m_next;
1837 so->so_rcv.sb_mb = m_free(m);
1838 m = so->so_rcv.sb_mb;
1840 sockbuf_pushsync(&so->so_rcv, nextrecord);
1841 SBLASTRECORDCHK(&so->so_rcv);
1842 SBLASTMBUFCHK(&so->so_rcv);
1845 if (flags & MSG_PEEK)
1851 if (flags & MSG_DONTWAIT)
1852 copy_flag = M_DONTWAIT;
1855 if (copy_flag == M_WAIT)
1856 SOCKBUF_UNLOCK(&so->so_rcv);
1857 *mp = m_copym(m, 0, len, copy_flag);
1858 if (copy_flag == M_WAIT)
1859 SOCKBUF_LOCK(&so->so_rcv);
1862 * m_copym() couldn't
1863 * allocate an mbuf. Adjust
1864 * uio_resid back (it was
1865 * adjusted down by len
1866 * bytes, which we didn't end
1867 * up "copying" over).
1869 uio->uio_resid += len;
1875 so->so_rcv.sb_cc -= len;
1878 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1879 if (so->so_oobmark) {
1880 if ((flags & MSG_PEEK) == 0) {
1881 so->so_oobmark -= len;
1882 if (so->so_oobmark == 0) {
1883 so->so_rcv.sb_state |= SBS_RCVATMARK;
1888 if (offset == so->so_oobmark)
1892 if (flags & MSG_EOR)
1895 * If the MSG_WAITALL flag is set (for non-atomic socket), we
1896 * must not quit until "uio->uio_resid == 0" or an error
1897 * termination. If a signal/timeout occurs, return with a
1898 * short count but without error. Keep sockbuf locked
1899 * against other readers.
1901 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1902 !sosendallatonce(so) && nextrecord == NULL) {
1903 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1904 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
1907 * Notify the protocol that some data has been
1908 * drained before blocking.
1910 if (pr->pr_flags & PR_WANTRCVD) {
1911 SOCKBUF_UNLOCK(&so->so_rcv);
1913 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1914 SOCKBUF_LOCK(&so->so_rcv);
1916 SBLASTRECORDCHK(&so->so_rcv);
1917 SBLASTMBUFCHK(&so->so_rcv);
1919 * We could receive some data while was notifying
1920 * the protocol. Skip blocking in this case.
1922 if (so->so_rcv.sb_mb == NULL) {
1923 error = sbwait(&so->so_rcv);
1925 SOCKBUF_UNLOCK(&so->so_rcv);
1929 m = so->so_rcv.sb_mb;
1931 nextrecord = m->m_nextpkt;
1935 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1936 if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1938 if ((flags & MSG_PEEK) == 0)
1939 (void) sbdroprecord_locked(&so->so_rcv);
1941 if ((flags & MSG_PEEK) == 0) {
1944 * First part is an inline SB_EMPTY_FIXUP(). Second
1945 * part makes sure sb_lastrecord is up-to-date if
1946 * there is still data in the socket buffer.
1948 so->so_rcv.sb_mb = nextrecord;
1949 if (so->so_rcv.sb_mb == NULL) {
1950 so->so_rcv.sb_mbtail = NULL;
1951 so->so_rcv.sb_lastrecord = NULL;
1952 } else if (nextrecord->m_nextpkt == NULL)
1953 so->so_rcv.sb_lastrecord = nextrecord;
1955 SBLASTRECORDCHK(&so->so_rcv);
1956 SBLASTMBUFCHK(&so->so_rcv);
1958 * If soreceive() is being done from the socket callback,
1959 * then don't need to generate ACK to peer to update window,
1960 * since ACK will be generated on return to TCP.
1962 if (!(flags & MSG_SOCALLBCK) &&
1963 (pr->pr_flags & PR_WANTRCVD)) {
1964 SOCKBUF_UNLOCK(&so->so_rcv);
1966 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1967 SOCKBUF_LOCK(&so->so_rcv);
1970 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1971 if (orig_resid == uio->uio_resid && orig_resid &&
1972 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1973 SOCKBUF_UNLOCK(&so->so_rcv);
1976 SOCKBUF_UNLOCK(&so->so_rcv);
1981 sbunlock(&so->so_rcv);
1986 * Optimized version of soreceive() for stream (TCP) sockets.
1987 * XXXAO: (MSG_WAITALL | MSG_PEEK) isn't properly handled.
1990 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
1991 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1993 int len = 0, error = 0, flags, oresid;
1995 struct mbuf *m, *n = NULL;
1997 /* We only do stream sockets. */
1998 if (so->so_type != SOCK_STREAM)
2002 if (controlp != NULL)
2005 flags = *flagsp &~ MSG_EOR;
2008 if (flags & MSG_OOB)
2009 return (soreceive_rcvoob(so, uio, flags));
2015 /* Prevent other readers from entering the socket. */
2016 error = sblock(sb, SBLOCKWAIT(flags));
2021 /* Easy one, no space to copyout anything. */
2022 if (uio->uio_resid == 0) {
2026 oresid = uio->uio_resid;
2028 /* We will never ever get anything unless we are or were connected. */
2029 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
2035 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2037 /* Abort if socket has reported problems. */
2041 if (oresid > uio->uio_resid)
2043 error = so->so_error;
2044 if (!(flags & MSG_PEEK))
2049 /* Door is closed. Deliver what is left, if any. */
2050 if (sb->sb_state & SBS_CANTRCVMORE) {
2057 /* Socket buffer is empty and we shall not block. */
2058 if (sb->sb_cc == 0 &&
2059 ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
2064 /* Socket buffer got some data that we shall deliver now. */
2065 if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) &&
2066 ((so->so_state & SS_NBIO) ||
2067 (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
2068 sb->sb_cc >= sb->sb_lowat ||
2069 sb->sb_cc >= uio->uio_resid ||
2070 sb->sb_cc >= sb->sb_hiwat) ) {
2074 /* On MSG_WAITALL we must wait until all data or error arrives. */
2075 if ((flags & MSG_WAITALL) &&
2076 (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_hiwat))
2080 * Wait and block until (more) data comes in.
2081 * NB: Drops the sockbuf lock during wait.
2089 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2090 KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__));
2091 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
2095 uio->uio_td->td_ru.ru_msgrcv++;
2097 /* Fill uio until full or current end of socket buffer is reached. */
2098 len = min(uio->uio_resid, sb->sb_cc);
2100 /* Dequeue as many mbufs as possible. */
2101 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
2105 m_cat(*mp0, sb->sb_mb);
2107 m != NULL && m->m_len <= len;
2110 uio->uio_resid -= m->m_len;
2116 sb->sb_lastrecord = sb->sb_mb;
2117 if (sb->sb_mb == NULL)
2120 /* Copy the remainder. */
2122 KASSERT(sb->sb_mb != NULL,
2123 ("%s: len > 0 && sb->sb_mb empty", __func__));
2125 m = m_copym(sb->sb_mb, 0, len, M_DONTWAIT);
2127 len = 0; /* Don't flush data from sockbuf. */
2129 uio->uio_resid -= len;
2140 /* NB: Must unlock socket buffer as uiomove may sleep. */
2142 error = m_mbuftouio(uio, sb->sb_mb, len);
2147 SBLASTRECORDCHK(sb);
2151 * Remove the delivered data from the socket buffer unless we
2152 * were only peeking.
2154 if (!(flags & MSG_PEEK)) {
2156 sbdrop_locked(sb, len);
2158 /* Notify protocol that we drained some data. */
2159 if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
2160 (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
2161 !(flags & MSG_SOCALLBCK))) {
2164 (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
2170 * For MSG_WAITALL we may have to loop again and wait for
2171 * more data to come in.
2173 if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
2176 SOCKBUF_LOCK_ASSERT(sb);
2177 SBLASTRECORDCHK(sb);
2185 * Optimized version of soreceive() for simple datagram cases from userspace.
2186 * Unlike in the stream case, we're able to drop a datagram if copyout()
2187 * fails, and because we handle datagrams atomically, we don't need to use a
2188 * sleep lock to prevent I/O interlacing.
2191 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
2192 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2194 struct mbuf *m, *m2;
2197 struct protosw *pr = so->so_proto;
2198 struct mbuf *nextrecord;
2202 if (controlp != NULL)
2205 flags = *flagsp &~ MSG_EOR;
2210 * For any complicated cases, fall back to the full
2211 * soreceive_generic().
2213 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
2214 return (soreceive_generic(so, psa, uio, mp0, controlp,
2218 * Enforce restrictions on use.
2220 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
2221 ("soreceive_dgram: wantrcvd"));
2222 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
2223 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
2224 ("soreceive_dgram: SBS_RCVATMARK"));
2225 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
2226 ("soreceive_dgram: P_CONNREQUIRED"));
2229 * Loop blocking while waiting for a datagram.
2231 SOCKBUF_LOCK(&so->so_rcv);
2232 while ((m = so->so_rcv.sb_mb) == NULL) {
2233 KASSERT(so->so_rcv.sb_cc == 0,
2234 ("soreceive_dgram: sb_mb NULL but sb_cc %u",
2237 error = so->so_error;
2239 SOCKBUF_UNLOCK(&so->so_rcv);
2242 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
2243 uio->uio_resid == 0) {
2244 SOCKBUF_UNLOCK(&so->so_rcv);
2247 if ((so->so_state & SS_NBIO) ||
2248 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2249 SOCKBUF_UNLOCK(&so->so_rcv);
2250 return (EWOULDBLOCK);
2252 SBLASTRECORDCHK(&so->so_rcv);
2253 SBLASTMBUFCHK(&so->so_rcv);
2254 error = sbwait(&so->so_rcv);
2256 SOCKBUF_UNLOCK(&so->so_rcv);
2260 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2263 uio->uio_td->td_ru.ru_msgrcv++;
2264 SBLASTRECORDCHK(&so->so_rcv);
2265 SBLASTMBUFCHK(&so->so_rcv);
2266 nextrecord = m->m_nextpkt;
2267 if (nextrecord == NULL) {
2268 KASSERT(so->so_rcv.sb_lastrecord == m,
2269 ("soreceive_dgram: lastrecord != m"));
2272 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
2273 ("soreceive_dgram: m_nextpkt != nextrecord"));
2276 * Pull 'm' and its chain off the front of the packet queue.
2278 so->so_rcv.sb_mb = NULL;
2279 sockbuf_pushsync(&so->so_rcv, nextrecord);
2282 * Walk 'm's chain and free that many bytes from the socket buffer.
2284 for (m2 = m; m2 != NULL; m2 = m2->m_next)
2285 sbfree(&so->so_rcv, m2);
2288 * Do a few last checks before we let go of the lock.
2290 SBLASTRECORDCHK(&so->so_rcv);
2291 SBLASTMBUFCHK(&so->so_rcv);
2292 SOCKBUF_UNLOCK(&so->so_rcv);
2294 if (pr->pr_flags & PR_ADDR) {
2295 KASSERT(m->m_type == MT_SONAME,
2296 ("m->m_type == %d", m->m_type));
2298 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
2303 /* XXXRW: Can this happen? */
2308 * Packet to copyout() is now in 'm' and it is disconnected from the
2311 * Process one or more MT_CONTROL mbufs present before any data mbufs
2312 * in the first mbuf chain on the socket buffer. We call into the
2313 * protocol to perform externalization (or freeing if controlp ==
2314 * NULL). In some cases there can be only MT_CONTROL mbufs without
2317 if (m->m_type == MT_CONTROL) {
2318 struct mbuf *cm = NULL, *cmn;
2319 struct mbuf **cme = &cm;
2325 cme = &(*cme)->m_next;
2327 } while (m != NULL && m->m_type == MT_CONTROL);
2328 while (cm != NULL) {
2331 if (pr->pr_domain->dom_externalize != NULL) {
2332 error = (*pr->pr_domain->dom_externalize)
2334 } else if (controlp != NULL)
2338 if (controlp != NULL) {
2339 while (*controlp != NULL)
2340 controlp = &(*controlp)->m_next;
2345 KASSERT(m == NULL || m->m_type == MT_DATA,
2346 ("soreceive_dgram: !data"));
2347 while (m != NULL && uio->uio_resid > 0) {
2348 len = uio->uio_resid;
2351 error = uiomove(mtod(m, char *), (int)len, uio);
2356 if (len == m->m_len)
2373 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2374 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2378 CURVNET_SET(so->so_vnet);
2379 error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
2386 soshutdown(struct socket *so, int how)
2388 struct protosw *pr = so->so_proto;
2391 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2394 CURVNET_SET(so->so_vnet);
2395 if (pr->pr_usrreqs->pru_flush != NULL) {
2396 (*pr->pr_usrreqs->pru_flush)(so, how);
2400 if (how != SHUT_RD) {
2401 error = (*pr->pr_usrreqs->pru_shutdown)(so);
2402 wakeup(&so->so_timeo);
2406 wakeup(&so->so_timeo);
2412 sorflush(struct socket *so)
2414 struct sockbuf *sb = &so->so_rcv;
2415 struct protosw *pr = so->so_proto;
2421 * In order to avoid calling dom_dispose with the socket buffer mutex
2422 * held, and in order to generally avoid holding the lock for a long
2423 * time, we make a copy of the socket buffer and clear the original
2424 * (except locks, state). The new socket buffer copy won't have
2425 * initialized locks so we can only call routines that won't use or
2426 * assert those locks.
2428 * Dislodge threads currently blocked in receive and wait to acquire
2429 * a lock against other simultaneous readers before clearing the
2430 * socket buffer. Don't let our acquire be interrupted by a signal
2431 * despite any existing socket disposition on interruptable waiting.
2434 (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2437 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2438 * and mutex data unchanged.
2441 bzero(&asb, offsetof(struct sockbuf, sb_startzero));
2442 bcopy(&sb->sb_startzero, &asb.sb_startzero,
2443 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2444 bzero(&sb->sb_startzero,
2445 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2450 * Dispose of special rights and flush the socket buffer. Don't call
2451 * any unsafe routines (that rely on locks being initialized) on asb.
2453 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2454 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
2455 sbrelease_internal(&asb, so);
2459 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2460 * additional variant to handle the case where the option value needs to be
2461 * some kind of integer, but not a specific size. In addition to their use
2462 * here, these functions are also called by the protocol-level pr_ctloutput()
2466 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2471 * If the user gives us more than we wanted, we ignore it, but if we
2472 * don't get the minimum length the caller wants, we return EINVAL.
2473 * On success, sopt->sopt_valsize is set to however much we actually
2476 if ((valsize = sopt->sopt_valsize) < minlen)
2479 sopt->sopt_valsize = valsize = len;
2481 if (sopt->sopt_td != NULL)
2482 return (copyin(sopt->sopt_val, buf, valsize));
2484 bcopy(sopt->sopt_val, buf, valsize);
2489 * Kernel version of setsockopt(2).
2491 * XXX: optlen is size_t, not socklen_t
2494 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2497 struct sockopt sopt;
2499 sopt.sopt_level = level;
2500 sopt.sopt_name = optname;
2501 sopt.sopt_dir = SOPT_SET;
2502 sopt.sopt_val = optval;
2503 sopt.sopt_valsize = optlen;
2504 sopt.sopt_td = NULL;
2505 return (sosetopt(so, &sopt));
2509 sosetopt(struct socket *so, struct sockopt *sopt)
2520 CURVNET_SET(so->so_vnet);
2522 if (sopt->sopt_level != SOL_SOCKET) {
2523 if (so->so_proto->pr_ctloutput != NULL) {
2524 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2528 error = ENOPROTOOPT;
2530 switch (sopt->sopt_name) {
2532 case SO_ACCEPTFILTER:
2533 error = do_setopt_accept_filter(so, sopt);
2539 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2544 so->so_linger = l.l_linger;
2546 so->so_options |= SO_LINGER;
2548 so->so_options &= ~SO_LINGER;
2555 case SO_USELOOPBACK:
2565 error = sooptcopyin(sopt, &optval, sizeof optval,
2571 so->so_options |= sopt->sopt_name;
2573 so->so_options &= ~sopt->sopt_name;
2578 error = sooptcopyin(sopt, &optval, sizeof optval,
2583 if (optval < 0 || optval >= rt_numfibs) {
2587 if (((so->so_proto->pr_domain->dom_family == PF_INET) ||
2588 (so->so_proto->pr_domain->dom_family == PF_INET6) ||
2589 (so->so_proto->pr_domain->dom_family == PF_ROUTE)))
2590 so->so_fibnum = optval;
2595 case SO_USER_COOKIE:
2596 error = sooptcopyin(sopt, &val32, sizeof val32,
2600 so->so_user_cookie = val32;
2607 error = sooptcopyin(sopt, &optval, sizeof optval,
2613 * Values < 1 make no sense for any of these options,
2621 switch (sopt->sopt_name) {
2624 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
2625 &so->so_snd : &so->so_rcv, (u_long)optval,
2626 so, curthread) == 0) {
2630 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
2631 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
2635 * Make sure the low-water is never greater than the
2639 SOCKBUF_LOCK(&so->so_snd);
2640 so->so_snd.sb_lowat =
2641 (optval > so->so_snd.sb_hiwat) ?
2642 so->so_snd.sb_hiwat : optval;
2643 SOCKBUF_UNLOCK(&so->so_snd);
2646 SOCKBUF_LOCK(&so->so_rcv);
2647 so->so_rcv.sb_lowat =
2648 (optval > so->so_rcv.sb_hiwat) ?
2649 so->so_rcv.sb_hiwat : optval;
2650 SOCKBUF_UNLOCK(&so->so_rcv);
2657 #ifdef COMPAT_FREEBSD32
2658 if (SV_CURPROC_FLAG(SV_ILP32)) {
2659 struct timeval32 tv32;
2661 error = sooptcopyin(sopt, &tv32, sizeof tv32,
2663 CP(tv32, tv, tv_sec);
2664 CP(tv32, tv, tv_usec);
2667 error = sooptcopyin(sopt, &tv, sizeof tv,
2671 if (tv.tv_sec < 0 || tv.tv_usec < 0 ||
2672 tv.tv_usec >= 1000000) {
2678 switch (sopt->sopt_name) {
2680 so->so_snd.sb_timeo = val;
2683 so->so_rcv.sb_timeo = val;
2690 error = sooptcopyin(sopt, &extmac, sizeof extmac,
2694 error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2702 error = ENOPROTOOPT;
2705 if (error == 0 && so->so_proto->pr_ctloutput != NULL)
2706 (void)(*so->so_proto->pr_ctloutput)(so, sopt);
2714 * Helper routine for getsockopt.
2717 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2725 * Documented get behavior is that we always return a value, possibly
2726 * truncated to fit in the user's buffer. Traditional behavior is
2727 * that we always tell the user precisely how much we copied, rather
2728 * than something useful like the total amount we had available for
2729 * her. Note that this interface is not idempotent; the entire
2730 * answer must generated ahead of time.
2732 valsize = min(len, sopt->sopt_valsize);
2733 sopt->sopt_valsize = valsize;
2734 if (sopt->sopt_val != NULL) {
2735 if (sopt->sopt_td != NULL)
2736 error = copyout(buf, sopt->sopt_val, valsize);
2738 bcopy(buf, sopt->sopt_val, valsize);
2744 sogetopt(struct socket *so, struct sockopt *sopt)
2753 CURVNET_SET(so->so_vnet);
2755 if (sopt->sopt_level != SOL_SOCKET) {
2756 if (so->so_proto->pr_ctloutput != NULL)
2757 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2759 error = ENOPROTOOPT;
2763 switch (sopt->sopt_name) {
2765 case SO_ACCEPTFILTER:
2766 error = do_getopt_accept_filter(so, sopt);
2771 l.l_onoff = so->so_options & SO_LINGER;
2772 l.l_linger = so->so_linger;
2774 error = sooptcopyout(sopt, &l, sizeof l);
2777 case SO_USELOOPBACK:
2789 optval = so->so_options & sopt->sopt_name;
2791 error = sooptcopyout(sopt, &optval, sizeof optval);
2795 optval = so->so_type;
2799 optval = so->so_proto->pr_protocol;
2804 optval = so->so_error;
2810 optval = so->so_snd.sb_hiwat;
2814 optval = so->so_rcv.sb_hiwat;
2818 optval = so->so_snd.sb_lowat;
2822 optval = so->so_rcv.sb_lowat;
2827 optval = (sopt->sopt_name == SO_SNDTIMEO ?
2828 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2830 tv.tv_sec = optval / hz;
2831 tv.tv_usec = (optval % hz) * tick;
2832 #ifdef COMPAT_FREEBSD32
2833 if (SV_CURPROC_FLAG(SV_ILP32)) {
2834 struct timeval32 tv32;
2836 CP(tv, tv32, tv_sec);
2837 CP(tv, tv32, tv_usec);
2838 error = sooptcopyout(sopt, &tv32, sizeof tv32);
2841 error = sooptcopyout(sopt, &tv, sizeof tv);
2846 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2850 error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
2854 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2862 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2866 error = mac_getsockopt_peerlabel(
2867 sopt->sopt_td->td_ucred, so, &extmac);
2870 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2876 case SO_LISTENQLIMIT:
2877 optval = so->so_qlimit;
2881 optval = so->so_qlen;
2884 case SO_LISTENINCQLEN:
2885 optval = so->so_incqlen;
2889 error = ENOPROTOOPT;
2900 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2902 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2904 struct mbuf *m, *m_prev;
2905 int sopt_size = sopt->sopt_valsize;
2907 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2910 if (sopt_size > MLEN) {
2911 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
2912 if ((m->m_flags & M_EXT) == 0) {
2916 m->m_len = min(MCLBYTES, sopt_size);
2918 m->m_len = min(MLEN, sopt_size);
2920 sopt_size -= m->m_len;
2925 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2930 if (sopt_size > MLEN) {
2931 MCLGET(m, sopt->sopt_td != NULL ? M_WAIT :
2933 if ((m->m_flags & M_EXT) == 0) {
2938 m->m_len = min(MCLBYTES, sopt_size);
2940 m->m_len = min(MLEN, sopt_size);
2942 sopt_size -= m->m_len;
2949 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2951 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2953 struct mbuf *m0 = m;
2955 if (sopt->sopt_val == NULL)
2957 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2958 if (sopt->sopt_td != NULL) {
2961 error = copyin(sopt->sopt_val, mtod(m, char *),
2968 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2969 sopt->sopt_valsize -= m->m_len;
2970 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2973 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2974 panic("ip6_sooptmcopyin");
2978 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2980 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2982 struct mbuf *m0 = m;
2985 if (sopt->sopt_val == NULL)
2987 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2988 if (sopt->sopt_td != NULL) {
2991 error = copyout(mtod(m, char *), sopt->sopt_val,
2998 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2999 sopt->sopt_valsize -= m->m_len;
3000 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
3001 valsize += m->m_len;
3005 /* enough soopt buffer should be given from user-land */
3009 sopt->sopt_valsize = valsize;
3014 * sohasoutofband(): protocol notifies socket layer of the arrival of new
3015 * out-of-band data, which will then notify socket consumers.
3018 sohasoutofband(struct socket *so)
3021 if (so->so_sigio != NULL)
3022 pgsigio(&so->so_sigio, SIGURG, 0);
3023 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
3027 sopoll(struct socket *so, int events, struct ucred *active_cred,
3032 * We do not need to set or assert curvnet as long as everyone uses
3035 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
3040 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
3045 SOCKBUF_LOCK(&so->so_snd);
3046 SOCKBUF_LOCK(&so->so_rcv);
3047 if (events & (POLLIN | POLLRDNORM))
3048 if (soreadabledata(so))
3049 revents |= events & (POLLIN | POLLRDNORM);
3051 if (events & (POLLOUT | POLLWRNORM))
3052 if (sowriteable(so))
3053 revents |= events & (POLLOUT | POLLWRNORM);
3055 if (events & (POLLPRI | POLLRDBAND))
3056 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
3057 revents |= events & (POLLPRI | POLLRDBAND);
3059 if ((events & POLLINIGNEOF) == 0) {
3060 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3061 revents |= events & (POLLIN | POLLRDNORM);
3062 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
3068 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
3069 selrecord(td, &so->so_rcv.sb_sel);
3070 so->so_rcv.sb_flags |= SB_SEL;
3073 if (events & (POLLOUT | POLLWRNORM)) {
3074 selrecord(td, &so->so_snd.sb_sel);
3075 so->so_snd.sb_flags |= SB_SEL;
3079 SOCKBUF_UNLOCK(&so->so_rcv);
3080 SOCKBUF_UNLOCK(&so->so_snd);
3085 soo_kqfilter(struct file *fp, struct knote *kn)
3087 struct socket *so = kn->kn_fp->f_data;
3090 switch (kn->kn_filter) {
3092 if (so->so_options & SO_ACCEPTCONN)
3093 kn->kn_fop = &solisten_filtops;
3095 kn->kn_fop = &soread_filtops;
3099 kn->kn_fop = &sowrite_filtops;
3107 knlist_add(&sb->sb_sel.si_note, kn, 1);
3108 sb->sb_flags |= SB_KNOTE;
3114 * Some routines that return EOPNOTSUPP for entry points that are not
3115 * supported by a protocol. Fill in as needed.
3118 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
3125 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
3132 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3139 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3146 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
3153 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
3154 struct ifnet *ifp, struct thread *td)
3161 pru_disconnect_notsupp(struct socket *so)
3168 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
3175 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
3182 pru_rcvd_notsupp(struct socket *so, int flags)
3189 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
3196 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
3197 struct sockaddr *addr, struct mbuf *control, struct thread *td)
3204 * This isn't really a ``null'' operation, but it's the default one and
3205 * doesn't do anything destructive.
3208 pru_sense_null(struct socket *so, struct stat *sb)
3211 sb->st_blksize = so->so_snd.sb_hiwat;
3216 pru_shutdown_notsupp(struct socket *so)
3223 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
3230 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
3231 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
3238 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
3239 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
3246 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
3254 filt_sordetach(struct knote *kn)
3256 struct socket *so = kn->kn_fp->f_data;
3258 SOCKBUF_LOCK(&so->so_rcv);
3259 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
3260 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
3261 so->so_rcv.sb_flags &= ~SB_KNOTE;
3262 SOCKBUF_UNLOCK(&so->so_rcv);
3267 filt_soread(struct knote *kn, long hint)
3271 so = kn->kn_fp->f_data;
3272 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
3274 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
3275 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3276 kn->kn_flags |= EV_EOF;
3277 kn->kn_fflags = so->so_error;
3279 } else if (so->so_error) /* temporary udp error */
3281 else if (kn->kn_sfflags & NOTE_LOWAT)
3282 return (kn->kn_data >= kn->kn_sdata);
3284 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
3288 filt_sowdetach(struct knote *kn)
3290 struct socket *so = kn->kn_fp->f_data;
3292 SOCKBUF_LOCK(&so->so_snd);
3293 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
3294 if (knlist_empty(&so->so_snd.sb_sel.si_note))
3295 so->so_snd.sb_flags &= ~SB_KNOTE;
3296 SOCKBUF_UNLOCK(&so->so_snd);
3301 filt_sowrite(struct knote *kn, long hint)
3305 so = kn->kn_fp->f_data;
3306 SOCKBUF_LOCK_ASSERT(&so->so_snd);
3307 kn->kn_data = sbspace(&so->so_snd);
3308 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
3309 kn->kn_flags |= EV_EOF;
3310 kn->kn_fflags = so->so_error;
3312 } else if (so->so_error) /* temporary udp error */
3314 else if (((so->so_state & SS_ISCONNECTED) == 0) &&
3315 (so->so_proto->pr_flags & PR_CONNREQUIRED))
3317 else if (kn->kn_sfflags & NOTE_LOWAT)
3318 return (kn->kn_data >= kn->kn_sdata);
3320 return (kn->kn_data >= so->so_snd.sb_lowat);
3325 filt_solisten(struct knote *kn, long hint)
3327 struct socket *so = kn->kn_fp->f_data;
3329 kn->kn_data = so->so_qlen;
3330 return (! TAILQ_EMPTY(&so->so_comp));
3334 socheckuid(struct socket *so, uid_t uid)
3339 if (so->so_cred->cr_uid != uid)
3345 * These functions are used by protocols to notify the socket layer (and its
3346 * consumers) of state changes in the sockets driven by protocol-side events.
3350 * Procedures to manipulate state flags of socket and do appropriate wakeups.
3352 * Normal sequence from the active (originating) side is that
3353 * soisconnecting() is called during processing of connect() call, resulting
3354 * in an eventual call to soisconnected() if/when the connection is
3355 * established. When the connection is torn down soisdisconnecting() is
3356 * called during processing of disconnect() call, and soisdisconnected() is
3357 * called when the connection to the peer is totally severed. The semantics
3358 * of these routines are such that connectionless protocols can call
3359 * soisconnected() and soisdisconnected() only, bypassing the in-progress
3360 * calls when setting up a ``connection'' takes no time.
3362 * From the passive side, a socket is created with two queues of sockets:
3363 * so_incomp for connections in progress and so_comp for connections already
3364 * made and awaiting user acceptance. As a protocol is preparing incoming
3365 * connections, it creates a socket structure queued on so_incomp by calling
3366 * sonewconn(). When the connection is established, soisconnected() is
3367 * called, and transfers the socket structure to so_comp, making it available
3370 * If a socket is closed with sockets on either so_incomp or so_comp, these
3371 * sockets are dropped.
3373 * If higher-level protocols are implemented in the kernel, the wakeups done
3374 * here will sometimes cause software-interrupt process scheduling.
3377 soisconnecting(struct socket *so)
3381 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3382 so->so_state |= SS_ISCONNECTING;
3387 soisconnected(struct socket *so)
3389 struct socket *head;
3395 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3396 so->so_state |= SS_ISCONNECTED;
3398 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
3399 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3401 TAILQ_REMOVE(&head->so_incomp, so, so_list);
3403 so->so_qstate &= ~SQ_INCOMP;
3404 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
3406 so->so_qstate |= SQ_COMP;
3409 wakeup_one(&head->so_timeo);
3412 soupcall_set(so, SO_RCV,
3413 head->so_accf->so_accept_filter->accf_callback,
3414 head->so_accf->so_accept_filter_arg);
3415 so->so_options &= ~SO_ACCEPTFILTER;
3416 ret = head->so_accf->so_accept_filter->accf_callback(so,
3417 head->so_accf->so_accept_filter_arg, M_DONTWAIT);
3418 if (ret == SU_ISCONNECTED)
3419 soupcall_clear(so, SO_RCV);
3421 if (ret == SU_ISCONNECTED)
3428 wakeup(&so->so_timeo);
3434 soisdisconnecting(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;
3443 so->so_state |= SS_ISDISCONNECTING;
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 sowwakeup_locked(so);
3449 wakeup(&so->so_timeo);
3453 soisdisconnected(struct socket *so)
3457 * Note: This code assumes that SOCK_LOCK(so) and
3458 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3460 SOCKBUF_LOCK(&so->so_rcv);
3461 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3462 so->so_state |= SS_ISDISCONNECTED;
3463 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3464 sorwakeup_locked(so);
3465 SOCKBUF_LOCK(&so->so_snd);
3466 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3467 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
3468 sowwakeup_locked(so);
3469 wakeup(&so->so_timeo);
3473 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3476 sodupsockaddr(const struct sockaddr *sa, int mflags)
3478 struct sockaddr *sa2;
3480 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3482 bcopy(sa, sa2, sa->sa_len);
3487 * Register per-socket buffer upcalls.
3490 soupcall_set(struct socket *so, int which,
3491 int (*func)(struct socket *, void *, int), void *arg)
3503 panic("soupcall_set: bad which");
3505 SOCKBUF_LOCK_ASSERT(sb);
3507 /* XXX: accf_http actually wants to do this on purpose. */
3508 KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall"));
3510 sb->sb_upcall = func;
3511 sb->sb_upcallarg = arg;
3512 sb->sb_flags |= SB_UPCALL;
3516 soupcall_clear(struct socket *so, int which)
3528 panic("soupcall_clear: bad which");
3530 SOCKBUF_LOCK_ASSERT(sb);
3531 KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear"));
3532 sb->sb_upcall = NULL;
3533 sb->sb_upcallarg = NULL;
3534 sb->sb_flags &= ~SB_UPCALL;
3538 * Create an external-format (``xsocket'') structure using the information in
3539 * the kernel-format socket structure pointed to by so. This is done to
3540 * reduce the spew of irrelevant information over this interface, to isolate
3541 * user code from changes in the kernel structure, and potentially to provide
3542 * information-hiding if we decide that some of this information should be
3543 * hidden from users.
3546 sotoxsocket(struct socket *so, struct xsocket *xso)
3549 xso->xso_len = sizeof *xso;
3551 xso->so_type = so->so_type;
3552 xso->so_options = so->so_options;
3553 xso->so_linger = so->so_linger;
3554 xso->so_state = so->so_state;
3555 xso->so_pcb = so->so_pcb;
3556 xso->xso_protocol = so->so_proto->pr_protocol;
3557 xso->xso_family = so->so_proto->pr_domain->dom_family;
3558 xso->so_qlen = so->so_qlen;
3559 xso->so_incqlen = so->so_incqlen;
3560 xso->so_qlimit = so->so_qlimit;
3561 xso->so_timeo = so->so_timeo;
3562 xso->so_error = so->so_error;
3563 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
3564 xso->so_oobmark = so->so_oobmark;
3565 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
3566 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
3567 xso->so_uid = so->so_cred->cr_uid;
3572 * Socket accessor functions to provide external consumers with
3573 * a safe interface to socket state
3578 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg)
3581 TAILQ_FOREACH(so, &so->so_comp, so_list)
3586 so_sockbuf_rcv(struct socket *so)
3589 return (&so->so_rcv);
3593 so_sockbuf_snd(struct socket *so)
3596 return (&so->so_snd);
3600 so_state_get(const struct socket *so)
3603 return (so->so_state);
3607 so_state_set(struct socket *so, int val)
3614 so_options_get(const struct socket *so)
3617 return (so->so_options);
3621 so_options_set(struct socket *so, int val)
3624 so->so_options = val;
3628 so_error_get(const struct socket *so)
3631 return (so->so_error);
3635 so_error_set(struct socket *so, int val)
3642 so_linger_get(const struct socket *so)
3645 return (so->so_linger);
3649 so_linger_set(struct socket *so, int val)
3652 so->so_linger = val;
3656 so_protosw_get(const struct socket *so)
3659 return (so->so_proto);
3663 so_protosw_set(struct socket *so, struct protosw *val)
3670 so_sorwakeup(struct socket *so)
3677 so_sowwakeup(struct socket *so)
3684 so_sorwakeup_locked(struct socket *so)
3687 sorwakeup_locked(so);
3691 so_sowwakeup_locked(struct socket *so)
3694 sowwakeup_locked(so);
3698 so_lock(struct socket *so)
3704 so_unlock(struct socket *so)