checkpoint almost working version of kernel TP0/X.25; TP4/Cons seems
[unix-history] / usr / src / sys / netiso / tp_subr2.c
index b4cccc7..864f474 100644 (file)
@@ -1,3 +1,12 @@
+/*-
+ * Copyright (c) 1991 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ *
+ *     @(#)tp_subr2.c  7.12 (Berkeley) %G%
+ */
+
 /***********************************************************
                Copyright IBM Corporation 1987
 
 /***********************************************************
                Copyright IBM Corporation 1987
 
@@ -29,26 +38,22 @@ SOFTWARE.
  *
  * $Header: tp_subr2.c,v 5.5 88/11/18 17:28:55 nhall Exp $
  * $Source: /usr/argo/sys/netiso/RCS/tp_subr2.c,v $
  *
  * $Header: tp_subr2.c,v 5.5 88/11/18 17:28:55 nhall Exp $
  * $Source: /usr/argo/sys/netiso/RCS/tp_subr2.c,v $
- *     @(#)tp_subr2.c  7.5 (Berkeley) %G%
  *
  * Some auxiliary routines:
  *
  * Some auxiliary routines:
- *             tp_protocol_error: required by xebec- called when a combo of state,
- *                     event, predicate isn't covered for by the transition file.
- *             tp_indicate: gives indications(signals) to the user process
- *             tp_getoptions: initializes variables that are affected by the options
- *          chosen.
+ *     tp_protocol_error: required by xebec- called when a combo of state,
+ *         event, predicate isn't covered for by the transition file.
+ *     tp_indicate: gives indications(signals) to the user process
+ *     tp_getoptions: initializes variables that are affected by the options
+ *         chosen.
  */
 
  */
 
-#ifndef lint
-static char *rcsid = "$Header: tp_subr2.c,v 5.5 88/11/18 17:28:55 nhall Exp $";
-#endif lint
-
 /* this def'n is to cause the expansion of this macro in the
  * routine tp_local_credit :
  */
 #define LOCAL_CREDIT_EXPAND
 
 #include "param.h"
 /* this def'n is to cause the expansion of this macro in the
  * routine tp_local_credit :
  */
 #define LOCAL_CREDIT_EXPAND
 
 #include "param.h"
+#include "systm.h"
 #include "mbuf.h"
 #include "socket.h"
 #include "socketvar.h"
 #include "mbuf.h"
 #include "socket.h"
 #include "socketvar.h"
@@ -75,6 +80,7 @@ static char *rcsid = "$Header: tp_subr2.c,v 5.5 88/11/18 17:28:55 nhall Exp $";
 #include "cons.h"
 
 #include "../net/if.h"
 #include "cons.h"
 
 #include "../net/if.h"
+#include "../net/if_types.h"
 #ifdef TRUE
 #undef FALSE
 #undef TRUE
 #ifdef TRUE
 #undef FALSE
 #undef TRUE
@@ -209,9 +215,27 @@ tp_indicate(ind, tpcb, error)
                tpcb->tp_lref);
        ENDDEBUG
 
                tpcb->tp_lref);
        ENDDEBUG
 
+       if (ind == ER_TPDU) {
+               register struct mbuf *m;
+               struct tp_disc_reason x;
+
+               if ((so->so_state & SS_CANTRCVMORE) == 0 &&
+                               (m = m_get(M_DONTWAIT, MT_OOBDATA)) != 0) {
+
+                       x.dr_hdr.cmsg_len = m->m_len = sizeof(x);
+                       x.dr_hdr.cmsg_level = SOL_TRANSPORT;
+                       x.dr_hdr.cmsg_type= TPOPT_DISC_REASON;
+                       x.dr_reason = error;
+                       *mtod(m, struct tp_disc_reason *) = x;
+                       sbappendrecord(&tpcb->tp_Xrcv, m);
+                       error = 0;
+               } else
+                       error = ECONNRESET;
+       }
        so->so_error = error;
 
        if (ind == T_DISCONNECT)  {
        so->so_error = error;
 
        if (ind == T_DISCONNECT)  {
+               so->so_error = ENOTCONN;
                if ( tpcb->tp_no_disc_indications )
                        return;
        }
                if ( tpcb->tp_no_disc_indications )
                        return;
        }
@@ -359,10 +383,17 @@ tp_netcmd( tpcb, cmd )
 
        case CONN_CLOSE:
        case CONN_REFUSE:
 
        case CONN_CLOSE:
        case CONN_REFUSE:
-               if (isop->isop_refcnt == 1)
+               if (isop->isop_refcnt == 1) {
+                       /* This is really superfluous, since it would happen
+                          anyway in iso_pcbdetach, although it is a courtesy
+                          to free up the x.25 channel before the refwait timer
+                          expires. */
+                       lcp->lcd_upper = 0;
+                       lcp->lcd_upnext = 0;
                        pk_disconnect(lcp);
                        pk_disconnect(lcp);
-               isop->isop_chan = 0;
-               isop->isop_refcnt = 0;
+                       isop->isop_chan = 0;
+                       isop->isop_refcnt = 0;
+               }
                break;
 
        default:
                break;
 
        default:
@@ -451,8 +482,8 @@ tp_route_to( m, tpcb, channel)
 {
        register struct sockaddr_iso *siso;     /* NOTE: this may be a sockaddr_in */
        extern struct tp_conn_param tp_conn_param[];
 {
        register struct sockaddr_iso *siso;     /* NOTE: this may be a sockaddr_in */
        extern struct tp_conn_param tp_conn_param[];
-       struct pklcd *lcp = (struct pklcd *)channel;
-       int error = 0;
+       int error = 0, save_netservice = tpcb->tp_netservice;
+       register struct rtentry *rt = 0;
 
        siso = mtod(m, struct sockaddr_iso *);
        IFTRACE(D_CONN)
 
        siso = mtod(m, struct sockaddr_iso *);
        IFTRACE(D_CONN)
@@ -467,39 +498,65 @@ tp_route_to( m, tpcb, channel)
                printf("m->mlen x%x, m->m_data:\n", m->m_len);
                dump_buf(mtod(m, caddr_t), m->m_len);
        ENDDEBUG
                printf("m->mlen x%x, m->m_data:\n", m->m_len);
                dump_buf(mtod(m, caddr_t), m->m_len);
        ENDDEBUG
-       if (siso->siso_family != tpcb->tp_domain) {
-               error = EAFNOSUPPORT;
-               goto done;
-       }
-       IFDEBUG(D_CONN)
-               printf("tp_route_to  calling nlp_pcbconn, netserv %d\n",
-                       tpcb->tp_netservice);
-       ENDDEBUG
+       if (channel) {
 #ifdef TPCONS
 #ifdef TPCONS
-       if (lcp) {
+               struct pklcd *lcp = (struct pklcd *)channel;
                struct isopcb *isop = (struct isopcb *)lcp->lcd_upnext,
                struct isopcb *isop = (struct isopcb *)lcp->lcd_upnext,
-                       *isop_new = (struct isopcb *)tpcb->tp_sock->so_pcb;
+                       *isop_new = (struct isopcb *)tpcb->tp_npcb;
+               /* The next 2 lines believe that you haven't
+                  set any network level options or done a pcbconnect
+                  and XXXXXXX'edly apply to both inpcb's and isopcb's */
                remque(isop_new);
                free(isop_new, M_PCB);
                remque(isop_new);
                free(isop_new, M_PCB);
-               tpcb->tp_sock->so_pcb = (caddr_t)isop;
-               if (isop->isop_refcnt == 0) {
-                       extern struct isopcb tp_isopcb;
-                       remque(isop);
-                       insque(isop, &tp_isopcb);
-                       isop->isop_head = &tp_isopcb;
+               tpcb->tp_npcb = (caddr_t)isop;
+               tpcb->tp_netservice = ISO_CONS;
+               tpcb->tp_nlproto = nl_protosw + ISO_CONS;
+               isop->isop_socket = tpcb->tp_sock;
+               if (isop->isop_refcnt == 0)
                        iso_putsufx(isop, tpcb->tp_lsuffix, tpcb->tp_lsuffixlen, TP_LOCAL);
                        iso_putsufx(isop, tpcb->tp_lsuffix, tpcb->tp_lsuffixlen, TP_LOCAL);
-               }
-               /* else there are already connections sharing this */
+               else
+                       /* there are already connections sharing this */;
                isop->isop_refcnt++;
                isop->isop_refcnt++;
-       } else
 #endif
 #endif
-               error = (tpcb->tp_nlproto->nlp_pcbconn)(tpcb->tp_sock->so_pcb, m);
+       } else {
+               switch (siso->siso_family) {
+               default:
+                       error = EAFNOSUPPORT;
+                       goto done;
+#ifdef ISO
+               case AF_ISO:
+                       tpcb->tp_netservice = ISO_CLNS;
+                       if (rt = rtalloc1((struct sockaddr *)siso, 0)) {
+                               rt->rt_refcnt--;
+                               if (rt->rt_flags & RTF_PROTO1)
+                                       tpcb->tp_netservice = ISO_CONS;
+                       }
+                       break;
+#endif
+#ifdef INET
+               case AF_INET:
+                       tpcb->tp_netservice = IN_CLNS;
+               }
+#endif
+               if (tpcb->tp_nlproto->nlp_afamily != siso->siso_family) {
+                       IFDEBUG(D_CONN)
+                               printf("tp_route_to( CHANGING nlproto old 0x%x new 0x%x)\n", 
+                                               save_netservice, tpcb->tp_netservice);
+                       ENDDEBUG
+                       if (error = tp_set_npcb(tpcb))
+                               goto done;
+               }
+               IFDEBUG(D_CONN)
+                       printf("tp_route_to  calling nlp_pcbconn, netserv %d\n",
+                               tpcb->tp_netservice);
+               ENDDEBUG
+               tpcb->tp_nlproto = nl_protosw + tpcb->tp_netservice;
+               error = (tpcb->tp_nlproto->nlp_pcbconn)(tpcb->tp_npcb, m);
+       }
        if( error )
                goto done;
 
        {
        if( error )
                goto done;
 
        {
-               register int save_netservice = tpcb->tp_netservice;
-
                switch(tpcb->tp_netservice) {
                case ISO_COSNS:
                case ISO_CLNS:
                switch(tpcb->tp_netservice) {
                case ISO_COSNS:
                case ISO_CLNS:
@@ -507,8 +564,7 @@ tp_route_to( m, tpcb, channel)
                         * can get long enough timers. sigh.
                        if( siso->siso_addr.osinet_idi[1] == (u_char)IDI_OSINET )
                         */
                         * can get long enough timers. sigh.
                        if( siso->siso_addr.osinet_idi[1] == (u_char)IDI_OSINET )
                         */
-#define        IDI_OSINET      0x0004  /* bcd of "0004" */     
-                       if( siso->siso_addr.isoa_genaddr[2] == (char)IDI_OSINET ) {
+                       if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_X25) {
                                if( tpcb->tp_dont_change_params == 0) {
                                        copyQOSparms( &tp_conn_param[ISO_COSNS],
                                                        &tpcb->_tp_param);
                                if( tpcb->tp_dont_change_params == 0) {
                                        copyQOSparms( &tp_conn_param[ISO_COSNS],
                                                        &tpcb->_tp_param);
@@ -519,7 +575,7 @@ tp_route_to( m, tpcb, channel)
                case IN_CLNS:
                        if (iso_localifa(siso))
                                tpcb->tp_flags |= TPF_PEER_ON_SAMENET;
                case IN_CLNS:
                        if (iso_localifa(siso))
                                tpcb->tp_flags |= TPF_PEER_ON_SAMENET;
-                       if( (tpcb->tp_class & TP_CLASS_4)==0 ) {
+                       if((tpcb->tp_class & TP_CLASS_4) == 0) {
                                error = EPROTOTYPE;
                                break;
                        } 
                                error = EPROTOTYPE;
                                break;
                        } 
@@ -544,14 +600,6 @@ tp_route_to( m, tpcb, channel)
                        }
                        tpcb->tp_rx_strat =  TPRX_USE_CW;
 
                        }
                        tpcb->tp_rx_strat =  TPRX_USE_CW;
 
-                       if( (tpcb->tp_nlproto != &nl_protosw[ISO_CONS]) ) {
-                               IFDEBUG(D_CONN)
-                                       printf(
-                                       "tp_route_to( CHANGING nlproto old 0x%x new 0x%x)\n", 
-                                                       tpcb->tp_nlproto , &nl_protosw[ISO_CONS]);
-                               ENDDEBUG
-                               tpcb->tp_nlproto = &nl_protosw[ISO_CONS];
-                       }
                        /* class 4 doesn't need to open a vc now - may use one already 
                         * opened or may open one only when it sends a pkt.
                         */
                        /* class 4 doesn't need to open a vc now - may use one already 
                         * opened or may open one only when it sends a pkt.
                         */
@@ -563,7 +611,6 @@ tp_route_to( m, tpcb, channel)
                        error = EPROTOTYPE;
                }
 
                        error = EPROTOTYPE;
                }
 
-               ASSERT( save_netservice == tpcb->tp_netservice);
        }
        if (error) {
                tp_netcmd( tpcb, CONN_CLOSE);
        }
        if (error) {
                tp_netcmd( tpcb, CONN_CLOSE);
@@ -587,6 +634,73 @@ done:
        return error;
 }
 
        return error;
 }
 
+
+/* class zero version */
+void
+tp0_stash( tpcb, e )
+       register struct tp_pcb          *tpcb;
+       register struct tp_event        *e;
+{
+#ifndef lint
+#define E e->ATTR(DT_TPDU)
+#else lint
+#define E e->ev_union.EV_DT_TPDU
+#endif lint
+
+       register struct sockbuf *sb = &tpcb->tp_sock->so_rcv;
+       register struct isopcb *isop = (struct isopcb *)tpcb->tp_npcb;
+
+       IFPERF(tpcb)
+               PStat(tpcb, Nb_from_ll) += E.e_datalen;
+               tpmeas(tpcb->tp_lref, TPtime_from_ll, &e->e_time,
+                               E.e_seq, PStat(tpcb, Nb_from_ll), E.e_datalen);
+       ENDPERF
+
+       IFDEBUG(D_STASH)
+               printf("stash EQ: seq 0x%x datalen 0x%x eot 0x%x", 
+               E.e_seq, E.e_datalen, E.e_eot);
+       ENDDEBUG
+
+       IFTRACE(D_STASH)
+               tptraceTPCB(TPPTmisc, "stash EQ: seq len eot", 
+               E.e_seq, E.e_datalen, E.e_eot, 0);
+       ENDTRACE
+
+       if ( E.e_eot ) {
+               register struct mbuf *n = E.e_data;
+               n->m_flags |= M_EOR;
+               n->m_act = MNULL; /* set on tp_input */
+       }
+       sbappend(sb, E.e_data);
+       IFDEBUG(D_STASH)
+               dump_mbuf(sb->sb_mb, "stash 0: so_rcv after appending");
+       ENDDEBUG
+       if (tpcb->tp_netservice != ISO_CONS)
+               printf("tp0_stash: tp running over something wierd\n");
+       else {
+               register struct pklcd *lcp = (struct pklcd *)isop->isop_chan;
+               pk_flowcontrol(lcp, sbspace(sb) <= 0, 1);
+       }
+} 
+
+void
+tp0_openflow(tpcb)
+register struct tp_pcb *tpcb;
+{
+       register struct isopcb *isop = (struct isopcb *)tpcb->tp_npcb;
+       if (tpcb->tp_netservice != ISO_CONS)
+               printf("tp0_openflow: tp running over something wierd\n");
+       else {
+               register struct pklcd *lcp = (struct pklcd *)isop->isop_chan;
+               if (lcp->lcd_rxrnr_condition)
+                       pk_flowcontrol(lcp, 0, 0);
+       }
+}
+#ifndef TPCONS
+static
+pk_flowcontrol() {}
+#endif
+
 #ifdef TP_PERF_MEAS
 /*
  * CALLED FROM:
 #ifdef TP_PERF_MEAS
 /*
  * CALLED FROM:
@@ -663,13 +777,13 @@ caddr_t   buf;
 int            len;
 {
        int             i,j;
 int            len;
 {
        int             i,j;
-
+#define Buf ((u_char *)buf)
        printf("Dump buf 0x%x len 0x%x\n", buf, len);
        for (i = 0; i < len; i += MAX_COLUMNS) {
                printf("+%d:\t", i);
                for (j = 0; j < MAX_COLUMNS; j++) {
                        if (i + j < len) {
        printf("Dump buf 0x%x len 0x%x\n", buf, len);
        for (i = 0; i < len; i += MAX_COLUMNS) {
                printf("+%d:\t", i);
                for (j = 0; j < MAX_COLUMNS; j++) {
                        if (i + j < len) {
-                               printf("%x/%d\t", buf[i+j]&0xff, buf[i+j]);
+                               printf("%x/%d\t", Buf[i+j], Buf[i+j]);
                        } else {
                                printf("        ");
                        }
                        } else {
                                printf("        ");
                        }
@@ -677,8 +791,8 @@ int         len;
 
                for (j = 0; j < MAX_COLUMNS; j++) {
                        if (i + j < len) {
 
                for (j = 0; j < MAX_COLUMNS; j++) {
                        if (i + j < len) {
-                               if (((buf[i+j]) > 31) && ((buf[i+j]) < 128))
-                                       printf("%c", buf[i+j]&0xff);
+                               if (((Buf[i+j]) > 31) && ((Buf[i+j]) < 128))
+                                       printf("%c", Buf[i+j]);
                                else
                                        printf(".");
                        }
                                else
                                        printf(".");
                        }