Loading...
1/*
2 * ppp_mppe.c - interface MPPE to the PPP code.
3 * This version is for use with Linux kernel 2.6.14+
4 *
5 * By Frank Cusack <fcusack@fcusack.com>.
6 * Copyright (c) 2002,2003,2004 Google, Inc.
7 * All rights reserved.
8 *
9 * License:
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation is hereby granted, provided that the above copyright
12 * notice appears in all copies. This software is provided without any
13 * warranty, express or implied.
14 *
15 * ALTERNATIVELY, provided that this notice is retained in full, this product
16 * may be distributed under the terms of the GNU General Public License (GPL),
17 * in which case the provisions of the GPL apply INSTEAD OF those given above.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 *
32 *
33 * Changelog:
34 * 08/12/05 - Matt Domsch <Matt_Domsch@dell.com>
35 * Only need extra skb padding on transmit, not receive.
36 * 06/18/04 - Matt Domsch <Matt_Domsch@dell.com>, Oleg Makarenko <mole@quadra.ru>
37 * Use Linux kernel 2.6 arc4 and sha1 routines rather than
38 * providing our own.
39 * 2/15/04 - TS: added #include <version.h> and testing for Kernel
40 * version before using
41 * MOD_DEC_USAGE_COUNT/MOD_INC_USAGE_COUNT which are
42 * deprecated in 2.6
43 */
44
45#include <crypto/arc4.h>
46#include <crypto/hash.h>
47#include <linux/err.h>
48#include <linux/fips.h>
49#include <linux/module.h>
50#include <linux/kernel.h>
51#include <linux/init.h>
52#include <linux/types.h>
53#include <linux/slab.h>
54#include <linux/string.h>
55#include <linux/mm.h>
56#include <linux/ppp_defs.h>
57#include <linux/ppp-comp.h>
58#include <linux/scatterlist.h>
59#include <asm/unaligned.h>
60
61#include "ppp_mppe.h"
62
63MODULE_AUTHOR("Frank Cusack <fcusack@fcusack.com>");
64MODULE_DESCRIPTION("Point-to-Point Protocol Microsoft Point-to-Point Encryption support");
65MODULE_LICENSE("Dual BSD/GPL");
66MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
67MODULE_VERSION("1.0.2");
68
69#define SHA1_PAD_SIZE 40
70
71/*
72 * kernel crypto API needs its arguments to be in kmalloc'd memory, not in the module
73 * static data area. That means sha_pad needs to be kmalloc'd.
74 */
75
76struct sha_pad {
77 unsigned char sha_pad1[SHA1_PAD_SIZE];
78 unsigned char sha_pad2[SHA1_PAD_SIZE];
79};
80static struct sha_pad *sha_pad;
81
82static inline void sha_pad_init(struct sha_pad *shapad)
83{
84 memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1));
85 memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2));
86}
87
88/*
89 * State for an MPPE (de)compressor.
90 */
91struct ppp_mppe_state {
92 struct arc4_ctx arc4;
93 struct shash_desc *sha1;
94 unsigned char *sha1_digest;
95 unsigned char master_key[MPPE_MAX_KEY_LEN];
96 unsigned char session_key[MPPE_MAX_KEY_LEN];
97 unsigned keylen; /* key length in bytes */
98 /* NB: 128-bit == 16, 40-bit == 8! */
99 /* If we want to support 56-bit, */
100 /* the unit has to change to bits */
101 unsigned char bits; /* MPPE control bits */
102 unsigned ccount; /* 12-bit coherency count (seqno) */
103 unsigned stateful; /* stateful mode flag */
104 int discard; /* stateful mode packet loss flag */
105 int sanity_errors; /* take down LCP if too many */
106 int unit;
107 int debug;
108 struct compstat stats;
109};
110
111/* struct ppp_mppe_state.bits definitions */
112#define MPPE_BIT_A 0x80 /* Encryption table were (re)inititalized */
113#define MPPE_BIT_B 0x40 /* MPPC only (not implemented) */
114#define MPPE_BIT_C 0x20 /* MPPC only (not implemented) */
115#define MPPE_BIT_D 0x10 /* This is an encrypted frame */
116
117#define MPPE_BIT_FLUSHED MPPE_BIT_A
118#define MPPE_BIT_ENCRYPTED MPPE_BIT_D
119
120#define MPPE_BITS(p) ((p)[4] & 0xf0)
121#define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
122#define MPPE_CCOUNT_SPACE 0x1000 /* The size of the ccount space */
123
124#define MPPE_OVHD 2 /* MPPE overhead/packet */
125#define SANITY_MAX 1600 /* Max bogon factor we will tolerate */
126
127/*
128 * Key Derivation, from RFC 3078, RFC 3079.
129 * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
130 */
131static void get_new_key_from_sha(struct ppp_mppe_state * state)
132{
133 crypto_shash_init(state->sha1);
134 crypto_shash_update(state->sha1, state->master_key,
135 state->keylen);
136 crypto_shash_update(state->sha1, sha_pad->sha_pad1,
137 sizeof(sha_pad->sha_pad1));
138 crypto_shash_update(state->sha1, state->session_key,
139 state->keylen);
140 crypto_shash_update(state->sha1, sha_pad->sha_pad2,
141 sizeof(sha_pad->sha_pad2));
142 crypto_shash_final(state->sha1, state->sha1_digest);
143}
144
145/*
146 * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
147 * Well, not what's written there, but rather what they meant.
148 */
149static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
150{
151 get_new_key_from_sha(state);
152 if (!initial_key) {
153 arc4_setkey(&state->arc4, state->sha1_digest, state->keylen);
154 arc4_crypt(&state->arc4, state->session_key, state->sha1_digest,
155 state->keylen);
156 } else {
157 memcpy(state->session_key, state->sha1_digest, state->keylen);
158 }
159 if (state->keylen == 8) {
160 /* See RFC 3078 */
161 state->session_key[0] = 0xd1;
162 state->session_key[1] = 0x26;
163 state->session_key[2] = 0x9e;
164 }
165 arc4_setkey(&state->arc4, state->session_key, state->keylen);
166}
167
168/*
169 * Allocate space for a (de)compressor.
170 */
171static void *mppe_alloc(unsigned char *options, int optlen)
172{
173 struct ppp_mppe_state *state;
174 struct crypto_shash *shash;
175 unsigned int digestsize;
176
177 if (optlen != CILEN_MPPE + sizeof(state->master_key) ||
178 options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
179 fips_enabled)
180 goto out;
181
182 state = kzalloc(sizeof(*state), GFP_KERNEL);
183 if (state == NULL)
184 goto out;
185
186
187 shash = crypto_alloc_shash("sha1", 0, 0);
188 if (IS_ERR(shash))
189 goto out_free;
190
191 state->sha1 = kmalloc(sizeof(*state->sha1) +
192 crypto_shash_descsize(shash),
193 GFP_KERNEL);
194 if (!state->sha1) {
195 crypto_free_shash(shash);
196 goto out_free;
197 }
198 state->sha1->tfm = shash;
199
200 digestsize = crypto_shash_digestsize(shash);
201 if (digestsize < MPPE_MAX_KEY_LEN)
202 goto out_free;
203
204 state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
205 if (!state->sha1_digest)
206 goto out_free;
207
208 /* Save keys. */
209 memcpy(state->master_key, &options[CILEN_MPPE],
210 sizeof(state->master_key));
211 memcpy(state->session_key, state->master_key,
212 sizeof(state->master_key));
213
214 /*
215 * We defer initial key generation until mppe_init(), as mppe_alloc()
216 * is called frequently during negotiation.
217 */
218
219 return (void *)state;
220
221out_free:
222 kfree(state->sha1_digest);
223 if (state->sha1) {
224 crypto_free_shash(state->sha1->tfm);
225 kfree_sensitive(state->sha1);
226 }
227 kfree(state);
228out:
229 return NULL;
230}
231
232/*
233 * Deallocate space for a (de)compressor.
234 */
235static void mppe_free(void *arg)
236{
237 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
238 if (state) {
239 kfree(state->sha1_digest);
240 crypto_free_shash(state->sha1->tfm);
241 kfree_sensitive(state->sha1);
242 kfree_sensitive(state);
243 }
244}
245
246/*
247 * Initialize (de)compressor state.
248 */
249static int
250mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
251 const char *debugstr)
252{
253 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
254 unsigned char mppe_opts;
255
256 if (optlen != CILEN_MPPE ||
257 options[0] != CI_MPPE || options[1] != CILEN_MPPE)
258 return 0;
259
260 MPPE_CI_TO_OPTS(&options[2], mppe_opts);
261 if (mppe_opts & MPPE_OPT_128)
262 state->keylen = 16;
263 else if (mppe_opts & MPPE_OPT_40)
264 state->keylen = 8;
265 else {
266 printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
267 unit);
268 return 0;
269 }
270 if (mppe_opts & MPPE_OPT_STATEFUL)
271 state->stateful = 1;
272
273 /* Generate the initial session key. */
274 mppe_rekey(state, 1);
275
276 if (debug) {
277 printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
278 debugstr, unit, (state->keylen == 16) ? 128 : 40,
279 (state->stateful) ? "stateful" : "stateless");
280 printk(KERN_DEBUG
281 "%s[%d]: keys: master: %*phN initial session: %*phN\n",
282 debugstr, unit,
283 (int)sizeof(state->master_key), state->master_key,
284 (int)sizeof(state->session_key), state->session_key);
285 }
286
287 /*
288 * Initialize the coherency count. The initial value is not specified
289 * in RFC 3078, but we can make a reasonable assumption that it will
290 * start at 0. Setting it to the max here makes the comp/decomp code
291 * do the right thing (determined through experiment).
292 */
293 state->ccount = MPPE_CCOUNT_SPACE - 1;
294
295 /*
296 * Note that even though we have initialized the key table, we don't
297 * set the FLUSHED bit. This is contrary to RFC 3078, sec. 3.1.
298 */
299 state->bits = MPPE_BIT_ENCRYPTED;
300
301 state->unit = unit;
302 state->debug = debug;
303
304 return 1;
305}
306
307static int
308mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
309 int hdrlen, int debug)
310{
311 /* ARGSUSED */
312 return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
313}
314
315/*
316 * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
317 * tell the compressor to rekey. Note that we MUST NOT rekey for
318 * every CCP Reset-Request; we only rekey on the next xmit packet.
319 * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
320 * So, rekeying for every CCP Reset-Request is broken as the peer will not
321 * know how many times we've rekeyed. (If we rekey and THEN get another
322 * CCP Reset-Request, we must rekey again.)
323 */
324static void mppe_comp_reset(void *arg)
325{
326 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
327
328 state->bits |= MPPE_BIT_FLUSHED;
329}
330
331/*
332 * Compress (encrypt) a packet.
333 * It's strange to call this a compressor, since the output is always
334 * MPPE_OVHD + 2 bytes larger than the input.
335 */
336static int
337mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
338 int isize, int osize)
339{
340 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
341 int proto;
342
343 /*
344 * Check that the protocol is in the range we handle.
345 */
346 proto = PPP_PROTOCOL(ibuf);
347 if (proto < 0x0021 || proto > 0x00fa)
348 return 0;
349
350 /* Make sure we have enough room to generate an encrypted packet. */
351 if (osize < isize + MPPE_OVHD + 2) {
352 /* Drop the packet if we should encrypt it, but can't. */
353 printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
354 "(have: %d need: %d)\n", state->unit,
355 osize, osize + MPPE_OVHD + 2);
356 return -1;
357 }
358
359 osize = isize + MPPE_OVHD + 2;
360
361 /*
362 * Copy over the PPP header and set control bits.
363 */
364 obuf[0] = PPP_ADDRESS(ibuf);
365 obuf[1] = PPP_CONTROL(ibuf);
366 put_unaligned_be16(PPP_COMP, obuf + 2);
367 obuf += PPP_HDRLEN;
368
369 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
370 if (state->debug >= 7)
371 printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
372 state->ccount);
373 put_unaligned_be16(state->ccount, obuf);
374
375 if (!state->stateful || /* stateless mode */
376 ((state->ccount & 0xff) == 0xff) || /* "flag" packet */
377 (state->bits & MPPE_BIT_FLUSHED)) { /* CCP Reset-Request */
378 /* We must rekey */
379 if (state->debug && state->stateful)
380 printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
381 state->unit);
382 mppe_rekey(state, 0);
383 state->bits |= MPPE_BIT_FLUSHED;
384 }
385 obuf[0] |= state->bits;
386 state->bits &= ~MPPE_BIT_FLUSHED; /* reset for next xmit */
387
388 obuf += MPPE_OVHD;
389 ibuf += 2; /* skip to proto field */
390 isize -= 2;
391
392 arc4_crypt(&state->arc4, obuf, ibuf, isize);
393
394 state->stats.unc_bytes += isize;
395 state->stats.unc_packets++;
396 state->stats.comp_bytes += osize;
397 state->stats.comp_packets++;
398
399 return osize;
400}
401
402/*
403 * Since every frame grows by MPPE_OVHD + 2 bytes, this is always going
404 * to look bad ... and the longer the link is up the worse it will get.
405 */
406static void mppe_comp_stats(void *arg, struct compstat *stats)
407{
408 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
409
410 *stats = state->stats;
411}
412
413static int
414mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
415 int hdrlen, int mru, int debug)
416{
417 /* ARGSUSED */
418 return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
419}
420
421/*
422 * We received a CCP Reset-Ack. Just ignore it.
423 */
424static void mppe_decomp_reset(void *arg)
425{
426 /* ARGSUSED */
427 return;
428}
429
430/*
431 * Decompress (decrypt) an MPPE packet.
432 */
433static int
434mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
435 int osize)
436{
437 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
438 unsigned ccount;
439 int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
440
441 if (isize <= PPP_HDRLEN + MPPE_OVHD) {
442 if (state->debug)
443 printk(KERN_DEBUG
444 "mppe_decompress[%d]: short pkt (%d)\n",
445 state->unit, isize);
446 return DECOMP_ERROR;
447 }
448
449 /*
450 * Make sure we have enough room to decrypt the packet.
451 * Note that for our test we only subtract 1 byte whereas in
452 * mppe_compress() we added 2 bytes (+MPPE_OVHD);
453 * this is to account for possible PFC.
454 */
455 if (osize < isize - MPPE_OVHD - 1) {
456 printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
457 "(have: %d need: %d)\n", state->unit,
458 osize, isize - MPPE_OVHD - 1);
459 return DECOMP_ERROR;
460 }
461 osize = isize - MPPE_OVHD - 2; /* assume no PFC */
462
463 ccount = MPPE_CCOUNT(ibuf);
464 if (state->debug >= 7)
465 printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
466 state->unit, ccount);
467
468 /* sanity checks -- terminate with extreme prejudice */
469 if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
470 printk(KERN_DEBUG
471 "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
472 state->unit);
473 state->sanity_errors += 100;
474 goto sanity_error;
475 }
476 if (!state->stateful && !flushed) {
477 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
478 "stateless mode!\n", state->unit);
479 state->sanity_errors += 100;
480 goto sanity_error;
481 }
482 if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
483 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
484 "flag packet!\n", state->unit);
485 state->sanity_errors += 100;
486 goto sanity_error;
487 }
488
489 /*
490 * Check the coherency count.
491 */
492
493 if (!state->stateful) {
494 /* Discard late packet */
495 if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE
496 > MPPE_CCOUNT_SPACE / 2) {
497 state->sanity_errors++;
498 goto sanity_error;
499 }
500
501 /* RFC 3078, sec 8.1. Rekey for every packet. */
502 while (state->ccount != ccount) {
503 mppe_rekey(state, 0);
504 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
505 }
506 } else {
507 /* RFC 3078, sec 8.2. */
508 if (!state->discard) {
509 /* normal state */
510 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
511 if (ccount != state->ccount) {
512 /*
513 * (ccount > state->ccount)
514 * Packet loss detected, enter the discard state.
515 * Signal the peer to rekey (by sending a CCP Reset-Request).
516 */
517 state->discard = 1;
518 return DECOMP_ERROR;
519 }
520 } else {
521 /* discard state */
522 if (!flushed) {
523 /* ccp.c will be silent (no additional CCP Reset-Requests). */
524 return DECOMP_ERROR;
525 } else {
526 /* Rekey for every missed "flag" packet. */
527 while ((ccount & ~0xff) !=
528 (state->ccount & ~0xff)) {
529 mppe_rekey(state, 0);
530 state->ccount =
531 (state->ccount +
532 256) % MPPE_CCOUNT_SPACE;
533 }
534
535 /* reset */
536 state->discard = 0;
537 state->ccount = ccount;
538 /*
539 * Another problem with RFC 3078 here. It implies that the
540 * peer need not send a Reset-Ack packet. But RFC 1962
541 * requires it. Hopefully, M$ does send a Reset-Ack; even
542 * though it isn't required for MPPE synchronization, it is
543 * required to reset CCP state.
544 */
545 }
546 }
547 if (flushed)
548 mppe_rekey(state, 0);
549 }
550
551 /*
552 * Fill in the first part of the PPP header. The protocol field
553 * comes from the decrypted data.
554 */
555 obuf[0] = PPP_ADDRESS(ibuf); /* +1 */
556 obuf[1] = PPP_CONTROL(ibuf); /* +1 */
557 obuf += 2;
558 ibuf += PPP_HDRLEN + MPPE_OVHD;
559 isize -= PPP_HDRLEN + MPPE_OVHD; /* -6 */
560 /* net osize: isize-4 */
561
562 /*
563 * Decrypt the first byte in order to check if it is
564 * a compressed or uncompressed protocol field.
565 */
566 arc4_crypt(&state->arc4, obuf, ibuf, 1);
567
568 /*
569 * Do PFC decompression.
570 * This would be nicer if we were given the actual sk_buff
571 * instead of a char *.
572 */
573 if ((obuf[0] & 0x01) != 0) {
574 obuf[1] = obuf[0];
575 obuf[0] = 0;
576 obuf++;
577 osize++;
578 }
579
580 /* And finally, decrypt the rest of the packet. */
581 arc4_crypt(&state->arc4, obuf + 1, ibuf + 1, isize - 1);
582
583 state->stats.unc_bytes += osize;
584 state->stats.unc_packets++;
585 state->stats.comp_bytes += isize;
586 state->stats.comp_packets++;
587
588 /* good packet credit */
589 state->sanity_errors >>= 1;
590
591 return osize;
592
593sanity_error:
594 if (state->sanity_errors < SANITY_MAX)
595 return DECOMP_ERROR;
596 else
597 /* Take LCP down if the peer is sending too many bogons.
598 * We don't want to do this for a single or just a few
599 * instances since it could just be due to packet corruption.
600 */
601 return DECOMP_FATALERROR;
602}
603
604/*
605 * Incompressible data has arrived (this should never happen!).
606 * We should probably drop the link if the protocol is in the range
607 * of what should be encrypted. At the least, we should drop this
608 * packet. (How to do this?)
609 */
610static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
611{
612 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
613
614 if (state->debug &&
615 (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
616 printk(KERN_DEBUG
617 "mppe_incomp[%d]: incompressible (unencrypted) data! "
618 "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
619
620 state->stats.inc_bytes += icnt;
621 state->stats.inc_packets++;
622 state->stats.unc_bytes += icnt;
623 state->stats.unc_packets++;
624}
625
626/*************************************************************
627 * Module interface table
628 *************************************************************/
629
630/*
631 * Procedures exported to if_ppp.c.
632 */
633static struct compressor ppp_mppe = {
634 .compress_proto = CI_MPPE,
635 .comp_alloc = mppe_alloc,
636 .comp_free = mppe_free,
637 .comp_init = mppe_comp_init,
638 .comp_reset = mppe_comp_reset,
639 .compress = mppe_compress,
640 .comp_stat = mppe_comp_stats,
641 .decomp_alloc = mppe_alloc,
642 .decomp_free = mppe_free,
643 .decomp_init = mppe_decomp_init,
644 .decomp_reset = mppe_decomp_reset,
645 .decompress = mppe_decompress,
646 .incomp = mppe_incomp,
647 .decomp_stat = mppe_comp_stats,
648 .owner = THIS_MODULE,
649 .comp_extra = MPPE_PAD,
650};
651
652/*
653 * ppp_mppe_init()
654 *
655 * Prior to allowing load, try to load the arc4 and sha1 crypto
656 * libraries. The actual use will be allocated later, but
657 * this way the module will fail to insmod if they aren't available.
658 */
659
660static int __init ppp_mppe_init(void)
661{
662 int answer;
663 if (fips_enabled || !crypto_has_ahash("sha1", 0, CRYPTO_ALG_ASYNC))
664 return -ENODEV;
665
666 sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
667 if (!sha_pad)
668 return -ENOMEM;
669 sha_pad_init(sha_pad);
670
671 answer = ppp_register_compressor(&ppp_mppe);
672
673 if (answer == 0)
674 printk(KERN_INFO "PPP MPPE Compression module registered\n");
675 else
676 kfree(sha_pad);
677
678 return answer;
679}
680
681static void __exit ppp_mppe_cleanup(void)
682{
683 ppp_unregister_compressor(&ppp_mppe);
684 kfree(sha_pad);
685}
686
687module_init(ppp_mppe_init);
688module_exit(ppp_mppe_cleanup);
1/*
2 * ppp_mppe.c - interface MPPE to the PPP code.
3 * This version is for use with Linux kernel 2.6.14+
4 *
5 * By Frank Cusack <fcusack@fcusack.com>.
6 * Copyright (c) 2002,2003,2004 Google, Inc.
7 * All rights reserved.
8 *
9 * License:
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation is hereby granted, provided that the above copyright
12 * notice appears in all copies. This software is provided without any
13 * warranty, express or implied.
14 *
15 * ALTERNATIVELY, provided that this notice is retained in full, this product
16 * may be distributed under the terms of the GNU General Public License (GPL),
17 * in which case the provisions of the GPL apply INSTEAD OF those given above.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 *
32 *
33 * Changelog:
34 * 08/12/05 - Matt Domsch <Matt_Domsch@dell.com>
35 * Only need extra skb padding on transmit, not receive.
36 * 06/18/04 - Matt Domsch <Matt_Domsch@dell.com>, Oleg Makarenko <mole@quadra.ru>
37 * Use Linux kernel 2.6 arc4 and sha1 routines rather than
38 * providing our own.
39 * 2/15/04 - TS: added #include <version.h> and testing for Kernel
40 * version before using
41 * MOD_DEC_USAGE_COUNT/MOD_INC_USAGE_COUNT which are
42 * deprecated in 2.6
43 */
44
45#include <crypto/hash.h>
46#include <crypto/skcipher.h>
47#include <linux/err.h>
48#include <linux/module.h>
49#include <linux/kernel.h>
50#include <linux/init.h>
51#include <linux/types.h>
52#include <linux/slab.h>
53#include <linux/string.h>
54#include <linux/mm.h>
55#include <linux/ppp_defs.h>
56#include <linux/ppp-comp.h>
57#include <linux/scatterlist.h>
58#include <asm/unaligned.h>
59
60#include "ppp_mppe.h"
61
62MODULE_AUTHOR("Frank Cusack <fcusack@fcusack.com>");
63MODULE_DESCRIPTION("Point-to-Point Protocol Microsoft Point-to-Point Encryption support");
64MODULE_LICENSE("Dual BSD/GPL");
65MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
66MODULE_VERSION("1.0.2");
67
68static unsigned int
69setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
70{
71 sg_set_buf(sg, address, length);
72 return length;
73}
74
75#define SHA1_PAD_SIZE 40
76
77/*
78 * kernel crypto API needs its arguments to be in kmalloc'd memory, not in the module
79 * static data area. That means sha_pad needs to be kmalloc'd.
80 */
81
82struct sha_pad {
83 unsigned char sha_pad1[SHA1_PAD_SIZE];
84 unsigned char sha_pad2[SHA1_PAD_SIZE];
85};
86static struct sha_pad *sha_pad;
87
88static inline void sha_pad_init(struct sha_pad *shapad)
89{
90 memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1));
91 memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2));
92}
93
94/*
95 * State for an MPPE (de)compressor.
96 */
97struct ppp_mppe_state {
98 struct crypto_skcipher *arc4;
99 struct crypto_ahash *sha1;
100 unsigned char *sha1_digest;
101 unsigned char master_key[MPPE_MAX_KEY_LEN];
102 unsigned char session_key[MPPE_MAX_KEY_LEN];
103 unsigned keylen; /* key length in bytes */
104 /* NB: 128-bit == 16, 40-bit == 8! */
105 /* If we want to support 56-bit, */
106 /* the unit has to change to bits */
107 unsigned char bits; /* MPPE control bits */
108 unsigned ccount; /* 12-bit coherency count (seqno) */
109 unsigned stateful; /* stateful mode flag */
110 int discard; /* stateful mode packet loss flag */
111 int sanity_errors; /* take down LCP if too many */
112 int unit;
113 int debug;
114 struct compstat stats;
115};
116
117/* struct ppp_mppe_state.bits definitions */
118#define MPPE_BIT_A 0x80 /* Encryption table were (re)inititalized */
119#define MPPE_BIT_B 0x40 /* MPPC only (not implemented) */
120#define MPPE_BIT_C 0x20 /* MPPC only (not implemented) */
121#define MPPE_BIT_D 0x10 /* This is an encrypted frame */
122
123#define MPPE_BIT_FLUSHED MPPE_BIT_A
124#define MPPE_BIT_ENCRYPTED MPPE_BIT_D
125
126#define MPPE_BITS(p) ((p)[4] & 0xf0)
127#define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
128#define MPPE_CCOUNT_SPACE 0x1000 /* The size of the ccount space */
129
130#define MPPE_OVHD 2 /* MPPE overhead/packet */
131#define SANITY_MAX 1600 /* Max bogon factor we will tolerate */
132
133/*
134 * Key Derivation, from RFC 3078, RFC 3079.
135 * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
136 */
137static void get_new_key_from_sha(struct ppp_mppe_state * state)
138{
139 AHASH_REQUEST_ON_STACK(req, state->sha1);
140 struct scatterlist sg[4];
141 unsigned int nbytes;
142
143 sg_init_table(sg, 4);
144
145 nbytes = setup_sg(&sg[0], state->master_key, state->keylen);
146 nbytes += setup_sg(&sg[1], sha_pad->sha_pad1,
147 sizeof(sha_pad->sha_pad1));
148 nbytes += setup_sg(&sg[2], state->session_key, state->keylen);
149 nbytes += setup_sg(&sg[3], sha_pad->sha_pad2,
150 sizeof(sha_pad->sha_pad2));
151
152 ahash_request_set_tfm(req, state->sha1);
153 ahash_request_set_callback(req, 0, NULL, NULL);
154 ahash_request_set_crypt(req, sg, state->sha1_digest, nbytes);
155
156 crypto_ahash_digest(req);
157 ahash_request_zero(req);
158}
159
160/*
161 * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
162 * Well, not what's written there, but rather what they meant.
163 */
164static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
165{
166 struct scatterlist sg_in[1], sg_out[1];
167 SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
168
169 skcipher_request_set_tfm(req, state->arc4);
170 skcipher_request_set_callback(req, 0, NULL, NULL);
171
172 get_new_key_from_sha(state);
173 if (!initial_key) {
174 crypto_skcipher_setkey(state->arc4, state->sha1_digest,
175 state->keylen);
176 sg_init_table(sg_in, 1);
177 sg_init_table(sg_out, 1);
178 setup_sg(sg_in, state->sha1_digest, state->keylen);
179 setup_sg(sg_out, state->session_key, state->keylen);
180 skcipher_request_set_crypt(req, sg_in, sg_out, state->keylen,
181 NULL);
182 if (crypto_skcipher_encrypt(req))
183 printk(KERN_WARNING "mppe_rekey: cipher_encrypt failed\n");
184 } else {
185 memcpy(state->session_key, state->sha1_digest, state->keylen);
186 }
187 if (state->keylen == 8) {
188 /* See RFC 3078 */
189 state->session_key[0] = 0xd1;
190 state->session_key[1] = 0x26;
191 state->session_key[2] = 0x9e;
192 }
193 crypto_skcipher_setkey(state->arc4, state->session_key, state->keylen);
194 skcipher_request_zero(req);
195}
196
197/*
198 * Allocate space for a (de)compressor.
199 */
200static void *mppe_alloc(unsigned char *options, int optlen)
201{
202 struct ppp_mppe_state *state;
203 unsigned int digestsize;
204
205 if (optlen != CILEN_MPPE + sizeof(state->master_key) ||
206 options[0] != CI_MPPE || options[1] != CILEN_MPPE)
207 goto out;
208
209 state = kzalloc(sizeof(*state), GFP_KERNEL);
210 if (state == NULL)
211 goto out;
212
213
214 state->arc4 = crypto_alloc_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
215 if (IS_ERR(state->arc4)) {
216 state->arc4 = NULL;
217 goto out_free;
218 }
219
220 state->sha1 = crypto_alloc_ahash("sha1", 0, CRYPTO_ALG_ASYNC);
221 if (IS_ERR(state->sha1)) {
222 state->sha1 = NULL;
223 goto out_free;
224 }
225
226 digestsize = crypto_ahash_digestsize(state->sha1);
227 if (digestsize < MPPE_MAX_KEY_LEN)
228 goto out_free;
229
230 state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
231 if (!state->sha1_digest)
232 goto out_free;
233
234 /* Save keys. */
235 memcpy(state->master_key, &options[CILEN_MPPE],
236 sizeof(state->master_key));
237 memcpy(state->session_key, state->master_key,
238 sizeof(state->master_key));
239
240 /*
241 * We defer initial key generation until mppe_init(), as mppe_alloc()
242 * is called frequently during negotiation.
243 */
244
245 return (void *)state;
246
247out_free:
248 kfree(state->sha1_digest);
249 crypto_free_ahash(state->sha1);
250 crypto_free_skcipher(state->arc4);
251 kfree(state);
252out:
253 return NULL;
254}
255
256/*
257 * Deallocate space for a (de)compressor.
258 */
259static void mppe_free(void *arg)
260{
261 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
262 if (state) {
263 kfree(state->sha1_digest);
264 crypto_free_ahash(state->sha1);
265 crypto_free_skcipher(state->arc4);
266 kfree(state);
267 }
268}
269
270/*
271 * Initialize (de)compressor state.
272 */
273static int
274mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
275 const char *debugstr)
276{
277 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
278 unsigned char mppe_opts;
279
280 if (optlen != CILEN_MPPE ||
281 options[0] != CI_MPPE || options[1] != CILEN_MPPE)
282 return 0;
283
284 MPPE_CI_TO_OPTS(&options[2], mppe_opts);
285 if (mppe_opts & MPPE_OPT_128)
286 state->keylen = 16;
287 else if (mppe_opts & MPPE_OPT_40)
288 state->keylen = 8;
289 else {
290 printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
291 unit);
292 return 0;
293 }
294 if (mppe_opts & MPPE_OPT_STATEFUL)
295 state->stateful = 1;
296
297 /* Generate the initial session key. */
298 mppe_rekey(state, 1);
299
300 if (debug) {
301 int i;
302 char mkey[sizeof(state->master_key) * 2 + 1];
303 char skey[sizeof(state->session_key) * 2 + 1];
304
305 printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
306 debugstr, unit, (state->keylen == 16) ? 128 : 40,
307 (state->stateful) ? "stateful" : "stateless");
308
309 for (i = 0; i < sizeof(state->master_key); i++)
310 sprintf(mkey + i * 2, "%02x", state->master_key[i]);
311 for (i = 0; i < sizeof(state->session_key); i++)
312 sprintf(skey + i * 2, "%02x", state->session_key[i]);
313 printk(KERN_DEBUG
314 "%s[%d]: keys: master: %s initial session: %s\n",
315 debugstr, unit, mkey, skey);
316 }
317
318 /*
319 * Initialize the coherency count. The initial value is not specified
320 * in RFC 3078, but we can make a reasonable assumption that it will
321 * start at 0. Setting it to the max here makes the comp/decomp code
322 * do the right thing (determined through experiment).
323 */
324 state->ccount = MPPE_CCOUNT_SPACE - 1;
325
326 /*
327 * Note that even though we have initialized the key table, we don't
328 * set the FLUSHED bit. This is contrary to RFC 3078, sec. 3.1.
329 */
330 state->bits = MPPE_BIT_ENCRYPTED;
331
332 state->unit = unit;
333 state->debug = debug;
334
335 return 1;
336}
337
338static int
339mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
340 int hdrlen, int debug)
341{
342 /* ARGSUSED */
343 return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
344}
345
346/*
347 * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
348 * tell the compressor to rekey. Note that we MUST NOT rekey for
349 * every CCP Reset-Request; we only rekey on the next xmit packet.
350 * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
351 * So, rekeying for every CCP Reset-Request is broken as the peer will not
352 * know how many times we've rekeyed. (If we rekey and THEN get another
353 * CCP Reset-Request, we must rekey again.)
354 */
355static void mppe_comp_reset(void *arg)
356{
357 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
358
359 state->bits |= MPPE_BIT_FLUSHED;
360}
361
362/*
363 * Compress (encrypt) a packet.
364 * It's strange to call this a compressor, since the output is always
365 * MPPE_OVHD + 2 bytes larger than the input.
366 */
367static int
368mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
369 int isize, int osize)
370{
371 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
372 SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
373 int proto;
374 int err;
375 struct scatterlist sg_in[1], sg_out[1];
376
377 /*
378 * Check that the protocol is in the range we handle.
379 */
380 proto = PPP_PROTOCOL(ibuf);
381 if (proto < 0x0021 || proto > 0x00fa)
382 return 0;
383
384 /* Make sure we have enough room to generate an encrypted packet. */
385 if (osize < isize + MPPE_OVHD + 2) {
386 /* Drop the packet if we should encrypt it, but can't. */
387 printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
388 "(have: %d need: %d)\n", state->unit,
389 osize, osize + MPPE_OVHD + 2);
390 return -1;
391 }
392
393 osize = isize + MPPE_OVHD + 2;
394
395 /*
396 * Copy over the PPP header and set control bits.
397 */
398 obuf[0] = PPP_ADDRESS(ibuf);
399 obuf[1] = PPP_CONTROL(ibuf);
400 put_unaligned_be16(PPP_COMP, obuf + 2);
401 obuf += PPP_HDRLEN;
402
403 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
404 if (state->debug >= 7)
405 printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
406 state->ccount);
407 put_unaligned_be16(state->ccount, obuf);
408
409 if (!state->stateful || /* stateless mode */
410 ((state->ccount & 0xff) == 0xff) || /* "flag" packet */
411 (state->bits & MPPE_BIT_FLUSHED)) { /* CCP Reset-Request */
412 /* We must rekey */
413 if (state->debug && state->stateful)
414 printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
415 state->unit);
416 mppe_rekey(state, 0);
417 state->bits |= MPPE_BIT_FLUSHED;
418 }
419 obuf[0] |= state->bits;
420 state->bits &= ~MPPE_BIT_FLUSHED; /* reset for next xmit */
421
422 obuf += MPPE_OVHD;
423 ibuf += 2; /* skip to proto field */
424 isize -= 2;
425
426 /* Encrypt packet */
427 sg_init_table(sg_in, 1);
428 sg_init_table(sg_out, 1);
429 setup_sg(sg_in, ibuf, isize);
430 setup_sg(sg_out, obuf, osize);
431
432 skcipher_request_set_tfm(req, state->arc4);
433 skcipher_request_set_callback(req, 0, NULL, NULL);
434 skcipher_request_set_crypt(req, sg_in, sg_out, isize, NULL);
435 err = crypto_skcipher_encrypt(req);
436 skcipher_request_zero(req);
437 if (err) {
438 printk(KERN_DEBUG "crypto_cypher_encrypt failed\n");
439 return -1;
440 }
441
442 state->stats.unc_bytes += isize;
443 state->stats.unc_packets++;
444 state->stats.comp_bytes += osize;
445 state->stats.comp_packets++;
446
447 return osize;
448}
449
450/*
451 * Since every frame grows by MPPE_OVHD + 2 bytes, this is always going
452 * to look bad ... and the longer the link is up the worse it will get.
453 */
454static void mppe_comp_stats(void *arg, struct compstat *stats)
455{
456 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
457
458 *stats = state->stats;
459}
460
461static int
462mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
463 int hdrlen, int mru, int debug)
464{
465 /* ARGSUSED */
466 return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
467}
468
469/*
470 * We received a CCP Reset-Ack. Just ignore it.
471 */
472static void mppe_decomp_reset(void *arg)
473{
474 /* ARGSUSED */
475 return;
476}
477
478/*
479 * Decompress (decrypt) an MPPE packet.
480 */
481static int
482mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
483 int osize)
484{
485 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
486 SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
487 unsigned ccount;
488 int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
489 struct scatterlist sg_in[1], sg_out[1];
490
491 if (isize <= PPP_HDRLEN + MPPE_OVHD) {
492 if (state->debug)
493 printk(KERN_DEBUG
494 "mppe_decompress[%d]: short pkt (%d)\n",
495 state->unit, isize);
496 return DECOMP_ERROR;
497 }
498
499 /*
500 * Make sure we have enough room to decrypt the packet.
501 * Note that for our test we only subtract 1 byte whereas in
502 * mppe_compress() we added 2 bytes (+MPPE_OVHD);
503 * this is to account for possible PFC.
504 */
505 if (osize < isize - MPPE_OVHD - 1) {
506 printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
507 "(have: %d need: %d)\n", state->unit,
508 osize, isize - MPPE_OVHD - 1);
509 return DECOMP_ERROR;
510 }
511 osize = isize - MPPE_OVHD - 2; /* assume no PFC */
512
513 ccount = MPPE_CCOUNT(ibuf);
514 if (state->debug >= 7)
515 printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
516 state->unit, ccount);
517
518 /* sanity checks -- terminate with extreme prejudice */
519 if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
520 printk(KERN_DEBUG
521 "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
522 state->unit);
523 state->sanity_errors += 100;
524 goto sanity_error;
525 }
526 if (!state->stateful && !flushed) {
527 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
528 "stateless mode!\n", state->unit);
529 state->sanity_errors += 100;
530 goto sanity_error;
531 }
532 if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
533 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
534 "flag packet!\n", state->unit);
535 state->sanity_errors += 100;
536 goto sanity_error;
537 }
538
539 /*
540 * Check the coherency count.
541 */
542
543 if (!state->stateful) {
544 /* Discard late packet */
545 if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE
546 > MPPE_CCOUNT_SPACE / 2) {
547 state->sanity_errors++;
548 goto sanity_error;
549 }
550
551 /* RFC 3078, sec 8.1. Rekey for every packet. */
552 while (state->ccount != ccount) {
553 mppe_rekey(state, 0);
554 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
555 }
556 } else {
557 /* RFC 3078, sec 8.2. */
558 if (!state->discard) {
559 /* normal state */
560 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
561 if (ccount != state->ccount) {
562 /*
563 * (ccount > state->ccount)
564 * Packet loss detected, enter the discard state.
565 * Signal the peer to rekey (by sending a CCP Reset-Request).
566 */
567 state->discard = 1;
568 return DECOMP_ERROR;
569 }
570 } else {
571 /* discard state */
572 if (!flushed) {
573 /* ccp.c will be silent (no additional CCP Reset-Requests). */
574 return DECOMP_ERROR;
575 } else {
576 /* Rekey for every missed "flag" packet. */
577 while ((ccount & ~0xff) !=
578 (state->ccount & ~0xff)) {
579 mppe_rekey(state, 0);
580 state->ccount =
581 (state->ccount +
582 256) % MPPE_CCOUNT_SPACE;
583 }
584
585 /* reset */
586 state->discard = 0;
587 state->ccount = ccount;
588 /*
589 * Another problem with RFC 3078 here. It implies that the
590 * peer need not send a Reset-Ack packet. But RFC 1962
591 * requires it. Hopefully, M$ does send a Reset-Ack; even
592 * though it isn't required for MPPE synchronization, it is
593 * required to reset CCP state.
594 */
595 }
596 }
597 if (flushed)
598 mppe_rekey(state, 0);
599 }
600
601 /*
602 * Fill in the first part of the PPP header. The protocol field
603 * comes from the decrypted data.
604 */
605 obuf[0] = PPP_ADDRESS(ibuf); /* +1 */
606 obuf[1] = PPP_CONTROL(ibuf); /* +1 */
607 obuf += 2;
608 ibuf += PPP_HDRLEN + MPPE_OVHD;
609 isize -= PPP_HDRLEN + MPPE_OVHD; /* -6 */
610 /* net osize: isize-4 */
611
612 /*
613 * Decrypt the first byte in order to check if it is
614 * a compressed or uncompressed protocol field.
615 */
616 sg_init_table(sg_in, 1);
617 sg_init_table(sg_out, 1);
618 setup_sg(sg_in, ibuf, 1);
619 setup_sg(sg_out, obuf, 1);
620
621 skcipher_request_set_tfm(req, state->arc4);
622 skcipher_request_set_callback(req, 0, NULL, NULL);
623 skcipher_request_set_crypt(req, sg_in, sg_out, 1, NULL);
624 if (crypto_skcipher_decrypt(req)) {
625 printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
626 osize = DECOMP_ERROR;
627 goto out_zap_req;
628 }
629
630 /*
631 * Do PFC decompression.
632 * This would be nicer if we were given the actual sk_buff
633 * instead of a char *.
634 */
635 if ((obuf[0] & 0x01) != 0) {
636 obuf[1] = obuf[0];
637 obuf[0] = 0;
638 obuf++;
639 osize++;
640 }
641
642 /* And finally, decrypt the rest of the packet. */
643 setup_sg(sg_in, ibuf + 1, isize - 1);
644 setup_sg(sg_out, obuf + 1, osize - 1);
645 skcipher_request_set_crypt(req, sg_in, sg_out, isize - 1, NULL);
646 if (crypto_skcipher_decrypt(req)) {
647 printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
648 osize = DECOMP_ERROR;
649 goto out_zap_req;
650 }
651
652 state->stats.unc_bytes += osize;
653 state->stats.unc_packets++;
654 state->stats.comp_bytes += isize;
655 state->stats.comp_packets++;
656
657 /* good packet credit */
658 state->sanity_errors >>= 1;
659
660out_zap_req:
661 skcipher_request_zero(req);
662 return osize;
663
664sanity_error:
665 if (state->sanity_errors < SANITY_MAX)
666 return DECOMP_ERROR;
667 else
668 /* Take LCP down if the peer is sending too many bogons.
669 * We don't want to do this for a single or just a few
670 * instances since it could just be due to packet corruption.
671 */
672 return DECOMP_FATALERROR;
673}
674
675/*
676 * Incompressible data has arrived (this should never happen!).
677 * We should probably drop the link if the protocol is in the range
678 * of what should be encrypted. At the least, we should drop this
679 * packet. (How to do this?)
680 */
681static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
682{
683 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
684
685 if (state->debug &&
686 (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
687 printk(KERN_DEBUG
688 "mppe_incomp[%d]: incompressible (unencrypted) data! "
689 "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
690
691 state->stats.inc_bytes += icnt;
692 state->stats.inc_packets++;
693 state->stats.unc_bytes += icnt;
694 state->stats.unc_packets++;
695}
696
697/*************************************************************
698 * Module interface table
699 *************************************************************/
700
701/*
702 * Procedures exported to if_ppp.c.
703 */
704static struct compressor ppp_mppe = {
705 .compress_proto = CI_MPPE,
706 .comp_alloc = mppe_alloc,
707 .comp_free = mppe_free,
708 .comp_init = mppe_comp_init,
709 .comp_reset = mppe_comp_reset,
710 .compress = mppe_compress,
711 .comp_stat = mppe_comp_stats,
712 .decomp_alloc = mppe_alloc,
713 .decomp_free = mppe_free,
714 .decomp_init = mppe_decomp_init,
715 .decomp_reset = mppe_decomp_reset,
716 .decompress = mppe_decompress,
717 .incomp = mppe_incomp,
718 .decomp_stat = mppe_comp_stats,
719 .owner = THIS_MODULE,
720 .comp_extra = MPPE_PAD,
721};
722
723/*
724 * ppp_mppe_init()
725 *
726 * Prior to allowing load, try to load the arc4 and sha1 crypto
727 * libraries. The actual use will be allocated later, but
728 * this way the module will fail to insmod if they aren't available.
729 */
730
731static int __init ppp_mppe_init(void)
732{
733 int answer;
734 if (!(crypto_has_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC) &&
735 crypto_has_ahash("sha1", 0, CRYPTO_ALG_ASYNC)))
736 return -ENODEV;
737
738 sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
739 if (!sha_pad)
740 return -ENOMEM;
741 sha_pad_init(sha_pad);
742
743 answer = ppp_register_compressor(&ppp_mppe);
744
745 if (answer == 0)
746 printk(KERN_INFO "PPP MPPE Compression module registered\n");
747 else
748 kfree(sha_pad);
749
750 return answer;
751}
752
753static void __exit ppp_mppe_cleanup(void)
754{
755 ppp_unregister_compressor(&ppp_mppe);
756 kfree(sha_pad);
757}
758
759module_init(ppp_mppe_init);
760module_exit(ppp_mppe_cleanup);