2018-04-28 18:45:16 +03:00
|
|
|
/* $NetBSD: ipsec_output.c,v 1.74 2018/04/28 15:45:16 maxv Exp $ */
|
2004-03-02 02:30:01 +03:00
|
|
|
|
2018-02-21 20:04:52 +03:00
|
|
|
/*
|
2004-03-02 02:30:01 +03:00
|
|
|
* Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
|
|
|
*
|
2018-04-19 11:27:38 +03:00
|
|
|
* $FreeBSD: sys/netipsec/ipsec_output.c,v 1.3.2.2 2003/03/28 20:32:53 sam Exp $
|
2004-03-02 02:30:01 +03:00
|
|
|
*/
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2018-04-28 18:45:16 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.74 2018/04/28 15:45:16 maxv Exp $");
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2017-04-06 12:20:07 +03:00
|
|
|
#if defined(_KERNEL_OPT)
|
2003-08-14 00:06:49 +04:00
|
|
|
#include "opt_inet.h"
|
2017-07-27 09:59:28 +03:00
|
|
|
#include "opt_net_mpsafe.h"
|
2017-04-06 12:20:07 +03:00
|
|
|
#endif
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
#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
|
2007-06-28 00:38:32 +04:00
|
|
|
#include <netinet/udp.h>
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
#include <netipsec/ipsec.h>
|
2004-05-07 04:55:14 +04:00
|
|
|
#include <netipsec/ipsec_var.h>
|
2008-04-23 10:09:04 +04:00
|
|
|
#include <netipsec/ipsec_private.h>
|
2003-08-14 00:06:49 +04:00
|
|
|
#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>
|
|
|
|
|
2003-10-07 02:05:15 +04:00
|
|
|
#include <netipsec/key.h>
|
|
|
|
#include <netipsec/keydb.h>
|
|
|
|
#include <netipsec/key_debug.h>
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2017-08-10 09:11:24 +03:00
|
|
|
static percpu_t *ipsec_rtcache_percpu __cacheline_aligned;
|
2007-12-29 17:53:24 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a IPSEC_OUT_DONE tag to mark that we have finished the ipsec processing
|
2018-02-21 20:04:52 +03:00
|
|
|
* It will be used by ip{,6}_output to check if we have already or not
|
2007-12-29 17:53:24 +03:00
|
|
|
* processed this packet.
|
|
|
|
*/
|
|
|
|
static int
|
2018-04-18 09:43:10 +03:00
|
|
|
ipsec_register_done(struct mbuf *m, int *error)
|
2007-12-29 17:53:24 +03:00
|
|
|
{
|
|
|
|
struct m_tag *mtag;
|
|
|
|
|
|
|
|
mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 0, M_NOWAIT);
|
|
|
|
if (mtag == NULL) {
|
2017-05-19 07:34:09 +03:00
|
|
|
IPSECLOG(LOG_DEBUG, "could not get packet tag\n");
|
2007-12-29 17:53:24 +03:00
|
|
|
*error = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_tag_prepend(m, mtag);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-29 19:43:17 +03:00
|
|
|
static int
|
|
|
|
ipsec_reinject_ipstack(struct mbuf *m, int af)
|
|
|
|
{
|
2017-08-10 09:08:59 +03:00
|
|
|
int rv = -1;
|
2017-08-10 09:11:24 +03:00
|
|
|
struct route *ro;
|
2017-08-10 09:08:59 +03:00
|
|
|
|
|
|
|
KASSERT(af == AF_INET || af == AF_INET6);
|
2007-12-29 19:43:17 +03:00
|
|
|
|
2017-11-17 10:37:12 +03:00
|
|
|
KERNEL_LOCK_UNLESS_NET_MPSAFE();
|
2017-08-10 09:11:24 +03:00
|
|
|
ro = percpu_getref(ipsec_rtcache_percpu);
|
2007-12-29 19:43:17 +03:00
|
|
|
switch (af) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
2017-08-10 09:11:24 +03:00
|
|
|
rv = ip_output(m, NULL, ro, IP_RAWOUTPUT|IP_NOIPNEWID,
|
2011-08-31 22:31:02 +04:00
|
|
|
NULL, NULL);
|
2017-08-10 09:08:59 +03:00
|
|
|
break;
|
2017-07-27 09:59:28 +03:00
|
|
|
#endif
|
2007-12-29 19:43:17 +03:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
/*
|
|
|
|
* We don't need massage, IPv6 header fields are always in
|
|
|
|
* net endian.
|
|
|
|
*/
|
2017-08-10 09:11:24 +03:00
|
|
|
rv = ip6_output(m, NULL, ro, 0, NULL, NULL, NULL);
|
2017-08-10 09:08:59 +03:00
|
|
|
break;
|
2017-07-27 09:59:28 +03:00
|
|
|
#endif
|
2007-12-29 19:43:17 +03:00
|
|
|
}
|
2017-08-10 09:11:24 +03:00
|
|
|
percpu_putref(ipsec_rtcache_percpu);
|
2017-11-17 10:37:12 +03:00
|
|
|
KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
|
2007-12-29 19:43:17 +03:00
|
|
|
|
2017-08-10 09:08:59 +03:00
|
|
|
return rv;
|
2007-12-29 19:43:17 +03:00
|
|
|
}
|
|
|
|
|
2003-08-14 00:06:49 +04:00
|
|
|
int
|
2017-10-03 11:56:52 +03:00
|
|
|
ipsec_process_done(struct mbuf *m, const struct ipsecrequest *isr,
|
2017-07-14 15:26:26 +03:00
|
|
|
struct secasvar *sav)
|
2003-08-14 00:06:49 +04:00
|
|
|
{
|
|
|
|
struct secasindex *saidx;
|
|
|
|
int error;
|
2007-06-28 00:38:32 +04:00
|
|
|
#ifdef INET
|
2018-04-18 09:43:10 +03:00
|
|
|
struct ip *ip;
|
2018-02-21 20:04:52 +03:00
|
|
|
#endif
|
2007-06-28 00:38:32 +04:00
|
|
|
#ifdef INET6
|
2018-04-18 09:43:10 +03:00
|
|
|
struct ip6_hdr *ip6;
|
2018-02-21 20:04:52 +03:00
|
|
|
#endif
|
2018-04-18 09:43:10 +03:00
|
|
|
struct mbuf *mo;
|
2007-06-28 00:38:32 +04:00
|
|
|
struct udphdr *udp = NULL;
|
2018-04-18 09:43:10 +03:00
|
|
|
uint64_t *data = NULL;
|
2007-06-28 00:38:32 +04:00
|
|
|
int hlen, roff;
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2017-04-18 08:26:41 +03:00
|
|
|
KASSERT(m != NULL);
|
|
|
|
KASSERT(isr != NULL);
|
|
|
|
KASSERT(sav != NULL);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
saidx = &sav->sah->saidx;
|
2007-06-28 00:38:32 +04:00
|
|
|
|
2018-02-21 20:04:52 +03:00
|
|
|
if (sav->natt_type != 0) {
|
2007-06-28 00:38:32 +04:00
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
|
|
|
|
hlen = sizeof(struct udphdr);
|
2018-02-21 20:04:52 +03:00
|
|
|
if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
|
2007-06-28 00:38:32 +04:00
|
|
|
hlen += sizeof(uint64_t);
|
|
|
|
|
|
|
|
mo = m_makespace(m, sizeof(struct ip), hlen, &roff);
|
|
|
|
if (mo == NULL) {
|
2017-05-11 08:55:14 +03:00
|
|
|
char buf[IPSEC_ADDRSTRLEN];
|
2017-05-19 07:34:09 +03:00
|
|
|
IPSECLOG(LOG_DEBUG,
|
|
|
|
"failed to inject %u byte UDP for SA %s/%08lx\n",
|
2017-05-11 08:55:14 +03:00
|
|
|
hlen, ipsec_address(&saidx->dst, buf, sizeof(buf)),
|
2018-04-18 09:43:10 +03:00
|
|
|
(u_long)ntohl(sav->spi));
|
2007-06-28 00:38:32 +04:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto bad;
|
|
|
|
}
|
2018-02-21 20:04:52 +03:00
|
|
|
|
|
|
|
udp = (struct udphdr *)(mtod(mo, char *) + roff);
|
|
|
|
data = (uint64_t *)(udp + 1);
|
2007-06-28 00:38:32 +04:00
|
|
|
|
|
|
|
if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
|
|
|
|
*data = 0; /* NON-IKE Marker */
|
|
|
|
|
|
|
|
if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
|
|
|
|
udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
|
|
|
|
else
|
|
|
|
udp->uh_sport = key_portfromsaddr(&saidx->src);
|
2018-02-21 20:04:52 +03:00
|
|
|
|
2007-06-28 00:38:32 +04:00
|
|
|
udp->uh_dport = key_portfromsaddr(&saidx->dst);
|
|
|
|
udp->uh_sum = 0;
|
2009-12-01 04:01:34 +03:00
|
|
|
udp->uh_ulen = htons(m->m_pkthdr.len - (ip->ip_hl << 2));
|
2007-06-28 00:38:32 +04:00
|
|
|
}
|
2018-02-21 20:04:52 +03:00
|
|
|
|
2018-04-18 09:43:10 +03:00
|
|
|
/*
|
|
|
|
* Fix the header length, for AH processing.
|
|
|
|
*/
|
2003-08-14 00:06:49 +04:00
|
|
|
switch (saidx->dst.sa.sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
2007-06-28 00:38:32 +04:00
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
ip->ip_len = htons(m->m_pkthdr.len);
|
|
|
|
if (sav->natt_type != 0)
|
|
|
|
ip->ip_p = IPPROTO_UDP;
|
2003-08-14 00:06:49 +04:00
|
|
|
break;
|
2018-02-21 20:04:52 +03:00
|
|
|
#endif
|
2003-08-14 00:06:49 +04:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2018-02-21 20:04:52 +03:00
|
|
|
if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
|
2003-08-14 00:06:49 +04:00
|
|
|
error = ENXIO;
|
|
|
|
goto bad;
|
|
|
|
}
|
2018-02-21 20:04:52 +03:00
|
|
|
if (m->m_pkthdr.len - sizeof(struct ip6_hdr) > IPV6_MAXPACKET) {
|
2003-08-14 00:06:49 +04:00
|
|
|
/* No jumbogram support. */
|
|
|
|
error = ENXIO; /*?*/
|
|
|
|
goto bad;
|
|
|
|
}
|
2007-06-28 00:38:32 +04:00
|
|
|
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;
|
2003-08-14 00:06:49 +04:00
|
|
|
break;
|
2018-02-21 20:04:52 +03:00
|
|
|
#endif
|
2003-08-14 00:06:49 +04:00
|
|
|
default:
|
2017-05-19 07:34:09 +03:00
|
|
|
IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
|
|
|
|
saidx->dst.sa.sa_family);
|
2003-08-14 00:06:49 +04:00
|
|
|
error = ENXIO;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2011-02-18 19:12:26 +03:00
|
|
|
key_sa_recordxfer(sav, m);
|
|
|
|
|
2003-08-14 00:06:49 +04:00
|
|
|
/*
|
|
|
|
* If there's another (bundled) SA to apply, do so.
|
|
|
|
* Note that this puts a burden on the kernel stack size.
|
|
|
|
* If this is a problem we'll need to introduce a queue
|
|
|
|
* to set the packet on so we can unwind the stack before
|
|
|
|
* doing further processing.
|
|
|
|
*/
|
|
|
|
if (isr->next) {
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC_STATINC(IPSEC_STAT_OUT_BUNDLESA);
|
2018-02-21 20:04:52 +03:00
|
|
|
switch (saidx->dst.sa.sa_family) {
|
2007-02-10 12:43:05 +03:00
|
|
|
#ifdef INET
|
2009-12-01 04:01:34 +03:00
|
|
|
case AF_INET:
|
2017-07-21 06:08:10 +03:00
|
|
|
return ipsec4_process_packet(m, isr->next, NULL);
|
2018-02-21 20:04:52 +03:00
|
|
|
#endif
|
2007-02-10 12:43:05 +03:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2018-02-21 20:04:52 +03:00
|
|
|
return ipsec6_process_packet(m, isr->next);
|
|
|
|
#endif
|
|
|
|
default:
|
2017-05-19 07:34:09 +03:00
|
|
|
IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
|
|
|
|
saidx->dst.sa.sa_family);
|
2007-02-10 12:43:05 +03:00
|
|
|
error = ENXIO;
|
|
|
|
goto bad;
|
2009-12-01 04:01:34 +03:00
|
|
|
}
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-04-18 09:43:10 +03:00
|
|
|
* We're done with IPsec processing, mark the packet as processed,
|
|
|
|
* and transmit it using the appropriate network protocol
|
|
|
|
* (IPv4/IPv6).
|
2003-08-14 00:06:49 +04:00
|
|
|
*/
|
2007-12-29 17:53:24 +03:00
|
|
|
|
|
|
|
if (ipsec_register_done(m, &error) < 0)
|
|
|
|
goto bad;
|
|
|
|
|
2007-12-29 19:43:17 +03:00
|
|
|
return ipsec_reinject_ipstack(m, saidx->dst.sa.sa_family);
|
2018-02-21 20:04:52 +03:00
|
|
|
|
2003-08-14 00:06:49 +04:00
|
|
|
bad:
|
|
|
|
m_freem(m);
|
2018-02-21 20:04:52 +03:00
|
|
|
return error;
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
|
2017-10-03 10:32:53 +03:00
|
|
|
static void
|
|
|
|
ipsec_fill_saidx_bymbuf(struct secasindex *saidx, const struct mbuf *m,
|
|
|
|
const int af)
|
|
|
|
{
|
|
|
|
|
|
|
|
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 = 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 {
|
|
|
|
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 = IPSEC_PORT_ANY;
|
|
|
|
sin6->sin6_addr = ip6->ip6_src;
|
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
|
|
|
|
/* fix scope id for comparing SPD */
|
|
|
|
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)) {
|
|
|
|
/* fix scope id for comparing SPD */
|
|
|
|
sin6->sin6_addr.s6_addr16[1] = 0;
|
|
|
|
sin6->sin6_scope_id =
|
|
|
|
ntohs(ip6->ip6_dst.s6_addr16[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 11:25:21 +03:00
|
|
|
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) {
|
|
|
|
/* Fillin unspecified SA peers only for transport mode */
|
|
|
|
ipsec_fill_saidx_bymbuf(&saidx, m, isr->saidx.dst.sa.sa_family);
|
|
|
|
}
|
|
|
|
|
|
|
|
return key_lookup_sa_bysaidx(&saidx);
|
|
|
|
}
|
|
|
|
|
2007-12-29 19:43:17 +03:00
|
|
|
/*
|
|
|
|
* ipsec_nextisr can return :
|
|
|
|
* - isr == NULL and error != 0 => something is bad : the packet must be
|
|
|
|
* discarded
|
2018-02-21 20:04:52 +03:00
|
|
|
* - isr == NULL and error == 0 => no more rules to apply, ipsec processing
|
2007-12-29 19:43:17 +03:00
|
|
|
* is done, reinject it in ip stack
|
|
|
|
* - isr != NULL (error == 0) => we need to apply one rule to the packet
|
|
|
|
*/
|
2017-10-03 11:56:52 +03:00
|
|
|
static const struct ipsecrequest *
|
2018-02-21 20:04:52 +03:00
|
|
|
ipsec_nextisr(struct mbuf *m, const struct ipsecrequest *isr, int af,
|
|
|
|
int *error, struct secasvar **ret)
|
2003-08-14 00:06:49 +04:00
|
|
|
{
|
2017-07-04 09:45:05 +03:00
|
|
|
#define IPSEC_OSTAT(type) \
|
2008-04-23 10:09:04 +04:00
|
|
|
do { \
|
|
|
|
switch (isr->saidx.proto) { \
|
|
|
|
case IPPROTO_ESP: \
|
2017-07-04 09:45:05 +03:00
|
|
|
ESP_STATINC(ESP_STAT_ ## type); \
|
2008-04-23 10:09:04 +04:00
|
|
|
break; \
|
|
|
|
case IPPROTO_AH: \
|
2017-07-04 09:45:05 +03:00
|
|
|
AH_STATINC(AH_STAT_ ## type); \
|
2008-04-23 10:09:04 +04:00
|
|
|
break; \
|
|
|
|
default: \
|
2017-07-04 09:45:05 +03:00
|
|
|
IPCOMP_STATINC(IPCOMP_STAT_ ## type); \
|
2008-04-23 10:09:04 +04:00
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
|
2017-07-14 15:26:26 +03:00
|
|
|
struct secasvar *sav = NULL;
|
2017-10-03 11:34:28 +03:00
|
|
|
struct secasindex saidx;
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2017-04-18 08:26:41 +03:00
|
|
|
KASSERTMSG(af == AF_INET || af == AF_INET6,
|
|
|
|
"invalid address family %u", af);
|
2003-08-14 00:06:49 +04:00
|
|
|
again:
|
|
|
|
/*
|
|
|
|
* Craft SA index to search for proper SA. Note that
|
|
|
|
* we only fillin unspecified SA peers for transport
|
|
|
|
* mode; for tunnel mode they must already be filled in.
|
|
|
|
*/
|
2017-10-03 11:34:28 +03:00
|
|
|
saidx = isr->saidx;
|
2003-08-14 00:06:49 +04:00
|
|
|
if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
|
|
|
|
/* Fillin unspecified SA peers only for transport mode */
|
2017-10-03 11:34:28 +03:00
|
|
|
ipsec_fill_saidx_bymbuf(&saidx, m, af);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup SA and validate it.
|
|
|
|
*/
|
2017-10-03 11:34:28 +03:00
|
|
|
*error = key_checkrequest(isr, &saidx, &sav);
|
2003-08-14 00:06:49 +04:00
|
|
|
if (*error != 0) {
|
|
|
|
/*
|
|
|
|
* IPsec processing is required, but no SA found.
|
|
|
|
* I assume that key_acquire() had been called
|
|
|
|
* to get/establish the SA. Here I discard
|
|
|
|
* this packet because it is responsibility for
|
|
|
|
* upper layer to retransmit the packet.
|
|
|
|
*/
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC_STATINC(IPSEC_STAT_OUT_NOSA);
|
2003-08-14 00:06:49 +04:00
|
|
|
goto bad;
|
|
|
|
}
|
2007-12-29 19:43:17 +03:00
|
|
|
/* sav may be NULL here if we have an USE rule */
|
2018-02-21 20:04:52 +03:00
|
|
|
if (sav == NULL) {
|
2017-04-18 08:26:41 +03:00
|
|
|
KASSERTMSG(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
|
|
|
|
"no SA found, but required; level %u",
|
|
|
|
ipsec_get_reqlevel(isr));
|
2003-08-14 00:06:49 +04:00
|
|
|
isr = isr->next;
|
2018-02-21 20:04:52 +03:00
|
|
|
/*
|
2018-04-18 09:43:10 +03:00
|
|
|
* No more rules to apply, return NULL isr and no error.
|
|
|
|
* It can happen when the last rules are USE rules.
|
2018-02-21 20:04:52 +03:00
|
|
|
*/
|
2003-08-14 00:06:49 +04:00
|
|
|
if (isr == NULL) {
|
2017-07-14 15:26:26 +03:00
|
|
|
*ret = NULL;
|
2018-02-21 20:04:52 +03:00
|
|
|
*error = 0;
|
2003-08-14 00:06:49 +04:00
|
|
|
return isr;
|
|
|
|
}
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check system global policy controls.
|
|
|
|
*/
|
|
|
|
if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
|
|
|
|
(isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
|
|
|
|
(isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
|
2017-05-19 07:34:09 +03:00
|
|
|
IPSECLOG(LOG_DEBUG, "IPsec outbound packet dropped due"
|
|
|
|
" to policy (check your sysctls)\n");
|
2017-07-04 09:45:05 +03:00
|
|
|
IPSEC_OSTAT(PDROPS);
|
2003-08-14 00:06:49 +04:00
|
|
|
*error = EHOSTUNREACH;
|
2017-08-03 09:32:51 +03:00
|
|
|
KEY_SA_UNREF(&sav);
|
2003-08-14 00:06:49 +04:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanity check the SA contents for the caller
|
|
|
|
* before they invoke the xform output method.
|
|
|
|
*/
|
2017-07-06 12:49:46 +03:00
|
|
|
KASSERT(sav->tdb_xform != NULL);
|
2017-07-14 15:26:26 +03:00
|
|
|
*ret = sav;
|
2003-08-14 00:06:49 +04:00
|
|
|
return isr;
|
2018-02-21 20:04:52 +03:00
|
|
|
|
2003-08-14 00:06:49 +04:00
|
|
|
bad:
|
2017-04-18 08:26:41 +03:00
|
|
|
KASSERTMSG(*error != 0, "error return w/ no error code");
|
2003-08-14 00:06:49 +04:00
|
|
|
return NULL;
|
|
|
|
#undef IPSEC_OSTAT
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef INET
|
|
|
|
/*
|
|
|
|
* IPsec output logic for IPv4.
|
|
|
|
*/
|
|
|
|
int
|
2017-10-03 11:56:52 +03:00
|
|
|
ipsec4_process_packet(struct mbuf *m, const struct ipsecrequest *isr,
|
2017-07-21 06:08:10 +03:00
|
|
|
u_long *mtu)
|
2003-08-14 00:06:49 +04:00
|
|
|
{
|
2017-07-14 15:26:26 +03:00
|
|
|
struct secasvar *sav = NULL;
|
2003-08-14 00:06:49 +04:00
|
|
|
struct ip *ip;
|
|
|
|
int s, error, i, off;
|
2017-05-08 09:39:23 +03:00
|
|
|
union sockaddr_union *dst;
|
|
|
|
int setdf;
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2017-04-18 08:26:41 +03:00
|
|
|
KASSERT(m != NULL);
|
2018-03-03 12:39:29 +03:00
|
|
|
KASSERT(m->m_nextpkt == NULL);
|
2017-04-18 08:26:41 +03:00
|
|
|
KASSERT(isr != NULL);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2018-02-21 20:04:52 +03:00
|
|
|
s = splsoftnet(); /* insure SA contents don't change */
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2017-07-14 15:26:26 +03:00
|
|
|
isr = ipsec_nextisr(m, isr, AF_INET, &error, &sav);
|
2007-12-29 19:43:17 +03:00
|
|
|
if (isr == NULL) {
|
|
|
|
if (error != 0) {
|
|
|
|
goto bad;
|
|
|
|
} else {
|
|
|
|
if (ipsec_register_done(m, &error) < 0)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
return ipsec_reinject_ipstack(m, AF_INET);
|
|
|
|
}
|
|
|
|
}
|
2017-07-14 15:26:26 +03:00
|
|
|
KASSERT(sav != NULL);
|
2018-02-21 20:04:52 +03:00
|
|
|
|
2018-03-05 14:50:25 +03:00
|
|
|
if (m->m_len < sizeof(struct ip) &&
|
|
|
|
(m = m_pullup(m, sizeof(struct ip))) == NULL) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto unrefsav;
|
|
|
|
}
|
|
|
|
|
2017-07-21 06:08:10 +03:00
|
|
|
/*
|
|
|
|
* Check if we need to handle NAT-T fragmentation.
|
|
|
|
*/
|
|
|
|
if (isr == isr->sp->req) { /* Check only if called from ipsec4_output */
|
|
|
|
KASSERT(mtu != NULL);
|
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
if (!(sav->natt_type &
|
|
|
|
(UDP_ENCAP_ESPINUDP|UDP_ENCAP_ESPINUDP_NON_IKE))) {
|
|
|
|
goto noneed;
|
|
|
|
}
|
|
|
|
if (ntohs(ip->ip_len) <= sav->esp_frag)
|
|
|
|
goto noneed;
|
|
|
|
*mtu = sav->esp_frag;
|
2017-08-03 09:32:51 +03:00
|
|
|
KEY_SA_UNREF(&sav);
|
2017-07-21 06:08:10 +03:00
|
|
|
splx(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
noneed:
|
2017-05-08 09:39:23 +03:00
|
|
|
dst = &sav->sah->saidx.dst;
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2017-05-08 09:39:23 +03:00
|
|
|
/*
|
|
|
|
* Collect IP_DF state from the outer header.
|
|
|
|
*/
|
|
|
|
if (dst->sa.sa_family == AF_INET) {
|
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
/* Honor system-wide control of how to handle IP_DF */
|
|
|
|
switch (ip4_ipsec_dfbit) {
|
|
|
|
case 0: /* clear in outer header */
|
|
|
|
case 1: /* set in outer header */
|
|
|
|
setdf = ip4_ipsec_dfbit;
|
|
|
|
break;
|
|
|
|
default: /* propagate to outer header */
|
|
|
|
setdf = ip->ip_off;
|
|
|
|
setdf = ntohs(setdf);
|
|
|
|
setdf = htons(setdf & IP_DF);
|
|
|
|
break;
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
2017-05-08 09:39:23 +03:00
|
|
|
} else {
|
|
|
|
ip = NULL; /* keep compiler happy */
|
|
|
|
setdf = 0;
|
|
|
|
}
|
2018-02-21 20:04:52 +03:00
|
|
|
|
2017-05-08 09:39:23 +03:00
|
|
|
/* Do the appropriate encapsulation, if necessary */
|
|
|
|
if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
|
|
|
|
dst->sa.sa_family != AF_INET || /* PF mismatch */
|
2003-08-14 00:06:49 +04:00
|
|
|
#if 0
|
2017-05-08 09:39:23 +03:00
|
|
|
(sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
|
|
|
|
sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
|
2003-08-14 00:06:49 +04:00
|
|
|
#endif
|
2017-05-08 09:39:23 +03:00
|
|
|
(dst->sa.sa_family == AF_INET && /* Proxy */
|
|
|
|
dst->sin.sin_addr.s_addr != INADDR_ANY &&
|
|
|
|
dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
|
|
|
|
struct mbuf *mp;
|
|
|
|
|
|
|
|
/* Fix IPv4 header checksum and length */
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* Encapsulate the packet */
|
2017-07-14 15:26:26 +03:00
|
|
|
error = ipip_output(m, isr, sav, &mp, 0, 0);
|
2017-05-08 09:39:23 +03:00
|
|
|
if (mp == NULL && !error) {
|
|
|
|
/* Should never happen. */
|
2017-05-19 07:34:09 +03:00
|
|
|
IPSECLOG(LOG_DEBUG,
|
|
|
|
"ipip_output returns no mbuf and no error!");
|
2017-05-08 09:39:23 +03:00
|
|
|
error = EFAULT;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
if (mp) {
|
|
|
|
/* XXX: Should never happen! */
|
|
|
|
m_freem(mp);
|
|
|
|
}
|
|
|
|
m = NULL; /* ipip_output() already freed it */
|
2017-07-14 15:26:26 +03:00
|
|
|
goto unrefsav;
|
2017-05-08 09:39:23 +03:00
|
|
|
}
|
|
|
|
m = mp, mp = NULL;
|
2018-02-21 20:04:52 +03:00
|
|
|
|
2017-05-08 09:39:23 +03:00
|
|
|
/*
|
|
|
|
* ipip_output clears IP_DF in the new header. If
|
|
|
|
* we need to propagate IP_DF from the outer header,
|
|
|
|
* then we have to do it here.
|
|
|
|
*
|
|
|
|
* XXX shouldn't assume what ipip_output does.
|
|
|
|
*/
|
|
|
|
if (dst->sa.sa_family == AF_INET && setdf) {
|
2018-02-21 20:04:52 +03:00
|
|
|
if (m->m_len < sizeof(struct ip) &&
|
|
|
|
(m = m_pullup(m, sizeof(struct ip))) == NULL) {
|
2003-08-14 00:06:49 +04:00
|
|
|
error = ENOBUFS;
|
2017-07-14 15:26:26 +03:00
|
|
|
goto unrefsav;
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
ip = mtod(m, struct ip *);
|
2017-05-08 09:39:23 +03:00
|
|
|
ip->ip_off |= htons(IP_DF);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dispatch to the appropriate IPsec transform logic. The
|
|
|
|
* packet will be returned for transmission after crypto
|
|
|
|
* processing, etc. are completed. For encapsulation we
|
|
|
|
* bypass this call because of the explicit call done above
|
|
|
|
* (necessary to deal with IP_DF handling for IPv4).
|
|
|
|
*
|
|
|
|
* NB: m & sav are ``passed to caller'' who's reponsible for
|
|
|
|
* for reclaiming their resources.
|
|
|
|
*/
|
|
|
|
if (sav->tdb_xform->xf_type != XF_IP4) {
|
2011-06-06 20:48:35 +04:00
|
|
|
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);
|
|
|
|
}
|
2017-07-14 15:26:26 +03:00
|
|
|
error = (*sav->tdb_xform->xf_output)(m, isr, sav, NULL, i, off);
|
2003-08-14 00:06:49 +04:00
|
|
|
} else {
|
2017-07-14 15:26:26 +03:00
|
|
|
error = ipsec_process_done(m, isr, sav);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
2017-08-03 09:32:51 +03:00
|
|
|
KEY_SA_UNREF(&sav);
|
2003-08-14 00:06:49 +04:00
|
|
|
splx(s);
|
|
|
|
return error;
|
2018-02-21 20:04:52 +03:00
|
|
|
|
2017-07-14 15:26:26 +03:00
|
|
|
unrefsav:
|
2017-08-03 09:32:51 +03:00
|
|
|
KEY_SA_UNREF(&sav);
|
2003-08-14 00:06:49 +04:00
|
|
|
bad:
|
|
|
|
splx(s);
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef INET6
|
2012-01-11 00:01:56 +04:00
|
|
|
static void
|
|
|
|
compute_ipsec_pos(struct mbuf *m, int *i, int *off)
|
|
|
|
{
|
2018-02-21 20:04:52 +03:00
|
|
|
struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
|
2012-01-11 00:01:56 +04:00
|
|
|
struct ip6_ext ip6e;
|
|
|
|
int dstopt = 0;
|
2018-02-21 20:04:52 +03:00
|
|
|
int nxt;
|
2012-01-11 00:01:56 +04:00
|
|
|
|
|
|
|
*i = sizeof(struct ip6_hdr);
|
|
|
|
*off = offsetof(struct ip6_hdr, ip6_nxt);
|
|
|
|
nxt = ip6->ip6_nxt;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* chase mbuf chain to find the appropriate place to
|
|
|
|
* put AH/ESP/IPcomp header.
|
2018-02-21 20:04:52 +03:00
|
|
|
* IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
|
2012-01-11 00:01:56 +04:00
|
|
|
*/
|
|
|
|
do {
|
|
|
|
switch (nxt) {
|
|
|
|
case IPPROTO_AH:
|
|
|
|
case IPPROTO_ESP:
|
|
|
|
case IPPROTO_IPCOMP:
|
|
|
|
/*
|
|
|
|
* we should not skip security header added
|
|
|
|
* beforehand.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
|
|
|
|
case IPPROTO_HOPOPTS:
|
|
|
|
case IPPROTO_DSTOPTS:
|
|
|
|
case IPPROTO_ROUTING:
|
|
|
|
/*
|
|
|
|
* if we see 2nd destination option header,
|
|
|
|
* we should stop there.
|
|
|
|
*/
|
|
|
|
if (nxt == IPPROTO_DSTOPTS && dstopt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (nxt == IPPROTO_DSTOPTS) {
|
|
|
|
/*
|
|
|
|
* seen 1st or 2nd destination option.
|
|
|
|
* next time we see one, it must be 2nd.
|
|
|
|
*/
|
|
|
|
dstopt = 1;
|
|
|
|
} else if (nxt == IPPROTO_ROUTING) {
|
|
|
|
/*
|
2018-02-21 20:04:52 +03:00
|
|
|
* if we see destination option next
|
2012-01-11 00:01:56 +04:00
|
|
|
* time, it must be dest2.
|
|
|
|
*/
|
|
|
|
dstopt = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip this header */
|
|
|
|
m_copydata(m, *i, sizeof(ip6e), &ip6e);
|
|
|
|
nxt = ip6e.ip6e_nxt;
|
|
|
|
*off = *i + offsetof(struct ip6_ext, ip6e_nxt);
|
|
|
|
/*
|
|
|
|
* we will never see nxt == IPPROTO_AH
|
|
|
|
* so it is safe to omit AH case.
|
|
|
|
*/
|
|
|
|
*i += (ip6e.ip6e_len + 1) << 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2018-02-21 19:55:53 +03:00
|
|
|
} while (*i + sizeof(ip6e) < m->m_pkthdr.len);
|
2012-01-11 00:01:56 +04:00
|
|
|
}
|
|
|
|
|
2011-06-10 01:04:37 +04:00
|
|
|
static int
|
2018-02-21 20:04:52 +03:00
|
|
|
in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa,
|
|
|
|
const struct in6_addr *ia)
|
2011-06-10 01:04:37 +04:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2003-08-14 00:06:49 +04:00
|
|
|
int
|
2018-02-21 20:04:52 +03:00
|
|
|
ipsec6_process_packet(struct mbuf *m, const struct ipsecrequest *isr)
|
2003-08-14 00:06:49 +04:00
|
|
|
{
|
2017-07-14 15:26:26 +03:00
|
|
|
struct secasvar *sav = NULL;
|
2007-02-10 12:43:05 +03:00
|
|
|
struct ip6_hdr *ip6;
|
2011-06-07 19:54:57 +04:00
|
|
|
int s, error, i, off;
|
|
|
|
union sockaddr_union *dst;
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2017-04-18 08:26:41 +03:00
|
|
|
KASSERT(m != NULL);
|
2018-03-03 12:39:29 +03:00
|
|
|
KASSERT(m->m_nextpkt == NULL);
|
2017-04-18 08:26:41 +03:00
|
|
|
KASSERT(isr != NULL);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2007-02-10 12:43:05 +03:00
|
|
|
s = splsoftnet(); /* insure SA contents don't change */
|
2011-06-07 19:50:42 +04:00
|
|
|
|
2017-07-14 15:26:26 +03:00
|
|
|
isr = ipsec_nextisr(m, isr, AF_INET6, &error, &sav);
|
2003-08-14 00:06:49 +04:00
|
|
|
if (isr == NULL) {
|
2007-12-29 19:43:17 +03:00
|
|
|
if (error != 0) {
|
2011-06-07 19:50:42 +04:00
|
|
|
/* XXX Should we send a notification ? */
|
2007-12-29 19:43:17 +03:00
|
|
|
goto bad;
|
|
|
|
} else {
|
|
|
|
if (ipsec_register_done(m, &error) < 0)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
splx(s);
|
2008-04-28 21:40:11 +04:00
|
|
|
return ipsec_reinject_ipstack(m, AF_INET6);
|
2007-12-29 19:43:17 +03:00
|
|
|
}
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
|
2017-07-14 15:26:26 +03:00
|
|
|
KASSERT(sav != NULL);
|
2011-06-07 19:54:57 +04:00
|
|
|
dst = &sav->sah->saidx.dst;
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2018-02-26 09:34:39 +03:00
|
|
|
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 *);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2011-06-07 19:54:57 +04:00
|
|
|
/* Do the appropriate encapsulation, if necessary */
|
|
|
|
if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
|
2018-02-21 20:04:52 +03:00
|
|
|
dst->sa.sa_family != AF_INET6 || /* AF mismatch */
|
2011-06-07 19:54:57 +04:00
|
|
|
((dst->sa.sa_family == AF_INET6) &&
|
|
|
|
(!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
|
2018-02-21 20:04:52 +03:00
|
|
|
(!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) {
|
2011-06-07 19:54:57 +04:00
|
|
|
struct mbuf *mp;
|
2011-06-07 19:50:42 +04:00
|
|
|
|
2011-06-07 19:54:57 +04:00
|
|
|
if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
|
|
|
|
/* No jumbogram support. */
|
2017-07-13 04:48:52 +03:00
|
|
|
error = ENXIO; /*XXX*/
|
2017-07-14 15:26:26 +03:00
|
|
|
goto unrefsav;
|
2011-06-07 19:54:57 +04:00
|
|
|
}
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2018-02-26 09:34:39 +03:00
|
|
|
/* Fix IPv6 header payload length. */
|
2011-06-07 19:54:57 +04:00
|
|
|
ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
|
|
|
|
|
|
|
|
/* Encapsulate the packet */
|
2017-07-14 15:26:26 +03:00
|
|
|
error = ipip_output(m, isr, sav, &mp, 0, 0);
|
2011-06-07 19:54:57 +04:00
|
|
|
if (mp == NULL && !error) {
|
|
|
|
/* Should never happen. */
|
2017-05-19 07:34:09 +03:00
|
|
|
IPSECLOG(LOG_DEBUG,
|
|
|
|
"ipip_output returns no mbuf and no error!");
|
2011-06-07 19:54:57 +04:00
|
|
|
error = EFAULT;
|
|
|
|
}
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2011-06-07 19:54:57 +04:00
|
|
|
if (error) {
|
|
|
|
if (mp) {
|
|
|
|
/* XXX: Should never happen! */
|
|
|
|
m_freem(mp);
|
2006-12-16 00:18:52 +03:00
|
|
|
}
|
2011-06-07 19:54:57 +04:00
|
|
|
m = NULL; /* ipip_output() already freed it */
|
2017-07-14 15:26:26 +03:00
|
|
|
goto unrefsav;
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
2011-06-07 19:54:57 +04:00
|
|
|
|
|
|
|
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);
|
2018-02-21 20:04:52 +03:00
|
|
|
} else {
|
2012-01-11 00:01:56 +04:00
|
|
|
compute_ipsec_pos(m, &i, &off);
|
2011-06-07 19:50:42 +04:00
|
|
|
}
|
2017-07-14 15:26:26 +03:00
|
|
|
error = (*sav->tdb_xform->xf_output)(m, isr, sav, NULL, i, off);
|
2017-08-03 09:32:51 +03:00
|
|
|
KEY_SA_UNREF(&sav);
|
2011-06-07 19:50:42 +04:00
|
|
|
splx(s);
|
|
|
|
return error;
|
2018-02-21 20:04:52 +03:00
|
|
|
|
2017-07-14 15:26:26 +03:00
|
|
|
unrefsav:
|
2017-08-03 09:32:51 +03:00
|
|
|
KEY_SA_UNREF(&sav);
|
2003-08-14 00:06:49 +04:00
|
|
|
bad:
|
2007-02-10 12:43:05 +03:00
|
|
|
splx(s);
|
2003-08-14 00:06:49 +04:00
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return error;
|
|
|
|
}
|
2018-02-21 20:04:52 +03:00
|
|
|
#endif /* INET6 */
|
2017-08-10 09:11:24 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
ipsec_output_init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
ipsec_rtcache_percpu = percpu_alloc(sizeof(struct route));
|
|
|
|
}
|