ppp_mppe.c 21 KB

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