File:  [DragonFly] / src / sys / kern / uipc_socket.c
Revision 1.18: download - view: text, annotated - select for diffs
Wed Apr 28 06:59:27 2004 UTC (10 years, 3 months ago) by joerg
Branches: MAIN
CVS tags: HEAD
Conditionalize accept_filter variable on defined(INET).

    1: /*
    2:  * Copyright (c) 2004 Jeffrey M. Hsu.  All rights reserved.
    3:  * Copyright (c) 1982, 1986, 1988, 1990, 1993
    4:  *	The Regents of the University of California.  All rights reserved.
    5:  *
    6:  * Redistribution and use in source and binary forms, with or without
    7:  * modification, are permitted provided that the following conditions
    8:  * are met:
    9:  * 1. Redistributions of source code must retain the above copyright
   10:  *    notice, this list of conditions and the following disclaimer.
   11:  * 2. Redistributions in binary form must reproduce the above copyright
   12:  *    notice, this list of conditions and the following disclaimer in the
   13:  *    documentation and/or other materials provided with the distribution.
   14:  * 3. All advertising materials mentioning features or use of this software
   15:  *    must display the following acknowledgement:
   16:  *	This product includes software developed by the University of
   17:  *	California, Berkeley and its contributors.
   18:  * 4. Neither the name of the University nor the names of its contributors
   19:  *    may be used to endorse or promote products derived from this software
   20:  *    without specific prior written permission.
   21:  *
   22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32:  * SUCH DAMAGE.
   33:  *
   34:  *	@(#)uipc_socket.c	8.3 (Berkeley) 4/15/94
   35:  * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.24 2003/11/11 17:18:18 silby Exp $
   36:  * $DragonFly: src/sys/kern/uipc_socket.c,v 1.18 2004/04/28 06:59:27 joerg Exp $
   37:  */
   38: 
   39: #include "opt_inet.h"
   40: 
   41: #include <sys/param.h>
   42: #include <sys/systm.h>
   43: #include <sys/fcntl.h>
   44: #include <sys/malloc.h>
   45: #include <sys/mbuf.h>
   46: #include <sys/domain.h>
   47: #include <sys/file.h>			/* for struct knote */
   48: #include <sys/kernel.h>
   49: #include <sys/malloc.h>
   50: #include <sys/event.h>
   51: #include <sys/poll.h>
   52: #include <sys/proc.h>
   53: #include <sys/protosw.h>
   54: #include <sys/socket.h>
   55: #include <sys/socketvar.h>
   56: #include <sys/socketops.h>
   57: #include <sys/resourcevar.h>
   58: #include <sys/signalvar.h>
   59: #include <sys/sysctl.h>
   60: #include <sys/uio.h>
   61: #include <sys/jail.h>
   62: #include <vm/vm_zone.h>
   63: 
   64: #include <machine/limits.h>
   65: 
   66: #ifdef INET
   67: static int	 do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
   68: #endif /* INET */
   69: 
   70: static void 	filt_sordetach(struct knote *kn);
   71: static int 	filt_soread(struct knote *kn, long hint);
   72: static void 	filt_sowdetach(struct knote *kn);
   73: static int	filt_sowrite(struct knote *kn, long hint);
   74: static int	filt_solisten(struct knote *kn, long hint);
   75: 
   76: static struct filterops solisten_filtops = 
   77: 	{ 1, NULL, filt_sordetach, filt_solisten };
   78: static struct filterops soread_filtops =
   79: 	{ 1, NULL, filt_sordetach, filt_soread };
   80: static struct filterops sowrite_filtops = 
   81: 	{ 1, NULL, filt_sowdetach, filt_sowrite };
   82: 
   83: struct	vm_zone *socket_zone;
   84: so_gen_t	so_gencnt;	/* generation count for sockets */
   85: 
   86: MALLOC_DEFINE(M_SONAME, "soname", "socket name");
   87: MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
   88: 
   89: SYSCTL_DECL(_kern_ipc);
   90: 
   91: static int somaxconn = SOMAXCONN;
   92: SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
   93:     &somaxconn, 0, "Maximum pending socket connection queue size");
   94: 
   95: /*
   96:  * Socket operation routines.
   97:  * These routines are called by the routines in
   98:  * sys_socket.c or from a system process, and
   99:  * implement the semantics of socket operations by
  100:  * switching out to the protocol specific routines.
  101:  */
  102: 
  103: /*
  104:  * Get a socket structure from our zone, and initialize it.
  105:  * We don't implement `waitok' yet (see comments in uipc_domain.c).
  106:  * Note that it would probably be better to allocate socket
  107:  * and PCB at the same time, but I'm not convinced that all
  108:  * the protocols can be easily modified to do this.
  109:  */
  110: struct socket *
  111: soalloc(waitok)
  112: 	int waitok;
  113: {
  114: 	struct socket *so;
  115: 
  116: 	so = zalloc(socket_zone);
  117: 	if (so) {
  118: 		/* XXX race condition for reentrant kernel */
  119: 		bzero(so, sizeof *so);
  120: 		so->so_gencnt = ++so_gencnt;
  121: 		TAILQ_INIT(&so->so_aiojobq);
  122: 		TAILQ_INIT(&so->so_rcv.sb_sel.si_mlist);
  123: 		TAILQ_INIT(&so->so_snd.sb_sel.si_mlist);
  124: 	}
  125: 	return so;
  126: }
  127: 
  128: int
  129: socreate(int dom, struct socket **aso, int type,
  130: 	int proto, struct thread *td)
  131: {
  132: 	struct proc *p = td->td_proc;
  133: 	struct protosw *prp;
  134: 	struct socket *so;
  135: 	struct pru_attach_info ai;
  136: 	int error;
  137: 
  138: 	if (proto)
  139: 		prp = pffindproto(dom, proto, type);
  140: 	else
  141: 		prp = pffindtype(dom, type);
  142: 
  143: 	if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
  144: 		return (EPROTONOSUPPORT);
  145: 
  146: 	if (p->p_ucred->cr_prison && jail_socket_unixiproute_only &&
  147: 	    prp->pr_domain->dom_family != PF_LOCAL &&
  148: 	    prp->pr_domain->dom_family != PF_INET &&
  149: 	    prp->pr_domain->dom_family != PF_ROUTE) {
  150: 		return (EPROTONOSUPPORT);
  151: 	}
  152: 
  153: 	if (prp->pr_type != type)
  154: 		return (EPROTOTYPE);
  155: 	so = soalloc(p != 0);
  156: 	if (so == 0)
  157: 		return (ENOBUFS);
  158: 
  159: 	TAILQ_INIT(&so->so_incomp);
  160: 	TAILQ_INIT(&so->so_comp);
  161: 	so->so_type = type;
  162: 	so->so_cred = crhold(p->p_ucred);
  163: 	so->so_proto = prp;
  164: 	ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE];
  165: 	ai.p_ucred = p->p_ucred;
  166: 	ai.fd_rdir = p->p_fd->fd_rdir;
  167: 	error = so_pru_attach(so, proto, &ai);
  168: 	if (error) {
  169: 		so->so_state |= SS_NOFDREF;
  170: 		sofree(so);
  171: 		return (error);
  172: 	}
  173: 	*aso = so;
  174: 	return (0);
  175: }
  176: 
  177: int
  178: sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
  179: {
  180: 	int s = splnet();
  181: 	int error;
  182: 
  183: 	error = so_pru_bind(so, nam, td);
  184: 	splx(s);
  185: 	return (error);
  186: }
  187: 
  188: void
  189: sodealloc(struct socket *so)
  190: {
  191: 
  192: 	so->so_gencnt = ++so_gencnt;
  193: 	if (so->so_rcv.sb_hiwat)
  194: 		(void)chgsbsize(so->so_cred->cr_uidinfo,
  195: 		    &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
  196: 	if (so->so_snd.sb_hiwat)
  197: 		(void)chgsbsize(so->so_cred->cr_uidinfo,
  198: 		    &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
  199: #ifdef INET
  200: 	if (so->so_accf != NULL) {
  201: 		if (so->so_accf->so_accept_filter != NULL && 
  202: 			so->so_accf->so_accept_filter->accf_destroy != NULL) {
  203: 			so->so_accf->so_accept_filter->accf_destroy(so);
  204: 		}
  205: 		if (so->so_accf->so_accept_filter_str != NULL)
  206: 			FREE(so->so_accf->so_accept_filter_str, M_ACCF);
  207: 		FREE(so->so_accf, M_ACCF);
  208: 	}
  209: #endif /* INET */
  210: 	crfree(so->so_cred);
  211: 	zfree(socket_zone, so);
  212: }
  213: 
  214: int
  215: solisten(struct socket *so, int backlog, struct thread *td)
  216: {
  217: 	int s, error;
  218: 
  219: 	s = splnet();
  220:  	if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) {
  221:  		splx(s);
  222:  		return (EINVAL);
  223:  	}
  224: 
  225: 	error = so_pru_listen(so, td);
  226: 	if (error) {
  227: 		splx(s);
  228: 		return (error);
  229: 	}
  230: 	if (TAILQ_EMPTY(&so->so_comp))
  231: 		so->so_options |= SO_ACCEPTCONN;
  232: 	if (backlog < 0 || backlog > somaxconn)
  233: 		backlog = somaxconn;
  234: 	so->so_qlimit = backlog;
  235: 	splx(s);
  236: 	return (0);
  237: }
  238: 
  239: void
  240: sofree(struct socket *so)
  241: {
  242: 	struct socket *head = so->so_head;
  243: 
  244: 	if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
  245: 		return;
  246: 	if (head != NULL) {
  247: 		if (so->so_state & SS_INCOMP) {
  248: 			TAILQ_REMOVE(&head->so_incomp, so, so_list);
  249: 			head->so_incqlen--;
  250: 		} else if (so->so_state & SS_COMP) {
  251: 			/*
  252: 			 * We must not decommission a socket that's
  253: 			 * on the accept(2) queue.  If we do, then
  254: 			 * accept(2) may hang after select(2) indicated
  255: 			 * that the listening socket was ready.
  256: 			 */
  257: 			return;
  258: 		} else {
  259: 			panic("sofree: not queued");
  260: 		}
  261: 		so->so_state &= ~SS_INCOMP;
  262: 		so->so_head = NULL;
  263: 	}
  264: 	sbrelease(&so->so_snd, so);
  265: 	sorflush(so);
  266: 	sodealloc(so);
  267: }
  268: 
  269: /*
  270:  * Close a socket on last file table reference removal.
  271:  * Initiate disconnect if connected.
  272:  * Free socket when disconnect complete.
  273:  */
  274: int
  275: soclose(struct socket *so)
  276: {
  277: 	int s = splnet();		/* conservative */
  278: 	int error = 0;
  279: 
  280: 	funsetown(so->so_sigio);
  281: 	if (so->so_options & SO_ACCEPTCONN) {
  282: 		struct socket *sp, *sonext;
  283: 
  284: 		sp = TAILQ_FIRST(&so->so_incomp);
  285: 		for (; sp != NULL; sp = sonext) {
  286: 			sonext = TAILQ_NEXT(sp, so_list);
  287: 			(void) soabort(sp);
  288: 		}
  289: 		for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
  290: 			sonext = TAILQ_NEXT(sp, so_list);
  291: 			/* Dequeue from so_comp since sofree() won't do it */
  292: 			TAILQ_REMOVE(&so->so_comp, sp, so_list);
  293: 			so->so_qlen--;
  294: 			sp->so_state &= ~SS_COMP;
  295: 			sp->so_head = NULL;
  296: 			(void) soabort(sp);
  297: 		}
  298: 	}
  299: 	if (so->so_pcb == 0)
  300: 		goto discard;
  301: 	if (so->so_state & SS_ISCONNECTED) {
  302: 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
  303: 			error = sodisconnect(so);
  304: 			if (error)
  305: 				goto drop;
  306: 		}
  307: 		if (so->so_options & SO_LINGER) {
  308: 			if ((so->so_state & SS_ISDISCONNECTING) &&
  309: 			    (so->so_state & SS_NBIO))
  310: 				goto drop;
  311: 			while (so->so_state & SS_ISCONNECTED) {
  312: 				error = tsleep((caddr_t)&so->so_timeo,
  313: 				    PCATCH, "soclos", so->so_linger * hz);
  314: 				if (error)
  315: 					break;
  316: 			}
  317: 		}
  318: 	}
  319: drop:
  320: 	if (so->so_pcb) {
  321: 		int error2;
  322: 
  323: 		error2 = so_pru_detach(so);
  324: 		if (error == 0)
  325: 			error = error2;
  326: 	}
  327: discard:
  328: 	if (so->so_state & SS_NOFDREF)
  329: 		panic("soclose: NOFDREF");
  330: 	so->so_state |= SS_NOFDREF;
  331: 	sofree(so);
  332: 	splx(s);
  333: 	return (error);
  334: }
  335: 
  336: /*
  337:  * Must be called at splnet...
  338:  */
  339: int
  340: soabort(so)
  341: 	struct socket *so;
  342: {
  343: 	int error;
  344: 
  345: 	error = so_pru_abort(so);
  346: 	if (error) {
  347: 		sofree(so);
  348: 		return error;
  349: 	}
  350: 	return (0);
  351: }
  352: 
  353: int
  354: soaccept(struct socket *so, struct sockaddr **nam)
  355: {
  356: 	int s = splnet();
  357: 	int error;
  358: 
  359: 	if ((so->so_state & SS_NOFDREF) == 0)
  360: 		panic("soaccept: !NOFDREF");
  361: 	so->so_state &= ~SS_NOFDREF;
  362: 	error = so_pru_accept(so, nam);
  363: 	splx(s);
  364: 	return (error);
  365: }
  366: 
  367: int
  368: soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
  369: {
  370: 	int s;
  371: 	int error;
  372: 
  373: 	if (so->so_options & SO_ACCEPTCONN)
  374: 		return (EOPNOTSUPP);
  375: 	s = splnet();
  376: 	/*
  377: 	 * If protocol is connection-based, can only connect once.
  378: 	 * Otherwise, if connected, try to disconnect first.
  379: 	 * This allows user to disconnect by connecting to, e.g.,
  380: 	 * a null address.
  381: 	 */
  382: 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
  383: 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
  384: 	    (error = sodisconnect(so))))
  385: 		error = EISCONN;
  386: 	else
  387: 		error = so_pru_connect(so, nam, td);
  388: 	splx(s);
  389: 	return (error);
  390: }
  391: 
  392: int
  393: soconnect2(struct socket *so1, struct socket *so2)
  394: {
  395: 	int s = splnet();
  396: 	int error;
  397: 
  398: 	error = so_pru_connect2(so1, so2);
  399: 	splx(s);
  400: 	return (error);
  401: }
  402: 
  403: int
  404: sodisconnect(struct socket *so)
  405: {
  406: 	int s = splnet();
  407: 	int error;
  408: 
  409: 	if ((so->so_state & SS_ISCONNECTED) == 0) {
  410: 		error = ENOTCONN;
  411: 		goto bad;
  412: 	}
  413: 	if (so->so_state & SS_ISDISCONNECTING) {
  414: 		error = EALREADY;
  415: 		goto bad;
  416: 	}
  417: 	error = so_pru_disconnect(so);
  418: bad:
  419: 	splx(s);
  420: 	return (error);
  421: }
  422: 
  423: #define	SBLOCKWAIT(f)	(((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
  424: /*
  425:  * Send on a socket.
  426:  * If send must go all at once and message is larger than
  427:  * send buffering, then hard error.
  428:  * Lock against other senders.
  429:  * If must go all at once and not enough room now, then
  430:  * inform user that this would block and do nothing.
  431:  * Otherwise, if nonblocking, send as much as possible.
  432:  * The data to be sent is described by "uio" if nonzero,
  433:  * otherwise by the mbuf chain "top" (which must be null
  434:  * if uio is not).  Data provided in mbuf chain must be small
  435:  * enough to send all at once.
  436:  *
  437:  * Returns nonzero on error, timeout or signal; callers
  438:  * must check for short counts if EINTR/ERESTART are returned.
  439:  * Data and control buffers are freed on return.
  440:  */
  441: int
  442: sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
  443: 	struct mbuf *top, struct mbuf *control, int flags,
  444: 	struct thread *td)
  445: {
  446: 	struct mbuf **mp;
  447: 	struct mbuf *m;
  448: 	long space, len, resid;
  449: 	int clen = 0, error, s, dontroute, mlen;
  450: 	int atomic = sosendallatonce(so) || top;
  451: 	int pru_flags;
  452: 
  453: 	if (uio)
  454: 		resid = uio->uio_resid;
  455: 	else
  456: 		resid = top->m_pkthdr.len;
  457: 	/*
  458: 	 * In theory resid should be unsigned.
  459: 	 * However, space must be signed, as it might be less than 0
  460: 	 * if we over-committed, and we must use a signed comparison
  461: 	 * of space and resid.  On the other hand, a negative resid
  462: 	 * causes us to loop sending 0-length segments to the protocol.
  463: 	 *
  464: 	 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
  465: 	 * type sockets since that's an error.
  466: 	 */
  467: 	if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
  468: 		error = EINVAL;
  469: 		goto out;
  470: 	}
  471: 
  472: 	dontroute =
  473: 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
  474: 	    (so->so_proto->pr_flags & PR_ATOMIC);
  475: 	if (td->td_proc && td->td_proc->p_stats)
  476: 		td->td_proc->p_stats->p_ru.ru_msgsnd++;
  477: 	if (control)
  478: 		clen = control->m_len;
  479: #define	gotoerr(errno)	{ error = errno; splx(s); goto release; }
  480: 
  481: restart:
  482: 	error = sblock(&so->so_snd, SBLOCKWAIT(flags));
  483: 	if (error)
  484: 		goto out;
  485: 	do {
  486: 		s = splnet();
  487: 		if (so->so_state & SS_CANTSENDMORE)
  488: 			gotoerr(EPIPE);
  489: 		if (so->so_error) {
  490: 			error = so->so_error;
  491: 			so->so_error = 0;
  492: 			splx(s);
  493: 			goto release;
  494: 		}
  495: 		if ((so->so_state & SS_ISCONNECTED) == 0) {
  496: 			/*
  497: 			 * `sendto' and `sendmsg' is allowed on a connection-
  498: 			 * based socket if it supports implied connect.
  499: 			 * Return ENOTCONN if not connected and no address is
  500: 			 * supplied.
  501: 			 */
  502: 			if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
  503: 			    (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
  504: 				if ((so->so_state & SS_ISCONFIRMING) == 0 &&
  505: 				    !(resid == 0 && clen != 0))
  506: 					gotoerr(ENOTCONN);
  507: 			} else if (addr == 0)
  508: 			    gotoerr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
  509: 				   ENOTCONN : EDESTADDRREQ);
  510: 		}
  511: 		space = sbspace(&so->so_snd);
  512: 		if (flags & MSG_OOB)
  513: 			space += 1024;
  514: 		if ((atomic && resid > so->so_snd.sb_hiwat) ||
  515: 		    clen > so->so_snd.sb_hiwat)
  516: 			gotoerr(EMSGSIZE);
  517: 		if (space < resid + clen && uio &&
  518: 		    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
  519: 			if (so->so_state & SS_NBIO)
  520: 				gotoerr(EWOULDBLOCK);
  521: 			sbunlock(&so->so_snd);
  522: 			error = sbwait(&so->so_snd);
  523: 			splx(s);
  524: 			if (error)
  525: 				goto out;
  526: 			goto restart;
  527: 		}
  528: 		splx(s);
  529: 		mp = &top;
  530: 		space -= clen;
  531: 		do {
  532: 		    if (uio == NULL) {
  533: 			/*
  534: 			 * Data is prepackaged in "top".
  535: 			 */
  536: 			resid = 0;
  537: 			if (flags & MSG_EOR)
  538: 				top->m_flags |= M_EOR;
  539: 		    } else do {
  540: 			if (top == 0) {
  541: 				MGETHDR(m, M_WAIT, MT_DATA);
  542: 				if (m == NULL) {
  543: 					error = ENOBUFS;
  544: 					goto release;
  545: 				}
  546: 				mlen = MHLEN;
  547: 				m->m_pkthdr.len = 0;
  548: 				m->m_pkthdr.rcvif = (struct ifnet *)0;
  549: 			} else {
  550: 				MGET(m, M_WAIT, MT_DATA);
  551: 				if (m == NULL) {
  552: 					error = ENOBUFS;
  553: 					goto release;
  554: 				}
  555: 				mlen = MLEN;
  556: 			}
  557: 			if (resid >= MINCLSIZE) {
  558: 				MCLGET(m, M_WAIT);
  559: 				if ((m->m_flags & M_EXT) == 0)
  560: 					goto nopages;
  561: 				mlen = MCLBYTES;
  562: 				len = min(min(mlen, resid), space);
  563: 			} else {
  564: nopages:
  565: 				len = min(min(mlen, resid), space);
  566: 				/*
  567: 				 * For datagram protocols, leave room
  568: 				 * for protocol headers in first mbuf.
  569: 				 */
  570: 				if (atomic && top == 0 && len < mlen)
  571: 					MH_ALIGN(m, len);
  572: 			}
  573: 			space -= len;
  574: 			error = uiomove(mtod(m, caddr_t), (int)len, uio);
  575: 			resid = uio->uio_resid;
  576: 			m->m_len = len;
  577: 			*mp = m;
  578: 			top->m_pkthdr.len += len;
  579: 			if (error)
  580: 				goto release;
  581: 			mp = &m->m_next;
  582: 			if (resid <= 0) {
  583: 				if (flags & MSG_EOR)
  584: 					top->m_flags |= M_EOR;
  585: 				break;
  586: 			}
  587: 		    } while (space > 0 && atomic);
  588: 		    if (dontroute)
  589: 			    so->so_options |= SO_DONTROUTE;
  590: 		    if (flags & MSG_OOB) {
  591: 		    	    pru_flags = PRUS_OOB;
  592: 		    } else if ((flags & MSG_EOF) &&
  593: 		    	       (so->so_proto->pr_flags & PR_IMPLOPCL) &&
  594: 		    	       (resid <= 0)) {
  595: 			    /*
  596: 			     * If the user set MSG_EOF, the protocol
  597: 			     * understands this flag and nothing left to
  598: 			     * send then use PRU_SEND_EOF instead of PRU_SEND.
  599: 			     */
  600: 		    	    pru_flags = PRUS_EOF;
  601: 		    } else if (resid > 0 && space > 0) {
  602: 			    /* If there is more to send, set PRUS_MORETOCOME */
  603: 		    	    pru_flags = PRUS_MORETOCOME;
  604: 		    } else {
  605: 		    	    pru_flags = 0;
  606: 		    }
  607: 		    s = splnet();				/* XXX */
  608: 		    /*
  609: 		     * XXX all the SS_CANTSENDMORE checks previously
  610: 		     * done could be out of date.  We could have recieved
  611: 		     * a reset packet in an interrupt or maybe we slept
  612: 		     * while doing page faults in uiomove() etc. We could
  613: 		     * probably recheck again inside the splnet() protection
  614: 		     * here, but there are probably other places that this
  615: 		     * also happens.  We must rethink this.
  616: 		     */
  617: 		    error = so_pru_send(so, pru_flags, top, addr, control, td);
  618: 		    splx(s);
  619: 		    if (dontroute)
  620: 			    so->so_options &= ~SO_DONTROUTE;
  621: 		    clen = 0;
  622: 		    control = 0;
  623: 		    top = 0;
  624: 		    mp = &top;
  625: 		    if (error)
  626: 			    goto release;
  627: 		} while (resid && space > 0);
  628: 	} while (resid);
  629: 
  630: release:
  631: 	sbunlock(&so->so_snd);
  632: out:
  633: 	if (top)
  634: 		m_freem(top);
  635: 	if (control)
  636: 		m_freem(control);
  637: 	return (error);
  638: }
  639: 
  640: /*
  641:  * A specialization of sosend() for UDP based on protocol-specific knowledge:
  642:  *   so->so_proto->pr_flags has the PR_ATOMIC field set.  This means that
  643:  *	sosendallatonce() returns true,
  644:  *	the "atomic" variable is true,
  645:  *	and sosendudp() blocks until space is available for the entire send.
  646:  *   so->so_proto->pr_flags does not have the PR_CONNREQUIRED or
  647:  *	PR_IMPLOPCL flags set.
  648:  *   UDP has no out-of-band data.
  649:  *   UDP has no control data.
  650:  *   UDP does not support MSG_EOR.
  651:  */
  652: int
  653: sosendudp(struct socket *so, struct sockaddr *addr, struct uio *uio,
  654: 	  struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
  655: {
  656: 	int resid, error, s;
  657: 	boolean_t dontroute;		/* temporary SO_DONTROUTE setting */
  658: 
  659: 	if (td->td_proc && td->td_proc->p_stats)
  660: 		td->td_proc->p_stats->p_ru.ru_msgsnd++;
  661: 	if (control)
  662: 		m_freem(control);
  663: 
  664: 	KASSERT((uio && !top) || (top && !uio), ("bad arguments to sosendudp"));
  665: 	resid = uio ? uio->uio_resid : top->m_pkthdr.len;
  666: 
  667: restart:
  668: 	error = sblock(&so->so_snd, SBLOCKWAIT(flags));
  669: 	if (error)
  670: 		goto out;
  671: 
  672: 	s = splnet();
  673: 	if (so->so_state & SS_CANTSENDMORE)
  674: 		gotoerr(EPIPE);
  675: 	if (so->so_error) {
  676: 		error = so->so_error;
  677: 		so->so_error = 0;
  678: 		splx(s);
  679: 		goto release;
  680: 	}
  681: 	if (!(so->so_state & SS_ISCONNECTED) && addr == NULL)
  682: 		gotoerr(EDESTADDRREQ);
  683: 	if (resid > so->so_snd.sb_hiwat)
  684: 		gotoerr(EMSGSIZE);
  685: 	if (uio && sbspace(&so->so_snd) < resid) {
  686: 		if (so->so_state & SS_NBIO)
  687: 			gotoerr(EWOULDBLOCK);
  688: 		sbunlock(&so->so_snd);
  689: 		error = sbwait(&so->so_snd);
  690: 		splx(s);
  691: 		if (error)
  692: 			goto out;
  693: 		goto restart;
  694: 	}
  695: 	splx(s);
  696: 
  697: 	if (uio) {
  698: 		top = m_uiomove(uio, M_WAIT, 0);
  699: 		if (top == NULL)
  700: 			goto release;
  701: 	}
  702: 
  703: 	dontroute = (flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE);
  704: 	if (dontroute)
  705: 		so->so_options |= SO_DONTROUTE;
  706: 
  707: 	error = so_pru_send(so, 0, top, addr, NULL, td);
  708: 	top = NULL;		/* sent or freed in lower layer */
  709: 
  710: 	if (dontroute)
  711: 		so->so_options &= ~SO_DONTROUTE;
  712: 
  713: release:
  714: 	sbunlock(&so->so_snd);
  715: out:
  716: 	if (top)
  717: 		m_freem(top);
  718: 	return (error);
  719: }
  720: 
  721: /*
  722:  * Implement receive operations on a socket.
  723:  * We depend on the way that records are added to the sockbuf
  724:  * by sbappend*.  In particular, each record (mbufs linked through m_next)
  725:  * must begin with an address if the protocol so specifies,
  726:  * followed by an optional mbuf or mbufs containing ancillary data,
  727:  * and then zero or more mbufs of data.
  728:  * In order to avoid blocking network interrupts for the entire time here,
  729:  * we splx() while doing the actual copy to user space.
  730:  * Although the sockbuf is locked, new data may still be appended,
  731:  * and thus we must maintain consistency of the sockbuf during that time.
  732:  *
  733:  * The caller may receive the data as a single mbuf chain by supplying
  734:  * an mbuf **mp0 for use in returning the chain.  The uio is then used
  735:  * only for the count in uio_resid.
  736:  */
  737: int
  738: soreceive(so, psa, uio, mp0, controlp, flagsp)
  739: 	struct socket *so;
  740: 	struct sockaddr **psa;
  741: 	struct uio *uio;
  742: 	struct mbuf **mp0;
  743: 	struct mbuf **controlp;
  744: 	int *flagsp;
  745: {
  746: 	struct mbuf *m, **mp;
  747: 	int flags, len, error, s, offset;
  748: 	struct protosw *pr = so->so_proto;
  749: 	struct mbuf *nextrecord;
  750: 	int moff, type = 0;
  751: 	int orig_resid = uio->uio_resid;
  752: 
  753: 	mp = mp0;
  754: 	if (psa)
  755: 		*psa = 0;
  756: 	if (controlp)
  757: 		*controlp = 0;
  758: 	if (flagsp)
  759: 		flags = *flagsp &~ MSG_EOR;
  760: 	else
  761: 		flags = 0;
  762: 	if (flags & MSG_OOB) {
  763: 		m = m_get(M_WAIT, MT_DATA);
  764: 		if (m == NULL)
  765: 			return (ENOBUFS);
  766: 		error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
  767: 		if (error)
  768: 			goto bad;
  769: 		do {
  770: 			error = uiomove(mtod(m, caddr_t),
  771: 			    (int) min(uio->uio_resid, m->m_len), uio);
  772: 			m = m_free(m);
  773: 		} while (uio->uio_resid && error == 0 && m);
  774: bad:
  775: 		if (m)
  776: 			m_freem(m);
  777: 		return (error);
  778: 	}
  779: 	if (mp)
  780: 		*mp = (struct mbuf *)0;
  781: 	if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
  782: 		so_pru_rcvd(so, 0);
  783: 
  784: restart:
  785: 	error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
  786: 	if (error)
  787: 		return (error);
  788: 	s = splnet();
  789: 
  790: 	m = so->so_rcv.sb_mb;
  791: 	/*
  792: 	 * If we have less data than requested, block awaiting more
  793: 	 * (subject to any timeout) if:
  794: 	 *   1. the current count is less than the low water mark, or
  795: 	 *   2. MSG_WAITALL is set, and it is possible to do the entire
  796: 	 *	receive operation at once if we block (resid <= hiwat).
  797: 	 *   3. MSG_DONTWAIT is not set
  798: 	 * If MSG_WAITALL is set but resid is larger than the receive buffer,
  799: 	 * we have to do the receive in sections, and thus risk returning
  800: 	 * a short count if a timeout or signal occurs after we start.
  801: 	 */
  802: 	if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
  803: 	    so->so_rcv.sb_cc < uio->uio_resid) &&
  804: 	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
  805: 	    ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
  806: 	    m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
  807: 		KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
  808: 		if (so->so_error) {
  809: 			if (m)
  810: 				goto dontblock;
  811: 			error = so->so_error;
  812: 			if ((flags & MSG_PEEK) == 0)
  813: 				so->so_error = 0;
  814: 			goto release;
  815: 		}
  816: 		if (so->so_state & SS_CANTRCVMORE) {
  817: 			if (m)
  818: 				goto dontblock;
  819: 			else
  820: 				goto release;
  821: 		}
  822: 		for (; m; m = m->m_next)
  823: 			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
  824: 				m = so->so_rcv.sb_mb;
  825: 				goto dontblock;
  826: 			}
  827: 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
  828: 		    (pr->pr_flags & PR_CONNREQUIRED)) {
  829: 			error = ENOTCONN;
  830: 			goto release;
  831: 		}
  832: 		if (uio->uio_resid == 0)
  833: 			goto release;
  834: 		if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
  835: 			error = EWOULDBLOCK;
  836: 			goto release;
  837: 		}
  838: 		sbunlock(&so->so_rcv);
  839: 		error = sbwait(&so->so_rcv);
  840: 		splx(s);
  841: 		if (error)
  842: 			return (error);
  843: 		goto restart;
  844: 	}
  845: dontblock:
  846: 	if (uio->uio_td && uio->uio_td->td_proc)
  847: 		uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++;
  848: 	nextrecord = m->m_nextpkt;
  849: 	if (pr->pr_flags & PR_ADDR) {
  850: 		KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
  851: 		orig_resid = 0;
  852: 		if (psa)
  853: 			*psa = dup_sockaddr(mtod(m, struct sockaddr *),
  854: 					    mp0 == 0);
  855: 		if (flags & MSG_PEEK) {
  856: 			m = m->m_next;
  857: 		} else {
  858: 			sbfree(&so->so_rcv, m);
  859: 			so->so_rcv.sb_mb = m_free(m);
  860: 			m = so->so_rcv.sb_mb;
  861: 		}
  862: 	}
  863: 	while (m && m->m_type == MT_CONTROL && error == 0) {
  864: 		if (flags & MSG_PEEK) {
  865: 			if (controlp)
  866: 				*controlp = m_copy(m, 0, m->m_len);
  867: 			m = m->m_next;
  868: 		} else {
  869: 			sbfree(&so->so_rcv, m);
  870: 			if (controlp) {
  871: 				if (pr->pr_domain->dom_externalize &&
  872: 				    mtod(m, struct cmsghdr *)->cmsg_type ==
  873: 				    SCM_RIGHTS)
  874: 				   error = (*pr->pr_domain->dom_externalize)(m);
  875: 				*controlp = m;
  876: 				so->so_rcv.sb_mb = m->m_next;
  877: 				m->m_next = 0;
  878: 				m = so->so_rcv.sb_mb;
  879: 			} else {
  880: 				so->so_rcv.sb_mb = m_free(m);
  881: 				m = so->so_rcv.sb_mb;
  882: 			}
  883: 		}
  884: 		if (controlp) {
  885: 			orig_resid = 0;
  886: 			controlp = &(*controlp)->m_next;
  887: 		}
  888: 	}
  889: 	if (m) {
  890: 		if ((flags & MSG_PEEK) == 0)
  891: 			m->m_nextpkt = nextrecord;
  892: 		type = m->m_type;
  893: 		if (type == MT_OOBDATA)
  894: 			flags |= MSG_OOB;
  895: 	}
  896: 	moff = 0;
  897: 	offset = 0;
  898: 	while (m && uio->uio_resid > 0 && error == 0) {
  899: 		if (m->m_type == MT_OOBDATA) {
  900: 			if (type != MT_OOBDATA)
  901: 				break;
  902: 		} else if (type == MT_OOBDATA)
  903: 			break;
  904: 		else
  905: 		    KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
  906: 			("receive 3"));
  907: 		so->so_state &= ~SS_RCVATMARK;
  908: 		len = uio->uio_resid;
  909: 		if (so->so_oobmark && len > so->so_oobmark - offset)
  910: 			len = so->so_oobmark - offset;
  911: 		if (len > m->m_len - moff)
  912: 			len = m->m_len - moff;
  913: 		/*
  914: 		 * If mp is set, just pass back the mbufs.
  915: 		 * Otherwise copy them out via the uio, then free.
  916: 		 * Sockbuf must be consistent here (points to current mbuf,
  917: 		 * it points to next record) when we drop priority;
  918: 		 * we must note any additions to the sockbuf when we
  919: 		 * block interrupts again.
  920: 		 */
  921: 		if (mp == 0) {
  922: 			splx(s);
  923: 			error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
  924: 			s = splnet();
  925: 			if (error)
  926: 				goto release;
  927: 		} else
  928: 			uio->uio_resid -= len;
  929: 		if (len == m->m_len - moff) {
  930: 			if (m->m_flags & M_EOR)
  931: 				flags |= MSG_EOR;
  932: 			if (flags & MSG_PEEK) {
  933: 				m = m->m_next;
  934: 				moff = 0;
  935: 			} else {
  936: 				nextrecord = m->m_nextpkt;
  937: 				sbfree(&so->so_rcv, m);
  938: 				if (mp) {
  939: 					*mp = m;
  940: 					mp = &m->m_next;
  941: 					so->so_rcv.sb_mb = m = m->m_next;
  942: 					*mp = (struct mbuf *)0;
  943: 				} else {
  944: 					so->so_rcv.sb_mb = m = m_free(m);
  945: 				}
  946: 				if (m)
  947: 					m->m_nextpkt = nextrecord;
  948: 			}
  949: 		} else {
  950: 			if (flags & MSG_PEEK)
  951: 				moff += len;
  952: 			else {
  953: 				if (mp)
  954: 					*mp = m_copym(m, 0, len, M_WAIT);
  955: 				m->m_data += len;
  956: 				m->m_len -= len;
  957: 				so->so_rcv.sb_cc -= len;
  958: 			}
  959: 		}
  960: 		if (so->so_oobmark) {
  961: 			if ((flags & MSG_PEEK) == 0) {
  962: 				so->so_oobmark -= len;
  963: 				if (so->so_oobmark == 0) {
  964: 					so->so_state |= SS_RCVATMARK;
  965: 					break;
  966: 				}
  967: 			} else {
  968: 				offset += len;
  969: 				if (offset == so->so_oobmark)
  970: 					break;
  971: 			}
  972: 		}
  973: 		if (flags & MSG_EOR)
  974: 			break;
  975: 		/*
  976: 		 * If the MSG_WAITALL flag is set (for non-atomic socket),
  977: 		 * we must not quit until "uio->uio_resid == 0" or an error
  978: 		 * termination.  If a signal/timeout occurs, return
  979: 		 * with a short count but without error.
  980: 		 * Keep sockbuf locked against other readers.
  981: 		 */
  982: 		while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
  983: 		    !sosendallatonce(so) && !nextrecord) {
  984: 			if (so->so_error || so->so_state & SS_CANTRCVMORE)
  985: 				break;
  986: 			/*
  987: 			 * The window might have closed to zero, make
  988: 			 * sure we send an ack now that we've drained
  989: 			 * the buffer or we might end up blocking until
  990: 			 * the idle takes over (5 seconds).
  991: 			 */
  992: 			if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
  993: 				so_pru_rcvd(so, flags);
  994: 			error = sbwait(&so->so_rcv);
  995: 			if (error) {
  996: 				sbunlock(&so->so_rcv);
  997: 				splx(s);
  998: 				return (0);
  999: 			}
 1000: 			m = so->so_rcv.sb_mb;
 1001: 			if (m)
 1002: 				nextrecord = m->m_nextpkt;
 1003: 		}
 1004: 	}
 1005: 
 1006: 	if (m && pr->pr_flags & PR_ATOMIC) {
 1007: 		flags |= MSG_TRUNC;
 1008: 		if ((flags & MSG_PEEK) == 0)
 1009: 			(void) sbdroprecord(&so->so_rcv);
 1010: 	}
 1011: 	if ((flags & MSG_PEEK) == 0) {
 1012: 		if (m == 0)
 1013: 			so->so_rcv.sb_mb = nextrecord;
 1014: 		if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
 1015: 			so_pru_rcvd(so, flags);
 1016: 	}
 1017: 	if (orig_resid == uio->uio_resid && orig_resid &&
 1018: 	    (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
 1019: 		sbunlock(&so->so_rcv);
 1020: 		splx(s);
 1021: 		goto restart;
 1022: 	}
 1023: 
 1024: 	if (flagsp)
 1025: 		*flagsp |= flags;
 1026: release:
 1027: 	sbunlock(&so->so_rcv);
 1028: 	splx(s);
 1029: 	return (error);
 1030: }
 1031: 
 1032: int
 1033: soshutdown(so, how)
 1034: 	struct socket *so;
 1035: 	int how;
 1036: {
 1037: 	if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
 1038: 		return (EINVAL);
 1039: 
 1040: 	if (how != SHUT_WR)
 1041: 		sorflush(so);
 1042: 	if (how != SHUT_RD)
 1043: 		return (so_pru_shutdown(so));
 1044: 	return (0);
 1045: }
 1046: 
 1047: void
 1048: sorflush(so)
 1049: 	struct socket *so;
 1050: {
 1051: 	struct sockbuf *sb = &so->so_rcv;
 1052: 	struct protosw *pr = so->so_proto;
 1053: 	int s;
 1054: 	struct sockbuf asb;
 1055: 
 1056: 	sb->sb_flags |= SB_NOINTR;
 1057: 	(void) sblock(sb, M_WAITOK);
 1058: 	s = splimp();
 1059: 	socantrcvmore(so);
 1060: 	sbunlock(sb);
 1061: 	asb = *sb;
 1062: 	bzero((caddr_t)sb, sizeof (*sb));
 1063: 	if (asb.sb_flags & SB_KNOTE) {
 1064: 		sb->sb_sel.si_note = asb.sb_sel.si_note;
 1065: 		sb->sb_flags = SB_KNOTE;
 1066: 	}
 1067: 	splx(s);
 1068: 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
 1069: 		(*pr->pr_domain->dom_dispose)(asb.sb_mb);
 1070: 	sbrelease(&asb, so);
 1071: }
 1072: 
 1073: #ifdef INET
 1074: static int
 1075: do_setopt_accept_filter(so, sopt)
 1076: 	struct	socket *so;
 1077: 	struct	sockopt *sopt;
 1078: {
 1079: 	struct accept_filter_arg	*afap = NULL;
 1080: 	struct accept_filter	*afp;
 1081: 	struct so_accf	*af = so->so_accf;
 1082: 	int	error = 0;
 1083: 
 1084: 	/* do not set/remove accept filters on non listen sockets */
 1085: 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
 1086: 		error = EINVAL;
 1087: 		goto out;
 1088: 	}
 1089: 
 1090: 	/* removing the filter */
 1091: 	if (sopt == NULL) {
 1092: 		if (af != NULL) {
 1093: 			if (af->so_accept_filter != NULL && 
 1094: 				af->so_accept_filter->accf_destroy != NULL) {
 1095: 				af->so_accept_filter->accf_destroy(so);
 1096: 			}
 1097: 			if (af->so_accept_filter_str != NULL) {
 1098: 				FREE(af->so_accept_filter_str, M_ACCF);
 1099: 			}
 1100: 			FREE(af, M_ACCF);
 1101: 			so->so_accf = NULL;
 1102: 		}
 1103: 		so->so_options &= ~SO_ACCEPTFILTER;
 1104: 		return (0);
 1105: 	}
 1106: 	/* adding a filter */
 1107: 	/* must remove previous filter first */
 1108: 	if (af != NULL) {
 1109: 		error = EINVAL;
 1110: 		goto out;
 1111: 	}
 1112: 	/* don't put large objects on the kernel stack */
 1113: 	MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
 1114: 	error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
 1115: 	afap->af_name[sizeof(afap->af_name)-1] = '\0';
 1116: 	afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
 1117: 	if (error)
 1118: 		goto out;
 1119: 	afp = accept_filt_get(afap->af_name);
 1120: 	if (afp == NULL) {
 1121: 		error = ENOENT;
 1122: 		goto out;
 1123: 	}
 1124: 	MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
 1125: 	bzero(af, sizeof(*af));
 1126: 	if (afp->accf_create != NULL) {
 1127: 		if (afap->af_name[0] != '\0') {
 1128: 			int len = strlen(afap->af_name) + 1;
 1129: 
 1130: 			MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
 1131: 			strcpy(af->so_accept_filter_str, afap->af_name);
 1132: 		}
 1133: 		af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
 1134: 		if (af->so_accept_filter_arg == NULL) {
 1135: 			FREE(af->so_accept_filter_str, M_ACCF);
 1136: 			FREE(af, M_ACCF);
 1137: 			so->so_accf = NULL;
 1138: 			error = EINVAL;
 1139: 			goto out;
 1140: 		}
 1141: 	}
 1142: 	af->so_accept_filter = afp;
 1143: 	so->so_accf = af;
 1144: 	so->so_options |= SO_ACCEPTFILTER;
 1145: out:
 1146: 	if (afap != NULL)
 1147: 		FREE(afap, M_TEMP);
 1148: 	return (error);
 1149: }
 1150: #endif /* INET */
 1151: 
 1152: /*
 1153:  * Perhaps this routine, and sooptcopyout(), below, ought to come in
 1154:  * an additional variant to handle the case where the option value needs
 1155:  * to be some kind of integer, but not a specific size.
 1156:  * In addition to their use here, these functions are also called by the
 1157:  * protocol-level pr_ctloutput() routines.
 1158:  */
 1159: int
 1160: sooptcopyin(sopt, buf, len, minlen)
 1161: 	struct	sockopt *sopt;
 1162: 	void	*buf;
 1163: 	size_t	len;
 1164: 	size_t	minlen;
 1165: {
 1166: 	size_t	valsize;
 1167: 
 1168: 	/*
 1169: 	 * If the user gives us more than we wanted, we ignore it,
 1170: 	 * but if we don't get the minimum length the caller
 1171: 	 * wants, we return EINVAL.  On success, sopt->sopt_valsize
 1172: 	 * is set to however much we actually retrieved.
 1173: 	 */
 1174: 	if ((valsize = sopt->sopt_valsize) < minlen)
 1175: 		return EINVAL;
 1176: 	if (valsize > len)
 1177: 		sopt->sopt_valsize = valsize = len;
 1178: 
 1179: 	if (sopt->sopt_td != NULL)
 1180: 		return (copyin(sopt->sopt_val, buf, valsize));
 1181: 
 1182: 	bcopy(sopt->sopt_val, buf, valsize);
 1183: 	return 0;
 1184: }
 1185: 
 1186: int
 1187: sosetopt(so, sopt)
 1188: 	struct socket *so;
 1189: 	struct sockopt *sopt;
 1190: {
 1191: 	int	error, optval;
 1192: 	struct	linger l;
 1193: 	struct	timeval tv;
 1194: 	u_long  val;
 1195: 
 1196: 	error = 0;
 1197: 	if (sopt->sopt_level != SOL_SOCKET) {
 1198: 		if (so->so_proto && so->so_proto->pr_ctloutput) {
 1199: 			return (so_pr_ctloutput(so, sopt));
 1200: 		}
 1201: 		error = ENOPROTOOPT;
 1202: 	} else {
 1203: 		switch (sopt->sopt_name) {
 1204: #ifdef INET
 1205: 		case SO_ACCEPTFILTER:
 1206: 			error = do_setopt_accept_filter(so, sopt);
 1207: 			if (error)
 1208: 				goto bad;
 1209: 			break;
 1210: #endif /* INET */
 1211: 		case SO_LINGER:
 1212: 			error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
 1213: 			if (error)
 1214: 				goto bad;
 1215: 
 1216: 			so->so_linger = l.l_linger;
 1217: 			if (l.l_onoff)
 1218: 				so->so_options |= SO_LINGER;
 1219: 			else
 1220: 				so->so_options &= ~SO_LINGER;
 1221: 			break;
 1222: 
 1223: 		case SO_DEBUG:
 1224: 		case SO_KEEPALIVE:
 1225: 		case SO_DONTROUTE:
 1226: 		case SO_USELOOPBACK:
 1227: 		case SO_BROADCAST:
 1228: 		case SO_REUSEADDR:
 1229: 		case SO_REUSEPORT:
 1230: 		case SO_OOBINLINE:
 1231: 		case SO_TIMESTAMP:
 1232: 			error = sooptcopyin(sopt, &optval, sizeof optval,
 1233: 					    sizeof optval);
 1234: 			if (error)
 1235: 				goto bad;
 1236: 			if (optval)
 1237: 				so->so_options |= sopt->sopt_name;
 1238: 			else
 1239: 				so->so_options &= ~sopt->sopt_name;
 1240: 			break;
 1241: 
 1242: 		case SO_SNDBUF:
 1243: 		case SO_RCVBUF:
 1244: 		case SO_SNDLOWAT:
 1245: 		case SO_RCVLOWAT:
 1246: 			error = sooptcopyin(sopt, &optval, sizeof optval,
 1247: 					    sizeof optval);
 1248: 			if (error)
 1249: 				goto bad;
 1250: 
 1251: 			/*
 1252: 			 * Values < 1 make no sense for any of these
 1253: 			 * options, so disallow them.
 1254: 			 */
 1255: 			if (optval < 1) {
 1256: 				error = EINVAL;
 1257: 				goto bad;
 1258: 			}
 1259: 
 1260: 			switch (sopt->sopt_name) {
 1261: 			case SO_SNDBUF:
 1262: 			case SO_RCVBUF:
 1263: 				if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
 1264: 				    &so->so_snd : &so->so_rcv, (u_long)optval,
 1265: 				    so,
 1266: 				    &curproc->p_rlimit[RLIMIT_SBSIZE]) == 0) {
 1267: 					error = ENOBUFS;
 1268: 					goto bad;
 1269: 				}
 1270: 				break;
 1271: 
 1272: 			/*
 1273: 			 * Make sure the low-water is never greater than
 1274: 			 * the high-water.
 1275: 			 */
 1276: 			case SO_SNDLOWAT:
 1277: 				so->so_snd.sb_lowat =
 1278: 				    (optval > so->so_snd.sb_hiwat) ?
 1279: 				    so->so_snd.sb_hiwat : optval;
 1280: 				break;
 1281: 			case SO_RCVLOWAT:
 1282: 				so->so_rcv.sb_lowat =
 1283: 				    (optval > so->so_rcv.sb_hiwat) ?
 1284: 				    so->so_rcv.sb_hiwat : optval;
 1285: 				break;
 1286: 			}
 1287: 			break;
 1288: 
 1289: 		case SO_SNDTIMEO:
 1290: 		case SO_RCVTIMEO:
 1291: 			error = sooptcopyin(sopt, &tv, sizeof tv,
 1292: 					    sizeof tv);
 1293: 			if (error)
 1294: 				goto bad;
 1295: 
 1296: 			/* assert(hz > 0); */
 1297: 			if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
 1298: 			    tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
 1299: 				error = EDOM;
 1300: 				goto bad;
 1301: 			}
 1302: 			/* assert(tick > 0); */
 1303: 			/* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
 1304: 			val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
 1305: 			if (val > SHRT_MAX) {
 1306: 				error = EDOM;
 1307: 				goto bad;
 1308: 			}
 1309: 			if (val == 0 && tv.tv_usec != 0)
 1310: 				val = 1;
 1311: 
 1312: 			switch (sopt->sopt_name) {
 1313: 			case SO_SNDTIMEO:
 1314: 				so->so_snd.sb_timeo = val;
 1315: 				break;
 1316: 			case SO_RCVTIMEO:
 1317: 				so->so_rcv.sb_timeo = val;
 1318: 				break;
 1319: 			}
 1320: 			break;
 1321: 		default:
 1322: 			error = ENOPROTOOPT;
 1323: 			break;
 1324: 		}
 1325: 		if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
 1326: 			(void) so_pr_ctloutput(so, sopt);
 1327: 		}
 1328: 	}
 1329: bad:
 1330: 	return (error);
 1331: }
 1332: 
 1333: /* Helper routine for getsockopt */
 1334: int
 1335: sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
 1336: {
 1337: 	int	error;
 1338: 	size_t	valsize;
 1339: 
 1340: 	error = 0;
 1341: 
 1342: 	/*
 1343: 	 * Documented get behavior is that we always return a value,
 1344: 	 * possibly truncated to fit in the user's buffer.
 1345: 	 * Traditional behavior is that we always tell the user
 1346: 	 * precisely how much we copied, rather than something useful
 1347: 	 * like the total amount we had available for her.
 1348: 	 * Note that this interface is not idempotent; the entire answer must
 1349: 	 * generated ahead of time.
 1350: 	 */
 1351: 	valsize = min(len, sopt->sopt_valsize);
 1352: 	sopt->sopt_valsize = valsize;
 1353: 	if (sopt->sopt_val != 0) {
 1354: 		if (sopt->sopt_td != NULL)
 1355: 			error = copyout(buf, sopt->sopt_val, valsize);
 1356: 		else
 1357: 			bcopy(buf, sopt->sopt_val, valsize);
 1358: 	}
 1359: 	return error;
 1360: }
 1361: 
 1362: int
 1363: sogetopt(so, sopt)
 1364: 	struct socket *so;
 1365: 	struct sockopt *sopt;
 1366: {
 1367: 	int	error, optval;
 1368: 	struct	linger l;
 1369: 	struct	timeval tv;
 1370: #ifdef INET
 1371: 	struct accept_filter_arg *afap;
 1372: #endif
 1373: 
 1374: 	error = 0;
 1375: 	if (sopt->sopt_level != SOL_SOCKET) {
 1376: 		if (so->so_proto && so->so_proto->pr_ctloutput) {
 1377: 			return (so_pr_ctloutput(so, sopt));
 1378: 		} else
 1379: 			return (ENOPROTOOPT);
 1380: 	} else {
 1381: 		switch (sopt->sopt_name) {
 1382: #ifdef INET
 1383: 		case SO_ACCEPTFILTER:
 1384: 			if ((so->so_options & SO_ACCEPTCONN) == 0)
 1385: 				return (EINVAL);
 1386: 			MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
 1387: 				M_TEMP, M_WAITOK);
 1388: 			bzero(afap, sizeof(*afap));
 1389: 			if ((so->so_options & SO_ACCEPTFILTER) != 0) {
 1390: 				strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
 1391: 				if (so->so_accf->so_accept_filter_str != NULL)
 1392: 					strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
 1393: 			}
 1394: 			error = sooptcopyout(sopt, afap, sizeof(*afap));
 1395: 			FREE(afap, M_TEMP);
 1396: 			break;
 1397: #endif /* INET */
 1398: 			
 1399: 		case SO_LINGER:
 1400: 			l.l_onoff = so->so_options & SO_LINGER;
 1401: 			l.l_linger = so->so_linger;
 1402: 			error = sooptcopyout(sopt, &l, sizeof l);
 1403: 			break;
 1404: 
 1405: 		case SO_USELOOPBACK:
 1406: 		case SO_DONTROUTE:
 1407: 		case SO_DEBUG:
 1408: 		case SO_KEEPALIVE:
 1409: 		case SO_REUSEADDR:
 1410: 		case SO_REUSEPORT:
 1411: 		case SO_BROADCAST:
 1412: 		case SO_OOBINLINE:
 1413: 		case SO_TIMESTAMP:
 1414: 			optval = so->so_options & sopt->sopt_name;
 1415: integer:
 1416: 			error = sooptcopyout(sopt, &optval, sizeof optval);
 1417: 			break;
 1418: 
 1419: 		case SO_TYPE:
 1420: 			optval = so->so_type;
 1421: 			goto integer;
 1422: 
 1423: 		case SO_ERROR:
 1424: 			optval = so->so_error;
 1425: 			so->so_error = 0;
 1426: 			goto integer;
 1427: 
 1428: 		case SO_SNDBUF:
 1429: 			optval = so->so_snd.sb_hiwat;
 1430: 			goto integer;
 1431: 
 1432: 		case SO_RCVBUF:
 1433: 			optval = so->so_rcv.sb_hiwat;
 1434: 			goto integer;
 1435: 
 1436: 		case SO_SNDLOWAT:
 1437: 			optval = so->so_snd.sb_lowat;
 1438: 			goto integer;
 1439: 
 1440: 		case SO_RCVLOWAT:
 1441: 			optval = so->so_rcv.sb_lowat;
 1442: 			goto integer;
 1443: 
 1444: 		case SO_SNDTIMEO:
 1445: 		case SO_RCVTIMEO:
 1446: 			optval = (sopt->sopt_name == SO_SNDTIMEO ?
 1447: 				  so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
 1448: 
 1449: 			tv.tv_sec = optval / hz;
 1450: 			tv.tv_usec = (optval % hz) * tick;
 1451: 			error = sooptcopyout(sopt, &tv, sizeof tv);
 1452: 			break;			
 1453: 
 1454: 		default:
 1455: 			error = ENOPROTOOPT;
 1456: 			break;
 1457: 		}
 1458: 		return (error);
 1459: 	}
 1460: }
 1461: 
 1462: /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
 1463: int
 1464: soopt_getm(struct sockopt *sopt, struct mbuf **mp)
 1465: {
 1466: 	struct mbuf *m, *m_prev;
 1467: 	int sopt_size = sopt->sopt_valsize;
 1468: 
 1469: 	MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
 1470: 	if (m == 0)
 1471: 		return ENOBUFS;
 1472: 	if (sopt_size > MLEN) {
 1473: 		MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
 1474: 		if ((m->m_flags & M_EXT) == 0) {
 1475: 			m_free(m);
 1476: 			return ENOBUFS;
 1477: 		}
 1478: 		m->m_len = min(MCLBYTES, sopt_size);
 1479: 	} else {
 1480: 		m->m_len = min(MLEN, sopt_size);
 1481: 	}
 1482: 	sopt_size -= m->m_len;
 1483: 	*mp = m;
 1484: 	m_prev = m;
 1485: 
 1486: 	while (sopt_size) {
 1487: 		MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
 1488: 		if (m == 0) {
 1489: 			m_freem(*mp);
 1490: 			return ENOBUFS;
 1491: 		}
 1492: 		if (sopt_size > MLEN) {
 1493: 			MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
 1494: 			if ((m->m_flags & M_EXT) == 0) {
 1495: 				m_freem(*mp);
 1496: 				return ENOBUFS;
 1497: 			}
 1498: 			m->m_len = min(MCLBYTES, sopt_size);
 1499: 		} else {
 1500: 			m->m_len = min(MLEN, sopt_size);
 1501: 		}
 1502: 		sopt_size -= m->m_len;
 1503: 		m_prev->m_next = m;
 1504: 		m_prev = m;
 1505: 	}
 1506: 	return 0;
 1507: }
 1508: 
 1509: /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
 1510: int
 1511: soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
 1512: {
 1513: 	struct mbuf *m0 = m;
 1514: 
 1515: 	if (sopt->sopt_val == NULL)
 1516: 		return 0;
 1517: 	while (m != NULL && sopt->sopt_valsize >= m->m_len) {
 1518: 		if (sopt->sopt_td != NULL) {
 1519: 			int error;
 1520: 
 1521: 			error = copyin(sopt->sopt_val, mtod(m, char *),
 1522: 				       m->m_len);
 1523: 			if (error != 0) {
 1524: 				m_freem(m0);
 1525: 				return(error);
 1526: 			}
 1527: 		} else
 1528: 			bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
 1529: 		sopt->sopt_valsize -= m->m_len;
 1530: 		(caddr_t)sopt->sopt_val += m->m_len;
 1531: 		m = m->m_next;
 1532: 	}
 1533: 	if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
 1534: 		panic("ip6_sooptmcopyin");
 1535: 	return 0;
 1536: }
 1537: 
 1538: /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
 1539: int
 1540: soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
 1541: {
 1542: 	struct mbuf *m0 = m;
 1543: 	size_t valsize = 0;
 1544: 
 1545: 	if (sopt->sopt_val == NULL)
 1546: 		return 0;
 1547: 	while (m != NULL && sopt->sopt_valsize >= m->m_len) {
 1548: 		if (sopt->sopt_td != NULL) {
 1549: 			int error;
 1550: 
 1551: 			error = copyout(mtod(m, char *), sopt->sopt_val,
 1552: 				       m->m_len);
 1553: 			if (error != 0) {
 1554: 				m_freem(m0);
 1555: 				return(error);
 1556: 			}
 1557: 		} else
 1558: 			bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
 1559: 	       sopt->sopt_valsize -= m->m_len;
 1560: 	       (caddr_t)sopt->sopt_val += m->m_len;
 1561: 	       valsize += m->m_len;
 1562: 	       m = m->m_next;
 1563: 	}
 1564: 	if (m != NULL) {
 1565: 		/* enough soopt buffer should be given from user-land */
 1566: 		m_freem(m0);
 1567: 		return(EINVAL);
 1568: 	}
 1569: 	sopt->sopt_valsize = valsize;
 1570: 	return 0;
 1571: }
 1572: 
 1573: void
 1574: sohasoutofband(so)
 1575: 	struct socket *so;
 1576: {
 1577: 	if (so->so_sigio != NULL)
 1578: 		pgsigio(so->so_sigio, SIGURG, 0);
 1579: 	selwakeup(&so->so_rcv.sb_sel);
 1580: }
 1581: 
 1582: int
 1583: sopoll(struct socket *so, int events, struct ucred *cred, struct thread *td)
 1584: {
 1585: 	int revents = 0;
 1586: 	int s = splnet();
 1587: 
 1588: 	if (events & (POLLIN | POLLRDNORM))
 1589: 		if (soreadable(so))
 1590: 			revents |= events & (POLLIN | POLLRDNORM);
 1591: 
 1592: 	if (events & POLLINIGNEOF)
 1593: 		if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
 1594: 			!TAILQ_EMPTY(&so->so_comp) || so->so_error)
 1595: 			revents |= POLLINIGNEOF;
 1596: 
 1597: 	if (events & (POLLOUT | POLLWRNORM))
 1598: 		if (sowriteable(so))
 1599: 			revents |= events & (POLLOUT | POLLWRNORM);
 1600: 
 1601: 	if (events & (POLLPRI | POLLRDBAND))
 1602: 		if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
 1603: 			revents |= events & (POLLPRI | POLLRDBAND);
 1604: 
 1605: 	if (revents == 0) {
 1606: 		if (events &
 1607: 			(POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
 1608: 			 POLLRDBAND)) {
 1609: 			selrecord(td, &so->so_rcv.sb_sel);
 1610: 			so->so_rcv.sb_flags |= SB_SEL;
 1611: 		}
 1612: 
 1613: 		if (events & (POLLOUT | POLLWRNORM)) {
 1614: 			selrecord(td, &so->so_snd.sb_sel);
 1615: 			so->so_snd.sb_flags |= SB_SEL;
 1616: 		}
 1617: 	}
 1618: 
 1619: 	splx(s);
 1620: 	return (revents);
 1621: }
 1622: 
 1623: int
 1624: sokqfilter(struct file *fp, struct knote *kn)
 1625: {
 1626: 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
 1627: 	struct sockbuf *sb;
 1628: 	int s;
 1629: 
 1630: 	switch (kn->kn_filter) {
 1631: 	case EVFILT_READ:
 1632: 		if (so->so_options & SO_ACCEPTCONN)
 1633: 			kn->kn_fop = &solisten_filtops;
 1634: 		else
 1635: 			kn->kn_fop = &soread_filtops;
 1636: 		sb = &so->so_rcv;
 1637: 		break;
 1638: 	case EVFILT_WRITE:
 1639: 		kn->kn_fop = &sowrite_filtops;
 1640: 		sb = &so->so_snd;
 1641: 		break;
 1642: 	default:
 1643: 		return (1);
 1644: 	}
 1645: 
 1646: 	s = splnet();
 1647: 	SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
 1648: 	sb->sb_flags |= SB_KNOTE;
 1649: 	splx(s);
 1650: 	return (0);
 1651: }
 1652: 
 1653: static void
 1654: filt_sordetach(struct knote *kn)
 1655: {
 1656: 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
 1657: 	int s = splnet();
 1658: 
 1659: 	SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
 1660: 	if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
 1661: 		so->so_rcv.sb_flags &= ~SB_KNOTE;
 1662: 	splx(s);
 1663: }
 1664: 
 1665: /*ARGSUSED*/
 1666: static int
 1667: filt_soread(struct knote *kn, long hint)
 1668: {
 1669: 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
 1670: 
 1671: 	kn->kn_data = so->so_rcv.sb_cc;
 1672: 	if (so->so_state & SS_CANTRCVMORE) {
 1673: 		kn->kn_flags |= EV_EOF; 
 1674: 		kn->kn_fflags = so->so_error;
 1675: 		return (1);
 1676: 	}
 1677: 	if (so->so_error)	/* temporary udp error */
 1678: 		return (1);
 1679: 	if (kn->kn_sfflags & NOTE_LOWAT)
 1680: 		return (kn->kn_data >= kn->kn_sdata);
 1681: 	return (kn->kn_data >= so->so_rcv.sb_lowat);
 1682: }
 1683: 
 1684: static void
 1685: filt_sowdetach(struct knote *kn)
 1686: {
 1687: 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
 1688: 	int s = splnet();
 1689: 
 1690: 	SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
 1691: 	if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
 1692: 		so->so_snd.sb_flags &= ~SB_KNOTE;
 1693: 	splx(s);
 1694: }
 1695: 
 1696: /*ARGSUSED*/
 1697: static int
 1698: filt_sowrite(struct knote *kn, long hint)
 1699: {
 1700: 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
 1701: 
 1702: 	kn->kn_data = sbspace(&so->so_snd);
 1703: 	if (so->so_state & SS_CANTSENDMORE) {
 1704: 		kn->kn_flags |= EV_EOF; 
 1705: 		kn->kn_fflags = so->so_error;
 1706: 		return (1);
 1707: 	}
 1708: 	if (so->so_error)	/* temporary udp error */
 1709: 		return (1);
 1710: 	if (((so->so_state & SS_ISCONNECTED) == 0) &&
 1711: 	    (so->so_proto->pr_flags & PR_CONNREQUIRED))
 1712: 		return (0);
 1713: 	if (kn->kn_sfflags & NOTE_LOWAT)
 1714: 		return (kn->kn_data >= kn->kn_sdata);
 1715: 	return (kn->kn_data >= so->so_snd.sb_lowat);
 1716: }
 1717: 
 1718: /*ARGSUSED*/
 1719: static int
 1720: filt_solisten(struct knote *kn, long hint)
 1721: {
 1722: 	struct socket *so = (struct socket *)kn->kn_fp->f_data;
 1723: 
 1724: 	kn->kn_data = so->so_qlen;
 1725: 	return (! TAILQ_EMPTY(&so->so_comp));
 1726: }