[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_socket.c

Version: -  FREEBSD  -  FREEBSD8  -  FREEBSD7  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

    1 /*-
    2  * Copyright (c) 1982, 1986, 1988, 1990, 1993
    3  *      The Regents of the University of California.
    4  * Copyright (c) 2004 The FreeBSD Foundation
    5  * Copyright (c) 2004-2008 Robert N. M. Watson
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      @(#)uipc_socket.c       8.3 (Berkeley) 4/15/94
   33  */
   34 
   35 /*
   36  * Comments on the socket life cycle:
   37  *
   38  * soalloc() sets of socket layer state for a socket, called only by
   39  * socreate() and sonewconn().  Socket layer private.
   40  *
   41  * sodealloc() tears down socket layer state for a socket, called only by
   42  * sofree() and sonewconn().  Socket layer private.
   43  *
   44  * pru_attach() associates protocol layer state with an allocated socket;
   45  * called only once, may fail, aborting socket allocation.  This is called
   46  * from socreate() and sonewconn().  Socket layer private.
   47  *
   48  * pru_detach() disassociates protocol layer state from an attached socket,
   49  * and will be called exactly once for sockets in which pru_attach() has
   50  * been successfully called.  If pru_attach() returned an error,
   51  * pru_detach() will not be called.  Socket layer private.
   52  *
   53  * pru_abort() and pru_close() notify the protocol layer that the last
   54  * consumer of a socket is starting to tear down the socket, and that the
   55  * protocol should terminate the connection.  Historically, pru_abort() also
   56  * detached protocol state from the socket state, but this is no longer the
   57  * case.
   58  *
   59  * socreate() creates a socket and attaches protocol state.  This is a public
   60  * interface that may be used by socket layer consumers to create new
   61  * sockets.
   62  *
   63  * sonewconn() creates a socket and attaches protocol state.  This is a
   64  * public interface  that may be used by protocols to create new sockets when
   65  * a new connection is received and will be available for accept() on a
   66  * listen socket.
   67  *
   68  * soclose() destroys a socket after possibly waiting for it to disconnect.
   69  * This is a public interface that socket consumers should use to close and
   70  * release a socket when done with it.
   71  *
   72  * soabort() destroys a socket without waiting for it to disconnect (used
   73  * only for incoming connections that are already partially or fully
   74  * connected).  This is used internally by the socket layer when clearing
   75  * listen socket queues (due to overflow or close on the listen socket), but
   76  * is also a public interface protocols may use to abort connections in
   77  * their incomplete listen queues should they no longer be required.  Sockets
   78  * placed in completed connection listen queues should not be aborted for
   79  * reasons described in the comment above the soclose() implementation.  This
   80  * is not a general purpose close routine, and except in the specific
   81  * circumstances described here, should not be used.
   82  *
   83  * sofree() will free a socket and its protocol state if all references on
   84  * the socket have been released, and is the public interface to attempt to
   85  * free a socket when a reference is removed.  This is a socket layer private
   86  * interface.
   87  *
   88  * NOTE: In addition to socreate() and soclose(), which provide a single
   89  * socket reference to the consumer to be managed as required, there are two
   90  * calls to explicitly manage socket references, soref(), and sorele().
   91  * Currently, these are generally required only when transitioning a socket
   92  * from a listen queue to a file descriptor, in order to prevent garbage
   93  * collection of the socket at an untimely moment.  For a number of reasons,
   94  * these interfaces are not preferred, and should be avoided.
   95  */
   96 
   97 #include <sys/cdefs.h>
   98 __FBSDID("$FreeBSD$");
   99 
  100 #include "opt_inet.h"
  101 #include "opt_inet6.h"
  102 #include "opt_zero.h"
  103 #include "opt_compat.h"
  104 
  105 #include <sys/param.h>
  106 #include <sys/systm.h>
  107 #include <sys/fcntl.h>
  108 #include <sys/limits.h>
  109 #include <sys/lock.h>
  110 #include <sys/mac.h>
  111 #include <sys/malloc.h>
  112 #include <sys/mbuf.h>
  113 #include <sys/mutex.h>
  114 #include <sys/domain.h>
  115 #include <sys/file.h>                   /* for struct knote */
  116 #include <sys/kernel.h>
  117 #include <sys/event.h>
  118 #include <sys/eventhandler.h>
  119 #include <sys/poll.h>
  120 #include <sys/proc.h>
  121 #include <sys/protosw.h>
  122 #include <sys/socket.h>
  123 #include <sys/socketvar.h>
  124 #include <sys/resourcevar.h>
  125 #include <net/route.h>
  126 #include <sys/signalvar.h>
  127 #include <sys/stat.h>
  128 #include <sys/sx.h>
  129 #include <sys/sysctl.h>
  130 #include <sys/uio.h>
  131 #include <sys/jail.h>
  132 
  133 #include <net/vnet.h>
  134 
  135 #include <security/mac/mac_framework.h>
  136 
  137 #include <vm/uma.h>
  138 
  139 #ifdef COMPAT_IA32
  140 #include <sys/mount.h>
  141 #include <sys/sysent.h>
  142 #include <compat/freebsd32/freebsd32.h>
  143 #endif
  144 
  145 static int      soreceive_rcvoob(struct socket *so, struct uio *uio,
  146                     int flags);
  147 
  148 static void     filt_sordetach(struct knote *kn);
  149 static int      filt_soread(struct knote *kn, long hint);
  150 static void     filt_sowdetach(struct knote *kn);
  151 static int      filt_sowrite(struct knote *kn, long hint);
  152 static int      filt_solisten(struct knote *kn, long hint);
  153 
  154 static struct filterops solisten_filtops = {
  155         .f_isfd = 1,
  156         .f_detach = filt_sordetach,
  157         .f_event = filt_solisten,
  158 };
  159 static struct filterops soread_filtops = {
  160         .f_isfd = 1,
  161         .f_detach = filt_sordetach,
  162         .f_event = filt_soread,
  163 };
  164 static struct filterops sowrite_filtops = {
  165         .f_isfd = 1,
  166         .f_detach = filt_sowdetach,
  167         .f_event = filt_sowrite,
  168 };
  169 
  170 uma_zone_t socket_zone;
  171 so_gen_t        so_gencnt;      /* generation count for sockets */
  172 
  173 int     maxsockets;
  174 
  175 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
  176 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
  177 
  178 static int somaxconn = SOMAXCONN;
  179 static int sysctl_somaxconn(SYSCTL_HANDLER_ARGS);
  180 /* XXX: we dont have SYSCTL_USHORT */
  181 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLTYPE_UINT | CTLFLAG_RW,
  182     0, sizeof(int), sysctl_somaxconn, "I", "Maximum pending socket connection "
  183     "queue size");
  184 static int numopensockets;
  185 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
  186     &numopensockets, 0, "Number of open sockets");
  187 #ifdef ZERO_COPY_SOCKETS
  188 /* These aren't static because they're used in other files. */
  189 int so_zero_copy_send = 1;
  190 int so_zero_copy_receive = 1;
  191 SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0,
  192     "Zero copy controls");
  193 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW,
  194     &so_zero_copy_receive, 0, "Enable zero copy receive");
  195 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW,
  196     &so_zero_copy_send, 0, "Enable zero copy send");
  197 #endif /* ZERO_COPY_SOCKETS */
  198 
  199 /*
  200  * accept_mtx locks down per-socket fields relating to accept queues.  See
  201  * socketvar.h for an annotation of the protected fields of struct socket.
  202  */
  203 struct mtx accept_mtx;
  204 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
  205 
  206 /*
  207  * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
  208  * so_gencnt field.
  209  */
  210 static struct mtx so_global_mtx;
  211 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
  212 
  213 /*
  214  * General IPC sysctl name space, used by sockets and a variety of other IPC
  215  * types.
  216  */
  217 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
  218 
  219 /*
  220  * Sysctl to get and set the maximum global sockets limit.  Notify protocols
  221  * of the change so that they can update their dependent limits as required.
  222  */
  223 static int
  224 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
  225 {
  226         int error, newmaxsockets;
  227 
  228         newmaxsockets = maxsockets;
  229         error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
  230         if (error == 0 && req->newptr) {
  231                 if (newmaxsockets > maxsockets) {
  232                         maxsockets = newmaxsockets;
  233                         if (maxsockets > ((maxfiles / 4) * 3)) {
  234                                 maxfiles = (maxsockets * 5) / 4;
  235                                 maxfilesperproc = (maxfiles * 9) / 10;
  236                         }
  237                         EVENTHANDLER_INVOKE(maxsockets_change);
  238                 } else
  239                         error = EINVAL;
  240         }
  241         return (error);
  242 }
  243 
  244 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
  245     &maxsockets, 0, sysctl_maxsockets, "IU",
  246     "Maximum number of sockets avaliable");
  247 
  248 /*
  249  * Initialise maxsockets.  This SYSINIT must be run after
  250  * tunable_mbinit().
  251  */
  252 static void
  253 init_maxsockets(void *ignored)
  254 {
  255 
  256         TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
  257         maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
  258 }
  259 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
  260 
  261 /*
  262  * Socket operation routines.  These routines are called by the routines in
  263  * sys_socket.c or from a system process, and implement the semantics of
  264  * socket operations by switching out to the protocol specific routines.
  265  */
  266 
  267 /*
  268  * Get a socket structure from our zone, and initialize it.  Note that it
  269  * would probably be better to allocate socket and PCB at the same time, but
  270  * I'm not convinced that all the protocols can be easily modified to do
  271  * this.
  272  *
  273  * soalloc() returns a socket with a ref count of 0.
  274  */
  275 static struct socket *
  276 soalloc(struct vnet *vnet)
  277 {
  278         struct socket *so;
  279 
  280         so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
  281         if (so == NULL)
  282                 return (NULL);
  283 #ifdef MAC
  284         if (mac_socket_init(so, M_NOWAIT) != 0) {
  285                 uma_zfree(socket_zone, so);
  286                 return (NULL);
  287         }
  288 #endif
  289         SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
  290         SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
  291         sx_init(&so->so_snd.sb_sx, "so_snd_sx");
  292         sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
  293         TAILQ_INIT(&so->so_aiojobq);
  294         mtx_lock(&so_global_mtx);
  295         so->so_gencnt = ++so_gencnt;
  296         ++numopensockets;
  297 #ifdef VIMAGE
  298         vnet->vnet_sockcnt++;
  299         so->so_vnet = vnet;
  300 #endif
  301         mtx_unlock(&so_global_mtx);
  302         return (so);
  303 }
  304 
  305 /*
  306  * Free the storage associated with a socket at the socket layer, tear down
  307  * locks, labels, etc.  All protocol state is assumed already to have been
  308  * torn down (and possibly never set up) by the caller.
  309  */
  310 static void
  311 sodealloc(struct socket *so)
  312 {
  313 
  314         KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
  315         KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
  316 
  317         mtx_lock(&so_global_mtx);
  318         so->so_gencnt = ++so_gencnt;
  319         --numopensockets;       /* Could be below, but faster here. */
  320 #ifdef VIMAGE
  321         so->so_vnet->vnet_sockcnt--;
  322 #endif
  323         mtx_unlock(&so_global_mtx);
  324         if (so->so_rcv.sb_hiwat)
  325                 (void)chgsbsize(so->so_cred->cr_uidinfo,
  326                     &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
  327         if (so->so_snd.sb_hiwat)
  328                 (void)chgsbsize(so->so_cred->cr_uidinfo,
  329                     &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
  330 #ifdef INET
  331         /* remove acccept filter if one is present. */
  332         if (so->so_accf != NULL)
  333                 do_setopt_accept_filter(so, NULL);
  334 #endif
  335 #ifdef MAC
  336         mac_socket_destroy(so);
  337 #endif
  338         crfree(so->so_cred);
  339         sx_destroy(&so->so_snd.sb_sx);
  340         sx_destroy(&so->so_rcv.sb_sx);
  341         SOCKBUF_LOCK_DESTROY(&so->so_snd);
  342         SOCKBUF_LOCK_DESTROY(&so->so_rcv);
  343         uma_zfree(socket_zone, so);
  344 }
  345 
  346 /*
  347  * socreate returns a socket with a ref count of 1.  The socket should be
  348  * closed with soclose().
  349  */
  350 int
  351 socreate(int dom, struct socket **aso, int type, int proto,
  352     struct ucred *cred, struct thread *td)
  353 {
  354         struct protosw *prp;
  355         struct socket *so;
  356         int error;
  357 
  358         if (proto)
  359                 prp = pffindproto(dom, proto, type);
  360         else
  361                 prp = pffindtype(dom, type);
  362 
  363         if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
  364             prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
  365                 return (EPROTONOSUPPORT);
  366 
  367         if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
  368                 return (EPROTONOSUPPORT);
  369 
  370         if (prp->pr_type != type)
  371                 return (EPROTOTYPE);
  372         so = soalloc(CRED_TO_VNET(cred));
  373         if (so == NULL)
  374                 return (ENOBUFS);
  375 
  376         TAILQ_INIT(&so->so_incomp);
  377         TAILQ_INIT(&so->so_comp);
  378         so->so_type = type;
  379         so->so_cred = crhold(cred);
  380         if ((prp->pr_domain->dom_family == PF_INET) ||
  381             (prp->pr_domain->dom_family == PF_ROUTE))
  382                 so->so_fibnum = td->td_proc->p_fibnum;
  383         else
  384                 so->so_fibnum = 0;
  385         so->so_proto = prp;
  386 #ifdef MAC
  387         mac_socket_create(cred, so);
  388 #endif
  389         knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
  390         knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
  391         so->so_count = 1;
  392         /*
  393          * Auto-sizing of socket buffers is managed by the protocols and
  394          * the appropriate flags must be set in the pru_attach function.
  395          */
  396         CURVNET_SET(so->so_vnet);
  397         error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
  398         CURVNET_RESTORE();
  399         if (error) {
  400                 KASSERT(so->so_count == 1, ("socreate: so_count %d",
  401                     so->so_count));
  402                 so->so_count = 0;
  403                 sodealloc(so);
  404                 return (error);
  405         }
  406         *aso = so;
  407         return (0);
  408 }
  409 
  410 #ifdef REGRESSION
  411 static int regression_sonewconn_earlytest = 1;
  412 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
  413     &regression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
  414 #endif
  415 
  416 /*
  417  * When an attempt at a new connection is noted on a socket which accepts
  418  * connections, sonewconn is called.  If the connection is possible (subject
  419  * to space constraints, etc.) then we allocate a new structure, propoerly
  420  * linked into the data structure of the original socket, and return this.
  421  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
  422  *
  423  * Note: the ref count on the socket is 0 on return.
  424  */
  425 struct socket *
  426 sonewconn(struct socket *head, int connstatus)
  427 {
  428         struct socket *so;
  429         int over;
  430 
  431         ACCEPT_LOCK();
  432         over = (head->so_qlen > 3 * head->so_qlimit / 2);
  433         ACCEPT_UNLOCK();
  434 #ifdef REGRESSION
  435         if (regression_sonewconn_earlytest && over)
  436 #else
  437         if (over)
  438 #endif
  439                 return (NULL);
  440         VNET_ASSERT(head->so_vnet);
  441         so = soalloc(head->so_vnet);
  442         if (so == NULL)
  443                 return (NULL);
  444         if ((head->so_options & SO_ACCEPTFILTER) != 0)
  445                 connstatus = 0;
  446         so->so_head = head;
  447         so->so_type = head->so_type;
  448         so->so_options = head->so_options &~ SO_ACCEPTCONN;
  449         so->so_linger = head->so_linger;
  450         so->so_state = head->so_state | SS_NOFDREF;
  451         so->so_fibnum = head->so_fibnum;
  452         so->so_proto = head->so_proto;
  453         so->so_cred = crhold(head->so_cred);
  454 #ifdef MAC
  455         mac_socket_newconn(head, so);
  456 #endif
  457         knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
  458         knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
  459         if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
  460             (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
  461                 sodealloc(so);
  462                 return (NULL);
  463         }
  464         so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
  465         so->so_snd.sb_lowat = head->so_snd.sb_lowat;
  466         so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
  467         so->so_snd.sb_timeo = head->so_snd.sb_timeo;
  468         so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
  469         so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
  470         so->so_state |= connstatus;
  471         ACCEPT_LOCK();
  472         if (connstatus) {
  473                 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
  474                 so->so_qstate |= SQ_COMP;
  475                 head->so_qlen++;
  476         } else {
  477                 /*
  478                  * Keep removing sockets from the head until there's room for
  479                  * us to insert on the tail.  In pre-locking revisions, this
  480                  * was a simple if(), but as we could be racing with other
  481                  * threads and soabort() requires dropping locks, we must
  482                  * loop waiting for the condition to be true.
  483                  */
  484                 while (head->so_incqlen > head->so_qlimit) {
  485                         struct socket *sp;
  486                         sp = TAILQ_FIRST(&head->so_incomp);
  487                         TAILQ_REMOVE(&head->so_incomp, sp, so_list);
  488                         head->so_incqlen--;
  489                         sp->so_qstate &= ~SQ_INCOMP;
  490                         sp->so_head = NULL;
  491                         ACCEPT_UNLOCK();
  492                         soabort(sp);
  493                         ACCEPT_LOCK();
  494                 }
  495                 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
  496                 so->so_qstate |= SQ_INCOMP;
  497                 head->so_incqlen++;
  498         }
  499         ACCEPT_UNLOCK();
  500         if (connstatus) {
  501                 sorwakeup(head);
  502                 wakeup_one(&head->so_timeo);
  503         }
  504         return (so);
  505 }
  506 
  507 int
  508 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
  509 {
  510         int error;
  511 
  512         CURVNET_SET(so->so_vnet);
  513         error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
  514         CURVNET_RESTORE();
  515         return error;
  516 }
  517 
  518 /*
  519  * solisten() transitions a socket from a non-listening state to a listening
  520  * state, but can also be used to update the listen queue depth on an
  521  * existing listen socket.  The protocol will call back into the sockets
  522  * layer using solisten_proto_check() and solisten_proto() to check and set
  523  * socket-layer listen state.  Call backs are used so that the protocol can
  524  * acquire both protocol and socket layer locks in whatever order is required
  525  * by the protocol.
  526  *
  527  * Protocol implementors are advised to hold the socket lock across the
  528  * socket-layer test and set to avoid races at the socket layer.
  529  */
  530 int
  531 solisten(struct socket *so, int backlog, struct thread *td)
  532 {
  533 
  534         return ((*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td));
  535 }
  536 
  537 int
  538 solisten_proto_check(struct socket *so)
  539 {
  540 
  541         SOCK_LOCK_ASSERT(so);
  542 
  543         if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
  544             SS_ISDISCONNECTING))
  545                 return (EINVAL);
  546         return (0);
  547 }
  548 
  549 void
  550 solisten_proto(struct socket *so, int backlog)
  551 {
  552 
  553         SOCK_LOCK_ASSERT(so);
  554 
  555         if (backlog < 0 || backlog > somaxconn)
  556                 backlog = somaxconn;
  557         so->so_qlimit = backlog;
  558         so->so_options |= SO_ACCEPTCONN;
  559 }
  560 
  561 /*
  562  * Attempt to free a socket.  This should really be sotryfree().
  563  *
  564  * sofree() will succeed if:
  565  *
  566  * - There are no outstanding file descriptor references or related consumers
  567  *   (so_count == 0).
  568  *
  569  * - The socket has been closed by user space, if ever open (SS_NOFDREF).
  570  *
  571  * - The protocol does not have an outstanding strong reference on the socket
  572  *   (SS_PROTOREF).
  573  *
  574  * - The socket is not in a completed connection queue, so a process has been
  575  *   notified that it is present.  If it is removed, the user process may
  576  *   block in accept() despite select() saying the socket was ready.
  577  *
  578  * Otherwise, it will quietly abort so that a future call to sofree(), when
  579  * conditions are right, can succeed.
  580  */
  581 void
  582 sofree(struct socket *so)
  583 {
  584         struct protosw *pr = so->so_proto;
  585         struct socket *head;
  586 
  587         ACCEPT_LOCK_ASSERT();
  588         SOCK_LOCK_ASSERT(so);
  589 
  590         if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
  591             (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
  592                 SOCK_UNLOCK(so);
  593                 ACCEPT_UNLOCK();
  594                 return;
  595         }
  596 
  597         head = so->so_head;
  598         if (head != NULL) {
  599                 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
  600                     (so->so_qstate & SQ_INCOMP) != 0,
  601                     ("sofree: so_head != NULL, but neither SQ_COMP nor "
  602                     "SQ_INCOMP"));
  603                 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
  604                     (so->so_qstate & SQ_INCOMP) == 0,
  605                     ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
  606                 TAILQ_REMOVE(&head->so_incomp, so, so_list);
  607                 head->so_incqlen--;
  608                 so->so_qstate &= ~SQ_INCOMP;
  609                 so->so_head = NULL;
  610         }
  611         KASSERT((so->so_qstate & SQ_COMP) == 0 &&
  612             (so->so_qstate & SQ_INCOMP) == 0,
  613             ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
  614             so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
  615         if (so->so_options & SO_ACCEPTCONN) {
  616                 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
  617                 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated"));
  618         }
  619         SOCK_UNLOCK(so);
  620         ACCEPT_UNLOCK();
  621 
  622         if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
  623                 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
  624         if (pr->pr_usrreqs->pru_detach != NULL)
  625                 (*pr->pr_usrreqs->pru_detach)(so);
  626 
  627         /*
  628          * From this point on, we assume that no other references to this
  629          * socket exist anywhere else in the stack.  Therefore, no locks need
  630          * to be acquired or held.
  631          *
  632          * We used to do a lot of socket buffer and socket locking here, as
  633          * well as invoke sorflush() and perform wakeups.  The direct call to
  634          * dom_dispose() and sbrelease_internal() are an inlining of what was
  635          * necessary from sorflush().
  636          *
  637          * Notice that the socket buffer and kqueue state are torn down
  638          * before calling pru_detach.  This means that protocols shold not
  639          * assume they can perform socket wakeups, etc, in their detach code.
  640          */
  641         sbdestroy(&so->so_snd, so);
  642         sbdestroy(&so->so_rcv, so);
  643         knlist_destroy(&so->so_rcv.sb_sel.si_note);
  644         knlist_destroy(&so->so_snd.sb_sel.si_note);
  645         sodealloc(so);
  646 }
  647 
  648 /*
  649  * Close a socket on last file table reference removal.  Initiate disconnect
  650  * if connected.  Free socket when disconnect complete.
  651  *
  652  * This function will sorele() the socket.  Note that soclose() may be called
  653  * prior to the ref count reaching zero.  The actual socket structure will
  654  * not be freed until the ref count reaches zero.
  655  */
  656 int
  657 soclose(struct socket *so)
  658 {
  659         int error = 0;
  660 
  661         KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
  662 
  663         CURVNET_SET(so->so_vnet);
  664         funsetown(&so->so_sigio);
  665         if (so->so_state & SS_ISCONNECTED) {
  666                 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
  667                         error = sodisconnect(so);
  668                         if (error)
  669                                 goto drop;
  670                 }
  671                 if (so->so_options & SO_LINGER) {
  672                         if ((so->so_state & SS_ISDISCONNECTING) &&
  673                             (so->so_state & SS_NBIO))
  674                                 goto drop;
  675                         while (so->so_state & SS_ISCONNECTED) {
  676                                 error = tsleep(&so->so_timeo,
  677                                     PSOCK | PCATCH, "soclos", so->so_linger * hz);
  678                                 if (error)
  679                                         break;
  680                         }
  681                 }
  682         }
  683 
  684 drop:
  685         if (so->so_proto->pr_usrreqs->pru_close != NULL)
  686                 (*so->so_proto->pr_usrreqs->pru_close)(so);
  687         if (so->so_options & SO_ACCEPTCONN) {
  688                 struct socket *sp;
  689                 ACCEPT_LOCK();
  690                 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
  691                         TAILQ_REMOVE(&so->so_incomp, sp, so_list);
  692                         so->so_incqlen--;
  693                         sp->so_qstate &= ~SQ_INCOMP;
  694                         sp->so_head = NULL;
  695                         ACCEPT_UNLOCK();
  696                         soabort(sp);
  697                         ACCEPT_LOCK();
  698                 }
  699                 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
  700                         TAILQ_REMOVE(&so->so_comp, sp, so_list);
  701                         so->so_qlen--;
  702                         sp->so_qstate &= ~SQ_COMP;
  703                         sp->so_head = NULL;
  704                         ACCEPT_UNLOCK();
  705                         soabort(sp);
  706                         ACCEPT_LOCK();
  707                 }
  708                 ACCEPT_UNLOCK();
  709         }
  710         ACCEPT_LOCK();
  711         SOCK_LOCK(so);
  712         KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
  713         so->so_state |= SS_NOFDREF;
  714         sorele(so);
  715         CURVNET_RESTORE();
  716         return (error);
  717 }
  718 
  719 /*
  720  * soabort() is used to abruptly tear down a connection, such as when a
  721  * resource limit is reached (listen queue depth exceeded), or if a listen
  722  * socket is closed while there are sockets waiting to be accepted.
  723  *
  724  * This interface is tricky, because it is called on an unreferenced socket,
  725  * and must be called only by a thread that has actually removed the socket
  726  * from the listen queue it was on, or races with other threads are risked.
  727  *
  728  * This interface will call into the protocol code, so must not be called
  729  * with any socket locks held.  Protocols do call it while holding their own
  730  * recursible protocol mutexes, but this is something that should be subject
  731  * to review in the future.
  732  */
  733 void
  734 soabort(struct socket *so)
  735 {
  736 
  737         /*
  738          * In as much as is possible, assert that no references to this
  739          * socket are held.  This is not quite the same as asserting that the
  740          * current thread is responsible for arranging for no references, but
  741          * is as close as we can get for now.
  742          */
  743         KASSERT(so->so_count == 0, ("soabort: so_count"));
  744         KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
  745         KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
  746         KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
  747         KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
  748 
  749         if (so->so_proto->pr_usrreqs->pru_abort != NULL)
  750                 (*so->so_proto->pr_usrreqs->pru_abort)(so);
  751         ACCEPT_LOCK();
  752         SOCK_LOCK(so);
  753         sofree(so);
  754 }
  755 
  756 int
  757 soaccept(struct socket *so, struct sockaddr **nam)
  758 {
  759         int error;
  760 
  761         SOCK_LOCK(so);
  762         KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
  763         so->so_state &= ~SS_NOFDREF;
  764         SOCK_UNLOCK(so);
  765         error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
  766         return (error);
  767 }
  768 
  769 int
  770 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
  771 {
  772         int error;
  773 
  774         if (so->so_options & SO_ACCEPTCONN)
  775                 return (EOPNOTSUPP);
  776         /*
  777          * If protocol is connection-based, can only connect once.
  778          * Otherwise, if connected, try to disconnect first.  This allows
  779          * user to disconnect by connecting to, e.g., a null address.
  780          */
  781         if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
  782             ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
  783             (error = sodisconnect(so)))) {
  784                 error = EISCONN;
  785         } else {
  786                 /*
  787                  * Prevent accumulated error from previous connection from
  788                  * biting us.
  789                  */
  790                 so->so_error = 0;
  791                 CURVNET_SET(so->so_vnet);
  792                 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
  793                 CURVNET_RESTORE();
  794         }
  795 
  796         return (error);
  797 }
  798 
  799 int
  800 soconnect2(struct socket *so1, struct socket *so2)
  801 {
  802 
  803         return ((*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2));
  804 }
  805 
  806 int
  807 sodisconnect(struct socket *so)
  808 {
  809         int error;
  810 
  811         if ((so->so_state & SS_ISCONNECTED) == 0)
  812                 return (ENOTCONN);
  813         if (so->so_state & SS_ISDISCONNECTING)
  814                 return (EALREADY);
  815         error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
  816         return (error);
  817 }
  818 
  819 #ifdef ZERO_COPY_SOCKETS
  820 struct so_zerocopy_stats{
  821         int size_ok;
  822         int align_ok;
  823         int found_ifp;
  824 };
  825 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
  826 #include <netinet/in.h>
  827 #include <net/route.h>
  828 #include <netinet/in_pcb.h>
  829 #include <vm/vm.h>
  830 #include <vm/vm_page.h>
  831 #include <vm/vm_object.h>
  832 
  833 /*
  834  * sosend_copyin() is only used if zero copy sockets are enabled.  Otherwise
  835  * sosend_dgram() and sosend_generic() use m_uiotombuf().
  836  * 
  837  * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or
  838  * all of the data referenced by the uio.  If desired, it uses zero-copy.
  839  * *space will be updated to reflect data copied in.
  840  *
  841  * NB: If atomic I/O is requested, the caller must already have checked that
  842  * space can hold resid bytes.
  843  *
  844  * NB: In the event of an error, the caller may need to free the partial
  845  * chain pointed to by *mpp.  The contents of both *uio and *space may be
  846  * modified even in the case of an error.
  847  */
  848 static int
  849 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space,
  850     int flags)
  851 {
  852         struct mbuf *m, **mp, *top;
  853         long len, resid;
  854         int error;
  855 #ifdef ZERO_COPY_SOCKETS
  856         int cow_send;
  857 #endif
  858 
  859         *retmp = top = NULL;
  860         mp = &top;
  861         len = 0;
  862         resid = uio->uio_resid;
  863         error = 0;
  864         do {
  865 #ifdef ZERO_COPY_SOCKETS
  866                 cow_send = 0;
  867 #endif /* ZERO_COPY_SOCKETS */
  868                 if (resid >= MINCLSIZE) {
  869 #ifdef ZERO_COPY_SOCKETS
  870                         if (top == NULL) {
  871                                 m = m_gethdr(M_WAITOK, MT_DATA);
  872                                 m->m_pkthdr.len = 0;
  873                                 m->m_pkthdr.rcvif = NULL;
  874                         } else
  875                                 m = m_get(M_WAITOK, MT_DATA);
  876                         if (so_zero_copy_send &&
  877                             resid>=PAGE_SIZE &&
  878                             *space>=PAGE_SIZE &&
  879                             uio->uio_iov->iov_len>=PAGE_SIZE) {
  880                                 so_zerocp_stats.size_ok++;
  881                                 so_zerocp_stats.align_ok++;
  882                                 cow_send = socow_setup(m, uio);
  883                                 len = cow_send;
  884                         }
  885                         if (!cow_send) {
  886                                 m_clget(m, M_WAITOK);
  887                                 len = min(min(MCLBYTES, resid), *space);
  888                         }
  889 #else /* ZERO_COPY_SOCKETS */
  890                         if (top == NULL) {
  891                                 m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
  892                                 m->m_pkthdr.len = 0;
  893                                 m->m_pkthdr.rcvif = NULL;
  894                         } else
  895                                 m = m_getcl(M_WAIT, MT_DATA, 0);
  896                         len = min(min(MCLBYTES, resid), *space);
  897 #endif /* ZERO_COPY_SOCKETS */
  898                 } else {
  899                         if (top == NULL) {
  900                                 m = m_gethdr(M_WAIT, MT_DATA);
  901                                 m->m_pkthdr.len = 0;
  902                                 m->m_pkthdr.rcvif = NULL;
  903 
  904                                 len = min(min(MHLEN, resid), *space);
  905                                 /*
  906                                  * For datagram protocols, leave room
  907                                  * for protocol headers in first mbuf.
  908                                  */
  909                                 if (atomic && m && len < MHLEN)
  910                                         MH_ALIGN(m, len);
  911                         } else {
  912                                 m = m_get(M_WAIT, MT_DATA);
  913                                 len = min(min(MLEN, resid), *space);
  914                         }
  915                 }
  916                 if (m == NULL) {
  917                         error = ENOBUFS;
  918                         goto out;
  919                 }
  920 
  921                 *space -= len;
  922 #ifdef ZERO_COPY_SOCKETS
  923                 if (cow_send)
  924                         error = 0;
  925                 else
  926 #endif /* ZERO_COPY_SOCKETS */
  927                 error = uiomove(mtod(m, void *), (int)len, uio);
  928                 resid = uio->uio_resid;
  929                 m->m_len = len;
  930                 *mp = m;
  931                 top->m_pkthdr.len += len;
  932                 if (error)
  933                         goto out;
  934                 mp = &m->m_next;
  935                 if (resid <= 0) {
  936                         if (flags & MSG_EOR)
  937                                 top->m_flags |= M_EOR;
  938                         break;
  939                 }
  940         } while (*space > 0 && atomic);
  941 out:
  942         *retmp = top;
  943         return (error);
  944 }
  945 #endif /*ZERO_COPY_SOCKETS*/
  946 
  947 #define SBLOCKWAIT(f)   (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
  948 
  949 int
  950 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
  951     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
  952 {
  953         long space, resid;
  954         int clen = 0, error, dontroute;
  955 #ifdef ZERO_COPY_SOCKETS
  956         int atomic = sosendallatonce(so) || top;
  957 #endif
  958 
  959         KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
  960         KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
  961             ("sodgram_send: !PR_ATOMIC"));
  962 
  963         if (uio != NULL)
  964                 resid = uio->uio_resid;
  965         else
  966                 resid = top->m_pkthdr.len;
  967         /*
  968          * In theory resid should be unsigned.  However, space must be
  969          * signed, as it might be less than 0 if we over-committed, and we
  970          * must use a signed comparison of space and resid.  On the other
  971          * hand, a negative resid causes us to loop sending 0-length
  972          * segments to the protocol.
  973          */
  974         if (resid < 0) {
  975                 error = EINVAL;
  976                 goto out;
  977         }
  978 
  979         dontroute =
  980             (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
  981         if (td != NULL)
  982                 td->td_ru.ru_msgsnd++;
  983         if (control != NULL)
  984                 clen = control->m_len;
  985 
  986         SOCKBUF_LOCK(&so->so_snd);
  987         if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
  988                 SOCKBUF_UNLOCK(&so->so_snd);
  989                 error = EPIPE;
  990                 goto out;
  991         }
  992         if (so->so_error) {
  993                 error = so->so_error;
  994                 so->so_error = 0;
  995                 SOCKBUF_UNLOCK(&so->so_snd);
  996                 goto out;
  997         }
  998         if ((so->so_state & SS_ISCONNECTED) == 0) {
  999                 /*
 1000                  * `sendto' and `sendmsg' is allowed on a connection-based
 1001                  * socket if it supports implied connect.  Return ENOTCONN if
 1002                  * not connected and no address is supplied.
 1003                  */
 1004                 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
 1005                     (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
 1006                         if ((so->so_state & SS_ISCONFIRMING) == 0 &&
 1007                             !(resid == 0 && clen != 0)) {
 1008                                 SOCKBUF_UNLOCK(&so->so_snd);
 1009                                 error = ENOTCONN;
 1010                                 goto out;
 1011                         }
 1012                 } else if (addr == NULL) {
 1013                         if (so->so_proto->pr_flags & PR_CONNREQUIRED)
 1014                                 error = ENOTCONN;
 1015                         else
 1016                                 error = EDESTADDRREQ;
 1017                         SOCKBUF_UNLOCK(&so->so_snd);
 1018                         goto out;
 1019                 }
 1020         }
 1021 
 1022         /*
 1023          * Do we need MSG_OOB support in SOCK_DGRAM?  Signs here may be a
 1024          * problem and need fixing.
 1025          */
 1026         space = sbspace(&so->so_snd);
 1027         if (flags & MSG_OOB)
 1028                 space += 1024;
 1029         space -= clen;
 1030         SOCKBUF_UNLOCK(&so->so_snd);
 1031         if (resid > space) {
 1032                 error = EMSGSIZE;
 1033                 goto out;
 1034         }
 1035         if (uio == NULL) {
 1036                 resid = 0;
 1037                 if (flags & MSG_EOR)
 1038                         top->m_flags |= M_EOR;
 1039         } else {
 1040 #ifdef ZERO_COPY_SOCKETS
 1041                 error = sosend_copyin(uio, &top, atomic, &space, flags);
 1042                 if (error)
 1043                         goto out;
 1044 #else
 1045                 /*
 1046                  * Copy the data from userland into a mbuf chain.
 1047                  * If no data is to be copied in, a single empty mbuf
 1048                  * is returned.
 1049                  */
 1050                 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
 1051                     (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
 1052                 if (top == NULL) {
 1053                         error = EFAULT; /* only possible error */
 1054                         goto out;
 1055                 }
 1056                 space -= resid - uio->uio_resid;
 1057 #endif
 1058                 resid = uio->uio_resid;
 1059         }
 1060         KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
 1061         /*
 1062          * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
 1063          * than with.
 1064          */
 1065         if (dontroute) {
 1066                 SOCK_LOCK(so);
 1067                 so->so_options |= SO_DONTROUTE;
 1068                 SOCK_UNLOCK(so);
 1069         }
 1070         /*
 1071          * XXX all the SBS_CANTSENDMORE checks previously done could be out
 1072          * of date.  We could have recieved a reset packet in an interrupt or
 1073          * maybe we slept while doing page faults in uiomove() etc.  We could
 1074          * probably recheck again inside the locking protection here, but
 1075          * there are probably other places that this also happens.  We must
 1076          * rethink this.
 1077          */
 1078         error = (*so->so_proto->pr_usrreqs->pru_send)(so,
 1079             (flags & MSG_OOB) ? PRUS_OOB :
 1080         /*
 1081          * If the user set MSG_EOF, the protocol understands this flag and
 1082          * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
 1083          */
 1084             ((flags & MSG_EOF) &&
 1085              (so->so_proto->pr_flags & PR_IMPLOPCL) &&
 1086              (resid <= 0)) ?
 1087                 PRUS_EOF :
 1088                 /* If there is more to send set PRUS_MORETOCOME */
 1089                 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
 1090                 top, addr, control, td);
 1091         if (dontroute) {
 1092                 SOCK_LOCK(so);
 1093                 so->so_options &= ~SO_DONTROUTE;
 1094                 SOCK_UNLOCK(so);
 1095         }
 1096         clen = 0;
 1097         control = NULL;
 1098         top = NULL;
 1099 out:
 1100         if (top != NULL)
 1101                 m_freem(top);
 1102         if (control != NULL)
 1103                 m_freem(control);
 1104         return (error);
 1105 }
 1106 
 1107 /*
 1108  * Send on a socket.  If send must go all at once and message is larger than
 1109  * send buffering, then hard error.  Lock against other senders.  If must go
 1110  * all at once and not enough room now, then inform user that this would
 1111  * block and do nothing.  Otherwise, if nonblocking, send as much as
 1112  * possible.  The data to be sent is described by "uio" if nonzero, otherwise
 1113  * by the mbuf chain "top" (which must be null if uio is not).  Data provided
 1114  * in mbuf chain must be small enough to send all at once.
 1115  *
 1116  * Returns nonzero on error, timeout or signal; callers must check for short
 1117  * counts if EINTR/ERESTART are returned.  Data and control buffers are freed
 1118  * on return.
 1119  */
 1120 int
 1121 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
 1122     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
 1123 {
 1124         long space, resid;
 1125         int clen = 0, error, dontroute;
 1126         int atomic = sosendallatonce(so) || top;
 1127 
 1128         if (uio != NULL)
 1129                 resid = uio->uio_resid;
 1130         else
 1131                 resid = top->m_pkthdr.len;
 1132         /*
 1133          * In theory resid should be unsigned.  However, space must be
 1134          * signed, as it might be less than 0 if we over-committed, and we
 1135          * must use a signed comparison of space and resid.  On the other
 1136          * hand, a negative resid causes us to loop sending 0-length
 1137          * segments to the protocol.
 1138          *
 1139          * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
 1140          * type sockets since that's an error.
 1141          */
 1142         if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
 1143                 error = EINVAL;
 1144                 goto out;
 1145         }
 1146 
 1147         dontroute =
 1148             (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
 1149             (so->so_proto->pr_flags & PR_ATOMIC);
 1150         if (td != NULL)
 1151                 td->td_ru.ru_msgsnd++;
 1152         if (control != NULL)
 1153                 clen = control->m_len;
 1154 
 1155         error = sblock(&so->so_snd, SBLOCKWAIT(flags));
 1156         if (error)
 1157                 goto out;
 1158 
 1159 restart:
 1160         do {
 1161                 SOCKBUF_LOCK(&so->so_snd);
 1162                 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
 1163                         SOCKBUF_UNLOCK(&so->so_snd);
 1164                         error = EPIPE;
 1165                         goto release;
 1166                 }
 1167                 if (so->so_error) {
 1168                         error = so->so_error;
 1169                         so->so_error = 0;
 1170                         SOCKBUF_UNLOCK(&so->so_snd);
 1171                         goto release;
 1172                 }
 1173                 if ((so->so_state & SS_ISCONNECTED) == 0) {
 1174                         /*
 1175                          * `sendto' and `sendmsg' is allowed on a connection-
 1176                          * based socket if it supports implied connect.
 1177                          * Return ENOTCONN if not connected and no address is
 1178                          * supplied.
 1179                          */
 1180                         if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
 1181                             (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
 1182                                 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
 1183                                     !(resid == 0 && clen != 0)) {
 1184                                         SOCKBUF_UNLOCK(&so->so_snd);
 1185                                         error = ENOTCONN;
 1186                                         goto release;
 1187                                 }
 1188                         } else if (addr == NULL) {
 1189                                 SOCKBUF_UNLOCK(&so->so_snd);
 1190                                 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
 1191                                         error = ENOTCONN;
 1192                                 else
 1193                                         error = EDESTADDRREQ;
 1194                                 goto release;
 1195                         }
 1196                 }
 1197                 space = sbspace(&so->so_snd);
 1198                 if (flags & MSG_OOB)
 1199                         space += 1024;
 1200                 if ((atomic && resid > so->so_snd.sb_hiwat) ||
 1201                     clen > so->so_snd.sb_hiwat) {
 1202                         SOCKBUF_UNLOCK(&so->so_snd);
 1203                         error = EMSGSIZE;
 1204                         goto release;
 1205                 }
 1206                 if (space < resid + clen &&
 1207                     (atomic || space < so->so_snd.sb_lowat || space < clen)) {
 1208                         if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
 1209                                 SOCKBUF_UNLOCK(&so->so_snd);
 1210                                 error = EWOULDBLOCK;
 1211                                 goto release;
 1212                         }
 1213                         error = sbwait(&so->so_snd);
 1214                         SOCKBUF_UNLOCK(&so->so_snd);
 1215                         if (error)
 1216                                 goto release;
 1217                         goto restart;
 1218                 }
 1219                 SOCKBUF_UNLOCK(&so->so_snd);
 1220                 space -= clen;
 1221                 do {
 1222                         if (uio == NULL) {
 1223                                 resid = 0;
 1224                                 if (flags & MSG_EOR)
 1225                                         top->m_flags |= M_EOR;
 1226                         } else {
 1227 #ifdef ZERO_COPY_SOCKETS
 1228                                 error = sosend_copyin(uio, &top, atomic,
 1229                                     &space, flags);
 1230                                 if (error != 0)
 1231                                         goto release;
 1232 #else
 1233                                 /*
 1234                                  * Copy the data from userland into a mbuf
 1235                                  * chain.  If no data is to be copied in,
 1236                                  * a single empty mbuf is returned.
 1237                                  */
 1238                                 top = m_uiotombuf(uio, M_WAITOK, space,
 1239                                     (atomic ? max_hdr : 0),
 1240                                     (atomic ? M_PKTHDR : 0) |
 1241                                     ((flags & MSG_EOR) ? M_EOR : 0));
 1242                                 if (top == NULL) {
 1243                                         error = EFAULT; /* only possible error */
 1244                                         goto release;
 1245                                 }
 1246                                 space -= resid - uio->uio_resid;
 1247 #endif
 1248                                 resid = uio->uio_resid;
 1249                         }
 1250                         if (dontroute) {
 1251                                 SOCK_LOCK(so);
 1252                                 so->so_options |= SO_DONTROUTE;
 1253                                 SOCK_UNLOCK(so);
 1254                         }
 1255                         /*
 1256                          * XXX all the SBS_CANTSENDMORE checks previously
 1257                          * done could be out of date.  We could have recieved
 1258                          * a reset packet in an interrupt or maybe we slept
 1259                          * while doing page faults in uiomove() etc.  We
 1260                          * could probably recheck again inside the locking
 1261                          * protection here, but there are probably other
 1262                          * places that this also happens.  We must rethink
 1263                          * this.
 1264                          */
 1265                         error = (*so->so_proto->pr_usrreqs->pru_send)(so,
 1266                             (flags & MSG_OOB) ? PRUS_OOB :
 1267                         /*
 1268                          * If the user set MSG_EOF, the protocol understands
 1269                          * this flag and nothing left to send then use
 1270                          * PRU_SEND_EOF instead of PRU_SEND.
 1271                          */
 1272                             ((flags & MSG_EOF) &&
 1273                              (so->so_proto->pr_flags & PR_IMPLOPCL) &&
 1274                              (resid <= 0)) ?
 1275                                 PRUS_EOF :
 1276                         /* If there is more to send set PRUS_MORETOCOME. */
 1277                             (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
 1278                             top, addr, control, td);
 1279                         if (dontroute) {
 1280                                 SOCK_LOCK(so);
 1281                                 so->so_options &= ~SO_DONTROUTE;
 1282                                 SOCK_UNLOCK(so);
 1283                         }
 1284                         clen = 0;
 1285                         control = NULL;
 1286                         top = NULL;
 1287                         if (error)
 1288                                 goto release;
 1289                 } while (resid && space > 0);
 1290         } while (resid);
 1291 
 1292 release:
 1293         sbunlock(&so->so_snd);
 1294 out:
 1295         if (top != NULL)
 1296                 m_freem(top);
 1297         if (control != NULL)
 1298                 m_freem(control);
 1299         return (error);
 1300 }
 1301 
 1302 int
 1303 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
 1304     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
 1305 {
 1306         int error;
 1307 
 1308         CURVNET_SET(so->so_vnet);
 1309         error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
 1310             control, flags, td);
 1311         CURVNET_RESTORE();
 1312         return (error);
 1313 }
 1314 
 1315 /*
 1316  * The part of soreceive() that implements reading non-inline out-of-band
 1317  * data from a socket.  For more complete comments, see soreceive(), from
 1318  * which this code originated.
 1319  *
 1320  * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
 1321  * unable to return an mbuf chain to the caller.
 1322  */
 1323 static int
 1324 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
 1325 {
 1326         struct protosw *pr = so->so_proto;
 1327         struct mbuf *m;
 1328         int error;
 1329 
 1330         KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
 1331 
 1332         m = m_get(M_WAIT, MT_DATA);
 1333         error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
 1334         if (error)
 1335                 goto bad;
 1336         do {
 1337 #ifdef ZERO_COPY_SOCKETS
 1338                 if (so_zero_copy_receive) {
 1339                         int disposable;
 1340 
 1341                         if ((m->m_flags & M_EXT)
 1342                          && (m->m_ext.ext_type == EXT_DISPOSABLE))
 1343                                 disposable = 1;
 1344                         else
 1345                                 disposable = 0;
 1346 
 1347                         error = uiomoveco(mtod(m, void *),
 1348                                           min(uio->uio_resid, m->m_len),
 1349                                           uio, disposable);
 1350                 } else
 1351 #endif /* ZERO_COPY_SOCKETS */
 1352                 error = uiomove(mtod(m, void *),
 1353                     (int) min(uio->uio_resid, m->m_len), uio);
 1354                 m = m_free(m);
 1355         } while (uio->uio_resid && error == 0 && m);
 1356 bad:
 1357         if (m != NULL)
 1358                 m_freem(m);
 1359         return (error);
 1360 }
 1361 
 1362 /*
 1363  * Following replacement or removal of the first mbuf on the first mbuf chain
 1364  * of a socket buffer, push necessary state changes back into the socket
 1365  * buffer so that other consumers see the values consistently.  'nextrecord'
 1366  * is the callers locally stored value of the original value of
 1367  * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
 1368  * NOTE: 'nextrecord' may be NULL.
 1369  */
 1370 static __inline void
 1371 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
 1372 {
 1373 
 1374         SOCKBUF_LOCK_ASSERT(sb);
 1375         /*
 1376          * First, update for the new value of nextrecord.  If necessary, make
 1377          * it the first record.
 1378          */
 1379         if (sb->sb_mb != NULL)
 1380                 sb->sb_mb->m_nextpkt = nextrecord;
 1381         else
 1382                 sb->sb_mb = nextrecord;
 1383 
 1384         /*
 1385          * Now update any dependent socket buffer fields to reflect the new
 1386          * state.  This is an expanded inline of SB_EMPTY_FIXUP(), with the
 1387          * addition of a second clause that takes care of the case where
 1388          * sb_mb has been updated, but remains the last record.
 1389          */
 1390         if (sb->sb_mb == NULL) {
 1391                 sb->sb_mbtail = NULL;
 1392                 sb->sb_lastrecord = NULL;
 1393         } else if (sb->sb_mb->m_nextpkt == NULL)
 1394                 sb->sb_lastrecord = sb->sb_mb;
 1395 }
 1396 
 1397 
 1398 /*
 1399  * Implement receive operations on a socket.  We depend on the way that
 1400  * records are added to the sockbuf by sbappend.  In particular, each record
 1401  * (mbufs linked through m_next) must begin with an address if the protocol
 1402  * so specifies, followed by an optional mbuf or mbufs containing ancillary
 1403  * data, and then zero or more mbufs of data.  In order to allow parallelism
 1404  * between network receive and copying to user space, as well as avoid
 1405  * sleeping with a mutex held, we release the socket buffer mutex during the
 1406  * user space copy.  Although the sockbuf is locked, new data may still be
 1407  * appended, and thus we must maintain consistency of the sockbuf during that
 1408  * time.
 1409  *
 1410  * The caller may receive the data as a single mbuf chain by supplying an
 1411  * mbuf **mp0 for use in returning the chain.  The uio is then used only for
 1412  * the count in uio_resid.
 1413  */
 1414 int
 1415 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
 1416     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
 1417 {
 1418         struct mbuf *m, **mp;
 1419         int flags, len, error, offset;
 1420         struct protosw *pr = so->so_proto;
 1421         struct mbuf *nextrecord;
 1422         int moff, type = 0;
 1423         int orig_resid = uio->uio_resid;
 1424 
 1425         mp = mp0;
 1426         if (psa != NULL)
 1427                 *psa = NULL;
 1428         if (controlp != NULL)
 1429                 *controlp = NULL;
 1430         if (flagsp != NULL)
 1431                 flags = *flagsp &~ MSG_EOR;
 1432         else
 1433                 flags = 0;
 1434         if (flags & MSG_OOB)
 1435                 return (soreceive_rcvoob(so, uio, flags));
 1436         if (mp != NULL)
 1437                 *mp = NULL;
 1438         if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
 1439             && uio->uio_resid)
 1440                 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
 1441 
 1442         error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
 1443         if (error)
 1444                 return (error);
 1445 
 1446 restart:
 1447         SOCKBUF_LOCK(&so->so_rcv);
 1448         m = so->so_rcv.sb_mb;
 1449         /*
 1450          * If we have less data than requested, block awaiting more (subject
 1451          * to any timeout) if:
 1452          *   1. the current count is less than the low water mark, or
 1453          *   2. MSG_WAITALL is set, and it is possible to do the entire
 1454          *      receive operation at once if we block (resid <= hiwat).
 1455          *   3. MSG_DONTWAIT is not set
 1456          * If MSG_WAITALL is set but resid is larger than the receive buffer,
 1457          * we have to do the receive in sections, and thus risk returning a
 1458          * short count if a timeout or signal occurs after we start.
 1459          */
 1460         if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
 1461             so->so_rcv.sb_cc < uio->uio_resid) &&
 1462             (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
 1463             ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
 1464             m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
 1465                 KASSERT(m != NULL || !so->so_rcv.sb_cc,
 1466                     ("receive: m == %p so->so_rcv.sb_cc == %u",
 1467                     m, so->so_rcv.sb_cc));
 1468                 if (so->so_error) {
 1469                         if (m != NULL)
 1470                                 goto dontblock;
 1471                         error = so->so_error;
 1472                         if ((flags & MSG_PEEK) == 0)
 1473                                 so->so_error = 0;
 1474                         SOCKBUF_UNLOCK(&so->so_rcv);
 1475                         goto release;
 1476                 }
 1477                 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1478                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
 1479                         if (m == NULL) {
 1480                                 SOCKBUF_UNLOCK(&so->so_rcv);
 1481                                 goto release;
 1482                         } else
 1483                                 goto dontblock;
 1484                 }
 1485                 for (; m != NULL; m = m->m_next)
 1486                         if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
 1487                                 m = so->so_rcv.sb_mb;
 1488                                 goto dontblock;
 1489                         }
 1490                 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
 1491                     (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
 1492                         SOCKBUF_UNLOCK(&so->so_rcv);
 1493                         error = ENOTCONN;
 1494                         goto release;
 1495                 }
 1496                 if (uio->uio_resid == 0) {
 1497                         SOCKBUF_UNLOCK(&so->so_rcv);
 1498                         goto release;
 1499                 }
 1500                 if ((so->so_state & SS_NBIO) ||
 1501                     (flags & (MSG_DONTWAIT|MSG_NBIO))) {
 1502                         SOCKBUF_UNLOCK(&so->so_rcv);
 1503                         error = EWOULDBLOCK;
 1504                         goto release;
 1505                 }
 1506                 SBLASTRECORDCHK(&so->so_rcv);
 1507                 SBLASTMBUFCHK(&so->so_rcv);
 1508                 error = sbwait(&so->so_rcv);
 1509                 SOCKBUF_UNLOCK(&so->so_rcv);
 1510                 if (error)
 1511                         goto release;
 1512                 goto restart;
 1513         }
 1514 dontblock:
 1515         /*
 1516          * From this point onward, we maintain 'nextrecord' as a cache of the
 1517          * pointer to the next record in the socket buffer.  We must keep the
 1518          * various socket buffer pointers and local stack versions of the
 1519          * pointers in sync, pushing out modifications before dropping the
 1520          * socket buffer mutex, and re-reading them when picking it up.
 1521          *
 1522          * Otherwise, we will race with the network stack appending new data
 1523          * or records onto the socket buffer by using inconsistent/stale
 1524          * versions of the field, possibly resulting in socket buffer
 1525          * corruption.
 1526          *
 1527          * By holding the high-level sblock(), we prevent simultaneous
 1528          * readers from pulling off the front of the socket buffer.
 1529          */
 1530         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1531         if (uio->uio_td)
 1532                 uio->uio_td->td_ru.ru_msgrcv++;
 1533         KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
 1534         SBLASTRECORDCHK(&so->so_rcv);
 1535         SBLASTMBUFCHK(&so->so_rcv);
 1536         nextrecord = m->m_nextpkt;
 1537         if (pr->pr_flags & PR_ADDR) {
 1538                 KASSERT(m->m_type == MT_SONAME,
 1539                     ("m->m_type == %d", m->m_type));
 1540                 orig_resid = 0;
 1541                 if (psa != NULL)
 1542                         *psa = sodupsockaddr(mtod(m, struct sockaddr *),
 1543                             M_NOWAIT);
 1544                 if (flags & MSG_PEEK) {
 1545                         m = m->m_next;
 1546                 } else {
 1547                         sbfree(&so->so_rcv, m);
 1548                         so->so_rcv.sb_mb = m_free(m);
 1549                         m = so->so_rcv.sb_mb;
 1550                         sockbuf_pushsync(&so->so_rcv, nextrecord);
 1551                 }
 1552         }
 1553 
 1554         /*
 1555          * Process one or more MT_CONTROL mbufs present before any data mbufs
 1556          * in the first mbuf chain on the socket buffer.  If MSG_PEEK, we
 1557          * just copy the data; if !MSG_PEEK, we call into the protocol to
 1558          * perform externalization (or freeing if controlp == NULL).
 1559          */
 1560         if (m != NULL && m->m_type == MT_CONTROL) {
 1561                 struct mbuf *cm = NULL, *cmn;
 1562                 struct mbuf **cme = &cm;
 1563 
 1564                 do {
 1565                         if (flags & MSG_PEEK) {
 1566                                 if (controlp != NULL) {
 1567                                         *controlp = m_copy(m, 0, m->m_len);
 1568                                         controlp = &(*controlp)->m_next;
 1569                                 }
 1570                                 m = m->m_next;
 1571                         } else {
 1572                                 sbfree(&so->so_rcv, m);
 1573                                 so->so_rcv.sb_mb = m->m_next;
 1574                                 m->m_next = NULL;
 1575                                 *cme = m;
 1576                                 cme = &(*cme)->m_next;
 1577                                 m = so->so_rcv.sb_mb;
 1578                         }
 1579                 } while (m != NULL && m->m_type == MT_CONTROL);
 1580                 if ((flags & MSG_PEEK) == 0)
 1581                         sockbuf_pushsync(&so->so_rcv, nextrecord);
 1582                 while (cm != NULL) {
 1583                         cmn = cm->m_next;
 1584                         cm->m_next = NULL;
 1585                         if (pr->pr_domain->dom_externalize != NULL) {
 1586                                 SOCKBUF_UNLOCK(&so->so_rcv);
 1587                                 error = (*pr->pr_domain->dom_externalize)
 1588                                     (cm, controlp);
 1589                                 SOCKBUF_LOCK(&so->so_rcv);
 1590                         } else if (controlp != NULL)
 1591                                 *controlp = cm;
 1592                         else
 1593                                 m_freem(cm);
 1594                         if (controlp != NULL) {
 1595                                 orig_resid = 0;
 1596                                 while (*controlp != NULL)
 1597                                         controlp = &(*controlp)->m_next;
 1598                         }
 1599                         cm = cmn;
 1600                 }
 1601                 if (m != NULL)
 1602                         nextrecord = so->so_rcv.sb_mb->m_nextpkt;
 1603                 else
 1604                         nextrecord = so->so_rcv.sb_mb;
 1605                 orig_resid = 0;
 1606         }
 1607         if (m != NULL) {
 1608                 if ((flags & MSG_PEEK) == 0) {
 1609                         KASSERT(m->m_nextpkt == nextrecord,
 1610                             ("soreceive: post-control, nextrecord !sync"));
 1611                         if (nextrecord == NULL) {
 1612                                 KASSERT(so->so_rcv.sb_mb == m,
 1613                                     ("soreceive: post-control, sb_mb!=m"));
 1614                                 KASSERT(so->so_rcv.sb_lastrecord == m,
 1615                                     ("soreceive: post-control, lastrecord!=m"));
 1616                         }
 1617                 }
 1618                 type = m->m_type;
 1619                 if (type == MT_OOBDATA)
 1620                         flags |= MSG_OOB;
 1621         } else {
 1622                 if ((flags & MSG_PEEK) == 0) {
 1623                         KASSERT(so->so_rcv.sb_mb == nextrecord,
 1624                             ("soreceive: sb_mb != nextrecord"));
 1625                         if (so->so_rcv.sb_mb == NULL) {
 1626                                 KASSERT(so->so_rcv.sb_lastrecord == NULL,
 1627                                     ("soreceive: sb_lastercord != NULL"));
 1628                         }
 1629                 }
 1630         }
 1631         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1632         SBLASTRECORDCHK(&so->so_rcv);
 1633         SBLASTMBUFCHK(&so->so_rcv);
 1634 
 1635         /*
 1636          * Now continue to read any data mbufs off of the head of the socket
 1637          * buffer until the read request is satisfied.  Note that 'type' is
 1638          * used to store the type of any mbuf reads that have happened so far
 1639          * such that soreceive() can stop reading if the type changes, which
 1640          * causes soreceive() to return only one of regular data and inline
 1641          * out-of-band data in a single socket receive operation.
 1642          */
 1643         moff = 0;
 1644         offset = 0;
 1645         while (m != NULL && uio->uio_resid > 0 && error == 0) {
 1646                 /*
 1647                  * If the type of mbuf has changed since the last mbuf
 1648                  * examined ('type'), end the receive operation.
 1649                  */
 1650                 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1651                 if (m->m_type == MT_OOBDATA) {
 1652                         if (type != MT_OOBDATA)
 1653                                 break;
 1654                 } else if (type == MT_OOBDATA)
 1655                         break;
 1656                 else
 1657                     KASSERT(m->m_type == MT_DATA,
 1658                         ("m->m_type == %d", m->m_type));
 1659                 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
 1660                 len = uio->uio_resid;
 1661                 if (so->so_oobmark && len > so->so_oobmark - offset)
 1662                         len = so->so_oobmark - offset;
 1663                 if (len > m->m_len - moff)
 1664                         len = m->m_len - moff;
 1665                 /*
 1666                  * If mp is set, just pass back the mbufs.  Otherwise copy
 1667                  * them out via the uio, then free.  Sockbuf must be
 1668                  * consistent here (points to current mbuf, it points to next
 1669                  * record) when we drop priority; we must note any additions
 1670                  * to the sockbuf when we block interrupts again.
 1671                  */
 1672                 if (mp == NULL) {
 1673                         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1674                         SBLASTRECORDCHK(&so->so_rcv);
 1675                         SBLASTMBUFCHK(&so->so_rcv);
 1676                         SOCKBUF_UNLOCK(&so->so_rcv);
 1677 #ifdef ZERO_COPY_SOCKETS
 1678                         if (so_zero_copy_receive) {
 1679                                 int disposable;
 1680 
 1681                                 if ((m->m_flags & M_EXT)
 1682                                  && (m->m_ext.ext_type == EXT_DISPOSABLE))
 1683                                         disposable = 1;
 1684                                 else
 1685                                         disposable = 0;
 1686 
 1687                                 error = uiomoveco(mtod(m, char *) + moff,
 1688                                                   (int)len, uio,
 1689                                                   disposable);
 1690                         } else
 1691 #endif /* ZERO_COPY_SOCKETS */
 1692                         error = uiomove(mtod(m, char *) + moff, (int)len, uio);
 1693                         SOCKBUF_LOCK(&so->so_rcv);
 1694                         if (error) {
 1695                                 /*
 1696                                  * The MT_SONAME mbuf has already been removed
 1697                                  * from the record, so it is necessary to
 1698                                  * remove the data mbufs, if any, to preserve
 1699                                  * the invariant in the case of PR_ADDR that
 1700                                  * requires MT_SONAME mbufs at the head of
 1701                                  * each record.
 1702                                  */
 1703                                 if (m && pr->pr_flags & PR_ATOMIC &&
 1704                                     ((flags & MSG_PEEK) == 0))
 1705                                         (void)sbdroprecord_locked(&so->so_rcv);
 1706                                 SOCKBUF_UNLOCK(&so->so_rcv);
 1707                                 goto release;
 1708                         }
 1709                 } else
 1710                         uio->uio_resid -= len;
 1711                 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1712                 if (len == m->m_len - moff) {
 1713                         if (m->m_flags & M_EOR)
 1714                                 flags |= MSG_EOR;
 1715                         if (flags & MSG_PEEK) {
 1716                                 m = m->m_next;
 1717                                 moff = 0;
 1718                         } else {
 1719                                 nextrecord = m->m_nextpkt;
 1720                                 sbfree(&so->so_rcv, m);
 1721                                 if (mp != NULL) {
 1722                                         *mp = m;
 1723                                         mp = &m->m_next;
 1724                                         so->so_rcv.sb_mb = m = m->m_next;
 1725                                         *mp = NULL;
 1726                                 } else {
 1727                                         so->so_rcv.sb_mb = m_free(m);
 1728                                         m = so->so_rcv.sb_mb;
 1729                                 }
 1730                                 sockbuf_pushsync(&so->so_rcv, nextrecord);
 1731                                 SBLASTRECORDCHK(&so->so_rcv);
 1732                                 SBLASTMBUFCHK(&so->so_rcv);
 1733                         }
 1734                 } else {
 1735                         if (flags & MSG_PEEK)
 1736                                 moff += len;
 1737                         else {
 1738                                 if (mp != NULL) {
 1739                                         int copy_flag;
 1740 
 1741                                         if (flags & MSG_DONTWAIT)
 1742                                                 copy_flag = M_DONTWAIT;
 1743                                         else
 1744                                                 copy_flag = M_WAIT;
 1745                                         if (copy_flag == M_WAIT)
 1746                                                 SOCKBUF_UNLOCK(&so->so_rcv);
 1747                                         *mp = m_copym(m, 0, len, copy_flag);
 1748                                         if (copy_flag == M_WAIT)
 1749                                                 SOCKBUF_LOCK(&so->so_rcv);
 1750                                         if (*mp == NULL) {
 1751                                                 /*
 1752                                                  * m_copym() couldn't
 1753                                                  * allocate an mbuf.  Adjust
 1754                                                  * uio_resid back (it was
 1755                                                  * adjusted down by len
 1756                                                  * bytes, which we didn't end
 1757                                                  * up "copying" over).
 1758                                                  */
 1759                                                 uio->uio_resid += len;
 1760                                                 break;
 1761                                         }
 1762                                 }
 1763                                 m->m_data += len;
 1764                                 m->m_len -= len;
 1765                                 so->so_rcv.sb_cc -= len;
 1766                         }
 1767                 }
 1768                 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1769                 if (so->so_oobmark) {
 1770                         if ((flags & MSG_PEEK) == 0) {
 1771                                 so->so_oobmark -= len;
 1772                                 if (so->so_oobmark == 0) {
 1773                                         so->so_rcv.sb_state |= SBS_RCVATMARK;
 1774                                         break;
 1775                                 }
 1776                         } else {
 1777                                 offset += len;
 1778                                 if (offset == so->so_oobmark)
 1779                                         break;
 1780                         }
 1781                 }
 1782                 if (flags & MSG_EOR)
 1783                         break;
 1784                 /*
 1785                  * If the MSG_WAITALL flag is set (for non-atomic socket), we
 1786                  * must not quit until "uio->uio_resid == 0" or an error
 1787                  * termination.  If a signal/timeout occurs, return with a
 1788                  * short count but without error.  Keep sockbuf locked
 1789                  * against other readers.
 1790                  */
 1791                 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
 1792                     !sosendallatonce(so) && nextrecord == NULL) {
 1793                         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1794                         if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
 1795                                 break;
 1796                         /*
 1797                          * Notify the protocol that some data has been
 1798                          * drained before blocking.
 1799                          */
 1800                         if (pr->pr_flags & PR_WANTRCVD) {
 1801                                 SOCKBUF_UNLOCK(&so->so_rcv);
 1802                                 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
 1803                                 SOCKBUF_LOCK(&so->so_rcv);
 1804                         }
 1805                         SBLASTRECORDCHK(&so->so_rcv);
 1806                         SBLASTMBUFCHK(&so->so_rcv);
 1807                         error = sbwait(&so->so_rcv);
 1808                         if (error) {
 1809                                 SOCKBUF_UNLOCK(&so->so_rcv);
 1810                                 goto release;
 1811                         }
 1812                         m = so->so_rcv.sb_mb;
 1813                         if (m != NULL)
 1814                                 nextrecord = m->m_nextpkt;
 1815                 }
 1816         }
 1817 
 1818         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1819         if (m != NULL && pr->pr_flags & PR_ATOMIC) {
 1820                 flags |= MSG_TRUNC;
 1821                 if ((flags & MSG_PEEK) == 0)
 1822                         (void) sbdroprecord_locked(&so->so_rcv);
 1823         }
 1824         if ((flags & MSG_PEEK) == 0) {
 1825                 if (m == NULL) {
 1826                         /*
 1827                          * First part is an inline SB_EMPTY_FIXUP().  Second
 1828                          * part makes sure sb_lastrecord is up-to-date if
 1829                          * there is still data in the socket buffer.
 1830                          */
 1831                         so->so_rcv.sb_mb = nextrecord;
 1832                         if (so->so_rcv.sb_mb == NULL) {
 1833                                 so->so_rcv.sb_mbtail = NULL;
 1834                                 so->so_rcv.sb_lastrecord = NULL;
 1835                         } else if (nextrecord->m_nextpkt == NULL)
 1836                                 so->so_rcv.sb_lastrecord = nextrecord;
 1837                 }
 1838                 SBLASTRECORDCHK(&so->so_rcv);
 1839                 SBLASTMBUFCHK(&so->so_rcv);
 1840                 /*
 1841                  * If soreceive() is being done from the socket callback,
 1842                  * then don't need to generate ACK to peer to update window,
 1843                  * since ACK will be generated on return to TCP.
 1844                  */
 1845                 if (!(flags & MSG_SOCALLBCK) &&
 1846                     (pr->pr_flags & PR_WANTRCVD)) {
 1847                         SOCKBUF_UNLOCK(&so->so_rcv);
 1848                         (*pr->pr_usrreqs->pru_rcvd)(so, flags);
 1849                         SOCKBUF_LOCK(&so->so_rcv);
 1850                 }
 1851         }
 1852         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1853         if (orig_resid == uio->uio_resid && orig_resid &&
 1854             (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
 1855                 SOCKBUF_UNLOCK(&so->so_rcv);
 1856                 goto restart;
 1857         }
 1858         SOCKBUF_UNLOCK(&so->so_rcv);
 1859 
 1860         if (flagsp != NULL)
 1861                 *flagsp |= flags;
 1862 release:
 1863         sbunlock(&so->so_rcv);
 1864         return (error);
 1865 }
 1866 
 1867 /*
 1868  * Optimized version of soreceive() for stream (TCP) sockets.
 1869  */
 1870 #ifdef TCP_SORECEIVE_STREAM
 1871 int
 1872 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
 1873     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
 1874 {
 1875         int len = 0, error = 0, flags, oresid;
 1876         struct sockbuf *sb;
 1877         struct mbuf *m, *n = NULL;
 1878 
 1879         /* We only do stream sockets. */
 1880         if (so->so_type != SOCK_STREAM)
 1881                 return (EINVAL);
 1882         if (psa != NULL)
 1883                 *psa = NULL;
 1884         if (controlp != NULL)
 1885                 return (EINVAL);
 1886         if (flagsp != NULL)
 1887                 flags = *flagsp &~ MSG_EOR;
 1888         else
 1889                 flags = 0;
 1890         if (flags & MSG_OOB)
 1891                 return (soreceive_rcvoob(so, uio, flags));
 1892         if (mp0 != NULL)
 1893                 *mp0 = NULL;
 1894 
 1895         sb = &so->so_rcv;
 1896 
 1897         /* Prevent other readers from entering the socket. */
 1898         error = sblock(sb, SBLOCKWAIT(flags));
 1899         if (error)
 1900                 goto out;
 1901         SOCKBUF_LOCK(sb);
 1902 
 1903         /* Easy one, no space to copyout anything. */
 1904         if (uio->uio_resid == 0) {
 1905                 error = EINVAL;
 1906                 goto out;
 1907         }
 1908         oresid = uio->uio_resid;
 1909 
 1910         /* We will never ever get anything unless we are connected. */
 1911         if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
 1912                 /* When disconnecting there may be still some data left. */
 1913                 if (sb->sb_cc > 0)
 1914                         goto deliver;
 1915                 if (!(so->so_state & SS_ISDISCONNECTED))
 1916                         error = ENOTCONN;
 1917                 goto out;
 1918         }
 1919 
 1920         /* Socket buffer is empty and we shall not block. */
 1921         if (sb->sb_cc == 0 &&
 1922             ((sb->sb_flags & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
 1923                 error = EAGAIN;
 1924                 goto out;
 1925         }
 1926 
 1927 restart:
 1928         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1929 
 1930         /* Abort if socket has reported problems. */
 1931         if (so->so_error) {
 1932                 if (sb->sb_cc > 0)
 1933                         goto deliver;
 1934                 if (oresid > uio->uio_resid)
 1935                         goto out;
 1936                 error = so->so_error;
 1937                 if (!(flags & MSG_PEEK))
 1938                         so->so_error = 0;
 1939                 goto out;
 1940         }
 1941 
 1942         /* Door is closed.  Deliver what is left, if any. */
 1943         if (sb->sb_state & SBS_CANTRCVMORE) {
 1944                 if (sb->sb_cc > 0)
 1945                         goto deliver;
 1946                 else
 1947                         goto out;
 1948         }
 1949 
 1950         /* Socket buffer got some data that we shall deliver now. */
 1951         if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) &&
 1952             ((sb->sb_flags & SS_NBIO) ||
 1953              (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
 1954              sb->sb_cc >= sb->sb_lowat ||
 1955              sb->sb_cc >= uio->uio_resid ||
 1956              sb->sb_cc >= sb->sb_hiwat) ) {
 1957                 goto deliver;
 1958         }
 1959 
 1960         /* On MSG_WAITALL we must wait until all data or error arrives. */
 1961         if ((flags & MSG_WAITALL) &&
 1962             (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_lowat))
 1963                 goto deliver;
 1964 
 1965         /*
 1966          * Wait and block until (more) data comes in.
 1967          * NB: Drops the sockbuf lock during wait.
 1968          */
 1969         error = sbwait(sb);
 1970         if (error)
 1971                 goto out;
 1972         goto restart;
 1973 
 1974 deliver:
 1975         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1976         KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__));
 1977         KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
 1978 
 1979         /* Statistics. */
 1980         if (uio->uio_td)
 1981                 uio->uio_td->td_ru.ru_msgrcv++;
 1982 
 1983         /* Fill uio until full or current end of socket buffer is reached. */
 1984         len = min(uio->uio_resid, sb->sb_cc);
 1985         if (mp0 != NULL) {
 1986                 /* Dequeue as many mbufs as possible. */
 1987                 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
 1988                         for (*mp0 = m = sb->sb_mb;
 1989                              m != NULL && m->m_len <= len;
 1990                              m = m->m_next) {
 1991                                 len -= m->m_len;
 1992                                 uio->uio_resid -= m->m_len;
 1993                                 sbfree(sb, m);
 1994                                 n = m;
 1995                         }
 1996                         sb->sb_mb = m;
 1997                         if (sb->sb_mb == NULL)
 1998                                 SB_EMPTY_FIXUP(sb);
 1999                         n->m_next = NULL;
 2000                 }
 2001                 /* Copy the remainder. */
 2002                 if (len > 0) {
 2003                         KASSERT(sb->sb_mb != NULL,
 2004                             ("%s: len > 0 && sb->sb_mb empty", __func__));
 2005 
 2006                         m = m_copym(sb->sb_mb, 0, len, M_DONTWAIT);
 2007                         if (m == NULL)
 2008                                 len = 0;        /* Don't flush data from sockbuf. */
 2009                         else
 2010                                 uio->uio_resid -= m->m_len;
 2011                         if (*mp0 != NULL)
 2012                                 n->m_next = m;
 2013                         else
 2014                                 *mp0 = m;
 2015                         if (*mp0 == NULL) {
 2016                                 error = ENOBUFS;
 2017                                 goto out;
 2018                         }
 2019                 }
 2020         } else {
 2021                 /* NB: Must unlock socket buffer as uiomove may sleep. */
 2022                 SOCKBUF_UNLOCK(sb);
 2023                 error = m_mbuftouio(uio, sb->sb_mb, len);
 2024                 SOCKBUF_LOCK(sb);
 2025                 if (error)
 2026                         goto out;
 2027         }
 2028         SBLASTRECORDCHK(sb);
 2029         SBLASTMBUFCHK(sb);
 2030 
 2031         /*
 2032          * Remove the delivered data from the socket buffer unless we
 2033          * were only peeking.
 2034          */
 2035         if (!(flags & MSG_PEEK)) {
 2036                 if (len > 0)
 2037                         sbdrop_locked(sb, len);
 2038 
 2039                 /* Notify protocol that we drained some data. */
 2040                 if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
 2041                     (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
 2042                      !(flags & MSG_SOCALLBCK))) {
 2043                         SOCKBUF_UNLOCK(sb);
 2044                         (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
 2045                         SOCKBUF_LOCK(sb);
 2046                 }
 2047         }
 2048 
 2049         /*
 2050          * For MSG_WAITALL we may have to loop again and wait for
 2051          * more data to come in.
 2052          */
 2053         if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
 2054                 goto restart;
 2055 out:
 2056         SOCKBUF_LOCK_ASSERT(sb);
 2057         SBLASTRECORDCHK(sb);
 2058         SBLASTMBUFCHK(sb);
 2059         SOCKBUF_UNLOCK(sb);
 2060         sbunlock(sb);
 2061         return (error);
 2062 }
 2063 #endif /* TCP_SORECEIVE_STREAM */
 2064 
 2065 /*
 2066  * Optimized version of soreceive() for simple datagram cases from userspace.
 2067  * Unlike in the stream case, we're able to drop a datagram if copyout()
 2068  * fails, and because we handle datagrams atomically, we don't need to use a
 2069  * sleep lock to prevent I/O interlacing.
 2070  */
 2071 int
 2072 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
 2073     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
 2074 {
 2075         struct mbuf *m, *m2;
 2076         int flags, len, error;
 2077         struct protosw *pr = so->so_proto;
 2078         struct mbuf *nextrecord;
 2079 
 2080         if (psa != NULL)
 2081                 *psa = NULL;
 2082         if (controlp != NULL)
 2083                 *controlp = NULL;
 2084         if (flagsp != NULL)
 2085                 flags = *flagsp &~ MSG_EOR;
 2086         else
 2087                 flags = 0;
 2088 
 2089         /*
 2090          * For any complicated cases, fall back to the full
 2091          * soreceive_generic().
 2092          */
 2093         if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
 2094                 return (soreceive_generic(so, psa, uio, mp0, controlp,
 2095                     flagsp));
 2096 
 2097         /*
 2098          * Enforce restrictions on use.
 2099          */
 2100         KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
 2101             ("soreceive_dgram: wantrcvd"));
 2102         KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
 2103         KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
 2104             ("soreceive_dgram: SBS_RCVATMARK"));
 2105         KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
 2106             ("soreceive_dgram: P_CONNREQUIRED"));
 2107 
 2108         /*
 2109          * Loop blocking while waiting for a datagram.
 2110          */
 2111         SOCKBUF_LOCK(&so->so_rcv);
 2112         while ((m = so->so_rcv.sb_mb) == NULL) {
 2113                 KASSERT(so->so_rcv.sb_cc == 0,
 2114                     ("soreceive_dgram: sb_mb NULL but sb_cc %u",
 2115                     so->so_rcv.sb_cc));
 2116                 if (so->so_error) {
 2117                         error = so->so_error;
 2118                         so->so_error = 0;
 2119                         SOCKBUF_UNLOCK(&so->so_rcv);
 2120                         return (error);
 2121                 }
 2122                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
 2123                     uio->uio_resid == 0) {
 2124                         SOCKBUF_UNLOCK(&so->so_rcv);
 2125                         return (0);
 2126                 }
 2127                 if ((so->so_state & SS_NBIO) ||
 2128                     (flags & (MSG_DONTWAIT|MSG_NBIO))) {
 2129                         SOCKBUF_UNLOCK(&so->so_rcv);
 2130                         return (EWOULDBLOCK);
 2131                 }
 2132                 SBLASTRECORDCHK(&so->so_rcv);
 2133                 SBLASTMBUFCHK(&so->so_rcv);
 2134                 error = sbwait(&so->so_rcv);
 2135                 if (error) {
 2136                         SOCKBUF_UNLOCK(&so->so_rcv);
 2137                         return (error);
 2138                 }
 2139         }
 2140         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 2141 
 2142         if (uio->uio_td)
 2143                 uio->uio_td->td_ru.ru_msgrcv++;
 2144         SBLASTRECORDCHK(&so->so_rcv);
 2145         SBLASTMBUFCHK(&so->so_rcv);
 2146         nextrecord = m->m_nextpkt;
 2147         if (nextrecord == NULL) {
 2148                 KASSERT(so->so_rcv.sb_lastrecord == m,
 2149                     ("soreceive_dgram: lastrecord != m"));
 2150         }
 2151 
 2152         KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
 2153             ("soreceive_dgram: m_nextpkt != nextrecord"));
 2154 
 2155         /*
 2156          * Pull 'm' and its chain off the front of the packet queue.
 2157          */
 2158         so->so_rcv.sb_mb = NULL;
 2159         sockbuf_pushsync(&so->so_rcv, nextrecord);
 2160 
 2161         /*
 2162          * Walk 'm's chain and free that many bytes from the socket buffer.
 2163          */
 2164         for (m2 = m; m2 != NULL; m2 = m2->m_next)
 2165                 sbfree(&so->so_rcv, m2);
 2166 
 2167         /*
 2168          * Do a few last checks before we let go of the lock.
 2169          */
 2170         SBLASTRECORDCHK(&so->so_rcv);
 2171         SBLASTMBUFCHK(&so->so_rcv);
 2172         SOCKBUF_UNLOCK(&so->so_rcv);
 2173 
 2174         if (pr->pr_flags & PR_ADDR) {
 2175                 KASSERT(m->m_type == MT_SONAME,
 2176                     ("m->m_type == %d", m->m_type));
 2177                 if (psa != NULL)
 2178                         *psa = sodupsockaddr(mtod(m, struct sockaddr *),
 2179                             M_NOWAIT);
 2180                 m = m_free(m);
 2181         }
 2182         if (m == NULL) {
 2183                 /* XXXRW: Can this happen? */
 2184                 return (0);
 2185         }
 2186 
 2187         /*
 2188          * Packet to copyout() is now in 'm' and it is disconnected from the
 2189          * queue.
 2190          *
 2191          * Process one or more MT_CONTROL mbufs present before any data mbufs
 2192          * in the first mbuf chain on the socket buffer.  We call into the
 2193          * protocol to perform externalization (or freeing if controlp ==
 2194          * NULL).
 2195          */
 2196         if (m->m_type == MT_CONTROL) {
 2197                 struct mbuf *cm = NULL, *cmn;
 2198                 struct mbuf **cme = &cm;
 2199 
 2200                 do {
 2201                         m2 = m->m_next;
 2202                         m->m_next = NULL;
 2203                         *cme = m;
 2204                         cme = &(*cme)->m_next;
 2205                         m = m2;
 2206                 } while (m != NULL && m->m_type == MT_CONTROL);
 2207                 while (cm != NULL) {
 2208                         cmn = cm->m_next;
 2209                         cm->m_next = NULL;
 2210                         if (pr->pr_domain->dom_externalize != NULL) {
 2211                                 error = (*pr->pr_domain->dom_externalize)
 2212                                     (cm, controlp);
 2213                         } else if (controlp != NULL)
 2214                                 *controlp = cm;
 2215                         else
 2216                                 m_freem(cm);
 2217                         if (controlp != NULL) {
 2218                                 while (*controlp != NULL)
 2219                                         controlp = &(*controlp)->m_next;
 2220                         }
 2221                         cm = cmn;
 2222                 }
 2223         }
 2224         KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
 2225 
 2226         while (m != NULL && uio->uio_resid > 0) {
 2227                 len = uio->uio_resid;
 2228                 if (len > m->m_len)
 2229                         len = m->m_len;
 2230                 error = uiomove(mtod(m, char *), (int)len, uio);
 2231                 if (error) {
 2232                         m_freem(m);
 2233                         return (error);
 2234                 }
 2235                 m = m_free(m);
 2236         }
 2237         if (m != NULL)
 2238                 flags |= MSG_TRUNC;
 2239         m_freem(m);
 2240         if (flagsp != NULL)
 2241                 *flagsp |= flags;
 2242         return (0);
 2243 }
 2244 
 2245 int
 2246 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
 2247     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
 2248 {
 2249 
 2250         return (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
 2251             controlp, flagsp));
 2252 }
 2253 
 2254 int
 2255 soshutdown(struct socket *so, int how)
 2256 {
 2257         struct protosw *pr = so->so_proto;
 2258         int error;
 2259 
 2260         if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
 2261                 return (EINVAL);
 2262         if (pr->pr_usrreqs->pru_flush != NULL) {
 2263                 (*pr->pr_usrreqs->pru_flush)(so, how);
 2264         }
 2265         if (how != SHUT_WR)
 2266                 sorflush(so);
 2267         if (how != SHUT_RD) {
 2268                 CURVNET_SET(so->so_vnet);
 2269                 error = (*pr->pr_usrreqs->pru_shutdown)(so);
 2270                 CURVNET_RESTORE();
 2271                 return (error);
 2272         }
 2273         return (0);
 2274 }
 2275 
 2276 void
 2277 sorflush(struct socket *so)
 2278 {
 2279         struct sockbuf *sb = &so->so_rcv;
 2280         struct protosw *pr = so->so_proto;
 2281         struct sockbuf asb;
 2282 
 2283         /*
 2284          * In order to avoid calling dom_dispose with the socket buffer mutex
 2285          * held, and in order to generally avoid holding the lock for a long
 2286          * time, we make a copy of the socket buffer and clear the original
 2287          * (except locks, state).  The new socket buffer copy won't have
 2288          * initialized locks so we can only call routines that won't use or
 2289          * assert those locks.
 2290          *
 2291          * Dislodge threads currently blocked in receive and wait to acquire
 2292          * a lock against other simultaneous readers before clearing the
 2293          * socket buffer.  Don't let our acquire be interrupted by a signal
 2294          * despite any existing socket disposition on interruptable waiting.
 2295          */
 2296         CURVNET_SET(so->so_vnet);
 2297         socantrcvmore(so);
 2298         (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
 2299 
 2300         /*
 2301          * Invalidate/clear most of the sockbuf structure, but leave selinfo
 2302          * and mutex data unchanged.
 2303          */
 2304         SOCKBUF_LOCK(sb);
 2305         bzero(&asb, offsetof(struct sockbuf, sb_startzero));
 2306         bcopy(&sb->sb_startzero, &asb.sb_startzero,
 2307             sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
 2308         bzero(&sb->sb_startzero,
 2309             sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
 2310         SOCKBUF_UNLOCK(sb);
 2311         sbunlock(sb);
 2312 
 2313         /*
 2314          * Dispose of special rights and flush the socket buffer.  Don't call
 2315          * any unsafe routines (that rely on locks being initialized) on asb.
 2316          */
 2317         if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
 2318                 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
 2319         sbrelease_internal(&asb, so);
 2320         CURVNET_RESTORE();
 2321 }
 2322 
 2323 /*
 2324  * Perhaps this routine, and sooptcopyout(), below, ought to come in an
 2325  * additional variant to handle the case where the option value needs to be
 2326  * some kind of integer, but not a specific size.  In addition to their use
 2327  * here, these functions are also called by the protocol-level pr_ctloutput()
 2328  * routines.
 2329  */
 2330 int
 2331 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
 2332 {
 2333         size_t  valsize;
 2334 
 2335         /*
 2336          * If the user gives us more than we wanted, we ignore it, but if we
 2337          * don't get the minimum length the caller wants, we return EINVAL.
 2338          * On success, sopt->sopt_valsize is set to however much we actually
 2339          * retrieved.
 2340          */
 2341         if ((valsize = sopt->sopt_valsize) < minlen)
 2342                 return EINVAL;
 2343         if (valsize > len)
 2344                 sopt->sopt_valsize = valsize = len;
 2345 
 2346         if (sopt->sopt_td != NULL)
 2347                 return (copyin(sopt->sopt_val, buf, valsize));
 2348 
 2349         bcopy(sopt->sopt_val, buf, valsize);
 2350         return (0);
 2351 }
 2352 
 2353 /*
 2354  * Kernel version of setsockopt(2).
 2355  *
 2356  * XXX: optlen is size_t, not socklen_t
 2357  */
 2358 int
 2359 so_setsockopt(struct socket *so, int level, int optname, void *optval,
 2360     size_t optlen)
 2361 {
 2362         struct sockopt sopt;
 2363 
 2364         sopt.sopt_level = level;
 2365         sopt.sopt_name = optname;
 2366         sopt.sopt_dir = SOPT_SET;
 2367         sopt.sopt_val = optval;
 2368         sopt.sopt_valsize = optlen;
 2369         sopt.sopt_td = NULL;
 2370         return (sosetopt(so, &sopt));
 2371 }
 2372 
 2373 int
 2374 sosetopt(struct socket *so, struct sockopt *sopt)
 2375 {
 2376         int     error, optval;
 2377         struct  linger l;
 2378         struct  timeval tv;
 2379         u_long  val;
 2380 #ifdef MAC
 2381         struct mac extmac;
 2382 #endif
 2383 
 2384         error = 0;
 2385         if (sopt->sopt_level != SOL_SOCKET) {
 2386                 if (so->so_proto && so->so_proto->pr_ctloutput)
 2387                         return ((*so->so_proto->pr_ctloutput)
 2388                                   (so, sopt));
 2389                 error = ENOPROTOOPT;
 2390         } else {
 2391                 switch (sopt->sopt_name) {
 2392 #ifdef INET
 2393                 case SO_ACCEPTFILTER:
 2394                         error = do_setopt_accept_filter(so, sopt);
 2395                         if (error)
 2396                                 goto bad;
 2397                         break;
 2398 #endif
 2399                 case SO_LINGER:
 2400                         error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
 2401                         if (error)
 2402                                 goto bad;
 2403 
 2404                         SOCK_LOCK(so);
 2405                         so->so_linger = l.l_linger;
 2406                         if (l.l_onoff)
 2407                                 so->so_options |= SO_LINGER;
 2408                         else
 2409                                 so->so_options &= ~SO_LINGER;
 2410                         SOCK_UNLOCK(so);
 2411                         break;
 2412 
 2413                 case SO_DEBUG:
 2414                 case SO_KEEPALIVE:
 2415                 case SO_DONTROUTE:
 2416                 case SO_USELOOPBACK:
 2417                 case SO_BROADCAST:
 2418                 case SO_REUSEADDR:
 2419                 case SO_REUSEPORT:
 2420                 case SO_OOBINLINE:
 2421                 case SO_TIMESTAMP:
 2422                 case SO_BINTIME:
 2423                 case SO_NOSIGPIPE:
 2424                 case SO_NO_DDP:
 2425                 case SO_NO_OFFLOAD:
 2426                         error = sooptcopyin(sopt, &optval, sizeof optval,
 2427                                             sizeof optval);
 2428                         if (error)
 2429                                 goto bad;
 2430                         SOCK_LOCK(so);
 2431                         if (optval)
 2432                                 so->so_options |= sopt->sopt_name;
 2433                         else
 2434                                 so->so_options &= ~sopt->sopt_name;
 2435                         SOCK_UNLOCK(so);
 2436                         break;
 2437 
 2438                 case SO_SETFIB:
 2439                         error = sooptcopyin(sopt, &optval, sizeof optval,
 2440                                             sizeof optval);
 2441                         if (optval < 1 || optval > rt_numfibs) {
 2442                                 error = EINVAL;
 2443                                 goto bad;
 2444                         }
 2445                         if ((so->so_proto->pr_domain->dom_family == PF_INET) ||
 2446                             (so->so_proto->pr_domain->dom_family == PF_ROUTE)) {
 2447                                 so->so_fibnum = optval;
 2448                                 /* Note: ignore error */
 2449                                 if (so->so_proto && so->so_proto->pr_ctloutput)
 2450                                         (*so->so_proto->pr_ctloutput)(so, sopt);
 2451                         } else {
 2452                                 so->so_fibnum = 0;
 2453                         }
 2454                         break;
 2455                 case SO_SNDBUF:
 2456                 case SO_RCVBUF:
 2457                 case SO_SNDLOWAT:
 2458                 case SO_RCVLOWAT:
 2459                         error = sooptcopyin(sopt, &optval, sizeof optval,
 2460                                             sizeof optval);
 2461                         if (error)
 2462                                 goto bad;
 2463 
 2464                         /*
 2465                          * Values < 1 make no sense for any of these options,
 2466                          * so disallow them.
 2467                          */
 2468                         if (optval < 1) {
 2469                                 error = EINVAL;
 2470                                 goto bad;
 2471                         }
 2472 
 2473                         switch (sopt->sopt_name) {
 2474                         case SO_SNDBUF:
 2475                         case SO_RCVBUF:
 2476                                 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
 2477                                     &so->so_snd : &so->so_rcv, (u_long)optval,
 2478                                     so, curthread) == 0) {
 2479                                         error = ENOBUFS;
 2480                                         goto bad;
 2481                                 }
 2482                                 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
 2483                                     &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
 2484                                 break;
 2485 
 2486                         /*
 2487                          * Make sure the low-water is never greater than the
 2488                          * high-water.
 2489                          */
 2490                         case SO_SNDLOWAT:
 2491                                 SOCKBUF_LOCK(&so->so_snd);
 2492                                 so->so_snd.sb_lowat =
 2493                                     (optval > so->so_snd.sb_hiwat) ?
 2494                                     so->so_snd.sb_hiwat : optval;
 2495                                 SOCKBUF_UNLOCK(&so->so_snd);
 2496                                 break;
 2497                         case SO_RCVLOWAT:
 2498                                 SOCKBUF_LOCK(&so->so_rcv);
 2499                                 so->so_rcv.sb_lowat =
 2500                                     (optval > so->so_rcv.sb_hiwat) ?
 2501                                     so->so_rcv.sb_hiwat : optval;
 2502                                 SOCKBUF_UNLOCK(&so->so_rcv);
 2503                                 break;
 2504                         }
 2505                         break;
 2506 
 2507                 case SO_SNDTIMEO:
 2508                 case SO_RCVTIMEO:
 2509 #ifdef COMPAT_IA32
 2510                         if (SV_CURPROC_FLAG(SV_ILP32)) {
 2511                                 struct timeval32 tv32;
 2512 
 2513                                 error = sooptcopyin(sopt, &tv32, sizeof tv32,
 2514                                     sizeof tv32);
 2515                                 CP(tv32, tv, tv_sec);
 2516                                 CP(tv32, tv, tv_usec);
 2517                         } else
 2518 #endif
 2519                                 error = sooptcopyin(sopt, &tv, sizeof tv,
 2520                                     sizeof tv);
 2521                         if (error)
 2522                                 goto bad;
 2523 
 2524                         /* assert(hz > 0); */
 2525                         if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
 2526                             tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
 2527                                 error = EDOM;
 2528                                 goto bad;
 2529                         }
 2530                         /* assert(tick > 0); */
 2531                         /* assert(ULONG_MAX - INT_MAX >= 1000000); */
 2532                         val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
 2533                         if (val > INT_MAX) {
 2534                                 error = EDOM;
 2535                                 goto bad;
 2536                         }
 2537                         if (val == 0 && tv.tv_usec != 0)
 2538                                 val = 1;
 2539 
 2540                         switch (sopt->sopt_name) {
 2541                         case SO_SNDTIMEO:
 2542                                 so->so_snd.sb_timeo = val;
 2543                                 break;
 2544                         case SO_RCVTIMEO:
 2545                                 so->so_rcv.sb_timeo = val;
 2546                                 break;
 2547                         }
 2548                         break;
 2549 
 2550                 case SO_LABEL:
 2551 #ifdef MAC
 2552                         error = sooptcopyin(sopt, &extmac, sizeof extmac,
 2553                             sizeof extmac);
 2554                         if (error)
 2555                                 goto bad;
 2556                         error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
 2557                             so, &extmac);
 2558 #else
 2559                         error = EOPNOTSUPP;
 2560 #endif
 2561                         break;
 2562 
 2563                 default:
 2564                         error = ENOPROTOOPT;
 2565                         break;
 2566                 }
 2567                 if (error == 0 && so->so_proto != NULL &&
 2568                     so->so_proto->pr_ctloutput != NULL) {
 2569                         (void) ((*so->so_proto->pr_ctloutput)
 2570                                   (so, sopt));
 2571                 }
 2572         }
 2573 bad:
 2574         return (error);
 2575 }
 2576 
 2577 /*
 2578  * Helper routine for getsockopt.
 2579  */
 2580 int
 2581 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
 2582 {
 2583         int     error;
 2584         size_t  valsize;
 2585 
 2586         error = 0;
 2587 
 2588         /*
 2589          * Documented get behavior is that we always return a value, possibly
 2590          * truncated to fit in the user's buffer.  Traditional behavior is
 2591          * that we always tell the user precisely how much we copied, rather
 2592          * than something useful like the total amount we had available for
 2593          * her.  Note that this interface is not idempotent; the entire
 2594          * answer must generated ahead of time.
 2595          */
 2596         valsize = min(len, sopt->sopt_valsize);
 2597         sopt->sopt_valsize = valsize;
 2598         if (sopt->sopt_val != NULL) {
 2599                 if (sopt->sopt_td != NULL)
 2600                         error = copyout(buf, sopt->sopt_val, valsize);
 2601                 else
 2602                         bcopy(buf, sopt->sopt_val, valsize);
 2603         }
 2604         return (error);
 2605 }
 2606 
 2607 int
 2608 sogetopt(struct socket *so, struct sockopt *sopt)
 2609 {
 2610         int     error, optval;
 2611         struct  linger l;
 2612         struct  timeval tv;
 2613 #ifdef MAC
 2614         struct mac extmac;
 2615 #endif
 2616 
 2617         error = 0;
 2618         if (sopt->sopt_level != SOL_SOCKET) {
 2619                 if (so->so_proto && so->so_proto->pr_ctloutput) {
 2620                         return ((*so->so_proto->pr_ctloutput)
 2621                                   (so, sopt));
 2622                 } else
 2623                         return (ENOPROTOOPT);
 2624         } else {
 2625                 switch (sopt->sopt_name) {
 2626 #ifdef INET
 2627                 case SO_ACCEPTFILTER:
 2628                         error = do_getopt_accept_filter(so, sopt);
 2629                         break;
 2630 #endif
 2631                 case SO_LINGER:
 2632                         SOCK_LOCK(so);
 2633                         l.l_onoff = so->so_options & SO_LINGER;
 2634                         l.l_linger = so->so_linger;
 2635                         SOCK_UNLOCK(so);
 2636                         error = sooptcopyout(sopt, &l, sizeof l);
 2637                         break;
 2638 
 2639                 case SO_USELOOPBACK:
 2640                 case SO_DONTROUTE:
 2641                 case SO_DEBUG:
 2642                 case SO_KEEPALIVE:
 2643                 case SO_REUSEADDR:
 2644                 case SO_REUSEPORT:
 2645                 case SO_BROADCAST:
 2646                 case SO_OOBINLINE:
 2647                 case SO_ACCEPTCONN:
 2648                 case SO_TIMESTAMP:
 2649                 case SO_BINTIME:
 2650                 case SO_NOSIGPIPE:
 2651                         optval = so->so_options & sopt->sopt_name;
 2652 integer:
 2653                         error = sooptcopyout(sopt, &optval, sizeof optval);
 2654                         break;
 2655 
 2656                 case SO_TYPE:
 2657                         optval = so->so_type;
 2658                         goto integer;
 2659 
 2660                 case SO_ERROR:
 2661                         SOCK_LOCK(so);
 2662                         optval = so->so_error;
 2663                         so->so_error = 0;
 2664                         SOCK_UNLOCK(so);
 2665                         goto integer;
 2666 
 2667                 case SO_SNDBUF:
 2668                         optval = so->so_snd.sb_hiwat;
 2669                         goto integer;
 2670 
 2671                 case SO_RCVBUF:
 2672                         optval = so->so_rcv.sb_hiwat;
 2673                         goto integer;
 2674 
 2675                 case SO_SNDLOWAT:
 2676                         optval = so->so_snd.sb_lowat;
 2677                         goto integer;
 2678 
 2679                 case SO_RCVLOWAT:
 2680                         optval = so->so_rcv.sb_lowat;
 2681                         goto integer;
 2682 
 2683                 case SO_SNDTIMEO:
 2684                 case SO_RCVTIMEO:
 2685                         optval = (sopt->sopt_name == SO_SNDTIMEO ?
 2686                                   so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
 2687 
 2688                         tv.tv_sec = optval / hz;
 2689                         tv.tv_usec = (optval % hz) * tick;
 2690 #ifdef COMPAT_IA32
 2691                         if (SV_CURPROC_FLAG(SV_ILP32)) {
 2692                                 struct timeval32 tv32;
 2693 
 2694                                 CP(tv, tv32, tv_sec);
 2695                                 CP(tv, tv32, tv_usec);
 2696                                 error = sooptcopyout(sopt, &tv32, sizeof tv32);
 2697                         } else
 2698 #endif
 2699                                 error = sooptcopyout(sopt, &tv, sizeof tv);
 2700                         break;
 2701 
 2702                 case SO_LABEL:
 2703 #ifdef MAC
 2704                         error = sooptcopyin(sopt, &extmac, sizeof(extmac),
 2705                             sizeof(extmac));
 2706                         if (error)
 2707                                 return (error);
 2708                         error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
 2709                             so, &extmac);
 2710                         if (error)
 2711                                 return (error);
 2712                         error = sooptcopyout(sopt, &extmac, sizeof extmac);
 2713 #else
 2714                         error = EOPNOTSUPP;
 2715 #endif
 2716                         break;
 2717 
 2718                 case SO_PEERLABEL:
 2719 #ifdef MAC
 2720                         error = sooptcopyin(sopt, &extmac, sizeof(extmac),
 2721                             sizeof(extmac));
 2722                         if (error)
 2723                                 return (error);
 2724                         error = mac_getsockopt_peerlabel(
 2725                             sopt->sopt_td->td_ucred, so, &extmac);
 2726                         if (error)
 2727                                 return (error);
 2728                         error = sooptcopyout(sopt, &extmac, sizeof extmac);
 2729 #else
 2730                         error = EOPNOTSUPP;
 2731 #endif
 2732                         break;
 2733 
 2734                 case SO_LISTENQLIMIT:
 2735                         optval = so->so_qlimit;
 2736                         goto integer;
 2737 
 2738                 case SO_LISTENQLEN:
 2739                         optval = so->so_qlen;
 2740                         goto integer;
 2741 
 2742                 case SO_LISTENINCQLEN:
 2743                         optval = so->so_incqlen;
 2744                         goto integer;
 2745 
 2746                 default:
 2747                         error = ENOPROTOOPT;
 2748                         break;
 2749                 }
 2750                 return (error);
 2751         }
 2752 }
 2753 
 2754 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
 2755 int
 2756 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
 2757 {
 2758         struct mbuf *m, *m_prev;
 2759         int sopt_size = sopt->sopt_valsize;
 2760 
 2761         MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
 2762         if (m == NULL)
 2763                 return ENOBUFS;
 2764         if (sopt_size > MLEN) {
 2765                 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
 2766                 if ((m->m_flags & M_EXT) == 0) {
 2767                         m_free(m);
 2768                         return ENOBUFS;
 2769                 }
 2770                 m->m_len = min(MCLBYTES, sopt_size);
 2771         } else {
 2772                 m->m_len = min(MLEN, sopt_size);
 2773         }
 2774         sopt_size -= m->m_len;
 2775         *mp = m;
 2776         m_prev = m;
 2777 
 2778         while (sopt_size) {
 2779                 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
 2780                 if (m == NULL) {
 2781                         m_freem(*mp);
 2782                         return ENOBUFS;
 2783                 }
 2784                 if (sopt_size > MLEN) {
 2785                         MCLGET(m, sopt->sopt_td != NULL ? M_WAIT :
 2786                             M_DONTWAIT);
 2787                         if ((m->m_flags & M_EXT) == 0) {
 2788                                 m_freem(m);
 2789                                 m_freem(*mp);
 2790                                 return ENOBUFS;
 2791                         }
 2792                         m->m_len = min(MCLBYTES, sopt_size);
 2793                 } else {
 2794                         m->m_len = min(MLEN, sopt_size);
 2795                 }
 2796                 sopt_size -= m->m_len;
 2797                 m_prev->m_next = m;
 2798                 m_prev = m;
 2799         }
 2800         return (0);
 2801 }
 2802 
 2803 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
 2804 int
 2805 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
 2806 {
 2807         struct mbuf *m0 = m;
 2808 
 2809         if (sopt->sopt_val == NULL)
 2810                 return (0);
 2811         while (m != NULL && sopt->sopt_valsize >= m->m_len) {
 2812                 if (sopt->sopt_td != NULL) {
 2813                         int error;
 2814 
 2815                         error = copyin(sopt->sopt_val, mtod(m, char *),
 2816                                        m->m_len);
 2817                         if (error != 0) {
 2818                                 m_freem(m0);
 2819                                 return(error);
 2820                         }
 2821                 } else
 2822                         bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
 2823                 sopt->sopt_valsize -= m->m_len;
 2824                 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
 2825                 m = m->m_next;
 2826         }
 2827         if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
 2828                 panic("ip6_sooptmcopyin");
 2829         return (0);
 2830 }
 2831 
 2832 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
 2833 int
 2834 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
 2835 {
 2836         struct mbuf *m0 = m;
 2837         size_t valsize = 0;
 2838 
 2839         if (sopt->sopt_val == NULL)
 2840                 return (0);
 2841         while (m != NULL && sopt->sopt_valsize >= m->m_len) {
 2842                 if (sopt->sopt_td != NULL) {
 2843                         int error;
 2844 
 2845                         error = copyout(mtod(m, char *), sopt->sopt_val,
 2846                                        m->m_len);
 2847                         if (error != 0) {
 2848                                 m_freem(m0);
 2849                                 return(error);
 2850                         }
 2851                 } else
 2852                         bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
 2853                sopt->sopt_valsize -= m->m_len;
 2854                sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
 2855                valsize += m->m_len;
 2856                m = m->m_next;
 2857         }
 2858         if (m != NULL) {
 2859                 /* enough soopt buffer should be given from user-land */
 2860                 m_freem(m0);
 2861                 return(EINVAL);
 2862         }
 2863         sopt->sopt_valsize = valsize;
 2864         return (0);
 2865 }
 2866 
 2867 /*
 2868  * sohasoutofband(): protocol notifies socket layer of the arrival of new
 2869  * out-of-band data, which will then notify socket consumers.
 2870  */
 2871 void
 2872 sohasoutofband(struct socket *so)
 2873 {
 2874 
 2875         if (so->so_sigio != NULL)
 2876                 pgsigio(&so->so_sigio, SIGURG, 0);
 2877         selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
 2878 }
 2879 
 2880 int
 2881 sopoll(struct socket *so, int events, struct ucred *active_cred,
 2882     struct thread *td)
 2883 {
 2884 
 2885         return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
 2886             td));
 2887 }
 2888 
 2889 int
 2890 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
 2891     struct thread *td)
 2892 {
 2893         int revents = 0;
 2894 
 2895         SOCKBUF_LOCK(&so->so_snd);
 2896         SOCKBUF_LOCK(&so->so_rcv);
 2897         if (events & (POLLIN | POLLRDNORM))
 2898                 if (soreadabledata(so))
 2899                         revents |= events & (POLLIN | POLLRDNORM);
 2900 
 2901         if (events & (POLLOUT | POLLWRNORM))
 2902                 if (sowriteable(so))
 2903                         revents |= events & (POLLOUT | POLLWRNORM);
 2904 
 2905         if (events & (POLLPRI | POLLRDBAND))
 2906                 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
 2907                         revents |= events & (POLLPRI | POLLRDBAND);
 2908 
 2909         if ((events & POLLINIGNEOF) == 0) {
 2910                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
 2911                         revents |= events & (POLLIN | POLLRDNORM);
 2912                         if (so->so_snd.sb_state & SBS_CANTSENDMORE)
 2913                                 revents |= POLLHUP;
 2914                 }
 2915         }
 2916 
 2917         if (revents == 0) {
 2918                 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
 2919                         selrecord(td, &so->so_rcv.sb_sel);
 2920                         so->so_rcv.sb_flags |= SB_SEL;
 2921                 }
 2922 
 2923                 if (events & (POLLOUT | POLLWRNORM)) {
 2924                         selrecord(td, &so->so_snd.sb_sel);
 2925                         so->so_snd.sb_flags |= SB_SEL;
 2926                 }
 2927         }
 2928 
 2929         SOCKBUF_UNLOCK(&so->so_rcv);
 2930         SOCKBUF_UNLOCK(&so->so_snd);
 2931         return (revents);
 2932 }
 2933 
 2934 int
 2935 soo_kqfilter(struct file *fp, struct knote *kn)
 2936 {
 2937         struct socket *so = kn->kn_fp->f_data;
 2938         struct sockbuf *sb;
 2939 
 2940         switch (kn->kn_filter) {
 2941         case EVFILT_READ:
 2942                 if (so->so_options & SO_ACCEPTCONN)
 2943                         kn->kn_fop = &solisten_filtops;
 2944                 else
 2945                         kn->kn_fop = &soread_filtops;
 2946                 sb = &so->so_rcv;
 2947                 break;
 2948         case EVFILT_WRITE:
 2949                 kn->kn_fop = &sowrite_filtops;
 2950                 sb = &so->so_snd;
 2951                 break;
 2952         default:
 2953                 return (EINVAL);
 2954         }
 2955 
 2956         SOCKBUF_LOCK(sb);
 2957         knlist_add(&sb->sb_sel.si_note, kn, 1);
 2958         sb->sb_flags |= SB_KNOTE;
 2959         SOCKBUF_UNLOCK(sb);
 2960         return (0);
 2961 }
 2962 
 2963 /*
 2964  * Some routines that return EOPNOTSUPP for entry points that are not
 2965  * supported by a protocol.  Fill in as needed.
 2966  */
 2967 int
 2968 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
 2969 {
 2970 
 2971         return EOPNOTSUPP;
 2972 }
 2973 
 2974 int
 2975 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
 2976 {
 2977 
 2978         return EOPNOTSUPP;
 2979 }
 2980 
 2981 int
 2982 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
 2983 {
 2984 
 2985         return EOPNOTSUPP;
 2986 }
 2987 
 2988 int
 2989 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
 2990 {
 2991 
 2992         return EOPNOTSUPP;
 2993 }
 2994 
 2995 int
 2996 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
 2997 {
 2998 
 2999         return EOPNOTSUPP;
 3000 }
 3001 
 3002 int
 3003 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
 3004     struct ifnet *ifp, struct thread *td)
 3005 {
 3006 
 3007         return EOPNOTSUPP;
 3008 }
 3009 
 3010 int
 3011 pru_disconnect_notsupp(struct socket *so)
 3012 {
 3013 
 3014         return EOPNOTSUPP;
 3015 }
 3016 
 3017 int
 3018 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
 3019 {
 3020 
 3021         return EOPNOTSUPP;
 3022 }
 3023 
 3024 int
 3025 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
 3026 {
 3027 
 3028         return EOPNOTSUPP;
 3029 }
 3030 
 3031 int
 3032 pru_rcvd_notsupp(struct socket *so, int flags)
 3033 {
 3034 
 3035         return EOPNOTSUPP;
 3036 }
 3037 
 3038 int
 3039 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
 3040 {
 3041 
 3042         return EOPNOTSUPP;
 3043 }
 3044 
 3045 int
 3046 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
 3047     struct sockaddr *addr, struct mbuf *control, struct thread *td)
 3048 {
 3049 
 3050         return EOPNOTSUPP;
 3051 }
 3052 
 3053 /*
 3054  * This isn't really a ``null'' operation, but it's the default one and
 3055  * doesn't do anything destructive.
 3056  */
 3057 int
 3058 pru_sense_null(struct socket *so, struct stat *sb)
 3059 {
 3060 
 3061         sb->st_blksize = so->so_snd.sb_hiwat;
 3062         return 0;
 3063 }
 3064 
 3065 int
 3066 pru_shutdown_notsupp(struct socket *so)
 3067 {
 3068 
 3069         return EOPNOTSUPP;
 3070 }
 3071 
 3072 int
 3073 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
 3074 {
 3075 
 3076         return EOPNOTSUPP;
 3077 }
 3078 
 3079 int
 3080 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
 3081     struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
 3082 {
 3083 
 3084         return EOPNOTSUPP;
 3085 }
 3086 
 3087 int
 3088 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
 3089     struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
 3090 {
 3091 
 3092         return EOPNOTSUPP;
 3093 }
 3094 
 3095 int
 3096 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
 3097     struct thread *td)
 3098 {
 3099 
 3100         return EOPNOTSUPP;
 3101 }
 3102 
 3103 static void
 3104 filt_sordetach(struct knote *kn)
 3105 {
 3106         struct socket *so = kn->kn_fp->f_data;
 3107 
 3108         SOCKBUF_LOCK(&so->so_rcv);
 3109         knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
 3110         if (knlist_empty(&so->so_rcv.sb_sel.si_note))
 3111                 so->so_rcv.sb_flags &= ~SB_KNOTE;
 3112         SOCKBUF_UNLOCK(&so->so_rcv);
 3113 }
 3114 
 3115 /*ARGSUSED*/
 3116 static int
 3117 filt_soread(struct knote *kn, long hint)
 3118 {
 3119         struct socket *so;
 3120 
 3121         so = kn->kn_fp->f_data;
 3122         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 3123 
 3124         kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
 3125         if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
 3126                 kn->kn_flags |= EV_EOF;
 3127                 kn->kn_fflags = so->so_error;
 3128                 return (1);
 3129         } else if (so->so_error)        /* temporary udp error */
 3130                 return (1);
 3131         else if (kn->kn_sfflags & NOTE_LOWAT)
 3132                 return (kn->kn_data >= kn->kn_sdata);
 3133         else
 3134                 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
 3135 }
 3136 
 3137 static void
 3138 filt_sowdetach(struct knote *kn)
 3139 {
 3140         struct socket *so = kn->kn_fp->f_data;
 3141 
 3142         SOCKBUF_LOCK(&so->so_snd);
 3143         knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
 3144         if (knlist_empty(&so->so_snd.sb_sel.si_note))
 3145                 so->so_snd.sb_flags &= ~SB_KNOTE;
 3146         SOCKBUF_UNLOCK(&so->so_snd);
 3147 }
 3148 
 3149 /*ARGSUSED*/
 3150 static int
 3151 filt_sowrite(struct knote *kn, long hint)
 3152 {
 3153         struct socket *so;
 3154 
 3155         so = kn->kn_fp->f_data;
 3156         SOCKBUF_LOCK_ASSERT(&so->so_snd);
 3157         kn->kn_data = sbspace(&so->so_snd);
 3158         if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
 3159                 kn->kn_flags |= EV_EOF;
 3160                 kn->kn_fflags = so->so_error;
 3161                 return (1);
 3162         } else if (so->so_error)        /* temporary udp error */
 3163                 return (1);
 3164         else if (((so->so_state & SS_ISCONNECTED) == 0) &&
 3165             (so->so_proto->pr_flags & PR_CONNREQUIRED))
 3166                 return (0);
 3167         else if (kn->kn_sfflags & NOTE_LOWAT)
 3168                 return (kn->kn_data >= kn->kn_sdata);
 3169         else
 3170                 return (kn->kn_data >= so->so_snd.sb_lowat);
 3171 }
 3172 
 3173 /*ARGSUSED*/
 3174 static int
 3175 filt_solisten(struct knote *kn, long hint)
 3176 {
 3177         struct socket *so = kn->kn_fp->f_data;
 3178 
 3179         kn->kn_data = so->so_qlen;
 3180         return (! TAILQ_EMPTY(&so->so_comp));
 3181 }
 3182 
 3183 int
 3184 socheckuid(struct socket *so, uid_t uid)
 3185 {
 3186 
 3187         if (so == NULL)
 3188                 return (EPERM);
 3189         if (so->so_cred->cr_uid != uid)
 3190                 return (EPERM);
 3191         return (0);
 3192 }
 3193 
 3194 static int
 3195 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
 3196 {
 3197         int error;
 3198         int val;
 3199 
 3200         val = somaxconn;
 3201         error = sysctl_handle_int(oidp, &val, 0, req);
 3202         if (error || !req->newptr )
 3203                 return (error);
 3204 
 3205         if (val < 1 || val > USHRT_MAX)
 3206                 return (EINVAL);
 3207 
 3208         somaxconn = val;
 3209         return (0);
 3210 }
 3211 
 3212 /*
 3213  * These functions are used by protocols to notify the socket layer (and its
 3214  * consumers) of state changes in the sockets driven by protocol-side events.
 3215  */
 3216 
 3217 /*
 3218  * Procedures to manipulate state flags of socket and do appropriate wakeups.
 3219  *
 3220  * Normal sequence from the active (originating) side is that
 3221  * soisconnecting() is called during processing of connect() call, resulting
 3222  * in an eventual call to soisconnected() if/when the connection is
 3223  * established.  When the connection is torn down soisdisconnecting() is
 3224  * called during processing of disconnect() call, and soisdisconnected() is
 3225  * called when the connection to the peer is totally severed.  The semantics
 3226  * of these routines are such that connectionless protocols can call
 3227  * soisconnected() and soisdisconnected() only, bypassing the in-progress
 3228  * calls when setting up a ``connection'' takes no time.
 3229  *
 3230  * From the passive side, a socket is created with two queues of sockets:
 3231  * so_incomp for connections in progress and so_comp for connections already
 3232  * made and awaiting user acceptance.  As a protocol is preparing incoming
 3233  * connections, it creates a socket structure queued on so_incomp by calling
 3234  * sonewconn().  When the connection is established, soisconnected() is
 3235  * called, and transfers the socket structure to so_comp, making it available
 3236  * to accept().
 3237  *
 3238  * If a socket is closed with sockets on either so_incomp or so_comp, these
 3239  * sockets are dropped.
 3240  *
 3241  * If higher-level protocols are implemented in the kernel, the wakeups done
 3242  * here will sometimes cause software-interrupt process scheduling.
 3243  */
 3244 void
 3245 soisconnecting(struct socket *so)
 3246 {
 3247 
 3248         SOCK_LOCK(so);
 3249         so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
 3250         so->so_state |= SS_ISCONNECTING;
 3251         SOCK_UNLOCK(so);
 3252 }
 3253 
 3254 void
 3255 soisconnected(struct socket *so)
 3256 {
 3257         struct socket *head;    
 3258         int ret;
 3259 
 3260 restart:
 3261         ACCEPT_LOCK();
 3262         SOCK_LOCK(so);
 3263         so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
 3264         so->so_state |= SS_ISCONNECTED;
 3265         head = so->so_head;
 3266         if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
 3267                 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
 3268                         SOCK_UNLOCK(so);
 3269                         TAILQ_REMOVE(&head->so_incomp, so, so_list);
 3270                         head->so_incqlen--;
 3271                         so->so_qstate &= ~SQ_INCOMP;
 3272                         TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
 3273                         head->so_qlen++;
 3274                         so->so_qstate |= SQ_COMP;
 3275                         ACCEPT_UNLOCK();
 3276                         sorwakeup(head);
 3277                         wakeup_one(&head->so_timeo);
 3278                 } else {
 3279                         ACCEPT_UNLOCK();
 3280                         soupcall_set(so, SO_RCV,
 3281                             head->so_accf->so_accept_filter->accf_callback,
 3282                             head->so_accf->so_accept_filter_arg);
 3283                         so->so_options &= ~SO_ACCEPTFILTER;
 3284                         ret = head->so_accf->so_accept_filter->accf_callback(so,
 3285                             head->so_accf->so_accept_filter_arg, M_DONTWAIT);
 3286                         if (ret == SU_ISCONNECTED)
 3287                                 soupcall_clear(so, SO_RCV);
 3288                         SOCK_UNLOCK(so);
 3289                         if (ret == SU_ISCONNECTED)
 3290                                 goto restart;
 3291                 }
 3292                 return;
 3293         }
 3294         SOCK_UNLOCK(so);
 3295         ACCEPT_UNLOCK();
 3296         wakeup(&so->so_timeo);
 3297         sorwakeup(so);
 3298         sowwakeup(so);
 3299 }
 3300 
 3301 void
 3302 soisdisconnecting(struct socket *so)
 3303 {
 3304 
 3305         /*
 3306          * Note: This code assumes that SOCK_LOCK(so) and
 3307          * SOCKBUF_LOCK(&so->so_rcv) are the same.
 3308          */
 3309         SOCKBUF_LOCK(&so->so_rcv);
 3310         so->so_state &= ~SS_ISCONNECTING;
 3311         so->so_state |= SS_ISDISCONNECTING;
 3312         so->so_rcv.sb_state |= SBS_CANTRCVMORE;
 3313         sorwakeup_locked(so);
 3314         SOCKBUF_LOCK(&so->so_snd);
 3315         so->so_snd.sb_state |= SBS_CANTSENDMORE;
 3316         sowwakeup_locked(so);
 3317         wakeup(&so->so_timeo);
 3318 }
 3319 
 3320 void
 3321 soisdisconnected(struct socket *so)
 3322 {
 3323 
 3324         /*
 3325          * Note: This code assumes that SOCK_LOCK(so) and
 3326          * SOCKBUF_LOCK(&so->so_rcv) are the same.
 3327          */
 3328         SOCKBUF_LOCK(&so->so_rcv);
 3329         so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
 3330         so->so_state |= SS_ISDISCONNECTED;
 3331         so->so_rcv.sb_state |= SBS_CANTRCVMORE;
 3332         sorwakeup_locked(so);
 3333         SOCKBUF_LOCK(&so->so_snd);
 3334         so->so_snd.sb_state |= SBS_CANTSENDMORE;
 3335         sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
 3336         sowwakeup_locked(so);
 3337         wakeup(&so->so_timeo);
 3338 }
 3339 
 3340 /*
 3341  * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
 3342  */
 3343 struct sockaddr *
 3344 sodupsockaddr(const struct sockaddr *sa, int mflags)
 3345 {
 3346         struct sockaddr *sa2;
 3347 
 3348         sa2 = malloc(sa->sa_len, M_SONAME, mflags);
 3349         if (sa2)
 3350                 bcopy(sa, sa2, sa->sa_len);
 3351         return sa2;
 3352 }
 3353 
 3354 /*
 3355  * Register per-socket buffer upcalls.
 3356  */
 3357 void
 3358 soupcall_set(struct socket *so, int which,
 3359     int (*func)(struct socket *, void *, int), void *arg)
 3360 {
 3361         struct sockbuf *sb;
 3362         
 3363         switch (which) {
 3364         case SO_RCV:
 3365                 sb = &so->so_rcv;
 3366                 break;
 3367         case SO_SND:
 3368                 sb = &so->so_snd;
 3369                 break;
 3370         default:
 3371                 panic("soupcall_set: bad which");
 3372         }
 3373         SOCKBUF_LOCK_ASSERT(sb);
 3374 #if 0
 3375         /* XXX: accf_http actually wants to do this on purpose. */
 3376         KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall"));
 3377 #endif
 3378         sb->sb_upcall = func;
 3379         sb->sb_upcallarg = arg;
 3380         sb->sb_flags |= SB_UPCALL;
 3381 }
 3382 
 3383 void
 3384 soupcall_clear(struct socket *so, int which)
 3385 {
 3386         struct sockbuf *sb;
 3387 
 3388         switch (which) {
 3389         case SO_RCV:
 3390                 sb = &so->so_rcv;
 3391                 break;
 3392         case SO_SND:
 3393                 sb = &so->so_snd;
 3394                 break;
 3395         default:
 3396                 panic("soupcall_clear: bad which");
 3397         }
 3398         SOCKBUF_LOCK_ASSERT(sb);
 3399         KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear"));
 3400         sb->sb_upcall = NULL;
 3401         sb->sb_upcallarg = NULL;
 3402         sb->sb_flags &= ~SB_UPCALL;
 3403 }
 3404 
 3405 /*
 3406  * Create an external-format (``xsocket'') structure using the information in
 3407  * the kernel-format socket structure pointed to by so.  This is done to
 3408  * reduce the spew of irrelevant information over this interface, to isolate
 3409  * user code from changes in the kernel structure, and potentially to provide
 3410  * information-hiding if we decide that some of this information should be
 3411  * hidden from users.
 3412  */
 3413 void
 3414 sotoxsocket(struct socket *so, struct xsocket *xso)
 3415 {
 3416 
 3417         xso->xso_len = sizeof *xso;
 3418         xso->xso_so = so;
 3419         xso->so_type = so->so_type;
 3420         xso->so_options = so->so_options;
 3421         xso->so_linger = so->so_linger;
 3422         xso->so_state = so->so_state;
 3423         xso->so_pcb = so->so_pcb;
 3424         xso->xso_protocol = so->so_proto->pr_protocol;
 3425         xso->xso_family = so->so_proto->pr_domain->dom_family;
 3426         xso->so_qlen = so->so_qlen;
 3427         xso->so_incqlen = so->so_incqlen;
 3428         xso->so_qlimit = so->so_qlimit;
 3429         xso->so_timeo = so->so_timeo;
 3430         xso->so_error = so->so_error;
 3431         xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
 3432         xso->so_oobmark = so->so_oobmark;
 3433         sbtoxsockbuf(&so->so_snd, &xso->so_snd);
 3434         sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
 3435         xso->so_uid = so->so_cred->cr_uid;
 3436 }
 3437 
 3438 
 3439 /*
 3440  * Socket accessor functions to provide external consumers with
 3441  * a safe interface to socket state
 3442  *
 3443  */
 3444 
 3445 void
 3446 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg)
 3447 {
 3448         
 3449         TAILQ_FOREACH(so, &so->so_comp, so_list)
 3450                 func(so, arg);
 3451 }
 3452 
 3453 struct sockbuf *
 3454 so_sockbuf_rcv(struct socket *so)
 3455 {
 3456 
 3457         return (&so->so_rcv);
 3458 }
 3459 
 3460 struct sockbuf *
 3461 so_sockbuf_snd(struct socket *so)
 3462 {
 3463 
 3464         return (&so->so_snd);
 3465 }
 3466 
 3467 int
 3468 so_state_get(const struct socket *so)
 3469 {
 3470 
 3471         return (so->so_state);
 3472 }
 3473 
 3474 void
 3475 so_state_set(struct socket *so, int val)
 3476 {
 3477 
 3478         so->so_state = val;
 3479 }
 3480 
 3481 int
 3482 so_options_get(const struct socket *so)
 3483 {
 3484 
 3485         return (so->so_options);
 3486 }
 3487 
 3488 void
 3489 so_options_set(struct socket *so, int val)
 3490 {
 3491 
 3492         so->so_options = val;
 3493 }
 3494 
 3495 int
 3496 so_error_get(const struct socket *so)
 3497 {
 3498 
 3499         return (so->so_error);
 3500 }
 3501 
 3502 void
 3503 so_error_set(struct socket *so, int val)
 3504 {
 3505 
 3506         so->so_error = val;
 3507 }
 3508 
 3509 int
 3510 so_linger_get(const struct socket *so)
 3511 {
 3512 
 3513         return (so->so_linger);
 3514 }
 3515 
 3516 void
 3517 so_linger_set(struct socket *so, int val)
 3518 {
 3519 
 3520         so->so_linger = val;
 3521 }
 3522 
 3523 struct protosw *
 3524 so_protosw_get(const struct socket *so)
 3525 {
 3526 
 3527         return (so->so_proto);
 3528 }
 3529 
 3530 void
 3531 so_protosw_set(struct socket *so, struct protosw *val)
 3532 {
 3533 
 3534         so->so_proto = val;
 3535 }
 3536 
 3537 void
 3538 so_sorwakeup(struct socket *so)
 3539 {
 3540 
 3541         sorwakeup(so);
 3542 }
 3543 
 3544 void
 3545 so_sowwakeup(struct socket *so)
 3546 {
 3547 
 3548         sowwakeup(so);
 3549 }
 3550 
 3551 void
 3552 so_sorwakeup_locked(struct socket *so)
 3553 {
 3554 
 3555         sorwakeup_locked(so);
 3556 }
 3557 
 3558 void
 3559 so_sowwakeup_locked(struct socket *so)
 3560 {
 3561 
 3562         sowwakeup_locked(so);
 3563 }
 3564 
 3565 void
 3566 so_lock(struct socket *so)
 3567 {
 3568         SOCK_LOCK(so);
 3569 }
 3570 
 3571 void
 3572 so_unlock(struct socket *so)
 3573 {
 3574         SOCK_UNLOCK(so);
 3575 }

Cache object: 90efbbde14c328b3a25a299593303636


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.