summaryrefslogtreecommitdiffstats
path: root/sys/netinet6/ipcomp_core.c
diff options
context:
space:
mode:
authorume <ume@FreeBSD.org>2001-06-11 12:39:29 +0000
committerume <ume@FreeBSD.org>2001-06-11 12:39:29 +0000
commit832f8d224926758a9ae0b23a6b45353e44fbc87a (patch)
treea79fc7ad2b97862c4a404f352f0211ad93a7b5f1 /sys/netinet6/ipcomp_core.c
parent2693854b01a52b0395a91322aa3edf926bddff38 (diff)
downloadFreeBSD-src-832f8d224926758a9ae0b23a6b45353e44fbc87a.zip
FreeBSD-src-832f8d224926758a9ae0b23a6b45353e44fbc87a.tar.gz
Sync with recent KAME.
This work was based on kame-20010528-freebsd43-snap.tgz and some critical problem after the snap was out were fixed. There are many many changes since last KAME merge. TODO: - The definitions of SADB_* in sys/net/pfkeyv2.h are still different from RFC2407/IANA assignment because of binary compatibility issue. It should be fixed under 5-CURRENT. - ip6po_m member of struct ip6_pktopts is no longer used. But, it is still there because of binary compatibility issue. It should be removed under 5-CURRENT. Reviewed by: itojun Obtained from: KAME MFC after: 3 weeks
Diffstat (limited to 'sys/netinet6/ipcomp_core.c')
-rw-r--r--sys/netinet6/ipcomp_core.c220
1 files changed, 130 insertions, 90 deletions
diff --git a/sys/netinet6/ipcomp_core.c b/sys/netinet6/ipcomp_core.c
index 1eee253..ec031f6 100644
--- a/sys/netinet6/ipcomp_core.c
+++ b/sys/netinet6/ipcomp_core.c
@@ -1,5 +1,5 @@
/* $FreeBSD$ */
-/* $KAME: ipcomp_core.c,v 1.12 2000/05/05 11:01:01 sumikawa Exp $ */
+/* $KAME: ipcomp_core.c,v 1.24 2000/10/23 04:24:22 itojun Exp $ */
/*
* Copyright (C) 1999 WIDE Project.
@@ -85,13 +85,20 @@ static int deflate_window_out = -12;
static const int deflate_window_in = -1 * MAX_WBITS; /* don't change it */
static int deflate_memlevel = MAX_MEM_LEVEL;
-struct ipcomp_algorithm ipcomp_algorithms[] = {
- { NULL, NULL, -1 },
- { NULL, NULL, -1 },
+static const struct ipcomp_algorithm ipcomp_algorithms[] = {
{ deflate_compress, deflate_decompress, 90 },
- { NULL, NULL, 90 },
};
+const struct ipcomp_algorithm *
+ipcomp_algorithm_lookup(idx)
+ int idx;
+{
+
+ if (idx == SADB_X_CALG_DEFLATE)
+ return &ipcomp_algorithms[0];
+ return NULL;
+}
+
static void *
deflate_alloc(aux, items, siz)
void *aux;
@@ -99,7 +106,7 @@ deflate_alloc(aux, items, siz)
u_int siz;
{
void *ptr;
- MALLOC(ptr, void *, items * siz, M_TEMP, M_NOWAIT);
+ ptr = malloc(items * siz, M_TEMP, M_NOWAIT);
return ptr;
}
@@ -108,7 +115,7 @@ deflate_free(aux, ptr)
void *aux;
void *ptr;
{
- FREE(ptr, M_TEMP);
+ free(ptr, M_TEMP);
}
static int
@@ -120,12 +127,47 @@ deflate_common(m, md, lenp, mode)
{
struct mbuf *mprev;
struct mbuf *p;
- struct mbuf *n, *n0 = NULL, **np;
+ struct mbuf *n = NULL, *n0 = NULL, **np;
z_stream zs;
int error = 0;
int zerror;
size_t offset;
- int firsttime, final, flush;
+
+#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)
for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
;
@@ -148,113 +190,107 @@ deflate_common(m, md, lenp, mode)
n0 = n = NULL;
np = &n0;
offset = 0;
- firsttime = 1;
- final = 0;
- flush = Z_NO_FLUSH;
zerror = 0;
p = md;
- while (1) {
- /*
- * first time, we need to setup the buffer before calling
- * compression function.
- */
- if (firsttime)
- firsttime = 0;
- else {
- zerror = mode ? inflate(&zs, flush)
- : deflate(&zs, flush);
- }
+ while (p && p->m_len == 0) {
+ p = p->m_next;
+ }
+ /* input stream and output stream are available */
+ while (p && zs.avail_in == 0) {
/* get input buffer */
if (p && zs.avail_in == 0) {
zs.next_in = mtod(p, u_int8_t *);
zs.avail_in = p->m_len;
p = p->m_next;
- if (!p) {
- final = 1;
- flush = Z_PARTIAL_FLUSH;
+ while (p && p->m_len == 0) {
+ p = p->m_next;
}
}
/* get output buffer */
if (zs.next_out == NULL || zs.avail_out == 0) {
- /* 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;
- }
+ MOREBLOCK();
+ }
- /* 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);
+ zerror = mode ? inflate(&zs, Z_NO_FLUSH)
+ : deflate(&zs, Z_NO_FLUSH);
+
+ if (zerror == Z_STREAM_END)
+ ; /*once more.*/
+ else if (zerror == Z_OK) {
+ /* inflate: Z_OK can indicate the end of decode */
+ if (mode && !p && zs.avail_out != 0)
+ goto terminate;
+ else
+ ; /*once more.*/
+ } 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));
}
+ mode ? inflateEnd(&zs) : deflateEnd(&zs);
+ error = EINVAL;
+ goto fail;
+ }
+ }
- zs.next_out = mtod(n, u_int8_t *);
- zs.avail_out = n->m_len;
+ if (zerror == Z_STREAM_END)
+ goto terminate;
+
+ /* termination */
+ while (1) {
+ /* get output buffer */
+ if (zs.next_out == NULL || zs.avail_out == 0) {
+ MOREBLOCK();
}
- if (zerror == Z_OK) {
- /*
- * to terminate deflate/inflate process, we need to
- * call {in,de}flate() with different flushing methods.
- *
- * deflate() needs at least one Z_PARTIAL_FLUSH,
- * then use Z_FINISH until we get to the end.
- * (if we use Z_FLUSH without Z_PARTIAL_FLUSH, deflate()
- * will assume contiguous single output buffer, and that
- * is not what we want)
- * inflate() does not care about flushing method, but
- * needs output buffer until it gets to the end.
- *
- * the most outer loop will be terminated with
- * Z_STREAM_END.
- */
- if (final == 1) {
- /* reached end of mbuf chain */
- if (mode == 0)
- final = 2;
- else
- final = 3;
- } else if (final == 2) {
- /* terminate deflate case */
- flush = Z_FINISH;
- } else if (final == 3) {
- /* terminate inflate case */
- ;
- }
- } else if (zerror == Z_STREAM_END)
+ zerror = mode ? inflate(&zs, Z_FINISH)
+ : deflate(&zs, Z_FINISH);
+
+ if (zerror == Z_STREAM_END)
break;
+ else if (zerror == Z_OK)
+ ; /*once more.*/
else {
- ipseclog((LOG_ERR, "ipcomp_%scompress: %sflate: %s\n",
- mode ? "de" : "", mode ? "in" : "de",
- zs.msg ? zs.msg : "unknown error"));
+ 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));
+ }
+ mode ? inflateEnd(&zs) : deflateEnd(&zs);
error = EINVAL;
goto fail;
}
}
+
+terminate:
zerror = mode ? inflateEnd(&zs) : deflateEnd(&zs);
if (zerror != Z_OK) {
- ipseclog((LOG_ERR, "ipcomp_%scompress: %sflate: %s\n",
- mode ? "de" : "", mode ? "in" : "de",
- zs.msg ? zs.msg : "unknown error"));
+ 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));
+ }
error = EINVAL;
goto fail;
}
@@ -264,6 +300,7 @@ deflate_common(m, md, lenp, mode)
offset = zs.total_out;
*np = n;
np = &n->m_next;
+ n = NULL;
}
/* switch the mbuf to the new one */
@@ -276,9 +313,12 @@ deflate_common(m, md, lenp, mode)
fail:
if (m)
m_freem(m);
+ if (n)
+ m_freem(n);
if (n0)
m_freem(n0);
return error;
+#undef MOREBLOCK
}
static int
OpenPOWER on IntegriCloud