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