ppp_mppe.c 21 KB

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