1 2 /* 3 * ng_mppc.c 4 * 5 * Copyright (c) 1996-2000 Whistle Communications, Inc. 6 * All rights reserved. 7 * 8 * Subject to the following obligations and disclaimer of warranty, use and 9 * redistribution of this software, in source or object code forms, with or 10 * without modifications are expressly permitted by Whistle Communications; 11 * provided, however, that: 12 * 1. Any and all reproductions of the source or object code must include the 13 * copyright notice above and the following disclaimer of warranties; and 14 * 2. No rights are granted, in any manner or form, to use Whistle 15 * Communications, Inc. trademarks, including the mark "WHISTLE 16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17 * such appears in the above copyright notice or in the software. 18 * 19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35 * OF SUCH DAMAGE. 36 * 37 * Author: Archie Cobbs <archie@whistle.com> 38 * 39 * $Whistle: ng_mppc.c,v 1.4 1999/11/25 00:10:12 archie Exp $ 40 * $FreeBSD$ 41 */ 42 43 /* 44 * Microsoft PPP compression (MPPC) and encryption (MPPE) netgraph node type. 45 * 46 * You must define one or both of the NETGRAPH_MPPC_COMPRESSION and/or 47 * NETGRAPH_MPPC_ENCRYPTION options for this node type to be useful. 48 */ 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/kernel.h> 53 #include <sys/mbuf.h> 54 #include <sys/malloc.h> 55 #include <sys/protosw.h> 56 #include <sys/errno.h> 57 #include <sys/syslog.h> 58 59 #include <netgraph/ng_message.h> 60 #include <netgraph/netgraph.h> 61 #include <netgraph/ng_mppc.h> 62 63 #include "opt_netgraph.h" 64 65 #if !defined(NETGRAPH_MPPC_COMPRESSION) && !defined(NETGRAPH_MPPC_ENCRYPTION) 66 #error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION 67 #endif 68 69 #ifdef NETGRAPH_MPPC_COMPRESSION 70 /* XXX this file doesn't exist yet, but hopefully someday it will... */ 71 #include <net/mppc.h> 72 #endif 73 #ifdef NETGRAPH_MPPC_ENCRYPTION 74 #include <crypto/rc4/rc4.h> 75 #endif 76 #include <crypto/sha1.h> 77 78 /* Decompression blowup */ 79 #define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */ 80 #define MPPC_DECOMP_SAFETY 100 /* plus this much margin */ 81 82 /* MPPC/MPPE header length */ 83 #define MPPC_HDRLEN 2 84 85 /* Key length */ 86 #define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8) 87 88 /* What sequence number jump is too far */ 89 #define MPPC_INSANE_JUMP 256 90 91 /* MPPC packet header bits */ 92 #define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */ 93 #define MPPC_FLAG_RESTART 0x4000 /* compress history restart */ 94 #define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */ 95 #define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */ 96 #define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */ 97 98 #define MPPE_UPDATE_MASK 0xff /* coherency count when we're */ 99 #define MPPE_UPDATE_FLAG 0xff /* supposed to update key */ 100 101 #define MPPC_COMP_OK 0x05 102 #define MPPC_DECOMP_OK 0x05 103 104 /* Per direction info */ 105 struct ng_mppc_dir { 106 struct ng_mppc_config cfg; /* configuration */ 107 hook_p hook; /* netgraph hook */ 108 u_int16_t cc:12; /* coherency count */ 109 u_char flushed; /* clean history (xmit only) */ 110 #ifdef NETGRAPH_MPPC_COMPRESSION 111 u_char *history; /* compression history */ 112 #endif 113 #ifdef NETGRAPH_MPPC_ENCRYPTION 114 u_char key[MPPE_KEY_LEN]; /* session key */ 115 struct rc4_state rc4; /* rc4 state */ 116 #endif 117 }; 118 119 /* Node private data */ 120 struct ng_mppc_private { 121 struct ng_mppc_dir xmit; /* compress/encrypt config */ 122 struct ng_mppc_dir recv; /* decompress/decrypt config */ 123 char *ctrlpath; /* path to controlling node */ 124 }; 125 typedef struct ng_mppc_private *priv_p; 126 127 /* Netgraph node methods */ 128 static ng_constructor_t ng_mppc_constructor; 129 static ng_rcvmsg_t ng_mppc_rcvmsg; 130 static ng_shutdown_t ng_mppc_rmnode; 131 static ng_newhook_t ng_mppc_newhook; 132 static ng_rcvdata_t ng_mppc_rcvdata; 133 static ng_disconnect_t ng_mppc_disconnect; 134 135 /* Helper functions */ 136 static int ng_mppc_compress(node_p node, 137 struct mbuf *m, struct mbuf **resultp); 138 static int ng_mppc_decompress(node_p node, 139 struct mbuf *m, struct mbuf **resultp); 140 static void ng_mppc_getkey(const u_char *h, u_char *h2, int len); 141 static void ng_mppc_updatekey(u_int32_t bits, 142 u_char *key0, u_char *key, struct rc4_state *rc4); 143 static void ng_mppc_reset_req(node_p node); 144 145 /* Node type descriptor */ 146 static struct ng_type ng_mppc_typestruct = { 147 NG_VERSION, 148 NG_MPPC_NODE_TYPE, 149 NULL, 150 ng_mppc_constructor, 151 ng_mppc_rcvmsg, 152 ng_mppc_rmnode, 153 ng_mppc_newhook, 154 NULL, 155 NULL, 156 ng_mppc_rcvdata, 157 ng_mppc_rcvdata, 158 ng_mppc_disconnect, 159 NULL 160 }; 161 NETGRAPH_INIT(mppc, &ng_mppc_typestruct); 162 163 /* Fixed bit pattern to weaken keysize down to 40 bits */ 164 static const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e }; 165 166 #define ERROUT(x) do { error = (x); goto done; } while (0) 167 168 /************************************************************************ 169 NETGRAPH NODE STUFF 170 ************************************************************************/ 171 172 /* 173 * Node type constructor 174 */ 175 static int 176 ng_mppc_constructor(node_p *nodep) 177 { 178 priv_p priv; 179 int error; 180 181 /* Allocate private structure */ 182 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK); 183 if (priv == NULL) 184 return (ENOMEM); 185 bzero(priv, sizeof(*priv)); 186 187 /* Call generic node constructor */ 188 if ((error = ng_make_node_common(&ng_mppc_typestruct, nodep))) { 189 FREE(priv, M_NETGRAPH); 190 return (error); 191 } 192 (*nodep)->private = priv; 193 194 /* Done */ 195 return (0); 196 } 197 198 /* 199 * Give our OK for a hook to be added 200 */ 201 static int 202 ng_mppc_newhook(node_p node, hook_p hook, const char *name) 203 { 204 const priv_p priv = node->private; 205 hook_p *hookPtr; 206 207 /* Check hook name */ 208 if (strcmp(name, NG_MPPC_HOOK_COMP) == 0) 209 hookPtr = &priv->xmit.hook; 210 else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0) 211 hookPtr = &priv->recv.hook; 212 else 213 return (EINVAL); 214 215 /* See if already connected */ 216 if (*hookPtr != NULL) 217 return (EISCONN); 218 219 /* OK */ 220 *hookPtr = hook; 221 return (0); 222 } 223 224 /* 225 * Receive a control message 226 */ 227 static int 228 ng_mppc_rcvmsg(node_p node, struct ng_mesg *msg, 229 const char *raddr, struct ng_mesg **rptr, hook_p lasthook) 230 { 231 const priv_p priv = node->private; 232 struct ng_mesg *resp = NULL; 233 int error = 0; 234 235 switch (msg->header.typecookie) { 236 case NGM_MPPC_COOKIE: 237 switch (msg->header.cmd) { 238 case NGM_MPPC_CONFIG_COMP: 239 case NGM_MPPC_CONFIG_DECOMP: 240 { 241 struct ng_mppc_config *const cfg 242 = (struct ng_mppc_config *)msg->data; 243 const int isComp = 244 msg->header.cmd == NGM_MPPC_CONFIG_COMP; 245 struct ng_mppc_dir *const d = isComp ? 246 &priv->xmit : &priv->recv; 247 248 /* Check configuration */ 249 if (msg->header.arglen != sizeof(*cfg)) 250 ERROUT(EINVAL); 251 if (cfg->enable) { 252 if ((cfg->bits & ~MPPC_VALID_BITS) != 0) 253 ERROUT(EINVAL); 254 #ifndef NETGRAPH_MPPC_COMPRESSION 255 if ((cfg->bits & MPPC_BIT) != 0) 256 ERROUT(EPROTONOSUPPORT); 257 #endif 258 #ifndef NETGRAPH_MPPC_ENCRYPTION 259 if ((cfg->bits & MPPE_BITS) != 0) 260 ERROUT(EPROTONOSUPPORT); 261 #endif 262 } else 263 cfg->bits = 0; 264 265 /* Save return address so we can send reset-req's */ 266 if (priv->ctrlpath != NULL) { 267 FREE(priv->ctrlpath, M_NETGRAPH); 268 priv->ctrlpath = NULL; 269 } 270 if (!isComp && raddr != NULL) { 271 MALLOC(priv->ctrlpath, char *, 272 strlen(raddr) + 1, M_NETGRAPH, M_NOWAIT); 273 if (priv->ctrlpath == NULL) 274 ERROUT(ENOMEM); 275 strcpy(priv->ctrlpath, raddr); 276 } 277 278 /* Configuration is OK, reset to it */ 279 d->cfg = *cfg; 280 281 #ifdef NETGRAPH_MPPC_COMPRESSION 282 /* Initialize state buffers for compression */ 283 if (d->history != NULL) { 284 FREE(d->history, M_NETGRAPH); 285 d->history = NULL; 286 } 287 if ((cfg->bits & MPPC_BIT) != 0) { 288 MALLOC(d->history, u_char *, 289 isComp ? MPPC_SizeOfCompressionHistory() : 290 MPPC_SizeOfDecompressionHistory(), 291 M_NETGRAPH, M_NOWAIT); 292 if (d->history == NULL) 293 ERROUT(ENOMEM); 294 if (isComp) 295 MPPC_InitCompressionHistory(d->history); 296 else { 297 MPPC_InitDecompressionHistory( 298 d->history); 299 } 300 } 301 #endif 302 303 #ifdef NETGRAPH_MPPC_ENCRYPTION 304 /* Generate initial session keys for encryption */ 305 if ((cfg->bits & MPPE_BITS) != 0) { 306 const int keylen = KEYLEN(cfg->bits); 307 308 bcopy(cfg->startkey, d->key, keylen); 309 ng_mppc_getkey(cfg->startkey, d->key, keylen); 310 if ((cfg->bits & MPPE_128) == 0) { 311 bcopy(&ng_mppe_weakenkey, d->key, 312 sizeof(ng_mppe_weakenkey)); 313 } 314 rc4_init(&d->rc4, d->key, keylen); 315 } 316 #endif 317 318 /* Initialize other state */ 319 d->cc = 0; 320 d->flushed = 0; 321 break; 322 } 323 324 case NGM_MPPC_RESETREQ: 325 ng_mppc_reset_req(node); 326 break; 327 328 default: 329 error = EINVAL; 330 break; 331 } 332 break; 333 default: 334 error = EINVAL; 335 break; 336 } 337 if (rptr) 338 *rptr = resp; 339 else if (resp) 340 FREE(resp, M_NETGRAPH); 341 342 done: 343 FREE(msg, M_NETGRAPH); 344 return (error); 345 } 346 347 /* 348 * Receive incoming data on our hook. 349 */ 350 static int 351 ng_mppc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 352 struct mbuf **ret_m, meta_p *ret_meta) 353 { 354 const node_p node = hook->node; 355 const priv_p priv = node->private; 356 struct mbuf *out; 357 int error; 358 359 /* Compress and/or encrypt */ 360 if (hook == priv->xmit.hook) { 361 if (!priv->xmit.cfg.enable) { 362 NG_FREE_DATA(m, meta); 363 return (ENXIO); 364 } 365 if ((error = ng_mppc_compress(node, m, &out)) != 0) { 366 NG_FREE_DATA(m, meta); 367 return(error); 368 } 369 m_freem(m); 370 NG_SEND_DATA(error, priv->xmit.hook, out, meta); 371 return (error); 372 } 373 374 /* Decompress and/or decrypt */ 375 if (hook == priv->recv.hook) { 376 if (!priv->recv.cfg.enable) { 377 NG_FREE_DATA(m, meta); 378 return (ENXIO); 379 } 380 if ((error = ng_mppc_decompress(node, m, &out)) != 0) { 381 NG_FREE_DATA(m, meta); 382 if (error == EINVAL && priv->ctrlpath != NULL) { 383 struct ng_mesg *msg; 384 385 /* Need to send a reset-request */ 386 NG_MKMESSAGE(msg, NGM_MPPC_COOKIE, 387 NGM_MPPC_RESETREQ, 0, M_NOWAIT); 388 if (msg == NULL) 389 return (error); 390 ng_send_msg(node, msg, priv->ctrlpath, NULL); 391 } 392 return (error); 393 } 394 m_freem(m); 395 NG_SEND_DATA(error, priv->recv.hook, out, meta); 396 return (error); 397 } 398 399 /* Oops */ 400 panic("%s: unknown hook", __FUNCTION__); 401 } 402 403 /* 404 * Destroy node 405 */ 406 static int 407 ng_mppc_rmnode(node_p node) 408 { 409 const priv_p priv = node->private; 410 411 /* Take down netgraph node */ 412 node->flags |= NG_INVALID; 413 ng_cutlinks(node); 414 ng_unname(node); 415 if (priv->ctrlpath != NULL) 416 FREE(priv->ctrlpath, M_NETGRAPH); 417 #ifdef NETGRAPH_MPPC_COMPRESSION 418 if (priv->xmit.history != NULL) 419 FREE(priv->xmit.history, M_NETGRAPH); 420 if (priv->recv.history != NULL) 421 FREE(priv->recv.history, M_NETGRAPH); 422 #endif 423 bzero(priv, sizeof(*priv)); 424 FREE(priv, M_NETGRAPH); 425 node->private = NULL; 426 ng_unref(node); /* let the node escape */ 427 return (0); 428 } 429 430 /* 431 * Hook disconnection 432 */ 433 static int 434 ng_mppc_disconnect(hook_p hook) 435 { 436 const node_p node = hook->node; 437 const priv_p priv = node->private; 438 439 /* Zero out hook pointer */ 440 if (hook == priv->xmit.hook) 441 priv->xmit.hook = NULL; 442 if (hook == priv->recv.hook) 443 priv->recv.hook = NULL; 444 445 /* Go away if no longer connected */ 446 if (node->numhooks == 0) 447 ng_rmnode(node); 448 return (0); 449 } 450 451 /************************************************************************ 452 HELPER STUFF 453 ************************************************************************/ 454 455 /* 456 * Compress/encrypt a packet and put the result in a new mbuf at *resultp. 457 * The original mbuf is not free'd. 458 */ 459 static int 460 ng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp) 461 { 462 const priv_p priv = node->private; 463 struct ng_mppc_dir *const d = &priv->xmit; 464 u_char *inbuf, *outbuf; 465 int outlen, inlen; 466 u_int16_t header; 467 468 /* Initialize */ 469 *resultp = NULL; 470 header = d->cc; 471 if (d->flushed) { 472 header |= MPPC_FLAG_FLUSHED; 473 d->flushed = 0; 474 } 475 476 /* Work with contiguous regions of memory */ 477 inlen = m->m_pkthdr.len; 478 MALLOC(inbuf, u_char *, inlen, M_NETGRAPH, M_NOWAIT); 479 if (inbuf == NULL) 480 return (ENOMEM); 481 m_copydata(m, 0, inlen, (caddr_t)inbuf); 482 if ((d->cfg.bits & MPPC_BIT) != 0) 483 outlen = MPPC_MAX_BLOWUP(inlen); 484 else 485 outlen = MPPC_HDRLEN + inlen; 486 MALLOC(outbuf, u_char *, outlen, M_NETGRAPH, M_WAITOK); 487 if (outbuf == NULL) { 488 FREE(inbuf, M_NETGRAPH); 489 return (ENOMEM); 490 } 491 492 /* Compress "inbuf" into "outbuf" (if compression enabled) */ 493 #ifdef NETGRAPH_MPPC_COMPRESSION 494 if ((d->cfg.bits & MPPC_BIT) != 0) { 495 u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS; 496 u_char *source, *dest; 497 u_long sourceCnt, destCnt; 498 int rtn; 499 500 /* Prepare to compress */ 501 source = inbuf; 502 sourceCnt = inlen; 503 dest = outbuf + MPPC_HDRLEN; 504 destCnt = outlen - MPPC_HDRLEN; 505 if ((d->cfg.bits & MPPE_STATELESS) == 0) 506 flags |= MPPC_SAVE_HISTORY; 507 508 /* Compress */ 509 rtn = MPPC_Compress(&source, &dest, &sourceCnt, 510 &destCnt, d->history, flags, 0); 511 512 /* Check return value */ 513 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __FUNCTION__)); 514 if ((rtn & MPPC_EXPANDED) == 0 515 && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) { 516 outlen -= destCnt; 517 header |= MPPC_FLAG_COMPRESSED; 518 if ((rtn & MPPC_RESTART_HISTORY) != 0) 519 header |= MPPC_FLAG_RESTART; 520 } 521 d->flushed = (rtn & MPPC_EXPANDED) != 0 522 || (flags & MPPC_SAVE_HISTORY) == 0; 523 } 524 #endif 525 526 /* If we did not compress this packet, copy it to output buffer */ 527 if ((header & MPPC_FLAG_COMPRESSED) == 0) { 528 bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen); 529 outlen = MPPC_HDRLEN + inlen; 530 } 531 FREE(inbuf, M_NETGRAPH); 532 533 /* Always set the flushed bit in stateless mode */ 534 if ((d->cfg.bits & MPPE_STATELESS) != 0) 535 header |= MPPC_FLAG_FLUSHED; 536 537 /* Now encrypt packet (if encryption enabled) */ 538 #ifdef NETGRAPH_MPPC_ENCRYPTION 539 if ((d->cfg.bits & MPPE_BITS) != 0) { 540 541 /* Set header bits; need to reset key if we say we did */ 542 header |= MPPC_FLAG_ENCRYPTED; 543 if ((header & MPPC_FLAG_FLUSHED) != 0) 544 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 545 546 /* Update key if it's time */ 547 if ((d->cfg.bits & MPPE_STATELESS) != 0 548 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { 549 ng_mppc_updatekey(d->cfg.bits, 550 d->cfg.startkey, d->key, &d->rc4); 551 } 552 553 /* Encrypt packet */ 554 rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN, 555 outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN); 556 } 557 #endif 558 559 /* Update sequence number */ 560 d->cc++; 561 562 /* Install header */ 563 *((u_int16_t *)outbuf) = htons(header); 564 565 /* Return packet in an mbuf */ 566 *resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL); 567 FREE(outbuf, M_NETGRAPH); 568 return (*resultp == NULL ? ENOBUFS : 0); 569 } 570 571 /* 572 * Decompress/decrypt packet and put the result in a new mbuf at *resultp. 573 * The original mbuf is not free'd. 574 */ 575 static int 576 ng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp) 577 { 578 const priv_p priv = node->private; 579 struct ng_mppc_dir *const d = &priv->recv; 580 u_int16_t header, cc, numLost; 581 u_char *buf; 582 int len; 583 584 /* Pull off header */ 585 if (m->m_pkthdr.len < MPPC_HDRLEN) 586 return (EINVAL); 587 m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header); 588 NTOHS(header); 589 cc = (header & MPPC_CCOUNT_MASK); 590 591 /* Copy payload into a contiguous region of memory */ 592 len = m->m_pkthdr.len - MPPC_HDRLEN; 593 MALLOC(buf, u_char *, len, M_NETGRAPH, M_NOWAIT); 594 if (buf == NULL) 595 return (ENOMEM); 596 m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf); 597 598 /* Check for insane jumps in sequence numbering (D.O.S. attack) */ 599 numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK); 600 if (numLost >= MPPC_INSANE_JUMP) { 601 log(LOG_ERR, "%s: insane jump %d", __FUNCTION__, numLost); 602 priv->recv.cfg.enable = 0; 603 goto failed; 604 } 605 606 /* If flushed bit set, we can always handle packet */ 607 if ((header & MPPC_FLAG_FLUSHED) != 0) { 608 #ifdef NETGRAPH_MPPC_COMPRESSION 609 if (d->history != NULL) 610 MPPC_InitDecompressionHistory(d->history); 611 #endif 612 #ifdef NETGRAPH_MPPC_ENCRYPTION 613 if ((d->cfg.bits & MPPE_BITS) != 0) { 614 615 /* Resync as necessary, skipping lost packets */ 616 while (d->cc != cc) { 617 if ((d->cfg.bits & MPPE_STATELESS) 618 || (d->cc & MPPE_UPDATE_MASK) 619 == MPPE_UPDATE_FLAG) { 620 ng_mppc_updatekey(d->cfg.bits, 621 d->cfg.startkey, d->key, &d->rc4); 622 } 623 d->cc++; 624 } 625 626 /* Reset key (except in stateless mode, see below) */ 627 if ((d->cfg.bits & MPPE_STATELESS) == 0) 628 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 629 } 630 #endif 631 d->cc = cc; /* skip over lost seq numbers */ 632 numLost = 0; /* act like no packets were lost */ 633 } 634 635 /* Can't decode non-sequential packets without a flushed bit */ 636 if (numLost != 0) 637 goto failed; 638 639 /* Decrypt packet */ 640 if ((header & MPPC_FLAG_ENCRYPTED) != 0) { 641 642 /* Are we not expecting encryption? */ 643 if ((d->cfg.bits & MPPE_BITS) == 0) { 644 log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 645 __FUNCTION__, "encrypted"); 646 goto failed; 647 } 648 649 #ifdef NETGRAPH_MPPC_ENCRYPTION 650 /* Update key if it's time (always in stateless mode) */ 651 if ((d->cfg.bits & MPPE_STATELESS) != 0 652 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { 653 ng_mppc_updatekey(d->cfg.bits, 654 d->cfg.startkey, d->key, &d->rc4); 655 } 656 657 /* Decrypt packet */ 658 rc4_crypt(&d->rc4, buf, buf, len); 659 #endif 660 } else { 661 662 /* Are we expecting encryption? */ 663 if ((d->cfg.bits & MPPE_BITS) != 0) { 664 log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 665 __FUNCTION__, "unencrypted"); 666 goto failed; 667 } 668 } 669 670 /* Update coherency count for next time (12 bit arithmetic) */ 671 d->cc++; 672 673 /* Check for unexpected compressed packet */ 674 if ((header & MPPC_FLAG_COMPRESSED) != 0 675 && (d->cfg.bits & MPPC_BIT) == 0) { 676 log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 677 __FUNCTION__, "compressed"); 678 failed: 679 FREE(buf, M_NETGRAPH); 680 return (EINVAL); 681 } 682 683 #ifdef NETGRAPH_MPPC_COMPRESSION 684 /* Decompress packet */ 685 if ((header & MPPC_FLAG_COMPRESSED) != 0) { 686 int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS; 687 u_char *decompbuf, *source, *dest; 688 u_long sourceCnt, destCnt; 689 int decomplen, rtn; 690 691 /* Allocate a buffer for decompressed data */ 692 MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE 693 + MPPC_DECOMP_SAFETY, M_NETGRAPH, M_NOWAIT); 694 if (decompbuf == NULL) { 695 FREE(buf, M_NETGRAPH); 696 return (ENOMEM); 697 } 698 decomplen = MPPC_DECOMP_BUFSIZE; 699 700 /* Prepare to decompress */ 701 source = buf; 702 sourceCnt = len; 703 dest = decompbuf; 704 destCnt = decomplen; 705 if ((header & MPPC_FLAG_RESTART) != 0) 706 flags |= MPPC_RESTART_HISTORY; 707 708 /* Decompress */ 709 rtn = MPPC_Decompress(&source, &dest, 710 &sourceCnt, &destCnt, d->history, flags); 711 712 /* Check return value */ 713 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __FUNCTION__)); 714 if ((rtn & MPPC_DEST_EXHAUSTED) != 0 715 || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) { 716 log(LOG_ERR, "%s: decomp returned 0x%x", 717 __FUNCTION__, rtn); 718 FREE(decompbuf, M_NETGRAPH); 719 goto failed; 720 } 721 722 /* Replace compressed data with decompressed data */ 723 FREE(buf, M_NETGRAPH); 724 buf = decompbuf; 725 len = decomplen - destCnt; 726 } 727 #endif 728 729 /* Return result in an mbuf */ 730 *resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL); 731 FREE(buf, M_NETGRAPH); 732 return (*resultp == NULL ? ENOBUFS : 0); 733 } 734 735 /* 736 * The peer has sent us a CCP ResetRequest, so reset our transmit state. 737 */ 738 static void 739 ng_mppc_reset_req(node_p node) 740 { 741 const priv_p priv = node->private; 742 struct ng_mppc_dir *const d = &priv->xmit; 743 744 #ifdef NETGRAPH_MPPC_COMPRESSION 745 if (d->history != NULL) 746 MPPC_InitCompressionHistory(d->history); 747 #endif 748 #ifdef NETGRAPH_MPPC_ENCRYPTION 749 if ((d->cfg.bits & MPPE_STATELESS) == 0) 750 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 751 #endif 752 d->flushed = 1; 753 } 754 755 /* 756 * Generate a new encryption key 757 */ 758 static void 759 ng_mppc_getkey(const u_char *h, u_char *h2, int len) 760 { 761 static const u_char pad1[10] = 762 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; 763 static const u_char pad2[10] = 764 { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, }; 765 u_char hash[20]; 766 SHA1_CTX c; 767 int k; 768 769 bzero(&hash, sizeof(hash)); 770 SHA1Init(&c); 771 SHA1Update(&c, h, len); 772 for (k = 0; k < 4; k++) 773 SHA1Update(&c, pad1, sizeof(pad2)); 774 SHA1Update(&c, h2, len); 775 for (k = 0; k < 4; k++) 776 SHA1Update(&c, pad2, sizeof(pad2)); 777 SHA1Final(hash, &c); 778 bcopy(hash, h2, len); 779 } 780 781 /* 782 * Update the encryption key 783 */ 784 static void 785 ng_mppc_updatekey(u_int32_t bits, 786 u_char *key0, u_char *key, struct rc4_state *rc4) 787 { 788 const int keylen = KEYLEN(bits); 789 790 ng_mppc_getkey(key0, key, keylen); 791 rc4_init(rc4, key, keylen); 792 rc4_crypt(rc4, key, key, keylen); 793 if ((bits & MPPE_128) == 0) 794 bcopy(&ng_mppe_weakenkey, key, sizeof(ng_mppe_weakenkey)); 795 rc4_init(rc4, key, keylen); 796 } 797 798