2000-07-04 16:35:15 +00:00
|
|
|
/* $FreeBSD$ */
|
2001-06-11 12:39:29 +00:00
|
|
|
/* $KAME: ipcomp_input.c,v 1.25 2001/03/01 09:12:09 itojun Exp $ */
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 1999 WIDE Project.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opt_inet.h"
|
|
|
|
#include "opt_inet6.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.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/syslog.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/netisr.h>
|
|
|
|
#include <net/zlib.h>
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/ip_ecn.h>
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet6/ip6_var.h>
|
|
|
|
#endif
|
|
|
|
#include <netinet6/ipcomp.h>
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet6/ipcomp6.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <netinet6/ipsec.h>
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet6/ipsec6.h>
|
|
|
|
#endif
|
|
|
|
#include <netkey/key.h>
|
|
|
|
#include <netkey/keydb.h>
|
|
|
|
|
|
|
|
#include <machine/stdarg.h>
|
|
|
|
|
|
|
|
#include <net/net_osdep.h>
|
|
|
|
|
|
|
|
#define IPLEN_FLIPPED
|
|
|
|
|
|
|
|
#ifdef INET
|
2001-09-03 20:03:55 +00:00
|
|
|
extern struct protosw inetsw[];
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
void
|
2001-09-07 07:19:12 +00:00
|
|
|
ipcomp4_input(m, off)
|
2000-07-04 16:35:15 +00:00
|
|
|
struct mbuf *m;
|
2001-09-07 07:19:12 +00:00
|
|
|
int off;
|
2000-07-04 16:35:15 +00:00
|
|
|
{
|
2001-06-11 12:39:29 +00:00
|
|
|
struct mbuf *md;
|
2000-07-04 16:35:15 +00:00
|
|
|
struct ip *ip;
|
|
|
|
struct ipcomp *ipcomp;
|
2001-06-11 12:39:29 +00:00
|
|
|
const struct ipcomp_algorithm *algo;
|
2000-07-04 16:35:15 +00:00
|
|
|
u_int16_t cpi; /* host order */
|
|
|
|
u_int16_t nxt;
|
|
|
|
size_t hlen;
|
|
|
|
int error;
|
|
|
|
size_t newlen, olen;
|
|
|
|
struct secasvar *sav = NULL;
|
2001-09-07 07:19:12 +00:00
|
|
|
int proto;
|
2000-07-04 16:35:15 +00:00
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
|
2000-07-04 16:35:15 +00:00
|
|
|
ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
|
2001-06-11 12:39:29 +00:00
|
|
|
"(packet too short)\n"));
|
2000-07-04 16:35:15 +00:00
|
|
|
ipsecstat.in_inval++;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
|
|
|
|
if (!m) {
|
2002-04-19 04:46:24 +00:00
|
|
|
m = NULL; /* already freed */
|
2001-06-11 12:39:29 +00:00
|
|
|
ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
|
|
|
|
"(pulldown failure)\n"));
|
|
|
|
ipsecstat.in_inval++;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ipcomp = mtod(md, struct ipcomp *);
|
2000-07-04 16:35:15 +00:00
|
|
|
ip = mtod(m, struct ip *);
|
2001-09-07 07:19:12 +00:00
|
|
|
proto = ip->ip_p;
|
2000-07-04 16:35:15 +00:00
|
|
|
nxt = ipcomp->comp_nxt;
|
|
|
|
#ifdef _IP_VHL
|
|
|
|
hlen = IP_VHL_HL(ip->ip_vhl) << 2;
|
|
|
|
#else
|
|
|
|
hlen = ip->ip_hl << 2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cpi = ntohs(ipcomp->comp_cpi);
|
|
|
|
|
|
|
|
if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
|
|
|
|
sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
|
|
|
|
(caddr_t)&ip->ip_dst, IPPROTO_IPCOMP, htonl(cpi));
|
|
|
|
if (sav != NULL
|
|
|
|
&& (sav->state == SADB_SASTATE_MATURE
|
|
|
|
|| sav->state == SADB_SASTATE_DYING)) {
|
2002-04-19 04:46:24 +00:00
|
|
|
cpi = sav->alg_enc; /* XXX */
|
2000-07-04 16:35:15 +00:00
|
|
|
/* other parameters to look at? */
|
|
|
|
}
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
algo = ipcomp_algorithm_lookup(cpi);
|
2000-07-04 16:35:15 +00:00
|
|
|
if (!algo) {
|
|
|
|
ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
|
|
|
|
cpi));
|
|
|
|
ipsecstat.in_nosa++;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* chop ipcomp header */
|
|
|
|
ipcomp = NULL;
|
2001-06-11 12:39:29 +00:00
|
|
|
md->m_data += sizeof(struct ipcomp);
|
|
|
|
md->m_len -= sizeof(struct ipcomp);
|
2000-07-04 16:35:15 +00:00
|
|
|
m->m_pkthdr.len -= sizeof(struct ipcomp);
|
|
|
|
#ifdef IPLEN_FLIPPED
|
|
|
|
ip->ip_len -= sizeof(struct ipcomp);
|
|
|
|
#else
|
|
|
|
ip->ip_len = htons(ntohs(ip->ip_len) - sizeof(struct ipcomp));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
olen = m->m_pkthdr.len;
|
|
|
|
newlen = m->m_pkthdr.len - off;
|
|
|
|
error = (*algo->decompress)(m, m->m_next, &newlen);
|
|
|
|
if (error != 0) {
|
|
|
|
if (error == EINVAL)
|
|
|
|
ipsecstat.in_inval++;
|
|
|
|
else if (error == ENOBUFS)
|
|
|
|
ipsecstat.in_nomem++;
|
|
|
|
m = NULL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ipsecstat.in_comphist[cpi]++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* returning decompressed packet onto icmp is meaningless.
|
|
|
|
* mark it decrypted to prevent icmp from attaching original packet.
|
|
|
|
*/
|
|
|
|
m->m_flags |= M_DECRYPTED;
|
|
|
|
|
|
|
|
m->m_pkthdr.len = off + newlen;
|
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
#ifdef IPLEN_FLIPPED
|
|
|
|
len = ip->ip_len;
|
|
|
|
#else
|
|
|
|
len = ntohs(ip->ip_len);
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* be careful about underflow. also, do not assign exact value
|
|
|
|
* as ip_len is manipulated differently on *BSDs.
|
|
|
|
*/
|
|
|
|
len += m->m_pkthdr.len;
|
|
|
|
len -= olen;
|
|
|
|
if (len & ~0xffff) {
|
|
|
|
/* packet too big after decompress */
|
|
|
|
ipsecstat.in_inval++;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#ifdef IPLEN_FLIPPED
|
|
|
|
ip->ip_len = len & 0xffff;
|
|
|
|
#else
|
|
|
|
ip->ip_len = htons(len & 0xffff);
|
|
|
|
#endif
|
|
|
|
ip->ip_p = nxt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sav) {
|
|
|
|
key_sa_recordxfer(sav, m);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
|
|
|
|
ipsecstat.in_nomem++;
|
|
|
|
goto fail;
|
|
|
|
}
|
2000-07-04 16:35:15 +00:00
|
|
|
key_freesav(sav);
|
|
|
|
sav = NULL;
|
|
|
|
}
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if (nxt != IPPROTO_DONE) {
|
|
|
|
if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
|
|
|
|
ipsec4_in_reject(m, NULL)) {
|
|
|
|
ipsecstat.in_polvio++;
|
|
|
|
goto fail;
|
|
|
|
}
|
2001-09-03 20:03:55 +00:00
|
|
|
(*inetsw[ip_protox[nxt]].pr_input)(m, off);
|
2001-06-11 12:39:29 +00:00
|
|
|
} else
|
2000-07-04 16:35:15 +00:00
|
|
|
m_freem(m);
|
|
|
|
m = NULL;
|
|
|
|
|
|
|
|
ipsecstat.in_success++;
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (sav)
|
|
|
|
key_freesav(sav);
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* INET */
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
int
|
|
|
|
ipcomp6_input(mp, offp, proto)
|
|
|
|
struct mbuf **mp;
|
|
|
|
int *offp, proto;
|
|
|
|
{
|
|
|
|
struct mbuf *m, *md;
|
|
|
|
int off;
|
|
|
|
struct ip6_hdr *ip6;
|
|
|
|
struct ipcomp *ipcomp;
|
2001-06-11 12:39:29 +00:00
|
|
|
const struct ipcomp_algorithm *algo;
|
2000-07-04 16:35:15 +00:00
|
|
|
u_int16_t cpi; /* host order */
|
|
|
|
u_int16_t nxt;
|
|
|
|
int error;
|
|
|
|
size_t newlen;
|
|
|
|
struct secasvar *sav = NULL;
|
2001-06-11 12:39:29 +00:00
|
|
|
char *prvnxtp;
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
m = *mp;
|
|
|
|
off = *offp;
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
|
|
|
|
if (!m) {
|
2002-04-19 04:46:24 +00:00
|
|
|
m = NULL; /* already freed */
|
2001-06-11 12:39:29 +00:00
|
|
|
ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
|
|
|
|
"(pulldown failure)\n"));
|
|
|
|
ipsec6stat.in_inval++;
|
2000-07-04 16:35:15 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
ipcomp = mtod(md, struct ipcomp *);
|
2000-07-04 16:35:15 +00:00
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
|
|
|
nxt = ipcomp->comp_nxt;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
2000-07-04 16:35:15 +00:00
|
|
|
cpi = ntohs(ipcomp->comp_cpi);
|
|
|
|
|
|
|
|
if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
|
|
|
|
sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
|
2003-10-08 18:26:08 +00:00
|
|
|
(caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
|
2000-07-04 16:35:15 +00:00
|
|
|
if (sav != NULL
|
|
|
|
&& (sav->state == SADB_SASTATE_MATURE
|
|
|
|
|| sav->state == SADB_SASTATE_DYING)) {
|
2002-04-19 04:46:24 +00:00
|
|
|
cpi = sav->alg_enc; /* XXX */
|
2000-07-04 16:35:15 +00:00
|
|
|
/* other parameters to look at? */
|
|
|
|
}
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
algo = ipcomp_algorithm_lookup(cpi);
|
2000-07-04 16:35:15 +00:00
|
|
|
if (!algo) {
|
|
|
|
ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
|
|
|
|
"dropping the packet for simplicity\n", cpi));
|
|
|
|
ipsec6stat.in_nosa++;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
/* chop ipcomp header */
|
|
|
|
ipcomp = NULL;
|
|
|
|
md->m_data += sizeof(struct ipcomp);
|
|
|
|
md->m_len -= sizeof(struct ipcomp);
|
|
|
|
m->m_pkthdr.len -= sizeof(struct ipcomp);
|
|
|
|
|
|
|
|
newlen = m->m_pkthdr.len - off;
|
2000-07-04 16:35:15 +00:00
|
|
|
error = (*algo->decompress)(m, md, &newlen);
|
|
|
|
if (error != 0) {
|
|
|
|
if (error == EINVAL)
|
|
|
|
ipsec6stat.in_inval++;
|
|
|
|
else if (error == ENOBUFS)
|
|
|
|
ipsec6stat.in_nomem++;
|
|
|
|
m = NULL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ipsec6stat.in_comphist[cpi]++;
|
2001-06-11 12:39:29 +00:00
|
|
|
m->m_pkthdr.len = off + newlen;
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* returning decompressed packet onto icmp is meaningless.
|
|
|
|
* mark it decrypted to prevent icmp from attaching original packet.
|
|
|
|
*/
|
|
|
|
m->m_flags |= M_DECRYPTED;
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
/* update next header field */
|
2000-07-04 16:35:15 +00:00
|
|
|
prvnxtp = ip6_get_prevhdr(m, off);
|
|
|
|
*prvnxtp = nxt;
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
/*
|
|
|
|
* no need to adjust payload length, as all the IPv6 protocols
|
|
|
|
* look at m->m_pkthdr.len
|
|
|
|
*/
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
if (sav) {
|
|
|
|
key_sa_recordxfer(sav, m);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
|
|
|
|
ipsec6stat.in_nomem++;
|
|
|
|
goto fail;
|
|
|
|
}
|
2000-07-04 16:35:15 +00:00
|
|
|
key_freesav(sav);
|
|
|
|
sav = NULL;
|
|
|
|
}
|
|
|
|
*offp = off;
|
|
|
|
*mp = m;
|
|
|
|
ipsec6stat.in_success++;
|
|
|
|
return nxt;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
if (sav)
|
|
|
|
key_freesav(sav);
|
|
|
|
return IPPROTO_DONE;
|
|
|
|
}
|
|
|
|
#endif /* INET6 */
|