#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.87 2024/07/05 04:31:54 rin Exp $");
#if defined(_KERNEL_OPT)
#include "opt_inet.h"
#include "opt_net_mpsafe.h"
#endif
#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/syslog.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/in_var.h>
#include <netinet/ip_ecn.h>
#include <netinet/ip6.h>
#ifdef INET6
#include <netinet6/ip6_var.h>
#endif
#include <netinet/in_pcb.h>
#ifdef INET6
#include <netinet/icmp6.h>
#endif
#include <netinet/udp.h>
#include <netipsec/ipsec.h>
#include <netipsec/ipsec_var.h>
#include <netipsec/ipsec_private.h>
#ifdef INET6
#include <netipsec/ipsec6.h>
#endif
#include <netipsec/ah_var.h>
#include <netipsec/esp_var.h>
#include <netipsec/ipcomp_var.h>
#include <netipsec/xform.h>
#include <netipsec/key.h>
#include <netipsec/keydb.h>
#include <netipsec/key_debug.h>
static percpu_t *ipsec_rtcache_percpu __cacheline_aligned;
static int
ipsec_register_done(struct mbuf *m, int *error)
{
struct m_tag *mtag;
mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 0, M_NOWAIT);
if (mtag == NULL) {
IPSECLOG(LOG_DEBUG, "could not get packet tag\n");
*error = ENOMEM;
return -1;
}
m_tag_prepend(m, mtag);
return 0;
}
static int
ipsec_reinject_ipstack(struct mbuf *m, int af, int flags)
{
int rv = -1;
struct route *ro;
KASSERT(af == AF_INET || af == AF_INET6);
KERNEL_LOCK_UNLESS_NET_MPSAFE();
ro = rtcache_percpu_getref(ipsec_rtcache_percpu);
switch (af) {
#ifdef INET
case AF_INET:
rv = ip_output(m, NULL, ro, IP_RAWOUTPUT|IP_NOIPNEWID,
NULL, NULL);
break;
#endif
#ifdef INET6
case AF_INET6:
rv = ip6_output(m, NULL, ro, flags, NULL, NULL, NULL);
break;
#endif
}
rtcache_percpu_putref(ipsec_rtcache_percpu);
KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
return rv;
}
int
ipsec_process_done(struct mbuf *m, const struct ipsecrequest *isr,
struct secasvar *sav, int flags)
{
struct secasindex *saidx;
int error;
#ifdef INET
struct ip *ip;
#endif
#ifdef INET6
struct ip6_hdr *ip6;
#endif
struct mbuf *mo;
struct udphdr *udp = NULL;
int hlen, roff, iphlen;
KASSERT(m != NULL);
KASSERT(isr != NULL);
KASSERT(sav != NULL);
saidx = &sav->sah->saidx;
if (sav->natt_type != 0) {
hlen = sizeof(struct udphdr);
switch (saidx->dst.sa.sa_family) {
#ifdef INET
case AF_INET:
ip = mtod(m, struct ip *);
mo = m_makespace(m, sizeof(struct ip), hlen, &roff);
iphlen = ip->ip_hl << 2;
break;
#endif
#ifdef INET6
case AF_INET6:
ip6 = mtod(m, struct ip6_hdr *);
mo = m_makespace(m, sizeof(struct ip6_hdr), hlen, &roff);
iphlen = sizeof(*ip6);
break;
#endif
default:
IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
saidx->dst.sa.sa_family);
error = ENXIO;
goto bad;
}
if (mo == NULL) {
char buf[IPSEC_ADDRSTRLEN];
IPSECLOG(LOG_DEBUG,
"failed to inject %u byte UDP for SA %s/%08lx\n",
hlen, ipsec_address(&saidx->dst, buf, sizeof(buf)),
(u_long)ntohl(sav->spi));
error = ENOBUFS;
goto bad;
}
udp = (struct udphdr *)(mtod(mo, char *) + roff);
udp->uh_sport = key_portfromsaddr(&saidx->src);
udp->uh_dport = key_portfromsaddr(&saidx->dst);
udp->uh_sum = 0;
udp->uh_ulen = htons(m->m_pkthdr.len - iphlen);
}
switch (saidx->dst.sa.sa_family) {
#ifdef INET
case AF_INET:
ip = mtod(m, struct ip *);
ip->ip_len = htons(m->m_pkthdr.len);
if (sav->natt_type != 0)
ip->ip_p = IPPROTO_UDP;
break;
#endif
#ifdef INET6
case AF_INET6:
if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
error = ENXIO;
goto bad;
}
if (m->m_pkthdr.len - sizeof(struct ip6_hdr) > IPV6_MAXPACKET) {
error = ENXIO;
goto bad;
}
ip6 = mtod(m, struct ip6_hdr *);
ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
if (sav->natt_type != 0) {
ip6->ip6_nxt = IPPROTO_UDP;
ipsec6_udp_cksum(m);
}
break;
#endif
default:
IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
saidx->dst.sa.sa_family);
error = ENXIO;
goto bad;
}
key_sa_recordxfer(sav, m);
if (isr->next) {
IPSEC_STATINC(IPSEC_STAT_OUT_BUNDLESA);
switch (saidx->dst.sa.sa_family) {
#ifdef INET
case AF_INET:
return ipsec4_process_packet(m, isr->next, NULL);
#endif
#ifdef INET6
case AF_INET6:
return ipsec6_process_packet(m, isr->next, flags);
#endif
default:
IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
saidx->dst.sa.sa_family);
error = ENXIO;
goto bad;
}
}
if (ipsec_register_done(m, &error) < 0)
goto bad;
return ipsec_reinject_ipstack(m, saidx->dst.sa.sa_family, flags);
bad:
m_freem(m);
return error;
}
static void
ipsec_fill_saidx_bymbuf(struct secasindex *saidx, const struct mbuf *m,
const int af)
{
struct m_tag *mtag;
u_int16_t natt_src = IPSEC_PORT_ANY;
u_int16_t natt_dst = IPSEC_PORT_ANY;
mtag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS);
if (mtag) {
u_int16_t *natt_ports;
natt_ports = (u_int16_t *)(mtag + 1);
natt_src = natt_ports[1];
natt_dst = natt_ports[0];
}
if (af == AF_INET) {
struct sockaddr_in *sin;
struct ip *ip = mtod(m, struct ip *);
if (saidx->src.sa.sa_len == 0) {
sin = &saidx->src.sin;
sin->sin_len = sizeof(*sin);
sin->sin_family = AF_INET;
sin->sin_port = natt_src;
sin->sin_addr = ip->ip_src;
}
if (saidx->dst.sa.sa_len == 0) {
sin = &saidx->dst.sin;
sin->sin_len = sizeof(*sin);
sin->sin_family = AF_INET;
sin->sin_port = natt_dst;
sin->sin_addr = ip->ip_dst;
}
} else {
struct sockaddr_in6 *sin6;
struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
if (saidx->src.sin6.sin6_len == 0) {
sin6 = (struct sockaddr_in6 *)&saidx->src;
sin6->sin6_len = sizeof(*sin6);
sin6->sin6_family = AF_INET6;
sin6->sin6_port = natt_src;
sin6->sin6_addr = ip6->ip6_src;
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
sin6->sin6_addr.s6_addr16[1] = 0;
sin6->sin6_scope_id =
ntohs(ip6->ip6_src.s6_addr16[1]);
}
}
if (saidx->dst.sin6.sin6_len == 0) {
sin6 = (struct sockaddr_in6 *)&saidx->dst;
sin6->sin6_len = sizeof(*sin6);
sin6->sin6_family = AF_INET6;
sin6->sin6_port = natt_dst;
sin6->sin6_addr = ip6->ip6_dst;
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
sin6->sin6_addr.s6_addr16[1] = 0;
sin6->sin6_scope_id =
ntohs(ip6->ip6_dst.s6_addr16[1]);
}
}
}
}
struct secasvar *
ipsec_lookup_sa(const struct ipsecrequest *isr, const struct mbuf *m)
{
struct secasindex saidx;
saidx = isr->saidx;
if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
ipsec_fill_saidx_bymbuf(&saidx, m, isr->saidx.dst.sa.sa_family);
}
return key_lookup_sa_bysaidx(&saidx);
}
static const struct ipsecrequest *
ipsec_nextisr(struct mbuf *m, const struct ipsecrequest *isr, int af,
int *error, struct secasvar **ret)
{
#define IPSEC_OSTAT(type) \
do { \
switch (isr->saidx.proto) { \
case IPPROTO_ESP: \
ESP_STATINC(ESP_STAT_ ## type); \
break; \
case IPPROTO_AH: \
AH_STATINC(AH_STAT_ ## type); \
break; \
default: \
IPCOMP_STATINC(IPCOMP_STAT_ ## type); \
break; \
} \
} while (0)
struct secasvar *sav = NULL;
struct secasindex saidx;
KASSERTMSG(af == AF_INET || af == AF_INET6,
"invalid address family %u", af);
again:
saidx = isr->saidx;
if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
ipsec_fill_saidx_bymbuf(&saidx, m, af);
}
*error = key_checkrequest(isr, &saidx, &sav);
if (*error != 0) {
IPSEC_STATINC(IPSEC_STAT_OUT_NOSA);
goto bad;
}
if (sav == NULL) {
KASSERTMSG(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
"no SA found, but required; level %u",
ipsec_get_reqlevel(isr));
isr = isr->next;
if (isr == NULL) {
*ret = NULL;
*error = 0;
return isr;
}
goto again;
}
if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
(isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
(isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
IPSECLOG(LOG_DEBUG, "IPsec outbound packet dropped due"
" to policy (check your sysctls)\n");
IPSEC_OSTAT(PDROPS);
*error = EHOSTUNREACH;
KEY_SA_UNREF(&sav);
goto bad;
}
KASSERT(sav->tdb_xform != NULL);
*ret = sav;
return isr;
bad:
KASSERTMSG(*error != 0, "error return w/ no error code");
return NULL;
#undef IPSEC_OSTAT
}
#ifdef INET
int
ipsec4_process_packet(struct mbuf *m, const struct ipsecrequest *isr,
u_long *mtu)
{
struct secasvar *sav = NULL;
struct ip *ip;
int error, i, off;
union sockaddr_union *dst;
int setdf;
KASSERT(m != NULL);
KASSERT(m->m_nextpkt == NULL);
KASSERT(isr != NULL);
isr = ipsec_nextisr(m, isr, AF_INET, &error, &sav);
if (isr == NULL) {
if (error != 0) {
goto bad;
} else {
if (ipsec_register_done(m, &error) < 0)
goto bad;
return ipsec_reinject_ipstack(m, AF_INET, 0);
}
}
KASSERT(sav != NULL);
if (m->m_len < sizeof(struct ip) &&
(m = m_pullup(m, sizeof(struct ip))) == NULL) {
error = ENOBUFS;
goto unrefsav;
}
if (isr == isr->sp->req) {
KASSERT(mtu != NULL);
ip = mtod(m, struct ip *);
if (!(sav->natt_type & UDP_ENCAP_ESPINUDP)) {
goto noneed;
}
if (ntohs(ip->ip_len) <= sav->esp_frag)
goto noneed;
*mtu = sav->esp_frag;
KEY_SA_UNREF(&sav);
return 0;
}
noneed:
dst = &sav->sah->saidx.dst;
if (dst->sa.sa_family == AF_INET) {
ip = mtod(m, struct ip *);
switch (ip4_ipsec_dfbit) {
case 0:
case 1:
setdf = ip4_ipsec_dfbit;
break;
default:
setdf = ip->ip_off;
setdf = ntohs(setdf);
setdf = htons(setdf & IP_DF);
break;
}
} else {
ip = NULL;
setdf = 0;
}
if (isr->saidx.mode == IPSEC_MODE_TUNNEL ||
dst->sa.sa_family != AF_INET ||
(dst->sa.sa_family == AF_INET &&
dst->sin.sin_addr.s_addr != INADDR_ANY &&
dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
struct mbuf *mp;
ip = mtod(m, struct ip *);
ip->ip_len = htons(m->m_pkthdr.len);
ip->ip_sum = 0;
ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
error = ipip_output(m, sav, &mp);
if (mp == NULL && !error) {
IPSECLOG(LOG_DEBUG,
"ipip_output returns no mbuf and no error!");
error = EFAULT;
}
if (error) {
if (mp) {
m_freem(mp);
}
m = NULL;
goto unrefsav;
}
m = mp, mp = NULL;
if (dst->sa.sa_family == AF_INET && setdf) {
if (m->m_len < sizeof(struct ip) &&
(m = m_pullup(m, sizeof(struct ip))) == NULL) {
error = ENOBUFS;
goto unrefsav;
}
ip = mtod(m, struct ip *);
ip->ip_off |= htons(IP_DF);
}
}
if (sav->tdb_xform->xf_type != XF_IP4) {
if (dst->sa.sa_family == AF_INET) {
ip = mtod(m, struct ip *);
i = ip->ip_hl << 2;
off = offsetof(struct ip, ip_p);
} else {
i = sizeof(struct ip6_hdr);
off = offsetof(struct ip6_hdr, ip6_nxt);
}
error = (*sav->tdb_xform->xf_output)(m, isr, sav, i, off, 0);
} else {
error = ipsec_process_done(m, isr, sav, 0);
}
KEY_SA_UNREF(&sav);
return error;
unrefsav:
KEY_SA_UNREF(&sav);
bad:
m_freem(m);
return error;
}
#endif
#ifdef INET6
static int
compute_ipsec_pos(struct mbuf *m, int *i, int *off)
{
struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
struct ip6_ext ip6e;
int dstopt = 0;
int nxt;
*i = sizeof(struct ip6_hdr);
*off = offsetof(struct ip6_hdr, ip6_nxt);
nxt = ip6->ip6_nxt;
while (1) {
switch (nxt) {
case IPPROTO_AH:
case IPPROTO_ESP:
case IPPROTO_IPCOMP:
return 0;
case IPPROTO_HOPOPTS:
case IPPROTO_DSTOPTS:
case IPPROTO_ROUTING:
if (*i + sizeof(ip6e) > m->m_pkthdr.len) {
return EINVAL;
}
if (nxt == IPPROTO_DSTOPTS && dstopt)
return 0;
if (nxt == IPPROTO_DSTOPTS) {
dstopt = 1;
} else if (nxt == IPPROTO_ROUTING) {
dstopt = 2;
}
m_copydata(m, *i, sizeof(ip6e), &ip6e);
nxt = ip6e.ip6e_nxt;
*off = *i + offsetof(struct ip6_ext, ip6e_nxt);
*i += (ip6e.ip6e_len + 1) << 3;
if (*i > m->m_pkthdr.len) {
return EINVAL;
}
break;
default:
return 0;
}
}
return 0;
}
static int
in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa,
const struct in6_addr *ia)
{
struct in6_addr ia2;
memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
return IN6_ARE_ADDR_EQUAL(ia, &ia2);
}
int
ipsec6_process_packet(struct mbuf *m, const struct ipsecrequest *isr, int flags)
{
struct secasvar *sav = NULL;
struct ip6_hdr *ip6;
int error, i, off;
union sockaddr_union *dst;
KASSERT(m != NULL);
KASSERT(m->m_nextpkt == NULL);
KASSERT(isr != NULL);
isr = ipsec_nextisr(m, isr, AF_INET6, &error, &sav);
if (isr == NULL) {
if (error != 0) {
goto bad;
} else {
if (ipsec_register_done(m, &error) < 0)
goto bad;
return ipsec_reinject_ipstack(m, AF_INET6, flags);
}
}
KASSERT(sav != NULL);
dst = &sav->sah->saidx.dst;
if (m->m_len < sizeof(struct ip6_hdr)) {
if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL) {
error = ENOBUFS;
goto unrefsav;
}
}
ip6 = mtod(m, struct ip6_hdr *);
if (isr->saidx.mode == IPSEC_MODE_TUNNEL ||
dst->sa.sa_family != AF_INET6 ||
((dst->sa.sa_family == AF_INET6) &&
(!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
(!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) {
struct mbuf *mp;
if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
error = ENXIO;
goto unrefsav;
}
ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
error = ipip_output(m, sav, &mp);
if (mp == NULL && !error) {
IPSECLOG(LOG_DEBUG,
"ipip_output returns no mbuf and no error!");
error = EFAULT;
}
if (error) {
if (mp) {
m_freem(mp);
}
m = NULL;
goto unrefsav;
}
m = mp;
mp = NULL;
}
if (dst->sa.sa_family == AF_INET) {
struct ip *ip;
ip = mtod(m, struct ip *);
i = ip->ip_hl << 2;
off = offsetof(struct ip, ip_p);
} else {
error = compute_ipsec_pos(m, &i, &off);
if (error)
goto unrefsav;
}
error = (*sav->tdb_xform->xf_output)(m, isr, sav, i, off, flags);
KEY_SA_UNREF(&sav);
return error;
unrefsav:
KEY_SA_UNREF(&sav);
bad:
m_freem(m);
return error;
}
#endif
void
ipsec_output_init(void)
{
ipsec_rtcache_percpu = rtcache_percpu_alloc();
}