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