The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ 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  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 4411f912c08c1ffdbbd38e41971a24ef


[ 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.