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
27 * $FreeBSD: src/sys/netgraph/ng_deflate.c,v 1.3 2007/01/15 05:55:56 glebius Exp $
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/errno.h>
40 #include <sys/syslog.h>
44 #include <netgraph7/ng_message.h>
45 #include <netgraph7/netgraph.h>
46 #include <netgraph7/ng_parse.h>
47 #include "ng_deflate.h"
49 #include "opt_netgraph.h"
51 MALLOC_DEFINE(M_NETGRAPH_DEFLATE
, "netgraph_deflate", "netgraph deflate node ");
53 /* DEFLATE header length */
54 #define DEFLATE_HDRLEN 2
56 #define PROT_COMPD 0x00fd
58 #define DEFLATE_BUF_SIZE 4096
60 /* Node private data */
61 struct ng_deflate_private
{
62 struct ng_deflate_config cfg
; /* configuration */
63 u_char inbuf
[DEFLATE_BUF_SIZE
]; /* input buffer */
64 u_char outbuf
[DEFLATE_BUF_SIZE
]; /* output buffer */
65 z_stream cx
; /* compression context */
66 struct ng_deflate_stats stats
; /* statistics */
67 ng_ID_t ctrlnode
; /* path to controlling node */
68 uint16_t seqnum
; /* sequence number */
69 u_char compress
; /* compress/decompress flag */
71 typedef struct ng_deflate_private
*priv_p
;
73 /* Netgraph node methods */
74 static ng_constructor_t ng_deflate_constructor
;
75 static ng_rcvmsg_t ng_deflate_rcvmsg
;
76 static ng_shutdown_t ng_deflate_shutdown
;
77 static ng_newhook_t ng_deflate_newhook
;
78 static ng_rcvdata_t ng_deflate_rcvdata
;
79 static ng_disconnect_t ng_deflate_disconnect
;
81 /* Helper functions */
82 static void *z_alloc(void *, u_int items
, u_int size
);
83 static void z_free(void *, void *ptr
);
84 static int ng_deflate_compress(node_p node
,
85 struct mbuf
*m
, struct mbuf
**resultp
);
86 static int ng_deflate_decompress(node_p node
,
87 struct mbuf
*m
, struct mbuf
**resultp
);
88 static void ng_deflate_reset_req(node_p node
);
90 /* Parse type for struct ng_deflate_config. */
91 static const struct ng_parse_struct_field ng_deflate_config_type_fields
[]
92 = NG_DEFLATE_CONFIG_INFO
;
93 static const struct ng_parse_type ng_deflate_config_type
= {
94 &ng_parse_struct_type
,
95 ng_deflate_config_type_fields
98 /* Parse type for struct ng_deflate_stat. */
99 static const struct ng_parse_struct_field ng_deflate_stats_type_fields
[]
100 = NG_DEFLATE_STATS_INFO
;
101 static const struct ng_parse_type ng_deflate_stat_type
= {
102 &ng_parse_struct_type
,
103 ng_deflate_stats_type_fields
106 /* List of commands and how to convert arguments to/from ASCII. */
107 static const struct ng_cmdlist ng_deflate_cmds
[] = {
112 &ng_deflate_config_type
,
117 NGM_DEFLATE_RESETREQ
,
124 NGM_DEFLATE_GET_STATS
,
127 &ng_deflate_stat_type
131 NGM_DEFLATE_CLR_STATS
,
138 NGM_DEFLATE_GETCLR_STATS
,
141 &ng_deflate_stat_type
146 /* Node type descriptor */
147 static struct ng_type ng_deflate_typestruct
= {
148 .version
= NG_ABI_VERSION
,
149 .name
= NG_DEFLATE_NODE_TYPE
,
150 .constructor
= ng_deflate_constructor
,
151 .rcvmsg
= ng_deflate_rcvmsg
,
152 .shutdown
= ng_deflate_shutdown
,
153 .newhook
= ng_deflate_newhook
,
154 .rcvdata
= ng_deflate_rcvdata
,
155 .disconnect
= ng_deflate_disconnect
,
156 .cmdlist
= ng_deflate_cmds
,
158 NETGRAPH_INIT(deflate
, &ng_deflate_typestruct
);
160 /* Depend on separate zlib module. */
161 MODULE_DEPEND(ng_deflate
, zlib
, 1, 1, 1);
163 #define ERROUT(x) do { error = (x); goto done; } while (0)
165 /************************************************************************
167 ************************************************************************/
170 * Node type constructor
173 ng_deflate_constructor(node_p node
)
177 /* Allocate private structure. */
178 priv
= kmalloc(sizeof(*priv
), M_NETGRAPH_DEFLATE
, M_WAITOK
| M_ZERO
);
180 NG_NODE_SET_PRIVATE(node
, priv
);
182 /* This node is not thread safe. */
183 NG_NODE_FORCE_WRITER(node
);
190 * Give our OK for a hook to be added.
193 ng_deflate_newhook(node_p node
, hook_p hook
, const char *name
)
195 const priv_p priv
= NG_NODE_PRIVATE(node
);
197 if (NG_NODE_NUMHOOKS(node
) > 0)
200 if (strcmp(name
, NG_DEFLATE_HOOK_COMP
) == 0)
202 else if (strcmp(name
, NG_DEFLATE_HOOK_DECOMP
) == 0)
211 * Receive a control message
214 ng_deflate_rcvmsg(node_p node
, item_p item
, hook_p lasthook
)
216 const priv_p priv
= NG_NODE_PRIVATE(node
);
217 struct ng_mesg
*resp
= NULL
;
221 NGI_GET_MSG(item
, msg
);
223 if (msg
->header
.typecookie
!= NGM_DEFLATE_COOKIE
)
226 switch (msg
->header
.cmd
) {
227 case NGM_DEFLATE_CONFIG
:
229 struct ng_deflate_config
*const cfg
230 = (struct ng_deflate_config
*)msg
->data
;
232 /* Check configuration. */
233 if (msg
->header
.arglen
!= sizeof(*cfg
))
236 if (cfg
->windowBits
< 8 || cfg
->windowBits
> 15)
241 /* Clear previous state. */
242 if (priv
->cfg
.enable
) {
244 deflateEnd(&priv
->cx
);
246 inflateEnd(&priv
->cx
);
247 priv
->cfg
.enable
= 0;
250 /* Configuration is OK, reset to it. */
253 if (priv
->cfg
.enable
) {
254 priv
->cx
.next_in
= NULL
;
255 priv
->cx
.zalloc
= z_alloc
;
256 priv
->cx
.zfree
= z_free
;
258 if (priv
->compress
) {
259 if ((res
= deflateInit2(&priv
->cx
,
260 Z_DEFAULT_COMPRESSION
, Z_DEFLATED
,
262 Z_DEFAULT_STRATEGY
)) != Z_OK
) {
264 "deflateInit2: error %d, %s\n",
266 priv
->cfg
.enable
= 0;
270 if ((res
= inflateInit2(&priv
->cx
,
271 -cfg
->windowBits
)) != Z_OK
) {
273 "inflateInit2: error %d, %s\n",
275 priv
->cfg
.enable
= 0;
281 /* Initialize other state. */
284 /* Save return address so we can send reset-req's */
285 priv
->ctrlnode
= NGI_RETADDR(item
);
289 case NGM_DEFLATE_RESETREQ
:
290 ng_deflate_reset_req(node
);
293 case NGM_DEFLATE_GET_STATS
:
294 case NGM_DEFLATE_CLR_STATS
:
295 case NGM_DEFLATE_GETCLR_STATS
:
296 /* Create response if requested. */
297 if (msg
->header
.cmd
!= NGM_DEFLATE_CLR_STATS
) {
298 NG_MKRESPONSE(resp
, msg
,
299 sizeof(struct ng_deflate_stats
), M_WAITOK
| M_NULLOK
);
302 bcopy(&priv
->stats
, resp
->data
,
303 sizeof(struct ng_deflate_stats
));
306 /* Clear stats if requested. */
307 if (msg
->header
.cmd
!= NGM_DEFLATE_GET_STATS
)
309 sizeof(struct ng_deflate_stats
));
317 NG_RESPOND_MSG(error
, node
, item
, resp
);
323 * Receive incoming data on our hook.
326 ng_deflate_rcvdata(hook_p hook
, item_p item
)
328 const node_p node
= NG_HOOK_NODE(hook
);
329 const priv_p priv
= NG_NODE_PRIVATE(node
);
330 struct mbuf
*m
, *out
;
333 if (!priv
->cfg
.enable
) {
340 if (priv
->compress
) {
341 if ((error
= ng_deflate_compress(node
, m
, &out
)) != 0) {
343 log(LOG_NOTICE
, "%s: error: %d\n", __func__
, error
);
347 } else { /* Decompress */
348 if ((error
= ng_deflate_decompress(node
, m
, &out
)) != 0) {
350 log(LOG_NOTICE
, "%s: error: %d\n", __func__
, error
);
351 if (priv
->ctrlnode
!= 0) {
354 /* Need to send a reset-request. */
355 NG_MKMESSAGE(msg
, NGM_DEFLATE_COOKIE
,
356 NGM_DEFLATE_RESETREQ
, 0, M_WAITOK
| M_NULLOK
);
359 NG_SEND_MSG_ID(error
, node
, msg
,
366 NG_FWD_NEW_DATA(error
, item
, hook
, out
);
374 ng_deflate_shutdown(node_p node
)
376 const priv_p priv
= NG_NODE_PRIVATE(node
);
378 /* Take down netgraph node. */
379 if (priv
->cfg
.enable
) {
381 deflateEnd(&priv
->cx
);
383 inflateEnd(&priv
->cx
);
386 kfree(priv
, M_NETGRAPH_DEFLATE
);
387 NG_NODE_SET_PRIVATE(node
, NULL
);
388 NG_NODE_UNREF(node
); /* let the node escape */
396 ng_deflate_disconnect(hook_p hook
)
398 const node_p node
= NG_HOOK_NODE(hook
);
399 const priv_p priv
= NG_NODE_PRIVATE(node
);
401 if (priv
->cfg
.enable
) {
403 deflateEnd(&priv
->cx
);
405 inflateEnd(&priv
->cx
);
406 priv
->cfg
.enable
= 0;
409 /* Go away if no longer connected. */
410 if ((NG_NODE_NUMHOOKS(node
) == 0) && NG_NODE_IS_VALID(node
))
411 ng_rmnode_self(node
);
415 /************************************************************************
417 ************************************************************************/
420 * Space allocation and freeing routines for use by zlib routines.
424 z_alloc(void *notused
, u_int items
, u_int size
)
427 return (kmalloc(items
* size
, M_NETGRAPH_DEFLATE
, M_WAITOK
| M_NULLOK
));
431 z_free(void *notused
, void *ptr
)
434 kfree(ptr
, M_NETGRAPH_DEFLATE
);
438 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
439 * The original mbuf is not free'd.
442 ng_deflate_compress(node_p node
, struct mbuf
*m
, struct mbuf
**resultp
)
444 const priv_p priv
= NG_NODE_PRIVATE(node
);
451 inlen
= m
->m_pkthdr
.len
;
453 priv
->stats
.FramesPlain
++;
454 priv
->stats
.InOctets
+=inlen
;
456 if (inlen
> DEFLATE_BUF_SIZE
) {
457 priv
->stats
.Errors
++;
462 /* Work with contiguous regions of memory. */
463 m_copydata(m
, 0, inlen
, (caddr_t
)priv
->inbuf
);
464 outlen
= DEFLATE_BUF_SIZE
;
466 /* Compress "inbuf" into "outbuf". */
467 /* Prepare to compress. */
468 if (priv
->inbuf
[0] != 0) {
469 priv
->cx
.next_in
= priv
->inbuf
;
470 priv
->cx
.avail_in
= inlen
;
472 priv
->cx
.next_in
= priv
->inbuf
+ 1; /* compress protocol */
473 priv
->cx
.avail_in
= inlen
- 1;
475 priv
->cx
.next_out
= priv
->outbuf
+ 2 + DEFLATE_HDRLEN
;
476 priv
->cx
.avail_out
= outlen
- 2 - DEFLATE_HDRLEN
;
479 rtn
= deflate(&priv
->cx
, Z_PACKET_FLUSH
);
481 /* Check return value. */
483 priv
->stats
.Errors
++;
484 log(LOG_NOTICE
, "ng_deflate: compression error: %d (%s)\n",
490 /* Calculate resulting size. */
491 outlen
-= priv
->cx
.avail_out
;
493 /* If we can't compress this packet, send it as-is. */
494 if (outlen
> inlen
) {
495 /* Return original packet uncompressed. */
497 priv
->stats
.FramesUncomp
++;
498 priv
->stats
.OutOctets
+=inlen
;
502 /* Install header. */
503 ((u_int16_t
*)priv
->outbuf
)[0] = htons(PROT_COMPD
);
504 ((u_int16_t
*)priv
->outbuf
)[1] = htons(priv
->seqnum
);
506 /* Return packet in an mbuf. */
507 *resultp
= m_devget((caddr_t
)priv
->outbuf
, outlen
, 0, NULL
);
508 if (*resultp
== NULL
) {
509 priv
->stats
.Errors
++;
512 priv
->stats
.FramesComp
++;
513 priv
->stats
.OutOctets
+=outlen
;
516 /* Update sequence number. */
523 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
524 * The original mbuf is not free'd.
527 ng_deflate_decompress(node_p node
, struct mbuf
*m
, struct mbuf
**resultp
)
529 const priv_p priv
= NG_NODE_PRIVATE(node
);
539 inlen
= m
->m_pkthdr
.len
;
541 if (inlen
> DEFLATE_BUF_SIZE
) {
542 priv
->stats
.Errors
++;
548 /* Work with contiguous regions of memory. */
549 m_copydata(m
, 0, inlen
, (caddr_t
)priv
->inbuf
);
551 /* Separate proto. */
552 if ((priv
->inbuf
[0] & 0x01) != 0) {
553 proto
= priv
->inbuf
[0];
556 proto
= ntohs(((uint16_t *)priv
->inbuf
)[0]);
560 priv
->stats
.InOctets
+= inlen
;
562 /* Packet is compressed, so decompress. */
563 if (proto
== PROT_COMPD
) {
564 priv
->stats
.FramesComp
++;
566 /* Check sequence number. */
567 rseqnum
= ntohs(((uint16_t *)(priv
->inbuf
+ offset
))[0]);
569 if (rseqnum
!= priv
->seqnum
) {
570 priv
->stats
.Errors
++;
571 log(LOG_NOTICE
, "ng_deflate: wrong sequence: %u "
572 "instead of %u\n", rseqnum
, priv
->seqnum
);
578 outlen
= DEFLATE_BUF_SIZE
;
580 /* Decompress "inbuf" into "outbuf". */
581 /* Prepare to decompress. */
582 priv
->cx
.next_in
= priv
->inbuf
+ offset
;
583 priv
->cx
.avail_in
= inlen
- offset
;
584 /* Reserve space for protocol decompression. */
585 priv
->cx
.next_out
= priv
->outbuf
+ 1;
586 priv
->cx
.avail_out
= outlen
- 1;
589 rtn
= inflate(&priv
->cx
, Z_PACKET_FLUSH
);
591 /* Check return value. */
592 if (rtn
!= Z_OK
&& rtn
!= Z_STREAM_END
) {
593 priv
->stats
.Errors
++;
596 log(LOG_NOTICE
, "%s: decompression error: %d (%s)\n",
597 __func__
, rtn
, priv
->cx
.msg
);
609 /* Calculate resulting size. */
610 outlen
-= priv
->cx
.avail_out
;
614 /* Decompress protocol. */
615 if ((priv
->outbuf
[1] & 0x01) != 0) {
617 /* Return packet in an mbuf. */
618 *resultp
= m_devget((caddr_t
)priv
->outbuf
, outlen
, 0,
622 /* Return packet in an mbuf. */
623 *resultp
= m_devget((caddr_t
)(priv
->outbuf
+ 1),
626 if (*resultp
== NULL
) {
627 priv
->stats
.Errors
++;
631 priv
->stats
.FramesPlain
++;
632 priv
->stats
.OutOctets
+=outlen
;
634 } else { /* Packet is not compressed, just update dictionary. */
635 priv
->stats
.FramesUncomp
++;
636 if (priv
->inbuf
[0] == 0) {
637 priv
->cx
.next_in
= priv
->inbuf
+ 1; /* compress protocol */
638 priv
->cx
.avail_in
= inlen
- 1;
640 priv
->cx
.next_in
= priv
->inbuf
;
641 priv
->cx
.avail_in
= inlen
;
644 rtn
= inflateIncomp(&priv
->cx
);
646 /* Check return value */
648 priv
->stats
.Errors
++;
649 log(LOG_NOTICE
, "%s: inflateIncomp error: %d (%s)\n",
650 __func__
, rtn
, priv
->cx
.msg
);
657 priv
->stats
.FramesPlain
++;
658 priv
->stats
.OutOctets
+= inlen
;
661 /* Update sequence number. */
668 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
671 ng_deflate_reset_req(node_p node
)
673 const priv_p priv
= NG_NODE_PRIVATE(node
);
676 if (priv
->cfg
.enable
) {
678 deflateReset(&priv
->cx
);
680 inflateReset(&priv
->cx
);