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