xref: /freebsd/sys/kern/uipc_socket.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
1 /*-
2  * Copyright (c) 2004 The FreeBSD Foundation
3  * Copyright (c) 2004-2005 Robert N. M. Watson
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)uipc_socket.c	8.3 (Berkeley) 4/15/94
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_inet.h"
38 #include "opt_mac.h"
39 #include "opt_zero.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/fcntl.h>
44 #include <sys/limits.h>
45 #include <sys/lock.h>
46 #include <sys/mac.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/mutex.h>
50 #include <sys/domain.h>
51 #include <sys/file.h>			/* for struct knote */
52 #include <sys/kernel.h>
53 #include <sys/event.h>
54 #include <sys/poll.h>
55 #include <sys/proc.h>
56 #include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/resourcevar.h>
60 #include <sys/signalvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/uio.h>
63 #include <sys/jail.h>
64 
65 #include <vm/uma.h>
66 
67 
68 static int	soreceive_rcvoob(struct socket *so, struct uio *uio,
69 		    int flags);
70 
71 static void	filt_sordetach(struct knote *kn);
72 static int	filt_soread(struct knote *kn, long hint);
73 static void	filt_sowdetach(struct knote *kn);
74 static int	filt_sowrite(struct knote *kn, long hint);
75 static int	filt_solisten(struct knote *kn, long hint);
76 
77 static struct filterops solisten_filtops =
78 	{ 1, NULL, filt_sordetach, filt_solisten };
79 static struct filterops soread_filtops =
80 	{ 1, NULL, filt_sordetach, filt_soread };
81 static struct filterops sowrite_filtops =
82 	{ 1, NULL, filt_sowdetach, filt_sowrite };
83 
84 uma_zone_t socket_zone;
85 so_gen_t	so_gencnt;	/* generation count for sockets */
86 
87 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
88 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
89 
90 SYSCTL_DECL(_kern_ipc);
91 
92 static int somaxconn = SOMAXCONN;
93 static int somaxconn_sysctl(SYSCTL_HANDLER_ARGS);
94 /* XXX: we dont have SYSCTL_USHORT */
95 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLTYPE_UINT | CTLFLAG_RW,
96     0, sizeof(int), somaxconn_sysctl, "I", "Maximum pending socket connection "
97     "queue size");
98 static int numopensockets;
99 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
100     &numopensockets, 0, "Number of open sockets");
101 #ifdef ZERO_COPY_SOCKETS
102 /* These aren't static because they're used in other files. */
103 int so_zero_copy_send = 1;
104 int so_zero_copy_receive = 1;
105 SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0,
106     "Zero copy controls");
107 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW,
108     &so_zero_copy_receive, 0, "Enable zero copy receive");
109 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW,
110     &so_zero_copy_send, 0, "Enable zero copy send");
111 #endif /* ZERO_COPY_SOCKETS */
112 
113 /*
114  * accept_mtx locks down per-socket fields relating to accept queues.  See
115  * socketvar.h for an annotation of the protected fields of struct socket.
116  */
117 struct mtx accept_mtx;
118 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
119 
120 /*
121  * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
122  * so_gencnt field.
123  */
124 static struct mtx so_global_mtx;
125 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
126 
127 /*
128  * Socket operation routines.
129  * These routines are called by the routines in
130  * sys_socket.c or from a system process, and
131  * implement the semantics of socket operations by
132  * switching out to the protocol specific routines.
133  */
134 
135 /*
136  * Get a socket structure from our zone, and initialize it.
137  * Note that it would probably be better to allocate socket
138  * and PCB at the same time, but I'm not convinced that all
139  * the protocols can be easily modified to do this.
140  *
141  * soalloc() returns a socket with a ref count of 0.
142  */
143 struct socket *
144 soalloc(int mflags)
145 {
146 	struct socket *so;
147 
148 	so = uma_zalloc(socket_zone, mflags | M_ZERO);
149 	if (so != NULL) {
150 #ifdef MAC
151 		if (mac_init_socket(so, mflags) != 0) {
152 			uma_zfree(socket_zone, so);
153 			return (NULL);
154 		}
155 #endif
156 		SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
157 		SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
158 		TAILQ_INIT(&so->so_aiojobq);
159 		mtx_lock(&so_global_mtx);
160 		so->so_gencnt = ++so_gencnt;
161 		++numopensockets;
162 		mtx_unlock(&so_global_mtx);
163 	}
164 	return (so);
165 }
166 
167 /*
168  * socreate returns a socket with a ref count of 1.  The socket should be
169  * closed with soclose().
170  */
171 int
172 socreate(dom, aso, type, proto, cred, td)
173 	int dom;
174 	struct socket **aso;
175 	int type;
176 	int proto;
177 	struct ucred *cred;
178 	struct thread *td;
179 {
180 	struct protosw *prp;
181 	struct socket *so;
182 	int error;
183 
184 	if (proto)
185 		prp = pffindproto(dom, proto, type);
186 	else
187 		prp = pffindtype(dom, type);
188 
189 	if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
190 	    prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
191 		return (EPROTONOSUPPORT);
192 
193 	if (jailed(cred) && jail_socket_unixiproute_only &&
194 	    prp->pr_domain->dom_family != PF_LOCAL &&
195 	    prp->pr_domain->dom_family != PF_INET &&
196 	    prp->pr_domain->dom_family != PF_ROUTE) {
197 		return (EPROTONOSUPPORT);
198 	}
199 
200 	if (prp->pr_type != type)
201 		return (EPROTOTYPE);
202 	so = soalloc(M_WAITOK);
203 	if (so == NULL)
204 		return (ENOBUFS);
205 
206 	TAILQ_INIT(&so->so_incomp);
207 	TAILQ_INIT(&so->so_comp);
208 	so->so_type = type;
209 	so->so_cred = crhold(cred);
210 	so->so_proto = prp;
211 #ifdef MAC
212 	mac_create_socket(cred, so);
213 #endif
214 	knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv),
215 	    NULL, NULL, NULL);
216 	knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd),
217 	    NULL, NULL, NULL);
218 	so->so_count = 1;
219 	error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
220 	if (error) {
221 		ACCEPT_LOCK();
222 		SOCK_LOCK(so);
223 		so->so_state |= SS_NOFDREF;
224 		sorele(so);
225 		return (error);
226 	}
227 	*aso = so;
228 	return (0);
229 }
230 
231 int
232 sobind(so, nam, td)
233 	struct socket *so;
234 	struct sockaddr *nam;
235 	struct thread *td;
236 {
237 
238 	return ((*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td));
239 }
240 
241 void
242 sodealloc(struct socket *so)
243 {
244 
245 	KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
246 	mtx_lock(&so_global_mtx);
247 	so->so_gencnt = ++so_gencnt;
248 	mtx_unlock(&so_global_mtx);
249 	if (so->so_rcv.sb_hiwat)
250 		(void)chgsbsize(so->so_cred->cr_uidinfo,
251 		    &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
252 	if (so->so_snd.sb_hiwat)
253 		(void)chgsbsize(so->so_cred->cr_uidinfo,
254 		    &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
255 #ifdef INET
256 	/* remove acccept filter if one is present. */
257 	if (so->so_accf != NULL)
258 		do_setopt_accept_filter(so, NULL);
259 #endif
260 #ifdef MAC
261 	mac_destroy_socket(so);
262 #endif
263 	crfree(so->so_cred);
264 	SOCKBUF_LOCK_DESTROY(&so->so_snd);
265 	SOCKBUF_LOCK_DESTROY(&so->so_rcv);
266 	uma_zfree(socket_zone, so);
267 	mtx_lock(&so_global_mtx);
268 	--numopensockets;
269 	mtx_unlock(&so_global_mtx);
270 }
271 
272 /*
273  * solisten() transitions a socket from a non-listening state to a listening
274  * state, but can also be used to update the listen queue depth on an
275  * existing listen socket.  The protocol will call back into the sockets
276  * layer using solisten_proto_check() and solisten_proto() to check and set
277  * socket-layer listen state.  Call backs are used so that the protocol can
278  * acquire both protocol and socket layer locks in whatever order is required
279  * by the protocol.
280  *
281  * Protocol implementors are advised to hold the socket lock across the
282  * socket-layer test and set to avoid races at the socket layer.
283  */
284 int
285 solisten(so, backlog, td)
286 	struct socket *so;
287 	int backlog;
288 	struct thread *td;
289 {
290 	int error;
291 
292 	error = (*so->so_proto->pr_usrreqs->pru_listen)(so, td);
293 	if (error)
294 		return (error);
295 
296 	/*
297 	 * XXXRW: The following state adjustment should occur in
298 	 * solisten_proto(), but we don't currently pass the backlog request
299 	 * to the protocol via pru_listen().
300 	 */
301 	if (backlog < 0 || backlog > somaxconn)
302 		backlog = somaxconn;
303 	so->so_qlimit = backlog;
304 	return (0);
305 }
306 
307 int
308 solisten_proto_check(so)
309 	struct socket *so;
310 {
311 
312 	SOCK_LOCK_ASSERT(so);
313 
314 	if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
315 	    SS_ISDISCONNECTING))
316 		return (EINVAL);
317 	return (0);
318 }
319 
320 void
321 solisten_proto(so)
322 	struct socket *so;
323 {
324 
325 	SOCK_LOCK_ASSERT(so);
326 
327 	so->so_options |= SO_ACCEPTCONN;
328 }
329 
330 /*
331  * Attempt to free a socket.  This should really be sotryfree().
332  *
333  * We free the socket if the protocol is no longer interested in the socket,
334  * there's no file descriptor reference, and the refcount is 0.  While the
335  * calling macro sotryfree() tests the refcount, sofree() has to test it
336  * again as it's possible to race with an accept()ing thread if the socket is
337  * in an listen queue of a listen socket, as being in the listen queue
338  * doesn't elevate the reference count.  sofree() acquires the accept mutex
339  * early for this test in order to avoid that race.
340  */
341 void
342 sofree(so)
343 	struct socket *so;
344 {
345 	struct socket *head;
346 
347 	ACCEPT_LOCK_ASSERT();
348 	SOCK_LOCK_ASSERT(so);
349 
350 	if (so->so_pcb != NULL || (so->so_state & SS_NOFDREF) == 0 ||
351 	    so->so_count != 0) {
352 		SOCK_UNLOCK(so);
353 		ACCEPT_UNLOCK();
354 		return;
355 	}
356 
357 	head = so->so_head;
358 	if (head != NULL) {
359 		KASSERT((so->so_qstate & SQ_COMP) != 0 ||
360 		    (so->so_qstate & SQ_INCOMP) != 0,
361 		    ("sofree: so_head != NULL, but neither SQ_COMP nor "
362 		    "SQ_INCOMP"));
363 		KASSERT((so->so_qstate & SQ_COMP) == 0 ||
364 		    (so->so_qstate & SQ_INCOMP) == 0,
365 		    ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
366 		/*
367 		 * accept(2) is responsible draining the completed
368 		 * connection queue and freeing those sockets, so
369 		 * we just return here if this socket is currently
370 		 * on the completed connection queue.  Otherwise,
371 		 * accept(2) may hang after select(2) has indicating
372 		 * that a listening socket was ready.  If it's an
373 		 * incomplete connection, we remove it from the queue
374 		 * and free it; otherwise, it won't be released until
375 		 * the listening socket is closed.
376 		 */
377 		if ((so->so_qstate & SQ_COMP) != 0) {
378 			SOCK_UNLOCK(so);
379 			ACCEPT_UNLOCK();
380 			return;
381 		}
382 		TAILQ_REMOVE(&head->so_incomp, so, so_list);
383 		head->so_incqlen--;
384 		so->so_qstate &= ~SQ_INCOMP;
385 		so->so_head = NULL;
386 	}
387 	KASSERT((so->so_qstate & SQ_COMP) == 0 &&
388 	    (so->so_qstate & SQ_INCOMP) == 0,
389 	    ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
390 	    so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
391 	SOCK_UNLOCK(so);
392 	ACCEPT_UNLOCK();
393 	SOCKBUF_LOCK(&so->so_snd);
394 	so->so_snd.sb_flags |= SB_NOINTR;
395 	(void)sblock(&so->so_snd, M_WAITOK);
396 	/*
397 	 * socantsendmore_locked() drops the socket buffer mutex so that it
398 	 * can safely perform wakeups.  Re-acquire the mutex before
399 	 * continuing.
400 	 */
401 	socantsendmore_locked(so);
402 	SOCKBUF_LOCK(&so->so_snd);
403 	sbunlock(&so->so_snd);
404 	sbrelease_locked(&so->so_snd, so);
405 	SOCKBUF_UNLOCK(&so->so_snd);
406 	sorflush(so);
407 	knlist_destroy(&so->so_rcv.sb_sel.si_note);
408 	knlist_destroy(&so->so_snd.sb_sel.si_note);
409 	sodealloc(so);
410 }
411 
412 /*
413  * Close a socket on last file table reference removal.
414  * Initiate disconnect if connected.
415  * Free socket when disconnect complete.
416  *
417  * This function will sorele() the socket.  Note that soclose() may be
418  * called prior to the ref count reaching zero.  The actual socket
419  * structure will not be freed until the ref count reaches zero.
420  */
421 int
422 soclose(so)
423 	struct socket *so;
424 {
425 	int error = 0;
426 
427 	KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
428 
429 	funsetown(&so->so_sigio);
430 	if (so->so_options & SO_ACCEPTCONN) {
431 		struct socket *sp;
432 		ACCEPT_LOCK();
433 		while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
434 			TAILQ_REMOVE(&so->so_incomp, sp, so_list);
435 			so->so_incqlen--;
436 			sp->so_qstate &= ~SQ_INCOMP;
437 			sp->so_head = NULL;
438 			ACCEPT_UNLOCK();
439 			(void) soabort(sp);
440 			ACCEPT_LOCK();
441 		}
442 		while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
443 			TAILQ_REMOVE(&so->so_comp, sp, so_list);
444 			so->so_qlen--;
445 			sp->so_qstate &= ~SQ_COMP;
446 			sp->so_head = NULL;
447 			ACCEPT_UNLOCK();
448 			(void) soabort(sp);
449 			ACCEPT_LOCK();
450 		}
451 		ACCEPT_UNLOCK();
452 	}
453 	if (so->so_pcb == NULL)
454 		goto discard;
455 	if (so->so_state & SS_ISCONNECTED) {
456 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
457 			error = sodisconnect(so);
458 			if (error)
459 				goto drop;
460 		}
461 		if (so->so_options & SO_LINGER) {
462 			if ((so->so_state & SS_ISDISCONNECTING) &&
463 			    (so->so_state & SS_NBIO))
464 				goto drop;
465 			while (so->so_state & SS_ISCONNECTED) {
466 				error = tsleep(&so->so_timeo,
467 				    PSOCK | PCATCH, "soclos", so->so_linger * hz);
468 				if (error)
469 					break;
470 			}
471 		}
472 	}
473 drop:
474 	if (so->so_pcb != NULL) {
475 		int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
476 		if (error == 0)
477 			error = error2;
478 	}
479 discard:
480 	ACCEPT_LOCK();
481 	SOCK_LOCK(so);
482 	KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
483 	so->so_state |= SS_NOFDREF;
484 	sorele(so);
485 	return (error);
486 }
487 
488 /*
489  * soabort() must not be called with any socket locks held, as it calls
490  * into the protocol, which will call back into the socket code causing
491  * it to acquire additional socket locks that may cause recursion or lock
492  * order reversals.
493  */
494 int
495 soabort(so)
496 	struct socket *so;
497 {
498 	int error;
499 
500 	error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
501 	if (error) {
502 		ACCEPT_LOCK();
503 		SOCK_LOCK(so);
504 		sotryfree(so);	/* note: does not decrement the ref count */
505 		return error;
506 	}
507 	return (0);
508 }
509 
510 int
511 soaccept(so, nam)
512 	struct socket *so;
513 	struct sockaddr **nam;
514 {
515 	int error;
516 
517 	SOCK_LOCK(so);
518 	KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
519 	so->so_state &= ~SS_NOFDREF;
520 	SOCK_UNLOCK(so);
521 	error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
522 	return (error);
523 }
524 
525 int
526 soconnect(so, nam, td)
527 	struct socket *so;
528 	struct sockaddr *nam;
529 	struct thread *td;
530 {
531 	int error;
532 
533 	if (so->so_options & SO_ACCEPTCONN)
534 		return (EOPNOTSUPP);
535 	/*
536 	 * If protocol is connection-based, can only connect once.
537 	 * Otherwise, if connected, try to disconnect first.
538 	 * This allows user to disconnect by connecting to, e.g.,
539 	 * a null address.
540 	 */
541 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
542 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
543 	    (error = sodisconnect(so)))) {
544 		error = EISCONN;
545 	} else {
546 		/*
547 		 * Prevent accumulated error from previous connection
548 		 * from biting us.
549 		 */
550 		so->so_error = 0;
551 		error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
552 	}
553 
554 	return (error);
555 }
556 
557 int
558 soconnect2(so1, so2)
559 	struct socket *so1;
560 	struct socket *so2;
561 {
562 
563 	return ((*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2));
564 }
565 
566 int
567 sodisconnect(so)
568 	struct socket *so;
569 {
570 	int error;
571 
572 	if ((so->so_state & SS_ISCONNECTED) == 0)
573 		return (ENOTCONN);
574 	if (so->so_state & SS_ISDISCONNECTING)
575 		return (EALREADY);
576 	error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
577 	return (error);
578 }
579 
580 #define	SBLOCKWAIT(f)	(((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
581 /*
582  * Send on a socket.
583  * If send must go all at once and message is larger than
584  * send buffering, then hard error.
585  * Lock against other senders.
586  * If must go all at once and not enough room now, then
587  * inform user that this would block and do nothing.
588  * Otherwise, if nonblocking, send as much as possible.
589  * The data to be sent is described by "uio" if nonzero,
590  * otherwise by the mbuf chain "top" (which must be null
591  * if uio is not).  Data provided in mbuf chain must be small
592  * enough to send all at once.
593  *
594  * Returns nonzero on error, timeout or signal; callers
595  * must check for short counts if EINTR/ERESTART are returned.
596  * Data and control buffers are freed on return.
597  */
598 
599 #ifdef ZERO_COPY_SOCKETS
600 struct so_zerocopy_stats{
601 	int size_ok;
602 	int align_ok;
603 	int found_ifp;
604 };
605 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
606 #include <netinet/in.h>
607 #include <net/route.h>
608 #include <netinet/in_pcb.h>
609 #include <vm/vm.h>
610 #include <vm/vm_page.h>
611 #include <vm/vm_object.h>
612 #endif /*ZERO_COPY_SOCKETS*/
613 
614 int
615 sosend(so, addr, uio, top, control, flags, td)
616 	struct socket *so;
617 	struct sockaddr *addr;
618 	struct uio *uio;
619 	struct mbuf *top;
620 	struct mbuf *control;
621 	int flags;
622 	struct thread *td;
623 {
624 	struct mbuf **mp;
625 	struct mbuf *m;
626 	long space, len = 0, resid;
627 	int clen = 0, error, dontroute;
628 	int atomic = sosendallatonce(so) || top;
629 #ifdef ZERO_COPY_SOCKETS
630 	int cow_send;
631 #endif /* ZERO_COPY_SOCKETS */
632 
633 	if (uio != NULL)
634 		resid = uio->uio_resid;
635 	else
636 		resid = top->m_pkthdr.len;
637 	/*
638 	 * In theory resid should be unsigned.
639 	 * However, space must be signed, as it might be less than 0
640 	 * if we over-committed, and we must use a signed comparison
641 	 * of space and resid.  On the other hand, a negative resid
642 	 * causes us to loop sending 0-length segments to the protocol.
643 	 *
644 	 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
645 	 * type sockets since that's an error.
646 	 */
647 	if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
648 		error = EINVAL;
649 		goto out;
650 	}
651 
652 	dontroute =
653 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
654 	    (so->so_proto->pr_flags & PR_ATOMIC);
655 	if (td != NULL)
656 		td->td_proc->p_stats->p_ru.ru_msgsnd++;
657 	if (control != NULL)
658 		clen = control->m_len;
659 #define	snderr(errno)	{ error = (errno); goto release; }
660 
661 	SOCKBUF_LOCK(&so->so_snd);
662 restart:
663 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
664 	error = sblock(&so->so_snd, SBLOCKWAIT(flags));
665 	if (error)
666 		goto out_locked;
667 	do {
668 		SOCKBUF_LOCK_ASSERT(&so->so_snd);
669 		if (so->so_snd.sb_state & SBS_CANTSENDMORE)
670 			snderr(EPIPE);
671 		if (so->so_error) {
672 			error = so->so_error;
673 			so->so_error = 0;
674 			goto release;
675 		}
676 		if ((so->so_state & SS_ISCONNECTED) == 0) {
677 			/*
678 			 * `sendto' and `sendmsg' is allowed on a connection-
679 			 * based socket if it supports implied connect.
680 			 * Return ENOTCONN if not connected and no address is
681 			 * supplied.
682 			 */
683 			if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
684 			    (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
685 				if ((so->so_state & SS_ISCONFIRMING) == 0 &&
686 				    !(resid == 0 && clen != 0))
687 					snderr(ENOTCONN);
688 			} else if (addr == NULL)
689 			    snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
690 				   ENOTCONN : EDESTADDRREQ);
691 		}
692 		space = sbspace(&so->so_snd);
693 		if (flags & MSG_OOB)
694 			space += 1024;
695 		if ((atomic && resid > so->so_snd.sb_hiwat) ||
696 		    clen > so->so_snd.sb_hiwat)
697 			snderr(EMSGSIZE);
698 		if (space < resid + clen &&
699 		    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
700 			if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO))
701 				snderr(EWOULDBLOCK);
702 			sbunlock(&so->so_snd);
703 			error = sbwait(&so->so_snd);
704 			if (error)
705 				goto out_locked;
706 			goto restart;
707 		}
708 		SOCKBUF_UNLOCK(&so->so_snd);
709 		mp = &top;
710 		space -= clen;
711 		do {
712 		    if (uio == NULL) {
713 			/*
714 			 * Data is prepackaged in "top".
715 			 */
716 			resid = 0;
717 			if (flags & MSG_EOR)
718 				top->m_flags |= M_EOR;
719 		    } else do {
720 #ifdef ZERO_COPY_SOCKETS
721 			cow_send = 0;
722 #endif /* ZERO_COPY_SOCKETS */
723 			if (resid >= MINCLSIZE) {
724 #ifdef ZERO_COPY_SOCKETS
725 				if (top == NULL) {
726 					MGETHDR(m, M_TRYWAIT, MT_DATA);
727 					if (m == NULL) {
728 						error = ENOBUFS;
729 						SOCKBUF_LOCK(&so->so_snd);
730 						goto release;
731 					}
732 					m->m_pkthdr.len = 0;
733 					m->m_pkthdr.rcvif = NULL;
734 				} else {
735 					MGET(m, M_TRYWAIT, MT_DATA);
736 					if (m == NULL) {
737 						error = ENOBUFS;
738 						SOCKBUF_LOCK(&so->so_snd);
739 						goto release;
740 					}
741 				}
742 				if (so_zero_copy_send &&
743 				    resid>=PAGE_SIZE &&
744 				    space>=PAGE_SIZE &&
745 				    uio->uio_iov->iov_len>=PAGE_SIZE) {
746 					so_zerocp_stats.size_ok++;
747 					so_zerocp_stats.align_ok++;
748 					cow_send = socow_setup(m, uio);
749 					len = cow_send;
750 				}
751 				if (!cow_send) {
752 					MCLGET(m, M_TRYWAIT);
753 					if ((m->m_flags & M_EXT) == 0) {
754 						m_free(m);
755 						m = NULL;
756 					} else {
757 						len = min(min(MCLBYTES, resid), space);
758 					}
759 				}
760 #else /* ZERO_COPY_SOCKETS */
761 				if (top == NULL) {
762 					m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
763 					m->m_pkthdr.len = 0;
764 					m->m_pkthdr.rcvif = NULL;
765 				} else
766 					m = m_getcl(M_TRYWAIT, MT_DATA, 0);
767 				len = min(min(MCLBYTES, resid), space);
768 #endif /* ZERO_COPY_SOCKETS */
769 			} else {
770 				if (top == NULL) {
771 					m = m_gethdr(M_TRYWAIT, MT_DATA);
772 					m->m_pkthdr.len = 0;
773 					m->m_pkthdr.rcvif = NULL;
774 
775 					len = min(min(MHLEN, resid), space);
776 					/*
777 					 * For datagram protocols, leave room
778 					 * for protocol headers in first mbuf.
779 					 */
780 					if (atomic && m && len < MHLEN)
781 						MH_ALIGN(m, len);
782 				} else {
783 					m = m_get(M_TRYWAIT, MT_DATA);
784 					len = min(min(MLEN, resid), space);
785 				}
786 			}
787 			if (m == NULL) {
788 				error = ENOBUFS;
789 				SOCKBUF_LOCK(&so->so_snd);
790 				goto release;
791 			}
792 
793 			space -= len;
794 #ifdef ZERO_COPY_SOCKETS
795 			if (cow_send)
796 				error = 0;
797 			else
798 #endif /* ZERO_COPY_SOCKETS */
799 			error = uiomove(mtod(m, void *), (int)len, uio);
800 			resid = uio->uio_resid;
801 			m->m_len = len;
802 			*mp = m;
803 			top->m_pkthdr.len += len;
804 			if (error) {
805 				SOCKBUF_LOCK(&so->so_snd);
806 				goto release;
807 			}
808 			mp = &m->m_next;
809 			if (resid <= 0) {
810 				if (flags & MSG_EOR)
811 					top->m_flags |= M_EOR;
812 				break;
813 			}
814 		    } while (space > 0 && atomic);
815 		    if (dontroute) {
816 			    SOCK_LOCK(so);
817 			    so->so_options |= SO_DONTROUTE;
818 			    SOCK_UNLOCK(so);
819 		    }
820 		    /*
821 		     * XXX all the SBS_CANTSENDMORE checks previously
822 		     * done could be out of date.  We could have recieved
823 		     * a reset packet in an interrupt or maybe we slept
824 		     * while doing page faults in uiomove() etc. We could
825 		     * probably recheck again inside the locking protection
826 		     * here, but there are probably other places that this
827 		     * also happens.  We must rethink this.
828 		     */
829 		    error = (*so->so_proto->pr_usrreqs->pru_send)(so,
830 			(flags & MSG_OOB) ? PRUS_OOB :
831 			/*
832 			 * If the user set MSG_EOF, the protocol
833 			 * understands this flag and nothing left to
834 			 * send then use PRU_SEND_EOF instead of PRU_SEND.
835 			 */
836 			((flags & MSG_EOF) &&
837 			 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
838 			 (resid <= 0)) ?
839 				PRUS_EOF :
840 			/* If there is more to send set PRUS_MORETOCOME */
841 			(resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
842 			top, addr, control, td);
843 		    if (dontroute) {
844 			    SOCK_LOCK(so);
845 			    so->so_options &= ~SO_DONTROUTE;
846 			    SOCK_UNLOCK(so);
847 		    }
848 		    clen = 0;
849 		    control = NULL;
850 		    top = NULL;
851 		    mp = &top;
852 		    if (error) {
853 			SOCKBUF_LOCK(&so->so_snd);
854 			goto release;
855 		    }
856 		} while (resid && space > 0);
857 		SOCKBUF_LOCK(&so->so_snd);
858 	} while (resid);
859 
860 release:
861 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
862 	sbunlock(&so->so_snd);
863 out_locked:
864 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
865 	SOCKBUF_UNLOCK(&so->so_snd);
866 out:
867 	if (top != NULL)
868 		m_freem(top);
869 	if (control != NULL)
870 		m_freem(control);
871 	return (error);
872 }
873 
874 /*
875  * The part of soreceive() that implements reading non-inline out-of-band
876  * data from a socket.  For more complete comments, see soreceive(), from
877  * which this code originated.
878  *
879  * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
880  * unable to return an mbuf chain to the caller.
881  */
882 static int
883 soreceive_rcvoob(so, uio, flags)
884 	struct socket *so;
885 	struct uio *uio;
886 	int flags;
887 {
888 	struct protosw *pr = so->so_proto;
889 	struct mbuf *m;
890 	int error;
891 
892 	KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
893 
894 	m = m_get(M_TRYWAIT, MT_DATA);
895 	if (m == NULL)
896 		return (ENOBUFS);
897 	error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
898 	if (error)
899 		goto bad;
900 	do {
901 #ifdef ZERO_COPY_SOCKETS
902 		if (so_zero_copy_receive) {
903 			int disposable;
904 
905 			if ((m->m_flags & M_EXT)
906 			 && (m->m_ext.ext_type == EXT_DISPOSABLE))
907 				disposable = 1;
908 			else
909 				disposable = 0;
910 
911 			error = uiomoveco(mtod(m, void *),
912 					  min(uio->uio_resid, m->m_len),
913 					  uio, disposable);
914 		} else
915 #endif /* ZERO_COPY_SOCKETS */
916 		error = uiomove(mtod(m, void *),
917 		    (int) min(uio->uio_resid, m->m_len), uio);
918 		m = m_free(m);
919 	} while (uio->uio_resid && error == 0 && m);
920 bad:
921 	if (m != NULL)
922 		m_freem(m);
923 	return (error);
924 }
925 
926 /*
927  * Following replacement or removal of the first mbuf on the first mbuf chain
928  * of a socket buffer, push necessary state changes back into the socket
929  * buffer so that other consumers see the values consistently.  'nextrecord'
930  * is the callers locally stored value of the original value of
931  * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
932  * NOTE: 'nextrecord' may be NULL.
933  */
934 static __inline void
935 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
936 {
937 
938 	SOCKBUF_LOCK_ASSERT(sb);
939 	/*
940 	 * First, update for the new value of nextrecord.  If necessary, make
941 	 * it the first record.
942 	 */
943 	if (sb->sb_mb != NULL)
944 		sb->sb_mb->m_nextpkt = nextrecord;
945 	else
946 		sb->sb_mb = nextrecord;
947 
948         /*
949          * Now update any dependent socket buffer fields to reflect the new
950          * state.  This is an expanded inline of SB_EMPTY_FIXUP(), with the
951 	 * addition of a second clause that takes care of the case where
952 	 * sb_mb has been updated, but remains the last record.
953          */
954         if (sb->sb_mb == NULL) {
955                 sb->sb_mbtail = NULL;
956                 sb->sb_lastrecord = NULL;
957         } else if (sb->sb_mb->m_nextpkt == NULL)
958                 sb->sb_lastrecord = sb->sb_mb;
959 }
960 
961 
962 /*
963  * Implement receive operations on a socket.
964  * We depend on the way that records are added to the sockbuf
965  * by sbappend*.  In particular, each record (mbufs linked through m_next)
966  * must begin with an address if the protocol so specifies,
967  * followed by an optional mbuf or mbufs containing ancillary data,
968  * and then zero or more mbufs of data.
969  * In order to avoid blocking network interrupts for the entire time here,
970  * we splx() while doing the actual copy to user space.
971  * Although the sockbuf is locked, new data may still be appended,
972  * and thus we must maintain consistency of the sockbuf during that time.
973  *
974  * The caller may receive the data as a single mbuf chain by supplying
975  * an mbuf **mp0 for use in returning the chain.  The uio is then used
976  * only for the count in uio_resid.
977  */
978 int
979 soreceive(so, psa, uio, mp0, controlp, flagsp)
980 	struct socket *so;
981 	struct sockaddr **psa;
982 	struct uio *uio;
983 	struct mbuf **mp0;
984 	struct mbuf **controlp;
985 	int *flagsp;
986 {
987 	struct mbuf *m, **mp;
988 	int flags, len, error, offset;
989 	struct protosw *pr = so->so_proto;
990 	struct mbuf *nextrecord;
991 	int moff, type = 0;
992 	int orig_resid = uio->uio_resid;
993 
994 	mp = mp0;
995 	if (psa != NULL)
996 		*psa = NULL;
997 	if (controlp != NULL)
998 		*controlp = NULL;
999 	if (flagsp != NULL)
1000 		flags = *flagsp &~ MSG_EOR;
1001 	else
1002 		flags = 0;
1003 	if (flags & MSG_OOB)
1004 		return (soreceive_rcvoob(so, uio, flags));
1005 	if (mp != NULL)
1006 		*mp = NULL;
1007 	if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1008 	    && uio->uio_resid)
1009 		(*pr->pr_usrreqs->pru_rcvd)(so, 0);
1010 
1011 	SOCKBUF_LOCK(&so->so_rcv);
1012 restart:
1013 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1014 	error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1015 	if (error)
1016 		goto out;
1017 
1018 	m = so->so_rcv.sb_mb;
1019 	/*
1020 	 * If we have less data than requested, block awaiting more
1021 	 * (subject to any timeout) if:
1022 	 *   1. the current count is less than the low water mark, or
1023 	 *   2. MSG_WAITALL is set, and it is possible to do the entire
1024 	 *	receive operation at once if we block (resid <= hiwat).
1025 	 *   3. MSG_DONTWAIT is not set
1026 	 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1027 	 * we have to do the receive in sections, and thus risk returning
1028 	 * a short count if a timeout or signal occurs after we start.
1029 	 */
1030 	if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1031 	    so->so_rcv.sb_cc < uio->uio_resid) &&
1032 	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1033 	    ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1034 	    m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1035 		KASSERT(m != NULL || !so->so_rcv.sb_cc,
1036 		    ("receive: m == %p so->so_rcv.sb_cc == %u",
1037 		    m, so->so_rcv.sb_cc));
1038 		if (so->so_error) {
1039 			if (m != NULL)
1040 				goto dontblock;
1041 			error = so->so_error;
1042 			if ((flags & MSG_PEEK) == 0)
1043 				so->so_error = 0;
1044 			goto release;
1045 		}
1046 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1047 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1048 			if (m)
1049 				goto dontblock;
1050 			else
1051 				goto release;
1052 		}
1053 		for (; m != NULL; m = m->m_next)
1054 			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
1055 				m = so->so_rcv.sb_mb;
1056 				goto dontblock;
1057 			}
1058 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1059 		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1060 			error = ENOTCONN;
1061 			goto release;
1062 		}
1063 		if (uio->uio_resid == 0)
1064 			goto release;
1065 		if ((so->so_state & SS_NBIO) ||
1066 		    (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1067 			error = EWOULDBLOCK;
1068 			goto release;
1069 		}
1070 		SBLASTRECORDCHK(&so->so_rcv);
1071 		SBLASTMBUFCHK(&so->so_rcv);
1072 		sbunlock(&so->so_rcv);
1073 		error = sbwait(&so->so_rcv);
1074 		if (error)
1075 			goto out;
1076 		goto restart;
1077 	}
1078 dontblock:
1079 	/*
1080 	 * From this point onward, we maintain 'nextrecord' as a cache of the
1081 	 * pointer to the next record in the socket buffer.  We must keep the
1082 	 * various socket buffer pointers and local stack versions of the
1083 	 * pointers in sync, pushing out modifications before dropping the
1084 	 * socket buffer mutex, and re-reading them when picking it up.
1085 	 *
1086 	 * Otherwise, we will race with the network stack appending new data
1087 	 * or records onto the socket buffer by using inconsistent/stale
1088 	 * versions of the field, possibly resulting in socket buffer
1089 	 * corruption.
1090 	 *
1091 	 * By holding the high-level sblock(), we prevent simultaneous
1092 	 * readers from pulling off the front of the socket buffer.
1093 	 */
1094 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1095 	if (uio->uio_td)
1096 		uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++;
1097 	KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1098 	SBLASTRECORDCHK(&so->so_rcv);
1099 	SBLASTMBUFCHK(&so->so_rcv);
1100 	nextrecord = m->m_nextpkt;
1101 	if (pr->pr_flags & PR_ADDR) {
1102 		KASSERT(m->m_type == MT_SONAME,
1103 		    ("m->m_type == %d", m->m_type));
1104 		orig_resid = 0;
1105 		if (psa != NULL)
1106 			*psa = sodupsockaddr(mtod(m, struct sockaddr *),
1107 			    M_NOWAIT);
1108 		if (flags & MSG_PEEK) {
1109 			m = m->m_next;
1110 		} else {
1111 			sbfree(&so->so_rcv, m);
1112 			so->so_rcv.sb_mb = m_free(m);
1113 			m = so->so_rcv.sb_mb;
1114 			sockbuf_pushsync(&so->so_rcv, nextrecord);
1115 		}
1116 	}
1117 
1118 	/*
1119 	 * Process one or more MT_CONTROL mbufs present before any data mbufs
1120 	 * in the first mbuf chain on the socket buffer.  If MSG_PEEK, we
1121 	 * just copy the data; if !MSG_PEEK, we call into the protocol to
1122 	 * perform externalization (or freeing if controlp == NULL).
1123 	 */
1124 	if (m != NULL && m->m_type == MT_CONTROL) {
1125 		struct mbuf *cm = NULL, *cmn;
1126 		struct mbuf **cme = &cm;
1127 
1128 		do {
1129 			if (flags & MSG_PEEK) {
1130 				if (controlp != NULL) {
1131 					*controlp = m_copy(m, 0, m->m_len);
1132 					controlp = &(*controlp)->m_next;
1133 				}
1134 				m = m->m_next;
1135 			} else {
1136 				sbfree(&so->so_rcv, m);
1137 				so->so_rcv.sb_mb = m->m_next;
1138 				m->m_next = NULL;
1139 				*cme = m;
1140 				cme = &(*cme)->m_next;
1141 				m = so->so_rcv.sb_mb;
1142 			}
1143 		} while (m != NULL && m->m_type == MT_CONTROL);
1144 		if ((flags & MSG_PEEK) == 0)
1145 			sockbuf_pushsync(&so->so_rcv, nextrecord);
1146 		while (cm != NULL) {
1147 			cmn = cm->m_next;
1148 			cm->m_next = NULL;
1149 			if (pr->pr_domain->dom_externalize != NULL) {
1150 				SOCKBUF_UNLOCK(&so->so_rcv);
1151 				error = (*pr->pr_domain->dom_externalize)
1152 				    (cm, controlp);
1153 				SOCKBUF_LOCK(&so->so_rcv);
1154 			} else if (controlp != NULL)
1155 				*controlp = cm;
1156 			else
1157 				m_freem(cm);
1158 			if (controlp != NULL) {
1159 				orig_resid = 0;
1160 				while (*controlp != NULL)
1161 					controlp = &(*controlp)->m_next;
1162 			}
1163 			cm = cmn;
1164 		}
1165 		if (so->so_rcv.sb_mb)
1166 			nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1167 		else
1168 			nextrecord = NULL;
1169 		orig_resid = 0;
1170 	}
1171 	if (m != NULL) {
1172 		if ((flags & MSG_PEEK) == 0) {
1173 			KASSERT(m->m_nextpkt == nextrecord,
1174 			    ("soreceive: post-control, nextrecord !sync"));
1175 			if (nextrecord == NULL) {
1176 				KASSERT(so->so_rcv.sb_mb == m,
1177 				    ("soreceive: post-control, sb_mb!=m"));
1178 				KASSERT(so->so_rcv.sb_lastrecord == m,
1179 				    ("soreceive: post-control, lastrecord!=m"));
1180 			}
1181 		}
1182 		type = m->m_type;
1183 		if (type == MT_OOBDATA)
1184 			flags |= MSG_OOB;
1185 	} else {
1186 		if ((flags & MSG_PEEK) == 0) {
1187 			KASSERT(so->so_rcv.sb_mb == nextrecord,
1188 			    ("soreceive: sb_mb != nextrecord"));
1189 			if (so->so_rcv.sb_mb == NULL) {
1190 				KASSERT(so->so_rcv.sb_lastrecord == NULL,
1191 				    ("soreceive: sb_lastercord != NULL"));
1192 			}
1193 		}
1194 	}
1195 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1196 	SBLASTRECORDCHK(&so->so_rcv);
1197 	SBLASTMBUFCHK(&so->so_rcv);
1198 
1199 	/*
1200 	 * Now continue to read any data mbufs off of the head of the socket
1201 	 * buffer until the read request is satisfied.  Note that 'type' is
1202 	 * used to store the type of any mbuf reads that have happened so far
1203 	 * such that soreceive() can stop reading if the type changes, which
1204 	 * causes soreceive() to return only one of regular data and inline
1205 	 * out-of-band data in a single socket receive operation.
1206 	 */
1207 	moff = 0;
1208 	offset = 0;
1209 	while (m != NULL && uio->uio_resid > 0 && error == 0) {
1210 		/*
1211 		 * If the type of mbuf has changed since the last mbuf
1212 		 * examined ('type'), end the receive operation.
1213 	 	 */
1214 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1215 		if (m->m_type == MT_OOBDATA) {
1216 			if (type != MT_OOBDATA)
1217 				break;
1218 		} else if (type == MT_OOBDATA)
1219 			break;
1220 		else
1221 		    KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
1222 			("m->m_type == %d", m->m_type));
1223 		so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1224 		len = uio->uio_resid;
1225 		if (so->so_oobmark && len > so->so_oobmark - offset)
1226 			len = so->so_oobmark - offset;
1227 		if (len > m->m_len - moff)
1228 			len = m->m_len - moff;
1229 		/*
1230 		 * If mp is set, just pass back the mbufs.
1231 		 * Otherwise copy them out via the uio, then free.
1232 		 * Sockbuf must be consistent here (points to current mbuf,
1233 		 * it points to next record) when we drop priority;
1234 		 * we must note any additions to the sockbuf when we
1235 		 * block interrupts again.
1236 		 */
1237 		if (mp == NULL) {
1238 			SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1239 			SBLASTRECORDCHK(&so->so_rcv);
1240 			SBLASTMBUFCHK(&so->so_rcv);
1241 			SOCKBUF_UNLOCK(&so->so_rcv);
1242 #ifdef ZERO_COPY_SOCKETS
1243 			if (so_zero_copy_receive) {
1244 				int disposable;
1245 
1246 				if ((m->m_flags & M_EXT)
1247 				 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1248 					disposable = 1;
1249 				else
1250 					disposable = 0;
1251 
1252 				error = uiomoveco(mtod(m, char *) + moff,
1253 						  (int)len, uio,
1254 						  disposable);
1255 			} else
1256 #endif /* ZERO_COPY_SOCKETS */
1257 			error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1258 			SOCKBUF_LOCK(&so->so_rcv);
1259 			if (error)
1260 				goto release;
1261 		} else
1262 			uio->uio_resid -= len;
1263 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1264 		if (len == m->m_len - moff) {
1265 			if (m->m_flags & M_EOR)
1266 				flags |= MSG_EOR;
1267 			if (flags & MSG_PEEK) {
1268 				m = m->m_next;
1269 				moff = 0;
1270 			} else {
1271 				nextrecord = m->m_nextpkt;
1272 				sbfree(&so->so_rcv, m);
1273 				if (mp != NULL) {
1274 					*mp = m;
1275 					mp = &m->m_next;
1276 					so->so_rcv.sb_mb = m = m->m_next;
1277 					*mp = NULL;
1278 				} else {
1279 					so->so_rcv.sb_mb = m_free(m);
1280 					m = so->so_rcv.sb_mb;
1281 				}
1282 				sockbuf_pushsync(&so->so_rcv, nextrecord);
1283 				SBLASTRECORDCHK(&so->so_rcv);
1284 				SBLASTMBUFCHK(&so->so_rcv);
1285 			}
1286 		} else {
1287 			if (flags & MSG_PEEK)
1288 				moff += len;
1289 			else {
1290 				if (mp != NULL) {
1291 					int copy_flag;
1292 
1293 					if (flags & MSG_DONTWAIT)
1294 						copy_flag = M_DONTWAIT;
1295 					else
1296 						copy_flag = M_TRYWAIT;
1297 					if (copy_flag == M_TRYWAIT)
1298 						SOCKBUF_UNLOCK(&so->so_rcv);
1299 					*mp = m_copym(m, 0, len, copy_flag);
1300 					if (copy_flag == M_TRYWAIT)
1301 						SOCKBUF_LOCK(&so->so_rcv);
1302  					if (*mp == NULL) {
1303  						/*
1304  						 * m_copym() couldn't allocate an mbuf.
1305 						 * Adjust uio_resid back (it was adjusted
1306 						 * down by len bytes, which we didn't end
1307 						 * up "copying" over).
1308  						 */
1309  						uio->uio_resid += len;
1310  						break;
1311  					}
1312 				}
1313 				m->m_data += len;
1314 				m->m_len -= len;
1315 				so->so_rcv.sb_cc -= len;
1316 			}
1317 		}
1318 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1319 		if (so->so_oobmark) {
1320 			if ((flags & MSG_PEEK) == 0) {
1321 				so->so_oobmark -= len;
1322 				if (so->so_oobmark == 0) {
1323 					so->so_rcv.sb_state |= SBS_RCVATMARK;
1324 					break;
1325 				}
1326 			} else {
1327 				offset += len;
1328 				if (offset == so->so_oobmark)
1329 					break;
1330 			}
1331 		}
1332 		if (flags & MSG_EOR)
1333 			break;
1334 		/*
1335 		 * If the MSG_WAITALL flag is set (for non-atomic socket),
1336 		 * we must not quit until "uio->uio_resid == 0" or an error
1337 		 * termination.  If a signal/timeout occurs, return
1338 		 * with a short count but without error.
1339 		 * Keep sockbuf locked against other readers.
1340 		 */
1341 		while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1342 		    !sosendallatonce(so) && nextrecord == NULL) {
1343 			SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1344 			if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
1345 				break;
1346 			/*
1347 			 * Notify the protocol that some data has been
1348 			 * drained before blocking.
1349 			 */
1350 			if (pr->pr_flags & PR_WANTRCVD && so->so_pcb != NULL) {
1351 				SOCKBUF_UNLOCK(&so->so_rcv);
1352 				(*pr->pr_usrreqs->pru_rcvd)(so, flags);
1353 				SOCKBUF_LOCK(&so->so_rcv);
1354 			}
1355 			SBLASTRECORDCHK(&so->so_rcv);
1356 			SBLASTMBUFCHK(&so->so_rcv);
1357 			error = sbwait(&so->so_rcv);
1358 			if (error)
1359 				goto release;
1360 			m = so->so_rcv.sb_mb;
1361 			if (m != NULL)
1362 				nextrecord = m->m_nextpkt;
1363 		}
1364 	}
1365 
1366 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1367 	if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1368 		flags |= MSG_TRUNC;
1369 		if ((flags & MSG_PEEK) == 0)
1370 			(void) sbdroprecord_locked(&so->so_rcv);
1371 	}
1372 	if ((flags & MSG_PEEK) == 0) {
1373 		if (m == NULL) {
1374 			/*
1375 			 * First part is an inline SB_EMPTY_FIXUP().  Second
1376 			 * part makes sure sb_lastrecord is up-to-date if
1377 			 * there is still data in the socket buffer.
1378 			 */
1379 			so->so_rcv.sb_mb = nextrecord;
1380 			if (so->so_rcv.sb_mb == NULL) {
1381 				so->so_rcv.sb_mbtail = NULL;
1382 				so->so_rcv.sb_lastrecord = NULL;
1383 			} else if (nextrecord->m_nextpkt == NULL)
1384 				so->so_rcv.sb_lastrecord = nextrecord;
1385 		}
1386 		SBLASTRECORDCHK(&so->so_rcv);
1387 		SBLASTMBUFCHK(&so->so_rcv);
1388 		/*
1389 		 * If soreceive() is being done from the socket callback, then
1390 		 * don't need to generate ACK to peer to update window, since
1391 		 * ACK will be generated on return to TCP.
1392 		 */
1393 		if (!(flags & MSG_SOCALLBCK) &&
1394 		    (pr->pr_flags & PR_WANTRCVD) && so->so_pcb) {
1395 			SOCKBUF_UNLOCK(&so->so_rcv);
1396 			(*pr->pr_usrreqs->pru_rcvd)(so, flags);
1397 			SOCKBUF_LOCK(&so->so_rcv);
1398 		}
1399 	}
1400 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1401 	if (orig_resid == uio->uio_resid && orig_resid &&
1402 	    (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1403 		sbunlock(&so->so_rcv);
1404 		goto restart;
1405 	}
1406 
1407 	if (flagsp != NULL)
1408 		*flagsp |= flags;
1409 release:
1410 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1411 	sbunlock(&so->so_rcv);
1412 out:
1413 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1414 	SOCKBUF_UNLOCK(&so->so_rcv);
1415 	return (error);
1416 }
1417 
1418 int
1419 soshutdown(so, how)
1420 	struct socket *so;
1421 	int how;
1422 {
1423 	struct protosw *pr = so->so_proto;
1424 
1425 	if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1426 		return (EINVAL);
1427 
1428 	if (how != SHUT_WR)
1429 		sorflush(so);
1430 	if (how != SHUT_RD)
1431 		return ((*pr->pr_usrreqs->pru_shutdown)(so));
1432 	return (0);
1433 }
1434 
1435 void
1436 sorflush(so)
1437 	struct socket *so;
1438 {
1439 	struct sockbuf *sb = &so->so_rcv;
1440 	struct protosw *pr = so->so_proto;
1441 	struct sockbuf asb;
1442 
1443 	/*
1444 	 * XXXRW: This is quite ugly.  Previously, this code made a copy of
1445 	 * the socket buffer, then zero'd the original to clear the buffer
1446 	 * fields.  However, with mutexes in the socket buffer, this causes
1447 	 * problems.  We only clear the zeroable bits of the original;
1448 	 * however, we have to initialize and destroy the mutex in the copy
1449 	 * so that dom_dispose() and sbrelease() can lock t as needed.
1450 	 */
1451 	SOCKBUF_LOCK(sb);
1452 	sb->sb_flags |= SB_NOINTR;
1453 	(void) sblock(sb, M_WAITOK);
1454 	/*
1455 	 * socantrcvmore_locked() drops the socket buffer mutex so that it
1456 	 * can safely perform wakeups.  Re-acquire the mutex before
1457 	 * continuing.
1458 	 */
1459 	socantrcvmore_locked(so);
1460 	SOCKBUF_LOCK(sb);
1461 	sbunlock(sb);
1462 	/*
1463 	 * Invalidate/clear most of the sockbuf structure, but leave
1464 	 * selinfo and mutex data unchanged.
1465 	 */
1466 	bzero(&asb, offsetof(struct sockbuf, sb_startzero));
1467 	bcopy(&sb->sb_startzero, &asb.sb_startzero,
1468 	    sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
1469 	bzero(&sb->sb_startzero,
1470 	    sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
1471 	SOCKBUF_UNLOCK(sb);
1472 
1473 	SOCKBUF_LOCK_INIT(&asb, "so_rcv");
1474 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
1475 		(*pr->pr_domain->dom_dispose)(asb.sb_mb);
1476 	sbrelease(&asb, so);
1477 	SOCKBUF_LOCK_DESTROY(&asb);
1478 }
1479 
1480 /*
1481  * Perhaps this routine, and sooptcopyout(), below, ought to come in
1482  * an additional variant to handle the case where the option value needs
1483  * to be some kind of integer, but not a specific size.
1484  * In addition to their use here, these functions are also called by the
1485  * protocol-level pr_ctloutput() routines.
1486  */
1487 int
1488 sooptcopyin(sopt, buf, len, minlen)
1489 	struct	sockopt *sopt;
1490 	void	*buf;
1491 	size_t	len;
1492 	size_t	minlen;
1493 {
1494 	size_t	valsize;
1495 
1496 	/*
1497 	 * If the user gives us more than we wanted, we ignore it,
1498 	 * but if we don't get the minimum length the caller
1499 	 * wants, we return EINVAL.  On success, sopt->sopt_valsize
1500 	 * is set to however much we actually retrieved.
1501 	 */
1502 	if ((valsize = sopt->sopt_valsize) < minlen)
1503 		return EINVAL;
1504 	if (valsize > len)
1505 		sopt->sopt_valsize = valsize = len;
1506 
1507 	if (sopt->sopt_td != NULL)
1508 		return (copyin(sopt->sopt_val, buf, valsize));
1509 
1510 	bcopy(sopt->sopt_val, buf, valsize);
1511 	return 0;
1512 }
1513 
1514 /*
1515  * Kernel version of setsockopt(2)/
1516  * XXX: optlen is size_t, not socklen_t
1517  */
1518 int
1519 so_setsockopt(struct socket *so, int level, int optname, void *optval,
1520     size_t optlen)
1521 {
1522 	struct sockopt sopt;
1523 
1524 	sopt.sopt_level = level;
1525 	sopt.sopt_name = optname;
1526 	sopt.sopt_dir = SOPT_SET;
1527 	sopt.sopt_val = optval;
1528 	sopt.sopt_valsize = optlen;
1529 	sopt.sopt_td = NULL;
1530 	return (sosetopt(so, &sopt));
1531 }
1532 
1533 int
1534 sosetopt(so, sopt)
1535 	struct socket *so;
1536 	struct sockopt *sopt;
1537 {
1538 	int	error, optval;
1539 	struct	linger l;
1540 	struct	timeval tv;
1541 	u_long  val;
1542 #ifdef MAC
1543 	struct mac extmac;
1544 #endif
1545 
1546 	error = 0;
1547 	if (sopt->sopt_level != SOL_SOCKET) {
1548 		if (so->so_proto && so->so_proto->pr_ctloutput)
1549 			return ((*so->so_proto->pr_ctloutput)
1550 				  (so, sopt));
1551 		error = ENOPROTOOPT;
1552 	} else {
1553 		switch (sopt->sopt_name) {
1554 #ifdef INET
1555 		case SO_ACCEPTFILTER:
1556 			error = do_setopt_accept_filter(so, sopt);
1557 			if (error)
1558 				goto bad;
1559 			break;
1560 #endif
1561 		case SO_LINGER:
1562 			error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1563 			if (error)
1564 				goto bad;
1565 
1566 			SOCK_LOCK(so);
1567 			so->so_linger = l.l_linger;
1568 			if (l.l_onoff)
1569 				so->so_options |= SO_LINGER;
1570 			else
1571 				so->so_options &= ~SO_LINGER;
1572 			SOCK_UNLOCK(so);
1573 			break;
1574 
1575 		case SO_DEBUG:
1576 		case SO_KEEPALIVE:
1577 		case SO_DONTROUTE:
1578 		case SO_USELOOPBACK:
1579 		case SO_BROADCAST:
1580 		case SO_REUSEADDR:
1581 		case SO_REUSEPORT:
1582 		case SO_OOBINLINE:
1583 		case SO_TIMESTAMP:
1584 		case SO_BINTIME:
1585 		case SO_NOSIGPIPE:
1586 			error = sooptcopyin(sopt, &optval, sizeof optval,
1587 					    sizeof optval);
1588 			if (error)
1589 				goto bad;
1590 			SOCK_LOCK(so);
1591 			if (optval)
1592 				so->so_options |= sopt->sopt_name;
1593 			else
1594 				so->so_options &= ~sopt->sopt_name;
1595 			SOCK_UNLOCK(so);
1596 			break;
1597 
1598 		case SO_SNDBUF:
1599 		case SO_RCVBUF:
1600 		case SO_SNDLOWAT:
1601 		case SO_RCVLOWAT:
1602 			error = sooptcopyin(sopt, &optval, sizeof optval,
1603 					    sizeof optval);
1604 			if (error)
1605 				goto bad;
1606 
1607 			/*
1608 			 * Values < 1 make no sense for any of these
1609 			 * options, so disallow them.
1610 			 */
1611 			if (optval < 1) {
1612 				error = EINVAL;
1613 				goto bad;
1614 			}
1615 
1616 			switch (sopt->sopt_name) {
1617 			case SO_SNDBUF:
1618 			case SO_RCVBUF:
1619 				if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1620 				    &so->so_snd : &so->so_rcv, (u_long)optval,
1621 				    so, curthread) == 0) {
1622 					error = ENOBUFS;
1623 					goto bad;
1624 				}
1625 				break;
1626 
1627 			/*
1628 			 * Make sure the low-water is never greater than
1629 			 * the high-water.
1630 			 */
1631 			case SO_SNDLOWAT:
1632 				SOCKBUF_LOCK(&so->so_snd);
1633 				so->so_snd.sb_lowat =
1634 				    (optval > so->so_snd.sb_hiwat) ?
1635 				    so->so_snd.sb_hiwat : optval;
1636 				SOCKBUF_UNLOCK(&so->so_snd);
1637 				break;
1638 			case SO_RCVLOWAT:
1639 				SOCKBUF_LOCK(&so->so_rcv);
1640 				so->so_rcv.sb_lowat =
1641 				    (optval > so->so_rcv.sb_hiwat) ?
1642 				    so->so_rcv.sb_hiwat : optval;
1643 				SOCKBUF_UNLOCK(&so->so_rcv);
1644 				break;
1645 			}
1646 			break;
1647 
1648 		case SO_SNDTIMEO:
1649 		case SO_RCVTIMEO:
1650 			error = sooptcopyin(sopt, &tv, sizeof tv,
1651 					    sizeof tv);
1652 			if (error)
1653 				goto bad;
1654 
1655 			/* assert(hz > 0); */
1656 			if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
1657 			    tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1658 				error = EDOM;
1659 				goto bad;
1660 			}
1661 			/* assert(tick > 0); */
1662 			/* assert(ULONG_MAX - INT_MAX >= 1000000); */
1663 			val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1664 			if (val > INT_MAX) {
1665 				error = EDOM;
1666 				goto bad;
1667 			}
1668 			if (val == 0 && tv.tv_usec != 0)
1669 				val = 1;
1670 
1671 			switch (sopt->sopt_name) {
1672 			case SO_SNDTIMEO:
1673 				so->so_snd.sb_timeo = val;
1674 				break;
1675 			case SO_RCVTIMEO:
1676 				so->so_rcv.sb_timeo = val;
1677 				break;
1678 			}
1679 			break;
1680 
1681 		case SO_LABEL:
1682 #ifdef MAC
1683 			error = sooptcopyin(sopt, &extmac, sizeof extmac,
1684 			    sizeof extmac);
1685 			if (error)
1686 				goto bad;
1687 			error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
1688 			    so, &extmac);
1689 #else
1690 			error = EOPNOTSUPP;
1691 #endif
1692 			break;
1693 
1694 		default:
1695 			error = ENOPROTOOPT;
1696 			break;
1697 		}
1698 		if (error == 0 && so->so_proto != NULL &&
1699 		    so->so_proto->pr_ctloutput != NULL) {
1700 			(void) ((*so->so_proto->pr_ctloutput)
1701 				  (so, sopt));
1702 		}
1703 	}
1704 bad:
1705 	return (error);
1706 }
1707 
1708 /* Helper routine for getsockopt */
1709 int
1710 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
1711 {
1712 	int	error;
1713 	size_t	valsize;
1714 
1715 	error = 0;
1716 
1717 	/*
1718 	 * Documented get behavior is that we always return a value,
1719 	 * possibly truncated to fit in the user's buffer.
1720 	 * Traditional behavior is that we always tell the user
1721 	 * precisely how much we copied, rather than something useful
1722 	 * like the total amount we had available for her.
1723 	 * Note that this interface is not idempotent; the entire answer must
1724 	 * generated ahead of time.
1725 	 */
1726 	valsize = min(len, sopt->sopt_valsize);
1727 	sopt->sopt_valsize = valsize;
1728 	if (sopt->sopt_val != NULL) {
1729 		if (sopt->sopt_td != NULL)
1730 			error = copyout(buf, sopt->sopt_val, valsize);
1731 		else
1732 			bcopy(buf, sopt->sopt_val, valsize);
1733 	}
1734 	return error;
1735 }
1736 
1737 int
1738 sogetopt(so, sopt)
1739 	struct socket *so;
1740 	struct sockopt *sopt;
1741 {
1742 	int	error, optval;
1743 	struct	linger l;
1744 	struct	timeval tv;
1745 #ifdef MAC
1746 	struct mac extmac;
1747 #endif
1748 
1749 	error = 0;
1750 	if (sopt->sopt_level != SOL_SOCKET) {
1751 		if (so->so_proto && so->so_proto->pr_ctloutput) {
1752 			return ((*so->so_proto->pr_ctloutput)
1753 				  (so, sopt));
1754 		} else
1755 			return (ENOPROTOOPT);
1756 	} else {
1757 		switch (sopt->sopt_name) {
1758 #ifdef INET
1759 		case SO_ACCEPTFILTER:
1760 			error = do_getopt_accept_filter(so, sopt);
1761 			break;
1762 #endif
1763 		case SO_LINGER:
1764 			SOCK_LOCK(so);
1765 			l.l_onoff = so->so_options & SO_LINGER;
1766 			l.l_linger = so->so_linger;
1767 			SOCK_UNLOCK(so);
1768 			error = sooptcopyout(sopt, &l, sizeof l);
1769 			break;
1770 
1771 		case SO_USELOOPBACK:
1772 		case SO_DONTROUTE:
1773 		case SO_DEBUG:
1774 		case SO_KEEPALIVE:
1775 		case SO_REUSEADDR:
1776 		case SO_REUSEPORT:
1777 		case SO_BROADCAST:
1778 		case SO_OOBINLINE:
1779 		case SO_ACCEPTCONN:
1780 		case SO_TIMESTAMP:
1781 		case SO_BINTIME:
1782 		case SO_NOSIGPIPE:
1783 			optval = so->so_options & sopt->sopt_name;
1784 integer:
1785 			error = sooptcopyout(sopt, &optval, sizeof optval);
1786 			break;
1787 
1788 		case SO_TYPE:
1789 			optval = so->so_type;
1790 			goto integer;
1791 
1792 		case SO_ERROR:
1793 			optval = so->so_error;
1794 			so->so_error = 0;
1795 			goto integer;
1796 
1797 		case SO_SNDBUF:
1798 			optval = so->so_snd.sb_hiwat;
1799 			goto integer;
1800 
1801 		case SO_RCVBUF:
1802 			optval = so->so_rcv.sb_hiwat;
1803 			goto integer;
1804 
1805 		case SO_SNDLOWAT:
1806 			optval = so->so_snd.sb_lowat;
1807 			goto integer;
1808 
1809 		case SO_RCVLOWAT:
1810 			optval = so->so_rcv.sb_lowat;
1811 			goto integer;
1812 
1813 		case SO_SNDTIMEO:
1814 		case SO_RCVTIMEO:
1815 			optval = (sopt->sopt_name == SO_SNDTIMEO ?
1816 				  so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1817 
1818 			tv.tv_sec = optval / hz;
1819 			tv.tv_usec = (optval % hz) * tick;
1820 			error = sooptcopyout(sopt, &tv, sizeof tv);
1821 			break;
1822 
1823 		case SO_LABEL:
1824 #ifdef MAC
1825 			error = sooptcopyin(sopt, &extmac, sizeof(extmac),
1826 			    sizeof(extmac));
1827 			if (error)
1828 				return (error);
1829 			error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
1830 			    so, &extmac);
1831 			if (error)
1832 				return (error);
1833 			error = sooptcopyout(sopt, &extmac, sizeof extmac);
1834 #else
1835 			error = EOPNOTSUPP;
1836 #endif
1837 			break;
1838 
1839 		case SO_PEERLABEL:
1840 #ifdef MAC
1841 			error = sooptcopyin(sopt, &extmac, sizeof(extmac),
1842 			    sizeof(extmac));
1843 			if (error)
1844 				return (error);
1845 			error = mac_getsockopt_peerlabel(
1846 			    sopt->sopt_td->td_ucred, so, &extmac);
1847 			if (error)
1848 				return (error);
1849 			error = sooptcopyout(sopt, &extmac, sizeof extmac);
1850 #else
1851 			error = EOPNOTSUPP;
1852 #endif
1853 			break;
1854 
1855 		case SO_LISTENQLIMIT:
1856 			optval = so->so_qlimit;
1857 			goto integer;
1858 
1859 		case SO_LISTENQLEN:
1860 			optval = so->so_qlen;
1861 			goto integer;
1862 
1863 		case SO_LISTENINCQLEN:
1864 			optval = so->so_incqlen;
1865 			goto integer;
1866 
1867 		default:
1868 			error = ENOPROTOOPT;
1869 			break;
1870 		}
1871 		return (error);
1872 	}
1873 }
1874 
1875 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1876 int
1877 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1878 {
1879 	struct mbuf *m, *m_prev;
1880 	int sopt_size = sopt->sopt_valsize;
1881 
1882 	MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
1883 	if (m == NULL)
1884 		return ENOBUFS;
1885 	if (sopt_size > MLEN) {
1886 		MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT);
1887 		if ((m->m_flags & M_EXT) == 0) {
1888 			m_free(m);
1889 			return ENOBUFS;
1890 		}
1891 		m->m_len = min(MCLBYTES, sopt_size);
1892 	} else {
1893 		m->m_len = min(MLEN, sopt_size);
1894 	}
1895 	sopt_size -= m->m_len;
1896 	*mp = m;
1897 	m_prev = m;
1898 
1899 	while (sopt_size) {
1900 		MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
1901 		if (m == NULL) {
1902 			m_freem(*mp);
1903 			return ENOBUFS;
1904 		}
1905 		if (sopt_size > MLEN) {
1906 			MCLGET(m, sopt->sopt_td != NULL ? M_TRYWAIT :
1907 			    M_DONTWAIT);
1908 			if ((m->m_flags & M_EXT) == 0) {
1909 				m_freem(m);
1910 				m_freem(*mp);
1911 				return ENOBUFS;
1912 			}
1913 			m->m_len = min(MCLBYTES, sopt_size);
1914 		} else {
1915 			m->m_len = min(MLEN, sopt_size);
1916 		}
1917 		sopt_size -= m->m_len;
1918 		m_prev->m_next = m;
1919 		m_prev = m;
1920 	}
1921 	return 0;
1922 }
1923 
1924 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1925 int
1926 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1927 {
1928 	struct mbuf *m0 = m;
1929 
1930 	if (sopt->sopt_val == NULL)
1931 		return 0;
1932 	while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1933 		if (sopt->sopt_td != NULL) {
1934 			int error;
1935 
1936 			error = copyin(sopt->sopt_val, mtod(m, char *),
1937 				       m->m_len);
1938 			if (error != 0) {
1939 				m_freem(m0);
1940 				return(error);
1941 			}
1942 		} else
1943 			bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1944 		sopt->sopt_valsize -= m->m_len;
1945 		sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
1946 		m = m->m_next;
1947 	}
1948 	if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1949 		panic("ip6_sooptmcopyin");
1950 	return 0;
1951 }
1952 
1953 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1954 int
1955 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1956 {
1957 	struct mbuf *m0 = m;
1958 	size_t valsize = 0;
1959 
1960 	if (sopt->sopt_val == NULL)
1961 		return 0;
1962 	while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1963 		if (sopt->sopt_td != NULL) {
1964 			int error;
1965 
1966 			error = copyout(mtod(m, char *), sopt->sopt_val,
1967 				       m->m_len);
1968 			if (error != 0) {
1969 				m_freem(m0);
1970 				return(error);
1971 			}
1972 		} else
1973 			bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1974 	       sopt->sopt_valsize -= m->m_len;
1975 	       sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
1976 	       valsize += m->m_len;
1977 	       m = m->m_next;
1978 	}
1979 	if (m != NULL) {
1980 		/* enough soopt buffer should be given from user-land */
1981 		m_freem(m0);
1982 		return(EINVAL);
1983 	}
1984 	sopt->sopt_valsize = valsize;
1985 	return 0;
1986 }
1987 
1988 void
1989 sohasoutofband(so)
1990 	struct socket *so;
1991 {
1992 	if (so->so_sigio != NULL)
1993 		pgsigio(&so->so_sigio, SIGURG, 0);
1994 	selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
1995 }
1996 
1997 int
1998 sopoll(struct socket *so, int events, struct ucred *active_cred,
1999     struct thread *td)
2000 {
2001 	int revents = 0;
2002 
2003 	SOCKBUF_LOCK(&so->so_snd);
2004 	SOCKBUF_LOCK(&so->so_rcv);
2005 	if (events & (POLLIN | POLLRDNORM))
2006 		if (soreadable(so))
2007 			revents |= events & (POLLIN | POLLRDNORM);
2008 
2009 	if (events & POLLINIGNEOF)
2010 		if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
2011 		    !TAILQ_EMPTY(&so->so_comp) || so->so_error)
2012 			revents |= POLLINIGNEOF;
2013 
2014 	if (events & (POLLOUT | POLLWRNORM))
2015 		if (sowriteable(so))
2016 			revents |= events & (POLLOUT | POLLWRNORM);
2017 
2018 	if (events & (POLLPRI | POLLRDBAND))
2019 		if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
2020 			revents |= events & (POLLPRI | POLLRDBAND);
2021 
2022 	if (revents == 0) {
2023 		if (events &
2024 		    (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
2025 		     POLLRDBAND)) {
2026 			selrecord(td, &so->so_rcv.sb_sel);
2027 			so->so_rcv.sb_flags |= SB_SEL;
2028 		}
2029 
2030 		if (events & (POLLOUT | POLLWRNORM)) {
2031 			selrecord(td, &so->so_snd.sb_sel);
2032 			so->so_snd.sb_flags |= SB_SEL;
2033 		}
2034 	}
2035 
2036 	SOCKBUF_UNLOCK(&so->so_rcv);
2037 	SOCKBUF_UNLOCK(&so->so_snd);
2038 	return (revents);
2039 }
2040 
2041 int
2042 soo_kqfilter(struct file *fp, struct knote *kn)
2043 {
2044 	struct socket *so = kn->kn_fp->f_data;
2045 	struct sockbuf *sb;
2046 
2047 	switch (kn->kn_filter) {
2048 	case EVFILT_READ:
2049 		if (so->so_options & SO_ACCEPTCONN)
2050 			kn->kn_fop = &solisten_filtops;
2051 		else
2052 			kn->kn_fop = &soread_filtops;
2053 		sb = &so->so_rcv;
2054 		break;
2055 	case EVFILT_WRITE:
2056 		kn->kn_fop = &sowrite_filtops;
2057 		sb = &so->so_snd;
2058 		break;
2059 	default:
2060 		return (EINVAL);
2061 	}
2062 
2063 	SOCKBUF_LOCK(sb);
2064 	knlist_add(&sb->sb_sel.si_note, kn, 1);
2065 	sb->sb_flags |= SB_KNOTE;
2066 	SOCKBUF_UNLOCK(sb);
2067 	return (0);
2068 }
2069 
2070 static void
2071 filt_sordetach(struct knote *kn)
2072 {
2073 	struct socket *so = kn->kn_fp->f_data;
2074 
2075 	SOCKBUF_LOCK(&so->so_rcv);
2076 	knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
2077 	if (knlist_empty(&so->so_rcv.sb_sel.si_note))
2078 		so->so_rcv.sb_flags &= ~SB_KNOTE;
2079 	SOCKBUF_UNLOCK(&so->so_rcv);
2080 }
2081 
2082 /*ARGSUSED*/
2083 static int
2084 filt_soread(struct knote *kn, long hint)
2085 {
2086 	struct socket *so;
2087 
2088 	so = kn->kn_fp->f_data;
2089 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2090 
2091 	kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
2092 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2093 		kn->kn_flags |= EV_EOF;
2094 		kn->kn_fflags = so->so_error;
2095 		return (1);
2096 	} else if (so->so_error)	/* temporary udp error */
2097 		return (1);
2098 	else if (kn->kn_sfflags & NOTE_LOWAT)
2099 		return (kn->kn_data >= kn->kn_sdata);
2100 	else
2101 		return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
2102 }
2103 
2104 static void
2105 filt_sowdetach(struct knote *kn)
2106 {
2107 	struct socket *so = kn->kn_fp->f_data;
2108 
2109 	SOCKBUF_LOCK(&so->so_snd);
2110 	knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
2111 	if (knlist_empty(&so->so_snd.sb_sel.si_note))
2112 		so->so_snd.sb_flags &= ~SB_KNOTE;
2113 	SOCKBUF_UNLOCK(&so->so_snd);
2114 }
2115 
2116 /*ARGSUSED*/
2117 static int
2118 filt_sowrite(struct knote *kn, long hint)
2119 {
2120 	struct socket *so;
2121 
2122 	so = kn->kn_fp->f_data;
2123 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
2124 	kn->kn_data = sbspace(&so->so_snd);
2125 	if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2126 		kn->kn_flags |= EV_EOF;
2127 		kn->kn_fflags = so->so_error;
2128 		return (1);
2129 	} else if (so->so_error)	/* temporary udp error */
2130 		return (1);
2131 	else if (((so->so_state & SS_ISCONNECTED) == 0) &&
2132 	    (so->so_proto->pr_flags & PR_CONNREQUIRED))
2133 		return (0);
2134 	else if (kn->kn_sfflags & NOTE_LOWAT)
2135 		return (kn->kn_data >= kn->kn_sdata);
2136 	else
2137 		return (kn->kn_data >= so->so_snd.sb_lowat);
2138 }
2139 
2140 /*ARGSUSED*/
2141 static int
2142 filt_solisten(struct knote *kn, long hint)
2143 {
2144 	struct socket *so = kn->kn_fp->f_data;
2145 
2146 	kn->kn_data = so->so_qlen;
2147 	return (! TAILQ_EMPTY(&so->so_comp));
2148 }
2149 
2150 int
2151 socheckuid(struct socket *so, uid_t uid)
2152 {
2153 
2154 	if (so == NULL)
2155 		return (EPERM);
2156 	if (so->so_cred->cr_uid != uid)
2157 		return (EPERM);
2158 	return (0);
2159 }
2160 
2161 static int
2162 somaxconn_sysctl(SYSCTL_HANDLER_ARGS)
2163 {
2164 	int error;
2165 	int val;
2166 
2167 	val = somaxconn;
2168 	error = sysctl_handle_int(oidp, &val, sizeof(int), req);
2169 	if (error || !req->newptr )
2170 		return (error);
2171 
2172 	if (val < 1 || val > USHRT_MAX)
2173 		return (EINVAL);
2174 
2175 	somaxconn = val;
2176 	return (0);
2177 }
2178