#include "opt_inet.h"
#include "opt_inet6.h"
#include "opt_ipsec.h"
#include "opt_sctp.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/hhook.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <net/if_enc.h>
#include <net/if_var.h>
#include <net/vnet.h>
#include <netinet/in.h>
#include <netinet/in_pcb.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>
#ifdef INET6
#include <netinet6/ip6_ecn.h>
#endif
#include <netinet/ip_icmp.h>
#include <netinet/tcp_var.h>
#include <netinet/ip6.h>
#ifdef INET6
#include <netinet6/ip6_var.h>
#include <netinet6/scope6_var.h>
#endif
#include <netinet/in_pcb.h>
#ifdef INET6
#include <netinet/icmp6.h>
#endif
#if defined(SCTP) || defined(SCTP_SUPPORT)
#include <netinet/sctp_crc32.h>
#endif
#include <netinet/udp.h>
#include <netipsec/ah.h>
#include <netipsec/esp.h>
#include <netipsec/ipsec.h>
#ifdef INET6
#include <netipsec/ipsec6.h>
#endif
#include <netipsec/ipsec_support.h>
#include <netipsec/ipsec_offload.h>
#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>
#include <machine/in_cksum.h>
#define IPSEC_OSTAT_INC(proto, name) do { \
if ((proto) == IPPROTO_ESP) \
ESPSTAT_INC(esps_##name); \
else if ((proto) == IPPROTO_AH)\
AHSTAT_INC(ahs_##name); \
else \
IPCOMPSTAT_INC(ipcomps_##name); \
} while (0)
static int ipsec_encap(struct mbuf **mp, struct secasindex *saidx);
static size_t ipsec_get_pmtu(struct secasvar *sav);
#ifdef INET
static struct secasvar *
ipsec4_allocsa(struct ifnet *ifp, struct mbuf *m, const struct ip *ip,
struct secpolicy *sp, u_int *pidx, int *error)
{
struct secasindex *saidx, tmpsaidx;
struct ipsecrequest *isr;
struct sockaddr_in *sin;
struct secasvar *sav;
next:
isr = sp->req[*pidx];
if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
(isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
(isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
DPRINTF(("%s: IPsec outbound packet dropped due"
" to policy (check your sysctls)\n", __func__));
IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
*error = EHOSTUNREACH;
return (NULL);
}
if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
saidx = &tmpsaidx;
*saidx = isr->saidx;
if (saidx->src.sa.sa_len == 0) {
sin = &saidx->src.sin;
sin->sin_len = sizeof(*sin);
sin->sin_family = AF_INET;
sin->sin_port = IPSEC_PORT_ANY;
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 = IPSEC_PORT_ANY;
sin->sin_addr = ip->ip_dst;
}
} else
saidx = &sp->req[*pidx]->saidx;
sav = key_allocsa_policy(sp, saidx, error);
if (sav == NULL) {
IPSECSTAT_INC(ips_out_nosa);
if (*error != 0)
return (NULL);
if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
if (sp->tcount > ++(*pidx))
goto next;
*error = EJUSTRETURN;
}
return (NULL);
}
IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
return (sav);
}
static int
ipsec4_perform_request(struct ifnet *ifp, struct mbuf *m, struct ip *ip1,
struct secpolicy *sp, struct inpcb *inp, u_int idx, u_long mtu)
{
struct ipsec_ctx_data ctx;
union sockaddr_union *dst;
struct secasvar *sav;
struct ip *ip;
struct mbuf *m1;
int error, hwassist, i, off;
bool accel;
IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
sav = ipsec4_allocsa(ifp, m, ip1, sp, &idx, &error);
if (sav == NULL) {
if (error == EJUSTRETURN) {
(void)ipsec_accel_output(ifp, m, inp, sp, NULL,
AF_INET, mtu, &hwassist);
key_freesp(&sp);
return (error);
}
goto bad;
}
IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET, IPSEC_ENC_BEFORE);
if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
goto bad;
m_copydata(m, 0, sizeof(*ip1), (char *)ip1);
hwassist = 0;
accel = ipsec_accel_output(ifp, m, inp, sp, sav, AF_INET, mtu,
&hwassist);
if ((m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~hwassist) != 0) {
in_delayed_cksum(m);
m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
}
#if defined(SCTP) || defined(SCTP_SUPPORT)
if ((m->m_pkthdr.csum_flags & CSUM_SCTP & ~hwassist) != 0) {
sctp_delayed_cksum(m, (uint32_t)(ip1->ip_hl << 2));
m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
}
#endif
if (accel)
return (EJUSTRETURN);
error = mb_unmapped_to_ext(m, &m1);
if (error != 0) {
if (error == EINVAL) {
if (bootverbose)
if_printf(ifp, "Tx TLS+IPSEC packet\n");
}
return (error);
}
m = m1;
ip = mtod(m, struct ip *);
dst = &sav->sah->saidx.dst;
if (sp->req[idx]->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)) {
ip->ip_len = htons(m->m_pkthdr.len);
ip->ip_sum = 0;
ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
error = ipsec_encap(&m, &sav->sah->saidx);
if (error != 0) {
DPRINTF(("%s: encapsulation for SPI 0x%08x failed "
"with error %d\n", __func__, ntohl(sav->spi),
error));
goto bad;
}
inp = NULL;
}
IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
goto bad;
switch(dst->sa.sa_family) {
case AF_INET:
ip = mtod(m, struct ip *);
i = ip->ip_hl << 2;
off = offsetof(struct ip, ip_p);
break;
#ifdef INET6
case AF_INET6:
i = sizeof(struct ip6_hdr);
off = offsetof(struct ip6_hdr, ip6_nxt);
break;
#endif
default:
DPRINTF(("%s: unsupported protocol family %u\n",
__func__, dst->sa.sa_family));
error = EPFNOSUPPORT;
IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
goto bad;
}
error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
return (error);
bad:
IPSECSTAT_INC(ips_out_inval);
if (m != NULL)
m_freem(m);
if (sav != NULL)
key_freesav(&sav);
key_freesp(&sp);
return (error);
}
int
ipsec4_process_packet(struct ifnet *ifp, struct mbuf *m, struct ip *ip1,
struct secpolicy *sp, struct inpcb *inp, u_long mtu)
{
return (ipsec4_perform_request(ifp, m, ip1, sp, inp, 0, mtu));
}
int
ipsec4_check_pmtu(struct ifnet *ifp, struct mbuf *m, struct ip *ip1,
struct secpolicy *sp, int forwarding)
{
struct secasvar *sav;
size_t hlen, pmtu;
uint32_t idx;
int error;
if (!V_ip4_ipsec_dfbit)
return (0);
if (V_ip4_ipsec_dfbit == 1)
goto setdf;
if ((ip1->ip_off & htons(IP_DF)) == 0)
return (0);
setdf:
idx = sp->tcount - 1;
sav = ipsec4_allocsa(ifp, m, ip1, sp, &idx, &error);
if (sav == NULL) {
key_freesp(&sp);
if (error == 0)
error = EINPROGRESS;
if (error != EJUSTRETURN)
m_freem(m);
return (error);
}
pmtu = ipsec_get_pmtu(sav);
if (pmtu == 0) {
key_freesav(&sav);
return (0);
}
hlen = ipsec_hdrsiz_internal(sp);
key_freesav(&sav);
if (m_length(m, NULL) + hlen > pmtu) {
if (forwarding) {
if (pmtu > hlen)
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
0, pmtu - hlen);
else
m_freem(m);
key_freesp(&sp);
return (EINPROGRESS);
} else {
m_freem(m);
key_freesp(&sp);
return (EMSGSIZE);
}
}
return (0);
}
static int
ipsec4_common_output1(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp,
struct ip *ip1, int forwarding, u_long mtu)
{
struct secpolicy *sp;
int error;
sp = ipsec4_checkpolicy(m, inp, ip1, &error, !forwarding);
if (sp == NULL) {
if (error == -EINVAL) {
m_freem(m);
return (EACCES);
}
return (0);
}
error = ipsec4_check_pmtu(ifp, m, ip1, sp, forwarding);
if (error != 0) {
if (error == EJUSTRETURN)
return (0);
return (error);
}
error = ipsec4_process_packet(ifp, m, ip1, sp, inp, mtu);
if (error == EJUSTRETURN) {
return (0);
}
if (error == 0)
return (EINPROGRESS);
return (error);
}
static int
ipsec4_common_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp,
struct ip *ip1, int forwarding, u_long mtu)
{
struct ip ip_hdr;
struct ip *ip;
if (((m->m_flags & M_PKTHDR) != 0 && m->m_pkthdr.len < sizeof(*ip)) ||
((m->m_flags & M_PKTHDR) == 0 && m->m_len < sizeof(*ip))) {
m_free(m);
return (EACCES);
}
if (ip1 != NULL) {
ip = ip1;
} else {
ip = &ip_hdr;
m_copydata(m, 0, sizeof(*ip), (char *)ip);
}
return (ipsec4_common_output1(ifp, m, inp, ip, forwarding, mtu));
}
int
ipsec4_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp, u_long mtu)
{
if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
return (0);
return (ipsec4_common_output(ifp, m, inp, NULL, 0, mtu));
}
int
ipsec4_forward(struct mbuf *m)
{
struct ip ip_hdr;
m_copydata(m, 0, sizeof(ip_hdr), (char *)&ip_hdr);
if (ipsec4_in_reject1(m, &ip_hdr, NULL) != 0) {
m_freem(m);
return (EACCES);
}
return (ipsec4_common_output(NULL , m, NULL, &ip_hdr,
1, 0));
}
#endif
#ifdef INET6
static int
in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa,
const struct in6_addr *ia)
{
struct in6_addr ia2;
if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) {
memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
return (IN6_ARE_ADDR_EQUAL(ia, &ia2));
}
return (IN6_ARE_ADDR_EQUAL(&sa->sin6_addr, ia));
}
static struct secasvar *
ipsec6_allocsa(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
u_int *pidx, int *error)
{
struct secasindex *saidx, tmpsaidx;
struct ipsecrequest *isr;
struct sockaddr_in6 *sin6;
struct secasvar *sav;
struct ip6_hdr *ip6;
next:
isr = sp->req[*pidx];
if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
(isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
(isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
DPRINTF(("%s: IPsec outbound packet dropped due"
" to policy (check your sysctls)\n", __func__));
IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
*error = EHOSTUNREACH;
return (NULL);
}
if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
saidx = &tmpsaidx;
*saidx = isr->saidx;
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 = IPSEC_PORT_ANY;
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 = IPSEC_PORT_ANY;
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]);
}
}
} else
saidx = &sp->req[*pidx]->saidx;
sav = key_allocsa_policy(sp, saidx, error);
if (sav == NULL) {
IPSEC6STAT_INC(ips_out_nosa);
if (*error != 0)
return (NULL);
if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
if (sp->tcount > ++(*pidx))
goto next;
*error = EJUSTRETURN;
}
return (NULL);
}
IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
return (sav);
}
static int
ipsec6_perform_request(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
struct inpcb *inp, u_int idx, u_long mtu)
{
struct ipsec_ctx_data ctx;
union sockaddr_union *dst;
struct secasvar *sav;
struct ip6_hdr *ip6;
int error, hwassist, i, off;
bool accel;
IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
sav = ipsec6_allocsa(ifp, m, sp, &idx, &error);
if (sav == NULL) {
if (error == EJUSTRETURN) {
(void)ipsec_accel_output(ifp, m, inp, sp, NULL,
AF_INET6, mtu, &hwassist);
key_freesp(&sp);
return (error);
}
goto bad;
}
ip6 = mtod(m, struct ip6_hdr *);
ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET6, IPSEC_ENC_BEFORE);
if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
goto bad;
hwassist = 0;
accel = ipsec_accel_output(ifp, m, inp, sp, sav, AF_INET6, mtu,
&hwassist);
if ((m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6 & ~hwassist) != 0) {
in6_delayed_cksum(m, m->m_pkthdr.len -
sizeof(struct ip6_hdr), sizeof(struct ip6_hdr));
m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
}
#if defined(SCTP) || defined(SCTP_SUPPORT)
if ((m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6 & ~hwassist) != 0) {
sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
}
#endif
if (accel)
return (EJUSTRETURN);
ip6 = mtod(m, struct ip6_hdr *);
dst = &sav->sah->saidx.dst;
if (sp->req[idx]->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)))) {
if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
error = ENXIO;
goto bad;
}
error = ipsec_encap(&m, &sav->sah->saidx);
if (error != 0) {
DPRINTF(("%s: encapsulation for SPI 0x%08x failed "
"with error %d\n", __func__, ntohl(sav->spi),
error));
goto bad;
}
inp = NULL;
}
IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
goto bad;
switch(dst->sa.sa_family) {
#ifdef INET
case AF_INET:
{
struct ip *ip;
ip = mtod(m, struct ip *);
i = ip->ip_hl << 2;
off = offsetof(struct ip, ip_p);
}
break;
#endif
case AF_INET6:
i = sizeof(struct ip6_hdr);
off = offsetof(struct ip6_hdr, ip6_nxt);
break;
default:
DPRINTF(("%s: unsupported protocol family %u\n",
__func__, dst->sa.sa_family));
error = EPFNOSUPPORT;
IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
goto bad;
}
error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
return (error);
bad:
IPSEC6STAT_INC(ips_out_inval);
if (m != NULL)
m_freem(m);
if (sav != NULL)
key_freesav(&sav);
key_freesp(&sp);
return (error);
}
int
ipsec6_process_packet(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
struct inpcb *inp, u_long mtu)
{
return (ipsec6_perform_request(ifp, m, sp, inp, 0, mtu));
}
int
ipsec6_check_pmtu(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
int forwarding)
{
struct secasvar *sav;
size_t hlen, pmtu;
uint32_t idx;
int error;
if (!forwarding)
return (0);
idx = sp->tcount - 1;
sav = ipsec6_allocsa(ifp, m, sp, &idx, &error);
if (sav == NULL) {
key_freesp(&sp);
if (error == 0)
error = EINPROGRESS;
if (error != EJUSTRETURN)
m_freem(m);
return (error);
}
pmtu = ipsec_get_pmtu(sav);
if (pmtu == 0) {
key_freesav(&sav);
return (0);
}
hlen = ipsec_hdrsiz_internal(sp);
key_freesav(&sav);
if (m_length(m, NULL) + hlen > pmtu) {
if (forwarding) {
if (pmtu > hlen)
icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, pmtu - hlen);
else
m_freem(m);
key_freesp(&sp);
return (EINPROGRESS);
}
}
return (0);
}
static int
ipsec6_common_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp,
int forwarding, u_long mtu)
{
struct secpolicy *sp;
int error;
sp = ipsec6_checkpolicy(m, inp, &error, !forwarding);
if (sp == NULL) {
if (error == -EINVAL) {
m_freem(m);
return (EACCES);
}
return (0);
}
error = ipsec6_check_pmtu(ifp, m, sp, forwarding);
if (error != 0) {
if (error == EJUSTRETURN)
return (0);
return (error);
}
error = ipsec6_process_packet(ifp, m, sp, inp, mtu);
if (error == EJUSTRETURN) {
return (0);
}
if (error == 0)
return (EINPROGRESS);
return (error);
}
int
ipsec6_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp, u_long mtu)
{
if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
return (0);
return (ipsec6_common_output(ifp, m, inp, 0, mtu));
}
int
ipsec6_forward(struct mbuf *m)
{
if (ipsec6_in_reject(m, NULL) != 0) {
m_freem(m);
return (EACCES);
}
return (ipsec6_common_output(NULL , m, NULL, 1, 0));
}
#endif
int
ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
u_int idx)
{
struct epoch_tracker et;
struct xform_history *xh;
struct secasindex *saidx;
struct m_tag *mtag;
#ifdef INET
struct ip *ip;
#endif
int error;
if (sav->state >= SADB_SASTATE_DEAD) {
error = ESRCH;
goto bad;
}
saidx = &sav->sah->saidx;
switch (saidx->dst.sa.sa_family) {
#ifdef INET
case AF_INET:
ip = mtod(m, struct ip *);
ip->ip_len = htons(m->m_pkthdr.len);
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;
}
mtod(m, struct ip6_hdr *)->ip6_plen =
htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
break;
#endif
default:
DPRINTF(("%s: unknown protocol family %u\n", __func__,
saidx->dst.sa.sa_family));
error = ENXIO;
goto bad;
}
mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, sizeof(*xh), M_NOWAIT);
if (mtag == NULL) {
DPRINTF(("%s: could not get packet tag\n", __func__));
error = ENOMEM;
goto bad;
}
xh = (struct xform_history *)(mtag + 1);
xh->dst = saidx->dst;
xh->proto = saidx->proto;
xh->mode = saidx->mode;
xh->spi = sav->spi;
m_tag_prepend(m, mtag);
key_sa_recordxfer(sav, m);
if (++idx < sp->tcount) {
switch (saidx->dst.sa.sa_family) {
#ifdef INET
case AF_INET:
key_freesav(&sav);
IPSECSTAT_INC(ips_out_bundlesa);
return (ipsec4_perform_request(NULL, m, ip, sp, NULL,
idx, 0));
#endif
#ifdef INET6
case AF_INET6:
key_freesav(&sav);
IPSEC6STAT_INC(ips_out_bundlesa);
return (ipsec6_perform_request(NULL, m, sp, NULL,
idx, 0));
#endif
default:
DPRINTF(("%s: unknown protocol family %u\n", __func__,
saidx->dst.sa.sa_family));
error = EPFNOSUPPORT;
goto bad;
}
}
key_freesp(&sp), sp = NULL;
#if defined(INET) || defined(INET6)
if (sav->natt != NULL) {
error = udp_ipsec_output(m, sav);
if (error != 0)
goto bad;
}
#endif
NET_EPOCH_ENTER(et);
switch (saidx->dst.sa.sa_family) {
#ifdef INET
case AF_INET:
key_freesav(&sav);
error = ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
break;
#endif
#ifdef INET6
case AF_INET6:
key_freesav(&sav);
error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
break;
#endif
default:
panic("ipsec_process_done");
}
NET_EPOCH_EXIT(et);
return (error);
bad:
m_freem(m);
key_freesav(&sav);
if (sp != NULL)
key_freesp(&sp);
return (error);
}
#define IPSEC_TRAILINGSPACE (sizeof(struct udphdr) + \
max(sizeof(struct newesp) + EALG_MAX_BLOCK_LEN, \
sizeof(struct newah) + HASH_MAX_LEN ))
static struct mbuf *
ipsec_prepend(struct mbuf *m, int len, int how)
{
struct mbuf *n;
M_ASSERTPKTHDR(m);
IPSEC_ASSERT(len < MHLEN, ("wrong length"));
if (M_LEADINGSPACE(m) >= len) {
m->m_data -= len;
m->m_len += len;
m->m_pkthdr.len += len;
return (m);
}
n = m_gethdr(how, m->m_type);
if (n == NULL) {
m_freem(m);
return (NULL);
}
m_move_pkthdr(n, m);
n->m_next = m;
if (len + IPSEC_TRAILINGSPACE < M_SIZE(n))
m_align(n, len + IPSEC_TRAILINGSPACE);
n->m_len = len;
n->m_pkthdr.len += len;
return (n);
}
static size_t
ipsec_get_pmtu(struct secasvar *sav)
{
union sockaddr_union *dst;
struct in_conninfo inc;
size_t pmtu;
dst = &sav->sah->saidx.dst;
memset(&inc, 0, sizeof(inc));
switch (dst->sa.sa_family) {
#ifdef INET
case AF_INET:
inc.inc_faddr = satosin(&dst->sa)->sin_addr;
break;
#endif
#ifdef INET6
case AF_INET6:
inc.inc6_faddr = satosin6(&dst->sa)->sin6_addr;
inc.inc_flags |= INC_ISIPV6;
break;
#endif
default:
return (0);
}
pmtu = tcp_hc_getmtu(&inc);
if (pmtu != 0)
return (pmtu);
switch (dst->sa.sa_family) {
#ifdef INET
case AF_INET:
pmtu = tcp_maxmtu(&inc, NULL);
break;
#endif
#ifdef INET6
case AF_INET6:
pmtu = tcp_maxmtu6(&inc, NULL);
break;
#endif
default:
return (0);
}
if (pmtu == 0)
return (0);
tcp_hc_updatemtu(&inc, pmtu);
return (pmtu);
}
static int
ipsec_encap(struct mbuf **mp, struct secasindex *saidx)
{
#ifdef INET6
struct ip6_hdr *ip6;
#endif
struct ip *ip;
#ifdef INET
int setdf = V_ip4_ipsec_dfbit == 1 ? 1: 0;
#endif
uint8_t itos, proto;
ip = mtod(*mp, struct ip *);
switch (ip->ip_v) {
#ifdef INET
case IPVERSION:
proto = IPPROTO_IPIP;
if (V_ip4_ipsec_dfbit > 1)
setdf = (ip->ip_off & htons(IP_DF)) != 0;
itos = ip->ip_tos;
break;
#endif
#ifdef INET6
case (IPV6_VERSION >> 4):
proto = IPPROTO_IPV6;
ip6 = mtod(*mp, struct ip6_hdr *);
itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
in6_clearscope(&ip6->ip6_src);
in6_clearscope(&ip6->ip6_dst);
break;
#endif
default:
return (EAFNOSUPPORT);
}
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)
return (EINVAL);
*mp = ipsec_prepend(*mp, sizeof(struct ip), M_NOWAIT);
if (*mp == NULL)
return (ENOBUFS);
ip = mtod(*mp, struct ip *);
ip->ip_v = IPVERSION;
ip->ip_hl = sizeof(struct ip) >> 2;
ip->ip_p = proto;
ip->ip_len = htons((*mp)->m_pkthdr.len);
ip->ip_ttl = V_ip_defttl;
ip->ip_sum = 0;
ip->ip_off = setdf ? htons(IP_DF): 0;
ip->ip_src = saidx->src.sin.sin_addr;
ip->ip_dst = saidx->dst.sin.sin_addr;
ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
ip_fillid(ip, V_ip4_ipsec_random_id);
break;
#endif
#ifdef INET6
case AF_INET6:
if (saidx->src.sa.sa_family != AF_INET6 ||
IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
return (EINVAL);
*mp = ipsec_prepend(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
if (*mp == NULL)
return (ENOBUFS);
ip6 = mtod(*mp, struct ip6_hdr *);
ip6->ip6_flow = 0;
ip6->ip6_vfc = IPV6_VERSION;
ip6->ip6_hlim = V_ip6_defhlim;
ip6->ip6_nxt = proto;
ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
ip6->ip6_dst.s6_addr16[1] =
htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
ip6->ip6_src = saidx->src.sin6.sin6_addr;
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
ip6->ip6_src.s6_addr16[1] =
htons(saidx->src.sin6.sin6_scope_id & 0xffff);
ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
ip6->ip6_flow |= htonl((uint32_t)proto << 20);
break;
#endif
default:
return (EAFNOSUPPORT);
}
(*mp)->m_flags &= ~(M_BCAST | M_MCAST);
return (0);
}