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