ppp_mppe.c 20 KB

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