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