2 * ppp_mppe.c - interface MPPE to the PPP code.
3 * This version is for use with Linux kernel 2.6.14+
5 * By Frank Cusack <fcusack@fcusack.com>.
6 * Copyright (c) 2002,2003,2004 Google, Inc.
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.
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.
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.
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.
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/>.
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
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
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>
55 #include <linux/ppp_defs.h>
56 #include <linux/ppp-comp.h>
57 #include <linux/scatterlist.h>
58 #include <asm/unaligned.h>
62 MODULE_AUTHOR("Frank Cusack <fcusack@fcusack.com>");
63 MODULE_DESCRIPTION("Point-to-Point Protocol Microsoft Point-to-Point Encryption support");
64 MODULE_LICENSE("Dual BSD/GPL");
65 MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
66 MODULE_SOFTDEP("pre: arc4");
67 MODULE_VERSION("1.0.2");
70 setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
72 sg_set_buf(sg, address, length);
76 #define SHA1_PAD_SIZE 40
79 * kernel crypto API needs its arguments to be in kmalloc'd memory, not in the module
80 * static data area. That means sha_pad needs to be kmalloc'd.
84 unsigned char sha_pad1[SHA1_PAD_SIZE];
85 unsigned char sha_pad2[SHA1_PAD_SIZE];
87 static struct sha_pad *sha_pad;
89 static inline void sha_pad_init(struct sha_pad *shapad)
91 memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1));
92 memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2));
96 * State for an MPPE (de)compressor.
98 struct ppp_mppe_state {
99 struct crypto_skcipher *arc4;
100 struct crypto_ahash *sha1;
101 unsigned char *sha1_digest;
102 unsigned char master_key[MPPE_MAX_KEY_LEN];
103 unsigned char session_key[MPPE_MAX_KEY_LEN];
104 unsigned keylen; /* key length in bytes */
105 /* NB: 128-bit == 16, 40-bit == 8! */
106 /* If we want to support 56-bit, */
107 /* the unit has to change to bits */
108 unsigned char bits; /* MPPE control bits */
109 unsigned ccount; /* 12-bit coherency count (seqno) */
110 unsigned stateful; /* stateful mode flag */
111 int discard; /* stateful mode packet loss flag */
112 int sanity_errors; /* take down LCP if too many */
115 struct compstat stats;
118 /* struct ppp_mppe_state.bits definitions */
119 #define MPPE_BIT_A 0x80 /* Encryption table were (re)inititalized */
120 #define MPPE_BIT_B 0x40 /* MPPC only (not implemented) */
121 #define MPPE_BIT_C 0x20 /* MPPC only (not implemented) */
122 #define MPPE_BIT_D 0x10 /* This is an encrypted frame */
124 #define MPPE_BIT_FLUSHED MPPE_BIT_A
125 #define MPPE_BIT_ENCRYPTED MPPE_BIT_D
127 #define MPPE_BITS(p) ((p)[4] & 0xf0)
128 #define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
129 #define MPPE_CCOUNT_SPACE 0x1000 /* The size of the ccount space */
131 #define MPPE_OVHD 2 /* MPPE overhead/packet */
132 #define SANITY_MAX 1600 /* Max bogon factor we will tolerate */
135 * Key Derivation, from RFC 3078, RFC 3079.
136 * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
138 static void get_new_key_from_sha(struct ppp_mppe_state * state)
140 AHASH_REQUEST_ON_STACK(req, state->sha1);
141 struct scatterlist sg[4];
144 sg_init_table(sg, 4);
146 nbytes = setup_sg(&sg[0], state->master_key, state->keylen);
147 nbytes += setup_sg(&sg[1], sha_pad->sha_pad1,
148 sizeof(sha_pad->sha_pad1));
149 nbytes += setup_sg(&sg[2], state->session_key, state->keylen);
150 nbytes += setup_sg(&sg[3], sha_pad->sha_pad2,
151 sizeof(sha_pad->sha_pad2));
153 ahash_request_set_tfm(req, state->sha1);
154 ahash_request_set_callback(req, 0, NULL, NULL);
155 ahash_request_set_crypt(req, sg, state->sha1_digest, nbytes);
157 crypto_ahash_digest(req);
158 ahash_request_zero(req);
162 * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
163 * Well, not what's written there, but rather what they meant.
165 static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
167 struct scatterlist sg_in[1], sg_out[1];
168 SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
170 skcipher_request_set_tfm(req, state->arc4);
171 skcipher_request_set_callback(req, 0, NULL, NULL);
173 get_new_key_from_sha(state);
175 crypto_skcipher_setkey(state->arc4, state->sha1_digest,
177 sg_init_table(sg_in, 1);
178 sg_init_table(sg_out, 1);
179 setup_sg(sg_in, state->sha1_digest, state->keylen);
180 setup_sg(sg_out, state->session_key, state->keylen);
181 skcipher_request_set_crypt(req, sg_in, sg_out, state->keylen,
183 if (crypto_skcipher_encrypt(req))
184 printk(KERN_WARNING "mppe_rekey: cipher_encrypt failed\n");
186 memcpy(state->session_key, state->sha1_digest, state->keylen);
188 if (state->keylen == 8) {
190 state->session_key[0] = 0xd1;
191 state->session_key[1] = 0x26;
192 state->session_key[2] = 0x9e;
194 crypto_skcipher_setkey(state->arc4, state->session_key, state->keylen);
195 skcipher_request_zero(req);
199 * Allocate space for a (de)compressor.
201 static void *mppe_alloc(unsigned char *options, int optlen)
203 struct ppp_mppe_state *state;
204 unsigned int digestsize;
206 if (optlen != CILEN_MPPE + sizeof(state->master_key) ||
207 options[0] != CI_MPPE || options[1] != CILEN_MPPE)
210 state = kzalloc(sizeof(*state), GFP_KERNEL);
215 state->arc4 = crypto_alloc_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
216 if (IS_ERR(state->arc4)) {
221 state->sha1 = crypto_alloc_ahash("sha1", 0, CRYPTO_ALG_ASYNC);
222 if (IS_ERR(state->sha1)) {
227 digestsize = crypto_ahash_digestsize(state->sha1);
228 if (digestsize < MPPE_MAX_KEY_LEN)
231 state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
232 if (!state->sha1_digest)
236 memcpy(state->master_key, &options[CILEN_MPPE],
237 sizeof(state->master_key));
238 memcpy(state->session_key, state->master_key,
239 sizeof(state->master_key));
242 * We defer initial key generation until mppe_init(), as mppe_alloc()
243 * is called frequently during negotiation.
246 return (void *)state;
249 kfree(state->sha1_digest);
250 crypto_free_ahash(state->sha1);
251 crypto_free_skcipher(state->arc4);
258 * Deallocate space for a (de)compressor.
260 static void mppe_free(void *arg)
262 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
264 kfree(state->sha1_digest);
265 crypto_free_ahash(state->sha1);
266 crypto_free_skcipher(state->arc4);
272 * Initialize (de)compressor state.
275 mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
276 const char *debugstr)
278 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
279 unsigned char mppe_opts;
281 if (optlen != CILEN_MPPE ||
282 options[0] != CI_MPPE || options[1] != CILEN_MPPE)
285 MPPE_CI_TO_OPTS(&options[2], mppe_opts);
286 if (mppe_opts & MPPE_OPT_128)
288 else if (mppe_opts & MPPE_OPT_40)
291 printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
295 if (mppe_opts & MPPE_OPT_STATEFUL)
298 /* Generate the initial session key. */
299 mppe_rekey(state, 1);
302 printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
303 debugstr, unit, (state->keylen == 16) ? 128 : 40,
304 (state->stateful) ? "stateful" : "stateless");
306 "%s[%d]: keys: master: %*phN initial session: %*phN\n",
308 (int)sizeof(state->master_key), state->master_key,
309 (int)sizeof(state->session_key), state->session_key);
313 * Initialize the coherency count. The initial value is not specified
314 * in RFC 3078, but we can make a reasonable assumption that it will
315 * start at 0. Setting it to the max here makes the comp/decomp code
316 * do the right thing (determined through experiment).
318 state->ccount = MPPE_CCOUNT_SPACE - 1;
321 * Note that even though we have initialized the key table, we don't
322 * set the FLUSHED bit. This is contrary to RFC 3078, sec. 3.1.
324 state->bits = MPPE_BIT_ENCRYPTED;
327 state->debug = debug;
333 mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
334 int hdrlen, int debug)
337 return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
341 * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
342 * tell the compressor to rekey. Note that we MUST NOT rekey for
343 * every CCP Reset-Request; we only rekey on the next xmit packet.
344 * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
345 * So, rekeying for every CCP Reset-Request is broken as the peer will not
346 * know how many times we've rekeyed. (If we rekey and THEN get another
347 * CCP Reset-Request, we must rekey again.)
349 static void mppe_comp_reset(void *arg)
351 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
353 state->bits |= MPPE_BIT_FLUSHED;
357 * Compress (encrypt) a packet.
358 * It's strange to call this a compressor, since the output is always
359 * MPPE_OVHD + 2 bytes larger than the input.
362 mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
363 int isize, int osize)
365 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
366 SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
369 struct scatterlist sg_in[1], sg_out[1];
372 * Check that the protocol is in the range we handle.
374 proto = PPP_PROTOCOL(ibuf);
375 if (proto < 0x0021 || proto > 0x00fa)
378 /* Make sure we have enough room to generate an encrypted packet. */
379 if (osize < isize + MPPE_OVHD + 2) {
380 /* Drop the packet if we should encrypt it, but can't. */
381 printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
382 "(have: %d need: %d)\n", state->unit,
383 osize, osize + MPPE_OVHD + 2);
387 osize = isize + MPPE_OVHD + 2;
390 * Copy over the PPP header and set control bits.
392 obuf[0] = PPP_ADDRESS(ibuf);
393 obuf[1] = PPP_CONTROL(ibuf);
394 put_unaligned_be16(PPP_COMP, obuf + 2);
397 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
398 if (state->debug >= 7)
399 printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
401 put_unaligned_be16(state->ccount, obuf);
403 if (!state->stateful || /* stateless mode */
404 ((state->ccount & 0xff) == 0xff) || /* "flag" packet */
405 (state->bits & MPPE_BIT_FLUSHED)) { /* CCP Reset-Request */
407 if (state->debug && state->stateful)
408 printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
410 mppe_rekey(state, 0);
411 state->bits |= MPPE_BIT_FLUSHED;
413 obuf[0] |= state->bits;
414 state->bits &= ~MPPE_BIT_FLUSHED; /* reset for next xmit */
417 ibuf += 2; /* skip to proto field */
421 sg_init_table(sg_in, 1);
422 sg_init_table(sg_out, 1);
423 setup_sg(sg_in, ibuf, isize);
424 setup_sg(sg_out, obuf, osize);
426 skcipher_request_set_tfm(req, state->arc4);
427 skcipher_request_set_callback(req, 0, NULL, NULL);
428 skcipher_request_set_crypt(req, sg_in, sg_out, isize, NULL);
429 err = crypto_skcipher_encrypt(req);
430 skcipher_request_zero(req);
432 printk(KERN_DEBUG "crypto_cypher_encrypt failed\n");
436 state->stats.unc_bytes += isize;
437 state->stats.unc_packets++;
438 state->stats.comp_bytes += osize;
439 state->stats.comp_packets++;
445 * Since every frame grows by MPPE_OVHD + 2 bytes, this is always going
446 * to look bad ... and the longer the link is up the worse it will get.
448 static void mppe_comp_stats(void *arg, struct compstat *stats)
450 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
452 *stats = state->stats;
456 mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
457 int hdrlen, int mru, int debug)
460 return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
464 * We received a CCP Reset-Ack. Just ignore it.
466 static void mppe_decomp_reset(void *arg)
473 * Decompress (decrypt) an MPPE packet.
476 mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
479 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
480 SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
482 int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
483 struct scatterlist sg_in[1], sg_out[1];
485 if (isize <= PPP_HDRLEN + MPPE_OVHD) {
488 "mppe_decompress[%d]: short pkt (%d)\n",
494 * Make sure we have enough room to decrypt the packet.
495 * Note that for our test we only subtract 1 byte whereas in
496 * mppe_compress() we added 2 bytes (+MPPE_OVHD);
497 * this is to account for possible PFC.
499 if (osize < isize - MPPE_OVHD - 1) {
500 printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
501 "(have: %d need: %d)\n", state->unit,
502 osize, isize - MPPE_OVHD - 1);
505 osize = isize - MPPE_OVHD - 2; /* assume no PFC */
507 ccount = MPPE_CCOUNT(ibuf);
508 if (state->debug >= 7)
509 printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
510 state->unit, ccount);
512 /* sanity checks -- terminate with extreme prejudice */
513 if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
515 "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
517 state->sanity_errors += 100;
520 if (!state->stateful && !flushed) {
521 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
522 "stateless mode!\n", state->unit);
523 state->sanity_errors += 100;
526 if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
527 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
528 "flag packet!\n", state->unit);
529 state->sanity_errors += 100;
534 * Check the coherency count.
537 if (!state->stateful) {
538 /* Discard late packet */
539 if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE
540 > MPPE_CCOUNT_SPACE / 2) {
541 state->sanity_errors++;
545 /* RFC 3078, sec 8.1. Rekey for every packet. */
546 while (state->ccount != ccount) {
547 mppe_rekey(state, 0);
548 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
551 /* RFC 3078, sec 8.2. */
552 if (!state->discard) {
554 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
555 if (ccount != state->ccount) {
557 * (ccount > state->ccount)
558 * Packet loss detected, enter the discard state.
559 * Signal the peer to rekey (by sending a CCP Reset-Request).
567 /* ccp.c will be silent (no additional CCP Reset-Requests). */
570 /* Rekey for every missed "flag" packet. */
571 while ((ccount & ~0xff) !=
572 (state->ccount & ~0xff)) {
573 mppe_rekey(state, 0);
576 256) % MPPE_CCOUNT_SPACE;
581 state->ccount = ccount;
583 * Another problem with RFC 3078 here. It implies that the
584 * peer need not send a Reset-Ack packet. But RFC 1962
585 * requires it. Hopefully, M$ does send a Reset-Ack; even
586 * though it isn't required for MPPE synchronization, it is
587 * required to reset CCP state.
592 mppe_rekey(state, 0);
596 * Fill in the first part of the PPP header. The protocol field
597 * comes from the decrypted data.
599 obuf[0] = PPP_ADDRESS(ibuf); /* +1 */
600 obuf[1] = PPP_CONTROL(ibuf); /* +1 */
602 ibuf += PPP_HDRLEN + MPPE_OVHD;
603 isize -= PPP_HDRLEN + MPPE_OVHD; /* -6 */
604 /* net osize: isize-4 */
607 * Decrypt the first byte in order to check if it is
608 * a compressed or uncompressed protocol field.
610 sg_init_table(sg_in, 1);
611 sg_init_table(sg_out, 1);
612 setup_sg(sg_in, ibuf, 1);
613 setup_sg(sg_out, obuf, 1);
615 skcipher_request_set_tfm(req, state->arc4);
616 skcipher_request_set_callback(req, 0, NULL, NULL);
617 skcipher_request_set_crypt(req, sg_in, sg_out, 1, NULL);
618 if (crypto_skcipher_decrypt(req)) {
619 printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
620 osize = DECOMP_ERROR;
625 * Do PFC decompression.
626 * This would be nicer if we were given the actual sk_buff
627 * instead of a char *.
629 if ((obuf[0] & 0x01) != 0) {
636 /* And finally, decrypt the rest of the packet. */
637 setup_sg(sg_in, ibuf + 1, isize - 1);
638 setup_sg(sg_out, obuf + 1, osize - 1);
639 skcipher_request_set_crypt(req, sg_in, sg_out, isize - 1, NULL);
640 if (crypto_skcipher_decrypt(req)) {
641 printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
642 osize = DECOMP_ERROR;
646 state->stats.unc_bytes += osize;
647 state->stats.unc_packets++;
648 state->stats.comp_bytes += isize;
649 state->stats.comp_packets++;
651 /* good packet credit */
652 state->sanity_errors >>= 1;
655 skcipher_request_zero(req);
659 if (state->sanity_errors < SANITY_MAX)
662 /* Take LCP down if the peer is sending too many bogons.
663 * We don't want to do this for a single or just a few
664 * instances since it could just be due to packet corruption.
666 return DECOMP_FATALERROR;
670 * Incompressible data has arrived (this should never happen!).
671 * We should probably drop the link if the protocol is in the range
672 * of what should be encrypted. At the least, we should drop this
673 * packet. (How to do this?)
675 static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
677 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
680 (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
682 "mppe_incomp[%d]: incompressible (unencrypted) data! "
683 "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
685 state->stats.inc_bytes += icnt;
686 state->stats.inc_packets++;
687 state->stats.unc_bytes += icnt;
688 state->stats.unc_packets++;
691 /*************************************************************
692 * Module interface table
693 *************************************************************/
696 * Procedures exported to if_ppp.c.
698 static struct compressor ppp_mppe = {
699 .compress_proto = CI_MPPE,
700 .comp_alloc = mppe_alloc,
701 .comp_free = mppe_free,
702 .comp_init = mppe_comp_init,
703 .comp_reset = mppe_comp_reset,
704 .compress = mppe_compress,
705 .comp_stat = mppe_comp_stats,
706 .decomp_alloc = mppe_alloc,
707 .decomp_free = mppe_free,
708 .decomp_init = mppe_decomp_init,
709 .decomp_reset = mppe_decomp_reset,
710 .decompress = mppe_decompress,
711 .incomp = mppe_incomp,
712 .decomp_stat = mppe_comp_stats,
713 .owner = THIS_MODULE,
714 .comp_extra = MPPE_PAD,
720 * Prior to allowing load, try to load the arc4 and sha1 crypto
721 * libraries. The actual use will be allocated later, but
722 * this way the module will fail to insmod if they aren't available.
725 static int __init ppp_mppe_init(void)
728 if (!(crypto_has_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC) &&
729 crypto_has_ahash("sha1", 0, CRYPTO_ALG_ASYNC)))
732 sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
735 sha_pad_init(sha_pad);
737 answer = ppp_register_compressor(&ppp_mppe);
740 printk(KERN_INFO "PPP MPPE Compression module registered\n");
747 static void __exit ppp_mppe_cleanup(void)
749 ppp_unregister_compressor(&ppp_mppe);
753 module_init(ppp_mppe_init);
754 module_exit(ppp_mppe_cleanup);