xref: /freebsd/sys/kern/uipc_socket.c (revision 41466b50c1d5bfd1cf6adaae547a579a75d7c04e)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)uipc_socket.c	8.3 (Berkeley) 4/15/94
34  * $FreeBSD$
35  */
36 
37 #include "opt_inet.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/fcntl.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/mutex.h>
46 #include <sys/domain.h>
47 #include <sys/file.h>			/* for struct knote */
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/event.h>
51 #include <sys/poll.h>
52 #include <sys/proc.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/resourcevar.h>
57 #include <sys/signalvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/uio.h>
60 #include <sys/jail.h>
61 
62 #include <vm/vm_zone.h>
63 
64 #include <machine/limits.h>
65 
66 #ifdef INET
67 static int	 do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
68 #endif
69 
70 static void 	filt_sordetach(struct knote *kn);
71 static int 	filt_soread(struct knote *kn, long hint);
72 static void 	filt_sowdetach(struct knote *kn);
73 static int	filt_sowrite(struct knote *kn, long hint);
74 static int	filt_solisten(struct knote *kn, long hint);
75 
76 static struct filterops solisten_filtops =
77 	{ 1, NULL, filt_sordetach, filt_solisten };
78 static struct filterops soread_filtops =
79 	{ 1, NULL, filt_sordetach, filt_soread };
80 static struct filterops sowrite_filtops =
81 	{ 1, NULL, filt_sowdetach, filt_sowrite };
82 
83 struct	vm_zone *socket_zone;
84 so_gen_t	so_gencnt;	/* generation count for sockets */
85 
86 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
87 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
88 
89 SYSCTL_DECL(_kern_ipc);
90 
91 static int somaxconn = SOMAXCONN;
92 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
93     &somaxconn, 0, "Maximum pending socket connection queue size");
94 
95 /*
96  * Socket operation routines.
97  * These routines are called by the routines in
98  * sys_socket.c or from a system process, and
99  * implement the semantics of socket operations by
100  * switching out to the protocol specific routines.
101  */
102 
103 /*
104  * Get a socket structure from our zone, and initialize it.
105  * We don't implement `waitok' yet (see comments in uipc_domain.c).
106  * Note that it would probably be better to allocate socket
107  * and PCB at the same time, but I'm not convinced that all
108  * the protocols can be easily modified to do this.
109  */
110 struct socket *
111 soalloc(waitok)
112 	int waitok;
113 {
114 	struct socket *so;
115 
116 	so = zalloc(socket_zone);
117 	if (so) {
118 		/* XXX race condition for reentrant kernel */
119 		bzero(so, sizeof *so);
120 		so->so_gencnt = ++so_gencnt;
121 		so->so_zone = socket_zone;
122 		TAILQ_INIT(&so->so_aiojobq);
123 	}
124 	return so;
125 }
126 
127 int
128 socreate(dom, aso, type, proto, td)
129 	int dom;
130 	struct socket **aso;
131 	register int type;
132 	int proto;
133 	struct thread *td;
134 {
135 	register struct protosw *prp;
136 	register struct socket *so;
137 	register int error;
138 
139 	if (proto)
140 		prp = pffindproto(dom, proto, type);
141 	else
142 		prp = pffindtype(dom, type);
143 
144 	if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
145 		return (EPROTONOSUPPORT);
146 
147 	if (jailed(td->td_proc->p_ucred) && jail_socket_unixiproute_only &&
148 	    prp->pr_domain->dom_family != PF_LOCAL &&
149 	    prp->pr_domain->dom_family != PF_INET &&
150 	    prp->pr_domain->dom_family != PF_ROUTE) {
151 		return (EPROTONOSUPPORT);
152 	}
153 
154 	if (prp->pr_type != type)
155 		return (EPROTOTYPE);
156 	so = soalloc(td != 0);
157 	if (so == 0)
158 		return (ENOBUFS);
159 
160 	TAILQ_INIT(&so->so_incomp);
161 	TAILQ_INIT(&so->so_comp);
162 	so->so_type = type;
163 	so->so_cred = crhold(td->td_proc->p_ucred);
164 	so->so_proto = prp;
165 	error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
166 	if (error) {
167 		so->so_state |= SS_NOFDREF;
168 		sofree(so);
169 		return (error);
170 	}
171 	*aso = so;
172 	return (0);
173 }
174 
175 int
176 sobind(so, nam, td)
177 	struct socket *so;
178 	struct sockaddr *nam;
179 	struct thread *td;
180 {
181 	int s = splnet();
182 	int error;
183 
184 	error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
185 	splx(s);
186 	return (error);
187 }
188 
189 void
190 sodealloc(so)
191 	struct socket *so;
192 {
193 
194 	so->so_gencnt = ++so_gencnt;
195 	if (so->so_rcv.sb_hiwat)
196 		(void)chgsbsize(so->so_cred->cr_uidinfo,
197 		    &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
198 	if (so->so_snd.sb_hiwat)
199 		(void)chgsbsize(so->so_cred->cr_uidinfo,
200 		    &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
201 #ifdef INET
202 	if (so->so_accf != NULL) {
203 		if (so->so_accf->so_accept_filter != NULL &&
204 			so->so_accf->so_accept_filter->accf_destroy != NULL) {
205 			so->so_accf->so_accept_filter->accf_destroy(so);
206 		}
207 		if (so->so_accf->so_accept_filter_str != NULL)
208 			FREE(so->so_accf->so_accept_filter_str, M_ACCF);
209 		FREE(so->so_accf, M_ACCF);
210 	}
211 #endif
212 	crfree(so->so_cred);
213 	zfree(so->so_zone, so);
214 }
215 
216 int
217 solisten(so, backlog, td)
218 	register struct socket *so;
219 	int backlog;
220 	struct thread *td;
221 {
222 	int s, error;
223 
224 	s = splnet();
225 	error = (*so->so_proto->pr_usrreqs->pru_listen)(so, td);
226 	if (error) {
227 		splx(s);
228 		return (error);
229 	}
230 	if (TAILQ_EMPTY(&so->so_comp))
231 		so->so_options |= SO_ACCEPTCONN;
232 	if (backlog < 0 || backlog > somaxconn)
233 		backlog = somaxconn;
234 	so->so_qlimit = backlog;
235 	splx(s);
236 	return (0);
237 }
238 
239 void
240 sofree(so)
241 	register struct socket *so;
242 {
243 	struct socket *head = so->so_head;
244 
245 	if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
246 		return;
247 	if (head != NULL) {
248 		if (so->so_state & SS_INCOMP) {
249 			TAILQ_REMOVE(&head->so_incomp, so, so_list);
250 			head->so_incqlen--;
251 		} else if (so->so_state & SS_COMP) {
252 			/*
253 			 * We must not decommission a socket that's
254 			 * on the accept(2) queue.  If we do, then
255 			 * accept(2) may hang after select(2) indicated
256 			 * that the listening socket was ready.
257 			 */
258 			return;
259 		} else {
260 			panic("sofree: not queued");
261 		}
262 		head->so_qlen--;
263 		so->so_state &= ~SS_INCOMP;
264 		so->so_head = NULL;
265 	}
266 	sbrelease(&so->so_snd, so);
267 	sorflush(so);
268 	sodealloc(so);
269 }
270 
271 /*
272  * Close a socket on last file table reference removal.
273  * Initiate disconnect if connected.
274  * Free socket when disconnect complete.
275  */
276 int
277 soclose(so)
278 	register struct socket *so;
279 {
280 	int s = splnet();		/* conservative */
281 	int error = 0;
282 
283 	funsetown(so->so_sigio);
284 	if (so->so_options & SO_ACCEPTCONN) {
285 		struct socket *sp, *sonext;
286 
287 		sp = TAILQ_FIRST(&so->so_incomp);
288 		for (; sp != NULL; sp = sonext) {
289 			sonext = TAILQ_NEXT(sp, so_list);
290 			(void) soabort(sp);
291 		}
292 		for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
293 			sonext = TAILQ_NEXT(sp, so_list);
294 			/* Dequeue from so_comp since sofree() won't do it */
295 			TAILQ_REMOVE(&so->so_comp, sp, so_list);
296 			so->so_qlen--;
297 			sp->so_state &= ~SS_COMP;
298 			sp->so_head = NULL;
299 			(void) soabort(sp);
300 		}
301 	}
302 	if (so->so_pcb == 0)
303 		goto discard;
304 	if (so->so_state & SS_ISCONNECTED) {
305 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
306 			error = sodisconnect(so);
307 			if (error)
308 				goto drop;
309 		}
310 		if (so->so_options & SO_LINGER) {
311 			if ((so->so_state & SS_ISDISCONNECTING) &&
312 			    (so->so_state & SS_NBIO))
313 				goto drop;
314 			while (so->so_state & SS_ISCONNECTED) {
315 				error = tsleep((caddr_t)&so->so_timeo,
316 				    PSOCK | PCATCH, "soclos", so->so_linger * hz);
317 				if (error)
318 					break;
319 			}
320 		}
321 	}
322 drop:
323 	if (so->so_pcb) {
324 		int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
325 		if (error == 0)
326 			error = error2;
327 	}
328 discard:
329 	if (so->so_state & SS_NOFDREF)
330 		panic("soclose: NOFDREF");
331 	so->so_state |= SS_NOFDREF;
332 	sofree(so);
333 	splx(s);
334 	return (error);
335 }
336 
337 /*
338  * Must be called at splnet...
339  */
340 int
341 soabort(so)
342 	struct socket *so;
343 {
344 	int error;
345 
346 	error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
347 	if (error) {
348 		sofree(so);
349 		return error;
350 	}
351 	return (0);
352 }
353 
354 int
355 soaccept(so, nam)
356 	register struct socket *so;
357 	struct sockaddr **nam;
358 {
359 	int s = splnet();
360 	int error;
361 
362 	if ((so->so_state & SS_NOFDREF) == 0)
363 		panic("soaccept: !NOFDREF");
364 	so->so_state &= ~SS_NOFDREF;
365 	error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
366 	splx(s);
367 	return (error);
368 }
369 
370 int
371 soconnect(so, nam, td)
372 	register struct socket *so;
373 	struct sockaddr *nam;
374 	struct thread *td;
375 {
376 	int s;
377 	int error;
378 
379 	if (so->so_options & SO_ACCEPTCONN)
380 		return (EOPNOTSUPP);
381 	s = splnet();
382 	/*
383 	 * If protocol is connection-based, can only connect once.
384 	 * Otherwise, if connected, try to disconnect first.
385 	 * This allows user to disconnect by connecting to, e.g.,
386 	 * a null address.
387 	 */
388 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
389 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
390 	    (error = sodisconnect(so))))
391 		error = EISCONN;
392 	else
393 		error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
394 	splx(s);
395 	return (error);
396 }
397 
398 int
399 soconnect2(so1, so2)
400 	register struct socket *so1;
401 	struct socket *so2;
402 {
403 	int s = splnet();
404 	int error;
405 
406 	error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
407 	splx(s);
408 	return (error);
409 }
410 
411 int
412 sodisconnect(so)
413 	register struct socket *so;
414 {
415 	int s = splnet();
416 	int error;
417 
418 	if ((so->so_state & SS_ISCONNECTED) == 0) {
419 		error = ENOTCONN;
420 		goto bad;
421 	}
422 	if (so->so_state & SS_ISDISCONNECTING) {
423 		error = EALREADY;
424 		goto bad;
425 	}
426 	error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
427 bad:
428 	splx(s);
429 	return (error);
430 }
431 
432 #define	SBLOCKWAIT(f)	(((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
433 /*
434  * Send on a socket.
435  * If send must go all at once and message is larger than
436  * send buffering, then hard error.
437  * Lock against other senders.
438  * If must go all at once and not enough room now, then
439  * inform user that this would block and do nothing.
440  * Otherwise, if nonblocking, send as much as possible.
441  * The data to be sent is described by "uio" if nonzero,
442  * otherwise by the mbuf chain "top" (which must be null
443  * if uio is not).  Data provided in mbuf chain must be small
444  * enough to send all at once.
445  *
446  * Returns nonzero on error, timeout or signal; callers
447  * must check for short counts if EINTR/ERESTART are returned.
448  * Data and control buffers are freed on return.
449  */
450 int
451 sosend(so, addr, uio, top, control, flags, td)
452 	register struct socket *so;
453 	struct sockaddr *addr;
454 	struct uio *uio;
455 	struct mbuf *top;
456 	struct mbuf *control;
457 	int flags;
458 	struct thread *td;
459 {
460 	struct mbuf **mp;
461 	register struct mbuf *m;
462 	register long space, len, resid;
463 	int clen = 0, error, s, dontroute, mlen;
464 	int atomic = sosendallatonce(so) || top;
465 
466 	if (uio)
467 		resid = uio->uio_resid;
468 	else
469 		resid = top->m_pkthdr.len;
470 	/*
471 	 * In theory resid should be unsigned.
472 	 * However, space must be signed, as it might be less than 0
473 	 * if we over-committed, and we must use a signed comparison
474 	 * of space and resid.  On the other hand, a negative resid
475 	 * causes us to loop sending 0-length segments to the protocol.
476 	 *
477 	 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
478 	 * type sockets since that's an error.
479 	 */
480 	if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
481 		error = EINVAL;
482 		goto out;
483 	}
484 
485 	dontroute =
486 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
487 	    (so->so_proto->pr_flags & PR_ATOMIC);
488 	if (td)
489 		td->td_proc->p_stats->p_ru.ru_msgsnd++;
490 	if (control)
491 		clen = control->m_len;
492 #define	snderr(errno)	{ error = errno; splx(s); goto release; }
493 
494 restart:
495 	error = sblock(&so->so_snd, SBLOCKWAIT(flags));
496 	if (error)
497 		goto out;
498 	do {
499 		s = splnet();
500 		if (so->so_state & SS_CANTSENDMORE)
501 			snderr(EPIPE);
502 		if (so->so_error) {
503 			error = so->so_error;
504 			so->so_error = 0;
505 			splx(s);
506 			goto release;
507 		}
508 		if ((so->so_state & SS_ISCONNECTED) == 0) {
509 			/*
510 			 * `sendto' and `sendmsg' is allowed on a connection-
511 			 * based socket if it supports implied connect.
512 			 * Return ENOTCONN if not connected and no address is
513 			 * supplied.
514 			 */
515 			if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
516 			    (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
517 				if ((so->so_state & SS_ISCONFIRMING) == 0 &&
518 				    !(resid == 0 && clen != 0))
519 					snderr(ENOTCONN);
520 			} else if (addr == 0)
521 			    snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
522 				   ENOTCONN : EDESTADDRREQ);
523 		}
524 		space = sbspace(&so->so_snd);
525 		if (flags & MSG_OOB)
526 			space += 1024;
527 		if ((atomic && resid > so->so_snd.sb_hiwat) ||
528 		    clen > so->so_snd.sb_hiwat)
529 			snderr(EMSGSIZE);
530 		if (space < resid + clen && uio &&
531 		    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
532 			if (so->so_state & SS_NBIO)
533 				snderr(EWOULDBLOCK);
534 			sbunlock(&so->so_snd);
535 			error = sbwait(&so->so_snd);
536 			splx(s);
537 			if (error)
538 				goto out;
539 			goto restart;
540 		}
541 		splx(s);
542 		mp = &top;
543 		space -= clen;
544 		do {
545 		    if (uio == NULL) {
546 			/*
547 			 * Data is prepackaged in "top".
548 			 */
549 			resid = 0;
550 			if (flags & MSG_EOR)
551 				top->m_flags |= M_EOR;
552 		    } else do {
553 			if (top == 0) {
554 				MGETHDR(m, M_TRYWAIT, MT_DATA);
555 				if (m == NULL) {
556 					error = ENOBUFS;
557 					goto release;
558 				}
559 				mlen = MHLEN;
560 				m->m_pkthdr.len = 0;
561 				m->m_pkthdr.rcvif = (struct ifnet *)0;
562 			} else {
563 				MGET(m, M_TRYWAIT, MT_DATA);
564 				if (m == NULL) {
565 					error = ENOBUFS;
566 					goto release;
567 				}
568 				mlen = MLEN;
569 			}
570 			if (resid >= MINCLSIZE) {
571 				MCLGET(m, M_TRYWAIT);
572 				if ((m->m_flags & M_EXT) == 0)
573 					goto nopages;
574 				mlen = MCLBYTES;
575 				len = min(min(mlen, resid), space);
576 			} else {
577 nopages:
578 				len = min(min(mlen, resid), space);
579 				/*
580 				 * For datagram protocols, leave room
581 				 * for protocol headers in first mbuf.
582 				 */
583 				if (atomic && top == 0 && len < mlen)
584 					MH_ALIGN(m, len);
585 			}
586 			space -= len;
587 			error = uiomove(mtod(m, caddr_t), (int)len, uio);
588 			resid = uio->uio_resid;
589 			m->m_len = len;
590 			*mp = m;
591 			top->m_pkthdr.len += len;
592 			if (error)
593 				goto release;
594 			mp = &m->m_next;
595 			if (resid <= 0) {
596 				if (flags & MSG_EOR)
597 					top->m_flags |= M_EOR;
598 				break;
599 			}
600 		    } while (space > 0 && atomic);
601 		    if (dontroute)
602 			    so->so_options |= SO_DONTROUTE;
603 		    s = splnet();				/* XXX */
604 		    /*
605 		     * XXX all the SS_CANTSENDMORE checks previously
606 		     * done could be out of date.  We could have recieved
607 		     * a reset packet in an interrupt or maybe we slept
608 		     * while doing page faults in uiomove() etc. We could
609 		     * probably recheck again inside the splnet() protection
610 		     * here, but there are probably other places that this
611 		     * also happens.  We must rethink this.
612 		     */
613 		    error = (*so->so_proto->pr_usrreqs->pru_send)(so,
614 			(flags & MSG_OOB) ? PRUS_OOB :
615 			/*
616 			 * If the user set MSG_EOF, the protocol
617 			 * understands this flag and nothing left to
618 			 * send then use PRU_SEND_EOF instead of PRU_SEND.
619 			 */
620 			((flags & MSG_EOF) &&
621 			 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
622 			 (resid <= 0)) ?
623 				PRUS_EOF :
624 			/* If there is more to send set PRUS_MORETOCOME */
625 			(resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
626 			top, addr, control, td);
627 		    splx(s);
628 		    if (dontroute)
629 			    so->so_options &= ~SO_DONTROUTE;
630 		    clen = 0;
631 		    control = 0;
632 		    top = 0;
633 		    mp = &top;
634 		    if (error)
635 			goto release;
636 		} while (resid && space > 0);
637 	} while (resid);
638 
639 release:
640 	sbunlock(&so->so_snd);
641 out:
642 	if (top)
643 		m_freem(top);
644 	if (control)
645 		m_freem(control);
646 	return (error);
647 }
648 
649 /*
650  * Implement receive operations on a socket.
651  * We depend on the way that records are added to the sockbuf
652  * by sbappend*.  In particular, each record (mbufs linked through m_next)
653  * must begin with an address if the protocol so specifies,
654  * followed by an optional mbuf or mbufs containing ancillary data,
655  * and then zero or more mbufs of data.
656  * In order to avoid blocking network interrupts for the entire time here,
657  * we splx() while doing the actual copy to user space.
658  * Although the sockbuf is locked, new data may still be appended,
659  * and thus we must maintain consistency of the sockbuf during that time.
660  *
661  * The caller may receive the data as a single mbuf chain by supplying
662  * an mbuf **mp0 for use in returning the chain.  The uio is then used
663  * only for the count in uio_resid.
664  */
665 int
666 soreceive(so, psa, uio, mp0, controlp, flagsp)
667 	register struct socket *so;
668 	struct sockaddr **psa;
669 	struct uio *uio;
670 	struct mbuf **mp0;
671 	struct mbuf **controlp;
672 	int *flagsp;
673 {
674 	struct mbuf *m, **mp;
675 	register int flags, len, error, s, offset;
676 	struct protosw *pr = so->so_proto;
677 	struct mbuf *nextrecord;
678 	int moff, type = 0;
679 	int orig_resid = uio->uio_resid;
680 
681 	mp = mp0;
682 	if (psa)
683 		*psa = 0;
684 	if (controlp)
685 		*controlp = 0;
686 	if (flagsp)
687 		flags = *flagsp &~ MSG_EOR;
688 	else
689 		flags = 0;
690 	if (flags & MSG_OOB) {
691 		m = m_get(M_TRYWAIT, MT_DATA);
692 		if (m == NULL)
693 			return (ENOBUFS);
694 		error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
695 		if (error)
696 			goto bad;
697 		do {
698 			error = uiomove(mtod(m, caddr_t),
699 			    (int) min(uio->uio_resid, m->m_len), uio);
700 			m = m_free(m);
701 		} while (uio->uio_resid && error == 0 && m);
702 bad:
703 		if (m)
704 			m_freem(m);
705 		return (error);
706 	}
707 	if (mp)
708 		*mp = (struct mbuf *)0;
709 	if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
710 		(*pr->pr_usrreqs->pru_rcvd)(so, 0);
711 
712 restart:
713 	error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
714 	if (error)
715 		return (error);
716 	s = splnet();
717 
718 	m = so->so_rcv.sb_mb;
719 	/*
720 	 * If we have less data than requested, block awaiting more
721 	 * (subject to any timeout) if:
722 	 *   1. the current count is less than the low water mark, or
723 	 *   2. MSG_WAITALL is set, and it is possible to do the entire
724 	 *	receive operation at once if we block (resid <= hiwat).
725 	 *   3. MSG_DONTWAIT is not set
726 	 * If MSG_WAITALL is set but resid is larger than the receive buffer,
727 	 * we have to do the receive in sections, and thus risk returning
728 	 * a short count if a timeout or signal occurs after we start.
729 	 */
730 	if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
731 	    so->so_rcv.sb_cc < uio->uio_resid) &&
732 	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
733 	    ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
734 	    m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
735 		KASSERT(m != 0 || !so->so_rcv.sb_cc,
736 		    ("receive: m == %p so->so_rcv.sb_cc == %lu",
737 		    m, so->so_rcv.sb_cc));
738 		if (so->so_error) {
739 			if (m)
740 				goto dontblock;
741 			error = so->so_error;
742 			if ((flags & MSG_PEEK) == 0)
743 				so->so_error = 0;
744 			goto release;
745 		}
746 		if (so->so_state & SS_CANTRCVMORE) {
747 			if (m)
748 				goto dontblock;
749 			else
750 				goto release;
751 		}
752 		for (; m; m = m->m_next)
753 			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
754 				m = so->so_rcv.sb_mb;
755 				goto dontblock;
756 			}
757 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
758 		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
759 			error = ENOTCONN;
760 			goto release;
761 		}
762 		if (uio->uio_resid == 0)
763 			goto release;
764 		if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
765 			error = EWOULDBLOCK;
766 			goto release;
767 		}
768 		sbunlock(&so->so_rcv);
769 		error = sbwait(&so->so_rcv);
770 		splx(s);
771 		if (error)
772 			return (error);
773 		goto restart;
774 	}
775 dontblock:
776 	if (uio->uio_td)
777 		uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++;
778 	nextrecord = m->m_nextpkt;
779 	if (pr->pr_flags & PR_ADDR) {
780 		KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
781 		orig_resid = 0;
782 		if (psa)
783 			*psa = dup_sockaddr(mtod(m, struct sockaddr *),
784 					    mp0 == 0);
785 		if (flags & MSG_PEEK) {
786 			m = m->m_next;
787 		} else {
788 			sbfree(&so->so_rcv, m);
789 			MFREE(m, so->so_rcv.sb_mb);
790 			m = so->so_rcv.sb_mb;
791 		}
792 	}
793 	while (m && m->m_type == MT_CONTROL && error == 0) {
794 		if (flags & MSG_PEEK) {
795 			if (controlp)
796 				*controlp = m_copy(m, 0, m->m_len);
797 			m = m->m_next;
798 		} else {
799 			sbfree(&so->so_rcv, m);
800 			so->so_rcv.sb_mb = m->m_next;
801 			m->m_next = NULL;
802 			if (pr->pr_domain->dom_externalize)
803 				error =
804 				(*pr->pr_domain->dom_externalize)(m, controlp);
805 			else if (controlp)
806 				*controlp = m;
807 			else
808 				m_freem(m);
809 			m = so->so_rcv.sb_mb;
810 		}
811 		if (controlp) {
812 			orig_resid = 0;
813 			do
814 				controlp = &(*controlp)->m_next;
815 			while (*controlp != NULL);
816 		}
817 	}
818 	if (m) {
819 		if ((flags & MSG_PEEK) == 0)
820 			m->m_nextpkt = nextrecord;
821 		type = m->m_type;
822 		if (type == MT_OOBDATA)
823 			flags |= MSG_OOB;
824 	}
825 	moff = 0;
826 	offset = 0;
827 	while (m && uio->uio_resid > 0 && error == 0) {
828 		if (m->m_type == MT_OOBDATA) {
829 			if (type != MT_OOBDATA)
830 				break;
831 		} else if (type == MT_OOBDATA)
832 			break;
833 		else
834 		    KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
835 			("receive 3"));
836 		so->so_state &= ~SS_RCVATMARK;
837 		len = uio->uio_resid;
838 		if (so->so_oobmark && len > so->so_oobmark - offset)
839 			len = so->so_oobmark - offset;
840 		if (len > m->m_len - moff)
841 			len = m->m_len - moff;
842 		/*
843 		 * If mp is set, just pass back the mbufs.
844 		 * Otherwise copy them out via the uio, then free.
845 		 * Sockbuf must be consistent here (points to current mbuf,
846 		 * it points to next record) when we drop priority;
847 		 * we must note any additions to the sockbuf when we
848 		 * block interrupts again.
849 		 */
850 		if (mp == 0) {
851 			splx(s);
852 			error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
853 			s = splnet();
854 			if (error)
855 				goto release;
856 		} else
857 			uio->uio_resid -= len;
858 		if (len == m->m_len - moff) {
859 			if (m->m_flags & M_EOR)
860 				flags |= MSG_EOR;
861 			if (flags & MSG_PEEK) {
862 				m = m->m_next;
863 				moff = 0;
864 			} else {
865 				nextrecord = m->m_nextpkt;
866 				sbfree(&so->so_rcv, m);
867 				if (mp) {
868 					*mp = m;
869 					mp = &m->m_next;
870 					so->so_rcv.sb_mb = m = m->m_next;
871 					*mp = (struct mbuf *)0;
872 				} else {
873 					MFREE(m, so->so_rcv.sb_mb);
874 					m = so->so_rcv.sb_mb;
875 				}
876 				if (m)
877 					m->m_nextpkt = nextrecord;
878 			}
879 		} else {
880 			if (flags & MSG_PEEK)
881 				moff += len;
882 			else {
883 				if (mp)
884 					*mp = m_copym(m, 0, len, M_TRYWAIT);
885 				m->m_data += len;
886 				m->m_len -= len;
887 				so->so_rcv.sb_cc -= len;
888 			}
889 		}
890 		if (so->so_oobmark) {
891 			if ((flags & MSG_PEEK) == 0) {
892 				so->so_oobmark -= len;
893 				if (so->so_oobmark == 0) {
894 					so->so_state |= SS_RCVATMARK;
895 					break;
896 				}
897 			} else {
898 				offset += len;
899 				if (offset == so->so_oobmark)
900 					break;
901 			}
902 		}
903 		if (flags & MSG_EOR)
904 			break;
905 		/*
906 		 * If the MSG_WAITALL flag is set (for non-atomic socket),
907 		 * we must not quit until "uio->uio_resid == 0" or an error
908 		 * termination.  If a signal/timeout occurs, return
909 		 * with a short count but without error.
910 		 * Keep sockbuf locked against other readers.
911 		 */
912 		while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
913 		    !sosendallatonce(so) && !nextrecord) {
914 			if (so->so_error || so->so_state & SS_CANTRCVMORE)
915 				break;
916 			/*
917 			 * Notify the protocol that some data has been
918 			 * drained before blocking.
919 			 */
920 			if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
921 				(*pr->pr_usrreqs->pru_rcvd)(so, flags);
922 			error = sbwait(&so->so_rcv);
923 			if (error) {
924 				sbunlock(&so->so_rcv);
925 				splx(s);
926 				return (0);
927 			}
928 			m = so->so_rcv.sb_mb;
929 			if (m)
930 				nextrecord = m->m_nextpkt;
931 		}
932 	}
933 
934 	if (m && pr->pr_flags & PR_ATOMIC) {
935 		flags |= MSG_TRUNC;
936 		if ((flags & MSG_PEEK) == 0)
937 			(void) sbdroprecord(&so->so_rcv);
938 	}
939 	if ((flags & MSG_PEEK) == 0) {
940 		if (m == 0)
941 			so->so_rcv.sb_mb = nextrecord;
942 		if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
943 			(*pr->pr_usrreqs->pru_rcvd)(so, flags);
944 	}
945 	if (orig_resid == uio->uio_resid && orig_resid &&
946 	    (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
947 		sbunlock(&so->so_rcv);
948 		splx(s);
949 		goto restart;
950 	}
951 
952 	if (flagsp)
953 		*flagsp |= flags;
954 release:
955 	sbunlock(&so->so_rcv);
956 	splx(s);
957 	return (error);
958 }
959 
960 int
961 soshutdown(so, how)
962 	register struct socket *so;
963 	register int how;
964 {
965 	register struct protosw *pr = so->so_proto;
966 
967 	if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
968 		return (EINVAL);
969 
970 	if (how != SHUT_WR)
971 		sorflush(so);
972 	if (how != SHUT_RD)
973 		return ((*pr->pr_usrreqs->pru_shutdown)(so));
974 	return (0);
975 }
976 
977 void
978 sorflush(so)
979 	register struct socket *so;
980 {
981 	register struct sockbuf *sb = &so->so_rcv;
982 	register struct protosw *pr = so->so_proto;
983 	register int s;
984 	struct sockbuf asb;
985 
986 	sb->sb_flags |= SB_NOINTR;
987 	(void) sblock(sb, M_WAITOK);
988 	s = splimp();
989 	socantrcvmore(so);
990 	sbunlock(sb);
991 	asb = *sb;
992 	bzero((caddr_t)sb, sizeof (*sb));
993 	splx(s);
994 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
995 		(*pr->pr_domain->dom_dispose)(asb.sb_mb);
996 	sbrelease(&asb, so);
997 }
998 
999 #ifdef INET
1000 static int
1001 do_setopt_accept_filter(so, sopt)
1002 	struct	socket *so;
1003 	struct	sockopt *sopt;
1004 {
1005 	struct accept_filter_arg	*afap = NULL;
1006 	struct accept_filter	*afp;
1007 	struct so_accf	*af = so->so_accf;
1008 	int	error = 0;
1009 
1010 	/* do not set/remove accept filters on non listen sockets */
1011 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
1012 		error = EINVAL;
1013 		goto out;
1014 	}
1015 
1016 	/* removing the filter */
1017 	if (sopt == NULL) {
1018 		if (af != NULL) {
1019 			if (af->so_accept_filter != NULL &&
1020 				af->so_accept_filter->accf_destroy != NULL) {
1021 				af->so_accept_filter->accf_destroy(so);
1022 			}
1023 			if (af->so_accept_filter_str != NULL) {
1024 				FREE(af->so_accept_filter_str, M_ACCF);
1025 			}
1026 			FREE(af, M_ACCF);
1027 			so->so_accf = NULL;
1028 		}
1029 		so->so_options &= ~SO_ACCEPTFILTER;
1030 		return (0);
1031 	}
1032 	/* adding a filter */
1033 	/* must remove previous filter first */
1034 	if (af != NULL) {
1035 		error = EINVAL;
1036 		goto out;
1037 	}
1038 	/* don't put large objects on the kernel stack */
1039 	MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1040 	error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1041 	afap->af_name[sizeof(afap->af_name)-1] = '\0';
1042 	afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1043 	if (error)
1044 		goto out;
1045 	afp = accept_filt_get(afap->af_name);
1046 	if (afp == NULL) {
1047 		error = ENOENT;
1048 		goto out;
1049 	}
1050 	MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO);
1051 	if (afp->accf_create != NULL) {
1052 		if (afap->af_name[0] != '\0') {
1053 			int len = strlen(afap->af_name) + 1;
1054 
1055 			MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1056 			strcpy(af->so_accept_filter_str, afap->af_name);
1057 		}
1058 		af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1059 		if (af->so_accept_filter_arg == NULL) {
1060 			FREE(af->so_accept_filter_str, M_ACCF);
1061 			FREE(af, M_ACCF);
1062 			so->so_accf = NULL;
1063 			error = EINVAL;
1064 			goto out;
1065 		}
1066 	}
1067 	af->so_accept_filter = afp;
1068 	so->so_accf = af;
1069 	so->so_options |= SO_ACCEPTFILTER;
1070 out:
1071 	if (afap != NULL)
1072 		FREE(afap, M_TEMP);
1073 	return (error);
1074 }
1075 #endif /* INET */
1076 
1077 /*
1078  * Perhaps this routine, and sooptcopyout(), below, ought to come in
1079  * an additional variant to handle the case where the option value needs
1080  * to be some kind of integer, but not a specific size.
1081  * In addition to their use here, these functions are also called by the
1082  * protocol-level pr_ctloutput() routines.
1083  */
1084 int
1085 sooptcopyin(sopt, buf, len, minlen)
1086 	struct	sockopt *sopt;
1087 	void	*buf;
1088 	size_t	len;
1089 	size_t	minlen;
1090 {
1091 	size_t	valsize;
1092 
1093 	/*
1094 	 * If the user gives us more than we wanted, we ignore it,
1095 	 * but if we don't get the minimum length the caller
1096 	 * wants, we return EINVAL.  On success, sopt->sopt_valsize
1097 	 * is set to however much we actually retrieved.
1098 	 */
1099 	if ((valsize = sopt->sopt_valsize) < minlen)
1100 		return EINVAL;
1101 	if (valsize > len)
1102 		sopt->sopt_valsize = valsize = len;
1103 
1104 	if (sopt->sopt_td != 0)
1105 		return (copyin(sopt->sopt_val, buf, valsize));
1106 
1107 	bcopy(sopt->sopt_val, buf, valsize);
1108 	return 0;
1109 }
1110 
1111 int
1112 sosetopt(so, sopt)
1113 	struct socket *so;
1114 	struct sockopt *sopt;
1115 {
1116 	int	error, optval;
1117 	struct	linger l;
1118 	struct	timeval tv;
1119 	u_long  val;
1120 
1121 	error = 0;
1122 	if (sopt->sopt_level != SOL_SOCKET) {
1123 		if (so->so_proto && so->so_proto->pr_ctloutput)
1124 			return ((*so->so_proto->pr_ctloutput)
1125 				  (so, sopt));
1126 		error = ENOPROTOOPT;
1127 	} else {
1128 		switch (sopt->sopt_name) {
1129 #ifdef INET
1130 		case SO_ACCEPTFILTER:
1131 			error = do_setopt_accept_filter(so, sopt);
1132 			if (error)
1133 				goto bad;
1134 			break;
1135 #endif
1136 		case SO_LINGER:
1137 			error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1138 			if (error)
1139 				goto bad;
1140 
1141 			so->so_linger = l.l_linger;
1142 			if (l.l_onoff)
1143 				so->so_options |= SO_LINGER;
1144 			else
1145 				so->so_options &= ~SO_LINGER;
1146 			break;
1147 
1148 		case SO_DEBUG:
1149 		case SO_KEEPALIVE:
1150 		case SO_DONTROUTE:
1151 		case SO_USELOOPBACK:
1152 		case SO_BROADCAST:
1153 		case SO_REUSEADDR:
1154 		case SO_REUSEPORT:
1155 		case SO_OOBINLINE:
1156 		case SO_TIMESTAMP:
1157 			error = sooptcopyin(sopt, &optval, sizeof optval,
1158 					    sizeof optval);
1159 			if (error)
1160 				goto bad;
1161 			if (optval)
1162 				so->so_options |= sopt->sopt_name;
1163 			else
1164 				so->so_options &= ~sopt->sopt_name;
1165 			break;
1166 
1167 		case SO_SNDBUF:
1168 		case SO_RCVBUF:
1169 		case SO_SNDLOWAT:
1170 		case SO_RCVLOWAT:
1171 			error = sooptcopyin(sopt, &optval, sizeof optval,
1172 					    sizeof optval);
1173 			if (error)
1174 				goto bad;
1175 
1176 			/*
1177 			 * Values < 1 make no sense for any of these
1178 			 * options, so disallow them.
1179 			 */
1180 			if (optval < 1) {
1181 				error = EINVAL;
1182 				goto bad;
1183 			}
1184 
1185 			switch (sopt->sopt_name) {
1186 			case SO_SNDBUF:
1187 			case SO_RCVBUF:
1188 				if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1189 				    &so->so_snd : &so->so_rcv, (u_long)optval,
1190 				    so, curthread) == 0) {
1191 					error = ENOBUFS;
1192 					goto bad;
1193 				}
1194 				break;
1195 
1196 			/*
1197 			 * Make sure the low-water is never greater than
1198 			 * the high-water.
1199 			 */
1200 			case SO_SNDLOWAT:
1201 				so->so_snd.sb_lowat =
1202 				    (optval > so->so_snd.sb_hiwat) ?
1203 				    so->so_snd.sb_hiwat : optval;
1204 				break;
1205 			case SO_RCVLOWAT:
1206 				so->so_rcv.sb_lowat =
1207 				    (optval > so->so_rcv.sb_hiwat) ?
1208 				    so->so_rcv.sb_hiwat : optval;
1209 				break;
1210 			}
1211 			break;
1212 
1213 		case SO_SNDTIMEO:
1214 		case SO_RCVTIMEO:
1215 			error = sooptcopyin(sopt, &tv, sizeof tv,
1216 					    sizeof tv);
1217 			if (error)
1218 				goto bad;
1219 
1220 			/* assert(hz > 0); */
1221 			if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1222 			    tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1223 				error = EDOM;
1224 				goto bad;
1225 			}
1226 			/* assert(tick > 0); */
1227 			/* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1228 			val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1229 			if (val > SHRT_MAX) {
1230 				error = EDOM;
1231 				goto bad;
1232 			}
1233 
1234 			switch (sopt->sopt_name) {
1235 			case SO_SNDTIMEO:
1236 				so->so_snd.sb_timeo = val;
1237 				break;
1238 			case SO_RCVTIMEO:
1239 				so->so_rcv.sb_timeo = val;
1240 				break;
1241 			}
1242 			break;
1243 		default:
1244 			error = ENOPROTOOPT;
1245 			break;
1246 		}
1247 		if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1248 			(void) ((*so->so_proto->pr_ctloutput)
1249 				  (so, sopt));
1250 		}
1251 	}
1252 bad:
1253 	return (error);
1254 }
1255 
1256 /* Helper routine for getsockopt */
1257 int
1258 sooptcopyout(sopt, buf, len)
1259 	struct	sockopt *sopt;
1260 	void	*buf;
1261 	size_t	len;
1262 {
1263 	int	error;
1264 	size_t	valsize;
1265 
1266 	error = 0;
1267 
1268 	/*
1269 	 * Documented get behavior is that we always return a value,
1270 	 * possibly truncated to fit in the user's buffer.
1271 	 * Traditional behavior is that we always tell the user
1272 	 * precisely how much we copied, rather than something useful
1273 	 * like the total amount we had available for her.
1274 	 * Note that this interface is not idempotent; the entire answer must
1275 	 * generated ahead of time.
1276 	 */
1277 	valsize = min(len, sopt->sopt_valsize);
1278 	sopt->sopt_valsize = valsize;
1279 	if (sopt->sopt_val != 0) {
1280 		if (sopt->sopt_td != 0)
1281 			error = copyout(buf, sopt->sopt_val, valsize);
1282 		else
1283 			bcopy(buf, sopt->sopt_val, valsize);
1284 	}
1285 	return error;
1286 }
1287 
1288 int
1289 sogetopt(so, sopt)
1290 	struct socket *so;
1291 	struct sockopt *sopt;
1292 {
1293 	int	error, optval;
1294 	struct	linger l;
1295 	struct	timeval tv;
1296 #ifdef INET
1297 	struct accept_filter_arg *afap;
1298 #endif
1299 
1300 	error = 0;
1301 	if (sopt->sopt_level != SOL_SOCKET) {
1302 		if (so->so_proto && so->so_proto->pr_ctloutput) {
1303 			return ((*so->so_proto->pr_ctloutput)
1304 				  (so, sopt));
1305 		} else
1306 			return (ENOPROTOOPT);
1307 	} else {
1308 		switch (sopt->sopt_name) {
1309 #ifdef INET
1310 		case SO_ACCEPTFILTER:
1311 			if ((so->so_options & SO_ACCEPTCONN) == 0)
1312 				return (EINVAL);
1313 			MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1314 				M_TEMP, M_WAITOK | M_ZERO);
1315 			if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1316 				strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1317 				if (so->so_accf->so_accept_filter_str != NULL)
1318 					strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1319 			}
1320 			error = sooptcopyout(sopt, afap, sizeof(*afap));
1321 			FREE(afap, M_TEMP);
1322 			break;
1323 #endif
1324 
1325 		case SO_LINGER:
1326 			l.l_onoff = so->so_options & SO_LINGER;
1327 			l.l_linger = so->so_linger;
1328 			error = sooptcopyout(sopt, &l, sizeof l);
1329 			break;
1330 
1331 		case SO_USELOOPBACK:
1332 		case SO_DONTROUTE:
1333 		case SO_DEBUG:
1334 		case SO_KEEPALIVE:
1335 		case SO_REUSEADDR:
1336 		case SO_REUSEPORT:
1337 		case SO_BROADCAST:
1338 		case SO_OOBINLINE:
1339 		case SO_TIMESTAMP:
1340 			optval = so->so_options & sopt->sopt_name;
1341 integer:
1342 			error = sooptcopyout(sopt, &optval, sizeof optval);
1343 			break;
1344 
1345 		case SO_TYPE:
1346 			optval = so->so_type;
1347 			goto integer;
1348 
1349 		case SO_ERROR:
1350 			optval = so->so_error;
1351 			so->so_error = 0;
1352 			goto integer;
1353 
1354 		case SO_SNDBUF:
1355 			optval = so->so_snd.sb_hiwat;
1356 			goto integer;
1357 
1358 		case SO_RCVBUF:
1359 			optval = so->so_rcv.sb_hiwat;
1360 			goto integer;
1361 
1362 		case SO_SNDLOWAT:
1363 			optval = so->so_snd.sb_lowat;
1364 			goto integer;
1365 
1366 		case SO_RCVLOWAT:
1367 			optval = so->so_rcv.sb_lowat;
1368 			goto integer;
1369 
1370 		case SO_SNDTIMEO:
1371 		case SO_RCVTIMEO:
1372 			optval = (sopt->sopt_name == SO_SNDTIMEO ?
1373 				  so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1374 
1375 			tv.tv_sec = optval / hz;
1376 			tv.tv_usec = (optval % hz) * tick;
1377 			error = sooptcopyout(sopt, &tv, sizeof tv);
1378 			break;
1379 
1380 		default:
1381 			error = ENOPROTOOPT;
1382 			break;
1383 		}
1384 		return (error);
1385 	}
1386 }
1387 
1388 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1389 int
1390 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1391 {
1392 	struct mbuf *m, *m_prev;
1393 	int sopt_size = sopt->sopt_valsize;
1394 
1395 	MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
1396 	if (m == 0)
1397 		return ENOBUFS;
1398 	if (sopt_size > MLEN) {
1399 		MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT);
1400 		if ((m->m_flags & M_EXT) == 0) {
1401 			m_free(m);
1402 			return ENOBUFS;
1403 		}
1404 		m->m_len = min(MCLBYTES, sopt_size);
1405 	} else {
1406 		m->m_len = min(MLEN, sopt_size);
1407 	}
1408 	sopt_size -= m->m_len;
1409 	*mp = m;
1410 	m_prev = m;
1411 
1412 	while (sopt_size) {
1413 		MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
1414 		if (m == 0) {
1415 			m_freem(*mp);
1416 			return ENOBUFS;
1417 		}
1418 		if (sopt_size > MLEN) {
1419 			MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT);
1420 			if ((m->m_flags & M_EXT) == 0) {
1421 				m_freem(*mp);
1422 				return ENOBUFS;
1423 			}
1424 			m->m_len = min(MCLBYTES, sopt_size);
1425 		} else {
1426 			m->m_len = min(MLEN, sopt_size);
1427 		}
1428 		sopt_size -= m->m_len;
1429 		m_prev->m_next = m;
1430 		m_prev = m;
1431 	}
1432 	return 0;
1433 }
1434 
1435 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1436 int
1437 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1438 {
1439 	struct mbuf *m0 = m;
1440 
1441 	if (sopt->sopt_val == NULL)
1442 		return 0;
1443 	while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1444 		if (sopt->sopt_td != NULL) {
1445 			int error;
1446 
1447 			error = copyin(sopt->sopt_val, mtod(m, char *),
1448 				       m->m_len);
1449 			if (error != 0) {
1450 				m_freem(m0);
1451 				return(error);
1452 			}
1453 		} else
1454 			bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1455 		sopt->sopt_valsize -= m->m_len;
1456 		(caddr_t)sopt->sopt_val += m->m_len;
1457 		m = m->m_next;
1458 	}
1459 	if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1460 		panic("ip6_sooptmcopyin");
1461 	return 0;
1462 }
1463 
1464 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1465 int
1466 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1467 {
1468 	struct mbuf *m0 = m;
1469 	size_t valsize = 0;
1470 
1471 	if (sopt->sopt_val == NULL)
1472 		return 0;
1473 	while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1474 		if (sopt->sopt_td != NULL) {
1475 			int error;
1476 
1477 			error = copyout(mtod(m, char *), sopt->sopt_val,
1478 				       m->m_len);
1479 			if (error != 0) {
1480 				m_freem(m0);
1481 				return(error);
1482 			}
1483 		} else
1484 			bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1485 	       sopt->sopt_valsize -= m->m_len;
1486 	       (caddr_t)sopt->sopt_val += m->m_len;
1487 	       valsize += m->m_len;
1488 	       m = m->m_next;
1489 	}
1490 	if (m != NULL) {
1491 		/* enough soopt buffer should be given from user-land */
1492 		m_freem(m0);
1493 		return(EINVAL);
1494 	}
1495 	sopt->sopt_valsize = valsize;
1496 	return 0;
1497 }
1498 
1499 void
1500 sohasoutofband(so)
1501 	register struct socket *so;
1502 {
1503 	if (so->so_sigio != NULL)
1504 		pgsigio(so->so_sigio, SIGURG, 0);
1505 	selwakeup(&so->so_rcv.sb_sel);
1506 }
1507 
1508 int
1509 sopoll(struct socket *so, int events, struct ucred *cred, struct thread *td)
1510 {
1511 	int revents = 0;
1512 	int s = splnet();
1513 
1514 	if (events & (POLLIN | POLLRDNORM))
1515 		if (soreadable(so))
1516 			revents |= events & (POLLIN | POLLRDNORM);
1517 
1518 	if (events & (POLLOUT | POLLWRNORM))
1519 		if (sowriteable(so))
1520 			revents |= events & (POLLOUT | POLLWRNORM);
1521 
1522 	if (events & (POLLPRI | POLLRDBAND))
1523 		if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1524 			revents |= events & (POLLPRI | POLLRDBAND);
1525 
1526 	if (revents == 0) {
1527 		if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1528 			selrecord(td, &so->so_rcv.sb_sel);
1529 			so->so_rcv.sb_flags |= SB_SEL;
1530 		}
1531 
1532 		if (events & (POLLOUT | POLLWRNORM)) {
1533 			selrecord(td, &so->so_snd.sb_sel);
1534 			so->so_snd.sb_flags |= SB_SEL;
1535 		}
1536 	}
1537 
1538 	splx(s);
1539 	return (revents);
1540 }
1541 
1542 int
1543 sokqfilter(struct file *fp, struct knote *kn)
1544 {
1545 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
1546 	struct sockbuf *sb;
1547 	int s;
1548 
1549 	switch (kn->kn_filter) {
1550 	case EVFILT_READ:
1551 		if (so->so_options & SO_ACCEPTCONN)
1552 			kn->kn_fop = &solisten_filtops;
1553 		else
1554 			kn->kn_fop = &soread_filtops;
1555 		sb = &so->so_rcv;
1556 		break;
1557 	case EVFILT_WRITE:
1558 		kn->kn_fop = &sowrite_filtops;
1559 		sb = &so->so_snd;
1560 		break;
1561 	default:
1562 		return (1);
1563 	}
1564 
1565 	s = splnet();
1566 	SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
1567 	sb->sb_flags |= SB_KNOTE;
1568 	splx(s);
1569 	return (0);
1570 }
1571 
1572 static void
1573 filt_sordetach(struct knote *kn)
1574 {
1575 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
1576 	int s = splnet();
1577 
1578 	SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1579 	if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1580 		so->so_rcv.sb_flags &= ~SB_KNOTE;
1581 	splx(s);
1582 }
1583 
1584 /*ARGSUSED*/
1585 static int
1586 filt_soread(struct knote *kn, long hint)
1587 {
1588 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
1589 
1590 	kn->kn_data = so->so_rcv.sb_cc;
1591 	if (so->so_state & SS_CANTRCVMORE) {
1592 		kn->kn_flags |= EV_EOF;
1593 		kn->kn_fflags = so->so_error;
1594 		return (1);
1595 	}
1596 	if (so->so_error)	/* temporary udp error */
1597 		return (1);
1598 	if (kn->kn_sfflags & NOTE_LOWAT)
1599 		return (kn->kn_data >= kn->kn_sdata);
1600 	return (kn->kn_data >= so->so_rcv.sb_lowat);
1601 }
1602 
1603 static void
1604 filt_sowdetach(struct knote *kn)
1605 {
1606 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
1607 	int s = splnet();
1608 
1609 	SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1610 	if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1611 		so->so_snd.sb_flags &= ~SB_KNOTE;
1612 	splx(s);
1613 }
1614 
1615 /*ARGSUSED*/
1616 static int
1617 filt_sowrite(struct knote *kn, long hint)
1618 {
1619 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
1620 
1621 	kn->kn_data = sbspace(&so->so_snd);
1622 	if (so->so_state & SS_CANTSENDMORE) {
1623 		kn->kn_flags |= EV_EOF;
1624 		kn->kn_fflags = so->so_error;
1625 		return (1);
1626 	}
1627 	if (so->so_error)	/* temporary udp error */
1628 		return (1);
1629 	if (((so->so_state & SS_ISCONNECTED) == 0) &&
1630 	    (so->so_proto->pr_flags & PR_CONNREQUIRED))
1631 		return (0);
1632 	if (kn->kn_sfflags & NOTE_LOWAT)
1633 		return (kn->kn_data >= kn->kn_sdata);
1634 	return (kn->kn_data >= so->so_snd.sb_lowat);
1635 }
1636 
1637 /*ARGSUSED*/
1638 static int
1639 filt_solisten(struct knote *kn, long hint)
1640 {
1641 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
1642 
1643 	kn->kn_data = so->so_qlen - so->so_incqlen;
1644 	return (! TAILQ_EMPTY(&so->so_comp));
1645 }
1646 
1647 int
1648 socheckuid(struct socket *so, uid_t uid)
1649 {
1650 
1651 	if (so == NULL)
1652 		return (EPERM);
1653 	if (so->so_cred->cr_uid == uid)
1654 		return (0);
1655 	return (EPERM);
1656 }
1657