106-mppe_mppc.patch 53 KB

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