#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/mcache.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>
#include <net/if.h>
#include <net/route.h>
#include <libkern/zlib.h>
#include <kern/cpu_number.h>
#include <kern/locks.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>
#include <netinet/kpi_ipfilter_var.h>
#if INET6
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#endif
#include <netinet6/ipcomp.h>
#if INET6
#include <netinet6/ipcomp6.h>
#endif
#include <netinet6/ipsec.h>
#if INET6
#include <netinet6/ipsec6.h>
#endif
#include <netkey/key.h>
#include <netkey/keydb.h>
#include <net/net_osdep.h>
#include <mach/sdt.h>
#define IPLEN_FLIPPED
void
ipcomp4_input(struct mbuf *m, int off)
{
struct mbuf *md;
struct ip *ip;
struct ipcomp *ipcomp;
const struct ipcomp_algorithm *algo;
u_int16_t cpi;
u_int16_t nxt;
size_t hlen;
int error;
size_t newlen, olen;
struct secasvar *sav = NULL;
if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
"(packet too short)\n"));
IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
goto fail;
}
md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
if (!md) {
m = NULL;
ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
"(pulldown failure)\n"));
IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
goto fail;
}
ipcomp = mtod(md, struct ipcomp *);
MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
ip = mtod(m, struct ip *);
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)) {
cpi = sav->alg_enc;
}
}
algo = ipcomp_algorithm_lookup(cpi);
if (!algo) {
ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
cpi));
IPSEC_STAT_INCREMENT(ipsecstat.in_nosa);
goto fail;
}
ipcomp = NULL;
md->m_data += sizeof(struct ipcomp);
md->m_len -= sizeof(struct ipcomp);
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) {
IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
} else if (error == ENOBUFS)
IPSEC_STAT_INCREMENT(ipsecstat.in_nomem);
m = NULL;
goto fail;
}
IPSEC_STAT_INCREMENT(ipsecstat.in_comphist[cpi]);
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
len += m->m_pkthdr.len;
len -= olen;
if (len & ~0xffff) {
IPSEC_STAT_INCREMENT(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);
if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
IPSEC_STAT_INCREMENT(ipsecstat.in_nomem);
goto fail;
}
key_freesav(sav, KEY_SADB_UNLOCKED);
sav = NULL;
}
if (nxt != IPPROTO_DONE) {
if ((ip_protox[nxt]->pr_flags & PR_LASTHDR) != 0 &&
ipsec4_in_reject(m, NULL)) {
IPSEC_STAT_INCREMENT(ipsecstat.in_polvio);
goto fail;
}
DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL,
struct ip *, ip, struct ifnet *, m->m_pkthdr.rcvif,
struct ip *, ip, struct ip6_hdr *, NULL);
ip_proto_dispatch_in(m, off, nxt, 0);
} else
m_freem(m);
m = NULL;
IPSEC_STAT_INCREMENT(ipsecstat.in_success);
return;
fail:
if (sav)
key_freesav(sav, KEY_SADB_UNLOCKED);
if (m)
m_freem(m);
return;
}
#if INET6
int
ipcomp6_input(struct mbuf **mp, int *offp, int proto)
{
#pragma unused(proto)
struct mbuf *m, *md;
int off;
struct ip6_hdr *ip6;
struct ipcomp *ipcomp;
const struct ipcomp_algorithm *algo;
u_int16_t cpi;
u_int16_t nxt;
int error;
size_t newlen;
struct secasvar *sav = NULL;
char *prvnxtp;
m = *mp;
off = *offp;
md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
if (!md) {
m = NULL;
ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
"(pulldown failure)\n"));
IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
goto fail;
}
ipcomp = mtod(md, struct ipcomp *);
MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
ip6 = mtod(m, struct ip6_hdr *);
nxt = ipcomp->comp_nxt;
cpi = ntohs(ipcomp->comp_cpi);
if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
(caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
if (sav != NULL
&& (sav->state == SADB_SASTATE_MATURE
|| sav->state == SADB_SASTATE_DYING)) {
cpi = sav->alg_enc;
}
}
algo = ipcomp_algorithm_lookup(cpi);
if (!algo) {
ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
"dropping the packet for simplicity\n", cpi));
IPSEC_STAT_INCREMENT(ipsec6stat.in_nosa);
goto fail;
}
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;
error = (*algo->decompress)(m, md, &newlen);
if (error != 0) {
if (error == EINVAL) {
IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
} else if (error == ENOBUFS)
IPSEC_STAT_INCREMENT(ipsec6stat.in_nomem);
m = NULL;
goto fail;
}
IPSEC_STAT_INCREMENT(ipsec6stat.in_comphist[cpi]);
m->m_pkthdr.len = off + newlen;
m->m_flags |= M_DECRYPTED;
prvnxtp = ip6_get_prevhdr(m, off);
*prvnxtp = nxt;
if (sav) {
key_sa_recordxfer(sav, m);
if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
IPSEC_STAT_INCREMENT(ipsec6stat.in_nomem);
goto fail;
}
key_freesav(sav, KEY_SADB_UNLOCKED);
sav = NULL;
}
*offp = off;
*mp = m;
IPSEC_STAT_INCREMENT(ipsec6stat.in_success);
return nxt;
fail:
if (m)
m_freem(m);
if (sav)
key_freesav(sav, KEY_SADB_UNLOCKED);
return IPPROTO_DONE;
}
#endif