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