2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Alexander Motin <mav@alkar.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Deflate PPP compression netgraph node type.
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/endian.h>
42 #include <sys/errno.h>
43 #include <sys/syslog.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/netgraph.h>
48 #include <netgraph/ng_parse.h>
49 #include <netgraph/ng_deflate.h>
51 #include "opt_netgraph.h"
53 static MALLOC_DEFINE(M_NETGRAPH_DEFLATE, "netgraph_deflate",
54 "netgraph deflate node");
56 /* DEFLATE header length */
57 #define DEFLATE_HDRLEN 2
59 #define PROT_COMPD 0x00fd
61 #define DEFLATE_BUF_SIZE 4096
63 /* Node private data */
64 struct ng_deflate_private {
65 struct ng_deflate_config cfg; /* configuration */
66 u_char inbuf[DEFLATE_BUF_SIZE]; /* input buffer */
67 u_char outbuf[DEFLATE_BUF_SIZE]; /* output buffer */
68 z_stream cx; /* compression context */
69 struct ng_deflate_stats stats; /* statistics */
70 ng_ID_t ctrlnode; /* path to controlling node */
71 uint16_t seqnum; /* sequence number */
72 u_char compress; /* compress/decompress flag */
74 typedef struct ng_deflate_private *priv_p;
76 /* Netgraph node methods */
77 static ng_constructor_t ng_deflate_constructor;
78 static ng_rcvmsg_t ng_deflate_rcvmsg;
79 static ng_shutdown_t ng_deflate_shutdown;
80 static ng_newhook_t ng_deflate_newhook;
81 static ng_rcvdata_t ng_deflate_rcvdata;
82 static ng_disconnect_t ng_deflate_disconnect;
84 /* Helper functions */
85 static void *z_alloc(void *, u_int items, u_int size);
86 static void z_free(void *, void *ptr);
87 static int ng_deflate_compress(node_p node,
88 struct mbuf *m, struct mbuf **resultp);
89 static int ng_deflate_decompress(node_p node,
90 struct mbuf *m, struct mbuf **resultp);
91 static void ng_deflate_reset_req(node_p node);
93 /* Parse type for struct ng_deflate_config. */
94 static const struct ng_parse_struct_field ng_deflate_config_type_fields[]
95 = NG_DEFLATE_CONFIG_INFO;
96 static const struct ng_parse_type ng_deflate_config_type = {
97 &ng_parse_struct_type,
98 ng_deflate_config_type_fields
101 /* Parse type for struct ng_deflate_stat. */
102 static const struct ng_parse_struct_field ng_deflate_stats_type_fields[]
103 = NG_DEFLATE_STATS_INFO;
104 static const struct ng_parse_type ng_deflate_stat_type = {
105 &ng_parse_struct_type,
106 ng_deflate_stats_type_fields
109 /* List of commands and how to convert arguments to/from ASCII. */
110 static const struct ng_cmdlist ng_deflate_cmds[] = {
115 &ng_deflate_config_type,
120 NGM_DEFLATE_RESETREQ,
127 NGM_DEFLATE_GET_STATS,
130 &ng_deflate_stat_type
134 NGM_DEFLATE_CLR_STATS,
141 NGM_DEFLATE_GETCLR_STATS,
144 &ng_deflate_stat_type
149 /* Node type descriptor */
150 static struct ng_type ng_deflate_typestruct = {
151 .version = NG_ABI_VERSION,
152 .name = NG_DEFLATE_NODE_TYPE,
153 .constructor = ng_deflate_constructor,
154 .rcvmsg = ng_deflate_rcvmsg,
155 .shutdown = ng_deflate_shutdown,
156 .newhook = ng_deflate_newhook,
157 .rcvdata = ng_deflate_rcvdata,
158 .disconnect = ng_deflate_disconnect,
159 .cmdlist = ng_deflate_cmds,
161 NETGRAPH_INIT(deflate, &ng_deflate_typestruct);
163 /* Depend on separate zlib module. */
164 MODULE_DEPEND(ng_deflate, zlib, 1, 1, 1);
166 #define ERROUT(x) do { error = (x); goto done; } while (0)
168 /************************************************************************
170 ************************************************************************/
173 * Node type constructor
176 ng_deflate_constructor(node_p node)
180 /* Allocate private structure. */
181 priv = malloc(sizeof(*priv), M_NETGRAPH_DEFLATE, M_WAITOK | M_ZERO);
183 NG_NODE_SET_PRIVATE(node, priv);
185 /* This node is not thread safe. */
186 NG_NODE_FORCE_WRITER(node);
193 * Give our OK for a hook to be added.
196 ng_deflate_newhook(node_p node, hook_p hook, const char *name)
198 const priv_p priv = NG_NODE_PRIVATE(node);
200 if (NG_NODE_NUMHOOKS(node) > 0)
203 if (strcmp(name, NG_DEFLATE_HOOK_COMP) == 0)
205 else if (strcmp(name, NG_DEFLATE_HOOK_DECOMP) == 0)
214 * Receive a control message
217 ng_deflate_rcvmsg(node_p node, item_p item, hook_p lasthook)
219 const priv_p priv = NG_NODE_PRIVATE(node);
220 struct ng_mesg *resp = NULL;
224 NGI_GET_MSG(item, msg);
226 if (msg->header.typecookie != NGM_DEFLATE_COOKIE)
229 switch (msg->header.cmd) {
230 case NGM_DEFLATE_CONFIG:
232 struct ng_deflate_config *const cfg
233 = (struct ng_deflate_config *)msg->data;
235 /* Check configuration. */
236 if (msg->header.arglen != sizeof(*cfg))
239 if (cfg->windowBits < 8 || cfg->windowBits > 15)
244 /* Clear previous state. */
245 if (priv->cfg.enable) {
247 deflateEnd(&priv->cx);
249 inflateEnd(&priv->cx);
250 priv->cfg.enable = 0;
253 /* Configuration is OK, reset to it. */
256 if (priv->cfg.enable) {
257 priv->cx.next_in = NULL;
258 priv->cx.zalloc = z_alloc;
259 priv->cx.zfree = z_free;
261 if (priv->compress) {
262 if ((res = deflateInit2(&priv->cx,
263 Z_DEFAULT_COMPRESSION, Z_DEFLATED,
265 Z_DEFAULT_STRATEGY)) != Z_OK) {
267 "deflateInit2: error %d, %s\n",
269 priv->cfg.enable = 0;
273 if ((res = inflateInit2(&priv->cx,
274 -cfg->windowBits)) != Z_OK) {
276 "inflateInit2: error %d, %s\n",
278 priv->cfg.enable = 0;
284 /* Initialize other state. */
287 /* Save return address so we can send reset-req's */
288 priv->ctrlnode = NGI_RETADDR(item);
292 case NGM_DEFLATE_RESETREQ:
293 ng_deflate_reset_req(node);
296 case NGM_DEFLATE_GET_STATS:
297 case NGM_DEFLATE_CLR_STATS:
298 case NGM_DEFLATE_GETCLR_STATS:
299 /* Create response if requested. */
300 if (msg->header.cmd != NGM_DEFLATE_CLR_STATS) {
301 NG_MKRESPONSE(resp, msg,
302 sizeof(struct ng_deflate_stats), M_NOWAIT);
305 bcopy(&priv->stats, resp->data,
306 sizeof(struct ng_deflate_stats));
309 /* Clear stats if requested. */
310 if (msg->header.cmd != NGM_DEFLATE_GET_STATS)
312 sizeof(struct ng_deflate_stats));
320 NG_RESPOND_MSG(error, node, item, resp);
326 * Receive incoming data on our hook.
329 ng_deflate_rcvdata(hook_p hook, item_p item)
331 const node_p node = NG_HOOK_NODE(hook);
332 const priv_p priv = NG_NODE_PRIVATE(node);
333 struct mbuf *m, *out;
336 if (!priv->cfg.enable) {
343 if (priv->compress) {
344 if ((error = ng_deflate_compress(node, m, &out)) != 0) {
346 log(LOG_NOTICE, "%s: error: %d\n", __func__, error);
350 } else { /* Decompress */
351 if ((error = ng_deflate_decompress(node, m, &out)) != 0) {
353 log(LOG_NOTICE, "%s: error: %d\n", __func__, error);
354 if (priv->ctrlnode != 0) {
357 /* Need to send a reset-request. */
358 NG_MKMESSAGE(msg, NGM_DEFLATE_COOKIE,
359 NGM_DEFLATE_RESETREQ, 0, M_NOWAIT);
362 NG_SEND_MSG_ID(error, node, msg,
369 NG_FWD_NEW_DATA(error, item, hook, out);
377 ng_deflate_shutdown(node_p node)
379 const priv_p priv = NG_NODE_PRIVATE(node);
381 /* Take down netgraph node. */
382 if (priv->cfg.enable) {
384 deflateEnd(&priv->cx);
386 inflateEnd(&priv->cx);
389 free(priv, M_NETGRAPH_DEFLATE);
390 NG_NODE_SET_PRIVATE(node, NULL);
391 NG_NODE_UNREF(node); /* let the node escape */
399 ng_deflate_disconnect(hook_p hook)
401 const node_p node = NG_HOOK_NODE(hook);
402 const priv_p priv = NG_NODE_PRIVATE(node);
404 if (priv->cfg.enable) {
406 deflateEnd(&priv->cx);
408 inflateEnd(&priv->cx);
409 priv->cfg.enable = 0;
412 /* Go away if no longer connected. */
413 if ((NG_NODE_NUMHOOKS(node) == 0) && NG_NODE_IS_VALID(node))
414 ng_rmnode_self(node);
418 /************************************************************************
420 ************************************************************************/
423 * Space allocation and freeing routines for use by zlib routines.
427 z_alloc(void *notused, u_int items, u_int size)
430 return (malloc(items * size, M_NETGRAPH_DEFLATE, M_NOWAIT));
434 z_free(void *notused, void *ptr)
437 free(ptr, M_NETGRAPH_DEFLATE);
441 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
442 * The original mbuf is not free'd.
445 ng_deflate_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
447 const priv_p priv = NG_NODE_PRIVATE(node);
454 inlen = m->m_pkthdr.len;
456 priv->stats.FramesPlain++;
457 priv->stats.InOctets+=inlen;
459 if (inlen > DEFLATE_BUF_SIZE) {
460 priv->stats.Errors++;
465 /* We must own the mbuf chain exclusively to modify it. */
466 m = m_unshare(m, M_NOWAIT);
468 priv->stats.Errors++;
472 /* Work with contiguous regions of memory. */
473 m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
474 outlen = DEFLATE_BUF_SIZE;
476 /* Compress "inbuf" into "outbuf". */
477 /* Prepare to compress. */
478 if (priv->inbuf[0] != 0) {
479 priv->cx.next_in = priv->inbuf;
480 priv->cx.avail_in = inlen;
482 priv->cx.next_in = priv->inbuf + 1; /* compress protocol */
483 priv->cx.avail_in = inlen - 1;
485 priv->cx.next_out = priv->outbuf + 2 + DEFLATE_HDRLEN;
486 priv->cx.avail_out = outlen - 2 - DEFLATE_HDRLEN;
489 rtn = deflate(&priv->cx, Z_PACKET_FLUSH);
491 /* Check return value. */
493 priv->stats.Errors++;
494 log(LOG_NOTICE, "ng_deflate: compression error: %d (%s)\n",
500 /* Calculate resulting size. */
501 outlen -= priv->cx.avail_out;
503 /* If we can't compress this packet, send it as-is. */
504 if (outlen > inlen) {
505 /* Return original packet uncompressed. */
507 priv->stats.FramesUncomp++;
508 priv->stats.OutOctets+=inlen;
510 /* Install header. */
511 be16enc(priv->outbuf, PROT_COMPD);
512 be16enc(priv->outbuf + 2, priv->seqnum);
514 /* Return packet in an mbuf. */
515 m_copyback(m, 0, outlen, (caddr_t)priv->outbuf);
516 if (m->m_pkthdr.len < outlen) {
518 priv->stats.Errors++;
520 } else if (outlen < m->m_pkthdr.len)
521 m_adj(m, outlen - m->m_pkthdr.len);
523 priv->stats.FramesComp++;
524 priv->stats.OutOctets+=outlen;
527 /* Update sequence number. */
534 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
535 * The original mbuf is not free'd.
538 ng_deflate_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
540 const priv_p priv = NG_NODE_PRIVATE(node);
550 inlen = m->m_pkthdr.len;
552 if (inlen > DEFLATE_BUF_SIZE) {
553 priv->stats.Errors++;
559 /* We must own the mbuf chain exclusively to modify it. */
560 m = m_unshare(m, M_NOWAIT);
562 priv->stats.Errors++;
566 /* Work with contiguous regions of memory. */
567 m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
569 /* Separate proto. */
570 if ((priv->inbuf[0] & 0x01) != 0) {
571 proto = priv->inbuf[0];
574 proto = be16dec(priv->inbuf);
578 priv->stats.InOctets += inlen;
580 /* Packet is compressed, so decompress. */
581 if (proto == PROT_COMPD) {
582 priv->stats.FramesComp++;
584 /* Check sequence number. */
585 rseqnum = be16dec(priv->inbuf + offset);
587 if (rseqnum != priv->seqnum) {
588 priv->stats.Errors++;
589 log(LOG_NOTICE, "ng_deflate: wrong sequence: %u "
590 "instead of %u\n", rseqnum, priv->seqnum);
596 outlen = DEFLATE_BUF_SIZE;
598 /* Decompress "inbuf" into "outbuf". */
599 /* Prepare to decompress. */
600 priv->cx.next_in = priv->inbuf + offset;
601 priv->cx.avail_in = inlen - offset;
602 /* Reserve space for protocol decompression. */
603 priv->cx.next_out = priv->outbuf + 1;
604 priv->cx.avail_out = outlen - 1;
607 rtn = inflate(&priv->cx, Z_PACKET_FLUSH);
609 /* Check return value. */
610 if (rtn != Z_OK && rtn != Z_STREAM_END) {
611 priv->stats.Errors++;
614 log(LOG_NOTICE, "%s: decompression error: %d (%s)\n",
615 __func__, rtn, priv->cx.msg);
627 /* Calculate resulting size. */
628 outlen -= priv->cx.avail_out;
630 /* Decompress protocol. */
631 if ((priv->outbuf[1] & 0x01) != 0) {
633 /* Return packet in an mbuf. */
634 m_copyback(m, 0, outlen, (caddr_t)priv->outbuf);
637 /* Return packet in an mbuf. */
638 m_copyback(m, 0, outlen, (caddr_t)(priv->outbuf + 1));
640 if (m->m_pkthdr.len < outlen) {
642 priv->stats.Errors++;
645 } else if (outlen < m->m_pkthdr.len)
646 m_adj(m, outlen - m->m_pkthdr.len);
648 priv->stats.FramesPlain++;
649 priv->stats.OutOctets+=outlen;
651 } else { /* Packet is not compressed, just update dictionary. */
652 priv->stats.FramesUncomp++;
653 if (priv->inbuf[0] == 0) {
654 priv->cx.next_in = priv->inbuf + 1; /* compress protocol */
655 priv->cx.avail_in = inlen - 1;
657 priv->cx.next_in = priv->inbuf;
658 priv->cx.avail_in = inlen;
661 rtn = inflateIncomp(&priv->cx);
663 /* Check return value */
665 priv->stats.Errors++;
666 log(LOG_NOTICE, "%s: inflateIncomp error: %d (%s)\n",
667 __func__, rtn, priv->cx.msg);
674 priv->stats.FramesPlain++;
675 priv->stats.OutOctets += inlen;
678 /* Update sequence number. */
685 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
688 ng_deflate_reset_req(node_p node)
690 const priv_p priv = NG_NODE_PRIVATE(node);
693 if (priv->cfg.enable) {
695 deflateReset(&priv->cx);
697 inflateReset(&priv->cx);