1 /* $FreeBSD: src/sys/netinet6/ipcomp_core.c,v 1.1.2.2 2001/07/03 11:01:54 ume Exp $ */
2 /* $KAME: ipcomp_core.c,v 1.24 2000/10/23 04:24:22 itojun 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 <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/syslog.h>
49 #include <sys/queue.h>
52 #include <net/route.h>
54 #include <libkern/zlib.h>
56 #include <kern/cpu_number.h>
58 #include <netinet6/ipcomp.h>
60 #include <netinet6/ipcomp6.h>
62 #include <netinet6/ipsec.h>
64 #include <netinet6/ipsec6.h>
67 #include <net/net_osdep.h>
70 static void *deflate_alloc(void *, u_int
, u_int
);
71 static void deflate_free(void *, void *);
72 static int deflate_common(struct mbuf
*, struct mbuf
*, size_t *, int);
73 static int deflate_compress(struct mbuf
*, struct mbuf
*, size_t *);
74 static int deflate_decompress(struct mbuf
*, struct mbuf
*, size_t *);
77 * We need to use default window size (2^15 = 32Kbytes as of writing) for
78 * inbound case. Otherwise we get interop problem.
79 * Use negative value to avoid Adler32 checksum. This is an undocumented
80 * feature in zlib (see ipsec wg mailing list archive in January 2000).
82 static int deflate_policy
= Z_DEFAULT_COMPRESSION
;
83 static int deflate_window_out
= -12;
84 static const int deflate_window_in
= -1 * MAX_WBITS
; /* don't change it */
85 static int deflate_memlevel
= MAX_MEM_LEVEL
;
87 static z_stream deflate_stream
;
88 static z_stream inflate_stream
;
89 #endif /* IPCOMP_ZLIB */
92 static const struct ipcomp_algorithm ipcomp_algorithms
[] = {
93 { deflate_compress
, deflate_decompress
, 90 },
96 static const struct ipcomp_algorithm ipcomp_algorithms
[] __unused
= {};
99 const struct ipcomp_algorithm
*
100 ipcomp_algorithm_lookup(
109 if (idx
== SADB_X_CALG_DEFLATE
) {
111 * Avert your gaze, ugly hack follows!
112 * We init here so our malloc can allocate using M_WAIT.
113 * We don't want to allocate if ipcomp isn't used, and we
114 * don't want to allocate on the input or output path.
115 * Allocation fails if we use M_NOWAIT because init allocates
116 * something like 256k (ouch).
118 if (deflate_stream
.zalloc
== NULL
) {
119 deflate_stream
.zalloc
= deflate_alloc
;
120 deflate_stream
.zfree
= deflate_free
;
121 if (deflateInit2(&deflate_stream
, deflate_policy
, Z_DEFLATED
,
122 deflate_window_out
, deflate_memlevel
, Z_DEFAULT_STRATEGY
)) {
123 /* Allocation failed */
124 bzero(&deflate_stream
, sizeof(deflate_stream
));
126 printf("ipcomp_algorithm_lookup: deflateInit2 failed.\n");
131 if (inflate_stream
.zalloc
== NULL
) {
132 inflate_stream
.zalloc
= deflate_alloc
;
133 inflate_stream
.zfree
= deflate_free
;
134 if (inflateInit2(&inflate_stream
, deflate_window_in
)) {
135 /* Allocation failed */
136 bzero(&inflate_stream
, sizeof(inflate_stream
));
138 printf("ipcomp_algorithm_lookup: inflateInit2 failed.\n");
143 return &ipcomp_algorithms
[0];
145 #endif /* IPCOMP_ZLIB */
157 ptr
= _MALLOC(items
* siz
, M_TEMP
, M_NOWAIT
);
170 deflate_common(m
, md
, lenp
, mode
)
174 int mode
; /* 0: compress 1: decompress */
178 struct mbuf
*n
= NULL
, *n0
= NULL
, **np
;
184 #define MOREBLOCK() \
186 /* keep the reply buffer into our chain */ \
188 n->m_len = zs->total_out - offset; \
189 offset = zs->total_out; \
195 /* get a fresh reply buffer */ \
196 MGET(n, M_DONTWAIT, MT_DATA); \
198 MCLGET(n, M_DONTWAIT); \
205 n->m_len = M_TRAILINGSPACE(n); \
208 * if this is the first reply buffer, reserve \
209 * region for ipcomp header. \
212 n->m_len -= sizeof(struct ipcomp); \
213 n->m_data += sizeof(struct ipcomp); \
216 zs->next_out = mtod(n, u_int8_t *); \
217 zs->avail_out = n->m_len; \
220 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
)
223 panic("md is not in m in deflate_common");
226 zs
= mode
? &inflate_stream
: &deflate_stream
;
227 if (zs
->zalloc
== NULL
) {
229 * init is called in ipcomp_algorithm_lookup.
230 * if zs->zalloc is NULL, either init hasn't been called (unlikely)
231 * or init failed because of no memory.
247 while (p
&& p
->m_len
== 0) {
251 /* input stream and output stream are available */
252 while (p
&& zs
->avail_in
== 0) {
253 /* get input buffer */
254 if (p
&& zs
->avail_in
== 0) {
255 zs
->next_in
= mtod(p
, u_int8_t
*);
256 zs
->avail_in
= p
->m_len
;
258 while (p
&& p
->m_len
== 0) {
263 /* get output buffer */
264 if (zs
->next_out
== NULL
|| zs
->avail_out
== 0) {
268 zerror
= mode
? inflate(zs
, Z_NO_FLUSH
)
269 : deflate(zs
, Z_NO_FLUSH
);
271 if (zerror
== Z_STREAM_END
)
273 else if (zerror
== Z_OK
) {
274 /* inflate: Z_OK can indicate the end of decode */
275 if (mode
&& !p
&& zs
->avail_out
!= 0)
281 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
282 "%sflate(Z_NO_FLUSH): %s\n",
283 mode
? "de" : "", mode
? "in" : "de",
286 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
287 "%sflate(Z_NO_FLUSH): unknown error (%d)\n",
288 mode
? "de" : "", mode
? "in" : "de",
291 mode
? inflateReset(zs
) : deflateReset(zs
);
292 /* mode ? inflateEnd(zs) : deflateEnd(zs);*/
298 if (zerror
== Z_STREAM_END
)
303 /* get output buffer */
304 if (zs
->next_out
== NULL
|| zs
->avail_out
== 0) {
308 zerror
= mode
? inflate(zs
, Z_FINISH
)
309 : deflate(zs
, Z_FINISH
);
311 if (zerror
== Z_STREAM_END
)
313 else if (zerror
== Z_OK
)
317 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
318 "%sflate(Z_FINISH): %s\n",
319 mode
? "de" : "", mode
? "in" : "de",
322 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
323 "%sflate(Z_FINISH): unknown error (%d)\n",
324 mode
? "de" : "", mode
? "in" : "de",
327 mode
? inflateReset(zs
) : deflateReset(zs
);
328 /* mode ? inflateEnd(zs) : deflateEnd(zs); */
335 /* keep the final reply buffer into our chain */
337 n
->m_len
= zs
->total_out
- offset
;
338 offset
= zs
->total_out
;
344 /* switch the mbuf to the new one */
347 *lenp
= zs
->total_out
;
349 /* reset the inflate/deflate state */
350 zerror
= mode
? inflateReset(zs
) : deflateReset(zs
);
351 if (zerror
!= Z_OK
) {
353 * A failure here is uncommon. If this does
354 * fail, the packet can still be used but
355 * the z_stream will be messed up so subsequent
356 * inflates/deflates will probably fail.
359 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
361 mode
? "de" : "", mode
? "in" : "de",
364 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
365 "%sflateEnd: unknown error (%d)\n",
366 mode
? "de" : "", mode
? "in" : "de",
385 deflate_compress(m
, md
, lenp
)
391 panic("m == NULL in deflate_compress");
393 panic("md == NULL in deflate_compress");
395 panic("lenp == NULL in deflate_compress");
397 return deflate_common(m
, md
, lenp
, 0);
401 deflate_decompress(m
, md
, lenp
)
407 panic("m == NULL in deflate_decompress");
409 panic("md == NULL in deflate_decompress");
411 panic("lenp == NULL in deflate_decompress");
413 return deflate_common(m
, md
, lenp
, 1);
415 #endif /* IPCOMP_ZLIB */