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