2 * Copyright (c) 2006 Alexander Motin <mav@alkar.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Deflate PPP compression netgraph node type.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/endian.h>
40 #include <sys/errno.h>
41 #include <sys/syslog.h>
45 #include <netgraph/ng_message.h>
46 #include <netgraph/netgraph.h>
47 #include <netgraph/ng_parse.h>
48 #include <netgraph/ng_deflate.h>
50 #include "opt_netgraph.h"
52 static MALLOC_DEFINE(M_NETGRAPH_DEFLATE, "netgraph_deflate",
53 "netgraph deflate node");
55 /* DEFLATE header length */
56 #define DEFLATE_HDRLEN 2
58 #define PROT_COMPD 0x00fd
60 #define DEFLATE_BUF_SIZE 4096
62 /* Node private data */
63 struct ng_deflate_private {
64 struct ng_deflate_config cfg; /* configuration */
65 u_char inbuf[DEFLATE_BUF_SIZE]; /* input buffer */
66 u_char outbuf[DEFLATE_BUF_SIZE]; /* output buffer */
67 z_stream cx; /* compression context */
68 struct ng_deflate_stats stats; /* statistics */
69 ng_ID_t ctrlnode; /* path to controlling node */
70 uint16_t seqnum; /* sequence number */
71 u_char compress; /* compress/decompress flag */
73 typedef struct ng_deflate_private *priv_p;
75 /* Netgraph node methods */
76 static ng_constructor_t ng_deflate_constructor;
77 static ng_rcvmsg_t ng_deflate_rcvmsg;
78 static ng_shutdown_t ng_deflate_shutdown;
79 static ng_newhook_t ng_deflate_newhook;
80 static ng_rcvdata_t ng_deflate_rcvdata;
81 static ng_disconnect_t ng_deflate_disconnect;
83 /* Helper functions */
84 static void *z_alloc(void *, u_int items, u_int size);
85 static void z_free(void *, void *ptr);
86 static int ng_deflate_compress(node_p node,
87 struct mbuf *m, struct mbuf **resultp);
88 static int ng_deflate_decompress(node_p node,
89 struct mbuf *m, struct mbuf **resultp);
90 static void ng_deflate_reset_req(node_p node);
92 /* Parse type for struct ng_deflate_config. */
93 static const struct ng_parse_struct_field ng_deflate_config_type_fields[]
94 = NG_DEFLATE_CONFIG_INFO;
95 static const struct ng_parse_type ng_deflate_config_type = {
96 &ng_parse_struct_type,
97 ng_deflate_config_type_fields
100 /* Parse type for struct ng_deflate_stat. */
101 static const struct ng_parse_struct_field ng_deflate_stats_type_fields[]
102 = NG_DEFLATE_STATS_INFO;
103 static const struct ng_parse_type ng_deflate_stat_type = {
104 &ng_parse_struct_type,
105 ng_deflate_stats_type_fields
108 /* List of commands and how to convert arguments to/from ASCII. */
109 static const struct ng_cmdlist ng_deflate_cmds[] = {
114 &ng_deflate_config_type,
119 NGM_DEFLATE_RESETREQ,
126 NGM_DEFLATE_GET_STATS,
129 &ng_deflate_stat_type
133 NGM_DEFLATE_CLR_STATS,
140 NGM_DEFLATE_GETCLR_STATS,
143 &ng_deflate_stat_type
148 /* Node type descriptor */
149 static struct ng_type ng_deflate_typestruct = {
150 .version = NG_ABI_VERSION,
151 .name = NG_DEFLATE_NODE_TYPE,
152 .constructor = ng_deflate_constructor,
153 .rcvmsg = ng_deflate_rcvmsg,
154 .shutdown = ng_deflate_shutdown,
155 .newhook = ng_deflate_newhook,
156 .rcvdata = ng_deflate_rcvdata,
157 .disconnect = ng_deflate_disconnect,
158 .cmdlist = ng_deflate_cmds,
160 NETGRAPH_INIT(deflate, &ng_deflate_typestruct);
162 /* Depend on separate zlib module. */
163 MODULE_DEPEND(ng_deflate, zlib, 1, 1, 1);
165 #define ERROUT(x) do { error = (x); goto done; } while (0)
167 /************************************************************************
169 ************************************************************************/
172 * Node type constructor
175 ng_deflate_constructor(node_p node)
179 /* Allocate private structure. */
180 priv = malloc(sizeof(*priv), M_NETGRAPH_DEFLATE, M_WAITOK | M_ZERO);
182 NG_NODE_SET_PRIVATE(node, priv);
184 /* This node is not thread safe. */
185 NG_NODE_FORCE_WRITER(node);
192 * Give our OK for a hook to be added.
195 ng_deflate_newhook(node_p node, hook_p hook, const char *name)
197 const priv_p priv = NG_NODE_PRIVATE(node);
199 if (NG_NODE_NUMHOOKS(node) > 0)
202 if (strcmp(name, NG_DEFLATE_HOOK_COMP) == 0)
204 else if (strcmp(name, NG_DEFLATE_HOOK_DECOMP) == 0)
213 * Receive a control message
216 ng_deflate_rcvmsg(node_p node, item_p item, hook_p lasthook)
218 const priv_p priv = NG_NODE_PRIVATE(node);
219 struct ng_mesg *resp = NULL;
223 NGI_GET_MSG(item, msg);
225 if (msg->header.typecookie != NGM_DEFLATE_COOKIE)
228 switch (msg->header.cmd) {
229 case NGM_DEFLATE_CONFIG:
231 struct ng_deflate_config *const cfg
232 = (struct ng_deflate_config *)msg->data;
234 /* Check configuration. */
235 if (msg->header.arglen != sizeof(*cfg))
238 if (cfg->windowBits < 8 || cfg->windowBits > 15)
243 /* Clear previous state. */
244 if (priv->cfg.enable) {
246 deflateEnd(&priv->cx);
248 inflateEnd(&priv->cx);
249 priv->cfg.enable = 0;
252 /* Configuration is OK, reset to it. */
255 if (priv->cfg.enable) {
256 priv->cx.next_in = NULL;
257 priv->cx.zalloc = z_alloc;
258 priv->cx.zfree = z_free;
260 if (priv->compress) {
261 if ((res = deflateInit2(&priv->cx,
262 Z_DEFAULT_COMPRESSION, Z_DEFLATED,
264 Z_DEFAULT_STRATEGY)) != Z_OK) {
266 "deflateInit2: error %d, %s\n",
268 priv->cfg.enable = 0;
272 if ((res = inflateInit2(&priv->cx,
273 -cfg->windowBits)) != Z_OK) {
275 "inflateInit2: error %d, %s\n",
277 priv->cfg.enable = 0;
283 /* Initialize other state. */
286 /* Save return address so we can send reset-req's */
287 priv->ctrlnode = NGI_RETADDR(item);
291 case NGM_DEFLATE_RESETREQ:
292 ng_deflate_reset_req(node);
295 case NGM_DEFLATE_GET_STATS:
296 case NGM_DEFLATE_CLR_STATS:
297 case NGM_DEFLATE_GETCLR_STATS:
298 /* Create response if requested. */
299 if (msg->header.cmd != NGM_DEFLATE_CLR_STATS) {
300 NG_MKRESPONSE(resp, msg,
301 sizeof(struct ng_deflate_stats), M_NOWAIT);
304 bcopy(&priv->stats, resp->data,
305 sizeof(struct ng_deflate_stats));
308 /* Clear stats if requested. */
309 if (msg->header.cmd != NGM_DEFLATE_GET_STATS)
311 sizeof(struct ng_deflate_stats));
319 NG_RESPOND_MSG(error, node, item, resp);
325 * Receive incoming data on our hook.
328 ng_deflate_rcvdata(hook_p hook, item_p item)
330 const node_p node = NG_HOOK_NODE(hook);
331 const priv_p priv = NG_NODE_PRIVATE(node);
332 struct mbuf *m, *out;
335 if (!priv->cfg.enable) {
342 if (priv->compress) {
343 if ((error = ng_deflate_compress(node, m, &out)) != 0) {
345 log(LOG_NOTICE, "%s: error: %d\n", __func__, error);
349 } else { /* Decompress */
350 if ((error = ng_deflate_decompress(node, m, &out)) != 0) {
352 log(LOG_NOTICE, "%s: error: %d\n", __func__, error);
353 if (priv->ctrlnode != 0) {
356 /* Need to send a reset-request. */
357 NG_MKMESSAGE(msg, NGM_DEFLATE_COOKIE,
358 NGM_DEFLATE_RESETREQ, 0, M_NOWAIT);
361 NG_SEND_MSG_ID(error, node, msg,
368 NG_FWD_NEW_DATA(error, item, hook, out);
376 ng_deflate_shutdown(node_p node)
378 const priv_p priv = NG_NODE_PRIVATE(node);
380 /* Take down netgraph node. */
381 if (priv->cfg.enable) {
383 deflateEnd(&priv->cx);
385 inflateEnd(&priv->cx);
388 free(priv, M_NETGRAPH_DEFLATE);
389 NG_NODE_SET_PRIVATE(node, NULL);
390 NG_NODE_UNREF(node); /* let the node escape */
398 ng_deflate_disconnect(hook_p hook)
400 const node_p node = NG_HOOK_NODE(hook);
401 const priv_p priv = NG_NODE_PRIVATE(node);
403 if (priv->cfg.enable) {
405 deflateEnd(&priv->cx);
407 inflateEnd(&priv->cx);
408 priv->cfg.enable = 0;
411 /* Go away if no longer connected. */
412 if ((NG_NODE_NUMHOOKS(node) == 0) && NG_NODE_IS_VALID(node))
413 ng_rmnode_self(node);
417 /************************************************************************
419 ************************************************************************/
422 * Space allocation and freeing routines for use by zlib routines.
426 z_alloc(void *notused, u_int items, u_int size)
429 return (malloc(items * size, M_NETGRAPH_DEFLATE, M_NOWAIT));
433 z_free(void *notused, void *ptr)
436 free(ptr, M_NETGRAPH_DEFLATE);
440 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
441 * The original mbuf is not free'd.
444 ng_deflate_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
446 const priv_p priv = NG_NODE_PRIVATE(node);
453 inlen = m->m_pkthdr.len;
455 priv->stats.FramesPlain++;
456 priv->stats.InOctets+=inlen;
458 if (inlen > DEFLATE_BUF_SIZE) {
459 priv->stats.Errors++;
464 /* We must own the mbuf chain exclusively to modify it. */
465 m = m_unshare(m, M_NOWAIT);
467 priv->stats.Errors++;
471 /* Work with contiguous regions of memory. */
472 m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
473 outlen = DEFLATE_BUF_SIZE;
475 /* Compress "inbuf" into "outbuf". */
476 /* Prepare to compress. */
477 if (priv->inbuf[0] != 0) {
478 priv->cx.next_in = priv->inbuf;
479 priv->cx.avail_in = inlen;
481 priv->cx.next_in = priv->inbuf + 1; /* compress protocol */
482 priv->cx.avail_in = inlen - 1;
484 priv->cx.next_out = priv->outbuf + 2 + DEFLATE_HDRLEN;
485 priv->cx.avail_out = outlen - 2 - DEFLATE_HDRLEN;
488 rtn = deflate(&priv->cx, Z_PACKET_FLUSH);
490 /* Check return value. */
492 priv->stats.Errors++;
493 log(LOG_NOTICE, "ng_deflate: compression error: %d (%s)\n",
499 /* Calculate resulting size. */
500 outlen -= priv->cx.avail_out;
502 /* If we can't compress this packet, send it as-is. */
503 if (outlen > inlen) {
504 /* Return original packet uncompressed. */
506 priv->stats.FramesUncomp++;
507 priv->stats.OutOctets+=inlen;
509 /* Install header. */
510 be16enc(priv->outbuf, PROT_COMPD);
511 be16enc(priv->outbuf + 2, priv->seqnum);
513 /* Return packet in an mbuf. */
514 m_copyback(m, 0, outlen, (caddr_t)priv->outbuf);
515 if (m->m_pkthdr.len < outlen) {
517 priv->stats.Errors++;
519 } else if (outlen < m->m_pkthdr.len)
520 m_adj(m, outlen - m->m_pkthdr.len);
522 priv->stats.FramesComp++;
523 priv->stats.OutOctets+=outlen;
526 /* Update sequence number. */
533 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
534 * The original mbuf is not free'd.
537 ng_deflate_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
539 const priv_p priv = NG_NODE_PRIVATE(node);
549 inlen = m->m_pkthdr.len;
551 if (inlen > DEFLATE_BUF_SIZE) {
552 priv->stats.Errors++;
558 /* We must own the mbuf chain exclusively to modify it. */
559 m = m_unshare(m, M_NOWAIT);
561 priv->stats.Errors++;
565 /* Work with contiguous regions of memory. */
566 m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
568 /* Separate proto. */
569 if ((priv->inbuf[0] & 0x01) != 0) {
570 proto = priv->inbuf[0];
573 proto = be16dec(priv->inbuf);
577 priv->stats.InOctets += inlen;
579 /* Packet is compressed, so decompress. */
580 if (proto == PROT_COMPD) {
581 priv->stats.FramesComp++;
583 /* Check sequence number. */
584 rseqnum = be16dec(priv->inbuf + offset);
586 if (rseqnum != priv->seqnum) {
587 priv->stats.Errors++;
588 log(LOG_NOTICE, "ng_deflate: wrong sequence: %u "
589 "instead of %u\n", rseqnum, priv->seqnum);
595 outlen = DEFLATE_BUF_SIZE;
597 /* Decompress "inbuf" into "outbuf". */
598 /* Prepare to decompress. */
599 priv->cx.next_in = priv->inbuf + offset;
600 priv->cx.avail_in = inlen - offset;
601 /* Reserve space for protocol decompression. */
602 priv->cx.next_out = priv->outbuf + 1;
603 priv->cx.avail_out = outlen - 1;
606 rtn = inflate(&priv->cx, Z_PACKET_FLUSH);
608 /* Check return value. */
609 if (rtn != Z_OK && rtn != Z_STREAM_END) {
610 priv->stats.Errors++;
613 log(LOG_NOTICE, "%s: decompression error: %d (%s)\n",
614 __func__, rtn, priv->cx.msg);
626 /* Calculate resulting size. */
627 outlen -= priv->cx.avail_out;
629 /* Decompress protocol. */
630 if ((priv->outbuf[1] & 0x01) != 0) {
632 /* Return packet in an mbuf. */
633 m_copyback(m, 0, outlen, (caddr_t)priv->outbuf);
636 /* Return packet in an mbuf. */
637 m_copyback(m, 0, outlen, (caddr_t)(priv->outbuf + 1));
639 if (m->m_pkthdr.len < outlen) {
641 priv->stats.Errors++;
644 } else if (outlen < m->m_pkthdr.len)
645 m_adj(m, outlen - m->m_pkthdr.len);
647 priv->stats.FramesPlain++;
648 priv->stats.OutOctets+=outlen;
650 } else { /* Packet is not compressed, just update dictionary. */
651 priv->stats.FramesUncomp++;
652 if (priv->inbuf[0] == 0) {
653 priv->cx.next_in = priv->inbuf + 1; /* compress protocol */
654 priv->cx.avail_in = inlen - 1;
656 priv->cx.next_in = priv->inbuf;
657 priv->cx.avail_in = inlen;
660 rtn = inflateIncomp(&priv->cx);
662 /* Check return value */
664 priv->stats.Errors++;
665 log(LOG_NOTICE, "%s: inflateIncomp error: %d (%s)\n",
666 __func__, rtn, priv->cx.msg);
673 priv->stats.FramesPlain++;
674 priv->stats.OutOctets += inlen;
677 /* Update sequence number. */
684 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
687 ng_deflate_reset_req(node_p node)
689 const priv_p priv = NG_NODE_PRIVATE(node);
692 if (priv->cfg.enable) {
694 deflateReset(&priv->cx);
696 inflateReset(&priv->cx);