summaryrefslogtreecommitdiff
path: root/net/mppe-lkm/patches/patch-aa
blob: 8d0b9a907cd5c59c3ab187adc5af9f097e682c11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
$NetBSD: patch-aa,v 1.3 2005/04/29 11:41:29 cube Exp $

--- ppp_mppe_compress.c.orig	2004-05-31 07:31:51.000000000 +0200
+++ ppp_mppe_compress.c
@@ -5,6 +5,8 @@
  * By Frank Cusack <frank@google.com>.
  * Copyright (c) 2002,2003,2004 Google, Inc.
  * All rights reserved.
+ * Copyright (c) 1999 Darrin B. Jewell <dbj@NetBSD.org>
+ * Copyright (c) 2004, 2005 Quentin Garnier <cube@NetBSD.org>
  *
  * Permission to use, copy, modify, and distribute this software and its
  * documentation is hereby granted, provided that the above copyright
@@ -18,19 +20,35 @@
  *                    deprecated in 2.6
  */
 
-#include <linux/module.h>
-#include <linux/kernel.h>
-#include <linux/version.h>
-#include <linux/init.h>
-#include <linux/types.h>
-#include <linux/slab.h>
-#include <linux/string.h>
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/systm.h>
+#include <sys/malloc.h>
+#include <sys/mbuf.h>
+
+#define PACKETPTR	struct mbuf *
+
+#include <net/ppp_defs.h>
+#include <net/ppp-comp.h>
+
+#include <sys/sha1.h>
+#include <crypto/arc4/arc4.h>
+
+#if __NetBSD_Version__ < 299001600
+/* MPPE definitions were included into the kernel source */
+#include "ppp-comp-local.h"
+#endif
 
-#include <linux/ppp_defs.h>
-#include <linux/ppp-comp.h>
+int mppe_in_use;
+#define MOD_INC_USE_COUNT	mppe_in_use++
+#define MOD_DEC_USE_COUNT	mppe_in_use--
 
-#include "arcfour.h"
-#include "sha1.h"
+#ifdef MPPE_DEBUG
+#define DPRINTF(x)	aprint_normal x
+#else
+#define DPRINTF(x)
+#endif
 
 /*
  * State for an MPPE (de)compressor.
@@ -38,8 +56,8 @@
 typedef struct ppp_mppe_state {
     unsigned char	master_key[MPPE_MAX_KEY_LEN];
     unsigned char	session_key[MPPE_MAX_KEY_LEN];
-    arcfour_context	arcfour_context; /* encryption state */
-    unsigned		keylen;		/* key length in bytes             */
+    void		*arcfour_context; /* encryption state */
+    unsigned 		keylen;		/* key length in bytes             */
 					/* NB: 128-bit == 16, 40-bit == 8! */
 					/* If we want to support 56-bit,   */
 					/* the unit has to change to bits  */
@@ -84,12 +102,12 @@ static int	mppe_comp_init __P((void *sta
 static int	mppe_decomp_init __P((void *state, unsigned char *options,
 				      int optlen, int unit,
 				      int hdrlen, int mru, int debug));
-static int	mppe_compress __P((void *state, unsigned char *ibuf,
-				   unsigned char *obuf,
+static int	mppe_compress __P((void *state, struct mbuf **opkt,
+				   struct mbuf *ipkt,
 				   int isize, int osize));
-static void	mppe_incomp __P((void *state, unsigned char *ibuf, int icnt));
-static int	mppe_decompress __P((void *state, unsigned char *ibuf,
-				     int isize, unsigned char *obuf,int osize));
+static void	mppe_incomp __P((void *state, struct mbuf *mp));
+static int	mppe_decompress __P((void *state, struct mbuf *ipkt,
+				     struct mbuf **opkt));
 static void	mppe_comp_reset __P((void *state));
 static void	mppe_decomp_reset __P((void *state));
 static void	mppe_comp_stats __P((void *state, struct compstat *stats));
@@ -104,7 +122,7 @@ GetNewKeyFromSHA(unsigned char *MasterKe
 		 unsigned SessionKeyLength, unsigned char *InterimKey)
 {
     SHA1_CTX Context;
-    unsigned char Digest[SHA1_SIGNATURE_SIZE];
+    unsigned char Digest[20];
 
     unsigned char SHApad1[40] =
     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -119,12 +137,12 @@ GetNewKeyFromSHA(unsigned char *MasterKe
 
     /* assert(SessionKeyLength <= SHA1_SIGNATURE_SIZE); */
 
-    SHA1_Init(&Context);
-    SHA1_Update(&Context, MasterKey, SessionKeyLength);
-    SHA1_Update(&Context, SHApad1, sizeof(SHApad1));
-    SHA1_Update(&Context, SessionKey, SessionKeyLength);
-    SHA1_Update(&Context, SHApad2, sizeof(SHApad2));
-    SHA1_Final(Digest, &Context);
+    SHA1Init(&Context);
+    SHA1Update(&Context, MasterKey, SessionKeyLength);
+    SHA1Update(&Context, SHApad1, sizeof(SHApad1));
+    SHA1Update(&Context, SessionKey, SessionKeyLength);
+    SHA1Update(&Context, SHApad2, sizeof(SHApad2));
+    SHA1Final(Digest, &Context);
 
     memcpy(InterimKey, Digest, SessionKeyLength);
 }
@@ -141,9 +159,9 @@ mppe_rekey(ppp_mppe_state *state, int in
     GetNewKeyFromSHA(state->master_key, state->session_key,
 		     state->keylen, InterimKey);
     if (!initial_key) {
-	arcfour_setkey(&state->arcfour_context, InterimKey, state->keylen);
-	arcfour_encrypt(&state->arcfour_context, InterimKey, state->keylen,
-			state->session_key);
+	arc4_setkey(state->arcfour_context, InterimKey, state->keylen);
+	arc4_encrypt(state->arcfour_context, state->session_key, InterimKey,
+			state->keylen);
     } else {
 	memcpy(state->session_key, InterimKey, state->keylen);
     }
@@ -153,7 +171,7 @@ mppe_rekey(ppp_mppe_state *state, int in
 	state->session_key[1] = 0x26;
 	state->session_key[2] = 0x9e;
     }
-    arcfour_setkey(&state->arcfour_context, state->session_key, state->keylen);
+    arc4_setkey(state->arcfour_context, state->session_key, state->keylen);
 }
 
 
@@ -170,15 +188,12 @@ mppe_alloc(unsigned char *options, int o
 	|| options[1] != CILEN_MPPE)
 	return NULL;
 
-    state = (ppp_mppe_state *) kmalloc(sizeof(*state), GFP_KERNEL);
+    state = (ppp_mppe_state *) malloc(sizeof(*state), M_DEVBUF, M_NOWAIT);
     if (state == NULL)
 	return NULL;
+    state->arcfour_context = NULL;
 
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
-    try_module_get(THIS_MODULE);
-#else
     MOD_INC_USE_COUNT;
-#endif
     memset(state, 0, sizeof(*state));
 
     /* Save keys. */
@@ -201,12 +216,10 @@ mppe_free(void *arg)
     ppp_mppe_state *state = (ppp_mppe_state *) arg;
 
     if (state) {
-	kfree(state);
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
-	module_put(THIS_MODULE);
-#else
+	if (state->arcfour_context)
+	    free(state->arcfour_context, M_DEVBUF);
+	free(state, M_DEVBUF);
 	MOD_DEC_USE_COUNT;
-#endif
     }
 }
 
@@ -232,12 +245,16 @@ mppe_init(void *arg, unsigned char *opti
     else if (mppe_opts & MPPE_OPT_40)
 	state->keylen = 8;
     else {
-	printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr, unit);
+	aprint_error("%s[%d]: unknown key length\n", debugstr, unit);
 	return 0;
     }
     if (mppe_opts & MPPE_OPT_STATEFUL)
 	state->stateful = 1;
 
+    state->arcfour_context = malloc(arc4_ctxlen(), M_DEVBUF, M_NOWAIT);
+    if (state->arcfour_context == NULL)
+	return 0;
+
     /* Generate the initial session key. */
     mppe_rekey(state, 1);
 
@@ -246,7 +263,7 @@ mppe_init(void *arg, unsigned char *opti
 	char mkey[sizeof(state->master_key) * 2 + 1];
 	char skey[sizeof(state->session_key) * 2 + 1];
 
-	printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n", debugstr,
+	aprint_normal("%s[%d]: initialized with %d-bit %s mode\n", debugstr,
 	       unit, (state->keylen == 16)? 128: 40,
 	       (state->stateful)? "stateful": "stateless");
 
@@ -254,7 +271,7 @@ mppe_init(void *arg, unsigned char *opti
 	    sprintf(mkey + i * 2, "%.2x", state->master_key[i]);
 	for (i = 0; i < sizeof(state->session_key); i++)
 	    sprintf(skey + i * 2, "%.2x", state->session_key[i]);
-	printk(KERN_DEBUG "%s[%d]: keys: master: %s initial session: %s\n",
+	aprint_normal("%s[%d]: keys: master: %s initial session: %s\n",
 	       debugstr, unit, mkey, skey);
     }
 
@@ -311,30 +328,75 @@ mppe_comp_reset(void *arg)
  * MPPE_OVHD + 2 bytes larger than the input.
  */
 int
-mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
+mppe_compress(void *arg, struct mbuf **mret, struct mbuf *mp,
 	      int isize, int osize)
 {
     ppp_mppe_state *state = (ppp_mppe_state *) arg;
     int proto;
+    unsigned char *ibuf, *obuf;
 
+    *mret = NULL;
+    ibuf = mtod(mp, unsigned char *);
     /*
      * Check that the protocol is in the range we handle.
      */
     proto = PPP_PROTOCOL(ibuf);
-    if (proto < 0x0021 || proto > 0x00fa)
+    if (proto < 0x0021 || proto > 0x00fa) {
+	DPRINTF(("unhandled proto %d\n", proto));
 	return 0;
+    }
 
     /* Make sure we have enough room to generate an encrypted packet. */
-    if (osize < isize + MPPE_OVHD + 2) {
+    /* XXX */
+    if (osize + MPPE_OVHD < isize + MPPE_OVHD) {
 	/* Drop the packet if we should encrypt it, but can't. */
-	printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
+	aprint_normal("mppe_compress[%d]: osize too small! "
 	       "(have: %d need: %d)\n", state->unit,
-	       osize, osize + MPPE_OVHD + 2);
-	return -1;
+	       osize, isize + MPPE_OVHD + 2);
+	/* XXX */
+	return 0;
     }
 
     osize = isize + MPPE_OVHD + 2;
 
+    /* Allocate an mbuf chain to hold the encrypted packet */
+    {
+      struct mbuf *mfirst = NULL;
+      struct mbuf *mprev;
+      struct mbuf *m = NULL;
+      int bleft = isize+MPPE_OVHD+2;
+      do {
+	mprev = m;
+	MGET(m,M_DONTWAIT, MT_DATA);
+	if (m == NULL) {
+	  m_freem(mfirst);
+	  /* XXX: what should we do here?  If we return NULL, the data
+	   * will go out unencrypted. We can't use M_WAITOK, since this
+	   * will be called from splsoftnet()
+	   */
+	  panic("ppp%d/mppe: unable to allocate mbuf to encrypt packet",
+		state->unit);
+	}
+	m->m_len = 0;
+	if (mfirst == NULL) {
+	  mfirst = m;
+	  M_COPY_PKTHDR(m,mp);
+	  if (bleft > MHLEN) {
+	    MCLGET(m, M_DONTWAIT);
+	  }
+	} else {
+	  mprev->m_next = m;
+	  if (bleft > MLEN) {
+	    MCLGET(m, M_DONTWAIT);
+	  }
+	}
+	bleft -= M_TRAILINGSPACE(m);
+      } while (bleft > 0);
+      *mret = mfirst;
+    }
+
+    obuf = mtod(*mret, unsigned char *);
+
     /*
      * Copy over the PPP header and set control bits.
      */
@@ -346,7 +408,7 @@ mppe_compress(void *arg, unsigned char *
 
     state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
     if (state->debug >= 7)
-	printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
+	aprint_verbose("mppe_compress[%d]: ccount %d\n", state->unit,
 	       state->ccount);
     obuf[0] = state->ccount >> 8;
     obuf[1] = state->ccount & 0xff;
@@ -356,19 +418,62 @@ mppe_compress(void *arg, unsigned char *
 	(state->bits & MPPE_BIT_FLUSHED)) {	/* CCP Reset-Request  */
 	/* We must rekey */
 	if (state->debug && state->stateful)
-	    printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n", state->unit);
+	    aprint_verbose("mppe_compress[%d]: rekeying\n", state->unit);
 	mppe_rekey(state, 0);
 	state->bits |= MPPE_BIT_FLUSHED;
     }
     obuf[0] |= state->bits;
     state->bits &= ~MPPE_BIT_FLUSHED;	/* reset for next xmit */
 
-    obuf  += MPPE_OVHD;
     ibuf  += 2;	/* skip to proto field */
     isize -= 2;
+    (*mret)->m_len += PPP_HDRLEN + MPPE_OVHD;
 
-    /* Encrypt packet */
-    arcfour_encrypt(&state->arcfour_context, ibuf, isize, obuf);
+    /* March down input and output mbuf chains, encoding with RC4 */
+    {
+      struct mbuf *mi = mp;	/* mbuf in */
+      struct mbuf *mo = *mret;	/* mbuf out */
+      int maxi, maxo;
+      maxi = mi->m_len-2;
+      maxo = M_TRAILINGSPACE(mo);
+      while (mi) {
+	if (maxi < maxo) {
+	  arc4_encrypt(state->arcfour_context,
+	      mtod(mo,unsigned char *)+mo->m_len,
+	      mtod(mi,unsigned char *)+mi->m_len-maxi,
+	      maxi);
+	  mo->m_len += maxi;
+	  maxo -= maxi;
+	  mi = mi->m_next;
+	  if (mi) {
+	    maxi = mi->m_len;
+	  }
+	} else if (maxi > maxo) {
+	  arc4_encrypt(state->arcfour_context,
+	      mtod(mo,unsigned char *)+mo->m_len,
+	      mtod(mi,unsigned char *)+mi->m_len-maxi,
+	      maxo);
+	  mo->m_len += maxo;
+	  maxi -= maxo;
+	  mo = mo->m_next;
+	  if (mo) {
+	    maxo = M_TRAILINGSPACE(mo);
+	  }
+	} else {
+	  arc4_encrypt(state->arcfour_context,
+	      mtod(mo,unsigned char *)+mo->m_len,
+	      mtod(mi,unsigned char *)+mi->m_len-maxi,
+	      maxi);
+	  mo->m_len += maxi;
+	  mi = mi->m_next;
+	  mo = mo->m_next;
+	  if (mi) {
+	    maxi = mi->m_len;
+	    maxo = M_TRAILINGSPACE(mo);
+	  }
+	}
+      }
+    }
 
     state->stats.unc_bytes += isize;
     state->stats.unc_packets++;
@@ -413,56 +518,50 @@ mppe_decomp_reset(void *arg)
  * Decompress (decrypt) an MPPE packet.
  */
 int
-mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
-		int osize)
+mppe_decompress(void *arg, struct mbuf *mp, struct mbuf **mret)
 {
     ppp_mppe_state *state = (ppp_mppe_state *) arg;
     unsigned ccount;
-    int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
-    int sanity = 0;
+    int flushed;
+    int sanity = 0, isize;
+    unsigned char *ibuf, *obuf;
 
-    if (isize <= PPP_HDRLEN + MPPE_OVHD) {
-	if (state->debug)
-	    printk(KERN_DEBUG "mppe_decompress[%d]: short pkt (%d)\n",
-		   state->unit, isize);
+    if (!mp) {
+	DPRINTF(("ppp%d/mppe: null input packet\n",state->unit));
 	return DECOMP_ERROR;
     }
 
-    /*
-     * Make sure we have enough room to decrypt the packet.
-     * Note that for our test we only subtract 1 byte whereas in
-     * mppe_compress() we added 2 bytes (+MPPE_OVHD);
-     * this is to account for possible PFC.
-     */
-    if (osize < isize - MPPE_OVHD - 1) {
-	printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
-	       "(have: %d need: %d)\n", state->unit,
-	       osize, isize - MPPE_OVHD - 1);
+    if (mp->m_len <= PPP_HDRLEN + MPPE_OVHD) {
+	if (state->debug)
+	    aprint_error("mppe_decompress[%d]: short pkt (%d)\n",
+		   state->unit, mp->m_len);
 	return DECOMP_ERROR;
     }
-    osize = isize - MPPE_OVHD - 2;	/* assume no PFC */
+
+    ibuf = mtod(mp,unsigned char *);
+    flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
 
     ccount = MPPE_CCOUNT(ibuf);
     if (state->debug >= 7)
-	printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n", state->unit,
+	aprint_error("mppe_decompress[%d]: ccount %d\n", state->unit,
 	       ccount);
 
     /* sanity checks -- terminate with extreme prejudice */
     if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
-	printk(KERN_DEBUG "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
-	       state->unit);
+	DPRINTF(("mppe_decompress[%d]: ENCRYPTED bit not set!\n",
+	       state->unit));
 	state->sanity_errors += 100;
 	sanity = 1;
     }
     if (!state->stateful && !flushed) {
-	printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
-	       "stateless mode!\n", state->unit);
+	DPRINTF(("mppe_decompress[%d]: FLUSHED bit not set in "
+	       "stateless mode!\n", state->unit));
 	state->sanity_errors += 100;
 	sanity = 1;
     }
     if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
-	printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
-	       "flag packet!\n", state->unit);
+	DPRINTF(("mppe_decompress[%d]: FLUSHED bit not set on "
+	       "flag packet!\n", state->unit));
 	state->sanity_errors += 100;
 	sanity = 1;
     }
@@ -531,6 +630,46 @@ mppe_decompress(void *arg, unsigned char
 	    mppe_rekey(state, 0);
     }
 
+    /* Allocate an mbuf chain to hold the decrypted packet */
+    {
+	struct mbuf *mfirst = 0;
+	struct mbuf *mprev;
+	struct mbuf *m = 0;
+	int bleft;
+	isize = 0;
+	for (m=mp; m; m= m->m_next) isize += m->m_len;
+	bleft = isize-MPPE_OVHD;
+	do {
+	    mprev = m;
+	    MGET(m,M_DONTWAIT, MT_DATA);
+	    if (m == NULL) {
+		m_freem(mfirst);
+#ifdef DEBUG
+		aprint_error("ppp%d/mppe: unable to allocate mbuf to decrypt packet\n",
+		    state->unit);
+#endif
+		return DECOMP_ERROR;
+	    }
+	    m->m_len = 0;
+	    if (mfirst == NULL) {
+		mfirst=m;
+		M_COPY_PKTHDR(m,mp);
+		if (bleft > MHLEN) {
+		    MCLGET(m, M_DONTWAIT);
+		}
+	    } else {
+		mprev->m_next = m;
+		if (bleft > MLEN) {
+		    MCLGET(m, M_DONTWAIT);
+		}
+	    }
+	    bleft -= M_TRAILINGSPACE(m);
+	} while (bleft > 0);
+	*mret = mfirst;
+    }
+
+    obuf = mtod(*mret, unsigned char *);
+
     /*
      * Fill in the first part of the PPP header.  The protocol field
      * comes from the decrypted data.
@@ -538,15 +677,17 @@ mppe_decompress(void *arg, unsigned char
     obuf[0] = PPP_ADDRESS(ibuf);	/* +1 */
     obuf[1] = PPP_CONTROL(ibuf);	/* +1 */
     obuf  += 2;
+    (*mret)->m_len += 2;
     ibuf  += PPP_HDRLEN + MPPE_OVHD;
     isize -= PPP_HDRLEN + MPPE_OVHD;	/* -6 */
 					/* net osize: isize-4 */
 
+#ifdef notyet
     /*
      * Decrypt the first byte in order to check if it is
      * a compressed or uncompressed protocol field.
      */
-    arcfour_decrypt(&state->arcfour_context, ibuf, 1, obuf);
+    arc4_decrypt(state->arcfour_context, obuf, ibuf, 1);
 
     /*
      * Do PFC decompression.
@@ -557,13 +698,56 @@ mppe_decompress(void *arg, unsigned char
 	obuf[1] = obuf[0];
 	obuf[0] = 0;
 	obuf++;
-	osize++;
     }
+#endif
 
     /* And finally, decrypt the rest of the packet. */
-    arcfour_decrypt(&state->arcfour_context, ibuf + 1, isize - 1, obuf + 1);
-
-    state->stats.unc_bytes += osize;
+	/* March down input and output mbuf chains, decoding with RC4 */
+	{
+	    struct mbuf *mi = mp;	/* mbuf in */
+	    struct mbuf *mo = *mret;	/* mbuf out */
+	    int maxi, maxo;
+	    maxi = mi->m_len-6;	/* adjust for PPP_HDRLEN and MPPE_OVERHEAD */
+	    maxo = M_TRAILINGSPACE(mo);
+	    while (mi) {
+		if (maxi < maxo) {
+		    arc4_encrypt(state->arcfour_context,
+			mtod(mo,unsigned char *)+mo->m_len,
+			mtod(mi,unsigned char *)+mi->m_len-maxi,
+			maxi);
+		    mo->m_len += maxi;
+		    maxo -= maxi;
+		    mi = mi->m_next;
+		    if (mi) {
+			maxi = mi->m_len;
+		    }
+		} else if (maxi > maxo) {
+		    arc4_encrypt(state->arcfour_context,
+			mtod(mo,unsigned char *)+mo->m_len,
+			mtod(mi,unsigned char *)+mi->m_len-maxi,
+			maxo);
+		    mo->m_len += maxo;
+		    maxi -= maxo;
+		    mo = mo->m_next;
+		    if (mo) {
+			maxo = M_TRAILINGSPACE(mo);
+		    }
+		} else {
+		    arc4_encrypt(state->arcfour_context,
+			mtod(mo,unsigned char *)+mo->m_len,
+			mtod(mi,unsigned char *)+mi->m_len-maxi,
+			maxi);
+		    mo->m_len += maxi;
+		    mi = mi->m_next;
+		    mo = mo->m_next;
+		    if (mi) {
+			maxi = mi->m_len;
+			maxo = M_TRAILINGSPACE(mo);
+		    }
+		}
+	    }
+	}
+    state->stats.unc_bytes += (*mret)->m_len;
     state->stats.unc_packets++;
     state->stats.comp_bytes += isize;
     state->stats.comp_packets++;
@@ -571,7 +755,7 @@ mppe_decompress(void *arg, unsigned char
     /* good packet credit */
     state->sanity_errors >>= 1;
 
-    return osize;
+    return DECOMP_OK;
 }
 
 /*
@@ -581,29 +765,23 @@ mppe_decompress(void *arg, unsigned char
  * packet.  (How to do this?)
  */
 static void
-mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
+mppe_incomp(void *arg, struct mbuf *mp)
 {
     ppp_mppe_state *state = (ppp_mppe_state *) arg;
+    struct mbuf *m;
 
-    if (state->debug &&
-	(PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
-	printk(KERN_DEBUG "mppe_incomp[%d]: incompressible (unencrypted) data! "
-	       "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
-
-    state->stats.inc_bytes += icnt;
-    state->stats.inc_packets++;
-    state->stats.unc_bytes += icnt;
-    state->stats.unc_packets++;
+    for (m=mp;m;m = m->m_next) {
+      (state->stats).inc_bytes += m->m_len;
+      (state->stats).unc_bytes += m->m_len;
+    }
+    (state->stats).inc_packets++;
+    (state->stats).unc_packets++;
 }
 
 /*************************************************************
  * Module interface table
  *************************************************************/
 
-/* These are in ppp.c (2.2.x) or ppp_generic.c (2.4.x) */
-extern int  ppp_register_compressor   (struct compressor *cp);
-extern void ppp_unregister_compressor (struct compressor *cp);
-
 /*
  * Procedures exported to if_ppp.c.
  */
@@ -623,34 +801,3 @@ struct compressor ppp_mppe = {
     mppe_incomp,	/* incomp */
     mppe_comp_stats,	/* decomp_stat */
 };
-
-/* 2.2 compatibility defines */
-#ifndef __init
-#define __init
-#endif
-#ifndef __exit
-#define __exit
-#endif
-#ifndef MODULE_LICENSE
-#define MODULE_LICENSE(license)
-#endif
-
-int __init
-ppp_mppe_init(void)
-{  
-    int answer = ppp_register_compressor(&ppp_mppe);
-
-    if (answer == 0)
-	printk(KERN_INFO "PPP MPPE Compression module registered\n");
-    return answer;
-}
-
-void __exit
-ppp_mppe_cleanup(void)
-{
-    ppp_unregister_compressor(&ppp_mppe);
-}
-
-module_init(ppp_mppe_init);
-module_exit(ppp_mppe_cleanup);
-MODULE_LICENSE("BSD without advertisement clause");