ng_mppc.c revision 169263
159109Sarchie/* 259109Sarchie * ng_mppc.c 3139823Simp */ 4139823Simp 5139823Simp/*- 659109Sarchie * Copyright (c) 1996-2000 Whistle Communications, Inc. 759109Sarchie * All rights reserved. 859109Sarchie * 959109Sarchie * Subject to the following obligations and disclaimer of warranty, use and 1059109Sarchie * redistribution of this software, in source or object code forms, with or 1159109Sarchie * without modifications are expressly permitted by Whistle Communications; 1259109Sarchie * provided, however, that: 1359109Sarchie * 1. Any and all reproductions of the source or object code must include the 1459109Sarchie * copyright notice above and the following disclaimer of warranties; and 1559109Sarchie * 2. No rights are granted, in any manner or form, to use Whistle 1659109Sarchie * Communications, Inc. trademarks, including the mark "WHISTLE 1759109Sarchie * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1859109Sarchie * such appears in the above copyright notice or in the software. 1959109Sarchie * 2059109Sarchie * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 2159109Sarchie * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2259109Sarchie * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2359109Sarchie * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2459109Sarchie * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2559109Sarchie * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2659109Sarchie * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2759109Sarchie * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2859109Sarchie * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 2959109Sarchie * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 3059109Sarchie * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 3159109Sarchie * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3259109Sarchie * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 3359109Sarchie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3459109Sarchie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3559109Sarchie * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3659109Sarchie * OF SUCH DAMAGE. 3759109Sarchie * 3867506Sjulian * Author: Archie Cobbs <archie@freebsd.org> 3959109Sarchie * 4059109Sarchie * $Whistle: ng_mppc.c,v 1.4 1999/11/25 00:10:12 archie Exp $ 4159109Sarchie * $FreeBSD: head/sys/netgraph/ng_mppc.c 169263 2007-05-04 16:20:47Z mav $ 4259109Sarchie */ 4359109Sarchie 4459109Sarchie/* 4559109Sarchie * Microsoft PPP compression (MPPC) and encryption (MPPE) netgraph node type. 4659109Sarchie * 4759109Sarchie * You must define one or both of the NETGRAPH_MPPC_COMPRESSION and/or 4859109Sarchie * NETGRAPH_MPPC_ENCRYPTION options for this node type to be useful. 4959109Sarchie */ 5059109Sarchie 5159109Sarchie#include <sys/param.h> 5259109Sarchie#include <sys/systm.h> 5359109Sarchie#include <sys/kernel.h> 5459109Sarchie#include <sys/mbuf.h> 5559109Sarchie#include <sys/malloc.h> 5659109Sarchie#include <sys/errno.h> 5759109Sarchie#include <sys/syslog.h> 5859109Sarchie 5959109Sarchie#include <netgraph/ng_message.h> 6059109Sarchie#include <netgraph/netgraph.h> 6159109Sarchie#include <netgraph/ng_mppc.h> 6259109Sarchie 6359109Sarchie#include "opt_netgraph.h" 6459109Sarchie 6559109Sarchie#if !defined(NETGRAPH_MPPC_COMPRESSION) && !defined(NETGRAPH_MPPC_ENCRYPTION) 66151349Syar#ifdef KLD_MODULE 67151349Syar/* XXX NETGRAPH_MPPC_COMPRESSION isn't functional yet */ 68151349Syar#define NETGRAPH_MPPC_ENCRYPTION 69151349Syar#else 70151349Syar/* This case is indicative of an error in sys/conf files */ 7159109Sarchie#error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION 7259109Sarchie#endif 73151349Syar#endif 7459109Sarchie 7570870Sjulian#ifdef NG_SEPARATE_MALLOC 7670870SjulianMALLOC_DEFINE(M_NETGRAPH_MPPC, "netgraph_mppc", "netgraph mppc node "); 7770870Sjulian#else 7870870Sjulian#define M_NETGRAPH_MPPC M_NETGRAPH 7970870Sjulian#endif 8070870Sjulian 8159109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 8259109Sarchie/* XXX this file doesn't exist yet, but hopefully someday it will... */ 8359109Sarchie#include <net/mppc.h> 8459109Sarchie#endif 8559109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 8659109Sarchie#include <crypto/rc4/rc4.h> 8759109Sarchie#endif 8859109Sarchie#include <crypto/sha1.h> 8959109Sarchie 9059109Sarchie/* Decompression blowup */ 9159109Sarchie#define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */ 9259109Sarchie#define MPPC_DECOMP_SAFETY 100 /* plus this much margin */ 9359109Sarchie 9459109Sarchie/* MPPC/MPPE header length */ 9559109Sarchie#define MPPC_HDRLEN 2 9659109Sarchie 9759109Sarchie/* Key length */ 9859109Sarchie#define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8) 9959109Sarchie 100107845Sarchie/* 101107845Sarchie * When packets are lost with MPPE, we may have to re-key arbitrarily 102107845Sarchie * many times to 'catch up' to the new jumped-ahead sequence number. 103107845Sarchie * Since this can be expensive, we pose a limit on how many re-keyings 104107845Sarchie * we will do at one time to avoid a possible D.O.S. vulnerability. 105107845Sarchie * This should instead be a configurable parameter. 106107845Sarchie */ 107107845Sarchie#define MPPE_MAX_REKEY 1000 10859109Sarchie 10959109Sarchie/* MPPC packet header bits */ 11059109Sarchie#define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */ 11159109Sarchie#define MPPC_FLAG_RESTART 0x4000 /* compress history restart */ 11259109Sarchie#define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */ 11359109Sarchie#define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */ 11459109Sarchie#define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */ 11559109Sarchie 116169261Smav#define MPPC_CCOUNT_INC(d) ((d) = (((d) + 1) & MPPC_CCOUNT_MASK)) 117169261Smav 11859109Sarchie#define MPPE_UPDATE_MASK 0xff /* coherency count when we're */ 11959109Sarchie#define MPPE_UPDATE_FLAG 0xff /* supposed to update key */ 12059109Sarchie 12159109Sarchie#define MPPC_COMP_OK 0x05 12259109Sarchie#define MPPC_DECOMP_OK 0x05 12359109Sarchie 12459109Sarchie/* Per direction info */ 12559109Sarchiestruct ng_mppc_dir { 12659109Sarchie struct ng_mppc_config cfg; /* configuration */ 12759109Sarchie hook_p hook; /* netgraph hook */ 12859109Sarchie u_int16_t cc:12; /* coherency count */ 12959109Sarchie u_char flushed; /* clean history (xmit only) */ 13059109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 13159109Sarchie u_char *history; /* compression history */ 13259109Sarchie#endif 13359109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 13459109Sarchie u_char key[MPPE_KEY_LEN]; /* session key */ 13559109Sarchie struct rc4_state rc4; /* rc4 state */ 13659109Sarchie#endif 13759109Sarchie}; 13859109Sarchie 13959109Sarchie/* Node private data */ 14059109Sarchiestruct ng_mppc_private { 14159109Sarchie struct ng_mppc_dir xmit; /* compress/encrypt config */ 14259109Sarchie struct ng_mppc_dir recv; /* decompress/decrypt config */ 14370700Sjulian ng_ID_t ctrlnode; /* path to controlling node */ 14459109Sarchie}; 14559109Sarchietypedef struct ng_mppc_private *priv_p; 14659109Sarchie 14759109Sarchie/* Netgraph node methods */ 14859109Sarchiestatic ng_constructor_t ng_mppc_constructor; 14959109Sarchiestatic ng_rcvmsg_t ng_mppc_rcvmsg; 15070700Sjulianstatic ng_shutdown_t ng_mppc_shutdown; 15159109Sarchiestatic ng_newhook_t ng_mppc_newhook; 15259109Sarchiestatic ng_rcvdata_t ng_mppc_rcvdata; 15359109Sarchiestatic ng_disconnect_t ng_mppc_disconnect; 15459109Sarchie 15559109Sarchie/* Helper functions */ 15659109Sarchiestatic int ng_mppc_compress(node_p node, 15759109Sarchie struct mbuf *m, struct mbuf **resultp); 15859109Sarchiestatic int ng_mppc_decompress(node_p node, 15959109Sarchie struct mbuf *m, struct mbuf **resultp); 16059109Sarchiestatic void ng_mppc_getkey(const u_char *h, u_char *h2, int len); 16159109Sarchiestatic void ng_mppc_updatekey(u_int32_t bits, 16259109Sarchie u_char *key0, u_char *key, struct rc4_state *rc4); 16359109Sarchiestatic void ng_mppc_reset_req(node_p node); 16459109Sarchie 16559109Sarchie/* Node type descriptor */ 16659109Sarchiestatic struct ng_type ng_mppc_typestruct = { 167129823Sjulian .version = NG_ABI_VERSION, 168129823Sjulian .name = NG_MPPC_NODE_TYPE, 169129823Sjulian .constructor = ng_mppc_constructor, 170129823Sjulian .rcvmsg = ng_mppc_rcvmsg, 171129823Sjulian .shutdown = ng_mppc_shutdown, 172129823Sjulian .newhook = ng_mppc_newhook, 173129823Sjulian .rcvdata = ng_mppc_rcvdata, 174129823Sjulian .disconnect = ng_mppc_disconnect, 17559109Sarchie}; 17659109SarchieNETGRAPH_INIT(mppc, &ng_mppc_typestruct); 17759109Sarchie 178110409Sambrisko#ifdef NETGRAPH_MPPC_ENCRYPTION 179110409Sambrisko/* Depend on separate rc4 module */ 180110409SambriskoMODULE_DEPEND(ng_mppc, rc4, 1, 1, 1); 181110409Sambrisko#endif 182110409Sambrisko 18387971Sarchie/* Fixed bit pattern to weaken keysize down to 40 or 56 bits */ 18459109Sarchiestatic const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e }; 18559109Sarchie 18659109Sarchie#define ERROUT(x) do { error = (x); goto done; } while (0) 18759109Sarchie 18859109Sarchie/************************************************************************ 18959109Sarchie NETGRAPH NODE STUFF 19059109Sarchie ************************************************************************/ 19159109Sarchie 19259109Sarchie/* 19359109Sarchie * Node type constructor 19459109Sarchie */ 19559109Sarchiestatic int 19670700Sjulianng_mppc_constructor(node_p node) 19759109Sarchie{ 19859109Sarchie priv_p priv; 19959109Sarchie 20059109Sarchie /* Allocate private structure */ 20170870Sjulian MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_MPPC, M_NOWAIT | M_ZERO); 20259109Sarchie if (priv == NULL) 20359109Sarchie return (ENOMEM); 20459109Sarchie 20570784Sjulian NG_NODE_SET_PRIVATE(node, priv); 20659109Sarchie 207146919Sglebius /* This node is not thread safe. */ 208146919Sglebius NG_NODE_FORCE_WRITER(node); 209146919Sglebius 21059109Sarchie /* Done */ 21159109Sarchie return (0); 21259109Sarchie} 21359109Sarchie 21459109Sarchie/* 21559109Sarchie * Give our OK for a hook to be added 21659109Sarchie */ 21759109Sarchiestatic int 21859109Sarchieng_mppc_newhook(node_p node, hook_p hook, const char *name) 21959109Sarchie{ 22070784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 22159109Sarchie hook_p *hookPtr; 22259109Sarchie 22359109Sarchie /* Check hook name */ 22459109Sarchie if (strcmp(name, NG_MPPC_HOOK_COMP) == 0) 22559109Sarchie hookPtr = &priv->xmit.hook; 22659109Sarchie else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0) 22759109Sarchie hookPtr = &priv->recv.hook; 22859109Sarchie else 22959109Sarchie return (EINVAL); 23059109Sarchie 23159109Sarchie /* See if already connected */ 23259109Sarchie if (*hookPtr != NULL) 23359109Sarchie return (EISCONN); 23459109Sarchie 23559109Sarchie /* OK */ 23659109Sarchie *hookPtr = hook; 23759109Sarchie return (0); 23859109Sarchie} 23959109Sarchie 24059109Sarchie/* 24159109Sarchie * Receive a control message 24259109Sarchie */ 24359109Sarchiestatic int 24470700Sjulianng_mppc_rcvmsg(node_p node, item_p item, hook_p lasthook) 24559109Sarchie{ 24670784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 24759109Sarchie struct ng_mesg *resp = NULL; 24859109Sarchie int error = 0; 24970700Sjulian struct ng_mesg *msg; 25059109Sarchie 25170700Sjulian NGI_GET_MSG(item, msg); 25259109Sarchie switch (msg->header.typecookie) { 25359109Sarchie case NGM_MPPC_COOKIE: 25459109Sarchie switch (msg->header.cmd) { 25559109Sarchie case NGM_MPPC_CONFIG_COMP: 25659109Sarchie case NGM_MPPC_CONFIG_DECOMP: 25759109Sarchie { 25859109Sarchie struct ng_mppc_config *const cfg 25959109Sarchie = (struct ng_mppc_config *)msg->data; 26059109Sarchie const int isComp = 26159109Sarchie msg->header.cmd == NGM_MPPC_CONFIG_COMP; 26259109Sarchie struct ng_mppc_dir *const d = isComp ? 26359109Sarchie &priv->xmit : &priv->recv; 26459109Sarchie 26559109Sarchie /* Check configuration */ 26659109Sarchie if (msg->header.arglen != sizeof(*cfg)) 26759109Sarchie ERROUT(EINVAL); 26859109Sarchie if (cfg->enable) { 26959109Sarchie if ((cfg->bits & ~MPPC_VALID_BITS) != 0) 27059109Sarchie ERROUT(EINVAL); 27159109Sarchie#ifndef NETGRAPH_MPPC_COMPRESSION 27259109Sarchie if ((cfg->bits & MPPC_BIT) != 0) 27359109Sarchie ERROUT(EPROTONOSUPPORT); 27459109Sarchie#endif 27559109Sarchie#ifndef NETGRAPH_MPPC_ENCRYPTION 27659109Sarchie if ((cfg->bits & MPPE_BITS) != 0) 27759109Sarchie ERROUT(EPROTONOSUPPORT); 27859109Sarchie#endif 27959109Sarchie } else 28059109Sarchie cfg->bits = 0; 28159109Sarchie 28259109Sarchie /* Save return address so we can send reset-req's */ 283107845Sarchie if (!isComp) 284107845Sarchie priv->ctrlnode = NGI_RETADDR(item); 28559109Sarchie 28659109Sarchie /* Configuration is OK, reset to it */ 28759109Sarchie d->cfg = *cfg; 28859109Sarchie 28959109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 29059109Sarchie /* Initialize state buffers for compression */ 29159109Sarchie if (d->history != NULL) { 29270870Sjulian FREE(d->history, M_NETGRAPH_MPPC); 29359109Sarchie d->history = NULL; 29459109Sarchie } 29559109Sarchie if ((cfg->bits & MPPC_BIT) != 0) { 29659109Sarchie MALLOC(d->history, u_char *, 29759109Sarchie isComp ? MPPC_SizeOfCompressionHistory() : 29859109Sarchie MPPC_SizeOfDecompressionHistory(), 29970870Sjulian M_NETGRAPH_MPPC, M_NOWAIT); 30059109Sarchie if (d->history == NULL) 30159109Sarchie ERROUT(ENOMEM); 30259109Sarchie if (isComp) 30359109Sarchie MPPC_InitCompressionHistory(d->history); 30459109Sarchie else { 30559109Sarchie MPPC_InitDecompressionHistory( 30659109Sarchie d->history); 30759109Sarchie } 30859109Sarchie } 30959109Sarchie#endif 31059109Sarchie 31159109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 31259109Sarchie /* Generate initial session keys for encryption */ 31359109Sarchie if ((cfg->bits & MPPE_BITS) != 0) { 31459109Sarchie const int keylen = KEYLEN(cfg->bits); 31559109Sarchie 31659109Sarchie bcopy(cfg->startkey, d->key, keylen); 31759109Sarchie ng_mppc_getkey(cfg->startkey, d->key, keylen); 31887971Sarchie if ((cfg->bits & MPPE_40) != 0) 31987971Sarchie bcopy(&ng_mppe_weakenkey, d->key, 3); 32087971Sarchie else if ((cfg->bits & MPPE_56) != 0) 32187971Sarchie bcopy(&ng_mppe_weakenkey, d->key, 1); 32259109Sarchie rc4_init(&d->rc4, d->key, keylen); 32359109Sarchie } 32459109Sarchie#endif 32559109Sarchie 32659109Sarchie /* Initialize other state */ 32759109Sarchie d->cc = 0; 32859109Sarchie d->flushed = 0; 32959109Sarchie break; 33059109Sarchie } 33159109Sarchie 33259109Sarchie case NGM_MPPC_RESETREQ: 33359109Sarchie ng_mppc_reset_req(node); 33459109Sarchie break; 33559109Sarchie 33659109Sarchie default: 33759109Sarchie error = EINVAL; 33859109Sarchie break; 33959109Sarchie } 34059109Sarchie break; 34159109Sarchie default: 34259109Sarchie error = EINVAL; 34359109Sarchie break; 34459109Sarchie } 34559109Sarchiedone: 34670700Sjulian NG_RESPOND_MSG(error, node, item, resp); 34770700Sjulian NG_FREE_MSG(msg); 34859109Sarchie return (error); 34959109Sarchie} 35059109Sarchie 35159109Sarchie/* 35259109Sarchie * Receive incoming data on our hook. 35359109Sarchie */ 35459109Sarchiestatic int 35570700Sjulianng_mppc_rcvdata(hook_p hook, item_p item) 35659109Sarchie{ 35770784Sjulian const node_p node = NG_HOOK_NODE(hook); 35870784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 35959109Sarchie struct mbuf *out; 36059109Sarchie int error; 36170700Sjulian struct mbuf *m; 36259109Sarchie 36370700Sjulian NGI_GET_M(item, m); 36459109Sarchie /* Compress and/or encrypt */ 36559109Sarchie if (hook == priv->xmit.hook) { 36659109Sarchie if (!priv->xmit.cfg.enable) { 36770700Sjulian NG_FREE_M(m); 36870700Sjulian NG_FREE_ITEM(item); 36959109Sarchie return (ENXIO); 37059109Sarchie } 37159109Sarchie if ((error = ng_mppc_compress(node, m, &out)) != 0) { 37270700Sjulian NG_FREE_M(m); 37370700Sjulian NG_FREE_ITEM(item); 37459109Sarchie return(error); 37559109Sarchie } 37670700Sjulian NG_FREE_M(m); 37770700Sjulian NG_FWD_NEW_DATA(error, item, priv->xmit.hook, out); 37859109Sarchie return (error); 37959109Sarchie } 38059109Sarchie 38159109Sarchie /* Decompress and/or decrypt */ 38259109Sarchie if (hook == priv->recv.hook) { 38359109Sarchie if (!priv->recv.cfg.enable) { 38470700Sjulian NG_FREE_M(m); 38570700Sjulian NG_FREE_ITEM(item); 38659109Sarchie return (ENXIO); 38759109Sarchie } 38859109Sarchie if ((error = ng_mppc_decompress(node, m, &out)) != 0) { 38970700Sjulian NG_FREE_M(m); 39070700Sjulian NG_FREE_ITEM(item); 391102244Sarchie if (error == EINVAL && priv->ctrlnode != 0) { 39259109Sarchie struct ng_mesg *msg; 39359109Sarchie 39459109Sarchie /* Need to send a reset-request */ 39559109Sarchie NG_MKMESSAGE(msg, NGM_MPPC_COOKIE, 39659109Sarchie NGM_MPPC_RESETREQ, 0, M_NOWAIT); 39759109Sarchie if (msg == NULL) 39859109Sarchie return (error); 39970700Sjulian NG_SEND_MSG_ID(error, node, msg, 400102244Sarchie priv->ctrlnode, 0); 40159109Sarchie } 40259109Sarchie return (error); 40359109Sarchie } 40470700Sjulian NG_FREE_M(m); 40570700Sjulian NG_FWD_NEW_DATA(error, item, priv->recv.hook, out); 40659109Sarchie return (error); 40759109Sarchie } 40859109Sarchie 40959109Sarchie /* Oops */ 41087599Sobrien panic("%s: unknown hook", __func__); 41183366Sjulian#ifdef RESTARTABLE_PANICS 41283366Sjulian return (EINVAL); 41383366Sjulian#endif 41459109Sarchie} 41559109Sarchie 41659109Sarchie/* 41759109Sarchie * Destroy node 41859109Sarchie */ 41959109Sarchiestatic int 42070700Sjulianng_mppc_shutdown(node_p node) 42159109Sarchie{ 42270784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 42359109Sarchie 42459109Sarchie /* Take down netgraph node */ 42559109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 42659109Sarchie if (priv->xmit.history != NULL) 42770870Sjulian FREE(priv->xmit.history, M_NETGRAPH_MPPC); 42859109Sarchie if (priv->recv.history != NULL) 42970870Sjulian FREE(priv->recv.history, M_NETGRAPH_MPPC); 43059109Sarchie#endif 43159109Sarchie bzero(priv, sizeof(*priv)); 43270870Sjulian FREE(priv, M_NETGRAPH_MPPC); 43370784Sjulian NG_NODE_SET_PRIVATE(node, NULL); 43470784Sjulian NG_NODE_UNREF(node); /* let the node escape */ 43559109Sarchie return (0); 43659109Sarchie} 43759109Sarchie 43859109Sarchie/* 43959109Sarchie * Hook disconnection 44059109Sarchie */ 44159109Sarchiestatic int 44259109Sarchieng_mppc_disconnect(hook_p hook) 44359109Sarchie{ 44470784Sjulian const node_p node = NG_HOOK_NODE(hook); 44570784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 44659109Sarchie 44759109Sarchie /* Zero out hook pointer */ 44859109Sarchie if (hook == priv->xmit.hook) 44959109Sarchie priv->xmit.hook = NULL; 45059109Sarchie if (hook == priv->recv.hook) 45159109Sarchie priv->recv.hook = NULL; 45259109Sarchie 45359109Sarchie /* Go away if no longer connected */ 45470784Sjulian if ((NG_NODE_NUMHOOKS(node) == 0) 45570784Sjulian && NG_NODE_IS_VALID(node)) 45670700Sjulian ng_rmnode_self(node); 45759109Sarchie return (0); 45859109Sarchie} 45959109Sarchie 46059109Sarchie/************************************************************************ 46159109Sarchie HELPER STUFF 46259109Sarchie ************************************************************************/ 46359109Sarchie 46459109Sarchie/* 46559109Sarchie * Compress/encrypt a packet and put the result in a new mbuf at *resultp. 46659109Sarchie * The original mbuf is not free'd. 46759109Sarchie */ 46859109Sarchiestatic int 46959109Sarchieng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp) 47059109Sarchie{ 47170784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 47259109Sarchie struct ng_mppc_dir *const d = &priv->xmit; 47359109Sarchie u_char *inbuf, *outbuf; 47459109Sarchie int outlen, inlen; 47559109Sarchie u_int16_t header; 47659109Sarchie 47759109Sarchie /* Initialize */ 47859109Sarchie *resultp = NULL; 47959109Sarchie header = d->cc; 480169262Smav 481169262Smav /* Always set the flushed bit in stateless mode */ 482169262Smav if (d->flushed || ((d->cfg.bits & MPPE_STATELESS) != 0)) { 48359109Sarchie header |= MPPC_FLAG_FLUSHED; 48459109Sarchie d->flushed = 0; 48559109Sarchie } 48659109Sarchie 48759109Sarchie /* Work with contiguous regions of memory */ 48859109Sarchie inlen = m->m_pkthdr.len; 48970870Sjulian MALLOC(inbuf, u_char *, inlen, M_NETGRAPH_MPPC, M_NOWAIT); 49059109Sarchie if (inbuf == NULL) 49159109Sarchie return (ENOMEM); 49259109Sarchie m_copydata(m, 0, inlen, (caddr_t)inbuf); 49359109Sarchie if ((d->cfg.bits & MPPC_BIT) != 0) 49459109Sarchie outlen = MPPC_MAX_BLOWUP(inlen); 49559109Sarchie else 49659109Sarchie outlen = MPPC_HDRLEN + inlen; 49770870Sjulian MALLOC(outbuf, u_char *, outlen, M_NETGRAPH_MPPC, M_NOWAIT); 49859109Sarchie if (outbuf == NULL) { 49970870Sjulian FREE(inbuf, M_NETGRAPH_MPPC); 50059109Sarchie return (ENOMEM); 50159109Sarchie } 50259109Sarchie 50359109Sarchie /* Compress "inbuf" into "outbuf" (if compression enabled) */ 50459109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 50559109Sarchie if ((d->cfg.bits & MPPC_BIT) != 0) { 50659109Sarchie u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS; 50759109Sarchie u_char *source, *dest; 50859109Sarchie u_long sourceCnt, destCnt; 50959109Sarchie int rtn; 51059109Sarchie 51159109Sarchie /* Prepare to compress */ 51259109Sarchie source = inbuf; 51359109Sarchie sourceCnt = inlen; 51459109Sarchie dest = outbuf + MPPC_HDRLEN; 51559109Sarchie destCnt = outlen - MPPC_HDRLEN; 51659109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 51759109Sarchie flags |= MPPC_SAVE_HISTORY; 51859109Sarchie 51959109Sarchie /* Compress */ 52059109Sarchie rtn = MPPC_Compress(&source, &dest, &sourceCnt, 52159109Sarchie &destCnt, d->history, flags, 0); 52259109Sarchie 52359109Sarchie /* Check return value */ 52487599Sobrien KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__)); 52559109Sarchie if ((rtn & MPPC_EXPANDED) == 0 52659109Sarchie && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) { 52759109Sarchie outlen -= destCnt; 52859109Sarchie header |= MPPC_FLAG_COMPRESSED; 52959109Sarchie if ((rtn & MPPC_RESTART_HISTORY) != 0) 53059109Sarchie header |= MPPC_FLAG_RESTART; 53159109Sarchie } 53259109Sarchie d->flushed = (rtn & MPPC_EXPANDED) != 0 53359109Sarchie || (flags & MPPC_SAVE_HISTORY) == 0; 53459109Sarchie } 53559109Sarchie#endif 53659109Sarchie 53759109Sarchie /* If we did not compress this packet, copy it to output buffer */ 53859109Sarchie if ((header & MPPC_FLAG_COMPRESSED) == 0) { 53959109Sarchie bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen); 54059109Sarchie outlen = MPPC_HDRLEN + inlen; 54159109Sarchie } 54270870Sjulian FREE(inbuf, M_NETGRAPH_MPPC); 54359109Sarchie 54459109Sarchie /* Now encrypt packet (if encryption enabled) */ 54559109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 54659109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 54759109Sarchie 548169263Smav /* Set header bits */ 54959109Sarchie header |= MPPC_FLAG_ENCRYPTED; 55059109Sarchie 55159109Sarchie /* Update key if it's time */ 55259109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0 55359109Sarchie || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { 554169263Smav ng_mppc_updatekey(d->cfg.bits, 555169263Smav d->cfg.startkey, d->key, &d->rc4); 556169263Smav } else if ((header & MPPC_FLAG_FLUSHED) != 0) { 557169263Smav /* Need to reset key if we say we did 558169263Smav and ng_mppc_updatekey wasn't called to do it also. */ 559169263Smav rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 56059109Sarchie } 56159109Sarchie 56259109Sarchie /* Encrypt packet */ 56359109Sarchie rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN, 56459109Sarchie outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN); 56559109Sarchie } 56659109Sarchie#endif 56759109Sarchie 568169261Smav /* Update coherency count for next time (12 bit arithmetic) */ 569169261Smav MPPC_CCOUNT_INC(d->cc); 57059109Sarchie 57159109Sarchie /* Install header */ 57259109Sarchie *((u_int16_t *)outbuf) = htons(header); 57359109Sarchie 57459109Sarchie /* Return packet in an mbuf */ 57559109Sarchie *resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL); 57670870Sjulian FREE(outbuf, M_NETGRAPH_MPPC); 57759109Sarchie return (*resultp == NULL ? ENOBUFS : 0); 57859109Sarchie} 57959109Sarchie 58059109Sarchie/* 58159109Sarchie * Decompress/decrypt packet and put the result in a new mbuf at *resultp. 58259109Sarchie * The original mbuf is not free'd. 58359109Sarchie */ 58459109Sarchiestatic int 58559109Sarchieng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp) 58659109Sarchie{ 58770784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 58859109Sarchie struct ng_mppc_dir *const d = &priv->recv; 589107845Sarchie u_int16_t header, cc; 590107845Sarchie u_int numLost; 59159109Sarchie u_char *buf; 59259109Sarchie int len; 59359109Sarchie 59459109Sarchie /* Pull off header */ 59559109Sarchie if (m->m_pkthdr.len < MPPC_HDRLEN) 59659109Sarchie return (EINVAL); 59759109Sarchie m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header); 59890868Smike header = ntohs(header); 59959109Sarchie cc = (header & MPPC_CCOUNT_MASK); 60059109Sarchie 60159109Sarchie /* Copy payload into a contiguous region of memory */ 60259109Sarchie len = m->m_pkthdr.len - MPPC_HDRLEN; 60370870Sjulian MALLOC(buf, u_char *, len, M_NETGRAPH_MPPC, M_NOWAIT); 60459109Sarchie if (buf == NULL) 60559109Sarchie return (ENOMEM); 60659109Sarchie m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf); 60759109Sarchie 608107845Sarchie /* Check for an unexpected jump in the sequence number */ 60959109Sarchie numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK); 61059109Sarchie 61159109Sarchie /* If flushed bit set, we can always handle packet */ 61259109Sarchie if ((header & MPPC_FLAG_FLUSHED) != 0) { 61359109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 61459109Sarchie if (d->history != NULL) 61559109Sarchie MPPC_InitDecompressionHistory(d->history); 61659109Sarchie#endif 61759109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 61859109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 619107845Sarchie u_int rekey; 62059109Sarchie 621107845Sarchie /* How many times are we going to have to re-key? */ 622107845Sarchie rekey = ((d->cfg.bits & MPPE_STATELESS) != 0) ? 623107845Sarchie numLost : (numLost / (MPPE_UPDATE_MASK + 1)); 624107845Sarchie if (rekey > MPPE_MAX_REKEY) { 625107845Sarchie log(LOG_ERR, "%s: too many (%d) packets" 626107845Sarchie " dropped, disabling node %p!", 627107845Sarchie __func__, numLost, node); 628107845Sarchie priv->recv.cfg.enable = 0; 629107845Sarchie goto failed; 630107845Sarchie } 631107845Sarchie 632107845Sarchie /* Re-key as necessary to catch up to peer */ 63359109Sarchie while (d->cc != cc) { 634107845Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0 63559109Sarchie || (d->cc & MPPE_UPDATE_MASK) 63659109Sarchie == MPPE_UPDATE_FLAG) { 63759109Sarchie ng_mppc_updatekey(d->cfg.bits, 63859109Sarchie d->cfg.startkey, d->key, &d->rc4); 63959109Sarchie } 640169261Smav MPPC_CCOUNT_INC(d->cc); 64159109Sarchie } 64259109Sarchie 64359109Sarchie /* Reset key (except in stateless mode, see below) */ 64459109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 64559109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 64659109Sarchie } 64759109Sarchie#endif 64859109Sarchie d->cc = cc; /* skip over lost seq numbers */ 64959109Sarchie numLost = 0; /* act like no packets were lost */ 65059109Sarchie } 65159109Sarchie 65259109Sarchie /* Can't decode non-sequential packets without a flushed bit */ 65359109Sarchie if (numLost != 0) 65459109Sarchie goto failed; 65559109Sarchie 65659109Sarchie /* Decrypt packet */ 65759109Sarchie if ((header & MPPC_FLAG_ENCRYPTED) != 0) { 65859109Sarchie 65959109Sarchie /* Are we not expecting encryption? */ 66059109Sarchie if ((d->cfg.bits & MPPE_BITS) == 0) { 66159109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 66287599Sobrien __func__, "encrypted"); 66359109Sarchie goto failed; 66459109Sarchie } 66559109Sarchie 66659109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 66759109Sarchie /* Update key if it's time (always in stateless mode) */ 66859109Sarchie if ((d->cfg.bits & MPPE_STATELESS) != 0 66959109Sarchie || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { 67059109Sarchie ng_mppc_updatekey(d->cfg.bits, 67159109Sarchie d->cfg.startkey, d->key, &d->rc4); 67259109Sarchie } 67359109Sarchie 67459109Sarchie /* Decrypt packet */ 67559109Sarchie rc4_crypt(&d->rc4, buf, buf, len); 67659109Sarchie#endif 67759109Sarchie } else { 67859109Sarchie 67959109Sarchie /* Are we expecting encryption? */ 68059109Sarchie if ((d->cfg.bits & MPPE_BITS) != 0) { 68159109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 68287599Sobrien __func__, "unencrypted"); 68359109Sarchie goto failed; 68459109Sarchie } 68559109Sarchie } 68659109Sarchie 68759109Sarchie /* Update coherency count for next time (12 bit arithmetic) */ 688169261Smav MPPC_CCOUNT_INC(d->cc); 68959109Sarchie 69059109Sarchie /* Check for unexpected compressed packet */ 69159109Sarchie if ((header & MPPC_FLAG_COMPRESSED) != 0 69259109Sarchie && (d->cfg.bits & MPPC_BIT) == 0) { 69359109Sarchie log(LOG_ERR, "%s: rec'd unexpectedly %s packet", 69487599Sobrien __func__, "compressed"); 69559109Sarchiefailed: 69670870Sjulian FREE(buf, M_NETGRAPH_MPPC); 69759109Sarchie return (EINVAL); 69859109Sarchie } 69959109Sarchie 70059109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 70159109Sarchie /* Decompress packet */ 70259109Sarchie if ((header & MPPC_FLAG_COMPRESSED) != 0) { 70359109Sarchie int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS; 70459109Sarchie u_char *decompbuf, *source, *dest; 70559109Sarchie u_long sourceCnt, destCnt; 70659109Sarchie int decomplen, rtn; 70759109Sarchie 70859109Sarchie /* Allocate a buffer for decompressed data */ 70959109Sarchie MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE 71070870Sjulian + MPPC_DECOMP_SAFETY, M_NETGRAPH_MPPC, M_NOWAIT); 71159109Sarchie if (decompbuf == NULL) { 71270870Sjulian FREE(buf, M_NETGRAPH_MPPC); 71359109Sarchie return (ENOMEM); 71459109Sarchie } 71559109Sarchie decomplen = MPPC_DECOMP_BUFSIZE; 71659109Sarchie 71759109Sarchie /* Prepare to decompress */ 71859109Sarchie source = buf; 71959109Sarchie sourceCnt = len; 72059109Sarchie dest = decompbuf; 72159109Sarchie destCnt = decomplen; 72259109Sarchie if ((header & MPPC_FLAG_RESTART) != 0) 72359109Sarchie flags |= MPPC_RESTART_HISTORY; 72459109Sarchie 72559109Sarchie /* Decompress */ 72659109Sarchie rtn = MPPC_Decompress(&source, &dest, 72759109Sarchie &sourceCnt, &destCnt, d->history, flags); 72859109Sarchie 72959109Sarchie /* Check return value */ 73087599Sobrien KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__)); 73159109Sarchie if ((rtn & MPPC_DEST_EXHAUSTED) != 0 73259109Sarchie || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) { 73359109Sarchie log(LOG_ERR, "%s: decomp returned 0x%x", 73487599Sobrien __func__, rtn); 73570870Sjulian FREE(decompbuf, M_NETGRAPH_MPPC); 73659109Sarchie goto failed; 73759109Sarchie } 73859109Sarchie 73959109Sarchie /* Replace compressed data with decompressed data */ 74070870Sjulian FREE(buf, M_NETGRAPH_MPPC); 74159109Sarchie buf = decompbuf; 74259109Sarchie len = decomplen - destCnt; 74359109Sarchie } 74459109Sarchie#endif 74559109Sarchie 74659109Sarchie /* Return result in an mbuf */ 74759109Sarchie *resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL); 74870870Sjulian FREE(buf, M_NETGRAPH_MPPC); 74959109Sarchie return (*resultp == NULL ? ENOBUFS : 0); 75059109Sarchie} 75159109Sarchie 75259109Sarchie/* 75359109Sarchie * The peer has sent us a CCP ResetRequest, so reset our transmit state. 75459109Sarchie */ 75559109Sarchiestatic void 75659109Sarchieng_mppc_reset_req(node_p node) 75759109Sarchie{ 75870784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 75959109Sarchie struct ng_mppc_dir *const d = &priv->xmit; 76059109Sarchie 76159109Sarchie#ifdef NETGRAPH_MPPC_COMPRESSION 76259109Sarchie if (d->history != NULL) 76359109Sarchie MPPC_InitCompressionHistory(d->history); 76459109Sarchie#endif 76559109Sarchie#ifdef NETGRAPH_MPPC_ENCRYPTION 76659109Sarchie if ((d->cfg.bits & MPPE_STATELESS) == 0) 76759109Sarchie rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); 76859109Sarchie#endif 76959109Sarchie d->flushed = 1; 77059109Sarchie} 77159109Sarchie 77259109Sarchie/* 77359109Sarchie * Generate a new encryption key 77459109Sarchie */ 77559109Sarchiestatic void 77659109Sarchieng_mppc_getkey(const u_char *h, u_char *h2, int len) 77759109Sarchie{ 77859109Sarchie static const u_char pad1[10] = 77959109Sarchie { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; 78059109Sarchie static const u_char pad2[10] = 78159109Sarchie { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, }; 78259109Sarchie u_char hash[20]; 78359109Sarchie SHA1_CTX c; 78459109Sarchie int k; 78559109Sarchie 78659109Sarchie SHA1Init(&c); 78759109Sarchie SHA1Update(&c, h, len); 78859109Sarchie for (k = 0; k < 4; k++) 789169258Smav SHA1Update(&c, pad1, sizeof(pad1)); 79059109Sarchie SHA1Update(&c, h2, len); 79159109Sarchie for (k = 0; k < 4; k++) 79259109Sarchie SHA1Update(&c, pad2, sizeof(pad2)); 79359109Sarchie SHA1Final(hash, &c); 79459109Sarchie bcopy(hash, h2, len); 79559109Sarchie} 79659109Sarchie 79759109Sarchie/* 79859109Sarchie * Update the encryption key 79959109Sarchie */ 80059109Sarchiestatic void 80159109Sarchieng_mppc_updatekey(u_int32_t bits, 80259109Sarchie u_char *key0, u_char *key, struct rc4_state *rc4) 80359109Sarchie{ 80459109Sarchie const int keylen = KEYLEN(bits); 80559109Sarchie 80659109Sarchie ng_mppc_getkey(key0, key, keylen); 80759109Sarchie rc4_init(rc4, key, keylen); 80859109Sarchie rc4_crypt(rc4, key, key, keylen); 80987971Sarchie if ((bits & MPPE_40) != 0) 81087971Sarchie bcopy(&ng_mppe_weakenkey, key, 3); 81187971Sarchie else if ((bits & MPPE_56) != 0) 81287971Sarchie bcopy(&ng_mppe_weakenkey, key, 1); 81359109Sarchie rc4_init(rc4, key, keylen); 81459109Sarchie} 81559109Sarchie 816