xref: /openbsd-src/sys/kern/uipc_socket.c (revision 7b08975fc0d222558ca53c00d21416b54423d3bb)
1 /*	$OpenBSD: uipc_socket.c,v 1.360 2025/01/13 18:10:20 mvs Exp $	*/
2 /*	$NetBSD: uipc_socket.c,v 1.21 1996/02/04 02:17:52 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)uipc_socket.c	8.3 (Berkeley) 4/15/94
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/file.h>
39 #include <sys/filedesc.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/domain.h>
43 #include <sys/event.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/unpcb.h>
47 #include <sys/socketvar.h>
48 #include <sys/signalvar.h>
49 #include <sys/pool.h>
50 #include <sys/atomic.h>
51 #include <sys/rwlock.h>
52 #include <sys/time.h>
53 #include <sys/refcnt.h>
54 
55 #ifdef DDB
56 #include <machine/db_machdep.h>
57 #endif
58 
59 void	sbsync(struct sockbuf *, struct mbuf *);
60 
61 int	sosplice(struct socket *, int, off_t, struct timeval *);
62 void	sounsplice(struct socket *, struct socket *, int);
63 void	soidle(void *);
64 void	sotask(void *);
65 int	somove(struct socket *, int);
66 void	sorflush(struct socket *);
67 
68 void	filt_sordetach(struct knote *kn);
69 int	filt_soread(struct knote *kn, long hint);
70 void	filt_sowdetach(struct knote *kn);
71 int	filt_sowrite(struct knote *kn, long hint);
72 int	filt_soexcept(struct knote *kn, long hint);
73 
74 int	filt_sowmodify(struct kevent *kev, struct knote *kn);
75 int	filt_sowprocess(struct knote *kn, struct kevent *kev);
76 
77 int	filt_sormodify(struct kevent *kev, struct knote *kn);
78 int	filt_sorprocess(struct knote *kn, struct kevent *kev);
79 
80 const struct filterops soread_filtops = {
81 	.f_flags	= FILTEROP_ISFD | FILTEROP_MPSAFE,
82 	.f_attach	= NULL,
83 	.f_detach	= filt_sordetach,
84 	.f_event	= filt_soread,
85 	.f_modify	= filt_sormodify,
86 	.f_process	= filt_sorprocess,
87 };
88 
89 const struct filterops sowrite_filtops = {
90 	.f_flags	= FILTEROP_ISFD | FILTEROP_MPSAFE,
91 	.f_attach	= NULL,
92 	.f_detach	= filt_sowdetach,
93 	.f_event	= filt_sowrite,
94 	.f_modify	= filt_sowmodify,
95 	.f_process	= filt_sowprocess,
96 };
97 
98 const struct filterops soexcept_filtops = {
99 	.f_flags	= FILTEROP_ISFD | FILTEROP_MPSAFE,
100 	.f_attach	= NULL,
101 	.f_detach	= filt_sordetach,
102 	.f_event	= filt_soexcept,
103 	.f_modify	= filt_sormodify,
104 	.f_process	= filt_sorprocess,
105 };
106 
107 #ifndef SOMINCONN
108 #define SOMINCONN 80
109 #endif /* SOMINCONN */
110 
111 int	somaxconn = SOMAXCONN;
112 int	sominconn = SOMINCONN;
113 
114 struct pool socket_pool;
115 #ifdef SOCKET_SPLICE
116 struct pool sosplice_pool;
117 struct taskq *sosplice_taskq;
118 struct rwlock sosplice_lock = RWLOCK_INITIALIZER("sosplicelk");
119 #endif
120 
121 void
122 soinit(void)
123 {
124 	pool_init(&socket_pool, sizeof(struct socket), 0, IPL_SOFTNET, 0,
125 	    "sockpl", NULL);
126 #ifdef SOCKET_SPLICE
127 	pool_init(&sosplice_pool, sizeof(struct sosplice), 0, IPL_SOFTNET, 0,
128 	    "sosppl", NULL);
129 #endif
130 }
131 
132 struct socket *
133 soalloc(const struct protosw *prp, int wait)
134 {
135 	const struct domain *dp = prp->pr_domain;
136 	const char *dom_name = dp->dom_name;
137 	struct socket *so;
138 
139 	so = pool_get(&socket_pool, (wait == M_WAIT ? PR_WAITOK : PR_NOWAIT) |
140 	    PR_ZERO);
141 	if (so == NULL)
142 		return (NULL);
143 
144 #ifdef WITNESS
145 	/*
146 	 * XXX: Make WITNESS happy. AF_INET and AF_INET6 sockets could be
147 	 * spliced together.
148 	 */
149 	switch (dp->dom_family) {
150 	case AF_INET:
151 	case AF_INET6:
152 		dom_name = "inet46";
153 		break;
154 	}
155 #endif
156 
157 	refcnt_init_trace(&so->so_refcnt, DT_REFCNT_IDX_SOCKET);
158 	rw_init_flags(&so->so_lock, dom_name, RWL_DUPOK);
159 	rw_init(&so->so_rcv.sb_lock, "sbufrcv");
160 	rw_init(&so->so_snd.sb_lock, "sbufsnd");
161 	mtx_init_flags(&so->so_rcv.sb_mtx, IPL_MPFLOOR, "sbrcv", 0);
162 	mtx_init_flags(&so->so_snd.sb_mtx, IPL_MPFLOOR, "sbsnd", 0);
163 	klist_init_mutex(&so->so_rcv.sb_klist, &so->so_rcv.sb_mtx);
164 	klist_init_mutex(&so->so_snd.sb_klist, &so->so_snd.sb_mtx);
165 	sigio_init(&so->so_sigio);
166 	TAILQ_INIT(&so->so_q0);
167 	TAILQ_INIT(&so->so_q);
168 
169 	so->so_snd.sb_flags |= SB_MTXLOCK;
170 	so->so_rcv.sb_flags |= SB_MTXLOCK;
171 
172 	return (so);
173 }
174 
175 /*
176  * Socket operation routines.
177  * These routines are called by the routines in
178  * sys_socket.c or from a system process, and
179  * implement the semantics of socket operations by
180  * switching out to the protocol specific routines.
181  */
182 int
183 socreate(int dom, struct socket **aso, int type, int proto)
184 {
185 	struct proc *p = curproc;		/* XXX */
186 	const struct protosw *prp;
187 	struct socket *so;
188 	int error;
189 
190 	if (proto)
191 		prp = pffindproto(dom, proto, type);
192 	else
193 		prp = pffindtype(dom, type);
194 	if (prp == NULL || prp->pr_usrreqs == NULL)
195 		return (EPROTONOSUPPORT);
196 	if (prp->pr_type != type)
197 		return (EPROTOTYPE);
198 	so = soalloc(prp, M_WAIT);
199 	so->so_type = type;
200 	if (suser(p) == 0)
201 		so->so_state = SS_PRIV;
202 	so->so_ruid = p->p_ucred->cr_ruid;
203 	so->so_euid = p->p_ucred->cr_uid;
204 	so->so_rgid = p->p_ucred->cr_rgid;
205 	so->so_egid = p->p_ucred->cr_gid;
206 	so->so_cpid = p->p_p->ps_pid;
207 	so->so_proto = prp;
208 	so->so_snd.sb_timeo_nsecs = INFSLP;
209 	so->so_rcv.sb_timeo_nsecs = INFSLP;
210 
211 	solock(so);
212 	error = pru_attach(so, proto, M_WAIT);
213 	if (error) {
214 		so->so_state |= SS_NOFDREF;
215 		/* sofree() calls sounlock(). */
216 		sofree(so, 0);
217 		return (error);
218 	}
219 	sounlock(so);
220 	*aso = so;
221 	return (0);
222 }
223 
224 int
225 sobind(struct socket *so, struct mbuf *nam, struct proc *p)
226 {
227 	soassertlocked(so);
228 	return pru_bind(so, nam, p);
229 }
230 
231 int
232 solisten(struct socket *so, int backlog)
233 {
234 	int somaxconn_local = atomic_load_int(&somaxconn);
235 	int sominconn_local = atomic_load_int(&sominconn);
236 	int error;
237 
238 	switch (so->so_type) {
239 	case SOCK_STREAM:
240 	case SOCK_SEQPACKET:
241 		break;
242 	default:
243 		return (EOPNOTSUPP);
244 	}
245 
246 	soassertlocked(so);
247 
248 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING))
249 		return (EINVAL);
250 #ifdef SOCKET_SPLICE
251 	if (isspliced(so) || issplicedback(so))
252 		return (EOPNOTSUPP);
253 #endif /* SOCKET_SPLICE */
254 	error = pru_listen(so);
255 	if (error)
256 		return (error);
257 	if (TAILQ_FIRST(&so->so_q) == NULL)
258 		so->so_options |= SO_ACCEPTCONN;
259 	if (backlog < 0 || backlog > somaxconn_local)
260 		backlog = somaxconn_local;
261 	if (backlog < sominconn_local)
262 		backlog = sominconn_local;
263 	so->so_qlimit = backlog;
264 	return (0);
265 }
266 
267 void
268 sorele(struct socket *so)
269 {
270 	if (refcnt_rele(&so->so_refcnt) == 0)
271 		return;
272 
273 	sigio_free(&so->so_sigio);
274 	klist_free(&so->so_rcv.sb_klist);
275 	klist_free(&so->so_snd.sb_klist);
276 
277 	mtx_enter(&so->so_snd.sb_mtx);
278 	sbrelease(so, &so->so_snd);
279 	mtx_leave(&so->so_snd.sb_mtx);
280 
281 	if (so->so_proto->pr_flags & PR_RIGHTS &&
282 	    so->so_proto->pr_domain->dom_dispose)
283 		(*so->so_proto->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
284 	m_purge(so->so_rcv.sb_mb);
285 
286 #ifdef SOCKET_SPLICE
287 	if (so->so_sp)
288 		pool_put(&sosplice_pool, so->so_sp);
289 #endif
290 	pool_put(&socket_pool, so);
291 }
292 
293 #define SOSP_FREEING_READ	1
294 #define SOSP_FREEING_WRITE	2
295 void
296 sofree(struct socket *so, int keep_lock)
297 {
298 	int persocket = solock_persocket(so);
299 
300 	soassertlocked(so);
301 
302 	if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) {
303 		if (!keep_lock)
304 			sounlock(so);
305 		return;
306 	}
307 	if (so->so_head) {
308 		struct socket *head = so->so_head;
309 
310 		/*
311 		 * We must not decommission a socket that's on the accept(2)
312 		 * queue.  If we do, then accept(2) may hang after select(2)
313 		 * indicated that the listening socket was ready.
314 		 */
315 		if (so->so_onq == &head->so_q) {
316 			if (!keep_lock)
317 				sounlock(so);
318 			return;
319 		}
320 
321 		if (persocket) {
322 			soref(head);
323 			sounlock(so);
324 			solock(head);
325 			solock(so);
326 
327 			if (so->so_onq != &head->so_q0) {
328 				sounlock(so);
329 				sounlock(head);
330 				sorele(head);
331 				return;
332 			}
333 		}
334 
335 		soqremque(so, 0);
336 
337 		if (persocket) {
338 			sounlock(head);
339 			sorele(head);
340 		}
341 	}
342 
343 	if (!keep_lock)
344 		sounlock(so);
345 	sorele(so);
346 }
347 
348 static inline uint64_t
349 solinger_nsec(struct socket *so)
350 {
351 	if (so->so_linger == 0)
352 		return INFSLP;
353 
354 	return SEC_TO_NSEC(so->so_linger);
355 }
356 
357 /*
358  * Close a socket on last file table reference removal.
359  * Initiate disconnect if connected.
360  * Free socket when disconnect complete.
361  */
362 int
363 soclose(struct socket *so, int flags)
364 {
365 	struct socket *so2;
366 	int error = 0;
367 
368 	solock(so);
369 	/* Revoke async IO early. There is a final revocation in sofree(). */
370 	sigio_free(&so->so_sigio);
371 	if (so->so_state & SS_ISCONNECTED) {
372 		if (so->so_pcb == NULL)
373 			goto discard;
374 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
375 			error = sodisconnect(so);
376 			if (error)
377 				goto drop;
378 		}
379 		if (so->so_options & SO_LINGER) {
380 			if ((so->so_state & SS_ISDISCONNECTING) &&
381 			    (flags & MSG_DONTWAIT))
382 				goto drop;
383 			while (so->so_state & SS_ISCONNECTED) {
384 				error = sosleep_nsec(so, &so->so_timeo,
385 				    PSOCK | PCATCH, "netcls",
386 				    solinger_nsec(so));
387 				if (error)
388 					break;
389 			}
390 		}
391 	}
392 drop:
393 	if (so->so_pcb) {
394 		int error2;
395 		error2 = pru_detach(so);
396 		if (error == 0)
397 			error = error2;
398 	}
399 	if (so->so_options & SO_ACCEPTCONN) {
400 		int persocket = solock_persocket(so);
401 
402 		while ((so2 = TAILQ_FIRST(&so->so_q0)) != NULL) {
403 			if (persocket)
404 				solock(so2);
405 			(void) soqremque(so2, 0);
406 			if (persocket)
407 				sounlock(so);
408 			soabort(so2);
409 			if (persocket)
410 				solock(so);
411 		}
412 		while ((so2 = TAILQ_FIRST(&so->so_q)) != NULL) {
413 			if (persocket)
414 				solock(so2);
415 			(void) soqremque(so2, 1);
416 			if (persocket)
417 				sounlock(so);
418 			soabort(so2);
419 			if (persocket)
420 				solock(so);
421 		}
422 	}
423 discard:
424 #ifdef SOCKET_SPLICE
425 	if (so->so_sp) {
426 		struct socket *soback;
427 
428 		sounlock(so);
429 		mtx_enter(&so->so_snd.sb_mtx);
430 		/*
431 		 * Concurrent sounsplice() locks `sb_mtx' mutexes on
432 		 * both `so_snd' and `so_rcv' before unsplice sockets.
433 		 */
434 		if ((soback = so->so_sp->ssp_soback) == NULL) {
435 			mtx_leave(&so->so_snd.sb_mtx);
436 			goto notsplicedback;
437 		}
438 		soref(soback);
439 		mtx_leave(&so->so_snd.sb_mtx);
440 
441 		/*
442 		 * `so' can be only unspliced, and never spliced again.
443 		 * Thus if issplicedback(so) check is positive, socket is
444 		 * still spliced and `ssp_soback' points to the same
445 		 * socket that `soback'.
446 		 */
447 		sblock(&soback->so_rcv, SBL_WAIT | SBL_NOINTR);
448 		if (issplicedback(so)) {
449 			int freeing = SOSP_FREEING_WRITE;
450 
451 			if (so->so_sp->ssp_soback == so)
452 				freeing |= SOSP_FREEING_READ;
453 			sounsplice(so->so_sp->ssp_soback, so, freeing);
454 		}
455 		sbunlock(&soback->so_rcv);
456 		sorele(soback);
457 
458 notsplicedback:
459 		sblock(&so->so_rcv, SBL_WAIT | SBL_NOINTR);
460 		if (isspliced(so)) {
461 			struct socket *sosp;
462 			int freeing = SOSP_FREEING_READ;
463 
464 			if (so == so->so_sp->ssp_socket)
465 				freeing |= SOSP_FREEING_WRITE;
466 			sosp = soref(so->so_sp->ssp_socket);
467 			sounsplice(so, so->so_sp->ssp_socket, freeing);
468 			sorele(sosp);
469 		}
470 		sbunlock(&so->so_rcv);
471 
472 		timeout_del_barrier(&so->so_sp->ssp_idleto);
473 		task_del(sosplice_taskq, &so->so_sp->ssp_task);
474 		taskq_barrier(sosplice_taskq);
475 
476 		solock(so);
477 	}
478 #endif /* SOCKET_SPLICE */
479 
480 	if (so->so_state & SS_NOFDREF)
481 		panic("soclose NOFDREF: so %p, so_type %d", so, so->so_type);
482 	so->so_state |= SS_NOFDREF;
483 
484 	/* sofree() calls sounlock(). */
485 	sofree(so, 0);
486 	return (error);
487 }
488 
489 void
490 soabort(struct socket *so)
491 {
492 	soassertlocked(so);
493 	pru_abort(so);
494 }
495 
496 int
497 soaccept(struct socket *so, struct mbuf *nam)
498 {
499 	int error = 0;
500 
501 	soassertlocked(so);
502 
503 	if ((so->so_state & SS_NOFDREF) == 0)
504 		panic("soaccept !NOFDREF: so %p, so_type %d", so, so->so_type);
505 	so->so_state &= ~SS_NOFDREF;
506 	if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
507 	    (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
508 		error = pru_accept(so, nam);
509 	else
510 		error = ECONNABORTED;
511 	return (error);
512 }
513 
514 int
515 soconnect(struct socket *so, struct mbuf *nam)
516 {
517 	int error;
518 
519 	soassertlocked(so);
520 
521 	if (so->so_options & SO_ACCEPTCONN)
522 		return (EOPNOTSUPP);
523 	/*
524 	 * If protocol is connection-based, can only connect once.
525 	 * Otherwise, if connected, try to disconnect first.
526 	 * This allows user to disconnect by connecting to, e.g.,
527 	 * a null address.
528 	 */
529 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
530 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
531 	    (error = sodisconnect(so))))
532 		error = EISCONN;
533 	else
534 		error = pru_connect(so, nam);
535 	return (error);
536 }
537 
538 int
539 soconnect2(struct socket *so1, struct socket *so2)
540 {
541 	int persocket, error;
542 
543 	if ((persocket = solock_persocket(so1)))
544 		solock_pair(so1, so2);
545 	else
546 		solock(so1);
547 
548 	error = pru_connect2(so1, so2);
549 
550 	if (persocket)
551 		sounlock(so2);
552 	sounlock(so1);
553 	return (error);
554 }
555 
556 int
557 sodisconnect(struct socket *so)
558 {
559 	int error;
560 
561 	soassertlocked(so);
562 
563 	if ((so->so_state & SS_ISCONNECTED) == 0)
564 		return (ENOTCONN);
565 	if (so->so_state & SS_ISDISCONNECTING)
566 		return (EALREADY);
567 	error = pru_disconnect(so);
568 	return (error);
569 }
570 
571 int m_getuio(struct mbuf **, int, long, struct uio *);
572 
573 #define	SBLOCKWAIT(f)	(((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
574 /*
575  * Send on a socket.
576  * If send must go all at once and message is larger than
577  * send buffering, then hard error.
578  * Lock against other senders.
579  * If must go all at once and not enough room now, then
580  * inform user that this would block and do nothing.
581  * Otherwise, if nonblocking, send as much as possible.
582  * The data to be sent is described by "uio" if nonzero,
583  * otherwise by the mbuf chain "top" (which must be null
584  * if uio is not).  Data provided in mbuf chain must be small
585  * enough to send all at once.
586  *
587  * Returns nonzero on error, timeout or signal; callers
588  * must check for short counts if EINTR/ERESTART are returned.
589  * Data and control buffers are freed on return.
590  */
591 int
592 sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
593     struct mbuf *control, int flags)
594 {
595 	long space, clen = 0;
596 	size_t resid;
597 	int error;
598 	int atomic = sosendallatonce(so) || top;
599 	int dosolock = ((so->so_snd.sb_flags & SB_MTXLOCK) == 0);
600 
601 	if (uio)
602 		resid = uio->uio_resid;
603 	else
604 		resid = top->m_pkthdr.len;
605 	/* MSG_EOR on a SOCK_STREAM socket is invalid. */
606 	if (so->so_type == SOCK_STREAM && (flags & MSG_EOR)) {
607 		m_freem(top);
608 		m_freem(control);
609 		return (EINVAL);
610 	}
611 	if (uio && uio->uio_procp)
612 		uio->uio_procp->p_ru.ru_msgsnd++;
613 	if (control) {
614 		/*
615 		 * In theory clen should be unsigned (since control->m_len is).
616 		 * However, space must be signed, as it might be less than 0
617 		 * if we over-committed, and we must use a signed comparison
618 		 * of space and clen.
619 		 */
620 		clen = control->m_len;
621 		/* reserve extra space for AF_UNIX's internalize */
622 		if (so->so_proto->pr_domain->dom_family == AF_UNIX &&
623 		    clen >= CMSG_ALIGN(sizeof(struct cmsghdr)) &&
624 		    mtod(control, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
625 			clen = CMSG_SPACE(
626 			    (clen - CMSG_ALIGN(sizeof(struct cmsghdr))) *
627 			    (sizeof(struct fdpass) / sizeof(int)));
628 	}
629 
630 #define	snderr(errno)	{ error = errno; goto release; }
631 
632 restart:
633 	if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
634 		goto out;
635 	if (dosolock)
636 		solock_shared(so);
637 	sb_mtx_lock(&so->so_snd);
638 	so->so_snd.sb_state |= SS_ISSENDING;
639 	do {
640 		if (so->so_snd.sb_state & SS_CANTSENDMORE)
641 			snderr(EPIPE);
642 		if ((error = READ_ONCE(so->so_error))) {
643 			so->so_error = 0;
644 			snderr(error);
645 		}
646 		if ((so->so_state & SS_ISCONNECTED) == 0) {
647 			if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
648 				if (!(resid == 0 && clen != 0))
649 					snderr(ENOTCONN);
650 			} else if (addr == NULL)
651 				snderr(EDESTADDRREQ);
652 		}
653 		space = sbspace_locked(so, &so->so_snd);
654 		if (flags & MSG_OOB)
655 			space += 1024;
656 		if (so->so_proto->pr_domain->dom_family == AF_UNIX) {
657 			if (atomic && resid > so->so_snd.sb_hiwat)
658 				snderr(EMSGSIZE);
659 		} else {
660 			if (clen > so->so_snd.sb_hiwat ||
661 			    (atomic && resid > so->so_snd.sb_hiwat - clen))
662 				snderr(EMSGSIZE);
663 		}
664 		if (space < clen ||
665 		    (space - clen < resid &&
666 		    (atomic || space < so->so_snd.sb_lowat))) {
667 			if (flags & MSG_DONTWAIT)
668 				snderr(EWOULDBLOCK);
669 			sbunlock(&so->so_snd);
670 			error = sbwait(so, &so->so_snd);
671 			so->so_snd.sb_state &= ~SS_ISSENDING;
672 			sb_mtx_unlock(&so->so_snd);
673 			if (dosolock)
674 				sounlock_shared(so);
675 			if (error)
676 				goto out;
677 			goto restart;
678 		}
679 		space -= clen;
680 		do {
681 			if (uio == NULL) {
682 				/*
683 				 * Data is prepackaged in "top".
684 				 */
685 				resid = 0;
686 				if (flags & MSG_EOR)
687 					top->m_flags |= M_EOR;
688 			} else {
689 				sb_mtx_unlock(&so->so_snd);
690 				if (dosolock)
691 					sounlock_shared(so);
692 				error = m_getuio(&top, atomic, space, uio);
693 				if (dosolock)
694 					solock_shared(so);
695 				sb_mtx_lock(&so->so_snd);
696 				if (error)
697 					goto release;
698 				space -= top->m_pkthdr.len;
699 				resid = uio->uio_resid;
700 				if (flags & MSG_EOR)
701 					top->m_flags |= M_EOR;
702 			}
703 			if (resid == 0)
704 				so->so_snd.sb_state &= ~SS_ISSENDING;
705 			if (top && so->so_options & SO_ZEROIZE)
706 				top->m_flags |= M_ZEROIZE;
707 			sb_mtx_unlock(&so->so_snd);
708 			if (!dosolock)
709 				solock_shared(so);
710 			if (flags & MSG_OOB)
711 				error = pru_sendoob(so, top, addr, control);
712 			else
713 				error = pru_send(so, top, addr, control);
714 			if (!dosolock)
715 				sounlock_shared(so);
716 			sb_mtx_lock(&so->so_snd);
717 			clen = 0;
718 			control = NULL;
719 			top = NULL;
720 			if (error)
721 				goto release;
722 		} while (resid && space > 0);
723 	} while (resid);
724 
725 release:
726 	so->so_snd.sb_state &= ~SS_ISSENDING;
727 	sb_mtx_unlock(&so->so_snd);
728 	if (dosolock)
729 		sounlock_shared(so);
730 	sbunlock(&so->so_snd);
731 out:
732 	m_freem(top);
733 	m_freem(control);
734 	return (error);
735 }
736 
737 int
738 m_getuio(struct mbuf **mp, int atomic, long space, struct uio *uio)
739 {
740 	struct mbuf *m, *top = NULL;
741 	struct mbuf **nextp = &top;
742 	u_long len, mlen;
743 	size_t resid = uio->uio_resid;
744 	int error;
745 
746 	do {
747 		if (top == NULL) {
748 			MGETHDR(m, M_WAIT, MT_DATA);
749 			mlen = MHLEN;
750 		} else {
751 			MGET(m, M_WAIT, MT_DATA);
752 			mlen = MLEN;
753 		}
754 		/* chain mbuf together */
755 		*nextp = m;
756 		nextp = &m->m_next;
757 
758 		resid = ulmin(resid, space);
759 		if (resid >= MINCLSIZE) {
760 			MCLGETL(m, M_NOWAIT, ulmin(resid, MAXMCLBYTES));
761 			if ((m->m_flags & M_EXT) == 0)
762 				MCLGETL(m, M_NOWAIT, MCLBYTES);
763 			if ((m->m_flags & M_EXT) == 0)
764 				goto nopages;
765 			mlen = m->m_ext.ext_size;
766 			len = ulmin(mlen, resid);
767 			/*
768 			 * For datagram protocols, leave room
769 			 * for protocol headers in first mbuf.
770 			 */
771 			if (atomic && m == top && len < mlen - max_hdr)
772 				m->m_data += max_hdr;
773 		} else {
774 nopages:
775 			len = ulmin(mlen, resid);
776 			/*
777 			 * For datagram protocols, leave room
778 			 * for protocol headers in first mbuf.
779 			 */
780 			if (atomic && m == top && len < mlen - max_hdr)
781 				m_align(m, len);
782 		}
783 
784 		error = uiomove(mtod(m, caddr_t), len, uio);
785 		if (error) {
786 			m_freem(top);
787 			return (error);
788 		}
789 
790 		/* adjust counters */
791 		resid = uio->uio_resid;
792 		space -= len;
793 		m->m_len = len;
794 		top->m_pkthdr.len += len;
795 
796 		/* Is there more space and more data? */
797 	} while (space > 0 && resid > 0);
798 
799 	*mp = top;
800 	return 0;
801 }
802 
803 /*
804  * Following replacement or removal of the first mbuf on the first
805  * mbuf chain of a socket buffer, push necessary state changes back
806  * into the socket buffer so that other consumers see the values
807  * consistently.  'nextrecord' is the callers locally stored value of
808  * the original value of sb->sb_mb->m_nextpkt which must be restored
809  * when the lead mbuf changes.  NOTE: 'nextrecord' may be NULL.
810  */
811 void
812 sbsync(struct sockbuf *sb, struct mbuf *nextrecord)
813 {
814 
815 	/*
816 	 * First, update for the new value of nextrecord.  If necessary,
817 	 * make it the first record.
818 	 */
819 	if (sb->sb_mb != NULL)
820 		sb->sb_mb->m_nextpkt = nextrecord;
821 	else
822 		sb->sb_mb = nextrecord;
823 
824 	/*
825 	 * Now update any dependent socket buffer fields to reflect
826 	 * the new state.  This is an inline of SB_EMPTY_FIXUP, with
827 	 * the addition of a second clause that takes care of the
828 	 * case where sb_mb has been updated, but remains the last
829 	 * record.
830 	 */
831 	if (sb->sb_mb == NULL) {
832 		sb->sb_mbtail = NULL;
833 		sb->sb_lastrecord = NULL;
834 	} else if (sb->sb_mb->m_nextpkt == NULL)
835 		sb->sb_lastrecord = sb->sb_mb;
836 }
837 
838 /*
839  * Implement receive operations on a socket.
840  * We depend on the way that records are added to the sockbuf
841  * by sbappend*.  In particular, each record (mbufs linked through m_next)
842  * must begin with an address if the protocol so specifies,
843  * followed by an optional mbuf or mbufs containing ancillary data,
844  * and then zero or more mbufs of data.
845  * In order to avoid blocking network for the entire time here, we release
846  * the solock() while doing the actual copy to user space.
847  * Although the sockbuf is locked, new data may still be appended,
848  * and thus we must maintain consistency of the sockbuf during that time.
849  *
850  * The caller may receive the data as a single mbuf chain by supplying
851  * an mbuf **mp0 for use in returning the chain.  The uio is then used
852  * only for the count in uio_resid.
853  */
854 int
855 soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
856     struct mbuf **mp0, struct mbuf **controlp, int *flagsp,
857     socklen_t controllen)
858 {
859 	struct mbuf *m, **mp;
860 	struct mbuf *cm;
861 	u_long len, offset, moff;
862 	int flags, error, error2, type, uio_error = 0;
863 	const struct protosw *pr = so->so_proto;
864 	struct mbuf *nextrecord;
865 	size_t resid, orig_resid = uio->uio_resid;
866 	int dosolock = ((so->so_rcv.sb_flags & SB_MTXLOCK) == 0);
867 
868 	mp = mp0;
869 	if (paddr)
870 		*paddr = NULL;
871 	if (controlp)
872 		*controlp = NULL;
873 	if (flagsp)
874 		flags = *flagsp &~ MSG_EOR;
875 	else
876 		flags = 0;
877 	if (flags & MSG_OOB) {
878 		m = m_get(M_WAIT, MT_DATA);
879 		solock_shared(so);
880 		error = pru_rcvoob(so, m, flags & MSG_PEEK);
881 		sounlock_shared(so);
882 		if (error)
883 			goto bad;
884 		do {
885 			error = uiomove(mtod(m, caddr_t),
886 			    ulmin(uio->uio_resid, m->m_len), uio);
887 			m = m_free(m);
888 		} while (uio->uio_resid && error == 0 && m);
889 bad:
890 		m_freem(m);
891 		return (error);
892 	}
893 	if (mp)
894 		*mp = NULL;
895 
896 restart:
897 	if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
898 		return (error);
899 	if (dosolock)
900 		solock_shared(so);
901 	sb_mtx_lock(&so->so_rcv);
902 
903 	m = so->so_rcv.sb_mb;
904 #ifdef SOCKET_SPLICE
905 	if (isspliced(so))
906 		m = NULL;
907 #endif /* SOCKET_SPLICE */
908 	/*
909 	 * If we have less data than requested, block awaiting more
910 	 * (subject to any timeout) if:
911 	 *   1. the current count is less than the low water mark,
912 	 *   2. MSG_WAITALL is set, and it is possible to do the entire
913 	 *	receive operation at once if we block (resid <= hiwat), or
914 	 *   3. MSG_DONTWAIT is not set.
915 	 * If MSG_WAITALL is set but resid is larger than the receive buffer,
916 	 * we have to do the receive in sections, and thus risk returning
917 	 * a short count if a timeout or signal occurs after we start.
918 	 */
919 	if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
920 	    so->so_rcv.sb_cc < uio->uio_resid) &&
921 	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
922 	    ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
923 	    m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
924 #ifdef DIAGNOSTIC
925 		if (m == NULL && so->so_rcv.sb_cc)
926 #ifdef SOCKET_SPLICE
927 		    if (!isspliced(so))
928 #endif /* SOCKET_SPLICE */
929 			panic("receive 1: so %p, so_type %d, sb_cc %lu",
930 			    so, so->so_type, so->so_rcv.sb_cc);
931 #endif
932 		if ((error2 = READ_ONCE(so->so_error))) {
933 			if (m)
934 				goto dontblock;
935 			error = error2;
936 			if ((flags & MSG_PEEK) == 0)
937 				so->so_error = 0;
938 			goto release;
939 		}
940 		if (so->so_rcv.sb_state & SS_CANTRCVMORE) {
941 			if (m)
942 				goto dontblock;
943 			else if (so->so_rcv.sb_cc == 0)
944 				goto release;
945 		}
946 		for (; m; m = m->m_next)
947 			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
948 				m = so->so_rcv.sb_mb;
949 				goto dontblock;
950 			}
951 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
952 		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
953 			error = ENOTCONN;
954 			goto release;
955 		}
956 		if (uio->uio_resid == 0 && controlp == NULL)
957 			goto release;
958 		if (flags & MSG_DONTWAIT) {
959 			error = EWOULDBLOCK;
960 			goto release;
961 		}
962 		SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1");
963 		SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1");
964 
965 		sbunlock(&so->so_rcv);
966 		error = sbwait(so, &so->so_rcv);
967 		sb_mtx_unlock(&so->so_rcv);
968 		if (dosolock)
969 			sounlock_shared(so);
970 		if (error)
971 			return (error);
972 		goto restart;
973 	}
974 dontblock:
975 	/*
976 	 * On entry here, m points to the first record of the socket buffer.
977 	 * From this point onward, we maintain 'nextrecord' as a cache of the
978 	 * pointer to the next record in the socket buffer.  We must keep the
979 	 * various socket buffer pointers and local stack versions of the
980 	 * pointers in sync, pushing out modifications before operations that
981 	 * may sleep, and re-reading them afterwards.
982 	 *
983 	 * Otherwise, we will race with the network stack appending new data
984 	 * or records onto the socket buffer by using inconsistent/stale
985 	 * versions of the field, possibly resulting in socket buffer
986 	 * corruption.
987 	 */
988 	if (uio->uio_procp)
989 		uio->uio_procp->p_ru.ru_msgrcv++;
990 	KASSERT(m == so->so_rcv.sb_mb);
991 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
992 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
993 	nextrecord = m->m_nextpkt;
994 	if (pr->pr_flags & PR_ADDR) {
995 #ifdef DIAGNOSTIC
996 		if (m->m_type != MT_SONAME)
997 			panic("receive 1a: so %p, so_type %d, m %p, m_type %d",
998 			    so, so->so_type, m, m->m_type);
999 #endif
1000 		orig_resid = 0;
1001 		if (flags & MSG_PEEK) {
1002 			if (paddr)
1003 				*paddr = m_copym(m, 0, m->m_len, M_NOWAIT);
1004 			m = m->m_next;
1005 		} else {
1006 			sbfree(so, &so->so_rcv, m);
1007 			if (paddr) {
1008 				*paddr = m;
1009 				so->so_rcv.sb_mb = m->m_next;
1010 				m->m_next = NULL;
1011 				m = so->so_rcv.sb_mb;
1012 			} else {
1013 				so->so_rcv.sb_mb = m_free(m);
1014 				m = so->so_rcv.sb_mb;
1015 			}
1016 			sbsync(&so->so_rcv, nextrecord);
1017 		}
1018 	}
1019 	while (m && m->m_type == MT_CONTROL && error == 0) {
1020 		int skip = 0;
1021 		if (flags & MSG_PEEK) {
1022 			if (mtod(m, struct cmsghdr *)->cmsg_type ==
1023 			    SCM_RIGHTS) {
1024 				/* don't leak internalized SCM_RIGHTS msgs */
1025 				skip = 1;
1026 			} else if (controlp)
1027 				*controlp = m_copym(m, 0, m->m_len, M_NOWAIT);
1028 			m = m->m_next;
1029 		} else {
1030 			sbfree(so, &so->so_rcv, m);
1031 			so->so_rcv.sb_mb = m->m_next;
1032 			m->m_nextpkt = m->m_next = NULL;
1033 			cm = m;
1034 			m = so->so_rcv.sb_mb;
1035 			sbsync(&so->so_rcv, nextrecord);
1036 			if (controlp) {
1037 				if (pr->pr_domain->dom_externalize) {
1038 					sb_mtx_unlock(&so->so_rcv);
1039 					if (dosolock)
1040 						sounlock_shared(so);
1041 					error =
1042 					    (*pr->pr_domain->dom_externalize)
1043 					    (cm, controllen, flags);
1044 					if (dosolock)
1045 						solock_shared(so);
1046 					sb_mtx_lock(&so->so_rcv);
1047 				}
1048 				*controlp = cm;
1049 			} else {
1050 				/*
1051 				 * Dispose of any SCM_RIGHTS message that went
1052 				 * through the read path rather than recv.
1053 				 */
1054 				if (pr->pr_domain->dom_dispose) {
1055 					sb_mtx_unlock(&so->so_rcv);
1056 					pr->pr_domain->dom_dispose(cm);
1057 					sb_mtx_lock(&so->so_rcv);
1058 				}
1059 				m_free(cm);
1060 			}
1061 		}
1062 		if (m != NULL)
1063 			nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1064 		else
1065 			nextrecord = so->so_rcv.sb_mb;
1066 		if (controlp && !skip)
1067 			controlp = &(*controlp)->m_next;
1068 		orig_resid = 0;
1069 	}
1070 
1071 	/* If m is non-NULL, we have some data to read. */
1072 	if (m) {
1073 		type = m->m_type;
1074 		if (type == MT_OOBDATA)
1075 			flags |= MSG_OOB;
1076 		if (m->m_flags & M_BCAST)
1077 			flags |= MSG_BCAST;
1078 		if (m->m_flags & M_MCAST)
1079 			flags |= MSG_MCAST;
1080 	}
1081 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
1082 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
1083 
1084 	moff = 0;
1085 	offset = 0;
1086 	while (m && uio->uio_resid > 0 && error == 0) {
1087 		if (m->m_type == MT_OOBDATA) {
1088 			if (type != MT_OOBDATA)
1089 				break;
1090 		} else if (type == MT_OOBDATA) {
1091 			break;
1092 		} else if (m->m_type == MT_CONTROL) {
1093 			/*
1094 			 * If there is more than one control message in the
1095 			 * stream, we do a short read.  Next can be received
1096 			 * or disposed by another system call.
1097 			 */
1098 			break;
1099 #ifdef DIAGNOSTIC
1100 		} else if (m->m_type != MT_DATA && m->m_type != MT_HEADER) {
1101 			panic("receive 3: so %p, so_type %d, m %p, m_type %d",
1102 			    so, so->so_type, m, m->m_type);
1103 #endif
1104 		}
1105 		so->so_rcv.sb_state &= ~SS_RCVATMARK;
1106 		len = uio->uio_resid;
1107 		if (so->so_oobmark && len > so->so_oobmark - offset)
1108 			len = so->so_oobmark - offset;
1109 		if (len > m->m_len - moff)
1110 			len = m->m_len - moff;
1111 		/*
1112 		 * If mp is set, just pass back the mbufs.
1113 		 * Otherwise copy them out via the uio, then free.
1114 		 * Sockbuf must be consistent here (points to current mbuf,
1115 		 * it points to next record) when we drop priority;
1116 		 * we must note any additions to the sockbuf when we
1117 		 * block interrupts again.
1118 		 */
1119 		if (mp == NULL && uio_error == 0) {
1120 			SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove");
1121 			SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");
1122 			resid = uio->uio_resid;
1123 			sb_mtx_unlock(&so->so_rcv);
1124 			if (dosolock)
1125 				sounlock_shared(so);
1126 			uio_error = uiomove(mtod(m, caddr_t) + moff, len, uio);
1127 			if (dosolock)
1128 				solock_shared(so);
1129 			sb_mtx_lock(&so->so_rcv);
1130 			if (uio_error)
1131 				uio->uio_resid = resid - len;
1132 		} else
1133 			uio->uio_resid -= len;
1134 		if (len == m->m_len - moff) {
1135 			if (m->m_flags & M_EOR)
1136 				flags |= MSG_EOR;
1137 			if (flags & MSG_PEEK) {
1138 				m = m->m_next;
1139 				moff = 0;
1140 				orig_resid = 0;
1141 			} else {
1142 				nextrecord = m->m_nextpkt;
1143 				sbfree(so, &so->so_rcv, m);
1144 				if (mp) {
1145 					*mp = m;
1146 					mp = &m->m_next;
1147 					so->so_rcv.sb_mb = m = m->m_next;
1148 					*mp = NULL;
1149 				} else {
1150 					so->so_rcv.sb_mb = m_free(m);
1151 					m = so->so_rcv.sb_mb;
1152 				}
1153 				/*
1154 				 * If m != NULL, we also know that
1155 				 * so->so_rcv.sb_mb != NULL.
1156 				 */
1157 				KASSERT(so->so_rcv.sb_mb == m);
1158 				if (m) {
1159 					m->m_nextpkt = nextrecord;
1160 					if (nextrecord == NULL)
1161 						so->so_rcv.sb_lastrecord = m;
1162 				} else {
1163 					so->so_rcv.sb_mb = nextrecord;
1164 					SB_EMPTY_FIXUP(&so->so_rcv);
1165 				}
1166 				SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
1167 				SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
1168 			}
1169 		} else {
1170 			if (flags & MSG_PEEK) {
1171 				moff += len;
1172 				orig_resid = 0;
1173 			} else {
1174 				if (mp)
1175 					*mp = m_copym(m, 0, len, M_WAIT);
1176 				m->m_data += len;
1177 				m->m_len -= len;
1178 				so->so_rcv.sb_cc -= len;
1179 				so->so_rcv.sb_datacc -= len;
1180 			}
1181 		}
1182 		if (so->so_oobmark) {
1183 			if ((flags & MSG_PEEK) == 0) {
1184 				so->so_oobmark -= len;
1185 				if (so->so_oobmark == 0) {
1186 					so->so_rcv.sb_state |= SS_RCVATMARK;
1187 					break;
1188 				}
1189 			} else {
1190 				offset += len;
1191 				if (offset == so->so_oobmark)
1192 					break;
1193 			}
1194 		}
1195 		if (flags & MSG_EOR)
1196 			break;
1197 		/*
1198 		 * If the MSG_WAITALL flag is set (for non-atomic socket),
1199 		 * we must not quit until "uio->uio_resid == 0" or an error
1200 		 * termination.  If a signal/timeout occurs, return
1201 		 * with a short count but without error.
1202 		 * Keep sockbuf locked against other readers.
1203 		 */
1204 		while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1205 		    !sosendallatonce(so) && !nextrecord) {
1206 			if (so->so_rcv.sb_state & SS_CANTRCVMORE ||
1207 			    so->so_error)
1208 				break;
1209 			SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
1210 			SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
1211 			if (sbwait(so, &so->so_rcv)) {
1212 				sb_mtx_unlock(&so->so_rcv);
1213 				if (dosolock)
1214 					sounlock_shared(so);
1215 				sbunlock(&so->so_rcv);
1216 				return (0);
1217 			}
1218 			if ((m = so->so_rcv.sb_mb) != NULL)
1219 				nextrecord = m->m_nextpkt;
1220 		}
1221 	}
1222 
1223 	if (m && pr->pr_flags & PR_ATOMIC) {
1224 		flags |= MSG_TRUNC;
1225 		if ((flags & MSG_PEEK) == 0)
1226 			(void) sbdroprecord(so, &so->so_rcv);
1227 	}
1228 	if ((flags & MSG_PEEK) == 0) {
1229 		if (m == NULL) {
1230 			/*
1231 			 * First part is an inline SB_EMPTY_FIXUP().  Second
1232 			 * part makes sure sb_lastrecord is up-to-date if
1233 			 * there is still data in the socket buffer.
1234 			 */
1235 			so->so_rcv.sb_mb = nextrecord;
1236 			if (so->so_rcv.sb_mb == NULL) {
1237 				so->so_rcv.sb_mbtail = NULL;
1238 				so->so_rcv.sb_lastrecord = NULL;
1239 			} else if (nextrecord->m_nextpkt == NULL)
1240 				so->so_rcv.sb_lastrecord = nextrecord;
1241 		}
1242 		SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
1243 		SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
1244 		if (pr->pr_flags & PR_WANTRCVD) {
1245 			sb_mtx_unlock(&so->so_rcv);
1246 			if (!dosolock)
1247 				solock_shared(so);
1248 			pru_rcvd(so);
1249 			if (!dosolock)
1250 				sounlock_shared(so);
1251 			sb_mtx_lock(&so->so_rcv);
1252 		}
1253 	}
1254 	if (orig_resid == uio->uio_resid && orig_resid &&
1255 	    (flags & MSG_EOR) == 0 &&
1256 	    (so->so_rcv.sb_state & SS_CANTRCVMORE) == 0) {
1257 		sb_mtx_unlock(&so->so_rcv);
1258 		sbunlock(&so->so_rcv);
1259 		goto restart;
1260 	}
1261 
1262 	if (uio_error)
1263 		error = uio_error;
1264 
1265 	if (flagsp)
1266 		*flagsp |= flags;
1267 release:
1268 	sb_mtx_unlock(&so->so_rcv);
1269 	if (dosolock)
1270 		sounlock_shared(so);
1271 	sbunlock(&so->so_rcv);
1272 	return (error);
1273 }
1274 
1275 int
1276 soshutdown(struct socket *so, int how)
1277 {
1278 	int error = 0;
1279 
1280 	switch (how) {
1281 	case SHUT_RD:
1282 		sorflush(so);
1283 		break;
1284 	case SHUT_RDWR:
1285 		sorflush(so);
1286 		/* FALLTHROUGH */
1287 	case SHUT_WR:
1288 		solock(so);
1289 		error = pru_shutdown(so);
1290 		sounlock(so);
1291 		break;
1292 	default:
1293 		error = EINVAL;
1294 		break;
1295 	}
1296 
1297 	return (error);
1298 }
1299 
1300 void
1301 sorflush(struct socket *so)
1302 {
1303 	struct sockbuf *sb = &so->so_rcv;
1304 	struct mbuf *m;
1305 	const struct protosw *pr = so->so_proto;
1306 	int error;
1307 
1308 	error = sblock(sb, SBL_WAIT | SBL_NOINTR);
1309 	/* with SBL_WAIT and SLB_NOINTR sblock() must not fail */
1310 	KASSERT(error == 0);
1311 
1312 	solock_shared(so);
1313 	socantrcvmore(so);
1314 	mtx_enter(&sb->sb_mtx);
1315 	m = sb->sb_mb;
1316 	memset(&sb->sb_startzero, 0,
1317 	     (caddr_t)&sb->sb_endzero - (caddr_t)&sb->sb_startzero);
1318 	sb->sb_timeo_nsecs = INFSLP;
1319 	mtx_leave(&sb->sb_mtx);
1320 	sounlock_shared(so);
1321 	sbunlock(sb);
1322 
1323 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
1324 		(*pr->pr_domain->dom_dispose)(m);
1325 	m_purge(m);
1326 }
1327 
1328 #ifdef SOCKET_SPLICE
1329 
1330 #define so_splicelen	so_sp->ssp_len
1331 #define so_splicemax	so_sp->ssp_max
1332 #define so_idletv	so_sp->ssp_idletv
1333 #define so_idleto	so_sp->ssp_idleto
1334 #define so_splicetask	so_sp->ssp_task
1335 
1336 void
1337 sosplice_solock_pair(struct socket *so1, struct socket *so2)
1338 {
1339 	NET_LOCK_SHARED();
1340 
1341 	if (so1 == so2)
1342 		rw_enter_write(&so1->so_lock);
1343 	else if (so1 < so2) {
1344 		rw_enter_write(&so1->so_lock);
1345 		rw_enter_write(&so2->so_lock);
1346 	} else {
1347 		rw_enter_write(&so2->so_lock);
1348 		rw_enter_write(&so1->so_lock);
1349 	}
1350 }
1351 
1352 void
1353 sosplice_sounlock_pair(struct socket *so1, struct socket *so2)
1354 {
1355 	if (so1 == so2)
1356 		rw_exit_write(&so1->so_lock);
1357 	else if (so1 < so2) {
1358 		rw_exit_write(&so2->so_lock);
1359 		rw_exit_write(&so1->so_lock);
1360 	} else {
1361 		rw_exit_write(&so1->so_lock);
1362 		rw_exit_write(&so2->so_lock);
1363 	}
1364 
1365 	NET_UNLOCK_SHARED();
1366 }
1367 
1368 int
1369 sosplice(struct socket *so, int fd, off_t max, struct timeval *tv)
1370 {
1371 	struct file	*fp;
1372 	struct socket	*sosp;
1373 	struct taskq	*tq;
1374 	int		 error = 0;
1375 
1376 	if ((so->so_proto->pr_flags & PR_SPLICE) == 0)
1377 		return (EPROTONOSUPPORT);
1378 	if (max && max < 0)
1379 		return (EINVAL);
1380 	if (tv && (tv->tv_sec < 0 || !timerisvalid(tv)))
1381 		return (EINVAL);
1382 
1383 	/* If no fd is given, unsplice by removing existing link. */
1384 	if (fd < 0) {
1385 		if ((error = sblock(&so->so_rcv, SBL_WAIT)) != 0)
1386 			return (error);
1387 		if (so->so_sp && so->so_sp->ssp_socket) {
1388 			sosp = soref(so->so_sp->ssp_socket);
1389 			sounsplice(so, so->so_sp->ssp_socket, 0);
1390 			sorele(sosp);
1391 		} else
1392 			error = EPROTO;
1393 		sbunlock(&so->so_rcv);
1394 		return (error);
1395 	}
1396 
1397 	if (sosplice_taskq == NULL) {
1398 		rw_enter_write(&sosplice_lock);
1399 		if (sosplice_taskq == NULL) {
1400 			tq = taskq_create("sosplice", 1, IPL_SOFTNET,
1401 			    TASKQ_MPSAFE);
1402 			if (tq == NULL) {
1403 				rw_exit_write(&sosplice_lock);
1404 				return (ENOMEM);
1405 			}
1406 			/* Ensure the taskq is fully visible to other CPUs. */
1407 			membar_producer();
1408 			sosplice_taskq = tq;
1409 		}
1410 		rw_exit_write(&sosplice_lock);
1411 	} else {
1412 		/* Ensure the taskq is fully visible on this CPU. */
1413 		membar_consumer();
1414 	}
1415 
1416 	/* Find sosp, the drain socket where data will be spliced into. */
1417 	if ((error = getsock(curproc, fd, &fp)) != 0)
1418 		return (error);
1419 	sosp = fp->f_data;
1420 
1421 	if (sosp->so_proto->pr_usrreqs->pru_send !=
1422 	    so->so_proto->pr_usrreqs->pru_send) {
1423 		error = EPROTONOSUPPORT;
1424 		goto frele;
1425 	}
1426 
1427 	if ((error = sblock(&so->so_rcv, SBL_WAIT)) != 0)
1428 		goto frele;
1429 	if ((error = sblock(&sosp->so_snd, SBL_WAIT)) != 0) {
1430 		sbunlock(&so->so_rcv);
1431 		goto frele;
1432 	}
1433 	sosplice_solock_pair(so, sosp);
1434 
1435 	if ((so->so_options & SO_ACCEPTCONN) ||
1436 	    (sosp->so_options & SO_ACCEPTCONN)) {
1437 		error = EOPNOTSUPP;
1438 		goto release;
1439 	}
1440 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1441 	    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1442 		error = ENOTCONN;
1443 		goto release;
1444 	}
1445 	if ((sosp->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0) {
1446 		error = ENOTCONN;
1447 		goto release;
1448 	}
1449 	if (so->so_sp == NULL) {
1450 		struct sosplice *so_sp;
1451 
1452 		so_sp = pool_get(&sosplice_pool, PR_WAITOK | PR_ZERO);
1453 		timeout_set_flags(&so_sp->ssp_idleto, soidle, so,
1454 		    KCLOCK_NONE, TIMEOUT_PROC | TIMEOUT_MPSAFE);
1455 		task_set(&so_sp->ssp_task, sotask, so);
1456 
1457 		so->so_sp = so_sp;
1458 	}
1459 	if (sosp->so_sp == NULL) {
1460 		struct sosplice *so_sp;
1461 
1462 		so_sp = pool_get(&sosplice_pool, PR_WAITOK | PR_ZERO);
1463 		timeout_set_flags(&so_sp->ssp_idleto, soidle, sosp,
1464 		    KCLOCK_NONE, TIMEOUT_PROC | TIMEOUT_MPSAFE);
1465 		task_set(&so_sp->ssp_task, sotask, sosp);
1466 
1467 		sosp->so_sp = so_sp;
1468 	}
1469 	if (so->so_sp->ssp_socket || sosp->so_sp->ssp_soback) {
1470 		error = EBUSY;
1471 		goto release;
1472 	}
1473 
1474 	so->so_splicelen = 0;
1475 	so->so_splicemax = max;
1476 	if (tv)
1477 		so->so_idletv = *tv;
1478 	else
1479 		timerclear(&so->so_idletv);
1480 
1481 	/*
1482 	 * To prevent sorwakeup() calling somove() before this somove()
1483 	 * has finished, the socket buffers are not marked as spliced yet.
1484 	 */
1485 
1486 	/* Splice so and sosp together. */
1487 	mtx_enter(&so->so_rcv.sb_mtx);
1488 	mtx_enter(&sosp->so_snd.sb_mtx);
1489 	so->so_sp->ssp_socket = sosp;
1490 	sosp->so_sp->ssp_soback = so;
1491 	mtx_leave(&sosp->so_snd.sb_mtx);
1492 	mtx_leave(&so->so_rcv.sb_mtx);
1493 
1494 	sosplice_sounlock_pair(so, sosp);
1495 	sbunlock(&sosp->so_snd);
1496 
1497 	if (somove(so, M_WAIT)) {
1498 		mtx_enter(&so->so_rcv.sb_mtx);
1499 		mtx_enter(&sosp->so_snd.sb_mtx);
1500 		so->so_rcv.sb_flags |= SB_SPLICE;
1501 		sosp->so_snd.sb_flags |= SB_SPLICE;
1502 		mtx_leave(&sosp->so_snd.sb_mtx);
1503 		mtx_leave(&so->so_rcv.sb_mtx);
1504 	}
1505 
1506 	sbunlock(&so->so_rcv);
1507 	FRELE(fp, curproc);
1508 	return (0);
1509 
1510  release:
1511 	sosplice_sounlock_pair(so, sosp);
1512 	sbunlock(&sosp->so_snd);
1513 	sbunlock(&so->so_rcv);
1514  frele:
1515 	FRELE(fp, curproc);
1516 	return (error);
1517 }
1518 
1519 void
1520 sounsplice(struct socket *so, struct socket *sosp, int freeing)
1521 {
1522 	sbassertlocked(&so->so_rcv);
1523 
1524 	mtx_enter(&so->so_rcv.sb_mtx);
1525 	mtx_enter(&sosp->so_snd.sb_mtx);
1526 	so->so_rcv.sb_flags &= ~SB_SPLICE;
1527 	sosp->so_snd.sb_flags &= ~SB_SPLICE;
1528 	so->so_sp->ssp_socket = sosp->so_sp->ssp_soback = NULL;
1529 	mtx_leave(&sosp->so_snd.sb_mtx);
1530 	mtx_leave(&so->so_rcv.sb_mtx);
1531 
1532 	task_del(sosplice_taskq, &so->so_splicetask);
1533 	timeout_del(&so->so_idleto);
1534 
1535 	/* Do not wakeup a socket that is about to be freed. */
1536 	if ((freeing & SOSP_FREEING_READ) == 0) {
1537 		int readable;
1538 
1539 		solock_shared(so);
1540 		mtx_enter(&so->so_rcv.sb_mtx);
1541 		readable = soreadable(so);
1542 		mtx_leave(&so->so_rcv.sb_mtx);
1543 		if (readable)
1544 			sorwakeup(so);
1545 		sounlock_shared(so);
1546 	}
1547 	if ((freeing & SOSP_FREEING_WRITE) == 0) {
1548 		solock_shared(sosp);
1549 		if (sowriteable(sosp))
1550 			sowwakeup(sosp);
1551 		sounlock_shared(sosp);
1552 	}
1553 }
1554 
1555 void
1556 soidle(void *arg)
1557 {
1558 	struct socket *so = arg;
1559 
1560 	sblock(&so->so_rcv, SBL_WAIT | SBL_NOINTR);
1561 	if (so->so_rcv.sb_flags & SB_SPLICE) {
1562 		struct socket *sosp;
1563 
1564 		WRITE_ONCE(so->so_error, ETIMEDOUT);
1565 		sosp = soref(so->so_sp->ssp_socket);
1566 		sounsplice(so, so->so_sp->ssp_socket, 0);
1567 		sorele(sosp);
1568 	}
1569 	sbunlock(&so->so_rcv);
1570 }
1571 
1572 void
1573 sotask(void *arg)
1574 {
1575 	struct socket *so = arg;
1576 	int doyield = 0;
1577 
1578 	sblock(&so->so_rcv, SBL_WAIT | SBL_NOINTR);
1579 	if (so->so_rcv.sb_flags & SB_SPLICE) {
1580 		if (so->so_proto->pr_flags & PR_WANTRCVD)
1581 			doyield = 1;
1582 		somove(so, M_DONTWAIT);
1583 	}
1584 	sbunlock(&so->so_rcv);
1585 
1586 	if (doyield) {
1587 		/* Avoid user land starvation. */
1588 		yield();
1589 	}
1590 }
1591 
1592 /*
1593  * Move data from receive buffer of spliced source socket to send
1594  * buffer of drain socket.  Try to move as much as possible in one
1595  * big chunk.  It is a TCP only implementation.
1596  * Return value 0 means splicing has been finished, 1 continue.
1597  */
1598 int
1599 somove(struct socket *so, int wait)
1600 {
1601 	struct socket	*sosp = so->so_sp->ssp_socket;
1602 	struct mbuf	*m, **mp, *nextrecord;
1603 	u_long		 len, off, oobmark;
1604 	long		 space;
1605 	int		 error = 0, maxreached = 0, unsplice = 0;
1606 	unsigned int	 rcvstate;
1607 
1608 	sbassertlocked(&so->so_rcv);
1609 
1610 	if (so->so_proto->pr_flags & PR_WANTRCVD)
1611 		sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1612 
1613 	mtx_enter(&so->so_rcv.sb_mtx);
1614 	mtx_enter(&sosp->so_snd.sb_mtx);
1615 
1616  nextpkt:
1617 	if ((error = READ_ONCE(so->so_error)))
1618 		goto release;
1619 	if (sosp->so_snd.sb_state & SS_CANTSENDMORE) {
1620 		error = EPIPE;
1621 		goto release;
1622 	}
1623 
1624 	error = READ_ONCE(sosp->so_error);
1625 	if (error) {
1626 		if (error != ETIMEDOUT && error != EFBIG && error != ELOOP)
1627 			goto release;
1628 		error = 0;
1629 	}
1630 	if ((sosp->so_state & SS_ISCONNECTED) == 0)
1631 		goto release;
1632 
1633 	/* Calculate how many bytes can be copied now. */
1634 	len = so->so_rcv.sb_datacc;
1635 	if (so->so_splicemax) {
1636 		KASSERT(so->so_splicelen < so->so_splicemax);
1637 		if (so->so_splicemax <= so->so_splicelen + len) {
1638 			len = so->so_splicemax - so->so_splicelen;
1639 			maxreached = 1;
1640 		}
1641 	}
1642 	space = sbspace_locked(sosp, &sosp->so_snd);
1643 	if (so->so_oobmark && so->so_oobmark < len &&
1644 	    so->so_oobmark < space + 1024)
1645 		space += 1024;
1646 	if (space <= 0) {
1647 		maxreached = 0;
1648 		goto release;
1649 	}
1650 	if (space < len) {
1651 		maxreached = 0;
1652 		if (space < sosp->so_snd.sb_lowat)
1653 			goto release;
1654 		len = space;
1655 	}
1656 	sosp->so_snd.sb_state |= SS_ISSENDING;
1657 
1658 	SBLASTRECORDCHK(&so->so_rcv, "somove 1");
1659 	SBLASTMBUFCHK(&so->so_rcv, "somove 1");
1660 	m = so->so_rcv.sb_mb;
1661 	if (m == NULL)
1662 		goto release;
1663 	nextrecord = m->m_nextpkt;
1664 
1665 	/* Drop address and control information not used with splicing. */
1666 	if (so->so_proto->pr_flags & PR_ADDR) {
1667 #ifdef DIAGNOSTIC
1668 		if (m->m_type != MT_SONAME)
1669 			panic("somove soname: so %p, so_type %d, m %p, "
1670 			    "m_type %d", so, so->so_type, m, m->m_type);
1671 #endif
1672 		m = m->m_next;
1673 	}
1674 	while (m && m->m_type == MT_CONTROL)
1675 		m = m->m_next;
1676 	if (m == NULL) {
1677 		sbdroprecord(so, &so->so_rcv);
1678 		if (so->so_proto->pr_flags & PR_WANTRCVD) {
1679 			mtx_leave(&sosp->so_snd.sb_mtx);
1680 			mtx_leave(&so->so_rcv.sb_mtx);
1681 			solock_shared(so);
1682 			pru_rcvd(so);
1683 			sounlock_shared(so);
1684 			mtx_enter(&so->so_rcv.sb_mtx);
1685 			mtx_enter(&sosp->so_snd.sb_mtx);
1686 		}
1687 		goto nextpkt;
1688 	}
1689 
1690 	/*
1691 	 * By splicing sockets connected to localhost, userland might create a
1692 	 * loop.  Dissolve splicing with error if loop is detected by counter.
1693 	 *
1694 	 * If we deal with looped broadcast/multicast packet we bail out with
1695 	 * no error to suppress splice termination.
1696 	 */
1697 	if ((m->m_flags & M_PKTHDR) &&
1698 	    ((m->m_pkthdr.ph_loopcnt++ >= M_MAXLOOP) ||
1699 	    ((m->m_flags & M_LOOP) && (m->m_flags & (M_BCAST|M_MCAST))))) {
1700 		error = ELOOP;
1701 		goto release;
1702 	}
1703 
1704 	if (so->so_proto->pr_flags & PR_ATOMIC) {
1705 		if ((m->m_flags & M_PKTHDR) == 0)
1706 			panic("somove !PKTHDR: so %p, so_type %d, m %p, "
1707 			    "m_type %d", so, so->so_type, m, m->m_type);
1708 		if (sosp->so_snd.sb_hiwat < m->m_pkthdr.len) {
1709 			error = EMSGSIZE;
1710 			goto release;
1711 		}
1712 		if (len < m->m_pkthdr.len)
1713 			goto release;
1714 		if (m->m_pkthdr.len < len) {
1715 			maxreached = 0;
1716 			len = m->m_pkthdr.len;
1717 		}
1718 		/*
1719 		 * Throw away the name mbuf after it has been assured
1720 		 * that the whole first record can be processed.
1721 		 */
1722 		m = so->so_rcv.sb_mb;
1723 		sbfree(so, &so->so_rcv, m);
1724 		so->so_rcv.sb_mb = m_free(m);
1725 		sbsync(&so->so_rcv, nextrecord);
1726 	}
1727 	/*
1728 	 * Throw away the control mbufs after it has been assured
1729 	 * that the whole first record can be processed.
1730 	 */
1731 	m = so->so_rcv.sb_mb;
1732 	while (m && m->m_type == MT_CONTROL) {
1733 		sbfree(so, &so->so_rcv, m);
1734 		so->so_rcv.sb_mb = m_free(m);
1735 		m = so->so_rcv.sb_mb;
1736 		sbsync(&so->so_rcv, nextrecord);
1737 	}
1738 
1739 	SBLASTRECORDCHK(&so->so_rcv, "somove 2");
1740 	SBLASTMBUFCHK(&so->so_rcv, "somove 2");
1741 
1742 	/* Take at most len mbufs out of receive buffer. */
1743 	for (off = 0, mp = &m; off <= len && *mp;
1744 	    off += (*mp)->m_len, mp = &(*mp)->m_next) {
1745 		u_long size = len - off;
1746 
1747 #ifdef DIAGNOSTIC
1748 		if ((*mp)->m_type != MT_DATA && (*mp)->m_type != MT_HEADER)
1749 			panic("somove type: so %p, so_type %d, m %p, "
1750 			    "m_type %d", so, so->so_type, *mp, (*mp)->m_type);
1751 #endif
1752 		if ((*mp)->m_len > size) {
1753 			/*
1754 			 * Move only a partial mbuf at maximum splice length or
1755 			 * if the drain buffer is too small for this large mbuf.
1756 			 */
1757 			if (!maxreached && sosp->so_snd.sb_datacc > 0) {
1758 				len -= size;
1759 				break;
1760 			}
1761 			*mp = m_copym(so->so_rcv.sb_mb, 0, size, wait);
1762 			if (*mp == NULL) {
1763 				len -= size;
1764 				break;
1765 			}
1766 			so->so_rcv.sb_mb->m_data += size;
1767 			so->so_rcv.sb_mb->m_len -= size;
1768 			so->so_rcv.sb_cc -= size;
1769 			so->so_rcv.sb_datacc -= size;
1770 		} else {
1771 			*mp = so->so_rcv.sb_mb;
1772 			sbfree(so, &so->so_rcv, *mp);
1773 			so->so_rcv.sb_mb = (*mp)->m_next;
1774 			sbsync(&so->so_rcv, nextrecord);
1775 		}
1776 	}
1777 	*mp = NULL;
1778 
1779 	SBLASTRECORDCHK(&so->so_rcv, "somove 3");
1780 	SBLASTMBUFCHK(&so->so_rcv, "somove 3");
1781 	SBCHECK(so, &so->so_rcv);
1782 	if (m == NULL)
1783 		goto release;
1784 	m->m_nextpkt = NULL;
1785 	if (m->m_flags & M_PKTHDR) {
1786 		m_resethdr(m);
1787 		m->m_pkthdr.len = len;
1788 	}
1789 
1790 	/* Send window update to source peer as receive buffer has changed. */
1791 	if (so->so_proto->pr_flags & PR_WANTRCVD) {
1792 		mtx_leave(&sosp->so_snd.sb_mtx);
1793 		mtx_leave(&so->so_rcv.sb_mtx);
1794 		solock_shared(so);
1795 		pru_rcvd(so);
1796 		sounlock_shared(so);
1797 		mtx_enter(&so->so_rcv.sb_mtx);
1798 		mtx_enter(&sosp->so_snd.sb_mtx);
1799 	}
1800 
1801 	/* Receive buffer did shrink by len bytes, adjust oob. */
1802 	rcvstate = so->so_rcv.sb_state;
1803 	so->so_rcv.sb_state &= ~SS_RCVATMARK;
1804 	oobmark = so->so_oobmark;
1805 	so->so_oobmark = oobmark > len ? oobmark - len : 0;
1806 	if (oobmark) {
1807 		if (oobmark == len)
1808 			so->so_rcv.sb_state |= SS_RCVATMARK;
1809 		if (oobmark >= len)
1810 			oobmark = 0;
1811 	}
1812 
1813 	/*
1814 	 * Handle oob data.  If any malloc fails, ignore error.
1815 	 * TCP urgent data is not very reliable anyway.
1816 	 */
1817 	while (((rcvstate & SS_RCVATMARK) || oobmark) &&
1818 	    (so->so_options & SO_OOBINLINE)) {
1819 		struct mbuf *o = NULL;
1820 
1821 		if (rcvstate & SS_RCVATMARK) {
1822 			o = m_get(wait, MT_DATA);
1823 			rcvstate &= ~SS_RCVATMARK;
1824 		} else if (oobmark) {
1825 			o = m_split(m, oobmark, wait);
1826 			if (o) {
1827 				mtx_leave(&sosp->so_snd.sb_mtx);
1828 				mtx_leave(&so->so_rcv.sb_mtx);
1829 				solock_shared(sosp);
1830 				error = pru_send(sosp, m, NULL, NULL);
1831 				sounlock_shared(sosp);
1832 				mtx_enter(&so->so_rcv.sb_mtx);
1833 				mtx_enter(&sosp->so_snd.sb_mtx);
1834 
1835 				if (error) {
1836 					if (sosp->so_snd.sb_state &
1837 					    SS_CANTSENDMORE)
1838 						error = EPIPE;
1839 					m_freem(o);
1840 					goto release;
1841 				}
1842 				len -= oobmark;
1843 				so->so_splicelen += oobmark;
1844 				m = o;
1845 				o = m_get(wait, MT_DATA);
1846 			}
1847 			oobmark = 0;
1848 		}
1849 		if (o) {
1850 			o->m_len = 1;
1851 			*mtod(o, caddr_t) = *mtod(m, caddr_t);
1852 
1853 			mtx_leave(&sosp->so_snd.sb_mtx);
1854 			mtx_leave(&so->so_rcv.sb_mtx);
1855 			solock_shared(sosp);
1856 			error = pru_sendoob(sosp, o, NULL, NULL);
1857 			sounlock_shared(sosp);
1858 			mtx_enter(&so->so_rcv.sb_mtx);
1859 			mtx_enter(&sosp->so_snd.sb_mtx);
1860 
1861 			if (error) {
1862 				if (sosp->so_snd.sb_state & SS_CANTSENDMORE)
1863 					error = EPIPE;
1864 				m_freem(m);
1865 				goto release;
1866 			}
1867 			len -= 1;
1868 			so->so_splicelen += 1;
1869 			if (oobmark) {
1870 				oobmark -= 1;
1871 				if (oobmark == 0)
1872 					rcvstate |= SS_RCVATMARK;
1873 			}
1874 			m_adj(m, 1);
1875 		}
1876 	}
1877 
1878 	/* Append all remaining data to drain socket. */
1879 	if (so->so_rcv.sb_cc == 0 || maxreached)
1880 		sosp->so_snd.sb_state &= ~SS_ISSENDING;
1881 
1882 	mtx_leave(&sosp->so_snd.sb_mtx);
1883 	mtx_leave(&so->so_rcv.sb_mtx);
1884 	solock_shared(sosp);
1885 	error = pru_send(sosp, m, NULL, NULL);
1886 	sounlock_shared(sosp);
1887 	mtx_enter(&so->so_rcv.sb_mtx);
1888 	mtx_enter(&sosp->so_snd.sb_mtx);
1889 
1890 	if (error) {
1891 		if (sosp->so_snd.sb_state & SS_CANTSENDMORE ||
1892 		    sosp->so_pcb == NULL)
1893 			error = EPIPE;
1894 		goto release;
1895 	}
1896 	so->so_splicelen += len;
1897 
1898 	/* Move several packets if possible. */
1899 	if (!maxreached && nextrecord)
1900 		goto nextpkt;
1901 
1902  release:
1903 	sosp->so_snd.sb_state &= ~SS_ISSENDING;
1904 
1905 	if (!error && maxreached && so->so_splicemax == so->so_splicelen)
1906 		error = EFBIG;
1907 	if (error)
1908 		WRITE_ONCE(so->so_error, error);
1909 
1910 	if (((so->so_rcv.sb_state & SS_CANTRCVMORE) &&
1911 	    so->so_rcv.sb_cc == 0) ||
1912 	    (sosp->so_snd.sb_state & SS_CANTSENDMORE) ||
1913 	    maxreached || error)
1914 		unsplice = 1;
1915 
1916 	mtx_leave(&sosp->so_snd.sb_mtx);
1917 	mtx_leave(&so->so_rcv.sb_mtx);
1918 
1919 	if (so->so_proto->pr_flags & PR_WANTRCVD)
1920 		sbunlock(&so->so_snd);
1921 
1922 	if (unsplice) {
1923 		soref(sosp);
1924 		sounsplice(so, sosp, 0);
1925 		sorele(sosp);
1926 
1927 		return (0);
1928 	}
1929 	if (timerisset(&so->so_idletv))
1930 		timeout_add_tv(&so->so_idleto, &so->so_idletv);
1931 	return (1);
1932 }
1933 #endif /* SOCKET_SPLICE */
1934 
1935 void
1936 sorwakeup(struct socket *so)
1937 {
1938 	if ((so->so_rcv.sb_flags & SB_MTXLOCK) == 0)
1939 		soassertlocked_readonly(so);
1940 
1941 #ifdef SOCKET_SPLICE
1942 	if (so->so_proto->pr_flags & PR_SPLICE) {
1943 		sb_mtx_lock(&so->so_rcv);
1944 		if (so->so_rcv.sb_flags & SB_SPLICE)
1945 			task_add(sosplice_taskq, &so->so_splicetask);
1946 		if (isspliced(so)) {
1947 			sb_mtx_unlock(&so->so_rcv);
1948 			return;
1949 		}
1950 		sb_mtx_unlock(&so->so_rcv);
1951 	}
1952 #endif
1953 	sowakeup(so, &so->so_rcv);
1954 	if (so->so_upcall)
1955 		(*(so->so_upcall))(so, so->so_upcallarg, M_DONTWAIT);
1956 }
1957 
1958 void
1959 sowwakeup(struct socket *so)
1960 {
1961 	if ((so->so_snd.sb_flags & SB_MTXLOCK) == 0)
1962 		soassertlocked_readonly(so);
1963 
1964 #ifdef SOCKET_SPLICE
1965 	if (so->so_proto->pr_flags & PR_SPLICE) {
1966 		sb_mtx_lock(&so->so_snd);
1967 		if (so->so_snd.sb_flags & SB_SPLICE)
1968 			task_add(sosplice_taskq,
1969 			    &so->so_sp->ssp_soback->so_splicetask);
1970 		if (issplicedback(so)) {
1971 			sb_mtx_unlock(&so->so_snd);
1972 			return;
1973 		}
1974 		sb_mtx_unlock(&so->so_snd);
1975 	}
1976 #endif
1977 	sowakeup(so, &so->so_snd);
1978 }
1979 
1980 int
1981 sosetopt(struct socket *so, int level, int optname, struct mbuf *m)
1982 {
1983 	int error = 0;
1984 
1985 	if (level != SOL_SOCKET) {
1986 		if (so->so_proto->pr_ctloutput) {
1987 			solock(so);
1988 			error = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so,
1989 			    level, optname, m);
1990 			sounlock(so);
1991 			return (error);
1992 		}
1993 		error = ENOPROTOOPT;
1994 	} else {
1995 		switch (optname) {
1996 
1997 		case SO_LINGER:
1998 			if (m == NULL || m->m_len != sizeof (struct linger) ||
1999 			    mtod(m, struct linger *)->l_linger < 0 ||
2000 			    mtod(m, struct linger *)->l_linger > SHRT_MAX)
2001 				return (EINVAL);
2002 
2003 			solock(so);
2004 			so->so_linger = mtod(m, struct linger *)->l_linger;
2005 			if (*mtod(m, int *))
2006 				so->so_options |= optname;
2007 			else
2008 				so->so_options &= ~optname;
2009 			sounlock(so);
2010 
2011 			break;
2012 		case SO_BINDANY:
2013 			if ((error = suser(curproc)) != 0)	/* XXX */
2014 				return (error);
2015 			/* FALLTHROUGH */
2016 
2017 		case SO_DEBUG:
2018 		case SO_KEEPALIVE:
2019 		case SO_USELOOPBACK:
2020 		case SO_BROADCAST:
2021 		case SO_REUSEADDR:
2022 		case SO_REUSEPORT:
2023 		case SO_OOBINLINE:
2024 		case SO_TIMESTAMP:
2025 		case SO_ZEROIZE:
2026 			if (m == NULL || m->m_len < sizeof (int))
2027 				return (EINVAL);
2028 
2029 			solock(so);
2030 			if (*mtod(m, int *))
2031 				so->so_options |= optname;
2032 			else
2033 				so->so_options &= ~optname;
2034 			sounlock(so);
2035 
2036 			break;
2037 		case SO_DONTROUTE:
2038 			if (m == NULL || m->m_len < sizeof (int))
2039 				return (EINVAL);
2040 			if (*mtod(m, int *))
2041 				error = EOPNOTSUPP;
2042 			break;
2043 
2044 		case SO_SNDBUF:
2045 		case SO_RCVBUF:
2046 		case SO_SNDLOWAT:
2047 		case SO_RCVLOWAT:
2048 		    {
2049 			struct sockbuf *sb = (optname == SO_SNDBUF ||
2050 			    optname == SO_SNDLOWAT ?
2051 			    &so->so_snd : &so->so_rcv);
2052 			u_long cnt;
2053 
2054 			if (m == NULL || m->m_len < sizeof (int))
2055 				return (EINVAL);
2056 			cnt = *mtod(m, int *);
2057 			if ((long)cnt <= 0)
2058 				cnt = 1;
2059 
2060 			if (((sb->sb_flags & SB_MTXLOCK) == 0))
2061 				solock(so);
2062 			mtx_enter(&sb->sb_mtx);
2063 
2064 			switch (optname) {
2065 			case SO_SNDBUF:
2066 			case SO_RCVBUF:
2067 				if (sb->sb_state &
2068 				    (SS_CANTSENDMORE | SS_CANTRCVMORE)) {
2069 					error = EINVAL;
2070 					break;
2071 				}
2072 				if (sbcheckreserve(cnt, sb->sb_wat) ||
2073 				    sbreserve(so, sb, cnt)) {
2074 					error = ENOBUFS;
2075 					break;
2076 				}
2077 				sb->sb_wat = cnt;
2078 				break;
2079 			case SO_SNDLOWAT:
2080 			case SO_RCVLOWAT:
2081 				sb->sb_lowat = (cnt > sb->sb_hiwat) ?
2082 				    sb->sb_hiwat : cnt;
2083 				break;
2084 			}
2085 
2086 			mtx_leave(&sb->sb_mtx);
2087 			if (((sb->sb_flags & SB_MTXLOCK) == 0))
2088 				sounlock(so);
2089 
2090 			break;
2091 		    }
2092 
2093 		case SO_SNDTIMEO:
2094 		case SO_RCVTIMEO:
2095 		    {
2096 			struct sockbuf *sb = (optname == SO_SNDTIMEO ?
2097 			    &so->so_snd : &so->so_rcv);
2098 			struct timeval tv;
2099 			uint64_t nsecs;
2100 
2101 			if (m == NULL || m->m_len < sizeof (tv))
2102 				return (EINVAL);
2103 			memcpy(&tv, mtod(m, struct timeval *), sizeof tv);
2104 			if (!timerisvalid(&tv))
2105 				return (EINVAL);
2106 			nsecs = TIMEVAL_TO_NSEC(&tv);
2107 			if (nsecs == UINT64_MAX)
2108 				return (EDOM);
2109 			if (nsecs == 0)
2110 				nsecs = INFSLP;
2111 
2112 			mtx_enter(&sb->sb_mtx);
2113 			sb->sb_timeo_nsecs = nsecs;
2114 			mtx_leave(&sb->sb_mtx);
2115 			break;
2116 		    }
2117 
2118 		case SO_RTABLE:
2119 			if (so->so_proto->pr_domain &&
2120 			    so->so_proto->pr_domain->dom_protosw &&
2121 			    so->so_proto->pr_ctloutput) {
2122 				const struct domain *dom =
2123 				    so->so_proto->pr_domain;
2124 
2125 				level = dom->dom_protosw->pr_protocol;
2126 				solock(so);
2127 				error = (*so->so_proto->pr_ctloutput)
2128 				    (PRCO_SETOPT, so, level, optname, m);
2129 				sounlock(so);
2130 			} else
2131 				error = ENOPROTOOPT;
2132 			break;
2133 #ifdef SOCKET_SPLICE
2134 		case SO_SPLICE:
2135 			if (m == NULL) {
2136 				error = sosplice(so, -1, 0, NULL);
2137 			} else if (m->m_len < sizeof(int)) {
2138 				error = EINVAL;
2139 			} else if (m->m_len < sizeof(struct splice)) {
2140 				error = sosplice(so, *mtod(m, int *), 0, NULL);
2141 			} else {
2142 				error = sosplice(so,
2143 				    mtod(m, struct splice *)->sp_fd,
2144 				    mtod(m, struct splice *)->sp_max,
2145 				   &mtod(m, struct splice *)->sp_idle);
2146 			}
2147 			break;
2148 #endif /* SOCKET_SPLICE */
2149 
2150 		default:
2151 			error = ENOPROTOOPT;
2152 			break;
2153 		}
2154 	}
2155 
2156 	return (error);
2157 }
2158 
2159 int
2160 sogetopt(struct socket *so, int level, int optname, struct mbuf *m)
2161 {
2162 	int error = 0;
2163 
2164 	if (level != SOL_SOCKET) {
2165 		if (so->so_proto->pr_ctloutput) {
2166 			m->m_len = 0;
2167 
2168 			solock(so);
2169 			error = (*so->so_proto->pr_ctloutput)(PRCO_GETOPT, so,
2170 			    level, optname, m);
2171 			sounlock(so);
2172 			return (error);
2173 		} else
2174 			return (ENOPROTOOPT);
2175 	} else {
2176 		m->m_len = sizeof (int);
2177 
2178 		switch (optname) {
2179 
2180 		case SO_LINGER:
2181 			m->m_len = sizeof (struct linger);
2182 			solock_shared(so);
2183 			mtod(m, struct linger *)->l_onoff =
2184 				so->so_options & SO_LINGER;
2185 			mtod(m, struct linger *)->l_linger = so->so_linger;
2186 			sounlock_shared(so);
2187 			break;
2188 
2189 		case SO_BINDANY:
2190 		case SO_USELOOPBACK:
2191 		case SO_DEBUG:
2192 		case SO_KEEPALIVE:
2193 		case SO_REUSEADDR:
2194 		case SO_REUSEPORT:
2195 		case SO_BROADCAST:
2196 		case SO_OOBINLINE:
2197 		case SO_ACCEPTCONN:
2198 		case SO_TIMESTAMP:
2199 		case SO_ZEROIZE:
2200 			*mtod(m, int *) = so->so_options & optname;
2201 			break;
2202 
2203 		case SO_DONTROUTE:
2204 			*mtod(m, int *) = 0;
2205 			break;
2206 
2207 		case SO_TYPE:
2208 			*mtod(m, int *) = so->so_type;
2209 			break;
2210 
2211 		case SO_ERROR:
2212 			solock(so);
2213 			*mtod(m, int *) = so->so_error;
2214 			so->so_error = 0;
2215 			sounlock(so);
2216 
2217 			break;
2218 
2219 		case SO_DOMAIN:
2220 			*mtod(m, int *) = so->so_proto->pr_domain->dom_family;
2221 			break;
2222 
2223 		case SO_PROTOCOL:
2224 			*mtod(m, int *) = so->so_proto->pr_protocol;
2225 			break;
2226 
2227 		case SO_SNDBUF:
2228 			*mtod(m, int *) = so->so_snd.sb_hiwat;
2229 			break;
2230 
2231 		case SO_RCVBUF:
2232 			*mtod(m, int *) = so->so_rcv.sb_hiwat;
2233 			break;
2234 
2235 		case SO_SNDLOWAT:
2236 			*mtod(m, int *) = so->so_snd.sb_lowat;
2237 			break;
2238 
2239 		case SO_RCVLOWAT:
2240 			*mtod(m, int *) = so->so_rcv.sb_lowat;
2241 			break;
2242 
2243 		case SO_SNDTIMEO:
2244 		case SO_RCVTIMEO:
2245 		    {
2246 			struct sockbuf *sb = (optname == SO_SNDTIMEO ?
2247 			    &so->so_snd : &so->so_rcv);
2248 			struct timeval tv;
2249 			uint64_t nsecs;
2250 
2251 			mtx_enter(&sb->sb_mtx);
2252 			nsecs = sb->sb_timeo_nsecs;
2253 			mtx_leave(&sb->sb_mtx);
2254 
2255 			m->m_len = sizeof(struct timeval);
2256 			memset(&tv, 0, sizeof(tv));
2257 			if (nsecs != INFSLP)
2258 				NSEC_TO_TIMEVAL(nsecs, &tv);
2259 			memcpy(mtod(m, struct timeval *), &tv, sizeof tv);
2260 			break;
2261 		    }
2262 
2263 		case SO_RTABLE:
2264 			if (so->so_proto->pr_domain &&
2265 			    so->so_proto->pr_domain->dom_protosw &&
2266 			    so->so_proto->pr_ctloutput) {
2267 				const struct domain *dom =
2268 				    so->so_proto->pr_domain;
2269 
2270 				level = dom->dom_protosw->pr_protocol;
2271 				solock(so);
2272 				error = (*so->so_proto->pr_ctloutput)
2273 				    (PRCO_GETOPT, so, level, optname, m);
2274 				sounlock(so);
2275 				if (error)
2276 					return (error);
2277 				break;
2278 			}
2279 			return (ENOPROTOOPT);
2280 
2281 #ifdef SOCKET_SPLICE
2282 		case SO_SPLICE:
2283 		    {
2284 			off_t len;
2285 
2286 			m->m_len = sizeof(off_t);
2287 			solock_shared(so);
2288 			len = so->so_sp ? so->so_sp->ssp_len : 0;
2289 			sounlock_shared(so);
2290 			memcpy(mtod(m, off_t *), &len, sizeof(off_t));
2291 			break;
2292 		    }
2293 #endif /* SOCKET_SPLICE */
2294 
2295 		case SO_PEERCRED:
2296 			if (so->so_proto->pr_protocol == AF_UNIX) {
2297 				struct unpcb *unp = sotounpcb(so);
2298 
2299 				solock(so);
2300 				if (unp->unp_flags & UNP_FEIDS) {
2301 					m->m_len = sizeof(unp->unp_connid);
2302 					memcpy(mtod(m, caddr_t),
2303 					    &(unp->unp_connid), m->m_len);
2304 					sounlock(so);
2305 					break;
2306 				}
2307 				sounlock(so);
2308 
2309 				return (ENOTCONN);
2310 			}
2311 			return (EOPNOTSUPP);
2312 
2313 		default:
2314 			return (ENOPROTOOPT);
2315 		}
2316 		return (0);
2317 	}
2318 }
2319 
2320 void
2321 sohasoutofband(struct socket *so)
2322 {
2323 	pgsigio(&so->so_sigio, SIGURG, 0);
2324 	knote(&so->so_rcv.sb_klist, 0);
2325 }
2326 
2327 void
2328 sofilt_lock(struct socket *so, struct sockbuf *sb)
2329 {
2330 	switch (so->so_proto->pr_domain->dom_family) {
2331 	case PF_INET:
2332 	case PF_INET6:
2333 		NET_LOCK_SHARED();
2334 		break;
2335 	default:
2336 		rw_enter_write(&so->so_lock);
2337 		break;
2338 	}
2339 
2340 	mtx_enter(&sb->sb_mtx);
2341 }
2342 
2343 void
2344 sofilt_unlock(struct socket *so, struct sockbuf *sb)
2345 {
2346 	mtx_leave(&sb->sb_mtx);
2347 
2348 	switch (so->so_proto->pr_domain->dom_family) {
2349 	case PF_INET:
2350 	case PF_INET6:
2351 		NET_UNLOCK_SHARED();
2352 		break;
2353 	default:
2354 		rw_exit_write(&so->so_lock);
2355 		break;
2356 	}
2357 }
2358 
2359 int
2360 soo_kqfilter(struct file *fp, struct knote *kn)
2361 {
2362 	struct socket *so = kn->kn_fp->f_data;
2363 	struct sockbuf *sb;
2364 
2365 	switch (kn->kn_filter) {
2366 	case EVFILT_READ:
2367 		kn->kn_fop = &soread_filtops;
2368 		sb = &so->so_rcv;
2369 		break;
2370 	case EVFILT_WRITE:
2371 		kn->kn_fop = &sowrite_filtops;
2372 		sb = &so->so_snd;
2373 		break;
2374 	case EVFILT_EXCEPT:
2375 		kn->kn_fop = &soexcept_filtops;
2376 		sb = &so->so_rcv;
2377 		break;
2378 	default:
2379 		return (EINVAL);
2380 	}
2381 
2382 	klist_insert(&sb->sb_klist, kn);
2383 
2384 	return (0);
2385 }
2386 
2387 void
2388 filt_sordetach(struct knote *kn)
2389 {
2390 	struct socket *so = kn->kn_fp->f_data;
2391 
2392 	klist_remove(&so->so_rcv.sb_klist, kn);
2393 }
2394 
2395 int
2396 filt_soread(struct knote *kn, long hint)
2397 {
2398 	struct socket *so = kn->kn_fp->f_data;
2399 	u_int state = READ_ONCE(so->so_state);
2400 	u_int error = READ_ONCE(so->so_error);
2401 	int rv = 0;
2402 
2403 	MUTEX_ASSERT_LOCKED(&so->so_rcv.sb_mtx);
2404 	if ((so->so_rcv.sb_flags & SB_MTXLOCK) == 0)
2405 		soassertlocked_readonly(so);
2406 
2407 	if (so->so_options & SO_ACCEPTCONN) {
2408 		short qlen = READ_ONCE(so->so_qlen);
2409 
2410 		if (so->so_rcv.sb_flags & SB_MTXLOCK)
2411 			soassertlocked_readonly(so);
2412 
2413 		kn->kn_data = qlen;
2414 		rv = (kn->kn_data != 0);
2415 
2416 		if (kn->kn_flags & (__EV_POLL | __EV_SELECT)) {
2417 			if (state & SS_ISDISCONNECTED) {
2418 				kn->kn_flags |= __EV_HUP;
2419 				rv = 1;
2420 			} else {
2421 				rv = qlen || soreadable(so);
2422 			}
2423 		}
2424 
2425 		return rv;
2426 	}
2427 
2428 	kn->kn_data = so->so_rcv.sb_cc;
2429 #ifdef SOCKET_SPLICE
2430 	if (isspliced(so)) {
2431 		rv = 0;
2432 	} else
2433 #endif /* SOCKET_SPLICE */
2434 	if (so->so_rcv.sb_state & SS_CANTRCVMORE) {
2435 		kn->kn_flags |= EV_EOF;
2436 		if (kn->kn_flags & __EV_POLL) {
2437 			if (state & SS_ISDISCONNECTED)
2438 				kn->kn_flags |= __EV_HUP;
2439 		}
2440 		kn->kn_fflags = error;
2441 		rv = 1;
2442 	} else if (error) {
2443 		rv = 1;
2444 	} else if (kn->kn_sfflags & NOTE_LOWAT) {
2445 		rv = (kn->kn_data >= kn->kn_sdata);
2446 	} else {
2447 		rv = (kn->kn_data >= so->so_rcv.sb_lowat);
2448 	}
2449 
2450 	return rv;
2451 }
2452 
2453 void
2454 filt_sowdetach(struct knote *kn)
2455 {
2456 	struct socket *so = kn->kn_fp->f_data;
2457 
2458 	klist_remove(&so->so_snd.sb_klist, kn);
2459 }
2460 
2461 int
2462 filt_sowrite(struct knote *kn, long hint)
2463 {
2464 	struct socket *so = kn->kn_fp->f_data;
2465 	u_int state = READ_ONCE(so->so_state);
2466 	u_int error = READ_ONCE(so->so_error);
2467 	int rv;
2468 
2469 	MUTEX_ASSERT_LOCKED(&so->so_snd.sb_mtx);
2470 	if ((so->so_snd.sb_flags & SB_MTXLOCK) == 0)
2471 		soassertlocked_readonly(so);
2472 
2473 	kn->kn_data = sbspace_locked(so, &so->so_snd);
2474 	if (so->so_snd.sb_state & SS_CANTSENDMORE) {
2475 		kn->kn_flags |= EV_EOF;
2476 		if (kn->kn_flags & __EV_POLL) {
2477 			if (state & SS_ISDISCONNECTED)
2478 				kn->kn_flags |= __EV_HUP;
2479 		}
2480 		kn->kn_fflags = error;
2481 		rv = 1;
2482 	} else if (error) {
2483 		rv = 1;
2484 	} else if (((state & SS_ISCONNECTED) == 0) &&
2485 	    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
2486 		rv = 0;
2487 	} else if (kn->kn_sfflags & NOTE_LOWAT) {
2488 		rv = (kn->kn_data >= kn->kn_sdata);
2489 	} else {
2490 		rv = (kn->kn_data >= so->so_snd.sb_lowat);
2491 	}
2492 
2493 	return (rv);
2494 }
2495 
2496 int
2497 filt_soexcept(struct knote *kn, long hint)
2498 {
2499 	struct socket *so = kn->kn_fp->f_data;
2500 	int rv = 0;
2501 
2502 	MUTEX_ASSERT_LOCKED(&so->so_rcv.sb_mtx);
2503 	if ((so->so_rcv.sb_flags & SB_MTXLOCK) == 0)
2504 		soassertlocked_readonly(so);
2505 
2506 #ifdef SOCKET_SPLICE
2507 	if (isspliced(so)) {
2508 		rv = 0;
2509 	} else
2510 #endif /* SOCKET_SPLICE */
2511 	if (kn->kn_sfflags & NOTE_OOB) {
2512 		if (so->so_oobmark || (so->so_rcv.sb_state & SS_RCVATMARK)) {
2513 			kn->kn_fflags |= NOTE_OOB;
2514 			kn->kn_data -= so->so_oobmark;
2515 			rv = 1;
2516 		}
2517 	}
2518 
2519 	if (kn->kn_flags & __EV_POLL) {
2520 		u_int state = READ_ONCE(so->so_state);
2521 
2522 		if (state & SS_ISDISCONNECTED) {
2523 			kn->kn_flags |= __EV_HUP;
2524 			rv = 1;
2525 		}
2526 	}
2527 
2528 	return rv;
2529 }
2530 
2531 int
2532 filt_sowmodify(struct kevent *kev, struct knote *kn)
2533 {
2534 	struct socket *so = kn->kn_fp->f_data;
2535 	int rv;
2536 
2537 	sofilt_lock(so, &so->so_snd);
2538 	rv = knote_modify(kev, kn);
2539 	sofilt_unlock(so, &so->so_snd);
2540 
2541 	return (rv);
2542 }
2543 
2544 int
2545 filt_sowprocess(struct knote *kn, struct kevent *kev)
2546 {
2547 	struct socket *so = kn->kn_fp->f_data;
2548 	int rv;
2549 
2550 	sofilt_lock(so, &so->so_snd);
2551 	rv = knote_process(kn, kev);
2552 	sofilt_unlock(so, &so->so_snd);
2553 
2554 	return (rv);
2555 }
2556 
2557 int
2558 filt_sormodify(struct kevent *kev, struct knote *kn)
2559 {
2560 	struct socket *so = kn->kn_fp->f_data;
2561 	int rv;
2562 
2563 	sofilt_lock(so, &so->so_rcv);
2564 	rv = knote_modify(kev, kn);
2565 	sofilt_unlock(so, &so->so_rcv);
2566 
2567 	return (rv);
2568 }
2569 
2570 int
2571 filt_sorprocess(struct knote *kn, struct kevent *kev)
2572 {
2573 	struct socket *so = kn->kn_fp->f_data;
2574 	int rv;
2575 
2576 	sofilt_lock(so, &so->so_rcv);
2577 	rv = knote_process(kn, kev);
2578 	sofilt_unlock(so, &so->so_rcv);
2579 
2580 	return (rv);
2581 }
2582 
2583 #ifdef DDB
2584 void
2585 sobuf_print(struct sockbuf *,
2586     int (*)(const char *, ...) __attribute__((__format__(__kprintf__,1,2))));
2587 
2588 void
2589 sobuf_print(struct sockbuf *sb,
2590     int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2))))
2591 {
2592 	(*pr)("\tsb_cc: %lu\n", sb->sb_cc);
2593 	(*pr)("\tsb_datacc: %lu\n", sb->sb_datacc);
2594 	(*pr)("\tsb_hiwat: %lu\n", sb->sb_hiwat);
2595 	(*pr)("\tsb_wat: %lu\n", sb->sb_wat);
2596 	(*pr)("\tsb_mbcnt: %lu\n", sb->sb_mbcnt);
2597 	(*pr)("\tsb_mbmax: %lu\n", sb->sb_mbmax);
2598 	(*pr)("\tsb_lowat: %ld\n", sb->sb_lowat);
2599 	(*pr)("\tsb_mb: %p\n", sb->sb_mb);
2600 	(*pr)("\tsb_mbtail: %p\n", sb->sb_mbtail);
2601 	(*pr)("\tsb_lastrecord: %p\n", sb->sb_lastrecord);
2602 	(*pr)("\tsb_flags: %04x\n", sb->sb_flags);
2603 	(*pr)("\tsb_state: %04x\n", sb->sb_state);
2604 	(*pr)("\tsb_timeo_nsecs: %llu\n", sb->sb_timeo_nsecs);
2605 }
2606 
2607 void
2608 so_print(void *v,
2609     int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2))))
2610 {
2611 	struct socket *so = v;
2612 
2613 	(*pr)("socket %p\n", so);
2614 	(*pr)("so_type: %i\n", so->so_type);
2615 	(*pr)("so_options: 0x%04x\n", so->so_options); /* %b */
2616 	(*pr)("so_linger: %i\n", so->so_linger);
2617 	(*pr)("so_state: 0x%04x\n", so->so_state);
2618 	(*pr)("so_pcb: %p\n", so->so_pcb);
2619 	(*pr)("so_proto: %p\n", so->so_proto);
2620 	(*pr)("so_sigio: %p\n", so->so_sigio.sir_sigio);
2621 
2622 	(*pr)("so_head: %p\n", so->so_head);
2623 	(*pr)("so_onq: %p\n", so->so_onq);
2624 	(*pr)("so_q0: @%p first: %p\n", &so->so_q0, TAILQ_FIRST(&so->so_q0));
2625 	(*pr)("so_q: @%p first: %p\n", &so->so_q, TAILQ_FIRST(&so->so_q));
2626 	(*pr)("so_eq: next: %p\n", TAILQ_NEXT(so, so_qe));
2627 	(*pr)("so_q0len: %i\n", so->so_q0len);
2628 	(*pr)("so_qlen: %i\n", so->so_qlen);
2629 	(*pr)("so_qlimit: %i\n", so->so_qlimit);
2630 	(*pr)("so_timeo: %i\n", so->so_timeo);
2631 	(*pr)("so_obmark: %lu\n", so->so_oobmark);
2632 
2633 	(*pr)("so_sp: %p\n", so->so_sp);
2634 	if (so->so_sp != NULL) {
2635 		(*pr)("\tssp_socket: %p\n", so->so_sp->ssp_socket);
2636 		(*pr)("\tssp_soback: %p\n", so->so_sp->ssp_soback);
2637 		(*pr)("\tssp_len: %lld\n",
2638 		    (unsigned long long)so->so_sp->ssp_len);
2639 		(*pr)("\tssp_max: %lld\n",
2640 		    (unsigned long long)so->so_sp->ssp_max);
2641 		(*pr)("\tssp_idletv: %lld %ld\n", so->so_sp->ssp_idletv.tv_sec,
2642 		    so->so_sp->ssp_idletv.tv_usec);
2643 		(*pr)("\tssp_idleto: %spending (@%i)\n",
2644 		    timeout_pending(&so->so_sp->ssp_idleto) ? "" : "not ",
2645 		    so->so_sp->ssp_idleto.to_time);
2646 	}
2647 
2648 	(*pr)("so_rcv:\n");
2649 	sobuf_print(&so->so_rcv, pr);
2650 	(*pr)("so_snd:\n");
2651 	sobuf_print(&so->so_snd, pr);
2652 
2653 	(*pr)("so_upcall: %p so_upcallarg: %p\n",
2654 	    so->so_upcall, so->so_upcallarg);
2655 
2656 	(*pr)("so_euid: %d so_ruid: %d\n", so->so_euid, so->so_ruid);
2657 	(*pr)("so_egid: %d so_rgid: %d\n", so->so_egid, so->so_rgid);
2658 	(*pr)("so_cpid: %d\n", so->so_cpid);
2659 }
2660 #endif
2661