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 diff -ruN linux-2.4.29.orig/Documentation/Configure.help linux-2.4.29/Documentation/Configure.help
8 --- linux-2.4.29.orig/Documentation/Configure.help 2005-02-02 22:18:18.000000000 +0100
9 +++ linux-2.4.29/Documentation/Configure.help 2005-02-02 22:26:37.000000000 +0100
10 @@ -9969,6 +9969,28 @@
11 module; it is called bsd_comp.o and will show up in the directory
12 modules once you have said "make modules". If unsure, say N.
14 +Microsoft PPP compression/encryption (MPPC/MPPE)
16 + Support for the Microsoft Point-To-Point Compression (RFC2118) and
17 + Microsoft Point-To-Point Encryption (RFC3078). These protocols are
18 + supported by Microsoft Windows and wide range of "hardware" access
19 + servers. MPPE is common protocol in Virtual Private Networks. According
20 + to RFC3078, MPPE supports 40, 56 and 128-bit key lengths. Depending on
21 + PPP daemon configuration on both ends of the link, following scenarios
23 + - only compression (MPPC) is used,
24 + - only encryption (MPPE) is used,
25 + - compression and encryption (MPPC+MPPE) are used.
27 + Please note that Hi/Fn (http://www.hifn.com) holds patent on MPPC so
28 + you should check if this patent is valid in your country in order to
29 + avoid legal problems.
31 + For more information please visit http://free.polbox.pl/h/hs001
33 + To compile this driver as a module, choose M here. The module will
34 + be called ppp_mppe_mppc.o.
38 Support for PPP over Ethernet.
39 diff -ruN linux-2.4.29.orig/crypto/Config.in linux-2.4.29/crypto/Config.in
40 --- linux-2.4.29.orig/crypto/Config.in 2005-02-02 22:18:14.000000000 +0100
41 +++ linux-2.4.29/crypto/Config.in 2005-02-02 22:26:37.000000000 +0100
43 "$CONFIG_INET6_AH" = "y" -o \
44 "$CONFIG_INET6_AH" = "m" -o \
45 "$CONFIG_INET6_ESP" = "y" -o \
46 - "$CONFIG_INET6_ESP" = "m" ]; then
47 + "$CONFIG_INET6_ESP" = "m" -o \
48 + "$CONFIG_PPP_MPPE_MPPC" = "y" -o \
49 + "$CONFIG_PPP_MPPE_MPPC" = "m" ]; then
50 define_bool CONFIG_CRYPTO y
52 bool 'Cryptographic API' CONFIG_CRYPTO
54 "$CONFIG_INET6_AH" = "y" -o \
55 "$CONFIG_INET6_AH" = "m" -o \
56 "$CONFIG_INET6_ESP" = "y" -o \
57 - "$CONFIG_INET6_ESP" = "m" ]; then
58 - define_bool CONFIG_CRYPTO_SHA1 y
60 - tristate ' SHA1 digest algorithm' CONFIG_CRYPTO_SHA1
61 + "$CONFIG_INET6_ESP" = "m" -o \
62 + "$CONFIG_PPP_MPPE_MPPC" = "y" -o \
63 + "$CONFIG_PPP_MPPE_MPPC" = "m" ]; then
64 + if [ "$CONFIG_INET_AH" = "y" -o \
65 + "$CONFIG_INET_ESP" = "y" -o \
66 + "$CONFIG_INET6_AH" = "y" -o \
67 + "$CONFIG_INET6_ESP" = "y" -o \
68 + "$CONFIG_PPP_MPPE_MPPC" = "y" ]; then
69 + define_tristate CONFIG_CRYPTO_SHA1 y
71 + if [ "$CONFIG_CRYPTO_SHA1" != "y" -a \
72 + "$CONFIG_CRYPTO_SHA1" != "m" ]; then
73 + define_tristate CONFIG_CRYPTO_SHA1 m
77 + tristate ' SHA1 digest algorithm' CONFIG_CRYPTO_SHA1
79 tristate ' SHA256 digest algorithm' CONFIG_CRYPTO_SHA256
80 tristate ' SHA384 and SHA512 digest algorithms' CONFIG_CRYPTO_SHA512
81 tristate ' Whirlpool digest algorithms' CONFIG_CRYPTO_WP512
83 tristate ' TEA and XTEA cipher algorithms' CONFIG_CRYPTO_TEA
84 tristate ' Khazad cipher algorithm' CONFIG_CRYPTO_KHAZAD
85 tristate ' Anubis cipher algorithm' CONFIG_CRYPTO_ANUBIS
87 + if [ "$CONFIG_PPP_MPPE_MPPC" = "y" -o \
88 + "$CONFIG_PPP_MPPE_MPPC" = "m" ]; then
89 + if [ "$CONFIG_PPP_MPPE_MPPC" = "y" ]; then
90 + define_tristate CONFIG_CRYPTO_ARC4 y
92 + if [ "$CONFIG_CRYPTO_ARC4" != "y" -a \
93 + "$CONFIG_CRYPTO_ARC4" != "m" ]; then
94 + define_tristate CONFIG_CRYPTO_ARC4 m
98 tristate ' ARC4 cipher algorithm' CONFIG_CRYPTO_ARC4
100 if [ "$CONFIG_INET_IPCOMP" = "y" -o \
101 "$CONFIG_INET_IPCOMP" = "m" -o \
102 "$CONFIG_INET6_IPCOMP" = "y" -o \
103 diff -ruN linux-2.4.29.orig/drivers/net/Config.in linux-2.4.29/drivers/net/Config.in
104 --- linux-2.4.29.orig/drivers/net/Config.in 2005-02-02 22:17:31.000000000 +0100
105 +++ linux-2.4.29/drivers/net/Config.in 2005-02-02 22:26:37.000000000 +0100
107 dep_tristate ' PPP support for sync tty ports' CONFIG_PPP_SYNC_TTY $CONFIG_PPP
108 dep_tristate ' PPP Deflate compression' CONFIG_PPP_DEFLATE $CONFIG_PPP
109 dep_tristate ' PPP BSD-Compress compression' CONFIG_PPP_BSDCOMP $CONFIG_PPP
110 + dep_tristate ' Microsoft PPP compression/encryption (MPPC/MPPE)' CONFIG_PPP_MPPE_MPPC $CONFIG_PPP
111 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
112 dep_tristate ' PPP over Ethernet (EXPERIMENTAL)' CONFIG_PPPOE $CONFIG_PPP
114 diff -ruN linux-2.4.29.orig/drivers/net/Makefile linux-2.4.29/drivers/net/Makefile
115 --- linux-2.4.29.orig/drivers/net/Makefile 2005-02-02 22:17:23.000000000 +0100
116 +++ linux-2.4.29/drivers/net/Makefile 2005-02-02 22:26:37.000000000 +0100
118 obj-$(CONFIG_PPP_SYNC_TTY) += ppp_synctty.o
119 obj-$(CONFIG_PPP_DEFLATE) += ppp_deflate.o
120 obj-$(CONFIG_PPP_BSDCOMP) += bsd_comp.o
121 +obj-$(CONFIG_PPP_MPPE_MPPC) += ppp_mppe_mppc.o
122 obj-$(CONFIG_PPPOE) += pppox.o pppoe.o
124 obj-$(CONFIG_SLIP) += slip.o
125 diff -ruN linux-2.4.29.orig/drivers/net/ppp_generic.c linux-2.4.29/drivers/net/ppp_generic.c
126 --- linux-2.4.29.orig/drivers/net/ppp_generic.c 2005-02-02 22:17:24.000000000 +0100
127 +++ linux-2.4.29/drivers/net/ppp_generic.c 2005-02-02 22:26:37.000000000 +0100
129 * PPP driver, written by Michael Callahan and Al Longyear, and
130 * subsequently hacked by Paul Mackerras.
132 - * ==FILEVERSION 20020217==
133 + * ==FILEVERSION 20040509==
136 #include <linux/config.h>
138 spinlock_t rlock; /* lock for receive side 58 */
139 spinlock_t wlock; /* lock for transmit side 5c */
140 int mru; /* max receive unit 60 */
141 + int mru_alloc; /* MAX(1500,MRU) for dev_alloc_skb() */
142 unsigned int flags; /* control bits 64 */
143 unsigned int xstate; /* transmit state bits 68 */
144 unsigned int rstate; /* receive state bits 6c */
147 if (get_user(val, (int *) arg))
150 + ppp->mru_alloc = ppp->mru = val;
151 + if (ppp->mru_alloc < PPP_MRU)
152 + ppp->mru_alloc = PPP_MRU; /* increase for broken peers */
156 @@ -1025,14 +1028,37 @@
158 /* peek at outbound CCP frames */
159 ppp_ccp_peek(ppp, skb, 0);
161 + * When LZS or MPPE/MPPC has been negotiated we don't send
162 + * CCP_RESETACK after receiving CCP_RESETREQ; in fact pppd
163 + * sends such a packet but we silently discard it here
165 + if (CCP_CODE(skb->data+2) == CCP_RESETACK
166 + && (ppp->xcomp->compress_proto == CI_MPPE
167 + || ppp->xcomp->compress_proto == CI_LZS)) {
168 + --ppp->stats.tx_packets;
169 + ppp->stats.tx_bytes -= skb->len - 2;
176 /* try to do packet compression */
177 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
178 && proto != PPP_LCP && proto != PPP_CCP) {
179 - new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
183 + * because of possible data expansion when MPPC or LZS
184 + * is used, allocate compressor's buffer 12.5% bigger
187 + if (ppp->xcomp->compress_proto == CI_MPPE)
188 + comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + MPPE_OVHD;
189 + else if (ppp->xcomp->compress_proto == CI_LZS)
190 + comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + LZS_OVHD;
191 + new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len
192 + + comp_ovhd, GFP_ATOMIC);
194 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
196 @@ -1050,9 +1076,21 @@
199 skb_pull(skb, 2); /* pull off A/C bytes */
201 + } else if (len == 0) {
202 /* didn't compress, or CCP not up yet */
207 + * MPPE requires that we do not send unencrypted
208 + * frames. The compressor will return -1 if we
209 + * should drop the frame. We cannot simply test
210 + * the compress_proto because MPPE and MPPC share
213 + printk(KERN_ERR "ppp: compressor dropped pkt\n");
214 + kfree_skb(new_skb);
219 @@ -1540,14 +1578,15 @@
222 if (proto == PPP_COMP) {
223 - ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
224 + ns = dev_alloc_skb(ppp->mru_alloc + PPP_HDRLEN);
226 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
229 /* the decompressor still expects the A/C bytes in the hdr */
230 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
231 - skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
232 + skb->len + 2, ns->data,
233 + ppp->mru_alloc + PPP_HDRLEN);
235 /* Pass the compressed frame to pppd as an
237 @@ -1573,7 +1612,14 @@
241 - ppp->rstate |= SC_DC_ERROR;
242 + if (ppp->rcomp->compress_proto != CI_MPPE
243 + && ppp->rcomp->compress_proto != CI_LZS) {
245 + * If decompression protocol isn't MPPE/MPPC or LZS, we set
246 + * SC_DC_ERROR flag and wait for CCP_RESETACK
248 + ppp->rstate |= SC_DC_ERROR;
250 ppp_receive_error(ppp);
253 @@ -2253,6 +2299,7 @@
254 /* Initialize the new ppp unit */
255 ppp->file.index = unit;
257 + ppp->mru_alloc = PPP_MRU;
258 init_ppp_file(&ppp->file, INTERFACE);
259 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
260 for (i = 0; i < NUM_NP; ++i)
261 diff -ruN linux-2.4.29.orig/drivers/net/ppp_mppe_mppc.c linux-2.4.29/drivers/net/ppp_mppe_mppc.c
262 --- linux-2.4.29.orig/drivers/net/ppp_mppe_mppc.c 1970-01-01 01:00:00.000000000 +0100
263 +++ linux-2.4.29/drivers/net/ppp_mppe_mppc.c 2005-02-02 23:07:40.000000000 +0100
266 + * ppp_mppe_mppc.c - MPPC/MPPE "compressor/decompressor" module.
268 + * Copyright (c) 1994 Árpád Magosányi <mag@bunuel.tii.matav.hu>
269 + * Copyright (c) 1999 Tim Hockin, Cobalt Networks Inc. <thockin@cobaltnet.com>
270 + * Copyright (c) 2002-2005 Jan Dubiec <jdx@slackware.pl>
272 + * Permission to use, copy, modify, and distribute this software and its
273 + * documentation is hereby granted, provided that the above copyright
274 + * notice appears in all copies. This software is provided without any
275 + * warranty, express or implied.
277 + * The code is based on MPPE kernel module written by Árpád Magosányi and
278 + * Tim Hockin which can be found on http://planetmirror.com/pub/mppe/.
279 + * I have added MPPC and 56 bit session keys support in MPPE.
281 + * WARNING! Although this is open source code, its usage in some countries
282 + * (in particular in the USA) may violate Stac Inc. patent for MPPC.
284 + * ==FILEVERSION 20050202==
288 +#include <linux/init.h>
289 +#include <linux/module.h>
290 +#include <linux/mm.h>
291 +#include <linux/slab.h>
292 +#include <asm/scatterlist.h>
293 +#include <linux/vmalloc.h>
294 +#include <linux/crypto.h>
296 +#include <linux/ppp_defs.h>
297 +#include <linux/ppp-comp.h>
300 + * In 2.4.x kernels macro offset_in_page() is not defined in linux/mm.h so
301 + * we define it here; PAGE_MASK is defined in asm/page.h which is included
304 +#define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
307 + * State for a mppc/mppe "(de)compressor".
309 +struct ppp_mppe_state {
310 + struct crypto_tfm *arc4_tfm;
311 + struct crypto_tfm *sha1_tfm;
313 + u8 master_key[MPPE_MAX_KEY_LEN];
314 + u8 session_key[MPPE_MAX_KEY_LEN];
315 + u8 mppc; /* do we use compression (MPPC)? */
316 + u8 mppe; /* do we use encryption (MPPE)? */
317 + u8 keylen; /* key length in bytes */
318 + u8 bitkeylen; /* key length in bits */
319 + u16 ccount; /* coherency counter */
320 + u16 bits; /* MPPC/MPPE control bits */
321 + u8 stateless; /* do we use stateless mode? */
322 + u8 nextflushed; /* set A bit in the next outgoing packet;
323 + used only by compressor*/
324 + u8 flushexpected; /* drop packets until A bit is received;
325 + used only by decompressor*/
326 + u8 *hist; /* MPPC history */
327 + u16 *hash; /* Hash table; used only by compressor */
328 + u16 histptr; /* history "cursor" */
332 + struct compstat stats;
335 +#define MPPE_HIST_LEN 8192 /* MPPC history size */
336 +#define MPPE_MAX_CCOUNT 0x0FFF /* max. coherency counter value */
338 +#define MPPE_BIT_FLUSHED 0x80 /* bit A */
339 +#define MPPE_BIT_RESET 0x40 /* bit B */
340 +#define MPPE_BIT_COMP 0x20 /* bit C */
341 +#define MPPE_BIT_ENCRYPTED 0x10 /* bit D */
343 +#define MPPE_SALT0 0xD1 /* values used in MPPE key derivation */
344 +#define MPPE_SALT1 0x26 /* according to RFC3079 */
345 +#define MPPE_SALT2 0x9E
347 +#define MPPE_CCOUNT(x) ((((x)[4] & 0x0f) << 8) + (x)[5])
348 +#define MPPE_BITS(x) ((x)[4] & 0xf0)
349 +#define MPPE_CTRLHI(x) ((((x)->ccount & 0xf00)>>8)|((x)->bits))
350 +#define MPPE_CTRLLO(x) ((x)->ccount & 0xff)
353 + * Kernel Crypto API needs its arguments to be in kmalloc'd memory, not in the
354 + * module static data area. That means sha_pad needs to be kmalloc'd. It is done
355 + * in mppe_module_init(). This has been pointed out on 30th July 2004 by Oleg
356 + * Makarenko on pptpclient-devel mailing list.
358 +#define SHA1_PAD_SIZE 40
360 + unsigned char sha_pad1[SHA1_PAD_SIZE];
361 + unsigned char sha_pad2[SHA1_PAD_SIZE];
363 +static struct sha_pad *sha_pad;
366 +setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
368 + sg[0].page = virt_to_page(address);
369 + sg[0].offset = offset_in_page(address);
370 + sg[0].length = length;
374 +arc4_setkey(struct ppp_mppe_state *state, const unsigned char *key,
375 + const unsigned int keylen)
377 + crypto_cipher_setkey(state->arc4_tfm, key, keylen);
381 +arc4_encrypt(struct ppp_mppe_state *state, const unsigned char *in,
382 + const unsigned int len, unsigned char *out)
384 + struct scatterlist sgin[4], sgout[4];
386 + setup_sg(sgin, in, len);
387 + setup_sg(sgout, out, len);
388 + crypto_cipher_encrypt(state->arc4_tfm, sgout, sgin, len);
391 +#define arc4_decrypt arc4_encrypt
394 + * Key Derivation, from RFC 3078, RFC 3079.
395 + * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
398 +get_new_key_from_sha(struct ppp_mppe_state *state, unsigned char *interim_key)
400 + struct scatterlist sg[4];
402 + setup_sg(&sg[0], state->master_key, state->keylen);
403 + setup_sg(&sg[1], sha_pad->sha_pad1, sizeof(sha_pad->sha_pad1));
404 + setup_sg(&sg[2], state->session_key, state->keylen);
405 + setup_sg(&sg[3], sha_pad->sha_pad2, sizeof(sha_pad->sha_pad2));
407 + crypto_digest_digest (state->sha1_tfm, sg, 4, state->sha1_digest);
409 + memcpy(interim_key, state->sha1_digest, state->keylen);
413 +mppe_change_key(struct ppp_mppe_state *state, int initialize)
415 + unsigned char interim_key[MPPE_MAX_KEY_LEN];
417 + get_new_key_from_sha(state, interim_key);
419 + memcpy(state->session_key, interim_key, state->keylen);
421 + arc4_setkey(state, interim_key, state->keylen);
422 + arc4_encrypt(state, interim_key, state->keylen, state->session_key);
424 + if (state->keylen == 8) {
425 + if (state->bitkeylen == 40) {
426 + state->session_key[0] = MPPE_SALT0;
427 + state->session_key[1] = MPPE_SALT1;
428 + state->session_key[2] = MPPE_SALT2;
430 + state->session_key[0] = MPPE_SALT0;
433 + arc4_setkey(state, state->session_key, state->keylen);
436 +/* increase 12-bit coherency counter */
438 +mppe_increase_ccount(struct ppp_mppe_state *state)
440 + state->ccount = (state->ccount + 1) & MPPE_MAX_CCOUNT;
442 + if (state->stateless) {
443 + mppe_change_key(state, 0);
444 + state->nextflushed = 1;
446 + if ((state->ccount & 0xff) == 0xff) {
447 + mppe_change_key(state, 0);
453 +/* allocate space for a MPPE/MPPC (de)compressor. */
454 +/* comp != 0 -> init compressor */
455 +/* comp = 0 -> init decompressor */
457 +mppe_alloc(unsigned char *options, int opt_len, int comp)
459 + struct ppp_mppe_state *state;
460 + unsigned int digestsize;
463 + fname = comp ? "mppe_comp_alloc" : "mppe_decomp_alloc";
466 + * Hack warning - additionally to the standard MPPC/MPPE configuration
467 + * options, pppd passes to the (de)copressor 8 or 16 byte session key.
468 + * Therefore options[1] contains MPPC/MPPE configuration option length
469 + * (CILEN_MPPE = 6), but the real options length, depending on the key
470 + * length, is 6+8 or 6+16.
472 + if (opt_len < CILEN_MPPE) {
473 + printk(KERN_WARNING "%s: wrong options length: %u\n", fname, opt_len);
477 + if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
478 + (options[2] & ~MPPE_STATELESS) != 0 ||
479 + options[3] != 0 || options[4] != 0 ||
480 + (options[5] & ~(MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
481 + (options[5] & (MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
482 + printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
483 + "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname, options[0],
484 + options[1], options[2], options[3], options[4], options[5]);
488 + state = (struct ppp_mppe_state *)kmalloc(sizeof(*state), GFP_KERNEL);
489 + if (state == NULL) {
490 + printk(KERN_ERR "%s: cannot allocate space for %scompressor\n", fname,
494 + memset(state, 0, sizeof(struct ppp_mppe_state));
496 + state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */
497 + state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
498 + MPPE_40BIT); /* Do we use MPPE? */
501 + /* allocate MPPC history */
502 + state->hist = (u8*)vmalloc(2*MPPE_HIST_LEN*sizeof(u8));
503 + if (state->hist == NULL) {
505 + printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
510 + /* allocate hashtable for MPPC compressor */
512 + state->hash = (u16*)vmalloc(MPPE_HIST_LEN*sizeof(u16));
513 + if (state->hash == NULL) {
514 + vfree(state->hist);
516 + printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
523 + if (state->mppe) { /* specific for MPPE */
524 + /* Load ARC4 algorithm */
525 + state->arc4_tfm = crypto_alloc_tfm("arc4", 0);
526 + if (state->arc4_tfm == NULL) {
528 + vfree(state->hash);
530 + vfree(state->hist);
533 + printk(KERN_ERR "%s: cannot load ARC4 module\n", fname);
537 + /* Load SHA1 algorithm */
538 + state->sha1_tfm = crypto_alloc_tfm("sha1", 0);
539 + if (state->sha1_tfm == NULL) {
540 + crypto_free_tfm(state->arc4_tfm);
542 + vfree(state->hash);
544 + vfree(state->hist);
547 + printk(KERN_ERR "%s: cannot load SHA1 module\n", fname);
551 + digestsize = crypto_tfm_alg_digestsize(state->sha1_tfm);
552 + if (digestsize < MPPE_MAX_KEY_LEN) {
553 + crypto_free_tfm(state->sha1_tfm);
554 + crypto_free_tfm(state->arc4_tfm);
556 + vfree(state->hash);
558 + vfree(state->hist);
561 + printk(KERN_ERR "%s: CryptoAPI SHA1 digest size too small\n", fname);
564 + state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
565 + if (!state->sha1_digest) {
566 + crypto_free_tfm(state->sha1_tfm);
567 + crypto_free_tfm(state->arc4_tfm);
569 + vfree(state->hash);
571 + vfree(state->hist);
574 + printk(KERN_ERR "%s: cannot allocate space for SHA1 digest\n", fname);
577 + memcpy(state->master_key, options+CILEN_MPPE, MPPE_MAX_KEY_LEN);
578 + memcpy(state->session_key, state->master_key, MPPE_MAX_KEY_LEN);
579 + /* initial key generation is done in mppe_init() */
584 + return (void *) state;
588 +mppe_comp_alloc(unsigned char *options, int opt_len)
590 + return mppe_alloc(options, opt_len, 1);
594 +mppe_decomp_alloc(unsigned char *options, int opt_len)
596 + return mppe_alloc(options, opt_len, 0);
599 +/* cleanup the (de)compressor */
601 +mppe_comp_free(void *arg)
603 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
605 + if (state != NULL) {
607 + if (state->sha1_tfm != NULL)
608 + crypto_free_tfm(state->sha1_tfm);
609 + if (state->arc4_tfm != NULL)
610 + crypto_free_tfm(state->arc4_tfm);
612 + if (state->hist != NULL)
613 + vfree(state->hist);
614 + if (state->hash != NULL)
615 + vfree(state->hash);
622 +/* init MPPC/MPPE (de)compresor */
623 +/* comp != 0 -> init compressor */
624 +/* comp = 0 -> init decompressor */
626 +mppe_init(void *arg, unsigned char *options, int opt_len, int unit,
627 + int hdrlen, int mru, int debug, int comp)
629 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
632 + fname = comp ? "mppe_comp_init" : "mppe_decomp_init";
634 + if (opt_len < CILEN_MPPE) {
636 + printk(KERN_WARNING "%s: wrong options length: %u\n",
641 + if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
642 + (options[2] & ~MPPE_STATELESS) != 0 ||
643 + options[3] != 0 || options[4] != 0 ||
644 + (options[5] & ~(MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
645 + (options[5] & (MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
647 + printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
648 + "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname,
649 + options[0], options[1], options[2], options[3], options[4],
654 + if ((options[5] & ~MPPE_MPPC) != MPPE_128BIT &&
655 + (options[5] & ~MPPE_MPPC) != MPPE_56BIT &&
656 + (options[5] & ~MPPE_MPPC) != MPPE_40BIT &&
657 + (options[5] & MPPE_MPPC) != MPPE_MPPC) {
659 + printk(KERN_WARNING "%s: don't know what to do: o[5]=%02x\n",
660 + fname, options[5]);
664 + state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */
665 + state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
666 + MPPE_40BIT); /* Do we use MPPE? */
667 + state->stateless = options[2] & MPPE_STATELESS; /* Do we use stateless mode? */
669 + switch (state->mppe) {
670 + case MPPE_40BIT: /* 40 bit key */
672 + state->bitkeylen = 40;
674 + case MPPE_56BIT: /* 56 bit key */
676 + state->bitkeylen = 56;
678 + case MPPE_128BIT: /* 128 bit key */
679 + state->keylen = 16;
680 + state->bitkeylen = 128;
684 + state->bitkeylen = 0;
687 + state->ccount = MPPE_MAX_CCOUNT;
689 + state->unit = unit;
690 + state->debug = debug;
691 + state->histptr = MPPE_HIST_LEN;
692 + if (state->mppc) { /* reset history if MPPC was negotiated */
693 + memset(state->hist, 0, 2*MPPE_HIST_LEN*sizeof(u8));
696 + if (state->mppe) { /* generate initial session keys */
697 + mppe_change_key(state, 1);
700 + if (comp) { /* specific for compressor */
701 + state->nextflushed = 1;
702 + } else { /* specific for decompressor */
704 + state->flushexpected = 1;
711 +mppe_comp_init(void *arg, unsigned char *options, int opt_len, int unit,
712 + int hdrlen, int debug)
714 + return mppe_init(arg, options, opt_len, unit, hdrlen, 0, debug, 1);
719 +mppe_decomp_init(void *arg, unsigned char *options, int opt_len, int unit,
720 + int hdrlen, int mru, int debug)
722 + return mppe_init(arg, options, opt_len, unit, hdrlen, mru, debug, 0);
726 +mppe_comp_reset(void *arg)
728 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
731 + printk(KERN_DEBUG "%s%d: resetting MPPC/MPPE compressor\n",
732 + __FUNCTION__, state->unit);
734 + state->nextflushed = 1;
736 + arc4_setkey(state, state->session_key, state->keylen);
740 +mppe_decomp_reset(void *arg)
742 + /* When MPPC/MPPE is in use, we shouldn't receive any CCP Reset-Ack.
743 + But when we receive such a packet, we just ignore it. */
748 +mppe_stats(void *arg, struct compstat *stats)
750 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
752 + *stats = state->stats;
755 +/***************************/
756 +/**** Compression stuff ****/
757 +/***************************/
758 +/* inserts 1 to 8 bits into the output buffer */
759 +static inline void putbits8(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
765 + *buf = *buf | (val & 0xff);
775 + *buf = *buf | ((val >> 8) & 0xff);
776 + *(++buf) = val & 0xff;
780 +/* inserts 9 to 16 bits into the output buffer */
781 +static inline void putbits16(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
788 + *buf = *buf | ((val >> 8) & 0xff);
789 + *(++buf) = val & 0xff;
797 + *l = 16 - n + (*l);
799 + *buf = *buf | ((val >> 16) & 0xff);
800 + *(++buf) = (val >> 8) & 0xff;
801 + *(++buf) = val & 0xff;
805 +/* inserts 17 to 24 bits into the output buffer */
806 +static inline void putbits24(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
809 + if (*l >= n - 16) {
811 + *l = 16 - n + (*l);
813 + *buf = *buf | ((val >> 16) & 0xff);
814 + *(++buf) = (val >> 8) & 0xff;
815 + *(++buf) = val & 0xff;
822 + (*i)++; (*i)++; (*i)++;
823 + *l = 24 - n + (*l);
825 + *buf = *buf | ((val >> 24) & 0xff);
826 + *(++buf) = (val >> 16) & 0xff;
827 + *(++buf) = (val >> 8) & 0xff;
828 + *(++buf) = val & 0xff;
833 +mppc_compress(struct ppp_mppe_state *state, unsigned char *ibuf,
834 + unsigned char *obuf, int isize, int osize)
836 + u32 olen, off, len, idx, i, l;
837 + u8 *hist, *sbuf, *p, *q, *r, *s;
840 + At this point, to avoid possible buffer overflow caused by packet
841 + expansion during/after compression, we should make sure that
842 + osize >= (((isize*9)/8)+1)+2, but we don't do that because in
843 + ppp_generic.c we simply allocate bigger obuf.
845 + Maximum MPPC packet expansion is 12.5%. This is the worst case when
846 + all octets in the input buffer are >= 0x80 and we cannot find any
847 + repeated tokens. Additionally we have to reserve 2 bytes for MPPE/MPPC
848 + status bits and coherency counter.
851 + hist = state->hist + MPPE_HIST_LEN;
852 + /* check if there is enough room at the end of the history */
853 + if (state->histptr + isize >= 2*MPPE_HIST_LEN) {
854 + state->bits |= MPPE_BIT_RESET;
855 + state->histptr = MPPE_HIST_LEN;
856 + memcpy(state->hist, hist, MPPE_HIST_LEN);
858 + /* add packet to the history; isize must be <= MPPE_HIST_LEN */
859 + sbuf = state->hist + state->histptr;
860 + memcpy(sbuf, ibuf, isize);
861 + state->histptr += isize;
863 + /* compress data */
865 + *obuf = olen = i = 0;
867 + while (i < isize - 2) {
869 + idx = ((40543*((((s[0]<<4)^s[1])<<4)^s[2]))>>4) & 0x1fff;
870 + p = hist + state->hash[idx];
871 + state->hash[idx] = (u16) (s - hist);
873 + if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ || *p++ != *s++ ||
875 + /* no match found; encode literal byte */
876 + if (ibuf[i] < 0x80) { /* literal byte < 0x80 */
877 + putbits8(obuf, (u32) ibuf[i], 8, &olen, &l);
878 + } else { /* literal byte >= 0x80 */
879 + putbits16(obuf, (u32) (0x100|(ibuf[i]&0x7f)), 9, &olen, &l);
885 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
886 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
887 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
888 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
889 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
890 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
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++ ||
903 + while((*p++ == *s++) && (s < r) && (p < q));
906 + while((*p++ == *s++) && (s < r) && (p < q));
911 + /* at least 3 character match found; code data */
912 + /* encode offset */
913 + if (off < 64) { /* 10-bit offset; 0 <= offset < 64 */
914 + putbits16(obuf, 0x3c0|off, 10, &olen, &l);
915 + } else if (off < 320) { /* 12-bit offset; 64 <= offset < 320 */
916 + putbits16(obuf, 0xe00|(off-64), 12, &olen, &l);
917 + } else if (off < 8192) { /* 16-bit offset; 320 <= offset < 8192 */
918 + putbits16(obuf, 0xc000|(off-320), 16, &olen, &l);
920 + /* This shouldn't happen; we return 0 what means "packet expands",
921 + and we send packet uncompressed. */
923 + printk(KERN_DEBUG "%s%d: wrong offset value: %d\n",
924 + __FUNCTION__, state->unit, off);
927 + /* encode length of match */
928 + if (len < 4) { /* length = 3 */
929 + putbits8(obuf, 0, 1, &olen, &l);
930 + } else if (len < 8) { /* 4 <= length < 8 */
931 + putbits8(obuf, 0x08|(len&0x03), 4, &olen, &l);
932 + } else if (len < 16) { /* 8 <= length < 16 */
933 + putbits8(obuf, 0x30|(len&0x07), 6, &olen, &l);
934 + } else if (len < 32) { /* 16 <= length < 32 */
935 + putbits8(obuf, 0xe0|(len&0x0f), 8, &olen, &l);
936 + } else if (len < 64) { /* 32 <= length < 64 */
937 + putbits16(obuf, 0x3c0|(len&0x1f), 10, &olen, &l);
938 + } else if (len < 128) { /* 64 <= length < 128 */
939 + putbits16(obuf, 0xf80|(len&0x3f), 12, &olen, &l);
940 + } else if (len < 256) { /* 128 <= length < 256 */
941 + putbits16(obuf, 0x3f00|(len&0x7f), 14, &olen, &l);
942 + } else if (len < 512) { /* 256 <= length < 512 */
943 + putbits16(obuf, 0xfe00|(len&0xff), 16, &olen, &l);
944 + } else if (len < 1024) { /* 512 <= length < 1024 */
945 + putbits24(obuf, 0x3fc00|(len&0x1ff), 18, &olen, &l);
946 + } else if (len < 2048) { /* 1024 <= length < 2048 */
947 + putbits24(obuf, 0xff800|(len&0x3ff), 20, &olen, &l);
948 + } else if (len < 4096) { /* 2048 <= length < 4096 */
949 + putbits24(obuf, 0x3ff000|(len&0x7ff), 22, &olen, &l);
950 + } else if (len < 8192) { /* 4096 <= length < 8192 */
951 + putbits24(obuf, 0xffe000|(len&0xfff), 24, &olen, &l);
953 + /* This shouldn't happen; we return 0 what means "packet expands",
954 + and send packet uncompressed. */
956 + printk(KERN_DEBUG "%s%d: wrong length of match value: %d\n",
957 + __FUNCTION__, state->unit, len);
962 + /* Add remaining octets to the output */
963 + while(isize - i > 0) {
964 + if (ibuf[i] < 0x80) { /* literal byte < 0x80 */
965 + putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l);
966 + } else { /* literal byte >= 0x80 */
967 + putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l);
970 + /* Reset unused bits of the last output octet */
971 + if ((l != 0) && (l != 8)) {
972 + putbits8(obuf, 0, l, &olen, &l);
979 +mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
980 + int isize, int osize)
982 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
983 + int proto, olen, complen, off;
984 + unsigned char *wptr;
986 + /* Check that the protocol is in the range we handle. */
987 + proto = PPP_PROTOCOL(ibuf);
988 + if (proto < 0x0021 || proto > 0x00fa)
992 + /* Copy over the PPP header */
993 + wptr[0] = PPP_ADDRESS(ibuf);
994 + wptr[1] = PPP_CONTROL(ibuf);
995 + wptr[2] = PPP_COMP >> 8;
996 + wptr[3] = PPP_COMP;
997 + wptr += PPP_HDRLEN + (MPPE_OVHD / 2); /* Leave two octets for MPPE/MPPC bits */
1000 + * In ver. 0.99 protocol field was compressed. Deflate and BSD compress
1001 + * do PFC before actual compression, RCF2118 and RFC3078 are not precise
1002 + * on this topic so I decided to do PFC. Unfortunately this change caused
1003 + * incompatibility with older/other MPPE/MPPC modules. I have received
1004 + * a lot of complaints from unexperienced users so I have decided to revert
1005 + * to previous state, i.e. the protocol field is sent uncompressed now.
1006 + * Although this may be changed in the future.
1008 + * Receiving side (mppe_decompress()) still accepts packets with compressed
1009 + * and uncompressed protocol field so you shouldn't get "Unsupported protocol
1010 + * 0x2145 received" messages anymore.
1012 + //off = (proto > 0xff) ? 2 : 3; /* PFC - skip first protocol byte if 0 */
1017 + mppe_increase_ccount(state);
1019 + if (state->nextflushed) {
1020 + state->bits |= MPPE_BIT_FLUSHED;
1021 + state->nextflushed = 0;
1022 + if (state->mppe && !state->stateless) {
1024 + * If this is the flag packet, the key has been already changed in
1025 + * mppe_increase_ccount() so we dont't do it once again.
1027 + if ((state->ccount & 0xff) != 0xff) {
1028 + arc4_setkey(state, state->session_key, state->keylen);
1031 + if (state->mppc) { /* reset history */
1032 + state->bits |= MPPE_BIT_RESET;
1033 + state->histptr = MPPE_HIST_LEN;
1034 + memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
1038 + if (state->mppc && !state->mppe) { /* Do only compression */
1039 + complen = mppc_compress(state, ibuf, wptr, isize - off,
1040 + osize - PPP_HDRLEN - (MPPE_OVHD / 2));
1042 + * TODO: Implement an heuristics to handle packet expansion in a smart
1043 + * way. Now, when a packet expands, we send it as uncompressed and
1044 + * when next packet is sent we have to reset compressor's history.
1045 + * Maybe it would be better to send such packet as compressed in order
1046 + * to keep history's continuity.
1048 + if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
1050 + /* packet expands */
1051 + state->nextflushed = 1;
1052 + memcpy(wptr, ibuf, isize - off);
1053 + olen = isize - (off - 2) + MPPE_OVHD;
1054 + (state->stats).inc_bytes += olen;
1055 + (state->stats).inc_packets++;
1057 + state->bits |= MPPE_BIT_COMP;
1058 + olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
1059 + (state->stats).comp_bytes += olen;
1060 + (state->stats).comp_packets++;
1062 + } else { /* Do encryption with or without compression */
1063 + state->bits |= MPPE_BIT_ENCRYPTED;
1064 + if (!state->mppc && state->mppe) { /* Do only encryption */
1065 + /* read from ibuf, write to wptr, adjust for PPP_HDRLEN */
1066 + arc4_encrypt(state, ibuf, isize - off, wptr);
1067 + olen = isize - (off - 2) + MPPE_OVHD;
1068 + (state->stats).inc_bytes += olen;
1069 + (state->stats).inc_packets++;
1070 + } else { /* Do compression and then encryption - RFC3078 */
1071 + complen = mppc_compress(state, ibuf, wptr, isize - off,
1072 + osize - PPP_HDRLEN - (MPPE_OVHD / 2));
1074 + * TODO: Implement an heuristics to handle packet expansion in a smart
1075 + * way. Now, when a packet expands, we send it as uncompressed and
1076 + * when next packet is sent we have to reset compressor's history.
1077 + * Maybe it would be good to send such packet as compressed in order
1078 + * to keep history's continuity.
1080 + if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
1082 + /* packet expands */
1083 + state->nextflushed = 1;
1084 + arc4_encrypt(state, ibuf, isize - off, wptr);
1085 + olen = isize - (off - 2) + MPPE_OVHD;
1086 + (state->stats).inc_bytes += olen;
1087 + (state->stats).inc_packets++;
1089 + state->bits |= MPPE_BIT_COMP;
1090 + /* Hack warning !!! RC4 implementation which we use does
1091 + encryption "in place" - it means that input and output
1092 + buffers can be *the same* memory area. Therefore we don't
1093 + need to use a temporary buffer. But be careful - other
1094 + implementations don't have to be so nice.
1095 + I used to use ibuf as temporary buffer here, but it led
1096 + packet sniffers into error. Thanks to Wilfried Weissmann
1097 + for pointing that. */
1098 + arc4_encrypt(state, wptr, complen, wptr);
1099 + olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
1100 + (state->stats).comp_bytes += olen;
1101 + (state->stats).comp_packets++;
1106 + /* write status bits and coherency counter into the output buffer */
1107 + wptr = obuf + PPP_HDRLEN;
1108 + wptr[0] = MPPE_CTRLHI(state);
1109 + wptr[1] = MPPE_CTRLLO(state);
1113 + (state->stats).unc_bytes += isize;
1114 + (state->stats).unc_packets++;
1119 +/***************************/
1120 +/*** Decompression stuff ***/
1121 +/***************************/
1122 +static inline u32 getbits(const u8 *buf, const u32 n, u32 *i, u32 *l)
1124 + static const u32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1130 + res = (buf[*i] & m[ol]) >> (*l);
1136 + *l = 8 - n + (*l);
1137 + res = (buf[(*i)++] & m[ol]) << 8;
1138 + res = (res | buf[*i]) >> (*l);
1144 +static inline u32 getbyte(const u8 *buf, const u32 i, const u32 l)
1149 + return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff;
1153 +static inline void lamecopy(u8 *dst, u8 *src, u32 len)
1160 +mppc_decompress(struct ppp_mppe_state *state, unsigned char *ibuf,
1161 + unsigned char *obuf, int isize, int osize)
1163 + u32 olen, off, len, bits, val, sig, i, l;
1166 + history = state->hist + state->histptr;
1167 + olen = len = i = 0;
1170 + while (bits >= 8) {
1171 + val = getbyte(ibuf, i++, l);
1172 + if (val < 0x80) { /* literal byte < 0x80 */
1173 + if (state->histptr < 2*MPPE_HIST_LEN) {
1174 + /* copy uncompressed byte to the history */
1175 + (state->hist)[(state->histptr)++] = (u8) val;
1177 + /* buffer overflow; drop packet */
1179 + printk(KERN_ERR "%s%d: trying to write outside history "
1180 + "buffer\n", __FUNCTION__, state->unit);
1181 + return DECOMP_ERROR;
1189 + if (sig == 0x80) { /* literal byte >= 0x80 */
1190 + if (state->histptr < 2*MPPE_HIST_LEN) {
1191 + /* copy uncompressed byte to the history */
1192 + (state->hist)[(state->histptr)++] =
1193 + (u8) (0x80|((val&0x3f)<<1)|getbits(ibuf, 1 , &i ,&l));
1195 + /* buffer overflow; drop packet */
1197 + printk(KERN_ERR "%s%d: trying to write outside history "
1198 + "buffer\n", __FUNCTION__, state->unit);
1199 + return DECOMP_ERROR;
1206 + /* Not a literal byte so it must be an (offset,length) pair */
1207 + /* decode offset */
1209 + if (sig == 0xf0) { /* 10-bit offset; 0 <= offset < 64 */
1210 + off = (((val&0x0f)<<2)|getbits(ibuf, 2 , &i ,&l));
1213 + if (sig == 0xe0) { /* 12-bit offset; 64 <= offset < 320 */
1214 + off = ((((val&0x0f)<<4)|getbits(ibuf, 4 , &i ,&l))+64);
1217 + if ((sig&0xe0) == 0xc0) {/* 16-bit offset; 320 <= offset < 8192 */
1218 + off = ((((val&0x1f)<<8)|getbyte(ibuf, i++, l))+320);
1220 + if (off > MPPE_HIST_LEN - 1) {
1222 + printk(KERN_DEBUG "%s%d: too big offset value: %d\n",
1223 + __FUNCTION__, state->unit, off);
1224 + return DECOMP_ERROR;
1226 + } else { /* this shouldn't happen */
1228 + printk(KERN_DEBUG "%s%d: cannot decode offset value\n",
1229 + __FUNCTION__, state->unit);
1230 + return DECOMP_ERROR;
1234 + /* decode length of match */
1235 + val = getbyte(ibuf, i, l);
1236 + if ((val & 0x80) == 0x00) { /* len = 3 */
1239 + getbits(ibuf, 1 , &i ,&l);
1240 + } else if ((val & 0xc0) == 0x80) { /* 4 <= len < 8 */
1241 + len = 0x04 | ((val>>4) & 0x03);
1243 + getbits(ibuf, 4 , &i ,&l);
1244 + } else if ((val & 0xe0) == 0xc0) { /* 8 <= len < 16 */
1245 + len = 0x08 | ((val>>2) & 0x07);
1247 + getbits(ibuf, 6 , &i ,&l);
1248 + } else if ((val & 0xf0) == 0xe0) { /* 16 <= len < 32 */
1249 + len = 0x10 | (val & 0x0f);
1254 + val = (val << 8) | getbyte(ibuf, ++i, l);
1255 + if ((val & 0xf800) == 0xf000) { /* 32 <= len < 64 */
1256 + len = 0x0020 | ((val >> 6) & 0x001f);
1258 + getbits(ibuf, 2 , &i ,&l);
1259 + } else if ((val & 0xfc00) == 0xf800) { /* 64 <= len < 128 */
1260 + len = 0x0040 | ((val >> 4) & 0x003f);
1262 + getbits(ibuf, 4 , &i ,&l);
1263 + } else if ((val & 0xfe00) == 0xfc00) { /* 128 <= len < 256 */
1264 + len = 0x0080 | ((val >> 2) & 0x007f);
1266 + getbits(ibuf, 6 , &i ,&l);
1267 + } else if ((val & 0xff00) == 0xfe00) { /* 256 <= len < 512 */
1268 + len = 0x0100 | (val & 0x00ff);
1273 + val = (val << 8) | getbyte(ibuf, ++i, l);
1274 + if ((val & 0xff8000) == 0xff0000) { /* 512 <= len < 1024 */
1275 + len = 0x000200 | ((val >> 6) & 0x0001ff);
1277 + getbits(ibuf, 2 , &i ,&l);
1278 + } else if ((val & 0xffc000) == 0xff8000) {/* 1024 <= len < 2048 */
1279 + len = 0x000400 | ((val >> 4) & 0x0003ff);
1281 + getbits(ibuf, 4 , &i ,&l);
1282 + } else if ((val & 0xffe000) == 0xffc000) {/* 2048 <= len < 4096 */
1283 + len = 0x000800 | ((val >> 2) & 0x0007ff);
1285 + getbits(ibuf, 6 , &i ,&l);
1286 + } else if ((val & 0xfff000) == 0xffe000) {/* 4096 <= len < 8192 */
1287 + len = 0x001000 | (val & 0x000fff);
1290 + } else { /* this shouldn't happen */
1292 + printk(KERN_DEBUG "%s%d: wrong length code: 0x%X\n",
1293 + __FUNCTION__, state->unit, val);
1294 + return DECOMP_ERROR;
1298 + s = state->hist + state->histptr;
1299 + state->histptr += len;
1301 + if (state->histptr < 2*MPPE_HIST_LEN) {
1302 + /* copy uncompressed bytes to the history */
1304 + /* In some cases len may be greater than off. It means that memory
1305 + * areas pointed by s and s-off overlap. I had used memmove() here
1306 + * because I thought that it acts as libc's version. Unfortunately,
1307 + * I was wrong. :-) I got strange errors sometimes. Wilfried suggested
1308 + * using of byte by byte copying here and strange errors disappeared.
1310 + lamecopy(s, s - off, len);
1312 + /* buffer overflow; drop packet */
1314 + printk(KERN_ERR "%s%d: trying to write outside history "
1315 + "buffer\n", __FUNCTION__, state->unit);
1316 + return DECOMP_ERROR;
1320 + /* Do PFC decompression */
1322 + if ((history[0] & 0x01) != 0) {
1328 + if (len <= osize) {
1329 + /* copy uncompressed packet to the output buffer */
1330 + memcpy(obuf, history, olen);
1332 + /* buffer overflow; drop packet */
1334 + printk(KERN_ERR "%s%d: too big uncompressed packet: %d\n",
1335 + __FUNCTION__, state->unit, len + (PPP_HDRLEN / 2));
1336 + return DECOMP_ERROR;
1343 +mppe_decompress(void *arg, unsigned char *ibuf, int isize,
1344 + unsigned char *obuf, int osize)
1346 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
1347 + int seq, bits, uncomplen;
1349 + if (isize <= PPP_HDRLEN + MPPE_OVHD) {
1350 + if (state->debug) {
1351 + printk(KERN_DEBUG "%s%d: short packet (len=%d)\n", __FUNCTION__,
1352 + state->unit, isize);
1354 + return DECOMP_ERROR;
1357 + /* Get coherency counter and control bits from input buffer */
1358 + seq = MPPE_CCOUNT(ibuf);
1359 + bits = MPPE_BITS(ibuf);
1361 + if (state->stateless) {
1362 + /* RFC 3078, sec 8.1. */
1363 + mppe_increase_ccount(state);
1364 + if ((seq != state->ccount) && state->debug)
1365 + printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1366 + __FUNCTION__, state->unit, seq, state->ccount);
1367 + while (seq != state->ccount)
1368 + mppe_increase_ccount(state);
1370 + /* RFC 3078, sec 8.2. */
1371 + if (state->flushexpected) { /* discard state */
1372 + if ((bits & MPPE_BIT_FLUSHED)) { /* we received expected FLUSH bit */
1373 + while (seq != state->ccount)
1374 + mppe_increase_ccount(state);
1375 + state->flushexpected = 0;
1376 + } else /* drop packet*/
1377 + return DECOMP_ERROR;
1378 + } else { /* normal state */
1379 + mppe_increase_ccount(state);
1380 + if (seq != state->ccount) {
1381 + /* Packet loss detected, enter the discard state. */
1383 + printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1384 + __FUNCTION__, state->unit, seq, state->ccount);
1385 + state->flushexpected = 1;
1386 + return DECOMP_ERROR;
1389 + if (state->mppe && (bits & MPPE_BIT_FLUSHED)) {
1390 + arc4_setkey(state, state->session_key, state->keylen);
1394 + if (state->mppc && (bits & (MPPE_BIT_FLUSHED | MPPE_BIT_RESET))) {
1395 + state->histptr = MPPE_HIST_LEN;
1396 + if ((bits & MPPE_BIT_FLUSHED)) {
1397 + memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
1399 + if ((bits & MPPE_BIT_RESET)) {
1400 + memcpy(state->hist, state->hist + MPPE_HIST_LEN, MPPE_HIST_LEN);
1404 + /* Fill in the first part of the PPP header. The protocol field
1405 + comes from the decompressed data. */
1406 + obuf[0] = PPP_ADDRESS(ibuf);
1407 + obuf[1] = PPP_CONTROL(ibuf);
1408 + obuf += PPP_HDRLEN / 2;
1410 + if (state->mppe) { /* process encrypted packet */
1411 + if ((bits & MPPE_BIT_ENCRYPTED)) {
1412 + /* OK, packet encrypted, so decrypt it */
1413 + if (state->mppc && (bits & MPPE_BIT_COMP)) {
1414 + /* Hack warning !!! RC4 implementation which we use does
1415 + decryption "in place" - it means that input and output
1416 + buffers can be *the same* memory area. Therefore we don't
1417 + need to use a temporary buffer. But be careful - other
1418 + implementations don't have to be so nice. */
1419 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1420 + PPP_HDRLEN - (MPPE_OVHD / 2), ibuf + PPP_HDRLEN +
1422 + uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1423 + (MPPE_OVHD / 2), obuf, isize -
1424 + PPP_HDRLEN - (MPPE_OVHD / 2),
1425 + osize - (PPP_HDRLEN / 2));
1426 + if (uncomplen == DECOMP_ERROR) {
1427 + state->flushexpected = 1;
1428 + return DECOMP_ERROR;
1430 + uncomplen += PPP_HDRLEN / 2;
1431 + (state->stats).comp_bytes += isize;
1432 + (state->stats).comp_packets++;
1434 + uncomplen = isize - MPPE_OVHD;
1435 + /* Decrypt the first byte in order to check if it is
1436 + compressed or uncompressed protocol field */
1437 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), 1, obuf);
1438 + /* Do PFC decompression */
1439 + if ((obuf[0] & 0x01) != 0) {
1440 + obuf[1] = obuf[0];
1445 + /* And finally, decrypt the rest of the frame. */
1446 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2) + 1,
1447 + isize - PPP_HDRLEN - (MPPE_OVHD / 2) - 1, obuf + 1);
1448 + (state->stats).inc_bytes += isize;
1449 + (state->stats).inc_packets++;
1451 + } else { /* this shouldn't happen */
1453 + printk(KERN_ERR "%s%d: encryption negotiated but not an "
1454 + "encrypted packet received\n", __FUNCTION__, state->unit);
1455 + mppe_change_key(state, 0);
1456 + state->flushexpected = 1;
1457 + return DECOMP_ERROR;
1460 + if (state->mppc) { /* no MPPE, only MPPC */
1461 + if ((bits & MPPE_BIT_COMP)) {
1462 + uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1463 + (MPPE_OVHD / 2), obuf, isize -
1464 + PPP_HDRLEN - (MPPE_OVHD / 2),
1465 + osize - (PPP_HDRLEN / 2));
1466 + if (uncomplen == DECOMP_ERROR) {
1467 + state->flushexpected = 1;
1468 + return DECOMP_ERROR;
1470 + uncomplen += PPP_HDRLEN / 2;
1471 + (state->stats).comp_bytes += isize;
1472 + (state->stats).comp_packets++;
1474 + memcpy(obuf, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1475 + PPP_HDRLEN - (MPPE_OVHD / 2));
1476 + uncomplen = isize - MPPE_OVHD;
1477 + (state->stats).inc_bytes += isize;
1478 + (state->stats).inc_packets++;
1480 + } else { /* this shouldn't happen */
1482 + printk(KERN_ERR "%s%d: error - not an MPPC or MPPE frame "
1483 + "received\n", __FUNCTION__, state->unit);
1484 + state->flushexpected = 1;
1485 + return DECOMP_ERROR;
1489 + (state->stats).unc_bytes += uncomplen;
1490 + (state->stats).unc_packets++;
1496 +/************************************************************
1497 + * Module interface table
1498 + ************************************************************/
1500 +/* These are in ppp_generic.c */
1501 +extern int ppp_register_compressor (struct compressor *cp);
1502 +extern void ppp_unregister_compressor (struct compressor *cp);
1505 + * Functions exported to ppp_generic.c.
1507 + * In case of MPPC/MPPE there is no need to process incompressible data
1508 + * because such a data is sent in MPPC/MPPE frame. Therefore the (*incomp)
1509 + * callback function isn't needed.
1511 +struct compressor ppp_mppe = {
1512 + CI_MPPE, /* compress_proto */
1513 + mppe_comp_alloc, /* comp_alloc */
1514 + mppe_comp_free, /* comp_free */
1515 + mppe_comp_init, /* comp_init */
1516 + mppe_comp_reset, /* comp_reset */
1517 + mppe_compress, /* compress */
1518 + mppe_stats, /* comp_stat */
1519 + mppe_decomp_alloc, /* decomp_alloc */
1520 + mppe_comp_free, /* decomp_free */
1521 + mppe_decomp_init, /* decomp_init */
1522 + mppe_decomp_reset, /* decomp_reset */
1523 + mppe_decompress, /* decompress */
1524 + NULL, /* incomp */
1525 + mppe_stats, /* decomp_stat */
1528 +/************************************************************
1529 + * Module support routines
1530 + ************************************************************/
1532 +int __init mppe_module_init(void)
1536 + /* When the module is compiled into kernel, it is initialized before
1537 + * CryptoAPI. Therefore the code below reports that there is no crypto
1538 + * stuff in the kernel. Marlon de Boer who has pointed out the problem,
1539 + * has sent also a message to CryptoAPI developers, so maybe in the future
1540 + * the code will be uncommented.
1542 + if (!(crypto_alg_available("arc4", 0) && crypto_alg_available("sha1", 0))) {
1543 + printk(KERN_ERR "Kernel doesn't provide ARC4 and/or SHA1 algorithms "
1544 + "required by MPPE/MPPC. Check CryptoAPI configuration.\n");
1549 + /* Allocate space for SHAPad1, SHAPad2 and ... */
1550 + sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
1551 + if (sha_pad == NULL)
1553 + /* ... initialize them */
1554 + memset(sha_pad->sha_pad1, 0x00, sizeof(sha_pad->sha_pad1));
1555 + memset(sha_pad->sha_pad2, 0xf2, sizeof(sha_pad->sha_pad2));
1557 + answer = ppp_register_compressor(&ppp_mppe);
1558 + if (answer == 0) {
1559 + printk(KERN_INFO "MPPE/MPPC encryption/compression module registered\n");
1564 +void __exit mppe_module_cleanup(void)
1567 + ppp_unregister_compressor(&ppp_mppe);
1568 + printk(KERN_INFO "MPPE/MPPC encryption/compression module unregistered\n");
1571 +module_init(mppe_module_init);
1572 +module_exit(mppe_module_cleanup);
1574 +MODULE_AUTHOR("Jan Dubiec <jdx@slackware.pl>");
1575 +MODULE_DESCRIPTION("MPPE/MPPC encryption/compression module for Linux");
1576 +MODULE_LICENSE("Dual BSD/GPL");
1577 diff -ruN linux-2.4.29.orig/include/linux/ppp-comp.h linux-2.4.29/include/linux/ppp-comp.h
1578 --- linux-2.4.29.orig/include/linux/ppp-comp.h 2005-02-02 22:18:25.000000000 +0100
1579 +++ linux-2.4.29/include/linux/ppp-comp.h 2005-02-02 22:41:01.000000000 +0100
1584 - * ==FILEVERSION 980319==
1585 + * ==FILEVERSION 20040509==
1587 * NOTE TO MAINTAINERS:
1588 * If you modify this file at all, please set the above date.
1591 /* Compress a packet */
1592 int (*compress) (void *state, unsigned char *rptr,
1593 - unsigned char *obuf, int isize, int osize);
1594 + unsigned char *obuf, int isize, int osize);
1596 /* Return compression statistics */
1597 void (*comp_stat) (void *state, struct compstat *stats);
1600 /* Decompress a packet. */
1601 int (*decompress) (void *state, unsigned char *ibuf, int isize,
1602 - unsigned char *obuf, int osize);
1603 + unsigned char *obuf, int osize);
1605 /* Update state for an incompressible packet received */
1606 void (*incomp) (void *state, unsigned char *ibuf, int icnt);
1607 @@ -187,6 +187,42 @@
1608 #define DEFLATE_CHK_SEQUENCE 0
1611 + * Definitions for MPPE/MPPC.
1614 +#define CI_MPPE 18 /* config option for MPPE */
1615 +#define CILEN_MPPE 6 /* length of config option */
1617 +#define MPPE_OVHD 4 /* MPPE overhead */
1618 +#define MPPE_MAX_KEY_LEN 16 /* largest key length (128-bit) */
1620 +#define MPPE_STATELESS 0x01 /* configuration bit H */
1621 +#define MPPE_40BIT 0x20 /* configuration bit L */
1622 +#define MPPE_56BIT 0x80 /* configuration bit M */
1623 +#define MPPE_128BIT 0x40 /* configuration bit S */
1624 +#define MPPE_MPPC 0x01 /* configuration bit C */
1627 + * Definitions for Stac LZS.
1630 +#define CI_LZS 17 /* config option for Stac LZS */
1631 +#define CILEN_LZS 5 /* length of config option */
1633 +#define LZS_OVHD 4 /* max. LZS overhead */
1634 +#define LZS_HIST_LEN 2048 /* LZS history size */
1635 +#define LZS_MAX_CCOUNT 0x0FFF /* max. coherency counter value */
1637 +#define LZS_MODE_NONE 0
1638 +#define LZS_MODE_LCB 1
1639 +#define LZS_MODE_CRC 2
1640 +#define LZS_MODE_SEQ 3
1641 +#define LZS_MODE_EXT 4
1643 +#define LZS_EXT_BIT_FLUSHED 0x80 /* bit A */
1644 +#define LZS_EXT_BIT_COMP 0x20 /* bit C */
1647 * Definitions for other, as yet unsupported, compression methods.