1/* 2 * ppp_mppe_mppc_comp.c - MPPC/MPPE "compressor/decompressor" module. 3 * 4 * Copyright (c) 1994 �rp�d Magos�nyi <mag@bunuel.tii.matav.hu> 5 * Copyright (c) 1999 Tim Hockin, Cobalt Networks Inc. <thockin@cobaltnet.com> 6 * Copyright (c) 2002, 2003 Jan Dubiec <jdx@slackware.pl> 7 * 8 * Permission to use, copy, modify, and distribute this software and its 9 * documentation is hereby granted, provided that the above copyright 10 * notice appears in all copies. This software is provided without any 11 * warranty, express or implied. 12 * 13 * The code is based on MPPE kernel module written by �rp�d Magos�nyi and 14 * Tim Hockin which can be found on http://planetmirror.com/pub/mppe/. 15 * I have added MPPC and 56 bit session keys support in MPPE. 16 * 17 * WARNING! Although this is open source code, its usage in some countries 18 * (in particular in the USA) may violate Stac Inc. patent for MPPC. 19 * 20 * Compile command: 21 * gcc -O2 -Wall -I/usr/src/linux/include -D__KERNEL__ -DMODULE -c ppp_mppe_mppc_comp.c 22 * 23 * ==FILEVERSION 20030807== 24 * 25 */ 26 27#include <linux/module.h> 28#include <linux/slab.h> 29#include <linux/vmalloc.h> 30#include <linux/init.h> 31 32#include <linux/ppp_defs.h> 33#include <linux/ppp-comp.h> 34 35#include "ppp_mppe_crypto.h" 36 37/* 38 * State for a mppc/mppe "(de)compressor". 39 */ 40struct ppp_mppe_state { 41 arcfour_context arcfour_context; 42 u8 master_key[MPPE_MAX_KEY_LEN]; 43 u8 session_key[MPPE_MAX_KEY_LEN]; 44 u8 mppc; /* do we use compression (MPPC)? */ 45 u8 mppe; /* do we use encryption (MPPE)? */ 46 u8 keylen; /* key length in bytes */ 47 u8 bitkeylen; /* key length in bits */ 48 u16 ccount; /* coherency counter */ 49 u16 bits; /* MPPC/MPPE control bits */ 50 u8 stateless; /* do we use stateless mode? */ 51 u8 nextflushed; /* set A bit in the next outgoing packet; 52 used only by compressor*/ 53 u8 flushexpected; /* drop packets until A bit is received; 54 used only by decompressor*/ 55 u8 *hist; /* MPPC history */ 56 u16 *hash; /* Hash table; used only by compressor */ 57 u16 histptr; /* history "cursor" */ 58 int unit; 59 int debug; 60 int mru; 61 struct compstat stats; 62}; 63 64#define MPPE_OVHD 2 /* MPPE overhead */ 65#define MPPE_HIST_LEN 8192 /* MPPC history size */ 66#define MPPE_MAX_CCOUNT 0x0FFF /* max. coherency counter value */ 67 68#define MPPE_BIT_FLUSHED 0x80 /* bit A */ 69#define MPPE_BIT_RESET 0x40 /* bit B */ 70#define MPPE_BIT_COMP 0x20 /* bit C */ 71#define MPPE_BIT_ENCRYPTED 0x10 /* bit D */ 72 73#define MPPE_SALT0 0xD1 /* values used in MPPE key derivation */ 74#define MPPE_SALT1 0x26 /* according to RFC3079 */ 75#define MPPE_SALT2 0x9E 76 77#define MPPE_CCOUNT(x) ((((x)[4] & 0x0f) << 8) + (x)[5]) 78#define MPPE_BITS(x) ((x)[4] & 0xf0) 79#define MPPE_CTRLHI(x) ((((x)->ccount & 0xf00)>>8)|((x)->bits)) 80#define MPPE_CTRLLO(x) ((x)->ccount & 0xff) 81 82/* 83 * Key Derivation, from RFC 3078, RFC 3079. 84 * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079. 85 */ 86static void 87GetNewKeyFromSHA(unsigned char *MasterKey, unsigned char *SessionKey, 88 unsigned long SessionKeyLength, unsigned char *InterimKey) 89{ 90 /*Pads used in key derivation */ 91 static unsigned char SHAPad1[40] = 92 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 94 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 96 97 static unsigned char SHAPad2[40] = 98 { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 99 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 100 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 101 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2 }; 102 103 SHA1_CTX Context; 104 unsigned char Digest[SHA1_SIGNATURE_SIZE]; 105 106 SHA1_Init(&Context); 107 SHA1_Update(&Context, MasterKey, SessionKeyLength); 108 SHA1_Update(&Context, SHAPad1, sizeof(SHAPad1)); 109 SHA1_Update(&Context, SessionKey, SessionKeyLength); 110 SHA1_Update(&Context, SHAPad2, sizeof(SHAPad2)); 111 SHA1_Final(Digest,&Context); 112 memcpy(InterimKey, Digest, SessionKeyLength); 113} 114 115static void 116mppe_change_key(struct ppp_mppe_state *state, int initialize) 117{ 118 unsigned char InterimKey[MPPE_MAX_KEY_LEN]; 119 120 GetNewKeyFromSHA(state->master_key, state->session_key, 121 state->keylen, InterimKey); 122 if (initialize) { 123 memcpy(state->session_key, InterimKey, state->keylen); 124 } else { 125 arcfour_setkey(&state->arcfour_context, InterimKey, state->keylen); 126 arcfour_encrypt(&state->arcfour_context, InterimKey, state->keylen, 127 state->session_key); 128 } 129 if (state->keylen == 8) { 130 if (state->bitkeylen == 40) { 131 state->session_key[0] = MPPE_SALT0; 132 state->session_key[1] = MPPE_SALT1; 133 state->session_key[2] = MPPE_SALT2; 134 } else { 135 state->session_key[0] = MPPE_SALT0; 136 } 137 } 138 arcfour_setkey(&state->arcfour_context, state->session_key, state->keylen); 139} 140 141/* increase 12-bit coherency counter */ 142static inline void 143mppe_increase_ccount(struct ppp_mppe_state *state) 144{ 145 state->ccount = (state->ccount + 1) & MPPE_MAX_CCOUNT; 146 if (state->mppe) { 147 if (state->stateless) { 148 mppe_change_key(state, 0); 149 state->nextflushed = 1; 150 } else { 151 if ((state->ccount & 0xff) == 0xff) { 152 mppe_change_key(state, 0); 153 state->nextflushed = 1; 154 } 155 } 156 } 157} 158 159/* allocate space for a MPPE/MPPC (de)compressor. */ 160/* comp != 0 -> init compressor */ 161/* comp = 0 -> init decompressor */ 162static void * 163mppe_alloc(unsigned char *options, int opt_len, int comp) 164{ 165 struct ppp_mppe_state *state; 166 u8* fname; 167 168 fname = comp ? "mppe_comp_alloc" : "mppe_decomp_alloc"; 169 170 /* 171 Hack warning - additionally to the standard MPPC/MPPE configuration 172 options, pppd passes to the (de)copressor 8 or 16 byte session key. 173 Therefore options[1] contains MPPC/MPPE configuration option length 174 (CILEN_MPPE = 6), but the real options length, depending on the key 175 length, is 6+8 or 6+16. 176 */ 177 if (opt_len < CILEN_MPPE) { 178 printk(KERN_WARNING "%s: wrong options length: %u\n", fname, opt_len); 179 return NULL; 180 } 181 182 if (options[0] != CI_MPPE || options[1] != CILEN_MPPE || 183 (options[2] & ~MPPE_STATELESS) != 0 || 184 options[3] != 0 || options[4] != 0 || 185 (options[5] & ~(MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) != 0 || 186 (options[5] & (MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) == 0) { 187 printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, " 188 "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname, options[0], 189 options[1], options[2], options[3], options[4], options[5]); 190 return NULL; 191 } 192 193 state = (struct ppp_mppe_state *)kmalloc(sizeof(*state), GFP_KERNEL); 194 if (state == NULL) { 195 printk(KERN_ERR "%s: cannot allocate space for %scompressor\n", fname, 196 comp ? "" : "de"); 197 return NULL; 198 } 199 memset(state, 0, sizeof(struct ppp_mppe_state)); 200 201 state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */ 202 state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT | 203 MPPE_40BIT); /* Do we use MPPE? */ 204 205 if (state->mppc) { 206 /* allocate MPPC history */ 207 state->hist = (u8*)vmalloc(2*MPPE_HIST_LEN*sizeof(u8)); 208 if (state->hist == NULL) { 209 kfree(state); 210 printk(KERN_ERR "%s: cannot allocate space for MPPC history\n", 211 fname); 212 return NULL; 213 } 214 215 /* allocate hashtable for MPPC compressor */ 216 if (comp) { 217 state->hash = (u16*)vmalloc(MPPE_HIST_LEN*sizeof(u16)); 218 if (state->hash == NULL) { 219 vfree(state->hist); 220 kfree(state); 221 printk(KERN_ERR "%s: cannot allocate space for MPPC history\n", 222 fname); 223 return NULL; 224 } 225 } 226 } 227 228 MOD_INC_USE_COUNT; 229 230 if (state->mppe) { /* specific for MPPE */ 231 memcpy(state->master_key, options+CILEN_MPPE, MPPE_MAX_KEY_LEN); 232 memcpy(state->session_key, state->master_key, MPPE_MAX_KEY_LEN); 233 /* initial key generation is done in mppe_init() */ 234 } 235 236 return (void *) state; 237} 238 239static void * 240mppe_comp_alloc(unsigned char *options, int opt_len) 241{ 242 return mppe_alloc(options, opt_len, 1); 243} 244 245static void * 246mppe_decomp_alloc(unsigned char *options, int opt_len) 247{ 248 return mppe_alloc(options, opt_len, 0); 249} 250 251/* cleanup the (de)compressor */ 252static void 253mppe_comp_free(void *arg) 254{ 255 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg; 256 257 if (state != NULL) { 258 if (state->hist != NULL) 259 vfree(state->hist); 260 if (state->hash != NULL) 261 vfree(state->hash); 262 kfree(state); 263 } 264 MOD_DEC_USE_COUNT; 265} 266 267/* init MPPC/MPPE (de)compresor */ 268/* comp != 0 -> init compressor */ 269/* comp = 0 -> init decompressor */ 270static int 271mppe_init(void *arg, unsigned char *options, int opt_len, int unit, 272 int hdrlen, int mru, int debug, int comp) 273{ 274 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg; 275 u8* fname; 276 277 fname = comp ? "mppe_comp_init" : "mppe_decomp_init"; 278 279 if (opt_len < CILEN_MPPE) { 280 if (debug) 281 printk(KERN_WARNING "%s: wrong options length: %u\n", 282 fname, opt_len); 283 return 0; 284 } 285 286 if (options[0] != CI_MPPE || options[1] != CILEN_MPPE || 287 (options[2] & ~MPPE_STATELESS) != 0 || 288 options[3] != 0 || options[4] != 0 || 289 (options[5] & ~(MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) != 0 || 290 (options[5] & (MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) == 0) { 291 if (debug) 292 printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, " 293 "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname, 294 options[0], options[1], options[2], options[3], options[4], 295 options[5]); 296 return 0; 297 } 298 299 if ((options[5] & ~MPPE_MPPC) != MPPE_128BIT && 300 (options[5] & ~MPPE_MPPC) != MPPE_56BIT && 301 (options[5] & ~MPPE_MPPC) != MPPE_40BIT && 302 (options[5] & MPPE_MPPC) != MPPE_MPPC) { 303 if (debug) 304 printk(KERN_WARNING "%s: don't know what to do: o[5]=%02x\n", 305 fname, options[5]); 306 return 0; 307 } 308 309 state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */ 310 state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT | 311 MPPE_40BIT); /* Do we use MPPE? */ 312 state->stateless = options[2] & MPPE_STATELESS; /* Do we use stateless mode? */ 313 314 switch (state->mppe) { 315 case MPPE_40BIT: /* 40 bit key */ 316 state->keylen = 8; 317 state->bitkeylen = 40; 318 break; 319 case MPPE_56BIT: /* 56 bit key */ 320 state->keylen = 8; 321 state->bitkeylen = 56; 322 break; 323 case MPPE_128BIT: /* 128 bit key */ 324 state->keylen = 16; 325 state->bitkeylen = 128; 326 break; 327 default: 328 state->keylen = 0; 329 state->bitkeylen = 0; 330 } 331 332 state->ccount = MPPE_MAX_CCOUNT; 333 state->bits = 0; 334 state->unit = unit; 335 state->debug = debug; 336 state->histptr = MPPE_HIST_LEN; 337 if (state->mppc) { /* reset history if MPPC was negotiated */ 338 memset(state->hist, 0, 2*MPPE_HIST_LEN*sizeof(u8)); 339 } 340 341 if (state->mppe) { /* generate initial session keys */ 342 mppe_change_key(state, 1); 343 } 344 345 if (comp) { /* specific for compressor */ 346 state->nextflushed = 1; 347 } else { /* specific for decompressor */ 348 state->mru = mru; 349 state->flushexpected = 1; 350 } 351 352 return 1; 353} 354 355static int 356mppe_comp_init(void *arg, unsigned char *options, int opt_len, int unit, 357 int hdrlen, int debug) 358{ 359 return mppe_init(arg, options, opt_len, unit, hdrlen, 0, debug, 1); 360} 361 362 363static int 364mppe_decomp_init(void *arg, unsigned char *options, int opt_len, int unit, 365 int hdrlen, int mru, int debug) 366{ 367 return mppe_init(arg, options, opt_len, unit, hdrlen, mru, debug, 0); 368} 369 370static void 371mppe_comp_reset(void *arg) 372{ 373 struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg; 374 375 if (state->debug) 376 printk(KERN_DEBUG "%s%d: resetting MPPC/MPPE compressor\n", 377 __FUNCTION__, state->unit); 378 379 state->nextflushed = 1; 380 if (state->mppe) 381 arcfour_setkey(&state->arcfour_context, state->session_key, 382 state->keylen); 383} 384 385static void 386mppe_decomp_reset(void *arg) 387{ 388 /* When MPPC/MPPE is in use, we shouldn't receive any CCP Reset-Ack. 389 But when we receive such a packet, we just ignore it. */ 390 return; 391} 392 393static void 394mppe_stats(void *arg, struct compstat *stats) 395{ 396 struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg; 397 398 *stats = state->stats; 399} 400 401/***************************/ 402/**** Compression stuff ****/ 403/***************************/ 404/* inserts 1 to max. 8 bits into the output buffer */ 405static inline void putbits8(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l) 406{ 407 buf += *i; 408 if (*l >= n) { 409 *l = (*l) - n; 410 val <<= *l; 411 *buf = *buf | (val & 0xff); 412 if (*l == 0) { 413 *l = 8; 414 (*i)++; 415 *(++buf) = 0; 416 } 417 } else { 418 (*i)++; 419 *l = 8 - n + (*l); 420 val <<= *l; 421 *buf = *buf | ((val >> 8) & 0xff); 422 *(++buf) = val & 0xff; 423 } 424} 425 426/* inserts 9 to max. 16 bits into the output buffer */ 427static inline void putbits16(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l) 428{ 429 buf += *i; 430 if (*l >= n - 8) { 431 (*i)++; 432 *l = 8 - n + (*l); 433 val <<= *l; 434 *buf = *buf | ((val >> 8) & 0xff); 435 *(++buf) = val & 0xff; 436 if (*l == 0) { 437 *l = 8; 438 (*i)++; 439 *(++buf) = 0; 440 } 441 } else { 442 (*i)++; (*i)++; 443 *l = 16 - n + (*l); 444 val <<= *l; 445 *buf = *buf | ((val >> 16) & 0xff); 446 *(++buf) = (val >> 8) & 0xff; 447 *(++buf) = val & 0xff; 448 } 449} 450 451/* inserts 17 to max. 24 bits into the output buffer */ 452static inline void putbits24(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l) 453{ 454 buf += *i; 455 if (*l >= n - 16) { 456 (*i)++; (*i)++; 457 *l = 16 - n + (*l); 458 val <<= *l; 459 *buf = *buf | ((val >> 16) & 0xff); 460 *(++buf) = (val >> 8) & 0xff; 461 *(++buf) = val & 0xff; 462 if (*l == 0) { 463 *l = 8; 464 (*i)++; 465 *(++buf) = 0; 466 } 467 } else { 468 (*i)++; (*i)++; (*i)++; 469 *l = 24 - n + (*l); 470 val <<= *l; 471 *buf = *buf | ((val >> 24) & 0xff); 472 *(++buf) = (val >> 16) & 0xff; 473 *(++buf) = (val >> 8) & 0xff; 474 *(++buf) = val & 0xff; 475 } 476} 477 478static int 479mppc_compress(struct ppp_mppe_state *state, unsigned char *ibuf, 480 unsigned char *obuf, int isize, int osize) 481{ 482 u32 olen, off, len, idx, i, l; 483 u8 *hist, *sbuf, *p, *q, *r, *s; 484 485 /* 486 At this point, to avoid possible buffer overflow caused by packet 487 expansion during/after compression, we should make sure that 488 osize >= (((isize*9)/8)+1)+2, but we don't do that because in 489 ppp_generic.c we just allocate bigger obuf. 490 491 Maximum MPPC packet expansion is 12.5%. This is the worst case when 492 all octets in the input buffer are >= 0x80 and we cannot find any 493 repeated tokens. Additionally we have to reserve 2 bytes for MPPE/MPPC 494 status bits and coherency counter. 495 */ 496 497 hist = state->hist + MPPE_HIST_LEN; 498 /* check if there is enough room at the end of the history */ 499 if (state->histptr + isize >= 2*MPPE_HIST_LEN) { 500 state->bits |= MPPE_BIT_RESET; 501 state->histptr = MPPE_HIST_LEN; 502 memcpy(state->hist, hist, MPPE_HIST_LEN); 503 } 504 /* add packet to the history; isize must be <= MPPE_HIST_LEN */ 505 sbuf = state->hist + state->histptr; 506 memcpy(sbuf, ibuf, isize); 507 state->histptr += isize; 508 509 /* compress data */ 510 r = sbuf + isize; 511 *obuf = olen = i = 0; 512 l = 8; 513 while (i < isize - 2) { 514 s = q = sbuf + i; 515 idx = ((40542*((((s[0]<<4)^s[1])<<4)^s[2]))>>4) & 0x1FFF; 516 p = hist + state->hash[idx]; 517 state->hash[idx] = (u16) (s - hist); 518 off = s - p; 519 if (off > 8191 || off < 1 || *p++ != *s++ || *p++ != *s++ || *p++ != *s++) 520 goto literal; 521 if (r - q >= 64) { 522 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 523 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 524 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 525 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 526 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 527 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 528 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 529 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 530 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 531 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 532 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 533 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 534 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 535 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 536 *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || 537 *p++ != *s++; 538 if (s - q == 64) { 539 p--; s--; 540 while((*p++ == *s++) && (s < r)); 541 } 542 } else { 543 while((*p++ == *s++) && (s < r)); 544 } 545 len = s - q - 1; 546 i += len; 547 548 /* at least 3 character match found; code data */ 549 /* encode offset */ 550 if (off < 64) { /* 10-bit offset; 0 <= offset < 64 */ 551 putbits16(obuf, 0x3c0|off, 10, &olen, &l); 552 } else if (off < 320) { /* 12-bit offset; 64 <= offset < 320 */ 553 putbits16(obuf, 0xe00|(off-64), 12, &olen, &l); 554 } else if (off < 8192) { /* 16-bit offset; 320 <= offset < 8192 */ 555 putbits16(obuf, 0xc000|(off-320), 16, &olen, &l); 556 } else { 557 /* This shouldn't happen; we return 0 what means "packet expands", 558 and we send packet uncompressed. */ 559 if (state->debug) 560 printk(KERN_DEBUG "%s%d: wrong offset value: %d\n", 561 __FUNCTION__, state->unit, off); 562 return 0; 563 } 564 /* encode length of match */ 565 if (len < 4) { /* length = 3 */ 566 putbits8(obuf, 0, 1, &olen, &l); 567 } else if (len < 8) { /* 4 <= length < 8 */ 568 putbits8(obuf, 0x08|(len&0x03), 4, &olen, &l); 569 } else if (len < 16) { /* 8 <= length < 16 */ 570 putbits8(obuf, 0x30|(len&0x07), 6, &olen, &l); 571 } else if (len < 32) { /* 16 <= length < 32 */ 572 putbits8(obuf, 0xe0|(len&0x0f), 8, &olen, &l); 573 } else if (len < 64) { /* 32 <= length < 64 */ 574 putbits16(obuf, 0x3c0|(len&0x1f), 10, &olen, &l); 575 } else if (len < 128) { /* 64 <= length < 128 */ 576 putbits16(obuf, 0xf80|(len&0x3f), 12, &olen, &l); 577 } else if (len < 256) { /* 128 <= length < 256 */ 578 putbits16(obuf, 0x3f00|(len&0x7f), 14, &olen, &l); 579 } else if (len < 512) { /* 256 <= length < 512 */ 580 putbits16(obuf, 0xfe00|(len&0xff), 16, &olen, &l); 581 } else if (len < 1024) { /* 512 <= length < 1024 */ 582 putbits24(obuf, 0x3fc00|(len&0x1ff), 18, &olen, &l); 583 } else if (len < 2048) { /* 1024 <= length < 2048 */ 584 putbits24(obuf, 0xff800|(len&0x3ff), 20, &olen, &l); 585 } else if (len < 4096) { /* 2048 <= length < 4096 */ 586 putbits24(obuf, 0x3ff000|(len&0x7ff), 22, &olen, &l); 587 } else if (len < 8192) { /* 4096 <= length < 8192 */ 588 putbits24(obuf, 0xffe000|(len&0xfff), 24, &olen, &l); 589 } else { 590 /* This shouldn't happen; we return 0 what means "packet expands", 591 and send packet uncompressed. */ 592 if (state->debug) 593 printk(KERN_DEBUG "%s%d: wrong length of match value: %d\n", 594 __FUNCTION__, state->unit, len); 595 return 0; 596 } 597 continue; 598 599 literal: 600 /* no match found; encode literal byte */ 601 if (ibuf[i] < 0x80) { /* literal byte < 0x80 */ 602 putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l); 603 } else { /* literal byte >= 0x80 */ 604 putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l); 605 } 606 } 607 608 /* Add remaining octets to the output */ 609 while(isize - i > 0) { 610 if (ibuf[i] < 0x80) { /* literal byte < 0x80 */ 611 putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l); 612 } else { /* literal byte >= 0x80 */ 613 putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l); 614 } 615 } 616 /* Reset unused bits of the last output octet */ 617 if ((l != 0) && (l != 8)) { 618 putbits8(obuf, 0, l, &olen, &l); 619 } 620 621 return (int) olen; 622} 623 624int 625mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf, 626 int isize, int osize) 627{ 628 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg; 629 int proto, olen, complen; 630 unsigned char *wptr; 631 632 /* Check that the protocol is in the range we handle. */ 633 proto = PPP_PROTOCOL(ibuf); 634 if (proto < 0x0021 || proto > 0x00fa) 635 return 0; 636 637 wptr = obuf; 638 /* Copy over the PPP header */ 639 wptr[0] = PPP_ADDRESS(ibuf); 640 wptr[1] = PPP_CONTROL(ibuf); 641 wptr[2] = PPP_COMP >> 8; 642 wptr[3] = PPP_COMP; 643 wptr += PPP_HDRLEN + MPPE_OVHD; /* Leave two octets for MPPE/MPPC bits */ 644 645 mppe_increase_ccount(state); 646 647 if (state->nextflushed) { 648 if (!state->stateless) { 649 state->nextflushed = 0; 650 } 651 state->bits |= MPPE_BIT_FLUSHED; 652 if (state->mppc) { /* reset history */ 653 state->bits |= MPPE_BIT_RESET; 654 state->histptr = MPPE_HIST_LEN; 655 memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8)); 656 } 657 } 658 659 if (state->mppc && !state->mppe) { /* Do only compression */ 660 complen = mppc_compress(state, ibuf+2, wptr, isize-2, 661 osize-PPP_HDRLEN-MPPE_OVHD); 662 if ((complen > isize) || (complen == 0)) { 663 /* packet expands */ 664 state->nextflushed = 1; 665 memcpy(wptr, ibuf+2, isize-2); 666 olen = isize + (PPP_HDRLEN / 2) + MPPE_OVHD; 667 (state->stats).inc_bytes += olen; 668 (state->stats).inc_packets++; 669 } else { 670 state->bits |= MPPE_BIT_COMP; 671 olen = complen + PPP_HDRLEN + MPPE_OVHD; 672 (state->stats).comp_bytes += olen; 673 (state->stats).comp_packets++; 674 } 675 } else { 676 if (!state->mppc && state->mppe) { /* Do only encryption */ 677 /* read from ibuf, write to wptr, adjust for PPP_HDRLEN */ 678 arcfour_encrypt(&state->arcfour_context, ibuf+2, isize-2, wptr); 679 state->bits |= MPPE_BIT_ENCRYPTED; 680 olen = isize + (PPP_HDRLEN / 2) + MPPE_OVHD; 681 (state->stats).inc_bytes += olen; 682 (state->stats).inc_packets++; 683 } else { /* Do compression and then encryption - RFC3078 */ 684 complen = mppc_compress(state, ibuf+2, wptr, isize-2, 685 osize-PPP_HDRLEN-MPPE_OVHD); 686 if ((complen > isize) || (complen == 0)) { 687 /* packet expands */ 688 memcpy(wptr, ibuf+2, isize-2); 689 state->nextflushed = 1; 690 arcfour_encrypt(&state->arcfour_context, ibuf+2, isize-2, wptr); 691 olen = isize + (PPP_HDRLEN / 2) + MPPE_OVHD; 692 (state->stats).inc_bytes += olen; 693 (state->stats).inc_packets++; 694 } else { 695 state->bits |= MPPE_BIT_COMP; 696 /* Hack warning !!! RC4 implementation which we use does 697 encryption "in place" - it means that input and output 698 buffers can be *the same* memory area. Therefore we don't 699 need to use a temporary buffer. But be careful - other 700 implementations don't have to be so nice. 701 I used to use ibuf as temporary buffer here, but it led 702 packet sniffers into error. Thanks to Wilfried Weissmann 703 for pointing that. */ 704 arcfour_encrypt(&state->arcfour_context, wptr, complen, wptr); 705 olen = complen + PPP_HDRLEN + MPPE_OVHD; 706 (state->stats).comp_bytes += olen; 707 (state->stats).comp_packets++; 708 } 709 state->bits |= MPPE_BIT_ENCRYPTED; 710 } 711 } 712 713 /* write status bits and coherency counter into the output buffer */ 714 wptr = obuf + PPP_HDRLEN; 715 wptr[0] = MPPE_CTRLHI(state); 716 wptr[1] = MPPE_CTRLLO(state); 717 718 state->bits = 0; 719 720 (state->stats).unc_bytes += isize; 721 (state->stats).unc_packets++; 722 723 return olen; 724} 725 726/***************************/ 727/*** Decompression stuff ***/ 728/***************************/ 729static inline u32 getbits(const u8 *buf, const u32 n, u32 *i, u32 *l) 730{ 731 static u32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; 732 u32 res, ol; 733 734 ol = *l; 735 if (*l >= n) { 736 *l = (*l) - n; 737 res = (buf[*i] & m[ol]) >> (*l); 738 if (*l == 0) { 739 *l = 8; 740 (*i)++; 741 } 742 } else { 743 *l = 8 - n + (*l); 744 res = (buf[(*i)++] & m[ol]) << 8; 745 res = (res | buf[*i]) >> (*l); 746 } 747 748 return res; 749} 750 751static inline u32 getbyte(const u8 *buf, const u32 i, const u32 l) 752{ 753 if (l == 8) { 754 return buf[i]; 755 } else { 756 return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff; 757 } 758} 759 760static inline void lamecopy(u8 *dst, u8 *src, u32 len) 761{ 762 while (len--) 763 *dst++ = *src++; 764} 765 766static int 767mppc_decompress(struct ppp_mppe_state *state, unsigned char *ibuf, 768 unsigned char *obuf, int isize, int osize) 769{ 770 u32 olen, off, len, bits, val, sig, i, l; 771 u8 *history, *s; 772 773 history = state->hist + state->histptr; 774 olen = len = i = 0; 775 l = 8; 776 bits = isize * 8; 777 while (bits >= 8) { 778 val = getbyte(ibuf, i++, l); 779 if (val < 0x80) { /* literal byte < 0x80 */ 780 if (state->histptr < 2*MPPE_HIST_LEN) { 781 /* copy uncompressed byte to the history */ 782 (state->hist)[(state->histptr)++] = (u8) val; 783 } else { 784 /* buffer overflow; drop packet */ 785 if (state->debug) 786 printk(KERN_ERR "%s%d: trying to write outside history " 787 "buffer\n", __FUNCTION__, state->unit); 788 return DECOMP_ERROR; 789 } 790 olen++; 791 bits -= 8; 792 continue; 793 } 794 795 sig = val & 0xc0; 796 if (sig == 0x80) { /* literal byte >= 0x80 */ 797 if (state->histptr < 2*MPPE_HIST_LEN) { 798 /* copy uncompressed byte to the history */ 799 (state->hist)[(state->histptr)++] = 800 (u8) (0x80|((val&0x3f)<<1)|getbits(ibuf, 1 , &i ,&l)); 801 } else { 802 /* buffer overflow; drop packet */ 803 if (state->debug) 804 printk(KERN_ERR "%s%d: trying to write outside history " 805 "buffer\n", __FUNCTION__, state->unit); 806 return DECOMP_ERROR; 807 } 808 olen++; 809 bits -= 9; 810 continue; 811 } 812 813 /* Not a literal byte so it must be an (offset,length) pair */ 814 /* decode offset */ 815 sig = val & 0xf0; 816 if (sig == 0xf0) { /* 10-bit offset; 0 <= offset < 64 */ 817 off = (((val&0x0f)<<2)|getbits(ibuf, 2 , &i ,&l)); 818 bits -= 10; 819 } else { 820 if (sig == 0xe0) { /* 12-bit offset; 64 <= offset < 320 */ 821 off = ((((val&0x0f)<<4)|getbits(ibuf, 4 , &i ,&l))+64); 822 bits -= 12; 823 } else { 824 if ((sig&0xe0) == 0xc0) {/* 16-bit offset; 320 <= offset < 8192 */ 825 off = ((((val&0x1f)<<8)|getbyte(ibuf, i++, l))+320); 826 bits -= 16; 827 if (off > MPPE_HIST_LEN - 1) { 828 if (state->debug) 829 printk(KERN_DEBUG "%s%d: too big offset value: %d\n", 830 __FUNCTION__, state->unit, off); 831 return DECOMP_ERROR; 832 } 833 } else { /* this shouldn't happen */ 834 if (state->debug) 835 printk(KERN_DEBUG "%s%d: cannot decode offset value\n", 836 __FUNCTION__, state->unit); 837 return DECOMP_ERROR; 838 } 839 } 840 } 841 /* decode length of match */ 842 val = getbyte(ibuf, i, l); 843 if ((val & 0x80) == 0x00) { /* len = 3 */ 844 len = 3; 845 bits--; 846 getbits(ibuf, 1 , &i ,&l); 847 } else if ((val & 0xc0) == 0x80) { /* 4 <= len < 8 */ 848 len = 0x04 | ((val>>4) & 0x03); 849 bits -= 4; 850 getbits(ibuf, 4 , &i ,&l); 851 } else if ((val & 0xe0) == 0xc0) { /* 8 <= len < 16 */ 852 len = 0x08 | ((val>>2) & 0x07); 853 bits -= 6; 854 getbits(ibuf, 6 , &i ,&l); 855 } else if ((val & 0xf0) == 0xe0) { /* 16 <= len < 32 */ 856 len = 0x10 | (val & 0x0f); 857 bits -= 8; 858 i++; 859 } else { 860 bits -= 8; 861 val = (val << 8) | getbyte(ibuf, ++i, l); 862 if ((val & 0xf800) == 0xf000) { /* 32 <= len < 64 */ 863 len = 0x0020 | ((val >> 6) & 0x001f); 864 bits -= 2; 865 getbits(ibuf, 2 , &i ,&l); 866 } else if ((val & 0xfc00) == 0xf800) { /* 64 <= len < 128 */ 867 len = 0x0040 | ((val >> 4) & 0x003f); 868 bits -= 4; 869 getbits(ibuf, 4 , &i ,&l); 870 } else if ((val & 0xfe00) == 0xfc00) { /* 128 <= len < 256 */ 871 len = 0x0080 | ((val >> 2) & 0x007f); 872 bits -= 6; 873 getbits(ibuf, 6 , &i ,&l); 874 } else if ((val & 0xff00) == 0xfe00) { /* 256 <= len < 512 */ 875 len = 0x0100 | (val & 0x00ff); 876 bits -= 8; 877 i++; 878 } else { 879 bits -= 8; 880 val = (val << 8) | getbyte(ibuf, ++i, l); 881 if ((val & 0xff8000) == 0xff0000) { /* 512 <= len < 1024 */ 882 len = 0x000200 | ((val >> 6) & 0x0001ff); 883 bits -= 2; 884 getbits(ibuf, 2 , &i ,&l); 885 } else if ((val & 0xffc000) == 0xff8000) {/* 1024 <= len < 2048 */ 886 len = 0x000400 | ((val >> 4) & 0x0003ff); 887 bits -= 4; 888 getbits(ibuf, 4 , &i ,&l); 889 } else if ((val & 0xffe000) == 0xffc000) {/* 2048 <= len < 4096 */ 890 len = 0x000800 | ((val >> 2) & 0x0007ff); 891 bits -= 6; 892 getbits(ibuf, 6 , &i ,&l); 893 } else if ((val & 0xfff000) == 0xffe000) {/* 4096 <= len < 8192 */ 894 len = 0x001000 | (val & 0x000fff); 895 bits -= 8; 896 i++; 897 } else { /* this shouldn't happen */ 898 if (state->debug) 899 printk(KERN_DEBUG "%s%d: wrong length code: 0x%X\n", 900 __FUNCTION__, state->unit, val); 901 return DECOMP_ERROR; 902 } 903 } 904 } 905 s = state->hist + state->histptr; 906 state->histptr += len; 907 olen += len; 908 if (state->histptr < 2*MPPE_HIST_LEN) { 909 /* copy uncompressed bytes to the history */ 910 911 /* In some cases len may be greater than off. It means that memory 912 * areas pointed by s and s-off overlap. I used to use memmove() 913 * here, because I thought that it acts as libc's version. But 914 * I was wrong. I got strange errors sometimes. Wilfried suggested 915 * using of byte by byte copying here and strange errors disappeared. 916 */ 917 lamecopy(s, s-off, len); 918 } else { 919 /* buffer overflow; drop packet */ 920 if (state->debug) 921 printk(KERN_ERR "%s%d: trying to write outside history " 922 "buffer\n", __FUNCTION__, state->unit); 923 return DECOMP_ERROR; 924 } 925 } 926 927 if (olen <= osize) { 928 /* copy uncompressed packet to the output buffer */ 929 memcpy(obuf, history, olen); 930 } else { 931 /* buffer overflow; drop packet */ 932 if (state->debug) 933 printk(KERN_ERR "%s%d: too big uncompressed packet: %d\n", 934 __FUNCTION__, state->unit, olen+(PPP_HDRLEN/2)); 935 return DECOMP_ERROR; 936 } 937 938 return (int) olen; 939} 940 941int 942mppe_decompress(void *arg, unsigned char *ibuf, int isize, 943 unsigned char *obuf, int osize) 944{ 945 struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg; 946 int seq, bits, uncomplen; 947 948 if (isize <= PPP_HDRLEN + MPPE_OVHD) { 949 if (state->debug) { 950 printk(KERN_DEBUG "%s%d: short packet (len=%d)\n", __FUNCTION__, 951 state->unit, isize); 952 } 953 return DECOMP_ERROR; 954 } 955 956 /* Get coherency counter and control bits from input buffer */ 957 seq = MPPE_CCOUNT(ibuf); 958 bits = MPPE_BITS(ibuf); 959 960 if (state->stateless) { 961 /* RFC 3078, sec 8.1. */ 962 mppe_increase_ccount(state); 963 if ((seq != state->ccount) && state->debug) 964 printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n", 965 __FUNCTION__, state->unit, seq, state->ccount); 966 while (seq != state->ccount) 967 mppe_increase_ccount(state); 968 } else { 969 /* RFC 3078, sec 8.2. */ 970 if (state->flushexpected) { /* discard state */ 971 if ((bits & MPPE_BIT_FLUSHED)) { /* we received expected FLUSH bit */ 972 while (seq != state->ccount) 973 mppe_increase_ccount(state); 974 state->flushexpected = 0; 975 } else /* drop packet*/ 976 return DECOMP_ERROR; 977 } else { /* normal state */ 978 mppe_increase_ccount(state); 979 if (seq != state->ccount) { 980 /* Packet loss detected, enter the discard state. */ 981 if (state->debug) 982 printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n", 983 __FUNCTION__, state->unit, seq, state->ccount); 984 state->flushexpected = 1; 985 return DECOMP_ERROR; 986 } 987 } 988 if (state->mppe && (bits & MPPE_BIT_FLUSHED)) { 989 arcfour_setkey(&state->arcfour_context, state->session_key, 990 state->keylen); 991 } 992 } 993 994 if (state->mppc && (bits & (MPPE_BIT_FLUSHED | MPPE_BIT_RESET))) { 995 state->histptr = MPPE_HIST_LEN; 996 if ((bits & MPPE_BIT_FLUSHED)) { 997 memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8)); 998 } else 999 if ((bits & MPPE_BIT_RESET)) { 1000 memcpy(state->hist, state->hist+MPPE_HIST_LEN, MPPE_HIST_LEN); 1001 } 1002 } 1003 1004 /* Fill in the first part of the PPP header. The protocol field 1005 comes from the decompressed data. */ 1006 obuf[0] = PPP_ADDRESS(ibuf); 1007 obuf[1] = PPP_CONTROL(ibuf); 1008 obuf += PPP_HDRLEN / 2; 1009 1010 if (state->mppe) { /* process encrypted packet */ 1011 if ((bits & MPPE_BIT_ENCRYPTED)) { 1012 /* OK, packet encrypted, so decrypt it */ 1013 if (state->mppc && (bits & MPPE_BIT_COMP)) { 1014 /* Hack warning !!! RC4 implementation which we use does 1015 decryption "in place" - it means that input and output 1016 buffers can be *the same* memory area. Therefore we don't 1017 need to use a temporary buffer. But be careful - other 1018 implementations don't have to be so nice. */ 1019 arcfour_decrypt(&state->arcfour_context, ibuf+PPP_HDRLEN+MPPE_OVHD, 1020 isize-PPP_HDRLEN-MPPE_OVHD, ibuf+PPP_HDRLEN+MPPE_OVHD); 1021 uncomplen = mppc_decompress(state, ibuf+PPP_HDRLEN+MPPE_OVHD, 1022 obuf, isize-PPP_HDRLEN-MPPE_OVHD, 1023 osize-(PPP_HDRLEN/2)); 1024 if (uncomplen == DECOMP_ERROR) { 1025 state->flushexpected = 1; 1026 return DECOMP_ERROR; 1027 } 1028 uncomplen += PPP_HDRLEN / 2; 1029 (state->stats).comp_bytes += isize; 1030 (state->stats).comp_packets++; 1031 } else { 1032 arcfour_decrypt(&state->arcfour_context, ibuf+PPP_HDRLEN+MPPE_OVHD, 1033 isize-PPP_HDRLEN-MPPE_OVHD, obuf); 1034 uncomplen = isize - (PPP_HDRLEN / 2) - MPPE_OVHD; 1035 (state->stats).inc_bytes += isize; 1036 (state->stats).inc_packets++; 1037 } 1038 } else { /* this shouldn't happen */ 1039 if (state->debug) 1040 printk(KERN_ERR "%s%d: encryption negotiated but not an " 1041 "encrypted packet received\n", __FUNCTION__, state->unit); 1042 mppe_change_key(state, 0); 1043 state->flushexpected = 1; 1044 return DECOMP_ERROR; 1045 } 1046 } else { 1047 if (state->mppc) { /* no MPPE, only MPPC */ 1048 if ((bits & MPPE_BIT_COMP)) { 1049 uncomplen = mppc_decompress(state, ibuf+PPP_HDRLEN+MPPE_OVHD, 1050 obuf, isize-PPP_HDRLEN-MPPE_OVHD, 1051 osize-(PPP_HDRLEN/2)); 1052 if (uncomplen == DECOMP_ERROR) { 1053 state->flushexpected = 1; 1054 return DECOMP_ERROR; 1055 } 1056 uncomplen += PPP_HDRLEN / 2; 1057 (state->stats).comp_bytes += isize; 1058 (state->stats).comp_packets++; 1059 } else { 1060 memcpy(obuf, ibuf+PPP_HDRLEN+MPPE_OVHD, 1061 isize-PPP_HDRLEN-MPPE_OVHD); 1062 uncomplen = isize - (PPP_HDRLEN / 2) - MPPE_OVHD; 1063 (state->stats).inc_bytes += isize; 1064 (state->stats).inc_packets++; 1065 } 1066 } else { /* this shouldn't happen */ 1067 if (state->debug) 1068 printk(KERN_ERR "%s%d: error - no MPPC nor MPPE negotiated\n", 1069 __FUNCTION__, state->unit); 1070 state->flushexpected = 1; 1071 return DECOMP_ERROR; 1072 } 1073 } 1074 1075 (state->stats).unc_bytes += uncomplen; 1076 (state->stats).unc_packets++; 1077 1078 return uncomplen; 1079} 1080 1081 1082/************************************************************ 1083 * Module interface table 1084 ************************************************************/ 1085 1086/* These are in ppp_generic.c */ 1087extern int ppp_register_compressor (struct compressor *cp); 1088extern void ppp_unregister_compressor (struct compressor *cp); 1089 1090/* 1091 * Functions exported to ppp_generic.c. 1092 */ 1093struct compressor ppp_mppe = { 1094 CI_MPPE, /* compress_proto */ 1095 mppe_comp_alloc, /* comp_alloc */ 1096 mppe_comp_free, /* comp_free */ 1097 mppe_comp_init, /* comp_init */ 1098 mppe_comp_reset, /* comp_reset */ 1099 mppe_compress, /* compress */ 1100 mppe_stats, /* comp_stat */ 1101 mppe_decomp_alloc, /* decomp_alloc */ 1102 mppe_comp_free, /* decomp_free */ 1103 mppe_decomp_init, /* decomp_init */ 1104 mppe_decomp_reset, /* decomp_reset */ 1105 mppe_decompress, /* decompress */ 1106 NULL, /* incomp */ 1107 mppe_stats, /* decomp_stat */ 1108}; 1109 1110/* 1111 In case of MPPC/MPPE there is no need to process incompressible data 1112 because such a data is sent in MPPC/MPPE frame. Therefore the (*incomp) 1113 callback function isn't needed. 1114*/ 1115 1116/************************************************************ 1117 * Module support routines 1118 ************************************************************/ 1119 1120int __init mppe_module_init(void) 1121{ 1122 int answer = ppp_register_compressor(&ppp_mppe); 1123 if (answer == 0) { 1124 printk(KERN_INFO "MPPE/MPPC encryption/compression module registered\n"); 1125 } 1126 return answer; 1127} 1128 1129void __exit mppe_module_cleanup(void) 1130{ 1131 if (MOD_IN_USE) { 1132 printk (KERN_INFO "MPPE/MPPC module busy, removing delayed\n"); 1133 } else { 1134 ppp_unregister_compressor (&ppp_mppe); 1135 printk(KERN_INFO "MPPE/MPPC encryption/compression module unregistered\n"); 1136 } 1137} 1138 1139module_init(mppe_module_init); 1140module_exit(mppe_module_cleanup); 1141 1142MODULE_AUTHOR("Jan Dubiec <jdx@slackware.pl>"); 1143MODULE_DESCRIPTION("MPPE/MPPC encryption/compression module for Linux"); 1144MODULE_LICENSE("Dual BSD/GPL"); 1145