xref: /openbsd-src/sys/kern/uipc_socket.c (revision fb8aa7497fded39583f40e800732f9c046411717)
1 /*	$OpenBSD: uipc_socket.c,v 1.152 2016/06/13 21:24:43 bluhm 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/kernel.h>
44 #include <sys/event.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/unpcb.h>
48 #include <sys/socketvar.h>
49 #include <sys/signalvar.h>
50 #include <net/if.h>
51 #include <sys/pool.h>
52 
53 #ifdef DDB
54 #include <machine/db_machdep.h>
55 #endif
56 
57 void	sbsync(struct sockbuf *, struct mbuf *);
58 
59 int	sosplice(struct socket *, int, off_t, struct timeval *);
60 void	sounsplice(struct socket *, struct socket *, int);
61 void	soidle(void *);
62 int	somove(struct socket *, int);
63 
64 void	filt_sordetach(struct knote *kn);
65 int	filt_soread(struct knote *kn, long hint);
66 void	filt_sowdetach(struct knote *kn);
67 int	filt_sowrite(struct knote *kn, long hint);
68 int	filt_solisten(struct knote *kn, long hint);
69 
70 struct filterops solisten_filtops =
71 	{ 1, NULL, filt_sordetach, filt_solisten };
72 struct filterops soread_filtops =
73 	{ 1, NULL, filt_sordetach, filt_soread };
74 struct filterops sowrite_filtops =
75 	{ 1, NULL, filt_sowdetach, filt_sowrite };
76 
77 
78 #ifndef SOMINCONN
79 #define SOMINCONN 80
80 #endif /* SOMINCONN */
81 
82 int	somaxconn = SOMAXCONN;
83 int	sominconn = SOMINCONN;
84 
85 struct pool socket_pool;
86 #ifdef SOCKET_SPLICE
87 struct pool sosplice_pool;
88 #endif
89 
90 void
91 soinit(void)
92 {
93 	pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0, "sockpl", NULL);
94 	pool_setipl(&socket_pool, IPL_SOFTNET);
95 #ifdef SOCKET_SPLICE
96 	pool_init(&sosplice_pool, sizeof(struct sosplice), 0, 0, 0, "sosppl",
97 	    NULL);
98 	pool_setipl(&sosplice_pool, IPL_SOFTNET);
99 #endif
100 }
101 
102 /*
103  * Socket operation routines.
104  * These routines are called by the routines in
105  * sys_socket.c or from a system process, and
106  * implement the semantics of socket operations by
107  * switching out to the protocol specific routines.
108  */
109 int
110 socreate(int dom, struct socket **aso, int type, int proto)
111 {
112 	struct proc *p = curproc;		/* XXX */
113 	struct protosw *prp;
114 	struct socket *so;
115 	int error, s;
116 
117 	if (proto)
118 		prp = pffindproto(dom, proto, type);
119 	else
120 		prp = pffindtype(dom, type);
121 	if (prp == NULL || prp->pr_usrreq == 0)
122 		return (EPROTONOSUPPORT);
123 	if (prp->pr_type != type)
124 		return (EPROTOTYPE);
125 	s = splsoftnet();
126 	so = pool_get(&socket_pool, PR_WAITOK | PR_ZERO);
127 	TAILQ_INIT(&so->so_q0);
128 	TAILQ_INIT(&so->so_q);
129 	so->so_type = type;
130 	if (suser(p, 0) == 0)
131 		so->so_state = SS_PRIV;
132 	so->so_ruid = p->p_ucred->cr_ruid;
133 	so->so_euid = p->p_ucred->cr_uid;
134 	so->so_rgid = p->p_ucred->cr_rgid;
135 	so->so_egid = p->p_ucred->cr_gid;
136 	so->so_cpid = p->p_p->ps_pid;
137 	so->so_proto = prp;
138 	error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL,
139 	    (struct mbuf *)(long)proto, NULL, p);
140 	if (error) {
141 		so->so_state |= SS_NOFDREF;
142 		sofree(so);
143 		splx(s);
144 		return (error);
145 	}
146 	splx(s);
147 	*aso = so;
148 	return (0);
149 }
150 
151 int
152 sobind(struct socket *so, struct mbuf *nam, struct proc *p)
153 {
154 	int s = splsoftnet();
155 	int error;
156 
157 	error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, p);
158 	splx(s);
159 	return (error);
160 }
161 
162 int
163 solisten(struct socket *so, int backlog)
164 {
165 	int s, error;
166 
167 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING))
168 		return (EOPNOTSUPP);
169 #ifdef SOCKET_SPLICE
170 	if (isspliced(so) || issplicedback(so))
171 		return (EOPNOTSUPP);
172 #endif /* SOCKET_SPLICE */
173 	s = splsoftnet();
174 	error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, NULL, NULL,
175 	    curproc);
176 	if (error) {
177 		splx(s);
178 		return (error);
179 	}
180 	if (TAILQ_FIRST(&so->so_q) == NULL)
181 		so->so_options |= SO_ACCEPTCONN;
182 	if (backlog < 0 || backlog > somaxconn)
183 		backlog = somaxconn;
184 	if (backlog < sominconn)
185 		backlog = sominconn;
186 	so->so_qlimit = backlog;
187 	splx(s);
188 	return (0);
189 }
190 
191 /*
192  *  Must be called at splsoftnet()
193  */
194 
195 void
196 sofree(struct socket *so)
197 {
198 	splsoftassert(IPL_SOFTNET);
199 
200 	if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
201 		return;
202 	if (so->so_head) {
203 		/*
204 		 * We must not decommission a socket that's on the accept(2)
205 		 * queue.  If we do, then accept(2) may hang after select(2)
206 		 * indicated that the listening socket was ready.
207 		 */
208 		if (!soqremque(so, 0))
209 			return;
210 	}
211 #ifdef SOCKET_SPLICE
212 	if (so->so_sp) {
213 		if (issplicedback(so))
214 			sounsplice(so->so_sp->ssp_soback, so,
215 			    so->so_sp->ssp_soback != so);
216 		if (isspliced(so))
217 			sounsplice(so, so->so_sp->ssp_socket, 0);
218 		pool_put(&sosplice_pool, so->so_sp);
219 		so->so_sp = NULL;
220 	}
221 #endif /* SOCKET_SPLICE */
222 	sbrelease(&so->so_snd);
223 	sorflush(so);
224 	pool_put(&socket_pool, so);
225 }
226 
227 /*
228  * Close a socket on last file table reference removal.
229  * Initiate disconnect if connected.
230  * Free socket when disconnect complete.
231  */
232 int
233 soclose(struct socket *so)
234 {
235 	struct socket *so2;
236 	int s = splsoftnet();		/* conservative */
237 	int error = 0;
238 
239 	if (so->so_options & SO_ACCEPTCONN) {
240 		while ((so2 = TAILQ_FIRST(&so->so_q0)) != NULL) {
241 			(void) soqremque(so2, 0);
242 			(void) soabort(so2);
243 		}
244 		while ((so2 = TAILQ_FIRST(&so->so_q)) != NULL) {
245 			(void) soqremque(so2, 1);
246 			(void) soabort(so2);
247 		}
248 	}
249 	if (so->so_pcb == 0)
250 		goto discard;
251 	if (so->so_state & SS_ISCONNECTED) {
252 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
253 			error = sodisconnect(so);
254 			if (error)
255 				goto drop;
256 		}
257 		if (so->so_options & SO_LINGER) {
258 			if ((so->so_state & SS_ISDISCONNECTING) &&
259 			    (so->so_state & SS_NBIO))
260 				goto drop;
261 			while (so->so_state & SS_ISCONNECTED) {
262 				error = tsleep(&so->so_timeo,
263 				    PSOCK | PCATCH, "netcls",
264 				    so->so_linger * hz);
265 				if (error)
266 					break;
267 			}
268 		}
269 	}
270 drop:
271 	if (so->so_pcb) {
272 		int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, NULL,
273 		    NULL, NULL, curproc);
274 		if (error == 0)
275 			error = error2;
276 	}
277 discard:
278 	if (so->so_state & SS_NOFDREF)
279 		panic("soclose NOFDREF: so %p, so_type %d", so, so->so_type);
280 	so->so_state |= SS_NOFDREF;
281 	sofree(so);
282 	splx(s);
283 	return (error);
284 }
285 
286 /*
287  * Must be called at splsoftnet.
288  */
289 int
290 soabort(struct socket *so)
291 {
292 	splsoftassert(IPL_SOFTNET);
293 
294 	return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, NULL, NULL,
295 	   curproc);
296 }
297 
298 int
299 soaccept(struct socket *so, struct mbuf *nam)
300 {
301 	int s = splsoftnet();
302 	int error = 0;
303 
304 	if ((so->so_state & SS_NOFDREF) == 0)
305 		panic("soaccept !NOFDREF: so %p, so_type %d", so, so->so_type);
306 	so->so_state &= ~SS_NOFDREF;
307 	if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
308 	    (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
309 		error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, NULL,
310 		    nam, NULL, curproc);
311 	else
312 		error = ECONNABORTED;
313 	splx(s);
314 	return (error);
315 }
316 
317 int
318 soconnect(struct socket *so, struct mbuf *nam)
319 {
320 	int s;
321 	int error;
322 
323 	if (so->so_options & SO_ACCEPTCONN)
324 		return (EOPNOTSUPP);
325 	s = splsoftnet();
326 	/*
327 	 * If protocol is connection-based, can only connect once.
328 	 * Otherwise, if connected, try to disconnect first.
329 	 * This allows user to disconnect by connecting to, e.g.,
330 	 * a null address.
331 	 */
332 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
333 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
334 	    (error = sodisconnect(so))))
335 		error = EISCONN;
336 	else
337 		error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
338 		    NULL, nam, NULL, curproc);
339 	splx(s);
340 	return (error);
341 }
342 
343 int
344 soconnect2(struct socket *so1, struct socket *so2)
345 {
346 	int s = splsoftnet();
347 	int error;
348 
349 	error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, NULL,
350 	    (struct mbuf *)so2, NULL, curproc);
351 	splx(s);
352 	return (error);
353 }
354 
355 int
356 sodisconnect(struct socket *so)
357 {
358 	int s = splsoftnet();
359 	int error;
360 
361 	if ((so->so_state & SS_ISCONNECTED) == 0) {
362 		error = ENOTCONN;
363 		goto bad;
364 	}
365 	if (so->so_state & SS_ISDISCONNECTING) {
366 		error = EALREADY;
367 		goto bad;
368 	}
369 	error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, NULL, NULL,
370 	    NULL, curproc);
371 bad:
372 	splx(s);
373 	return (error);
374 }
375 
376 #define	SBLOCKWAIT(f)	(((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
377 /*
378  * Send on a socket.
379  * If send must go all at once and message is larger than
380  * send buffering, then hard error.
381  * Lock against other senders.
382  * If must go all at once and not enough room now, then
383  * inform user that this would block and do nothing.
384  * Otherwise, if nonblocking, send as much as possible.
385  * The data to be sent is described by "uio" if nonzero,
386  * otherwise by the mbuf chain "top" (which must be null
387  * if uio is not).  Data provided in mbuf chain must be small
388  * enough to send all at once.
389  *
390  * Returns nonzero on error, timeout or signal; callers
391  * must check for short counts if EINTR/ERESTART are returned.
392  * Data and control buffers are freed on return.
393  */
394 int
395 sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
396     struct mbuf *control, int flags)
397 {
398 	struct mbuf **mp;
399 	struct mbuf *m;
400 	long space, clen = 0;
401 	u_long len, mlen;
402 	size_t resid;
403 	int error, s;
404 	int atomic = sosendallatonce(so) || top;
405 
406 	if (uio)
407 		resid = uio->uio_resid;
408 	else
409 		resid = top->m_pkthdr.len;
410 	/* MSG_EOR on a SOCK_STREAM socket is invalid. */
411 	if (so->so_type == SOCK_STREAM && (flags & MSG_EOR)) {
412 		error = EINVAL;
413 		goto out;
414 	}
415 	if (uio && uio->uio_procp)
416 		uio->uio_procp->p_ru.ru_msgsnd++;
417 	if (control) {
418 		/*
419 		 * In theory clen should be unsigned (since control->m_len is).
420 		 * However, space must be signed, as it might be less than 0
421 		 * if we over-committed, and we must use a signed comparison
422 		 * of space and clen.
423 		 */
424 		clen = control->m_len;
425 		/* reserve extra space for AF_LOCAL's internalize */
426 		if (so->so_proto->pr_domain->dom_family == AF_LOCAL &&
427 		    clen >= CMSG_ALIGN(sizeof(struct cmsghdr)) &&
428 		    mtod(control, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
429 			clen = CMSG_SPACE(
430 			    (clen - CMSG_ALIGN(sizeof(struct cmsghdr))) *
431 			    (sizeof(struct file *) / sizeof(int)));
432 	}
433 
434 #define	snderr(errno)	{ error = errno; splx(s); goto release; }
435 
436 restart:
437 	if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
438 		goto out;
439 	so->so_state |= SS_ISSENDING;
440 	do {
441 		s = splsoftnet();
442 		if (so->so_state & SS_CANTSENDMORE)
443 			snderr(EPIPE);
444 		if (so->so_error) {
445 			error = so->so_error;
446 			so->so_error = 0;
447 			splx(s);
448 			goto release;
449 		}
450 		if ((so->so_state & SS_ISCONNECTED) == 0) {
451 			if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
452 				if (!(resid == 0 && clen != 0))
453 					snderr(ENOTCONN);
454 			} else if (addr == 0)
455 				snderr(EDESTADDRREQ);
456 		}
457 		space = sbspace(&so->so_snd);
458 		if (flags & MSG_OOB)
459 			space += 1024;
460 		if ((atomic && resid > so->so_snd.sb_hiwat) ||
461 		    (so->so_proto->pr_domain->dom_family != AF_LOCAL &&
462 		    clen > so->so_snd.sb_hiwat))
463 			snderr(EMSGSIZE);
464 		if (space < clen ||
465 		    (space - clen < resid &&
466 		    (atomic || space < so->so_snd.sb_lowat))) {
467 			if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT))
468 				snderr(EWOULDBLOCK);
469 			sbunlock(&so->so_snd);
470 			error = sbwait(&so->so_snd);
471 			so->so_state &= ~SS_ISSENDING;
472 			splx(s);
473 			if (error)
474 				goto out;
475 			goto restart;
476 		}
477 		splx(s);
478 		mp = &top;
479 		space -= clen;
480 		do {
481 			if (uio == NULL) {
482 				/*
483 				 * Data is prepackaged in "top".
484 				 */
485 				resid = 0;
486 				if (flags & MSG_EOR)
487 					top->m_flags |= M_EOR;
488 			} else do {
489 				if (top == 0) {
490 					MGETHDR(m, M_WAIT, MT_DATA);
491 					mlen = MHLEN;
492 					m->m_pkthdr.len = 0;
493 					m->m_pkthdr.ph_ifidx = 0;
494 				} else {
495 					MGET(m, M_WAIT, MT_DATA);
496 					mlen = MLEN;
497 				}
498 				if (resid >= MINCLSIZE && space >= MCLBYTES) {
499 					MCLGET(m, M_NOWAIT);
500 					if ((m->m_flags & M_EXT) == 0)
501 						goto nopages;
502 					if (atomic && top == 0) {
503 						len = ulmin(MCLBYTES - max_hdr,
504 						    resid);
505 						m->m_data += max_hdr;
506 					} else
507 						len = ulmin(MCLBYTES, resid);
508 					space -= len;
509 				} else {
510 nopages:
511 					len = ulmin(ulmin(mlen, resid), space);
512 					space -= len;
513 					/*
514 					 * For datagram protocols, leave room
515 					 * for protocol headers in first mbuf.
516 					 */
517 					if (atomic && top == 0 && len < mlen)
518 						MH_ALIGN(m, len);
519 				}
520 				error = uiomove(mtod(m, caddr_t), len, uio);
521 				resid = uio->uio_resid;
522 				m->m_len = len;
523 				*mp = m;
524 				top->m_pkthdr.len += len;
525 				if (error)
526 					goto release;
527 				mp = &m->m_next;
528 				if (resid == 0) {
529 					if (flags & MSG_EOR)
530 						top->m_flags |= M_EOR;
531 					break;
532 				}
533 			} while (space > 0 && atomic);
534 			s = splsoftnet();		/* XXX */
535 			if (resid == 0)
536 				so->so_state &= ~SS_ISSENDING;
537 			error = (*so->so_proto->pr_usrreq)(so,
538 			    (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
539 			    top, addr, control, curproc);
540 			splx(s);
541 			clen = 0;
542 			control = 0;
543 			top = 0;
544 			mp = &top;
545 			if (error)
546 				goto release;
547 		} while (resid && space > 0);
548 	} while (resid);
549 
550 release:
551 	so->so_state &= ~SS_ISSENDING;
552 	sbunlock(&so->so_snd);
553 out:
554 	if (top)
555 		m_freem(top);
556 	if (control)
557 		m_freem(control);
558 	return (error);
559 }
560 
561 /*
562  * Following replacement or removal of the first mbuf on the first
563  * mbuf chain of a socket buffer, push necessary state changes back
564  * into the socket buffer so that other consumers see the values
565  * consistently.  'nextrecord' is the callers locally stored value of
566  * the original value of sb->sb_mb->m_nextpkt which must be restored
567  * when the lead mbuf changes.  NOTE: 'nextrecord' may be NULL.
568  */
569 void
570 sbsync(struct sockbuf *sb, struct mbuf *nextrecord)
571 {
572 
573 	/*
574 	 * First, update for the new value of nextrecord.  If necessary,
575 	 * make it the first record.
576 	 */
577 	if (sb->sb_mb != NULL)
578 		sb->sb_mb->m_nextpkt = nextrecord;
579 	else
580 		sb->sb_mb = nextrecord;
581 
582 	/*
583 	 * Now update any dependent socket buffer fields to reflect
584 	 * the new state.  This is an inline of SB_EMPTY_FIXUP, with
585 	 * the addition of a second clause that takes care of the
586 	 * case where sb_mb has been updated, but remains the last
587 	 * record.
588 	 */
589 	if (sb->sb_mb == NULL) {
590 		sb->sb_mbtail = NULL;
591 		sb->sb_lastrecord = NULL;
592 	} else if (sb->sb_mb->m_nextpkt == NULL)
593 		sb->sb_lastrecord = sb->sb_mb;
594 }
595 
596 /*
597  * Implement receive operations on a socket.
598  * We depend on the way that records are added to the sockbuf
599  * by sbappend*.  In particular, each record (mbufs linked through m_next)
600  * must begin with an address if the protocol so specifies,
601  * followed by an optional mbuf or mbufs containing ancillary data,
602  * and then zero or more mbufs of data.
603  * In order to avoid blocking network interrupts for the entire time here,
604  * we splx() while doing the actual copy to user space.
605  * Although the sockbuf is locked, new data may still be appended,
606  * and thus we must maintain consistency of the sockbuf during that time.
607  *
608  * The caller may receive the data as a single mbuf chain by supplying
609  * an mbuf **mp0 for use in returning the chain.  The uio is then used
610  * only for the count in uio_resid.
611  */
612 int
613 soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
614     struct mbuf **mp0, struct mbuf **controlp, int *flagsp,
615     socklen_t controllen)
616 {
617 	struct mbuf *m, **mp;
618 	struct mbuf *cm;
619 	u_long len, offset, moff;
620 	int flags, error, s, type, uio_error = 0;
621 	struct protosw *pr = so->so_proto;
622 	struct mbuf *nextrecord;
623 	size_t resid, orig_resid = uio->uio_resid;
624 
625 	mp = mp0;
626 	if (paddr)
627 		*paddr = 0;
628 	if (controlp)
629 		*controlp = 0;
630 	if (flagsp)
631 		flags = *flagsp &~ MSG_EOR;
632 	else
633 		flags = 0;
634 	if (so->so_state & SS_NBIO)
635 		flags |= MSG_DONTWAIT;
636 	if (flags & MSG_OOB) {
637 		m = m_get(M_WAIT, MT_DATA);
638 		error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
639 		    (struct mbuf *)(long)(flags & MSG_PEEK), NULL, curproc);
640 		if (error)
641 			goto bad;
642 		do {
643 			error = uiomove(mtod(m, caddr_t),
644 			    ulmin(uio->uio_resid, m->m_len), uio);
645 			m = m_free(m);
646 		} while (uio->uio_resid && error == 0 && m);
647 bad:
648 		if (m)
649 			m_freem(m);
650 		return (error);
651 	}
652 	if (mp)
653 		*mp = NULL;
654 
655 restart:
656 	if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
657 		return (error);
658 	s = splsoftnet();
659 
660 	m = so->so_rcv.sb_mb;
661 #ifdef SOCKET_SPLICE
662 	if (isspliced(so))
663 		m = NULL;
664 #endif /* SOCKET_SPLICE */
665 	/*
666 	 * If we have less data than requested, block awaiting more
667 	 * (subject to any timeout) if:
668 	 *   1. the current count is less than the low water mark,
669 	 *   2. MSG_WAITALL is set, and it is possible to do the entire
670 	 *	receive operation at once if we block (resid <= hiwat), or
671 	 *   3. MSG_DONTWAIT is not set.
672 	 * If MSG_WAITALL is set but resid is larger than the receive buffer,
673 	 * we have to do the receive in sections, and thus risk returning
674 	 * a short count if a timeout or signal occurs after we start.
675 	 */
676 	if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
677 	    so->so_rcv.sb_cc < uio->uio_resid) &&
678 	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
679 	    ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
680 	    m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
681 #ifdef DIAGNOSTIC
682 		if (m == NULL && so->so_rcv.sb_cc)
683 #ifdef SOCKET_SPLICE
684 		    if (!isspliced(so))
685 #endif /* SOCKET_SPLICE */
686 			panic("receive 1: so %p, so_type %d, sb_cc %lu",
687 			    so, so->so_type, so->so_rcv.sb_cc);
688 #endif
689 		if (so->so_error) {
690 			if (m)
691 				goto dontblock;
692 			error = so->so_error;
693 			if ((flags & MSG_PEEK) == 0)
694 				so->so_error = 0;
695 			goto release;
696 		}
697 		if (so->so_state & SS_CANTRCVMORE) {
698 			if (m)
699 				goto dontblock;
700 			else if (so->so_rcv.sb_cc == 0)
701 				goto release;
702 		}
703 		for (; m; m = m->m_next)
704 			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
705 				m = so->so_rcv.sb_mb;
706 				goto dontblock;
707 			}
708 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
709 		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
710 			error = ENOTCONN;
711 			goto release;
712 		}
713 		if (uio->uio_resid == 0 && controlp == NULL)
714 			goto release;
715 		if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
716 			error = EWOULDBLOCK;
717 			goto release;
718 		}
719 		SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1");
720 		SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1");
721 		sbunlock(&so->so_rcv);
722 		error = sbwait(&so->so_rcv);
723 		splx(s);
724 		if (error)
725 			return (error);
726 		goto restart;
727 	}
728 dontblock:
729 	/*
730 	 * On entry here, m points to the first record of the socket buffer.
731 	 * From this point onward, we maintain 'nextrecord' as a cache of the
732 	 * pointer to the next record in the socket buffer.  We must keep the
733 	 * various socket buffer pointers and local stack versions of the
734 	 * pointers in sync, pushing out modifications before operations that
735 	 * may sleep, and re-reading them afterwards.
736 	 *
737 	 * Otherwise, we will race with the network stack appending new data
738 	 * or records onto the socket buffer by using inconsistent/stale
739 	 * versions of the field, possibly resulting in socket buffer
740 	 * corruption.
741 	 */
742 	if (uio->uio_procp)
743 		uio->uio_procp->p_ru.ru_msgrcv++;
744 	KASSERT(m == so->so_rcv.sb_mb);
745 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
746 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
747 	nextrecord = m->m_nextpkt;
748 	if (pr->pr_flags & PR_ADDR) {
749 #ifdef DIAGNOSTIC
750 		if (m->m_type != MT_SONAME)
751 			panic("receive 1a: so %p, so_type %d, m %p, m_type %d",
752 			    so, so->so_type, m, m->m_type);
753 #endif
754 		orig_resid = 0;
755 		if (flags & MSG_PEEK) {
756 			if (paddr)
757 				*paddr = m_copym(m, 0, m->m_len, M_NOWAIT);
758 			m = m->m_next;
759 		} else {
760 			sbfree(&so->so_rcv, m);
761 			if (paddr) {
762 				*paddr = m;
763 				so->so_rcv.sb_mb = m->m_next;
764 				m->m_next = 0;
765 				m = so->so_rcv.sb_mb;
766 			} else {
767 				so->so_rcv.sb_mb = m_free(m);
768 				m = so->so_rcv.sb_mb;
769 			}
770 			sbsync(&so->so_rcv, nextrecord);
771 		}
772 	}
773 	while (m && m->m_type == MT_CONTROL && error == 0) {
774 		if (flags & MSG_PEEK) {
775 			if (controlp)
776 				*controlp = m_copym(m, 0, m->m_len, M_NOWAIT);
777 			m = m->m_next;
778 		} else {
779 			sbfree(&so->so_rcv, m);
780 			so->so_rcv.sb_mb = m->m_next;
781 			m->m_nextpkt = m->m_next = NULL;
782 			cm = m;
783 			m = so->so_rcv.sb_mb;
784 			sbsync(&so->so_rcv, nextrecord);
785 			if (controlp) {
786 				if (pr->pr_domain->dom_externalize &&
787 				    mtod(cm, struct cmsghdr *)->cmsg_type ==
788 				    SCM_RIGHTS)
789 				   error = (*pr->pr_domain->dom_externalize)(cm,
790 				       controllen, flags);
791 				*controlp = cm;
792 			} else {
793 				/*
794 				 * Dispose of any SCM_RIGHTS message that went
795 				 * through the read path rather than recv.
796 				 */
797 				if (pr->pr_domain->dom_dispose &&
798 				    mtod(cm, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
799 					pr->pr_domain->dom_dispose(cm);
800 				m_free(cm);
801 			}
802 		}
803 		if (m != NULL)
804 			nextrecord = so->so_rcv.sb_mb->m_nextpkt;
805 		else
806 			nextrecord = so->so_rcv.sb_mb;
807 		if (controlp) {
808 			orig_resid = 0;
809 			controlp = &(*controlp)->m_next;
810 		}
811 	}
812 
813 	/* If m is non-NULL, we have some data to read. */
814 	if (m) {
815 		type = m->m_type;
816 		if (type == MT_OOBDATA)
817 			flags |= MSG_OOB;
818 		if (m->m_flags & M_BCAST)
819 			flags |= MSG_BCAST;
820 		if (m->m_flags & M_MCAST)
821 			flags |= MSG_MCAST;
822 	}
823 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
824 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
825 
826 	moff = 0;
827 	offset = 0;
828 	while (m && uio->uio_resid > 0 && error == 0) {
829 		if (m->m_type == MT_OOBDATA) {
830 			if (type != MT_OOBDATA)
831 				break;
832 		} else if (type == MT_OOBDATA)
833 			break;
834 #ifdef DIAGNOSTIC
835 		else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
836 			panic("receive 3: so %p, so_type %d, m %p, m_type %d",
837 			    so, so->so_type, m, m->m_type);
838 #endif
839 		so->so_state &= ~SS_RCVATMARK;
840 		len = uio->uio_resid;
841 		if (so->so_oobmark && len > so->so_oobmark - offset)
842 			len = so->so_oobmark - offset;
843 		if (len > m->m_len - moff)
844 			len = m->m_len - moff;
845 		/*
846 		 * If mp is set, just pass back the mbufs.
847 		 * Otherwise copy them out via the uio, then free.
848 		 * Sockbuf must be consistent here (points to current mbuf,
849 		 * it points to next record) when we drop priority;
850 		 * we must note any additions to the sockbuf when we
851 		 * block interrupts again.
852 		 */
853 		if (mp == NULL && uio_error == 0) {
854 			SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove");
855 			SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");
856 			resid = uio->uio_resid;
857 			splx(s);
858 			uio_error = uiomove(mtod(m, caddr_t) + moff, len, uio);
859 			s = splsoftnet();
860 			if (uio_error)
861 				uio->uio_resid = resid - len;
862 		} else
863 			uio->uio_resid -= len;
864 		if (len == m->m_len - moff) {
865 			if (m->m_flags & M_EOR)
866 				flags |= MSG_EOR;
867 			if (flags & MSG_PEEK) {
868 				m = m->m_next;
869 				moff = 0;
870 			} else {
871 				nextrecord = m->m_nextpkt;
872 				sbfree(&so->so_rcv, m);
873 				if (mp) {
874 					*mp = m;
875 					mp = &m->m_next;
876 					so->so_rcv.sb_mb = m = m->m_next;
877 					*mp = NULL;
878 				} else {
879 					so->so_rcv.sb_mb = m_free(m);
880 					m = so->so_rcv.sb_mb;
881 				}
882 				/*
883 				 * If m != NULL, we also know that
884 				 * so->so_rcv.sb_mb != NULL.
885 				 */
886 				KASSERT(so->so_rcv.sb_mb == m);
887 				if (m) {
888 					m->m_nextpkt = nextrecord;
889 					if (nextrecord == NULL)
890 						so->so_rcv.sb_lastrecord = m;
891 				} else {
892 					so->so_rcv.sb_mb = nextrecord;
893 					SB_EMPTY_FIXUP(&so->so_rcv);
894 				}
895 				SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
896 				SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
897 			}
898 		} else {
899 			if (flags & MSG_PEEK)
900 				moff += len;
901 			else {
902 				if (mp)
903 					*mp = m_copym(m, 0, len, M_WAIT);
904 				m->m_data += len;
905 				m->m_len -= len;
906 				so->so_rcv.sb_cc -= len;
907 				so->so_rcv.sb_datacc -= len;
908 			}
909 		}
910 		if (so->so_oobmark) {
911 			if ((flags & MSG_PEEK) == 0) {
912 				so->so_oobmark -= len;
913 				if (so->so_oobmark == 0) {
914 					so->so_state |= SS_RCVATMARK;
915 					break;
916 				}
917 			} else {
918 				offset += len;
919 				if (offset == so->so_oobmark)
920 					break;
921 			}
922 		}
923 		if (flags & MSG_EOR)
924 			break;
925 		/*
926 		 * If the MSG_WAITALL flag is set (for non-atomic socket),
927 		 * we must not quit until "uio->uio_resid == 0" or an error
928 		 * termination.  If a signal/timeout occurs, return
929 		 * with a short count but without error.
930 		 * Keep sockbuf locked against other readers.
931 		 */
932 		while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
933 		    !sosendallatonce(so) && !nextrecord) {
934 			if (so->so_error || so->so_state & SS_CANTRCVMORE)
935 				break;
936 			SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
937 			SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
938 			error = sbwait(&so->so_rcv);
939 			if (error) {
940 				sbunlock(&so->so_rcv);
941 				splx(s);
942 				return (0);
943 			}
944 			if ((m = so->so_rcv.sb_mb) != NULL)
945 				nextrecord = m->m_nextpkt;
946 		}
947 	}
948 
949 	if (m && pr->pr_flags & PR_ATOMIC) {
950 		flags |= MSG_TRUNC;
951 		if ((flags & MSG_PEEK) == 0)
952 			(void) sbdroprecord(&so->so_rcv);
953 	}
954 	if ((flags & MSG_PEEK) == 0) {
955 		if (m == NULL) {
956 			/*
957 			 * First part is an inline SB_EMPTY_FIXUP().  Second
958 			 * part makes sure sb_lastrecord is up-to-date if
959 			 * there is still data in the socket buffer.
960 			 */
961 			so->so_rcv.sb_mb = nextrecord;
962 			if (so->so_rcv.sb_mb == NULL) {
963 				so->so_rcv.sb_mbtail = NULL;
964 				so->so_rcv.sb_lastrecord = NULL;
965 			} else if (nextrecord->m_nextpkt == NULL)
966 				so->so_rcv.sb_lastrecord = nextrecord;
967 		}
968 		SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
969 		SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
970 		if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
971 			(*pr->pr_usrreq)(so, PRU_RCVD, NULL,
972 			    (struct mbuf *)(long)flags, NULL, curproc);
973 	}
974 	if (orig_resid == uio->uio_resid && orig_resid &&
975 	    (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
976 		sbunlock(&so->so_rcv);
977 		splx(s);
978 		goto restart;
979 	}
980 
981 	if (uio_error)
982 		error = uio_error;
983 
984 	if (flagsp)
985 		*flagsp |= flags;
986 release:
987 	sbunlock(&so->so_rcv);
988 	splx(s);
989 	return (error);
990 }
991 
992 int
993 soshutdown(struct socket *so, int how)
994 {
995 	struct protosw *pr = so->so_proto;
996 
997 	switch (how) {
998 	case SHUT_RD:
999 	case SHUT_RDWR:
1000 		sorflush(so);
1001 		if (how == SHUT_RD)
1002 			return (0);
1003 		/* FALLTHROUGH */
1004 	case SHUT_WR:
1005 		return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, NULL, NULL,
1006 		    curproc);
1007 	default:
1008 		return (EINVAL);
1009 	}
1010 }
1011 
1012 void
1013 sorflush(struct socket *so)
1014 {
1015 	struct sockbuf *sb = &so->so_rcv;
1016 	struct protosw *pr = so->so_proto;
1017 	int s;
1018 	struct sockbuf asb;
1019 
1020 	sb->sb_flags |= SB_NOINTR;
1021 	(void) sblock(sb, M_WAITOK);
1022 	s = splnet();
1023 	socantrcvmore(so);
1024 	sbunlock(sb);
1025 	asb = *sb;
1026 	memset(sb, 0, sizeof (*sb));
1027 	/* XXX - the memset stomps all over so_rcv */
1028 	if (asb.sb_flags & SB_KNOTE) {
1029 		sb->sb_sel.si_note = asb.sb_sel.si_note;
1030 		sb->sb_flags = SB_KNOTE;
1031 	}
1032 	splx(s);
1033 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
1034 		(*pr->pr_domain->dom_dispose)(asb.sb_mb);
1035 	sbrelease(&asb);
1036 }
1037 
1038 #ifdef SOCKET_SPLICE
1039 
1040 #define so_splicelen	so_sp->ssp_len
1041 #define so_splicemax	so_sp->ssp_max
1042 #define so_idletv	so_sp->ssp_idletv
1043 #define so_idleto	so_sp->ssp_idleto
1044 
1045 int
1046 sosplice(struct socket *so, int fd, off_t max, struct timeval *tv)
1047 {
1048 	struct file	*fp;
1049 	struct socket	*sosp;
1050 	int		 s, error = 0;
1051 
1052 	if ((so->so_proto->pr_flags & PR_SPLICE) == 0)
1053 		return (EPROTONOSUPPORT);
1054 	if (so->so_options & SO_ACCEPTCONN)
1055 		return (EOPNOTSUPP);
1056 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1057 	    (so->so_proto->pr_flags & PR_CONNREQUIRED))
1058 		return (ENOTCONN);
1059 	if (so->so_sp == NULL)
1060 		so->so_sp = pool_get(&sosplice_pool, PR_WAITOK | PR_ZERO);
1061 
1062 	/* If no fd is given, unsplice by removing existing link. */
1063 	if (fd < 0) {
1064 		/* Lock receive buffer. */
1065 		if ((error = sblock(&so->so_rcv,
1066 		    (so->so_state & SS_NBIO) ? M_NOWAIT : M_WAITOK)) != 0)
1067 			return (error);
1068 		s = splsoftnet();
1069 		if (so->so_sp->ssp_socket)
1070 			sounsplice(so, so->so_sp->ssp_socket, 1);
1071 		splx(s);
1072 		sbunlock(&so->so_rcv);
1073 		return (0);
1074 	}
1075 
1076 	if (max && max < 0)
1077 		return (EINVAL);
1078 
1079 	if (tv && (tv->tv_sec < 0 || tv->tv_usec < 0))
1080 		return (EINVAL);
1081 
1082 	/* Find sosp, the drain socket where data will be spliced into. */
1083 	if ((error = getsock(curproc, fd, &fp)) != 0)
1084 		return (error);
1085 	sosp = fp->f_data;
1086 	if (sosp->so_sp == NULL)
1087 		sosp->so_sp = pool_get(&sosplice_pool, PR_WAITOK | PR_ZERO);
1088 
1089 	/* Lock both receive and send buffer. */
1090 	if ((error = sblock(&so->so_rcv,
1091 	    (so->so_state & SS_NBIO) ? M_NOWAIT : M_WAITOK)) != 0) {
1092 		FRELE(fp, curproc);
1093 		return (error);
1094 	}
1095 	if ((error = sblock(&sosp->so_snd, M_WAITOK)) != 0) {
1096 		sbunlock(&so->so_rcv);
1097 		FRELE(fp, curproc);
1098 		return (error);
1099 	}
1100 	s = splsoftnet();
1101 
1102 	if (so->so_sp->ssp_socket || sosp->so_sp->ssp_soback) {
1103 		error = EBUSY;
1104 		goto release;
1105 	}
1106 	if (sosp->so_proto->pr_usrreq != so->so_proto->pr_usrreq) {
1107 		error = EPROTONOSUPPORT;
1108 		goto release;
1109 	}
1110 	if (sosp->so_options & SO_ACCEPTCONN) {
1111 		error = EOPNOTSUPP;
1112 		goto release;
1113 	}
1114 	if ((sosp->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0) {
1115 		error = ENOTCONN;
1116 		goto release;
1117 	}
1118 
1119 	/* Splice so and sosp together. */
1120 	so->so_sp->ssp_socket = sosp;
1121 	sosp->so_sp->ssp_soback = so;
1122 	so->so_splicelen = 0;
1123 	so->so_splicemax = max;
1124 	if (tv)
1125 		so->so_idletv = *tv;
1126 	else
1127 		timerclear(&so->so_idletv);
1128 	timeout_set(&so->so_idleto, soidle, so);
1129 
1130 	/*
1131 	 * To prevent softnet interrupt from calling somove() while
1132 	 * we sleep, the socket buffers are not marked as spliced yet.
1133 	 */
1134 	if (somove(so, M_WAIT)) {
1135 		so->so_rcv.sb_flagsintr |= SB_SPLICE;
1136 		sosp->so_snd.sb_flagsintr |= SB_SPLICE;
1137 	}
1138 
1139  release:
1140 	splx(s);
1141 	sbunlock(&sosp->so_snd);
1142 	sbunlock(&so->so_rcv);
1143 	FRELE(fp, curproc);
1144 	return (error);
1145 }
1146 
1147 void
1148 sounsplice(struct socket *so, struct socket *sosp, int wakeup)
1149 {
1150 	splsoftassert(IPL_SOFTNET);
1151 
1152 	timeout_del(&so->so_idleto);
1153 	sosp->so_snd.sb_flagsintr &= ~SB_SPLICE;
1154 	so->so_rcv.sb_flagsintr &= ~SB_SPLICE;
1155 	so->so_sp->ssp_socket = sosp->so_sp->ssp_soback = NULL;
1156 	if (wakeup && soreadable(so))
1157 		sorwakeup(so);
1158 }
1159 
1160 void
1161 soidle(void *arg)
1162 {
1163 	struct socket *so = arg;
1164 	int s;
1165 
1166 	s = splsoftnet();
1167 	if (so->so_rcv.sb_flagsintr & SB_SPLICE) {
1168 		so->so_error = ETIMEDOUT;
1169 		sounsplice(so, so->so_sp->ssp_socket, 1);
1170 	}
1171 	splx(s);
1172 }
1173 
1174 /*
1175  * Move data from receive buffer of spliced source socket to send
1176  * buffer of drain socket.  Try to move as much as possible in one
1177  * big chunk.  It is a TCP only implementation.
1178  * Return value 0 means splicing has been finished, 1 continue.
1179  */
1180 int
1181 somove(struct socket *so, int wait)
1182 {
1183 	struct socket	*sosp = so->so_sp->ssp_socket;
1184 	struct mbuf	*m, **mp, *nextrecord;
1185 	u_long		 len, off, oobmark;
1186 	long		 space;
1187 	int		 error = 0, maxreached = 0;
1188 	short		 state;
1189 
1190 	splsoftassert(IPL_SOFTNET);
1191 
1192  nextpkt:
1193 	if (so->so_error) {
1194 		error = so->so_error;
1195 		goto release;
1196 	}
1197 	if (sosp->so_state & SS_CANTSENDMORE) {
1198 		error = EPIPE;
1199 		goto release;
1200 	}
1201 	if (sosp->so_error && sosp->so_error != ETIMEDOUT &&
1202 	    sosp->so_error != EFBIG && sosp->so_error != ELOOP) {
1203 		error = sosp->so_error;
1204 		goto release;
1205 	}
1206 	if ((sosp->so_state & SS_ISCONNECTED) == 0)
1207 		goto release;
1208 
1209 	/* Calculate how many bytes can be copied now. */
1210 	len = so->so_rcv.sb_datacc;
1211 	if (so->so_splicemax) {
1212 		KASSERT(so->so_splicelen < so->so_splicemax);
1213 		if (so->so_splicemax <= so->so_splicelen + len) {
1214 			len = so->so_splicemax - so->so_splicelen;
1215 			maxreached = 1;
1216 		}
1217 	}
1218 	space = sbspace(&sosp->so_snd);
1219 	if (so->so_oobmark && so->so_oobmark < len &&
1220 	    so->so_oobmark < space + 1024)
1221 		space += 1024;
1222 	if (space <= 0) {
1223 		maxreached = 0;
1224 		goto release;
1225 	}
1226 	if (space < len) {
1227 		maxreached = 0;
1228 		if (space < sosp->so_snd.sb_lowat)
1229 			goto release;
1230 		len = space;
1231 	}
1232 	sosp->so_state |= SS_ISSENDING;
1233 
1234 	SBLASTRECORDCHK(&so->so_rcv, "somove 1");
1235 	SBLASTMBUFCHK(&so->so_rcv, "somove 1");
1236 	m = so->so_rcv.sb_mb;
1237 	if (m == NULL)
1238 		goto release;
1239 	nextrecord = m->m_nextpkt;
1240 
1241 	/* Drop address and control information not used with splicing. */
1242 	if (so->so_proto->pr_flags & PR_ADDR) {
1243 #ifdef DIAGNOSTIC
1244 		if (m->m_type != MT_SONAME)
1245 			panic("somove soname: so %p, so_type %d, m %p, "
1246 			    "m_type %d", so, so->so_type, m, m->m_type);
1247 #endif
1248 		m = m->m_next;
1249 	}
1250 	while (m && m->m_type == MT_CONTROL)
1251 		m = m->m_next;
1252 	if (m == NULL) {
1253 		sbdroprecord(&so->so_rcv);
1254 		if (so->so_proto->pr_flags & PR_WANTRCVD && so->so_pcb)
1255 			(so->so_proto->pr_usrreq)(so, PRU_RCVD, NULL,
1256 			    NULL, NULL, NULL);
1257 		goto nextpkt;
1258 	}
1259 
1260 	/*
1261 	 * By splicing sockets connected to localhost, userland might create a
1262 	 * loop.  Dissolve splicing with error if loop is detected by counter.
1263 	 */
1264 	if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.ph_loopcnt++ >= M_MAXLOOP) {
1265 		error = ELOOP;
1266 		goto release;
1267 	}
1268 
1269 	if (so->so_proto->pr_flags & PR_ATOMIC) {
1270 		if ((m->m_flags & M_PKTHDR) == 0)
1271 			panic("somove !PKTHDR: so %p, so_type %d, m %p, "
1272 			    "m_type %d", so, so->so_type, m, m->m_type);
1273 		if (sosp->so_snd.sb_hiwat < m->m_pkthdr.len) {
1274 			error = EMSGSIZE;
1275 			goto release;
1276 		}
1277 		if (len < m->m_pkthdr.len)
1278 			goto release;
1279 		if (m->m_pkthdr.len < len) {
1280 			maxreached = 0;
1281 			len = m->m_pkthdr.len;
1282 		}
1283 		/*
1284 		 * Throw away the name mbuf after it has been assured
1285 		 * that the whole first record can be processed.
1286 		 */
1287 		m = so->so_rcv.sb_mb;
1288 		sbfree(&so->so_rcv, m);
1289 		so->so_rcv.sb_mb = m_free(m);
1290 		sbsync(&so->so_rcv, nextrecord);
1291 	}
1292 	/*
1293 	 * Throw away the control mbufs after it has been assured
1294 	 * that the whole first record can be processed.
1295 	 */
1296 	m = so->so_rcv.sb_mb;
1297 	while (m && m->m_type == MT_CONTROL) {
1298 		sbfree(&so->so_rcv, m);
1299 		so->so_rcv.sb_mb = m_free(m);
1300 		m = so->so_rcv.sb_mb;
1301 		sbsync(&so->so_rcv, nextrecord);
1302 	}
1303 
1304 	SBLASTRECORDCHK(&so->so_rcv, "somove 2");
1305 	SBLASTMBUFCHK(&so->so_rcv, "somove 2");
1306 
1307 	/* Take at most len mbufs out of receive buffer. */
1308 	for (off = 0, mp = &m; off <= len && *mp;
1309 	    off += (*mp)->m_len, mp = &(*mp)->m_next) {
1310 		u_long size = len - off;
1311 
1312 #ifdef DIAGNOSTIC
1313 		if ((*mp)->m_type != MT_DATA && (*mp)->m_type != MT_HEADER)
1314 			panic("somove type: so %p, so_type %d, m %p, "
1315 			    "m_type %d", so, so->so_type, *mp, (*mp)->m_type);
1316 #endif
1317 		if ((*mp)->m_len > size) {
1318 			if (!maxreached || (*mp = m_copym(
1319 			    so->so_rcv.sb_mb, 0, size, wait)) == NULL) {
1320 				len -= size;
1321 				break;
1322 			}
1323 			so->so_rcv.sb_mb->m_data += size;
1324 			so->so_rcv.sb_mb->m_len -= size;
1325 			so->so_rcv.sb_cc -= size;
1326 			so->so_rcv.sb_datacc -= size;
1327 		} else {
1328 			*mp = so->so_rcv.sb_mb;
1329 			sbfree(&so->so_rcv, *mp);
1330 			so->so_rcv.sb_mb = (*mp)->m_next;
1331 			sbsync(&so->so_rcv, nextrecord);
1332 		}
1333 	}
1334 	*mp = NULL;
1335 
1336 	SBLASTRECORDCHK(&so->so_rcv, "somove 3");
1337 	SBLASTMBUFCHK(&so->so_rcv, "somove 3");
1338 	SBCHECK(&so->so_rcv);
1339 	if (m == NULL)
1340 		goto release;
1341 	m->m_nextpkt = NULL;
1342 	if (m->m_flags & M_PKTHDR) {
1343 		m_resethdr(m);
1344 		m->m_pkthdr.len = len;
1345 	}
1346 
1347 	/* Send window update to source peer as receive buffer has changed. */
1348 	if (so->so_proto->pr_flags & PR_WANTRCVD && so->so_pcb)
1349 		(so->so_proto->pr_usrreq)(so, PRU_RCVD, NULL,
1350 		    NULL, NULL, NULL);
1351 
1352 	/* Receive buffer did shrink by len bytes, adjust oob. */
1353 	state = so->so_state;
1354 	so->so_state &= ~SS_RCVATMARK;
1355 	oobmark = so->so_oobmark;
1356 	so->so_oobmark = oobmark > len ? oobmark - len : 0;
1357 	if (oobmark) {
1358 		if (oobmark == len)
1359 			so->so_state |= SS_RCVATMARK;
1360 		if (oobmark >= len)
1361 			oobmark = 0;
1362 	}
1363 
1364 	/*
1365 	 * Handle oob data.  If any malloc fails, ignore error.
1366 	 * TCP urgent data is not very reliable anyway.
1367 	 */
1368 	while (((state & SS_RCVATMARK) || oobmark) &&
1369 	    (so->so_options & SO_OOBINLINE)) {
1370 		struct mbuf *o = NULL;
1371 
1372 		if (state & SS_RCVATMARK) {
1373 			o = m_get(wait, MT_DATA);
1374 			state &= ~SS_RCVATMARK;
1375 		} else if (oobmark) {
1376 			o = m_split(m, oobmark, wait);
1377 			if (o) {
1378 				error = (*sosp->so_proto->pr_usrreq)(sosp,
1379 				    PRU_SEND, m, NULL, NULL, NULL);
1380 				if (error) {
1381 					if (sosp->so_state & SS_CANTSENDMORE)
1382 						error = EPIPE;
1383 					m_freem(o);
1384 					goto release;
1385 				}
1386 				len -= oobmark;
1387 				so->so_splicelen += oobmark;
1388 				m = o;
1389 				o = m_get(wait, MT_DATA);
1390 			}
1391 			oobmark = 0;
1392 		}
1393 		if (o) {
1394 			o->m_len = 1;
1395 			*mtod(o, caddr_t) = *mtod(m, caddr_t);
1396 			error = (*sosp->so_proto->pr_usrreq)(sosp, PRU_SENDOOB,
1397 			    o, NULL, NULL, NULL);
1398 			if (error) {
1399 				if (sosp->so_state & SS_CANTSENDMORE)
1400 					error = EPIPE;
1401 				m_freem(m);
1402 				goto release;
1403 			}
1404 			len -= 1;
1405 			so->so_splicelen += 1;
1406 			if (oobmark) {
1407 				oobmark -= 1;
1408 				if (oobmark == 0)
1409 					state |= SS_RCVATMARK;
1410 			}
1411 			m_adj(m, 1);
1412 		}
1413 	}
1414 
1415 	/* Append all remaining data to drain socket. */
1416 	if (so->so_rcv.sb_cc == 0 || maxreached)
1417 		sosp->so_state &= ~SS_ISSENDING;
1418 	error = (*sosp->so_proto->pr_usrreq)(sosp, PRU_SEND, m, NULL, NULL,
1419 	    NULL);
1420 	if (error) {
1421 		if (sosp->so_state & SS_CANTSENDMORE)
1422 			error = EPIPE;
1423 		goto release;
1424 	}
1425 	so->so_splicelen += len;
1426 
1427 	/* Move several packets if possible. */
1428 	if (!maxreached && nextrecord)
1429 		goto nextpkt;
1430 
1431  release:
1432 	sosp->so_state &= ~SS_ISSENDING;
1433 	if (!error && maxreached && so->so_splicemax == so->so_splicelen)
1434 		error = EFBIG;
1435 	if (error)
1436 		so->so_error = error;
1437 	if (((so->so_state & SS_CANTRCVMORE) && so->so_rcv.sb_cc == 0) ||
1438 	    (sosp->so_state & SS_CANTSENDMORE) || maxreached || error) {
1439 		sounsplice(so, sosp, 1);
1440 		return (0);
1441 	}
1442 	if (timerisset(&so->so_idletv))
1443 		timeout_add_tv(&so->so_idleto, &so->so_idletv);
1444 	return (1);
1445 }
1446 
1447 #undef so_splicelen
1448 #undef so_splicemax
1449 #undef so_idletv
1450 #undef so_idleto
1451 
1452 #endif /* SOCKET_SPLICE */
1453 
1454 void
1455 sorwakeup(struct socket *so)
1456 {
1457 #ifdef SOCKET_SPLICE
1458 	if (so->so_rcv.sb_flagsintr & SB_SPLICE)
1459 		(void) somove(so, M_DONTWAIT);
1460 	if (isspliced(so))
1461 		return;
1462 #endif
1463 	sowakeup(so, &so->so_rcv);
1464 	if (so->so_upcall)
1465 		(*(so->so_upcall))(so, so->so_upcallarg, M_DONTWAIT);
1466 }
1467 
1468 void
1469 sowwakeup(struct socket *so)
1470 {
1471 #ifdef SOCKET_SPLICE
1472 	if (so->so_snd.sb_flagsintr & SB_SPLICE)
1473 		(void) somove(so->so_sp->ssp_soback, M_DONTWAIT);
1474 #endif
1475 	sowakeup(so, &so->so_snd);
1476 }
1477 
1478 int
1479 sosetopt(struct socket *so, int level, int optname, struct mbuf *m0)
1480 {
1481 	int error = 0;
1482 	struct mbuf *m = m0;
1483 
1484 	if (level != SOL_SOCKET) {
1485 		if (so->so_proto && so->so_proto->pr_ctloutput)
1486 			return ((*so->so_proto->pr_ctloutput)
1487 				  (PRCO_SETOPT, so, level, optname, &m0));
1488 		error = ENOPROTOOPT;
1489 	} else {
1490 		switch (optname) {
1491 		case SO_BINDANY:
1492 			if ((error = suser(curproc, 0)) != 0)	/* XXX */
1493 				goto bad;
1494 			break;
1495 		}
1496 
1497 		switch (optname) {
1498 
1499 		case SO_LINGER:
1500 			if (m == NULL || m->m_len != sizeof (struct linger) ||
1501 			    mtod(m, struct linger *)->l_linger < 0 ||
1502 			    mtod(m, struct linger *)->l_linger > SHRT_MAX) {
1503 				error = EINVAL;
1504 				goto bad;
1505 			}
1506 			so->so_linger = mtod(m, struct linger *)->l_linger;
1507 			/* FALLTHROUGH */
1508 
1509 		case SO_BINDANY:
1510 		case SO_DEBUG:
1511 		case SO_KEEPALIVE:
1512 		case SO_USELOOPBACK:
1513 		case SO_BROADCAST:
1514 		case SO_REUSEADDR:
1515 		case SO_REUSEPORT:
1516 		case SO_OOBINLINE:
1517 		case SO_TIMESTAMP:
1518 			if (m == NULL || m->m_len < sizeof (int)) {
1519 				error = EINVAL;
1520 				goto bad;
1521 			}
1522 			if (*mtod(m, int *))
1523 				so->so_options |= optname;
1524 			else
1525 				so->so_options &= ~optname;
1526 			break;
1527 
1528 		case SO_DONTROUTE:
1529 			if (m == NULL || m->m_len < sizeof (int)) {
1530 				error = EINVAL;
1531 				goto bad;
1532 			}
1533 			if (*mtod(m, int *))
1534 				error = EOPNOTSUPP;
1535 			break;
1536 
1537 		case SO_SNDBUF:
1538 		case SO_RCVBUF:
1539 		case SO_SNDLOWAT:
1540 		case SO_RCVLOWAT:
1541 		    {
1542 			u_long cnt;
1543 
1544 			if (m == NULL || m->m_len < sizeof (int)) {
1545 				error = EINVAL;
1546 				goto bad;
1547 			}
1548 			cnt = *mtod(m, int *);
1549 			if ((long)cnt <= 0)
1550 				cnt = 1;
1551 			switch (optname) {
1552 
1553 			case SO_SNDBUF:
1554 				if (so->so_state & SS_CANTSENDMORE) {
1555 					error = EINVAL;
1556 					goto bad;
1557 				}
1558 				if (sbcheckreserve(cnt, so->so_snd.sb_wat) ||
1559 				    sbreserve(&so->so_snd, cnt)) {
1560 					error = ENOBUFS;
1561 					goto bad;
1562 				}
1563 				so->so_snd.sb_wat = cnt;
1564 				break;
1565 
1566 			case SO_RCVBUF:
1567 				if (so->so_state & SS_CANTRCVMORE) {
1568 					error = EINVAL;
1569 					goto bad;
1570 				}
1571 				if (sbcheckreserve(cnt, so->so_rcv.sb_wat) ||
1572 				    sbreserve(&so->so_rcv, cnt)) {
1573 					error = ENOBUFS;
1574 					goto bad;
1575 				}
1576 				so->so_rcv.sb_wat = cnt;
1577 				break;
1578 
1579 			case SO_SNDLOWAT:
1580 				so->so_snd.sb_lowat =
1581 				    (cnt > so->so_snd.sb_hiwat) ?
1582 				    so->so_snd.sb_hiwat : cnt;
1583 				break;
1584 			case SO_RCVLOWAT:
1585 				so->so_rcv.sb_lowat =
1586 				    (cnt > so->so_rcv.sb_hiwat) ?
1587 				    so->so_rcv.sb_hiwat : cnt;
1588 				break;
1589 			}
1590 			break;
1591 		    }
1592 
1593 		case SO_SNDTIMEO:
1594 		case SO_RCVTIMEO:
1595 		    {
1596 			struct timeval tv;
1597 			int val;
1598 
1599 			if (m == NULL || m->m_len < sizeof (tv)) {
1600 				error = EINVAL;
1601 				goto bad;
1602 			}
1603 			memcpy(&tv, mtod(m, struct timeval *), sizeof tv);
1604 			val = tvtohz(&tv);
1605 			if (val > USHRT_MAX) {
1606 				error = EDOM;
1607 				goto bad;
1608 			}
1609 
1610 			switch (optname) {
1611 
1612 			case SO_SNDTIMEO:
1613 				so->so_snd.sb_timeo = val;
1614 				break;
1615 			case SO_RCVTIMEO:
1616 				so->so_rcv.sb_timeo = val;
1617 				break;
1618 			}
1619 			break;
1620 		    }
1621 
1622 		case SO_RTABLE:
1623 			if (so->so_proto && so->so_proto->pr_domain &&
1624 			    so->so_proto->pr_domain->dom_protosw &&
1625 			    so->so_proto->pr_ctloutput) {
1626 				struct domain *dom = so->so_proto->pr_domain;
1627 
1628 				level = dom->dom_protosw->pr_protocol;
1629 				return ((*so->so_proto->pr_ctloutput)
1630 				    (PRCO_SETOPT, so, level, optname, &m0));
1631 			}
1632 			error = ENOPROTOOPT;
1633 			break;
1634 
1635 #ifdef SOCKET_SPLICE
1636 		case SO_SPLICE:
1637 			if (m == NULL) {
1638 				error = sosplice(so, -1, 0, NULL);
1639 			} else if (m->m_len < sizeof(int)) {
1640 				error = EINVAL;
1641 				goto bad;
1642 			} else if (m->m_len < sizeof(struct splice)) {
1643 				error = sosplice(so, *mtod(m, int *), 0, NULL);
1644 			} else {
1645 				error = sosplice(so,
1646 				    mtod(m, struct splice *)->sp_fd,
1647 				    mtod(m, struct splice *)->sp_max,
1648 				   &mtod(m, struct splice *)->sp_idle);
1649 			}
1650 			break;
1651 #endif /* SOCKET_SPLICE */
1652 
1653 		default:
1654 			error = ENOPROTOOPT;
1655 			break;
1656 		}
1657 		if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1658 			(void) ((*so->so_proto->pr_ctloutput)
1659 				  (PRCO_SETOPT, so, level, optname, &m0));
1660 			m = NULL;	/* freed by protocol */
1661 		}
1662 	}
1663 bad:
1664 	if (m)
1665 		(void) m_free(m);
1666 	return (error);
1667 }
1668 
1669 int
1670 sogetopt(struct socket *so, int level, int optname, struct mbuf **mp)
1671 {
1672 	struct mbuf *m;
1673 
1674 	if (level != SOL_SOCKET) {
1675 		if (so->so_proto && so->so_proto->pr_ctloutput) {
1676 			return ((*so->so_proto->pr_ctloutput)
1677 				  (PRCO_GETOPT, so, level, optname, mp));
1678 		} else
1679 			return (ENOPROTOOPT);
1680 	} else {
1681 		m = m_get(M_WAIT, MT_SOOPTS);
1682 		m->m_len = sizeof (int);
1683 
1684 		switch (optname) {
1685 
1686 		case SO_LINGER:
1687 			m->m_len = sizeof (struct linger);
1688 			mtod(m, struct linger *)->l_onoff =
1689 				so->so_options & SO_LINGER;
1690 			mtod(m, struct linger *)->l_linger = so->so_linger;
1691 			break;
1692 
1693 		case SO_BINDANY:
1694 		case SO_USELOOPBACK:
1695 		case SO_DEBUG:
1696 		case SO_KEEPALIVE:
1697 		case SO_REUSEADDR:
1698 		case SO_REUSEPORT:
1699 		case SO_BROADCAST:
1700 		case SO_OOBINLINE:
1701 		case SO_TIMESTAMP:
1702 			*mtod(m, int *) = so->so_options & optname;
1703 			break;
1704 
1705 		case SO_DONTROUTE:
1706 			*mtod(m, int *) = 0;
1707 			break;
1708 
1709 		case SO_TYPE:
1710 			*mtod(m, int *) = so->so_type;
1711 			break;
1712 
1713 		case SO_ERROR:
1714 			*mtod(m, int *) = so->so_error;
1715 			so->so_error = 0;
1716 			break;
1717 
1718 		case SO_SNDBUF:
1719 			*mtod(m, int *) = so->so_snd.sb_hiwat;
1720 			break;
1721 
1722 		case SO_RCVBUF:
1723 			*mtod(m, int *) = so->so_rcv.sb_hiwat;
1724 			break;
1725 
1726 		case SO_SNDLOWAT:
1727 			*mtod(m, int *) = so->so_snd.sb_lowat;
1728 			break;
1729 
1730 		case SO_RCVLOWAT:
1731 			*mtod(m, int *) = so->so_rcv.sb_lowat;
1732 			break;
1733 
1734 		case SO_SNDTIMEO:
1735 		case SO_RCVTIMEO:
1736 		    {
1737 			struct timeval tv;
1738 			int val = (optname == SO_SNDTIMEO ?
1739 			    so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1740 
1741 			m->m_len = sizeof(struct timeval);
1742 			memset(&tv, 0, sizeof(tv));
1743 			tv.tv_sec = val / hz;
1744 			tv.tv_usec = (val % hz) * tick;
1745 			memcpy(mtod(m, struct timeval *), &tv, sizeof tv);
1746 			break;
1747 		    }
1748 
1749 		case SO_RTABLE:
1750 			(void)m_free(m);
1751 			if (so->so_proto && so->so_proto->pr_domain &&
1752 			    so->so_proto->pr_domain->dom_protosw &&
1753 			    so->so_proto->pr_ctloutput) {
1754 				struct domain *dom = so->so_proto->pr_domain;
1755 
1756 				level = dom->dom_protosw->pr_protocol;
1757 				return ((*so->so_proto->pr_ctloutput)
1758 				    (PRCO_GETOPT, so, level, optname, mp));
1759 			}
1760 			return (ENOPROTOOPT);
1761 			break;
1762 
1763 #ifdef SOCKET_SPLICE
1764 		case SO_SPLICE:
1765 		    {
1766 			off_t len;
1767 			int s = splsoftnet();
1768 
1769 			m->m_len = sizeof(off_t);
1770 			len = so->so_sp ? so->so_sp->ssp_len : 0;
1771 			memcpy(mtod(m, off_t *), &len, sizeof(off_t));
1772 			splx(s);
1773 			break;
1774 		    }
1775 #endif /* SOCKET_SPLICE */
1776 
1777 		case SO_PEERCRED:
1778 			if (so->so_proto->pr_protocol == AF_UNIX) {
1779 				struct unpcb *unp = sotounpcb(so);
1780 
1781 				if (unp->unp_flags & UNP_FEIDS) {
1782 					m->m_len = sizeof(unp->unp_connid);
1783 					memcpy(mtod(m, caddr_t),
1784 					    &(unp->unp_connid), m->m_len);
1785 					break;
1786 				}
1787 				(void)m_free(m);
1788 				return (ENOTCONN);
1789 			}
1790 			(void)m_free(m);
1791 			return (EOPNOTSUPP);
1792 			break;
1793 
1794 		default:
1795 			(void)m_free(m);
1796 			return (ENOPROTOOPT);
1797 		}
1798 		*mp = m;
1799 		return (0);
1800 	}
1801 }
1802 
1803 void
1804 sohasoutofband(struct socket *so)
1805 {
1806 	csignal(so->so_pgid, SIGURG, so->so_siguid, so->so_sigeuid);
1807 	selwakeup(&so->so_rcv.sb_sel);
1808 }
1809 
1810 int
1811 soo_kqfilter(struct file *fp, struct knote *kn)
1812 {
1813 	struct socket *so = kn->kn_fp->f_data;
1814 	struct sockbuf *sb;
1815 	int s;
1816 
1817 	switch (kn->kn_filter) {
1818 	case EVFILT_READ:
1819 		if (so->so_options & SO_ACCEPTCONN)
1820 			kn->kn_fop = &solisten_filtops;
1821 		else
1822 			kn->kn_fop = &soread_filtops;
1823 		sb = &so->so_rcv;
1824 		break;
1825 	case EVFILT_WRITE:
1826 		kn->kn_fop = &sowrite_filtops;
1827 		sb = &so->so_snd;
1828 		break;
1829 	default:
1830 		return (EINVAL);
1831 	}
1832 
1833 	s = splnet();
1834 	SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
1835 	sb->sb_flags |= SB_KNOTE;
1836 	splx(s);
1837 	return (0);
1838 }
1839 
1840 void
1841 filt_sordetach(struct knote *kn)
1842 {
1843 	struct socket *so = kn->kn_fp->f_data;
1844 	int s = splnet();
1845 
1846 	SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1847 	if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1848 		so->so_rcv.sb_flags &= ~SB_KNOTE;
1849 	splx(s);
1850 }
1851 
1852 int
1853 filt_soread(struct knote *kn, long hint)
1854 {
1855 	struct socket *so = kn->kn_fp->f_data;
1856 
1857 	kn->kn_data = so->so_rcv.sb_cc;
1858 #ifdef SOCKET_SPLICE
1859 	if (isspliced(so))
1860 		return (0);
1861 #endif /* SOCKET_SPLICE */
1862 	if (so->so_state & SS_CANTRCVMORE) {
1863 		kn->kn_flags |= EV_EOF;
1864 		kn->kn_fflags = so->so_error;
1865 		return (1);
1866 	}
1867 	if (so->so_error)	/* temporary udp error */
1868 		return (1);
1869 	if (kn->kn_sfflags & NOTE_LOWAT)
1870 		return (kn->kn_data >= kn->kn_sdata);
1871 	return (kn->kn_data >= so->so_rcv.sb_lowat);
1872 }
1873 
1874 void
1875 filt_sowdetach(struct knote *kn)
1876 {
1877 	struct socket *so = kn->kn_fp->f_data;
1878 	int s = splnet();
1879 
1880 	SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1881 	if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1882 		so->so_snd.sb_flags &= ~SB_KNOTE;
1883 	splx(s);
1884 }
1885 
1886 int
1887 filt_sowrite(struct knote *kn, long hint)
1888 {
1889 	struct socket *so = kn->kn_fp->f_data;
1890 
1891 	kn->kn_data = sbspace(&so->so_snd);
1892 	if (so->so_state & SS_CANTSENDMORE) {
1893 		kn->kn_flags |= EV_EOF;
1894 		kn->kn_fflags = so->so_error;
1895 		return (1);
1896 	}
1897 	if (so->so_error)	/* temporary udp error */
1898 		return (1);
1899 	if (((so->so_state & SS_ISCONNECTED) == 0) &&
1900 	    (so->so_proto->pr_flags & PR_CONNREQUIRED))
1901 		return (0);
1902 	if (kn->kn_sfflags & NOTE_LOWAT)
1903 		return (kn->kn_data >= kn->kn_sdata);
1904 	return (kn->kn_data >= so->so_snd.sb_lowat);
1905 }
1906 
1907 int
1908 filt_solisten(struct knote *kn, long hint)
1909 {
1910 	struct socket *so = kn->kn_fp->f_data;
1911 
1912 	kn->kn_data = so->so_qlen;
1913 	return (so->so_qlen != 0);
1914 }
1915 
1916 #ifdef DDB
1917 void
1918 sobuf_print(struct sockbuf *,
1919     int (*)(const char *, ...) __attribute__((__format__(__kprintf__,1,2))));
1920 
1921 void
1922 sobuf_print(struct sockbuf *sb,
1923     int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2))))
1924 {
1925 	(*pr)("\tsb_cc: %lu\n", sb->sb_cc);
1926 	(*pr)("\tsb_datacc: %lu\n", sb->sb_datacc);
1927 	(*pr)("\tsb_hiwat: %lu\n", sb->sb_hiwat);
1928 	(*pr)("\tsb_wat: %lu\n", sb->sb_wat);
1929 	(*pr)("\tsb_mbcnt: %lu\n", sb->sb_mbcnt);
1930 	(*pr)("\tsb_mbmax: %lu\n", sb->sb_mbmax);
1931 	(*pr)("\tsb_lowat: %ld\n", sb->sb_lowat);
1932 	(*pr)("\tsb_mb: %p\n", sb->sb_mb);
1933 	(*pr)("\tsb_mbtail: %p\n", sb->sb_mbtail);
1934 	(*pr)("\tsb_lastrecord: %p\n", sb->sb_lastrecord);
1935 	(*pr)("\tsb_sel: ...\n");
1936 	(*pr)("\tsb_flagsintr: %d\n", sb->sb_flagsintr);
1937 	(*pr)("\tsb_flags: %i\n", sb->sb_flags);
1938 	(*pr)("\tsb_timeo: %i\n", sb->sb_timeo);
1939 }
1940 
1941 void
1942 so_print(void *v,
1943     int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2))))
1944 {
1945 	struct socket *so = v;
1946 
1947 	(*pr)("socket %p\n", so);
1948 	(*pr)("so_type: %i\n", so->so_type);
1949 	(*pr)("so_options: 0x%04x\n", so->so_options); /* %b */
1950 	(*pr)("so_linger: %i\n", so->so_linger);
1951 	(*pr)("so_state: %i\n", so->so_state);
1952 	(*pr)("so_pcb: %p\n", so->so_pcb);
1953 	(*pr)("so_proto: %p\n", so->so_proto);
1954 
1955 	(*pr)("so_head: %p\n", so->so_head);
1956 	(*pr)("so_onq: %p\n", so->so_onq);
1957 	(*pr)("so_q0: @%p first: %p\n", &so->so_q0, TAILQ_FIRST(&so->so_q0));
1958 	(*pr)("so_q: @%p first: %p\n", &so->so_q, TAILQ_FIRST(&so->so_q));
1959 	(*pr)("so_eq: next: %p\n", TAILQ_NEXT(so, so_qe));
1960 	(*pr)("so_q0len: %i\n", so->so_q0len);
1961 	(*pr)("so_qlen: %i\n", so->so_qlen);
1962 	(*pr)("so_qlimit: %i\n", so->so_qlimit);
1963 	(*pr)("so_timeo: %i\n", so->so_timeo);
1964 	(*pr)("so_pgid: %i\n", so->so_pgid);
1965 	(*pr)("so_siguid: %i\n", so->so_siguid);
1966 	(*pr)("so_sigeuid: %i\n", so->so_sigeuid);
1967 	(*pr)("so_obmark: %lu\n", so->so_oobmark);
1968 
1969 	(*pr)("so_sp: %p\n", so->so_sp);
1970 	if (so->so_sp != NULL) {
1971 		(*pr)("\tssp_socket: %p\n", so->so_sp->ssp_socket);
1972 		(*pr)("\tssp_soback: %p\n", so->so_sp->ssp_soback);
1973 		(*pr)("\tssp_len: %lld\n",
1974 		    (unsigned long long)so->so_sp->ssp_len);
1975 		(*pr)("\tssp_max: %lld\n",
1976 		    (unsigned long long)so->so_sp->ssp_max);
1977 		(*pr)("\tssp_idletv: %lld %ld\n", so->so_sp->ssp_idletv.tv_sec,
1978 		    so->so_sp->ssp_idletv.tv_usec);
1979 		(*pr)("\tssp_idleto: %spending (@%i)\n",
1980 		    timeout_pending(&so->so_sp->ssp_idleto) ? "" : "not ",
1981 		    so->so_sp->ssp_idleto.to_time);
1982 	}
1983 
1984 	(*pr)("so_rcv:\n");
1985 	sobuf_print(&so->so_rcv, pr);
1986 	(*pr)("so_snd:\n");
1987 	sobuf_print(&so->so_snd, pr);
1988 
1989 	(*pr)("so_upcall: %p so_upcallarg: %p\n",
1990 	    so->so_upcall, so->so_upcallarg);
1991 
1992 	(*pr)("so_euid: %d so_ruid: %d\n", so->so_euid, so->so_ruid);
1993 	(*pr)("so_egid: %d so_rgid: %d\n", so->so_egid, so->so_rgid);
1994 	(*pr)("so_cpid: %d\n", so->so_cpid);
1995 }
1996 #endif
1997 
1998