#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: xform_ipip.c,v 1.80 2025/06/11 02:44:13 ozaki-r Exp $");
#if defined(_KERNEL_OPT)
#include "opt_inet.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/protosw.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#include <netinet/ip_ecn.h>
#include <netinet/ip_var.h>
#include <netinet/ip_encap.h>
#include <netipsec/ipsec.h>
#include <netipsec/ipsec_private.h>
#include <netipsec/xform.h>
#include <netipsec/ipip_var.h>
#ifdef INET6
#include <netinet/ip6.h>
#include <netipsec/ipsec6.h>
#include <netinet6/in6_var.h>
#include <netinet6/ip6protosw.h>
#endif
#include <netipsec/key.h>
#include <netipsec/key_debug.h>
#define M_IPSEC (M_AUTHIPHDR|M_DECRYPTED)
int ipip_spoofcheck = 1;
percpu_t *ipipstat_percpu;
static void _ipip_input(struct mbuf *, int);
#ifdef INET6
static int
ip4_input6(struct mbuf **m, int *offp, int proto, void *eparg __unused)
{
_ipip_input(*m, *offp);
return IPPROTO_DONE;
}
#endif
#ifdef INET
static void
ip4_input(struct mbuf *m, int off, int proto, void *eparg __unused)
{
_ipip_input(m, off);
}
#endif
static void
_ipip_input(struct mbuf *m, int iphlen)
{
register struct sockaddr_in *sin;
register struct ifnet *ifp;
register struct ifaddr *ifa;
pktqueue_t *pktq = NULL;
struct ip *ip4 = NULL;
#ifdef INET6
register struct sockaddr_in6 *sin6;
struct ip6_hdr *ip6 = NULL;
uint8_t itos;
#endif
uint8_t otos;
uint8_t v;
int hlen;
IPIP_STATINC(IPIP_STAT_IPACKETS);
m_copydata(m, 0, 1, &v);
switch (v >> 4) {
#ifdef INET
case 4:
hlen = sizeof(struct ip);
break;
#endif
#ifdef INET6
case 6:
hlen = sizeof(struct ip6_hdr);
break;
#endif
default:
DPRINTF("bad protocol version 0x%x (%u) "
"for outer header\n", v, v>>4);
IPIP_STATINC(IPIP_STAT_FAMILY);
m_freem(m);
return;
}
if (m->m_len < hlen) {
if ((m = m_pullup(m, hlen)) == NULL) {
DPRINTF("m_pullup (1) failed\n");
IPIP_STATINC(IPIP_STAT_HDROPS);
return;
}
}
switch (v >> 4) {
#ifdef INET
case 4:
otos = mtod(m, struct ip *)->ip_tos;
break;
#endif
#ifdef INET6
case 6:
otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
break;
#endif
default:
panic("%s: impossible (1)", __func__);
}
m_adj(m, iphlen);
if (m->m_pkthdr.len < sizeof(struct ip)) {
IPIP_STATINC(IPIP_STAT_HDROPS);
m_freem(m);
return;
}
m_copydata(m, 0, 1, &v);
switch (v >> 4) {
#ifdef INET
case 4:
hlen = sizeof(struct ip);
pktq = ip_pktq;
break;
#endif
#ifdef INET6
case 6:
hlen = sizeof(struct ip6_hdr);
pktq = ip6_pktq;
break;
#endif
default:
DPRINTF("bad protocol version %#x (%u) "
"for inner header\n", v, v >> 4);
IPIP_STATINC(IPIP_STAT_FAMILY);
m_freem(m);
return;
}
if (m->m_len < hlen) {
if ((m = m_pullup(m, hlen)) == NULL) {
DPRINTF("m_pullup (2) failed\n");
IPIP_STATINC(IPIP_STAT_HDROPS);
return;
}
}
switch (v >> 4) {
#ifdef INET
case 4:
ip4 = mtod(m, struct ip *);
ip_ecn_egress(ip4_ipsec_ecn, &otos, &ip4->ip_tos);
break;
#endif
#ifdef INET6
case 6:
ip6 = mtod(m, struct ip6_hdr *);
itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
ip6->ip6_flow &= ~htonl(0xff << 20);
ip6->ip6_flow |= htonl((uint32_t)itos << 20);
break;
#endif
default:
panic("%s: impossible (2)", __func__);
}
if ((m_get_rcvif_NOMPSAFE(m) == NULL ||
!(m_get_rcvif_NOMPSAFE(m)->if_flags & IFF_LOOPBACK)) &&
ipip_spoofcheck) {
int s = pserialize_read_enter();
IFNET_READER_FOREACH(ifp) {
IFADDR_READER_FOREACH(ifa, ifp) {
#ifdef INET
if (ip4) {
if (ifa->ifa_addr->sa_family !=
AF_INET)
continue;
sin = (struct sockaddr_in *)ifa->ifa_addr;
if (sin->sin_addr.s_addr ==
ip4->ip_src.s_addr) {
pserialize_read_exit(s);
IPIP_STATINC(IPIP_STAT_SPOOF);
m_freem(m);
return;
}
}
#endif
#ifdef INET6
if (ip6) {
if (ifa->ifa_addr->sa_family !=
AF_INET6)
continue;
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
pserialize_read_exit(s);
IPIP_STATINC(IPIP_STAT_SPOOF);
m_freem(m);
return;
}
}
#endif
}
}
pserialize_read_exit(s);
}
IPIP_STATADD(IPIP_STAT_IBYTES, m->m_pkthdr.len);
int s = splnet();
if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
IPIP_STATINC(IPIP_STAT_QFULL);
m_freem(m);
}
splx(s);
}
int
ipip_output(struct mbuf *m, struct secasvar *sav, struct mbuf **mp)
{
char buf[IPSEC_ADDRSTRLEN];
uint8_t tp, otos;
struct secasindex *saidx;
int error, iphlen;
#ifdef INET
uint8_t itos;
struct ip *ipo;
#endif
#ifdef INET6
struct ip6_hdr *ip6, *ip6o;
#endif
KASSERT(sav != NULL);
m_copydata(m, 0, 1, &tp);
tp = (tp >> 4) & 0xff;
saidx = &sav->sah->saidx;
switch (saidx->dst.sa.sa_family) {
#ifdef INET
case AF_INET:
if (saidx->src.sa.sa_family != AF_INET ||
saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
DPRINTF("unspecified tunnel endpoint "
"address in SA %s/%08lx\n",
ipsec_address(&saidx->dst, buf, sizeof(buf)),
(u_long)ntohl(sav->spi));
IPIP_STATINC(IPIP_STAT_UNSPEC);
error = EINVAL;
goto bad;
}
M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
if (m == NULL) {
DPRINTF("M_PREPEND failed\n");
IPIP_STATINC(IPIP_STAT_HDROPS);
error = ENOBUFS;
goto bad;
}
iphlen = sizeof(struct ip);
ipo = mtod(m, struct ip *);
ipo->ip_v = IPVERSION;
ipo->ip_hl = 5;
ipo->ip_len = htons(m->m_pkthdr.len);
ipo->ip_ttl = ip_defttl;
ipo->ip_sum = 0;
ipo->ip_src = saidx->src.sin.sin_addr;
ipo->ip_dst = saidx->dst.sin.sin_addr;
ipo->ip_id = ip_newid();
if (tp == IPVERSION) {
m_copydata(m, sizeof(struct ip) +
offsetof(struct ip, ip_tos),
sizeof(uint8_t), &itos);
ipo->ip_p = IPPROTO_IPIP;
m_copydata(m, sizeof(struct ip) +
offsetof(struct ip, ip_off),
sizeof(uint16_t), &ipo->ip_off);
ipo->ip_off &= ~ htons(IP_DF | IP_MF | IP_OFFMASK);
}
#ifdef INET6
else if (tp == (IPV6_VERSION >> 4)) {
uint32_t itos32;
m_copydata(m, sizeof(struct ip) +
offsetof(struct ip6_hdr, ip6_flow),
sizeof(uint32_t), &itos32);
itos = ntohl(itos32) >> 20;
ipo->ip_p = IPPROTO_IPV6;
ipo->ip_off = 0;
}
#endif
else {
goto nofamily;
}
otos = 0;
ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
ipo->ip_tos = otos;
break;
#endif
#ifdef INET6
case AF_INET6:
if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
saidx->src.sa.sa_family != AF_INET6 ||
IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
DPRINTF("unspecified tunnel endpoint "
"address in SA %s/%08lx\n",
ipsec_address(&saidx->dst, buf, sizeof(buf)),
(u_long)ntohl(sav->spi));
IPIP_STATINC(IPIP_STAT_UNSPEC);
error = ENOBUFS;
goto bad;
}
if (tp == (IPV6_VERSION >> 4)) {
ip6 = mtod(m, struct ip6_hdr *);
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
ip6->ip6_src.s6_addr16[1] = 0;
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
ip6->ip6_dst.s6_addr16[1] = 0;
}
M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
if (m == NULL) {
DPRINTF("M_PREPEND failed\n");
IPIP_STATINC(IPIP_STAT_HDROPS);
error = ENOBUFS;
goto bad;
}
iphlen = sizeof(struct ip6_hdr);
ip6o = mtod(m, struct ip6_hdr *);
ip6o->ip6_flow = 0;
ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
ip6o->ip6_vfc |= IPV6_VERSION;
ip6o->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6o));
ip6o->ip6_hlim = ip_defttl;
ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
ip6o->ip6_src = saidx->src.sin6.sin6_addr;
if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_dst))
ip6o->ip6_dst.s6_addr16[1] = htons(saidx->dst.sin6.sin6_scope_id);
if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_src))
ip6o->ip6_src.s6_addr16[1] = htons(saidx->src.sin6.sin6_scope_id);
#ifdef INET
if (tp == IPVERSION) {
m_copydata(m, sizeof(struct ip6_hdr) +
offsetof(struct ip, ip_tos), sizeof(uint8_t),
&itos);
ip6o->ip6_nxt = IPPROTO_IPIP;
} else
#endif
if (tp == (IPV6_VERSION >> 4)) {
uint32_t itos32;
m_copydata(m, sizeof(struct ip6_hdr) +
offsetof(struct ip6_hdr, ip6_flow),
sizeof(uint32_t), &itos32);
itos = ntohl(itos32) >> 20;
ip6o->ip6_nxt = IPPROTO_IPV6;
} else {
goto nofamily;
}
otos = 0;
ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
ip6o->ip6_flow |= htonl((uint32_t)otos << 20);
break;
#endif
default:
nofamily:
DPRINTF("unsupported protocol family %u\n",
saidx->dst.sa.sa_family);
IPIP_STATINC(IPIP_STAT_FAMILY);
error = EAFNOSUPPORT;
goto bad;
}
IPIP_STATINC(IPIP_STAT_OPACKETS);
IPIP_STATADD(IPIP_STAT_OBYTES, m->m_pkthdr.len - iphlen);
*mp = m;
return 0;
bad:
m_freem(m);
*mp = NULL;
return error;
}
#ifdef INET
static struct encapsw ipe4_encapsw = {
.encapsw4 = {
.pr_input = ip4_input,
.pr_ctlinput = NULL,
}
};
#endif
#ifdef INET6
static struct encapsw ipe4_encapsw6 = {
.encapsw6 = {
.pr_input = ip4_input6,
.pr_ctlinput = NULL,
}
};
#endif
static int
ipe4_encapcheck(struct mbuf *m, int off, int proto, void *arg)
{
return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
}
static int
ipe4_init(struct secasvar *sav, const struct xformsw *xsp)
{
sav->tdb_xform = xsp;
return 0;
}
static void
ipe4_zeroize(struct secasvar *sav)
{
sav->tdb_xform = NULL;
}
static int
ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
{
printf("should never be called\n");
m_freem(m);
return EOPNOTSUPP;
}
static int
ipe4_output(struct mbuf *m, const struct ipsecrequest *isr,
struct secasvar *sav, int skip, int protoff, int flags)
{
panic("%s: should not have been called", __func__);
}
static struct xformsw ipe4_xformsw = {
.xf_type = XF_IP4,
.xf_flags = 0,
.xf_name = "IPv4 Simple Encapsulation",
.xf_init = ipe4_init,
.xf_zeroize = ipe4_zeroize,
.xf_input = ipe4_input,
.xf_output = ipe4_output,
.xf_next = NULL,
};
void
ipe4_attach(void)
{
ipipstat_percpu = percpu_alloc(sizeof(uint64_t) * IPIP_NSTATS);
xform_register(&ipe4_xformsw);
encapinit();
(void)encap_lock_enter();
#ifdef INET
(void)encap_attach_func(AF_INET, -1, ipe4_encapcheck, &ipe4_encapsw,
NULL);
#endif
#ifdef INET6
(void)encap_attach_func(AF_INET6, -1, ipe4_encapcheck, &ipe4_encapsw6,
NULL);
#endif
encap_lock_exit();
}