ng_mppc.c revision 67506
159109Sarchie 259109Sarchie/* 359109Sarchie * ng_mppc.c 459109Sarchie * 559109Sarchie * Copyright (c) 1996-2000 Whistle Communications, Inc. 659109Sarchie * All rights reserved. 759109Sarchie * 859109Sarchie * Subject to the following obligations and disclaimer of warranty, use and 959109Sarchie * redistribution of this software, in source or object code forms, with or 1059109Sarchie * without modifications are expressly permitted by Whistle Communications; 1159109Sarchie * provided, however, that: 1259109Sarchie * 1. Any and all reproductions of the source or object code must include the 1359109Sarchie * copyright notice above and the following disclaimer of warranties; and 1459109Sarchie * 2. No rights are granted, in any manner or form, to use Whistle 1559109Sarchie * Communications, Inc. trademarks, including the mark "WHISTLE 1659109Sarchie * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1759109Sarchie * such appears in the above copyright notice or in the software. 1859109Sarchie * 1959109Sarchie * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 2059109Sarchie * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2159109Sarchie * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2259109Sarchie * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2359109Sarchie * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2459109Sarchie * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2559109Sarchie * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2659109Sarchie * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2759109Sarchie * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 2859109Sarchie * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 2959109Sarchie * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 3059109Sarchie * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3159109Sarchie * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 3259109Sarchie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3359109Sarchie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3459109Sarchie * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3559109Sarchie * OF SUCH DAMAGE. 3659109Sarchie * 3767506Sjulian * Author: Archie Cobbs <archie@freebsd.org> 3859109Sarchie * 3959109Sarchie * $Whistle: ng_mppc.c,v 1.4 1999/11/25 00:10:12 archie Exp $ 4059109Sarchie * $FreeBSD: head/sys/netgraph/ng_mppc.c 67506 2000-10-24 17:32:45Z julian $ 4159109Sarchie */ 4259109Sarchie 4359109Sarchie/* 4459109Sarchie * Microsoft PPP compression (MPPC) and encryption (MPPE) netgraph node type. 4559109Sarchie * 4659109Sarchie * You must define one or both of the NETGRAPH_MPPC_COMPRESSION and/or 4759109Sarchie * NETGRAPH_MPPC_ENCRYPTION options for this node type to be useful. 4859109Sarchie */ 4959109Sarchie 5059109Sarchie#include <sys/param.h> 5159109Sarchie#include <sys/systm.h> 5259109Sarchie#include <sys/kernel.h> 5359109Sarchie#include <sys/mbuf.h> 5459109Sarchie#include <sys/malloc.h> 5559109Sarchie#include <sys/errno.h> 5659109Sarchie#include <sys/syslog.h> 5759109Sarchie 5859109Sarchie#include <netgraph/ng_message.h> 5959109Sarchie#include <netgraph/netgraph.h> 6059109Sarchie#include <netgraph/ng_mppc.h> 6159109Sarchie 6259109Sarchie#include "opt_netgraph.h" 6359109Sarchie 6459109Sarchie#if !defined(NETGRAPH_MPPC_COMPRESSION) && !defined(NETGRAPH_MPPC_ENCRYPTION) 6559109Sarchie#error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION 6659109Sarchie#endif 6759109Sarchie 6859109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 6959109Sarchie/* XXX this file doesn't exist yet, but hopefully someday it will... */ 7059109Sarchie#include <net/mppc.h> 7159109Sarchie#endif 7259109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 7359109Sarchie#include <crypto/rc4/rc4.h> 7459109Sarchie#endif 7559109Sarchie#include <crypto/sha1.h> 7659109Sarchie 7759109Sarchie/* Decompression blowup */ 7859109Sarchie#define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */ 7959109Sarchie#define MPPC_DECOMP_SAFETY 100 /* plus this much margin */ 8059109Sarchie 8159109Sarchie/* MPPC/MPPE header length */ 8259109Sarchie#define MPPC_HDRLEN 2 8359109Sarchie 8459109Sarchie/* Key length */ 8559109Sarchie#define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8) 8659109Sarchie 8759109Sarchie/* What sequence number jump is too far */ 8859109Sarchie#define MPPC_INSANE_JUMP 256 8959109Sarchie 9059109Sarchie/* MPPC packet header bits */ 9159109Sarchie#define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */ 9259109Sarchie#define MPPC_FLAG_RESTART 0x4000 /* compress history restart */ 9359109Sarchie#define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */ 9459109Sarchie#define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */ 9559109Sarchie#define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */ 9659109Sarchie 9759109Sarchie#define MPPE_UPDATE_MASK 0xff /* coherency count when we're */ 9859109Sarchie#define MPPE_UPDATE_FLAG 0xff /* supposed to update key */ 9959109Sarchie 10059109Sarchie#define MPPC_COMP_OK 0x05 10159109Sarchie#define MPPC_DECOMP_OK 0x05 10259109Sarchie 10359109Sarchie/* Per direction info */ 10459109Sarchiestruct ng_mppc_dir { 10559109Sarchie struct ng_mppc_config cfg; /* configuration */ 10659109Sarchie hook_p hook; /* netgraph hook */ 10759109Sarchie u_int16_t cc:12; /* coherency count */ 10859109Sarchie u_char flushed; /* clean history (xmit only) */ 10959109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 11059109Sarchie u_char *history; /* compression history */ 11159109Sarchie#endif 11259109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 11359109Sarchie u_char key[MPPE_KEY_LEN]; /* session key */ 11459109Sarchie struct rc4_state rc4; /* rc4 state */ 11559109Sarchie#endif 11659109Sarchie}; 11759109Sarchie 11859109Sarchie/* Node private data */ 11959109Sarchiestruct ng_mppc_private { 12059109Sarchie struct ng_mppc_dir xmit; /* compress/encrypt config */ 12159109Sarchie struct ng_mppc_dir recv; /* decompress/decrypt config */ 12259109Sarchie char *ctrlpath; /* path to controlling node */ 12359109Sarchie}; 12459109Sarchietypedef struct ng_mppc_private *priv_p; 12559109Sarchie 12659109Sarchie/* Netgraph node methods */ 12759109Sarchiestatic ng_constructor_t ng_mppc_constructor; 12859109Sarchiestatic ng_rcvmsg_t ng_mppc_rcvmsg; 12959109Sarchiestatic ng_shutdown_t ng_mppc_rmnode; 13059109Sarchiestatic ng_newhook_t ng_mppc_newhook; 13159109Sarchiestatic ng_rcvdata_t ng_mppc_rcvdata; 13259109Sarchiestatic ng_disconnect_t ng_mppc_disconnect; 13359109Sarchie 13459109Sarchie/* Helper functions */ 13559109Sarchiestatic int ng_mppc_compress(node_p node, 13659109Sarchie struct mbuf *m, struct mbuf **resultp); 13759109Sarchiestatic int ng_mppc_decompress(node_p node, 13859109Sarchie struct mbuf *m, struct mbuf **resultp); 13959109Sarchiestatic void ng_mppc_getkey(const u_char *h, u_char *h2, int len); 14059109Sarchiestatic void ng_mppc_updatekey(u_int32_t bits, 14159109Sarchie u_char *key0, u_char *key, struct rc4_state *rc4); 14259109Sarchiestatic void ng_mppc_reset_req(node_p node); 14359109Sarchie 14459109Sarchie/* Node type descriptor */ 14559109Sarchiestatic struct ng_type ng_mppc_typestruct = { 14659109Sarchie NG_VERSION, 14759109Sarchie NG_MPPC_NODE_TYPE, 14859109Sarchie NULL, 14959109Sarchie ng_mppc_constructor, 15059109Sarchie ng_mppc_rcvmsg, 15159109Sarchie ng_mppc_rmnode, 15259109Sarchie ng_mppc_newhook, 15359109Sarchie NULL, 15459109Sarchie NULL, 15559109Sarchie ng_mppc_rcvdata, 15659109Sarchie ng_mppc_rcvdata, 15759109Sarchie ng_mppc_disconnect, 15859109Sarchie NULL 15959109Sarchie}; 16059109SarchieNETGRAPH_INIT(mppc, &ng_mppc_typestruct); 16159109Sarchie 16259109Sarchie/* Fixed bit pattern to weaken keysize down to 40 bits */ 16359109Sarchiestatic const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e }; 16459109Sarchie 16559109Sarchie#define ERROUT(x) do { error = (x); goto done; } while (0) 16659109Sarchie 16759109Sarchie/************************************************************************ 16859109Sarchie NETGRAPH NODE STUFF 16959109Sarchie ************************************************************************/ 17059109Sarchie 17159109Sarchie/* 17259109Sarchie * Node type constructor 17359109Sarchie */ 17459109Sarchiestatic int 17559109Sarchieng_mppc_constructor(node_p *nodep) 17659109Sarchie{ 17759109Sarchie priv_p priv; 17859109Sarchie int error; 17959109Sarchie 18059109Sarchie /* Allocate private structure */ 18166182Sarchie MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT); 18259109Sarchie if (priv == NULL) 18359109Sarchie return (ENOMEM); 18459109Sarchie bzero(priv, sizeof(*priv)); 18559109Sarchie 18659109Sarchie /* Call generic node constructor */ 18759109Sarchie if ((error = ng_make_node_common(&ng_mppc_typestruct, nodep))) { 18859109Sarchie FREE(priv, M_NETGRAPH); 18959109Sarchie return (error); 19059109Sarchie } 19159109Sarchie (*nodep)->private = priv; 19259109Sarchie 19359109Sarchie /* Done */ 19459109Sarchie return (0); 19559109Sarchie} 19659109Sarchie 19759109Sarchie/* 19859109Sarchie * Give our OK for a hook to be added 19959109Sarchie */ 20059109Sarchiestatic int 20159109Sarchieng_mppc_newhook(node_p node, hook_p hook, const char *name) 20259109Sarchie{ 20359109Sarchie const priv_p priv = node->private; 20459109Sarchie hook_p *hookPtr; 20559109Sarchie 20659109Sarchie /* Check hook name */ 20759109Sarchie if (strcmp(name, NG_MPPC_HOOK_COMP) == 0) 20859109Sarchie hookPtr = &priv->xmit.hook; 20959109Sarchie else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0) 21059109Sarchie hookPtr = &priv->recv.hook; 21159109Sarchie else 21259109Sarchie return (EINVAL); 21359109Sarchie 21459109Sarchie /* See if already connected */ 21559109Sarchie if (*hookPtr != NULL) 21659109Sarchie return (EISCONN); 21759109Sarchie 21859109Sarchie /* OK */ 21959109Sarchie *hookPtr = hook; 22059109Sarchie return (0); 22159109Sarchie} 22259109Sarchie 22359109Sarchie/* 22459109Sarchie * Receive a control message 22559109Sarchie */ 22659109Sarchiestatic int 22759109Sarchieng_mppc_rcvmsg(node_p node, struct ng_mesg *msg, 22859728Sjulian const char *raddr, struct ng_mesg **rptr, hook_p lasthook) 22959109Sarchie{ 23059109Sarchie const priv_p priv = node->private; 23159109Sarchie struct ng_mesg *resp = NULL; 23259109Sarchie int error = 0; 23359109Sarchie 23459109Sarchie switch (msg->header.typecookie) { 23559109Sarchie case NGM_MPPC_COOKIE: 23659109Sarchie switch (msg->header.cmd) { 23759109Sarchie case NGM_MPPC_CONFIG_COMP: 23859109Sarchie case NGM_MPPC_CONFIG_DECOMP: 23959109Sarchie { 24059109Sarchie struct ng_mppc_config *const cfg 24159109Sarchie = (struct ng_mppc_config *)msg->data; 24259109Sarchie const int isComp = 24359109Sarchie msg->header.cmd == NGM_MPPC_CONFIG_COMP; 24459109Sarchie struct ng_mppc_dir *const d = isComp ? 24559109Sarchie &priv->xmit : &priv->recv; 24659109Sarchie 24759109Sarchie /* Check configuration */ 24859109Sarchie if (msg->header.arglen != sizeof(*cfg)) 24959109Sarchie ERROUT(EINVAL); 25059109Sarchie if (cfg->enable) { 25159109Sarchie if ((cfg->bits & ~MPPC_VALID_BITS) != 0) 25259109Sarchie ERROUT(EINVAL); 25359109Sarchie#ifndef NETGRAPH_MPPC_COMPRESSION 25459109Sarchie if ((cfg->bits & MPPC_BIT) != 0) 25559109Sarchie ERROUT(EPROTONOSUPPORT); 25659109Sarchie#endif 25759109Sarchie#ifndef NETGRAPH_MPPC_ENCRYPTION 25859109Sarchie if ((cfg->bits & MPPE_BITS) != 0) 25959109Sarchie ERROUT(EPROTONOSUPPORT); 26059109Sarchie#endif 26159109Sarchie } else 26259109Sarchie cfg->bits = 0; 26359109Sarchie 26459109Sarchie /* Save return address so we can send reset-req's */ 26559109Sarchie if (priv->ctrlpath != NULL) { 26659109Sarchie FREE(priv->ctrlpath, M_NETGRAPH); 26759109Sarchie priv->ctrlpath = NULL; 26859109Sarchie } 26959109Sarchie if (!isComp && raddr != NULL) { 27059109Sarchie MALLOC(priv->ctrlpath, char *, 27159109Sarchie strlen(raddr) + 1, M_NETGRAPH, M_NOWAIT); 27259109Sarchie if (priv->ctrlpath == NULL) 27359109Sarchie ERROUT(ENOMEM); 27459109Sarchie strcpy(priv->ctrlpath, raddr); 27559109Sarchie } 27659109Sarchie 27759109Sarchie /* Configuration is OK, reset to it */ 27859109Sarchie d->cfg = *cfg; 27959109Sarchie 28059109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 28159109Sarchie /* Initialize state buffers for compression */ 28259109Sarchie if (d->history != NULL) { 28359109Sarchie FREE(d->history, M_NETGRAPH); 28459109Sarchie d->history = NULL; 28559109Sarchie } 28659109Sarchie if ((cfg->bits & MPPC_BIT) != 0) { 28759109Sarchie MALLOC(d->history, u_char *, 28859109Sarchie isComp ? MPPC_SizeOfCompressionHistory() : 28959109Sarchie MPPC_SizeOfDecompressionHistory(), 29059109Sarchie M_NETGRAPH, M_NOWAIT); 29159109Sarchie if (d->history == NULL) 29259109Sarchie ERROUT(ENOMEM); 29359109Sarchie if (isComp) 29459109Sarchie MPPC_InitCompressionHistory(d->history); 29559109Sarchie else { 29659109Sarchie MPPC_InitDecompressionHistory( 29759109Sarchie d->history); 29859109Sarchie } 29959109Sarchie } 30059109Sarchie#endif 30159109Sarchie 30259109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 30359109Sarchie /* Generate initial session keys for encryption */ 30459109Sarchie if ((cfg->bits & MPPE_BITS) != 0) { 30559109Sarchie const int keylen = KEYLEN(cfg->bits); 30659109Sarchie 30759109Sarchie bcopy(cfg->startkey, d->key, keylen); 30859109Sarchie ng_mppc_getkey(cfg->startkey, d->key, keylen); 30959109Sarchie if ((cfg->bits & MPPE_128) == 0) { 31059109Sarchie bcopy(&ng_mppe_weakenkey, d->key, 31159109Sarchie sizeof(ng_mppe_weakenkey)); 31259109Sarchie } 31359109Sarchie rc4_init(&d->rc4, d->key, keylen); 31459109Sarchie } 31559109Sarchie#endif 31659109Sarchie 31759109Sarchie /* Initialize other state */ 31859109Sarchie d->cc = 0; 31959109Sarchie d->flushed = 0; 32059109Sarchie break; 32159109Sarchie } 32259109Sarchie 32359109Sarchie case NGM_MPPC_RESETREQ: 32459109Sarchie ng_mppc_reset_req(node); 32559109Sarchie break; 32659109Sarchie 32759109Sarchie default: 32859109Sarchie error = EINVAL; 32959109Sarchie break; 33059109Sarchie } 33159109Sarchie break; 33259109Sarchie default: 33359109Sarchie error = EINVAL; 33459109Sarchie break; 33559109Sarchie } 33659109Sarchie if (rptr) 33759109Sarchie *rptr = resp; 33859109Sarchie else if (resp) 33959109Sarchie FREE(resp, M_NETGRAPH); 34059109Sarchie 34159109Sarchiedone: 34259109Sarchie FREE(msg, M_NETGRAPH); 34359109Sarchie return (error); 34459109Sarchie} 34559109Sarchie 34659109Sarchie/* 34759109Sarchie * Receive incoming data on our hook. 34859109Sarchie */ 34959109Sarchiestatic int 35059728Sjulianng_mppc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 35159728Sjulian struct mbuf **ret_m, meta_p *ret_meta) 35259109Sarchie{ 35359109Sarchie const node_p node = hook->node; 35459109Sarchie const priv_p priv = node->private; 35559109Sarchie struct mbuf *out; 35659109Sarchie int error; 35759109Sarchie 35859109Sarchie /* Compress and/or encrypt */ 35959109Sarchie if (hook == priv->xmit.hook) { 36059109Sarchie if (!priv->xmit.cfg.enable) { 36159109Sarchie NG_FREE_DATA(m, meta); 36259109Sarchie return (ENXIO); 36359109Sarchie } 36459109Sarchie if ((error = ng_mppc_compress(node, m, &out)) != 0) { 36559109Sarchie NG_FREE_DATA(m, meta); 36659109Sarchie return(error); 36759109Sarchie } 36859109Sarchie m_freem(m); 36959109Sarchie NG_SEND_DATA(error, priv->xmit.hook, out, meta); 37059109Sarchie return (error); 37159109Sarchie } 37259109Sarchie 37359109Sarchie /* Decompress and/or decrypt */ 37459109Sarchie if (hook == priv->recv.hook) { 37559109Sarchie if (!priv->recv.cfg.enable) { 37659109Sarchie NG_FREE_DATA(m, meta); 37759109Sarchie return (ENXIO); 37859109Sarchie } 37959109Sarchie if ((error = ng_mppc_decompress(node, m, &out)) != 0) { 38059109Sarchie NG_FREE_DATA(m, meta); 38159109Sarchie if (error == EINVAL && priv->ctrlpath != NULL) { 38259109Sarchie struct ng_mesg *msg; 38359109Sarchie 38459109Sarchie /* Need to send a reset-request */ 38559109Sarchie NG_MKMESSAGE(msg, NGM_MPPC_COOKIE, 38659109Sarchie NGM_MPPC_RESETREQ, 0, M_NOWAIT); 38759109Sarchie if (msg == NULL) 38859109Sarchie return (error); 38959109Sarchie ng_send_msg(node, msg, priv->ctrlpath, NULL); 39059109Sarchie } 39159109Sarchie return (error); 39259109Sarchie } 39359109Sarchie m_freem(m); 39459109Sarchie NG_SEND_DATA(error, priv->recv.hook, out, meta); 39559109Sarchie return (error); 39659109Sarchie } 39759109Sarchie 39859109Sarchie /* Oops */ 39959109Sarchie panic("%s: unknown hook", __FUNCTION__); 40059109Sarchie} 40159109Sarchie 40259109Sarchie/* 40359109Sarchie * Destroy node 40459109Sarchie */ 40559109Sarchiestatic int 40659109Sarchieng_mppc_rmnode(node_p node) 40759109Sarchie{ 40859109Sarchie const priv_p priv = node->private; 40959109Sarchie 41059109Sarchie /* Take down netgraph node */ 41159109Sarchie node->flags |= NG_INVALID; 41259109Sarchie ng_cutlinks(node); 41359109Sarchie ng_unname(node); 41459109Sarchie if (priv->ctrlpath != NULL) 41559109Sarchie FREE(priv->ctrlpath, M_NETGRAPH); 41659109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 41759109Sarchie if (priv->xmit.history != NULL) 41859109Sarchie FREE(priv->xmit.history, M_NETGRAPH); 41959109Sarchie if (priv->recv.history != NULL) 42059109Sarchie FREE(priv->recv.history, M_NETGRAPH); 42159109Sarchie#endif 42259109Sarchie bzero(priv, sizeof(*priv)); 42359109Sarchie FREE(priv, M_NETGRAPH); 42459109Sarchie node->private = NULL; 42559109Sarchie ng_unref(node); /* let the node escape */ 42659109Sarchie return (0); 42759109Sarchie} 42859109Sarchie 42959109Sarchie/* 43059109Sarchie * Hook disconnection 43159109Sarchie */ 43259109Sarchiestatic int 43359109Sarchieng_mppc_disconnect(hook_p hook) 43459109Sarchie{ 43559109Sarchie const node_p node = hook->node; 43659109Sarchie const priv_p priv = node->private; 43759109Sarchie 43859109Sarchie /* Zero out hook pointer */ 43959109Sarchie if (hook == priv->xmit.hook) 44059109Sarchie priv->xmit.hook = NULL; 44159109Sarchie if (hook == priv->recv.hook) 44259109Sarchie priv->recv.hook = NULL; 44359109Sarchie 44459109Sarchie /* Go away if no longer connected */ 44559109Sarchie if (node->numhooks == 0) 44659109Sarchie ng_rmnode(node); 44759109Sarchie return (0); 44859109Sarchie} 44959109Sarchie 45059109Sarchie/************************************************************************ 45159109Sarchie HELPER STUFF 45259109Sarchie ************************************************************************/ 45359109Sarchie 45459109Sarchie/* 45559109Sarchie * Compress/encrypt a packet and put the result in a new mbuf at *resultp. 45659109Sarchie * The original mbuf is not free'd. 45759109Sarchie */ 45859109Sarchiestatic int 45959109Sarchieng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp) 46059109Sarchie{ 46159109Sarchie const priv_p priv = node->private; 46259109Sarchie struct ng_mppc_dir *const d = &priv->xmit; 46359109Sarchie u_char *inbuf, *outbuf; 46459109Sarchie int outlen, inlen; 46559109Sarchie u_int16_t header; 46659109Sarchie 46759109Sarchie /* Initialize */ 46859109Sarchie *resultp = NULL; 46959109Sarchie header = d->cc; 47059109Sarchie if (d->flushed) { 47159109Sarchie header |= MPPC_FLAG_FLUSHED; 47259109Sarchie d->flushed = 0; 47359109Sarchie } 47459109Sarchie 47559109Sarchie /* Work with contiguous regions of memory */ 47659109Sarchie inlen = m->m_pkthdr.len; 47759109Sarchie MALLOC(inbuf, u_char *, inlen, M_NETGRAPH, M_NOWAIT); 47859109Sarchie if (inbuf == NULL) 47959109Sarchie return (ENOMEM); 48059109Sarchie m_copydata(m, 0, inlen, (caddr_t)inbuf); 48159109Sarchie if ((d->cfg.bits & MPPC_BIT) != 0) 48259109Sarchie outlen = MPPC_MAX_BLOWUP(inlen); 48359109Sarchie else 48459109Sarchie outlen = MPPC_HDRLEN + inlen; 48566177Sarchie MALLOC(outbuf, u_char *, outlen, M_NETGRAPH, M_NOWAIT); 48659109Sarchie if (outbuf == NULL) { 48759109Sarchie FREE(inbuf, M_NETGRAPH); 48859109Sarchie return (ENOMEM); 48959109Sarchie } 49059109Sarchie 49159109Sarchie /* Compress "inbuf" into "outbuf" (if compression enabled) */ 49259109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 49359109Sarchie if ((d->cfg.bits & MPPC_BIT) != 0) { 49459109Sarchie u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS; 49559109Sarchie u_char *source, *dest; 49659109Sarchie u_long sourceCnt, destCnt; 49759109Sarchie int rtn; 49859109Sarchie 49959109Sarchie /* Prepare to compress */ 50059109Sarchie source = inbuf; 50159109Sarchie sourceCnt = inlen; 50259109Sarchie dest = outbuf + MPPC_HDRLEN; 50359109Sarchie destCnt = outlen - MPPC_HDRLEN; 50459109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 50559109Sarchie flags |= MPPC_SAVE_HISTORY; 50659109Sarchie 50759109Sarchie /* Compress */ 50859109Sarchie rtn = MPPC_Compress(&source, &dest, &sourceCnt, 50959109Sarchie &destCnt, d->history, flags, 0); 51059109Sarchie 51159109Sarchie /* Check return value */ 51259109Sarchie KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __FUNCTION__)); 51359109Sarchie if ((rtn & MPPC_EXPANDED) == 0 51459109Sarchie && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) { 51559109Sarchie outlen -= destCnt; 51659109Sarchie header |= MPPC_FLAG_COMPRESSED; 51759109Sarchie if ((rtn & MPPC_RESTART_HISTORY) != 0) 51859109Sarchie header |= MPPC_FLAG_RESTART; 51959109Sarchie } 52059109Sarchie d->flushed = (rtn & MPPC_EXPANDED) != 0 52159109Sarchie || (flags & MPPC_SAVE_HISTORY) == 0; 52259109Sarchie } 52359109Sarchie#endif 52459109Sarchie 52559109Sarchie /* If we did not compress this packet, copy it to output buffer */ 52659109Sarchie if ((header & MPPC_FLAG_COMPRESSED) == 0) { 52759109Sarchie bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen); 52859109Sarchie outlen = MPPC_HDRLEN + inlen; 52959109Sarchie } 53059109Sarchie FREE(inbuf, M_NETGRAPH); 53159109Sarchie 53259109Sarchie /* Always set the flushed bit in stateless mode */ 53359109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0) 53459109Sarchie header |= MPPC_FLAG_FLUSHED; 53559109Sarchie 53659109Sarchie /* Now encrypt packet (if encryption enabled) */ 53759109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 53859109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 53959109Sarchie 54059109Sarchie /* Set header bits; need to reset key if we say we did */ 54159109Sarchie header |= MPPC_FLAG_ENCRYPTED; 54259109Sarchie if ((header & MPPC_FLAG_FLUSHED) != 0) 54359109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 54459109Sarchie 54559109Sarchie /* Update key if it's time */ 54659109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0 54759109Sarchie || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { 54859109Sarchie ng_mppc_updatekey(d->cfg.bits, 54959109Sarchie d->cfg.startkey, d->key, &d->rc4); 55059109Sarchie } 55159109Sarchie 55259109Sarchie /* Encrypt packet */ 55359109Sarchie rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN, 55459109Sarchie outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN); 55559109Sarchie } 55659109Sarchie#endif 55759109Sarchie 55859109Sarchie /* Update sequence number */ 55959109Sarchie d->cc++; 56059109Sarchie 56159109Sarchie /* Install header */ 56259109Sarchie *((u_int16_t *)outbuf) = htons(header); 56359109Sarchie 56459109Sarchie /* Return packet in an mbuf */ 56559109Sarchie *resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL); 56659109Sarchie FREE(outbuf, M_NETGRAPH); 56759109Sarchie return (*resultp == NULL ? ENOBUFS : 0); 56859109Sarchie} 56959109Sarchie 57059109Sarchie/* 57159109Sarchie * Decompress/decrypt packet and put the result in a new mbuf at *resultp. 57259109Sarchie * The original mbuf is not free'd. 57359109Sarchie */ 57459109Sarchiestatic int 57559109Sarchieng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp) 57659109Sarchie{ 57759109Sarchie const priv_p priv = node->private; 57859109Sarchie struct ng_mppc_dir *const d = &priv->recv; 57959109Sarchie u_int16_t header, cc, numLost; 58059109Sarchie u_char *buf; 58159109Sarchie int len; 58259109Sarchie 58359109Sarchie /* Pull off header */ 58459109Sarchie if (m->m_pkthdr.len < MPPC_HDRLEN) 58559109Sarchie return (EINVAL); 58659109Sarchie m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header); 58759109Sarchie NTOHS(header); 58859109Sarchie cc = (header & MPPC_CCOUNT_MASK); 58959109Sarchie 59059109Sarchie /* Copy payload into a contiguous region of memory */ 59159109Sarchie len = m->m_pkthdr.len - MPPC_HDRLEN; 59259109Sarchie MALLOC(buf, u_char *, len, M_NETGRAPH, M_NOWAIT); 59359109Sarchie if (buf == NULL) 59459109Sarchie return (ENOMEM); 59559109Sarchie m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf); 59659109Sarchie 59759109Sarchie /* Check for insane jumps in sequence numbering (D.O.S. attack) */ 59859109Sarchie numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK); 59959109Sarchie if (numLost >= MPPC_INSANE_JUMP) { 60059109Sarchie log(LOG_ERR, "%s: insane jump %d", __FUNCTION__, numLost); 60159109Sarchie priv->recv.cfg.enable = 0; 60259109Sarchie goto failed; 60359109Sarchie } 60459109Sarchie 60559109Sarchie /* If flushed bit set, we can always handle packet */ 60659109Sarchie if ((header & MPPC_FLAG_FLUSHED) != 0) { 60759109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 60859109Sarchie if (d->history != NULL) 60959109Sarchie MPPC_InitDecompressionHistory(d->history); 61059109Sarchie#endif 61159109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 61259109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 61359109Sarchie 61459109Sarchie /* Resync as necessary, skipping lost packets */ 61559109Sarchie while (d->cc != cc) { 61659109Sarchie if ((d->cfg.bits & MPPE_STATELESS) 61759109Sarchie || (d->cc & MPPE_UPDATE_MASK) 61859109Sarchie == MPPE_UPDATE_FLAG) { 61959109Sarchie ng_mppc_updatekey(d->cfg.bits, 62059109Sarchie d->cfg.startkey, d->key, &d->rc4); 62159109Sarchie } 62259109Sarchie d->cc++; 62359109Sarchie } 62459109Sarchie 62559109Sarchie /* Reset key (except in stateless mode, see below) */ 62659109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 62759109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 62859109Sarchie } 62959109Sarchie#endif 63059109Sarchie d->cc = cc; /* skip over lost seq numbers */ 63159109Sarchie numLost = 0; /* act like no packets were lost */ 63259109Sarchie } 63359109Sarchie 63459109Sarchie /* Can't decode non-sequential packets without a flushed bit */ 63559109Sarchie if (numLost != 0) 63659109Sarchie goto failed; 63759109Sarchie 63859109Sarchie /* Decrypt packet */ 63959109Sarchie if ((header & MPPC_FLAG_ENCRYPTED) != 0) { 64059109Sarchie 64159109Sarchie /* Are we not expecting encryption? */ 64259109Sarchie if ((d->cfg.bits & MPPE_BITS) == 0) { 64359109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 64459109Sarchie __FUNCTION__, "encrypted"); 64559109Sarchie goto failed; 64659109Sarchie } 64759109Sarchie 64859109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 64959109Sarchie /* Update key if it's time (always in stateless mode) */ 65059109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0 65159109Sarchie || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { 65259109Sarchie ng_mppc_updatekey(d->cfg.bits, 65359109Sarchie d->cfg.startkey, d->key, &d->rc4); 65459109Sarchie } 65559109Sarchie 65659109Sarchie /* Decrypt packet */ 65759109Sarchie rc4_crypt(&d->rc4, buf, buf, len); 65859109Sarchie#endif 65959109Sarchie } else { 66059109Sarchie 66159109Sarchie /* Are we expecting encryption? */ 66259109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 66359109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 66459109Sarchie __FUNCTION__, "unencrypted"); 66559109Sarchie goto failed; 66659109Sarchie } 66759109Sarchie } 66859109Sarchie 66959109Sarchie /* Update coherency count for next time (12 bit arithmetic) */ 67059109Sarchie d->cc++; 67159109Sarchie 67259109Sarchie /* Check for unexpected compressed packet */ 67359109Sarchie if ((header & MPPC_FLAG_COMPRESSED) != 0 67459109Sarchie && (d->cfg.bits & MPPC_BIT) == 0) { 67559109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 67659109Sarchie __FUNCTION__, "compressed"); 67759109Sarchiefailed: 67859109Sarchie FREE(buf, M_NETGRAPH); 67959109Sarchie return (EINVAL); 68059109Sarchie } 68159109Sarchie 68259109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 68359109Sarchie /* Decompress packet */ 68459109Sarchie if ((header & MPPC_FLAG_COMPRESSED) != 0) { 68559109Sarchie int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS; 68659109Sarchie u_char *decompbuf, *source, *dest; 68759109Sarchie u_long sourceCnt, destCnt; 68859109Sarchie int decomplen, rtn; 68959109Sarchie 69059109Sarchie /* Allocate a buffer for decompressed data */ 69159109Sarchie MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE 69259109Sarchie + MPPC_DECOMP_SAFETY, M_NETGRAPH, M_NOWAIT); 69359109Sarchie if (decompbuf == NULL) { 69459109Sarchie FREE(buf, M_NETGRAPH); 69559109Sarchie return (ENOMEM); 69659109Sarchie } 69759109Sarchie decomplen = MPPC_DECOMP_BUFSIZE; 69859109Sarchie 69959109Sarchie /* Prepare to decompress */ 70059109Sarchie source = buf; 70159109Sarchie sourceCnt = len; 70259109Sarchie dest = decompbuf; 70359109Sarchie destCnt = decomplen; 70459109Sarchie if ((header & MPPC_FLAG_RESTART) != 0) 70559109Sarchie flags |= MPPC_RESTART_HISTORY; 70659109Sarchie 70759109Sarchie /* Decompress */ 70859109Sarchie rtn = MPPC_Decompress(&source, &dest, 70959109Sarchie &sourceCnt, &destCnt, d->history, flags); 71059109Sarchie 71159109Sarchie /* Check return value */ 71259109Sarchie KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __FUNCTION__)); 71359109Sarchie if ((rtn & MPPC_DEST_EXHAUSTED) != 0 71459109Sarchie || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) { 71559109Sarchie log(LOG_ERR, "%s: decomp returned 0x%x", 71659109Sarchie __FUNCTION__, rtn); 71759109Sarchie FREE(decompbuf, M_NETGRAPH); 71859109Sarchie goto failed; 71959109Sarchie } 72059109Sarchie 72159109Sarchie /* Replace compressed data with decompressed data */ 72259109Sarchie FREE(buf, M_NETGRAPH); 72359109Sarchie buf = decompbuf; 72459109Sarchie len = decomplen - destCnt; 72559109Sarchie } 72659109Sarchie#endif 72759109Sarchie 72859109Sarchie /* Return result in an mbuf */ 72959109Sarchie *resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL); 73059109Sarchie FREE(buf, M_NETGRAPH); 73159109Sarchie return (*resultp == NULL ? ENOBUFS : 0); 73259109Sarchie} 73359109Sarchie 73459109Sarchie/* 73559109Sarchie * The peer has sent us a CCP ResetRequest, so reset our transmit state. 73659109Sarchie */ 73759109Sarchiestatic void 73859109Sarchieng_mppc_reset_req(node_p node) 73959109Sarchie{ 74059109Sarchie const priv_p priv = node->private; 74159109Sarchie struct ng_mppc_dir *const d = &priv->xmit; 74259109Sarchie 74359109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 74459109Sarchie if (d->history != NULL) 74559109Sarchie MPPC_InitCompressionHistory(d->history); 74659109Sarchie#endif 74759109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 74859109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 74959109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 75059109Sarchie#endif 75159109Sarchie d->flushed = 1; 75259109Sarchie} 75359109Sarchie 75459109Sarchie/* 75559109Sarchie * Generate a new encryption key 75659109Sarchie */ 75759109Sarchiestatic void 75859109Sarchieng_mppc_getkey(const u_char *h, u_char *h2, int len) 75959109Sarchie{ 76059109Sarchie static const u_char pad1[10] = 76159109Sarchie { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; 76259109Sarchie static const u_char pad2[10] = 76359109Sarchie { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, }; 76459109Sarchie u_char hash[20]; 76559109Sarchie SHA1_CTX c; 76659109Sarchie int k; 76759109Sarchie 76859109Sarchie bzero(&hash, sizeof(hash)); 76959109Sarchie SHA1Init(&c); 77059109Sarchie SHA1Update(&c, h, len); 77159109Sarchie for (k = 0; k < 4; k++) 77259109Sarchie SHA1Update(&c, pad1, sizeof(pad2)); 77359109Sarchie SHA1Update(&c, h2, len); 77459109Sarchie for (k = 0; k < 4; k++) 77559109Sarchie SHA1Update(&c, pad2, sizeof(pad2)); 77659109Sarchie SHA1Final(hash, &c); 77759109Sarchie bcopy(hash, h2, len); 77859109Sarchie} 77959109Sarchie 78059109Sarchie/* 78159109Sarchie * Update the encryption key 78259109Sarchie */ 78359109Sarchiestatic void 78459109Sarchieng_mppc_updatekey(u_int32_t bits, 78559109Sarchie u_char *key0, u_char *key, struct rc4_state *rc4) 78659109Sarchie{ 78759109Sarchie const int keylen = KEYLEN(bits); 78859109Sarchie 78959109Sarchie ng_mppc_getkey(key0, key, keylen); 79059109Sarchie rc4_init(rc4, key, keylen); 79159109Sarchie rc4_crypt(rc4, key, key, keylen); 79259109Sarchie if ((bits & MPPE_128) == 0) 79359109Sarchie bcopy(&ng_mppe_weakenkey, key, sizeof(ng_mppe_weakenkey)); 79459109Sarchie rc4_init(rc4, key, keylen); 79559109Sarchie} 79659109Sarchie 797