--- linux-2.6.19.2/drivers/net/Kconfig.orig	2009-07-22 15:44:09.000000000 +0200
+++ linux-2.6.19.2/drivers/net/Kconfig	2010-01-24 20:14:13.000000000 +0100
@@ -2758,18 +2758,31 @@
 	  module; it is called bsd_comp and will show up in the directory
 	  modules once you have said "make modules". If unsure, say N.
 
-config PPP_MPPE
-       tristate "PPP MPPE compression (encryption) (EXPERIMENTAL)"
-       depends on PPP && EXPERIMENTAL
-       select CRYPTO
-       select CRYPTO_SHA1
-       select CRYPTO_ARC4
-       ---help---
-         Support for the MPPE Encryption protocol, as employed by the
-	 Microsoft Point-to-Point Tunneling Protocol.
+config PPP_MPPE_MPPC
+         tristate "Microsoft PPP compression/encryption (MPPC/MPPE)"
+         depends on PPP
+         select CRYPTO_SHA1
+         select CRYPTO_ARC4
+         ---help---
+         Support for the Microsoft Point-To-Point Compression (RFC2118) and
+         Microsoft Point-To-Point Encryption (RFC3078). These protocols are
+         supported by Microsoft Windows and wide range of "hardware" access
+         servers. MPPE is common protocol in Virtual Private Networks. According
+         to RFC3078, MPPE supports 40, 56 and 128-bit key lengths. Depending on
+         PPP daemon configuration on both ends of the link, following scenarios
+         are possible:
+               - only compression (MPPC) is used,
+               - only encryption (MPPE) is used,
+               - compression and encryption (MPPC+MPPE) are used.
+
+         Please note that Hi/Fn (http://www.hifn.com) holds patent on MPPC so
+         you should check if this patent is valid in your country in order to
+         avoid legal problems.
 
-	 See http://pptpclient.sourceforge.net/ for information on
-	 configuring PPTP clients and servers to utilize this method.
+         For more information please visit http://mppe-mppc.alphacron.de
+
+         To compile this driver as a module, choose M here. The module will
+         be called ppp_mppe_mppc.ko.
 
 config PPPOE
 	tristate "PPP over Ethernet (EXPERIMENTAL)"
--- linux-2.6.19.2/drivers/net/Makefile.orig	2009-07-22 15:44:09.000000000 +0200
+++ linux-2.6.19.2/drivers/net/Makefile	2010-01-24 20:14:13.000000000 +0100
@@ -122,7 +122,7 @@
 obj-$(CONFIG_PPP_SYNC_TTY) += ppp_synctty.o
 obj-$(CONFIG_PPP_DEFLATE) += ppp_deflate.o
 obj-$(CONFIG_PPP_BSDCOMP) += bsd_comp.o
-obj-$(CONFIG_PPP_MPPE) += ppp_mppe.o
+obj-$(CONFIG_PPP_MPPE_MPPC) += ppp_mppe_mppc.o
 obj-$(CONFIG_PPPOE) += pppox.o pppoe.o
 
 obj-$(CONFIG_SLIP) += slip.o
--- linux-2.6.19.2/drivers/net/ppp_generic.c.orig	2010-01-24 20:14:12.000000000 +0100
+++ linux-2.6.19.2/drivers/net/ppp_generic.c	2010-01-24 20:14:13.000000000 +0100
@@ -19,7 +19,7 @@
  * PPP driver, written by Michael Callahan and Al Longyear, and
  * subsequently hacked by Paul Mackerras.
  *
- * ==FILEVERSION 20041108==
+ * ==FILEVERSION 20050110==
  */
 
 #include <linux/module.h>
@@ -104,6 +104,7 @@
 	spinlock_t	rlock;		/* lock for receive side 58 */
 	spinlock_t	wlock;		/* lock for transmit side 5c */
 	int		mru;		/* max receive unit 60 */
+	int		mru_alloc;	/* MAX(1500,MRU) for dev_alloc_skb() */
 	unsigned int	flags;		/* control bits 64 */
 	unsigned int	xstate;		/* transmit state bits 68 */
 	unsigned int	rstate;		/* receive state bits 6c */
@@ -136,14 +137,13 @@
 
 /*
  * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
- * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
- * SC_MUST_COMP
+ * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
  * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
  * Bits in xstate: SC_COMP_RUN
  */
 #define SC_FLAG_BITS	(SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
 			 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
-			 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
+			 |SC_COMP_TCP|SC_REJ_COMP_TCP)
 
 /*
  * Private data structure for each channel.
@@ -629,7 +629,9 @@
 	case PPPIOCSMRU:
 		if (get_user(val, p))
 			break;
-		ppp->mru = val;
+		ppp->mru_alloc = ppp->mru = val;
+		if (ppp->mru_alloc < PPP_MRU)
+		    ppp->mru_alloc = PPP_MRU;	/* increase for broken peers */
 		err = 0;
 		break;
 
@@ -1017,56 +1019,6 @@
 	ppp_xmit_unlock(ppp);
 }
 
-static inline struct sk_buff *
-pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
-{
-	struct sk_buff *new_skb;
-	int len;
-	int new_skb_size = ppp->dev->mtu +
-		ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
-	int compressor_skb_size = ppp->dev->mtu +
-		ppp->xcomp->comp_extra + PPP_HDRLEN;
-	new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
-	if (!new_skb) {
-		if (net_ratelimit())
-			printk(KERN_ERR "PPP: no memory (comp pkt)\n");
-		return NULL;
-	}
-	if (ppp->dev->hard_header_len > PPP_HDRLEN)
-		skb_reserve(new_skb,
-			    ppp->dev->hard_header_len - PPP_HDRLEN);
-
-	/* compressor still expects A/C bytes in hdr */
-	len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
-				   new_skb->data, skb->len + 2,
-				   compressor_skb_size);
-	if (len > 0 && (ppp->flags & SC_CCP_UP)) {
-		kfree_skb(skb);
-		skb = new_skb;
-		skb_put(skb, len);
-		skb_pull(skb, 2);	/* pull off A/C bytes */
-	} else if (len == 0) {
-		/* didn't compress, or CCP not up yet */
-		kfree_skb(new_skb);
-		new_skb = skb;
-	} else {
-		/*
-		 * (len < 0)
-		 * MPPE requires that we do not send unencrypted
-		 * frames.  The compressor will return -1 if we
-		 * should drop the frame.  We cannot simply test
-		 * the compress_proto because MPPE and MPPC share
-		 * the same number.
-		 */
-		if (net_ratelimit())
-			printk(KERN_ERR "ppp: compressor dropped pkt\n");
-		kfree_skb(skb);
-		kfree_skb(new_skb);
-		new_skb = NULL;
-	}
-	return new_skb;
-}
-
 /*
  * Compress and send a frame.
  * The caller should have locked the xmit path,
@@ -1147,20 +1099,70 @@
 	case PPP_CCP:
 		/* peek at outbound CCP frames */
 		ppp_ccp_peek(ppp, skb, 0);
+		/*
+		 * When LZS or MPPE/MPPC has been negotiated we don't send
+		 * CCP_RESETACK after receiving CCP_RESETREQ; in fact pppd
+		 * sends such a packet but we silently discard it here
+		 */
+		if (CCP_CODE(skb->data+2) == CCP_RESETACK
+		    && (ppp->xcomp->compress_proto == CI_MPPE
+			|| ppp->xcomp->compress_proto == CI_LZS)) {
+		    --ppp->stats.tx_packets;
+		    ppp->stats.tx_bytes -= skb->len - 2;
+		    kfree_skb(skb);
+		    return;
+		}
 		break;
 	}
 
 	/* try to do packet compression */
 	if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
 	    && proto != PPP_LCP && proto != PPP_CCP) {
-		if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
-			if (net_ratelimit())
-				printk(KERN_ERR "ppp: compression required but down - pkt dropped.\n");
+		int comp_ovhd = 0;
+		/* 
+		 * because of possible data expansion when MPPC or LZS
+		 * is used, allocate compressor's buffer 12.5% bigger
+		 * than MTU
+		 */
+		if (ppp->xcomp->compress_proto == CI_MPPE)
+		    comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + MPPE_OVHD;
+		else if (ppp->xcomp->compress_proto == CI_LZS)
+		    comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + LZS_OVHD;
+		new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len
+				    + comp_ovhd, GFP_ATOMIC);
+		if (new_skb == 0) {
+			printk(KERN_ERR "PPP: no memory (comp pkt)\n");
 			goto drop;
 		}
-		skb = pad_compress_skb(ppp, skb);
-		if (!skb)
+		if (ppp->dev->hard_header_len > PPP_HDRLEN)
+			skb_reserve(new_skb,
+				    ppp->dev->hard_header_len - PPP_HDRLEN);
+
+		/* compressor still expects A/C bytes in hdr */
+		len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
+					   new_skb->data, skb->len + 2,
+					   ppp->dev->mtu + PPP_HDRLEN);
+		if (len > 0 && (ppp->flags & SC_CCP_UP)) {
+			kfree_skb(skb);
+			skb = new_skb;
+			skb_put(skb, len);
+			skb_pull(skb, 2);	/* pull off A/C bytes */
+		} else if (len == 0) {
+			/* didn't compress, or CCP not up yet */
+			kfree_skb(new_skb);
+		} else {
+			/*
+			 * (len < 0)
+			 * MPPE requires that we do not send unencrypted
+			 * frames.  The compressor will return -1 if we
+			 * should drop the frame.  We cannot simply test
+			 * the compress_proto because MPPE and MPPC share
+			 * the same number.
+			 */
+			printk(KERN_ERR "ppp: compressor dropped pkt\n");
+			kfree_skb(new_skb);
 			goto drop;
+		}
 	}
 
 	/*
@@ -1180,8 +1182,7 @@
 	return;
 
  drop:
-	if (skb)
-		kfree_skb(skb);
+	kfree_skb(skb);
 	++ppp->stats.tx_errors;
 }
 
@@ -1578,9 +1579,6 @@
 	    && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
 		skb = ppp_decompress_frame(ppp, skb);
 
-	if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
-		goto err;
-
 	proto = PPP_PROTO(skb);
 	switch (proto) {
 	case PPP_VJC_COMP:
@@ -1711,14 +1709,15 @@
 		goto err;
 
 	if (proto == PPP_COMP) {
-		ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
+		ns = dev_alloc_skb(ppp->mru_alloc + PPP_HDRLEN);
 		if (ns == 0) {
 			printk(KERN_ERR "ppp_decompress_frame: no memory\n");
 			goto err;
 		}
 		/* the decompressor still expects the A/C bytes in the hdr */
 		len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
-				skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
+				skb->len + 2, ns->data,
+				ppp->mru_alloc + PPP_HDRLEN);
 		if (len < 0) {
 			/* Pass the compressed frame to pppd as an
 			   error indication. */
@@ -1744,7 +1743,14 @@
 	return skb;
 
  err:
-	ppp->rstate |= SC_DC_ERROR;
+	if (ppp->rcomp->compress_proto != CI_MPPE
+	    && ppp->rcomp->compress_proto != CI_LZS) {
+	    /*
+	     * If decompression protocol isn't MPPE/MPPC or LZS, we set
+	     * SC_DC_ERROR flag and wait for CCP_RESETACK
+	     */
+	    ppp->rstate |= SC_DC_ERROR;
+	}
 	ppp_receive_error(ppp);
 	return skb;
 }
@@ -2415,6 +2421,7 @@
 		goto out1;
 
 	ppp->mru = PPP_MRU;
+	ppp->mru_alloc = PPP_MRU;
 	init_ppp_file(&ppp->file, INTERFACE);
 	ppp->file.hdrlen = PPP_HDRLEN - 2;	/* don't count proto bytes */
 	for (i = 0; i < NUM_NP; ++i)
--- linux-2.6.19.2/drivers/net/ppp_mppe.c.orig	2007-01-11 08:38:19.000000000 +0100
+++ linux-2.6.19.2/drivers/net/ppp_mppe.c	1970-01-01 01:00:00.000000000 +0100
@@ -1,739 +0,0 @@
-/*
- * ppp_mppe.c - interface MPPE to the PPP code.
- * This version is for use with Linux kernel 2.6.14+
- *
- * By Frank Cusack <fcusack@fcusack.com>.
- * Copyright (c) 2002,2003,2004 Google, Inc.
- * All rights reserved.
- *
- * License:
- * Permission to use, copy, modify, and distribute this software and its
- * documentation is hereby granted, provided that the above copyright
- * notice appears in all copies.  This software is provided without any
- * warranty, express or implied.
- *
- * ALTERNATIVELY, provided that this notice is retained in full, this product
- * may be distributed under the terms of the GNU General Public License (GPL),
- * in which case the provisions of the GPL apply INSTEAD OF those given above.
- *
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- *
- * Changelog:
- *      08/12/05 - Matt Domsch <Matt_Domsch@dell.com>
- *                 Only need extra skb padding on transmit, not receive.
- *      06/18/04 - Matt Domsch <Matt_Domsch@dell.com>, Oleg Makarenko <mole@quadra.ru>
- *                 Use Linux kernel 2.6 arc4 and sha1 routines rather than
- *                 providing our own.
- *      2/15/04 - TS: added #include <version.h> and testing for Kernel
- *                    version before using
- *                    MOD_DEC_USAGE_COUNT/MOD_INC_USAGE_COUNT which are
- *                    deprecated in 2.6
- */
-
-#include <linux/err.h>
-#include <linux/module.h>
-#include <linux/kernel.h>
-#include <linux/version.h>
-#include <linux/init.h>
-#include <linux/types.h>
-#include <linux/slab.h>
-#include <linux/string.h>
-#include <linux/crypto.h>
-#include <linux/mm.h>
-#include <linux/ppp_defs.h>
-#include <linux/ppp-comp.h>
-#include <asm/scatterlist.h>
-
-#include "ppp_mppe.h"
-
-MODULE_AUTHOR("Frank Cusack <fcusack@fcusack.com>");
-MODULE_DESCRIPTION("Point-to-Point Protocol Microsoft Point-to-Point Encryption support");
-MODULE_LICENSE("Dual BSD/GPL");
-MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
-MODULE_VERSION("1.0.2");
-
-static unsigned int
-setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
-{
-	sg[0].page = virt_to_page(address);
-	sg[0].offset = offset_in_page(address);
-	sg[0].length = length;
-	return length;
-}
-
-#define SHA1_PAD_SIZE 40
-
-/*
- * kernel crypto API needs its arguments to be in kmalloc'd memory, not in the module
- * static data area.  That means sha_pad needs to be kmalloc'd.
- */
-
-struct sha_pad {
-	unsigned char sha_pad1[SHA1_PAD_SIZE];
-	unsigned char sha_pad2[SHA1_PAD_SIZE];
-};
-static struct sha_pad *sha_pad;
-
-static inline void sha_pad_init(struct sha_pad *shapad)
-{
-	memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1));
-	memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2));
-}
-
-/*
- * State for an MPPE (de)compressor.
- */
-struct ppp_mppe_state {
-	struct crypto_blkcipher *arc4;
-	struct crypto_hash *sha1;
-	unsigned char *sha1_digest;
-	unsigned char master_key[MPPE_MAX_KEY_LEN];
-	unsigned char session_key[MPPE_MAX_KEY_LEN];
-	unsigned keylen;	/* key length in bytes             */
-	/* NB: 128-bit == 16, 40-bit == 8! */
-	/* If we want to support 56-bit,   */
-	/* the unit has to change to bits  */
-	unsigned char bits;	/* MPPE control bits */
-	unsigned ccount;	/* 12-bit coherency count (seqno)  */
-	unsigned stateful;	/* stateful mode flag */
-	int discard;		/* stateful mode packet loss flag */
-	int sanity_errors;	/* take down LCP if too many */
-	int unit;
-	int debug;
-	struct compstat stats;
-};
-
-/* struct ppp_mppe_state.bits definitions */
-#define MPPE_BIT_A	0x80	/* Encryption table were (re)inititalized */
-#define MPPE_BIT_B	0x40	/* MPPC only (not implemented) */
-#define MPPE_BIT_C	0x20	/* MPPC only (not implemented) */
-#define MPPE_BIT_D	0x10	/* This is an encrypted frame */
-
-#define MPPE_BIT_FLUSHED	MPPE_BIT_A
-#define MPPE_BIT_ENCRYPTED	MPPE_BIT_D
-
-#define MPPE_BITS(p) ((p)[4] & 0xf0)
-#define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
-#define MPPE_CCOUNT_SPACE 0x1000	/* The size of the ccount space */
-
-#define MPPE_OVHD	2	/* MPPE overhead/packet */
-#define SANITY_MAX	1600	/* Max bogon factor we will tolerate */
-
-/*
- * Key Derivation, from RFC 3078, RFC 3079.
- * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
- */
-static void get_new_key_from_sha(struct ppp_mppe_state * state, unsigned char *InterimKey)
-{
-	struct hash_desc desc;
-	struct scatterlist sg[4];
-	unsigned int nbytes;
-
-	nbytes = setup_sg(&sg[0], state->master_key, state->keylen);
-	nbytes += setup_sg(&sg[1], sha_pad->sha_pad1,
-			   sizeof(sha_pad->sha_pad1));
-	nbytes += setup_sg(&sg[2], state->session_key, state->keylen);
-	nbytes += setup_sg(&sg[3], sha_pad->sha_pad2,
-			   sizeof(sha_pad->sha_pad2));
-
-	desc.tfm = state->sha1;
-	desc.flags = 0;
-
-	crypto_hash_digest(&desc, sg, nbytes, state->sha1_digest);
-
-	memcpy(InterimKey, state->sha1_digest, state->keylen);
-}
-
-/*
- * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
- * Well, not what's written there, but rather what they meant.
- */
-static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
-{
-	unsigned char InterimKey[MPPE_MAX_KEY_LEN];
-	struct scatterlist sg_in[1], sg_out[1];
-	struct blkcipher_desc desc = { .tfm = state->arc4 };
-
-	get_new_key_from_sha(state, InterimKey);
-	if (!initial_key) {
-		crypto_blkcipher_setkey(state->arc4, InterimKey, state->keylen);
-		setup_sg(sg_in, InterimKey, state->keylen);
-		setup_sg(sg_out, state->session_key, state->keylen);
-		if (crypto_blkcipher_encrypt(&desc, sg_out, sg_in,
-					     state->keylen) != 0) {
-    		    printk(KERN_WARNING "mppe_rekey: cipher_encrypt failed\n");
-		}
-	} else {
-		memcpy(state->session_key, InterimKey, state->keylen);
-	}
-	if (state->keylen == 8) {
-		/* See RFC 3078 */
-		state->session_key[0] = 0xd1;
-		state->session_key[1] = 0x26;
-		state->session_key[2] = 0x9e;
-	}
-	crypto_blkcipher_setkey(state->arc4, state->session_key, state->keylen);
-}
-
-/*
- * Allocate space for a (de)compressor.
- */
-static void *mppe_alloc(unsigned char *options, int optlen)
-{
-	struct ppp_mppe_state *state;
-	unsigned int digestsize;
-
-	if (optlen != CILEN_MPPE + sizeof(state->master_key)
-	    || options[0] != CI_MPPE || options[1] != CILEN_MPPE)
-		goto out;
-
-	state = (struct ppp_mppe_state *) kmalloc(sizeof(*state), GFP_KERNEL);
-	if (state == NULL)
-		goto out;
-
-	memset(state, 0, sizeof(*state));
-
-	state->arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
-	if (IS_ERR(state->arc4)) {
-		state->arc4 = NULL;
-		goto out_free;
-	}
-
-	state->sha1 = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
-	if (IS_ERR(state->sha1)) {
-		state->sha1 = NULL;
-		goto out_free;
-	}
-
-	digestsize = crypto_hash_digestsize(state->sha1);
-	if (digestsize < MPPE_MAX_KEY_LEN)
-		goto out_free;
-
-	state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
-	if (!state->sha1_digest)
-		goto out_free;
-
-	/* Save keys. */
-	memcpy(state->master_key, &options[CILEN_MPPE],
-	       sizeof(state->master_key));
-	memcpy(state->session_key, state->master_key,
-	       sizeof(state->master_key));
-
-	/*
-	 * We defer initial key generation until mppe_init(), as mppe_alloc()
-	 * is called frequently during negotiation.
-	 */
-
-	return (void *)state;
-
-	out_free:
-	    if (state->sha1_digest)
-		kfree(state->sha1_digest);
-	    if (state->sha1)
-		crypto_free_hash(state->sha1);
-	    if (state->arc4)
-		crypto_free_blkcipher(state->arc4);
-	    kfree(state);
-	out:
-	return NULL;
-}
-
-/*
- * Deallocate space for a (de)compressor.
- */
-static void mppe_free(void *arg)
-{
-	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
-	if (state) {
-	    if (state->sha1_digest)
-		kfree(state->sha1_digest);
-	    if (state->sha1)
-		crypto_free_hash(state->sha1);
-	    if (state->arc4)
-		crypto_free_blkcipher(state->arc4);
-	    kfree(state);
-	}
-}
-
-/*
- * Initialize (de)compressor state.
- */
-static int
-mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
-	  const char *debugstr)
-{
-	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
-	unsigned char mppe_opts;
-
-	if (optlen != CILEN_MPPE
-	    || options[0] != CI_MPPE || options[1] != CILEN_MPPE)
-		return 0;
-
-	MPPE_CI_TO_OPTS(&options[2], mppe_opts);
-	if (mppe_opts & MPPE_OPT_128)
-		state->keylen = 16;
-	else if (mppe_opts & MPPE_OPT_40)
-		state->keylen = 8;
-	else {
-		printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
-		       unit);
-		return 0;
-	}
-	if (mppe_opts & MPPE_OPT_STATEFUL)
-		state->stateful = 1;
-
-	/* Generate the initial session key. */
-	mppe_rekey(state, 1);
-
-	if (debug) {
-		int i;
-		char mkey[sizeof(state->master_key) * 2 + 1];
-		char skey[sizeof(state->session_key) * 2 + 1];
-
-		printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
-		       debugstr, unit, (state->keylen == 16) ? 128 : 40,
-		       (state->stateful) ? "stateful" : "stateless");
-
-		for (i = 0; i < sizeof(state->master_key); i++)
-			sprintf(mkey + i * 2, "%02x", state->master_key[i]);
-		for (i = 0; i < sizeof(state->session_key); i++)
-			sprintf(skey + i * 2, "%02x", state->session_key[i]);
-		printk(KERN_DEBUG
-		       "%s[%d]: keys: master: %s initial session: %s\n",
-		       debugstr, unit, mkey, skey);
-	}
-
-	/*
-	 * Initialize the coherency count.  The initial value is not specified
-	 * in RFC 3078, but we can make a reasonable assumption that it will
-	 * start at 0.  Setting it to the max here makes the comp/decomp code
-	 * do the right thing (determined through experiment).
-	 */
-	state->ccount = MPPE_CCOUNT_SPACE - 1;
-
-	/*
-	 * Note that even though we have initialized the key table, we don't
-	 * set the FLUSHED bit.  This is contrary to RFC 3078, sec. 3.1.
-	 */
-	state->bits = MPPE_BIT_ENCRYPTED;
-
-	state->unit = unit;
-	state->debug = debug;
-
-	return 1;
-}
-
-static int
-mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
-	       int hdrlen, int debug)
-{
-	/* ARGSUSED */
-	return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
-}
-
-/*
- * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
- * tell the compressor to rekey.  Note that we MUST NOT rekey for
- * every CCP Reset-Request; we only rekey on the next xmit packet.
- * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
- * So, rekeying for every CCP Reset-Request is broken as the peer will not
- * know how many times we've rekeyed.  (If we rekey and THEN get another
- * CCP Reset-Request, we must rekey again.)
- */
-static void mppe_comp_reset(void *arg)
-{
-	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
-
-	state->bits |= MPPE_BIT_FLUSHED;
-}
-
-/*
- * Compress (encrypt) a packet.
- * It's strange to call this a compressor, since the output is always
- * MPPE_OVHD + 2 bytes larger than the input.
- */
-static int
-mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
-	      int isize, int osize)
-{
-	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
-	struct blkcipher_desc desc = { .tfm = state->arc4 };
-	int proto;
-	struct scatterlist sg_in[1], sg_out[1];
-
-	/*
-	 * Check that the protocol is in the range we handle.
-	 */
-	proto = PPP_PROTOCOL(ibuf);
-	if (proto < 0x0021 || proto > 0x00fa)
-		return 0;
-
-	/* Make sure we have enough room to generate an encrypted packet. */
-	if (osize < isize + MPPE_OVHD + 2) {
-		/* Drop the packet if we should encrypt it, but can't. */
-		printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
-		       "(have: %d need: %d)\n", state->unit,
-		       osize, osize + MPPE_OVHD + 2);
-		return -1;
-	}
-
-	osize = isize + MPPE_OVHD + 2;
-
-	/*
-	 * Copy over the PPP header and set control bits.
-	 */
-	obuf[0] = PPP_ADDRESS(ibuf);
-	obuf[1] = PPP_CONTROL(ibuf);
-	obuf[2] = PPP_COMP >> 8;	/* isize + MPPE_OVHD + 1 */
-	obuf[3] = PPP_COMP;	/* isize + MPPE_OVHD + 2 */
-	obuf += PPP_HDRLEN;
-
-	state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
-	if (state->debug >= 7)
-		printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
-		       state->ccount);
-	obuf[0] = state->ccount >> 8;
-	obuf[1] = state->ccount & 0xff;
-
-	if (!state->stateful ||	/* stateless mode     */
-	    ((state->ccount & 0xff) == 0xff) ||	/* "flag" packet      */
-	    (state->bits & MPPE_BIT_FLUSHED)) {	/* CCP Reset-Request  */
-		/* We must rekey */
-		if (state->debug && state->stateful)
-			printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
-			       state->unit);
-		mppe_rekey(state, 0);
-		state->bits |= MPPE_BIT_FLUSHED;
-	}
-	obuf[0] |= state->bits;
-	state->bits &= ~MPPE_BIT_FLUSHED;	/* reset for next xmit */
-
-	obuf += MPPE_OVHD;
-	ibuf += 2;		/* skip to proto field */
-	isize -= 2;
-
-	/* Encrypt packet */
-	setup_sg(sg_in, ibuf, isize);
-	setup_sg(sg_out, obuf, osize);
-	if (crypto_blkcipher_encrypt(&desc, sg_out, sg_in, isize) != 0) {
-		printk(KERN_DEBUG "crypto_cypher_encrypt failed\n");
-		return -1;
-	}
-
-	state->stats.unc_bytes += isize;
-	state->stats.unc_packets++;
-	state->stats.comp_bytes += osize;
-	state->stats.comp_packets++;
-
-	return osize;
-}
-
-/*
- * Since every frame grows by MPPE_OVHD + 2 bytes, this is always going
- * to look bad ... and the longer the link is up the worse it will get.
- */
-static void mppe_comp_stats(void *arg, struct compstat *stats)
-{
-	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
-
-	*stats = state->stats;
-}
-
-static int
-mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
-		 int hdrlen, int mru, int debug)
-{
-	/* ARGSUSED */
-	return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
-}
-
-/*
- * We received a CCP Reset-Ack.  Just ignore it.
- */
-static void mppe_decomp_reset(void *arg)
-{
-	/* ARGSUSED */
-	return;
-}
-
-/*
- * Decompress (decrypt) an MPPE packet.
- */
-static int
-mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
-		int osize)
-{
-	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
-	struct blkcipher_desc desc = { .tfm = state->arc4 };
-	unsigned ccount;
-	int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
-	int sanity = 0;
-	struct scatterlist sg_in[1], sg_out[1];
-
-	if (isize <= PPP_HDRLEN + MPPE_OVHD) {
-		if (state->debug)
-			printk(KERN_DEBUG
-			       "mppe_decompress[%d]: short pkt (%d)\n",
-			       state->unit, isize);
-		return DECOMP_ERROR;
-	}
-
-	/*
-	 * Make sure we have enough room to decrypt the packet.
-	 * Note that for our test we only subtract 1 byte whereas in
-	 * mppe_compress() we added 2 bytes (+MPPE_OVHD);
-	 * this is to account for possible PFC.
-	 */
-	if (osize < isize - MPPE_OVHD - 1) {
-		printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
-		       "(have: %d need: %d)\n", state->unit,
-		       osize, isize - MPPE_OVHD - 1);
-		return DECOMP_ERROR;
-	}
-	osize = isize - MPPE_OVHD - 2;	/* assume no PFC */
-
-	ccount = MPPE_CCOUNT(ibuf);
-	if (state->debug >= 7)
-		printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
-		       state->unit, ccount);
-
-	/* sanity checks -- terminate with extreme prejudice */
-	if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
-		printk(KERN_DEBUG
-		       "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
-		       state->unit);
-		state->sanity_errors += 100;
-		sanity = 1;
-	}
-	if (!state->stateful && !flushed) {
-		printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
-		       "stateless mode!\n", state->unit);
-		state->sanity_errors += 100;
-		sanity = 1;
-	}
-	if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
-		printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
-		       "flag packet!\n", state->unit);
-		state->sanity_errors += 100;
-		sanity = 1;
-	}
-
-	if (sanity) {
-		if (state->sanity_errors < SANITY_MAX)
-			return DECOMP_ERROR;
-		else
-			/*
-			 * Take LCP down if the peer is sending too many bogons.
-			 * We don't want to do this for a single or just a few
-			 * instances since it could just be due to packet corruption.
-			 */
-			return DECOMP_FATALERROR;
-	}
-
-	/*
-	 * Check the coherency count.
-	 */
-
-	if (!state->stateful) {
-		/* RFC 3078, sec 8.1.  Rekey for every packet. */
-		while (state->ccount != ccount) {
-			mppe_rekey(state, 0);
-			state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
-		}
-	} else {
-		/* RFC 3078, sec 8.2. */
-		if (!state->discard) {
-			/* normal state */
-			state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
-			if (ccount != state->ccount) {
-				/*
-				 * (ccount > state->ccount)
-				 * Packet loss detected, enter the discard state.
-				 * Signal the peer to rekey (by sending a CCP Reset-Request).
-				 */
-				state->discard = 1;
-				return DECOMP_ERROR;
-			}
-		} else {
-			/* discard state */
-			if (!flushed) {
-				/* ccp.c will be silent (no additional CCP Reset-Requests). */
-				return DECOMP_ERROR;
-			} else {
-				/* Rekey for every missed "flag" packet. */
-				while ((ccount & ~0xff) !=
-				       (state->ccount & ~0xff)) {
-					mppe_rekey(state, 0);
-					state->ccount =
-					    (state->ccount +
-					     256) % MPPE_CCOUNT_SPACE;
-				}
-
-				/* reset */
-				state->discard = 0;
-				state->ccount = ccount;
-				/*
-				 * Another problem with RFC 3078 here.  It implies that the
-				 * peer need not send a Reset-Ack packet.  But RFC 1962
-				 * requires it.  Hopefully, M$ does send a Reset-Ack; even
-				 * though it isn't required for MPPE synchronization, it is
-				 * required to reset CCP state.
-				 */
-			}
-		}
-		if (flushed)
-			mppe_rekey(state, 0);
-	}
-
-	/*
-	 * Fill in the first part of the PPP header.  The protocol field
-	 * comes from the decrypted data.
-	 */
-	obuf[0] = PPP_ADDRESS(ibuf);	/* +1 */
-	obuf[1] = PPP_CONTROL(ibuf);	/* +1 */
-	obuf += 2;
-	ibuf += PPP_HDRLEN + MPPE_OVHD;
-	isize -= PPP_HDRLEN + MPPE_OVHD;	/* -6 */
-	/* net osize: isize-4 */
-
-	/*
-	 * Decrypt the first byte in order to check if it is
-	 * a compressed or uncompressed protocol field.
-	 */
-	setup_sg(sg_in, ibuf, 1);
-	setup_sg(sg_out, obuf, 1);
-	if (crypto_blkcipher_decrypt(&desc, sg_out, sg_in, 1) != 0) {
-		printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
-		return DECOMP_ERROR;
-	}
-
-	/*
-	 * Do PFC decompression.
-	 * This would be nicer if we were given the actual sk_buff
-	 * instead of a char *.
-	 */
-	if ((obuf[0] & 0x01) != 0) {
-		obuf[1] = obuf[0];
-		obuf[0] = 0;
-		obuf++;
-		osize++;
-	}
-
-	/* And finally, decrypt the rest of the packet. */
-	setup_sg(sg_in, ibuf + 1, isize - 1);
-	setup_sg(sg_out, obuf + 1, osize - 1);
-	if (crypto_blkcipher_decrypt(&desc, sg_out, sg_in, isize - 1)) {
-		printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
-		return DECOMP_ERROR;
-	}
-
-	state->stats.unc_bytes += osize;
-	state->stats.unc_packets++;
-	state->stats.comp_bytes += isize;
-	state->stats.comp_packets++;
-
-	/* good packet credit */
-	state->sanity_errors >>= 1;
-
-	return osize;
-}
-
-/*
- * Incompressible data has arrived (this should never happen!).
- * We should probably drop the link if the protocol is in the range
- * of what should be encrypted.  At the least, we should drop this
- * packet.  (How to do this?)
- */
-static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
-{
-	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
-
-	if (state->debug &&
-	    (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
-		printk(KERN_DEBUG
-		       "mppe_incomp[%d]: incompressible (unencrypted) data! "
-		       "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
-
-	state->stats.inc_bytes += icnt;
-	state->stats.inc_packets++;
-	state->stats.unc_bytes += icnt;
-	state->stats.unc_packets++;
-}
-
-/*************************************************************
- * Module interface table
- *************************************************************/
-
-/*
- * Procedures exported to if_ppp.c.
- */
-static struct compressor ppp_mppe = {
-	.compress_proto = CI_MPPE,
-	.comp_alloc     = mppe_alloc,
-	.comp_free      = mppe_free,
-	.comp_init      = mppe_comp_init,
-	.comp_reset     = mppe_comp_reset,
-	.compress       = mppe_compress,
-	.comp_stat      = mppe_comp_stats,
-	.decomp_alloc   = mppe_alloc,
-	.decomp_free    = mppe_free,
-	.decomp_init    = mppe_decomp_init,
-	.decomp_reset   = mppe_decomp_reset,
-	.decompress     = mppe_decompress,
-	.incomp         = mppe_incomp,
-	.decomp_stat    = mppe_comp_stats,
-	.owner          = THIS_MODULE,
-	.comp_extra     = MPPE_PAD,
-};
-
-/*
- * ppp_mppe_init()
- *
- * Prior to allowing load, try to load the arc4 and sha1 crypto
- * libraries.  The actual use will be allocated later, but
- * this way the module will fail to insmod if they aren't available.
- */
-
-static int __init ppp_mppe_init(void)
-{
-	int answer;
-	if (!(crypto_has_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC) &&
-	      crypto_has_hash("sha1", 0, CRYPTO_ALG_ASYNC)))
-		return -ENODEV;
-
-	sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
-	if (!sha_pad)
-		return -ENOMEM;
-	sha_pad_init(sha_pad);
-
-	answer = ppp_register_compressor(&ppp_mppe);
-
-	if (answer == 0)
-		printk(KERN_INFO "PPP MPPE Compression module registered\n");
-	else
-		kfree(sha_pad);
-
-	return answer;
-}
-
-static void __exit ppp_mppe_cleanup(void)
-{
-	ppp_unregister_compressor(&ppp_mppe);
-	kfree(sha_pad);
-}
-
-module_init(ppp_mppe_init);
-module_exit(ppp_mppe_cleanup);
--- linux-2.6.19.2/drivers/net/ppp_mppe.h.orig	2007-01-11 08:38:19.000000000 +0100
+++ linux-2.6.19.2/drivers/net/ppp_mppe.h	1970-01-01 01:00:00.000000000 +0100
@@ -1,86 +0,0 @@
-#define MPPE_PAD                4      /* MPPE growth per frame */
-#define MPPE_MAX_KEY_LEN       16      /* largest key length (128-bit) */
-
-/* option bits for ccp_options.mppe */
-#define MPPE_OPT_40            0x01    /* 40 bit */
-#define MPPE_OPT_128           0x02    /* 128 bit */
-#define MPPE_OPT_STATEFUL      0x04    /* stateful mode */
-/* unsupported opts */
-#define MPPE_OPT_56            0x08    /* 56 bit */
-#define MPPE_OPT_MPPC          0x10    /* MPPC compression */
-#define MPPE_OPT_D             0x20    /* Unknown */
-#define MPPE_OPT_UNSUPPORTED (MPPE_OPT_56|MPPE_OPT_MPPC|MPPE_OPT_D)
-#define MPPE_OPT_UNKNOWN       0x40    /* Bits !defined in RFC 3078 were set */
-
-/*
- * This is not nice ... the alternative is a bitfield struct though.
- * And unfortunately, we cannot share the same bits for the option
- * names above since C and H are the same bit.  We could do a u_int32
- * but then we have to do a htonl() all the time and/or we still need
- * to know which octet is which.
- */
-#define MPPE_C_BIT             0x01    /* MPPC */
-#define MPPE_D_BIT             0x10    /* Obsolete, usage unknown */
-#define MPPE_L_BIT             0x20    /* 40-bit */
-#define MPPE_S_BIT             0x40    /* 128-bit */
-#define MPPE_M_BIT             0x80    /* 56-bit, not supported */
-#define MPPE_H_BIT             0x01    /* Stateless (in a different byte) */
-
-/* Does not include H bit; used for least significant octet only. */
-#define MPPE_ALL_BITS (MPPE_D_BIT|MPPE_L_BIT|MPPE_S_BIT|MPPE_M_BIT|MPPE_H_BIT)
-
-/* Build a CI from mppe opts (see RFC 3078) */
-#define MPPE_OPTS_TO_CI(opts, ci)              \
-    do {                                       \
-       u_char *ptr = ci; /* u_char[4] */       \
-                                               \
-       /* H bit */                             \
-       if (opts & MPPE_OPT_STATEFUL)           \
-           *ptr++ = 0x0;                       \
-       else                                    \
-           *ptr++ = MPPE_H_BIT;                \
-       *ptr++ = 0;                             \
-       *ptr++ = 0;                             \
-                                               \
-       /* S,L bits */                          \
-       *ptr = 0;                               \
-       if (opts & MPPE_OPT_128)                \
-           *ptr |= MPPE_S_BIT;                 \
-       if (opts & MPPE_OPT_40)                 \
-           *ptr |= MPPE_L_BIT;                 \
-       /* M,D,C bits not supported */          \
-    } while (/* CONSTCOND */ 0)
-
-/* The reverse of the above */
-#define MPPE_CI_TO_OPTS(ci, opts)              \
-    do {                                       \
-       u_char *ptr = ci; /* u_char[4] */       \
-                                               \
-       opts = 0;                               \
-                                               \
-       /* H bit */                             \
-       if (!(ptr[0] & MPPE_H_BIT))             \
-           opts |= MPPE_OPT_STATEFUL;          \
-                                               \
-       /* S,L bits */                          \
-       if (ptr[3] & MPPE_S_BIT)                \
-           opts |= MPPE_OPT_128;               \
-       if (ptr[3] & MPPE_L_BIT)                \
-           opts |= MPPE_OPT_40;                \
-                                               \
-       /* M,D,C bits */                        \
-       if (ptr[3] & MPPE_M_BIT)                \
-           opts |= MPPE_OPT_56;                \
-       if (ptr[3] & MPPE_D_BIT)                \
-           opts |= MPPE_OPT_D;                 \
-       if (ptr[3] & MPPE_C_BIT)                \
-           opts |= MPPE_OPT_MPPC;              \
-                                               \
-       /* Other bits */                        \
-       if (ptr[0] & ~MPPE_H_BIT)               \
-           opts |= MPPE_OPT_UNKNOWN;           \
-       if (ptr[1] || ptr[2])                   \
-           opts |= MPPE_OPT_UNKNOWN;           \
-       if (ptr[3] & ~MPPE_ALL_BITS)            \
-           opts |= MPPE_OPT_UNKNOWN;           \
-    } while (/* CONSTCOND */ 0)
--- linux-2.6.19.2/drivers/net/ppp_mppe_mppc.c.orig	1970-01-01 01:00:00.000000000 +0100
+++ linux-2.6.19.2/drivers/net/ppp_mppe_mppc.c	2010-01-24 20:14:13.000000000 +0100
@@ -0,0 +1,1299 @@
+/*
+ * ppp_mppe_mppc.c - MPPC/MPPE "compressor/decompressor" module.
+ *
+ * Copyright (c) 1994 Árpád Magosányi <mag@bunuel.tii.matav.hu>
+ * Copyright (c) 1999 Tim Hockin, Cobalt Networks Inc. <thockin@cobaltnet.com>
+ * Copyright (c) 2002-2004 Jan Dubiec <jdx@slackware.pl>
+ * 
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation is hereby granted, provided that the above copyright
+ * notice appears in all copies. This software is provided without any
+ * warranty, express or implied.
+ *
+ * The code is based on MPPE kernel module written by Árpád Magosányi and
+ * Tim Hockin which can be found on http://planetmirror.com/pub/mppe/.
+ * I have added MPPC and 56 bit session keys support in MPPE.
+ *
+ * WARNING! Although this is open source code, its usage in some countries
+ * (in particular in the USA) may violate Stac Inc. patent for MPPC.
+ *
+ *  ==FILEVERSION 20041123==
+ *
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <asm/scatterlist.h>
+#include <linux/vmalloc.h>
+#include <linux/crypto.h>
+
+#include <linux/ppp_defs.h>
+#include <linux/ppp-comp.h>
+
+/*
+ * State for a mppc/mppe "(de)compressor".
+ */
+struct ppp_mppe_state {
+    struct crypto_tfm *arc4_tfm;
+    struct crypto_tfm *sha1_tfm;
+    u8		*sha1_digest;
+    u8		master_key[MPPE_MAX_KEY_LEN];
+    u8		session_key[MPPE_MAX_KEY_LEN];
+    u8		mppc;		/* do we use compression (MPPC)? */
+    u8		mppe;		/* do we use encryption (MPPE)? */
+    u8		keylen;		/* key length in bytes */
+    u8		bitkeylen;	/* key length in bits */
+    u16		ccount;		/* coherency counter */
+    u16		bits;		/* MPPC/MPPE control bits */
+    u8		stateless;	/* do we use stateless mode? */
+    u8		nextflushed;	/* set A bit in the next outgoing packet;
+				   used only by compressor*/
+    u8		flushexpected;	/* drop packets until A bit is received;
+				   used only by decompressor*/
+    u8		*hist;		/* MPPC history */
+    u16		*hash;		/* Hash table; used only by compressor */
+    u16		histptr;	/* history "cursor" */
+    int		unit;
+    int		debug;
+    int		mru;
+    struct compstat stats;
+};
+
+#define MPPE_HIST_LEN		8192	/* MPPC history size */
+#define MPPE_MAX_CCOUNT		0x0FFF	/* max. coherency counter value */
+
+#define MPPE_BIT_FLUSHED	0x80	/* bit A */
+#define MPPE_BIT_RESET		0x40	/* bit B */
+#define MPPE_BIT_COMP		0x20	/* bit C */
+#define MPPE_BIT_ENCRYPTED	0x10	/* bit D */
+
+#define MPPE_SALT0		0xD1	/* values used in MPPE key derivation */
+#define MPPE_SALT1		0x26	/* according to RFC3079 */
+#define MPPE_SALT2		0x9E
+
+#define MPPE_CCOUNT(x)		((((x)[4] & 0x0f) << 8) + (x)[5])
+#define MPPE_BITS(x)		((x)[4] & 0xf0)
+#define MPPE_CTRLHI(x)		((((x)->ccount & 0xf00)>>8)|((x)->bits))
+#define MPPE_CTRLLO(x)		((x)->ccount & 0xff)
+
+/*
+ * Kernel Crypto API needs its arguments to be in kmalloc'd memory, not in the
+ * module static data area. That means sha_pad needs to be kmalloc'd. It is done
+ * in mppe_module_init(). This has been pointed out on 30th July 2004 by Oleg
+ * Makarenko on pptpclient-devel mailing list.
+ */
+#define SHA1_PAD_SIZE		40
+struct sha_pad {
+    unsigned char sha_pad1[SHA1_PAD_SIZE];
+    unsigned char sha_pad2[SHA1_PAD_SIZE];
+};
+static struct sha_pad *sha_pad;
+
+static inline void
+setup_sg(struct scatterlist *sg, const void  *address, unsigned int length)
+{
+    sg[0].page = virt_to_page(address);
+    sg[0].offset = offset_in_page(address);
+    sg[0].length = length;
+}
+
+static inline void
+arc4_setkey(struct ppp_mppe_state *state, const unsigned char *key,
+	    const unsigned int keylen)
+{
+    crypto_cipher_setkey(state->arc4_tfm, key, keylen);
+}
+
+static inline void
+arc4_encrypt(struct ppp_mppe_state *state, const unsigned char *in,
+	     const unsigned int len, unsigned char *out)
+{
+    struct scatterlist sgin[4], sgout[4];
+
+    setup_sg(sgin, in, len);
+    setup_sg(sgout, out, len);
+    crypto_cipher_encrypt(state->arc4_tfm, sgout, sgin, len);
+}
+
+#define arc4_decrypt arc4_encrypt
+
+/*
+ * Key Derivation, from RFC 3078, RFC 3079.
+ * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
+ */
+static void
+get_new_key_from_sha(struct ppp_mppe_state *state, unsigned char *interim_key)
+{
+    struct scatterlist sg[4];
+
+    setup_sg(&sg[0], state->master_key, state->keylen);
+    setup_sg(&sg[1], sha_pad->sha_pad1, sizeof(sha_pad->sha_pad1));
+    setup_sg(&sg[2], state->session_key, state->keylen);
+    setup_sg(&sg[3], sha_pad->sha_pad2, sizeof(sha_pad->sha_pad2));
+
+    crypto_digest_digest (state->sha1_tfm, sg, 4, state->sha1_digest);
+
+    memcpy(interim_key, state->sha1_digest, state->keylen);
+}
+
+static void
+mppe_change_key(struct ppp_mppe_state *state, int initialize)
+{
+    unsigned char interim_key[MPPE_MAX_KEY_LEN];
+
+    get_new_key_from_sha(state, interim_key);
+    if (initialize) {
+	memcpy(state->session_key, interim_key, state->keylen);
+    } else {
+	arc4_setkey(state, interim_key, state->keylen);
+	arc4_encrypt(state, interim_key, state->keylen, state->session_key);
+    }
+    if (state->keylen == 8) {
+	if (state->bitkeylen == 40) {
+	    state->session_key[0] = MPPE_SALT0;
+	    state->session_key[1] = MPPE_SALT1;
+	    state->session_key[2] = MPPE_SALT2;
+	} else {
+	    state->session_key[0] = MPPE_SALT0;
+	}
+    }
+    arc4_setkey(state, state->session_key, state->keylen);
+}
+
+/* increase 12-bit coherency counter */
+static inline void
+mppe_increase_ccount(struct ppp_mppe_state *state)
+{
+    state->ccount = (state->ccount + 1) & MPPE_MAX_CCOUNT;
+    if (state->mppe) {
+	if (state->stateless) {
+	    mppe_change_key(state, 0);
+	    state->nextflushed = 1;
+	} else {
+	    if ((state->ccount & 0xff) == 0xff) {
+		mppe_change_key(state, 0);
+	    }
+	}
+    }
+}
+
+/* allocate space for a MPPE/MPPC (de)compressor.  */
+/*   comp != 0 -> init compressor */
+/*   comp = 0 -> init decompressor */
+static void *
+mppe_alloc(unsigned char *options, int opt_len, int comp)
+{
+    struct ppp_mppe_state *state;
+    unsigned int digestsize;
+    u8* fname;
+
+    fname = comp ? "mppe_comp_alloc" : "mppe_decomp_alloc";
+
+    /*  
+     * Hack warning - additionally to the standard MPPC/MPPE configuration
+     * options, pppd passes to the (de)copressor 8 or 16 byte session key.
+     * Therefore options[1] contains MPPC/MPPE configuration option length
+     * (CILEN_MPPE = 6), but the real options length, depending on the key
+     * length, is 6+8 or 6+16.
+     */
+    if (opt_len < CILEN_MPPE) {
+	printk(KERN_WARNING "%s: wrong options length: %u\n", fname, opt_len);
+	return NULL;
+    }
+
+    if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
+	(options[2] & ~MPPE_STATELESS) != 0 ||
+	options[3] != 0 || options[4] != 0 ||
+	(options[5] & ~(MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
+	(options[5] & (MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
+	printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
+	       "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname, options[0],
+	       options[1], options[2], options[3], options[4], options[5]);
+	return NULL;
+    }
+
+    state = (struct ppp_mppe_state *)kmalloc(sizeof(*state), GFP_KERNEL);
+    if (state == NULL) {
+	printk(KERN_ERR "%s: cannot allocate space for %scompressor\n", fname,
+	       comp ? "" : "de");
+	return NULL;
+    }
+    memset(state, 0, sizeof(struct ppp_mppe_state));
+
+    state->mppc = options[5] & MPPE_MPPC;	/* Do we use MPPC? */
+    state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
+	MPPE_40BIT);				/* Do we use MPPE? */
+
+    if (state->mppc) {
+	/* allocate MPPC history */
+	state->hist = (u8*)vmalloc(2*MPPE_HIST_LEN*sizeof(u8));
+	if (state->hist == NULL) {
+	    kfree(state);
+	    printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
+		   fname);
+	    return NULL;
+	}
+
+	/* allocate hashtable for MPPC compressor */
+	if (comp) {
+	    state->hash = (u16*)vmalloc(MPPE_HIST_LEN*sizeof(u16));
+	    if (state->hash == NULL) {
+		vfree(state->hist);
+		kfree(state);
+		printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
+		       fname);
+		return NULL;
+	    }
+	}
+    }
+
+    if (state->mppe) { /* specific for MPPE */
+	/* Load ARC4 algorithm */
+	state->arc4_tfm = crypto_alloc_tfm("arc4", 0);
+	if (state->arc4_tfm == NULL) {
+	    if (state->mppc) {
+		vfree(state->hash);
+		if (comp)
+		    vfree(state->hist);
+	    }
+	    kfree(state);
+	    printk(KERN_ERR "%s: cannot load ARC4 module\n", fname);
+	    return NULL;
+	}
+
+	/* Load SHA1 algorithm */
+	state->sha1_tfm = crypto_alloc_tfm("sha1", 0);
+	if (state->sha1_tfm == NULL) {
+	    crypto_free_tfm(state->arc4_tfm);
+	    if (state->mppc) {
+		vfree(state->hash);
+		if (comp)
+		    vfree(state->hist);
+	    }
+	    kfree(state);
+	    printk(KERN_ERR "%s: cannot load SHA1 module\n", fname);
+	    return NULL;
+	}
+
+	digestsize = crypto_tfm_alg_digestsize(state->sha1_tfm);
+	if (digestsize < MPPE_MAX_KEY_LEN) {
+	    crypto_free_tfm(state->sha1_tfm);
+	    crypto_free_tfm(state->arc4_tfm);
+	    if (state->mppc) {
+		vfree(state->hash);
+		if (comp)
+		    vfree(state->hist);
+	    }
+	    kfree(state);
+	    printk(KERN_ERR "%s: CryptoAPI SHA1 digest size too small\n", fname);
+	}
+
+	state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
+	if (!state->sha1_digest) {
+	    crypto_free_tfm(state->sha1_tfm);
+	    crypto_free_tfm(state->arc4_tfm);
+	    if (state->mppc) {
+		vfree(state->hash);
+		if (comp)
+		    vfree(state->hist);
+	    }
+	    kfree(state);
+	    printk(KERN_ERR "%s: cannot allocate space for SHA1 digest\n", fname);
+	}
+
+	memcpy(state->master_key, options+CILEN_MPPE, MPPE_MAX_KEY_LEN);
+	memcpy(state->session_key, state->master_key, MPPE_MAX_KEY_LEN);
+	/* initial key generation is done in mppe_init() */
+    }
+
+    return (void *) state;
+}
+
+static void *
+mppe_comp_alloc(unsigned char *options, int opt_len)
+{
+    return mppe_alloc(options, opt_len, 1);
+}
+
+static void *
+mppe_decomp_alloc(unsigned char *options, int opt_len)
+{
+    return mppe_alloc(options, opt_len, 0);
+}
+
+/* cleanup the (de)compressor */
+static void
+mppe_comp_free(void *arg)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
+
+    if (state != NULL) {
+	if (state->mppe) {
+	    if (state->sha1_digest != NULL)
+		kfree(state->sha1_digest);
+	    if (state->sha1_tfm != NULL)
+		crypto_free_tfm(state->sha1_tfm);
+	    if (state->arc4_tfm != NULL)
+		crypto_free_tfm(state->arc4_tfm);
+	}
+	if (state->hist != NULL)
+	    vfree(state->hist);
+	if (state->hash != NULL)
+	    vfree(state->hash);
+	kfree(state);
+    }
+}
+
+/* init MPPC/MPPE (de)compresor */
+/*   comp != 0 -> init compressor */
+/*   comp = 0 -> init decompressor */
+static int
+mppe_init(void *arg, unsigned char *options, int opt_len, int unit,
+	  int hdrlen, int mru, int debug, int comp)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
+    u8* fname;
+
+    fname = comp ? "mppe_comp_init" : "mppe_decomp_init";
+
+    if (opt_len < CILEN_MPPE) {
+	if (debug)
+	    printk(KERN_WARNING "%s: wrong options length: %u\n",
+		   fname, opt_len);
+	return 0;
+    }
+
+    if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
+	(options[2] & ~MPPE_STATELESS) != 0 ||
+	options[3] != 0 || options[4] != 0 ||
+	(options[5] & ~(MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
+	(options[5] & (MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
+	if (debug)
+	    printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
+		   "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname,
+		   options[0], options[1], options[2], options[3], options[4],
+		   options[5]);
+	return 0;
+    }
+
+    if ((options[5] & ~MPPE_MPPC) != MPPE_128BIT &&
+	(options[5] & ~MPPE_MPPC) != MPPE_56BIT &&
+	(options[5] & ~MPPE_MPPC) != MPPE_40BIT &&
+	(options[5] & MPPE_MPPC) != MPPE_MPPC) {
+	if (debug)
+	    printk(KERN_WARNING "%s: don't know what to do: o[5]=%02x\n",
+		   fname, options[5]);
+	return 0;
+    }
+
+    state->mppc = options[5] & MPPE_MPPC;	/* Do we use MPPC? */
+    state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
+	MPPE_40BIT);				/* Do we use MPPE? */
+    state->stateless = options[2] & MPPE_STATELESS; /* Do we use stateless mode? */
+
+    switch (state->mppe) {
+    case MPPE_40BIT:     /* 40 bit key */
+	state->keylen = 8;
+	state->bitkeylen = 40;
+	break;
+    case MPPE_56BIT:     /* 56 bit key */
+	state->keylen = 8;
+	state->bitkeylen = 56;
+	break;
+    case MPPE_128BIT:    /* 128 bit key */
+	state->keylen = 16;
+	state->bitkeylen = 128;
+	break;
+    default:
+	state->keylen = 0;
+	state->bitkeylen = 0;
+    }
+
+    state->ccount = MPPE_MAX_CCOUNT;
+    state->bits = 0;
+    state->unit  = unit;
+    state->debug = debug;
+    state->histptr = MPPE_HIST_LEN;
+    if (state->mppc) {	/* reset history if MPPC was negotiated */
+	memset(state->hist, 0, 2*MPPE_HIST_LEN*sizeof(u8));
+    }
+
+    if (state->mppe) { /* generate initial session keys */
+	mppe_change_key(state, 1);
+    }
+
+    if (comp) { /* specific for compressor */
+	state->nextflushed = 1;
+    } else { /* specific for decompressor */
+	state->mru = mru;
+	state->flushexpected = 1;
+    }
+
+    return 1;
+}
+
+static int
+mppe_comp_init(void *arg, unsigned char *options, int opt_len, int unit,
+	       int hdrlen, int debug)
+{
+    return mppe_init(arg, options, opt_len, unit, hdrlen, 0, debug, 1);
+}
+
+
+static int
+mppe_decomp_init(void *arg, unsigned char *options, int opt_len, int unit,
+		 int hdrlen, int mru, int debug)
+{
+    return mppe_init(arg, options, opt_len, unit, hdrlen, mru, debug, 0);
+}
+
+static void
+mppe_comp_reset(void *arg)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
+
+    if (state->debug)
+	printk(KERN_DEBUG "%s%d: resetting MPPC/MPPE compressor\n",
+	       __FUNCTION__, state->unit);
+
+    state->nextflushed = 1;
+    if (state->mppe)
+	arc4_setkey(state, state->session_key, state->keylen);
+}
+
+static void
+mppe_decomp_reset(void *arg)
+{
+    /* When MPPC/MPPE is in use, we shouldn't receive any CCP Reset-Ack.
+       But when we receive such a packet, we just ignore it. */
+    return;
+}
+
+static void
+mppe_stats(void *arg, struct compstat *stats)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
+
+    *stats = state->stats;
+}
+
+/***************************/
+/**** Compression stuff ****/
+/***************************/
+/* inserts 1 to 8 bits into the output buffer */
+static inline void putbits8(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
+{
+    buf += *i;
+    if (*l >= n) {
+	*l = (*l) - n;
+	val <<= *l;
+	*buf = *buf | (val & 0xff);
+	if (*l == 0) {
+	    *l = 8;
+	    (*i)++;
+	    *(++buf) = 0;
+	}
+    } else {
+	(*i)++;
+	*l = 8 - n + (*l);
+	val <<= *l;
+	*buf = *buf | ((val >> 8) & 0xff);
+	*(++buf) = val & 0xff;
+    }
+}
+
+/* inserts 9 to 16 bits into the output buffer */
+static inline void putbits16(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
+{
+    buf += *i;
+    if (*l >= n - 8) {
+	(*i)++;
+	*l = 8 - n + (*l);
+	val <<= *l;
+	*buf = *buf | ((val >> 8) & 0xff);
+	*(++buf) = val & 0xff;
+	if (*l == 0) {
+	    *l = 8;
+	    (*i)++;
+	    *(++buf) = 0;
+	}
+    } else {
+	(*i)++; (*i)++;
+	*l = 16 - n + (*l);
+	val <<= *l;
+	*buf = *buf | ((val >> 16) & 0xff);
+	*(++buf) = (val >> 8) & 0xff;
+	*(++buf) = val & 0xff;
+    }
+}
+
+/* inserts 17 to 24 bits into the output buffer */
+static inline void putbits24(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
+{
+    buf += *i;
+    if (*l >= n - 16) {
+	(*i)++; (*i)++;
+	*l = 16 - n + (*l);
+	val <<= *l;
+	*buf = *buf | ((val >> 16) & 0xff);
+	*(++buf) = (val >> 8) & 0xff;
+	*(++buf) = val & 0xff;
+	if (*l == 0) {
+	    *l = 8;
+	    (*i)++;
+	    *(++buf) = 0;
+	}
+    } else {
+	(*i)++; (*i)++; (*i)++;
+	*l = 24 - n + (*l);
+	val <<= *l;
+	*buf = *buf | ((val >> 24) & 0xff);
+	*(++buf) = (val >> 16) & 0xff;
+	*(++buf) = (val >> 8) & 0xff;
+	*(++buf) = val & 0xff;
+    }
+}
+
+static int
+mppc_compress(struct ppp_mppe_state *state, unsigned char *ibuf,
+	      unsigned char *obuf, int isize, int osize)
+{
+    u32 olen, off, len, idx, i, l;
+    u8 *hist, *sbuf, *p, *q, *r, *s;
+
+    /*  
+	At this point, to avoid possible buffer overflow caused by packet
+	expansion during/after compression,  we should make sure that
+	osize >= (((isize*9)/8)+1)+2, but we don't do that because in
+	ppp_generic.c we simply allocate bigger obuf.
+
+	Maximum MPPC packet expansion is 12.5%. This is the worst case when
+	all octets in the input buffer are >= 0x80 and we cannot find any
+	repeated tokens. Additionally we have to reserve 2 bytes for MPPE/MPPC
+	status bits and coherency counter.
+    */
+
+    hist = state->hist + MPPE_HIST_LEN;
+    /* check if there is enough room at the end of the history */
+    if (state->histptr + isize >= 2*MPPE_HIST_LEN) {
+	state->bits |= MPPE_BIT_RESET;
+	state->histptr = MPPE_HIST_LEN;
+	memcpy(state->hist, hist, MPPE_HIST_LEN);
+    }
+    /* add packet to the history; isize must be <= MPPE_HIST_LEN */
+    sbuf = state->hist + state->histptr;
+    memcpy(sbuf, ibuf, isize);
+    state->histptr += isize;
+
+    /* compress data */
+    r = sbuf + isize;
+    *obuf = olen = i = 0;
+    l = 8;
+    while (i < isize - 2) {
+	s = q = sbuf + i;
+	idx = ((40543*((((s[0]<<4)^s[1])<<4)^s[2]))>>4) & 0x1fff;
+	p = hist + state->hash[idx];
+	state->hash[idx] = (u16) (s - hist);
+	off = s - p;
+	if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++) {
+	    /* no match found; encode literal byte */
+	    if (ibuf[i] < 0x80) {		/* literal byte < 0x80 */
+		putbits8(obuf, (u32) ibuf[i], 8, &olen, &l);
+	    } else {				/* literal byte >= 0x80 */
+		putbits16(obuf, (u32) (0x100|(ibuf[i]&0x7f)), 9, &olen, &l);
+	    }
+	    ++i;
+	    continue;
+	}
+	if (r - q >= 64) {
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++;
+	    if (s - q == 64) {
+		p--; s--;
+		while((*p++ == *s++) && (s < r) && (p < q));
+	    }
+	} else {
+	    while((*p++ == *s++) && (s < r) && (p < q));
+	}
+	len = s - q - 1;
+	i += len;
+
+	/* at least 3 character match found; code data */
+	/* encode offset */
+	if (off < 64) {			/* 10-bit offset; 0 <= offset < 64 */
+	    putbits16(obuf, 0x3c0|off, 10, &olen, &l);
+	} else if (off < 320) {		/* 12-bit offset; 64 <= offset < 320 */
+	    putbits16(obuf, 0xe00|(off-64), 12, &olen, &l);
+	} else if (off < 8192) {	/* 16-bit offset; 320 <= offset < 8192 */
+	    putbits16(obuf, 0xc000|(off-320), 16, &olen, &l);
+	} else {
+	    /* This shouldn't happen; we return 0 what means "packet expands",
+	    and we send packet uncompressed. */
+	    if (state->debug)
+		printk(KERN_DEBUG "%s%d: wrong offset value: %d\n",
+		       __FUNCTION__, state->unit, off);
+	    return 0;
+	}
+	/* encode length of match */
+	if (len < 4) {			/* length = 3 */
+	    putbits8(obuf, 0, 1, &olen, &l);
+	} else if (len < 8) {		/* 4 <= length < 8 */
+	    putbits8(obuf, 0x08|(len&0x03), 4, &olen, &l);
+	} else if (len < 16) {		/* 8 <= length < 16 */
+	    putbits8(obuf, 0x30|(len&0x07), 6, &olen, &l);
+	} else if (len < 32) {		/* 16 <= length < 32 */
+	    putbits8(obuf, 0xe0|(len&0x0f), 8, &olen, &l);
+	} else if (len < 64) {		/* 32 <= length < 64 */
+	    putbits16(obuf, 0x3c0|(len&0x1f), 10, &olen, &l);
+	} else if (len < 128) {		/* 64 <= length < 128 */
+	    putbits16(obuf, 0xf80|(len&0x3f), 12, &olen, &l);
+	} else if (len < 256) {		/* 128 <= length < 256 */
+	    putbits16(obuf, 0x3f00|(len&0x7f), 14, &olen, &l);
+	} else if (len < 512) {		/* 256 <= length < 512 */
+	    putbits16(obuf, 0xfe00|(len&0xff), 16, &olen, &l);
+	} else if (len < 1024) {	/* 512 <= length < 1024 */
+	    putbits24(obuf, 0x3fc00|(len&0x1ff), 18, &olen, &l);
+	} else if (len < 2048) {	/* 1024 <= length < 2048 */
+	    putbits24(obuf, 0xff800|(len&0x3ff), 20, &olen, &l);
+	} else if (len < 4096) {	/* 2048 <= length < 4096 */
+	    putbits24(obuf, 0x3ff000|(len&0x7ff), 22, &olen, &l);
+	} else if (len < 8192) {	/* 4096 <= length < 8192 */
+	    putbits24(obuf, 0xffe000|(len&0xfff), 24, &olen, &l);
+	} else {
+	    /* This shouldn't happen; we return 0 what means "packet expands",
+	    and send packet uncompressed. */
+	    if (state->debug)
+		printk(KERN_DEBUG "%s%d: wrong length of match value: %d\n",
+		       __FUNCTION__, state->unit, len);
+	    return 0;
+	}
+    }
+
+    /* Add remaining octets to the output */
+    while(isize - i > 0) {
+	if (ibuf[i] < 0x80) {	/* literal byte < 0x80 */
+	    putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l);
+	} else {		/* literal byte >= 0x80 */
+	    putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l);
+	}
+    }
+    /* Reset unused bits of the last output octet */
+    if ((l != 0) && (l != 8)) {
+	putbits8(obuf, 0, l, &olen, &l);
+    }
+
+    return (int) olen;
+}
+
+int
+mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
+	      int isize, int osize)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
+    int proto, olen, complen, off;
+    unsigned char *wptr;
+
+    /* Check that the protocol is in the range we handle. */
+    proto = PPP_PROTOCOL(ibuf);
+    if (proto < 0x0021 || proto > 0x00fa)
+	return 0;
+
+    wptr = obuf;
+    /* Copy over the PPP header */
+    wptr[0] = PPP_ADDRESS(ibuf);
+    wptr[1] = PPP_CONTROL(ibuf);
+    wptr[2] = PPP_COMP >> 8;
+    wptr[3] = PPP_COMP;
+    wptr += PPP_HDRLEN + (MPPE_OVHD / 2); /* Leave two octets for MPPE/MPPC bits */
+
+    /* 
+     * In ver. 0.99 protocol field was compressed. Deflate and BSD compress
+     * do PFC before actual compression, RCF2118 and RFC3078 are not precise
+     * on this topic so I decided to do PFC. Unfortunately this change caused
+     * incompatibility with older/other MPPE/MPPC modules. I have received
+     * a lot of complaints from unexperienced users so I have decided to revert
+     * to previous state, i.e. the protocol field is sent uncompressed now.
+     * Although this may be changed in the future.
+     *
+     * Receiving side (mppe_decompress()) still accepts packets with compressed
+     * and uncompressed protocol field so you shouldn't get "Unsupported protocol
+     * 0x2145 received" messages anymore.
+     */
+    //off = (proto > 0xff) ? 2 : 3; /* PFC - skip first protocol byte if 0 */
+    off = 2;
+
+    ibuf += off;
+
+    mppe_increase_ccount(state);
+
+    if (state->nextflushed) {
+	state->bits |= MPPE_BIT_FLUSHED;
+	state->nextflushed = 0;
+	if (state->mppe && !state->stateless) {
+	    /*
+	     * If this is the flag packet, the key has been already changed in
+	     * mppe_increase_ccount() so we dont't do it once again.
+	     */
+	    if ((state->ccount & 0xff) != 0xff) {
+		arc4_setkey(state, state->session_key, state->keylen);
+	    }
+	}
+	if (state->mppc) { /* reset history */
+	    state->bits |= MPPE_BIT_RESET;
+	    state->histptr = MPPE_HIST_LEN;
+	    memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
+	}
+    }
+
+    if (state->mppc && !state->mppe) { /* Do only compression */
+	complen = mppc_compress(state, ibuf, wptr, isize - off,
+				osize - PPP_HDRLEN - (MPPE_OVHD / 2));
+	/*
+	 * TODO: Implement an heuristics to handle packet expansion in a smart
+	 * way. Now, when a packet expands, we send it as uncompressed and
+	 * when next packet is sent we have to reset compressor's history.
+	 * Maybe it would be better to send such packet as compressed in order
+	 * to keep history's continuity.
+	 */
+	if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
+	    (complen == 0)) {
+	    /* packet expands */
+	    state->nextflushed = 1;
+	    memcpy(wptr, ibuf, isize - off);
+	    olen = isize - (off - 2) + MPPE_OVHD;
+	    (state->stats).inc_bytes += olen;
+	    (state->stats).inc_packets++;
+	} else {
+	    state->bits |= MPPE_BIT_COMP;
+	    olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
+	    (state->stats).comp_bytes += olen;
+	    (state->stats).comp_packets++;
+	}
+    } else { /* Do encryption with or without compression */
+	state->bits |= MPPE_BIT_ENCRYPTED;
+	if (!state->mppc && state->mppe) { /* Do only encryption */
+	    /* read from ibuf, write to wptr, adjust for PPP_HDRLEN */
+	    arc4_encrypt(state, ibuf, isize - off, wptr);
+	    olen = isize - (off - 2) + MPPE_OVHD;
+	    (state->stats).inc_bytes += olen;
+	    (state->stats).inc_packets++;
+	} else { /* Do compression and then encryption - RFC3078 */
+	    complen = mppc_compress(state, ibuf, wptr, isize - off,
+				    osize - PPP_HDRLEN - (MPPE_OVHD / 2));
+	    /*
+	     * TODO: Implement an heuristics to handle packet expansion in a smart
+	     * way. Now, when a packet expands, we send it as uncompressed and
+	     * when next packet is sent we have to reset compressor's history.
+	     * Maybe it would be good to send such packet as compressed in order
+	     * to keep history's continuity.
+	     */
+	    if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
+		(complen == 0)) {
+		/* packet expands */
+		state->nextflushed = 1;
+		arc4_encrypt(state, ibuf, isize - off, wptr);
+		olen = isize - (off - 2) + MPPE_OVHD;
+		(state->stats).inc_bytes += olen;
+		(state->stats).inc_packets++;
+	    } else {
+		state->bits |= MPPE_BIT_COMP;
+		/* Hack warning !!! RC4 implementation which we use does
+		   encryption "in place" - it means that input and output
+		   buffers can be *the same* memory area. Therefore we don't
+		   need to use a temporary buffer. But be careful - other
+		   implementations don't have to be so nice.
+		   I used to use ibuf as temporary buffer here, but it led
+		   packet sniffers into error. Thanks to Wilfried Weissmann
+		   for pointing that. */
+		arc4_encrypt(state, wptr, complen, wptr);
+		olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
+		(state->stats).comp_bytes += olen;
+		(state->stats).comp_packets++;
+	    }
+	}
+    }
+
+    /* write status bits and coherency counter into the output buffer */
+    wptr = obuf + PPP_HDRLEN;
+    wptr[0] = MPPE_CTRLHI(state);
+    wptr[1] = MPPE_CTRLLO(state);
+
+    state->bits = 0;
+
+    (state->stats).unc_bytes += isize;
+    (state->stats).unc_packets++;
+
+    return olen;
+}
+
+/***************************/
+/*** Decompression stuff ***/
+/***************************/
+static inline u32 getbits(const u8 *buf, const u32 n, u32 *i, u32 *l)
+{
+    static const u32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
+    u32 res, ol;
+
+    ol = *l;
+    if (*l >= n) {
+	*l = (*l) - n;
+	res = (buf[*i] & m[ol]) >> (*l);
+	if (*l == 0) {
+	    *l = 8;
+	    (*i)++;
+	}
+    } else {
+	*l = 8 - n + (*l);
+	res = (buf[(*i)++] & m[ol]) << 8;
+	res = (res | buf[*i]) >> (*l);
+    }
+
+    return res;
+}
+
+static inline u32 getbyte(const u8 *buf, const u32 i, const u32 l)
+{
+    if (l == 8) {
+	return buf[i];
+    } else {
+	return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff;
+    }
+}
+
+static inline void lamecopy(u8 *dst, u8 *src, u32 len)
+{
+    while (len--)
+	*dst++ = *src++;
+}
+
+static int
+mppc_decompress(struct ppp_mppe_state *state, unsigned char *ibuf,
+		unsigned char *obuf, int isize, int osize)
+{
+    u32 olen, off, len, bits, val, sig, i, l;
+    u8 *history, *s;
+
+    history = state->hist + state->histptr;
+    olen = len = i = 0;
+    l = 8;
+    bits = isize * 8;
+    while (bits >= 8) {
+	val = getbyte(ibuf, i++, l);
+	if (val < 0x80) {		/* literal byte < 0x80 */
+	    if (state->histptr < 2*MPPE_HIST_LEN) {
+		/* copy uncompressed byte to the history */
+		(state->hist)[(state->histptr)++] = (u8) val;
+	    } else {
+		/* buffer overflow; drop packet */
+		if (state->debug)
+		    printk(KERN_ERR "%s%d: trying to write outside history "
+			   "buffer\n", __FUNCTION__, state->unit);
+		return DECOMP_ERROR;
+	    }
+	    olen++;
+	    bits -= 8;
+	    continue;
+	}
+
+	sig = val & 0xc0;
+	if (sig == 0x80) {		/* literal byte >= 0x80 */
+	    if (state->histptr < 2*MPPE_HIST_LEN) {
+		/* copy uncompressed byte to the history */
+		(state->hist)[(state->histptr)++] = 
+		    (u8) (0x80|((val&0x3f)<<1)|getbits(ibuf, 1 , &i ,&l));
+	    } else {
+		/* buffer overflow; drop packet */
+		if (state->debug)
+		    printk(KERN_ERR "%s%d: trying to write outside history "
+			   "buffer\n", __FUNCTION__, state->unit);
+		return DECOMP_ERROR;
+	    }
+	    olen++;
+	    bits -= 9;
+	    continue;
+	}
+
+	/* Not a literal byte so it must be an (offset,length) pair */
+	/* decode offset */
+	sig = val & 0xf0;
+	if (sig == 0xf0) {		/* 10-bit offset; 0 <= offset < 64 */
+	    off = (((val&0x0f)<<2)|getbits(ibuf, 2 , &i ,&l));
+	    bits -= 10;
+	} else {
+	    if (sig == 0xe0) {		/* 12-bit offset; 64 <= offset < 320 */
+		off = ((((val&0x0f)<<4)|getbits(ibuf, 4 , &i ,&l))+64);
+		bits -= 12;
+	    } else {
+		if ((sig&0xe0) == 0xc0) {/* 16-bit offset; 320 <= offset < 8192 */
+		    off = ((((val&0x1f)<<8)|getbyte(ibuf, i++, l))+320);
+		    bits -= 16;
+		    if (off > MPPE_HIST_LEN - 1) {
+			if (state->debug)
+			    printk(KERN_DEBUG "%s%d: too big offset value: %d\n",
+				   __FUNCTION__, state->unit, off);
+			return DECOMP_ERROR;
+		    }
+		} else {		/* this shouldn't happen */
+		    if (state->debug)
+			printk(KERN_DEBUG "%s%d: cannot decode offset value\n",
+			       __FUNCTION__, state->unit);
+		    return DECOMP_ERROR;
+		}
+	    }
+	}
+	/* decode length of match */
+	val = getbyte(ibuf, i, l);
+	if ((val & 0x80) == 0x00) {			/* len = 3 */
+	    len = 3;
+	    bits--;
+	    getbits(ibuf, 1 , &i ,&l);
+	} else if ((val & 0xc0) == 0x80) {		/* 4 <= len < 8 */
+	    len = 0x04 | ((val>>4) & 0x03);
+	    bits -= 4;
+	    getbits(ibuf, 4 , &i ,&l);
+	} else if ((val & 0xe0) == 0xc0) {		/* 8 <= len < 16 */
+	    len = 0x08 | ((val>>2) & 0x07);
+	    bits -= 6;
+	    getbits(ibuf, 6 , &i ,&l);
+	} else if ((val & 0xf0) == 0xe0) {		/* 16 <= len < 32 */
+	    len = 0x10 | (val & 0x0f);
+	    bits -= 8;
+	    i++;
+	} else {
+	    bits -= 8;
+	    val = (val << 8) | getbyte(ibuf, ++i, l);
+	    if ((val & 0xf800) == 0xf000) {		/* 32 <= len < 64 */
+		len = 0x0020 | ((val >> 6) & 0x001f);
+		bits -= 2;
+		getbits(ibuf, 2 , &i ,&l);
+	    } else if ((val & 0xfc00) == 0xf800) {	/* 64 <= len < 128 */
+		len = 0x0040 | ((val >> 4) & 0x003f);
+		bits -= 4;
+		getbits(ibuf, 4 , &i ,&l);
+	    } else if ((val & 0xfe00) == 0xfc00) {	/* 128 <= len < 256 */
+		len = 0x0080 | ((val >> 2) & 0x007f);
+		bits -= 6;
+		getbits(ibuf, 6 , &i ,&l);
+	    } else if ((val & 0xff00) == 0xfe00) {	/* 256 <= len < 512 */
+		len = 0x0100 | (val & 0x00ff);
+		bits -= 8;
+		i++;
+	    } else {
+		bits -= 8;
+		val = (val << 8) | getbyte(ibuf, ++i, l);
+		if ((val & 0xff8000) == 0xff0000) {	/* 512 <= len < 1024 */
+		    len = 0x000200 | ((val >> 6) & 0x0001ff);
+		    bits -= 2;
+		    getbits(ibuf, 2 , &i ,&l);
+		} else if ((val & 0xffc000) == 0xff8000) {/* 1024 <= len < 2048 */
+		    len = 0x000400 | ((val >> 4) & 0x0003ff);
+		    bits -= 4;
+		    getbits(ibuf, 4 , &i ,&l);
+		} else if ((val & 0xffe000) == 0xffc000) {/* 2048 <= len < 4096 */
+		    len = 0x000800 | ((val >> 2) & 0x0007ff);
+		    bits -= 6;
+		    getbits(ibuf, 6 , &i ,&l);
+		} else if ((val & 0xfff000) == 0xffe000) {/* 4096 <= len < 8192 */
+		    len = 0x001000 | (val & 0x000fff);
+		    bits -= 8;
+		    i++;
+		} else {				/* this shouldn't happen */
+		    if (state->debug)
+			printk(KERN_DEBUG "%s%d: wrong length code: 0x%X\n",
+			       __FUNCTION__, state->unit, val);
+		    return DECOMP_ERROR;
+		}
+	    }
+	}
+	s = state->hist + state->histptr;
+	state->histptr += len;
+	olen += len;
+	if (state->histptr < 2*MPPE_HIST_LEN) {
+	    /* copy uncompressed bytes to the history */
+
+	    /* In some cases len may be greater than off. It means that memory
+	     * areas pointed by s and s-off overlap. I had used memmove() here
+	     * because I thought that it acts as libc's version. Unfortunately,
+	     * I was wrong. :-) I got strange errors sometimes. Wilfried suggested
+	     * using of byte by byte copying here and strange errors disappeared.
+	     */
+	    lamecopy(s, s - off, len);
+	} else {
+	    /* buffer overflow; drop packet */
+	    if (state->debug)
+		printk(KERN_ERR "%s%d: trying to write outside history "
+		       "buffer\n", __FUNCTION__, state->unit);
+	    return DECOMP_ERROR;
+	}
+    }
+
+    /* Do PFC decompression */
+    len = olen;
+    if ((history[0] & 0x01) != 0) {
+	obuf[0] = 0;
+	obuf++;
+	len++;
+    }
+
+    if (len <= osize) {
+	/* copy uncompressed packet to the output buffer */
+	memcpy(obuf, history, olen);
+    } else {
+	/* buffer overflow; drop packet */
+	if (state->debug)
+	    printk(KERN_ERR "%s%d: too big uncompressed packet: %d\n",
+		   __FUNCTION__, state->unit, len + (PPP_HDRLEN / 2));
+	return DECOMP_ERROR;
+    }
+
+    return (int) len;
+}
+
+int
+mppe_decompress(void *arg, unsigned char *ibuf, int isize,
+		unsigned char *obuf, int osize)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
+    int seq, bits, uncomplen;
+
+    if (isize <= PPP_HDRLEN + MPPE_OVHD) {
+	if (state->debug) {
+	    printk(KERN_DEBUG "%s%d: short packet (len=%d)\n",  __FUNCTION__,
+		   state->unit, isize);
+	}
+	return DECOMP_ERROR;
+    }
+
+    /* Get coherency counter and control bits from input buffer */
+    seq = MPPE_CCOUNT(ibuf);
+    bits = MPPE_BITS(ibuf);
+
+    if (state->stateless) {
+	/* RFC 3078, sec 8.1. */
+	mppe_increase_ccount(state);
+	if ((seq != state->ccount) && state->debug)
+	    printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
+		   __FUNCTION__, state->unit, seq, state->ccount);
+	while (seq != state->ccount)
+	    mppe_increase_ccount(state);
+    } else {
+	/* RFC 3078, sec 8.2. */
+	if (state->flushexpected) { /* discard state */
+	    if ((bits & MPPE_BIT_FLUSHED)) { /* we received expected FLUSH bit */
+		while (seq != state->ccount)
+		    mppe_increase_ccount(state);
+		state->flushexpected = 0;
+	    } else /* drop packet*/
+		return DECOMP_ERROR;
+	} else { /* normal state */
+	    mppe_increase_ccount(state);
+	    if (seq != state->ccount) {
+		/* Packet loss detected, enter the discard state. */
+		if (state->debug)
+		    printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
+			   __FUNCTION__, state->unit, seq, state->ccount);
+		state->flushexpected = 1;
+		return DECOMP_ERROR;
+	    }
+	}
+	if (state->mppe && (bits & MPPE_BIT_FLUSHED)) {
+	    arc4_setkey(state, state->session_key, state->keylen);
+	}
+    }
+
+    if (state->mppc && (bits & (MPPE_BIT_FLUSHED | MPPE_BIT_RESET))) {
+	state->histptr = MPPE_HIST_LEN;
+	if ((bits & MPPE_BIT_FLUSHED)) {
+	    memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
+	} else
+	    if ((bits & MPPE_BIT_RESET)) {
+		memcpy(state->hist, state->hist + MPPE_HIST_LEN, MPPE_HIST_LEN);
+	    }
+    }
+
+    /* Fill in the first part of the PPP header. The protocol field
+       comes from the decompressed data. */
+    obuf[0] = PPP_ADDRESS(ibuf);
+    obuf[1] = PPP_CONTROL(ibuf);
+    obuf += PPP_HDRLEN / 2;
+
+    if (state->mppe) { /* process encrypted packet */
+	if ((bits & MPPE_BIT_ENCRYPTED)) {
+	    /* OK, packet encrypted, so decrypt it */
+	    if (state->mppc && (bits & MPPE_BIT_COMP)) {
+		/* Hack warning !!! RC4 implementation which we use does
+		   decryption "in place" - it means that input and output
+		   buffers can be *the same* memory area. Therefore we don't
+		   need to use a temporary buffer. But be careful - other
+		   implementations don't have to be so nice. */
+		arc4_decrypt(state, ibuf + PPP_HDRLEN +	(MPPE_OVHD / 2), isize -
+			     PPP_HDRLEN - (MPPE_OVHD / 2), ibuf + PPP_HDRLEN +
+			     (MPPE_OVHD / 2));
+		uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
+					    (MPPE_OVHD / 2), obuf, isize -
+					    PPP_HDRLEN - (MPPE_OVHD / 2),
+					    osize - (PPP_HDRLEN / 2));
+		if (uncomplen == DECOMP_ERROR) {
+		    state->flushexpected = 1;
+		    return DECOMP_ERROR;
+		}
+		uncomplen += PPP_HDRLEN / 2;
+		(state->stats).comp_bytes += isize;
+		(state->stats).comp_packets++;
+	    } else {
+		uncomplen = isize - MPPE_OVHD;
+		/* Decrypt the first byte in order to check if it is
+		   compressed or uncompressed protocol field */
+		arc4_decrypt(state, ibuf + PPP_HDRLEN +	(MPPE_OVHD / 2), 1, obuf);
+		/* Do PFC decompression */
+		if ((obuf[0] & 0x01) != 0) {
+		    obuf[1] = obuf[0];
+		    obuf[0] = 0;
+		    obuf++;
+		    uncomplen++;
+		}
+		/* And finally, decrypt the rest of the frame. */
+		arc4_decrypt(state, ibuf + PPP_HDRLEN +	(MPPE_OVHD / 2) + 1,
+			     isize - PPP_HDRLEN - (MPPE_OVHD / 2) - 1, obuf + 1);
+		(state->stats).inc_bytes += isize;
+		(state->stats).inc_packets++;
+	    }
+	} else { /* this shouldn't happen */
+	    if (state->debug)
+		printk(KERN_ERR "%s%d: encryption negotiated but not an "
+		       "encrypted packet received\n", __FUNCTION__, state->unit);
+	    mppe_change_key(state, 0);
+	    state->flushexpected = 1;
+	    return DECOMP_ERROR;
+	}
+    } else {
+	if (state->mppc) { /* no MPPE, only MPPC */
+	    if ((bits & MPPE_BIT_COMP)) {
+		uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
+					    (MPPE_OVHD / 2), obuf, isize -
+					    PPP_HDRLEN - (MPPE_OVHD / 2),
+					    osize - (PPP_HDRLEN / 2));
+		if (uncomplen == DECOMP_ERROR) {
+		    state->flushexpected = 1;
+		    return DECOMP_ERROR;
+		}
+		uncomplen += PPP_HDRLEN / 2;
+		(state->stats).comp_bytes += isize;
+		(state->stats).comp_packets++;
+	    } else {
+		memcpy(obuf, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
+		       PPP_HDRLEN - (MPPE_OVHD / 2));
+		uncomplen = isize - MPPE_OVHD;
+		(state->stats).inc_bytes += isize;
+		(state->stats).inc_packets++;
+	    }
+	} else { /* this shouldn't happen */
+	    if (state->debug)
+		printk(KERN_ERR "%s%d: error - not an  MPPC or MPPE frame "
+		       "received\n", __FUNCTION__, state->unit);
+	    state->flushexpected = 1;
+	    return DECOMP_ERROR;
+	}
+    }
+
+    (state->stats).unc_bytes += uncomplen;
+    (state->stats).unc_packets++;
+
+    return uncomplen;
+}
+
+
+/************************************************************
+ * Module interface table
+ ************************************************************/
+
+/* These are in ppp_generic.c */
+extern int  ppp_register_compressor   (struct compressor *cp);
+extern void ppp_unregister_compressor (struct compressor *cp);
+
+/*
+ * Functions exported to ppp_generic.c.
+ *
+ * In case of MPPC/MPPE there is no need to process incompressible data
+ * because such a data is sent in MPPC/MPPE frame. Therefore the (*incomp)
+ * callback function isn't needed.
+ */
+struct compressor ppp_mppe = {
+    .compress_proto =	CI_MPPE,
+    .comp_alloc =	mppe_comp_alloc,
+    .comp_free =	mppe_comp_free,
+    .comp_init =	mppe_comp_init,
+    .comp_reset =	mppe_comp_reset,
+    .compress =		mppe_compress,
+    .comp_stat =	mppe_stats,
+    .decomp_alloc =	mppe_decomp_alloc,
+    .decomp_free =	mppe_comp_free,
+    .decomp_init =	mppe_decomp_init,
+    .decomp_reset =	mppe_decomp_reset,
+    .decompress =	mppe_decompress,
+    .incomp =		NULL,
+    .decomp_stat =	mppe_stats,
+    .owner =		THIS_MODULE
+};
+
+/************************************************************
+ * Module support routines
+ ************************************************************/
+
+int __init mppe_module_init(void)
+{
+    int answer;
+
+    if (!(crypto_alg_available("arc4", 0) && crypto_alg_available("sha1", 0))) {
+	printk(KERN_ERR "Kernel doesn't provide ARC4 and/or SHA1 algorithms "
+	       "required by MPPE/MPPC. Check CryptoAPI configuration.\n");
+	return -ENODEV;
+    }
+
+    /* Allocate space for SHAPad1, SHAPad2 and ... */
+    sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
+    if (sha_pad == NULL)
+	return -ENOMEM;
+    /* ... initialize them */
+    memset(sha_pad->sha_pad1, 0x00, sizeof(sha_pad->sha_pad1));
+    memset(sha_pad->sha_pad2, 0xf2, sizeof(sha_pad->sha_pad2));
+
+    answer = ppp_register_compressor(&ppp_mppe);
+    if (answer == 0) {
+	printk(KERN_INFO "MPPE/MPPC encryption/compression module registered\n");
+    }
+    return answer;
+}
+
+void __exit mppe_module_cleanup(void)
+{
+    kfree(sha_pad);
+    ppp_unregister_compressor(&ppp_mppe);
+    printk(KERN_INFO "MPPE/MPPC encryption/compression module unregistered\n");
+}
+
+module_init(mppe_module_init);
+module_exit(mppe_module_cleanup);
+
+MODULE_AUTHOR("Jan Dubiec <jdx@slackware.pl>");
+MODULE_DESCRIPTION("MPPE/MPPC encryption/compression module for Linux");
+MODULE_VERSION("1.3");
+MODULE_LICENSE("Dual BSD/GPL");
+MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
--- linux-2.6.19.2/include/linux/if_ppp.h.orig	2007-01-11 08:38:19.000000000 +0100
+++ linux-2.6.19.2/include/linux/if_ppp.h	2010-01-24 20:14:13.000000000 +0100
@@ -21,7 +21,7 @@
  */
 
 /*
- *  ==FILEVERSION 20050812==
+ *  ==FILEVERSION 20000724==
  *
  *  NOTE TO MAINTAINERS:
  *     If you modify this file at all, please set the above date.
@@ -72,8 +72,7 @@
 #define SC_LOG_RAWIN	0x00080000	/* log all chars received */
 #define SC_LOG_FLUSH	0x00100000	/* log all chars flushed */
 #define	SC_SYNC		0x00200000	/* synchronous serial mode */
-#define	SC_MUST_COMP    0x00400000	/* no uncompressed packets may be sent or received */
-#define	SC_MASK		0x0f600fff	/* bits that user can change */
+#define	SC_MASK		0x0f200fff	/* bits that user can change */
 
 /* state bits */
 #define SC_XMIT_BUSY	0x10000000	/* (used by isdn_ppp?) */
--- linux-2.6.19.2/include/linux/ppp-comp.h.orig	2007-01-11 08:38:19.000000000 +0100
+++ linux-2.6.19.2/include/linux/ppp-comp.h	2010-01-24 20:14:13.000000000 +0100
@@ -28,7 +28,7 @@
  */
 
 /*
- *  ==FILEVERSION 980319==
+ *  ==FILEVERSION 20040509==
  *
  *  NOTE TO MAINTAINERS:
  *     If you modify this file at all, please set the above date.
@@ -80,7 +80,7 @@
 
 	/* Compress a packet */
 	int     (*compress) (void *state, unsigned char *rptr,
-			      unsigned char *obuf, int isize, int osize);
+			     unsigned char *obuf, int isize, int osize);
 
 	/* Return compression statistics */
 	void	(*comp_stat) (void *state, struct compstat *stats);
@@ -101,7 +101,7 @@
 
 	/* Decompress a packet. */
 	int	(*decompress) (void *state, unsigned char *ibuf, int isize,
-				unsigned char *obuf, int osize);
+			       unsigned char *obuf, int osize);
 
 	/* Update state for an incompressible packet received */
 	void	(*incomp) (void *state, unsigned char *ibuf, int icnt);
@@ -111,8 +111,6 @@
 
 	/* Used in locking compressor modules */
 	struct module *owner;
-	/* Extra skb space needed by the compressor algorithm */
-	unsigned int comp_extra;
 };
 
 /*
@@ -193,11 +191,40 @@
 #define DEFLATE_CHK_SEQUENCE	0
 
 /*
- * Definitions for MPPE.
+ * Definitions for MPPE/MPPC.
  */
 
-#define CI_MPPE                18      /* config option for MPPE */
-#define CILEN_MPPE              6      /* length of config option */
+#define CI_MPPE			18	/* config option for MPPE */
+#define CILEN_MPPE		6	/* length of config option */
+
+#define MPPE_OVHD		4	/* MPPE overhead */
+#define MPPE_MAX_KEY_LEN	16	/* largest key length (128-bit) */
+
+#define MPPE_STATELESS          0x01	/* configuration bit H */
+#define MPPE_40BIT              0x20	/* configuration bit L */
+#define MPPE_56BIT              0x80	/* configuration bit M */
+#define MPPE_128BIT             0x40	/* configuration bit S */
+#define MPPE_MPPC               0x01	/* configuration bit C */
+
+/*
+ * Definitions for Stac LZS.
+ */
+
+#define CI_LZS			17	/* config option for Stac LZS */
+#define CILEN_LZS		5	/* length of config option */
+
+#define LZS_OVHD		4	/* max. LZS overhead */
+#define LZS_HIST_LEN		2048	/* LZS history size */
+#define LZS_MAX_CCOUNT		0x0FFF	/* max. coherency counter value */
+
+#define LZS_MODE_NONE		0
+#define LZS_MODE_LCB		1
+#define LZS_MODE_CRC		2
+#define LZS_MODE_SEQ		3
+#define LZS_MODE_EXT		4
+
+#define LZS_EXT_BIT_FLUSHED	0x80	/* bit A */
+#define LZS_EXT_BIT_COMP	0x20	/* bit C */
 
 /*
  * Definitions for other, as yet unsupported, compression methods.
