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