xref: /openbmc/linux/drivers/net/ppp/ppp_mppe.c (revision 8bd1369b)
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 
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_VERSION("1.0.2");
67 
68 static unsigned int
69 setup_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 
82 struct sha_pad {
83 	unsigned char sha_pad1[SHA1_PAD_SIZE];
84 	unsigned char sha_pad2[SHA1_PAD_SIZE];
85 };
86 static struct sha_pad *sha_pad;
87 
88 static 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  */
97 struct 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  */
137 static 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  */
164 static 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  */
200 static 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 
247 out_free:
248 	kfree(state->sha1_digest);
249 	crypto_free_ahash(state->sha1);
250 	crypto_free_skcipher(state->arc4);
251 	kfree(state);
252 out:
253 	return NULL;
254 }
255 
256 /*
257  * Deallocate space for a (de)compressor.
258  */
259 static 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  */
273 static int
274 mppe_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 		printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
302 		       debugstr, unit, (state->keylen == 16) ? 128 : 40,
303 		       (state->stateful) ? "stateful" : "stateless");
304 		printk(KERN_DEBUG
305 		       "%s[%d]: keys: master: %*phN initial session: %*phN\n",
306 		       debugstr, unit,
307 		       (int)sizeof(state->master_key), state->master_key,
308 		       (int)sizeof(state->session_key), state->session_key);
309 	}
310 
311 	/*
312 	 * Initialize the coherency count.  The initial value is not specified
313 	 * in RFC 3078, but we can make a reasonable assumption that it will
314 	 * start at 0.  Setting it to the max here makes the comp/decomp code
315 	 * do the right thing (determined through experiment).
316 	 */
317 	state->ccount = MPPE_CCOUNT_SPACE - 1;
318 
319 	/*
320 	 * Note that even though we have initialized the key table, we don't
321 	 * set the FLUSHED bit.  This is contrary to RFC 3078, sec. 3.1.
322 	 */
323 	state->bits = MPPE_BIT_ENCRYPTED;
324 
325 	state->unit = unit;
326 	state->debug = debug;
327 
328 	return 1;
329 }
330 
331 static int
332 mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
333 	       int hdrlen, int debug)
334 {
335 	/* ARGSUSED */
336 	return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
337 }
338 
339 /*
340  * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
341  * tell the compressor to rekey.  Note that we MUST NOT rekey for
342  * every CCP Reset-Request; we only rekey on the next xmit packet.
343  * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
344  * So, rekeying for every CCP Reset-Request is broken as the peer will not
345  * know how many times we've rekeyed.  (If we rekey and THEN get another
346  * CCP Reset-Request, we must rekey again.)
347  */
348 static void mppe_comp_reset(void *arg)
349 {
350 	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
351 
352 	state->bits |= MPPE_BIT_FLUSHED;
353 }
354 
355 /*
356  * Compress (encrypt) a packet.
357  * It's strange to call this a compressor, since the output is always
358  * MPPE_OVHD + 2 bytes larger than the input.
359  */
360 static int
361 mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
362 	      int isize, int osize)
363 {
364 	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
365 	SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
366 	int proto;
367 	int err;
368 	struct scatterlist sg_in[1], sg_out[1];
369 
370 	/*
371 	 * Check that the protocol is in the range we handle.
372 	 */
373 	proto = PPP_PROTOCOL(ibuf);
374 	if (proto < 0x0021 || proto > 0x00fa)
375 		return 0;
376 
377 	/* Make sure we have enough room to generate an encrypted packet. */
378 	if (osize < isize + MPPE_OVHD + 2) {
379 		/* Drop the packet if we should encrypt it, but can't. */
380 		printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
381 		       "(have: %d need: %d)\n", state->unit,
382 		       osize, osize + MPPE_OVHD + 2);
383 		return -1;
384 	}
385 
386 	osize = isize + MPPE_OVHD + 2;
387 
388 	/*
389 	 * Copy over the PPP header and set control bits.
390 	 */
391 	obuf[0] = PPP_ADDRESS(ibuf);
392 	obuf[1] = PPP_CONTROL(ibuf);
393 	put_unaligned_be16(PPP_COMP, obuf + 2);
394 	obuf += PPP_HDRLEN;
395 
396 	state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
397 	if (state->debug >= 7)
398 		printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
399 		       state->ccount);
400 	put_unaligned_be16(state->ccount, obuf);
401 
402 	if (!state->stateful ||	/* stateless mode     */
403 	    ((state->ccount & 0xff) == 0xff) ||	/* "flag" packet      */
404 	    (state->bits & MPPE_BIT_FLUSHED)) {	/* CCP Reset-Request  */
405 		/* We must rekey */
406 		if (state->debug && state->stateful)
407 			printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
408 			       state->unit);
409 		mppe_rekey(state, 0);
410 		state->bits |= MPPE_BIT_FLUSHED;
411 	}
412 	obuf[0] |= state->bits;
413 	state->bits &= ~MPPE_BIT_FLUSHED;	/* reset for next xmit */
414 
415 	obuf += MPPE_OVHD;
416 	ibuf += 2;		/* skip to proto field */
417 	isize -= 2;
418 
419 	/* Encrypt packet */
420 	sg_init_table(sg_in, 1);
421 	sg_init_table(sg_out, 1);
422 	setup_sg(sg_in, ibuf, isize);
423 	setup_sg(sg_out, obuf, osize);
424 
425 	skcipher_request_set_tfm(req, state->arc4);
426 	skcipher_request_set_callback(req, 0, NULL, NULL);
427 	skcipher_request_set_crypt(req, sg_in, sg_out, isize, NULL);
428 	err = crypto_skcipher_encrypt(req);
429 	skcipher_request_zero(req);
430 	if (err) {
431 		printk(KERN_DEBUG "crypto_cypher_encrypt failed\n");
432 		return -1;
433 	}
434 
435 	state->stats.unc_bytes += isize;
436 	state->stats.unc_packets++;
437 	state->stats.comp_bytes += osize;
438 	state->stats.comp_packets++;
439 
440 	return osize;
441 }
442 
443 /*
444  * Since every frame grows by MPPE_OVHD + 2 bytes, this is always going
445  * to look bad ... and the longer the link is up the worse it will get.
446  */
447 static void mppe_comp_stats(void *arg, struct compstat *stats)
448 {
449 	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
450 
451 	*stats = state->stats;
452 }
453 
454 static int
455 mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
456 		 int hdrlen, int mru, int debug)
457 {
458 	/* ARGSUSED */
459 	return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
460 }
461 
462 /*
463  * We received a CCP Reset-Ack.  Just ignore it.
464  */
465 static void mppe_decomp_reset(void *arg)
466 {
467 	/* ARGSUSED */
468 	return;
469 }
470 
471 /*
472  * Decompress (decrypt) an MPPE packet.
473  */
474 static int
475 mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
476 		int osize)
477 {
478 	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
479 	SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
480 	unsigned ccount;
481 	int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
482 	struct scatterlist sg_in[1], sg_out[1];
483 
484 	if (isize <= PPP_HDRLEN + MPPE_OVHD) {
485 		if (state->debug)
486 			printk(KERN_DEBUG
487 			       "mppe_decompress[%d]: short pkt (%d)\n",
488 			       state->unit, isize);
489 		return DECOMP_ERROR;
490 	}
491 
492 	/*
493 	 * Make sure we have enough room to decrypt the packet.
494 	 * Note that for our test we only subtract 1 byte whereas in
495 	 * mppe_compress() we added 2 bytes (+MPPE_OVHD);
496 	 * this is to account for possible PFC.
497 	 */
498 	if (osize < isize - MPPE_OVHD - 1) {
499 		printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
500 		       "(have: %d need: %d)\n", state->unit,
501 		       osize, isize - MPPE_OVHD - 1);
502 		return DECOMP_ERROR;
503 	}
504 	osize = isize - MPPE_OVHD - 2;	/* assume no PFC */
505 
506 	ccount = MPPE_CCOUNT(ibuf);
507 	if (state->debug >= 7)
508 		printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
509 		       state->unit, ccount);
510 
511 	/* sanity checks -- terminate with extreme prejudice */
512 	if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
513 		printk(KERN_DEBUG
514 		       "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
515 		       state->unit);
516 		state->sanity_errors += 100;
517 		goto sanity_error;
518 	}
519 	if (!state->stateful && !flushed) {
520 		printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
521 		       "stateless mode!\n", state->unit);
522 		state->sanity_errors += 100;
523 		goto sanity_error;
524 	}
525 	if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
526 		printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
527 		       "flag packet!\n", state->unit);
528 		state->sanity_errors += 100;
529 		goto sanity_error;
530 	}
531 
532 	/*
533 	 * Check the coherency count.
534 	 */
535 
536 	if (!state->stateful) {
537 		/* Discard late packet */
538 		if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE
539 						> MPPE_CCOUNT_SPACE / 2) {
540 			state->sanity_errors++;
541 			goto sanity_error;
542 		}
543 
544 		/* RFC 3078, sec 8.1.  Rekey for every packet. */
545 		while (state->ccount != ccount) {
546 			mppe_rekey(state, 0);
547 			state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
548 		}
549 	} else {
550 		/* RFC 3078, sec 8.2. */
551 		if (!state->discard) {
552 			/* normal state */
553 			state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
554 			if (ccount != state->ccount) {
555 				/*
556 				 * (ccount > state->ccount)
557 				 * Packet loss detected, enter the discard state.
558 				 * Signal the peer to rekey (by sending a CCP Reset-Request).
559 				 */
560 				state->discard = 1;
561 				return DECOMP_ERROR;
562 			}
563 		} else {
564 			/* discard state */
565 			if (!flushed) {
566 				/* ccp.c will be silent (no additional CCP Reset-Requests). */
567 				return DECOMP_ERROR;
568 			} else {
569 				/* Rekey for every missed "flag" packet. */
570 				while ((ccount & ~0xff) !=
571 				       (state->ccount & ~0xff)) {
572 					mppe_rekey(state, 0);
573 					state->ccount =
574 					    (state->ccount +
575 					     256) % MPPE_CCOUNT_SPACE;
576 				}
577 
578 				/* reset */
579 				state->discard = 0;
580 				state->ccount = ccount;
581 				/*
582 				 * Another problem with RFC 3078 here.  It implies that the
583 				 * peer need not send a Reset-Ack packet.  But RFC 1962
584 				 * requires it.  Hopefully, M$ does send a Reset-Ack; even
585 				 * though it isn't required for MPPE synchronization, it is
586 				 * required to reset CCP state.
587 				 */
588 			}
589 		}
590 		if (flushed)
591 			mppe_rekey(state, 0);
592 	}
593 
594 	/*
595 	 * Fill in the first part of the PPP header.  The protocol field
596 	 * comes from the decrypted data.
597 	 */
598 	obuf[0] = PPP_ADDRESS(ibuf);	/* +1 */
599 	obuf[1] = PPP_CONTROL(ibuf);	/* +1 */
600 	obuf += 2;
601 	ibuf += PPP_HDRLEN + MPPE_OVHD;
602 	isize -= PPP_HDRLEN + MPPE_OVHD;	/* -6 */
603 	/* net osize: isize-4 */
604 
605 	/*
606 	 * Decrypt the first byte in order to check if it is
607 	 * a compressed or uncompressed protocol field.
608 	 */
609 	sg_init_table(sg_in, 1);
610 	sg_init_table(sg_out, 1);
611 	setup_sg(sg_in, ibuf, 1);
612 	setup_sg(sg_out, obuf, 1);
613 
614 	skcipher_request_set_tfm(req, state->arc4);
615 	skcipher_request_set_callback(req, 0, NULL, NULL);
616 	skcipher_request_set_crypt(req, sg_in, sg_out, 1, NULL);
617 	if (crypto_skcipher_decrypt(req)) {
618 		printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
619 		osize = DECOMP_ERROR;
620 		goto out_zap_req;
621 	}
622 
623 	/*
624 	 * Do PFC decompression.
625 	 * This would be nicer if we were given the actual sk_buff
626 	 * instead of a char *.
627 	 */
628 	if ((obuf[0] & 0x01) != 0) {
629 		obuf[1] = obuf[0];
630 		obuf[0] = 0;
631 		obuf++;
632 		osize++;
633 	}
634 
635 	/* And finally, decrypt the rest of the packet. */
636 	setup_sg(sg_in, ibuf + 1, isize - 1);
637 	setup_sg(sg_out, obuf + 1, osize - 1);
638 	skcipher_request_set_crypt(req, sg_in, sg_out, isize - 1, NULL);
639 	if (crypto_skcipher_decrypt(req)) {
640 		printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
641 		osize = DECOMP_ERROR;
642 		goto out_zap_req;
643 	}
644 
645 	state->stats.unc_bytes += osize;
646 	state->stats.unc_packets++;
647 	state->stats.comp_bytes += isize;
648 	state->stats.comp_packets++;
649 
650 	/* good packet credit */
651 	state->sanity_errors >>= 1;
652 
653 out_zap_req:
654 	skcipher_request_zero(req);
655 	return osize;
656 
657 sanity_error:
658 	if (state->sanity_errors < SANITY_MAX)
659 		return DECOMP_ERROR;
660 	else
661 		/* Take LCP down if the peer is sending too many bogons.
662 		 * We don't want to do this for a single or just a few
663 		 * instances since it could just be due to packet corruption.
664 		 */
665 		return DECOMP_FATALERROR;
666 }
667 
668 /*
669  * Incompressible data has arrived (this should never happen!).
670  * We should probably drop the link if the protocol is in the range
671  * of what should be encrypted.  At the least, we should drop this
672  * packet.  (How to do this?)
673  */
674 static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
675 {
676 	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
677 
678 	if (state->debug &&
679 	    (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
680 		printk(KERN_DEBUG
681 		       "mppe_incomp[%d]: incompressible (unencrypted) data! "
682 		       "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
683 
684 	state->stats.inc_bytes += icnt;
685 	state->stats.inc_packets++;
686 	state->stats.unc_bytes += icnt;
687 	state->stats.unc_packets++;
688 }
689 
690 /*************************************************************
691  * Module interface table
692  *************************************************************/
693 
694 /*
695  * Procedures exported to if_ppp.c.
696  */
697 static struct compressor ppp_mppe = {
698 	.compress_proto = CI_MPPE,
699 	.comp_alloc     = mppe_alloc,
700 	.comp_free      = mppe_free,
701 	.comp_init      = mppe_comp_init,
702 	.comp_reset     = mppe_comp_reset,
703 	.compress       = mppe_compress,
704 	.comp_stat      = mppe_comp_stats,
705 	.decomp_alloc   = mppe_alloc,
706 	.decomp_free    = mppe_free,
707 	.decomp_init    = mppe_decomp_init,
708 	.decomp_reset   = mppe_decomp_reset,
709 	.decompress     = mppe_decompress,
710 	.incomp         = mppe_incomp,
711 	.decomp_stat    = mppe_comp_stats,
712 	.owner          = THIS_MODULE,
713 	.comp_extra     = MPPE_PAD,
714 };
715 
716 /*
717  * ppp_mppe_init()
718  *
719  * Prior to allowing load, try to load the arc4 and sha1 crypto
720  * libraries.  The actual use will be allocated later, but
721  * this way the module will fail to insmod if they aren't available.
722  */
723 
724 static int __init ppp_mppe_init(void)
725 {
726 	int answer;
727 	if (!(crypto_has_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC) &&
728 	      crypto_has_ahash("sha1", 0, CRYPTO_ALG_ASYNC)))
729 		return -ENODEV;
730 
731 	sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
732 	if (!sha_pad)
733 		return -ENOMEM;
734 	sha_pad_init(sha_pad);
735 
736 	answer = ppp_register_compressor(&ppp_mppe);
737 
738 	if (answer == 0)
739 		printk(KERN_INFO "PPP MPPE Compression module registered\n");
740 	else
741 		kfree(sha_pad);
742 
743 	return answer;
744 }
745 
746 static void __exit ppp_mppe_cleanup(void)
747 {
748 	ppp_unregister_compressor(&ppp_mppe);
749 	kfree(sha_pad);
750 }
751 
752 module_init(ppp_mppe_init);
753 module_exit(ppp_mppe_cleanup);
754