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 <kern/cpu_number.h>
56 #include <netinet6/ipcomp.h>
58 #include <netinet6/ipcomp6.h>
60 #include <netinet6/ipsec.h>
62 #include <netinet6/ipsec6.h>
65 #include <net/net_osdep.h>
67 static void *deflate_alloc(void *, u_int
, u_int
);
68 static void deflate_free(void *, void *);
69 static int deflate_common(struct mbuf
*, struct mbuf
*, size_t *, int);
70 static int deflate_compress(struct mbuf
*, struct mbuf
*, size_t *);
71 static int deflate_decompress(struct mbuf
*, struct mbuf
*, size_t *);
74 * We need to use default window size (2^15 = 32Kbytes as of writing) for
75 * inbound case. Otherwise we get interop problem.
76 * Use negative value to avoid Adler32 checksum. This is an undocumented
77 * feature in zlib (see ipsec wg mailing list archive in January 2000).
79 static int deflate_policy
= Z_DEFAULT_COMPRESSION
;
80 static int deflate_window_out
= -12;
81 static const int deflate_window_in
= -1 * MAX_WBITS
; /* don't change it */
82 static int deflate_memlevel
= MAX_MEM_LEVEL
;
84 static z_stream deflate_stream
;
85 static z_stream inflate_stream
;
87 static const struct ipcomp_algorithm ipcomp_algorithms
[] = {
88 { deflate_compress
, deflate_decompress
, 90 },
91 const struct ipcomp_algorithm
*
92 ipcomp_algorithm_lookup(idx
)
96 if (idx
== SADB_X_CALG_DEFLATE
) {
98 * Avert your gaze, ugly hack follows!
99 * We init here so our malloc can allocate using M_WAIT.
100 * We don't want to allocate if ipcomp isn't used, and we
101 * don't want to allocate on the input or output path.
102 * Allocation fails if we use M_NOWAIT because init allocates
103 * something like 256k (ouch).
105 if (deflate_stream
.zalloc
== NULL
) {
106 deflate_stream
.zalloc
= deflate_alloc
;
107 deflate_stream
.zfree
= deflate_free
;
108 if (deflateInit2(&deflate_stream
, deflate_policy
, Z_DEFLATED
,
109 deflate_window_out
, deflate_memlevel
, Z_DEFAULT_STRATEGY
)) {
110 /* Allocation failed */
111 bzero(&deflate_stream
, sizeof(deflate_stream
));
113 printf("ipcomp_algorithm_lookup: deflateInit2 failed.\n");
118 if (inflate_stream
.zalloc
== NULL
) {
119 inflate_stream
.zalloc
= deflate_alloc
;
120 inflate_stream
.zfree
= deflate_free
;
121 if (inflateInit2(&inflate_stream
, deflate_window_in
)) {
122 /* Allocation failed */
123 bzero(&inflate_stream
, sizeof(inflate_stream
));
125 printf("ipcomp_algorithm_lookup: inflateInit2 failed.\n");
130 return &ipcomp_algorithms
[0];
136 deflate_alloc(aux
, items
, siz
)
142 ptr
= _MALLOC(items
* siz
, M_TEMP
, M_WAIT
);
147 deflate_free(aux
, ptr
)
155 deflate_common(m
, md
, lenp
, mode
)
159 int mode
; /* 0: compress 1: decompress */
163 struct mbuf
*n
= NULL
, *n0
= NULL
, **np
;
169 #define MOREBLOCK() \
171 /* keep the reply buffer into our chain */ \
173 n->m_len = zs->total_out - offset; \
174 offset = zs->total_out; \
180 /* get a fresh reply buffer */ \
181 MGET(n, M_DONTWAIT, MT_DATA); \
183 MCLGET(n, M_DONTWAIT); \
190 n->m_len = M_TRAILINGSPACE(n); \
193 * if this is the first reply buffer, reserve \
194 * region for ipcomp header. \
197 n->m_len -= sizeof(struct ipcomp); \
198 n->m_data += sizeof(struct ipcomp); \
201 zs->next_out = mtod(n, u_int8_t *); \
202 zs->avail_out = n->m_len; \
205 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
)
208 panic("md is not in m in deflate_common");
211 zs
= mode
? &inflate_stream
: &deflate_stream
;
212 if (zs
->zalloc
== NULL
) {
214 * init is called in ipcomp_algorithm_lookup.
215 * if zs->zalloc is NULL, either init hasn't been called (unlikely)
216 * or init failed because of no memory.
232 while (p
&& p
->m_len
== 0) {
236 /* input stream and output stream are available */
237 while (p
&& zs
->avail_in
== 0) {
238 /* get input buffer */
239 if (p
&& zs
->avail_in
== 0) {
240 zs
->next_in
= mtod(p
, u_int8_t
*);
241 zs
->avail_in
= p
->m_len
;
243 while (p
&& p
->m_len
== 0) {
248 /* get output buffer */
249 if (zs
->next_out
== NULL
|| zs
->avail_out
== 0) {
253 zerror
= mode
? inflate(zs
, Z_NO_FLUSH
)
254 : deflate(zs
, Z_NO_FLUSH
);
256 if (zerror
== Z_STREAM_END
)
258 else if (zerror
== Z_OK
) {
259 /* inflate: Z_OK can indicate the end of decode */
260 if (mode
&& !p
&& zs
->avail_out
!= 0)
266 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
267 "%sflate(Z_NO_FLUSH): %s\n",
268 mode
? "de" : "", mode
? "in" : "de",
271 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
272 "%sflate(Z_NO_FLUSH): unknown error (%d)\n",
273 mode
? "de" : "", mode
? "in" : "de",
276 mode
? inflateReset(zs
) : deflateReset(zs
);
277 /* mode ? inflateEnd(zs) : deflateEnd(zs);*/
283 if (zerror
== Z_STREAM_END
)
288 /* get output buffer */
289 if (zs
->next_out
== NULL
|| zs
->avail_out
== 0) {
293 zerror
= mode
? inflate(zs
, Z_FINISH
)
294 : deflate(zs
, Z_FINISH
);
296 if (zerror
== Z_STREAM_END
)
298 else if (zerror
== Z_OK
)
302 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
303 "%sflate(Z_FINISH): %s\n",
304 mode
? "de" : "", mode
? "in" : "de",
307 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
308 "%sflate(Z_FINISH): unknown error (%d)\n",
309 mode
? "de" : "", mode
? "in" : "de",
312 mode
? inflateReset(zs
) : deflateReset(zs
);
313 /* mode ? inflateEnd(zs) : deflateEnd(zs); */
320 /* keep the final reply buffer into our chain */
322 n
->m_len
= zs
->total_out
- offset
;
323 offset
= zs
->total_out
;
329 /* switch the mbuf to the new one */
332 *lenp
= zs
->total_out
;
334 /* reset the inflate/deflate state */
335 zerror
= mode
? inflateReset(zs
) : deflateReset(zs
);
336 if (zerror
!= Z_OK
) {
338 * A failure here is uncommon. If this does
339 * fail, the packet can still be used but
340 * the z_stream will be messed up so subsequent
341 * inflates/deflates will probably fail.
344 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
346 mode
? "de" : "", mode
? "in" : "de",
349 ipseclog((LOG_ERR
, "ipcomp_%scompress: "
350 "%sflateEnd: unknown error (%d)\n",
351 mode
? "de" : "", mode
? "in" : "de",
370 deflate_compress(m
, md
, lenp
)
376 panic("m == NULL in deflate_compress");
378 panic("md == NULL in deflate_compress");
380 panic("lenp == NULL in deflate_compress");
382 return deflate_common(m
, md
, lenp
, 0);
386 deflate_decompress(m
, md
, lenp
)
392 panic("m == NULL in deflate_decompress");
394 panic("md == NULL in deflate_decompress");
396 panic("lenp == NULL in deflate_decompress");
398 return deflate_common(m
, md
, lenp
, 1);