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) 2004 The FreeBSD Foundation
    3  * Copyright (c) 2004-2005 Robert N. M. Watson
    4  * Copyright (c) 1982, 1986, 1988, 1990, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 4. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      @(#)uipc_socket.c       8.3 (Berkeley) 4/15/94
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD: releng/6.1/sys/kern/uipc_socket.c 158179 2006-04-30 16:44:43Z cvs2svn $");
   36 
   37 #include "opt_inet.h"
   38 #include "opt_mac.h"
   39 #include "opt_zero.h"
   40 #include "opt_compat.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/fcntl.h>
   45 #include <sys/limits.h>
   46 #include <sys/lock.h>
   47 #include <sys/mac.h>
   48 #include <sys/malloc.h>
   49 #include <sys/mbuf.h>
   50 #include <sys/mutex.h>
   51 #include <sys/domain.h>
   52 #include <sys/file.h>                   /* for struct knote */
   53 #include <sys/kernel.h>
   54 #include <sys/event.h>
   55 #include <sys/poll.h>
   56 #include <sys/proc.h>
   57 #include <sys/protosw.h>
   58 #include <sys/socket.h>
   59 #include <sys/socketvar.h>
   60 #include <sys/resourcevar.h>
   61 #include <sys/signalvar.h>
   62 #include <sys/sysctl.h>
   63 #include <sys/uio.h>
   64 #include <sys/jail.h>
   65 
   66 #include <vm/uma.h>
   67 
   68 #ifdef COMPAT_IA32
   69 #include <sys/mount.h>
   70 #include <compat/freebsd32/freebsd32.h>
   71 
   72 extern struct sysentvec ia32_freebsd_sysvec;
   73 #endif
   74 
   75 static int      soreceive_rcvoob(struct socket *so, struct uio *uio,
   76                     int flags);
   77 
   78 static void     filt_sordetach(struct knote *kn);
   79 static int      filt_soread(struct knote *kn, long hint);
   80 static void     filt_sowdetach(struct knote *kn);
   81 static int      filt_sowrite(struct knote *kn, long hint);
   82 static int      filt_solisten(struct knote *kn, long hint);
   83 
   84 static struct filterops solisten_filtops =
   85         { 1, NULL, filt_sordetach, filt_solisten };
   86 static struct filterops soread_filtops =
   87         { 1, NULL, filt_sordetach, filt_soread };
   88 static struct filterops sowrite_filtops =
   89         { 1, NULL, filt_sowdetach, filt_sowrite };
   90 
   91 uma_zone_t socket_zone;
   92 so_gen_t        so_gencnt;      /* generation count for sockets */
   93 
   94 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
   95 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
   96 
   97 SYSCTL_DECL(_kern_ipc);
   98 
   99 static int somaxconn = SOMAXCONN;
  100 static int somaxconn_sysctl(SYSCTL_HANDLER_ARGS);
  101 /* XXX: we dont have SYSCTL_USHORT */
  102 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLTYPE_UINT | CTLFLAG_RW,
  103     0, sizeof(int), somaxconn_sysctl, "I", "Maximum pending socket connection "
  104     "queue size");
  105 static int numopensockets;
  106 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
  107     &numopensockets, 0, "Number of open sockets");
  108 #ifdef ZERO_COPY_SOCKETS
  109 /* These aren't static because they're used in other files. */
  110 int so_zero_copy_send = 1;
  111 int so_zero_copy_receive = 1;
  112 SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0,
  113     "Zero copy controls");
  114 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW,
  115     &so_zero_copy_receive, 0, "Enable zero copy receive");
  116 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW,
  117     &so_zero_copy_send, 0, "Enable zero copy send");
  118 #endif /* ZERO_COPY_SOCKETS */
  119 
  120 /*
  121  * accept_mtx locks down per-socket fields relating to accept queues.  See
  122  * socketvar.h for an annotation of the protected fields of struct socket.
  123  */
  124 struct mtx accept_mtx;
  125 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
  126 
  127 /*
  128  * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
  129  * so_gencnt field.
  130  */
  131 static struct mtx so_global_mtx;
  132 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
  133 
  134 /*
  135  * Socket operation routines.
  136  * These routines are called by the routines in
  137  * sys_socket.c or from a system process, and
  138  * implement the semantics of socket operations by
  139  * switching out to the protocol specific routines.
  140  */
  141 
  142 /*
  143  * Get a socket structure from our zone, and initialize it.
  144  * Note that it would probably be better to allocate socket
  145  * and PCB at the same time, but I'm not convinced that all
  146  * the protocols can be easily modified to do this.
  147  *
  148  * soalloc() returns a socket with a ref count of 0.
  149  */
  150 struct socket *
  151 soalloc(int mflags)
  152 {
  153         struct socket *so;
  154 
  155         so = uma_zalloc(socket_zone, mflags | M_ZERO);
  156         if (so != NULL) {
  157 #ifdef MAC
  158                 if (mac_init_socket(so, mflags) != 0) {
  159                         uma_zfree(socket_zone, so);
  160                         return (NULL);
  161                 }
  162 #endif
  163                 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
  164                 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
  165                 TAILQ_INIT(&so->so_aiojobq);
  166                 mtx_lock(&so_global_mtx);
  167                 so->so_gencnt = ++so_gencnt;
  168                 ++numopensockets;
  169                 mtx_unlock(&so_global_mtx);
  170         }
  171         return (so);
  172 }
  173 
  174 /*
  175  * socreate returns a socket with a ref count of 1.  The socket should be
  176  * closed with soclose().
  177  */
  178 int
  179 socreate(dom, aso, type, proto, cred, td)
  180         int dom;
  181         struct socket **aso;
  182         int type;
  183         int proto;
  184         struct ucred *cred;
  185         struct thread *td;
  186 {
  187         struct protosw *prp;
  188         struct socket *so;
  189         int error;
  190 
  191         if (proto)
  192                 prp = pffindproto(dom, proto, type);
  193         else
  194                 prp = pffindtype(dom, type);
  195 
  196         if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
  197             prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
  198                 return (EPROTONOSUPPORT);
  199 
  200         if (jailed(cred) && jail_socket_unixiproute_only &&
  201             prp->pr_domain->dom_family != PF_LOCAL &&
  202             prp->pr_domain->dom_family != PF_INET &&
  203             prp->pr_domain->dom_family != PF_ROUTE) {
  204                 return (EPROTONOSUPPORT);
  205         }
  206 
  207         if (prp->pr_type != type)
  208                 return (EPROTOTYPE);
  209         so = soalloc(M_WAITOK);
  210         if (so == NULL)
  211                 return (ENOBUFS);
  212 
  213         TAILQ_INIT(&so->so_incomp);
  214         TAILQ_INIT(&so->so_comp);
  215         so->so_type = type;
  216         so->so_cred = crhold(cred);
  217         so->so_proto = prp;
  218 #ifdef MAC
  219         mac_create_socket(cred, so);
  220 #endif
  221         knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv),
  222             NULL, NULL, NULL);
  223         knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd),
  224             NULL, NULL, NULL);
  225         so->so_count = 1;
  226         error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
  227         if (error) {
  228                 ACCEPT_LOCK();
  229                 SOCK_LOCK(so);
  230                 so->so_state |= SS_NOFDREF;
  231                 sorele(so);
  232                 return (error);
  233         }
  234         *aso = so;
  235         return (0);
  236 }
  237 
  238 int
  239 sobind(so, nam, td)
  240         struct socket *so;
  241         struct sockaddr *nam;
  242         struct thread *td;
  243 {
  244 
  245         return ((*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td));
  246 }
  247 
  248 void
  249 sodealloc(struct socket *so)
  250 {
  251 
  252         KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
  253         mtx_lock(&so_global_mtx);
  254         so->so_gencnt = ++so_gencnt;
  255         mtx_unlock(&so_global_mtx);
  256         if (so->so_rcv.sb_hiwat)
  257                 (void)chgsbsize(so->so_cred->cr_uidinfo,
  258                     &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
  259         if (so->so_snd.sb_hiwat)
  260                 (void)chgsbsize(so->so_cred->cr_uidinfo,
  261                     &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
  262 #ifdef INET
  263         /* remove acccept filter if one is present. */
  264         if (so->so_accf != NULL)
  265                 do_setopt_accept_filter(so, NULL);
  266 #endif
  267 #ifdef MAC
  268         mac_destroy_socket(so);
  269 #endif
  270         crfree(so->so_cred);
  271         SOCKBUF_LOCK_DESTROY(&so->so_snd);
  272         SOCKBUF_LOCK_DESTROY(&so->so_rcv);
  273         uma_zfree(socket_zone, so);
  274         mtx_lock(&so_global_mtx);
  275         --numopensockets;
  276         mtx_unlock(&so_global_mtx);
  277 }
  278 
  279 /*
  280  * solisten() transitions a socket from a non-listening state to a listening
  281  * state, but can also be used to update the listen queue depth on an
  282  * existing listen socket.  The protocol will call back into the sockets
  283  * layer using solisten_proto_check() and solisten_proto() to check and set
  284  * socket-layer listen state.  Call backs are used so that the protocol can
  285  * acquire both protocol and socket layer locks in whatever order is required
  286  * by the protocol.
  287  *
  288  * Protocol implementors are advised to hold the socket lock across the
  289  * socket-layer test and set to avoid races at the socket layer.
  290  */
  291 int
  292 solisten(so, backlog, td)
  293         struct socket *so;
  294         int backlog;
  295         struct thread *td;
  296 {
  297         int error;
  298 
  299         error = (*so->so_proto->pr_usrreqs->pru_listen)(so, td);
  300         if (error)
  301                 return (error);
  302 
  303         /*
  304          * XXXRW: The following state adjustment should occur in
  305          * solisten_proto(), but we don't currently pass the backlog request
  306          * to the protocol via pru_listen().
  307          */
  308         if (backlog < 0 || backlog > somaxconn)
  309                 backlog = somaxconn;
  310         so->so_qlimit = backlog;
  311         return (0);
  312 }
  313 
  314 int
  315 solisten_proto_check(so)
  316         struct socket *so;
  317 {
  318 
  319         SOCK_LOCK_ASSERT(so);
  320 
  321         if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
  322             SS_ISDISCONNECTING))
  323                 return (EINVAL);
  324         return (0);
  325 }
  326 
  327 void
  328 solisten_proto(so)
  329         struct socket *so;
  330 {
  331 
  332         SOCK_LOCK_ASSERT(so);
  333 
  334         so->so_options |= SO_ACCEPTCONN;
  335 }
  336 
  337 /*
  338  * Attempt to free a socket.  This should really be sotryfree().
  339  *
  340  * We free the socket if the protocol is no longer interested in the socket,
  341  * there's no file descriptor reference, and the refcount is 0.  While the
  342  * calling macro sotryfree() tests the refcount, sofree() has to test it
  343  * again as it's possible to race with an accept()ing thread if the socket is
  344  * in an listen queue of a listen socket, as being in the listen queue
  345  * doesn't elevate the reference count.  sofree() acquires the accept mutex
  346  * early for this test in order to avoid that race.
  347  */
  348 void
  349 sofree(so)
  350         struct socket *so;
  351 {
  352         struct socket *head;
  353 
  354         ACCEPT_LOCK_ASSERT();
  355         SOCK_LOCK_ASSERT(so);
  356 
  357         if (so->so_pcb != NULL || (so->so_state & SS_NOFDREF) == 0 ||
  358             so->so_count != 0) {
  359                 SOCK_UNLOCK(so);
  360                 ACCEPT_UNLOCK();
  361                 return;
  362         }
  363 
  364         head = so->so_head;
  365         if (head != NULL) {
  366                 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
  367                     (so->so_qstate & SQ_INCOMP) != 0,
  368                     ("sofree: so_head != NULL, but neither SQ_COMP nor "
  369                     "SQ_INCOMP"));
  370                 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
  371                     (so->so_qstate & SQ_INCOMP) == 0,
  372                     ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
  373                 /*
  374                  * accept(2) is responsible draining the completed
  375                  * connection queue and freeing those sockets, so
  376                  * we just return here if this socket is currently
  377                  * on the completed connection queue.  Otherwise,
  378                  * accept(2) may hang after select(2) has indicating
  379                  * that a listening socket was ready.  If it's an
  380                  * incomplete connection, we remove it from the queue
  381                  * and free it; otherwise, it won't be released until
  382                  * the listening socket is closed.
  383                  */
  384                 if ((so->so_qstate & SQ_COMP) != 0) {
  385                         SOCK_UNLOCK(so);
  386                         ACCEPT_UNLOCK();
  387                         return;
  388                 }
  389                 TAILQ_REMOVE(&head->so_incomp, so, so_list);
  390                 head->so_incqlen--;
  391                 so->so_qstate &= ~SQ_INCOMP;
  392                 so->so_head = NULL;
  393         }
  394         KASSERT((so->so_qstate & SQ_COMP) == 0 &&
  395             (so->so_qstate & SQ_INCOMP) == 0,
  396             ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
  397             so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
  398         SOCK_UNLOCK(so);
  399         ACCEPT_UNLOCK();
  400         SOCKBUF_LOCK(&so->so_snd);
  401         so->so_snd.sb_flags |= SB_NOINTR;
  402         (void)sblock(&so->so_snd, M_WAITOK);
  403         /*
  404          * socantsendmore_locked() drops the socket buffer mutex so that it
  405          * can safely perform wakeups.  Re-acquire the mutex before
  406          * continuing.
  407          */
  408         socantsendmore_locked(so);
  409         SOCKBUF_LOCK(&so->so_snd);
  410         sbunlock(&so->so_snd);
  411         sbrelease_locked(&so->so_snd, so);
  412         SOCKBUF_UNLOCK(&so->so_snd);
  413         sorflush(so);
  414         knlist_destroy(&so->so_rcv.sb_sel.si_note);
  415         knlist_destroy(&so->so_snd.sb_sel.si_note);
  416         sodealloc(so);
  417 }
  418 
  419 /*
  420  * Close a socket on last file table reference removal.
  421  * Initiate disconnect if connected.
  422  * Free socket when disconnect complete.
  423  *
  424  * This function will sorele() the socket.  Note that soclose() may be
  425  * called prior to the ref count reaching zero.  The actual socket
  426  * structure will not be freed until the ref count reaches zero.
  427  */
  428 int
  429 soclose(so)
  430         struct socket *so;
  431 {
  432         int error = 0;
  433 
  434         KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
  435 
  436         funsetown(&so->so_sigio);
  437         if (so->so_options & SO_ACCEPTCONN) {
  438                 struct socket *sp;
  439                 ACCEPT_LOCK();
  440                 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
  441                         TAILQ_REMOVE(&so->so_incomp, sp, so_list);
  442                         so->so_incqlen--;
  443                         sp->so_qstate &= ~SQ_INCOMP;
  444                         sp->so_head = NULL;
  445                         ACCEPT_UNLOCK();
  446                         (void) soabort(sp);
  447                         ACCEPT_LOCK();
  448                 }
  449                 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
  450                         TAILQ_REMOVE(&so->so_comp, sp, so_list);
  451                         so->so_qlen--;
  452                         sp->so_qstate &= ~SQ_COMP;
  453                         sp->so_head = NULL;
  454                         ACCEPT_UNLOCK();
  455                         (void) soabort(sp);
  456                         ACCEPT_LOCK();
  457                 }
  458                 ACCEPT_UNLOCK();
  459         }
  460         if (so->so_pcb == NULL)
  461                 goto discard;
  462         if (so->so_state & SS_ISCONNECTED) {
  463                 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
  464                         error = sodisconnect(so);
  465                         if (error)
  466                                 goto drop;
  467                 }
  468                 if (so->so_options & SO_LINGER) {
  469                         if ((so->so_state & SS_ISDISCONNECTING) &&
  470                             (so->so_state & SS_NBIO))
  471                                 goto drop;
  472                         while (so->so_state & SS_ISCONNECTED) {
  473                                 error = tsleep(&so->so_timeo,
  474                                     PSOCK | PCATCH, "soclos", so->so_linger * hz);
  475                                 if (error)
  476                                         break;
  477                         }
  478                 }
  479         }
  480 drop:
  481         if (so->so_pcb != NULL) {
  482                 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
  483                 if (error == 0)
  484                         error = error2;
  485         }
  486 discard:
  487         ACCEPT_LOCK();
  488         SOCK_LOCK(so);
  489         KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
  490         so->so_state |= SS_NOFDREF;
  491         sorele(so);
  492         return (error);
  493 }
  494 
  495 /*
  496  * soabort() must not be called with any socket locks held, as it calls
  497  * into the protocol, which will call back into the socket code causing
  498  * it to acquire additional socket locks that may cause recursion or lock
  499  * order reversals.
  500  */
  501 int
  502 soabort(so)
  503         struct socket *so;
  504 {
  505         int error;
  506 
  507         error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
  508         if (error) {
  509                 ACCEPT_LOCK();
  510                 SOCK_LOCK(so);
  511                 sotryfree(so);  /* note: does not decrement the ref count */
  512                 return error;
  513         }
  514         return (0);
  515 }
  516 
  517 int
  518 soaccept(so, nam)
  519         struct socket *so;
  520         struct sockaddr **nam;
  521 {
  522         int error;
  523 
  524         SOCK_LOCK(so);
  525         KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
  526         so->so_state &= ~SS_NOFDREF;
  527         SOCK_UNLOCK(so);
  528         error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
  529         return (error);
  530 }
  531 
  532 int
  533 soconnect(so, nam, td)
  534         struct socket *so;
  535         struct sockaddr *nam;
  536         struct thread *td;
  537 {
  538         int error;
  539 
  540         if (so->so_options & SO_ACCEPTCONN)
  541                 return (EOPNOTSUPP);
  542         /*
  543          * If protocol is connection-based, can only connect once.
  544          * Otherwise, if connected, try to disconnect first.
  545          * This allows user to disconnect by connecting to, e.g.,
  546          * a null address.
  547          */
  548         if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
  549             ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
  550             (error = sodisconnect(so)))) {
  551                 error = EISCONN;
  552         } else {
  553                 /*
  554                  * Prevent accumulated error from previous connection
  555                  * from biting us.
  556                  */
  557                 so->so_error = 0;
  558                 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
  559         }
  560 
  561         return (error);
  562 }
  563 
  564 int
  565 soconnect2(so1, so2)
  566         struct socket *so1;
  567         struct socket *so2;
  568 {
  569 
  570         return ((*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2));
  571 }
  572 
  573 int
  574 sodisconnect(so)
  575         struct socket *so;
  576 {
  577         int error;
  578 
  579         if ((so->so_state & SS_ISCONNECTED) == 0)
  580                 return (ENOTCONN);
  581         if (so->so_state & SS_ISDISCONNECTING)
  582                 return (EALREADY);
  583         error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
  584         return (error);
  585 }
  586 
  587 #define SBLOCKWAIT(f)   (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
  588 /*
  589  * Send on a socket.
  590  * If send must go all at once and message is larger than
  591  * send buffering, then hard error.
  592  * Lock against other senders.
  593  * If must go all at once and not enough room now, then
  594  * inform user that this would block and do nothing.
  595  * Otherwise, if nonblocking, send as much as possible.
  596  * The data to be sent is described by "uio" if nonzero,
  597  * otherwise by the mbuf chain "top" (which must be null
  598  * if uio is not).  Data provided in mbuf chain must be small
  599  * enough to send all at once.
  600  *
  601  * Returns nonzero on error, timeout or signal; callers
  602  * must check for short counts if EINTR/ERESTART are returned.
  603  * Data and control buffers are freed on return.
  604  */
  605 
  606 #ifdef ZERO_COPY_SOCKETS
  607 struct so_zerocopy_stats{
  608         int size_ok;
  609         int align_ok;
  610         int found_ifp;
  611 };
  612 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
  613 #include <netinet/in.h>
  614 #include <net/route.h>
  615 #include <netinet/in_pcb.h>
  616 #include <vm/vm.h>
  617 #include <vm/vm_page.h>
  618 #include <vm/vm_object.h>
  619 #endif /*ZERO_COPY_SOCKETS*/
  620 
  621 int
  622 sosend(so, addr, uio, top, control, flags, td)
  623         struct socket *so;
  624         struct sockaddr *addr;
  625         struct uio *uio;
  626         struct mbuf *top;
  627         struct mbuf *control;
  628         int flags;
  629         struct thread *td;
  630 {
  631         struct mbuf **mp;
  632         struct mbuf *m;
  633         long space, len = 0, resid;
  634         int clen = 0, error, dontroute;
  635         int atomic = sosendallatonce(so) || top;
  636 #ifdef ZERO_COPY_SOCKETS
  637         int cow_send;
  638 #endif /* ZERO_COPY_SOCKETS */
  639 
  640         if (uio != NULL)
  641                 resid = uio->uio_resid;
  642         else
  643                 resid = top->m_pkthdr.len;
  644         /*
  645          * In theory resid should be unsigned.
  646          * However, space must be signed, as it might be less than 0
  647          * if we over-committed, and we must use a signed comparison
  648          * of space and resid.  On the other hand, a negative resid
  649          * causes us to loop sending 0-length segments to the protocol.
  650          *
  651          * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
  652          * type sockets since that's an error.
  653          */
  654         if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
  655                 error = EINVAL;
  656                 goto out;
  657         }
  658 
  659         dontroute =
  660             (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
  661             (so->so_proto->pr_flags & PR_ATOMIC);
  662         if (td != NULL)
  663                 td->td_proc->p_stats->p_ru.ru_msgsnd++;
  664         if (control != NULL)
  665                 clen = control->m_len;
  666 #define snderr(errno)   { error = (errno); goto release; }
  667 
  668         SOCKBUF_LOCK(&so->so_snd);
  669 restart:
  670         SOCKBUF_LOCK_ASSERT(&so->so_snd);
  671         error = sblock(&so->so_snd, SBLOCKWAIT(flags));
  672         if (error)
  673                 goto out_locked;
  674         do {
  675                 SOCKBUF_LOCK_ASSERT(&so->so_snd);
  676                 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
  677                         snderr(EPIPE);
  678                 if (so->so_error) {
  679                         error = so->so_error;
  680                         so->so_error = 0;
  681                         goto release;
  682                 }
  683                 if ((so->so_state & SS_ISCONNECTED) == 0) {
  684                         /*
  685                          * `sendto' and `sendmsg' is allowed on a connection-
  686                          * based socket if it supports implied connect.
  687                          * Return ENOTCONN if not connected and no address is
  688                          * supplied.
  689                          */
  690                         if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
  691                             (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
  692                                 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
  693                                     !(resid == 0 && clen != 0))
  694                                         snderr(ENOTCONN);
  695                         } else if (addr == NULL)
  696                             snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
  697                                    ENOTCONN : EDESTADDRREQ);
  698                 }
  699                 space = sbspace(&so->so_snd);
  700                 if (flags & MSG_OOB)
  701                         space += 1024;
  702                 if ((atomic && resid > so->so_snd.sb_hiwat) ||
  703                     clen > so->so_snd.sb_hiwat)
  704                         snderr(EMSGSIZE);
  705                 if (space < resid + clen &&
  706                     (atomic || space < so->so_snd.sb_lowat || space < clen)) {
  707                         if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO))
  708                                 snderr(EWOULDBLOCK);
  709                         sbunlock(&so->so_snd);
  710                         error = sbwait(&so->so_snd);
  711                         if (error)
  712                                 goto out_locked;
  713                         goto restart;
  714                 }
  715                 SOCKBUF_UNLOCK(&so->so_snd);
  716                 mp = &top;
  717                 space -= clen;
  718                 do {
  719                     if (uio == NULL) {
  720                         /*
  721                          * Data is prepackaged in "top".
  722                          */
  723                         resid = 0;
  724                         if (flags & MSG_EOR)
  725                                 top->m_flags |= M_EOR;
  726                     } else do {
  727 #ifdef ZERO_COPY_SOCKETS
  728                         cow_send = 0;
  729 #endif /* ZERO_COPY_SOCKETS */
  730                         if (resid >= MINCLSIZE) {
  731 #ifdef ZERO_COPY_SOCKETS
  732                                 if (top == NULL) {
  733                                         MGETHDR(m, M_TRYWAIT, MT_DATA);
  734                                         if (m == NULL) {
  735                                                 error = ENOBUFS;
  736                                                 SOCKBUF_LOCK(&so->so_snd);
  737                                                 goto release;
  738                                         }
  739                                         m->m_pkthdr.len = 0;
  740                                         m->m_pkthdr.rcvif = NULL; 
  741                                 } else {
  742                                         MGET(m, M_TRYWAIT, MT_DATA);
  743                                         if (m == NULL) {
  744                                                 error = ENOBUFS;
  745                                                 SOCKBUF_LOCK(&so->so_snd);
  746                                                 goto release;
  747                                         }
  748                                 }
  749                                 if (so_zero_copy_send &&
  750                                     resid>=PAGE_SIZE &&
  751                                     space>=PAGE_SIZE &&
  752                                     uio->uio_iov->iov_len>=PAGE_SIZE) {
  753                                         so_zerocp_stats.size_ok++;
  754                                         so_zerocp_stats.align_ok++;
  755                                         cow_send = socow_setup(m, uio);
  756                                         len = cow_send;
  757                                 }
  758                                 if (!cow_send) {
  759                                         MCLGET(m, M_TRYWAIT);
  760                                         if ((m->m_flags & M_EXT) == 0) {
  761                                                 m_free(m);
  762                                                 m = NULL;
  763                                         } else {
  764                                                 len = min(min(MCLBYTES, resid), space);
  765                                         }
  766                                 }
  767 #else /* ZERO_COPY_SOCKETS */
  768                                 if (top == NULL) {
  769                                         m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
  770                                         m->m_pkthdr.len = 0;
  771                                         m->m_pkthdr.rcvif = NULL;
  772                                 } else
  773                                         m = m_getcl(M_TRYWAIT, MT_DATA, 0);
  774                                 len = min(min(MCLBYTES, resid), space);
  775 #endif /* ZERO_COPY_SOCKETS */
  776                         } else {
  777                                 if (top == NULL) {
  778                                         m = m_gethdr(M_TRYWAIT, MT_DATA);
  779                                         m->m_pkthdr.len = 0;
  780                                         m->m_pkthdr.rcvif = NULL;
  781 
  782                                         len = min(min(MHLEN, resid), space);
  783                                         /*
  784                                          * For datagram protocols, leave room
  785                                          * for protocol headers in first mbuf.
  786                                          */
  787                                         if (atomic && m && len < MHLEN)
  788                                                 MH_ALIGN(m, len);
  789                                 } else {
  790                                         m = m_get(M_TRYWAIT, MT_DATA);
  791                                         len = min(min(MLEN, resid), space);
  792                                 }
  793                         }
  794                         if (m == NULL) {
  795                                 error = ENOBUFS;
  796                                 SOCKBUF_LOCK(&so->so_snd);
  797                                 goto release;
  798                         }
  799 
  800                         space -= len;
  801 #ifdef ZERO_COPY_SOCKETS
  802                         if (cow_send)
  803                                 error = 0;
  804                         else
  805 #endif /* ZERO_COPY_SOCKETS */
  806                         error = uiomove(mtod(m, void *), (int)len, uio);
  807                         resid = uio->uio_resid;
  808                         m->m_len = len;
  809                         *mp = m;
  810                         top->m_pkthdr.len += len;
  811                         if (error) {
  812                                 SOCKBUF_LOCK(&so->so_snd);
  813                                 goto release;
  814                         }
  815                         mp = &m->m_next;
  816                         if (resid <= 0) {
  817                                 if (flags & MSG_EOR)
  818                                         top->m_flags |= M_EOR;
  819                                 break;
  820                         }
  821                     } while (space > 0 && atomic);
  822                     if (dontroute) {
  823                             SOCK_LOCK(so);
  824                             so->so_options |= SO_DONTROUTE;
  825                             SOCK_UNLOCK(so);
  826                     }
  827                     /*
  828                      * XXX all the SBS_CANTSENDMORE checks previously
  829                      * done could be out of date.  We could have recieved
  830                      * a reset packet in an interrupt or maybe we slept
  831                      * while doing page faults in uiomove() etc. We could
  832                      * probably recheck again inside the locking protection
  833                      * here, but there are probably other places that this
  834                      * also happens.  We must rethink this.
  835                      */
  836                     error = (*so->so_proto->pr_usrreqs->pru_send)(so,
  837                         (flags & MSG_OOB) ? PRUS_OOB :
  838                         /*
  839                          * If the user set MSG_EOF, the protocol
  840                          * understands this flag and nothing left to
  841                          * send then use PRU_SEND_EOF instead of PRU_SEND.
  842                          */
  843                         ((flags & MSG_EOF) &&
  844                          (so->so_proto->pr_flags & PR_IMPLOPCL) &&
  845                          (resid <= 0)) ?
  846                                 PRUS_EOF :
  847                         /* If there is more to send set PRUS_MORETOCOME */
  848                         (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
  849                         top, addr, control, td);
  850                     if (dontroute) {
  851                             SOCK_LOCK(so);
  852                             so->so_options &= ~SO_DONTROUTE;
  853                             SOCK_UNLOCK(so);
  854                     }
  855                     clen = 0;
  856                     control = NULL;
  857                     top = NULL;
  858                     mp = &top;
  859                     if (error) {
  860                         SOCKBUF_LOCK(&so->so_snd);
  861                         goto release;
  862                     }
  863                 } while (resid && space > 0);
  864                 SOCKBUF_LOCK(&so->so_snd);
  865         } while (resid);
  866 
  867 release:
  868         SOCKBUF_LOCK_ASSERT(&so->so_snd);
  869         sbunlock(&so->so_snd);
  870 out_locked:
  871         SOCKBUF_LOCK_ASSERT(&so->so_snd);
  872         SOCKBUF_UNLOCK(&so->so_snd);
  873 out:
  874         if (top != NULL)
  875                 m_freem(top);
  876         if (control != NULL)
  877                 m_freem(control);
  878         return (error);
  879 }
  880 
  881 /*
  882  * The part of soreceive() that implements reading non-inline out-of-band
  883  * data from a socket.  For more complete comments, see soreceive(), from
  884  * which this code originated.
  885  *
  886  * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
  887  * unable to return an mbuf chain to the caller.
  888  */
  889 static int
  890 soreceive_rcvoob(so, uio, flags)
  891         struct socket *so;
  892         struct uio *uio;
  893         int flags;
  894 {
  895         struct protosw *pr = so->so_proto;
  896         struct mbuf *m;
  897         int error;
  898 
  899         KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
  900 
  901         m = m_get(M_TRYWAIT, MT_DATA);
  902         if (m == NULL)
  903                 return (ENOBUFS);
  904         error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
  905         if (error)
  906                 goto bad;
  907         do {
  908 #ifdef ZERO_COPY_SOCKETS
  909                 if (so_zero_copy_receive) {
  910                         int disposable;
  911 
  912                         if ((m->m_flags & M_EXT)
  913                          && (m->m_ext.ext_type == EXT_DISPOSABLE))
  914                                 disposable = 1;
  915                         else
  916                                 disposable = 0;
  917 
  918                         error = uiomoveco(mtod(m, void *),
  919                                           min(uio->uio_resid, m->m_len),
  920                                           uio, disposable);
  921                 } else
  922 #endif /* ZERO_COPY_SOCKETS */
  923                 error = uiomove(mtod(m, void *),
  924                     (int) min(uio->uio_resid, m->m_len), uio);
  925                 m = m_free(m);
  926         } while (uio->uio_resid && error == 0 && m);
  927 bad:
  928         if (m != NULL)
  929                 m_freem(m);
  930         return (error);
  931 }
  932 
  933 /*
  934  * Following replacement or removal of the first mbuf on the first mbuf chain
  935  * of a socket buffer, push necessary state changes back into the socket
  936  * buffer so that other consumers see the values consistently.  'nextrecord'
  937  * is the callers locally stored value of the original value of
  938  * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
  939  * NOTE: 'nextrecord' may be NULL.
  940  */
  941 static __inline void
  942 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
  943 {
  944 
  945         SOCKBUF_LOCK_ASSERT(sb);
  946         /*
  947          * First, update for the new value of nextrecord.  If necessary, make
  948          * it the first record.
  949          */
  950         if (sb->sb_mb != NULL)
  951                 sb->sb_mb->m_nextpkt = nextrecord;
  952         else
  953                 sb->sb_mb = nextrecord;
  954 
  955         /*
  956          * Now update any dependent socket buffer fields to reflect the new
  957          * state.  This is an expanded inline of SB_EMPTY_FIXUP(), with the
  958          * addition of a second clause that takes care of the case where
  959          * sb_mb has been updated, but remains the last record.
  960          */
  961         if (sb->sb_mb == NULL) {
  962                 sb->sb_mbtail = NULL;
  963                 sb->sb_lastrecord = NULL;
  964         } else if (sb->sb_mb->m_nextpkt == NULL)
  965                 sb->sb_lastrecord = sb->sb_mb;
  966 }
  967 
  968 
  969 /*
  970  * Implement receive operations on a socket.
  971  * We depend on the way that records are added to the sockbuf
  972  * by sbappend*.  In particular, each record (mbufs linked through m_next)
  973  * must begin with an address if the protocol so specifies,
  974  * followed by an optional mbuf or mbufs containing ancillary data,
  975  * and then zero or more mbufs of data.
  976  * In order to avoid blocking network interrupts for the entire time here,
  977  * we splx() while doing the actual copy to user space.
  978  * Although the sockbuf is locked, new data may still be appended,
  979  * and thus we must maintain consistency of the sockbuf during that time.
  980  *
  981  * The caller may receive the data as a single mbuf chain by supplying
  982  * an mbuf **mp0 for use in returning the chain.  The uio is then used
  983  * only for the count in uio_resid.
  984  */
  985 int
  986 soreceive(so, psa, uio, mp0, controlp, flagsp)
  987         struct socket *so;
  988         struct sockaddr **psa;
  989         struct uio *uio;
  990         struct mbuf **mp0;
  991         struct mbuf **controlp;
  992         int *flagsp;
  993 {
  994         struct mbuf *m, **mp;
  995         int flags, len, error, offset;
  996         struct protosw *pr = so->so_proto;
  997         struct mbuf *nextrecord;
  998         int moff, type = 0;
  999         int orig_resid = uio->uio_resid;
 1000 
 1001         mp = mp0;
 1002         if (psa != NULL)
 1003                 *psa = NULL;
 1004         if (controlp != NULL)
 1005                 *controlp = NULL;
 1006         if (flagsp != NULL)
 1007                 flags = *flagsp &~ MSG_EOR;
 1008         else
 1009                 flags = 0;
 1010         if (flags & MSG_OOB)
 1011                 return (soreceive_rcvoob(so, uio, flags));
 1012         if (mp != NULL)
 1013                 *mp = NULL;
 1014         if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
 1015             && uio->uio_resid)
 1016                 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
 1017 
 1018         SOCKBUF_LOCK(&so->so_rcv);
 1019 restart:
 1020         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1021         error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
 1022         if (error)
 1023                 goto out;
 1024 
 1025         m = so->so_rcv.sb_mb;
 1026         /*
 1027          * If we have less data than requested, block awaiting more
 1028          * (subject to any timeout) if:
 1029          *   1. the current count is less than the low water mark, or
 1030          *   2. MSG_WAITALL is set, and it is possible to do the entire
 1031          *      receive operation at once if we block (resid <= hiwat).
 1032          *   3. MSG_DONTWAIT is not set
 1033          * If MSG_WAITALL is set but resid is larger than the receive buffer,
 1034          * we have to do the receive in sections, and thus risk returning
 1035          * a short count if a timeout or signal occurs after we start.
 1036          */
 1037         if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
 1038             so->so_rcv.sb_cc < uio->uio_resid) &&
 1039             (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
 1040             ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
 1041             m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
 1042                 KASSERT(m != NULL || !so->so_rcv.sb_cc,
 1043                     ("receive: m == %p so->so_rcv.sb_cc == %u",
 1044                     m, so->so_rcv.sb_cc));
 1045                 if (so->so_error) {
 1046                         if (m != NULL)
 1047                                 goto dontblock;
 1048                         error = so->so_error;
 1049                         if ((flags & MSG_PEEK) == 0)
 1050                                 so->so_error = 0;
 1051                         goto release;
 1052                 }
 1053                 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1054                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
 1055                         if (m)
 1056                                 goto dontblock;
 1057                         else
 1058                                 goto release;
 1059                 }
 1060                 for (; m != NULL; m = m->m_next)
 1061                         if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
 1062                                 m = so->so_rcv.sb_mb;
 1063                                 goto dontblock;
 1064                         }
 1065                 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
 1066                     (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
 1067                         error = ENOTCONN;
 1068                         goto release;
 1069                 }
 1070                 if (uio->uio_resid == 0)
 1071                         goto release;
 1072                 if ((so->so_state & SS_NBIO) ||
 1073                     (flags & (MSG_DONTWAIT|MSG_NBIO))) {
 1074                         error = EWOULDBLOCK;
 1075                         goto release;
 1076                 }
 1077                 SBLASTRECORDCHK(&so->so_rcv);
 1078                 SBLASTMBUFCHK(&so->so_rcv);
 1079                 sbunlock(&so->so_rcv);
 1080                 error = sbwait(&so->so_rcv);
 1081                 if (error)
 1082                         goto out;
 1083                 goto restart;
 1084         }
 1085 dontblock:
 1086         /*
 1087          * From this point onward, we maintain 'nextrecord' as a cache of the
 1088          * pointer to the next record in the socket buffer.  We must keep the
 1089          * various socket buffer pointers and local stack versions of the
 1090          * pointers in sync, pushing out modifications before dropping the
 1091          * socket buffer mutex, and re-reading them when picking it up.
 1092          *
 1093          * Otherwise, we will race with the network stack appending new data
 1094          * or records onto the socket buffer by using inconsistent/stale
 1095          * versions of the field, possibly resulting in socket buffer
 1096          * corruption.
 1097          *
 1098          * By holding the high-level sblock(), we prevent simultaneous
 1099          * readers from pulling off the front of the socket buffer.
 1100          */
 1101         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1102         if (uio->uio_td)
 1103                 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++;
 1104         KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
 1105         SBLASTRECORDCHK(&so->so_rcv);
 1106         SBLASTMBUFCHK(&so->so_rcv);
 1107         nextrecord = m->m_nextpkt;
 1108         if (pr->pr_flags & PR_ADDR) {
 1109                 KASSERT(m->m_type == MT_SONAME,
 1110                     ("m->m_type == %d", m->m_type));
 1111                 orig_resid = 0;
 1112                 if (psa != NULL)
 1113                         *psa = sodupsockaddr(mtod(m, struct sockaddr *),
 1114                             M_NOWAIT);
 1115                 if (flags & MSG_PEEK) {
 1116                         m = m->m_next;
 1117                 } else {
 1118                         sbfree(&so->so_rcv, m);
 1119                         so->so_rcv.sb_mb = m_free(m);
 1120                         m = so->so_rcv.sb_mb;
 1121                         sockbuf_pushsync(&so->so_rcv, nextrecord);
 1122                 }
 1123         }
 1124 
 1125         /*
 1126          * Process one or more MT_CONTROL mbufs present before any data mbufs
 1127          * in the first mbuf chain on the socket buffer.  If MSG_PEEK, we
 1128          * just copy the data; if !MSG_PEEK, we call into the protocol to
 1129          * perform externalization (or freeing if controlp == NULL).
 1130          */
 1131         if (m != NULL && m->m_type == MT_CONTROL) {
 1132                 struct mbuf *cm = NULL, *cmn;
 1133                 struct mbuf **cme = &cm;
 1134 
 1135                 do {
 1136                         if (flags & MSG_PEEK) {
 1137                                 if (controlp != NULL) {
 1138                                         *controlp = m_copy(m, 0, m->m_len);
 1139                                         controlp = &(*controlp)->m_next;
 1140                                 }
 1141                                 m = m->m_next;
 1142                         } else {
 1143                                 sbfree(&so->so_rcv, m);
 1144                                 so->so_rcv.sb_mb = m->m_next;
 1145                                 m->m_next = NULL;
 1146                                 *cme = m;
 1147                                 cme = &(*cme)->m_next;
 1148                                 m = so->so_rcv.sb_mb;
 1149                         }
 1150                 } while (m != NULL && m->m_type == MT_CONTROL);
 1151                 if ((flags & MSG_PEEK) == 0)
 1152                         sockbuf_pushsync(&so->so_rcv, nextrecord);
 1153                 while (cm != NULL) {
 1154                         cmn = cm->m_next;
 1155                         cm->m_next = NULL;
 1156                         if (pr->pr_domain->dom_externalize != NULL) {
 1157                                 SOCKBUF_UNLOCK(&so->so_rcv);
 1158                                 error = (*pr->pr_domain->dom_externalize)
 1159                                     (cm, controlp);
 1160                                 SOCKBUF_LOCK(&so->so_rcv);
 1161                         } else if (controlp != NULL)
 1162                                 *controlp = cm;
 1163                         else
 1164                                 m_freem(cm);
 1165                         if (controlp != NULL) {
 1166                                 orig_resid = 0;
 1167                                 while (*controlp != NULL)
 1168                                         controlp = &(*controlp)->m_next;
 1169                         }
 1170                         cm = cmn;
 1171                 }
 1172                 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
 1173                 orig_resid = 0;
 1174         }
 1175         if (m != NULL) {
 1176                 if ((flags & MSG_PEEK) == 0) {
 1177                         KASSERT(m->m_nextpkt == nextrecord,
 1178                             ("soreceive: post-control, nextrecord !sync"));
 1179                         if (nextrecord == NULL) {
 1180                                 KASSERT(so->so_rcv.sb_mb == m,
 1181                                     ("soreceive: post-control, sb_mb!=m"));
 1182                                 KASSERT(so->so_rcv.sb_lastrecord == m,
 1183                                     ("soreceive: post-control, lastrecord!=m"));
 1184                         }
 1185                 }
 1186                 type = m->m_type;
 1187                 if (type == MT_OOBDATA)
 1188                         flags |= MSG_OOB;
 1189         } else {
 1190                 if ((flags & MSG_PEEK) == 0) {
 1191                         KASSERT(so->so_rcv.sb_mb == nextrecord,
 1192                             ("soreceive: sb_mb != nextrecord"));
 1193                         if (so->so_rcv.sb_mb == NULL) {
 1194                                 KASSERT(so->so_rcv.sb_lastrecord == NULL,
 1195                                     ("soreceive: sb_lastercord != NULL"));
 1196                         }
 1197                 }
 1198         }
 1199         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1200         SBLASTRECORDCHK(&so->so_rcv);
 1201         SBLASTMBUFCHK(&so->so_rcv);
 1202 
 1203         /*
 1204          * Now continue to read any data mbufs off of the head of the socket
 1205          * buffer until the read request is satisfied.  Note that 'type' is
 1206          * used to store the type of any mbuf reads that have happened so far
 1207          * such that soreceive() can stop reading if the type changes, which
 1208          * causes soreceive() to return only one of regular data and inline
 1209          * out-of-band data in a single socket receive operation.
 1210          */
 1211         moff = 0;
 1212         offset = 0;
 1213         while (m != NULL && uio->uio_resid > 0 && error == 0) {
 1214                 /*
 1215                  * If the type of mbuf has changed since the last mbuf
 1216                  * examined ('type'), end the receive operation.
 1217                  */
 1218                 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1219                 if (m->m_type == MT_OOBDATA) {
 1220                         if (type != MT_OOBDATA)
 1221                                 break;
 1222                 } else if (type == MT_OOBDATA)
 1223                         break;
 1224                 else
 1225                     KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
 1226                         ("m->m_type == %d", m->m_type));
 1227                 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
 1228                 len = uio->uio_resid;
 1229                 if (so->so_oobmark && len > so->so_oobmark - offset)
 1230                         len = so->so_oobmark - offset;
 1231                 if (len > m->m_len - moff)
 1232                         len = m->m_len - moff;
 1233                 /*
 1234                  * If mp is set, just pass back the mbufs.
 1235                  * Otherwise copy them out via the uio, then free.
 1236                  * Sockbuf must be consistent here (points to current mbuf,
 1237                  * it points to next record) when we drop priority;
 1238                  * we must note any additions to the sockbuf when we
 1239                  * block interrupts again.
 1240                  */
 1241                 if (mp == NULL) {
 1242                         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1243                         SBLASTRECORDCHK(&so->so_rcv);
 1244                         SBLASTMBUFCHK(&so->so_rcv);
 1245                         SOCKBUF_UNLOCK(&so->so_rcv);
 1246 #ifdef ZERO_COPY_SOCKETS
 1247                         if (so_zero_copy_receive) {
 1248                                 int disposable;
 1249 
 1250                                 if ((m->m_flags & M_EXT)
 1251                                  && (m->m_ext.ext_type == EXT_DISPOSABLE))
 1252                                         disposable = 1;
 1253                                 else
 1254                                         disposable = 0;
 1255 
 1256                                 error = uiomoveco(mtod(m, char *) + moff,
 1257                                                   (int)len, uio,
 1258                                                   disposable);
 1259                         } else
 1260 #endif /* ZERO_COPY_SOCKETS */
 1261                         error = uiomove(mtod(m, char *) + moff, (int)len, uio);
 1262                         SOCKBUF_LOCK(&so->so_rcv);
 1263                         if (error)
 1264                                 goto release;
 1265                 } else
 1266                         uio->uio_resid -= len;
 1267                 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1268                 if (len == m->m_len - moff) {
 1269                         if (m->m_flags & M_EOR)
 1270                                 flags |= MSG_EOR;
 1271                         if (flags & MSG_PEEK) {
 1272                                 m = m->m_next;
 1273                                 moff = 0;
 1274                         } else {
 1275                                 nextrecord = m->m_nextpkt;
 1276                                 sbfree(&so->so_rcv, m);
 1277                                 if (mp != NULL) {
 1278                                         *mp = m;
 1279                                         mp = &m->m_next;
 1280                                         so->so_rcv.sb_mb = m = m->m_next;
 1281                                         *mp = NULL;
 1282                                 } else {
 1283                                         so->so_rcv.sb_mb = m_free(m);
 1284                                         m = so->so_rcv.sb_mb;
 1285                                 }
 1286                                 if (m != NULL) {
 1287                                         m->m_nextpkt = nextrecord;
 1288                                         if (nextrecord == NULL)
 1289                                                 so->so_rcv.sb_lastrecord = m;
 1290                                 } else {
 1291                                         so->so_rcv.sb_mb = nextrecord;
 1292                                         SB_EMPTY_FIXUP(&so->so_rcv);
 1293                                 }
 1294                                 SBLASTRECORDCHK(&so->so_rcv);
 1295                                 SBLASTMBUFCHK(&so->so_rcv);
 1296                         }
 1297                 } else {
 1298                         if (flags & MSG_PEEK)
 1299                                 moff += len;
 1300                         else {
 1301                                 if (mp != NULL) {
 1302                                         int copy_flag;
 1303 
 1304                                         if (flags & MSG_DONTWAIT)
 1305                                                 copy_flag = M_DONTWAIT;
 1306                                         else
 1307                                                 copy_flag = M_TRYWAIT;
 1308                                         if (copy_flag == M_TRYWAIT)
 1309                                                 SOCKBUF_UNLOCK(&so->so_rcv);
 1310                                         *mp = m_copym(m, 0, len, copy_flag);
 1311                                         if (copy_flag == M_TRYWAIT)
 1312                                                 SOCKBUF_LOCK(&so->so_rcv);
 1313                                         if (*mp == NULL) {
 1314                                                 /*
 1315                                                  * m_copym() couldn't allocate an mbuf. 
 1316                                                  * Adjust uio_resid back (it was adjusted 
 1317                                                  * down by len bytes, which we didn't end 
 1318                                                  * up "copying" over).
 1319                                                  */
 1320                                                 uio->uio_resid += len;
 1321                                                 break;
 1322                                         }
 1323                                 }
 1324                                 m->m_data += len;
 1325                                 m->m_len -= len;
 1326                                 so->so_rcv.sb_cc -= len;
 1327                         }
 1328                 }
 1329                 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1330                 if (so->so_oobmark) {
 1331                         if ((flags & MSG_PEEK) == 0) {
 1332                                 so->so_oobmark -= len;
 1333                                 if (so->so_oobmark == 0) {
 1334                                         so->so_rcv.sb_state |= SBS_RCVATMARK;
 1335                                         break;
 1336                                 }
 1337                         } else {
 1338                                 offset += len;
 1339                                 if (offset == so->so_oobmark)
 1340                                         break;
 1341                         }
 1342                 }
 1343                 if (flags & MSG_EOR)
 1344                         break;
 1345                 /*
 1346                  * If the MSG_WAITALL flag is set (for non-atomic socket),
 1347                  * we must not quit until "uio->uio_resid == 0" or an error
 1348                  * termination.  If a signal/timeout occurs, return
 1349                  * with a short count but without error.
 1350                  * Keep sockbuf locked against other readers.
 1351                  */
 1352                 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
 1353                     !sosendallatonce(so) && nextrecord == NULL) {
 1354                         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1355                         if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
 1356                                 break;
 1357                         /*
 1358                          * Notify the protocol that some data has been
 1359                          * drained before blocking.
 1360                          */
 1361                         if (pr->pr_flags & PR_WANTRCVD && so->so_pcb != NULL) {
 1362                                 SOCKBUF_UNLOCK(&so->so_rcv);
 1363                                 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
 1364                                 SOCKBUF_LOCK(&so->so_rcv);
 1365                         }
 1366                         SBLASTRECORDCHK(&so->so_rcv);
 1367                         SBLASTMBUFCHK(&so->so_rcv);
 1368                         error = sbwait(&so->so_rcv);
 1369                         if (error)
 1370                                 goto release;
 1371                         m = so->so_rcv.sb_mb;
 1372                         if (m != NULL)
 1373                                 nextrecord = m->m_nextpkt;
 1374                 }
 1375         }
 1376 
 1377         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1378         if (m != NULL && pr->pr_flags & PR_ATOMIC) {
 1379                 flags |= MSG_TRUNC;
 1380                 if ((flags & MSG_PEEK) == 0)
 1381                         (void) sbdroprecord_locked(&so->so_rcv);
 1382         }
 1383         if ((flags & MSG_PEEK) == 0) {
 1384                 if (m == NULL) {
 1385                         /*
 1386                          * First part is an inline SB_EMPTY_FIXUP().  Second
 1387                          * part makes sure sb_lastrecord is up-to-date if
 1388                          * there is still data in the socket buffer.
 1389                          */
 1390                         so->so_rcv.sb_mb = nextrecord;
 1391                         if (so->so_rcv.sb_mb == NULL) {
 1392                                 so->so_rcv.sb_mbtail = NULL;
 1393                                 so->so_rcv.sb_lastrecord = NULL;
 1394                         } else if (nextrecord->m_nextpkt == NULL)
 1395                                 so->so_rcv.sb_lastrecord = nextrecord;
 1396                 }
 1397                 SBLASTRECORDCHK(&so->so_rcv);
 1398                 SBLASTMBUFCHK(&so->so_rcv);
 1399                 /*
 1400                  * If soreceive() is being done from the socket callback, then 
 1401                  * don't need to generate ACK to peer to update window, since 
 1402                  * ACK will be generated on return to TCP.
 1403                  */
 1404                 if (!(flags & MSG_SOCALLBCK) && 
 1405                     (pr->pr_flags & PR_WANTRCVD) && so->so_pcb) {
 1406                         SOCKBUF_UNLOCK(&so->so_rcv);
 1407                         (*pr->pr_usrreqs->pru_rcvd)(so, flags);
 1408                         SOCKBUF_LOCK(&so->so_rcv);
 1409                 }
 1410         }
 1411         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1412         if (orig_resid == uio->uio_resid && orig_resid &&
 1413             (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
 1414                 sbunlock(&so->so_rcv);
 1415                 goto restart;
 1416         }
 1417 
 1418         if (flagsp != NULL)
 1419                 *flagsp |= flags;
 1420 release:
 1421         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1422         sbunlock(&so->so_rcv);
 1423 out:
 1424         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 1425         SOCKBUF_UNLOCK(&so->so_rcv);
 1426         return (error);
 1427 }
 1428 
 1429 int
 1430 soshutdown(so, how)
 1431         struct socket *so;
 1432         int how;
 1433 {
 1434         struct protosw *pr = so->so_proto;
 1435 
 1436         if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
 1437                 return (EINVAL);
 1438 
 1439         if (how != SHUT_WR)
 1440                 sorflush(so);
 1441         if (how != SHUT_RD)
 1442                 return ((*pr->pr_usrreqs->pru_shutdown)(so));
 1443         return (0);
 1444 }
 1445 
 1446 void
 1447 sorflush(so)
 1448         struct socket *so;
 1449 {
 1450         struct sockbuf *sb = &so->so_rcv;
 1451         struct protosw *pr = so->so_proto;
 1452         struct sockbuf asb;
 1453 
 1454         /*
 1455          * XXXRW: This is quite ugly.  Previously, this code made a copy of
 1456          * the socket buffer, then zero'd the original to clear the buffer
 1457          * fields.  However, with mutexes in the socket buffer, this causes
 1458          * problems.  We only clear the zeroable bits of the original;
 1459          * however, we have to initialize and destroy the mutex in the copy
 1460          * so that dom_dispose() and sbrelease() can lock t as needed.
 1461          */
 1462         SOCKBUF_LOCK(sb);
 1463         sb->sb_flags |= SB_NOINTR;
 1464         (void) sblock(sb, M_WAITOK);
 1465         /*
 1466          * socantrcvmore_locked() drops the socket buffer mutex so that it
 1467          * can safely perform wakeups.  Re-acquire the mutex before
 1468          * continuing.
 1469          */
 1470         socantrcvmore_locked(so);
 1471         SOCKBUF_LOCK(sb);
 1472         sbunlock(sb);
 1473         /*
 1474          * Invalidate/clear most of the sockbuf structure, but leave
 1475          * selinfo and mutex data unchanged.
 1476          */
 1477         bzero(&asb, offsetof(struct sockbuf, sb_startzero));
 1478         bcopy(&sb->sb_startzero, &asb.sb_startzero,
 1479             sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
 1480         bzero(&sb->sb_startzero,
 1481             sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
 1482         SOCKBUF_UNLOCK(sb);
 1483 
 1484         SOCKBUF_LOCK_INIT(&asb, "so_rcv");
 1485         if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
 1486                 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
 1487         sbrelease(&asb, so);
 1488         SOCKBUF_LOCK_DESTROY(&asb);
 1489 }
 1490 
 1491 /*
 1492  * Perhaps this routine, and sooptcopyout(), below, ought to come in
 1493  * an additional variant to handle the case where the option value needs
 1494  * to be some kind of integer, but not a specific size.
 1495  * In addition to their use here, these functions are also called by the
 1496  * protocol-level pr_ctloutput() routines.
 1497  */
 1498 int
 1499 sooptcopyin(sopt, buf, len, minlen)
 1500         struct  sockopt *sopt;
 1501         void    *buf;
 1502         size_t  len;
 1503         size_t  minlen;
 1504 {
 1505         size_t  valsize;
 1506 
 1507         /*
 1508          * If the user gives us more than we wanted, we ignore it,
 1509          * but if we don't get the minimum length the caller
 1510          * wants, we return EINVAL.  On success, sopt->sopt_valsize
 1511          * is set to however much we actually retrieved.
 1512          */
 1513         if ((valsize = sopt->sopt_valsize) < minlen)
 1514                 return EINVAL;
 1515         if (valsize > len)
 1516                 sopt->sopt_valsize = valsize = len;
 1517 
 1518         if (sopt->sopt_td != NULL)
 1519                 return (copyin(sopt->sopt_val, buf, valsize));
 1520 
 1521         bcopy(sopt->sopt_val, buf, valsize);
 1522         return 0;
 1523 }
 1524 
 1525 /*
 1526  * Kernel version of setsockopt(2)/
 1527  * XXX: optlen is size_t, not socklen_t
 1528  */
 1529 int
 1530 so_setsockopt(struct socket *so, int level, int optname, void *optval,
 1531     size_t optlen)
 1532 {
 1533         struct sockopt sopt;
 1534 
 1535         sopt.sopt_level = level;
 1536         sopt.sopt_name = optname;
 1537         sopt.sopt_dir = SOPT_SET;
 1538         sopt.sopt_val = optval;
 1539         sopt.sopt_valsize = optlen;
 1540         sopt.sopt_td = NULL;
 1541         return (sosetopt(so, &sopt));
 1542 }
 1543 
 1544 int
 1545 sosetopt(so, sopt)
 1546         struct socket *so;
 1547         struct sockopt *sopt;
 1548 {
 1549         int     error, optval;
 1550         struct  linger l;
 1551         struct  timeval tv;
 1552         u_long  val;
 1553 #ifdef MAC
 1554         struct mac extmac;
 1555 #endif
 1556 
 1557         error = 0;
 1558         if (sopt->sopt_level != SOL_SOCKET) {
 1559                 if (so->so_proto && so->so_proto->pr_ctloutput)
 1560                         return ((*so->so_proto->pr_ctloutput)
 1561                                   (so, sopt));
 1562                 error = ENOPROTOOPT;
 1563         } else {
 1564                 switch (sopt->sopt_name) {
 1565 #ifdef INET
 1566                 case SO_ACCEPTFILTER:
 1567                         error = do_setopt_accept_filter(so, sopt);
 1568                         if (error)
 1569                                 goto bad;
 1570                         break;
 1571 #endif
 1572                 case SO_LINGER:
 1573                         error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
 1574                         if (error)
 1575                                 goto bad;
 1576 
 1577                         SOCK_LOCK(so);
 1578                         so->so_linger = l.l_linger;
 1579                         if (l.l_onoff)
 1580                                 so->so_options |= SO_LINGER;
 1581                         else
 1582                                 so->so_options &= ~SO_LINGER;
 1583                         SOCK_UNLOCK(so);
 1584                         break;
 1585 
 1586                 case SO_DEBUG:
 1587                 case SO_KEEPALIVE:
 1588                 case SO_DONTROUTE:
 1589                 case SO_USELOOPBACK:
 1590                 case SO_BROADCAST:
 1591                 case SO_REUSEADDR:
 1592                 case SO_REUSEPORT:
 1593                 case SO_OOBINLINE:
 1594                 case SO_TIMESTAMP:
 1595                 case SO_BINTIME:
 1596                 case SO_NOSIGPIPE:
 1597                         error = sooptcopyin(sopt, &optval, sizeof optval,
 1598                                             sizeof optval);
 1599                         if (error)
 1600                                 goto bad;
 1601                         SOCK_LOCK(so);
 1602                         if (optval)
 1603                                 so->so_options |= sopt->sopt_name;
 1604                         else
 1605                                 so->so_options &= ~sopt->sopt_name;
 1606                         SOCK_UNLOCK(so);
 1607                         break;
 1608 
 1609                 case SO_SNDBUF:
 1610                 case SO_RCVBUF:
 1611                 case SO_SNDLOWAT:
 1612                 case SO_RCVLOWAT:
 1613                         error = sooptcopyin(sopt, &optval, sizeof optval,
 1614                                             sizeof optval);
 1615                         if (error)
 1616                                 goto bad;
 1617 
 1618                         /*
 1619                          * Values < 1 make no sense for any of these
 1620                          * options, so disallow them.
 1621                          */
 1622                         if (optval < 1) {
 1623                                 error = EINVAL;
 1624                                 goto bad;
 1625                         }
 1626 
 1627                         switch (sopt->sopt_name) {
 1628                         case SO_SNDBUF:
 1629                         case SO_RCVBUF:
 1630                                 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
 1631                                     &so->so_snd : &so->so_rcv, (u_long)optval,
 1632                                     so, curthread) == 0) {
 1633                                         error = ENOBUFS;
 1634                                         goto bad;
 1635                                 }
 1636                                 break;
 1637 
 1638                         /*
 1639                          * Make sure the low-water is never greater than
 1640                          * the high-water.
 1641                          */
 1642                         case SO_SNDLOWAT:
 1643                                 SOCKBUF_LOCK(&so->so_snd);
 1644                                 so->so_snd.sb_lowat =
 1645                                     (optval > so->so_snd.sb_hiwat) ?
 1646                                     so->so_snd.sb_hiwat : optval;
 1647                                 SOCKBUF_UNLOCK(&so->so_snd);
 1648                                 break;
 1649                         case SO_RCVLOWAT:
 1650                                 SOCKBUF_LOCK(&so->so_rcv);
 1651                                 so->so_rcv.sb_lowat =
 1652                                     (optval > so->so_rcv.sb_hiwat) ?
 1653                                     so->so_rcv.sb_hiwat : optval;
 1654                                 SOCKBUF_UNLOCK(&so->so_rcv);
 1655                                 break;
 1656                         }
 1657                         break;
 1658 
 1659                 case SO_SNDTIMEO:
 1660                 case SO_RCVTIMEO:
 1661 #ifdef COMPAT_IA32
 1662                         if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) {
 1663                                 struct timeval32 tv32;
 1664 
 1665                                 error = sooptcopyin(sopt, &tv32, sizeof tv32,
 1666                                     sizeof tv32);
 1667                                 CP(tv32, tv, tv_sec);
 1668                                 CP(tv32, tv, tv_usec);
 1669                         } else
 1670 #endif
 1671                                 error = sooptcopyin(sopt, &tv, sizeof tv,
 1672                                     sizeof tv);
 1673                         if (error)
 1674                                 goto bad;
 1675 
 1676                         /* assert(hz > 0); */
 1677                         if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
 1678                             tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
 1679                                 error = EDOM;
 1680                                 goto bad;
 1681                         }
 1682                         /* assert(tick > 0); */
 1683                         /* assert(ULONG_MAX - INT_MAX >= 1000000); */
 1684                         val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
 1685                         if (val > INT_MAX) {
 1686                                 error = EDOM;
 1687                                 goto bad;
 1688                         }
 1689                         if (val == 0 && tv.tv_usec != 0)
 1690                                 val = 1;
 1691 
 1692                         switch (sopt->sopt_name) {
 1693                         case SO_SNDTIMEO:
 1694                                 so->so_snd.sb_timeo = val;
 1695                                 break;
 1696                         case SO_RCVTIMEO:
 1697                                 so->so_rcv.sb_timeo = val;
 1698                                 break;
 1699                         }
 1700                         break;
 1701 
 1702                 case SO_LABEL:
 1703 #ifdef MAC
 1704                         error = sooptcopyin(sopt, &extmac, sizeof extmac,
 1705                             sizeof extmac);
 1706                         if (error)
 1707                                 goto bad;
 1708                         error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
 1709                             so, &extmac);
 1710 #else
 1711                         error = EOPNOTSUPP;
 1712 #endif
 1713                         break;
 1714 
 1715                 default:
 1716                         error = ENOPROTOOPT;
 1717                         break;
 1718                 }
 1719                 if (error == 0 && so->so_proto != NULL &&
 1720                     so->so_proto->pr_ctloutput != NULL) {
 1721                         (void) ((*so->so_proto->pr_ctloutput)
 1722                                   (so, sopt));
 1723                 }
 1724         }
 1725 bad:
 1726         return (error);
 1727 }
 1728 
 1729 /* Helper routine for getsockopt */
 1730 int
 1731 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
 1732 {
 1733         int     error;
 1734         size_t  valsize;
 1735 
 1736         error = 0;
 1737 
 1738         /*
 1739          * Documented get behavior is that we always return a value,
 1740          * possibly truncated to fit in the user's buffer.
 1741          * Traditional behavior is that we always tell the user
 1742          * precisely how much we copied, rather than something useful
 1743          * like the total amount we had available for her.
 1744          * Note that this interface is not idempotent; the entire answer must
 1745          * generated ahead of time.
 1746          */
 1747         valsize = min(len, sopt->sopt_valsize);
 1748         sopt->sopt_valsize = valsize;
 1749         if (sopt->sopt_val != NULL) {
 1750                 if (sopt->sopt_td != NULL)
 1751                         error = copyout(buf, sopt->sopt_val, valsize);
 1752                 else
 1753                         bcopy(buf, sopt->sopt_val, valsize);
 1754         }
 1755         return error;
 1756 }
 1757 
 1758 int
 1759 sogetopt(so, sopt)
 1760         struct socket *so;
 1761         struct sockopt *sopt;
 1762 {
 1763         int     error, optval;
 1764         struct  linger l;
 1765         struct  timeval tv;
 1766 #ifdef MAC
 1767         struct mac extmac;
 1768 #endif
 1769 
 1770         error = 0;
 1771         if (sopt->sopt_level != SOL_SOCKET) {
 1772                 if (so->so_proto && so->so_proto->pr_ctloutput) {
 1773                         return ((*so->so_proto->pr_ctloutput)
 1774                                   (so, sopt));
 1775                 } else
 1776                         return (ENOPROTOOPT);
 1777         } else {
 1778                 switch (sopt->sopt_name) {
 1779 #ifdef INET
 1780                 case SO_ACCEPTFILTER:
 1781                         error = do_getopt_accept_filter(so, sopt);
 1782                         break;
 1783 #endif
 1784                 case SO_LINGER:
 1785                         SOCK_LOCK(so);
 1786                         l.l_onoff = so->so_options & SO_LINGER;
 1787                         l.l_linger = so->so_linger;
 1788                         SOCK_UNLOCK(so);
 1789                         error = sooptcopyout(sopt, &l, sizeof l);
 1790                         break;
 1791 
 1792                 case SO_USELOOPBACK:
 1793                 case SO_DONTROUTE:
 1794                 case SO_DEBUG:
 1795                 case SO_KEEPALIVE:
 1796                 case SO_REUSEADDR:
 1797                 case SO_REUSEPORT:
 1798                 case SO_BROADCAST:
 1799                 case SO_OOBINLINE:
 1800                 case SO_ACCEPTCONN:
 1801                 case SO_TIMESTAMP:
 1802                 case SO_BINTIME:
 1803                 case SO_NOSIGPIPE:
 1804                         optval = so->so_options & sopt->sopt_name;
 1805 integer:
 1806                         error = sooptcopyout(sopt, &optval, sizeof optval);
 1807                         break;
 1808 
 1809                 case SO_TYPE:
 1810                         optval = so->so_type;
 1811                         goto integer;
 1812 
 1813                 case SO_ERROR:
 1814                         optval = so->so_error;
 1815                         so->so_error = 0;
 1816                         goto integer;
 1817 
 1818                 case SO_SNDBUF:
 1819                         optval = so->so_snd.sb_hiwat;
 1820                         goto integer;
 1821 
 1822                 case SO_RCVBUF:
 1823                         optval = so->so_rcv.sb_hiwat;
 1824                         goto integer;
 1825 
 1826                 case SO_SNDLOWAT:
 1827                         optval = so->so_snd.sb_lowat;
 1828                         goto integer;
 1829 
 1830                 case SO_RCVLOWAT:
 1831                         optval = so->so_rcv.sb_lowat;
 1832                         goto integer;
 1833 
 1834                 case SO_SNDTIMEO:
 1835                 case SO_RCVTIMEO:
 1836                         optval = (sopt->sopt_name == SO_SNDTIMEO ?
 1837                                   so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
 1838 
 1839                         tv.tv_sec = optval / hz;
 1840                         tv.tv_usec = (optval % hz) * tick;
 1841 #ifdef COMPAT_IA32
 1842                         if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) {
 1843                                 struct timeval32 tv32;
 1844 
 1845                                 CP(tv, tv32, tv_sec);
 1846                                 CP(tv, tv32, tv_usec);
 1847                                 error = sooptcopyout(sopt, &tv32, sizeof tv32);
 1848                         } else
 1849 #endif
 1850                                 error = sooptcopyout(sopt, &tv, sizeof tv);
 1851                         break;
 1852 
 1853                 case SO_LABEL:
 1854 #ifdef MAC
 1855                         error = sooptcopyin(sopt, &extmac, sizeof(extmac),
 1856                             sizeof(extmac));
 1857                         if (error)
 1858                                 return (error);
 1859                         error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
 1860                             so, &extmac);
 1861                         if (error)
 1862                                 return (error);
 1863                         error = sooptcopyout(sopt, &extmac, sizeof extmac);
 1864 #else
 1865                         error = EOPNOTSUPP;
 1866 #endif
 1867                         break;
 1868 
 1869                 case SO_PEERLABEL:
 1870 #ifdef MAC
 1871                         error = sooptcopyin(sopt, &extmac, sizeof(extmac),
 1872                             sizeof(extmac));
 1873                         if (error)
 1874                                 return (error);
 1875                         error = mac_getsockopt_peerlabel(
 1876                             sopt->sopt_td->td_ucred, so, &extmac);
 1877                         if (error)
 1878                                 return (error);
 1879                         error = sooptcopyout(sopt, &extmac, sizeof extmac);
 1880 #else
 1881                         error = EOPNOTSUPP;
 1882 #endif
 1883                         break;
 1884 
 1885                 case SO_LISTENQLIMIT:
 1886                         optval = so->so_qlimit;
 1887                         goto integer;
 1888 
 1889                 case SO_LISTENQLEN:
 1890                         optval = so->so_qlen;
 1891                         goto integer;
 1892 
 1893                 case SO_LISTENINCQLEN:
 1894                         optval = so->so_incqlen;
 1895                         goto integer;
 1896 
 1897                 default:
 1898                         error = ENOPROTOOPT;
 1899                         break;
 1900                 }
 1901                 return (error);
 1902         }
 1903 }
 1904 
 1905 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
 1906 int
 1907 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
 1908 {
 1909         struct mbuf *m, *m_prev;
 1910         int sopt_size = sopt->sopt_valsize;
 1911 
 1912         MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
 1913         if (m == NULL)
 1914                 return ENOBUFS;
 1915         if (sopt_size > MLEN) {
 1916                 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT);
 1917                 if ((m->m_flags & M_EXT) == 0) {
 1918                         m_free(m);
 1919                         return ENOBUFS;
 1920                 }
 1921                 m->m_len = min(MCLBYTES, sopt_size);
 1922         } else {
 1923                 m->m_len = min(MLEN, sopt_size);
 1924         }
 1925         sopt_size -= m->m_len;
 1926         *mp = m;
 1927         m_prev = m;
 1928 
 1929         while (sopt_size) {
 1930                 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
 1931                 if (m == NULL) {
 1932                         m_freem(*mp);
 1933                         return ENOBUFS;
 1934                 }
 1935                 if (sopt_size > MLEN) {
 1936                         MCLGET(m, sopt->sopt_td != NULL ? M_TRYWAIT :
 1937                             M_DONTWAIT);
 1938                         if ((m->m_flags & M_EXT) == 0) {
 1939                                 m_freem(m);
 1940                                 m_freem(*mp);
 1941                                 return ENOBUFS;
 1942                         }
 1943                         m->m_len = min(MCLBYTES, sopt_size);
 1944                 } else {
 1945                         m->m_len = min(MLEN, sopt_size);
 1946                 }
 1947                 sopt_size -= m->m_len;
 1948                 m_prev->m_next = m;
 1949                 m_prev = m;
 1950         }
 1951         return 0;
 1952 }
 1953 
 1954 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
 1955 int
 1956 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
 1957 {
 1958         struct mbuf *m0 = m;
 1959 
 1960         if (sopt->sopt_val == NULL)
 1961                 return 0;
 1962         while (m != NULL && sopt->sopt_valsize >= m->m_len) {
 1963                 if (sopt->sopt_td != NULL) {
 1964                         int error;
 1965 
 1966                         error = copyin(sopt->sopt_val, mtod(m, char *),
 1967                                        m->m_len);
 1968                         if (error != 0) {
 1969                                 m_freem(m0);
 1970                                 return(error);
 1971                         }
 1972                 } else
 1973                         bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
 1974                 sopt->sopt_valsize -= m->m_len;
 1975                 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
 1976                 m = m->m_next;
 1977         }
 1978         if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
 1979                 panic("ip6_sooptmcopyin");
 1980         return 0;
 1981 }
 1982 
 1983 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
 1984 int
 1985 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
 1986 {
 1987         struct mbuf *m0 = m;
 1988         size_t valsize = 0;
 1989 
 1990         if (sopt->sopt_val == NULL)
 1991                 return 0;
 1992         while (m != NULL && sopt->sopt_valsize >= m->m_len) {
 1993                 if (sopt->sopt_td != NULL) {
 1994                         int error;
 1995 
 1996                         error = copyout(mtod(m, char *), sopt->sopt_val,
 1997                                        m->m_len);
 1998                         if (error != 0) {
 1999                                 m_freem(m0);
 2000                                 return(error);
 2001                         }
 2002                 } else
 2003                         bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
 2004                sopt->sopt_valsize -= m->m_len;
 2005                sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
 2006                valsize += m->m_len;
 2007                m = m->m_next;
 2008         }
 2009         if (m != NULL) {
 2010                 /* enough soopt buffer should be given from user-land */
 2011                 m_freem(m0);
 2012                 return(EINVAL);
 2013         }
 2014         sopt->sopt_valsize = valsize;
 2015         return 0;
 2016 }
 2017 
 2018 void
 2019 sohasoutofband(so)
 2020         struct socket *so;
 2021 {
 2022         if (so->so_sigio != NULL)
 2023                 pgsigio(&so->so_sigio, SIGURG, 0);
 2024         selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
 2025 }
 2026 
 2027 int
 2028 sopoll(struct socket *so, int events, struct ucred *active_cred,
 2029     struct thread *td)
 2030 {
 2031         int revents = 0;
 2032 
 2033         SOCKBUF_LOCK(&so->so_snd);
 2034         SOCKBUF_LOCK(&so->so_rcv);
 2035         if (events & (POLLIN | POLLRDNORM))
 2036                 if (soreadable(so))
 2037                         revents |= events & (POLLIN | POLLRDNORM);
 2038 
 2039         if (events & POLLINIGNEOF)
 2040                 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
 2041                     !TAILQ_EMPTY(&so->so_comp) || so->so_error)
 2042                         revents |= POLLINIGNEOF;
 2043 
 2044         if (events & (POLLOUT | POLLWRNORM))
 2045                 if (sowriteable(so))
 2046                         revents |= events & (POLLOUT | POLLWRNORM);
 2047 
 2048         if (events & (POLLPRI | POLLRDBAND))
 2049                 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
 2050                         revents |= events & (POLLPRI | POLLRDBAND);
 2051 
 2052         if (revents == 0) {
 2053                 if (events &
 2054                     (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
 2055                      POLLRDBAND)) {
 2056                         selrecord(td, &so->so_rcv.sb_sel);
 2057                         so->so_rcv.sb_flags |= SB_SEL;
 2058                 }
 2059 
 2060                 if (events & (POLLOUT | POLLWRNORM)) {
 2061                         selrecord(td, &so->so_snd.sb_sel);
 2062                         so->so_snd.sb_flags |= SB_SEL;
 2063                 }
 2064         }
 2065 
 2066         SOCKBUF_UNLOCK(&so->so_rcv);
 2067         SOCKBUF_UNLOCK(&so->so_snd);
 2068         return (revents);
 2069 }
 2070 
 2071 int
 2072 soo_kqfilter(struct file *fp, struct knote *kn)
 2073 {
 2074         struct socket *so = kn->kn_fp->f_data;
 2075         struct sockbuf *sb;
 2076 
 2077         switch (kn->kn_filter) {
 2078         case EVFILT_READ:
 2079                 if (so->so_options & SO_ACCEPTCONN)
 2080                         kn->kn_fop = &solisten_filtops;
 2081                 else
 2082                         kn->kn_fop = &soread_filtops;
 2083                 sb = &so->so_rcv;
 2084                 break;
 2085         case EVFILT_WRITE:
 2086                 kn->kn_fop = &sowrite_filtops;
 2087                 sb = &so->so_snd;
 2088                 break;
 2089         default:
 2090                 return (EINVAL);
 2091         }
 2092 
 2093         SOCKBUF_LOCK(sb);
 2094         knlist_add(&sb->sb_sel.si_note, kn, 1);
 2095         sb->sb_flags |= SB_KNOTE;
 2096         SOCKBUF_UNLOCK(sb);
 2097         return (0);
 2098 }
 2099 
 2100 static void
 2101 filt_sordetach(struct knote *kn)
 2102 {
 2103         struct socket *so = kn->kn_fp->f_data;
 2104 
 2105         SOCKBUF_LOCK(&so->so_rcv);
 2106         knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
 2107         if (knlist_empty(&so->so_rcv.sb_sel.si_note))
 2108                 so->so_rcv.sb_flags &= ~SB_KNOTE;
 2109         SOCKBUF_UNLOCK(&so->so_rcv);
 2110 }
 2111 
 2112 /*ARGSUSED*/
 2113 static int
 2114 filt_soread(struct knote *kn, long hint)
 2115 {
 2116         struct socket *so;
 2117 
 2118         so = kn->kn_fp->f_data;
 2119         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
 2120 
 2121         kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
 2122         if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
 2123                 kn->kn_flags |= EV_EOF;
 2124                 kn->kn_fflags = so->so_error;
 2125                 return (1);
 2126         } else if (so->so_error)        /* temporary udp error */
 2127                 return (1);
 2128         else if (kn->kn_sfflags & NOTE_LOWAT)
 2129                 return (kn->kn_data >= kn->kn_sdata);
 2130         else
 2131                 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
 2132 }
 2133 
 2134 static void
 2135 filt_sowdetach(struct knote *kn)
 2136 {
 2137         struct socket *so = kn->kn_fp->f_data;
 2138 
 2139         SOCKBUF_LOCK(&so->so_snd);
 2140         knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
 2141         if (knlist_empty(&so->so_snd.sb_sel.si_note))
 2142                 so->so_snd.sb_flags &= ~SB_KNOTE;
 2143         SOCKBUF_UNLOCK(&so->so_snd);
 2144 }
 2145 
 2146 /*ARGSUSED*/
 2147 static int
 2148 filt_sowrite(struct knote *kn, long hint)
 2149 {
 2150         struct socket *so;
 2151 
 2152         so = kn->kn_fp->f_data;
 2153         SOCKBUF_LOCK_ASSERT(&so->so_snd);
 2154         kn->kn_data = sbspace(&so->so_snd);
 2155         if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
 2156                 kn->kn_flags |= EV_EOF;
 2157                 kn->kn_fflags = so->so_error;
 2158                 return (1);
 2159         } else if (so->so_error)        /* temporary udp error */
 2160                 return (1);
 2161         else if (((so->so_state & SS_ISCONNECTED) == 0) &&
 2162             (so->so_proto->pr_flags & PR_CONNREQUIRED))
 2163                 return (0);
 2164         else if (kn->kn_sfflags & NOTE_LOWAT)
 2165                 return (kn->kn_data >= kn->kn_sdata);
 2166         else
 2167                 return (kn->kn_data >= so->so_snd.sb_lowat);
 2168 }
 2169 
 2170 /*ARGSUSED*/
 2171 static int
 2172 filt_solisten(struct knote *kn, long hint)
 2173 {
 2174         struct socket *so = kn->kn_fp->f_data;
 2175 
 2176         kn->kn_data = so->so_qlen;
 2177         return (! TAILQ_EMPTY(&so->so_comp));
 2178 }
 2179 
 2180 int
 2181 socheckuid(struct socket *so, uid_t uid)
 2182 {
 2183 
 2184         if (so == NULL)
 2185                 return (EPERM);
 2186         if (so->so_cred->cr_uid != uid)
 2187                 return (EPERM);
 2188         return (0);
 2189 }
 2190 
 2191 static int
 2192 somaxconn_sysctl(SYSCTL_HANDLER_ARGS)
 2193 {
 2194         int error;
 2195         int val;
 2196 
 2197         val = somaxconn;
 2198         error = sysctl_handle_int(oidp, &val, sizeof(int), req);
 2199         if (error || !req->newptr )
 2200                 return (error);
 2201 
 2202         if (val < 1 || val > USHRT_MAX)
 2203                 return (EINVAL);
 2204 
 2205         somaxconn = val;
 2206         return (0);
 2207 }

Cache object: ebda32d1480b4faf7859778cdcd90158


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