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