ng_mppc.c revision 70784
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 70784 2001-01-08 05:34:06Z 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 */ 12270700Sjulian ng_ID_t ctrlnode; /* 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; 12970700Sjulianstatic ng_shutdown_t ng_mppc_shutdown; 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 = { 14670159Sjulian NG_ABI_VERSION, 14759109Sarchie NG_MPPC_NODE_TYPE, 14859109Sarchie NULL, 14959109Sarchie ng_mppc_constructor, 15059109Sarchie ng_mppc_rcvmsg, 15170700Sjulian ng_mppc_shutdown, 15259109Sarchie ng_mppc_newhook, 15359109Sarchie NULL, 15459109Sarchie NULL, 15559109Sarchie ng_mppc_rcvdata, 15659109Sarchie ng_mppc_disconnect, 15759109Sarchie NULL 15859109Sarchie}; 15959109SarchieNETGRAPH_INIT(mppc, &ng_mppc_typestruct); 16059109Sarchie 16159109Sarchie/* Fixed bit pattern to weaken keysize down to 40 bits */ 16259109Sarchiestatic const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e }; 16359109Sarchie 16459109Sarchie#define ERROUT(x) do { error = (x); goto done; } while (0) 16559109Sarchie 16659109Sarchie/************************************************************************ 16759109Sarchie NETGRAPH NODE STUFF 16859109Sarchie ************************************************************************/ 16959109Sarchie 17059109Sarchie/* 17159109Sarchie * Node type constructor 17259109Sarchie */ 17359109Sarchiestatic int 17470700Sjulianng_mppc_constructor(node_p node) 17559109Sarchie{ 17659109Sarchie priv_p priv; 17759109Sarchie 17859109Sarchie /* Allocate private structure */ 17968876Sdwmalone MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 18059109Sarchie if (priv == NULL) 18159109Sarchie return (ENOMEM); 18259109Sarchie 18370784Sjulian NG_NODE_SET_PRIVATE(node, priv); 18459109Sarchie 18559109Sarchie /* Done */ 18659109Sarchie return (0); 18759109Sarchie} 18859109Sarchie 18959109Sarchie/* 19059109Sarchie * Give our OK for a hook to be added 19159109Sarchie */ 19259109Sarchiestatic int 19359109Sarchieng_mppc_newhook(node_p node, hook_p hook, const char *name) 19459109Sarchie{ 19570784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 19659109Sarchie hook_p *hookPtr; 19759109Sarchie 19859109Sarchie /* Check hook name */ 19959109Sarchie if (strcmp(name, NG_MPPC_HOOK_COMP) == 0) 20059109Sarchie hookPtr = &priv->xmit.hook; 20159109Sarchie else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0) 20259109Sarchie hookPtr = &priv->recv.hook; 20359109Sarchie else 20459109Sarchie return (EINVAL); 20559109Sarchie 20659109Sarchie /* See if already connected */ 20759109Sarchie if (*hookPtr != NULL) 20859109Sarchie return (EISCONN); 20959109Sarchie 21059109Sarchie /* OK */ 21159109Sarchie *hookPtr = hook; 21259109Sarchie return (0); 21359109Sarchie} 21459109Sarchie 21559109Sarchie/* 21659109Sarchie * Receive a control message 21759109Sarchie */ 21859109Sarchiestatic int 21970700Sjulianng_mppc_rcvmsg(node_p node, item_p item, hook_p lasthook) 22059109Sarchie{ 22170784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 22259109Sarchie struct ng_mesg *resp = NULL; 22359109Sarchie int error = 0; 22470700Sjulian struct ng_mesg *msg; 22559109Sarchie 22670700Sjulian NGI_GET_MSG(item, msg); 22759109Sarchie switch (msg->header.typecookie) { 22859109Sarchie case NGM_MPPC_COOKIE: 22959109Sarchie switch (msg->header.cmd) { 23059109Sarchie case NGM_MPPC_CONFIG_COMP: 23159109Sarchie case NGM_MPPC_CONFIG_DECOMP: 23259109Sarchie { 23359109Sarchie struct ng_mppc_config *const cfg 23459109Sarchie = (struct ng_mppc_config *)msg->data; 23559109Sarchie const int isComp = 23659109Sarchie msg->header.cmd == NGM_MPPC_CONFIG_COMP; 23759109Sarchie struct ng_mppc_dir *const d = isComp ? 23859109Sarchie &priv->xmit : &priv->recv; 23959109Sarchie 24059109Sarchie /* Check configuration */ 24159109Sarchie if (msg->header.arglen != sizeof(*cfg)) 24259109Sarchie ERROUT(EINVAL); 24359109Sarchie if (cfg->enable) { 24459109Sarchie if ((cfg->bits & ~MPPC_VALID_BITS) != 0) 24559109Sarchie ERROUT(EINVAL); 24659109Sarchie#ifndef NETGRAPH_MPPC_COMPRESSION 24759109Sarchie if ((cfg->bits & MPPC_BIT) != 0) 24859109Sarchie ERROUT(EPROTONOSUPPORT); 24959109Sarchie#endif 25059109Sarchie#ifndef NETGRAPH_MPPC_ENCRYPTION 25159109Sarchie if ((cfg->bits & MPPE_BITS) != 0) 25259109Sarchie ERROUT(EPROTONOSUPPORT); 25359109Sarchie#endif 25459109Sarchie } else 25559109Sarchie cfg->bits = 0; 25659109Sarchie 25759109Sarchie /* Save return address so we can send reset-req's */ 25870700Sjulian priv->ctrlnode = NGI_RETADDR(item); 25959109Sarchie 26059109Sarchie /* Configuration is OK, reset to it */ 26159109Sarchie d->cfg = *cfg; 26259109Sarchie 26359109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 26459109Sarchie /* Initialize state buffers for compression */ 26559109Sarchie if (d->history != NULL) { 26659109Sarchie FREE(d->history, M_NETGRAPH); 26759109Sarchie d->history = NULL; 26859109Sarchie } 26959109Sarchie if ((cfg->bits & MPPC_BIT) != 0) { 27059109Sarchie MALLOC(d->history, u_char *, 27159109Sarchie isComp ? MPPC_SizeOfCompressionHistory() : 27259109Sarchie MPPC_SizeOfDecompressionHistory(), 27359109Sarchie M_NETGRAPH, M_NOWAIT); 27459109Sarchie if (d->history == NULL) 27559109Sarchie ERROUT(ENOMEM); 27659109Sarchie if (isComp) 27759109Sarchie MPPC_InitCompressionHistory(d->history); 27859109Sarchie else { 27959109Sarchie MPPC_InitDecompressionHistory( 28059109Sarchie d->history); 28159109Sarchie } 28259109Sarchie } 28359109Sarchie#endif 28459109Sarchie 28559109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 28659109Sarchie /* Generate initial session keys for encryption */ 28759109Sarchie if ((cfg->bits & MPPE_BITS) != 0) { 28859109Sarchie const int keylen = KEYLEN(cfg->bits); 28959109Sarchie 29059109Sarchie bcopy(cfg->startkey, d->key, keylen); 29159109Sarchie ng_mppc_getkey(cfg->startkey, d->key, keylen); 29259109Sarchie if ((cfg->bits & MPPE_128) == 0) { 29359109Sarchie bcopy(&ng_mppe_weakenkey, d->key, 29459109Sarchie sizeof(ng_mppe_weakenkey)); 29559109Sarchie } 29659109Sarchie rc4_init(&d->rc4, d->key, keylen); 29759109Sarchie } 29859109Sarchie#endif 29959109Sarchie 30059109Sarchie /* Initialize other state */ 30159109Sarchie d->cc = 0; 30259109Sarchie d->flushed = 0; 30359109Sarchie break; 30459109Sarchie } 30559109Sarchie 30659109Sarchie case NGM_MPPC_RESETREQ: 30759109Sarchie ng_mppc_reset_req(node); 30859109Sarchie break; 30959109Sarchie 31059109Sarchie default: 31159109Sarchie error = EINVAL; 31259109Sarchie break; 31359109Sarchie } 31459109Sarchie break; 31559109Sarchie default: 31659109Sarchie error = EINVAL; 31759109Sarchie break; 31859109Sarchie } 31959109Sarchiedone: 32070700Sjulian NG_RESPOND_MSG(error, node, item, resp); 32170700Sjulian NG_FREE_MSG(msg); 32259109Sarchie return (error); 32359109Sarchie} 32459109Sarchie 32559109Sarchie/* 32659109Sarchie * Receive incoming data on our hook. 32759109Sarchie */ 32859109Sarchiestatic int 32970700Sjulianng_mppc_rcvdata(hook_p hook, item_p item) 33059109Sarchie{ 33170784Sjulian const node_p node = NG_HOOK_NODE(hook); 33270784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 33359109Sarchie struct mbuf *out; 33459109Sarchie int error; 33570700Sjulian struct mbuf *m; 33659109Sarchie 33770700Sjulian NGI_GET_M(item, m); 33859109Sarchie /* Compress and/or encrypt */ 33959109Sarchie if (hook == priv->xmit.hook) { 34059109Sarchie if (!priv->xmit.cfg.enable) { 34170700Sjulian NG_FREE_M(m); 34270700Sjulian NG_FREE_ITEM(item); 34359109Sarchie return (ENXIO); 34459109Sarchie } 34559109Sarchie if ((error = ng_mppc_compress(node, m, &out)) != 0) { 34670700Sjulian NG_FREE_M(m); 34770700Sjulian NG_FREE_ITEM(item); 34859109Sarchie return(error); 34959109Sarchie } 35070700Sjulian NG_FREE_M(m); 35170700Sjulian NG_FWD_NEW_DATA(error, item, priv->xmit.hook, out); 35259109Sarchie return (error); 35359109Sarchie } 35459109Sarchie 35559109Sarchie /* Decompress and/or decrypt */ 35659109Sarchie if (hook == priv->recv.hook) { 35759109Sarchie if (!priv->recv.cfg.enable) { 35870700Sjulian NG_FREE_M(m); 35970700Sjulian NG_FREE_ITEM(item); 36059109Sarchie return (ENXIO); 36159109Sarchie } 36259109Sarchie if ((error = ng_mppc_decompress(node, m, &out)) != 0) { 36370700Sjulian NG_FREE_M(m); 36470700Sjulian NG_FREE_ITEM(item); 36570700Sjulian if (error == EINVAL && priv->ctrlnode != NULL) { 36659109Sarchie struct ng_mesg *msg; 36759109Sarchie 36859109Sarchie /* Need to send a reset-request */ 36959109Sarchie NG_MKMESSAGE(msg, NGM_MPPC_COOKIE, 37059109Sarchie NGM_MPPC_RESETREQ, 0, M_NOWAIT); 37159109Sarchie if (msg == NULL) 37259109Sarchie return (error); 37370700Sjulian NG_SEND_MSG_ID(error, node, msg, 37470700Sjulian priv->ctrlnode, NULL); 37559109Sarchie } 37659109Sarchie return (error); 37759109Sarchie } 37870700Sjulian NG_FREE_M(m); 37970700Sjulian NG_FWD_NEW_DATA(error, item, priv->recv.hook, out); 38059109Sarchie return (error); 38159109Sarchie } 38259109Sarchie 38359109Sarchie /* Oops */ 38459109Sarchie panic("%s: unknown hook", __FUNCTION__); 38559109Sarchie} 38659109Sarchie 38759109Sarchie/* 38859109Sarchie * Destroy node 38959109Sarchie */ 39059109Sarchiestatic int 39170700Sjulianng_mppc_shutdown(node_p node) 39259109Sarchie{ 39370784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 39459109Sarchie 39559109Sarchie /* Take down netgraph node */ 39659109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 39759109Sarchie if (priv->xmit.history != NULL) 39859109Sarchie FREE(priv->xmit.history, M_NETGRAPH); 39959109Sarchie if (priv->recv.history != NULL) 40059109Sarchie FREE(priv->recv.history, M_NETGRAPH); 40159109Sarchie#endif 40259109Sarchie bzero(priv, sizeof(*priv)); 40359109Sarchie FREE(priv, M_NETGRAPH); 40470784Sjulian NG_NODE_SET_PRIVATE(node, NULL); 40570784Sjulian NG_NODE_UNREF(node); /* let the node escape */ 40659109Sarchie return (0); 40759109Sarchie} 40859109Sarchie 40959109Sarchie/* 41059109Sarchie * Hook disconnection 41159109Sarchie */ 41259109Sarchiestatic int 41359109Sarchieng_mppc_disconnect(hook_p hook) 41459109Sarchie{ 41570784Sjulian const node_p node = NG_HOOK_NODE(hook); 41670784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 41759109Sarchie 41859109Sarchie /* Zero out hook pointer */ 41959109Sarchie if (hook == priv->xmit.hook) 42059109Sarchie priv->xmit.hook = NULL; 42159109Sarchie if (hook == priv->recv.hook) 42259109Sarchie priv->recv.hook = NULL; 42359109Sarchie 42459109Sarchie /* Go away if no longer connected */ 42570784Sjulian if ((NG_NODE_NUMHOOKS(node) == 0) 42670784Sjulian && NG_NODE_IS_VALID(node)) 42770700Sjulian ng_rmnode_self(node); 42859109Sarchie return (0); 42959109Sarchie} 43059109Sarchie 43159109Sarchie/************************************************************************ 43259109Sarchie HELPER STUFF 43359109Sarchie ************************************************************************/ 43459109Sarchie 43559109Sarchie/* 43659109Sarchie * Compress/encrypt a packet and put the result in a new mbuf at *resultp. 43759109Sarchie * The original mbuf is not free'd. 43859109Sarchie */ 43959109Sarchiestatic int 44059109Sarchieng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp) 44159109Sarchie{ 44270784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 44359109Sarchie struct ng_mppc_dir *const d = &priv->xmit; 44459109Sarchie u_char *inbuf, *outbuf; 44559109Sarchie int outlen, inlen; 44659109Sarchie u_int16_t header; 44759109Sarchie 44859109Sarchie /* Initialize */ 44959109Sarchie *resultp = NULL; 45059109Sarchie header = d->cc; 45159109Sarchie if (d->flushed) { 45259109Sarchie header |= MPPC_FLAG_FLUSHED; 45359109Sarchie d->flushed = 0; 45459109Sarchie } 45559109Sarchie 45659109Sarchie /* Work with contiguous regions of memory */ 45759109Sarchie inlen = m->m_pkthdr.len; 45859109Sarchie MALLOC(inbuf, u_char *, inlen, M_NETGRAPH, M_NOWAIT); 45959109Sarchie if (inbuf == NULL) 46059109Sarchie return (ENOMEM); 46159109Sarchie m_copydata(m, 0, inlen, (caddr_t)inbuf); 46259109Sarchie if ((d->cfg.bits & MPPC_BIT) != 0) 46359109Sarchie outlen = MPPC_MAX_BLOWUP(inlen); 46459109Sarchie else 46559109Sarchie outlen = MPPC_HDRLEN + inlen; 46666177Sarchie MALLOC(outbuf, u_char *, outlen, M_NETGRAPH, M_NOWAIT); 46759109Sarchie if (outbuf == NULL) { 46859109Sarchie FREE(inbuf, M_NETGRAPH); 46959109Sarchie return (ENOMEM); 47059109Sarchie } 47159109Sarchie 47259109Sarchie /* Compress "inbuf" into "outbuf" (if compression enabled) */ 47359109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 47459109Sarchie if ((d->cfg.bits & MPPC_BIT) != 0) { 47559109Sarchie u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS; 47659109Sarchie u_char *source, *dest; 47759109Sarchie u_long sourceCnt, destCnt; 47859109Sarchie int rtn; 47959109Sarchie 48059109Sarchie /* Prepare to compress */ 48159109Sarchie source = inbuf; 48259109Sarchie sourceCnt = inlen; 48359109Sarchie dest = outbuf + MPPC_HDRLEN; 48459109Sarchie destCnt = outlen - MPPC_HDRLEN; 48559109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 48659109Sarchie flags |= MPPC_SAVE_HISTORY; 48759109Sarchie 48859109Sarchie /* Compress */ 48959109Sarchie rtn = MPPC_Compress(&source, &dest, &sourceCnt, 49059109Sarchie &destCnt, d->history, flags, 0); 49159109Sarchie 49259109Sarchie /* Check return value */ 49359109Sarchie KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __FUNCTION__)); 49459109Sarchie if ((rtn & MPPC_EXPANDED) == 0 49559109Sarchie && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) { 49659109Sarchie outlen -= destCnt; 49759109Sarchie header |= MPPC_FLAG_COMPRESSED; 49859109Sarchie if ((rtn & MPPC_RESTART_HISTORY) != 0) 49959109Sarchie header |= MPPC_FLAG_RESTART; 50059109Sarchie } 50159109Sarchie d->flushed = (rtn & MPPC_EXPANDED) != 0 50259109Sarchie || (flags & MPPC_SAVE_HISTORY) == 0; 50359109Sarchie } 50459109Sarchie#endif 50559109Sarchie 50659109Sarchie /* If we did not compress this packet, copy it to output buffer */ 50759109Sarchie if ((header & MPPC_FLAG_COMPRESSED) == 0) { 50859109Sarchie bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen); 50959109Sarchie outlen = MPPC_HDRLEN + inlen; 51059109Sarchie } 51159109Sarchie FREE(inbuf, M_NETGRAPH); 51259109Sarchie 51359109Sarchie /* Always set the flushed bit in stateless mode */ 51459109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0) 51559109Sarchie header |= MPPC_FLAG_FLUSHED; 51659109Sarchie 51759109Sarchie /* Now encrypt packet (if encryption enabled) */ 51859109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 51959109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 52059109Sarchie 52159109Sarchie /* Set header bits; need to reset key if we say we did */ 52259109Sarchie header |= MPPC_FLAG_ENCRYPTED; 52359109Sarchie if ((header & MPPC_FLAG_FLUSHED) != 0) 52459109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 52559109Sarchie 52659109Sarchie /* Update key if it's time */ 52759109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0 52859109Sarchie || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { 52959109Sarchie ng_mppc_updatekey(d->cfg.bits, 53059109Sarchie d->cfg.startkey, d->key, &d->rc4); 53159109Sarchie } 53259109Sarchie 53359109Sarchie /* Encrypt packet */ 53459109Sarchie rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN, 53559109Sarchie outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN); 53659109Sarchie } 53759109Sarchie#endif 53859109Sarchie 53959109Sarchie /* Update sequence number */ 54059109Sarchie d->cc++; 54159109Sarchie 54259109Sarchie /* Install header */ 54359109Sarchie *((u_int16_t *)outbuf) = htons(header); 54459109Sarchie 54559109Sarchie /* Return packet in an mbuf */ 54659109Sarchie *resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL); 54759109Sarchie FREE(outbuf, M_NETGRAPH); 54859109Sarchie return (*resultp == NULL ? ENOBUFS : 0); 54959109Sarchie} 55059109Sarchie 55159109Sarchie/* 55259109Sarchie * Decompress/decrypt packet and put the result in a new mbuf at *resultp. 55359109Sarchie * The original mbuf is not free'd. 55459109Sarchie */ 55559109Sarchiestatic int 55659109Sarchieng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp) 55759109Sarchie{ 55870784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 55959109Sarchie struct ng_mppc_dir *const d = &priv->recv; 56059109Sarchie u_int16_t header, cc, numLost; 56159109Sarchie u_char *buf; 56259109Sarchie int len; 56359109Sarchie 56459109Sarchie /* Pull off header */ 56559109Sarchie if (m->m_pkthdr.len < MPPC_HDRLEN) 56659109Sarchie return (EINVAL); 56759109Sarchie m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header); 56859109Sarchie NTOHS(header); 56959109Sarchie cc = (header & MPPC_CCOUNT_MASK); 57059109Sarchie 57159109Sarchie /* Copy payload into a contiguous region of memory */ 57259109Sarchie len = m->m_pkthdr.len - MPPC_HDRLEN; 57359109Sarchie MALLOC(buf, u_char *, len, M_NETGRAPH, M_NOWAIT); 57459109Sarchie if (buf == NULL) 57559109Sarchie return (ENOMEM); 57659109Sarchie m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf); 57759109Sarchie 57859109Sarchie /* Check for insane jumps in sequence numbering (D.O.S. attack) */ 57959109Sarchie numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK); 58059109Sarchie if (numLost >= MPPC_INSANE_JUMP) { 58159109Sarchie log(LOG_ERR, "%s: insane jump %d", __FUNCTION__, numLost); 58259109Sarchie priv->recv.cfg.enable = 0; 58359109Sarchie goto failed; 58459109Sarchie } 58559109Sarchie 58659109Sarchie /* If flushed bit set, we can always handle packet */ 58759109Sarchie if ((header & MPPC_FLAG_FLUSHED) != 0) { 58859109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 58959109Sarchie if (d->history != NULL) 59059109Sarchie MPPC_InitDecompressionHistory(d->history); 59159109Sarchie#endif 59259109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 59359109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 59459109Sarchie 59559109Sarchie /* Resync as necessary, skipping lost packets */ 59659109Sarchie while (d->cc != cc) { 59759109Sarchie if ((d->cfg.bits & MPPE_STATELESS) 59859109Sarchie || (d->cc & MPPE_UPDATE_MASK) 59959109Sarchie == MPPE_UPDATE_FLAG) { 60059109Sarchie ng_mppc_updatekey(d->cfg.bits, 60159109Sarchie d->cfg.startkey, d->key, &d->rc4); 60259109Sarchie } 60359109Sarchie d->cc++; 60459109Sarchie } 60559109Sarchie 60659109Sarchie /* Reset key (except in stateless mode, see below) */ 60759109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 60859109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 60959109Sarchie } 61059109Sarchie#endif 61159109Sarchie d->cc = cc; /* skip over lost seq numbers */ 61259109Sarchie numLost = 0; /* act like no packets were lost */ 61359109Sarchie } 61459109Sarchie 61559109Sarchie /* Can't decode non-sequential packets without a flushed bit */ 61659109Sarchie if (numLost != 0) 61759109Sarchie goto failed; 61859109Sarchie 61959109Sarchie /* Decrypt packet */ 62059109Sarchie if ((header & MPPC_FLAG_ENCRYPTED) != 0) { 62159109Sarchie 62259109Sarchie /* Are we not expecting encryption? */ 62359109Sarchie if ((d->cfg.bits & MPPE_BITS) == 0) { 62459109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 62559109Sarchie __FUNCTION__, "encrypted"); 62659109Sarchie goto failed; 62759109Sarchie } 62859109Sarchie 62959109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 63059109Sarchie /* Update key if it's time (always in stateless mode) */ 63159109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0 63259109Sarchie || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { 63359109Sarchie ng_mppc_updatekey(d->cfg.bits, 63459109Sarchie d->cfg.startkey, d->key, &d->rc4); 63559109Sarchie } 63659109Sarchie 63759109Sarchie /* Decrypt packet */ 63859109Sarchie rc4_crypt(&d->rc4, buf, buf, len); 63959109Sarchie#endif 64059109Sarchie } else { 64159109Sarchie 64259109Sarchie /* Are we expecting encryption? */ 64359109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 64459109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 64559109Sarchie __FUNCTION__, "unencrypted"); 64659109Sarchie goto failed; 64759109Sarchie } 64859109Sarchie } 64959109Sarchie 65059109Sarchie /* Update coherency count for next time (12 bit arithmetic) */ 65159109Sarchie d->cc++; 65259109Sarchie 65359109Sarchie /* Check for unexpected compressed packet */ 65459109Sarchie if ((header & MPPC_FLAG_COMPRESSED) != 0 65559109Sarchie && (d->cfg.bits & MPPC_BIT) == 0) { 65659109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 65759109Sarchie __FUNCTION__, "compressed"); 65859109Sarchiefailed: 65959109Sarchie FREE(buf, M_NETGRAPH); 66059109Sarchie return (EINVAL); 66159109Sarchie } 66259109Sarchie 66359109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 66459109Sarchie /* Decompress packet */ 66559109Sarchie if ((header & MPPC_FLAG_COMPRESSED) != 0) { 66659109Sarchie int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS; 66759109Sarchie u_char *decompbuf, *source, *dest; 66859109Sarchie u_long sourceCnt, destCnt; 66959109Sarchie int decomplen, rtn; 67059109Sarchie 67159109Sarchie /* Allocate a buffer for decompressed data */ 67259109Sarchie MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE 67359109Sarchie + MPPC_DECOMP_SAFETY, M_NETGRAPH, M_NOWAIT); 67459109Sarchie if (decompbuf == NULL) { 67559109Sarchie FREE(buf, M_NETGRAPH); 67659109Sarchie return (ENOMEM); 67759109Sarchie } 67859109Sarchie decomplen = MPPC_DECOMP_BUFSIZE; 67959109Sarchie 68059109Sarchie /* Prepare to decompress */ 68159109Sarchie source = buf; 68259109Sarchie sourceCnt = len; 68359109Sarchie dest = decompbuf; 68459109Sarchie destCnt = decomplen; 68559109Sarchie if ((header & MPPC_FLAG_RESTART) != 0) 68659109Sarchie flags |= MPPC_RESTART_HISTORY; 68759109Sarchie 68859109Sarchie /* Decompress */ 68959109Sarchie rtn = MPPC_Decompress(&source, &dest, 69059109Sarchie &sourceCnt, &destCnt, d->history, flags); 69159109Sarchie 69259109Sarchie /* Check return value */ 69359109Sarchie KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __FUNCTION__)); 69459109Sarchie if ((rtn & MPPC_DEST_EXHAUSTED) != 0 69559109Sarchie || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) { 69659109Sarchie log(LOG_ERR, "%s: decomp returned 0x%x", 69759109Sarchie __FUNCTION__, rtn); 69859109Sarchie FREE(decompbuf, M_NETGRAPH); 69959109Sarchie goto failed; 70059109Sarchie } 70159109Sarchie 70259109Sarchie /* Replace compressed data with decompressed data */ 70359109Sarchie FREE(buf, M_NETGRAPH); 70459109Sarchie buf = decompbuf; 70559109Sarchie len = decomplen - destCnt; 70659109Sarchie } 70759109Sarchie#endif 70859109Sarchie 70959109Sarchie /* Return result in an mbuf */ 71059109Sarchie *resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL); 71159109Sarchie FREE(buf, M_NETGRAPH); 71259109Sarchie return (*resultp == NULL ? ENOBUFS : 0); 71359109Sarchie} 71459109Sarchie 71559109Sarchie/* 71659109Sarchie * The peer has sent us a CCP ResetRequest, so reset our transmit state. 71759109Sarchie */ 71859109Sarchiestatic void 71959109Sarchieng_mppc_reset_req(node_p node) 72059109Sarchie{ 72170784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 72259109Sarchie struct ng_mppc_dir *const d = &priv->xmit; 72359109Sarchie 72459109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 72559109Sarchie if (d->history != NULL) 72659109Sarchie MPPC_InitCompressionHistory(d->history); 72759109Sarchie#endif 72859109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 72959109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 73059109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 73159109Sarchie#endif 73259109Sarchie d->flushed = 1; 73359109Sarchie} 73459109Sarchie 73559109Sarchie/* 73659109Sarchie * Generate a new encryption key 73759109Sarchie */ 73859109Sarchiestatic void 73959109Sarchieng_mppc_getkey(const u_char *h, u_char *h2, int len) 74059109Sarchie{ 74159109Sarchie static const u_char pad1[10] = 74259109Sarchie { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; 74359109Sarchie static const u_char pad2[10] = 74459109Sarchie { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, }; 74559109Sarchie u_char hash[20]; 74659109Sarchie SHA1_CTX c; 74759109Sarchie int k; 74859109Sarchie 74959109Sarchie bzero(&hash, sizeof(hash)); 75059109Sarchie SHA1Init(&c); 75159109Sarchie SHA1Update(&c, h, len); 75259109Sarchie for (k = 0; k < 4; k++) 75359109Sarchie SHA1Update(&c, pad1, sizeof(pad2)); 75459109Sarchie SHA1Update(&c, h2, len); 75559109Sarchie for (k = 0; k < 4; k++) 75659109Sarchie SHA1Update(&c, pad2, sizeof(pad2)); 75759109Sarchie SHA1Final(hash, &c); 75859109Sarchie bcopy(hash, h2, len); 75959109Sarchie} 76059109Sarchie 76159109Sarchie/* 76259109Sarchie * Update the encryption key 76359109Sarchie */ 76459109Sarchiestatic void 76559109Sarchieng_mppc_updatekey(u_int32_t bits, 76659109Sarchie u_char *key0, u_char *key, struct rc4_state *rc4) 76759109Sarchie{ 76859109Sarchie const int keylen = KEYLEN(bits); 76959109Sarchie 77059109Sarchie ng_mppc_getkey(key0, key, keylen); 77159109Sarchie rc4_init(rc4, key, keylen); 77259109Sarchie rc4_crypt(rc4, key, key, keylen); 77359109Sarchie if ((bits & MPPE_128) == 0) 77459109Sarchie bcopy(&ng_mppe_weakenkey, key, sizeof(ng_mppe_weakenkey)); 77559109Sarchie rc4_init(rc4, key, keylen); 77659109Sarchie} 77759109Sarchie 778