1 /*
2 * ng_mppc.c
3 */
4
5 /*-
6 * Copyright (c) 1996-2000 Whistle Communications, Inc.
7 * All rights reserved.
8 *
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.
19 *
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
36 * OF SUCH DAMAGE.
37 *
38 * Author: Archie Cobbs <archie@freebsd.org>
39 *
40 * $Whistle: ng_mppc.c,v 1.4 1999/11/25 00:10:12 archie Exp $
41 * $FreeBSD: releng/5.4/sys/netgraph/ng_mppc.c 141090 2005-01-31 23:27:04Z imp $
42 */
43
44 /*
45 * Microsoft PPP compression (MPPC) and encryption (MPPE) netgraph node type.
46 *
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.
49 */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/mbuf.h>
55 #include <sys/malloc.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 NG_SEPARATE_MALLOC
70 MALLOC_DEFINE(M_NETGRAPH_MPPC, "netgraph_mppc", "netgraph mppc node ");
71 #else
72 #define M_NETGRAPH_MPPC M_NETGRAPH
73 #endif
74
75 #ifdef NETGRAPH_MPPC_COMPRESSION
76 /* XXX this file doesn't exist yet, but hopefully someday it will... */
77 #include <net/mppc.h>
78 #endif
79 #ifdef NETGRAPH_MPPC_ENCRYPTION
80 #include <crypto/rc4/rc4.h>
81 #endif
82 #include <crypto/sha1.h>
83
84 /* Decompression blowup */
85 #define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */
86 #define MPPC_DECOMP_SAFETY 100 /* plus this much margin */
87
88 /* MPPC/MPPE header length */
89 #define MPPC_HDRLEN 2
90
91 /* Key length */
92 #define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8)
93
94 /*
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.
100 */
101 #define MPPE_MAX_REKEY 1000
102
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 */
109
110 #define MPPE_UPDATE_MASK 0xff /* coherency count when we're */
111 #define MPPE_UPDATE_FLAG 0xff /* supposed to update key */
112
113 #define MPPC_COMP_OK 0x05
114 #define MPPC_DECOMP_OK 0x05
115
116 /* Per direction info */
117 struct ng_mppc_dir {
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 */
124 #endif
125 #ifdef NETGRAPH_MPPC_ENCRYPTION
126 u_char key[MPPE_KEY_LEN]; /* session key */
127 struct rc4_state rc4; /* rc4 state */
128 #endif
129 };
130
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 */
136 };
137 typedef struct ng_mppc_private *priv_p;
138
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;
146
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);
156
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,
167 };
168 NETGRAPH_INIT(mppc, &ng_mppc_typestruct);
169
170 #ifdef NETGRAPH_MPPC_ENCRYPTION
171 /* Depend on separate rc4 module */
172 MODULE_DEPEND(ng_mppc, rc4, 1, 1, 1);
173 #endif
174
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 };
177
178 #define ERROUT(x) do { error = (x); goto done; } while (0)
179
180 /************************************************************************
181 NETGRAPH NODE STUFF
182 ************************************************************************/
183
184 /*
185 * Node type constructor
186 */
187 static int
188 ng_mppc_constructor(node_p node)
189 {
190 priv_p priv;
191
192 /* Allocate private structure */
193 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_MPPC, M_NOWAIT | M_ZERO);
194 if (priv == NULL)
195 return (ENOMEM);
196
197 NG_NODE_SET_PRIVATE(node, priv);
198
199 /* Done */
200 return (0);
201 }
202
203 /*
204 * Give our OK for a hook to be added
205 */
206 static int
207 ng_mppc_newhook(node_p node, hook_p hook, const char *name)
208 {
209 const priv_p priv = NG_NODE_PRIVATE(node);
210 hook_p *hookPtr;
211
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;
217 else
218 return (EINVAL);
219
220 /* See if already connected */
221 if (*hookPtr != NULL)
222 return (EISCONN);
223
224 /* OK */
225 *hookPtr = hook;
226 return (0);
227 }
228
229 /*
230 * Receive a control message
231 */
232 static int
233 ng_mppc_rcvmsg(node_p node, item_p item, hook_p lasthook)
234 {
235 const priv_p priv = NG_NODE_PRIVATE(node);
236 struct ng_mesg *resp = NULL;
237 int error = 0;
238 struct ng_mesg *msg;
239
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:
246 {
247 struct ng_mppc_config *const cfg
248 = (struct ng_mppc_config *)msg->data;
249 const int isComp =
250 msg->header.cmd == NGM_MPPC_CONFIG_COMP;
251 struct ng_mppc_dir *const d = isComp ?
252 &priv->xmit : &priv->recv;
253
254 /* Check configuration */
255 if (msg->header.arglen != sizeof(*cfg))
256 ERROUT(EINVAL);
257 if (cfg->enable) {
258 if ((cfg->bits & ~MPPC_VALID_BITS) != 0)
259 ERROUT(EINVAL);
260 #ifndef NETGRAPH_MPPC_COMPRESSION
261 if ((cfg->bits & MPPC_BIT) != 0)
262 ERROUT(EPROTONOSUPPORT);
263 #endif
264 #ifndef NETGRAPH_MPPC_ENCRYPTION
265 if ((cfg->bits & MPPE_BITS) != 0)
266 ERROUT(EPROTONOSUPPORT);
267 #endif
268 } else
269 cfg->bits = 0;
270
271 /* Save return address so we can send reset-req's */
272 if (!isComp)
273 priv->ctrlnode = NGI_RETADDR(item);
274
275 /* Configuration is OK, reset to it */
276 d->cfg = *cfg;
277
278 #ifdef NETGRAPH_MPPC_COMPRESSION
279 /* Initialize state buffers for compression */
280 if (d->history != NULL) {
281 FREE(d->history, M_NETGRAPH_MPPC);
282 d->history = NULL;
283 }
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)
290 ERROUT(ENOMEM);
291 if (isComp)
292 MPPC_InitCompressionHistory(d->history);
293 else {
294 MPPC_InitDecompressionHistory(
295 d->history);
296 }
297 }
298 #endif
299
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);
304
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);
312 }
313 #endif
314
315 /* Initialize other state */
316 d->cc = 0;
317 d->flushed = 0;
318 break;
319 }
320
321 case NGM_MPPC_RESETREQ:
322 ng_mppc_reset_req(node);
323 break;
324
325 default:
326 error = EINVAL;
327 break;
328 }
329 break;
330 default:
331 error = EINVAL;
332 break;
333 }
334 done:
335 NG_RESPOND_MSG(error, node, item, resp);
336 NG_FREE_MSG(msg);
337 return (error);
338 }
339
340 /*
341 * Receive incoming data on our hook.
342 */
343 static int
344 ng_mppc_rcvdata(hook_p hook, item_p item)
345 {
346 const node_p node = NG_HOOK_NODE(hook);
347 const priv_p priv = NG_NODE_PRIVATE(node);
348 struct mbuf *out;
349 int error;
350 struct mbuf *m;
351
352 NGI_GET_M(item, m);
353 /* Compress and/or encrypt */
354 if (hook == priv->xmit.hook) {
355 if (!priv->xmit.cfg.enable) {
356 NG_FREE_M(m);
357 NG_FREE_ITEM(item);
358 return (ENXIO);
359 }
360 if ((error = ng_mppc_compress(node, m, &out)) != 0) {
361 NG_FREE_M(m);
362 NG_FREE_ITEM(item);
363 return(error);
364 }
365 NG_FREE_M(m);
366 NG_FWD_NEW_DATA(error, item, priv->xmit.hook, out);
367 return (error);
368 }
369
370 /* Decompress and/or decrypt */
371 if (hook == priv->recv.hook) {
372 if (!priv->recv.cfg.enable) {
373 NG_FREE_M(m);
374 NG_FREE_ITEM(item);
375 return (ENXIO);
376 }
377 if ((error = ng_mppc_decompress(node, m, &out)) != 0) {
378 NG_FREE_M(m);
379 NG_FREE_ITEM(item);
380 if (error == EINVAL && priv->ctrlnode != 0) {
381 struct ng_mesg *msg;
382
383 /* Need to send a reset-request */
384 NG_MKMESSAGE(msg, NGM_MPPC_COOKIE,
385 NGM_MPPC_RESETREQ, 0, M_NOWAIT);
386 if (msg == NULL)
387 return (error);
388 NG_SEND_MSG_ID(error, node, msg,
389 priv->ctrlnode, 0);
390 }
391 return (error);
392 }
393 NG_FREE_M(m);
394 NG_FWD_NEW_DATA(error, item, priv->recv.hook, out);
395 return (error);
396 }
397
398 /* Oops */
399 panic("%s: unknown hook", __func__);
400 #ifdef RESTARTABLE_PANICS
401 return (EINVAL);
402 #endif
403 }
404
405 /*
406 * Destroy node
407 */
408 static int
409 ng_mppc_shutdown(node_p node)
410 {
411 const priv_p priv = NG_NODE_PRIVATE(node);
412
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);
419 #endif
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 */
424 return (0);
425 }
426
427 /*
428 * Hook disconnection
429 */
430 static int
431 ng_mppc_disconnect(hook_p hook)
432 {
433 const node_p node = NG_HOOK_NODE(hook);
434 const priv_p priv = NG_NODE_PRIVATE(node);
435
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;
441
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);
446 return (0);
447 }
448
449 /************************************************************************
450 HELPER STUFF
451 ************************************************************************/
452
453 /*
454 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
455 * The original mbuf is not free'd.
456 */
457 static int
458 ng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
459 {
460 const priv_p priv = NG_NODE_PRIVATE(node);
461 struct ng_mppc_dir *const d = &priv->xmit;
462 u_char *inbuf, *outbuf;
463 int outlen, inlen;
464 u_int16_t header;
465
466 /* Initialize */
467 *resultp = NULL;
468 header = d->cc;
469 if (d->flushed) {
470 header |= MPPC_FLAG_FLUSHED;
471 d->flushed = 0;
472 }
473
474 /* Work with contiguous regions of memory */
475 inlen = m->m_pkthdr.len;
476 MALLOC(inbuf, u_char *, inlen, M_NETGRAPH_MPPC, M_NOWAIT);
477 if (inbuf == NULL)
478 return (ENOMEM);
479 m_copydata(m, 0, inlen, (caddr_t)inbuf);
480 if ((d->cfg.bits & MPPC_BIT) != 0)
481 outlen = MPPC_MAX_BLOWUP(inlen);
482 else
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);
487 return (ENOMEM);
488 }
489
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;
496 int rtn;
497
498 /* Prepare to compress */
499 source = inbuf;
500 sourceCnt = inlen;
501 dest = outbuf + MPPC_HDRLEN;
502 destCnt = outlen - MPPC_HDRLEN;
503 if ((d->cfg.bits & MPPE_STATELESS) == 0)
504 flags |= MPPC_SAVE_HISTORY;
505
506 /* Compress */
507 rtn = MPPC_Compress(&source, &dest, &sourceCnt,
508 &destCnt, d->history, flags, 0);
509
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) {
514 outlen -= destCnt;
515 header |= MPPC_FLAG_COMPRESSED;
516 if ((rtn & MPPC_RESTART_HISTORY) != 0)
517 header |= MPPC_FLAG_RESTART;
518 }
519 d->flushed = (rtn & MPPC_EXPANDED) != 0
520 || (flags & MPPC_SAVE_HISTORY) == 0;
521 }
522 #endif
523
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;
528 }
529 FREE(inbuf, M_NETGRAPH_MPPC);
530
531 /* Always set the flushed bit in stateless mode */
532 if ((d->cfg.bits & MPPE_STATELESS) != 0)
533 header |= MPPC_FLAG_FLUSHED;
534
535 /* Now encrypt packet (if encryption enabled) */
536 #ifdef NETGRAPH_MPPC_ENCRYPTION
537 if ((d->cfg.bits & MPPE_BITS) != 0) {
538
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));
543
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);
549 }
550
551 /* Encrypt packet */
552 rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN,
553 outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN);
554 }
555 #endif
556
557 /* Update sequence number */
558 d->cc++;
559
560 /* Install header */
561 *((u_int16_t *)outbuf) = htons(header);
562
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);
567 }
568
569 /*
570 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
571 * The original mbuf is not free'd.
572 */
573 static int
574 ng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
575 {
576 const priv_p priv = NG_NODE_PRIVATE(node);
577 struct ng_mppc_dir *const d = &priv->recv;
578 u_int16_t header, cc;
579 u_int numLost;
580 u_char *buf;
581 int len;
582
583 /* Pull off header */
584 if (m->m_pkthdr.len < MPPC_HDRLEN)
585 return (EINVAL);
586 m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header);
587 header = ntohs(header);
588 cc = (header & MPPC_CCOUNT_MASK);
589
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);
593 if (buf == NULL)
594 return (ENOMEM);
595 m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf);
596
597 /* Check for an unexpected jump in the sequence number */
598 numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK);
599
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);
605 #endif
606 #ifdef NETGRAPH_MPPC_ENCRYPTION
607 if ((d->cfg.bits & MPPE_BITS) != 0) {
608 u_int rekey;
609
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;
618 goto failed;
619 }
620
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);
628 }
629 d->cc++;
630 }
631
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));
635 }
636 #endif
637 d->cc = cc; /* skip over lost seq numbers */
638 numLost = 0; /* act like no packets were lost */
639 }
640
641 /* Can't decode non-sequential packets without a flushed bit */
642 if (numLost != 0)
643 goto failed;
644
645 /* Decrypt packet */
646 if ((header & MPPC_FLAG_ENCRYPTED) != 0) {
647
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");
652 goto failed;
653 }
654
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);
661 }
662
663 /* Decrypt packet */
664 rc4_crypt(&d->rc4, buf, buf, len);
665 #endif
666 } else {
667
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");
672 goto failed;
673 }
674 }
675
676 /* Update coherency count for next time (12 bit arithmetic) */
677 d->cc++;
678
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");
684 failed:
685 FREE(buf, M_NETGRAPH_MPPC);
686 return (EINVAL);
687 }
688
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;
695 int decomplen, rtn;
696
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);
702 return (ENOMEM);
703 }
704 decomplen = MPPC_DECOMP_BUFSIZE;
705
706 /* Prepare to decompress */
707 source = buf;
708 sourceCnt = len;
709 dest = decompbuf;
710 destCnt = decomplen;
711 if ((header & MPPC_FLAG_RESTART) != 0)
712 flags |= MPPC_RESTART_HISTORY;
713
714 /* Decompress */
715 rtn = MPPC_Decompress(&source, &dest,
716 &sourceCnt, &destCnt, d->history, flags);
717
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",
723 __func__, rtn);
724 FREE(decompbuf, M_NETGRAPH_MPPC);
725 goto failed;
726 }
727
728 /* Replace compressed data with decompressed data */
729 FREE(buf, M_NETGRAPH_MPPC);
730 buf = decompbuf;
731 len = decomplen - destCnt;
732 }
733 #endif
734
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);
739 }
740
741 /*
742 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
743 */
744 static void
745 ng_mppc_reset_req(node_p node)
746 {
747 const priv_p priv = NG_NODE_PRIVATE(node);
748 struct ng_mppc_dir *const d = &priv->xmit;
749
750 #ifdef NETGRAPH_MPPC_COMPRESSION
751 if (d->history != NULL)
752 MPPC_InitCompressionHistory(d->history);
753 #endif
754 #ifdef NETGRAPH_MPPC_ENCRYPTION
755 if ((d->cfg.bits & MPPE_STATELESS) == 0)
756 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
757 #endif
758 d->flushed = 1;
759 }
760
761 /*
762 * Generate a new encryption key
763 */
764 static void
765 ng_mppc_getkey(const u_char *h, u_char *h2, int len)
766 {
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, };
771 u_char hash[20];
772 SHA1_CTX c;
773 int k;
774
775 bzero(&hash, sizeof(hash));
776 SHA1Init(&c);
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));
783 SHA1Final(hash, &c);
784 bcopy(hash, h2, len);
785 }
786
787 /*
788 * Update the encryption key
789 */
790 static void
791 ng_mppc_updatekey(u_int32_t bits,
792 u_char *key0, u_char *key, struct rc4_state *rc4)
793 {
794 const int keylen = KEYLEN(bits);
795
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);
804 }
805
Cache object: f7e2f7afc656578850ca141df35ab75b
|