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