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/bsd/kern/uipc_socket2.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) 2000 Apple Computer, Inc. All rights reserved.
    3  *
    4  * @APPLE_LICENSE_HEADER_START@
    5  * 
    6  * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
    7  * 
    8  * This file contains Original Code and/or Modifications of Original Code
    9  * as defined in and that are subject to the Apple Public Source License
   10  * Version 2.0 (the 'License'). You may not use this file except in
   11  * compliance with the License. Please obtain a copy of the License at
   12  * http://www.opensource.apple.com/apsl/ and read it before using this
   13  * file.
   14  * 
   15  * The Original Code and all software distributed under the License are
   16  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
   17  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
   18  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
   19  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
   20  * Please see the License for the specific language governing rights and
   21  * limitations under the License.
   22  * 
   23  * @APPLE_LICENSE_HEADER_END@
   24  */
   25 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
   26 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
   27 /*
   28  * Copyright (c) 1982, 1986, 1988, 1990, 1993
   29  *      The Regents of the University of California.  All rights reserved.
   30  *
   31  * Redistribution and use in source and binary forms, with or without
   32  * modification, are permitted provided that the following conditions
   33  * are met:
   34  * 1. Redistributions of source code must retain the above copyright
   35  *    notice, this list of conditions and the following disclaimer.
   36  * 2. Redistributions in binary form must reproduce the above copyright
   37  *    notice, this list of conditions and the following disclaimer in the
   38  *    documentation and/or other materials provided with the distribution.
   39  * 3. All advertising materials mentioning features or use of this software
   40  *    must display the following acknowledgement:
   41  *      This product includes software developed by the University of
   42  *      California, Berkeley and its contributors.
   43  * 4. Neither the name of the University nor the names of its contributors
   44  *    may be used to endorse or promote products derived from this software
   45  *    without specific prior written permission.
   46  *
   47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   57  * SUCH DAMAGE.
   58  *
   59  *      @(#)uipc_socket2.c      8.1 (Berkeley) 6/10/93
   60  * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.9 2001/07/26 18:53:02 peter Exp $
   61  */
   62 
   63 #include <sys/param.h>
   64 #include <sys/systm.h>
   65 #include <sys/domain.h>
   66 #include <sys/kernel.h>
   67 #include <sys/proc.h>
   68 #include <sys/malloc.h>
   69 #include <sys/mbuf.h>
   70 #include <sys/protosw.h>
   71 #include <sys/stat.h>
   72 #include <sys/socket.h>
   73 #include <sys/socketvar.h>
   74 #include <sys/signalvar.h>
   75 #include <sys/sysctl.h>
   76 #include <sys/ev.h>
   77 
   78 #include <sys/kdebug.h>
   79 
   80 #define DBG_FNC_SBDROP  NETDBG_CODE(DBG_NETSOCK, 4)
   81 #define DBG_FNC_SBAPPEND        NETDBG_CODE(DBG_NETSOCK, 5)
   82 
   83 
   84 /*
   85  * Primitive routines for operating on sockets and socket buffers
   86  */
   87 
   88 u_long  sb_max = SB_MAX;                /* XXX should be static */
   89 
   90 static  u_long sb_efficiency = 8;       /* parameter for sbreserve() */
   91 
   92 /*
   93  * Procedures to manipulate state flags of socket
   94  * and do appropriate wakeups.  Normal sequence from the
   95  * active (originating) side is that soisconnecting() is
   96  * called during processing of connect() call,
   97  * resulting in an eventual call to soisconnected() if/when the
   98  * connection is established.  When the connection is torn down
   99  * soisdisconnecting() is called during processing of disconnect() call,
  100  * and soisdisconnected() is called when the connection to the peer
  101  * is totally severed.  The semantics of these routines are such that
  102  * connectionless protocols can call soisconnected() and soisdisconnected()
  103  * only, bypassing the in-progress calls when setting up a ``connection''
  104  * takes no time.
  105  *
  106  * From the passive side, a socket is created with
  107  * two queues of sockets: so_incomp for connections in progress
  108  * and so_comp for connections already made and awaiting user acceptance.
  109  * As a protocol is preparing incoming connections, it creates a socket
  110  * structure queued on so_incomp by calling sonewconn().  When the connection
  111  * is established, soisconnected() is called, and transfers the
  112  * socket structure to so_comp, making it available to accept().
  113  *
  114  * If a socket is closed with sockets on either
  115  * so_incomp or so_comp, these sockets are dropped.
  116  *
  117  * If higher level protocols are implemented in
  118  * the kernel, the wakeups done here will sometimes
  119  * cause software-interrupt process scheduling.
  120  */
  121 
  122 void
  123 soisconnecting(so)
  124         register struct socket *so;
  125 {
  126 
  127         so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
  128         so->so_state |= SS_ISCONNECTING;
  129 }
  130 
  131 void
  132 soisconnected(so)
  133         struct socket *so;
  134 {
  135         struct socket *head = so->so_head;
  136         struct kextcb *kp;
  137 
  138         kp = sotokextcb(so);
  139         while (kp) {
  140                 if (kp->e_soif && kp->e_soif->sf_soisconnected) {
  141                         if ((*kp->e_soif->sf_soisconnected)(so, kp))
  142                                 return;
  143                 }
  144                 kp = kp->e_next;
  145         }
  146 
  147         so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
  148         so->so_state |= SS_ISCONNECTED;
  149         if (head && (so->so_state & SS_INCOMP)) {
  150                 postevent(head,0,EV_RCONN);
  151                 TAILQ_REMOVE(&head->so_incomp, so, so_list);
  152                 head->so_incqlen--;
  153                 so->so_state &= ~SS_INCOMP;
  154                 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
  155                 so->so_state |= SS_COMP;
  156                 sorwakeup(head);
  157                 wakeup_one(&head->so_timeo);
  158         } else {
  159                 postevent(so,0,EV_WCONN);
  160                 wakeup((caddr_t)&so->so_timeo);
  161                 sorwakeup(so);
  162                 sowwakeup(so);
  163         }
  164 }
  165 
  166 void
  167 soisdisconnecting(so)
  168         register struct socket *so;
  169 {
  170         register struct kextcb *kp;
  171 
  172         kp = sotokextcb(so);
  173         while (kp) {
  174                 if (kp->e_soif && kp->e_soif->sf_soisdisconnecting) {
  175                         if ((*kp->e_soif->sf_soisdisconnecting)(so, kp))
  176                                 return;
  177                 }
  178                 kp = kp->e_next;
  179         }
  180 
  181         so->so_state &= ~SS_ISCONNECTING;
  182         so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
  183         wakeup((caddr_t)&so->so_timeo);
  184         sowwakeup(so);
  185         sorwakeup(so);
  186 }
  187 
  188 void
  189 soisdisconnected(so)
  190         register struct socket *so;
  191 {
  192         register struct kextcb *kp;
  193 
  194         kp = sotokextcb(so);
  195         while (kp) {
  196                 if (kp->e_soif && kp->e_soif->sf_soisdisconnected) {
  197                         if ((*kp->e_soif->sf_soisdisconnected)(so, kp))
  198                                 return;
  199                 }
  200                 kp = kp->e_next;
  201         }
  202 
  203         so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
  204         so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
  205         wakeup((caddr_t)&so->so_timeo);
  206         sowwakeup(so);
  207         sorwakeup(so);
  208 }
  209 
  210 /*
  211  * Return a random connection that hasn't been serviced yet and
  212  * is eligible for discard.  There is a one in qlen chance that
  213  * we will return a null, saying that there are no dropable
  214  * requests.  In this case, the protocol specific code should drop
  215  * the new request.  This insures fairness.
  216  *
  217  * This may be used in conjunction with protocol specific queue
  218  * congestion routines.
  219  */
  220 struct socket *
  221 sodropablereq(head)
  222         register struct socket *head;
  223 {
  224         register struct socket *so;
  225         unsigned int i, j, qlen;
  226         static int rnd;
  227         static struct timeval old_runtime;
  228         static unsigned int cur_cnt, old_cnt;
  229         struct timeval tv;
  230 
  231         microtime(&tv);
  232         if ((i = (tv.tv_sec - old_runtime.tv_sec)) != 0) {
  233                 old_runtime = tv;
  234                 old_cnt = cur_cnt / i;
  235                 cur_cnt = 0;
  236         }
  237 
  238         so = TAILQ_FIRST(&head->so_incomp);
  239         if (!so)
  240                 return (so);
  241 
  242         qlen = head->so_incqlen;
  243         if (++cur_cnt > qlen || old_cnt > qlen) {
  244                 rnd = (314159 * rnd + 66329) & 0xffff;
  245                 j = ((qlen + 1) * rnd) >> 16;
  246 
  247                 while (j-- && so)
  248                     so = TAILQ_NEXT(so, so_list);
  249         }
  250 
  251         return (so);
  252 }
  253 
  254 /*
  255  * When an attempt at a new connection is noted on a socket
  256  * which accepts connections, sonewconn is called.  If the
  257  * connection is possible (subject to space constraints, etc.)
  258  * then we allocate a new structure, propoerly linked into the
  259  * data structure of the original socket, and return this.
  260  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
  261  */
  262 struct socket *
  263 sonewconn(head, connstatus)
  264         register struct socket *head;
  265         int connstatus;
  266 {
  267         int error = 0;
  268         register struct socket *so;
  269         register struct kextcb *kp;
  270 
  271         if (head->so_qlen > 3 * head->so_qlimit / 2)
  272                 return ((struct socket *)0);
  273         so = soalloc(1, head->so_proto->pr_domain->dom_family, head->so_type);
  274         if (so == NULL)
  275                 return ((struct socket *)0);
  276         /* check if head was closed during the soalloc */
  277         if (head->so_proto == NULL) {
  278           sodealloc(so);
  279           return ((struct socket *)0);
  280         }
  281 
  282         so->so_head = head;
  283         so->so_type = head->so_type;
  284         so->so_options = head->so_options &~ SO_ACCEPTCONN;
  285         so->so_linger = head->so_linger;
  286         so->so_state = head->so_state | SS_NOFDREF;
  287         so->so_proto = head->so_proto;
  288         so->so_timeo = head->so_timeo;
  289         so->so_pgid  = head->so_pgid;
  290         so->so_uid = head->so_uid;
  291 
  292         /* Attach socket filters for this protocol */
  293         if (so->so_proto->pr_sfilter.tqh_first)
  294                 error = sfilter_init(so);
  295         if (error != 0) {
  296                 sodealloc(so);
  297                 return ((struct socket *)0);
  298         }
  299 
  300         /* Call socket filters' sonewconn1 function if set */
  301         kp = sotokextcb(so);
  302         while (kp) {
  303                 if (kp->e_soif && kp->e_soif->sf_sonewconn) {
  304                         error = (int)(*kp->e_soif->sf_sonewconn)(so, connstatus, kp);
  305                         if (error == EJUSTRETURN) {
  306                                 return so;
  307                         } else if (error != 0) {
  308                                 sodealloc(so);
  309                                 return NULL;
  310                         }
  311                 }
  312                 kp = kp->e_next;
  313         }
  314 
  315         if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
  316             (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
  317                 sfilter_term(so);
  318                 sodealloc(so);
  319                 return ((struct socket *)0);
  320         }
  321 #ifdef __APPLE__
  322         so->so_proto->pr_domain->dom_refs++;
  323 #endif
  324 
  325         if (connstatus) {
  326                 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
  327                 so->so_state |= SS_COMP;
  328         } else {
  329                 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
  330                 so->so_state |= SS_INCOMP;
  331                 head->so_incqlen++;
  332         }
  333         head->so_qlen++;
  334         if (connstatus) {
  335                 sorwakeup(head);
  336                 wakeup((caddr_t)&head->so_timeo);
  337                 so->so_state |= connstatus;
  338         }
  339 #ifdef __APPLE__
  340         so->so_rcv.sb_so = so->so_snd.sb_so = so;
  341         TAILQ_INIT(&so->so_evlist);
  342 #endif
  343         return (so);
  344 }
  345 
  346 /*
  347  * Socantsendmore indicates that no more data will be sent on the
  348  * socket; it would normally be applied to a socket when the user
  349  * informs the system that no more data is to be sent, by the protocol
  350  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
  351  * will be received, and will normally be applied to the socket by a
  352  * protocol when it detects that the peer will send no more data.
  353  * Data queued for reading in the socket may yet be read.
  354  */
  355 
  356 void
  357 socantsendmore(so)
  358         struct socket *so;
  359 {
  360         register struct kextcb *kp;
  361         
  362         kp = sotokextcb(so);
  363         while (kp) {
  364                 if (kp->e_soif && kp->e_soif->sf_socantsendmore) {
  365                         if ((*kp->e_soif->sf_socantsendmore)(so, kp))
  366                                 return;
  367                 }
  368                 kp = kp->e_next;
  369         }
  370 
  371 
  372         so->so_state |= SS_CANTSENDMORE;
  373         sowwakeup(so);
  374 }
  375 
  376 void
  377 socantrcvmore(so)
  378         struct socket *so;
  379 {
  380         register struct kextcb *kp;
  381 
  382         kp = sotokextcb(so);
  383         while (kp) {
  384                 if (kp->e_soif && kp->e_soif->sf_socantrcvmore) {
  385                         if ((*kp->e_soif->sf_socantrcvmore)(so, kp))
  386                                 return;
  387                 }
  388                 kp = kp->e_next;
  389         }
  390 
  391 
  392         so->so_state |= SS_CANTRCVMORE;
  393         sorwakeup(so);
  394 }
  395 
  396 /*
  397  * Wait for data to arrive at/drain from a socket buffer.
  398  */
  399 int
  400 sbwait(sb)
  401         struct sockbuf *sb;
  402 {
  403 
  404         sb->sb_flags |= SB_WAIT;
  405         return (tsleep((caddr_t)&sb->sb_cc,
  406             (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
  407             sb->sb_timeo));
  408 }
  409 
  410 /*
  411  * Lock a sockbuf already known to be locked;
  412  * return any error returned from sleep (EINTR).
  413  */
  414 int
  415 sb_lock(sb)
  416         register struct sockbuf *sb;
  417 {
  418         int error;
  419 
  420         while (sb->sb_flags & SB_LOCK) {
  421                 sb->sb_flags |= SB_WANT;
  422                 error = tsleep((caddr_t)&sb->sb_flags,
  423                     (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
  424                     "sblock", 0);
  425                 if (error)
  426                         return (error);
  427         }
  428         sb->sb_flags |= SB_LOCK;
  429         return (0);
  430 }
  431 
  432 /*
  433  * Wakeup processes waiting on a socket buffer.
  434  * Do asynchronous notification via SIGIO
  435  * if the socket has the SS_ASYNC flag set.
  436  */
  437 void
  438 sowakeup(so, sb)
  439         register struct socket *so;
  440         register struct sockbuf *sb;
  441 {
  442         struct proc *p = current_proc();
  443         /* We clear the flag before calling selwakeup. */
  444         /* BSD calls selwakeup then sets the flag */
  445         sb->sb_flags &= ~SB_SEL;
  446         selwakeup(&sb->sb_sel);
  447         if (sb->sb_flags & SB_WAIT) {
  448                 sb->sb_flags &= ~SB_WAIT;
  449                 wakeup((caddr_t)&sb->sb_cc);
  450         }
  451         if (so->so_state & SS_ASYNC) {
  452                 if (so->so_pgid < 0)
  453                         gsignal(-so->so_pgid, SIGIO);
  454                 else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
  455                         psignal(p, SIGIO);
  456         }
  457         if (sb->sb_flags & SB_UPCALL)
  458                 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
  459         if (sb->sb_flags & SB_KNOTE &&
  460                 !(sb->sb_sel.si_flags & SI_INITED))
  461                 KNOTE(&sb->sb_sel.si_note, 0);
  462 }
  463 
  464 /*
  465  * Socket buffer (struct sockbuf) utility routines.
  466  *
  467  * Each socket contains two socket buffers: one for sending data and
  468  * one for receiving data.  Each buffer contains a queue of mbufs,
  469  * information about the number of mbufs and amount of data in the
  470  * queue, and other fields allowing select() statements and notification
  471  * on data availability to be implemented.
  472  *
  473  * Data stored in a socket buffer is maintained as a list of records.
  474  * Each record is a list of mbufs chained together with the m_next
  475  * field.  Records are chained together with the m_nextpkt field. The upper
  476  * level routine soreceive() expects the following conventions to be
  477  * observed when placing information in the receive buffer:
  478  *
  479  * 1. If the protocol requires each message be preceded by the sender's
  480  *    name, then a record containing that name must be present before
  481  *    any associated data (mbuf's must be of type MT_SONAME).
  482  * 2. If the protocol supports the exchange of ``access rights'' (really
  483  *    just additional data associated with the message), and there are
  484  *    ``rights'' to be received, then a record containing this data
  485  *    should be present (mbuf's must be of type MT_RIGHTS).
  486  * 3. If a name or rights record exists, then it must be followed by
  487  *    a data record, perhaps of zero length.
  488  *
  489  * Before using a new socket structure it is first necessary to reserve
  490  * buffer space to the socket, by calling sbreserve().  This should commit
  491  * some of the available buffer space in the system buffer pool for the
  492  * socket (currently, it does nothing but enforce limits).  The space
  493  * should be released by calling sbrelease() when the socket is destroyed.
  494  */
  495 
  496 int
  497 soreserve(so, sndcc, rcvcc)
  498         register struct socket *so;
  499         u_long sndcc, rcvcc;
  500 {
  501         register struct kextcb *kp;
  502 
  503         kp = sotokextcb(so);
  504         while (kp) {
  505                 if (kp->e_soif && kp->e_soif->sf_soreserve) {
  506                         if ((*kp->e_soif->sf_soreserve)(so, sndcc, rcvcc, kp))
  507                                 return;
  508                 }
  509                 kp = kp->e_next;
  510         }
  511 
  512         if (sbreserve(&so->so_snd, sndcc) == 0)
  513                 goto bad;
  514         if (sbreserve(&so->so_rcv, rcvcc) == 0)
  515                 goto bad2;
  516         if (so->so_rcv.sb_lowat == 0)
  517                 so->so_rcv.sb_lowat = 1;
  518         if (so->so_snd.sb_lowat == 0)
  519                 so->so_snd.sb_lowat = MCLBYTES;
  520         if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
  521                 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
  522         return (0);
  523 bad2:
  524 #ifdef __APPLE__
  525         selthreadclear(&so->so_snd.sb_sel);
  526 #endif
  527         sbrelease(&so->so_snd);
  528 bad:
  529         return (ENOBUFS);
  530 }
  531 
  532 /*
  533  * Allot mbufs to a sockbuf.
  534  * Attempt to scale mbmax so that mbcnt doesn't become limiting
  535  * if buffering efficiency is near the normal case.
  536  */
  537 int
  538 sbreserve(sb, cc)
  539         struct sockbuf *sb;
  540         u_long cc;
  541 {
  542         if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES))
  543                 return (0);
  544         sb->sb_hiwat = cc;
  545         sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
  546         if (sb->sb_lowat > sb->sb_hiwat)
  547                 sb->sb_lowat = sb->sb_hiwat;
  548         return (1);
  549 }
  550 
  551 /*
  552  * Free mbufs held by a socket, and reserved mbuf space.
  553  */
  554  /*  WARNING needs to do selthreadclear() before calling this */
  555 void
  556 sbrelease(sb)
  557         struct sockbuf *sb;
  558 {
  559 
  560         sbflush(sb);
  561         sb->sb_hiwat = 0;
  562         sb->sb_mbmax = 0;
  563         
  564 }
  565 
  566 /*
  567  * Routines to add and remove
  568  * data from an mbuf queue.
  569  *
  570  * The routines sbappend() or sbappendrecord() are normally called to
  571  * append new mbufs to a socket buffer, after checking that adequate
  572  * space is available, comparing the function sbspace() with the amount
  573  * of data to be added.  sbappendrecord() differs from sbappend() in
  574  * that data supplied is treated as the beginning of a new record.
  575  * To place a sender's address, optional access rights, and data in a
  576  * socket receive buffer, sbappendaddr() should be used.  To place
  577  * access rights and data in a socket receive buffer, sbappendrights()
  578  * should be used.  In either case, the new data begins a new record.
  579  * Note that unlike sbappend() and sbappendrecord(), these routines check
  580  * for the caller that there will be enough space to store the data.
  581  * Each fails if there is not enough space, or if it cannot find mbufs
  582  * to store additional information in.
  583  *
  584  * Reliable protocols may use the socket send buffer to hold data
  585  * awaiting acknowledgement.  Data is normally copied from a socket
  586  * send buffer in a protocol with m_copy for output to a peer,
  587  * and then removing the data from the socket buffer with sbdrop()
  588  * or sbdroprecord() when the data is acknowledged by the peer.
  589  */
  590 
  591 /*
  592  * Append mbuf chain m to the last record in the
  593  * socket buffer sb.  The additional space associated
  594  * the mbuf chain is recorded in sb.  Empty mbufs are
  595  * discarded and mbufs are compacted where possible.
  596  */
  597 void
  598 sbappend(sb, m)
  599         struct sockbuf *sb;
  600         struct mbuf *m;
  601 {
  602         struct kextcb *kp;
  603         register struct mbuf *n;
  604 
  605 
  606         KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_START), sb, m->m_len, 0, 0, 0);
  607 
  608         if (m == 0)
  609                 return;
  610         kp = sotokextcb(sbtoso(sb));
  611         while (kp) {
  612                 if (kp->e_sout && kp->e_sout->su_sbappend) {
  613                         if ((*kp->e_sout->su_sbappend)(sb, m, kp)) {
  614                                 KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_END), sb, sb->sb_cc, kp, 0, 0);
  615                                 return;
  616                         }
  617                 }
  618                 kp = kp->e_next;
  619         }
  620         n = sb->sb_mb;
  621         if (n) {
  622                 while (n->m_nextpkt)
  623                         n = n->m_nextpkt;
  624                 do {
  625                         if (n->m_flags & M_EOR) {
  626                                 sbappendrecord(sb, m); /* XXXXXX!!!! */
  627                                 KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_END), sb, sb->sb_cc, 0, 0, 0);
  628                                 return;
  629                         }
  630                 } while (n->m_next && (n = n->m_next));
  631         }
  632         sbcompress(sb, m, n);
  633 
  634         KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_END), sb, sb->sb_cc, 0, 0, 0);
  635 }
  636 
  637 #ifdef SOCKBUF_DEBUG
  638 void
  639 sbcheck(sb)
  640         register struct sockbuf *sb;
  641 {
  642         register struct mbuf *m;
  643         register struct mbuf *n = 0;
  644         register u_long len = 0, mbcnt = 0;
  645 
  646         for (m = sb->sb_mb; m; m = n) {
  647             n = m->m_nextpkt;
  648             for (; m; m = m->m_next) {
  649                 len += m->m_len;
  650                 mbcnt += MSIZE;
  651                 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
  652                     mbcnt += m->m_ext.ext_size;
  653             }
  654         }
  655 #ifndef __APPLE__
  656         if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
  657                 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
  658                     mbcnt, sb->sb_mbcnt);
  659                 panic("sbcheck");
  660         }
  661 #else
  662         if (len != sb->sb_cc)
  663             printf("sbcheck len %ld != sb_cc %ld\n", len, sb->sb_cc);
  664         if (mbcnt != sb->sb_mbcnt)
  665             printf("sbcheck mbcnt %ld != sb_mbcnt %ld\n", mbcnt, sb->sb_mbcnt);
  666 #endif
  667 }
  668 #endif
  669 
  670 /*
  671  * As above, except the mbuf chain
  672  * begins a new record.
  673  */
  674 void
  675 sbappendrecord(sb, m0)
  676         register struct sockbuf *sb;
  677         register struct mbuf *m0;
  678 {
  679         register struct mbuf *m;
  680         register struct kextcb *kp;
  681 
  682         if (m0 == 0)
  683                 return;
  684         
  685         kp = sotokextcb(sbtoso(sb));
  686         while (kp)
  687         {       if (kp->e_sout && kp->e_sout->su_sbappendrecord)
  688                 {       if ((*kp->e_sout->su_sbappendrecord)(sb, m0, kp))
  689                                 return;
  690                 }
  691                 kp = kp->e_next;
  692         }
  693     
  694         m = sb->sb_mb;
  695         if (m)
  696                 while (m->m_nextpkt)
  697                         m = m->m_nextpkt;
  698         /*
  699          * Put the first mbuf on the queue.
  700          * Note this permits zero length records.
  701          */
  702         sballoc(sb, m0);
  703         if (m)
  704                 m->m_nextpkt = m0;
  705         else
  706                 sb->sb_mb = m0;
  707         m = m0->m_next;
  708         m0->m_next = 0;
  709         if (m && (m0->m_flags & M_EOR)) {
  710                 m0->m_flags &= ~M_EOR;
  711                 m->m_flags |= M_EOR;
  712         }
  713         sbcompress(sb, m, m0);
  714 }
  715 
  716 /*
  717  * As above except that OOB data
  718  * is inserted at the beginning of the sockbuf,
  719  * but after any other OOB data.
  720  */
  721 void
  722 sbinsertoob(sb, m0)
  723         register struct sockbuf *sb;
  724         register struct mbuf *m0;
  725 {
  726         register struct mbuf *m;
  727         register struct mbuf **mp;
  728         register struct kextcb *kp;
  729 
  730         if (m0 == 0)
  731                 return;
  732         
  733         kp = sotokextcb(sbtoso(sb));
  734         while (kp)
  735         {       if (kp->e_sout && kp->e_sout->su_sbinsertoob)
  736                 {       if ((*kp->e_sout->su_sbinsertoob)(sb, m0, kp))
  737                                 return;
  738                 }
  739                 kp = kp->e_next;
  740         }
  741     
  742         for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
  743             m = *mp;
  744             again:
  745                 switch (m->m_type) {
  746 
  747                 case MT_OOBDATA:
  748                         continue;               /* WANT next train */
  749 
  750                 case MT_CONTROL:
  751                         m = m->m_next;
  752                         if (m)
  753                                 goto again;     /* inspect THIS train further */
  754                 }
  755                 break;
  756         }
  757         /*
  758          * Put the first mbuf on the queue.
  759          * Note this permits zero length records.
  760          */
  761         sballoc(sb, m0);
  762         m0->m_nextpkt = *mp;
  763         *mp = m0;
  764         m = m0->m_next;
  765         m0->m_next = 0;
  766         if (m && (m0->m_flags & M_EOR)) {
  767                 m0->m_flags &= ~M_EOR;
  768                 m->m_flags |= M_EOR;
  769         }
  770         sbcompress(sb, m, m0);
  771 }
  772 
  773 /*
  774  * Append address and data, and optionally, control (ancillary) data
  775  * to the receive queue of a socket.  If present,
  776  * m0 must include a packet header with total length.
  777  * Returns 0 if no space in sockbuf or insufficient mbufs.
  778  */
  779 int
  780 sbappendaddr(sb, asa, m0, control)
  781         register struct sockbuf *sb;
  782         struct sockaddr *asa;
  783         struct mbuf *m0, *control;
  784 {
  785         register struct mbuf *m, *n;
  786         int space = asa->sa_len;
  787         register struct kextcb *kp;
  788 
  789         if (m0 && (m0->m_flags & M_PKTHDR) == 0)
  790                 panic("sbappendaddr");
  791 
  792         kp = sotokextcb(sbtoso(sb));
  793         while (kp)
  794         {       if (kp->e_sout && kp->e_sout->su_sbappendaddr)
  795                 {       if ((*kp->e_sout->su_sbappendaddr)(sb, asa, m0, control, kp))
  796                                 return 0;
  797                 }
  798                 kp = kp->e_next;
  799         }
  800 
  801         if (m0)
  802                 space += m0->m_pkthdr.len;
  803         for (n = control; n; n = n->m_next) {
  804                 space += n->m_len;
  805                 if (n->m_next == 0)     /* keep pointer to last control buf */
  806                         break;
  807         }
  808         if (space > sbspace(sb))
  809                 return (0);
  810         if (asa->sa_len > MLEN)
  811                 return (0);
  812         MGET(m, M_DONTWAIT, MT_SONAME);
  813         if (m == 0)
  814                 return (0);
  815         m->m_len = asa->sa_len;
  816         bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
  817         if (n)
  818                 n->m_next = m0;         /* concatenate data to control */
  819         else
  820                 control = m0;
  821         m->m_next = control;
  822         for (n = m; n; n = n->m_next)
  823                 sballoc(sb, n);
  824         n = sb->sb_mb;
  825         if (n) {
  826                 while (n->m_nextpkt)
  827                         n = n->m_nextpkt;
  828                 n->m_nextpkt = m;
  829         } else
  830                 sb->sb_mb = m;
  831         postevent(0,sb,EV_RWBYTES);
  832         return (1);
  833 }
  834 
  835 int
  836 sbappendcontrol(sb, m0, control)
  837         struct sockbuf *sb;
  838         struct mbuf *control, *m0;
  839 {
  840         register struct mbuf *m, *n;
  841         int space = 0;
  842         register struct kextcb *kp;
  843 
  844         if (control == 0)
  845                 panic("sbappendcontrol");
  846 
  847         kp = sotokextcb(sbtoso(sb));
  848         while (kp)
  849         {       if (kp->e_sout && kp->e_sout->su_sbappendcontrol)
  850                 {       if ((*kp->e_sout->su_sbappendcontrol)(sb, m0, control, kp))
  851                                 return 0;
  852                 }
  853                 kp = kp->e_next;
  854         }
  855 
  856         for (m = control; ; m = m->m_next) {
  857                 space += m->m_len;
  858                 if (m->m_next == 0)
  859                         break;
  860         }
  861         n = m;                  /* save pointer to last control buffer */
  862         for (m = m0; m; m = m->m_next)
  863                 space += m->m_len;
  864         if (space > sbspace(sb))
  865                 return (0);
  866         n->m_next = m0;                 /* concatenate data to control */
  867         for (m = control; m; m = m->m_next)
  868                 sballoc(sb, m);
  869         n = sb->sb_mb;
  870         if (n) {
  871                 while (n->m_nextpkt)
  872                         n = n->m_nextpkt;
  873                 n->m_nextpkt = control;
  874         } else
  875                 sb->sb_mb = control;
  876         postevent(0,sb,EV_RWBYTES);
  877         return (1);
  878 }
  879 
  880 /*
  881  * Compress mbuf chain m into the socket
  882  * buffer sb following mbuf n.  If n
  883  * is null, the buffer is presumed empty.
  884  */
  885 void
  886 sbcompress(sb, m, n)
  887         register struct sockbuf *sb;
  888         register struct mbuf *m, *n;
  889 {
  890         register int eor = 0;
  891         register struct mbuf *o;
  892 
  893         while (m) {
  894                 eor |= m->m_flags & M_EOR;
  895                 if (m->m_len == 0 &&
  896                     (eor == 0 ||
  897                      (((o = m->m_next) || (o = n)) &&
  898                       o->m_type == m->m_type))) {
  899                         m = m_free(m);
  900                         continue;
  901                 }
  902                 if (n && (n->m_flags & M_EOR) == 0 &&
  903 #ifndef __APPLE__
  904                     M_WRITABLE(n) &&
  905 #endif
  906                     m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
  907                     m->m_len <= M_TRAILINGSPACE(n) &&
  908                     n->m_type == m->m_type) {
  909                         bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
  910                             (unsigned)m->m_len);
  911                         n->m_len += m->m_len;
  912                         sb->sb_cc += m->m_len;
  913                         m = m_free(m);
  914                         continue;
  915                 }
  916                 if (n)
  917                         n->m_next = m;
  918                 else
  919                         sb->sb_mb = m;
  920                 sballoc(sb, m);
  921                 n = m;
  922                 m->m_flags &= ~M_EOR;
  923                 m = m->m_next;
  924                 n->m_next = 0;
  925         }
  926         if (eor) {
  927                 if (n)
  928                         n->m_flags |= eor;
  929                 else
  930                         printf("semi-panic: sbcompress\n");
  931         }
  932         postevent(0,sb, EV_RWBYTES);
  933 }
  934 
  935 /*
  936  * Free all mbufs in a sockbuf.
  937  * Check that all resources are reclaimed.
  938  */
  939 void
  940 sbflush(sb)
  941         register struct sockbuf *sb;
  942 {
  943         register struct kextcb *kp;
  944 
  945         kp = sotokextcb(sbtoso(sb));
  946         while (kp) {
  947                 if (kp->e_sout && kp->e_sout->su_sbflush) {
  948                         if ((*kp->e_sout->su_sbflush)(sb, kp))
  949                                 return;
  950                 }
  951                 kp = kp->e_next;
  952         }
  953 
  954         (void)sblock(sb, M_WAIT);
  955         while (sb->sb_mbcnt) {
  956                 /*
  957                  * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
  958                  * we would loop forever. Panic instead.
  959                  */
  960                 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
  961                         break;
  962                 sbdrop(sb, (int)sb->sb_cc);
  963         }
  964         if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
  965                 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
  966 
  967         sbunlock(sb);
  968 
  969         postevent(0, sb, EV_RWBYTES);
  970 }
  971 
  972 /*
  973  * Drop data from (the front of) a sockbuf.
  974  * use m_freem_list to free the mbuf structures
  975  * under a single lock... this is done by pruning
  976  * the top of the tree from the body by keeping track
  977  * of where we get to in the tree and then zeroing the
  978  * two pertinent pointers m_nextpkt and m_next
  979  * the socket buffer is then updated to point at the new
  980  * top of the tree and the pruned area is released via
  981  * m_freem_list.
  982  */
  983 void
  984 sbdrop(sb, len)
  985         register struct sockbuf *sb;
  986         register int len;
  987 {
  988         register struct mbuf *m, *free_list, *ml;
  989         struct mbuf *next, *last;
  990         register struct kextcb *kp;
  991 
  992         KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_START), sb, len, 0, 0, 0);
  993 
  994         kp = sotokextcb(sbtoso(sb));
  995         while (kp) {
  996                 if (kp->e_sout && kp->e_sout->su_sbdrop) {
  997                         if ((*kp->e_sout->su_sbdrop)(sb, len, kp)) {
  998                                 KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_END), sb, len, kp, 0, 0);
  999                                 return;
 1000                         }
 1001                 }
 1002                 kp = kp->e_next;
 1003         }
 1004         next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
 1005         free_list = last = m;
 1006         ml = (struct mbuf *)0;
 1007 
 1008         while (len > 0) {
 1009                 if (m == 0) {
 1010                   if (next == 0) {
 1011                     /* temporarily replacing this panic with printf because
 1012                      * it occurs occasionally when closing a socket when there
 1013                      * is no harm in ignoring it.  This problem will be investigated
 1014                      * further.
 1015                      */
 1016                     /* panic("sbdrop"); */
 1017                     printf("sbdrop - count not zero\n");
 1018                     len = 0;
 1019                     /* zero the counts. if we have no mbufs, we have no data (PR-2986815) */
 1020                     sb->sb_cc = 0;
 1021                     sb->sb_mbcnt = 0;
 1022                     break;
 1023                   }
 1024                   m = last = next;
 1025                   next = m->m_nextpkt;
 1026                   continue;
 1027                 }
 1028                 if (m->m_len > len) {
 1029                         m->m_len -= len;
 1030                         m->m_data += len;
 1031                         sb->sb_cc -= len;
 1032                         break;
 1033                 }
 1034                 len -= m->m_len;
 1035                 sbfree(sb, m);
 1036 
 1037                 ml = m;
 1038                 m = m->m_next;
 1039         }
 1040         while (m && m->m_len == 0) {
 1041                 sbfree(sb, m);
 1042 
 1043                 ml = m;
 1044                 m = m->m_next;
 1045         }
 1046         if (ml) {
 1047                 ml->m_next = (struct mbuf *)0;
 1048                 last->m_nextpkt = (struct mbuf *)0;
 1049                 m_freem_list(free_list);
 1050         }
 1051         if (m) {
 1052                 sb->sb_mb = m;
 1053                 m->m_nextpkt = next;
 1054         } else
 1055                 sb->sb_mb = next;
 1056 
 1057         postevent(0, sb, EV_RWBYTES);
 1058 
 1059         KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_END), sb, 0, 0, 0, 0);
 1060 }
 1061 
 1062 /*
 1063  * Drop a record off the front of a sockbuf
 1064  * and move the next record to the front.
 1065  */
 1066 void
 1067 sbdroprecord(sb)
 1068         register struct sockbuf *sb;
 1069 {
 1070         register struct mbuf *m, *mn;
 1071         register struct kextcb *kp;
 1072 
 1073         kp = sotokextcb(sbtoso(sb));
 1074         while (kp) {
 1075                 if (kp->e_sout && kp->e_sout->su_sbdroprecord) {
 1076                         if ((*kp->e_sout->su_sbdroprecord)(sb, kp))
 1077                                 return;
 1078                 }
 1079                 kp = kp->e_next;
 1080         }
 1081 
 1082         m = sb->sb_mb;
 1083         if (m) {
 1084                 sb->sb_mb = m->m_nextpkt;
 1085                 do {
 1086                         sbfree(sb, m);
 1087                         MFREE(m, mn);
 1088                         m = mn;
 1089                 } while (m);
 1090         }
 1091         postevent(0, sb, EV_RWBYTES);
 1092 }
 1093 
 1094 /*
 1095  * Create a "control" mbuf containing the specified data
 1096  * with the specified type for presentation on a socket buffer.
 1097  */
 1098 struct mbuf *
 1099 sbcreatecontrol(p, size, type, level)
 1100         caddr_t p;
 1101         register int size;
 1102         int type, level;
 1103 {
 1104         register struct cmsghdr *cp;
 1105         struct mbuf *m;
 1106 
 1107         if (CMSG_SPACE((u_int)size) > MLEN)
 1108                 return ((struct mbuf *) NULL);
 1109         if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
 1110                 return ((struct mbuf *) NULL);
 1111         cp = mtod(m, struct cmsghdr *);
 1112         /* XXX check size? */
 1113         (void)memcpy(CMSG_DATA(cp), p, size);
 1114         m->m_len = CMSG_SPACE(size);
 1115         cp->cmsg_len = CMSG_LEN(size);
 1116         cp->cmsg_level = level;
 1117         cp->cmsg_type = type;
 1118         return (m);
 1119 }
 1120 
 1121 /*
 1122  * Some routines that return EOPNOTSUPP for entry points that are not
 1123  * supported by a protocol.  Fill in as needed.
 1124  */
 1125 int
 1126 pru_abort_notsupp(struct socket *so)
 1127 {
 1128         return EOPNOTSUPP;
 1129 }
 1130 
 1131 
 1132 int
 1133 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
 1134 {
 1135         return EOPNOTSUPP;
 1136 }
 1137 
 1138 int
 1139 pru_attach_notsupp(struct socket *so, int proto, struct proc *p)
 1140 {
 1141         return EOPNOTSUPP;
 1142 }
 1143 
 1144 int
 1145 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
 1146 {
 1147         return EOPNOTSUPP;
 1148 }
 1149 
 1150 int
 1151 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
 1152 {
 1153         return EOPNOTSUPP;
 1154 }
 1155 
 1156 int
 1157 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
 1158 {
 1159         return EOPNOTSUPP;
 1160 }
 1161 
 1162 int
 1163 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
 1164                     struct ifnet *ifp, struct proc *p)
 1165 {
 1166         return EOPNOTSUPP;
 1167 }
 1168 
 1169 int
 1170 pru_detach_notsupp(struct socket *so)
 1171 {
 1172         return EOPNOTSUPP;
 1173 }
 1174 
 1175 int
 1176 pru_disconnect_notsupp(struct socket *so)
 1177 {
 1178         return EOPNOTSUPP;
 1179 }
 1180 
 1181 int
 1182 pru_listen_notsupp(struct socket *so, struct proc *p)
 1183 {
 1184         return EOPNOTSUPP;
 1185 }
 1186 
 1187 int
 1188 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
 1189 {
 1190         return EOPNOTSUPP;
 1191 }
 1192 
 1193 int
 1194 pru_rcvd_notsupp(struct socket *so, int flags)
 1195 {
 1196         return EOPNOTSUPP;
 1197 }
 1198 
 1199 int
 1200 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
 1201 {
 1202         return EOPNOTSUPP;
 1203 }
 1204 
 1205 int
 1206 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
 1207                  struct sockaddr *addr, struct mbuf *control,
 1208                  struct proc *p)
 1209 
 1210 {
 1211         return EOPNOTSUPP;
 1212 }
 1213 
 1214 
 1215 /*
 1216  * This isn't really a ``null'' operation, but it's the default one
 1217  * and doesn't do anything destructive.
 1218  */
 1219 int
 1220 pru_sense_null(struct socket *so, struct stat *sb)
 1221 {
 1222         sb->st_blksize = so->so_snd.sb_hiwat;
 1223         return 0;
 1224 }
 1225 
 1226 
 1227 int     pru_sosend_notsupp(struct socket *so, struct sockaddr *addr,
 1228                    struct uio *uio, struct mbuf *top,
 1229                    struct mbuf *control, int flags)
 1230 
 1231 {
 1232     return EOPNOTSUPP;
 1233 }
 1234 
 1235 int     pru_soreceive_notsupp(struct socket *so, 
 1236                       struct sockaddr **paddr,
 1237                       struct uio *uio, struct mbuf **mp0,
 1238                       struct mbuf **controlp, int *flagsp)
 1239 {
 1240     return EOPNOTSUPP;
 1241 }
 1242 
 1243 int
 1244 
 1245 pru_shutdown_notsupp(struct socket *so)
 1246 {
 1247         return EOPNOTSUPP;
 1248 }
 1249 
 1250 int
 1251 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
 1252 {
 1253         return EOPNOTSUPP;
 1254 }
 1255 
 1256 int     pru_sosend(struct socket *so, struct sockaddr *addr,
 1257                    struct uio *uio, struct mbuf *top,
 1258                    struct mbuf *control, int flags)
 1259 {
 1260         return EOPNOTSUPP;
 1261 }
 1262 
 1263 int     pru_soreceive(struct socket *so,
 1264                       struct sockaddr **paddr,
 1265                       struct uio *uio, struct mbuf **mp0,
 1266                       struct mbuf **controlp, int *flagsp)
 1267 {
 1268         return EOPNOTSUPP;
 1269 }
 1270 
 1271 
 1272 int     pru_sopoll_notsupp(struct socket *so, int events,
 1273                    struct ucred *cred)
 1274 {
 1275     return EOPNOTSUPP;
 1276 }
 1277 
 1278 
 1279 #ifdef __APPLE__
 1280 /*
 1281  * The following are macros on BSD and functions on Darwin
 1282  */
 1283 
 1284 /*
 1285  * Do we need to notify the other side when I/O is possible?
 1286  */
 1287 
 1288 int 
 1289 sb_notify(struct sockbuf *sb)
 1290 {
 1291         return ((sb->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC|SB_UPCALL|SB_KNOTE)) != 0); 
 1292 }
 1293 
 1294 /*
 1295  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
 1296  * This is problematical if the fields are unsigned, as the space might
 1297  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
 1298  * overflow and return 0.  Should use "lmin" but it doesn't exist now.
 1299  */
 1300 long
 1301 sbspace(struct sockbuf *sb)
 1302 {
 1303     return ((long) imin((int)(sb->sb_hiwat - sb->sb_cc), 
 1304          (int)(sb->sb_mbmax - sb->sb_mbcnt)));
 1305 }
 1306 
 1307 /* do we have to send all at once on a socket? */
 1308 int
 1309 sosendallatonce(struct socket *so)
 1310 {
 1311     return (so->so_proto->pr_flags & PR_ATOMIC);
 1312 }
 1313 
 1314 /* can we read something from so? */
 1315 int
 1316 soreadable(struct socket *so)
 1317 {
 1318     return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat || 
 1319         (so->so_state & SS_CANTRCVMORE) || 
 1320         so->so_comp.tqh_first || so->so_error);
 1321 }
 1322 
 1323 /* can we write something to so? */
 1324 
 1325 int
 1326 sowriteable(struct socket *so)
 1327 {
 1328     return ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && 
 1329         ((so->so_state&SS_ISCONNECTED) || 
 1330           (so->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || 
 1331      (so->so_state & SS_CANTSENDMORE) || 
 1332      so->so_error);
 1333 }
 1334 
 1335 /* adjust counters in sb reflecting allocation of m */
 1336 
 1337 void
 1338 sballoc(struct sockbuf *sb, struct mbuf *m)
 1339 {
 1340         sb->sb_cc += m->m_len; 
 1341         sb->sb_mbcnt += MSIZE; 
 1342         if (m->m_flags & M_EXT) 
 1343                 sb->sb_mbcnt += m->m_ext.ext_size; 
 1344 }
 1345 
 1346 /* adjust counters in sb reflecting freeing of m */
 1347 void
 1348 sbfree(struct sockbuf *sb, struct mbuf *m)
 1349 {
 1350         sb->sb_cc -= m->m_len; 
 1351         sb->sb_mbcnt -= MSIZE; 
 1352         if (m->m_flags & M_EXT) 
 1353                 sb->sb_mbcnt -= m->m_ext.ext_size; 
 1354 }
 1355 
 1356 /*
 1357  * Set lock on sockbuf sb; sleep if lock is already held.
 1358  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
 1359  * Returns error without lock if sleep is interrupted.
 1360  */
 1361 int
 1362 sblock(struct sockbuf *sb, int wf)
 1363 {
 1364         return(sb->sb_flags & SB_LOCK ? 
 1365                 ((wf == M_WAIT) ? sb_lock(sb) : EWOULDBLOCK) : 
 1366                 (sb->sb_flags |= SB_LOCK), 0);
 1367 }
 1368 
 1369 /* release lock on sockbuf sb */
 1370 void
 1371 sbunlock(struct sockbuf *sb)
 1372 {
 1373         sb->sb_flags &= ~SB_LOCK; 
 1374         if (sb->sb_flags & SB_WANT) { 
 1375                 sb->sb_flags &= ~SB_WANT; 
 1376                 wakeup((caddr_t)&(sb)->sb_flags); 
 1377         } 
 1378 }
 1379 
 1380 void
 1381 sorwakeup(struct socket * so)
 1382 {
 1383   if (sb_notify(&so->so_rcv)) 
 1384         sowakeup(so, &so->so_rcv); 
 1385 }
 1386 
 1387 void
 1388 sowwakeup(struct socket * so)
 1389 {
 1390   if (sb_notify(&so->so_snd)) 
 1391         sowakeup(so, &so->so_snd); 
 1392 }
 1393 #endif __APPLE__
 1394 
 1395 /*
 1396  * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
 1397  */
 1398 struct sockaddr *
 1399 dup_sockaddr(sa, canwait)
 1400         struct sockaddr *sa;
 1401         int canwait;
 1402 {
 1403         struct sockaddr *sa2;
 1404 
 1405         MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME, 
 1406                canwait ? M_WAITOK : M_NOWAIT);
 1407         if (sa2)
 1408                 bcopy(sa, sa2, sa->sa_len);
 1409         return sa2;
 1410 }
 1411 
 1412 /*
 1413  * Create an external-format (``xsocket'') structure using the information
 1414  * in the kernel-format socket structure pointed to by so.  This is done
 1415  * to reduce the spew of irrelevant information over this interface,
 1416  * to isolate user code from changes in the kernel structure, and
 1417  * potentially to provide information-hiding if we decide that
 1418  * some of this information should be hidden from users.
 1419  */
 1420 void
 1421 sotoxsocket(struct socket *so, struct xsocket *xso)
 1422 {
 1423         xso->xso_len = sizeof *xso;
 1424         xso->xso_so = so;
 1425         xso->so_type = so->so_type;
 1426         xso->so_options = so->so_options;
 1427         xso->so_linger = so->so_linger;
 1428         xso->so_state = so->so_state;
 1429         xso->so_pcb = so->so_pcb;
 1430         xso->xso_protocol = so->so_proto->pr_protocol;
 1431         xso->xso_family = so->so_proto->pr_domain->dom_family;
 1432         xso->so_qlen = so->so_qlen;
 1433         xso->so_incqlen = so->so_incqlen;
 1434         xso->so_qlimit = so->so_qlimit;
 1435         xso->so_timeo = so->so_timeo;
 1436         xso->so_error = so->so_error;
 1437         xso->so_pgid = so->so_pgid;
 1438         xso->so_oobmark = so->so_oobmark;
 1439         sbtoxsockbuf(&so->so_snd, &xso->so_snd);
 1440         sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
 1441         xso->so_uid = so->so_uid;
 1442 }
 1443 
 1444 /*
 1445  * This does the same for sockbufs.  Note that the xsockbuf structure,
 1446  * since it is always embedded in a socket, does not include a self
 1447  * pointer nor a length.  We make this entry point public in case
 1448  * some other mechanism needs it.
 1449  */
 1450 void
 1451 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
 1452 {
 1453         xsb->sb_cc = sb->sb_cc;
 1454         xsb->sb_hiwat = sb->sb_hiwat;
 1455         xsb->sb_mbcnt = sb->sb_mbcnt;
 1456         xsb->sb_mbmax = sb->sb_mbmax;
 1457         xsb->sb_lowat = sb->sb_lowat;
 1458         xsb->sb_flags = sb->sb_flags;
 1459         xsb->sb_timeo = sb->sb_timeo;
 1460 }
 1461 
 1462 /*
 1463  * Here is the definition of some of the basic objects in the kern.ipc
 1464  * branch of the MIB.
 1465  */
 1466 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
 1467 
 1468 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
 1469 static int dummy;
 1470 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
 1471 
 1472 SYSCTL_INT(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW, 
 1473     &sb_max, 0, "Maximum socket buffer size");
 1474 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD, 
 1475     &maxsockets, 0, "Maximum number of sockets avaliable");
 1476 SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
 1477            &sb_efficiency, 0, "");
 1478 SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD, &nmbclusters, 0, "");
 1479 

Cache object: bc3830e752ee193031691f54529dd320


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