BSD 4_4 release
[unix-history] / usr / src / sys / netccitt / pk_input.c
index e293d7d..1f8f0bc 100644 (file)
 /*
  * Copyright (c) University of British Columbia, 1984
 /*
  * Copyright (c) University of British Columbia, 1984
- * Copyright (c) 1990 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.2 (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 "../h/param.h"
-#include "../h/systm.h"
-#include "../h/mbuf.h"
-#include "../h/socket.h"
-#include "../h/protosw.h"
-#include "../h/socketvar.h"
-#include "../h/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>
 
 
-#include "../net/if.h"
+struct pkcb_q pkcb_q = {&pkcb_q, &pkcb_q};
+
+/*
+ * 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 "../netccitt/x25.h"
-#include "../netccitt/pk.h"
-#include "../netccitt/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 *
 
 struct pkcb *
-pk_newlink (xcp)
-struct x25config *xcp;
+pk_newlink (ia, llnext)
+struct x25_ifaddr *ia;
+caddr_t llnext;
 {
 {
+       register struct x25config *xcp = &ia -> ia_xc;
        register struct pkcb *pkp;
        register struct pkcb *pkp;
-       register struct mbuf *m;
        register struct pklcd *lcp;
        register struct protosw *pp;
        register struct pklcd *lcp;
        register struct protosw *pp;
-       register unsigned size;
+       unsigned size;
 
 
-       if (xcp -> xc_ntnlen <= 0 || xcp -> xc_ntnlen > sizeof (xcp -> xc_ntn) * 2)
-               return ((struct pkcb *)0);
-#ifdef BSD4_3
-       pp = pffindproto (AF_CCITT, (int)xcp -> xc_lproto, 0);
-#else
-       pp = pffindproto (AF_CCITT, (int)xcp -> xc_lproto);
-#endif
+       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);
        }
-
        /*
         * Allocate a network control block structure
         */
        /*
         * Allocate a network control block structure
         */
-
-       size = sizeof (struct pkcb) + xcp->xc_maxlcn * sizeof (struct pklcd *);
-#ifdef sun
-       if (xcp -> xc_maxlcn < 1 || size > mclbytes) {
-#else
-       if (xcp -> xc_maxlcn < 1 || size > CLBYTES) {
-#endif
-               pk_message (0, xcp, "invalid maxlcn");
-               return ((struct pkcb *)0);
-       }
-       m = m_get (M_DONTWAIT, MT_PCB);
-       if (m == 0)
-               return ((struct pkcb *)0);
-       if (size > MLEN) {
-#ifdef sun
-               if (mclget (m) == 0) {
-                       m_freem (m);
-                       return ((struct pkcb *)0);
-               }
-#else
-#ifdef BSD4_3
-               MCLGET (m);
-               if (m -> m_len != CLBYTES) {
-                       (void) m_free (m);
-                       return ((struct pkcb *)0);
-               }
-#else
-               register struct mbuf *p;
-
-               MCLGET (p, 1);
-               if (p == 0) {
-                       m_freem (m);
-                       return ((struct pkcb *)0);
-               }
-               m -> m_off = (int)p - (int)m;
-#endif
-#endif
-       }
-       pkp = mtod (m, struct pkcb *);
-       bzero ((caddr_t)pkp, size);
-
-       /*
-        * Allocate a logical channel descriptor for lcn 0
-        */
-
-       m = m_getclr (M_DONTWAIT, MT_PCB);
-       if (m == 0) {
-               m_freem (dtom (pkp));
+       size = sizeof (struct pkcb);
+       pkp = (struct pkcb *) malloc (size, M_PCB, M_WAITOK);
+       if (pkp == 0)
                return ((struct pkcb *)0);
                return ((struct pkcb *)0);
-       }
-       lcp = mtod (m, struct pklcd *);
-       lcp -> lcd_state = READY;
-       lcp -> lcd_pkp = pkp;
-       pkp -> pk_chan[0] = lcp;
-
-       pkp -> pk_output = pp -> pr_output;
+       bzero ((caddr_t) pkp, size);
+       pkp -> pk_lloutput = pp -> pr_output;
+       pkp -> pk_llctlinput = (caddr_t (*)()) pp -> pr_ctlinput;
        pkp -> pk_xcp = xcp;
        pkp -> pk_xcp = xcp;
+       pkp -> pk_ia = ia;
        pkp -> pk_state = DTE_WAITING;
        pkp -> pk_state = DTE_WAITING;
-       pkp -> pk_maxlcn = xcp -> xc_maxlcn;
-       pkp -> pk_next = pkcbhead;
-       pkcbhead = pkp;
+       pkp -> pk_llnext = llnext;
+       insque (pkp, &pkcb_q);
 
        /*
         * set defaults
 
        /*
         * set defaults
@@ -121,25 +126,125 @@ struct x25config *xcp;
                xcp -> xc_pwsize = DEFAULT_WINDOW_SIZE;
        if (xcp -> xc_psize == 0)
                xcp -> xc_psize = X25_PS128;
                xcp -> xc_pwsize = DEFAULT_WINDOW_SIZE;
        if (xcp -> xc_psize == 0)
                xcp -> xc_psize = X25_PS128;
+       /*
+        * Allocate logical channel descriptor vector
+        */
+
+       (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;
+{
+       struct pklcd *dev_lcp = 0;
+       struct x25config *xcp = pkp -> pk_xcp;
+       if (pkp -> pk_chan &&
+           (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);
+               pkp -> pk_chan = 0;
+       }
+       if (pkp -> pk_chan == 0) {
+               unsigned size;
+               pkp -> pk_maxlcn = xcp -> xc_maxlcn;
+               size = (pkp -> pk_maxlcn + 1) * sizeof (struct pklcd *);
+               pkp -> pk_chan =
+                       (struct pklcd **) malloc (size, M_IFADDR, M_WAITOK);
+               if (pkp -> pk_chan) {
+                       bzero ((caddr_t) pkp -> pk_chan, size);
+                       /*
+                        * Allocate a logical channel descriptor for lcn 0
+                        */
+                       if (dev_lcp == 0 &&
+                           (dev_lcp = pk_attach ((struct socket *)0)) == 0)
+                               return (ENOBUFS);
+                       dev_lcp -> lcd_state = READY;
+                       dev_lcp -> lcd_pkp = pkp;
+                       pkp -> pk_chan[0] = dev_lcp;
+               } else {
+                       if (dev_lcp)
+                               pk_close (dev_lcp);
+                       return (ENOBUFS);
+               }
+       }
+       return 0;
+}
+
 /* 
  *  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, xcp)
-struct x25config *xcp;
+/*VARARGS*/
+caddr_t
+pk_ctlinput (code, src, addr)
+       struct sockaddr *src;
+       caddr_t addr;
 {
 {
-       register struct pkcb *pkp;
-
-       for (pkp = pkcbhead; pkp; pkp = pkp -> pk_next)
-               if (pkp -> pk_xcp == xcp)
-                       break;
-
-       if (pkp == 0 && (pkp = pk_newlink (xcp)) == 0)
-               return (EINVAL);
+       register struct pkcb *pkp = (struct pkcb *) addr;
 
        switch (code) {
        case PRC_LINKUP: 
 
        switch (code) {
        case PRC_LINKUP: 
@@ -155,11 +260,58 @@ struct x25config *xcp;
        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);
 }
-
+struct ifqueue pkintrq;
+/*
+ * This routine is called if there are semi-smart devices that do HDLC
+ * in hardware and want to queue the packet and call level 3 directly
+ */
+pkintr ()
+{
+       register struct mbuf *m;
+       register struct ifaddr *ifa;
+       register struct ifnet *ifp;
+       register int s;
+
+       for (;;) {
+               s = splimp ();
+               IF_DEQUEUE (&pkintrq, m);
+               splx (s);
+               if (m == 0)
+                       break;
+               if (m -> m_len < PKHEADERLN) {
+                       printf ("pkintr: packet too short (len=%d)\n",
+                               m -> m_len);
+                       m_freem (m);
+                       continue;
+               }
+               pk_input (m);
+       }
+}
+struct mbuf *pk_bad_packet;
+struct mbuf_cache pk_input_cache = {0 };
 /* 
  *  X.25 PACKET INPUT
  *
 /* 
  *  X.25 PACKET INPUT
  *
@@ -167,39 +319,37 @@ struct x25config *xcp;
  *  an information frame is received. It decodes the packet and
  *  demultiplexes based on the logical channel number.
  *
  *  an information frame is received. It decodes the packet and
  *  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 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.
+ *
  */
 
  */
 
-pk_input (m, xcp)
+#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;
 register struct mbuf *m;
-struct x25config *xcp;
 {
        register struct x25_packet *xp;
        register struct pklcd *lcp;
        register struct socket *so = 0;
        register struct pkcb *pkp;
        int  ptype, lcn, lcdstate = LISTEN;
 {
        register struct x25_packet *xp;
        register struct pklcd *lcp;
        register struct socket *so = 0;
        register struct pkcb *pkp;
        int  ptype, lcn, lcdstate = LISTEN;
-       static struct x25config *lastxcp;
-       static struct pkcb *lastpkp;
-
-       if (xcp == lastxcp)
-               pkp = lastpkp;
-       else {
-               for (pkp = pkcbhead; ; pkp = pkp -> pk_next) {
-                       if (pkp == 0) {
-                               pk_message (0, xcp, "pk_input: unknown network");
-                               m_freem (m);
-                               return;
-                       }
-                       if (pkp -> pk_xcp == xcp)
-                               break;
-               }
-               lastxcp = xcp;
-               lastpkp = pkp;
-       }
 
 
+       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)
+               return;
        xp = mtod (m, struct x25_packet *);
        ptype = pk_decode (xp);
        xp = mtod (m, struct x25_packet *);
        ptype = pk_decode (xp);
-       lcn = xp -> logical_channel_number;
+       lcn = LCN(xp);
        lcp = pkp -> pk_chan[lcn];
 
        /* 
        lcp = pkp -> pk_chan[lcn];
 
        /* 
@@ -213,7 +363,7 @@ struct x25config *xcp;
                return;
        }
 
                return;
        }
 
-       pk_trace (pkp -> pk_xcp, xp, "P-In");
+       pk_trace (pkp -> pk_xcp, m, "P-In");
 
        if (pkp -> pk_state != DTE_READY && ptype != RESTART && ptype != RESTART_CONF) {
                m_freem (m);
 
        if (pkp -> pk_state != DTE_READY && ptype != RESTART && ptype != RESTART_CONF) {
                m_freem (m);
@@ -225,6 +375,7 @@ struct x25config *xcp;
        } else {
                if (ptype == CLEAR) {   /* idle line probe (Datapac specific) */
                        /* send response on lcd 0's output queue */
        } else {
                if (ptype == CLEAR) {   /* idle line probe (Datapac specific) */
                        /* send response on lcd 0's output queue */
+                       lcp = pkp -> pk_chan[0];
                        lcp -> lcd_template = pk_template (lcn, X25_CLEAR_CONFIRM);
                        pk_output (lcp);
                        m_freem (m);
                        lcp -> lcd_template = pk_template (lcn, X25_CLEAR_CONFIRM);
                        pk_output (lcp);
                        m_freem (m);
@@ -235,18 +386,22 @@ struct x25config *xcp;
        }
 
        if (lcn == 0 && ptype != RESTART && ptype != RESTART_CONF) {
        }
 
        if (lcn == 0 && ptype != RESTART && ptype != RESTART_CONF) {
-               pk_message (0, pkp -> pk_xcp, "illegal ptype (%s) on lcn 0",
-                       pk_name[ptype / MAXSTATES]);
-               m_freem (m);
+               pk_message (0, pkp -> pk_xcp, "illegal ptype (%d, %s) on lcn 0",
+                       ptype, pk_name[ptype / MAXSTATES]);
+               if (pk_bad_packet)
+                       m_freem (pk_bad_packet);
+               pk_bad_packet = m;
                return;
        }
 
                return;
        }
 
+       m -> m_pkthdr.rcvif = pkp -> pk_ia -> ia_ifp;
+
        switch (ptype + lcdstate) {
        /* 
         *  Incoming Call packet received. 
         */
        case CALL + LISTEN: 
        switch (ptype + lcdstate) {
        /* 
         *  Incoming Call packet received. 
         */
        case CALL + LISTEN: 
-               incoming_call (pkp, xp, m -> m_len);
+               pk_incoming_call (pkp, m);
                break;
 
        /*      
                break;
 
        /*      
@@ -254,7 +409,7 @@ struct x25config *xcp;
         *  the DCE will ignore it anyway. 
         */
        case CALL + SENT_CALL: 
         *  the DCE will ignore it anyway. 
         */
        case CALL + SENT_CALL: 
-               pk_message ((int)xp -> logical_channel_number, pkp -> pk_xcp, 
+               pk_message ((int) lcn, pkp -> pk_xcp, 
                        "incoming call collision");
                break;
 
                        "incoming call collision");
                break;
 
@@ -263,7 +418,8 @@ struct x25config *xcp;
         *  previous connect request is now complete.
         */
        case CALL_ACCEPTED + SENT_CALL: 
         *  previous connect request is now complete.
         */
        case CALL_ACCEPTED + SENT_CALL: 
-               call_accepted (lcp, xp, m -> m_len);
+               MCHTYPE(m, MT_CONTROL);
+               pk_call_accepted (lcp, m);
                break;
 
        /* 
                break;
 
        /* 
@@ -287,7 +443,12 @@ struct x25config *xcp;
                lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CLEAR_CONFIRM);
                pk_output (lcp);
                pk_clearcause (pkp, xp);
                lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CLEAR_CONFIRM);
                pk_output (lcp);
                pk_clearcause (pkp, xp);
+               if (lcp -> lcd_upper) {
+                       MCHTYPE(m, MT_CONTROL);
+                       lcp -> lcd_upper (lcp, m);
+               }
                pk_close (lcp);
                pk_close (lcp);
+               lcp = 0;
                break;
 
        /* 
                break;
 
        /* 
@@ -311,6 +472,7 @@ struct x25config *xcp;
         *  results in a clear.
         */
        case CLEAR_CONF + READY:
         *  results in a clear.
         */
        case CLEAR_CONF + READY:
+       case CLEAR_CONF + LISTEN:
                break;
 
        /* 
                break;
 
        /* 
@@ -331,9 +493,9 @@ struct x25config *xcp;
                 */
 
                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);
                        m_freem (m);
-                       pk_procerror (RESET, lcp, "p(s) flow control error");
+                       pk_procerror (RESET, lcp, "p(s) flow control error", 1);
                        break;
                }
                lcp -> lcd_rsn = PS(xp);
                        break;
                }
                lcp -> lcd_rsn = PS(xp);
@@ -342,43 +504,70 @@ struct x25config *xcp;
                        m_freem (m);
                        break;
                }
                        m_freem (m);
                        break;
                }
-
-               m -> m_off += PKHEADERLN;
+               m -> m_data += PKHEADERLN;
                m -> m_len -= PKHEADERLN;
                m -> m_len -= PKHEADERLN;
+               m -> m_pkthdr.len -= PKHEADERLN;
+
+               lcp -> lcd_rxcnt++;
+               if (lcp -> lcd_flags & X25_MBS_HOLD) {
+                       register struct mbuf *n = lcp -> lcd_cps;
+                       int mbit = MBIT(xp);
+                       octet q_and_d_bits;
+
+                       if (n) {
+                               n -> m_pkthdr.len += m -> m_pkthdr.len;
+                               while (n -> m_next)
+                                       n = n -> m_next;
+                               n -> m_next = m;
+                               m = lcp -> lcd_cps;
+
+                               if (lcp -> lcd_cpsmax &&
+                                   n -> m_pkthdr.len > lcp -> lcd_cpsmax) {
+                                       pk_procerror (RESET, lcp,
+                                               "C.P.S. overflow", 128);
+                                       return;
+                               }
+                               q_and_d_bits = 0xc0 & *(octet *) xp;
+                               xp = (struct x25_packet *)
+                                       (mtod (m, octet *) - PKHEADERLN);
+                               *(octet *) xp |= q_and_d_bits;
+                       }
+                       if (mbit) {
+                               lcp -> lcd_cps = m;
+                               pk_flowcontrol (lcp, 0, 1);
+                               return;
+                       }
+                       lcp -> lcd_cps = 0;
+               }
+               if (so == 0)
+                       break;
                if (lcp -> lcd_flags & X25_MQBIT) {
                if (lcp -> lcd_flags & X25_MQBIT) {
-                       octet *t;
+                       octet t = (X25GBITS(xp -> bits, q_bit)) ? t = 0x80 : 0;
 
 
-                       m -> m_off -= 1;
-                       m -> m_len += 1;
-                       t = mtod (m, octet *);
-                       *t = 0x00;
-                       if (xp -> q_bit)
-                               *t |= 0x80;
                        if (MBIT(xp))
                        if (MBIT(xp))
-                               *t |= 0x40;
+                               t |= 0x40;
+                       m -> m_data -= 1;
+                       m -> m_len += 1;
+                       m -> m_pkthdr.len += 1;
+                       *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);
                        m_freem (m);
-                       lcp -> lcd_rxcnt++;
                        /*
                         * NB.  This is dangerous: sending a RR here can
                         * cause sequence number errors if a previous data
                         * packet has not yet been passed up to the application
                         * (RR's are normally generated via PRU_RCVD).
                         */
                        /*
                         * NB.  This is dangerous: sending a RR here can
                         * cause sequence number errors if a previous data
                         * packet has not yet been passed up to the application
                         * (RR's are normally generated via PRU_RCVD).
                         */
-                       lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RR);
-                       pk_output (lcp);
+                       pk_flowcontrol (lcp, 0, 1);
                } else {
                } else {
-#ifdef BSD4_3
                        sbappendrecord (&so -> so_rcv, m);
                        sbappendrecord (&so -> so_rcv, m);
-#else
-                       sbappend (&so -> so_rcv, m);
-#endif
                        sorwakeup (so);
                }
                break;
                        sorwakeup (so);
                }
                break;
@@ -390,9 +579,19 @@ struct x25config *xcp;
                if (lcp -> lcd_reset_condition)
                        break;
                lcp -> lcd_intrdata = xp -> packet_data;
                if (lcp -> lcd_reset_condition)
                        break;
                lcp -> lcd_intrdata = xp -> packet_data;
-               sohasoutofband (so);
                lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_INTERRUPT_CONFIRM);
                pk_output (lcp);
                lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_INTERRUPT_CONFIRM);
                pk_output (lcp);
+               m -> m_data += PKHEADERLN;
+               m -> m_len -= PKHEADERLN;
+               m -> m_pkthdr.len -= PKHEADERLN;
+               MCHTYPE(m, MT_OOBDATA);
+               if (so) {
+                       if (so -> so_options & SO_OOBINLINE)
+                               sbinsertoob (&so -> so_rcv, m);
+                       else
+                               m_freem (m);
+                       sohasoutofband (so);
+               }
                break;
 
        /* 
                break;
 
        /* 
@@ -404,7 +603,7 @@ struct x25config *xcp;
                if (lcp -> lcd_intrconf_pending == TRUE)
                        lcp -> lcd_intrconf_pending = FALSE;
                else
                if (lcp -> lcd_intrconf_pending == TRUE)
                        lcp -> lcd_intrconf_pending = FALSE;
                else
-                       pk_procerror (RESET, lcp, "unexpected packet");
+                       pk_procerror (RESET, lcp, "unexpected packet", 43);
                break;
 
        /* 
                break;
 
        /* 
@@ -412,10 +611,11 @@ struct x25config *xcp;
         *  any data packets waiting transmission.
         */
        case RR + DATA_TRANSFER: 
         *  any data packets waiting transmission.
         */
        case RR + DATA_TRANSFER: 
-               if (lcp -> lcd_reset_condition)
-                       break;
-               if (pk_ack (lcp, PR(xp)) != PACKET_OK)
+               if (lcp -> lcd_reset_condition ||
+                   pk_ack (lcp, PR(xp)) != PACKET_OK) {
+                       ptype = DELETE_PACKET;
                        break;
                        break;
+               }
                if (lcp -> lcd_rnr_condition == TRUE)
                        lcp -> lcd_rnr_condition = FALSE;
                pk_output (lcp);
                if (lcp -> lcd_rnr_condition == TRUE)
                        lcp -> lcd_rnr_condition = FALSE;
                pk_output (lcp);
@@ -426,10 +626,11 @@ struct x25config *xcp;
         *  be sent. Condition is cleared with a RR.
         */
        case RNR + DATA_TRANSFER: 
         *  be sent. Condition is cleared with a RR.
         */
        case RNR + DATA_TRANSFER: 
-               if (lcp -> lcd_reset_condition)
-                       break;
-               if (pk_ack (lcp, PR(xp)) != PACKET_OK)
+               if (lcp -> lcd_reset_condition ||
+                   pk_ack (lcp, PR(xp)) != PACKET_OK) {
+                       ptype = DELETE_PACKET;
                        break;
                        break;
+               }
                lcp -> lcd_rnr_condition = TRUE;
                break;
 
                lcp -> lcd_rnr_condition = TRUE;
                break;
 
@@ -444,13 +645,6 @@ struct x25config *xcp;
                        break;
 
                pk_resetcause (pkp, xp);
                        break;
 
                pk_resetcause (pkp, xp);
-               sbflush (&so -> so_snd);
-               sbflush (&so -> so_rcv);
-
-               wakeup ((caddr_t) & so -> so_timeo);
-               sorwakeup (so);
-               sowwakeup (so);
-
                lcp -> lcd_window_condition = lcp -> lcd_rnr_condition =
                        lcp -> lcd_intrconf_pending = FALSE;
                lcp -> lcd_output_window = lcp -> lcd_input_window =
                lcp -> lcd_window_condition = lcp -> lcd_rnr_condition =
                        lcp -> lcd_intrconf_pending = FALSE;
                lcp -> lcd_output_window = lcp -> lcd_input_window =
@@ -460,6 +654,13 @@ struct x25config *xcp;
 
                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);
+               if (so == 0)
+                       break;
+               wakeup ((caddr_t) & so -> so_timeo);
+               sorwakeup (so);
+               sowwakeup (so);
                break;
 
        /* 
                break;
 
        /* 
@@ -471,7 +672,7 @@ struct x25config *xcp;
                        pk_output (lcp);
                }
                else
                        pk_output (lcp);
                }
                else
-                       pk_procerror (RESET, lcp, "unexpected packet");
+                       pk_procerror (RESET, lcp, "unexpected packet", 32);
                break;
 
        case DATA + SENT_CLEAR: 
                break;
 
        case DATA + SENT_CLEAR: 
@@ -482,7 +683,7 @@ struct x25config *xcp;
        case INTERRUPT_CONF + SENT_CLEAR: 
        case RESET + SENT_CLEAR: 
        case RESET_CONF + SENT_CLEAR: 
        case INTERRUPT_CONF + SENT_CLEAR: 
        case RESET + SENT_CLEAR: 
        case RESET_CONF + SENT_CLEAR: 
-               /* Just ignore packet if we have sent a CLEAR already.
+               /* Just ignore p if we have sent a CLEAR already.
                   */
                break;
 
                   */
                break;
 
@@ -494,10 +695,32 @@ struct x25config *xcp;
        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: 
@@ -506,6 +729,20 @@ struct x25config *xcp;
                        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;
 
@@ -517,34 +754,132 @@ struct x25config *xcp;
                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;
 
        default: 
                if (lcp) {
                }
                break;
 
        default: 
                if (lcp) {
-                       pk_procerror (CLEAR, lcp, "unknown packet error");
+                       pk_procerror (CLEAR, lcp, "unknown packet error", 33);
                        pk_message (lcn, pkp -> pk_xcp,
                                "\"%s\" unexpected in \"%s\" state",
                                pk_name[ptype/MAXSTATES], pk_state[lcdstate]);
                        pk_message (lcn, pkp -> pk_xcp,
                                "\"%s\" unexpected in \"%s\" state",
                                pk_name[ptype/MAXSTATES], pk_state[lcdstate]);
-               }
-               else    /* Packets arrived on an unassigned channel. 
-                       */
-                       pk_message ((int)xp->logical_channel_number, pkp -> pk_xcp,
+               } else
+                       pk_message (lcn, pkp -> pk_xcp,
                                "packet arrived on unassigned lcn");
                break;
        }
                                "packet arrived on unassigned lcn");
                break;
        }
-       if (ptype != DATA)
+       if (so == 0 && lcp && lcp -> lcd_upper && lcdstate == DATA_TRANSFER) {
+               if (ptype != DATA && ptype != INTERRUPT)
+                       MCHTYPE(m, MT_CONTROL);
+               lcp -> lcd_upper (lcp, m);
+       } else if (ptype != DATA && ptype != INTERRUPT)
                m_freem (m);
 }
 
                m_freem (m);
 }
 
+static
+prune_dnic (from, to, dnicname, xcp)
+char *from, *to, *dnicname;
+register struct x25config *xcp;
+{
+       register char *cp1 = from, *cp2 = from;
+       if (xcp -> xc_prepnd0 && *cp1 == '0') {
+               from = ++cp1;
+               goto copyrest;
+       }
+       if (xcp -> xc_nodnic) {
+               for (cp1 = dnicname; *cp2 = *cp1++;)
+                       cp2++;
+               cp1 = from;
+       }
+copyrest:
+       for (cp1 = dnicname; *cp2 = *cp1++;)
+               cp2++;
+}
+/* static */
+pk_simple_bsd (from, to, lower, len)
+register octet *from, *to;
+register len, lower;
+{
+       register int c;
+       while (--len >= 0) {
+               c = *from;
+               if (lower & 0x01)
+                       *from++;
+               else
+                       c >>= 4;
+               c &= 0x0f; c |= 0x30; *to++ = c; lower++;
+       }
+       *to = 0;
+}
+
+/*static octet * */
+pk_from_bcd (a, iscalling, sa, xcp)
+register struct x25_calladdr *a;
+register struct sockaddr_x25 *sa;
+register struct x25config *xcp;
+{
+       octet buf[MAXADDRLN+1];
+       octet *cp;
+       unsigned count;
+
+       bzero ((caddr_t) sa, sizeof (*sa));
+       sa -> x25_len = sizeof (*sa);
+       sa -> x25_family = AF_CCITT;
+       if (iscalling) {
+               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 {
+               count = X25GBITS(a -> addrlens, called_addrlen);
+               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];
+
+               sprintf ((char *) dnicname, "%d", xcp -> xc_addr.x25_net);
+               prune_dnic ((char *) buf, sa -> x25_addr, dnicname, xcp);
+       } else
+               bcopy ((caddr_t) buf, (caddr_t) sa -> x25_addr, count + 1);
+}
+
+static
+save_extra (m0, fp, so)
+struct mbuf *m0;
+octet *fp;
+struct socket *so;
+{
+       register struct mbuf *m;
+       struct cmsghdr cmsghdr;
+       if (m = m_copy (m, 0, (int)M_COPYALL)) {
+               int off = fp - mtod (m0, octet *);
+               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);
+               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);
+               sbappendrecord (&so -> so_rcv, m);
+       }
+}
 
 /* 
  * This routine handles incoming call packets. It matches the protocol
 
 /* 
  * This routine handles incoming call packets. It matches the protocol
@@ -552,61 +887,65 @@ struct x25config *xcp;
  * sockets awaiting connections.
  */
 
  * sockets awaiting connections.
  */
 
-static
-incoming_call (pkp, xp, len)
+pk_incoming_call (pkp, m0)
+struct mbuf *m0;
 struct pkcb *pkp;
 struct pkcb *pkp;
-struct x25_packet *xp;
 {
 {
-       register struct pklcd *lcp, *l;
+       register struct pklcd *lcp = 0, *l;
        register struct sockaddr_x25 *sa;
        register struct x25_calladdr *a;
        register struct sockaddr_x25 *sa;
        register struct x25_calladdr *a;
-       register struct socket *so;
-       struct mbuf *m;
-       register int l1, l2;
-       char *e, *errstr = "server unavailable";
-       octet *u;
-       int lcn = xp -> logical_channel_number;
-
-       /* First, copy the data from the incoming call packet to a X25_socket
-          descriptor. */
-
-       a = (struct x25_calladdr *) &xp -> packet_data;
-       l1 = a -> calling_addrlen;
-       l2 = a -> called_addrlen;
-       if ((m = m_getclr (M_DONTWAIT, MT_HEADER)) == 0)
+       register struct socket *so = 0;
+       struct  x25_packet *xp = mtod (m0, struct x25_packet *);
+       struct  mbuf *m;
+       struct  x25config *xcp = pkp -> pk_xcp;
+       int len = m0 -> m_pkthdr.len;
+       unsigned udlen;
+       char *errstr = "server unavailable";
+       octet *u, *facp;
+       int lcn = LCN(xp);
+
+       /* First, copy the data from the incoming call packet to a X25 address
+          descriptor. It is to be regretted that you have
+          to parse the facilities into a sockaddr to determine
+          if reverse charging is being requested */
+       if ((m = m_get (M_DONTWAIT, MT_SONAME)) == 0)
                return;
        sa = mtod (m, struct sockaddr_x25 *);
                return;
        sa = mtod (m, struct sockaddr_x25 *);
-       u = (octet *) (a -> address_field + l2 / 2);
-       e = sa -> x25_addr;
-       if (l2 & 0x01) {
-               *e++ = *u++ & 0x0f;
-               l1--;
-       }
-       from_bcd (e, &u, l1);
-       if (l1 & 0x01)
-               u++;
-
-       parse_facilities (u, sa);
+       a = (struct x25_calladdr *) &xp -> packet_data;
+       facp = u = (octet *) (a -> address_field +
+               ((X25GBITS(a -> addrlens, called_addrlen) + X25GBITS(a -> addrlens, calling_addrlen) + 1) / 2));
        u += *u + 1;
        u += *u + 1;
-       sa -> x25_udlen = min (16, ((octet *)xp) + len - u);
-       if (sa -> x25_udlen < 0)
-               sa -> x25_udlen = 0;
-       bcopy ((caddr_t)u, sa -> x25_udata, (unsigned)sa -> x25_udlen);
+       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);
+       bcopy ((caddr_t) u, sa -> x25_udata, udlen);
+       sa -> x25_udlen = udlen;
 
        /*
 
        /*
-        * Now, loop through the  listen sockets looking for a match on the
-        * PID. That is  the first  four octets  of the user data field.  This
-        * is the closest thing to a port number for X.25 packets. What it
-        * does provide is away of  multiplexing  services at the user level. 
+        * Now, loop through the listen sockets looking for a match on the
+        * PID. That is the first few octets of the user data field.
+        * This is the closest thing to a port number for X.25 packets.
+        * It does provide a way of multiplexing services at the user level. 
         */
 
        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, sa -> x25_udata, sxp->x25_udlen))
+               if (bcmp (sxp -> x25_udata, u, sxp -> x25_udlen))
                        continue;
                        continue;
-               if (sxp -> x25_net && sxp -> x25_net != pkp->pk_xcp->xc_net)
+               if (sxp -> x25_net &&
+                   sxp -> x25_net != xcp -> xc_addr.x25_net)
                        continue;
                        continue;
+               /*
+                * don't accept incoming calls with the D-Bit on
+                * unless the server agrees
+                */
+               if (X25GBITS(xp -> bits, d_bit) && !(sxp -> x25_opts.op_flags & X25_DBIT)) {
+                       errstr = "incoming D-Bit mismatch";
+                       break;
+               }
                /*
                 * don't accept incoming collect calls unless
                 * the server sets the reverse charging option.
                /*
                 * don't accept incoming collect calls unless
                 * the server sets the reverse charging option.
@@ -616,8 +955,12 @@ struct x25_packet *xp;
                        errstr = "incoming collect call refused";
                        break;
                }
                        errstr = "incoming collect call refused";
                        break;
                }
-               so = sonewconn (l -> lcd_so);
-               if (so == NULL) {
+               if (l -> lcd_so) {
+                       if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED))
+                                   lcp = (struct pklcd *) so -> so_pcb;
+               } else 
+                       lcp = pk_attach ((struct socket *) 0);
+               if (lcp == 0) {
                        /*
                         * Insufficient space or too many unaccepted
                         * connections.  Just throw the call away.
                        /*
                         * Insufficient space or too many unaccepted
                         * connections.  Just throw the call away.
@@ -625,16 +968,33 @@ struct x25_packet *xp;
                        errstr = "server malfunction";
                        break;
                }
                        errstr = "server malfunction";
                        break;
                }
-               lcp = (struct pklcd *) so -> so_pcb;
+               lcp -> lcd_upper = l -> lcd_upper;
+               lcp -> lcd_upnext = l -> lcd_upnext;
                lcp -> lcd_lcn = lcn;
                lcp -> lcd_state = RECEIVED_CALL;
                lcp -> lcd_lcn = lcn;
                lcp -> lcd_state = RECEIVED_CALL;
-               lcp -> lcd_craddr = sa;
-               sa -> x25_opts.op_flags |= sxp -> x25_opts.op_flags &
-                       ~X25_REVERSE_CHARGE;
+               sa -> x25_opts.op_flags |= (sxp -> x25_opts.op_flags &
+                       ~X25_REVERSE_CHARGE) | l -> lcd_flags;
                pk_assoc (pkp, lcp, sa);
                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_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED);
                lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED);
-               pk_output (lcp);
-               soisconnected (so);
+               if (lcp -> lcd_flags & X25_DBIT) {
+                       if (X25GBITS(xp -> bits, d_bit))
+                               X25SBITS(mtod (lcp -> lcd_template,
+                                       struct x25_packet *) -> bits, d_bit, 1);
+                       else
+                               lcp -> lcd_flags &= ~X25_DBIT;
+               }
+               if (so) {
+                       pk_output (lcp);
+                       soisconnected (so);
+                       if (so -> so_options & SO_OOBINLINE)
+                               save_extra (m0, facp, so);
+               } else if (lcp -> lcd_upper) {
+                       (*lcp -> lcd_upper) (lcp, m0);
+               }
+               (void) m_free (m);
                return;
        }
 
                return;
        }
 
@@ -644,52 +1004,56 @@ struct x25_packet *xp;
         * 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 ((m = m_getclr (M_DONTWAIT, MT_HEADER)) == 0) {
-               (void) m_free (dtom (sa));
+       if ((lcp = pk_attach ((struct socket *)0)) == 0) {
+               (void) m_free (m);
                return;
        }
                return;
        }
-       lcp = mtod (m, struct pklcd *);
        lcp -> lcd_lcn = lcn;
        lcp -> lcd_state = RECEIVED_CALL;
        pk_assoc (pkp, lcp, sa);
        lcp -> lcd_lcn = lcn;
        lcp -> lcd_state = RECEIVED_CALL;
        pk_assoc (pkp, lcp, sa);
-       (void) m_free (dtom (sa));
-       pk_clear (lcp);
+       (void) m_free (m);
+       pk_clear (lcp, 0, 1);
 }
 
 }
 
-static
-call_accepted (lcp, xp, len)
+pk_call_accepted (lcp, m)
 struct pklcd *lcp;
 struct pklcd *lcp;
-struct x25_packet *xp;
+struct mbuf *m;
 {
        register struct x25_calladdr *ap;
        register octet *fcp;
 {
        register struct x25_calladdr *ap;
        register octet *fcp;
+       struct x25_packet *xp = mtod (m, struct x25_packet *);
+       int len = m -> m_len;
 
        lcp -> lcd_state = DATA_TRANSFER;
 
        lcp -> lcd_state = DATA_TRANSFER;
-       soisconnected (lcp -> lcd_so);
+       if (lcp -> lcd_so)
+               soisconnected (lcp -> lcd_so);
+       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;
        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)
-                       parse_facilities (fcp, lcp -> lcd_ceaddr);
+               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);
        }
        pk_assoc (lcp -> lcd_pkp, lcp, lcp -> lcd_ceaddr);
+       if (lcp -> lcd_so == 0 && lcp -> lcd_upper)
+               lcp -> lcd_upper (lcp, m);
 }
 
 }
 
-static
-parse_facilities (fcp, sa)
+pk_parse_facilities (fcp, sa)
 register octet *fcp;
 register struct sockaddr_x25 *sa;
 {
 register octet *fcp;
 register struct sockaddr_x25 *sa;
 {
@@ -753,19 +1117,3 @@ register struct sockaddr_x25 *sa;
                }
        }
 }
                }
        }
 }
-
-from_bcd (a, x, len)
-register char *a;
-register octet **x;
-register int len;
-{
-       register int posn = 0;
-
-       while (--len >= 0) {
-               if (posn++ & 0x01)
-                       *a = *(*x)++ & 0x0f;
-               else
-                       *a = (**x >> 4) & 0x0F;
-               *a++ |= 0x30;
-       }
-}