ppp_mppe.c 21 KB

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