ng_pred1.c revision 166019
1165619Sglebius/*- 2165619Sglebius * Copyright (c) 2006 Alexander Motin <mav@alkar.net> 3165619Sglebius * All rights reserved. 4165619Sglebius * 5165619Sglebius * Redistribution and use in source and binary forms, with or without 6165619Sglebius * modification, are permitted provided that the following conditions 7165619Sglebius * are met: 8165619Sglebius * 1. Redistributions of source code must retain the above copyright 9165619Sglebius * notice unmodified, this list of conditions, and the following 10165619Sglebius * disclaimer. 11165619Sglebius * 2. Redistributions in binary form must reproduce the above copyright 12165619Sglebius * notice, this list of conditions and the following disclaimer in the 13165619Sglebius * documentation and/or other materials provided with the distribution. 14165619Sglebius * 15165619Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16165619Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17165619Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18165619Sglebius * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19165619Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20165619Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21165619Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22165619Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23165619Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24165619Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25165619Sglebius * SUCH DAMAGE. 26165619Sglebius * 27165619Sglebius * $FreeBSD: head/sys/netgraph/ng_pred1.c 166019 2007-01-15 05:55:56Z glebius $ 28165619Sglebius */ 29165619Sglebius 30165619Sglebius/* 31165619Sglebius * Predictor-1 PPP compression netgraph node type. 32165619Sglebius */ 33165619Sglebius 34165619Sglebius#include <sys/param.h> 35165619Sglebius#include <sys/systm.h> 36165619Sglebius#include <sys/kernel.h> 37165619Sglebius#include <sys/mbuf.h> 38165619Sglebius#include <sys/malloc.h> 39165619Sglebius#include <sys/errno.h> 40165619Sglebius#include <sys/syslog.h> 41165619Sglebius 42165619Sglebius#include <netgraph/ng_message.h> 43165619Sglebius#include <netgraph/netgraph.h> 44165619Sglebius#include <netgraph/ng_parse.h> 45165619Sglebius#include <netgraph/ng_pred1.h> 46165619Sglebius 47165619Sglebius#include "opt_netgraph.h" 48165619Sglebius 49165619SglebiusMALLOC_DEFINE(M_NETGRAPH_PRED1, "netgraph_pred1", "netgraph pred1 node "); 50165619Sglebius 51165619Sglebius/* PRED1 header length */ 52165619Sglebius#define PRED1_HDRLEN 2 53165619Sglebius 54165619Sglebius#define PRED1_TABLE_SIZE 0x10000 55165619Sglebius#define PRED1_BUF_SIZE 4096 56165619Sglebius#define PPP_INITFCS 0xffff /* Initial FCS value */ 57165619Sglebius#define PPP_GOODFCS 0xf0b8 /* Good final FCS value */ 58165619Sglebius 59165619Sglebius/* 60165619Sglebius * The following hash code is the heart of the algorithm: 61165619Sglebius * it builds a sliding hash sum of the previous 3-and-a-bit 62165619Sglebius * characters which will be used to index the guess table. 63165619Sglebius * A better hash function would result in additional compression, 64165619Sglebius * at the expense of time. 65165619Sglebius */ 66165619Sglebius 67165619Sglebius#define HASH(x) priv->Hash = (priv->Hash << 4) ^ (x) 68165619Sglebius 69165619Sglebius/* Node private data */ 70165619Sglebiusstruct ng_pred1_private { 71165619Sglebius struct ng_pred1_config cfg; /* configuration */ 72165619Sglebius u_char GuessTable[PRED1_TABLE_SIZE]; /* dictionary */ 73165619Sglebius u_char inbuf[PRED1_BUF_SIZE]; /* input buffer */ 74165619Sglebius u_char outbuf[PRED1_BUF_SIZE]; /* output buffer */ 75165619Sglebius struct ng_pred1_stats stats; /* statistics */ 76165619Sglebius uint16_t Hash; 77165619Sglebius ng_ID_t ctrlnode; /* path to controlling node */ 78165619Sglebius uint16_t seqnum; /* sequence number */ 79165619Sglebius u_char compress; /* compress/decompress flag */ 80165619Sglebius}; 81165619Sglebiustypedef struct ng_pred1_private *priv_p; 82165619Sglebius 83165619Sglebius/* Netgraph node methods */ 84165619Sglebiusstatic ng_constructor_t ng_pred1_constructor; 85165619Sglebiusstatic ng_rcvmsg_t ng_pred1_rcvmsg; 86165619Sglebiusstatic ng_shutdown_t ng_pred1_shutdown; 87165619Sglebiusstatic ng_newhook_t ng_pred1_newhook; 88165619Sglebiusstatic ng_rcvdata_t ng_pred1_rcvdata; 89165619Sglebiusstatic ng_disconnect_t ng_pred1_disconnect; 90165619Sglebius 91165619Sglebius/* Helper functions */ 92165619Sglebiusstatic int ng_pred1_compress(node_p node, struct mbuf *m, 93165619Sglebius struct mbuf **resultp); 94165619Sglebiusstatic int ng_pred1_decompress(node_p node, struct mbuf *m, 95165619Sglebius struct mbuf **resultp); 96165619Sglebiusstatic void Pred1Init(node_p node); 97165619Sglebiusstatic int Pred1Compress(node_p node, u_char *source, u_char *dest, 98165619Sglebius int len); 99165619Sglebiusstatic int Pred1Decompress(node_p node, u_char *source, u_char *dest, 100165619Sglebius int slen, int dlen); 101165619Sglebiusstatic void Pred1SyncTable(node_p node, u_char *source, int len); 102165619Sglebiusstatic uint16_t Crc16(uint16_t fcs, u_char *cp, int len); 103165619Sglebius 104165619Sglebiusstatic const uint16_t Crc16Table[]; 105165619Sglebius 106165619Sglebius/* Parse type for struct ng_pred1_config. */ 107165619Sglebiusstatic const struct ng_parse_struct_field ng_pred1_config_type_fields[] 108165619Sglebius = NG_PRED1_CONFIG_INFO; 109165619Sglebiusstatic const struct ng_parse_type ng_pred1_config_type = { 110165619Sglebius &ng_parse_struct_type, 111165619Sglebius ng_pred1_config_type_fields 112165619Sglebius}; 113165619Sglebius 114165619Sglebius/* Parse type for struct ng_pred1_stat. */ 115165619Sglebiusstatic const struct ng_parse_struct_field ng_pred1_stats_type_fields[] 116165619Sglebius = NG_PRED1_STATS_INFO; 117165619Sglebiusstatic const struct ng_parse_type ng_pred1_stat_type = { 118165619Sglebius &ng_parse_struct_type, 119165619Sglebius ng_pred1_stats_type_fields 120165619Sglebius}; 121165619Sglebius 122165619Sglebius/* List of commands and how to convert arguments to/from ASCII. */ 123165619Sglebiusstatic const struct ng_cmdlist ng_pred1_cmds[] = { 124165619Sglebius { 125165619Sglebius NGM_PRED1_COOKIE, 126165619Sglebius NGM_PRED1_CONFIG, 127165619Sglebius "config", 128165619Sglebius &ng_pred1_config_type, 129165619Sglebius NULL 130165619Sglebius }, 131165619Sglebius { 132165619Sglebius NGM_PRED1_COOKIE, 133165619Sglebius NGM_PRED1_RESETREQ, 134165619Sglebius "resetreq", 135165619Sglebius NULL, 136165619Sglebius NULL 137165619Sglebius }, 138165619Sglebius { 139165619Sglebius NGM_PRED1_COOKIE, 140165619Sglebius NGM_PRED1_GET_STATS, 141165619Sglebius "getstats", 142165619Sglebius NULL, 143165619Sglebius &ng_pred1_stat_type 144165619Sglebius }, 145165619Sglebius { 146165619Sglebius NGM_PRED1_COOKIE, 147165619Sglebius NGM_PRED1_CLR_STATS, 148165619Sglebius "clrstats", 149165619Sglebius NULL, 150165619Sglebius NULL 151165619Sglebius }, 152165619Sglebius { 153165619Sglebius NGM_PRED1_COOKIE, 154165619Sglebius NGM_PRED1_GETCLR_STATS, 155165619Sglebius "getclrstats", 156165619Sglebius NULL, 157165619Sglebius &ng_pred1_stat_type 158165619Sglebius }, 159165619Sglebius { 0 } 160165619Sglebius}; 161165619Sglebius 162165619Sglebius/* Node type descriptor */ 163165619Sglebiusstatic struct ng_type ng_pred1_typestruct = { 164165619Sglebius .version = NG_ABI_VERSION, 165165619Sglebius .name = NG_PRED1_NODE_TYPE, 166165619Sglebius .constructor = ng_pred1_constructor, 167165619Sglebius .rcvmsg = ng_pred1_rcvmsg, 168165619Sglebius .shutdown = ng_pred1_shutdown, 169165619Sglebius .newhook = ng_pred1_newhook, 170165619Sglebius .rcvdata = ng_pred1_rcvdata, 171165619Sglebius .disconnect = ng_pred1_disconnect, 172165619Sglebius .cmdlist = ng_pred1_cmds, 173165619Sglebius}; 174165619SglebiusNETGRAPH_INIT(pred1, &ng_pred1_typestruct); 175165619Sglebius 176165619Sglebius#define ERROUT(x) do { error = (x); goto done; } while (0) 177165619Sglebius 178165619Sglebius/************************************************************************ 179165619Sglebius NETGRAPH NODE STUFF 180165619Sglebius ************************************************************************/ 181165619Sglebius 182165619Sglebius/* 183165619Sglebius * Node type constructor 184165619Sglebius */ 185165619Sglebiusstatic int 186165619Sglebiusng_pred1_constructor(node_p node) 187165619Sglebius{ 188165619Sglebius priv_p priv; 189165619Sglebius 190165619Sglebius /* Allocate private structure. */ 191165619Sglebius priv = malloc(sizeof(*priv), M_NETGRAPH_PRED1, M_WAITOK | M_ZERO); 192165619Sglebius 193165619Sglebius NG_NODE_SET_PRIVATE(node, priv); 194165619Sglebius 195165619Sglebius /* This node is not thread safe. */ 196165619Sglebius NG_NODE_FORCE_WRITER(node); 197165619Sglebius 198165619Sglebius /* Done */ 199165619Sglebius return (0); 200165619Sglebius} 201165619Sglebius 202165619Sglebius/* 203165619Sglebius * Give our OK for a hook to be added. 204165619Sglebius */ 205165619Sglebiusstatic int 206165619Sglebiusng_pred1_newhook(node_p node, hook_p hook, const char *name) 207165619Sglebius{ 208165619Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 209165619Sglebius 210165619Sglebius if (NG_NODE_NUMHOOKS(node) > 0) 211165619Sglebius return (EINVAL); 212165619Sglebius 213165619Sglebius if (strcmp(name, NG_PRED1_HOOK_COMP) == 0) 214165619Sglebius priv->compress = 1; 215165619Sglebius else if (strcmp(name, NG_PRED1_HOOK_DECOMP) == 0) 216165619Sglebius priv->compress = 0; 217165619Sglebius else 218165619Sglebius return (EINVAL); 219165619Sglebius 220165619Sglebius return (0); 221165619Sglebius} 222165619Sglebius 223165619Sglebius/* 224165619Sglebius * Receive a control message. 225165619Sglebius */ 226165619Sglebiusstatic int 227165619Sglebiusng_pred1_rcvmsg(node_p node, item_p item, hook_p lasthook) 228165619Sglebius{ 229165619Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 230165619Sglebius struct ng_mesg *resp = NULL; 231165619Sglebius int error = 0; 232165619Sglebius struct ng_mesg *msg; 233165619Sglebius 234165619Sglebius NGI_GET_MSG(item, msg); 235165619Sglebius 236165619Sglebius if (msg->header.typecookie != NGM_PRED1_COOKIE) 237165619Sglebius ERROUT(EINVAL); 238166019Sglebius 239165619Sglebius switch (msg->header.cmd) { 240165619Sglebius case NGM_PRED1_CONFIG: 241165619Sglebius { 242165619Sglebius struct ng_pred1_config *const cfg = 243165619Sglebius (struct ng_pred1_config *)msg->data; 244165619Sglebius 245165619Sglebius /* Check configuration. */ 246165619Sglebius if (msg->header.arglen != sizeof(*cfg)) 247165619Sglebius ERROUT(EINVAL); 248165619Sglebius 249165619Sglebius /* Configuration is OK, reset to it. */ 250165619Sglebius priv->cfg = *cfg; 251165619Sglebius 252165619Sglebius /* Save return address so we can send reset-req's. */ 253165619Sglebius priv->ctrlnode = NGI_RETADDR(item); 254165619Sglebius 255165619Sglebius /* Clear our state. */ 256165619Sglebius Pred1Init(node); 257165619Sglebius 258165619Sglebius break; 259165619Sglebius } 260165619Sglebius case NGM_PRED1_RESETREQ: 261165619Sglebius Pred1Init(node); 262165619Sglebius break; 263165619Sglebius 264165619Sglebius case NGM_PRED1_GET_STATS: 265165619Sglebius case NGM_PRED1_CLR_STATS: 266165619Sglebius case NGM_PRED1_GETCLR_STATS: 267165619Sglebius { 268165619Sglebius /* Create response. */ 269165619Sglebius if (msg->header.cmd != NGM_PRED1_CLR_STATS) { 270165619Sglebius NG_MKRESPONSE(resp, msg, 271165619Sglebius sizeof(struct ng_pred1_stats), M_NOWAIT); 272165619Sglebius if (resp == NULL) 273165619Sglebius ERROUT(ENOMEM); 274165619Sglebius bcopy(&priv->stats, resp->data, 275166019Sglebius sizeof(struct ng_pred1_stats)); 276165619Sglebius } 277165619Sglebius 278165619Sglebius if (msg->header.cmd != NGM_PRED1_GET_STATS) 279165619Sglebius bzero(&priv->stats, sizeof(struct ng_pred1_stats)); 280165619Sglebius break; 281165619Sglebius } 282165619Sglebius 283165619Sglebius default: 284165619Sglebius error = EINVAL; 285165619Sglebius break; 286165619Sglebius } 287165619Sglebiusdone: 288165619Sglebius NG_RESPOND_MSG(error, node, item, resp); 289165619Sglebius NG_FREE_MSG(msg); 290165619Sglebius return (error); 291165619Sglebius} 292165619Sglebius 293165619Sglebius/* 294165619Sglebius * Receive incoming data on our hook. 295165619Sglebius */ 296165619Sglebiusstatic int 297165619Sglebiusng_pred1_rcvdata(hook_p hook, item_p item) 298165619Sglebius{ 299165619Sglebius const node_p node = NG_HOOK_NODE(hook); 300165619Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 301165619Sglebius struct mbuf *m, *out; 302165619Sglebius int error; 303165619Sglebius 304165619Sglebius if (!priv->cfg.enable) { 305165619Sglebius NG_FREE_ITEM(item); 306165619Sglebius return (ENXIO); 307165619Sglebius } 308166019Sglebius 309165619Sglebius NGI_GET_M(item, m); 310165619Sglebius /* Compress. */ 311165619Sglebius if (priv->compress) { 312165619Sglebius if ((error = ng_pred1_compress(node, m, &out)) != 0) { 313165619Sglebius NG_FREE_ITEM(item); 314165619Sglebius log(LOG_NOTICE, "%s: error: %d\n", __func__, error); 315165619Sglebius return (error); 316165619Sglebius } 317165619Sglebius 318165619Sglebius } else { /* Decompress. */ 319165619Sglebius if ((error = ng_pred1_decompress(node, m, &out)) != 0) { 320165619Sglebius NG_FREE_ITEM(item); 321165619Sglebius log(LOG_NOTICE, "%s: error: %d\n", __func__, error); 322165619Sglebius if (priv->ctrlnode != 0) { 323165619Sglebius struct ng_mesg *msg; 324165619Sglebius 325165619Sglebius /* Need to send a reset-request. */ 326165619Sglebius NG_MKMESSAGE(msg, NGM_PRED1_COOKIE, 327165619Sglebius NGM_PRED1_RESETREQ, 0, M_NOWAIT); 328165619Sglebius if (msg == NULL) 329165619Sglebius return (error); 330165619Sglebius NG_SEND_MSG_ID(error, node, msg, 331165619Sglebius priv->ctrlnode, 0); 332165619Sglebius } 333165619Sglebius return (error); 334165619Sglebius } 335165619Sglebius } 336165619Sglebius 337165619Sglebius NG_FWD_NEW_DATA(error, item, hook, out); 338165619Sglebius return (error); 339165619Sglebius} 340165619Sglebius 341165619Sglebius/* 342165619Sglebius * Destroy node. 343165619Sglebius */ 344165619Sglebiusstatic int 345165619Sglebiusng_pred1_shutdown(node_p node) 346165619Sglebius{ 347165619Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 348165619Sglebius 349165619Sglebius free(priv, M_NETGRAPH_PRED1); 350165619Sglebius NG_NODE_SET_PRIVATE(node, NULL); 351165619Sglebius NG_NODE_UNREF(node); /* Let the node escape. */ 352165619Sglebius return (0); 353165619Sglebius} 354165619Sglebius 355165619Sglebius/* 356165619Sglebius * Hook disconnection 357165619Sglebius */ 358165619Sglebiusstatic int 359165619Sglebiusng_pred1_disconnect(hook_p hook) 360165619Sglebius{ 361165619Sglebius const node_p node = NG_HOOK_NODE(hook); 362165619Sglebius 363165619Sglebius Pred1Init(node); 364165619Sglebius 365165619Sglebius /* Go away if no longer connected. */ 366165619Sglebius if ((NG_NODE_NUMHOOKS(node) == 0) && NG_NODE_IS_VALID(node)) 367165619Sglebius ng_rmnode_self(node); 368165619Sglebius return (0); 369165619Sglebius} 370165619Sglebius 371165619Sglebius/************************************************************************ 372165619Sglebius HELPER STUFF 373165619Sglebius ************************************************************************/ 374165619Sglebius 375165619Sglebius/* 376165619Sglebius * Compress/encrypt a packet and put the result in a new mbuf at *resultp. 377165619Sglebius * The original mbuf is not free'd. 378165619Sglebius */ 379165619Sglebiusstatic int 380165619Sglebiusng_pred1_compress(node_p node, struct mbuf *m, struct mbuf **resultp) 381165619Sglebius{ 382165619Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 383165619Sglebius int outlen, inlen; 384165619Sglebius u_char *out; 385165619Sglebius uint16_t fcs, lenn; 386165619Sglebius int len; 387165619Sglebius 388165619Sglebius /* Initialize. */ 389165619Sglebius *resultp = NULL; 390165619Sglebius 391165619Sglebius inlen = m->m_pkthdr.len; 392165619Sglebius 393165619Sglebius priv->stats.FramesPlain++; 394165619Sglebius priv->stats.InOctets += inlen; 395165619Sglebius 396165619Sglebius /* Reserve space for expansion. */ 397165619Sglebius if (inlen > (PRED1_BUF_SIZE*8/9 + 1 + 4)) { 398165619Sglebius priv->stats.Errors++; 399165619Sglebius NG_FREE_M(m); 400165619Sglebius return (ENOMEM); 401165619Sglebius } 402166019Sglebius 403165619Sglebius /* Work with contiguous regions of memory. */ 404165619Sglebius m_copydata(m, 0, inlen, (caddr_t)(priv->inbuf + 2)); 405165619Sglebius 406165619Sglebius NG_FREE_M(m); 407165619Sglebius 408165619Sglebius lenn = htons(inlen & 0x7FFF); 409165619Sglebius 410165619Sglebius /* Compute FCS. */ 411165619Sglebius fcs = Crc16(PPP_INITFCS, (u_char *)&lenn, 2); 412165619Sglebius fcs = Crc16(fcs, priv->inbuf + 2, inlen); 413165619Sglebius fcs = ~fcs; 414165619Sglebius 415165619Sglebius /* Compress data. */ 416165619Sglebius len = Pred1Compress(node, priv->inbuf + 2, priv->outbuf + 2, inlen); 417165619Sglebius 418165619Sglebius /* What happened? */ 419165619Sglebius if (len < inlen) { 420165619Sglebius out = priv->outbuf; 421165619Sglebius outlen = 2 + len; 422165619Sglebius *(uint16_t *)out = lenn; 423165619Sglebius *out |= 0x80; 424165619Sglebius priv->stats.FramesComp++; 425165619Sglebius } else { 426165619Sglebius out = priv->inbuf; 427165619Sglebius outlen = 2 + inlen; 428165619Sglebius *(uint16_t *)out = lenn; 429165619Sglebius priv->stats.FramesUncomp++; 430165619Sglebius } 431165619Sglebius 432165619Sglebius /* Add FCS. */ 433165619Sglebius (out + outlen)[0] = fcs & 0xFF; 434165619Sglebius (out + outlen)[1] = fcs >> 8; 435165619Sglebius 436165619Sglebius /* Calculate resulting size. */ 437165619Sglebius outlen += 2; 438165619Sglebius 439165619Sglebius /* Return packet in an mbuf. */ 440165619Sglebius *resultp = m_devget((caddr_t)out, outlen, 0, NULL, NULL); 441165619Sglebius if (*resultp == NULL) { 442165619Sglebius priv->stats.Errors++; 443165619Sglebius return (ENOMEM); 444165619Sglebius }; 445165619Sglebius 446165619Sglebius priv->stats.OutOctets += outlen; 447165619Sglebius 448165619Sglebius return (0); 449165619Sglebius} 450165619Sglebius 451165619Sglebius/* 452165619Sglebius * Decompress/decrypt packet and put the result in a new mbuf at *resultp. 453165619Sglebius * The original mbuf is not free'd. 454165619Sglebius */ 455165619Sglebiusstatic int 456165619Sglebiusng_pred1_decompress(node_p node, struct mbuf *m, struct mbuf **resultp) 457165619Sglebius{ 458165619Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 459165619Sglebius int inlen; 460165619Sglebius uint16_t len, len1, cf, lenn; 461165619Sglebius uint16_t fcs; 462165619Sglebius 463165619Sglebius /* Initialize. */ 464165619Sglebius *resultp = NULL; 465165619Sglebius 466165619Sglebius inlen = m->m_pkthdr.len; 467166019Sglebius 468165619Sglebius if (inlen > PRED1_BUF_SIZE) { 469165619Sglebius priv->stats.Errors++; 470165619Sglebius NG_FREE_M(m); 471165619Sglebius return (ENOMEM); 472165619Sglebius } 473165619Sglebius 474165619Sglebius /* Work with contiguous regions of memory. */ 475165619Sglebius m_copydata(m, 0, inlen, (caddr_t)priv->inbuf); 476165619Sglebius 477165619Sglebius priv->stats.InOctets += inlen; 478166019Sglebius 479165619Sglebius /* Get initial length value. */ 480165619Sglebius len = priv->inbuf[0] << 8; 481165619Sglebius len += priv->inbuf[1]; 482165619Sglebius 483165619Sglebius cf = (len & 0x8000); 484165619Sglebius len &= 0x7fff; 485165619Sglebius 486165619Sglebius /* Is data compressed or not really? */ 487165619Sglebius if (cf) { 488165619Sglebius NG_FREE_M(m); 489166019Sglebius 490165619Sglebius priv->stats.FramesComp++; 491165619Sglebius len1 = Pred1Decompress(node, priv->inbuf + 2, priv->outbuf, 492165619Sglebius inlen - 4, PRED1_BUF_SIZE); 493165619Sglebius if (len != len1) { 494165619Sglebius /* Error is detected. Send reset request */ 495165619Sglebius priv->stats.Errors++; 496165619Sglebius log(LOG_NOTICE, "ng_pred1: Comp length error (%d) " 497165619Sglebius "--> len (%d)\n", len, len1); 498165619Sglebius return (EIO); 499165619Sglebius } 500165619Sglebius 501165619Sglebius /* 502165619Sglebius * CRC check on receive is defined in RFC. It is surely required 503165619Sglebius * for compressed frames to signal dictionary corruption, 504165619Sglebius * but it is actually useless for uncompressed frames because 505165619Sglebius * the same check has already done by HDLC and/or other layer. 506165619Sglebius */ 507165619Sglebius lenn = htons(len); 508165619Sglebius fcs = Crc16(PPP_INITFCS, (u_char *)&lenn, 2); 509165619Sglebius fcs = Crc16(fcs, priv->outbuf, len); 510165619Sglebius fcs = Crc16(fcs, priv->inbuf + inlen - 2, 2); 511165619Sglebius 512165619Sglebius if (fcs != PPP_GOODFCS) { 513165619Sglebius priv->stats.Errors++; 514165619Sglebius log(LOG_NOTICE, "ng_pred1: Pred1: Bad CRC-16\n"); 515165619Sglebius return (EIO); 516165619Sglebius } 517165619Sglebius 518165619Sglebius /* Return packet in an mbuf. */ 519165619Sglebius *resultp = m_devget((caddr_t)priv->outbuf, len, 0, NULL, NULL); 520165619Sglebius if (*resultp == NULL) { 521165619Sglebius priv->stats.Errors++; 522165619Sglebius return (ENOMEM); 523165619Sglebius }; 524165619Sglebius 525165619Sglebius } else { 526165619Sglebius priv->stats.FramesUncomp++; 527165619Sglebius if (len != (inlen - 4)) { 528165619Sglebius /* Wrong length. Send reset request */ 529165619Sglebius priv->stats.Errors++; 530165619Sglebius log(LOG_NOTICE, "ng_pred1: Uncomp length error (%d) " 531165619Sglebius "--> len (%d)\n", len, inlen - 4); 532165619Sglebius NG_FREE_M(m); 533165619Sglebius return (EIO); 534165619Sglebius } 535165619Sglebius Pred1SyncTable(node, priv->inbuf + 2, len); 536165619Sglebius m_adj(m, 2); /* Strip length. */ 537165619Sglebius m_adj(m, -2); /* Strip fcs. */ 538165619Sglebius *resultp = m; 539165619Sglebius } 540165619Sglebius 541165619Sglebius priv->stats.FramesPlain++; 542165619Sglebius priv->stats.OutOctets += len; 543165619Sglebius 544165619Sglebius return (0); 545165619Sglebius} 546165619Sglebius 547165619Sglebius/* 548165619Sglebius * Pred1Init() 549165619Sglebius */ 550165619Sglebius 551165619Sglebiusstatic void 552165619SglebiusPred1Init(node_p node) 553165619Sglebius{ 554165619Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 555165619Sglebius 556165619Sglebius priv->Hash = 0; 557165619Sglebius memset(priv->GuessTable, 0, PRED1_TABLE_SIZE); 558165619Sglebius} 559165619Sglebius 560165619Sglebius/* 561165619Sglebius * Pred1Compress() 562165619Sglebius */ 563165619Sglebius 564165619Sglebiusstatic int 565165619SglebiusPred1Compress(node_p node, u_char *source, u_char *dest, int len) 566165619Sglebius{ 567165619Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 568165619Sglebius int i, bitmask; 569165619Sglebius u_char flags; 570165619Sglebius u_char *flagdest, *orgdest; 571165619Sglebius 572165619Sglebius orgdest = dest; 573165619Sglebius while (len) { 574165619Sglebius flagdest = dest++; 575165619Sglebius flags = 0; /* All guesses are wrong initially. */ 576165619Sglebius for (bitmask = 1, i = 0; i < 8 && len; i++, bitmask <<= 1) { 577165619Sglebius if (priv->GuessTable[priv->Hash] == *source) 578165619Sglebius /* Guess was right - don't output. */ 579165619Sglebius flags |= bitmask; 580165619Sglebius else { 581165619Sglebius /* Guess wrong, output char. */ 582165619Sglebius priv->GuessTable[priv->Hash] = *source; 583165619Sglebius *dest++ = *source; 584165619Sglebius } 585165619Sglebius HASH(*source++); 586165619Sglebius len--; 587165619Sglebius } 588165619Sglebius *flagdest = flags; 589165619Sglebius } 590165619Sglebius return (dest - orgdest); 591165619Sglebius} 592165619Sglebius 593165619Sglebius/* 594165619Sglebius * Pred1Decompress() 595165619Sglebius * 596165619Sglebius * Returns decompressed size, or -1 if we ran out of space. 597165619Sglebius */ 598165619Sglebius 599165619Sglebiusstatic int 600165619SglebiusPred1Decompress(node_p node, u_char *source, u_char *dest, int slen, int dlen) 601165619Sglebius{ 602165619Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 603165619Sglebius int i, bitmask; 604165619Sglebius u_char flags, *orgdest; 605165619Sglebius 606165619Sglebius orgdest = dest; 607165619Sglebius while (slen) { 608165619Sglebius flags = *source++; 609165619Sglebius slen--; 610165619Sglebius for (i = 0, bitmask = 1; i < 8; i++, bitmask <<= 1) { 611165619Sglebius if (dlen <= 0) 612165619Sglebius return(-1); 613165619Sglebius if (flags & bitmask) 614165619Sglebius /* Guess correct */ 615165619Sglebius *dest = priv->GuessTable[priv->Hash]; 616165619Sglebius else { 617165619Sglebius if (!slen) 618165619Sglebius /* We seem to be really done -- cabo. */ 619165619Sglebius break; 620165619Sglebius 621165619Sglebius /* Guess wrong. */ 622165619Sglebius priv->GuessTable[priv->Hash] = *source; 623165619Sglebius /* Read from source. */ 624165619Sglebius *dest = *source++; 625165619Sglebius slen--; 626165619Sglebius } 627165619Sglebius HASH(*dest++); 628165619Sglebius dlen--; 629165619Sglebius } 630165619Sglebius } 631165619Sglebius return (dest - orgdest); 632165619Sglebius} 633165619Sglebius 634165619Sglebius/* 635165619Sglebius * Pred1SyncTable() 636165619Sglebius */ 637165619Sglebius 638165619Sglebiusstatic void 639165619SglebiusPred1SyncTable(node_p node, u_char *source, int len) 640165619Sglebius{ 641165619Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 642165619Sglebius 643165619Sglebius while (len--) { 644165619Sglebius priv->GuessTable[priv->Hash] = *source; 645165619Sglebius HASH(*source++); 646165619Sglebius } 647165619Sglebius} 648165619Sglebius 649165619Sglebius/* 650165619Sglebius * Crc16() 651165619Sglebius * 652165619Sglebius * Compute the 16 bit frame check value, per RFC 1171 Appendix B, 653165619Sglebius * on an array of bytes. 654165619Sglebius */ 655165619Sglebius 656165619Sglebiusstatic uint16_t 657165619SglebiusCrc16(uint16_t crc, u_char *cp, int len) 658165619Sglebius{ 659165619Sglebius while (len--) 660165619Sglebius crc = (crc >> 8) ^ Crc16Table[(crc ^ *cp++) & 0xff]; 661165619Sglebius return (crc); 662165619Sglebius} 663165619Sglebius 664165619Sglebiusstatic const uint16_t Crc16Table[256] = { 665165619Sglebius/* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 666165619Sglebius/* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 667165619Sglebius/* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 668165619Sglebius/* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 669165619Sglebius/* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 670165619Sglebius/* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 671165619Sglebius/* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 672165619Sglebius/* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 673165619Sglebius/* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 674165619Sglebius/* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 675165619Sglebius/* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 676165619Sglebius/* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 677165619Sglebius/* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 678165619Sglebius/* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 679165619Sglebius/* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 680165619Sglebius/* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 681165619Sglebius/* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 682165619Sglebius/* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 683165619Sglebius/* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 684165619Sglebius/* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 685165619Sglebius/* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 686165619Sglebius/* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 687165619Sglebius/* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 688165619Sglebius/* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 689165619Sglebius/* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 690165619Sglebius/* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 691165619Sglebius/* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 692165619Sglebius/* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 693165619Sglebius/* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 694165619Sglebius/* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 695165619Sglebius/* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 696165619Sglebius/* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 697165619Sglebius}; 698165619Sglebius 699