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)
66 #error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION
69 #ifdef NG_SEPARATE_MALLOC
70 MALLOC_DEFINE(M_NETGRAPH_MPPC, "netgraph_mppc", "netgraph mppc node ");
72 #define M_NETGRAPH_MPPC M_NETGRAPH
75 #ifdef NETGRAPH_MPPC_COMPRESSION
76 /* XXX this file doesn't exist yet, but hopefully someday it will... */
79 #ifdef NETGRAPH_MPPC_ENCRYPTION
80 #include <crypto/rc4/rc4.h>
82 #include <crypto/sha1.h>
84 /* Decompression blowup */
85 #define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */
86 #define MPPC_DECOMP_SAFETY 100 /* plus this much margin */
88 /* MPPC/MPPE header length */
92 #define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8)
95 * When packets are lost with MPPE, we may have to re-key arbitrarily
96 * many times to 'catch up' to the new jumped-ahead sequence number.
97 * Since this can be expensive, we pose a limit on how many re-keyings
98 * we will do at one time to avoid a possible D.O.S. vulnerability.
99 * This should instead be a configurable parameter.
101 #define MPPE_MAX_REKEY 1000
103 /* MPPC packet header bits */
104 #define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */
105 #define MPPC_FLAG_RESTART 0x4000 /* compress history restart */
106 #define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */
107 #define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */
108 #define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */
110 #define MPPE_UPDATE_MASK 0xff /* coherency count when we're */
111 #define MPPE_UPDATE_FLAG 0xff /* supposed to update key */
113 #define MPPC_COMP_OK 0x05
114 #define MPPC_DECOMP_OK 0x05
116 /* Per direction info */
118 struct ng_mppc_config cfg; /* configuration */
119 hook_p hook; /* netgraph hook */
120 u_int16_t cc:12; /* coherency count */
121 u_char flushed; /* clean history (xmit only) */
122 #ifdef NETGRAPH_MPPC_COMPRESSION
123 u_char *history; /* compression history */
125 #ifdef NETGRAPH_MPPC_ENCRYPTION
126 u_char key[MPPE_KEY_LEN]; /* session key */
127 struct rc4_state rc4; /* rc4 state */
131 /* Node private data */
132 struct ng_mppc_private {
133 struct ng_mppc_dir xmit; /* compress/encrypt config */
134 struct ng_mppc_dir recv; /* decompress/decrypt config */
135 ng_ID_t ctrlnode; /* path to controlling node */
137 typedef struct ng_mppc_private *priv_p;
139 /* Netgraph node methods */
140 static ng_constructor_t ng_mppc_constructor;
141 static ng_rcvmsg_t ng_mppc_rcvmsg;
142 static ng_shutdown_t ng_mppc_shutdown;
143 static ng_newhook_t ng_mppc_newhook;
144 static ng_rcvdata_t ng_mppc_rcvdata;
145 static ng_disconnect_t ng_mppc_disconnect;
147 /* Helper functions */
148 static int ng_mppc_compress(node_p node,
149 struct mbuf *m, struct mbuf **resultp);
150 static int ng_mppc_decompress(node_p node,
151 struct mbuf *m, struct mbuf **resultp);
152 static void ng_mppc_getkey(const u_char *h, u_char *h2, int len);
153 static void ng_mppc_updatekey(u_int32_t bits,
154 u_char *key0, u_char *key, struct rc4_state *rc4);
155 static void ng_mppc_reset_req(node_p node);
157 /* Node type descriptor */
158 static struct ng_type ng_mppc_typestruct = {
159 .version = NG_ABI_VERSION,
160 .name = NG_MPPC_NODE_TYPE,
161 .constructor = ng_mppc_constructor,
162 .rcvmsg = ng_mppc_rcvmsg,
163 .shutdown = ng_mppc_shutdown,
164 .newhook = ng_mppc_newhook,
165 .rcvdata = ng_mppc_rcvdata,
166 .disconnect = ng_mppc_disconnect,
168 NETGRAPH_INIT(mppc, &ng_mppc_typestruct);
170 #ifdef NETGRAPH_MPPC_ENCRYPTION
171 /* Depend on separate rc4 module */
172 MODULE_DEPEND(ng_mppc, rc4, 1, 1, 1);
175 /* Fixed bit pattern to weaken keysize down to 40 or 56 bits */
176 static const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e };
178 #define ERROUT(x) do { error = (x); goto done; } while (0)
180 /************************************************************************
182 ************************************************************************/
185 * Node type constructor
188 ng_mppc_constructor(node_p node)
192 /* Allocate private structure */
193 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_MPPC, M_NOWAIT | M_ZERO);
197 NG_NODE_SET_PRIVATE(node, priv);
204 * Give our OK for a hook to be added
207 ng_mppc_newhook(node_p node, hook_p hook, const char *name)
209 const priv_p priv = NG_NODE_PRIVATE(node);
212 /* Check hook name */
213 if (strcmp(name, NG_MPPC_HOOK_COMP) == 0)
214 hookPtr = &priv->xmit.hook;
215 else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0)
216 hookPtr = &priv->recv.hook;
220 /* See if already connected */
221 if (*hookPtr != NULL)
230 * Receive a control message
233 ng_mppc_rcvmsg(node_p node, item_p item, hook_p lasthook)
235 const priv_p priv = NG_NODE_PRIVATE(node);
236 struct ng_mesg *resp = NULL;
240 NGI_GET_MSG(item, msg);
241 switch (msg->header.typecookie) {
242 case NGM_MPPC_COOKIE:
243 switch (msg->header.cmd) {
244 case NGM_MPPC_CONFIG_COMP:
245 case NGM_MPPC_CONFIG_DECOMP:
247 struct ng_mppc_config *const cfg
248 = (struct ng_mppc_config *)msg->data;
250 msg->header.cmd == NGM_MPPC_CONFIG_COMP;
251 struct ng_mppc_dir *const d = isComp ?
252 &priv->xmit : &priv->recv;
254 /* Check configuration */
255 if (msg->header.arglen != sizeof(*cfg))
258 if ((cfg->bits & ~MPPC_VALID_BITS) != 0)
260 #ifndef NETGRAPH_MPPC_COMPRESSION
261 if ((cfg->bits & MPPC_BIT) != 0)
262 ERROUT(EPROTONOSUPPORT);
264 #ifndef NETGRAPH_MPPC_ENCRYPTION
265 if ((cfg->bits & MPPE_BITS) != 0)
266 ERROUT(EPROTONOSUPPORT);
271 /* Save return address so we can send reset-req's */
273 priv->ctrlnode = NGI_RETADDR(item);
275 /* Configuration is OK, reset to it */
278 #ifdef NETGRAPH_MPPC_COMPRESSION
279 /* Initialize state buffers for compression */
280 if (d->history != NULL) {
281 FREE(d->history, M_NETGRAPH_MPPC);
284 if ((cfg->bits & MPPC_BIT) != 0) {
285 MALLOC(d->history, u_char *,
286 isComp ? MPPC_SizeOfCompressionHistory() :
287 MPPC_SizeOfDecompressionHistory(),
288 M_NETGRAPH_MPPC, M_NOWAIT);
289 if (d->history == NULL)
292 MPPC_InitCompressionHistory(d->history);
294 MPPC_InitDecompressionHistory(
300 #ifdef NETGRAPH_MPPC_ENCRYPTION
301 /* Generate initial session keys for encryption */
302 if ((cfg->bits & MPPE_BITS) != 0) {
303 const int keylen = KEYLEN(cfg->bits);
305 bcopy(cfg->startkey, d->key, keylen);
306 ng_mppc_getkey(cfg->startkey, d->key, keylen);
307 if ((cfg->bits & MPPE_40) != 0)
308 bcopy(&ng_mppe_weakenkey, d->key, 3);
309 else if ((cfg->bits & MPPE_56) != 0)
310 bcopy(&ng_mppe_weakenkey, d->key, 1);
311 rc4_init(&d->rc4, d->key, keylen);
315 /* Initialize other state */
321 case NGM_MPPC_RESETREQ:
322 ng_mppc_reset_req(node);
335 NG_RESPOND_MSG(error, node, item, resp);
341 * Receive incoming data on our hook.
344 ng_mppc_rcvdata(hook_p hook, item_p item)
346 const node_p node = NG_HOOK_NODE(hook);
347 const priv_p priv = NG_NODE_PRIVATE(node);
353 /* Compress and/or encrypt */
354 if (hook == priv->xmit.hook) {
355 if (!priv->xmit.cfg.enable) {
360 if ((error = ng_mppc_compress(node, m, &out)) != 0) {
366 NG_FWD_NEW_DATA(error, item, priv->xmit.hook, out);
370 /* Decompress and/or decrypt */
371 if (hook == priv->recv.hook) {
372 if (!priv->recv.cfg.enable) {
377 if ((error = ng_mppc_decompress(node, m, &out)) != 0) {
380 if (error == EINVAL && priv->ctrlnode != 0) {
383 /* Need to send a reset-request */
384 NG_MKMESSAGE(msg, NGM_MPPC_COOKIE,
385 NGM_MPPC_RESETREQ, 0, M_NOWAIT);
388 NG_SEND_MSG_ID(error, node, msg,
394 NG_FWD_NEW_DATA(error, item, priv->recv.hook, out);
399 panic("%s: unknown hook", __func__);
400 #ifdef RESTARTABLE_PANICS
409 ng_mppc_shutdown(node_p node)
411 const priv_p priv = NG_NODE_PRIVATE(node);
413 /* Take down netgraph node */
414 #ifdef NETGRAPH_MPPC_COMPRESSION
415 if (priv->xmit.history != NULL)
416 FREE(priv->xmit.history, M_NETGRAPH_MPPC);
417 if (priv->recv.history != NULL)
418 FREE(priv->recv.history, M_NETGRAPH_MPPC);
420 bzero(priv, sizeof(*priv));
421 FREE(priv, M_NETGRAPH_MPPC);
422 NG_NODE_SET_PRIVATE(node, NULL);
423 NG_NODE_UNREF(node); /* let the node escape */
431 ng_mppc_disconnect(hook_p hook)
433 const node_p node = NG_HOOK_NODE(hook);
434 const priv_p priv = NG_NODE_PRIVATE(node);
436 /* Zero out hook pointer */
437 if (hook == priv->xmit.hook)
438 priv->xmit.hook = NULL;
439 if (hook == priv->recv.hook)
440 priv->recv.hook = NULL;
442 /* Go away if no longer connected */
443 if ((NG_NODE_NUMHOOKS(node) == 0)
444 && NG_NODE_IS_VALID(node))
445 ng_rmnode_self(node);
449 /************************************************************************
451 ************************************************************************/
454 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
455 * The original mbuf is not free'd.
458 ng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
460 const priv_p priv = NG_NODE_PRIVATE(node);
461 struct ng_mppc_dir *const d = &priv->xmit;
462 u_char *inbuf, *outbuf;
470 header |= MPPC_FLAG_FLUSHED;
474 /* Work with contiguous regions of memory */
475 inlen = m->m_pkthdr.len;
476 MALLOC(inbuf, u_char *, inlen, M_NETGRAPH_MPPC, M_NOWAIT);
479 m_copydata(m, 0, inlen, (caddr_t)inbuf);
480 if ((d->cfg.bits & MPPC_BIT) != 0)
481 outlen = MPPC_MAX_BLOWUP(inlen);
483 outlen = MPPC_HDRLEN + inlen;
484 MALLOC(outbuf, u_char *, outlen, M_NETGRAPH_MPPC, M_NOWAIT);
485 if (outbuf == NULL) {
486 FREE(inbuf, M_NETGRAPH_MPPC);
490 /* Compress "inbuf" into "outbuf" (if compression enabled) */
491 #ifdef NETGRAPH_MPPC_COMPRESSION
492 if ((d->cfg.bits & MPPC_BIT) != 0) {
493 u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS;
494 u_char *source, *dest;
495 u_long sourceCnt, destCnt;
498 /* Prepare to compress */
501 dest = outbuf + MPPC_HDRLEN;
502 destCnt = outlen - MPPC_HDRLEN;
503 if ((d->cfg.bits & MPPE_STATELESS) == 0)
504 flags |= MPPC_SAVE_HISTORY;
507 rtn = MPPC_Compress(&source, &dest, &sourceCnt,
508 &destCnt, d->history, flags, 0);
510 /* Check return value */
511 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
512 if ((rtn & MPPC_EXPANDED) == 0
513 && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) {
515 header |= MPPC_FLAG_COMPRESSED;
516 if ((rtn & MPPC_RESTART_HISTORY) != 0)
517 header |= MPPC_FLAG_RESTART;
519 d->flushed = (rtn & MPPC_EXPANDED) != 0
520 || (flags & MPPC_SAVE_HISTORY) == 0;
524 /* If we did not compress this packet, copy it to output buffer */
525 if ((header & MPPC_FLAG_COMPRESSED) == 0) {
526 bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen);
527 outlen = MPPC_HDRLEN + inlen;
529 FREE(inbuf, M_NETGRAPH_MPPC);
531 /* Always set the flushed bit in stateless mode */
532 if ((d->cfg.bits & MPPE_STATELESS) != 0)
533 header |= MPPC_FLAG_FLUSHED;
535 /* Now encrypt packet (if encryption enabled) */
536 #ifdef NETGRAPH_MPPC_ENCRYPTION
537 if ((d->cfg.bits & MPPE_BITS) != 0) {
539 /* Set header bits; need to reset key if we say we did */
540 header |= MPPC_FLAG_ENCRYPTED;
541 if ((header & MPPC_FLAG_FLUSHED) != 0)
542 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
544 /* Update key if it's time */
545 if ((d->cfg.bits & MPPE_STATELESS) != 0
546 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
547 ng_mppc_updatekey(d->cfg.bits,
548 d->cfg.startkey, d->key, &d->rc4);
552 rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN,
553 outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN);
557 /* Update sequence number */
561 *((u_int16_t *)outbuf) = htons(header);
563 /* Return packet in an mbuf */
564 *resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL);
565 FREE(outbuf, M_NETGRAPH_MPPC);
566 return (*resultp == NULL ? ENOBUFS : 0);
570 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
571 * The original mbuf is not free'd.
574 ng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
576 const priv_p priv = NG_NODE_PRIVATE(node);
577 struct ng_mppc_dir *const d = &priv->recv;
578 u_int16_t header, cc;
583 /* Pull off header */
584 if (m->m_pkthdr.len < MPPC_HDRLEN)
586 m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header);
587 header = ntohs(header);
588 cc = (header & MPPC_CCOUNT_MASK);
590 /* Copy payload into a contiguous region of memory */
591 len = m->m_pkthdr.len - MPPC_HDRLEN;
592 MALLOC(buf, u_char *, len, M_NETGRAPH_MPPC, M_NOWAIT);
595 m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf);
597 /* Check for an unexpected jump in the sequence number */
598 numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK);
600 /* If flushed bit set, we can always handle packet */
601 if ((header & MPPC_FLAG_FLUSHED) != 0) {
602 #ifdef NETGRAPH_MPPC_COMPRESSION
603 if (d->history != NULL)
604 MPPC_InitDecompressionHistory(d->history);
606 #ifdef NETGRAPH_MPPC_ENCRYPTION
607 if ((d->cfg.bits & MPPE_BITS) != 0) {
610 /* How many times are we going to have to re-key? */
611 rekey = ((d->cfg.bits & MPPE_STATELESS) != 0) ?
612 numLost : (numLost / (MPPE_UPDATE_MASK + 1));
613 if (rekey > MPPE_MAX_REKEY) {
614 log(LOG_ERR, "%s: too many (%d) packets"
615 " dropped, disabling node %p!",
616 __func__, numLost, node);
617 priv->recv.cfg.enable = 0;
621 /* Re-key as necessary to catch up to peer */
622 while (d->cc != cc) {
623 if ((d->cfg.bits & MPPE_STATELESS) != 0
624 || (d->cc & MPPE_UPDATE_MASK)
625 == MPPE_UPDATE_FLAG) {
626 ng_mppc_updatekey(d->cfg.bits,
627 d->cfg.startkey, d->key, &d->rc4);
632 /* Reset key (except in stateless mode, see below) */
633 if ((d->cfg.bits & MPPE_STATELESS) == 0)
634 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
637 d->cc = cc; /* skip over lost seq numbers */
638 numLost = 0; /* act like no packets were lost */
641 /* Can't decode non-sequential packets without a flushed bit */
646 if ((header & MPPC_FLAG_ENCRYPTED) != 0) {
648 /* Are we not expecting encryption? */
649 if ((d->cfg.bits & MPPE_BITS) == 0) {
650 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
651 __func__, "encrypted");
655 #ifdef NETGRAPH_MPPC_ENCRYPTION
656 /* Update key if it's time (always in stateless mode) */
657 if ((d->cfg.bits & MPPE_STATELESS) != 0
658 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
659 ng_mppc_updatekey(d->cfg.bits,
660 d->cfg.startkey, d->key, &d->rc4);
664 rc4_crypt(&d->rc4, buf, buf, len);
668 /* Are we expecting encryption? */
669 if ((d->cfg.bits & MPPE_BITS) != 0) {
670 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
671 __func__, "unencrypted");
676 /* Update coherency count for next time (12 bit arithmetic) */
679 /* Check for unexpected compressed packet */
680 if ((header & MPPC_FLAG_COMPRESSED) != 0
681 && (d->cfg.bits & MPPC_BIT) == 0) {
682 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
683 __func__, "compressed");
685 FREE(buf, M_NETGRAPH_MPPC);
689 #ifdef NETGRAPH_MPPC_COMPRESSION
690 /* Decompress packet */
691 if ((header & MPPC_FLAG_COMPRESSED) != 0) {
692 int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS;
693 u_char *decompbuf, *source, *dest;
694 u_long sourceCnt, destCnt;
697 /* Allocate a buffer for decompressed data */
698 MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE
699 + MPPC_DECOMP_SAFETY, M_NETGRAPH_MPPC, M_NOWAIT);
700 if (decompbuf == NULL) {
701 FREE(buf, M_NETGRAPH_MPPC);
704 decomplen = MPPC_DECOMP_BUFSIZE;
706 /* Prepare to decompress */
711 if ((header & MPPC_FLAG_RESTART) != 0)
712 flags |= MPPC_RESTART_HISTORY;
715 rtn = MPPC_Decompress(&source, &dest,
716 &sourceCnt, &destCnt, d->history, flags);
718 /* Check return value */
719 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
720 if ((rtn & MPPC_DEST_EXHAUSTED) != 0
721 || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) {
722 log(LOG_ERR, "%s: decomp returned 0x%x",
724 FREE(decompbuf, M_NETGRAPH_MPPC);
728 /* Replace compressed data with decompressed data */
729 FREE(buf, M_NETGRAPH_MPPC);
731 len = decomplen - destCnt;
735 /* Return result in an mbuf */
736 *resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL);
737 FREE(buf, M_NETGRAPH_MPPC);
738 return (*resultp == NULL ? ENOBUFS : 0);
742 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
745 ng_mppc_reset_req(node_p node)
747 const priv_p priv = NG_NODE_PRIVATE(node);
748 struct ng_mppc_dir *const d = &priv->xmit;
750 #ifdef NETGRAPH_MPPC_COMPRESSION
751 if (d->history != NULL)
752 MPPC_InitCompressionHistory(d->history);
754 #ifdef NETGRAPH_MPPC_ENCRYPTION
755 if ((d->cfg.bits & MPPE_STATELESS) == 0)
756 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
762 * Generate a new encryption key
765 ng_mppc_getkey(const u_char *h, u_char *h2, int len)
767 static const u_char pad1[10] =
768 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
769 static const u_char pad2[10] =
770 { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, };
775 bzero(&hash, sizeof(hash));
777 SHA1Update(&c, h, len);
778 for (k = 0; k < 4; k++)
779 SHA1Update(&c, pad1, sizeof(pad2));
780 SHA1Update(&c, h2, len);
781 for (k = 0; k < 4; k++)
782 SHA1Update(&c, pad2, sizeof(pad2));
784 bcopy(hash, h2, len);
788 * Update the encryption key
791 ng_mppc_updatekey(u_int32_t bits,
792 u_char *key0, u_char *key, struct rc4_state *rc4)
794 const int keylen = KEYLEN(bits);
796 ng_mppc_getkey(key0, key, keylen);
797 rc4_init(rc4, key, keylen);
798 rc4_crypt(rc4, key, key, keylen);
799 if ((bits & MPPE_40) != 0)
800 bcopy(&ng_mppe_weakenkey, key, 3);
801 else if ((bits & MPPE_56) != 0)
802 bcopy(&ng_mppe_weakenkey, key, 1);
803 rc4_init(rc4, key, keylen);