xref: /csrg-svn/sys/kern/uipc_socket.c (revision 46452)
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.25 (Berkeley) 02/18/91
8  */
9 
10 #include "param.h"
11 #include "user.h"
12 #include "proc.h"
13 #include "file.h"
14 #include "malloc.h"
15 #include "mbuf.h"
16 #include "domain.h"
17 #include "kernel.h"
18 #include "protosw.h"
19 #include "socket.h"
20 #include "socketvar.h"
21 #include "time.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  * TODO:
31  *	test socketpair
32  *	clean up async
33  *	out-of-band is a kludge
34  */
35 /*ARGSUSED*/
36 socreate(dom, aso, type, proto)
37 	struct socket **aso;
38 	register int type;
39 	int proto;
40 {
41 	register struct protosw *prp;
42 	register struct socket *so;
43 	register int error;
44 
45 	if (proto)
46 		prp = pffindproto(dom, proto, type);
47 	else
48 		prp = pffindtype(dom, type);
49 	if (prp == 0)
50 		return (EPROTONOSUPPORT);
51 	if (prp->pr_type != type)
52 		return (EPROTOTYPE);
53 	MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_WAIT);
54 	bzero((caddr_t)so, sizeof(*so));
55 	so->so_type = type;
56 	if (u.u_uid == 0)
57 		so->so_state = SS_PRIV;
58 	so->so_proto = prp;
59 	error =
60 	    (*prp->pr_usrreq)(so, PRU_ATTACH,
61 		(struct mbuf *)0, (struct mbuf *)proto, (struct mbuf *)0);
62 	if (error) {
63 		so->so_state |= SS_NOFDREF;
64 		sofree(so);
65 		return (error);
66 	}
67 	*aso = so;
68 	return (0);
69 }
70 
71 sobind(so, nam)
72 	struct socket *so;
73 	struct mbuf *nam;
74 {
75 	int s = splnet();
76 	int error;
77 
78 	error =
79 	    (*so->so_proto->pr_usrreq)(so, PRU_BIND,
80 		(struct mbuf *)0, nam, (struct mbuf *)0);
81 	splx(s);
82 	return (error);
83 }
84 
85 solisten(so, backlog)
86 	register struct socket *so;
87 	int backlog;
88 {
89 	int s = splnet(), error;
90 
91 	error =
92 	    (*so->so_proto->pr_usrreq)(so, PRU_LISTEN,
93 		(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
94 	if (error) {
95 		splx(s);
96 		return (error);
97 	}
98 	if (so->so_q == 0)
99 		so->so_options |= SO_ACCEPTCONN;
100 	if (backlog < 0)
101 		backlog = 0;
102 	so->so_qlimit = min(backlog, SOMAXCONN);
103 	splx(s);
104 	return (0);
105 }
106 
107 sofree(so)
108 	register struct socket *so;
109 {
110 
111 	if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
112 		return;
113 	if (so->so_head) {
114 		if (!soqremque(so, 0) && !soqremque(so, 1))
115 			panic("sofree dq");
116 		so->so_head = 0;
117 	}
118 	sbrelease(&so->so_snd);
119 	sorflush(so);
120 	FREE(so, M_SOCKET);
121 }
122 
123 /*
124  * Close a socket on last file table reference removal.
125  * Initiate disconnect if connected.
126  * Free socket when disconnect complete.
127  */
128 soclose(so)
129 	register struct socket *so;
130 {
131 	int s = splnet();		/* conservative */
132 	int error = 0;
133 
134 	if (so->so_options & SO_ACCEPTCONN) {
135 		while (so->so_q0)
136 			(void) soabort(so->so_q0);
137 		while (so->so_q)
138 			(void) soabort(so->so_q);
139 	}
140 	if (so->so_pcb == 0)
141 		goto discard;
142 	if (so->so_state & SS_ISCONNECTED) {
143 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
144 			error = sodisconnect(so);
145 			if (error)
146 				goto drop;
147 		}
148 		if (so->so_options & SO_LINGER) {
149 			if ((so->so_state & SS_ISDISCONNECTING) &&
150 			    (so->so_state & SS_NBIO))
151 				goto drop;
152 			while (so->so_state & SS_ISCONNECTED)
153 				if (error = tsleep((caddr_t)&so->so_timeo,
154 				    PSOCK | PCATCH, netcls, so->so_linger))
155 					break;
156 		}
157 	}
158 drop:
159 	if (so->so_pcb) {
160 		int error2 =
161 		    (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
162 			(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
163 		if (error == 0)
164 			error = error2;
165 	}
166 discard:
167 	if (so->so_state & SS_NOFDREF)
168 		panic("soclose: NOFDREF");
169 	so->so_state |= SS_NOFDREF;
170 	sofree(so);
171 	splx(s);
172 	return (error);
173 }
174 
175 /*
176  * Must be called at splnet...
177  */
178 soabort(so)
179 	struct socket *so;
180 {
181 
182 	return (
183 	    (*so->so_proto->pr_usrreq)(so, PRU_ABORT,
184 		(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0));
185 }
186 
187 soaccept(so, nam)
188 	register struct socket *so;
189 	struct mbuf *nam;
190 {
191 	int s = splnet();
192 	int error;
193 
194 	if ((so->so_state & SS_NOFDREF) == 0)
195 		panic("soaccept: !NOFDREF");
196 	so->so_state &= ~SS_NOFDREF;
197 	error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
198 	    (struct mbuf *)0, nam, (struct mbuf *)0);
199 	splx(s);
200 	return (error);
201 }
202 
203 soconnect(so, nam)
204 	register struct socket *so;
205 	struct mbuf *nam;
206 {
207 	int s;
208 	int error;
209 
210 	if (so->so_options & SO_ACCEPTCONN)
211 		return (EOPNOTSUPP);
212 	s = splnet();
213 	/*
214 	 * If protocol is connection-based, can only connect once.
215 	 * Otherwise, if connected, try to disconnect first.
216 	 * This allows user to disconnect by connecting to, e.g.,
217 	 * a null address.
218 	 */
219 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
220 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
221 	    (error = sodisconnect(so))))
222 		error = EISCONN;
223 	else
224 		error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
225 		    (struct mbuf *)0, nam, (struct mbuf *)0);
226 	splx(s);
227 	return (error);
228 }
229 
230 soconnect2(so1, so2)
231 	register struct socket *so1;
232 	struct socket *so2;
233 {
234 	int s = splnet();
235 	int error;
236 
237 	error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
238 	    (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0);
239 	splx(s);
240 	return (error);
241 }
242 
243 sodisconnect(so)
244 	register struct socket *so;
245 {
246 	int s = splnet();
247 	int error;
248 
249 	if ((so->so_state & SS_ISCONNECTED) == 0) {
250 		error = ENOTCONN;
251 		goto bad;
252 	}
253 	if (so->so_state & SS_ISDISCONNECTING) {
254 		error = EALREADY;
255 		goto bad;
256 	}
257 	error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
258 	    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
259 bad:
260 	splx(s);
261 	return (error);
262 }
263 
264 /*
265  * Send on a socket.
266  * If send must go all at once and message is larger than
267  * send buffering, then hard error.
268  * Lock against other senders.
269  * If must go all at once and not enough room now, then
270  * inform user that this would block and do nothing.
271  * Otherwise, if nonblocking, send as much as possible.
272  * The data to be sent is described by "uio" if nonzero,
273  * otherwise by the mbuf chain "top" (which must be null
274  * if uio is not).  Data provided in mbuf chain must be small
275  * enough to send all at once.
276  *
277  * Returns nonzero on error, timeout or signal; callers
278  * must check for short counts if EINTR/ERESTART are returned.
279  * Data and control buffers are freed on return.
280  */
281 sosend(so, addr, uio, top, control, flags)
282 	register struct socket *so;
283 	struct mbuf *addr;
284 	struct uio *uio;
285 	struct mbuf *top;
286 	struct mbuf *control;
287 	int flags;
288 {
289 	struct mbuf **mp;
290 	register struct mbuf *m;
291 	register long space, len, resid;
292 	int clen = 0, error, s, dontroute, mlen;
293 	int atomic = sosendallatonce(so) || top;
294 
295 	if (uio)
296 		resid = uio->uio_resid;
297 	else
298 		resid = top->m_pkthdr.len;
299 	dontroute =
300 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
301 	    (so->so_proto->pr_flags & PR_ATOMIC);
302 	u.u_ru.ru_msgsnd++;
303 	if (control)
304 		clen = control->m_len;
305 #define	snderr(errno)	{ error = errno; splx(s); goto release; }
306 
307 restart:
308 	if (error = sblock(&so->so_snd))
309 		goto out;
310 	do {
311 		s = splnet();
312 		if (so->so_state & SS_CANTSENDMORE)
313 			snderr(EPIPE);
314 		if (so->so_error)
315 			snderr(so->so_error);
316 		if ((so->so_state & SS_ISCONNECTED) == 0) {
317 			if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
318 				if ((so->so_state & SS_ISCONFIRMING) == 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 (space < resid + clen &&
327 		    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
328 			if (atomic && resid > so->so_snd.sb_hiwat ||
329 			    clen > so->so_snd.sb_hiwat)
330 				snderr(EMSGSIZE);
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))
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 	 * If MSG_WAITALL is set but resid is larger than the receive buffer,
503 	 * we have to do the receive in sections, and thus risk returning
504 	 * a short count if a timeout or signal occurs after we start.
505 	 */
506 	if (m == 0 || so->so_rcv.sb_cc < uio->uio_resid &&
507 	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
508 	    ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat))) {
509 #ifdef DIAGNOSTIC
510 		if (m == 0 && so->so_rcv.sb_cc)
511 			panic("receive 1");
512 #endif
513 		if (so->so_error) {
514 			error = so->so_error;
515 			so->so_error = 0;
516 			goto release;
517 		}
518 		if (so->so_state & SS_CANTRCVMORE)
519 			goto release;
520 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
521 		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
522 			error = ENOTCONN;
523 			goto release;
524 		}
525 		if (uio->uio_resid == 0)
526 			goto release;
527 		if (so->so_state & SS_NBIO) {
528 			error = EWOULDBLOCK;
529 			goto release;
530 		}
531 		sbunlock(&so->so_rcv);
532 		error = sbwait(&so->so_rcv);
533 		splx(s);
534 		if (error)
535 			return (error);
536 		goto restart;
537 	}
538 	u.u_ru.ru_msgrcv++;
539 	nextrecord = m->m_nextpkt;
540 	if (pr->pr_flags & PR_ADDR) {
541 #ifdef DIAGNOSTIC
542 		if (m->m_type != MT_SONAME)
543 			panic("receive 1a");
544 #endif
545 		if (flags & MSG_PEEK) {
546 			if (paddr)
547 				*paddr = m_copy(m, 0, m->m_len);
548 			m = m->m_next;
549 		} else {
550 			sbfree(&so->so_rcv, m);
551 			if (paddr) {
552 				*paddr = m;
553 				so->so_rcv.sb_mb = m->m_next;
554 				m->m_next = 0;
555 				m = so->so_rcv.sb_mb;
556 			} else {
557 				MFREE(m, so->so_rcv.sb_mb);
558 				m = so->so_rcv.sb_mb;
559 			}
560 		}
561 	}
562 	while (m && m->m_type == MT_CONTROL && error == 0) {
563 		if (flags & MSG_PEEK) {
564 			if (controlp)
565 				*controlp = m_copy(m, 0, m->m_len);
566 			m = m->m_next;
567 		} else {
568 			sbfree(&so->so_rcv, m);
569 			if (controlp) {
570 				if (pr->pr_domain->dom_externalize &&
571 				    mtod(m, struct cmsghdr *)->cmsg_type ==
572 				    SCM_RIGHTS)
573 				   error = (*pr->pr_domain->dom_externalize)(m);
574 				*controlp = m;
575 				so->so_rcv.sb_mb = m->m_next;
576 				m->m_next = 0;
577 				m = so->so_rcv.sb_mb;
578 			} else {
579 				MFREE(m, so->so_rcv.sb_mb);
580 				m = so->so_rcv.sb_mb;
581 			}
582 		}
583 		if (controlp)
584 			controlp = &(*controlp)->m_next;
585 	}
586 	if (m) {
587 		if ((flags & MSG_PEEK) == 0)
588 			m->m_nextpkt = nextrecord;
589 		type = m->m_type;
590 		if (type == MT_OOBDATA)
591 			flags |= MSG_OOB;
592 	}
593 	moff = 0;
594 	offset = 0;
595 	while (m && uio->uio_resid > 0 && error == 0) {
596 		if (m->m_type == MT_OOBDATA) {
597 			if (type != MT_OOBDATA)
598 				break;
599 		} else if (type == MT_OOBDATA)
600 			break;
601 #ifdef DIAGNOSTIC
602 		else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
603 			panic("receive 3");
604 #endif
605 		so->so_state &= ~SS_RCVATMARK;
606 		len = uio->uio_resid;
607 		if (so->so_oobmark && len > so->so_oobmark - offset)
608 			len = so->so_oobmark - offset;
609 		if (len > m->m_len - moff)
610 			len = m->m_len - moff;
611 		/*
612 		 * If mp is set, just pass back the mbufs.
613 		 * Otherwise copy them out via the uio, then free.
614 		 * Sockbuf must be consistent here (points to current mbuf,
615 		 * it points to next record) when we drop priority;
616 		 * we must note any additions to the sockbuf when we
617 		 * block interrupts again.
618 		 */
619 		if (mp == 0) {
620 			splx(s);
621 			error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
622 			s = splnet();
623 		} else
624 			uio->uio_resid -= len;
625 		if (len == m->m_len - moff) {
626 			if (m->m_flags & M_EOR)
627 				flags |= MSG_EOR;
628 			if (flags & MSG_PEEK) {
629 				m = m->m_next;
630 				moff = 0;
631 			} else {
632 				nextrecord = m->m_nextpkt;
633 				sbfree(&so->so_rcv, m);
634 				if (mp) {
635 					*mp = m;
636 					mp = &m->m_next;
637 					so->so_rcv.sb_mb = m = m->m_next;
638 					*mp = (struct mbuf *)0;
639 				} else {
640 					MFREE(m, so->so_rcv.sb_mb);
641 					m = so->so_rcv.sb_mb;
642 				}
643 				if (m)
644 					m->m_nextpkt = nextrecord;
645 			}
646 		} else {
647 			if (flags & MSG_PEEK)
648 				moff += len;
649 			else {
650 				if (mp)
651 					*mp = m_copym(m, 0, len, M_WAIT);
652 				m->m_data += len;
653 				m->m_len -= len;
654 				so->so_rcv.sb_cc -= len;
655 			}
656 		}
657 		if (so->so_oobmark) {
658 			if ((flags & MSG_PEEK) == 0) {
659 				so->so_oobmark -= len;
660 				if (so->so_oobmark == 0) {
661 					so->so_state |= SS_RCVATMARK;
662 					break;
663 				}
664 			} else
665 				offset += len;
666 		}
667 		if (flags & MSG_EOR)
668 			break;
669 		/*
670 		 * If the MSG_WAITALL flag is set (for non-atomic socket),
671 		 * we must not quit until "uio->uio_resid == 0" or an error
672 		 * termination.  If a signal/timeout occurs, return
673 		 * with a short count but without error.
674 		 * Keep sockbuf locked against other readers.
675 		 */
676 		while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
677 		    !sosendallatonce(so)) {
678 			error = sbwait(&so->so_rcv);
679 			if (error) {
680 				sbunlock(&so->so_rcv);
681 				splx(s);
682 				return (0);
683 			}
684 			if (m = so->so_rcv.sb_mb)
685 				nextrecord = m->m_nextpkt;
686 			if (so->so_error || so->so_state & SS_CANTRCVMORE)
687 				break;
688 			continue;
689 		}
690 	}
691 	if ((flags & MSG_PEEK) == 0) {
692 		if (m == 0)
693 			so->so_rcv.sb_mb = nextrecord;
694 		else if (pr->pr_flags & PR_ATOMIC) {
695 			flags |= MSG_TRUNC;
696 			(void) sbdroprecord(&so->so_rcv);
697 		}
698 		if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
699 			(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
700 			    (struct mbuf *)flags, (struct mbuf *)0,
701 			    (struct mbuf *)0);
702 	}
703 	if (flagsp)
704 		*flagsp |= flags;
705 release:
706 	sbunlock(&so->so_rcv);
707 	splx(s);
708 	return (error);
709 }
710 
711 soshutdown(so, how)
712 	register struct socket *so;
713 	register int how;
714 {
715 	register struct protosw *pr = so->so_proto;
716 
717 	how++;
718 	if (how & FREAD)
719 		sorflush(so);
720 	if (how & FWRITE)
721 		return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN,
722 		    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0));
723 	return (0);
724 }
725 
726 sorflush(so)
727 	register struct socket *so;
728 {
729 	register struct sockbuf *sb = &so->so_rcv;
730 	register struct protosw *pr = so->so_proto;
731 	register int s;
732 	struct sockbuf asb;
733 
734 	sb->sb_flags |= SB_NOINTR;
735 	(void) sblock(sb);
736 	s = splimp();
737 	socantrcvmore(so);
738 	sbunlock(sb);
739 	asb = *sb;
740 	bzero((caddr_t)sb, sizeof (*sb));
741 	splx(s);
742 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
743 		(*pr->pr_domain->dom_dispose)(asb.sb_mb);
744 	sbrelease(&asb);
745 }
746 
747 sosetopt(so, level, optname, m0)
748 	register struct socket *so;
749 	int level, optname;
750 	struct mbuf *m0;
751 {
752 	int error = 0;
753 	register struct mbuf *m = m0;
754 
755 	if (level != SOL_SOCKET) {
756 		if (so->so_proto && so->so_proto->pr_ctloutput)
757 			return ((*so->so_proto->pr_ctloutput)
758 				  (PRCO_SETOPT, so, level, optname, &m0));
759 		error = ENOPROTOOPT;
760 	} else {
761 		switch (optname) {
762 
763 		case SO_LINGER:
764 			if (m == NULL || m->m_len != sizeof (struct linger)) {
765 				error = EINVAL;
766 				goto bad;
767 			}
768 			so->so_linger = mtod(m, struct linger *)->l_linger;
769 			/* fall thru... */
770 
771 		case SO_DEBUG:
772 		case SO_KEEPALIVE:
773 		case SO_DONTROUTE:
774 		case SO_USELOOPBACK:
775 		case SO_BROADCAST:
776 		case SO_REUSEADDR:
777 		case SO_OOBINLINE:
778 			if (m == NULL || m->m_len < sizeof (int)) {
779 				error = EINVAL;
780 				goto bad;
781 			}
782 			if (*mtod(m, int *))
783 				so->so_options |= optname;
784 			else
785 				so->so_options &= ~optname;
786 			break;
787 
788 		case SO_SNDBUF:
789 		case SO_RCVBUF:
790 		case SO_SNDLOWAT:
791 		case SO_RCVLOWAT:
792 			if (m == NULL || m->m_len < sizeof (int)) {
793 				error = EINVAL;
794 				goto bad;
795 			}
796 			switch (optname) {
797 
798 			case SO_SNDBUF:
799 			case SO_RCVBUF:
800 				if (sbreserve(optname == SO_SNDBUF ?
801 				    &so->so_snd : &so->so_rcv,
802 				    (u_long) *mtod(m, int *)) == 0) {
803 					error = ENOBUFS;
804 					goto bad;
805 				}
806 				break;
807 
808 			case SO_SNDLOWAT:
809 				so->so_snd.sb_lowat = *mtod(m, int *);
810 				break;
811 			case SO_RCVLOWAT:
812 				so->so_rcv.sb_lowat = *mtod(m, int *);
813 				break;
814 			}
815 			break;
816 
817 		case SO_SNDTIMEO:
818 		case SO_RCVTIMEO:
819 		    {
820 			struct timeval *tv;
821 			short val;
822 
823 			if (m == NULL || m->m_len < sizeof (*tv)) {
824 				error = EINVAL;
825 				goto bad;
826 			}
827 			tv = mtod(m, struct timeval *);
828 			if (tv->tv_sec > SHRT_MAX / hz - hz) {
829 				error = EDOM;
830 				goto bad;
831 			}
832 			val = tv->tv_sec * hz + tv->tv_usec / tick;
833 
834 			switch (optname) {
835 
836 			case SO_SNDTIMEO:
837 				so->so_snd.sb_timeo = val;
838 				break;
839 			case SO_RCVTIMEO:
840 				so->so_rcv.sb_timeo = val;
841 				break;
842 			}
843 			break;
844 		    }
845 
846 		default:
847 			error = ENOPROTOOPT;
848 			break;
849 		}
850 	}
851 bad:
852 	if (m)
853 		(void) m_free(m);
854 	return (error);
855 }
856 
857 sogetopt(so, level, optname, mp)
858 	register struct socket *so;
859 	int level, optname;
860 	struct mbuf **mp;
861 {
862 	register struct mbuf *m;
863 
864 	if (level != SOL_SOCKET) {
865 		if (so->so_proto && so->so_proto->pr_ctloutput) {
866 			return ((*so->so_proto->pr_ctloutput)
867 				  (PRCO_GETOPT, so, level, optname, mp));
868 		} else
869 			return (ENOPROTOOPT);
870 	} else {
871 		m = m_get(M_WAIT, MT_SOOPTS);
872 		m->m_len = sizeof (int);
873 
874 		switch (optname) {
875 
876 		case SO_LINGER:
877 			m->m_len = sizeof (struct linger);
878 			mtod(m, struct linger *)->l_onoff =
879 				so->so_options & SO_LINGER;
880 			mtod(m, struct linger *)->l_linger = so->so_linger;
881 			break;
882 
883 		case SO_USELOOPBACK:
884 		case SO_DONTROUTE:
885 		case SO_DEBUG:
886 		case SO_KEEPALIVE:
887 		case SO_REUSEADDR:
888 		case SO_BROADCAST:
889 		case SO_OOBINLINE:
890 			*mtod(m, int *) = so->so_options & optname;
891 			break;
892 
893 		case SO_TYPE:
894 			*mtod(m, int *) = so->so_type;
895 			break;
896 
897 		case SO_ERROR:
898 			*mtod(m, int *) = so->so_error;
899 			so->so_error = 0;
900 			break;
901 
902 		case SO_SNDBUF:
903 			*mtod(m, int *) = so->so_snd.sb_hiwat;
904 			break;
905 
906 		case SO_RCVBUF:
907 			*mtod(m, int *) = so->so_rcv.sb_hiwat;
908 			break;
909 
910 		case SO_SNDLOWAT:
911 			*mtod(m, int *) = so->so_snd.sb_lowat;
912 			break;
913 
914 		case SO_RCVLOWAT:
915 			*mtod(m, int *) = so->so_rcv.sb_lowat;
916 			break;
917 
918 		case SO_SNDTIMEO:
919 		case SO_RCVTIMEO:
920 		    {
921 			int val = (optname == SO_SNDTIMEO ?
922 			     so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
923 
924 			m->m_len = sizeof(struct timeval);
925 			mtod(m, struct timeval *)->tv_sec = val / hz;
926 			mtod(m, struct timeval *)->tv_usec =
927 			    (val % hz) / tick;
928 			break;
929 		    }
930 
931 		default:
932 			(void)m_free(m);
933 			return (ENOPROTOOPT);
934 		}
935 		*mp = m;
936 		return (0);
937 	}
938 }
939 
940 sohasoutofband(so)
941 	register struct socket *so;
942 {
943 	struct proc *p;
944 
945 	if (so->so_pgid < 0)
946 		gsignal(-so->so_pgid, SIGURG);
947 	else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
948 		psignal(p, SIGURG);
949 	if (so->so_rcv.sb_sel) {
950 		selwakeup(so->so_rcv.sb_sel, so->so_rcv.sb_flags & SB_COLL);
951 		so->so_rcv.sb_sel = 0;
952 		so->so_rcv.sb_flags &= ~SB_COLL;
953 	}
954 }
955