summaryrefslogtreecommitdiff
path: root/recipes/linux/linux-wrt-2.4.20/150-mppe-mppc-0.98.patch
blob: 4a240df3349d1491873ac35fd3a1ef9e1232245c (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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825

#
# Patch managed by http://www.mn-logistik.de/unsupported/pxa250/patcher
#

--- linux-2.4.20/drivers/net/Config.in~150-mppe-mppc-0.98	2005-01-07 02:52:47.191946000 -0500
+++ linux-2.4.20/drivers/net/Config.in	2005-01-07 02:57:48.408155016 -0500
@@ -288,6 +288,7 @@
    dep_tristate '  PPP support for sync tty ports' CONFIG_PPP_SYNC_TTY $CONFIG_PPP
    dep_tristate '  PPP Deflate compression' CONFIG_PPP_DEFLATE $CONFIG_PPP
    dep_tristate '  PPP BSD-Compress compression' CONFIG_PPP_BSDCOMP $CONFIG_PPP
+   dep_tristate '  Microsoft PPP compression/encryption (MPPC/MPPE)' CONFIG_PPP_MPPE $CONFIG_PPP
    if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
       dep_tristate '  PPP over Ethernet (EXPERIMENTAL)' CONFIG_PPPOE $CONFIG_PPP
    fi
--- linux-2.4.20/drivers/net/Makefile~150-mppe-mppc-0.98	2005-01-07 02:54:46.352831000 -0500
+++ linux-2.4.20/drivers/net/Makefile	2005-01-07 02:57:48.409154864 -0500
@@ -18,8 +18,9 @@
 export-objs     :=	8390.o arlan.o aironet4500_core.o aironet4500_card.o \
 			ppp_async.o ppp_generic.o slhc.o pppox.o auto_irq.o \
 			net_init.o mii.o
-list-multi	:=	rcpci.o
+list-multi	:=	rcpci.o ppp_mppe_mppc.o
 rcpci-objs	:=	rcpci45.o rclanmtl.o
+ppp_mppe_c-objs	:=	ppp_mppe_mppc_comp.o ppp_mppe_crypto.o
 
 # subdir-m += mac
 subdir-m += diag
@@ -159,6 +160,15 @@
 obj-$(CONFIG_PPP_SYNC_TTY) += ppp_synctty.o
 obj-$(CONFIG_PPP_DEFLATE) += ppp_deflate.o
 obj-$(CONFIG_PPP_BSDCOMP) += bsd_comp.o
+
+ifeq ($(CONFIG_PPP_MPPE),y)
+  obj-y += $(ppp_mppe_c-objs)
+else
+  ifeq ($(CONFIG_PPP_MPPE),m)
+    obj-m += ppp_mppe_mppc.o
+  endif
+endif
+
 obj-$(CONFIG_PPPOE) += pppox.o pppoe.o
 
 obj-$(CONFIG_SLIP) += slip.o
@@ -291,3 +301,5 @@
 rcpci.o: $(rcpci-objs)
 	$(LD) -r -o $@ $(rcpci-objs)
 
+ppp_mppe_mppc.o: $(ppp_mppe_c-objs)
+	$(LD) -r -o $@ $(ppp_mppe_c-objs)
--- linux-2.4.20/drivers/net/ppp_generic.c~150-mppe-mppc-0.98	2002-11-28 18:53:14.000000000 -0500
+++ linux-2.4.20/drivers/net/ppp_generic.c	2005-01-07 02:57:48.414154104 -0500
@@ -19,7 +19,7 @@
  * PPP driver, written by Michael Callahan and Al Longyear, and
  * subsequently hacked by Paul Mackerras.
  *
- * ==FILEVERSION 20020217==
+ * ==FILEVERSION 20030706==
  */
 
 #include <linux/config.h>
@@ -102,6 +102,7 @@
 	spinlock_t	rlock;		/* lock for receive side 58 */
 	spinlock_t	wlock;		/* lock for transmit side 5c */
 	int		mru;		/* max receive unit 60 */
+	int		mru_alloc;	/* MAX(1500,MRU) for dev_alloc_skb() */
 	unsigned int	flags;		/* control bits 64 */
 	unsigned int	xstate;		/* transmit state bits 68 */
 	unsigned int	rstate;		/* receive state bits 6c */
@@ -129,6 +130,7 @@
 	struct sock_fprog pass_filter;	/* filter for packets to pass */
 	struct sock_fprog active_filter;/* filter for pkts to reset idle */
 #endif /* CONFIG_PPP_FILTER */
+	int		xpad;		/* ECP or CCP (MPPE) transmit padding */
 };
 
 /*
@@ -552,7 +554,9 @@
 	case PPPIOCSMRU:
 		if (get_user(val, (int *) arg))
 			break;
-		ppp->mru = val;
+		ppp->mru_alloc = ppp->mru = val;
+		if (ppp->mru_alloc < PPP_MRU)
+		    ppp->mru_alloc = PPP_MRU;	/* increase for broken peers */
 		err = 0;
 		break;
 
@@ -1024,14 +1028,35 @@
 	case PPP_CCP:
 		/* peek at outbound CCP frames */
 		ppp_ccp_peek(ppp, skb, 0);
+		/*
+		 * When LZS or MPPE/MPPC is negotiated we don't send
+		 * CCP_RESETACK after receiving CCP_RESETREQ; in fact pppd
+		 * sends such a packet but we silently discard it here
+		 */
+		if (CCP_CODE(skb->data+2) == CCP_RESETACK
+		    && (ppp->xcomp->compress_proto == CI_MPPE
+			|| ppp->xcomp->compress_proto == CI_LZS)) {
+		    --ppp->stats.tx_packets;
+		    ppp->stats.tx_bytes -= skb->len - 2;
+		    kfree_skb(skb);
+		    return;
+		}
 		break;
 	}
 
 	/* try to do packet compression */
 	if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
 	    && proto != PPP_LCP && proto != PPP_CCP) {
-		new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
-				    GFP_ATOMIC);
+		int comp_ovhd = 0;
+		/* because of possible data expansion when MPPC or LZS
+		   is used, allocate compressor's buffer about 12.5% bigger
+		   than MTU */
+		if (ppp->xcomp->compress_proto == CI_MPPE)
+		    comp_ovhd = (((ppp->dev->mtu * 9) / 8) + 1);
+		else if (ppp->xcomp->compress_proto == CI_LZS)
+		    comp_ovhd = (((ppp->dev->mtu * 9) / 8) + 1) + LZS_OVHD;
+		new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len
+				    + ppp->xpad + comp_ovhd, GFP_ATOMIC);
 		if (new_skb == 0) {
 			printk(KERN_ERR "PPP: no memory (comp pkt)\n");
 			goto drop;
@@ -1043,15 +1068,28 @@
 		/* compressor still expects A/C bytes in hdr */
 		len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
 					   new_skb->data, skb->len + 2,
-					   ppp->dev->mtu + PPP_HDRLEN);
+					   ppp->dev->mtu + ppp->xpad
+					   + PPP_HDRLEN);
 		if (len > 0 && (ppp->flags & SC_CCP_UP)) {
 			kfree_skb(skb);
 			skb = new_skb;
 			skb_put(skb, len);
 			skb_pull(skb, 2);	/* pull off A/C bytes */
-		} else {
+		} else if (len == 0) {
 			/* didn't compress, or CCP not up yet */
 			kfree_skb(new_skb);
+		} else {
+			/*
+			 * (len < 0)
+			 * MPPE requires that we do not send unencrypted
+			 * frames.  The compressor will return -1 if we
+			 * should drop the frame.  We cannot simply test
+			 * the compress_proto because MPPE and MPPC share
+			 * the same number.
+			 */
+			printk(KERN_ERR "ppp: compressor dropped pkt\n");
+			kfree_skb(new_skb);
+			goto drop;
 		}
 	}
 
@@ -1539,14 +1577,15 @@
 	int len;
 
 	if (proto == PPP_COMP) {
-		ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
+		ns = dev_alloc_skb(ppp->mru_alloc + PPP_HDRLEN);
 		if (ns == 0) {
 			printk(KERN_ERR "ppp_decompress_frame: no memory\n");
 			goto err;
 		}
 		/* the decompressor still expects the A/C bytes in the hdr */
 		len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
-				skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
+				skb->len + 2, ns->data,
+				ppp->mru_alloc + PPP_HDRLEN);
 		if (len < 0) {
 			/* Pass the compressed frame to pppd as an
 			   error indication. */
@@ -1572,7 +1611,12 @@
 	return skb;
 
  err:
-	ppp->rstate |= SC_DC_ERROR;
+	if (ppp->rcomp->compress_proto != CI_MPPE
+	    && ppp->rcomp->compress_proto != CI_LZS) {
+	    /* If decompression protocol isn't MPPE/MPPC or LZS, we set
+	     SC_DC_ERROR flag and wait for CCP_RESETACK */
+	    ppp->rstate |= SC_DC_ERROR;
+	}
 	ppp_receive_error(ppp);
 	return skb;
 }
@@ -1981,6 +2025,20 @@
 				ocomp->comp_free(ostate);
 			err = 0;
 		}
+		if (ccp_option[0] == CI_MPPE)
+			/*
+			 * pppd (userland) has reduced the MTU by MPPE_PAD,
+			 * to accomodate "compressor" growth.  We must
+			 * increase the space allocated for compressor
+			 * output in ppp_send_frame() accordingly.  Note
+			 * that from a purist's view, it may be more correct
+			 * to require multilink and fragment large packets,
+			 * but that seems inefficient compared to this
+			 * little trick.
+			 */
+			ppp->xpad = MPPE_PAD;
+		else
+			ppp->xpad = 0;
 
 	} else {
 		state = cp->decomp_alloc(ccp_option, data.length);
@@ -2252,6 +2310,7 @@
 	/* Initialize the new ppp unit */
 	ppp->file.index = unit;
 	ppp->mru = PPP_MRU;
+	ppp->mru_alloc = PPP_MRU;
 	init_ppp_file(&ppp->file, INTERFACE);
 	ppp->file.hdrlen = PPP_HDRLEN - 2;	/* don't count proto bytes */
 	for (i = 0; i < NUM_NP; ++i)
--- /dev/null	2004-04-06 13:56:48.000000000 -0400
+++ linux-2.4.20/drivers/net/ppp_mppe_crypto.c	2005-01-07 02:57:48.414154104 -0500
@@ -0,0 +1,257 @@
+/*
+ * ppp_mppe_crypto.c - cryptografic funtions for MPPE
+ *
+ *  This code is Public Domain. Please see comments below.
+ *
+ *  I have just put SHA1 and ARCFOUR implementations into one file
+ *  in order to not pollute kernel namespace. 
+ *
+ *  Jan Dubiec <jdx@slackware.pl>, 2003-07-08
+ */
+
+/*
+ * ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c
+ * 
+ * SHA-1 in C
+ * By Steve Reid <steve@edmweb.com>
+ * 100% Public Domain
+ * 
+ * Test Vectors (from FIPS PUB 180-1)
+ * "abc"
+ * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
+ * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+ * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
+ * A million repetitions of "a"
+ * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
+ */
+
+/* #define SHA1HANDSOFF * Copies data before messing with it. */
+
+#if defined(__linux__)
+#include <asm/byteorder.h>
+#include <linux/string.h>
+#elif defined(__solaris__)
+#include <sys/isa_defs.h>
+#include <sys/ddi.h>
+#include <sys/sunddi.h>
+#define memcpy(d, s, c) bcopy(s, d, c)
+#define memset(d, b, c) bzero(d, c)
+#endif
+
+#include "ppp_mppe_crypto.h"
+
+static void SHA1_Transform(unsigned long[5], const unsigned char[64]);
+
+#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
+
+/* blk0() and blk() perform the initial expand. */
+/* I got the idea of expanding during the round function from SSLeay */
+#if defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN)
+#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
+    |(rol(block->l[i],8)&0x00FF00FF))
+#elif defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)
+#define blk0(i) block->l[i]
+#else
+#error Endianness not defined
+#endif
+#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
+    ^block->l[(i+2)&15]^block->l[i&15],1))
+
+/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
+#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
+#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
+#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
+#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
+#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
+
+/* Hash a single 512-bit block. This is the core of the algorithm. */
+static void
+SHA1_Transform(unsigned long state[5], const unsigned char buffer[64])
+{
+    unsigned long a, b, c, d, e;
+    typedef union {
+	unsigned char c[64];
+	unsigned long l[16];
+    } CHAR64LONG16;
+    CHAR64LONG16 *block;
+
+#ifdef SHA1HANDSOFF
+    static unsigned char workspace[64];
+    block = (CHAR64LONG16 *) workspace;
+    memcpy(block, buffer, 64);
+#else
+    block = (CHAR64LONG16 *) buffer;
+#endif
+    /* Copy context->state[] to working vars */
+    a = state[0];
+    b = state[1];
+    c = state[2];
+    d = state[3];
+    e = state[4];
+    /* 4 rounds of 20 operations each. Loop unrolled. */
+    R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
+    R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
+    R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
+    R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
+    R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
+    R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
+    R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
+    R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
+    R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
+    R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
+    R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
+    R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
+    R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
+    R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
+    R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
+    R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
+    R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
+    R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
+    R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
+    R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
+    /* Add the working vars back into context.state[] */
+    state[0] += a;
+    state[1] += b;
+    state[2] += c;
+    state[3] += d;
+    state[4] += e;
+    /* Wipe variables */
+    a = b = c = d = e = 0;
+}
+
+/* SHA1Init - Initialize new context */
+void
+SHA1_Init(SHA1_CTX *context)
+{
+    /* SHA1 initialization constants */
+    context->state[0] = 0x67452301;
+    context->state[1] = 0xEFCDAB89;
+    context->state[2] = 0x98BADCFE;
+    context->state[3] = 0x10325476;
+    context->state[4] = 0xC3D2E1F0;
+    context->count[0] = context->count[1] = 0;
+}
+
+/* Run your data through this. */
+void
+SHA1_Update(SHA1_CTX *context, const unsigned char *data, unsigned int len)
+{
+    unsigned int i, j;
+
+    j = (context->count[0] >> 3) & 63;
+    if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
+    context->count[1] += (len >> 29);
+    if ((j + len) > 63) {
+	memcpy(&context->buffer[j], data, (i = 64-j));
+	SHA1_Transform(context->state, context->buffer);
+	for ( ; i + 63 < len; i += 64) {
+	    SHA1_Transform(context->state, &data[i]);
+	}
+	j = 0;
+    }
+    else
+	i = 0;
+
+    memcpy(&context->buffer[j], &data[i], len - i);
+}
+
+/* Add padding and return the message digest. */
+void
+SHA1_Final(unsigned char digest[20], SHA1_CTX *context)
+{
+    unsigned long i, j;
+    unsigned char finalcount[8];
+
+    for (i = 0; i < 8; i++) {
+        finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
+         >> ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */
+    }
+    SHA1_Update(context, (unsigned char *) "\200", 1);
+    while ((context->count[0] & 504) != 448) {
+	SHA1_Update(context, (unsigned char *) "\0", 1);
+    }
+    SHA1_Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */
+    for (i = 0; i < 20; i++) {
+	digest[i] = (unsigned char)
+		     ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
+    }
+    /* Wipe variables */
+    i = j = 0;
+    memset(context->buffer, 0, 64);
+    memset(context->state, 0, 20);
+    memset(context->count, 0, 8);
+    memset(&finalcount, 0, 8);
+#ifdef SHA1HANDSOFF  /* make SHA1Transform overwrite it's own static vars */
+    SHA1Transform(context->state, context->buffer);
+#endif
+}
+
+/*
+ * arcfour.c
+ * by Frank Cusack <frank@google.com>
+ * 100% public domain
+ *
+ * Implemented from the description in _Applied Cryptography_, 2nd ed.
+ *
+ * ** Distribution ** of this software is unlimited and unrestricted.
+ *
+ * ** Use ** of this software is almost certainly legal; however, refer
+ * to <http://theory.lcs.mit.edu/~rivest/faq.html>.
+ */
+
+#define swap(a, b)		\
+{				\
+    unsigned char t = b;	\
+    b = a;			\
+    a = t;			\
+}
+
+/*
+ * Initialize arcfour from a key.
+ */
+void
+arcfour_setkey(arcfour_context *context, const unsigned char *key,
+	       unsigned keylen)
+{
+    unsigned i, j;
+    unsigned char K[256];
+
+    context->i = context->j = 0;
+
+    for (i = 0; i < 256; i++) {
+	context->S[i] = i;
+	K[i] = key[i % keylen];
+    }
+
+    j = 0;
+    for (i = 0; i < 256; i++) {
+	j = (j + context->S[i] + K[i]) % 256;
+	swap(context->S[i], context->S[j]);
+    }
+
+    memset(K, 0, sizeof(K));
+}
+
+/*
+ * plaintext -> ciphertext (or vice versa)
+ */
+void
+arcfour_encrypt(arcfour_context *context, const unsigned char *in, unsigned len,
+		unsigned char *out)
+{
+    unsigned i = context->i;
+    unsigned j = context->j;
+    unsigned char *S = context->S;
+    unsigned char K;
+
+    while (len--) {
+	i = (i + 1) % 256;
+	j = (j + S[i]) % 256;
+	swap(S[i], S[j]);
+	K = S[(S[i] + S[j]) % 256];
+	*out++ = *in++ ^ K;
+    }
+
+    context->i = i;
+    context->j = j;
+}
--- /dev/null	2004-04-06 13:56:48.000000000 -0400
+++ linux-2.4.20/drivers/net/ppp_mppe_crypto.h	2005-01-07 02:57:48.415153952 -0500
@@ -0,0 +1,40 @@
+/*
+ * ppp_mppe_crypto.h - cryptografic funtion prototypes for MPPE
+ *
+ *  This code is Public Domain. Please see comments below.
+ *
+ *  I have just put SHA1 and ARCFOUR declarations into one file
+ *  in order to not pollute kernel namespace. 
+ *
+ *  Jan Dubiec <jdx@slackware.pl>, 2003-07-08
+ */
+
+#ifndef _PPP_MPPE_CRYPTO_
+#define _PPP_MPPE_CRYPTO_
+
+/* SHA1 definitions and prototypes */
+typedef struct {
+    unsigned long state[5];
+    unsigned long count[2];
+    unsigned char buffer[64];
+} SHA1_CTX;
+
+#define SHA1_SIGNATURE_SIZE 20
+
+extern void SHA1_Init(SHA1_CTX *);
+extern void SHA1_Update(SHA1_CTX *, const unsigned char *, unsigned int);
+extern void SHA1_Final(unsigned char[SHA1_SIGNATURE_SIZE], SHA1_CTX *);
+
+/* ARCFOUR (aka RC4) definitions and prototypes */
+typedef struct {
+    unsigned i;
+    unsigned j;
+    unsigned char S[256];
+} arcfour_context;
+
+extern void arcfour_setkey(arcfour_context *, const unsigned char *, unsigned);
+extern void arcfour_encrypt(arcfour_context *, const unsigned char *, unsigned,
+			    unsigned char *);
+#define arcfour_decrypt arcfour_encrypt
+
+#endif /* _PPP_MPPE_CRYPTO_ */
--- /dev/null	2004-04-06 13:56:48.000000000 -0400
+++ linux-2.4.20/drivers/net/ppp_mppe_mppc_comp.c	2005-01-07 02:57:48.417153648 -0500
@@ -0,0 +1,1144 @@
+/*
+ * ppp_mppe_mppc_comp.c - MPPC/MPPE "compressor/decompressor" module.
+ *
+ * Copyright (c) 1994 Árpád Magosányi <mag@bunuel.tii.matav.hu>
+ * Copyright (c) 1999 Tim Hockin, Cobalt Networks Inc. <thockin@cobaltnet.com>
+ * Copyright (c) 2002, 2003 Jan Dubiec <jdx@slackware.pl>
+ * 
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation is hereby granted, provided that the above copyright
+ * notice appears in all copies. This software is provided without any
+ * warranty, express or implied.
+ *
+ * The code is based on MPPE kernel module written by Árpád Magosányi and
+ * Tim Hockin which can be found on http://planetmirror.com/pub/mppe/.
+ * I have added MPPC and 56 bit session keys support in MPPE.
+ *
+ * WARNING! Although this is open source code, its usage in some countries
+ * (in particular in the USA) may violate Stac Inc. patent for MPPC.
+ *
+ * Compile command:
+ * gcc -O2 -Wall -I/usr/src/linux/include -D__KERNEL__ -DMODULE -c ppp_mppe_mppc_comp.c
+ *
+ *  ==FILEVERSION 20030807==
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/vmalloc.h>
+#include <linux/init.h>
+
+#include <linux/ppp_defs.h>
+#include <linux/ppp-comp.h>
+
+#include "ppp_mppe_crypto.h"
+
+/*
+ * State for a mppc/mppe "(de)compressor".
+ */
+struct ppp_mppe_state {
+    arcfour_context arcfour_context;
+    u8		master_key[MPPE_MAX_KEY_LEN];
+    u8		session_key[MPPE_MAX_KEY_LEN];
+    u8		mppc;		/* do we use compression (MPPC)? */
+    u8		mppe;		/* do we use encryption (MPPE)? */
+    u8		keylen;		/* key length in bytes */
+    u8		bitkeylen;	/* key length in bits */
+    u16		ccount;		/* coherency counter */
+    u16		bits;		/* MPPC/MPPE control bits */
+    u8		stateless;	/* do we use stateless mode? */
+    u8		nextflushed;	/* set A bit in the next outgoing packet;
+				   used only by compressor*/
+    u8		flushexpected;	/* drop packets until A bit is received;
+				   used only by decompressor*/
+    u8		*hist;		/* MPPC history */
+    u16		*hash;		/* Hash table; used only by compressor */
+    u16		histptr;	/* history "cursor" */
+    int		unit;
+    int		debug;
+    int		mru;
+    struct compstat stats;
+};
+
+#define MPPE_OVHD		2	/* MPPE overhead */
+#define MPPE_HIST_LEN		8192	/* MPPC history size */
+#define MPPE_MAX_CCOUNT		0x0FFF	/* max. coherency counter value */
+
+#define MPPE_BIT_FLUSHED	0x80	/* bit A */
+#define MPPE_BIT_RESET		0x40	/* bit B */
+#define MPPE_BIT_COMP		0x20	/* bit C */
+#define MPPE_BIT_ENCRYPTED	0x10	/* bit D */
+
+#define MPPE_SALT0		0xD1	/* values used in MPPE key derivation */
+#define MPPE_SALT1		0x26	/* according to RFC3079 */
+#define MPPE_SALT2		0x9E
+
+#define MPPE_CCOUNT(x)		((((x)[4] & 0x0f) << 8) + (x)[5])
+#define MPPE_BITS(x)		((x)[4] & 0xf0)
+#define MPPE_CTRLHI(x)		((((x)->ccount & 0xf00)>>8)|((x)->bits))
+#define MPPE_CTRLLO(x)		((x)->ccount & 0xff)
+
+/*
+ * Key Derivation, from RFC 3078, RFC 3079.
+ * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
+ */
+static void
+GetNewKeyFromSHA(unsigned char *MasterKey, unsigned char *SessionKey,
+		 unsigned long SessionKeyLength, unsigned char *InterimKey)
+{
+    /*Pads used in key derivation */
+    static unsigned char  SHAPad1[40] =
+    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+
+    static unsigned char  SHAPad2[40] =
+    { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
+      0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
+      0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
+      0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2 };
+
+    SHA1_CTX Context;
+    unsigned char Digest[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);
+    memcpy(InterimKey, Digest, SessionKeyLength);
+}
+
+static void
+mppe_change_key(struct ppp_mppe_state *state, int initialize)
+{
+    unsigned char InterimKey[MPPE_MAX_KEY_LEN];
+
+    GetNewKeyFromSHA(state->master_key, state->session_key,
+		     state->keylen, InterimKey);
+    if (initialize) {
+	memcpy(state->session_key, InterimKey, state->keylen);
+    } else {
+	arcfour_setkey(&state->arcfour_context, InterimKey, state->keylen);
+	arcfour_encrypt(&state->arcfour_context, InterimKey, state->keylen,
+			state->session_key);
+    }
+    if (state->keylen == 8) {
+	if (state->bitkeylen == 40) {
+	    state->session_key[0] = MPPE_SALT0;
+	    state->session_key[1] = MPPE_SALT1;
+	    state->session_key[2] = MPPE_SALT2;
+	} else {
+	    state->session_key[0] = MPPE_SALT0;
+	}
+    }
+    arcfour_setkey(&state->arcfour_context, state->session_key, state->keylen);
+}
+
+/* increase 12-bit coherency counter */
+static inline void
+mppe_increase_ccount(struct ppp_mppe_state *state)
+{
+    state->ccount = (state->ccount + 1) & MPPE_MAX_CCOUNT;
+    if (state->mppe) {
+	if (state->stateless) {
+	    mppe_change_key(state, 0);
+	    state->nextflushed = 1;
+	} else {
+	    if ((state->ccount & 0xff) == 0xff) {
+		mppe_change_key(state, 0);
+		state->nextflushed = 1;
+	    }
+	}
+    }
+}
+
+/* allocate space for a MPPE/MPPC (de)compressor.  */
+/*   comp != 0 -> init compressor */
+/*   comp = 0 -> init decompressor */
+static void *
+mppe_alloc(unsigned char *options, int opt_len, int comp)
+{
+    struct ppp_mppe_state *state;
+    u8* fname;
+
+    fname = comp ? "mppe_comp_alloc" : "mppe_decomp_alloc";
+
+    /*  
+	Hack warning - additionally to the standard MPPC/MPPE configuration
+	options, pppd passes to the (de)copressor 8 or 16 byte session key.
+	Therefore options[1] contains MPPC/MPPE configuration option length
+	(CILEN_MPPE = 6), but the real options length, depending on the key
+	length, is 6+8 or 6+16.
+    */
+    if (opt_len < CILEN_MPPE) {
+	printk(KERN_WARNING "%s: wrong options length: %u\n", fname, opt_len);
+	return NULL;
+    }
+
+    if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
+	(options[2] & ~MPPE_STATELESS) != 0 ||
+	options[3] != 0 || options[4] != 0 ||
+	(options[5] & ~(MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
+	(options[5] & (MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
+	printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
+	       "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname, options[0],
+	       options[1], options[2], options[3], options[4], options[5]);
+	return NULL;
+    }
+
+    state = (struct ppp_mppe_state *)kmalloc(sizeof(*state), GFP_KERNEL);
+    if (state == NULL) {
+	printk(KERN_ERR "%s: cannot allocate space for %scompressor\n", fname,
+	       comp ? "" : "de");
+	return NULL;
+    }
+    memset(state, 0, sizeof(struct ppp_mppe_state));
+
+    state->mppc = options[5] & MPPE_MPPC;	/* Do we use MPPC? */
+    state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
+	MPPE_40BIT);				/* Do we use MPPE? */
+
+    if (state->mppc) {
+	/* allocate MPPC history */
+	state->hist = (u8*)vmalloc(2*MPPE_HIST_LEN*sizeof(u8));
+	if (state->hist == NULL) {
+	    kfree(state);
+	    printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
+		   fname);
+	    return NULL;
+	}
+
+	/* allocate hashtable for MPPC compressor */
+	if (comp) {
+	    state->hash = (u16*)vmalloc(MPPE_HIST_LEN*sizeof(u16));
+	    if (state->hash == NULL) {
+		vfree(state->hist);
+		kfree(state);
+		printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
+		       fname);
+		return NULL;
+	    }
+	}
+    }
+
+    MOD_INC_USE_COUNT;
+
+    if (state->mppe) { /* specific for MPPE */
+	memcpy(state->master_key, options+CILEN_MPPE, MPPE_MAX_KEY_LEN);
+	memcpy(state->session_key, state->master_key, MPPE_MAX_KEY_LEN);
+	/* initial key generation is done in mppe_init() */
+    }
+
+    return (void *) state;
+}
+
+static void *
+mppe_comp_alloc(unsigned char *options, int opt_len)
+{
+    return mppe_alloc(options, opt_len, 1);
+}
+
+static void *
+mppe_decomp_alloc(unsigned char *options, int opt_len)
+{
+    return mppe_alloc(options, opt_len, 0);
+}
+
+/* cleanup the (de)compressor */
+static void
+mppe_comp_free(void *arg)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
+
+    if (state != NULL) {
+	if (state->hist != NULL)
+	    vfree(state->hist);
+	if (state->hash != NULL)
+	    vfree(state->hash);
+	kfree(state);
+    }
+    MOD_DEC_USE_COUNT;
+}
+
+/* init MPPC/MPPE (de)compresor */
+/*   comp != 0 -> init compressor */
+/*   comp = 0 -> init decompressor */
+static int
+mppe_init(void *arg, unsigned char *options, int opt_len, int unit,
+	  int hdrlen, int mru, int debug, int comp)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
+    u8* fname;
+
+    fname = comp ? "mppe_comp_init" : "mppe_decomp_init";
+
+    if (opt_len < CILEN_MPPE) {
+	if (debug)
+	    printk(KERN_WARNING "%s: wrong options length: %u\n",
+		   fname, opt_len);
+	return 0;
+    }
+
+    if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
+	(options[2] & ~MPPE_STATELESS) != 0 ||
+	options[3] != 0 || options[4] != 0 ||
+	(options[5] & ~(MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
+	(options[5] & (MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
+	if (debug)
+	    printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
+		   "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname,
+		   options[0], options[1], options[2], options[3], options[4],
+		   options[5]);
+	return 0;
+    }
+
+    if ((options[5] & ~MPPE_MPPC) != MPPE_128BIT &&
+	(options[5] & ~MPPE_MPPC) != MPPE_56BIT &&
+	(options[5] & ~MPPE_MPPC) != MPPE_40BIT &&
+	(options[5] & MPPE_MPPC) != MPPE_MPPC) {
+	if (debug)
+	    printk(KERN_WARNING "%s: don't know what to do: o[5]=%02x\n",
+		   fname, options[5]);
+	return 0;
+    }
+
+    state->mppc = options[5] & MPPE_MPPC;	/* Do we use MPPC? */
+    state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
+	MPPE_40BIT);				/* Do we use MPPE? */
+    state->stateless = options[2] & MPPE_STATELESS; /* Do we use stateless mode? */
+
+    switch (state->mppe) {
+    case MPPE_40BIT:     /* 40 bit key */
+	state->keylen = 8;
+	state->bitkeylen = 40;
+	break;
+    case MPPE_56BIT:     /* 56 bit key */
+	state->keylen = 8;
+	state->bitkeylen = 56;
+	break;
+    case MPPE_128BIT:     /* 128 bit key */
+	state->keylen = 16;
+	state->bitkeylen = 128;
+	break;
+    default:
+	state->keylen = 0;
+	state->bitkeylen = 0;
+    }
+
+    state->ccount = MPPE_MAX_CCOUNT;
+    state->bits = 0;
+    state->unit  = unit;
+    state->debug = debug;
+    state->histptr = MPPE_HIST_LEN;
+    if (state->mppc) {	/* reset history if MPPC was negotiated */
+	memset(state->hist, 0, 2*MPPE_HIST_LEN*sizeof(u8));
+    }
+
+    if (state->mppe) { /* generate initial session keys */
+	mppe_change_key(state, 1);
+    }
+
+    if (comp) { /* specific for compressor */
+	state->nextflushed = 1;
+    } else { /* specific for decompressor */
+	state->mru = mru;
+	state->flushexpected = 1;
+    }
+
+    return 1;
+}
+
+static int
+mppe_comp_init(void *arg, unsigned char *options, int opt_len, int unit,
+	       int hdrlen, int debug)
+{
+    return mppe_init(arg, options, opt_len, unit, hdrlen, 0, debug, 1);
+}
+
+
+static int
+mppe_decomp_init(void *arg, unsigned char *options, int opt_len, int unit,
+		 int hdrlen, int mru, int debug)
+{
+    return mppe_init(arg, options, opt_len, unit, hdrlen, mru, debug, 0);
+}
+
+static void
+mppe_comp_reset(void *arg)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
+
+    if (state->debug)
+	printk(KERN_DEBUG "%s%d: resetting MPPC/MPPE compressor\n",
+	       __FUNCTION__, state->unit);
+
+    state->nextflushed = 1;
+    if (state->mppe)
+	arcfour_setkey(&state->arcfour_context, state->session_key,
+		       state->keylen);
+}
+
+static void
+mppe_decomp_reset(void *arg)
+{
+    /* When MPPC/MPPE is in use, we shouldn't receive any CCP Reset-Ack.
+       But when we receive such a packet, we just ignore it. */
+    return;
+}
+
+static void
+mppe_stats(void *arg, struct compstat *stats)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
+
+    *stats = state->stats;
+}
+
+/***************************/
+/**** Compression stuff ****/
+/***************************/
+/* inserts 1 to max. 8 bits into the output buffer */
+static inline void putbits8(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
+{
+    buf += *i;
+    if (*l >= n) {
+	*l = (*l) - n;
+	val <<= *l;
+	*buf = *buf | (val & 0xff);
+	if (*l == 0) {
+	    *l = 8;
+	    (*i)++;
+	    *(++buf) = 0;
+	}
+    } else {
+	(*i)++;
+	*l = 8 - n + (*l);
+	val <<= *l;
+	*buf = *buf | ((val >> 8) & 0xff);
+	*(++buf) = val & 0xff;
+    }
+}
+
+/* inserts 9 to max. 16 bits into the output buffer */
+static inline void putbits16(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
+{
+    buf += *i;
+    if (*l >= n - 8) {
+	(*i)++;
+	*l = 8 - n + (*l);
+	val <<= *l;
+	*buf = *buf | ((val >> 8) & 0xff);
+	*(++buf) = val & 0xff;
+	if (*l == 0) {
+	    *l = 8;
+	    (*i)++;
+	    *(++buf) = 0;
+	}
+    } else {
+	(*i)++; (*i)++;
+	*l = 16 - n + (*l);
+	val <<= *l;
+	*buf = *buf | ((val >> 16) & 0xff);
+	*(++buf) = (val >> 8) & 0xff;
+	*(++buf) = val & 0xff;
+    }
+}
+
+/* inserts 17 to max. 24 bits into the output buffer */
+static inline void putbits24(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
+{
+    buf += *i;
+    if (*l >= n - 16) {
+	(*i)++; (*i)++;
+	*l = 16 - n + (*l);
+	val <<= *l;
+	*buf = *buf | ((val >> 16) & 0xff);
+	*(++buf) = (val >> 8) & 0xff;
+	*(++buf) = val & 0xff;
+	if (*l == 0) {
+	    *l = 8;
+	    (*i)++;
+	    *(++buf) = 0;
+	}
+    } else {
+	(*i)++; (*i)++; (*i)++;
+	*l = 24 - n + (*l);
+	val <<= *l;
+	*buf = *buf | ((val >> 24) & 0xff);
+	*(++buf) = (val >> 16) & 0xff;
+	*(++buf) = (val >> 8) & 0xff;
+	*(++buf) = val & 0xff;
+    }
+}
+
+static int
+mppc_compress(struct ppp_mppe_state *state, unsigned char *ibuf,
+	      unsigned char *obuf, int isize, int osize)
+{
+    u32 olen, off, len, idx, i, l;
+    u8 *hist, *sbuf, *p, *q, *r, *s;
+
+    /*  
+	At this point, to avoid possible buffer overflow caused by packet
+	expansion during/after compression,  we should make sure that
+	osize >= (((isize*9)/8)+1)+2, but we don't do that because in
+	ppp_generic.c we just allocate bigger obuf.
+
+	Maximum MPPC packet expansion is 12.5%. This is the worst case when
+	all octets in the input buffer are >= 0x80 and we cannot find any
+	repeated tokens. Additionally we have to reserve 2 bytes for MPPE/MPPC
+	status bits and coherency counter.
+    */
+
+    hist = state->hist + MPPE_HIST_LEN;
+    /* check if there is enough room at the end of the history */
+    if (state->histptr + isize >= 2*MPPE_HIST_LEN) {
+	state->bits |= MPPE_BIT_RESET;
+	state->histptr = MPPE_HIST_LEN;
+	memcpy(state->hist, hist, MPPE_HIST_LEN);
+    }
+    /* add packet to the history; isize must be <= MPPE_HIST_LEN */
+    sbuf = state->hist + state->histptr;
+    memcpy(sbuf, ibuf, isize);
+    state->histptr += isize;
+
+    /* compress data */
+    r = sbuf + isize;
+    *obuf = olen = i = 0;
+    l = 8;
+    while (i < isize - 2) {
+	s = q = sbuf + i;
+	idx = ((40542*((((s[0]<<4)^s[1])<<4)^s[2]))>>4) & 0x1FFF;
+	p = hist + state->hash[idx];
+	state->hash[idx] = (u16) (s - hist);
+	off = s - p;
+	if (off > 8191 || off < 1 || *p++ != *s++ || *p++ != *s++ || *p++ != *s++)
+	    goto literal;
+	if (r - q >= 64) {
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
+	    *p++ != *s++;
+	    if (s - q == 64) {
+		p--; s--;
+		while((*p++ == *s++) && (s < r));
+	    }
+	} else {
+	    while((*p++ == *s++) && (s < r));
+	}
+	len = s - q - 1;
+	i += len;
+
+	/* at least 3 character match found; code data */
+	/* encode offset */
+	if (off < 64) {			/* 10-bit offset; 0 <= offset < 64 */
+	    putbits16(obuf, 0x3c0|off, 10, &olen, &l);
+	} else if (off < 320) {		/* 12-bit offset; 64 <= offset < 320 */
+	    putbits16(obuf, 0xe00|(off-64), 12, &olen, &l);
+	} else if (off < 8192) {	/* 16-bit offset; 320 <= offset < 8192 */
+	    putbits16(obuf, 0xc000|(off-320), 16, &olen, &l);
+	} else {
+	    /* This shouldn't happen; we return 0 what means "packet expands",
+	    and we send packet uncompressed. */
+	    if (state->debug)
+		printk(KERN_DEBUG "%s%d: wrong offset value: %d\n",
+		       __FUNCTION__, state->unit, off);
+	    return 0;
+	}
+	/* encode length of match */
+	if (len < 4) {			/* length = 3 */
+	    putbits8(obuf, 0, 1, &olen, &l);
+	} else if (len < 8) {		/* 4 <= length < 8 */
+	    putbits8(obuf, 0x08|(len&0x03), 4, &olen, &l);
+	} else if (len < 16) {		/* 8 <= length < 16 */
+	    putbits8(obuf, 0x30|(len&0x07), 6, &olen, &l);
+	} else if (len < 32) {		/* 16 <= length < 32 */
+	    putbits8(obuf, 0xe0|(len&0x0f), 8, &olen, &l);
+	} else if (len < 64) {		/* 32 <= length < 64 */
+	    putbits16(obuf, 0x3c0|(len&0x1f), 10, &olen, &l);
+	} else if (len < 128) {		/* 64 <= length < 128 */
+	    putbits16(obuf, 0xf80|(len&0x3f), 12, &olen, &l);
+	} else if (len < 256) {		/* 128 <= length < 256 */
+	    putbits16(obuf, 0x3f00|(len&0x7f), 14, &olen, &l);
+	} else if (len < 512) {		/* 256 <= length < 512 */
+	    putbits16(obuf, 0xfe00|(len&0xff), 16, &olen, &l);
+	} else if (len < 1024) {	/* 512 <= length < 1024 */
+	    putbits24(obuf, 0x3fc00|(len&0x1ff), 18, &olen, &l);
+	} else if (len < 2048) {	/* 1024 <= length < 2048 */
+	    putbits24(obuf, 0xff800|(len&0x3ff), 20, &olen, &l);
+	} else if (len < 4096) {	/* 2048 <= length < 4096 */
+	    putbits24(obuf, 0x3ff000|(len&0x7ff), 22, &olen, &l);
+	} else if (len < 8192) {	/* 4096 <= length < 8192 */
+	    putbits24(obuf, 0xffe000|(len&0xfff), 24, &olen, &l);
+	} else {
+	    /* This shouldn't happen; we return 0 what means "packet expands",
+	    and send packet uncompressed. */
+	    if (state->debug)
+		printk(KERN_DEBUG "%s%d: wrong length of match value: %d\n",
+		       __FUNCTION__, state->unit, len);
+	    return 0;
+	}
+	continue;
+
+    literal:
+	/* no match found; encode literal byte */
+	if (ibuf[i] < 0x80) {		/* literal byte < 0x80 */
+	    putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l);
+	} else {			/* literal byte >= 0x80 */
+	    putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l);
+	}
+    }
+
+    /* Add remaining octets to the output */
+    while(isize - i > 0) {
+	if (ibuf[i] < 0x80) {	/* literal byte < 0x80 */
+	    putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l);
+	} else {		/* literal byte >= 0x80 */
+	    putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l);
+	}
+    }
+    /* Reset unused bits of the last output octet */
+    if ((l != 0) && (l != 8)) {
+	putbits8(obuf, 0, l, &olen, &l);
+    }
+
+    return (int) olen;
+}
+
+int
+mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
+	      int isize, int osize)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
+    int proto, olen, complen;
+    unsigned char *wptr;
+
+    /* Check that the protocol is in the range we handle. */
+    proto = PPP_PROTOCOL(ibuf);
+    if (proto < 0x0021 || proto > 0x00fa)
+	return 0;
+
+    wptr = obuf;
+    /* Copy over the PPP header */
+    wptr[0] = PPP_ADDRESS(ibuf);
+    wptr[1] = PPP_CONTROL(ibuf);
+    wptr[2] = PPP_COMP >> 8;
+    wptr[3] = PPP_COMP;
+    wptr += PPP_HDRLEN + MPPE_OVHD; /* Leave two octets for MPPE/MPPC bits */
+
+    mppe_increase_ccount(state);
+
+    if (state->nextflushed) {
+	if (!state->stateless) {
+	    state->nextflushed = 0;
+	}
+	state->bits |= MPPE_BIT_FLUSHED;
+	if (state->mppc) { /* reset history */
+	    state->bits |= MPPE_BIT_RESET;
+	    state->histptr = MPPE_HIST_LEN;
+	    memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
+	}
+    }
+
+    if (state->mppc && !state->mppe) { /* Do only compression */
+	complen = mppc_compress(state, ibuf+2, wptr, isize-2,
+				osize-PPP_HDRLEN-MPPE_OVHD);
+	if ((complen > isize) || (complen == 0)) {
+	    /* packet expands */
+	    state->nextflushed = 1;
+	    memcpy(wptr, ibuf+2, isize-2);
+	    olen = isize + (PPP_HDRLEN / 2) + MPPE_OVHD;
+	    (state->stats).inc_bytes += olen;
+	    (state->stats).inc_packets++;
+	} else {
+	    state->bits |= MPPE_BIT_COMP;
+	    olen = complen + PPP_HDRLEN + MPPE_OVHD;
+	    (state->stats).comp_bytes += olen;
+	    (state->stats).comp_packets++;
+	}
+    } else {
+	if (!state->mppc && state->mppe) { /* Do only encryption */
+	    /* read from ibuf, write to wptr, adjust for PPP_HDRLEN */
+	    arcfour_encrypt(&state->arcfour_context, ibuf+2, isize-2, wptr);
+	    state->bits |= MPPE_BIT_ENCRYPTED;
+	    olen = isize + (PPP_HDRLEN / 2) + MPPE_OVHD;
+	    (state->stats).inc_bytes += olen;
+	    (state->stats).inc_packets++;
+	} else { /* Do compression and then encryption - RFC3078 */
+	    complen = mppc_compress(state, ibuf+2, wptr, isize-2,
+				    osize-PPP_HDRLEN-MPPE_OVHD);
+	    if ((complen > isize) || (complen == 0)) {
+		/* packet expands */
+		memcpy(wptr, ibuf+2, isize-2);
+		state->nextflushed = 1;
+		arcfour_encrypt(&state->arcfour_context, ibuf+2, isize-2, wptr);
+		olen = isize + (PPP_HDRLEN / 2) + MPPE_OVHD;
+		(state->stats).inc_bytes += olen;
+		(state->stats).inc_packets++;
+	    } else {
+		state->bits |= MPPE_BIT_COMP;
+		/* Hack warning !!! RC4 implementation which we use does
+		   encryption "in place" - it means that input and output
+		   buffers can be *the same* memory area. Therefore we don't
+		   need to use a temporary buffer. But be careful - other
+		   implementations don't have to be so nice.
+		   I used to use ibuf as temporary buffer here, but it led
+		   packet sniffers into error. Thanks to Wilfried Weissmann
+		   for pointing that. */
+		arcfour_encrypt(&state->arcfour_context, wptr, complen, wptr);
+		olen = complen + PPP_HDRLEN + MPPE_OVHD;
+		(state->stats).comp_bytes += olen;
+		(state->stats).comp_packets++;
+	    }
+	    state->bits |= MPPE_BIT_ENCRYPTED;
+	}
+    }
+
+    /* write status bits and coherency counter into the output buffer */
+    wptr = obuf + PPP_HDRLEN;
+    wptr[0] = MPPE_CTRLHI(state);
+    wptr[1] = MPPE_CTRLLO(state);
+
+    state->bits = 0;
+
+    (state->stats).unc_bytes += isize;
+    (state->stats).unc_packets++;
+
+    return olen;
+}
+
+/***************************/
+/*** Decompression stuff ***/
+/***************************/
+static inline u32 getbits(const u8 *buf, const u32 n, u32 *i, u32 *l)
+{
+    static u32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
+    u32 res, ol;
+
+    ol = *l;
+    if (*l >= n) {
+	*l = (*l) - n;
+	res = (buf[*i] & m[ol]) >> (*l);
+	if (*l == 0) {
+	    *l = 8;
+	    (*i)++;
+	}
+    } else {
+	*l = 8 - n + (*l);
+	res = (buf[(*i)++] & m[ol]) << 8;
+	res = (res | buf[*i]) >> (*l);
+    }
+
+    return res;
+}
+
+static inline u32 getbyte(const u8 *buf, const u32 i, const u32 l)
+{
+    if (l == 8) {
+	return buf[i];
+    } else {
+	return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff;
+    }
+}
+
+static inline void lamecopy(u8 *dst, u8 *src, u32 len)
+{
+    while (len--)
+	*dst++ = *src++;
+}
+
+static int
+mppc_decompress(struct ppp_mppe_state *state, unsigned char *ibuf,
+		unsigned char *obuf, int isize, int osize)
+{
+    u32 olen, off, len, bits, val, sig, i, l;
+    u8 *history, *s;
+
+    history = state->hist + state->histptr;
+    olen = len = i = 0;
+    l = 8;
+    bits = isize * 8;
+    while (bits >= 8) {
+	val = getbyte(ibuf, i++, l);
+	if (val < 0x80) {		/* literal byte < 0x80 */
+	    if (state->histptr < 2*MPPE_HIST_LEN) {
+		/* copy uncompressed byte to the history */
+		(state->hist)[(state->histptr)++] = (u8) val;
+	    } else {
+		/* buffer overflow; drop packet */
+		if (state->debug)
+		    printk(KERN_ERR "%s%d: trying to write outside history "
+			   "buffer\n", __FUNCTION__, state->unit);
+		return DECOMP_ERROR;
+	    }
+	    olen++;
+	    bits -= 8;
+	    continue;
+	}
+
+	sig = val & 0xc0;
+	if (sig == 0x80) {		/* literal byte >= 0x80 */
+	    if (state->histptr < 2*MPPE_HIST_LEN) {
+		/* copy uncompressed byte to the history */
+		(state->hist)[(state->histptr)++] = 
+		    (u8) (0x80|((val&0x3f)<<1)|getbits(ibuf, 1 , &i ,&l));
+	    } else {
+		/* buffer overflow; drop packet */
+		if (state->debug)
+		    printk(KERN_ERR "%s%d: trying to write outside history "
+			   "buffer\n", __FUNCTION__, state->unit);
+		return DECOMP_ERROR;
+	    }
+	    olen++;
+	    bits -= 9;
+	    continue;
+	}
+
+	/* Not a literal byte so it must be an (offset,length) pair */
+	/* decode offset */
+	sig = val & 0xf0;
+	if (sig == 0xf0) {		/* 10-bit offset; 0 <= offset < 64 */
+	    off = (((val&0x0f)<<2)|getbits(ibuf, 2 , &i ,&l));
+	    bits -= 10;
+	} else {
+	    if (sig == 0xe0) {		/* 12-bit offset; 64 <= offset < 320 */
+		off = ((((val&0x0f)<<4)|getbits(ibuf, 4 , &i ,&l))+64);
+		bits -= 12;
+	    } else {
+		if ((sig&0xe0) == 0xc0) {/* 16-bit offset; 320 <= offset < 8192 */
+		    off = ((((val&0x1f)<<8)|getbyte(ibuf, i++, l))+320);
+		    bits -= 16;
+		    if (off > MPPE_HIST_LEN - 1) {
+			if (state->debug)
+			    printk(KERN_DEBUG "%s%d: too big offset value: %d\n",
+				   __FUNCTION__, state->unit, off);
+			return DECOMP_ERROR;
+		    }
+		} else {		/* this shouldn't happen */
+		    if (state->debug)
+			printk(KERN_DEBUG "%s%d: cannot decode offset value\n",
+			       __FUNCTION__, state->unit);
+		    return DECOMP_ERROR;
+		}
+	    }
+	}
+	/* decode length of match */
+	val = getbyte(ibuf, i, l);
+	if ((val & 0x80) == 0x00) {			/* len = 3 */
+	    len = 3;
+	    bits--;
+	    getbits(ibuf, 1 , &i ,&l);
+	} else if ((val & 0xc0) == 0x80) {		/* 4 <= len < 8 */
+	    len = 0x04 | ((val>>4) & 0x03);
+	    bits -= 4;
+	    getbits(ibuf, 4 , &i ,&l);
+	} else if ((val & 0xe0) == 0xc0) {		/* 8 <= len < 16 */
+	    len = 0x08 | ((val>>2) & 0x07);
+	    bits -= 6;
+	    getbits(ibuf, 6 , &i ,&l);
+	} else if ((val & 0xf0) == 0xe0) {		/* 16 <= len < 32 */
+	    len = 0x10 | (val & 0x0f);
+	    bits -= 8;
+	    i++;
+	} else {
+	    bits -= 8;
+	    val = (val << 8) | getbyte(ibuf, ++i, l);
+	    if ((val & 0xf800) == 0xf000) {		/* 32 <= len < 64 */
+		len = 0x0020 | ((val >> 6) & 0x001f);
+		bits -= 2;
+		getbits(ibuf, 2 , &i ,&l);
+	    } else if ((val & 0xfc00) == 0xf800) {	/* 64 <= len < 128 */
+		len = 0x0040 | ((val >> 4) & 0x003f);
+		bits -= 4;
+		getbits(ibuf, 4 , &i ,&l);
+	    } else if ((val & 0xfe00) == 0xfc00) {	/* 128 <= len < 256 */
+		len = 0x0080 | ((val >> 2) & 0x007f);
+		bits -= 6;
+		getbits(ibuf, 6 , &i ,&l);
+	    } else if ((val & 0xff00) == 0xfe00) {	/* 256 <= len < 512 */
+		len = 0x0100 | (val & 0x00ff);
+		bits -= 8;
+		i++;
+	    } else {
+		bits -= 8;
+		val = (val << 8) | getbyte(ibuf, ++i, l);
+		if ((val & 0xff8000) == 0xff0000) {	/* 512 <= len < 1024 */
+		    len = 0x000200 | ((val >> 6) & 0x0001ff);
+		    bits -= 2;
+		    getbits(ibuf, 2 , &i ,&l);
+		} else if ((val & 0xffc000) == 0xff8000) {/* 1024 <= len < 2048 */
+		    len = 0x000400 | ((val >> 4) & 0x0003ff);
+		    bits -= 4;
+		    getbits(ibuf, 4 , &i ,&l);
+		} else if ((val & 0xffe000) == 0xffc000) {/* 2048 <= len < 4096 */
+		    len = 0x000800 | ((val >> 2) & 0x0007ff);
+		    bits -= 6;
+		    getbits(ibuf, 6 , &i ,&l);
+		} else if ((val & 0xfff000) == 0xffe000) {/* 4096 <= len < 8192 */
+		    len = 0x001000 | (val & 0x000fff);
+		    bits -= 8;
+		    i++;
+		} else {				/* this shouldn't happen */
+		    if (state->debug)
+			printk(KERN_DEBUG "%s%d: wrong length code: 0x%X\n",
+			       __FUNCTION__, state->unit, val);
+		    return DECOMP_ERROR;
+		}
+	    }
+	}
+	s = state->hist + state->histptr;
+	state->histptr += len;
+	olen += len;
+	if (state->histptr < 2*MPPE_HIST_LEN) {
+	    /* copy uncompressed bytes to the history */
+
+	    /* In some cases len may be greater than off. It means that memory
+	     * areas pointed by s and s-off overlap. I used to use memmove()
+	     * here, because I thought that it acts as libc's version. But
+	     * I was wrong. I got strange errors sometimes. Wilfried suggested
+	     * using of byte by byte copying here and strange errors disappeared.
+	     */
+	    lamecopy(s, s-off, len);
+	} else {
+	    /* buffer overflow; drop packet */
+	    if (state->debug)
+		printk(KERN_ERR "%s%d: trying to write outside history "
+		       "buffer\n", __FUNCTION__, state->unit);
+	    return DECOMP_ERROR;
+	}
+    }
+
+    if (olen <= osize) {
+	/* copy uncompressed packet to the output buffer */
+	memcpy(obuf, history, olen);
+    } else {
+	/* buffer overflow; drop packet */
+	if (state->debug)
+	    printk(KERN_ERR "%s%d: too big uncompressed packet: %d\n",
+		   __FUNCTION__, state->unit, olen+(PPP_HDRLEN/2));
+	return DECOMP_ERROR;
+    }
+
+    return (int) olen;
+}
+
+int
+mppe_decompress(void *arg, unsigned char *ibuf, int isize,
+		unsigned char *obuf, int osize)
+{
+    struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
+    int seq, bits, uncomplen;
+
+    if (isize <= PPP_HDRLEN + MPPE_OVHD) {
+	if (state->debug) {
+	    printk(KERN_DEBUG "%s%d: short packet (len=%d)\n",  __FUNCTION__,
+		   state->unit, isize);
+	}
+	return DECOMP_ERROR;
+    }
+
+    /* Get coherency counter and control bits from input buffer */
+    seq = MPPE_CCOUNT(ibuf);
+    bits = MPPE_BITS(ibuf);
+
+    if (state->stateless) {
+	/* RFC 3078, sec 8.1. */
+	mppe_increase_ccount(state);
+	if ((seq != state->ccount) && state->debug)
+	    printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
+		   __FUNCTION__, state->unit, seq, state->ccount);
+	while (seq != state->ccount)
+	    mppe_increase_ccount(state);
+    } else {
+	/* RFC 3078, sec 8.2. */
+	if (state->flushexpected) { /* discard state */
+	    if ((bits & MPPE_BIT_FLUSHED)) { /* we received expected FLUSH bit */
+		while (seq != state->ccount)
+		    mppe_increase_ccount(state);
+		state->flushexpected = 0;
+	    } else /* drop packet*/
+		return DECOMP_ERROR;
+	} else { /* normal state */
+	    mppe_increase_ccount(state);
+	    if (seq != state->ccount) {
+		/* Packet loss detected, enter the discard state. */
+		if (state->debug)
+		    printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
+			   __FUNCTION__, state->unit, seq, state->ccount);
+		state->flushexpected = 1;
+		return DECOMP_ERROR;
+	    }
+	}
+	if (state->mppe && (bits & MPPE_BIT_FLUSHED)) {
+	    arcfour_setkey(&state->arcfour_context, state->session_key,
+			   state->keylen);
+	}
+    }
+
+    if (state->mppc && (bits & (MPPE_BIT_FLUSHED | MPPE_BIT_RESET))) {
+	state->histptr = MPPE_HIST_LEN;
+	if ((bits & MPPE_BIT_FLUSHED)) {
+	    memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
+	} else
+	    if ((bits & MPPE_BIT_RESET)) {
+		memcpy(state->hist, state->hist+MPPE_HIST_LEN, MPPE_HIST_LEN);
+	    }
+    }
+
+    /* Fill in the first part of the PPP header. The protocol field
+       comes from the decompressed data. */
+    obuf[0] = PPP_ADDRESS(ibuf);
+    obuf[1] = PPP_CONTROL(ibuf);
+    obuf += PPP_HDRLEN / 2;
+
+    if (state->mppe) { /* process encrypted packet */
+	if ((bits & MPPE_BIT_ENCRYPTED)) {
+	    /* OK, packet encrypted, so decrypt it */
+	    if (state->mppc && (bits & MPPE_BIT_COMP)) {
+		/* Hack warning !!! RC4 implementation which we use does
+		   decryption "in place" - it means that input and output
+		   buffers can be *the same* memory area. Therefore we don't
+		   need to use a temporary buffer. But be careful - other
+		   implementations don't have to be so nice. */
+		arcfour_decrypt(&state->arcfour_context, ibuf+PPP_HDRLEN+MPPE_OVHD,
+				isize-PPP_HDRLEN-MPPE_OVHD, ibuf+PPP_HDRLEN+MPPE_OVHD);
+		uncomplen = mppc_decompress(state, ibuf+PPP_HDRLEN+MPPE_OVHD,
+					    obuf, isize-PPP_HDRLEN-MPPE_OVHD,
+					    osize-(PPP_HDRLEN/2));
+		if (uncomplen == DECOMP_ERROR) {
+		    state->flushexpected = 1;
+		    return DECOMP_ERROR;
+		}
+		uncomplen += PPP_HDRLEN / 2;
+		(state->stats).comp_bytes += isize;
+		(state->stats).comp_packets++;
+	    } else {
+		arcfour_decrypt(&state->arcfour_context, ibuf+PPP_HDRLEN+MPPE_OVHD,
+				isize-PPP_HDRLEN-MPPE_OVHD, obuf);
+		uncomplen = isize - (PPP_HDRLEN / 2) - MPPE_OVHD;
+		(state->stats).inc_bytes += isize;
+		(state->stats).inc_packets++;
+	    }
+	} else { /* this shouldn't happen */
+	    if (state->debug)
+		printk(KERN_ERR "%s%d: encryption negotiated but not an "
+		       "encrypted packet received\n", __FUNCTION__, state->unit);
+	    mppe_change_key(state, 0);
+	    state->flushexpected = 1;
+	    return DECOMP_ERROR;
+	}
+    } else {
+	if (state->mppc) { /* no MPPE, only MPPC */
+	    if ((bits & MPPE_BIT_COMP)) {
+		uncomplen = mppc_decompress(state, ibuf+PPP_HDRLEN+MPPE_OVHD,
+					    obuf, isize-PPP_HDRLEN-MPPE_OVHD,
+					    osize-(PPP_HDRLEN/2));
+		if (uncomplen == DECOMP_ERROR) {
+		    state->flushexpected = 1;
+		    return DECOMP_ERROR;
+		}
+		uncomplen += PPP_HDRLEN / 2;
+		(state->stats).comp_bytes += isize;
+		(state->stats).comp_packets++;
+	    } else {
+		memcpy(obuf, ibuf+PPP_HDRLEN+MPPE_OVHD,
+		       isize-PPP_HDRLEN-MPPE_OVHD);
+		uncomplen = isize - (PPP_HDRLEN / 2) - MPPE_OVHD;
+		(state->stats).inc_bytes += isize;
+		(state->stats).inc_packets++;
+	    }
+	} else { /* this shouldn't happen */
+	    if (state->debug)
+		printk(KERN_ERR "%s%d: error - no MPPC nor MPPE negotiated\n",
+		       __FUNCTION__, state->unit);
+	    state->flushexpected = 1;
+	    return DECOMP_ERROR;
+	}
+    }
+
+    (state->stats).unc_bytes += uncomplen;
+    (state->stats).unc_packets++;
+
+    return uncomplen;
+}
+
+
+/************************************************************
+ * Module interface table
+ ************************************************************/
+
+/* These are in ppp_generic.c */
+extern int  ppp_register_compressor   (struct compressor *cp);
+extern void ppp_unregister_compressor (struct compressor *cp);
+
+/*
+ * Functions exported to ppp_generic.c.
+ */
+struct compressor ppp_mppe = {
+    CI_MPPE,		/* compress_proto */
+    mppe_comp_alloc,	/* comp_alloc */
+    mppe_comp_free,	/* comp_free */
+    mppe_comp_init,	/* comp_init */
+    mppe_comp_reset,	/* comp_reset */
+    mppe_compress,	/* compress */
+    mppe_stats,		/* comp_stat */
+    mppe_decomp_alloc,	/* decomp_alloc */
+    mppe_comp_free,	/* decomp_free */
+    mppe_decomp_init,	/* decomp_init */
+    mppe_decomp_reset,	/* decomp_reset */
+    mppe_decompress,	/* decompress */
+    NULL,		/* incomp */
+    mppe_stats,		/* decomp_stat */
+};
+
+/*
+  In case of MPPC/MPPE there is no need to process incompressible data
+  because such a data is sent in MPPC/MPPE frame. Therefore the (*incomp)
+  callback function isn't needed.
+*/
+
+/************************************************************
+ * Module support routines
+ ************************************************************/
+
+int __init mppe_module_init(void)
+{
+    int answer = ppp_register_compressor(&ppp_mppe);
+    if (answer == 0) {
+	printk(KERN_INFO "MPPE/MPPC encryption/compression module registered\n");
+    }
+    return answer;
+}
+
+void __exit mppe_module_cleanup(void)
+{
+    if (MOD_IN_USE) {
+	printk (KERN_INFO "MPPE/MPPC module busy, removing delayed\n");
+    } else {
+	ppp_unregister_compressor (&ppp_mppe);
+	printk(KERN_INFO "MPPE/MPPC encryption/compression module unregistered\n");
+    }
+}
+
+module_init(mppe_module_init);
+module_exit(mppe_module_cleanup);
+
+MODULE_AUTHOR("Jan Dubiec <jdx@slackware.pl>");
+MODULE_DESCRIPTION("MPPE/MPPC encryption/compression module for Linux");
+MODULE_LICENSE("Dual BSD/GPL");
--- linux-2.4.20/include/linux/ppp-comp.h~150-mppe-mppc-0.98	1999-08-06 13:44:11.000000000 -0400
+++ linux-2.4.20/include/linux/ppp-comp.h	2005-01-07 02:58:08.293132040 -0500
@@ -24,7 +24,7 @@
  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
  * OR MODIFICATIONS.
  *
- * $Id: ppp-comp.h,v 1.6 1997/11/27 06:04:44 paulus Exp $
+ * $Id: ppp-comp.h,v 1.10 2002/12/06 09:49:15 paulus Exp $
  */
 
 /*
@@ -78,7 +78,7 @@
 
 	/* Compress a packet */
 	int     (*compress) (void *state, unsigned char *rptr,
-			      unsigned char *obuf, int isize, int osize);
+			     unsigned char *obuf, int isize, int osize);
 
 	/* Return compression statistics */
 	void	(*comp_stat) (void *state, struct compstat *stats);
@@ -99,7 +99,7 @@
 
 	/* Decompress a packet. */
 	int	(*decompress) (void *state, unsigned char *ibuf, int isize,
-				unsigned char *obuf, int osize);
+			       unsigned char *obuf, int osize);
 
 	/* Update state for an incompressible packet received */
 	void	(*incomp) (void *state, unsigned char *ibuf, int icnt);
@@ -187,6 +187,127 @@
 #define DEFLATE_CHK_SEQUENCE	0
 
 /*
+ * Definitions for MPPE.
+ */
+
+#define CI_MPPE			18	/* config option for MPPE */
+#define CILEN_MPPE		6	/* length of config option */
+
+#define MPPE_PAD		4	/* MPPE growth per frame */
+#define MPPE_MAX_KEY_LEN	16	/* largest key length (128-bit) */
+
+/* option bits for ccp_options.mppe */
+#define MPPE_OPT_40		0x01	/* 40 bit */
+#define MPPE_OPT_128		0x02	/* 128 bit */
+#define MPPE_OPT_STATEFUL	0x04	/* stateful mode */
+/* unsupported opts */
+#define MPPE_OPT_56		0x08	/* 56 bit */
+#define MPPE_OPT_MPPC		0x10	/* MPPC compression */
+#define MPPE_OPT_D		0x20	/* Unknown */
+#define MPPE_OPT_UNSUPPORTED (MPPE_OPT_56|MPPE_OPT_MPPC|MPPE_OPT_D)
+#define MPPE_OPT_UNKNOWN	0x40	/* Bits !defined in RFC 3078 were set */
+
+/*
+ * This is not nice ... the alternative is a bitfield struct though.
+ * And unfortunately, we cannot share the same bits for the option
+ * names above since C and H are the same bit.  We could do a u_int32
+ * but then we have to do a htonl() all the time and/or we still need
+ * to know which octet is which.
+ */
+#define MPPE_C_BIT		0x01	/* MPPC */
+#define MPPE_D_BIT		0x10	/* Obsolete, usage unknown */
+#define MPPE_L_BIT		0x20	/* 40-bit */
+#define MPPE_S_BIT		0x40	/* 128-bit */
+#define MPPE_M_BIT		0x80	/* 56-bit, not supported */
+#define MPPE_H_BIT		0x01	/* Stateless (in a different byte) */
+
+/* Does not include H bit; used for least significant octet only. */
+#define MPPE_ALL_BITS (MPPE_D_BIT|MPPE_L_BIT|MPPE_S_BIT|MPPE_M_BIT|MPPE_H_BIT)
+
+/* Build a CI from mppe opts (see RFC 3078) */
+#define MPPE_OPTS_TO_CI(opts, ci)		\
+    do {					\
+	u_char *ptr = ci; /* u_char[4] */	\
+						\
+	/* H bit */				\
+	if (opts & MPPE_OPT_STATEFUL)		\
+	    *ptr++ = 0x0;			\
+	else					\
+	    *ptr++ = MPPE_H_BIT;		\
+	*ptr++ = 0;				\
+	*ptr++ = 0;				\
+						\
+	/* S,L bits */				\
+	*ptr = 0;				\
+	if (opts & MPPE_OPT_128)		\
+	    *ptr |= MPPE_S_BIT;			\
+	if (opts & MPPE_OPT_40)			\
+	    *ptr |= MPPE_L_BIT;			\
+	/* M,D,C bits not supported */		\
+    } while (/* CONSTCOND */ 0)
+
+/* The reverse of the above */
+#define MPPE_CI_TO_OPTS(ci, opts)		\
+    do {					\
+	u_char *ptr = ci; /* u_char[4] */	\
+						\
+	opts = 0;				\
+						\
+	/* H bit */				\
+	if (!(ptr[0] & MPPE_H_BIT))		\
+	    opts |= MPPE_OPT_STATEFUL;		\
+						\
+	/* S,L bits */				\
+	if (ptr[3] & MPPE_S_BIT)		\
+	    opts |= MPPE_OPT_128;		\
+	if (ptr[3] & MPPE_L_BIT)		\
+	    opts |= MPPE_OPT_40;		\
+						\
+	/* M,D,C bits */			\
+	if (ptr[3] & MPPE_M_BIT)		\
+	    opts |= MPPE_OPT_56;		\
+	if (ptr[3] & MPPE_D_BIT)		\
+	    opts |= MPPE_OPT_D;			\
+	if (ptr[3] & MPPE_C_BIT)		\
+	    opts |= MPPE_OPT_MPPC;		\
+						\
+	/* Other bits */			\
+	if (ptr[0] & ~MPPE_H_BIT)		\
+	    opts |= MPPE_OPT_UNKNOWN;		\
+	if (ptr[1] || ptr[2])			\
+	    opts |= MPPE_OPT_UNKNOWN;		\
+	if (ptr[3] & ~MPPE_ALL_BITS)		\
+	    opts |= MPPE_OPT_UNKNOWN;		\
+    } while (/* CONSTCOND */ 0)
+
+/* MPPE/MPPC definitions by J.D.*/
+#define MPPE_STATELESS          MPPE_H_BIT	/* configuration bit H */
+#define MPPE_40BIT              MPPE_L_BIT	/* configuration bit L */
+#define MPPE_56BIT              MPPE_M_BIT	/* configuration bit M */
+#define MPPE_128BIT             MPPE_S_BIT	/* configuration bit S */
+#define MPPE_MPPC               MPPE_C_BIT	/* configuration bit C */
+
+/*
+ * Definitions for Stac LZS.
+ */
+
+#define CI_LZS			17	/* config option for Stac LZS */
+#define CILEN_LZS		5	/* length of config option */
+
+#define LZS_OVHD		4	/* max. LZS overhead */
+#define LZS_HIST_LEN		2048	/* LZS history size */
+#define LZS_MAX_CCOUNT		0x0FFF	/* max. coherency counter value */
+
+#define LZS_MODE_NONE		0
+#define LZS_MODE_LCB		1
+#define LZS_MODE_CRC		2
+#define LZS_MODE_SEQ		3
+#define LZS_MODE_EXT		4
+
+#define LZS_EXT_BIT_FLUSHED	0x80	/* bit A */
+#define LZS_EXT_BIT_COMP	0x20	/* bit C */
+
+/*
  * Definitions for other, as yet unsupported, compression methods.
  */