BSD 4_4 release
[unix-history] / usr / src / sys / netccitt / pk_input.c
index e5de291..1f8f0bc 100644 (file)
 /*
  * Copyright (c) University of British Columbia, 1984
 /*
  * Copyright (c) University of British Columbia, 1984
- * Copyright (c) 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (C) Computer Science Department IV, 
+ *              University of Erlangen-Nuremberg, Germany, 1992
+ * Copyright (c) 1991, 1992, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * This code is derived from software contributed to Berkeley by
- * the Laboratory for Computation Vision and the Computer Science Department
- * of the University of British Columbia.
+ * This code is derived from software contributed to Berkeley by the
+ * Laboratory for Computation Vision and the Computer Science Department
+ * of the the University of British Columbia and the Computer Science
+ * Department (IV) of the University of Erlangen-Nuremberg, Germany.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)pk_input.c  7.14 (Berkeley) %G%
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)pk_input.c  8.1 (Berkeley) 6/10/93
  */
 
  */
 
-#include "param.h"
-#include "systm.h"
-#include "mbuf.h"
-#include "socket.h"
-#include "protosw.h"
-#include "socketvar.h"
-#include "errno.h"
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/mbuf.h>
+#include <sys/socket.h>
+#include <sys/protosw.h>
+#include <sys/socketvar.h>
+#include <sys/errno.h>
+
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/if_llc.h>
+#include <net/route.h>
+
+#include <netccitt/dll.h>
+#include <netccitt/x25.h>
+#include <netccitt/pk.h>
+#include <netccitt/pk_var.h>
+#include <netccitt/llc_var.h>
+
+struct pkcb_q pkcb_q = {&pkcb_q, &pkcb_q};
 
 
-#include "../net/if.h"
+/*
+ * ccittintr() is the generic interrupt handler for HDLC, LLC2, and X.25. This
+ * allows to have kernel running X.25 but no HDLC or LLC2 or both (in case we
+ * employ boards that do all the stuff themselves, e.g. ADAX X.25 or TPS ISDN.)
+ */
+void
+ccittintr ()
+{
+       extern struct ifqueue pkintrq;
+       extern struct ifqueue hdintrq;
+       extern struct ifqueue llcintrq;
 
 
-#include "x25.h"
-#include "pk.h"
-#include "pk_var.h"
+#ifdef HDLC
+       if (hdintrq.ifq_len)
+               hdintr ();
+#endif
+#ifdef LLC
+       if (llcintrq.ifq_len)
+               llcintr ();
+#endif
+       if (pkintrq.ifq_len)
+               pkintr ();
+}
 
 struct pkcb *
 pk_newlink (ia, llnext)
 struct x25_ifaddr *ia;
 caddr_t llnext;
 {
 
 struct pkcb *
 pk_newlink (ia, llnext)
 struct x25_ifaddr *ia;
 caddr_t llnext;
 {
-       register struct x25config *xcp = &ia->ia_xc;
+       register struct x25config *xcp = &ia -> ia_xc;
        register struct pkcb *pkp;
        register struct pklcd *lcp;
        register struct protosw *pp;
        unsigned size;
 
        register struct pkcb *pkp;
        register struct pklcd *lcp;
        register struct protosw *pp;
        unsigned size;
 
-       pp = pffindproto (AF_CCITT, (int)xcp -> xc_lproto, 0);
+       pp = pffindproto (AF_CCITT, (int) xcp -> xc_lproto, 0);
        if (pp == 0 || pp -> pr_output == 0) {
                pk_message (0, xcp, "link level protosw error");
                return ((struct pkcb *)0);
        if (pp == 0 || pp -> pr_output == 0) {
                pk_message (0, xcp, "link level protosw error");
                return ((struct pkcb *)0);
@@ -46,17 +106,17 @@ caddr_t llnext;
         * Allocate a network control block structure
         */
        size = sizeof (struct pkcb);
         * Allocate a network control block structure
         */
        size = sizeof (struct pkcb);
-       pkp = (struct pkcb *)malloc(size, M_PCB, M_WAITOK);
+       pkp = (struct pkcb *) malloc (size, M_PCB, M_WAITOK);
        if (pkp == 0)
                return ((struct pkcb *)0);
        if (pkp == 0)
                return ((struct pkcb *)0);
-       bzero ((caddr_t)pkp, size);
+       bzero ((caddr_t) pkp, size);
        pkp -> pk_lloutput = pp -> pr_output;
        pkp -> pk_lloutput = pp -> pr_output;
+       pkp -> pk_llctlinput = (caddr_t (*)()) pp -> pr_ctlinput;
        pkp -> pk_xcp = xcp;
        pkp -> pk_ia = ia;
        pkp -> pk_state = DTE_WAITING;
        pkp -> pk_xcp = xcp;
        pkp -> pk_ia = ia;
        pkp -> pk_state = DTE_WAITING;
-       pkp -> pk_next = pkcbhead;
        pkp -> pk_llnext = llnext;
        pkp -> pk_llnext = llnext;
-       pkcbhead = pkp;
+       insque (pkp, &pkcb_q);
 
        /*
         * set defaults
 
        /*
         * set defaults
@@ -70,10 +130,72 @@ caddr_t llnext;
         * Allocate logical channel descriptor vector
         */
 
         * Allocate logical channel descriptor vector
         */
 
-       (void)pk_resize(pkp);
+       (void) pk_resize (pkp);
        return (pkp);
 }
 
        return (pkp);
 }
 
+
+pk_dellink (pkp)
+register struct pkcb *pkp;
+{
+       register int i;
+       register struct protosw *pp;
+       
+       /*
+        * Essentially we have the choice to
+        * (a) go ahead and let the route be deleted and
+        *     leave the pkcb associated with that route
+        *     as it is, i.e. the connections stay open
+        * (b) do a pk_disconnect() on all channels associated
+        *     with the route via the pkcb and then proceed.
+        *
+        * For the time being we stick with (b)
+        */
+       
+       for (i = 1; i < pkp -> pk_maxlcn; ++i)
+               if (pkp -> pk_chan[i])
+                       pk_disconnect (pkp -> pk_chan[i]);
+
+       /*
+        * Free the pkcb
+        */
+
+       /*
+        * First find the protoswitch to get hold of the link level
+        * protocol to be notified that the packet level entity is
+        * dissolving ...
+        */
+       pp = pffindproto (AF_CCITT, (int) pkp -> pk_xcp -> xc_lproto, 0);
+       if (pp == 0 || pp -> pr_output == 0) {
+               pk_message (0, pkp -> pk_xcp, "link level protosw error");
+               return (EPROTONOSUPPORT);
+       }
+
+       pkp -> pk_refcount--;
+       if (!pkp -> pk_refcount) {
+               struct dll_ctlinfo ctlinfo;
+
+               remque (pkp);
+               if (pkp -> pk_rt -> rt_llinfo == (caddr_t) pkp)
+                       pkp -> pk_rt -> rt_llinfo = (caddr_t) NULL;
+               
+               /*
+                * Tell the link level that the pkcb is dissolving
+                */
+               if (pp -> pr_ctlinput && pkp -> pk_llnext) {
+                       ctlinfo.dlcti_pcb = pkp -> pk_llnext;
+                       ctlinfo.dlcti_rt = pkp -> pk_rt;
+                       (pp -> pr_ctlinput)(PRC_DISCONNECT_REQUEST, 
+                                           pkp -> pk_xcp, &ctlinfo);
+               }
+               free ((caddr_t) pkp -> pk_chan, M_IFADDR);
+               free ((caddr_t) pkp, M_PCB);
+       }
+
+       return (0);
+}
+
+
 pk_resize (pkp)
 register struct pkcb *pkp;
 {
 pk_resize (pkp)
 register struct pkcb *pkp;
 {
@@ -83,7 +205,7 @@ register struct pkcb *pkp;
            (pkp -> pk_maxlcn != xcp -> xc_maxlcn)) {
                pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION);
                dev_lcp = pkp -> pk_chan[0];
            (pkp -> pk_maxlcn != xcp -> xc_maxlcn)) {
                pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION);
                dev_lcp = pkp -> pk_chan[0];
-               free ((caddr_t)pkp -> pk_chan, M_IFADDR);
+               free ((caddr_t) pkp -> pk_chan, M_IFADDR);
                pkp -> pk_chan = 0;
        }
        if (pkp -> pk_chan == 0) {
                pkp -> pk_chan = 0;
        }
        if (pkp -> pk_chan == 0) {
@@ -93,7 +215,7 @@ register struct pkcb *pkp;
                pkp -> pk_chan =
                        (struct pklcd **) malloc (size, M_IFADDR, M_WAITOK);
                if (pkp -> pk_chan) {
                pkp -> pk_chan =
                        (struct pklcd **) malloc (size, M_IFADDR, M_WAITOK);
                if (pkp -> pk_chan) {
-                       bzero ((caddr_t)pkp -> pk_chan, size);
+                       bzero ((caddr_t) pkp -> pk_chan, size);
                        /*
                         * Allocate a logical channel descriptor for lcn 0
                         */
                        /*
                         * Allocate a logical channel descriptor for lcn 0
                         */
@@ -116,11 +238,13 @@ register struct pkcb *pkp;
  *  This procedure is called by the link level whenever the link
  *  becomes operational, is reset, or when the link goes down. 
  */
  *  This procedure is called by the link level whenever the link
  *  becomes operational, is reset, or when the link goes down. 
  */
-
-pk_ctlinput (code, pkp)
-register struct pkcb *pkp;
+/*VARARGS*/
+caddr_t
+pk_ctlinput (code, src, addr)
+       struct sockaddr *src;
+       caddr_t addr;
 {
 {
-
+       register struct pkcb *pkp = (struct pkcb *) addr;
 
        switch (code) {
        case PRC_LINKUP: 
 
        switch (code) {
        case PRC_LINKUP: 
@@ -136,7 +260,26 @@ register struct pkcb *pkp;
        case PRC_LINKRESET: 
                pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION);
                break;
        case PRC_LINKRESET: 
                pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION);
                break;
-
+               
+       case PRC_CONNECT_INDICATION: {
+               struct rtentry *llrt;
+
+               if ((llrt = rtalloc1(src, 0)) == 0)
+                       return 0;
+               else llrt -> rt_refcnt--;
+               
+               pkp = (((struct npaidbentry *) llrt -> rt_llinfo) -> np_rt) ?
+                       (struct pkcb *)(((struct npaidbentry *) llrt -> rt_llinfo) -> np_rt -> rt_llinfo) : (struct pkcb *) 0;
+               if (pkp == (struct pkcb *) 0)
+                       return 0;
+               pkp -> pk_llnext = addr;
+
+               return ((caddr_t) pkp);
+       }
+       case PRC_DISCONNECT_INDICATION:
+               pk_restart (pkp, -1) ;  /* Clear all active circuits */
+               pkp -> pk_state = DTE_WAITING;
+               pkp -> pk_llnext = (caddr_t) 0;
        }
        return (0);
 }
        }
        return (0);
 }
@@ -158,13 +301,13 @@ pkintr ()
                splx (s);
                if (m == 0)
                        break;
                splx (s);
                if (m == 0)
                        break;
-               if (m->m_len < PKHEADERLN) {
+               if (m -> m_len < PKHEADERLN) {
                        printf ("pkintr: packet too short (len=%d)\n",
                        printf ("pkintr: packet too short (len=%d)\n",
-                               m->m_len);
+                               m -> m_len);
                        m_freem (m);
                        continue;
                }
                        m_freem (m);
                        continue;
                }
-               pk_input(m);
+               pk_input (m);
        }
 }
 struct mbuf *pk_bad_packet;
        }
 }
 struct mbuf *pk_bad_packet;
@@ -177,12 +320,17 @@ struct mbuf_cache pk_input_cache = {0 };
  *  demultiplexes based on the logical channel number.
  *
  *  We change the original conventions of the UBC code here --
  *  demultiplexes based on the logical channel number.
  *
  *  We change the original conventions of the UBC code here --
- *  since there may be multiple pkcb's for 802.2 class 2
- *  for a given interface, we must be informed which one it is;
- *  so we overwrite the pkthdr.rcvif; it can be recovered if necessary.
+ *  since there may be multiple pkcb's for a given interface
+ *  of type 802.2 class 2, we retrieve which one it is from
+ *  m_pkthdr.rcvif (which has been overwritten by lower layers);
+ *  That field is then restored for the benefit of upper layers which
+ *  may make use of it, such as CLNP.
  *
  */
 
  *
  */
 
+#define RESTART_DTE_ORIGINATED(xp) (((xp) -> packet_cause == X25_RESTART_DTE_ORIGINATED) || \
+                           ((xp) -> packet_cause >= X25_RESTART_DTE_ORIGINATED2))
+
 pk_input (m)
 register struct mbuf *m;
 {
 pk_input (m)
 register struct mbuf *m;
 {
@@ -193,10 +341,11 @@ register struct mbuf *m;
        int  ptype, lcn, lcdstate = LISTEN;
 
        if (pk_input_cache.mbc_size || pk_input_cache.mbc_oldsize)
        int  ptype, lcn, lcdstate = LISTEN;
 
        if (pk_input_cache.mbc_size || pk_input_cache.mbc_oldsize)
-               mbuf_cache(&pk_input_cache, m);
-       if ((m->m_flags & M_PKTHDR) == 0)
-               panic("pkintr");
-       if ((pkp = (struct pkcb *)m->m_pkthdr.rcvif) == 0)
+               mbuf_cache (&pk_input_cache, m);
+       if ((m -> m_flags & M_PKTHDR) == 0)
+               panic ("pkintr");
+
+       if ((pkp = (struct pkcb *) m -> m_pkthdr.rcvif) == 0)
                return;
        xp = mtod (m, struct x25_packet *);
        ptype = pk_decode (xp);
                return;
        xp = mtod (m, struct x25_packet *);
        ptype = pk_decode (xp);
@@ -245,6 +394,8 @@ register struct mbuf *m;
                return;
        }
 
                return;
        }
 
+       m -> m_pkthdr.rcvif = pkp -> pk_ia -> ia_ifp;
+
        switch (ptype + lcdstate) {
        /* 
         *  Incoming Call packet received. 
        switch (ptype + lcdstate) {
        /* 
         *  Incoming Call packet received. 
@@ -258,7 +409,7 @@ register struct mbuf *m;
         *  the DCE will ignore it anyway. 
         */
        case CALL + SENT_CALL: 
         *  the DCE will ignore it anyway. 
         */
        case CALL + SENT_CALL: 
-               pk_message ((int)lcn, pkp -> pk_xcp, 
+               pk_message ((int) lcn, pkp -> pk_xcp, 
                        "incoming call collision");
                break;
 
                        "incoming call collision");
                break;
 
@@ -342,7 +493,7 @@ register struct mbuf *m;
                 */
 
                if (PS(xp) != ((lcp -> lcd_rsn + 1) % MODULUS) ||
                 */
 
                if (PS(xp) != ((lcp -> lcd_rsn + 1) % MODULUS) ||
-                       PS(xp) == ((lcp -> lcd_input_window + lcp->lcd_windowsize) % MODULUS)) {
+                       PS(xp) == ((lcp -> lcd_input_window + lcp -> lcd_windowsize) % MODULUS)) {
                        m_freem (m);
                        pk_procerror (RESET, lcp, "p(s) flow control error", 1);
                        break;
                        m_freem (m);
                        pk_procerror (RESET, lcp, "p(s) flow control error", 1);
                        break;
@@ -376,14 +527,14 @@ register struct mbuf *m;
                                                "C.P.S. overflow", 128);
                                        return;
                                }
                                                "C.P.S. overflow", 128);
                                        return;
                                }
-                               q_and_d_bits = 0xc0 & *(octet *)xp;
+                               q_and_d_bits = 0xc0 & *(octet *) xp;
                                xp = (struct x25_packet *)
                                xp = (struct x25_packet *)
-                                       (mtod(m, octet *) - PKHEADERLN);
-                               *(octet *)xp |= q_and_d_bits;
+                                       (mtod (m, octet *) - PKHEADERLN);
+                               *(octet *) xp |= q_and_d_bits;
                        }
                        if (mbit) {
                                lcp -> lcd_cps = m;
                        }
                        if (mbit) {
                                lcp -> lcd_cps = m;
-                               pk_flowcontrol(lcp, 0, 1);
+                               pk_flowcontrol (lcp, 0, 1);
                                return;
                        }
                        lcp -> lcd_cps = 0;
                                return;
                        }
                        lcp -> lcd_cps = 0;
@@ -391,21 +542,22 @@ register struct mbuf *m;
                if (so == 0)
                        break;
                if (lcp -> lcd_flags & X25_MQBIT) {
                if (so == 0)
                        break;
                if (lcp -> lcd_flags & X25_MQBIT) {
-                       octet t = (xp -> q_bit) ? t = 0x80 : 0;
+                       octet t = (X25GBITS(xp -> bits, q_bit)) ? t = 0x80 : 0;
 
                        if (MBIT(xp))
                                t |= 0x40;
                        m -> m_data -= 1;
                        m -> m_len += 1;
                        m -> m_pkthdr.len += 1;
 
                        if (MBIT(xp))
                                t |= 0x40;
                        m -> m_data -= 1;
                        m -> m_len += 1;
                        m -> m_pkthdr.len += 1;
-                       *mtod(m, octet *) = t;
+                       *mtod (m, octet *) = t;
                }
 
                /*
                 * Discard Q-BIT packets if the application
                 * doesn't want to be informed of M and Q bit status
                 */
                }
 
                /*
                 * Discard Q-BIT packets if the application
                 * doesn't want to be informed of M and Q bit status
                 */
-               if (xp -> q_bit && (lcp -> lcd_flags & X25_MQBIT) == 0) {
+               if (X25GBITS(xp -> bits, q_bit) 
+                   && (lcp -> lcd_flags & X25_MQBIT) == 0) {
                        m_freem (m);
                        /*
                         * NB.  This is dangerous: sending a RR here can
                        m_freem (m);
                        /*
                         * NB.  This is dangerous: sending a RR here can
@@ -413,7 +565,7 @@ register struct mbuf *m;
                         * packet has not yet been passed up to the application
                         * (RR's are normally generated via PRU_RCVD).
                         */
                         * packet has not yet been passed up to the application
                         * (RR's are normally generated via PRU_RCVD).
                         */
-                       pk_flowcontrol(lcp, 0, 1);
+                       pk_flowcontrol (lcp, 0, 1);
                } else {
                        sbappendrecord (&so -> so_rcv, m);
                        sorwakeup (so);
                } else {
                        sbappendrecord (&so -> so_rcv, m);
                        sorwakeup (so);
@@ -503,7 +655,7 @@ register struct mbuf *m;
                lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESET_CONFIRM);
                pk_output (lcp);
 
                lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESET_CONFIRM);
                pk_output (lcp);
 
-               pk_flush(lcp);
+               pk_flush (lcp);
                if (so == 0)
                        break;
                wakeup ((caddr_t) & so -> so_timeo);
                if (so == 0)
                        break;
                wakeup ((caddr_t) & so -> so_timeo);
@@ -543,10 +695,32 @@ register struct mbuf *m;
        case RESTART + READY: 
                switch (pkp -> pk_state) {
                case DTE_SENT_RESTART: 
        case RESTART + READY: 
                switch (pkp -> pk_state) {
                case DTE_SENT_RESTART: 
-                       /* Restart collision. */
+                       /* 
+                        * Restart collision.
+                        * If case the restart cause is "DTE originated" we
+                        * have a DTE-DTE situation and are trying to resolve
+                        * who is going to play DTE/DCE [ISO 8208:4.2-4.5]
+                        */
+                       if (RESTART_DTE_ORIGINATED(xp)) {
+                               pk_restart (pkp, X25_RESTART_DTE_ORIGINATED);
+                               pk_message (0, pkp -> pk_xcp,
+                                           "RESTART collision");
+                               if ((pkp -> pk_restartcolls++) > MAXRESTARTCOLLISIONS) {
+                                       pk_message (0, pkp -> pk_xcp,
+                                                   "excessive RESTART collisions");
+                                       pkp -> pk_restartcolls = 0;
+                               }
+                               break;
+                       }
                        pkp -> pk_state = DTE_READY;
                        pkp -> pk_state = DTE_READY;
+                       pkp -> pk_dxerole |= DTE_PLAYDTE;
+                       pkp -> pk_dxerole &= ~DTE_PLAYDCE;
                        pk_message (0, pkp -> pk_xcp,
                                "Packet level operational");
                        pk_message (0, pkp -> pk_xcp,
                                "Packet level operational");
+                       pk_message (0, pkp -> pk_xcp, 
+                                   "Assuming DTE role");
+                       if (pkp -> pk_dxerole & DTE_CONNECTPENDING)
+                               pk_callcomplete (pkp);
                        break;
 
                default: 
                        break;
 
                default: 
@@ -555,6 +729,20 @@ register struct mbuf *m;
                        pkp -> pk_chan[0] -> lcd_template = pk_template (0,
                                X25_RESTART_CONFIRM);
                        pk_output (pkp -> pk_chan[0]);
                        pkp -> pk_chan[0] -> lcd_template = pk_template (0,
                                X25_RESTART_CONFIRM);
                        pk_output (pkp -> pk_chan[0]);
+                       pkp -> pk_state = DTE_READY;
+                       pkp -> pk_dxerole |= RESTART_DTE_ORIGINATED(xp) ? DTE_PLAYDCE :
+                               DTE_PLAYDTE;
+                       if (pkp -> pk_dxerole & DTE_PLAYDTE) {
+                               pkp -> pk_dxerole &= ~DTE_PLAYDCE;
+                               pk_message (0, pkp -> pk_xcp, 
+                                           "Assuming DTE role");
+                       } else {
+                               pkp -> pk_dxerole &= ~DTE_PLAYDTE;
+                               pk_message (0, pkp -> pk_xcp, 
+                                        "Assuming DCE role");
+                       }
+                       if (pkp -> pk_dxerole & DTE_CONNECTPENDING)
+                               pk_callcomplete (pkp);
                }
                break;
 
                }
                break;
 
@@ -566,14 +754,21 @@ register struct mbuf *m;
                switch (pkp -> pk_state) {
                case DTE_SENT_RESTART: 
                        pkp -> pk_state = DTE_READY;
                switch (pkp -> pk_state) {
                case DTE_SENT_RESTART: 
                        pkp -> pk_state = DTE_READY;
+                       pkp -> pk_dxerole |= DTE_PLAYDTE;
+                       pkp -> pk_dxerole &= ~DTE_PLAYDCE;
                        pk_message (0, pkp -> pk_xcp,
                        pk_message (0, pkp -> pk_xcp,
-                               "Packet level operational");
+                                   "Packet level operational");
+                       pk_message (0, pkp -> pk_xcp,
+                                   "Assuming DTE role");
+                       if (pkp -> pk_dxerole & DTE_CONNECTPENDING)
+                               pk_callcomplete (pkp);
                        break;
 
                default: 
                        /* Restart local procedure error. */
                        pk_restart (pkp, X25_RESTART_LOCAL_PROCEDURE_ERROR);
                        pkp -> pk_state = DTE_SENT_RESTART;
                        break;
 
                default: 
                        /* Restart local procedure error. */
                        pk_restart (pkp, X25_RESTART_LOCAL_PROCEDURE_ERROR);
                        pkp -> pk_state = DTE_SENT_RESTART;
+                       pkp -> pk_dxerole &= ~(DTE_PLAYDTE | DTE_PLAYDCE);
                }
                break;
 
                }
                break;
 
@@ -597,16 +792,16 @@ register struct mbuf *m;
 }
 
 static
 }
 
 static
-prune_dnic(from, to, dnicname, xcp)
+prune_dnic (from, to, dnicname, xcp)
 char *from, *to, *dnicname;
 register struct x25config *xcp;
 {
        register char *cp1 = from, *cp2 = from;
 char *from, *to, *dnicname;
 register struct x25config *xcp;
 {
        register char *cp1 = from, *cp2 = from;
-       if (xcp->xc_prepnd0 && *cp1 == '0') {
+       if (xcp -> xc_prepnd0 && *cp1 == '0') {
                from = ++cp1;
                goto copyrest;
        }
                from = ++cp1;
                goto copyrest;
        }
-       if (xcp->xc_nodnic) {
+       if (xcp -> xc_nodnic) {
                for (cp1 = dnicname; *cp2 = *cp1++;)
                        cp2++;
                cp1 = from;
                for (cp1 = dnicname; *cp2 = *cp1++;)
                        cp2++;
                cp1 = from;
@@ -642,47 +837,47 @@ register struct x25config *xcp;
        octet *cp;
        unsigned count;
 
        octet *cp;
        unsigned count;
 
-       bzero ((caddr_t)sa, sizeof (*sa));
+       bzero ((caddr_t) sa, sizeof (*sa));
        sa -> x25_len = sizeof (*sa);
        sa -> x25_family = AF_CCITT;
        if (iscalling) {
        sa -> x25_len = sizeof (*sa);
        sa -> x25_family = AF_CCITT;
        if (iscalling) {
-               cp = a -> address_field + (a -> called_addrlen / 2);
-               count = a -> calling_addrlen;
-               pk_simple_bsd (cp, buf, a -> called_addrlen, count);
+               cp = a -> address_field + (X25GBITS(a -> addrlens, called_addrlen) / 2);
+               count = X25GBITS(a -> addrlens, calling_addrlen);
+               pk_simple_bsd (cp, buf, X25GBITS(a -> addrlens, called_addrlen), count);
        } else {
        } else {
-               count = a -> called_addrlen;
+               count = X25GBITS(a -> addrlens, called_addrlen);
                pk_simple_bsd (a -> address_field, buf, 0, count);
        }
                pk_simple_bsd (a -> address_field, buf, 0, count);
        }
-       if (xcp -> xc_addr.x25_net && (xcp -> xc_nodnic || xcp ->xc_prepnd0)) {
-               octet dnicname[sizeof(long) * NBBY/3 + 2];
+       if (xcp -> xc_addr.x25_net && (xcp -> xc_nodnic || xcp -> xc_prepnd0)) {
+               octet dnicname[sizeof (long) * NBBY/3 + 2];
 
 
-               sprintf (dnicname, "%d", xcp -> xc_addr.x25_net);
-               prune_dnic (buf, sa -> x25_addr, dnicname, xcp);
+               sprintf ((char *) dnicname, "%d", xcp -> xc_addr.x25_net);
+               prune_dnic ((char *) buf, sa -> x25_addr, dnicname, xcp);
        } else
        } else
-               bcopy ((caddr_t)buf, (caddr_t)sa -> x25_addr, count + 1);
+               bcopy ((caddr_t) buf, (caddr_t) sa -> x25_addr, count + 1);
 }
 
 static
 }
 
 static
-save_extra(m0, fp, so)
+save_extra (m0, fp, so)
 struct mbuf *m0;
 octet *fp;
 struct socket *so;
 {
        register struct mbuf *m;
        struct cmsghdr cmsghdr;
 struct mbuf *m0;
 octet *fp;
 struct socket *so;
 {
        register struct mbuf *m;
        struct cmsghdr cmsghdr;
-       if (m = m_copym (m, 0, (int)M_COPYALL)) {
+       if (m = m_copy (m, 0, (int)M_COPYALL)) {
                int off = fp - mtod (m0, octet *);
                int off = fp - mtod (m0, octet *);
-               int len = m->m_pkthdr.len - off + sizeof (cmsghdr);
+               int len = m -> m_pkthdr.len - off + sizeof (cmsghdr);
                cmsghdr.cmsg_len = len;
                cmsghdr.cmsg_level = AF_CCITT;
                cmsghdr.cmsg_type = PK_FACILITIES;
                m_adj (m, off);
                cmsghdr.cmsg_len = len;
                cmsghdr.cmsg_level = AF_CCITT;
                cmsghdr.cmsg_type = PK_FACILITIES;
                m_adj (m, off);
-               M_PREPEND (m, sizeof(cmsghdr), M_DONTWAIT);
+               M_PREPEND (m, sizeof (cmsghdr), M_DONTWAIT);
                if (m == 0)
                        return;
                bcopy ((caddr_t)&cmsghdr, mtod (m, caddr_t), sizeof (cmsghdr));
                MCHTYPE(m, MT_CONTROL);
                if (m == 0)
                        return;
                bcopy ((caddr_t)&cmsghdr, mtod (m, caddr_t), sizeof (cmsghdr));
                MCHTYPE(m, MT_CONTROL);
-               sbappendrecord(&so -> so_rcv, m);
+               sbappendrecord (&so -> so_rcv, m);
        }
 }
 
        }
 }
 
@@ -700,10 +895,10 @@ struct pkcb *pkp;
        register struct sockaddr_x25 *sa;
        register struct x25_calladdr *a;
        register struct socket *so = 0;
        register struct sockaddr_x25 *sa;
        register struct x25_calladdr *a;
        register struct socket *so = 0;
-       struct  x25_packet *xp = mtod(m0, struct x25_packet *);
+       struct  x25_packet *xp = mtod (m0, struct x25_packet *);
        struct  mbuf *m;
        struct  x25config *xcp = pkp -> pk_xcp;
        struct  mbuf *m;
        struct  x25config *xcp = pkp -> pk_xcp;
-       int len = m0->m_pkthdr.len;
+       int len = m0 -> m_pkthdr.len;
        unsigned udlen;
        char *errstr = "server unavailable";
        octet *u, *facp;
        unsigned udlen;
        char *errstr = "server unavailable";
        octet *u, *facp;
@@ -718,14 +913,14 @@ struct pkcb *pkp;
        sa = mtod (m, struct sockaddr_x25 *);
        a = (struct x25_calladdr *) &xp -> packet_data;
        facp = u = (octet *) (a -> address_field +
        sa = mtod (m, struct sockaddr_x25 *);
        a = (struct x25_calladdr *) &xp -> packet_data;
        facp = u = (octet *) (a -> address_field +
-               ((a -> called_addrlen + a -> calling_addrlen + 1) / 2));
+               ((X25GBITS(a -> addrlens, called_addrlen) + X25GBITS(a -> addrlens, calling_addrlen) + 1) / 2));
        u += *u + 1;
        u += *u + 1;
-       udlen = min (16, ((octet *)xp) + len - u);
+       udlen = min (16, ((octet *) xp) + len - u);
        if (udlen < 0)
                udlen = 0;
        pk_from_bcd (a, 1, sa, pkp -> pk_xcp); /* get calling address */
        pk_parse_facilities (facp, sa);
        if (udlen < 0)
                udlen = 0;
        pk_from_bcd (a, 1, sa, pkp -> pk_xcp); /* get calling address */
        pk_parse_facilities (facp, sa);
-       bcopy ((caddr_t)u, sa -> x25_udata, udlen);
+       bcopy ((caddr_t) u, sa -> x25_udata, udlen);
        sa -> x25_udlen = udlen;
 
        /*
        sa -> x25_udlen = udlen;
 
        /*
@@ -738,7 +933,7 @@ struct pkcb *pkp;
        for (l = pk_listenhead; l; l = l -> lcd_listen) {
                struct sockaddr_x25 *sxp = l -> lcd_ceaddr;
 
        for (l = pk_listenhead; l; l = l -> lcd_listen) {
                struct sockaddr_x25 *sxp = l -> lcd_ceaddr;
 
-               if (bcmp (sxp -> x25_udata, u, sxp->x25_udlen))
+               if (bcmp (sxp -> x25_udata, u, sxp -> x25_udlen))
                        continue;
                if (sxp -> x25_net &&
                    sxp -> x25_net != xcp -> xc_addr.x25_net)
                        continue;
                if (sxp -> x25_net &&
                    sxp -> x25_net != xcp -> xc_addr.x25_net)
@@ -747,7 +942,7 @@ struct pkcb *pkp;
                 * don't accept incoming calls with the D-Bit on
                 * unless the server agrees
                 */
                 * don't accept incoming calls with the D-Bit on
                 * unless the server agrees
                 */
-               if (xp -> d_bit && !(sxp -> x25_opts.op_flags & X25_DBIT)) {
+               if (X25GBITS(xp -> bits, d_bit) && !(sxp -> x25_opts.op_flags & X25_DBIT)) {
                        errstr = "incoming D-Bit mismatch";
                        break;
                }
                        errstr = "incoming D-Bit mismatch";
                        break;
                }
@@ -764,7 +959,7 @@ struct pkcb *pkp;
                        if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED))
                                    lcp = (struct pklcd *) so -> so_pcb;
                } else 
                        if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED))
                                    lcp = (struct pklcd *) so -> so_pcb;
                } else 
-                       lcp = pk_attach((struct socket *) 0);
+                       lcp = pk_attach ((struct socket *) 0);
                if (lcp == 0) {
                        /*
                         * Insufficient space or too many unaccepted
                if (lcp == 0) {
                        /*
                         * Insufficient space or too many unaccepted
@@ -782,12 +977,12 @@ struct pkcb *pkp;
                pk_assoc (pkp, lcp, sa);
                lcp -> lcd_faddr = *sa;
                lcp -> lcd_laddr.x25_udlen = sxp -> x25_udlen;
                pk_assoc (pkp, lcp, sa);
                lcp -> lcd_faddr = *sa;
                lcp -> lcd_laddr.x25_udlen = sxp -> x25_udlen;
-               lcp -> lcd_craddr = &lcp->lcd_faddr;
+               lcp -> lcd_craddr = &lcp -> lcd_faddr;
                lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED);
                if (lcp -> lcd_flags & X25_DBIT) {
                lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED);
                if (lcp -> lcd_flags & X25_DBIT) {
-                       if (xp -> d_bit)
-                               mtod(lcp -> lcd_template,
-                                       struct x25_packet *) -> d_bit = 1;
+                       if (X25GBITS(xp -> bits, d_bit))
+                               X25SBITS(mtod (lcp -> lcd_template,
+                                       struct x25_packet *) -> bits, d_bit, 1);
                        else
                                lcp -> lcd_flags &= ~X25_DBIT;
                }
                        else
                                lcp -> lcd_flags &= ~X25_DBIT;
                }
@@ -795,7 +990,7 @@ struct pkcb *pkp;
                        pk_output (lcp);
                        soisconnected (so);
                        if (so -> so_options & SO_OOBINLINE)
                        pk_output (lcp);
                        soisconnected (so);
                        if (so -> so_options & SO_OOBINLINE)
-                               save_extra(m0, facp, so);
+                               save_extra (m0, facp, so);
                } else if (lcp -> lcd_upper) {
                        (*lcp -> lcd_upper) (lcp, m0);
                }
                } else if (lcp -> lcd_upper) {
                        (*lcp -> lcd_upper) (lcp, m0);
                }
@@ -809,19 +1004,19 @@ struct pkcb *pkp;
         * CONFIRMATION.
         */
 #ifdef WATERLOO                /* be explicit */
         * CONFIRMATION.
         */
 #ifdef WATERLOO                /* be explicit */
-       if (l == 0 && bcmp(sa->x25_udata, "ean", 3) == 0)
+       if (l == 0 && bcmp (sa -> x25_udata, "ean", 3) == 0)
                pk_message (lcn, pkp -> pk_xcp, "host=%s ean%c: %s",
                pk_message (lcn, pkp -> pk_xcp, "host=%s ean%c: %s",
-                       sa->x25_addr, sa->x25_udata[3] & 0xff, errstr);
-       else if (l == 0 && bcmp(sa->x25_udata, "\1\0\0\0", 4) == 0)
+                       sa -> x25_addr, sa -> x25_udata[3] & 0xff, errstr);
+       else if (l == 0 && bcmp (sa -> x25_udata, "\1\0\0\0", 4) == 0)
                pk_message (lcn, pkp -> pk_xcp, "host=%s x29d: %s",
                pk_message (lcn, pkp -> pk_xcp, "host=%s x29d: %s",
-                       sa->x25_addr, errstr);
+                       sa -> x25_addr, errstr);
        else
 #endif
        pk_message (lcn, pkp -> pk_xcp, "host=%s pid=%x %x %x %x: %s",
                sa -> x25_addr, sa -> x25_udata[0] & 0xff,
                sa -> x25_udata[1] & 0xff, sa -> x25_udata[2] & 0xff,
                sa -> x25_udata[3] & 0xff, errstr);
        else
 #endif
        pk_message (lcn, pkp -> pk_xcp, "host=%s pid=%x %x %x %x: %s",
                sa -> x25_addr, sa -> x25_udata[0] & 0xff,
                sa -> x25_udata[1] & 0xff, sa -> x25_udata[2] & 0xff,
                sa -> x25_udata[3] & 0xff, errstr);
-       if ((lcp = pk_attach((struct socket *)0)) == 0) {
+       if ((lcp = pk_attach ((struct socket *)0)) == 0) {
                (void) m_free (m);
                return;
        }
                (void) m_free (m);
                return;
        }
@@ -844,18 +1039,18 @@ struct mbuf *m;
        lcp -> lcd_state = DATA_TRANSFER;
        if (lcp -> lcd_so)
                soisconnected (lcp -> lcd_so);
        lcp -> lcd_state = DATA_TRANSFER;
        if (lcp -> lcd_so)
                soisconnected (lcp -> lcd_so);
-       if ((lcp -> lcd_flags & X25_DBIT) && (xp -> d_bit == 0))
+       if ((lcp -> lcd_flags & X25_DBIT) && (X25GBITS(xp -> bits, d_bit) == 0))
                lcp -> lcd_flags &= ~X25_DBIT;
        if (len > 3) {
                ap = (struct x25_calladdr *) &xp -> packet_data;
                lcp -> lcd_flags &= ~X25_DBIT;
        if (len > 3) {
                ap = (struct x25_calladdr *) &xp -> packet_data;
-               fcp = (octet *) ap -> address_field + (ap -> calling_addrlen +
-                       ap -> called_addrlen + 1) / 2;
-               if (fcp + *fcp <= ((octet *)xp) + len)
+               fcp = (octet *) ap -> address_field + (X25GBITS(ap -> addrlens, calling_addrlen) +
+                       X25GBITS(ap -> addrlens, called_addrlen) + 1) / 2;
+               if (fcp + *fcp <= ((octet *) xp) + len)
                        pk_parse_facilities (fcp, lcp -> lcd_ceaddr);
        }
        pk_assoc (lcp -> lcd_pkp, lcp, lcp -> lcd_ceaddr);
        if (lcp -> lcd_so == 0 && lcp -> lcd_upper)
                        pk_parse_facilities (fcp, lcp -> lcd_ceaddr);
        }
        pk_assoc (lcp -> lcd_pkp, lcp, lcp -> lcd_ceaddr);
        if (lcp -> lcd_so == 0 && lcp -> lcd_upper)
-               lcp -> lcd_upper(lcp, m);
+               lcp -> lcd_upper (lcp, m);
 }
 
 pk_parse_facilities (fcp, sa)
 }
 
 pk_parse_facilities (fcp, sa)