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