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