1224cf5adSJeff Kirsher /*
2224cf5adSJeff Kirsher * ppp_mppe.c - interface MPPE to the PPP code.
3224cf5adSJeff Kirsher * This version is for use with Linux kernel 2.6.14+
4224cf5adSJeff Kirsher *
5224cf5adSJeff Kirsher * By Frank Cusack <fcusack@fcusack.com>.
6224cf5adSJeff Kirsher * Copyright (c) 2002,2003,2004 Google, Inc.
7224cf5adSJeff Kirsher * All rights reserved.
8224cf5adSJeff Kirsher *
9224cf5adSJeff Kirsher * License:
10224cf5adSJeff Kirsher * Permission to use, copy, modify, and distribute this software and its
11224cf5adSJeff Kirsher * documentation is hereby granted, provided that the above copyright
12224cf5adSJeff Kirsher * notice appears in all copies. This software is provided without any
13224cf5adSJeff Kirsher * warranty, express or implied.
14224cf5adSJeff Kirsher *
15224cf5adSJeff Kirsher * ALTERNATIVELY, provided that this notice is retained in full, this product
16224cf5adSJeff Kirsher * may be distributed under the terms of the GNU General Public License (GPL),
17224cf5adSJeff Kirsher * in which case the provisions of the GPL apply INSTEAD OF those given above.
18224cf5adSJeff Kirsher *
19224cf5adSJeff Kirsher * This program is free software; you can redistribute it and/or modify
20224cf5adSJeff Kirsher * it under the terms of the GNU General Public License as published by
21224cf5adSJeff Kirsher * the Free Software Foundation; either version 2 of the License, or
22224cf5adSJeff Kirsher * (at your option) any later version.
23224cf5adSJeff Kirsher *
24224cf5adSJeff Kirsher * This program is distributed in the hope that it will be useful,
25224cf5adSJeff Kirsher * but WITHOUT ANY WARRANTY; without even the implied warranty of
26224cf5adSJeff Kirsher * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27224cf5adSJeff Kirsher * GNU General Public License for more details.
28224cf5adSJeff Kirsher *
29224cf5adSJeff Kirsher * You should have received a copy of the GNU General Public License
30adf8d3ffSJeff Kirsher * along with this program; if not, see <http://www.gnu.org/licenses/>.
31224cf5adSJeff Kirsher *
32224cf5adSJeff Kirsher *
33224cf5adSJeff Kirsher * Changelog:
34224cf5adSJeff Kirsher * 08/12/05 - Matt Domsch <Matt_Domsch@dell.com>
35224cf5adSJeff Kirsher * Only need extra skb padding on transmit, not receive.
36224cf5adSJeff Kirsher * 06/18/04 - Matt Domsch <Matt_Domsch@dell.com>, Oleg Makarenko <mole@quadra.ru>
37224cf5adSJeff Kirsher * Use Linux kernel 2.6 arc4 and sha1 routines rather than
38224cf5adSJeff Kirsher * providing our own.
39224cf5adSJeff Kirsher * 2/15/04 - TS: added #include <version.h> and testing for Kernel
40224cf5adSJeff Kirsher * version before using
41224cf5adSJeff Kirsher * MOD_DEC_USAGE_COUNT/MOD_INC_USAGE_COUNT which are
42224cf5adSJeff Kirsher * deprecated in 2.6
43224cf5adSJeff Kirsher */
44224cf5adSJeff Kirsher
450e5a610bSArd Biesheuvel #include <crypto/arc4.h>
46fdb89b1bSHerbert Xu #include <crypto/hash.h>
47224cf5adSJeff Kirsher #include <linux/err.h>
480e5a610bSArd Biesheuvel #include <linux/fips.h>
49224cf5adSJeff Kirsher #include <linux/module.h>
50224cf5adSJeff Kirsher #include <linux/kernel.h>
51224cf5adSJeff Kirsher #include <linux/init.h>
52224cf5adSJeff Kirsher #include <linux/types.h>
53224cf5adSJeff Kirsher #include <linux/slab.h>
54224cf5adSJeff Kirsher #include <linux/string.h>
55224cf5adSJeff Kirsher #include <linux/mm.h>
56224cf5adSJeff Kirsher #include <linux/ppp_defs.h>
57224cf5adSJeff Kirsher #include <linux/ppp-comp.h>
58224cf5adSJeff Kirsher #include <linux/scatterlist.h>
59224cf5adSJeff Kirsher #include <asm/unaligned.h>
60224cf5adSJeff Kirsher
61224cf5adSJeff Kirsher #include "ppp_mppe.h"
62224cf5adSJeff Kirsher
63224cf5adSJeff Kirsher MODULE_AUTHOR("Frank Cusack <fcusack@fcusack.com>");
64224cf5adSJeff Kirsher MODULE_DESCRIPTION("Point-to-Point Protocol Microsoft Point-to-Point Encryption support");
65224cf5adSJeff Kirsher MODULE_LICENSE("Dual BSD/GPL");
66224cf5adSJeff Kirsher MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
67224cf5adSJeff Kirsher MODULE_VERSION("1.0.2");
68224cf5adSJeff Kirsher
69224cf5adSJeff Kirsher #define SHA1_PAD_SIZE 40
70224cf5adSJeff Kirsher
71224cf5adSJeff Kirsher /*
72224cf5adSJeff Kirsher * kernel crypto API needs its arguments to be in kmalloc'd memory, not in the module
73224cf5adSJeff Kirsher * static data area. That means sha_pad needs to be kmalloc'd.
74224cf5adSJeff Kirsher */
75224cf5adSJeff Kirsher
76224cf5adSJeff Kirsher struct sha_pad {
77224cf5adSJeff Kirsher unsigned char sha_pad1[SHA1_PAD_SIZE];
78224cf5adSJeff Kirsher unsigned char sha_pad2[SHA1_PAD_SIZE];
79224cf5adSJeff Kirsher };
80224cf5adSJeff Kirsher static struct sha_pad *sha_pad;
81224cf5adSJeff Kirsher
sha_pad_init(struct sha_pad * shapad)82224cf5adSJeff Kirsher static inline void sha_pad_init(struct sha_pad *shapad)
83224cf5adSJeff Kirsher {
84224cf5adSJeff Kirsher memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1));
85224cf5adSJeff Kirsher memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2));
86224cf5adSJeff Kirsher }
87224cf5adSJeff Kirsher
88224cf5adSJeff Kirsher /*
89224cf5adSJeff Kirsher * State for an MPPE (de)compressor.
90224cf5adSJeff Kirsher */
91224cf5adSJeff Kirsher struct ppp_mppe_state {
920e5a610bSArd Biesheuvel struct arc4_ctx arc4;
93a394b3afSKees Cook struct shash_desc *sha1;
94224cf5adSJeff Kirsher unsigned char *sha1_digest;
95224cf5adSJeff Kirsher unsigned char master_key[MPPE_MAX_KEY_LEN];
96224cf5adSJeff Kirsher unsigned char session_key[MPPE_MAX_KEY_LEN];
97224cf5adSJeff Kirsher unsigned keylen; /* key length in bytes */
98224cf5adSJeff Kirsher /* NB: 128-bit == 16, 40-bit == 8! */
99224cf5adSJeff Kirsher /* If we want to support 56-bit, */
100224cf5adSJeff Kirsher /* the unit has to change to bits */
101224cf5adSJeff Kirsher unsigned char bits; /* MPPE control bits */
102224cf5adSJeff Kirsher unsigned ccount; /* 12-bit coherency count (seqno) */
103224cf5adSJeff Kirsher unsigned stateful; /* stateful mode flag */
104224cf5adSJeff Kirsher int discard; /* stateful mode packet loss flag */
105224cf5adSJeff Kirsher int sanity_errors; /* take down LCP if too many */
106224cf5adSJeff Kirsher int unit;
107224cf5adSJeff Kirsher int debug;
108224cf5adSJeff Kirsher struct compstat stats;
109224cf5adSJeff Kirsher };
110224cf5adSJeff Kirsher
111224cf5adSJeff Kirsher /* struct ppp_mppe_state.bits definitions */
112224cf5adSJeff Kirsher #define MPPE_BIT_A 0x80 /* Encryption table were (re)inititalized */
113224cf5adSJeff Kirsher #define MPPE_BIT_B 0x40 /* MPPC only (not implemented) */
114224cf5adSJeff Kirsher #define MPPE_BIT_C 0x20 /* MPPC only (not implemented) */
115224cf5adSJeff Kirsher #define MPPE_BIT_D 0x10 /* This is an encrypted frame */
116224cf5adSJeff Kirsher
117224cf5adSJeff Kirsher #define MPPE_BIT_FLUSHED MPPE_BIT_A
118224cf5adSJeff Kirsher #define MPPE_BIT_ENCRYPTED MPPE_BIT_D
119224cf5adSJeff Kirsher
120224cf5adSJeff Kirsher #define MPPE_BITS(p) ((p)[4] & 0xf0)
121224cf5adSJeff Kirsher #define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
122224cf5adSJeff Kirsher #define MPPE_CCOUNT_SPACE 0x1000 /* The size of the ccount space */
123224cf5adSJeff Kirsher
124224cf5adSJeff Kirsher #define MPPE_OVHD 2 /* MPPE overhead/packet */
125224cf5adSJeff Kirsher #define SANITY_MAX 1600 /* Max bogon factor we will tolerate */
126224cf5adSJeff Kirsher
127224cf5adSJeff Kirsher /*
128224cf5adSJeff Kirsher * Key Derivation, from RFC 3078, RFC 3079.
129224cf5adSJeff Kirsher * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
130224cf5adSJeff Kirsher */
get_new_key_from_sha(struct ppp_mppe_state * state)131224cf5adSJeff Kirsher static void get_new_key_from_sha(struct ppp_mppe_state * state)
132224cf5adSJeff Kirsher {
133a394b3afSKees Cook crypto_shash_init(state->sha1);
134a394b3afSKees Cook crypto_shash_update(state->sha1, state->master_key,
135a394b3afSKees Cook state->keylen);
136a394b3afSKees Cook crypto_shash_update(state->sha1, sha_pad->sha_pad1,
137224cf5adSJeff Kirsher sizeof(sha_pad->sha_pad1));
138a394b3afSKees Cook crypto_shash_update(state->sha1, state->session_key,
139a394b3afSKees Cook state->keylen);
140a394b3afSKees Cook crypto_shash_update(state->sha1, sha_pad->sha_pad2,
141224cf5adSJeff Kirsher sizeof(sha_pad->sha_pad2));
142a394b3afSKees Cook crypto_shash_final(state->sha1, state->sha1_digest);
143224cf5adSJeff Kirsher }
144224cf5adSJeff Kirsher
145224cf5adSJeff Kirsher /*
146224cf5adSJeff Kirsher * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
147224cf5adSJeff Kirsher * Well, not what's written there, but rather what they meant.
148224cf5adSJeff Kirsher */
mppe_rekey(struct ppp_mppe_state * state,int initial_key)149224cf5adSJeff Kirsher static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
150224cf5adSJeff Kirsher {
151224cf5adSJeff Kirsher get_new_key_from_sha(state);
152224cf5adSJeff Kirsher if (!initial_key) {
1530e5a610bSArd Biesheuvel arc4_setkey(&state->arc4, state->sha1_digest, state->keylen);
1540e5a610bSArd Biesheuvel arc4_crypt(&state->arc4, state->session_key, state->sha1_digest,
155224cf5adSJeff Kirsher state->keylen);
156224cf5adSJeff Kirsher } else {
157224cf5adSJeff Kirsher memcpy(state->session_key, state->sha1_digest, state->keylen);
158224cf5adSJeff Kirsher }
159224cf5adSJeff Kirsher if (state->keylen == 8) {
160224cf5adSJeff Kirsher /* See RFC 3078 */
161224cf5adSJeff Kirsher state->session_key[0] = 0xd1;
162224cf5adSJeff Kirsher state->session_key[1] = 0x26;
163224cf5adSJeff Kirsher state->session_key[2] = 0x9e;
164224cf5adSJeff Kirsher }
1650e5a610bSArd Biesheuvel arc4_setkey(&state->arc4, state->session_key, state->keylen);
166224cf5adSJeff Kirsher }
167224cf5adSJeff Kirsher
168224cf5adSJeff Kirsher /*
169224cf5adSJeff Kirsher * Allocate space for a (de)compressor.
170224cf5adSJeff Kirsher */
mppe_alloc(unsigned char * options,int optlen)171224cf5adSJeff Kirsher static void *mppe_alloc(unsigned char *options, int optlen)
172224cf5adSJeff Kirsher {
173224cf5adSJeff Kirsher struct ppp_mppe_state *state;
174a394b3afSKees Cook struct crypto_shash *shash;
175224cf5adSJeff Kirsher unsigned int digestsize;
176224cf5adSJeff Kirsher
177224cf5adSJeff Kirsher if (optlen != CILEN_MPPE + sizeof(state->master_key) ||
1780e5a610bSArd Biesheuvel options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
1790e5a610bSArd Biesheuvel fips_enabled)
180224cf5adSJeff Kirsher goto out;
181224cf5adSJeff Kirsher
182224cf5adSJeff Kirsher state = kzalloc(sizeof(*state), GFP_KERNEL);
183224cf5adSJeff Kirsher if (state == NULL)
184224cf5adSJeff Kirsher goto out;
185224cf5adSJeff Kirsher
186224cf5adSJeff Kirsher
187a394b3afSKees Cook shash = crypto_alloc_shash("sha1", 0, 0);
188a394b3afSKees Cook if (IS_ERR(shash))
189a394b3afSKees Cook goto out_free;
190a394b3afSKees Cook
191a394b3afSKees Cook state->sha1 = kmalloc(sizeof(*state->sha1) +
192a394b3afSKees Cook crypto_shash_descsize(shash),
193a394b3afSKees Cook GFP_KERNEL);
194a394b3afSKees Cook if (!state->sha1) {
195a394b3afSKees Cook crypto_free_shash(shash);
196224cf5adSJeff Kirsher goto out_free;
197224cf5adSJeff Kirsher }
198a394b3afSKees Cook state->sha1->tfm = shash;
199224cf5adSJeff Kirsher
200a394b3afSKees Cook digestsize = crypto_shash_digestsize(shash);
201224cf5adSJeff Kirsher if (digestsize < MPPE_MAX_KEY_LEN)
202224cf5adSJeff Kirsher goto out_free;
203224cf5adSJeff Kirsher
204224cf5adSJeff Kirsher state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
205224cf5adSJeff Kirsher if (!state->sha1_digest)
206224cf5adSJeff Kirsher goto out_free;
207224cf5adSJeff Kirsher
208224cf5adSJeff Kirsher /* Save keys. */
209224cf5adSJeff Kirsher memcpy(state->master_key, &options[CILEN_MPPE],
210224cf5adSJeff Kirsher sizeof(state->master_key));
211224cf5adSJeff Kirsher memcpy(state->session_key, state->master_key,
212224cf5adSJeff Kirsher sizeof(state->master_key));
213224cf5adSJeff Kirsher
214224cf5adSJeff Kirsher /*
215224cf5adSJeff Kirsher * We defer initial key generation until mppe_init(), as mppe_alloc()
216224cf5adSJeff Kirsher * is called frequently during negotiation.
217224cf5adSJeff Kirsher */
218224cf5adSJeff Kirsher
219224cf5adSJeff Kirsher return (void *)state;
220224cf5adSJeff Kirsher
221224cf5adSJeff Kirsher out_free:
222224cf5adSJeff Kirsher kfree(state->sha1_digest);
223a394b3afSKees Cook if (state->sha1) {
224a394b3afSKees Cook crypto_free_shash(state->sha1->tfm);
225*453431a5SWaiman Long kfree_sensitive(state->sha1);
226a394b3afSKees Cook }
227224cf5adSJeff Kirsher kfree(state);
228224cf5adSJeff Kirsher out:
229224cf5adSJeff Kirsher return NULL;
230224cf5adSJeff Kirsher }
231224cf5adSJeff Kirsher
232224cf5adSJeff Kirsher /*
233224cf5adSJeff Kirsher * Deallocate space for a (de)compressor.
234224cf5adSJeff Kirsher */
mppe_free(void * arg)235224cf5adSJeff Kirsher static void mppe_free(void *arg)
236224cf5adSJeff Kirsher {
237224cf5adSJeff Kirsher struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
238224cf5adSJeff Kirsher if (state) {
239224cf5adSJeff Kirsher kfree(state->sha1_digest);
240a394b3afSKees Cook crypto_free_shash(state->sha1->tfm);
241*453431a5SWaiman Long kfree_sensitive(state->sha1);
242*453431a5SWaiman Long kfree_sensitive(state);
243224cf5adSJeff Kirsher }
244224cf5adSJeff Kirsher }
245224cf5adSJeff Kirsher
246224cf5adSJeff Kirsher /*
247224cf5adSJeff Kirsher * Initialize (de)compressor state.
248224cf5adSJeff Kirsher */
249224cf5adSJeff Kirsher static int
mppe_init(void * arg,unsigned char * options,int optlen,int unit,int debug,const char * debugstr)250224cf5adSJeff Kirsher mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
251224cf5adSJeff Kirsher const char *debugstr)
252224cf5adSJeff Kirsher {
253224cf5adSJeff Kirsher struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
254224cf5adSJeff Kirsher unsigned char mppe_opts;
255224cf5adSJeff Kirsher
256224cf5adSJeff Kirsher if (optlen != CILEN_MPPE ||
257224cf5adSJeff Kirsher options[0] != CI_MPPE || options[1] != CILEN_MPPE)
258224cf5adSJeff Kirsher return 0;
259224cf5adSJeff Kirsher
260224cf5adSJeff Kirsher MPPE_CI_TO_OPTS(&options[2], mppe_opts);
261224cf5adSJeff Kirsher if (mppe_opts & MPPE_OPT_128)
262224cf5adSJeff Kirsher state->keylen = 16;
263224cf5adSJeff Kirsher else if (mppe_opts & MPPE_OPT_40)
264224cf5adSJeff Kirsher state->keylen = 8;
265224cf5adSJeff Kirsher else {
266224cf5adSJeff Kirsher printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
267224cf5adSJeff Kirsher unit);
268224cf5adSJeff Kirsher return 0;
269224cf5adSJeff Kirsher }
270224cf5adSJeff Kirsher if (mppe_opts & MPPE_OPT_STATEFUL)
271224cf5adSJeff Kirsher state->stateful = 1;
272224cf5adSJeff Kirsher
273224cf5adSJeff Kirsher /* Generate the initial session key. */
274224cf5adSJeff Kirsher mppe_rekey(state, 1);
275224cf5adSJeff Kirsher
276224cf5adSJeff Kirsher if (debug) {
277224cf5adSJeff Kirsher printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
278224cf5adSJeff Kirsher debugstr, unit, (state->keylen == 16) ? 128 : 40,
279224cf5adSJeff Kirsher (state->stateful) ? "stateful" : "stateless");
280224cf5adSJeff Kirsher printk(KERN_DEBUG
2814f5a9841SJoe Perches "%s[%d]: keys: master: %*phN initial session: %*phN\n",
2824f5a9841SJoe Perches debugstr, unit,
2834f5a9841SJoe Perches (int)sizeof(state->master_key), state->master_key,
2844f5a9841SJoe Perches (int)sizeof(state->session_key), state->session_key);
285224cf5adSJeff Kirsher }
286224cf5adSJeff Kirsher
287224cf5adSJeff Kirsher /*
288224cf5adSJeff Kirsher * Initialize the coherency count. The initial value is not specified
289224cf5adSJeff Kirsher * in RFC 3078, but we can make a reasonable assumption that it will
290224cf5adSJeff Kirsher * start at 0. Setting it to the max here makes the comp/decomp code
291224cf5adSJeff Kirsher * do the right thing (determined through experiment).
292224cf5adSJeff Kirsher */
293224cf5adSJeff Kirsher state->ccount = MPPE_CCOUNT_SPACE - 1;
294224cf5adSJeff Kirsher
295224cf5adSJeff Kirsher /*
296224cf5adSJeff Kirsher * Note that even though we have initialized the key table, we don't
297224cf5adSJeff Kirsher * set the FLUSHED bit. This is contrary to RFC 3078, sec. 3.1.
298224cf5adSJeff Kirsher */
299224cf5adSJeff Kirsher state->bits = MPPE_BIT_ENCRYPTED;
300224cf5adSJeff Kirsher
301224cf5adSJeff Kirsher state->unit = unit;
302224cf5adSJeff Kirsher state->debug = debug;
303224cf5adSJeff Kirsher
304224cf5adSJeff Kirsher return 1;
305224cf5adSJeff Kirsher }
306224cf5adSJeff Kirsher
307224cf5adSJeff Kirsher static int
mppe_comp_init(void * arg,unsigned char * options,int optlen,int unit,int hdrlen,int debug)308224cf5adSJeff Kirsher mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
309224cf5adSJeff Kirsher int hdrlen, int debug)
310224cf5adSJeff Kirsher {
311224cf5adSJeff Kirsher /* ARGSUSED */
312224cf5adSJeff Kirsher return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
313224cf5adSJeff Kirsher }
314224cf5adSJeff Kirsher
315224cf5adSJeff Kirsher /*
316224cf5adSJeff Kirsher * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
317224cf5adSJeff Kirsher * tell the compressor to rekey. Note that we MUST NOT rekey for
318224cf5adSJeff Kirsher * every CCP Reset-Request; we only rekey on the next xmit packet.
319224cf5adSJeff Kirsher * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
320224cf5adSJeff Kirsher * So, rekeying for every CCP Reset-Request is broken as the peer will not
321224cf5adSJeff Kirsher * know how many times we've rekeyed. (If we rekey and THEN get another
322224cf5adSJeff Kirsher * CCP Reset-Request, we must rekey again.)
323224cf5adSJeff Kirsher */
mppe_comp_reset(void * arg)324224cf5adSJeff Kirsher static void mppe_comp_reset(void *arg)
325224cf5adSJeff Kirsher {
326224cf5adSJeff Kirsher struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
327224cf5adSJeff Kirsher
328224cf5adSJeff Kirsher state->bits |= MPPE_BIT_FLUSHED;
329224cf5adSJeff Kirsher }
330224cf5adSJeff Kirsher
331224cf5adSJeff Kirsher /*
332224cf5adSJeff Kirsher * Compress (encrypt) a packet.
333224cf5adSJeff Kirsher * It's strange to call this a compressor, since the output is always
334224cf5adSJeff Kirsher * MPPE_OVHD + 2 bytes larger than the input.
335224cf5adSJeff Kirsher */
336224cf5adSJeff Kirsher static int
mppe_compress(void * arg,unsigned char * ibuf,unsigned char * obuf,int isize,int osize)337224cf5adSJeff Kirsher mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
338224cf5adSJeff Kirsher int isize, int osize)
339224cf5adSJeff Kirsher {
340224cf5adSJeff Kirsher struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
341224cf5adSJeff Kirsher int proto;
342224cf5adSJeff Kirsher
343224cf5adSJeff Kirsher /*
344224cf5adSJeff Kirsher * Check that the protocol is in the range we handle.
345224cf5adSJeff Kirsher */
346224cf5adSJeff Kirsher proto = PPP_PROTOCOL(ibuf);
347224cf5adSJeff Kirsher if (proto < 0x0021 || proto > 0x00fa)
348224cf5adSJeff Kirsher return 0;
349224cf5adSJeff Kirsher
350224cf5adSJeff Kirsher /* Make sure we have enough room to generate an encrypted packet. */
351224cf5adSJeff Kirsher if (osize < isize + MPPE_OVHD + 2) {
352224cf5adSJeff Kirsher /* Drop the packet if we should encrypt it, but can't. */
353224cf5adSJeff Kirsher printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
354224cf5adSJeff Kirsher "(have: %d need: %d)\n", state->unit,
355224cf5adSJeff Kirsher osize, osize + MPPE_OVHD + 2);
356224cf5adSJeff Kirsher return -1;
357224cf5adSJeff Kirsher }
358224cf5adSJeff Kirsher
359224cf5adSJeff Kirsher osize = isize + MPPE_OVHD + 2;
360224cf5adSJeff Kirsher
361224cf5adSJeff Kirsher /*
362224cf5adSJeff Kirsher * Copy over the PPP header and set control bits.
363224cf5adSJeff Kirsher */
364224cf5adSJeff Kirsher obuf[0] = PPP_ADDRESS(ibuf);
365224cf5adSJeff Kirsher obuf[1] = PPP_CONTROL(ibuf);
366224cf5adSJeff Kirsher put_unaligned_be16(PPP_COMP, obuf + 2);
367224cf5adSJeff Kirsher obuf += PPP_HDRLEN;
368224cf5adSJeff Kirsher
369224cf5adSJeff Kirsher state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
370224cf5adSJeff Kirsher if (state->debug >= 7)
371224cf5adSJeff Kirsher printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
372224cf5adSJeff Kirsher state->ccount);
373224cf5adSJeff Kirsher put_unaligned_be16(state->ccount, obuf);
374224cf5adSJeff Kirsher
375224cf5adSJeff Kirsher if (!state->stateful || /* stateless mode */
376224cf5adSJeff Kirsher ((state->ccount & 0xff) == 0xff) || /* "flag" packet */
377224cf5adSJeff Kirsher (state->bits & MPPE_BIT_FLUSHED)) { /* CCP Reset-Request */
378224cf5adSJeff Kirsher /* We must rekey */
379224cf5adSJeff Kirsher if (state->debug && state->stateful)
380224cf5adSJeff Kirsher printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
381224cf5adSJeff Kirsher state->unit);
382224cf5adSJeff Kirsher mppe_rekey(state, 0);
383224cf5adSJeff Kirsher state->bits |= MPPE_BIT_FLUSHED;
384224cf5adSJeff Kirsher }
385224cf5adSJeff Kirsher obuf[0] |= state->bits;
386224cf5adSJeff Kirsher state->bits &= ~MPPE_BIT_FLUSHED; /* reset for next xmit */
387224cf5adSJeff Kirsher
388224cf5adSJeff Kirsher obuf += MPPE_OVHD;
389224cf5adSJeff Kirsher ibuf += 2; /* skip to proto field */
390224cf5adSJeff Kirsher isize -= 2;
391224cf5adSJeff Kirsher
3920e5a610bSArd Biesheuvel arc4_crypt(&state->arc4, obuf, ibuf, isize);
393224cf5adSJeff Kirsher
394224cf5adSJeff Kirsher state->stats.unc_bytes += isize;
395224cf5adSJeff Kirsher state->stats.unc_packets++;
396224cf5adSJeff Kirsher state->stats.comp_bytes += osize;
397224cf5adSJeff Kirsher state->stats.comp_packets++;
398224cf5adSJeff Kirsher
399224cf5adSJeff Kirsher return osize;
400224cf5adSJeff Kirsher }
401224cf5adSJeff Kirsher
402224cf5adSJeff Kirsher /*
403224cf5adSJeff Kirsher * Since every frame grows by MPPE_OVHD + 2 bytes, this is always going
404224cf5adSJeff Kirsher * to look bad ... and the longer the link is up the worse it will get.
405224cf5adSJeff Kirsher */
mppe_comp_stats(void * arg,struct compstat * stats)406224cf5adSJeff Kirsher static void mppe_comp_stats(void *arg, struct compstat *stats)
407224cf5adSJeff Kirsher {
408224cf5adSJeff Kirsher struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
409224cf5adSJeff Kirsher
410224cf5adSJeff Kirsher *stats = state->stats;
411224cf5adSJeff Kirsher }
412224cf5adSJeff Kirsher
413224cf5adSJeff Kirsher static int
mppe_decomp_init(void * arg,unsigned char * options,int optlen,int unit,int hdrlen,int mru,int debug)414224cf5adSJeff Kirsher mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
415224cf5adSJeff Kirsher int hdrlen, int mru, int debug)
416224cf5adSJeff Kirsher {
417224cf5adSJeff Kirsher /* ARGSUSED */
418224cf5adSJeff Kirsher return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
419224cf5adSJeff Kirsher }
420224cf5adSJeff Kirsher
421224cf5adSJeff Kirsher /*
422224cf5adSJeff Kirsher * We received a CCP Reset-Ack. Just ignore it.
423224cf5adSJeff Kirsher */
mppe_decomp_reset(void * arg)424224cf5adSJeff Kirsher static void mppe_decomp_reset(void *arg)
425224cf5adSJeff Kirsher {
426224cf5adSJeff Kirsher /* ARGSUSED */
427224cf5adSJeff Kirsher return;
428224cf5adSJeff Kirsher }
429224cf5adSJeff Kirsher
430224cf5adSJeff Kirsher /*
431224cf5adSJeff Kirsher * Decompress (decrypt) an MPPE packet.
432224cf5adSJeff Kirsher */
433224cf5adSJeff Kirsher static int
mppe_decompress(void * arg,unsigned char * ibuf,int isize,unsigned char * obuf,int osize)434224cf5adSJeff Kirsher mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
435224cf5adSJeff Kirsher int osize)
436224cf5adSJeff Kirsher {
437224cf5adSJeff Kirsher struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
438224cf5adSJeff Kirsher unsigned ccount;
439224cf5adSJeff Kirsher int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
440224cf5adSJeff Kirsher
441224cf5adSJeff Kirsher if (isize <= PPP_HDRLEN + MPPE_OVHD) {
442224cf5adSJeff Kirsher if (state->debug)
443224cf5adSJeff Kirsher printk(KERN_DEBUG
444224cf5adSJeff Kirsher "mppe_decompress[%d]: short pkt (%d)\n",
445224cf5adSJeff Kirsher state->unit, isize);
446224cf5adSJeff Kirsher return DECOMP_ERROR;
447224cf5adSJeff Kirsher }
448224cf5adSJeff Kirsher
449224cf5adSJeff Kirsher /*
450224cf5adSJeff Kirsher * Make sure we have enough room to decrypt the packet.
451224cf5adSJeff Kirsher * Note that for our test we only subtract 1 byte whereas in
452224cf5adSJeff Kirsher * mppe_compress() we added 2 bytes (+MPPE_OVHD);
453224cf5adSJeff Kirsher * this is to account for possible PFC.
454224cf5adSJeff Kirsher */
455224cf5adSJeff Kirsher if (osize < isize - MPPE_OVHD - 1) {
456224cf5adSJeff Kirsher printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
457224cf5adSJeff Kirsher "(have: %d need: %d)\n", state->unit,
458224cf5adSJeff Kirsher osize, isize - MPPE_OVHD - 1);
459224cf5adSJeff Kirsher return DECOMP_ERROR;
460224cf5adSJeff Kirsher }
461224cf5adSJeff Kirsher osize = isize - MPPE_OVHD - 2; /* assume no PFC */
462224cf5adSJeff Kirsher
463224cf5adSJeff Kirsher ccount = MPPE_CCOUNT(ibuf);
464224cf5adSJeff Kirsher if (state->debug >= 7)
465224cf5adSJeff Kirsher printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
466224cf5adSJeff Kirsher state->unit, ccount);
467224cf5adSJeff Kirsher
468224cf5adSJeff Kirsher /* sanity checks -- terminate with extreme prejudice */
469224cf5adSJeff Kirsher if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
470224cf5adSJeff Kirsher printk(KERN_DEBUG
471224cf5adSJeff Kirsher "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
472224cf5adSJeff Kirsher state->unit);
473224cf5adSJeff Kirsher state->sanity_errors += 100;
47432530189SSylvain Rochet goto sanity_error;
475224cf5adSJeff Kirsher }
476224cf5adSJeff Kirsher if (!state->stateful && !flushed) {
477224cf5adSJeff Kirsher printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
478224cf5adSJeff Kirsher "stateless mode!\n", state->unit);
479224cf5adSJeff Kirsher state->sanity_errors += 100;
48032530189SSylvain Rochet goto sanity_error;
481224cf5adSJeff Kirsher }
482224cf5adSJeff Kirsher if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
483224cf5adSJeff Kirsher printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
484224cf5adSJeff Kirsher "flag packet!\n", state->unit);
485224cf5adSJeff Kirsher state->sanity_errors += 100;
48632530189SSylvain Rochet goto sanity_error;
487224cf5adSJeff Kirsher }
488224cf5adSJeff Kirsher
489224cf5adSJeff Kirsher /*
490224cf5adSJeff Kirsher * Check the coherency count.
491224cf5adSJeff Kirsher */
492224cf5adSJeff Kirsher
493224cf5adSJeff Kirsher if (!state->stateful) {
49403654763SSylvain Rochet /* Discard late packet */
49503654763SSylvain Rochet if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE
49603654763SSylvain Rochet > MPPE_CCOUNT_SPACE / 2) {
49703654763SSylvain Rochet state->sanity_errors++;
49803654763SSylvain Rochet goto sanity_error;
49903654763SSylvain Rochet }
50003654763SSylvain Rochet
501224cf5adSJeff Kirsher /* RFC 3078, sec 8.1. Rekey for every packet. */
502224cf5adSJeff Kirsher while (state->ccount != ccount) {
503224cf5adSJeff Kirsher mppe_rekey(state, 0);
504224cf5adSJeff Kirsher state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
505224cf5adSJeff Kirsher }
506224cf5adSJeff Kirsher } else {
507224cf5adSJeff Kirsher /* RFC 3078, sec 8.2. */
508224cf5adSJeff Kirsher if (!state->discard) {
509224cf5adSJeff Kirsher /* normal state */
510224cf5adSJeff Kirsher state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
511224cf5adSJeff Kirsher if (ccount != state->ccount) {
512224cf5adSJeff Kirsher /*
513224cf5adSJeff Kirsher * (ccount > state->ccount)
514224cf5adSJeff Kirsher * Packet loss detected, enter the discard state.
515224cf5adSJeff Kirsher * Signal the peer to rekey (by sending a CCP Reset-Request).
516224cf5adSJeff Kirsher */
517224cf5adSJeff Kirsher state->discard = 1;
518224cf5adSJeff Kirsher return DECOMP_ERROR;
519224cf5adSJeff Kirsher }
520224cf5adSJeff Kirsher } else {
521224cf5adSJeff Kirsher /* discard state */
522224cf5adSJeff Kirsher if (!flushed) {
523224cf5adSJeff Kirsher /* ccp.c will be silent (no additional CCP Reset-Requests). */
524224cf5adSJeff Kirsher return DECOMP_ERROR;
525224cf5adSJeff Kirsher } else {
526224cf5adSJeff Kirsher /* Rekey for every missed "flag" packet. */
527224cf5adSJeff Kirsher while ((ccount & ~0xff) !=
528224cf5adSJeff Kirsher (state->ccount & ~0xff)) {
529224cf5adSJeff Kirsher mppe_rekey(state, 0);
530224cf5adSJeff Kirsher state->ccount =
531224cf5adSJeff Kirsher (state->ccount +
532224cf5adSJeff Kirsher 256) % MPPE_CCOUNT_SPACE;
533224cf5adSJeff Kirsher }
534224cf5adSJeff Kirsher
535224cf5adSJeff Kirsher /* reset */
536224cf5adSJeff Kirsher state->discard = 0;
537224cf5adSJeff Kirsher state->ccount = ccount;
538224cf5adSJeff Kirsher /*
539224cf5adSJeff Kirsher * Another problem with RFC 3078 here. It implies that the
540224cf5adSJeff Kirsher * peer need not send a Reset-Ack packet. But RFC 1962
541224cf5adSJeff Kirsher * requires it. Hopefully, M$ does send a Reset-Ack; even
542224cf5adSJeff Kirsher * though it isn't required for MPPE synchronization, it is
543224cf5adSJeff Kirsher * required to reset CCP state.
544224cf5adSJeff Kirsher */
545224cf5adSJeff Kirsher }
546224cf5adSJeff Kirsher }
547224cf5adSJeff Kirsher if (flushed)
548224cf5adSJeff Kirsher mppe_rekey(state, 0);
549224cf5adSJeff Kirsher }
550224cf5adSJeff Kirsher
551224cf5adSJeff Kirsher /*
552224cf5adSJeff Kirsher * Fill in the first part of the PPP header. The protocol field
553224cf5adSJeff Kirsher * comes from the decrypted data.
554224cf5adSJeff Kirsher */
555224cf5adSJeff Kirsher obuf[0] = PPP_ADDRESS(ibuf); /* +1 */
556224cf5adSJeff Kirsher obuf[1] = PPP_CONTROL(ibuf); /* +1 */
557224cf5adSJeff Kirsher obuf += 2;
558224cf5adSJeff Kirsher ibuf += PPP_HDRLEN + MPPE_OVHD;
559224cf5adSJeff Kirsher isize -= PPP_HDRLEN + MPPE_OVHD; /* -6 */
560224cf5adSJeff Kirsher /* net osize: isize-4 */
561224cf5adSJeff Kirsher
562224cf5adSJeff Kirsher /*
563224cf5adSJeff Kirsher * Decrypt the first byte in order to check if it is
564224cf5adSJeff Kirsher * a compressed or uncompressed protocol field.
565224cf5adSJeff Kirsher */
5660e5a610bSArd Biesheuvel arc4_crypt(&state->arc4, obuf, ibuf, 1);
567224cf5adSJeff Kirsher
568224cf5adSJeff Kirsher /*
569224cf5adSJeff Kirsher * Do PFC decompression.
570224cf5adSJeff Kirsher * This would be nicer if we were given the actual sk_buff
571224cf5adSJeff Kirsher * instead of a char *.
572224cf5adSJeff Kirsher */
573224cf5adSJeff Kirsher if ((obuf[0] & 0x01) != 0) {
574224cf5adSJeff Kirsher obuf[1] = obuf[0];
575224cf5adSJeff Kirsher obuf[0] = 0;
576224cf5adSJeff Kirsher obuf++;
577224cf5adSJeff Kirsher osize++;
578224cf5adSJeff Kirsher }
579224cf5adSJeff Kirsher
580224cf5adSJeff Kirsher /* And finally, decrypt the rest of the packet. */
5810e5a610bSArd Biesheuvel arc4_crypt(&state->arc4, obuf + 1, ibuf + 1, isize - 1);
582224cf5adSJeff Kirsher
583224cf5adSJeff Kirsher state->stats.unc_bytes += osize;
584224cf5adSJeff Kirsher state->stats.unc_packets++;
585224cf5adSJeff Kirsher state->stats.comp_bytes += isize;
586224cf5adSJeff Kirsher state->stats.comp_packets++;
587224cf5adSJeff Kirsher
588224cf5adSJeff Kirsher /* good packet credit */
589224cf5adSJeff Kirsher state->sanity_errors >>= 1;
590224cf5adSJeff Kirsher
591224cf5adSJeff Kirsher return osize;
59232530189SSylvain Rochet
59332530189SSylvain Rochet sanity_error:
59432530189SSylvain Rochet if (state->sanity_errors < SANITY_MAX)
59532530189SSylvain Rochet return DECOMP_ERROR;
59632530189SSylvain Rochet else
59732530189SSylvain Rochet /* Take LCP down if the peer is sending too many bogons.
59832530189SSylvain Rochet * We don't want to do this for a single or just a few
59932530189SSylvain Rochet * instances since it could just be due to packet corruption.
60032530189SSylvain Rochet */
60132530189SSylvain Rochet return DECOMP_FATALERROR;
602224cf5adSJeff Kirsher }
603224cf5adSJeff Kirsher
604224cf5adSJeff Kirsher /*
605224cf5adSJeff Kirsher * Incompressible data has arrived (this should never happen!).
606224cf5adSJeff Kirsher * We should probably drop the link if the protocol is in the range
607224cf5adSJeff Kirsher * of what should be encrypted. At the least, we should drop this
608224cf5adSJeff Kirsher * packet. (How to do this?)
609224cf5adSJeff Kirsher */
mppe_incomp(void * arg,unsigned char * ibuf,int icnt)610224cf5adSJeff Kirsher static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
611224cf5adSJeff Kirsher {
612224cf5adSJeff Kirsher struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
613224cf5adSJeff Kirsher
614224cf5adSJeff Kirsher if (state->debug &&
615224cf5adSJeff Kirsher (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
616224cf5adSJeff Kirsher printk(KERN_DEBUG
617224cf5adSJeff Kirsher "mppe_incomp[%d]: incompressible (unencrypted) data! "
618224cf5adSJeff Kirsher "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
619224cf5adSJeff Kirsher
620224cf5adSJeff Kirsher state->stats.inc_bytes += icnt;
621224cf5adSJeff Kirsher state->stats.inc_packets++;
622224cf5adSJeff Kirsher state->stats.unc_bytes += icnt;
623224cf5adSJeff Kirsher state->stats.unc_packets++;
624224cf5adSJeff Kirsher }
625224cf5adSJeff Kirsher
626224cf5adSJeff Kirsher /*************************************************************
627224cf5adSJeff Kirsher * Module interface table
628224cf5adSJeff Kirsher *************************************************************/
629224cf5adSJeff Kirsher
630224cf5adSJeff Kirsher /*
631224cf5adSJeff Kirsher * Procedures exported to if_ppp.c.
632224cf5adSJeff Kirsher */
633224cf5adSJeff Kirsher static struct compressor ppp_mppe = {
634224cf5adSJeff Kirsher .compress_proto = CI_MPPE,
635224cf5adSJeff Kirsher .comp_alloc = mppe_alloc,
636224cf5adSJeff Kirsher .comp_free = mppe_free,
637224cf5adSJeff Kirsher .comp_init = mppe_comp_init,
638224cf5adSJeff Kirsher .comp_reset = mppe_comp_reset,
639224cf5adSJeff Kirsher .compress = mppe_compress,
640224cf5adSJeff Kirsher .comp_stat = mppe_comp_stats,
641224cf5adSJeff Kirsher .decomp_alloc = mppe_alloc,
642224cf5adSJeff Kirsher .decomp_free = mppe_free,
643224cf5adSJeff Kirsher .decomp_init = mppe_decomp_init,
644224cf5adSJeff Kirsher .decomp_reset = mppe_decomp_reset,
645224cf5adSJeff Kirsher .decompress = mppe_decompress,
646224cf5adSJeff Kirsher .incomp = mppe_incomp,
647224cf5adSJeff Kirsher .decomp_stat = mppe_comp_stats,
648224cf5adSJeff Kirsher .owner = THIS_MODULE,
649224cf5adSJeff Kirsher .comp_extra = MPPE_PAD,
650224cf5adSJeff Kirsher };
651224cf5adSJeff Kirsher
652224cf5adSJeff Kirsher /*
653224cf5adSJeff Kirsher * ppp_mppe_init()
654224cf5adSJeff Kirsher *
655224cf5adSJeff Kirsher * Prior to allowing load, try to load the arc4 and sha1 crypto
656224cf5adSJeff Kirsher * libraries. The actual use will be allocated later, but
657224cf5adSJeff Kirsher * this way the module will fail to insmod if they aren't available.
658224cf5adSJeff Kirsher */
659224cf5adSJeff Kirsher
ppp_mppe_init(void)660224cf5adSJeff Kirsher static int __init ppp_mppe_init(void)
661224cf5adSJeff Kirsher {
662224cf5adSJeff Kirsher int answer;
6630e5a610bSArd Biesheuvel if (fips_enabled || !crypto_has_ahash("sha1", 0, CRYPTO_ALG_ASYNC))
664224cf5adSJeff Kirsher return -ENODEV;
665224cf5adSJeff Kirsher
666224cf5adSJeff Kirsher sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
667224cf5adSJeff Kirsher if (!sha_pad)
668224cf5adSJeff Kirsher return -ENOMEM;
669224cf5adSJeff Kirsher sha_pad_init(sha_pad);
670224cf5adSJeff Kirsher
671224cf5adSJeff Kirsher answer = ppp_register_compressor(&ppp_mppe);
672224cf5adSJeff Kirsher
673224cf5adSJeff Kirsher if (answer == 0)
674224cf5adSJeff Kirsher printk(KERN_INFO "PPP MPPE Compression module registered\n");
675224cf5adSJeff Kirsher else
676224cf5adSJeff Kirsher kfree(sha_pad);
677224cf5adSJeff Kirsher
678224cf5adSJeff Kirsher return answer;
679224cf5adSJeff Kirsher }
680224cf5adSJeff Kirsher
ppp_mppe_cleanup(void)681224cf5adSJeff Kirsher static void __exit ppp_mppe_cleanup(void)
682224cf5adSJeff Kirsher {
683224cf5adSJeff Kirsher ppp_unregister_compressor(&ppp_mppe);
684224cf5adSJeff Kirsher kfree(sha_pad);
685224cf5adSJeff Kirsher }
686224cf5adSJeff Kirsher
687224cf5adSJeff Kirsher module_init(ppp_mppe_init);
688224cf5adSJeff Kirsher module_exit(ppp_mppe_cleanup);
689