fix imagebuilder package lists for ar7
[openwrt.git] / target / linux / linux-2.6 / patches / generic / 101-mppe-mppc-1.3.patch
1 diff -ruN linux-2.6.12.orig/drivers/net/Kconfig linux-2.6.12/drivers/net/Kconfig
2 --- linux-2.6.12.orig/drivers/net/Kconfig 2005-06-28 19:57:16.000000000 +0200
3 +++ linux-2.6.12/drivers/net/Kconfig 2005-06-28 20:07:01.000000000 +0200
4 @@ -2417,6 +2417,32 @@
5 module; it is called bsd_comp and will show up in the directory
6 modules once you have said "make modules". If unsure, say N.
7
8 +config PPP_MPPE_MPPC
9 + tristate "Microsoft PPP compression/encryption (MPPC/MPPE)"
10 + depends on PPP
11 + select CRYPTO_SHA1
12 + select CRYPTO_ARC4
13 + ---help---
14 + Support for the Microsoft Point-To-Point Compression (RFC2118) and
15 + Microsoft Point-To-Point Encryption (RFC3078). These protocols are
16 + supported by Microsoft Windows and wide range of "hardware" access
17 + servers. MPPE is common protocol in Virtual Private Networks. According
18 + to RFC3078, MPPE supports 40, 56 and 128-bit key lengths. Depending on
19 + PPP daemon configuration on both ends of the link, following scenarios
20 + are possible:
21 + - only compression (MPPC) is used,
22 + - only encryption (MPPE) is used,
23 + - compression and encryption (MPPC+MPPE) are used.
24 +
25 + Please note that Hi/Fn (http://www.hifn.com) holds patent on MPPC so
26 + you should check if this patent is valid in your country in order to
27 + avoid legal problems.
28 +
29 + For more information please visit http://free.polbox.pl/h/hs001
30 +
31 + To compile this driver as a module, choose M here. The module will
32 + be called ppp_mppe_mppc.ko.
33 +
34 config PPPOE
35 tristate "PPP over Ethernet (EXPERIMENTAL)"
36 depends on EXPERIMENTAL && PPP
37 diff -ruN linux-2.6.12.orig/drivers/net/Makefile linux-2.6.12/drivers/net/Makefile
38 --- linux-2.6.12.orig/drivers/net/Makefile 2005-06-28 19:57:16.000000000 +0200
39 +++ linux-2.6.12/drivers/net/Makefile 2005-06-28 20:07:01.000000000 +0200
40 @@ -105,6 +105,7 @@
41 obj-$(CONFIG_PPP_SYNC_TTY) += ppp_synctty.o
42 obj-$(CONFIG_PPP_DEFLATE) += ppp_deflate.o
43 obj-$(CONFIG_PPP_BSDCOMP) += bsd_comp.o
44 +obj-$(CONFIG_PPP_MPPE_MPPC) += ppp_mppe_mppc.o
45 obj-$(CONFIG_PPPOE) += pppox.o pppoe.o
46
47 obj-$(CONFIG_SLIP) += slip.o
48 diff -ruN linux-2.6.12.orig/drivers/net/ppp_generic.c linux-2.6.12/drivers/net/ppp_generic.c
49 --- linux-2.6.12.orig/drivers/net/ppp_generic.c 2005-06-28 19:57:20.000000000 +0200
50 +++ linux-2.6.12/drivers/net/ppp_generic.c 2005-06-28 20:07:01.000000000 +0200
51 @@ -19,7 +19,7 @@
52 * PPP driver, written by Michael Callahan and Al Longyear, and
53 * subsequently hacked by Paul Mackerras.
54 *
55 - * ==FILEVERSION 20041108==
56 + * ==FILEVERSION 20050110==
57 */
58
59 #include <linux/config.h>
60 @@ -105,6 +105,7 @@
61 spinlock_t rlock; /* lock for receive side 58 */
62 spinlock_t wlock; /* lock for transmit side 5c */
63 int mru; /* max receive unit 60 */
64 + int mru_alloc; /* MAX(1500,MRU) for dev_alloc_skb() */
65 unsigned int flags; /* control bits 64 */
66 unsigned int xstate; /* transmit state bits 68 */
67 unsigned int rstate; /* receive state bits 6c */
68 @@ -632,7 +633,9 @@
69 case PPPIOCSMRU:
70 if (get_user(val, p))
71 break;
72 - ppp->mru = val;
73 + ppp->mru_alloc = ppp->mru = val;
74 + if (ppp->mru_alloc < PPP_MRU)
75 + ppp->mru_alloc = PPP_MRU; /* increase for broken peers */
76 err = 0;
77 break;
78
79 @@ -1107,14 +1110,37 @@
80 case PPP_CCP:
81 /* peek at outbound CCP frames */
82 ppp_ccp_peek(ppp, skb, 0);
83 + /*
84 + * When LZS or MPPE/MPPC has been negotiated we don't send
85 + * CCP_RESETACK after receiving CCP_RESETREQ; in fact pppd
86 + * sends such a packet but we silently discard it here
87 + */
88 + if (CCP_CODE(skb->data+2) == CCP_RESETACK
89 + && (ppp->xcomp->compress_proto == CI_MPPE
90 + || ppp->xcomp->compress_proto == CI_LZS)) {
91 + --ppp->stats.tx_packets;
92 + ppp->stats.tx_bytes -= skb->len - 2;
93 + kfree_skb(skb);
94 + return;
95 + }
96 break;
97 }
98
99 /* try to do packet compression */
100 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
101 && proto != PPP_LCP && proto != PPP_CCP) {
102 - new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
103 - GFP_ATOMIC);
104 + int comp_ovhd = 0;
105 + /*
106 + * because of possible data expansion when MPPC or LZS
107 + * is used, allocate compressor's buffer 12.5% bigger
108 + * than MTU
109 + */
110 + if (ppp->xcomp->compress_proto == CI_MPPE)
111 + comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + MPPE_OVHD;
112 + else if (ppp->xcomp->compress_proto == CI_LZS)
113 + comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + LZS_OVHD;
114 + new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len
115 + + comp_ovhd, GFP_ATOMIC);
116 if (new_skb == 0) {
117 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
118 goto drop;
119 @@ -1132,9 +1158,21 @@
120 skb = new_skb;
121 skb_put(skb, len);
122 skb_pull(skb, 2); /* pull off A/C bytes */
123 - } else {
124 + } else if (len == 0) {
125 /* didn't compress, or CCP not up yet */
126 kfree_skb(new_skb);
127 + } else {
128 + /*
129 + * (len < 0)
130 + * MPPE requires that we do not send unencrypted
131 + * frames. The compressor will return -1 if we
132 + * should drop the frame. We cannot simply test
133 + * the compress_proto because MPPE and MPPC share
134 + * the same number.
135 + */
136 + printk(KERN_ERR "ppp: compressor dropped pkt\n");
137 + kfree_skb(new_skb);
138 + goto drop;
139 }
140 }
141
142 @@ -1640,14 +1678,15 @@
143 goto err;
144
145 if (proto == PPP_COMP) {
146 - ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
147 + ns = dev_alloc_skb(ppp->mru_alloc + PPP_HDRLEN);
148 if (ns == 0) {
149 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
150 goto err;
151 }
152 /* the decompressor still expects the A/C bytes in the hdr */
153 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
154 - skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
155 + skb->len + 2, ns->data,
156 + ppp->mru_alloc + PPP_HDRLEN);
157 if (len < 0) {
158 /* Pass the compressed frame to pppd as an
159 error indication. */
160 @@ -1673,7 +1712,14 @@
161 return skb;
162
163 err:
164 - ppp->rstate |= SC_DC_ERROR;
165 + if (ppp->rcomp->compress_proto != CI_MPPE
166 + && ppp->rcomp->compress_proto != CI_LZS) {
167 + /*
168 + * If decompression protocol isn't MPPE/MPPC or LZS, we set
169 + * SC_DC_ERROR flag and wait for CCP_RESETACK
170 + */
171 + ppp->rstate |= SC_DC_ERROR;
172 + }
173 ppp_receive_error(ppp);
174 return skb;
175 }
176 @@ -2349,6 +2395,7 @@
177 memset(ppp, 0, sizeof(struct ppp));
178
179 ppp->mru = PPP_MRU;
180 + ppp->mru_alloc = PPP_MRU;
181 init_ppp_file(&ppp->file, INTERFACE);
182 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
183 for (i = 0; i < NUM_NP; ++i)
184 diff -ruN linux-2.6.12.orig/drivers/net/ppp_mppe_mppc.c linux-2.6.12/drivers/net/ppp_mppe_mppc.c
185 --- linux-2.6.12.orig/drivers/net/ppp_mppe_mppc.c 1970-01-01 01:00:00.000000000 +0100
186 +++ linux-2.6.12/drivers/net/ppp_mppe_mppc.c 2005-06-28 20:07:01.000000000 +0200
187 @@ -0,0 +1,1299 @@
188 +/*
189 + * ppp_mppe_mppc.c - MPPC/MPPE "compressor/decompressor" module.
190 + *
191 + * Copyright (c) 1994 Árpád Magosányi <mag@bunuel.tii.matav.hu>
192 + * Copyright (c) 1999 Tim Hockin, Cobalt Networks Inc. <thockin@cobaltnet.com>
193 + * Copyright (c) 2002-2004 Jan Dubiec <jdx@slackware.pl>
194 + *
195 + * Permission to use, copy, modify, and distribute this software and its
196 + * documentation is hereby granted, provided that the above copyright
197 + * notice appears in all copies. This software is provided without any
198 + * warranty, express or implied.
199 + *
200 + * The code is based on MPPE kernel module written by Árpád Magosányi and
201 + * Tim Hockin which can be found on http://planetmirror.com/pub/mppe/.
202 + * I have added MPPC and 56 bit session keys support in MPPE.
203 + *
204 + * WARNING! Although this is open source code, its usage in some countries
205 + * (in particular in the USA) may violate Stac Inc. patent for MPPC.
206 + *
207 + * ==FILEVERSION 20041123==
208 + *
209 + */
210 +
211 +#include <linux/init.h>
212 +#include <linux/module.h>
213 +#include <linux/mm.h>
214 +#include <linux/slab.h>
215 +#include <asm/scatterlist.h>
216 +#include <linux/vmalloc.h>
217 +#include <linux/crypto.h>
218 +
219 +#include <linux/ppp_defs.h>
220 +#include <linux/ppp-comp.h>
221 +
222 +/*
223 + * State for a mppc/mppe "(de)compressor".
224 + */
225 +struct ppp_mppe_state {
226 + struct crypto_tfm *arc4_tfm;
227 + struct crypto_tfm *sha1_tfm;
228 + u8 *sha1_digest;
229 + u8 master_key[MPPE_MAX_KEY_LEN];
230 + u8 session_key[MPPE_MAX_KEY_LEN];
231 + u8 mppc; /* do we use compression (MPPC)? */
232 + u8 mppe; /* do we use encryption (MPPE)? */
233 + u8 keylen; /* key length in bytes */
234 + u8 bitkeylen; /* key length in bits */
235 + u16 ccount; /* coherency counter */
236 + u16 bits; /* MPPC/MPPE control bits */
237 + u8 stateless; /* do we use stateless mode? */
238 + u8 nextflushed; /* set A bit in the next outgoing packet;
239 + used only by compressor*/
240 + u8 flushexpected; /* drop packets until A bit is received;
241 + used only by decompressor*/
242 + u8 *hist; /* MPPC history */
243 + u16 *hash; /* Hash table; used only by compressor */
244 + u16 histptr; /* history "cursor" */
245 + int unit;
246 + int debug;
247 + int mru;
248 + struct compstat stats;
249 +};
250 +
251 +#define MPPE_HIST_LEN 8192 /* MPPC history size */
252 +#define MPPE_MAX_CCOUNT 0x0FFF /* max. coherency counter value */
253 +
254 +#define MPPE_BIT_FLUSHED 0x80 /* bit A */
255 +#define MPPE_BIT_RESET 0x40 /* bit B */
256 +#define MPPE_BIT_COMP 0x20 /* bit C */
257 +#define MPPE_BIT_ENCRYPTED 0x10 /* bit D */
258 +
259 +#define MPPE_SALT0 0xD1 /* values used in MPPE key derivation */
260 +#define MPPE_SALT1 0x26 /* according to RFC3079 */
261 +#define MPPE_SALT2 0x9E
262 +
263 +#define MPPE_CCOUNT(x) ((((x)[4] & 0x0f) << 8) + (x)[5])
264 +#define MPPE_BITS(x) ((x)[4] & 0xf0)
265 +#define MPPE_CTRLHI(x) ((((x)->ccount & 0xf00)>>8)|((x)->bits))
266 +#define MPPE_CTRLLO(x) ((x)->ccount & 0xff)
267 +
268 +/*
269 + * Kernel Crypto API needs its arguments to be in kmalloc'd memory, not in the
270 + * module static data area. That means sha_pad needs to be kmalloc'd. It is done
271 + * in mppe_module_init(). This has been pointed out on 30th July 2004 by Oleg
272 + * Makarenko on pptpclient-devel mailing list.
273 + */
274 +#define SHA1_PAD_SIZE 40
275 +struct sha_pad {
276 + unsigned char sha_pad1[SHA1_PAD_SIZE];
277 + unsigned char sha_pad2[SHA1_PAD_SIZE];
278 +};
279 +static struct sha_pad *sha_pad;
280 +
281 +static inline void
282 +setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
283 +{
284 + sg[0].page = virt_to_page(address);
285 + sg[0].offset = offset_in_page(address);
286 + sg[0].length = length;
287 +}
288 +
289 +static inline void
290 +arc4_setkey(struct ppp_mppe_state *state, const unsigned char *key,
291 + const unsigned int keylen)
292 +{
293 + crypto_cipher_setkey(state->arc4_tfm, key, keylen);
294 +}
295 +
296 +static inline void
297 +arc4_encrypt(struct ppp_mppe_state *state, const unsigned char *in,
298 + const unsigned int len, unsigned char *out)
299 +{
300 + struct scatterlist sgin[4], sgout[4];
301 +
302 + setup_sg(sgin, in, len);
303 + setup_sg(sgout, out, len);
304 + crypto_cipher_encrypt(state->arc4_tfm, sgout, sgin, len);
305 +}
306 +
307 +#define arc4_decrypt arc4_encrypt
308 +
309 +/*
310 + * Key Derivation, from RFC 3078, RFC 3079.
311 + * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
312 + */
313 +static void
314 +get_new_key_from_sha(struct ppp_mppe_state *state, unsigned char *interim_key)
315 +{
316 + struct scatterlist sg[4];
317 +
318 + setup_sg(&sg[0], state->master_key, state->keylen);
319 + setup_sg(&sg[1], sha_pad->sha_pad1, sizeof(sha_pad->sha_pad1));
320 + setup_sg(&sg[2], state->session_key, state->keylen);
321 + setup_sg(&sg[3], sha_pad->sha_pad2, sizeof(sha_pad->sha_pad2));
322 +
323 + crypto_digest_digest (state->sha1_tfm, sg, 4, state->sha1_digest);
324 +
325 + memcpy(interim_key, state->sha1_digest, state->keylen);
326 +}
327 +
328 +static void
329 +mppe_change_key(struct ppp_mppe_state *state, int initialize)
330 +{
331 + unsigned char interim_key[MPPE_MAX_KEY_LEN];
332 +
333 + get_new_key_from_sha(state, interim_key);
334 + if (initialize) {
335 + memcpy(state->session_key, interim_key, state->keylen);
336 + } else {
337 + arc4_setkey(state, interim_key, state->keylen);
338 + arc4_encrypt(state, interim_key, state->keylen, state->session_key);
339 + }
340 + if (state->keylen == 8) {
341 + if (state->bitkeylen == 40) {
342 + state->session_key[0] = MPPE_SALT0;
343 + state->session_key[1] = MPPE_SALT1;
344 + state->session_key[2] = MPPE_SALT2;
345 + } else {
346 + state->session_key[0] = MPPE_SALT0;
347 + }
348 + }
349 + arc4_setkey(state, state->session_key, state->keylen);
350 +}
351 +
352 +/* increase 12-bit coherency counter */
353 +static inline void
354 +mppe_increase_ccount(struct ppp_mppe_state *state)
355 +{
356 + state->ccount = (state->ccount + 1) & MPPE_MAX_CCOUNT;
357 + if (state->mppe) {
358 + if (state->stateless) {
359 + mppe_change_key(state, 0);
360 + state->nextflushed = 1;
361 + } else {
362 + if ((state->ccount & 0xff) == 0xff) {
363 + mppe_change_key(state, 0);
364 + }
365 + }
366 + }
367 +}
368 +
369 +/* allocate space for a MPPE/MPPC (de)compressor. */
370 +/* comp != 0 -> init compressor */
371 +/* comp = 0 -> init decompressor */
372 +static void *
373 +mppe_alloc(unsigned char *options, int opt_len, int comp)
374 +{
375 + struct ppp_mppe_state *state;
376 + unsigned int digestsize;
377 + u8* fname;
378 +
379 + fname = comp ? "mppe_comp_alloc" : "mppe_decomp_alloc";
380 +
381 + /*
382 + * Hack warning - additionally to the standard MPPC/MPPE configuration
383 + * options, pppd passes to the (de)copressor 8 or 16 byte session key.
384 + * Therefore options[1] contains MPPC/MPPE configuration option length
385 + * (CILEN_MPPE = 6), but the real options length, depending on the key
386 + * length, is 6+8 or 6+16.
387 + */
388 + if (opt_len < CILEN_MPPE) {
389 + printk(KERN_WARNING "%s: wrong options length: %u\n", fname, opt_len);
390 + return NULL;
391 + }
392 +
393 + if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
394 + (options[2] & ~MPPE_STATELESS) != 0 ||
395 + options[3] != 0 || options[4] != 0 ||
396 + (options[5] & ~(MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
397 + (options[5] & (MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
398 + printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
399 + "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname, options[0],
400 + options[1], options[2], options[3], options[4], options[5]);
401 + return NULL;
402 + }
403 +
404 + state = (struct ppp_mppe_state *)kmalloc(sizeof(*state), GFP_KERNEL);
405 + if (state == NULL) {
406 + printk(KERN_ERR "%s: cannot allocate space for %scompressor\n", fname,
407 + comp ? "" : "de");
408 + return NULL;
409 + }
410 + memset(state, 0, sizeof(struct ppp_mppe_state));
411 +
412 + state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */
413 + state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
414 + MPPE_40BIT); /* Do we use MPPE? */
415 +
416 + if (state->mppc) {
417 + /* allocate MPPC history */
418 + state->hist = (u8*)vmalloc(2*MPPE_HIST_LEN*sizeof(u8));
419 + if (state->hist == NULL) {
420 + kfree(state);
421 + printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
422 + fname);
423 + return NULL;
424 + }
425 +
426 + /* allocate hashtable for MPPC compressor */
427 + if (comp) {
428 + state->hash = (u16*)vmalloc(MPPE_HIST_LEN*sizeof(u16));
429 + if (state->hash == NULL) {
430 + vfree(state->hist);
431 + kfree(state);
432 + printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
433 + fname);
434 + return NULL;
435 + }
436 + }
437 + }
438 +
439 + if (state->mppe) { /* specific for MPPE */
440 + /* Load ARC4 algorithm */
441 + state->arc4_tfm = crypto_alloc_tfm("arc4", 0);
442 + if (state->arc4_tfm == NULL) {
443 + if (state->mppc) {
444 + vfree(state->hash);
445 + if (comp)
446 + vfree(state->hist);
447 + }
448 + kfree(state);
449 + printk(KERN_ERR "%s: cannot load ARC4 module\n", fname);
450 + return NULL;
451 + }
452 +
453 + /* Load SHA1 algorithm */
454 + state->sha1_tfm = crypto_alloc_tfm("sha1", 0);
455 + if (state->sha1_tfm == NULL) {
456 + crypto_free_tfm(state->arc4_tfm);
457 + if (state->mppc) {
458 + vfree(state->hash);
459 + if (comp)
460 + vfree(state->hist);
461 + }
462 + kfree(state);
463 + printk(KERN_ERR "%s: cannot load SHA1 module\n", fname);
464 + return NULL;
465 + }
466 +
467 + digestsize = crypto_tfm_alg_digestsize(state->sha1_tfm);
468 + if (digestsize < MPPE_MAX_KEY_LEN) {
469 + crypto_free_tfm(state->sha1_tfm);
470 + crypto_free_tfm(state->arc4_tfm);
471 + if (state->mppc) {
472 + vfree(state->hash);
473 + if (comp)
474 + vfree(state->hist);
475 + }
476 + kfree(state);
477 + printk(KERN_ERR "%s: CryptoAPI SHA1 digest size too small\n", fname);
478 + }
479 +
480 + state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
481 + if (!state->sha1_digest) {
482 + crypto_free_tfm(state->sha1_tfm);
483 + crypto_free_tfm(state->arc4_tfm);
484 + if (state->mppc) {
485 + vfree(state->hash);
486 + if (comp)
487 + vfree(state->hist);
488 + }
489 + kfree(state);
490 + printk(KERN_ERR "%s: cannot allocate space for SHA1 digest\n", fname);
491 + }
492 +
493 + memcpy(state->master_key, options+CILEN_MPPE, MPPE_MAX_KEY_LEN);
494 + memcpy(state->session_key, state->master_key, MPPE_MAX_KEY_LEN);
495 + /* initial key generation is done in mppe_init() */
496 + }
497 +
498 + return (void *) state;
499 +}
500 +
501 +static void *
502 +mppe_comp_alloc(unsigned char *options, int opt_len)
503 +{
504 + return mppe_alloc(options, opt_len, 1);
505 +}
506 +
507 +static void *
508 +mppe_decomp_alloc(unsigned char *options, int opt_len)
509 +{
510 + return mppe_alloc(options, opt_len, 0);
511 +}
512 +
513 +/* cleanup the (de)compressor */
514 +static void
515 +mppe_comp_free(void *arg)
516 +{
517 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
518 +
519 + if (state != NULL) {
520 + if (state->mppe) {
521 + if (state->sha1_digest != NULL)
522 + kfree(state->sha1_digest);
523 + if (state->sha1_tfm != NULL)
524 + crypto_free_tfm(state->sha1_tfm);
525 + if (state->arc4_tfm != NULL)
526 + crypto_free_tfm(state->arc4_tfm);
527 + }
528 + if (state->hist != NULL)
529 + vfree(state->hist);
530 + if (state->hash != NULL)
531 + vfree(state->hash);
532 + kfree(state);
533 + }
534 +}
535 +
536 +/* init MPPC/MPPE (de)compresor */
537 +/* comp != 0 -> init compressor */
538 +/* comp = 0 -> init decompressor */
539 +static int
540 +mppe_init(void *arg, unsigned char *options, int opt_len, int unit,
541 + int hdrlen, int mru, int debug, int comp)
542 +{
543 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
544 + u8* fname;
545 +
546 + fname = comp ? "mppe_comp_init" : "mppe_decomp_init";
547 +
548 + if (opt_len < CILEN_MPPE) {
549 + if (debug)
550 + printk(KERN_WARNING "%s: wrong options length: %u\n",
551 + fname, opt_len);
552 + return 0;
553 + }
554 +
555 + if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
556 + (options[2] & ~MPPE_STATELESS) != 0 ||
557 + options[3] != 0 || options[4] != 0 ||
558 + (options[5] & ~(MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
559 + (options[5] & (MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
560 + if (debug)
561 + printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
562 + "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname,
563 + options[0], options[1], options[2], options[3], options[4],
564 + options[5]);
565 + return 0;
566 + }
567 +
568 + if ((options[5] & ~MPPE_MPPC) != MPPE_128BIT &&
569 + (options[5] & ~MPPE_MPPC) != MPPE_56BIT &&
570 + (options[5] & ~MPPE_MPPC) != MPPE_40BIT &&
571 + (options[5] & MPPE_MPPC) != MPPE_MPPC) {
572 + if (debug)
573 + printk(KERN_WARNING "%s: don't know what to do: o[5]=%02x\n",
574 + fname, options[5]);
575 + return 0;
576 + }
577 +
578 + state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */
579 + state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
580 + MPPE_40BIT); /* Do we use MPPE? */
581 + state->stateless = options[2] & MPPE_STATELESS; /* Do we use stateless mode? */
582 +
583 + switch (state->mppe) {
584 + case MPPE_40BIT: /* 40 bit key */
585 + state->keylen = 8;
586 + state->bitkeylen = 40;
587 + break;
588 + case MPPE_56BIT: /* 56 bit key */
589 + state->keylen = 8;
590 + state->bitkeylen = 56;
591 + break;
592 + case MPPE_128BIT: /* 128 bit key */
593 + state->keylen = 16;
594 + state->bitkeylen = 128;
595 + break;
596 + default:
597 + state->keylen = 0;
598 + state->bitkeylen = 0;
599 + }
600 +
601 + state->ccount = MPPE_MAX_CCOUNT;
602 + state->bits = 0;
603 + state->unit = unit;
604 + state->debug = debug;
605 + state->histptr = MPPE_HIST_LEN;
606 + if (state->mppc) { /* reset history if MPPC was negotiated */
607 + memset(state->hist, 0, 2*MPPE_HIST_LEN*sizeof(u8));
608 + }
609 +
610 + if (state->mppe) { /* generate initial session keys */
611 + mppe_change_key(state, 1);
612 + }
613 +
614 + if (comp) { /* specific for compressor */
615 + state->nextflushed = 1;
616 + } else { /* specific for decompressor */
617 + state->mru = mru;
618 + state->flushexpected = 1;
619 + }
620 +
621 + return 1;
622 +}
623 +
624 +static int
625 +mppe_comp_init(void *arg, unsigned char *options, int opt_len, int unit,
626 + int hdrlen, int debug)
627 +{
628 + return mppe_init(arg, options, opt_len, unit, hdrlen, 0, debug, 1);
629 +}
630 +
631 +
632 +static int
633 +mppe_decomp_init(void *arg, unsigned char *options, int opt_len, int unit,
634 + int hdrlen, int mru, int debug)
635 +{
636 + return mppe_init(arg, options, opt_len, unit, hdrlen, mru, debug, 0);
637 +}
638 +
639 +static void
640 +mppe_comp_reset(void *arg)
641 +{
642 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
643 +
644 + if (state->debug)
645 + printk(KERN_DEBUG "%s%d: resetting MPPC/MPPE compressor\n",
646 + __FUNCTION__, state->unit);
647 +
648 + state->nextflushed = 1;
649 + if (state->mppe)
650 + arc4_setkey(state, state->session_key, state->keylen);
651 +}
652 +
653 +static void
654 +mppe_decomp_reset(void *arg)
655 +{
656 + /* When MPPC/MPPE is in use, we shouldn't receive any CCP Reset-Ack.
657 + But when we receive such a packet, we just ignore it. */
658 + return;
659 +}
660 +
661 +static void
662 +mppe_stats(void *arg, struct compstat *stats)
663 +{
664 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
665 +
666 + *stats = state->stats;
667 +}
668 +
669 +/***************************/
670 +/**** Compression stuff ****/
671 +/***************************/
672 +/* inserts 1 to 8 bits into the output buffer */
673 +static inline void putbits8(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
674 +{
675 + buf += *i;
676 + if (*l >= n) {
677 + *l = (*l) - n;
678 + val <<= *l;
679 + *buf = *buf | (val & 0xff);
680 + if (*l == 0) {
681 + *l = 8;
682 + (*i)++;
683 + *(++buf) = 0;
684 + }
685 + } else {
686 + (*i)++;
687 + *l = 8 - n + (*l);
688 + val <<= *l;
689 + *buf = *buf | ((val >> 8) & 0xff);
690 + *(++buf) = val & 0xff;
691 + }
692 +}
693 +
694 +/* inserts 9 to 16 bits into the output buffer */
695 +static inline void putbits16(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
696 +{
697 + buf += *i;
698 + if (*l >= n - 8) {
699 + (*i)++;
700 + *l = 8 - n + (*l);
701 + val <<= *l;
702 + *buf = *buf | ((val >> 8) & 0xff);
703 + *(++buf) = val & 0xff;
704 + if (*l == 0) {
705 + *l = 8;
706 + (*i)++;
707 + *(++buf) = 0;
708 + }
709 + } else {
710 + (*i)++; (*i)++;
711 + *l = 16 - n + (*l);
712 + val <<= *l;
713 + *buf = *buf | ((val >> 16) & 0xff);
714 + *(++buf) = (val >> 8) & 0xff;
715 + *(++buf) = val & 0xff;
716 + }
717 +}
718 +
719 +/* inserts 17 to 24 bits into the output buffer */
720 +static inline void putbits24(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
721 +{
722 + buf += *i;
723 + if (*l >= n - 16) {
724 + (*i)++; (*i)++;
725 + *l = 16 - n + (*l);
726 + val <<= *l;
727 + *buf = *buf | ((val >> 16) & 0xff);
728 + *(++buf) = (val >> 8) & 0xff;
729 + *(++buf) = val & 0xff;
730 + if (*l == 0) {
731 + *l = 8;
732 + (*i)++;
733 + *(++buf) = 0;
734 + }
735 + } else {
736 + (*i)++; (*i)++; (*i)++;
737 + *l = 24 - n + (*l);
738 + val <<= *l;
739 + *buf = *buf | ((val >> 24) & 0xff);
740 + *(++buf) = (val >> 16) & 0xff;
741 + *(++buf) = (val >> 8) & 0xff;
742 + *(++buf) = val & 0xff;
743 + }
744 +}
745 +
746 +static int
747 +mppc_compress(struct ppp_mppe_state *state, unsigned char *ibuf,
748 + unsigned char *obuf, int isize, int osize)
749 +{
750 + u32 olen, off, len, idx, i, l;
751 + u8 *hist, *sbuf, *p, *q, *r, *s;
752 +
753 + /*
754 + At this point, to avoid possible buffer overflow caused by packet
755 + expansion during/after compression, we should make sure that
756 + osize >= (((isize*9)/8)+1)+2, but we don't do that because in
757 + ppp_generic.c we simply allocate bigger obuf.
758 +
759 + Maximum MPPC packet expansion is 12.5%. This is the worst case when
760 + all octets in the input buffer are >= 0x80 and we cannot find any
761 + repeated tokens. Additionally we have to reserve 2 bytes for MPPE/MPPC
762 + status bits and coherency counter.
763 + */
764 +
765 + hist = state->hist + MPPE_HIST_LEN;
766 + /* check if there is enough room at the end of the history */
767 + if (state->histptr + isize >= 2*MPPE_HIST_LEN) {
768 + state->bits |= MPPE_BIT_RESET;
769 + state->histptr = MPPE_HIST_LEN;
770 + memcpy(state->hist, hist, MPPE_HIST_LEN);
771 + }
772 + /* add packet to the history; isize must be <= MPPE_HIST_LEN */
773 + sbuf = state->hist + state->histptr;
774 + memcpy(sbuf, ibuf, isize);
775 + state->histptr += isize;
776 +
777 + /* compress data */
778 + r = sbuf + isize;
779 + *obuf = olen = i = 0;
780 + l = 8;
781 + while (i < isize - 2) {
782 + s = q = sbuf + i;
783 + idx = ((40543*((((s[0]<<4)^s[1])<<4)^s[2]))>>4) & 0x1fff;
784 + p = hist + state->hash[idx];
785 + state->hash[idx] = (u16) (s - hist);
786 + off = s - p;
787 + if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ || *p++ != *s++ ||
788 + *p++ != *s++) {
789 + /* no match found; encode literal byte */
790 + if (ibuf[i] < 0x80) { /* literal byte < 0x80 */
791 + putbits8(obuf, (u32) ibuf[i], 8, &olen, &l);
792 + } else { /* literal byte >= 0x80 */
793 + putbits16(obuf, (u32) (0x100|(ibuf[i]&0x7f)), 9, &olen, &l);
794 + }
795 + ++i;
796 + continue;
797 + }
798 + if (r - q >= 64) {
799 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
800 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
801 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
802 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
803 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
804 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
805 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
806 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
807 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
808 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
809 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
810 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
811 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
812 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
813 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
814 + *p++ != *s++;
815 + if (s - q == 64) {
816 + p--; s--;
817 + while((*p++ == *s++) && (s < r) && (p < q));
818 + }
819 + } else {
820 + while((*p++ == *s++) && (s < r) && (p < q));
821 + }
822 + len = s - q - 1;
823 + i += len;
824 +
825 + /* at least 3 character match found; code data */
826 + /* encode offset */
827 + if (off < 64) { /* 10-bit offset; 0 <= offset < 64 */
828 + putbits16(obuf, 0x3c0|off, 10, &olen, &l);
829 + } else if (off < 320) { /* 12-bit offset; 64 <= offset < 320 */
830 + putbits16(obuf, 0xe00|(off-64), 12, &olen, &l);
831 + } else if (off < 8192) { /* 16-bit offset; 320 <= offset < 8192 */
832 + putbits16(obuf, 0xc000|(off-320), 16, &olen, &l);
833 + } else {
834 + /* This shouldn't happen; we return 0 what means "packet expands",
835 + and we send packet uncompressed. */
836 + if (state->debug)
837 + printk(KERN_DEBUG "%s%d: wrong offset value: %d\n",
838 + __FUNCTION__, state->unit, off);
839 + return 0;
840 + }
841 + /* encode length of match */
842 + if (len < 4) { /* length = 3 */
843 + putbits8(obuf, 0, 1, &olen, &l);
844 + } else if (len < 8) { /* 4 <= length < 8 */
845 + putbits8(obuf, 0x08|(len&0x03), 4, &olen, &l);
846 + } else if (len < 16) { /* 8 <= length < 16 */
847 + putbits8(obuf, 0x30|(len&0x07), 6, &olen, &l);
848 + } else if (len < 32) { /* 16 <= length < 32 */
849 + putbits8(obuf, 0xe0|(len&0x0f), 8, &olen, &l);
850 + } else if (len < 64) { /* 32 <= length < 64 */
851 + putbits16(obuf, 0x3c0|(len&0x1f), 10, &olen, &l);
852 + } else if (len < 128) { /* 64 <= length < 128 */
853 + putbits16(obuf, 0xf80|(len&0x3f), 12, &olen, &l);
854 + } else if (len < 256) { /* 128 <= length < 256 */
855 + putbits16(obuf, 0x3f00|(len&0x7f), 14, &olen, &l);
856 + } else if (len < 512) { /* 256 <= length < 512 */
857 + putbits16(obuf, 0xfe00|(len&0xff), 16, &olen, &l);
858 + } else if (len < 1024) { /* 512 <= length < 1024 */
859 + putbits24(obuf, 0x3fc00|(len&0x1ff), 18, &olen, &l);
860 + } else if (len < 2048) { /* 1024 <= length < 2048 */
861 + putbits24(obuf, 0xff800|(len&0x3ff), 20, &olen, &l);
862 + } else if (len < 4096) { /* 2048 <= length < 4096 */
863 + putbits24(obuf, 0x3ff000|(len&0x7ff), 22, &olen, &l);
864 + } else if (len < 8192) { /* 4096 <= length < 8192 */
865 + putbits24(obuf, 0xffe000|(len&0xfff), 24, &olen, &l);
866 + } else {
867 + /* This shouldn't happen; we return 0 what means "packet expands",
868 + and send packet uncompressed. */
869 + if (state->debug)
870 + printk(KERN_DEBUG "%s%d: wrong length of match value: %d\n",
871 + __FUNCTION__, state->unit, len);
872 + return 0;
873 + }
874 + }
875 +
876 + /* Add remaining octets to the output */
877 + while(isize - i > 0) {
878 + if (ibuf[i] < 0x80) { /* literal byte < 0x80 */
879 + putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l);
880 + } else { /* literal byte >= 0x80 */
881 + putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l);
882 + }
883 + }
884 + /* Reset unused bits of the last output octet */
885 + if ((l != 0) && (l != 8)) {
886 + putbits8(obuf, 0, l, &olen, &l);
887 + }
888 +
889 + return (int) olen;
890 +}
891 +
892 +int
893 +mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
894 + int isize, int osize)
895 +{
896 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
897 + int proto, olen, complen, off;
898 + unsigned char *wptr;
899 +
900 + /* Check that the protocol is in the range we handle. */
901 + proto = PPP_PROTOCOL(ibuf);
902 + if (proto < 0x0021 || proto > 0x00fa)
903 + return 0;
904 +
905 + wptr = obuf;
906 + /* Copy over the PPP header */
907 + wptr[0] = PPP_ADDRESS(ibuf);
908 + wptr[1] = PPP_CONTROL(ibuf);
909 + wptr[2] = PPP_COMP >> 8;
910 + wptr[3] = PPP_COMP;
911 + wptr += PPP_HDRLEN + (MPPE_OVHD / 2); /* Leave two octets for MPPE/MPPC bits */
912 +
913 + /*
914 + * In ver. 0.99 protocol field was compressed. Deflate and BSD compress
915 + * do PFC before actual compression, RCF2118 and RFC3078 are not precise
916 + * on this topic so I decided to do PFC. Unfortunately this change caused
917 + * incompatibility with older/other MPPE/MPPC modules. I have received
918 + * a lot of complaints from unexperienced users so I have decided to revert
919 + * to previous state, i.e. the protocol field is sent uncompressed now.
920 + * Although this may be changed in the future.
921 + *
922 + * Receiving side (mppe_decompress()) still accepts packets with compressed
923 + * and uncompressed protocol field so you shouldn't get "Unsupported protocol
924 + * 0x2145 received" messages anymore.
925 + */
926 + //off = (proto > 0xff) ? 2 : 3; /* PFC - skip first protocol byte if 0 */
927 + off = 2;
928 +
929 + ibuf += off;
930 +
931 + mppe_increase_ccount(state);
932 +
933 + if (state->nextflushed) {
934 + state->bits |= MPPE_BIT_FLUSHED;
935 + state->nextflushed = 0;
936 + if (state->mppe && !state->stateless) {
937 + /*
938 + * If this is the flag packet, the key has been already changed in
939 + * mppe_increase_ccount() so we dont't do it once again.
940 + */
941 + if ((state->ccount & 0xff) != 0xff) {
942 + arc4_setkey(state, state->session_key, state->keylen);
943 + }
944 + }
945 + if (state->mppc) { /* reset history */
946 + state->bits |= MPPE_BIT_RESET;
947 + state->histptr = MPPE_HIST_LEN;
948 + memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
949 + }
950 + }
951 +
952 + if (state->mppc && !state->mppe) { /* Do only compression */
953 + complen = mppc_compress(state, ibuf, wptr, isize - off,
954 + osize - PPP_HDRLEN - (MPPE_OVHD / 2));
955 + /*
956 + * TODO: Implement an heuristics to handle packet expansion in a smart
957 + * way. Now, when a packet expands, we send it as uncompressed and
958 + * when next packet is sent we have to reset compressor's history.
959 + * Maybe it would be better to send such packet as compressed in order
960 + * to keep history's continuity.
961 + */
962 + if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
963 + (complen == 0)) {
964 + /* packet expands */
965 + state->nextflushed = 1;
966 + memcpy(wptr, ibuf, isize - off);
967 + olen = isize - (off - 2) + MPPE_OVHD;
968 + (state->stats).inc_bytes += olen;
969 + (state->stats).inc_packets++;
970 + } else {
971 + state->bits |= MPPE_BIT_COMP;
972 + olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
973 + (state->stats).comp_bytes += olen;
974 + (state->stats).comp_packets++;
975 + }
976 + } else { /* Do encryption with or without compression */
977 + state->bits |= MPPE_BIT_ENCRYPTED;
978 + if (!state->mppc && state->mppe) { /* Do only encryption */
979 + /* read from ibuf, write to wptr, adjust for PPP_HDRLEN */
980 + arc4_encrypt(state, ibuf, isize - off, wptr);
981 + olen = isize - (off - 2) + MPPE_OVHD;
982 + (state->stats).inc_bytes += olen;
983 + (state->stats).inc_packets++;
984 + } else { /* Do compression and then encryption - RFC3078 */
985 + complen = mppc_compress(state, ibuf, wptr, isize - off,
986 + osize - PPP_HDRLEN - (MPPE_OVHD / 2));
987 + /*
988 + * TODO: Implement an heuristics to handle packet expansion in a smart
989 + * way. Now, when a packet expands, we send it as uncompressed and
990 + * when next packet is sent we have to reset compressor's history.
991 + * Maybe it would be good to send such packet as compressed in order
992 + * to keep history's continuity.
993 + */
994 + if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
995 + (complen == 0)) {
996 + /* packet expands */
997 + state->nextflushed = 1;
998 + arc4_encrypt(state, ibuf, isize - off, wptr);
999 + olen = isize - (off - 2) + MPPE_OVHD;
1000 + (state->stats).inc_bytes += olen;
1001 + (state->stats).inc_packets++;
1002 + } else {
1003 + state->bits |= MPPE_BIT_COMP;
1004 + /* Hack warning !!! RC4 implementation which we use does
1005 + encryption "in place" - it means that input and output
1006 + buffers can be *the same* memory area. Therefore we don't
1007 + need to use a temporary buffer. But be careful - other
1008 + implementations don't have to be so nice.
1009 + I used to use ibuf as temporary buffer here, but it led
1010 + packet sniffers into error. Thanks to Wilfried Weissmann
1011 + for pointing that. */
1012 + arc4_encrypt(state, wptr, complen, wptr);
1013 + olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
1014 + (state->stats).comp_bytes += olen;
1015 + (state->stats).comp_packets++;
1016 + }
1017 + }
1018 + }
1019 +
1020 + /* write status bits and coherency counter into the output buffer */
1021 + wptr = obuf + PPP_HDRLEN;
1022 + wptr[0] = MPPE_CTRLHI(state);
1023 + wptr[1] = MPPE_CTRLLO(state);
1024 +
1025 + state->bits = 0;
1026 +
1027 + (state->stats).unc_bytes += isize;
1028 + (state->stats).unc_packets++;
1029 +
1030 + return olen;
1031 +}
1032 +
1033 +/***************************/
1034 +/*** Decompression stuff ***/
1035 +/***************************/
1036 +static inline u32 getbits(const u8 *buf, const u32 n, u32 *i, u32 *l)
1037 +{
1038 + static const u32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1039 + u32 res, ol;
1040 +
1041 + ol = *l;
1042 + if (*l >= n) {
1043 + *l = (*l) - n;
1044 + res = (buf[*i] & m[ol]) >> (*l);
1045 + if (*l == 0) {
1046 + *l = 8;
1047 + (*i)++;
1048 + }
1049 + } else {
1050 + *l = 8 - n + (*l);
1051 + res = (buf[(*i)++] & m[ol]) << 8;
1052 + res = (res | buf[*i]) >> (*l);
1053 + }
1054 +
1055 + return res;
1056 +}
1057 +
1058 +static inline u32 getbyte(const u8 *buf, const u32 i, const u32 l)
1059 +{
1060 + if (l == 8) {
1061 + return buf[i];
1062 + } else {
1063 + return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff;
1064 + }
1065 +}
1066 +
1067 +static inline void lamecopy(u8 *dst, u8 *src, u32 len)
1068 +{
1069 + while (len--)
1070 + *dst++ = *src++;
1071 +}
1072 +
1073 +static int
1074 +mppc_decompress(struct ppp_mppe_state *state, unsigned char *ibuf,
1075 + unsigned char *obuf, int isize, int osize)
1076 +{
1077 + u32 olen, off, len, bits, val, sig, i, l;
1078 + u8 *history, *s;
1079 +
1080 + history = state->hist + state->histptr;
1081 + olen = len = i = 0;
1082 + l = 8;
1083 + bits = isize * 8;
1084 + while (bits >= 8) {
1085 + val = getbyte(ibuf, i++, l);
1086 + if (val < 0x80) { /* literal byte < 0x80 */
1087 + if (state->histptr < 2*MPPE_HIST_LEN) {
1088 + /* copy uncompressed byte to the history */
1089 + (state->hist)[(state->histptr)++] = (u8) val;
1090 + } else {
1091 + /* buffer overflow; drop packet */
1092 + if (state->debug)
1093 + printk(KERN_ERR "%s%d: trying to write outside history "
1094 + "buffer\n", __FUNCTION__, state->unit);
1095 + return DECOMP_ERROR;
1096 + }
1097 + olen++;
1098 + bits -= 8;
1099 + continue;
1100 + }
1101 +
1102 + sig = val & 0xc0;
1103 + if (sig == 0x80) { /* literal byte >= 0x80 */
1104 + if (state->histptr < 2*MPPE_HIST_LEN) {
1105 + /* copy uncompressed byte to the history */
1106 + (state->hist)[(state->histptr)++] =
1107 + (u8) (0x80|((val&0x3f)<<1)|getbits(ibuf, 1 , &i ,&l));
1108 + } else {
1109 + /* buffer overflow; drop packet */
1110 + if (state->debug)
1111 + printk(KERN_ERR "%s%d: trying to write outside history "
1112 + "buffer\n", __FUNCTION__, state->unit);
1113 + return DECOMP_ERROR;
1114 + }
1115 + olen++;
1116 + bits -= 9;
1117 + continue;
1118 + }
1119 +
1120 + /* Not a literal byte so it must be an (offset,length) pair */
1121 + /* decode offset */
1122 + sig = val & 0xf0;
1123 + if (sig == 0xf0) { /* 10-bit offset; 0 <= offset < 64 */
1124 + off = (((val&0x0f)<<2)|getbits(ibuf, 2 , &i ,&l));
1125 + bits -= 10;
1126 + } else {
1127 + if (sig == 0xe0) { /* 12-bit offset; 64 <= offset < 320 */
1128 + off = ((((val&0x0f)<<4)|getbits(ibuf, 4 , &i ,&l))+64);
1129 + bits -= 12;
1130 + } else {
1131 + if ((sig&0xe0) == 0xc0) {/* 16-bit offset; 320 <= offset < 8192 */
1132 + off = ((((val&0x1f)<<8)|getbyte(ibuf, i++, l))+320);
1133 + bits -= 16;
1134 + if (off > MPPE_HIST_LEN - 1) {
1135 + if (state->debug)
1136 + printk(KERN_DEBUG "%s%d: too big offset value: %d\n",
1137 + __FUNCTION__, state->unit, off);
1138 + return DECOMP_ERROR;
1139 + }
1140 + } else { /* this shouldn't happen */
1141 + if (state->debug)
1142 + printk(KERN_DEBUG "%s%d: cannot decode offset value\n",
1143 + __FUNCTION__, state->unit);
1144 + return DECOMP_ERROR;
1145 + }
1146 + }
1147 + }
1148 + /* decode length of match */
1149 + val = getbyte(ibuf, i, l);
1150 + if ((val & 0x80) == 0x00) { /* len = 3 */
1151 + len = 3;
1152 + bits--;
1153 + getbits(ibuf, 1 , &i ,&l);
1154 + } else if ((val & 0xc0) == 0x80) { /* 4 <= len < 8 */
1155 + len = 0x04 | ((val>>4) & 0x03);
1156 + bits -= 4;
1157 + getbits(ibuf, 4 , &i ,&l);
1158 + } else if ((val & 0xe0) == 0xc0) { /* 8 <= len < 16 */
1159 + len = 0x08 | ((val>>2) & 0x07);
1160 + bits -= 6;
1161 + getbits(ibuf, 6 , &i ,&l);
1162 + } else if ((val & 0xf0) == 0xe0) { /* 16 <= len < 32 */
1163 + len = 0x10 | (val & 0x0f);
1164 + bits -= 8;
1165 + i++;
1166 + } else {
1167 + bits -= 8;
1168 + val = (val << 8) | getbyte(ibuf, ++i, l);
1169 + if ((val & 0xf800) == 0xf000) { /* 32 <= len < 64 */
1170 + len = 0x0020 | ((val >> 6) & 0x001f);
1171 + bits -= 2;
1172 + getbits(ibuf, 2 , &i ,&l);
1173 + } else if ((val & 0xfc00) == 0xf800) { /* 64 <= len < 128 */
1174 + len = 0x0040 | ((val >> 4) & 0x003f);
1175 + bits -= 4;
1176 + getbits(ibuf, 4 , &i ,&l);
1177 + } else if ((val & 0xfe00) == 0xfc00) { /* 128 <= len < 256 */
1178 + len = 0x0080 | ((val >> 2) & 0x007f);
1179 + bits -= 6;
1180 + getbits(ibuf, 6 , &i ,&l);
1181 + } else if ((val & 0xff00) == 0xfe00) { /* 256 <= len < 512 */
1182 + len = 0x0100 | (val & 0x00ff);
1183 + bits -= 8;
1184 + i++;
1185 + } else {
1186 + bits -= 8;
1187 + val = (val << 8) | getbyte(ibuf, ++i, l);
1188 + if ((val & 0xff8000) == 0xff0000) { /* 512 <= len < 1024 */
1189 + len = 0x000200 | ((val >> 6) & 0x0001ff);
1190 + bits -= 2;
1191 + getbits(ibuf, 2 , &i ,&l);
1192 + } else if ((val & 0xffc000) == 0xff8000) {/* 1024 <= len < 2048 */
1193 + len = 0x000400 | ((val >> 4) & 0x0003ff);
1194 + bits -= 4;
1195 + getbits(ibuf, 4 , &i ,&l);
1196 + } else if ((val & 0xffe000) == 0xffc000) {/* 2048 <= len < 4096 */
1197 + len = 0x000800 | ((val >> 2) & 0x0007ff);
1198 + bits -= 6;
1199 + getbits(ibuf, 6 , &i ,&l);
1200 + } else if ((val & 0xfff000) == 0xffe000) {/* 4096 <= len < 8192 */
1201 + len = 0x001000 | (val & 0x000fff);
1202 + bits -= 8;
1203 + i++;
1204 + } else { /* this shouldn't happen */
1205 + if (state->debug)
1206 + printk(KERN_DEBUG "%s%d: wrong length code: 0x%X\n",
1207 + __FUNCTION__, state->unit, val);
1208 + return DECOMP_ERROR;
1209 + }
1210 + }
1211 + }
1212 + s = state->hist + state->histptr;
1213 + state->histptr += len;
1214 + olen += len;
1215 + if (state->histptr < 2*MPPE_HIST_LEN) {
1216 + /* copy uncompressed bytes to the history */
1217 +
1218 + /* In some cases len may be greater than off. It means that memory
1219 + * areas pointed by s and s-off overlap. I had used memmove() here
1220 + * because I thought that it acts as libc's version. Unfortunately,
1221 + * I was wrong. :-) I got strange errors sometimes. Wilfried suggested
1222 + * using of byte by byte copying here and strange errors disappeared.
1223 + */
1224 + lamecopy(s, s - off, len);
1225 + } else {
1226 + /* buffer overflow; drop packet */
1227 + if (state->debug)
1228 + printk(KERN_ERR "%s%d: trying to write outside history "
1229 + "buffer\n", __FUNCTION__, state->unit);
1230 + return DECOMP_ERROR;
1231 + }
1232 + }
1233 +
1234 + /* Do PFC decompression */
1235 + len = olen;
1236 + if ((history[0] & 0x01) != 0) {
1237 + obuf[0] = 0;
1238 + obuf++;
1239 + len++;
1240 + }
1241 +
1242 + if (len <= osize) {
1243 + /* copy uncompressed packet to the output buffer */
1244 + memcpy(obuf, history, olen);
1245 + } else {
1246 + /* buffer overflow; drop packet */
1247 + if (state->debug)
1248 + printk(KERN_ERR "%s%d: too big uncompressed packet: %d\n",
1249 + __FUNCTION__, state->unit, len + (PPP_HDRLEN / 2));
1250 + return DECOMP_ERROR;
1251 + }
1252 +
1253 + return (int) len;
1254 +}
1255 +
1256 +int
1257 +mppe_decompress(void *arg, unsigned char *ibuf, int isize,
1258 + unsigned char *obuf, int osize)
1259 +{
1260 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
1261 + int seq, bits, uncomplen;
1262 +
1263 + if (isize <= PPP_HDRLEN + MPPE_OVHD) {
1264 + if (state->debug) {
1265 + printk(KERN_DEBUG "%s%d: short packet (len=%d)\n", __FUNCTION__,
1266 + state->unit, isize);
1267 + }
1268 + return DECOMP_ERROR;
1269 + }
1270 +
1271 + /* Get coherency counter and control bits from input buffer */
1272 + seq = MPPE_CCOUNT(ibuf);
1273 + bits = MPPE_BITS(ibuf);
1274 +
1275 + if (state->stateless) {
1276 + /* RFC 3078, sec 8.1. */
1277 + mppe_increase_ccount(state);
1278 + if ((seq != state->ccount) && state->debug)
1279 + printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1280 + __FUNCTION__, state->unit, seq, state->ccount);
1281 + while (seq != state->ccount)
1282 + mppe_increase_ccount(state);
1283 + } else {
1284 + /* RFC 3078, sec 8.2. */
1285 + if (state->flushexpected) { /* discard state */
1286 + if ((bits & MPPE_BIT_FLUSHED)) { /* we received expected FLUSH bit */
1287 + while (seq != state->ccount)
1288 + mppe_increase_ccount(state);
1289 + state->flushexpected = 0;
1290 + } else /* drop packet*/
1291 + return DECOMP_ERROR;
1292 + } else { /* normal state */
1293 + mppe_increase_ccount(state);
1294 + if (seq != state->ccount) {
1295 + /* Packet loss detected, enter the discard state. */
1296 + if (state->debug)
1297 + printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1298 + __FUNCTION__, state->unit, seq, state->ccount);
1299 + state->flushexpected = 1;
1300 + return DECOMP_ERROR;
1301 + }
1302 + }
1303 + if (state->mppe && (bits & MPPE_BIT_FLUSHED)) {
1304 + arc4_setkey(state, state->session_key, state->keylen);
1305 + }
1306 + }
1307 +
1308 + if (state->mppc && (bits & (MPPE_BIT_FLUSHED | MPPE_BIT_RESET))) {
1309 + state->histptr = MPPE_HIST_LEN;
1310 + if ((bits & MPPE_BIT_FLUSHED)) {
1311 + memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
1312 + } else
1313 + if ((bits & MPPE_BIT_RESET)) {
1314 + memcpy(state->hist, state->hist + MPPE_HIST_LEN, MPPE_HIST_LEN);
1315 + }
1316 + }
1317 +
1318 + /* Fill in the first part of the PPP header. The protocol field
1319 + comes from the decompressed data. */
1320 + obuf[0] = PPP_ADDRESS(ibuf);
1321 + obuf[1] = PPP_CONTROL(ibuf);
1322 + obuf += PPP_HDRLEN / 2;
1323 +
1324 + if (state->mppe) { /* process encrypted packet */
1325 + if ((bits & MPPE_BIT_ENCRYPTED)) {
1326 + /* OK, packet encrypted, so decrypt it */
1327 + if (state->mppc && (bits & MPPE_BIT_COMP)) {
1328 + /* Hack warning !!! RC4 implementation which we use does
1329 + decryption "in place" - it means that input and output
1330 + buffers can be *the same* memory area. Therefore we don't
1331 + need to use a temporary buffer. But be careful - other
1332 + implementations don't have to be so nice. */
1333 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1334 + PPP_HDRLEN - (MPPE_OVHD / 2), ibuf + PPP_HDRLEN +
1335 + (MPPE_OVHD / 2));
1336 + uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1337 + (MPPE_OVHD / 2), obuf, isize -
1338 + PPP_HDRLEN - (MPPE_OVHD / 2),
1339 + osize - (PPP_HDRLEN / 2));
1340 + if (uncomplen == DECOMP_ERROR) {
1341 + state->flushexpected = 1;
1342 + return DECOMP_ERROR;
1343 + }
1344 + uncomplen += PPP_HDRLEN / 2;
1345 + (state->stats).comp_bytes += isize;
1346 + (state->stats).comp_packets++;
1347 + } else {
1348 + uncomplen = isize - MPPE_OVHD;
1349 + /* Decrypt the first byte in order to check if it is
1350 + compressed or uncompressed protocol field */
1351 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), 1, obuf);
1352 + /* Do PFC decompression */
1353 + if ((obuf[0] & 0x01) != 0) {
1354 + obuf[1] = obuf[0];
1355 + obuf[0] = 0;
1356 + obuf++;
1357 + uncomplen++;
1358 + }
1359 + /* And finally, decrypt the rest of the frame. */
1360 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2) + 1,
1361 + isize - PPP_HDRLEN - (MPPE_OVHD / 2) - 1, obuf + 1);
1362 + (state->stats).inc_bytes += isize;
1363 + (state->stats).inc_packets++;
1364 + }
1365 + } else { /* this shouldn't happen */
1366 + if (state->debug)
1367 + printk(KERN_ERR "%s%d: encryption negotiated but not an "
1368 + "encrypted packet received\n", __FUNCTION__, state->unit);
1369 + mppe_change_key(state, 0);
1370 + state->flushexpected = 1;
1371 + return DECOMP_ERROR;
1372 + }
1373 + } else {
1374 + if (state->mppc) { /* no MPPE, only MPPC */
1375 + if ((bits & MPPE_BIT_COMP)) {
1376 + uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1377 + (MPPE_OVHD / 2), obuf, isize -
1378 + PPP_HDRLEN - (MPPE_OVHD / 2),
1379 + osize - (PPP_HDRLEN / 2));
1380 + if (uncomplen == DECOMP_ERROR) {
1381 + state->flushexpected = 1;
1382 + return DECOMP_ERROR;
1383 + }
1384 + uncomplen += PPP_HDRLEN / 2;
1385 + (state->stats).comp_bytes += isize;
1386 + (state->stats).comp_packets++;
1387 + } else {
1388 + memcpy(obuf, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1389 + PPP_HDRLEN - (MPPE_OVHD / 2));
1390 + uncomplen = isize - MPPE_OVHD;
1391 + (state->stats).inc_bytes += isize;
1392 + (state->stats).inc_packets++;
1393 + }
1394 + } else { /* this shouldn't happen */
1395 + if (state->debug)
1396 + printk(KERN_ERR "%s%d: error - not an MPPC or MPPE frame "
1397 + "received\n", __FUNCTION__, state->unit);
1398 + state->flushexpected = 1;
1399 + return DECOMP_ERROR;
1400 + }
1401 + }
1402 +
1403 + (state->stats).unc_bytes += uncomplen;
1404 + (state->stats).unc_packets++;
1405 +
1406 + return uncomplen;
1407 +}
1408 +
1409 +
1410 +/************************************************************
1411 + * Module interface table
1412 + ************************************************************/
1413 +
1414 +/* These are in ppp_generic.c */
1415 +extern int ppp_register_compressor (struct compressor *cp);
1416 +extern void ppp_unregister_compressor (struct compressor *cp);
1417 +
1418 +/*
1419 + * Functions exported to ppp_generic.c.
1420 + *
1421 + * In case of MPPC/MPPE there is no need to process incompressible data
1422 + * because such a data is sent in MPPC/MPPE frame. Therefore the (*incomp)
1423 + * callback function isn't needed.
1424 + */
1425 +struct compressor ppp_mppe = {
1426 + .compress_proto = CI_MPPE,
1427 + .comp_alloc = mppe_comp_alloc,
1428 + .comp_free = mppe_comp_free,
1429 + .comp_init = mppe_comp_init,
1430 + .comp_reset = mppe_comp_reset,
1431 + .compress = mppe_compress,
1432 + .comp_stat = mppe_stats,
1433 + .decomp_alloc = mppe_decomp_alloc,
1434 + .decomp_free = mppe_comp_free,
1435 + .decomp_init = mppe_decomp_init,
1436 + .decomp_reset = mppe_decomp_reset,
1437 + .decompress = mppe_decompress,
1438 + .incomp = NULL,
1439 + .decomp_stat = mppe_stats,
1440 + .owner = THIS_MODULE
1441 +};
1442 +
1443 +/************************************************************
1444 + * Module support routines
1445 + ************************************************************/
1446 +
1447 +int __init mppe_module_init(void)
1448 +{
1449 + int answer;
1450 +
1451 + if (!(crypto_alg_available("arc4", 0) && crypto_alg_available("sha1", 0))) {
1452 + printk(KERN_ERR "Kernel doesn't provide ARC4 and/or SHA1 algorithms "
1453 + "required by MPPE/MPPC. Check CryptoAPI configuration.\n");
1454 + return -ENODEV;
1455 + }
1456 +
1457 + /* Allocate space for SHAPad1, SHAPad2 and ... */
1458 + sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
1459 + if (sha_pad == NULL)
1460 + return -ENOMEM;
1461 + /* ... initialize them */
1462 + memset(sha_pad->sha_pad1, 0x00, sizeof(sha_pad->sha_pad1));
1463 + memset(sha_pad->sha_pad2, 0xf2, sizeof(sha_pad->sha_pad2));
1464 +
1465 + answer = ppp_register_compressor(&ppp_mppe);
1466 + if (answer == 0) {
1467 + printk(KERN_INFO "MPPE/MPPC encryption/compression module registered\n");
1468 + }
1469 + return answer;
1470 +}
1471 +
1472 +void __exit mppe_module_cleanup(void)
1473 +{
1474 + kfree(sha_pad);
1475 + ppp_unregister_compressor(&ppp_mppe);
1476 + printk(KERN_INFO "MPPE/MPPC encryption/compression module unregistered\n");
1477 +}
1478 +
1479 +module_init(mppe_module_init);
1480 +module_exit(mppe_module_cleanup);
1481 +
1482 +MODULE_AUTHOR("Jan Dubiec <jdx@slackware.pl>");
1483 +MODULE_DESCRIPTION("MPPE/MPPC encryption/compression module for Linux");
1484 +MODULE_VERSION("1.2");
1485 +MODULE_LICENSE("Dual BSD/GPL");
1486 +MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
1487 diff -ruN linux-2.6.12.orig/include/linux/ppp-comp.h linux-2.6.12/include/linux/ppp-comp.h
1488 --- linux-2.6.12.orig/include/linux/ppp-comp.h 2004-12-24 22:33:47.000000000 +0100
1489 +++ linux-2.6.12/include/linux/ppp-comp.h 2005-06-28 20:07:01.000000000 +0200
1490 @@ -28,7 +28,7 @@
1491 */
1492
1493 /*
1494 - * ==FILEVERSION 980319==
1495 + * ==FILEVERSION 20040509==
1496 *
1497 * NOTE TO MAINTAINERS:
1498 * If you modify this file at all, please set the above date.
1499 @@ -80,7 +80,7 @@
1500
1501 /* Compress a packet */
1502 int (*compress) (void *state, unsigned char *rptr,
1503 - unsigned char *obuf, int isize, int osize);
1504 + unsigned char *obuf, int isize, int osize);
1505
1506 /* Return compression statistics */
1507 void (*comp_stat) (void *state, struct compstat *stats);
1508 @@ -101,7 +101,7 @@
1509
1510 /* Decompress a packet. */
1511 int (*decompress) (void *state, unsigned char *ibuf, int isize,
1512 - unsigned char *obuf, int osize);
1513 + unsigned char *obuf, int osize);
1514
1515 /* Update state for an incompressible packet received */
1516 void (*incomp) (void *state, unsigned char *ibuf, int icnt);
1517 @@ -191,6 +191,42 @@
1518 #define DEFLATE_CHK_SEQUENCE 0
1519
1520 /*
1521 + * Definitions for MPPE/MPPC.
1522 + */
1523 +
1524 +#define CI_MPPE 18 /* config option for MPPE */
1525 +#define CILEN_MPPE 6 /* length of config option */
1526 +
1527 +#define MPPE_OVHD 4 /* MPPE overhead */
1528 +#define MPPE_MAX_KEY_LEN 16 /* largest key length (128-bit) */
1529 +
1530 +#define MPPE_STATELESS 0x01 /* configuration bit H */
1531 +#define MPPE_40BIT 0x20 /* configuration bit L */
1532 +#define MPPE_56BIT 0x80 /* configuration bit M */
1533 +#define MPPE_128BIT 0x40 /* configuration bit S */
1534 +#define MPPE_MPPC 0x01 /* configuration bit C */
1535 +
1536 +/*
1537 + * Definitions for Stac LZS.
1538 + */
1539 +
1540 +#define CI_LZS 17 /* config option for Stac LZS */
1541 +#define CILEN_LZS 5 /* length of config option */
1542 +
1543 +#define LZS_OVHD 4 /* max. LZS overhead */
1544 +#define LZS_HIST_LEN 2048 /* LZS history size */
1545 +#define LZS_MAX_CCOUNT 0x0FFF /* max. coherency counter value */
1546 +
1547 +#define LZS_MODE_NONE 0
1548 +#define LZS_MODE_LCB 1
1549 +#define LZS_MODE_CRC 2
1550 +#define LZS_MODE_SEQ 3
1551 +#define LZS_MODE_EXT 4
1552 +
1553 +#define LZS_EXT_BIT_FLUSHED 0x80 /* bit A */
1554 +#define LZS_EXT_BIT_COMP 0x20 /* bit C */
1555 +
1556 +/*
1557 * Definitions for other, as yet unsupported, compression methods.
1558 */
1559
This page took 0.107891 seconds and 5 git commands to generate.