home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / PPP.C < prev    next >
C/C++ Source or Header  |  1991-09-18  |  21KB  |  821 lines

  1. /*
  2.  *  PPP.C    -- Send and receive datagrams on serial lines with
  3.  *           Point-to-Point Protocol
  4.  *
  5.  *    This implementation of PPP is declared to be in the public domain.
  6.  *
  7.  *    Based (in part) upon previous implementations by:
  8.  *    1989    -- Drew Daniel Perkins        (ddp@andrew.cmu.edu)
  9.  *           Carnegie Mellon University
  10.  *    09-90    -- Katie Stevens        (dkstevens@ucdavis.edu)
  11.  *           UC Davis, Computing Services
  12.  *
  13.  *    Jan 91    Bill_Simpson@um.cc.umich.edu
  14.  *        Computer Systems Consulting Services
  15.  *
  16.  *    Feb 91    Glenn McGregor            (ghm@merit.edu)
  17.  *        Testing and suggestions.
  18.  *
  19.  *    May 91    Bill Simpson & Glenn McGregor
  20.  *        Update to newest LCP and IPCP draft RFCs.
  21.  *        Add quick installation features.
  22.  *        Add support for echo and discard message sending.
  23.  *
  24.  *    Jul 91    Glenn McGregor & Bill Simpson
  25.  *        Improve PAP user interface and fix related bugs.
  26.  *        Remove pwaits and "phase machine".
  27.  */
  28.  
  29. #include "global.h"
  30. #include "mbuf.h"
  31. #include "proc.h"
  32. #include "iface.h"
  33. #include "internet.h"
  34. #include "ip.h"
  35. #include "slhc.h"
  36. #include "8250.h"
  37. #include "asy.h"
  38. #include "pktdrvr.h"
  39. #include "socket.h"
  40. #include "devparam.h"
  41. #include "ppp.h"
  42. #include "pppfsm.h"
  43. #include "ppplcp.h"
  44. #include "ppppap.h"
  45. #include "pppipcp.h"
  46. #include "trace.h"
  47.  
  48. /* Routines local to this file */
  49. static struct mbuf *htonppp __ARGS((struct ppp_hdr *ppp, struct mbuf *data));
  50.  
  51. static void ppp_log __ARGS((struct ppp_s *ppp_p, char *comment));
  52. static void ppp_error __ARGS((struct ppp_s *ppp_p, struct mbuf *bp, char *comment));
  53. static void ppp_skipped __ARGS((struct ppp_s *ppp_p, struct mbuf *bp, char *comment));
  54.  
  55. static int ppp_raw __ARGS((struct iface *ifp, struct mbuf *data));
  56.  
  57. static void ppp_recv __ARGS((int dev, void *p1, void *p2));
  58.  
  59. static int ppp_iostatus __ARGS((struct iface *ifp, int command, int32 value));
  60. static int ppp_discard     __ARGS((struct iface *ifp, struct mbuf *bp));
  61. static int ppp_echo    __ARGS((struct iface *ifp, struct mbuf *bp));
  62.  
  63.  
  64. /*
  65.  * FCS lookup table as generated by fcsgen.c
  66.  */
  67. static int16 fcstab[256] = {
  68.     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  69.     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  70.     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  71.     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  72.     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  73.     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  74.     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  75.     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  76.     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  77.     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  78.     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  79.     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  80.     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  81.     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  82.     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  83.     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  84.     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  85.     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  86.     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  87.     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  88.     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  89.     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  90.     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  91.     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  92.     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  93.     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  94.     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  95.     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  96.     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  97.     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  98.     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  99.     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  100. };
  101.  
  102. #define pppfcs(fcs, c)        ((fcs >> 8) ^ fcstab[(fcs ^ c) & 0x00ff])
  103. #define SP_CHAR            0x20
  104.  
  105.  
  106. /****************************************************************************/
  107.  
  108. /* Convert PPP header in host form to network form */
  109. static struct mbuf *
  110. htonppp(ppp, data)
  111. struct ppp_hdr *ppp;
  112. struct mbuf *data;
  113. {
  114.     struct mbuf *bp;
  115.     register char *cp;
  116.  
  117.     /* Prepend header onto packet data */
  118.     if ((bp = pushdown(data, PPP_HDR_LEN)) == NULLBUF) {
  119.         free_p( data );
  120.         return NULLBUF;
  121.     }
  122.  
  123.     /* Load header with proper values */
  124.     cp = bp->data;
  125.     *cp++ = ppp->addr;
  126.     *cp++ = ppp->control;
  127.     cp = put16(cp,ppp->protocol);
  128.  
  129.     return bp;
  130. }
  131.  
  132.  
  133. /************************************************************************/
  134. /* General log routine */
  135. static void
  136. ppp_log( ppp_p, comment )
  137. struct ppp_s *ppp_p;
  138. char *comment;
  139. {
  140.     if (ppp_p->trace)
  141.         trace_log(ppp_p->iface,"%s PPP %s",
  142.             ppp_p->iface->name,
  143.             comment);
  144. }
  145.  
  146.  
  147. /* Bad packet */
  148. static void
  149. ppp_error(ppp_p, bp, comment)
  150. struct ppp_s *ppp_p;
  151. struct mbuf *bp;
  152. char *comment;
  153. {
  154.     free_p(bp);
  155.     ppp_log( ppp_p, comment );
  156. }
  157.  
  158.  
  159. /* Unknown type input packet */
  160. static void
  161. ppp_skipped(ppp_p, bp, comment)
  162. struct ppp_s *ppp_p;
  163. struct mbuf *bp;
  164. char *comment;
  165. {
  166.     struct ipcp_s *ipcp_p;
  167.  
  168.     if ( (ipcp_p = ppp_p->fsm[IPcp].pdv) != NULL ) {
  169.         slhc_toss( ipcp_p->slhcp );
  170.     }
  171.     ppp_error( ppp_p, bp, comment );
  172. }
  173.  
  174.  
  175. /****************************************************************************/
  176. /* Send IP datagram with Point-to-Point Protocol */
  177. int
  178. ppp_send(bp,ifp,gateway,prec,del,tput,rel)
  179. struct mbuf *bp;    /* Buffer to send */
  180. struct iface *ifp;    /* Pointer to interface control block */
  181. int32 gateway;        /* Ignored (PPP is point-to-point) */
  182. int prec;
  183. int del;
  184. int tput;
  185. int rel;
  186. {
  187.     struct ppp_s *ppp_p;
  188.     struct ipcp_s *ipcp_p;
  189.     int protocol = PPP_IP_PROTOCOL;
  190.  
  191.     if (ifp == NULLIF
  192.      || (ppp_p = ifp->edv) == NULL) {
  193.         free_p(bp);
  194.         return -1;
  195.     }
  196.  
  197.     if (ppp_p->fsm[IPcp].state != fsmOPENED) {
  198.         ppp_error( ppp_p, bp, "not open for IP traffic" );
  199.         ppp_p->OutError++;
  200.         return -1;
  201.     }
  202.  
  203.     ipcp_p = ppp_p->fsm[IPcp].pdv;
  204.     if (ipcp_p->remote.work.negotiate & IPCP_N_COMPRESS) {
  205.         /* Attempt IP/TCP header compression */
  206.         switch ( slhc_compress(ipcp_p->slhcp, &bp,
  207.             ipcp_p->remote.work.slot_compress) ) {
  208.         case SL_TYPE_IP:
  209.             protocol = PPP_IP_PROTOCOL;
  210.             break;
  211.         case SL_TYPE_COMPRESSED_TCP:
  212.             protocol = PPP_COMPR_PROTOCOL;
  213.             break;
  214.         case SL_TYPE_UNCOMPRESSED_TCP:
  215.             protocol = PPP_UNCOMP_PROTOCOL;
  216.             break;
  217.         default:
  218.             ppp_error( ppp_p, bp, "bad IP packet" );
  219.             ppp_p->OutError++;
  220.             return -1;
  221.         };
  222.     }
  223.     return (*ifp->output)(ifp,NULLCHAR,NULLCHAR,protocol,bp);
  224. }
  225.  
  226.  
  227. /* Send a packet with PPP header */
  228. int
  229. ppp_output(ifp,dest,source,protocol,data)
  230. struct iface *ifp;    /* Pointer to interface control block */
  231. char *dest;        /* Dest addr (ignored; PPP is point-to-point) */
  232. char *source;        /* Source addr (ignored; PPP is point-to-point) */
  233. int16 protocol;        /* PPP Protocol Type field */
  234. struct mbuf *data;    /* Actual data to be sent */
  235. {
  236.     struct ppp_s *ppp_p;
  237.     struct mbuf *bp;
  238.     struct ppp_hdr hdr;
  239.  
  240.     if (ifp == NULLIF
  241.      || (ppp_p = ifp->edv) == NULL) {
  242.         free_p(data);
  243.         return -1;
  244.     }
  245.  
  246.     if (ppp_p->phase == pppDEAD) {
  247.         ppp_error( ppp_p, data, "line not up" );
  248.         ppp_p->OutError++;
  249.         return -1;
  250.     }
  251.  
  252.     hdr.addr = HDLC_ALL_ADDR;
  253.     hdr.control = HDLC_UI;
  254.     hdr.protocol = protocol;
  255.  
  256.     if ((bp = htonppp(&hdr, data)) == NULLBUF) {
  257.         ppp_log( ppp_p, Nospace );
  258.         ppp_p->OutMemory++;
  259.         return -1;
  260.     }
  261.  
  262.     return (*ifp->raw)(ifp,bp);
  263. }
  264.  
  265.  
  266. /* Encode a raw packet in PPP framing, put on link output queue */
  267. static int
  268. ppp_raw(ifp,bp)
  269. struct iface *ifp;
  270. struct mbuf *bp;
  271. {
  272.     struct ppp_s *ppp_p = ifp->edv;
  273.     struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  274.     int full_lcp, full_ac, full_p;
  275.     int16 calc_fcs = HDLC_FCS_START;
  276.     int32 accm = LCP_ACCM_DEFAULT;
  277.     struct ppp_hdr ph;
  278.     int len = PPP_HDR_LEN;
  279.     struct mbuf *vbp;
  280.     register char *cp;
  281.     register int c;
  282.  
  283.     dump(ifp,IF_TRACE_OUT,CL_PPP,bp);
  284.     ppp_p->OutTxOctetCount += len_p(bp) + 2;  /* count FCS bytes */
  285.     ifp->rawsndcnt++;
  286.     ifp->lastsent = secclock();
  287.  
  288.     /* Get the HDLC/PPP header, without actually pulling mbuf up */
  289.     if ( bp == NULLBUF || bp->cnt < PPP_HDR_LEN ) {
  290.         ppp_error( ppp_p, bp, "link header missing" );
  291.         ppp_p->OutError++;
  292.         return -1;
  293.     }
  294.  
  295.     ph.addr = bp->data[0];
  296.     ph.control = bp->data[1];
  297.     ph.protocol = get16(&bp->data[2]);
  298.  
  299.     if ( ( full_lcp = (ph.protocol == PPP_LCP_PROTOCOL) ) == 0
  300.      && (lcp_p->remote.work.negotiate & LCP_N_ACCM) ) {
  301.         accm = lcp_p->remote.work.accm;
  302.     }
  303.  
  304.     if ( ( full_p = ( full_lcp
  305.             ||  !(lcp_p->remote.work.negotiate & LCP_N_PFC)
  306.             ||  ph.protocol >= 0x00ff ) )
  307.         == 0 ) {
  308.         --len;
  309.     }
  310.  
  311.     /* Discard HDLC address and control fields if possible */
  312.     if ( ( full_ac = ( full_lcp
  313.             ||  !(lcp_p->remote.work.negotiate & LCP_N_ACFC) ) )
  314.         == 0 ) {
  315.         len -= 2;
  316.     }
  317.  
  318.     /* Load header with proper values */
  319.     bp->cnt -= PPP_HDR_LEN - len;
  320.     cp = (bp->data += PPP_HDR_LEN - len);
  321.  
  322.     if ( full_ac ) {
  323.         *cp++ = ph.addr;
  324.         *cp++ = ph.control;
  325.     }
  326.     if ( full_p )
  327.         *cp++ = (ph.protocol >> 8);
  328.     *cp++ = (ph.protocol & 0x00ff);
  329.  
  330.     /* Allocate output mbuf that's twice as long as the packet.
  331.      * This is a worst-case guess (consider a packet full of HDLC_FLAGs!)
  332.      */
  333.     if ((vbp = alloc_mbuf((int16)(2*len_p(bp) + HDLC_ENVLEN))) == NULLBUF) {
  334.         ppp_error( ppp_p, bp, Nospace );
  335.         ppp_p->OutMemory++;
  336.         return -1;
  337.     }
  338.     cp = vbp->data;
  339.  
  340.     /* No need to send an opening flag if the previous packet is still
  341.      * being transmitted.
  342.      */
  343.     if ( Asy[ifp->dev].dma.flags == 0 ) {
  344.         /* Flush out any line garbage */
  345.         *cp++ = HDLC_FLAG;
  346.         ppp_p->OutOpenFlag++;
  347.     }
  348.  
  349.     /* Copy input to output, escaping special characters */
  350.     while ((c = PULLCHAR(&bp)) != -1) {
  351.         /* Fold char value into FCS calculated so far */
  352.         calc_fcs = pppfcs(calc_fcs, c);
  353.  
  354.         if ( ((c < SP_CHAR) && (accm & (1L << c)))
  355.             || (c == HDLC_ESC_ASYNC)
  356.             || (c == HDLC_FLAG)) {
  357.             *cp++ = HDLC_ESC_ASYNC;
  358.             *cp++ = (c ^ HDLC_ESC_COMPL);
  359.         } else {
  360.             *cp++ = c;
  361.         }
  362.     }
  363.  
  364.     /* Final FCS calculation */
  365.     calc_fcs ^= 0xffff;
  366.     c = (calc_fcs & 0x00ff);    /* Least significant byte first */
  367.     if ( ((c < SP_CHAR) && (accm & (1L << c)))
  368.          ||(c == HDLC_ESC_ASYNC)
  369.          ||(c == HDLC_FLAG)) {
  370.         *cp++ = HDLC_ESC_ASYNC;
  371.         *cp++ = (c ^ HDLC_ESC_COMPL);
  372.     } else {
  373.         *cp++ = c;
  374.     }
  375.     c = (calc_fcs >> 8);        /* Most significant byte next */
  376.     if ( ((c < SP_CHAR) && (accm & (1L << c)))
  377.          ||(c == HDLC_ESC_ASYNC)
  378.          ||(c == HDLC_FLAG)) {
  379.         *cp++ = HDLC_ESC_ASYNC;
  380.         *cp++ = (c ^ HDLC_ESC_COMPL);
  381.     } else {
  382.         *cp++ = c;
  383.     }
  384.  
  385.     /* Tie off the packet */
  386.     *cp++ = HDLC_FLAG;
  387.     vbp->cnt = cp - vbp->data;
  388.  
  389.     if (ifp->trace & IF_TRACE_RAW)
  390.         raw_dump(ifp,IF_TRACE_OUT,vbp);
  391.     return asy_send(ifp->dev,vbp);
  392. }
  393.  
  394.  
  395. /****************************************************************************/
  396. /* Packetize PPP input from device */
  397. /* (process started by ppp_init) */
  398. void
  399. ppp_recv(dev,p1,p2)
  400. int dev;
  401. void *p1;
  402. void *p2;
  403. {
  404.     struct iface *ifp = p1;
  405.     struct ppp_s *ppp_p = ifp->edv;
  406.     int32 accm = LCP_ACCM_DEFAULT;
  407.     int16 calc_fcs = HDLC_FCS_START;
  408.     struct mbuf *raw_bp = NULLBUF;
  409.     struct mbuf *head_bp = NULLBUF;
  410.     register struct mbuf *tail_bp = NULLBUF;
  411.     char *cp;            /* next byte in tail mbuf */
  412.     register int mode = FALSE;
  413.     register int c;
  414.  
  415.     while ( (c = get_asy(dev)) != -1 ) {
  416. #ifdef PPP_DEBUG_RAW
  417.         if (ifp->trace & IF_TRACE_RAW) {
  418.             if ( raw_bp != NULLBUF
  419.               || (raw_bp = alloc_mbuf( LCP_MRU_HI * 2 )) != NULLBUF ) {
  420.                 *raw_bp->data++ = c;
  421.                 raw_bp->cnt++;
  422.                 if ( raw_bp->cnt != 1 && c == HDLC_FLAG ) {
  423.                     raw_bp->data = (char *)(raw_bp + 1);
  424.                     raw_dump( ifp, IF_TRACE_IN, raw_bp );
  425.                     raw_bp->cnt = 0;
  426.                 }
  427.             }
  428.         }
  429. #endif
  430.         if ( c == HDLC_FLAG ) {
  431.             if ( mode & PPP_ESCAPED ) {
  432.                 ppp_skipped( ppp_p, head_bp,
  433.                     "deliberate cancellation" );
  434.                 ppp_p->InFrame++;
  435.             } else if ( mode & PPP_TOSS ) {
  436.                 free_p( head_bp );
  437.             } else if ( head_bp != NULLBUF ) {
  438.                 if ( calc_fcs != HDLC_FCS_FINAL ) {
  439.                     ppp_skipped( ppp_p, head_bp,
  440.                         "checksum error" );
  441.                     ppp_p->InChecksum++;
  442.                 } else {
  443.                     /* trim off FCS bytes */
  444.                     trim_mbuf(&head_bp, len_p(head_bp)-2);
  445.  
  446.                     net_route( ifp, CL_PPP, head_bp );
  447.                 }
  448.             } else {
  449.                 ppp_p->InOpenFlag++;
  450.             }
  451.  
  452.             /* setup for next buffer */
  453.             mode = FALSE;
  454.             head_bp = tail_bp = NULLBUF;
  455.             calc_fcs = HDLC_FCS_START;
  456.             accm = LCP_ACCM_DEFAULT;
  457.  
  458.             /* Use negotiated values if LCP finished */
  459.             if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  460.                 struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  461.  
  462.                 if (lcp_p->local.work.negotiate & LCP_N_ACCM) {
  463.                     accm = lcp_p->local.work.accm;
  464.                 }
  465.             }
  466. #ifdef PPP_DEBUG_RAW
  467.             if (!(ifp->trace & IF_TRACE_RAW)) {
  468.                 if ( raw_bp != NULLBUF ) {
  469.                     free_p( raw_bp );
  470.                     raw_bp = NULLBUF;
  471.                 }
  472.             }
  473. #endif
  474.             continue;
  475.         }
  476.  
  477.         /* We reach here for every byte inside a frame.
  478.          * (The order of the following tests is important.)
  479.          * Discard spurious control characters.
  480.          * Check for escape sequence.
  481.          * (Allow escaped escape.)
  482.          */
  483.         if ( c < SP_CHAR && (accm & (1L << c)) ) {
  484.             continue;
  485.         } else if ( mode & PPP_ESCAPED ) {
  486.             mode &= ~PPP_ESCAPED;
  487.             c ^= HDLC_ESC_COMPL;
  488.         } else if ( c == HDLC_ESC_ASYNC ) {
  489.             mode |= PPP_ESCAPED;
  490.             continue;
  491.         }
  492.  
  493.         /* We reach here with a byte for the buffer.
  494.          * Make sure there is room for it.
  495.          */
  496.         if ( tail_bp == NULLBUF ) {
  497.             if ((tail_bp = alloc_mbuf(PPP_ALLOC)) == NULLBUF) {
  498.                 ppp_skipped( ppp_p, tail_bp, Nospace );
  499.                 ppp_p->InMemory++;
  500.                 mode |= PPP_TOSS;
  501.                 continue;
  502.             }
  503.             head_bp = tail_bp;
  504.             cp = tail_bp->data;
  505.         } else if ( tail_bp->cnt >= tail_bp->size ) {
  506.             /* Current mbuf is full */
  507.             if ( (tail_bp->next = alloc_mbuf(PPP_ALLOC)) == NULLBUF ) {
  508.                 /* No memory, drop the whole packet */
  509.                 ppp_skipped( ppp_p, head_bp, Nospace );
  510.                 ppp_p->InMemory++;
  511.                 head_bp = NULLBUF;
  512.                 mode |= PPP_TOSS;
  513.                 continue;
  514.             }
  515.             tail_bp = tail_bp->next;
  516.             cp = tail_bp->data;
  517.         }
  518.  
  519.         /* Store the byte, increment counts */
  520.         *cp++ = c;
  521.         tail_bp->cnt++;
  522.         calc_fcs = pppfcs(calc_fcs, c);
  523.     }
  524.  
  525.     /* clean up afterward */
  526.     free_p(raw_bp);
  527.     free_p(head_bp);
  528.     ifp->rxproc = NULLPROC;
  529. }
  530.  
  531. #ifdef TURBOC_SWITCH_BUG
  532. #pragma option -G-
  533. #endif
  534.  
  535. /* Process incoming PPP packets */
  536. /* (called from network task) */
  537. void
  538. ppp_proc(ifp,bp)
  539. struct iface *ifp;
  540. struct mbuf *bp;
  541. {
  542.     struct ppp_s *ppp_p;
  543.     struct ipcp_s *ipcp_p;
  544.     struct ppp_hdr ph;
  545.     struct mbuf *hbp;
  546.     int16 negotiated = FALSE;
  547.  
  548.     if ( ifp == NULLIF ) {
  549.         log(-1, "ppp_proc: missing iface" );
  550.         return;
  551.     }
  552.     if ( bp == NULLBUF ) {
  553.         trace_log(ifp, "ppp_proc: missing buffer" );
  554.         return;
  555.     }
  556.  
  557.     ppp_p = ifp->edv;
  558.     ppp_p->InRxOctetCount += len_p(bp) + 2;   /* count FCS bytes */
  559.  
  560.     /* Use negotiated values if LCP finished */
  561.     if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  562.         struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  563.  
  564.         negotiated = lcp_p->local.work.negotiate;
  565.     }
  566.  
  567.     /* HDLC address and control fields may be compressed out */
  568.     if ((byte_t)bp->data[0] != HDLC_ALL_ADDR) {
  569.         if (!(negotiated & LCP_N_ACFC)) {
  570.             ppp_skipped( ppp_p, bp, "missing ALL address" );
  571.             ppp_p->InFrame++;
  572.             return;
  573.         }
  574.     } else if ((byte_t)bp->data[1] != HDLC_UI) {
  575.         if (!(negotiated & LCP_N_ACFC)
  576.          || !(negotiated & LCP_N_PFC)) {
  577.             ppp_skipped( ppp_p, bp, "missing UI" );
  578.             ppp_p->InFrame++;
  579.             return;
  580.         }
  581.     } else {
  582.         /* skip address/control fields */
  583.         pull16(&bp);
  584.     }
  585.  
  586.     /* Initialize the expected header */
  587.     ph.addr = HDLC_ALL_ADDR;
  588.     ph.control = HDLC_UI;
  589.     ph.protocol = PULLCHAR(&bp);
  590.  
  591.     /* First byte of PPP protocol field may be compressed out */
  592.     if ( ph.protocol & 0x01 ) {
  593.         if (!(negotiated & LCP_N_PFC)) {
  594.             ppp_skipped( ppp_p, bp, "missing upper protocol byte" );
  595.             ppp_p->InFrame++;
  596.             return;
  597.         }
  598.     } else {
  599.         ph.protocol = (ph.protocol << 8) | PULLCHAR(&bp);
  600.  
  601.         /* Second byte of PPP protocol field must be odd */
  602.         if ( !(ph.protocol & 0x01) ) {
  603.             ppp_skipped( ppp_p, bp, "missing lower protocol byte" );
  604.             ppp_p->InFrame++;
  605.             return;
  606.         }
  607.     }
  608.  
  609.  
  610.     switch(ph.protocol) {
  611.     case PPP_IP_PROTOCOL:    /* Regular IP */
  612.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  613.             ppp_error( ppp_p, bp, "not open for IP traffic" );
  614.             ppp_p->InError++;
  615.             break;
  616.         }
  617.         ip_route(ifp,bp,0);
  618.         break;
  619.  
  620.     case PPP_COMPR_PROTOCOL:    /* Van Jacobson Compressed TCP/IP */
  621.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  622.             ppp_skipped( ppp_p, bp, "not open for Compressed TCP/IP traffic" );
  623.             ppp_p->InError++;
  624.             break;
  625.         }
  626.  
  627.         ipcp_p = ppp_p->fsm[IPcp].pdv;
  628.         if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  629.             ppp_skipped( ppp_p, bp, "Compressed TCP/IP not enabled" );
  630.             ppp_p->InError++;
  631.             break;
  632.         }
  633.  
  634.         if ( slhc_uncompress(ipcp_p->slhcp, &bp) <= 0 ) {
  635.             ppp_error( ppp_p, bp, "Compressed TCP/IP packet error" );
  636.             ppp_p->InError++;
  637.             break;
  638.         }
  639.         ip_route(ifp,bp,0);
  640.         break;
  641.  
  642.     case PPP_UNCOMP_PROTOCOL:    /* Van Jacobson Uncompressed TCP/IP */
  643.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  644.             ppp_skipped( ppp_p, bp, "not open for Uncompressed TCP/IP traffic" );
  645.             ppp_p->InError++;
  646.             break;
  647.         }
  648.  
  649.         ipcp_p = ppp_p->fsm[IPcp].pdv;
  650.         if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  651.             ppp_skipped( ppp_p, bp, "Uncompressed TCP/IP not enabled" );
  652.             ppp_p->InError++;
  653.             break;
  654.         }
  655.  
  656.         if ( slhc_remember(ipcp_p->slhcp, &bp) <= 0 ) {
  657.             ppp_error( ppp_p, bp, "Uncompressed TCP/IP packet error" );
  658.             ppp_p->InError++;
  659.             break;
  660.         }
  661.         ip_route(ifp,bp,0);
  662.         break;
  663.  
  664.     case PPP_LCP_PROTOCOL:    /* Link Control Protocol */
  665.         ppp_p->InNCP[Lcp]++;
  666.         fsm_proc(&(ppp_p->fsm[Lcp]),bp);
  667.         break;
  668.  
  669.     case PPP_PAP_PROTOCOL:    /* Password Authenticate Protocol */
  670.         if (ppp_p->phase != pppAP
  671.          && ppp_p->phase != pppREADY) {
  672.             ppp_error( ppp_p, bp, "not ready for Authentication" );
  673.             ppp_p->InError++;
  674.             break;
  675.         }
  676.         ppp_p->InNCP[Pap]++;
  677.         pap_proc(&(ppp_p->fsm[Pap]),bp);
  678.         break;
  679.  
  680.     case PPP_IPCP_PROTOCOL:    /* IP Control Protocol */
  681.         if (ppp_p->phase != pppREADY) {
  682.             ppp_error( ppp_p, bp, "not ready for IPCP traffic" );
  683.             ppp_p->InError++;
  684.             break;
  685.         }
  686.         ppp_p->InNCP[IPcp]++;
  687.         fsm_proc(&(ppp_p->fsm[IPcp]),bp);
  688.         break;
  689.  
  690.     default:
  691.         if ( ppp_p->trace )
  692.             trace_log(ppp_p->iface, "%s PPP Unknown packet protocol: %x;",
  693.                 ppp_p->iface->name,
  694.                 ph.protocol);
  695.         ppp_p->InUnknown++;
  696.  
  697.         /* Build Protocol Reject packet:
  698.          * put the header back on ...
  699.          */
  700.         if ((hbp = htonppp(&ph,bp)) == NULLBUF){
  701.             ppp_log( ppp_p, Nospace );
  702.             ppp_p->InMemory++;
  703.             return;
  704.         }
  705.         /* ... then pull off the address and control fields ... */
  706.         pull16(&hbp);
  707.         /* ... and send it as an LCP packet */
  708.         fsm_send( &(ppp_p->fsm[Lcp]), PROT_REJ, 0, hbp );
  709.         break;
  710.     };
  711. }
  712.  
  713. #ifdef TURBOC_SWITCH_BUG
  714. #pragma option -G
  715. #endif
  716.  
  717. /************************************************************************/
  718.  
  719. /* Keep track of changes in I-O status */
  720. /* (called through iface iostatus vector) */
  721. static int
  722. ppp_iostatus( ifp, command, value )
  723. struct iface *ifp;
  724. int command;
  725. int32 value;
  726. {
  727.     struct ppp_s *ppp_p = ifp->edv;
  728.  
  729.     switch ( command ) {
  730.     case PARAM_UP:
  731.         ppp_log( ppp_p, "Physical layer up" );
  732.  
  733.         if ( ppp_p->phase == pppDEAD ) {
  734.             ppp_p->phase = pppLCP;
  735.         }
  736.         fsm_start( &(ppp_p->fsm[Lcp]) );
  737.         return 0;
  738.  
  739.     case PARAM_DOWN:
  740.         ppp_log( ppp_p, "Physical layer down" );
  741.  
  742.         fsm_down( &(ppp_p->fsm[Lcp]) );
  743.         ppp_p->phase = pppDEAD;
  744.         return 0;
  745.     };
  746.     return -1;
  747. }
  748.  
  749.  
  750. static int
  751. ppp_discard(ifp,bp)
  752. struct iface *ifp;
  753. struct mbuf *bp;
  754. {
  755.     struct ppp_s *ppp_p = ifp->edv;
  756.  
  757.     return fsm_send(&(ppp_p->fsm[Lcp]), DISCARD_REQ, 0, bp);
  758. }
  759.  
  760.  
  761. static int
  762. ppp_echo(ifp,bp)
  763. struct iface *ifp;
  764. struct mbuf *bp;
  765. {
  766.     struct ppp_s *ppp_p = ifp->edv;
  767.  
  768.     return fsm_send(&(ppp_p->fsm[Lcp]), ECHO_REQ, 0, bp);
  769. }
  770.  
  771.  
  772. /****************************************************************************/
  773. /* Initialize PPP control structures for a Point-to-Point interface */
  774. int
  775. ppp_init(ifp)
  776. struct iface *ifp;
  777. {
  778.     struct ppp_s *ppp_p;
  779.     char *ifn;
  780.  
  781.     ppp_p = callocw(1,sizeof(struct ppp_s));
  782.  
  783.     ifp->edv = ppp_p;
  784.     ifp->iostatus = ppp_iostatus;
  785.     ifp->raw = ppp_raw;
  786.     ifp->show = ppp_show;
  787.     ifp->echo = ppp_echo;
  788.     ifp->discard = ppp_discard;
  789.  
  790.     ppp_p->iface = ifp;
  791.     ppp_p->phase = pppDEAD;
  792.  
  793.     lcp_init(ppp_p);
  794.     pap_init(ppp_p);
  795.     ipcp_init(ppp_p);
  796.  
  797.     ifp->rxproc = newproc( ifn = if_name( ifp, " receive" ),
  798.             320, ppp_recv, ifp->dev, ifp, NULL, 0);
  799.     free(ifn);
  800.     return 0;
  801. }
  802.  
  803.  
  804. int
  805. ppp_free(ifp)
  806. struct iface *ifp;
  807. {
  808.     struct ppp_s *ppp_p = ifp->edv;
  809.     int fsmi;
  810.  
  811.     alert( ifp->rxproc, -1 );
  812.  
  813.     for ( fsmi = Lcp; fsmi < fsmi_Size; ) {
  814.         fsm_free( &(ppp_p->fsm[fsmi++]) );
  815.     }
  816.     free( ppp_p->peername );
  817.     free( ppp_p );
  818.     return 0;
  819. }
  820.  
  821.