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