2 * SPDX-License-Identifier: BSD-2-Clause
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
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>
42 #include <contrib/zlib/zlib.h>
44 #include <netgraph/ng_message.h>
45 #include <netgraph/netgraph.h>
46 #include <netgraph/ng_parse.h>
47 #include <netgraph/ng_deflate.h>
49 #include "opt_netgraph.h"
51 static MALLOC_DEFINE(M_NETGRAPH_DEFLATE, "netgraph_deflate",
52 "netgraph deflate node");
54 /* DEFLATE header length */
55 #define DEFLATE_HDRLEN 2
57 #define PROT_COMPD 0x00fd
59 #define DEFLATE_BUF_SIZE 4096
61 /* Node private data */
62 struct ng_deflate_private {
63 struct ng_deflate_config cfg; /* configuration */
64 u_char inbuf[DEFLATE_BUF_SIZE]; /* input buffer */
65 u_char outbuf[DEFLATE_BUF_SIZE]; /* output buffer */
66 z_stream cx; /* compression context */
67 struct ng_deflate_stats stats; /* statistics */
68 ng_ID_t ctrlnode; /* path to controlling node */
69 uint16_t seqnum; /* sequence number */
70 u_char compress; /* compress/decompress flag */
72 typedef struct ng_deflate_private *priv_p;
74 /* Netgraph node methods */
75 static ng_constructor_t ng_deflate_constructor;
76 static ng_rcvmsg_t ng_deflate_rcvmsg;
77 static ng_shutdown_t ng_deflate_shutdown;
78 static ng_newhook_t ng_deflate_newhook;
79 static ng_rcvdata_t ng_deflate_rcvdata;
80 static ng_disconnect_t ng_deflate_disconnect;
82 /* Helper functions */
83 static int ng_deflate_compress(node_p, struct mbuf *, struct mbuf **);
84 static int ng_deflate_decompress(node_p, struct mbuf *, struct mbuf **);
85 static void ng_deflate_reset_req(node_p);
87 /* Parse type for struct ng_deflate_config. */
88 static const struct ng_parse_struct_field ng_deflate_config_type_fields[]
89 = NG_DEFLATE_CONFIG_INFO;
90 static const struct ng_parse_type ng_deflate_config_type = {
91 &ng_parse_struct_type,
92 ng_deflate_config_type_fields
95 /* Parse type for struct ng_deflate_stat. */
96 static const struct ng_parse_struct_field ng_deflate_stats_type_fields[]
97 = NG_DEFLATE_STATS_INFO;
98 static const struct ng_parse_type ng_deflate_stat_type = {
99 &ng_parse_struct_type,
100 ng_deflate_stats_type_fields
103 /* List of commands and how to convert arguments to/from ASCII. */
104 static const struct ng_cmdlist ng_deflate_cmds[] = {
109 &ng_deflate_config_type,
114 NGM_DEFLATE_RESETREQ,
121 NGM_DEFLATE_GET_STATS,
124 &ng_deflate_stat_type
128 NGM_DEFLATE_CLR_STATS,
135 NGM_DEFLATE_GETCLR_STATS,
138 &ng_deflate_stat_type
143 /* Node type descriptor */
144 static struct ng_type ng_deflate_typestruct = {
145 .version = NG_ABI_VERSION,
146 .name = NG_DEFLATE_NODE_TYPE,
147 .constructor = ng_deflate_constructor,
148 .rcvmsg = ng_deflate_rcvmsg,
149 .shutdown = ng_deflate_shutdown,
150 .newhook = ng_deflate_newhook,
151 .rcvdata = ng_deflate_rcvdata,
152 .disconnect = ng_deflate_disconnect,
153 .cmdlist = ng_deflate_cmds,
155 NETGRAPH_INIT(deflate, &ng_deflate_typestruct);
157 /* Depend on separate zlib module. */
158 MODULE_DEPEND(ng_deflate, zlib, 1, 1, 1);
160 #define ERROUT(x) do { error = (x); goto done; } while (0)
162 /************************************************************************
164 ************************************************************************/
167 * Node type constructor
170 ng_deflate_constructor(node_p node)
174 /* Allocate private structure. */
175 priv = malloc(sizeof(*priv), M_NETGRAPH_DEFLATE, M_WAITOK | M_ZERO);
177 NG_NODE_SET_PRIVATE(node, priv);
179 /* This node is not thread safe. */
180 NG_NODE_FORCE_WRITER(node);
187 * Give our OK for a hook to be added.
190 ng_deflate_newhook(node_p node, hook_p hook, const char *name)
192 const priv_p priv = NG_NODE_PRIVATE(node);
194 if (NG_NODE_NUMHOOKS(node) > 0)
197 if (strcmp(name, NG_DEFLATE_HOOK_COMP) == 0)
199 else if (strcmp(name, NG_DEFLATE_HOOK_DECOMP) == 0)
208 * Receive a control message
211 ng_deflate_rcvmsg(node_p node, item_p item, hook_p lasthook)
213 const priv_p priv = NG_NODE_PRIVATE(node);
214 struct ng_mesg *resp = NULL;
218 NGI_GET_MSG(item, msg);
220 if (msg->header.typecookie != NGM_DEFLATE_COOKIE)
223 switch (msg->header.cmd) {
224 case NGM_DEFLATE_CONFIG:
226 struct ng_deflate_config *const cfg
227 = (struct ng_deflate_config *)msg->data;
229 /* Check configuration. */
230 if (msg->header.arglen != sizeof(*cfg))
233 if (cfg->windowBits < 8 || cfg->windowBits > 15)
238 /* Clear previous state. */
239 if (priv->cfg.enable) {
241 deflateEnd(&priv->cx);
243 inflateEnd(&priv->cx);
244 priv->cfg.enable = 0;
247 /* Configuration is OK, reset to it. */
250 if (priv->cfg.enable) {
251 priv->cx.next_in = NULL;
253 if (priv->compress) {
254 if ((res = deflateInit2(&priv->cx,
255 Z_DEFAULT_COMPRESSION, Z_DEFLATED,
257 Z_DEFAULT_STRATEGY)) != Z_OK) {
259 "deflateInit2: error %d, %s\n",
261 priv->cfg.enable = 0;
265 if ((res = inflateInit2(&priv->cx,
266 -cfg->windowBits)) != Z_OK) {
268 "inflateInit2: error %d, %s\n",
270 priv->cfg.enable = 0;
276 /* Initialize other state. */
279 /* Save return address so we can send reset-req's */
280 priv->ctrlnode = NGI_RETADDR(item);
284 case NGM_DEFLATE_RESETREQ:
285 ng_deflate_reset_req(node);
288 case NGM_DEFLATE_GET_STATS:
289 case NGM_DEFLATE_CLR_STATS:
290 case NGM_DEFLATE_GETCLR_STATS:
291 /* Create response if requested. */
292 if (msg->header.cmd != NGM_DEFLATE_CLR_STATS) {
293 NG_MKRESPONSE(resp, msg,
294 sizeof(struct ng_deflate_stats), M_NOWAIT);
297 bcopy(&priv->stats, resp->data,
298 sizeof(struct ng_deflate_stats));
301 /* Clear stats if requested. */
302 if (msg->header.cmd != NGM_DEFLATE_GET_STATS)
304 sizeof(struct ng_deflate_stats));
312 NG_RESPOND_MSG(error, node, item, resp);
318 * Receive incoming data on our hook.
321 ng_deflate_rcvdata(hook_p hook, item_p item)
323 const node_p node = NG_HOOK_NODE(hook);
324 const priv_p priv = NG_NODE_PRIVATE(node);
325 struct mbuf *m, *out;
328 if (!priv->cfg.enable) {
335 if (priv->compress) {
336 if ((error = ng_deflate_compress(node, m, &out)) != 0) {
338 log(LOG_NOTICE, "%s: error: %d\n", __func__, error);
341 } else { /* Decompress */
342 if ((error = ng_deflate_decompress(node, m, &out)) != 0) {
344 log(LOG_NOTICE, "%s: error: %d\n", __func__, error);
345 if (priv->ctrlnode != 0) {
348 /* Need to send a reset-request. */
349 NG_MKMESSAGE(msg, NGM_DEFLATE_COOKIE,
350 NGM_DEFLATE_RESETREQ, 0, M_NOWAIT);
353 NG_SEND_MSG_ID(error, node, msg,
360 NG_FWD_NEW_DATA(error, item, hook, out);
368 ng_deflate_shutdown(node_p node)
370 const priv_p priv = NG_NODE_PRIVATE(node);
372 /* Take down netgraph node. */
373 if (priv->cfg.enable) {
375 deflateEnd(&priv->cx);
377 inflateEnd(&priv->cx);
380 free(priv, M_NETGRAPH_DEFLATE);
381 NG_NODE_SET_PRIVATE(node, NULL);
382 NG_NODE_UNREF(node); /* let the node escape */
390 ng_deflate_disconnect(hook_p hook)
392 const node_p node = NG_HOOK_NODE(hook);
393 const priv_p priv = NG_NODE_PRIVATE(node);
395 if (priv->cfg.enable) {
397 deflateEnd(&priv->cx);
399 inflateEnd(&priv->cx);
400 priv->cfg.enable = 0;
403 /* Go away if no longer connected. */
404 if ((NG_NODE_NUMHOOKS(node) == 0) && NG_NODE_IS_VALID(node))
405 ng_rmnode_self(node);
409 /************************************************************************
411 ************************************************************************/
414 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
415 * The original mbuf is not free'd.
418 ng_deflate_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
420 const priv_p priv = NG_NODE_PRIVATE(node);
427 inlen = m->m_pkthdr.len;
429 priv->stats.FramesPlain++;
430 priv->stats.InOctets+=inlen;
432 if (inlen > DEFLATE_BUF_SIZE) {
433 priv->stats.Errors++;
438 /* We must own the mbuf chain exclusively to modify it. */
439 m = m_unshare(m, M_NOWAIT);
441 priv->stats.Errors++;
445 /* Work with contiguous regions of memory. */
446 m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
447 outlen = DEFLATE_BUF_SIZE;
449 /* Compress "inbuf" into "outbuf". */
450 /* Prepare to compress. */
451 if (priv->inbuf[0] != 0) {
452 priv->cx.next_in = priv->inbuf;
453 priv->cx.avail_in = inlen;
455 priv->cx.next_in = priv->inbuf + 1; /* compress protocol */
456 priv->cx.avail_in = inlen - 1;
458 priv->cx.next_out = priv->outbuf + 2 + DEFLATE_HDRLEN;
459 priv->cx.avail_out = outlen - 2 - DEFLATE_HDRLEN;
462 rtn = deflate(&priv->cx, Z_SYNC_FLUSH);
464 /* Check return value. */
466 priv->stats.Errors++;
467 log(LOG_NOTICE, "ng_deflate: compression error: %d (%s)\n",
473 /* Calculate resulting size. */
474 outlen -= priv->cx.avail_out;
476 * Z_SYNC_FLUSH completes the current deflate block and follows
477 * it with an empty stored block that is three bits plus filler
478 * bits to the next byte, followed by four bytes (00 00 ff ff).
479 * RFC 1979 Section 2.1, "Data" requires the four bytes be
480 * removed before transmission.
484 MPASS(priv->outbuf[outlen + 0] == 0x00);
485 MPASS(priv->outbuf[outlen + 1] == 0x00);
486 MPASS(priv->outbuf[outlen + 2] == 0xff);
487 MPASS(priv->outbuf[outlen + 3] == 0xff);
489 /* If we can't compress this packet, send it as-is. */
490 if (outlen > inlen) {
491 /* Return original packet uncompressed. */
493 priv->stats.FramesUncomp++;
494 priv->stats.OutOctets+=inlen;
496 /* Install header. */
497 be16enc(priv->outbuf, PROT_COMPD);
498 be16enc(priv->outbuf + 2, priv->seqnum);
500 /* Return packet in an mbuf. */
501 m_copyback(m, 0, outlen, (caddr_t)priv->outbuf);
502 if (m->m_pkthdr.len < outlen) {
504 priv->stats.Errors++;
506 } else if (outlen < m->m_pkthdr.len)
507 m_adj(m, outlen - m->m_pkthdr.len);
509 priv->stats.FramesComp++;
510 priv->stats.OutOctets+=outlen;
513 /* Update sequence number. */
520 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
521 * The original mbuf is not free'd.
524 ng_deflate_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
526 const priv_p priv = NG_NODE_PRIVATE(node);
527 int outlen, inlen, datalen;
533 static u_char EMPTY_BLOCK[4] = { 0x00, 0x00, 0xff, 0xff };
538 inlen = m->m_pkthdr.len;
540 if (inlen > DEFLATE_BUF_SIZE) {
541 priv->stats.Errors++;
547 /* We must own the mbuf chain exclusively to modify it. */
548 m = m_unshare(m, M_NOWAIT);
550 priv->stats.Errors++;
554 /* Work with contiguous regions of memory. */
555 m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
557 /* Separate proto. */
558 if ((priv->inbuf[0] & 0x01) != 0) {
559 proto = priv->inbuf[0];
562 proto = be16dec(priv->inbuf);
566 priv->stats.InOctets += inlen;
568 /* Packet is compressed, so decompress. */
569 if (proto == PROT_COMPD) {
570 priv->stats.FramesComp++;
572 /* Check sequence number. */
573 rseqnum = be16dec(priv->inbuf + offset);
575 if (rseqnum != priv->seqnum) {
576 priv->stats.Errors++;
577 log(LOG_NOTICE, "ng_deflate: wrong sequence: %u "
578 "instead of %u\n", rseqnum, priv->seqnum);
584 outlen = DEFLATE_BUF_SIZE;
586 /* Decompress "inbuf" into "outbuf". */
587 /* Prepare to decompress. */
588 priv->cx.next_in = priv->inbuf + offset;
589 priv->cx.avail_in = inlen - offset;
590 /* Reserve space for protocol decompression. */
591 priv->cx.next_out = priv->outbuf + 1;
592 priv->cx.avail_out = outlen - 1;
595 rtn = inflate(&priv->cx, Z_SYNC_FLUSH);
597 /* Check return value. */
598 if (rtn != Z_OK && rtn != Z_STREAM_END) {
599 priv->stats.Errors++;
602 log(LOG_NOTICE, "%s: decompression error: %d (%s)\n",
603 __func__, rtn, priv->cx.msg);
615 /* Handle the EMPTY_BLOCK omitted by sender */
616 if (inflateSyncPoint(&priv->cx)) {
617 priv->cx.avail_in = 4;
618 priv->cx.next_in = EMPTY_BLOCK;
619 inflate(&priv->cx, Z_SYNC_FLUSH);
622 /* Calculate resulting size. */
623 outlen -= priv->cx.avail_out;
625 /* Decompress protocol. */
626 if ((priv->outbuf[1] & 0x01) != 0) {
628 /* Return packet in an mbuf. */
629 m_copyback(m, 0, outlen, (caddr_t)priv->outbuf);
632 /* Return packet in an mbuf. */
633 m_copyback(m, 0, outlen, (caddr_t)(priv->outbuf + 1));
635 if (m->m_pkthdr.len < outlen) {
637 priv->stats.Errors++;
640 } else if (outlen < m->m_pkthdr.len)
641 m_adj(m, outlen - m->m_pkthdr.len);
643 priv->stats.FramesPlain++;
644 priv->stats.OutOctets+=outlen;
647 /* Packet is not compressed, just update dictionary. */
648 priv->stats.FramesUncomp++;
651 * Fake a header for uncompressed data block
653 datalen = inlen - offset + 1;
655 headbuf[1] = datalen & 0xff;
656 headbuf[2] = datalen >> 8;
657 headbuf[3] = (~datalen) & 0xff;
658 headbuf[4] = (~datalen) >> 8;
660 priv->cx.next_in = headbuf;
661 priv->cx.avail_in = sizeof(headbuf);
662 priv->cx.next_out = priv->outbuf;
663 priv->cx.avail_out = DEFLATE_BUF_SIZE;
665 rtn = inflate(&priv->cx, Z_NO_FLUSH);
667 if (priv->inbuf[0] == 0) {
669 priv->inbuf + 1; /* compress protocol */
670 priv->cx.avail_in = inlen - 1;
672 priv->cx.next_in = priv->inbuf;
673 priv->cx.avail_in = inlen;
675 priv->cx.next_out = priv->outbuf;
676 priv->cx.avail_out = DEFLATE_BUF_SIZE;
678 rtn = inflate(&priv->cx, Z_SYNC_FLUSH);
680 /* Check return value */
682 priv->stats.Errors++;
683 log(LOG_NOTICE, "%s: inflate error: %d (%s)\n",
684 __func__, rtn, priv->cx.msg);
691 priv->stats.FramesPlain++;
692 priv->stats.OutOctets += inlen;
695 /* Update sequence number. */
702 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
705 ng_deflate_reset_req(node_p node)
707 const priv_p priv = NG_NODE_PRIVATE(node);
710 if (priv->cfg.enable) {
712 deflateReset(&priv->cx);
714 inflateReset(&priv->cx);