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