blob: c0b6d19d1457598ba7a83738a194400344725197 [file] [log] [blame]
Matt Domschb3f9b922005-11-08 09:40:47 -08001/*
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, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 *
33 *
34 * Changelog:
35 * 08/12/05 - Matt Domsch <Matt_Domsch@dell.com>
36 * Only need extra skb padding on transmit, not receive.
37 * 06/18/04 - Matt Domsch <Matt_Domsch@dell.com>, Oleg Makarenko <mole@quadra.ru>
38 * Use Linux kernel 2.6 arc4 and sha1 routines rather than
39 * providing our own.
40 * 2/15/04 - TS: added #include <version.h> and testing for Kernel
41 * version before using
42 * MOD_DEC_USAGE_COUNT/MOD_INC_USAGE_COUNT which are
43 * deprecated in 2.6
44 */
45
Herbert Xuf12cc202006-08-22 20:36:13 +100046#include <linux/err.h>
Matt Domschb3f9b922005-11-08 09:40:47 -080047#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/version.h>
50#include <linux/init.h>
51#include <linux/types.h>
52#include <linux/slab.h>
53#include <linux/string.h>
54#include <linux/crypto.h>
55#include <linux/mm.h>
56#include <linux/ppp_defs.h>
57#include <linux/ppp-comp.h>
58#include <asm/scatterlist.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
Herbert Xu35058682006-08-24 19:10:20 +100068static unsigned int
Matt Domschb3f9b922005-11-08 09:40:47 -080069setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
70{
71 sg[0].page = virt_to_page(address);
72 sg[0].offset = offset_in_page(address);
73 sg[0].length = length;
Herbert Xu35058682006-08-24 19:10:20 +100074 return length;
Matt Domschb3f9b922005-11-08 09:40:47 -080075}
76
77#define SHA1_PAD_SIZE 40
78
79/*
80 * kernel crypto API needs its arguments to be in kmalloc'd memory, not in the module
81 * static data area. That means sha_pad needs to be kmalloc'd.
82 */
83
84struct sha_pad {
85 unsigned char sha_pad1[SHA1_PAD_SIZE];
86 unsigned char sha_pad2[SHA1_PAD_SIZE];
87};
88static struct sha_pad *sha_pad;
89
90static inline void sha_pad_init(struct sha_pad *shapad)
91{
92 memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1));
93 memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2));
94}
95
96/*
97 * State for an MPPE (de)compressor.
98 */
99struct ppp_mppe_state {
Herbert Xuf12cc202006-08-22 20:36:13 +1000100 struct crypto_blkcipher *arc4;
Herbert Xu35058682006-08-24 19:10:20 +1000101 struct crypto_hash *sha1;
Matt Domschb3f9b922005-11-08 09:40:47 -0800102 unsigned char *sha1_digest;
103 unsigned char master_key[MPPE_MAX_KEY_LEN];
104 unsigned char session_key[MPPE_MAX_KEY_LEN];
105 unsigned keylen; /* key length in bytes */
106 /* NB: 128-bit == 16, 40-bit == 8! */
107 /* If we want to support 56-bit, */
108 /* the unit has to change to bits */
109 unsigned char bits; /* MPPE control bits */
110 unsigned ccount; /* 12-bit coherency count (seqno) */
111 unsigned stateful; /* stateful mode flag */
112 int discard; /* stateful mode packet loss flag */
113 int sanity_errors; /* take down LCP if too many */
114 int unit;
115 int debug;
116 struct compstat stats;
117};
118
119/* struct ppp_mppe_state.bits definitions */
120#define MPPE_BIT_A 0x80 /* Encryption table were (re)inititalized */
121#define MPPE_BIT_B 0x40 /* MPPC only (not implemented) */
122#define MPPE_BIT_C 0x20 /* MPPC only (not implemented) */
123#define MPPE_BIT_D 0x10 /* This is an encrypted frame */
124
125#define MPPE_BIT_FLUSHED MPPE_BIT_A
126#define MPPE_BIT_ENCRYPTED MPPE_BIT_D
127
128#define MPPE_BITS(p) ((p)[4] & 0xf0)
129#define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
130#define MPPE_CCOUNT_SPACE 0x1000 /* The size of the ccount space */
131
132#define MPPE_OVHD 2 /* MPPE overhead/packet */
133#define SANITY_MAX 1600 /* Max bogon factor we will tolerate */
134
135/*
136 * Key Derivation, from RFC 3078, RFC 3079.
137 * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
138 */
Michal Schmidt45dfd5b2007-09-25 23:05:39 -0700139static void get_new_key_from_sha(struct ppp_mppe_state * state)
Matt Domschb3f9b922005-11-08 09:40:47 -0800140{
Herbert Xu35058682006-08-24 19:10:20 +1000141 struct hash_desc desc;
Matt Domschb3f9b922005-11-08 09:40:47 -0800142 struct scatterlist sg[4];
Herbert Xu35058682006-08-24 19:10:20 +1000143 unsigned int nbytes;
Matt Domschb3f9b922005-11-08 09:40:47 -0800144
Herbert Xu35058682006-08-24 19:10:20 +1000145 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));
Matt Domschb3f9b922005-11-08 09:40:47 -0800151
Herbert Xu35058682006-08-24 19:10:20 +1000152 desc.tfm = state->sha1;
153 desc.flags = 0;
154
155 crypto_hash_digest(&desc, sg, nbytes, state->sha1_digest);
Matt Domschb3f9b922005-11-08 09:40:47 -0800156}
157
158/*
159 * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
160 * Well, not what's written there, but rather what they meant.
161 */
162static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
163{
Matt Domschb3f9b922005-11-08 09:40:47 -0800164 struct scatterlist sg_in[1], sg_out[1];
Herbert Xuf12cc202006-08-22 20:36:13 +1000165 struct blkcipher_desc desc = { .tfm = state->arc4 };
Matt Domschb3f9b922005-11-08 09:40:47 -0800166
Michal Schmidt45dfd5b2007-09-25 23:05:39 -0700167 get_new_key_from_sha(state);
Matt Domschb3f9b922005-11-08 09:40:47 -0800168 if (!initial_key) {
Michal Schmidt45dfd5b2007-09-25 23:05:39 -0700169 crypto_blkcipher_setkey(state->arc4, state->sha1_digest,
170 state->keylen);
171 setup_sg(sg_in, state->sha1_digest, state->keylen);
Matt Domschb3f9b922005-11-08 09:40:47 -0800172 setup_sg(sg_out, state->session_key, state->keylen);
Herbert Xuf12cc202006-08-22 20:36:13 +1000173 if (crypto_blkcipher_encrypt(&desc, sg_out, sg_in,
174 state->keylen) != 0) {
Matt Domschb3f9b922005-11-08 09:40:47 -0800175 printk(KERN_WARNING "mppe_rekey: cipher_encrypt failed\n");
176 }
177 } else {
Michal Schmidt45dfd5b2007-09-25 23:05:39 -0700178 memcpy(state->session_key, state->sha1_digest, state->keylen);
Matt Domschb3f9b922005-11-08 09:40:47 -0800179 }
180 if (state->keylen == 8) {
181 /* See RFC 3078 */
182 state->session_key[0] = 0xd1;
183 state->session_key[1] = 0x26;
184 state->session_key[2] = 0x9e;
185 }
Herbert Xuf12cc202006-08-22 20:36:13 +1000186 crypto_blkcipher_setkey(state->arc4, state->session_key, state->keylen);
Matt Domschb3f9b922005-11-08 09:40:47 -0800187}
188
189/*
190 * Allocate space for a (de)compressor.
191 */
192static void *mppe_alloc(unsigned char *options, int optlen)
193{
194 struct ppp_mppe_state *state;
195 unsigned int digestsize;
196
197 if (optlen != CILEN_MPPE + sizeof(state->master_key)
198 || options[0] != CI_MPPE || options[1] != CILEN_MPPE)
199 goto out;
200
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700201 state = kzalloc(sizeof(*state), GFP_KERNEL);
Matt Domschb3f9b922005-11-08 09:40:47 -0800202 if (state == NULL)
203 goto out;
204
Matt Domschb3f9b922005-11-08 09:40:47 -0800205
Herbert Xuf12cc202006-08-22 20:36:13 +1000206 state->arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
207 if (IS_ERR(state->arc4)) {
208 state->arc4 = NULL;
Matt Domschb3f9b922005-11-08 09:40:47 -0800209 goto out_free;
Herbert Xuf12cc202006-08-22 20:36:13 +1000210 }
Matt Domschb3f9b922005-11-08 09:40:47 -0800211
Herbert Xu35058682006-08-24 19:10:20 +1000212 state->sha1 = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
213 if (IS_ERR(state->sha1)) {
214 state->sha1 = NULL;
Matt Domschb3f9b922005-11-08 09:40:47 -0800215 goto out_free;
Herbert Xu35058682006-08-24 19:10:20 +1000216 }
Matt Domschb3f9b922005-11-08 09:40:47 -0800217
Herbert Xu35058682006-08-24 19:10:20 +1000218 digestsize = crypto_hash_digestsize(state->sha1);
Matt Domschb3f9b922005-11-08 09:40:47 -0800219 if (digestsize < MPPE_MAX_KEY_LEN)
220 goto out_free;
221
222 state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
223 if (!state->sha1_digest)
224 goto out_free;
225
226 /* Save keys. */
227 memcpy(state->master_key, &options[CILEN_MPPE],
228 sizeof(state->master_key));
229 memcpy(state->session_key, state->master_key,
230 sizeof(state->master_key));
231
232 /*
233 * We defer initial key generation until mppe_init(), as mppe_alloc()
234 * is called frequently during negotiation.
235 */
236
237 return (void *)state;
238
239 out_free:
240 if (state->sha1_digest)
241 kfree(state->sha1_digest);
242 if (state->sha1)
Herbert Xu35058682006-08-24 19:10:20 +1000243 crypto_free_hash(state->sha1);
Matt Domschb3f9b922005-11-08 09:40:47 -0800244 if (state->arc4)
Herbert Xuf12cc202006-08-22 20:36:13 +1000245 crypto_free_blkcipher(state->arc4);
Matt Domschb3f9b922005-11-08 09:40:47 -0800246 kfree(state);
247 out:
248 return NULL;
249}
250
251/*
252 * Deallocate space for a (de)compressor.
253 */
254static void mppe_free(void *arg)
255{
256 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
257 if (state) {
258 if (state->sha1_digest)
259 kfree(state->sha1_digest);
260 if (state->sha1)
Herbert Xu35058682006-08-24 19:10:20 +1000261 crypto_free_hash(state->sha1);
Matt Domschb3f9b922005-11-08 09:40:47 -0800262 if (state->arc4)
Herbert Xuf12cc202006-08-22 20:36:13 +1000263 crypto_free_blkcipher(state->arc4);
Matt Domschb3f9b922005-11-08 09:40:47 -0800264 kfree(state);
265 }
266}
267
268/*
269 * Initialize (de)compressor state.
270 */
271static int
272mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
273 const char *debugstr)
274{
275 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
276 unsigned char mppe_opts;
277
278 if (optlen != CILEN_MPPE
279 || options[0] != CI_MPPE || options[1] != CILEN_MPPE)
280 return 0;
281
282 MPPE_CI_TO_OPTS(&options[2], mppe_opts);
283 if (mppe_opts & MPPE_OPT_128)
284 state->keylen = 16;
285 else if (mppe_opts & MPPE_OPT_40)
286 state->keylen = 8;
287 else {
288 printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
289 unit);
290 return 0;
291 }
292 if (mppe_opts & MPPE_OPT_STATEFUL)
293 state->stateful = 1;
294
295 /* Generate the initial session key. */
296 mppe_rekey(state, 1);
297
298 if (debug) {
299 int i;
300 char mkey[sizeof(state->master_key) * 2 + 1];
301 char skey[sizeof(state->session_key) * 2 + 1];
302
303 printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
304 debugstr, unit, (state->keylen == 16) ? 128 : 40,
305 (state->stateful) ? "stateful" : "stateless");
306
307 for (i = 0; i < sizeof(state->master_key); i++)
308 sprintf(mkey + i * 2, "%02x", state->master_key[i]);
309 for (i = 0; i < sizeof(state->session_key); i++)
310 sprintf(skey + i * 2, "%02x", state->session_key[i]);
311 printk(KERN_DEBUG
312 "%s[%d]: keys: master: %s initial session: %s\n",
313 debugstr, unit, mkey, skey);
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
336static int
337mppe_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 */
353static 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 */
365static int
366mppe_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;
Herbert Xuf12cc202006-08-22 20:36:13 +1000370 struct blkcipher_desc desc = { .tfm = state->arc4 };
Matt Domschb3f9b922005-11-08 09:40:47 -0800371 int proto;
372 struct scatterlist sg_in[1], sg_out[1];
373
374 /*
375 * Check that the protocol is in the range we handle.
376 */
377 proto = PPP_PROTOCOL(ibuf);
378 if (proto < 0x0021 || proto > 0x00fa)
379 return 0;
380
381 /* Make sure we have enough room to generate an encrypted packet. */
382 if (osize < isize + MPPE_OVHD + 2) {
383 /* Drop the packet if we should encrypt it, but can't. */
384 printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
385 "(have: %d need: %d)\n", state->unit,
386 osize, osize + MPPE_OVHD + 2);
387 return -1;
388 }
389
390 osize = isize + MPPE_OVHD + 2;
391
392 /*
393 * Copy over the PPP header and set control bits.
394 */
395 obuf[0] = PPP_ADDRESS(ibuf);
396 obuf[1] = PPP_CONTROL(ibuf);
397 obuf[2] = PPP_COMP >> 8; /* isize + MPPE_OVHD + 1 */
398 obuf[3] = PPP_COMP; /* isize + MPPE_OVHD + 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 obuf[0] = state->ccount >> 8;
406 obuf[1] = state->ccount & 0xff;
407
408 if (!state->stateful || /* stateless mode */
409 ((state->ccount & 0xff) == 0xff) || /* "flag" packet */
410 (state->bits & MPPE_BIT_FLUSHED)) { /* CCP Reset-Request */
411 /* We must rekey */
412 if (state->debug && state->stateful)
413 printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
414 state->unit);
415 mppe_rekey(state, 0);
416 state->bits |= MPPE_BIT_FLUSHED;
417 }
418 obuf[0] |= state->bits;
419 state->bits &= ~MPPE_BIT_FLUSHED; /* reset for next xmit */
420
421 obuf += MPPE_OVHD;
422 ibuf += 2; /* skip to proto field */
423 isize -= 2;
424
425 /* Encrypt packet */
426 setup_sg(sg_in, ibuf, isize);
427 setup_sg(sg_out, obuf, osize);
Herbert Xuf12cc202006-08-22 20:36:13 +1000428 if (crypto_blkcipher_encrypt(&desc, sg_out, sg_in, isize) != 0) {
Matt Domschb3f9b922005-11-08 09:40:47 -0800429 printk(KERN_DEBUG "crypto_cypher_encrypt failed\n");
430 return -1;
431 }
432
433 state->stats.unc_bytes += isize;
434 state->stats.unc_packets++;
435 state->stats.comp_bytes += osize;
436 state->stats.comp_packets++;
437
438 return osize;
439}
440
441/*
442 * Since every frame grows by MPPE_OVHD + 2 bytes, this is always going
443 * to look bad ... and the longer the link is up the worse it will get.
444 */
445static void mppe_comp_stats(void *arg, struct compstat *stats)
446{
447 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
448
449 *stats = state->stats;
450}
451
452static int
453mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
454 int hdrlen, int mru, int debug)
455{
456 /* ARGSUSED */
457 return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
458}
459
460/*
461 * We received a CCP Reset-Ack. Just ignore it.
462 */
463static void mppe_decomp_reset(void *arg)
464{
465 /* ARGSUSED */
466 return;
467}
468
469/*
470 * Decompress (decrypt) an MPPE packet.
471 */
472static int
473mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
474 int osize)
475{
476 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
Herbert Xuf12cc202006-08-22 20:36:13 +1000477 struct blkcipher_desc desc = { .tfm = state->arc4 };
Matt Domschb3f9b922005-11-08 09:40:47 -0800478 unsigned ccount;
479 int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
480 int sanity = 0;
481 struct scatterlist sg_in[1], sg_out[1];
482
483 if (isize <= PPP_HDRLEN + MPPE_OVHD) {
484 if (state->debug)
485 printk(KERN_DEBUG
486 "mppe_decompress[%d]: short pkt (%d)\n",
487 state->unit, isize);
488 return DECOMP_ERROR;
489 }
490
491 /*
492 * Make sure we have enough room to decrypt the packet.
David S. Miller7e4a6da2007-06-23 23:04:11 -0700493 * Note that for our test we only subtract 1 byte whereas in
494 * mppe_compress() we added 2 bytes (+MPPE_OVHD);
495 * this is to account for possible PFC.
Matt Domschb3f9b922005-11-08 09:40:47 -0800496 */
David S. Miller7e4a6da2007-06-23 23:04:11 -0700497 if (osize < isize - MPPE_OVHD - 1) {
Matt Domschb3f9b922005-11-08 09:40:47 -0800498 printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
499 "(have: %d need: %d)\n", state->unit,
David S. Miller7e4a6da2007-06-23 23:04:11 -0700500 osize, isize - MPPE_OVHD - 1);
Matt Domschb3f9b922005-11-08 09:40:47 -0800501 return DECOMP_ERROR;
502 }
503 osize = isize - MPPE_OVHD - 2; /* assume no PFC */
504
505 ccount = MPPE_CCOUNT(ibuf);
506 if (state->debug >= 7)
507 printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
508 state->unit, ccount);
509
510 /* sanity checks -- terminate with extreme prejudice */
511 if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
512 printk(KERN_DEBUG
513 "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
514 state->unit);
515 state->sanity_errors += 100;
516 sanity = 1;
517 }
518 if (!state->stateful && !flushed) {
519 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
520 "stateless mode!\n", state->unit);
521 state->sanity_errors += 100;
522 sanity = 1;
523 }
524 if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
525 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
526 "flag packet!\n", state->unit);
527 state->sanity_errors += 100;
528 sanity = 1;
529 }
530
531 if (sanity) {
532 if (state->sanity_errors < SANITY_MAX)
533 return DECOMP_ERROR;
534 else
535 /*
536 * Take LCP down if the peer is sending too many bogons.
537 * We don't want to do this for a single or just a few
538 * instances since it could just be due to packet corruption.
539 */
540 return DECOMP_FATALERROR;
541 }
542
543 /*
544 * Check the coherency count.
545 */
546
547 if (!state->stateful) {
548 /* RFC 3078, sec 8.1. Rekey for every packet. */
549 while (state->ccount != ccount) {
550 mppe_rekey(state, 0);
551 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
552 }
553 } else {
554 /* RFC 3078, sec 8.2. */
555 if (!state->discard) {
556 /* normal state */
557 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
558 if (ccount != state->ccount) {
559 /*
560 * (ccount > state->ccount)
561 * Packet loss detected, enter the discard state.
562 * Signal the peer to rekey (by sending a CCP Reset-Request).
563 */
564 state->discard = 1;
565 return DECOMP_ERROR;
566 }
567 } else {
568 /* discard state */
569 if (!flushed) {
570 /* ccp.c will be silent (no additional CCP Reset-Requests). */
571 return DECOMP_ERROR;
572 } else {
573 /* Rekey for every missed "flag" packet. */
574 while ((ccount & ~0xff) !=
575 (state->ccount & ~0xff)) {
576 mppe_rekey(state, 0);
577 state->ccount =
578 (state->ccount +
579 256) % MPPE_CCOUNT_SPACE;
580 }
581
582 /* reset */
583 state->discard = 0;
584 state->ccount = ccount;
585 /*
586 * Another problem with RFC 3078 here. It implies that the
587 * peer need not send a Reset-Ack packet. But RFC 1962
588 * requires it. Hopefully, M$ does send a Reset-Ack; even
589 * though it isn't required for MPPE synchronization, it is
590 * required to reset CCP state.
591 */
592 }
593 }
594 if (flushed)
595 mppe_rekey(state, 0);
596 }
597
598 /*
599 * Fill in the first part of the PPP header. The protocol field
600 * comes from the decrypted data.
601 */
602 obuf[0] = PPP_ADDRESS(ibuf); /* +1 */
603 obuf[1] = PPP_CONTROL(ibuf); /* +1 */
604 obuf += 2;
605 ibuf += PPP_HDRLEN + MPPE_OVHD;
606 isize -= PPP_HDRLEN + MPPE_OVHD; /* -6 */
607 /* net osize: isize-4 */
608
609 /*
610 * Decrypt the first byte in order to check if it is
611 * a compressed or uncompressed protocol field.
612 */
613 setup_sg(sg_in, ibuf, 1);
614 setup_sg(sg_out, obuf, 1);
Herbert Xuf12cc202006-08-22 20:36:13 +1000615 if (crypto_blkcipher_decrypt(&desc, sg_out, sg_in, 1) != 0) {
Matt Domschb3f9b922005-11-08 09:40:47 -0800616 printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
617 return DECOMP_ERROR;
618 }
619
620 /*
621 * Do PFC decompression.
622 * This would be nicer if we were given the actual sk_buff
623 * instead of a char *.
624 */
625 if ((obuf[0] & 0x01) != 0) {
626 obuf[1] = obuf[0];
627 obuf[0] = 0;
628 obuf++;
629 osize++;
630 }
631
632 /* And finally, decrypt the rest of the packet. */
633 setup_sg(sg_in, ibuf + 1, isize - 1);
634 setup_sg(sg_out, obuf + 1, osize - 1);
Herbert Xuf12cc202006-08-22 20:36:13 +1000635 if (crypto_blkcipher_decrypt(&desc, sg_out, sg_in, isize - 1)) {
Matt Domschb3f9b922005-11-08 09:40:47 -0800636 printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
637 return DECOMP_ERROR;
638 }
639
640 state->stats.unc_bytes += osize;
641 state->stats.unc_packets++;
642 state->stats.comp_bytes += isize;
643 state->stats.comp_packets++;
644
645 /* good packet credit */
646 state->sanity_errors >>= 1;
647
648 return osize;
649}
650
651/*
652 * Incompressible data has arrived (this should never happen!).
653 * We should probably drop the link if the protocol is in the range
654 * of what should be encrypted. At the least, we should drop this
655 * packet. (How to do this?)
656 */
657static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
658{
659 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
660
661 if (state->debug &&
662 (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
663 printk(KERN_DEBUG
664 "mppe_incomp[%d]: incompressible (unencrypted) data! "
665 "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
666
667 state->stats.inc_bytes += icnt;
668 state->stats.inc_packets++;
669 state->stats.unc_bytes += icnt;
670 state->stats.unc_packets++;
671}
672
673/*************************************************************
674 * Module interface table
675 *************************************************************/
676
677/*
678 * Procedures exported to if_ppp.c.
679 */
680static struct compressor ppp_mppe = {
681 .compress_proto = CI_MPPE,
682 .comp_alloc = mppe_alloc,
683 .comp_free = mppe_free,
684 .comp_init = mppe_comp_init,
685 .comp_reset = mppe_comp_reset,
686 .compress = mppe_compress,
687 .comp_stat = mppe_comp_stats,
688 .decomp_alloc = mppe_alloc,
689 .decomp_free = mppe_free,
690 .decomp_init = mppe_decomp_init,
691 .decomp_reset = mppe_decomp_reset,
692 .decompress = mppe_decompress,
693 .incomp = mppe_incomp,
694 .decomp_stat = mppe_comp_stats,
695 .owner = THIS_MODULE,
696 .comp_extra = MPPE_PAD,
697};
698
699/*
700 * ppp_mppe_init()
701 *
702 * Prior to allowing load, try to load the arc4 and sha1 crypto
703 * libraries. The actual use will be allocated later, but
704 * this way the module will fail to insmod if they aren't available.
705 */
706
707static int __init ppp_mppe_init(void)
708{
709 int answer;
Herbert Xue4d5b792006-08-26 18:12:40 +1000710 if (!(crypto_has_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC) &&
711 crypto_has_hash("sha1", 0, CRYPTO_ALG_ASYNC)))
Matt Domschb3f9b922005-11-08 09:40:47 -0800712 return -ENODEV;
713
714 sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
715 if (!sha_pad)
716 return -ENOMEM;
717 sha_pad_init(sha_pad);
718
719 answer = ppp_register_compressor(&ppp_mppe);
720
721 if (answer == 0)
722 printk(KERN_INFO "PPP MPPE Compression module registered\n");
723 else
724 kfree(sha_pad);
725
726 return answer;
727}
728
729static void __exit ppp_mppe_cleanup(void)
730{
731 ppp_unregister_compressor(&ppp_mppe);
732 kfree(sha_pad);
733}
734
735module_init(ppp_mppe_init);
736module_exit(ppp_mppe_cleanup);