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