ng_mppc.c revision 110409
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 110409 2003-02-05 19:11:11Z ambrisko $ 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 93107845Sarchie/* 94107845Sarchie * When packets are lost with MPPE, we may have to re-key arbitrarily 95107845Sarchie * many times to 'catch up' to the new jumped-ahead sequence number. 96107845Sarchie * Since this can be expensive, we pose a limit on how many re-keyings 97107845Sarchie * we will do at one time to avoid a possible D.O.S. vulnerability. 98107845Sarchie * This should instead be a configurable parameter. 99107845Sarchie */ 100107845Sarchie#define MPPE_MAX_REKEY 1000 10159109Sarchie 10259109Sarchie/* MPPC packet header bits */ 10359109Sarchie#define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */ 10459109Sarchie#define MPPC_FLAG_RESTART 0x4000 /* compress history restart */ 10559109Sarchie#define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */ 10659109Sarchie#define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */ 10759109Sarchie#define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */ 10859109Sarchie 10959109Sarchie#define MPPE_UPDATE_MASK 0xff /* coherency count when we're */ 11059109Sarchie#define MPPE_UPDATE_FLAG 0xff /* supposed to update key */ 11159109Sarchie 11259109Sarchie#define MPPC_COMP_OK 0x05 11359109Sarchie#define MPPC_DECOMP_OK 0x05 11459109Sarchie 11559109Sarchie/* Per direction info */ 11659109Sarchiestruct ng_mppc_dir { 11759109Sarchie struct ng_mppc_config cfg; /* configuration */ 11859109Sarchie hook_p hook; /* netgraph hook */ 11959109Sarchie u_int16_t cc:12; /* coherency count */ 12059109Sarchie u_char flushed; /* clean history (xmit only) */ 12159109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 12259109Sarchie u_char *history; /* compression history */ 12359109Sarchie#endif 12459109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 12559109Sarchie u_char key[MPPE_KEY_LEN]; /* session key */ 12659109Sarchie struct rc4_state rc4; /* rc4 state */ 12759109Sarchie#endif 12859109Sarchie}; 12959109Sarchie 13059109Sarchie/* Node private data */ 13159109Sarchiestruct ng_mppc_private { 13259109Sarchie struct ng_mppc_dir xmit; /* compress/encrypt config */ 13359109Sarchie struct ng_mppc_dir recv; /* decompress/decrypt config */ 13470700Sjulian ng_ID_t ctrlnode; /* path to controlling node */ 13559109Sarchie}; 13659109Sarchietypedef struct ng_mppc_private *priv_p; 13759109Sarchie 13859109Sarchie/* Netgraph node methods */ 13959109Sarchiestatic ng_constructor_t ng_mppc_constructor; 14059109Sarchiestatic ng_rcvmsg_t ng_mppc_rcvmsg; 14170700Sjulianstatic ng_shutdown_t ng_mppc_shutdown; 14259109Sarchiestatic ng_newhook_t ng_mppc_newhook; 14359109Sarchiestatic ng_rcvdata_t ng_mppc_rcvdata; 14459109Sarchiestatic ng_disconnect_t ng_mppc_disconnect; 14559109Sarchie 14659109Sarchie/* Helper functions */ 14759109Sarchiestatic int ng_mppc_compress(node_p node, 14859109Sarchie struct mbuf *m, struct mbuf **resultp); 14959109Sarchiestatic int ng_mppc_decompress(node_p node, 15059109Sarchie struct mbuf *m, struct mbuf **resultp); 15159109Sarchiestatic void ng_mppc_getkey(const u_char *h, u_char *h2, int len); 15259109Sarchiestatic void ng_mppc_updatekey(u_int32_t bits, 15359109Sarchie u_char *key0, u_char *key, struct rc4_state *rc4); 15459109Sarchiestatic void ng_mppc_reset_req(node_p node); 15559109Sarchie 15659109Sarchie/* Node type descriptor */ 15759109Sarchiestatic struct ng_type ng_mppc_typestruct = { 15870159Sjulian NG_ABI_VERSION, 15959109Sarchie NG_MPPC_NODE_TYPE, 16059109Sarchie NULL, 16159109Sarchie ng_mppc_constructor, 16259109Sarchie ng_mppc_rcvmsg, 16370700Sjulian ng_mppc_shutdown, 16459109Sarchie ng_mppc_newhook, 16559109Sarchie NULL, 16659109Sarchie NULL, 16759109Sarchie ng_mppc_rcvdata, 16859109Sarchie ng_mppc_disconnect, 16959109Sarchie NULL 17059109Sarchie}; 17159109SarchieNETGRAPH_INIT(mppc, &ng_mppc_typestruct); 17259109Sarchie 173110409Sambrisko#ifdef NETGRAPH_MPPC_ENCRYPTION 174110409Sambrisko/* Depend on separate rc4 module */ 175110409SambriskoMODULE_DEPEND(ng_mppc, rc4, 1, 1, 1); 176110409Sambrisko#endif 177110409Sambrisko 17887971Sarchie/* Fixed bit pattern to weaken keysize down to 40 or 56 bits */ 17959109Sarchiestatic const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e }; 18059109Sarchie 18159109Sarchie#define ERROUT(x) do { error = (x); goto done; } while (0) 18259109Sarchie 18359109Sarchie/************************************************************************ 18459109Sarchie NETGRAPH NODE STUFF 18559109Sarchie ************************************************************************/ 18659109Sarchie 18759109Sarchie/* 18859109Sarchie * Node type constructor 18959109Sarchie */ 19059109Sarchiestatic int 19170700Sjulianng_mppc_constructor(node_p node) 19259109Sarchie{ 19359109Sarchie priv_p priv; 19459109Sarchie 19559109Sarchie /* Allocate private structure */ 19670870Sjulian MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_MPPC, M_NOWAIT | M_ZERO); 19759109Sarchie if (priv == NULL) 19859109Sarchie return (ENOMEM); 19959109Sarchie 20070784Sjulian NG_NODE_SET_PRIVATE(node, priv); 20159109Sarchie 20259109Sarchie /* Done */ 20359109Sarchie return (0); 20459109Sarchie} 20559109Sarchie 20659109Sarchie/* 20759109Sarchie * Give our OK for a hook to be added 20859109Sarchie */ 20959109Sarchiestatic int 21059109Sarchieng_mppc_newhook(node_p node, hook_p hook, const char *name) 21159109Sarchie{ 21270784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 21359109Sarchie hook_p *hookPtr; 21459109Sarchie 21559109Sarchie /* Check hook name */ 21659109Sarchie if (strcmp(name, NG_MPPC_HOOK_COMP) == 0) 21759109Sarchie hookPtr = &priv->xmit.hook; 21859109Sarchie else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0) 21959109Sarchie hookPtr = &priv->recv.hook; 22059109Sarchie else 22159109Sarchie return (EINVAL); 22259109Sarchie 22359109Sarchie /* See if already connected */ 22459109Sarchie if (*hookPtr != NULL) 22559109Sarchie return (EISCONN); 22659109Sarchie 22759109Sarchie /* OK */ 22859109Sarchie *hookPtr = hook; 22959109Sarchie return (0); 23059109Sarchie} 23159109Sarchie 23259109Sarchie/* 23359109Sarchie * Receive a control message 23459109Sarchie */ 23559109Sarchiestatic int 23670700Sjulianng_mppc_rcvmsg(node_p node, item_p item, hook_p lasthook) 23759109Sarchie{ 23870784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 23959109Sarchie struct ng_mesg *resp = NULL; 24059109Sarchie int error = 0; 24170700Sjulian struct ng_mesg *msg; 24259109Sarchie 24370700Sjulian NGI_GET_MSG(item, msg); 24459109Sarchie switch (msg->header.typecookie) { 24559109Sarchie case NGM_MPPC_COOKIE: 24659109Sarchie switch (msg->header.cmd) { 24759109Sarchie case NGM_MPPC_CONFIG_COMP: 24859109Sarchie case NGM_MPPC_CONFIG_DECOMP: 24959109Sarchie { 25059109Sarchie struct ng_mppc_config *const cfg 25159109Sarchie = (struct ng_mppc_config *)msg->data; 25259109Sarchie const int isComp = 25359109Sarchie msg->header.cmd == NGM_MPPC_CONFIG_COMP; 25459109Sarchie struct ng_mppc_dir *const d = isComp ? 25559109Sarchie &priv->xmit : &priv->recv; 25659109Sarchie 25759109Sarchie /* Check configuration */ 25859109Sarchie if (msg->header.arglen != sizeof(*cfg)) 25959109Sarchie ERROUT(EINVAL); 26059109Sarchie if (cfg->enable) { 26159109Sarchie if ((cfg->bits & ~MPPC_VALID_BITS) != 0) 26259109Sarchie ERROUT(EINVAL); 26359109Sarchie#ifndef NETGRAPH_MPPC_COMPRESSION 26459109Sarchie if ((cfg->bits & MPPC_BIT) != 0) 26559109Sarchie ERROUT(EPROTONOSUPPORT); 26659109Sarchie#endif 26759109Sarchie#ifndef NETGRAPH_MPPC_ENCRYPTION 26859109Sarchie if ((cfg->bits & MPPE_BITS) != 0) 26959109Sarchie ERROUT(EPROTONOSUPPORT); 27059109Sarchie#endif 27159109Sarchie } else 27259109Sarchie cfg->bits = 0; 27359109Sarchie 27459109Sarchie /* Save return address so we can send reset-req's */ 275107845Sarchie if (!isComp) 276107845Sarchie priv->ctrlnode = NGI_RETADDR(item); 27759109Sarchie 27859109Sarchie /* Configuration is OK, reset to it */ 27959109Sarchie d->cfg = *cfg; 28059109Sarchie 28159109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 28259109Sarchie /* Initialize state buffers for compression */ 28359109Sarchie if (d->history != NULL) { 28470870Sjulian FREE(d->history, M_NETGRAPH_MPPC); 28559109Sarchie d->history = NULL; 28659109Sarchie } 28759109Sarchie if ((cfg->bits & MPPC_BIT) != 0) { 28859109Sarchie MALLOC(d->history, u_char *, 28959109Sarchie isComp ? MPPC_SizeOfCompressionHistory() : 29059109Sarchie MPPC_SizeOfDecompressionHistory(), 29170870Sjulian M_NETGRAPH_MPPC, M_NOWAIT); 29259109Sarchie if (d->history == NULL) 29359109Sarchie ERROUT(ENOMEM); 29459109Sarchie if (isComp) 29559109Sarchie MPPC_InitCompressionHistory(d->history); 29659109Sarchie else { 29759109Sarchie MPPC_InitDecompressionHistory( 29859109Sarchie d->history); 29959109Sarchie } 30059109Sarchie } 30159109Sarchie#endif 30259109Sarchie 30359109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 30459109Sarchie /* Generate initial session keys for encryption */ 30559109Sarchie if ((cfg->bits & MPPE_BITS) != 0) { 30659109Sarchie const int keylen = KEYLEN(cfg->bits); 30759109Sarchie 30859109Sarchie bcopy(cfg->startkey, d->key, keylen); 30959109Sarchie ng_mppc_getkey(cfg->startkey, d->key, keylen); 31087971Sarchie if ((cfg->bits & MPPE_40) != 0) 31187971Sarchie bcopy(&ng_mppe_weakenkey, d->key, 3); 31287971Sarchie else if ((cfg->bits & MPPE_56) != 0) 31387971Sarchie bcopy(&ng_mppe_weakenkey, d->key, 1); 31459109Sarchie rc4_init(&d->rc4, d->key, keylen); 31559109Sarchie } 31659109Sarchie#endif 31759109Sarchie 31859109Sarchie /* Initialize other state */ 31959109Sarchie d->cc = 0; 32059109Sarchie d->flushed = 0; 32159109Sarchie break; 32259109Sarchie } 32359109Sarchie 32459109Sarchie case NGM_MPPC_RESETREQ: 32559109Sarchie ng_mppc_reset_req(node); 32659109Sarchie break; 32759109Sarchie 32859109Sarchie default: 32959109Sarchie error = EINVAL; 33059109Sarchie break; 33159109Sarchie } 33259109Sarchie break; 33359109Sarchie default: 33459109Sarchie error = EINVAL; 33559109Sarchie break; 33659109Sarchie } 33759109Sarchiedone: 33870700Sjulian NG_RESPOND_MSG(error, node, item, resp); 33970700Sjulian NG_FREE_MSG(msg); 34059109Sarchie return (error); 34159109Sarchie} 34259109Sarchie 34359109Sarchie/* 34459109Sarchie * Receive incoming data on our hook. 34559109Sarchie */ 34659109Sarchiestatic int 34770700Sjulianng_mppc_rcvdata(hook_p hook, item_p item) 34859109Sarchie{ 34970784Sjulian const node_p node = NG_HOOK_NODE(hook); 35070784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 35159109Sarchie struct mbuf *out; 35259109Sarchie int error; 35370700Sjulian struct mbuf *m; 35459109Sarchie 35570700Sjulian NGI_GET_M(item, m); 35659109Sarchie /* Compress and/or encrypt */ 35759109Sarchie if (hook == priv->xmit.hook) { 35859109Sarchie if (!priv->xmit.cfg.enable) { 35970700Sjulian NG_FREE_M(m); 36070700Sjulian NG_FREE_ITEM(item); 36159109Sarchie return (ENXIO); 36259109Sarchie } 36359109Sarchie if ((error = ng_mppc_compress(node, m, &out)) != 0) { 36470700Sjulian NG_FREE_M(m); 36570700Sjulian NG_FREE_ITEM(item); 36659109Sarchie return(error); 36759109Sarchie } 36870700Sjulian NG_FREE_M(m); 36970700Sjulian NG_FWD_NEW_DATA(error, item, priv->xmit.hook, out); 37059109Sarchie return (error); 37159109Sarchie } 37259109Sarchie 37359109Sarchie /* Decompress and/or decrypt */ 37459109Sarchie if (hook == priv->recv.hook) { 37559109Sarchie if (!priv->recv.cfg.enable) { 37670700Sjulian NG_FREE_M(m); 37770700Sjulian NG_FREE_ITEM(item); 37859109Sarchie return (ENXIO); 37959109Sarchie } 38059109Sarchie if ((error = ng_mppc_decompress(node, m, &out)) != 0) { 38170700Sjulian NG_FREE_M(m); 38270700Sjulian NG_FREE_ITEM(item); 383102244Sarchie if (error == EINVAL && priv->ctrlnode != 0) { 38459109Sarchie struct ng_mesg *msg; 38559109Sarchie 38659109Sarchie /* Need to send a reset-request */ 38759109Sarchie NG_MKMESSAGE(msg, NGM_MPPC_COOKIE, 38859109Sarchie NGM_MPPC_RESETREQ, 0, M_NOWAIT); 38959109Sarchie if (msg == NULL) 39059109Sarchie return (error); 39170700Sjulian NG_SEND_MSG_ID(error, node, msg, 392102244Sarchie priv->ctrlnode, 0); 39359109Sarchie } 39459109Sarchie return (error); 39559109Sarchie } 39670700Sjulian NG_FREE_M(m); 39770700Sjulian NG_FWD_NEW_DATA(error, item, priv->recv.hook, out); 39859109Sarchie return (error); 39959109Sarchie } 40059109Sarchie 40159109Sarchie /* Oops */ 40287599Sobrien panic("%s: unknown hook", __func__); 40383366Sjulian#ifdef RESTARTABLE_PANICS 40483366Sjulian return (EINVAL); 40583366Sjulian#endif 40659109Sarchie} 40759109Sarchie 40859109Sarchie/* 40959109Sarchie * Destroy node 41059109Sarchie */ 41159109Sarchiestatic int 41270700Sjulianng_mppc_shutdown(node_p node) 41359109Sarchie{ 41470784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 41559109Sarchie 41659109Sarchie /* Take down netgraph node */ 41759109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 41859109Sarchie if (priv->xmit.history != NULL) 41970870Sjulian FREE(priv->xmit.history, M_NETGRAPH_MPPC); 42059109Sarchie if (priv->recv.history != NULL) 42170870Sjulian FREE(priv->recv.history, M_NETGRAPH_MPPC); 42259109Sarchie#endif 42359109Sarchie bzero(priv, sizeof(*priv)); 42470870Sjulian FREE(priv, M_NETGRAPH_MPPC); 42570784Sjulian NG_NODE_SET_PRIVATE(node, NULL); 42670784Sjulian NG_NODE_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{ 43670784Sjulian const node_p node = NG_HOOK_NODE(hook); 43770784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 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 */ 44670784Sjulian if ((NG_NODE_NUMHOOKS(node) == 0) 44770784Sjulian && NG_NODE_IS_VALID(node)) 44870700Sjulian ng_rmnode_self(node); 44959109Sarchie return (0); 45059109Sarchie} 45159109Sarchie 45259109Sarchie/************************************************************************ 45359109Sarchie HELPER STUFF 45459109Sarchie ************************************************************************/ 45559109Sarchie 45659109Sarchie/* 45759109Sarchie * Compress/encrypt a packet and put the result in a new mbuf at *resultp. 45859109Sarchie * The original mbuf is not free'd. 45959109Sarchie */ 46059109Sarchiestatic int 46159109Sarchieng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp) 46259109Sarchie{ 46370784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 46459109Sarchie struct ng_mppc_dir *const d = &priv->xmit; 46559109Sarchie u_char *inbuf, *outbuf; 46659109Sarchie int outlen, inlen; 46759109Sarchie u_int16_t header; 46859109Sarchie 46959109Sarchie /* Initialize */ 47059109Sarchie *resultp = NULL; 47159109Sarchie header = d->cc; 47259109Sarchie if (d->flushed) { 47359109Sarchie header |= MPPC_FLAG_FLUSHED; 47459109Sarchie d->flushed = 0; 47559109Sarchie } 47659109Sarchie 47759109Sarchie /* Work with contiguous regions of memory */ 47859109Sarchie inlen = m->m_pkthdr.len; 47970870Sjulian MALLOC(inbuf, u_char *, inlen, M_NETGRAPH_MPPC, M_NOWAIT); 48059109Sarchie if (inbuf == NULL) 48159109Sarchie return (ENOMEM); 48259109Sarchie m_copydata(m, 0, inlen, (caddr_t)inbuf); 48359109Sarchie if ((d->cfg.bits & MPPC_BIT) != 0) 48459109Sarchie outlen = MPPC_MAX_BLOWUP(inlen); 48559109Sarchie else 48659109Sarchie outlen = MPPC_HDRLEN + inlen; 48770870Sjulian MALLOC(outbuf, u_char *, outlen, M_NETGRAPH_MPPC, M_NOWAIT); 48859109Sarchie if (outbuf == NULL) { 48970870Sjulian FREE(inbuf, M_NETGRAPH_MPPC); 49059109Sarchie return (ENOMEM); 49159109Sarchie } 49259109Sarchie 49359109Sarchie /* Compress "inbuf" into "outbuf" (if compression enabled) */ 49459109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 49559109Sarchie if ((d->cfg.bits & MPPC_BIT) != 0) { 49659109Sarchie u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS; 49759109Sarchie u_char *source, *dest; 49859109Sarchie u_long sourceCnt, destCnt; 49959109Sarchie int rtn; 50059109Sarchie 50159109Sarchie /* Prepare to compress */ 50259109Sarchie source = inbuf; 50359109Sarchie sourceCnt = inlen; 50459109Sarchie dest = outbuf + MPPC_HDRLEN; 50559109Sarchie destCnt = outlen - MPPC_HDRLEN; 50659109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 50759109Sarchie flags |= MPPC_SAVE_HISTORY; 50859109Sarchie 50959109Sarchie /* Compress */ 51059109Sarchie rtn = MPPC_Compress(&source, &dest, &sourceCnt, 51159109Sarchie &destCnt, d->history, flags, 0); 51259109Sarchie 51359109Sarchie /* Check return value */ 51487599Sobrien KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__)); 51559109Sarchie if ((rtn & MPPC_EXPANDED) == 0 51659109Sarchie && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) { 51759109Sarchie outlen -= destCnt; 51859109Sarchie header |= MPPC_FLAG_COMPRESSED; 51959109Sarchie if ((rtn & MPPC_RESTART_HISTORY) != 0) 52059109Sarchie header |= MPPC_FLAG_RESTART; 52159109Sarchie } 52259109Sarchie d->flushed = (rtn & MPPC_EXPANDED) != 0 52359109Sarchie || (flags & MPPC_SAVE_HISTORY) == 0; 52459109Sarchie } 52559109Sarchie#endif 52659109Sarchie 52759109Sarchie /* If we did not compress this packet, copy it to output buffer */ 52859109Sarchie if ((header & MPPC_FLAG_COMPRESSED) == 0) { 52959109Sarchie bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen); 53059109Sarchie outlen = MPPC_HDRLEN + inlen; 53159109Sarchie } 53270870Sjulian FREE(inbuf, M_NETGRAPH_MPPC); 53359109Sarchie 53459109Sarchie /* Always set the flushed bit in stateless mode */ 53559109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0) 53659109Sarchie header |= MPPC_FLAG_FLUSHED; 53759109Sarchie 53859109Sarchie /* Now encrypt packet (if encryption enabled) */ 53959109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 54059109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 54159109Sarchie 54259109Sarchie /* Set header bits; need to reset key if we say we did */ 54359109Sarchie header |= MPPC_FLAG_ENCRYPTED; 54459109Sarchie if ((header & MPPC_FLAG_FLUSHED) != 0) 54559109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 54659109Sarchie 54759109Sarchie /* Update key if it's time */ 54859109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0 54959109Sarchie || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { 55059109Sarchie ng_mppc_updatekey(d->cfg.bits, 55159109Sarchie d->cfg.startkey, d->key, &d->rc4); 55259109Sarchie } 55359109Sarchie 55459109Sarchie /* Encrypt packet */ 55559109Sarchie rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN, 55659109Sarchie outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN); 55759109Sarchie } 55859109Sarchie#endif 55959109Sarchie 56059109Sarchie /* Update sequence number */ 56159109Sarchie d->cc++; 56259109Sarchie 56359109Sarchie /* Install header */ 56459109Sarchie *((u_int16_t *)outbuf) = htons(header); 56559109Sarchie 56659109Sarchie /* Return packet in an mbuf */ 56759109Sarchie *resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL); 56870870Sjulian FREE(outbuf, M_NETGRAPH_MPPC); 56959109Sarchie return (*resultp == NULL ? ENOBUFS : 0); 57059109Sarchie} 57159109Sarchie 57259109Sarchie/* 57359109Sarchie * Decompress/decrypt packet and put the result in a new mbuf at *resultp. 57459109Sarchie * The original mbuf is not free'd. 57559109Sarchie */ 57659109Sarchiestatic int 57759109Sarchieng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp) 57859109Sarchie{ 57970784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 58059109Sarchie struct ng_mppc_dir *const d = &priv->recv; 581107845Sarchie u_int16_t header, cc; 582107845Sarchie u_int numLost; 58359109Sarchie u_char *buf; 58459109Sarchie int len; 58559109Sarchie 58659109Sarchie /* Pull off header */ 58759109Sarchie if (m->m_pkthdr.len < MPPC_HDRLEN) 58859109Sarchie return (EINVAL); 58959109Sarchie m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header); 59090868Smike header = ntohs(header); 59159109Sarchie cc = (header & MPPC_CCOUNT_MASK); 59259109Sarchie 59359109Sarchie /* Copy payload into a contiguous region of memory */ 59459109Sarchie len = m->m_pkthdr.len - MPPC_HDRLEN; 59570870Sjulian MALLOC(buf, u_char *, len, M_NETGRAPH_MPPC, M_NOWAIT); 59659109Sarchie if (buf == NULL) 59759109Sarchie return (ENOMEM); 59859109Sarchie m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf); 59959109Sarchie 600107845Sarchie /* Check for an unexpected jump in the sequence number */ 60159109Sarchie numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK); 60259109Sarchie 60359109Sarchie /* If flushed bit set, we can always handle packet */ 60459109Sarchie if ((header & MPPC_FLAG_FLUSHED) != 0) { 60559109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 60659109Sarchie if (d->history != NULL) 60759109Sarchie MPPC_InitDecompressionHistory(d->history); 60859109Sarchie#endif 60959109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 61059109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 611107845Sarchie u_int rekey; 61259109Sarchie 613107845Sarchie /* How many times are we going to have to re-key? */ 614107845Sarchie rekey = ((d->cfg.bits & MPPE_STATELESS) != 0) ? 615107845Sarchie numLost : (numLost / (MPPE_UPDATE_MASK + 1)); 616107845Sarchie if (rekey > MPPE_MAX_REKEY) { 617107845Sarchie log(LOG_ERR, "%s: too many (%d) packets" 618107845Sarchie " dropped, disabling node %p!", 619107845Sarchie __func__, numLost, node); 620107845Sarchie priv->recv.cfg.enable = 0; 621107845Sarchie goto failed; 622107845Sarchie } 623107845Sarchie 624107845Sarchie /* Re-key as necessary to catch up to peer */ 62559109Sarchie while (d->cc != cc) { 626107845Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0 62759109Sarchie || (d->cc & MPPE_UPDATE_MASK) 62859109Sarchie == MPPE_UPDATE_FLAG) { 62959109Sarchie ng_mppc_updatekey(d->cfg.bits, 63059109Sarchie d->cfg.startkey, d->key, &d->rc4); 63159109Sarchie } 63259109Sarchie d->cc++; 63359109Sarchie } 63459109Sarchie 63559109Sarchie /* Reset key (except in stateless mode, see below) */ 63659109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 63759109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 63859109Sarchie } 63959109Sarchie#endif 64059109Sarchie d->cc = cc; /* skip over lost seq numbers */ 64159109Sarchie numLost = 0; /* act like no packets were lost */ 64259109Sarchie } 64359109Sarchie 64459109Sarchie /* Can't decode non-sequential packets without a flushed bit */ 64559109Sarchie if (numLost != 0) 64659109Sarchie goto failed; 64759109Sarchie 64859109Sarchie /* Decrypt packet */ 64959109Sarchie if ((header & MPPC_FLAG_ENCRYPTED) != 0) { 65059109Sarchie 65159109Sarchie /* Are we not expecting encryption? */ 65259109Sarchie if ((d->cfg.bits & MPPE_BITS) == 0) { 65359109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 65487599Sobrien __func__, "encrypted"); 65559109Sarchie goto failed; 65659109Sarchie } 65759109Sarchie 65859109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 65959109Sarchie /* Update key if it's time (always in stateless mode) */ 66059109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0 66159109Sarchie || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { 66259109Sarchie ng_mppc_updatekey(d->cfg.bits, 66359109Sarchie d->cfg.startkey, d->key, &d->rc4); 66459109Sarchie } 66559109Sarchie 66659109Sarchie /* Decrypt packet */ 66759109Sarchie rc4_crypt(&d->rc4, buf, buf, len); 66859109Sarchie#endif 66959109Sarchie } else { 67059109Sarchie 67159109Sarchie /* Are we expecting encryption? */ 67259109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 67359109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 67487599Sobrien __func__, "unencrypted"); 67559109Sarchie goto failed; 67659109Sarchie } 67759109Sarchie } 67859109Sarchie 67959109Sarchie /* Update coherency count for next time (12 bit arithmetic) */ 68059109Sarchie d->cc++; 68159109Sarchie 68259109Sarchie /* Check for unexpected compressed packet */ 68359109Sarchie if ((header & MPPC_FLAG_COMPRESSED) != 0 68459109Sarchie && (d->cfg.bits & MPPC_BIT) == 0) { 68559109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 68687599Sobrien __func__, "compressed"); 68759109Sarchiefailed: 68870870Sjulian FREE(buf, M_NETGRAPH_MPPC); 68959109Sarchie return (EINVAL); 69059109Sarchie } 69159109Sarchie 69259109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 69359109Sarchie /* Decompress packet */ 69459109Sarchie if ((header & MPPC_FLAG_COMPRESSED) != 0) { 69559109Sarchie int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS; 69659109Sarchie u_char *decompbuf, *source, *dest; 69759109Sarchie u_long sourceCnt, destCnt; 69859109Sarchie int decomplen, rtn; 69959109Sarchie 70059109Sarchie /* Allocate a buffer for decompressed data */ 70159109Sarchie MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE 70270870Sjulian + MPPC_DECOMP_SAFETY, M_NETGRAPH_MPPC, M_NOWAIT); 70359109Sarchie if (decompbuf == NULL) { 70470870Sjulian FREE(buf, M_NETGRAPH_MPPC); 70559109Sarchie return (ENOMEM); 70659109Sarchie } 70759109Sarchie decomplen = MPPC_DECOMP_BUFSIZE; 70859109Sarchie 70959109Sarchie /* Prepare to decompress */ 71059109Sarchie source = buf; 71159109Sarchie sourceCnt = len; 71259109Sarchie dest = decompbuf; 71359109Sarchie destCnt = decomplen; 71459109Sarchie if ((header & MPPC_FLAG_RESTART) != 0) 71559109Sarchie flags |= MPPC_RESTART_HISTORY; 71659109Sarchie 71759109Sarchie /* Decompress */ 71859109Sarchie rtn = MPPC_Decompress(&source, &dest, 71959109Sarchie &sourceCnt, &destCnt, d->history, flags); 72059109Sarchie 72159109Sarchie /* Check return value */ 72287599Sobrien KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__)); 72359109Sarchie if ((rtn & MPPC_DEST_EXHAUSTED) != 0 72459109Sarchie || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) { 72559109Sarchie log(LOG_ERR, "%s: decomp returned 0x%x", 72687599Sobrien __func__, rtn); 72770870Sjulian FREE(decompbuf, M_NETGRAPH_MPPC); 72859109Sarchie goto failed; 72959109Sarchie } 73059109Sarchie 73159109Sarchie /* Replace compressed data with decompressed data */ 73270870Sjulian FREE(buf, M_NETGRAPH_MPPC); 73359109Sarchie buf = decompbuf; 73459109Sarchie len = decomplen - destCnt; 73559109Sarchie } 73659109Sarchie#endif 73759109Sarchie 73859109Sarchie /* Return result in an mbuf */ 73959109Sarchie *resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL); 74070870Sjulian FREE(buf, M_NETGRAPH_MPPC); 74159109Sarchie return (*resultp == NULL ? ENOBUFS : 0); 74259109Sarchie} 74359109Sarchie 74459109Sarchie/* 74559109Sarchie * The peer has sent us a CCP ResetRequest, so reset our transmit state. 74659109Sarchie */ 74759109Sarchiestatic void 74859109Sarchieng_mppc_reset_req(node_p node) 74959109Sarchie{ 75070784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 75159109Sarchie struct ng_mppc_dir *const d = &priv->xmit; 75259109Sarchie 75359109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 75459109Sarchie if (d->history != NULL) 75559109Sarchie MPPC_InitCompressionHistory(d->history); 75659109Sarchie#endif 75759109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 75859109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 75959109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 76059109Sarchie#endif 76159109Sarchie d->flushed = 1; 76259109Sarchie} 76359109Sarchie 76459109Sarchie/* 76559109Sarchie * Generate a new encryption key 76659109Sarchie */ 76759109Sarchiestatic void 76859109Sarchieng_mppc_getkey(const u_char *h, u_char *h2, int len) 76959109Sarchie{ 77059109Sarchie static const u_char pad1[10] = 77159109Sarchie { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; 77259109Sarchie static const u_char pad2[10] = 77359109Sarchie { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, }; 77459109Sarchie u_char hash[20]; 77559109Sarchie SHA1_CTX c; 77659109Sarchie int k; 77759109Sarchie 77859109Sarchie bzero(&hash, sizeof(hash)); 77959109Sarchie SHA1Init(&c); 78059109Sarchie SHA1Update(&c, h, len); 78159109Sarchie for (k = 0; k < 4; k++) 78259109Sarchie SHA1Update(&c, pad1, sizeof(pad2)); 78359109Sarchie SHA1Update(&c, h2, len); 78459109Sarchie for (k = 0; k < 4; k++) 78559109Sarchie SHA1Update(&c, pad2, sizeof(pad2)); 78659109Sarchie SHA1Final(hash, &c); 78759109Sarchie bcopy(hash, h2, len); 78859109Sarchie} 78959109Sarchie 79059109Sarchie/* 79159109Sarchie * Update the encryption key 79259109Sarchie */ 79359109Sarchiestatic void 79459109Sarchieng_mppc_updatekey(u_int32_t bits, 79559109Sarchie u_char *key0, u_char *key, struct rc4_state *rc4) 79659109Sarchie{ 79759109Sarchie const int keylen = KEYLEN(bits); 79859109Sarchie 79959109Sarchie ng_mppc_getkey(key0, key, keylen); 80059109Sarchie rc4_init(rc4, key, keylen); 80159109Sarchie rc4_crypt(rc4, key, key, keylen); 80287971Sarchie if ((bits & MPPE_40) != 0) 80387971Sarchie bcopy(&ng_mppe_weakenkey, key, 3); 80487971Sarchie else if ((bits & MPPE_56) != 0) 80587971Sarchie bcopy(&ng_mppe_weakenkey, key, 1); 80659109Sarchie rc4_init(rc4, key, keylen); 80759109Sarchie} 80859109Sarchie 809