home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / PPP.C < prev    next >
C/C++ Source or Header  |  1992-05-15  |  22KB  |  827 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 "n8250.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, bp)
  111. struct ppp_hdr *ppp;
  112. struct mbuf *bp;
  113. {
  114.     register char *cp;
  115.  
  116.     /* Prepend header onto packet data */
  117.     bp = pushdown(bp, PPP_HDR_LEN);
  118.  
  119.     /* Load header with proper values */
  120.     cp = bp->data;
  121.     *cp++ = ppp->addr;
  122.     *cp++ = ppp->control;
  123.     cp = put16(cp,ppp->protocol);
  124.  
  125.     return bp;
  126. }
  127.  
  128.  
  129. /************************************************************************/
  130. /* General log routine */
  131. static void
  132. ppp_log( ppp_p, comment )
  133. struct ppp_s *ppp_p;
  134. char *comment;
  135. {
  136.     if (ppp_p->trace)
  137.         trace_log(ppp_p->iface,"%s PPP %s",
  138.             ppp_p->iface->name,
  139.             comment);
  140. }
  141.  
  142.  
  143. /* Bad packet */
  144. static void
  145. ppp_error(ppp_p, bp, comment)
  146. struct ppp_s *ppp_p;
  147. struct mbuf *bp;
  148. char *comment;
  149. {
  150.     free_p(bp);
  151.     ppp_log( ppp_p, comment );
  152. }
  153.  
  154.  
  155. /* Unknown type input packet */
  156. static void
  157. ppp_skipped(ppp_p, bp, comment)
  158. struct ppp_s *ppp_p;
  159. struct mbuf *bp;
  160. char *comment;
  161. {
  162.     struct ipcp_s *ipcp_p;
  163.  
  164.     if ( (ipcp_p = ppp_p->fsm[IPcp].pdv) != NULL ) {
  165.         slhc_toss( ipcp_p->slhcp );
  166.     }
  167.     ppp_error( ppp_p, bp, comment );
  168. }
  169.  
  170.  
  171. /****************************************************************************/
  172. /* Send IP datagram with Point-to-Point Protocol */
  173. int
  174. ppp_send(bp,ifp,gateway,tos)
  175. struct mbuf *bp;    /* Buffer to send */
  176. struct iface *ifp;    /* Pointer to interface control block */
  177. int32 gateway;        /* Ignored (PPP is point-to-point) */
  178. int tos;
  179. {
  180.     struct ppp_s *ppp_p;
  181.     struct ipcp_s *ipcp_p;
  182.     int protocol = PPP_IP_PROTOCOL;
  183.  
  184.     if (ifp == NULLIF
  185.      || (ppp_p = ifp->edv) == NULL) {
  186.         free_p(bp);
  187.         return -1;
  188.     }
  189.  
  190.     if (ppp_p->fsm[IPcp].state != fsmOPENED) {
  191.         ppp_error( ppp_p, bp, "not open for IP traffic" );
  192.         ppp_p->OutError++;
  193.         return -1;
  194.     }
  195.  
  196.     ipcp_p = ppp_p->fsm[IPcp].pdv;
  197.     if (ipcp_p->remote.work.negotiate & IPCP_N_COMPRESS) {
  198.         /* Attempt IP/TCP header compression */
  199.         switch ( slhc_compress(ipcp_p->slhcp, &bp,
  200.             ipcp_p->remote.work.slot_compress) ) {
  201.         case SL_TYPE_IP:
  202.             protocol = PPP_IP_PROTOCOL;
  203.             break;
  204.         case SL_TYPE_COMPRESSED_TCP:
  205.             protocol = PPP_COMPR_PROTOCOL;
  206.             break;
  207.         case SL_TYPE_UNCOMPRESSED_TCP:
  208.             protocol = PPP_UNCOMP_PROTOCOL;
  209.             break;
  210.         default:
  211.             ppp_error( ppp_p, bp, "bad IP packet" );
  212.             ppp_p->OutError++;
  213.             return -1;
  214.         };
  215.     }
  216.     return (*ifp->output)(ifp,NULLCHAR,NULLCHAR,protocol,bp);
  217. }
  218.  
  219.  
  220. /* Send a packet with PPP header */
  221. int
  222. ppp_output(ifp,dest,source,protocol,data)
  223. struct iface *ifp;    /* Pointer to interface control block */
  224. char *dest;        /* Dest addr (ignored; PPP is point-to-point) */
  225. char *source;        /* Source addr (ignored; PPP is point-to-point) */
  226. int16 protocol;        /* PPP Protocol Type field */
  227. struct mbuf *data;    /* Actual data to be sent */
  228. {
  229.     struct ppp_s *ppp_p;
  230.     struct mbuf *bp;
  231.     struct ppp_hdr hdr;
  232.  
  233.     if (ifp == NULLIF
  234.      || (ppp_p = ifp->edv) == NULL) {
  235.         free_p(data);
  236.         return -1;
  237.     }
  238.  
  239.     if (ppp_p->phase == pppDEAD) {
  240.         ppp_error( ppp_p, data, "line not up" );
  241.         ppp_p->OutError++;
  242.         return -1;
  243.     }
  244.  
  245.     hdr.addr = HDLC_ALL_ADDR;
  246.     hdr.control = HDLC_UI;
  247.     hdr.protocol = protocol;
  248.  
  249.     if ((bp = htonppp(&hdr, data)) == NULLBUF) {
  250.         ppp_log( ppp_p, Nospace );
  251.         ppp_p->OutMemory++;
  252.         return -1;
  253.     }
  254.  
  255.     return (*ifp->raw)(ifp,bp);
  256. }
  257.  
  258.  
  259. /* Encode a raw packet in PPP framing, put on link output queue */
  260. static int
  261. ppp_raw(ifp,bp)
  262. struct iface *ifp;
  263. struct mbuf *bp;
  264. {
  265.     struct ppp_s *ppp_p = ifp->edv;
  266.     struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  267.     int full_lcp, full_ac, full_p;
  268.     int16 calc_fcs = HDLC_FCS_START;
  269.     int32 accm = LCP_ACCM_DEFAULT;
  270.     struct ppp_hdr ph;
  271.     int len = PPP_HDR_LEN;
  272.     struct mbuf *vbp;
  273.     register char *cp;
  274.     register int c;
  275.  
  276.     dump(ifp,IF_TRACE_OUT,bp);
  277.     ppp_p->OutTxOctetCount += len_p(bp) + 2;  /* count FCS bytes */
  278.     ifp->rawsndcnt++;
  279.     ifp->lastsent = secclock();
  280.  
  281.     /* Get the HDLC/PPP header, without actually pulling mbuf up */
  282.     if ( bp == NULLBUF || bp->cnt < PPP_HDR_LEN ) {
  283.         ppp_error( ppp_p, bp, "link header missing" );
  284.         ppp_p->OutError++;
  285.         return -1;
  286.     }
  287.  
  288.     ph.addr = bp->data[0];
  289.     ph.control = bp->data[1];
  290.     ph.protocol = get16(&bp->data[2]);
  291.  
  292.     if ( ( full_lcp = (ph.protocol == PPP_LCP_PROTOCOL) ) == 0
  293.      && (lcp_p->remote.work.negotiate & LCP_N_ACCM) ) {
  294.         accm = lcp_p->remote.work.accm;
  295.     }
  296.  
  297.     if ( ( full_p = ( full_lcp
  298.             ||  !(lcp_p->remote.work.negotiate & LCP_N_PFC)
  299.             ||  ph.protocol >= 0x00ff ) )
  300.         == 0 ) {
  301.         --len;
  302.     }
  303.  
  304.     /* Discard HDLC address and control fields if possible */
  305.     if ( ( full_ac = ( full_lcp
  306.             ||  !(lcp_p->remote.work.negotiate & LCP_N_ACFC) ) )
  307.         == 0 ) {
  308.         len -= 2;
  309.     }
  310.  
  311.     /* Load header with proper values */
  312.     bp->cnt -= PPP_HDR_LEN - len;
  313.     cp = (bp->data += PPP_HDR_LEN - len);
  314.  
  315.     if ( full_ac ) {
  316.         *cp++ = ph.addr;
  317.         *cp++ = ph.control;
  318.     }
  319.     if ( full_p )
  320.         *cp++ = (ph.protocol >> 8);
  321.     *cp++ = (ph.protocol & 0x00ff);
  322.  
  323.     /* Allocate output mbuf that's twice as long as the packet.
  324.      * This is a worst-case guess (consider a packet full of HDLC_FLAGs!)
  325.      */
  326.     if ((vbp = alloc_mbuf((int16)(2*len_p(bp) + HDLC_ENVLEN))) == NULLBUF) {
  327.         ppp_error( ppp_p, bp, Nospace );
  328.         ppp_p->OutMemory++;
  329.         return -1;
  330.     }
  331.     cp = vbp->data;
  332.  
  333.     /* No need to send an opening flag if the previous packet is still
  334.      * being transmitted.
  335.      */
  336.     if ( Asy[ifp->dev].dma.busy == 0 ) {
  337.         /* Flush out any line garbage */
  338.         *cp++ = HDLC_FLAG;
  339.         ppp_p->OutOpenFlag++;
  340.     }
  341.  
  342.     /* Copy input to output, escaping special characters */
  343.     while ((c = PULLCHAR(&bp)) != -1) {
  344.         /* Fold char value into FCS calculated so far */
  345.         calc_fcs = pppfcs(calc_fcs, c);
  346.  
  347.         if ( ((c < SP_CHAR) && (accm & (1L << c)))
  348.             || (c == HDLC_ESC_ASYNC)
  349.             || (c == HDLC_FLAG)) {
  350.             *cp++ = HDLC_ESC_ASYNC;
  351.             *cp++ = (c ^ HDLC_ESC_COMPL);
  352.         } else {
  353.             *cp++ = c;
  354.         }
  355.     }
  356.  
  357.     /* Final FCS calculation */
  358.     calc_fcs ^= 0xffff;
  359.     c = (calc_fcs & 0x00ff);    /* Least significant byte first */
  360.     if ( ((c < SP_CHAR) && (accm & (1L << c)))
  361.          ||(c == HDLC_ESC_ASYNC)
  362.          ||(c == HDLC_FLAG)) {
  363.         *cp++ = HDLC_ESC_ASYNC;
  364.         *cp++ = (c ^ HDLC_ESC_COMPL);
  365.     } else {
  366.         *cp++ = c;
  367.     }
  368.     c = (calc_fcs >> 8);        /* Most significant byte next */
  369.     if ( ((c < SP_CHAR) && (accm & (1L << c)))
  370.          ||(c == HDLC_ESC_ASYNC)
  371.          ||(c == HDLC_FLAG)) {
  372.         *cp++ = HDLC_ESC_ASYNC;
  373.         *cp++ = (c ^ HDLC_ESC_COMPL);
  374.     } else {
  375.         *cp++ = c;
  376.     }
  377.  
  378.     /* Tie off the packet */
  379.     *cp++ = HDLC_FLAG;
  380.     vbp->cnt = cp - vbp->data;
  381.  
  382.     if (ifp->trace & IF_TRACE_RAW)
  383.         raw_dump(ifp,IF_TRACE_OUT,vbp);
  384.     return asy_send(ifp->dev,vbp);
  385. }
  386.  
  387.  
  388. /****************************************************************************/
  389. /* Packetize PPP input from device */
  390. /* (process started by ppp_init) */
  391. void
  392. ppp_recv(dev,p1,p2)
  393. int dev;
  394. void *p1;
  395. void *p2;
  396. {
  397.     struct iface *ifp = p1;
  398.     struct ppp_s *ppp_p = ifp->edv;
  399.     int32 accm = LCP_ACCM_DEFAULT;
  400.     int16 calc_fcs = HDLC_FCS_START;
  401.     struct mbuf *raw_bp = NULLBUF;
  402.     struct mbuf *head_bp = NULLBUF;
  403.     register struct mbuf *tail_bp = NULLBUF;
  404.     char *cp;            /* next byte in tail mbuf */
  405.     register int mode = FALSE;
  406.     register int c;
  407.  
  408.     while ( (c = get_asy(dev)) != -1 ) {
  409. #ifdef PPP_DEBUG_RAW
  410.         if (ifp->trace & IF_TRACE_RAW) {
  411.             if ( raw_bp != NULLBUF
  412.               || (raw_bp = alloc_mbuf( LCP_MRU_HI * 2 )) != NULLBUF ) {
  413.                 *raw_bp->data++ = c;
  414.                 raw_bp->cnt++;
  415.                 if ( raw_bp->cnt != 1 && c == HDLC_FLAG ) {
  416.                     raw_bp->data = (char *)(raw_bp + 1);
  417.                     raw_dump( ifp, IF_TRACE_IN, raw_bp );
  418.                     raw_bp->cnt = 0;
  419.                 }
  420.             }
  421.         }
  422. #endif
  423.         if ( c == HDLC_FLAG ) {
  424.             if ( mode & PPP_ESCAPED ) {
  425.                 ppp_skipped( ppp_p, head_bp,
  426.                     "deliberate cancellation" );
  427.                 ppp_p->InFrame++;
  428.             } else if ( mode & PPP_TOSS ) {
  429.                 free_p( head_bp );
  430.             } else if ( head_bp != NULLBUF ) {
  431.                 if ( calc_fcs != HDLC_FCS_FINAL ) {
  432.                     ppp_skipped( ppp_p, head_bp,
  433.                         "checksum error" );
  434.                     ppp_p->InChecksum++;
  435.                 } else {
  436.                     /* trim off FCS bytes */
  437.                     trim_mbuf(&head_bp, len_p(head_bp)-2);
  438.  
  439.                     net_route(ifp,head_bp);
  440.                     /* Especially on slow machines, serial I/O can be quite
  441.                      * compute intensive, so release the machine before we
  442.                      * do the next packet.  This will allow this packet to
  443.                      * go on toward its ultimate destination. [Karn]
  444.                      */
  445.                     pwait(NULL);
  446.                 }
  447.             } else {
  448.                 ppp_p->InOpenFlag++;
  449.             }
  450.  
  451.             /* setup for next buffer */
  452.             mode = FALSE;
  453.             head_bp = tail_bp = NULLBUF;
  454.             calc_fcs = HDLC_FCS_START;
  455.             accm = LCP_ACCM_DEFAULT;
  456.  
  457.             /* Use negotiated values if LCP finished */
  458.             if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  459.                 struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  460.  
  461.                 if (lcp_p->local.work.negotiate & LCP_N_ACCM) {
  462.                     accm = lcp_p->local.work.accm;
  463.                 }
  464.             }
  465. #ifdef PPP_DEBUG_RAW
  466.             if (!(ifp->trace & IF_TRACE_RAW)) {
  467.                 if ( raw_bp != NULLBUF ) {
  468.                     free_p( raw_bp );
  469.                     raw_bp = NULLBUF;
  470.                 }
  471.             }
  472. #endif
  473.             continue;
  474.         }
  475.  
  476.         /* We reach here for every byte inside a frame.
  477.          * (The order of the following tests is important.)
  478.          * Discard spurious control characters.
  479.          * Check for escape sequence.
  480.          * (Allow escaped escape.)
  481.          */
  482.         if ( c < SP_CHAR && (accm & (1L << c)) ) {
  483.             continue;
  484.         } else if ( mode & PPP_ESCAPED ) {
  485.             mode &= ~PPP_ESCAPED;
  486.             c ^= HDLC_ESC_COMPL;
  487.         } else if ( c == HDLC_ESC_ASYNC ) {
  488.             mode |= PPP_ESCAPED;
  489.             continue;
  490.         }
  491.  
  492.         /* We reach here with a byte for the buffer.
  493.          * Make sure there is room for it.
  494.          */
  495.         if ( tail_bp == NULLBUF ) {
  496.             if ((tail_bp = alloc_mbuf(PPP_ALLOC)) == NULLBUF) {
  497.                 ppp_skipped( ppp_p, tail_bp, Nospace );
  498.                 ppp_p->InMemory++;
  499.                 mode |= PPP_TOSS;
  500.                 continue;
  501.             }
  502.             head_bp = tail_bp;
  503.             cp = tail_bp->data;
  504.         } else if ( tail_bp->cnt >= tail_bp->size ) {
  505.             /* Current mbuf is full */
  506.             if ( (tail_bp->next = alloc_mbuf(PPP_ALLOC)) == NULLBUF ) {
  507.                 /* No memory, drop the whole packet */
  508.                 ppp_skipped( ppp_p, head_bp, Nospace );
  509.                 ppp_p->InMemory++;
  510.                 head_bp = NULLBUF;
  511.                 mode |= PPP_TOSS;
  512.                 continue;
  513.             }
  514.             tail_bp = tail_bp->next;
  515.             cp = tail_bp->data;
  516.         }
  517.  
  518.         /* Store the byte, increment counts */
  519.         *cp++ = c;
  520.         tail_bp->cnt++;
  521.         calc_fcs = pppfcs(calc_fcs, c);
  522.     }
  523.  
  524.     /* clean up afterward */
  525.     free_p(raw_bp);
  526.     free_p(head_bp);
  527.     ifp->rxproc = NULLPROC;
  528. }
  529.  
  530. #ifdef TURBOC_SWITCH_BUG
  531. #pragma option -G-
  532. #endif
  533.  
  534. /* Process incoming PPP packets */
  535. /* (called from network task) */
  536. void
  537. ppp_proc(ifp,bp)
  538. struct iface *ifp;
  539. struct mbuf *bp;
  540. {
  541.     struct ppp_s *ppp_p;
  542.     struct ipcp_s *ipcp_p;
  543.     struct ppp_hdr ph;
  544.     struct mbuf *hbp;
  545.     int16 negotiated = FALSE;
  546.  
  547.     if ( ifp == NULLIF ) {
  548.         log(-1, "ppp_proc: missing iface" );
  549.         return;
  550.     }
  551.     if ( bp == NULLBUF ) {
  552.         trace_log(ifp, "ppp_proc: missing buffer" );
  553.         return;
  554.     }
  555.  
  556.     ppp_p = ifp->edv;
  557.     ppp_p->InRxOctetCount += len_p(bp) + 2;   /* count FCS bytes */
  558.  
  559.     /* Use negotiated values if LCP finished */
  560.     if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  561.         struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  562.  
  563.         negotiated = lcp_p->local.work.negotiate;
  564.     }
  565.  
  566.     /* HDLC address and control fields may be compressed out */
  567.     if ((byte_t)bp->data[0] != HDLC_ALL_ADDR) {
  568.         if (!(negotiated & LCP_N_ACFC)) {
  569.             ppp_skipped( ppp_p, bp, "missing ALL address" );
  570.             ppp_p->InFrame++;
  571.             return;
  572.         }
  573.     } else if ((byte_t)bp->data[1] != HDLC_UI) {
  574.         if (!(negotiated & LCP_N_ACFC)
  575.          || !(negotiated & LCP_N_PFC)) {
  576.             ppp_skipped( ppp_p, bp, "missing UI" );
  577.             ppp_p->InFrame++;
  578.             return;
  579.         }
  580.     } else {
  581.         /* skip address/control fields */
  582.         pull16(&bp);
  583.     }
  584.  
  585.     /* Initialize the expected header */
  586.     ph.addr = HDLC_ALL_ADDR;
  587.     ph.control = HDLC_UI;
  588.     ph.protocol = PULLCHAR(&bp);
  589.  
  590.     /* First byte of PPP protocol field may be compressed out */
  591.     if ( ph.protocol & 0x01 ) {
  592.         if (!(negotiated & LCP_N_PFC)) {
  593.             ppp_skipped( ppp_p, bp, "missing upper protocol byte" );
  594.             ppp_p->InFrame++;
  595.             return;
  596.         }
  597.     } else {
  598.         ph.protocol = (ph.protocol << 8) | PULLCHAR(&bp);
  599.  
  600.         /* Second byte of PPP protocol field must be odd */
  601.         if ( !(ph.protocol & 0x01) ) {
  602.             ppp_skipped( ppp_p, bp, "missing lower protocol byte" );
  603.             ppp_p->InFrame++;
  604.             return;
  605.         }
  606.     }
  607.  
  608.  
  609.     switch(ph.protocol) {
  610.     case PPP_IP_PROTOCOL:    /* Regular IP */
  611.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  612.             ppp_error( ppp_p, bp, "not open for IP traffic" );
  613.             ppp_p->InError++;
  614.             break;
  615.         }
  616.         ip_route(ifp,bp,0);
  617.         break;
  618.  
  619.     case PPP_COMPR_PROTOCOL:    /* Van Jacobson Compressed TCP/IP */
  620.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  621.             ppp_skipped( ppp_p, bp, "not open for Compressed TCP/IP traffic" );
  622.             ppp_p->InError++;
  623.             break;
  624.         }
  625.  
  626.         ipcp_p = ppp_p->fsm[IPcp].pdv;
  627.         if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  628.             ppp_skipped( ppp_p, bp, "Compressed TCP/IP not enabled" );
  629.             ppp_p->InError++;
  630.             break;
  631.         }
  632.  
  633.         if ( slhc_uncompress(ipcp_p->slhcp, &bp) <= 0 ) {
  634.             ppp_error( ppp_p, bp, "Compressed TCP/IP packet error" );
  635.             ppp_p->InError++;
  636.             break;
  637.         }
  638.         ip_route(ifp,bp,0);
  639.         break;
  640.  
  641.     case PPP_UNCOMP_PROTOCOL:    /* Van Jacobson Uncompressed TCP/IP */
  642.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  643.             ppp_skipped( ppp_p, bp, "not open for Uncompressed TCP/IP traffic" );
  644.             ppp_p->InError++;
  645.             break;
  646.         }
  647.  
  648.         ipcp_p = ppp_p->fsm[IPcp].pdv;
  649.         if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  650.             ppp_skipped( ppp_p, bp, "Uncompressed TCP/IP not enabled" );
  651.             ppp_p->InError++;
  652.             break;
  653.         }
  654.  
  655.         if ( slhc_remember(ipcp_p->slhcp, &bp) <= 0 ) {
  656.             ppp_error( ppp_p, bp, "Uncompressed TCP/IP packet error" );
  657.             ppp_p->InError++;
  658.             break;
  659.         }
  660.         ip_route(ifp,bp,0);
  661.         break;
  662.  
  663.     case PPP_LCP_PROTOCOL:    /* Link Control Protocol */
  664.         ppp_p->InNCP[Lcp]++;
  665.         fsm_proc(&(ppp_p->fsm[Lcp]),bp);
  666.         break;
  667.  
  668.     case PPP_PAP_PROTOCOL:    /* Password Authenticate Protocol */
  669.         if (ppp_p->phase != pppAP
  670.          && ppp_p->phase != pppREADY) {
  671.             ppp_error( ppp_p, bp, "not ready for Authentication" );
  672.             ppp_p->InError++;
  673.             break;
  674.         }
  675.         ppp_p->InNCP[Pap]++;
  676.         pap_proc(&(ppp_p->fsm[Pap]),bp);
  677.         break;
  678.  
  679.     case PPP_IPCP_PROTOCOL:    /* IP Control Protocol */
  680.         if (ppp_p->phase != pppREADY) {
  681.             ppp_error( ppp_p, bp, "not ready for IPCP traffic" );
  682.             ppp_p->InError++;
  683.             break;
  684.         }
  685.         ppp_p->InNCP[IPcp]++;
  686.         fsm_proc(&(ppp_p->fsm[IPcp]),bp);
  687.         break;
  688.  
  689.     default:
  690.         if ( ppp_p->trace )
  691.             trace_log(ppp_p->iface, "%s PPP Unknown packet protocol: %x;",
  692.                 ppp_p->iface->name,
  693.                 ph.protocol);
  694.         ppp_p->InUnknown++;
  695.  
  696.         /* Build Protocol Reject packet:
  697.          * put the header back on ...
  698.          */
  699.         if ((hbp = htonppp(&ph,bp)) == NULLBUF){
  700.             ppp_log( ppp_p, Nospace );
  701.             ppp_p->InMemory++;
  702.             return;
  703.         }
  704.         /* ... then pull off the address and control fields ... */
  705.         pull16(&hbp);
  706.         /* ... and send it as an LCP packet */
  707.         fsm_send( &(ppp_p->fsm[Lcp]), PROT_REJ, 0, hbp );
  708.         break;
  709.     };
  710. }
  711.  
  712. #ifdef TURBOC_SWITCH_BUG
  713. #pragma option -G
  714. #endif
  715.  
  716. /************************************************************************/
  717.  
  718. /* Keep track of changes in I-O status */
  719. /* (called through iface iostatus vector) */
  720. static int
  721. ppp_iostatus( ifp, command, value )
  722. struct iface *ifp;
  723. int command;
  724. int32 value;
  725. {
  726.     struct ppp_s *ppp_p = ifp->edv;
  727.  
  728.     switch ( command ) {
  729.     case PARAM_UP:
  730.         ppp_log( ppp_p, "Physical layer up" );
  731.  
  732.         if ( ppp_p->phase == pppDEAD ) {
  733.             ppp_p->phase = pppLCP;
  734.         }
  735.         fsm_start( &(ppp_p->fsm[Lcp]) );
  736.         return 0;
  737.  
  738.     case PARAM_DOWN:
  739.         ppp_log( ppp_p, "Physical layer down" );
  740.  
  741.         fsm_down( &(ppp_p->fsm[Lcp]) );
  742.         ppp_p->phase = pppDEAD;
  743.         return 0;
  744.     };
  745.     return -1;
  746. }
  747.  
  748.  
  749. static int
  750. ppp_discard(ifp,bp)
  751. struct iface *ifp;
  752. struct mbuf *bp;
  753. {
  754.     struct ppp_s *ppp_p = ifp->edv;
  755.  
  756.     return fsm_send(&(ppp_p->fsm[Lcp]), DISCARD_REQ, 0, bp);
  757. }
  758.  
  759.  
  760. static int
  761. ppp_echo(ifp,bp)
  762. struct iface *ifp;
  763. struct mbuf *bp;
  764. {
  765.     struct ppp_s *ppp_p = ifp->edv;
  766.  
  767.     return fsm_send(&(ppp_p->fsm[Lcp]), ECHO_REQ, 0, bp);
  768. }
  769.  
  770.  
  771. /****************************************************************************/
  772. /* Initialize PPP control structures for a Point-to-Point interface */
  773. int
  774. ppp_init(ifp,vj)
  775. struct iface *ifp;
  776. int vj;    /* Unused */
  777. {
  778.     struct ppp_s *ppp_p;
  779.     char *ifn;
  780.  
  781.     /* Setup for Point-to-Point Protocol */
  782.     setencap(ifp,"PPP");
  783.     ifp->ioctl = asy_ioctl;
  784.     ifp->flags = FALSE;
  785.  
  786.  
  787.     ppp_p = callocw(1,sizeof(struct ppp_s));
  788.  
  789.     ifp->edv = ppp_p;
  790.     ifp->iostatus = ppp_iostatus;
  791.     ifp->raw = ppp_raw;
  792.     ifp->show = ppp_show;
  793.     ifp->echo = ppp_echo;
  794.     ifp->discard = ppp_discard;
  795.  
  796.     ppp_p->iface = ifp;
  797.     ppp_p->phase = pppDEAD;
  798.  
  799.     lcp_init(ppp_p);
  800.     pap_init(ppp_p);
  801.     ipcp_init(ppp_p);
  802.  
  803.     ifp->rxproc = newproc( ifn = if_name( ifp, " receive" ),
  804.             320, ppp_recv, ifp->dev, ifp, NULL, 0);
  805.     free(ifn);
  806.     return 0;
  807. }
  808.  
  809.  
  810. int
  811. ppp_free(ifp)
  812. struct iface *ifp;
  813. {
  814.     struct ppp_s *ppp_p = ifp->edv;
  815.     int fsmi;
  816.  
  817.     alert( ifp->rxproc, 1 );
  818.  
  819.     for ( fsmi = Lcp; fsmi < fsmi_Size; ) {
  820.         fsm_free( &(ppp_p->fsm[fsmi++]) );
  821.     }
  822.     free( ppp_p->peername );
  823.     free( ppp_p );
  824.     return 0;
  825. }
  826.  
  827.