1 diff -ruN linux-2.6.12.orig/drivers/net/Kconfig linux-2.6.12/drivers/net/Kconfig
2 --- linux-2.6.12.orig/drivers/net/Kconfig 2005-06-28 19:57:16.000000000 +0200
3 +++ linux-2.6.12/drivers/net/Kconfig 2005-06-28 20:07:01.000000000 +0200
5 module; it is called bsd_comp and will show up in the directory
6 modules once you have said "make modules". If unsure, say N.
9 + tristate "Microsoft PPP compression/encryption (MPPC/MPPE)"
14 + Support for the Microsoft Point-To-Point Compression (RFC2118) and
15 + Microsoft Point-To-Point Encryption (RFC3078). These protocols are
16 + supported by Microsoft Windows and wide range of "hardware" access
17 + servers. MPPE is common protocol in Virtual Private Networks. According
18 + to RFC3078, MPPE supports 40, 56 and 128-bit key lengths. Depending on
19 + PPP daemon configuration on both ends of the link, following scenarios
21 + - only compression (MPPC) is used,
22 + - only encryption (MPPE) is used,
23 + - compression and encryption (MPPC+MPPE) are used.
25 + Please note that Hi/Fn (http://www.hifn.com) holds patent on MPPC so
26 + you should check if this patent is valid in your country in order to
27 + avoid legal problems.
29 + For more information please visit http://free.polbox.pl/h/hs001
31 + To compile this driver as a module, choose M here. The module will
32 + be called ppp_mppe_mppc.ko.
35 tristate "PPP over Ethernet (EXPERIMENTAL)"
36 depends on EXPERIMENTAL && PPP
37 diff -ruN linux-2.6.12.orig/drivers/net/Makefile linux-2.6.12/drivers/net/Makefile
38 --- linux-2.6.12.orig/drivers/net/Makefile 2005-06-28 19:57:16.000000000 +0200
39 +++ linux-2.6.12/drivers/net/Makefile 2005-06-28 20:07:01.000000000 +0200
41 obj-$(CONFIG_PPP_SYNC_TTY) += ppp_synctty.o
42 obj-$(CONFIG_PPP_DEFLATE) += ppp_deflate.o
43 obj-$(CONFIG_PPP_BSDCOMP) += bsd_comp.o
44 +obj-$(CONFIG_PPP_MPPE_MPPC) += ppp_mppe_mppc.o
45 obj-$(CONFIG_PPPOE) += pppox.o pppoe.o
47 obj-$(CONFIG_SLIP) += slip.o
48 diff -ruN linux-2.6.12.orig/drivers/net/ppp_generic.c linux-2.6.12/drivers/net/ppp_generic.c
49 --- linux-2.6.12.orig/drivers/net/ppp_generic.c 2005-06-28 19:57:20.000000000 +0200
50 +++ linux-2.6.12/drivers/net/ppp_generic.c 2005-06-28 20:07:01.000000000 +0200
52 * PPP driver, written by Michael Callahan and Al Longyear, and
53 * subsequently hacked by Paul Mackerras.
55 - * ==FILEVERSION 20041108==
56 + * ==FILEVERSION 20050110==
59 #include <linux/config.h>
61 spinlock_t rlock; /* lock for receive side 58 */
62 spinlock_t wlock; /* lock for transmit side 5c */
63 int mru; /* max receive unit 60 */
64 + int mru_alloc; /* MAX(1500,MRU) for dev_alloc_skb() */
65 unsigned int flags; /* control bits 64 */
66 unsigned int xstate; /* transmit state bits 68 */
67 unsigned int rstate; /* receive state bits 6c */
73 + ppp->mru_alloc = ppp->mru = val;
74 + if (ppp->mru_alloc < PPP_MRU)
75 + ppp->mru_alloc = PPP_MRU; /* increase for broken peers */
79 @@ -1107,14 +1110,37 @@
81 /* peek at outbound CCP frames */
82 ppp_ccp_peek(ppp, skb, 0);
84 + * When LZS or MPPE/MPPC has been negotiated we don't send
85 + * CCP_RESETACK after receiving CCP_RESETREQ; in fact pppd
86 + * sends such a packet but we silently discard it here
88 + if (CCP_CODE(skb->data+2) == CCP_RESETACK
89 + && (ppp->xcomp->compress_proto == CI_MPPE
90 + || ppp->xcomp->compress_proto == CI_LZS)) {
91 + --ppp->stats.tx_packets;
92 + ppp->stats.tx_bytes -= skb->len - 2;
99 /* try to do packet compression */
100 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
101 && proto != PPP_LCP && proto != PPP_CCP) {
102 - new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
106 + * because of possible data expansion when MPPC or LZS
107 + * is used, allocate compressor's buffer 12.5% bigger
110 + if (ppp->xcomp->compress_proto == CI_MPPE)
111 + comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + MPPE_OVHD;
112 + else if (ppp->xcomp->compress_proto == CI_LZS)
113 + comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + LZS_OVHD;
114 + new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len
115 + + comp_ovhd, GFP_ATOMIC);
117 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
119 @@ -1132,9 +1158,21 @@
122 skb_pull(skb, 2); /* pull off A/C bytes */
124 + } else if (len == 0) {
125 /* didn't compress, or CCP not up yet */
130 + * MPPE requires that we do not send unencrypted
131 + * frames. The compressor will return -1 if we
132 + * should drop the frame. We cannot simply test
133 + * the compress_proto because MPPE and MPPC share
136 + printk(KERN_ERR "ppp: compressor dropped pkt\n");
137 + kfree_skb(new_skb);
142 @@ -1640,14 +1678,15 @@
145 if (proto == PPP_COMP) {
146 - ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
147 + ns = dev_alloc_skb(ppp->mru_alloc + PPP_HDRLEN);
149 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
152 /* the decompressor still expects the A/C bytes in the hdr */
153 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
154 - skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
155 + skb->len + 2, ns->data,
156 + ppp->mru_alloc + PPP_HDRLEN);
158 /* Pass the compressed frame to pppd as an
160 @@ -1673,7 +1712,14 @@
164 - ppp->rstate |= SC_DC_ERROR;
165 + if (ppp->rcomp->compress_proto != CI_MPPE
166 + && ppp->rcomp->compress_proto != CI_LZS) {
168 + * If decompression protocol isn't MPPE/MPPC or LZS, we set
169 + * SC_DC_ERROR flag and wait for CCP_RESETACK
171 + ppp->rstate |= SC_DC_ERROR;
173 ppp_receive_error(ppp);
176 @@ -2349,6 +2395,7 @@
177 memset(ppp, 0, sizeof(struct ppp));
180 + ppp->mru_alloc = PPP_MRU;
181 init_ppp_file(&ppp->file, INTERFACE);
182 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
183 for (i = 0; i < NUM_NP; ++i)
184 diff -ruN linux-2.6.12.orig/drivers/net/ppp_mppe_mppc.c linux-2.6.12/drivers/net/ppp_mppe_mppc.c
185 --- linux-2.6.12.orig/drivers/net/ppp_mppe_mppc.c 1970-01-01 01:00:00.000000000 +0100
186 +++ linux-2.6.12/drivers/net/ppp_mppe_mppc.c 2005-06-28 20:07:01.000000000 +0200
189 + * ppp_mppe_mppc.c - MPPC/MPPE "compressor/decompressor" module.
191 + * Copyright (c) 1994 Árpád Magosányi <mag@bunuel.tii.matav.hu>
192 + * Copyright (c) 1999 Tim Hockin, Cobalt Networks Inc. <thockin@cobaltnet.com>
193 + * Copyright (c) 2002-2004 Jan Dubiec <jdx@slackware.pl>
195 + * Permission to use, copy, modify, and distribute this software and its
196 + * documentation is hereby granted, provided that the above copyright
197 + * notice appears in all copies. This software is provided without any
198 + * warranty, express or implied.
200 + * The code is based on MPPE kernel module written by Árpád Magosányi and
201 + * Tim Hockin which can be found on http://planetmirror.com/pub/mppe/.
202 + * I have added MPPC and 56 bit session keys support in MPPE.
204 + * WARNING! Although this is open source code, its usage in some countries
205 + * (in particular in the USA) may violate Stac Inc. patent for MPPC.
207 + * ==FILEVERSION 20041123==
211 +#include <linux/init.h>
212 +#include <linux/module.h>
213 +#include <linux/mm.h>
214 +#include <linux/slab.h>
215 +#include <asm/scatterlist.h>
216 +#include <linux/vmalloc.h>
217 +#include <linux/crypto.h>
219 +#include <linux/ppp_defs.h>
220 +#include <linux/ppp-comp.h>
223 + * State for a mppc/mppe "(de)compressor".
225 +struct ppp_mppe_state {
226 + struct crypto_tfm *arc4_tfm;
227 + struct crypto_tfm *sha1_tfm;
229 + u8 master_key[MPPE_MAX_KEY_LEN];
230 + u8 session_key[MPPE_MAX_KEY_LEN];
231 + u8 mppc; /* do we use compression (MPPC)? */
232 + u8 mppe; /* do we use encryption (MPPE)? */
233 + u8 keylen; /* key length in bytes */
234 + u8 bitkeylen; /* key length in bits */
235 + u16 ccount; /* coherency counter */
236 + u16 bits; /* MPPC/MPPE control bits */
237 + u8 stateless; /* do we use stateless mode? */
238 + u8 nextflushed; /* set A bit in the next outgoing packet;
239 + used only by compressor*/
240 + u8 flushexpected; /* drop packets until A bit is received;
241 + used only by decompressor*/
242 + u8 *hist; /* MPPC history */
243 + u16 *hash; /* Hash table; used only by compressor */
244 + u16 histptr; /* history "cursor" */
248 + struct compstat stats;
251 +#define MPPE_HIST_LEN 8192 /* MPPC history size */
252 +#define MPPE_MAX_CCOUNT 0x0FFF /* max. coherency counter value */
254 +#define MPPE_BIT_FLUSHED 0x80 /* bit A */
255 +#define MPPE_BIT_RESET 0x40 /* bit B */
256 +#define MPPE_BIT_COMP 0x20 /* bit C */
257 +#define MPPE_BIT_ENCRYPTED 0x10 /* bit D */
259 +#define MPPE_SALT0 0xD1 /* values used in MPPE key derivation */
260 +#define MPPE_SALT1 0x26 /* according to RFC3079 */
261 +#define MPPE_SALT2 0x9E
263 +#define MPPE_CCOUNT(x) ((((x)[4] & 0x0f) << 8) + (x)[5])
264 +#define MPPE_BITS(x) ((x)[4] & 0xf0)
265 +#define MPPE_CTRLHI(x) ((((x)->ccount & 0xf00)>>8)|((x)->bits))
266 +#define MPPE_CTRLLO(x) ((x)->ccount & 0xff)
269 + * Kernel Crypto API needs its arguments to be in kmalloc'd memory, not in the
270 + * module static data area. That means sha_pad needs to be kmalloc'd. It is done
271 + * in mppe_module_init(). This has been pointed out on 30th July 2004 by Oleg
272 + * Makarenko on pptpclient-devel mailing list.
274 +#define SHA1_PAD_SIZE 40
276 + unsigned char sha_pad1[SHA1_PAD_SIZE];
277 + unsigned char sha_pad2[SHA1_PAD_SIZE];
279 +static struct sha_pad *sha_pad;
282 +setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
284 + sg[0].page = virt_to_page(address);
285 + sg[0].offset = offset_in_page(address);
286 + sg[0].length = length;
290 +arc4_setkey(struct ppp_mppe_state *state, const unsigned char *key,
291 + const unsigned int keylen)
293 + crypto_cipher_setkey(state->arc4_tfm, key, keylen);
297 +arc4_encrypt(struct ppp_mppe_state *state, const unsigned char *in,
298 + const unsigned int len, unsigned char *out)
300 + struct scatterlist sgin[4], sgout[4];
302 + setup_sg(sgin, in, len);
303 + setup_sg(sgout, out, len);
304 + crypto_cipher_encrypt(state->arc4_tfm, sgout, sgin, len);
307 +#define arc4_decrypt arc4_encrypt
310 + * Key Derivation, from RFC 3078, RFC 3079.
311 + * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
314 +get_new_key_from_sha(struct ppp_mppe_state *state, unsigned char *interim_key)
316 + struct scatterlist sg[4];
318 + setup_sg(&sg[0], state->master_key, state->keylen);
319 + setup_sg(&sg[1], sha_pad->sha_pad1, sizeof(sha_pad->sha_pad1));
320 + setup_sg(&sg[2], state->session_key, state->keylen);
321 + setup_sg(&sg[3], sha_pad->sha_pad2, sizeof(sha_pad->sha_pad2));
323 + crypto_digest_digest (state->sha1_tfm, sg, 4, state->sha1_digest);
325 + memcpy(interim_key, state->sha1_digest, state->keylen);
329 +mppe_change_key(struct ppp_mppe_state *state, int initialize)
331 + unsigned char interim_key[MPPE_MAX_KEY_LEN];
333 + get_new_key_from_sha(state, interim_key);
335 + memcpy(state->session_key, interim_key, state->keylen);
337 + arc4_setkey(state, interim_key, state->keylen);
338 + arc4_encrypt(state, interim_key, state->keylen, state->session_key);
340 + if (state->keylen == 8) {
341 + if (state->bitkeylen == 40) {
342 + state->session_key[0] = MPPE_SALT0;
343 + state->session_key[1] = MPPE_SALT1;
344 + state->session_key[2] = MPPE_SALT2;
346 + state->session_key[0] = MPPE_SALT0;
349 + arc4_setkey(state, state->session_key, state->keylen);
352 +/* increase 12-bit coherency counter */
354 +mppe_increase_ccount(struct ppp_mppe_state *state)
356 + state->ccount = (state->ccount + 1) & MPPE_MAX_CCOUNT;
358 + if (state->stateless) {
359 + mppe_change_key(state, 0);
360 + state->nextflushed = 1;
362 + if ((state->ccount & 0xff) == 0xff) {
363 + mppe_change_key(state, 0);
369 +/* allocate space for a MPPE/MPPC (de)compressor. */
370 +/* comp != 0 -> init compressor */
371 +/* comp = 0 -> init decompressor */
373 +mppe_alloc(unsigned char *options, int opt_len, int comp)
375 + struct ppp_mppe_state *state;
376 + unsigned int digestsize;
379 + fname = comp ? "mppe_comp_alloc" : "mppe_decomp_alloc";
382 + * Hack warning - additionally to the standard MPPC/MPPE configuration
383 + * options, pppd passes to the (de)copressor 8 or 16 byte session key.
384 + * Therefore options[1] contains MPPC/MPPE configuration option length
385 + * (CILEN_MPPE = 6), but the real options length, depending on the key
386 + * length, is 6+8 or 6+16.
388 + if (opt_len < CILEN_MPPE) {
389 + printk(KERN_WARNING "%s: wrong options length: %u\n", fname, opt_len);
393 + if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
394 + (options[2] & ~MPPE_STATELESS) != 0 ||
395 + options[3] != 0 || options[4] != 0 ||
396 + (options[5] & ~(MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
397 + (options[5] & (MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
398 + printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
399 + "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname, options[0],
400 + options[1], options[2], options[3], options[4], options[5]);
404 + state = (struct ppp_mppe_state *)kmalloc(sizeof(*state), GFP_KERNEL);
405 + if (state == NULL) {
406 + printk(KERN_ERR "%s: cannot allocate space for %scompressor\n", fname,
410 + memset(state, 0, sizeof(struct ppp_mppe_state));
412 + state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */
413 + state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
414 + MPPE_40BIT); /* Do we use MPPE? */
417 + /* allocate MPPC history */
418 + state->hist = (u8*)vmalloc(2*MPPE_HIST_LEN*sizeof(u8));
419 + if (state->hist == NULL) {
421 + printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
426 + /* allocate hashtable for MPPC compressor */
428 + state->hash = (u16*)vmalloc(MPPE_HIST_LEN*sizeof(u16));
429 + if (state->hash == NULL) {
430 + vfree(state->hist);
432 + printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
439 + if (state->mppe) { /* specific for MPPE */
440 + /* Load ARC4 algorithm */
441 + state->arc4_tfm = crypto_alloc_tfm("arc4", 0);
442 + if (state->arc4_tfm == NULL) {
444 + vfree(state->hash);
446 + vfree(state->hist);
449 + printk(KERN_ERR "%s: cannot load ARC4 module\n", fname);
453 + /* Load SHA1 algorithm */
454 + state->sha1_tfm = crypto_alloc_tfm("sha1", 0);
455 + if (state->sha1_tfm == NULL) {
456 + crypto_free_tfm(state->arc4_tfm);
458 + vfree(state->hash);
460 + vfree(state->hist);
463 + printk(KERN_ERR "%s: cannot load SHA1 module\n", fname);
467 + digestsize = crypto_tfm_alg_digestsize(state->sha1_tfm);
468 + if (digestsize < MPPE_MAX_KEY_LEN) {
469 + crypto_free_tfm(state->sha1_tfm);
470 + crypto_free_tfm(state->arc4_tfm);
472 + vfree(state->hash);
474 + vfree(state->hist);
477 + printk(KERN_ERR "%s: CryptoAPI SHA1 digest size too small\n", fname);
480 + state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
481 + if (!state->sha1_digest) {
482 + crypto_free_tfm(state->sha1_tfm);
483 + crypto_free_tfm(state->arc4_tfm);
485 + vfree(state->hash);
487 + vfree(state->hist);
490 + printk(KERN_ERR "%s: cannot allocate space for SHA1 digest\n", fname);
493 + memcpy(state->master_key, options+CILEN_MPPE, MPPE_MAX_KEY_LEN);
494 + memcpy(state->session_key, state->master_key, MPPE_MAX_KEY_LEN);
495 + /* initial key generation is done in mppe_init() */
498 + return (void *) state;
502 +mppe_comp_alloc(unsigned char *options, int opt_len)
504 + return mppe_alloc(options, opt_len, 1);
508 +mppe_decomp_alloc(unsigned char *options, int opt_len)
510 + return mppe_alloc(options, opt_len, 0);
513 +/* cleanup the (de)compressor */
515 +mppe_comp_free(void *arg)
517 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
519 + if (state != NULL) {
521 + if (state->sha1_digest != NULL)
522 + kfree(state->sha1_digest);
523 + if (state->sha1_tfm != NULL)
524 + crypto_free_tfm(state->sha1_tfm);
525 + if (state->arc4_tfm != NULL)
526 + crypto_free_tfm(state->arc4_tfm);
528 + if (state->hist != NULL)
529 + vfree(state->hist);
530 + if (state->hash != NULL)
531 + vfree(state->hash);
536 +/* init MPPC/MPPE (de)compresor */
537 +/* comp != 0 -> init compressor */
538 +/* comp = 0 -> init decompressor */
540 +mppe_init(void *arg, unsigned char *options, int opt_len, int unit,
541 + int hdrlen, int mru, int debug, int comp)
543 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
546 + fname = comp ? "mppe_comp_init" : "mppe_decomp_init";
548 + if (opt_len < CILEN_MPPE) {
550 + printk(KERN_WARNING "%s: wrong options length: %u\n",
555 + if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
556 + (options[2] & ~MPPE_STATELESS) != 0 ||
557 + options[3] != 0 || options[4] != 0 ||
558 + (options[5] & ~(MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
559 + (options[5] & (MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
561 + printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
562 + "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname,
563 + options[0], options[1], options[2], options[3], options[4],
568 + if ((options[5] & ~MPPE_MPPC) != MPPE_128BIT &&
569 + (options[5] & ~MPPE_MPPC) != MPPE_56BIT &&
570 + (options[5] & ~MPPE_MPPC) != MPPE_40BIT &&
571 + (options[5] & MPPE_MPPC) != MPPE_MPPC) {
573 + printk(KERN_WARNING "%s: don't know what to do: o[5]=%02x\n",
574 + fname, options[5]);
578 + state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */
579 + state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
580 + MPPE_40BIT); /* Do we use MPPE? */
581 + state->stateless = options[2] & MPPE_STATELESS; /* Do we use stateless mode? */
583 + switch (state->mppe) {
584 + case MPPE_40BIT: /* 40 bit key */
586 + state->bitkeylen = 40;
588 + case MPPE_56BIT: /* 56 bit key */
590 + state->bitkeylen = 56;
592 + case MPPE_128BIT: /* 128 bit key */
593 + state->keylen = 16;
594 + state->bitkeylen = 128;
598 + state->bitkeylen = 0;
601 + state->ccount = MPPE_MAX_CCOUNT;
603 + state->unit = unit;
604 + state->debug = debug;
605 + state->histptr = MPPE_HIST_LEN;
606 + if (state->mppc) { /* reset history if MPPC was negotiated */
607 + memset(state->hist, 0, 2*MPPE_HIST_LEN*sizeof(u8));
610 + if (state->mppe) { /* generate initial session keys */
611 + mppe_change_key(state, 1);
614 + if (comp) { /* specific for compressor */
615 + state->nextflushed = 1;
616 + } else { /* specific for decompressor */
618 + state->flushexpected = 1;
625 +mppe_comp_init(void *arg, unsigned char *options, int opt_len, int unit,
626 + int hdrlen, int debug)
628 + return mppe_init(arg, options, opt_len, unit, hdrlen, 0, debug, 1);
633 +mppe_decomp_init(void *arg, unsigned char *options, int opt_len, int unit,
634 + int hdrlen, int mru, int debug)
636 + return mppe_init(arg, options, opt_len, unit, hdrlen, mru, debug, 0);
640 +mppe_comp_reset(void *arg)
642 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
645 + printk(KERN_DEBUG "%s%d: resetting MPPC/MPPE compressor\n",
646 + __FUNCTION__, state->unit);
648 + state->nextflushed = 1;
650 + arc4_setkey(state, state->session_key, state->keylen);
654 +mppe_decomp_reset(void *arg)
656 + /* When MPPC/MPPE is in use, we shouldn't receive any CCP Reset-Ack.
657 + But when we receive such a packet, we just ignore it. */
662 +mppe_stats(void *arg, struct compstat *stats)
664 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
666 + *stats = state->stats;
669 +/***************************/
670 +/**** Compression stuff ****/
671 +/***************************/
672 +/* inserts 1 to 8 bits into the output buffer */
673 +static inline void putbits8(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
679 + *buf = *buf | (val & 0xff);
689 + *buf = *buf | ((val >> 8) & 0xff);
690 + *(++buf) = val & 0xff;
694 +/* inserts 9 to 16 bits into the output buffer */
695 +static inline void putbits16(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
702 + *buf = *buf | ((val >> 8) & 0xff);
703 + *(++buf) = val & 0xff;
711 + *l = 16 - n + (*l);
713 + *buf = *buf | ((val >> 16) & 0xff);
714 + *(++buf) = (val >> 8) & 0xff;
715 + *(++buf) = val & 0xff;
719 +/* inserts 17 to 24 bits into the output buffer */
720 +static inline void putbits24(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
723 + if (*l >= n - 16) {
725 + *l = 16 - n + (*l);
727 + *buf = *buf | ((val >> 16) & 0xff);
728 + *(++buf) = (val >> 8) & 0xff;
729 + *(++buf) = val & 0xff;
736 + (*i)++; (*i)++; (*i)++;
737 + *l = 24 - n + (*l);
739 + *buf = *buf | ((val >> 24) & 0xff);
740 + *(++buf) = (val >> 16) & 0xff;
741 + *(++buf) = (val >> 8) & 0xff;
742 + *(++buf) = val & 0xff;
747 +mppc_compress(struct ppp_mppe_state *state, unsigned char *ibuf,
748 + unsigned char *obuf, int isize, int osize)
750 + u32 olen, off, len, idx, i, l;
751 + u8 *hist, *sbuf, *p, *q, *r, *s;
754 + At this point, to avoid possible buffer overflow caused by packet
755 + expansion during/after compression, we should make sure that
756 + osize >= (((isize*9)/8)+1)+2, but we don't do that because in
757 + ppp_generic.c we simply allocate bigger obuf.
759 + Maximum MPPC packet expansion is 12.5%. This is the worst case when
760 + all octets in the input buffer are >= 0x80 and we cannot find any
761 + repeated tokens. Additionally we have to reserve 2 bytes for MPPE/MPPC
762 + status bits and coherency counter.
765 + hist = state->hist + MPPE_HIST_LEN;
766 + /* check if there is enough room at the end of the history */
767 + if (state->histptr + isize >= 2*MPPE_HIST_LEN) {
768 + state->bits |= MPPE_BIT_RESET;
769 + state->histptr = MPPE_HIST_LEN;
770 + memcpy(state->hist, hist, MPPE_HIST_LEN);
772 + /* add packet to the history; isize must be <= MPPE_HIST_LEN */
773 + sbuf = state->hist + state->histptr;
774 + memcpy(sbuf, ibuf, isize);
775 + state->histptr += isize;
777 + /* compress data */
779 + *obuf = olen = i = 0;
781 + while (i < isize - 2) {
783 + idx = ((40543*((((s[0]<<4)^s[1])<<4)^s[2]))>>4) & 0x1fff;
784 + p = hist + state->hash[idx];
785 + state->hash[idx] = (u16) (s - hist);
787 + if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ || *p++ != *s++ ||
789 + /* no match found; encode literal byte */
790 + if (ibuf[i] < 0x80) { /* literal byte < 0x80 */
791 + putbits8(obuf, (u32) ibuf[i], 8, &olen, &l);
792 + } else { /* literal byte >= 0x80 */
793 + putbits16(obuf, (u32) (0x100|(ibuf[i]&0x7f)), 9, &olen, &l);
799 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
800 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
801 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
802 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
803 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
804 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
805 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
806 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
807 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
808 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
809 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
810 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
811 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
812 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
813 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
817 + while((*p++ == *s++) && (s < r) && (p < q));
820 + while((*p++ == *s++) && (s < r) && (p < q));
825 + /* at least 3 character match found; code data */
826 + /* encode offset */
827 + if (off < 64) { /* 10-bit offset; 0 <= offset < 64 */
828 + putbits16(obuf, 0x3c0|off, 10, &olen, &l);
829 + } else if (off < 320) { /* 12-bit offset; 64 <= offset < 320 */
830 + putbits16(obuf, 0xe00|(off-64), 12, &olen, &l);
831 + } else if (off < 8192) { /* 16-bit offset; 320 <= offset < 8192 */
832 + putbits16(obuf, 0xc000|(off-320), 16, &olen, &l);
834 + /* This shouldn't happen; we return 0 what means "packet expands",
835 + and we send packet uncompressed. */
837 + printk(KERN_DEBUG "%s%d: wrong offset value: %d\n",
838 + __FUNCTION__, state->unit, off);
841 + /* encode length of match */
842 + if (len < 4) { /* length = 3 */
843 + putbits8(obuf, 0, 1, &olen, &l);
844 + } else if (len < 8) { /* 4 <= length < 8 */
845 + putbits8(obuf, 0x08|(len&0x03), 4, &olen, &l);
846 + } else if (len < 16) { /* 8 <= length < 16 */
847 + putbits8(obuf, 0x30|(len&0x07), 6, &olen, &l);
848 + } else if (len < 32) { /* 16 <= length < 32 */
849 + putbits8(obuf, 0xe0|(len&0x0f), 8, &olen, &l);
850 + } else if (len < 64) { /* 32 <= length < 64 */
851 + putbits16(obuf, 0x3c0|(len&0x1f), 10, &olen, &l);
852 + } else if (len < 128) { /* 64 <= length < 128 */
853 + putbits16(obuf, 0xf80|(len&0x3f), 12, &olen, &l);
854 + } else if (len < 256) { /* 128 <= length < 256 */
855 + putbits16(obuf, 0x3f00|(len&0x7f), 14, &olen, &l);
856 + } else if (len < 512) { /* 256 <= length < 512 */
857 + putbits16(obuf, 0xfe00|(len&0xff), 16, &olen, &l);
858 + } else if (len < 1024) { /* 512 <= length < 1024 */
859 + putbits24(obuf, 0x3fc00|(len&0x1ff), 18, &olen, &l);
860 + } else if (len < 2048) { /* 1024 <= length < 2048 */
861 + putbits24(obuf, 0xff800|(len&0x3ff), 20, &olen, &l);
862 + } else if (len < 4096) { /* 2048 <= length < 4096 */
863 + putbits24(obuf, 0x3ff000|(len&0x7ff), 22, &olen, &l);
864 + } else if (len < 8192) { /* 4096 <= length < 8192 */
865 + putbits24(obuf, 0xffe000|(len&0xfff), 24, &olen, &l);
867 + /* This shouldn't happen; we return 0 what means "packet expands",
868 + and send packet uncompressed. */
870 + printk(KERN_DEBUG "%s%d: wrong length of match value: %d\n",
871 + __FUNCTION__, state->unit, len);
876 + /* Add remaining octets to the output */
877 + while(isize - i > 0) {
878 + if (ibuf[i] < 0x80) { /* literal byte < 0x80 */
879 + putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l);
880 + } else { /* literal byte >= 0x80 */
881 + putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l);
884 + /* Reset unused bits of the last output octet */
885 + if ((l != 0) && (l != 8)) {
886 + putbits8(obuf, 0, l, &olen, &l);
893 +mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
894 + int isize, int osize)
896 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
897 + int proto, olen, complen, off;
898 + unsigned char *wptr;
900 + /* Check that the protocol is in the range we handle. */
901 + proto = PPP_PROTOCOL(ibuf);
902 + if (proto < 0x0021 || proto > 0x00fa)
906 + /* Copy over the PPP header */
907 + wptr[0] = PPP_ADDRESS(ibuf);
908 + wptr[1] = PPP_CONTROL(ibuf);
909 + wptr[2] = PPP_COMP >> 8;
910 + wptr[3] = PPP_COMP;
911 + wptr += PPP_HDRLEN + (MPPE_OVHD / 2); /* Leave two octets for MPPE/MPPC bits */
914 + * In ver. 0.99 protocol field was compressed. Deflate and BSD compress
915 + * do PFC before actual compression, RCF2118 and RFC3078 are not precise
916 + * on this topic so I decided to do PFC. Unfortunately this change caused
917 + * incompatibility with older/other MPPE/MPPC modules. I have received
918 + * a lot of complaints from unexperienced users so I have decided to revert
919 + * to previous state, i.e. the protocol field is sent uncompressed now.
920 + * Although this may be changed in the future.
922 + * Receiving side (mppe_decompress()) still accepts packets with compressed
923 + * and uncompressed protocol field so you shouldn't get "Unsupported protocol
924 + * 0x2145 received" messages anymore.
926 + //off = (proto > 0xff) ? 2 : 3; /* PFC - skip first protocol byte if 0 */
931 + mppe_increase_ccount(state);
933 + if (state->nextflushed) {
934 + state->bits |= MPPE_BIT_FLUSHED;
935 + state->nextflushed = 0;
936 + if (state->mppe && !state->stateless) {
938 + * If this is the flag packet, the key has been already changed in
939 + * mppe_increase_ccount() so we dont't do it once again.
941 + if ((state->ccount & 0xff) != 0xff) {
942 + arc4_setkey(state, state->session_key, state->keylen);
945 + if (state->mppc) { /* reset history */
946 + state->bits |= MPPE_BIT_RESET;
947 + state->histptr = MPPE_HIST_LEN;
948 + memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
952 + if (state->mppc && !state->mppe) { /* Do only compression */
953 + complen = mppc_compress(state, ibuf, wptr, isize - off,
954 + osize - PPP_HDRLEN - (MPPE_OVHD / 2));
956 + * TODO: Implement an heuristics to handle packet expansion in a smart
957 + * way. Now, when a packet expands, we send it as uncompressed and
958 + * when next packet is sent we have to reset compressor's history.
959 + * Maybe it would be better to send such packet as compressed in order
960 + * to keep history's continuity.
962 + if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
964 + /* packet expands */
965 + state->nextflushed = 1;
966 + memcpy(wptr, ibuf, isize - off);
967 + olen = isize - (off - 2) + MPPE_OVHD;
968 + (state->stats).inc_bytes += olen;
969 + (state->stats).inc_packets++;
971 + state->bits |= MPPE_BIT_COMP;
972 + olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
973 + (state->stats).comp_bytes += olen;
974 + (state->stats).comp_packets++;
976 + } else { /* Do encryption with or without compression */
977 + state->bits |= MPPE_BIT_ENCRYPTED;
978 + if (!state->mppc && state->mppe) { /* Do only encryption */
979 + /* read from ibuf, write to wptr, adjust for PPP_HDRLEN */
980 + arc4_encrypt(state, ibuf, isize - off, wptr);
981 + olen = isize - (off - 2) + MPPE_OVHD;
982 + (state->stats).inc_bytes += olen;
983 + (state->stats).inc_packets++;
984 + } else { /* Do compression and then encryption - RFC3078 */
985 + complen = mppc_compress(state, ibuf, wptr, isize - off,
986 + osize - PPP_HDRLEN - (MPPE_OVHD / 2));
988 + * TODO: Implement an heuristics to handle packet expansion in a smart
989 + * way. Now, when a packet expands, we send it as uncompressed and
990 + * when next packet is sent we have to reset compressor's history.
991 + * Maybe it would be good to send such packet as compressed in order
992 + * to keep history's continuity.
994 + if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
996 + /* packet expands */
997 + state->nextflushed = 1;
998 + arc4_encrypt(state, ibuf, isize - off, wptr);
999 + olen = isize - (off - 2) + MPPE_OVHD;
1000 + (state->stats).inc_bytes += olen;
1001 + (state->stats).inc_packets++;
1003 + state->bits |= MPPE_BIT_COMP;
1004 + /* Hack warning !!! RC4 implementation which we use does
1005 + encryption "in place" - it means that input and output
1006 + buffers can be *the same* memory area. Therefore we don't
1007 + need to use a temporary buffer. But be careful - other
1008 + implementations don't have to be so nice.
1009 + I used to use ibuf as temporary buffer here, but it led
1010 + packet sniffers into error. Thanks to Wilfried Weissmann
1011 + for pointing that. */
1012 + arc4_encrypt(state, wptr, complen, wptr);
1013 + olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
1014 + (state->stats).comp_bytes += olen;
1015 + (state->stats).comp_packets++;
1020 + /* write status bits and coherency counter into the output buffer */
1021 + wptr = obuf + PPP_HDRLEN;
1022 + wptr[0] = MPPE_CTRLHI(state);
1023 + wptr[1] = MPPE_CTRLLO(state);
1027 + (state->stats).unc_bytes += isize;
1028 + (state->stats).unc_packets++;
1033 +/***************************/
1034 +/*** Decompression stuff ***/
1035 +/***************************/
1036 +static inline u32 getbits(const u8 *buf, const u32 n, u32 *i, u32 *l)
1038 + static const u32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1044 + res = (buf[*i] & m[ol]) >> (*l);
1050 + *l = 8 - n + (*l);
1051 + res = (buf[(*i)++] & m[ol]) << 8;
1052 + res = (res | buf[*i]) >> (*l);
1058 +static inline u32 getbyte(const u8 *buf, const u32 i, const u32 l)
1063 + return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff;
1067 +static inline void lamecopy(u8 *dst, u8 *src, u32 len)
1074 +mppc_decompress(struct ppp_mppe_state *state, unsigned char *ibuf,
1075 + unsigned char *obuf, int isize, int osize)
1077 + u32 olen, off, len, bits, val, sig, i, l;
1080 + history = state->hist + state->histptr;
1081 + olen = len = i = 0;
1084 + while (bits >= 8) {
1085 + val = getbyte(ibuf, i++, l);
1086 + if (val < 0x80) { /* literal byte < 0x80 */
1087 + if (state->histptr < 2*MPPE_HIST_LEN) {
1088 + /* copy uncompressed byte to the history */
1089 + (state->hist)[(state->histptr)++] = (u8) val;
1091 + /* buffer overflow; drop packet */
1093 + printk(KERN_ERR "%s%d: trying to write outside history "
1094 + "buffer\n", __FUNCTION__, state->unit);
1095 + return DECOMP_ERROR;
1103 + if (sig == 0x80) { /* literal byte >= 0x80 */
1104 + if (state->histptr < 2*MPPE_HIST_LEN) {
1105 + /* copy uncompressed byte to the history */
1106 + (state->hist)[(state->histptr)++] =
1107 + (u8) (0x80|((val&0x3f)<<1)|getbits(ibuf, 1 , &i ,&l));
1109 + /* buffer overflow; drop packet */
1111 + printk(KERN_ERR "%s%d: trying to write outside history "
1112 + "buffer\n", __FUNCTION__, state->unit);
1113 + return DECOMP_ERROR;
1120 + /* Not a literal byte so it must be an (offset,length) pair */
1121 + /* decode offset */
1123 + if (sig == 0xf0) { /* 10-bit offset; 0 <= offset < 64 */
1124 + off = (((val&0x0f)<<2)|getbits(ibuf, 2 , &i ,&l));
1127 + if (sig == 0xe0) { /* 12-bit offset; 64 <= offset < 320 */
1128 + off = ((((val&0x0f)<<4)|getbits(ibuf, 4 , &i ,&l))+64);
1131 + if ((sig&0xe0) == 0xc0) {/* 16-bit offset; 320 <= offset < 8192 */
1132 + off = ((((val&0x1f)<<8)|getbyte(ibuf, i++, l))+320);
1134 + if (off > MPPE_HIST_LEN - 1) {
1136 + printk(KERN_DEBUG "%s%d: too big offset value: %d\n",
1137 + __FUNCTION__, state->unit, off);
1138 + return DECOMP_ERROR;
1140 + } else { /* this shouldn't happen */
1142 + printk(KERN_DEBUG "%s%d: cannot decode offset value\n",
1143 + __FUNCTION__, state->unit);
1144 + return DECOMP_ERROR;
1148 + /* decode length of match */
1149 + val = getbyte(ibuf, i, l);
1150 + if ((val & 0x80) == 0x00) { /* len = 3 */
1153 + getbits(ibuf, 1 , &i ,&l);
1154 + } else if ((val & 0xc0) == 0x80) { /* 4 <= len < 8 */
1155 + len = 0x04 | ((val>>4) & 0x03);
1157 + getbits(ibuf, 4 , &i ,&l);
1158 + } else if ((val & 0xe0) == 0xc0) { /* 8 <= len < 16 */
1159 + len = 0x08 | ((val>>2) & 0x07);
1161 + getbits(ibuf, 6 , &i ,&l);
1162 + } else if ((val & 0xf0) == 0xe0) { /* 16 <= len < 32 */
1163 + len = 0x10 | (val & 0x0f);
1168 + val = (val << 8) | getbyte(ibuf, ++i, l);
1169 + if ((val & 0xf800) == 0xf000) { /* 32 <= len < 64 */
1170 + len = 0x0020 | ((val >> 6) & 0x001f);
1172 + getbits(ibuf, 2 , &i ,&l);
1173 + } else if ((val & 0xfc00) == 0xf800) { /* 64 <= len < 128 */
1174 + len = 0x0040 | ((val >> 4) & 0x003f);
1176 + getbits(ibuf, 4 , &i ,&l);
1177 + } else if ((val & 0xfe00) == 0xfc00) { /* 128 <= len < 256 */
1178 + len = 0x0080 | ((val >> 2) & 0x007f);
1180 + getbits(ibuf, 6 , &i ,&l);
1181 + } else if ((val & 0xff00) == 0xfe00) { /* 256 <= len < 512 */
1182 + len = 0x0100 | (val & 0x00ff);
1187 + val = (val << 8) | getbyte(ibuf, ++i, l);
1188 + if ((val & 0xff8000) == 0xff0000) { /* 512 <= len < 1024 */
1189 + len = 0x000200 | ((val >> 6) & 0x0001ff);
1191 + getbits(ibuf, 2 , &i ,&l);
1192 + } else if ((val & 0xffc000) == 0xff8000) {/* 1024 <= len < 2048 */
1193 + len = 0x000400 | ((val >> 4) & 0x0003ff);
1195 + getbits(ibuf, 4 , &i ,&l);
1196 + } else if ((val & 0xffe000) == 0xffc000) {/* 2048 <= len < 4096 */
1197 + len = 0x000800 | ((val >> 2) & 0x0007ff);
1199 + getbits(ibuf, 6 , &i ,&l);
1200 + } else if ((val & 0xfff000) == 0xffe000) {/* 4096 <= len < 8192 */
1201 + len = 0x001000 | (val & 0x000fff);
1204 + } else { /* this shouldn't happen */
1206 + printk(KERN_DEBUG "%s%d: wrong length code: 0x%X\n",
1207 + __FUNCTION__, state->unit, val);
1208 + return DECOMP_ERROR;
1212 + s = state->hist + state->histptr;
1213 + state->histptr += len;
1215 + if (state->histptr < 2*MPPE_HIST_LEN) {
1216 + /* copy uncompressed bytes to the history */
1218 + /* In some cases len may be greater than off. It means that memory
1219 + * areas pointed by s and s-off overlap. I had used memmove() here
1220 + * because I thought that it acts as libc's version. Unfortunately,
1221 + * I was wrong. :-) I got strange errors sometimes. Wilfried suggested
1222 + * using of byte by byte copying here and strange errors disappeared.
1224 + lamecopy(s, s - off, len);
1226 + /* buffer overflow; drop packet */
1228 + printk(KERN_ERR "%s%d: trying to write outside history "
1229 + "buffer\n", __FUNCTION__, state->unit);
1230 + return DECOMP_ERROR;
1234 + /* Do PFC decompression */
1236 + if ((history[0] & 0x01) != 0) {
1242 + if (len <= osize) {
1243 + /* copy uncompressed packet to the output buffer */
1244 + memcpy(obuf, history, olen);
1246 + /* buffer overflow; drop packet */
1248 + printk(KERN_ERR "%s%d: too big uncompressed packet: %d\n",
1249 + __FUNCTION__, state->unit, len + (PPP_HDRLEN / 2));
1250 + return DECOMP_ERROR;
1257 +mppe_decompress(void *arg, unsigned char *ibuf, int isize,
1258 + unsigned char *obuf, int osize)
1260 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
1261 + int seq, bits, uncomplen;
1263 + if (isize <= PPP_HDRLEN + MPPE_OVHD) {
1264 + if (state->debug) {
1265 + printk(KERN_DEBUG "%s%d: short packet (len=%d)\n", __FUNCTION__,
1266 + state->unit, isize);
1268 + return DECOMP_ERROR;
1271 + /* Get coherency counter and control bits from input buffer */
1272 + seq = MPPE_CCOUNT(ibuf);
1273 + bits = MPPE_BITS(ibuf);
1275 + if (state->stateless) {
1276 + /* RFC 3078, sec 8.1. */
1277 + mppe_increase_ccount(state);
1278 + if ((seq != state->ccount) && state->debug)
1279 + printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1280 + __FUNCTION__, state->unit, seq, state->ccount);
1281 + while (seq != state->ccount)
1282 + mppe_increase_ccount(state);
1284 + /* RFC 3078, sec 8.2. */
1285 + if (state->flushexpected) { /* discard state */
1286 + if ((bits & MPPE_BIT_FLUSHED)) { /* we received expected FLUSH bit */
1287 + while (seq != state->ccount)
1288 + mppe_increase_ccount(state);
1289 + state->flushexpected = 0;
1290 + } else /* drop packet*/
1291 + return DECOMP_ERROR;
1292 + } else { /* normal state */
1293 + mppe_increase_ccount(state);
1294 + if (seq != state->ccount) {
1295 + /* Packet loss detected, enter the discard state. */
1297 + printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1298 + __FUNCTION__, state->unit, seq, state->ccount);
1299 + state->flushexpected = 1;
1300 + return DECOMP_ERROR;
1303 + if (state->mppe && (bits & MPPE_BIT_FLUSHED)) {
1304 + arc4_setkey(state, state->session_key, state->keylen);
1308 + if (state->mppc && (bits & (MPPE_BIT_FLUSHED | MPPE_BIT_RESET))) {
1309 + state->histptr = MPPE_HIST_LEN;
1310 + if ((bits & MPPE_BIT_FLUSHED)) {
1311 + memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
1313 + if ((bits & MPPE_BIT_RESET)) {
1314 + memcpy(state->hist, state->hist + MPPE_HIST_LEN, MPPE_HIST_LEN);
1318 + /* Fill in the first part of the PPP header. The protocol field
1319 + comes from the decompressed data. */
1320 + obuf[0] = PPP_ADDRESS(ibuf);
1321 + obuf[1] = PPP_CONTROL(ibuf);
1322 + obuf += PPP_HDRLEN / 2;
1324 + if (state->mppe) { /* process encrypted packet */
1325 + if ((bits & MPPE_BIT_ENCRYPTED)) {
1326 + /* OK, packet encrypted, so decrypt it */
1327 + if (state->mppc && (bits & MPPE_BIT_COMP)) {
1328 + /* Hack warning !!! RC4 implementation which we use does
1329 + decryption "in place" - it means that input and output
1330 + buffers can be *the same* memory area. Therefore we don't
1331 + need to use a temporary buffer. But be careful - other
1332 + implementations don't have to be so nice. */
1333 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1334 + PPP_HDRLEN - (MPPE_OVHD / 2), ibuf + PPP_HDRLEN +
1336 + uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1337 + (MPPE_OVHD / 2), obuf, isize -
1338 + PPP_HDRLEN - (MPPE_OVHD / 2),
1339 + osize - (PPP_HDRLEN / 2));
1340 + if (uncomplen == DECOMP_ERROR) {
1341 + state->flushexpected = 1;
1342 + return DECOMP_ERROR;
1344 + uncomplen += PPP_HDRLEN / 2;
1345 + (state->stats).comp_bytes += isize;
1346 + (state->stats).comp_packets++;
1348 + uncomplen = isize - MPPE_OVHD;
1349 + /* Decrypt the first byte in order to check if it is
1350 + compressed or uncompressed protocol field */
1351 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), 1, obuf);
1352 + /* Do PFC decompression */
1353 + if ((obuf[0] & 0x01) != 0) {
1354 + obuf[1] = obuf[0];
1359 + /* And finally, decrypt the rest of the frame. */
1360 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2) + 1,
1361 + isize - PPP_HDRLEN - (MPPE_OVHD / 2) - 1, obuf + 1);
1362 + (state->stats).inc_bytes += isize;
1363 + (state->stats).inc_packets++;
1365 + } else { /* this shouldn't happen */
1367 + printk(KERN_ERR "%s%d: encryption negotiated but not an "
1368 + "encrypted packet received\n", __FUNCTION__, state->unit);
1369 + mppe_change_key(state, 0);
1370 + state->flushexpected = 1;
1371 + return DECOMP_ERROR;
1374 + if (state->mppc) { /* no MPPE, only MPPC */
1375 + if ((bits & MPPE_BIT_COMP)) {
1376 + uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1377 + (MPPE_OVHD / 2), obuf, isize -
1378 + PPP_HDRLEN - (MPPE_OVHD / 2),
1379 + osize - (PPP_HDRLEN / 2));
1380 + if (uncomplen == DECOMP_ERROR) {
1381 + state->flushexpected = 1;
1382 + return DECOMP_ERROR;
1384 + uncomplen += PPP_HDRLEN / 2;
1385 + (state->stats).comp_bytes += isize;
1386 + (state->stats).comp_packets++;
1388 + memcpy(obuf, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1389 + PPP_HDRLEN - (MPPE_OVHD / 2));
1390 + uncomplen = isize - MPPE_OVHD;
1391 + (state->stats).inc_bytes += isize;
1392 + (state->stats).inc_packets++;
1394 + } else { /* this shouldn't happen */
1396 + printk(KERN_ERR "%s%d: error - not an MPPC or MPPE frame "
1397 + "received\n", __FUNCTION__, state->unit);
1398 + state->flushexpected = 1;
1399 + return DECOMP_ERROR;
1403 + (state->stats).unc_bytes += uncomplen;
1404 + (state->stats).unc_packets++;
1410 +/************************************************************
1411 + * Module interface table
1412 + ************************************************************/
1414 +/* These are in ppp_generic.c */
1415 +extern int ppp_register_compressor (struct compressor *cp);
1416 +extern void ppp_unregister_compressor (struct compressor *cp);
1419 + * Functions exported to ppp_generic.c.
1421 + * In case of MPPC/MPPE there is no need to process incompressible data
1422 + * because such a data is sent in MPPC/MPPE frame. Therefore the (*incomp)
1423 + * callback function isn't needed.
1425 +struct compressor ppp_mppe = {
1426 + .compress_proto = CI_MPPE,
1427 + .comp_alloc = mppe_comp_alloc,
1428 + .comp_free = mppe_comp_free,
1429 + .comp_init = mppe_comp_init,
1430 + .comp_reset = mppe_comp_reset,
1431 + .compress = mppe_compress,
1432 + .comp_stat = mppe_stats,
1433 + .decomp_alloc = mppe_decomp_alloc,
1434 + .decomp_free = mppe_comp_free,
1435 + .decomp_init = mppe_decomp_init,
1436 + .decomp_reset = mppe_decomp_reset,
1437 + .decompress = mppe_decompress,
1439 + .decomp_stat = mppe_stats,
1440 + .owner = THIS_MODULE
1443 +/************************************************************
1444 + * Module support routines
1445 + ************************************************************/
1447 +int __init mppe_module_init(void)
1451 + if (!(crypto_alg_available("arc4", 0) && crypto_alg_available("sha1", 0))) {
1452 + printk(KERN_ERR "Kernel doesn't provide ARC4 and/or SHA1 algorithms "
1453 + "required by MPPE/MPPC. Check CryptoAPI configuration.\n");
1457 + /* Allocate space for SHAPad1, SHAPad2 and ... */
1458 + sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
1459 + if (sha_pad == NULL)
1461 + /* ... initialize them */
1462 + memset(sha_pad->sha_pad1, 0x00, sizeof(sha_pad->sha_pad1));
1463 + memset(sha_pad->sha_pad2, 0xf2, sizeof(sha_pad->sha_pad2));
1465 + answer = ppp_register_compressor(&ppp_mppe);
1466 + if (answer == 0) {
1467 + printk(KERN_INFO "MPPE/MPPC encryption/compression module registered\n");
1472 +void __exit mppe_module_cleanup(void)
1475 + ppp_unregister_compressor(&ppp_mppe);
1476 + printk(KERN_INFO "MPPE/MPPC encryption/compression module unregistered\n");
1479 +module_init(mppe_module_init);
1480 +module_exit(mppe_module_cleanup);
1482 +MODULE_AUTHOR("Jan Dubiec <jdx@slackware.pl>");
1483 +MODULE_DESCRIPTION("MPPE/MPPC encryption/compression module for Linux");
1484 +MODULE_VERSION("1.2");
1485 +MODULE_LICENSE("Dual BSD/GPL");
1486 +MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
1487 diff -ruN linux-2.6.12.orig/include/linux/ppp-comp.h linux-2.6.12/include/linux/ppp-comp.h
1488 --- linux-2.6.12.orig/include/linux/ppp-comp.h 2004-12-24 22:33:47.000000000 +0100
1489 +++ linux-2.6.12/include/linux/ppp-comp.h 2005-06-28 20:07:01.000000000 +0200
1494 - * ==FILEVERSION 980319==
1495 + * ==FILEVERSION 20040509==
1497 * NOTE TO MAINTAINERS:
1498 * If you modify this file at all, please set the above date.
1501 /* Compress a packet */
1502 int (*compress) (void *state, unsigned char *rptr,
1503 - unsigned char *obuf, int isize, int osize);
1504 + unsigned char *obuf, int isize, int osize);
1506 /* Return compression statistics */
1507 void (*comp_stat) (void *state, struct compstat *stats);
1510 /* Decompress a packet. */
1511 int (*decompress) (void *state, unsigned char *ibuf, int isize,
1512 - unsigned char *obuf, int osize);
1513 + unsigned char *obuf, int osize);
1515 /* Update state for an incompressible packet received */
1516 void (*incomp) (void *state, unsigned char *ibuf, int icnt);
1517 @@ -191,6 +191,42 @@
1518 #define DEFLATE_CHK_SEQUENCE 0
1521 + * Definitions for MPPE/MPPC.
1524 +#define CI_MPPE 18 /* config option for MPPE */
1525 +#define CILEN_MPPE 6 /* length of config option */
1527 +#define MPPE_OVHD 4 /* MPPE overhead */
1528 +#define MPPE_MAX_KEY_LEN 16 /* largest key length (128-bit) */
1530 +#define MPPE_STATELESS 0x01 /* configuration bit H */
1531 +#define MPPE_40BIT 0x20 /* configuration bit L */
1532 +#define MPPE_56BIT 0x80 /* configuration bit M */
1533 +#define MPPE_128BIT 0x40 /* configuration bit S */
1534 +#define MPPE_MPPC 0x01 /* configuration bit C */
1537 + * Definitions for Stac LZS.
1540 +#define CI_LZS 17 /* config option for Stac LZS */
1541 +#define CILEN_LZS 5 /* length of config option */
1543 +#define LZS_OVHD 4 /* max. LZS overhead */
1544 +#define LZS_HIST_LEN 2048 /* LZS history size */
1545 +#define LZS_MAX_CCOUNT 0x0FFF /* max. coherency counter value */
1547 +#define LZS_MODE_NONE 0
1548 +#define LZS_MODE_LCB 1
1549 +#define LZS_MODE_CRC 2
1550 +#define LZS_MODE_SEQ 3
1551 +#define LZS_MODE_EXT 4
1553 +#define LZS_EXT_BIT_FLUSHED 0x80 /* bit A */
1554 +#define LZS_EXT_BIT_COMP 0x20 /* bit C */
1557 * Definitions for other, as yet unsupported, compression methods.