finally move buildroot-ng to trunk
[openwrt.git] / target / linux / generic-2.4 / patches / 106-mppe_mppc.patch
1 MPPE/MPPC kernel module for Linux
2
3 Microsoft Point-to-Point Encryption / Compression support
4
5   http://www.polbox.com/h/hs001/linux-2.4.29-mppe-mppc-1.3.patch.gz
6
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.
13  
14 +Microsoft PPP compression/encryption (MPPC/MPPE)
15 +CONFIG_PPP_MPPE_MPPC
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
22 +  are possible:
23 +       - only compression (MPPC) is used,
24 +       - only encryption (MPPE) is used,
25 +       - compression and encryption (MPPC+MPPE) are used.
26 +
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.
30 +
31 +  For more information please visit http://free.polbox.pl/h/hs001
32 +
33 +  To compile this driver as a module, choose M here. The module will
34 +  be called ppp_mppe_mppc.o.
35 +
36  PPP over Ethernet
37  CONFIG_PPPOE
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
42 @@ -11,7 +11,9 @@
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
51  else
52    bool 'Cryptographic API' CONFIG_CRYPTO
53 @@ -51,11 +53,24 @@
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
59 -  else
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
70 +    else
71 +      if [ "$CONFIG_CRYPTO_SHA1" != "y" -a \
72 +          "$CONFIG_CRYPTO_SHA1" != "m" ]; then
73 +       define_tristate CONFIG_CRYPTO_SHA1 m
74 +      fi
75 +    fi
76    fi
77 +  tristate       '  SHA1 digest algorithm' CONFIG_CRYPTO_SHA1
78 +
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
82 @@ -76,7 +91,20 @@
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
86 +
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
91 +    else
92 +      if [ "$CONFIG_CRYPTO_ARC4" != "y" -a \
93 +          "$CONFIG_CRYPTO_ARC4" != "m" ]; then
94 +       define_tristate CONFIG_CRYPTO_ARC4 m
95 +      fi
96 +    fi
97 +  fi
98    tristate       '  ARC4 cipher algorithm' CONFIG_CRYPTO_ARC4
99 +
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
106 @@ -332,6 +332,7 @@
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
113     fi
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
117 @@ -163,6 +163,7 @@
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
123  
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
128 @@ -19,7 +19,7 @@
129   * PPP driver, written by Michael Callahan and Al Longyear, and
130   * subsequently hacked by Paul Mackerras.
131   *
132 - * ==FILEVERSION 20020217==
133 + * ==FILEVERSION 20040509==
134   */
135  
136  #include <linux/config.h>
137 @@ -102,6 +102,7 @@
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 */
145 @@ -552,7 +553,9 @@
146         case PPPIOCSMRU:
147                 if (get_user(val, (int *) arg))
148                         break;
149 -               ppp->mru = val;
150 +               ppp->mru_alloc = ppp->mru = val;
151 +               if (ppp->mru_alloc < PPP_MRU)
152 +                   ppp->mru_alloc = PPP_MRU;   /* increase for broken peers */
153                 err = 0;
154                 break;
155  
156 @@ -1025,14 +1028,37 @@
157         case PPP_CCP:
158                 /* peek at outbound CCP frames */
159                 ppp_ccp_peek(ppp, skb, 0);
160 +               /*
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
164 +                */
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;
170 +                   kfree_skb(skb);
171 +                   return;
172 +               }
173                 break;
174         }
175  
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,
180 -                                   GFP_ATOMIC);
181 +               int comp_ovhd = 0;
182 +               /* 
183 +                * because of possible data expansion when MPPC or LZS
184 +                * is used, allocate compressor's buffer 12.5% bigger
185 +                * than MTU
186 +                */
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);
193                 if (new_skb == 0) {
194                         printk(KERN_ERR "PPP: no memory (comp pkt)\n");
195                         goto drop;
196 @@ -1050,9 +1076,21 @@
197                         skb = new_skb;
198                         skb_put(skb, len);
199                         skb_pull(skb, 2);       /* pull off A/C bytes */
200 -               } else {
201 +               } else if (len == 0) {
202                         /* didn't compress, or CCP not up yet */
203                         kfree_skb(new_skb);
204 +               } else {
205 +                       /*
206 +                        * (len < 0)
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
211 +                        * the same number.
212 +                        */
213 +                       printk(KERN_ERR "ppp: compressor dropped pkt\n");
214 +                       kfree_skb(new_skb);
215 +                       goto drop;
216                 }
217         }
218  
219 @@ -1540,14 +1578,15 @@
220         int len;
221  
222         if (proto == PPP_COMP) {
223 -               ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
224 +               ns = dev_alloc_skb(ppp->mru_alloc + PPP_HDRLEN);
225                 if (ns == 0) {
226                         printk(KERN_ERR "ppp_decompress_frame: no memory\n");
227                         goto err;
228                 }
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);
234                 if (len < 0) {
235                         /* Pass the compressed frame to pppd as an
236                            error indication. */
237 @@ -1573,7 +1612,14 @@
238         return skb;
239  
240   err:
241 -       ppp->rstate |= SC_DC_ERROR;
242 +       if (ppp->rcomp->compress_proto != CI_MPPE
243 +           && ppp->rcomp->compress_proto != CI_LZS) {
244 +           /*
245 +            * If decompression protocol isn't MPPE/MPPC or LZS, we set
246 +            * SC_DC_ERROR flag and wait for CCP_RESETACK
247 +            */
248 +           ppp->rstate |= SC_DC_ERROR;
249 +       }
250         ppp_receive_error(ppp);
251         return skb;
252  }
253 @@ -2253,6 +2299,7 @@
254         /* Initialize the new ppp unit */
255         ppp->file.index = unit;
256         ppp->mru = PPP_MRU;
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
264 @@ -0,0 +1,1312 @@
265 +/*
266 + * ppp_mppe_mppc.c - MPPC/MPPE "compressor/decompressor" module.
267 + *
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>
271 + * 
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.
276 + *
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.
280 + *
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.
283 + *
284 + *  ==FILEVERSION 20050202==
285 + *
286 + */
287 +
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>
295 +
296 +#include <linux/ppp_defs.h>
297 +#include <linux/ppp-comp.h>
298 +
299 +/*
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
302 + * by linux/mm.h.
303 + */
304 +#define offset_in_page(p)      ((unsigned long)(p) & ~PAGE_MASK)
305 +
306 +/*
307 + * State for a mppc/mppe "(de)compressor".
308 + */
309 +struct ppp_mppe_state {
310 +    struct crypto_tfm *arc4_tfm;
311 +    struct crypto_tfm *sha1_tfm;
312 +    u8         *sha1_digest;
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" */
329 +    int                unit;
330 +    int                debug;
331 +    int                mru;
332 +    struct compstat stats;
333 +};
334 +
335 +#define MPPE_HIST_LEN          8192    /* MPPC history size */
336 +#define MPPE_MAX_CCOUNT                0x0FFF  /* max. coherency counter value */
337 +
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 */
342 +
343 +#define MPPE_SALT0             0xD1    /* values used in MPPE key derivation */
344 +#define MPPE_SALT1             0x26    /* according to RFC3079 */
345 +#define MPPE_SALT2             0x9E
346 +
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)
351 +
352 +/*
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.
357 + */
358 +#define SHA1_PAD_SIZE          40
359 +struct sha_pad {
360 +    unsigned char sha_pad1[SHA1_PAD_SIZE];
361 +    unsigned char sha_pad2[SHA1_PAD_SIZE];
362 +};
363 +static struct sha_pad *sha_pad;
364 +
365 +static inline void
366 +setup_sg(struct scatterlist *sg, const void  *address, unsigned int length)
367 +{
368 +    sg[0].page = virt_to_page(address);
369 +    sg[0].offset = offset_in_page(address);
370 +    sg[0].length = length;
371 +}
372 +
373 +static inline void
374 +arc4_setkey(struct ppp_mppe_state *state, const unsigned char *key,
375 +           const unsigned int keylen)
376 +{
377 +    crypto_cipher_setkey(state->arc4_tfm, key, keylen);
378 +}
379 +
380 +static inline void
381 +arc4_encrypt(struct ppp_mppe_state *state, const unsigned char *in,
382 +            const unsigned int len, unsigned char *out)
383 +{
384 +    struct scatterlist sgin[4], sgout[4];
385 +
386 +    setup_sg(sgin, in, len);
387 +    setup_sg(sgout, out, len);
388 +    crypto_cipher_encrypt(state->arc4_tfm, sgout, sgin, len);
389 +}
390 +
391 +#define arc4_decrypt arc4_encrypt
392 +
393 +/*
394 + * Key Derivation, from RFC 3078, RFC 3079.
395 + * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
396 + */
397 +static void
398 +get_new_key_from_sha(struct ppp_mppe_state *state, unsigned char *interim_key)
399 +{
400 +    struct scatterlist sg[4];
401 +
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));
406 +
407 +    crypto_digest_digest (state->sha1_tfm, sg, 4, state->sha1_digest);
408 +
409 +    memcpy(interim_key, state->sha1_digest, state->keylen);
410 +}
411 +
412 +static void
413 +mppe_change_key(struct ppp_mppe_state *state, int initialize)
414 +{
415 +    unsigned char interim_key[MPPE_MAX_KEY_LEN];
416 +
417 +    get_new_key_from_sha(state, interim_key);
418 +    if (initialize) {
419 +       memcpy(state->session_key, interim_key, state->keylen);
420 +    } else {
421 +       arc4_setkey(state, interim_key, state->keylen);
422 +       arc4_encrypt(state, interim_key, state->keylen, state->session_key);
423 +    }
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;
429 +       } else {
430 +           state->session_key[0] = MPPE_SALT0;
431 +       }
432 +    }
433 +    arc4_setkey(state, state->session_key, state->keylen);
434 +}
435 +
436 +/* increase 12-bit coherency counter */
437 +static inline void
438 +mppe_increase_ccount(struct ppp_mppe_state *state)
439 +{
440 +    state->ccount = (state->ccount + 1) & MPPE_MAX_CCOUNT;
441 +    if (state->mppe) {
442 +       if (state->stateless) {
443 +           mppe_change_key(state, 0);
444 +           state->nextflushed = 1;
445 +       } else {
446 +           if ((state->ccount & 0xff) == 0xff) {
447 +               mppe_change_key(state, 0);
448 +           }
449 +       }
450 +    }
451 +}
452 +
453 +/* allocate space for a MPPE/MPPC (de)compressor.  */
454 +/*   comp != 0 -> init compressor */
455 +/*   comp = 0 -> init decompressor */
456 +static void *
457 +mppe_alloc(unsigned char *options, int opt_len, int comp)
458 +{
459 +    struct ppp_mppe_state *state;
460 +    unsigned int digestsize;
461 +    u8* fname;
462 +
463 +    fname = comp ? "mppe_comp_alloc" : "mppe_decomp_alloc";
464 +
465 +    /*  
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.
471 +     */
472 +    if (opt_len < CILEN_MPPE) {
473 +       printk(KERN_WARNING "%s: wrong options length: %u\n", fname, opt_len);
474 +       return NULL;
475 +    }
476 +
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]);
485 +       return NULL;
486 +    }
487 +
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,
491 +              comp ? "" : "de");
492 +       return NULL;
493 +    }
494 +    memset(state, 0, sizeof(struct ppp_mppe_state));
495 +
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? */
499 +
500 +    if (state->mppc) {
501 +       /* allocate MPPC history */
502 +       state->hist = (u8*)vmalloc(2*MPPE_HIST_LEN*sizeof(u8));
503 +       if (state->hist == NULL) {
504 +           kfree(state);
505 +           printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
506 +                  fname);
507 +           return NULL;
508 +       }
509 +
510 +       /* allocate hashtable for MPPC compressor */
511 +       if (comp) {
512 +           state->hash = (u16*)vmalloc(MPPE_HIST_LEN*sizeof(u16));
513 +           if (state->hash == NULL) {
514 +               vfree(state->hist);
515 +               kfree(state);
516 +               printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
517 +                      fname);
518 +               return NULL;
519 +           }
520 +       }
521 +    }
522 +
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) {
527 +           if (state->mppc) {
528 +               vfree(state->hash);
529 +               if (comp)
530 +                   vfree(state->hist);
531 +           }
532 +           kfree(state);
533 +           printk(KERN_ERR "%s: cannot load ARC4 module\n", fname);
534 +           return NULL;
535 +       }
536 +
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);
541 +           if (state->mppc) {
542 +               vfree(state->hash);
543 +               if (comp)
544 +                   vfree(state->hist);
545 +           }
546 +           kfree(state);
547 +           printk(KERN_ERR "%s: cannot load SHA1 module\n", fname);
548 +           return NULL;
549 +       }
550 +
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);
555 +           if (state->mppc) {
556 +               vfree(state->hash);
557 +               if (comp)
558 +                   vfree(state->hist);
559 +           }
560 +           kfree(state);
561 +           printk(KERN_ERR "%s: CryptoAPI SHA1 digest size too small\n", fname);
562 +       }
563 +
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);
568 +           if (state->mppc) {
569 +               vfree(state->hash);
570 +               if (comp)
571 +                   vfree(state->hist);
572 +           }
573 +           kfree(state);
574 +           printk(KERN_ERR "%s: cannot allocate space for SHA1 digest\n", fname);
575 +       }
576 +
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() */
580 +    }
581 +
582 +    MOD_INC_USE_COUNT;
583 +
584 +    return (void *) state;
585 +}
586 +
587 +static void *
588 +mppe_comp_alloc(unsigned char *options, int opt_len)
589 +{
590 +    return mppe_alloc(options, opt_len, 1);
591 +}
592 +
593 +static void *
594 +mppe_decomp_alloc(unsigned char *options, int opt_len)
595 +{
596 +    return mppe_alloc(options, opt_len, 0);
597 +}
598 +
599 +/* cleanup the (de)compressor */
600 +static void
601 +mppe_comp_free(void *arg)
602 +{
603 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
604 +
605 +    if (state != NULL) {
606 +       if (state->mppe) {
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);
611 +       }
612 +       if (state->hist != NULL)
613 +           vfree(state->hist);
614 +       if (state->hash != NULL)
615 +           vfree(state->hash);
616 +       kfree(state);
617 +    }
618 +
619 +    MOD_DEC_USE_COUNT;
620 +}
621 +
622 +/* init MPPC/MPPE (de)compresor */
623 +/*   comp != 0 -> init compressor */
624 +/*   comp = 0 -> init decompressor */
625 +static int
626 +mppe_init(void *arg, unsigned char *options, int opt_len, int unit,
627 +         int hdrlen, int mru, int debug, int comp)
628 +{
629 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
630 +    u8* fname;
631 +
632 +    fname = comp ? "mppe_comp_init" : "mppe_decomp_init";
633 +
634 +    if (opt_len < CILEN_MPPE) {
635 +       if (debug)
636 +           printk(KERN_WARNING "%s: wrong options length: %u\n",
637 +                  fname, opt_len);
638 +       return 0;
639 +    }
640 +
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) {
646 +       if (debug)
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],
650 +                  options[5]);
651 +       return 0;
652 +    }
653 +
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) {
658 +       if (debug)
659 +           printk(KERN_WARNING "%s: don't know what to do: o[5]=%02x\n",
660 +                  fname, options[5]);
661 +       return 0;
662 +    }
663 +
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? */
668 +
669 +    switch (state->mppe) {
670 +    case MPPE_40BIT:     /* 40 bit key */
671 +       state->keylen = 8;
672 +       state->bitkeylen = 40;
673 +       break;
674 +    case MPPE_56BIT:     /* 56 bit key */
675 +       state->keylen = 8;
676 +       state->bitkeylen = 56;
677 +       break;
678 +    case MPPE_128BIT:    /* 128 bit key */
679 +       state->keylen = 16;
680 +       state->bitkeylen = 128;
681 +       break;
682 +    default:
683 +       state->keylen = 0;
684 +       state->bitkeylen = 0;
685 +    }
686 +
687 +    state->ccount = MPPE_MAX_CCOUNT;
688 +    state->bits = 0;
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));
694 +    }
695 +
696 +    if (state->mppe) { /* generate initial session keys */
697 +       mppe_change_key(state, 1);
698 +    }
699 +
700 +    if (comp) { /* specific for compressor */
701 +       state->nextflushed = 1;
702 +    } else { /* specific for decompressor */
703 +       state->mru = mru;
704 +       state->flushexpected = 1;
705 +    }
706 +
707 +    return 1;
708 +}
709 +
710 +static int
711 +mppe_comp_init(void *arg, unsigned char *options, int opt_len, int unit,
712 +              int hdrlen, int debug)
713 +{
714 +    return mppe_init(arg, options, opt_len, unit, hdrlen, 0, debug, 1);
715 +}
716 +
717 +
718 +static int
719 +mppe_decomp_init(void *arg, unsigned char *options, int opt_len, int unit,
720 +                int hdrlen, int mru, int debug)
721 +{
722 +    return mppe_init(arg, options, opt_len, unit, hdrlen, mru, debug, 0);
723 +}
724 +
725 +static void
726 +mppe_comp_reset(void *arg)
727 +{
728 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
729 +
730 +    if (state->debug)
731 +       printk(KERN_DEBUG "%s%d: resetting MPPC/MPPE compressor\n",
732 +              __FUNCTION__, state->unit);
733 +
734 +    state->nextflushed = 1;
735 +    if (state->mppe)
736 +       arc4_setkey(state, state->session_key, state->keylen);
737 +}
738 +
739 +static void
740 +mppe_decomp_reset(void *arg)
741 +{
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. */
744 +    return;
745 +}
746 +
747 +static void
748 +mppe_stats(void *arg, struct compstat *stats)
749 +{
750 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
751 +
752 +    *stats = state->stats;
753 +}
754 +
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)
760 +{
761 +    buf += *i;
762 +    if (*l >= n) {
763 +       *l = (*l) - n;
764 +       val <<= *l;
765 +       *buf = *buf | (val & 0xff);
766 +       if (*l == 0) {
767 +           *l = 8;
768 +           (*i)++;
769 +           *(++buf) = 0;
770 +       }
771 +    } else {
772 +       (*i)++;
773 +       *l = 8 - n + (*l);
774 +       val <<= *l;
775 +       *buf = *buf | ((val >> 8) & 0xff);
776 +       *(++buf) = val & 0xff;
777 +    }
778 +}
779 +
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)
782 +{
783 +    buf += *i;
784 +    if (*l >= n - 8) {
785 +       (*i)++;
786 +       *l = 8 - n + (*l);
787 +       val <<= *l;
788 +       *buf = *buf | ((val >> 8) & 0xff);
789 +       *(++buf) = val & 0xff;
790 +       if (*l == 0) {
791 +           *l = 8;
792 +           (*i)++;
793 +           *(++buf) = 0;
794 +       }
795 +    } else {
796 +       (*i)++; (*i)++;
797 +       *l = 16 - n + (*l);
798 +       val <<= *l;
799 +       *buf = *buf | ((val >> 16) & 0xff);
800 +       *(++buf) = (val >> 8) & 0xff;
801 +       *(++buf) = val & 0xff;
802 +    }
803 +}
804 +
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)
807 +{
808 +    buf += *i;
809 +    if (*l >= n - 16) {
810 +       (*i)++; (*i)++;
811 +       *l = 16 - n + (*l);
812 +       val <<= *l;
813 +       *buf = *buf | ((val >> 16) & 0xff);
814 +       *(++buf) = (val >> 8) & 0xff;
815 +       *(++buf) = val & 0xff;
816 +       if (*l == 0) {
817 +           *l = 8;
818 +           (*i)++;
819 +           *(++buf) = 0;
820 +       }
821 +    } else {
822 +       (*i)++; (*i)++; (*i)++;
823 +       *l = 24 - n + (*l);
824 +       val <<= *l;
825 +       *buf = *buf | ((val >> 24) & 0xff);
826 +       *(++buf) = (val >> 16) & 0xff;
827 +       *(++buf) = (val >> 8) & 0xff;
828 +       *(++buf) = val & 0xff;
829 +    }
830 +}
831 +
832 +static int
833 +mppc_compress(struct ppp_mppe_state *state, unsigned char *ibuf,
834 +             unsigned char *obuf, int isize, int osize)
835 +{
836 +    u32 olen, off, len, idx, i, l;
837 +    u8 *hist, *sbuf, *p, *q, *r, *s;
838 +
839 +    /*  
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.
844 +
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.
849 +    */
850 +
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);
857 +    }
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;
862 +
863 +    /* compress data */
864 +    r = sbuf + isize;
865 +    *obuf = olen = i = 0;
866 +    l = 8;
867 +    while (i < isize - 2) {
868 +       s = q = sbuf + i;
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);
872 +       off = s - p;
873 +       if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ || *p++ != *s++ ||
874 +           *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);
880 +           }
881 +           ++i;
882 +           continue;
883 +       }
884 +       if (r - q >= 64) {
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++ ||
900 +           *p++ != *s++;
901 +           if (s - q == 64) {
902 +               p--; s--;
903 +               while((*p++ == *s++) && (s < r) && (p < q));
904 +           }
905 +       } else {
906 +           while((*p++ == *s++) && (s < r) && (p < q));
907 +       }
908 +       len = s - q - 1;
909 +       i += len;
910 +
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);
919 +       } else {
920 +           /* This shouldn't happen; we return 0 what means "packet expands",
921 +           and we send packet uncompressed. */
922 +           if (state->debug)
923 +               printk(KERN_DEBUG "%s%d: wrong offset value: %d\n",
924 +                      __FUNCTION__, state->unit, off);
925 +           return 0;
926 +       }
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);
952 +       } else {
953 +           /* This shouldn't happen; we return 0 what means "packet expands",
954 +           and send packet uncompressed. */
955 +           if (state->debug)
956 +               printk(KERN_DEBUG "%s%d: wrong length of match value: %d\n",
957 +                      __FUNCTION__, state->unit, len);
958 +           return 0;
959 +       }
960 +    }
961 +
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);
968 +       }
969 +    }
970 +    /* Reset unused bits of the last output octet */
971 +    if ((l != 0) && (l != 8)) {
972 +       putbits8(obuf, 0, l, &olen, &l);
973 +    }
974 +
975 +    return (int) olen;
976 +}
977 +
978 +int
979 +mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
980 +             int isize, int osize)
981 +{
982 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
983 +    int proto, olen, complen, off;
984 +    unsigned char *wptr;
985 +
986 +    /* Check that the protocol is in the range we handle. */
987 +    proto = PPP_PROTOCOL(ibuf);
988 +    if (proto < 0x0021 || proto > 0x00fa)
989 +       return 0;
990 +
991 +    wptr = obuf;
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 */
998 +
999 +    /* 
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.
1007 +     *
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.
1011 +     */
1012 +    //off = (proto > 0xff) ? 2 : 3; /* PFC - skip first protocol byte if 0 */
1013 +    off = 2;
1014 +
1015 +    ibuf += off;
1016 +
1017 +    mppe_increase_ccount(state);
1018 +
1019 +    if (state->nextflushed) {
1020 +       state->bits |= MPPE_BIT_FLUSHED;
1021 +       state->nextflushed = 0;
1022 +       if (state->mppe && !state->stateless) {
1023 +           /*
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.
1026 +            */
1027 +           if ((state->ccount & 0xff) != 0xff) {
1028 +               arc4_setkey(state, state->session_key, state->keylen);
1029 +           }
1030 +       }
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));
1035 +       }
1036 +    }
1037 +
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));
1041 +       /*
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.
1047 +        */
1048 +       if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
1049 +           (complen == 0)) {
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++;
1056 +       } else {
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++;
1061 +       }
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));
1073 +           /*
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.
1079 +            */
1080 +           if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
1081 +               (complen == 0)) {
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++;
1088 +           } else {
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++;
1102 +           }
1103 +       }
1104 +    }
1105 +
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);
1110 +
1111 +    state->bits = 0;
1112 +
1113 +    (state->stats).unc_bytes += isize;
1114 +    (state->stats).unc_packets++;
1115 +
1116 +    return olen;
1117 +}
1118 +
1119 +/***************************/
1120 +/*** Decompression stuff ***/
1121 +/***************************/
1122 +static inline u32 getbits(const u8 *buf, const u32 n, u32 *i, u32 *l)
1123 +{
1124 +    static const u32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1125 +    u32 res, ol;
1126 +
1127 +    ol = *l;
1128 +    if (*l >= n) {
1129 +       *l = (*l) - n;
1130 +       res = (buf[*i] & m[ol]) >> (*l);
1131 +       if (*l == 0) {
1132 +           *l = 8;
1133 +           (*i)++;
1134 +       }
1135 +    } else {
1136 +       *l = 8 - n + (*l);
1137 +       res = (buf[(*i)++] & m[ol]) << 8;
1138 +       res = (res | buf[*i]) >> (*l);
1139 +    }
1140 +
1141 +    return res;
1142 +}
1143 +
1144 +static inline u32 getbyte(const u8 *buf, const u32 i, const u32 l)
1145 +{
1146 +    if (l == 8) {
1147 +       return buf[i];
1148 +    } else {
1149 +       return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff;
1150 +    }
1151 +}
1152 +
1153 +static inline void lamecopy(u8 *dst, u8 *src, u32 len)
1154 +{
1155 +    while (len--)
1156 +       *dst++ = *src++;
1157 +}
1158 +
1159 +static int
1160 +mppc_decompress(struct ppp_mppe_state *state, unsigned char *ibuf,
1161 +               unsigned char *obuf, int isize, int osize)
1162 +{
1163 +    u32 olen, off, len, bits, val, sig, i, l;
1164 +    u8 *history, *s;
1165 +
1166 +    history = state->hist + state->histptr;
1167 +    olen = len = i = 0;
1168 +    l = 8;
1169 +    bits = isize * 8;
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;
1176 +           } else {
1177 +               /* buffer overflow; drop packet */
1178 +               if (state->debug)
1179 +                   printk(KERN_ERR "%s%d: trying to write outside history "
1180 +                          "buffer\n", __FUNCTION__, state->unit);
1181 +               return DECOMP_ERROR;
1182 +           }
1183 +           olen++;
1184 +           bits -= 8;
1185 +           continue;
1186 +       }
1187 +
1188 +       sig = val & 0xc0;
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));
1194 +           } else {
1195 +               /* buffer overflow; drop packet */
1196 +               if (state->debug)
1197 +                   printk(KERN_ERR "%s%d: trying to write outside history "
1198 +                          "buffer\n", __FUNCTION__, state->unit);
1199 +               return DECOMP_ERROR;
1200 +           }
1201 +           olen++;
1202 +           bits -= 9;
1203 +           continue;
1204 +       }
1205 +
1206 +       /* Not a literal byte so it must be an (offset,length) pair */
1207 +       /* decode offset */
1208 +       sig = val & 0xf0;
1209 +       if (sig == 0xf0) {              /* 10-bit offset; 0 <= offset < 64 */
1210 +           off = (((val&0x0f)<<2)|getbits(ibuf, 2 , &i ,&l));
1211 +           bits -= 10;
1212 +       } else {
1213 +           if (sig == 0xe0) {          /* 12-bit offset; 64 <= offset < 320 */
1214 +               off = ((((val&0x0f)<<4)|getbits(ibuf, 4 , &i ,&l))+64);
1215 +               bits -= 12;
1216 +           } else {
1217 +               if ((sig&0xe0) == 0xc0) {/* 16-bit offset; 320 <= offset < 8192 */
1218 +                   off = ((((val&0x1f)<<8)|getbyte(ibuf, i++, l))+320);
1219 +                   bits -= 16;
1220 +                   if (off > MPPE_HIST_LEN - 1) {
1221 +                       if (state->debug)
1222 +                           printk(KERN_DEBUG "%s%d: too big offset value: %d\n",
1223 +                                  __FUNCTION__, state->unit, off);
1224 +                       return DECOMP_ERROR;
1225 +                   }
1226 +               } else {                /* this shouldn't happen */
1227 +                   if (state->debug)
1228 +                       printk(KERN_DEBUG "%s%d: cannot decode offset value\n",
1229 +                              __FUNCTION__, state->unit);
1230 +                   return DECOMP_ERROR;
1231 +               }
1232 +           }
1233 +       }
1234 +       /* decode length of match */
1235 +       val = getbyte(ibuf, i, l);
1236 +       if ((val & 0x80) == 0x00) {                     /* len = 3 */
1237 +           len = 3;
1238 +           bits--;
1239 +           getbits(ibuf, 1 , &i ,&l);
1240 +       } else if ((val & 0xc0) == 0x80) {              /* 4 <= len < 8 */
1241 +           len = 0x04 | ((val>>4) & 0x03);
1242 +           bits -= 4;
1243 +           getbits(ibuf, 4 , &i ,&l);
1244 +       } else if ((val & 0xe0) == 0xc0) {              /* 8 <= len < 16 */
1245 +           len = 0x08 | ((val>>2) & 0x07);
1246 +           bits -= 6;
1247 +           getbits(ibuf, 6 , &i ,&l);
1248 +       } else if ((val & 0xf0) == 0xe0) {              /* 16 <= len < 32 */
1249 +           len = 0x10 | (val & 0x0f);
1250 +           bits -= 8;
1251 +           i++;
1252 +       } else {
1253 +           bits -= 8;
1254 +           val = (val << 8) | getbyte(ibuf, ++i, l);
1255 +           if ((val & 0xf800) == 0xf000) {             /* 32 <= len < 64 */
1256 +               len = 0x0020 | ((val >> 6) & 0x001f);
1257 +               bits -= 2;
1258 +               getbits(ibuf, 2 , &i ,&l);
1259 +           } else if ((val & 0xfc00) == 0xf800) {      /* 64 <= len < 128 */
1260 +               len = 0x0040 | ((val >> 4) & 0x003f);
1261 +               bits -= 4;
1262 +               getbits(ibuf, 4 , &i ,&l);
1263 +           } else if ((val & 0xfe00) == 0xfc00) {      /* 128 <= len < 256 */
1264 +               len = 0x0080 | ((val >> 2) & 0x007f);
1265 +               bits -= 6;
1266 +               getbits(ibuf, 6 , &i ,&l);
1267 +           } else if ((val & 0xff00) == 0xfe00) {      /* 256 <= len < 512 */
1268 +               len = 0x0100 | (val & 0x00ff);
1269 +               bits -= 8;
1270 +               i++;
1271 +           } else {
1272 +               bits -= 8;
1273 +               val = (val << 8) | getbyte(ibuf, ++i, l);
1274 +               if ((val & 0xff8000) == 0xff0000) {     /* 512 <= len < 1024 */
1275 +                   len = 0x000200 | ((val >> 6) & 0x0001ff);
1276 +                   bits -= 2;
1277 +                   getbits(ibuf, 2 , &i ,&l);
1278 +               } else if ((val & 0xffc000) == 0xff8000) {/* 1024 <= len < 2048 */
1279 +                   len = 0x000400 | ((val >> 4) & 0x0003ff);
1280 +                   bits -= 4;
1281 +                   getbits(ibuf, 4 , &i ,&l);
1282 +               } else if ((val & 0xffe000) == 0xffc000) {/* 2048 <= len < 4096 */
1283 +                   len = 0x000800 | ((val >> 2) & 0x0007ff);
1284 +                   bits -= 6;
1285 +                   getbits(ibuf, 6 , &i ,&l);
1286 +               } else if ((val & 0xfff000) == 0xffe000) {/* 4096 <= len < 8192 */
1287 +                   len = 0x001000 | (val & 0x000fff);
1288 +                   bits -= 8;
1289 +                   i++;
1290 +               } else {                                /* this shouldn't happen */
1291 +                   if (state->debug)
1292 +                       printk(KERN_DEBUG "%s%d: wrong length code: 0x%X\n",
1293 +                              __FUNCTION__, state->unit, val);
1294 +                   return DECOMP_ERROR;
1295 +               }
1296 +           }
1297 +       }
1298 +       s = state->hist + state->histptr;
1299 +       state->histptr += len;
1300 +       olen += len;
1301 +       if (state->histptr < 2*MPPE_HIST_LEN) {
1302 +           /* copy uncompressed bytes to the history */
1303 +
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.
1309 +            */
1310 +           lamecopy(s, s - off, len);
1311 +       } else {
1312 +           /* buffer overflow; drop packet */
1313 +           if (state->debug)
1314 +               printk(KERN_ERR "%s%d: trying to write outside history "
1315 +                      "buffer\n", __FUNCTION__, state->unit);
1316 +           return DECOMP_ERROR;
1317 +       }
1318 +    }
1319 +
1320 +    /* Do PFC decompression */
1321 +    len = olen;
1322 +    if ((history[0] & 0x01) != 0) {
1323 +       obuf[0] = 0;
1324 +       obuf++;
1325 +       len++;
1326 +    }
1327 +
1328 +    if (len <= osize) {
1329 +       /* copy uncompressed packet to the output buffer */
1330 +       memcpy(obuf, history, olen);
1331 +    } else {
1332 +       /* buffer overflow; drop packet */
1333 +       if (state->debug)
1334 +           printk(KERN_ERR "%s%d: too big uncompressed packet: %d\n",
1335 +                  __FUNCTION__, state->unit, len + (PPP_HDRLEN / 2));
1336 +       return DECOMP_ERROR;
1337 +    }
1338 +
1339 +    return (int) len;
1340 +}
1341 +
1342 +int
1343 +mppe_decompress(void *arg, unsigned char *ibuf, int isize,
1344 +               unsigned char *obuf, int osize)
1345 +{
1346 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
1347 +    int seq, bits, uncomplen;
1348 +
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);
1353 +       }
1354 +       return DECOMP_ERROR;
1355 +    }
1356 +
1357 +    /* Get coherency counter and control bits from input buffer */
1358 +    seq = MPPE_CCOUNT(ibuf);
1359 +    bits = MPPE_BITS(ibuf);
1360 +
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);
1369 +    } else {
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. */
1382 +               if (state->debug)
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;
1387 +           }
1388 +       }
1389 +       if (state->mppe && (bits & MPPE_BIT_FLUSHED)) {
1390 +           arc4_setkey(state, state->session_key, state->keylen);
1391 +       }
1392 +    }
1393 +
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));
1398 +       } else
1399 +           if ((bits & MPPE_BIT_RESET)) {
1400 +               memcpy(state->hist, state->hist + MPPE_HIST_LEN, MPPE_HIST_LEN);
1401 +           }
1402 +    }
1403 +
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;
1409 +
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 +
1421 +                            (MPPE_OVHD / 2));
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;
1429 +               }
1430 +               uncomplen += PPP_HDRLEN / 2;
1431 +               (state->stats).comp_bytes += isize;
1432 +               (state->stats).comp_packets++;
1433 +           } else {
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];
1441 +                   obuf[0] = 0;
1442 +                   obuf++;
1443 +                   uncomplen++;
1444 +               }
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++;
1450 +           }
1451 +       } else { /* this shouldn't happen */
1452 +           if (state->debug)
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;
1458 +       }
1459 +    } else {
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;
1469 +               }
1470 +               uncomplen += PPP_HDRLEN / 2;
1471 +               (state->stats).comp_bytes += isize;
1472 +               (state->stats).comp_packets++;
1473 +           } else {
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++;
1479 +           }
1480 +       } else { /* this shouldn't happen */
1481 +           if (state->debug)
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;
1486 +       }
1487 +    }
1488 +
1489 +    (state->stats).unc_bytes += uncomplen;
1490 +    (state->stats).unc_packets++;
1491 +
1492 +    return uncomplen;
1493 +}
1494 +
1495 +
1496 +/************************************************************
1497 + * Module interface table
1498 + ************************************************************/
1499 +
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);
1503 +
1504 +/*
1505 + * Functions exported to ppp_generic.c.
1506 + *
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.
1510 + */
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 */
1526 +};
1527 +
1528 +/************************************************************
1529 + * Module support routines
1530 + ************************************************************/
1531 +
1532 +int __init mppe_module_init(void)
1533 +{
1534 +    int answer;
1535 +
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.
1541 +     *
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");
1545 +       return -ENODEV;
1546 +    }
1547 +    */
1548 +
1549 +    /* Allocate space for SHAPad1, SHAPad2 and ... */
1550 +    sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
1551 +    if (sha_pad == NULL)
1552 +       return -ENOMEM;
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));
1556 +
1557 +    answer = ppp_register_compressor(&ppp_mppe);
1558 +    if (answer == 0) {
1559 +       printk(KERN_INFO "MPPE/MPPC encryption/compression module registered\n");
1560 +    }
1561 +    return answer;
1562 +}
1563 +
1564 +void __exit mppe_module_cleanup(void)
1565 +{
1566 +    kfree(sha_pad);
1567 +    ppp_unregister_compressor(&ppp_mppe);
1568 +    printk(KERN_INFO "MPPE/MPPC encryption/compression module unregistered\n");
1569 +}
1570 +
1571 +module_init(mppe_module_init);
1572 +module_exit(mppe_module_cleanup);
1573 +
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
1580 @@ -28,7 +28,7 @@
1581   */
1582  
1583  /*
1584 - *  ==FILEVERSION 980319==
1585 + *  ==FILEVERSION 20040509==
1586   *
1587   *  NOTE TO MAINTAINERS:
1588   *     If you modify this file at all, please set the above date.
1589 @@ -78,7 +78,7 @@
1590  
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);
1595  
1596         /* Return compression statistics */
1597         void    (*comp_stat) (void *state, struct compstat *stats);
1598 @@ -99,7 +99,7 @@
1599  
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);
1604  
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
1609  
1610  /*
1611 + * Definitions for MPPE/MPPC.
1612 + */
1613 +
1614 +#define CI_MPPE                        18      /* config option for MPPE */
1615 +#define CILEN_MPPE             6       /* length of config option */
1616 +
1617 +#define MPPE_OVHD              4       /* MPPE overhead */
1618 +#define MPPE_MAX_KEY_LEN       16      /* largest key length (128-bit) */
1619 +
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 */
1625 +
1626 +/*
1627 + * Definitions for Stac LZS.
1628 + */
1629 +
1630 +#define CI_LZS                 17      /* config option for Stac LZS */
1631 +#define CILEN_LZS              5       /* length of config option */
1632 +
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 */
1636 +
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
1642 +
1643 +#define LZS_EXT_BIT_FLUSHED    0x80    /* bit A */
1644 +#define LZS_EXT_BIT_COMP       0x20    /* bit C */
1645 +
1646 +/*
1647   * Definitions for other, as yet unsupported, compression methods.
1648   */
1649