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