xref: /netbsd-src/sys/kern/uipc_socket.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
3  * 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  *	from: @(#)uipc_socket.c	7.28 (Berkeley) 5/4/91
34  *	$Id: uipc_socket.c,v 1.3 1993/06/27 06:08:15 andrew Exp $
35  */
36 
37 #include "param.h"
38 #include "systm.h"
39 #include "proc.h"
40 #include "file.h"
41 #include "malloc.h"
42 #include "mbuf.h"
43 #include "domain.h"
44 #include "kernel.h"
45 #include "select.h"
46 #include "protosw.h"
47 #include "socket.h"
48 #include "socketvar.h"
49 #include "resourcevar.h"
50 
51 /*
52  * Socket operation routines.
53  * These routines are called by the routines in
54  * sys_socket.c or from a system process, and
55  * implement the semantics of socket operations by
56  * switching out to the protocol specific routines.
57  */
58 /*ARGSUSED*/
59 int
60 socreate(dom, aso, type, proto)
61 	struct socket **aso;
62 	register int type;
63 	int proto;
64 {
65 	struct proc *p = curproc;		/* XXX */
66 	register struct protosw *prp;
67 	register struct socket *so;
68 	register int error;
69 
70 	if (proto)
71 		prp = pffindproto(dom, proto, type);
72 	else
73 		prp = pffindtype(dom, type);
74 	if (prp == 0)
75 		return (EPROTONOSUPPORT);
76 	if (prp->pr_type != type)
77 		return (EPROTOTYPE);
78 	MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_WAIT);
79 	bzero((caddr_t)so, sizeof(*so));
80 	so->so_type = type;
81 	if (p->p_ucred->cr_uid == 0)
82 		so->so_state = SS_PRIV;
83 	so->so_proto = prp;
84 	error =
85 	    (*prp->pr_usrreq)(so, PRU_ATTACH,
86 		(struct mbuf *)0, (struct mbuf *)proto, (struct mbuf *)0);
87 	if (error) {
88 		so->so_state |= SS_NOFDREF;
89 		sofree(so);
90 		return (error);
91 	}
92 	*aso = so;
93 	return (0);
94 }
95 
96 int
97 sobind(so, nam)
98 	struct socket *so;
99 	struct mbuf *nam;
100 {
101 	int s = splnet();
102 	int error;
103 
104 	error =
105 	    (*so->so_proto->pr_usrreq)(so, PRU_BIND,
106 		(struct mbuf *)0, nam, (struct mbuf *)0);
107 	splx(s);
108 	return (error);
109 }
110 
111 int
112 solisten(so, backlog)
113 	register struct socket *so;
114 	int backlog;
115 {
116 	int s = splnet(), error;
117 
118 	error =
119 	    (*so->so_proto->pr_usrreq)(so, PRU_LISTEN,
120 		(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
121 	if (error) {
122 		splx(s);
123 		return (error);
124 	}
125 	if (so->so_q == 0)
126 		so->so_options |= SO_ACCEPTCONN;
127 	if (backlog < 0)
128 		backlog = 0;
129 	so->so_qlimit = min(backlog, SOMAXCONN);
130 	splx(s);
131 	return (0);
132 }
133 
134 int
135 sofree(so)
136 	register struct socket *so;
137 {
138 
139 	if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
140 		return;
141 	if (so->so_head) {
142 		if (!soqremque(so, 0) && !soqremque(so, 1))
143 			panic("sofree dq");
144 		so->so_head = 0;
145 	}
146 	sbrelease(&so->so_snd);
147 	sorflush(so);
148 	FREE(so, M_SOCKET);
149 }
150 
151 /*
152  * Close a socket on last file table reference removal.
153  * Initiate disconnect if connected.
154  * Free socket when disconnect complete.
155  */
156 int
157 soclose(so)
158 	register struct socket *so;
159 {
160 	int s = splnet();		/* conservative */
161 	int error = 0;
162 
163 	if (so->so_options & SO_ACCEPTCONN) {
164 		while (so->so_q0)
165 			(void) soabort(so->so_q0);
166 		while (so->so_q)
167 			(void) soabort(so->so_q);
168 	}
169 	if (so->so_pcb == 0)
170 		goto discard;
171 	if (so->so_state & SS_ISCONNECTED) {
172 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
173 			error = sodisconnect(so);
174 			if (error)
175 				goto drop;
176 		}
177 		if (so->so_options & SO_LINGER) {
178 			if ((so->so_state & SS_ISDISCONNECTING) &&
179 			    (so->so_state & SS_NBIO))
180 				goto drop;
181 			while (so->so_state & SS_ISCONNECTED)
182 				if (error = tsleep((caddr_t)&so->so_timeo,
183 				    PSOCK | PCATCH, netcls, so->so_linger))
184 					break;
185 		}
186 	}
187 drop:
188 	if (so->so_pcb) {
189 		int error2 =
190 		    (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
191 			(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
192 		if (error == 0)
193 			error = error2;
194 	}
195 discard:
196 	if (so->so_state & SS_NOFDREF)
197 		panic("soclose: NOFDREF");
198 	so->so_state |= SS_NOFDREF;
199 	sofree(so);
200 	splx(s);
201 	return (error);
202 }
203 
204 /*
205  * Must be called at splnet...
206  */
207 int
208 soabort(so)
209 	struct socket *so;
210 {
211 
212 	return (
213 	    (*so->so_proto->pr_usrreq)(so, PRU_ABORT,
214 		(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0));
215 }
216 
217 int
218 soaccept(so, nam)
219 	register struct socket *so;
220 	struct mbuf *nam;
221 {
222 	int s = splnet();
223 	int error;
224 
225 	if ((so->so_state & SS_NOFDREF) == 0)
226 		panic("soaccept: !NOFDREF");
227 	so->so_state &= ~SS_NOFDREF;
228 	error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
229 	    (struct mbuf *)0, nam, (struct mbuf *)0);
230 	splx(s);
231 	return (error);
232 }
233 
234 int
235 soconnect(so, nam)
236 	register struct socket *so;
237 	struct mbuf *nam;
238 {
239 	int s;
240 	int error;
241 
242 	if (so->so_options & SO_ACCEPTCONN)
243 		return (EOPNOTSUPP);
244 	s = splnet();
245 	/*
246 	 * If protocol is connection-based, can only connect once.
247 	 * Otherwise, if connected, try to disconnect first.
248 	 * This allows user to disconnect by connecting to, e.g.,
249 	 * a null address.
250 	 */
251 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
252 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
253 	    (error = sodisconnect(so))))
254 		error = EISCONN;
255 	else
256 		error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
257 		    (struct mbuf *)0, nam, (struct mbuf *)0);
258 	splx(s);
259 	return (error);
260 }
261 
262 int
263 soconnect2(so1, so2)
264 	register struct socket *so1;
265 	struct socket *so2;
266 {
267 	int s = splnet();
268 	int error;
269 
270 	error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
271 	    (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0);
272 	splx(s);
273 	return (error);
274 }
275 
276 int
277 sodisconnect(so)
278 	register struct socket *so;
279 {
280 	int s = splnet();
281 	int error;
282 
283 	if ((so->so_state & SS_ISCONNECTED) == 0) {
284 		error = ENOTCONN;
285 		goto bad;
286 	}
287 	if (so->so_state & SS_ISDISCONNECTING) {
288 		error = EALREADY;
289 		goto bad;
290 	}
291 	error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
292 	    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
293 bad:
294 	splx(s);
295 	return (error);
296 }
297 
298 /*
299  * Send on a socket.
300  * If send must go all at once and message is larger than
301  * send buffering, then hard error.
302  * Lock against other senders.
303  * If must go all at once and not enough room now, then
304  * inform user that this would block and do nothing.
305  * Otherwise, if nonblocking, send as much as possible.
306  * The data to be sent is described by "uio" if nonzero,
307  * otherwise by the mbuf chain "top" (which must be null
308  * if uio is not).  Data provided in mbuf chain must be small
309  * enough to send all at once.
310  *
311  * Returns nonzero on error, timeout or signal; callers
312  * must check for short counts if EINTR/ERESTART are returned.
313  * Data and control buffers are freed on return.
314  */
315 int
316 sosend(so, addr, uio, top, control, flags)
317 	register struct socket *so;
318 	struct mbuf *addr;
319 	struct uio *uio;
320 	struct mbuf *top;
321 	struct mbuf *control;
322 	int flags;
323 {
324 	struct proc *p = curproc;		/* XXX */
325 	struct mbuf **mp;
326 	register struct mbuf *m;
327 	register long space, len, resid;
328 	int clen = 0, error, s, dontroute, mlen;
329 	int atomic = sosendallatonce(so) || top;
330 
331 	if (uio)
332 		resid = uio->uio_resid;
333 	else
334 		resid = top->m_pkthdr.len;
335 	dontroute =
336 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
337 	    (so->so_proto->pr_flags & PR_ATOMIC);
338 	p->p_stats->p_ru.ru_msgsnd++;
339 	if (control)
340 		clen = control->m_len;
341 #define	snderr(errno)	{ error = errno; splx(s); goto release; }
342 
343 restart:
344 	if (error = sblock(&so->so_snd))
345 		goto out;
346 	do {
347 		s = splnet();
348 		if (so->so_state & SS_CANTSENDMORE)
349 			snderr(EPIPE);
350 		if (so->so_error)
351 			snderr(so->so_error);
352 		if ((so->so_state & SS_ISCONNECTED) == 0) {
353 			if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
354 				if ((so->so_state & SS_ISCONFIRMING) == 0 &&
355 				    !(resid == 0 && clen != 0))
356 					snderr(ENOTCONN);
357 			} else if (addr == 0)
358 				snderr(EDESTADDRREQ);
359 		}
360 		space = sbspace(&so->so_snd);
361 		if (flags & MSG_OOB)
362 			space += 1024;
363 		if (space < resid + clen &&
364 		    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
365 			if (atomic && resid > so->so_snd.sb_hiwat ||
366 			    clen > so->so_snd.sb_hiwat)
367 				snderr(EMSGSIZE);
368 			if (so->so_state & SS_NBIO)
369 				snderr(EWOULDBLOCK);
370 			sbunlock(&so->so_snd);
371 			error = sbwait(&so->so_snd);
372 			splx(s);
373 			if (error)
374 				goto out;
375 			goto restart;
376 		}
377 		splx(s);
378 		mp = &top;
379 		space -= clen;
380 		do {
381 		    if (uio == NULL) {
382 			/*
383 			 * Data is prepackaged in "top".
384 			 */
385 			resid = 0;
386 			if (flags & MSG_EOR)
387 				top->m_flags |= M_EOR;
388 		    } else do {
389 			if (top == 0) {
390 				MGETHDR(m, M_WAIT, MT_DATA);
391 				mlen = MHLEN;
392 				m->m_pkthdr.len = 0;
393 				m->m_pkthdr.rcvif = (struct ifnet *)0;
394 			} else {
395 				MGET(m, M_WAIT, MT_DATA);
396 				mlen = MLEN;
397 			}
398 			if (resid >= MINCLSIZE && space >= MCLBYTES) {
399 				MCLGET(m, M_WAIT);
400 				if ((m->m_flags & M_EXT) == 0)
401 					goto nopages;
402 				mlen = MCLBYTES;
403 #ifdef	MAPPED_MBUFS
404 				len = min(MCLBYTES, resid);
405 #else
406 				if (top == 0) {
407 					len = min(MCLBYTES - max_hdr, resid);
408 					m->m_data += max_hdr;
409 				} else
410 					len = min(MCLBYTES, resid);
411 #endif
412 				space -= MCLBYTES;
413 			} else {
414 nopages:
415 				len = min(min(mlen, resid), space);
416 				space -= len;
417 				/*
418 				 * For datagram protocols, leave room
419 				 * for protocol headers in first mbuf.
420 				 */
421 				if (atomic && top == 0 && len < mlen)
422 					MH_ALIGN(m, len);
423 			}
424 			error = uiomove(mtod(m, caddr_t), (int)len, uio);
425 			resid = uio->uio_resid;
426 			m->m_len = len;
427 			*mp = m;
428 			top->m_pkthdr.len += len;
429 			if (error)
430 				goto release;
431 			mp = &m->m_next;
432 			if (resid <= 0) {
433 				if (flags & MSG_EOR)
434 					top->m_flags |= M_EOR;
435 				break;
436 			}
437 		    } while (space > 0 && atomic);
438 		    if (dontroute)
439 			    so->so_options |= SO_DONTROUTE;
440 		    s = splnet();				/* XXX */
441 		    error = (*so->so_proto->pr_usrreq)(so,
442 			(flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
443 			top, addr, control);
444 		    splx(s);
445 		    if (dontroute)
446 			    so->so_options &= ~SO_DONTROUTE;
447 		    clen = 0;
448 		    control = 0;
449 		    top = 0;
450 		    mp = &top;
451 		    if (error)
452 			goto release;
453 		} while (resid && space > 0);
454 	} while (resid);
455 
456 release:
457 	sbunlock(&so->so_snd);
458 out:
459 	if (top)
460 		m_freem(top);
461 	if (control)
462 		m_freem(control);
463 	return (error);
464 }
465 
466 /*
467  * Implement receive operations on a socket.
468  * We depend on the way that records are added to the sockbuf
469  * by sbappend*.  In particular, each record (mbufs linked through m_next)
470  * must begin with an address if the protocol so specifies,
471  * followed by an optional mbuf or mbufs containing ancillary data,
472  * and then zero or more mbufs of data.
473  * In order to avoid blocking network interrupts for the entire time here,
474  * we splx() while doing the actual copy to user space.
475  * Although the sockbuf is locked, new data may still be appended,
476  * and thus we must maintain consistency of the sockbuf during that time.
477  *
478  * The caller may receive the data as a single mbuf chain by supplying
479  * an mbuf **mp0 for use in returning the chain.  The uio is then used
480  * only for the count in uio_resid.
481  */
482 int
483 soreceive(so, paddr, uio, mp0, controlp, flagsp)
484 	register struct socket *so;
485 	struct mbuf **paddr;
486 	struct uio *uio;
487 	struct mbuf **mp0;
488 	struct mbuf **controlp;
489 	int *flagsp;
490 {
491 	struct proc *p = curproc;		/* XXX */
492 	register struct mbuf *m, **mp;
493 	register int flags, len, error, s, offset;
494 	struct protosw *pr = so->so_proto;
495 	struct mbuf *nextrecord;
496 	int moff, type;
497 	int orig_resid = uio->uio_resid;
498 
499 	mp = mp0;
500 	if (paddr)
501 		*paddr = 0;
502 	if (controlp)
503 		*controlp = 0;
504 	if (flagsp)
505 		flags = *flagsp &~ MSG_EOR;
506 	else
507 		flags = 0;
508 	if (flags & MSG_OOB) {
509 		m = m_get(M_WAIT, MT_DATA);
510 		error = (*pr->pr_usrreq)(so, PRU_RCVOOB,
511 		    m, (struct mbuf *)(flags & MSG_PEEK), (struct mbuf *)0);
512 		if (error)
513 			goto bad;
514 		do {
515 			error = uiomove(mtod(m, caddr_t),
516 			    (int) min(uio->uio_resid, m->m_len), uio);
517 			m = m_free(m);
518 		} while (uio->uio_resid && error == 0 && m);
519 bad:
520 		if (m)
521 			m_freem(m);
522 		return (error);
523 	}
524 	if (mp)
525 		*mp = (struct mbuf *)0;
526 	if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
527 		(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
528 		    (struct mbuf *)0, (struct mbuf *)0);
529 
530 restart:
531 	if (error = sblock(&so->so_rcv))
532 		return (error);
533 	s = splnet();
534 
535 	m = so->so_rcv.sb_mb;
536 	/*
537 	 * If we have less data than requested, block awaiting more
538 	 * (subject to any timeout) if:
539 	 *   1. the current count is less than the low water mark, or
540 	 *   2. MSG_WAITALL is set, and it is possible to do the entire
541 	 *	receive operation at once if we block (resid <= hiwat).
542 	 * If MSG_WAITALL is set but resid is larger than the receive buffer,
543 	 * we have to do the receive in sections, and thus risk returning
544 	 * a short count if a timeout or signal occurs after we start.
545 	 */
546 	while (m == 0 || so->so_rcv.sb_cc < uio->uio_resid &&
547 	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
548 	    ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
549 	    m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0) {
550 #ifdef DIAGNOSTIC
551 		if (m == 0 && so->so_rcv.sb_cc)
552 			panic("receive 1");
553 #endif
554 		if (so->so_error) {
555 			if (m)
556 				break;
557 			error = so->so_error;
558 			if ((flags & MSG_PEEK) == 0)
559 				so->so_error = 0;
560 			goto release;
561 		}
562 		if (so->so_state & SS_CANTRCVMORE) {
563 			if (m)
564 				break;
565 			else
566 				goto release;
567 		}
568 		for (; m; m = m->m_next)
569 			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
570 				m = so->so_rcv.sb_mb;
571 				goto dontblock;
572 			}
573 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
574 		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
575 			error = ENOTCONN;
576 			goto release;
577 		}
578 		if (uio->uio_resid == 0)
579 			goto release;
580 		if (so->so_state & SS_NBIO) {
581 			error = EWOULDBLOCK;
582 			goto release;
583 		}
584 		sbunlock(&so->so_rcv);
585 		error = sbwait(&so->so_rcv);
586 		splx(s);
587 		if (error)
588 			return (error);
589 		goto restart;
590 	}
591 dontblock:
592 	p->p_stats->p_ru.ru_msgrcv++;
593 	nextrecord = m->m_nextpkt;
594 	if (pr->pr_flags & PR_ADDR) {
595 #ifdef DIAGNOSTIC
596 		if (m->m_type != MT_SONAME)
597 			panic("receive 1a");
598 #endif
599 		orig_resid = 0;
600 		if (flags & MSG_PEEK) {
601 			if (paddr)
602 				*paddr = m_copy(m, 0, m->m_len);
603 			m = m->m_next;
604 		} else {
605 			sbfree(&so->so_rcv, m);
606 			if (paddr) {
607 				*paddr = m;
608 				so->so_rcv.sb_mb = m->m_next;
609 				m->m_next = 0;
610 				m = so->so_rcv.sb_mb;
611 			} else {
612 				MFREE(m, so->so_rcv.sb_mb);
613 				m = so->so_rcv.sb_mb;
614 			}
615 		}
616 	}
617 	while (m && m->m_type == MT_CONTROL && error == 0) {
618 		if (flags & MSG_PEEK) {
619 			if (controlp)
620 				*controlp = m_copy(m, 0, m->m_len);
621 			m = m->m_next;
622 		} else {
623 			sbfree(&so->so_rcv, m);
624 			if (controlp) {
625 				if (pr->pr_domain->dom_externalize &&
626 				    mtod(m, struct cmsghdr *)->cmsg_type ==
627 				    SCM_RIGHTS)
628 				   error = (*pr->pr_domain->dom_externalize)(m);
629 				*controlp = m;
630 				so->so_rcv.sb_mb = m->m_next;
631 				m->m_next = 0;
632 				m = so->so_rcv.sb_mb;
633 			} else {
634 				MFREE(m, so->so_rcv.sb_mb);
635 				m = so->so_rcv.sb_mb;
636 			}
637 		}
638 		if (controlp) {
639 			orig_resid = 0;
640 			controlp = &(*controlp)->m_next;
641 		}
642 	}
643 	if (m) {
644 		if ((flags & MSG_PEEK) == 0)
645 			m->m_nextpkt = nextrecord;
646 		type = m->m_type;
647 		if (type == MT_OOBDATA)
648 			flags |= MSG_OOB;
649 	}
650 	moff = 0;
651 	offset = 0;
652 	while (m && uio->uio_resid > 0 && error == 0) {
653 		if (m->m_type == MT_OOBDATA) {
654 			if (type != MT_OOBDATA)
655 				break;
656 		} else if (type == MT_OOBDATA)
657 			break;
658 #ifdef DIAGNOSTIC
659 		else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
660 			panic("receive 3");
661 #endif
662 		so->so_state &= ~SS_RCVATMARK;
663 		len = uio->uio_resid;
664 		if (so->so_oobmark && len > so->so_oobmark - offset)
665 			len = so->so_oobmark - offset;
666 		if (len > m->m_len - moff)
667 			len = m->m_len - moff;
668 		/*
669 		 * If mp is set, just pass back the mbufs.
670 		 * Otherwise copy them out via the uio, then free.
671 		 * Sockbuf must be consistent here (points to current mbuf,
672 		 * it points to next record) when we drop priority;
673 		 * we must note any additions to the sockbuf when we
674 		 * block interrupts again.
675 		 */
676 		if (mp == 0) {
677 			splx(s);
678 			error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
679 			s = splnet();
680 		} else
681 			uio->uio_resid -= len;
682 		if (len == m->m_len - moff) {
683 			if (m->m_flags & M_EOR)
684 				flags |= MSG_EOR;
685 			if (flags & MSG_PEEK) {
686 				m = m->m_next;
687 				moff = 0;
688 			} else {
689 				nextrecord = m->m_nextpkt;
690 				sbfree(&so->so_rcv, m);
691 				if (mp) {
692 					*mp = m;
693 					mp = &m->m_next;
694 					so->so_rcv.sb_mb = m = m->m_next;
695 					*mp = (struct mbuf *)0;
696 				} else {
697 					MFREE(m, so->so_rcv.sb_mb);
698 					m = so->so_rcv.sb_mb;
699 				}
700 				if (m)
701 					m->m_nextpkt = nextrecord;
702 			}
703 		} else {
704 			if (flags & MSG_PEEK)
705 				moff += len;
706 			else {
707 				if (mp)
708 					*mp = m_copym(m, 0, len, M_WAIT);
709 				m->m_data += len;
710 				m->m_len -= len;
711 				so->so_rcv.sb_cc -= len;
712 			}
713 		}
714 		if (so->so_oobmark) {
715 			if ((flags & MSG_PEEK) == 0) {
716 				so->so_oobmark -= len;
717 				if (so->so_oobmark == 0) {
718 					so->so_state |= SS_RCVATMARK;
719 					break;
720 				}
721 			} else
722 				offset += len;
723 		}
724 		if (flags & MSG_EOR)
725 			break;
726 		/*
727 		 * If the MSG_WAITALL flag is set (for non-atomic socket),
728 		 * we must not quit until "uio->uio_resid == 0" or an error
729 		 * termination.  If a signal/timeout occurs, return
730 		 * with a short count but without error.
731 		 * Keep sockbuf locked against other readers.
732 		 */
733 		while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
734 		    !sosendallatonce(so) && !nextrecord) {
735 			if (so->so_error || so->so_state & SS_CANTRCVMORE)
736 				break;
737 			error = sbwait(&so->so_rcv);
738 			if (error) {
739 				sbunlock(&so->so_rcv);
740 				splx(s);
741 				return (0);
742 			}
743 			if (m = so->so_rcv.sb_mb)
744 				nextrecord = m->m_nextpkt;
745 		}
746 	}
747 
748 	if (m && pr->pr_flags & PR_ATOMIC) {
749 		flags |= MSG_TRUNC;
750 		if ((flags & MSG_PEEK) == 0)
751 			(void) sbdroprecord(&so->so_rcv);
752 	}
753 	if ((flags & MSG_PEEK) == 0) {
754 		if (m == 0)
755 			so->so_rcv.sb_mb = nextrecord;
756 		if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
757 			(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
758 			    (struct mbuf *)flags, (struct mbuf *)0,
759 			    (struct mbuf *)0);
760 	}
761 	if (orig_resid == uio->uio_resid && orig_resid &&
762 	    (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
763 		sbunlock(&so->so_rcv);
764 		splx(s);
765 		goto restart;
766 	}
767 
768 	if (flagsp)
769 		*flagsp |= flags;
770 release:
771 	sbunlock(&so->so_rcv);
772 	splx(s);
773 	return (error);
774 }
775 
776 soshutdown(so, how)
777 	register struct socket *so;
778 	register int how;
779 {
780 	register struct protosw *pr = so->so_proto;
781 
782 	how++;
783 	if (how & FREAD)
784 		sorflush(so);
785 	if (how & FWRITE)
786 		return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN,
787 		    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0));
788 	return (0);
789 }
790 
791 sorflush(so)
792 	register struct socket *so;
793 {
794 	register struct sockbuf *sb = &so->so_rcv;
795 	register struct protosw *pr = so->so_proto;
796 	register int s;
797 	struct sockbuf asb;
798 
799 	sb->sb_flags |= SB_NOINTR;
800 	(void) sblock(sb);
801 	s = splimp();
802 	socantrcvmore(so);
803 	sbunlock(sb);
804 	asb = *sb;
805 	bzero((caddr_t)sb, sizeof (*sb));
806 	splx(s);
807 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
808 		(*pr->pr_domain->dom_dispose)(asb.sb_mb);
809 	sbrelease(&asb);
810 }
811 
812 sosetopt(so, level, optname, m0)
813 	register struct socket *so;
814 	int level, optname;
815 	struct mbuf *m0;
816 {
817 	int error = 0;
818 	register struct mbuf *m = m0;
819 
820 	if (level != SOL_SOCKET) {
821 		if (so->so_proto && so->so_proto->pr_ctloutput)
822 			return ((*so->so_proto->pr_ctloutput)
823 				  (PRCO_SETOPT, so, level, optname, &m0));
824 		error = ENOPROTOOPT;
825 	} else {
826 		switch (optname) {
827 
828 		case SO_LINGER:
829 			if (m == NULL || m->m_len != sizeof (struct linger)) {
830 				error = EINVAL;
831 				goto bad;
832 			}
833 			so->so_linger = mtod(m, struct linger *)->l_linger;
834 			/* fall thru... */
835 
836 		case SO_DEBUG:
837 		case SO_KEEPALIVE:
838 		case SO_DONTROUTE:
839 		case SO_USELOOPBACK:
840 		case SO_BROADCAST:
841 		case SO_REUSEADDR:
842 		case SO_OOBINLINE:
843 			if (m == NULL || m->m_len < sizeof (int)) {
844 				error = EINVAL;
845 				goto bad;
846 			}
847 			if (*mtod(m, int *))
848 				so->so_options |= optname;
849 			else
850 				so->so_options &= ~optname;
851 			break;
852 
853 		case SO_SNDBUF:
854 		case SO_RCVBUF:
855 		case SO_SNDLOWAT:
856 		case SO_RCVLOWAT:
857 			if (m == NULL || m->m_len < sizeof (int)) {
858 				error = EINVAL;
859 				goto bad;
860 			}
861 			switch (optname) {
862 
863 			case SO_SNDBUF:
864 			case SO_RCVBUF:
865 				if (sbreserve(optname == SO_SNDBUF ?
866 				    &so->so_snd : &so->so_rcv,
867 				    (u_long) *mtod(m, int *)) == 0) {
868 					error = ENOBUFS;
869 					goto bad;
870 				}
871 				break;
872 
873 			case SO_SNDLOWAT:
874 				so->so_snd.sb_lowat = *mtod(m, int *);
875 				break;
876 			case SO_RCVLOWAT:
877 				so->so_rcv.sb_lowat = *mtod(m, int *);
878 				break;
879 			}
880 			break;
881 
882 		case SO_SNDTIMEO:
883 		case SO_RCVTIMEO:
884 		    {
885 			struct timeval *tv;
886 			short val;
887 
888 			if (m == NULL || m->m_len < sizeof (*tv)) {
889 				error = EINVAL;
890 				goto bad;
891 			}
892 			tv = mtod(m, struct timeval *);
893 			if (tv->tv_sec > SHRT_MAX / hz - hz) {
894 				error = EDOM;
895 				goto bad;
896 			}
897 			val = tv->tv_sec * hz + tv->tv_usec / tick;
898 
899 			switch (optname) {
900 
901 			case SO_SNDTIMEO:
902 				so->so_snd.sb_timeo = val;
903 				break;
904 			case SO_RCVTIMEO:
905 				so->so_rcv.sb_timeo = val;
906 				break;
907 			}
908 			break;
909 		    }
910 
911 		default:
912 			error = ENOPROTOOPT;
913 			break;
914 		}
915 	}
916 bad:
917 	if (m)
918 		(void) m_free(m);
919 	return (error);
920 }
921 
922 sogetopt(so, level, optname, mp)
923 	register struct socket *so;
924 	int level, optname;
925 	struct mbuf **mp;
926 {
927 	register struct mbuf *m;
928 
929 	if (level != SOL_SOCKET) {
930 		if (so->so_proto && so->so_proto->pr_ctloutput) {
931 			return ((*so->so_proto->pr_ctloutput)
932 				  (PRCO_GETOPT, so, level, optname, mp));
933 		} else
934 			return (ENOPROTOOPT);
935 	} else {
936 		m = m_get(M_WAIT, MT_SOOPTS);
937 		m->m_len = sizeof (int);
938 
939 		switch (optname) {
940 
941 		case SO_LINGER:
942 			m->m_len = sizeof (struct linger);
943 			mtod(m, struct linger *)->l_onoff =
944 				so->so_options & SO_LINGER;
945 			mtod(m, struct linger *)->l_linger = so->so_linger;
946 			break;
947 
948 		case SO_USELOOPBACK:
949 		case SO_DONTROUTE:
950 		case SO_DEBUG:
951 		case SO_KEEPALIVE:
952 		case SO_REUSEADDR:
953 		case SO_BROADCAST:
954 		case SO_OOBINLINE:
955 			*mtod(m, int *) = so->so_options & optname;
956 			break;
957 
958 		case SO_TYPE:
959 			*mtod(m, int *) = so->so_type;
960 			break;
961 
962 		case SO_ERROR:
963 			*mtod(m, int *) = so->so_error;
964 			so->so_error = 0;
965 			break;
966 
967 		case SO_SNDBUF:
968 			*mtod(m, int *) = so->so_snd.sb_hiwat;
969 			break;
970 
971 		case SO_RCVBUF:
972 			*mtod(m, int *) = so->so_rcv.sb_hiwat;
973 			break;
974 
975 		case SO_SNDLOWAT:
976 			*mtod(m, int *) = so->so_snd.sb_lowat;
977 			break;
978 
979 		case SO_RCVLOWAT:
980 			*mtod(m, int *) = so->so_rcv.sb_lowat;
981 			break;
982 
983 		case SO_SNDTIMEO:
984 		case SO_RCVTIMEO:
985 		    {
986 			int val = (optname == SO_SNDTIMEO ?
987 			     so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
988 
989 			m->m_len = sizeof(struct timeval);
990 			mtod(m, struct timeval *)->tv_sec = val / hz;
991 			mtod(m, struct timeval *)->tv_usec =
992 			    (val % hz) / tick;
993 			break;
994 		    }
995 
996 		default:
997 			(void)m_free(m);
998 			return (ENOPROTOOPT);
999 		}
1000 		*mp = m;
1001 		return (0);
1002 	}
1003 }
1004 
1005 sohasoutofband(so)
1006 	register struct socket *so;
1007 {
1008 	struct proc *p;
1009 
1010 	if (so->so_pgid < 0)
1011 		gsignal(-so->so_pgid, SIGURG);
1012 	else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
1013 		psignal(p, SIGURG);
1014 	selwakeup(&so->so_rcv.sb_sel);
1015 }
1016