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