Deleted Added
full compact
ng_mppc.c (69922) ng_mppc.c (70159)
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@freebsd.org>
38 *
39 * $Whistle: ng_mppc.c,v 1.4 1999/11/25 00:10:12 archie Exp $
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@freebsd.org>
38 *
39 * $Whistle: ng_mppc.c,v 1.4 1999/11/25 00:10:12 archie Exp $
40 * $FreeBSD: head/sys/netgraph/ng_mppc.c 69922 2000-12-12 18:52:14Z julian $
40 * $FreeBSD: head/sys/netgraph/ng_mppc.c 70159 2000-12-18 20:03:32Z julian $
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/errno.h>
56#include <sys/syslog.h>
57
58#include <netgraph/ng_message.h>
59#include <netgraph/netgraph.h>
60#include <netgraph/ng_mppc.h>
61
62#include "opt_netgraph.h"
63
64#if !defined(NETGRAPH_MPPC_COMPRESSION) && !defined(NETGRAPH_MPPC_ENCRYPTION)
65#error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION
66#endif
67
68#ifdef NETGRAPH_MPPC_COMPRESSION
69/* XXX this file doesn't exist yet, but hopefully someday it will... */
70#include <net/mppc.h>
71#endif
72#ifdef NETGRAPH_MPPC_ENCRYPTION
73#include <crypto/rc4/rc4.h>
74#endif
75#include <crypto/sha1.h>
76
77/* Decompression blowup */
78#define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */
79#define MPPC_DECOMP_SAFETY 100 /* plus this much margin */
80
81/* MPPC/MPPE header length */
82#define MPPC_HDRLEN 2
83
84/* Key length */
85#define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8)
86
87/* What sequence number jump is too far */
88#define MPPC_INSANE_JUMP 256
89
90/* MPPC packet header bits */
91#define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */
92#define MPPC_FLAG_RESTART 0x4000 /* compress history restart */
93#define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */
94#define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */
95#define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */
96
97#define MPPE_UPDATE_MASK 0xff /* coherency count when we're */
98#define MPPE_UPDATE_FLAG 0xff /* supposed to update key */
99
100#define MPPC_COMP_OK 0x05
101#define MPPC_DECOMP_OK 0x05
102
103/* Per direction info */
104struct ng_mppc_dir {
105 struct ng_mppc_config cfg; /* configuration */
106 hook_p hook; /* netgraph hook */
107 u_int16_t cc:12; /* coherency count */
108 u_char flushed; /* clean history (xmit only) */
109#ifdef NETGRAPH_MPPC_COMPRESSION
110 u_char *history; /* compression history */
111#endif
112#ifdef NETGRAPH_MPPC_ENCRYPTION
113 u_char key[MPPE_KEY_LEN]; /* session key */
114 struct rc4_state rc4; /* rc4 state */
115#endif
116};
117
118/* Node private data */
119struct ng_mppc_private {
120 struct ng_mppc_dir xmit; /* compress/encrypt config */
121 struct ng_mppc_dir recv; /* decompress/decrypt config */
122 char *ctrlpath; /* path to controlling node */
123};
124typedef struct ng_mppc_private *priv_p;
125
126/* Netgraph node methods */
127static ng_constructor_t ng_mppc_constructor;
128static ng_rcvmsg_t ng_mppc_rcvmsg;
129static ng_shutdown_t ng_mppc_rmnode;
130static ng_newhook_t ng_mppc_newhook;
131static ng_rcvdata_t ng_mppc_rcvdata;
132static ng_disconnect_t ng_mppc_disconnect;
133
134/* Helper functions */
135static int ng_mppc_compress(node_p node,
136 struct mbuf *m, struct mbuf **resultp);
137static int ng_mppc_decompress(node_p node,
138 struct mbuf *m, struct mbuf **resultp);
139static void ng_mppc_getkey(const u_char *h, u_char *h2, int len);
140static void ng_mppc_updatekey(u_int32_t bits,
141 u_char *key0, u_char *key, struct rc4_state *rc4);
142static void ng_mppc_reset_req(node_p node);
143
144/* Node type descriptor */
145static struct ng_type ng_mppc_typestruct = {
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/errno.h>
56#include <sys/syslog.h>
57
58#include <netgraph/ng_message.h>
59#include <netgraph/netgraph.h>
60#include <netgraph/ng_mppc.h>
61
62#include "opt_netgraph.h"
63
64#if !defined(NETGRAPH_MPPC_COMPRESSION) && !defined(NETGRAPH_MPPC_ENCRYPTION)
65#error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION
66#endif
67
68#ifdef NETGRAPH_MPPC_COMPRESSION
69/* XXX this file doesn't exist yet, but hopefully someday it will... */
70#include <net/mppc.h>
71#endif
72#ifdef NETGRAPH_MPPC_ENCRYPTION
73#include <crypto/rc4/rc4.h>
74#endif
75#include <crypto/sha1.h>
76
77/* Decompression blowup */
78#define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */
79#define MPPC_DECOMP_SAFETY 100 /* plus this much margin */
80
81/* MPPC/MPPE header length */
82#define MPPC_HDRLEN 2
83
84/* Key length */
85#define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8)
86
87/* What sequence number jump is too far */
88#define MPPC_INSANE_JUMP 256
89
90/* MPPC packet header bits */
91#define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */
92#define MPPC_FLAG_RESTART 0x4000 /* compress history restart */
93#define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */
94#define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */
95#define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */
96
97#define MPPE_UPDATE_MASK 0xff /* coherency count when we're */
98#define MPPE_UPDATE_FLAG 0xff /* supposed to update key */
99
100#define MPPC_COMP_OK 0x05
101#define MPPC_DECOMP_OK 0x05
102
103/* Per direction info */
104struct ng_mppc_dir {
105 struct ng_mppc_config cfg; /* configuration */
106 hook_p hook; /* netgraph hook */
107 u_int16_t cc:12; /* coherency count */
108 u_char flushed; /* clean history (xmit only) */
109#ifdef NETGRAPH_MPPC_COMPRESSION
110 u_char *history; /* compression history */
111#endif
112#ifdef NETGRAPH_MPPC_ENCRYPTION
113 u_char key[MPPE_KEY_LEN]; /* session key */
114 struct rc4_state rc4; /* rc4 state */
115#endif
116};
117
118/* Node private data */
119struct ng_mppc_private {
120 struct ng_mppc_dir xmit; /* compress/encrypt config */
121 struct ng_mppc_dir recv; /* decompress/decrypt config */
122 char *ctrlpath; /* path to controlling node */
123};
124typedef struct ng_mppc_private *priv_p;
125
126/* Netgraph node methods */
127static ng_constructor_t ng_mppc_constructor;
128static ng_rcvmsg_t ng_mppc_rcvmsg;
129static ng_shutdown_t ng_mppc_rmnode;
130static ng_newhook_t ng_mppc_newhook;
131static ng_rcvdata_t ng_mppc_rcvdata;
132static ng_disconnect_t ng_mppc_disconnect;
133
134/* Helper functions */
135static int ng_mppc_compress(node_p node,
136 struct mbuf *m, struct mbuf **resultp);
137static int ng_mppc_decompress(node_p node,
138 struct mbuf *m, struct mbuf **resultp);
139static void ng_mppc_getkey(const u_char *h, u_char *h2, int len);
140static void ng_mppc_updatekey(u_int32_t bits,
141 u_char *key0, u_char *key, struct rc4_state *rc4);
142static void ng_mppc_reset_req(node_p node);
143
144/* Node type descriptor */
145static struct ng_type ng_mppc_typestruct = {
146 NG_VERSION,
146 NG_ABI_VERSION,
147 NG_MPPC_NODE_TYPE,
148 NULL,
149 ng_mppc_constructor,
150 ng_mppc_rcvmsg,
151 ng_mppc_rmnode,
152 ng_mppc_newhook,
153 NULL,
154 NULL,
155 ng_mppc_rcvdata,
156 ng_mppc_disconnect,
157 NULL
158};
159NETGRAPH_INIT(mppc, &ng_mppc_typestruct);
160
161/* Fixed bit pattern to weaken keysize down to 40 bits */
162static const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e };
163
164#define ERROUT(x) do { error = (x); goto done; } while (0)
165
166/************************************************************************
167 NETGRAPH NODE STUFF
168 ************************************************************************/
169
170/*
171 * Node type constructor
172 */
173static int
174ng_mppc_constructor(node_p *nodep)
175{
176 priv_p priv;
177 int error;
178
179 /* Allocate private structure */
180 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
181 if (priv == NULL)
182 return (ENOMEM);
183
184 /* Call generic node constructor */
185 if ((error = ng_make_node_common(&ng_mppc_typestruct, nodep))) {
186 FREE(priv, M_NETGRAPH);
187 return (error);
188 }
189 (*nodep)->private = priv;
190
191 /* Done */
192 return (0);
193}
194
195/*
196 * Give our OK for a hook to be added
197 */
198static int
199ng_mppc_newhook(node_p node, hook_p hook, const char *name)
200{
201 const priv_p priv = node->private;
202 hook_p *hookPtr;
203
204 /* Check hook name */
205 if (strcmp(name, NG_MPPC_HOOK_COMP) == 0)
206 hookPtr = &priv->xmit.hook;
207 else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0)
208 hookPtr = &priv->recv.hook;
209 else
210 return (EINVAL);
211
212 /* See if already connected */
213 if (*hookPtr != NULL)
214 return (EISCONN);
215
216 /* OK */
217 *hookPtr = hook;
218 return (0);
219}
220
221/*
222 * Receive a control message
223 */
224static int
225ng_mppc_rcvmsg(node_p node, struct ng_mesg *msg,
226 const char *raddr, struct ng_mesg **rptr, hook_p lasthook)
227{
228 const priv_p priv = node->private;
229 struct ng_mesg *resp = NULL;
230 int error = 0;
231
232 switch (msg->header.typecookie) {
233 case NGM_MPPC_COOKIE:
234 switch (msg->header.cmd) {
235 case NGM_MPPC_CONFIG_COMP:
236 case NGM_MPPC_CONFIG_DECOMP:
237 {
238 struct ng_mppc_config *const cfg
239 = (struct ng_mppc_config *)msg->data;
240 const int isComp =
241 msg->header.cmd == NGM_MPPC_CONFIG_COMP;
242 struct ng_mppc_dir *const d = isComp ?
243 &priv->xmit : &priv->recv;
244
245 /* Check configuration */
246 if (msg->header.arglen != sizeof(*cfg))
247 ERROUT(EINVAL);
248 if (cfg->enable) {
249 if ((cfg->bits & ~MPPC_VALID_BITS) != 0)
250 ERROUT(EINVAL);
251#ifndef NETGRAPH_MPPC_COMPRESSION
252 if ((cfg->bits & MPPC_BIT) != 0)
253 ERROUT(EPROTONOSUPPORT);
254#endif
255#ifndef NETGRAPH_MPPC_ENCRYPTION
256 if ((cfg->bits & MPPE_BITS) != 0)
257 ERROUT(EPROTONOSUPPORT);
258#endif
259 } else
260 cfg->bits = 0;
261
262 /* Save return address so we can send reset-req's */
263 if (priv->ctrlpath != NULL) {
264 FREE(priv->ctrlpath, M_NETGRAPH);
265 priv->ctrlpath = NULL;
266 }
267 if (!isComp && raddr != NULL) {
268 MALLOC(priv->ctrlpath, char *,
269 strlen(raddr) + 1, M_NETGRAPH, M_NOWAIT);
270 if (priv->ctrlpath == NULL)
271 ERROUT(ENOMEM);
272 strcpy(priv->ctrlpath, raddr);
273 }
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);
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, 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_128) == 0) {
308 bcopy(&ng_mppe_weakenkey, d->key,
309 sizeof(ng_mppe_weakenkey));
310 }
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 if (rptr)
335 *rptr = resp;
336 else if (resp)
337 FREE(resp, M_NETGRAPH);
338
339done:
340 FREE(msg, M_NETGRAPH);
341 return (error);
342}
343
344/*
345 * Receive incoming data on our hook.
346 */
347static int
348ng_mppc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
349 struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp)
350{
351 const node_p node = hook->node;
352 const priv_p priv = node->private;
353 struct mbuf *out;
354 int error;
355
356 /* Compress and/or encrypt */
357 if (hook == priv->xmit.hook) {
358 if (!priv->xmit.cfg.enable) {
359 NG_FREE_DATA(m, meta);
360 return (ENXIO);
361 }
362 if ((error = ng_mppc_compress(node, m, &out)) != 0) {
363 NG_FREE_DATA(m, meta);
364 return(error);
365 }
366 m_freem(m);
367 NG_SEND_DATA(error, priv->xmit.hook, out, meta);
368 return (error);
369 }
370
371 /* Decompress and/or decrypt */
372 if (hook == priv->recv.hook) {
373 if (!priv->recv.cfg.enable) {
374 NG_FREE_DATA(m, meta);
375 return (ENXIO);
376 }
377 if ((error = ng_mppc_decompress(node, m, &out)) != 0) {
378 NG_FREE_DATA(m, meta);
379 if (error == EINVAL && priv->ctrlpath != NULL) {
380 struct ng_mesg *msg;
381
382 /* Need to send a reset-request */
383 NG_MKMESSAGE(msg, NGM_MPPC_COOKIE,
384 NGM_MPPC_RESETREQ, 0, M_NOWAIT);
385 if (msg == NULL)
386 return (error);
387 /* XXX can we use a hook instead of ctrlpath? */
388 ng_send_msg(node, msg, priv->ctrlpath,
389 NULL, NULL, NULL);
390 }
391 return (error);
392 }
393 m_freem(m);
394 NG_SEND_DATA(error, priv->recv.hook, out, meta);
395 return (error);
396 }
397
398 /* Oops */
399 panic("%s: unknown hook", __FUNCTION__);
400}
401
402/*
403 * Destroy node
404 */
405static int
406ng_mppc_rmnode(node_p node)
407{
408 const priv_p priv = node->private;
409
410 /* Take down netgraph node */
411 node->flags |= NG_INVALID;
412 ng_cutlinks(node);
413 ng_unname(node);
414 if (priv->ctrlpath != NULL)
415 FREE(priv->ctrlpath, M_NETGRAPH);
416#ifdef NETGRAPH_MPPC_COMPRESSION
417 if (priv->xmit.history != NULL)
418 FREE(priv->xmit.history, M_NETGRAPH);
419 if (priv->recv.history != NULL)
420 FREE(priv->recv.history, M_NETGRAPH);
421#endif
422 bzero(priv, sizeof(*priv));
423 FREE(priv, M_NETGRAPH);
424 node->private = NULL;
425 ng_unref(node); /* let the node escape */
426 return (0);
427}
428
429/*
430 * Hook disconnection
431 */
432static int
433ng_mppc_disconnect(hook_p hook)
434{
435 const node_p node = hook->node;
436 const priv_p priv = node->private;
437
438 /* Zero out hook pointer */
439 if (hook == priv->xmit.hook)
440 priv->xmit.hook = NULL;
441 if (hook == priv->recv.hook)
442 priv->recv.hook = NULL;
443
444 /* Go away if no longer connected */
445 if (node->numhooks == 0)
446 ng_rmnode(node);
447 return (0);
448}
449
450/************************************************************************
451 HELPER STUFF
452 ************************************************************************/
453
454/*
455 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
456 * The original mbuf is not free'd.
457 */
458static int
459ng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
460{
461 const priv_p priv = node->private;
462 struct ng_mppc_dir *const d = &priv->xmit;
463 u_char *inbuf, *outbuf;
464 int outlen, inlen;
465 u_int16_t header;
466
467 /* Initialize */
468 *resultp = NULL;
469 header = d->cc;
470 if (d->flushed) {
471 header |= MPPC_FLAG_FLUSHED;
472 d->flushed = 0;
473 }
474
475 /* Work with contiguous regions of memory */
476 inlen = m->m_pkthdr.len;
477 MALLOC(inbuf, u_char *, inlen, M_NETGRAPH, M_NOWAIT);
478 if (inbuf == NULL)
479 return (ENOMEM);
480 m_copydata(m, 0, inlen, (caddr_t)inbuf);
481 if ((d->cfg.bits & MPPC_BIT) != 0)
482 outlen = MPPC_MAX_BLOWUP(inlen);
483 else
484 outlen = MPPC_HDRLEN + inlen;
485 MALLOC(outbuf, u_char *, outlen, M_NETGRAPH, M_NOWAIT);
486 if (outbuf == NULL) {
487 FREE(inbuf, M_NETGRAPH);
488 return (ENOMEM);
489 }
490
491 /* Compress "inbuf" into "outbuf" (if compression enabled) */
492#ifdef NETGRAPH_MPPC_COMPRESSION
493 if ((d->cfg.bits & MPPC_BIT) != 0) {
494 u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS;
495 u_char *source, *dest;
496 u_long sourceCnt, destCnt;
497 int rtn;
498
499 /* Prepare to compress */
500 source = inbuf;
501 sourceCnt = inlen;
502 dest = outbuf + MPPC_HDRLEN;
503 destCnt = outlen - MPPC_HDRLEN;
504 if ((d->cfg.bits & MPPE_STATELESS) == 0)
505 flags |= MPPC_SAVE_HISTORY;
506
507 /* Compress */
508 rtn = MPPC_Compress(&source, &dest, &sourceCnt,
509 &destCnt, d->history, flags, 0);
510
511 /* Check return value */
512 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __FUNCTION__));
513 if ((rtn & MPPC_EXPANDED) == 0
514 && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) {
515 outlen -= destCnt;
516 header |= MPPC_FLAG_COMPRESSED;
517 if ((rtn & MPPC_RESTART_HISTORY) != 0)
518 header |= MPPC_FLAG_RESTART;
519 }
520 d->flushed = (rtn & MPPC_EXPANDED) != 0
521 || (flags & MPPC_SAVE_HISTORY) == 0;
522 }
523#endif
524
525 /* If we did not compress this packet, copy it to output buffer */
526 if ((header & MPPC_FLAG_COMPRESSED) == 0) {
527 bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen);
528 outlen = MPPC_HDRLEN + inlen;
529 }
530 FREE(inbuf, M_NETGRAPH);
531
532 /* Always set the flushed bit in stateless mode */
533 if ((d->cfg.bits & MPPE_STATELESS) != 0)
534 header |= MPPC_FLAG_FLUSHED;
535
536 /* Now encrypt packet (if encryption enabled) */
537#ifdef NETGRAPH_MPPC_ENCRYPTION
538 if ((d->cfg.bits & MPPE_BITS) != 0) {
539
540 /* Set header bits; need to reset key if we say we did */
541 header |= MPPC_FLAG_ENCRYPTED;
542 if ((header & MPPC_FLAG_FLUSHED) != 0)
543 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
544
545 /* Update key if it's time */
546 if ((d->cfg.bits & MPPE_STATELESS) != 0
547 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
548 ng_mppc_updatekey(d->cfg.bits,
549 d->cfg.startkey, d->key, &d->rc4);
550 }
551
552 /* Encrypt packet */
553 rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN,
554 outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN);
555 }
556#endif
557
558 /* Update sequence number */
559 d->cc++;
560
561 /* Install header */
562 *((u_int16_t *)outbuf) = htons(header);
563
564 /* Return packet in an mbuf */
565 *resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL);
566 FREE(outbuf, M_NETGRAPH);
567 return (*resultp == NULL ? ENOBUFS : 0);
568}
569
570/*
571 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
572 * The original mbuf is not free'd.
573 */
574static int
575ng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
576{
577 const priv_p priv = node->private;
578 struct ng_mppc_dir *const d = &priv->recv;
579 u_int16_t header, cc, 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 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, M_NOWAIT);
593 if (buf == NULL)
594 return (ENOMEM);
595 m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf);
596
597 /* Check for insane jumps in sequence numbering (D.O.S. attack) */
598 numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK);
599 if (numLost >= MPPC_INSANE_JUMP) {
600 log(LOG_ERR, "%s: insane jump %d", __FUNCTION__, numLost);
601 priv->recv.cfg.enable = 0;
602 goto failed;
603 }
604
605 /* If flushed bit set, we can always handle packet */
606 if ((header & MPPC_FLAG_FLUSHED) != 0) {
607#ifdef NETGRAPH_MPPC_COMPRESSION
608 if (d->history != NULL)
609 MPPC_InitDecompressionHistory(d->history);
610#endif
611#ifdef NETGRAPH_MPPC_ENCRYPTION
612 if ((d->cfg.bits & MPPE_BITS) != 0) {
613
614 /* Resync as necessary, skipping lost packets */
615 while (d->cc != cc) {
616 if ((d->cfg.bits & MPPE_STATELESS)
617 || (d->cc & MPPE_UPDATE_MASK)
618 == MPPE_UPDATE_FLAG) {
619 ng_mppc_updatekey(d->cfg.bits,
620 d->cfg.startkey, d->key, &d->rc4);
621 }
622 d->cc++;
623 }
624
625 /* Reset key (except in stateless mode, see below) */
626 if ((d->cfg.bits & MPPE_STATELESS) == 0)
627 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
628 }
629#endif
630 d->cc = cc; /* skip over lost seq numbers */
631 numLost = 0; /* act like no packets were lost */
632 }
633
634 /* Can't decode non-sequential packets without a flushed bit */
635 if (numLost != 0)
636 goto failed;
637
638 /* Decrypt packet */
639 if ((header & MPPC_FLAG_ENCRYPTED) != 0) {
640
641 /* Are we not expecting encryption? */
642 if ((d->cfg.bits & MPPE_BITS) == 0) {
643 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
644 __FUNCTION__, "encrypted");
645 goto failed;
646 }
647
648#ifdef NETGRAPH_MPPC_ENCRYPTION
649 /* Update key if it's time (always in stateless mode) */
650 if ((d->cfg.bits & MPPE_STATELESS) != 0
651 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
652 ng_mppc_updatekey(d->cfg.bits,
653 d->cfg.startkey, d->key, &d->rc4);
654 }
655
656 /* Decrypt packet */
657 rc4_crypt(&d->rc4, buf, buf, len);
658#endif
659 } else {
660
661 /* Are we expecting encryption? */
662 if ((d->cfg.bits & MPPE_BITS) != 0) {
663 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
664 __FUNCTION__, "unencrypted");
665 goto failed;
666 }
667 }
668
669 /* Update coherency count for next time (12 bit arithmetic) */
670 d->cc++;
671
672 /* Check for unexpected compressed packet */
673 if ((header & MPPC_FLAG_COMPRESSED) != 0
674 && (d->cfg.bits & MPPC_BIT) == 0) {
675 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
676 __FUNCTION__, "compressed");
677failed:
678 FREE(buf, M_NETGRAPH);
679 return (EINVAL);
680 }
681
682#ifdef NETGRAPH_MPPC_COMPRESSION
683 /* Decompress packet */
684 if ((header & MPPC_FLAG_COMPRESSED) != 0) {
685 int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS;
686 u_char *decompbuf, *source, *dest;
687 u_long sourceCnt, destCnt;
688 int decomplen, rtn;
689
690 /* Allocate a buffer for decompressed data */
691 MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE
692 + MPPC_DECOMP_SAFETY, M_NETGRAPH, M_NOWAIT);
693 if (decompbuf == NULL) {
694 FREE(buf, M_NETGRAPH);
695 return (ENOMEM);
696 }
697 decomplen = MPPC_DECOMP_BUFSIZE;
698
699 /* Prepare to decompress */
700 source = buf;
701 sourceCnt = len;
702 dest = decompbuf;
703 destCnt = decomplen;
704 if ((header & MPPC_FLAG_RESTART) != 0)
705 flags |= MPPC_RESTART_HISTORY;
706
707 /* Decompress */
708 rtn = MPPC_Decompress(&source, &dest,
709 &sourceCnt, &destCnt, d->history, flags);
710
711 /* Check return value */
712 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __FUNCTION__));
713 if ((rtn & MPPC_DEST_EXHAUSTED) != 0
714 || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) {
715 log(LOG_ERR, "%s: decomp returned 0x%x",
716 __FUNCTION__, rtn);
717 FREE(decompbuf, M_NETGRAPH);
718 goto failed;
719 }
720
721 /* Replace compressed data with decompressed data */
722 FREE(buf, M_NETGRAPH);
723 buf = decompbuf;
724 len = decomplen - destCnt;
725 }
726#endif
727
728 /* Return result in an mbuf */
729 *resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL);
730 FREE(buf, M_NETGRAPH);
731 return (*resultp == NULL ? ENOBUFS : 0);
732}
733
734/*
735 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
736 */
737static void
738ng_mppc_reset_req(node_p node)
739{
740 const priv_p priv = node->private;
741 struct ng_mppc_dir *const d = &priv->xmit;
742
743#ifdef NETGRAPH_MPPC_COMPRESSION
744 if (d->history != NULL)
745 MPPC_InitCompressionHistory(d->history);
746#endif
747#ifdef NETGRAPH_MPPC_ENCRYPTION
748 if ((d->cfg.bits & MPPE_STATELESS) == 0)
749 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
750#endif
751 d->flushed = 1;
752}
753
754/*
755 * Generate a new encryption key
756 */
757static void
758ng_mppc_getkey(const u_char *h, u_char *h2, int len)
759{
760 static const u_char pad1[10] =
761 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
762 static const u_char pad2[10] =
763 { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, };
764 u_char hash[20];
765 SHA1_CTX c;
766 int k;
767
768 bzero(&hash, sizeof(hash));
769 SHA1Init(&c);
770 SHA1Update(&c, h, len);
771 for (k = 0; k < 4; k++)
772 SHA1Update(&c, pad1, sizeof(pad2));
773 SHA1Update(&c, h2, len);
774 for (k = 0; k < 4; k++)
775 SHA1Update(&c, pad2, sizeof(pad2));
776 SHA1Final(hash, &c);
777 bcopy(hash, h2, len);
778}
779
780/*
781 * Update the encryption key
782 */
783static void
784ng_mppc_updatekey(u_int32_t bits,
785 u_char *key0, u_char *key, struct rc4_state *rc4)
786{
787 const int keylen = KEYLEN(bits);
788
789 ng_mppc_getkey(key0, key, keylen);
790 rc4_init(rc4, key, keylen);
791 rc4_crypt(rc4, key, key, keylen);
792 if ((bits & MPPE_128) == 0)
793 bcopy(&ng_mppe_weakenkey, key, sizeof(ng_mppe_weakenkey));
794 rc4_init(rc4, key, keylen);
795}
796
147 NG_MPPC_NODE_TYPE,
148 NULL,
149 ng_mppc_constructor,
150 ng_mppc_rcvmsg,
151 ng_mppc_rmnode,
152 ng_mppc_newhook,
153 NULL,
154 NULL,
155 ng_mppc_rcvdata,
156 ng_mppc_disconnect,
157 NULL
158};
159NETGRAPH_INIT(mppc, &ng_mppc_typestruct);
160
161/* Fixed bit pattern to weaken keysize down to 40 bits */
162static const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e };
163
164#define ERROUT(x) do { error = (x); goto done; } while (0)
165
166/************************************************************************
167 NETGRAPH NODE STUFF
168 ************************************************************************/
169
170/*
171 * Node type constructor
172 */
173static int
174ng_mppc_constructor(node_p *nodep)
175{
176 priv_p priv;
177 int error;
178
179 /* Allocate private structure */
180 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
181 if (priv == NULL)
182 return (ENOMEM);
183
184 /* Call generic node constructor */
185 if ((error = ng_make_node_common(&ng_mppc_typestruct, nodep))) {
186 FREE(priv, M_NETGRAPH);
187 return (error);
188 }
189 (*nodep)->private = priv;
190
191 /* Done */
192 return (0);
193}
194
195/*
196 * Give our OK for a hook to be added
197 */
198static int
199ng_mppc_newhook(node_p node, hook_p hook, const char *name)
200{
201 const priv_p priv = node->private;
202 hook_p *hookPtr;
203
204 /* Check hook name */
205 if (strcmp(name, NG_MPPC_HOOK_COMP) == 0)
206 hookPtr = &priv->xmit.hook;
207 else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0)
208 hookPtr = &priv->recv.hook;
209 else
210 return (EINVAL);
211
212 /* See if already connected */
213 if (*hookPtr != NULL)
214 return (EISCONN);
215
216 /* OK */
217 *hookPtr = hook;
218 return (0);
219}
220
221/*
222 * Receive a control message
223 */
224static int
225ng_mppc_rcvmsg(node_p node, struct ng_mesg *msg,
226 const char *raddr, struct ng_mesg **rptr, hook_p lasthook)
227{
228 const priv_p priv = node->private;
229 struct ng_mesg *resp = NULL;
230 int error = 0;
231
232 switch (msg->header.typecookie) {
233 case NGM_MPPC_COOKIE:
234 switch (msg->header.cmd) {
235 case NGM_MPPC_CONFIG_COMP:
236 case NGM_MPPC_CONFIG_DECOMP:
237 {
238 struct ng_mppc_config *const cfg
239 = (struct ng_mppc_config *)msg->data;
240 const int isComp =
241 msg->header.cmd == NGM_MPPC_CONFIG_COMP;
242 struct ng_mppc_dir *const d = isComp ?
243 &priv->xmit : &priv->recv;
244
245 /* Check configuration */
246 if (msg->header.arglen != sizeof(*cfg))
247 ERROUT(EINVAL);
248 if (cfg->enable) {
249 if ((cfg->bits & ~MPPC_VALID_BITS) != 0)
250 ERROUT(EINVAL);
251#ifndef NETGRAPH_MPPC_COMPRESSION
252 if ((cfg->bits & MPPC_BIT) != 0)
253 ERROUT(EPROTONOSUPPORT);
254#endif
255#ifndef NETGRAPH_MPPC_ENCRYPTION
256 if ((cfg->bits & MPPE_BITS) != 0)
257 ERROUT(EPROTONOSUPPORT);
258#endif
259 } else
260 cfg->bits = 0;
261
262 /* Save return address so we can send reset-req's */
263 if (priv->ctrlpath != NULL) {
264 FREE(priv->ctrlpath, M_NETGRAPH);
265 priv->ctrlpath = NULL;
266 }
267 if (!isComp && raddr != NULL) {
268 MALLOC(priv->ctrlpath, char *,
269 strlen(raddr) + 1, M_NETGRAPH, M_NOWAIT);
270 if (priv->ctrlpath == NULL)
271 ERROUT(ENOMEM);
272 strcpy(priv->ctrlpath, raddr);
273 }
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);
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, 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_128) == 0) {
308 bcopy(&ng_mppe_weakenkey, d->key,
309 sizeof(ng_mppe_weakenkey));
310 }
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 if (rptr)
335 *rptr = resp;
336 else if (resp)
337 FREE(resp, M_NETGRAPH);
338
339done:
340 FREE(msg, M_NETGRAPH);
341 return (error);
342}
343
344/*
345 * Receive incoming data on our hook.
346 */
347static int
348ng_mppc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
349 struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp)
350{
351 const node_p node = hook->node;
352 const priv_p priv = node->private;
353 struct mbuf *out;
354 int error;
355
356 /* Compress and/or encrypt */
357 if (hook == priv->xmit.hook) {
358 if (!priv->xmit.cfg.enable) {
359 NG_FREE_DATA(m, meta);
360 return (ENXIO);
361 }
362 if ((error = ng_mppc_compress(node, m, &out)) != 0) {
363 NG_FREE_DATA(m, meta);
364 return(error);
365 }
366 m_freem(m);
367 NG_SEND_DATA(error, priv->xmit.hook, out, meta);
368 return (error);
369 }
370
371 /* Decompress and/or decrypt */
372 if (hook == priv->recv.hook) {
373 if (!priv->recv.cfg.enable) {
374 NG_FREE_DATA(m, meta);
375 return (ENXIO);
376 }
377 if ((error = ng_mppc_decompress(node, m, &out)) != 0) {
378 NG_FREE_DATA(m, meta);
379 if (error == EINVAL && priv->ctrlpath != NULL) {
380 struct ng_mesg *msg;
381
382 /* Need to send a reset-request */
383 NG_MKMESSAGE(msg, NGM_MPPC_COOKIE,
384 NGM_MPPC_RESETREQ, 0, M_NOWAIT);
385 if (msg == NULL)
386 return (error);
387 /* XXX can we use a hook instead of ctrlpath? */
388 ng_send_msg(node, msg, priv->ctrlpath,
389 NULL, NULL, NULL);
390 }
391 return (error);
392 }
393 m_freem(m);
394 NG_SEND_DATA(error, priv->recv.hook, out, meta);
395 return (error);
396 }
397
398 /* Oops */
399 panic("%s: unknown hook", __FUNCTION__);
400}
401
402/*
403 * Destroy node
404 */
405static int
406ng_mppc_rmnode(node_p node)
407{
408 const priv_p priv = node->private;
409
410 /* Take down netgraph node */
411 node->flags |= NG_INVALID;
412 ng_cutlinks(node);
413 ng_unname(node);
414 if (priv->ctrlpath != NULL)
415 FREE(priv->ctrlpath, M_NETGRAPH);
416#ifdef NETGRAPH_MPPC_COMPRESSION
417 if (priv->xmit.history != NULL)
418 FREE(priv->xmit.history, M_NETGRAPH);
419 if (priv->recv.history != NULL)
420 FREE(priv->recv.history, M_NETGRAPH);
421#endif
422 bzero(priv, sizeof(*priv));
423 FREE(priv, M_NETGRAPH);
424 node->private = NULL;
425 ng_unref(node); /* let the node escape */
426 return (0);
427}
428
429/*
430 * Hook disconnection
431 */
432static int
433ng_mppc_disconnect(hook_p hook)
434{
435 const node_p node = hook->node;
436 const priv_p priv = node->private;
437
438 /* Zero out hook pointer */
439 if (hook == priv->xmit.hook)
440 priv->xmit.hook = NULL;
441 if (hook == priv->recv.hook)
442 priv->recv.hook = NULL;
443
444 /* Go away if no longer connected */
445 if (node->numhooks == 0)
446 ng_rmnode(node);
447 return (0);
448}
449
450/************************************************************************
451 HELPER STUFF
452 ************************************************************************/
453
454/*
455 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
456 * The original mbuf is not free'd.
457 */
458static int
459ng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
460{
461 const priv_p priv = node->private;
462 struct ng_mppc_dir *const d = &priv->xmit;
463 u_char *inbuf, *outbuf;
464 int outlen, inlen;
465 u_int16_t header;
466
467 /* Initialize */
468 *resultp = NULL;
469 header = d->cc;
470 if (d->flushed) {
471 header |= MPPC_FLAG_FLUSHED;
472 d->flushed = 0;
473 }
474
475 /* Work with contiguous regions of memory */
476 inlen = m->m_pkthdr.len;
477 MALLOC(inbuf, u_char *, inlen, M_NETGRAPH, M_NOWAIT);
478 if (inbuf == NULL)
479 return (ENOMEM);
480 m_copydata(m, 0, inlen, (caddr_t)inbuf);
481 if ((d->cfg.bits & MPPC_BIT) != 0)
482 outlen = MPPC_MAX_BLOWUP(inlen);
483 else
484 outlen = MPPC_HDRLEN + inlen;
485 MALLOC(outbuf, u_char *, outlen, M_NETGRAPH, M_NOWAIT);
486 if (outbuf == NULL) {
487 FREE(inbuf, M_NETGRAPH);
488 return (ENOMEM);
489 }
490
491 /* Compress "inbuf" into "outbuf" (if compression enabled) */
492#ifdef NETGRAPH_MPPC_COMPRESSION
493 if ((d->cfg.bits & MPPC_BIT) != 0) {
494 u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS;
495 u_char *source, *dest;
496 u_long sourceCnt, destCnt;
497 int rtn;
498
499 /* Prepare to compress */
500 source = inbuf;
501 sourceCnt = inlen;
502 dest = outbuf + MPPC_HDRLEN;
503 destCnt = outlen - MPPC_HDRLEN;
504 if ((d->cfg.bits & MPPE_STATELESS) == 0)
505 flags |= MPPC_SAVE_HISTORY;
506
507 /* Compress */
508 rtn = MPPC_Compress(&source, &dest, &sourceCnt,
509 &destCnt, d->history, flags, 0);
510
511 /* Check return value */
512 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __FUNCTION__));
513 if ((rtn & MPPC_EXPANDED) == 0
514 && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) {
515 outlen -= destCnt;
516 header |= MPPC_FLAG_COMPRESSED;
517 if ((rtn & MPPC_RESTART_HISTORY) != 0)
518 header |= MPPC_FLAG_RESTART;
519 }
520 d->flushed = (rtn & MPPC_EXPANDED) != 0
521 || (flags & MPPC_SAVE_HISTORY) == 0;
522 }
523#endif
524
525 /* If we did not compress this packet, copy it to output buffer */
526 if ((header & MPPC_FLAG_COMPRESSED) == 0) {
527 bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen);
528 outlen = MPPC_HDRLEN + inlen;
529 }
530 FREE(inbuf, M_NETGRAPH);
531
532 /* Always set the flushed bit in stateless mode */
533 if ((d->cfg.bits & MPPE_STATELESS) != 0)
534 header |= MPPC_FLAG_FLUSHED;
535
536 /* Now encrypt packet (if encryption enabled) */
537#ifdef NETGRAPH_MPPC_ENCRYPTION
538 if ((d->cfg.bits & MPPE_BITS) != 0) {
539
540 /* Set header bits; need to reset key if we say we did */
541 header |= MPPC_FLAG_ENCRYPTED;
542 if ((header & MPPC_FLAG_FLUSHED) != 0)
543 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
544
545 /* Update key if it's time */
546 if ((d->cfg.bits & MPPE_STATELESS) != 0
547 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
548 ng_mppc_updatekey(d->cfg.bits,
549 d->cfg.startkey, d->key, &d->rc4);
550 }
551
552 /* Encrypt packet */
553 rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN,
554 outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN);
555 }
556#endif
557
558 /* Update sequence number */
559 d->cc++;
560
561 /* Install header */
562 *((u_int16_t *)outbuf) = htons(header);
563
564 /* Return packet in an mbuf */
565 *resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL);
566 FREE(outbuf, M_NETGRAPH);
567 return (*resultp == NULL ? ENOBUFS : 0);
568}
569
570/*
571 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
572 * The original mbuf is not free'd.
573 */
574static int
575ng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
576{
577 const priv_p priv = node->private;
578 struct ng_mppc_dir *const d = &priv->recv;
579 u_int16_t header, cc, 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 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, M_NOWAIT);
593 if (buf == NULL)
594 return (ENOMEM);
595 m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf);
596
597 /* Check for insane jumps in sequence numbering (D.O.S. attack) */
598 numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK);
599 if (numLost >= MPPC_INSANE_JUMP) {
600 log(LOG_ERR, "%s: insane jump %d", __FUNCTION__, numLost);
601 priv->recv.cfg.enable = 0;
602 goto failed;
603 }
604
605 /* If flushed bit set, we can always handle packet */
606 if ((header & MPPC_FLAG_FLUSHED) != 0) {
607#ifdef NETGRAPH_MPPC_COMPRESSION
608 if (d->history != NULL)
609 MPPC_InitDecompressionHistory(d->history);
610#endif
611#ifdef NETGRAPH_MPPC_ENCRYPTION
612 if ((d->cfg.bits & MPPE_BITS) != 0) {
613
614 /* Resync as necessary, skipping lost packets */
615 while (d->cc != cc) {
616 if ((d->cfg.bits & MPPE_STATELESS)
617 || (d->cc & MPPE_UPDATE_MASK)
618 == MPPE_UPDATE_FLAG) {
619 ng_mppc_updatekey(d->cfg.bits,
620 d->cfg.startkey, d->key, &d->rc4);
621 }
622 d->cc++;
623 }
624
625 /* Reset key (except in stateless mode, see below) */
626 if ((d->cfg.bits & MPPE_STATELESS) == 0)
627 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
628 }
629#endif
630 d->cc = cc; /* skip over lost seq numbers */
631 numLost = 0; /* act like no packets were lost */
632 }
633
634 /* Can't decode non-sequential packets without a flushed bit */
635 if (numLost != 0)
636 goto failed;
637
638 /* Decrypt packet */
639 if ((header & MPPC_FLAG_ENCRYPTED) != 0) {
640
641 /* Are we not expecting encryption? */
642 if ((d->cfg.bits & MPPE_BITS) == 0) {
643 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
644 __FUNCTION__, "encrypted");
645 goto failed;
646 }
647
648#ifdef NETGRAPH_MPPC_ENCRYPTION
649 /* Update key if it's time (always in stateless mode) */
650 if ((d->cfg.bits & MPPE_STATELESS) != 0
651 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
652 ng_mppc_updatekey(d->cfg.bits,
653 d->cfg.startkey, d->key, &d->rc4);
654 }
655
656 /* Decrypt packet */
657 rc4_crypt(&d->rc4, buf, buf, len);
658#endif
659 } else {
660
661 /* Are we expecting encryption? */
662 if ((d->cfg.bits & MPPE_BITS) != 0) {
663 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
664 __FUNCTION__, "unencrypted");
665 goto failed;
666 }
667 }
668
669 /* Update coherency count for next time (12 bit arithmetic) */
670 d->cc++;
671
672 /* Check for unexpected compressed packet */
673 if ((header & MPPC_FLAG_COMPRESSED) != 0
674 && (d->cfg.bits & MPPC_BIT) == 0) {
675 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
676 __FUNCTION__, "compressed");
677failed:
678 FREE(buf, M_NETGRAPH);
679 return (EINVAL);
680 }
681
682#ifdef NETGRAPH_MPPC_COMPRESSION
683 /* Decompress packet */
684 if ((header & MPPC_FLAG_COMPRESSED) != 0) {
685 int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS;
686 u_char *decompbuf, *source, *dest;
687 u_long sourceCnt, destCnt;
688 int decomplen, rtn;
689
690 /* Allocate a buffer for decompressed data */
691 MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE
692 + MPPC_DECOMP_SAFETY, M_NETGRAPH, M_NOWAIT);
693 if (decompbuf == NULL) {
694 FREE(buf, M_NETGRAPH);
695 return (ENOMEM);
696 }
697 decomplen = MPPC_DECOMP_BUFSIZE;
698
699 /* Prepare to decompress */
700 source = buf;
701 sourceCnt = len;
702 dest = decompbuf;
703 destCnt = decomplen;
704 if ((header & MPPC_FLAG_RESTART) != 0)
705 flags |= MPPC_RESTART_HISTORY;
706
707 /* Decompress */
708 rtn = MPPC_Decompress(&source, &dest,
709 &sourceCnt, &destCnt, d->history, flags);
710
711 /* Check return value */
712 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __FUNCTION__));
713 if ((rtn & MPPC_DEST_EXHAUSTED) != 0
714 || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) {
715 log(LOG_ERR, "%s: decomp returned 0x%x",
716 __FUNCTION__, rtn);
717 FREE(decompbuf, M_NETGRAPH);
718 goto failed;
719 }
720
721 /* Replace compressed data with decompressed data */
722 FREE(buf, M_NETGRAPH);
723 buf = decompbuf;
724 len = decomplen - destCnt;
725 }
726#endif
727
728 /* Return result in an mbuf */
729 *resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL);
730 FREE(buf, M_NETGRAPH);
731 return (*resultp == NULL ? ENOBUFS : 0);
732}
733
734/*
735 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
736 */
737static void
738ng_mppc_reset_req(node_p node)
739{
740 const priv_p priv = node->private;
741 struct ng_mppc_dir *const d = &priv->xmit;
742
743#ifdef NETGRAPH_MPPC_COMPRESSION
744 if (d->history != NULL)
745 MPPC_InitCompressionHistory(d->history);
746#endif
747#ifdef NETGRAPH_MPPC_ENCRYPTION
748 if ((d->cfg.bits & MPPE_STATELESS) == 0)
749 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
750#endif
751 d->flushed = 1;
752}
753
754/*
755 * Generate a new encryption key
756 */
757static void
758ng_mppc_getkey(const u_char *h, u_char *h2, int len)
759{
760 static const u_char pad1[10] =
761 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
762 static const u_char pad2[10] =
763 { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, };
764 u_char hash[20];
765 SHA1_CTX c;
766 int k;
767
768 bzero(&hash, sizeof(hash));
769 SHA1Init(&c);
770 SHA1Update(&c, h, len);
771 for (k = 0; k < 4; k++)
772 SHA1Update(&c, pad1, sizeof(pad2));
773 SHA1Update(&c, h2, len);
774 for (k = 0; k < 4; k++)
775 SHA1Update(&c, pad2, sizeof(pad2));
776 SHA1Final(hash, &c);
777 bcopy(hash, h2, len);
778}
779
780/*
781 * Update the encryption key
782 */
783static void
784ng_mppc_updatekey(u_int32_t bits,
785 u_char *key0, u_char *key, struct rc4_state *rc4)
786{
787 const int keylen = KEYLEN(bits);
788
789 ng_mppc_getkey(key0, key, keylen);
790 rc4_init(rc4, key, keylen);
791 rc4_crypt(rc4, key, key, keylen);
792 if ((bits & MPPE_128) == 0)
793 bcopy(&ng_mppe_weakenkey, key, sizeof(ng_mppe_weakenkey));
794 rc4_init(rc4, key, keylen);
795}
796