xref: /freebsd/sys/kern/uipc_socket.c (revision e1e636193db45630c7881246d25902e57c43d24e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
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
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Comments on the socket life cycle:
37  *
38  * soalloc() sets of socket layer state for a socket, called only by
39  * socreate() and sonewconn().  Socket layer private.
40  *
41  * sodealloc() tears down socket layer state for a socket, called only by
42  * sofree() and sonewconn().  Socket layer private.
43  *
44  * pru_attach() associates protocol layer state with an allocated socket;
45  * called only once, may fail, aborting socket allocation.  This is called
46  * from socreate() and sonewconn().  Socket layer private.
47  *
48  * pru_detach() disassociates protocol layer state from an attached socket,
49  * and will be called exactly once for sockets in which pru_attach() has
50  * been successfully called.  If pru_attach() returned an error,
51  * pru_detach() will not be called.  Socket layer private.
52  *
53  * pru_abort() and pru_close() notify the protocol layer that the last
54  * consumer of a socket is starting to tear down the socket, and that the
55  * protocol should terminate the connection.  Historically, pru_abort() also
56  * detached protocol state from the socket state, but this is no longer the
57  * case.
58  *
59  * socreate() creates a socket and attaches protocol state.  This is a public
60  * interface that may be used by socket layer consumers to create new
61  * sockets.
62  *
63  * sonewconn() creates a socket and attaches protocol state.  This is a
64  * public interface  that may be used by protocols to create new sockets when
65  * a new connection is received and will be available for accept() on a
66  * listen socket.
67  *
68  * soclose() destroys a socket after possibly waiting for it to disconnect.
69  * This is a public interface that socket consumers should use to close and
70  * release a socket when done with it.
71  *
72  * soabort() destroys a socket without waiting for it to disconnect (used
73  * only for incoming connections that are already partially or fully
74  * connected).  This is used internally by the socket layer when clearing
75  * listen socket queues (due to overflow or close on the listen socket), but
76  * is also a public interface protocols may use to abort connections in
77  * their incomplete listen queues should they no longer be required.  Sockets
78  * placed in completed connection listen queues should not be aborted for
79  * reasons described in the comment above the soclose() implementation.  This
80  * is not a general purpose close routine, and except in the specific
81  * circumstances described here, should not be used.
82  *
83  * sofree() will free a socket and its protocol state if all references on
84  * the socket have been released, and is the public interface to attempt to
85  * free a socket when a reference is removed.  This is a socket layer private
86  * interface.
87  *
88  * NOTE: In addition to socreate() and soclose(), which provide a single
89  * socket reference to the consumer to be managed as required, there are two
90  * calls to explicitly manage socket references, soref(), and sorele().
91  * Currently, these are generally required only when transitioning a socket
92  * from a listen queue to a file descriptor, in order to prevent garbage
93  * collection of the socket at an untimely moment.  For a number of reasons,
94  * these interfaces are not preferred, and should be avoided.
95  *
96  * NOTE: With regard to VNETs the general rule is that callers do not set
97  * curvnet. Exceptions to this rule include soabort(), sodisconnect(),
98  * sofree(), sorele(), sonewconn() and sorflush(), which are usually called
99  * from a pre-set VNET context.  sopoll() currently does not need a VNET
100  * context to be set.
101  */
102 
103 #include <sys/cdefs.h>
104 #include "opt_inet.h"
105 #include "opt_inet6.h"
106 #include "opt_kern_tls.h"
107 #include "opt_ktrace.h"
108 #include "opt_sctp.h"
109 
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/capsicum.h>
113 #include <sys/fcntl.h>
114 #include <sys/limits.h>
115 #include <sys/lock.h>
116 #include <sys/mac.h>
117 #include <sys/malloc.h>
118 #include <sys/mbuf.h>
119 #include <sys/mutex.h>
120 #include <sys/domain.h>
121 #include <sys/file.h>			/* for struct knote */
122 #include <sys/hhook.h>
123 #include <sys/kernel.h>
124 #include <sys/khelp.h>
125 #include <sys/ktls.h>
126 #include <sys/event.h>
127 #include <sys/eventhandler.h>
128 #include <sys/poll.h>
129 #include <sys/proc.h>
130 #include <sys/protosw.h>
131 #include <sys/sbuf.h>
132 #include <sys/socket.h>
133 #include <sys/socketvar.h>
134 #include <sys/resourcevar.h>
135 #include <net/route.h>
136 #include <sys/signalvar.h>
137 #include <sys/stat.h>
138 #include <sys/sx.h>
139 #include <sys/sysctl.h>
140 #include <sys/taskqueue.h>
141 #include <sys/uio.h>
142 #include <sys/un.h>
143 #include <sys/unpcb.h>
144 #include <sys/jail.h>
145 #include <sys/syslog.h>
146 #include <netinet/in.h>
147 #include <netinet/in_pcb.h>
148 #include <netinet/tcp.h>
149 
150 #include <net/vnet.h>
151 
152 #include <security/mac/mac_framework.h>
153 
154 #include <vm/uma.h>
155 
156 #ifdef COMPAT_FREEBSD32
157 #include <sys/mount.h>
158 #include <sys/sysent.h>
159 #include <compat/freebsd32/freebsd32.h>
160 #endif
161 
162 static int	soreceive_rcvoob(struct socket *so, struct uio *uio,
163 		    int flags);
164 static void	so_rdknl_lock(void *);
165 static void	so_rdknl_unlock(void *);
166 static void	so_rdknl_assert_lock(void *, int);
167 static void	so_wrknl_lock(void *);
168 static void	so_wrknl_unlock(void *);
169 static void	so_wrknl_assert_lock(void *, int);
170 
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;
178 
179 static struct filterops soread_filtops = {
180 	.f_isfd = 1,
181 	.f_detach = filt_sordetach,
182 	.f_event = filt_soread,
183 };
184 static struct filterops sowrite_filtops = {
185 	.f_isfd = 1,
186 	.f_detach = filt_sowdetach,
187 	.f_event = filt_sowrite,
188 };
189 static struct filterops soempty_filtops = {
190 	.f_isfd = 1,
191 	.f_detach = filt_sowdetach,
192 	.f_event = filt_soempty,
193 };
194 
195 so_gen_t	so_gencnt;	/* generation count for sockets */
196 
197 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
198 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
199 
200 #define	VNET_SO_ASSERT(so)						\
201 	VNET_ASSERT(curvnet != NULL,					\
202 	    ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so)));
203 
204 VNET_DEFINE(struct hhook_head *, socket_hhh[HHOOK_SOCKET_LAST + 1]);
205 #define	V_socket_hhh		VNET(socket_hhh)
206 
207 /*
208  * Limit on the number of connections in the listen queue waiting
209  * for accept(2).
210  * NB: The original sysctl somaxconn is still available but hidden
211  * to prevent confusion about the actual purpose of this number.
212  */
213 static u_int somaxconn = SOMAXCONN;
214 
215 static int
216 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
217 {
218 	int error;
219 	int val;
220 
221 	val = somaxconn;
222 	error = sysctl_handle_int(oidp, &val, 0, req);
223 	if (error || !req->newptr )
224 		return (error);
225 
226 	/*
227 	 * The purpose of the UINT_MAX / 3 limit, is so that the formula
228 	 *   3 * so_qlimit / 2
229 	 * below, will not overflow.
230          */
231 
232 	if (val < 1 || val > UINT_MAX / 3)
233 		return (EINVAL);
234 
235 	somaxconn = val;
236 	return (0);
237 }
238 SYSCTL_PROC(_kern_ipc, OID_AUTO, soacceptqueue,
239     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int),
240     sysctl_somaxconn, "I",
241     "Maximum listen socket pending connection accept queue size");
242 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn,
243     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_SKIP | CTLFLAG_MPSAFE, 0,
244     sizeof(int), sysctl_somaxconn, "I",
245     "Maximum listen socket pending connection accept queue size (compat)");
246 
247 static int numopensockets;
248 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
249     &numopensockets, 0, "Number of open sockets");
250 
251 /*
252  * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
253  * so_gencnt field.
254  */
255 static struct mtx so_global_mtx;
256 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
257 
258 /*
259  * General IPC sysctl name space, used by sockets and a variety of other IPC
260  * types.
261  */
262 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
263     "IPC");
264 
265 /*
266  * Initialize the socket subsystem and set up the socket
267  * memory allocator.
268  */
269 static uma_zone_t socket_zone;
270 int	maxsockets;
271 
272 static void
273 socket_zone_change(void *tag)
274 {
275 
276 	maxsockets = uma_zone_set_max(socket_zone, maxsockets);
277 }
278 
279 static void
280 socket_hhook_register(int subtype)
281 {
282 
283 	if (hhook_head_register(HHOOK_TYPE_SOCKET, subtype,
284 	    &V_socket_hhh[subtype],
285 	    HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0)
286 		printf("%s: WARNING: unable to register hook\n", __func__);
287 }
288 
289 static void
290 socket_hhook_deregister(int subtype)
291 {
292 
293 	if (hhook_head_deregister(V_socket_hhh[subtype]) != 0)
294 		printf("%s: WARNING: unable to deregister hook\n", __func__);
295 }
296 
297 static void
298 socket_init(void *tag)
299 {
300 
301 	socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
302 	    NULL, NULL, UMA_ALIGN_PTR, 0);
303 	maxsockets = uma_zone_set_max(socket_zone, maxsockets);
304 	uma_zone_set_warning(socket_zone, "kern.ipc.maxsockets limit reached");
305 	EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
306 	    EVENTHANDLER_PRI_FIRST);
307 }
308 SYSINIT(socket, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, socket_init, NULL);
309 
310 static void
311 socket_vnet_init(const void *unused __unused)
312 {
313 	int i;
314 
315 	/* We expect a contiguous range */
316 	for (i = 0; i <= HHOOK_SOCKET_LAST; i++)
317 		socket_hhook_register(i);
318 }
319 VNET_SYSINIT(socket_vnet_init, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY,
320     socket_vnet_init, NULL);
321 
322 static void
323 socket_vnet_uninit(const void *unused __unused)
324 {
325 	int i;
326 
327 	for (i = 0; i <= HHOOK_SOCKET_LAST; i++)
328 		socket_hhook_deregister(i);
329 }
330 VNET_SYSUNINIT(socket_vnet_uninit, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY,
331     socket_vnet_uninit, NULL);
332 
333 /*
334  * Initialise maxsockets.  This SYSINIT must be run after
335  * tunable_mbinit().
336  */
337 static void
338 init_maxsockets(void *ignored)
339 {
340 
341 	TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
342 	maxsockets = imax(maxsockets, maxfiles);
343 }
344 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
345 
346 /*
347  * Sysctl to get and set the maximum global sockets limit.  Notify protocols
348  * of the change so that they can update their dependent limits as required.
349  */
350 static int
351 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
352 {
353 	int error, newmaxsockets;
354 
355 	newmaxsockets = maxsockets;
356 	error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
357 	if (error == 0 && req->newptr && newmaxsockets != maxsockets) {
358 		if (newmaxsockets > maxsockets &&
359 		    newmaxsockets <= maxfiles) {
360 			maxsockets = newmaxsockets;
361 			EVENTHANDLER_INVOKE(maxsockets_change);
362 		} else
363 			error = EINVAL;
364 	}
365 	return (error);
366 }
367 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets,
368     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
369     &maxsockets, 0, sysctl_maxsockets, "IU",
370     "Maximum number of sockets available");
371 
372 /*
373  * Socket operation routines.  These routines are called by the routines in
374  * sys_socket.c or from a system process, and implement the semantics of
375  * socket operations by switching out to the protocol specific routines.
376  */
377 
378 /*
379  * Get a socket structure from our zone, and initialize it.  Note that it
380  * would probably be better to allocate socket and PCB at the same time, but
381  * I'm not convinced that all the protocols can be easily modified to do
382  * this.
383  *
384  * soalloc() returns a socket with a ref count of 0.
385  */
386 static struct socket *
387 soalloc(struct vnet *vnet)
388 {
389 	struct socket *so;
390 
391 	so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
392 	if (so == NULL)
393 		return (NULL);
394 #ifdef MAC
395 	if (mac_socket_init(so, M_NOWAIT) != 0) {
396 		uma_zfree(socket_zone, so);
397 		return (NULL);
398 	}
399 #endif
400 	if (khelp_init_osd(HELPER_CLASS_SOCKET, &so->osd)) {
401 		uma_zfree(socket_zone, so);
402 		return (NULL);
403 	}
404 
405 	/*
406 	 * The socket locking protocol allows to lock 2 sockets at a time,
407 	 * however, the first one must be a listening socket.  WITNESS lacks
408 	 * a feature to change class of an existing lock, so we use DUPOK.
409 	 */
410 	mtx_init(&so->so_lock, "socket", NULL, MTX_DEF | MTX_DUPOK);
411 	mtx_init(&so->so_snd_mtx, "so_snd", NULL, MTX_DEF);
412 	mtx_init(&so->so_rcv_mtx, "so_rcv", NULL, MTX_DEF);
413 	so->so_rcv.sb_sel = &so->so_rdsel;
414 	so->so_snd.sb_sel = &so->so_wrsel;
415 	sx_init(&so->so_snd_sx, "so_snd_sx");
416 	sx_init(&so->so_rcv_sx, "so_rcv_sx");
417 	TAILQ_INIT(&so->so_snd.sb_aiojobq);
418 	TAILQ_INIT(&so->so_rcv.sb_aiojobq);
419 	TASK_INIT(&so->so_snd.sb_aiotask, 0, soaio_snd, so);
420 	TASK_INIT(&so->so_rcv.sb_aiotask, 0, soaio_rcv, so);
421 #ifdef VIMAGE
422 	VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p",
423 	    __func__, __LINE__, so));
424 	so->so_vnet = vnet;
425 #endif
426 	/* We shouldn't need the so_global_mtx */
427 	if (hhook_run_socket(so, NULL, HHOOK_SOCKET_CREATE)) {
428 		/* Do we need more comprehensive error returns? */
429 		uma_zfree(socket_zone, so);
430 		return (NULL);
431 	}
432 	mtx_lock(&so_global_mtx);
433 	so->so_gencnt = ++so_gencnt;
434 	++numopensockets;
435 #ifdef VIMAGE
436 	vnet->vnet_sockcnt++;
437 #endif
438 	mtx_unlock(&so_global_mtx);
439 
440 	return (so);
441 }
442 
443 /*
444  * Free the storage associated with a socket at the socket layer, tear down
445  * locks, labels, etc.  All protocol state is assumed already to have been
446  * torn down (and possibly never set up) by the caller.
447  */
448 void
449 sodealloc(struct socket *so)
450 {
451 
452 	KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
453 	KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
454 
455 	mtx_lock(&so_global_mtx);
456 	so->so_gencnt = ++so_gencnt;
457 	--numopensockets;	/* Could be below, but faster here. */
458 #ifdef VIMAGE
459 	VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p",
460 	    __func__, __LINE__, so));
461 	so->so_vnet->vnet_sockcnt--;
462 #endif
463 	mtx_unlock(&so_global_mtx);
464 #ifdef MAC
465 	mac_socket_destroy(so);
466 #endif
467 	hhook_run_socket(so, NULL, HHOOK_SOCKET_CLOSE);
468 
469 	khelp_destroy_osd(&so->osd);
470 	if (SOLISTENING(so)) {
471 		if (so->sol_accept_filter != NULL)
472 			accept_filt_setopt(so, NULL);
473 	} else {
474 		if (so->so_rcv.sb_hiwat)
475 			(void)chgsbsize(so->so_cred->cr_uidinfo,
476 			    &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
477 		if (so->so_snd.sb_hiwat)
478 			(void)chgsbsize(so->so_cred->cr_uidinfo,
479 			    &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
480 		sx_destroy(&so->so_snd_sx);
481 		sx_destroy(&so->so_rcv_sx);
482 		mtx_destroy(&so->so_snd_mtx);
483 		mtx_destroy(&so->so_rcv_mtx);
484 	}
485 	crfree(so->so_cred);
486 	mtx_destroy(&so->so_lock);
487 	uma_zfree(socket_zone, so);
488 }
489 
490 /*
491  * socreate returns a socket with a ref count of 1 and a file descriptor
492  * reference.  The socket should be closed with soclose().
493  */
494 int
495 socreate(int dom, struct socket **aso, int type, int proto,
496     struct ucred *cred, struct thread *td)
497 {
498 	struct protosw *prp;
499 	struct socket *so;
500 	int error;
501 
502 	/*
503 	 * XXX: divert(4) historically abused PF_INET.  Keep this compatibility
504 	 * shim until all applications have been updated.
505 	 */
506 	if (__predict_false(dom == PF_INET && type == SOCK_RAW &&
507 	    proto == IPPROTO_DIVERT)) {
508 		dom = PF_DIVERT;
509 		printf("%s uses obsolete way to create divert(4) socket\n",
510 		    td->td_proc->p_comm);
511 	}
512 
513 	prp = pffindproto(dom, type, proto);
514 	if (prp == NULL) {
515 		/* No support for domain. */
516 		if (pffinddomain(dom) == NULL)
517 			return (EAFNOSUPPORT);
518 		/* No support for socket type. */
519 		if (proto == 0 && type != 0)
520 			return (EPROTOTYPE);
521 		return (EPROTONOSUPPORT);
522 	}
523 
524 	MPASS(prp->pr_attach);
525 
526 	if ((prp->pr_flags & PR_CAPATTACH) == 0) {
527 		if (CAP_TRACING(td))
528 			ktrcapfail(CAPFAIL_PROTO, &proto);
529 		if (IN_CAPABILITY_MODE(td))
530 			return (ECAPMODE);
531 	}
532 
533 	if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
534 		return (EPROTONOSUPPORT);
535 
536 	so = soalloc(CRED_TO_VNET(cred));
537 	if (so == NULL)
538 		return (ENOBUFS);
539 
540 	so->so_type = type;
541 	so->so_cred = crhold(cred);
542 	if ((prp->pr_domain->dom_family == PF_INET) ||
543 	    (prp->pr_domain->dom_family == PF_INET6) ||
544 	    (prp->pr_domain->dom_family == PF_ROUTE))
545 		so->so_fibnum = td->td_proc->p_fibnum;
546 	else
547 		so->so_fibnum = 0;
548 	so->so_proto = prp;
549 #ifdef MAC
550 	mac_socket_create(cred, so);
551 #endif
552 	knlist_init(&so->so_rdsel.si_note, so, so_rdknl_lock, so_rdknl_unlock,
553 	    so_rdknl_assert_lock);
554 	knlist_init(&so->so_wrsel.si_note, so, so_wrknl_lock, so_wrknl_unlock,
555 	    so_wrknl_assert_lock);
556 	if ((prp->pr_flags & PR_SOCKBUF) == 0) {
557 		so->so_snd.sb_mtx = &so->so_snd_mtx;
558 		so->so_rcv.sb_mtx = &so->so_rcv_mtx;
559 	}
560 	/*
561 	 * Auto-sizing of socket buffers is managed by the protocols and
562 	 * the appropriate flags must be set in the pru_attach function.
563 	 */
564 	CURVNET_SET(so->so_vnet);
565 	error = prp->pr_attach(so, proto, td);
566 	CURVNET_RESTORE();
567 	if (error) {
568 		sodealloc(so);
569 		return (error);
570 	}
571 	soref(so);
572 	*aso = so;
573 	return (0);
574 }
575 
576 #ifdef REGRESSION
577 static int regression_sonewconn_earlytest = 1;
578 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
579     &regression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
580 #endif
581 
582 static int sooverprio = LOG_DEBUG;
583 SYSCTL_INT(_kern_ipc, OID_AUTO, sooverprio, CTLFLAG_RW,
584     &sooverprio, 0, "Log priority for listen socket overflows: 0..7 or -1 to disable");
585 
586 static struct timeval overinterval = { 60, 0 };
587 SYSCTL_TIMEVAL_SEC(_kern_ipc, OID_AUTO, sooverinterval, CTLFLAG_RW,
588     &overinterval,
589     "Delay in seconds between warnings for listen socket overflows");
590 
591 /*
592  * When an attempt at a new connection is noted on a socket which supports
593  * accept(2), the protocol has two options:
594  * 1) Call legacy sonewconn() function, which would call protocol attach
595  *    method, same as used for socket(2).
596  * 2) Call solisten_clone(), do attach that is specific to a cloned connection,
597  *    and then call solisten_enqueue().
598  *
599  * Note: the ref count on the socket is 0 on return.
600  */
601 struct socket *
602 solisten_clone(struct socket *head)
603 {
604 	struct sbuf descrsb;
605 	struct socket *so;
606 	int len, overcount;
607 	u_int qlen;
608 	const char localprefix[] = "local:";
609 	char descrbuf[SUNPATHLEN + sizeof(localprefix)];
610 #if defined(INET6)
611 	char addrbuf[INET6_ADDRSTRLEN];
612 #elif defined(INET)
613 	char addrbuf[INET_ADDRSTRLEN];
614 #endif
615 	bool dolog, over;
616 
617 	SOLISTEN_LOCK(head);
618 	over = (head->sol_qlen > 3 * head->sol_qlimit / 2);
619 #ifdef REGRESSION
620 	if (regression_sonewconn_earlytest && over) {
621 #else
622 	if (over) {
623 #endif
624 		head->sol_overcount++;
625 		dolog = (sooverprio >= 0) &&
626 			!!ratecheck(&head->sol_lastover, &overinterval);
627 
628 		/*
629 		 * If we're going to log, copy the overflow count and queue
630 		 * length from the listen socket before dropping the lock.
631 		 * Also, reset the overflow count.
632 		 */
633 		if (dolog) {
634 			overcount = head->sol_overcount;
635 			head->sol_overcount = 0;
636 			qlen = head->sol_qlen;
637 		}
638 		SOLISTEN_UNLOCK(head);
639 
640 		if (dolog) {
641 			/*
642 			 * Try to print something descriptive about the
643 			 * socket for the error message.
644 			 */
645 			sbuf_new(&descrsb, descrbuf, sizeof(descrbuf),
646 			    SBUF_FIXEDLEN);
647 			switch (head->so_proto->pr_domain->dom_family) {
648 #if defined(INET) || defined(INET6)
649 #ifdef INET
650 			case AF_INET:
651 #endif
652 #ifdef INET6
653 			case AF_INET6:
654 				if (head->so_proto->pr_domain->dom_family ==
655 				    AF_INET6 ||
656 				    (sotoinpcb(head)->inp_inc.inc_flags &
657 				    INC_ISIPV6)) {
658 					ip6_sprintf(addrbuf,
659 					    &sotoinpcb(head)->inp_inc.inc6_laddr);
660 					sbuf_printf(&descrsb, "[%s]", addrbuf);
661 				} else
662 #endif
663 				{
664 #ifdef INET
665 					inet_ntoa_r(
666 					    sotoinpcb(head)->inp_inc.inc_laddr,
667 					    addrbuf);
668 					sbuf_cat(&descrsb, addrbuf);
669 #endif
670 				}
671 				sbuf_printf(&descrsb, ":%hu (proto %u)",
672 				    ntohs(sotoinpcb(head)->inp_inc.inc_lport),
673 				    head->so_proto->pr_protocol);
674 				break;
675 #endif /* INET || INET6 */
676 			case AF_UNIX:
677 				sbuf_cat(&descrsb, localprefix);
678 				if (sotounpcb(head)->unp_addr != NULL)
679 					len =
680 					    sotounpcb(head)->unp_addr->sun_len -
681 					    offsetof(struct sockaddr_un,
682 					    sun_path);
683 				else
684 					len = 0;
685 				if (len > 0)
686 					sbuf_bcat(&descrsb,
687 					    sotounpcb(head)->unp_addr->sun_path,
688 					    len);
689 				else
690 					sbuf_cat(&descrsb, "(unknown)");
691 				break;
692 			}
693 
694 			/*
695 			 * If we can't print something more specific, at least
696 			 * print the domain name.
697 			 */
698 			if (sbuf_finish(&descrsb) != 0 ||
699 			    sbuf_len(&descrsb) <= 0) {
700 				sbuf_clear(&descrsb);
701 				sbuf_cat(&descrsb,
702 				    head->so_proto->pr_domain->dom_name ?:
703 				    "unknown");
704 				sbuf_finish(&descrsb);
705 			}
706 			KASSERT(sbuf_len(&descrsb) > 0,
707 			    ("%s: sbuf creation failed", __func__));
708 			/*
709 			 * Preserve the historic listen queue overflow log
710 			 * message, that starts with "sonewconn:".  It has
711 			 * been known to sysadmins for years and also test
712 			 * sys/kern/sonewconn_overflow checks for it.
713 			 */
714 			if (head->so_cred == 0) {
715 				log(LOG_PRI(sooverprio),
716 				    "sonewconn: pcb %p (%s): "
717 				    "Listen queue overflow: %i already in "
718 				    "queue awaiting acceptance (%d "
719 				    "occurrences)\n", head->so_pcb,
720 				    sbuf_data(&descrsb),
721 			    	qlen, overcount);
722 			} else {
723 				log(LOG_PRI(sooverprio),
724 				    "sonewconn: pcb %p (%s): "
725 				    "Listen queue overflow: "
726 				    "%i already in queue awaiting acceptance "
727 				    "(%d occurrences), euid %d, rgid %d, jail %s\n",
728 				    head->so_pcb, sbuf_data(&descrsb), qlen,
729 				    overcount, head->so_cred->cr_uid,
730 				    head->so_cred->cr_rgid,
731 				    head->so_cred->cr_prison ?
732 					head->so_cred->cr_prison->pr_name :
733 					"not_jailed");
734 			}
735 			sbuf_delete(&descrsb);
736 
737 			overcount = 0;
738 		}
739 
740 		return (NULL);
741 	}
742 	SOLISTEN_UNLOCK(head);
743 	VNET_ASSERT(head->so_vnet != NULL, ("%s: so %p vnet is NULL",
744 	    __func__, head));
745 	so = soalloc(head->so_vnet);
746 	if (so == NULL) {
747 		log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: "
748 		    "limit reached or out of memory\n",
749 		    __func__, head->so_pcb);
750 		return (NULL);
751 	}
752 	so->so_listen = head;
753 	so->so_type = head->so_type;
754 	/*
755 	 * POSIX is ambiguous on what options an accept(2)ed socket should
756 	 * inherit from the listener.  Words "create a new socket" may be
757 	 * interpreted as not inheriting anything.  Best programming practice
758 	 * for application developers is to not rely on such inheritance.
759 	 * FreeBSD had historically inherited all so_options excluding
760 	 * SO_ACCEPTCONN, which virtually means all SOL_SOCKET level options,
761 	 * including those completely irrelevant to a new born socket.  For
762 	 * compatibility with older versions we will inherit a list of
763 	 * meaningful options.
764 	 */
765 	so->so_options = head->so_options & (SO_KEEPALIVE | SO_DONTROUTE |
766 	    SO_LINGER | SO_OOBINLINE | SO_NOSIGPIPE);
767 	so->so_linger = head->so_linger;
768 	so->so_state = head->so_state;
769 	so->so_fibnum = head->so_fibnum;
770 	so->so_proto = head->so_proto;
771 	so->so_cred = crhold(head->so_cred);
772 	if (V_socket_hhh[HHOOK_SOCKET_NEWCONN]->hhh_nhooks > 0) {
773 		if (hhook_run_socket(so, head, HHOOK_SOCKET_NEWCONN)) {
774 			sodealloc(so);
775 			log(LOG_DEBUG, "%s: hhook run failed\n", __func__);
776 			return (NULL);
777 		}
778 	}
779 #ifdef MAC
780 	mac_socket_newconn(head, so);
781 #endif
782 	knlist_init(&so->so_rdsel.si_note, so, so_rdknl_lock, so_rdknl_unlock,
783 	    so_rdknl_assert_lock);
784 	knlist_init(&so->so_wrsel.si_note, so, so_wrknl_lock, so_wrknl_unlock,
785 	    so_wrknl_assert_lock);
786 	VNET_SO_ASSERT(head);
787 	if (soreserve(so, head->sol_sbsnd_hiwat, head->sol_sbrcv_hiwat)) {
788 		sodealloc(so);
789 		log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n",
790 		    __func__, head->so_pcb);
791 		return (NULL);
792 	}
793 	so->so_rcv.sb_lowat = head->sol_sbrcv_lowat;
794 	so->so_snd.sb_lowat = head->sol_sbsnd_lowat;
795 	so->so_rcv.sb_timeo = head->sol_sbrcv_timeo;
796 	so->so_snd.sb_timeo = head->sol_sbsnd_timeo;
797 	so->so_rcv.sb_flags = head->sol_sbrcv_flags & SB_AUTOSIZE;
798 	so->so_snd.sb_flags = head->sol_sbsnd_flags & SB_AUTOSIZE;
799 	if ((so->so_proto->pr_flags & PR_SOCKBUF) == 0) {
800 		so->so_snd.sb_mtx = &so->so_snd_mtx;
801 		so->so_rcv.sb_mtx = &so->so_rcv_mtx;
802 	}
803 
804 	return (so);
805 }
806 
807 /* Connstatus may be 0 or SS_ISCONNECTED. */
808 struct socket *
809 sonewconn(struct socket *head, int connstatus)
810 {
811 	struct socket *so;
812 
813 	if ((so = solisten_clone(head)) == NULL)
814 		return (NULL);
815 
816 	if (so->so_proto->pr_attach(so, 0, NULL) != 0) {
817 		sodealloc(so);
818 		log(LOG_DEBUG, "%s: pcb %p: pr_attach() failed\n",
819 		    __func__, head->so_pcb);
820 		return (NULL);
821 	}
822 
823 	(void)solisten_enqueue(so, connstatus);
824 
825 	return (so);
826 }
827 
828 /*
829  * Enqueue socket cloned by solisten_clone() to the listen queue of the
830  * listener it has been cloned from.
831  *
832  * Return 'true' if socket landed on complete queue, otherwise 'false'.
833  */
834 bool
835 solisten_enqueue(struct socket *so, int connstatus)
836 {
837 	struct socket *head = so->so_listen;
838 
839 	MPASS(refcount_load(&so->so_count) == 0);
840 	refcount_init(&so->so_count, 1);
841 
842 	SOLISTEN_LOCK(head);
843 	if (head->sol_accept_filter != NULL)
844 		connstatus = 0;
845 	so->so_state |= connstatus;
846 	soref(head); /* A socket on (in)complete queue refs head. */
847 	if (connstatus) {
848 		TAILQ_INSERT_TAIL(&head->sol_comp, so, so_list);
849 		so->so_qstate = SQ_COMP;
850 		head->sol_qlen++;
851 		solisten_wakeup(head);	/* unlocks */
852 		return (true);
853 	} else {
854 		/*
855 		 * Keep removing sockets from the head until there's room for
856 		 * us to insert on the tail.  In pre-locking revisions, this
857 		 * was a simple if(), but as we could be racing with other
858 		 * threads and soabort() requires dropping locks, we must
859 		 * loop waiting for the condition to be true.
860 		 */
861 		while (head->sol_incqlen > head->sol_qlimit) {
862 			struct socket *sp;
863 
864 			sp = TAILQ_FIRST(&head->sol_incomp);
865 			TAILQ_REMOVE(&head->sol_incomp, sp, so_list);
866 			head->sol_incqlen--;
867 			SOCK_LOCK(sp);
868 			sp->so_qstate = SQ_NONE;
869 			sp->so_listen = NULL;
870 			SOCK_UNLOCK(sp);
871 			sorele_locked(head);	/* does SOLISTEN_UNLOCK, head stays */
872 			soabort(sp);
873 			SOLISTEN_LOCK(head);
874 		}
875 		TAILQ_INSERT_TAIL(&head->sol_incomp, so, so_list);
876 		so->so_qstate = SQ_INCOMP;
877 		head->sol_incqlen++;
878 		SOLISTEN_UNLOCK(head);
879 		return (false);
880 	}
881 }
882 
883 #if defined(SCTP) || defined(SCTP_SUPPORT)
884 /*
885  * Socket part of sctp_peeloff().  Detach a new socket from an
886  * association.  The new socket is returned with a reference.
887  *
888  * XXXGL: reduce copy-paste with solisten_clone().
889  */
890 struct socket *
891 sopeeloff(struct socket *head)
892 {
893 	struct socket *so;
894 
895 	VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
896 	    __func__, __LINE__, head));
897 	so = soalloc(head->so_vnet);
898 	if (so == NULL) {
899 		log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: "
900 		    "limit reached or out of memory\n",
901 		    __func__, head->so_pcb);
902 		return (NULL);
903 	}
904 	so->so_type = head->so_type;
905 	so->so_options = head->so_options;
906 	so->so_linger = head->so_linger;
907 	so->so_state = (head->so_state & SS_NBIO) | SS_ISCONNECTED;
908 	so->so_fibnum = head->so_fibnum;
909 	so->so_proto = head->so_proto;
910 	so->so_cred = crhold(head->so_cred);
911 #ifdef MAC
912 	mac_socket_newconn(head, so);
913 #endif
914 	knlist_init(&so->so_rdsel.si_note, so, so_rdknl_lock, so_rdknl_unlock,
915 	    so_rdknl_assert_lock);
916 	knlist_init(&so->so_wrsel.si_note, so, so_wrknl_lock, so_wrknl_unlock,
917 	    so_wrknl_assert_lock);
918 	VNET_SO_ASSERT(head);
919 	if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
920 		sodealloc(so);
921 		log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n",
922 		    __func__, head->so_pcb);
923 		return (NULL);
924 	}
925 	if ((*so->so_proto->pr_attach)(so, 0, NULL)) {
926 		sodealloc(so);
927 		log(LOG_DEBUG, "%s: pcb %p: pru_attach() failed\n",
928 		    __func__, head->so_pcb);
929 		return (NULL);
930 	}
931 	so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
932 	so->so_snd.sb_lowat = head->so_snd.sb_lowat;
933 	so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
934 	so->so_snd.sb_timeo = head->so_snd.sb_timeo;
935 	so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
936 	so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
937 	if ((so->so_proto->pr_flags & PR_SOCKBUF) == 0) {
938 		so->so_snd.sb_mtx = &so->so_snd_mtx;
939 		so->so_rcv.sb_mtx = &so->so_rcv_mtx;
940 	}
941 
942 	soref(so);
943 
944 	return (so);
945 }
946 #endif	/* SCTP */
947 
948 int
949 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
950 {
951 	int error;
952 
953 	CURVNET_SET(so->so_vnet);
954 	error = so->so_proto->pr_bind(so, nam, td);
955 	CURVNET_RESTORE();
956 	return (error);
957 }
958 
959 int
960 sobindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
961 {
962 	int error;
963 
964 	CURVNET_SET(so->so_vnet);
965 	error = so->so_proto->pr_bindat(fd, so, nam, td);
966 	CURVNET_RESTORE();
967 	return (error);
968 }
969 
970 /*
971  * solisten() transitions a socket from a non-listening state to a listening
972  * state, but can also be used to update the listen queue depth on an
973  * existing listen socket.  The protocol will call back into the sockets
974  * layer using solisten_proto_check() and solisten_proto() to check and set
975  * socket-layer listen state.  Call backs are used so that the protocol can
976  * acquire both protocol and socket layer locks in whatever order is required
977  * by the protocol.
978  *
979  * Protocol implementors are advised to hold the socket lock across the
980  * socket-layer test and set to avoid races at the socket layer.
981  */
982 int
983 solisten(struct socket *so, int backlog, struct thread *td)
984 {
985 	int error;
986 
987 	CURVNET_SET(so->so_vnet);
988 	error = so->so_proto->pr_listen(so, backlog, td);
989 	CURVNET_RESTORE();
990 	return (error);
991 }
992 
993 /*
994  * Prepare for a call to solisten_proto().  Acquire all socket buffer locks in
995  * order to interlock with socket I/O.
996  */
997 int
998 solisten_proto_check(struct socket *so)
999 {
1000 	SOCK_LOCK_ASSERT(so);
1001 
1002 	if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
1003 	    SS_ISDISCONNECTING)) != 0)
1004 		return (EINVAL);
1005 
1006 	/*
1007 	 * Sleeping is not permitted here, so simply fail if userspace is
1008 	 * attempting to transmit or receive on the socket.  This kind of
1009 	 * transient failure is not ideal, but it should occur only if userspace
1010 	 * is misusing the socket interfaces.
1011 	 */
1012 	if (!sx_try_xlock(&so->so_snd_sx))
1013 		return (EAGAIN);
1014 	if (!sx_try_xlock(&so->so_rcv_sx)) {
1015 		sx_xunlock(&so->so_snd_sx);
1016 		return (EAGAIN);
1017 	}
1018 	mtx_lock(&so->so_snd_mtx);
1019 	mtx_lock(&so->so_rcv_mtx);
1020 
1021 	/* Interlock with soo_aio_queue() and KTLS. */
1022 	if (!SOLISTENING(so)) {
1023 		bool ktls;
1024 
1025 #ifdef KERN_TLS
1026 		ktls = so->so_snd.sb_tls_info != NULL ||
1027 		    so->so_rcv.sb_tls_info != NULL;
1028 #else
1029 		ktls = false;
1030 #endif
1031 		if (ktls ||
1032 		    (so->so_snd.sb_flags & (SB_AIO | SB_AIO_RUNNING)) != 0 ||
1033 		    (so->so_rcv.sb_flags & (SB_AIO | SB_AIO_RUNNING)) != 0) {
1034 			solisten_proto_abort(so);
1035 			return (EINVAL);
1036 		}
1037 	}
1038 
1039 	return (0);
1040 }
1041 
1042 /*
1043  * Undo the setup done by solisten_proto_check().
1044  */
1045 void
1046 solisten_proto_abort(struct socket *so)
1047 {
1048 	mtx_unlock(&so->so_snd_mtx);
1049 	mtx_unlock(&so->so_rcv_mtx);
1050 	sx_xunlock(&so->so_snd_sx);
1051 	sx_xunlock(&so->so_rcv_sx);
1052 }
1053 
1054 void
1055 solisten_proto(struct socket *so, int backlog)
1056 {
1057 	int sbrcv_lowat, sbsnd_lowat;
1058 	u_int sbrcv_hiwat, sbsnd_hiwat;
1059 	short sbrcv_flags, sbsnd_flags;
1060 	sbintime_t sbrcv_timeo, sbsnd_timeo;
1061 
1062 	SOCK_LOCK_ASSERT(so);
1063 	KASSERT((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
1064 	    SS_ISDISCONNECTING)) == 0,
1065 	    ("%s: bad socket state %p", __func__, so));
1066 
1067 	if (SOLISTENING(so))
1068 		goto listening;
1069 
1070 	/*
1071 	 * Change this socket to listening state.
1072 	 */
1073 	sbrcv_lowat = so->so_rcv.sb_lowat;
1074 	sbsnd_lowat = so->so_snd.sb_lowat;
1075 	sbrcv_hiwat = so->so_rcv.sb_hiwat;
1076 	sbsnd_hiwat = so->so_snd.sb_hiwat;
1077 	sbrcv_flags = so->so_rcv.sb_flags;
1078 	sbsnd_flags = so->so_snd.sb_flags;
1079 	sbrcv_timeo = so->so_rcv.sb_timeo;
1080 	sbsnd_timeo = so->so_snd.sb_timeo;
1081 
1082 	if (!(so->so_proto->pr_flags & PR_SOCKBUF)) {
1083 		sbdestroy(so, SO_SND);
1084 		sbdestroy(so, SO_RCV);
1085 	}
1086 
1087 #ifdef INVARIANTS
1088 	bzero(&so->so_rcv,
1089 	    sizeof(struct socket) - offsetof(struct socket, so_rcv));
1090 #endif
1091 
1092 	so->sol_sbrcv_lowat = sbrcv_lowat;
1093 	so->sol_sbsnd_lowat = sbsnd_lowat;
1094 	so->sol_sbrcv_hiwat = sbrcv_hiwat;
1095 	so->sol_sbsnd_hiwat = sbsnd_hiwat;
1096 	so->sol_sbrcv_flags = sbrcv_flags;
1097 	so->sol_sbsnd_flags = sbsnd_flags;
1098 	so->sol_sbrcv_timeo = sbrcv_timeo;
1099 	so->sol_sbsnd_timeo = sbsnd_timeo;
1100 
1101 	so->sol_qlen = so->sol_incqlen = 0;
1102 	TAILQ_INIT(&so->sol_incomp);
1103 	TAILQ_INIT(&so->sol_comp);
1104 
1105 	so->sol_accept_filter = NULL;
1106 	so->sol_accept_filter_arg = NULL;
1107 	so->sol_accept_filter_str = NULL;
1108 
1109 	so->sol_upcall = NULL;
1110 	so->sol_upcallarg = NULL;
1111 
1112 	so->so_options |= SO_ACCEPTCONN;
1113 
1114 listening:
1115 	if (backlog < 0 || backlog > somaxconn)
1116 		backlog = somaxconn;
1117 	so->sol_qlimit = backlog;
1118 
1119 	mtx_unlock(&so->so_snd_mtx);
1120 	mtx_unlock(&so->so_rcv_mtx);
1121 	sx_xunlock(&so->so_snd_sx);
1122 	sx_xunlock(&so->so_rcv_sx);
1123 }
1124 
1125 /*
1126  * Wakeup listeners/subsystems once we have a complete connection.
1127  * Enters with lock, returns unlocked.
1128  */
1129 void
1130 solisten_wakeup(struct socket *sol)
1131 {
1132 
1133 	if (sol->sol_upcall != NULL)
1134 		(void )sol->sol_upcall(sol, sol->sol_upcallarg, M_NOWAIT);
1135 	else {
1136 		selwakeuppri(&sol->so_rdsel, PSOCK);
1137 		KNOTE_LOCKED(&sol->so_rdsel.si_note, 0);
1138 	}
1139 	SOLISTEN_UNLOCK(sol);
1140 	wakeup_one(&sol->sol_comp);
1141 	if ((sol->so_state & SS_ASYNC) && sol->so_sigio != NULL)
1142 		pgsigio(&sol->so_sigio, SIGIO, 0);
1143 }
1144 
1145 /*
1146  * Return single connection off a listening socket queue.  Main consumer of
1147  * the function is kern_accept4().  Some modules, that do their own accept
1148  * management also use the function.  The socket reference held by the
1149  * listen queue is handed to the caller.
1150  *
1151  * Listening socket must be locked on entry and is returned unlocked on
1152  * return.
1153  * The flags argument is set of accept4(2) flags and ACCEPT4_INHERIT.
1154  */
1155 int
1156 solisten_dequeue(struct socket *head, struct socket **ret, int flags)
1157 {
1158 	struct socket *so;
1159 	int error;
1160 
1161 	SOLISTEN_LOCK_ASSERT(head);
1162 
1163 	while (!(head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->sol_comp) &&
1164 	    head->so_error == 0) {
1165 		error = msleep(&head->sol_comp, SOCK_MTX(head), PSOCK | PCATCH,
1166 		    "accept", 0);
1167 		if (error != 0) {
1168 			SOLISTEN_UNLOCK(head);
1169 			return (error);
1170 		}
1171 	}
1172 	if (head->so_error) {
1173 		error = head->so_error;
1174 		head->so_error = 0;
1175 	} else if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->sol_comp))
1176 		error = EWOULDBLOCK;
1177 	else
1178 		error = 0;
1179 	if (error) {
1180 		SOLISTEN_UNLOCK(head);
1181 		return (error);
1182 	}
1183 	so = TAILQ_FIRST(&head->sol_comp);
1184 	SOCK_LOCK(so);
1185 	KASSERT(so->so_qstate == SQ_COMP,
1186 	    ("%s: so %p not SQ_COMP", __func__, so));
1187 	head->sol_qlen--;
1188 	so->so_qstate = SQ_NONE;
1189 	so->so_listen = NULL;
1190 	TAILQ_REMOVE(&head->sol_comp, so, so_list);
1191 	if (flags & ACCEPT4_INHERIT)
1192 		so->so_state |= (head->so_state & SS_NBIO);
1193 	else
1194 		so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
1195 	SOCK_UNLOCK(so);
1196 	sorele_locked(head);
1197 
1198 	*ret = so;
1199 	return (0);
1200 }
1201 
1202 /*
1203  * Free socket upon release of the very last reference.
1204  */
1205 static void
1206 sofree(struct socket *so)
1207 {
1208 	struct protosw *pr = so->so_proto;
1209 
1210 	SOCK_LOCK_ASSERT(so);
1211 	KASSERT(refcount_load(&so->so_count) == 0,
1212 	    ("%s: so %p has references", __func__, so));
1213 	KASSERT(SOLISTENING(so) || so->so_qstate == SQ_NONE,
1214 	    ("%s: so %p is on listen queue", __func__, so));
1215 
1216 	SOCK_UNLOCK(so);
1217 
1218 	if (so->so_dtor != NULL)
1219 		so->so_dtor(so);
1220 
1221 	VNET_SO_ASSERT(so);
1222 	if (pr->pr_detach != NULL)
1223 		pr->pr_detach(so);
1224 
1225 	/*
1226 	 * From this point on, we assume that no other references to this
1227 	 * socket exist anywhere else in the stack.  Therefore, no locks need
1228 	 * to be acquired or held.
1229 	 */
1230 	if (!(pr->pr_flags & PR_SOCKBUF) && !SOLISTENING(so)) {
1231 		sbdestroy(so, SO_SND);
1232 		sbdestroy(so, SO_RCV);
1233 	}
1234 	seldrain(&so->so_rdsel);
1235 	seldrain(&so->so_wrsel);
1236 	knlist_destroy(&so->so_rdsel.si_note);
1237 	knlist_destroy(&so->so_wrsel.si_note);
1238 	sodealloc(so);
1239 }
1240 
1241 /*
1242  * Release a reference on a socket while holding the socket lock.
1243  * Unlocks the socket lock before returning.
1244  */
1245 void
1246 sorele_locked(struct socket *so)
1247 {
1248 	SOCK_LOCK_ASSERT(so);
1249 	if (refcount_release(&so->so_count))
1250 		sofree(so);
1251 	else
1252 		SOCK_UNLOCK(so);
1253 }
1254 
1255 /*
1256  * Close a socket on last file table reference removal.  Initiate disconnect
1257  * if connected.  Free socket when disconnect complete.
1258  *
1259  * This function will sorele() the socket.  Note that soclose() may be called
1260  * prior to the ref count reaching zero.  The actual socket structure will
1261  * not be freed until the ref count reaches zero.
1262  */
1263 int
1264 soclose(struct socket *so)
1265 {
1266 	struct accept_queue lqueue;
1267 	int error = 0;
1268 	bool listening, last __diagused;
1269 
1270 	CURVNET_SET(so->so_vnet);
1271 	funsetown(&so->so_sigio);
1272 	if (so->so_state & SS_ISCONNECTED) {
1273 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
1274 			error = sodisconnect(so);
1275 			if (error) {
1276 				if (error == ENOTCONN)
1277 					error = 0;
1278 				goto drop;
1279 			}
1280 		}
1281 
1282 		if ((so->so_options & SO_LINGER) != 0 && so->so_linger != 0) {
1283 			if ((so->so_state & SS_ISDISCONNECTING) &&
1284 			    (so->so_state & SS_NBIO))
1285 				goto drop;
1286 			while (so->so_state & SS_ISCONNECTED) {
1287 				error = tsleep(&so->so_timeo,
1288 				    PSOCK | PCATCH, "soclos",
1289 				    so->so_linger * hz);
1290 				if (error)
1291 					break;
1292 			}
1293 		}
1294 	}
1295 
1296 drop:
1297 	if (so->so_proto->pr_close != NULL)
1298 		so->so_proto->pr_close(so);
1299 
1300 	SOCK_LOCK(so);
1301 	if ((listening = SOLISTENING(so))) {
1302 		struct socket *sp;
1303 
1304 		TAILQ_INIT(&lqueue);
1305 		TAILQ_SWAP(&lqueue, &so->sol_incomp, socket, so_list);
1306 		TAILQ_CONCAT(&lqueue, &so->sol_comp, so_list);
1307 
1308 		so->sol_qlen = so->sol_incqlen = 0;
1309 
1310 		TAILQ_FOREACH(sp, &lqueue, so_list) {
1311 			SOCK_LOCK(sp);
1312 			sp->so_qstate = SQ_NONE;
1313 			sp->so_listen = NULL;
1314 			SOCK_UNLOCK(sp);
1315 			last = refcount_release(&so->so_count);
1316 			KASSERT(!last, ("%s: released last reference for %p",
1317 			    __func__, so));
1318 		}
1319 	}
1320 	sorele_locked(so);
1321 	if (listening) {
1322 		struct socket *sp, *tsp;
1323 
1324 		TAILQ_FOREACH_SAFE(sp, &lqueue, so_list, tsp)
1325 			soabort(sp);
1326 	}
1327 	CURVNET_RESTORE();
1328 	return (error);
1329 }
1330 
1331 /*
1332  * soabort() is used to abruptly tear down a connection, such as when a
1333  * resource limit is reached (listen queue depth exceeded), or if a listen
1334  * socket is closed while there are sockets waiting to be accepted.
1335  *
1336  * This interface is tricky, because it is called on an unreferenced socket,
1337  * and must be called only by a thread that has actually removed the socket
1338  * from the listen queue it was on.  Likely this thread holds the last
1339  * reference on the socket and soabort() will proceed with sofree().  But
1340  * it might be not the last, as the sockets on the listen queues are seen
1341  * from the protocol side.
1342  *
1343  * This interface will call into the protocol code, so must not be called
1344  * with any socket locks held.  Protocols do call it while holding their own
1345  * recursible protocol mutexes, but this is something that should be subject
1346  * to review in the future.
1347  *
1348  * Usually socket should have a single reference left, but this is not a
1349  * requirement.  In the past, when we have had named references for file
1350  * descriptor and protocol, we asserted that none of them are being held.
1351  */
1352 void
1353 soabort(struct socket *so)
1354 {
1355 
1356 	VNET_SO_ASSERT(so);
1357 
1358 	if (so->so_proto->pr_abort != NULL)
1359 		so->so_proto->pr_abort(so);
1360 	SOCK_LOCK(so);
1361 	sorele_locked(so);
1362 }
1363 
1364 int
1365 soaccept(struct socket *so, struct sockaddr *sa)
1366 {
1367 #ifdef INVARIANTS
1368 	u_char len = sa->sa_len;
1369 #endif
1370 	int error;
1371 
1372 	CURVNET_SET(so->so_vnet);
1373 	error = so->so_proto->pr_accept(so, sa);
1374 	KASSERT(sa->sa_len <= len,
1375 	    ("%s: protocol %p sockaddr overflow", __func__, so->so_proto));
1376 	CURVNET_RESTORE();
1377 	return (error);
1378 }
1379 
1380 int
1381 sopeeraddr(struct socket *so, struct sockaddr *sa)
1382 {
1383 #ifdef INVARIANTS
1384 	u_char len = sa->sa_len;
1385 #endif
1386 	int error;
1387 
1388 	CURVNET_SET(so->so_vnet);
1389 	error = so->so_proto->pr_peeraddr(so, sa);
1390 	KASSERT(sa->sa_len <= len,
1391 	    ("%s: protocol %p sockaddr overflow", __func__, so->so_proto));
1392 	CURVNET_RESTORE();
1393 
1394 	return (error);
1395 }
1396 
1397 int
1398 sosockaddr(struct socket *so, struct sockaddr *sa)
1399 {
1400 #ifdef INVARIANTS
1401 	u_char len = sa->sa_len;
1402 #endif
1403 	int error;
1404 
1405 	CURVNET_SET(so->so_vnet);
1406 	error = so->so_proto->pr_sockaddr(so, sa);
1407 	KASSERT(sa->sa_len <= len,
1408 	    ("%s: protocol %p sockaddr overflow", __func__, so->so_proto));
1409 	CURVNET_RESTORE();
1410 
1411 	return (error);
1412 }
1413 
1414 int
1415 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
1416 {
1417 
1418 	return (soconnectat(AT_FDCWD, so, nam, td));
1419 }
1420 
1421 int
1422 soconnectat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
1423 {
1424 	int error;
1425 
1426 	CURVNET_SET(so->so_vnet);
1427 
1428 	/*
1429 	 * If protocol is connection-based, can only connect once.
1430 	 * Otherwise, if connected, try to disconnect first.  This allows
1431 	 * user to disconnect by connecting to, e.g., a null address.
1432 	 *
1433 	 * Note, this check is racy and may need to be re-evaluated at the
1434 	 * protocol layer.
1435 	 */
1436 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
1437 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
1438 	    (error = sodisconnect(so)))) {
1439 		error = EISCONN;
1440 	} else {
1441 		/*
1442 		 * Prevent accumulated error from previous connection from
1443 		 * biting us.
1444 		 */
1445 		so->so_error = 0;
1446 		if (fd == AT_FDCWD) {
1447 			error = so->so_proto->pr_connect(so, nam, td);
1448 		} else {
1449 			error = so->so_proto->pr_connectat(fd, so, nam, td);
1450 		}
1451 	}
1452 	CURVNET_RESTORE();
1453 
1454 	return (error);
1455 }
1456 
1457 int
1458 soconnect2(struct socket *so1, struct socket *so2)
1459 {
1460 	int error;
1461 
1462 	CURVNET_SET(so1->so_vnet);
1463 	error = so1->so_proto->pr_connect2(so1, so2);
1464 	CURVNET_RESTORE();
1465 	return (error);
1466 }
1467 
1468 int
1469 sodisconnect(struct socket *so)
1470 {
1471 	int error;
1472 
1473 	if ((so->so_state & SS_ISCONNECTED) == 0)
1474 		return (ENOTCONN);
1475 	if (so->so_state & SS_ISDISCONNECTING)
1476 		return (EALREADY);
1477 	VNET_SO_ASSERT(so);
1478 	error = so->so_proto->pr_disconnect(so);
1479 	return (error);
1480 }
1481 
1482 int
1483 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
1484     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1485 {
1486 	long space;
1487 	ssize_t resid;
1488 	int clen = 0, error, dontroute;
1489 
1490 	KASSERT(so->so_type == SOCK_DGRAM, ("sosend_dgram: !SOCK_DGRAM"));
1491 	KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
1492 	    ("sosend_dgram: !PR_ATOMIC"));
1493 
1494 	if (uio != NULL)
1495 		resid = uio->uio_resid;
1496 	else
1497 		resid = top->m_pkthdr.len;
1498 	/*
1499 	 * In theory resid should be unsigned.  However, space must be
1500 	 * signed, as it might be less than 0 if we over-committed, and we
1501 	 * must use a signed comparison of space and resid.  On the other
1502 	 * hand, a negative resid causes us to loop sending 0-length
1503 	 * segments to the protocol.
1504 	 */
1505 	if (resid < 0) {
1506 		error = EINVAL;
1507 		goto out;
1508 	}
1509 
1510 	dontroute =
1511 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
1512 	if (td != NULL)
1513 		td->td_ru.ru_msgsnd++;
1514 	if (control != NULL)
1515 		clen = control->m_len;
1516 
1517 	SOCKBUF_LOCK(&so->so_snd);
1518 	if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1519 		SOCKBUF_UNLOCK(&so->so_snd);
1520 		error = EPIPE;
1521 		goto out;
1522 	}
1523 	if (so->so_error) {
1524 		error = so->so_error;
1525 		so->so_error = 0;
1526 		SOCKBUF_UNLOCK(&so->so_snd);
1527 		goto out;
1528 	}
1529 	if ((so->so_state & SS_ISCONNECTED) == 0) {
1530 		/*
1531 		 * `sendto' and `sendmsg' is allowed on a connection-based
1532 		 * socket if it supports implied connect.  Return ENOTCONN if
1533 		 * not connected and no address is supplied.
1534 		 */
1535 		if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1536 		    (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1537 			if (!(resid == 0 && clen != 0)) {
1538 				SOCKBUF_UNLOCK(&so->so_snd);
1539 				error = ENOTCONN;
1540 				goto out;
1541 			}
1542 		} else if (addr == NULL) {
1543 			if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1544 				error = ENOTCONN;
1545 			else
1546 				error = EDESTADDRREQ;
1547 			SOCKBUF_UNLOCK(&so->so_snd);
1548 			goto out;
1549 		}
1550 	}
1551 
1552 	/*
1553 	 * Do we need MSG_OOB support in SOCK_DGRAM?  Signs here may be a
1554 	 * problem and need fixing.
1555 	 */
1556 	space = sbspace(&so->so_snd);
1557 	if (flags & MSG_OOB)
1558 		space += 1024;
1559 	space -= clen;
1560 	SOCKBUF_UNLOCK(&so->so_snd);
1561 	if (resid > space) {
1562 		error = EMSGSIZE;
1563 		goto out;
1564 	}
1565 	if (uio == NULL) {
1566 		resid = 0;
1567 		if (flags & MSG_EOR)
1568 			top->m_flags |= M_EOR;
1569 	} else {
1570 		/*
1571 		 * Copy the data from userland into a mbuf chain.
1572 		 * If no data is to be copied in, a single empty mbuf
1573 		 * is returned.
1574 		 */
1575 		top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1576 		    (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1577 		if (top == NULL) {
1578 			error = EFAULT;	/* only possible error */
1579 			goto out;
1580 		}
1581 		space -= resid - uio->uio_resid;
1582 		resid = uio->uio_resid;
1583 	}
1584 	KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1585 	/*
1586 	 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1587 	 * than with.
1588 	 */
1589 	if (dontroute) {
1590 		SOCK_LOCK(so);
1591 		so->so_options |= SO_DONTROUTE;
1592 		SOCK_UNLOCK(so);
1593 	}
1594 	/*
1595 	 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1596 	 * of date.  We could have received a reset packet in an interrupt or
1597 	 * maybe we slept while doing page faults in uiomove() etc.  We could
1598 	 * probably recheck again inside the locking protection here, but
1599 	 * there are probably other places that this also happens.  We must
1600 	 * rethink this.
1601 	 */
1602 	VNET_SO_ASSERT(so);
1603 	error = so->so_proto->pr_send(so, (flags & MSG_OOB) ? PRUS_OOB :
1604 	/*
1605 	 * If the user set MSG_EOF, the protocol understands this flag and
1606 	 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1607 	 */
1608 	    ((flags & MSG_EOF) &&
1609 	     (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1610 	     (resid <= 0)) ?
1611 		PRUS_EOF :
1612 		/* If there is more to send set PRUS_MORETOCOME */
1613 		(flags & MSG_MORETOCOME) ||
1614 		(resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1615 		top, addr, control, td);
1616 	if (dontroute) {
1617 		SOCK_LOCK(so);
1618 		so->so_options &= ~SO_DONTROUTE;
1619 		SOCK_UNLOCK(so);
1620 	}
1621 	clen = 0;
1622 	control = NULL;
1623 	top = NULL;
1624 out:
1625 	if (top != NULL)
1626 		m_freem(top);
1627 	if (control != NULL)
1628 		m_freem(control);
1629 	return (error);
1630 }
1631 
1632 /*
1633  * Send on a socket.  If send must go all at once and message is larger than
1634  * send buffering, then hard error.  Lock against other senders.  If must go
1635  * all at once and not enough room now, then inform user that this would
1636  * block and do nothing.  Otherwise, if nonblocking, send as much as
1637  * possible.  The data to be sent is described by "uio" if nonzero, otherwise
1638  * by the mbuf chain "top" (which must be null if uio is not).  Data provided
1639  * in mbuf chain must be small enough to send all at once.
1640  *
1641  * Returns nonzero on error, timeout or signal; callers must check for short
1642  * counts if EINTR/ERESTART are returned.  Data and control buffers are freed
1643  * on return.
1644  */
1645 int
1646 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1647     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1648 {
1649 	long space;
1650 	ssize_t resid;
1651 	int clen = 0, error, dontroute;
1652 	int atomic = sosendallatonce(so) || top;
1653 	int pr_send_flag;
1654 #ifdef KERN_TLS
1655 	struct ktls_session *tls;
1656 	int tls_enq_cnt, tls_send_flag;
1657 	uint8_t tls_rtype;
1658 
1659 	tls = NULL;
1660 	tls_rtype = TLS_RLTYPE_APP;
1661 #endif
1662 	if (uio != NULL)
1663 		resid = uio->uio_resid;
1664 	else if ((top->m_flags & M_PKTHDR) != 0)
1665 		resid = top->m_pkthdr.len;
1666 	else
1667 		resid = m_length(top, NULL);
1668 	/*
1669 	 * In theory resid should be unsigned.  However, space must be
1670 	 * signed, as it might be less than 0 if we over-committed, and we
1671 	 * must use a signed comparison of space and resid.  On the other
1672 	 * hand, a negative resid causes us to loop sending 0-length
1673 	 * segments to the protocol.
1674 	 *
1675 	 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1676 	 * type sockets since that's an error.
1677 	 */
1678 	if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1679 		error = EINVAL;
1680 		goto out;
1681 	}
1682 
1683 	dontroute =
1684 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1685 	    (so->so_proto->pr_flags & PR_ATOMIC);
1686 	if (td != NULL)
1687 		td->td_ru.ru_msgsnd++;
1688 	if (control != NULL)
1689 		clen = control->m_len;
1690 
1691 	error = SOCK_IO_SEND_LOCK(so, SBLOCKWAIT(flags));
1692 	if (error)
1693 		goto out;
1694 
1695 #ifdef KERN_TLS
1696 	tls_send_flag = 0;
1697 	tls = ktls_hold(so->so_snd.sb_tls_info);
1698 	if (tls != NULL) {
1699 		if (tls->mode == TCP_TLS_MODE_SW)
1700 			tls_send_flag = PRUS_NOTREADY;
1701 
1702 		if (control != NULL) {
1703 			struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1704 
1705 			if (clen >= sizeof(*cm) &&
1706 			    cm->cmsg_type == TLS_SET_RECORD_TYPE) {
1707 				tls_rtype = *((uint8_t *)CMSG_DATA(cm));
1708 				clen = 0;
1709 				m_freem(control);
1710 				control = NULL;
1711 				atomic = 1;
1712 			}
1713 		}
1714 
1715 		if (resid == 0 && !ktls_permit_empty_frames(tls)) {
1716 			error = EINVAL;
1717 			goto release;
1718 		}
1719 	}
1720 #endif
1721 
1722 restart:
1723 	do {
1724 		SOCKBUF_LOCK(&so->so_snd);
1725 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1726 			SOCKBUF_UNLOCK(&so->so_snd);
1727 			error = EPIPE;
1728 			goto release;
1729 		}
1730 		if (so->so_error) {
1731 			error = so->so_error;
1732 			so->so_error = 0;
1733 			SOCKBUF_UNLOCK(&so->so_snd);
1734 			goto release;
1735 		}
1736 		if ((so->so_state & SS_ISCONNECTED) == 0) {
1737 			/*
1738 			 * `sendto' and `sendmsg' is allowed on a connection-
1739 			 * based socket if it supports implied connect.
1740 			 * Return ENOTCONN if not connected and no address is
1741 			 * supplied.
1742 			 */
1743 			if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1744 			    (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1745 				if (!(resid == 0 && clen != 0)) {
1746 					SOCKBUF_UNLOCK(&so->so_snd);
1747 					error = ENOTCONN;
1748 					goto release;
1749 				}
1750 			} else if (addr == NULL) {
1751 				SOCKBUF_UNLOCK(&so->so_snd);
1752 				if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1753 					error = ENOTCONN;
1754 				else
1755 					error = EDESTADDRREQ;
1756 				goto release;
1757 			}
1758 		}
1759 		space = sbspace(&so->so_snd);
1760 		if (flags & MSG_OOB)
1761 			space += 1024;
1762 		if ((atomic && resid > so->so_snd.sb_hiwat) ||
1763 		    clen > so->so_snd.sb_hiwat) {
1764 			SOCKBUF_UNLOCK(&so->so_snd);
1765 			error = EMSGSIZE;
1766 			goto release;
1767 		}
1768 		if (space < resid + clen &&
1769 		    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1770 			if ((so->so_state & SS_NBIO) ||
1771 			    (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
1772 				SOCKBUF_UNLOCK(&so->so_snd);
1773 				error = EWOULDBLOCK;
1774 				goto release;
1775 			}
1776 			error = sbwait(so, SO_SND);
1777 			SOCKBUF_UNLOCK(&so->so_snd);
1778 			if (error)
1779 				goto release;
1780 			goto restart;
1781 		}
1782 		SOCKBUF_UNLOCK(&so->so_snd);
1783 		space -= clen;
1784 		do {
1785 			if (uio == NULL) {
1786 				resid = 0;
1787 				if (flags & MSG_EOR)
1788 					top->m_flags |= M_EOR;
1789 #ifdef KERN_TLS
1790 				if (tls != NULL) {
1791 					ktls_frame(top, tls, &tls_enq_cnt,
1792 					    tls_rtype);
1793 					tls_rtype = TLS_RLTYPE_APP;
1794 				}
1795 #endif
1796 			} else {
1797 				/*
1798 				 * Copy the data from userland into a mbuf
1799 				 * chain.  If resid is 0, which can happen
1800 				 * only if we have control to send, then
1801 				 * a single empty mbuf is returned.  This
1802 				 * is a workaround to prevent protocol send
1803 				 * methods to panic.
1804 				 */
1805 #ifdef KERN_TLS
1806 				if (tls != NULL) {
1807 					top = m_uiotombuf(uio, M_WAITOK, space,
1808 					    tls->params.max_frame_len,
1809 					    M_EXTPG |
1810 					    ((flags & MSG_EOR) ? M_EOR : 0));
1811 					if (top != NULL) {
1812 						ktls_frame(top, tls,
1813 						    &tls_enq_cnt, tls_rtype);
1814 					}
1815 					tls_rtype = TLS_RLTYPE_APP;
1816 				} else
1817 #endif
1818 					top = m_uiotombuf(uio, M_WAITOK, space,
1819 					    (atomic ? max_hdr : 0),
1820 					    (atomic ? M_PKTHDR : 0) |
1821 					    ((flags & MSG_EOR) ? M_EOR : 0));
1822 				if (top == NULL) {
1823 					error = EFAULT; /* only possible error */
1824 					goto release;
1825 				}
1826 				space -= resid - uio->uio_resid;
1827 				resid = uio->uio_resid;
1828 			}
1829 			if (dontroute) {
1830 				SOCK_LOCK(so);
1831 				so->so_options |= SO_DONTROUTE;
1832 				SOCK_UNLOCK(so);
1833 			}
1834 			/*
1835 			 * XXX all the SBS_CANTSENDMORE checks previously
1836 			 * done could be out of date.  We could have received
1837 			 * a reset packet in an interrupt or maybe we slept
1838 			 * while doing page faults in uiomove() etc.  We
1839 			 * could probably recheck again inside the locking
1840 			 * protection here, but there are probably other
1841 			 * places that this also happens.  We must rethink
1842 			 * this.
1843 			 */
1844 			VNET_SO_ASSERT(so);
1845 
1846 			pr_send_flag = (flags & MSG_OOB) ? PRUS_OOB :
1847 			/*
1848 			 * If the user set MSG_EOF, the protocol understands
1849 			 * this flag and nothing left to send then use
1850 			 * PRU_SEND_EOF instead of PRU_SEND.
1851 			 */
1852 			    ((flags & MSG_EOF) &&
1853 			     (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1854 			     (resid <= 0)) ?
1855 				PRUS_EOF :
1856 			/* If there is more to send set PRUS_MORETOCOME. */
1857 			    (flags & MSG_MORETOCOME) ||
1858 			    (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0;
1859 
1860 #ifdef KERN_TLS
1861 			pr_send_flag |= tls_send_flag;
1862 #endif
1863 
1864 			error = so->so_proto->pr_send(so, pr_send_flag, top,
1865 			    addr, control, td);
1866 
1867 			if (dontroute) {
1868 				SOCK_LOCK(so);
1869 				so->so_options &= ~SO_DONTROUTE;
1870 				SOCK_UNLOCK(so);
1871 			}
1872 
1873 #ifdef KERN_TLS
1874 			if (tls != NULL && tls->mode == TCP_TLS_MODE_SW) {
1875 				if (error != 0) {
1876 					m_freem(top);
1877 					top = NULL;
1878 				} else {
1879 					soref(so);
1880 					ktls_enqueue(top, so, tls_enq_cnt);
1881 				}
1882 			}
1883 #endif
1884 			clen = 0;
1885 			control = NULL;
1886 			top = NULL;
1887 			if (error)
1888 				goto release;
1889 		} while (resid && space > 0);
1890 	} while (resid);
1891 
1892 release:
1893 	SOCK_IO_SEND_UNLOCK(so);
1894 out:
1895 #ifdef KERN_TLS
1896 	if (tls != NULL)
1897 		ktls_free(tls);
1898 #endif
1899 	if (top != NULL)
1900 		m_freem(top);
1901 	if (control != NULL)
1902 		m_freem(control);
1903 	return (error);
1904 }
1905 
1906 /*
1907  * Send to a socket from a kernel thread.
1908  *
1909  * XXXGL: in almost all cases uio is NULL and the mbuf is supplied.
1910  * Exception is nfs/bootp_subr.c.  It is arguable that the VNET context needs
1911  * to be set at all.  This function should just boil down to a static inline
1912  * calling the protocol method.
1913  */
1914 int
1915 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1916     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1917 {
1918 	int error;
1919 
1920 	CURVNET_SET(so->so_vnet);
1921 	error = so->so_proto->pr_sosend(so, addr, uio,
1922 	    top, control, flags, td);
1923 	CURVNET_RESTORE();
1924 	return (error);
1925 }
1926 
1927 /*
1928  * send(2), write(2) or aio_write(2) on a socket.
1929  */
1930 int
1931 sousrsend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1932     struct mbuf *control, int flags, struct proc *userproc)
1933 {
1934 	struct thread *td;
1935 	ssize_t len;
1936 	int error;
1937 
1938 	td = uio->uio_td;
1939 	len = uio->uio_resid;
1940 	CURVNET_SET(so->so_vnet);
1941 	error = so->so_proto->pr_sosend(so, addr, uio, NULL, control, flags,
1942 	    td);
1943 	CURVNET_RESTORE();
1944 	if (error != 0) {
1945 		/*
1946 		 * Clear transient errors for stream protocols if they made
1947 		 * some progress.  Make exclusion for aio(4) that would
1948 		 * schedule a new write in case of EWOULDBLOCK and clear
1949 		 * error itself.  See soaio_process_job().
1950 		 */
1951 		if (uio->uio_resid != len &&
1952 		    (so->so_proto->pr_flags & PR_ATOMIC) == 0 &&
1953 		    userproc == NULL &&
1954 		    (error == ERESTART || error == EINTR ||
1955 		    error == EWOULDBLOCK))
1956 			error = 0;
1957 		/* Generation of SIGPIPE can be controlled per socket. */
1958 		if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0 &&
1959 		    (flags & MSG_NOSIGNAL) == 0) {
1960 			if (userproc != NULL) {
1961 				/* aio(4) job */
1962 				PROC_LOCK(userproc);
1963 				kern_psignal(userproc, SIGPIPE);
1964 				PROC_UNLOCK(userproc);
1965 			} else {
1966 				PROC_LOCK(td->td_proc);
1967 				tdsignal(td, SIGPIPE);
1968 				PROC_UNLOCK(td->td_proc);
1969 			}
1970 		}
1971 	}
1972 	return (error);
1973 }
1974 
1975 /*
1976  * The part of soreceive() that implements reading non-inline out-of-band
1977  * data from a socket.  For more complete comments, see soreceive(), from
1978  * which this code originated.
1979  *
1980  * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1981  * unable to return an mbuf chain to the caller.
1982  */
1983 static int
1984 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1985 {
1986 	struct protosw *pr = so->so_proto;
1987 	struct mbuf *m;
1988 	int error;
1989 
1990 	KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1991 	VNET_SO_ASSERT(so);
1992 
1993 	m = m_get(M_WAITOK, MT_DATA);
1994 	error = pr->pr_rcvoob(so, m, flags & MSG_PEEK);
1995 	if (error)
1996 		goto bad;
1997 	do {
1998 		error = uiomove(mtod(m, void *),
1999 		    (int) min(uio->uio_resid, m->m_len), uio);
2000 		m = m_free(m);
2001 	} while (uio->uio_resid && error == 0 && m);
2002 bad:
2003 	if (m != NULL)
2004 		m_freem(m);
2005 	return (error);
2006 }
2007 
2008 /*
2009  * Following replacement or removal of the first mbuf on the first mbuf chain
2010  * of a socket buffer, push necessary state changes back into the socket
2011  * buffer so that other consumers see the values consistently.  'nextrecord'
2012  * is the callers locally stored value of the original value of
2013  * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
2014  * NOTE: 'nextrecord' may be NULL.
2015  */
2016 static __inline void
2017 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
2018 {
2019 
2020 	SOCKBUF_LOCK_ASSERT(sb);
2021 	/*
2022 	 * First, update for the new value of nextrecord.  If necessary, make
2023 	 * it the first record.
2024 	 */
2025 	if (sb->sb_mb != NULL)
2026 		sb->sb_mb->m_nextpkt = nextrecord;
2027 	else
2028 		sb->sb_mb = nextrecord;
2029 
2030 	/*
2031 	 * Now update any dependent socket buffer fields to reflect the new
2032 	 * state.  This is an expanded inline of SB_EMPTY_FIXUP(), with the
2033 	 * addition of a second clause that takes care of the case where
2034 	 * sb_mb has been updated, but remains the last record.
2035 	 */
2036 	if (sb->sb_mb == NULL) {
2037 		sb->sb_mbtail = NULL;
2038 		sb->sb_lastrecord = NULL;
2039 	} else if (sb->sb_mb->m_nextpkt == NULL)
2040 		sb->sb_lastrecord = sb->sb_mb;
2041 }
2042 
2043 /*
2044  * Implement receive operations on a socket.  We depend on the way that
2045  * records are added to the sockbuf by sbappend.  In particular, each record
2046  * (mbufs linked through m_next) must begin with an address if the protocol
2047  * so specifies, followed by an optional mbuf or mbufs containing ancillary
2048  * data, and then zero or more mbufs of data.  In order to allow parallelism
2049  * between network receive and copying to user space, as well as avoid
2050  * sleeping with a mutex held, we release the socket buffer mutex during the
2051  * user space copy.  Although the sockbuf is locked, new data may still be
2052  * appended, and thus we must maintain consistency of the sockbuf during that
2053  * time.
2054  *
2055  * The caller may receive the data as a single mbuf chain by supplying an
2056  * mbuf **mp0 for use in returning the chain.  The uio is then used only for
2057  * the count in uio_resid.
2058  */
2059 int
2060 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
2061     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2062 {
2063 	struct mbuf *m, **mp;
2064 	int flags, error, offset;
2065 	ssize_t len;
2066 	struct protosw *pr = so->so_proto;
2067 	struct mbuf *nextrecord;
2068 	int moff, type = 0;
2069 	ssize_t orig_resid = uio->uio_resid;
2070 	bool report_real_len = false;
2071 
2072 	mp = mp0;
2073 	if (psa != NULL)
2074 		*psa = NULL;
2075 	if (controlp != NULL)
2076 		*controlp = NULL;
2077 	if (flagsp != NULL) {
2078 		report_real_len = *flagsp & MSG_TRUNC;
2079 		*flagsp &= ~MSG_TRUNC;
2080 		flags = *flagsp &~ MSG_EOR;
2081 	} else
2082 		flags = 0;
2083 	if (flags & MSG_OOB)
2084 		return (soreceive_rcvoob(so, uio, flags));
2085 	if (mp != NULL)
2086 		*mp = NULL;
2087 
2088 	error = SOCK_IO_RECV_LOCK(so, SBLOCKWAIT(flags));
2089 	if (error)
2090 		return (error);
2091 
2092 restart:
2093 	SOCKBUF_LOCK(&so->so_rcv);
2094 	m = so->so_rcv.sb_mb;
2095 	/*
2096 	 * If we have less data than requested, block awaiting more (subject
2097 	 * to any timeout) if:
2098 	 *   1. the current count is less than the low water mark, or
2099 	 *   2. MSG_DONTWAIT is not set
2100 	 */
2101 	if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
2102 	    sbavail(&so->so_rcv) < uio->uio_resid) &&
2103 	    sbavail(&so->so_rcv) < so->so_rcv.sb_lowat &&
2104 	    m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
2105 		KASSERT(m != NULL || !sbavail(&so->so_rcv),
2106 		    ("receive: m == %p sbavail == %u",
2107 		    m, sbavail(&so->so_rcv)));
2108 		if (so->so_error || so->so_rerror) {
2109 			if (m != NULL)
2110 				goto dontblock;
2111 			if (so->so_error)
2112 				error = so->so_error;
2113 			else
2114 				error = so->so_rerror;
2115 			if ((flags & MSG_PEEK) == 0) {
2116 				if (so->so_error)
2117 					so->so_error = 0;
2118 				else
2119 					so->so_rerror = 0;
2120 			}
2121 			SOCKBUF_UNLOCK(&so->so_rcv);
2122 			goto release;
2123 		}
2124 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2125 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2126 			if (m != NULL)
2127 				goto dontblock;
2128 #ifdef KERN_TLS
2129 			else if (so->so_rcv.sb_tlsdcc == 0 &&
2130 			    so->so_rcv.sb_tlscc == 0) {
2131 #else
2132 			else {
2133 #endif
2134 				SOCKBUF_UNLOCK(&so->so_rcv);
2135 				goto release;
2136 			}
2137 		}
2138 		for (; m != NULL; m = m->m_next)
2139 			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
2140 				m = so->so_rcv.sb_mb;
2141 				goto dontblock;
2142 			}
2143 		if ((so->so_state & (SS_ISCONNECTING | SS_ISCONNECTED |
2144 		    SS_ISDISCONNECTING | SS_ISDISCONNECTED)) == 0 &&
2145 		    (so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) {
2146 			SOCKBUF_UNLOCK(&so->so_rcv);
2147 			error = ENOTCONN;
2148 			goto release;
2149 		}
2150 		if (uio->uio_resid == 0 && !report_real_len) {
2151 			SOCKBUF_UNLOCK(&so->so_rcv);
2152 			goto release;
2153 		}
2154 		if ((so->so_state & SS_NBIO) ||
2155 		    (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2156 			SOCKBUF_UNLOCK(&so->so_rcv);
2157 			error = EWOULDBLOCK;
2158 			goto release;
2159 		}
2160 		SBLASTRECORDCHK(&so->so_rcv);
2161 		SBLASTMBUFCHK(&so->so_rcv);
2162 		error = sbwait(so, SO_RCV);
2163 		SOCKBUF_UNLOCK(&so->so_rcv);
2164 		if (error)
2165 			goto release;
2166 		goto restart;
2167 	}
2168 dontblock:
2169 	/*
2170 	 * From this point onward, we maintain 'nextrecord' as a cache of the
2171 	 * pointer to the next record in the socket buffer.  We must keep the
2172 	 * various socket buffer pointers and local stack versions of the
2173 	 * pointers in sync, pushing out modifications before dropping the
2174 	 * socket buffer mutex, and re-reading them when picking it up.
2175 	 *
2176 	 * Otherwise, we will race with the network stack appending new data
2177 	 * or records onto the socket buffer by using inconsistent/stale
2178 	 * versions of the field, possibly resulting in socket buffer
2179 	 * corruption.
2180 	 *
2181 	 * By holding the high-level sblock(), we prevent simultaneous
2182 	 * readers from pulling off the front of the socket buffer.
2183 	 */
2184 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2185 	if (uio->uio_td)
2186 		uio->uio_td->td_ru.ru_msgrcv++;
2187 	KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
2188 	SBLASTRECORDCHK(&so->so_rcv);
2189 	SBLASTMBUFCHK(&so->so_rcv);
2190 	nextrecord = m->m_nextpkt;
2191 	if (pr->pr_flags & PR_ADDR) {
2192 		KASSERT(m->m_type == MT_SONAME,
2193 		    ("m->m_type == %d", m->m_type));
2194 		orig_resid = 0;
2195 		if (psa != NULL)
2196 			*psa = sodupsockaddr(mtod(m, struct sockaddr *),
2197 			    M_NOWAIT);
2198 		if (flags & MSG_PEEK) {
2199 			m = m->m_next;
2200 		} else {
2201 			sbfree(&so->so_rcv, m);
2202 			so->so_rcv.sb_mb = m_free(m);
2203 			m = so->so_rcv.sb_mb;
2204 			sockbuf_pushsync(&so->so_rcv, nextrecord);
2205 		}
2206 	}
2207 
2208 	/*
2209 	 * Process one or more MT_CONTROL mbufs present before any data mbufs
2210 	 * in the first mbuf chain on the socket buffer.  If MSG_PEEK, we
2211 	 * just copy the data; if !MSG_PEEK, we call into the protocol to
2212 	 * perform externalization (or freeing if controlp == NULL).
2213 	 */
2214 	if (m != NULL && m->m_type == MT_CONTROL) {
2215 		struct mbuf *cm = NULL, *cmn;
2216 		struct mbuf **cme = &cm;
2217 #ifdef KERN_TLS
2218 		struct cmsghdr *cmsg;
2219 		struct tls_get_record tgr;
2220 
2221 		/*
2222 		 * For MSG_TLSAPPDATA, check for an alert record.
2223 		 * If found, return ENXIO without removing
2224 		 * it from the receive queue.  This allows a subsequent
2225 		 * call without MSG_TLSAPPDATA to receive it.
2226 		 * Note that, for TLS, there should only be a single
2227 		 * control mbuf with the TLS_GET_RECORD message in it.
2228 		 */
2229 		if (flags & MSG_TLSAPPDATA) {
2230 			cmsg = mtod(m, struct cmsghdr *);
2231 			if (cmsg->cmsg_type == TLS_GET_RECORD &&
2232 			    cmsg->cmsg_len == CMSG_LEN(sizeof(tgr))) {
2233 				memcpy(&tgr, CMSG_DATA(cmsg), sizeof(tgr));
2234 				if (__predict_false(tgr.tls_type ==
2235 				    TLS_RLTYPE_ALERT)) {
2236 					SOCKBUF_UNLOCK(&so->so_rcv);
2237 					error = ENXIO;
2238 					goto release;
2239 				}
2240 			}
2241 		}
2242 #endif
2243 
2244 		do {
2245 			if (flags & MSG_PEEK) {
2246 				if (controlp != NULL) {
2247 					*controlp = m_copym(m, 0, m->m_len,
2248 					    M_NOWAIT);
2249 					controlp = &(*controlp)->m_next;
2250 				}
2251 				m = m->m_next;
2252 			} else {
2253 				sbfree(&so->so_rcv, m);
2254 				so->so_rcv.sb_mb = m->m_next;
2255 				m->m_next = NULL;
2256 				*cme = m;
2257 				cme = &(*cme)->m_next;
2258 				m = so->so_rcv.sb_mb;
2259 			}
2260 		} while (m != NULL && m->m_type == MT_CONTROL);
2261 		if ((flags & MSG_PEEK) == 0)
2262 			sockbuf_pushsync(&so->so_rcv, nextrecord);
2263 		while (cm != NULL) {
2264 			cmn = cm->m_next;
2265 			cm->m_next = NULL;
2266 			if (pr->pr_domain->dom_externalize != NULL) {
2267 				SOCKBUF_UNLOCK(&so->so_rcv);
2268 				VNET_SO_ASSERT(so);
2269 				error = (*pr->pr_domain->dom_externalize)
2270 				    (cm, controlp, flags);
2271 				SOCKBUF_LOCK(&so->so_rcv);
2272 			} else if (controlp != NULL)
2273 				*controlp = cm;
2274 			else
2275 				m_freem(cm);
2276 			if (controlp != NULL) {
2277 				while (*controlp != NULL)
2278 					controlp = &(*controlp)->m_next;
2279 			}
2280 			cm = cmn;
2281 		}
2282 		if (m != NULL)
2283 			nextrecord = so->so_rcv.sb_mb->m_nextpkt;
2284 		else
2285 			nextrecord = so->so_rcv.sb_mb;
2286 		orig_resid = 0;
2287 	}
2288 	if (m != NULL) {
2289 		if ((flags & MSG_PEEK) == 0) {
2290 			KASSERT(m->m_nextpkt == nextrecord,
2291 			    ("soreceive: post-control, nextrecord !sync"));
2292 			if (nextrecord == NULL) {
2293 				KASSERT(so->so_rcv.sb_mb == m,
2294 				    ("soreceive: post-control, sb_mb!=m"));
2295 				KASSERT(so->so_rcv.sb_lastrecord == m,
2296 				    ("soreceive: post-control, lastrecord!=m"));
2297 			}
2298 		}
2299 		type = m->m_type;
2300 		if (type == MT_OOBDATA)
2301 			flags |= MSG_OOB;
2302 	} else {
2303 		if ((flags & MSG_PEEK) == 0) {
2304 			KASSERT(so->so_rcv.sb_mb == nextrecord,
2305 			    ("soreceive: sb_mb != nextrecord"));
2306 			if (so->so_rcv.sb_mb == NULL) {
2307 				KASSERT(so->so_rcv.sb_lastrecord == NULL,
2308 				    ("soreceive: sb_lastercord != NULL"));
2309 			}
2310 		}
2311 	}
2312 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2313 	SBLASTRECORDCHK(&so->so_rcv);
2314 	SBLASTMBUFCHK(&so->so_rcv);
2315 
2316 	/*
2317 	 * Now continue to read any data mbufs off of the head of the socket
2318 	 * buffer until the read request is satisfied.  Note that 'type' is
2319 	 * used to store the type of any mbuf reads that have happened so far
2320 	 * such that soreceive() can stop reading if the type changes, which
2321 	 * causes soreceive() to return only one of regular data and inline
2322 	 * out-of-band data in a single socket receive operation.
2323 	 */
2324 	moff = 0;
2325 	offset = 0;
2326 	while (m != NULL && !(m->m_flags & M_NOTAVAIL) && uio->uio_resid > 0
2327 	    && error == 0) {
2328 		/*
2329 		 * If the type of mbuf has changed since the last mbuf
2330 		 * examined ('type'), end the receive operation.
2331 		 */
2332 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2333 		if (m->m_type == MT_OOBDATA || m->m_type == MT_CONTROL) {
2334 			if (type != m->m_type)
2335 				break;
2336 		} else if (type == MT_OOBDATA)
2337 			break;
2338 		else
2339 		    KASSERT(m->m_type == MT_DATA,
2340 			("m->m_type == %d", m->m_type));
2341 		so->so_rcv.sb_state &= ~SBS_RCVATMARK;
2342 		len = uio->uio_resid;
2343 		if (so->so_oobmark && len > so->so_oobmark - offset)
2344 			len = so->so_oobmark - offset;
2345 		if (len > m->m_len - moff)
2346 			len = m->m_len - moff;
2347 		/*
2348 		 * If mp is set, just pass back the mbufs.  Otherwise copy
2349 		 * them out via the uio, then free.  Sockbuf must be
2350 		 * consistent here (points to current mbuf, it points to next
2351 		 * record) when we drop priority; we must note any additions
2352 		 * to the sockbuf when we block interrupts again.
2353 		 */
2354 		if (mp == NULL) {
2355 			SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2356 			SBLASTRECORDCHK(&so->so_rcv);
2357 			SBLASTMBUFCHK(&so->so_rcv);
2358 			SOCKBUF_UNLOCK(&so->so_rcv);
2359 			if ((m->m_flags & M_EXTPG) != 0)
2360 				error = m_unmapped_uiomove(m, moff, uio,
2361 				    (int)len);
2362 			else
2363 				error = uiomove(mtod(m, char *) + moff,
2364 				    (int)len, uio);
2365 			SOCKBUF_LOCK(&so->so_rcv);
2366 			if (error) {
2367 				/*
2368 				 * The MT_SONAME mbuf has already been removed
2369 				 * from the record, so it is necessary to
2370 				 * remove the data mbufs, if any, to preserve
2371 				 * the invariant in the case of PR_ADDR that
2372 				 * requires MT_SONAME mbufs at the head of
2373 				 * each record.
2374 				 */
2375 				if (pr->pr_flags & PR_ATOMIC &&
2376 				    ((flags & MSG_PEEK) == 0))
2377 					(void)sbdroprecord_locked(&so->so_rcv);
2378 				SOCKBUF_UNLOCK(&so->so_rcv);
2379 				goto release;
2380 			}
2381 		} else
2382 			uio->uio_resid -= len;
2383 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2384 		if (len == m->m_len - moff) {
2385 			if (m->m_flags & M_EOR)
2386 				flags |= MSG_EOR;
2387 			if (flags & MSG_PEEK) {
2388 				m = m->m_next;
2389 				moff = 0;
2390 			} else {
2391 				nextrecord = m->m_nextpkt;
2392 				sbfree(&so->so_rcv, m);
2393 				if (mp != NULL) {
2394 					m->m_nextpkt = NULL;
2395 					*mp = m;
2396 					mp = &m->m_next;
2397 					so->so_rcv.sb_mb = m = m->m_next;
2398 					*mp = NULL;
2399 				} else {
2400 					so->so_rcv.sb_mb = m_free(m);
2401 					m = so->so_rcv.sb_mb;
2402 				}
2403 				sockbuf_pushsync(&so->so_rcv, nextrecord);
2404 				SBLASTRECORDCHK(&so->so_rcv);
2405 				SBLASTMBUFCHK(&so->so_rcv);
2406 			}
2407 		} else {
2408 			if (flags & MSG_PEEK)
2409 				moff += len;
2410 			else {
2411 				if (mp != NULL) {
2412 					if (flags & MSG_DONTWAIT) {
2413 						*mp = m_copym(m, 0, len,
2414 						    M_NOWAIT);
2415 						if (*mp == NULL) {
2416 							/*
2417 							 * m_copym() couldn't
2418 							 * allocate an mbuf.
2419 							 * Adjust uio_resid back
2420 							 * (it was adjusted
2421 							 * down by len bytes,
2422 							 * which we didn't end
2423 							 * up "copying" over).
2424 							 */
2425 							uio->uio_resid += len;
2426 							break;
2427 						}
2428 					} else {
2429 						SOCKBUF_UNLOCK(&so->so_rcv);
2430 						*mp = m_copym(m, 0, len,
2431 						    M_WAITOK);
2432 						SOCKBUF_LOCK(&so->so_rcv);
2433 					}
2434 				}
2435 				sbcut_locked(&so->so_rcv, len);
2436 			}
2437 		}
2438 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2439 		if (so->so_oobmark) {
2440 			if ((flags & MSG_PEEK) == 0) {
2441 				so->so_oobmark -= len;
2442 				if (so->so_oobmark == 0) {
2443 					so->so_rcv.sb_state |= SBS_RCVATMARK;
2444 					break;
2445 				}
2446 			} else {
2447 				offset += len;
2448 				if (offset == so->so_oobmark)
2449 					break;
2450 			}
2451 		}
2452 		if (flags & MSG_EOR)
2453 			break;
2454 		/*
2455 		 * If the MSG_WAITALL flag is set (for non-atomic socket), we
2456 		 * must not quit until "uio->uio_resid == 0" or an error
2457 		 * termination.  If a signal/timeout occurs, return with a
2458 		 * short count but without error.  Keep sockbuf locked
2459 		 * against other readers.
2460 		 */
2461 		while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
2462 		    !sosendallatonce(so) && nextrecord == NULL) {
2463 			SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2464 			if (so->so_error || so->so_rerror ||
2465 			    so->so_rcv.sb_state & SBS_CANTRCVMORE)
2466 				break;
2467 			/*
2468 			 * Notify the protocol that some data has been
2469 			 * drained before blocking.
2470 			 */
2471 			if (pr->pr_flags & PR_WANTRCVD) {
2472 				SOCKBUF_UNLOCK(&so->so_rcv);
2473 				VNET_SO_ASSERT(so);
2474 				pr->pr_rcvd(so, flags);
2475 				SOCKBUF_LOCK(&so->so_rcv);
2476 				if (__predict_false(so->so_rcv.sb_mb == NULL &&
2477 				    (so->so_error || so->so_rerror ||
2478 				    so->so_rcv.sb_state & SBS_CANTRCVMORE)))
2479 					break;
2480 			}
2481 			SBLASTRECORDCHK(&so->so_rcv);
2482 			SBLASTMBUFCHK(&so->so_rcv);
2483 			/*
2484 			 * We could receive some data while was notifying
2485 			 * the protocol. Skip blocking in this case.
2486 			 */
2487 			if (so->so_rcv.sb_mb == NULL) {
2488 				error = sbwait(so, SO_RCV);
2489 				if (error) {
2490 					SOCKBUF_UNLOCK(&so->so_rcv);
2491 					goto release;
2492 				}
2493 			}
2494 			m = so->so_rcv.sb_mb;
2495 			if (m != NULL)
2496 				nextrecord = m->m_nextpkt;
2497 		}
2498 	}
2499 
2500 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2501 	if (m != NULL && pr->pr_flags & PR_ATOMIC) {
2502 		if (report_real_len)
2503 			uio->uio_resid -= m_length(m, NULL) - moff;
2504 		flags |= MSG_TRUNC;
2505 		if ((flags & MSG_PEEK) == 0)
2506 			(void) sbdroprecord_locked(&so->so_rcv);
2507 	}
2508 	if ((flags & MSG_PEEK) == 0) {
2509 		if (m == NULL) {
2510 			/*
2511 			 * First part is an inline SB_EMPTY_FIXUP().  Second
2512 			 * part makes sure sb_lastrecord is up-to-date if
2513 			 * there is still data in the socket buffer.
2514 			 */
2515 			so->so_rcv.sb_mb = nextrecord;
2516 			if (so->so_rcv.sb_mb == NULL) {
2517 				so->so_rcv.sb_mbtail = NULL;
2518 				so->so_rcv.sb_lastrecord = NULL;
2519 			} else if (nextrecord->m_nextpkt == NULL)
2520 				so->so_rcv.sb_lastrecord = nextrecord;
2521 		}
2522 		SBLASTRECORDCHK(&so->so_rcv);
2523 		SBLASTMBUFCHK(&so->so_rcv);
2524 		/*
2525 		 * If soreceive() is being done from the socket callback,
2526 		 * then don't need to generate ACK to peer to update window,
2527 		 * since ACK will be generated on return to TCP.
2528 		 */
2529 		if (!(flags & MSG_SOCALLBCK) &&
2530 		    (pr->pr_flags & PR_WANTRCVD)) {
2531 			SOCKBUF_UNLOCK(&so->so_rcv);
2532 			VNET_SO_ASSERT(so);
2533 			pr->pr_rcvd(so, flags);
2534 			SOCKBUF_LOCK(&so->so_rcv);
2535 		}
2536 	}
2537 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2538 	if (orig_resid == uio->uio_resid && orig_resid &&
2539 	    (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
2540 		SOCKBUF_UNLOCK(&so->so_rcv);
2541 		goto restart;
2542 	}
2543 	SOCKBUF_UNLOCK(&so->so_rcv);
2544 
2545 	if (flagsp != NULL)
2546 		*flagsp |= flags;
2547 release:
2548 	SOCK_IO_RECV_UNLOCK(so);
2549 	return (error);
2550 }
2551 
2552 /*
2553  * Optimized version of soreceive() for stream (TCP) sockets.
2554  */
2555 int
2556 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
2557     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2558 {
2559 	int len = 0, error = 0, flags, oresid;
2560 	struct sockbuf *sb;
2561 	struct mbuf *m, *n = NULL;
2562 
2563 	/* We only do stream sockets. */
2564 	if (so->so_type != SOCK_STREAM)
2565 		return (EINVAL);
2566 	if (psa != NULL)
2567 		*psa = NULL;
2568 	if (flagsp != NULL)
2569 		flags = *flagsp &~ MSG_EOR;
2570 	else
2571 		flags = 0;
2572 	if (controlp != NULL)
2573 		*controlp = NULL;
2574 	if (flags & MSG_OOB)
2575 		return (soreceive_rcvoob(so, uio, flags));
2576 	if (mp0 != NULL)
2577 		*mp0 = NULL;
2578 
2579 	sb = &so->so_rcv;
2580 
2581 #ifdef KERN_TLS
2582 	/*
2583 	 * KTLS store TLS records as records with a control message to
2584 	 * describe the framing.
2585 	 *
2586 	 * We check once here before acquiring locks to optimize the
2587 	 * common case.
2588 	 */
2589 	if (sb->sb_tls_info != NULL)
2590 		return (soreceive_generic(so, psa, uio, mp0, controlp,
2591 		    flagsp));
2592 #endif
2593 
2594 	/* Prevent other readers from entering the socket. */
2595 	error = SOCK_IO_RECV_LOCK(so, SBLOCKWAIT(flags));
2596 	if (error)
2597 		return (error);
2598 	SOCKBUF_LOCK(sb);
2599 
2600 #ifdef KERN_TLS
2601 	if (sb->sb_tls_info != NULL) {
2602 		SOCKBUF_UNLOCK(sb);
2603 		SOCK_IO_RECV_UNLOCK(so);
2604 		return (soreceive_generic(so, psa, uio, mp0, controlp,
2605 		    flagsp));
2606 	}
2607 #endif
2608 
2609 	/* Easy one, no space to copyout anything. */
2610 	if (uio->uio_resid == 0) {
2611 		error = EINVAL;
2612 		goto out;
2613 	}
2614 	oresid = uio->uio_resid;
2615 
2616 	/* We will never ever get anything unless we are or were connected. */
2617 	if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
2618 		error = ENOTCONN;
2619 		goto out;
2620 	}
2621 
2622 restart:
2623 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2624 
2625 	/* Abort if socket has reported problems. */
2626 	if (so->so_error) {
2627 		if (sbavail(sb) > 0)
2628 			goto deliver;
2629 		if (oresid > uio->uio_resid)
2630 			goto out;
2631 		error = so->so_error;
2632 		if (!(flags & MSG_PEEK))
2633 			so->so_error = 0;
2634 		goto out;
2635 	}
2636 
2637 	/* Door is closed.  Deliver what is left, if any. */
2638 	if (sb->sb_state & SBS_CANTRCVMORE) {
2639 		if (sbavail(sb) > 0)
2640 			goto deliver;
2641 		else
2642 			goto out;
2643 	}
2644 
2645 	/* Socket buffer is empty and we shall not block. */
2646 	if (sbavail(sb) == 0 &&
2647 	    ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
2648 		error = EAGAIN;
2649 		goto out;
2650 	}
2651 
2652 	/* Socket buffer got some data that we shall deliver now. */
2653 	if (sbavail(sb) > 0 && !(flags & MSG_WAITALL) &&
2654 	    ((so->so_state & SS_NBIO) ||
2655 	     (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
2656 	     sbavail(sb) >= sb->sb_lowat ||
2657 	     sbavail(sb) >= uio->uio_resid ||
2658 	     sbavail(sb) >= sb->sb_hiwat) ) {
2659 		goto deliver;
2660 	}
2661 
2662 	/* On MSG_WAITALL we must wait until all data or error arrives. */
2663 	if ((flags & MSG_WAITALL) &&
2664 	    (sbavail(sb) >= uio->uio_resid || sbavail(sb) >= sb->sb_hiwat))
2665 		goto deliver;
2666 
2667 	/*
2668 	 * Wait and block until (more) data comes in.
2669 	 * NB: Drops the sockbuf lock during wait.
2670 	 */
2671 	error = sbwait(so, SO_RCV);
2672 	if (error)
2673 		goto out;
2674 	goto restart;
2675 
2676 deliver:
2677 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2678 	KASSERT(sbavail(sb) > 0, ("%s: sockbuf empty", __func__));
2679 	KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
2680 
2681 	/* Statistics. */
2682 	if (uio->uio_td)
2683 		uio->uio_td->td_ru.ru_msgrcv++;
2684 
2685 	/* Fill uio until full or current end of socket buffer is reached. */
2686 	len = min(uio->uio_resid, sbavail(sb));
2687 	if (mp0 != NULL) {
2688 		/* Dequeue as many mbufs as possible. */
2689 		if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
2690 			if (*mp0 == NULL)
2691 				*mp0 = sb->sb_mb;
2692 			else
2693 				m_cat(*mp0, sb->sb_mb);
2694 			for (m = sb->sb_mb;
2695 			     m != NULL && m->m_len <= len;
2696 			     m = m->m_next) {
2697 				KASSERT(!(m->m_flags & M_NOTAVAIL),
2698 				    ("%s: m %p not available", __func__, m));
2699 				len -= m->m_len;
2700 				uio->uio_resid -= m->m_len;
2701 				sbfree(sb, m);
2702 				n = m;
2703 			}
2704 			n->m_next = NULL;
2705 			sb->sb_mb = m;
2706 			sb->sb_lastrecord = sb->sb_mb;
2707 			if (sb->sb_mb == NULL)
2708 				SB_EMPTY_FIXUP(sb);
2709 		}
2710 		/* Copy the remainder. */
2711 		if (len > 0) {
2712 			KASSERT(sb->sb_mb != NULL,
2713 			    ("%s: len > 0 && sb->sb_mb empty", __func__));
2714 
2715 			m = m_copym(sb->sb_mb, 0, len, M_NOWAIT);
2716 			if (m == NULL)
2717 				len = 0;	/* Don't flush data from sockbuf. */
2718 			else
2719 				uio->uio_resid -= len;
2720 			if (*mp0 != NULL)
2721 				m_cat(*mp0, m);
2722 			else
2723 				*mp0 = m;
2724 			if (*mp0 == NULL) {
2725 				error = ENOBUFS;
2726 				goto out;
2727 			}
2728 		}
2729 	} else {
2730 		/* NB: Must unlock socket buffer as uiomove may sleep. */
2731 		SOCKBUF_UNLOCK(sb);
2732 		error = m_mbuftouio(uio, sb->sb_mb, len);
2733 		SOCKBUF_LOCK(sb);
2734 		if (error)
2735 			goto out;
2736 	}
2737 	SBLASTRECORDCHK(sb);
2738 	SBLASTMBUFCHK(sb);
2739 
2740 	/*
2741 	 * Remove the delivered data from the socket buffer unless we
2742 	 * were only peeking.
2743 	 */
2744 	if (!(flags & MSG_PEEK)) {
2745 		if (len > 0)
2746 			sbdrop_locked(sb, len);
2747 
2748 		/* Notify protocol that we drained some data. */
2749 		if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
2750 		    (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
2751 		     !(flags & MSG_SOCALLBCK))) {
2752 			SOCKBUF_UNLOCK(sb);
2753 			VNET_SO_ASSERT(so);
2754 			so->so_proto->pr_rcvd(so, flags);
2755 			SOCKBUF_LOCK(sb);
2756 		}
2757 	}
2758 
2759 	/*
2760 	 * For MSG_WAITALL we may have to loop again and wait for
2761 	 * more data to come in.
2762 	 */
2763 	if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
2764 		goto restart;
2765 out:
2766 	SBLASTRECORDCHK(sb);
2767 	SBLASTMBUFCHK(sb);
2768 	SOCKBUF_UNLOCK(sb);
2769 	SOCK_IO_RECV_UNLOCK(so);
2770 	return (error);
2771 }
2772 
2773 /*
2774  * Optimized version of soreceive() for simple datagram cases from userspace.
2775  * Unlike in the stream case, we're able to drop a datagram if copyout()
2776  * fails, and because we handle datagrams atomically, we don't need to use a
2777  * sleep lock to prevent I/O interlacing.
2778  */
2779 int
2780 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
2781     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2782 {
2783 	struct mbuf *m, *m2;
2784 	int flags, error;
2785 	ssize_t len;
2786 	struct protosw *pr = so->so_proto;
2787 	struct mbuf *nextrecord;
2788 
2789 	if (psa != NULL)
2790 		*psa = NULL;
2791 	if (controlp != NULL)
2792 		*controlp = NULL;
2793 	if (flagsp != NULL)
2794 		flags = *flagsp &~ MSG_EOR;
2795 	else
2796 		flags = 0;
2797 
2798 	/*
2799 	 * For any complicated cases, fall back to the full
2800 	 * soreceive_generic().
2801 	 */
2802 	if (mp0 != NULL || (flags & (MSG_PEEK | MSG_OOB | MSG_TRUNC)))
2803 		return (soreceive_generic(so, psa, uio, mp0, controlp,
2804 		    flagsp));
2805 
2806 	/*
2807 	 * Enforce restrictions on use.
2808 	 */
2809 	KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
2810 	    ("soreceive_dgram: wantrcvd"));
2811 	KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
2812 	KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
2813 	    ("soreceive_dgram: SBS_RCVATMARK"));
2814 	KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
2815 	    ("soreceive_dgram: P_CONNREQUIRED"));
2816 
2817 	/*
2818 	 * Loop blocking while waiting for a datagram.
2819 	 */
2820 	SOCKBUF_LOCK(&so->so_rcv);
2821 	while ((m = so->so_rcv.sb_mb) == NULL) {
2822 		KASSERT(sbavail(&so->so_rcv) == 0,
2823 		    ("soreceive_dgram: sb_mb NULL but sbavail %u",
2824 		    sbavail(&so->so_rcv)));
2825 		if (so->so_error) {
2826 			error = so->so_error;
2827 			so->so_error = 0;
2828 			SOCKBUF_UNLOCK(&so->so_rcv);
2829 			return (error);
2830 		}
2831 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
2832 		    uio->uio_resid == 0) {
2833 			SOCKBUF_UNLOCK(&so->so_rcv);
2834 			return (0);
2835 		}
2836 		if ((so->so_state & SS_NBIO) ||
2837 		    (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2838 			SOCKBUF_UNLOCK(&so->so_rcv);
2839 			return (EWOULDBLOCK);
2840 		}
2841 		SBLASTRECORDCHK(&so->so_rcv);
2842 		SBLASTMBUFCHK(&so->so_rcv);
2843 		error = sbwait(so, SO_RCV);
2844 		if (error) {
2845 			SOCKBUF_UNLOCK(&so->so_rcv);
2846 			return (error);
2847 		}
2848 	}
2849 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2850 
2851 	if (uio->uio_td)
2852 		uio->uio_td->td_ru.ru_msgrcv++;
2853 	SBLASTRECORDCHK(&so->so_rcv);
2854 	SBLASTMBUFCHK(&so->so_rcv);
2855 	nextrecord = m->m_nextpkt;
2856 	if (nextrecord == NULL) {
2857 		KASSERT(so->so_rcv.sb_lastrecord == m,
2858 		    ("soreceive_dgram: lastrecord != m"));
2859 	}
2860 
2861 	KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
2862 	    ("soreceive_dgram: m_nextpkt != nextrecord"));
2863 
2864 	/*
2865 	 * Pull 'm' and its chain off the front of the packet queue.
2866 	 */
2867 	so->so_rcv.sb_mb = NULL;
2868 	sockbuf_pushsync(&so->so_rcv, nextrecord);
2869 
2870 	/*
2871 	 * Walk 'm's chain and free that many bytes from the socket buffer.
2872 	 */
2873 	for (m2 = m; m2 != NULL; m2 = m2->m_next)
2874 		sbfree(&so->so_rcv, m2);
2875 
2876 	/*
2877 	 * Do a few last checks before we let go of the lock.
2878 	 */
2879 	SBLASTRECORDCHK(&so->so_rcv);
2880 	SBLASTMBUFCHK(&so->so_rcv);
2881 	SOCKBUF_UNLOCK(&so->so_rcv);
2882 
2883 	if (pr->pr_flags & PR_ADDR) {
2884 		KASSERT(m->m_type == MT_SONAME,
2885 		    ("m->m_type == %d", m->m_type));
2886 		if (psa != NULL)
2887 			*psa = sodupsockaddr(mtod(m, struct sockaddr *),
2888 			    M_WAITOK);
2889 		m = m_free(m);
2890 	}
2891 	KASSERT(m, ("%s: no data or control after soname", __func__));
2892 
2893 	/*
2894 	 * Packet to copyout() is now in 'm' and it is disconnected from the
2895 	 * queue.
2896 	 *
2897 	 * Process one or more MT_CONTROL mbufs present before any data mbufs
2898 	 * in the first mbuf chain on the socket buffer.  We call into the
2899 	 * protocol to perform externalization (or freeing if controlp ==
2900 	 * NULL). In some cases there can be only MT_CONTROL mbufs without
2901 	 * MT_DATA mbufs.
2902 	 */
2903 	if (m->m_type == MT_CONTROL) {
2904 		struct mbuf *cm = NULL, *cmn;
2905 		struct mbuf **cme = &cm;
2906 
2907 		do {
2908 			m2 = m->m_next;
2909 			m->m_next = NULL;
2910 			*cme = m;
2911 			cme = &(*cme)->m_next;
2912 			m = m2;
2913 		} while (m != NULL && m->m_type == MT_CONTROL);
2914 		while (cm != NULL) {
2915 			cmn = cm->m_next;
2916 			cm->m_next = NULL;
2917 			if (pr->pr_domain->dom_externalize != NULL) {
2918 				error = (*pr->pr_domain->dom_externalize)
2919 				    (cm, controlp, flags);
2920 			} else if (controlp != NULL)
2921 				*controlp = cm;
2922 			else
2923 				m_freem(cm);
2924 			if (controlp != NULL) {
2925 				while (*controlp != NULL)
2926 					controlp = &(*controlp)->m_next;
2927 			}
2928 			cm = cmn;
2929 		}
2930 	}
2931 	KASSERT(m == NULL || m->m_type == MT_DATA,
2932 	    ("soreceive_dgram: !data"));
2933 	while (m != NULL && uio->uio_resid > 0) {
2934 		len = uio->uio_resid;
2935 		if (len > m->m_len)
2936 			len = m->m_len;
2937 		error = uiomove(mtod(m, char *), (int)len, uio);
2938 		if (error) {
2939 			m_freem(m);
2940 			return (error);
2941 		}
2942 		if (len == m->m_len)
2943 			m = m_free(m);
2944 		else {
2945 			m->m_data += len;
2946 			m->m_len -= len;
2947 		}
2948 	}
2949 	if (m != NULL) {
2950 		flags |= MSG_TRUNC;
2951 		m_freem(m);
2952 	}
2953 	if (flagsp != NULL)
2954 		*flagsp |= flags;
2955 	return (0);
2956 }
2957 
2958 int
2959 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2960     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2961 {
2962 	int error;
2963 
2964 	CURVNET_SET(so->so_vnet);
2965 	error = so->so_proto->pr_soreceive(so, psa, uio, mp0, controlp, flagsp);
2966 	CURVNET_RESTORE();
2967 	return (error);
2968 }
2969 
2970 int
2971 soshutdown(struct socket *so, enum shutdown_how how)
2972 {
2973 	int error;
2974 
2975 	CURVNET_SET(so->so_vnet);
2976 	error = so->so_proto->pr_shutdown(so, how);
2977 	CURVNET_RESTORE();
2978 
2979 	return (error);
2980 }
2981 
2982 /*
2983  * Used by several pr_shutdown implementations that use generic socket buffers.
2984  */
2985 void
2986 sorflush(struct socket *so)
2987 {
2988 	int error;
2989 
2990 	VNET_SO_ASSERT(so);
2991 
2992 	/*
2993 	 * Dislodge threads currently blocked in receive and wait to acquire
2994 	 * a lock against other simultaneous readers before clearing the
2995 	 * socket buffer.  Don't let our acquire be interrupted by a signal
2996 	 * despite any existing socket disposition on interruptable waiting.
2997 	 *
2998 	 * The SOCK_IO_RECV_LOCK() is important here as there some pr_soreceive
2999 	 * methods that read the top of the socket buffer without acquisition
3000 	 * of the socket buffer mutex, assuming that top of the buffer
3001 	 * exclusively belongs to the read(2) syscall.  This is handy when
3002 	 * performing MSG_PEEK.
3003 	 */
3004 	socantrcvmore(so);
3005 
3006 	error = SOCK_IO_RECV_LOCK(so, SBL_WAIT | SBL_NOINTR);
3007 	if (error != 0) {
3008 		KASSERT(SOLISTENING(so),
3009 		    ("%s: soiolock(%p) failed", __func__, so));
3010 		return;
3011 	}
3012 
3013 	sbrelease(so, SO_RCV);
3014 	SOCK_IO_RECV_UNLOCK(so);
3015 
3016 }
3017 
3018 /*
3019  * Wrapper for Socket established helper hook.
3020  * Parameters: socket, context of the hook point, hook id.
3021  */
3022 static int inline
3023 hhook_run_socket(struct socket *so, void *hctx, int32_t h_id)
3024 {
3025 	struct socket_hhook_data hhook_data = {
3026 		.so = so,
3027 		.hctx = hctx,
3028 		.m = NULL,
3029 		.status = 0
3030 	};
3031 
3032 	CURVNET_SET(so->so_vnet);
3033 	HHOOKS_RUN_IF(V_socket_hhh[h_id], &hhook_data, &so->osd);
3034 	CURVNET_RESTORE();
3035 
3036 	/* Ugly but needed, since hhooks return void for now */
3037 	return (hhook_data.status);
3038 }
3039 
3040 /*
3041  * Perhaps this routine, and sooptcopyout(), below, ought to come in an
3042  * additional variant to handle the case where the option value needs to be
3043  * some kind of integer, but not a specific size.  In addition to their use
3044  * here, these functions are also called by the protocol-level pr_ctloutput()
3045  * routines.
3046  */
3047 int
3048 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
3049 {
3050 	size_t	valsize;
3051 
3052 	/*
3053 	 * If the user gives us more than we wanted, we ignore it, but if we
3054 	 * don't get the minimum length the caller wants, we return EINVAL.
3055 	 * On success, sopt->sopt_valsize is set to however much we actually
3056 	 * retrieved.
3057 	 */
3058 	if ((valsize = sopt->sopt_valsize) < minlen)
3059 		return EINVAL;
3060 	if (valsize > len)
3061 		sopt->sopt_valsize = valsize = len;
3062 
3063 	if (sopt->sopt_td != NULL)
3064 		return (copyin(sopt->sopt_val, buf, valsize));
3065 
3066 	bcopy(sopt->sopt_val, buf, valsize);
3067 	return (0);
3068 }
3069 
3070 /*
3071  * Kernel version of setsockopt(2).
3072  *
3073  * XXX: optlen is size_t, not socklen_t
3074  */
3075 int
3076 so_setsockopt(struct socket *so, int level, int optname, void *optval,
3077     size_t optlen)
3078 {
3079 	struct sockopt sopt;
3080 
3081 	sopt.sopt_level = level;
3082 	sopt.sopt_name = optname;
3083 	sopt.sopt_dir = SOPT_SET;
3084 	sopt.sopt_val = optval;
3085 	sopt.sopt_valsize = optlen;
3086 	sopt.sopt_td = NULL;
3087 	return (sosetopt(so, &sopt));
3088 }
3089 
3090 int
3091 sosetopt(struct socket *so, struct sockopt *sopt)
3092 {
3093 	int	error, optval;
3094 	struct	linger l;
3095 	struct	timeval tv;
3096 	sbintime_t val, *valp;
3097 	uint32_t val32;
3098 #ifdef MAC
3099 	struct mac extmac;
3100 #endif
3101 
3102 	CURVNET_SET(so->so_vnet);
3103 	error = 0;
3104 	if (sopt->sopt_level != SOL_SOCKET) {
3105 		if (so->so_proto->pr_ctloutput != NULL)
3106 			error = (*so->so_proto->pr_ctloutput)(so, sopt);
3107 		else
3108 			error = ENOPROTOOPT;
3109 	} else {
3110 		switch (sopt->sopt_name) {
3111 		case SO_ACCEPTFILTER:
3112 			error = accept_filt_setopt(so, sopt);
3113 			if (error)
3114 				goto bad;
3115 			break;
3116 
3117 		case SO_LINGER:
3118 			error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
3119 			if (error)
3120 				goto bad;
3121 			if (l.l_linger < 0 ||
3122 			    l.l_linger > USHRT_MAX ||
3123 			    l.l_linger > (INT_MAX / hz)) {
3124 				error = EDOM;
3125 				goto bad;
3126 			}
3127 			SOCK_LOCK(so);
3128 			so->so_linger = l.l_linger;
3129 			if (l.l_onoff)
3130 				so->so_options |= SO_LINGER;
3131 			else
3132 				so->so_options &= ~SO_LINGER;
3133 			SOCK_UNLOCK(so);
3134 			break;
3135 
3136 		case SO_DEBUG:
3137 		case SO_KEEPALIVE:
3138 		case SO_DONTROUTE:
3139 		case SO_USELOOPBACK:
3140 		case SO_BROADCAST:
3141 		case SO_REUSEADDR:
3142 		case SO_REUSEPORT:
3143 		case SO_REUSEPORT_LB:
3144 		case SO_OOBINLINE:
3145 		case SO_TIMESTAMP:
3146 		case SO_BINTIME:
3147 		case SO_NOSIGPIPE:
3148 		case SO_NO_DDP:
3149 		case SO_NO_OFFLOAD:
3150 		case SO_RERROR:
3151 			error = sooptcopyin(sopt, &optval, sizeof optval,
3152 			    sizeof optval);
3153 			if (error)
3154 				goto bad;
3155 			SOCK_LOCK(so);
3156 			if (optval)
3157 				so->so_options |= sopt->sopt_name;
3158 			else
3159 				so->so_options &= ~sopt->sopt_name;
3160 			SOCK_UNLOCK(so);
3161 			break;
3162 
3163 		case SO_SETFIB:
3164 			error = sooptcopyin(sopt, &optval, sizeof optval,
3165 			    sizeof optval);
3166 			if (error)
3167 				goto bad;
3168 
3169 			if (optval < 0 || optval >= rt_numfibs) {
3170 				error = EINVAL;
3171 				goto bad;
3172 			}
3173 			if (((so->so_proto->pr_domain->dom_family == PF_INET) ||
3174 			   (so->so_proto->pr_domain->dom_family == PF_INET6) ||
3175 			   (so->so_proto->pr_domain->dom_family == PF_ROUTE)))
3176 				so->so_fibnum = optval;
3177 			else
3178 				so->so_fibnum = 0;
3179 			break;
3180 
3181 		case SO_USER_COOKIE:
3182 			error = sooptcopyin(sopt, &val32, sizeof val32,
3183 			    sizeof val32);
3184 			if (error)
3185 				goto bad;
3186 			so->so_user_cookie = val32;
3187 			break;
3188 
3189 		case SO_SNDBUF:
3190 		case SO_RCVBUF:
3191 		case SO_SNDLOWAT:
3192 		case SO_RCVLOWAT:
3193 			error = so->so_proto->pr_setsbopt(so, sopt);
3194 			if (error)
3195 				goto bad;
3196 			break;
3197 
3198 		case SO_SNDTIMEO:
3199 		case SO_RCVTIMEO:
3200 #ifdef COMPAT_FREEBSD32
3201 			if (SV_CURPROC_FLAG(SV_ILP32)) {
3202 				struct timeval32 tv32;
3203 
3204 				error = sooptcopyin(sopt, &tv32, sizeof tv32,
3205 				    sizeof tv32);
3206 				CP(tv32, tv, tv_sec);
3207 				CP(tv32, tv, tv_usec);
3208 			} else
3209 #endif
3210 				error = sooptcopyin(sopt, &tv, sizeof tv,
3211 				    sizeof tv);
3212 			if (error)
3213 				goto bad;
3214 			if (tv.tv_sec < 0 || tv.tv_usec < 0 ||
3215 			    tv.tv_usec >= 1000000) {
3216 				error = EDOM;
3217 				goto bad;
3218 			}
3219 			if (tv.tv_sec > INT32_MAX)
3220 				val = SBT_MAX;
3221 			else
3222 				val = tvtosbt(tv);
3223 			SOCK_LOCK(so);
3224 			valp = sopt->sopt_name == SO_SNDTIMEO ?
3225 			    (SOLISTENING(so) ? &so->sol_sbsnd_timeo :
3226 			    &so->so_snd.sb_timeo) :
3227 			    (SOLISTENING(so) ? &so->sol_sbrcv_timeo :
3228 			    &so->so_rcv.sb_timeo);
3229 			*valp = val;
3230 			SOCK_UNLOCK(so);
3231 			break;
3232 
3233 		case SO_LABEL:
3234 #ifdef MAC
3235 			error = sooptcopyin(sopt, &extmac, sizeof extmac,
3236 			    sizeof extmac);
3237 			if (error)
3238 				goto bad;
3239 			error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
3240 			    so, &extmac);
3241 #else
3242 			error = EOPNOTSUPP;
3243 #endif
3244 			break;
3245 
3246 		case SO_TS_CLOCK:
3247 			error = sooptcopyin(sopt, &optval, sizeof optval,
3248 			    sizeof optval);
3249 			if (error)
3250 				goto bad;
3251 			if (optval < 0 || optval > SO_TS_CLOCK_MAX) {
3252 				error = EINVAL;
3253 				goto bad;
3254 			}
3255 			so->so_ts_clock = optval;
3256 			break;
3257 
3258 		case SO_MAX_PACING_RATE:
3259 			error = sooptcopyin(sopt, &val32, sizeof(val32),
3260 			    sizeof(val32));
3261 			if (error)
3262 				goto bad;
3263 			so->so_max_pacing_rate = val32;
3264 			break;
3265 
3266 		default:
3267 			if (V_socket_hhh[HHOOK_SOCKET_OPT]->hhh_nhooks > 0)
3268 				error = hhook_run_socket(so, sopt,
3269 				    HHOOK_SOCKET_OPT);
3270 			else
3271 				error = ENOPROTOOPT;
3272 			break;
3273 		}
3274 		if (error == 0 && so->so_proto->pr_ctloutput != NULL)
3275 			(void)(*so->so_proto->pr_ctloutput)(so, sopt);
3276 	}
3277 bad:
3278 	CURVNET_RESTORE();
3279 	return (error);
3280 }
3281 
3282 /*
3283  * Helper routine for getsockopt.
3284  */
3285 int
3286 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
3287 {
3288 	int	error;
3289 	size_t	valsize;
3290 
3291 	error = 0;
3292 
3293 	/*
3294 	 * Documented get behavior is that we always return a value, possibly
3295 	 * truncated to fit in the user's buffer.  Traditional behavior is
3296 	 * that we always tell the user precisely how much we copied, rather
3297 	 * than something useful like the total amount we had available for
3298 	 * her.  Note that this interface is not idempotent; the entire
3299 	 * answer must be generated ahead of time.
3300 	 */
3301 	valsize = min(len, sopt->sopt_valsize);
3302 	sopt->sopt_valsize = valsize;
3303 	if (sopt->sopt_val != NULL) {
3304 		if (sopt->sopt_td != NULL)
3305 			error = copyout(buf, sopt->sopt_val, valsize);
3306 		else
3307 			bcopy(buf, sopt->sopt_val, valsize);
3308 	}
3309 	return (error);
3310 }
3311 
3312 int
3313 sogetopt(struct socket *so, struct sockopt *sopt)
3314 {
3315 	int	error, optval;
3316 	struct	linger l;
3317 	struct	timeval tv;
3318 #ifdef MAC
3319 	struct mac extmac;
3320 #endif
3321 
3322 	CURVNET_SET(so->so_vnet);
3323 	error = 0;
3324 	if (sopt->sopt_level != SOL_SOCKET) {
3325 		if (so->so_proto->pr_ctloutput != NULL)
3326 			error = (*so->so_proto->pr_ctloutput)(so, sopt);
3327 		else
3328 			error = ENOPROTOOPT;
3329 		CURVNET_RESTORE();
3330 		return (error);
3331 	} else {
3332 		switch (sopt->sopt_name) {
3333 		case SO_ACCEPTFILTER:
3334 			error = accept_filt_getopt(so, sopt);
3335 			break;
3336 
3337 		case SO_LINGER:
3338 			SOCK_LOCK(so);
3339 			l.l_onoff = so->so_options & SO_LINGER;
3340 			l.l_linger = so->so_linger;
3341 			SOCK_UNLOCK(so);
3342 			error = sooptcopyout(sopt, &l, sizeof l);
3343 			break;
3344 
3345 		case SO_USELOOPBACK:
3346 		case SO_DONTROUTE:
3347 		case SO_DEBUG:
3348 		case SO_KEEPALIVE:
3349 		case SO_REUSEADDR:
3350 		case SO_REUSEPORT:
3351 		case SO_REUSEPORT_LB:
3352 		case SO_BROADCAST:
3353 		case SO_OOBINLINE:
3354 		case SO_ACCEPTCONN:
3355 		case SO_TIMESTAMP:
3356 		case SO_BINTIME:
3357 		case SO_NOSIGPIPE:
3358 		case SO_NO_DDP:
3359 		case SO_NO_OFFLOAD:
3360 		case SO_RERROR:
3361 			optval = so->so_options & sopt->sopt_name;
3362 integer:
3363 			error = sooptcopyout(sopt, &optval, sizeof optval);
3364 			break;
3365 
3366 		case SO_DOMAIN:
3367 			optval = so->so_proto->pr_domain->dom_family;
3368 			goto integer;
3369 
3370 		case SO_TYPE:
3371 			optval = so->so_type;
3372 			goto integer;
3373 
3374 		case SO_PROTOCOL:
3375 			optval = so->so_proto->pr_protocol;
3376 			goto integer;
3377 
3378 		case SO_ERROR:
3379 			SOCK_LOCK(so);
3380 			if (so->so_error) {
3381 				optval = so->so_error;
3382 				so->so_error = 0;
3383 			} else {
3384 				optval = so->so_rerror;
3385 				so->so_rerror = 0;
3386 			}
3387 			SOCK_UNLOCK(so);
3388 			goto integer;
3389 
3390 		case SO_SNDBUF:
3391 			optval = SOLISTENING(so) ? so->sol_sbsnd_hiwat :
3392 			    so->so_snd.sb_hiwat;
3393 			goto integer;
3394 
3395 		case SO_RCVBUF:
3396 			optval = SOLISTENING(so) ? so->sol_sbrcv_hiwat :
3397 			    so->so_rcv.sb_hiwat;
3398 			goto integer;
3399 
3400 		case SO_SNDLOWAT:
3401 			optval = SOLISTENING(so) ? so->sol_sbsnd_lowat :
3402 			    so->so_snd.sb_lowat;
3403 			goto integer;
3404 
3405 		case SO_RCVLOWAT:
3406 			optval = SOLISTENING(so) ? so->sol_sbrcv_lowat :
3407 			    so->so_rcv.sb_lowat;
3408 			goto integer;
3409 
3410 		case SO_SNDTIMEO:
3411 		case SO_RCVTIMEO:
3412 			SOCK_LOCK(so);
3413 			tv = sbttotv(sopt->sopt_name == SO_SNDTIMEO ?
3414 			    (SOLISTENING(so) ? so->sol_sbsnd_timeo :
3415 			    so->so_snd.sb_timeo) :
3416 			    (SOLISTENING(so) ? so->sol_sbrcv_timeo :
3417 			    so->so_rcv.sb_timeo));
3418 			SOCK_UNLOCK(so);
3419 #ifdef COMPAT_FREEBSD32
3420 			if (SV_CURPROC_FLAG(SV_ILP32)) {
3421 				struct timeval32 tv32;
3422 
3423 				CP(tv, tv32, tv_sec);
3424 				CP(tv, tv32, tv_usec);
3425 				error = sooptcopyout(sopt, &tv32, sizeof tv32);
3426 			} else
3427 #endif
3428 				error = sooptcopyout(sopt, &tv, sizeof tv);
3429 			break;
3430 
3431 		case SO_LABEL:
3432 #ifdef MAC
3433 			error = sooptcopyin(sopt, &extmac, sizeof(extmac),
3434 			    sizeof(extmac));
3435 			if (error)
3436 				goto bad;
3437 			error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
3438 			    so, &extmac);
3439 			if (error)
3440 				goto bad;
3441 			/* Don't copy out extmac, it is unchanged. */
3442 #else
3443 			error = EOPNOTSUPP;
3444 #endif
3445 			break;
3446 
3447 		case SO_PEERLABEL:
3448 #ifdef MAC
3449 			error = sooptcopyin(sopt, &extmac, sizeof(extmac),
3450 			    sizeof(extmac));
3451 			if (error)
3452 				goto bad;
3453 			error = mac_getsockopt_peerlabel(
3454 			    sopt->sopt_td->td_ucred, so, &extmac);
3455 			if (error)
3456 				goto bad;
3457 			/* Don't copy out extmac, it is unchanged. */
3458 #else
3459 			error = EOPNOTSUPP;
3460 #endif
3461 			break;
3462 
3463 		case SO_LISTENQLIMIT:
3464 			optval = SOLISTENING(so) ? so->sol_qlimit : 0;
3465 			goto integer;
3466 
3467 		case SO_LISTENQLEN:
3468 			optval = SOLISTENING(so) ? so->sol_qlen : 0;
3469 			goto integer;
3470 
3471 		case SO_LISTENINCQLEN:
3472 			optval = SOLISTENING(so) ? so->sol_incqlen : 0;
3473 			goto integer;
3474 
3475 		case SO_TS_CLOCK:
3476 			optval = so->so_ts_clock;
3477 			goto integer;
3478 
3479 		case SO_MAX_PACING_RATE:
3480 			optval = so->so_max_pacing_rate;
3481 			goto integer;
3482 
3483 		default:
3484 			if (V_socket_hhh[HHOOK_SOCKET_OPT]->hhh_nhooks > 0)
3485 				error = hhook_run_socket(so, sopt,
3486 				    HHOOK_SOCKET_OPT);
3487 			else
3488 				error = ENOPROTOOPT;
3489 			break;
3490 		}
3491 	}
3492 #ifdef MAC
3493 bad:
3494 #endif
3495 	CURVNET_RESTORE();
3496 	return (error);
3497 }
3498 
3499 int
3500 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
3501 {
3502 	struct mbuf *m, *m_prev;
3503 	int sopt_size = sopt->sopt_valsize;
3504 
3505 	MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
3506 	if (m == NULL)
3507 		return ENOBUFS;
3508 	if (sopt_size > MLEN) {
3509 		MCLGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT);
3510 		if ((m->m_flags & M_EXT) == 0) {
3511 			m_free(m);
3512 			return ENOBUFS;
3513 		}
3514 		m->m_len = min(MCLBYTES, sopt_size);
3515 	} else {
3516 		m->m_len = min(MLEN, sopt_size);
3517 	}
3518 	sopt_size -= m->m_len;
3519 	*mp = m;
3520 	m_prev = m;
3521 
3522 	while (sopt_size) {
3523 		MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
3524 		if (m == NULL) {
3525 			m_freem(*mp);
3526 			return ENOBUFS;
3527 		}
3528 		if (sopt_size > MLEN) {
3529 			MCLGET(m, sopt->sopt_td != NULL ? M_WAITOK :
3530 			    M_NOWAIT);
3531 			if ((m->m_flags & M_EXT) == 0) {
3532 				m_freem(m);
3533 				m_freem(*mp);
3534 				return ENOBUFS;
3535 			}
3536 			m->m_len = min(MCLBYTES, sopt_size);
3537 		} else {
3538 			m->m_len = min(MLEN, sopt_size);
3539 		}
3540 		sopt_size -= m->m_len;
3541 		m_prev->m_next = m;
3542 		m_prev = m;
3543 	}
3544 	return (0);
3545 }
3546 
3547 int
3548 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
3549 {
3550 	struct mbuf *m0 = m;
3551 
3552 	if (sopt->sopt_val == NULL)
3553 		return (0);
3554 	while (m != NULL && sopt->sopt_valsize >= m->m_len) {
3555 		if (sopt->sopt_td != NULL) {
3556 			int error;
3557 
3558 			error = copyin(sopt->sopt_val, mtod(m, char *),
3559 			    m->m_len);
3560 			if (error != 0) {
3561 				m_freem(m0);
3562 				return(error);
3563 			}
3564 		} else
3565 			bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
3566 		sopt->sopt_valsize -= m->m_len;
3567 		sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
3568 		m = m->m_next;
3569 	}
3570 	if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
3571 		panic("ip6_sooptmcopyin");
3572 	return (0);
3573 }
3574 
3575 int
3576 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
3577 {
3578 	struct mbuf *m0 = m;
3579 	size_t valsize = 0;
3580 
3581 	if (sopt->sopt_val == NULL)
3582 		return (0);
3583 	while (m != NULL && sopt->sopt_valsize >= m->m_len) {
3584 		if (sopt->sopt_td != NULL) {
3585 			int error;
3586 
3587 			error = copyout(mtod(m, char *), sopt->sopt_val,
3588 			    m->m_len);
3589 			if (error != 0) {
3590 				m_freem(m0);
3591 				return(error);
3592 			}
3593 		} else
3594 			bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
3595 		sopt->sopt_valsize -= m->m_len;
3596 		sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
3597 		valsize += m->m_len;
3598 		m = m->m_next;
3599 	}
3600 	if (m != NULL) {
3601 		/* enough soopt buffer should be given from user-land */
3602 		m_freem(m0);
3603 		return(EINVAL);
3604 	}
3605 	sopt->sopt_valsize = valsize;
3606 	return (0);
3607 }
3608 
3609 /*
3610  * sohasoutofband(): protocol notifies socket layer of the arrival of new
3611  * out-of-band data, which will then notify socket consumers.
3612  */
3613 void
3614 sohasoutofband(struct socket *so)
3615 {
3616 
3617 	if (so->so_sigio != NULL)
3618 		pgsigio(&so->so_sigio, SIGURG, 0);
3619 	selwakeuppri(&so->so_rdsel, PSOCK);
3620 }
3621 
3622 int
3623 sopoll(struct socket *so, int events, struct ucred *active_cred,
3624     struct thread *td)
3625 {
3626 
3627 	/*
3628 	 * We do not need to set or assert curvnet as long as everyone uses
3629 	 * sopoll_generic().
3630 	 */
3631 	return (so->so_proto->pr_sopoll(so, events, active_cred, td));
3632 }
3633 
3634 int
3635 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
3636     struct thread *td)
3637 {
3638 	int revents;
3639 
3640 	SOCK_LOCK(so);
3641 	if (SOLISTENING(so)) {
3642 		if (!(events & (POLLIN | POLLRDNORM)))
3643 			revents = 0;
3644 		else if (!TAILQ_EMPTY(&so->sol_comp))
3645 			revents = events & (POLLIN | POLLRDNORM);
3646 		else if ((events & POLLINIGNEOF) == 0 && so->so_error)
3647 			revents = (events & (POLLIN | POLLRDNORM)) | POLLHUP;
3648 		else {
3649 			selrecord(td, &so->so_rdsel);
3650 			revents = 0;
3651 		}
3652 	} else {
3653 		revents = 0;
3654 		SOCK_SENDBUF_LOCK(so);
3655 		SOCK_RECVBUF_LOCK(so);
3656 		if (events & (POLLIN | POLLRDNORM))
3657 			if (soreadabledata(so))
3658 				revents |= events & (POLLIN | POLLRDNORM);
3659 		if (events & (POLLOUT | POLLWRNORM))
3660 			if (sowriteable(so))
3661 				revents |= events & (POLLOUT | POLLWRNORM);
3662 		if (events & (POLLPRI | POLLRDBAND))
3663 			if (so->so_oobmark ||
3664 			    (so->so_rcv.sb_state & SBS_RCVATMARK))
3665 				revents |= events & (POLLPRI | POLLRDBAND);
3666 		if ((events & POLLINIGNEOF) == 0) {
3667 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3668 				revents |= events & (POLLIN | POLLRDNORM);
3669 				if (so->so_snd.sb_state & SBS_CANTSENDMORE)
3670 					revents |= POLLHUP;
3671 			}
3672 		}
3673 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3674 			revents |= events & POLLRDHUP;
3675 		if (revents == 0) {
3676 			if (events &
3677 			    (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND | POLLRDHUP)) {
3678 				selrecord(td, &so->so_rdsel);
3679 				so->so_rcv.sb_flags |= SB_SEL;
3680 			}
3681 			if (events & (POLLOUT | POLLWRNORM)) {
3682 				selrecord(td, &so->so_wrsel);
3683 				so->so_snd.sb_flags |= SB_SEL;
3684 			}
3685 		}
3686 		SOCK_RECVBUF_UNLOCK(so);
3687 		SOCK_SENDBUF_UNLOCK(so);
3688 	}
3689 	SOCK_UNLOCK(so);
3690 	return (revents);
3691 }
3692 
3693 int
3694 soo_kqfilter(struct file *fp, struct knote *kn)
3695 {
3696 	struct socket *so = kn->kn_fp->f_data;
3697 	struct sockbuf *sb;
3698 	sb_which which;
3699 	struct knlist *knl;
3700 
3701 	switch (kn->kn_filter) {
3702 	case EVFILT_READ:
3703 		kn->kn_fop = &soread_filtops;
3704 		knl = &so->so_rdsel.si_note;
3705 		sb = &so->so_rcv;
3706 		which = SO_RCV;
3707 		break;
3708 	case EVFILT_WRITE:
3709 		kn->kn_fop = &sowrite_filtops;
3710 		knl = &so->so_wrsel.si_note;
3711 		sb = &so->so_snd;
3712 		which = SO_SND;
3713 		break;
3714 	case EVFILT_EMPTY:
3715 		kn->kn_fop = &soempty_filtops;
3716 		knl = &so->so_wrsel.si_note;
3717 		sb = &so->so_snd;
3718 		which = SO_SND;
3719 		break;
3720 	default:
3721 		return (EINVAL);
3722 	}
3723 
3724 	SOCK_LOCK(so);
3725 	if (SOLISTENING(so)) {
3726 		knlist_add(knl, kn, 1);
3727 	} else {
3728 		SOCK_BUF_LOCK(so, which);
3729 		knlist_add(knl, kn, 1);
3730 		sb->sb_flags |= SB_KNOTE;
3731 		SOCK_BUF_UNLOCK(so, which);
3732 	}
3733 	SOCK_UNLOCK(so);
3734 	return (0);
3735 }
3736 
3737 static void
3738 filt_sordetach(struct knote *kn)
3739 {
3740 	struct socket *so = kn->kn_fp->f_data;
3741 
3742 	so_rdknl_lock(so);
3743 	knlist_remove(&so->so_rdsel.si_note, kn, 1);
3744 	if (!SOLISTENING(so) && knlist_empty(&so->so_rdsel.si_note))
3745 		so->so_rcv.sb_flags &= ~SB_KNOTE;
3746 	so_rdknl_unlock(so);
3747 }
3748 
3749 /*ARGSUSED*/
3750 static int
3751 filt_soread(struct knote *kn, long hint)
3752 {
3753 	struct socket *so;
3754 
3755 	so = kn->kn_fp->f_data;
3756 
3757 	if (SOLISTENING(so)) {
3758 		SOCK_LOCK_ASSERT(so);
3759 		kn->kn_data = so->sol_qlen;
3760 		if (so->so_error) {
3761 			kn->kn_flags |= EV_EOF;
3762 			kn->kn_fflags = so->so_error;
3763 			return (1);
3764 		}
3765 		return (!TAILQ_EMPTY(&so->sol_comp));
3766 	}
3767 
3768 	SOCK_RECVBUF_LOCK_ASSERT(so);
3769 
3770 	kn->kn_data = sbavail(&so->so_rcv) - so->so_rcv.sb_ctl;
3771 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3772 		kn->kn_flags |= EV_EOF;
3773 		kn->kn_fflags = so->so_error;
3774 		return (1);
3775 	} else if (so->so_error || so->so_rerror)
3776 		return (1);
3777 
3778 	if (kn->kn_sfflags & NOTE_LOWAT) {
3779 		if (kn->kn_data >= kn->kn_sdata)
3780 			return (1);
3781 	} else if (sbavail(&so->so_rcv) >= so->so_rcv.sb_lowat)
3782 		return (1);
3783 
3784 	/* This hook returning non-zero indicates an event, not error */
3785 	return (hhook_run_socket(so, NULL, HHOOK_FILT_SOREAD));
3786 }
3787 
3788 static void
3789 filt_sowdetach(struct knote *kn)
3790 {
3791 	struct socket *so = kn->kn_fp->f_data;
3792 
3793 	so_wrknl_lock(so);
3794 	knlist_remove(&so->so_wrsel.si_note, kn, 1);
3795 	if (!SOLISTENING(so) && knlist_empty(&so->so_wrsel.si_note))
3796 		so->so_snd.sb_flags &= ~SB_KNOTE;
3797 	so_wrknl_unlock(so);
3798 }
3799 
3800 /*ARGSUSED*/
3801 static int
3802 filt_sowrite(struct knote *kn, long hint)
3803 {
3804 	struct socket *so;
3805 
3806 	so = kn->kn_fp->f_data;
3807 
3808 	if (SOLISTENING(so))
3809 		return (0);
3810 
3811 	SOCK_SENDBUF_LOCK_ASSERT(so);
3812 	kn->kn_data = sbspace(&so->so_snd);
3813 
3814 	hhook_run_socket(so, kn, HHOOK_FILT_SOWRITE);
3815 
3816 	if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
3817 		kn->kn_flags |= EV_EOF;
3818 		kn->kn_fflags = so->so_error;
3819 		return (1);
3820 	} else if (so->so_error)	/* temporary udp error */
3821 		return (1);
3822 	else if (((so->so_state & SS_ISCONNECTED) == 0) &&
3823 	    (so->so_proto->pr_flags & PR_CONNREQUIRED))
3824 		return (0);
3825 	else if (kn->kn_sfflags & NOTE_LOWAT)
3826 		return (kn->kn_data >= kn->kn_sdata);
3827 	else
3828 		return (kn->kn_data >= so->so_snd.sb_lowat);
3829 }
3830 
3831 static int
3832 filt_soempty(struct knote *kn, long hint)
3833 {
3834 	struct socket *so;
3835 
3836 	so = kn->kn_fp->f_data;
3837 
3838 	if (SOLISTENING(so))
3839 		return (1);
3840 
3841 	SOCK_SENDBUF_LOCK_ASSERT(so);
3842 	kn->kn_data = sbused(&so->so_snd);
3843 
3844 	if (kn->kn_data == 0)
3845 		return (1);
3846 	else
3847 		return (0);
3848 }
3849 
3850 int
3851 socheckuid(struct socket *so, uid_t uid)
3852 {
3853 
3854 	if (so == NULL)
3855 		return (EPERM);
3856 	if (so->so_cred->cr_uid != uid)
3857 		return (EPERM);
3858 	return (0);
3859 }
3860 
3861 /*
3862  * These functions are used by protocols to notify the socket layer (and its
3863  * consumers) of state changes in the sockets driven by protocol-side events.
3864  */
3865 
3866 /*
3867  * Procedures to manipulate state flags of socket and do appropriate wakeups.
3868  *
3869  * Normal sequence from the active (originating) side is that
3870  * soisconnecting() is called during processing of connect() call, resulting
3871  * in an eventual call to soisconnected() if/when the connection is
3872  * established.  When the connection is torn down soisdisconnecting() is
3873  * called during processing of disconnect() call, and soisdisconnected() is
3874  * called when the connection to the peer is totally severed.  The semantics
3875  * of these routines are such that connectionless protocols can call
3876  * soisconnected() and soisdisconnected() only, bypassing the in-progress
3877  * calls when setting up a ``connection'' takes no time.
3878  *
3879  * From the passive side, a socket is created with two queues of sockets:
3880  * so_incomp for connections in progress and so_comp for connections already
3881  * made and awaiting user acceptance.  As a protocol is preparing incoming
3882  * connections, it creates a socket structure queued on so_incomp by calling
3883  * sonewconn().  When the connection is established, soisconnected() is
3884  * called, and transfers the socket structure to so_comp, making it available
3885  * to accept().
3886  *
3887  * If a socket is closed with sockets on either so_incomp or so_comp, these
3888  * sockets are dropped.
3889  *
3890  * If higher-level protocols are implemented in the kernel, the wakeups done
3891  * here will sometimes cause software-interrupt process scheduling.
3892  */
3893 void
3894 soisconnecting(struct socket *so)
3895 {
3896 
3897 	SOCK_LOCK(so);
3898 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3899 	so->so_state |= SS_ISCONNECTING;
3900 	SOCK_UNLOCK(so);
3901 }
3902 
3903 void
3904 soisconnected(struct socket *so)
3905 {
3906 	bool last __diagused;
3907 
3908 	SOCK_LOCK(so);
3909 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING);
3910 	so->so_state |= SS_ISCONNECTED;
3911 
3912 	if (so->so_qstate == SQ_INCOMP) {
3913 		struct socket *head = so->so_listen;
3914 		int ret;
3915 
3916 		KASSERT(head, ("%s: so %p on incomp of NULL", __func__, so));
3917 		/*
3918 		 * Promoting a socket from incomplete queue to complete, we
3919 		 * need to go through reverse order of locking.  We first do
3920 		 * trylock, and if that doesn't succeed, we go the hard way
3921 		 * leaving a reference and rechecking consistency after proper
3922 		 * locking.
3923 		 */
3924 		if (__predict_false(SOLISTEN_TRYLOCK(head) == 0)) {
3925 			soref(head);
3926 			SOCK_UNLOCK(so);
3927 			SOLISTEN_LOCK(head);
3928 			SOCK_LOCK(so);
3929 			if (__predict_false(head != so->so_listen)) {
3930 				/*
3931 				 * The socket went off the listen queue,
3932 				 * should be lost race to close(2) of sol.
3933 				 * The socket is about to soabort().
3934 				 */
3935 				SOCK_UNLOCK(so);
3936 				sorele_locked(head);
3937 				return;
3938 			}
3939 			last = refcount_release(&head->so_count);
3940 			KASSERT(!last, ("%s: released last reference for %p",
3941 			    __func__, head));
3942 		}
3943 again:
3944 		if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3945 			TAILQ_REMOVE(&head->sol_incomp, so, so_list);
3946 			head->sol_incqlen--;
3947 			TAILQ_INSERT_TAIL(&head->sol_comp, so, so_list);
3948 			head->sol_qlen++;
3949 			so->so_qstate = SQ_COMP;
3950 			SOCK_UNLOCK(so);
3951 			solisten_wakeup(head);	/* unlocks */
3952 		} else {
3953 			SOCK_RECVBUF_LOCK(so);
3954 			soupcall_set(so, SO_RCV,
3955 			    head->sol_accept_filter->accf_callback,
3956 			    head->sol_accept_filter_arg);
3957 			so->so_options &= ~SO_ACCEPTFILTER;
3958 			ret = head->sol_accept_filter->accf_callback(so,
3959 			    head->sol_accept_filter_arg, M_NOWAIT);
3960 			if (ret == SU_ISCONNECTED) {
3961 				soupcall_clear(so, SO_RCV);
3962 				SOCK_RECVBUF_UNLOCK(so);
3963 				goto again;
3964 			}
3965 			SOCK_RECVBUF_UNLOCK(so);
3966 			SOCK_UNLOCK(so);
3967 			SOLISTEN_UNLOCK(head);
3968 		}
3969 		return;
3970 	}
3971 	SOCK_UNLOCK(so);
3972 	wakeup(&so->so_timeo);
3973 	sorwakeup(so);
3974 	sowwakeup(so);
3975 }
3976 
3977 void
3978 soisdisconnecting(struct socket *so)
3979 {
3980 
3981 	SOCK_LOCK(so);
3982 	so->so_state &= ~SS_ISCONNECTING;
3983 	so->so_state |= SS_ISDISCONNECTING;
3984 
3985 	if (!SOLISTENING(so)) {
3986 		SOCK_RECVBUF_LOCK(so);
3987 		socantrcvmore_locked(so);
3988 		SOCK_SENDBUF_LOCK(so);
3989 		socantsendmore_locked(so);
3990 	}
3991 	SOCK_UNLOCK(so);
3992 	wakeup(&so->so_timeo);
3993 }
3994 
3995 void
3996 soisdisconnected(struct socket *so)
3997 {
3998 
3999 	SOCK_LOCK(so);
4000 
4001 	/*
4002 	 * There is at least one reader of so_state that does not
4003 	 * acquire socket lock, namely soreceive_generic().  Ensure
4004 	 * that it never sees all flags that track connection status
4005 	 * cleared, by ordering the update with a barrier semantic of
4006 	 * our release thread fence.
4007 	 */
4008 	so->so_state |= SS_ISDISCONNECTED;
4009 	atomic_thread_fence_rel();
4010 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
4011 
4012 	if (!SOLISTENING(so)) {
4013 		SOCK_UNLOCK(so);
4014 		SOCK_RECVBUF_LOCK(so);
4015 		socantrcvmore_locked(so);
4016 		SOCK_SENDBUF_LOCK(so);
4017 		sbdrop_locked(&so->so_snd, sbused(&so->so_snd));
4018 		socantsendmore_locked(so);
4019 	} else
4020 		SOCK_UNLOCK(so);
4021 	wakeup(&so->so_timeo);
4022 }
4023 
4024 int
4025 soiolock(struct socket *so, struct sx *sx, int flags)
4026 {
4027 	int error;
4028 
4029 	KASSERT((flags & SBL_VALID) == flags,
4030 	    ("soiolock: invalid flags %#x", flags));
4031 
4032 	if ((flags & SBL_WAIT) != 0) {
4033 		if ((flags & SBL_NOINTR) != 0) {
4034 			sx_xlock(sx);
4035 		} else {
4036 			error = sx_xlock_sig(sx);
4037 			if (error != 0)
4038 				return (error);
4039 		}
4040 	} else if (!sx_try_xlock(sx)) {
4041 		return (EWOULDBLOCK);
4042 	}
4043 
4044 	if (__predict_false(SOLISTENING(so))) {
4045 		sx_xunlock(sx);
4046 		return (ENOTCONN);
4047 	}
4048 	return (0);
4049 }
4050 
4051 void
4052 soiounlock(struct sx *sx)
4053 {
4054 	sx_xunlock(sx);
4055 }
4056 
4057 /*
4058  * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
4059  */
4060 struct sockaddr *
4061 sodupsockaddr(const struct sockaddr *sa, int mflags)
4062 {
4063 	struct sockaddr *sa2;
4064 
4065 	sa2 = malloc(sa->sa_len, M_SONAME, mflags);
4066 	if (sa2)
4067 		bcopy(sa, sa2, sa->sa_len);
4068 	return sa2;
4069 }
4070 
4071 /*
4072  * Register per-socket destructor.
4073  */
4074 void
4075 sodtor_set(struct socket *so, so_dtor_t *func)
4076 {
4077 
4078 	SOCK_LOCK_ASSERT(so);
4079 	so->so_dtor = func;
4080 }
4081 
4082 /*
4083  * Register per-socket buffer upcalls.
4084  */
4085 void
4086 soupcall_set(struct socket *so, sb_which which, so_upcall_t func, void *arg)
4087 {
4088 	struct sockbuf *sb;
4089 
4090 	KASSERT(!SOLISTENING(so), ("%s: so %p listening", __func__, so));
4091 
4092 	switch (which) {
4093 	case SO_RCV:
4094 		sb = &so->so_rcv;
4095 		break;
4096 	case SO_SND:
4097 		sb = &so->so_snd;
4098 		break;
4099 	}
4100 	SOCK_BUF_LOCK_ASSERT(so, which);
4101 	sb->sb_upcall = func;
4102 	sb->sb_upcallarg = arg;
4103 	sb->sb_flags |= SB_UPCALL;
4104 }
4105 
4106 void
4107 soupcall_clear(struct socket *so, sb_which which)
4108 {
4109 	struct sockbuf *sb;
4110 
4111 	KASSERT(!SOLISTENING(so), ("%s: so %p listening", __func__, so));
4112 
4113 	switch (which) {
4114 	case SO_RCV:
4115 		sb = &so->so_rcv;
4116 		break;
4117 	case SO_SND:
4118 		sb = &so->so_snd;
4119 		break;
4120 	}
4121 	SOCK_BUF_LOCK_ASSERT(so, which);
4122 	KASSERT(sb->sb_upcall != NULL,
4123 	    ("%s: so %p no upcall to clear", __func__, so));
4124 	sb->sb_upcall = NULL;
4125 	sb->sb_upcallarg = NULL;
4126 	sb->sb_flags &= ~SB_UPCALL;
4127 }
4128 
4129 void
4130 solisten_upcall_set(struct socket *so, so_upcall_t func, void *arg)
4131 {
4132 
4133 	SOLISTEN_LOCK_ASSERT(so);
4134 	so->sol_upcall = func;
4135 	so->sol_upcallarg = arg;
4136 }
4137 
4138 static void
4139 so_rdknl_lock(void *arg)
4140 {
4141 	struct socket *so = arg;
4142 
4143 retry:
4144 	if (SOLISTENING(so)) {
4145 		SOLISTEN_LOCK(so);
4146 	} else {
4147 		SOCK_RECVBUF_LOCK(so);
4148 		if (__predict_false(SOLISTENING(so))) {
4149 			SOCK_RECVBUF_UNLOCK(so);
4150 			goto retry;
4151 		}
4152 	}
4153 }
4154 
4155 static void
4156 so_rdknl_unlock(void *arg)
4157 {
4158 	struct socket *so = arg;
4159 
4160 	if (SOLISTENING(so))
4161 		SOLISTEN_UNLOCK(so);
4162 	else
4163 		SOCK_RECVBUF_UNLOCK(so);
4164 }
4165 
4166 static void
4167 so_rdknl_assert_lock(void *arg, int what)
4168 {
4169 	struct socket *so = arg;
4170 
4171 	if (what == LA_LOCKED) {
4172 		if (SOLISTENING(so))
4173 			SOLISTEN_LOCK_ASSERT(so);
4174 		else
4175 			SOCK_RECVBUF_LOCK_ASSERT(so);
4176 	} else {
4177 		if (SOLISTENING(so))
4178 			SOLISTEN_UNLOCK_ASSERT(so);
4179 		else
4180 			SOCK_RECVBUF_UNLOCK_ASSERT(so);
4181 	}
4182 }
4183 
4184 static void
4185 so_wrknl_lock(void *arg)
4186 {
4187 	struct socket *so = arg;
4188 
4189 retry:
4190 	if (SOLISTENING(so)) {
4191 		SOLISTEN_LOCK(so);
4192 	} else {
4193 		SOCK_SENDBUF_LOCK(so);
4194 		if (__predict_false(SOLISTENING(so))) {
4195 			SOCK_SENDBUF_UNLOCK(so);
4196 			goto retry;
4197 		}
4198 	}
4199 }
4200 
4201 static void
4202 so_wrknl_unlock(void *arg)
4203 {
4204 	struct socket *so = arg;
4205 
4206 	if (SOLISTENING(so))
4207 		SOLISTEN_UNLOCK(so);
4208 	else
4209 		SOCK_SENDBUF_UNLOCK(so);
4210 }
4211 
4212 static void
4213 so_wrknl_assert_lock(void *arg, int what)
4214 {
4215 	struct socket *so = arg;
4216 
4217 	if (what == LA_LOCKED) {
4218 		if (SOLISTENING(so))
4219 			SOLISTEN_LOCK_ASSERT(so);
4220 		else
4221 			SOCK_SENDBUF_LOCK_ASSERT(so);
4222 	} else {
4223 		if (SOLISTENING(so))
4224 			SOLISTEN_UNLOCK_ASSERT(so);
4225 		else
4226 			SOCK_SENDBUF_UNLOCK_ASSERT(so);
4227 	}
4228 }
4229 
4230 /*
4231  * Create an external-format (``xsocket'') structure using the information in
4232  * the kernel-format socket structure pointed to by so.  This is done to
4233  * reduce the spew of irrelevant information over this interface, to isolate
4234  * user code from changes in the kernel structure, and potentially to provide
4235  * information-hiding if we decide that some of this information should be
4236  * hidden from users.
4237  */
4238 void
4239 sotoxsocket(struct socket *so, struct xsocket *xso)
4240 {
4241 
4242 	bzero(xso, sizeof(*xso));
4243 	xso->xso_len = sizeof *xso;
4244 	xso->xso_so = (uintptr_t)so;
4245 	xso->so_type = so->so_type;
4246 	xso->so_options = so->so_options;
4247 	xso->so_linger = so->so_linger;
4248 	xso->so_state = so->so_state;
4249 	xso->so_pcb = (uintptr_t)so->so_pcb;
4250 	xso->xso_protocol = so->so_proto->pr_protocol;
4251 	xso->xso_family = so->so_proto->pr_domain->dom_family;
4252 	xso->so_timeo = so->so_timeo;
4253 	xso->so_error = so->so_error;
4254 	xso->so_uid = so->so_cred->cr_uid;
4255 	xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
4256 	if (SOLISTENING(so)) {
4257 		xso->so_qlen = so->sol_qlen;
4258 		xso->so_incqlen = so->sol_incqlen;
4259 		xso->so_qlimit = so->sol_qlimit;
4260 		xso->so_oobmark = 0;
4261 	} else {
4262 		xso->so_state |= so->so_qstate;
4263 		xso->so_qlen = xso->so_incqlen = xso->so_qlimit = 0;
4264 		xso->so_oobmark = so->so_oobmark;
4265 		sbtoxsockbuf(&so->so_snd, &xso->so_snd);
4266 		sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
4267 	}
4268 }
4269 
4270 int
4271 so_options_get(const struct socket *so)
4272 {
4273 
4274 	return (so->so_options);
4275 }
4276 
4277 void
4278 so_options_set(struct socket *so, int val)
4279 {
4280 
4281 	so->so_options = val;
4282 }
4283 
4284 int
4285 so_error_get(const struct socket *so)
4286 {
4287 
4288 	return (so->so_error);
4289 }
4290 
4291 void
4292 so_error_set(struct socket *so, int val)
4293 {
4294 
4295 	so->so_error = val;
4296 }
4297