2000-04-09 21:04:55 +00:00
|
|
|
/*
|
|
|
|
* ng_mppc.c
|
2005-01-07 01:45:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
2000-04-09 21:04:55 +00:00
|
|
|
* Copyright (c) 1996-2000 Whistle Communications, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Subject to the following obligations and disclaimer of warranty, use and
|
|
|
|
* redistribution of this software, in source or object code forms, with or
|
|
|
|
* without modifications are expressly permitted by Whistle Communications;
|
|
|
|
* provided, however, that:
|
|
|
|
* 1. Any and all reproductions of the source or object code must include the
|
|
|
|
* copyright notice above and the following disclaimer of warranties; and
|
|
|
|
* 2. No rights are granted, in any manner or form, to use Whistle
|
|
|
|
* Communications, Inc. trademarks, including the mark "WHISTLE
|
|
|
|
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
|
|
|
|
* such appears in the above copyright notice or in the software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
|
|
|
|
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
|
|
|
|
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
|
|
|
|
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
|
|
|
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
|
|
|
|
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
|
|
|
|
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
|
|
|
|
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
|
|
|
|
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
|
|
|
|
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
2000-10-24 17:32:45 +00:00
|
|
|
* Author: Archie Cobbs <archie@freebsd.org>
|
2000-04-09 21:04:55 +00:00
|
|
|
*
|
|
|
|
* $Whistle: ng_mppc.c,v 1.4 1999/11/25 00:10:12 archie Exp $
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Microsoft PPP compression (MPPC) and encryption (MPPE) netgraph node type.
|
|
|
|
*
|
|
|
|
* You must define one or both of the NETGRAPH_MPPC_COMPRESSION and/or
|
|
|
|
* NETGRAPH_MPPC_ENCRYPTION options for this node type to be useful.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/malloc.h>
|
2010-03-31 22:47:55 +00:00
|
|
|
#include <sys/endian.h>
|
2000-04-09 21:04:55 +00:00
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
#include <netgraph/ng_message.h>
|
|
|
|
#include <netgraph/netgraph.h>
|
|
|
|
#include <netgraph/ng_mppc.h>
|
|
|
|
|
|
|
|
#include "opt_netgraph.h"
|
|
|
|
|
|
|
|
#if !defined(NETGRAPH_MPPC_COMPRESSION) && !defined(NETGRAPH_MPPC_ENCRYPTION)
|
2005-10-14 23:17:45 +00:00
|
|
|
#ifdef KLD_MODULE
|
|
|
|
/* XXX NETGRAPH_MPPC_COMPRESSION isn't functional yet */
|
|
|
|
#define NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
#else
|
|
|
|
/* This case is indicative of an error in sys/conf files */
|
2000-04-09 21:04:55 +00:00
|
|
|
#error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
#endif
|
2005-10-14 23:17:45 +00:00
|
|
|
#endif
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2001-01-10 07:13:58 +00:00
|
|
|
#ifdef NG_SEPARATE_MALLOC
|
2011-11-07 06:44:47 +00:00
|
|
|
static MALLOC_DEFINE(M_NETGRAPH_MPPC, "netgraph_mppc", "netgraph mppc node");
|
2001-01-10 07:13:58 +00:00
|
|
|
#else
|
|
|
|
#define M_NETGRAPH_MPPC M_NETGRAPH
|
|
|
|
#endif
|
|
|
|
|
2000-04-09 21:04:55 +00:00
|
|
|
#ifdef NETGRAPH_MPPC_COMPRESSION
|
|
|
|
/* XXX this file doesn't exist yet, but hopefully someday it will... */
|
|
|
|
#include <net/mppc.h>
|
|
|
|
#endif
|
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
#include <crypto/rc4/rc4.h>
|
|
|
|
#endif
|
|
|
|
#include <crypto/sha1.h>
|
|
|
|
|
|
|
|
/* Decompression blowup */
|
|
|
|
#define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */
|
|
|
|
#define MPPC_DECOMP_SAFETY 100 /* plus this much margin */
|
|
|
|
|
|
|
|
/* MPPC/MPPE header length */
|
|
|
|
#define MPPC_HDRLEN 2
|
|
|
|
|
|
|
|
/* Key length */
|
|
|
|
#define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8)
|
|
|
|
|
2002-12-14 00:56:07 +00:00
|
|
|
/*
|
|
|
|
* When packets are lost with MPPE, we may have to re-key arbitrarily
|
|
|
|
* many times to 'catch up' to the new jumped-ahead sequence number.
|
|
|
|
* Since this can be expensive, we pose a limit on how many re-keyings
|
|
|
|
* we will do at one time to avoid a possible D.O.S. vulnerability.
|
|
|
|
* This should instead be a configurable parameter.
|
|
|
|
*/
|
|
|
|
#define MPPE_MAX_REKEY 1000
|
2000-04-09 21:04:55 +00:00
|
|
|
|
|
|
|
/* MPPC packet header bits */
|
|
|
|
#define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */
|
|
|
|
#define MPPC_FLAG_RESTART 0x4000 /* compress history restart */
|
|
|
|
#define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */
|
|
|
|
#define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */
|
|
|
|
#define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */
|
|
|
|
|
2007-05-04 16:05:58 +00:00
|
|
|
#define MPPC_CCOUNT_INC(d) ((d) = (((d) + 1) & MPPC_CCOUNT_MASK))
|
|
|
|
|
2000-04-09 21:04:55 +00:00
|
|
|
#define MPPE_UPDATE_MASK 0xff /* coherency count when we're */
|
|
|
|
#define MPPE_UPDATE_FLAG 0xff /* supposed to update key */
|
|
|
|
|
|
|
|
#define MPPC_COMP_OK 0x05
|
|
|
|
#define MPPC_DECOMP_OK 0x05
|
|
|
|
|
|
|
|
/* Per direction info */
|
|
|
|
struct ng_mppc_dir {
|
|
|
|
struct ng_mppc_config cfg; /* configuration */
|
|
|
|
hook_p hook; /* netgraph hook */
|
|
|
|
u_int16_t cc:12; /* coherency count */
|
|
|
|
u_char flushed; /* clean history (xmit only) */
|
|
|
|
#ifdef NETGRAPH_MPPC_COMPRESSION
|
|
|
|
u_char *history; /* compression history */
|
|
|
|
#endif
|
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
u_char key[MPPE_KEY_LEN]; /* session key */
|
|
|
|
struct rc4_state rc4; /* rc4 state */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Node private data */
|
|
|
|
struct ng_mppc_private {
|
|
|
|
struct ng_mppc_dir xmit; /* compress/encrypt config */
|
|
|
|
struct ng_mppc_dir recv; /* decompress/decrypt config */
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_ID_t ctrlnode; /* path to controlling node */
|
2000-04-09 21:04:55 +00:00
|
|
|
};
|
|
|
|
typedef struct ng_mppc_private *priv_p;
|
|
|
|
|
|
|
|
/* Netgraph node methods */
|
|
|
|
static ng_constructor_t ng_mppc_constructor;
|
|
|
|
static ng_rcvmsg_t ng_mppc_rcvmsg;
|
2001-01-06 00:46:47 +00:00
|
|
|
static ng_shutdown_t ng_mppc_shutdown;
|
2000-04-09 21:04:55 +00:00
|
|
|
static ng_newhook_t ng_mppc_newhook;
|
|
|
|
static ng_rcvdata_t ng_mppc_rcvdata;
|
|
|
|
static ng_disconnect_t ng_mppc_disconnect;
|
|
|
|
|
|
|
|
/* Helper functions */
|
|
|
|
static int ng_mppc_compress(node_p node,
|
2007-05-11 14:36:02 +00:00
|
|
|
struct mbuf **datap);
|
2000-04-09 21:04:55 +00:00
|
|
|
static int ng_mppc_decompress(node_p node,
|
2007-05-11 14:36:02 +00:00
|
|
|
struct mbuf **datap);
|
2007-05-18 15:28:01 +00:00
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
2000-04-09 21:04:55 +00:00
|
|
|
static void ng_mppc_getkey(const u_char *h, u_char *h2, int len);
|
|
|
|
static void ng_mppc_updatekey(u_int32_t bits,
|
|
|
|
u_char *key0, u_char *key, struct rc4_state *rc4);
|
2007-05-18 15:28:01 +00:00
|
|
|
#endif
|
2000-04-09 21:04:55 +00:00
|
|
|
static void ng_mppc_reset_req(node_p node);
|
|
|
|
|
|
|
|
/* Node type descriptor */
|
|
|
|
static struct ng_type ng_mppc_typestruct = {
|
2004-05-29 00:51:19 +00:00
|
|
|
.version = NG_ABI_VERSION,
|
|
|
|
.name = NG_MPPC_NODE_TYPE,
|
|
|
|
.constructor = ng_mppc_constructor,
|
|
|
|
.rcvmsg = ng_mppc_rcvmsg,
|
|
|
|
.shutdown = ng_mppc_shutdown,
|
|
|
|
.newhook = ng_mppc_newhook,
|
|
|
|
.rcvdata = ng_mppc_rcvdata,
|
|
|
|
.disconnect = ng_mppc_disconnect,
|
2000-04-09 21:04:55 +00:00
|
|
|
};
|
|
|
|
NETGRAPH_INIT(mppc, &ng_mppc_typestruct);
|
|
|
|
|
2003-02-05 19:11:11 +00:00
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
/* Depend on separate rc4 module */
|
|
|
|
MODULE_DEPEND(ng_mppc, rc4, 1, 1, 1);
|
|
|
|
#endif
|
|
|
|
|
2001-12-15 02:07:32 +00:00
|
|
|
/* Fixed bit pattern to weaken keysize down to 40 or 56 bits */
|
2000-04-09 21:04:55 +00:00
|
|
|
static const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e };
|
|
|
|
|
|
|
|
#define ERROUT(x) do { error = (x); goto done; } while (0)
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
NETGRAPH NODE STUFF
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Node type constructor
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_mppc_constructor(node_p node)
|
2000-04-09 21:04:55 +00:00
|
|
|
{
|
|
|
|
priv_p priv;
|
|
|
|
|
|
|
|
/* Allocate private structure */
|
2011-04-18 09:12:27 +00:00
|
|
|
priv = malloc(sizeof(*priv), M_NETGRAPH_MPPC, M_WAITOK | M_ZERO);
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, priv);
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2005-06-03 09:06:00 +00:00
|
|
|
/* This node is not thread safe. */
|
|
|
|
NG_NODE_FORCE_WRITER(node);
|
|
|
|
|
2000-04-09 21:04:55 +00:00
|
|
|
/* Done */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give our OK for a hook to be added
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_mppc_newhook(node_p node, hook_p hook, const char *name)
|
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
2000-04-09 21:04:55 +00:00
|
|
|
hook_p *hookPtr;
|
|
|
|
|
|
|
|
/* Check hook name */
|
|
|
|
if (strcmp(name, NG_MPPC_HOOK_COMP) == 0)
|
|
|
|
hookPtr = &priv->xmit.hook;
|
|
|
|
else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0)
|
|
|
|
hookPtr = &priv->recv.hook;
|
|
|
|
else
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* See if already connected */
|
|
|
|
if (*hookPtr != NULL)
|
|
|
|
return (EISCONN);
|
|
|
|
|
|
|
|
/* OK */
|
|
|
|
*hookPtr = hook;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a control message
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_mppc_rcvmsg(node_p node, item_p item, hook_p lasthook)
|
2000-04-09 21:04:55 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
2000-04-09 21:04:55 +00:00
|
|
|
struct ng_mesg *resp = NULL;
|
|
|
|
int error = 0;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct ng_mesg *msg;
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
NGI_GET_MSG(item, msg);
|
2000-04-09 21:04:55 +00:00
|
|
|
switch (msg->header.typecookie) {
|
|
|
|
case NGM_MPPC_COOKIE:
|
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_MPPC_CONFIG_COMP:
|
|
|
|
case NGM_MPPC_CONFIG_DECOMP:
|
|
|
|
{
|
|
|
|
struct ng_mppc_config *const cfg
|
|
|
|
= (struct ng_mppc_config *)msg->data;
|
|
|
|
const int isComp =
|
|
|
|
msg->header.cmd == NGM_MPPC_CONFIG_COMP;
|
|
|
|
struct ng_mppc_dir *const d = isComp ?
|
|
|
|
&priv->xmit : &priv->recv;
|
|
|
|
|
|
|
|
/* Check configuration */
|
|
|
|
if (msg->header.arglen != sizeof(*cfg))
|
|
|
|
ERROUT(EINVAL);
|
|
|
|
if (cfg->enable) {
|
|
|
|
if ((cfg->bits & ~MPPC_VALID_BITS) != 0)
|
|
|
|
ERROUT(EINVAL);
|
|
|
|
#ifndef NETGRAPH_MPPC_COMPRESSION
|
|
|
|
if ((cfg->bits & MPPC_BIT) != 0)
|
|
|
|
ERROUT(EPROTONOSUPPORT);
|
|
|
|
#endif
|
|
|
|
#ifndef NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
if ((cfg->bits & MPPE_BITS) != 0)
|
|
|
|
ERROUT(EPROTONOSUPPORT);
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
cfg->bits = 0;
|
|
|
|
|
|
|
|
/* Save return address so we can send reset-req's */
|
2002-12-14 00:56:07 +00:00
|
|
|
if (!isComp)
|
|
|
|
priv->ctrlnode = NGI_RETADDR(item);
|
2000-04-09 21:04:55 +00:00
|
|
|
|
|
|
|
/* Configuration is OK, reset to it */
|
|
|
|
d->cfg = *cfg;
|
|
|
|
|
|
|
|
#ifdef NETGRAPH_MPPC_COMPRESSION
|
|
|
|
/* Initialize state buffers for compression */
|
|
|
|
if (d->history != NULL) {
|
2008-10-23 15:53:51 +00:00
|
|
|
free(d->history, M_NETGRAPH_MPPC);
|
2000-04-09 21:04:55 +00:00
|
|
|
d->history = NULL;
|
|
|
|
}
|
|
|
|
if ((cfg->bits & MPPC_BIT) != 0) {
|
2008-10-23 20:26:15 +00:00
|
|
|
d->history = malloc(isComp ?
|
|
|
|
MPPC_SizeOfCompressionHistory() :
|
2000-04-09 21:04:55 +00:00
|
|
|
MPPC_SizeOfDecompressionHistory(),
|
2001-01-10 07:13:58 +00:00
|
|
|
M_NETGRAPH_MPPC, M_NOWAIT);
|
2000-04-09 21:04:55 +00:00
|
|
|
if (d->history == NULL)
|
|
|
|
ERROUT(ENOMEM);
|
|
|
|
if (isComp)
|
|
|
|
MPPC_InitCompressionHistory(d->history);
|
|
|
|
else {
|
|
|
|
MPPC_InitDecompressionHistory(
|
|
|
|
d->history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
/* Generate initial session keys for encryption */
|
|
|
|
if ((cfg->bits & MPPE_BITS) != 0) {
|
|
|
|
const int keylen = KEYLEN(cfg->bits);
|
|
|
|
|
|
|
|
bcopy(cfg->startkey, d->key, keylen);
|
|
|
|
ng_mppc_getkey(cfg->startkey, d->key, keylen);
|
2001-12-15 02:07:32 +00:00
|
|
|
if ((cfg->bits & MPPE_40) != 0)
|
|
|
|
bcopy(&ng_mppe_weakenkey, d->key, 3);
|
|
|
|
else if ((cfg->bits & MPPE_56) != 0)
|
|
|
|
bcopy(&ng_mppe_weakenkey, d->key, 1);
|
2000-04-09 21:04:55 +00:00
|
|
|
rc4_init(&d->rc4, d->key, keylen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialize other state */
|
|
|
|
d->cc = 0;
|
|
|
|
d->flushed = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NGM_MPPC_RESETREQ:
|
|
|
|
ng_mppc_reset_req(node);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
done:
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_RESPOND_MSG(error, node, item, resp);
|
|
|
|
NG_FREE_MSG(msg);
|
2000-04-09 21:04:55 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive incoming data on our hook.
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_mppc_rcvdata(hook_p hook, item_p item)
|
2000-04-09 21:04:55 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const node_p node = NG_HOOK_NODE(hook);
|
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
2000-04-09 21:04:55 +00:00
|
|
|
int error;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct mbuf *m;
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
NGI_GET_M(item, m);
|
2000-04-09 21:04:55 +00:00
|
|
|
/* Compress and/or encrypt */
|
|
|
|
if (hook == priv->xmit.hook) {
|
|
|
|
if (!priv->xmit.cfg.enable) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_M(m);
|
|
|
|
NG_FREE_ITEM(item);
|
2000-04-09 21:04:55 +00:00
|
|
|
return (ENXIO);
|
|
|
|
}
|
2007-05-11 14:36:02 +00:00
|
|
|
if ((error = ng_mppc_compress(node, &m)) != 0) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_ITEM(item);
|
2000-04-09 21:04:55 +00:00
|
|
|
return(error);
|
|
|
|
}
|
2007-05-11 14:36:02 +00:00
|
|
|
NG_FWD_NEW_DATA(error, item, priv->xmit.hook, m);
|
2000-04-09 21:04:55 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decompress and/or decrypt */
|
|
|
|
if (hook == priv->recv.hook) {
|
|
|
|
if (!priv->recv.cfg.enable) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_M(m);
|
|
|
|
NG_FREE_ITEM(item);
|
2000-04-09 21:04:55 +00:00
|
|
|
return (ENXIO);
|
|
|
|
}
|
2007-05-11 14:36:02 +00:00
|
|
|
if ((error = ng_mppc_decompress(node, &m)) != 0) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_ITEM(item);
|
2002-08-22 00:30:03 +00:00
|
|
|
if (error == EINVAL && priv->ctrlnode != 0) {
|
2000-04-09 21:04:55 +00:00
|
|
|
struct ng_mesg *msg;
|
|
|
|
|
|
|
|
/* Need to send a reset-request */
|
|
|
|
NG_MKMESSAGE(msg, NGM_MPPC_COOKIE,
|
|
|
|
NGM_MPPC_RESETREQ, 0, M_NOWAIT);
|
|
|
|
if (msg == NULL)
|
|
|
|
return (error);
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_SEND_MSG_ID(error, node, msg,
|
2002-08-22 00:30:03 +00:00
|
|
|
priv->ctrlnode, 0);
|
2000-04-09 21:04:55 +00:00
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
2007-05-11 14:36:02 +00:00
|
|
|
NG_FWD_NEW_DATA(error, item, priv->recv.hook, m);
|
2000-04-09 21:04:55 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Oops */
|
2001-12-10 08:09:49 +00:00
|
|
|
panic("%s: unknown hook", __func__);
|
2000-04-09 21:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy node
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_mppc_shutdown(node_p node)
|
2000-04-09 21:04:55 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
2000-04-09 21:04:55 +00:00
|
|
|
|
|
|
|
/* Take down netgraph node */
|
|
|
|
#ifdef NETGRAPH_MPPC_COMPRESSION
|
|
|
|
if (priv->xmit.history != NULL)
|
2008-10-23 15:53:51 +00:00
|
|
|
free(priv->xmit.history, M_NETGRAPH_MPPC);
|
2000-04-09 21:04:55 +00:00
|
|
|
if (priv->recv.history != NULL)
|
2008-10-23 15:53:51 +00:00
|
|
|
free(priv->recv.history, M_NETGRAPH_MPPC);
|
2000-04-09 21:04:55 +00:00
|
|
|
#endif
|
|
|
|
bzero(priv, sizeof(*priv));
|
2008-10-23 15:53:51 +00:00
|
|
|
free(priv, M_NETGRAPH_MPPC);
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, NULL);
|
|
|
|
NG_NODE_UNREF(node); /* let the node escape */
|
2000-04-09 21:04:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hook disconnection
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_mppc_disconnect(hook_p hook)
|
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const node_p node = NG_HOOK_NODE(hook);
|
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
2000-04-09 21:04:55 +00:00
|
|
|
|
|
|
|
/* Zero out hook pointer */
|
|
|
|
if (hook == priv->xmit.hook)
|
|
|
|
priv->xmit.hook = NULL;
|
|
|
|
if (hook == priv->recv.hook)
|
|
|
|
priv->recv.hook = NULL;
|
|
|
|
|
|
|
|
/* Go away if no longer connected */
|
2001-01-08 05:34:06 +00:00
|
|
|
if ((NG_NODE_NUMHOOKS(node) == 0)
|
|
|
|
&& NG_NODE_IS_VALID(node))
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_rmnode_self(node);
|
2000-04-09 21:04:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
HELPER STUFF
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compress/encrypt a packet and put the result in a new mbuf at *resultp.
|
|
|
|
* The original mbuf is not free'd.
|
|
|
|
*/
|
|
|
|
static int
|
2007-05-11 14:36:02 +00:00
|
|
|
ng_mppc_compress(node_p node, struct mbuf **datap)
|
2000-04-09 21:04:55 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
2000-04-09 21:04:55 +00:00
|
|
|
struct ng_mppc_dir *const d = &priv->xmit;
|
|
|
|
u_int16_t header;
|
2007-05-11 14:36:02 +00:00
|
|
|
struct mbuf *m = *datap;
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2009-01-18 19:25:36 +00:00
|
|
|
/* We must own the mbuf chain exclusively to modify it. */
|
|
|
|
m = m_unshare(m, M_DONTWAIT);
|
|
|
|
if (m == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
|
2000-04-09 21:04:55 +00:00
|
|
|
/* Initialize */
|
|
|
|
header = d->cc;
|
2007-05-04 16:12:54 +00:00
|
|
|
|
|
|
|
/* Always set the flushed bit in stateless mode */
|
|
|
|
if (d->flushed || ((d->cfg.bits & MPPE_STATELESS) != 0)) {
|
2000-04-09 21:04:55 +00:00
|
|
|
header |= MPPC_FLAG_FLUSHED;
|
|
|
|
d->flushed = 0;
|
|
|
|
}
|
|
|
|
|
2007-05-11 14:36:02 +00:00
|
|
|
/* Compress packet (if compression enabled) */
|
2000-04-09 21:04:55 +00:00
|
|
|
#ifdef NETGRAPH_MPPC_COMPRESSION
|
|
|
|
if ((d->cfg.bits & MPPC_BIT) != 0) {
|
|
|
|
u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS;
|
2007-05-11 14:36:02 +00:00
|
|
|
u_char *inbuf, *outbuf;
|
2009-01-18 21:09:34 +00:00
|
|
|
int outlen, inlen, ina;
|
2000-04-09 21:04:55 +00:00
|
|
|
u_char *source, *dest;
|
|
|
|
u_long sourceCnt, destCnt;
|
|
|
|
int rtn;
|
|
|
|
|
2007-05-11 14:36:02 +00:00
|
|
|
/* Work with contiguous regions of memory. */
|
|
|
|
inlen = m->m_pkthdr.len;
|
2009-01-18 21:09:34 +00:00
|
|
|
if (m->m_next == NULL) {
|
|
|
|
inbuf = mtod(m, u_char *);
|
|
|
|
ina = 0;
|
|
|
|
} else {
|
|
|
|
inbuf = malloc(inlen, M_NETGRAPH_MPPC, M_NOWAIT);
|
|
|
|
if (inbuf == NULL)
|
|
|
|
goto err1;
|
|
|
|
m_copydata(m, 0, inlen, (caddr_t)inbuf);
|
|
|
|
ina = 1;
|
|
|
|
}
|
2007-05-11 14:36:02 +00:00
|
|
|
|
|
|
|
outlen = MPPC_MAX_BLOWUP(inlen);
|
|
|
|
outbuf = malloc(outlen, M_NETGRAPH_MPPC, M_NOWAIT);
|
|
|
|
if (outbuf == NULL) {
|
2009-01-18 21:09:34 +00:00
|
|
|
if (ina)
|
|
|
|
free(inbuf, M_NETGRAPH_MPPC);
|
2008-12-06 23:00:48 +00:00
|
|
|
err1:
|
|
|
|
m_freem(m);
|
|
|
|
MPPC_InitCompressionHistory(d->history);
|
|
|
|
d->flushed = 1;
|
2007-05-11 14:36:02 +00:00
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
|
2000-04-09 21:04:55 +00:00
|
|
|
/* Prepare to compress */
|
|
|
|
source = inbuf;
|
|
|
|
sourceCnt = inlen;
|
2007-05-11 14:36:02 +00:00
|
|
|
dest = outbuf;
|
|
|
|
destCnt = outlen;
|
2000-04-09 21:04:55 +00:00
|
|
|
if ((d->cfg.bits & MPPE_STATELESS) == 0)
|
|
|
|
flags |= MPPC_SAVE_HISTORY;
|
|
|
|
|
|
|
|
/* Compress */
|
|
|
|
rtn = MPPC_Compress(&source, &dest, &sourceCnt,
|
|
|
|
&destCnt, d->history, flags, 0);
|
|
|
|
|
|
|
|
/* Check return value */
|
2001-12-10 08:09:49 +00:00
|
|
|
KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
|
2000-04-09 21:04:55 +00:00
|
|
|
if ((rtn & MPPC_EXPANDED) == 0
|
|
|
|
&& (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) {
|
|
|
|
outlen -= destCnt;
|
|
|
|
header |= MPPC_FLAG_COMPRESSED;
|
|
|
|
if ((rtn & MPPC_RESTART_HISTORY) != 0)
|
|
|
|
header |= MPPC_FLAG_RESTART;
|
2007-05-11 14:36:02 +00:00
|
|
|
|
|
|
|
/* Replace m by the compresed one. */
|
2009-01-18 19:25:36 +00:00
|
|
|
m_copyback(m, 0, outlen, (caddr_t)outbuf);
|
|
|
|
if (m->m_pkthdr.len < outlen) {
|
|
|
|
m_freem(m);
|
|
|
|
m = NULL;
|
|
|
|
} else if (outlen < m->m_pkthdr.len)
|
|
|
|
m_adj(m, outlen - m->m_pkthdr.len);
|
2000-04-09 21:04:55 +00:00
|
|
|
}
|
|
|
|
d->flushed = (rtn & MPPC_EXPANDED) != 0
|
|
|
|
|| (flags & MPPC_SAVE_HISTORY) == 0;
|
|
|
|
|
2009-01-18 21:09:34 +00:00
|
|
|
if (ina)
|
|
|
|
free(inbuf, M_NETGRAPH_MPPC);
|
2007-05-11 14:36:02 +00:00
|
|
|
free(outbuf, M_NETGRAPH_MPPC);
|
|
|
|
|
2009-01-18 19:25:36 +00:00
|
|
|
/* Check mbuf chain reload result. */
|
2008-12-06 23:00:48 +00:00
|
|
|
if (m == NULL) {
|
|
|
|
if (!d->flushed) {
|
|
|
|
MPPC_InitCompressionHistory(d->history);
|
|
|
|
d->flushed = 1;
|
|
|
|
}
|
2007-05-11 14:36:02 +00:00
|
|
|
return (ENOMEM);
|
2008-12-06 23:00:48 +00:00
|
|
|
}
|
2000-04-09 21:04:55 +00:00
|
|
|
}
|
2007-05-11 14:36:02 +00:00
|
|
|
#endif
|
2000-04-09 21:04:55 +00:00
|
|
|
|
|
|
|
/* Now encrypt packet (if encryption enabled) */
|
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
if ((d->cfg.bits & MPPE_BITS) != 0) {
|
2007-05-11 14:36:02 +00:00
|
|
|
struct mbuf *m1;
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2007-05-04 16:20:47 +00:00
|
|
|
/* Set header bits */
|
2000-04-09 21:04:55 +00:00
|
|
|
header |= MPPC_FLAG_ENCRYPTED;
|
|
|
|
|
|
|
|
/* Update key if it's time */
|
|
|
|
if ((d->cfg.bits & MPPE_STATELESS) != 0
|
|
|
|
|| (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
|
2007-05-04 16:20:47 +00:00
|
|
|
ng_mppc_updatekey(d->cfg.bits,
|
|
|
|
d->cfg.startkey, d->key, &d->rc4);
|
|
|
|
} else if ((header & MPPC_FLAG_FLUSHED) != 0) {
|
|
|
|
/* Need to reset key if we say we did
|
|
|
|
and ng_mppc_updatekey wasn't called to do it also. */
|
|
|
|
rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
|
2000-04-09 21:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Encrypt packet */
|
2007-05-11 14:36:02 +00:00
|
|
|
m1 = m;
|
|
|
|
while (m1) {
|
|
|
|
rc4_crypt(&d->rc4, mtod(m1, u_char *),
|
|
|
|
mtod(m1, u_char *), m1->m_len);
|
|
|
|
m1 = m1->m_next;
|
|
|
|
}
|
2000-04-09 21:04:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-05-04 16:05:58 +00:00
|
|
|
/* Update coherency count for next time (12 bit arithmetic) */
|
|
|
|
MPPC_CCOUNT_INC(d->cc);
|
2000-04-09 21:04:55 +00:00
|
|
|
|
|
|
|
/* Install header */
|
2007-05-11 14:36:02 +00:00
|
|
|
M_PREPEND(m, MPPC_HDRLEN, M_DONTWAIT);
|
|
|
|
if (m != NULL)
|
2010-03-31 22:47:55 +00:00
|
|
|
be16enc(mtod(m, void *), header);
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2007-05-11 14:36:02 +00:00
|
|
|
*datap = m;
|
|
|
|
return (*datap == NULL ? ENOBUFS : 0);
|
2000-04-09 21:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decompress/decrypt packet and put the result in a new mbuf at *resultp.
|
|
|
|
* The original mbuf is not free'd.
|
|
|
|
*/
|
|
|
|
static int
|
2007-05-11 14:36:02 +00:00
|
|
|
ng_mppc_decompress(node_p node, struct mbuf **datap)
|
2000-04-09 21:04:55 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
2000-04-09 21:04:55 +00:00
|
|
|
struct ng_mppc_dir *const d = &priv->recv;
|
2002-12-14 00:56:07 +00:00
|
|
|
u_int16_t header, cc;
|
|
|
|
u_int numLost;
|
2007-05-11 14:36:02 +00:00
|
|
|
struct mbuf *m = *datap;
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2009-01-18 19:25:36 +00:00
|
|
|
/* We must own the mbuf chain exclusively to modify it. */
|
|
|
|
m = m_unshare(m, M_DONTWAIT);
|
|
|
|
if (m == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
|
2000-04-09 21:04:55 +00:00
|
|
|
/* Pull off header */
|
2007-05-11 14:36:02 +00:00
|
|
|
if (m->m_pkthdr.len < MPPC_HDRLEN) {
|
|
|
|
m_freem(m);
|
2000-04-09 21:04:55 +00:00
|
|
|
return (EINVAL);
|
2007-05-11 14:36:02 +00:00
|
|
|
}
|
2010-03-31 22:47:55 +00:00
|
|
|
header = be16dec(mtod(m, void *));
|
2000-04-09 21:04:55 +00:00
|
|
|
cc = (header & MPPC_CCOUNT_MASK);
|
2007-05-11 14:36:02 +00:00
|
|
|
m_adj(m, MPPC_HDRLEN);
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2002-12-14 00:56:07 +00:00
|
|
|
/* Check for an unexpected jump in the sequence number */
|
2000-04-09 21:04:55 +00:00
|
|
|
numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK);
|
|
|
|
|
|
|
|
/* If flushed bit set, we can always handle packet */
|
|
|
|
if ((header & MPPC_FLAG_FLUSHED) != 0) {
|
|
|
|
#ifdef NETGRAPH_MPPC_COMPRESSION
|
|
|
|
if (d->history != NULL)
|
|
|
|
MPPC_InitDecompressionHistory(d->history);
|
|
|
|
#endif
|
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
if ((d->cfg.bits & MPPE_BITS) != 0) {
|
2002-12-14 00:56:07 +00:00
|
|
|
u_int rekey;
|
|
|
|
|
|
|
|
/* How many times are we going to have to re-key? */
|
|
|
|
rekey = ((d->cfg.bits & MPPE_STATELESS) != 0) ?
|
|
|
|
numLost : (numLost / (MPPE_UPDATE_MASK + 1));
|
|
|
|
if (rekey > MPPE_MAX_REKEY) {
|
|
|
|
log(LOG_ERR, "%s: too many (%d) packets"
|
|
|
|
" dropped, disabling node %p!",
|
|
|
|
__func__, numLost, node);
|
|
|
|
priv->recv.cfg.enable = 0;
|
|
|
|
goto failed;
|
|
|
|
}
|
2000-04-09 21:04:55 +00:00
|
|
|
|
2002-12-14 00:56:07 +00:00
|
|
|
/* Re-key as necessary to catch up to peer */
|
2000-04-09 21:04:55 +00:00
|
|
|
while (d->cc != cc) {
|
2002-12-14 00:56:07 +00:00
|
|
|
if ((d->cfg.bits & MPPE_STATELESS) != 0
|
2000-04-09 21:04:55 +00:00
|
|
|
|| (d->cc & MPPE_UPDATE_MASK)
|
|
|
|
== MPPE_UPDATE_FLAG) {
|
|
|
|
ng_mppc_updatekey(d->cfg.bits,
|
|
|
|
d->cfg.startkey, d->key, &d->rc4);
|
|
|
|
}
|
2007-05-04 16:05:58 +00:00
|
|
|
MPPC_CCOUNT_INC(d->cc);
|
2000-04-09 21:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset key (except in stateless mode, see below) */
|
|
|
|
if ((d->cfg.bits & MPPE_STATELESS) == 0)
|
|
|
|
rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
d->cc = cc; /* skip over lost seq numbers */
|
|
|
|
numLost = 0; /* act like no packets were lost */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can't decode non-sequential packets without a flushed bit */
|
|
|
|
if (numLost != 0)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
/* Decrypt packet */
|
|
|
|
if ((header & MPPC_FLAG_ENCRYPTED) != 0) {
|
2007-05-11 14:36:02 +00:00
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
struct mbuf *m1;
|
|
|
|
#endif
|
2000-04-09 21:04:55 +00:00
|
|
|
|
|
|
|
/* Are we not expecting encryption? */
|
|
|
|
if ((d->cfg.bits & MPPE_BITS) == 0) {
|
|
|
|
log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
|
2001-12-10 08:09:49 +00:00
|
|
|
__func__, "encrypted");
|
2000-04-09 21:04:55 +00:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
/* Update key if it's time (always in stateless mode) */
|
|
|
|
if ((d->cfg.bits & MPPE_STATELESS) != 0
|
|
|
|
|| (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
|
|
|
|
ng_mppc_updatekey(d->cfg.bits,
|
|
|
|
d->cfg.startkey, d->key, &d->rc4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrypt packet */
|
2007-05-11 14:36:02 +00:00
|
|
|
m1 = m;
|
|
|
|
while (m1 != NULL) {
|
|
|
|
rc4_crypt(&d->rc4, mtod(m1, u_char *),
|
|
|
|
mtod(m1, u_char *), m1->m_len);
|
|
|
|
m1 = m1->m_next;
|
|
|
|
}
|
2000-04-09 21:04:55 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Are we expecting encryption? */
|
|
|
|
if ((d->cfg.bits & MPPE_BITS) != 0) {
|
|
|
|
log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
|
2001-12-10 08:09:49 +00:00
|
|
|
__func__, "unencrypted");
|
2000-04-09 21:04:55 +00:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update coherency count for next time (12 bit arithmetic) */
|
2007-05-04 16:05:58 +00:00
|
|
|
MPPC_CCOUNT_INC(d->cc);
|
2000-04-09 21:04:55 +00:00
|
|
|
|
|
|
|
/* Check for unexpected compressed packet */
|
|
|
|
if ((header & MPPC_FLAG_COMPRESSED) != 0
|
|
|
|
&& (d->cfg.bits & MPPC_BIT) == 0) {
|
|
|
|
log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
|
2001-12-10 08:09:49 +00:00
|
|
|
__func__, "compressed");
|
2000-04-09 21:04:55 +00:00
|
|
|
failed:
|
2007-05-11 14:36:02 +00:00
|
|
|
m_freem(m);
|
2000-04-09 21:04:55 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NETGRAPH_MPPC_COMPRESSION
|
|
|
|
/* Decompress packet */
|
|
|
|
if ((header & MPPC_FLAG_COMPRESSED) != 0) {
|
|
|
|
int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS;
|
2009-01-18 21:09:34 +00:00
|
|
|
u_char *inbuf, *outbuf;
|
|
|
|
int inlen, outlen, ina;
|
|
|
|
u_char *source, *dest;
|
2000-04-09 21:04:55 +00:00
|
|
|
u_long sourceCnt, destCnt;
|
2009-01-18 21:09:34 +00:00
|
|
|
int rtn;
|
2007-05-11 14:36:02 +00:00
|
|
|
|
|
|
|
/* Copy payload into a contiguous region of memory. */
|
2009-01-18 21:09:34 +00:00
|
|
|
inlen = m->m_pkthdr.len;
|
|
|
|
if (m->m_next == NULL) {
|
|
|
|
inbuf = mtod(m, u_char *);
|
|
|
|
ina = 0;
|
|
|
|
} else {
|
|
|
|
inbuf = malloc(inlen, M_NETGRAPH_MPPC, M_NOWAIT);
|
|
|
|
if (inbuf == NULL) {
|
|
|
|
m_freem(m);
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
m_copydata(m, 0, inlen, (caddr_t)inbuf);
|
|
|
|
ina = 1;
|
2007-05-11 14:36:02 +00:00
|
|
|
}
|
2000-04-09 21:04:55 +00:00
|
|
|
|
|
|
|
/* Allocate a buffer for decompressed data */
|
2009-01-18 21:09:34 +00:00
|
|
|
outbuf = malloc(MPPC_DECOMP_BUFSIZE + MPPC_DECOMP_SAFETY,
|
2007-05-11 14:36:02 +00:00
|
|
|
M_NETGRAPH_MPPC, M_NOWAIT);
|
2009-01-18 21:09:34 +00:00
|
|
|
if (outbuf == NULL) {
|
2007-05-11 14:36:02 +00:00
|
|
|
m_freem(m);
|
2009-01-18 21:09:34 +00:00
|
|
|
if (ina)
|
|
|
|
free(inbuf, M_NETGRAPH_MPPC);
|
2000-04-09 21:04:55 +00:00
|
|
|
return (ENOMEM);
|
|
|
|
}
|
2009-01-18 21:09:34 +00:00
|
|
|
outlen = MPPC_DECOMP_BUFSIZE;
|
2000-04-09 21:04:55 +00:00
|
|
|
|
|
|
|
/* Prepare to decompress */
|
2009-01-18 21:09:34 +00:00
|
|
|
source = inbuf;
|
|
|
|
sourceCnt = inlen;
|
|
|
|
dest = outbuf;
|
|
|
|
destCnt = outlen;
|
2000-04-09 21:04:55 +00:00
|
|
|
if ((header & MPPC_FLAG_RESTART) != 0)
|
|
|
|
flags |= MPPC_RESTART_HISTORY;
|
|
|
|
|
|
|
|
/* Decompress */
|
|
|
|
rtn = MPPC_Decompress(&source, &dest,
|
|
|
|
&sourceCnt, &destCnt, d->history, flags);
|
|
|
|
|
|
|
|
/* Check return value */
|
2001-12-10 08:09:49 +00:00
|
|
|
KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
|
2000-04-09 21:04:55 +00:00
|
|
|
if ((rtn & MPPC_DEST_EXHAUSTED) != 0
|
|
|
|
|| (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) {
|
|
|
|
log(LOG_ERR, "%s: decomp returned 0x%x",
|
2001-12-10 08:09:49 +00:00
|
|
|
__func__, rtn);
|
2009-01-18 21:09:34 +00:00
|
|
|
if (ina)
|
|
|
|
free(inbuf, M_NETGRAPH_MPPC);
|
|
|
|
free(outbuf, M_NETGRAPH_MPPC);
|
2000-04-09 21:04:55 +00:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace compressed data with decompressed data */
|
2009-01-18 21:09:34 +00:00
|
|
|
if (ina)
|
|
|
|
free(inbuf, M_NETGRAPH_MPPC);
|
|
|
|
outlen -= destCnt;
|
2007-05-11 14:36:02 +00:00
|
|
|
|
2009-01-18 21:09:34 +00:00
|
|
|
m_copyback(m, 0, outlen, (caddr_t)outbuf);
|
|
|
|
if (m->m_pkthdr.len < outlen) {
|
2009-01-18 19:25:36 +00:00
|
|
|
m_freem(m);
|
|
|
|
m = NULL;
|
2009-01-18 21:09:34 +00:00
|
|
|
} else if (outlen < m->m_pkthdr.len)
|
|
|
|
m_adj(m, outlen - m->m_pkthdr.len);
|
|
|
|
free(outbuf, M_NETGRAPH_MPPC);
|
2000-04-09 21:04:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Return result in an mbuf */
|
2007-05-11 14:36:02 +00:00
|
|
|
*datap = m;
|
|
|
|
return (*datap == NULL ? ENOBUFS : 0);
|
2000-04-09 21:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The peer has sent us a CCP ResetRequest, so reset our transmit state.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ng_mppc_reset_req(node_p node)
|
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
2000-04-09 21:04:55 +00:00
|
|
|
struct ng_mppc_dir *const d = &priv->xmit;
|
|
|
|
|
|
|
|
#ifdef NETGRAPH_MPPC_COMPRESSION
|
|
|
|
if (d->history != NULL)
|
|
|
|
MPPC_InitCompressionHistory(d->history);
|
|
|
|
#endif
|
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
|
|
|
if ((d->cfg.bits & MPPE_STATELESS) == 0)
|
|
|
|
rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
|
|
|
|
#endif
|
|
|
|
d->flushed = 1;
|
|
|
|
}
|
|
|
|
|
2007-05-18 15:28:01 +00:00
|
|
|
#ifdef NETGRAPH_MPPC_ENCRYPTION
|
2000-04-09 21:04:55 +00:00
|
|
|
/*
|
|
|
|
* Generate a new encryption key
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ng_mppc_getkey(const u_char *h, u_char *h2, int len)
|
|
|
|
{
|
2008-12-16 19:15:31 +00:00
|
|
|
static const u_char pad1[40] =
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
static const u_char pad2[40] =
|
|
|
|
{ 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
|
|
|
|
0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
|
|
|
|
0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
|
|
|
|
0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2 };
|
2000-04-09 21:04:55 +00:00
|
|
|
u_char hash[20];
|
|
|
|
SHA1_CTX c;
|
|
|
|
|
|
|
|
SHA1Init(&c);
|
|
|
|
SHA1Update(&c, h, len);
|
2008-12-16 19:15:31 +00:00
|
|
|
SHA1Update(&c, pad1, sizeof(pad1));
|
2000-04-09 21:04:55 +00:00
|
|
|
SHA1Update(&c, h2, len);
|
2008-12-16 19:15:31 +00:00
|
|
|
SHA1Update(&c, pad2, sizeof(pad2));
|
2000-04-09 21:04:55 +00:00
|
|
|
SHA1Final(hash, &c);
|
|
|
|
bcopy(hash, h2, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the encryption key
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ng_mppc_updatekey(u_int32_t bits,
|
|
|
|
u_char *key0, u_char *key, struct rc4_state *rc4)
|
|
|
|
{
|
|
|
|
const int keylen = KEYLEN(bits);
|
|
|
|
|
|
|
|
ng_mppc_getkey(key0, key, keylen);
|
|
|
|
rc4_init(rc4, key, keylen);
|
|
|
|
rc4_crypt(rc4, key, key, keylen);
|
2001-12-15 02:07:32 +00:00
|
|
|
if ((bits & MPPE_40) != 0)
|
|
|
|
bcopy(&ng_mppe_weakenkey, key, 3);
|
|
|
|
else if ((bits & MPPE_56) != 0)
|
|
|
|
bcopy(&ng_mppe_weakenkey, key, 1);
|
2000-04-09 21:04:55 +00:00
|
|
|
rc4_init(rc4, key, keylen);
|
|
|
|
}
|
2007-05-18 15:28:01 +00:00
|
|
|
#endif
|
2000-04-09 21:04:55 +00:00
|
|
|
|