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