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