2003-03-28 20:28:05 +00: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.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* IPsec output processing.
|
|
|
|
*/
|
|
|
|
#include "opt_inet.h"
|
|
|
|
#include "opt_inet6.h"
|
|
|
|
#include "opt_ipsec.h"
|
2006-06-26 22:30:08 +00:00
|
|
|
#include "opt_enc.h"
|
2002-10-16 02:10:08 +00: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>
|
2013-10-26 17:58:36 +00:00
|
|
|
#include <net/if_var.h>
|
2007-07-19 09:57:54 +00:00
|
|
|
#include <net/pfil.h>
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
#include <net/vnet.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
#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>
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet6/ip6_ecn.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet6/ip6_var.h>
|
2015-04-18 16:38:45 +00:00
|
|
|
#include <netinet6/scope6_var.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
#endif
|
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet/icmp6.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <netipsec/ipsec.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>
|
|
|
|
|
|
|
|
#include <machine/in_cksum.h>
|
|
|
|
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
#ifdef IPSEC_NAT_T
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#endif
|
|
|
|
|
2008-08-12 09:05:01 +00:00
|
|
|
#ifdef DEV_ENC
|
|
|
|
#include <net/if_enc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
int
|
|
|
|
ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
|
|
|
|
{
|
|
|
|
struct tdb_ident *tdbi;
|
|
|
|
struct m_tag *mtag;
|
|
|
|
struct secasvar *sav;
|
|
|
|
struct secasindex *saidx;
|
|
|
|
int error;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(isr != NULL, ("null ISR"));
|
2015-04-27 00:55:56 +00:00
|
|
|
IPSEC_ASSERT(isr->sp != NULL, ("NULL isr->sp"));
|
2002-10-16 02:10:08 +00:00
|
|
|
sav = isr->sav;
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(sav != NULL, ("null SA"));
|
|
|
|
IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
saidx = &sav->sah->saidx;
|
|
|
|
switch (saidx->dst.sa.sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
/* Fix the header length, for AH processing. */
|
|
|
|
mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
|
|
|
|
break;
|
|
|
|
#endif /* INET */
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
/* Fix the header length, for AH processing. */
|
|
|
|
if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
|
|
|
|
error = ENXIO;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
|
|
|
|
/* No jumbogram support. */
|
|
|
|
error = ENXIO; /*?*/
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
mtod(m, struct ip6_hdr *)->ip6_plen =
|
|
|
|
htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
|
|
|
|
break;
|
|
|
|
#endif /* INET6 */
|
|
|
|
default:
|
2003-09-29 22:57:43 +00:00
|
|
|
DPRINTF(("%s: unknown protocol family %u\n", __func__,
|
2002-10-16 02:10:08 +00:00
|
|
|
saidx->dst.sa.sa_family));
|
|
|
|
error = ENXIO;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a record of what we've done or what needs to be done to the
|
|
|
|
* packet.
|
|
|
|
*/
|
|
|
|
mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
|
|
|
|
sizeof(struct tdb_ident), M_NOWAIT);
|
|
|
|
if (mtag == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
DPRINTF(("%s: could not get packet tag\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
error = ENOMEM;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
tdbi = (struct tdb_ident *)(mtag + 1);
|
|
|
|
tdbi->dst = saidx->dst;
|
|
|
|
tdbi->proto = saidx->proto;
|
|
|
|
tdbi->spi = sav->spi;
|
|
|
|
m_tag_prepend(m, mtag);
|
|
|
|
|
2015-07-30 20:56:27 +00:00
|
|
|
key_sa_recordxfer(sav, m); /* record data transfer */
|
|
|
|
|
2002-10-16 02:10:08 +00: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.
|
2015-04-27 00:55:56 +00:00
|
|
|
*
|
|
|
|
* If ipsec[46]_process_packet() will successfully queue
|
|
|
|
* the request, we need to take additional reference to SP,
|
|
|
|
* because xform callback will release reference.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
|
|
|
if (isr->next) {
|
2012-07-22 17:46:05 +00:00
|
|
|
/* XXX-BZ currently only support same AF bundles. */
|
2011-04-27 19:28:42 +00:00
|
|
|
switch (saidx->dst.sa.sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
2014-11-13 12:58:33 +00:00
|
|
|
IPSECSTAT_INC(ips_out_bundlesa);
|
2015-04-27 00:55:56 +00:00
|
|
|
key_addref(isr->sp);
|
|
|
|
error = ipsec4_process_packet(m, isr->next);
|
|
|
|
if (error != 0)
|
|
|
|
KEY_FREESP(&isr->sp);
|
|
|
|
return (error);
|
2011-04-27 19:28:42 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
#endif
|
|
|
|
#ifdef notyet
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
/* XXX */
|
2014-11-13 12:58:33 +00:00
|
|
|
IPSEC6STAT_INC(ips_out_bundlesa);
|
2015-04-27 00:55:56 +00:00
|
|
|
key_addref(isr->sp);
|
|
|
|
error = ipsec6_process_packet(m, isr->next);
|
|
|
|
if (error != 0)
|
|
|
|
KEY_FREESP(&isr->sp);
|
|
|
|
return (error);
|
2011-04-27 19:28:42 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
#endif /* INET6 */
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
DPRINTF(("%s: unknown protocol family %u\n", __func__,
|
|
|
|
saidx->dst.sa.sa_family));
|
|
|
|
error = ENXIO;
|
|
|
|
goto bad;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're done with IPsec processing, transmit the packet using the
|
2015-04-28 11:03:47 +00:00
|
|
|
* appropriate network protocol (IP or IPv6).
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
|
|
|
switch (saidx->dst.sa.sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
#ifdef IPSEC_NAT_T
|
|
|
|
/*
|
|
|
|
* If NAT-T is enabled, now that all IPsec processing is done
|
|
|
|
* insert UDP encapsulation header after IP header.
|
|
|
|
*/
|
|
|
|
if (sav->natt_type) {
|
2012-10-22 22:42:28 +00:00
|
|
|
struct ip *ip = mtod(m, struct ip *);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
const int hlen = (ip->ip_hl << 2);
|
|
|
|
int size, off;
|
|
|
|
struct mbuf *mi;
|
|
|
|
struct udphdr *udp;
|
|
|
|
|
|
|
|
size = sizeof(struct udphdr);
|
|
|
|
if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
|
|
|
|
/*
|
|
|
|
* draft-ietf-ipsec-nat-t-ike-0[01].txt and
|
|
|
|
* draft-ietf-ipsec-udp-encaps-(00/)01.txt,
|
|
|
|
* ignoring possible AH mode
|
|
|
|
* non-IKE marker + non-ESP marker
|
|
|
|
* from draft-ietf-ipsec-udp-encaps-00.txt.
|
|
|
|
*/
|
|
|
|
size += sizeof(u_int64_t);
|
|
|
|
}
|
|
|
|
mi = m_makespace(m, hlen, size, &off);
|
|
|
|
if (mi == NULL) {
|
|
|
|
DPRINTF(("%s: m_makespace for udphdr failed\n",
|
|
|
|
__func__));
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
|
|
|
|
if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
|
|
|
|
udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
|
|
|
|
else
|
|
|
|
udp->uh_sport =
|
|
|
|
KEY_PORTFROMSADDR(&sav->sah->saidx.src);
|
|
|
|
udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
|
|
|
|
udp->uh_sum = 0;
|
|
|
|
udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
|
2012-10-22 22:42:28 +00:00
|
|
|
ip->ip_len = htons(m->m_pkthdr.len);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
ip->ip_p = IPPROTO_UDP;
|
|
|
|
|
|
|
|
if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
|
|
|
|
*(u_int64_t *)(udp + 1) = 0;
|
|
|
|
}
|
|
|
|
#endif /* IPSEC_NAT_T */
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
|
|
|
|
#endif /* INET */
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
/*
|
|
|
|
* We don't need massage, IPv6 header fields are always in
|
|
|
|
* net endian.
|
|
|
|
*/
|
|
|
|
return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
|
|
|
|
#endif /* INET6 */
|
|
|
|
}
|
|
|
|
panic("ipsec_process_done");
|
|
|
|
bad:
|
|
|
|
m_freem(m);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ipsecrequest *
|
|
|
|
ipsec_nextisr(
|
|
|
|
struct mbuf *m,
|
|
|
|
struct ipsecrequest *isr,
|
|
|
|
int af,
|
|
|
|
struct secasindex *saidx,
|
|
|
|
int *error
|
|
|
|
)
|
|
|
|
{
|
2013-06-20 11:44:16 +00:00
|
|
|
#define IPSEC_OSTAT(name) do { \
|
|
|
|
if (isr->saidx.proto == IPPROTO_ESP) \
|
|
|
|
ESPSTAT_INC(esps_##name); \
|
|
|
|
else if (isr->saidx.proto == IPPROTO_AH)\
|
|
|
|
AHSTAT_INC(ahs_##name); \
|
|
|
|
else \
|
|
|
|
IPCOMPSTAT_INC(ipcomps_##name); \
|
|
|
|
} while (0)
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secasvar *sav;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSECREQUEST_LOCK_ASSERT(isr);
|
|
|
|
|
|
|
|
IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
|
|
|
|
("invalid address family %u", af));
|
2002-10-16 02:10:08 +00: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.
|
|
|
|
*/
|
|
|
|
*saidx = isr->saidx;
|
|
|
|
if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
|
|
|
|
/* Fillin unspecified SA peers only for transport mode */
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup SA and validate it.
|
|
|
|
*/
|
|
|
|
*error = key_checkrequest(isr, saidx);
|
|
|
|
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.
|
|
|
|
*/
|
2014-11-12 14:00:49 +00:00
|
|
|
switch(af) {
|
|
|
|
case AF_INET:
|
|
|
|
IPSECSTAT_INC(ips_out_nosa);
|
|
|
|
break;
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
IPSEC6STAT_INC(ips_out_nosa);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
sav = isr->sav;
|
2008-03-14 16:38:11 +00:00
|
|
|
if (sav == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
|
|
|
|
("no SA found, but required; level %u",
|
2002-10-16 02:10:08 +00:00
|
|
|
ipsec_get_reqlevel(isr)));
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSECREQUEST_UNLOCK(isr);
|
2002-10-16 02:10:08 +00:00
|
|
|
isr = isr->next;
|
2008-03-14 16:38:11 +00:00
|
|
|
/*
|
|
|
|
* If isr is NULL, we found a 'use' policy w/o SA.
|
|
|
|
* Return w/o error and w/o isr so we can drop out
|
|
|
|
* and continue w/o IPsec processing.
|
|
|
|
*/
|
|
|
|
if (isr == NULL)
|
2002-10-16 02:10:08 +00:00
|
|
|
return isr;
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSECREQUEST_LOCK(isr);
|
2002-10-16 02:10:08 +00:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check system global policy controls.
|
|
|
|
*/
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
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)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
DPRINTF(("%s: IPsec outbound packet dropped due"
|
|
|
|
" to policy (check your sysctls)\n", __func__));
|
2013-06-20 11:44:16 +00:00
|
|
|
IPSEC_OSTAT(pdrops);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EHOSTUNREACH;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanity check the SA contents for the caller
|
|
|
|
* before they invoke the xform output method.
|
|
|
|
*/
|
|
|
|
if (sav->tdb_xform == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
DPRINTF(("%s: no transform for SA\n", __func__));
|
2013-06-20 11:44:16 +00:00
|
|
|
IPSEC_OSTAT(noxform);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EHOSTUNREACH;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
return isr;
|
|
|
|
bad:
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
|
|
|
|
IPSECREQUEST_UNLOCK(isr);
|
2002-10-16 02:10:08 +00:00
|
|
|
return NULL;
|
|
|
|
#undef IPSEC_OSTAT
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:38:45 +00:00
|
|
|
static int
|
|
|
|
ipsec_encap(struct mbuf **mp, struct secasindex *saidx)
|
|
|
|
{
|
|
|
|
#ifdef INET6
|
|
|
|
struct ip6_hdr *ip6;
|
|
|
|
#endif
|
|
|
|
struct ip *ip;
|
|
|
|
int setdf;
|
|
|
|
uint8_t itos, proto;
|
|
|
|
|
|
|
|
ip = mtod(*mp, struct ip *);
|
|
|
|
switch (ip->ip_v) {
|
|
|
|
#ifdef INET
|
|
|
|
case IPVERSION:
|
|
|
|
proto = IPPROTO_IPIP;
|
|
|
|
/*
|
|
|
|
* Collect IP_DF state from the inner header
|
|
|
|
* and honor system-wide control of how to handle it.
|
|
|
|
*/
|
|
|
|
switch (V_ip4_ipsec_dfbit) {
|
|
|
|
case 0: /* clear in outer header */
|
|
|
|
case 1: /* set in outer header */
|
|
|
|
setdf = V_ip4_ipsec_dfbit;
|
|
|
|
break;
|
|
|
|
default:/* propagate to outer header */
|
|
|
|
setdf = (ip->ip_off & ntohs(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;
|
|
|
|
setdf = V_ip4_ipsec_dfbit ? 1: 0;
|
|
|
|
/* scoped address handling */
|
|
|
|
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);
|
|
|
|
M_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);
|
|
|
|
break;
|
|
|
|
#endif /* INET */
|
|
|
|
#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);
|
|
|
|
M_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;
|
2015-04-18 16:46:31 +00:00
|
|
|
/* For link-local address embed scope zone id */
|
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
|
|
|
|
ip6->ip6_dst.s6_addr16[1] =
|
|
|
|
htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
|
2015-04-18 16:38:45 +00:00
|
|
|
ip6->ip6_src = saidx->src.sin6.sin6_addr;
|
2015-04-18 16:46:31 +00:00
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
|
|
|
|
ip6->ip6_src.s6_addr16[1] =
|
|
|
|
htons(saidx->src.sin6.sin6_scope_id & 0xffff);
|
2015-04-18 16:38:45 +00:00
|
|
|
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 /* INET6 */
|
|
|
|
default:
|
|
|
|
return (EAFNOSUPPORT);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
#ifdef INET
|
|
|
|
/*
|
|
|
|
* IPsec output logic for IPv4.
|
|
|
|
*/
|
|
|
|
int
|
2014-12-11 17:34:49 +00:00
|
|
|
ipsec4_process_packet(struct mbuf *m, struct ipsecrequest *isr)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2015-04-18 16:58:33 +00:00
|
|
|
char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
|
2014-12-11 17:34:49 +00:00
|
|
|
union sockaddr_union *dst;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secasindex saidx;
|
|
|
|
struct secasvar *sav;
|
|
|
|
struct ip *ip;
|
2015-04-18 16:38:45 +00:00
|
|
|
int error, i, off;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(isr != NULL, ("null isr"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
|
2008-03-14 16:38:11 +00:00
|
|
|
if (isr == NULL) {
|
|
|
|
if (error != 0)
|
|
|
|
goto bad;
|
|
|
|
return EJUSTRETURN;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
sav = isr->sav;
|
2014-12-11 17:34:49 +00:00
|
|
|
if (m->m_len < sizeof(struct ip) &&
|
|
|
|
(m = m_pullup(m, sizeof (struct ip))) == NULL) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
dst = &sav->sah->saidx.dst;
|
2006-06-26 22:30:08 +00:00
|
|
|
#ifdef DEV_ENC
|
2014-09-19 10:18:14 +00:00
|
|
|
if_inc_counter(encif, IFCOUNTER_OPACKETS, 1);
|
|
|
|
if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len);
|
2008-08-12 09:05:01 +00:00
|
|
|
|
2007-11-28 22:33:53 +00:00
|
|
|
/* pass the mbuf to enc0 for bpf processing */
|
|
|
|
ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
|
2006-06-26 22:30:08 +00:00
|
|
|
/* pass the mbuf to enc0 for packet filtering */
|
2007-11-28 22:33:53 +00:00
|
|
|
if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
|
2006-06-26 22:30:08 +00:00
|
|
|
goto bad;
|
2015-04-28 09:29:28 +00:00
|
|
|
ip = mtod(m, struct ip *);
|
2006-06-26 22:30:08 +00:00
|
|
|
#endif
|
2014-12-11 17:34:49 +00: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 */
|
|
|
|
(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)) {
|
|
|
|
/* Fix IPv4 header checksum and length */
|
|
|
|
ip->ip_len = htons(m->m_pkthdr.len);
|
|
|
|
ip->ip_sum = 0;
|
|
|
|
ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
|
2015-04-18 16:38:45 +00:00
|
|
|
error = ipsec_encap(&m, &sav->sah->saidx);
|
2014-12-11 17:34:49 +00:00
|
|
|
if (error != 0) {
|
2015-04-18 16:38:45 +00:00
|
|
|
DPRINTF(("%s: encapsulation for SA %s->%s "
|
|
|
|
"SPI 0x%08x failed with error %d\n", __func__,
|
2015-04-18 16:58:33 +00:00
|
|
|
ipsec_address(&sav->sah->saidx.src, sbuf,
|
|
|
|
sizeof(sbuf)),
|
|
|
|
ipsec_address(&sav->sah->saidx.dst, dbuf,
|
|
|
|
sizeof(dbuf)), ntohl(sav->spi), error));
|
2014-12-11 17:34:49 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2006-06-26 22:30:08 +00:00
|
|
|
#ifdef DEV_ENC
|
|
|
|
/* pass the mbuf to enc0 for bpf processing */
|
2014-05-28 12:45:27 +00:00
|
|
|
ipsec_bpf(m, sav, sav->sah->saidx.dst.sa.sa_family, ENC_OUT|ENC_AFTER);
|
2007-11-28 22:33:53 +00:00
|
|
|
/* pass the mbuf to enc0 for packet filtering */
|
|
|
|
if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
|
|
|
|
goto bad;
|
2006-06-26 22:30:08 +00:00
|
|
|
#endif
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
|
|
|
* Dispatch to the appropriate IPsec transform logic. The
|
|
|
|
* packet will be returned for transmission after crypto
|
2015-04-18 16:38:45 +00:00
|
|
|
* processing, etc. are completed.
|
2002-10-16 02:10:08 +00:00
|
|
|
*
|
|
|
|
* NB: m & sav are ``passed to caller'' who's reponsible for
|
|
|
|
* for reclaiming their resources.
|
|
|
|
*/
|
2015-04-18 16:38:45 +00:00
|
|
|
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;
|
2014-05-28 12:45:27 +00:00
|
|
|
#ifdef INET6
|
2015-04-18 16:38:45 +00:00
|
|
|
case AF_INET6:
|
|
|
|
i = sizeof(struct ip6_hdr);
|
|
|
|
off = offsetof(struct ip6_hdr, ip6_nxt);
|
|
|
|
break;
|
2014-05-28 12:45:27 +00:00
|
|
|
#endif /* INET6 */
|
2015-04-18 16:38:45 +00:00
|
|
|
default:
|
2014-05-28 12:45:27 +00:00
|
|
|
DPRINTF(("%s: unsupported protocol family %u\n",
|
2015-04-18 16:38:45 +00:00
|
|
|
__func__, dst->sa.sa_family));
|
|
|
|
error = EPFNOSUPPORT;
|
|
|
|
IPSECSTAT_INC(ips_out_inval);
|
|
|
|
goto bad;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2015-04-18 16:38:45 +00:00
|
|
|
error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSECREQUEST_UNLOCK(isr);
|
2015-04-18 16:38:45 +00:00
|
|
|
return (error);
|
2002-10-16 02:10:08 +00:00
|
|
|
bad:
|
2003-09-29 22:57:43 +00:00
|
|
|
if (isr)
|
|
|
|
IPSECREQUEST_UNLOCK(isr);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-05-28 12:45:27 +00:00
|
|
|
#ifdef INET6
|
2002-10-16 02:10:08 +00:00
|
|
|
static int
|
2014-05-28 12:45:27 +00:00
|
|
|
in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2014-05-28 12:45:27 +00:00
|
|
|
struct in6_addr ia2;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2014-05-28 12:45:27 +00:00
|
|
|
memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
|
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
|
|
|
|
ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2014-05-28 12:45:27 +00:00
|
|
|
return IN6_ARE_ADDR_EQUAL(ia, &ia2);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-05-28 12:45:27 +00:00
|
|
|
* IPsec output logic for IPv6.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
|
|
|
int
|
2015-04-18 16:58:33 +00:00
|
|
|
ipsec6_process_packet(struct mbuf *m, struct ipsecrequest *isr)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2015-04-18 16:58:33 +00:00
|
|
|
char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secasindex saidx;
|
2014-05-28 12:45:27 +00:00
|
|
|
struct secasvar *sav;
|
|
|
|
struct ip6_hdr *ip6;
|
|
|
|
int error, i, off;
|
|
|
|
union sockaddr_union *dst;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2014-05-28 12:45:27 +00:00
|
|
|
IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf"));
|
|
|
|
IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr"));
|
2007-05-29 22:44:24 +00:00
|
|
|
|
|
|
|
IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */
|
2014-05-28 12:45:27 +00:00
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
|
2008-03-14 16:38:11 +00:00
|
|
|
if (isr == NULL) {
|
|
|
|
if (error != 0)
|
|
|
|
goto bad;
|
2014-05-28 12:45:27 +00:00
|
|
|
return EJUSTRETURN;
|
2008-03-14 16:38:11 +00:00
|
|
|
}
|
2014-05-28 12:45:27 +00:00
|
|
|
sav = isr->sav;
|
|
|
|
dst = &sav->sah->saidx.dst;
|
|
|
|
|
2014-11-12 22:51:30 +00:00
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
|
|
|
ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
|
2007-11-28 22:33:53 +00:00
|
|
|
#ifdef DEV_ENC
|
2014-09-19 10:18:14 +00:00
|
|
|
if_inc_counter(encif, IFCOUNTER_OPACKETS, 1);
|
|
|
|
if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len);
|
2008-08-12 09:05:01 +00:00
|
|
|
|
2007-11-28 22:33:53 +00:00
|
|
|
/* pass the mbuf to enc0 for bpf processing */
|
|
|
|
ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
|
|
|
|
/* pass the mbuf to enc0 for packet filtering */
|
|
|
|
if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
|
|
|
|
goto bad;
|
2015-04-28 09:29:28 +00:00
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
2014-05-28 12:45:27 +00:00
|
|
|
#endif /* DEV_ENC */
|
|
|
|
|
|
|
|
/* Do the appropriate encapsulation, if necessary */
|
|
|
|
if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
|
|
|
|
dst->sa.sa_family != AF_INET6 || /* PF mismatch */
|
|
|
|
((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) {
|
|
|
|
/* No jumbogram support. */
|
|
|
|
error = ENXIO; /*XXX*/
|
2002-10-16 02:10:08 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
2015-04-18 16:38:45 +00:00
|
|
|
error = ipsec_encap(&m, &sav->sah->saidx);
|
|
|
|
if (error != 0) {
|
|
|
|
DPRINTF(("%s: encapsulation for SA %s->%s "
|
|
|
|
"SPI 0x%08x failed with error %d\n", __func__,
|
2015-04-18 16:58:33 +00:00
|
|
|
ipsec_address(&sav->sah->saidx.src, sbuf,
|
|
|
|
sizeof(sbuf)),
|
|
|
|
ipsec_address(&sav->sah->saidx.dst, dbuf,
|
|
|
|
sizeof(dbuf)), ntohl(sav->spi), error));
|
2002-10-16 02:10:08 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
}
|
2007-11-28 22:33:53 +00:00
|
|
|
|
|
|
|
#ifdef DEV_ENC
|
2014-05-28 12:45:27 +00:00
|
|
|
ipsec_bpf(m, isr->sav, dst->sa.sa_family, ENC_OUT|ENC_AFTER);
|
2007-11-28 22:33:53 +00:00
|
|
|
/* pass the mbuf to enc0 for packet filtering */
|
|
|
|
if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
|
|
|
|
goto bad;
|
2014-05-28 12:45:27 +00:00
|
|
|
#endif /* DEV_ENC */
|
2007-11-28 22:33:53 +00:00
|
|
|
|
2014-05-28 12:45:27 +00:00
|
|
|
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 /* AF_INET */
|
|
|
|
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;
|
|
|
|
IPSEC6STAT_INC(ips_out_inval);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
|
2007-05-29 22:44:24 +00:00
|
|
|
IPSECREQUEST_UNLOCK(isr);
|
|
|
|
return error;
|
2002-10-16 02:10:08 +00:00
|
|
|
bad:
|
2014-05-28 12:45:27 +00:00
|
|
|
|
2007-05-29 22:44:24 +00:00
|
|
|
if (isr)
|
|
|
|
IPSECREQUEST_UNLOCK(isr);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return error;
|
|
|
|
}
|
2014-05-28 23:01:20 +00:00
|
|
|
#endif /*INET6*/
|