]> git.ozlabs.org Git - ppp.git/blob - linux/mppe/ppp_mppe_compress.c
Corrected version of MPPE kernel support (Frank Cusack)
[ppp.git] / linux / mppe / ppp_mppe_compress.c
1 /*
2  *  ==FILEVERSION 20020320==
3  *
4  * ppp_mppe_compress.c - interface MPPE to the PPP code.
5  * This version is for use with Linux kernel 2.2.19+ and 2.4.x.
6  *
7  * By Frank Cusack <frank@google.com>.
8  * Copyright (c) 2002 Google, Inc.
9  * All rights reserved.
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation is hereby granted, provided that the above copyright
13  * notice appears in all copies.  This software is provided without any
14  * warranty, express or implied.
15  *
16  */
17
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24
25 #include <linux/ppp_defs.h>
26 #include <linux/ppp-comp.h>
27
28 #include "arcfour.h"
29 #include "sha1.h"
30
31 /*
32  * State for an MPPE (de)compressor.
33  */
34 typedef struct ppp_mppe_state {
35     unsigned char       master_key[MPPE_MAX_KEY_LEN];
36     unsigned char       session_key[MPPE_MAX_KEY_LEN];
37     arcfour_context     arcfour_context; /* encryption state */
38     unsigned            keylen;         /* key length in bytes             */
39                                         /* NB: 128-bit == 16, 40-bit == 8! */
40                                         /* If we want to support 56-bit,   */
41                                         /* the unit has to change to bits  */
42     unsigned char       bits;           /* MPPE control bits */
43     unsigned            ccount;         /* 12-bit coherency count (seqno)  */
44     unsigned            stateful;       /* stateful mode flag */
45     int                 discard;        /* stateful mode packet loss flag */
46     int                 sanity_errors;  /* take down LCP if too many */
47     int                 unit;
48     int                 debug;
49     struct compstat     stats;
50 } ppp_mppe_state;
51
52 /* ppp_mppe_state.bits definitions */
53 #define MPPE_BIT_A      0x80    /* Encryption table were (re)inititalized */
54 #define MPPE_BIT_B      0x40    /* MPPC only (not implemented) */
55 #define MPPE_BIT_C      0x20    /* MPPC only (not implemented) */
56 #define MPPE_BIT_D      0x10    /* This is an encrypted frame */
57
58 #define MPPE_BIT_FLUSHED        MPPE_BIT_A
59 #define MPPE_BIT_ENCRYPTED      MPPE_BIT_D
60
61 #define MPPE_BITS(p) ((p)[4] & 0xf0)
62 #define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
63 #define MPPE_CCOUNT_SPACE 0x1000        /* The size of the ccount space */
64
65 /*
66  * MPPE overhead/packet.
67  * Note that we use this differently than other compressors.
68  */
69 #define MPPE_OVHD       2               /* MPPE overhead/packet */
70 /* Max bogon factor we will tolerate */
71 #define SANITY_MAX      1600
72
73 static void     GetNewKeyFromSHA __P((unsigned char *StartKey,
74                                       unsigned char *SessionKey,
75                                       unsigned SessionKeyLength,
76                                       unsigned char *InterimKey));
77 static void     mppe_rekey __P((ppp_mppe_state *state, int));
78 static void     *mppe_alloc __P((unsigned char *options, int optlen));
79 static void     mppe_free __P((void *state));
80 static int      mppe_init __P((void *state, unsigned char *options,
81                                int optlen, int unit, int debug, const char *));
82 static int      mppe_comp_init __P((void *state, unsigned char *options,
83                                     int optlen,
84                                     int unit, int hdrlen, int debug));
85 static int      mppe_decomp_init __P((void *state, unsigned char *options,
86                                       int optlen, int unit,
87                                       int hdrlen, int mru, int debug));
88 static int      mppe_compress __P((void *state, unsigned char *ibuf,
89                                    unsigned char *obuf,
90                                    int isize, int osize));
91 static void     mppe_incomp __P((void *state, unsigned char *ibuf, int icnt));
92 static int      mppe_decompress __P((void *state, unsigned char *ibuf,
93                                      int isize, unsigned char *obuf,int osize));
94 static void     mppe_comp_reset __P((void *state));
95 static void     mppe_decomp_reset __P((void *state));
96 static void     mppe_comp_stats __P((void *state, struct compstat *stats));
97
98
99 /*
100  * Key Derivation, from RFC 3078, RFC 3079.
101  * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
102  */
103 static void
104 GetNewKeyFromSHA(unsigned char *MasterKey, unsigned char *SessionKey,
105                  unsigned SessionKeyLength, unsigned char *InterimKey)
106 {
107     SHA1_CTX Context;
108     unsigned char Digest[SHA1_SIGNATURE_SIZE];
109
110     unsigned char SHApad1[40] =
111     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
115     unsigned char SHApad2[40] =
116     { 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
117       0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
118       0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
119       0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2 };
120
121     /* assert(SessionKeyLength <= SHA1_SIGNATURE_SIZE); */
122
123     SHA1_Init(&Context);
124     SHA1_Update(&Context, MasterKey, SessionKeyLength);
125     SHA1_Update(&Context, SHApad1, sizeof(SHApad1));
126     SHA1_Update(&Context, SessionKey, SessionKeyLength);
127     SHA1_Update(&Context, SHApad2, sizeof(SHApad2));
128     SHA1_Final(Digest, &Context);
129
130     memcpy(InterimKey, Digest, SessionKeyLength);
131 }
132
133 /*
134  * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
135  * Well, not what's written there, but rather what they meant.
136  */
137 static void
138 mppe_rekey(ppp_mppe_state *state, int initial_key)
139 {
140     unsigned char InterimKey[MPPE_MAX_KEY_LEN];
141
142     GetNewKeyFromSHA(state->master_key, state->session_key,
143                      state->keylen, InterimKey);
144     if (!initial_key) {
145         arcfour_setkey(&state->arcfour_context, InterimKey, state->keylen);
146         arcfour_encrypt(&state->arcfour_context, InterimKey, state->keylen,
147                         state->session_key);
148     } else {
149         memcpy(state->session_key, InterimKey, state->keylen);
150     }
151     if (state->keylen == 8) {
152         /* See RFC 3078 */
153         state->session_key[0] = 0xd1;
154         state->session_key[1] = 0x26;
155         state->session_key[2] = 0x9e;
156     }
157     arcfour_setkey(&state->arcfour_context, state->session_key, state->keylen);
158 }
159
160
161 /*
162  * Allocate space for a (de)compressor.
163  */
164 static void *
165 mppe_alloc(unsigned char *options, int optlen)
166 {
167     ppp_mppe_state *state;
168
169     if (optlen != CILEN_MPPE + sizeof(state->master_key)
170         || options[0] != CI_MPPE
171         || options[1] != CILEN_MPPE)
172         return NULL;
173
174     state = (ppp_mppe_state *) kmalloc(sizeof(*state), GFP_KERNEL);
175     if (state == NULL)
176         return NULL;
177
178     MOD_INC_USE_COUNT;
179     memset(state, 0, sizeof(*state));
180
181     /* Save keys. */
182     memcpy(state->master_key, &options[CILEN_MPPE], sizeof(state->master_key));
183     memcpy(state->session_key, state->master_key, sizeof(state->master_key));
184     /*
185      * We defer initial key generation until mppe_init(), as mppe_alloc()
186      * is called frequently during negotiation.
187      */
188
189     return (void *) state;
190 }
191
192 /*
193  * Deallocate space for a (de)compressor.
194  */
195 static void
196 mppe_free(void *arg)
197 {
198     ppp_mppe_state *state = (ppp_mppe_state *) arg;
199
200     if (state) {
201         kfree(state);
202         MOD_DEC_USE_COUNT;
203     }
204 }
205
206
207 /* 
208  * Initialize (de)compressor state.
209  */
210 static int
211 mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
212           const char *debugstr)
213 {
214     ppp_mppe_state *state = (ppp_mppe_state *) arg;
215     unsigned char mppe_opts;
216
217     if (optlen != CILEN_MPPE
218         || options[0] != CI_MPPE
219         || options[1] != CILEN_MPPE)
220         return 0;
221
222     MPPE_CI_TO_OPTS(&options[2], mppe_opts);
223     if (mppe_opts & MPPE_OPT_128)
224         state->keylen = 16;
225     else if (mppe_opts & MPPE_OPT_40)
226         state->keylen = 8;
227     else {
228         printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr, unit);
229         return 0;
230     }
231     if (mppe_opts & MPPE_OPT_STATEFUL)
232         state->stateful = 1;
233
234     /* Generate the initial session key. */
235     mppe_rekey(state, 1);
236
237     if (debug) {
238         int i;
239         char mkey[sizeof(state->master_key) * 3 + 1];
240         char skey[sizeof(state->session_key) * 3 + 1];
241
242         printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n", debugstr,
243                unit, (state->keylen == 16)? 128: 40,
244                (state->stateful)? "stateful": "stateless");
245
246         for (i = 0; i < sizeof(state->master_key); i++)
247             sprintf(mkey + i * 2, "%.2x ", state->master_key[i]);
248         for (i = 0; i < sizeof(state->session_key); i++)
249             sprintf(skey + i * 2, "%.2x ", state->session_key[i]);
250         printk(KERN_DEBUG "%s[%d]: keys: master: %s initial session: %s\n",
251                debugstr, unit, mkey, skey);
252     }
253
254     /*
255      * Initialize the coherency count.  The initial value is not specified
256      * in RFC 3078, but we can make a reasonable assumption that it will
257      * start at 0.  Setting it to the max here makes the comp/decomp code
258      * do the right thing (determined through experiment).
259      */
260     state->ccount = MPPE_CCOUNT_SPACE - 1;
261
262     /*
263      * Note that even though we have initialized the key table, we don't
264      * set the FLUSHED bit.  This is contrary to RFC 3078, sec. 3.1.
265      */
266     state->bits = MPPE_BIT_ENCRYPTED;
267
268     state->unit  = unit;
269     state->debug = debug;
270
271     return 1;
272 }
273
274
275
276 static int
277 mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
278                int hdrlen, int debug)
279 {
280     /* ARGSUSED */
281     return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
282 }
283
284 /*
285  * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
286  * tell the compressor to rekey.  Note that we MUST NOT rekey for
287  * every CCP Reset-Request; we only rekey on the next xmit packet.
288  * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
289  * So, rekeying for every CCP Reset-Request is broken as the peer will not
290  * know how many times we've rekeyed.  (If we rekey and THEN get another
291  * CCP Reset-Request, we must rekey again.)
292  */
293 static void
294 mppe_comp_reset(void *arg)
295 {
296     ppp_mppe_state *state = (ppp_mppe_state *) arg;
297
298     state->bits |= MPPE_BIT_FLUSHED;
299 }
300
301 /*
302  * Compress (encrypt) a packet.
303  * It's strange to call this a compressor, since the output is always
304  * MPPE_OVHD + 2 bytes larger than the input.
305  */
306 int
307 mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
308               int isize, int osize)
309 {
310     ppp_mppe_state *state = (ppp_mppe_state *) arg;
311     int proto;
312
313     /*
314      * Check that the protocol is in the range we handle.
315      */
316     proto = PPP_PROTOCOL(ibuf);
317     if (proto < 0x0021 || proto > 0x00fa)
318         return 0;
319
320     /* Make sure we have enough room to generate an encrypted packet. */
321     if (osize < isize + MPPE_OVHD + 2) {
322         /* Drop the packet if we should encrypt it, but can't. */
323         printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
324                "(have: %d need: %d)\n", state->unit,
325                osize, osize + MPPE_OVHD + 2);
326         return -1;
327     }
328
329     osize = isize + MPPE_OVHD + 2;
330
331     /*
332      * Copy over the PPP header and set control bits.
333      */
334     obuf[0] = PPP_ADDRESS(ibuf);
335     obuf[1] = PPP_CONTROL(ibuf);
336     obuf[2] = PPP_COMP >> 8;            /* isize + MPPE_OVHD + 1 */
337     obuf[3] = PPP_COMP;                 /* isize + MPPE_OVHD + 2 */
338     obuf += PPP_HDRLEN;
339
340     state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
341     obuf[0] = state->ccount >> 8;
342     obuf[1] = state->ccount & 0xff;
343
344     if (!state->stateful ||                     /* stateless mode     */
345         ((state->ccount & 0xff) == 0xff) ||     /* "flag" packet      */
346         (state->bits & MPPE_BIT_FLUSHED)) {     /* CCP Reset-Request  */
347         /* We must rekey */
348         if (state->debug && state->stateful)
349             printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n", state->unit);
350         mppe_rekey(state, 0);
351         state->bits |= MPPE_BIT_FLUSHED;
352     }
353     obuf[0] |= state->bits;
354     state->bits &= ~MPPE_BIT_FLUSHED;   /* reset for next xmit */
355
356     obuf  += MPPE_OVHD;
357     ibuf  += 2; /* skip to proto field */
358     isize -= 2;
359
360     /* Encrypt packet */
361     arcfour_encrypt(&state->arcfour_context, ibuf, isize, obuf);
362
363     state->stats.unc_bytes += isize;
364     state->stats.unc_packets++;
365     state->stats.comp_bytes += osize;
366     state->stats.comp_packets++;
367
368     return osize;
369 }
370
371 /*
372  * Since every frame grows by MPPE_OVHD + 2 bytes, this is always going
373  * to look bad ... and the longer the link is up the worse it will get.
374  */
375 static void
376 mppe_comp_stats(void *arg, struct compstat *stats)
377 {
378     ppp_mppe_state *state = (ppp_mppe_state *) arg;
379
380     *stats = state->stats;
381 }
382
383
384 static int
385 mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
386                  int hdrlen, int mru, int debug)
387 {
388     /* ARGSUSED */
389     return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
390 }
391
392 /*
393  * We received a CCP Reset-Ack.  Just ignore it.
394  */
395 static void
396 mppe_decomp_reset(void *arg)
397 {
398     /* ARGSUSED */
399     return;
400 }
401
402 /*
403  * Decompress (decrypt) an MPPE packet.
404  */
405 int
406 mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
407                 int osize)
408 {
409     ppp_mppe_state *state = (ppp_mppe_state *) arg;
410     unsigned ccount;
411     int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
412     int sanity = 0;
413
414     if (isize <= PPP_HDRLEN + MPPE_OVHD) {
415         if (state->debug)
416             printk(KERN_DEBUG "mppe_decompress[%d]: short pkt (%d)\n",
417                    state->unit, isize);
418         return DECOMP_ERROR;
419     }
420     /* Strange ... our output size is always LESS than the input size. */
421     /* assert(osize >= isize - MPPE_OVHD - 2); */
422
423     osize = isize - MPPE_OVHD - 2;
424
425     ccount = MPPE_CCOUNT(ibuf);
426
427     /* sanity checks -- terminate with extreme prejudice */
428     if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
429         printk(KERN_DEBUG "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
430                state->unit);
431         state->sanity_errors += 100;
432         sanity = 1;
433     }
434     if (!state->stateful && !flushed) {
435         printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
436                "stateless mode!\n", state->unit);
437         state->sanity_errors += 100;
438         sanity = 1;
439     }
440     if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
441         printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
442                "flag packet!\n", state->unit);
443         state->sanity_errors += 100;
444         sanity = 1;
445     }
446
447     if (sanity) {
448         if (state->sanity_errors < SANITY_MAX)
449             return DECOMP_ERROR;
450         else
451             /*
452              * Take LCP down if the peer is sending too many bogons.
453              * We don't want to do this for a single or just a few
454              * instances since it could just be due to packet corruption.
455              */
456             return DECOMP_FATALERROR;
457     }
458
459     /*
460      * Check the coherency count.
461      */
462
463     if (!state->stateful) {
464         /* RFC 3078, sec 8.1.  Rekey for every packet. */
465         while (state->ccount != ccount) {
466             mppe_rekey(state, 0);
467             state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
468         }
469     } else {
470         /* RFC 3078, sec 8.2. */
471         if (!state->discard) {
472             /* normal state */
473             state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
474             if (ccount != state->ccount) {
475                 /*
476                  * (ccount > state->ccount)
477                  * Packet loss detected, enter the discard state.
478                  * Signal the peer to rekey (by sending a CCP Reset-Request).
479                  */
480                 state->discard = 1;
481                 return DECOMP_ERROR;
482             }
483         } else {
484             /* discard state */
485            if (!flushed) {
486                 /* ccp.c will be silent (no additional CCP Reset-Requests). */
487                 return DECOMP_ERROR;
488             } else {
489                 /* Rekey for every missed "flag" packet. */
490                 while ((ccount & ~0xff) != (state->ccount & ~0xff)) {
491                     mppe_rekey(state, 0);
492                     state->ccount = (state->ccount + 256) % MPPE_CCOUNT_SPACE;
493                 }
494
495                 /* reset */
496                 state->discard = 0;
497                 state->ccount = ccount;
498                 /*
499                  * Another problem with RFC 3078 here.  It implies that the
500                  * peer need not send a Reset-Ack packet.  But RFC 1962
501                  * requires it.  Hopefully, M$ does send a Reset-Ack; even
502                  * though it isn't required for MPPE synchronization, it is
503                  * required to reset CCP state.
504                  */
505             }
506         }
507         if (flushed)
508             mppe_rekey(state, 0);
509     }
510
511     /*
512      * Fill in the first part of the PPP header.  The protocol field
513      * comes from the decrypted data.
514      */
515     obuf[0] = PPP_ADDRESS(ibuf);        /* +1 */
516     obuf[1] = PPP_CONTROL(ibuf);        /* +2 */
517     obuf  += 2;
518     ibuf  += PPP_HDRLEN + MPPE_OVHD;
519     isize -= PPP_HDRLEN + MPPE_OVHD;    /* -6 */
520                                         /* net: -4 */
521
522     /* And finally, decrypt the packet. */
523     arcfour_decrypt(&state->arcfour_context, ibuf, isize, obuf);
524
525     state->stats.unc_bytes += osize;
526     state->stats.unc_packets++;
527     state->stats.comp_bytes += isize;
528     state->stats.comp_packets++;
529
530     /* good packet credit */
531     state->sanity_errors >>= 1;
532
533     return osize;
534 }
535
536 /*
537  * Incompressible data has arrived (this should never happen!).
538  * We should probably drop the link if the protocol is in the range
539  * of what should be encrypted.  At the least, we should drop this
540  * packet.  (How to do this?)
541  */
542 static void
543 mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
544 {
545     ppp_mppe_state *state = (ppp_mppe_state *) arg;
546
547     if (state->debug &&
548         (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
549         printk(KERN_DEBUG "mppe_incomp[%d]: incompressible (unencrypted) data! "
550                "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
551
552     state->stats.inc_bytes += icnt;
553     state->stats.inc_packets++;
554     state->stats.unc_bytes += icnt;
555     state->stats.unc_packets++;
556 }
557
558 /*************************************************************
559  * Module interface table
560  *************************************************************/
561
562 /* These are in ppp.c (2.2.x) or ppp_generic.c (2.4.x) */
563 extern int  ppp_register_compressor   (struct compressor *cp);
564 extern void ppp_unregister_compressor (struct compressor *cp);
565
566 /*
567  * Procedures exported to if_ppp.c.
568  */
569 struct compressor ppp_mppe = {
570     CI_MPPE,            /* compress_proto */
571     mppe_alloc,         /* comp_alloc */
572     mppe_free,          /* comp_free */
573     mppe_comp_init,     /* comp_init */
574     mppe_comp_reset,    /* comp_reset */
575     mppe_compress,      /* compress */
576     mppe_comp_stats,    /* comp_stat */
577     mppe_alloc,         /* decomp_alloc */
578     mppe_free,          /* decomp_free */
579     mppe_decomp_init,   /* decomp_init */
580     mppe_decomp_reset,  /* decomp_reset */
581     mppe_decompress,    /* decompress */
582     mppe_incomp,        /* incomp */
583     mppe_comp_stats,    /* decomp_stat */
584 };
585
586 /* 2.2 compatibility defines */
587 #ifndef __init
588 #define __init
589 #endif
590 #ifndef __exit
591 #define __exit
592 #endif
593 #ifndef MODULE_LICENSE
594 #define MODULE_LICENSE(license)
595 #endif
596
597 int __init
598 ppp_mppe_init(void)
599 {  
600     int answer = ppp_register_compressor(&ppp_mppe);
601
602     if (answer == 0)
603         printk(KERN_INFO "PPP MPPE Compression module registered\n");
604     return answer;
605 }
606
607 void __exit
608 ppp_mppe_cleanup(void)
609 {
610     ppp_unregister_compressor(&ppp_mppe);
611 }
612
613 module_init(ppp_mppe_init);
614 module_exit(ppp_mppe_cleanup);
615 MODULE_LICENSE("BSD without advertisement clause");