ppp_mppe.c 21 KB

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