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