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