xref: /freebsd/sys/netgraph/ng_mppc.c (revision f8aae7776f85d2fa8aa93f73c37782cd9e1204c6)
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 
93f3059f39SArchie Cobbs /*
94f3059f39SArchie Cobbs  * When packets are lost with MPPE, we may have to re-key arbitrarily
95f3059f39SArchie Cobbs  * many times to 'catch up' to the new jumped-ahead sequence number.
96f3059f39SArchie Cobbs  * Since this can be expensive, we pose a limit on how many re-keyings
97f3059f39SArchie Cobbs  * we will do at one time to avoid a possible D.O.S. vulnerability.
98f3059f39SArchie Cobbs  * This should instead be a configurable parameter.
99f3059f39SArchie Cobbs  */
100f3059f39SArchie Cobbs #define MPPE_MAX_REKEY		1000
101af7ab184SArchie Cobbs 
102af7ab184SArchie Cobbs /* MPPC packet header bits */
103af7ab184SArchie Cobbs #define MPPC_FLAG_FLUSHED	0x8000		/* xmitter reset state */
104af7ab184SArchie Cobbs #define MPPC_FLAG_RESTART	0x4000		/* compress history restart */
105af7ab184SArchie Cobbs #define MPPC_FLAG_COMPRESSED	0x2000		/* packet is compresed */
106af7ab184SArchie Cobbs #define MPPC_FLAG_ENCRYPTED	0x1000		/* packet is encrypted */
107af7ab184SArchie Cobbs #define MPPC_CCOUNT_MASK	0x0fff		/* sequence number mask */
108af7ab184SArchie Cobbs 
109af7ab184SArchie Cobbs #define MPPE_UPDATE_MASK	0xff		/* coherency count when we're */
110af7ab184SArchie Cobbs #define MPPE_UPDATE_FLAG	0xff		/*   supposed to update key */
111af7ab184SArchie Cobbs 
112af7ab184SArchie Cobbs #define MPPC_COMP_OK		0x05
113af7ab184SArchie Cobbs #define MPPC_DECOMP_OK		0x05
114af7ab184SArchie Cobbs 
115af7ab184SArchie Cobbs /* Per direction info */
116af7ab184SArchie Cobbs struct ng_mppc_dir {
117af7ab184SArchie Cobbs 	struct ng_mppc_config	cfg;		/* configuration */
118af7ab184SArchie Cobbs 	hook_p			hook;		/* netgraph hook */
119af7ab184SArchie Cobbs 	u_int16_t		cc:12;		/* coherency count */
120af7ab184SArchie Cobbs 	u_char			flushed;	/* clean history (xmit only) */
121af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_COMPRESSION
122af7ab184SArchie Cobbs 	u_char			*history;	/* compression history */
123af7ab184SArchie Cobbs #endif
124af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_ENCRYPTION
125af7ab184SArchie Cobbs 	u_char			key[MPPE_KEY_LEN];	/* session key */
126af7ab184SArchie Cobbs 	struct rc4_state	rc4;			/* rc4 state */
127af7ab184SArchie Cobbs #endif
128af7ab184SArchie Cobbs };
129af7ab184SArchie Cobbs 
130af7ab184SArchie Cobbs /* Node private data */
131af7ab184SArchie Cobbs struct ng_mppc_private {
132af7ab184SArchie Cobbs 	struct ng_mppc_dir	xmit;		/* compress/encrypt config */
133af7ab184SArchie Cobbs 	struct ng_mppc_dir	recv;		/* decompress/decrypt config */
134069154d5SJulian Elischer 	ng_ID_t			ctrlnode;	/* path to controlling node */
135af7ab184SArchie Cobbs };
136af7ab184SArchie Cobbs typedef struct ng_mppc_private *priv_p;
137af7ab184SArchie Cobbs 
138af7ab184SArchie Cobbs /* Netgraph node methods */
139af7ab184SArchie Cobbs static ng_constructor_t	ng_mppc_constructor;
140af7ab184SArchie Cobbs static ng_rcvmsg_t	ng_mppc_rcvmsg;
141069154d5SJulian Elischer static ng_shutdown_t	ng_mppc_shutdown;
142af7ab184SArchie Cobbs static ng_newhook_t	ng_mppc_newhook;
143af7ab184SArchie Cobbs static ng_rcvdata_t	ng_mppc_rcvdata;
144af7ab184SArchie Cobbs static ng_disconnect_t	ng_mppc_disconnect;
145af7ab184SArchie Cobbs 
146af7ab184SArchie Cobbs /* Helper functions */
147af7ab184SArchie Cobbs static int	ng_mppc_compress(node_p node,
148af7ab184SArchie Cobbs 			struct mbuf *m, struct mbuf **resultp);
149af7ab184SArchie Cobbs static int	ng_mppc_decompress(node_p node,
150af7ab184SArchie Cobbs 			struct mbuf *m, struct mbuf **resultp);
151af7ab184SArchie Cobbs static void	ng_mppc_getkey(const u_char *h, u_char *h2, int len);
152af7ab184SArchie Cobbs static void	ng_mppc_updatekey(u_int32_t bits,
153af7ab184SArchie Cobbs 			u_char *key0, u_char *key, struct rc4_state *rc4);
154af7ab184SArchie Cobbs static void	ng_mppc_reset_req(node_p node);
155af7ab184SArchie Cobbs 
156af7ab184SArchie Cobbs /* Node type descriptor */
157af7ab184SArchie Cobbs static struct ng_type ng_mppc_typestruct = {
158f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
159f8aae777SJulian Elischer 	.name =		NG_MPPC_NODE_TYPE,
160f8aae777SJulian Elischer 	.constructor =	ng_mppc_constructor,
161f8aae777SJulian Elischer 	.rcvmsg =	ng_mppc_rcvmsg,
162f8aae777SJulian Elischer 	.shutdown =	ng_mppc_shutdown,
163f8aae777SJulian Elischer 	.newhook =	ng_mppc_newhook,
164f8aae777SJulian Elischer 	.rcvdata =	ng_mppc_rcvdata,
165f8aae777SJulian Elischer 	.disconnect =	ng_mppc_disconnect,
166af7ab184SArchie Cobbs };
167af7ab184SArchie Cobbs NETGRAPH_INIT(mppc, &ng_mppc_typestruct);
168af7ab184SArchie Cobbs 
169233896e9SDoug Ambrisko #ifdef NETGRAPH_MPPC_ENCRYPTION
170233896e9SDoug Ambrisko /* Depend on separate rc4 module */
171233896e9SDoug Ambrisko MODULE_DEPEND(ng_mppc, rc4, 1, 1, 1);
172233896e9SDoug Ambrisko #endif
173233896e9SDoug Ambrisko 
17434fd2381SArchie Cobbs /* Fixed bit pattern to weaken keysize down to 40 or 56 bits */
175af7ab184SArchie Cobbs static const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e };
176af7ab184SArchie Cobbs 
177af7ab184SArchie Cobbs #define ERROUT(x)	do { error = (x); goto done; } while (0)
178af7ab184SArchie Cobbs 
179af7ab184SArchie Cobbs /************************************************************************
180af7ab184SArchie Cobbs 			NETGRAPH NODE STUFF
181af7ab184SArchie Cobbs  ************************************************************************/
182af7ab184SArchie Cobbs 
183af7ab184SArchie Cobbs /*
184af7ab184SArchie Cobbs  * Node type constructor
185af7ab184SArchie Cobbs  */
186af7ab184SArchie Cobbs static int
187069154d5SJulian Elischer ng_mppc_constructor(node_p node)
188af7ab184SArchie Cobbs {
189af7ab184SArchie Cobbs 	priv_p priv;
190af7ab184SArchie Cobbs 
191af7ab184SArchie Cobbs 	/* Allocate private structure */
1929c8c302fSJulian Elischer 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_MPPC, M_NOWAIT | M_ZERO);
193af7ab184SArchie Cobbs 	if (priv == NULL)
194af7ab184SArchie Cobbs 		return (ENOMEM);
195af7ab184SArchie Cobbs 
19630400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
197af7ab184SArchie Cobbs 
198af7ab184SArchie Cobbs 	/* Done */
199af7ab184SArchie Cobbs 	return (0);
200af7ab184SArchie Cobbs }
201af7ab184SArchie Cobbs 
202af7ab184SArchie Cobbs /*
203af7ab184SArchie Cobbs  * Give our OK for a hook to be added
204af7ab184SArchie Cobbs  */
205af7ab184SArchie Cobbs static int
206af7ab184SArchie Cobbs ng_mppc_newhook(node_p node, hook_p hook, const char *name)
207af7ab184SArchie Cobbs {
20830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
209af7ab184SArchie Cobbs 	hook_p *hookPtr;
210af7ab184SArchie Cobbs 
211af7ab184SArchie Cobbs 	/* Check hook name */
212af7ab184SArchie Cobbs 	if (strcmp(name, NG_MPPC_HOOK_COMP) == 0)
213af7ab184SArchie Cobbs 		hookPtr = &priv->xmit.hook;
214af7ab184SArchie Cobbs 	else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0)
215af7ab184SArchie Cobbs 		hookPtr = &priv->recv.hook;
216af7ab184SArchie Cobbs 	else
217af7ab184SArchie Cobbs 		return (EINVAL);
218af7ab184SArchie Cobbs 
219af7ab184SArchie Cobbs 	/* See if already connected */
220af7ab184SArchie Cobbs 	if (*hookPtr != NULL)
221af7ab184SArchie Cobbs 		return (EISCONN);
222af7ab184SArchie Cobbs 
223af7ab184SArchie Cobbs 	/* OK */
224af7ab184SArchie Cobbs 	*hookPtr = hook;
225af7ab184SArchie Cobbs 	return (0);
226af7ab184SArchie Cobbs }
227af7ab184SArchie Cobbs 
228af7ab184SArchie Cobbs /*
229af7ab184SArchie Cobbs  * Receive a control message
230af7ab184SArchie Cobbs  */
231af7ab184SArchie Cobbs static int
232069154d5SJulian Elischer ng_mppc_rcvmsg(node_p node, item_p item, hook_p lasthook)
233af7ab184SArchie Cobbs {
23430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
235af7ab184SArchie Cobbs 	struct ng_mesg *resp = NULL;
236af7ab184SArchie Cobbs 	int error = 0;
237069154d5SJulian Elischer 	struct ng_mesg *msg;
238af7ab184SArchie Cobbs 
239069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
240af7ab184SArchie Cobbs 	switch (msg->header.typecookie) {
241af7ab184SArchie Cobbs 	case NGM_MPPC_COOKIE:
242af7ab184SArchie Cobbs 		switch (msg->header.cmd) {
243af7ab184SArchie Cobbs 		case NGM_MPPC_CONFIG_COMP:
244af7ab184SArchie Cobbs 		case NGM_MPPC_CONFIG_DECOMP:
245af7ab184SArchie Cobbs 		    {
246af7ab184SArchie Cobbs 			struct ng_mppc_config *const cfg
247af7ab184SArchie Cobbs 			    = (struct ng_mppc_config *)msg->data;
248af7ab184SArchie Cobbs 			const int isComp =
249af7ab184SArchie Cobbs 			    msg->header.cmd == NGM_MPPC_CONFIG_COMP;
250af7ab184SArchie Cobbs 			struct ng_mppc_dir *const d = isComp ?
251af7ab184SArchie Cobbs 			    &priv->xmit : &priv->recv;
252af7ab184SArchie Cobbs 
253af7ab184SArchie Cobbs 			/* Check configuration */
254af7ab184SArchie Cobbs 			if (msg->header.arglen != sizeof(*cfg))
255af7ab184SArchie Cobbs 				ERROUT(EINVAL);
256af7ab184SArchie Cobbs 			if (cfg->enable) {
257af7ab184SArchie Cobbs 				if ((cfg->bits & ~MPPC_VALID_BITS) != 0)
258af7ab184SArchie Cobbs 					ERROUT(EINVAL);
259af7ab184SArchie Cobbs #ifndef NETGRAPH_MPPC_COMPRESSION
260af7ab184SArchie Cobbs 				if ((cfg->bits & MPPC_BIT) != 0)
261af7ab184SArchie Cobbs 					ERROUT(EPROTONOSUPPORT);
262af7ab184SArchie Cobbs #endif
263af7ab184SArchie Cobbs #ifndef NETGRAPH_MPPC_ENCRYPTION
264af7ab184SArchie Cobbs 				if ((cfg->bits & MPPE_BITS) != 0)
265af7ab184SArchie Cobbs 					ERROUT(EPROTONOSUPPORT);
266af7ab184SArchie Cobbs #endif
267af7ab184SArchie Cobbs 			} else
268af7ab184SArchie Cobbs 				cfg->bits = 0;
269af7ab184SArchie Cobbs 
270af7ab184SArchie Cobbs 			/* Save return address so we can send reset-req's */
271f3059f39SArchie Cobbs 			if (!isComp)
272069154d5SJulian Elischer 				priv->ctrlnode = NGI_RETADDR(item);
273af7ab184SArchie Cobbs 
274af7ab184SArchie Cobbs 			/* Configuration is OK, reset to it */
275af7ab184SArchie Cobbs 			d->cfg = *cfg;
276af7ab184SArchie Cobbs 
277af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_COMPRESSION
278af7ab184SArchie Cobbs 			/* Initialize state buffers for compression */
279af7ab184SArchie Cobbs 			if (d->history != NULL) {
2809c8c302fSJulian Elischer 				FREE(d->history, M_NETGRAPH_MPPC);
281af7ab184SArchie Cobbs 				d->history = NULL;
282af7ab184SArchie Cobbs 			}
283af7ab184SArchie Cobbs 			if ((cfg->bits & MPPC_BIT) != 0) {
284af7ab184SArchie Cobbs 				MALLOC(d->history, u_char *,
285af7ab184SArchie Cobbs 				    isComp ? MPPC_SizeOfCompressionHistory() :
286af7ab184SArchie Cobbs 				    MPPC_SizeOfDecompressionHistory(),
2879c8c302fSJulian Elischer 				    M_NETGRAPH_MPPC, M_NOWAIT);
288af7ab184SArchie Cobbs 				if (d->history == NULL)
289af7ab184SArchie Cobbs 					ERROUT(ENOMEM);
290af7ab184SArchie Cobbs 				if (isComp)
291af7ab184SArchie Cobbs 					MPPC_InitCompressionHistory(d->history);
292af7ab184SArchie Cobbs 				else {
293af7ab184SArchie Cobbs 					MPPC_InitDecompressionHistory(
294af7ab184SArchie Cobbs 					    d->history);
295af7ab184SArchie Cobbs 				}
296af7ab184SArchie Cobbs 			}
297af7ab184SArchie Cobbs #endif
298af7ab184SArchie Cobbs 
299af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_ENCRYPTION
300af7ab184SArchie Cobbs 			/* Generate initial session keys for encryption */
301af7ab184SArchie Cobbs 			if ((cfg->bits & MPPE_BITS) != 0) {
302af7ab184SArchie Cobbs 				const int keylen = KEYLEN(cfg->bits);
303af7ab184SArchie Cobbs 
304af7ab184SArchie Cobbs 				bcopy(cfg->startkey, d->key, keylen);
305af7ab184SArchie Cobbs 				ng_mppc_getkey(cfg->startkey, d->key, keylen);
30634fd2381SArchie Cobbs 				if ((cfg->bits & MPPE_40) != 0)
30734fd2381SArchie Cobbs 					bcopy(&ng_mppe_weakenkey, d->key, 3);
30834fd2381SArchie Cobbs 				else if ((cfg->bits & MPPE_56) != 0)
30934fd2381SArchie Cobbs 					bcopy(&ng_mppe_weakenkey, d->key, 1);
310af7ab184SArchie Cobbs 				rc4_init(&d->rc4, d->key, keylen);
311af7ab184SArchie Cobbs 			}
312af7ab184SArchie Cobbs #endif
313af7ab184SArchie Cobbs 
314af7ab184SArchie Cobbs 			/* Initialize other state */
315af7ab184SArchie Cobbs 			d->cc = 0;
316af7ab184SArchie Cobbs 			d->flushed = 0;
317af7ab184SArchie Cobbs 			break;
318af7ab184SArchie Cobbs 		    }
319af7ab184SArchie Cobbs 
320af7ab184SArchie Cobbs 		case NGM_MPPC_RESETREQ:
321af7ab184SArchie Cobbs 			ng_mppc_reset_req(node);
322af7ab184SArchie Cobbs 			break;
323af7ab184SArchie Cobbs 
324af7ab184SArchie Cobbs 		default:
325af7ab184SArchie Cobbs 			error = EINVAL;
326af7ab184SArchie Cobbs 			break;
327af7ab184SArchie Cobbs 		}
328af7ab184SArchie Cobbs 		break;
329af7ab184SArchie Cobbs 	default:
330af7ab184SArchie Cobbs 		error = EINVAL;
331af7ab184SArchie Cobbs 		break;
332af7ab184SArchie Cobbs 	}
333af7ab184SArchie Cobbs done:
334069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
335069154d5SJulian Elischer 	NG_FREE_MSG(msg);
336af7ab184SArchie Cobbs 	return (error);
337af7ab184SArchie Cobbs }
338af7ab184SArchie Cobbs 
339af7ab184SArchie Cobbs /*
340af7ab184SArchie Cobbs  * Receive incoming data on our hook.
341af7ab184SArchie Cobbs  */
342af7ab184SArchie Cobbs static int
343069154d5SJulian Elischer ng_mppc_rcvdata(hook_p hook, item_p item)
344af7ab184SArchie Cobbs {
34530400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
34630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
347af7ab184SArchie Cobbs 	struct mbuf *out;
348af7ab184SArchie Cobbs 	int error;
349069154d5SJulian Elischer 	struct mbuf *m;
350af7ab184SArchie Cobbs 
351069154d5SJulian Elischer 	NGI_GET_M(item, m);
352af7ab184SArchie Cobbs 	/* Compress and/or encrypt */
353af7ab184SArchie Cobbs 	if (hook == priv->xmit.hook) {
354af7ab184SArchie Cobbs 		if (!priv->xmit.cfg.enable) {
355069154d5SJulian Elischer 			NG_FREE_M(m);
356069154d5SJulian Elischer 			NG_FREE_ITEM(item);
357af7ab184SArchie Cobbs 			return (ENXIO);
358af7ab184SArchie Cobbs 		}
359af7ab184SArchie Cobbs 		if ((error = ng_mppc_compress(node, m, &out)) != 0) {
360069154d5SJulian Elischer 			NG_FREE_M(m);
361069154d5SJulian Elischer 			NG_FREE_ITEM(item);
362af7ab184SArchie Cobbs 			return(error);
363af7ab184SArchie Cobbs 		}
364069154d5SJulian Elischer 		NG_FREE_M(m);
365069154d5SJulian Elischer 		NG_FWD_NEW_DATA(error, item, priv->xmit.hook, out);
366af7ab184SArchie Cobbs 		return (error);
367af7ab184SArchie Cobbs 	}
368af7ab184SArchie Cobbs 
369af7ab184SArchie Cobbs 	/* Decompress and/or decrypt */
370af7ab184SArchie Cobbs 	if (hook == priv->recv.hook) {
371af7ab184SArchie Cobbs 		if (!priv->recv.cfg.enable) {
372069154d5SJulian Elischer 			NG_FREE_M(m);
373069154d5SJulian Elischer 			NG_FREE_ITEM(item);
374af7ab184SArchie Cobbs 			return (ENXIO);
375af7ab184SArchie Cobbs 		}
376af7ab184SArchie Cobbs 		if ((error = ng_mppc_decompress(node, m, &out)) != 0) {
377069154d5SJulian Elischer 			NG_FREE_M(m);
378069154d5SJulian Elischer 			NG_FREE_ITEM(item);
379facfd889SArchie Cobbs 			if (error == EINVAL && priv->ctrlnode != 0) {
380af7ab184SArchie Cobbs 				struct ng_mesg *msg;
381af7ab184SArchie Cobbs 
382af7ab184SArchie Cobbs 				/* Need to send a reset-request */
383af7ab184SArchie Cobbs 				NG_MKMESSAGE(msg, NGM_MPPC_COOKIE,
384af7ab184SArchie Cobbs 				    NGM_MPPC_RESETREQ, 0, M_NOWAIT);
385af7ab184SArchie Cobbs 				if (msg == NULL)
386af7ab184SArchie Cobbs 					return (error);
387069154d5SJulian Elischer 				NG_SEND_MSG_ID(error, node, msg,
388facfd889SArchie Cobbs 					priv->ctrlnode, 0);
389af7ab184SArchie Cobbs 			}
390af7ab184SArchie Cobbs 			return (error);
391af7ab184SArchie Cobbs 		}
392069154d5SJulian Elischer 		NG_FREE_M(m);
393069154d5SJulian Elischer 		NG_FWD_NEW_DATA(error, item, priv->recv.hook, out);
394af7ab184SArchie Cobbs 		return (error);
395af7ab184SArchie Cobbs 	}
396af7ab184SArchie Cobbs 
397af7ab184SArchie Cobbs 	/* Oops */
3986e551fb6SDavid E. O'Brien 	panic("%s: unknown hook", __func__);
399b40ce416SJulian Elischer #ifdef RESTARTABLE_PANICS
400b40ce416SJulian Elischer 	return (EINVAL);
401b40ce416SJulian Elischer #endif
402af7ab184SArchie Cobbs }
403af7ab184SArchie Cobbs 
404af7ab184SArchie Cobbs /*
405af7ab184SArchie Cobbs  * Destroy node
406af7ab184SArchie Cobbs  */
407af7ab184SArchie Cobbs static int
408069154d5SJulian Elischer ng_mppc_shutdown(node_p node)
409af7ab184SArchie Cobbs {
41030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
411af7ab184SArchie Cobbs 
412af7ab184SArchie Cobbs 	/* Take down netgraph node */
413af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_COMPRESSION
414af7ab184SArchie Cobbs 	if (priv->xmit.history != NULL)
4159c8c302fSJulian Elischer 		FREE(priv->xmit.history, M_NETGRAPH_MPPC);
416af7ab184SArchie Cobbs 	if (priv->recv.history != NULL)
4179c8c302fSJulian Elischer 		FREE(priv->recv.history, M_NETGRAPH_MPPC);
418af7ab184SArchie Cobbs #endif
419af7ab184SArchie Cobbs 	bzero(priv, sizeof(*priv));
4209c8c302fSJulian Elischer 	FREE(priv, M_NETGRAPH_MPPC);
42130400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, NULL);
42230400f03SJulian Elischer 	NG_NODE_UNREF(node);		/* let the node escape */
423af7ab184SArchie Cobbs 	return (0);
424af7ab184SArchie Cobbs }
425af7ab184SArchie Cobbs 
426af7ab184SArchie Cobbs /*
427af7ab184SArchie Cobbs  * Hook disconnection
428af7ab184SArchie Cobbs  */
429af7ab184SArchie Cobbs static int
430af7ab184SArchie Cobbs ng_mppc_disconnect(hook_p hook)
431af7ab184SArchie Cobbs {
43230400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
43330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
434af7ab184SArchie Cobbs 
435af7ab184SArchie Cobbs 	/* Zero out hook pointer */
436af7ab184SArchie Cobbs 	if (hook == priv->xmit.hook)
437af7ab184SArchie Cobbs 		priv->xmit.hook = NULL;
438af7ab184SArchie Cobbs 	if (hook == priv->recv.hook)
439af7ab184SArchie Cobbs 		priv->recv.hook = NULL;
440af7ab184SArchie Cobbs 
441af7ab184SArchie Cobbs 	/* Go away if no longer connected */
44230400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(node) == 0)
44330400f03SJulian Elischer 	&& NG_NODE_IS_VALID(node))
444069154d5SJulian Elischer 		ng_rmnode_self(node);
445af7ab184SArchie Cobbs 	return (0);
446af7ab184SArchie Cobbs }
447af7ab184SArchie Cobbs 
448af7ab184SArchie Cobbs /************************************************************************
449af7ab184SArchie Cobbs 			HELPER STUFF
450af7ab184SArchie Cobbs  ************************************************************************/
451af7ab184SArchie Cobbs 
452af7ab184SArchie Cobbs /*
453af7ab184SArchie Cobbs  * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
454af7ab184SArchie Cobbs  * The original mbuf is not free'd.
455af7ab184SArchie Cobbs  */
456af7ab184SArchie Cobbs static int
457af7ab184SArchie Cobbs ng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
458af7ab184SArchie Cobbs {
45930400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
460af7ab184SArchie Cobbs 	struct ng_mppc_dir *const d = &priv->xmit;
461af7ab184SArchie Cobbs 	u_char *inbuf, *outbuf;
462af7ab184SArchie Cobbs 	int outlen, inlen;
463af7ab184SArchie Cobbs 	u_int16_t header;
464af7ab184SArchie Cobbs 
465af7ab184SArchie Cobbs 	/* Initialize */
466af7ab184SArchie Cobbs 	*resultp = NULL;
467af7ab184SArchie Cobbs 	header = d->cc;
468af7ab184SArchie Cobbs 	if (d->flushed) {
469af7ab184SArchie Cobbs 		header |= MPPC_FLAG_FLUSHED;
470af7ab184SArchie Cobbs 		d->flushed = 0;
471af7ab184SArchie Cobbs 	}
472af7ab184SArchie Cobbs 
473af7ab184SArchie Cobbs 	/* Work with contiguous regions of memory */
474af7ab184SArchie Cobbs 	inlen = m->m_pkthdr.len;
4759c8c302fSJulian Elischer 	MALLOC(inbuf, u_char *, inlen, M_NETGRAPH_MPPC, M_NOWAIT);
476af7ab184SArchie Cobbs 	if (inbuf == NULL)
477af7ab184SArchie Cobbs 		return (ENOMEM);
478af7ab184SArchie Cobbs 	m_copydata(m, 0, inlen, (caddr_t)inbuf);
479af7ab184SArchie Cobbs 	if ((d->cfg.bits & MPPC_BIT) != 0)
480af7ab184SArchie Cobbs 		outlen = MPPC_MAX_BLOWUP(inlen);
481af7ab184SArchie Cobbs 	else
482af7ab184SArchie Cobbs 		outlen = MPPC_HDRLEN + inlen;
4839c8c302fSJulian Elischer 	MALLOC(outbuf, u_char *, outlen, M_NETGRAPH_MPPC, M_NOWAIT);
484af7ab184SArchie Cobbs 	if (outbuf == NULL) {
4859c8c302fSJulian Elischer 		FREE(inbuf, M_NETGRAPH_MPPC);
486af7ab184SArchie Cobbs 		return (ENOMEM);
487af7ab184SArchie Cobbs 	}
488af7ab184SArchie Cobbs 
489af7ab184SArchie Cobbs 	/* Compress "inbuf" into "outbuf" (if compression enabled) */
490af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_COMPRESSION
491af7ab184SArchie Cobbs 	if ((d->cfg.bits & MPPC_BIT) != 0) {
492af7ab184SArchie Cobbs 		u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS;
493af7ab184SArchie Cobbs 		u_char *source, *dest;
494af7ab184SArchie Cobbs 		u_long sourceCnt, destCnt;
495af7ab184SArchie Cobbs 		int rtn;
496af7ab184SArchie Cobbs 
497af7ab184SArchie Cobbs 		/* Prepare to compress */
498af7ab184SArchie Cobbs 		source = inbuf;
499af7ab184SArchie Cobbs 		sourceCnt = inlen;
500af7ab184SArchie Cobbs 		dest = outbuf + MPPC_HDRLEN;
501af7ab184SArchie Cobbs 		destCnt = outlen - MPPC_HDRLEN;
502af7ab184SArchie Cobbs 		if ((d->cfg.bits & MPPE_STATELESS) == 0)
503af7ab184SArchie Cobbs 			flags |= MPPC_SAVE_HISTORY;
504af7ab184SArchie Cobbs 
505af7ab184SArchie Cobbs 		/* Compress */
506af7ab184SArchie Cobbs 		rtn = MPPC_Compress(&source, &dest, &sourceCnt,
507af7ab184SArchie Cobbs 			&destCnt, d->history, flags, 0);
508af7ab184SArchie Cobbs 
509af7ab184SArchie Cobbs 		/* Check return value */
5106e551fb6SDavid E. O'Brien 		KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
511af7ab184SArchie Cobbs 		if ((rtn & MPPC_EXPANDED) == 0
512af7ab184SArchie Cobbs 		    && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) {
513af7ab184SArchie Cobbs 			outlen -= destCnt;
514af7ab184SArchie Cobbs 			header |= MPPC_FLAG_COMPRESSED;
515af7ab184SArchie Cobbs 			if ((rtn & MPPC_RESTART_HISTORY) != 0)
516af7ab184SArchie Cobbs 				header |= MPPC_FLAG_RESTART;
517af7ab184SArchie Cobbs 		}
518af7ab184SArchie Cobbs 		d->flushed = (rtn & MPPC_EXPANDED) != 0
519af7ab184SArchie Cobbs 		    || (flags & MPPC_SAVE_HISTORY) == 0;
520af7ab184SArchie Cobbs 	}
521af7ab184SArchie Cobbs #endif
522af7ab184SArchie Cobbs 
523af7ab184SArchie Cobbs 	/* If we did not compress this packet, copy it to output buffer */
524af7ab184SArchie Cobbs 	if ((header & MPPC_FLAG_COMPRESSED) == 0) {
525af7ab184SArchie Cobbs 		bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen);
526af7ab184SArchie Cobbs 		outlen = MPPC_HDRLEN + inlen;
527af7ab184SArchie Cobbs 	}
5289c8c302fSJulian Elischer 	FREE(inbuf, M_NETGRAPH_MPPC);
529af7ab184SArchie Cobbs 
530af7ab184SArchie Cobbs 	/* Always set the flushed bit in stateless mode */
531af7ab184SArchie Cobbs 	if ((d->cfg.bits & MPPE_STATELESS) != 0)
532af7ab184SArchie Cobbs 		header |= MPPC_FLAG_FLUSHED;
533af7ab184SArchie Cobbs 
534af7ab184SArchie Cobbs 	/* Now encrypt packet (if encryption enabled) */
535af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_ENCRYPTION
536af7ab184SArchie Cobbs 	if ((d->cfg.bits & MPPE_BITS) != 0) {
537af7ab184SArchie Cobbs 
538af7ab184SArchie Cobbs 		/* Set header bits; need to reset key if we say we did */
539af7ab184SArchie Cobbs 		header |= MPPC_FLAG_ENCRYPTED;
540af7ab184SArchie Cobbs 		if ((header & MPPC_FLAG_FLUSHED) != 0)
541af7ab184SArchie Cobbs 			rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
542af7ab184SArchie Cobbs 
543af7ab184SArchie Cobbs 		/* Update key if it's time */
544af7ab184SArchie Cobbs 		if ((d->cfg.bits & MPPE_STATELESS) != 0
545af7ab184SArchie Cobbs 		    || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
546af7ab184SArchie Cobbs 			  ng_mppc_updatekey(d->cfg.bits,
547af7ab184SArchie Cobbs 			      d->cfg.startkey, d->key, &d->rc4);
548af7ab184SArchie Cobbs 		}
549af7ab184SArchie Cobbs 
550af7ab184SArchie Cobbs 		/* Encrypt packet */
551af7ab184SArchie Cobbs 		rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN,
552af7ab184SArchie Cobbs 			outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN);
553af7ab184SArchie Cobbs 	}
554af7ab184SArchie Cobbs #endif
555af7ab184SArchie Cobbs 
556af7ab184SArchie Cobbs 	/* Update sequence number */
557af7ab184SArchie Cobbs 	d->cc++;
558af7ab184SArchie Cobbs 
559af7ab184SArchie Cobbs 	/* Install header */
560af7ab184SArchie Cobbs 	*((u_int16_t *)outbuf) = htons(header);
561af7ab184SArchie Cobbs 
562af7ab184SArchie Cobbs 	/* Return packet in an mbuf */
563af7ab184SArchie Cobbs 	*resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL);
5649c8c302fSJulian Elischer 	FREE(outbuf, M_NETGRAPH_MPPC);
565af7ab184SArchie Cobbs 	return (*resultp == NULL ? ENOBUFS : 0);
566af7ab184SArchie Cobbs }
567af7ab184SArchie Cobbs 
568af7ab184SArchie Cobbs /*
569af7ab184SArchie Cobbs  * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
570af7ab184SArchie Cobbs  * The original mbuf is not free'd.
571af7ab184SArchie Cobbs  */
572af7ab184SArchie Cobbs static int
573af7ab184SArchie Cobbs ng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
574af7ab184SArchie Cobbs {
57530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
576af7ab184SArchie Cobbs 	struct ng_mppc_dir *const d = &priv->recv;
577f3059f39SArchie Cobbs 	u_int16_t header, cc;
578f3059f39SArchie Cobbs 	u_int numLost;
579af7ab184SArchie Cobbs 	u_char *buf;
580af7ab184SArchie Cobbs 	int len;
581af7ab184SArchie Cobbs 
582af7ab184SArchie Cobbs 	/* Pull off header */
583af7ab184SArchie Cobbs 	if (m->m_pkthdr.len < MPPC_HDRLEN)
584af7ab184SArchie Cobbs 		return (EINVAL);
585af7ab184SArchie Cobbs 	m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header);
586fd8e4ebcSMike Barcroft 	header = ntohs(header);
587af7ab184SArchie Cobbs 	cc = (header & MPPC_CCOUNT_MASK);
588af7ab184SArchie Cobbs 
589af7ab184SArchie Cobbs 	/* Copy payload into a contiguous region of memory */
590af7ab184SArchie Cobbs 	len = m->m_pkthdr.len - MPPC_HDRLEN;
5919c8c302fSJulian Elischer 	MALLOC(buf, u_char *, len, M_NETGRAPH_MPPC, M_NOWAIT);
592af7ab184SArchie Cobbs 	if (buf == NULL)
593af7ab184SArchie Cobbs 		return (ENOMEM);
594af7ab184SArchie Cobbs 	m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf);
595af7ab184SArchie Cobbs 
596f3059f39SArchie Cobbs 	/* Check for an unexpected jump in the sequence number */
597af7ab184SArchie Cobbs 	numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK);
598af7ab184SArchie Cobbs 
599af7ab184SArchie Cobbs 	/* If flushed bit set, we can always handle packet */
600af7ab184SArchie Cobbs 	if ((header & MPPC_FLAG_FLUSHED) != 0) {
601af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_COMPRESSION
602af7ab184SArchie Cobbs 		if (d->history != NULL)
603af7ab184SArchie Cobbs 			MPPC_InitDecompressionHistory(d->history);
604af7ab184SArchie Cobbs #endif
605af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_ENCRYPTION
606af7ab184SArchie Cobbs 		if ((d->cfg.bits & MPPE_BITS) != 0) {
607f3059f39SArchie Cobbs 			u_int rekey;
608af7ab184SArchie Cobbs 
609f3059f39SArchie Cobbs 			/* How many times are we going to have to re-key? */
610f3059f39SArchie Cobbs 			rekey = ((d->cfg.bits & MPPE_STATELESS) != 0) ?
611f3059f39SArchie Cobbs 			    numLost : (numLost / (MPPE_UPDATE_MASK + 1));
612f3059f39SArchie Cobbs 			if (rekey > MPPE_MAX_REKEY) {
613f3059f39SArchie Cobbs 				log(LOG_ERR, "%s: too many (%d) packets"
614f3059f39SArchie Cobbs 				    " dropped, disabling node %p!",
615f3059f39SArchie Cobbs 				    __func__, numLost, node);
616f3059f39SArchie Cobbs 				priv->recv.cfg.enable = 0;
617f3059f39SArchie Cobbs 				goto failed;
618f3059f39SArchie Cobbs 			}
619f3059f39SArchie Cobbs 
620f3059f39SArchie Cobbs 			/* Re-key as necessary to catch up to peer */
621af7ab184SArchie Cobbs 			while (d->cc != cc) {
622f3059f39SArchie Cobbs 				if ((d->cfg.bits & MPPE_STATELESS) != 0
623af7ab184SArchie Cobbs 				    || (d->cc & MPPE_UPDATE_MASK)
624af7ab184SArchie Cobbs 				      == MPPE_UPDATE_FLAG) {
625af7ab184SArchie Cobbs 					ng_mppc_updatekey(d->cfg.bits,
626af7ab184SArchie Cobbs 					    d->cfg.startkey, d->key, &d->rc4);
627af7ab184SArchie Cobbs 				}
628af7ab184SArchie Cobbs 				d->cc++;
629af7ab184SArchie Cobbs 			}
630af7ab184SArchie Cobbs 
631af7ab184SArchie Cobbs 			/* Reset key (except in stateless mode, see below) */
632af7ab184SArchie Cobbs 			if ((d->cfg.bits & MPPE_STATELESS) == 0)
633af7ab184SArchie Cobbs 				rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
634af7ab184SArchie Cobbs 		}
635af7ab184SArchie Cobbs #endif
636af7ab184SArchie Cobbs 		d->cc = cc;		/* skip over lost seq numbers */
637af7ab184SArchie Cobbs 		numLost = 0;		/* act like no packets were lost */
638af7ab184SArchie Cobbs 	}
639af7ab184SArchie Cobbs 
640af7ab184SArchie Cobbs 	/* Can't decode non-sequential packets without a flushed bit */
641af7ab184SArchie Cobbs 	if (numLost != 0)
642af7ab184SArchie Cobbs 		goto failed;
643af7ab184SArchie Cobbs 
644af7ab184SArchie Cobbs 	/* Decrypt packet */
645af7ab184SArchie Cobbs 	if ((header & MPPC_FLAG_ENCRYPTED) != 0) {
646af7ab184SArchie Cobbs 
647af7ab184SArchie Cobbs 		/* Are we not expecting encryption? */
648af7ab184SArchie Cobbs 		if ((d->cfg.bits & MPPE_BITS) == 0) {
649af7ab184SArchie Cobbs 			log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
6506e551fb6SDavid E. O'Brien 				__func__, "encrypted");
651af7ab184SArchie Cobbs 			goto failed;
652af7ab184SArchie Cobbs 		}
653af7ab184SArchie Cobbs 
654af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_ENCRYPTION
655af7ab184SArchie Cobbs 		/* Update key if it's time (always in stateless mode) */
656af7ab184SArchie Cobbs 		if ((d->cfg.bits & MPPE_STATELESS) != 0
657af7ab184SArchie Cobbs 		    || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
658af7ab184SArchie Cobbs 			ng_mppc_updatekey(d->cfg.bits,
659af7ab184SArchie Cobbs 			    d->cfg.startkey, d->key, &d->rc4);
660af7ab184SArchie Cobbs 		}
661af7ab184SArchie Cobbs 
662af7ab184SArchie Cobbs 		/* Decrypt packet */
663af7ab184SArchie Cobbs 		rc4_crypt(&d->rc4, buf, buf, len);
664af7ab184SArchie Cobbs #endif
665af7ab184SArchie Cobbs 	} else {
666af7ab184SArchie Cobbs 
667af7ab184SArchie Cobbs 		/* Are we expecting encryption? */
668af7ab184SArchie Cobbs 		if ((d->cfg.bits & MPPE_BITS) != 0) {
669af7ab184SArchie Cobbs 			log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
6706e551fb6SDavid E. O'Brien 				__func__, "unencrypted");
671af7ab184SArchie Cobbs 			goto failed;
672af7ab184SArchie Cobbs 		}
673af7ab184SArchie Cobbs 	}
674af7ab184SArchie Cobbs 
675af7ab184SArchie Cobbs 	/* Update coherency count for next time (12 bit arithmetic) */
676af7ab184SArchie Cobbs 	d->cc++;
677af7ab184SArchie Cobbs 
678af7ab184SArchie Cobbs 	/* Check for unexpected compressed packet */
679af7ab184SArchie Cobbs 	if ((header & MPPC_FLAG_COMPRESSED) != 0
680af7ab184SArchie Cobbs 	    && (d->cfg.bits & MPPC_BIT) == 0) {
681af7ab184SArchie Cobbs 		log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
6826e551fb6SDavid E. O'Brien 			__func__, "compressed");
683af7ab184SArchie Cobbs failed:
6849c8c302fSJulian Elischer 		FREE(buf, M_NETGRAPH_MPPC);
685af7ab184SArchie Cobbs 		return (EINVAL);
686af7ab184SArchie Cobbs 	}
687af7ab184SArchie Cobbs 
688af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_COMPRESSION
689af7ab184SArchie Cobbs 	/* Decompress packet */
690af7ab184SArchie Cobbs 	if ((header & MPPC_FLAG_COMPRESSED) != 0) {
691af7ab184SArchie Cobbs 		int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS;
692af7ab184SArchie Cobbs 		u_char *decompbuf, *source, *dest;
693af7ab184SArchie Cobbs 		u_long sourceCnt, destCnt;
694af7ab184SArchie Cobbs 		int decomplen, rtn;
695af7ab184SArchie Cobbs 
696af7ab184SArchie Cobbs 		/* Allocate a buffer for decompressed data */
697af7ab184SArchie Cobbs 		MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE
6989c8c302fSJulian Elischer 		    + MPPC_DECOMP_SAFETY, M_NETGRAPH_MPPC, M_NOWAIT);
699af7ab184SArchie Cobbs 		if (decompbuf == NULL) {
7009c8c302fSJulian Elischer 			FREE(buf, M_NETGRAPH_MPPC);
701af7ab184SArchie Cobbs 			return (ENOMEM);
702af7ab184SArchie Cobbs 		}
703af7ab184SArchie Cobbs 		decomplen = MPPC_DECOMP_BUFSIZE;
704af7ab184SArchie Cobbs 
705af7ab184SArchie Cobbs 		/* Prepare to decompress */
706af7ab184SArchie Cobbs 		source = buf;
707af7ab184SArchie Cobbs 		sourceCnt = len;
708af7ab184SArchie Cobbs 		dest = decompbuf;
709af7ab184SArchie Cobbs 		destCnt = decomplen;
710af7ab184SArchie Cobbs 		if ((header & MPPC_FLAG_RESTART) != 0)
711af7ab184SArchie Cobbs 			flags |= MPPC_RESTART_HISTORY;
712af7ab184SArchie Cobbs 
713af7ab184SArchie Cobbs 		/* Decompress */
714af7ab184SArchie Cobbs 		rtn = MPPC_Decompress(&source, &dest,
715af7ab184SArchie Cobbs 			&sourceCnt, &destCnt, d->history, flags);
716af7ab184SArchie Cobbs 
717af7ab184SArchie Cobbs 		/* Check return value */
7186e551fb6SDavid E. O'Brien 		KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
719af7ab184SArchie Cobbs 		if ((rtn & MPPC_DEST_EXHAUSTED) != 0
720af7ab184SArchie Cobbs 		    || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) {
721af7ab184SArchie Cobbs 			log(LOG_ERR, "%s: decomp returned 0x%x",
7226e551fb6SDavid E. O'Brien 			    __func__, rtn);
7239c8c302fSJulian Elischer 			FREE(decompbuf, M_NETGRAPH_MPPC);
724af7ab184SArchie Cobbs 			goto failed;
725af7ab184SArchie Cobbs 		}
726af7ab184SArchie Cobbs 
727af7ab184SArchie Cobbs 		/* Replace compressed data with decompressed data */
7289c8c302fSJulian Elischer 		FREE(buf, M_NETGRAPH_MPPC);
729af7ab184SArchie Cobbs 		buf = decompbuf;
730af7ab184SArchie Cobbs 		len = decomplen - destCnt;
731af7ab184SArchie Cobbs 	}
732af7ab184SArchie Cobbs #endif
733af7ab184SArchie Cobbs 
734af7ab184SArchie Cobbs 	/* Return result in an mbuf */
735af7ab184SArchie Cobbs 	*resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL);
7369c8c302fSJulian Elischer 	FREE(buf, M_NETGRAPH_MPPC);
737af7ab184SArchie Cobbs 	return (*resultp == NULL ? ENOBUFS : 0);
738af7ab184SArchie Cobbs }
739af7ab184SArchie Cobbs 
740af7ab184SArchie Cobbs /*
741af7ab184SArchie Cobbs  * The peer has sent us a CCP ResetRequest, so reset our transmit state.
742af7ab184SArchie Cobbs  */
743af7ab184SArchie Cobbs static void
744af7ab184SArchie Cobbs ng_mppc_reset_req(node_p node)
745af7ab184SArchie Cobbs {
74630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
747af7ab184SArchie Cobbs 	struct ng_mppc_dir *const d = &priv->xmit;
748af7ab184SArchie Cobbs 
749af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_COMPRESSION
750af7ab184SArchie Cobbs 	if (d->history != NULL)
751af7ab184SArchie Cobbs 		MPPC_InitCompressionHistory(d->history);
752af7ab184SArchie Cobbs #endif
753af7ab184SArchie Cobbs #ifdef NETGRAPH_MPPC_ENCRYPTION
754af7ab184SArchie Cobbs 	if ((d->cfg.bits & MPPE_STATELESS) == 0)
755af7ab184SArchie Cobbs 		rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
756af7ab184SArchie Cobbs #endif
757af7ab184SArchie Cobbs 	d->flushed = 1;
758af7ab184SArchie Cobbs }
759af7ab184SArchie Cobbs 
760af7ab184SArchie Cobbs /*
761af7ab184SArchie Cobbs  * Generate a new encryption key
762af7ab184SArchie Cobbs  */
763af7ab184SArchie Cobbs static void
764af7ab184SArchie Cobbs ng_mppc_getkey(const u_char *h, u_char *h2, int len)
765af7ab184SArchie Cobbs {
766af7ab184SArchie Cobbs 	static const u_char pad1[10] =
767af7ab184SArchie Cobbs 	    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
768af7ab184SArchie Cobbs 	static const u_char pad2[10] =
769af7ab184SArchie Cobbs 	    { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, };
770af7ab184SArchie Cobbs 	u_char hash[20];
771af7ab184SArchie Cobbs 	SHA1_CTX c;
772af7ab184SArchie Cobbs 	int k;
773af7ab184SArchie Cobbs 
774af7ab184SArchie Cobbs 	bzero(&hash, sizeof(hash));
775af7ab184SArchie Cobbs 	SHA1Init(&c);
776af7ab184SArchie Cobbs 	SHA1Update(&c, h, len);
777af7ab184SArchie Cobbs 	for (k = 0; k < 4; k++)
778af7ab184SArchie Cobbs 		SHA1Update(&c, pad1, sizeof(pad2));
779af7ab184SArchie Cobbs 	SHA1Update(&c, h2, len);
780af7ab184SArchie Cobbs 	for (k = 0; k < 4; k++)
781af7ab184SArchie Cobbs 		SHA1Update(&c, pad2, sizeof(pad2));
782af7ab184SArchie Cobbs 	SHA1Final(hash, &c);
783af7ab184SArchie Cobbs 	bcopy(hash, h2, len);
784af7ab184SArchie Cobbs }
785af7ab184SArchie Cobbs 
786af7ab184SArchie Cobbs /*
787af7ab184SArchie Cobbs  * Update the encryption key
788af7ab184SArchie Cobbs  */
789af7ab184SArchie Cobbs static void
790af7ab184SArchie Cobbs ng_mppc_updatekey(u_int32_t bits,
791af7ab184SArchie Cobbs 	u_char *key0, u_char *key, struct rc4_state *rc4)
792af7ab184SArchie Cobbs {
793af7ab184SArchie Cobbs 	const int keylen = KEYLEN(bits);
794af7ab184SArchie Cobbs 
795af7ab184SArchie Cobbs 	ng_mppc_getkey(key0, key, keylen);
796af7ab184SArchie Cobbs 	rc4_init(rc4, key, keylen);
797af7ab184SArchie Cobbs 	rc4_crypt(rc4, key, key, keylen);
79834fd2381SArchie Cobbs 	if ((bits & MPPE_40) != 0)
79934fd2381SArchie Cobbs 		bcopy(&ng_mppe_weakenkey, key, 3);
80034fd2381SArchie Cobbs 	else if ((bits & MPPE_56) != 0)
80134fd2381SArchie Cobbs 		bcopy(&ng_mppe_weakenkey, key, 1);
802af7ab184SArchie Cobbs 	rc4_init(rc4, key, keylen);
803af7ab184SArchie Cobbs }
804af7ab184SArchie Cobbs 
805