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