1 /* $FreeBSD: src/sys/netinet6/ipcomp_core.c,v 1.1.2.5 2003/01/11 19:10:59 ume Exp $ */
2 /* $KAME: ipcomp_core.c,v 1.25 2001/07/26 06:53:17 jinmei Exp $ */
5 * Copyright (C) 1999 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * RFC2393 IP payload compression protocol (IPComp).
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/queue.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <net/netisr.h>
57 #include <machine/cpu.h>
59 #include <netinet6/ipcomp.h>
61 #include <netinet6/ipcomp6.h>
63 #include <netinet6/ipsec.h>
65 #include <netinet6/ipsec6.h>
68 #include <machine/stdarg.h>
70 #include <net/net_osdep.h>
72 static void *deflate_alloc __P((void *, u_int, u_int));
73 static void deflate_free __P((void *, void *));
74 static int deflate_common __P((struct mbuf *, struct mbuf *, size_t *, int));
75 static int deflate_compress __P((struct mbuf *, struct mbuf *, size_t *));
76 static int deflate_decompress __P((struct mbuf *, struct mbuf *, size_t *));
79 * We need to use default window size (2^15 = 32Kbytes as of writing) for
80 * inbound case. Otherwise we get interop problem.
81 * Use negative value to avoid Adler32 checksum. This is an undocumented
82 * feature in zlib (see ipsec wg mailing list archive in January 2000).
84 static int deflate_policy = Z_DEFAULT_COMPRESSION;
85 static int deflate_window_out = -12;
86 static const int deflate_window_in = -1 * MAX_WBITS; /* don't change it */
87 static int deflate_memlevel = MAX_MEM_LEVEL;
89 static const struct ipcomp_algorithm ipcomp_algorithms[] = {
90 { deflate_compress, deflate_decompress, 90 },
93 const struct ipcomp_algorithm *
94 ipcomp_algorithm_lookup(idx)
98 if (idx == SADB_X_CALG_DEFLATE)
99 return &ipcomp_algorithms[0];
104 deflate_alloc(aux, items, siz)
110 ptr = malloc(items * siz, M_TEMP, M_NOWAIT);
115 deflate_free(aux, ptr)
123 deflate_common(m, md, lenp, mode)
127 int mode; /* 0: compress 1: decompress */
131 struct mbuf *n = NULL, *n0 = NULL, **np;
137 #define MOREBLOCK() \
139 /* keep the reply buffer into our chain */ \
141 n->m_len = zs.total_out - offset; \
142 offset = zs.total_out; \
148 /* get a fresh reply buffer */ \
149 MGET(n, M_DONTWAIT, MT_DATA); \
151 MCLGET(n, M_DONTWAIT); \
158 n->m_len = M_TRAILINGSPACE(n); \
161 * if this is the first reply buffer, reserve \
162 * region for ipcomp header. \
165 n->m_len -= sizeof(struct ipcomp); \
166 n->m_data += sizeof(struct ipcomp); \
169 zs.next_out = mtod(n, u_int8_t *); \
170 zs.avail_out = n->m_len; \
173 for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
176 panic("md is not in m in deflate_common");
178 bzero(&zs, sizeof(zs));
179 zs.zalloc = deflate_alloc;
180 zs.zfree = deflate_free;
182 zerror = mode ? inflateInit2(&zs, deflate_window_in)
183 : deflateInit2(&zs, deflate_policy, Z_DEFLATED,
184 deflate_window_out, deflate_memlevel,
186 if (zerror != Z_OK) {
196 while (p && p->m_len == 0) {
200 /* input stream and output stream are available */
201 while (p && zs.avail_in == 0) {
202 /* get input buffer */
203 if (p && zs.avail_in == 0) {
204 zs.next_in = mtod(p, u_int8_t *);
205 zs.avail_in = p->m_len;
207 while (p && p->m_len == 0) {
212 /* get output buffer */
213 if (zs.next_out == NULL || zs.avail_out == 0) {
217 zerror = mode ? inflate(&zs, Z_NO_FLUSH)
218 : deflate(&zs, Z_NO_FLUSH);
220 if (zerror == Z_STREAM_END)
222 else if (zerror == Z_OK) {
223 /* inflate: Z_OK can indicate the end of decode */
224 if (mode && !p && zs.avail_out != 0)
230 ipseclog((LOG_ERR, "ipcomp_%scompress: "
231 "%sflate(Z_NO_FLUSH): %s\n",
232 mode ? "de" : "", mode ? "in" : "de",
235 ipseclog((LOG_ERR, "ipcomp_%scompress: "
236 "%sflate(Z_NO_FLUSH): unknown error (%d)\n",
237 mode ? "de" : "", mode ? "in" : "de",
240 mode ? inflateEnd(&zs) : deflateEnd(&zs);
246 if (zerror == Z_STREAM_END)
251 /* get output buffer */
252 if (zs.next_out == NULL || zs.avail_out == 0) {
256 zerror = mode ? inflate(&zs, Z_SYNC_FLUSH)
257 : deflate(&zs, Z_FINISH);
259 if (zerror == Z_STREAM_END)
261 else if (zerror == Z_OK) {
262 if (mode && zs.avail_out != 0)
268 ipseclog((LOG_ERR, "ipcomp_%scompress: "
269 "%sflate(Z_FINISH): %s\n",
270 mode ? "de" : "", mode ? "in" : "de",
273 ipseclog((LOG_ERR, "ipcomp_%scompress: "
274 "%sflate(Z_FINISH): unknown error (%d)\n",
275 mode ? "de" : "", mode ? "in" : "de",
278 mode ? inflateEnd(&zs) : deflateEnd(&zs);
285 zerror = mode ? inflateEnd(&zs) : deflateEnd(&zs);
286 if (zerror != Z_OK) {
288 ipseclog((LOG_ERR, "ipcomp_%scompress: "
290 mode ? "de" : "", mode ? "in" : "de",
293 ipseclog((LOG_ERR, "ipcomp_%scompress: "
294 "%sflateEnd: unknown error (%d)\n",
295 mode ? "de" : "", mode ? "in" : "de",
301 /* keep the final reply buffer into our chain */
303 n->m_len = zs.total_out - offset;
304 offset = zs.total_out;
310 /* switch the mbuf to the new one */
313 *lenp = zs.total_out;
329 deflate_compress(m, md, lenp)
335 panic("m == NULL in deflate_compress");
337 panic("md == NULL in deflate_compress");
339 panic("lenp == NULL in deflate_compress");
341 return deflate_common(m, md, lenp, 0);
345 deflate_decompress(m, md, lenp)
351 panic("m == NULL in deflate_decompress");
353 panic("md == NULL in deflate_decompress");
355 panic("lenp == NULL in deflate_decompress");
357 return deflate_common(m, md, lenp, 1);