ppp_mppe.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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_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_skcipher *arc4;
  90. struct crypto_ahash *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. AHASH_REQUEST_ON_STACK(req, state->sha1);
  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. ahash_request_set_tfm(req, state->sha1);
  136. ahash_request_set_callback(req, 0, NULL, NULL);
  137. ahash_request_set_crypt(req, sg, state->sha1_digest, nbytes);
  138. crypto_ahash_digest(req);
  139. ahash_request_zero(req);
  140. }
  141. /*
  142. * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
  143. * Well, not what's written there, but rather what they meant.
  144. */
  145. static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
  146. {
  147. struct scatterlist sg_in[1], sg_out[1];
  148. SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
  149. skcipher_request_set_tfm(req, state->arc4);
  150. skcipher_request_set_callback(req, 0, NULL, NULL);
  151. get_new_key_from_sha(state);
  152. if (!initial_key) {
  153. crypto_skcipher_setkey(state->arc4, state->sha1_digest,
  154. state->keylen);
  155. sg_init_table(sg_in, 1);
  156. sg_init_table(sg_out, 1);
  157. setup_sg(sg_in, state->sha1_digest, state->keylen);
  158. setup_sg(sg_out, state->session_key, state->keylen);
  159. skcipher_request_set_crypt(req, sg_in, sg_out, state->keylen,
  160. NULL);
  161. if (crypto_skcipher_encrypt(req))
  162. printk(KERN_WARNING "mppe_rekey: cipher_encrypt failed\n");
  163. } else {
  164. memcpy(state->session_key, state->sha1_digest, state->keylen);
  165. }
  166. if (state->keylen == 8) {
  167. /* See RFC 3078 */
  168. state->session_key[0] = 0xd1;
  169. state->session_key[1] = 0x26;
  170. state->session_key[2] = 0x9e;
  171. }
  172. crypto_skcipher_setkey(state->arc4, state->session_key, state->keylen);
  173. skcipher_request_zero(req);
  174. }
  175. /*
  176. * Allocate space for a (de)compressor.
  177. */
  178. static void *mppe_alloc(unsigned char *options, int optlen)
  179. {
  180. struct ppp_mppe_state *state;
  181. unsigned int digestsize;
  182. if (optlen != CILEN_MPPE + sizeof(state->master_key) ||
  183. options[0] != CI_MPPE || options[1] != CILEN_MPPE)
  184. goto out;
  185. state = kzalloc(sizeof(*state), GFP_KERNEL);
  186. if (state == NULL)
  187. goto out;
  188. state->arc4 = crypto_alloc_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
  189. if (IS_ERR(state->arc4)) {
  190. state->arc4 = NULL;
  191. goto out_free;
  192. }
  193. state->sha1 = crypto_alloc_ahash("sha1", 0, CRYPTO_ALG_ASYNC);
  194. if (IS_ERR(state->sha1)) {
  195. state->sha1 = NULL;
  196. goto out_free;
  197. }
  198. digestsize = crypto_ahash_digestsize(state->sha1);
  199. if (digestsize < MPPE_MAX_KEY_LEN)
  200. goto out_free;
  201. state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
  202. if (!state->sha1_digest)
  203. goto out_free;
  204. /* Save keys. */
  205. memcpy(state->master_key, &options[CILEN_MPPE],
  206. sizeof(state->master_key));
  207. memcpy(state->session_key, state->master_key,
  208. sizeof(state->master_key));
  209. /*
  210. * We defer initial key generation until mppe_init(), as mppe_alloc()
  211. * is called frequently during negotiation.
  212. */
  213. return (void *)state;
  214. out_free:
  215. kfree(state->sha1_digest);
  216. crypto_free_ahash(state->sha1);
  217. crypto_free_skcipher(state->arc4);
  218. kfree(state);
  219. out:
  220. return NULL;
  221. }
  222. /*
  223. * Deallocate space for a (de)compressor.
  224. */
  225. static void mppe_free(void *arg)
  226. {
  227. struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
  228. if (state) {
  229. kfree(state->sha1_digest);
  230. crypto_free_ahash(state->sha1);
  231. crypto_free_skcipher(state->arc4);
  232. kfree(state);
  233. }
  234. }
  235. /*
  236. * Initialize (de)compressor state.
  237. */
  238. static int
  239. mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
  240. const char *debugstr)
  241. {
  242. struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
  243. unsigned char mppe_opts;
  244. if (optlen != CILEN_MPPE ||
  245. options[0] != CI_MPPE || options[1] != CILEN_MPPE)
  246. return 0;
  247. MPPE_CI_TO_OPTS(&options[2], mppe_opts);
  248. if (mppe_opts & MPPE_OPT_128)
  249. state->keylen = 16;
  250. else if (mppe_opts & MPPE_OPT_40)
  251. state->keylen = 8;
  252. else {
  253. printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
  254. unit);
  255. return 0;
  256. }
  257. if (mppe_opts & MPPE_OPT_STATEFUL)
  258. state->stateful = 1;
  259. /* Generate the initial session key. */
  260. mppe_rekey(state, 1);
  261. if (debug) {
  262. printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
  263. debugstr, unit, (state->keylen == 16) ? 128 : 40,
  264. (state->stateful) ? "stateful" : "stateless");
  265. printk(KERN_DEBUG
  266. "%s[%d]: keys: master: %*phN initial session: %*phN\n",
  267. debugstr, unit,
  268. (int)sizeof(state->master_key), state->master_key,
  269. (int)sizeof(state->session_key), state->session_key);
  270. }
  271. /*
  272. * Initialize the coherency count. The initial value is not specified
  273. * in RFC 3078, but we can make a reasonable assumption that it will
  274. * start at 0. Setting it to the max here makes the comp/decomp code
  275. * do the right thing (determined through experiment).
  276. */
  277. state->ccount = MPPE_CCOUNT_SPACE - 1;
  278. /*
  279. * Note that even though we have initialized the key table, we don't
  280. * set the FLUSHED bit. This is contrary to RFC 3078, sec. 3.1.
  281. */
  282. state->bits = MPPE_BIT_ENCRYPTED;
  283. state->unit = unit;
  284. state->debug = debug;
  285. return 1;
  286. }
  287. static int
  288. mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
  289. int hdrlen, int debug)
  290. {
  291. /* ARGSUSED */
  292. return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
  293. }
  294. /*
  295. * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
  296. * tell the compressor to rekey. Note that we MUST NOT rekey for
  297. * every CCP Reset-Request; we only rekey on the next xmit packet.
  298. * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
  299. * So, rekeying for every CCP Reset-Request is broken as the peer will not
  300. * know how many times we've rekeyed. (If we rekey and THEN get another
  301. * CCP Reset-Request, we must rekey again.)
  302. */
  303. static void mppe_comp_reset(void *arg)
  304. {
  305. struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
  306. state->bits |= MPPE_BIT_FLUSHED;
  307. }
  308. /*
  309. * Compress (encrypt) a packet.
  310. * It's strange to call this a compressor, since the output is always
  311. * MPPE_OVHD + 2 bytes larger than the input.
  312. */
  313. static int
  314. mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
  315. int isize, int osize)
  316. {
  317. struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
  318. SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
  319. int proto;
  320. int err;
  321. struct scatterlist sg_in[1], sg_out[1];
  322. /*
  323. * Check that the protocol is in the range we handle.
  324. */
  325. proto = PPP_PROTOCOL(ibuf);
  326. if (proto < 0x0021 || proto > 0x00fa)
  327. return 0;
  328. /* Make sure we have enough room to generate an encrypted packet. */
  329. if (osize < isize + MPPE_OVHD + 2) {
  330. /* Drop the packet if we should encrypt it, but can't. */
  331. printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
  332. "(have: %d need: %d)\n", state->unit,
  333. osize, osize + MPPE_OVHD + 2);
  334. return -1;
  335. }
  336. osize = isize + MPPE_OVHD + 2;
  337. /*
  338. * Copy over the PPP header and set control bits.
  339. */
  340. obuf[0] = PPP_ADDRESS(ibuf);
  341. obuf[1] = PPP_CONTROL(ibuf);
  342. put_unaligned_be16(PPP_COMP, obuf + 2);
  343. obuf += PPP_HDRLEN;
  344. state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
  345. if (state->debug >= 7)
  346. printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
  347. state->ccount);
  348. put_unaligned_be16(state->ccount, obuf);
  349. if (!state->stateful || /* stateless mode */
  350. ((state->ccount & 0xff) == 0xff) || /* "flag" packet */
  351. (state->bits & MPPE_BIT_FLUSHED)) { /* CCP Reset-Request */
  352. /* We must rekey */
  353. if (state->debug && state->stateful)
  354. printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
  355. state->unit);
  356. mppe_rekey(state, 0);
  357. state->bits |= MPPE_BIT_FLUSHED;
  358. }
  359. obuf[0] |= state->bits;
  360. state->bits &= ~MPPE_BIT_FLUSHED; /* reset for next xmit */
  361. obuf += MPPE_OVHD;
  362. ibuf += 2; /* skip to proto field */
  363. isize -= 2;
  364. /* Encrypt packet */
  365. sg_init_table(sg_in, 1);
  366. sg_init_table(sg_out, 1);
  367. setup_sg(sg_in, ibuf, isize);
  368. setup_sg(sg_out, obuf, osize);
  369. skcipher_request_set_tfm(req, state->arc4);
  370. skcipher_request_set_callback(req, 0, NULL, NULL);
  371. skcipher_request_set_crypt(req, sg_in, sg_out, isize, NULL);
  372. err = crypto_skcipher_encrypt(req);
  373. skcipher_request_zero(req);
  374. if (err) {
  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. SKCIPHER_REQUEST_ON_STACK(req, state->arc4);
  417. unsigned ccount;
  418. int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
  419. struct scatterlist sg_in[1], sg_out[1];
  420. if (isize <= PPP_HDRLEN + MPPE_OVHD) {
  421. if (state->debug)
  422. printk(KERN_DEBUG
  423. "mppe_decompress[%d]: short pkt (%d)\n",
  424. state->unit, isize);
  425. return DECOMP_ERROR;
  426. }
  427. /*
  428. * Make sure we have enough room to decrypt the packet.
  429. * Note that for our test we only subtract 1 byte whereas in
  430. * mppe_compress() we added 2 bytes (+MPPE_OVHD);
  431. * this is to account for possible PFC.
  432. */
  433. if (osize < isize - MPPE_OVHD - 1) {
  434. printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
  435. "(have: %d need: %d)\n", state->unit,
  436. osize, isize - MPPE_OVHD - 1);
  437. return DECOMP_ERROR;
  438. }
  439. osize = isize - MPPE_OVHD - 2; /* assume no PFC */
  440. ccount = MPPE_CCOUNT(ibuf);
  441. if (state->debug >= 7)
  442. printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
  443. state->unit, ccount);
  444. /* sanity checks -- terminate with extreme prejudice */
  445. if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
  446. printk(KERN_DEBUG
  447. "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
  448. state->unit);
  449. state->sanity_errors += 100;
  450. goto sanity_error;
  451. }
  452. if (!state->stateful && !flushed) {
  453. printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
  454. "stateless mode!\n", state->unit);
  455. state->sanity_errors += 100;
  456. goto sanity_error;
  457. }
  458. if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
  459. printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
  460. "flag packet!\n", state->unit);
  461. state->sanity_errors += 100;
  462. goto sanity_error;
  463. }
  464. /*
  465. * Check the coherency count.
  466. */
  467. if (!state->stateful) {
  468. /* Discard late packet */
  469. if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE
  470. > MPPE_CCOUNT_SPACE / 2) {
  471. state->sanity_errors++;
  472. goto sanity_error;
  473. }
  474. /* RFC 3078, sec 8.1. Rekey for every packet. */
  475. while (state->ccount != ccount) {
  476. mppe_rekey(state, 0);
  477. state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
  478. }
  479. } else {
  480. /* RFC 3078, sec 8.2. */
  481. if (!state->discard) {
  482. /* normal state */
  483. state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
  484. if (ccount != state->ccount) {
  485. /*
  486. * (ccount > state->ccount)
  487. * Packet loss detected, enter the discard state.
  488. * Signal the peer to rekey (by sending a CCP Reset-Request).
  489. */
  490. state->discard = 1;
  491. return DECOMP_ERROR;
  492. }
  493. } else {
  494. /* discard state */
  495. if (!flushed) {
  496. /* ccp.c will be silent (no additional CCP Reset-Requests). */
  497. return DECOMP_ERROR;
  498. } else {
  499. /* Rekey for every missed "flag" packet. */
  500. while ((ccount & ~0xff) !=
  501. (state->ccount & ~0xff)) {
  502. mppe_rekey(state, 0);
  503. state->ccount =
  504. (state->ccount +
  505. 256) % MPPE_CCOUNT_SPACE;
  506. }
  507. /* reset */
  508. state->discard = 0;
  509. state->ccount = ccount;
  510. /*
  511. * Another problem with RFC 3078 here. It implies that the
  512. * peer need not send a Reset-Ack packet. But RFC 1962
  513. * requires it. Hopefully, M$ does send a Reset-Ack; even
  514. * though it isn't required for MPPE synchronization, it is
  515. * required to reset CCP state.
  516. */
  517. }
  518. }
  519. if (flushed)
  520. mppe_rekey(state, 0);
  521. }
  522. /*
  523. * Fill in the first part of the PPP header. The protocol field
  524. * comes from the decrypted data.
  525. */
  526. obuf[0] = PPP_ADDRESS(ibuf); /* +1 */
  527. obuf[1] = PPP_CONTROL(ibuf); /* +1 */
  528. obuf += 2;
  529. ibuf += PPP_HDRLEN + MPPE_OVHD;
  530. isize -= PPP_HDRLEN + MPPE_OVHD; /* -6 */
  531. /* net osize: isize-4 */
  532. /*
  533. * Decrypt the first byte in order to check if it is
  534. * a compressed or uncompressed protocol field.
  535. */
  536. sg_init_table(sg_in, 1);
  537. sg_init_table(sg_out, 1);
  538. setup_sg(sg_in, ibuf, 1);
  539. setup_sg(sg_out, obuf, 1);
  540. skcipher_request_set_tfm(req, state->arc4);
  541. skcipher_request_set_callback(req, 0, NULL, NULL);
  542. skcipher_request_set_crypt(req, sg_in, sg_out, 1, NULL);
  543. if (crypto_skcipher_decrypt(req)) {
  544. printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
  545. osize = DECOMP_ERROR;
  546. goto out_zap_req;
  547. }
  548. /*
  549. * Do PFC decompression.
  550. * This would be nicer if we were given the actual sk_buff
  551. * instead of a char *.
  552. */
  553. if ((obuf[0] & 0x01) != 0) {
  554. obuf[1] = obuf[0];
  555. obuf[0] = 0;
  556. obuf++;
  557. osize++;
  558. }
  559. /* And finally, decrypt the rest of the packet. */
  560. setup_sg(sg_in, ibuf + 1, isize - 1);
  561. setup_sg(sg_out, obuf + 1, osize - 1);
  562. skcipher_request_set_crypt(req, sg_in, sg_out, isize - 1, NULL);
  563. if (crypto_skcipher_decrypt(req)) {
  564. printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
  565. osize = DECOMP_ERROR;
  566. goto out_zap_req;
  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. out_zap_req:
  575. skcipher_request_zero(req);
  576. return osize;
  577. sanity_error:
  578. if (state->sanity_errors < SANITY_MAX)
  579. return DECOMP_ERROR;
  580. else
  581. /* Take LCP down if the peer is sending too many bogons.
  582. * We don't want to do this for a single or just a few
  583. * instances since it could just be due to packet corruption.
  584. */
  585. return DECOMP_FATALERROR;
  586. }
  587. /*
  588. * Incompressible data has arrived (this should never happen!).
  589. * We should probably drop the link if the protocol is in the range
  590. * of what should be encrypted. At the least, we should drop this
  591. * packet. (How to do this?)
  592. */
  593. static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
  594. {
  595. struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
  596. if (state->debug &&
  597. (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
  598. printk(KERN_DEBUG
  599. "mppe_incomp[%d]: incompressible (unencrypted) data! "
  600. "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
  601. state->stats.inc_bytes += icnt;
  602. state->stats.inc_packets++;
  603. state->stats.unc_bytes += icnt;
  604. state->stats.unc_packets++;
  605. }
  606. /*************************************************************
  607. * Module interface table
  608. *************************************************************/
  609. /*
  610. * Procedures exported to if_ppp.c.
  611. */
  612. static struct compressor ppp_mppe = {
  613. .compress_proto = CI_MPPE,
  614. .comp_alloc = mppe_alloc,
  615. .comp_free = mppe_free,
  616. .comp_init = mppe_comp_init,
  617. .comp_reset = mppe_comp_reset,
  618. .compress = mppe_compress,
  619. .comp_stat = mppe_comp_stats,
  620. .decomp_alloc = mppe_alloc,
  621. .decomp_free = mppe_free,
  622. .decomp_init = mppe_decomp_init,
  623. .decomp_reset = mppe_decomp_reset,
  624. .decompress = mppe_decompress,
  625. .incomp = mppe_incomp,
  626. .decomp_stat = mppe_comp_stats,
  627. .owner = THIS_MODULE,
  628. .comp_extra = MPPE_PAD,
  629. };
  630. /*
  631. * ppp_mppe_init()
  632. *
  633. * Prior to allowing load, try to load the arc4 and sha1 crypto
  634. * libraries. The actual use will be allocated later, but
  635. * this way the module will fail to insmod if they aren't available.
  636. */
  637. static int __init ppp_mppe_init(void)
  638. {
  639. int answer;
  640. if (!(crypto_has_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC) &&
  641. crypto_has_ahash("sha1", 0, CRYPTO_ALG_ASYNC)))
  642. return -ENODEV;
  643. sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
  644. if (!sha_pad)
  645. return -ENOMEM;
  646. sha_pad_init(sha_pad);
  647. answer = ppp_register_compressor(&ppp_mppe);
  648. if (answer == 0)
  649. printk(KERN_INFO "PPP MPPE Compression module registered\n");
  650. else
  651. kfree(sha_pad);
  652. return answer;
  653. }
  654. static void __exit ppp_mppe_cleanup(void)
  655. {
  656. ppp_unregister_compressor(&ppp_mppe);
  657. kfree(sha_pad);
  658. }
  659. module_init(ppp_mppe_init);
  660. module_exit(ppp_mppe_cleanup);