@@ -259,6 +259,51 @@ Q: Does Open vSwitch support SRv6?
options:remote_ip=fc00:100::1 \
options:srv6_segs="fc00:100::1,fc00:200::1,fc00:300::1"
+Q: Does the userspace datapath, for example with DPDK, support IPsec?
+
+ A: Yes. Starting with version 4.1, the userspace datapath supports an
+ ``esp`` tunnel type, which carries IPv4 and IPv6 packets in IPsec ESP
+ tunnel mode (RFC 4303) with AES-GCM (RFC 4106). The datapath threads
+ encrypt and decrypt the packets themselves, so this requires Open vSwitch
+ to be built with OpenSSL.
+
+ This is different from the IPsec support described in
+ :doc:`/howto/ipsec`, where ``ovs-monitor-ipsec`` has the Linux kernel
+ encrypt ``gre``, ``geneve`` and ``vxlan`` tunnels of the kernel datapath.
+
+ The following example creates a tunnel to 172.31.1.1, and the commands
+ after it configure the Linux kernel of that host as the other end::
+
+ $ ovs-vsctl add-port br0 esp0 -- set int esp0 type=esp \
+ options:remote_ip=172.31.1.1 \
+ options:esp_in_spi=0x1000 options:esp_out_spi=0x2000 \
+ options:esp_in_key=0x<40 hex digits> \
+ options:esp_out_key=0x<40 other hex digits>
+
+ $ ip xfrm state add src 172.31.1.100 dst 172.31.1.1 proto esp \
+ spi 0x2000 mode tunnel replay-window 64 \
+ aead 'rfc4106(gcm(aes))' 0x<esp_out_key digits> 128
+ $ ip xfrm state add src 172.31.1.1 dst 172.31.1.100 proto esp \
+ spi 0x1000 mode tunnel replay-window 64 \
+ aead 'rfc4106(gcm(aes))' 0x<esp_in_key digits> 128
+
+ plus ``ip xfrm policy`` rules for the traffic to protect. Received
+ packets have their SPI in ``tun_id``.
+
+ The current implementation has these restrictions:
+
+ * Keys are configured manually. Key exchange (IKE) is not supported yet.
+
+ * Only tunnel mode, without UDP encapsulation for NAT traversal.
+
+ * Encapsulated packets are not fragmented, and received ESP packets that
+ are fragments are not reassembled, so the MTU of the inner interfaces
+ must leave room for up to 57 bytes of overhead with an IPv4 underlay
+ and 77 with IPv6.
+
+ * With userspace TSO enabled, packets that need TCP segmentation are
+ dropped.
+
Q: How do I connect two bridges?
A: First, why do you want to do this? Two connected bridges are not much
@@ -101,6 +101,7 @@ Q: Are all features available with all datapaths?
Tunnel - ERSPAN-IPv6 4.18 2.10
Tunnel - GTP-U NO 2.14
Tunnel - SRv6 NO 3.2
+ Tunnel - ESP (IPsec) NO 4.1
Tunnel - Bareudp 5.7 NO
QoS - Policing YES 2.6
QoS - Shaping YES NO
@@ -6,6 +6,10 @@ Post-v4.0.0
- ovs-vswitchd:
* Mirror filters now accept masked fields, e.g. "ip,nw_src=10.0.0.0/24"
in the "filter" column of the Mirror table.
+ - Userspace datapath:
+ * New 'esp' tunnel type, which carries IPv4 and IPv6 packets in IPsec
+ ESP tunnel mode with AES-GCM, encrypted by the datapath threads.
+ The keys are configured manually for now.
v4.0.0 - 17 Aug 2026
@@ -261,6 +261,7 @@ enum ovs_vport_type {
OVS_VPORT_TYPE_GTPU = 110,
OVS_VPORT_TYPE_BAREUDP = 111, /* Bareudp tunnel. */
OVS_VPORT_TYPE_SRV6 = 112, /* SRv6 tunnel. */
+ OVS_VPORT_TYPE_ESP = 113, /* IPsec ESP tunnel. */
__OVS_VPORT_TYPE_MAX
};
@@ -133,6 +133,7 @@ vport_type_to_kind(enum ovs_vport_type type,
case OVS_VPORT_TYPE_INTERNAL:
case OVS_VPORT_TYPE_GTPU:
case OVS_VPORT_TYPE_SRV6:
+ case OVS_VPORT_TYPE_ESP:
case OVS_VPORT_TYPE_UNSPEC:
case __OVS_VPORT_TYPE_MAX:
default:
@@ -316,6 +317,7 @@ dpif_netlink_rtnl_verify(const struct netdev_tunnel_config *tnl_cfg,
case OVS_VPORT_TYPE_INTERNAL:
case OVS_VPORT_TYPE_GTPU:
case OVS_VPORT_TYPE_SRV6:
+ case OVS_VPORT_TYPE_ESP:
case OVS_VPORT_TYPE_UNSPEC:
case __OVS_VPORT_TYPE_MAX:
default:
@@ -407,6 +409,7 @@ dpif_netlink_rtnl_create(const struct netdev_tunnel_config *tnl_cfg,
case OVS_VPORT_TYPE_INTERNAL:
case OVS_VPORT_TYPE_GTPU:
case OVS_VPORT_TYPE_SRV6:
+ case OVS_VPORT_TYPE_ESP:
case OVS_VPORT_TYPE_UNSPEC:
case __OVS_VPORT_TYPE_MAX:
default:
@@ -521,6 +524,7 @@ dpif_netlink_rtnl_tunnel_destroy(const char *name, const char *type)
case OVS_VPORT_TYPE_INTERNAL:
case OVS_VPORT_TYPE_GTPU:
case OVS_VPORT_TYPE_SRV6:
+ case OVS_VPORT_TYPE_ESP:
case OVS_VPORT_TYPE_UNSPEC:
case __OVS_VPORT_TYPE_MAX:
default:
@@ -809,6 +809,9 @@ get_vport_type(const struct dpif_netlink_vport *vport)
case OVS_VPORT_TYPE_SRV6:
return "srv6";
+ case OVS_VPORT_TYPE_ESP:
+ return "esp";
+
case OVS_VPORT_TYPE_BAREUDP:
return "bareudp";
@@ -845,6 +848,8 @@ netdev_to_ovs_vport_type(const char *type)
return OVS_VPORT_TYPE_GTPU;
} else if (!strcmp(type, "srv6")) {
return OVS_VPORT_TYPE_SRV6;
+ } else if (!strcmp(type, "esp")) {
+ return OVS_VPORT_TYPE_ESP;
} else if (!strcmp(type, "bareudp")) {
return OVS_VPORT_TYPE_BAREUDP;
} else {
@@ -37,6 +37,7 @@
#include "csum.h"
#include "dp-packet.h"
#include "dpif-offload.h"
+#include "esp.h"
#include "netdev.h"
#include "netdev-vport.h"
#include "netdev-vport-private.h"
@@ -1112,6 +1113,124 @@ err:
return NULL;
}
+int
+netdev_esp_build_header(const struct netdev *netdev,
+ struct ovs_action_push_tnl *data,
+ const struct netdev_tnl_build_header_params *params)
+{
+ const struct netdev_tunnel_config *tnl_cfg;
+ struct esp_header *esp;
+
+ /* netdev_esp_push_header() finds the outbound SA from this SPI and the
+ * destination address, and fills in the rest of the ESP header. */
+ tnl_cfg = netdev_get_tunnel_config(netdev);
+ esp = netdev_tnl_ip_build_header(data, params, IPPROTO_ESP, 0);
+ put_16aligned_be32(&esp->spi, tnl_cfg->esp_out_spi);
+ put_16aligned_be32(&esp->seq_no, 0);
+ memset(esp + 1, 0, ESP_IV_LEN);
+
+ data->header_len += ESP_PREFIX_LEN;
+ data->tnl_type = OVS_VPORT_TYPE_ESP;
+
+ return 0;
+}
+
+int
+netdev_esp_push_header(const struct netdev *netdev,
+ const struct netdev *ingress_netdev OVS_UNUSED,
+ struct dp_packet *packet,
+ const struct ovs_action_push_tnl *data)
+{
+ struct esp_header *esp;
+ struct in6_addr dst;
+ size_t payload_len;
+ struct esp_sa *sa;
+ uint8_t next_hdr;
+ int ip_tot_size;
+
+ if (packet->packet_type == htonl(PT_IPV4)) {
+ next_hdr = IPPROTO_IPIP;
+ } else if (packet->packet_type == htonl(PT_IPV6)) {
+ next_hdr = IPPROTO_IPV6;
+ } else {
+ VLOG_WARN_RL(&err_rl, "%s: esp tunnels only carry IP packets",
+ netdev_get_name(netdev));
+ return EPROTONOSUPPORT;
+ }
+
+ /* Do not encrypt Ethernet padding. */
+ payload_len = dp_packet_size(packet) - dp_packet_l2_pad_size(packet);
+ dp_packet_set_size(packet, payload_len);
+
+ dp_packet_put_uninit(packet, esp_trailer_len(payload_len));
+ esp = netdev_tnl_push_ip_header(packet, data->header, data->header_len,
+ &ip_tot_size, 0);
+
+ /* 'netdev' is the datapath port shared by all esp tunnels, so find the
+ * outbound SA from the SPI and destination in the header. */
+ if (netdev_tnl_is_header_ipv6(data->header)) {
+ const struct ovs_16aligned_ip6_hdr *ip6
+ = netdev_tnl_ipv6_hdr(CONST_CAST(void *, data->header));
+
+ memcpy(&dst, &ip6->ip6_dst, sizeof dst);
+ } else {
+ const struct ip_header *ip
+ = netdev_tnl_ip_hdr(CONST_CAST(void *, data->header));
+
+ in6_addr_set_mapped_ipv4(&dst, get_16aligned_be32(&ip->ip_dst));
+ }
+ sa = esp_sad_lookup(get_16aligned_be32(&esp->spi), &dst);
+ if (OVS_UNLIKELY(!sa)) {
+ return ENOENT;
+ }
+
+ return esp_seal(sa, esp, payload_len, next_hdr);
+}
+
+struct dp_packet *
+netdev_esp_pop_header(struct dp_packet *packet)
+{
+ struct pkt_metadata *md = &packet->md;
+ struct flow_tnl *tnl = &md->tunnel;
+ size_t esp_len, payload_len;
+ struct esp_header *esp;
+ unsigned int hlen;
+ uint8_t next_hdr;
+ ovs_be32 spi;
+
+ pkt_metadata_init_tnl(md);
+ esp = ip_extract_tnl_md(packet, tnl, &hlen);
+ if (!esp) {
+ goto err;
+ }
+
+ esp_len = dp_packet_size(packet) - dp_packet_l2_pad_size(packet) - hlen;
+ if (esp_open(esp, esp_len, &spi, &next_hdr, &payload_len)) {
+ goto err;
+ }
+
+ if (next_hdr == IPPROTO_IPIP) {
+ packet->packet_type = htonl(PT_IPV4);
+ } else if (next_hdr == IPPROTO_IPV6) {
+ packet->packet_type = htonl(PT_IPV6);
+ } else {
+ /* This includes dummy packets (IPPROTO_NONE), which must be
+ * discarded (RFC 4303 section 2.6). */
+ goto err;
+ }
+
+ tnl->tun_id = htonll(ntohl(spi));
+ tnl->flags |= FLOW_TNL_F_KEY;
+
+ dp_packet_reset_packet(packet, hlen + ESP_PREFIX_LEN);
+ dp_packet_set_size(packet, payload_len);
+
+ return packet;
+err:
+ dp_packet_delete(packet);
+ return NULL;
+}
+
struct dp_packet *
netdev_vxlan_pop_header(struct dp_packet *packet)
{
@@ -79,6 +79,17 @@ int netdev_srv6_build_header(const struct netdev *,
struct ovs_action_push_tnl *,
const struct netdev_tnl_build_header_params *);
+struct dp_packet *netdev_esp_pop_header(struct dp_packet *);
+
+int netdev_esp_push_header(const struct netdev *,
+ const struct netdev *ingress_netdev,
+ struct dp_packet *,
+ const struct ovs_action_push_tnl *);
+
+int netdev_esp_build_header(const struct netdev *,
+ struct ovs_action_push_tnl *,
+ const struct netdev_tnl_build_header_params *);
+
int
netdev_tnl_push_udp_header(const struct netdev *netdev,
const struct netdev *ingress_netdev,
@@ -25,6 +25,8 @@
#include "ovs-atomic.h"
#include "ovs-thread.h"
+struct esp_sa;
+
struct netdev_vport {
struct netdev up;
@@ -45,6 +47,11 @@ struct netdev_vport {
/* Patch Ports. */
char *peer;
+
+ /* ESP tunnels. These security associations are in the ESP SA database,
+ * where the datapath finds them. Only used by the main thread. */
+ struct esp_sa *esp_in_sa;
+ struct esp_sa *esp_out_sa;
};
int netdev_vport_construct(struct netdev *);
@@ -32,6 +32,7 @@
#include "daemon.h"
#include "dirs.h"
#include "dpif.h"
+#include "esp.h"
#include "netdev.h"
#include "netdev-native-tnl.h"
#include "netdev-provider.h"
@@ -246,6 +247,15 @@ netdev_vport_destruct(struct netdev *netdev_)
update_vxlan_global_cfg(netdev_, tnl_cfg, NULL);
}
+ if (netdev->esp_in_sa) {
+ esp_sad_remove(netdev->esp_in_sa);
+ esp_sa_destroy_postponed(netdev->esp_in_sa);
+ }
+ if (netdev->esp_out_sa) {
+ esp_sad_remove(netdev->esp_out_sa);
+ esp_sa_destroy_postponed(netdev->esp_out_sa);
+ }
+
ovsrcu_set(&netdev->tnl_cfg, NULL);
ovsrcu_postpone(free, CONST_CAST(struct netdev_tunnel_config *, tnl_cfg));
free(netdev->peer);
@@ -482,6 +492,8 @@ tunnel_supported_layers(const char *type,
return TNL_L3;
} else if (!strcmp(type, "srv6")) {
return TNL_L3;
+ } else if (!strcmp(type, "esp")) {
+ return TNL_L3;
} else {
return TNL_L2;
}
@@ -604,6 +616,192 @@ is_concomitant_vxlan_tunnel_present(struct netdev_vport *dev,
return false;
}
+/* Parses 's' as an ESP SPI. SPIs 1 to 255 are reserved (RFC 4303). */
+static bool
+parse_esp_spi(const char *s, ovs_be32 *spi)
+{
+ unsigned long long int value;
+ char *tail;
+
+ errno = 0;
+ value = strtoull(s, &tail, 0);
+ if (errno || *tail || tail == s || value < 256 || value > UINT32_MAX) {
+ return false;
+ }
+ *spi = htonl(value);
+ return true;
+}
+
+static bool
+is_esp_option(const char *key)
+{
+ return (!strcmp(key, "esp_in_spi") || !strcmp(key, "esp_out_spi")
+ || !strcmp(key, "esp_in_key") || !strcmp(key, "esp_out_key")
+ || !strcmp(key, "esp_esn") || !strcmp(key, "esp_replay_window"));
+}
+
+/* Parses the ESP options in 'args' for ESP tunnel 'dev', checks them against
+ * 'tnl_cfg' and completes 'tnl_cfg'. Creates in '*new_in' and '*new_out' the
+ * security associations whose parameters changed, or sets them to NULL.
+ *
+ * Returns 0 if successful, otherwise appends an error to 'errors' and
+ * returns a positive errno value. */
+static int
+esp_tunnel_config(struct netdev_vport *dev, const struct smap *args,
+ struct netdev_tunnel_config *tnl_cfg,
+ struct esp_sa **new_in, struct esp_sa **new_out,
+ struct ds *errors)
+{
+ const char *name = netdev_get_name(&dev->up);
+ struct esp_sa *cur_in, *cur_out, *other;
+ struct esp_sa_params in, out;
+ unsigned int replay_window;
+ const char *s;
+ char *error;
+
+ *new_in = *new_out = NULL;
+
+ if (!esp_is_supported()) {
+ ds_put_format(errors, "%s: esp tunnels require Open vSwitch built "
+ "with OpenSSL\n", name);
+ return EOPNOTSUPP;
+ }
+ if (tnl_cfg->ip_dst_flow || tnl_cfg->ip_src_flow) {
+ ds_put_format(errors, "%s: esp tunnels do not support "
+ "'remote_ip=flow' or 'local_ip=flow'\n", name);
+ return EINVAL;
+ }
+ if (smap_get(args, "key") || smap_get(args, "in_key")
+ || smap_get(args, "out_key")) {
+ ds_put_format(errors, "%s: esp tunnels do not support 'key', "
+ "'in_key' or 'out_key', the tunnel ID is the "
+ "inbound SPI\n", name);
+ return EINVAL;
+ }
+
+ memset(&in, 0, sizeof in);
+ memset(&out, 0, sizeof out);
+
+ s = smap_get(args, "esp_in_spi");
+ if (!s || !parse_esp_spi(s, &in.spi)) {
+ ds_put_format(errors, "%s: esp tunnels require 'esp_in_spi' "
+ "between 256 and 4294967295\n", name);
+ return EINVAL;
+ }
+ s = smap_get(args, "esp_out_spi");
+ if (!s || !parse_esp_spi(s, &out.spi)) {
+ ds_put_format(errors, "%s: esp tunnels require 'esp_out_spi' "
+ "between 256 and 4294967295\n", name);
+ return EINVAL;
+ }
+
+ s = smap_get(args, "esp_in_key");
+ error = s ? esp_parse_key(s, &in) : xstrdup("missing");
+ if (error) {
+ ds_put_format(errors, "%s: bad 'esp_in_key': %s\n", name, error);
+ free(error);
+ return EINVAL;
+ }
+ s = smap_get(args, "esp_out_key");
+ error = s ? esp_parse_key(s, &out) : xstrdup("missing");
+ if (error) {
+ ds_put_format(errors, "%s: bad 'esp_out_key': %s\n", name, error);
+ free(error);
+ return EINVAL;
+ }
+
+ in.esn = out.esn = smap_get_bool(args, "esp_esn", false);
+ replay_window = smap_get_uint(args, "esp_replay_window",
+ ESP_DEFAULT_REPLAY_WINDOW);
+ if (replay_window > ESP_MAX_REPLAY_WINDOW
+ || (in.esn && !replay_window)) {
+ ds_put_format(errors, "%s: 'esp_replay_window' must be between "
+ "%d and %d\n", name, in.esn ? 1 : 0,
+ ESP_MAX_REPLAY_WINDOW);
+ return EINVAL;
+ }
+ in.replay_window = replay_window;
+
+ /* An SA is identified by its SPI and, for outbound SAs, the peer's
+ * address. Two tunnels cannot share one. */
+ out.dst = tnl_cfg->ipv6_dst;
+ cur_in = dev->esp_in_sa;
+ other = esp_sad_lookup(in.spi, &in.dst);
+ if (other && other != cur_in) {
+ ds_put_format(errors, "%s: 'esp_in_spi' %"PRIu32" is already used "
+ "by another esp tunnel\n", name, ntohl(in.spi));
+ return EEXIST;
+ }
+ cur_out = dev->esp_out_sa;
+ other = esp_sad_lookup(out.spi, &out.dst);
+ if (other && other != cur_out) {
+ ds_put_format(errors, "%s: 'esp_out_spi' %"PRIu32" is already used "
+ "by another esp tunnel to the same 'remote_ip'\n",
+ name, ntohl(out.spi));
+ return EEXIST;
+ }
+
+ /* Keep the SAs whose parameters do not change, so that their sequence
+ * numbers and anti-replay windows carry on. */
+ if (!cur_in || !esp_sa_params_equal(esp_sa_get_params(cur_in), &in)) {
+ *new_in = esp_sa_create(&in);
+ if (!*new_in) {
+ goto create_error;
+ }
+ }
+ if (!cur_out || !esp_sa_params_equal(esp_sa_get_params(cur_out), &out)) {
+ *new_out = esp_sa_create(&out);
+ if (!*new_out) {
+ goto create_error;
+ }
+ }
+
+ tnl_cfg->esp_in_spi = in.spi;
+ tnl_cfg->esp_out_spi = out.spi;
+ tnl_cfg->esp_esn = in.esn;
+ tnl_cfg->esp_replay_window = in.replay_window;
+
+ /* Received packets carry the inbound SPI as their tunnel ID. */
+ tnl_cfg->in_key = htonll(ntohl(in.spi));
+ tnl_cfg->in_key_present = true;
+ tnl_cfg->out_key = htonll(ntohl(out.spi));
+ tnl_cfg->out_key_present = true;
+
+ return 0;
+
+create_error:
+ esp_sa_destroy(*new_in);
+ *new_in = NULL;
+ ds_put_format(errors, "%s: failed to create esp security association\n",
+ name);
+ return EINVAL;
+}
+
+/* Replaces the SA in '*cur', if any, by 'new', if nonnull, in the SAD. */
+static void
+esp_tunnel_install_sa(struct esp_sa **cur, struct esp_sa *new)
+{
+ const struct esp_sa_params *new_p;
+ struct esp_sa *old = *cur;
+
+ if (!new) {
+ return;
+ }
+
+ new_p = esp_sa_get_params(new);
+ if (old && esp_sa_get_params(old)->spi == new_p->spi
+ && ipv6_addr_equals(&esp_sa_get_params(old)->dst, &new_p->dst)) {
+ esp_sad_replace(old, new);
+ } else {
+ if (old) {
+ esp_sad_remove(old);
+ }
+ ovs_assert(!esp_sad_insert(new));
+ }
+ *cur = new;
+ esp_sa_destroy_postponed(old);
+}
+
static int
set_tunnel_config(struct netdev *dev_, const struct smap *args, char **errp)
{
@@ -612,7 +810,8 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args, char **errp)
const char *name = netdev_get_name(dev_);
const char *type = netdev_get_type(dev_);
struct ds errors = DS_EMPTY_INITIALIZER;
- bool needs_dst_port, has_csum, has_seq;
+ bool needs_dst_port, has_csum, has_seq, is_esp;
+ struct esp_sa *esp_in = NULL, *esp_out = NULL;
uint16_t dst_proto = 0, src_proto = 0;
struct netdev_tunnel_config tnl_cfg;
struct smap_node *node;
@@ -621,6 +820,7 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args, char **errp)
has_csum = strstr(type, "gre") || strstr(type, "geneve") ||
strstr(type, "vxlan");
has_seq = strstr(type, "gre");
+ is_esp = !strcmp(type, "esp");
memset(&tnl_cfg, 0, sizeof tnl_cfg);
/* Add a default destination port for tunnel ports if none specified. */
@@ -700,7 +900,8 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args, char **errp)
} else if (!strcmp(node->key, "key") ||
!strcmp(node->key, "in_key") ||
!strcmp(node->key, "out_key") ||
- !strcmp(node->key, "packet_type")) {
+ !strcmp(node->key, "packet_type") ||
+ (is_esp && is_esp_option(node->key))) {
/* Handled separately below. */
} else if (!strcmp(node->key, "exts") && !strcmp(type, "vxlan")) {
char *str = xstrdup(node->value);
@@ -918,6 +1119,14 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args, char **errp)
&tnl_cfg.out_key_present,
&tnl_cfg.out_key_flow);
+ if (is_esp) {
+ err = esp_tunnel_config(dev, args, &tnl_cfg, &esp_in, &esp_out,
+ &errors);
+ if (err) {
+ goto out;
+ }
+ }
+
if (is_concomitant_vxlan_tunnel_present(dev, &tnl_cfg)) {
ds_put_format(&errors, "%s: VXLAN-GBP, and non-VXLAN-GBP "
"tunnels can't be configured on the same "
@@ -929,6 +1138,9 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args, char **errp)
ovs_mutex_lock(&dev->mutex);
+ esp_tunnel_install_sa(&dev->esp_in_sa, esp_in);
+ esp_tunnel_install_sa(&dev->esp_out_sa, esp_out);
+
curr_tnl_cfg = vport_tunnel_config(dev);
update_vxlan_global_cfg(dev_, curr_tnl_cfg, &tnl_cfg);
@@ -944,6 +1156,10 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args, char **errp)
err = 0;
out:
+ if (err) {
+ esp_sa_destroy(esp_in);
+ esp_sa_destroy(esp_out);
+ }
if (errors.length) {
ds_chomp(&errors, '\n');
VLOG_WARN("%s", ds_cstr(&errors));
@@ -979,7 +1195,21 @@ get_tunnel_config(const struct netdev *dev, struct smap *args)
smap_add(args, "local_ip", "flow");
}
- if (tnl_cfg->in_key_flow && tnl_cfg->out_key_flow) {
+ if (!strcmp(type, "esp")) {
+ /* The tunnel IDs are derived from the SPIs. The ESP keys are secret,
+ * so they are left out. */
+ smap_add_format(args, "esp_in_spi", "%"PRIu32,
+ ntohl(tnl_cfg->esp_in_spi));
+ smap_add_format(args, "esp_out_spi", "%"PRIu32,
+ ntohl(tnl_cfg->esp_out_spi));
+ if (tnl_cfg->esp_esn) {
+ smap_add(args, "esp_esn", "true");
+ }
+ if (tnl_cfg->esp_replay_window != ESP_DEFAULT_REPLAY_WINDOW) {
+ smap_add_format(args, "esp_replay_window", "%"PRIu16,
+ tnl_cfg->esp_replay_window);
+ }
+ } else if (tnl_cfg->in_key_flow && tnl_cfg->out_key_flow) {
smap_add(args, "key", "flow");
} else if (tnl_cfg->in_key_present && tnl_cfg->out_key_present
&& tnl_cfg->in_key == tnl_cfg->out_key) {
@@ -1361,6 +1591,17 @@ netdev_vport_tunnel_register(void)
},
{{NULL, NULL, 0, 0}}
},
+ { "esp_sys",
+ {
+ TUNNEL_FUNCTIONS_COMMON,
+ .type = "esp",
+ .build_header = netdev_esp_build_header,
+ .push_header = netdev_esp_push_header,
+ .pop_header = netdev_esp_pop_header,
+ .get_ifindex = NETDEV_VPORT_GET_IFINDEX,
+ },
+ {{NULL, NULL, 0, 0}}
+ },
};
static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
@@ -176,6 +176,12 @@ struct netdev_tunnel_config {
#define SRV6_MAX_SEGS 6
struct in6_addr srv6_segs[SRV6_MAX_SEGS];
enum netdev_srv6_flowlabel srv6_flowlabel;
+
+ /* ESP. The keys are only kept in the tunnel's security associations. */
+ ovs_be32 esp_in_spi;
+ ovs_be32 esp_out_spi;
+ bool esp_esn;
+ uint16_t esp_replay_window;
};
void netdev_run(void);
@@ -30,6 +30,7 @@
#include "byte-order.h"
#include "coverage.h"
#include "dpif.h"
+#include "esp.h"
#include "openvswitch/dynamic-string.h"
#include "flow.h"
#include "netlink.h"
@@ -903,6 +904,16 @@ format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
",msgtype=%"PRIu8",teid=0x%"PRIx32")",
gtph->md.flags, gtph->md.msgtype,
ntohl(get_16aligned_be32(>ph->teid)));
+ } else if (data->tnl_type == OVS_VPORT_TYPE_ESP) {
+ const struct esp_header *esp = l4;
+
+ if (bytes_left < sizeof *esp) {
+ ds_put_cstr(ds, "esp(truncated header))");
+ return;
+ }
+
+ ds_put_format(ds, "esp(spi=0x%"PRIx32")",
+ ntohl(get_16aligned_be32(&esp->spi)));
}
ds_put_format(ds, ")");
@@ -1716,6 +1727,7 @@ ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
uint32_t teid;
uint8_t gtpu_flags, gtpu_msgtype;
uint8_t segments_left;
+ uint32_t spi;
if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
return -EINVAL;
@@ -2008,6 +2020,15 @@ ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
if (n_segs != segments_left + 1) {
return -EINVAL;
}
+ } else if (ovs_scan_len(s, &n, "esp(spi=0x%"SCNx32"))", &spi)) {
+ struct esp_header *esp = l4;
+
+ put_16aligned_be32(&esp->spi, htonl(spi));
+ put_16aligned_be32(&esp->seq_no, 0);
+ memset(esp + 1, 0, ESP_IV_LEN);
+
+ tnl_type = OVS_VPORT_TYPE_ESP;
+ header_len = sizeof *eth + ip_len + ESP_PREFIX_LEN;
} else {
return -EINVAL;
}
@@ -126,7 +126,7 @@ map_insert(odp_port_t port, struct eth_addr mac, struct in6_addr *addr,
/* XXX: No fragments support. */
match.wc.masks.nw_frag = FLOW_NW_FRAG_MASK;
- /* 'tp_port' is zero for GRE and SRv6 tunnels. In this case it
+ /* 'tp_port' is zero for GRE, SRv6 and ESP tunnels. In this case it
* doesn't make sense to match on UDP port numbers. */
if (tp_port) {
match.wc.masks.tp_dst = OVS_BE16_MAX;
@@ -175,6 +175,8 @@ tnl_type_to_nw_proto(const char type[], uint8_t nw_protos[2])
} else if (!strcmp(type, "srv6")) {
nw_protos[0] = IPPROTO_IPIP;
nw_protos[1] = IPPROTO_IPV6;
+ } else if (!strcmp(type, "esp")) {
+ nw_protos[0] = IPPROTO_ESP;
}
}
@@ -3883,6 +3883,9 @@ propagate_tunnel_data_to_flow(struct xlate_ctx *ctx, struct eth_addr dmac,
nw_proto = (flow->dl_type == htons(ETH_TYPE_IP))
? IPPROTO_IPIP : IPPROTO_IPV6;
break;
+ case OVS_VPORT_TYPE_ESP:
+ nw_proto = IPPROTO_ESP;
+ break;
case OVS_VPORT_TYPE_UNSPEC:
case OVS_VPORT_TYPE_NETDEV:
case OVS_VPORT_TYPE_INTERNAL:
@@ -525,6 +525,13 @@ class ODPFlow(Flow):
}
)
),
+ "esp": nested_kv_decoder(
+ KVDecoders(
+ {
+ "spi": decode_int,
+ }
+ )
+ ),
}
)
),
@@ -475,6 +475,21 @@ def test_odp_fields(input_string, expected):
),
],
),
+ (
+ "actions:tnl_push(header(esp(spi=0x1000)))",
+ [
+ KeyValue(
+ "tnl_push",
+ {
+ "header": {
+ "esp": {
+ "spi": 0x1000,
+ }
+ }
+ },
+ ),
+ ],
+ ),
(
"actions:clone(1),clone(clone(push_vlan(vid=12,pcp=0),2),1)",
[
@@ -344,6 +344,8 @@ tnl_push(tnl_port(6),header(size=78,type=5,eth(dst=f8:bc:12:44:34:b6,src=f8:bc:1
tnl_push(tnl_port(6),header(size=70,type=5,eth(dst=f8:bc:12:44:34:b6,src=f8:bc:12:46:58:e0,dl_type=0x86dd),ipv6(src=2001:cafe::88,dst=2001:cafe::92,label=0,proto=17,tclass=0x0,hlimit=64),udp(src=0,dst=6081,csum=0xffff),geneve(vni=0x1c7)),out_port(1))
tnl_push(tnl_port(6),header(size=78,type=112,eth(dst=f8:bc:12:44:34:b6,src=f8:bc:12:46:58:e0,dl_type=0x86dd),ipv6(src=2001:cafe::88,dst=2001:cafe::92,label=0,proto=43,tclass=0x0,hlimit=64),srv6(segments_left=0,segs(2001:cafe::90))),out_port(1))
tnl_push(tnl_port(6),header(size=110,type=112,eth(dst=f8:bc:12:44:34:b6,src=f8:bc:12:46:58:e0,dl_type=0x86dd),ipv6(src=2001:cafe::88,dst=2001:cafe::92,label=0,proto=43,tclass=0x0,hlimit=64),srv6(segments_left=2,segs(2001:cafe::90,2001:cafe::91,2001:cafe::92))),out_port(1))
+tnl_push(tnl_port(7),header(size=50,type=113,eth(dst=f8:bc:12:44:34:b6,src=f8:bc:12:46:58:e0,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=50,tos=0,ttl=64,frag=0x4000),esp(spi=0x1000)),out_port(1))
+tnl_push(tnl_port(7),header(size=70,type=113,eth(dst=f8:bc:12:44:34:b6,src=f8:bc:12:46:58:e0,dl_type=0x86dd),ipv6(src=2001:cafe::88,dst=2001:cafe::92,label=0,proto=50,tclass=0x0,hlimit=64),esp(spi=0xfffffffe)),out_port(1))
ct
ct(commit)
ct(commit,zone=5)
@@ -410,6 +412,7 @@ AT_DATA([actions.txt], [dnl
encap_nsh@:{@
tnl_push(tnl_port(6),header(size=94,type=112,eth(dst=f8:bc:12:44:34:b6,src=f8:bc:12:46:58:e0,dl_type=0x86dd),ipv6(src=2001:cafe::88,dst=2001:cafe::92,label=0,proto=43,tclass=0x0,hlimit=64),srv6(segments_left=2,segs(2001:cafe::90,2001:cafe::91))),out_port(1))
tnl_push(tnl_port(6),header(size=126,type=112,eth(dst=f8:bc:12:44:34:b6,src=f8:bc:12:46:58:e0,dl_type=0x86dd),ipv6(src=2001:cafe::88,dst=2001:cafe::92,label=0,proto=43,tclass=0x0,hlimit=64),srv6(segments_left=2,segs(2001:cafe::90,2001:cafe::91,2001:cafe::92,2001:cafe::93))),out_port(1))
+tnl_push(tnl_port(7),header(size=42,type=113,eth(dst=f8:bc:12:44:34:b6,src=f8:bc:12:46:58:e0,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=50,tos=0,ttl=64,frag=0x4000),esp(spi=0x1000)),out_port(1))
psample(group_id=12,cookie=0x0102030405060708090a0b0c0d0e0f0f0f)
psample(cookie=0x010203)
psample(group=12,cookie=0x010203,group=12)
@@ -427,6 +430,7 @@ odp_actions_from_string: error
odp_actions_from_string: error
odp_actions_from_string: error
odp_actions_from_string: error
+odp_actions_from_string: error
])
AT_CLEANUP
@@ -246,6 +246,14 @@ m4_define([OVS_CHECK_SRV6],
AT_SKIP_IF([:])
])
+# OVS_CHECK_ESP()
+#
+# The kernel datapath does not support this feature.
+m4_define([OVS_CHECK_ESP],
+[
+ AT_SKIP_IF([:])
+])
+
# CHECK_LATER_IPV6_FRAGMENTS()
#
# Upstream kernels beetween 4.20 and 5.19 are not parsing IPv6 fragments
@@ -1703,6 +1703,94 @@ NS_CHECK_EXEC([at_ns1], [ping -q -c 3 -i 0.3 -W 2 10.100.100.100 | FORMAT_PING],
OVS_TRAFFIC_VSWITCHD_STOP
AT_CLEANUP
+# ESP_TUNNEL_TEST([esp-options], [xfrm-state-options])
+#
+# Checks IPv4 and IPv6 traffic through an ESP tunnel between OVS and the
+# kernel's IPsec implementation in the namespace 'at_ns0'.
+m4_define([ESP_TUNNEL_TEST], [
+OVS_CHECK_ESP()
+OVS_TRAFFIC_VSWITCHD_START()
+ADD_NAMESPACES(at_ns0)
+
+m4_define([KEY_IN], [0x101112131415161718191a1b1c1d1e1f22334455])
+m4_define([KEY_OUT], [0x303132333435363738393a3b3c3d3e3f66778899])
+
+dnl Set up the underlay link from host into the namespace 'at_ns0' using a
+dnl veth pair.
+ADD_BR([br-underlay])
+AT_CHECK([ovs-ofctl add-flow br-underlay "actions=normal"])
+ADD_VETH(p0, at_ns0, br-underlay, "172.31.1.1/24")
+AT_CHECK([ip addr add dev br-underlay "172.31.1.100/24"])
+AT_CHECK([ip link set dev br-underlay up])
+
+dnl Set up the tunnel endpoint on OVS outside the namespace. ESP adds up
+dnl to 57 bytes to each packet.
+ADD_OVS_TUNNEL([esp], [br0], [at_esp0], [172.31.1.1], [10.1.1.100/24],
+ [options:esp_in_spi=0x1000 options:esp_out_spi=0x2000 dnl
+ options:esp_in_key=KEY_IN options:esp_out_key=KEY_OUT $1])
+AT_CHECK([ovs-vsctl set bridge br0 other_config:hwaddr=aa:55:aa:55:00:00])
+AT_CHECK([ip link set dev br0 mtu 1400])
+AT_CHECK([ip addr add dev br0 fd00::100/64 nodad])
+AT_CHECK([ip neigh add 10.1.1.1 lladdr aa:55:aa:55:00:01 dev br0])
+AT_CHECK([ip -6 neigh add fd00::1 lladdr aa:55:aa:55:00:01 dev br0])
+AT_CHECK([ovs-ofctl add-flow br0 in_port=LOCAL,actions=output:at_esp0])
+AT_CHECK([ovs-ofctl add-flow br0 dnl
+ in_port=at_esp0,actions=mod_dl_dst:aa:55:aa:55:00:00,output:LOCAL])
+
+dnl Set up the other tunnel endpoint in the kernel of the namespace 'at_ns0'.
+dnl The SAs carry both IPv4 and IPv6, hence 'af-unspec'.
+NS_CHECK_EXEC([at_ns0], [ip link set dev lo up])
+NS_CHECK_EXEC([at_ns0], [ip addr add 10.1.1.1/32 dev lo])
+NS_CHECK_EXEC([at_ns0], [ip addr add fd00::1/128 dev lo])
+NS_CHECK_EXEC([at_ns0], [ip route add 10.1.1.100/32 dev p0 src 10.1.1.1])
+NS_CHECK_EXEC([at_ns0], [ip route add fd00::100/128 dev p0 src fd00::1])
+NS_CHECK_EXEC([at_ns0], [ip xfrm state add src 172.31.1.100 dst 172.31.1.1 dnl
+ proto esp spi 0x2000 mode tunnel flag af-unspec $2 dnl
+ aead 'rfc4106(gcm(aes))' KEY_OUT 128])
+NS_CHECK_EXEC([at_ns0], [ip xfrm state add src 172.31.1.1 dst 172.31.1.100 dnl
+ proto esp spi 0x1000 mode tunnel flag af-unspec $2 dnl
+ aead 'rfc4106(gcm(aes))' KEY_IN 128])
+for inner in "10.1.1.1/32 10.1.1.100/32" "fd00::1/128 fd00::100/128"; do
+ set $inner
+ NS_CHECK_EXEC([at_ns0], [ip xfrm policy add src $[]1 dst $[]2 dir out dnl
+ tmpl src 172.31.1.1 dst 172.31.1.100 proto esp mode tunnel])
+ NS_CHECK_EXEC([at_ns0], [ip xfrm policy add src $[]2 dst $[]1 dir in dnl
+ tmpl src 172.31.1.100 dst 172.31.1.1 proto esp mode tunnel])
+done
+
+dnl First, check the underlay.
+NS_CHECK_EXEC([at_ns0], [ping -q -c 3 -i 0.3 -W 2 172.31.1.100 | FORMAT_PING], [0], [dnl
+3 packets transmitted, 3 received, 0% packet loss, time 0ms
+])
+
+dnl Okay, now check the overlay with different packet sizes.
+NS_CHECK_EXEC([at_ns0], [ping -q -c 3 -i 0.3 -W 2 -I 10.1.1.1 10.1.1.100 | FORMAT_PING], [0], [dnl
+3 packets transmitted, 3 received, 0% packet loss, time 0ms
+])
+NS_CHECK_EXEC([at_ns0], [ping -s 1200 -q -c 3 -i 0.3 -W 2 -I 10.1.1.1 10.1.1.100 | FORMAT_PING], [0], [dnl
+3 packets transmitted, 3 received, 0% packet loss, time 0ms
+])
+NS_CHECK_EXEC([at_ns0], [ping6 -s 1000 -q -c 3 -i 0.3 -W 2 -I fd00::1 fd00::100 | FORMAT_PING], [0], [dnl
+3 packets transmitted, 3 received, 0% packet loss, time 0ms
+])
+
+dnl Traffic must have been encrypted in both directions.
+NS_CHECK_EXEC([at_ns0], [ip -s xfrm state list | grep -c 'lifetime current'], [0], [2
+])
+AT_CHECK([ovs-appctl coverage/read-counter esp_rx_auth_failed], [0], [0
+])
+
+OVS_TRAFFIC_VSWITCHD_STOP
+])
+
+AT_SETUP([datapath - ping over esp tunnel])
+ESP_TUNNEL_TEST([], [replay-window 64])
+AT_CLEANUP
+
+AT_SETUP([datapath - ping over esp tunnel with extended sequence numbers])
+ESP_TUNNEL_TEST([options:esp_esn=true], [flag esn replay-window 64])
+AT_CLEANUP
+
AT_SETUP([datapath - ping6 over srv6 tunnel])
OVS_CHECK_SRV6()
@@ -333,6 +333,15 @@ m4_define([OVS_CHECK_SRV6],
AT_CHECK([ip -6 route del fc00::1/96 2>&1 >/dev/null])
OVS_CHECK_FIREWALL()])
+# OVS_CHECK_ESP()
+#
+# ESP tunnels need OpenSSL, and the tests check interoperability with the
+# kernel's IPsec implementation.
+m4_define([OVS_CHECK_ESP],
+ [AT_SKIP_IF([test "$HAVE_OPENSSL" = no])
+ AT_SKIP_IF([! ip xfrm state list > /dev/null 2>&1])
+ OVS_CHECK_FIREWALL()])
+
# CHECK_LATER_IPV6_FRAGMENTS()
#
# Userspace is parsing later IPv6 fragments correctly.
@@ -88,6 +88,60 @@ AT_CHECK([ovs-ofctl parse-pcap p0.pcap | tail -n 2 | grep -o 'ipv6_label=0x[[0-9
OVS_VSWITCHD_STOP
AT_CLEANUP
+AT_SETUP([tunnel_push_pop_ipv6 - esp])
+AT_KEYWORDS([ipsec])
+AT_SKIP_IF([test "$HAVE_OPENSSL" = no])
+
+OVS_VSWITCHD_START([add-port br0 p0 -- set Interface p0 type=dummy dnl
+ ofport_request=1 other-config:hwaddr=aa:55:aa:55:00:00 dnl
+ options:tx_pcap=p0.pcap])
+AT_CHECK([ovs-vsctl add-br int-br -- set bridge int-br datapath_type=dummy])
+AT_CHECK([ovs-vsctl add-port int-br t1 -- set Interface t1 type=esp dnl
+ ofport_request=2 options:remote_ip=2001:cafe::92 dnl
+ options:esp_in_spi=4096 options:esp_out_spi=8192 dnl
+ options:esp_in_key=0x101112131415161718191a1b1c1d1e1fcafebabe dnl
+ options:esp_out_key=0x202122232425262728292a2b2c2d2e2fdeadbeef dnl
+ -- add-port int-br p1 -- set Interface p1 type=dummy dnl
+ ofport_request=3 options:tx_pcap=p1.pcap])
+
+AT_CHECK([ovs-appctl netdev-dummy/ip6addr br0 2001:cafe::88/24], [0], [OK
+])
+AT_CHECK([ovs-appctl tnl/neigh/set br0 2001:cafe::92 aa:55:aa:55:00:01], [0], [OK
+])
+AT_CHECK([ovs-ofctl add-flow br0 action=normal])
+AT_CHECK([ovs-ofctl add-flow int-br in_port=t1,action=p1])
+AT_CHECK([ovs-ofctl add-flow int-br in_port=p1,action=t1])
+
+dnl Encapsulation of an IPv6 packet.
+AT_CHECK([ovs-appctl ofproto/trace --names int-br dnl
+ in_port=p1,dl_src=f8:bc:12:44:34:b6,dl_dst=aa:55:aa:55:00:00,dnl
+udp6,ipv6_src=2001:beef::1,ipv6_dst=2001:beef::2,nw_ttl=64,udp_src=1,udp_dst=2],
+ [0], [stdout])
+AT_CHECK([tail -1 stdout], [0], [dnl
+Datapath actions: pop_eth,tnl_push(tnl_port(esp_sys),header(size=70,type=113,eth(dst=aa:55:aa:55:00:01,src=aa:55:aa:55:00:00,dl_type=0x86dd),ipv6(src=2001:cafe::88,dst=2001:cafe::92,label=0,proto=50,tclass=0x0,hlimit=64),esp(spi=0x2000)),out_port(br0)),p0
+])
+
+AT_CHECK([ovs-appctl netdev-dummy/receive p1 dnl
+ 'eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x86dd),ipv6(src=2001:beef::1,dst=2001:beef::2,label=0,proto=17,tclass=0x0,hlimit=64,frag=no),udp(src=1,dst=2)'])
+OVS_WAIT_UNTIL([test $(ovs-pcap p0.pcap | wc -l) -ge 1])
+
+dnl Only check the headers before the random IV. The 112-byte inner packet
+dnl becomes 16 + 112 + 2 (padding) + 2 + 16 bytes of ESP.
+AT_CHECK([ovs-pcap p0.pcap | cut -c1-124], [0], [dnl
+aa55aa550001aa55aa55000086dd60000000009432402001cafe0000000000000000000000882001cafe0000000000000000000000920000200000000001
+])
+
+dnl Decapsulation of a packet encrypted by an independent implementation.
+AT_CHECK([ovs-appctl netdev-dummy/receive p0 dnl
+aa55aa550000aa55aa55000186dd60000000004c32402001cafe0000000000000000000000922001cafe00000000000000000000008800001000000000010102030405060708319779ecb7385859a48de241704608a2b76f467926ada1846103e4f0c011b294f83a80b9ba98e1cfbfd7b9421e33c5e1a7d8e84de7b0d2a39cfc4887])
+OVS_WAIT_UNTIL([test $(ovs-pcap p1.pcap | wc -l) -ge 1])
+AT_CHECK([ovs-pcap p1.pcap], [0], [dnl
+00000000000000000000000008004500002800004000401126c30a0000020a00000100c80064001400006f76732d6573702d74657374
+])
+
+OVS_VSWITCHD_STOP
+AT_CLEANUP
+
AT_SETUP([tunnel_push_pop_ipv6 - ip6gre])
OVS_VSWITCHD_START([add-port br0 p0 -- set Interface p0 type=dummy ofport_request=1 other-config:hwaddr=aa:55:aa:55:00:00])
@@ -1616,3 +1616,101 @@ AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(int-br),dnl
OVS_VSWITCHD_STOP
AT_CLEANUP
+
+AT_SETUP([tunnel_push_pop - esp])
+AT_KEYWORDS([ipsec])
+AT_SKIP_IF([test "$HAVE_OPENSSL" = no])
+
+OVS_VSWITCHD_START([add-port br0 p0 -- set Interface p0 type=dummy dnl
+ ofport_request=1 other-config:hwaddr=aa:55:aa:55:00:00 dnl
+ options:tx_pcap=p0.pcap])
+AT_CHECK([ovs-vsctl add-br int-br -- set bridge int-br datapath_type=dummy])
+AT_CHECK([ovs-vsctl add-port int-br t1 -- set Interface t1 type=esp dnl
+ ofport_request=2 options:remote_ip=1.1.2.92 dnl
+ options:esp_in_spi=4096 options:esp_out_spi=8192 dnl
+ options:esp_in_key=0x101112131415161718191a1b1c1d1e1fcafebabe dnl
+ options:esp_out_key=0x202122232425262728292a2b2c2d2e2fdeadbeef dnl
+ -- add-port int-br p1 -- set Interface p1 type=dummy dnl
+ ofport_request=3 options:tx_pcap=p1.pcap])
+
+dnl The keys are secret and are not shown.
+AT_CHECK([ovs-appctl dpif/show | grep t1 | sed 's|2/[[0-9]]*|2/X|'], [0], [dnl
+ t1 2/X: (esp: esp_in_spi=4096, esp_out_spi=8192, remote_ip=1.1.2.92)
+])
+
+AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 1.1.2.88/24], [0], [OK
+])
+AT_CHECK([ovs-appctl tnl/neigh/set br0 1.1.2.92 aa:55:aa:55:00:01], [0], [OK
+])
+AT_CHECK([ovs-ofctl add-flow br0 action=normal])
+AT_CHECK([ovs-ofctl add-flow int-br in_port=t1,action=p1])
+AT_CHECK([ovs-ofctl add-flow int-br in_port=p1,action=t1])
+
+AT_CHECK([ovs-appctl tnl/ports/show | grep esp | sed 's/([[0-9]]*)/(X)/'],
+ [0], [dnl
+esp_sys (X) ref_cnt=1
+])
+
+dnl Encapsulation. The ESP tunnel carries IP packets, so the Ethernet
+dnl header is removed first.
+AT_CHECK([ovs-appctl ofproto/trace --names int-br dnl
+ in_port=p1,dl_src=f8:bc:12:44:34:b6,dl_dst=aa:55:aa:55:00:00,dnl
+udp,nw_src=10.0.0.1,nw_dst=10.0.0.2,nw_ttl=64,udp_src=100,udp_dst=200],
+ [0], [stdout])
+AT_CHECK([tail -1 stdout], [0], [dnl
+Datapath actions: pop_eth,tnl_push(tnl_port(esp_sys),header(size=50,type=113,eth(dst=aa:55:aa:55:00:01,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=50,tos=0,ttl=64,frag=0x4000),esp(spi=0x2000)),out_port(br0)),p0
+])
+
+m4_define([INNER_FLOW], [eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=10.0.0.1,dst=10.0.0.2,proto=17,tos=0,ttl=64,frag=no),udp(src=100,dst=200)])
+AT_CHECK([ovs-appctl netdev-dummy/receive p1 'INNER_FLOW'])
+AT_CHECK([ovs-appctl netdev-dummy/receive p1 'INNER_FLOW'])
+OVS_WAIT_UNTIL([test $(ovs-pcap p0.pcap | wc -l) -ge 2])
+
+dnl The IV is random, so only check the headers before it: the outer IP
+dnl header for 148 bytes of ESP, then the SPI and sequence numbers 1 and 2.
+dnl The 92-byte inner packet becomes 16 + 92 + 2 (padding) + 2 + 16 bytes.
+AT_CHECK([ovs-pcap p0.pcap | cut -c1-84], [0], [dnl
+aa55aa550001aa55aa5500000800450000940000400040323383010102580101025c0000200000000001
+aa55aa550001aa55aa5500000800450000940000400040323383010102580101025c0000200000000002
+])
+AT_CHECK([ovs-pcap p0.pcap | awk '{print length($0) / 2}'], [0], [162
+162
+])
+
+dnl Decapsulation. These packets from 1.1.2.92 were encrypted by an
+dnl independent implementation, with sequence numbers 1 and 2. Each carries a
+dnl UDP packet from 10.0.0.2 to 10.0.0.1.
+m4_define([ESP_SEQ1], [aa55aa550000aa55aa55000108004500006000004000403233b70101025c0101025800001000000000010102030405060708319779ecb7385859a48de241704608a2b76f467926ada1846103e4f0c011b294f83a80b9ba98e1cfbfd7b9421e33c5e1a7d8e84de7b0d2a39cfc4887])
+m4_define([ESP_SEQ2], [aa55aa550000aa55aa55000108004500006000004000403233b70101025c0101025800001000000000020102030405060708319779ecb7385859a48de241704608a2b76f467926ada1846103e4f0c011b294f83a80b9ba98e1cfbfd7b9423b6e79973c638391ccc22afb834ad3c0])
+m4_define([INNER_PKT], [00000000000000000000000008004500002800004000401126c30a0000020a00000100c80064001400006f76732d6573702d74657374])
+
+AT_CHECK([ovs-appctl netdev-dummy/receive p0 ESP_SEQ1])
+OVS_WAIT_UNTIL([test $(ovs-pcap p1.pcap | wc -l) -ge 1])
+
+dnl A replayed packet is dropped, and so is a modified one.
+AT_CHECK([ovs-appctl netdev-dummy/receive p0 ESP_SEQ1])
+AT_CHECK([ovs-appctl netdev-dummy/receive p0 dnl
+ $(echo ESP_SEQ2 | sed 's/0$/1/')])
+AT_CHECK([ovs-appctl netdev-dummy/receive p0 ESP_SEQ2])
+OVS_WAIT_UNTIL([test $(ovs-pcap p1.pcap | wc -l) -ge 2])
+AT_CHECK([ovs-pcap p1.pcap], [0], [dnl
+INNER_PKT
+INNER_PKT
+])
+AT_CHECK([ovs-appctl coverage/read-counter esp_rx_replay], [0], [1
+])
+AT_CHECK([ovs-appctl coverage/read-counter esp_rx_auth_failed], [0], [1
+])
+
+dnl Datapath flows for both directions. The tunnel ID of received packets
+dnl is the SPI. The first packet of each flow was an upcall.
+AT_CHECK([ovs-appctl dpctl/dump-flows | strip_used | sort | dnl
+ grep 'proto=50\|tun_id' | sed 's/([[0-9]]*)/(X)/g;s/,[[0-9]]*$/,X/'],
+ [0], [dnl
+recirc_id(X),in_port(X),packet_type(ns=0,id=0),eth(src=aa:55:aa:55:00:01,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(dst=1.1.2.88,proto=50,frag=no), packets:3, bytes:330, used:0.0s, actions:tnl_pop(X)
+recirc_id(X),in_port(X),packet_type(ns=0,id=0),eth_type(0x0800),ipv4(tos=0/0x3,frag=no), packets:1, bytes:106, used:0.0s, actions:pop_eth,tnl_push(tnl_port(X),header(size=50,type=113,eth(dst=aa:55:aa:55:00:01,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=50,tos=0,ttl=64,frag=0x4000),esp(spi=0x2000)),out_port(X)),X
+recirc_id(X),tunnel(tun_id=0x1000,src=1.1.2.92,dst=1.1.2.88,flags(+key)),in_port(X),packet_type(ns=1,id=0x800),eth_type(0x0800),ipv4(frag=no), packets:1, bytes:40, used:0.0s, actions:push_eth(src=00:00:00:00:00:00,dst=00:00:00:00:00:00),X
+])
+
+OVS_VSWITCHD_STOP
+AT_CLEANUP
@@ -1350,6 +1350,90 @@ AT_CHECK([tail -1 stdout], [0],
OVS_VSWITCHD_STOP
AT_CLEANUP
+AT_SETUP([tunnel - ESP configuration])
+AT_KEYWORDS([ipsec])
+AT_SKIP_IF([test "$HAVE_OPENSSL" = no])
+m4_define([KEY1], [0x101112131415161718191a1b1c1d1e1fcafebabe])
+m4_define([KEY2], [0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1fdeadbeef])
+OVS_VSWITCHD_START([add-port br0 p1 -- set Interface p1 type=esp dnl
+ options:remote_ip=1.1.1.1 options:esp_in_spi=0x1000 dnl
+ options:esp_out_spi=8192 options:esp_in_key=KEY1 dnl
+ options:esp_out_key=KEY2 options:esp_esn=true dnl
+ options:esp_replay_window=1024 ofport_request=1])
+
+AT_CHECK([ovs-appctl dpif/show | grep p1 | sed 's|1/[[0-9]]*|1/X|'], [0], [dnl
+ p1 1/X: (esp: esp_esn=true, esp_in_spi=4096, esp_out_spi=8192, esp_replay_window=1024, remote_ip=1.1.1.1)
+])
+dnl The tunnel IDs are the SPIs.
+AT_CHECK([ovs-appctl ofproto/list-tunnels | sed 's/port[[ =]][[0-9]]*/port X/g'],
+ [0], [dnl
+port X: p1 (esp: ::->1.1.1.1, key=0x1000, legacy_l3, dp port X, out_key=0x2000, ttl=64)
+])
+
+dnl Configuration errors.
+m4_define([ADD_ESP], [ovs-vsctl add-port br0 $1 -- set Interface $1 type=esp dnl
+ options:remote_ip=1.1.1.2 $2])
+m4_define([CHECK_ERROR], [dnl
+AT_CHECK([ADD_ESP([$1], [$2])], [65], [], [ignore])
+AT_CHECK([grep "$1: $3" ovs-vswitchd.log | sed 's/^.*|WARN|//'], [0], [$1: $3
+])
+AT_CHECK([ovs-vsctl del-port br0 $1])])
+
+CHECK_ERROR([p2], [options:esp_out_spi=8192 options:esp_in_key=KEY1 dnl
+ options:esp_out_key=KEY1],
+ [esp tunnels require 'esp_in_spi' between 256 and 4294967295])
+CHECK_ERROR([p3], [options:esp_in_spi=255 options:esp_out_spi=8192 dnl
+ options:esp_in_key=KEY1 options:esp_out_key=KEY1],
+ [esp tunnels require 'esp_in_spi' between 256 and 4294967295])
+CHECK_ERROR([p4], [options:esp_in_spi=4097 options:esp_out_spi=8192 dnl
+ options:esp_out_key=KEY1],
+ [bad 'esp_in_key': missing])
+CHECK_ERROR([p5], [options:esp_in_spi=4097 options:esp_out_spi=8192 dnl
+ options:esp_in_key=KEY1 options:esp_out_key=0x1234],
+ [bad 'esp_out_key': key must be 20, 28 or 36 bytes (an AES-128, -192 or -256 key followed by a 4-byte salt), got 4 hex digits])
+CHECK_ERROR([p6], [options:esp_in_spi=4096 options:esp_out_spi=8192 dnl
+ options:esp_in_key=KEY1 options:esp_out_key=KEY1],
+ ['esp_in_spi' 4096 is already used by another esp tunnel])
+CHECK_ERROR([p7], [options:esp_in_spi=4097 options:esp_out_spi=8192 dnl
+ options:esp_in_key=KEY1 options:esp_out_key=KEY1 dnl
+ options:key=5],
+ [esp tunnels do not support 'key', 'in_key' or 'out_key', the tunnel ID is the inbound SPI])
+CHECK_ERROR([p8], [options:esp_in_spi=4097 options:esp_out_spi=8192 dnl
+ options:esp_in_key=KEY1 options:esp_out_key=KEY1 dnl
+ options:esp_esn=true options:esp_replay_window=0],
+ ['esp_replay_window' must be between 1 and 4096])
+CHECK_ERROR([p10], [options:esp_in_spi=4097 options:esp_out_spi=8192 dnl
+ options:esp_in_key=KEY1 options:esp_out_key=KEY1 dnl
+ options:esp_replay_window=65536],
+ ['esp_replay_window' must be between 0 and 4096])
+
+dnl The same outbound SPI is fine towards another peer, but not towards the
+dnl same one.
+AT_CHECK([ADD_ESP([p9], [options:esp_in_spi=4097 options:esp_out_spi=8192 dnl
+ options:esp_in_key=KEY1 options:esp_out_key=KEY1])])
+AT_CHECK([ovs-vsctl set Interface p9 options:remote_ip=1.1.1.1], [0], [],
+ [ignore])
+AT_CHECK([grep "p9: 'esp_out_spi' 8192 is already used by another esp tunnel to the same 'remote_ip'" ovs-vswitchd.log], [0], [ignore])
+
+dnl Changing the SPIs and keys of an existing tunnel.
+AT_CHECK([ovs-vsctl set Interface p1 options:esp_in_spi=4098 dnl
+ options:esp_in_key=KEY2])
+AT_CHECK([ovs-vsctl set Interface p9 options:remote_ip=1.1.1.2 dnl
+ options:esp_in_spi=4096])
+AT_CHECK([ovs-appctl dpif/show | grep 'p[[19]] ' | sed 's|[[0-9]]*/[[0-9]]*:|X/X:|'],
+ [0], [dnl
+ p1 X/X: (esp: esp_esn=true, esp_in_spi=4098, esp_out_spi=8192, esp_replay_window=1024, remote_ip=1.1.1.1)
+ p9 X/X: (esp: esp_in_spi=4096, esp_out_spi=8192, remote_ip=1.1.1.2)
+])
+
+OVS_VSWITCHD_STOP(["/could not set configuration/d
+/: esp tunnels require/d
+/: bad 'esp_/d
+/is already used by another esp tunnel/d
+/: esp tunnels do not support/d
+/: 'esp_replay_window' must be/d"])
+AT_CLEANUP
+
AT_SETUP([tunnel - Geneve metadata mirror])
OVS_VSWITCHD_START([add-port br0 p1 -- set Interface p1 type=geneve \
options:remote_ip=1.1.1.1 ofport_request=1 \
@@ -2923,6 +2923,21 @@
</p>
</dd>
+ <dt><code>esp</code></dt>
+ <dd>
+ <p>
+ An IPsec tunnel that encapsulates L3 traffic, IPv4 or IPv6, with
+ the Encapsulating Security Payload (ESP) in tunnel mode, as
+ defined in RFC 4303, over IPv4 or IPv6. Packets are encrypted
+ and authenticated with AES-GCM (RFC 4106) by the datapath
+ itself, so the tunnel is only supported by the userspace
+ datapath, e.g. with DPDK, and requires Open vSwitch to be built
+ with OpenSSL. The security associations are configured with
+ ESP specific options. It interoperates with other IPsec
+ implementations, such as the Linux kernel's, in tunnel mode.
+ </p>
+ </dd>
+
</dl>
</column>
</group>
@@ -2931,7 +2946,8 @@
<p>
These options apply to interfaces with <ref column="type"/> of
<code>geneve</code>, <code>bareudp</code>, <code>gre</code>,
- <code>ip6gre</code>, <code>vxlan</code>, and <code>srv6</code>.
+ <code>ip6gre</code>, <code>vxlan</code>, <code>srv6</code>, and
+ <code>esp</code>.
</p>
<p>
@@ -2945,7 +2961,8 @@
a port defines one and another port defines the other.
<ref column="options" key="in_key"/> is not applicable for bareudp
and srv6 tunnels. Hence it is not considered while identifying
- bareudp or srv6 tunnels.
+ bareudp or srv6 tunnels. For esp tunnels, the key is the inbound SPI,
+ see <ref column="options" key="esp_in_spi"/>.
</p>
<column name="options" key="remote_ip">
@@ -3013,9 +3030,9 @@
<column name="options" key="in_key">
<p>
- Optional, not applicable for <code>bareudp</code> and
- <code>srv6</code>. The key that received packets must contain,
- one of:
+ Optional, not applicable for <code>bareudp</code>,
+ <code>srv6</code> and <code>esp</code>. The key that received
+ packets must contain, one of:
</p>
<ul>
@@ -3043,9 +3060,9 @@
<column name="options" key="out_key">
<p>
- Optional, not applicable for <code>bareudp</code> and
- <code>srv6</code>. The key to be set on outgoing packets,
- one of:
+ Optional, not applicable for <code>bareudp</code>,
+ <code>srv6</code> and <code>esp</code>. The key to be set on outgoing
+ packets, one of:
</p>
<ul>
@@ -3352,6 +3369,91 @@
</column>
</group>
+ <group title="Tunnel Options: esp only">
+ <p>
+ These options configure the two security associations (SAs) of an
+ <code>esp</code> tunnel: the inbound SA, for packets received from
+ <ref column="options" key="remote_ip"/>, and the outbound SA, for
+ packets sent to it. The peer must be configured with the same keys,
+ with inbound and outbound swapped. <ref column="options"
+ key="remote_ip"/> and <ref column="options" key="local_ip"/> cannot be
+ <code>flow</code>.
+ </p>
+
+ <p>
+ These options set the keys manually, which RFC 4301 permits but which
+ has limitations: the keys are never changed, and when either end
+ restarts, its sequence numbers start again from 1, so the anti-replay
+ window of the other end drops its packets until that end restarts too
+ or the SAs are reconfigured with new SPIs or keys. With manual keys,
+ Open vSwitch uses a random initialization vector base for each SA, so
+ that a restart does not reuse the GCM nonces.
+ </p>
+
+ <column name="options" key="esp_in_spi">
+ <p>
+ Required. The Security Parameters Index (SPI) of received packets,
+ an integer between 256 and 4294967295, in decimal or, with a
+ <code>0x</code> prefix, hexadecimal. It must differ from the
+ <ref column="options" key="esp_in_spi"/> of every other
+ <code>esp</code> tunnel. Received packets have their SPI in the
+ <code>tun_id</code> field for matching in the flow table.
+ </p>
+ </column>
+
+ <column name="options" key="esp_out_spi">
+ <p>
+ Required. The SPI of sent packets, in the same format as
+ <ref column="options" key="esp_in_spi"/>. Two <code>esp</code>
+ tunnels with the same <ref column="options" key="remote_ip"/> must
+ have different values.
+ </p>
+ </column>
+
+ <column name="options" key="esp_in_key">
+ <p>
+ Required. The key for received packets: an AES-128, AES-192 or
+ AES-256 key followed by a 4-byte salt, that is 20, 28 or 36 bytes, as
+ hexadecimal digits with an optional <code>0x</code> prefix. This is
+ the format of the key of the Linux <code>ip xfrm state</code>
+ command with <code>aead 'rfc4106(gcm(aes))' <var>key</var>
+ 128</code>. The integrity check value is always 16 bytes.
+ </p>
+ <p>
+ Anyone with read access to the database can read this key.
+ </p>
+ </column>
+
+ <column name="options" key="esp_out_key">
+ <p>
+ Required. The key for sent packets, in the same format as
+ <ref column="options" key="esp_in_key"/>.
+ </p>
+ </column>
+
+ <column name="options" key="esp_esn" type='{"type": "boolean"}'>
+ <p>
+ Optional. If <code>true</code>, use 64-bit Extended Sequence Numbers
+ (ESN). Otherwise, sequence numbers are 32 bits long, and a tunnel
+ stops sending packets after 2**32 - 1 of them, which happens in
+ minutes at high packet rates. Both ends must agree. Defaults to
+ <code>false</code>.
+ </p>
+ </column>
+
+ <column name="options" key="esp_replay_window"
+ type='{"type": "integer", "minInteger": 0, "maxInteger": 4096}'>
+ <p>
+ Optional. The size, in packets, of the anti-replay window of the
+ inbound SA, rounded up to a multiple of 64. Received packets
+ that are older than the window, or that were already received, are
+ dropped. <code>0</code> disables the anti-replay check, which is
+ not allowed with <ref column="options" key="esp_esn"/>. Defaults
+ to 64.
+ </p>
+ </column>
+ </group>
+
<group title="Patch Options">
<p>
These options apply only to <dfn>patch ports</dfn>, that is, interfaces
Add an 'esp' tunnel type to the userspace datapath. It carries IPv4 and IPv6 packets in IPsec ESP tunnel mode (RFC 4303), over IPv4 or IPv6, and the datapath threads encrypt and decrypt them with AES-GCM (RFC 4106). This gives OVS-DPDK IPsec without involving the kernel, whose IPsec support only applies to the kernel datapath. $ ovs-vsctl add-port br0 esp0 -- set int esp0 type=esp \ options:remote_ip=172.31.1.1 \ options:esp_in_spi=0x1000 options:esp_out_spi=0x2000 \ options:esp_in_key=0x<key><salt> options:esp_out_key=0x<key><salt> The keys are configured manually for now, in the format of 'ip xfrm state ... aead rfc4106(gcm(aes))'. Optional settings are Extended Sequence Numbers and the anti-replay window size. Like SRv6 and GTP-U, the tunnel carries L3 packets: the Ethernet header is popped before encapsulation, and an empty one is pushed after decapsulation. The tunnel ID of received packets is their SPI, which also identifies the receiving tunnel port. Encapsulation is the usual tnl_push() action with an ESP header. The datapath executes it on the 'esp_sys' port shared by all esp tunnels, so it finds the outbound security association from the SPI and destination in the pushed header. Decapsulation is triggered by tnl_pop() for ESP packets to a local address, and finds the inbound security association from the SPI. Packets that fail authentication or the anti-replay check are dropped and counted in coverage counters. Restrictions: no key exchange (IKE), no NAT traversal (UDP encapsulation), no fragmentation or reassembly of ESP packets, and TSO packets are dropped when userspace TSO is enabled. Tests: - tunnel-push-pop: encapsulation, and decapsulation of packets encrypted by an independent implementation, over IPv4 and IPv6, including replayed and tampered packets. - tunnel: configuration, errors, and changes of SPIs and keys. - system-userspace: ping and ping6 between OVS and the Linux kernel's IPsec, with and without ESN. Assisted-by: claude-opus-5-5, Claude Code Signed-off-by: Thilak Raj Surendra Babu <s.thilakraj@gmail.com> --- Documentation/faq/configuration.rst | 45 +++++ Documentation/faq/releases.rst | 1 + NEWS | 4 + include/linux/openvswitch.h | 1 + lib/dpif-netlink-rtnl.c | 4 + lib/dpif-netlink.c | 5 + lib/netdev-native-tnl.c | 119 ++++++++++++++ lib/netdev-native-tnl.h | 11 ++ lib/netdev-vport-private.h | 7 + lib/netdev-vport.c | 247 +++++++++++++++++++++++++++- lib/netdev.h | 6 + lib/odp-util.c | 21 +++ lib/tnl-ports.c | 4 +- ofproto/ofproto-dpif-xlate.c | 3 + python/ovs/flow/odp.py | 7 + python/ovs/tests/test_odp.py | 15 ++ tests/odp.at | 4 + tests/system-kmod-macros.at | 8 + tests/system-traffic.at | 88 ++++++++++ tests/system-userspace-macros.at | 9 + tests/tunnel-push-pop-ipv6.at | 54 ++++++ tests/tunnel-push-pop.at | 98 +++++++++++ tests/tunnel.at | 84 ++++++++++ vswitchd/vswitch.xml | 118 ++++++++++++- 24 files changed, 951 insertions(+), 12 deletions(-)