[kernel] refresh generic-2.4 patches
[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 Index: linux-2.4.35.4/Documentation/Configure.help
8 ===================================================================
9 --- linux-2.4.35.4.orig/Documentation/Configure.help
10 +++ linux-2.4.35.4/Documentation/Configure.help
11 @@ -9974,6 +9974,28 @@ CONFIG_PPP_BSDCOMP
12    module; it is called bsd_comp.o and will show up in the directory
13    modules once you have said "make modules". If unsure, say N.
14  
15 +Microsoft PPP compression/encryption (MPPC/MPPE)
16 +CONFIG_PPP_MPPE_MPPC
17 +  Support for the Microsoft Point-To-Point Compression (RFC2118) and 
18 +  Microsoft Point-To-Point Encryption (RFC3078). These protocols are
19 +  supported by Microsoft Windows and wide range of "hardware" access
20 +  servers. MPPE is common protocol in Virtual Private Networks. According
21 +  to RFC3078, MPPE supports 40, 56 and 128-bit key lengths. Depending on
22 +  PPP daemon configuration on both ends of the link, following scenarios
23 +  are possible:
24 +       - only compression (MPPC) is used,
25 +       - only encryption (MPPE) is used,
26 +       - compression and encryption (MPPC+MPPE) are used.
27 +
28 +  Please note that Hi/Fn (http://www.hifn.com) holds patent on MPPC so
29 +  you should check if this patent is valid in your country in order to
30 +  avoid legal problems.
31 +
32 +  For more information please visit http://free.polbox.pl/h/hs001
33 +
34 +  To compile this driver as a module, choose M here. The module will
35 +  be called ppp_mppe_mppc.o.
36 +
37  PPP over Ethernet
38  CONFIG_PPPOE
39    Support for PPP over Ethernet.
40 Index: linux-2.4.35.4/crypto/Config.in
41 ===================================================================
42 --- linux-2.4.35.4.orig/crypto/Config.in
43 +++ linux-2.4.35.4/crypto/Config.in
44 @@ -11,7 +11,9 @@ if [ "$CONFIG_INET_AH" = "y" -o \
45       "$CONFIG_INET6_AH" = "y" -o \
46       "$CONFIG_INET6_AH" = "m" -o \
47       "$CONFIG_INET6_ESP" = "y" -o \
48 -     "$CONFIG_INET6_ESP" = "m" ]; then
49 +     "$CONFIG_INET6_ESP" = "m" -o \
50 +     "$CONFIG_PPP_MPPE_MPPC" = "y" -o \
51 +     "$CONFIG_PPP_MPPE_MPPC" = "m" ]; then
52    define_bool CONFIG_CRYPTO y
53  else
54    bool 'Cryptographic API' CONFIG_CRYPTO
55 @@ -51,11 +53,24 @@ if [ "$CONFIG_CRYPTO" = "y" ]; then
56         "$CONFIG_INET6_AH" = "y" -o \
57         "$CONFIG_INET6_AH" = "m" -o \
58         "$CONFIG_INET6_ESP" = "y" -o \
59 -       "$CONFIG_INET6_ESP" = "m" ]; then
60 -    define_bool CONFIG_CRYPTO_SHA1 y
61 -  else
62 -    tristate       '  SHA1 digest algorithm' CONFIG_CRYPTO_SHA1
63 +       "$CONFIG_INET6_ESP" = "m" -o \
64 +       "$CONFIG_PPP_MPPE_MPPC" = "y" -o \
65 +       "$CONFIG_PPP_MPPE_MPPC" = "m" ]; then
66 +    if [ "$CONFIG_INET_AH" = "y" -o \
67 +        "$CONFIG_INET_ESP" = "y" -o \
68 +        "$CONFIG_INET6_AH" = "y" -o \
69 +        "$CONFIG_INET6_ESP" = "y" -o \
70 +        "$CONFIG_PPP_MPPE_MPPC" = "y" ]; then
71 +      define_tristate CONFIG_CRYPTO_SHA1 y
72 +    else
73 +      if [ "$CONFIG_CRYPTO_SHA1" != "y" -a \
74 +          "$CONFIG_CRYPTO_SHA1" != "m" ]; then
75 +       define_tristate CONFIG_CRYPTO_SHA1 m
76 +      fi
77 +    fi
78    fi
79 +  tristate       '  SHA1 digest algorithm' CONFIG_CRYPTO_SHA1
80 +
81    tristate       '  SHA256 digest algorithm' CONFIG_CRYPTO_SHA256
82    tristate       '  SHA384 and SHA512 digest algorithms' CONFIG_CRYPTO_SHA512
83    tristate       '  Whirlpool digest algorithms' CONFIG_CRYPTO_WP512
84 @@ -76,7 +91,20 @@ if [ "$CONFIG_CRYPTO" = "y" ]; then
85    tristate       '  TEA and XTEA cipher algorithms' CONFIG_CRYPTO_TEA
86    tristate       '  Khazad cipher algorithm' CONFIG_CRYPTO_KHAZAD
87    tristate       '  Anubis cipher algorithm' CONFIG_CRYPTO_ANUBIS
88 +
89 +  if [ "$CONFIG_PPP_MPPE_MPPC" = "y" -o \
90 +       "$CONFIG_PPP_MPPE_MPPC" = "m" ]; then
91 +    if [ "$CONFIG_PPP_MPPE_MPPC" = "y" ]; then
92 +      define_tristate CONFIG_CRYPTO_ARC4 y
93 +    else
94 +      if [ "$CONFIG_CRYPTO_ARC4" != "y" -a \
95 +          "$CONFIG_CRYPTO_ARC4" != "m" ]; then
96 +       define_tristate CONFIG_CRYPTO_ARC4 m
97 +      fi
98 +    fi
99 +  fi
100    tristate       '  ARC4 cipher algorithm' CONFIG_CRYPTO_ARC4
101 +
102    if [ "$CONFIG_INET_IPCOMP" = "y" -o \
103         "$CONFIG_INET_IPCOMP" = "m" -o \
104         "$CONFIG_INET6_IPCOMP" = "y" -o \
105 Index: linux-2.4.35.4/drivers/net/Config.in
106 ===================================================================
107 --- linux-2.4.35.4.orig/drivers/net/Config.in
108 +++ linux-2.4.35.4/drivers/net/Config.in
109 @@ -336,6 +336,7 @@ if [ ! "$CONFIG_PPP" = "n" ]; then
110     dep_tristate '  PPP support for sync tty ports' CONFIG_PPP_SYNC_TTY $CONFIG_PPP
111     dep_tristate '  PPP Deflate compression' CONFIG_PPP_DEFLATE $CONFIG_PPP
112     dep_tristate '  PPP BSD-Compress compression' CONFIG_PPP_BSDCOMP $CONFIG_PPP
113 +   dep_tristate '  Microsoft PPP compression/encryption (MPPC/MPPE)' CONFIG_PPP_MPPE_MPPC $CONFIG_PPP
114     if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
115        dep_tristate '  PPP over Ethernet (EXPERIMENTAL)' CONFIG_PPPOE $CONFIG_PPP
116     fi
117 Index: linux-2.4.35.4/drivers/net/Makefile
118 ===================================================================
119 --- linux-2.4.35.4.orig/drivers/net/Makefile
120 +++ linux-2.4.35.4/drivers/net/Makefile
121 @@ -166,6 +166,7 @@ obj-$(CONFIG_PPP_ASYNC) += ppp_async.o
122  obj-$(CONFIG_PPP_SYNC_TTY) += ppp_synctty.o
123  obj-$(CONFIG_PPP_DEFLATE) += ppp_deflate.o
124  obj-$(CONFIG_PPP_BSDCOMP) += bsd_comp.o
125 +obj-$(CONFIG_PPP_MPPE_MPPC) += ppp_mppe_mppc.o
126  obj-$(CONFIG_PPPOE) += pppox.o pppoe.o
127  
128  obj-$(CONFIG_SLIP) += slip.o
129 Index: linux-2.4.35.4/drivers/net/ppp_generic.c
130 ===================================================================
131 --- linux-2.4.35.4.orig/drivers/net/ppp_generic.c
132 +++ linux-2.4.35.4/drivers/net/ppp_generic.c
133 @@ -19,7 +19,7 @@
134   * PPP driver, written by Michael Callahan and Al Longyear, and
135   * subsequently hacked by Paul Mackerras.
136   *
137 - * ==FILEVERSION 20020217==
138 + * ==FILEVERSION 20040509==
139   */
140  
141  #include <linux/config.h>
142 @@ -102,6 +102,7 @@ struct ppp {
143         spinlock_t      rlock;          /* lock for receive side 58 */
144         spinlock_t      wlock;          /* lock for transmit side 5c */
145         int             mru;            /* max receive unit 60 */
146 +       int             mru_alloc;      /* MAX(1500,MRU) for dev_alloc_skb() */
147         unsigned int    flags;          /* control bits 64 */
148         unsigned int    xstate;         /* transmit state bits 68 */
149         unsigned int    rstate;         /* receive state bits 6c */
150 @@ -552,7 +553,9 @@ static int ppp_ioctl(struct inode *inode
151         case PPPIOCSMRU:
152                 if (get_user(val, (int *) arg))
153                         break;
154 -               ppp->mru = val;
155 +               ppp->mru_alloc = ppp->mru = val;
156 +               if (ppp->mru_alloc < PPP_MRU)
157 +                   ppp->mru_alloc = PPP_MRU;   /* increase for broken peers */
158                 err = 0;
159                 break;
160  
161 @@ -1025,14 +1028,37 @@ ppp_send_frame(struct ppp *ppp, struct s
162         case PPP_CCP:
163                 /* peek at outbound CCP frames */
164                 ppp_ccp_peek(ppp, skb, 0);
165 +               /*
166 +                * When LZS or MPPE/MPPC has been negotiated we don't send
167 +                * CCP_RESETACK after receiving CCP_RESETREQ; in fact pppd
168 +                * sends such a packet but we silently discard it here
169 +                */
170 +               if (CCP_CODE(skb->data+2) == CCP_RESETACK
171 +                   && (ppp->xcomp->compress_proto == CI_MPPE
172 +                       || ppp->xcomp->compress_proto == CI_LZS)) {
173 +                   --ppp->stats.tx_packets;
174 +                   ppp->stats.tx_bytes -= skb->len - 2;
175 +                   kfree_skb(skb);
176 +                   return;
177 +               }
178                 break;
179         }
180  
181         /* try to do packet compression */
182         if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
183             && proto != PPP_LCP && proto != PPP_CCP) {
184 -               new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
185 -                                   GFP_ATOMIC);
186 +               int comp_ovhd = 0;
187 +               /* 
188 +                * because of possible data expansion when MPPC or LZS
189 +                * is used, allocate compressor's buffer 12.5% bigger
190 +                * than MTU
191 +                */
192 +               if (ppp->xcomp->compress_proto == CI_MPPE)
193 +                   comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + MPPE_OVHD;
194 +               else if (ppp->xcomp->compress_proto == CI_LZS)
195 +                   comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + LZS_OVHD;
196 +               new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len
197 +                                   + comp_ovhd, GFP_ATOMIC);
198                 if (new_skb == 0) {
199                         printk(KERN_ERR "PPP: no memory (comp pkt)\n");
200                         goto drop;
201 @@ -1050,9 +1076,21 @@ ppp_send_frame(struct ppp *ppp, struct s
202                         skb = new_skb;
203                         skb_put(skb, len);
204                         skb_pull(skb, 2);       /* pull off A/C bytes */
205 -               } else {
206 +               } else if (len == 0) {
207                         /* didn't compress, or CCP not up yet */
208                         kfree_skb(new_skb);
209 +               } else {
210 +                       /*
211 +                        * (len < 0)
212 +                        * MPPE requires that we do not send unencrypted
213 +                        * frames.  The compressor will return -1 if we
214 +                        * should drop the frame.  We cannot simply test
215 +                        * the compress_proto because MPPE and MPPC share
216 +                        * the same number.
217 +                        */
218 +                       printk(KERN_ERR "ppp: compressor dropped pkt\n");
219 +                       kfree_skb(new_skb);
220 +                       goto drop;
221                 }
222         }
223  
224 @@ -1540,14 +1578,15 @@ ppp_decompress_frame(struct ppp *ppp, st
225         int len;
226  
227         if (proto == PPP_COMP) {
228 -               ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
229 +               ns = dev_alloc_skb(ppp->mru_alloc + PPP_HDRLEN);
230                 if (ns == 0) {
231                         printk(KERN_ERR "ppp_decompress_frame: no memory\n");
232                         goto err;
233                 }
234                 /* the decompressor still expects the A/C bytes in the hdr */
235                 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
236 -                               skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
237 +                               skb->len + 2, ns->data,
238 +                               ppp->mru_alloc + PPP_HDRLEN);
239                 if (len < 0) {
240                         /* Pass the compressed frame to pppd as an
241                            error indication. */
242 @@ -1573,7 +1612,14 @@ ppp_decompress_frame(struct ppp *ppp, st
243         return skb;
244  
245   err:
246 -       ppp->rstate |= SC_DC_ERROR;
247 +       if (ppp->rcomp->compress_proto != CI_MPPE
248 +           && ppp->rcomp->compress_proto != CI_LZS) {
249 +           /*
250 +            * If decompression protocol isn't MPPE/MPPC or LZS, we set
251 +            * SC_DC_ERROR flag and wait for CCP_RESETACK
252 +            */
253 +           ppp->rstate |= SC_DC_ERROR;
254 +       }
255         ppp_receive_error(ppp);
256         return skb;
257  }
258 @@ -2253,6 +2299,7 @@ ppp_create_interface(int unit, int *retp
259         /* Initialize the new ppp unit */
260         ppp->file.index = unit;
261         ppp->mru = PPP_MRU;
262 +       ppp->mru_alloc = PPP_MRU;
263         init_ppp_file(&ppp->file, INTERFACE);
264         ppp->file.hdrlen = PPP_HDRLEN - 2;      /* don't count proto bytes */
265         for (i = 0; i < NUM_NP; ++i)
266 Index: linux-2.4.35.4/drivers/net/ppp_mppe_mppc.c
267 ===================================================================
268 --- /dev/null
269 +++ linux-2.4.35.4/drivers/net/ppp_mppe_mppc.c
270 @@ -0,0 +1,1312 @@
271 +/*
272 + * ppp_mppe_mppc.c - MPPC/MPPE "compressor/decompressor" module.
273 + *
274 + * Copyright (c) 1994 Árpád Magosányi <mag@bunuel.tii.matav.hu>
275 + * Copyright (c) 1999 Tim Hockin, Cobalt Networks Inc. <thockin@cobaltnet.com>
276 + * Copyright (c) 2002-2005 Jan Dubiec <jdx@slackware.pl>
277 + * 
278 + * Permission to use, copy, modify, and distribute this software and its
279 + * documentation is hereby granted, provided that the above copyright
280 + * notice appears in all copies. This software is provided without any
281 + * warranty, express or implied.
282 + *
283 + * The code is based on MPPE kernel module written by Árpád Magosányi and
284 + * Tim Hockin which can be found on http://planetmirror.com/pub/mppe/.
285 + * I have added MPPC and 56 bit session keys support in MPPE.
286 + *
287 + * WARNING! Although this is open source code, its usage in some countries
288 + * (in particular in the USA) may violate Stac Inc. patent for MPPC.
289 + *
290 + *  ==FILEVERSION 20050202==
291 + *
292 + */
293 +
294 +#include <linux/init.h>
295 +#include <linux/module.h>
296 +#include <linux/mm.h>
297 +#include <linux/slab.h>
298 +#include <asm/scatterlist.h>
299 +#include <linux/vmalloc.h>
300 +#include <linux/crypto.h>
301 +
302 +#include <linux/ppp_defs.h>
303 +#include <linux/ppp-comp.h>
304 +
305 +/*
306 + * In 2.4.x kernels macro offset_in_page() is not defined in linux/mm.h so
307 + * we define it here; PAGE_MASK is defined in asm/page.h which is included
308 + * by linux/mm.h.
309 + */
310 +#define offset_in_page(p)      ((unsigned long)(p) & ~PAGE_MASK)
311 +
312 +/*
313 + * State for a mppc/mppe "(de)compressor".
314 + */
315 +struct ppp_mppe_state {
316 +    struct crypto_tfm *arc4_tfm;
317 +    struct crypto_tfm *sha1_tfm;
318 +    u8         *sha1_digest;
319 +    u8         master_key[MPPE_MAX_KEY_LEN];
320 +    u8         session_key[MPPE_MAX_KEY_LEN];
321 +    u8         mppc;           /* do we use compression (MPPC)? */
322 +    u8         mppe;           /* do we use encryption (MPPE)? */
323 +    u8         keylen;         /* key length in bytes */
324 +    u8         bitkeylen;      /* key length in bits */
325 +    u16                ccount;         /* coherency counter */
326 +    u16                bits;           /* MPPC/MPPE control bits */
327 +    u8         stateless;      /* do we use stateless mode? */
328 +    u8         nextflushed;    /* set A bit in the next outgoing packet;
329 +                                  used only by compressor*/
330 +    u8         flushexpected;  /* drop packets until A bit is received;
331 +                                  used only by decompressor*/
332 +    u8         *hist;          /* MPPC history */
333 +    u16                *hash;          /* Hash table; used only by compressor */
334 +    u16                histptr;        /* history "cursor" */
335 +    int                unit;
336 +    int                debug;
337 +    int                mru;
338 +    struct compstat stats;
339 +};
340 +
341 +#define MPPE_HIST_LEN          8192    /* MPPC history size */
342 +#define MPPE_MAX_CCOUNT                0x0FFF  /* max. coherency counter value */
343 +
344 +#define MPPE_BIT_FLUSHED       0x80    /* bit A */
345 +#define MPPE_BIT_RESET         0x40    /* bit B */
346 +#define MPPE_BIT_COMP          0x20    /* bit C */
347 +#define MPPE_BIT_ENCRYPTED     0x10    /* bit D */
348 +
349 +#define MPPE_SALT0             0xD1    /* values used in MPPE key derivation */
350 +#define MPPE_SALT1             0x26    /* according to RFC3079 */
351 +#define MPPE_SALT2             0x9E
352 +
353 +#define MPPE_CCOUNT(x)         ((((x)[4] & 0x0f) << 8) + (x)[5])
354 +#define MPPE_BITS(x)           ((x)[4] & 0xf0)
355 +#define MPPE_CTRLHI(x)         ((((x)->ccount & 0xf00)>>8)|((x)->bits))
356 +#define MPPE_CTRLLO(x)         ((x)->ccount & 0xff)
357 +
358 +/*
359 + * Kernel Crypto API needs its arguments to be in kmalloc'd memory, not in the
360 + * module static data area. That means sha_pad needs to be kmalloc'd. It is done
361 + * in mppe_module_init(). This has been pointed out on 30th July 2004 by Oleg
362 + * Makarenko on pptpclient-devel mailing list.
363 + */
364 +#define SHA1_PAD_SIZE          40
365 +struct sha_pad {
366 +    unsigned char sha_pad1[SHA1_PAD_SIZE];
367 +    unsigned char sha_pad2[SHA1_PAD_SIZE];
368 +};
369 +static struct sha_pad *sha_pad;
370 +
371 +static inline void
372 +setup_sg(struct scatterlist *sg, const void  *address, unsigned int length)
373 +{
374 +    sg[0].page = virt_to_page(address);
375 +    sg[0].offset = offset_in_page(address);
376 +    sg[0].length = length;
377 +}
378 +
379 +static inline void
380 +arc4_setkey(struct ppp_mppe_state *state, const unsigned char *key,
381 +           const unsigned int keylen)
382 +{
383 +    crypto_cipher_setkey(state->arc4_tfm, key, keylen);
384 +}
385 +
386 +static inline void
387 +arc4_encrypt(struct ppp_mppe_state *state, const unsigned char *in,
388 +            const unsigned int len, unsigned char *out)
389 +{
390 +    struct scatterlist sgin[4], sgout[4];
391 +
392 +    setup_sg(sgin, in, len);
393 +    setup_sg(sgout, out, len);
394 +    crypto_cipher_encrypt(state->arc4_tfm, sgout, sgin, len);
395 +}
396 +
397 +#define arc4_decrypt arc4_encrypt
398 +
399 +/*
400 + * Key Derivation, from RFC 3078, RFC 3079.
401 + * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
402 + */
403 +static void
404 +get_new_key_from_sha(struct ppp_mppe_state *state, unsigned char *interim_key)
405 +{
406 +    struct scatterlist sg[4];
407 +
408 +    setup_sg(&sg[0], state->master_key, state->keylen);
409 +    setup_sg(&sg[1], sha_pad->sha_pad1, sizeof(sha_pad->sha_pad1));
410 +    setup_sg(&sg[2], state->session_key, state->keylen);
411 +    setup_sg(&sg[3], sha_pad->sha_pad2, sizeof(sha_pad->sha_pad2));
412 +
413 +    crypto_digest_digest (state->sha1_tfm, sg, 4, state->sha1_digest);
414 +
415 +    memcpy(interim_key, state->sha1_digest, state->keylen);
416 +}
417 +
418 +static void
419 +mppe_change_key(struct ppp_mppe_state *state, int initialize)
420 +{
421 +    unsigned char interim_key[MPPE_MAX_KEY_LEN];
422 +
423 +    get_new_key_from_sha(state, interim_key);
424 +    if (initialize) {
425 +       memcpy(state->session_key, interim_key, state->keylen);
426 +    } else {
427 +       arc4_setkey(state, interim_key, state->keylen);
428 +       arc4_encrypt(state, interim_key, state->keylen, state->session_key);
429 +    }
430 +    if (state->keylen == 8) {
431 +       if (state->bitkeylen == 40) {
432 +           state->session_key[0] = MPPE_SALT0;
433 +           state->session_key[1] = MPPE_SALT1;
434 +           state->session_key[2] = MPPE_SALT2;
435 +       } else {
436 +           state->session_key[0] = MPPE_SALT0;
437 +       }
438 +    }
439 +    arc4_setkey(state, state->session_key, state->keylen);
440 +}
441 +
442 +/* increase 12-bit coherency counter */
443 +static inline void
444 +mppe_increase_ccount(struct ppp_mppe_state *state)
445 +{
446 +    state->ccount = (state->ccount + 1) & MPPE_MAX_CCOUNT;
447 +    if (state->mppe) {
448 +       if (state->stateless) {
449 +           mppe_change_key(state, 0);
450 +           state->nextflushed = 1;
451 +       } else {
452 +           if ((state->ccount & 0xff) == 0xff) {
453 +               mppe_change_key(state, 0);
454 +           }
455 +       }
456 +    }
457 +}
458 +
459 +/* allocate space for a MPPE/MPPC (de)compressor.  */
460 +/*   comp != 0 -> init compressor */
461 +/*   comp = 0 -> init decompressor */
462 +static void *
463 +mppe_alloc(unsigned char *options, int opt_len, int comp)
464 +{
465 +    struct ppp_mppe_state *state;
466 +    unsigned int digestsize;
467 +    u8* fname;
468 +
469 +    fname = comp ? "mppe_comp_alloc" : "mppe_decomp_alloc";
470 +
471 +    /*  
472 +     * Hack warning - additionally to the standard MPPC/MPPE configuration
473 +     * options, pppd passes to the (de)copressor 8 or 16 byte session key.
474 +     * Therefore options[1] contains MPPC/MPPE configuration option length
475 +     * (CILEN_MPPE = 6), but the real options length, depending on the key
476 +     * length, is 6+8 or 6+16.
477 +     */
478 +    if (opt_len < CILEN_MPPE) {
479 +       printk(KERN_WARNING "%s: wrong options length: %u\n", fname, opt_len);
480 +       return NULL;
481 +    }
482 +
483 +    if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
484 +       (options[2] & ~MPPE_STATELESS) != 0 ||
485 +       options[3] != 0 || options[4] != 0 ||
486 +       (options[5] & ~(MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
487 +       (options[5] & (MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
488 +       printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
489 +              "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname, options[0],
490 +              options[1], options[2], options[3], options[4], options[5]);
491 +       return NULL;
492 +    }
493 +
494 +    state = (struct ppp_mppe_state *)kmalloc(sizeof(*state), GFP_KERNEL);
495 +    if (state == NULL) {
496 +       printk(KERN_ERR "%s: cannot allocate space for %scompressor\n", fname,
497 +              comp ? "" : "de");
498 +       return NULL;
499 +    }
500 +    memset(state, 0, sizeof(struct ppp_mppe_state));
501 +
502 +    state->mppc = options[5] & MPPE_MPPC;      /* Do we use MPPC? */
503 +    state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
504 +       MPPE_40BIT);                            /* Do we use MPPE? */
505 +
506 +    if (state->mppc) {
507 +       /* allocate MPPC history */
508 +       state->hist = (u8*)vmalloc(2*MPPE_HIST_LEN*sizeof(u8));
509 +       if (state->hist == NULL) {
510 +           kfree(state);
511 +           printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
512 +                  fname);
513 +           return NULL;
514 +       }
515 +
516 +       /* allocate hashtable for MPPC compressor */
517 +       if (comp) {
518 +           state->hash = (u16*)vmalloc(MPPE_HIST_LEN*sizeof(u16));
519 +           if (state->hash == NULL) {
520 +               vfree(state->hist);
521 +               kfree(state);
522 +               printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
523 +                      fname);
524 +               return NULL;
525 +           }
526 +       }
527 +    }
528 +
529 +    if (state->mppe) { /* specific for MPPE */
530 +       /* Load ARC4 algorithm */
531 +       state->arc4_tfm = crypto_alloc_tfm("arc4", 0);
532 +       if (state->arc4_tfm == NULL) {
533 +           if (state->mppc) {
534 +               vfree(state->hash);
535 +               if (comp)
536 +                   vfree(state->hist);
537 +           }
538 +           kfree(state);
539 +           printk(KERN_ERR "%s: cannot load ARC4 module\n", fname);
540 +           return NULL;
541 +       }
542 +
543 +       /* Load SHA1 algorithm */
544 +       state->sha1_tfm = crypto_alloc_tfm("sha1", 0);
545 +       if (state->sha1_tfm == NULL) {
546 +           crypto_free_tfm(state->arc4_tfm);
547 +           if (state->mppc) {
548 +               vfree(state->hash);
549 +               if (comp)
550 +                   vfree(state->hist);
551 +           }
552 +           kfree(state);
553 +           printk(KERN_ERR "%s: cannot load SHA1 module\n", fname);
554 +           return NULL;
555 +       }
556 +
557 +       digestsize = crypto_tfm_alg_digestsize(state->sha1_tfm);
558 +       if (digestsize < MPPE_MAX_KEY_LEN) {
559 +           crypto_free_tfm(state->sha1_tfm);
560 +           crypto_free_tfm(state->arc4_tfm);
561 +           if (state->mppc) {
562 +               vfree(state->hash);
563 +               if (comp)
564 +                   vfree(state->hist);
565 +           }
566 +           kfree(state);
567 +           printk(KERN_ERR "%s: CryptoAPI SHA1 digest size too small\n", fname);
568 +       }
569 +
570 +       state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
571 +       if (!state->sha1_digest) {
572 +           crypto_free_tfm(state->sha1_tfm);
573 +           crypto_free_tfm(state->arc4_tfm);
574 +           if (state->mppc) {
575 +               vfree(state->hash);
576 +               if (comp)
577 +                   vfree(state->hist);
578 +           }
579 +           kfree(state);
580 +           printk(KERN_ERR "%s: cannot allocate space for SHA1 digest\n", fname);
581 +       }
582 +
583 +       memcpy(state->master_key, options+CILEN_MPPE, MPPE_MAX_KEY_LEN);
584 +       memcpy(state->session_key, state->master_key, MPPE_MAX_KEY_LEN);
585 +       /* initial key generation is done in mppe_init() */
586 +    }
587 +
588 +    MOD_INC_USE_COUNT;
589 +
590 +    return (void *) state;
591 +}
592 +
593 +static void *
594 +mppe_comp_alloc(unsigned char *options, int opt_len)
595 +{
596 +    return mppe_alloc(options, opt_len, 1);
597 +}
598 +
599 +static void *
600 +mppe_decomp_alloc(unsigned char *options, int opt_len)
601 +{
602 +    return mppe_alloc(options, opt_len, 0);
603 +}
604 +
605 +/* cleanup the (de)compressor */
606 +static void
607 +mppe_comp_free(void *arg)
608 +{
609 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
610 +
611 +    if (state != NULL) {
612 +       if (state->mppe) {
613 +           if (state->sha1_tfm != NULL)
614 +               crypto_free_tfm(state->sha1_tfm);
615 +           if (state->arc4_tfm != NULL)
616 +               crypto_free_tfm(state->arc4_tfm);
617 +       }
618 +       if (state->hist != NULL)
619 +           vfree(state->hist);
620 +       if (state->hash != NULL)
621 +           vfree(state->hash);
622 +       kfree(state);
623 +    }
624 +
625 +    MOD_DEC_USE_COUNT;
626 +}
627 +
628 +/* init MPPC/MPPE (de)compresor */
629 +/*   comp != 0 -> init compressor */
630 +/*   comp = 0 -> init decompressor */
631 +static int
632 +mppe_init(void *arg, unsigned char *options, int opt_len, int unit,
633 +         int hdrlen, int mru, int debug, int comp)
634 +{
635 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
636 +    u8* fname;
637 +
638 +    fname = comp ? "mppe_comp_init" : "mppe_decomp_init";
639 +
640 +    if (opt_len < CILEN_MPPE) {
641 +       if (debug)
642 +           printk(KERN_WARNING "%s: wrong options length: %u\n",
643 +                  fname, opt_len);
644 +       return 0;
645 +    }
646 +
647 +    if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
648 +       (options[2] & ~MPPE_STATELESS) != 0 ||
649 +       options[3] != 0 || options[4] != 0 ||
650 +       (options[5] & ~(MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
651 +       (options[5] & (MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
652 +       if (debug)
653 +           printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
654 +                  "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname,
655 +                  options[0], options[1], options[2], options[3], options[4],
656 +                  options[5]);
657 +       return 0;
658 +    }
659 +
660 +    if ((options[5] & ~MPPE_MPPC) != MPPE_128BIT &&
661 +       (options[5] & ~MPPE_MPPC) != MPPE_56BIT &&
662 +       (options[5] & ~MPPE_MPPC) != MPPE_40BIT &&
663 +       (options[5] & MPPE_MPPC) != MPPE_MPPC) {
664 +       if (debug)
665 +           printk(KERN_WARNING "%s: don't know what to do: o[5]=%02x\n",
666 +                  fname, options[5]);
667 +       return 0;
668 +    }
669 +
670 +    state->mppc = options[5] & MPPE_MPPC;      /* Do we use MPPC? */
671 +    state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
672 +       MPPE_40BIT);                            /* Do we use MPPE? */
673 +    state->stateless = options[2] & MPPE_STATELESS; /* Do we use stateless mode? */
674 +
675 +    switch (state->mppe) {
676 +    case MPPE_40BIT:     /* 40 bit key */
677 +       state->keylen = 8;
678 +       state->bitkeylen = 40;
679 +       break;
680 +    case MPPE_56BIT:     /* 56 bit key */
681 +       state->keylen = 8;
682 +       state->bitkeylen = 56;
683 +       break;
684 +    case MPPE_128BIT:    /* 128 bit key */
685 +       state->keylen = 16;
686 +       state->bitkeylen = 128;
687 +       break;
688 +    default:
689 +       state->keylen = 0;
690 +       state->bitkeylen = 0;
691 +    }
692 +
693 +    state->ccount = MPPE_MAX_CCOUNT;
694 +    state->bits = 0;
695 +    state->unit  = unit;
696 +    state->debug = debug;
697 +    state->histptr = MPPE_HIST_LEN;
698 +    if (state->mppc) { /* reset history if MPPC was negotiated */
699 +       memset(state->hist, 0, 2*MPPE_HIST_LEN*sizeof(u8));
700 +    }
701 +
702 +    if (state->mppe) { /* generate initial session keys */
703 +       mppe_change_key(state, 1);
704 +    }
705 +
706 +    if (comp) { /* specific for compressor */
707 +       state->nextflushed = 1;
708 +    } else { /* specific for decompressor */
709 +       state->mru = mru;
710 +       state->flushexpected = 1;
711 +    }
712 +
713 +    return 1;
714 +}
715 +
716 +static int
717 +mppe_comp_init(void *arg, unsigned char *options, int opt_len, int unit,
718 +              int hdrlen, int debug)
719 +{
720 +    return mppe_init(arg, options, opt_len, unit, hdrlen, 0, debug, 1);
721 +}
722 +
723 +
724 +static int
725 +mppe_decomp_init(void *arg, unsigned char *options, int opt_len, int unit,
726 +                int hdrlen, int mru, int debug)
727 +{
728 +    return mppe_init(arg, options, opt_len, unit, hdrlen, mru, debug, 0);
729 +}
730 +
731 +static void
732 +mppe_comp_reset(void *arg)
733 +{
734 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
735 +
736 +    if (state->debug)
737 +       printk(KERN_DEBUG "%s%d: resetting MPPC/MPPE compressor\n",
738 +              __FUNCTION__, state->unit);
739 +
740 +    state->nextflushed = 1;
741 +    if (state->mppe)
742 +       arc4_setkey(state, state->session_key, state->keylen);
743 +}
744 +
745 +static void
746 +mppe_decomp_reset(void *arg)
747 +{
748 +    /* When MPPC/MPPE is in use, we shouldn't receive any CCP Reset-Ack.
749 +       But when we receive such a packet, we just ignore it. */
750 +    return;
751 +}
752 +
753 +static void
754 +mppe_stats(void *arg, struct compstat *stats)
755 +{
756 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
757 +
758 +    *stats = state->stats;
759 +}
760 +
761 +/***************************/
762 +/**** Compression stuff ****/
763 +/***************************/
764 +/* inserts 1 to 8 bits into the output buffer */
765 +static inline void putbits8(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
766 +{
767 +    buf += *i;
768 +    if (*l >= n) {
769 +       *l = (*l) - n;
770 +       val <<= *l;
771 +       *buf = *buf | (val & 0xff);
772 +       if (*l == 0) {
773 +           *l = 8;
774 +           (*i)++;
775 +           *(++buf) = 0;
776 +       }
777 +    } else {
778 +       (*i)++;
779 +       *l = 8 - n + (*l);
780 +       val <<= *l;
781 +       *buf = *buf | ((val >> 8) & 0xff);
782 +       *(++buf) = val & 0xff;
783 +    }
784 +}
785 +
786 +/* inserts 9 to 16 bits into the output buffer */
787 +static inline void putbits16(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
788 +{
789 +    buf += *i;
790 +    if (*l >= n - 8) {
791 +       (*i)++;
792 +       *l = 8 - n + (*l);
793 +       val <<= *l;
794 +       *buf = *buf | ((val >> 8) & 0xff);
795 +       *(++buf) = val & 0xff;
796 +       if (*l == 0) {
797 +           *l = 8;
798 +           (*i)++;
799 +           *(++buf) = 0;
800 +       }
801 +    } else {
802 +       (*i)++; (*i)++;
803 +       *l = 16 - n + (*l);
804 +       val <<= *l;
805 +       *buf = *buf | ((val >> 16) & 0xff);
806 +       *(++buf) = (val >> 8) & 0xff;
807 +       *(++buf) = val & 0xff;
808 +    }
809 +}
810 +
811 +/* inserts 17 to 24 bits into the output buffer */
812 +static inline void putbits24(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
813 +{
814 +    buf += *i;
815 +    if (*l >= n - 16) {
816 +       (*i)++; (*i)++;
817 +       *l = 16 - n + (*l);
818 +       val <<= *l;
819 +       *buf = *buf | ((val >> 16) & 0xff);
820 +       *(++buf) = (val >> 8) & 0xff;
821 +       *(++buf) = val & 0xff;
822 +       if (*l == 0) {
823 +           *l = 8;
824 +           (*i)++;
825 +           *(++buf) = 0;
826 +       }
827 +    } else {
828 +       (*i)++; (*i)++; (*i)++;
829 +       *l = 24 - n + (*l);
830 +       val <<= *l;
831 +       *buf = *buf | ((val >> 24) & 0xff);
832 +       *(++buf) = (val >> 16) & 0xff;
833 +       *(++buf) = (val >> 8) & 0xff;
834 +       *(++buf) = val & 0xff;
835 +    }
836 +}
837 +
838 +static int
839 +mppc_compress(struct ppp_mppe_state *state, unsigned char *ibuf,
840 +             unsigned char *obuf, int isize, int osize)
841 +{
842 +    u32 olen, off, len, idx, i, l;
843 +    u8 *hist, *sbuf, *p, *q, *r, *s;
844 +
845 +    /*  
846 +       At this point, to avoid possible buffer overflow caused by packet
847 +       expansion during/after compression,  we should make sure that
848 +       osize >= (((isize*9)/8)+1)+2, but we don't do that because in
849 +       ppp_generic.c we simply allocate bigger obuf.
850 +
851 +       Maximum MPPC packet expansion is 12.5%. This is the worst case when
852 +       all octets in the input buffer are >= 0x80 and we cannot find any
853 +       repeated tokens. Additionally we have to reserve 2 bytes for MPPE/MPPC
854 +       status bits and coherency counter.
855 +    */
856 +
857 +    hist = state->hist + MPPE_HIST_LEN;
858 +    /* check if there is enough room at the end of the history */
859 +    if (state->histptr + isize >= 2*MPPE_HIST_LEN) {
860 +       state->bits |= MPPE_BIT_RESET;
861 +       state->histptr = MPPE_HIST_LEN;
862 +       memcpy(state->hist, hist, MPPE_HIST_LEN);
863 +    }
864 +    /* add packet to the history; isize must be <= MPPE_HIST_LEN */
865 +    sbuf = state->hist + state->histptr;
866 +    memcpy(sbuf, ibuf, isize);
867 +    state->histptr += isize;
868 +
869 +    /* compress data */
870 +    r = sbuf + isize;
871 +    *obuf = olen = i = 0;
872 +    l = 8;
873 +    while (i < isize - 2) {
874 +       s = q = sbuf + i;
875 +       idx = ((40543*((((s[0]<<4)^s[1])<<4)^s[2]))>>4) & 0x1fff;
876 +       p = hist + state->hash[idx];
877 +       state->hash[idx] = (u16) (s - hist);
878 +       off = s - p;
879 +       if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ || *p++ != *s++ ||
880 +           *p++ != *s++) {
881 +           /* no match found; encode literal byte */
882 +           if (ibuf[i] < 0x80) {               /* literal byte < 0x80 */
883 +               putbits8(obuf, (u32) ibuf[i], 8, &olen, &l);
884 +           } else {                            /* literal byte >= 0x80 */
885 +               putbits16(obuf, (u32) (0x100|(ibuf[i]&0x7f)), 9, &olen, &l);
886 +           }
887 +           ++i;
888 +           continue;
889 +       }
890 +       if (r - q >= 64) {
891 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
892 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
893 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
894 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
895 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
896 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
897 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
898 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
899 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
900 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
901 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
902 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
903 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
904 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
905 +           *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
906 +           *p++ != *s++;
907 +           if (s - q == 64) {
908 +               p--; s--;
909 +               while((*p++ == *s++) && (s < r) && (p < q));
910 +           }
911 +       } else {
912 +           while((*p++ == *s++) && (s < r) && (p < q));
913 +       }
914 +       len = s - q - 1;
915 +       i += len;
916 +
917 +       /* at least 3 character match found; code data */
918 +       /* encode offset */
919 +       if (off < 64) {                 /* 10-bit offset; 0 <= offset < 64 */
920 +           putbits16(obuf, 0x3c0|off, 10, &olen, &l);
921 +       } else if (off < 320) {         /* 12-bit offset; 64 <= offset < 320 */
922 +           putbits16(obuf, 0xe00|(off-64), 12, &olen, &l);
923 +       } else if (off < 8192) {        /* 16-bit offset; 320 <= offset < 8192 */
924 +           putbits16(obuf, 0xc000|(off-320), 16, &olen, &l);
925 +       } else {
926 +           /* This shouldn't happen; we return 0 what means "packet expands",
927 +           and we send packet uncompressed. */
928 +           if (state->debug)
929 +               printk(KERN_DEBUG "%s%d: wrong offset value: %d\n",
930 +                      __FUNCTION__, state->unit, off);
931 +           return 0;
932 +       }
933 +       /* encode length of match */
934 +       if (len < 4) {                  /* length = 3 */
935 +           putbits8(obuf, 0, 1, &olen, &l);
936 +       } else if (len < 8) {           /* 4 <= length < 8 */
937 +           putbits8(obuf, 0x08|(len&0x03), 4, &olen, &l);
938 +       } else if (len < 16) {          /* 8 <= length < 16 */
939 +           putbits8(obuf, 0x30|(len&0x07), 6, &olen, &l);
940 +       } else if (len < 32) {          /* 16 <= length < 32 */
941 +           putbits8(obuf, 0xe0|(len&0x0f), 8, &olen, &l);
942 +       } else if (len < 64) {          /* 32 <= length < 64 */
943 +           putbits16(obuf, 0x3c0|(len&0x1f), 10, &olen, &l);
944 +       } else if (len < 128) {         /* 64 <= length < 128 */
945 +           putbits16(obuf, 0xf80|(len&0x3f), 12, &olen, &l);
946 +       } else if (len < 256) {         /* 128 <= length < 256 */
947 +           putbits16(obuf, 0x3f00|(len&0x7f), 14, &olen, &l);
948 +       } else if (len < 512) {         /* 256 <= length < 512 */
949 +           putbits16(obuf, 0xfe00|(len&0xff), 16, &olen, &l);
950 +       } else if (len < 1024) {        /* 512 <= length < 1024 */
951 +           putbits24(obuf, 0x3fc00|(len&0x1ff), 18, &olen, &l);
952 +       } else if (len < 2048) {        /* 1024 <= length < 2048 */
953 +           putbits24(obuf, 0xff800|(len&0x3ff), 20, &olen, &l);
954 +       } else if (len < 4096) {        /* 2048 <= length < 4096 */
955 +           putbits24(obuf, 0x3ff000|(len&0x7ff), 22, &olen, &l);
956 +       } else if (len < 8192) {        /* 4096 <= length < 8192 */
957 +           putbits24(obuf, 0xffe000|(len&0xfff), 24, &olen, &l);
958 +       } else {
959 +           /* This shouldn't happen; we return 0 what means "packet expands",
960 +           and send packet uncompressed. */
961 +           if (state->debug)
962 +               printk(KERN_DEBUG "%s%d: wrong length of match value: %d\n",
963 +                      __FUNCTION__, state->unit, len);
964 +           return 0;
965 +       }
966 +    }
967 +
968 +    /* Add remaining octets to the output */
969 +    while(isize - i > 0) {
970 +       if (ibuf[i] < 0x80) {   /* literal byte < 0x80 */
971 +           putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l);
972 +       } else {                /* literal byte >= 0x80 */
973 +           putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l);
974 +       }
975 +    }
976 +    /* Reset unused bits of the last output octet */
977 +    if ((l != 0) && (l != 8)) {
978 +       putbits8(obuf, 0, l, &olen, &l);
979 +    }
980 +
981 +    return (int) olen;
982 +}
983 +
984 +int
985 +mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
986 +             int isize, int osize)
987 +{
988 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
989 +    int proto, olen, complen, off;
990 +    unsigned char *wptr;
991 +
992 +    /* Check that the protocol is in the range we handle. */
993 +    proto = PPP_PROTOCOL(ibuf);
994 +    if (proto < 0x0021 || proto > 0x00fa)
995 +       return 0;
996 +
997 +    wptr = obuf;
998 +    /* Copy over the PPP header */
999 +    wptr[0] = PPP_ADDRESS(ibuf);
1000 +    wptr[1] = PPP_CONTROL(ibuf);
1001 +    wptr[2] = PPP_COMP >> 8;
1002 +    wptr[3] = PPP_COMP;
1003 +    wptr += PPP_HDRLEN + (MPPE_OVHD / 2); /* Leave two octets for MPPE/MPPC bits */
1004 +
1005 +    /* 
1006 +     * In ver. 0.99 protocol field was compressed. Deflate and BSD compress
1007 +     * do PFC before actual compression, RCF2118 and RFC3078 are not precise
1008 +     * on this topic so I decided to do PFC. Unfortunately this change caused
1009 +     * incompatibility with older/other MPPE/MPPC modules. I have received
1010 +     * a lot of complaints from unexperienced users so I have decided to revert
1011 +     * to previous state, i.e. the protocol field is sent uncompressed now.
1012 +     * Although this may be changed in the future.
1013 +     *
1014 +     * Receiving side (mppe_decompress()) still accepts packets with compressed
1015 +     * and uncompressed protocol field so you shouldn't get "Unsupported protocol
1016 +     * 0x2145 received" messages anymore.
1017 +     */
1018 +    //off = (proto > 0xff) ? 2 : 3; /* PFC - skip first protocol byte if 0 */
1019 +    off = 2;
1020 +
1021 +    ibuf += off;
1022 +
1023 +    mppe_increase_ccount(state);
1024 +
1025 +    if (state->nextflushed) {
1026 +       state->bits |= MPPE_BIT_FLUSHED;
1027 +       state->nextflushed = 0;
1028 +       if (state->mppe && !state->stateless) {
1029 +           /*
1030 +            * If this is the flag packet, the key has been already changed in
1031 +            * mppe_increase_ccount() so we dont't do it once again.
1032 +            */
1033 +           if ((state->ccount & 0xff) != 0xff) {
1034 +               arc4_setkey(state, state->session_key, state->keylen);
1035 +           }
1036 +       }
1037 +       if (state->mppc) { /* reset history */
1038 +           state->bits |= MPPE_BIT_RESET;
1039 +           state->histptr = MPPE_HIST_LEN;
1040 +           memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
1041 +       }
1042 +    }
1043 +
1044 +    if (state->mppc && !state->mppe) { /* Do only compression */
1045 +       complen = mppc_compress(state, ibuf, wptr, isize - off,
1046 +                               osize - PPP_HDRLEN - (MPPE_OVHD / 2));
1047 +       /*
1048 +        * TODO: Implement an heuristics to handle packet expansion in a smart
1049 +        * way. Now, when a packet expands, we send it as uncompressed and
1050 +        * when next packet is sent we have to reset compressor's history.
1051 +        * Maybe it would be better to send such packet as compressed in order
1052 +        * to keep history's continuity.
1053 +        */
1054 +       if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
1055 +           (complen == 0)) {
1056 +           /* packet expands */
1057 +           state->nextflushed = 1;
1058 +           memcpy(wptr, ibuf, isize - off);
1059 +           olen = isize - (off - 2) + MPPE_OVHD;
1060 +           (state->stats).inc_bytes += olen;
1061 +           (state->stats).inc_packets++;
1062 +       } else {
1063 +           state->bits |= MPPE_BIT_COMP;
1064 +           olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
1065 +           (state->stats).comp_bytes += olen;
1066 +           (state->stats).comp_packets++;
1067 +       }
1068 +    } else { /* Do encryption with or without compression */
1069 +       state->bits |= MPPE_BIT_ENCRYPTED;
1070 +       if (!state->mppc && state->mppe) { /* Do only encryption */
1071 +           /* read from ibuf, write to wptr, adjust for PPP_HDRLEN */
1072 +           arc4_encrypt(state, ibuf, isize - off, wptr);
1073 +           olen = isize - (off - 2) + MPPE_OVHD;
1074 +           (state->stats).inc_bytes += olen;
1075 +           (state->stats).inc_packets++;
1076 +       } else { /* Do compression and then encryption - RFC3078 */
1077 +           complen = mppc_compress(state, ibuf, wptr, isize - off,
1078 +                                   osize - PPP_HDRLEN - (MPPE_OVHD / 2));
1079 +           /*
1080 +            * TODO: Implement an heuristics to handle packet expansion in a smart
1081 +            * way. Now, when a packet expands, we send it as uncompressed and
1082 +            * when next packet is sent we have to reset compressor's history.
1083 +            * Maybe it would be good to send such packet as compressed in order
1084 +            * to keep history's continuity.
1085 +            */
1086 +           if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
1087 +               (complen == 0)) {
1088 +               /* packet expands */
1089 +               state->nextflushed = 1;
1090 +               arc4_encrypt(state, ibuf, isize - off, wptr);
1091 +               olen = isize - (off - 2) + MPPE_OVHD;
1092 +               (state->stats).inc_bytes += olen;
1093 +               (state->stats).inc_packets++;
1094 +           } else {
1095 +               state->bits |= MPPE_BIT_COMP;
1096 +               /* Hack warning !!! RC4 implementation which we use does
1097 +                  encryption "in place" - it means that input and output
1098 +                  buffers can be *the same* memory area. Therefore we don't
1099 +                  need to use a temporary buffer. But be careful - other
1100 +                  implementations don't have to be so nice.
1101 +                  I used to use ibuf as temporary buffer here, but it led
1102 +                  packet sniffers into error. Thanks to Wilfried Weissmann
1103 +                  for pointing that. */
1104 +               arc4_encrypt(state, wptr, complen, wptr);
1105 +               olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
1106 +               (state->stats).comp_bytes += olen;
1107 +               (state->stats).comp_packets++;
1108 +           }
1109 +       }
1110 +    }
1111 +
1112 +    /* write status bits and coherency counter into the output buffer */
1113 +    wptr = obuf + PPP_HDRLEN;
1114 +    wptr[0] = MPPE_CTRLHI(state);
1115 +    wptr[1] = MPPE_CTRLLO(state);
1116 +
1117 +    state->bits = 0;
1118 +
1119 +    (state->stats).unc_bytes += isize;
1120 +    (state->stats).unc_packets++;
1121 +
1122 +    return olen;
1123 +}
1124 +
1125 +/***************************/
1126 +/*** Decompression stuff ***/
1127 +/***************************/
1128 +static inline u32 getbits(const u8 *buf, const u32 n, u32 *i, u32 *l)
1129 +{
1130 +    static const u32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1131 +    u32 res, ol;
1132 +
1133 +    ol = *l;
1134 +    if (*l >= n) {
1135 +       *l = (*l) - n;
1136 +       res = (buf[*i] & m[ol]) >> (*l);
1137 +       if (*l == 0) {
1138 +           *l = 8;
1139 +           (*i)++;
1140 +       }
1141 +    } else {
1142 +       *l = 8 - n + (*l);
1143 +       res = (buf[(*i)++] & m[ol]) << 8;
1144 +       res = (res | buf[*i]) >> (*l);
1145 +    }
1146 +
1147 +    return res;
1148 +}
1149 +
1150 +static inline u32 getbyte(const u8 *buf, const u32 i, const u32 l)
1151 +{
1152 +    if (l == 8) {
1153 +       return buf[i];
1154 +    } else {
1155 +       return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff;
1156 +    }
1157 +}
1158 +
1159 +static inline void lamecopy(u8 *dst, u8 *src, u32 len)
1160 +{
1161 +    while (len--)
1162 +       *dst++ = *src++;
1163 +}
1164 +
1165 +static int
1166 +mppc_decompress(struct ppp_mppe_state *state, unsigned char *ibuf,
1167 +               unsigned char *obuf, int isize, int osize)
1168 +{
1169 +    u32 olen, off, len, bits, val, sig, i, l;
1170 +    u8 *history, *s;
1171 +
1172 +    history = state->hist + state->histptr;
1173 +    olen = len = i = 0;
1174 +    l = 8;
1175 +    bits = isize * 8;
1176 +    while (bits >= 8) {
1177 +       val = getbyte(ibuf, i++, l);
1178 +       if (val < 0x80) {               /* literal byte < 0x80 */
1179 +           if (state->histptr < 2*MPPE_HIST_LEN) {
1180 +               /* copy uncompressed byte to the history */
1181 +               (state->hist)[(state->histptr)++] = (u8) val;
1182 +           } else {
1183 +               /* buffer overflow; drop packet */
1184 +               if (state->debug)
1185 +                   printk(KERN_ERR "%s%d: trying to write outside history "
1186 +                          "buffer\n", __FUNCTION__, state->unit);
1187 +               return DECOMP_ERROR;
1188 +           }
1189 +           olen++;
1190 +           bits -= 8;
1191 +           continue;
1192 +       }
1193 +
1194 +       sig = val & 0xc0;
1195 +       if (sig == 0x80) {              /* literal byte >= 0x80 */
1196 +           if (state->histptr < 2*MPPE_HIST_LEN) {
1197 +               /* copy uncompressed byte to the history */
1198 +               (state->hist)[(state->histptr)++] = 
1199 +                   (u8) (0x80|((val&0x3f)<<1)|getbits(ibuf, 1 , &i ,&l));
1200 +           } else {
1201 +               /* buffer overflow; drop packet */
1202 +               if (state->debug)
1203 +                   printk(KERN_ERR "%s%d: trying to write outside history "
1204 +                          "buffer\n", __FUNCTION__, state->unit);
1205 +               return DECOMP_ERROR;
1206 +           }
1207 +           olen++;
1208 +           bits -= 9;
1209 +           continue;
1210 +       }
1211 +
1212 +       /* Not a literal byte so it must be an (offset,length) pair */
1213 +       /* decode offset */
1214 +       sig = val & 0xf0;
1215 +       if (sig == 0xf0) {              /* 10-bit offset; 0 <= offset < 64 */
1216 +           off = (((val&0x0f)<<2)|getbits(ibuf, 2 , &i ,&l));
1217 +           bits -= 10;
1218 +       } else {
1219 +           if (sig == 0xe0) {          /* 12-bit offset; 64 <= offset < 320 */
1220 +               off = ((((val&0x0f)<<4)|getbits(ibuf, 4 , &i ,&l))+64);
1221 +               bits -= 12;
1222 +           } else {
1223 +               if ((sig&0xe0) == 0xc0) {/* 16-bit offset; 320 <= offset < 8192 */
1224 +                   off = ((((val&0x1f)<<8)|getbyte(ibuf, i++, l))+320);
1225 +                   bits -= 16;
1226 +                   if (off > MPPE_HIST_LEN - 1) {
1227 +                       if (state->debug)
1228 +                           printk(KERN_DEBUG "%s%d: too big offset value: %d\n",
1229 +                                  __FUNCTION__, state->unit, off);
1230 +                       return DECOMP_ERROR;
1231 +                   }
1232 +               } else {                /* this shouldn't happen */
1233 +                   if (state->debug)
1234 +                       printk(KERN_DEBUG "%s%d: cannot decode offset value\n",
1235 +                              __FUNCTION__, state->unit);
1236 +                   return DECOMP_ERROR;
1237 +               }
1238 +           }
1239 +       }
1240 +       /* decode length of match */
1241 +       val = getbyte(ibuf, i, l);
1242 +       if ((val & 0x80) == 0x00) {                     /* len = 3 */
1243 +           len = 3;
1244 +           bits--;
1245 +           getbits(ibuf, 1 , &i ,&l);
1246 +       } else if ((val & 0xc0) == 0x80) {              /* 4 <= len < 8 */
1247 +           len = 0x04 | ((val>>4) & 0x03);
1248 +           bits -= 4;
1249 +           getbits(ibuf, 4 , &i ,&l);
1250 +       } else if ((val & 0xe0) == 0xc0) {              /* 8 <= len < 16 */
1251 +           len = 0x08 | ((val>>2) & 0x07);
1252 +           bits -= 6;
1253 +           getbits(ibuf, 6 , &i ,&l);
1254 +       } else if ((val & 0xf0) == 0xe0) {              /* 16 <= len < 32 */
1255 +           len = 0x10 | (val & 0x0f);
1256 +           bits -= 8;
1257 +           i++;
1258 +       } else {
1259 +           bits -= 8;
1260 +           val = (val << 8) | getbyte(ibuf, ++i, l);
1261 +           if ((val & 0xf800) == 0xf000) {             /* 32 <= len < 64 */
1262 +               len = 0x0020 | ((val >> 6) & 0x001f);
1263 +               bits -= 2;
1264 +               getbits(ibuf, 2 , &i ,&l);
1265 +           } else if ((val & 0xfc00) == 0xf800) {      /* 64 <= len < 128 */
1266 +               len = 0x0040 | ((val >> 4) & 0x003f);
1267 +               bits -= 4;
1268 +               getbits(ibuf, 4 , &i ,&l);
1269 +           } else if ((val & 0xfe00) == 0xfc00) {      /* 128 <= len < 256 */
1270 +               len = 0x0080 | ((val >> 2) & 0x007f);
1271 +               bits -= 6;
1272 +               getbits(ibuf, 6 , &i ,&l);
1273 +           } else if ((val & 0xff00) == 0xfe00) {      /* 256 <= len < 512 */
1274 +               len = 0x0100 | (val & 0x00ff);
1275 +               bits -= 8;
1276 +               i++;
1277 +           } else {
1278 +               bits -= 8;
1279 +               val = (val << 8) | getbyte(ibuf, ++i, l);
1280 +               if ((val & 0xff8000) == 0xff0000) {     /* 512 <= len < 1024 */
1281 +                   len = 0x000200 | ((val >> 6) & 0x0001ff);
1282 +                   bits -= 2;
1283 +                   getbits(ibuf, 2 , &i ,&l);
1284 +               } else if ((val & 0xffc000) == 0xff8000) {/* 1024 <= len < 2048 */
1285 +                   len = 0x000400 | ((val >> 4) & 0x0003ff);
1286 +                   bits -= 4;
1287 +                   getbits(ibuf, 4 , &i ,&l);
1288 +               } else if ((val & 0xffe000) == 0xffc000) {/* 2048 <= len < 4096 */
1289 +                   len = 0x000800 | ((val >> 2) & 0x0007ff);
1290 +                   bits -= 6;
1291 +                   getbits(ibuf, 6 , &i ,&l);
1292 +               } else if ((val & 0xfff000) == 0xffe000) {/* 4096 <= len < 8192 */
1293 +                   len = 0x001000 | (val & 0x000fff);
1294 +                   bits -= 8;
1295 +                   i++;
1296 +               } else {                                /* this shouldn't happen */
1297 +                   if (state->debug)
1298 +                       printk(KERN_DEBUG "%s%d: wrong length code: 0x%X\n",
1299 +                              __FUNCTION__, state->unit, val);
1300 +                   return DECOMP_ERROR;
1301 +               }
1302 +           }
1303 +       }
1304 +       s = state->hist + state->histptr;
1305 +       state->histptr += len;
1306 +       olen += len;
1307 +       if (state->histptr < 2*MPPE_HIST_LEN) {
1308 +           /* copy uncompressed bytes to the history */
1309 +
1310 +           /* In some cases len may be greater than off. It means that memory
1311 +            * areas pointed by s and s-off overlap. I had used memmove() here
1312 +            * because I thought that it acts as libc's version. Unfortunately,
1313 +            * I was wrong. :-) I got strange errors sometimes. Wilfried suggested
1314 +            * using of byte by byte copying here and strange errors disappeared.
1315 +            */
1316 +           lamecopy(s, s - off, len);
1317 +       } else {
1318 +           /* buffer overflow; drop packet */
1319 +           if (state->debug)
1320 +               printk(KERN_ERR "%s%d: trying to write outside history "
1321 +                      "buffer\n", __FUNCTION__, state->unit);
1322 +           return DECOMP_ERROR;
1323 +       }
1324 +    }
1325 +
1326 +    /* Do PFC decompression */
1327 +    len = olen;
1328 +    if ((history[0] & 0x01) != 0) {
1329 +       obuf[0] = 0;
1330 +       obuf++;
1331 +       len++;
1332 +    }
1333 +
1334 +    if (len <= osize) {
1335 +       /* copy uncompressed packet to the output buffer */
1336 +       memcpy(obuf, history, olen);
1337 +    } else {
1338 +       /* buffer overflow; drop packet */
1339 +       if (state->debug)
1340 +           printk(KERN_ERR "%s%d: too big uncompressed packet: %d\n",
1341 +                  __FUNCTION__, state->unit, len + (PPP_HDRLEN / 2));
1342 +       return DECOMP_ERROR;
1343 +    }
1344 +
1345 +    return (int) len;
1346 +}
1347 +
1348 +int
1349 +mppe_decompress(void *arg, unsigned char *ibuf, int isize,
1350 +               unsigned char *obuf, int osize)
1351 +{
1352 +    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
1353 +    int seq, bits, uncomplen;
1354 +
1355 +    if (isize <= PPP_HDRLEN + MPPE_OVHD) {
1356 +       if (state->debug) {
1357 +           printk(KERN_DEBUG "%s%d: short packet (len=%d)\n",  __FUNCTION__,
1358 +                  state->unit, isize);
1359 +       }
1360 +       return DECOMP_ERROR;
1361 +    }
1362 +
1363 +    /* Get coherency counter and control bits from input buffer */
1364 +    seq = MPPE_CCOUNT(ibuf);
1365 +    bits = MPPE_BITS(ibuf);
1366 +
1367 +    if (state->stateless) {
1368 +       /* RFC 3078, sec 8.1. */
1369 +       mppe_increase_ccount(state);
1370 +       if ((seq != state->ccount) && state->debug)
1371 +           printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1372 +                  __FUNCTION__, state->unit, seq, state->ccount);
1373 +       while (seq != state->ccount)
1374 +           mppe_increase_ccount(state);
1375 +    } else {
1376 +       /* RFC 3078, sec 8.2. */
1377 +       if (state->flushexpected) { /* discard state */
1378 +           if ((bits & MPPE_BIT_FLUSHED)) { /* we received expected FLUSH bit */
1379 +               while (seq != state->ccount)
1380 +                   mppe_increase_ccount(state);
1381 +               state->flushexpected = 0;
1382 +           } else /* drop packet*/
1383 +               return DECOMP_ERROR;
1384 +       } else { /* normal state */
1385 +           mppe_increase_ccount(state);
1386 +           if (seq != state->ccount) {
1387 +               /* Packet loss detected, enter the discard state. */
1388 +               if (state->debug)
1389 +                   printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1390 +                          __FUNCTION__, state->unit, seq, state->ccount);
1391 +               state->flushexpected = 1;
1392 +               return DECOMP_ERROR;
1393 +           }
1394 +       }
1395 +       if (state->mppe && (bits & MPPE_BIT_FLUSHED)) {
1396 +           arc4_setkey(state, state->session_key, state->keylen);
1397 +       }
1398 +    }
1399 +
1400 +    if (state->mppc && (bits & (MPPE_BIT_FLUSHED | MPPE_BIT_RESET))) {
1401 +       state->histptr = MPPE_HIST_LEN;
1402 +       if ((bits & MPPE_BIT_FLUSHED)) {
1403 +           memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
1404 +       } else
1405 +           if ((bits & MPPE_BIT_RESET)) {
1406 +               memcpy(state->hist, state->hist + MPPE_HIST_LEN, MPPE_HIST_LEN);
1407 +           }
1408 +    }
1409 +
1410 +    /* Fill in the first part of the PPP header. The protocol field
1411 +       comes from the decompressed data. */
1412 +    obuf[0] = PPP_ADDRESS(ibuf);
1413 +    obuf[1] = PPP_CONTROL(ibuf);
1414 +    obuf += PPP_HDRLEN / 2;
1415 +
1416 +    if (state->mppe) { /* process encrypted packet */
1417 +       if ((bits & MPPE_BIT_ENCRYPTED)) {
1418 +           /* OK, packet encrypted, so decrypt it */
1419 +           if (state->mppc && (bits & MPPE_BIT_COMP)) {
1420 +               /* Hack warning !!! RC4 implementation which we use does
1421 +                  decryption "in place" - it means that input and output
1422 +                  buffers can be *the same* memory area. Therefore we don't
1423 +                  need to use a temporary buffer. But be careful - other
1424 +                  implementations don't have to be so nice. */
1425 +               arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1426 +                            PPP_HDRLEN - (MPPE_OVHD / 2), ibuf + PPP_HDRLEN +
1427 +                            (MPPE_OVHD / 2));
1428 +               uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1429 +                                           (MPPE_OVHD / 2), obuf, isize -
1430 +                                           PPP_HDRLEN - (MPPE_OVHD / 2),
1431 +                                           osize - (PPP_HDRLEN / 2));
1432 +               if (uncomplen == DECOMP_ERROR) {
1433 +                   state->flushexpected = 1;
1434 +                   return DECOMP_ERROR;
1435 +               }
1436 +               uncomplen += PPP_HDRLEN / 2;
1437 +               (state->stats).comp_bytes += isize;
1438 +               (state->stats).comp_packets++;
1439 +           } else {
1440 +               uncomplen = isize - MPPE_OVHD;
1441 +               /* Decrypt the first byte in order to check if it is
1442 +                  compressed or uncompressed protocol field */
1443 +               arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), 1, obuf);
1444 +               /* Do PFC decompression */
1445 +               if ((obuf[0] & 0x01) != 0) {
1446 +                   obuf[1] = obuf[0];
1447 +                   obuf[0] = 0;
1448 +                   obuf++;
1449 +                   uncomplen++;
1450 +               }
1451 +               /* And finally, decrypt the rest of the frame. */
1452 +               arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2) + 1,
1453 +                            isize - PPP_HDRLEN - (MPPE_OVHD / 2) - 1, obuf + 1);
1454 +               (state->stats).inc_bytes += isize;
1455 +               (state->stats).inc_packets++;
1456 +           }
1457 +       } else { /* this shouldn't happen */
1458 +           if (state->debug)
1459 +               printk(KERN_ERR "%s%d: encryption negotiated but not an "
1460 +                      "encrypted packet received\n", __FUNCTION__, state->unit);
1461 +           mppe_change_key(state, 0);
1462 +           state->flushexpected = 1;
1463 +           return DECOMP_ERROR;
1464 +       }
1465 +    } else {
1466 +       if (state->mppc) { /* no MPPE, only MPPC */
1467 +           if ((bits & MPPE_BIT_COMP)) {
1468 +               uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1469 +                                           (MPPE_OVHD / 2), obuf, isize -
1470 +                                           PPP_HDRLEN - (MPPE_OVHD / 2),
1471 +                                           osize - (PPP_HDRLEN / 2));
1472 +               if (uncomplen == DECOMP_ERROR) {
1473 +                   state->flushexpected = 1;
1474 +                   return DECOMP_ERROR;
1475 +               }
1476 +               uncomplen += PPP_HDRLEN / 2;
1477 +               (state->stats).comp_bytes += isize;
1478 +               (state->stats).comp_packets++;
1479 +           } else {
1480 +               memcpy(obuf, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1481 +                      PPP_HDRLEN - (MPPE_OVHD / 2));
1482 +               uncomplen = isize - MPPE_OVHD;
1483 +               (state->stats).inc_bytes += isize;
1484 +               (state->stats).inc_packets++;
1485 +           }
1486 +       } else { /* this shouldn't happen */
1487 +           if (state->debug)
1488 +               printk(KERN_ERR "%s%d: error - not an  MPPC or MPPE frame "
1489 +                      "received\n", __FUNCTION__, state->unit);
1490 +           state->flushexpected = 1;
1491 +           return DECOMP_ERROR;
1492 +       }
1493 +    }
1494 +
1495 +    (state->stats).unc_bytes += uncomplen;
1496 +    (state->stats).unc_packets++;
1497 +
1498 +    return uncomplen;
1499 +}
1500 +
1501 +
1502 +/************************************************************
1503 + * Module interface table
1504 + ************************************************************/
1505 +
1506 +/* These are in ppp_generic.c */
1507 +extern int  ppp_register_compressor   (struct compressor *cp);
1508 +extern void ppp_unregister_compressor (struct compressor *cp);
1509 +
1510 +/*
1511 + * Functions exported to ppp_generic.c.
1512 + *
1513 + * In case of MPPC/MPPE there is no need to process incompressible data
1514 + * because such a data is sent in MPPC/MPPE frame. Therefore the (*incomp)
1515 + * callback function isn't needed.
1516 + */
1517 +struct compressor ppp_mppe = {
1518 +    CI_MPPE,           /* compress_proto */
1519 +    mppe_comp_alloc,   /* comp_alloc */
1520 +    mppe_comp_free,    /* comp_free */
1521 +    mppe_comp_init,    /* comp_init */
1522 +    mppe_comp_reset,   /* comp_reset */
1523 +    mppe_compress,     /* compress */
1524 +    mppe_stats,                /* comp_stat */
1525 +    mppe_decomp_alloc, /* decomp_alloc */
1526 +    mppe_comp_free,    /* decomp_free */
1527 +    mppe_decomp_init,  /* decomp_init */
1528 +    mppe_decomp_reset, /* decomp_reset */
1529 +    mppe_decompress,   /* decompress */
1530 +    NULL,              /* incomp */
1531 +    mppe_stats,                /* decomp_stat */
1532 +};
1533 +
1534 +/************************************************************
1535 + * Module support routines
1536 + ************************************************************/
1537 +
1538 +int __init mppe_module_init(void)
1539 +{
1540 +    int answer;
1541 +
1542 +    /* When the module is compiled into kernel, it is initialized before
1543 +     * CryptoAPI. Therefore the code below reports that there is no crypto
1544 +     * stuff in the kernel. Marlon de Boer who has pointed out the problem,
1545 +     * has sent also a message to CryptoAPI developers, so maybe in the future
1546 +     * the code will be uncommented.
1547 +     *
1548 +    if (!(crypto_alg_available("arc4", 0) && crypto_alg_available("sha1", 0))) {
1549 +       printk(KERN_ERR "Kernel doesn't provide ARC4 and/or SHA1 algorithms "
1550 +              "required by MPPE/MPPC. Check CryptoAPI configuration.\n");
1551 +       return -ENODEV;
1552 +    }
1553 +    */
1554 +
1555 +    /* Allocate space for SHAPad1, SHAPad2 and ... */
1556 +    sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
1557 +    if (sha_pad == NULL)
1558 +       return -ENOMEM;
1559 +    /* ... initialize them */
1560 +    memset(sha_pad->sha_pad1, 0x00, sizeof(sha_pad->sha_pad1));
1561 +    memset(sha_pad->sha_pad2, 0xf2, sizeof(sha_pad->sha_pad2));
1562 +
1563 +    answer = ppp_register_compressor(&ppp_mppe);
1564 +    if (answer == 0) {
1565 +       printk(KERN_INFO "MPPE/MPPC encryption/compression module registered\n");
1566 +    }
1567 +    return answer;
1568 +}
1569 +
1570 +void __exit mppe_module_cleanup(void)
1571 +{
1572 +    kfree(sha_pad);
1573 +    ppp_unregister_compressor(&ppp_mppe);
1574 +    printk(KERN_INFO "MPPE/MPPC encryption/compression module unregistered\n");
1575 +}
1576 +
1577 +module_init(mppe_module_init);
1578 +module_exit(mppe_module_cleanup);
1579 +
1580 +MODULE_AUTHOR("Jan Dubiec <jdx@slackware.pl>");
1581 +MODULE_DESCRIPTION("MPPE/MPPC encryption/compression module for Linux");
1582 +MODULE_LICENSE("Dual BSD/GPL");
1583 Index: linux-2.4.35.4/include/linux/ppp-comp.h
1584 ===================================================================
1585 --- linux-2.4.35.4.orig/include/linux/ppp-comp.h
1586 +++ linux-2.4.35.4/include/linux/ppp-comp.h
1587 @@ -28,7 +28,7 @@
1588   */
1589  
1590  /*
1591 - *  ==FILEVERSION 980319==
1592 + *  ==FILEVERSION 20040509==
1593   *
1594   *  NOTE TO MAINTAINERS:
1595   *     If you modify this file at all, please set the above date.
1596 @@ -78,7 +78,7 @@ struct compressor {
1597  
1598         /* Compress a packet */
1599         int     (*compress) (void *state, unsigned char *rptr,
1600 -                             unsigned char *obuf, int isize, int osize);
1601 +                            unsigned char *obuf, int isize, int osize);
1602  
1603         /* Return compression statistics */
1604         void    (*comp_stat) (void *state, struct compstat *stats);
1605 @@ -99,7 +99,7 @@ struct compressor {
1606  
1607         /* Decompress a packet. */
1608         int     (*decompress) (void *state, unsigned char *ibuf, int isize,
1609 -                               unsigned char *obuf, int osize);
1610 +                              unsigned char *obuf, int osize);
1611  
1612         /* Update state for an incompressible packet received */
1613         void    (*incomp) (void *state, unsigned char *ibuf, int icnt);
1614 @@ -187,6 +187,42 @@ struct compressor {
1615  #define DEFLATE_CHK_SEQUENCE   0
1616  
1617  /*
1618 + * Definitions for MPPE/MPPC.
1619 + */
1620 +
1621 +#define CI_MPPE                        18      /* config option for MPPE */
1622 +#define CILEN_MPPE             6       /* length of config option */
1623 +
1624 +#define MPPE_OVHD              4       /* MPPE overhead */
1625 +#define MPPE_MAX_KEY_LEN       16      /* largest key length (128-bit) */
1626 +
1627 +#define MPPE_STATELESS          0x01   /* configuration bit H */
1628 +#define MPPE_40BIT              0x20   /* configuration bit L */
1629 +#define MPPE_56BIT              0x80   /* configuration bit M */
1630 +#define MPPE_128BIT             0x40   /* configuration bit S */
1631 +#define MPPE_MPPC               0x01   /* configuration bit C */
1632 +
1633 +/*
1634 + * Definitions for Stac LZS.
1635 + */
1636 +
1637 +#define CI_LZS                 17      /* config option for Stac LZS */
1638 +#define CILEN_LZS              5       /* length of config option */
1639 +
1640 +#define LZS_OVHD               4       /* max. LZS overhead */
1641 +#define LZS_HIST_LEN           2048    /* LZS history size */
1642 +#define LZS_MAX_CCOUNT         0x0FFF  /* max. coherency counter value */
1643 +
1644 +#define LZS_MODE_NONE          0
1645 +#define LZS_MODE_LCB           1
1646 +#define LZS_MODE_CRC           2
1647 +#define LZS_MODE_SEQ           3
1648 +#define LZS_MODE_EXT           4
1649 +
1650 +#define LZS_EXT_BIT_FLUSHED    0x80    /* bit A */
1651 +#define LZS_EXT_BIT_COMP       0x20    /* bit C */
1652 +
1653 +/*
1654   * Definitions for other, as yet unsupported, compression methods.
1655   */
1656