1 MPPE/MPPC kernel module for Linux
3 Microsoft Point-to-Point Encryption / Compression support
5 http://www.polbox.com/h/hs001/linux-2.4.29-mppe-mppc-1.3.patch.gz
7 Index: linux-2.4.35.4/Documentation/Configure.help
8 ===================================================================
9 --- linux-2.4.35.4.orig/Documentation/Configure.help
10 +++ linux-2.4.35.4/Documentation/Configure.help
11 @@ -9974,6 +9974,28 @@ CONFIG_PPP_BSDCOMP
12 module; it is called bsd_comp.o and will show up in the directory
13 modules once you have said "make modules". If unsure, say N.
15 +Microsoft PPP compression/encryption (MPPC/MPPE)
17 + Support for the Microsoft Point-To-Point Compression (RFC2118) and
18 + Microsoft Point-To-Point Encryption (RFC3078). These protocols are
19 + supported by Microsoft Windows and wide range of "hardware" access
20 + servers. MPPE is common protocol in Virtual Private Networks. According
21 + to RFC3078, MPPE supports 40, 56 and 128-bit key lengths. Depending on
22 + PPP daemon configuration on both ends of the link, following scenarios
24 + - only compression (MPPC) is used,
25 + - only encryption (MPPE) is used,
26 + - compression and encryption (MPPC+MPPE) are used.
28 + Please note that Hi/Fn (http://www.hifn.com) holds patent on MPPC so
29 + you should check if this patent is valid in your country in order to
30 + avoid legal problems.
32 + For more information please visit http://free.polbox.pl/h/hs001
34 + To compile this driver as a module, choose M here. The module will
35 + be called ppp_mppe_mppc.o.
39 Support for PPP over Ethernet.
40 Index: linux-2.4.35.4/crypto/Config.in
41 ===================================================================
42 --- linux-2.4.35.4.orig/crypto/Config.in
43 +++ linux-2.4.35.4/crypto/Config.in
44 @@ -11,7 +11,9 @@ if [ "$CONFIG_INET_AH" = "y" -o \
45 "$CONFIG_INET6_AH" = "y" -o \
46 "$CONFIG_INET6_AH" = "m" -o \
47 "$CONFIG_INET6_ESP" = "y" -o \
48 - "$CONFIG_INET6_ESP" = "m" ]; then
49 + "$CONFIG_INET6_ESP" = "m" -o \
50 + "$CONFIG_PPP_MPPE_MPPC" = "y" -o \
51 + "$CONFIG_PPP_MPPE_MPPC" = "m" ]; then
52 define_bool CONFIG_CRYPTO y
54 bool 'Cryptographic API' CONFIG_CRYPTO
55 @@ -51,11 +53,24 @@ if [ "$CONFIG_CRYPTO" = "y" ]; then
56 "$CONFIG_INET6_AH" = "y" -o \
57 "$CONFIG_INET6_AH" = "m" -o \
58 "$CONFIG_INET6_ESP" = "y" -o \
59 - "$CONFIG_INET6_ESP" = "m" ]; then
60 - define_bool CONFIG_CRYPTO_SHA1 y
62 - tristate ' SHA1 digest algorithm' CONFIG_CRYPTO_SHA1
63 + "$CONFIG_INET6_ESP" = "m" -o \
64 + "$CONFIG_PPP_MPPE_MPPC" = "y" -o \
65 + "$CONFIG_PPP_MPPE_MPPC" = "m" ]; then
66 + if [ "$CONFIG_INET_AH" = "y" -o \
67 + "$CONFIG_INET_ESP" = "y" -o \
68 + "$CONFIG_INET6_AH" = "y" -o \
69 + "$CONFIG_INET6_ESP" = "y" -o \
70 + "$CONFIG_PPP_MPPE_MPPC" = "y" ]; then
71 + define_tristate CONFIG_CRYPTO_SHA1 y
73 + if [ "$CONFIG_CRYPTO_SHA1" != "y" -a \
74 + "$CONFIG_CRYPTO_SHA1" != "m" ]; then
75 + define_tristate CONFIG_CRYPTO_SHA1 m
79 + tristate ' SHA1 digest algorithm' CONFIG_CRYPTO_SHA1
81 tristate ' SHA256 digest algorithm' CONFIG_CRYPTO_SHA256
82 tristate ' SHA384 and SHA512 digest algorithms' CONFIG_CRYPTO_SHA512
83 tristate ' Whirlpool digest algorithms' CONFIG_CRYPTO_WP512
84 @@ -76,7 +91,20 @@ if [ "$CONFIG_CRYPTO" = "y" ]; then
85 tristate ' TEA and XTEA cipher algorithms' CONFIG_CRYPTO_TEA
86 tristate ' Khazad cipher algorithm' CONFIG_CRYPTO_KHAZAD
87 tristate ' Anubis cipher algorithm' CONFIG_CRYPTO_ANUBIS
89 + if [ "$CONFIG_PPP_MPPE_MPPC" = "y" -o \
90 + "$CONFIG_PPP_MPPE_MPPC" = "m" ]; then
91 + if [ "$CONFIG_PPP_MPPE_MPPC" = "y" ]; then
92 + define_tristate CONFIG_CRYPTO_ARC4 y
94 + if [ "$CONFIG_CRYPTO_ARC4" != "y" -a \
95 + "$CONFIG_CRYPTO_ARC4" != "m" ]; then
96 + define_tristate CONFIG_CRYPTO_ARC4 m
100 tristate ' ARC4 cipher algorithm' CONFIG_CRYPTO_ARC4
102 if [ "$CONFIG_INET_IPCOMP" = "y" -o \
103 "$CONFIG_INET_IPCOMP" = "m" -o \
104 "$CONFIG_INET6_IPCOMP" = "y" -o \
105 Index: linux-2.4.35.4/drivers/net/Config.in
106 ===================================================================
107 --- linux-2.4.35.4.orig/drivers/net/Config.in
108 +++ linux-2.4.35.4/drivers/net/Config.in
109 @@ -336,6 +336,7 @@ if [ ! "$CONFIG_PPP" = "n" ]; then
110 dep_tristate ' PPP support for sync tty ports' CONFIG_PPP_SYNC_TTY $CONFIG_PPP
111 dep_tristate ' PPP Deflate compression' CONFIG_PPP_DEFLATE $CONFIG_PPP
112 dep_tristate ' PPP BSD-Compress compression' CONFIG_PPP_BSDCOMP $CONFIG_PPP
113 + dep_tristate ' Microsoft PPP compression/encryption (MPPC/MPPE)' CONFIG_PPP_MPPE_MPPC $CONFIG_PPP
114 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
115 dep_tristate ' PPP over Ethernet (EXPERIMENTAL)' CONFIG_PPPOE $CONFIG_PPP
117 Index: linux-2.4.35.4/drivers/net/Makefile
118 ===================================================================
119 --- linux-2.4.35.4.orig/drivers/net/Makefile
120 +++ linux-2.4.35.4/drivers/net/Makefile
121 @@ -166,6 +166,7 @@ obj-$(CONFIG_PPP_ASYNC) += ppp_async.o
122 obj-$(CONFIG_PPP_SYNC_TTY) += ppp_synctty.o
123 obj-$(CONFIG_PPP_DEFLATE) += ppp_deflate.o
124 obj-$(CONFIG_PPP_BSDCOMP) += bsd_comp.o
125 +obj-$(CONFIG_PPP_MPPE_MPPC) += ppp_mppe_mppc.o
126 obj-$(CONFIG_PPPOE) += pppox.o pppoe.o
128 obj-$(CONFIG_SLIP) += slip.o
129 Index: linux-2.4.35.4/drivers/net/ppp_generic.c
130 ===================================================================
131 --- linux-2.4.35.4.orig/drivers/net/ppp_generic.c
132 +++ linux-2.4.35.4/drivers/net/ppp_generic.c
134 * PPP driver, written by Michael Callahan and Al Longyear, and
135 * subsequently hacked by Paul Mackerras.
137 - * ==FILEVERSION 20020217==
138 + * ==FILEVERSION 20040509==
141 #include <linux/config.h>
142 @@ -102,6 +102,7 @@ struct ppp {
143 spinlock_t rlock; /* lock for receive side 58 */
144 spinlock_t wlock; /* lock for transmit side 5c */
145 int mru; /* max receive unit 60 */
146 + int mru_alloc; /* MAX(1500,MRU) for dev_alloc_skb() */
147 unsigned int flags; /* control bits 64 */
148 unsigned int xstate; /* transmit state bits 68 */
149 unsigned int rstate; /* receive state bits 6c */
150 @@ -552,7 +553,9 @@ static int ppp_ioctl(struct inode *inode
152 if (get_user(val, (int *) arg))
155 + ppp->mru_alloc = ppp->mru = val;
156 + if (ppp->mru_alloc < PPP_MRU)
157 + ppp->mru_alloc = PPP_MRU; /* increase for broken peers */
161 @@ -1025,14 +1028,37 @@ ppp_send_frame(struct ppp *ppp, struct s
163 /* peek at outbound CCP frames */
164 ppp_ccp_peek(ppp, skb, 0);
166 + * When LZS or MPPE/MPPC has been negotiated we don't send
167 + * CCP_RESETACK after receiving CCP_RESETREQ; in fact pppd
168 + * sends such a packet but we silently discard it here
170 + if (CCP_CODE(skb->data+2) == CCP_RESETACK
171 + && (ppp->xcomp->compress_proto == CI_MPPE
172 + || ppp->xcomp->compress_proto == CI_LZS)) {
173 + --ppp->stats.tx_packets;
174 + ppp->stats.tx_bytes -= skb->len - 2;
181 /* try to do packet compression */
182 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
183 && proto != PPP_LCP && proto != PPP_CCP) {
184 - new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
188 + * because of possible data expansion when MPPC or LZS
189 + * is used, allocate compressor's buffer 12.5% bigger
192 + if (ppp->xcomp->compress_proto == CI_MPPE)
193 + comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + MPPE_OVHD;
194 + else if (ppp->xcomp->compress_proto == CI_LZS)
195 + comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + LZS_OVHD;
196 + new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len
197 + + comp_ovhd, GFP_ATOMIC);
199 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
201 @@ -1050,9 +1076,21 @@ ppp_send_frame(struct ppp *ppp, struct s
204 skb_pull(skb, 2); /* pull off A/C bytes */
206 + } else if (len == 0) {
207 /* didn't compress, or CCP not up yet */
212 + * MPPE requires that we do not send unencrypted
213 + * frames. The compressor will return -1 if we
214 + * should drop the frame. We cannot simply test
215 + * the compress_proto because MPPE and MPPC share
218 + printk(KERN_ERR "ppp: compressor dropped pkt\n");
219 + kfree_skb(new_skb);
224 @@ -1540,14 +1578,15 @@ ppp_decompress_frame(struct ppp *ppp, st
227 if (proto == PPP_COMP) {
228 - ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
229 + ns = dev_alloc_skb(ppp->mru_alloc + PPP_HDRLEN);
231 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
234 /* the decompressor still expects the A/C bytes in the hdr */
235 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
236 - skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
237 + skb->len + 2, ns->data,
238 + ppp->mru_alloc + PPP_HDRLEN);
240 /* Pass the compressed frame to pppd as an
242 @@ -1573,7 +1612,14 @@ ppp_decompress_frame(struct ppp *ppp, st
246 - ppp->rstate |= SC_DC_ERROR;
247 + if (ppp->rcomp->compress_proto != CI_MPPE
248 + && ppp->rcomp->compress_proto != CI_LZS) {
250 + * If decompression protocol isn't MPPE/MPPC or LZS, we set
251 + * SC_DC_ERROR flag and wait for CCP_RESETACK
253 + ppp->rstate |= SC_DC_ERROR;
255 ppp_receive_error(ppp);
258 @@ -2253,6 +2299,7 @@ ppp_create_interface(int unit, int *retp
259 /* Initialize the new ppp unit */
260 ppp->file.index = unit;
262 + ppp->mru_alloc = PPP_MRU;
263 init_ppp_file(&ppp->file, INTERFACE);
264 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
265 for (i = 0; i < NUM_NP; ++i)
266 Index: linux-2.4.35.4/drivers/net/ppp_mppe_mppc.c
267 ===================================================================
269 +++ linux-2.4.35.4/drivers/net/ppp_mppe_mppc.c
272 + * ppp_mppe_mppc.c - MPPC/MPPE "compressor/decompressor" module.
274 + * Copyright (c) 1994 Árpád Magosányi <mag@bunuel.tii.matav.hu>
275 + * Copyright (c) 1999 Tim Hockin, Cobalt Networks Inc. <thockin@cobaltnet.com>
276 + * Copyright (c) 2002-2005 Jan Dubiec <jdx@slackware.pl>
278 + * Permission to use, copy, modify, and distribute this software and its
279 + * documentation is hereby granted, provided that the above copyright
280 + * notice appears in all copies. This software is provided without any
281 + * warranty, express or implied.
283 + * The code is based on MPPE kernel module written by Árpád Magosányi and
284 + * Tim Hockin which can be found on http://planetmirror.com/pub/mppe/.
285 + * I have added MPPC and 56 bit session keys support in MPPE.
287 + * WARNING! Although this is open source code, its usage in some countries
288 + * (in particular in the USA) may violate Stac Inc. patent for MPPC.
290 + * ==FILEVERSION 20050202==
294 +#include <linux/init.h>
295 +#include <linux/module.h>
296 +#include <linux/mm.h>
297 +#include <linux/slab.h>
298 +#include <asm/scatterlist.h>
299 +#include <linux/vmalloc.h>
300 +#include <linux/crypto.h>
302 +#include <linux/ppp_defs.h>
303 +#include <linux/ppp-comp.h>
306 + * In 2.4.x kernels macro offset_in_page() is not defined in linux/mm.h so
307 + * we define it here; PAGE_MASK is defined in asm/page.h which is included
310 +#define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
313 + * State for a mppc/mppe "(de)compressor".
315 +struct ppp_mppe_state {
316 + struct crypto_tfm *arc4_tfm;
317 + struct crypto_tfm *sha1_tfm;
319 + u8 master_key[MPPE_MAX_KEY_LEN];
320 + u8 session_key[MPPE_MAX_KEY_LEN];
321 + u8 mppc; /* do we use compression (MPPC)? */
322 + u8 mppe; /* do we use encryption (MPPE)? */
323 + u8 keylen; /* key length in bytes */
324 + u8 bitkeylen; /* key length in bits */
325 + u16 ccount; /* coherency counter */
326 + u16 bits; /* MPPC/MPPE control bits */
327 + u8 stateless; /* do we use stateless mode? */
328 + u8 nextflushed; /* set A bit in the next outgoing packet;
329 + used only by compressor*/
330 + u8 flushexpected; /* drop packets until A bit is received;
331 + used only by decompressor*/
332 + u8 *hist; /* MPPC history */
333 + u16 *hash; /* Hash table; used only by compressor */
334 + u16 histptr; /* history "cursor" */
338 + struct compstat stats;
341 +#define MPPE_HIST_LEN 8192 /* MPPC history size */
342 +#define MPPE_MAX_CCOUNT 0x0FFF /* max. coherency counter value */
344 +#define MPPE_BIT_FLUSHED 0x80 /* bit A */
345 +#define MPPE_BIT_RESET 0x40 /* bit B */
346 +#define MPPE_BIT_COMP 0x20 /* bit C */
347 +#define MPPE_BIT_ENCRYPTED 0x10 /* bit D */
349 +#define MPPE_SALT0 0xD1 /* values used in MPPE key derivation */
350 +#define MPPE_SALT1 0x26 /* according to RFC3079 */
351 +#define MPPE_SALT2 0x9E
353 +#define MPPE_CCOUNT(x) ((((x)[4] & 0x0f) << 8) + (x)[5])
354 +#define MPPE_BITS(x) ((x)[4] & 0xf0)
355 +#define MPPE_CTRLHI(x) ((((x)->ccount & 0xf00)>>8)|((x)->bits))
356 +#define MPPE_CTRLLO(x) ((x)->ccount & 0xff)
359 + * Kernel Crypto API needs its arguments to be in kmalloc'd memory, not in the
360 + * module static data area. That means sha_pad needs to be kmalloc'd. It is done
361 + * in mppe_module_init(). This has been pointed out on 30th July 2004 by Oleg
362 + * Makarenko on pptpclient-devel mailing list.
364 +#define SHA1_PAD_SIZE 40
366 + unsigned char sha_pad1[SHA1_PAD_SIZE];
367 + unsigned char sha_pad2[SHA1_PAD_SIZE];
369 +static struct sha_pad *sha_pad;
372 +setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
374 + sg[0].page = virt_to_page(address);
375 + sg[0].offset = offset_in_page(address);
376 + sg[0].length = length;
380 +arc4_setkey(struct ppp_mppe_state *state, const unsigned char *key,
381 + const unsigned int keylen)
383 + crypto_cipher_setkey(state->arc4_tfm, key, keylen);
387 +arc4_encrypt(struct ppp_mppe_state *state, const unsigned char *in,
388 + const unsigned int len, unsigned char *out)
390 + struct scatterlist sgin[4], sgout[4];
392 + setup_sg(sgin, in, len);
393 + setup_sg(sgout, out, len);
394 + crypto_cipher_encrypt(state->arc4_tfm, sgout, sgin, len);
397 +#define arc4_decrypt arc4_encrypt
400 + * Key Derivation, from RFC 3078, RFC 3079.
401 + * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
404 +get_new_key_from_sha(struct ppp_mppe_state *state, unsigned char *interim_key)
406 + struct scatterlist sg[4];
408 + setup_sg(&sg[0], state->master_key, state->keylen);
409 + setup_sg(&sg[1], sha_pad->sha_pad1, sizeof(sha_pad->sha_pad1));
410 + setup_sg(&sg[2], state->session_key, state->keylen);
411 + setup_sg(&sg[3], sha_pad->sha_pad2, sizeof(sha_pad->sha_pad2));
413 + crypto_digest_digest (state->sha1_tfm, sg, 4, state->sha1_digest);
415 + memcpy(interim_key, state->sha1_digest, state->keylen);
419 +mppe_change_key(struct ppp_mppe_state *state, int initialize)
421 + unsigned char interim_key[MPPE_MAX_KEY_LEN];
423 + get_new_key_from_sha(state, interim_key);
425 + memcpy(state->session_key, interim_key, state->keylen);
427 + arc4_setkey(state, interim_key, state->keylen);
428 + arc4_encrypt(state, interim_key, state->keylen, state->session_key);
430 + if (state->keylen == 8) {
431 + if (state->bitkeylen == 40) {
432 + state->session_key[0] = MPPE_SALT0;
433 + state->session_key[1] = MPPE_SALT1;
434 + state->session_key[2] = MPPE_SALT2;
436 + state->session_key[0] = MPPE_SALT0;
439 + arc4_setkey(state, state->session_key, state->keylen);
442 +/* increase 12-bit coherency counter */
444 +mppe_increase_ccount(struct ppp_mppe_state *state)
446 + state->ccount = (state->ccount + 1) & MPPE_MAX_CCOUNT;
448 + if (state->stateless) {
449 + mppe_change_key(state, 0);
450 + state->nextflushed = 1;
452 + if ((state->ccount & 0xff) == 0xff) {
453 + mppe_change_key(state, 0);
459 +/* allocate space for a MPPE/MPPC (de)compressor. */
460 +/* comp != 0 -> init compressor */
461 +/* comp = 0 -> init decompressor */
463 +mppe_alloc(unsigned char *options, int opt_len, int comp)
465 + struct ppp_mppe_state *state;
466 + unsigned int digestsize;
469 + fname = comp ? "mppe_comp_alloc" : "mppe_decomp_alloc";
472 + * Hack warning - additionally to the standard MPPC/MPPE configuration
473 + * options, pppd passes to the (de)copressor 8 or 16 byte session key.
474 + * Therefore options[1] contains MPPC/MPPE configuration option length
475 + * (CILEN_MPPE = 6), but the real options length, depending on the key
476 + * length, is 6+8 or 6+16.
478 + if (opt_len < CILEN_MPPE) {
479 + printk(KERN_WARNING "%s: wrong options length: %u\n", fname, opt_len);
483 + if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
484 + (options[2] & ~MPPE_STATELESS) != 0 ||
485 + options[3] != 0 || options[4] != 0 ||
486 + (options[5] & ~(MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
487 + (options[5] & (MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
488 + printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
489 + "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname, options[0],
490 + options[1], options[2], options[3], options[4], options[5]);
494 + state = (struct ppp_mppe_state *)kmalloc(sizeof(*state), GFP_KERNEL);
495 + if (state == NULL) {
496 + printk(KERN_ERR "%s: cannot allocate space for %scompressor\n", fname,
500 + memset(state, 0, sizeof(struct ppp_mppe_state));
502 + state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */
503 + state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
504 + MPPE_40BIT); /* Do we use MPPE? */
507 + /* allocate MPPC history */
508 + state->hist = (u8*)vmalloc(2*MPPE_HIST_LEN*sizeof(u8));
509 + if (state->hist == NULL) {
511 + printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
516 + /* allocate hashtable for MPPC compressor */
518 + state->hash = (u16*)vmalloc(MPPE_HIST_LEN*sizeof(u16));
519 + if (state->hash == NULL) {
520 + vfree(state->hist);
522 + printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
529 + if (state->mppe) { /* specific for MPPE */
530 + /* Load ARC4 algorithm */
531 + state->arc4_tfm = crypto_alloc_tfm("arc4", 0);
532 + if (state->arc4_tfm == NULL) {
534 + vfree(state->hash);
536 + vfree(state->hist);
539 + printk(KERN_ERR "%s: cannot load ARC4 module\n", fname);
543 + /* Load SHA1 algorithm */
544 + state->sha1_tfm = crypto_alloc_tfm("sha1", 0);
545 + if (state->sha1_tfm == NULL) {
546 + crypto_free_tfm(state->arc4_tfm);
548 + vfree(state->hash);
550 + vfree(state->hist);
553 + printk(KERN_ERR "%s: cannot load SHA1 module\n", fname);
557 + digestsize = crypto_tfm_alg_digestsize(state->sha1_tfm);
558 + if (digestsize < MPPE_MAX_KEY_LEN) {
559 + crypto_free_tfm(state->sha1_tfm);
560 + crypto_free_tfm(state->arc4_tfm);
562 + vfree(state->hash);
564 + vfree(state->hist);
567 + printk(KERN_ERR "%s: CryptoAPI SHA1 digest size too small\n", fname);
570 + state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
571 + if (!state->sha1_digest) {
572 + crypto_free_tfm(state->sha1_tfm);
573 + crypto_free_tfm(state->arc4_tfm);
575 + vfree(state->hash);
577 + vfree(state->hist);
580 + printk(KERN_ERR "%s: cannot allocate space for SHA1 digest\n", fname);
583 + memcpy(state->master_key, options+CILEN_MPPE, MPPE_MAX_KEY_LEN);
584 + memcpy(state->session_key, state->master_key, MPPE_MAX_KEY_LEN);
585 + /* initial key generation is done in mppe_init() */
590 + return (void *) state;
594 +mppe_comp_alloc(unsigned char *options, int opt_len)
596 + return mppe_alloc(options, opt_len, 1);
600 +mppe_decomp_alloc(unsigned char *options, int opt_len)
602 + return mppe_alloc(options, opt_len, 0);
605 +/* cleanup the (de)compressor */
607 +mppe_comp_free(void *arg)
609 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
611 + if (state != NULL) {
613 + if (state->sha1_tfm != NULL)
614 + crypto_free_tfm(state->sha1_tfm);
615 + if (state->arc4_tfm != NULL)
616 + crypto_free_tfm(state->arc4_tfm);
618 + if (state->hist != NULL)
619 + vfree(state->hist);
620 + if (state->hash != NULL)
621 + vfree(state->hash);
628 +/* init MPPC/MPPE (de)compresor */
629 +/* comp != 0 -> init compressor */
630 +/* comp = 0 -> init decompressor */
632 +mppe_init(void *arg, unsigned char *options, int opt_len, int unit,
633 + int hdrlen, int mru, int debug, int comp)
635 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
638 + fname = comp ? "mppe_comp_init" : "mppe_decomp_init";
640 + if (opt_len < CILEN_MPPE) {
642 + printk(KERN_WARNING "%s: wrong options length: %u\n",
647 + if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
648 + (options[2] & ~MPPE_STATELESS) != 0 ||
649 + options[3] != 0 || options[4] != 0 ||
650 + (options[5] & ~(MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
651 + (options[5] & (MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
653 + printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
654 + "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname,
655 + options[0], options[1], options[2], options[3], options[4],
660 + if ((options[5] & ~MPPE_MPPC) != MPPE_128BIT &&
661 + (options[5] & ~MPPE_MPPC) != MPPE_56BIT &&
662 + (options[5] & ~MPPE_MPPC) != MPPE_40BIT &&
663 + (options[5] & MPPE_MPPC) != MPPE_MPPC) {
665 + printk(KERN_WARNING "%s: don't know what to do: o[5]=%02x\n",
666 + fname, options[5]);
670 + state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */
671 + state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
672 + MPPE_40BIT); /* Do we use MPPE? */
673 + state->stateless = options[2] & MPPE_STATELESS; /* Do we use stateless mode? */
675 + switch (state->mppe) {
676 + case MPPE_40BIT: /* 40 bit key */
678 + state->bitkeylen = 40;
680 + case MPPE_56BIT: /* 56 bit key */
682 + state->bitkeylen = 56;
684 + case MPPE_128BIT: /* 128 bit key */
685 + state->keylen = 16;
686 + state->bitkeylen = 128;
690 + state->bitkeylen = 0;
693 + state->ccount = MPPE_MAX_CCOUNT;
695 + state->unit = unit;
696 + state->debug = debug;
697 + state->histptr = MPPE_HIST_LEN;
698 + if (state->mppc) { /* reset history if MPPC was negotiated */
699 + memset(state->hist, 0, 2*MPPE_HIST_LEN*sizeof(u8));
702 + if (state->mppe) { /* generate initial session keys */
703 + mppe_change_key(state, 1);
706 + if (comp) { /* specific for compressor */
707 + state->nextflushed = 1;
708 + } else { /* specific for decompressor */
710 + state->flushexpected = 1;
717 +mppe_comp_init(void *arg, unsigned char *options, int opt_len, int unit,
718 + int hdrlen, int debug)
720 + return mppe_init(arg, options, opt_len, unit, hdrlen, 0, debug, 1);
725 +mppe_decomp_init(void *arg, unsigned char *options, int opt_len, int unit,
726 + int hdrlen, int mru, int debug)
728 + return mppe_init(arg, options, opt_len, unit, hdrlen, mru, debug, 0);
732 +mppe_comp_reset(void *arg)
734 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
737 + printk(KERN_DEBUG "%s%d: resetting MPPC/MPPE compressor\n",
738 + __FUNCTION__, state->unit);
740 + state->nextflushed = 1;
742 + arc4_setkey(state, state->session_key, state->keylen);
746 +mppe_decomp_reset(void *arg)
748 + /* When MPPC/MPPE is in use, we shouldn't receive any CCP Reset-Ack.
749 + But when we receive such a packet, we just ignore it. */
754 +mppe_stats(void *arg, struct compstat *stats)
756 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
758 + *stats = state->stats;
761 +/***************************/
762 +/**** Compression stuff ****/
763 +/***************************/
764 +/* inserts 1 to 8 bits into the output buffer */
765 +static inline void putbits8(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
771 + *buf = *buf | (val & 0xff);
781 + *buf = *buf | ((val >> 8) & 0xff);
782 + *(++buf) = val & 0xff;
786 +/* inserts 9 to 16 bits into the output buffer */
787 +static inline void putbits16(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
794 + *buf = *buf | ((val >> 8) & 0xff);
795 + *(++buf) = val & 0xff;
803 + *l = 16 - n + (*l);
805 + *buf = *buf | ((val >> 16) & 0xff);
806 + *(++buf) = (val >> 8) & 0xff;
807 + *(++buf) = val & 0xff;
811 +/* inserts 17 to 24 bits into the output buffer */
812 +static inline void putbits24(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
815 + if (*l >= n - 16) {
817 + *l = 16 - n + (*l);
819 + *buf = *buf | ((val >> 16) & 0xff);
820 + *(++buf) = (val >> 8) & 0xff;
821 + *(++buf) = val & 0xff;
828 + (*i)++; (*i)++; (*i)++;
829 + *l = 24 - n + (*l);
831 + *buf = *buf | ((val >> 24) & 0xff);
832 + *(++buf) = (val >> 16) & 0xff;
833 + *(++buf) = (val >> 8) & 0xff;
834 + *(++buf) = val & 0xff;
839 +mppc_compress(struct ppp_mppe_state *state, unsigned char *ibuf,
840 + unsigned char *obuf, int isize, int osize)
842 + u32 olen, off, len, idx, i, l;
843 + u8 *hist, *sbuf, *p, *q, *r, *s;
846 + At this point, to avoid possible buffer overflow caused by packet
847 + expansion during/after compression, we should make sure that
848 + osize >= (((isize*9)/8)+1)+2, but we don't do that because in
849 + ppp_generic.c we simply allocate bigger obuf.
851 + Maximum MPPC packet expansion is 12.5%. This is the worst case when
852 + all octets in the input buffer are >= 0x80 and we cannot find any
853 + repeated tokens. Additionally we have to reserve 2 bytes for MPPE/MPPC
854 + status bits and coherency counter.
857 + hist = state->hist + MPPE_HIST_LEN;
858 + /* check if there is enough room at the end of the history */
859 + if (state->histptr + isize >= 2*MPPE_HIST_LEN) {
860 + state->bits |= MPPE_BIT_RESET;
861 + state->histptr = MPPE_HIST_LEN;
862 + memcpy(state->hist, hist, MPPE_HIST_LEN);
864 + /* add packet to the history; isize must be <= MPPE_HIST_LEN */
865 + sbuf = state->hist + state->histptr;
866 + memcpy(sbuf, ibuf, isize);
867 + state->histptr += isize;
869 + /* compress data */
871 + *obuf = olen = i = 0;
873 + while (i < isize - 2) {
875 + idx = ((40543*((((s[0]<<4)^s[1])<<4)^s[2]))>>4) & 0x1fff;
876 + p = hist + state->hash[idx];
877 + state->hash[idx] = (u16) (s - hist);
879 + if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ || *p++ != *s++ ||
881 + /* no match found; encode literal byte */
882 + if (ibuf[i] < 0x80) { /* literal byte < 0x80 */
883 + putbits8(obuf, (u32) ibuf[i], 8, &olen, &l);
884 + } else { /* literal byte >= 0x80 */
885 + putbits16(obuf, (u32) (0x100|(ibuf[i]&0x7f)), 9, &olen, &l);
891 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
892 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
893 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
894 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
895 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
896 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
897 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
898 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
899 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
900 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
901 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
902 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
903 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
904 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
905 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
909 + while((*p++ == *s++) && (s < r) && (p < q));
912 + while((*p++ == *s++) && (s < r) && (p < q));
917 + /* at least 3 character match found; code data */
918 + /* encode offset */
919 + if (off < 64) { /* 10-bit offset; 0 <= offset < 64 */
920 + putbits16(obuf, 0x3c0|off, 10, &olen, &l);
921 + } else if (off < 320) { /* 12-bit offset; 64 <= offset < 320 */
922 + putbits16(obuf, 0xe00|(off-64), 12, &olen, &l);
923 + } else if (off < 8192) { /* 16-bit offset; 320 <= offset < 8192 */
924 + putbits16(obuf, 0xc000|(off-320), 16, &olen, &l);
926 + /* This shouldn't happen; we return 0 what means "packet expands",
927 + and we send packet uncompressed. */
929 + printk(KERN_DEBUG "%s%d: wrong offset value: %d\n",
930 + __FUNCTION__, state->unit, off);
933 + /* encode length of match */
934 + if (len < 4) { /* length = 3 */
935 + putbits8(obuf, 0, 1, &olen, &l);
936 + } else if (len < 8) { /* 4 <= length < 8 */
937 + putbits8(obuf, 0x08|(len&0x03), 4, &olen, &l);
938 + } else if (len < 16) { /* 8 <= length < 16 */
939 + putbits8(obuf, 0x30|(len&0x07), 6, &olen, &l);
940 + } else if (len < 32) { /* 16 <= length < 32 */
941 + putbits8(obuf, 0xe0|(len&0x0f), 8, &olen, &l);
942 + } else if (len < 64) { /* 32 <= length < 64 */
943 + putbits16(obuf, 0x3c0|(len&0x1f), 10, &olen, &l);
944 + } else if (len < 128) { /* 64 <= length < 128 */
945 + putbits16(obuf, 0xf80|(len&0x3f), 12, &olen, &l);
946 + } else if (len < 256) { /* 128 <= length < 256 */
947 + putbits16(obuf, 0x3f00|(len&0x7f), 14, &olen, &l);
948 + } else if (len < 512) { /* 256 <= length < 512 */
949 + putbits16(obuf, 0xfe00|(len&0xff), 16, &olen, &l);
950 + } else if (len < 1024) { /* 512 <= length < 1024 */
951 + putbits24(obuf, 0x3fc00|(len&0x1ff), 18, &olen, &l);
952 + } else if (len < 2048) { /* 1024 <= length < 2048 */
953 + putbits24(obuf, 0xff800|(len&0x3ff), 20, &olen, &l);
954 + } else if (len < 4096) { /* 2048 <= length < 4096 */
955 + putbits24(obuf, 0x3ff000|(len&0x7ff), 22, &olen, &l);
956 + } else if (len < 8192) { /* 4096 <= length < 8192 */
957 + putbits24(obuf, 0xffe000|(len&0xfff), 24, &olen, &l);
959 + /* This shouldn't happen; we return 0 what means "packet expands",
960 + and send packet uncompressed. */
962 + printk(KERN_DEBUG "%s%d: wrong length of match value: %d\n",
963 + __FUNCTION__, state->unit, len);
968 + /* Add remaining octets to the output */
969 + while(isize - i > 0) {
970 + if (ibuf[i] < 0x80) { /* literal byte < 0x80 */
971 + putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l);
972 + } else { /* literal byte >= 0x80 */
973 + putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l);
976 + /* Reset unused bits of the last output octet */
977 + if ((l != 0) && (l != 8)) {
978 + putbits8(obuf, 0, l, &olen, &l);
985 +mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
986 + int isize, int osize)
988 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
989 + int proto, olen, complen, off;
990 + unsigned char *wptr;
992 + /* Check that the protocol is in the range we handle. */
993 + proto = PPP_PROTOCOL(ibuf);
994 + if (proto < 0x0021 || proto > 0x00fa)
998 + /* Copy over the PPP header */
999 + wptr[0] = PPP_ADDRESS(ibuf);
1000 + wptr[1] = PPP_CONTROL(ibuf);
1001 + wptr[2] = PPP_COMP >> 8;
1002 + wptr[3] = PPP_COMP;
1003 + wptr += PPP_HDRLEN + (MPPE_OVHD / 2); /* Leave two octets for MPPE/MPPC bits */
1006 + * In ver. 0.99 protocol field was compressed. Deflate and BSD compress
1007 + * do PFC before actual compression, RCF2118 and RFC3078 are not precise
1008 + * on this topic so I decided to do PFC. Unfortunately this change caused
1009 + * incompatibility with older/other MPPE/MPPC modules. I have received
1010 + * a lot of complaints from unexperienced users so I have decided to revert
1011 + * to previous state, i.e. the protocol field is sent uncompressed now.
1012 + * Although this may be changed in the future.
1014 + * Receiving side (mppe_decompress()) still accepts packets with compressed
1015 + * and uncompressed protocol field so you shouldn't get "Unsupported protocol
1016 + * 0x2145 received" messages anymore.
1018 + //off = (proto > 0xff) ? 2 : 3; /* PFC - skip first protocol byte if 0 */
1023 + mppe_increase_ccount(state);
1025 + if (state->nextflushed) {
1026 + state->bits |= MPPE_BIT_FLUSHED;
1027 + state->nextflushed = 0;
1028 + if (state->mppe && !state->stateless) {
1030 + * If this is the flag packet, the key has been already changed in
1031 + * mppe_increase_ccount() so we dont't do it once again.
1033 + if ((state->ccount & 0xff) != 0xff) {
1034 + arc4_setkey(state, state->session_key, state->keylen);
1037 + if (state->mppc) { /* reset history */
1038 + state->bits |= MPPE_BIT_RESET;
1039 + state->histptr = MPPE_HIST_LEN;
1040 + memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
1044 + if (state->mppc && !state->mppe) { /* Do only compression */
1045 + complen = mppc_compress(state, ibuf, wptr, isize - off,
1046 + osize - PPP_HDRLEN - (MPPE_OVHD / 2));
1048 + * TODO: Implement an heuristics to handle packet expansion in a smart
1049 + * way. Now, when a packet expands, we send it as uncompressed and
1050 + * when next packet is sent we have to reset compressor's history.
1051 + * Maybe it would be better to send such packet as compressed in order
1052 + * to keep history's continuity.
1054 + if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
1056 + /* packet expands */
1057 + state->nextflushed = 1;
1058 + memcpy(wptr, ibuf, isize - off);
1059 + olen = isize - (off - 2) + MPPE_OVHD;
1060 + (state->stats).inc_bytes += olen;
1061 + (state->stats).inc_packets++;
1063 + state->bits |= MPPE_BIT_COMP;
1064 + olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
1065 + (state->stats).comp_bytes += olen;
1066 + (state->stats).comp_packets++;
1068 + } else { /* Do encryption with or without compression */
1069 + state->bits |= MPPE_BIT_ENCRYPTED;
1070 + if (!state->mppc && state->mppe) { /* Do only encryption */
1071 + /* read from ibuf, write to wptr, adjust for PPP_HDRLEN */
1072 + arc4_encrypt(state, ibuf, isize - off, wptr);
1073 + olen = isize - (off - 2) + MPPE_OVHD;
1074 + (state->stats).inc_bytes += olen;
1075 + (state->stats).inc_packets++;
1076 + } else { /* Do compression and then encryption - RFC3078 */
1077 + complen = mppc_compress(state, ibuf, wptr, isize - off,
1078 + osize - PPP_HDRLEN - (MPPE_OVHD / 2));
1080 + * TODO: Implement an heuristics to handle packet expansion in a smart
1081 + * way. Now, when a packet expands, we send it as uncompressed and
1082 + * when next packet is sent we have to reset compressor's history.
1083 + * Maybe it would be good to send such packet as compressed in order
1084 + * to keep history's continuity.
1086 + if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
1088 + /* packet expands */
1089 + state->nextflushed = 1;
1090 + arc4_encrypt(state, ibuf, isize - off, wptr);
1091 + olen = isize - (off - 2) + MPPE_OVHD;
1092 + (state->stats).inc_bytes += olen;
1093 + (state->stats).inc_packets++;
1095 + state->bits |= MPPE_BIT_COMP;
1096 + /* Hack warning !!! RC4 implementation which we use does
1097 + encryption "in place" - it means that input and output
1098 + buffers can be *the same* memory area. Therefore we don't
1099 + need to use a temporary buffer. But be careful - other
1100 + implementations don't have to be so nice.
1101 + I used to use ibuf as temporary buffer here, but it led
1102 + packet sniffers into error. Thanks to Wilfried Weissmann
1103 + for pointing that. */
1104 + arc4_encrypt(state, wptr, complen, wptr);
1105 + olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
1106 + (state->stats).comp_bytes += olen;
1107 + (state->stats).comp_packets++;
1112 + /* write status bits and coherency counter into the output buffer */
1113 + wptr = obuf + PPP_HDRLEN;
1114 + wptr[0] = MPPE_CTRLHI(state);
1115 + wptr[1] = MPPE_CTRLLO(state);
1119 + (state->stats).unc_bytes += isize;
1120 + (state->stats).unc_packets++;
1125 +/***************************/
1126 +/*** Decompression stuff ***/
1127 +/***************************/
1128 +static inline u32 getbits(const u8 *buf, const u32 n, u32 *i, u32 *l)
1130 + static const u32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1136 + res = (buf[*i] & m[ol]) >> (*l);
1142 + *l = 8 - n + (*l);
1143 + res = (buf[(*i)++] & m[ol]) << 8;
1144 + res = (res | buf[*i]) >> (*l);
1150 +static inline u32 getbyte(const u8 *buf, const u32 i, const u32 l)
1155 + return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff;
1159 +static inline void lamecopy(u8 *dst, u8 *src, u32 len)
1166 +mppc_decompress(struct ppp_mppe_state *state, unsigned char *ibuf,
1167 + unsigned char *obuf, int isize, int osize)
1169 + u32 olen, off, len, bits, val, sig, i, l;
1172 + history = state->hist + state->histptr;
1173 + olen = len = i = 0;
1176 + while (bits >= 8) {
1177 + val = getbyte(ibuf, i++, l);
1178 + if (val < 0x80) { /* literal byte < 0x80 */
1179 + if (state->histptr < 2*MPPE_HIST_LEN) {
1180 + /* copy uncompressed byte to the history */
1181 + (state->hist)[(state->histptr)++] = (u8) val;
1183 + /* buffer overflow; drop packet */
1185 + printk(KERN_ERR "%s%d: trying to write outside history "
1186 + "buffer\n", __FUNCTION__, state->unit);
1187 + return DECOMP_ERROR;
1195 + if (sig == 0x80) { /* literal byte >= 0x80 */
1196 + if (state->histptr < 2*MPPE_HIST_LEN) {
1197 + /* copy uncompressed byte to the history */
1198 + (state->hist)[(state->histptr)++] =
1199 + (u8) (0x80|((val&0x3f)<<1)|getbits(ibuf, 1 , &i ,&l));
1201 + /* buffer overflow; drop packet */
1203 + printk(KERN_ERR "%s%d: trying to write outside history "
1204 + "buffer\n", __FUNCTION__, state->unit);
1205 + return DECOMP_ERROR;
1212 + /* Not a literal byte so it must be an (offset,length) pair */
1213 + /* decode offset */
1215 + if (sig == 0xf0) { /* 10-bit offset; 0 <= offset < 64 */
1216 + off = (((val&0x0f)<<2)|getbits(ibuf, 2 , &i ,&l));
1219 + if (sig == 0xe0) { /* 12-bit offset; 64 <= offset < 320 */
1220 + off = ((((val&0x0f)<<4)|getbits(ibuf, 4 , &i ,&l))+64);
1223 + if ((sig&0xe0) == 0xc0) {/* 16-bit offset; 320 <= offset < 8192 */
1224 + off = ((((val&0x1f)<<8)|getbyte(ibuf, i++, l))+320);
1226 + if (off > MPPE_HIST_LEN - 1) {
1228 + printk(KERN_DEBUG "%s%d: too big offset value: %d\n",
1229 + __FUNCTION__, state->unit, off);
1230 + return DECOMP_ERROR;
1232 + } else { /* this shouldn't happen */
1234 + printk(KERN_DEBUG "%s%d: cannot decode offset value\n",
1235 + __FUNCTION__, state->unit);
1236 + return DECOMP_ERROR;
1240 + /* decode length of match */
1241 + val = getbyte(ibuf, i, l);
1242 + if ((val & 0x80) == 0x00) { /* len = 3 */
1245 + getbits(ibuf, 1 , &i ,&l);
1246 + } else if ((val & 0xc0) == 0x80) { /* 4 <= len < 8 */
1247 + len = 0x04 | ((val>>4) & 0x03);
1249 + getbits(ibuf, 4 , &i ,&l);
1250 + } else if ((val & 0xe0) == 0xc0) { /* 8 <= len < 16 */
1251 + len = 0x08 | ((val>>2) & 0x07);
1253 + getbits(ibuf, 6 , &i ,&l);
1254 + } else if ((val & 0xf0) == 0xe0) { /* 16 <= len < 32 */
1255 + len = 0x10 | (val & 0x0f);
1260 + val = (val << 8) | getbyte(ibuf, ++i, l);
1261 + if ((val & 0xf800) == 0xf000) { /* 32 <= len < 64 */
1262 + len = 0x0020 | ((val >> 6) & 0x001f);
1264 + getbits(ibuf, 2 , &i ,&l);
1265 + } else if ((val & 0xfc00) == 0xf800) { /* 64 <= len < 128 */
1266 + len = 0x0040 | ((val >> 4) & 0x003f);
1268 + getbits(ibuf, 4 , &i ,&l);
1269 + } else if ((val & 0xfe00) == 0xfc00) { /* 128 <= len < 256 */
1270 + len = 0x0080 | ((val >> 2) & 0x007f);
1272 + getbits(ibuf, 6 , &i ,&l);
1273 + } else if ((val & 0xff00) == 0xfe00) { /* 256 <= len < 512 */
1274 + len = 0x0100 | (val & 0x00ff);
1279 + val = (val << 8) | getbyte(ibuf, ++i, l);
1280 + if ((val & 0xff8000) == 0xff0000) { /* 512 <= len < 1024 */
1281 + len = 0x000200 | ((val >> 6) & 0x0001ff);
1283 + getbits(ibuf, 2 , &i ,&l);
1284 + } else if ((val & 0xffc000) == 0xff8000) {/* 1024 <= len < 2048 */
1285 + len = 0x000400 | ((val >> 4) & 0x0003ff);
1287 + getbits(ibuf, 4 , &i ,&l);
1288 + } else if ((val & 0xffe000) == 0xffc000) {/* 2048 <= len < 4096 */
1289 + len = 0x000800 | ((val >> 2) & 0x0007ff);
1291 + getbits(ibuf, 6 , &i ,&l);
1292 + } else if ((val & 0xfff000) == 0xffe000) {/* 4096 <= len < 8192 */
1293 + len = 0x001000 | (val & 0x000fff);
1296 + } else { /* this shouldn't happen */
1298 + printk(KERN_DEBUG "%s%d: wrong length code: 0x%X\n",
1299 + __FUNCTION__, state->unit, val);
1300 + return DECOMP_ERROR;
1304 + s = state->hist + state->histptr;
1305 + state->histptr += len;
1307 + if (state->histptr < 2*MPPE_HIST_LEN) {
1308 + /* copy uncompressed bytes to the history */
1310 + /* In some cases len may be greater than off. It means that memory
1311 + * areas pointed by s and s-off overlap. I had used memmove() here
1312 + * because I thought that it acts as libc's version. Unfortunately,
1313 + * I was wrong. :-) I got strange errors sometimes. Wilfried suggested
1314 + * using of byte by byte copying here and strange errors disappeared.
1316 + lamecopy(s, s - off, len);
1318 + /* buffer overflow; drop packet */
1320 + printk(KERN_ERR "%s%d: trying to write outside history "
1321 + "buffer\n", __FUNCTION__, state->unit);
1322 + return DECOMP_ERROR;
1326 + /* Do PFC decompression */
1328 + if ((history[0] & 0x01) != 0) {
1334 + if (len <= osize) {
1335 + /* copy uncompressed packet to the output buffer */
1336 + memcpy(obuf, history, olen);
1338 + /* buffer overflow; drop packet */
1340 + printk(KERN_ERR "%s%d: too big uncompressed packet: %d\n",
1341 + __FUNCTION__, state->unit, len + (PPP_HDRLEN / 2));
1342 + return DECOMP_ERROR;
1349 +mppe_decompress(void *arg, unsigned char *ibuf, int isize,
1350 + unsigned char *obuf, int osize)
1352 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
1353 + int seq, bits, uncomplen;
1355 + if (isize <= PPP_HDRLEN + MPPE_OVHD) {
1356 + if (state->debug) {
1357 + printk(KERN_DEBUG "%s%d: short packet (len=%d)\n", __FUNCTION__,
1358 + state->unit, isize);
1360 + return DECOMP_ERROR;
1363 + /* Get coherency counter and control bits from input buffer */
1364 + seq = MPPE_CCOUNT(ibuf);
1365 + bits = MPPE_BITS(ibuf);
1367 + if (state->stateless) {
1368 + /* RFC 3078, sec 8.1. */
1369 + mppe_increase_ccount(state);
1370 + if ((seq != state->ccount) && state->debug)
1371 + printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1372 + __FUNCTION__, state->unit, seq, state->ccount);
1373 + while (seq != state->ccount)
1374 + mppe_increase_ccount(state);
1376 + /* RFC 3078, sec 8.2. */
1377 + if (state->flushexpected) { /* discard state */
1378 + if ((bits & MPPE_BIT_FLUSHED)) { /* we received expected FLUSH bit */
1379 + while (seq != state->ccount)
1380 + mppe_increase_ccount(state);
1381 + state->flushexpected = 0;
1382 + } else /* drop packet*/
1383 + return DECOMP_ERROR;
1384 + } else { /* normal state */
1385 + mppe_increase_ccount(state);
1386 + if (seq != state->ccount) {
1387 + /* Packet loss detected, enter the discard state. */
1389 + printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1390 + __FUNCTION__, state->unit, seq, state->ccount);
1391 + state->flushexpected = 1;
1392 + return DECOMP_ERROR;
1395 + if (state->mppe && (bits & MPPE_BIT_FLUSHED)) {
1396 + arc4_setkey(state, state->session_key, state->keylen);
1400 + if (state->mppc && (bits & (MPPE_BIT_FLUSHED | MPPE_BIT_RESET))) {
1401 + state->histptr = MPPE_HIST_LEN;
1402 + if ((bits & MPPE_BIT_FLUSHED)) {
1403 + memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
1405 + if ((bits & MPPE_BIT_RESET)) {
1406 + memcpy(state->hist, state->hist + MPPE_HIST_LEN, MPPE_HIST_LEN);
1410 + /* Fill in the first part of the PPP header. The protocol field
1411 + comes from the decompressed data. */
1412 + obuf[0] = PPP_ADDRESS(ibuf);
1413 + obuf[1] = PPP_CONTROL(ibuf);
1414 + obuf += PPP_HDRLEN / 2;
1416 + if (state->mppe) { /* process encrypted packet */
1417 + if ((bits & MPPE_BIT_ENCRYPTED)) {
1418 + /* OK, packet encrypted, so decrypt it */
1419 + if (state->mppc && (bits & MPPE_BIT_COMP)) {
1420 + /* Hack warning !!! RC4 implementation which we use does
1421 + decryption "in place" - it means that input and output
1422 + buffers can be *the same* memory area. Therefore we don't
1423 + need to use a temporary buffer. But be careful - other
1424 + implementations don't have to be so nice. */
1425 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1426 + PPP_HDRLEN - (MPPE_OVHD / 2), ibuf + PPP_HDRLEN +
1428 + uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1429 + (MPPE_OVHD / 2), obuf, isize -
1430 + PPP_HDRLEN - (MPPE_OVHD / 2),
1431 + osize - (PPP_HDRLEN / 2));
1432 + if (uncomplen == DECOMP_ERROR) {
1433 + state->flushexpected = 1;
1434 + return DECOMP_ERROR;
1436 + uncomplen += PPP_HDRLEN / 2;
1437 + (state->stats).comp_bytes += isize;
1438 + (state->stats).comp_packets++;
1440 + uncomplen = isize - MPPE_OVHD;
1441 + /* Decrypt the first byte in order to check if it is
1442 + compressed or uncompressed protocol field */
1443 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), 1, obuf);
1444 + /* Do PFC decompression */
1445 + if ((obuf[0] & 0x01) != 0) {
1446 + obuf[1] = obuf[0];
1451 + /* And finally, decrypt the rest of the frame. */
1452 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2) + 1,
1453 + isize - PPP_HDRLEN - (MPPE_OVHD / 2) - 1, obuf + 1);
1454 + (state->stats).inc_bytes += isize;
1455 + (state->stats).inc_packets++;
1457 + } else { /* this shouldn't happen */
1459 + printk(KERN_ERR "%s%d: encryption negotiated but not an "
1460 + "encrypted packet received\n", __FUNCTION__, state->unit);
1461 + mppe_change_key(state, 0);
1462 + state->flushexpected = 1;
1463 + return DECOMP_ERROR;
1466 + if (state->mppc) { /* no MPPE, only MPPC */
1467 + if ((bits & MPPE_BIT_COMP)) {
1468 + uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1469 + (MPPE_OVHD / 2), obuf, isize -
1470 + PPP_HDRLEN - (MPPE_OVHD / 2),
1471 + osize - (PPP_HDRLEN / 2));
1472 + if (uncomplen == DECOMP_ERROR) {
1473 + state->flushexpected = 1;
1474 + return DECOMP_ERROR;
1476 + uncomplen += PPP_HDRLEN / 2;
1477 + (state->stats).comp_bytes += isize;
1478 + (state->stats).comp_packets++;
1480 + memcpy(obuf, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1481 + PPP_HDRLEN - (MPPE_OVHD / 2));
1482 + uncomplen = isize - MPPE_OVHD;
1483 + (state->stats).inc_bytes += isize;
1484 + (state->stats).inc_packets++;
1486 + } else { /* this shouldn't happen */
1488 + printk(KERN_ERR "%s%d: error - not an MPPC or MPPE frame "
1489 + "received\n", __FUNCTION__, state->unit);
1490 + state->flushexpected = 1;
1491 + return DECOMP_ERROR;
1495 + (state->stats).unc_bytes += uncomplen;
1496 + (state->stats).unc_packets++;
1502 +/************************************************************
1503 + * Module interface table
1504 + ************************************************************/
1506 +/* These are in ppp_generic.c */
1507 +extern int ppp_register_compressor (struct compressor *cp);
1508 +extern void ppp_unregister_compressor (struct compressor *cp);
1511 + * Functions exported to ppp_generic.c.
1513 + * In case of MPPC/MPPE there is no need to process incompressible data
1514 + * because such a data is sent in MPPC/MPPE frame. Therefore the (*incomp)
1515 + * callback function isn't needed.
1517 +struct compressor ppp_mppe = {
1518 + CI_MPPE, /* compress_proto */
1519 + mppe_comp_alloc, /* comp_alloc */
1520 + mppe_comp_free, /* comp_free */
1521 + mppe_comp_init, /* comp_init */
1522 + mppe_comp_reset, /* comp_reset */
1523 + mppe_compress, /* compress */
1524 + mppe_stats, /* comp_stat */
1525 + mppe_decomp_alloc, /* decomp_alloc */
1526 + mppe_comp_free, /* decomp_free */
1527 + mppe_decomp_init, /* decomp_init */
1528 + mppe_decomp_reset, /* decomp_reset */
1529 + mppe_decompress, /* decompress */
1530 + NULL, /* incomp */
1531 + mppe_stats, /* decomp_stat */
1534 +/************************************************************
1535 + * Module support routines
1536 + ************************************************************/
1538 +int __init mppe_module_init(void)
1542 + /* When the module is compiled into kernel, it is initialized before
1543 + * CryptoAPI. Therefore the code below reports that there is no crypto
1544 + * stuff in the kernel. Marlon de Boer who has pointed out the problem,
1545 + * has sent also a message to CryptoAPI developers, so maybe in the future
1546 + * the code will be uncommented.
1548 + if (!(crypto_alg_available("arc4", 0) && crypto_alg_available("sha1", 0))) {
1549 + printk(KERN_ERR "Kernel doesn't provide ARC4 and/or SHA1 algorithms "
1550 + "required by MPPE/MPPC. Check CryptoAPI configuration.\n");
1555 + /* Allocate space for SHAPad1, SHAPad2 and ... */
1556 + sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
1557 + if (sha_pad == NULL)
1559 + /* ... initialize them */
1560 + memset(sha_pad->sha_pad1, 0x00, sizeof(sha_pad->sha_pad1));
1561 + memset(sha_pad->sha_pad2, 0xf2, sizeof(sha_pad->sha_pad2));
1563 + answer = ppp_register_compressor(&ppp_mppe);
1564 + if (answer == 0) {
1565 + printk(KERN_INFO "MPPE/MPPC encryption/compression module registered\n");
1570 +void __exit mppe_module_cleanup(void)
1573 + ppp_unregister_compressor(&ppp_mppe);
1574 + printk(KERN_INFO "MPPE/MPPC encryption/compression module unregistered\n");
1577 +module_init(mppe_module_init);
1578 +module_exit(mppe_module_cleanup);
1580 +MODULE_AUTHOR("Jan Dubiec <jdx@slackware.pl>");
1581 +MODULE_DESCRIPTION("MPPE/MPPC encryption/compression module for Linux");
1582 +MODULE_LICENSE("Dual BSD/GPL");
1583 Index: linux-2.4.35.4/include/linux/ppp-comp.h
1584 ===================================================================
1585 --- linux-2.4.35.4.orig/include/linux/ppp-comp.h
1586 +++ linux-2.4.35.4/include/linux/ppp-comp.h
1591 - * ==FILEVERSION 980319==
1592 + * ==FILEVERSION 20040509==
1594 * NOTE TO MAINTAINERS:
1595 * If you modify this file at all, please set the above date.
1596 @@ -78,7 +78,7 @@ struct compressor {
1598 /* Compress a packet */
1599 int (*compress) (void *state, unsigned char *rptr,
1600 - unsigned char *obuf, int isize, int osize);
1601 + unsigned char *obuf, int isize, int osize);
1603 /* Return compression statistics */
1604 void (*comp_stat) (void *state, struct compstat *stats);
1605 @@ -99,7 +99,7 @@ struct compressor {
1607 /* Decompress a packet. */
1608 int (*decompress) (void *state, unsigned char *ibuf, int isize,
1609 - unsigned char *obuf, int osize);
1610 + unsigned char *obuf, int osize);
1612 /* Update state for an incompressible packet received */
1613 void (*incomp) (void *state, unsigned char *ibuf, int icnt);
1614 @@ -187,6 +187,42 @@ struct compressor {
1615 #define DEFLATE_CHK_SEQUENCE 0
1618 + * Definitions for MPPE/MPPC.
1621 +#define CI_MPPE 18 /* config option for MPPE */
1622 +#define CILEN_MPPE 6 /* length of config option */
1624 +#define MPPE_OVHD 4 /* MPPE overhead */
1625 +#define MPPE_MAX_KEY_LEN 16 /* largest key length (128-bit) */
1627 +#define MPPE_STATELESS 0x01 /* configuration bit H */
1628 +#define MPPE_40BIT 0x20 /* configuration bit L */
1629 +#define MPPE_56BIT 0x80 /* configuration bit M */
1630 +#define MPPE_128BIT 0x40 /* configuration bit S */
1631 +#define MPPE_MPPC 0x01 /* configuration bit C */
1634 + * Definitions for Stac LZS.
1637 +#define CI_LZS 17 /* config option for Stac LZS */
1638 +#define CILEN_LZS 5 /* length of config option */
1640 +#define LZS_OVHD 4 /* max. LZS overhead */
1641 +#define LZS_HIST_LEN 2048 /* LZS history size */
1642 +#define LZS_MAX_CCOUNT 0x0FFF /* max. coherency counter value */
1644 +#define LZS_MODE_NONE 0
1645 +#define LZS_MODE_LCB 1
1646 +#define LZS_MODE_CRC 2
1647 +#define LZS_MODE_SEQ 3
1648 +#define LZS_MODE_EXT 4
1650 +#define LZS_EXT_BIT_FLUSHED 0x80 /* bit A */
1651 +#define LZS_EXT_BIT_COMP 0x20 /* bit C */
1654 * Definitions for other, as yet unsupported, compression methods.