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/netipx/spx_usrreq.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-2005 Robert N. M. Watson
    3  * Copyright (c) 1995, Mike Mitchell
    4  * Copyright (c) 1984, 1985, 1986, 1987, 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  * 3. All advertising materials mentioning features or use of this software
   16  *    must display the following acknowledgement:
   17  *      This product includes software developed by the University of
   18  *      California, Berkeley and its contributors.
   19  * 4. Neither the name of the University nor the names of its contributors
   20  *    may be used to endorse or promote products derived from this software
   21  *    without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   33  * SUCH DAMAGE.
   34  *
   35  *      @(#)spx_usrreq.h
   36  */
   37 
   38 #include <sys/cdefs.h>
   39 __FBSDID("$FreeBSD: src/sys/netipx/spx_usrreq.c,v 1.46.2.15 2005/04/01 15:04:19 rwatson Exp $");
   40 
   41 #include <sys/param.h>
   42 #include <sys/lock.h>
   43 #include <sys/malloc.h>
   44 #include <sys/mbuf.h>
   45 #include <sys/mutex.h>
   46 #include <sys/proc.h>
   47 #include <sys/protosw.h>
   48 #include <sys/signalvar.h>
   49 #include <sys/socket.h>
   50 #include <sys/socketvar.h>
   51 #include <sys/sx.h>
   52 #include <sys/systm.h>
   53 
   54 #include <net/route.h>
   55 #include <netinet/tcp_fsm.h>
   56 
   57 #include <netipx/ipx.h>
   58 #include <netipx/ipx_pcb.h>
   59 #include <netipx/ipx_var.h>
   60 #include <netipx/spx.h>
   61 #include <netipx/spx_debug.h>
   62 #include <netipx/spx_timer.h>
   63 #include <netipx/spx_var.h>
   64 
   65 /*
   66  * SPX protocol implementation.
   67  */
   68 static u_short  spx_iss;
   69 static u_short  spx_newchecks[50];
   70 static int      spx_hardnosed;
   71 static int      spx_use_delack = 0;
   72 static int      traceallspxs = 0;
   73 static struct   spx     spx_savesi;
   74 static struct   spx_istat spx_istat;
   75 
   76 /* Following was struct spxstat spxstat; */
   77 #ifndef spxstat
   78 #define spxstat spx_istat.newstats
   79 #endif
   80 
   81 static const int spx_backoff[SPX_MAXRXTSHIFT+1] =
   82     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
   83 
   84 static  void spx_close(struct spxpcb *cb);
   85 static  void spx_disconnect(struct spxpcb *cb);
   86 static  void spx_drop(struct spxpcb *cb, int errno);
   87 static  int spx_output(struct spxpcb *cb, struct mbuf *m0);
   88 static  int spx_reass(struct spxpcb *cb, struct spx *si);
   89 static  void spx_setpersist(struct spxpcb *cb);
   90 static  void spx_template(struct spxpcb *cb);
   91 static  struct spxpcb *spx_timers(struct spxpcb *cb, int timer);
   92 static  void spx_usrclosed(struct spxpcb *cb);
   93 
   94 static  int spx_usr_abort(struct socket *so);
   95 static  int spx_accept(struct socket *so, struct sockaddr **nam);
   96 static  int spx_attach(struct socket *so, int proto, struct thread *td);
   97 static  int spx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
   98 static  int spx_connect(struct socket *so, struct sockaddr *nam,
   99                         struct thread *td);
  100 static  int spx_detach(struct socket *so);
  101 static  int spx_usr_disconnect(struct socket *so);
  102 static  int spx_listen(struct socket *so, struct thread *td);
  103 static  int spx_rcvd(struct socket *so, int flags);
  104 static  int spx_rcvoob(struct socket *so, struct mbuf *m, int flags);
  105 static  int spx_send(struct socket *so, int flags, struct mbuf *m,
  106                      struct sockaddr *addr, struct mbuf *control,
  107                      struct thread *td);
  108 static  int spx_shutdown(struct socket *so);
  109 static  int spx_sp_attach(struct socket *so, int proto, struct thread *td);
  110 
  111 struct  pr_usrreqs spx_usrreqs = {
  112         spx_usr_abort, spx_accept, spx_attach, spx_bind,
  113         spx_connect, pru_connect2_notsupp, ipx_control, spx_detach,
  114         spx_usr_disconnect, spx_listen, ipx_peeraddr, spx_rcvd,
  115         spx_rcvoob, spx_send, pru_sense_null, spx_shutdown,
  116         ipx_sockaddr, sosend, soreceive, sopoll, pru_sosetlabel_null
  117 };
  118 
  119 struct  pr_usrreqs spx_usrreq_sps = {
  120         spx_usr_abort, spx_accept, spx_sp_attach, spx_bind,
  121         spx_connect, pru_connect2_notsupp, ipx_control, spx_detach,
  122         spx_usr_disconnect, spx_listen, ipx_peeraddr, spx_rcvd,
  123         spx_rcvoob, spx_send, pru_sense_null, spx_shutdown,
  124         ipx_sockaddr, sosend, soreceive, sopoll, pru_sosetlabel_null
  125 };
  126 
  127 void
  128 spx_init()
  129 {
  130 
  131         spx_iss = 1; /* WRONG !! should fish it out of TODR */
  132 }
  133 
  134 void
  135 spx_input(m, ipxp)
  136         register struct mbuf *m;
  137         register struct ipxpcb *ipxp;
  138 {
  139         register struct spxpcb *cb;
  140         register struct spx *si = mtod(m, struct spx *);
  141         register struct socket *so;
  142         int dropsocket = 0;
  143         short ostate = 0;
  144 
  145         spxstat.spxs_rcvtotal++;
  146         KASSERT(ipxp != NULL, ("spx_input: NULL ipxpcb"));
  147 
  148         /*
  149          * spx_input() assumes that the caller will hold both the pcb list
  150          * lock and also the ipxp lock.  spx_input() will release both before
  151          * returning, and may in fact trade in the ipxp lock for another pcb
  152          * lock following sonewconn().
  153          */
  154         IPX_LIST_LOCK_ASSERT();
  155         IPX_LOCK_ASSERT(ipxp);
  156 
  157         cb = ipxtospxpcb(ipxp);
  158         if (cb == NULL)
  159                 goto bad;
  160 
  161         if (m->m_len < sizeof(*si)) {
  162                 if ((m = m_pullup(m, sizeof(*si))) == NULL) {
  163                         IPX_UNLOCK(ipxp);
  164                         IPX_LIST_UNLOCK();
  165                         spxstat.spxs_rcvshort++;
  166                         return;
  167                 }
  168                 si = mtod(m, struct spx *);
  169         }
  170         si->si_seq = ntohs(si->si_seq);
  171         si->si_ack = ntohs(si->si_ack);
  172         si->si_alo = ntohs(si->si_alo);
  173 
  174         so = ipxp->ipxp_socket;
  175 
  176         if (so->so_options & SO_DEBUG || traceallspxs) {
  177                 ostate = cb->s_state;
  178                 spx_savesi = *si;
  179         }
  180         if (so->so_options & SO_ACCEPTCONN) {
  181                 struct spxpcb *ocb = cb;
  182 
  183                 so = sonewconn(so, 0);
  184                 if (so == NULL) {
  185                         goto drop;
  186                 }
  187                 /*
  188                  * This is ugly, but ....
  189                  *
  190                  * Mark socket as temporary until we're
  191                  * committed to keeping it.  The code at
  192                  * ``drop'' and ``dropwithreset'' check the
  193                  * flag dropsocket to see if the temporary
  194                  * socket created here should be discarded.
  195                  * We mark the socket as discardable until
  196                  * we're committed to it below in TCPS_LISTEN.
  197                  */
  198                 dropsocket++;
  199                 IPX_UNLOCK(ipxp);
  200                 ipxp = (struct ipxpcb *)so->so_pcb;
  201                 IPX_LOCK(ipxp);
  202                 ipxp->ipxp_laddr = si->si_dna;
  203                 cb = ipxtospxpcb(ipxp);
  204                 cb->s_mtu = ocb->s_mtu;         /* preserve sockopts */
  205                 cb->s_flags = ocb->s_flags;     /* preserve sockopts */
  206                 cb->s_flags2 = ocb->s_flags2;   /* preserve sockopts */
  207                 cb->s_state = TCPS_LISTEN;
  208         }
  209 
  210         /*
  211          * Packet received on connection.
  212          * reset idle time and keep-alive timer;
  213          */
  214         cb->s_idle = 0;
  215         cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
  216 
  217         switch (cb->s_state) {
  218 
  219         case TCPS_LISTEN:{
  220                 struct sockaddr_ipx *sipx, ssipx;
  221                 struct ipx_addr laddr;
  222 
  223                 /*
  224                  * If somebody here was carying on a conversation
  225                  * and went away, and his pen pal thinks he can
  226                  * still talk, we get the misdirected packet.
  227                  */
  228                 if (spx_hardnosed && (si->si_did != 0 || si->si_seq != 0)) {
  229                         spx_istat.gonawy++;
  230                         goto dropwithreset;
  231                 }
  232                 sipx = &ssipx;
  233                 bzero(sipx, sizeof *sipx);
  234                 sipx->sipx_len = sizeof(*sipx);
  235                 sipx->sipx_family = AF_IPX;
  236                 sipx->sipx_addr = si->si_sna;
  237                 laddr = ipxp->ipxp_laddr;
  238                 if (ipx_nullhost(laddr))
  239                         ipxp->ipxp_laddr = si->si_dna;
  240                 if (ipx_pcbconnect(ipxp, (struct sockaddr *)sipx, &thread0)) {
  241                         ipxp->ipxp_laddr = laddr;
  242                         spx_istat.noconn++;
  243                         goto drop;
  244                 }
  245                 spx_template(cb);
  246                 dropsocket = 0;         /* committed to socket */
  247                 cb->s_did = si->si_sid;
  248                 cb->s_rack = si->si_ack;
  249                 cb->s_ralo = si->si_alo;
  250 #define THREEWAYSHAKE
  251 #ifdef THREEWAYSHAKE
  252                 cb->s_state = TCPS_SYN_RECEIVED;
  253                 cb->s_force = 1 + SPXT_KEEP;
  254                 spxstat.spxs_accepts++;
  255                 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
  256                 }
  257                 break;
  258         /*
  259          * This state means that we have heard a response
  260          * to our acceptance of their connection
  261          * It is probably logically unnecessary in this
  262          * implementation.
  263          */
  264          case TCPS_SYN_RECEIVED: {
  265                 if (si->si_did != cb->s_sid) {
  266                         spx_istat.wrncon++;
  267                         goto drop;
  268                 }
  269 #endif
  270                 ipxp->ipxp_fport =  si->si_sport;
  271                 cb->s_timer[SPXT_REXMT] = 0;
  272                 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
  273                 soisconnected(so);
  274                 cb->s_state = TCPS_ESTABLISHED;
  275                 spxstat.spxs_accepts++;
  276                 }
  277                 break;
  278 
  279         /*
  280          * This state means that we have gotten a response
  281          * to our attempt to establish a connection.
  282          * We fill in the data from the other side,
  283          * telling us which port to respond to, instead of the well-
  284          * known one we might have sent to in the first place.
  285          * We also require that this is a response to our
  286          * connection id.
  287          */
  288         case TCPS_SYN_SENT:
  289                 if (si->si_did != cb->s_sid) {
  290                         spx_istat.notme++;
  291                         goto drop;
  292                 }
  293                 spxstat.spxs_connects++;
  294                 cb->s_did = si->si_sid;
  295                 cb->s_rack = si->si_ack;
  296                 cb->s_ralo = si->si_alo;
  297                 cb->s_dport = ipxp->ipxp_fport =  si->si_sport;
  298                 cb->s_timer[SPXT_REXMT] = 0;
  299                 cb->s_flags |= SF_ACKNOW;
  300                 soisconnected(so);
  301                 cb->s_state = TCPS_ESTABLISHED;
  302                 /* Use roundtrip time of connection request for initial rtt */
  303                 if (cb->s_rtt) {
  304                         cb->s_srtt = cb->s_rtt << 3;
  305                         cb->s_rttvar = cb->s_rtt << 1;
  306                         SPXT_RANGESET(cb->s_rxtcur,
  307                             ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
  308                             SPXTV_MIN, SPXTV_REXMTMAX);
  309                             cb->s_rtt = 0;
  310                 }
  311         }
  312         if (so->so_options & SO_DEBUG || traceallspxs)
  313                 spx_trace(SA_INPUT, (u_char)ostate, cb, &spx_savesi, 0);
  314 
  315         m->m_len -= sizeof(struct ipx);
  316         m->m_pkthdr.len -= sizeof(struct ipx);
  317         m->m_data += sizeof(struct ipx);
  318 
  319         if (spx_reass(cb, si)) {
  320                 m_freem(m);
  321         }
  322         if (cb->s_force || (cb->s_flags & (SF_ACKNOW|SF_WIN|SF_RXT)))
  323                 spx_output(cb, NULL);
  324         cb->s_flags &= ~(SF_WIN|SF_RXT);
  325         IPX_UNLOCK(ipxp);
  326         IPX_LIST_UNLOCK();
  327         return;
  328 
  329 dropwithreset:
  330         IPX_UNLOCK(ipxp);
  331         if (dropsocket) {
  332                 struct socket *head;
  333                 ACCEPT_LOCK();
  334                 KASSERT((so->so_qstate & SQ_INCOMP) != 0,
  335                     ("spx_input: nascent socket not SQ_INCOMP on soabort()"));
  336                 head = so->so_head;
  337                 TAILQ_REMOVE(&head->so_incomp, so, so_list);
  338                 head->so_incqlen--;
  339                 so->so_qstate &= ~SQ_INCOMP;
  340                 so->so_head = NULL;
  341                 ACCEPT_UNLOCK();
  342                 soabort(so);
  343                 cb = NULL;
  344         }
  345         IPX_LIST_UNLOCK();
  346         si->si_seq = ntohs(si->si_seq);
  347         si->si_ack = ntohs(si->si_ack);
  348         si->si_alo = ntohs(si->si_alo);
  349         m_freem(dtom(si));
  350         if (cb == NULL || cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG ||
  351             traceallspxs)
  352                 spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
  353         return;
  354 
  355 drop:
  356 bad:
  357         if (cb == NULL || cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG ||
  358             traceallspxs)
  359                 spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
  360         IPX_UNLOCK(ipxp);
  361         IPX_LIST_UNLOCK();
  362         m_freem(m);
  363 }
  364 
  365 static int spxrexmtthresh = 3;
  366 
  367 /*
  368  * This is structurally similar to the tcp reassembly routine
  369  * but its function is somewhat different:  It merely queues
  370  * packets up, and suppresses duplicates.
  371  */
  372 static int
  373 spx_reass(cb, si)
  374 register struct spxpcb *cb;
  375 register struct spx *si;
  376 {
  377         register struct spx_q *q;
  378         register struct mbuf *m;
  379         register struct socket *so = cb->s_ipxpcb->ipxp_socket;
  380         char packetp = cb->s_flags & SF_HI;
  381         int incr;
  382         char wakeup = 0;
  383 
  384         IPX_LOCK_ASSERT(cb->s_ipxpcb);
  385 
  386         if (si == SI(0))
  387                 goto present;
  388         /*
  389          * Update our news from them.
  390          */
  391         if (si->si_cc & SPX_SA)
  392                 cb->s_flags |= (spx_use_delack ? SF_DELACK : SF_ACKNOW);
  393         if (SSEQ_GT(si->si_alo, cb->s_ralo))
  394                 cb->s_flags |= SF_WIN;
  395         if (SSEQ_LEQ(si->si_ack, cb->s_rack)) {
  396                 if ((si->si_cc & SPX_SP) && cb->s_rack != (cb->s_smax + 1)) {
  397                         spxstat.spxs_rcvdupack++;
  398                         /*
  399                          * If this is a completely duplicate ack
  400                          * and other conditions hold, we assume
  401                          * a packet has been dropped and retransmit
  402                          * it exactly as in tcp_input().
  403                          */
  404                         if (si->si_ack != cb->s_rack ||
  405                             si->si_alo != cb->s_ralo)
  406                                 cb->s_dupacks = 0;
  407                         else if (++cb->s_dupacks == spxrexmtthresh) {
  408                                 u_short onxt = cb->s_snxt;
  409                                 int cwnd = cb->s_cwnd;
  410 
  411                                 cb->s_snxt = si->si_ack;
  412                                 cb->s_cwnd = CUNIT;
  413                                 cb->s_force = 1 + SPXT_REXMT;
  414                                 spx_output(cb, NULL);
  415                                 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
  416                                 cb->s_rtt = 0;
  417                                 if (cwnd >= 4 * CUNIT)
  418                                         cb->s_cwnd = cwnd / 2;
  419                                 if (SSEQ_GT(onxt, cb->s_snxt))
  420                                         cb->s_snxt = onxt;
  421                                 return (1);
  422                         }
  423                 } else
  424                         cb->s_dupacks = 0;
  425                 goto update_window;
  426         }
  427         cb->s_dupacks = 0;
  428         /*
  429          * If our correspondent acknowledges data we haven't sent
  430          * TCP would drop the packet after acking.  We'll be a little
  431          * more permissive
  432          */
  433         if (SSEQ_GT(si->si_ack, (cb->s_smax + 1))) {
  434                 spxstat.spxs_rcvacktoomuch++;
  435                 si->si_ack = cb->s_smax + 1;
  436         }
  437         spxstat.spxs_rcvackpack++;
  438         /*
  439          * If transmit timer is running and timed sequence
  440          * number was acked, update smoothed round trip time.
  441          * See discussion of algorithm in tcp_input.c
  442          */
  443         if (cb->s_rtt && SSEQ_GT(si->si_ack, cb->s_rtseq)) {
  444                 spxstat.spxs_rttupdated++;
  445                 if (cb->s_srtt != 0) {
  446                         register short delta;
  447                         delta = cb->s_rtt - (cb->s_srtt >> 3);
  448                         if ((cb->s_srtt += delta) <= 0)
  449                                 cb->s_srtt = 1;
  450                         if (delta < 0)
  451                                 delta = -delta;
  452                         delta -= (cb->s_rttvar >> 2);
  453                         if ((cb->s_rttvar += delta) <= 0)
  454                                 cb->s_rttvar = 1;
  455                 } else {
  456                         /*
  457                          * No rtt measurement yet
  458                          */
  459                         cb->s_srtt = cb->s_rtt << 3;
  460                         cb->s_rttvar = cb->s_rtt << 1;
  461                 }
  462                 cb->s_rtt = 0;
  463                 cb->s_rxtshift = 0;
  464                 SPXT_RANGESET(cb->s_rxtcur,
  465                         ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
  466                         SPXTV_MIN, SPXTV_REXMTMAX);
  467         }
  468         /*
  469          * If all outstanding data is acked, stop retransmit
  470          * timer and remember to restart (more output or persist).
  471          * If there is more data to be acked, restart retransmit
  472          * timer, using current (possibly backed-off) value;
  473          */
  474         if (si->si_ack == cb->s_smax + 1) {
  475                 cb->s_timer[SPXT_REXMT] = 0;
  476                 cb->s_flags |= SF_RXT;
  477         } else if (cb->s_timer[SPXT_PERSIST] == 0)
  478                 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
  479         /*
  480          * When new data is acked, open the congestion window.
  481          * If the window gives us less than ssthresh packets
  482          * in flight, open exponentially (maxseg at a time).
  483          * Otherwise open linearly (maxseg^2 / cwnd at a time).
  484          */
  485         incr = CUNIT;
  486         if (cb->s_cwnd > cb->s_ssthresh)
  487                 incr = max(incr * incr / cb->s_cwnd, 1);
  488         cb->s_cwnd = min(cb->s_cwnd + incr, cb->s_cwmx);
  489         /*
  490          * Trim Acked data from output queue.
  491          */
  492         SOCKBUF_LOCK(&so->so_snd);
  493         while ((m = so->so_snd.sb_mb) != NULL) {
  494                 if (SSEQ_LT((mtod(m, struct spx *))->si_seq, si->si_ack))
  495                         sbdroprecord_locked(&so->so_snd);
  496                 else
  497                         break;
  498         }
  499         sowwakeup_locked(so);
  500         cb->s_rack = si->si_ack;
  501 update_window:
  502         if (SSEQ_LT(cb->s_snxt, cb->s_rack))
  503                 cb->s_snxt = cb->s_rack;
  504         if (SSEQ_LT(cb->s_swl1, si->si_seq) || ((cb->s_swl1 == si->si_seq &&
  505             (SSEQ_LT(cb->s_swl2, si->si_ack))) ||
  506              (cb->s_swl2 == si->si_ack && SSEQ_LT(cb->s_ralo, si->si_alo)))) {
  507                 /* keep track of pure window updates */
  508                 if ((si->si_cc & SPX_SP) && cb->s_swl2 == si->si_ack
  509                     && SSEQ_LT(cb->s_ralo, si->si_alo)) {
  510                         spxstat.spxs_rcvwinupd++;
  511                         spxstat.spxs_rcvdupack--;
  512                 }
  513                 cb->s_ralo = si->si_alo;
  514                 cb->s_swl1 = si->si_seq;
  515                 cb->s_swl2 = si->si_ack;
  516                 cb->s_swnd = (1 + si->si_alo - si->si_ack);
  517                 if (cb->s_swnd > cb->s_smxw)
  518                         cb->s_smxw = cb->s_swnd;
  519                 cb->s_flags |= SF_WIN;
  520         }
  521         /*
  522          * If this packet number is higher than that which
  523          * we have allocated refuse it, unless urgent
  524          */
  525         if (SSEQ_GT(si->si_seq, cb->s_alo)) {
  526                 if (si->si_cc & SPX_SP) {
  527                         spxstat.spxs_rcvwinprobe++;
  528                         return (1);
  529                 } else
  530                         spxstat.spxs_rcvpackafterwin++;
  531                 if (si->si_cc & SPX_OB) {
  532                         if (SSEQ_GT(si->si_seq, cb->s_alo + 60)) {
  533                                 m_freem(dtom(si));
  534                                 return (0);
  535                         } /* else queue this packet; */
  536                 } else {
  537 #ifdef BROKEN
  538                         /*
  539                          * XXXRW: This is broken on at least one count:
  540                          * spx_close() will free the ipxp and related parts,
  541                          * which are then touched by spx_input() after the
  542                          * return from spx_reass().
  543                          */
  544                         /*register struct socket *so = cb->s_ipxpcb->ipxp_socket;
  545                         if (so->so_state && SS_NOFDREF) {
  546                                 spx_close(cb);
  547                         } else
  548                                        would crash system*/
  549 #endif
  550                         spx_istat.notyet++;
  551                         m_freem(dtom(si));
  552                         return (0);
  553                 }
  554         }
  555         /*
  556          * If this is a system packet, we don't need to
  557          * queue it up, and won't update acknowledge #
  558          */
  559         if (si->si_cc & SPX_SP) {
  560                 return (1);
  561         }
  562         /*
  563          * We have already seen this packet, so drop.
  564          */
  565         if (SSEQ_LT(si->si_seq, cb->s_ack)) {
  566                 spx_istat.bdreas++;
  567                 spxstat.spxs_rcvduppack++;
  568                 if (si->si_seq == cb->s_ack - 1)
  569                         spx_istat.lstdup++;
  570                 return (1);
  571         }
  572         /*
  573          * Loop through all packets queued up to insert in
  574          * appropriate sequence.
  575          */
  576         for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
  577                 if (si->si_seq == SI(q)->si_seq) {
  578                         spxstat.spxs_rcvduppack++;
  579                         return (1);
  580                 }
  581                 if (SSEQ_LT(si->si_seq, SI(q)->si_seq)) {
  582                         spxstat.spxs_rcvoopack++;
  583                         break;
  584                 }
  585         }
  586         insque(si, q->si_prev);
  587         /*
  588          * If this packet is urgent, inform process
  589          */
  590         if (si->si_cc & SPX_OB) {
  591                 cb->s_iobc = ((char *)si)[1 + sizeof(*si)];
  592                 sohasoutofband(so);
  593                 cb->s_oobflags |= SF_IOOB;
  594         }
  595 present:
  596 #define SPINC sizeof(struct spxhdr)
  597         SOCKBUF_LOCK(&so->so_rcv);
  598         /*
  599          * Loop through all packets queued up to update acknowledge
  600          * number, and present all acknowledged data to user;
  601          * If in packet interface mode, show packet headers.
  602          */
  603         for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
  604                   if (SI(q)->si_seq == cb->s_ack) {
  605                         cb->s_ack++;
  606                         m = dtom(q);
  607                         if (SI(q)->si_cc & SPX_OB) {
  608                                 cb->s_oobflags &= ~SF_IOOB;
  609                                 if (so->so_rcv.sb_cc)
  610                                         so->so_oobmark = so->so_rcv.sb_cc;
  611                                 else
  612                                         so->so_rcv.sb_state |= SBS_RCVATMARK;
  613                         }
  614                         q = q->si_prev;
  615                         remque(q->si_next);
  616                         wakeup = 1;
  617                         spxstat.spxs_rcvpack++;
  618 #ifdef SF_NEWCALL
  619                         if (cb->s_flags2 & SF_NEWCALL) {
  620                                 struct spxhdr *sp = mtod(m, struct spxhdr *);
  621                                 u_char dt = sp->spx_dt;
  622                                 spx_newchecks[4]++;
  623                                 if (dt != cb->s_rhdr.spx_dt) {
  624                                         struct mbuf *mm =
  625                                            m_getclr(M_DONTWAIT, MT_CONTROL);
  626                                         spx_newchecks[0]++;
  627                                         if (mm != NULL) {
  628                                                 u_short *s =
  629                                                         mtod(mm, u_short *);
  630                                                 cb->s_rhdr.spx_dt = dt;
  631                                                 mm->m_len = 5; /*XXX*/
  632                                                 s[0] = 5;
  633                                                 s[1] = 1;
  634                                                 *(u_char *)(&s[2]) = dt;
  635                                                 sbappend_locked(&so->so_rcv, mm);
  636                                         }
  637                                 }
  638                                 if (sp->spx_cc & SPX_OB) {
  639                                         MCHTYPE(m, MT_OOBDATA);
  640                                         spx_newchecks[1]++;
  641                                         so->so_oobmark = 0;
  642                                         so->so_rcv.sb_state &= ~SBS_RCVATMARK;
  643                                 }
  644                                 if (packetp == 0) {
  645                                         m->m_data += SPINC;
  646                                         m->m_len -= SPINC;
  647                                         m->m_pkthdr.len -= SPINC;
  648                                 }
  649                                 if ((sp->spx_cc & SPX_EM) || packetp) {
  650                                         sbappendrecord_locked(&so->so_rcv, m);
  651                                         spx_newchecks[9]++;
  652                                 } else
  653                                         sbappend_locked(&so->so_rcv, m);
  654                         } else
  655 #endif
  656                         if (packetp) {
  657                                 sbappendrecord_locked(&so->so_rcv, m);
  658                         } else {
  659                                 cb->s_rhdr = *mtod(m, struct spxhdr *);
  660                                 m->m_data += SPINC;
  661                                 m->m_len -= SPINC;
  662                                 m->m_pkthdr.len -= SPINC;
  663                                 sbappend_locked(&so->so_rcv, m);
  664                         }
  665                   } else
  666                         break;
  667         }
  668         if (wakeup)
  669                 sorwakeup_locked(so);
  670         else
  671                 SOCKBUF_UNLOCK(&so->so_rcv);
  672         return (0);
  673 }
  674 
  675 void
  676 spx_ctlinput(cmd, arg_as_sa, dummy)
  677         int cmd;
  678         struct sockaddr *arg_as_sa;     /* XXX should be swapped with dummy */
  679         void *dummy;
  680 {
  681         caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
  682         struct ipx_addr *na;
  683         struct sockaddr_ipx *sipx;
  684 
  685         if (cmd < 0 || cmd >= PRC_NCMDS)
  686                 return;
  687 
  688         switch (cmd) {
  689 
  690         case PRC_ROUTEDEAD:
  691                 return;
  692 
  693         case PRC_IFDOWN:
  694         case PRC_HOSTDEAD:
  695         case PRC_HOSTUNREACH:
  696                 sipx = (struct sockaddr_ipx *)arg;
  697                 if (sipx->sipx_family != AF_IPX)
  698                         return;
  699                 na = &sipx->sipx_addr;
  700                 break;
  701 
  702         default:
  703                 break;
  704         }
  705 }
  706 
  707 static int
  708 spx_output(cb, m0)
  709         register struct spxpcb *cb;
  710         struct mbuf *m0;
  711 {
  712         struct socket *so = cb->s_ipxpcb->ipxp_socket;
  713         register struct mbuf *m;
  714         register struct spx *si = NULL;
  715         register struct sockbuf *sb = &so->so_snd;
  716         int len = 0, win, rcv_win;
  717         short span, off, recordp = 0;
  718         u_short alo;
  719         int error = 0, sendalot;
  720 #ifdef notdef
  721         int idle;
  722 #endif
  723         struct mbuf *mprev;
  724 
  725         IPX_LOCK_ASSERT(cb->s_ipxpcb);
  726 
  727         if (m0 != NULL) {
  728                 int mtu = cb->s_mtu;
  729                 int datalen;
  730                 /*
  731                  * Make sure that packet isn't too big.
  732                  */
  733                 for (m = m0; m != NULL; m = m->m_next) {
  734                         mprev = m;
  735                         len += m->m_len;
  736                         if (m->m_flags & M_EOR)
  737                                 recordp = 1;
  738                 }
  739                 datalen = (cb->s_flags & SF_HO) ?
  740                                 len - sizeof(struct spxhdr) : len;
  741                 if (datalen > mtu) {
  742                         if (cb->s_flags & SF_PI) {
  743                                 m_freem(m0);
  744                                 return (EMSGSIZE);
  745                         } else {
  746                                 int oldEM = cb->s_cc & SPX_EM;
  747 
  748                                 cb->s_cc &= ~SPX_EM;
  749                                 while (len > mtu) {
  750                                         /*
  751                                          * Here we are only being called
  752                                          * from usrreq(), so it is OK to
  753                                          * block.
  754                                          */
  755                                         m = m_copym(m0, 0, mtu, M_TRYWAIT);
  756                                         if (cb->s_flags & SF_NEWCALL) {
  757                                             struct mbuf *mm = m;
  758                                             spx_newchecks[7]++;
  759                                             while (mm != NULL) {
  760                                                 mm->m_flags &= ~M_EOR;
  761                                                 mm = mm->m_next;
  762                                             }
  763                                         }
  764                                         error = spx_output(cb, m);
  765                                         if (error) {
  766                                                 cb->s_cc |= oldEM;
  767                                                 m_freem(m0);
  768                                                 return (error);
  769                                         }
  770                                         m_adj(m0, mtu);
  771                                         len -= mtu;
  772                                 }
  773                                 cb->s_cc |= oldEM;
  774                         }
  775                 }
  776                 /*
  777                  * Force length even, by adding a "garbage byte" if
  778                  * necessary.
  779                  */
  780                 if (len & 1) {
  781                         m = mprev;
  782                         if (M_TRAILINGSPACE(m) >= 1)
  783                                 m->m_len++;
  784                         else {
  785                                 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
  786 
  787                                 if (m1 == NULL) {
  788                                         m_freem(m0);
  789                                         return (ENOBUFS);
  790                                 }
  791                                 m1->m_len = 1;
  792                                 *(mtod(m1, u_char *)) = 0;
  793                                 m->m_next = m1;
  794                         }
  795                 }
  796                 m = m_gethdr(M_DONTWAIT, MT_HEADER);
  797                 if (m == NULL) {
  798                         m_freem(m0);
  799                         return (ENOBUFS);
  800                 }
  801                 /*
  802                  * Fill in mbuf with extended SP header
  803                  * and addresses and length put into network format.
  804                  */
  805                 MH_ALIGN(m, sizeof(struct spx));
  806                 m->m_len = sizeof(struct spx);
  807                 m->m_next = m0;
  808                 si = mtod(m, struct spx *);
  809                 si->si_i = *cb->s_ipx;
  810                 si->si_s = cb->s_shdr;
  811                 if ((cb->s_flags & SF_PI) && (cb->s_flags & SF_HO)) {
  812                         register struct spxhdr *sh;
  813                         if (m0->m_len < sizeof(*sh)) {
  814                                 if((m0 = m_pullup(m0, sizeof(*sh))) == NULL) {
  815                                         m_free(m);
  816                                         m_freem(m0);
  817                                         return (EINVAL);
  818                                 }
  819                                 m->m_next = m0;
  820                         }
  821                         sh = mtod(m0, struct spxhdr *);
  822                         si->si_dt = sh->spx_dt;
  823                         si->si_cc |= sh->spx_cc & SPX_EM;
  824                         m0->m_len -= sizeof(*sh);
  825                         m0->m_data += sizeof(*sh);
  826                         len -= sizeof(*sh);
  827                 }
  828                 len += sizeof(*si);
  829                 if ((cb->s_flags2 & SF_NEWCALL) && recordp) {
  830                         si->si_cc |= SPX_EM;
  831                         spx_newchecks[8]++;
  832                 }
  833                 if (cb->s_oobflags & SF_SOOB) {
  834                         /*
  835                          * Per jqj@cornell:
  836                          * make sure OB packets convey exactly 1 byte.
  837                          * If the packet is 1 byte or larger, we
  838                          * have already guaranted there to be at least
  839                          * one garbage byte for the checksum, and
  840                          * extra bytes shouldn't hurt!
  841                          */
  842                         if (len > sizeof(*si)) {
  843                                 si->si_cc |= SPX_OB;
  844                                 len = (1 + sizeof(*si));
  845                         }
  846                 }
  847                 si->si_len = htons((u_short)len);
  848                 m->m_pkthdr.len = ((len - 1) | 1) + 1;
  849                 /*
  850                  * queue stuff up for output
  851                  */
  852                 sbappendrecord(sb, m);
  853                 cb->s_seq++;
  854         }
  855 #ifdef notdef
  856         idle = (cb->s_smax == (cb->s_rack - 1));
  857 #endif
  858 again:
  859         sendalot = 0;
  860         off = cb->s_snxt - cb->s_rack;
  861         win = min(cb->s_swnd, (cb->s_cwnd / CUNIT));
  862 
  863         /*
  864          * If in persist timeout with window of 0, send a probe.
  865          * Otherwise, if window is small but nonzero
  866          * and timer expired, send what we can and go into
  867          * transmit state.
  868          */
  869         if (cb->s_force == 1 + SPXT_PERSIST) {
  870                 if (win != 0) {
  871                         cb->s_timer[SPXT_PERSIST] = 0;
  872                         cb->s_rxtshift = 0;
  873                 }
  874         }
  875         span = cb->s_seq - cb->s_rack;
  876         len = min(span, win) - off;
  877 
  878         if (len < 0) {
  879                 /*
  880                  * Window shrank after we went into it.
  881                  * If window shrank to 0, cancel pending
  882                  * restransmission and pull s_snxt back
  883                  * to (closed) window.  We will enter persist
  884                  * state below.  If the widndow didn't close completely,
  885                  * just wait for an ACK.
  886                  */
  887                 len = 0;
  888                 if (win == 0) {
  889                         cb->s_timer[SPXT_REXMT] = 0;
  890                         cb->s_snxt = cb->s_rack;
  891                 }
  892         }
  893         if (len > 1)
  894                 sendalot = 1;
  895         rcv_win = sbspace(&so->so_rcv);
  896 
  897         /*
  898          * Send if we owe peer an ACK.
  899          */
  900         if (cb->s_oobflags & SF_SOOB) {
  901                 /*
  902                  * must transmit this out of band packet
  903                  */
  904                 cb->s_oobflags &= ~ SF_SOOB;
  905                 sendalot = 1;
  906                 spxstat.spxs_sndurg++;
  907                 goto found;
  908         }
  909         if (cb->s_flags & SF_ACKNOW)
  910                 goto send;
  911         if (cb->s_state < TCPS_ESTABLISHED)
  912                 goto send;
  913         /*
  914          * Silly window can't happen in spx.
  915          * Code from tcp deleted.
  916          */
  917         if (len)
  918                 goto send;
  919         /*
  920          * Compare available window to amount of window
  921          * known to peer (as advertised window less
  922          * next expected input.)  If the difference is at least two
  923          * packets or at least 35% of the mximum possible window,
  924          * then want to send a window update to peer.
  925          */
  926         if (rcv_win > 0) {
  927                 u_short delta =  1 + cb->s_alo - cb->s_ack;
  928                 int adv = rcv_win - (delta * cb->s_mtu);
  929 
  930                 if ((so->so_rcv.sb_cc == 0 && adv >= (2 * cb->s_mtu)) ||
  931                     (100 * adv / so->so_rcv.sb_hiwat >= 35)) {
  932                         spxstat.spxs_sndwinup++;
  933                         cb->s_flags |= SF_ACKNOW;
  934                         goto send;
  935                 }
  936 
  937         }
  938         /*
  939          * Many comments from tcp_output.c are appropriate here
  940          * including . . .
  941          * If send window is too small, there is data to transmit, and no
  942          * retransmit or persist is pending, then go to persist state.
  943          * If nothing happens soon, send when timer expires:
  944          * if window is nonzero, transmit what we can,
  945          * otherwise send a probe.
  946          */
  947         if (so->so_snd.sb_cc && cb->s_timer[SPXT_REXMT] == 0 &&
  948                 cb->s_timer[SPXT_PERSIST] == 0) {
  949                         cb->s_rxtshift = 0;
  950                         spx_setpersist(cb);
  951         }
  952         /*
  953          * No reason to send a packet, just return.
  954          */
  955         cb->s_outx = 1;
  956         return (0);
  957 
  958 send:
  959         /*
  960          * Find requested packet.
  961          */
  962         si = 0;
  963         if (len > 0) {
  964                 cb->s_want = cb->s_snxt;
  965                 for (m = sb->sb_mb; m != NULL; m = m->m_act) {
  966                         si = mtod(m, struct spx *);
  967                         if (SSEQ_LEQ(cb->s_snxt, si->si_seq))
  968                                 break;
  969                 }
  970         found:
  971                 if (si != NULL) {
  972                         if (si->si_seq == cb->s_snxt)
  973                                         cb->s_snxt++;
  974                                 else
  975                                         spxstat.spxs_sndvoid++, si = 0;
  976                 }
  977         }
  978         /*
  979          * update window
  980          */
  981         if (rcv_win < 0)
  982                 rcv_win = 0;
  983         alo = cb->s_ack - 1 + (rcv_win / ((short)cb->s_mtu));
  984         if (SSEQ_LT(alo, cb->s_alo))
  985                 alo = cb->s_alo;
  986 
  987         if (si != NULL) {
  988                 /*
  989                  * must make a copy of this packet for
  990                  * ipx_output to monkey with
  991                  */
  992                 m = m_copy(dtom(si), 0, (int)M_COPYALL);
  993                 if (m == NULL) {
  994                         return (ENOBUFS);
  995                 }
  996                 si = mtod(m, struct spx *);
  997                 if (SSEQ_LT(si->si_seq, cb->s_smax))
  998                         spxstat.spxs_sndrexmitpack++;
  999                 else
 1000                         spxstat.spxs_sndpack++;
 1001         } else if (cb->s_force || cb->s_flags & SF_ACKNOW) {
 1002                 /*
 1003                  * Must send an acknowledgement or a probe
 1004                  */
 1005                 if (cb->s_force)
 1006                         spxstat.spxs_sndprobe++;
 1007                 if (cb->s_flags & SF_ACKNOW)
 1008                         spxstat.spxs_sndacks++;
 1009                 m = m_gethdr(M_DONTWAIT, MT_HEADER);
 1010                 if (m == NULL)
 1011                         return (ENOBUFS);
 1012                 /*
 1013                  * Fill in mbuf with extended SP header
 1014                  * and addresses and length put into network format.
 1015                  */
 1016                 MH_ALIGN(m, sizeof(struct spx));
 1017                 m->m_len = sizeof(*si);
 1018                 m->m_pkthdr.len = sizeof(*si);
 1019                 si = mtod(m, struct spx *);
 1020                 si->si_i = *cb->s_ipx;
 1021                 si->si_s = cb->s_shdr;
 1022                 si->si_seq = cb->s_smax + 1;
 1023                 si->si_len = htons(sizeof(*si));
 1024                 si->si_cc |= SPX_SP;
 1025         } else {
 1026                 cb->s_outx = 3;
 1027                 if (so->so_options & SO_DEBUG || traceallspxs)
 1028                         spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
 1029                 return (0);
 1030         }
 1031         /*
 1032          * Stuff checksum and output datagram.
 1033          */
 1034         if ((si->si_cc & SPX_SP) == 0) {
 1035                 if (cb->s_force != (1 + SPXT_PERSIST) ||
 1036                     cb->s_timer[SPXT_PERSIST] == 0) {
 1037                         /*
 1038                          * If this is a new packet and we are not currently
 1039                          * timing anything, time this one.
 1040                          */
 1041                         if (SSEQ_LT(cb->s_smax, si->si_seq)) {
 1042                                 cb->s_smax = si->si_seq;
 1043                                 if (cb->s_rtt == 0) {
 1044                                         spxstat.spxs_segstimed++;
 1045                                         cb->s_rtseq = si->si_seq;
 1046                                         cb->s_rtt = 1;
 1047                                 }
 1048                         }
 1049                         /*
 1050                          * Set rexmt timer if not currently set,
 1051                          * Initial value for retransmit timer is smoothed
 1052                          * round-trip time + 2 * round-trip time variance.
 1053                          * Initialize shift counter which is used for backoff
 1054                          * of retransmit time.
 1055                          */
 1056                         if (cb->s_timer[SPXT_REXMT] == 0 &&
 1057                             cb->s_snxt != cb->s_rack) {
 1058                                 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
 1059                                 if (cb->s_timer[SPXT_PERSIST]) {
 1060                                         cb->s_timer[SPXT_PERSIST] = 0;
 1061                                         cb->s_rxtshift = 0;
 1062                                 }
 1063                         }
 1064                 } else if (SSEQ_LT(cb->s_smax, si->si_seq)) {
 1065                         cb->s_smax = si->si_seq;
 1066                 }
 1067         } else if (cb->s_state < TCPS_ESTABLISHED) {
 1068                 if (cb->s_rtt == 0)
 1069                         cb->s_rtt = 1; /* Time initial handshake */
 1070                 if (cb->s_timer[SPXT_REXMT] == 0)
 1071                         cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
 1072         }
 1073         {
 1074                 /*
 1075                  * Do not request acks when we ack their data packets or
 1076                  * when we do a gratuitous window update.
 1077                  */
 1078                 if (((si->si_cc & SPX_SP) == 0) || cb->s_force)
 1079                                 si->si_cc |= SPX_SA;
 1080                 si->si_seq = htons(si->si_seq);
 1081                 si->si_alo = htons(alo);
 1082                 si->si_ack = htons(cb->s_ack);
 1083 
 1084                 if (ipxcksum) {
 1085                         si->si_sum = ipx_cksum(m, ntohs(si->si_len));
 1086                 } else
 1087                         si->si_sum = 0xffff;
 1088 
 1089                 cb->s_outx = 4;
 1090                 if (so->so_options & SO_DEBUG || traceallspxs)
 1091                         spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
 1092 
 1093                 if (so->so_options & SO_DONTROUTE)
 1094                         error = ipx_outputfl(m, NULL, IPX_ROUTETOIF);
 1095                 else
 1096                         error = ipx_outputfl(m, &cb->s_ipxpcb->ipxp_route, 0);
 1097         }
 1098         if (error) {
 1099                 return (error);
 1100         }
 1101         spxstat.spxs_sndtotal++;
 1102         /*
 1103          * Data sent (as far as we can tell).
 1104          * If this advertises a larger window than any other segment,
 1105          * then remember the size of the advertized window.
 1106          * Any pending ACK has now been sent.
 1107          */
 1108         cb->s_force = 0;
 1109         cb->s_flags &= ~(SF_ACKNOW|SF_DELACK);
 1110         if (SSEQ_GT(alo, cb->s_alo))
 1111                 cb->s_alo = alo;
 1112         if (sendalot)
 1113                 goto again;
 1114         cb->s_outx = 5;
 1115         return (0);
 1116 }
 1117 
 1118 static int spx_do_persist_panics = 0;
 1119 
 1120 static void
 1121 spx_setpersist(cb)
 1122         register struct spxpcb *cb;
 1123 {
 1124         register int t = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
 1125 
 1126         IPX_LOCK_ASSERT(cb->s_ipxpcb);
 1127 
 1128         if (cb->s_timer[SPXT_REXMT] && spx_do_persist_panics)
 1129                 panic("spx_output REXMT");
 1130         /*
 1131          * Start/restart persistance timer.
 1132          */
 1133         SPXT_RANGESET(cb->s_timer[SPXT_PERSIST],
 1134             t*spx_backoff[cb->s_rxtshift],
 1135             SPXTV_PERSMIN, SPXTV_PERSMAX);
 1136         if (cb->s_rxtshift < SPX_MAXRXTSHIFT)
 1137                 cb->s_rxtshift++;
 1138 }
 1139 
 1140 int
 1141 spx_ctloutput(so, sopt)
 1142         struct socket *so;
 1143         struct sockopt *sopt;
 1144 {
 1145         struct ipxpcb *ipxp = sotoipxpcb(so);
 1146         register struct spxpcb *cb;
 1147         int mask, error;
 1148         short soptval;
 1149         u_short usoptval;
 1150         int optval;
 1151 
 1152         error = 0;
 1153 
 1154         if (sopt->sopt_level != IPXPROTO_SPX) {
 1155                 /* This will have to be changed when we do more general
 1156                    stacking of protocols */
 1157                 return (ipx_ctloutput(so, sopt));
 1158         }
 1159         if (ipxp == NULL)
 1160                 return (EINVAL);
 1161         else
 1162                 cb = ipxtospxpcb(ipxp);
 1163 
 1164         switch (sopt->sopt_dir) {
 1165         case SOPT_GET:
 1166                 switch (sopt->sopt_name) {
 1167                 case SO_HEADERS_ON_INPUT:
 1168                         mask = SF_HI;
 1169                         goto get_flags;
 1170 
 1171                 case SO_HEADERS_ON_OUTPUT:
 1172                         mask = SF_HO;
 1173                 get_flags:
 1174                         /* Unlocked read. */
 1175                         soptval = cb->s_flags & mask;
 1176                         error = sooptcopyout(sopt, &soptval, sizeof soptval);
 1177                         break;
 1178 
 1179                 case SO_MTU:
 1180                         /* Unlocked read. */
 1181                         usoptval = cb->s_mtu;
 1182                         error = sooptcopyout(sopt, &usoptval, sizeof usoptval);
 1183                         break;
 1184 
 1185                 case SO_LAST_HEADER:
 1186                         /* Unlocked read. */
 1187                         error = sooptcopyout(sopt, &cb->s_rhdr,
 1188                                              sizeof cb->s_rhdr);
 1189                         break;
 1190 
 1191                 case SO_DEFAULT_HEADERS:
 1192                         /* Unlocked read. */
 1193                         error = sooptcopyout(sopt, &cb->s_shdr,
 1194                                              sizeof cb->s_shdr);
 1195                         break;
 1196 
 1197                 default:
 1198                         error = ENOPROTOOPT;
 1199                 }
 1200                 break;
 1201 
 1202         case SOPT_SET:
 1203                 switch (sopt->sopt_name) {
 1204                         /* XXX why are these shorts on get and ints on set?
 1205                            that doesn't make any sense... */
 1206                 case SO_HEADERS_ON_INPUT:
 1207                         mask = SF_HI;
 1208                         goto set_head;
 1209 
 1210                 case SO_HEADERS_ON_OUTPUT:
 1211                         mask = SF_HO;
 1212                 set_head:
 1213                         error = sooptcopyin(sopt, &optval, sizeof optval,
 1214                                             sizeof optval);
 1215                         if (error)
 1216                                 break;
 1217 
 1218                         IPX_LOCK(ipxp);
 1219                         if (cb->s_flags & SF_PI) {
 1220                                 if (optval)
 1221                                         cb->s_flags |= mask;
 1222                                 else
 1223                                         cb->s_flags &= ~mask;
 1224                         } else error = EINVAL;
 1225                         IPX_UNLOCK(ipxp);
 1226                         break;
 1227 
 1228                 case SO_MTU:
 1229                         error = sooptcopyin(sopt, &usoptval, sizeof usoptval,
 1230                                             sizeof usoptval);
 1231                         if (error)
 1232                                 break;
 1233                         /* Unlocked write. */
 1234                         cb->s_mtu = usoptval;
 1235                         break;
 1236 
 1237 #ifdef SF_NEWCALL
 1238                 case SO_NEWCALL:
 1239                         error = sooptcopyin(sopt, &optval, sizeof optval,
 1240                                             sizeof optval);
 1241                         if (error)
 1242                                 break;
 1243                         IPX_LOCK(ipxp);
 1244                         if (optval) {
 1245                                 cb->s_flags2 |= SF_NEWCALL;
 1246                                 spx_newchecks[5]++;
 1247                         } else {
 1248                                 cb->s_flags2 &= ~SF_NEWCALL;
 1249                                 spx_newchecks[6]++;
 1250                         }
 1251                         IPX_UNLOCK(ipxp);
 1252                         break;
 1253 #endif
 1254 
 1255                 case SO_DEFAULT_HEADERS:
 1256                         {
 1257                                 struct spxhdr sp;
 1258 
 1259                                 error = sooptcopyin(sopt, &sp, sizeof sp,
 1260                                                     sizeof sp);
 1261                                 if (error)
 1262                                         break;
 1263                                 IPX_LOCK(ipxp);
 1264                                 cb->s_dt = sp.spx_dt;
 1265                                 cb->s_cc = sp.spx_cc & SPX_EM;
 1266                                 IPX_UNLOCK(ipxp);
 1267                         }
 1268                         break;
 1269 
 1270                 default:
 1271                         error = ENOPROTOOPT;
 1272                 }
 1273                 break;
 1274         }
 1275         return (error);
 1276 }
 1277 
 1278 static int
 1279 spx_usr_abort(so)
 1280         struct socket *so;
 1281 {
 1282         struct ipxpcb *ipxp;
 1283         struct spxpcb *cb;
 1284 
 1285         ipxp = sotoipxpcb(so);
 1286         cb = ipxtospxpcb(ipxp);
 1287 
 1288         IPX_LIST_LOCK();
 1289         IPX_LOCK(ipxp);
 1290         spx_drop(cb, ECONNABORTED);
 1291         IPX_LIST_UNLOCK();
 1292         return (0);
 1293 }
 1294 
 1295 /*
 1296  * Accept a connection.  Essentially all the work is
 1297  * done at higher levels; just return the address
 1298  * of the peer, storing through addr.
 1299  */
 1300 static int
 1301 spx_accept(so, nam)
 1302         struct socket *so;
 1303         struct sockaddr **nam;
 1304 {
 1305         struct ipxpcb *ipxp;
 1306         struct sockaddr_ipx *sipx, ssipx;
 1307 
 1308         ipxp = sotoipxpcb(so);
 1309         sipx = &ssipx;
 1310         bzero(sipx, sizeof *sipx);
 1311         sipx->sipx_len = sizeof *sipx;
 1312         sipx->sipx_family = AF_IPX;
 1313         IPX_LOCK(ipxp);
 1314         sipx->sipx_addr = ipxp->ipxp_faddr;
 1315         IPX_UNLOCK(ipxp);
 1316         *nam = sodupsockaddr((struct sockaddr *)sipx, M_WAITOK);
 1317         return (0);
 1318 }
 1319 
 1320 static int
 1321 spx_attach(so, proto, td)
 1322         struct socket *so;
 1323         int proto;
 1324         struct thread *td;
 1325 {
 1326         struct ipxpcb *ipxp;
 1327         struct spxpcb *cb;
 1328         struct mbuf *mm;
 1329         struct sockbuf *sb;
 1330         int error;
 1331 
 1332         ipxp = sotoipxpcb(so);
 1333         cb = ipxtospxpcb(ipxp);
 1334 
 1335         if (ipxp != NULL)
 1336                 return (EISCONN);
 1337         IPX_LIST_LOCK();
 1338         error = ipx_pcballoc(so, &ipxpcb_list, td);
 1339         if (error)
 1340                 goto spx_attach_end;
 1341         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
 1342                 error = soreserve(so, (u_long) 3072, (u_long) 3072);
 1343                 if (error)
 1344                         goto spx_attach_end;
 1345         }
 1346         ipxp = sotoipxpcb(so);
 1347 
 1348         MALLOC(cb, struct spxpcb *, sizeof *cb, M_PCB, M_NOWAIT | M_ZERO);
 1349 
 1350         if (cb == NULL) {
 1351                 error = ENOBUFS;
 1352                 goto spx_attach_end;
 1353         }
 1354         sb = &so->so_snd;
 1355 
 1356         mm = m_getclr(M_DONTWAIT, MT_HEADER);
 1357         if (mm == NULL) {
 1358                 FREE(cb, M_PCB);
 1359                 error = ENOBUFS;
 1360                 goto spx_attach_end;
 1361         }
 1362         cb->s_ipx = mtod(mm, struct ipx *);
 1363         cb->s_state = TCPS_LISTEN;
 1364         cb->s_smax = -1;
 1365         cb->s_swl1 = -1;
 1366         cb->s_q.si_next = cb->s_q.si_prev = &cb->s_q;
 1367         cb->s_ipxpcb = ipxp;
 1368         cb->s_mtu = 576 - sizeof(struct spx);
 1369         cb->s_cwnd = sbspace(sb) * CUNIT / cb->s_mtu;
 1370         cb->s_ssthresh = cb->s_cwnd;
 1371         cb->s_cwmx = sbspace(sb) * CUNIT / (2 * sizeof(struct spx));
 1372         /* Above is recomputed when connecting to account
 1373            for changed buffering or mtu's */
 1374         cb->s_rtt = SPXTV_SRTTBASE;
 1375         cb->s_rttvar = SPXTV_SRTTDFLT << 2;
 1376         SPXT_RANGESET(cb->s_rxtcur,
 1377             ((SPXTV_SRTTBASE >> 2) + (SPXTV_SRTTDFLT << 2)) >> 1,
 1378             SPXTV_MIN, SPXTV_REXMTMAX);
 1379         ipxp->ipxp_pcb = (caddr_t)cb;
 1380 spx_attach_end:
 1381         IPX_LIST_UNLOCK();
 1382         return (error);
 1383 }
 1384 
 1385 static int
 1386 spx_bind(so, nam, td)
 1387         struct socket *so;
 1388         struct sockaddr *nam;
 1389         struct thread *td;
 1390 {
 1391         struct ipxpcb *ipxp;
 1392         int error;
 1393 
 1394         ipxp = sotoipxpcb(so);
 1395 
 1396         IPX_LIST_LOCK();
 1397         IPX_LOCK(ipxp);
 1398         error = ipx_pcbbind(ipxp, nam, td);
 1399         IPX_UNLOCK(ipxp);
 1400         IPX_LIST_UNLOCK();
 1401         return (error);
 1402 }
 1403 
 1404 /*
 1405  * Initiate connection to peer.
 1406  * Enter SYN_SENT state, and mark socket as connecting.
 1407  * Start keep-alive timer, setup prototype header,
 1408  * Send initial system packet requesting connection.
 1409  */
 1410 static int
 1411 spx_connect(so, nam, td)
 1412         struct socket *so;
 1413         struct sockaddr *nam;
 1414         struct thread *td;
 1415 {
 1416         struct ipxpcb *ipxp;
 1417         struct spxpcb *cb;
 1418         int error;
 1419 
 1420         ipxp = sotoipxpcb(so);
 1421         cb = ipxtospxpcb(ipxp);
 1422 
 1423         IPX_LIST_LOCK();
 1424         IPX_LOCK(ipxp);
 1425         if (ipxp->ipxp_lport == 0) {
 1426                 error = ipx_pcbbind(ipxp, NULL, td);
 1427                 if (error)
 1428                         goto spx_connect_end;
 1429         }
 1430         error = ipx_pcbconnect(ipxp, nam, td);
 1431         if (error)
 1432                 goto spx_connect_end;
 1433         soisconnecting(so);
 1434         spxstat.spxs_connattempt++;
 1435         cb->s_state = TCPS_SYN_SENT;
 1436         cb->s_did = 0;
 1437         spx_template(cb);
 1438         cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
 1439         cb->s_force = 1 + SPXTV_KEEP;
 1440         /*
 1441          * Other party is required to respond to
 1442          * the port I send from, but he is not
 1443          * required to answer from where I am sending to,
 1444          * so allow wildcarding.
 1445          * original port I am sending to is still saved in
 1446          * cb->s_dport.
 1447          */
 1448         ipxp->ipxp_fport = 0;
 1449         error = spx_output(cb, NULL);
 1450 spx_connect_end:
 1451         IPX_UNLOCK(ipxp);
 1452         IPX_LIST_UNLOCK();
 1453         return (error);
 1454 }
 1455 
 1456 static int
 1457 spx_detach(so)
 1458         struct socket *so;
 1459 {
 1460         struct ipxpcb *ipxp;
 1461         struct spxpcb *cb;
 1462 
 1463         ipxp = sotoipxpcb(so);
 1464         cb = ipxtospxpcb(ipxp);
 1465 
 1466         if (ipxp == NULL)
 1467                 return (ENOTCONN);
 1468         IPX_LIST_LOCK();
 1469         IPX_LOCK(ipxp);
 1470         if (cb->s_state > TCPS_LISTEN)
 1471                 spx_disconnect(cb);
 1472         else
 1473                 spx_close(cb);
 1474         IPX_LIST_UNLOCK();
 1475         return (0);
 1476 }
 1477 
 1478 /*
 1479  * We may decide later to implement connection closing
 1480  * handshaking at the spx level optionally.
 1481  * here is the hook to do it:
 1482  */
 1483 static int
 1484 spx_usr_disconnect(so)
 1485         struct socket *so;
 1486 {
 1487         struct ipxpcb *ipxp;
 1488         struct spxpcb *cb;
 1489 
 1490         ipxp = sotoipxpcb(so);
 1491         cb = ipxtospxpcb(ipxp);
 1492 
 1493         IPX_LIST_LOCK();
 1494         IPX_LOCK(ipxp);
 1495         spx_disconnect(cb);
 1496         IPX_LIST_UNLOCK();
 1497         return (0);
 1498 }
 1499 
 1500 static int
 1501 spx_listen(so, td)
 1502         struct socket *so;
 1503         struct thread *td;
 1504 {
 1505         int error;
 1506         struct ipxpcb *ipxp;
 1507         struct spxpcb *cb;
 1508 
 1509         error = 0;
 1510         ipxp = sotoipxpcb(so);
 1511         cb = ipxtospxpcb(ipxp);
 1512 
 1513         IPX_LIST_LOCK();
 1514         IPX_LOCK(ipxp);
 1515         SOCK_LOCK(so);
 1516         error = solisten_proto_check(so);
 1517         if (error == 0 && ipxp->ipxp_lport == 0)
 1518                 error = ipx_pcbbind(ipxp, NULL, td);
 1519         if (error == 0) {
 1520                 cb->s_state = TCPS_LISTEN;
 1521                 solisten_proto(so);
 1522         }
 1523         SOCK_UNLOCK(so);
 1524         IPX_UNLOCK(ipxp);
 1525         IPX_LIST_UNLOCK();
 1526         return (error);
 1527 }
 1528 
 1529 /*
 1530  * After a receive, possibly send acknowledgment
 1531  * updating allocation.
 1532  */
 1533 static int
 1534 spx_rcvd(so, flags)
 1535         struct socket *so;
 1536         int flags;
 1537 {
 1538         struct ipxpcb *ipxp;
 1539         struct spxpcb *cb;
 1540 
 1541         ipxp = sotoipxpcb(so);
 1542         cb = ipxtospxpcb(ipxp);
 1543 
 1544         IPX_LOCK(ipxp);
 1545         cb->s_flags |= SF_RVD;
 1546         spx_output(cb, NULL);
 1547         cb->s_flags &= ~SF_RVD;
 1548         IPX_UNLOCK(ipxp);
 1549         return (0);
 1550 }
 1551 
 1552 static int
 1553 spx_rcvoob(so, m, flags)
 1554         struct socket *so;
 1555         struct mbuf *m;
 1556         int flags;
 1557 {
 1558         struct ipxpcb *ipxp;
 1559         struct spxpcb *cb;
 1560 
 1561         ipxp = sotoipxpcb(so);
 1562         cb = ipxtospxpcb(ipxp);
 1563 
 1564         SOCKBUF_LOCK(&so->so_rcv);
 1565         if ((cb->s_oobflags & SF_IOOB) || so->so_oobmark ||
 1566             (so->so_rcv.sb_state & SBS_RCVATMARK)) {
 1567                 SOCKBUF_UNLOCK(&so->so_rcv);
 1568                 m->m_len = 1;
 1569                 /* Unlocked read. */
 1570                 *mtod(m, caddr_t) = cb->s_iobc;
 1571                 return (0);
 1572         }
 1573         SOCKBUF_UNLOCK(&so->so_rcv);
 1574         return (EINVAL);
 1575 }
 1576 
 1577 static int
 1578 spx_send(so, flags, m, addr, controlp, td)
 1579         struct socket *so;
 1580         int flags;
 1581         struct mbuf *m;
 1582         struct sockaddr *addr;
 1583         struct mbuf *controlp;
 1584         struct thread *td;
 1585 {
 1586         int error;
 1587         struct ipxpcb *ipxp;
 1588         struct spxpcb *cb;
 1589 
 1590         error = 0;
 1591         ipxp = sotoipxpcb(so);
 1592         cb = ipxtospxpcb(ipxp);
 1593 
 1594         IPX_LOCK(ipxp);
 1595         if (flags & PRUS_OOB) {
 1596                 if (sbspace(&so->so_snd) < -512) {
 1597                         error = ENOBUFS;
 1598                         goto spx_send_end;
 1599                 }
 1600                 cb->s_oobflags |= SF_SOOB;
 1601         }
 1602         if (controlp != NULL) {
 1603                 u_short *p = mtod(controlp, u_short *);
 1604                 spx_newchecks[2]++;
 1605                 if ((p[0] == 5) && (p[1] == 1)) { /* XXXX, for testing */
 1606                         cb->s_shdr.spx_dt = *(u_char *)(&p[2]);
 1607                         spx_newchecks[3]++;
 1608                 }
 1609                 m_freem(controlp);
 1610         }
 1611         controlp = NULL;
 1612         error = spx_output(cb, m);
 1613         m = NULL;
 1614 spx_send_end:
 1615         IPX_UNLOCK(ipxp);
 1616         if (controlp != NULL)
 1617                 m_freem(controlp);
 1618         if (m != NULL)
 1619                 m_freem(m);
 1620         return (error);
 1621 }
 1622 
 1623 static int
 1624 spx_shutdown(so)
 1625         struct socket *so;
 1626 {
 1627         struct ipxpcb *ipxp;
 1628         struct spxpcb *cb;
 1629 
 1630         ipxp = sotoipxpcb(so);
 1631         cb = ipxtospxpcb(ipxp);
 1632 
 1633         socantsendmore(so);
 1634         IPX_LIST_LOCK();
 1635         IPX_LOCK(ipxp);
 1636         spx_usrclosed(cb);
 1637         IPX_LIST_UNLOCK();
 1638         return (0);
 1639 }
 1640 
 1641 static int
 1642 spx_sp_attach(so, proto, td)
 1643         struct socket *so;
 1644         int proto;
 1645         struct thread *td;
 1646 {
 1647         int error;
 1648         struct ipxpcb *ipxp;
 1649 
 1650         error = spx_attach(so, proto, td);
 1651         if (error == 0) {
 1652                 ipxp = sotoipxpcb(so);
 1653                 ((struct spxpcb *)ipxp->ipxp_pcb)->s_flags |=
 1654                                         (SF_HI | SF_HO | SF_PI);
 1655         }
 1656         return (error);
 1657 }
 1658 
 1659 /*
 1660  * Create template to be used to send spx packets on a connection.
 1661  * Called after host entry created, fills
 1662  * in a skeletal spx header (choosing connection id),
 1663  * minimizing the amount of work necessary when the connection is used.
 1664  */
 1665 static void
 1666 spx_template(cb)
 1667         register struct spxpcb *cb;
 1668 {
 1669         register struct ipxpcb *ipxp = cb->s_ipxpcb;
 1670         register struct ipx *ipx = cb->s_ipx;
 1671         register struct sockbuf *sb = &(ipxp->ipxp_socket->so_snd);
 1672 
 1673         IPX_LOCK_ASSERT(ipxp);
 1674 
 1675         ipx->ipx_pt = IPXPROTO_SPX;
 1676         ipx->ipx_sna = ipxp->ipxp_laddr;
 1677         ipx->ipx_dna = ipxp->ipxp_faddr;
 1678         cb->s_sid = htons(spx_iss);
 1679         spx_iss += SPX_ISSINCR/2;
 1680         cb->s_alo = 1;
 1681         cb->s_cwnd = (sbspace(sb) * CUNIT) / cb->s_mtu;
 1682         cb->s_ssthresh = cb->s_cwnd; /* Try to expand fast to full complement
 1683                                         of large packets */
 1684         cb->s_cwmx = (sbspace(sb) * CUNIT) / (2 * sizeof(struct spx));
 1685         cb->s_cwmx = max(cb->s_cwmx, cb->s_cwnd);
 1686                 /* But allow for lots of little packets as well */
 1687 }
 1688 
 1689 /*
 1690  * Close a SPIP control block:
 1691  *      discard spx control block itself
 1692  *      discard ipx protocol control block
 1693  *      wake up any sleepers
 1694  * cb will always be invalid after this call.
 1695  */
 1696 void
 1697 spx_close(cb)
 1698         register struct spxpcb *cb;
 1699 {
 1700         register struct spx_q *s;
 1701         struct ipxpcb *ipxp = cb->s_ipxpcb;
 1702         struct socket *so = ipxp->ipxp_socket;
 1703         register struct mbuf *m;
 1704 
 1705         IPX_LIST_LOCK_ASSERT();
 1706         IPX_LOCK_ASSERT(ipxp);
 1707 
 1708         s = cb->s_q.si_next;
 1709         while (s != &(cb->s_q)) {
 1710                 s = s->si_next;
 1711                 m = dtom(s->si_prev);
 1712                 remque(s->si_prev);
 1713                 m_freem(m);
 1714         }
 1715         m_free(dtom(cb->s_ipx));
 1716         FREE(cb, M_PCB);
 1717         ipxp->ipxp_pcb = NULL;
 1718         soisdisconnected(so);
 1719         ipx_pcbdetach(ipxp);
 1720         spxstat.spxs_closed++;
 1721 }
 1722 
 1723 /*
 1724  *      Someday we may do level 3 handshaking
 1725  *      to close a connection or send a xerox style error.
 1726  *      For now, just close.
 1727  * cb will always be invalid after this call.
 1728  */
 1729 static void
 1730 spx_usrclosed(cb)
 1731         register struct spxpcb *cb;
 1732 {
 1733 
 1734         IPX_LIST_LOCK_ASSERT();
 1735         IPX_LOCK_ASSERT(cb->s_ipxpcb);
 1736 
 1737         spx_close(cb);
 1738 }
 1739 
 1740 /*
 1741  * cb will always be invalid after this call.
 1742  */
 1743 static void
 1744 spx_disconnect(cb)
 1745         register struct spxpcb *cb;
 1746 {
 1747 
 1748         IPX_LIST_LOCK_ASSERT();
 1749         IPX_LOCK_ASSERT(cb->s_ipxpcb);
 1750 
 1751         spx_close(cb);
 1752 }
 1753 
 1754 /*
 1755  * Drop connection, reporting
 1756  * the specified error.
 1757  * cb will always be invalid after this call.
 1758  */
 1759 static void
 1760 spx_drop(cb, errno)
 1761         register struct spxpcb *cb;
 1762         int errno;
 1763 {
 1764         struct socket *so = cb->s_ipxpcb->ipxp_socket;
 1765 
 1766         IPX_LIST_LOCK_ASSERT();
 1767         IPX_LOCK_ASSERT(cb->s_ipxpcb);
 1768 
 1769         /*
 1770          * someday, in the xerox world
 1771          * we will generate error protocol packets
 1772          * announcing that the socket has gone away.
 1773          */
 1774         if (TCPS_HAVERCVDSYN(cb->s_state)) {
 1775                 spxstat.spxs_drops++;
 1776                 cb->s_state = TCPS_CLOSED;
 1777                 /*tcp_output(cb);*/
 1778         } else
 1779                 spxstat.spxs_conndrops++;
 1780         so->so_error = errno;
 1781         spx_close(cb);
 1782 }
 1783 
 1784 /*
 1785  * Fast timeout routine for processing delayed acks
 1786  */
 1787 void
 1788 spx_fasttimo()
 1789 {
 1790         struct ipxpcb *ipxp;
 1791         struct spxpcb *cb;
 1792 
 1793         IPX_LIST_LOCK();
 1794         LIST_FOREACH(ipxp, &ipxpcb_list, ipxp_list) {
 1795                 IPX_LOCK(ipxp);
 1796                 if ((cb = (struct spxpcb *)ipxp->ipxp_pcb) != NULL &&
 1797                     (cb->s_flags & SF_DELACK)) {
 1798                         cb->s_flags &= ~SF_DELACK;
 1799                         cb->s_flags |= SF_ACKNOW;
 1800                         spxstat.spxs_delack++;
 1801                         spx_output(cb, NULL);
 1802                 }
 1803                 IPX_UNLOCK(ipxp);
 1804         }
 1805         IPX_LIST_UNLOCK();
 1806 }
 1807 
 1808 /*
 1809  * spx protocol timeout routine called every 500 ms.
 1810  * Updates the timers in all active pcb's and
 1811  * causes finite state machine actions if timers expire.
 1812  */
 1813 void
 1814 spx_slowtimo()
 1815 {
 1816         struct ipxpcb *ip, *ip_temp;
 1817         struct spxpcb *cb;
 1818         int i;
 1819 
 1820         /*
 1821          * Search through tcb's and update active timers.  Note that timers
 1822          * may free the ipxpcb, so be sure to handle that case.
 1823          *
 1824          * spx_timers() may remove an ipxpcb entry, so we have to be ready to
 1825          * continue despite that.  The logic here is a bit obfuscated.
 1826          */
 1827         IPX_LIST_LOCK();
 1828         LIST_FOREACH_SAFE(ip, &ipxpcb_list, ipxp_list, ip_temp) {
 1829                 cb = ipxtospxpcb(ip);
 1830                 if (cb == NULL)
 1831                         continue;
 1832                 IPX_LOCK(cb->s_ipxpcb);
 1833                 for (i = 0; i < SPXT_NTIMERS; i++) {
 1834                         if (cb->s_timer[i] && --cb->s_timer[i] == 0) {
 1835                                 /*
 1836                                  * spx_timers() returns (NULL) if it free'd
 1837                                  * the pcb.
 1838                                  */
 1839                                 cb = spx_timers(cb, i);
 1840                                 if (cb == NULL)
 1841                                         break;
 1842                         }
 1843                 }
 1844                 if (cb != NULL) {
 1845                         cb->s_idle++;
 1846                         if (cb->s_rtt)
 1847                                 cb->s_rtt++;
 1848                         IPX_UNLOCK(cb->s_ipxpcb);
 1849                 }
 1850         }
 1851         spx_iss += SPX_ISSINCR/PR_SLOWHZ;               /* increment iss */
 1852         IPX_LIST_UNLOCK();
 1853 }
 1854 
 1855 /*
 1856  * SPX timer processing.
 1857  */
 1858 static struct spxpcb *
 1859 spx_timers(cb, timer)
 1860         register struct spxpcb *cb;
 1861         int timer;
 1862 {
 1863         long rexmt;
 1864         int win;
 1865 
 1866         IPX_LIST_LOCK_ASSERT();
 1867         IPX_LOCK_ASSERT(cb->s_ipxpcb);
 1868 
 1869         cb->s_force = 1 + timer;
 1870         switch (timer) {
 1871 
 1872         /*
 1873          * 2 MSL timeout in shutdown went off.  TCP deletes connection
 1874          * control block.
 1875          */
 1876         case SPXT_2MSL:
 1877                 printf("spx: SPXT_2MSL went off for no reason\n");
 1878                 cb->s_timer[timer] = 0;
 1879                 break;
 1880 
 1881         /*
 1882          * Retransmission timer went off.  Message has not
 1883          * been acked within retransmit interval.  Back off
 1884          * to a longer retransmit interval and retransmit one packet.
 1885          */
 1886         case SPXT_REXMT:
 1887                 if (++cb->s_rxtshift > SPX_MAXRXTSHIFT) {
 1888                         cb->s_rxtshift = SPX_MAXRXTSHIFT;
 1889                         spxstat.spxs_timeoutdrop++;
 1890                         spx_drop(cb, ETIMEDOUT);
 1891                         cb = NULL;
 1892                         break;
 1893                 }
 1894                 spxstat.spxs_rexmttimeo++;
 1895                 rexmt = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
 1896                 rexmt *= spx_backoff[cb->s_rxtshift];
 1897                 SPXT_RANGESET(cb->s_rxtcur, rexmt, SPXTV_MIN, SPXTV_REXMTMAX);
 1898                 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
 1899                 /*
 1900                  * If we have backed off fairly far, our srtt
 1901                  * estimate is probably bogus.  Clobber it
 1902                  * so we'll take the next rtt measurement as our srtt;
 1903                  * move the current srtt into rttvar to keep the current
 1904                  * retransmit times until then.
 1905                  */
 1906                 if (cb->s_rxtshift > SPX_MAXRXTSHIFT / 4 ) {
 1907                         cb->s_rttvar += (cb->s_srtt >> 2);
 1908                         cb->s_srtt = 0;
 1909                 }
 1910                 cb->s_snxt = cb->s_rack;
 1911                 /*
 1912                  * If timing a packet, stop the timer.
 1913                  */
 1914                 cb->s_rtt = 0;
 1915                 /*
 1916                  * See very long discussion in tcp_timer.c about congestion
 1917                  * window and sstrhesh
 1918                  */
 1919                 win = min(cb->s_swnd, (cb->s_cwnd/CUNIT)) / 2;
 1920                 if (win < 2)
 1921                         win = 2;
 1922                 cb->s_cwnd = CUNIT;
 1923                 cb->s_ssthresh = win * CUNIT;
 1924                 spx_output(cb, NULL);
 1925                 break;
 1926 
 1927         /*
 1928          * Persistance timer into zero window.
 1929          * Force a probe to be sent.
 1930          */
 1931         case SPXT_PERSIST:
 1932                 spxstat.spxs_persisttimeo++;
 1933                 spx_setpersist(cb);
 1934                 spx_output(cb, NULL);
 1935                 break;
 1936 
 1937         /*
 1938          * Keep-alive timer went off; send something
 1939          * or drop connection if idle for too long.
 1940          */
 1941         case SPXT_KEEP:
 1942                 spxstat.spxs_keeptimeo++;
 1943                 if (cb->s_state < TCPS_ESTABLISHED)
 1944                         goto dropit;
 1945                 if (cb->s_ipxpcb->ipxp_socket->so_options & SO_KEEPALIVE) {
 1946                         if (cb->s_idle >= SPXTV_MAXIDLE)
 1947                                 goto dropit;
 1948                         spxstat.spxs_keepprobe++;
 1949                         spx_output(cb, NULL);
 1950                 } else
 1951                         cb->s_idle = 0;
 1952                 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
 1953                 break;
 1954         dropit:
 1955                 spxstat.spxs_keepdrops++;
 1956                 spx_drop(cb, ETIMEDOUT);
 1957                 cb = NULL;
 1958                 break;
 1959         }
 1960         return (cb);
 1961 }

Cache object: 25f6e7d2928237eb472807641707a4cf


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