2002-03-14 08:18:10 +03:00
|
|
|
/* $NetBSD: ipcomp_core.c,v 1.19 2002/03/14 05:18:10 itojun Exp $ */
|
2001-10-15 07:55:37 +04:00
|
|
|
/* $KAME: ipcomp_core.c,v 1.25 2001/07/26 06:53:17 jinmei Exp $ */
|
1999-07-04 01:24:45 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1999 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-09-21 01:43:52 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2000-09-21 01:43:52 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RFC2393 IP payload compression protocol (IPComp).
|
|
|
|
*/
|
|
|
|
|
2001-11-13 03:56:55 +03:00
|
|
|
#include <sys/cdefs.h>
|
2002-03-14 08:18:10 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ipcomp_core.c,v 1.19 2002/03/14 05:18:10 itojun Exp $");
|
2001-11-13 03:56:55 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
#include "opt_inet.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/domain.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/syslog.h>
|
2000-01-31 17:18:52 +03:00
|
|
|
#include <sys/queue.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/netisr.h>
|
|
|
|
#include <net/zlib.h>
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
|
|
|
|
#include <netinet6/ipcomp.h>
|
2000-01-31 17:18:52 +03:00
|
|
|
#include <netinet6/ipsec.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#include <machine/stdarg.h>
|
|
|
|
|
2000-01-16 20:56:46 +03:00
|
|
|
#include <net/net_osdep.h>
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
static void *deflate_alloc __P((void *, u_int, u_int));
|
|
|
|
static void deflate_free __P((void *, void *));
|
|
|
|
static int deflate_common __P((struct mbuf *, struct mbuf *, size_t *, int));
|
|
|
|
static int deflate_compress __P((struct mbuf *, struct mbuf *, size_t *));
|
|
|
|
static int deflate_decompress __P((struct mbuf *, struct mbuf *, size_t *));
|
|
|
|
|
2000-01-16 20:56:46 +03:00
|
|
|
/*
|
|
|
|
* We need to use default window size (2^15 = 32Kbytes as of writing) for
|
|
|
|
* inbound case. Otherwise we get interop problem.
|
2000-01-26 20:08:41 +03:00
|
|
|
* Use negative value to avoid Adler32 checksum. This is an undocumented
|
|
|
|
* feature in zlib (see ipsec wg mailing list archive in January 2000).
|
2000-01-16 20:56:46 +03:00
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
static int deflate_policy = Z_DEFAULT_COMPRESSION;
|
2000-01-26 20:08:41 +03:00
|
|
|
static int deflate_window_out = -12;
|
|
|
|
static const int deflate_window_in = -1 * MAX_WBITS; /* don't change it */
|
2000-09-21 01:43:52 +04:00
|
|
|
static int deflate_memlevel = MAX_MEM_LEVEL;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-09-26 12:40:23 +04:00
|
|
|
static const struct ipcomp_algorithm ipcomp_algorithms[] = {
|
1999-06-28 10:36:47 +04:00
|
|
|
{ deflate_compress, deflate_decompress, 90 },
|
|
|
|
};
|
|
|
|
|
2000-09-26 12:40:23 +04:00
|
|
|
const struct ipcomp_algorithm *
|
|
|
|
ipcomp_algorithm_lookup(idx)
|
|
|
|
int idx;
|
|
|
|
{
|
|
|
|
|
|
|
|
if (idx == SADB_X_CALG_DEFLATE)
|
|
|
|
return &ipcomp_algorithms[0];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
static void *
|
|
|
|
deflate_alloc(aux, items, siz)
|
|
|
|
void *aux;
|
|
|
|
u_int items;
|
|
|
|
u_int siz;
|
|
|
|
{
|
|
|
|
void *ptr;
|
2000-08-26 01:22:16 +04:00
|
|
|
ptr = malloc(items * siz, M_TEMP, M_NOWAIT);
|
1999-06-28 10:36:47 +04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
deflate_free(aux, ptr)
|
|
|
|
void *aux;
|
|
|
|
void *ptr;
|
|
|
|
{
|
2000-08-26 01:22:16 +04:00
|
|
|
free(ptr, M_TEMP);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
deflate_common(m, md, lenp, mode)
|
|
|
|
struct mbuf *m;
|
|
|
|
struct mbuf *md;
|
|
|
|
size_t *lenp;
|
|
|
|
int mode; /* 0: compress 1: decompress */
|
|
|
|
{
|
|
|
|
struct mbuf *mprev;
|
|
|
|
struct mbuf *p;
|
2000-09-21 01:43:52 +04:00
|
|
|
struct mbuf *n = NULL, *n0 = NULL, **np;
|
1999-06-28 10:36:47 +04:00
|
|
|
z_stream zs;
|
|
|
|
int error = 0;
|
|
|
|
int zerror;
|
|
|
|
size_t offset;
|
2000-09-22 00:28:52 +04:00
|
|
|
|
|
|
|
#define MOREBLOCK() \
|
|
|
|
do { \
|
|
|
|
/* keep the reply buffer into our chain */ \
|
|
|
|
if (n) { \
|
|
|
|
n->m_len = zs.total_out - offset; \
|
|
|
|
offset = zs.total_out; \
|
|
|
|
*np = n; \
|
|
|
|
np = &n->m_next; \
|
|
|
|
n = NULL; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
/* get a fresh reply buffer */ \
|
|
|
|
MGET(n, M_DONTWAIT, MT_DATA); \
|
|
|
|
if (n) { \
|
|
|
|
MCLGET(n, M_DONTWAIT); \
|
|
|
|
} \
|
|
|
|
if (!n) { \
|
|
|
|
error = ENOBUFS; \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
n->m_len = 0; \
|
|
|
|
n->m_len = M_TRAILINGSPACE(n); \
|
|
|
|
n->m_next = NULL; \
|
|
|
|
/* \
|
|
|
|
* if this is the first reply buffer, reserve \
|
|
|
|
* region for ipcomp header. \
|
|
|
|
*/ \
|
|
|
|
if (*np == NULL) { \
|
|
|
|
n->m_len -= sizeof(struct ipcomp); \
|
|
|
|
n->m_data += sizeof(struct ipcomp); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
zs.next_out = mtod(n, u_int8_t *); \
|
|
|
|
zs.avail_out = n->m_len; \
|
|
|
|
} while (0)
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
|
|
|
|
;
|
|
|
|
if (!mprev)
|
|
|
|
panic("md is not in m in deflate_common");
|
|
|
|
|
|
|
|
bzero(&zs, sizeof(zs));
|
|
|
|
zs.zalloc = deflate_alloc;
|
|
|
|
zs.zfree = deflate_free;
|
|
|
|
|
2000-01-16 20:56:46 +03:00
|
|
|
zerror = mode ? inflateInit2(&zs, deflate_window_in)
|
1999-11-05 17:56:26 +03:00
|
|
|
: deflateInit2(&zs, deflate_policy, Z_DEFLATED,
|
2000-01-16 20:56:46 +03:00
|
|
|
deflate_window_out, deflate_memlevel,
|
1999-11-05 17:56:26 +03:00
|
|
|
Z_DEFAULT_STRATEGY);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (zerror != Z_OK) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
n0 = n = NULL;
|
|
|
|
np = &n0;
|
|
|
|
offset = 0;
|
|
|
|
zerror = 0;
|
|
|
|
p = md;
|
2000-09-22 00:28:52 +04:00
|
|
|
while (p && p->m_len == 0) {
|
2000-09-21 03:35:16 +04:00
|
|
|
p = p->m_next;
|
2000-09-22 00:28:52 +04:00
|
|
|
}
|
2000-09-21 03:35:16 +04:00
|
|
|
|
2000-09-22 00:28:52 +04:00
|
|
|
/* input stream and output stream are available */
|
|
|
|
while (p && zs.avail_in == 0) {
|
1999-06-28 10:36:47 +04:00
|
|
|
/* get input buffer */
|
|
|
|
if (p && zs.avail_in == 0) {
|
|
|
|
zs.next_in = mtod(p, u_int8_t *);
|
|
|
|
zs.avail_in = p->m_len;
|
2000-09-21 10:08:26 +04:00
|
|
|
p = p->m_next;
|
2000-09-22 00:28:52 +04:00
|
|
|
while (p && p->m_len == 0) {
|
2000-09-21 03:35:16 +04:00
|
|
|
p = p->m_next;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get output buffer */
|
|
|
|
if (zs.next_out == NULL || zs.avail_out == 0) {
|
2000-09-22 00:28:52 +04:00
|
|
|
MOREBLOCK();
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-09-22 00:28:52 +04:00
|
|
|
zerror = mode ? inflate(&zs, Z_NO_FLUSH)
|
|
|
|
: deflate(&zs, Z_NO_FLUSH);
|
|
|
|
|
|
|
|
if (zerror == Z_STREAM_END)
|
2001-10-15 07:55:37 +04:00
|
|
|
; /* once more. */
|
2000-09-22 00:28:52 +04:00
|
|
|
else if (zerror == Z_OK) {
|
|
|
|
/* inflate: Z_OK can indicate the end of decode */
|
|
|
|
if (mode && !p && zs.avail_out != 0)
|
|
|
|
goto terminate;
|
|
|
|
else
|
2001-10-15 07:55:37 +04:00
|
|
|
; /* once more. */
|
2000-09-22 00:28:52 +04:00
|
|
|
} else {
|
|
|
|
if (zs.msg) {
|
|
|
|
ipseclog((LOG_ERR, "ipcomp_%scompress: "
|
|
|
|
"%sflate(Z_NO_FLUSH): %s\n",
|
|
|
|
mode ? "de" : "", mode ? "in" : "de",
|
|
|
|
zs.msg));
|
|
|
|
} else {
|
|
|
|
ipseclog((LOG_ERR, "ipcomp_%scompress: "
|
|
|
|
"%sflate(Z_NO_FLUSH): unknown error (%d)\n",
|
|
|
|
mode ? "de" : "", mode ? "in" : "de",
|
|
|
|
zerror));
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-09-22 00:28:52 +04:00
|
|
|
mode ? inflateEnd(&zs) : deflateEnd(&zs);
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-09-22 00:28:52 +04:00
|
|
|
if (zerror == Z_STREAM_END)
|
|
|
|
goto terminate;
|
|
|
|
|
|
|
|
/* termination */
|
|
|
|
while (1) {
|
|
|
|
/* get output buffer */
|
|
|
|
if (zs.next_out == NULL || zs.avail_out == 0) {
|
|
|
|
MOREBLOCK();
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2002-03-14 08:18:10 +03:00
|
|
|
zerror = mode ? inflate(&zs, Z_SYNC_FLUSH)
|
2000-09-22 00:28:52 +04:00
|
|
|
: deflate(&zs, Z_FINISH);
|
|
|
|
|
|
|
|
if (zerror == Z_STREAM_END)
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
2002-03-14 08:18:10 +03:00
|
|
|
else if (zerror == Z_OK) {
|
|
|
|
if (mode && zs.avail_out != 0)
|
|
|
|
goto terminate;
|
|
|
|
else
|
|
|
|
; /* once more. */
|
|
|
|
} else {
|
2000-09-22 00:28:52 +04:00
|
|
|
if (zs.msg) {
|
|
|
|
ipseclog((LOG_ERR, "ipcomp_%scompress: "
|
|
|
|
"%sflate(Z_FINISH): %s\n",
|
|
|
|
mode ? "de" : "", mode ? "in" : "de",
|
|
|
|
zs.msg));
|
|
|
|
} else {
|
|
|
|
ipseclog((LOG_ERR, "ipcomp_%scompress: "
|
|
|
|
"%sflate(Z_FINISH): unknown error (%d)\n",
|
|
|
|
mode ? "de" : "", mode ? "in" : "de",
|
|
|
|
zerror));
|
|
|
|
}
|
2000-09-21 02:34:24 +04:00
|
|
|
mode ? inflateEnd(&zs) : deflateEnd(&zs);
|
1999-06-28 10:36:47 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2000-09-22 00:28:52 +04:00
|
|
|
|
|
|
|
terminate:
|
1999-06-28 10:36:47 +04:00
|
|
|
zerror = mode ? inflateEnd(&zs) : deflateEnd(&zs);
|
|
|
|
if (zerror != Z_OK) {
|
2000-09-22 00:28:52 +04:00
|
|
|
if (zs.msg) {
|
|
|
|
ipseclog((LOG_ERR, "ipcomp_%scompress: "
|
|
|
|
"%sflateEnd: %s\n",
|
|
|
|
mode ? "de" : "", mode ? "in" : "de",
|
|
|
|
zs.msg));
|
|
|
|
} else {
|
|
|
|
ipseclog((LOG_ERR, "ipcomp_%scompress: "
|
|
|
|
"%sflateEnd: unknown error (%d)\n",
|
|
|
|
mode ? "de" : "", mode ? "in" : "de",
|
|
|
|
zerror));
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* keep the final reply buffer into our chain */
|
|
|
|
if (n) {
|
|
|
|
n->m_len = zs.total_out - offset;
|
|
|
|
offset = zs.total_out;
|
|
|
|
*np = n;
|
|
|
|
np = &n->m_next;
|
2000-09-21 01:43:52 +04:00
|
|
|
n = NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* switch the mbuf to the new one */
|
|
|
|
mprev->m_next = n0;
|
|
|
|
m_freem(md);
|
|
|
|
*lenp = zs.total_out;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
2000-09-21 01:43:52 +04:00
|
|
|
if (n)
|
|
|
|
m_freem(n);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (n0)
|
|
|
|
m_freem(n0);
|
|
|
|
return error;
|
2000-09-22 00:28:52 +04:00
|
|
|
#undef MOREBLOCK
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
deflate_compress(m, md, lenp)
|
|
|
|
struct mbuf *m;
|
|
|
|
struct mbuf *md;
|
|
|
|
size_t *lenp;
|
|
|
|
{
|
|
|
|
if (!m)
|
|
|
|
panic("m == NULL in deflate_compress");
|
|
|
|
if (!md)
|
|
|
|
panic("md == NULL in deflate_compress");
|
|
|
|
if (!lenp)
|
|
|
|
panic("lenp == NULL in deflate_compress");
|
|
|
|
|
|
|
|
return deflate_common(m, md, lenp, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
deflate_decompress(m, md, lenp)
|
|
|
|
struct mbuf *m;
|
|
|
|
struct mbuf *md;
|
|
|
|
size_t *lenp;
|
|
|
|
{
|
|
|
|
if (!m)
|
|
|
|
panic("m == NULL in deflate_decompress");
|
|
|
|
if (!md)
|
|
|
|
panic("md == NULL in deflate_decompress");
|
|
|
|
if (!lenp)
|
|
|
|
panic("lenp == NULL in deflate_decompress");
|
|
|
|
|
|
|
|
return deflate_common(m, md, lenp, 1);
|
|
|
|
}
|