/*
 *  PPP.C	-- Send and receive datagrams on serial lines with
 *		   Point-to-Point Protocol
 *
 *	12-89	-- Katie Stevens (dkstevens@ucdavis.edu)
 *		   UC Davis, Computing Services
 *	PPP.08	05-90	[ks] include PPP protocol fields for VJ TCP compression
 *	PPP.09	05-90	[ks] add UPAP negotiation and processing
 *	PPP.10	07-90	[ks] make ppp open/close/reset work properly
 *	PPP.14	08-90	[ks] change UPAP to PAP for consistency with RFC1172
 *			     add RLSD link up/down signal handling
 *			     add autobaud link speed message handling
 *			     bit-shift constants must be explicitly long if shifted more than 8 bits
 *			     must use unsigned char for comparison with SP_CHAR
 *	PPP.15	09-90 	[ks] update to KA9Q NOS v900828
 */

#include <stdio.h>
#include "global.h"
#include "mbuf.h"
#include "proc.h"
#include "iface.h"
#include "ax25.h"
#include "slcompress.h"
#include "ppp.h"
#include "slip.h"
#include "asy.h"
#include "internet.h"
#include "ip.h"
#include "config.h"
#include "trace.h"

/*#include "pktdrvr.h"*/
/*#include "trace.h"*/

/* In pppcmd.c */
extern int ppptrace;

/* Counter for PPP id field */
unsigned char pppid;


/* Routines local to this file */
static struct mbuf *htonppp __ARGS((struct ppphdr *ppp, struct mbuf *data));
/*static int pppq __ARGS((int16 dev,struct mbuf *data));*/
static struct mbuf *ppp_decode __ARGS((int16 dev,unsigned char c));
static struct mbuf *ppp_encode __ARGS((struct mbuf *bp, char flags,
			int32 ctlmap));

/*
 * FCS lookup table as generated by fcsgen.c
 */
static unsigned short fcstab[256] = {
    0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 
    0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 
    0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 
    0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 
    0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 
    0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 
    0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 
    0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 
    0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 
    0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 
    0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 
    0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 
    0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 
    0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 
    0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 
    0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 
    0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 
    0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 
    0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 
    0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 
    0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 
    0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 
    0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 
    0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 
    0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 
    0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 
    0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 
    0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 
    0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 
    0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 
    0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 
    0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
};
#define pppfcs(fcs, c)		((fcs >> 8) ^ fcstab[(fcs ^ c) & 0x00ff])
#define SP_CHAR			0x20

/****************************************************************************/

/* Convert PPP header in host form to network form */
static struct mbuf *
htonppp(ppp, data)
struct ppphdr *ppp;
struct mbuf *data;
{
    struct mbuf *bp;
    register unsigned char *cp;

    /* Prepend header onto packet data */
    if ((bp = pushdown(data, PPP_HDRLEN)) == NULLBUF)
	return NULLBUF;

    /* Load header with proper values */
    cp = (unsigned char *)bp->data;
    *cp++ = ppp->addr;
    *cp++ = ppp->control;
    cp = (unsigned char *)put16(cp,ppp->type);

    return bp;
}

/* Extract PPP header from incoming packet */
int
ntohppp(ppp, bpp)
struct ppphdr *ppp;
struct mbuf **bpp;
{
/*    ppp->addr = PULLCHAR(bpp);*/
	pullup(bpp, &ppp->addr, 1);
/*    ppp->control = PULLCHAR(bpp);*/
	pullup(bpp, &ppp->control, 1);
    ppp->type = pull16(bpp);
    return(PPP_HDRLEN);
}

/****************************************************************************/

/* Send IP datagram with Point-to-Point Protocol */
int
ppp_send(bp,iface,gateway,prec,del,tput,rel)
struct mbuf *bp;	/* Buffer to send */
struct iface *iface;	/* Pointer to interface control block */
int32 gateway;		/* Ignored (PPP is point-to-point) */
int prec;
int del;
int tput;
int rel;
{
	int type = PPP_IP_TYPE;
	register struct slip *sp;
	register struct iphdr *iph;
	register struct slcompress *scp;

	if(iface == NULLIF){
		free_p(bp);
		return -1;
	}
	sp = &Slip[iface->xdev];
	if (sp->pppio->ipcpio.ipcp_state != IPCP_OPEN) {
		free_p(bp);
		sp->pppio->snderr++;
		return -1;
	}
	if (sp->escaped & PPP_XMT_VJCOMPR) {
		/* Look for TCP packets */
		scp = sp->slcomp;
		iph = (struct iphdr *)bp->data;
		if (iph->ip_protocol == TCP_PTCL) {
			/* Attempt IP/TCP header compression */
			type = sl_compress_tcp(&bp, iph, scp, 1);
			switch(type) {
			case SL_TYPE_IP:
				type = PPP_IP_TYPE;
				break;
			case SL_TYPE_COMPRESSED_TCP:
				type = PPP_COMPR_TYPE;
				break;
			case SL_TYPE_UNCOMPRESSED_TCP:
				type = PPP_UNCOMP_TYPE;
				break;
			default:
				return -1;
			}
		} else {
			scp->sls_nontcp++;
		}
	}
	sp->pppio->sndip++;
	return (*iface->output)(iface,NULLCHAR,NULLCHAR,type,bp);
}
/* Send a packet with PPP header */
int
ppp_output(iface,dest,source,type,data)
struct iface *iface;	/* Pointer to interface control block */
char *dest;		/* Dest addr (ignored; PPP is point-to-point) */
char *source;		/* Source addr (ignored; PPP is point-to-point) */
int16 type;		/* PPP Protocol Type field */
struct mbuf *data;	/* Actual data to be sent */
{
	struct slip *sp;
	struct ppphdr ppp;
	struct mbuf *bp;

	sp = &Slip[iface->xdev];
	if (sp->pppio->state == PPP_CLOSED) {
		free_p(data);
		sp->pppio->snderr++;
		return -1;
	}
	ppp.addr = HDLC_ALL_ADDR;
	ppp.control = HDLC_UI;
	ppp.type = type;
	if ((bp = htonppp(&ppp, data)) == NULLBUF) {
		free_p(data);
		return -1;
	}
	return (*iface->raw)(iface,bp);
}
/* Encode a raw packet in PPP framing, put on link output queue, and kick
 * transmitter
 */
int
ppp_raw(iface,bp)
struct iface *iface;
struct mbuf *bp;
{
	int i;
	register struct slip *sp;
	struct mbuf *bp1;

	sp = &Slip[iface->xdev];
	dump(iface,IF_TRACE_OUT,sp->type,bp);
	/* Queue a frame on the PPP output queue and start transmitter */
	if ((bp1 = ppp_encode(bp,sp->escaped,sp->pppio->ctlmap)) == NULLBUF)
		return -1;	
	if ((iface->trace != 0) && ((iface->trace & IF_TRACE_NOBC) == 0))
		raw_dump(iface,IF_TRACE_OUT,bp1);
	sp->pppio->sndpkt++;
	return Slip[iface->xdev].send(iface->dev,bp1);
}

/* Encode a packet in PPP format */
static
struct mbuf *
ppp_encode(bp,flags,ctlmap)
struct mbuf *bp;
char flags;
int32 ctlmap;
{
	struct mbuf *lbp;	/* Mbuf containing line-ready packet */
	register unsigned char *cp;
	int c;
	unsigned char abcd;
	int16 calc_fcs;
	struct ppphdr ppp;
	int len = 1;
	char compr_ac, compr_prot;

	/* Get the HDLC/PPP header */
	ntohppp(&ppp, &bp);
	/* Discard HDLC addr and control fields if possible */
	compr_ac =  ((flags & PPP_XMT_ACCOMP)&&(ppp.type != PPP_LCP_TYPE));
	/* Compress PPP protocol field if possible */
	compr_prot = ((flags & PPP_XMT_PRCOMP) && 
		  (ppp.type < 0x100) &&
		  (ppp.type != PPP_LCP_TYPE));

	/* Calculate actual header length */
	if (!compr_ac)
		len += 2;
	if (!compr_prot)
		++len;

	/* Prepend header onto packet data */
	if ((bp = pushdown(bp, len)) == NULLBUF)
		return NULLBUF;

	/* Load header with proper values */
	cp = (unsigned char *)bp->data;
	if (!compr_ac) {
/*printf("B1 1 %x\n", (int)ppp.addr);*/
		*cp++ = ppp.addr;
		*cp++ = ppp.control;
	}
	if (!compr_prot)
		*cp++ = (ppp.type >> 8);
	*cp++ = (ppp.type & 0x00ff);

	/* Allocate output mbuf that's twice as long as the packet.
	 * This is a worst-case guess (consider a packet full of HDLC_FLAGs!)
	 */
	lbp = alloc_mbuf((int16)(2*len_p(bp) + HDLC_ENVLEN));
	if(lbp == NULLBUF){
		/* No space; drop */
		free_p(bp);
		return NULLBUF;
	}
	cp = (unsigned char *)lbp->data;

	/* Flush out any line garbage */
	*cp++ = HDLC_FLAG;

	/* Initialize FCS */
	calc_fcs = HDLC_FCS_START;


/*	while ((c = PULLCHAR(&bp)) != -1) {PULLCHARBUG*/
/* attach asy serial.device 0 ppp pp 2048 576 2400

	/* Copy input to output, escaping special characters */

	while(pullup(&bp,&abcd,1) == 1) {
		c=abcd;
		/* Fold char value into FCS calculated so far */
		calc_fcs = pppfcs(calc_fcs, c);
		if ( ((c < SP_CHAR) && (ctlmap & (1L << c)))
		    || (c == HDLC_ESC_ASYNC)
		    || (c == HDLC_FLAG)) {
/*printf("B2 1\n");*/
			*cp++ = HDLC_ESC_ASYNC;
			*cp++ = (c ^ HDLC_ESC_COMPL);
		} else {
/*printf("B2 2\n");*/
			*cp++ = c;
		}
	}

	/* Final FCS calculation */
	calc_fcs ^= 0xffff;
	c = (calc_fcs & 0x00ff);	/* Least significant byte first */
	if ( ((c < SP_CHAR) && (ctlmap & (1L << c)))
	     ||(c == HDLC_ESC_ASYNC)
	     ||(c == HDLC_FLAG)) {
		*cp++ = HDLC_ESC_ASYNC;
		*cp++ = (c ^ HDLC_ESC_COMPL);
	} else {
		*cp++ = c;
	}
	c = (calc_fcs >> 8);		/* Most significant byte next */
	if ( ((c < SP_CHAR) && (ctlmap & (1L << c)))
	     ||(c == HDLC_ESC_ASYNC)
	     ||(c == HDLC_FLAG)) {
		*cp++ = HDLC_ESC_ASYNC;
		*cp++ = (c ^ HDLC_ESC_COMPL);
	} else {
		*cp++ = c;
	}

	/* Tye off the packet */
	*cp++ = HDLC_FLAG;
	lbp->cnt = cp - lbp->data;
	return lbp;
}
/* Process incoming bytes in PPP format
 * When a buffer is complete, return it; otherwise NULLBUF
 */
static
struct mbuf *
ppp_decode(dev,c)
int16 dev;		/* PPP unit number */
unsigned char c;	/* Incoming character */
{
	struct mbuf *bp;
	register struct slip *sp;
	register struct pppctl *pppiop;

	sp = &Slip[dev];
	pppiop = Slip[dev].pppio;
	switch(c){
	case HDLC_FLAG:				/* Start/end of packet */
		/* Wrap it up if end of a packet */
		if (sp->rbp != NULLBUF) {
			if (pppiop->calc_fcs == HDLC_FCS_FINAL) {
				/* Good packet, trim off FCS bytes */
				trim_mbuf(&sp->rbp, (len_p(sp->rbp)-2));
			} else {
				/* FCS doesnt match; discard packet */
/*putchar('\007');*/
				free_p(sp->rbp);
				sp->rbp = NULLBUF;
				sp->rcnt = 0;
				pppiop->csumerr++;
			}
		}
		/* Prepare for next packet */
		bp = sp->rbp;
		sp->escaped &= ~PPP_BIT_BUCKET;
		sp->escaped &= ~PPP_ESCAPED;
		sp->rbp = NULLBUF;
		sp->rcnt = 0;
		pppiop->calc_fcs = HDLC_FCS_START;
		return bp;	/* Will be NULLBUF if empty frame */

	case HDLC_ESC_ASYNC:			/* Async HDLC escape char */
		sp->escaped |= PPP_ESCAPED;
		return NULLBUF;
	}

	if(sp->escaped & PPP_BIT_BUCKET)
		/* Eat bytes til end of packet */
		return NULLBUF;

	if(sp->escaped & PPP_ESCAPED){
		/* Translate 2-char escape sequence back to original char */
		sp->escaped &= ~PPP_ESCAPED;
		c ^= HDLC_ESC_COMPL;
	}
	/* We reach here with a character for the buffer;
	 * make sure there's space for it
	 */
	if(sp->rbp == NULLBUF){
		/* Allocate first mbuf for new packet */
		if((sp->rbp1 = sp->rbp = alloc_mbuf(PPP_ALLOC)) == NULLBUF)
			return NULLBUF; /* No memory, drop */
		sp->rcp = sp->rbp->data;
	} else if(sp->rbp1->cnt == PPP_ALLOC){
		/* Current mbuf is full; link in another */
		if((sp->rbp1->next = alloc_mbuf(PPP_ALLOC)) == NULLBUF){
			/* No memory, drop whole thing */
			free_p(sp->rbp);
			sp->rbp = NULLBUF;
			sp->rcnt = 0;
			return NULLBUF;
		}
		sp->rbp1 = sp->rbp1->next;
		sp->rcp = sp->rbp1->data;
	}
	/* Store the character, increment fragment and total
	 * byte counts
	 */
	/* HDLC address and control fields may be compressed out */
	if ((sp->rcnt == 0)&&(c != HDLC_ALL_ADDR)) {
		if (sp->escaped & PPP_RCV_ACCOMP) {
			*sp->rcp++ = HDLC_ALL_ADDR;
			*sp->rcp++ = HDLC_UI;
			sp->rbp1->cnt += 2;
			sp->rcnt += 2;
		} else {
			sp->escaped |= PPP_BIT_BUCKET;
			free_p(sp->rbp);
			sp->rbp = NULLBUF;
			sp->rcnt = 0;
			pppiop->rcverr++;

printf("rcverr: HDLC ALL ADDR compr not enabled\n");
fflush(stdout);

			return NULLBUF;
		}
	}
	/* HDLC control field must be present if not addr/ctl compression */
	if ((sp->rcnt == 1)&&(c != HDLC_UI)) {
		sp->escaped |= PPP_BIT_BUCKET;
		free_p(sp->rbp);
		sp->rbp = NULLBUF;
		sp->rcnt = 0;
		pppiop->rcverr++;

printf("rcverr: HDLC UI compr not enabled\n");
fflush(stdout);

		return NULLBUF;
	}
	/* First byte of PPP protocol field may be compressed out */
	if ((sp->rcnt == 2)&&((c & 1) == 1)) {
		if (sp->escaped & PPP_RCV_PRCOMP) {
			*sp->rcp++ = 0x00;
			sp->rbp1->cnt++;
			sp->rcnt++;
		} else {
			sp->escaped |= PPP_BIT_BUCKET;
			free_p(sp->rbp);
			sp->rbp = NULLBUF;
			sp->rcnt = 0;
			pppiop->rcverr++;

printf("rcverr: not PROT compr not enabled\n");
fflush(stdout);

			return NULLBUF;
		}
	}
	/* Second byte of PPP protocol field must be odd */
	if ((sp->rcnt == 3)&&((c & 1) == 0)) {
		sp->escaped |= PPP_BIT_BUCKET;
		free_p(sp->rbp);
		sp->rbp = NULLBUF;
		sp->rcnt = 0;
		pppiop->rcverr++;

printf("rcverr: second PPP protocol byte must be odd\n");
fflush(stdout);

		return NULLBUF;
	}

	/* Now add the actual character */
	*sp->rcp++ = c;
	sp->rbp1->cnt++;
	sp->rcnt++;
	pppiop->calc_fcs = pppfcs(pppiop->calc_fcs, c);
	return NULLBUF;
}
/* Process PPP line input */
void
ppp_recv(dev,p1,p2)
int dev;
void *p1;
void *p2;
{
	unsigned char c;
	struct mbuf *bp,*nbp;
	struct phdr *phdr;
	struct slip *sp;

	sp = &Slip[dev];

	for(;;){
		c = sp->get(sp->iface->dev);
		if((bp = ppp_decode(dev,c)) == NULLBUF)

			continue;	/* More to come */

		if((nbp = pushdown(bp,sizeof(struct phdr))) == NULLBUF){
			free_p(bp);
			continue;
		}
		phdr = (struct phdr *)nbp->data;
		phdr->iface = sp->iface;
		phdr->type = sp->type;
		enqueue(&Hopper,nbp);
	}
}

/****************************************************************************/
#ifdef COMMENT
/* Wait for autobaud message to set line speed then switch to regular PPP */
void
ppp_autobaud(dev,p1,p2)
int dev;
void *p1;
void *p2;
{
	struct slip *sp;
	struct iface *ifp;

	sp = &Slip[dev];
	ifp = sp->iface;
	/* Wait for an autobaud message */
	asy_autobaud(ifp->dev);

	/* Ready for PPP packets */
	ifp->proc = newproc("ppp recv", 256, ppp_recv, ifp->xdev, NULL, NULL);

	/* The LCP state machine has been waiting for us */
	sp->pppio->state = PPP_CLOSED;
	lcp_reset(sp->pppio);
	lcp_start(sp);

	/* Terminate this process */
	return;
}

/* Keep track of RLSD signal; if RLSD is down, physical layer is down;
 * if RLSD is asserted, physical layer is ready for traffic
 */
void
ppp_rlsd(dev,p1,p2)
int dev;
void *p1;
void *p2;
{
	int last_rlsd = 1;
	int i;
	unsigned long autospeed;
	struct slip *sp;
	struct pppctl *pppiop;
	struct ipcpctl *ipcpiop;

	autospeed = (unsigned long)p1;
	sp = &Slip[dev];
	pppiop = sp->pppio;
	for ( ; ; ) {
		/* Wait for RLSD to change */
		if (last_rlsd == 1) {
			/* Down now, wait for RSLD to be asserted */
			i = sp->get_rlsd(sp->iface->dev, 100);
		} else {
			/* Asserted now, wait for RSLD to be dropped */
			i = sp->get_rlsd(sp->iface->dev, 1);
		}

		last_rlsd = i;
/*
printf("ppp_rlsd: rlsd state change: %d\n",last_rlsd);
 */
		if (last_rlsd == 100) {
			/* Physical layer now open for traffic */
			if (ppptrace)
				log(-1,"%s: PPP/RLSD: Physical layer ready",
					sp->iface->name);
			/* RLSD signal is asserted */
			if (pppiop->state == PPP_PL_DOWN) {
				if (autospeed == 0L) {
					pppiop->state = PPP_CLOSED;
					lcp_reset(pppiop);
					lcp_start(sp);
				} else {
					pppiop->state = PPP_AUTOBAUD;
				}
			}
		} else {
			/* Physical layer is down */
			if (ppptrace)
				log(-1,"%s: PPP/RLSD: Physical layer down",
					sp->iface->name);
			/* RLSD signal not asserted */
			if ((pppiop->state != PPP_CLOSED) &&
			    (pppiop->state != PPP_AUTOBAUD)) {
				ipcpiop = &(pppiop->ipcpio);
				if (ipcpiop->ipcp_state == IPCP_OPEN) {
					/* Remove routing entry from table */
					rt_drop(ipcpiop->attempt_dest,
						(unsigned int)32);
				}
				/* Blast any layers that are open */
				ipcp_reset(sp);
				pap_init(sp);
				lcp_reset(pppiop);
			}
			/* Physical layer closed to all traffic */
			pppiop->state = PPP_PL_DOWN;
			if (autospeed != 0L) {
				killproc(sp->iface->proc);
				sp->iface->proc = NULLPROC;
				sp->iface->proc = newproc("ppp autobaud", 256,
							  ppp_autobaud, sp->iface->xdev, NULL, NULL);
			}
		}
	}
}
#endif

/****************************************************************************/

/* Initialize PPP control structures for a Point-to-Point interface */
int
ppp_init(dev)
int dev;
{
	struct slip *sp;
	struct pppctl *pppiop;

	sp = &Slip[dev];
	pppiop = sp->pppio;

	pppiop->state = PPP_CLOSED;	/* Ready for traffic */
	pppiop->ctlmap = DEF_CTL_MAP;	/* Overall PPP parameters */

	lcp_init(sp);			/* Link Control parameters */
	pap_init(sp);			/* Peer Authentication parameters */
	ipcp_init(sp);			/* IP Control parameters */

	return 0;
}

/* Close the PPP interface */
int
ppp_close(sp,pl_too)
struct slip *sp;
int pl_too;
{
	int argc = 2;
	char *argv[] = {
		"dtr",
		"0",
	};

	ipcp_close(sp);			/* Close IP layer */
	lcp_close(sp);			/* Close Link layer */
	if (pl_too) {			/* Close physical layer */
		sp->iface->ioctl(sp->iface,argc,argv);
	}
	sp->pppio->lcpio.active = -1;	/* We are strictly closed */
	tprintf("PPP interface  %s  closed\n",sp->iface->name);
	return 0;
}


/* Process incoming PPP packets */
void
pproc(iface,bp)
struct iface *iface;
struct mbuf *bp;
{
	int i;
	struct ppphdr hdr;
	struct pppctl *pppiop;
	register struct iphdr *iph;
	unsigned char c;
	struct slip *sp;
	register int16 len;


	sp = &Slip[iface->xdev];
	pppiop = Slip[iface->xdev].pppio;
	pppiop->rcvpkt++;
	/* Remove PPP header and kick packet upstairs */
	ntohppp(&hdr, &bp);
	switch(hdr.type) {
	case PPP_IP_TYPE:	/* Regular IP */
		if ((pppiop->ipcpio.ipcp_state != IPCP_OPEN) &&
		    (pppiop->ipcpio.ipcp_state != IPCP_TERMINATE)) {
			if (ppptrace)
				log(-1,"PPP link not open for IP traffic; dropping IP packet");
			pppiop->rcverr++;

printf("rcverr: link not open for IP\n");
fflush(stdout);

			free_p(bp);
			break;
		}
		pppiop->rcvip++;
		if (sp->escaped & PPP_RCV_VJCOMPR) {
			iph = (struct iphdr *)bp->data;
			if (iph->ip_protocol == TCP_PTCL)
				sp->slcomp->sls_tcpin++;
			else
				sp->slcomp->sls_nontcpin++;
		}
		ip_route(iface,bp,0);
		break;
	case PPP_COMPR_TYPE:	/* Van Jacobson Compressed TCP/IP */
		if (pppiop->ipcpio.ipcp_state != IPCP_OPEN) {
			if (ppptrace)
				log(-1,"PPP link not open for IP traffic; dropping Compressed TCP/IP packet");
			pppiop->rcverr++;

printf("rcverr: link not open for VJ Compr\n");
fflush(stdout);

			free_p(bp);
			break;
		}
		if ((len = len_p(bp)) < 3) {
			if (ppptrace)
				log(-1,"VJ Compressed TCP/IP packet error: short");
			free_p(bp);
			pppiop->rcverr++;

printf("rcverr: short VJ Compr pkt\n");
fflush(stdout);

			break;
		}
		/* Got a packet at least minimum length of
		 * a compressed packet
		 */
		if ((sp->escaped & PPP_RCV_VJCOMPR) == 0) {
			if (ppptrace)
				log(-1,"VJ Compressed TCP/IP packet error: not enabled");
			pppiop->rcverr++;
printf("rcverr: VJ Compr not enabled\n");
fflush(stdout);

			free_p(bp);
			break;
		}
		/* Got a packet other than regular IP */
		len = sl_uncompress_tcp(&bp, len,
			SL_TYPE_COMPRESSED_TCP,
			sp->slcomp);
		if (len <= 0) {
			free_p(bp);
			bp = NULLBUF;
			pppiop->rcverr++;
printf("rcverr: VJ Compr decompr error\n");
fflush(stdout);

			break;
		}
		pppiop->rcvip++;
		ip_route(iface,bp,0);
		break;
	case PPP_UNCOMP_TYPE:	/* Van Jacobson Uncompressed TCP/IP */
		if (pppiop->ipcpio.ipcp_state != IPCP_OPEN) {
			if (ppptrace)
				log(-1,"PPP link not open for IP traffic; dropping Uncompressed TCP/IP packet");
			free_p(bp);
			pppiop->rcverr++;
printf("rcverr: link not open for VJ uncompr\n");
fflush(stdout);

			break;
		}
		if ((len = len_p(bp)) < 3) {
			if (ppptrace)
				log(-1,"VJ Uncompressed TCP/IP packet error: short");
			free_p(bp);
			pppiop->rcverr++;

printf("rcverr: short VJ Uncompr pkt\n");
fflush(stdout);

			break;
		}
		/* Got a packet at least minimum length of
		 * a compressed packet
		 */
		if ((sp->escaped & PPP_RCV_VJCOMPR) == 0) {
			if (ppptrace)
				log(-1,"VJ Uncompressed TCP/IP packet error: not enabled");
			free_p(bp);
			pppiop->rcverr++;

printf("rcverr: VJ Uncompr not enabled\n");
fflush(stdout);

			break;
		}
		/* Got a packet other than regular IP */
		len = sl_uncompress_tcp(&bp, len,
			SL_TYPE_UNCOMPRESSED_TCP,
			sp->slcomp);
		if (len <= 0) {
			free_p(bp);
			bp = NULLBUF;
			pppiop->rcverr++;

printf("rcverr: VJ Uncompr decompr error\n");
fflush(stdout);

			break;
		}
		pppiop->rcvip++;
		ip_route(iface,bp,0);
		break;
	case PPP_LCP_TYPE:	/* Link Control Protocol */
		pppiop->rcvlcp++;
		lcpproc(iface,bp);
		break;
	case PPP_PAP_TYPE:	/* Password Authenticate Protocol */
		if (pppiop->lcpio.lcp_state != LCP_OPEN) {
			if (ppptrace)
				log(-1,"not in PAP phase; dropping PAP packet");
			free_p(bp);
			pppiop->rcverr++;

printf("rcverr: not open for PAP\n");
fflush(stdout);

			break;
		}
		pppiop->rcvpap++;
		papproc(iface,bp);
		break;
	case PPP_IPCP_TYPE:	/* IP Control Protocol */
		if (pppiop->lcpio.lcp_state != LCP_OPEN) {
			if (ppptrace)
				log(-1,"IPCP closed; dropping IPCP packet");
			free_p(bp);
			pppiop->rcverr++;

printf("rcverr: not open for IPCP\n");
fflush(stdout);

			break;
		}
		pppiop->rcvipcp++;
		ipcpproc(iface,bp);
		break;
	default:
		if (ppptrace)
			log(-1,"Unknown PPP packet type: %x; dropping packet",hdr.type);
		free_p(bp);
		pppiop->rcvunk++;
		break;
	}
	return;
}
