#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include "ng_mppc.h"
#include "opt_netgraph.h"
#if !defined(NETGRAPH_MPPC_COMPRESSION) && !defined(NETGRAPH_MPPC_ENCRYPTION)
#error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION
#endif
#ifdef NETGRAPH_MPPC_COMPRESSION
#include <net/mppc.h>
#endif
#ifdef NETGRAPH_MPPC_ENCRYPTION
#include <crypto/rc4/rc4.h>
#endif
#include <crypto/sha1.h>
#define MPPC_DECOMP_BUFSIZE 8092
#define MPPC_DECOMP_SAFETY 100
#define MPPC_HDRLEN 2
#define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8)
#define MPPE_MAX_REKEY 1000
#define MPPC_FLAG_FLUSHED 0x8000
#define MPPC_FLAG_RESTART 0x4000
#define MPPC_FLAG_COMPRESSED 0x2000
#define MPPC_FLAG_ENCRYPTED 0x1000
#define MPPC_CCOUNT_MASK 0x0fff
#define MPPE_UPDATE_MASK 0xff
#define MPPE_UPDATE_FLAG 0xff
#define MPPC_COMP_OK 0x05
#define MPPC_DECOMP_OK 0x05
struct ng_mppc_dir {
struct ng_mppc_config cfg;
hook_p hook;
u_int16_t cc:12;
u_char flushed;
#ifdef NETGRAPH_MPPC_COMPRESSION
u_char *history;
#endif
#ifdef NETGRAPH_MPPC_ENCRYPTION
u_char key[MPPE_KEY_LEN];
struct rc4_state rc4;
#endif
};
struct ng_mppc_private {
struct ng_mppc_dir xmit;
struct ng_mppc_dir recv;
char *ctrlpath;
};
typedef struct ng_mppc_private *priv_p;
static ng_constructor_t ng_mppc_constructor;
static ng_rcvmsg_t ng_mppc_rcvmsg;
static ng_shutdown_t ng_mppc_rmnode;
static ng_newhook_t ng_mppc_newhook;
static ng_rcvdata_t ng_mppc_rcvdata;
static ng_disconnect_t ng_mppc_disconnect;
static int ng_mppc_compress(node_p node,
struct mbuf *m, struct mbuf **resultp);
static int ng_mppc_decompress(node_p node,
struct mbuf *m, struct mbuf **resultp);
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);
static void ng_mppc_reset_req(node_p node);
static struct ng_type ng_mppc_typestruct = {
NG_VERSION,
NG_MPPC_NODE_TYPE,
NULL,
ng_mppc_constructor,
ng_mppc_rcvmsg,
ng_mppc_rmnode,
ng_mppc_newhook,
NULL,
NULL,
ng_mppc_rcvdata,
ng_mppc_rcvdata,
ng_mppc_disconnect,
NULL
};
NETGRAPH_INIT(mppc, &ng_mppc_typestruct);
static const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e };
#define ERROUT(x) do { error = (x); goto done; } while (0)
static int
ng_mppc_constructor(node_p *nodep)
{
priv_p priv;
int error;
priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
if (priv == NULL)
return (ENOMEM);
if ((error = ng_make_node_common(&ng_mppc_typestruct, nodep))) {
kfree(priv, M_NETGRAPH);
return (error);
}
(*nodep)->private = priv;
return (0);
}
static int
ng_mppc_newhook(node_p node, hook_p hook, const char *name)
{
const priv_p priv = node->private;
hook_p *hookPtr;
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);
if (*hookPtr != NULL)
return (EISCONN);
*hookPtr = hook;
return (0);
}
static int
ng_mppc_rcvmsg(node_p node, struct ng_mesg *msg,
const char *raddr, struct ng_mesg **rptr)
{
const priv_p priv = node->private;
struct ng_mesg *resp = NULL;
int error = 0;
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;
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;
if (!isComp && priv->ctrlpath != NULL) {
kfree(priv->ctrlpath, M_NETGRAPH);
priv->ctrlpath = NULL;
}
if (!isComp && raddr != NULL) {
priv->ctrlpath = kmalloc(strlen(raddr) + 1,
M_NETGRAPH, M_NOWAIT);
if (priv->ctrlpath == NULL)
ERROUT(ENOMEM);
strcpy(priv->ctrlpath, raddr);
}
d->cfg = *cfg;
#ifdef NETGRAPH_MPPC_COMPRESSION
if (d->history != NULL) {
kfree(d->history, M_NETGRAPH);
d->history = NULL;
}
if ((cfg->bits & MPPC_BIT) != 0) {
d->history = kmalloc(isComp ? MPPC_SizeOfCompressionHistory() : MPPC_SizeOfDecompressionHistory(),
M_NETGRAPH, M_NOWAIT);
if (d->history == NULL)
ERROUT(ENOMEM);
if (isComp)
MPPC_InitCompressionHistory(d->history);
else {
MPPC_InitDecompressionHistory(
d->history);
}
}
#endif
#ifdef NETGRAPH_MPPC_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);
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);
rc4_init(&d->rc4, d->key, keylen);
}
#endif
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;
}
if (rptr)
*rptr = resp;
else if (resp)
kfree(resp, M_NETGRAPH);
done:
kfree(msg, M_NETGRAPH);
return (error);
}
static int
ng_mppc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
{
const node_p node = hook->node;
const priv_p priv = node->private;
struct mbuf *out;
int error;
if (hook == priv->xmit.hook) {
if (!priv->xmit.cfg.enable) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
if ((error = ng_mppc_compress(node, m, &out)) != 0) {
NG_FREE_DATA(m, meta);
return(error);
}
m_freem(m);
NG_SEND_DATA(error, priv->xmit.hook, out, meta);
return (error);
}
if (hook == priv->recv.hook) {
if (!priv->recv.cfg.enable) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
if ((error = ng_mppc_decompress(node, m, &out)) != 0) {
NG_FREE_DATA(m, meta);
if (error == EINVAL && priv->ctrlpath != NULL) {
struct ng_mesg *msg;
NG_MKMESSAGE(msg, NGM_MPPC_COOKIE,
NGM_MPPC_RESETREQ, 0, M_NOWAIT);
if (msg == NULL)
return (error);
ng_send_msg(node, msg, priv->ctrlpath, NULL);
}
return (error);
}
m_freem(m);
NG_SEND_DATA(error, priv->recv.hook, out, meta);
return (error);
}
panic("%s: unknown hook", __func__);
}
static int
ng_mppc_rmnode(node_p node)
{
const priv_p priv = node->private;
node->flags |= NG_INVALID;
ng_cutlinks(node);
ng_unname(node);
if (priv->ctrlpath != NULL)
kfree(priv->ctrlpath, M_NETGRAPH);
#ifdef NETGRAPH_MPPC_COMPRESSION
if (priv->xmit.history != NULL)
kfree(priv->xmit.history, M_NETGRAPH);
if (priv->recv.history != NULL)
kfree(priv->recv.history, M_NETGRAPH);
#endif
bzero(priv, sizeof(*priv));
kfree(priv, M_NETGRAPH);
node->private = NULL;
ng_unref(node);
return (0);
}
static int
ng_mppc_disconnect(hook_p hook)
{
const node_p node = hook->node;
const priv_p priv = node->private;
if (hook == priv->xmit.hook)
priv->xmit.hook = NULL;
if (hook == priv->recv.hook)
priv->recv.hook = NULL;
if (node->numhooks == 0)
ng_rmnode(node);
return (0);
}
static int
ng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
{
const priv_p priv = node->private;
struct ng_mppc_dir *const d = &priv->xmit;
u_char *inbuf, *outbuf;
int outlen, inlen;
u_int16_t header;
*resultp = NULL;
header = d->cc;
if (d->flushed) {
header |= MPPC_FLAG_FLUSHED;
d->flushed = 0;
}
inlen = m->m_pkthdr.len;
inbuf = kmalloc(inlen, M_NETGRAPH, M_NOWAIT);
if (inbuf == NULL)
return (ENOMEM);
m_copydata(m, 0, inlen, inbuf);
if ((d->cfg.bits & MPPC_BIT) != 0)
outlen = MPPC_MAX_BLOWUP(inlen);
else
outlen = MPPC_HDRLEN + inlen;
outbuf = kmalloc(outlen, M_NETGRAPH, M_NOWAIT);
if (outbuf == NULL) {
kfree(inbuf, M_NETGRAPH);
return (ENOMEM);
}
#ifdef NETGRAPH_MPPC_COMPRESSION
if ((d->cfg.bits & MPPC_BIT) != 0) {
u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS;
u_char *source, *dest;
u_long sourceCnt, destCnt;
int rtn;
source = inbuf;
sourceCnt = inlen;
dest = outbuf + MPPC_HDRLEN;
destCnt = outlen - MPPC_HDRLEN;
if ((d->cfg.bits & MPPE_STATELESS) == 0)
flags |= MPPC_SAVE_HISTORY;
rtn = MPPC_Compress(&source, &dest, &sourceCnt,
&destCnt, d->history, flags, 0);
KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
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;
}
d->flushed = (rtn & MPPC_EXPANDED) != 0
|| (flags & MPPC_SAVE_HISTORY) == 0;
}
#endif
if ((header & MPPC_FLAG_COMPRESSED) == 0) {
bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen);
outlen = MPPC_HDRLEN + inlen;
}
kfree(inbuf, M_NETGRAPH);
if ((d->cfg.bits & MPPE_STATELESS) != 0)
header |= MPPC_FLAG_FLUSHED;
#ifdef NETGRAPH_MPPC_ENCRYPTION
if ((d->cfg.bits & MPPE_BITS) != 0) {
header |= MPPC_FLAG_ENCRYPTED;
if ((header & MPPC_FLAG_FLUSHED) != 0)
rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
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);
}
rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN,
outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN);
}
#endif
d->cc++;
*((u_int16_t *)outbuf) = htons(header);
*resultp = m_devget(outbuf, outlen, 0, NULL);
kfree(outbuf, M_NETGRAPH);
return (*resultp == NULL ? ENOBUFS : 0);
}
static int
ng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
{
const priv_p priv = node->private;
struct ng_mppc_dir *const d = &priv->recv;
u_int16_t header, cc;
u_int numLost;
u_char *buf;
int len;
if (m->m_pkthdr.len < MPPC_HDRLEN)
return (EINVAL);
m_copydata(m, 0, MPPC_HDRLEN, &header);
header = ntohs(header);
cc = (header & MPPC_CCOUNT_MASK);
len = m->m_pkthdr.len - MPPC_HDRLEN;
buf = kmalloc(len, M_NETGRAPH, M_NOWAIT);
if (buf == NULL)
return (ENOMEM);
m_copydata(m, MPPC_HDRLEN, len, buf);
numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK);
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) {
u_int rekey;
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;
}
while (d->cc != cc) {
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);
}
d->cc++;
}
if ((d->cfg.bits & MPPE_STATELESS) == 0)
rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
}
#endif
d->cc = cc;
numLost = 0;
}
if (numLost != 0)
goto failed;
if ((header & MPPC_FLAG_ENCRYPTED) != 0) {
if ((d->cfg.bits & MPPE_BITS) == 0) {
log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
__func__, "encrypted");
goto failed;
}
#ifdef NETGRAPH_MPPC_ENCRYPTION
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);
}
rc4_crypt(&d->rc4, buf, buf, len);
#endif
} else {
if ((d->cfg.bits & MPPE_BITS) != 0) {
log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
__func__, "unencrypted");
goto failed;
}
}
d->cc++;
if ((header & MPPC_FLAG_COMPRESSED) != 0
&& (d->cfg.bits & MPPC_BIT) == 0) {
log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
__func__, "compressed");
failed:
kfree(buf, M_NETGRAPH);
return (EINVAL);
}
#ifdef NETGRAPH_MPPC_COMPRESSION
if ((header & MPPC_FLAG_COMPRESSED) != 0) {
int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS;
u_char *decompbuf, *source, *dest;
u_long sourceCnt, destCnt;
int decomplen, rtn;
decompbuf = kmalloc(MPPC_DECOMP_BUFSIZE + MPPC_DECOMP_SAFETY,
M_NETGRAPH, M_NOWAIT);
if (decompbuf == NULL) {
kfree(buf, M_NETGRAPH);
return (ENOMEM);
}
decomplen = MPPC_DECOMP_BUFSIZE;
source = buf;
sourceCnt = len;
dest = decompbuf;
destCnt = decomplen;
if ((header & MPPC_FLAG_RESTART) != 0)
flags |= MPPC_RESTART_HISTORY;
rtn = MPPC_Decompress(&source, &dest,
&sourceCnt, &destCnt, d->history, flags);
KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
if ((rtn & MPPC_DEST_EXHAUSTED) != 0
|| (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) {
log(LOG_ERR, "%s: decomp returned 0x%x",
__func__, rtn);
kfree(decompbuf, M_NETGRAPH);
goto failed;
}
kfree(buf, M_NETGRAPH);
buf = decompbuf;
len = decomplen - destCnt;
}
#endif
*resultp = m_devget(buf, len, 0, NULL);
kfree(buf, M_NETGRAPH);
return (*resultp == NULL ? ENOBUFS : 0);
}
static void
ng_mppc_reset_req(node_p node)
{
const priv_p priv = node->private;
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;
}
static void
ng_mppc_getkey(const u_char *h, u_char *h2, int len)
{
static const u_char pad1[10] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
static const u_char pad2[10] =
{ 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, };
u_char hash[20];
SHA1_CTX c;
int k;
bzero(&hash, sizeof(hash));
SHA1Init(&c);
SHA1Update(&c, h, len);
for (k = 0; k < 4; k++)
SHA1Update(&c, pad1, sizeof(pad2));
SHA1Update(&c, h2, len);
for (k = 0; k < 4; k++)
SHA1Update(&c, pad2, sizeof(pad2));
SHA1Final(hash, &c);
bcopy(hash, h2, len);
}
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);
if ((bits & MPPE_40) != 0)
bcopy(&ng_mppe_weakenkey, key, 3);
else if ((bits & MPPE_56) != 0)
bcopy(&ng_mppe_weakenkey, key, 1);
rc4_init(rc4, key, keylen);
}