Home | History | Annotate | Line # | Download | only in kern
uipc_socket.c revision 1.44.2.1
      1 /*	$NetBSD: uipc_socket.c,v 1.44.2.1 1999/06/18 17:14:35 perry 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 			error = so->so_error;
    409 			so->so_error = 0;
    410 			splx(s);
    411 			goto release;
    412 		}
    413 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    414 			if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    415 				if ((so->so_state & SS_ISCONFIRMING) == 0 &&
    416 				    !(resid == 0 && clen != 0))
    417 					snderr(ENOTCONN);
    418 			} else if (addr == 0)
    419 				snderr(EDESTADDRREQ);
    420 		}
    421 		space = sbspace(&so->so_snd);
    422 		if (flags & MSG_OOB)
    423 			space += 1024;
    424 		if ((atomic && resid > so->so_snd.sb_hiwat) ||
    425 		    clen > so->so_snd.sb_hiwat)
    426 			snderr(EMSGSIZE);
    427 		if (space < resid + clen && uio &&
    428 		    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
    429 			if (so->so_state & SS_NBIO)
    430 				snderr(EWOULDBLOCK);
    431 			sbunlock(&so->so_snd);
    432 			error = sbwait(&so->so_snd);
    433 			splx(s);
    434 			if (error)
    435 				goto out;
    436 			goto restart;
    437 		}
    438 		splx(s);
    439 		mp = &top;
    440 		space -= clen;
    441 		do {
    442 		    if (uio == NULL) {
    443 			/*
    444 			 * Data is prepackaged in "top".
    445 			 */
    446 			resid = 0;
    447 			if (flags & MSG_EOR)
    448 				top->m_flags |= M_EOR;
    449 		    } else do {
    450 			if (top == 0) {
    451 				MGETHDR(m, M_WAIT, MT_DATA);
    452 				mlen = MHLEN;
    453 				m->m_pkthdr.len = 0;
    454 				m->m_pkthdr.rcvif = (struct ifnet *)0;
    455 			} else {
    456 				MGET(m, M_WAIT, MT_DATA);
    457 				mlen = MLEN;
    458 			}
    459 			if (resid >= MINCLSIZE && space >= MCLBYTES) {
    460 				MCLGET(m, M_WAIT);
    461 				if ((m->m_flags & M_EXT) == 0)
    462 					goto nopages;
    463 				mlen = MCLBYTES;
    464 #ifdef	MAPPED_MBUFS
    465 				len = min(MCLBYTES, resid);
    466 #else
    467 				if (atomic && top == 0) {
    468 					len = min(MCLBYTES - max_hdr, resid);
    469 					m->m_data += max_hdr;
    470 				} else
    471 					len = min(MCLBYTES, resid);
    472 #endif
    473 				space -= len;
    474 			} else {
    475 nopages:
    476 				len = min(min(mlen, resid), space);
    477 				space -= len;
    478 				/*
    479 				 * For datagram protocols, leave room
    480 				 * for protocol headers in first mbuf.
    481 				 */
    482 				if (atomic && top == 0 && len < mlen)
    483 					MH_ALIGN(m, len);
    484 			}
    485 			error = uiomove(mtod(m, caddr_t), (int)len, uio);
    486 			resid = uio->uio_resid;
    487 			m->m_len = len;
    488 			*mp = m;
    489 			top->m_pkthdr.len += len;
    490 			if (error)
    491 				goto release;
    492 			mp = &m->m_next;
    493 			if (resid <= 0) {
    494 				if (flags & MSG_EOR)
    495 					top->m_flags |= M_EOR;
    496 				break;
    497 			}
    498 		    } while (space > 0 && atomic);
    499 		    if (dontroute)
    500 			    so->so_options |= SO_DONTROUTE;
    501 		    if (resid > 0)
    502 			    so->so_state |= SS_MORETOCOME;
    503 		    s = splsoftnet();				/* XXX */
    504 		    error = (*so->so_proto->pr_usrreq)(so,
    505 			(flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
    506 			top, addr, control, p);
    507 		    splx(s);
    508 		    if (dontroute)
    509 			    so->so_options &= ~SO_DONTROUTE;
    510 		    if (resid > 0)
    511 			    so->so_state &= ~SS_MORETOCOME;
    512 		    clen = 0;
    513 		    control = 0;
    514 		    top = 0;
    515 		    mp = &top;
    516 		    if (error)
    517 			goto release;
    518 		} while (resid && space > 0);
    519 	} while (resid);
    520 
    521 release:
    522 	sbunlock(&so->so_snd);
    523 out:
    524 	if (top)
    525 		m_freem(top);
    526 	if (control)
    527 		m_freem(control);
    528 	return (error);
    529 }
    530 
    531 /*
    532  * Implement receive operations on a socket.
    533  * We depend on the way that records are added to the sockbuf
    534  * by sbappend*.  In particular, each record (mbufs linked through m_next)
    535  * must begin with an address if the protocol so specifies,
    536  * followed by an optional mbuf or mbufs containing ancillary data,
    537  * and then zero or more mbufs of data.
    538  * In order to avoid blocking network interrupts for the entire time here,
    539  * we splx() while doing the actual copy to user space.
    540  * Although the sockbuf is locked, new data may still be appended,
    541  * and thus we must maintain consistency of the sockbuf during that time.
    542  *
    543  * The caller may receive the data as a single mbuf chain by supplying
    544  * an mbuf **mp0 for use in returning the chain.  The uio is then used
    545  * only for the count in uio_resid.
    546  */
    547 int
    548 soreceive(so, paddr, uio, mp0, controlp, flagsp)
    549 	register struct socket *so;
    550 	struct mbuf **paddr;
    551 	struct uio *uio;
    552 	struct mbuf **mp0;
    553 	struct mbuf **controlp;
    554 	int *flagsp;
    555 {
    556 	register struct mbuf *m, **mp;
    557 	register int flags, len, error, s, offset;
    558 	struct protosw *pr = so->so_proto;
    559 	struct mbuf *nextrecord;
    560 	int moff, type = 0;
    561 	int orig_resid = uio->uio_resid;
    562 
    563 	mp = mp0;
    564 	if (paddr)
    565 		*paddr = 0;
    566 	if (controlp)
    567 		*controlp = 0;
    568 	if (flagsp)
    569 		flags = *flagsp &~ MSG_EOR;
    570 	else
    571 		flags = 0;
    572 	if (flags & MSG_OOB) {
    573 		m = m_get(M_WAIT, MT_DATA);
    574 		error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
    575 		    (struct mbuf *)(long)(flags & MSG_PEEK), (struct mbuf *)0,
    576 		    (struct proc *)0);
    577 		if (error)
    578 			goto bad;
    579 		do {
    580 			error = uiomove(mtod(m, caddr_t),
    581 			    (int) min(uio->uio_resid, m->m_len), uio);
    582 			m = m_free(m);
    583 		} while (uio->uio_resid && error == 0 && m);
    584 bad:
    585 		if (m)
    586 			m_freem(m);
    587 		return (error);
    588 	}
    589 	if (mp)
    590 		*mp = (struct mbuf *)0;
    591 	if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
    592 		(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
    593 		    (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
    594 
    595 restart:
    596 	if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
    597 		return (error);
    598 	s = splsoftnet();
    599 
    600 	m = so->so_rcv.sb_mb;
    601 	/*
    602 	 * If we have less data than requested, block awaiting more
    603 	 * (subject to any timeout) if:
    604 	 *   1. the current count is less than the low water mark,
    605 	 *   2. MSG_WAITALL is set, and it is possible to do the entire
    606 	 *	receive operation at once if we block (resid <= hiwat), or
    607 	 *   3. MSG_DONTWAIT is not set.
    608 	 * If MSG_WAITALL is set but resid is larger than the receive buffer,
    609 	 * we have to do the receive in sections, and thus risk returning
    610 	 * a short count if a timeout or signal occurs after we start.
    611 	 */
    612 	if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
    613 	    so->so_rcv.sb_cc < uio->uio_resid) &&
    614 	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
    615 	    ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
    616 	    m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
    617 #ifdef DIAGNOSTIC
    618 		if (m == 0 && so->so_rcv.sb_cc)
    619 			panic("receive 1");
    620 #endif
    621 		if (so->so_error) {
    622 			if (m)
    623 				goto dontblock;
    624 			error = so->so_error;
    625 			if ((flags & MSG_PEEK) == 0)
    626 				so->so_error = 0;
    627 			goto release;
    628 		}
    629 		if (so->so_state & SS_CANTRCVMORE) {
    630 			if (m)
    631 				goto dontblock;
    632 			else
    633 				goto release;
    634 		}
    635 		for (; m; m = m->m_next)
    636 			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
    637 				m = so->so_rcv.sb_mb;
    638 				goto dontblock;
    639 			}
    640 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
    641 		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
    642 			error = ENOTCONN;
    643 			goto release;
    644 		}
    645 		if (uio->uio_resid == 0)
    646 			goto release;
    647 		if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
    648 			error = EWOULDBLOCK;
    649 			goto release;
    650 		}
    651 		sbunlock(&so->so_rcv);
    652 		error = sbwait(&so->so_rcv);
    653 		splx(s);
    654 		if (error)
    655 			return (error);
    656 		goto restart;
    657 	}
    658 dontblock:
    659 #ifdef notyet /* XXXX */
    660 	if (uio->uio_procp)
    661 		uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
    662 #endif
    663 	nextrecord = m->m_nextpkt;
    664 	if (pr->pr_flags & PR_ADDR) {
    665 #ifdef DIAGNOSTIC
    666 		if (m->m_type != MT_SONAME)
    667 			panic("receive 1a");
    668 #endif
    669 		orig_resid = 0;
    670 		if (flags & MSG_PEEK) {
    671 			if (paddr)
    672 				*paddr = m_copy(m, 0, m->m_len);
    673 			m = m->m_next;
    674 		} else {
    675 			sbfree(&so->so_rcv, m);
    676 			if (paddr) {
    677 				*paddr = m;
    678 				so->so_rcv.sb_mb = m->m_next;
    679 				m->m_next = 0;
    680 				m = so->so_rcv.sb_mb;
    681 			} else {
    682 				MFREE(m, so->so_rcv.sb_mb);
    683 				m = so->so_rcv.sb_mb;
    684 			}
    685 		}
    686 	}
    687 	while (m && m->m_type == MT_CONTROL && error == 0) {
    688 		if (flags & MSG_PEEK) {
    689 			if (controlp)
    690 				*controlp = m_copy(m, 0, m->m_len);
    691 			m = m->m_next;
    692 		} else {
    693 			sbfree(&so->so_rcv, m);
    694 			if (controlp) {
    695 				if (pr->pr_domain->dom_externalize &&
    696 				    mtod(m, struct cmsghdr *)->cmsg_type ==
    697 				    SCM_RIGHTS)
    698 				   error = (*pr->pr_domain->dom_externalize)(m);
    699 				*controlp = m;
    700 				so->so_rcv.sb_mb = m->m_next;
    701 				m->m_next = 0;
    702 				m = so->so_rcv.sb_mb;
    703 			} else {
    704 				MFREE(m, so->so_rcv.sb_mb);
    705 				m = so->so_rcv.sb_mb;
    706 			}
    707 		}
    708 		if (controlp) {
    709 			orig_resid = 0;
    710 			controlp = &(*controlp)->m_next;
    711 		}
    712 	}
    713 	if (m) {
    714 		if ((flags & MSG_PEEK) == 0)
    715 			m->m_nextpkt = nextrecord;
    716 		type = m->m_type;
    717 		if (type == MT_OOBDATA)
    718 			flags |= MSG_OOB;
    719 	}
    720 	moff = 0;
    721 	offset = 0;
    722 	while (m && uio->uio_resid > 0 && error == 0) {
    723 		if (m->m_type == MT_OOBDATA) {
    724 			if (type != MT_OOBDATA)
    725 				break;
    726 		} else if (type == MT_OOBDATA)
    727 			break;
    728 #ifdef DIAGNOSTIC
    729 		else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
    730 			panic("receive 3");
    731 #endif
    732 		so->so_state &= ~SS_RCVATMARK;
    733 		len = uio->uio_resid;
    734 		if (so->so_oobmark && len > so->so_oobmark - offset)
    735 			len = so->so_oobmark - offset;
    736 		if (len > m->m_len - moff)
    737 			len = m->m_len - moff;
    738 		/*
    739 		 * If mp is set, just pass back the mbufs.
    740 		 * Otherwise copy them out via the uio, then free.
    741 		 * Sockbuf must be consistent here (points to current mbuf,
    742 		 * it points to next record) when we drop priority;
    743 		 * we must note any additions to the sockbuf when we
    744 		 * block interrupts again.
    745 		 */
    746 		if (mp == 0) {
    747 			splx(s);
    748 			error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
    749 			s = splsoftnet();
    750 		} else
    751 			uio->uio_resid -= len;
    752 		if (len == m->m_len - moff) {
    753 			if (m->m_flags & M_EOR)
    754 				flags |= MSG_EOR;
    755 			if (flags & MSG_PEEK) {
    756 				m = m->m_next;
    757 				moff = 0;
    758 			} else {
    759 				nextrecord = m->m_nextpkt;
    760 				sbfree(&so->so_rcv, m);
    761 				if (mp) {
    762 					*mp = m;
    763 					mp = &m->m_next;
    764 					so->so_rcv.sb_mb = m = m->m_next;
    765 					*mp = (struct mbuf *)0;
    766 				} else {
    767 					MFREE(m, so->so_rcv.sb_mb);
    768 					m = so->so_rcv.sb_mb;
    769 				}
    770 				if (m)
    771 					m->m_nextpkt = nextrecord;
    772 			}
    773 		} else {
    774 			if (flags & MSG_PEEK)
    775 				moff += len;
    776 			else {
    777 				if (mp)
    778 					*mp = m_copym(m, 0, len, M_WAIT);
    779 				m->m_data += len;
    780 				m->m_len -= len;
    781 				so->so_rcv.sb_cc -= len;
    782 			}
    783 		}
    784 		if (so->so_oobmark) {
    785 			if ((flags & MSG_PEEK) == 0) {
    786 				so->so_oobmark -= len;
    787 				if (so->so_oobmark == 0) {
    788 					so->so_state |= SS_RCVATMARK;
    789 					break;
    790 				}
    791 			} else {
    792 				offset += len;
    793 				if (offset == so->so_oobmark)
    794 					break;
    795 			}
    796 		}
    797 		if (flags & MSG_EOR)
    798 			break;
    799 		/*
    800 		 * If the MSG_WAITALL flag is set (for non-atomic socket),
    801 		 * we must not quit until "uio->uio_resid == 0" or an error
    802 		 * termination.  If a signal/timeout occurs, return
    803 		 * with a short count but without error.
    804 		 * Keep sockbuf locked against other readers.
    805 		 */
    806 		while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
    807 		    !sosendallatonce(so) && !nextrecord) {
    808 			if (so->so_error || so->so_state & SS_CANTRCVMORE)
    809 				break;
    810 			error = sbwait(&so->so_rcv);
    811 			if (error) {
    812 				sbunlock(&so->so_rcv);
    813 				splx(s);
    814 				return (0);
    815 			}
    816 			if ((m = so->so_rcv.sb_mb) != NULL)
    817 				nextrecord = m->m_nextpkt;
    818 		}
    819 	}
    820 
    821 	if (m && pr->pr_flags & PR_ATOMIC) {
    822 		flags |= MSG_TRUNC;
    823 		if ((flags & MSG_PEEK) == 0)
    824 			(void) sbdroprecord(&so->so_rcv);
    825 	}
    826 	if ((flags & MSG_PEEK) == 0) {
    827 		if (m == 0)
    828 			so->so_rcv.sb_mb = nextrecord;
    829 		if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
    830 			(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
    831 			    (struct mbuf *)(long)flags, (struct mbuf *)0,
    832 			    (struct proc *)0);
    833 	}
    834 	if (orig_resid == uio->uio_resid && orig_resid &&
    835 	    (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
    836 		sbunlock(&so->so_rcv);
    837 		splx(s);
    838 		goto restart;
    839 	}
    840 
    841 	if (flagsp)
    842 		*flagsp |= flags;
    843 release:
    844 	sbunlock(&so->so_rcv);
    845 	splx(s);
    846 	return (error);
    847 }
    848 
    849 int
    850 soshutdown(so, how)
    851 	struct socket *so;
    852 	int how;
    853 {
    854 	struct protosw *pr = so->so_proto;
    855 
    856 	if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
    857 		return (EINVAL);
    858 
    859 	if (how == SHUT_RD || how == SHUT_RDWR)
    860 		sorflush(so);
    861 	if (how == SHUT_WR || how == SHUT_RDWR)
    862 		return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0,
    863 		    (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
    864 	return (0);
    865 }
    866 
    867 void
    868 sorflush(so)
    869 	register struct socket *so;
    870 {
    871 	register struct sockbuf *sb = &so->so_rcv;
    872 	register struct protosw *pr = so->so_proto;
    873 	register int s;
    874 	struct sockbuf asb;
    875 
    876 	sb->sb_flags |= SB_NOINTR;
    877 	(void) sblock(sb, M_WAITOK);
    878 	s = splimp();
    879 	socantrcvmore(so);
    880 	sbunlock(sb);
    881 	asb = *sb;
    882 	memset((caddr_t)sb, 0, sizeof(*sb));
    883 	splx(s);
    884 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
    885 		(*pr->pr_domain->dom_dispose)(asb.sb_mb);
    886 	sbrelease(&asb);
    887 }
    888 
    889 int
    890 sosetopt(so, level, optname, m0)
    891 	register struct socket *so;
    892 	int level, optname;
    893 	struct mbuf *m0;
    894 {
    895 	int error = 0;
    896 	register struct mbuf *m = m0;
    897 
    898 	if (level != SOL_SOCKET) {
    899 		if (so->so_proto && so->so_proto->pr_ctloutput)
    900 			return ((*so->so_proto->pr_ctloutput)
    901 				  (PRCO_SETOPT, so, level, optname, &m0));
    902 		error = ENOPROTOOPT;
    903 	} else {
    904 		switch (optname) {
    905 
    906 		case SO_LINGER:
    907 			if (m == NULL || m->m_len != sizeof(struct linger)) {
    908 				error = EINVAL;
    909 				goto bad;
    910 			}
    911 			so->so_linger = mtod(m, struct linger *)->l_linger;
    912 			/* fall thru... */
    913 
    914 		case SO_DEBUG:
    915 		case SO_KEEPALIVE:
    916 		case SO_DONTROUTE:
    917 		case SO_USELOOPBACK:
    918 		case SO_BROADCAST:
    919 		case SO_REUSEADDR:
    920 		case SO_REUSEPORT:
    921 		case SO_OOBINLINE:
    922 		case SO_TIMESTAMP:
    923 			if (m == NULL || m->m_len < sizeof(int)) {
    924 				error = EINVAL;
    925 				goto bad;
    926 			}
    927 			if (*mtod(m, int *))
    928 				so->so_options |= optname;
    929 			else
    930 				so->so_options &= ~optname;
    931 			break;
    932 
    933 		case SO_SNDBUF:
    934 		case SO_RCVBUF:
    935 		case SO_SNDLOWAT:
    936 		case SO_RCVLOWAT:
    937 		    {
    938 			int optval;
    939 
    940 			if (m == NULL || m->m_len < sizeof(int)) {
    941 				error = EINVAL;
    942 				goto bad;
    943 			}
    944 
    945 			/*
    946 			 * Values < 1 make no sense for any of these
    947 			 * options, so disallow them.
    948 			 */
    949 			optval = *mtod(m, int *);
    950 			if (optval < 1) {
    951 				error = EINVAL;
    952 				goto bad;
    953 			}
    954 
    955 			switch (optname) {
    956 
    957 			case SO_SNDBUF:
    958 			case SO_RCVBUF:
    959 				if (sbreserve(optname == SO_SNDBUF ?
    960 				    &so->so_snd : &so->so_rcv,
    961 				    (u_long) optval) == 0) {
    962 					error = ENOBUFS;
    963 					goto bad;
    964 				}
    965 				break;
    966 
    967 			/*
    968 			 * Make sure the low-water is never greater than
    969 			 * the high-water.
    970 			 */
    971 			case SO_SNDLOWAT:
    972 				so->so_snd.sb_lowat =
    973 				    (optval > so->so_snd.sb_hiwat) ?
    974 				    so->so_snd.sb_hiwat : optval;
    975 				break;
    976 			case SO_RCVLOWAT:
    977 				so->so_rcv.sb_lowat =
    978 				    (optval > so->so_rcv.sb_hiwat) ?
    979 				    so->so_rcv.sb_hiwat : optval;
    980 				break;
    981 			}
    982 			break;
    983 		    }
    984 
    985 		case SO_SNDTIMEO:
    986 		case SO_RCVTIMEO:
    987 		    {
    988 			struct timeval *tv;
    989 			short val;
    990 
    991 			if (m == NULL || m->m_len < sizeof(*tv)) {
    992 				error = EINVAL;
    993 				goto bad;
    994 			}
    995 			tv = mtod(m, struct timeval *);
    996 			if (tv->tv_sec * hz + tv->tv_usec / tick > SHRT_MAX) {
    997 				error = EDOM;
    998 				goto bad;
    999 			}
   1000 			val = tv->tv_sec * hz + tv->tv_usec / tick;
   1001 
   1002 			switch (optname) {
   1003 
   1004 			case SO_SNDTIMEO:
   1005 				so->so_snd.sb_timeo = val;
   1006 				break;
   1007 			case SO_RCVTIMEO:
   1008 				so->so_rcv.sb_timeo = val;
   1009 				break;
   1010 			}
   1011 			break;
   1012 		    }
   1013 
   1014 		default:
   1015 			error = ENOPROTOOPT;
   1016 			break;
   1017 		}
   1018 		if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
   1019 			(void) ((*so->so_proto->pr_ctloutput)
   1020 				  (PRCO_SETOPT, so, level, optname, &m0));
   1021 			m = NULL;	/* freed by protocol */
   1022 		}
   1023 	}
   1024 bad:
   1025 	if (m)
   1026 		(void) m_free(m);
   1027 	return (error);
   1028 }
   1029 
   1030 int
   1031 sogetopt(so, level, optname, mp)
   1032 	register struct socket *so;
   1033 	int level, optname;
   1034 	struct mbuf **mp;
   1035 {
   1036 	register struct mbuf *m;
   1037 
   1038 	if (level != SOL_SOCKET) {
   1039 		if (so->so_proto && so->so_proto->pr_ctloutput) {
   1040 			return ((*so->so_proto->pr_ctloutput)
   1041 				  (PRCO_GETOPT, so, level, optname, mp));
   1042 		} else
   1043 			return (ENOPROTOOPT);
   1044 	} else {
   1045 		m = m_get(M_WAIT, MT_SOOPTS);
   1046 		m->m_len = sizeof(int);
   1047 
   1048 		switch (optname) {
   1049 
   1050 		case SO_LINGER:
   1051 			m->m_len = sizeof(struct linger);
   1052 			mtod(m, struct linger *)->l_onoff =
   1053 				so->so_options & SO_LINGER;
   1054 			mtod(m, struct linger *)->l_linger = so->so_linger;
   1055 			break;
   1056 
   1057 		case SO_USELOOPBACK:
   1058 		case SO_DONTROUTE:
   1059 		case SO_DEBUG:
   1060 		case SO_KEEPALIVE:
   1061 		case SO_REUSEADDR:
   1062 		case SO_REUSEPORT:
   1063 		case SO_BROADCAST:
   1064 		case SO_OOBINLINE:
   1065 		case SO_TIMESTAMP:
   1066 			*mtod(m, int *) = so->so_options & optname;
   1067 			break;
   1068 
   1069 		case SO_TYPE:
   1070 			*mtod(m, int *) = so->so_type;
   1071 			break;
   1072 
   1073 		case SO_ERROR:
   1074 			*mtod(m, int *) = so->so_error;
   1075 			so->so_error = 0;
   1076 			break;
   1077 
   1078 		case SO_SNDBUF:
   1079 			*mtod(m, int *) = so->so_snd.sb_hiwat;
   1080 			break;
   1081 
   1082 		case SO_RCVBUF:
   1083 			*mtod(m, int *) = so->so_rcv.sb_hiwat;
   1084 			break;
   1085 
   1086 		case SO_SNDLOWAT:
   1087 			*mtod(m, int *) = so->so_snd.sb_lowat;
   1088 			break;
   1089 
   1090 		case SO_RCVLOWAT:
   1091 			*mtod(m, int *) = so->so_rcv.sb_lowat;
   1092 			break;
   1093 
   1094 		case SO_SNDTIMEO:
   1095 		case SO_RCVTIMEO:
   1096 		    {
   1097 			int val = (optname == SO_SNDTIMEO ?
   1098 			     so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
   1099 
   1100 			m->m_len = sizeof(struct timeval);
   1101 			mtod(m, struct timeval *)->tv_sec = val / hz;
   1102 			mtod(m, struct timeval *)->tv_usec =
   1103 			    (val % hz) * tick;
   1104 			break;
   1105 		    }
   1106 
   1107 		default:
   1108 			(void)m_free(m);
   1109 			return (ENOPROTOOPT);
   1110 		}
   1111 		*mp = m;
   1112 		return (0);
   1113 	}
   1114 }
   1115 
   1116 void
   1117 sohasoutofband(so)
   1118 	register struct socket *so;
   1119 {
   1120 	struct proc *p;
   1121 
   1122 	if (so->so_pgid < 0)
   1123 		gsignal(-so->so_pgid, SIGURG);
   1124 	else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
   1125 		psignal(p, SIGURG);
   1126 	selwakeup(&so->so_rcv.sb_sel);
   1127 }
   1128