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