xref: /freebsd/sys/kern/uipc_socket.c (revision 5608fd23c27fa1e8ee595d7b678cbfd35d657fbe)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3  *	The Regents of the University of California.
4  * Copyright (c) 2004 The FreeBSD Foundation
5  * Copyright (c) 2004-2008 Robert N. M. Watson
6  * 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  * 4. 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 /*
36  * Comments on the socket life cycle:
37  *
38  * soalloc() sets of socket layer state for a socket, called only by
39  * socreate() and sonewconn().  Socket layer private.
40  *
41  * sodealloc() tears down socket layer state for a socket, called only by
42  * sofree() and sonewconn().  Socket layer private.
43  *
44  * pru_attach() associates protocol layer state with an allocated socket;
45  * called only once, may fail, aborting socket allocation.  This is called
46  * from socreate() and sonewconn().  Socket layer private.
47  *
48  * pru_detach() disassociates protocol layer state from an attached socket,
49  * and will be called exactly once for sockets in which pru_attach() has
50  * been successfully called.  If pru_attach() returned an error,
51  * pru_detach() will not be called.  Socket layer private.
52  *
53  * pru_abort() and pru_close() notify the protocol layer that the last
54  * consumer of a socket is starting to tear down the socket, and that the
55  * protocol should terminate the connection.  Historically, pru_abort() also
56  * detached protocol state from the socket state, but this is no longer the
57  * case.
58  *
59  * socreate() creates a socket and attaches protocol state.  This is a public
60  * interface that may be used by socket layer consumers to create new
61  * sockets.
62  *
63  * sonewconn() creates a socket and attaches protocol state.  This is a
64  * public interface  that may be used by protocols to create new sockets when
65  * a new connection is received and will be available for accept() on a
66  * listen socket.
67  *
68  * soclose() destroys a socket after possibly waiting for it to disconnect.
69  * This is a public interface that socket consumers should use to close and
70  * release a socket when done with it.
71  *
72  * soabort() destroys a socket without waiting for it to disconnect (used
73  * only for incoming connections that are already partially or fully
74  * connected).  This is used internally by the socket layer when clearing
75  * listen socket queues (due to overflow or close on the listen socket), but
76  * is also a public interface protocols may use to abort connections in
77  * their incomplete listen queues should they no longer be required.  Sockets
78  * placed in completed connection listen queues should not be aborted for
79  * reasons described in the comment above the soclose() implementation.  This
80  * is not a general purpose close routine, and except in the specific
81  * circumstances described here, should not be used.
82  *
83  * sofree() will free a socket and its protocol state if all references on
84  * the socket have been released, and is the public interface to attempt to
85  * free a socket when a reference is removed.  This is a socket layer private
86  * interface.
87  *
88  * NOTE: In addition to socreate() and soclose(), which provide a single
89  * socket reference to the consumer to be managed as required, there are two
90  * calls to explicitly manage socket references, soref(), and sorele().
91  * Currently, these are generally required only when transitioning a socket
92  * from a listen queue to a file descriptor, in order to prevent garbage
93  * collection of the socket at an untimely moment.  For a number of reasons,
94  * these interfaces are not preferred, and should be avoided.
95  *
96  * NOTE: With regard to VNETs the general rule is that callers do not set
97  * curvnet. Exceptions to this rule include soabort(), sodisconnect(),
98  * sofree() (and with that sorele(), sotryfree()), as well as sonewconn()
99  * and sorflush(), which are usually called from a pre-set VNET context.
100  * sopoll() currently does not need a VNET context to be set.
101  */
102 
103 #include <sys/cdefs.h>
104 __FBSDID("$FreeBSD$");
105 
106 #include "opt_inet.h"
107 #include "opt_inet6.h"
108 #include "opt_compat.h"
109 
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/fcntl.h>
113 #include <sys/limits.h>
114 #include <sys/lock.h>
115 #include <sys/mac.h>
116 #include <sys/malloc.h>
117 #include <sys/mbuf.h>
118 #include <sys/mutex.h>
119 #include <sys/domain.h>
120 #include <sys/file.h>			/* for struct knote */
121 #include <sys/hhook.h>
122 #include <sys/kernel.h>
123 #include <sys/khelp.h>
124 #include <sys/event.h>
125 #include <sys/eventhandler.h>
126 #include <sys/poll.h>
127 #include <sys/proc.h>
128 #include <sys/protosw.h>
129 #include <sys/socket.h>
130 #include <sys/socketvar.h>
131 #include <sys/resourcevar.h>
132 #include <net/route.h>
133 #include <sys/signalvar.h>
134 #include <sys/stat.h>
135 #include <sys/sx.h>
136 #include <sys/sysctl.h>
137 #include <sys/uio.h>
138 #include <sys/jail.h>
139 #include <sys/syslog.h>
140 #include <netinet/in.h>
141 
142 #include <net/vnet.h>
143 
144 #include <security/mac/mac_framework.h>
145 
146 #include <vm/uma.h>
147 
148 #ifdef COMPAT_FREEBSD32
149 #include <sys/mount.h>
150 #include <sys/sysent.h>
151 #include <compat/freebsd32/freebsd32.h>
152 #endif
153 
154 static int	soreceive_rcvoob(struct socket *so, struct uio *uio,
155 		    int flags);
156 
157 static void	filt_sordetach(struct knote *kn);
158 static int	filt_soread(struct knote *kn, long hint);
159 static void	filt_sowdetach(struct knote *kn);
160 static int	filt_sowrite(struct knote *kn, long hint);
161 static int	filt_solisten(struct knote *kn, long hint);
162 static int inline hhook_run_socket(struct socket *so, void *hctx, int32_t h_id);
163 
164 static struct filterops solisten_filtops = {
165 	.f_isfd = 1,
166 	.f_detach = filt_sordetach,
167 	.f_event = filt_solisten,
168 };
169 static struct filterops soread_filtops = {
170 	.f_isfd = 1,
171 	.f_detach = filt_sordetach,
172 	.f_event = filt_soread,
173 };
174 static struct filterops sowrite_filtops = {
175 	.f_isfd = 1,
176 	.f_detach = filt_sowdetach,
177 	.f_event = filt_sowrite,
178 };
179 
180 so_gen_t	so_gencnt;	/* generation count for sockets */
181 
182 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
183 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
184 
185 #define	VNET_SO_ASSERT(so)						\
186 	VNET_ASSERT(curvnet != NULL,					\
187 	    ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so)));
188 
189 VNET_DEFINE(struct hhook_head *, socket_hhh[HHOOK_SOCKET_LAST + 1]);
190 #define	V_socket_hhh		VNET(socket_hhh)
191 
192 /*
193  * Limit on the number of connections in the listen queue waiting
194  * for accept(2).
195  * NB: The orginal sysctl somaxconn is still available but hidden
196  * to prevent confusion about the actual purpose of this number.
197  */
198 static int somaxconn = SOMAXCONN;
199 
200 static int
201 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
202 {
203 	int error;
204 	int val;
205 
206 	val = somaxconn;
207 	error = sysctl_handle_int(oidp, &val, 0, req);
208 	if (error || !req->newptr )
209 		return (error);
210 
211 	if (val < 1 || val > USHRT_MAX)
212 		return (EINVAL);
213 
214 	somaxconn = val;
215 	return (0);
216 }
217 SYSCTL_PROC(_kern_ipc, OID_AUTO, soacceptqueue, CTLTYPE_UINT | CTLFLAG_RW,
218     0, sizeof(int), sysctl_somaxconn, "I",
219     "Maximum listen socket pending connection accept queue size");
220 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn,
221     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_SKIP,
222     0, sizeof(int), sysctl_somaxconn, "I",
223     "Maximum listen socket pending connection accept queue size (compat)");
224 
225 static int numopensockets;
226 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
227     &numopensockets, 0, "Number of open sockets");
228 
229 /*
230  * accept_mtx locks down per-socket fields relating to accept queues.  See
231  * socketvar.h for an annotation of the protected fields of struct socket.
232  */
233 struct mtx accept_mtx;
234 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
235 
236 /*
237  * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
238  * so_gencnt field.
239  */
240 static struct mtx so_global_mtx;
241 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
242 
243 /*
244  * General IPC sysctl name space, used by sockets and a variety of other IPC
245  * types.
246  */
247 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
248 
249 /*
250  * Initialize the socket subsystem and set up the socket
251  * memory allocator.
252  */
253 static uma_zone_t socket_zone;
254 int	maxsockets;
255 
256 static void
257 socket_zone_change(void *tag)
258 {
259 
260 	maxsockets = uma_zone_set_max(socket_zone, maxsockets);
261 }
262 
263 static void
264 socket_hhook_register(int subtype)
265 {
266 
267 	if (hhook_head_register(HHOOK_TYPE_SOCKET, subtype,
268 	    &V_socket_hhh[subtype],
269 	    HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0)
270 		printf("%s: WARNING: unable to register hook\n", __func__);
271 }
272 
273 static void
274 socket_init(void *tag)
275 {
276 	int i;
277 
278 	socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
279 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
280 	maxsockets = uma_zone_set_max(socket_zone, maxsockets);
281 	uma_zone_set_warning(socket_zone, "kern.ipc.maxsockets limit reached");
282 	EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
283 	    EVENTHANDLER_PRI_FIRST);
284 
285 	/* We expect a contiguous range */
286 	for (i = 0; i <= HHOOK_SOCKET_LAST; i++) {
287 		socket_hhook_register(i);
288 	}
289 }
290 SYSINIT(socket, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, socket_init, NULL);
291 
292 /*
293  * Initialise maxsockets.  This SYSINIT must be run after
294  * tunable_mbinit().
295  */
296 static void
297 init_maxsockets(void *ignored)
298 {
299 
300 	TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
301 	maxsockets = imax(maxsockets, maxfiles);
302 }
303 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
304 
305 /*
306  * Sysctl to get and set the maximum global sockets limit.  Notify protocols
307  * of the change so that they can update their dependent limits as required.
308  */
309 static int
310 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
311 {
312 	int error, newmaxsockets;
313 
314 	newmaxsockets = maxsockets;
315 	error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
316 	if (error == 0 && req->newptr) {
317 		if (newmaxsockets > maxsockets &&
318 		    newmaxsockets <= maxfiles) {
319 			maxsockets = newmaxsockets;
320 			EVENTHANDLER_INVOKE(maxsockets_change);
321 		} else
322 			error = EINVAL;
323 	}
324 	return (error);
325 }
326 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
327     &maxsockets, 0, sysctl_maxsockets, "IU",
328     "Maximum number of sockets avaliable");
329 
330 /*
331  * Socket operation routines.  These routines are called by the routines in
332  * sys_socket.c or from a system process, and implement the semantics of
333  * socket operations by switching out to the protocol specific routines.
334  */
335 
336 /*
337  * Get a socket structure from our zone, and initialize it.  Note that it
338  * would probably be better to allocate socket and PCB at the same time, but
339  * I'm not convinced that all the protocols can be easily modified to do
340  * this.
341  *
342  * soalloc() returns a socket with a ref count of 0.
343  */
344 static struct socket *
345 soalloc(struct vnet *vnet)
346 {
347 	struct socket *so;
348 
349 	so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
350 	if (so == NULL)
351 		return (NULL);
352 #ifdef MAC
353 	if (mac_socket_init(so, M_NOWAIT) != 0) {
354 		uma_zfree(socket_zone, so);
355 		return (NULL);
356 	}
357 #endif
358 	if (khelp_init_osd(HELPER_CLASS_SOCKET, &so->osd)) {
359 		uma_zfree(socket_zone, so);
360 		return (NULL);
361 	}
362 
363 	SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
364 	SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
365 	sx_init(&so->so_snd.sb_sx, "so_snd_sx");
366 	sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
367 	TAILQ_INIT(&so->so_aiojobq);
368 	mtx_lock(&so_global_mtx);
369 	so->so_gencnt = ++so_gencnt;
370 	++numopensockets;
371 #ifdef VIMAGE
372 	VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p",
373 	    __func__, __LINE__, so));
374 	vnet->vnet_sockcnt++;
375 	so->so_vnet = vnet;
376 #endif
377 	mtx_unlock(&so_global_mtx);
378 
379 	/* We shouldn't need the so_global_mtx */
380 	if (V_socket_hhh[HHOOK_SOCKET_CREATE]->hhh_nhooks > 0) {
381 		if (hhook_run_socket(so, NULL, HHOOK_SOCKET_CREATE))
382 			/* Do we need more comprehensive error returns? */
383 			return (NULL);
384 	}
385 	return (so);
386 }
387 
388 /*
389  * Free the storage associated with a socket at the socket layer, tear down
390  * locks, labels, etc.  All protocol state is assumed already to have been
391  * torn down (and possibly never set up) by the caller.
392  */
393 static void
394 sodealloc(struct socket *so)
395 {
396 
397 	KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
398 	KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
399 
400 	mtx_lock(&so_global_mtx);
401 	so->so_gencnt = ++so_gencnt;
402 	--numopensockets;	/* Could be below, but faster here. */
403 #ifdef VIMAGE
404 	VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p",
405 	    __func__, __LINE__, so));
406 	so->so_vnet->vnet_sockcnt--;
407 #endif
408 	mtx_unlock(&so_global_mtx);
409 	if (so->so_rcv.sb_hiwat)
410 		(void)chgsbsize(so->so_cred->cr_uidinfo,
411 		    &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
412 	if (so->so_snd.sb_hiwat)
413 		(void)chgsbsize(so->so_cred->cr_uidinfo,
414 		    &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
415 	/* remove acccept filter if one is present. */
416 	if (so->so_accf != NULL)
417 		do_setopt_accept_filter(so, NULL);
418 #ifdef MAC
419 	mac_socket_destroy(so);
420 #endif
421 	if (V_socket_hhh[HHOOK_SOCKET_CLOSE]->hhh_nhooks > 0)
422 		hhook_run_socket(so, NULL, HHOOK_SOCKET_CLOSE);
423 
424 	crfree(so->so_cred);
425 	khelp_destroy_osd(&so->osd);
426 	sx_destroy(&so->so_snd.sb_sx);
427 	sx_destroy(&so->so_rcv.sb_sx);
428 	SOCKBUF_LOCK_DESTROY(&so->so_snd);
429 	SOCKBUF_LOCK_DESTROY(&so->so_rcv);
430 	uma_zfree(socket_zone, so);
431 }
432 
433 /*
434  * socreate returns a socket with a ref count of 1.  The socket should be
435  * closed with soclose().
436  */
437 int
438 socreate(int dom, struct socket **aso, int type, int proto,
439     struct ucred *cred, struct thread *td)
440 {
441 	struct protosw *prp;
442 	struct socket *so;
443 	int error;
444 
445 	if (proto)
446 		prp = pffindproto(dom, proto, type);
447 	else
448 		prp = pffindtype(dom, type);
449 
450 	if (prp == NULL) {
451 		/* No support for domain. */
452 		if (pffinddomain(dom) == NULL)
453 			return (EAFNOSUPPORT);
454 		/* No support for socket type. */
455 		if (proto == 0 && type != 0)
456 			return (EPROTOTYPE);
457 		return (EPROTONOSUPPORT);
458 	}
459 	if (prp->pr_usrreqs->pru_attach == NULL ||
460 	    prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
461 		return (EPROTONOSUPPORT);
462 
463 	if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
464 		return (EPROTONOSUPPORT);
465 
466 	if (prp->pr_type != type)
467 		return (EPROTOTYPE);
468 	so = soalloc(CRED_TO_VNET(cred));
469 	if (so == NULL)
470 		return (ENOBUFS);
471 
472 	TAILQ_INIT(&so->so_incomp);
473 	TAILQ_INIT(&so->so_comp);
474 	so->so_type = type;
475 	so->so_cred = crhold(cred);
476 	if ((prp->pr_domain->dom_family == PF_INET) ||
477 	    (prp->pr_domain->dom_family == PF_INET6) ||
478 	    (prp->pr_domain->dom_family == PF_ROUTE))
479 		so->so_fibnum = td->td_proc->p_fibnum;
480 	else
481 		so->so_fibnum = 0;
482 	so->so_proto = prp;
483 #ifdef MAC
484 	mac_socket_create(cred, so);
485 #endif
486 	knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
487 	knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
488 	so->so_count = 1;
489 	/*
490 	 * Auto-sizing of socket buffers is managed by the protocols and
491 	 * the appropriate flags must be set in the pru_attach function.
492 	 */
493 	CURVNET_SET(so->so_vnet);
494 	error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
495 	CURVNET_RESTORE();
496 	if (error) {
497 		KASSERT(so->so_count == 1, ("socreate: so_count %d",
498 		    so->so_count));
499 		so->so_count = 0;
500 		sodealloc(so);
501 		return (error);
502 	}
503 	*aso = so;
504 	return (0);
505 }
506 
507 #ifdef REGRESSION
508 static int regression_sonewconn_earlytest = 1;
509 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
510     &regression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
511 #endif
512 
513 /*
514  * When an attempt at a new connection is noted on a socket which accepts
515  * connections, sonewconn is called.  If the connection is possible (subject
516  * to space constraints, etc.) then we allocate a new structure, propoerly
517  * linked into the data structure of the original socket, and return this.
518  * Connstatus may be 0, or SS_ISCONFIRMING, or SS_ISCONNECTED.
519  *
520  * Note: the ref count on the socket is 0 on return.
521  */
522 struct socket *
523 sonewconn(struct socket *head, int connstatus)
524 {
525 	static struct timeval lastover;
526 	static struct timeval overinterval = { 60, 0 };
527 	static int overcount;
528 
529 	struct socket *so;
530 	int over;
531 
532 	ACCEPT_LOCK();
533 	over = (head->so_qlen > 3 * head->so_qlimit / 2);
534 	ACCEPT_UNLOCK();
535 #ifdef REGRESSION
536 	if (regression_sonewconn_earlytest && over) {
537 #else
538 	if (over) {
539 #endif
540 		overcount++;
541 
542 		if (ratecheck(&lastover, &overinterval)) {
543 			log(LOG_DEBUG, "%s: pcb %p: Listen queue overflow: "
544 			    "%i already in queue awaiting acceptance "
545 			    "(%d occurrences)\n",
546 			    __func__, head->so_pcb, head->so_qlen, overcount);
547 
548 			overcount = 0;
549 		}
550 
551 		return (NULL);
552 	}
553 	VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
554 	    __func__, __LINE__, head));
555 	so = soalloc(head->so_vnet);
556 	if (so == NULL) {
557 		log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: "
558 		    "limit reached or out of memory\n",
559 		    __func__, head->so_pcb);
560 		return (NULL);
561 	}
562 	if ((head->so_options & SO_ACCEPTFILTER) != 0)
563 		connstatus = 0;
564 	so->so_head = head;
565 	so->so_type = head->so_type;
566 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
567 	so->so_linger = head->so_linger;
568 	so->so_state = head->so_state | SS_NOFDREF;
569 	so->so_fibnum = head->so_fibnum;
570 	so->so_proto = head->so_proto;
571 	so->so_cred = crhold(head->so_cred);
572 #ifdef MAC
573 	mac_socket_newconn(head, so);
574 #endif
575 	knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
576 	knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
577 	VNET_SO_ASSERT(head);
578 	if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
579 		sodealloc(so);
580 		log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n",
581 		    __func__, head->so_pcb);
582 		return (NULL);
583 	}
584 	if ((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
585 		sodealloc(so);
586 		log(LOG_DEBUG, "%s: pcb %p: pru_attach() failed\n",
587 		    __func__, head->so_pcb);
588 		return (NULL);
589 	}
590 	so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
591 	so->so_snd.sb_lowat = head->so_snd.sb_lowat;
592 	so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
593 	so->so_snd.sb_timeo = head->so_snd.sb_timeo;
594 	so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
595 	so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
596 	so->so_state |= connstatus;
597 	ACCEPT_LOCK();
598 	/*
599 	 * The accept socket may be tearing down but we just
600 	 * won a race on the ACCEPT_LOCK.
601 	 * However, if sctp_peeloff() is called on a 1-to-many
602 	 * style socket, the SO_ACCEPTCONN doesn't need to be set.
603 	 */
604 	if (!(head->so_options & SO_ACCEPTCONN) &&
605 	    ((head->so_proto->pr_protocol != IPPROTO_SCTP) ||
606 	     (head->so_type != SOCK_SEQPACKET))) {
607 		SOCK_LOCK(so);
608 		so->so_head = NULL;
609 		sofree(so);		/* NB: returns ACCEPT_UNLOCK'ed. */
610 		return (NULL);
611 	}
612 	if (connstatus) {
613 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
614 		so->so_qstate |= SQ_COMP;
615 		head->so_qlen++;
616 	} else {
617 		/*
618 		 * Keep removing sockets from the head until there's room for
619 		 * us to insert on the tail.  In pre-locking revisions, this
620 		 * was a simple if(), but as we could be racing with other
621 		 * threads and soabort() requires dropping locks, we must
622 		 * loop waiting for the condition to be true.
623 		 */
624 		while (head->so_incqlen > head->so_qlimit) {
625 			struct socket *sp;
626 			sp = TAILQ_FIRST(&head->so_incomp);
627 			TAILQ_REMOVE(&head->so_incomp, sp, so_list);
628 			head->so_incqlen--;
629 			sp->so_qstate &= ~SQ_INCOMP;
630 			sp->so_head = NULL;
631 			ACCEPT_UNLOCK();
632 			soabort(sp);
633 			ACCEPT_LOCK();
634 		}
635 		TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
636 		so->so_qstate |= SQ_INCOMP;
637 		head->so_incqlen++;
638 	}
639 	ACCEPT_UNLOCK();
640 	if (connstatus) {
641 		sorwakeup(head);
642 		wakeup_one(&head->so_timeo);
643 	}
644 	return (so);
645 }
646 
647 int
648 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
649 {
650 	int error;
651 
652 	CURVNET_SET(so->so_vnet);
653 	error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
654 	CURVNET_RESTORE();
655 	return (error);
656 }
657 
658 int
659 sobindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
660 {
661 	int error;
662 
663 	CURVNET_SET(so->so_vnet);
664 	error = (*so->so_proto->pr_usrreqs->pru_bindat)(fd, so, nam, td);
665 	CURVNET_RESTORE();
666 	return (error);
667 }
668 
669 /*
670  * solisten() transitions a socket from a non-listening state to a listening
671  * state, but can also be used to update the listen queue depth on an
672  * existing listen socket.  The protocol will call back into the sockets
673  * layer using solisten_proto_check() and solisten_proto() to check and set
674  * socket-layer listen state.  Call backs are used so that the protocol can
675  * acquire both protocol and socket layer locks in whatever order is required
676  * by the protocol.
677  *
678  * Protocol implementors are advised to hold the socket lock across the
679  * socket-layer test and set to avoid races at the socket layer.
680  */
681 int
682 solisten(struct socket *so, int backlog, struct thread *td)
683 {
684 	int error;
685 
686 	CURVNET_SET(so->so_vnet);
687 	error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td);
688 	CURVNET_RESTORE();
689 	return (error);
690 }
691 
692 int
693 solisten_proto_check(struct socket *so)
694 {
695 
696 	SOCK_LOCK_ASSERT(so);
697 
698 	if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
699 	    SS_ISDISCONNECTING))
700 		return (EINVAL);
701 	return (0);
702 }
703 
704 void
705 solisten_proto(struct socket *so, int backlog)
706 {
707 
708 	SOCK_LOCK_ASSERT(so);
709 
710 	if (backlog < 0 || backlog > somaxconn)
711 		backlog = somaxconn;
712 	so->so_qlimit = backlog;
713 	so->so_options |= SO_ACCEPTCONN;
714 }
715 
716 /*
717  * Evaluate the reference count and named references on a socket; if no
718  * references remain, free it.  This should be called whenever a reference is
719  * released, such as in sorele(), but also when named reference flags are
720  * cleared in socket or protocol code.
721  *
722  * sofree() will free the socket if:
723  *
724  * - There are no outstanding file descriptor references or related consumers
725  *   (so_count == 0).
726  *
727  * - The socket has been closed by user space, if ever open (SS_NOFDREF).
728  *
729  * - The protocol does not have an outstanding strong reference on the socket
730  *   (SS_PROTOREF).
731  *
732  * - The socket is not in a completed connection queue, so a process has been
733  *   notified that it is present.  If it is removed, the user process may
734  *   block in accept() despite select() saying the socket was ready.
735  */
736 void
737 sofree(struct socket *so)
738 {
739 	struct protosw *pr = so->so_proto;
740 	struct socket *head;
741 
742 	ACCEPT_LOCK_ASSERT();
743 	SOCK_LOCK_ASSERT(so);
744 
745 	if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
746 	    (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
747 		SOCK_UNLOCK(so);
748 		ACCEPT_UNLOCK();
749 		return;
750 	}
751 
752 	head = so->so_head;
753 	if (head != NULL) {
754 		KASSERT((so->so_qstate & SQ_COMP) != 0 ||
755 		    (so->so_qstate & SQ_INCOMP) != 0,
756 		    ("sofree: so_head != NULL, but neither SQ_COMP nor "
757 		    "SQ_INCOMP"));
758 		KASSERT((so->so_qstate & SQ_COMP) == 0 ||
759 		    (so->so_qstate & SQ_INCOMP) == 0,
760 		    ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
761 		TAILQ_REMOVE(&head->so_incomp, so, so_list);
762 		head->so_incqlen--;
763 		so->so_qstate &= ~SQ_INCOMP;
764 		so->so_head = NULL;
765 	}
766 	KASSERT((so->so_qstate & SQ_COMP) == 0 &&
767 	    (so->so_qstate & SQ_INCOMP) == 0,
768 	    ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
769 	    so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
770 	if (so->so_options & SO_ACCEPTCONN) {
771 		KASSERT((TAILQ_EMPTY(&so->so_comp)),
772 		    ("sofree: so_comp populated"));
773 		KASSERT((TAILQ_EMPTY(&so->so_incomp)),
774 		    ("sofree: so_incomp populated"));
775 	}
776 	SOCK_UNLOCK(so);
777 	ACCEPT_UNLOCK();
778 
779 	VNET_SO_ASSERT(so);
780 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
781 		(*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
782 	if (pr->pr_usrreqs->pru_detach != NULL)
783 		(*pr->pr_usrreqs->pru_detach)(so);
784 
785 	/*
786 	 * From this point on, we assume that no other references to this
787 	 * socket exist anywhere else in the stack.  Therefore, no locks need
788 	 * to be acquired or held.
789 	 *
790 	 * We used to do a lot of socket buffer and socket locking here, as
791 	 * well as invoke sorflush() and perform wakeups.  The direct call to
792 	 * dom_dispose() and sbrelease_internal() are an inlining of what was
793 	 * necessary from sorflush().
794 	 *
795 	 * Notice that the socket buffer and kqueue state are torn down
796 	 * before calling pru_detach.  This means that protocols shold not
797 	 * assume they can perform socket wakeups, etc, in their detach code.
798 	 */
799 	sbdestroy(&so->so_snd, so);
800 	sbdestroy(&so->so_rcv, so);
801 	seldrain(&so->so_snd.sb_sel);
802 	seldrain(&so->so_rcv.sb_sel);
803 	knlist_destroy(&so->so_rcv.sb_sel.si_note);
804 	knlist_destroy(&so->so_snd.sb_sel.si_note);
805 	sodealloc(so);
806 }
807 
808 /*
809  * Close a socket on last file table reference removal.  Initiate disconnect
810  * if connected.  Free socket when disconnect complete.
811  *
812  * This function will sorele() the socket.  Note that soclose() may be called
813  * prior to the ref count reaching zero.  The actual socket structure will
814  * not be freed until the ref count reaches zero.
815  */
816 int
817 soclose(struct socket *so)
818 {
819 	int error = 0;
820 
821 	KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
822 
823 	CURVNET_SET(so->so_vnet);
824 	funsetown(&so->so_sigio);
825 	if (so->so_state & SS_ISCONNECTED) {
826 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
827 			error = sodisconnect(so);
828 			if (error) {
829 				if (error == ENOTCONN)
830 					error = 0;
831 				goto drop;
832 			}
833 		}
834 		if (so->so_options & SO_LINGER) {
835 			if ((so->so_state & SS_ISDISCONNECTING) &&
836 			    (so->so_state & SS_NBIO))
837 				goto drop;
838 			while (so->so_state & SS_ISCONNECTED) {
839 				error = tsleep(&so->so_timeo,
840 				    PSOCK | PCATCH, "soclos",
841 				    so->so_linger * hz);
842 				if (error)
843 					break;
844 			}
845 		}
846 	}
847 
848 drop:
849 	if (so->so_proto->pr_usrreqs->pru_close != NULL)
850 		(*so->so_proto->pr_usrreqs->pru_close)(so);
851 	ACCEPT_LOCK();
852 	if (so->so_options & SO_ACCEPTCONN) {
853 		struct socket *sp;
854 		/*
855 		 * Prevent new additions to the accept queues due
856 		 * to ACCEPT_LOCK races while we are draining them.
857 		 */
858 		so->so_options &= ~SO_ACCEPTCONN;
859 		while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
860 			TAILQ_REMOVE(&so->so_incomp, sp, so_list);
861 			so->so_incqlen--;
862 			sp->so_qstate &= ~SQ_INCOMP;
863 			sp->so_head = NULL;
864 			ACCEPT_UNLOCK();
865 			soabort(sp);
866 			ACCEPT_LOCK();
867 		}
868 		while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
869 			TAILQ_REMOVE(&so->so_comp, sp, so_list);
870 			so->so_qlen--;
871 			sp->so_qstate &= ~SQ_COMP;
872 			sp->so_head = NULL;
873 			ACCEPT_UNLOCK();
874 			soabort(sp);
875 			ACCEPT_LOCK();
876 		}
877 		KASSERT((TAILQ_EMPTY(&so->so_comp)),
878 		    ("%s: so_comp populated", __func__));
879 		KASSERT((TAILQ_EMPTY(&so->so_incomp)),
880 		    ("%s: so_incomp populated", __func__));
881 	}
882 	SOCK_LOCK(so);
883 	KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
884 	so->so_state |= SS_NOFDREF;
885 	sorele(so);			/* NB: Returns with ACCEPT_UNLOCK(). */
886 	CURVNET_RESTORE();
887 	return (error);
888 }
889 
890 /*
891  * soabort() is used to abruptly tear down a connection, such as when a
892  * resource limit is reached (listen queue depth exceeded), or if a listen
893  * socket is closed while there are sockets waiting to be accepted.
894  *
895  * This interface is tricky, because it is called on an unreferenced socket,
896  * and must be called only by a thread that has actually removed the socket
897  * from the listen queue it was on, or races with other threads are risked.
898  *
899  * This interface will call into the protocol code, so must not be called
900  * with any socket locks held.  Protocols do call it while holding their own
901  * recursible protocol mutexes, but this is something that should be subject
902  * to review in the future.
903  */
904 void
905 soabort(struct socket *so)
906 {
907 
908 	/*
909 	 * In as much as is possible, assert that no references to this
910 	 * socket are held.  This is not quite the same as asserting that the
911 	 * current thread is responsible for arranging for no references, but
912 	 * is as close as we can get for now.
913 	 */
914 	KASSERT(so->so_count == 0, ("soabort: so_count"));
915 	KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
916 	KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
917 	KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
918 	KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
919 	VNET_SO_ASSERT(so);
920 
921 	if (so->so_proto->pr_usrreqs->pru_abort != NULL)
922 		(*so->so_proto->pr_usrreqs->pru_abort)(so);
923 	ACCEPT_LOCK();
924 	SOCK_LOCK(so);
925 	sofree(so);
926 }
927 
928 int
929 soaccept(struct socket *so, struct sockaddr **nam)
930 {
931 	int error;
932 
933 	SOCK_LOCK(so);
934 	KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
935 	so->so_state &= ~SS_NOFDREF;
936 	SOCK_UNLOCK(so);
937 
938 	CURVNET_SET(so->so_vnet);
939 	error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
940 	CURVNET_RESTORE();
941 	return (error);
942 }
943 
944 int
945 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
946 {
947 
948 	return (soconnectat(AT_FDCWD, so, nam, td));
949 }
950 
951 int
952 soconnectat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
953 {
954 	int error;
955 
956 	if (so->so_options & SO_ACCEPTCONN)
957 		return (EOPNOTSUPP);
958 
959 	CURVNET_SET(so->so_vnet);
960 	/*
961 	 * If protocol is connection-based, can only connect once.
962 	 * Otherwise, if connected, try to disconnect first.  This allows
963 	 * user to disconnect by connecting to, e.g., a null address.
964 	 */
965 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
966 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
967 	    (error = sodisconnect(so)))) {
968 		error = EISCONN;
969 	} else {
970 		/*
971 		 * Prevent accumulated error from previous connection from
972 		 * biting us.
973 		 */
974 		so->so_error = 0;
975 		if (fd == AT_FDCWD) {
976 			error = (*so->so_proto->pr_usrreqs->pru_connect)(so,
977 			    nam, td);
978 		} else {
979 			error = (*so->so_proto->pr_usrreqs->pru_connectat)(fd,
980 			    so, nam, td);
981 		}
982 	}
983 	CURVNET_RESTORE();
984 
985 	return (error);
986 }
987 
988 int
989 soconnect2(struct socket *so1, struct socket *so2)
990 {
991 	int error;
992 
993 	CURVNET_SET(so1->so_vnet);
994 	error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
995 	CURVNET_RESTORE();
996 	return (error);
997 }
998 
999 int
1000 sodisconnect(struct socket *so)
1001 {
1002 	int error;
1003 
1004 	if ((so->so_state & SS_ISCONNECTED) == 0)
1005 		return (ENOTCONN);
1006 	if (so->so_state & SS_ISDISCONNECTING)
1007 		return (EALREADY);
1008 	VNET_SO_ASSERT(so);
1009 	error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
1010 	return (error);
1011 }
1012 
1013 #define	SBLOCKWAIT(f)	(((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
1014 
1015 int
1016 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
1017     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1018 {
1019 	long space;
1020 	ssize_t resid;
1021 	int clen = 0, error, dontroute;
1022 
1023 	KASSERT(so->so_type == SOCK_DGRAM, ("sosend_dgram: !SOCK_DGRAM"));
1024 	KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
1025 	    ("sosend_dgram: !PR_ATOMIC"));
1026 
1027 	if (uio != NULL)
1028 		resid = uio->uio_resid;
1029 	else
1030 		resid = top->m_pkthdr.len;
1031 	/*
1032 	 * In theory resid should be unsigned.  However, space must be
1033 	 * signed, as it might be less than 0 if we over-committed, and we
1034 	 * must use a signed comparison of space and resid.  On the other
1035 	 * hand, a negative resid causes us to loop sending 0-length
1036 	 * segments to the protocol.
1037 	 */
1038 	if (resid < 0) {
1039 		error = EINVAL;
1040 		goto out;
1041 	}
1042 
1043 	dontroute =
1044 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
1045 	if (td != NULL)
1046 		td->td_ru.ru_msgsnd++;
1047 	if (control != NULL)
1048 		clen = control->m_len;
1049 
1050 	SOCKBUF_LOCK(&so->so_snd);
1051 	if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1052 		SOCKBUF_UNLOCK(&so->so_snd);
1053 		error = EPIPE;
1054 		goto out;
1055 	}
1056 	if (so->so_error) {
1057 		error = so->so_error;
1058 		so->so_error = 0;
1059 		SOCKBUF_UNLOCK(&so->so_snd);
1060 		goto out;
1061 	}
1062 	if ((so->so_state & SS_ISCONNECTED) == 0) {
1063 		/*
1064 		 * `sendto' and `sendmsg' is allowed on a connection-based
1065 		 * socket if it supports implied connect.  Return ENOTCONN if
1066 		 * not connected and no address is supplied.
1067 		 */
1068 		if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1069 		    (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1070 			if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1071 			    !(resid == 0 && clen != 0)) {
1072 				SOCKBUF_UNLOCK(&so->so_snd);
1073 				error = ENOTCONN;
1074 				goto out;
1075 			}
1076 		} else if (addr == NULL) {
1077 			if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1078 				error = ENOTCONN;
1079 			else
1080 				error = EDESTADDRREQ;
1081 			SOCKBUF_UNLOCK(&so->so_snd);
1082 			goto out;
1083 		}
1084 	}
1085 
1086 	/*
1087 	 * Do we need MSG_OOB support in SOCK_DGRAM?  Signs here may be a
1088 	 * problem and need fixing.
1089 	 */
1090 	space = sbspace(&so->so_snd);
1091 	if (flags & MSG_OOB)
1092 		space += 1024;
1093 	space -= clen;
1094 	SOCKBUF_UNLOCK(&so->so_snd);
1095 	if (resid > space) {
1096 		error = EMSGSIZE;
1097 		goto out;
1098 	}
1099 	if (uio == NULL) {
1100 		resid = 0;
1101 		if (flags & MSG_EOR)
1102 			top->m_flags |= M_EOR;
1103 	} else {
1104 		/*
1105 		 * Copy the data from userland into a mbuf chain.
1106 		 * If no data is to be copied in, a single empty mbuf
1107 		 * is returned.
1108 		 */
1109 		top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1110 		    (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1111 		if (top == NULL) {
1112 			error = EFAULT;	/* only possible error */
1113 			goto out;
1114 		}
1115 		space -= resid - uio->uio_resid;
1116 		resid = uio->uio_resid;
1117 	}
1118 	KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1119 	/*
1120 	 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1121 	 * than with.
1122 	 */
1123 	if (dontroute) {
1124 		SOCK_LOCK(so);
1125 		so->so_options |= SO_DONTROUTE;
1126 		SOCK_UNLOCK(so);
1127 	}
1128 	/*
1129 	 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1130 	 * of date.  We could have recieved a reset packet in an interrupt or
1131 	 * maybe we slept while doing page faults in uiomove() etc.  We could
1132 	 * probably recheck again inside the locking protection here, but
1133 	 * there are probably other places that this also happens.  We must
1134 	 * rethink this.
1135 	 */
1136 	VNET_SO_ASSERT(so);
1137 	error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1138 	    (flags & MSG_OOB) ? PRUS_OOB :
1139 	/*
1140 	 * If the user set MSG_EOF, the protocol understands this flag and
1141 	 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1142 	 */
1143 	    ((flags & MSG_EOF) &&
1144 	     (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1145 	     (resid <= 0)) ?
1146 		PRUS_EOF :
1147 		/* If there is more to send set PRUS_MORETOCOME */
1148 		(resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1149 		top, addr, control, td);
1150 	if (dontroute) {
1151 		SOCK_LOCK(so);
1152 		so->so_options &= ~SO_DONTROUTE;
1153 		SOCK_UNLOCK(so);
1154 	}
1155 	clen = 0;
1156 	control = NULL;
1157 	top = NULL;
1158 out:
1159 	if (top != NULL)
1160 		m_freem(top);
1161 	if (control != NULL)
1162 		m_freem(control);
1163 	return (error);
1164 }
1165 
1166 /*
1167  * Send on a socket.  If send must go all at once and message is larger than
1168  * send buffering, then hard error.  Lock against other senders.  If must go
1169  * all at once and not enough room now, then inform user that this would
1170  * block and do nothing.  Otherwise, if nonblocking, send as much as
1171  * possible.  The data to be sent is described by "uio" if nonzero, otherwise
1172  * by the mbuf chain "top" (which must be null if uio is not).  Data provided
1173  * in mbuf chain must be small enough to send all at once.
1174  *
1175  * Returns nonzero on error, timeout or signal; callers must check for short
1176  * counts if EINTR/ERESTART are returned.  Data and control buffers are freed
1177  * on return.
1178  */
1179 int
1180 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1181     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1182 {
1183 	long space;
1184 	ssize_t resid;
1185 	int clen = 0, error, dontroute;
1186 	int atomic = sosendallatonce(so) || top;
1187 
1188 	if (uio != NULL)
1189 		resid = uio->uio_resid;
1190 	else
1191 		resid = top->m_pkthdr.len;
1192 	/*
1193 	 * In theory resid should be unsigned.  However, space must be
1194 	 * signed, as it might be less than 0 if we over-committed, and we
1195 	 * must use a signed comparison of space and resid.  On the other
1196 	 * hand, a negative resid causes us to loop sending 0-length
1197 	 * segments to the protocol.
1198 	 *
1199 	 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1200 	 * type sockets since that's an error.
1201 	 */
1202 	if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1203 		error = EINVAL;
1204 		goto out;
1205 	}
1206 
1207 	dontroute =
1208 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1209 	    (so->so_proto->pr_flags & PR_ATOMIC);
1210 	if (td != NULL)
1211 		td->td_ru.ru_msgsnd++;
1212 	if (control != NULL)
1213 		clen = control->m_len;
1214 
1215 	error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1216 	if (error)
1217 		goto out;
1218 
1219 restart:
1220 	do {
1221 		SOCKBUF_LOCK(&so->so_snd);
1222 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1223 			SOCKBUF_UNLOCK(&so->so_snd);
1224 			error = EPIPE;
1225 			goto release;
1226 		}
1227 		if (so->so_error) {
1228 			error = so->so_error;
1229 			so->so_error = 0;
1230 			SOCKBUF_UNLOCK(&so->so_snd);
1231 			goto release;
1232 		}
1233 		if ((so->so_state & SS_ISCONNECTED) == 0) {
1234 			/*
1235 			 * `sendto' and `sendmsg' is allowed on a connection-
1236 			 * based socket if it supports implied connect.
1237 			 * Return ENOTCONN if not connected and no address is
1238 			 * supplied.
1239 			 */
1240 			if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1241 			    (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1242 				if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1243 				    !(resid == 0 && clen != 0)) {
1244 					SOCKBUF_UNLOCK(&so->so_snd);
1245 					error = ENOTCONN;
1246 					goto release;
1247 				}
1248 			} else if (addr == NULL) {
1249 				SOCKBUF_UNLOCK(&so->so_snd);
1250 				if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1251 					error = ENOTCONN;
1252 				else
1253 					error = EDESTADDRREQ;
1254 				goto release;
1255 			}
1256 		}
1257 		space = sbspace(&so->so_snd);
1258 		if (flags & MSG_OOB)
1259 			space += 1024;
1260 		if ((atomic && resid > so->so_snd.sb_hiwat) ||
1261 		    clen > so->so_snd.sb_hiwat) {
1262 			SOCKBUF_UNLOCK(&so->so_snd);
1263 			error = EMSGSIZE;
1264 			goto release;
1265 		}
1266 		if (space < resid + clen &&
1267 		    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1268 			if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
1269 				SOCKBUF_UNLOCK(&so->so_snd);
1270 				error = EWOULDBLOCK;
1271 				goto release;
1272 			}
1273 			error = sbwait(&so->so_snd);
1274 			SOCKBUF_UNLOCK(&so->so_snd);
1275 			if (error)
1276 				goto release;
1277 			goto restart;
1278 		}
1279 		SOCKBUF_UNLOCK(&so->so_snd);
1280 		space -= clen;
1281 		do {
1282 			if (uio == NULL) {
1283 				resid = 0;
1284 				if (flags & MSG_EOR)
1285 					top->m_flags |= M_EOR;
1286 			} else {
1287 				/*
1288 				 * Copy the data from userland into a mbuf
1289 				 * chain.  If no data is to be copied in,
1290 				 * a single empty mbuf is returned.
1291 				 */
1292 				top = m_uiotombuf(uio, M_WAITOK, space,
1293 				    (atomic ? max_hdr : 0),
1294 				    (atomic ? M_PKTHDR : 0) |
1295 				    ((flags & MSG_EOR) ? M_EOR : 0));
1296 				if (top == NULL) {
1297 					error = EFAULT; /* only possible error */
1298 					goto release;
1299 				}
1300 				space -= resid - uio->uio_resid;
1301 				resid = uio->uio_resid;
1302 			}
1303 			if (dontroute) {
1304 				SOCK_LOCK(so);
1305 				so->so_options |= SO_DONTROUTE;
1306 				SOCK_UNLOCK(so);
1307 			}
1308 			/*
1309 			 * XXX all the SBS_CANTSENDMORE checks previously
1310 			 * done could be out of date.  We could have recieved
1311 			 * a reset packet in an interrupt or maybe we slept
1312 			 * while doing page faults in uiomove() etc.  We
1313 			 * could probably recheck again inside the locking
1314 			 * protection here, but there are probably other
1315 			 * places that this also happens.  We must rethink
1316 			 * this.
1317 			 */
1318 			VNET_SO_ASSERT(so);
1319 			error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1320 			    (flags & MSG_OOB) ? PRUS_OOB :
1321 			/*
1322 			 * If the user set MSG_EOF, the protocol understands
1323 			 * this flag and nothing left to send then use
1324 			 * PRU_SEND_EOF instead of PRU_SEND.
1325 			 */
1326 			    ((flags & MSG_EOF) &&
1327 			     (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1328 			     (resid <= 0)) ?
1329 				PRUS_EOF :
1330 			/* If there is more to send set PRUS_MORETOCOME. */
1331 			    (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1332 			    top, addr, control, td);
1333 			if (dontroute) {
1334 				SOCK_LOCK(so);
1335 				so->so_options &= ~SO_DONTROUTE;
1336 				SOCK_UNLOCK(so);
1337 			}
1338 			clen = 0;
1339 			control = NULL;
1340 			top = NULL;
1341 			if (error)
1342 				goto release;
1343 		} while (resid && space > 0);
1344 	} while (resid);
1345 
1346 release:
1347 	sbunlock(&so->so_snd);
1348 out:
1349 	if (top != NULL)
1350 		m_freem(top);
1351 	if (control != NULL)
1352 		m_freem(control);
1353 	return (error);
1354 }
1355 
1356 int
1357 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1358     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1359 {
1360 	int error;
1361 
1362 	CURVNET_SET(so->so_vnet);
1363 	error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1364 	    control, flags, td);
1365 	CURVNET_RESTORE();
1366 	return (error);
1367 }
1368 
1369 /*
1370  * The part of soreceive() that implements reading non-inline out-of-band
1371  * data from a socket.  For more complete comments, see soreceive(), from
1372  * which this code originated.
1373  *
1374  * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1375  * unable to return an mbuf chain to the caller.
1376  */
1377 static int
1378 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1379 {
1380 	struct protosw *pr = so->so_proto;
1381 	struct mbuf *m;
1382 	int error;
1383 
1384 	KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1385 	VNET_SO_ASSERT(so);
1386 
1387 	m = m_get(M_WAITOK, MT_DATA);
1388 	error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1389 	if (error)
1390 		goto bad;
1391 	do {
1392 		error = uiomove(mtod(m, void *),
1393 		    (int) min(uio->uio_resid, m->m_len), uio);
1394 		m = m_free(m);
1395 	} while (uio->uio_resid && error == 0 && m);
1396 bad:
1397 	if (m != NULL)
1398 		m_freem(m);
1399 	return (error);
1400 }
1401 
1402 /*
1403  * Following replacement or removal of the first mbuf on the first mbuf chain
1404  * of a socket buffer, push necessary state changes back into the socket
1405  * buffer so that other consumers see the values consistently.  'nextrecord'
1406  * is the callers locally stored value of the original value of
1407  * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1408  * NOTE: 'nextrecord' may be NULL.
1409  */
1410 static __inline void
1411 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1412 {
1413 
1414 	SOCKBUF_LOCK_ASSERT(sb);
1415 	/*
1416 	 * First, update for the new value of nextrecord.  If necessary, make
1417 	 * it the first record.
1418 	 */
1419 	if (sb->sb_mb != NULL)
1420 		sb->sb_mb->m_nextpkt = nextrecord;
1421 	else
1422 		sb->sb_mb = nextrecord;
1423 
1424 	/*
1425 	 * Now update any dependent socket buffer fields to reflect the new
1426 	 * state.  This is an expanded inline of SB_EMPTY_FIXUP(), with the
1427 	 * addition of a second clause that takes care of the case where
1428 	 * sb_mb has been updated, but remains the last record.
1429 	 */
1430 	if (sb->sb_mb == NULL) {
1431 		sb->sb_mbtail = NULL;
1432 		sb->sb_lastrecord = NULL;
1433 	} else if (sb->sb_mb->m_nextpkt == NULL)
1434 		sb->sb_lastrecord = sb->sb_mb;
1435 }
1436 
1437 /*
1438  * Implement receive operations on a socket.  We depend on the way that
1439  * records are added to the sockbuf by sbappend.  In particular, each record
1440  * (mbufs linked through m_next) must begin with an address if the protocol
1441  * so specifies, followed by an optional mbuf or mbufs containing ancillary
1442  * data, and then zero or more mbufs of data.  In order to allow parallelism
1443  * between network receive and copying to user space, as well as avoid
1444  * sleeping with a mutex held, we release the socket buffer mutex during the
1445  * user space copy.  Although the sockbuf is locked, new data may still be
1446  * appended, and thus we must maintain consistency of the sockbuf during that
1447  * time.
1448  *
1449  * The caller may receive the data as a single mbuf chain by supplying an
1450  * mbuf **mp0 for use in returning the chain.  The uio is then used only for
1451  * the count in uio_resid.
1452  */
1453 int
1454 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1455     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1456 {
1457 	struct mbuf *m, **mp;
1458 	int flags, error, offset;
1459 	ssize_t len;
1460 	struct protosw *pr = so->so_proto;
1461 	struct mbuf *nextrecord;
1462 	int moff, type = 0;
1463 	ssize_t orig_resid = uio->uio_resid;
1464 
1465 	mp = mp0;
1466 	if (psa != NULL)
1467 		*psa = NULL;
1468 	if (controlp != NULL)
1469 		*controlp = NULL;
1470 	if (flagsp != NULL)
1471 		flags = *flagsp &~ MSG_EOR;
1472 	else
1473 		flags = 0;
1474 	if (flags & MSG_OOB)
1475 		return (soreceive_rcvoob(so, uio, flags));
1476 	if (mp != NULL)
1477 		*mp = NULL;
1478 	if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1479 	    && uio->uio_resid) {
1480 		VNET_SO_ASSERT(so);
1481 		(*pr->pr_usrreqs->pru_rcvd)(so, 0);
1482 	}
1483 
1484 	error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1485 	if (error)
1486 		return (error);
1487 
1488 restart:
1489 	SOCKBUF_LOCK(&so->so_rcv);
1490 	m = so->so_rcv.sb_mb;
1491 	/*
1492 	 * If we have less data than requested, block awaiting more (subject
1493 	 * to any timeout) if:
1494 	 *   1. the current count is less than the low water mark, or
1495 	 *   2. MSG_DONTWAIT is not set
1496 	 */
1497 	if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1498 	    so->so_rcv.sb_cc < uio->uio_resid) &&
1499 	    so->so_rcv.sb_cc < so->so_rcv.sb_lowat &&
1500 	    m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1501 		KASSERT(m != NULL || !so->so_rcv.sb_cc,
1502 		    ("receive: m == %p so->so_rcv.sb_cc == %u",
1503 		    m, so->so_rcv.sb_cc));
1504 		if (so->so_error) {
1505 			if (m != NULL)
1506 				goto dontblock;
1507 			error = so->so_error;
1508 			if ((flags & MSG_PEEK) == 0)
1509 				so->so_error = 0;
1510 			SOCKBUF_UNLOCK(&so->so_rcv);
1511 			goto release;
1512 		}
1513 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1514 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1515 			if (m == NULL) {
1516 				SOCKBUF_UNLOCK(&so->so_rcv);
1517 				goto release;
1518 			} else
1519 				goto dontblock;
1520 		}
1521 		for (; m != NULL; m = m->m_next)
1522 			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
1523 				m = so->so_rcv.sb_mb;
1524 				goto dontblock;
1525 			}
1526 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1527 		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1528 			SOCKBUF_UNLOCK(&so->so_rcv);
1529 			error = ENOTCONN;
1530 			goto release;
1531 		}
1532 		if (uio->uio_resid == 0) {
1533 			SOCKBUF_UNLOCK(&so->so_rcv);
1534 			goto release;
1535 		}
1536 		if ((so->so_state & SS_NBIO) ||
1537 		    (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1538 			SOCKBUF_UNLOCK(&so->so_rcv);
1539 			error = EWOULDBLOCK;
1540 			goto release;
1541 		}
1542 		SBLASTRECORDCHK(&so->so_rcv);
1543 		SBLASTMBUFCHK(&so->so_rcv);
1544 		error = sbwait(&so->so_rcv);
1545 		SOCKBUF_UNLOCK(&so->so_rcv);
1546 		if (error)
1547 			goto release;
1548 		goto restart;
1549 	}
1550 dontblock:
1551 	/*
1552 	 * From this point onward, we maintain 'nextrecord' as a cache of the
1553 	 * pointer to the next record in the socket buffer.  We must keep the
1554 	 * various socket buffer pointers and local stack versions of the
1555 	 * pointers in sync, pushing out modifications before dropping the
1556 	 * socket buffer mutex, and re-reading them when picking it up.
1557 	 *
1558 	 * Otherwise, we will race with the network stack appending new data
1559 	 * or records onto the socket buffer by using inconsistent/stale
1560 	 * versions of the field, possibly resulting in socket buffer
1561 	 * corruption.
1562 	 *
1563 	 * By holding the high-level sblock(), we prevent simultaneous
1564 	 * readers from pulling off the front of the socket buffer.
1565 	 */
1566 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1567 	if (uio->uio_td)
1568 		uio->uio_td->td_ru.ru_msgrcv++;
1569 	KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1570 	SBLASTRECORDCHK(&so->so_rcv);
1571 	SBLASTMBUFCHK(&so->so_rcv);
1572 	nextrecord = m->m_nextpkt;
1573 	if (pr->pr_flags & PR_ADDR) {
1574 		KASSERT(m->m_type == MT_SONAME,
1575 		    ("m->m_type == %d", m->m_type));
1576 		orig_resid = 0;
1577 		if (psa != NULL)
1578 			*psa = sodupsockaddr(mtod(m, struct sockaddr *),
1579 			    M_NOWAIT);
1580 		if (flags & MSG_PEEK) {
1581 			m = m->m_next;
1582 		} else {
1583 			sbfree(&so->so_rcv, m);
1584 			so->so_rcv.sb_mb = m_free(m);
1585 			m = so->so_rcv.sb_mb;
1586 			sockbuf_pushsync(&so->so_rcv, nextrecord);
1587 		}
1588 	}
1589 
1590 	/*
1591 	 * Process one or more MT_CONTROL mbufs present before any data mbufs
1592 	 * in the first mbuf chain on the socket buffer.  If MSG_PEEK, we
1593 	 * just copy the data; if !MSG_PEEK, we call into the protocol to
1594 	 * perform externalization (or freeing if controlp == NULL).
1595 	 */
1596 	if (m != NULL && m->m_type == MT_CONTROL) {
1597 		struct mbuf *cm = NULL, *cmn;
1598 		struct mbuf **cme = &cm;
1599 
1600 		do {
1601 			if (flags & MSG_PEEK) {
1602 				if (controlp != NULL) {
1603 					*controlp = m_copy(m, 0, m->m_len);
1604 					controlp = &(*controlp)->m_next;
1605 				}
1606 				m = m->m_next;
1607 			} else {
1608 				sbfree(&so->so_rcv, m);
1609 				so->so_rcv.sb_mb = m->m_next;
1610 				m->m_next = NULL;
1611 				*cme = m;
1612 				cme = &(*cme)->m_next;
1613 				m = so->so_rcv.sb_mb;
1614 			}
1615 		} while (m != NULL && m->m_type == MT_CONTROL);
1616 		if ((flags & MSG_PEEK) == 0)
1617 			sockbuf_pushsync(&so->so_rcv, nextrecord);
1618 		while (cm != NULL) {
1619 			cmn = cm->m_next;
1620 			cm->m_next = NULL;
1621 			if (pr->pr_domain->dom_externalize != NULL) {
1622 				SOCKBUF_UNLOCK(&so->so_rcv);
1623 				VNET_SO_ASSERT(so);
1624 				error = (*pr->pr_domain->dom_externalize)
1625 				    (cm, controlp, flags);
1626 				SOCKBUF_LOCK(&so->so_rcv);
1627 			} else if (controlp != NULL)
1628 				*controlp = cm;
1629 			else
1630 				m_freem(cm);
1631 			if (controlp != NULL) {
1632 				orig_resid = 0;
1633 				while (*controlp != NULL)
1634 					controlp = &(*controlp)->m_next;
1635 			}
1636 			cm = cmn;
1637 		}
1638 		if (m != NULL)
1639 			nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1640 		else
1641 			nextrecord = so->so_rcv.sb_mb;
1642 		orig_resid = 0;
1643 	}
1644 	if (m != NULL) {
1645 		if ((flags & MSG_PEEK) == 0) {
1646 			KASSERT(m->m_nextpkt == nextrecord,
1647 			    ("soreceive: post-control, nextrecord !sync"));
1648 			if (nextrecord == NULL) {
1649 				KASSERT(so->so_rcv.sb_mb == m,
1650 				    ("soreceive: post-control, sb_mb!=m"));
1651 				KASSERT(so->so_rcv.sb_lastrecord == m,
1652 				    ("soreceive: post-control, lastrecord!=m"));
1653 			}
1654 		}
1655 		type = m->m_type;
1656 		if (type == MT_OOBDATA)
1657 			flags |= MSG_OOB;
1658 	} else {
1659 		if ((flags & MSG_PEEK) == 0) {
1660 			KASSERT(so->so_rcv.sb_mb == nextrecord,
1661 			    ("soreceive: sb_mb != nextrecord"));
1662 			if (so->so_rcv.sb_mb == NULL) {
1663 				KASSERT(so->so_rcv.sb_lastrecord == NULL,
1664 				    ("soreceive: sb_lastercord != NULL"));
1665 			}
1666 		}
1667 	}
1668 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1669 	SBLASTRECORDCHK(&so->so_rcv);
1670 	SBLASTMBUFCHK(&so->so_rcv);
1671 
1672 	/*
1673 	 * Now continue to read any data mbufs off of the head of the socket
1674 	 * buffer until the read request is satisfied.  Note that 'type' is
1675 	 * used to store the type of any mbuf reads that have happened so far
1676 	 * such that soreceive() can stop reading if the type changes, which
1677 	 * causes soreceive() to return only one of regular data and inline
1678 	 * out-of-band data in a single socket receive operation.
1679 	 */
1680 	moff = 0;
1681 	offset = 0;
1682 	while (m != NULL && uio->uio_resid > 0 && error == 0) {
1683 		/*
1684 		 * If the type of mbuf has changed since the last mbuf
1685 		 * examined ('type'), end the receive operation.
1686 		 */
1687 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1688 		if (m->m_type == MT_OOBDATA || m->m_type == MT_CONTROL) {
1689 			if (type != m->m_type)
1690 				break;
1691 		} else if (type == MT_OOBDATA)
1692 			break;
1693 		else
1694 		    KASSERT(m->m_type == MT_DATA,
1695 			("m->m_type == %d", m->m_type));
1696 		so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1697 		len = uio->uio_resid;
1698 		if (so->so_oobmark && len > so->so_oobmark - offset)
1699 			len = so->so_oobmark - offset;
1700 		if (len > m->m_len - moff)
1701 			len = m->m_len - moff;
1702 		/*
1703 		 * If mp is set, just pass back the mbufs.  Otherwise copy
1704 		 * them out via the uio, then free.  Sockbuf must be
1705 		 * consistent here (points to current mbuf, it points to next
1706 		 * record) when we drop priority; we must note any additions
1707 		 * to the sockbuf when we block interrupts again.
1708 		 */
1709 		if (mp == NULL) {
1710 			SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1711 			SBLASTRECORDCHK(&so->so_rcv);
1712 			SBLASTMBUFCHK(&so->so_rcv);
1713 			SOCKBUF_UNLOCK(&so->so_rcv);
1714 			error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1715 			SOCKBUF_LOCK(&so->so_rcv);
1716 			if (error) {
1717 				/*
1718 				 * The MT_SONAME mbuf has already been removed
1719 				 * from the record, so it is necessary to
1720 				 * remove the data mbufs, if any, to preserve
1721 				 * the invariant in the case of PR_ADDR that
1722 				 * requires MT_SONAME mbufs at the head of
1723 				 * each record.
1724 				 */
1725 				if (m && pr->pr_flags & PR_ATOMIC &&
1726 				    ((flags & MSG_PEEK) == 0))
1727 					(void)sbdroprecord_locked(&so->so_rcv);
1728 				SOCKBUF_UNLOCK(&so->so_rcv);
1729 				goto release;
1730 			}
1731 		} else
1732 			uio->uio_resid -= len;
1733 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1734 		if (len == m->m_len - moff) {
1735 			if (m->m_flags & M_EOR)
1736 				flags |= MSG_EOR;
1737 			if (flags & MSG_PEEK) {
1738 				m = m->m_next;
1739 				moff = 0;
1740 			} else {
1741 				nextrecord = m->m_nextpkt;
1742 				sbfree(&so->so_rcv, m);
1743 				if (mp != NULL) {
1744 					m->m_nextpkt = NULL;
1745 					*mp = m;
1746 					mp = &m->m_next;
1747 					so->so_rcv.sb_mb = m = m->m_next;
1748 					*mp = NULL;
1749 				} else {
1750 					so->so_rcv.sb_mb = m_free(m);
1751 					m = so->so_rcv.sb_mb;
1752 				}
1753 				sockbuf_pushsync(&so->so_rcv, nextrecord);
1754 				SBLASTRECORDCHK(&so->so_rcv);
1755 				SBLASTMBUFCHK(&so->so_rcv);
1756 			}
1757 		} else {
1758 			if (flags & MSG_PEEK)
1759 				moff += len;
1760 			else {
1761 				if (mp != NULL) {
1762 					if (flags & MSG_DONTWAIT) {
1763 						*mp = m_copym(m, 0, len,
1764 						    M_NOWAIT);
1765 						if (*mp == NULL) {
1766 							/*
1767 							 * m_copym() couldn't
1768 							 * allocate an mbuf.
1769 							 * Adjust uio_resid back
1770 							 * (it was adjusted
1771 							 * down by len bytes,
1772 							 * which we didn't end
1773 							 * up "copying" over).
1774 							 */
1775 							uio->uio_resid += len;
1776 							break;
1777 						}
1778 					} else {
1779 						SOCKBUF_UNLOCK(&so->so_rcv);
1780 						*mp = m_copym(m, 0, len,
1781 						    M_WAITOK);
1782 						SOCKBUF_LOCK(&so->so_rcv);
1783 					}
1784 				}
1785 				m->m_data += len;
1786 				m->m_len -= len;
1787 				so->so_rcv.sb_cc -= len;
1788 			}
1789 		}
1790 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1791 		if (so->so_oobmark) {
1792 			if ((flags & MSG_PEEK) == 0) {
1793 				so->so_oobmark -= len;
1794 				if (so->so_oobmark == 0) {
1795 					so->so_rcv.sb_state |= SBS_RCVATMARK;
1796 					break;
1797 				}
1798 			} else {
1799 				offset += len;
1800 				if (offset == so->so_oobmark)
1801 					break;
1802 			}
1803 		}
1804 		if (flags & MSG_EOR)
1805 			break;
1806 		/*
1807 		 * If the MSG_WAITALL flag is set (for non-atomic socket), we
1808 		 * must not quit until "uio->uio_resid == 0" or an error
1809 		 * termination.  If a signal/timeout occurs, return with a
1810 		 * short count but without error.  Keep sockbuf locked
1811 		 * against other readers.
1812 		 */
1813 		while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1814 		    !sosendallatonce(so) && nextrecord == NULL) {
1815 			SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1816 			if (so->so_error ||
1817 			    so->so_rcv.sb_state & SBS_CANTRCVMORE)
1818 				break;
1819 			/*
1820 			 * Notify the protocol that some data has been
1821 			 * drained before blocking.
1822 			 */
1823 			if (pr->pr_flags & PR_WANTRCVD) {
1824 				SOCKBUF_UNLOCK(&so->so_rcv);
1825 				VNET_SO_ASSERT(so);
1826 				(*pr->pr_usrreqs->pru_rcvd)(so, flags);
1827 				SOCKBUF_LOCK(&so->so_rcv);
1828 			}
1829 			SBLASTRECORDCHK(&so->so_rcv);
1830 			SBLASTMBUFCHK(&so->so_rcv);
1831 			/*
1832 			 * We could receive some data while was notifying
1833 			 * the protocol. Skip blocking in this case.
1834 			 */
1835 			if (so->so_rcv.sb_mb == NULL) {
1836 				error = sbwait(&so->so_rcv);
1837 				if (error) {
1838 					SOCKBUF_UNLOCK(&so->so_rcv);
1839 					goto release;
1840 				}
1841 			}
1842 			m = so->so_rcv.sb_mb;
1843 			if (m != NULL)
1844 				nextrecord = m->m_nextpkt;
1845 		}
1846 	}
1847 
1848 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1849 	if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1850 		flags |= MSG_TRUNC;
1851 		if ((flags & MSG_PEEK) == 0)
1852 			(void) sbdroprecord_locked(&so->so_rcv);
1853 	}
1854 	if ((flags & MSG_PEEK) == 0) {
1855 		if (m == NULL) {
1856 			/*
1857 			 * First part is an inline SB_EMPTY_FIXUP().  Second
1858 			 * part makes sure sb_lastrecord is up-to-date if
1859 			 * there is still data in the socket buffer.
1860 			 */
1861 			so->so_rcv.sb_mb = nextrecord;
1862 			if (so->so_rcv.sb_mb == NULL) {
1863 				so->so_rcv.sb_mbtail = NULL;
1864 				so->so_rcv.sb_lastrecord = NULL;
1865 			} else if (nextrecord->m_nextpkt == NULL)
1866 				so->so_rcv.sb_lastrecord = nextrecord;
1867 		}
1868 		SBLASTRECORDCHK(&so->so_rcv);
1869 		SBLASTMBUFCHK(&so->so_rcv);
1870 		/*
1871 		 * If soreceive() is being done from the socket callback,
1872 		 * then don't need to generate ACK to peer to update window,
1873 		 * since ACK will be generated on return to TCP.
1874 		 */
1875 		if (!(flags & MSG_SOCALLBCK) &&
1876 		    (pr->pr_flags & PR_WANTRCVD)) {
1877 			SOCKBUF_UNLOCK(&so->so_rcv);
1878 			VNET_SO_ASSERT(so);
1879 			(*pr->pr_usrreqs->pru_rcvd)(so, flags);
1880 			SOCKBUF_LOCK(&so->so_rcv);
1881 		}
1882 	}
1883 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1884 	if (orig_resid == uio->uio_resid && orig_resid &&
1885 	    (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1886 		SOCKBUF_UNLOCK(&so->so_rcv);
1887 		goto restart;
1888 	}
1889 	SOCKBUF_UNLOCK(&so->so_rcv);
1890 
1891 	if (flagsp != NULL)
1892 		*flagsp |= flags;
1893 release:
1894 	sbunlock(&so->so_rcv);
1895 	return (error);
1896 }
1897 
1898 /*
1899  * Optimized version of soreceive() for stream (TCP) sockets.
1900  * XXXAO: (MSG_WAITALL | MSG_PEEK) isn't properly handled.
1901  */
1902 int
1903 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
1904     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1905 {
1906 	int len = 0, error = 0, flags, oresid;
1907 	struct sockbuf *sb;
1908 	struct mbuf *m, *n = NULL;
1909 
1910 	/* We only do stream sockets. */
1911 	if (so->so_type != SOCK_STREAM)
1912 		return (EINVAL);
1913 	if (psa != NULL)
1914 		*psa = NULL;
1915 	if (controlp != NULL)
1916 		return (EINVAL);
1917 	if (flagsp != NULL)
1918 		flags = *flagsp &~ MSG_EOR;
1919 	else
1920 		flags = 0;
1921 	if (flags & MSG_OOB)
1922 		return (soreceive_rcvoob(so, uio, flags));
1923 	if (mp0 != NULL)
1924 		*mp0 = NULL;
1925 
1926 	sb = &so->so_rcv;
1927 
1928 	/* Prevent other readers from entering the socket. */
1929 	error = sblock(sb, SBLOCKWAIT(flags));
1930 	if (error)
1931 		goto out;
1932 	SOCKBUF_LOCK(sb);
1933 
1934 	/* Easy one, no space to copyout anything. */
1935 	if (uio->uio_resid == 0) {
1936 		error = EINVAL;
1937 		goto out;
1938 	}
1939 	oresid = uio->uio_resid;
1940 
1941 	/* We will never ever get anything unless we are or were connected. */
1942 	if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1943 		error = ENOTCONN;
1944 		goto out;
1945 	}
1946 
1947 restart:
1948 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1949 
1950 	/* Abort if socket has reported problems. */
1951 	if (so->so_error) {
1952 		if (sb->sb_cc > 0)
1953 			goto deliver;
1954 		if (oresid > uio->uio_resid)
1955 			goto out;
1956 		error = so->so_error;
1957 		if (!(flags & MSG_PEEK))
1958 			so->so_error = 0;
1959 		goto out;
1960 	}
1961 
1962 	/* Door is closed.  Deliver what is left, if any. */
1963 	if (sb->sb_state & SBS_CANTRCVMORE) {
1964 		if (sb->sb_cc > 0)
1965 			goto deliver;
1966 		else
1967 			goto out;
1968 	}
1969 
1970 	/* Socket buffer is empty and we shall not block. */
1971 	if (sb->sb_cc == 0 &&
1972 	    ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
1973 		error = EAGAIN;
1974 		goto out;
1975 	}
1976 
1977 	/* Socket buffer got some data that we shall deliver now. */
1978 	if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) &&
1979 	    ((sb->sb_flags & SS_NBIO) ||
1980 	     (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
1981 	     sb->sb_cc >= sb->sb_lowat ||
1982 	     sb->sb_cc >= uio->uio_resid ||
1983 	     sb->sb_cc >= sb->sb_hiwat) ) {
1984 		goto deliver;
1985 	}
1986 
1987 	/* On MSG_WAITALL we must wait until all data or error arrives. */
1988 	if ((flags & MSG_WAITALL) &&
1989 	    (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_hiwat))
1990 		goto deliver;
1991 
1992 	/*
1993 	 * Wait and block until (more) data comes in.
1994 	 * NB: Drops the sockbuf lock during wait.
1995 	 */
1996 	error = sbwait(sb);
1997 	if (error)
1998 		goto out;
1999 	goto restart;
2000 
2001 deliver:
2002 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2003 	KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__));
2004 	KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
2005 
2006 	/* Statistics. */
2007 	if (uio->uio_td)
2008 		uio->uio_td->td_ru.ru_msgrcv++;
2009 
2010 	/* Fill uio until full or current end of socket buffer is reached. */
2011 	len = min(uio->uio_resid, sb->sb_cc);
2012 	if (mp0 != NULL) {
2013 		/* Dequeue as many mbufs as possible. */
2014 		if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
2015 			if (*mp0 == NULL)
2016 				*mp0 = sb->sb_mb;
2017 			else
2018 				m_cat(*mp0, sb->sb_mb);
2019 			for (m = sb->sb_mb;
2020 			     m != NULL && m->m_len <= len;
2021 			     m = m->m_next) {
2022 				len -= m->m_len;
2023 				uio->uio_resid -= m->m_len;
2024 				sbfree(sb, m);
2025 				n = m;
2026 			}
2027 			n->m_next = NULL;
2028 			sb->sb_mb = m;
2029 			sb->sb_lastrecord = sb->sb_mb;
2030 			if (sb->sb_mb == NULL)
2031 				SB_EMPTY_FIXUP(sb);
2032 		}
2033 		/* Copy the remainder. */
2034 		if (len > 0) {
2035 			KASSERT(sb->sb_mb != NULL,
2036 			    ("%s: len > 0 && sb->sb_mb empty", __func__));
2037 
2038 			m = m_copym(sb->sb_mb, 0, len, M_NOWAIT);
2039 			if (m == NULL)
2040 				len = 0;	/* Don't flush data from sockbuf. */
2041 			else
2042 				uio->uio_resid -= len;
2043 			if (*mp0 != NULL)
2044 				m_cat(*mp0, m);
2045 			else
2046 				*mp0 = m;
2047 			if (*mp0 == NULL) {
2048 				error = ENOBUFS;
2049 				goto out;
2050 			}
2051 		}
2052 	} else {
2053 		/* NB: Must unlock socket buffer as uiomove may sleep. */
2054 		SOCKBUF_UNLOCK(sb);
2055 		error = m_mbuftouio(uio, sb->sb_mb, len);
2056 		SOCKBUF_LOCK(sb);
2057 		if (error)
2058 			goto out;
2059 	}
2060 	SBLASTRECORDCHK(sb);
2061 	SBLASTMBUFCHK(sb);
2062 
2063 	/*
2064 	 * Remove the delivered data from the socket buffer unless we
2065 	 * were only peeking.
2066 	 */
2067 	if (!(flags & MSG_PEEK)) {
2068 		if (len > 0)
2069 			sbdrop_locked(sb, len);
2070 
2071 		/* Notify protocol that we drained some data. */
2072 		if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
2073 		    (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
2074 		     !(flags & MSG_SOCALLBCK))) {
2075 			SOCKBUF_UNLOCK(sb);
2076 			VNET_SO_ASSERT(so);
2077 			(*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
2078 			SOCKBUF_LOCK(sb);
2079 		}
2080 	}
2081 
2082 	/*
2083 	 * For MSG_WAITALL we may have to loop again and wait for
2084 	 * more data to come in.
2085 	 */
2086 	if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
2087 		goto restart;
2088 out:
2089 	SOCKBUF_LOCK_ASSERT(sb);
2090 	SBLASTRECORDCHK(sb);
2091 	SBLASTMBUFCHK(sb);
2092 	SOCKBUF_UNLOCK(sb);
2093 	sbunlock(sb);
2094 	return (error);
2095 }
2096 
2097 /*
2098  * Optimized version of soreceive() for simple datagram cases from userspace.
2099  * Unlike in the stream case, we're able to drop a datagram if copyout()
2100  * fails, and because we handle datagrams atomically, we don't need to use a
2101  * sleep lock to prevent I/O interlacing.
2102  */
2103 int
2104 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
2105     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2106 {
2107 	struct mbuf *m, *m2;
2108 	int flags, error;
2109 	ssize_t len;
2110 	struct protosw *pr = so->so_proto;
2111 	struct mbuf *nextrecord;
2112 
2113 	if (psa != NULL)
2114 		*psa = NULL;
2115 	if (controlp != NULL)
2116 		*controlp = NULL;
2117 	if (flagsp != NULL)
2118 		flags = *flagsp &~ MSG_EOR;
2119 	else
2120 		flags = 0;
2121 
2122 	/*
2123 	 * For any complicated cases, fall back to the full
2124 	 * soreceive_generic().
2125 	 */
2126 	if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
2127 		return (soreceive_generic(so, psa, uio, mp0, controlp,
2128 		    flagsp));
2129 
2130 	/*
2131 	 * Enforce restrictions on use.
2132 	 */
2133 	KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
2134 	    ("soreceive_dgram: wantrcvd"));
2135 	KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
2136 	KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
2137 	    ("soreceive_dgram: SBS_RCVATMARK"));
2138 	KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
2139 	    ("soreceive_dgram: P_CONNREQUIRED"));
2140 
2141 	/*
2142 	 * Loop blocking while waiting for a datagram.
2143 	 */
2144 	SOCKBUF_LOCK(&so->so_rcv);
2145 	while ((m = so->so_rcv.sb_mb) == NULL) {
2146 		KASSERT(so->so_rcv.sb_cc == 0,
2147 		    ("soreceive_dgram: sb_mb NULL but sb_cc %u",
2148 		    so->so_rcv.sb_cc));
2149 		if (so->so_error) {
2150 			error = so->so_error;
2151 			so->so_error = 0;
2152 			SOCKBUF_UNLOCK(&so->so_rcv);
2153 			return (error);
2154 		}
2155 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
2156 		    uio->uio_resid == 0) {
2157 			SOCKBUF_UNLOCK(&so->so_rcv);
2158 			return (0);
2159 		}
2160 		if ((so->so_state & SS_NBIO) ||
2161 		    (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2162 			SOCKBUF_UNLOCK(&so->so_rcv);
2163 			return (EWOULDBLOCK);
2164 		}
2165 		SBLASTRECORDCHK(&so->so_rcv);
2166 		SBLASTMBUFCHK(&so->so_rcv);
2167 		error = sbwait(&so->so_rcv);
2168 		if (error) {
2169 			SOCKBUF_UNLOCK(&so->so_rcv);
2170 			return (error);
2171 		}
2172 	}
2173 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2174 
2175 	if (uio->uio_td)
2176 		uio->uio_td->td_ru.ru_msgrcv++;
2177 	SBLASTRECORDCHK(&so->so_rcv);
2178 	SBLASTMBUFCHK(&so->so_rcv);
2179 	nextrecord = m->m_nextpkt;
2180 	if (nextrecord == NULL) {
2181 		KASSERT(so->so_rcv.sb_lastrecord == m,
2182 		    ("soreceive_dgram: lastrecord != m"));
2183 	}
2184 
2185 	KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
2186 	    ("soreceive_dgram: m_nextpkt != nextrecord"));
2187 
2188 	/*
2189 	 * Pull 'm' and its chain off the front of the packet queue.
2190 	 */
2191 	so->so_rcv.sb_mb = NULL;
2192 	sockbuf_pushsync(&so->so_rcv, nextrecord);
2193 
2194 	/*
2195 	 * Walk 'm's chain and free that many bytes from the socket buffer.
2196 	 */
2197 	for (m2 = m; m2 != NULL; m2 = m2->m_next)
2198 		sbfree(&so->so_rcv, m2);
2199 
2200 	/*
2201 	 * Do a few last checks before we let go of the lock.
2202 	 */
2203 	SBLASTRECORDCHK(&so->so_rcv);
2204 	SBLASTMBUFCHK(&so->so_rcv);
2205 	SOCKBUF_UNLOCK(&so->so_rcv);
2206 
2207 	if (pr->pr_flags & PR_ADDR) {
2208 		KASSERT(m->m_type == MT_SONAME,
2209 		    ("m->m_type == %d", m->m_type));
2210 		if (psa != NULL)
2211 			*psa = sodupsockaddr(mtod(m, struct sockaddr *),
2212 			    M_NOWAIT);
2213 		m = m_free(m);
2214 	}
2215 	if (m == NULL) {
2216 		/* XXXRW: Can this happen? */
2217 		return (0);
2218 	}
2219 
2220 	/*
2221 	 * Packet to copyout() is now in 'm' and it is disconnected from the
2222 	 * queue.
2223 	 *
2224 	 * Process one or more MT_CONTROL mbufs present before any data mbufs
2225 	 * in the first mbuf chain on the socket buffer.  We call into the
2226 	 * protocol to perform externalization (or freeing if controlp ==
2227 	 * NULL).
2228 	 */
2229 	if (m->m_type == MT_CONTROL) {
2230 		struct mbuf *cm = NULL, *cmn;
2231 		struct mbuf **cme = &cm;
2232 
2233 		do {
2234 			m2 = m->m_next;
2235 			m->m_next = NULL;
2236 			*cme = m;
2237 			cme = &(*cme)->m_next;
2238 			m = m2;
2239 		} while (m != NULL && m->m_type == MT_CONTROL);
2240 		while (cm != NULL) {
2241 			cmn = cm->m_next;
2242 			cm->m_next = NULL;
2243 			if (pr->pr_domain->dom_externalize != NULL) {
2244 				error = (*pr->pr_domain->dom_externalize)
2245 				    (cm, controlp, flags);
2246 			} else if (controlp != NULL)
2247 				*controlp = cm;
2248 			else
2249 				m_freem(cm);
2250 			if (controlp != NULL) {
2251 				while (*controlp != NULL)
2252 					controlp = &(*controlp)->m_next;
2253 			}
2254 			cm = cmn;
2255 		}
2256 	}
2257 	KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
2258 
2259 	while (m != NULL && uio->uio_resid > 0) {
2260 		len = uio->uio_resid;
2261 		if (len > m->m_len)
2262 			len = m->m_len;
2263 		error = uiomove(mtod(m, char *), (int)len, uio);
2264 		if (error) {
2265 			m_freem(m);
2266 			return (error);
2267 		}
2268 		if (len == m->m_len)
2269 			m = m_free(m);
2270 		else {
2271 			m->m_data += len;
2272 			m->m_len -= len;
2273 		}
2274 	}
2275 	if (m != NULL)
2276 		flags |= MSG_TRUNC;
2277 	m_freem(m);
2278 	if (flagsp != NULL)
2279 		*flagsp |= flags;
2280 	return (0);
2281 }
2282 
2283 int
2284 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2285     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2286 {
2287 	int error;
2288 
2289 	CURVNET_SET(so->so_vnet);
2290 	error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
2291 	    controlp, flagsp));
2292 	CURVNET_RESTORE();
2293 	return (error);
2294 }
2295 
2296 int
2297 soshutdown(struct socket *so, int how)
2298 {
2299 	struct protosw *pr = so->so_proto;
2300 	int error;
2301 
2302 	if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2303 		return (EINVAL);
2304 
2305 	CURVNET_SET(so->so_vnet);
2306 	if (pr->pr_usrreqs->pru_flush != NULL)
2307 		(*pr->pr_usrreqs->pru_flush)(so, how);
2308 	if (how != SHUT_WR)
2309 		sorflush(so);
2310 	if (how != SHUT_RD) {
2311 		error = (*pr->pr_usrreqs->pru_shutdown)(so);
2312 		wakeup(&so->so_timeo);
2313 		CURVNET_RESTORE();
2314 		return (error);
2315 	}
2316 	wakeup(&so->so_timeo);
2317 	CURVNET_RESTORE();
2318 	return (0);
2319 }
2320 
2321 void
2322 sorflush(struct socket *so)
2323 {
2324 	struct sockbuf *sb = &so->so_rcv;
2325 	struct protosw *pr = so->so_proto;
2326 	struct sockbuf asb;
2327 
2328 	VNET_SO_ASSERT(so);
2329 
2330 	/*
2331 	 * In order to avoid calling dom_dispose with the socket buffer mutex
2332 	 * held, and in order to generally avoid holding the lock for a long
2333 	 * time, we make a copy of the socket buffer and clear the original
2334 	 * (except locks, state).  The new socket buffer copy won't have
2335 	 * initialized locks so we can only call routines that won't use or
2336 	 * assert those locks.
2337 	 *
2338 	 * Dislodge threads currently blocked in receive and wait to acquire
2339 	 * a lock against other simultaneous readers before clearing the
2340 	 * socket buffer.  Don't let our acquire be interrupted by a signal
2341 	 * despite any existing socket disposition on interruptable waiting.
2342 	 */
2343 	socantrcvmore(so);
2344 	(void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2345 
2346 	/*
2347 	 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2348 	 * and mutex data unchanged.
2349 	 */
2350 	SOCKBUF_LOCK(sb);
2351 	bzero(&asb, offsetof(struct sockbuf, sb_startzero));
2352 	bcopy(&sb->sb_startzero, &asb.sb_startzero,
2353 	    sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2354 	bzero(&sb->sb_startzero,
2355 	    sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2356 	SOCKBUF_UNLOCK(sb);
2357 	sbunlock(sb);
2358 
2359 	/*
2360 	 * Dispose of special rights and flush the socket buffer.  Don't call
2361 	 * any unsafe routines (that rely on locks being initialized) on asb.
2362 	 */
2363 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2364 		(*pr->pr_domain->dom_dispose)(asb.sb_mb);
2365 	sbrelease_internal(&asb, so);
2366 }
2367 
2368 /*
2369  * Wrapper for Socket established helper hook.
2370  * Parameters: socket, context of the hook point, hook id.
2371  */
2372 static int inline
2373 hhook_run_socket(struct socket *so, void *hctx, int32_t h_id)
2374 {
2375 	struct socket_hhook_data hhook_data = {
2376 		.so = so,
2377 		.hctx = hctx,
2378 		.m = NULL
2379 	};
2380 
2381 	hhook_run_hooks(V_socket_hhh[h_id], &hhook_data, &so->osd);
2382 
2383 	/* Ugly but needed, since hhooks return void for now */
2384 	return (hhook_data.status);
2385 }
2386 
2387 /*
2388  * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2389  * additional variant to handle the case where the option value needs to be
2390  * some kind of integer, but not a specific size.  In addition to their use
2391  * here, these functions are also called by the protocol-level pr_ctloutput()
2392  * routines.
2393  */
2394 int
2395 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2396 {
2397 	size_t	valsize;
2398 
2399 	/*
2400 	 * If the user gives us more than we wanted, we ignore it, but if we
2401 	 * don't get the minimum length the caller wants, we return EINVAL.
2402 	 * On success, sopt->sopt_valsize is set to however much we actually
2403 	 * retrieved.
2404 	 */
2405 	if ((valsize = sopt->sopt_valsize) < minlen)
2406 		return EINVAL;
2407 	if (valsize > len)
2408 		sopt->sopt_valsize = valsize = len;
2409 
2410 	if (sopt->sopt_td != NULL)
2411 		return (copyin(sopt->sopt_val, buf, valsize));
2412 
2413 	bcopy(sopt->sopt_val, buf, valsize);
2414 	return (0);
2415 }
2416 
2417 /*
2418  * Kernel version of setsockopt(2).
2419  *
2420  * XXX: optlen is size_t, not socklen_t
2421  */
2422 int
2423 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2424     size_t optlen)
2425 {
2426 	struct sockopt sopt;
2427 
2428 	sopt.sopt_level = level;
2429 	sopt.sopt_name = optname;
2430 	sopt.sopt_dir = SOPT_SET;
2431 	sopt.sopt_val = optval;
2432 	sopt.sopt_valsize = optlen;
2433 	sopt.sopt_td = NULL;
2434 	return (sosetopt(so, &sopt));
2435 }
2436 
2437 int
2438 sosetopt(struct socket *so, struct sockopt *sopt)
2439 {
2440 	int	error, optval;
2441 	struct	linger l;
2442 	struct	timeval tv;
2443 	sbintime_t val;
2444 	uint32_t val32;
2445 #ifdef MAC
2446 	struct mac extmac;
2447 #endif
2448 
2449 	CURVNET_SET(so->so_vnet);
2450 	error = 0;
2451 	if (sopt->sopt_level != SOL_SOCKET) {
2452 		if (so->so_proto->pr_ctloutput != NULL) {
2453 			error = (*so->so_proto->pr_ctloutput)(so, sopt);
2454 			CURVNET_RESTORE();
2455 			return (error);
2456 		}
2457 		error = ENOPROTOOPT;
2458 	} else {
2459 		switch (sopt->sopt_name) {
2460 		case SO_ACCEPTFILTER:
2461 			error = do_setopt_accept_filter(so, sopt);
2462 			if (error)
2463 				goto bad;
2464 			break;
2465 
2466 		case SO_LINGER:
2467 			error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2468 			if (error)
2469 				goto bad;
2470 
2471 			SOCK_LOCK(so);
2472 			so->so_linger = l.l_linger;
2473 			if (l.l_onoff)
2474 				so->so_options |= SO_LINGER;
2475 			else
2476 				so->so_options &= ~SO_LINGER;
2477 			SOCK_UNLOCK(so);
2478 			break;
2479 
2480 		case SO_DEBUG:
2481 		case SO_KEEPALIVE:
2482 		case SO_DONTROUTE:
2483 		case SO_USELOOPBACK:
2484 		case SO_BROADCAST:
2485 		case SO_REUSEADDR:
2486 		case SO_REUSEPORT:
2487 		case SO_OOBINLINE:
2488 		case SO_TIMESTAMP:
2489 		case SO_BINTIME:
2490 		case SO_NOSIGPIPE:
2491 		case SO_NO_DDP:
2492 		case SO_NO_OFFLOAD:
2493 			error = sooptcopyin(sopt, &optval, sizeof optval,
2494 			    sizeof optval);
2495 			if (error)
2496 				goto bad;
2497 			SOCK_LOCK(so);
2498 			if (optval)
2499 				so->so_options |= sopt->sopt_name;
2500 			else
2501 				so->so_options &= ~sopt->sopt_name;
2502 			SOCK_UNLOCK(so);
2503 			break;
2504 
2505 		case SO_SETFIB:
2506 			error = sooptcopyin(sopt, &optval, sizeof optval,
2507 			    sizeof optval);
2508 			if (error)
2509 				goto bad;
2510 
2511 			if (optval < 0 || optval >= rt_numfibs) {
2512 				error = EINVAL;
2513 				goto bad;
2514 			}
2515 			if (((so->so_proto->pr_domain->dom_family == PF_INET) ||
2516 			   (so->so_proto->pr_domain->dom_family == PF_INET6) ||
2517 			   (so->so_proto->pr_domain->dom_family == PF_ROUTE)))
2518 				so->so_fibnum = optval;
2519 			else
2520 				so->so_fibnum = 0;
2521 			break;
2522 
2523 		case SO_USER_COOKIE:
2524 			error = sooptcopyin(sopt, &val32, sizeof val32,
2525 			    sizeof val32);
2526 			if (error)
2527 				goto bad;
2528 			so->so_user_cookie = val32;
2529 			break;
2530 
2531 		case SO_SNDBUF:
2532 		case SO_RCVBUF:
2533 		case SO_SNDLOWAT:
2534 		case SO_RCVLOWAT:
2535 			error = sooptcopyin(sopt, &optval, sizeof optval,
2536 			    sizeof optval);
2537 			if (error)
2538 				goto bad;
2539 
2540 			/*
2541 			 * Values < 1 make no sense for any of these options,
2542 			 * so disallow them.
2543 			 */
2544 			if (optval < 1) {
2545 				error = EINVAL;
2546 				goto bad;
2547 			}
2548 
2549 			switch (sopt->sopt_name) {
2550 			case SO_SNDBUF:
2551 			case SO_RCVBUF:
2552 				if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
2553 				    &so->so_snd : &so->so_rcv, (u_long)optval,
2554 				    so, curthread) == 0) {
2555 					error = ENOBUFS;
2556 					goto bad;
2557 				}
2558 				(sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
2559 				    &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
2560 				break;
2561 
2562 			/*
2563 			 * Make sure the low-water is never greater than the
2564 			 * high-water.
2565 			 */
2566 			case SO_SNDLOWAT:
2567 				SOCKBUF_LOCK(&so->so_snd);
2568 				so->so_snd.sb_lowat =
2569 				    (optval > so->so_snd.sb_hiwat) ?
2570 				    so->so_snd.sb_hiwat : optval;
2571 				SOCKBUF_UNLOCK(&so->so_snd);
2572 				break;
2573 			case SO_RCVLOWAT:
2574 				SOCKBUF_LOCK(&so->so_rcv);
2575 				so->so_rcv.sb_lowat =
2576 				    (optval > so->so_rcv.sb_hiwat) ?
2577 				    so->so_rcv.sb_hiwat : optval;
2578 				SOCKBUF_UNLOCK(&so->so_rcv);
2579 				break;
2580 			}
2581 			break;
2582 
2583 		case SO_SNDTIMEO:
2584 		case SO_RCVTIMEO:
2585 #ifdef COMPAT_FREEBSD32
2586 			if (SV_CURPROC_FLAG(SV_ILP32)) {
2587 				struct timeval32 tv32;
2588 
2589 				error = sooptcopyin(sopt, &tv32, sizeof tv32,
2590 				    sizeof tv32);
2591 				CP(tv32, tv, tv_sec);
2592 				CP(tv32, tv, tv_usec);
2593 			} else
2594 #endif
2595 				error = sooptcopyin(sopt, &tv, sizeof tv,
2596 				    sizeof tv);
2597 			if (error)
2598 				goto bad;
2599 			if (tv.tv_sec < 0 || tv.tv_usec < 0 ||
2600 			    tv.tv_usec >= 1000000) {
2601 				error = EDOM;
2602 				goto bad;
2603 			}
2604 			if (tv.tv_sec > INT32_MAX)
2605 				val = SBT_MAX;
2606 			else
2607 				val = tvtosbt(tv);
2608 			switch (sopt->sopt_name) {
2609 			case SO_SNDTIMEO:
2610 				so->so_snd.sb_timeo = val;
2611 				break;
2612 			case SO_RCVTIMEO:
2613 				so->so_rcv.sb_timeo = val;
2614 				break;
2615 			}
2616 			break;
2617 
2618 		case SO_LABEL:
2619 #ifdef MAC
2620 			error = sooptcopyin(sopt, &extmac, sizeof extmac,
2621 			    sizeof extmac);
2622 			if (error)
2623 				goto bad;
2624 			error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2625 			    so, &extmac);
2626 #else
2627 			error = EOPNOTSUPP;
2628 #endif
2629 			break;
2630 
2631 		default:
2632 			if (V_socket_hhh[HHOOK_SOCKET_OPT]->hhh_nhooks > 0)
2633 				error = hhook_run_socket(so, sopt,
2634 				    HHOOK_SOCKET_OPT);
2635 			else
2636 				error = ENOPROTOOPT;
2637 			break;
2638 		}
2639 		if (error == 0 && so->so_proto->pr_ctloutput != NULL)
2640 			(void)(*so->so_proto->pr_ctloutput)(so, sopt);
2641 	}
2642 bad:
2643 	CURVNET_RESTORE();
2644 	return (error);
2645 }
2646 
2647 /*
2648  * Helper routine for getsockopt.
2649  */
2650 int
2651 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2652 {
2653 	int	error;
2654 	size_t	valsize;
2655 
2656 	error = 0;
2657 
2658 	/*
2659 	 * Documented get behavior is that we always return a value, possibly
2660 	 * truncated to fit in the user's buffer.  Traditional behavior is
2661 	 * that we always tell the user precisely how much we copied, rather
2662 	 * than something useful like the total amount we had available for
2663 	 * her.  Note that this interface is not idempotent; the entire
2664 	 * answer must generated ahead of time.
2665 	 */
2666 	valsize = min(len, sopt->sopt_valsize);
2667 	sopt->sopt_valsize = valsize;
2668 	if (sopt->sopt_val != NULL) {
2669 		if (sopt->sopt_td != NULL)
2670 			error = copyout(buf, sopt->sopt_val, valsize);
2671 		else
2672 			bcopy(buf, sopt->sopt_val, valsize);
2673 	}
2674 	return (error);
2675 }
2676 
2677 int
2678 sogetopt(struct socket *so, struct sockopt *sopt)
2679 {
2680 	int	error, optval;
2681 	struct	linger l;
2682 	struct	timeval tv;
2683 #ifdef MAC
2684 	struct mac extmac;
2685 #endif
2686 
2687 	CURVNET_SET(so->so_vnet);
2688 	error = 0;
2689 	if (sopt->sopt_level != SOL_SOCKET) {
2690 		if (so->so_proto->pr_ctloutput != NULL)
2691 			error = (*so->so_proto->pr_ctloutput)(so, sopt);
2692 		else
2693 			error = ENOPROTOOPT;
2694 		CURVNET_RESTORE();
2695 		return (error);
2696 	} else {
2697 		switch (sopt->sopt_name) {
2698 		case SO_ACCEPTFILTER:
2699 			error = do_getopt_accept_filter(so, sopt);
2700 			break;
2701 
2702 		case SO_LINGER:
2703 			SOCK_LOCK(so);
2704 			l.l_onoff = so->so_options & SO_LINGER;
2705 			l.l_linger = so->so_linger;
2706 			SOCK_UNLOCK(so);
2707 			error = sooptcopyout(sopt, &l, sizeof l);
2708 			break;
2709 
2710 		case SO_USELOOPBACK:
2711 		case SO_DONTROUTE:
2712 		case SO_DEBUG:
2713 		case SO_KEEPALIVE:
2714 		case SO_REUSEADDR:
2715 		case SO_REUSEPORT:
2716 		case SO_BROADCAST:
2717 		case SO_OOBINLINE:
2718 		case SO_ACCEPTCONN:
2719 		case SO_TIMESTAMP:
2720 		case SO_BINTIME:
2721 		case SO_NOSIGPIPE:
2722 			optval = so->so_options & sopt->sopt_name;
2723 integer:
2724 			error = sooptcopyout(sopt, &optval, sizeof optval);
2725 			break;
2726 
2727 		case SO_TYPE:
2728 			optval = so->so_type;
2729 			goto integer;
2730 
2731 		case SO_PROTOCOL:
2732 			optval = so->so_proto->pr_protocol;
2733 			goto integer;
2734 
2735 		case SO_ERROR:
2736 			SOCK_LOCK(so);
2737 			optval = so->so_error;
2738 			so->so_error = 0;
2739 			SOCK_UNLOCK(so);
2740 			goto integer;
2741 
2742 		case SO_SNDBUF:
2743 			optval = so->so_snd.sb_hiwat;
2744 			goto integer;
2745 
2746 		case SO_RCVBUF:
2747 			optval = so->so_rcv.sb_hiwat;
2748 			goto integer;
2749 
2750 		case SO_SNDLOWAT:
2751 			optval = so->so_snd.sb_lowat;
2752 			goto integer;
2753 
2754 		case SO_RCVLOWAT:
2755 			optval = so->so_rcv.sb_lowat;
2756 			goto integer;
2757 
2758 		case SO_SNDTIMEO:
2759 		case SO_RCVTIMEO:
2760 			tv = sbttotv(sopt->sopt_name == SO_SNDTIMEO ?
2761 			    so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2762 #ifdef COMPAT_FREEBSD32
2763 			if (SV_CURPROC_FLAG(SV_ILP32)) {
2764 				struct timeval32 tv32;
2765 
2766 				CP(tv, tv32, tv_sec);
2767 				CP(tv, tv32, tv_usec);
2768 				error = sooptcopyout(sopt, &tv32, sizeof tv32);
2769 			} else
2770 #endif
2771 				error = sooptcopyout(sopt, &tv, sizeof tv);
2772 			break;
2773 
2774 		case SO_LABEL:
2775 #ifdef MAC
2776 			error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2777 			    sizeof(extmac));
2778 			if (error)
2779 				goto bad;
2780 			error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
2781 			    so, &extmac);
2782 			if (error)
2783 				goto bad;
2784 			error = sooptcopyout(sopt, &extmac, sizeof extmac);
2785 #else
2786 			error = EOPNOTSUPP;
2787 #endif
2788 			break;
2789 
2790 		case SO_PEERLABEL:
2791 #ifdef MAC
2792 			error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2793 			    sizeof(extmac));
2794 			if (error)
2795 				goto bad;
2796 			error = mac_getsockopt_peerlabel(
2797 			    sopt->sopt_td->td_ucred, so, &extmac);
2798 			if (error)
2799 				goto bad;
2800 			error = sooptcopyout(sopt, &extmac, sizeof extmac);
2801 #else
2802 			error = EOPNOTSUPP;
2803 #endif
2804 			break;
2805 
2806 		case SO_LISTENQLIMIT:
2807 			optval = so->so_qlimit;
2808 			goto integer;
2809 
2810 		case SO_LISTENQLEN:
2811 			optval = so->so_qlen;
2812 			goto integer;
2813 
2814 		case SO_LISTENINCQLEN:
2815 			optval = so->so_incqlen;
2816 			goto integer;
2817 
2818 		default:
2819 			if (V_socket_hhh[HHOOK_SOCKET_OPT]->hhh_nhooks > 0)
2820 				error = hhook_run_socket(so, sopt,
2821 				    HHOOK_SOCKET_OPT);
2822 			else
2823 				error = ENOPROTOOPT;
2824 			break;
2825 		}
2826 	}
2827 #ifdef MAC
2828 bad:
2829 #endif
2830 	CURVNET_RESTORE();
2831 	return (error);
2832 }
2833 
2834 int
2835 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2836 {
2837 	struct mbuf *m, *m_prev;
2838 	int sopt_size = sopt->sopt_valsize;
2839 
2840 	MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
2841 	if (m == NULL)
2842 		return ENOBUFS;
2843 	if (sopt_size > MLEN) {
2844 		MCLGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT);
2845 		if ((m->m_flags & M_EXT) == 0) {
2846 			m_free(m);
2847 			return ENOBUFS;
2848 		}
2849 		m->m_len = min(MCLBYTES, sopt_size);
2850 	} else {
2851 		m->m_len = min(MLEN, sopt_size);
2852 	}
2853 	sopt_size -= m->m_len;
2854 	*mp = m;
2855 	m_prev = m;
2856 
2857 	while (sopt_size) {
2858 		MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
2859 		if (m == NULL) {
2860 			m_freem(*mp);
2861 			return ENOBUFS;
2862 		}
2863 		if (sopt_size > MLEN) {
2864 			MCLGET(m, sopt->sopt_td != NULL ? M_WAITOK :
2865 			    M_NOWAIT);
2866 			if ((m->m_flags & M_EXT) == 0) {
2867 				m_freem(m);
2868 				m_freem(*mp);
2869 				return ENOBUFS;
2870 			}
2871 			m->m_len = min(MCLBYTES, sopt_size);
2872 		} else {
2873 			m->m_len = min(MLEN, sopt_size);
2874 		}
2875 		sopt_size -= m->m_len;
2876 		m_prev->m_next = m;
2877 		m_prev = m;
2878 	}
2879 	return (0);
2880 }
2881 
2882 int
2883 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2884 {
2885 	struct mbuf *m0 = m;
2886 
2887 	if (sopt->sopt_val == NULL)
2888 		return (0);
2889 	while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2890 		if (sopt->sopt_td != NULL) {
2891 			int error;
2892 
2893 			error = copyin(sopt->sopt_val, mtod(m, char *),
2894 			    m->m_len);
2895 			if (error != 0) {
2896 				m_freem(m0);
2897 				return(error);
2898 			}
2899 		} else
2900 			bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2901 		sopt->sopt_valsize -= m->m_len;
2902 		sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2903 		m = m->m_next;
2904 	}
2905 	if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2906 		panic("ip6_sooptmcopyin");
2907 	return (0);
2908 }
2909 
2910 int
2911 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2912 {
2913 	struct mbuf *m0 = m;
2914 	size_t valsize = 0;
2915 
2916 	if (sopt->sopt_val == NULL)
2917 		return (0);
2918 	while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2919 		if (sopt->sopt_td != NULL) {
2920 			int error;
2921 
2922 			error = copyout(mtod(m, char *), sopt->sopt_val,
2923 			    m->m_len);
2924 			if (error != 0) {
2925 				m_freem(m0);
2926 				return(error);
2927 			}
2928 		} else
2929 			bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2930 		sopt->sopt_valsize -= m->m_len;
2931 		sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2932 		valsize += m->m_len;
2933 		m = m->m_next;
2934 	}
2935 	if (m != NULL) {
2936 		/* enough soopt buffer should be given from user-land */
2937 		m_freem(m0);
2938 		return(EINVAL);
2939 	}
2940 	sopt->sopt_valsize = valsize;
2941 	return (0);
2942 }
2943 
2944 /*
2945  * sohasoutofband(): protocol notifies socket layer of the arrival of new
2946  * out-of-band data, which will then notify socket consumers.
2947  */
2948 void
2949 sohasoutofband(struct socket *so)
2950 {
2951 
2952 	if (so->so_sigio != NULL)
2953 		pgsigio(&so->so_sigio, SIGURG, 0);
2954 	selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
2955 }
2956 
2957 int
2958 sopoll(struct socket *so, int events, struct ucred *active_cred,
2959     struct thread *td)
2960 {
2961 
2962 	/*
2963 	 * We do not need to set or assert curvnet as long as everyone uses
2964 	 * sopoll_generic().
2965 	 */
2966 	return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
2967 	    td));
2968 }
2969 
2970 int
2971 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
2972     struct thread *td)
2973 {
2974 	int revents = 0;
2975 
2976 	SOCKBUF_LOCK(&so->so_snd);
2977 	SOCKBUF_LOCK(&so->so_rcv);
2978 	if (events & (POLLIN | POLLRDNORM))
2979 		if (soreadabledata(so))
2980 			revents |= events & (POLLIN | POLLRDNORM);
2981 
2982 	if (events & (POLLOUT | POLLWRNORM))
2983 		if (sowriteable(so))
2984 			revents |= events & (POLLOUT | POLLWRNORM);
2985 
2986 	if (events & (POLLPRI | POLLRDBAND))
2987 		if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
2988 			revents |= events & (POLLPRI | POLLRDBAND);
2989 
2990 	if ((events & POLLINIGNEOF) == 0) {
2991 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2992 			revents |= events & (POLLIN | POLLRDNORM);
2993 			if (so->so_snd.sb_state & SBS_CANTSENDMORE)
2994 				revents |= POLLHUP;
2995 		}
2996 	}
2997 
2998 	if (revents == 0) {
2999 		if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
3000 			selrecord(td, &so->so_rcv.sb_sel);
3001 			so->so_rcv.sb_flags |= SB_SEL;
3002 		}
3003 
3004 		if (events & (POLLOUT | POLLWRNORM)) {
3005 			selrecord(td, &so->so_snd.sb_sel);
3006 			so->so_snd.sb_flags |= SB_SEL;
3007 		}
3008 	}
3009 
3010 	SOCKBUF_UNLOCK(&so->so_rcv);
3011 	SOCKBUF_UNLOCK(&so->so_snd);
3012 	return (revents);
3013 }
3014 
3015 int
3016 soo_kqfilter(struct file *fp, struct knote *kn)
3017 {
3018 	struct socket *so = kn->kn_fp->f_data;
3019 	struct sockbuf *sb;
3020 
3021 	switch (kn->kn_filter) {
3022 	case EVFILT_READ:
3023 		if (so->so_options & SO_ACCEPTCONN)
3024 			kn->kn_fop = &solisten_filtops;
3025 		else
3026 			kn->kn_fop = &soread_filtops;
3027 		sb = &so->so_rcv;
3028 		break;
3029 	case EVFILT_WRITE:
3030 		kn->kn_fop = &sowrite_filtops;
3031 		sb = &so->so_snd;
3032 		break;
3033 	default:
3034 		return (EINVAL);
3035 	}
3036 
3037 	SOCKBUF_LOCK(sb);
3038 	knlist_add(&sb->sb_sel.si_note, kn, 1);
3039 	sb->sb_flags |= SB_KNOTE;
3040 	SOCKBUF_UNLOCK(sb);
3041 	return (0);
3042 }
3043 
3044 /*
3045  * Some routines that return EOPNOTSUPP for entry points that are not
3046  * supported by a protocol.  Fill in as needed.
3047  */
3048 int
3049 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
3050 {
3051 
3052 	return EOPNOTSUPP;
3053 }
3054 
3055 int
3056 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
3057 {
3058 
3059 	return EOPNOTSUPP;
3060 }
3061 
3062 int
3063 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3064 {
3065 
3066 	return EOPNOTSUPP;
3067 }
3068 
3069 int
3070 pru_bindat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
3071     struct thread *td)
3072 {
3073 
3074 	return EOPNOTSUPP;
3075 }
3076 
3077 int
3078 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3079 {
3080 
3081 	return EOPNOTSUPP;
3082 }
3083 
3084 int
3085 pru_connectat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
3086     struct thread *td)
3087 {
3088 
3089 	return EOPNOTSUPP;
3090 }
3091 
3092 int
3093 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
3094 {
3095 
3096 	return EOPNOTSUPP;
3097 }
3098 
3099 int
3100 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
3101     struct ifnet *ifp, struct thread *td)
3102 {
3103 
3104 	return EOPNOTSUPP;
3105 }
3106 
3107 int
3108 pru_disconnect_notsupp(struct socket *so)
3109 {
3110 
3111 	return EOPNOTSUPP;
3112 }
3113 
3114 int
3115 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
3116 {
3117 
3118 	return EOPNOTSUPP;
3119 }
3120 
3121 int
3122 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
3123 {
3124 
3125 	return EOPNOTSUPP;
3126 }
3127 
3128 int
3129 pru_rcvd_notsupp(struct socket *so, int flags)
3130 {
3131 
3132 	return EOPNOTSUPP;
3133 }
3134 
3135 int
3136 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
3137 {
3138 
3139 	return EOPNOTSUPP;
3140 }
3141 
3142 int
3143 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
3144     struct sockaddr *addr, struct mbuf *control, struct thread *td)
3145 {
3146 
3147 	return EOPNOTSUPP;
3148 }
3149 
3150 /*
3151  * This isn't really a ``null'' operation, but it's the default one and
3152  * doesn't do anything destructive.
3153  */
3154 int
3155 pru_sense_null(struct socket *so, struct stat *sb)
3156 {
3157 
3158 	sb->st_blksize = so->so_snd.sb_hiwat;
3159 	return 0;
3160 }
3161 
3162 int
3163 pru_shutdown_notsupp(struct socket *so)
3164 {
3165 
3166 	return EOPNOTSUPP;
3167 }
3168 
3169 int
3170 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
3171 {
3172 
3173 	return EOPNOTSUPP;
3174 }
3175 
3176 int
3177 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
3178     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
3179 {
3180 
3181 	return EOPNOTSUPP;
3182 }
3183 
3184 int
3185 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
3186     struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
3187 {
3188 
3189 	return EOPNOTSUPP;
3190 }
3191 
3192 int
3193 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
3194     struct thread *td)
3195 {
3196 
3197 	return EOPNOTSUPP;
3198 }
3199 
3200 static void
3201 filt_sordetach(struct knote *kn)
3202 {
3203 	struct socket *so = kn->kn_fp->f_data;
3204 
3205 	SOCKBUF_LOCK(&so->so_rcv);
3206 	knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
3207 	if (knlist_empty(&so->so_rcv.sb_sel.si_note))
3208 		so->so_rcv.sb_flags &= ~SB_KNOTE;
3209 	SOCKBUF_UNLOCK(&so->so_rcv);
3210 }
3211 
3212 /*ARGSUSED*/
3213 static int
3214 filt_soread(struct knote *kn, long hint)
3215 {
3216 	struct socket *so;
3217 
3218 	so = kn->kn_fp->f_data;
3219 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
3220 
3221 	kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
3222 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3223 		kn->kn_flags |= EV_EOF;
3224 		kn->kn_fflags = so->so_error;
3225 		return (1);
3226 	} else if (so->so_error)	/* temporary udp error */
3227 		return (1);
3228 
3229 	if (kn->kn_sfflags & NOTE_LOWAT) {
3230 		if (kn->kn_data >= kn->kn_sdata)
3231 			return 1;
3232 	} else {
3233 		if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat)
3234 			return 1;
3235 	}
3236 
3237 	if (V_socket_hhh[HHOOK_FILT_SOREAD]->hhh_nhooks > 0)
3238 		/* This hook returning non-zero indicates an event, not error */
3239 		return (hhook_run_socket(so, NULL, HHOOK_FILT_SOREAD));
3240 
3241 	return (0);
3242 }
3243 
3244 static void
3245 filt_sowdetach(struct knote *kn)
3246 {
3247 	struct socket *so = kn->kn_fp->f_data;
3248 
3249 	SOCKBUF_LOCK(&so->so_snd);
3250 	knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
3251 	if (knlist_empty(&so->so_snd.sb_sel.si_note))
3252 		so->so_snd.sb_flags &= ~SB_KNOTE;
3253 	SOCKBUF_UNLOCK(&so->so_snd);
3254 }
3255 
3256 /*ARGSUSED*/
3257 static int
3258 filt_sowrite(struct knote *kn, long hint)
3259 {
3260 	struct socket *so;
3261 
3262 	so = kn->kn_fp->f_data;
3263 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
3264 	kn->kn_data = sbspace(&so->so_snd);
3265 
3266 	if (V_socket_hhh[HHOOK_FILT_SOWRITE]->hhh_nhooks > 0)
3267 		hhook_run_socket(so, kn, HHOOK_FILT_SOWRITE);
3268 
3269 	if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
3270 		kn->kn_flags |= EV_EOF;
3271 		kn->kn_fflags = so->so_error;
3272 		return (1);
3273 	} else if (so->so_error)	/* temporary udp error */
3274 		return (1);
3275 	else if (((so->so_state & SS_ISCONNECTED) == 0) &&
3276 	    (so->so_proto->pr_flags & PR_CONNREQUIRED))
3277 		return (0);
3278 	else if (kn->kn_sfflags & NOTE_LOWAT)
3279 		return (kn->kn_data >= kn->kn_sdata);
3280 	else
3281 		return (kn->kn_data >= so->so_snd.sb_lowat);
3282 }
3283 
3284 /*ARGSUSED*/
3285 static int
3286 filt_solisten(struct knote *kn, long hint)
3287 {
3288 	struct socket *so = kn->kn_fp->f_data;
3289 
3290 	kn->kn_data = so->so_qlen;
3291 	return (!TAILQ_EMPTY(&so->so_comp));
3292 }
3293 
3294 int
3295 socheckuid(struct socket *so, uid_t uid)
3296 {
3297 
3298 	if (so == NULL)
3299 		return (EPERM);
3300 	if (so->so_cred->cr_uid != uid)
3301 		return (EPERM);
3302 	return (0);
3303 }
3304 
3305 /*
3306  * These functions are used by protocols to notify the socket layer (and its
3307  * consumers) of state changes in the sockets driven by protocol-side events.
3308  */
3309 
3310 /*
3311  * Procedures to manipulate state flags of socket and do appropriate wakeups.
3312  *
3313  * Normal sequence from the active (originating) side is that
3314  * soisconnecting() is called during processing of connect() call, resulting
3315  * in an eventual call to soisconnected() if/when the connection is
3316  * established.  When the connection is torn down soisdisconnecting() is
3317  * called during processing of disconnect() call, and soisdisconnected() is
3318  * called when the connection to the peer is totally severed.  The semantics
3319  * of these routines are such that connectionless protocols can call
3320  * soisconnected() and soisdisconnected() only, bypassing the in-progress
3321  * calls when setting up a ``connection'' takes no time.
3322  *
3323  * From the passive side, a socket is created with two queues of sockets:
3324  * so_incomp for connections in progress and so_comp for connections already
3325  * made and awaiting user acceptance.  As a protocol is preparing incoming
3326  * connections, it creates a socket structure queued on so_incomp by calling
3327  * sonewconn().  When the connection is established, soisconnected() is
3328  * called, and transfers the socket structure to so_comp, making it available
3329  * to accept().
3330  *
3331  * If a socket is closed with sockets on either so_incomp or so_comp, these
3332  * sockets are dropped.
3333  *
3334  * If higher-level protocols are implemented in the kernel, the wakeups done
3335  * here will sometimes cause software-interrupt process scheduling.
3336  */
3337 void
3338 soisconnecting(struct socket *so)
3339 {
3340 
3341 	SOCK_LOCK(so);
3342 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3343 	so->so_state |= SS_ISCONNECTING;
3344 	SOCK_UNLOCK(so);
3345 }
3346 
3347 void
3348 soisconnected(struct socket *so)
3349 {
3350 	struct socket *head;
3351 	int ret;
3352 
3353 restart:
3354 	ACCEPT_LOCK();
3355 	SOCK_LOCK(so);
3356 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3357 	so->so_state |= SS_ISCONNECTED;
3358 	head = so->so_head;
3359 	if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
3360 		if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3361 			SOCK_UNLOCK(so);
3362 			TAILQ_REMOVE(&head->so_incomp, so, so_list);
3363 			head->so_incqlen--;
3364 			so->so_qstate &= ~SQ_INCOMP;
3365 			TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
3366 			head->so_qlen++;
3367 			so->so_qstate |= SQ_COMP;
3368 			ACCEPT_UNLOCK();
3369 			sorwakeup(head);
3370 			wakeup_one(&head->so_timeo);
3371 		} else {
3372 			ACCEPT_UNLOCK();
3373 			soupcall_set(so, SO_RCV,
3374 			    head->so_accf->so_accept_filter->accf_callback,
3375 			    head->so_accf->so_accept_filter_arg);
3376 			so->so_options &= ~SO_ACCEPTFILTER;
3377 			ret = head->so_accf->so_accept_filter->accf_callback(so,
3378 			    head->so_accf->so_accept_filter_arg, M_NOWAIT);
3379 			if (ret == SU_ISCONNECTED)
3380 				soupcall_clear(so, SO_RCV);
3381 			SOCK_UNLOCK(so);
3382 			if (ret == SU_ISCONNECTED)
3383 				goto restart;
3384 		}
3385 		return;
3386 	}
3387 	SOCK_UNLOCK(so);
3388 	ACCEPT_UNLOCK();
3389 	wakeup(&so->so_timeo);
3390 	sorwakeup(so);
3391 	sowwakeup(so);
3392 }
3393 
3394 void
3395 soisdisconnecting(struct socket *so)
3396 {
3397 
3398 	/*
3399 	 * Note: This code assumes that SOCK_LOCK(so) and
3400 	 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3401 	 */
3402 	SOCKBUF_LOCK(&so->so_rcv);
3403 	so->so_state &= ~SS_ISCONNECTING;
3404 	so->so_state |= SS_ISDISCONNECTING;
3405 	so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3406 	sorwakeup_locked(so);
3407 	SOCKBUF_LOCK(&so->so_snd);
3408 	so->so_snd.sb_state |= SBS_CANTSENDMORE;
3409 	sowwakeup_locked(so);
3410 	wakeup(&so->so_timeo);
3411 }
3412 
3413 void
3414 soisdisconnected(struct socket *so)
3415 {
3416 
3417 	/*
3418 	 * Note: This code assumes that SOCK_LOCK(so) and
3419 	 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3420 	 */
3421 	SOCKBUF_LOCK(&so->so_rcv);
3422 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3423 	so->so_state |= SS_ISDISCONNECTED;
3424 	so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3425 	sorwakeup_locked(so);
3426 	SOCKBUF_LOCK(&so->so_snd);
3427 	so->so_snd.sb_state |= SBS_CANTSENDMORE;
3428 	sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
3429 	sowwakeup_locked(so);
3430 	wakeup(&so->so_timeo);
3431 }
3432 
3433 /*
3434  * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3435  */
3436 struct sockaddr *
3437 sodupsockaddr(const struct sockaddr *sa, int mflags)
3438 {
3439 	struct sockaddr *sa2;
3440 
3441 	sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3442 	if (sa2)
3443 		bcopy(sa, sa2, sa->sa_len);
3444 	return sa2;
3445 }
3446 
3447 /*
3448  * Register per-socket buffer upcalls.
3449  */
3450 void
3451 soupcall_set(struct socket *so, int which,
3452     int (*func)(struct socket *, void *, int), void *arg)
3453 {
3454 	struct sockbuf *sb;
3455 
3456 	switch (which) {
3457 	case SO_RCV:
3458 		sb = &so->so_rcv;
3459 		break;
3460 	case SO_SND:
3461 		sb = &so->so_snd;
3462 		break;
3463 	default:
3464 		panic("soupcall_set: bad which");
3465 	}
3466 	SOCKBUF_LOCK_ASSERT(sb);
3467 #if 0
3468 	/* XXX: accf_http actually wants to do this on purpose. */
3469 	KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall"));
3470 #endif
3471 	sb->sb_upcall = func;
3472 	sb->sb_upcallarg = arg;
3473 	sb->sb_flags |= SB_UPCALL;
3474 }
3475 
3476 void
3477 soupcall_clear(struct socket *so, int which)
3478 {
3479 	struct sockbuf *sb;
3480 
3481 	switch (which) {
3482 	case SO_RCV:
3483 		sb = &so->so_rcv;
3484 		break;
3485 	case SO_SND:
3486 		sb = &so->so_snd;
3487 		break;
3488 	default:
3489 		panic("soupcall_clear: bad which");
3490 	}
3491 	SOCKBUF_LOCK_ASSERT(sb);
3492 	KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear"));
3493 	sb->sb_upcall = NULL;
3494 	sb->sb_upcallarg = NULL;
3495 	sb->sb_flags &= ~SB_UPCALL;
3496 }
3497 
3498 /*
3499  * Create an external-format (``xsocket'') structure using the information in
3500  * the kernel-format socket structure pointed to by so.  This is done to
3501  * reduce the spew of irrelevant information over this interface, to isolate
3502  * user code from changes in the kernel structure, and potentially to provide
3503  * information-hiding if we decide that some of this information should be
3504  * hidden from users.
3505  */
3506 void
3507 sotoxsocket(struct socket *so, struct xsocket *xso)
3508 {
3509 
3510 	xso->xso_len = sizeof *xso;
3511 	xso->xso_so = so;
3512 	xso->so_type = so->so_type;
3513 	xso->so_options = so->so_options;
3514 	xso->so_linger = so->so_linger;
3515 	xso->so_state = so->so_state;
3516 	xso->so_pcb = so->so_pcb;
3517 	xso->xso_protocol = so->so_proto->pr_protocol;
3518 	xso->xso_family = so->so_proto->pr_domain->dom_family;
3519 	xso->so_qlen = so->so_qlen;
3520 	xso->so_incqlen = so->so_incqlen;
3521 	xso->so_qlimit = so->so_qlimit;
3522 	xso->so_timeo = so->so_timeo;
3523 	xso->so_error = so->so_error;
3524 	xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
3525 	xso->so_oobmark = so->so_oobmark;
3526 	sbtoxsockbuf(&so->so_snd, &xso->so_snd);
3527 	sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
3528 	xso->so_uid = so->so_cred->cr_uid;
3529 }
3530 
3531 
3532 /*
3533  * Socket accessor functions to provide external consumers with
3534  * a safe interface to socket state
3535  *
3536  */
3537 
3538 void
3539 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *),
3540     void *arg)
3541 {
3542 
3543 	TAILQ_FOREACH(so, &so->so_comp, so_list)
3544 		func(so, arg);
3545 }
3546 
3547 struct sockbuf *
3548 so_sockbuf_rcv(struct socket *so)
3549 {
3550 
3551 	return (&so->so_rcv);
3552 }
3553 
3554 struct sockbuf *
3555 so_sockbuf_snd(struct socket *so)
3556 {
3557 
3558 	return (&so->so_snd);
3559 }
3560 
3561 int
3562 so_state_get(const struct socket *so)
3563 {
3564 
3565 	return (so->so_state);
3566 }
3567 
3568 void
3569 so_state_set(struct socket *so, int val)
3570 {
3571 
3572 	so->so_state = val;
3573 }
3574 
3575 int
3576 so_options_get(const struct socket *so)
3577 {
3578 
3579 	return (so->so_options);
3580 }
3581 
3582 void
3583 so_options_set(struct socket *so, int val)
3584 {
3585 
3586 	so->so_options = val;
3587 }
3588 
3589 int
3590 so_error_get(const struct socket *so)
3591 {
3592 
3593 	return (so->so_error);
3594 }
3595 
3596 void
3597 so_error_set(struct socket *so, int val)
3598 {
3599 
3600 	so->so_error = val;
3601 }
3602 
3603 int
3604 so_linger_get(const struct socket *so)
3605 {
3606 
3607 	return (so->so_linger);
3608 }
3609 
3610 void
3611 so_linger_set(struct socket *so, int val)
3612 {
3613 
3614 	so->so_linger = val;
3615 }
3616 
3617 struct protosw *
3618 so_protosw_get(const struct socket *so)
3619 {
3620 
3621 	return (so->so_proto);
3622 }
3623 
3624 void
3625 so_protosw_set(struct socket *so, struct protosw *val)
3626 {
3627 
3628 	so->so_proto = val;
3629 }
3630 
3631 void
3632 so_sorwakeup(struct socket *so)
3633 {
3634 
3635 	sorwakeup(so);
3636 }
3637 
3638 void
3639 so_sowwakeup(struct socket *so)
3640 {
3641 
3642 	sowwakeup(so);
3643 }
3644 
3645 void
3646 so_sorwakeup_locked(struct socket *so)
3647 {
3648 
3649 	sorwakeup_locked(so);
3650 }
3651 
3652 void
3653 so_sowwakeup_locked(struct socket *so)
3654 {
3655 
3656 	sowwakeup_locked(so);
3657 }
3658 
3659 void
3660 so_lock(struct socket *so)
3661 {
3662 
3663 	SOCK_LOCK(so);
3664 }
3665 
3666 void
3667 so_unlock(struct socket *so)
3668 {
3669 
3670 	SOCK_UNLOCK(so);
3671 }
3672