2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2004 The FreeBSD Foundation
7 * Copyright (c) 2004-2008 Robert N. M. Watson
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
38 * Comments on the socket life cycle:
40 * soalloc() sets of socket layer state for a socket, called only by
41 * socreate() and sonewconn(). Socket layer private.
43 * sodealloc() tears down socket layer state for a socket, called only by
44 * sofree() and sonewconn(). Socket layer private.
46 * pru_attach() associates protocol layer state with an allocated socket;
47 * called only once, may fail, aborting socket allocation. This is called
48 * from socreate() and sonewconn(). Socket layer private.
50 * pru_detach() disassociates protocol layer state from an attached socket,
51 * and will be called exactly once for sockets in which pru_attach() has
52 * been successfully called. If pru_attach() returned an error,
53 * pru_detach() will not be called. Socket layer private.
55 * pru_abort() and pru_close() notify the protocol layer that the last
56 * consumer of a socket is starting to tear down the socket, and that the
57 * protocol should terminate the connection. Historically, pru_abort() also
58 * detached protocol state from the socket state, but this is no longer the
61 * socreate() creates a socket and attaches protocol state. This is a public
62 * interface that may be used by socket layer consumers to create new
65 * sonewconn() creates a socket and attaches protocol state. This is a
66 * public interface that may be used by protocols to create new sockets when
67 * a new connection is received and will be available for accept() on a
70 * soclose() destroys a socket after possibly waiting for it to disconnect.
71 * This is a public interface that socket consumers should use to close and
72 * release a socket when done with it.
74 * soabort() destroys a socket without waiting for it to disconnect (used
75 * only for incoming connections that are already partially or fully
76 * connected). This is used internally by the socket layer when clearing
77 * listen socket queues (due to overflow or close on the listen socket), but
78 * is also a public interface protocols may use to abort connections in
79 * their incomplete listen queues should they no longer be required. Sockets
80 * placed in completed connection listen queues should not be aborted for
81 * reasons described in the comment above the soclose() implementation. This
82 * is not a general purpose close routine, and except in the specific
83 * circumstances described here, should not be used.
85 * sofree() will free a socket and its protocol state if all references on
86 * the socket have been released, and is the public interface to attempt to
87 * free a socket when a reference is removed. This is a socket layer private
90 * NOTE: In addition to socreate() and soclose(), which provide a single
91 * socket reference to the consumer to be managed as required, there are two
92 * calls to explicitly manage socket references, soref(), and sorele().
93 * Currently, these are generally required only when transitioning a socket
94 * from a listen queue to a file descriptor, in order to prevent garbage
95 * collection of the socket at an untimely moment. For a number of reasons,
96 * these interfaces are not preferred, and should be avoided.
98 * NOTE: With regard to VNETs the general rule is that callers do not set
99 * curvnet. Exceptions to this rule include soabort(), sodisconnect(),
100 * sofree() (and with that sorele(), sotryfree()), as well as sonewconn()
101 * and sorflush(), which are usually called from a pre-set VNET context.
102 * sopoll() currently does not need a VNET context to be set.
105 #include <sys/cdefs.h>
106 __FBSDID("$FreeBSD$");
108 #include "opt_inet.h"
109 #include "opt_inet6.h"
110 #include "opt_kern_tls.h"
111 #include "opt_sctp.h"
113 #include <sys/param.h>
114 #include <sys/systm.h>
115 #include <sys/fcntl.h>
116 #include <sys/limits.h>
117 #include <sys/lock.h>
119 #include <sys/malloc.h>
120 #include <sys/mbuf.h>
121 #include <sys/mutex.h>
122 #include <sys/domain.h>
123 #include <sys/file.h> /* for struct knote */
124 #include <sys/hhook.h>
125 #include <sys/kernel.h>
126 #include <sys/khelp.h>
127 #include <sys/ktls.h>
128 #include <sys/event.h>
129 #include <sys/eventhandler.h>
130 #include <sys/poll.h>
131 #include <sys/proc.h>
132 #include <sys/protosw.h>
133 #include <sys/socket.h>
134 #include <sys/socketvar.h>
135 #include <sys/resourcevar.h>
136 #include <net/route.h>
137 #include <sys/signalvar.h>
138 #include <sys/stat.h>
140 #include <sys/sysctl.h>
141 #include <sys/taskqueue.h>
143 #include <sys/jail.h>
144 #include <sys/syslog.h>
145 #include <netinet/in.h>
146 #include <netinet/tcp.h>
148 #include <net/vnet.h>
150 #include <security/mac/mac_framework.h>
154 #ifdef COMPAT_FREEBSD32
155 #include <sys/mount.h>
156 #include <sys/sysent.h>
157 #include <compat/freebsd32/freebsd32.h>
160 static int soreceive_rcvoob(struct socket *so, struct uio *uio,
162 static void so_rdknl_lock(void *);
163 static void so_rdknl_unlock(void *);
164 static void so_rdknl_assert_locked(void *);
165 static void so_rdknl_assert_unlocked(void *);
166 static void so_wrknl_lock(void *);
167 static void so_wrknl_unlock(void *);
168 static void so_wrknl_assert_locked(void *);
169 static void so_wrknl_assert_unlocked(void *);
171 static void filt_sordetach(struct knote *kn);
172 static int filt_soread(struct knote *kn, long hint);
173 static void filt_sowdetach(struct knote *kn);
174 static int filt_sowrite(struct knote *kn, long hint);
175 static int filt_soempty(struct knote *kn, long hint);
176 static int inline hhook_run_socket(struct socket *so, void *hctx, int32_t h_id);
177 fo_kqfilter_t soo_kqfilter;
179 static struct filterops soread_filtops = {
181 .f_detach = filt_sordetach,
182 .f_event = filt_soread,
184 static struct filterops sowrite_filtops = {
186 .f_detach = filt_sowdetach,
187 .f_event = filt_sowrite,
189 static struct filterops soempty_filtops = {
191 .f_detach = filt_sowdetach,
192 .f_event = filt_soempty,
195 so_gen_t so_gencnt; /* generation count for sockets */
197 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
198 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
200 #define VNET_SO_ASSERT(so) \
201 VNET_ASSERT(curvnet != NULL, \
202 ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so)));
204 VNET_DEFINE(struct hhook_head *, socket_hhh[HHOOK_SOCKET_LAST + 1]);
205 #define V_socket_hhh VNET(socket_hhh)
208 * Limit on the number of connections in the listen queue waiting
210 * NB: The original sysctl somaxconn is still available but hidden
211 * to prevent confusion about the actual purpose of this number.
213 static u_int somaxconn = SOMAXCONN;
216 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
222 error = sysctl_handle_int(oidp, &val, 0, req);
223 if (error || !req->newptr )
227 * The purpose of the UINT_MAX / 3 limit, is so that the formula
229 * below, will not overflow.
232 if (val < 1 || val > UINT_MAX / 3)
238 SYSCTL_PROC(_kern_ipc, OID_AUTO, soacceptqueue, CTLTYPE_UINT | CTLFLAG_RW,
239 0, sizeof(int), sysctl_somaxconn, "I",
240 "Maximum listen socket pending connection accept queue size");
241 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn,
242 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_SKIP,
243 0, sizeof(int), sysctl_somaxconn, "I",
244 "Maximum listen socket pending connection accept queue size (compat)");
246 static int numopensockets;
247 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
248 &numopensockets, 0, "Number of open sockets");
251 * accept_mtx locks down per-socket fields relating to accept queues. See
252 * socketvar.h for an annotation of the protected fields of struct socket.
254 struct mtx accept_mtx;
255 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
258 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
261 static struct mtx so_global_mtx;
262 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
265 * General IPC sysctl name space, used by sockets and a variety of other IPC
268 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
271 * Initialize the socket subsystem and set up the socket
274 static uma_zone_t socket_zone;
278 socket_zone_change(void *tag)
281 maxsockets = uma_zone_set_max(socket_zone, maxsockets);
285 socket_hhook_register(int subtype)
288 if (hhook_head_register(HHOOK_TYPE_SOCKET, subtype,
289 &V_socket_hhh[subtype],
290 HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0)
291 printf("%s: WARNING: unable to register hook\n", __func__);
295 socket_hhook_deregister(int subtype)
298 if (hhook_head_deregister(V_socket_hhh[subtype]) != 0)
299 printf("%s: WARNING: unable to deregister hook\n", __func__);
303 socket_init(void *tag)
306 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
307 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
308 maxsockets = uma_zone_set_max(socket_zone, maxsockets);
309 uma_zone_set_warning(socket_zone, "kern.ipc.maxsockets limit reached");
310 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
311 EVENTHANDLER_PRI_FIRST);
313 SYSINIT(socket, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, socket_init, NULL);
316 socket_vnet_init(const void *unused __unused)
320 /* We expect a contiguous range */
321 for (i = 0; i <= HHOOK_SOCKET_LAST; i++)
322 socket_hhook_register(i);
324 VNET_SYSINIT(socket_vnet_init, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY,
325 socket_vnet_init, NULL);
328 socket_vnet_uninit(const void *unused __unused)
332 for (i = 0; i <= HHOOK_SOCKET_LAST; i++)
333 socket_hhook_deregister(i);
335 VNET_SYSUNINIT(socket_vnet_uninit, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY,
336 socket_vnet_uninit, NULL);
339 * Initialise maxsockets. This SYSINIT must be run after
343 init_maxsockets(void *ignored)
346 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
347 maxsockets = imax(maxsockets, maxfiles);
349 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
352 * Sysctl to get and set the maximum global sockets limit. Notify protocols
353 * of the change so that they can update their dependent limits as required.
356 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
358 int error, newmaxsockets;
360 newmaxsockets = maxsockets;
361 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
362 if (error == 0 && req->newptr) {
363 if (newmaxsockets > maxsockets &&
364 newmaxsockets <= maxfiles) {
365 maxsockets = newmaxsockets;
366 EVENTHANDLER_INVOKE(maxsockets_change);
372 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
373 &maxsockets, 0, sysctl_maxsockets, "IU",
374 "Maximum number of sockets available");
377 * Socket operation routines. These routines are called by the routines in
378 * sys_socket.c or from a system process, and implement the semantics of
379 * socket operations by switching out to the protocol specific routines.
383 * Get a socket structure from our zone, and initialize it. Note that it
384 * would probably be better to allocate socket and PCB at the same time, but
385 * I'm not convinced that all the protocols can be easily modified to do
388 * soalloc() returns a socket with a ref count of 0.
390 static struct socket *
391 soalloc(struct vnet *vnet)
395 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
399 if (mac_socket_init(so, M_NOWAIT) != 0) {
400 uma_zfree(socket_zone, so);
404 if (khelp_init_osd(HELPER_CLASS_SOCKET, &so->osd)) {
405 uma_zfree(socket_zone, so);
410 * The socket locking protocol allows to lock 2 sockets at a time,
411 * however, the first one must be a listening socket. WITNESS lacks
412 * a feature to change class of an existing lock, so we use DUPOK.
414 mtx_init(&so->so_lock, "socket", NULL, MTX_DEF | MTX_DUPOK);
415 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
416 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
417 so->so_rcv.sb_sel = &so->so_rdsel;
418 so->so_snd.sb_sel = &so->so_wrsel;
419 sx_init(&so->so_snd.sb_sx, "so_snd_sx");
420 sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
421 TAILQ_INIT(&so->so_snd.sb_aiojobq);
422 TAILQ_INIT(&so->so_rcv.sb_aiojobq);
423 TASK_INIT(&so->so_snd.sb_aiotask, 0, soaio_snd, so);
424 TASK_INIT(&so->so_rcv.sb_aiotask, 0, soaio_rcv, so);
426 VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p",
427 __func__, __LINE__, so));
430 /* We shouldn't need the so_global_mtx */
431 if (hhook_run_socket(so, NULL, HHOOK_SOCKET_CREATE)) {
432 /* Do we need more comprehensive error returns? */
433 uma_zfree(socket_zone, so);
436 mtx_lock(&so_global_mtx);
437 so->so_gencnt = ++so_gencnt;
440 vnet->vnet_sockcnt++;
442 mtx_unlock(&so_global_mtx);
448 * Free the storage associated with a socket at the socket layer, tear down
449 * locks, labels, etc. All protocol state is assumed already to have been
450 * torn down (and possibly never set up) by the caller.
453 sodealloc(struct socket *so)
456 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
457 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
459 mtx_lock(&so_global_mtx);
460 so->so_gencnt = ++so_gencnt;
461 --numopensockets; /* Could be below, but faster here. */
463 VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p",
464 __func__, __LINE__, so));
465 so->so_vnet->vnet_sockcnt--;
467 mtx_unlock(&so_global_mtx);
469 mac_socket_destroy(so);
471 hhook_run_socket(so, NULL, HHOOK_SOCKET_CLOSE);
474 khelp_destroy_osd(&so->osd);
475 if (SOLISTENING(so)) {
476 if (so->sol_accept_filter != NULL)
477 accept_filt_setopt(so, NULL);
479 if (so->so_rcv.sb_hiwat)
480 (void)chgsbsize(so->so_cred->cr_uidinfo,
481 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
482 if (so->so_snd.sb_hiwat)
483 (void)chgsbsize(so->so_cred->cr_uidinfo,
484 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
485 sx_destroy(&so->so_snd.sb_sx);
486 sx_destroy(&so->so_rcv.sb_sx);
487 SOCKBUF_LOCK_DESTROY(&so->so_snd);
488 SOCKBUF_LOCK_DESTROY(&so->so_rcv);
490 mtx_destroy(&so->so_lock);
491 uma_zfree(socket_zone, so);
495 * socreate returns a socket with a ref count of 1. The socket should be
496 * closed with soclose().
499 socreate(int dom, struct socket **aso, int type, int proto,
500 struct ucred *cred, struct thread *td)
507 prp = pffindproto(dom, proto, type);
509 prp = pffindtype(dom, type);
512 /* No support for domain. */
513 if (pffinddomain(dom) == NULL)
514 return (EAFNOSUPPORT);
515 /* No support for socket type. */
516 if (proto == 0 && type != 0)
518 return (EPROTONOSUPPORT);
520 if (prp->pr_usrreqs->pru_attach == NULL ||
521 prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
522 return (EPROTONOSUPPORT);
524 if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
525 return (EPROTONOSUPPORT);
527 if (prp->pr_type != type)
529 so = soalloc(CRED_TO_VNET(cred));
534 so->so_cred = crhold(cred);
535 if ((prp->pr_domain->dom_family == PF_INET) ||
536 (prp->pr_domain->dom_family == PF_INET6) ||
537 (prp->pr_domain->dom_family == PF_ROUTE))
538 so->so_fibnum = td->td_proc->p_fibnum;
543 mac_socket_create(cred, so);
545 knlist_init(&so->so_rdsel.si_note, so, so_rdknl_lock, so_rdknl_unlock,
546 so_rdknl_assert_locked, so_rdknl_assert_unlocked);
547 knlist_init(&so->so_wrsel.si_note, so, so_wrknl_lock, so_wrknl_unlock,
548 so_wrknl_assert_locked, so_wrknl_assert_unlocked);
550 * Auto-sizing of socket buffers is managed by the protocols and
551 * the appropriate flags must be set in the pru_attach function.
553 CURVNET_SET(so->so_vnet);
554 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
566 static int regression_sonewconn_earlytest = 1;
567 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
568 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
572 * When an attempt at a new connection is noted on a socket which accepts
573 * connections, sonewconn is called. If the connection is possible (subject
574 * to space constraints, etc.) then we allocate a new structure, properly
575 * linked into the data structure of the original socket, and return this.
576 * Connstatus may be 0, or SS_ISCONFIRMING, or SS_ISCONNECTED.
578 * Note: the ref count on the socket is 0 on return.
581 sonewconn(struct socket *head, int connstatus)
583 static struct timeval lastover;
584 static struct timeval overinterval = { 60, 0 };
585 static int overcount;
591 over = (head->sol_qlen > 3 * head->sol_qlimit / 2);
592 SOLISTEN_UNLOCK(head);
594 if (regression_sonewconn_earlytest && over) {
600 if (ratecheck(&lastover, &overinterval)) {
601 log(LOG_DEBUG, "%s: pcb %p: Listen queue overflow: "
602 "%i already in queue awaiting acceptance "
603 "(%d occurrences)\n",
604 __func__, head->so_pcb, head->sol_qlen, overcount);
611 VNET_ASSERT(head->so_vnet != NULL, ("%s: so %p vnet is NULL",
613 so = soalloc(head->so_vnet);
615 log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: "
616 "limit reached or out of memory\n",
617 __func__, head->so_pcb);
620 so->so_listen = head;
621 so->so_type = head->so_type;
622 so->so_linger = head->so_linger;
623 so->so_state = head->so_state | SS_NOFDREF;
624 so->so_fibnum = head->so_fibnum;
625 so->so_proto = head->so_proto;
626 so->so_cred = crhold(head->so_cred);
628 mac_socket_newconn(head, so);
630 knlist_init(&so->so_rdsel.si_note, so, so_rdknl_lock, so_rdknl_unlock,
631 so_rdknl_assert_locked, so_rdknl_assert_unlocked);
632 knlist_init(&so->so_wrsel.si_note, so, so_wrknl_lock, so_wrknl_unlock,
633 so_wrknl_assert_locked, so_wrknl_assert_unlocked);
634 VNET_SO_ASSERT(head);
635 if (soreserve(so, head->sol_sbsnd_hiwat, head->sol_sbrcv_hiwat)) {
637 log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n",
638 __func__, head->so_pcb);
641 if ((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
643 log(LOG_DEBUG, "%s: pcb %p: pru_attach() failed\n",
644 __func__, head->so_pcb);
647 so->so_rcv.sb_lowat = head->sol_sbrcv_lowat;
648 so->so_snd.sb_lowat = head->sol_sbsnd_lowat;
649 so->so_rcv.sb_timeo = head->sol_sbrcv_timeo;
650 so->so_snd.sb_timeo = head->sol_sbsnd_timeo;
651 so->so_rcv.sb_flags |= head->sol_sbrcv_flags & SB_AUTOSIZE;
652 so->so_snd.sb_flags |= head->sol_sbsnd_flags & SB_AUTOSIZE;
655 if (head->sol_accept_filter != NULL)
657 so->so_state |= connstatus;
658 so->so_options = head->so_options & ~SO_ACCEPTCONN;
659 soref(head); /* A socket on (in)complete queue refs head. */
661 TAILQ_INSERT_TAIL(&head->sol_comp, so, so_list);
662 so->so_qstate = SQ_COMP;
664 solisten_wakeup(head); /* unlocks */
667 * Keep removing sockets from the head until there's room for
668 * us to insert on the tail. In pre-locking revisions, this
669 * was a simple if(), but as we could be racing with other
670 * threads and soabort() requires dropping locks, we must
671 * loop waiting for the condition to be true.
673 while (head->sol_incqlen > head->sol_qlimit) {
676 sp = TAILQ_FIRST(&head->sol_incomp);
677 TAILQ_REMOVE(&head->sol_incomp, sp, so_list);
680 sp->so_qstate = SQ_NONE;
681 sp->so_listen = NULL;
683 sorele(head); /* does SOLISTEN_UNLOCK, head stays */
687 TAILQ_INSERT_TAIL(&head->sol_incomp, so, so_list);
688 so->so_qstate = SQ_INCOMP;
690 SOLISTEN_UNLOCK(head);
697 * Socket part of sctp_peeloff(). Detach a new socket from an
698 * association. The new socket is returned with a reference.
701 sopeeloff(struct socket *head)
705 VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
706 __func__, __LINE__, head));
707 so = soalloc(head->so_vnet);
709 log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: "
710 "limit reached or out of memory\n",
711 __func__, head->so_pcb);
714 so->so_type = head->so_type;
715 so->so_options = head->so_options;
716 so->so_linger = head->so_linger;
717 so->so_state = (head->so_state & SS_NBIO) | SS_ISCONNECTED;
718 so->so_fibnum = head->so_fibnum;
719 so->so_proto = head->so_proto;
720 so->so_cred = crhold(head->so_cred);
722 mac_socket_newconn(head, so);
724 knlist_init(&so->so_rdsel.si_note, so, so_rdknl_lock, so_rdknl_unlock,
725 so_rdknl_assert_locked, so_rdknl_assert_unlocked);
726 knlist_init(&so->so_wrsel.si_note, so, so_wrknl_lock, so_wrknl_unlock,
727 so_wrknl_assert_locked, so_wrknl_assert_unlocked);
728 VNET_SO_ASSERT(head);
729 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
731 log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n",
732 __func__, head->so_pcb);
735 if ((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
737 log(LOG_DEBUG, "%s: pcb %p: pru_attach() failed\n",
738 __func__, head->so_pcb);
741 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
742 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
743 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
744 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
745 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
746 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
755 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
759 CURVNET_SET(so->so_vnet);
760 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
766 sobindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
770 CURVNET_SET(so->so_vnet);
771 error = (*so->so_proto->pr_usrreqs->pru_bindat)(fd, so, nam, td);
777 * solisten() transitions a socket from a non-listening state to a listening
778 * state, but can also be used to update the listen queue depth on an
779 * existing listen socket. The protocol will call back into the sockets
780 * layer using solisten_proto_check() and solisten_proto() to check and set
781 * socket-layer listen state. Call backs are used so that the protocol can
782 * acquire both protocol and socket layer locks in whatever order is required
785 * Protocol implementors are advised to hold the socket lock across the
786 * socket-layer test and set to avoid races at the socket layer.
789 solisten(struct socket *so, int backlog, struct thread *td)
793 CURVNET_SET(so->so_vnet);
794 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td);
800 solisten_proto_check(struct socket *so)
803 SOCK_LOCK_ASSERT(so);
805 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
812 solisten_proto(struct socket *so, int backlog)
814 int sbrcv_lowat, sbsnd_lowat;
815 u_int sbrcv_hiwat, sbsnd_hiwat;
816 short sbrcv_flags, sbsnd_flags;
817 sbintime_t sbrcv_timeo, sbsnd_timeo;
819 SOCK_LOCK_ASSERT(so);
825 * Change this socket to listening state.
827 sbrcv_lowat = so->so_rcv.sb_lowat;
828 sbsnd_lowat = so->so_snd.sb_lowat;
829 sbrcv_hiwat = so->so_rcv.sb_hiwat;
830 sbsnd_hiwat = so->so_snd.sb_hiwat;
831 sbrcv_flags = so->so_rcv.sb_flags;
832 sbsnd_flags = so->so_snd.sb_flags;
833 sbrcv_timeo = so->so_rcv.sb_timeo;
834 sbsnd_timeo = so->so_snd.sb_timeo;
836 sbdestroy(&so->so_snd, so);
837 sbdestroy(&so->so_rcv, so);
838 sx_destroy(&so->so_snd.sb_sx);
839 sx_destroy(&so->so_rcv.sb_sx);
840 SOCKBUF_LOCK_DESTROY(&so->so_snd);
841 SOCKBUF_LOCK_DESTROY(&so->so_rcv);
845 sizeof(struct socket) - offsetof(struct socket, so_rcv));
848 so->sol_sbrcv_lowat = sbrcv_lowat;
849 so->sol_sbsnd_lowat = sbsnd_lowat;
850 so->sol_sbrcv_hiwat = sbrcv_hiwat;
851 so->sol_sbsnd_hiwat = sbsnd_hiwat;
852 so->sol_sbrcv_flags = sbrcv_flags;
853 so->sol_sbsnd_flags = sbsnd_flags;
854 so->sol_sbrcv_timeo = sbrcv_timeo;
855 so->sol_sbsnd_timeo = sbsnd_timeo;
857 so->sol_qlen = so->sol_incqlen = 0;
858 TAILQ_INIT(&so->sol_incomp);
859 TAILQ_INIT(&so->sol_comp);
861 so->sol_accept_filter = NULL;
862 so->sol_accept_filter_arg = NULL;
863 so->sol_accept_filter_str = NULL;
865 so->sol_upcall = NULL;
866 so->sol_upcallarg = NULL;
868 so->so_options |= SO_ACCEPTCONN;
871 if (backlog < 0 || backlog > somaxconn)
873 so->sol_qlimit = backlog;
877 * Wakeup listeners/subsystems once we have a complete connection.
878 * Enters with lock, returns unlocked.
881 solisten_wakeup(struct socket *sol)
884 if (sol->sol_upcall != NULL)
885 (void )sol->sol_upcall(sol, sol->sol_upcallarg, M_NOWAIT);
887 selwakeuppri(&sol->so_rdsel, PSOCK);
888 KNOTE_LOCKED(&sol->so_rdsel.si_note, 0);
890 SOLISTEN_UNLOCK(sol);
891 wakeup_one(&sol->sol_comp);
892 if ((sol->so_state & SS_ASYNC) && sol->so_sigio != NULL)
893 pgsigio(&sol->so_sigio, SIGIO, 0);
897 * Return single connection off a listening socket queue. Main consumer of
898 * the function is kern_accept4(). Some modules, that do their own accept
899 * management also use the function.
901 * Listening socket must be locked on entry and is returned unlocked on
903 * The flags argument is set of accept4(2) flags and ACCEPT4_INHERIT.
906 solisten_dequeue(struct socket *head, struct socket **ret, int flags)
911 SOLISTEN_LOCK_ASSERT(head);
913 while (!(head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->sol_comp) &&
914 head->so_error == 0) {
915 error = msleep(&head->sol_comp, &head->so_lock, PSOCK | PCATCH,
918 SOLISTEN_UNLOCK(head);
922 if (head->so_error) {
923 error = head->so_error;
925 } else if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->sol_comp))
930 SOLISTEN_UNLOCK(head);
933 so = TAILQ_FIRST(&head->sol_comp);
935 KASSERT(so->so_qstate == SQ_COMP,
936 ("%s: so %p not SQ_COMP", __func__, so));
939 so->so_qstate = SQ_NONE;
940 so->so_listen = NULL;
941 TAILQ_REMOVE(&head->sol_comp, so, so_list);
942 if (flags & ACCEPT4_INHERIT)
943 so->so_state |= (head->so_state & SS_NBIO);
945 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
954 * Evaluate the reference count and named references on a socket; if no
955 * references remain, free it. This should be called whenever a reference is
956 * released, such as in sorele(), but also when named reference flags are
957 * cleared in socket or protocol code.
959 * sofree() will free the socket if:
961 * - There are no outstanding file descriptor references or related consumers
964 * - The socket has been closed by user space, if ever open (SS_NOFDREF).
966 * - The protocol does not have an outstanding strong reference on the socket
969 * - The socket is not in a completed connection queue, so a process has been
970 * notified that it is present. If it is removed, the user process may
971 * block in accept() despite select() saying the socket was ready.
974 sofree(struct socket *so)
976 struct protosw *pr = so->so_proto;
978 SOCK_LOCK_ASSERT(so);
980 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
981 (so->so_state & SS_PROTOREF) || (so->so_qstate == SQ_COMP)) {
986 if (!SOLISTENING(so) && so->so_qstate == SQ_INCOMP) {
990 KASSERT(sol, ("%s: so %p on incomp of NULL", __func__, so));
993 * To solve race between close of a listening socket and
994 * a socket on its incomplete queue, we need to lock both.
995 * The order is first listening socket, then regular.
996 * Since we don't have SS_NOFDREF neither SS_PROTOREF, this
997 * function and the listening socket are the only pointers
998 * to so. To preserve so and sol, we reference both and then
1000 * After relock the socket may not move to so_comp since it
1001 * doesn't have PCB already, but it may be removed from
1002 * so_incomp. If that happens, we share responsiblity on
1003 * freeing the socket, but soclose() has already removed
1011 if (so->so_qstate == SQ_INCOMP) {
1012 KASSERT(so->so_listen == sol,
1013 ("%s: so %p migrated out of sol %p",
1014 __func__, so, sol));
1015 TAILQ_REMOVE(&sol->sol_incomp, so, so_list);
1017 /* This is guarenteed not to be the last. */
1018 refcount_release(&sol->so_count);
1019 so->so_qstate = SQ_NONE;
1020 so->so_listen = NULL;
1022 KASSERT(so->so_listen == NULL,
1023 ("%s: so %p not on (in)comp with so_listen",
1026 KASSERT(so->so_count == 1,
1027 ("%s: so %p count %u", __func__, so, so->so_count));
1030 if (SOLISTENING(so))
1031 so->so_error = ECONNABORTED;
1034 if (so->so_dtor != NULL)
1038 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
1039 (*pr->pr_domain->dom_dispose)(so);
1040 if (pr->pr_usrreqs->pru_detach != NULL)
1041 (*pr->pr_usrreqs->pru_detach)(so);
1044 * From this point on, we assume that no other references to this
1045 * socket exist anywhere else in the stack. Therefore, no locks need
1046 * to be acquired or held.
1048 * We used to do a lot of socket buffer and socket locking here, as
1049 * well as invoke sorflush() and perform wakeups. The direct call to
1050 * dom_dispose() and sbdestroy() are an inlining of what was
1051 * necessary from sorflush().
1053 * Notice that the socket buffer and kqueue state are torn down
1054 * before calling pru_detach. This means that protocols shold not
1055 * assume they can perform socket wakeups, etc, in their detach code.
1057 if (!SOLISTENING(so)) {
1058 sbdestroy(&so->so_snd, so);
1059 sbdestroy(&so->so_rcv, so);
1061 seldrain(&so->so_rdsel);
1062 seldrain(&so->so_wrsel);
1063 knlist_destroy(&so->so_rdsel.si_note);
1064 knlist_destroy(&so->so_wrsel.si_note);
1069 * Close a socket on last file table reference removal. Initiate disconnect
1070 * if connected. Free socket when disconnect complete.
1072 * This function will sorele() the socket. Note that soclose() may be called
1073 * prior to the ref count reaching zero. The actual socket structure will
1074 * not be freed until the ref count reaches zero.
1077 soclose(struct socket *so)
1079 struct accept_queue lqueue;
1083 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
1085 CURVNET_SET(so->so_vnet);
1086 funsetown(&so->so_sigio);
1087 if (so->so_state & SS_ISCONNECTED) {
1088 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
1089 error = sodisconnect(so);
1091 if (error == ENOTCONN)
1096 if (so->so_options & SO_LINGER) {
1097 if ((so->so_state & SS_ISDISCONNECTING) &&
1098 (so->so_state & SS_NBIO))
1100 while (so->so_state & SS_ISCONNECTED) {
1101 error = tsleep(&so->so_timeo,
1102 PSOCK | PCATCH, "soclos",
1103 so->so_linger * hz);
1111 if (so->so_proto->pr_usrreqs->pru_close != NULL)
1112 (*so->so_proto->pr_usrreqs->pru_close)(so);
1115 if ((listening = (so->so_options & SO_ACCEPTCONN))) {
1118 TAILQ_INIT(&lqueue);
1119 TAILQ_SWAP(&lqueue, &so->sol_incomp, socket, so_list);
1120 TAILQ_CONCAT(&lqueue, &so->sol_comp, so_list);
1122 so->sol_qlen = so->sol_incqlen = 0;
1124 TAILQ_FOREACH(sp, &lqueue, so_list) {
1126 sp->so_qstate = SQ_NONE;
1127 sp->so_listen = NULL;
1129 /* Guaranteed not to be the last. */
1130 refcount_release(&so->so_count);
1133 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
1134 so->so_state |= SS_NOFDREF;
1137 struct socket *sp, *tsp;
1139 TAILQ_FOREACH_SAFE(sp, &lqueue, so_list, tsp) {
1141 if (sp->so_count == 0) {
1145 /* sp is now in sofree() */
1154 * soabort() is used to abruptly tear down a connection, such as when a
1155 * resource limit is reached (listen queue depth exceeded), or if a listen
1156 * socket is closed while there are sockets waiting to be accepted.
1158 * This interface is tricky, because it is called on an unreferenced socket,
1159 * and must be called only by a thread that has actually removed the socket
1160 * from the listen queue it was on, or races with other threads are risked.
1162 * This interface will call into the protocol code, so must not be called
1163 * with any socket locks held. Protocols do call it while holding their own
1164 * recursible protocol mutexes, but this is something that should be subject
1165 * to review in the future.
1168 soabort(struct socket *so)
1172 * In as much as is possible, assert that no references to this
1173 * socket are held. This is not quite the same as asserting that the
1174 * current thread is responsible for arranging for no references, but
1175 * is as close as we can get for now.
1177 KASSERT(so->so_count == 0, ("soabort: so_count"));
1178 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
1179 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
1182 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
1183 (*so->so_proto->pr_usrreqs->pru_abort)(so);
1189 soaccept(struct socket *so, struct sockaddr **nam)
1194 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
1195 so->so_state &= ~SS_NOFDREF;
1198 CURVNET_SET(so->so_vnet);
1199 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
1205 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
1208 return (soconnectat(AT_FDCWD, so, nam, td));
1212 soconnectat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
1216 if (so->so_options & SO_ACCEPTCONN)
1217 return (EOPNOTSUPP);
1219 CURVNET_SET(so->so_vnet);
1221 * If protocol is connection-based, can only connect once.
1222 * Otherwise, if connected, try to disconnect first. This allows
1223 * user to disconnect by connecting to, e.g., a null address.
1225 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
1226 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
1227 (error = sodisconnect(so)))) {
1231 * Prevent accumulated error from previous connection from
1235 if (fd == AT_FDCWD) {
1236 error = (*so->so_proto->pr_usrreqs->pru_connect)(so,
1239 error = (*so->so_proto->pr_usrreqs->pru_connectat)(fd,
1249 soconnect2(struct socket *so1, struct socket *so2)
1253 CURVNET_SET(so1->so_vnet);
1254 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
1260 sodisconnect(struct socket *so)
1264 if ((so->so_state & SS_ISCONNECTED) == 0)
1266 if (so->so_state & SS_ISDISCONNECTING)
1269 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
1273 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
1276 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
1277 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1281 int clen = 0, error, dontroute;
1283 KASSERT(so->so_type == SOCK_DGRAM, ("sosend_dgram: !SOCK_DGRAM"));
1284 KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
1285 ("sosend_dgram: !PR_ATOMIC"));
1288 resid = uio->uio_resid;
1290 resid = top->m_pkthdr.len;
1292 * In theory resid should be unsigned. However, space must be
1293 * signed, as it might be less than 0 if we over-committed, and we
1294 * must use a signed comparison of space and resid. On the other
1295 * hand, a negative resid causes us to loop sending 0-length
1296 * segments to the protocol.
1304 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
1306 td->td_ru.ru_msgsnd++;
1307 if (control != NULL)
1308 clen = control->m_len;
1310 SOCKBUF_LOCK(&so->so_snd);
1311 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1312 SOCKBUF_UNLOCK(&so->so_snd);
1317 error = so->so_error;
1319 SOCKBUF_UNLOCK(&so->so_snd);
1322 if ((so->so_state & SS_ISCONNECTED) == 0) {
1324 * `sendto' and `sendmsg' is allowed on a connection-based
1325 * socket if it supports implied connect. Return ENOTCONN if
1326 * not connected and no address is supplied.
1328 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1329 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1330 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1331 !(resid == 0 && clen != 0)) {
1332 SOCKBUF_UNLOCK(&so->so_snd);
1336 } else if (addr == NULL) {
1337 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1340 error = EDESTADDRREQ;
1341 SOCKBUF_UNLOCK(&so->so_snd);
1347 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
1348 * problem and need fixing.
1350 space = sbspace(&so->so_snd);
1351 if (flags & MSG_OOB)
1354 SOCKBUF_UNLOCK(&so->so_snd);
1355 if (resid > space) {
1361 if (flags & MSG_EOR)
1362 top->m_flags |= M_EOR;
1365 * Copy the data from userland into a mbuf chain.
1366 * If no data is to be copied in, a single empty mbuf
1369 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1370 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1372 error = EFAULT; /* only possible error */
1375 space -= resid - uio->uio_resid;
1376 resid = uio->uio_resid;
1378 KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1380 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1385 so->so_options |= SO_DONTROUTE;
1389 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1390 * of date. We could have received a reset packet in an interrupt or
1391 * maybe we slept while doing page faults in uiomove() etc. We could
1392 * probably recheck again inside the locking protection here, but
1393 * there are probably other places that this also happens. We must
1397 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1398 (flags & MSG_OOB) ? PRUS_OOB :
1400 * If the user set MSG_EOF, the protocol understands this flag and
1401 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1403 ((flags & MSG_EOF) &&
1404 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1407 /* If there is more to send set PRUS_MORETOCOME */
1408 (flags & MSG_MORETOCOME) ||
1409 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1410 top, addr, control, td);
1413 so->so_options &= ~SO_DONTROUTE;
1422 if (control != NULL)
1428 * Send on a socket. If send must go all at once and message is larger than
1429 * send buffering, then hard error. Lock against other senders. If must go
1430 * all at once and not enough room now, then inform user that this would
1431 * block and do nothing. Otherwise, if nonblocking, send as much as
1432 * possible. The data to be sent is described by "uio" if nonzero, otherwise
1433 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1434 * in mbuf chain must be small enough to send all at once.
1436 * Returns nonzero on error, timeout or signal; callers must check for short
1437 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1441 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1442 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1446 int clen = 0, error, dontroute;
1447 int atomic = sosendallatonce(so) || top;
1450 struct ktls_session *tls;
1451 int tls_enq_cnt, tls_pruflag;
1455 tls_rtype = TLS_RLTYPE_APP;
1458 resid = uio->uio_resid;
1460 resid = top->m_pkthdr.len;
1462 * In theory resid should be unsigned. However, space must be
1463 * signed, as it might be less than 0 if we over-committed, and we
1464 * must use a signed comparison of space and resid. On the other
1465 * hand, a negative resid causes us to loop sending 0-length
1466 * segments to the protocol.
1468 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1469 * type sockets since that's an error.
1471 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1477 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1478 (so->so_proto->pr_flags & PR_ATOMIC);
1480 td->td_ru.ru_msgsnd++;
1481 if (control != NULL)
1482 clen = control->m_len;
1484 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1490 tls = ktls_hold(so->so_snd.sb_tls_info);
1492 if (tls->sw_encrypt != NULL)
1493 tls_pruflag = PRUS_NOTREADY;
1495 if (control != NULL) {
1496 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1498 if (clen >= sizeof(*cm) &&
1499 cm->cmsg_type == TLS_SET_RECORD_TYPE) {
1500 tls_rtype = *((uint8_t *)CMSG_DATA(cm));
1512 SOCKBUF_LOCK(&so->so_snd);
1513 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1514 SOCKBUF_UNLOCK(&so->so_snd);
1519 error = so->so_error;
1521 SOCKBUF_UNLOCK(&so->so_snd);
1524 if ((so->so_state & SS_ISCONNECTED) == 0) {
1526 * `sendto' and `sendmsg' is allowed on a connection-
1527 * based socket if it supports implied connect.
1528 * Return ENOTCONN if not connected and no address is
1531 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1532 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1533 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1534 !(resid == 0 && clen != 0)) {
1535 SOCKBUF_UNLOCK(&so->so_snd);
1539 } else if (addr == NULL) {
1540 SOCKBUF_UNLOCK(&so->so_snd);
1541 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1544 error = EDESTADDRREQ;
1548 space = sbspace(&so->so_snd);
1549 if (flags & MSG_OOB)
1551 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1552 clen > so->so_snd.sb_hiwat) {
1553 SOCKBUF_UNLOCK(&so->so_snd);
1557 if (space < resid + clen &&
1558 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1559 if ((so->so_state & SS_NBIO) ||
1560 (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
1561 SOCKBUF_UNLOCK(&so->so_snd);
1562 error = EWOULDBLOCK;
1565 error = sbwait(&so->so_snd);
1566 SOCKBUF_UNLOCK(&so->so_snd);
1571 SOCKBUF_UNLOCK(&so->so_snd);
1576 if (flags & MSG_EOR)
1577 top->m_flags |= M_EOR;
1580 * Copy the data from userland into a mbuf
1581 * chain. If resid is 0, which can happen
1582 * only if we have control to send, then
1583 * a single empty mbuf is returned. This
1584 * is a workaround to prevent protocol send
1589 top = m_uiotombuf(uio, M_WAITOK, space,
1590 tls->params.max_frame_len,
1592 ((flags & MSG_EOR) ? M_EOR : 0));
1594 error = ktls_frame(top, tls,
1595 &tls_enq_cnt, tls_rtype);
1601 tls_rtype = TLS_RLTYPE_APP;
1604 top = m_uiotombuf(uio, M_WAITOK, space,
1605 (atomic ? max_hdr : 0),
1606 (atomic ? M_PKTHDR : 0) |
1607 ((flags & MSG_EOR) ? M_EOR : 0));
1609 error = EFAULT; /* only possible error */
1612 space -= resid - uio->uio_resid;
1613 resid = uio->uio_resid;
1617 so->so_options |= SO_DONTROUTE;
1621 * XXX all the SBS_CANTSENDMORE checks previously
1622 * done could be out of date. We could have received
1623 * a reset packet in an interrupt or maybe we slept
1624 * while doing page faults in uiomove() etc. We
1625 * could probably recheck again inside the locking
1626 * protection here, but there are probably other
1627 * places that this also happens. We must rethink
1632 pru_flag = (flags & MSG_OOB) ? PRUS_OOB :
1634 * If the user set MSG_EOF, the protocol understands
1635 * this flag and nothing left to send then use
1636 * PRU_SEND_EOF instead of PRU_SEND.
1638 ((flags & MSG_EOF) &&
1639 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1642 /* If there is more to send set PRUS_MORETOCOME. */
1643 (flags & MSG_MORETOCOME) ||
1644 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0;
1647 pru_flag |= tls_pruflag;
1650 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1651 pru_flag, top, addr, control, td);
1655 so->so_options &= ~SO_DONTROUTE;
1660 if (tls != NULL && tls->sw_encrypt != NULL) {
1662 * Note that error is intentionally
1665 * Like sendfile(), we rely on the
1666 * completion routine (pru_ready())
1667 * to free the mbufs in the event that
1668 * pru_send() encountered an error and
1669 * did not append them to the sockbuf.
1672 ktls_enqueue(top, so, tls_enq_cnt);
1680 } while (resid && space > 0);
1684 sbunlock(&so->so_snd);
1692 if (control != NULL)
1698 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1699 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1703 CURVNET_SET(so->so_vnet);
1704 if (!SOLISTENING(so))
1705 error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio,
1706 top, control, flags, td);
1717 * The part of soreceive() that implements reading non-inline out-of-band
1718 * data from a socket. For more complete comments, see soreceive(), from
1719 * which this code originated.
1721 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1722 * unable to return an mbuf chain to the caller.
1725 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1727 struct protosw *pr = so->so_proto;
1731 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1734 m = m_get(M_WAITOK, MT_DATA);
1735 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1739 error = uiomove(mtod(m, void *),
1740 (int) min(uio->uio_resid, m->m_len), uio);
1742 } while (uio->uio_resid && error == 0 && m);
1750 * Following replacement or removal of the first mbuf on the first mbuf chain
1751 * of a socket buffer, push necessary state changes back into the socket
1752 * buffer so that other consumers see the values consistently. 'nextrecord'
1753 * is the callers locally stored value of the original value of
1754 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1755 * NOTE: 'nextrecord' may be NULL.
1757 static __inline void
1758 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1761 SOCKBUF_LOCK_ASSERT(sb);
1763 * First, update for the new value of nextrecord. If necessary, make
1764 * it the first record.
1766 if (sb->sb_mb != NULL)
1767 sb->sb_mb->m_nextpkt = nextrecord;
1769 sb->sb_mb = nextrecord;
1772 * Now update any dependent socket buffer fields to reflect the new
1773 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
1774 * addition of a second clause that takes care of the case where
1775 * sb_mb has been updated, but remains the last record.
1777 if (sb->sb_mb == NULL) {
1778 sb->sb_mbtail = NULL;
1779 sb->sb_lastrecord = NULL;
1780 } else if (sb->sb_mb->m_nextpkt == NULL)
1781 sb->sb_lastrecord = sb->sb_mb;
1785 * Implement receive operations on a socket. We depend on the way that
1786 * records are added to the sockbuf by sbappend. In particular, each record
1787 * (mbufs linked through m_next) must begin with an address if the protocol
1788 * so specifies, followed by an optional mbuf or mbufs containing ancillary
1789 * data, and then zero or more mbufs of data. In order to allow parallelism
1790 * between network receive and copying to user space, as well as avoid
1791 * sleeping with a mutex held, we release the socket buffer mutex during the
1792 * user space copy. Although the sockbuf is locked, new data may still be
1793 * appended, and thus we must maintain consistency of the sockbuf during that
1796 * The caller may receive the data as a single mbuf chain by supplying an
1797 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1798 * the count in uio_resid.
1801 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1802 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1804 struct mbuf *m, **mp;
1805 int flags, error, offset;
1807 struct protosw *pr = so->so_proto;
1808 struct mbuf *nextrecord;
1810 ssize_t orig_resid = uio->uio_resid;
1815 if (controlp != NULL)
1818 flags = *flagsp &~ MSG_EOR;
1821 if (flags & MSG_OOB)
1822 return (soreceive_rcvoob(so, uio, flags));
1825 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1826 && uio->uio_resid) {
1828 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1831 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1836 SOCKBUF_LOCK(&so->so_rcv);
1837 m = so->so_rcv.sb_mb;
1839 * If we have less data than requested, block awaiting more (subject
1840 * to any timeout) if:
1841 * 1. the current count is less than the low water mark, or
1842 * 2. MSG_DONTWAIT is not set
1844 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1845 sbavail(&so->so_rcv) < uio->uio_resid) &&
1846 sbavail(&so->so_rcv) < so->so_rcv.sb_lowat &&
1847 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1848 KASSERT(m != NULL || !sbavail(&so->so_rcv),
1849 ("receive: m == %p sbavail == %u",
1850 m, sbavail(&so->so_rcv)));
1854 error = so->so_error;
1855 if ((flags & MSG_PEEK) == 0)
1857 SOCKBUF_UNLOCK(&so->so_rcv);
1860 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1861 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1863 SOCKBUF_UNLOCK(&so->so_rcv);
1868 for (; m != NULL; m = m->m_next)
1869 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1870 m = so->so_rcv.sb_mb;
1873 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1874 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1875 SOCKBUF_UNLOCK(&so->so_rcv);
1879 if (uio->uio_resid == 0) {
1880 SOCKBUF_UNLOCK(&so->so_rcv);
1883 if ((so->so_state & SS_NBIO) ||
1884 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1885 SOCKBUF_UNLOCK(&so->so_rcv);
1886 error = EWOULDBLOCK;
1889 SBLASTRECORDCHK(&so->so_rcv);
1890 SBLASTMBUFCHK(&so->so_rcv);
1891 error = sbwait(&so->so_rcv);
1892 SOCKBUF_UNLOCK(&so->so_rcv);
1899 * From this point onward, we maintain 'nextrecord' as a cache of the
1900 * pointer to the next record in the socket buffer. We must keep the
1901 * various socket buffer pointers and local stack versions of the
1902 * pointers in sync, pushing out modifications before dropping the
1903 * socket buffer mutex, and re-reading them when picking it up.
1905 * Otherwise, we will race with the network stack appending new data
1906 * or records onto the socket buffer by using inconsistent/stale
1907 * versions of the field, possibly resulting in socket buffer
1910 * By holding the high-level sblock(), we prevent simultaneous
1911 * readers from pulling off the front of the socket buffer.
1913 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1915 uio->uio_td->td_ru.ru_msgrcv++;
1916 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1917 SBLASTRECORDCHK(&so->so_rcv);
1918 SBLASTMBUFCHK(&so->so_rcv);
1919 nextrecord = m->m_nextpkt;
1920 if (pr->pr_flags & PR_ADDR) {
1921 KASSERT(m->m_type == MT_SONAME,
1922 ("m->m_type == %d", m->m_type));
1925 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1927 if (flags & MSG_PEEK) {
1930 sbfree(&so->so_rcv, m);
1931 so->so_rcv.sb_mb = m_free(m);
1932 m = so->so_rcv.sb_mb;
1933 sockbuf_pushsync(&so->so_rcv, nextrecord);
1938 * Process one or more MT_CONTROL mbufs present before any data mbufs
1939 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1940 * just copy the data; if !MSG_PEEK, we call into the protocol to
1941 * perform externalization (or freeing if controlp == NULL).
1943 if (m != NULL && m->m_type == MT_CONTROL) {
1944 struct mbuf *cm = NULL, *cmn;
1945 struct mbuf **cme = &cm;
1948 if (flags & MSG_PEEK) {
1949 if (controlp != NULL) {
1950 *controlp = m_copym(m, 0, m->m_len,
1952 controlp = &(*controlp)->m_next;
1956 sbfree(&so->so_rcv, m);
1957 so->so_rcv.sb_mb = m->m_next;
1960 cme = &(*cme)->m_next;
1961 m = so->so_rcv.sb_mb;
1963 } while (m != NULL && m->m_type == MT_CONTROL);
1964 if ((flags & MSG_PEEK) == 0)
1965 sockbuf_pushsync(&so->so_rcv, nextrecord);
1966 while (cm != NULL) {
1969 if (pr->pr_domain->dom_externalize != NULL) {
1970 SOCKBUF_UNLOCK(&so->so_rcv);
1972 error = (*pr->pr_domain->dom_externalize)
1973 (cm, controlp, flags);
1974 SOCKBUF_LOCK(&so->so_rcv);
1975 } else if (controlp != NULL)
1979 if (controlp != NULL) {
1981 while (*controlp != NULL)
1982 controlp = &(*controlp)->m_next;
1987 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1989 nextrecord = so->so_rcv.sb_mb;
1993 if ((flags & MSG_PEEK) == 0) {
1994 KASSERT(m->m_nextpkt == nextrecord,
1995 ("soreceive: post-control, nextrecord !sync"));
1996 if (nextrecord == NULL) {
1997 KASSERT(so->so_rcv.sb_mb == m,
1998 ("soreceive: post-control, sb_mb!=m"));
1999 KASSERT(so->so_rcv.sb_lastrecord == m,
2000 ("soreceive: post-control, lastrecord!=m"));
2004 if (type == MT_OOBDATA)
2007 if ((flags & MSG_PEEK) == 0) {
2008 KASSERT(so->so_rcv.sb_mb == nextrecord,
2009 ("soreceive: sb_mb != nextrecord"));
2010 if (so->so_rcv.sb_mb == NULL) {
2011 KASSERT(so->so_rcv.sb_lastrecord == NULL,
2012 ("soreceive: sb_lastercord != NULL"));
2016 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2017 SBLASTRECORDCHK(&so->so_rcv);
2018 SBLASTMBUFCHK(&so->so_rcv);
2021 * Now continue to read any data mbufs off of the head of the socket
2022 * buffer until the read request is satisfied. Note that 'type' is
2023 * used to store the type of any mbuf reads that have happened so far
2024 * such that soreceive() can stop reading if the type changes, which
2025 * causes soreceive() to return only one of regular data and inline
2026 * out-of-band data in a single socket receive operation.
2030 while (m != NULL && !(m->m_flags & M_NOTAVAIL) && uio->uio_resid > 0
2033 * If the type of mbuf has changed since the last mbuf
2034 * examined ('type'), end the receive operation.
2036 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2037 if (m->m_type == MT_OOBDATA || m->m_type == MT_CONTROL) {
2038 if (type != m->m_type)
2040 } else if (type == MT_OOBDATA)
2043 KASSERT(m->m_type == MT_DATA,
2044 ("m->m_type == %d", m->m_type));
2045 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
2046 len = uio->uio_resid;
2047 if (so->so_oobmark && len > so->so_oobmark - offset)
2048 len = so->so_oobmark - offset;
2049 if (len > m->m_len - moff)
2050 len = m->m_len - moff;
2052 * If mp is set, just pass back the mbufs. Otherwise copy
2053 * them out via the uio, then free. Sockbuf must be
2054 * consistent here (points to current mbuf, it points to next
2055 * record) when we drop priority; we must note any additions
2056 * to the sockbuf when we block interrupts again.
2059 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2060 SBLASTRECORDCHK(&so->so_rcv);
2061 SBLASTMBUFCHK(&so->so_rcv);
2062 SOCKBUF_UNLOCK(&so->so_rcv);
2063 if ((m->m_flags & M_NOMAP) != 0)
2064 error = m_unmappedtouio(m, moff, uio, (int)len);
2066 error = uiomove(mtod(m, char *) + moff,
2068 SOCKBUF_LOCK(&so->so_rcv);
2071 * The MT_SONAME mbuf has already been removed
2072 * from the record, so it is necessary to
2073 * remove the data mbufs, if any, to preserve
2074 * the invariant in the case of PR_ADDR that
2075 * requires MT_SONAME mbufs at the head of
2078 if (pr->pr_flags & PR_ATOMIC &&
2079 ((flags & MSG_PEEK) == 0))
2080 (void)sbdroprecord_locked(&so->so_rcv);
2081 SOCKBUF_UNLOCK(&so->so_rcv);
2085 uio->uio_resid -= len;
2086 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2087 if (len == m->m_len - moff) {
2088 if (m->m_flags & M_EOR)
2090 if (flags & MSG_PEEK) {
2094 nextrecord = m->m_nextpkt;
2095 sbfree(&so->so_rcv, m);
2097 m->m_nextpkt = NULL;
2100 so->so_rcv.sb_mb = m = m->m_next;
2103 so->so_rcv.sb_mb = m_free(m);
2104 m = so->so_rcv.sb_mb;
2106 sockbuf_pushsync(&so->so_rcv, nextrecord);
2107 SBLASTRECORDCHK(&so->so_rcv);
2108 SBLASTMBUFCHK(&so->so_rcv);
2111 if (flags & MSG_PEEK)
2115 if (flags & MSG_DONTWAIT) {
2116 *mp = m_copym(m, 0, len,
2120 * m_copym() couldn't
2122 * Adjust uio_resid back
2124 * down by len bytes,
2125 * which we didn't end
2126 * up "copying" over).
2128 uio->uio_resid += len;
2132 SOCKBUF_UNLOCK(&so->so_rcv);
2133 *mp = m_copym(m, 0, len,
2135 SOCKBUF_LOCK(&so->so_rcv);
2138 sbcut_locked(&so->so_rcv, len);
2141 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2142 if (so->so_oobmark) {
2143 if ((flags & MSG_PEEK) == 0) {
2144 so->so_oobmark -= len;
2145 if (so->so_oobmark == 0) {
2146 so->so_rcv.sb_state |= SBS_RCVATMARK;
2151 if (offset == so->so_oobmark)
2155 if (flags & MSG_EOR)
2158 * If the MSG_WAITALL flag is set (for non-atomic socket), we
2159 * must not quit until "uio->uio_resid == 0" or an error
2160 * termination. If a signal/timeout occurs, return with a
2161 * short count but without error. Keep sockbuf locked
2162 * against other readers.
2164 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
2165 !sosendallatonce(so) && nextrecord == NULL) {
2166 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2168 so->so_rcv.sb_state & SBS_CANTRCVMORE)
2171 * Notify the protocol that some data has been
2172 * drained before blocking.
2174 if (pr->pr_flags & PR_WANTRCVD) {
2175 SOCKBUF_UNLOCK(&so->so_rcv);
2177 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
2178 SOCKBUF_LOCK(&so->so_rcv);
2180 SBLASTRECORDCHK(&so->so_rcv);
2181 SBLASTMBUFCHK(&so->so_rcv);
2183 * We could receive some data while was notifying
2184 * the protocol. Skip blocking in this case.
2186 if (so->so_rcv.sb_mb == NULL) {
2187 error = sbwait(&so->so_rcv);
2189 SOCKBUF_UNLOCK(&so->so_rcv);
2193 m = so->so_rcv.sb_mb;
2195 nextrecord = m->m_nextpkt;
2199 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2200 if (m != NULL && pr->pr_flags & PR_ATOMIC) {
2202 if ((flags & MSG_PEEK) == 0)
2203 (void) sbdroprecord_locked(&so->so_rcv);
2205 if ((flags & MSG_PEEK) == 0) {
2208 * First part is an inline SB_EMPTY_FIXUP(). Second
2209 * part makes sure sb_lastrecord is up-to-date if
2210 * there is still data in the socket buffer.
2212 so->so_rcv.sb_mb = nextrecord;
2213 if (so->so_rcv.sb_mb == NULL) {
2214 so->so_rcv.sb_mbtail = NULL;
2215 so->so_rcv.sb_lastrecord = NULL;
2216 } else if (nextrecord->m_nextpkt == NULL)
2217 so->so_rcv.sb_lastrecord = nextrecord;
2219 SBLASTRECORDCHK(&so->so_rcv);
2220 SBLASTMBUFCHK(&so->so_rcv);
2222 * If soreceive() is being done from the socket callback,
2223 * then don't need to generate ACK to peer to update window,
2224 * since ACK will be generated on return to TCP.
2226 if (!(flags & MSG_SOCALLBCK) &&
2227 (pr->pr_flags & PR_WANTRCVD)) {
2228 SOCKBUF_UNLOCK(&so->so_rcv);
2230 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
2231 SOCKBUF_LOCK(&so->so_rcv);
2234 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2235 if (orig_resid == uio->uio_resid && orig_resid &&
2236 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
2237 SOCKBUF_UNLOCK(&so->so_rcv);
2240 SOCKBUF_UNLOCK(&so->so_rcv);
2245 sbunlock(&so->so_rcv);
2250 * Optimized version of soreceive() for stream (TCP) sockets.
2253 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
2254 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2256 int len = 0, error = 0, flags, oresid;
2258 struct mbuf *m, *n = NULL;
2260 /* We only do stream sockets. */
2261 if (so->so_type != SOCK_STREAM)
2266 flags = *flagsp &~ MSG_EOR;
2269 if (controlp != NULL)
2271 if (flags & MSG_OOB)
2272 return (soreceive_rcvoob(so, uio, flags));
2278 /* Prevent other readers from entering the socket. */
2279 error = sblock(sb, SBLOCKWAIT(flags));
2284 /* Easy one, no space to copyout anything. */
2285 if (uio->uio_resid == 0) {
2289 oresid = uio->uio_resid;
2291 /* We will never ever get anything unless we are or were connected. */
2292 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
2298 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2300 /* Abort if socket has reported problems. */
2302 if (sbavail(sb) > 0)
2304 if (oresid > uio->uio_resid)
2306 error = so->so_error;
2307 if (!(flags & MSG_PEEK))
2312 /* Door is closed. Deliver what is left, if any. */
2313 if (sb->sb_state & SBS_CANTRCVMORE) {
2314 if (sbavail(sb) > 0)
2320 /* Socket buffer is empty and we shall not block. */
2321 if (sbavail(sb) == 0 &&
2322 ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
2327 /* Socket buffer got some data that we shall deliver now. */
2328 if (sbavail(sb) > 0 && !(flags & MSG_WAITALL) &&
2329 ((so->so_state & SS_NBIO) ||
2330 (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
2331 sbavail(sb) >= sb->sb_lowat ||
2332 sbavail(sb) >= uio->uio_resid ||
2333 sbavail(sb) >= sb->sb_hiwat) ) {
2337 /* On MSG_WAITALL we must wait until all data or error arrives. */
2338 if ((flags & MSG_WAITALL) &&
2339 (sbavail(sb) >= uio->uio_resid || sbavail(sb) >= sb->sb_hiwat))
2343 * Wait and block until (more) data comes in.
2344 * NB: Drops the sockbuf lock during wait.
2352 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2353 KASSERT(sbavail(sb) > 0, ("%s: sockbuf empty", __func__));
2354 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
2358 uio->uio_td->td_ru.ru_msgrcv++;
2360 /* Fill uio until full or current end of socket buffer is reached. */
2361 len = min(uio->uio_resid, sbavail(sb));
2363 /* Dequeue as many mbufs as possible. */
2364 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
2368 m_cat(*mp0, sb->sb_mb);
2370 m != NULL && m->m_len <= len;
2372 KASSERT(!(m->m_flags & M_NOTAVAIL),
2373 ("%s: m %p not available", __func__, m));
2375 uio->uio_resid -= m->m_len;
2381 sb->sb_lastrecord = sb->sb_mb;
2382 if (sb->sb_mb == NULL)
2385 /* Copy the remainder. */
2387 KASSERT(sb->sb_mb != NULL,
2388 ("%s: len > 0 && sb->sb_mb empty", __func__));
2390 m = m_copym(sb->sb_mb, 0, len, M_NOWAIT);
2392 len = 0; /* Don't flush data from sockbuf. */
2394 uio->uio_resid -= len;
2405 /* NB: Must unlock socket buffer as uiomove may sleep. */
2407 error = m_mbuftouio(uio, sb->sb_mb, len);
2412 SBLASTRECORDCHK(sb);
2416 * Remove the delivered data from the socket buffer unless we
2417 * were only peeking.
2419 if (!(flags & MSG_PEEK)) {
2421 sbdrop_locked(sb, len);
2423 /* Notify protocol that we drained some data. */
2424 if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
2425 (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
2426 !(flags & MSG_SOCALLBCK))) {
2429 (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
2435 * For MSG_WAITALL we may have to loop again and wait for
2436 * more data to come in.
2438 if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
2441 SOCKBUF_LOCK_ASSERT(sb);
2442 SBLASTRECORDCHK(sb);
2450 * Optimized version of soreceive() for simple datagram cases from userspace.
2451 * Unlike in the stream case, we're able to drop a datagram if copyout()
2452 * fails, and because we handle datagrams atomically, we don't need to use a
2453 * sleep lock to prevent I/O interlacing.
2456 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
2457 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2459 struct mbuf *m, *m2;
2462 struct protosw *pr = so->so_proto;
2463 struct mbuf *nextrecord;
2467 if (controlp != NULL)
2470 flags = *flagsp &~ MSG_EOR;
2475 * For any complicated cases, fall back to the full
2476 * soreceive_generic().
2478 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
2479 return (soreceive_generic(so, psa, uio, mp0, controlp,
2483 * Enforce restrictions on use.
2485 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
2486 ("soreceive_dgram: wantrcvd"));
2487 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
2488 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
2489 ("soreceive_dgram: SBS_RCVATMARK"));
2490 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
2491 ("soreceive_dgram: P_CONNREQUIRED"));
2494 * Loop blocking while waiting for a datagram.
2496 SOCKBUF_LOCK(&so->so_rcv);
2497 while ((m = so->so_rcv.sb_mb) == NULL) {
2498 KASSERT(sbavail(&so->so_rcv) == 0,
2499 ("soreceive_dgram: sb_mb NULL but sbavail %u",
2500 sbavail(&so->so_rcv)));
2502 error = so->so_error;
2504 SOCKBUF_UNLOCK(&so->so_rcv);
2507 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
2508 uio->uio_resid == 0) {
2509 SOCKBUF_UNLOCK(&so->so_rcv);
2512 if ((so->so_state & SS_NBIO) ||
2513 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2514 SOCKBUF_UNLOCK(&so->so_rcv);
2515 return (EWOULDBLOCK);
2517 SBLASTRECORDCHK(&so->so_rcv);
2518 SBLASTMBUFCHK(&so->so_rcv);
2519 error = sbwait(&so->so_rcv);
2521 SOCKBUF_UNLOCK(&so->so_rcv);
2525 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2528 uio->uio_td->td_ru.ru_msgrcv++;
2529 SBLASTRECORDCHK(&so->so_rcv);
2530 SBLASTMBUFCHK(&so->so_rcv);
2531 nextrecord = m->m_nextpkt;
2532 if (nextrecord == NULL) {
2533 KASSERT(so->so_rcv.sb_lastrecord == m,
2534 ("soreceive_dgram: lastrecord != m"));
2537 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
2538 ("soreceive_dgram: m_nextpkt != nextrecord"));
2541 * Pull 'm' and its chain off the front of the packet queue.
2543 so->so_rcv.sb_mb = NULL;
2544 sockbuf_pushsync(&so->so_rcv, nextrecord);
2547 * Walk 'm's chain and free that many bytes from the socket buffer.
2549 for (m2 = m; m2 != NULL; m2 = m2->m_next)
2550 sbfree(&so->so_rcv, m2);
2553 * Do a few last checks before we let go of the lock.
2555 SBLASTRECORDCHK(&so->so_rcv);
2556 SBLASTMBUFCHK(&so->so_rcv);
2557 SOCKBUF_UNLOCK(&so->so_rcv);
2559 if (pr->pr_flags & PR_ADDR) {
2560 KASSERT(m->m_type == MT_SONAME,
2561 ("m->m_type == %d", m->m_type));
2563 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
2568 /* XXXRW: Can this happen? */
2573 * Packet to copyout() is now in 'm' and it is disconnected from the
2576 * Process one or more MT_CONTROL mbufs present before any data mbufs
2577 * in the first mbuf chain on the socket buffer. We call into the
2578 * protocol to perform externalization (or freeing if controlp ==
2579 * NULL). In some cases there can be only MT_CONTROL mbufs without
2582 if (m->m_type == MT_CONTROL) {
2583 struct mbuf *cm = NULL, *cmn;
2584 struct mbuf **cme = &cm;
2590 cme = &(*cme)->m_next;
2592 } while (m != NULL && m->m_type == MT_CONTROL);
2593 while (cm != NULL) {
2596 if (pr->pr_domain->dom_externalize != NULL) {
2597 error = (*pr->pr_domain->dom_externalize)
2598 (cm, controlp, flags);
2599 } else if (controlp != NULL)
2603 if (controlp != NULL) {
2604 while (*controlp != NULL)
2605 controlp = &(*controlp)->m_next;
2610 KASSERT(m == NULL || m->m_type == MT_DATA,
2611 ("soreceive_dgram: !data"));
2612 while (m != NULL && uio->uio_resid > 0) {
2613 len = uio->uio_resid;
2616 error = uiomove(mtod(m, char *), (int)len, uio);
2621 if (len == m->m_len)
2638 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2639 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2643 CURVNET_SET(so->so_vnet);
2644 if (!SOLISTENING(so))
2645 error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio,
2646 mp0, controlp, flagsp));
2654 soshutdown(struct socket *so, int how)
2656 struct protosw *pr = so->so_proto;
2657 int error, soerror_enotconn;
2659 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2662 soerror_enotconn = 0;
2664 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
2666 * POSIX mandates us to return ENOTCONN when shutdown(2) is
2667 * invoked on a datagram sockets, however historically we would
2668 * actually tear socket down. This is known to be leveraged by
2669 * some applications to unblock process waiting in recvXXX(2)
2670 * by other process that it shares that socket with. Try to meet
2671 * both backward-compatibility and POSIX requirements by forcing
2672 * ENOTCONN but still asking protocol to perform pru_shutdown().
2674 if (so->so_type != SOCK_DGRAM && !SOLISTENING(so))
2676 soerror_enotconn = 1;
2679 if (SOLISTENING(so)) {
2680 if (how != SHUT_WR) {
2682 so->so_error = ECONNABORTED;
2683 solisten_wakeup(so); /* unlocks so */
2688 CURVNET_SET(so->so_vnet);
2689 if (pr->pr_usrreqs->pru_flush != NULL)
2690 (*pr->pr_usrreqs->pru_flush)(so, how);
2693 if (how != SHUT_RD) {
2694 error = (*pr->pr_usrreqs->pru_shutdown)(so);
2695 wakeup(&so->so_timeo);
2697 return ((error == 0 && soerror_enotconn) ? ENOTCONN : error);
2699 wakeup(&so->so_timeo);
2703 return (soerror_enotconn ? ENOTCONN : 0);
2707 sorflush(struct socket *so)
2709 struct sockbuf *sb = &so->so_rcv;
2710 struct protosw *pr = so->so_proto;
2716 * In order to avoid calling dom_dispose with the socket buffer mutex
2717 * held, and in order to generally avoid holding the lock for a long
2718 * time, we make a copy of the socket buffer and clear the original
2719 * (except locks, state). The new socket buffer copy won't have
2720 * initialized locks so we can only call routines that won't use or
2721 * assert those locks.
2723 * Dislodge threads currently blocked in receive and wait to acquire
2724 * a lock against other simultaneous readers before clearing the
2725 * socket buffer. Don't let our acquire be interrupted by a signal
2726 * despite any existing socket disposition on interruptable waiting.
2729 (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2732 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2733 * and mutex data unchanged.
2736 bzero(&aso, sizeof(aso));
2737 aso.so_pcb = so->so_pcb;
2738 bcopy(&sb->sb_startzero, &aso.so_rcv.sb_startzero,
2739 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2740 bzero(&sb->sb_startzero,
2741 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2746 * Dispose of special rights and flush the copied socket. Don't call
2747 * any unsafe routines (that rely on locks being initialized) on aso.
2749 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2750 (*pr->pr_domain->dom_dispose)(&aso);
2751 sbrelease_internal(&aso.so_rcv, so);
2755 * Wrapper for Socket established helper hook.
2756 * Parameters: socket, context of the hook point, hook id.
2759 hhook_run_socket(struct socket *so, void *hctx, int32_t h_id)
2761 struct socket_hhook_data hhook_data = {
2768 CURVNET_SET(so->so_vnet);
2769 HHOOKS_RUN_IF(V_socket_hhh[h_id], &hhook_data, &so->osd);
2772 /* Ugly but needed, since hhooks return void for now */
2773 return (hhook_data.status);
2777 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2778 * additional variant to handle the case where the option value needs to be
2779 * some kind of integer, but not a specific size. In addition to their use
2780 * here, these functions are also called by the protocol-level pr_ctloutput()
2784 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2789 * If the user gives us more than we wanted, we ignore it, but if we
2790 * don't get the minimum length the caller wants, we return EINVAL.
2791 * On success, sopt->sopt_valsize is set to however much we actually
2794 if ((valsize = sopt->sopt_valsize) < minlen)
2797 sopt->sopt_valsize = valsize = len;
2799 if (sopt->sopt_td != NULL)
2800 return (copyin(sopt->sopt_val, buf, valsize));
2802 bcopy(sopt->sopt_val, buf, valsize);
2807 * Kernel version of setsockopt(2).
2809 * XXX: optlen is size_t, not socklen_t
2812 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2815 struct sockopt sopt;
2817 sopt.sopt_level = level;
2818 sopt.sopt_name = optname;
2819 sopt.sopt_dir = SOPT_SET;
2820 sopt.sopt_val = optval;
2821 sopt.sopt_valsize = optlen;
2822 sopt.sopt_td = NULL;
2823 return (sosetopt(so, &sopt));
2827 sosetopt(struct socket *so, struct sockopt *sopt)
2838 CURVNET_SET(so->so_vnet);
2840 if (sopt->sopt_level != SOL_SOCKET) {
2841 if (so->so_proto->pr_ctloutput != NULL)
2842 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2844 error = ENOPROTOOPT;
2846 switch (sopt->sopt_name) {
2847 case SO_ACCEPTFILTER:
2848 error = accept_filt_setopt(so, sopt);
2854 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2857 if (l.l_linger < 0 ||
2858 l.l_linger > USHRT_MAX ||
2859 l.l_linger > (INT_MAX / hz)) {
2864 so->so_linger = l.l_linger;
2866 so->so_options |= SO_LINGER;
2868 so->so_options &= ~SO_LINGER;
2875 case SO_USELOOPBACK:
2879 case SO_REUSEPORT_LB:
2886 error = sooptcopyin(sopt, &optval, sizeof optval,
2892 so->so_options |= sopt->sopt_name;
2894 so->so_options &= ~sopt->sopt_name;
2899 error = sooptcopyin(sopt, &optval, sizeof optval,
2904 if (optval < 0 || optval >= rt_numfibs) {
2908 if (((so->so_proto->pr_domain->dom_family == PF_INET) ||
2909 (so->so_proto->pr_domain->dom_family == PF_INET6) ||
2910 (so->so_proto->pr_domain->dom_family == PF_ROUTE)))
2911 so->so_fibnum = optval;
2916 case SO_USER_COOKIE:
2917 error = sooptcopyin(sopt, &val32, sizeof val32,
2921 so->so_user_cookie = val32;
2928 error = sooptcopyin(sopt, &optval, sizeof optval,
2934 * Values < 1 make no sense for any of these options,
2942 error = sbsetopt(so, sopt->sopt_name, optval);
2947 #ifdef COMPAT_FREEBSD32
2948 if (SV_CURPROC_FLAG(SV_ILP32)) {
2949 struct timeval32 tv32;
2951 error = sooptcopyin(sopt, &tv32, sizeof tv32,
2953 CP(tv32, tv, tv_sec);
2954 CP(tv32, tv, tv_usec);
2957 error = sooptcopyin(sopt, &tv, sizeof tv,
2961 if (tv.tv_sec < 0 || tv.tv_usec < 0 ||
2962 tv.tv_usec >= 1000000) {
2966 if (tv.tv_sec > INT32_MAX)
2970 switch (sopt->sopt_name) {
2972 so->so_snd.sb_timeo = val;
2975 so->so_rcv.sb_timeo = val;
2982 error = sooptcopyin(sopt, &extmac, sizeof extmac,
2986 error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2994 error = sooptcopyin(sopt, &optval, sizeof optval,
2998 if (optval < 0 || optval > SO_TS_CLOCK_MAX) {
3002 so->so_ts_clock = optval;
3005 case SO_MAX_PACING_RATE:
3006 error = sooptcopyin(sopt, &val32, sizeof(val32),
3010 so->so_max_pacing_rate = val32;
3014 if (V_socket_hhh[HHOOK_SOCKET_OPT]->hhh_nhooks > 0)
3015 error = hhook_run_socket(so, sopt,
3018 error = ENOPROTOOPT;
3021 if (error == 0 && so->so_proto->pr_ctloutput != NULL)
3022 (void)(*so->so_proto->pr_ctloutput)(so, sopt);
3030 * Helper routine for getsockopt.
3033 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
3041 * Documented get behavior is that we always return a value, possibly
3042 * truncated to fit in the user's buffer. Traditional behavior is
3043 * that we always tell the user precisely how much we copied, rather
3044 * than something useful like the total amount we had available for
3045 * her. Note that this interface is not idempotent; the entire
3046 * answer must be generated ahead of time.
3048 valsize = min(len, sopt->sopt_valsize);
3049 sopt->sopt_valsize = valsize;
3050 if (sopt->sopt_val != NULL) {
3051 if (sopt->sopt_td != NULL)
3052 error = copyout(buf, sopt->sopt_val, valsize);
3054 bcopy(buf, sopt->sopt_val, valsize);
3060 sogetopt(struct socket *so, struct sockopt *sopt)
3069 CURVNET_SET(so->so_vnet);
3071 if (sopt->sopt_level != SOL_SOCKET) {
3072 if (so->so_proto->pr_ctloutput != NULL)
3073 error = (*so->so_proto->pr_ctloutput)(so, sopt);
3075 error = ENOPROTOOPT;
3079 switch (sopt->sopt_name) {
3080 case SO_ACCEPTFILTER:
3081 error = accept_filt_getopt(so, sopt);
3086 l.l_onoff = so->so_options & SO_LINGER;
3087 l.l_linger = so->so_linger;
3089 error = sooptcopyout(sopt, &l, sizeof l);
3092 case SO_USELOOPBACK:
3098 case SO_REUSEPORT_LB:
3105 optval = so->so_options & sopt->sopt_name;
3107 error = sooptcopyout(sopt, &optval, sizeof optval);
3111 optval = so->so_proto->pr_domain->dom_family;
3115 optval = so->so_type;
3119 optval = so->so_proto->pr_protocol;
3124 optval = so->so_error;
3130 optval = SOLISTENING(so) ? so->sol_sbsnd_hiwat :
3131 so->so_snd.sb_hiwat;
3135 optval = SOLISTENING(so) ? so->sol_sbrcv_hiwat :
3136 so->so_rcv.sb_hiwat;
3140 optval = SOLISTENING(so) ? so->sol_sbsnd_lowat :
3141 so->so_snd.sb_lowat;
3145 optval = SOLISTENING(so) ? so->sol_sbrcv_lowat :
3146 so->so_rcv.sb_lowat;
3151 tv = sbttotv(sopt->sopt_name == SO_SNDTIMEO ?
3152 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
3153 #ifdef COMPAT_FREEBSD32
3154 if (SV_CURPROC_FLAG(SV_ILP32)) {
3155 struct timeval32 tv32;
3157 CP(tv, tv32, tv_sec);
3158 CP(tv, tv32, tv_usec);
3159 error = sooptcopyout(sopt, &tv32, sizeof tv32);
3162 error = sooptcopyout(sopt, &tv, sizeof tv);
3167 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
3171 error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
3175 error = sooptcopyout(sopt, &extmac, sizeof extmac);
3183 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
3187 error = mac_getsockopt_peerlabel(
3188 sopt->sopt_td->td_ucred, so, &extmac);
3191 error = sooptcopyout(sopt, &extmac, sizeof extmac);
3197 case SO_LISTENQLIMIT:
3198 optval = SOLISTENING(so) ? so->sol_qlimit : 0;
3202 optval = SOLISTENING(so) ? so->sol_qlen : 0;
3205 case SO_LISTENINCQLEN:
3206 optval = SOLISTENING(so) ? so->sol_incqlen : 0;
3210 optval = so->so_ts_clock;
3213 case SO_MAX_PACING_RATE:
3214 optval = so->so_max_pacing_rate;
3218 if (V_socket_hhh[HHOOK_SOCKET_OPT]->hhh_nhooks > 0)
3219 error = hhook_run_socket(so, sopt,
3222 error = ENOPROTOOPT;
3234 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
3236 struct mbuf *m, *m_prev;
3237 int sopt_size = sopt->sopt_valsize;
3239 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
3242 if (sopt_size > MLEN) {
3243 MCLGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT);
3244 if ((m->m_flags & M_EXT) == 0) {
3248 m->m_len = min(MCLBYTES, sopt_size);
3250 m->m_len = min(MLEN, sopt_size);
3252 sopt_size -= m->m_len;
3257 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
3262 if (sopt_size > MLEN) {
3263 MCLGET(m, sopt->sopt_td != NULL ? M_WAITOK :
3265 if ((m->m_flags & M_EXT) == 0) {
3270 m->m_len = min(MCLBYTES, sopt_size);
3272 m->m_len = min(MLEN, sopt_size);
3274 sopt_size -= m->m_len;
3282 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
3284 struct mbuf *m0 = m;
3286 if (sopt->sopt_val == NULL)
3288 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
3289 if (sopt->sopt_td != NULL) {
3292 error = copyin(sopt->sopt_val, mtod(m, char *),
3299 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
3300 sopt->sopt_valsize -= m->m_len;
3301 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
3304 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
3305 panic("ip6_sooptmcopyin");
3310 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
3312 struct mbuf *m0 = m;
3315 if (sopt->sopt_val == NULL)
3317 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
3318 if (sopt->sopt_td != NULL) {
3321 error = copyout(mtod(m, char *), sopt->sopt_val,
3328 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
3329 sopt->sopt_valsize -= m->m_len;
3330 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
3331 valsize += m->m_len;
3335 /* enough soopt buffer should be given from user-land */
3339 sopt->sopt_valsize = valsize;
3344 * sohasoutofband(): protocol notifies socket layer of the arrival of new
3345 * out-of-band data, which will then notify socket consumers.
3348 sohasoutofband(struct socket *so)
3351 if (so->so_sigio != NULL)
3352 pgsigio(&so->so_sigio, SIGURG, 0);
3353 selwakeuppri(&so->so_rdsel, PSOCK);
3357 sopoll(struct socket *so, int events, struct ucred *active_cred,
3362 * We do not need to set or assert curvnet as long as everyone uses
3365 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
3370 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
3376 if (SOLISTENING(so)) {
3377 if (!(events & (POLLIN | POLLRDNORM)))
3379 else if (!TAILQ_EMPTY(&so->sol_comp))
3380 revents = events & (POLLIN | POLLRDNORM);
3381 else if ((events & POLLINIGNEOF) == 0 && so->so_error)
3382 revents = (events & (POLLIN | POLLRDNORM)) | POLLHUP;
3384 selrecord(td, &so->so_rdsel);
3389 SOCKBUF_LOCK(&so->so_snd);
3390 SOCKBUF_LOCK(&so->so_rcv);
3391 if (events & (POLLIN | POLLRDNORM))
3392 if (soreadabledata(so))
3393 revents |= events & (POLLIN | POLLRDNORM);
3394 if (events & (POLLOUT | POLLWRNORM))
3395 if (sowriteable(so))
3396 revents |= events & (POLLOUT | POLLWRNORM);
3397 if (events & (POLLPRI | POLLRDBAND))
3398 if (so->so_oobmark ||
3399 (so->so_rcv.sb_state & SBS_RCVATMARK))
3400 revents |= events & (POLLPRI | POLLRDBAND);
3401 if ((events & POLLINIGNEOF) == 0) {
3402 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3403 revents |= events & (POLLIN | POLLRDNORM);
3404 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
3410 (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
3411 selrecord(td, &so->so_rdsel);
3412 so->so_rcv.sb_flags |= SB_SEL;
3414 if (events & (POLLOUT | POLLWRNORM)) {
3415 selrecord(td, &so->so_wrsel);
3416 so->so_snd.sb_flags |= SB_SEL;
3419 SOCKBUF_UNLOCK(&so->so_rcv);
3420 SOCKBUF_UNLOCK(&so->so_snd);
3427 soo_kqfilter(struct file *fp, struct knote *kn)
3429 struct socket *so = kn->kn_fp->f_data;
3433 switch (kn->kn_filter) {
3435 kn->kn_fop = &soread_filtops;
3436 knl = &so->so_rdsel.si_note;
3440 kn->kn_fop = &sowrite_filtops;
3441 knl = &so->so_wrsel.si_note;
3445 kn->kn_fop = &soempty_filtops;
3446 knl = &so->so_wrsel.si_note;
3454 if (SOLISTENING(so)) {
3455 knlist_add(knl, kn, 1);
3458 knlist_add(knl, kn, 1);
3459 sb->sb_flags |= SB_KNOTE;
3467 * Some routines that return EOPNOTSUPP for entry points that are not
3468 * supported by a protocol. Fill in as needed.
3471 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
3478 pru_aio_queue_notsupp(struct socket *so, struct kaiocb *job)
3485 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
3492 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3499 pru_bindat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
3507 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3514 pru_connectat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
3522 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
3529 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
3530 struct ifnet *ifp, struct thread *td)
3537 pru_disconnect_notsupp(struct socket *so)
3544 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
3551 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
3558 pru_rcvd_notsupp(struct socket *so, int flags)
3565 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
3572 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
3573 struct sockaddr *addr, struct mbuf *control, struct thread *td)
3580 pru_ready_notsupp(struct socket *so, struct mbuf *m, int count)
3583 return (EOPNOTSUPP);
3587 * This isn't really a ``null'' operation, but it's the default one and
3588 * doesn't do anything destructive.
3591 pru_sense_null(struct socket *so, struct stat *sb)
3594 sb->st_blksize = so->so_snd.sb_hiwat;
3599 pru_shutdown_notsupp(struct socket *so)
3606 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
3613 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
3614 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
3621 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
3622 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
3629 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
3637 filt_sordetach(struct knote *kn)
3639 struct socket *so = kn->kn_fp->f_data;
3642 knlist_remove(&so->so_rdsel.si_note, kn, 1);
3643 if (!SOLISTENING(so) && knlist_empty(&so->so_rdsel.si_note))
3644 so->so_rcv.sb_flags &= ~SB_KNOTE;
3645 so_rdknl_unlock(so);
3650 filt_soread(struct knote *kn, long hint)
3654 so = kn->kn_fp->f_data;
3656 if (SOLISTENING(so)) {
3657 SOCK_LOCK_ASSERT(so);
3658 kn->kn_data = so->sol_qlen;
3660 kn->kn_flags |= EV_EOF;
3661 kn->kn_fflags = so->so_error;
3664 return (!TAILQ_EMPTY(&so->sol_comp));
3667 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
3669 kn->kn_data = sbavail(&so->so_rcv) - so->so_rcv.sb_ctl;
3670 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3671 kn->kn_flags |= EV_EOF;
3672 kn->kn_fflags = so->so_error;
3674 } else if (so->so_error) /* temporary udp error */
3677 if (kn->kn_sfflags & NOTE_LOWAT) {
3678 if (kn->kn_data >= kn->kn_sdata)
3680 } else if (sbavail(&so->so_rcv) >= so->so_rcv.sb_lowat)
3683 /* This hook returning non-zero indicates an event, not error */
3684 return (hhook_run_socket(so, NULL, HHOOK_FILT_SOREAD));
3688 filt_sowdetach(struct knote *kn)
3690 struct socket *so = kn->kn_fp->f_data;
3693 knlist_remove(&so->so_wrsel.si_note, kn, 1);
3694 if (!SOLISTENING(so) && knlist_empty(&so->so_wrsel.si_note))
3695 so->so_snd.sb_flags &= ~SB_KNOTE;
3696 so_wrknl_unlock(so);
3701 filt_sowrite(struct knote *kn, long hint)
3705 so = kn->kn_fp->f_data;
3707 if (SOLISTENING(so))
3710 SOCKBUF_LOCK_ASSERT(&so->so_snd);
3711 kn->kn_data = sbspace(&so->so_snd);
3713 hhook_run_socket(so, kn, HHOOK_FILT_SOWRITE);
3715 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
3716 kn->kn_flags |= EV_EOF;
3717 kn->kn_fflags = so->so_error;
3719 } else if (so->so_error) /* temporary udp error */
3721 else if (((so->so_state & SS_ISCONNECTED) == 0) &&
3722 (so->so_proto->pr_flags & PR_CONNREQUIRED))
3724 else if (kn->kn_sfflags & NOTE_LOWAT)
3725 return (kn->kn_data >= kn->kn_sdata);
3727 return (kn->kn_data >= so->so_snd.sb_lowat);
3731 filt_soempty(struct knote *kn, long hint)
3735 so = kn->kn_fp->f_data;
3737 if (SOLISTENING(so))
3740 SOCKBUF_LOCK_ASSERT(&so->so_snd);
3741 kn->kn_data = sbused(&so->so_snd);
3743 if (kn->kn_data == 0)
3750 socheckuid(struct socket *so, uid_t uid)
3755 if (so->so_cred->cr_uid != uid)
3761 * These functions are used by protocols to notify the socket layer (and its
3762 * consumers) of state changes in the sockets driven by protocol-side events.
3766 * Procedures to manipulate state flags of socket and do appropriate wakeups.
3768 * Normal sequence from the active (originating) side is that
3769 * soisconnecting() is called during processing of connect() call, resulting
3770 * in an eventual call to soisconnected() if/when the connection is
3771 * established. When the connection is torn down soisdisconnecting() is
3772 * called during processing of disconnect() call, and soisdisconnected() is
3773 * called when the connection to the peer is totally severed. The semantics
3774 * of these routines are such that connectionless protocols can call
3775 * soisconnected() and soisdisconnected() only, bypassing the in-progress
3776 * calls when setting up a ``connection'' takes no time.
3778 * From the passive side, a socket is created with two queues of sockets:
3779 * so_incomp for connections in progress and so_comp for connections already
3780 * made and awaiting user acceptance. As a protocol is preparing incoming
3781 * connections, it creates a socket structure queued on so_incomp by calling
3782 * sonewconn(). When the connection is established, soisconnected() is
3783 * called, and transfers the socket structure to so_comp, making it available
3786 * If a socket is closed with sockets on either so_incomp or so_comp, these
3787 * sockets are dropped.
3789 * If higher-level protocols are implemented in the kernel, the wakeups done
3790 * here will sometimes cause software-interrupt process scheduling.
3793 soisconnecting(struct socket *so)
3797 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3798 so->so_state |= SS_ISCONNECTING;
3803 soisconnected(struct socket *so)
3807 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3808 so->so_state |= SS_ISCONNECTED;
3810 if (so->so_qstate == SQ_INCOMP) {
3811 struct socket *head = so->so_listen;
3814 KASSERT(head, ("%s: so %p on incomp of NULL", __func__, so));
3816 * Promoting a socket from incomplete queue to complete, we
3817 * need to go through reverse order of locking. We first do
3818 * trylock, and if that doesn't succeed, we go the hard way
3819 * leaving a reference and rechecking consistency after proper
3822 if (__predict_false(SOLISTEN_TRYLOCK(head) == 0)) {
3825 SOLISTEN_LOCK(head);
3827 if (__predict_false(head != so->so_listen)) {
3829 * The socket went off the listen queue,
3830 * should be lost race to close(2) of sol.
3831 * The socket is about to soabort().
3837 /* Not the last one, as so holds a ref. */
3838 refcount_release(&head->so_count);
3841 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3842 TAILQ_REMOVE(&head->sol_incomp, so, so_list);
3843 head->sol_incqlen--;
3844 TAILQ_INSERT_TAIL(&head->sol_comp, so, so_list);
3846 so->so_qstate = SQ_COMP;
3848 solisten_wakeup(head); /* unlocks */
3850 SOCKBUF_LOCK(&so->so_rcv);
3851 soupcall_set(so, SO_RCV,
3852 head->sol_accept_filter->accf_callback,
3853 head->sol_accept_filter_arg);
3854 so->so_options &= ~SO_ACCEPTFILTER;
3855 ret = head->sol_accept_filter->accf_callback(so,
3856 head->sol_accept_filter_arg, M_NOWAIT);
3857 if (ret == SU_ISCONNECTED) {
3858 soupcall_clear(so, SO_RCV);
3859 SOCKBUF_UNLOCK(&so->so_rcv);
3862 SOCKBUF_UNLOCK(&so->so_rcv);
3864 SOLISTEN_UNLOCK(head);
3869 wakeup(&so->so_timeo);
3875 soisdisconnecting(struct socket *so)
3879 so->so_state &= ~SS_ISCONNECTING;
3880 so->so_state |= SS_ISDISCONNECTING;
3882 if (!SOLISTENING(so)) {
3883 SOCKBUF_LOCK(&so->so_rcv);
3884 socantrcvmore_locked(so);
3885 SOCKBUF_LOCK(&so->so_snd);
3886 socantsendmore_locked(so);
3889 wakeup(&so->so_timeo);
3893 soisdisconnected(struct socket *so)
3897 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3898 so->so_state |= SS_ISDISCONNECTED;
3900 if (!SOLISTENING(so)) {
3902 SOCKBUF_LOCK(&so->so_rcv);
3903 socantrcvmore_locked(so);
3904 SOCKBUF_LOCK(&so->so_snd);
3905 sbdrop_locked(&so->so_snd, sbused(&so->so_snd));
3906 socantsendmore_locked(so);
3909 wakeup(&so->so_timeo);
3913 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3916 sodupsockaddr(const struct sockaddr *sa, int mflags)
3918 struct sockaddr *sa2;
3920 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3922 bcopy(sa, sa2, sa->sa_len);
3927 * Register per-socket destructor.
3930 sodtor_set(struct socket *so, so_dtor_t *func)
3933 SOCK_LOCK_ASSERT(so);
3938 * Register per-socket buffer upcalls.
3941 soupcall_set(struct socket *so, int which, so_upcall_t func, void *arg)
3945 KASSERT(!SOLISTENING(so), ("%s: so %p listening", __func__, so));
3955 panic("soupcall_set: bad which");
3957 SOCKBUF_LOCK_ASSERT(sb);
3958 sb->sb_upcall = func;
3959 sb->sb_upcallarg = arg;
3960 sb->sb_flags |= SB_UPCALL;
3964 soupcall_clear(struct socket *so, int which)
3968 KASSERT(!SOLISTENING(so), ("%s: so %p listening", __func__, so));
3978 panic("soupcall_clear: bad which");
3980 SOCKBUF_LOCK_ASSERT(sb);
3981 KASSERT(sb->sb_upcall != NULL,
3982 ("%s: so %p no upcall to clear", __func__, so));
3983 sb->sb_upcall = NULL;
3984 sb->sb_upcallarg = NULL;
3985 sb->sb_flags &= ~SB_UPCALL;
3989 solisten_upcall_set(struct socket *so, so_upcall_t func, void *arg)
3992 SOLISTEN_LOCK_ASSERT(so);
3993 so->sol_upcall = func;
3994 so->sol_upcallarg = arg;
3998 so_rdknl_lock(void *arg)
4000 struct socket *so = arg;
4002 if (SOLISTENING(so))
4005 SOCKBUF_LOCK(&so->so_rcv);
4009 so_rdknl_unlock(void *arg)
4011 struct socket *so = arg;
4013 if (SOLISTENING(so))
4016 SOCKBUF_UNLOCK(&so->so_rcv);
4020 so_rdknl_assert_locked(void *arg)
4022 struct socket *so = arg;
4024 if (SOLISTENING(so))
4025 SOCK_LOCK_ASSERT(so);
4027 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
4031 so_rdknl_assert_unlocked(void *arg)
4033 struct socket *so = arg;
4035 if (SOLISTENING(so))
4036 SOCK_UNLOCK_ASSERT(so);
4038 SOCKBUF_UNLOCK_ASSERT(&so->so_rcv);
4042 so_wrknl_lock(void *arg)
4044 struct socket *so = arg;
4046 if (SOLISTENING(so))
4049 SOCKBUF_LOCK(&so->so_snd);
4053 so_wrknl_unlock(void *arg)
4055 struct socket *so = arg;
4057 if (SOLISTENING(so))
4060 SOCKBUF_UNLOCK(&so->so_snd);
4064 so_wrknl_assert_locked(void *arg)
4066 struct socket *so = arg;
4068 if (SOLISTENING(so))
4069 SOCK_LOCK_ASSERT(so);
4071 SOCKBUF_LOCK_ASSERT(&so->so_snd);
4075 so_wrknl_assert_unlocked(void *arg)
4077 struct socket *so = arg;
4079 if (SOLISTENING(so))
4080 SOCK_UNLOCK_ASSERT(so);
4082 SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
4086 * Create an external-format (``xsocket'') structure using the information in
4087 * the kernel-format socket structure pointed to by so. This is done to
4088 * reduce the spew of irrelevant information over this interface, to isolate
4089 * user code from changes in the kernel structure, and potentially to provide
4090 * information-hiding if we decide that some of this information should be
4091 * hidden from users.
4094 sotoxsocket(struct socket *so, struct xsocket *xso)
4097 bzero(xso, sizeof(*xso));
4098 xso->xso_len = sizeof *xso;
4099 xso->xso_so = (uintptr_t)so;
4100 xso->so_type = so->so_type;
4101 xso->so_options = so->so_options;
4102 xso->so_linger = so->so_linger;
4103 xso->so_state = so->so_state;
4104 xso->so_pcb = (uintptr_t)so->so_pcb;
4105 xso->xso_protocol = so->so_proto->pr_protocol;
4106 xso->xso_family = so->so_proto->pr_domain->dom_family;
4107 xso->so_timeo = so->so_timeo;
4108 xso->so_error = so->so_error;
4109 xso->so_uid = so->so_cred->cr_uid;
4110 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
4111 if (SOLISTENING(so)) {
4112 xso->so_qlen = so->sol_qlen;
4113 xso->so_incqlen = so->sol_incqlen;
4114 xso->so_qlimit = so->sol_qlimit;
4115 xso->so_oobmark = 0;
4117 xso->so_state |= so->so_qstate;
4118 xso->so_qlen = xso->so_incqlen = xso->so_qlimit = 0;
4119 xso->so_oobmark = so->so_oobmark;
4120 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
4121 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
4126 so_sockbuf_rcv(struct socket *so)
4129 return (&so->so_rcv);
4133 so_sockbuf_snd(struct socket *so)
4136 return (&so->so_snd);
4140 so_state_get(const struct socket *so)
4143 return (so->so_state);
4147 so_state_set(struct socket *so, int val)
4154 so_options_get(const struct socket *so)
4157 return (so->so_options);
4161 so_options_set(struct socket *so, int val)
4164 so->so_options = val;
4168 so_error_get(const struct socket *so)
4171 return (so->so_error);
4175 so_error_set(struct socket *so, int val)
4182 so_linger_get(const struct socket *so)
4185 return (so->so_linger);
4189 so_linger_set(struct socket *so, int val)
4192 KASSERT(val >= 0 && val <= USHRT_MAX && val <= (INT_MAX / hz),
4193 ("%s: val %d out of range", __func__, val));
4195 so->so_linger = val;
4199 so_protosw_get(const struct socket *so)
4202 return (so->so_proto);
4206 so_protosw_set(struct socket *so, struct protosw *val)
4213 so_sorwakeup(struct socket *so)
4220 so_sowwakeup(struct socket *so)
4227 so_sorwakeup_locked(struct socket *so)
4230 sorwakeup_locked(so);
4234 so_sowwakeup_locked(struct socket *so)
4237 sowwakeup_locked(so);
4241 so_lock(struct socket *so)
4248 so_unlock(struct socket *so)