1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * ppp_mppe.c - interface MPPE to the PPP code.
3*4882a593Smuzhiyun * This version is for use with Linux kernel 2.6.14+
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * By Frank Cusack <fcusack@fcusack.com>.
6*4882a593Smuzhiyun * Copyright (c) 2002,2003,2004 Google, Inc.
7*4882a593Smuzhiyun * All rights reserved.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * License:
10*4882a593Smuzhiyun * Permission to use, copy, modify, and distribute this software and its
11*4882a593Smuzhiyun * documentation is hereby granted, provided that the above copyright
12*4882a593Smuzhiyun * notice appears in all copies. This software is provided without any
13*4882a593Smuzhiyun * warranty, express or implied.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * ALTERNATIVELY, provided that this notice is retained in full, this product
16*4882a593Smuzhiyun * may be distributed under the terms of the GNU General Public License (GPL),
17*4882a593Smuzhiyun * in which case the provisions of the GPL apply INSTEAD OF those given above.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
20*4882a593Smuzhiyun * it under the terms of the GNU General Public License as published by
21*4882a593Smuzhiyun * the Free Software Foundation; either version 2 of the License, or
22*4882a593Smuzhiyun * (at your option) any later version.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful,
25*4882a593Smuzhiyun * but WITHOUT ANY WARRANTY; without even the implied warranty of
26*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27*4882a593Smuzhiyun * GNU General Public License for more details.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * You should have received a copy of the GNU General Public License
30*4882a593Smuzhiyun * along with this program; if not, see <http://www.gnu.org/licenses/>.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * Changelog:
34*4882a593Smuzhiyun * 08/12/05 - Matt Domsch <Matt_Domsch@dell.com>
35*4882a593Smuzhiyun * Only need extra skb padding on transmit, not receive.
36*4882a593Smuzhiyun * 06/18/04 - Matt Domsch <Matt_Domsch@dell.com>, Oleg Makarenko <mole@quadra.ru>
37*4882a593Smuzhiyun * Use Linux kernel 2.6 arc4 and sha1 routines rather than
38*4882a593Smuzhiyun * providing our own.
39*4882a593Smuzhiyun * 2/15/04 - TS: added #include <version.h> and testing for Kernel
40*4882a593Smuzhiyun * version before using
41*4882a593Smuzhiyun * MOD_DEC_USAGE_COUNT/MOD_INC_USAGE_COUNT which are
42*4882a593Smuzhiyun * deprecated in 2.6
43*4882a593Smuzhiyun */
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #include <crypto/arc4.h>
46*4882a593Smuzhiyun #include <crypto/hash.h>
47*4882a593Smuzhiyun #include <linux/err.h>
48*4882a593Smuzhiyun #include <linux/fips.h>
49*4882a593Smuzhiyun #include <linux/module.h>
50*4882a593Smuzhiyun #include <linux/kernel.h>
51*4882a593Smuzhiyun #include <linux/init.h>
52*4882a593Smuzhiyun #include <linux/types.h>
53*4882a593Smuzhiyun #include <linux/slab.h>
54*4882a593Smuzhiyun #include <linux/string.h>
55*4882a593Smuzhiyun #include <linux/mm.h>
56*4882a593Smuzhiyun #include <linux/ppp_defs.h>
57*4882a593Smuzhiyun #include <linux/ppp-comp.h>
58*4882a593Smuzhiyun #include <linux/scatterlist.h>
59*4882a593Smuzhiyun #include <asm/unaligned.h>
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #include "ppp_mppe.h"
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun MODULE_AUTHOR("Frank Cusack <fcusack@fcusack.com>");
64*4882a593Smuzhiyun MODULE_DESCRIPTION("Point-to-Point Protocol Microsoft Point-to-Point Encryption support");
65*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
66*4882a593Smuzhiyun MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
67*4882a593Smuzhiyun MODULE_VERSION("1.0.2");
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define SHA1_PAD_SIZE 40
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * kernel crypto API needs its arguments to be in kmalloc'd memory, not in the module
73*4882a593Smuzhiyun * static data area. That means sha_pad needs to be kmalloc'd.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun struct sha_pad {
77*4882a593Smuzhiyun unsigned char sha_pad1[SHA1_PAD_SIZE];
78*4882a593Smuzhiyun unsigned char sha_pad2[SHA1_PAD_SIZE];
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun static struct sha_pad *sha_pad;
81*4882a593Smuzhiyun
sha_pad_init(struct sha_pad * shapad)82*4882a593Smuzhiyun static inline void sha_pad_init(struct sha_pad *shapad)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1));
85*4882a593Smuzhiyun memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2));
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun * State for an MPPE (de)compressor.
90*4882a593Smuzhiyun */
91*4882a593Smuzhiyun struct ppp_mppe_state {
92*4882a593Smuzhiyun struct arc4_ctx arc4;
93*4882a593Smuzhiyun struct shash_desc *sha1;
94*4882a593Smuzhiyun unsigned char *sha1_digest;
95*4882a593Smuzhiyun unsigned char master_key[MPPE_MAX_KEY_LEN];
96*4882a593Smuzhiyun unsigned char session_key[MPPE_MAX_KEY_LEN];
97*4882a593Smuzhiyun unsigned keylen; /* key length in bytes */
98*4882a593Smuzhiyun /* NB: 128-bit == 16, 40-bit == 8! */
99*4882a593Smuzhiyun /* If we want to support 56-bit, */
100*4882a593Smuzhiyun /* the unit has to change to bits */
101*4882a593Smuzhiyun unsigned char bits; /* MPPE control bits */
102*4882a593Smuzhiyun unsigned ccount; /* 12-bit coherency count (seqno) */
103*4882a593Smuzhiyun unsigned stateful; /* stateful mode flag */
104*4882a593Smuzhiyun int discard; /* stateful mode packet loss flag */
105*4882a593Smuzhiyun int sanity_errors; /* take down LCP if too many */
106*4882a593Smuzhiyun int unit;
107*4882a593Smuzhiyun int debug;
108*4882a593Smuzhiyun struct compstat stats;
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* struct ppp_mppe_state.bits definitions */
112*4882a593Smuzhiyun #define MPPE_BIT_A 0x80 /* Encryption table were (re)inititalized */
113*4882a593Smuzhiyun #define MPPE_BIT_B 0x40 /* MPPC only (not implemented) */
114*4882a593Smuzhiyun #define MPPE_BIT_C 0x20 /* MPPC only (not implemented) */
115*4882a593Smuzhiyun #define MPPE_BIT_D 0x10 /* This is an encrypted frame */
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun #define MPPE_BIT_FLUSHED MPPE_BIT_A
118*4882a593Smuzhiyun #define MPPE_BIT_ENCRYPTED MPPE_BIT_D
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun #define MPPE_BITS(p) ((p)[4] & 0xf0)
121*4882a593Smuzhiyun #define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
122*4882a593Smuzhiyun #define MPPE_CCOUNT_SPACE 0x1000 /* The size of the ccount space */
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun #define MPPE_OVHD 2 /* MPPE overhead/packet */
125*4882a593Smuzhiyun #define SANITY_MAX 1600 /* Max bogon factor we will tolerate */
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun * Key Derivation, from RFC 3078, RFC 3079.
129*4882a593Smuzhiyun * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
130*4882a593Smuzhiyun */
get_new_key_from_sha(struct ppp_mppe_state * state)131*4882a593Smuzhiyun static void get_new_key_from_sha(struct ppp_mppe_state * state)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun crypto_shash_init(state->sha1);
134*4882a593Smuzhiyun crypto_shash_update(state->sha1, state->master_key,
135*4882a593Smuzhiyun state->keylen);
136*4882a593Smuzhiyun crypto_shash_update(state->sha1, sha_pad->sha_pad1,
137*4882a593Smuzhiyun sizeof(sha_pad->sha_pad1));
138*4882a593Smuzhiyun crypto_shash_update(state->sha1, state->session_key,
139*4882a593Smuzhiyun state->keylen);
140*4882a593Smuzhiyun crypto_shash_update(state->sha1, sha_pad->sha_pad2,
141*4882a593Smuzhiyun sizeof(sha_pad->sha_pad2));
142*4882a593Smuzhiyun crypto_shash_final(state->sha1, state->sha1_digest);
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
147*4882a593Smuzhiyun * Well, not what's written there, but rather what they meant.
148*4882a593Smuzhiyun */
mppe_rekey(struct ppp_mppe_state * state,int initial_key)149*4882a593Smuzhiyun static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun get_new_key_from_sha(state);
152*4882a593Smuzhiyun if (!initial_key) {
153*4882a593Smuzhiyun arc4_setkey(&state->arc4, state->sha1_digest, state->keylen);
154*4882a593Smuzhiyun arc4_crypt(&state->arc4, state->session_key, state->sha1_digest,
155*4882a593Smuzhiyun state->keylen);
156*4882a593Smuzhiyun } else {
157*4882a593Smuzhiyun memcpy(state->session_key, state->sha1_digest, state->keylen);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun if (state->keylen == 8) {
160*4882a593Smuzhiyun /* See RFC 3078 */
161*4882a593Smuzhiyun state->session_key[0] = 0xd1;
162*4882a593Smuzhiyun state->session_key[1] = 0x26;
163*4882a593Smuzhiyun state->session_key[2] = 0x9e;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun arc4_setkey(&state->arc4, state->session_key, state->keylen);
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /*
169*4882a593Smuzhiyun * Allocate space for a (de)compressor.
170*4882a593Smuzhiyun */
mppe_alloc(unsigned char * options,int optlen)171*4882a593Smuzhiyun static void *mppe_alloc(unsigned char *options, int optlen)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun struct ppp_mppe_state *state;
174*4882a593Smuzhiyun struct crypto_shash *shash;
175*4882a593Smuzhiyun unsigned int digestsize;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun if (optlen != CILEN_MPPE + sizeof(state->master_key) ||
178*4882a593Smuzhiyun options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
179*4882a593Smuzhiyun fips_enabled)
180*4882a593Smuzhiyun goto out;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun state = kzalloc(sizeof(*state), GFP_KERNEL);
183*4882a593Smuzhiyun if (state == NULL)
184*4882a593Smuzhiyun goto out;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun shash = crypto_alloc_shash("sha1", 0, 0);
188*4882a593Smuzhiyun if (IS_ERR(shash))
189*4882a593Smuzhiyun goto out_free;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun state->sha1 = kmalloc(sizeof(*state->sha1) +
192*4882a593Smuzhiyun crypto_shash_descsize(shash),
193*4882a593Smuzhiyun GFP_KERNEL);
194*4882a593Smuzhiyun if (!state->sha1) {
195*4882a593Smuzhiyun crypto_free_shash(shash);
196*4882a593Smuzhiyun goto out_free;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun state->sha1->tfm = shash;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun digestsize = crypto_shash_digestsize(shash);
201*4882a593Smuzhiyun if (digestsize < MPPE_MAX_KEY_LEN)
202*4882a593Smuzhiyun goto out_free;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
205*4882a593Smuzhiyun if (!state->sha1_digest)
206*4882a593Smuzhiyun goto out_free;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /* Save keys. */
209*4882a593Smuzhiyun memcpy(state->master_key, &options[CILEN_MPPE],
210*4882a593Smuzhiyun sizeof(state->master_key));
211*4882a593Smuzhiyun memcpy(state->session_key, state->master_key,
212*4882a593Smuzhiyun sizeof(state->master_key));
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun /*
215*4882a593Smuzhiyun * We defer initial key generation until mppe_init(), as mppe_alloc()
216*4882a593Smuzhiyun * is called frequently during negotiation.
217*4882a593Smuzhiyun */
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun return (void *)state;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun out_free:
222*4882a593Smuzhiyun kfree(state->sha1_digest);
223*4882a593Smuzhiyun if (state->sha1) {
224*4882a593Smuzhiyun crypto_free_shash(state->sha1->tfm);
225*4882a593Smuzhiyun kfree_sensitive(state->sha1);
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun kfree(state);
228*4882a593Smuzhiyun out:
229*4882a593Smuzhiyun return NULL;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /*
233*4882a593Smuzhiyun * Deallocate space for a (de)compressor.
234*4882a593Smuzhiyun */
mppe_free(void * arg)235*4882a593Smuzhiyun static void mppe_free(void *arg)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
238*4882a593Smuzhiyun if (state) {
239*4882a593Smuzhiyun kfree(state->sha1_digest);
240*4882a593Smuzhiyun crypto_free_shash(state->sha1->tfm);
241*4882a593Smuzhiyun kfree_sensitive(state->sha1);
242*4882a593Smuzhiyun kfree_sensitive(state);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /*
247*4882a593Smuzhiyun * Initialize (de)compressor state.
248*4882a593Smuzhiyun */
249*4882a593Smuzhiyun static int
mppe_init(void * arg,unsigned char * options,int optlen,int unit,int debug,const char * debugstr)250*4882a593Smuzhiyun mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
251*4882a593Smuzhiyun const char *debugstr)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
254*4882a593Smuzhiyun unsigned char mppe_opts;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun if (optlen != CILEN_MPPE ||
257*4882a593Smuzhiyun options[0] != CI_MPPE || options[1] != CILEN_MPPE)
258*4882a593Smuzhiyun return 0;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun MPPE_CI_TO_OPTS(&options[2], mppe_opts);
261*4882a593Smuzhiyun if (mppe_opts & MPPE_OPT_128)
262*4882a593Smuzhiyun state->keylen = 16;
263*4882a593Smuzhiyun else if (mppe_opts & MPPE_OPT_40)
264*4882a593Smuzhiyun state->keylen = 8;
265*4882a593Smuzhiyun else {
266*4882a593Smuzhiyun printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
267*4882a593Smuzhiyun unit);
268*4882a593Smuzhiyun return 0;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun if (mppe_opts & MPPE_OPT_STATEFUL)
271*4882a593Smuzhiyun state->stateful = 1;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /* Generate the initial session key. */
274*4882a593Smuzhiyun mppe_rekey(state, 1);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun if (debug) {
277*4882a593Smuzhiyun printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
278*4882a593Smuzhiyun debugstr, unit, (state->keylen == 16) ? 128 : 40,
279*4882a593Smuzhiyun (state->stateful) ? "stateful" : "stateless");
280*4882a593Smuzhiyun printk(KERN_DEBUG
281*4882a593Smuzhiyun "%s[%d]: keys: master: %*phN initial session: %*phN\n",
282*4882a593Smuzhiyun debugstr, unit,
283*4882a593Smuzhiyun (int)sizeof(state->master_key), state->master_key,
284*4882a593Smuzhiyun (int)sizeof(state->session_key), state->session_key);
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun * Initialize the coherency count. The initial value is not specified
289*4882a593Smuzhiyun * in RFC 3078, but we can make a reasonable assumption that it will
290*4882a593Smuzhiyun * start at 0. Setting it to the max here makes the comp/decomp code
291*4882a593Smuzhiyun * do the right thing (determined through experiment).
292*4882a593Smuzhiyun */
293*4882a593Smuzhiyun state->ccount = MPPE_CCOUNT_SPACE - 1;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /*
296*4882a593Smuzhiyun * Note that even though we have initialized the key table, we don't
297*4882a593Smuzhiyun * set the FLUSHED bit. This is contrary to RFC 3078, sec. 3.1.
298*4882a593Smuzhiyun */
299*4882a593Smuzhiyun state->bits = MPPE_BIT_ENCRYPTED;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun state->unit = unit;
302*4882a593Smuzhiyun state->debug = debug;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun return 1;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun static int
mppe_comp_init(void * arg,unsigned char * options,int optlen,int unit,int hdrlen,int debug)308*4882a593Smuzhiyun mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
309*4882a593Smuzhiyun int hdrlen, int debug)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun /* ARGSUSED */
312*4882a593Smuzhiyun return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /*
316*4882a593Smuzhiyun * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
317*4882a593Smuzhiyun * tell the compressor to rekey. Note that we MUST NOT rekey for
318*4882a593Smuzhiyun * every CCP Reset-Request; we only rekey on the next xmit packet.
319*4882a593Smuzhiyun * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
320*4882a593Smuzhiyun * So, rekeying for every CCP Reset-Request is broken as the peer will not
321*4882a593Smuzhiyun * know how many times we've rekeyed. (If we rekey and THEN get another
322*4882a593Smuzhiyun * CCP Reset-Request, we must rekey again.)
323*4882a593Smuzhiyun */
mppe_comp_reset(void * arg)324*4882a593Smuzhiyun static void mppe_comp_reset(void *arg)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun state->bits |= MPPE_BIT_FLUSHED;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun /*
332*4882a593Smuzhiyun * Compress (encrypt) a packet.
333*4882a593Smuzhiyun * It's strange to call this a compressor, since the output is always
334*4882a593Smuzhiyun * MPPE_OVHD + 2 bytes larger than the input.
335*4882a593Smuzhiyun */
336*4882a593Smuzhiyun static int
mppe_compress(void * arg,unsigned char * ibuf,unsigned char * obuf,int isize,int osize)337*4882a593Smuzhiyun mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
338*4882a593Smuzhiyun int isize, int osize)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
341*4882a593Smuzhiyun int proto;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun /*
344*4882a593Smuzhiyun * Check that the protocol is in the range we handle.
345*4882a593Smuzhiyun */
346*4882a593Smuzhiyun proto = PPP_PROTOCOL(ibuf);
347*4882a593Smuzhiyun if (proto < 0x0021 || proto > 0x00fa)
348*4882a593Smuzhiyun return 0;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun /* Make sure we have enough room to generate an encrypted packet. */
351*4882a593Smuzhiyun if (osize < isize + MPPE_OVHD + 2) {
352*4882a593Smuzhiyun /* Drop the packet if we should encrypt it, but can't. */
353*4882a593Smuzhiyun printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
354*4882a593Smuzhiyun "(have: %d need: %d)\n", state->unit,
355*4882a593Smuzhiyun osize, osize + MPPE_OVHD + 2);
356*4882a593Smuzhiyun return -1;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun osize = isize + MPPE_OVHD + 2;
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun /*
362*4882a593Smuzhiyun * Copy over the PPP header and set control bits.
363*4882a593Smuzhiyun */
364*4882a593Smuzhiyun obuf[0] = PPP_ADDRESS(ibuf);
365*4882a593Smuzhiyun obuf[1] = PPP_CONTROL(ibuf);
366*4882a593Smuzhiyun put_unaligned_be16(PPP_COMP, obuf + 2);
367*4882a593Smuzhiyun obuf += PPP_HDRLEN;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
370*4882a593Smuzhiyun if (state->debug >= 7)
371*4882a593Smuzhiyun printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
372*4882a593Smuzhiyun state->ccount);
373*4882a593Smuzhiyun put_unaligned_be16(state->ccount, obuf);
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun if (!state->stateful || /* stateless mode */
376*4882a593Smuzhiyun ((state->ccount & 0xff) == 0xff) || /* "flag" packet */
377*4882a593Smuzhiyun (state->bits & MPPE_BIT_FLUSHED)) { /* CCP Reset-Request */
378*4882a593Smuzhiyun /* We must rekey */
379*4882a593Smuzhiyun if (state->debug && state->stateful)
380*4882a593Smuzhiyun printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
381*4882a593Smuzhiyun state->unit);
382*4882a593Smuzhiyun mppe_rekey(state, 0);
383*4882a593Smuzhiyun state->bits |= MPPE_BIT_FLUSHED;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun obuf[0] |= state->bits;
386*4882a593Smuzhiyun state->bits &= ~MPPE_BIT_FLUSHED; /* reset for next xmit */
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun obuf += MPPE_OVHD;
389*4882a593Smuzhiyun ibuf += 2; /* skip to proto field */
390*4882a593Smuzhiyun isize -= 2;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun arc4_crypt(&state->arc4, obuf, ibuf, isize);
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun state->stats.unc_bytes += isize;
395*4882a593Smuzhiyun state->stats.unc_packets++;
396*4882a593Smuzhiyun state->stats.comp_bytes += osize;
397*4882a593Smuzhiyun state->stats.comp_packets++;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun return osize;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun /*
403*4882a593Smuzhiyun * Since every frame grows by MPPE_OVHD + 2 bytes, this is always going
404*4882a593Smuzhiyun * to look bad ... and the longer the link is up the worse it will get.
405*4882a593Smuzhiyun */
mppe_comp_stats(void * arg,struct compstat * stats)406*4882a593Smuzhiyun static void mppe_comp_stats(void *arg, struct compstat *stats)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun *stats = state->stats;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun static int
mppe_decomp_init(void * arg,unsigned char * options,int optlen,int unit,int hdrlen,int mru,int debug)414*4882a593Smuzhiyun mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
415*4882a593Smuzhiyun int hdrlen, int mru, int debug)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun /* ARGSUSED */
418*4882a593Smuzhiyun return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun /*
422*4882a593Smuzhiyun * We received a CCP Reset-Ack. Just ignore it.
423*4882a593Smuzhiyun */
mppe_decomp_reset(void * arg)424*4882a593Smuzhiyun static void mppe_decomp_reset(void *arg)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun /* ARGSUSED */
427*4882a593Smuzhiyun return;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun /*
431*4882a593Smuzhiyun * Decompress (decrypt) an MPPE packet.
432*4882a593Smuzhiyun */
433*4882a593Smuzhiyun static int
mppe_decompress(void * arg,unsigned char * ibuf,int isize,unsigned char * obuf,int osize)434*4882a593Smuzhiyun mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
435*4882a593Smuzhiyun int osize)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
438*4882a593Smuzhiyun unsigned ccount;
439*4882a593Smuzhiyun int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun if (isize <= PPP_HDRLEN + MPPE_OVHD) {
442*4882a593Smuzhiyun if (state->debug)
443*4882a593Smuzhiyun printk(KERN_DEBUG
444*4882a593Smuzhiyun "mppe_decompress[%d]: short pkt (%d)\n",
445*4882a593Smuzhiyun state->unit, isize);
446*4882a593Smuzhiyun return DECOMP_ERROR;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /*
450*4882a593Smuzhiyun * Make sure we have enough room to decrypt the packet.
451*4882a593Smuzhiyun * Note that for our test we only subtract 1 byte whereas in
452*4882a593Smuzhiyun * mppe_compress() we added 2 bytes (+MPPE_OVHD);
453*4882a593Smuzhiyun * this is to account for possible PFC.
454*4882a593Smuzhiyun */
455*4882a593Smuzhiyun if (osize < isize - MPPE_OVHD - 1) {
456*4882a593Smuzhiyun printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
457*4882a593Smuzhiyun "(have: %d need: %d)\n", state->unit,
458*4882a593Smuzhiyun osize, isize - MPPE_OVHD - 1);
459*4882a593Smuzhiyun return DECOMP_ERROR;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun osize = isize - MPPE_OVHD - 2; /* assume no PFC */
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun ccount = MPPE_CCOUNT(ibuf);
464*4882a593Smuzhiyun if (state->debug >= 7)
465*4882a593Smuzhiyun printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
466*4882a593Smuzhiyun state->unit, ccount);
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun /* sanity checks -- terminate with extreme prejudice */
469*4882a593Smuzhiyun if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
470*4882a593Smuzhiyun printk(KERN_DEBUG
471*4882a593Smuzhiyun "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
472*4882a593Smuzhiyun state->unit);
473*4882a593Smuzhiyun state->sanity_errors += 100;
474*4882a593Smuzhiyun goto sanity_error;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun if (!state->stateful && !flushed) {
477*4882a593Smuzhiyun printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
478*4882a593Smuzhiyun "stateless mode!\n", state->unit);
479*4882a593Smuzhiyun state->sanity_errors += 100;
480*4882a593Smuzhiyun goto sanity_error;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
483*4882a593Smuzhiyun printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
484*4882a593Smuzhiyun "flag packet!\n", state->unit);
485*4882a593Smuzhiyun state->sanity_errors += 100;
486*4882a593Smuzhiyun goto sanity_error;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun /*
490*4882a593Smuzhiyun * Check the coherency count.
491*4882a593Smuzhiyun */
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun if (!state->stateful) {
494*4882a593Smuzhiyun /* Discard late packet */
495*4882a593Smuzhiyun if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE
496*4882a593Smuzhiyun > MPPE_CCOUNT_SPACE / 2) {
497*4882a593Smuzhiyun state->sanity_errors++;
498*4882a593Smuzhiyun goto sanity_error;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun /* RFC 3078, sec 8.1. Rekey for every packet. */
502*4882a593Smuzhiyun while (state->ccount != ccount) {
503*4882a593Smuzhiyun mppe_rekey(state, 0);
504*4882a593Smuzhiyun state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun } else {
507*4882a593Smuzhiyun /* RFC 3078, sec 8.2. */
508*4882a593Smuzhiyun if (!state->discard) {
509*4882a593Smuzhiyun /* normal state */
510*4882a593Smuzhiyun state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
511*4882a593Smuzhiyun if (ccount != state->ccount) {
512*4882a593Smuzhiyun /*
513*4882a593Smuzhiyun * (ccount > state->ccount)
514*4882a593Smuzhiyun * Packet loss detected, enter the discard state.
515*4882a593Smuzhiyun * Signal the peer to rekey (by sending a CCP Reset-Request).
516*4882a593Smuzhiyun */
517*4882a593Smuzhiyun state->discard = 1;
518*4882a593Smuzhiyun return DECOMP_ERROR;
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun } else {
521*4882a593Smuzhiyun /* discard state */
522*4882a593Smuzhiyun if (!flushed) {
523*4882a593Smuzhiyun /* ccp.c will be silent (no additional CCP Reset-Requests). */
524*4882a593Smuzhiyun return DECOMP_ERROR;
525*4882a593Smuzhiyun } else {
526*4882a593Smuzhiyun /* Rekey for every missed "flag" packet. */
527*4882a593Smuzhiyun while ((ccount & ~0xff) !=
528*4882a593Smuzhiyun (state->ccount & ~0xff)) {
529*4882a593Smuzhiyun mppe_rekey(state, 0);
530*4882a593Smuzhiyun state->ccount =
531*4882a593Smuzhiyun (state->ccount +
532*4882a593Smuzhiyun 256) % MPPE_CCOUNT_SPACE;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun /* reset */
536*4882a593Smuzhiyun state->discard = 0;
537*4882a593Smuzhiyun state->ccount = ccount;
538*4882a593Smuzhiyun /*
539*4882a593Smuzhiyun * Another problem with RFC 3078 here. It implies that the
540*4882a593Smuzhiyun * peer need not send a Reset-Ack packet. But RFC 1962
541*4882a593Smuzhiyun * requires it. Hopefully, M$ does send a Reset-Ack; even
542*4882a593Smuzhiyun * though it isn't required for MPPE synchronization, it is
543*4882a593Smuzhiyun * required to reset CCP state.
544*4882a593Smuzhiyun */
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun if (flushed)
548*4882a593Smuzhiyun mppe_rekey(state, 0);
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun /*
552*4882a593Smuzhiyun * Fill in the first part of the PPP header. The protocol field
553*4882a593Smuzhiyun * comes from the decrypted data.
554*4882a593Smuzhiyun */
555*4882a593Smuzhiyun obuf[0] = PPP_ADDRESS(ibuf); /* +1 */
556*4882a593Smuzhiyun obuf[1] = PPP_CONTROL(ibuf); /* +1 */
557*4882a593Smuzhiyun obuf += 2;
558*4882a593Smuzhiyun ibuf += PPP_HDRLEN + MPPE_OVHD;
559*4882a593Smuzhiyun isize -= PPP_HDRLEN + MPPE_OVHD; /* -6 */
560*4882a593Smuzhiyun /* net osize: isize-4 */
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun /*
563*4882a593Smuzhiyun * Decrypt the first byte in order to check if it is
564*4882a593Smuzhiyun * a compressed or uncompressed protocol field.
565*4882a593Smuzhiyun */
566*4882a593Smuzhiyun arc4_crypt(&state->arc4, obuf, ibuf, 1);
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun /*
569*4882a593Smuzhiyun * Do PFC decompression.
570*4882a593Smuzhiyun * This would be nicer if we were given the actual sk_buff
571*4882a593Smuzhiyun * instead of a char *.
572*4882a593Smuzhiyun */
573*4882a593Smuzhiyun if ((obuf[0] & 0x01) != 0) {
574*4882a593Smuzhiyun obuf[1] = obuf[0];
575*4882a593Smuzhiyun obuf[0] = 0;
576*4882a593Smuzhiyun obuf++;
577*4882a593Smuzhiyun osize++;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun /* And finally, decrypt the rest of the packet. */
581*4882a593Smuzhiyun arc4_crypt(&state->arc4, obuf + 1, ibuf + 1, isize - 1);
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun state->stats.unc_bytes += osize;
584*4882a593Smuzhiyun state->stats.unc_packets++;
585*4882a593Smuzhiyun state->stats.comp_bytes += isize;
586*4882a593Smuzhiyun state->stats.comp_packets++;
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun /* good packet credit */
589*4882a593Smuzhiyun state->sanity_errors >>= 1;
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun return osize;
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun sanity_error:
594*4882a593Smuzhiyun if (state->sanity_errors < SANITY_MAX)
595*4882a593Smuzhiyun return DECOMP_ERROR;
596*4882a593Smuzhiyun else
597*4882a593Smuzhiyun /* Take LCP down if the peer is sending too many bogons.
598*4882a593Smuzhiyun * We don't want to do this for a single or just a few
599*4882a593Smuzhiyun * instances since it could just be due to packet corruption.
600*4882a593Smuzhiyun */
601*4882a593Smuzhiyun return DECOMP_FATALERROR;
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun /*
605*4882a593Smuzhiyun * Incompressible data has arrived (this should never happen!).
606*4882a593Smuzhiyun * We should probably drop the link if the protocol is in the range
607*4882a593Smuzhiyun * of what should be encrypted. At the least, we should drop this
608*4882a593Smuzhiyun * packet. (How to do this?)
609*4882a593Smuzhiyun */
mppe_incomp(void * arg,unsigned char * ibuf,int icnt)610*4882a593Smuzhiyun static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun if (state->debug &&
615*4882a593Smuzhiyun (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
616*4882a593Smuzhiyun printk(KERN_DEBUG
617*4882a593Smuzhiyun "mppe_incomp[%d]: incompressible (unencrypted) data! "
618*4882a593Smuzhiyun "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun state->stats.inc_bytes += icnt;
621*4882a593Smuzhiyun state->stats.inc_packets++;
622*4882a593Smuzhiyun state->stats.unc_bytes += icnt;
623*4882a593Smuzhiyun state->stats.unc_packets++;
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun /*************************************************************
627*4882a593Smuzhiyun * Module interface table
628*4882a593Smuzhiyun *************************************************************/
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun /*
631*4882a593Smuzhiyun * Procedures exported to if_ppp.c.
632*4882a593Smuzhiyun */
633*4882a593Smuzhiyun static struct compressor ppp_mppe = {
634*4882a593Smuzhiyun .compress_proto = CI_MPPE,
635*4882a593Smuzhiyun .comp_alloc = mppe_alloc,
636*4882a593Smuzhiyun .comp_free = mppe_free,
637*4882a593Smuzhiyun .comp_init = mppe_comp_init,
638*4882a593Smuzhiyun .comp_reset = mppe_comp_reset,
639*4882a593Smuzhiyun .compress = mppe_compress,
640*4882a593Smuzhiyun .comp_stat = mppe_comp_stats,
641*4882a593Smuzhiyun .decomp_alloc = mppe_alloc,
642*4882a593Smuzhiyun .decomp_free = mppe_free,
643*4882a593Smuzhiyun .decomp_init = mppe_decomp_init,
644*4882a593Smuzhiyun .decomp_reset = mppe_decomp_reset,
645*4882a593Smuzhiyun .decompress = mppe_decompress,
646*4882a593Smuzhiyun .incomp = mppe_incomp,
647*4882a593Smuzhiyun .decomp_stat = mppe_comp_stats,
648*4882a593Smuzhiyun .owner = THIS_MODULE,
649*4882a593Smuzhiyun .comp_extra = MPPE_PAD,
650*4882a593Smuzhiyun };
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun /*
653*4882a593Smuzhiyun * ppp_mppe_init()
654*4882a593Smuzhiyun *
655*4882a593Smuzhiyun * Prior to allowing load, try to load the arc4 and sha1 crypto
656*4882a593Smuzhiyun * libraries. The actual use will be allocated later, but
657*4882a593Smuzhiyun * this way the module will fail to insmod if they aren't available.
658*4882a593Smuzhiyun */
659*4882a593Smuzhiyun
ppp_mppe_init(void)660*4882a593Smuzhiyun static int __init ppp_mppe_init(void)
661*4882a593Smuzhiyun {
662*4882a593Smuzhiyun int answer;
663*4882a593Smuzhiyun if (fips_enabled || !crypto_has_ahash("sha1", 0, CRYPTO_ALG_ASYNC))
664*4882a593Smuzhiyun return -ENODEV;
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
667*4882a593Smuzhiyun if (!sha_pad)
668*4882a593Smuzhiyun return -ENOMEM;
669*4882a593Smuzhiyun sha_pad_init(sha_pad);
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun answer = ppp_register_compressor(&ppp_mppe);
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun if (answer == 0)
674*4882a593Smuzhiyun printk(KERN_INFO "PPP MPPE Compression module registered\n");
675*4882a593Smuzhiyun else
676*4882a593Smuzhiyun kfree(sha_pad);
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun return answer;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun
ppp_mppe_cleanup(void)681*4882a593Smuzhiyun static void __exit ppp_mppe_cleanup(void)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun ppp_unregister_compressor(&ppp_mppe);
684*4882a593Smuzhiyun kfree(sha_pad);
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun module_init(ppp_mppe_init);
688*4882a593Smuzhiyun module_exit(ppp_mppe_cleanup);
689