BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / netccitt / pk_input.c
index c38550d..6f4b358 100644 (file)
@@ -1,15 +1,41 @@
 /*
  * Copyright (c) University of British Columbia, 1984
 /*
  * Copyright (c) University of British Columbia, 1984
- * Copyright (c) 1990 The Regents of the University of California.
+ * Copyright (c) 1991 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.
  *
  * 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.
  *
- * %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.9 (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  7.14 (Berkeley) 7/16/91
  */
 
 #include "param.h"
  */
 
 #include "param.h"
 #include "pk.h"
 #include "pk_var.h"
 
 #include "pk.h"
 #include "pk_var.h"
 
+struct pkcb *
+pk_newlink (ia, llnext)
+struct x25_ifaddr *ia;
+caddr_t llnext;
+{
+       register struct x25config *xcp = &ia->ia_xc;
+       register struct pkcb *pkp;
+       register struct pklcd *lcp;
+       register struct protosw *pp;
+       unsigned size;
+
+       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);
+       }
+       /*
+        * Allocate a network control block structure
+        */
+       size = sizeof (struct pkcb);
+       pkp = (struct pkcb *)malloc(size, M_PCB, M_WAITOK);
+       if (pkp == 0)
+               return ((struct pkcb *)0);
+       bzero ((caddr_t)pkp, size);
+       pkp -> pk_lloutput = pp -> pr_output;
+       pkp -> pk_xcp = xcp;
+       pkp -> pk_ia = ia;
+       pkp -> pk_state = DTE_WAITING;
+       pkp -> pk_next = pkcbhead;
+       pkp -> pk_llnext = llnext;
+       pkcbhead = pkp;
+
+       /*
+        * set defaults
+        */
+
+       if (xcp -> xc_pwsize == 0)
+               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);
+}
+
+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)
-register struct x25config *xcp;
+pk_ctlinput (code, pkp)
+register struct pkcb *pkp;
 {
 
 {
 
-       register struct pkcb *pkp;
-
-       for (pkp = pkcbhead; pkp; pkp = pkp -> pk_next)
-               if (pkp -> pk_xcp == xcp)
-                       break;
-
-       if (pkp == 0)
-               return (EINVAL);
 
        switch (code) {
        case PRC_LINKUP: 
 
        switch (code) {
        case PRC_LINKUP: 
@@ -86,21 +190,11 @@ pkintr ()
                        m_freem (m);
                        continue;
                }
                        m_freem (m);
                        continue;
                }
-               if ((m->m_flags & M_PKTHDR) == 0)
-                       panic("pkintr");
-               ifp = m->m_pkthdr.rcvif;
-               /*
-                * look up the appropriate control block
-                */
-               for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
-                       if (ifa->ifa_addr->sa_family == AF_CCITT)
-                               break;
-               if (ifa == 0)
-                       continue;
-               pk_input(m, ((struct x25_ifaddr *)ifa)->ia_xcp);
+               pk_input(m);
        }
 }
 struct mbuf *pk_bad_packet;
        }
 }
 struct mbuf *pk_bad_packet;
+struct mbuf_cache pk_input_cache = {0 };
 /* 
  *  X.25 PACKET INPUT
  *
 /* 
  *  X.25 PACKET INPUT
  *
@@ -108,36 +202,28 @@ struct mbuf *pk_bad_packet;
  *  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 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.
+ *
  */
 
  */
 
-pk_input (m, xcp)
+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);
        lcn = LCN(xp);
        xp = mtod (m, struct x25_packet *);
        ptype = pk_decode (xp);
        lcn = LCN(xp);
@@ -166,6 +252,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);
@@ -189,7 +276,7 @@ struct x25config *xcp;
         *  Incoming Call packet received. 
         */
        case CALL + LISTEN: 
         *  Incoming Call packet received. 
         */
        case CALL + LISTEN: 
-               incoming_call (pkp, m);
+               pk_incoming_call (pkp, m);
                break;
 
        /*      
                break;
 
        /*      
@@ -206,7 +293,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;
 
        /* 
@@ -230,7 +318,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;
 
        /* 
@@ -254,6 +347,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;
 
        /* 
@@ -289,6 +383,7 @@ struct x25config *xcp;
                m -> m_len -= PKHEADERLN;
                m -> m_pkthdr.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);
                if (lcp -> lcd_flags & X25_MBS_HOLD) {
                        register struct mbuf *n = lcp -> lcd_cps;
                        int mbit = MBIT(xp);
@@ -314,7 +409,6 @@ struct x25config *xcp;
                        }
                        if (mbit) {
                                lcp -> lcd_cps = m;
                        }
                        if (mbit) {
                                lcp -> lcd_cps = m;
-                               lcp -> lcd_rxcnt++;
                                pk_flowcontrol(lcp, 0, 1);
                                return;
                        }
                                pk_flowcontrol(lcp, 0, 1);
                                return;
                        }
@@ -339,7 +433,6 @@ struct x25config *xcp;
                 */
                if (xp -> q_bit && (lcp -> lcd_flags & X25_MQBIT) == 0) {
                        m_freem (m);
                 */
                if (xp -> q_bit && (lcp -> lcd_flags & X25_MQBIT) == 0) {
                        m_freem (m);
-                       lcp -> lcd_rxcnt++;
                        /*
                         * NB.  This is dangerous: sending a RR here can
                         * cause sequence number errors if a previous data
                        /*
                         * NB.  This is dangerous: sending a RR here can
                         * cause sequence number errors if a previous data
@@ -521,8 +614,7 @@ struct x25config *xcp;
                                "packet arrived on unassigned lcn");
                break;
        }
                                "packet arrived on unassigned lcn");
                break;
        }
-       if (so == 0 && lcp && lcp -> lcd_upper &&
-           (lcdstate == SENT_CALL || lcdstate == DATA_TRANSFER)) {
+       if (so == 0 && lcp && lcp -> lcd_upper && lcdstate == DATA_TRANSFER) {
                if (ptype != DATA && ptype != INTERRUPT)
                        MCHTYPE(m, MT_CONTROL);
                lcp -> lcd_upper (lcp, m);
                if (ptype != DATA && ptype != INTERRUPT)
                        MCHTYPE(m, MT_CONTROL);
                lcp -> lcd_upper (lcp, m);
@@ -530,6 +622,95 @@ struct x25config *xcp;
                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 + (a -> called_addrlen / 2);
+               count = a -> calling_addrlen;
+               pk_simple_bsd (cp, buf, a -> called_addrlen, count);
+       } else {
+               count = a -> 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 (dnicname, "%d", xcp -> xc_addr.x25_net);
+               prune_dnic (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_copym (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
@@ -537,8 +718,7 @@ struct x25config *xcp;
  * sockets awaiting connections.
  */
 
  * sockets awaiting connections.
  */
 
-static
-incoming_call (pkp, m0)
+pk_incoming_call (pkp, m0)
 struct mbuf *m0;
 struct pkcb *pkp;
 {
 struct mbuf *m0;
 struct pkcb *pkp;
 {
@@ -546,56 +726,57 @@ 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 mbuf *m;
+       struct  x25_packet *xp = mtod(m0, struct x25_packet *);
+       struct  mbuf *m;
+       struct  x25config *xcp = pkp -> pk_xcp;
        int len = m0->m_pkthdr.len;
        int len = m0->m_pkthdr.len;
-       register int l1, l2;
-       char *e, *errstr = "server unavailable";
+       unsigned udlen;
+       char *errstr = "server unavailable";
        octet *u, *facp;
        int lcn = LCN(xp);
 
        octet *u, *facp;
        int lcn = LCN(xp);
 
-       /* 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_SONAME)) == 0)
+       /* 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++;
-
-       facp = u;
-       parse_facilities (u, sa);
+       a = (struct x25_calladdr *) &xp -> packet_data;
+       facp = u = (octet *) (a -> address_field +
+               ((a -> called_addrlen + a -> 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;
                if (sxp -> x25_net &&
                        continue;
                if (sxp -> x25_net &&
-                   sxp -> x25_net != pkp->pk_xc.xc_addr.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 (xp -> 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.
@@ -605,14 +786,6 @@ struct pkcb *pkp;
                        errstr = "incoming collect call refused";
                        break;
                }
                        errstr = "incoming collect call refused";
                        break;
                }
-               /*
-                * 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)) {
-                       errstr = "incoming D-Bit mismatch";
-                       break;
-               }
                if (l -> lcd_so) {
                        if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED))
                                    lcp = (struct pklcd *) so -> so_pcb;
                if (l -> lcd_so) {
                        if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED))
                                    lcp = (struct pklcd *) so -> so_pcb;
@@ -630,10 +803,12 @@ struct pkcb *pkp;
                lcp -> lcd_upnext = l -> lcd_upnext;
                lcp -> lcd_lcn = lcn;
                lcp -> lcd_state = RECEIVED_CALL;
                lcp -> lcd_upnext = l -> lcd_upnext;
                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);
                if (lcp -> lcd_flags & X25_DBIT) {
                        if (xp -> d_bit)
                lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED);
                if (lcp -> lcd_flags & X25_DBIT) {
                        if (xp -> d_bit)
@@ -648,10 +823,9 @@ struct pkcb *pkp;
                        if (so -> so_options & SO_OOBINLINE)
                                save_extra(m0, facp, so);
                } else if (lcp -> lcd_upper) {
                        if (so -> so_options & SO_OOBINLINE)
                                save_extra(m0, facp, so);
                } else if (lcp -> lcd_upper) {
-                       m -> m_next = m0;
-                       (*lcp -> lcd_upper) (lcp, m);
-                       (void) m_free (m);  /* only m; m0 freed by caller */
+                       (*lcp -> lcd_upper) (lcp, m0);
                }
                }
+               (void) m_free (m);
                return;
        }
 
                return;
        }
 
@@ -684,37 +858,14 @@ struct pkcb *pkp;
        pk_clear (lcp, 0, 1);
 }
 
        pk_clear (lcp, 0, 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_copym (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);
-       }
-}
-
-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;
        if (lcp -> lcd_so)
 
        lcp -> lcd_state = DATA_TRANSFER;
        if (lcp -> lcd_so)
@@ -726,13 +877,14 @@ struct x25_packet *xp;
                fcp = (octet *) ap -> address_field + (ap -> calling_addrlen +
                        ap -> called_addrlen + 1) / 2;
                if (fcp + *fcp <= ((octet *)xp) + len)
                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);
+                       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;
 {
@@ -796,19 +948,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;
-       }
-}