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>
44 #include <contrib/zlib/zlib.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 int ng_deflate_compress(node_p, struct mbuf *, struct mbuf **);
86 static int ng_deflate_decompress(node_p, struct mbuf *, struct mbuf **);
87 static void ng_deflate_reset_req(node_p);
89 /* Parse type for struct ng_deflate_config. */
90 static const struct ng_parse_struct_field ng_deflate_config_type_fields[]
91 = NG_DEFLATE_CONFIG_INFO;
92 static const struct ng_parse_type ng_deflate_config_type = {
93 &ng_parse_struct_type,
94 ng_deflate_config_type_fields
97 /* Parse type for struct ng_deflate_stat. */
98 static const struct ng_parse_struct_field ng_deflate_stats_type_fields[]
99 = NG_DEFLATE_STATS_INFO;
100 static const struct ng_parse_type ng_deflate_stat_type = {
101 &ng_parse_struct_type,
102 ng_deflate_stats_type_fields
105 /* List of commands and how to convert arguments to/from ASCII. */
106 static const struct ng_cmdlist ng_deflate_cmds[] = {
111 &ng_deflate_config_type,
116 NGM_DEFLATE_RESETREQ,
123 NGM_DEFLATE_GET_STATS,
126 &ng_deflate_stat_type
130 NGM_DEFLATE_CLR_STATS,
137 NGM_DEFLATE_GETCLR_STATS,
140 &ng_deflate_stat_type
145 /* Node type descriptor */
146 static struct ng_type ng_deflate_typestruct = {
147 .version = NG_ABI_VERSION,
148 .name = NG_DEFLATE_NODE_TYPE,
149 .constructor = ng_deflate_constructor,
150 .rcvmsg = ng_deflate_rcvmsg,
151 .shutdown = ng_deflate_shutdown,
152 .newhook = ng_deflate_newhook,
153 .rcvdata = ng_deflate_rcvdata,
154 .disconnect = ng_deflate_disconnect,
155 .cmdlist = ng_deflate_cmds,
157 NETGRAPH_INIT(deflate, &ng_deflate_typestruct);
159 /* Depend on separate zlib module. */
160 MODULE_DEPEND(ng_deflate, zlib, 1, 1, 1);
162 #define ERROUT(x) do { error = (x); goto done; } while (0)
164 /************************************************************************
166 ************************************************************************/
169 * Node type constructor
172 ng_deflate_constructor(node_p node)
176 /* Allocate private structure. */
177 priv = malloc(sizeof(*priv), M_NETGRAPH_DEFLATE, M_WAITOK | M_ZERO);
179 NG_NODE_SET_PRIVATE(node, priv);
181 /* This node is not thread safe. */
182 NG_NODE_FORCE_WRITER(node);
189 * Give our OK for a hook to be added.
192 ng_deflate_newhook(node_p node, hook_p hook, const char *name)
194 const priv_p priv = NG_NODE_PRIVATE(node);
196 if (NG_NODE_NUMHOOKS(node) > 0)
199 if (strcmp(name, NG_DEFLATE_HOOK_COMP) == 0)
201 else if (strcmp(name, NG_DEFLATE_HOOK_DECOMP) == 0)
210 * Receive a control message
213 ng_deflate_rcvmsg(node_p node, item_p item, hook_p lasthook)
215 const priv_p priv = NG_NODE_PRIVATE(node);
216 struct ng_mesg *resp = NULL;
220 NGI_GET_MSG(item, msg);
222 if (msg->header.typecookie != NGM_DEFLATE_COOKIE)
225 switch (msg->header.cmd) {
226 case NGM_DEFLATE_CONFIG:
228 struct ng_deflate_config *const cfg
229 = (struct ng_deflate_config *)msg->data;
231 /* Check configuration. */
232 if (msg->header.arglen != sizeof(*cfg))
235 if (cfg->windowBits < 8 || cfg->windowBits > 15)
240 /* Clear previous state. */
241 if (priv->cfg.enable) {
243 deflateEnd(&priv->cx);
245 inflateEnd(&priv->cx);
246 priv->cfg.enable = 0;
249 /* Configuration is OK, reset to it. */
252 if (priv->cfg.enable) {
253 priv->cx.next_in = NULL;
255 if (priv->compress) {
256 if ((res = deflateInit2(&priv->cx,
257 Z_DEFAULT_COMPRESSION, Z_DEFLATED,
259 Z_DEFAULT_STRATEGY)) != Z_OK) {
261 "deflateInit2: error %d, %s\n",
263 priv->cfg.enable = 0;
267 if ((res = inflateInit2(&priv->cx,
268 -cfg->windowBits)) != Z_OK) {
270 "inflateInit2: error %d, %s\n",
272 priv->cfg.enable = 0;
278 /* Initialize other state. */
281 /* Save return address so we can send reset-req's */
282 priv->ctrlnode = NGI_RETADDR(item);
286 case NGM_DEFLATE_RESETREQ:
287 ng_deflate_reset_req(node);
290 case NGM_DEFLATE_GET_STATS:
291 case NGM_DEFLATE_CLR_STATS:
292 case NGM_DEFLATE_GETCLR_STATS:
293 /* Create response if requested. */
294 if (msg->header.cmd != NGM_DEFLATE_CLR_STATS) {
295 NG_MKRESPONSE(resp, msg,
296 sizeof(struct ng_deflate_stats), M_NOWAIT);
299 bcopy(&priv->stats, resp->data,
300 sizeof(struct ng_deflate_stats));
303 /* Clear stats if requested. */
304 if (msg->header.cmd != NGM_DEFLATE_GET_STATS)
306 sizeof(struct ng_deflate_stats));
314 NG_RESPOND_MSG(error, node, item, resp);
320 * Receive incoming data on our hook.
323 ng_deflate_rcvdata(hook_p hook, item_p item)
325 const node_p node = NG_HOOK_NODE(hook);
326 const priv_p priv = NG_NODE_PRIVATE(node);
327 struct mbuf *m, *out;
330 if (!priv->cfg.enable) {
337 if (priv->compress) {
338 if ((error = ng_deflate_compress(node, m, &out)) != 0) {
340 log(LOG_NOTICE, "%s: error: %d\n", __func__, error);
343 } else { /* Decompress */
344 if ((error = ng_deflate_decompress(node, m, &out)) != 0) {
346 log(LOG_NOTICE, "%s: error: %d\n", __func__, error);
347 if (priv->ctrlnode != 0) {
350 /* Need to send a reset-request. */
351 NG_MKMESSAGE(msg, NGM_DEFLATE_COOKIE,
352 NGM_DEFLATE_RESETREQ, 0, M_NOWAIT);
355 NG_SEND_MSG_ID(error, node, msg,
362 NG_FWD_NEW_DATA(error, item, hook, out);
370 ng_deflate_shutdown(node_p node)
372 const priv_p priv = NG_NODE_PRIVATE(node);
374 /* Take down netgraph node. */
375 if (priv->cfg.enable) {
377 deflateEnd(&priv->cx);
379 inflateEnd(&priv->cx);
382 free(priv, M_NETGRAPH_DEFLATE);
383 NG_NODE_SET_PRIVATE(node, NULL);
384 NG_NODE_UNREF(node); /* let the node escape */
392 ng_deflate_disconnect(hook_p hook)
394 const node_p node = NG_HOOK_NODE(hook);
395 const priv_p priv = NG_NODE_PRIVATE(node);
397 if (priv->cfg.enable) {
399 deflateEnd(&priv->cx);
401 inflateEnd(&priv->cx);
402 priv->cfg.enable = 0;
405 /* Go away if no longer connected. */
406 if ((NG_NODE_NUMHOOKS(node) == 0) && NG_NODE_IS_VALID(node))
407 ng_rmnode_self(node);
411 /************************************************************************
413 ************************************************************************/
416 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
417 * The original mbuf is not free'd.
420 ng_deflate_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
422 const priv_p priv = NG_NODE_PRIVATE(node);
429 inlen = m->m_pkthdr.len;
431 priv->stats.FramesPlain++;
432 priv->stats.InOctets+=inlen;
434 if (inlen > DEFLATE_BUF_SIZE) {
435 priv->stats.Errors++;
440 /* We must own the mbuf chain exclusively to modify it. */
441 m = m_unshare(m, M_NOWAIT);
443 priv->stats.Errors++;
447 /* Work with contiguous regions of memory. */
448 m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
449 outlen = DEFLATE_BUF_SIZE;
451 /* Compress "inbuf" into "outbuf". */
452 /* Prepare to compress. */
453 if (priv->inbuf[0] != 0) {
454 priv->cx.next_in = priv->inbuf;
455 priv->cx.avail_in = inlen;
457 priv->cx.next_in = priv->inbuf + 1; /* compress protocol */
458 priv->cx.avail_in = inlen - 1;
460 priv->cx.next_out = priv->outbuf + 2 + DEFLATE_HDRLEN;
461 priv->cx.avail_out = outlen - 2 - DEFLATE_HDRLEN;
464 rtn = deflate(&priv->cx, Z_SYNC_FLUSH);
466 /* Check return value. */
468 priv->stats.Errors++;
469 log(LOG_NOTICE, "ng_deflate: compression error: %d (%s)\n",
475 /* Calculate resulting size. */
476 outlen -= priv->cx.avail_out;
478 * Z_SYNC_FLUSH completes the current deflate block and follows
479 * it with an empty stored block that is three bits plus filler
480 * bits to the next byte, followed by four bytes (00 00 ff ff).
481 * RFC 1979 Section 2.1, "Data" requires the four bytes be
482 * removed before transmission.
486 MPASS(priv->outbuf[outlen + 0] == 0x00);
487 MPASS(priv->outbuf[outlen + 1] == 0x00);
488 MPASS(priv->outbuf[outlen + 2] == 0xff);
489 MPASS(priv->outbuf[outlen + 3] == 0xff);
491 /* If we can't compress this packet, send it as-is. */
492 if (outlen > inlen) {
493 /* Return original packet uncompressed. */
495 priv->stats.FramesUncomp++;
496 priv->stats.OutOctets+=inlen;
498 /* Install header. */
499 be16enc(priv->outbuf, PROT_COMPD);
500 be16enc(priv->outbuf + 2, priv->seqnum);
502 /* Return packet in an mbuf. */
503 m_copyback(m, 0, outlen, (caddr_t)priv->outbuf);
504 if (m->m_pkthdr.len < outlen) {
506 priv->stats.Errors++;
508 } else if (outlen < m->m_pkthdr.len)
509 m_adj(m, outlen - m->m_pkthdr.len);
511 priv->stats.FramesComp++;
512 priv->stats.OutOctets+=outlen;
515 /* Update sequence number. */
522 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
523 * The original mbuf is not free'd.
526 ng_deflate_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
528 const priv_p priv = NG_NODE_PRIVATE(node);
529 int outlen, inlen, datalen;
535 static u_char EMPTY_BLOCK[4] = { 0x00, 0x00, 0xff, 0xff };
540 inlen = m->m_pkthdr.len;
542 if (inlen > DEFLATE_BUF_SIZE) {
543 priv->stats.Errors++;
549 /* We must own the mbuf chain exclusively to modify it. */
550 m = m_unshare(m, M_NOWAIT);
552 priv->stats.Errors++;
556 /* Work with contiguous regions of memory. */
557 m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
559 /* Separate proto. */
560 if ((priv->inbuf[0] & 0x01) != 0) {
561 proto = priv->inbuf[0];
564 proto = be16dec(priv->inbuf);
568 priv->stats.InOctets += inlen;
570 /* Packet is compressed, so decompress. */
571 if (proto == PROT_COMPD) {
572 priv->stats.FramesComp++;
574 /* Check sequence number. */
575 rseqnum = be16dec(priv->inbuf + offset);
577 if (rseqnum != priv->seqnum) {
578 priv->stats.Errors++;
579 log(LOG_NOTICE, "ng_deflate: wrong sequence: %u "
580 "instead of %u\n", rseqnum, priv->seqnum);
586 outlen = DEFLATE_BUF_SIZE;
588 /* Decompress "inbuf" into "outbuf". */
589 /* Prepare to decompress. */
590 priv->cx.next_in = priv->inbuf + offset;
591 priv->cx.avail_in = inlen - offset;
592 /* Reserve space for protocol decompression. */
593 priv->cx.next_out = priv->outbuf + 1;
594 priv->cx.avail_out = outlen - 1;
597 rtn = inflate(&priv->cx, Z_SYNC_FLUSH);
599 /* Check return value. */
600 if (rtn != Z_OK && rtn != Z_STREAM_END) {
601 priv->stats.Errors++;
604 log(LOG_NOTICE, "%s: decompression error: %d (%s)\n",
605 __func__, rtn, priv->cx.msg);
617 /* Handle the EMPTY_BLOCK omitted by sender */
618 if (inflateSyncPoint(&priv->cx)) {
619 priv->cx.avail_in = 4;
620 priv->cx.next_in = EMPTY_BLOCK;
621 inflate(&priv->cx, Z_SYNC_FLUSH);
624 /* Calculate resulting size. */
625 outlen -= priv->cx.avail_out;
627 /* Decompress protocol. */
628 if ((priv->outbuf[1] & 0x01) != 0) {
630 /* Return packet in an mbuf. */
631 m_copyback(m, 0, outlen, (caddr_t)priv->outbuf);
634 /* Return packet in an mbuf. */
635 m_copyback(m, 0, outlen, (caddr_t)(priv->outbuf + 1));
637 if (m->m_pkthdr.len < outlen) {
639 priv->stats.Errors++;
642 } else if (outlen < m->m_pkthdr.len)
643 m_adj(m, outlen - m->m_pkthdr.len);
645 priv->stats.FramesPlain++;
646 priv->stats.OutOctets+=outlen;
649 /* Packet is not compressed, just update dictionary. */
650 priv->stats.FramesUncomp++;
653 * Fake a header for uncompressed data block
655 datalen = inlen - offset + 1;
657 headbuf[1] = datalen & 0xff;
658 headbuf[2] = datalen >> 8;
659 headbuf[3] = (~datalen) & 0xff;
660 headbuf[4] = (~datalen) >> 8;
662 priv->cx.next_in = headbuf;
663 priv->cx.avail_in = sizeof(headbuf);
664 priv->cx.next_out = priv->outbuf;
665 priv->cx.avail_out = DEFLATE_BUF_SIZE;
667 rtn = inflate(&priv->cx, Z_NO_FLUSH);
669 if (priv->inbuf[0] == 0) {
671 priv->inbuf + 1; /* compress protocol */
672 priv->cx.avail_in = inlen - 1;
674 priv->cx.next_in = priv->inbuf;
675 priv->cx.avail_in = inlen;
677 priv->cx.next_out = priv->outbuf;
678 priv->cx.avail_out = DEFLATE_BUF_SIZE;
680 rtn = inflate(&priv->cx, Z_SYNC_FLUSH);
682 /* Check return value */
684 priv->stats.Errors++;
685 log(LOG_NOTICE, "%s: inflate error: %d (%s)\n",
686 __func__, rtn, priv->cx.msg);
693 priv->stats.FramesPlain++;
694 priv->stats.OutOctets += inlen;
697 /* Update sequence number. */
704 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
707 ng_deflate_reset_req(node_p node)
709 const priv_p priv = NG_NODE_PRIVATE(node);
712 if (priv->cfg.enable) {
714 deflateReset(&priv->cx);
716 inflateReset(&priv->cx);