ppp_mppe.c 20 KB

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