6 * Copyright (c) 1996-2000 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
38 * Author: Archie Cobbs <archie@freebsd.org>
40 * $Whistle: ng_mppc.c,v 1.4 1999/11/25 00:10:12 archie Exp $
45 * Microsoft PPP compression (MPPC) and encryption (MPPE) netgraph node type.
47 * You must define one or both of the NETGRAPH_MPPC_COMPRESSION and/or
48 * NETGRAPH_MPPC_ENCRYPTION options for this node type to be useful.
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/errno.h>
57 #include <sys/syslog.h>
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_mppc.h>
63 #include "opt_netgraph.h"
65 #if !defined(NETGRAPH_MPPC_COMPRESSION) && !defined(NETGRAPH_MPPC_ENCRYPTION)
67 /* XXX NETGRAPH_MPPC_COMPRESSION isn't functional yet */
68 #define NETGRAPH_MPPC_ENCRYPTION
70 /* This case is indicative of an error in sys/conf files */
71 #error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION
75 #ifdef NG_SEPARATE_MALLOC
76 MALLOC_DEFINE(M_NETGRAPH_MPPC, "netgraph_mppc", "netgraph mppc node ");
78 #define M_NETGRAPH_MPPC M_NETGRAPH
81 #ifdef NETGRAPH_MPPC_COMPRESSION
82 /* XXX this file doesn't exist yet, but hopefully someday it will... */
85 #ifdef NETGRAPH_MPPC_ENCRYPTION
86 #include <crypto/rc4/rc4.h>
88 #include <crypto/sha1.h>
90 /* Decompression blowup */
91 #define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */
92 #define MPPC_DECOMP_SAFETY 100 /* plus this much margin */
94 /* MPPC/MPPE header length */
98 #define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8)
101 * When packets are lost with MPPE, we may have to re-key arbitrarily
102 * many times to 'catch up' to the new jumped-ahead sequence number.
103 * Since this can be expensive, we pose a limit on how many re-keyings
104 * we will do at one time to avoid a possible D.O.S. vulnerability.
105 * This should instead be a configurable parameter.
107 #define MPPE_MAX_REKEY 1000
109 /* MPPC packet header bits */
110 #define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */
111 #define MPPC_FLAG_RESTART 0x4000 /* compress history restart */
112 #define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */
113 #define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */
114 #define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */
116 #define MPPE_UPDATE_MASK 0xff /* coherency count when we're */
117 #define MPPE_UPDATE_FLAG 0xff /* supposed to update key */
119 #define MPPC_COMP_OK 0x05
120 #define MPPC_DECOMP_OK 0x05
122 /* Per direction info */
124 struct ng_mppc_config cfg; /* configuration */
125 hook_p hook; /* netgraph hook */
126 u_int16_t cc:12; /* coherency count */
127 u_char flushed; /* clean history (xmit only) */
128 #ifdef NETGRAPH_MPPC_COMPRESSION
129 u_char *history; /* compression history */
131 #ifdef NETGRAPH_MPPC_ENCRYPTION
132 u_char key[MPPE_KEY_LEN]; /* session key */
133 struct rc4_state rc4; /* rc4 state */
137 /* Node private data */
138 struct ng_mppc_private {
139 struct ng_mppc_dir xmit; /* compress/encrypt config */
140 struct ng_mppc_dir recv; /* decompress/decrypt config */
141 ng_ID_t ctrlnode; /* path to controlling node */
143 typedef struct ng_mppc_private *priv_p;
145 /* Netgraph node methods */
146 static ng_constructor_t ng_mppc_constructor;
147 static ng_rcvmsg_t ng_mppc_rcvmsg;
148 static ng_shutdown_t ng_mppc_shutdown;
149 static ng_newhook_t ng_mppc_newhook;
150 static ng_rcvdata_t ng_mppc_rcvdata;
151 static ng_disconnect_t ng_mppc_disconnect;
153 /* Helper functions */
154 static int ng_mppc_compress(node_p node,
155 struct mbuf *m, struct mbuf **resultp);
156 static int ng_mppc_decompress(node_p node,
157 struct mbuf *m, struct mbuf **resultp);
158 static void ng_mppc_getkey(const u_char *h, u_char *h2, int len);
159 static void ng_mppc_updatekey(u_int32_t bits,
160 u_char *key0, u_char *key, struct rc4_state *rc4);
161 static void ng_mppc_reset_req(node_p node);
163 /* Node type descriptor */
164 static struct ng_type ng_mppc_typestruct = {
165 .version = NG_ABI_VERSION,
166 .name = NG_MPPC_NODE_TYPE,
167 .constructor = ng_mppc_constructor,
168 .rcvmsg = ng_mppc_rcvmsg,
169 .shutdown = ng_mppc_shutdown,
170 .newhook = ng_mppc_newhook,
171 .rcvdata = ng_mppc_rcvdata,
172 .disconnect = ng_mppc_disconnect,
174 NETGRAPH_INIT(mppc, &ng_mppc_typestruct);
176 #ifdef NETGRAPH_MPPC_ENCRYPTION
177 /* Depend on separate rc4 module */
178 MODULE_DEPEND(ng_mppc, rc4, 1, 1, 1);
181 /* Fixed bit pattern to weaken keysize down to 40 or 56 bits */
182 static const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e };
184 #define ERROUT(x) do { error = (x); goto done; } while (0)
186 /************************************************************************
188 ************************************************************************/
191 * Node type constructor
194 ng_mppc_constructor(node_p node)
198 /* Allocate private structure */
199 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_MPPC, M_NOWAIT | M_ZERO);
203 NG_NODE_SET_PRIVATE(node, priv);
205 /* This node is not thread safe. */
206 NG_NODE_FORCE_WRITER(node);
213 * Give our OK for a hook to be added
216 ng_mppc_newhook(node_p node, hook_p hook, const char *name)
218 const priv_p priv = NG_NODE_PRIVATE(node);
221 /* Check hook name */
222 if (strcmp(name, NG_MPPC_HOOK_COMP) == 0)
223 hookPtr = &priv->xmit.hook;
224 else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0)
225 hookPtr = &priv->recv.hook;
229 /* See if already connected */
230 if (*hookPtr != NULL)
239 * Receive a control message
242 ng_mppc_rcvmsg(node_p node, item_p item, hook_p lasthook)
244 const priv_p priv = NG_NODE_PRIVATE(node);
245 struct ng_mesg *resp = NULL;
249 NGI_GET_MSG(item, msg);
250 switch (msg->header.typecookie) {
251 case NGM_MPPC_COOKIE:
252 switch (msg->header.cmd) {
253 case NGM_MPPC_CONFIG_COMP:
254 case NGM_MPPC_CONFIG_DECOMP:
256 struct ng_mppc_config *const cfg
257 = (struct ng_mppc_config *)msg->data;
259 msg->header.cmd == NGM_MPPC_CONFIG_COMP;
260 struct ng_mppc_dir *const d = isComp ?
261 &priv->xmit : &priv->recv;
263 /* Check configuration */
264 if (msg->header.arglen != sizeof(*cfg))
267 if ((cfg->bits & ~MPPC_VALID_BITS) != 0)
269 #ifndef NETGRAPH_MPPC_COMPRESSION
270 if ((cfg->bits & MPPC_BIT) != 0)
271 ERROUT(EPROTONOSUPPORT);
273 #ifndef NETGRAPH_MPPC_ENCRYPTION
274 if ((cfg->bits & MPPE_BITS) != 0)
275 ERROUT(EPROTONOSUPPORT);
280 /* Save return address so we can send reset-req's */
282 priv->ctrlnode = NGI_RETADDR(item);
284 /* Configuration is OK, reset to it */
287 #ifdef NETGRAPH_MPPC_COMPRESSION
288 /* Initialize state buffers for compression */
289 if (d->history != NULL) {
290 FREE(d->history, M_NETGRAPH_MPPC);
293 if ((cfg->bits & MPPC_BIT) != 0) {
294 MALLOC(d->history, u_char *,
295 isComp ? MPPC_SizeOfCompressionHistory() :
296 MPPC_SizeOfDecompressionHistory(),
297 M_NETGRAPH_MPPC, M_NOWAIT);
298 if (d->history == NULL)
301 MPPC_InitCompressionHistory(d->history);
303 MPPC_InitDecompressionHistory(
309 #ifdef NETGRAPH_MPPC_ENCRYPTION
310 /* Generate initial session keys for encryption */
311 if ((cfg->bits & MPPE_BITS) != 0) {
312 const int keylen = KEYLEN(cfg->bits);
314 bcopy(cfg->startkey, d->key, keylen);
315 ng_mppc_getkey(cfg->startkey, d->key, keylen);
316 if ((cfg->bits & MPPE_40) != 0)
317 bcopy(&ng_mppe_weakenkey, d->key, 3);
318 else if ((cfg->bits & MPPE_56) != 0)
319 bcopy(&ng_mppe_weakenkey, d->key, 1);
320 rc4_init(&d->rc4, d->key, keylen);
324 /* Initialize other state */
330 case NGM_MPPC_RESETREQ:
331 ng_mppc_reset_req(node);
344 NG_RESPOND_MSG(error, node, item, resp);
350 * Receive incoming data on our hook.
353 ng_mppc_rcvdata(hook_p hook, item_p item)
355 const node_p node = NG_HOOK_NODE(hook);
356 const priv_p priv = NG_NODE_PRIVATE(node);
362 /* Compress and/or encrypt */
363 if (hook == priv->xmit.hook) {
364 if (!priv->xmit.cfg.enable) {
369 if ((error = ng_mppc_compress(node, m, &out)) != 0) {
375 NG_FWD_NEW_DATA(error, item, priv->xmit.hook, out);
379 /* Decompress and/or decrypt */
380 if (hook == priv->recv.hook) {
381 if (!priv->recv.cfg.enable) {
386 if ((error = ng_mppc_decompress(node, m, &out)) != 0) {
389 if (error == EINVAL && priv->ctrlnode != 0) {
392 /* Need to send a reset-request */
393 NG_MKMESSAGE(msg, NGM_MPPC_COOKIE,
394 NGM_MPPC_RESETREQ, 0, M_NOWAIT);
397 NG_SEND_MSG_ID(error, node, msg,
403 NG_FWD_NEW_DATA(error, item, priv->recv.hook, out);
408 panic("%s: unknown hook", __func__);
409 #ifdef RESTARTABLE_PANICS
418 ng_mppc_shutdown(node_p node)
420 const priv_p priv = NG_NODE_PRIVATE(node);
422 /* Take down netgraph node */
423 #ifdef NETGRAPH_MPPC_COMPRESSION
424 if (priv->xmit.history != NULL)
425 FREE(priv->xmit.history, M_NETGRAPH_MPPC);
426 if (priv->recv.history != NULL)
427 FREE(priv->recv.history, M_NETGRAPH_MPPC);
429 bzero(priv, sizeof(*priv));
430 FREE(priv, M_NETGRAPH_MPPC);
431 NG_NODE_SET_PRIVATE(node, NULL);
432 NG_NODE_UNREF(node); /* let the node escape */
440 ng_mppc_disconnect(hook_p hook)
442 const node_p node = NG_HOOK_NODE(hook);
443 const priv_p priv = NG_NODE_PRIVATE(node);
445 /* Zero out hook pointer */
446 if (hook == priv->xmit.hook)
447 priv->xmit.hook = NULL;
448 if (hook == priv->recv.hook)
449 priv->recv.hook = NULL;
451 /* Go away if no longer connected */
452 if ((NG_NODE_NUMHOOKS(node) == 0)
453 && NG_NODE_IS_VALID(node))
454 ng_rmnode_self(node);
458 /************************************************************************
460 ************************************************************************/
463 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
464 * The original mbuf is not free'd.
467 ng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
469 const priv_p priv = NG_NODE_PRIVATE(node);
470 struct ng_mppc_dir *const d = &priv->xmit;
471 u_char *inbuf, *outbuf;
479 header |= MPPC_FLAG_FLUSHED;
483 /* Work with contiguous regions of memory */
484 inlen = m->m_pkthdr.len;
485 MALLOC(inbuf, u_char *, inlen, M_NETGRAPH_MPPC, M_NOWAIT);
488 m_copydata(m, 0, inlen, (caddr_t)inbuf);
489 if ((d->cfg.bits & MPPC_BIT) != 0)
490 outlen = MPPC_MAX_BLOWUP(inlen);
492 outlen = MPPC_HDRLEN + inlen;
493 MALLOC(outbuf, u_char *, outlen, M_NETGRAPH_MPPC, M_NOWAIT);
494 if (outbuf == NULL) {
495 FREE(inbuf, M_NETGRAPH_MPPC);
499 /* Compress "inbuf" into "outbuf" (if compression enabled) */
500 #ifdef NETGRAPH_MPPC_COMPRESSION
501 if ((d->cfg.bits & MPPC_BIT) != 0) {
502 u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS;
503 u_char *source, *dest;
504 u_long sourceCnt, destCnt;
507 /* Prepare to compress */
510 dest = outbuf + MPPC_HDRLEN;
511 destCnt = outlen - MPPC_HDRLEN;
512 if ((d->cfg.bits & MPPE_STATELESS) == 0)
513 flags |= MPPC_SAVE_HISTORY;
516 rtn = MPPC_Compress(&source, &dest, &sourceCnt,
517 &destCnt, d->history, flags, 0);
519 /* Check return value */
520 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
521 if ((rtn & MPPC_EXPANDED) == 0
522 && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) {
524 header |= MPPC_FLAG_COMPRESSED;
525 if ((rtn & MPPC_RESTART_HISTORY) != 0)
526 header |= MPPC_FLAG_RESTART;
528 d->flushed = (rtn & MPPC_EXPANDED) != 0
529 || (flags & MPPC_SAVE_HISTORY) == 0;
533 /* If we did not compress this packet, copy it to output buffer */
534 if ((header & MPPC_FLAG_COMPRESSED) == 0) {
535 bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen);
536 outlen = MPPC_HDRLEN + inlen;
538 FREE(inbuf, M_NETGRAPH_MPPC);
540 /* Always set the flushed bit in stateless mode */
541 if ((d->cfg.bits & MPPE_STATELESS) != 0)
542 header |= MPPC_FLAG_FLUSHED;
544 /* Now encrypt packet (if encryption enabled) */
545 #ifdef NETGRAPH_MPPC_ENCRYPTION
546 if ((d->cfg.bits & MPPE_BITS) != 0) {
548 /* Set header bits; need to reset key if we say we did */
549 header |= MPPC_FLAG_ENCRYPTED;
550 if ((header & MPPC_FLAG_FLUSHED) != 0)
551 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
553 /* Update key if it's time */
554 if ((d->cfg.bits & MPPE_STATELESS) != 0
555 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
556 ng_mppc_updatekey(d->cfg.bits,
557 d->cfg.startkey, d->key, &d->rc4);
561 rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN,
562 outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN);
566 /* Update sequence number */
570 *((u_int16_t *)outbuf) = htons(header);
572 /* Return packet in an mbuf */
573 *resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL);
574 FREE(outbuf, M_NETGRAPH_MPPC);
575 return (*resultp == NULL ? ENOBUFS : 0);
579 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
580 * The original mbuf is not free'd.
583 ng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
585 const priv_p priv = NG_NODE_PRIVATE(node);
586 struct ng_mppc_dir *const d = &priv->recv;
587 u_int16_t header, cc;
592 /* Pull off header */
593 if (m->m_pkthdr.len < MPPC_HDRLEN)
595 m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header);
596 header = ntohs(header);
597 cc = (header & MPPC_CCOUNT_MASK);
599 /* Copy payload into a contiguous region of memory */
600 len = m->m_pkthdr.len - MPPC_HDRLEN;
601 MALLOC(buf, u_char *, len, M_NETGRAPH_MPPC, M_NOWAIT);
604 m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf);
606 /* Check for an unexpected jump in the sequence number */
607 numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK);
609 /* If flushed bit set, we can always handle packet */
610 if ((header & MPPC_FLAG_FLUSHED) != 0) {
611 #ifdef NETGRAPH_MPPC_COMPRESSION
612 if (d->history != NULL)
613 MPPC_InitDecompressionHistory(d->history);
615 #ifdef NETGRAPH_MPPC_ENCRYPTION
616 if ((d->cfg.bits & MPPE_BITS) != 0) {
619 /* How many times are we going to have to re-key? */
620 rekey = ((d->cfg.bits & MPPE_STATELESS) != 0) ?
621 numLost : (numLost / (MPPE_UPDATE_MASK + 1));
622 if (rekey > MPPE_MAX_REKEY) {
623 log(LOG_ERR, "%s: too many (%d) packets"
624 " dropped, disabling node %p!",
625 __func__, numLost, node);
626 priv->recv.cfg.enable = 0;
630 /* Re-key as necessary to catch up to peer */
631 while (d->cc != cc) {
632 if ((d->cfg.bits & MPPE_STATELESS) != 0
633 || (d->cc & MPPE_UPDATE_MASK)
634 == MPPE_UPDATE_FLAG) {
635 ng_mppc_updatekey(d->cfg.bits,
636 d->cfg.startkey, d->key, &d->rc4);
641 /* Reset key (except in stateless mode, see below) */
642 if ((d->cfg.bits & MPPE_STATELESS) == 0)
643 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
646 d->cc = cc; /* skip over lost seq numbers */
647 numLost = 0; /* act like no packets were lost */
650 /* Can't decode non-sequential packets without a flushed bit */
655 if ((header & MPPC_FLAG_ENCRYPTED) != 0) {
657 /* Are we not expecting encryption? */
658 if ((d->cfg.bits & MPPE_BITS) == 0) {
659 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
660 __func__, "encrypted");
664 #ifdef NETGRAPH_MPPC_ENCRYPTION
665 /* Update key if it's time (always in stateless mode) */
666 if ((d->cfg.bits & MPPE_STATELESS) != 0
667 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
668 ng_mppc_updatekey(d->cfg.bits,
669 d->cfg.startkey, d->key, &d->rc4);
673 rc4_crypt(&d->rc4, buf, buf, len);
677 /* Are we expecting encryption? */
678 if ((d->cfg.bits & MPPE_BITS) != 0) {
679 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
680 __func__, "unencrypted");
685 /* Update coherency count for next time (12 bit arithmetic) */
688 /* Check for unexpected compressed packet */
689 if ((header & MPPC_FLAG_COMPRESSED) != 0
690 && (d->cfg.bits & MPPC_BIT) == 0) {
691 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
692 __func__, "compressed");
694 FREE(buf, M_NETGRAPH_MPPC);
698 #ifdef NETGRAPH_MPPC_COMPRESSION
699 /* Decompress packet */
700 if ((header & MPPC_FLAG_COMPRESSED) != 0) {
701 int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS;
702 u_char *decompbuf, *source, *dest;
703 u_long sourceCnt, destCnt;
706 /* Allocate a buffer for decompressed data */
707 MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE
708 + MPPC_DECOMP_SAFETY, M_NETGRAPH_MPPC, M_NOWAIT);
709 if (decompbuf == NULL) {
710 FREE(buf, M_NETGRAPH_MPPC);
713 decomplen = MPPC_DECOMP_BUFSIZE;
715 /* Prepare to decompress */
720 if ((header & MPPC_FLAG_RESTART) != 0)
721 flags |= MPPC_RESTART_HISTORY;
724 rtn = MPPC_Decompress(&source, &dest,
725 &sourceCnt, &destCnt, d->history, flags);
727 /* Check return value */
728 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
729 if ((rtn & MPPC_DEST_EXHAUSTED) != 0
730 || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) {
731 log(LOG_ERR, "%s: decomp returned 0x%x",
733 FREE(decompbuf, M_NETGRAPH_MPPC);
737 /* Replace compressed data with decompressed data */
738 FREE(buf, M_NETGRAPH_MPPC);
740 len = decomplen - destCnt;
744 /* Return result in an mbuf */
745 *resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL);
746 FREE(buf, M_NETGRAPH_MPPC);
747 return (*resultp == NULL ? ENOBUFS : 0);
751 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
754 ng_mppc_reset_req(node_p node)
756 const priv_p priv = NG_NODE_PRIVATE(node);
757 struct ng_mppc_dir *const d = &priv->xmit;
759 #ifdef NETGRAPH_MPPC_COMPRESSION
760 if (d->history != NULL)
761 MPPC_InitCompressionHistory(d->history);
763 #ifdef NETGRAPH_MPPC_ENCRYPTION
764 if ((d->cfg.bits & MPPE_STATELESS) == 0)
765 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
771 * Generate a new encryption key
774 ng_mppc_getkey(const u_char *h, u_char *h2, int len)
776 static const u_char pad1[10] =
777 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
778 static const u_char pad2[10] =
779 { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, };
784 bzero(&hash, sizeof(hash));
786 SHA1Update(&c, h, len);
787 for (k = 0; k < 4; k++)
788 SHA1Update(&c, pad1, sizeof(pad2));
789 SHA1Update(&c, h2, len);
790 for (k = 0; k < 4; k++)
791 SHA1Update(&c, pad2, sizeof(pad2));
793 bcopy(hash, h2, len);
797 * Update the encryption key
800 ng_mppc_updatekey(u_int32_t bits,
801 u_char *key0, u_char *key, struct rc4_state *rc4)
803 const int keylen = KEYLEN(bits);
805 ng_mppc_getkey(key0, key, keylen);
806 rc4_init(rc4, key, keylen);
807 rc4_crypt(rc4, key, key, keylen);
808 if ((bits & MPPE_40) != 0)
809 bcopy(&ng_mppe_weakenkey, key, 3);
810 else if ((bits & MPPE_56) != 0)
811 bcopy(&ng_mppe_weakenkey, key, 1);
812 rc4_init(rc4, key, keylen);