aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet
diff options
context:
space:
mode:
Diffstat (limited to 'src/vnet')
-rw-r--r--src/vnet/devices/dpdk/ipsec/esp_decrypt.c2
-rw-r--r--src/vnet/devices/dpdk/ipsec/esp_encrypt.c2
-rw-r--r--src/vnet/ipsec/esp_decrypt.c2
-rw-r--r--src/vnet/ipsec/esp_encrypt.c2
-rw-r--r--src/vnet/ipsec/ikev2.c1548
-rw-r--r--src/vnet/ipsec/ikev2.h25
-rw-r--r--src/vnet/ipsec/ikev2_cli.c125
-rw-r--r--src/vnet/ipsec/ikev2_crypto.c149
-rw-r--r--src/vnet/ipsec/ikev2_payload.c19
-rw-r--r--src/vnet/ipsec/ikev2_priv.h63
-rw-r--r--src/vnet/ipsec/ipsec.api228
-rw-r--r--src/vnet/ipsec/ipsec.h3
-rw-r--r--src/vnet/ipsec/ipsec_api.c198
13 files changed, 2180 insertions, 186 deletions
diff --git a/src/vnet/devices/dpdk/ipsec/esp_decrypt.c b/src/vnet/devices/dpdk/ipsec/esp_decrypt.c
index 53b2d122e9f..760076093cb 100644
--- a/src/vnet/devices/dpdk/ipsec/esp_decrypt.c
+++ b/src/vnet/devices/dpdk/ipsec/esp_decrypt.c
@@ -174,6 +174,8 @@ dpdk_esp_decrypt_node_fn (vlib_main_t * vm,
}
}
+ sa0->total_data_size += b0->current_length;
+
if (PREDICT_FALSE(sa0->integ_alg == IPSEC_INTEG_ALG_NONE) ||
PREDICT_FALSE(sa0->crypto_alg == IPSEC_CRYPTO_ALG_NONE))
{
diff --git a/src/vnet/devices/dpdk/ipsec/esp_encrypt.c b/src/vnet/devices/dpdk/ipsec/esp_encrypt.c
index b6f000048f9..6eb1afc9501 100644
--- a/src/vnet/devices/dpdk/ipsec/esp_encrypt.c
+++ b/src/vnet/devices/dpdk/ipsec/esp_encrypt.c
@@ -177,6 +177,8 @@ dpdk_esp_encrypt_node_fn (vlib_main_t * vm,
goto trace;
}
+ sa0->total_data_size += b0->current_length;
+
sa_sess = pool_elt_at_index (cwm->sa_sess_d[1], sa_index0);
if (PREDICT_FALSE (!sa_sess->sess))
{
diff --git a/src/vnet/ipsec/esp_decrypt.c b/src/vnet/ipsec/esp_decrypt.c
index e69cd85101a..7289b260e22 100644
--- a/src/vnet/ipsec/esp_decrypt.c
+++ b/src/vnet/ipsec/esp_decrypt.c
@@ -190,6 +190,8 @@ esp_decrypt_node_fn (vlib_main_t * vm,
}
}
+ sa0->total_data_size += i_b0->current_length;
+
if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
{
u8 sig[64];
diff --git a/src/vnet/ipsec/esp_encrypt.c b/src/vnet/ipsec/esp_encrypt.c
index 7b7f9b9c4c7..44ae22972af 100644
--- a/src/vnet/ipsec/esp_encrypt.c
+++ b/src/vnet/ipsec/esp_encrypt.c
@@ -182,6 +182,8 @@ esp_encrypt_node_fn (vlib_main_t * vm,
goto trace;
}
+ sa0->total_data_size += i_b0->current_length;
+
/* grab free buffer */
last_empty_buffer = vec_len (empty_buffers) - 1;
o_bi0 = empty_buffers[last_empty_buffer];
diff --git a/src/vnet/ipsec/ikev2.c b/src/vnet/ipsec/ikev2.c
index 5a6c3674477..092093346fd 100644
--- a/src/vnet/ipsec/ikev2.c
+++ b/src/vnet/ipsec/ikev2.c
@@ -21,6 +21,7 @@
#include <vnet/ipsec/ipsec.h>
#include <vnet/ipsec/ikev2.h>
#include <vnet/ipsec/ikev2_priv.h>
+#include <openssl/sha.h>
static int ikev2_delete_tunnel_interface (vnet_main_t * vnm,
ikev2_sa_t * sa,
@@ -202,13 +203,15 @@ ikev2_sa_get_td_for_type (ikev2_sa_proposal_t * p,
}
ikev2_child_sa_t *
-ikev2_sa_get_child (ikev2_sa_t * sa, u32 spi, ikev2_protocol_id_t prot_id)
+ikev2_sa_get_child (ikev2_sa_t * sa, u32 spi, ikev2_protocol_id_t prot_id,
+ int by_initiator)
{
ikev2_child_sa_t *c;
vec_foreach (c, sa->childs)
{
- if (c->i_proposals[0].spi == spi
- && c->i_proposals[0].protocol_id == prot_id)
+ ikev2_sa_proposal_t *proposal =
+ by_initiator ? &c->i_proposals[0] : &c->r_proposals[0];
+ if (proposal && proposal->spi == spi && proposal->protocol_id == prot_id)
return c;
}
@@ -271,6 +274,7 @@ ikev2_sa_free_all_vec (ikev2_sa_t * sa)
vec_free (sa->i_nonce);
vec_free (sa->i_dh_data);
vec_free (sa->dh_shared_key);
+ vec_free (sa->dh_private_key);
ikev2_sa_free_proposal_vector (&sa->r_proposals);
ikev2_sa_free_proposal_vector (&sa->i_proposals);
@@ -341,15 +345,84 @@ ikev2_generate_sa_init_data (ikev2_sa_t * sa)
return;
}
- /* generate rspi */
- RAND_bytes ((u8 *) & sa->rspi, 8);
+ if (sa->is_initiator)
+ {
+ /* generate rspi */
+ RAND_bytes ((u8 *) & sa->ispi, 8);
+
+ /* generate nonce */
+ sa->i_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
+ RAND_bytes ((u8 *) sa->i_nonce, IKEV2_NONCE_SIZE);
+ }
+ else
+ {
+ /* generate rspi */
+ RAND_bytes ((u8 *) & sa->rspi, 8);
- /* generate nonce */
- sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
- RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
+ /* generate nonce */
+ sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
+ RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
+ }
/* generate dh keys */
ikev2_generate_dh (sa, t);
+
+}
+
+static void
+ikev2_complete_sa_data (ikev2_sa_t * sa, ikev2_sa_t * sai)
+{
+ ikev2_sa_transform_t *t = 0, *t2;
+ ikev2_main_t *km = &ikev2_main;
+
+
+ /*move some data to the new SA */
+#define _(A) ({void* __tmp__ = (A); (A) = 0; __tmp__;})
+ sa->i_nonce = _(sai->i_nonce);
+ sa->i_dh_data = _(sai->i_dh_data);
+ sa->dh_private_key = _(sai->dh_private_key);
+ sa->iaddr.as_u32 = sai->iaddr.as_u32;
+ sa->raddr.as_u32 = sai->raddr.as_u32;
+ sa->is_initiator = sai->is_initiator;
+ sa->profile = sai->profile;
+ sa->i_id.type = sai->i_id.type;
+ sa->i_id.data = _(sai->i_id.data);
+ sa->i_auth.method = sai->i_auth.method;
+ sa->i_auth.hex = sai->i_auth.hex;
+ sa->i_auth.data = _(sai->i_auth.data);
+ sa->i_auth.key = _(sai->i_auth.key);
+ sa->last_sa_init_req_packet_data = _(sai->last_sa_init_req_packet_data);
+ sa->childs = _(sai->childs);
+#undef _
+
+
+ if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
+ {
+ return;
+ }
+
+ /* check if received DH group is on our list of supported groups */
+ vec_foreach (t2, km->supported_transforms)
+ {
+ if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
+ {
+ t = t2;
+ break;
+ }
+ }
+
+ if (!t)
+ {
+ clib_warning ("unknown dh data group %u (data len %u)", sa->dh_group,
+ vec_len (sa->i_dh_data));
+ sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
+ return;
+ }
+
+
+ /* generate dh keys */
+ ikev2_complete_dh (sa, t);
+
}
static void
@@ -551,6 +624,86 @@ ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa,
ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
}
+static void
+ikev2_process_sa_init_resp (vlib_main_t * vm, ikev2_sa_t * sa,
+ ike_header_t * ike)
+{
+ int p = 0;
+ u32 len = clib_net_to_host_u32 (ike->length);
+ u8 payload = ike->nextpayload;
+
+ clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
+ "exchange %x flags %x msgid %x length %u",
+ clib_net_to_host_u64 (ike->ispi),
+ clib_net_to_host_u64 (ike->rspi),
+ payload, ike->version,
+ ike->exchange, ike->flags,
+ clib_net_to_host_u32 (ike->msgid), len);
+
+ sa->ispi = clib_net_to_host_u64 (ike->ispi);
+ sa->rspi = clib_net_to_host_u64 (ike->rspi);
+
+ /* store whole IKE payload - needed for PSK auth */
+ vec_free (sa->last_sa_init_res_packet_data);
+ vec_add (sa->last_sa_init_res_packet_data, ike, len);
+
+ while (p < len && payload != IKEV2_PAYLOAD_NONE)
+ {
+ ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
+ u32 plen = clib_net_to_host_u16 (ikep->length);
+
+ if (plen < sizeof (ike_payload_header_t))
+ return;
+
+ if (payload == IKEV2_PAYLOAD_SA)
+ {
+ ikev2_sa_free_proposal_vector (&sa->r_proposals);
+ sa->r_proposals = ikev2_parse_sa_payload (ikep);
+ if (sa->r_proposals)
+ {
+ ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
+ ike->msgid =
+ clib_host_to_net_u32 (clib_net_to_host_u32 (ike->msgid) + 1);
+ }
+ }
+ else if (payload == IKEV2_PAYLOAD_KE)
+ {
+ ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
+ sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
+ vec_free (sa->r_dh_data);
+ vec_add (sa->r_dh_data, ke->payload, plen - sizeof (*ke));
+ }
+ else if (payload == IKEV2_PAYLOAD_NONCE)
+ {
+ vec_free (sa->r_nonce);
+ vec_add (sa->r_nonce, ikep->payload, plen - sizeof (*ikep));
+ }
+ else if (payload == IKEV2_PAYLOAD_NOTIFY)
+ {
+ ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
+ vec_free (n);
+ }
+ else if (payload == IKEV2_PAYLOAD_VENDOR)
+ {
+ ikev2_parse_vendor_payload (ikep);
+ }
+ else
+ {
+ clib_warning ("unknown payload %u flags %x length %u", payload,
+ ikep->flags, plen);
+ if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
+ {
+ ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
+ sa->unsupported_cp = payload;
+ return;
+ }
+ }
+
+ payload = ikep->nextpayload;
+ p += plen;
+ }
+}
+
static u8 *
ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload)
{
@@ -599,8 +752,9 @@ ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload)
return 0;
}
- hmac = ikev2_calc_integr (tr_integ, sa->sk_ai, (u8 *) ike,
- len - tr_integ->key_trunc);
+ hmac =
+ ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ar : sa->sk_ai,
+ (u8 *) ike, len - tr_integ->key_trunc);
plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
@@ -683,9 +837,16 @@ ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa, ike_header_t * ike)
goto cleanup_and_exit;
}
- /* create 1st child SA */
- ikev2_sa_free_all_child_sa (&sa->childs);
- vec_add2 (sa->childs, first_child_sa, 1);
+ /* select or create 1st child SA */
+ if (sa->is_initiator)
+ {
+ first_child_sa = &sa->childs[0];
+ }
+ else
+ {
+ ikev2_sa_free_all_child_sa (&sa->childs);
+ vec_add2 (sa->childs, first_child_sa, 1);
+ }
/* process encrypted payload */
@@ -701,27 +862,54 @@ ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa, ike_header_t * ike)
if (payload == IKEV2_PAYLOAD_SA) /* 33 */
{
clib_warning ("received payload SA, len %u", plen - sizeof (*ikep));
- ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
- first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep);
+ if (sa->is_initiator)
+ {
+ ikev2_sa_free_proposal_vector (&first_child_sa->r_proposals);
+ first_child_sa->r_proposals = ikev2_parse_sa_payload (ikep);
+ }
+ else
+ {
+ ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
+ first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep);
+ }
}
- else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
+ else if (payload == IKEV2_PAYLOAD_IDI || payload == IKEV2_PAYLOAD_IDR) /* 35, 36 */
{
ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
- sa->i_id.type = id->id_type;
- vec_free (sa->i_id.data);
- vec_add (sa->i_id.data, id->payload, plen - sizeof (*id));
+ if (sa->is_initiator)
+ {
+ sa->r_id.type = id->id_type;
+ vec_free (sa->r_id.data);
+ vec_add (sa->r_id.data, id->payload, plen - sizeof (*id));
+ }
+ else
+ {
+ sa->i_id.type = id->id_type;
+ vec_free (sa->i_id.data);
+ vec_add (sa->i_id.data, id->payload, plen - sizeof (*id));
+ }
- clib_warning ("received payload IDi, len %u id_type %u",
+ clib_warning ("received payload %s, len %u id_type %u",
+ (payload == IKEV2_PAYLOAD_IDI ? "IDi" : "IDr"),
plen - sizeof (*id), id->id_type);
}
else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
{
ike_auth_payload_header_t *a = (ike_auth_payload_header_t *) ikep;
- sa->i_auth.method = a->auth_method;
- vec_free (sa->i_auth.data);
- vec_add (sa->i_auth.data, a->payload, plen - sizeof (*a));
+ if (sa->is_initiator)
+ {
+ sa->r_auth.method = a->auth_method;
+ vec_free (sa->r_auth.data);
+ vec_add (sa->r_auth.data, a->payload, plen - sizeof (*a));
+ }
+ else
+ {
+ sa->i_auth.method = a->auth_method;
+ vec_free (sa->i_auth.data);
+ vec_add (sa->i_auth.data, a->payload, plen - sizeof (*a));
+ }
clib_warning ("received payload AUTH, len %u auth_type %u",
plen - sizeof (*a), a->auth_method);
@@ -857,7 +1045,7 @@ ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa,
u8 payload = ike->nextpayload;
u8 *plaintext = 0;
u8 rekeying = 0;
- u8 i_nonce[IKEV2_NONCE_SIZE];
+ u8 nonce[IKEV2_NONCE_SIZE];
ike_payload_header_t *ikep;
u32 plen;
@@ -912,7 +1100,7 @@ ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa,
}
else if (payload == IKEV2_PAYLOAD_NONCE)
{
- clib_memcpy (i_nonce, ikep->payload, plen - sizeof (*ikep));
+ clib_memcpy (nonce, ikep->payload, plen - sizeof (*ikep));
}
else if (payload == IKEV2_PAYLOAD_TSI)
{
@@ -939,10 +1127,29 @@ ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa,
p += plen;
}
- if (rekeying)
+ if (sa->is_initiator && proposal->protocol_id == IKEV2_PROTOCOL_ESP)
+ {
+ ikev2_rekey_t *rekey = &sa->rekey[0];
+ rekey->protocol_id = proposal->protocol_id;
+ rekey->i_proposal =
+ ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
+ rekey->i_proposal->spi = rekey->spi;
+ rekey->r_proposal = proposal;
+ rekey->tsi = tsi;
+ rekey->tsr = tsr;
+ /* update Nr */
+ vec_free (sa->r_nonce);
+ vec_add (sa->r_nonce, nonce, IKEV2_NONCE_SIZE);
+ child_sa = ikev2_sa_get_child (sa, rekey->ispi, IKEV2_PROTOCOL_ESP, 1);
+ if (child_sa)
+ {
+ child_sa->rekey_retries = 0;
+ }
+ }
+ else if (rekeying)
{
ikev2_rekey_t *rekey;
- child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id);
+ child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id, 1);
if (!child_sa)
{
clib_warning ("child SA spi %lx not found", n->spi);
@@ -958,7 +1165,7 @@ ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa,
rekey->tsr = tsr;
/* update Ni */
vec_free (sa->i_nonce);
- vec_add (sa->i_nonce, i_nonce, IKEV2_NONCE_SIZE);
+ vec_add (sa->i_nonce, nonce, IKEV2_NONCE_SIZE);
/* generate new Nr */
vec_free (sa->r_nonce);
sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
@@ -1030,20 +1237,34 @@ ikev2_sa_match_ts (ikev2_sa_t * sa)
{
ikev2_main_t *km = &ikev2_main;
ikev2_profile_t *p;
- ikev2_ts_t *ts, *tsi = 0, *tsr = 0;
+ ikev2_ts_t *ts, *p_tsi, *p_tsr, *tsi = 0, *tsr = 0;
+ ikev2_id_t *id;
/* *INDENT-OFF* */
pool_foreach (p, km->profiles, ({
+ if (sa->is_initiator)
+ {
+ p_tsi = &p->loc_ts;
+ p_tsr = &p->rem_ts;
+ id = &sa->r_id;
+ }
+ else
+ {
+ p_tsi = &p->rem_ts;
+ p_tsr = &p->loc_ts;
+ id = &sa->i_id;
+ }
+
/* check id */
- if (p->rem_id.type != sa->i_id.type ||
- vec_len(p->rem_id.data) != vec_len(sa->i_id.data) ||
- memcmp(p->rem_id.data, sa->i_id.data, vec_len(p->rem_id.data)))
+ if (p->rem_id.type != id->type ||
+ vec_len(p->rem_id.data) != vec_len(id->data) ||
+ memcmp(p->rem_id.data, id->data, vec_len(p->rem_id.data)))
continue;
vec_foreach(ts, sa->childs[0].tsi)
{
- if (ikev2_ts_cmp(&p->rem_ts, ts))
+ if (ikev2_ts_cmp(p_tsi, ts))
{
tsi = vec_dup(ts);
break;
@@ -1052,7 +1273,7 @@ ikev2_sa_match_ts (ikev2_sa_t * sa)
vec_foreach(ts, sa->childs[0].tsr)
{
- if (ikev2_ts_cmp(&p->loc_ts, ts))
+ if (ikev2_ts_cmp(p_tsr, ts))
{
tsr = vec_dup(ts);
break;
@@ -1100,18 +1321,32 @@ ikev2_sa_auth (ikev2_sa_t * sa)
}
key_pad = format (0, "%s", IKEV2_KEY_PAD);
- authmsg = ikev2_sa_generate_authmsg (sa, 0);
+ authmsg = ikev2_sa_generate_authmsg (sa, sa->is_initiator);
+
+ ikev2_id_t *sa_id;
+ ikev2_auth_t *sa_auth;
+
+ if (sa->is_initiator)
+ {
+ sa_id = &sa->r_id;
+ sa_auth = &sa->r_auth;
+ }
+ else
+ {
+ sa_id = &sa->i_id;
+ sa_auth = &sa->i_auth;
+ }
/* *INDENT-OFF* */
pool_foreach (p, km->profiles, ({
/* check id */
- if (p->rem_id.type != sa->i_id.type ||
- vec_len(p->rem_id.data) != vec_len(sa->i_id.data) ||
- memcmp(p->rem_id.data, sa->i_id.data, vec_len(p->rem_id.data)))
+ if (p->rem_id.type != sa_id->type ||
+ vec_len(p->rem_id.data) != vec_len(sa_id->data) ||
+ memcmp(p->rem_id.data, sa_id->data, vec_len(p->rem_id.data)))
continue;
- if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
+ if (sa_auth->method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
{
if (!p->auth.data ||
p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
@@ -1120,7 +1355,7 @@ ikev2_sa_auth (ikev2_sa_t * sa)
psk = ikev2_calc_prf(tr_prf, p->auth.data, key_pad);
auth = ikev2_calc_prf(tr_prf, psk, authmsg);
- if (!memcmp(auth, sa->i_auth.data, vec_len(sa->i_auth.data)))
+ if (!memcmp(auth, sa_auth->data, vec_len(sa_auth->data)))
{
ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
vec_free(auth);
@@ -1129,12 +1364,12 @@ ikev2_sa_auth (ikev2_sa_t * sa)
}
}
- else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
+ else if (sa_auth->method == IKEV2_AUTH_METHOD_RSA_SIG)
{
if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
continue;
- if (ikev2_verify_sign(p->auth.key, sa->i_auth.data, authmsg) == 1)
+ if (ikev2_verify_sign(p->auth.key, sa_auth->data, authmsg) == 1)
{
ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
sel_p = p;
@@ -1151,28 +1386,32 @@ ikev2_sa_auth (ikev2_sa_t * sa)
if (sa->state == IKEV2_STATE_AUTHENTICATED)
{
- vec_free (sa->r_id.data);
- sa->r_id.data = vec_dup (sel_p->loc_id.data);
- sa->r_id.type = sel_p->loc_id.type;
-
- /* generate our auth data */
- authmsg = ikev2_sa_generate_authmsg (sa, 1);
- if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
+ if (!sa->is_initiator)
{
- sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
- sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
- }
- else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
- {
- sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
- sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
- }
- vec_free (authmsg);
+ vec_free (sa->r_id.data);
+ sa->r_id.data = vec_dup (sel_p->loc_id.data);
+ sa->r_id.type = sel_p->loc_id.type;
+
+ /* generate our auth data */
+ authmsg = ikev2_sa_generate_authmsg (sa, 1);
+ if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
+ {
+ sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
+ sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
+ }
+ else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
+ {
+ sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
+ sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
+ }
+ vec_free (authmsg);
- /* select transforms for 1st child sa */
- ikev2_sa_free_proposal_vector (&sa->childs[0].r_proposals);
- sa->childs[0].r_proposals =
- ikev2_select_proposal (sa->childs[0].i_proposals, IKEV2_PROTOCOL_ESP);
+ /* select transforms for 1st child sa */
+ ikev2_sa_free_proposal_vector (&sa->childs[0].r_proposals);
+ sa->childs[0].r_proposals =
+ ikev2_select_proposal (sa->childs[0].i_proposals,
+ IKEV2_PROTOCOL_ESP);
+ }
}
else
{
@@ -1182,12 +1421,58 @@ ikev2_sa_auth (ikev2_sa_t * sa)
vec_free (key_pad);
}
+
+static void
+ikev2_sa_auth_init (ikev2_sa_t * sa)
+{
+ ikev2_main_t *km = &ikev2_main;
+ u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
+ ikev2_sa_transform_t *tr_prf;
+
+ tr_prf =
+ ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
+
+ /* only shared key and rsa signature */
+ if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
+ sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
+ {
+ clib_warning ("unsupported authentication method %u",
+ sa->i_auth.method);
+ ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
+ return;
+ }
+
+ key_pad = format (0, "%s", IKEV2_KEY_PAD);
+ authmsg = ikev2_sa_generate_authmsg (sa, 0);
+ psk = ikev2_calc_prf (tr_prf, sa->i_auth.data, key_pad);
+ auth = ikev2_calc_prf (tr_prf, psk, authmsg);
+
+
+ if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
+ {
+ sa->i_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
+ sa->i_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
+ }
+ else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
+ {
+ sa->i_auth.data = ikev2_calc_sign (km->pkey, authmsg);
+ sa->i_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
+ }
+
+ vec_free (psk);
+ vec_free (key_pad);
+ vec_free (auth);
+ vec_free (authmsg);
+}
+
+
static int
ikev2_create_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
ikev2_child_sa_t * child)
{
ipsec_add_del_tunnel_args_t a;
ikev2_sa_transform_t *tr;
+ ikev2_sa_proposal_t *proposals;
u8 encr_type = 0;
if (!child->r_proposals)
@@ -1198,21 +1483,31 @@ ikev2_create_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
memset (&a, 0, sizeof (a));
a.is_add = 1;
- a.local_ip.as_u32 = sa->raddr.as_u32;
- a.remote_ip.as_u32 = sa->iaddr.as_u32;
- a.local_spi = child->i_proposals[0].spi;
- a.remote_spi = child->r_proposals[0].spi;
+ if (sa->is_initiator)
+ {
+ a.local_ip.as_u32 = sa->iaddr.as_u32;
+ a.remote_ip.as_u32 = sa->raddr.as_u32;
+ proposals = child->i_proposals;
+ a.local_spi = child->r_proposals[0].spi;
+ a.remote_spi = child->i_proposals[0].spi;
+ }
+ else
+ {
+ a.local_ip.as_u32 = sa->raddr.as_u32;
+ a.remote_ip.as_u32 = sa->iaddr.as_u32;
+ proposals = child->r_proposals;
+ a.local_spi = child->i_proposals[0].spi;
+ a.remote_spi = child->r_proposals[0].spi;
+ }
a.anti_replay = 1;
- tr =
- ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ESN);
+ tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
if (tr)
a.esn = tr->esn_type;
else
a.esn = 0;
- tr =
- ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
+ tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ENCR);
if (tr)
{
if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
@@ -1246,8 +1541,7 @@ ikev2_create_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
return 1;
}
- tr =
- ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
+ tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_INTEG);
if (tr)
{
if (tr->integ_type != IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96)
@@ -1264,17 +1558,44 @@ ikev2_create_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
ikev2_calc_child_keys (sa, child);
+ u8 *loc_ckey, *rem_ckey, *loc_ikey, *rem_ikey;
+ if (sa->is_initiator)
+ {
+ loc_ikey = child->sk_ai;
+ rem_ikey = child->sk_ar;
+ loc_ckey = child->sk_ei;
+ rem_ckey = child->sk_er;
+ }
+ else
+ {
+ loc_ikey = child->sk_ar;
+ rem_ikey = child->sk_ai;
+ loc_ckey = child->sk_er;
+ rem_ckey = child->sk_ei;
+ }
+
a.integ_alg = IPSEC_INTEG_ALG_SHA1_96;
- a.local_integ_key_len = vec_len (child->sk_ar);
- clib_memcpy (a.local_integ_key, child->sk_ar, a.local_integ_key_len);
- a.remote_integ_key_len = vec_len (child->sk_ai);
- clib_memcpy (a.remote_integ_key, child->sk_ai, a.remote_integ_key_len);
+ a.local_integ_key_len = vec_len (loc_ikey);
+ clib_memcpy (a.local_integ_key, loc_ikey, a.local_integ_key_len);
+ a.remote_integ_key_len = vec_len (rem_ikey);
+ clib_memcpy (a.remote_integ_key, rem_ikey, a.remote_integ_key_len);
a.crypto_alg = encr_type;
- a.local_crypto_key_len = vec_len (child->sk_er);
- clib_memcpy (a.local_crypto_key, child->sk_er, a.local_crypto_key_len);
- a.remote_crypto_key_len = vec_len (child->sk_ei);
- clib_memcpy (a.remote_crypto_key, child->sk_ei, a.remote_crypto_key_len);
+ a.local_crypto_key_len = vec_len (loc_ckey);
+ clib_memcpy (a.local_crypto_key, loc_ckey, a.local_crypto_key_len);
+ a.remote_crypto_key_len = vec_len (rem_ckey);
+ clib_memcpy (a.remote_crypto_key, rem_ckey, a.remote_crypto_key_len);
+
+ if (sa->profile && sa->profile->lifetime)
+ {
+ child->time_to_expiration = vlib_time_now (vnm->vlib_main)
+ + sa->profile->lifetime;
+ if (sa->profile->lifetime_jitter)
+ {
+ child->time_to_expiration +=
+ 1 + (rand () % sa->profile->lifetime_jitter);
+ }
+ }
ipsec_add_del_tunnel_if (&a);
@@ -1287,21 +1608,35 @@ ikev2_delete_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
{
ipsec_add_del_tunnel_args_t a;
- if (!vec_len (child->r_proposals))
- return 0;
+ if (sa->is_initiator)
+ {
+ if (!vec_len (child->i_proposals))
+ return 0;
- a.is_add = 0;
- a.local_ip.as_u32 = sa->raddr.as_u32;
- a.remote_ip.as_u32 = sa->iaddr.as_u32;
- a.local_spi = child->i_proposals[0].spi;
- a.remote_spi = child->r_proposals[0].spi;
+ a.is_add = 0;
+ a.local_ip.as_u32 = sa->iaddr.as_u32;
+ a.remote_ip.as_u32 = sa->raddr.as_u32;
+ a.local_spi = child->r_proposals[0].spi;
+ a.remote_spi = child->i_proposals[0].spi;
+ }
+ else
+ {
+ if (!vec_len (child->r_proposals))
+ return 0;
+
+ a.is_add = 0;
+ a.local_ip.as_u32 = sa->raddr.as_u32;
+ a.remote_ip.as_u32 = sa->iaddr.as_u32;
+ a.local_spi = child->i_proposals[0].spi;
+ a.remote_spi = child->r_proposals[0].spi;
+ }
ipsec_add_del_tunnel_if (&a);
return 0;
}
static u32
-ikev2_generate_resp (ikev2_sa_t * sa, ike_header_t * ike)
+ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user)
{
v8 *integ = 0;
ike_payload_header_t *ph;
@@ -1404,6 +1739,14 @@ ikev2_generate_resp (ikev2_sa_t * sa, ike_header_t * ike)
data);
vec_free (data);
}
+ else if (sa->state == IKEV2_STATE_SA_INIT)
+ {
+ ikev2_payload_add_id (chain, &sa->i_id, IKEV2_PAYLOAD_IDI);
+ ikev2_payload_add_auth (chain, &sa->i_auth);
+ ikev2_payload_add_sa (chain, sa->childs[0].i_proposals);
+ ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
+ ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
+ }
else
{
ikev2_set_state (sa, IKEV2_STATE_DELETED);
@@ -1415,10 +1758,13 @@ ikev2_generate_resp (ikev2_sa_t * sa, ike_header_t * ike)
/* if pending delete */
if (sa->del)
{
- /* The response to a request that deletes the IKE SA is an empty
- INFORMATIONAL response. */
if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
{
+ if (sa->is_initiator)
+ ikev2_payload_add_delete (chain, sa->del);
+
+ /* The response to a request that deletes the IKE SA is an empty
+ INFORMATIONAL response. */
ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
}
/* The response to a request that deletes ESP or AH SAs will contain
@@ -1452,37 +1798,71 @@ ikev2_generate_resp (ikev2_sa_t * sa, ike_header_t * ike)
}
else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
{
- if (sa->rekey)
- {
- ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
- ikev2_payload_add_nonce (chain, sa->r_nonce);
- ikev2_payload_add_ts (chain, sa->rekey[0].tsi, IKEV2_PAYLOAD_TSI);
- ikev2_payload_add_ts (chain, sa->rekey[0].tsr, IKEV2_PAYLOAD_TSR);
- vec_del1 (sa->rekey, 0);
- }
- else if (sa->unsupported_cp)
+ if (sa->is_initiator)
{
- u8 *data = vec_new (u8, 1);
- data[0] = sa->unsupported_cp;
- ikev2_payload_add_notify (chain,
- IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
- data);
+ ikev2_sa_proposal_t *proposals = (ikev2_sa_proposal_t *) user;
+ ikev2_notify_t notify;
+ u8 *data = vec_new (u8, 4);
+ memset (&notify, 0, sizeof (notify));
+ notify.protocol_id = IKEV2_PROTOCOL_ESP;
+ notify.spi = sa->childs[0].i_proposals->spi;
+ *(u32 *) data = clib_host_to_net_u32 (notify.spi);
+
+ ikev2_payload_add_sa (chain, proposals);
+ ikev2_payload_add_nonce (chain, sa->i_nonce);
+ ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
+ ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
+ ikev2_payload_add_notify_2 (chain, IKEV2_NOTIFY_MSG_REKEY_SA, data,
+ &notify);
+
vec_free (data);
- sa->unsupported_cp = 0;
}
else
{
- ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
- 0);
+ if (sa->rekey)
+ {
+ ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
+ ikev2_payload_add_nonce (chain, sa->r_nonce);
+ ikev2_payload_add_ts (chain, sa->rekey[0].tsi,
+ IKEV2_PAYLOAD_TSI);
+ ikev2_payload_add_ts (chain, sa->rekey[0].tsr,
+ IKEV2_PAYLOAD_TSR);
+ vec_del1 (sa->rekey, 0);
+ }
+ else if (sa->unsupported_cp)
+ {
+ u8 *data = vec_new (u8, 1);
+
+ data[0] = sa->unsupported_cp;
+ ikev2_payload_add_notify (chain,
+ IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
+ data);
+ vec_free (data);
+ sa->unsupported_cp = 0;
+ }
+ else
+ {
+ ikev2_payload_add_notify (chain,
+ IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
+ 0);
+ }
}
}
/* IKEv2 header */
ike->version = IKE_VERSION_2;
- ike->flags = IKEV2_HDR_FLAG_RESPONSE;
ike->nextpayload = IKEV2_PAYLOAD_SK;
tlen = sizeof (*ike);
+ if (sa->is_initiator)
+ {
+ ike->flags = IKEV2_HDR_FLAG_INITIATOR;
+ sa->last_init_msg_id = clib_net_to_host_u32 (ike->msgid);
+ }
+ else
+ {
+ ike->flags = IKEV2_HDR_FLAG_RESPONSE;
+ }
if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
@@ -1518,8 +1898,9 @@ ikev2_generate_resp (ikev2_sa_t * sa, ike_header_t * ike)
ike->length = clib_host_to_net_u32 (tlen);
/* calc integrity data for whole packet except hash itself */
- integ = ikev2_calc_integr (tr_integ, sa->sk_ar, (u8 *) ike,
- tlen - tr_integ->key_trunc);
+ integ =
+ ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ai : sa->sk_ar,
+ (u8 *) ike, tlen - tr_integ->key_trunc);
clib_memcpy (ike->payload + tlen - tr_integ->key_trunc - sizeof (*ike),
integ, tr_integ->key_trunc);
@@ -1708,43 +2089,81 @@ ikev2_node_fn (vlib_main_t * vm,
sa0 = &sa;
memset (sa0, 0, sizeof (*sa0));
- if (ike0->rspi == 0)
+ if (ike0->flags & IKEV2_HDR_FLAG_INITIATOR)
{
- sa0->raddr.as_u32 = ip40->dst_address.as_u32;
- sa0->iaddr.as_u32 = ip40->src_address.as_u32;
-
- r = ikev2_retransmit_sa_init (ike0, sa0->iaddr, sa0->raddr);
- if (r == 1)
+ if (ike0->rspi == 0)
{
- vlib_node_increment_counter (vm, ikev2_node.index,
- IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
- 1);
- len = clib_net_to_host_u32 (ike0->length);
- goto dispatch0;
- }
- else if (r == -1)
- {
- vlib_node_increment_counter (vm, ikev2_node.index,
- IKEV2_ERROR_IKE_SA_INIT_IGNORE,
- 1);
- goto dispatch0;
- }
+ sa0->raddr.as_u32 = ip40->dst_address.as_u32;
+ sa0->iaddr.as_u32 = ip40->src_address.as_u32;
+
+ r = ikev2_retransmit_sa_init (ike0, sa0->iaddr,
+ sa0->raddr);
+ if (r == 1)
+ {
+ vlib_node_increment_counter (vm, ikev2_node.index,
+ IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
+ 1);
+ len = clib_net_to_host_u32 (ike0->length);
+ goto dispatch0;
+ }
+ else if (r == -1)
+ {
+ vlib_node_increment_counter (vm, ikev2_node.index,
+ IKEV2_ERROR_IKE_SA_INIT_IGNORE,
+ 1);
+ goto dispatch0;
+ }
- ikev2_process_sa_init_req (vm, sa0, ike0);
+ ikev2_process_sa_init_req (vm, sa0, ike0);
- if (sa0->state == IKEV2_STATE_SA_INIT)
- {
- ikev2_sa_free_proposal_vector (&sa0->r_proposals);
- sa0->r_proposals =
- ikev2_select_proposal (sa0->i_proposals,
- IKEV2_PROTOCOL_IKE);
- ikev2_generate_sa_init_data (sa0);
+ if (sa0->state == IKEV2_STATE_SA_INIT)
+ {
+ ikev2_sa_free_proposal_vector (&sa0->r_proposals);
+ sa0->r_proposals =
+ ikev2_select_proposal (sa0->i_proposals,
+ IKEV2_PROTOCOL_IKE);
+ ikev2_generate_sa_init_data (sa0);
+ }
+
+ if (sa0->state == IKEV2_STATE_SA_INIT
+ || sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE)
+ {
+ len = ikev2_generate_message (sa0, ike0, 0);
+ }
+
+ if (sa0->state == IKEV2_STATE_SA_INIT)
+ {
+ /* add SA to the pool */
+ pool_get (km->per_thread_data[cpu_index].sas, sa0);
+ clib_memcpy (sa0, &sa, sizeof (*sa0));
+ hash_set (km->per_thread_data[cpu_index].sa_by_rspi,
+ sa0->rspi,
+ sa0 - km->per_thread_data[cpu_index].sas);
+ }
+ else
+ {
+ ikev2_sa_free_all_vec (sa0);
+ }
}
+ }
+ else
+ {
+ ikev2_process_sa_init_resp (vm, sa0, ike0);
- if (sa0->state == IKEV2_STATE_SA_INIT ||
- sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE)
+ if (sa0->state == IKEV2_STATE_SA_INIT)
{
- len = ikev2_generate_resp (sa0, ike0);
+ ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH;
+ uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
+ if (p)
+ {
+ ikev2_sa_t *sai =
+ pool_elt_at_index (km->sais, p[0]);
+
+ ikev2_complete_sa_data (sa0, sai);
+ ikev2_calc_keys (sa0);
+ ikev2_sa_auth_init (sa0);
+ len = ikev2_generate_message (sa0, ike0, 0);
+ }
}
if (sa0->state == IKEV2_STATE_SA_INIT)
@@ -1799,7 +2218,23 @@ ikev2_node_fn (vlib_main_t * vm,
ikev2_create_tunnel_interface (km->vnet_main, sa0,
&sa0->childs[0]);
}
- len = ikev2_generate_resp (sa0, ike0);
+
+ if (sa0->is_initiator)
+ {
+ uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
+ if (p)
+ {
+ ikev2_sa_t *sai =
+ pool_elt_at_index (km->sais, p[0]);
+ hash_unset (km->sa_by_ispi, sai->ispi);
+ ikev2_sa_free_all_vec (sai);
+ pool_put (km->sais, sai);
+ }
+ }
+ else
+ {
+ len = ikev2_generate_message (sa0, ike0, 0);
+ }
}
}
else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
@@ -1839,22 +2274,32 @@ ikev2_node_fn (vlib_main_t * vm,
{
ikev2_child_sa_t *ch_sa;
ch_sa = ikev2_sa_get_child (sa0, d->spi,
- d->protocol_id);
+ d->protocol_id,
+ !sa0->is_initiator);
if (ch_sa)
{
ikev2_delete_tunnel_interface (km->vnet_main,
sa0, ch_sa);
- vec_add2 (resp, tmp, 1);
- tmp->protocol_id = d->protocol_id;
- tmp->spi = ch_sa->r_proposals[0].spi;
+ if (!sa0->is_initiator)
+ {
+ vec_add2 (resp, tmp, 1);
+ tmp->protocol_id = d->protocol_id;
+ tmp->spi = ch_sa->r_proposals[0].spi;
+ }
ikev2_sa_del_child_sa (sa0, ch_sa);
}
}
- vec_free (sa0->del);
- sa0->del = resp;
+ if (!sa0->is_initiator)
+ {
+ vec_free (sa0->del);
+ sa0->del = resp;
+ }
}
}
- len = ikev2_generate_resp (sa0, ike0);
+ if (!sa0->is_initiator)
+ {
+ len = ikev2_generate_message (sa0, ike0, 0);
+ }
}
}
else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
@@ -1898,7 +2343,14 @@ ikev2_node_fn (vlib_main_t * vm,
ikev2_create_tunnel_interface (km->vnet_main, sa0,
child);
}
- len = ikev2_generate_resp (sa0, ike0);
+ if (sa0->is_initiator)
+ {
+ vec_del1 (sa0->rekey, 0);
+ }
+ else
+ {
+ len = ikev2_generate_message (sa0, ike0, 0);
+ }
}
}
}
@@ -1915,8 +2367,16 @@ ikev2_node_fn (vlib_main_t * vm,
if (len)
{
next0 = IKEV2_NEXT_IP4_LOOKUP;
- ip40->dst_address.as_u32 = sa0->iaddr.as_u32;
- ip40->src_address.as_u32 = sa0->raddr.as_u32;
+ if (sa0->is_initiator)
+ {
+ ip40->dst_address.as_u32 = sa0->raddr.as_u32;
+ ip40->src_address.as_u32 = sa0->iaddr.as_u32;
+ }
+ else
+ {
+ ip40->dst_address.as_u32 = sa0->iaddr.as_u32;
+ ip40->src_address.as_u32 = sa0->raddr.as_u32;
+ }
udp0->length =
clib_host_to_net_u16 (len + sizeof (udp_header_t));
udp0->checksum = 0;
@@ -1979,6 +2439,126 @@ VLIB_REGISTER_NODE (ikev2_node,static) = {
/* *INDENT-ON* */
+static clib_error_t *
+ikev2_set_initiator_proposals (vlib_main_t * vm, ikev2_sa_t * sa,
+ ikev2_transforms_set * ts,
+ ikev2_sa_proposal_t ** proposals, int is_ike)
+{
+ clib_error_t *r;
+ ikev2_main_t *km = &ikev2_main;
+ ikev2_sa_proposal_t *proposal;
+ vec_add2 (*proposals, proposal, 1);
+ ikev2_sa_transform_t *td;
+ int error;
+
+ /* Encryption */
+ error = 1;
+ vec_foreach (td, km->supported_transforms)
+ {
+ if (td->type == IKEV2_TRANSFORM_TYPE_ENCR
+ && td->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC
+ && td->key_len == ts->crypto_key_size / 8)
+ {
+ u16 attr[2];
+ attr[0] = clib_host_to_net_u16 (14 | (1 << 15));
+ attr[1] = clib_host_to_net_u16 (td->key_len << 3);
+ vec_add (td->attrs, (u8 *) attr, 4);
+ vec_add1 (proposal->transforms, *td);
+ td->attrs = 0;
+
+ error = 0;
+ break;
+ }
+ }
+ if (error)
+ {
+ r = clib_error_return (0, "Unsupported algorithm");
+ return r;
+ }
+
+ /* Integrity */
+ error = 1;
+ vec_foreach (td, km->supported_transforms)
+ {
+ if (td->type == IKEV2_TRANSFORM_TYPE_INTEG
+ && td->integ_type == IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96)
+ {
+ vec_add1 (proposal->transforms, *td);
+ error = 0;
+ break;
+ }
+ }
+ if (error)
+ {
+ r = clib_error_return (0, "Unsupported algorithm");
+ return r;
+ }
+
+ /* PRF */
+ if (is_ike)
+ {
+ error = 1;
+ vec_foreach (td, km->supported_transforms)
+ {
+ if (td->type == IKEV2_TRANSFORM_TYPE_PRF
+ && td->prf_type == IKEV2_TRANSFORM_PRF_TYPE_PRF_HMAC_SHA1)
+ {
+ vec_add1 (proposal->transforms, *td);
+ error = 0;
+ break;
+ }
+ }
+ if (error)
+ {
+ r = clib_error_return (0, "Unsupported algorithm");
+ return r;
+ }
+ }
+
+ /* DH */
+ error = 1;
+ vec_foreach (td, km->supported_transforms)
+ {
+ if (td->type == IKEV2_TRANSFORM_TYPE_DH && td->dh_type == ts->dh_type)
+ {
+ vec_add1 (proposal->transforms, *td);
+ if (is_ike)
+ {
+ sa->dh_group = td->dh_type;
+ }
+ error = 0;
+ break;
+ }
+ }
+ if (error)
+ {
+ r = clib_error_return (0, "Unsupported algorithm");
+ return r;
+ }
+
+ if (!is_ike)
+ {
+ error = 1;
+ vec_foreach (td, km->supported_transforms)
+ {
+ if (td->type == IKEV2_TRANSFORM_TYPE_ESN)
+ {
+ vec_add1 (proposal->transforms, *td);
+ error = 0;
+ break;
+ }
+ }
+ if (error)
+ {
+ r = clib_error_return (0, "Unsupported algorithm");
+ return r;
+ }
+ }
+
+
+ return 0;
+}
+
static ikev2_profile_t *
ikev2_profile_index_by_name (u8 * name)
{
@@ -1992,6 +2572,64 @@ ikev2_profile_index_by_name (u8 * name)
return pool_elt_at_index (km->profiles, p[0]);
}
+
+static void
+ikev2_send_ike (vlib_main_t * vm, ip4_address_t * src, ip4_address_t * dst,
+ u32 bi0, u32 len)
+{
+ ip4_header_t *ip40;
+ udp_header_t *udp0;
+ vlib_buffer_t *b0;
+ vlib_frame_t *f;
+ u32 *to_next;
+
+ b0 = vlib_get_buffer (vm, bi0);
+ vlib_buffer_advance (b0, -sizeof (udp_header_t));
+ udp0 = vlib_buffer_get_current (b0);
+ vlib_buffer_advance (b0, -sizeof (ip4_header_t));
+ ip40 = vlib_buffer_get_current (b0);
+
+
+ ip40->ip_version_and_header_length = 0x45;
+ ip40->tos = 0;
+ ip40->fragment_id = 0;
+ ip40->flags_and_fragment_offset = 0;
+ ip40->ttl = 0xff;
+ ip40->protocol = IP_PROTOCOL_UDP;
+ ip40->dst_address.as_u32 = dst->as_u32;
+ ip40->src_address.as_u32 = src->as_u32;
+ udp0->dst_port = clib_host_to_net_u16 (500);
+ udp0->src_port = clib_host_to_net_u16 (500);
+ udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
+ udp0->checksum = 0;
+ b0->current_length = len + sizeof (ip4_header_t) + sizeof (udp_header_t);
+ ip40->length = clib_host_to_net_u16 (b0->current_length);
+ ip40->checksum = ip4_header_checksum (ip40);
+
+
+ /* send the request */
+ f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
+ to_next = vlib_frame_vector_args (f);
+ to_next[0] = bi0;
+ f->n_vectors = 1;
+ vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
+
+}
+
+static u32
+ikev2_get_new_ike_header_buff (vlib_main_t * vm, ike_header_t ** ike)
+{
+ u32 bi0;
+ if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
+ {
+ *ike = 0;
+ return 0;
+ }
+ vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
+ *ike = vlib_buffer_get_current (b0);
+ return bi0;
+}
+
clib_error_t *
ikev2_set_local_key (vlib_main_t * vm, u8 * file)
{
@@ -2018,6 +2656,7 @@ ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add)
pool_get (km->profiles, p);
memset (p, 0, sizeof (*p));
p->name = vec_dup (name);
+ p->responder.sw_if_index = ~0;
uword index = p - km->profiles;
mhash_set_mem (&km->profile_index_by_name, name, &index, 0);
}
@@ -2145,6 +2784,470 @@ ikev2_set_profile_ts (vlib_main_t * vm, u8 * name, u8 protocol_id,
clib_error_t *
+ikev2_set_profile_responder (vlib_main_t * vm, u8 * name,
+ u32 sw_if_index, ip4_address_t ip4)
+{
+ ikev2_profile_t *p;
+ clib_error_t *r;
+
+ p = ikev2_profile_index_by_name (name);
+
+ if (!p)
+ {
+ r = clib_error_return (0, "unknown profile %v", name);
+ return r;
+ }
+
+ p->responder.sw_if_index = sw_if_index;
+ p->responder.ip4 = ip4;
+
+ return 0;
+}
+
+clib_error_t *
+ikev2_set_profile_ike_transforms (vlib_main_t * vm, u8 * name,
+ ikev2_transform_encr_type_t crypto_alg,
+ ikev2_transform_integ_type_t integ_alg,
+ ikev2_transform_dh_type_t dh_type,
+ u32 crypto_key_size)
+{
+ ikev2_profile_t *p;
+ clib_error_t *r;
+
+ p = ikev2_profile_index_by_name (name);
+
+ if (!p)
+ {
+ r = clib_error_return (0, "unknown profile %v", name);
+ return r;
+ }
+
+ p->ike_ts.crypto_alg = crypto_alg;
+ p->ike_ts.integ_alg = integ_alg;
+ p->ike_ts.dh_type = dh_type;
+ p->ike_ts.crypto_key_size = crypto_key_size;
+ return 0;
+}
+
+clib_error_t *
+ikev2_set_profile_esp_transforms (vlib_main_t * vm, u8 * name,
+ ikev2_transform_encr_type_t crypto_alg,
+ ikev2_transform_integ_type_t integ_alg,
+ ikev2_transform_dh_type_t dh_type,
+ u32 crypto_key_size)
+{
+ ikev2_profile_t *p;
+ clib_error_t *r;
+
+ p = ikev2_profile_index_by_name (name);
+
+ if (!p)
+ {
+ r = clib_error_return (0, "unknown profile %v", name);
+ return r;
+ }
+
+ p->esp_ts.crypto_alg = crypto_alg;
+ p->esp_ts.integ_alg = integ_alg;
+ p->esp_ts.dh_type = dh_type;
+ p->esp_ts.crypto_key_size = crypto_key_size;
+ return 0;
+}
+
+clib_error_t *
+ikev2_set_profile_sa_lifetime (vlib_main_t * vm, u8 * name,
+ u64 lifetime, u32 jitter, u32 handover,
+ u64 maxdata)
+{
+ ikev2_profile_t *p;
+ clib_error_t *r;
+
+ p = ikev2_profile_index_by_name (name);
+
+ if (!p)
+ {
+ r = clib_error_return (0, "unknown profile %v", name);
+ return r;
+ }
+
+ p->lifetime = lifetime;
+ p->lifetime_jitter = jitter;
+ p->handover = handover;
+ p->lifetime_maxdata = maxdata;
+ return 0;
+}
+
+clib_error_t *
+ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name)
+{
+ ikev2_profile_t *p;
+ clib_error_t *r;
+ ip4_main_t *im = &ip4_main;
+ ikev2_main_t *km = &ikev2_main;
+
+ p = ikev2_profile_index_by_name (name);
+
+ if (!p)
+ {
+ r = clib_error_return (0, "unknown profile %v", name);
+ return r;
+ }
+
+ if (p->responder.sw_if_index == ~0 || p->responder.ip4.data_u32 == 0)
+ {
+ r = clib_error_return (0, "responder not set for profile %v", name);
+ return r;
+ }
+
+
+ /* Create the Initiator Request */
+ {
+ ike_header_t *ike0;
+ u32 bi0 = 0;
+ ip_lookup_main_t *lm = &im->lookup_main;
+ u32 if_add_index0;
+ int len = sizeof (ike_header_t);
+
+ /* Get own iface IP */
+ if_add_index0 =
+ lm->if_address_pool_index_by_sw_if_index[p->responder.sw_if_index];
+ ip_interface_address_t *if_add =
+ pool_elt_at_index (lm->if_address_pool, if_add_index0);
+ ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
+
+ bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
+
+ /* Prepare the SA and the IKE payload */
+ ikev2_sa_t sa;
+ memset (&sa, 0, sizeof (ikev2_sa_t));
+ ikev2_payload_chain_t *chain = 0;
+ ikev2_payload_new_chain (chain);
+
+ /* Build the IKE proposal payload */
+ ikev2_sa_proposal_t *proposals = 0;
+ ikev2_set_initiator_proposals (vm, &sa, &p->ike_ts, &proposals, 1);
+ proposals[0].proposal_num = 1;
+ proposals[0].protocol_id = IKEV2_PROTOCOL_IKE;
+
+ /* Add and then cleanup proposal data */
+ ikev2_payload_add_sa (chain, proposals);
+ ikev2_sa_free_proposal_vector (&proposals);
+
+ sa.is_initiator = 1;
+ sa.profile = p;
+ sa.state = IKEV2_STATE_SA_INIT;
+ ikev2_generate_sa_init_data (&sa);
+ ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data);
+ ikev2_payload_add_nonce (chain, sa.i_nonce);
+
+ /* Build the child SA proposal */
+ vec_resize (sa.childs, 1);
+ ikev2_set_initiator_proposals (vm, &sa, &p->esp_ts,
+ &sa.childs[0].i_proposals, 0);
+ sa.childs[0].i_proposals[0].proposal_num = 1;
+ sa.childs[0].i_proposals[0].protocol_id = IKEV2_PROTOCOL_ESP;
+ RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi,
+ sizeof (sa.childs[0].i_proposals[0].spi));
+
+
+
+ /* Add NAT detection notification messages (mandatory) */
+ u8 nat_detection_source[8 + 8 + 4 + 2];
+ u8 *nat_detection_sha1 = vec_new (u8, 20);
+
+ u64 tmpspi = clib_host_to_net_u64 (sa.ispi);
+ clib_memcpy (&nat_detection_source[0], &tmpspi, sizeof (tmpspi));
+ tmpspi = clib_host_to_net_u64 (sa.rspi);
+ clib_memcpy (&nat_detection_source[8], &tmpspi, sizeof (tmpspi));
+ u16 tmpport = clib_host_to_net_u16 (500);
+ clib_memcpy (&nat_detection_source[8 + 8 + 4], &tmpport,
+ sizeof (tmpport));
+ u32 tmpip = clib_host_to_net_u32 (if_ip->as_u32);
+ clib_memcpy (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
+ SHA1 (nat_detection_source, sizeof (nat_detection_source),
+ nat_detection_sha1);
+ ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
+ nat_detection_sha1);
+ tmpip = clib_host_to_net_u32 (p->responder.ip4.as_u32);
+ clib_memcpy (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
+ SHA1 (nat_detection_source, sizeof (nat_detection_source),
+ nat_detection_sha1);
+ ikev2_payload_add_notify (chain,
+ IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
+ nat_detection_sha1);
+ vec_free (nat_detection_sha1);
+
+ u8 *sig_hash_algo = vec_new (u8, 8);
+ u64 tmpsig = clib_host_to_net_u64 (0x0001000200030004);
+ clib_memcpy (sig_hash_algo, &tmpsig, sizeof (tmpsig));
+ ikev2_payload_add_notify (chain,
+ IKEV2_NOTIFY_MSG_SIGNATURE_HASH_ALGORITHMS,
+ sig_hash_algo);
+ vec_free (sig_hash_algo);
+
+
+ /* Buffer update and bolierplate */
+ len += vec_len (chain->data);
+ ike0->nextpayload = chain->first_payload_type;
+ ike0->length = clib_host_to_net_u32 (len);
+ clib_memcpy (ike0->payload, chain->data, vec_len (chain->data));
+ ikev2_payload_destroy_chain (chain);
+
+ ike0->version = IKE_VERSION_2;
+ ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
+ ike0->exchange = IKEV2_EXCHANGE_SA_INIT;
+ ike0->ispi = sa.ispi;
+
+ /* store whole IKE payload - needed for PSK auth */
+ vec_free (sa.last_sa_init_req_packet_data);
+ vec_add (sa.last_sa_init_req_packet_data, ike0, len);
+
+ /* add data to the SA then add it to the pool */
+ sa.iaddr.as_u32 = if_ip->as_u32;
+ sa.raddr.as_u32 = p->responder.ip4.as_u32;
+ sa.i_id.type = p->loc_id.type;
+ sa.i_id.data = vec_dup (p->loc_id.data);
+ sa.i_auth.method = p->auth.method;
+ sa.i_auth.hex = p->auth.hex;
+ sa.i_auth.data = vec_dup (p->auth.data);
+ sa.i_auth.key = vec_dup (p->auth.key);
+ vec_add (sa.childs[0].tsi, &p->loc_ts, 1);
+ vec_add (sa.childs[0].tsr, &p->rem_ts, 1);
+
+ /* add SA to the pool */
+ ikev2_sa_t *sa0 = 0;
+ pool_get (km->sais, sa0);
+ clib_memcpy (sa0, &sa, sizeof (*sa0));
+ hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais);
+
+ ikev2_send_ike (vm, if_ip, &p->responder.ip4, bi0, len);
+
+ }
+
+ return 0;
+}
+
+static void
+ikev2_delete_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
+ ikev2_child_sa_t * csa)
+{
+ /* Create the Initiator notification for child SA removal */
+ ikev2_main_t *km = &ikev2_main;
+ ike_header_t *ike0;
+ u32 bi0 = 0;
+ int len;
+
+ bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
+
+
+ ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
+ ike0->ispi = clib_host_to_net_u64 (sa->ispi);
+ ike0->rspi = clib_host_to_net_u64 (sa->rspi);
+ vec_resize (sa->del, 1);
+ sa->del->protocol_id = IKEV2_PROTOCOL_ESP;
+ sa->del->spi = csa->i_proposals->spi;
+ ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
+ sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
+ len = ikev2_generate_message (sa, ike0, 0);
+
+ ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
+
+ /* delete local child SA */
+ ikev2_delete_tunnel_interface (km->vnet_main, sa, csa);
+ ikev2_sa_del_child_sa (sa, csa);
+}
+
+clib_error_t *
+ikev2_initiate_delete_child_sa (vlib_main_t * vm, u32 ispi)
+{
+ clib_error_t *r;
+ ikev2_main_t *km = &ikev2_main;
+ ikev2_main_per_thread_data_t *tkm;
+ ikev2_sa_t *fsa = 0;
+ ikev2_child_sa_t *fchild = 0;
+
+ /* Search for the child SA */
+ vec_foreach (tkm, km->per_thread_data)
+ {
+ ikev2_sa_t *sa;
+ if (fchild)
+ break;
+ /* *INDENT-OFF* */
+ pool_foreach (sa, tkm->sas, ({
+ fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
+ if (fchild)
+ {
+ fsa = sa;
+ break;
+ }
+ }));
+ /* *INDENT-ON* */
+ }
+
+ if (!fchild || !fsa)
+ {
+ r = clib_error_return (0, "Child SA not found");
+ return r;
+ }
+ else
+ {
+ ikev2_delete_child_sa_internal (vm, fsa, fchild);
+ }
+
+ return 0;
+}
+
+clib_error_t *
+ikev2_initiate_delete_ike_sa (vlib_main_t * vm, u64 ispi)
+{
+ clib_error_t *r;
+ ikev2_main_t *km = &ikev2_main;
+ ikev2_main_per_thread_data_t *tkm;
+ ikev2_sa_t *fsa = 0;
+ ikev2_main_per_thread_data_t *ftkm = 0;
+
+ /* Search for the IKE SA */
+ vec_foreach (tkm, km->per_thread_data)
+ {
+ ikev2_sa_t *sa;
+ if (fsa)
+ break;
+ /* *INDENT-OFF* */
+ pool_foreach (sa, tkm->sas, ({
+ if (sa->ispi == ispi)
+ {
+ fsa = sa;
+ ftkm = tkm;
+ break;
+ }
+ }));
+ /* *INDENT-ON* */
+ }
+
+ if (!fsa)
+ {
+ r = clib_error_return (0, "IKE SA not found");
+ return r;
+ }
+
+
+ /* Create the Initiator notification for IKE SA removal */
+ {
+ ike_header_t *ike0;
+ u32 bi0 = 0;
+ int len;
+
+ bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
+
+
+ ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
+ ike0->ispi = clib_host_to_net_u64 (fsa->ispi);
+ ike0->rspi = clib_host_to_net_u64 (fsa->rspi);
+ vec_resize (fsa->del, 1);
+ fsa->del->protocol_id = IKEV2_PROTOCOL_IKE;
+ fsa->del->spi = ispi;
+ ike0->msgid = clib_host_to_net_u32 (fsa->last_init_msg_id + 1);
+ fsa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
+ len = ikev2_generate_message (fsa, ike0, 0);
+
+ ikev2_send_ike (vm, &fsa->iaddr, &fsa->raddr, bi0, len);
+ }
+
+
+ /* delete local SA */
+ ikev2_child_sa_t *c;
+ vec_foreach (c, fsa->childs)
+ {
+ ikev2_delete_tunnel_interface (km->vnet_main, fsa, c);
+ ikev2_sa_del_child_sa (fsa, c);
+ }
+ ikev2_sa_free_all_vec (fsa);
+ uword *p = hash_get (ftkm->sa_by_rspi, fsa->rspi);
+ if (p)
+ {
+ hash_unset (ftkm->sa_by_rspi, fsa->rspi);
+ pool_put (ftkm->sas, fsa);
+ }
+
+
+ return 0;
+}
+
+static void
+ikev2_rekey_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
+ ikev2_child_sa_t * csa)
+{
+ /* Create the Initiator request for create child SA */
+ ike_header_t *ike0;
+ u32 bi0 = 0;
+ int len;
+
+
+ bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
+
+
+ ike0->version = IKE_VERSION_2;
+ ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
+ ike0->exchange = IKEV2_EXCHANGE_CREATE_CHILD_SA;
+ ike0->ispi = clib_host_to_net_u64 (sa->ispi);
+ ike0->rspi = clib_host_to_net_u64 (sa->rspi);
+ ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
+ sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
+
+ ikev2_rekey_t *rekey;
+ vec_add2 (sa->rekey, rekey, 1);
+ ikev2_sa_proposal_t *proposals = vec_dup (csa->i_proposals);
+
+ /*need new ispi */
+ RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi));
+ rekey->spi = proposals[0].spi;
+ rekey->ispi = csa->i_proposals->spi;
+ len = ikev2_generate_message (sa, ike0, proposals);
+ ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
+ vec_free (proposals);
+}
+
+clib_error_t *
+ikev2_initiate_rekey_child_sa (vlib_main_t * vm, u32 ispi)
+{
+ clib_error_t *r;
+ ikev2_main_t *km = &ikev2_main;
+ ikev2_main_per_thread_data_t *tkm;
+ ikev2_sa_t *fsa = 0;
+ ikev2_child_sa_t *fchild = 0;
+
+ /* Search for the child SA */
+ vec_foreach (tkm, km->per_thread_data)
+ {
+ ikev2_sa_t *sa;
+ if (fchild)
+ break;
+ /* *INDENT-OFF* */
+ pool_foreach (sa, tkm->sas, ({
+ fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
+ if (fchild)
+ {
+ fsa = sa;
+ break;
+ }
+ }));
+ /* *INDENT-ON* */
+ }
+
+ if (!fchild || !fsa)
+ {
+ r = clib_error_return (0, "Child SA not found");
+ return r;
+ }
+ else
+ {
+ ikev2_rekey_child_sa_internal (vm, fsa, fchild);
+ }
+
+ return 0;
+}
+
+clib_error_t *
ikev2_init (vlib_main_t * vm)
{
ikev2_main_t *km = &ikev2_main;
@@ -2167,6 +3270,9 @@ ikev2_init (vlib_main_t * vm)
hash_create (0, sizeof (uword));
}
+ km->sa_by_ispi = hash_create (0, sizeof (uword));
+
+
if ((error = vlib_call_init_function (vm, ikev2_cli_init)))
return error;
@@ -2176,6 +3282,146 @@ ikev2_init (vlib_main_t * vm)
}
+static u8
+ikev2_mngr_process_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * csa)
+{
+ ikev2_main_t *km = &ikev2_main;
+ vlib_main_t *vm = km->vlib_main;
+ f64 now = vlib_time_now (vm);
+ u8 res = 0;
+
+ if (sa->is_initiator && sa->profile && csa->time_to_expiration
+ && now > csa->time_to_expiration)
+ {
+ if (!csa->is_expired || csa->rekey_retries > 0)
+ {
+ ikev2_rekey_child_sa_internal (vm, sa, csa);
+ csa->time_to_expiration = now + sa->profile->handover;
+ csa->is_expired = 1;
+ if (csa->rekey_retries == 0)
+ {
+ csa->rekey_retries = 5;
+ }
+ else if (csa->rekey_retries > 0)
+ {
+ csa->rekey_retries--;
+ clib_warning ("Rekeing Child SA 0x%x, retries left %d",
+ csa->i_proposals->spi, csa->rekey_retries);
+ if (csa->rekey_retries == 0)
+ {
+ csa->rekey_retries = -1;
+ }
+ }
+ res |= 1;
+ }
+ else
+ {
+ csa->time_to_expiration = 0;
+ ikev2_delete_child_sa_internal (vm, sa, csa);
+ res |= 1;
+ }
+ }
+
+ return res;
+}
+
+static void
+ikev2_mngr_process_ipsec_sa (ipsec_sa_t * ipsec_sa)
+{
+ ikev2_main_t *km = &ikev2_main;
+ vlib_main_t *vm = km->vlib_main;
+ ikev2_main_per_thread_data_t *tkm;
+ ikev2_sa_t *fsa = 0;
+ ikev2_child_sa_t *fchild = 0;
+ f64 now = vlib_time_now (vm);
+
+ /* Search for the SA and child SA */
+ vec_foreach (tkm, km->per_thread_data)
+ {
+ ikev2_sa_t *sa;
+ if (fchild)
+ break;
+ /* *INDENT-OFF* */
+ pool_foreach (sa, tkm->sas, ({
+ fchild = ikev2_sa_get_child(sa, ipsec_sa->spi, IKEV2_PROTOCOL_ESP, 1);
+ if (fchild)
+ {
+ fsa = sa;
+ break;
+ }
+ }));
+ /* *INDENT-ON* */
+ }
+
+ if (fchild && fsa && fsa->profile && fsa->profile->lifetime_maxdata)
+ {
+ if (!fchild->is_expired
+ && ipsec_sa->total_data_size > fsa->profile->lifetime_maxdata)
+ {
+ fchild->time_to_expiration = now;
+ }
+ }
+}
+
+static vlib_node_registration_t ikev2_mngr_process_node;
+
+static uword
+ikev2_mngr_process_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
+ vlib_frame_t * f)
+{
+ ikev2_main_t *km = &ikev2_main;
+ ipsec_main_t *im = &ipsec_main;
+
+ while (1)
+ {
+ u8 req_sent = 0;
+ vlib_process_wait_for_event_or_clock (vm, 1);
+ vlib_process_get_events (vm, NULL);
+
+ /* process ike child sas */
+ ikev2_main_per_thread_data_t *tkm;
+ vec_foreach (tkm, km->per_thread_data)
+ {
+ ikev2_sa_t *sa;
+ /* *INDENT-OFF* */
+ pool_foreach (sa, tkm->sas, ({
+ ikev2_child_sa_t *c;
+ vec_foreach (c, sa->childs)
+ {
+ req_sent |= ikev2_mngr_process_child_sa(sa, c);
+ }
+ }));
+ /* *INDENT-ON* */
+ }
+
+ /* process ipsec sas */
+ ipsec_sa_t *sa;
+ /* *INDENT-OFF* */
+ pool_foreach (sa, im->sad, ({
+ ikev2_mngr_process_ipsec_sa(sa);
+ }));
+ /* *INDENT-ON* */
+
+ if (req_sent)
+ {
+ vlib_process_wait_for_event_or_clock (vm, 5);
+ vlib_process_get_events (vm, NULL);
+ req_sent = 0;
+ }
+
+ }
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (ikev2_mngr_process_node, static) = {
+ .function = ikev2_mngr_process_fn,
+ .type = VLIB_NODE_TYPE_PROCESS,
+ .name =
+ "ikev2-manager-process",
+};
+
+/* *INDENT-ON* */
/*
* fd.io coding-style-patch-verification: ON
diff --git a/src/vnet/ipsec/ikev2.h b/src/vnet/ipsec/ikev2.h
index 723fdde8c1c..84a8be538e9 100644
--- a/src/vnet/ipsec/ikev2.h
+++ b/src/vnet/ipsec/ikev2.h
@@ -371,6 +371,31 @@ clib_error_t *ikev2_set_profile_ts (vlib_main_t * vm, u8 * name,
u8 protocol_id, u16 start_port,
u16 end_port, ip4_address_t start_addr,
ip4_address_t end_addr, int is_local);
+clib_error_t *ikev2_set_profile_responder (vlib_main_t * vm, u8 * name,
+ u32 sw_if_index,
+ ip4_address_t ip4);
+clib_error_t *ikev2_set_profile_ike_transforms (vlib_main_t * vm, u8 * name,
+ ikev2_transform_encr_type_t
+ crypto_alg,
+ ikev2_transform_integ_type_t
+ integ_alg,
+ ikev2_transform_dh_type_t
+ dh_type, u32 crypto_key_size);
+clib_error_t *ikev2_set_profile_esp_transforms (vlib_main_t * vm, u8 * name,
+ ikev2_transform_encr_type_t
+ crypto_alg,
+ ikev2_transform_integ_type_t
+ integ_alg,
+ ikev2_transform_dh_type_t
+ dh_type, u32 crypto_key_size);
+clib_error_t *ikev2_set_profile_sa_lifetime (vlib_main_t * vm, u8 * name,
+ u64 lifetime, u32 jitter,
+ u32 handover, u64 maxdata);
+clib_error_t *ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name);
+clib_error_t *ikev2_initiate_delete_child_sa (vlib_main_t * vm, u32 ispi);
+clib_error_t *ikev2_initiate_delete_ike_sa (vlib_main_t * vm, u64 ispi);
+clib_error_t *ikev2_initiate_rekey_child_sa (vlib_main_t * vm, u32 ispi);
+
/* ikev2_format.c */
u8 *format_ikev2_auth_method (u8 * s, va_list * args);
u8 *format_ikev2_id_type (u8 * s, va_list * args);
diff --git a/src/vnet/ipsec/ikev2_cli.c b/src/vnet/ipsec/ikev2_cli.c
index 1369c187775..5c88d8d4681 100644
--- a/src/vnet/ipsec/ikev2_cli.c
+++ b/src/vnet/ipsec/ikev2_cli.c
@@ -173,14 +173,21 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
unformat_input_t * input,
vlib_cli_command_t * cmd)
{
+ vnet_main_t *vnm = vnet_get_main ();
unformat_input_t _line_input, *line_input = &_line_input;
u8 *name = 0;
clib_error_t *r = 0;
u32 id_type;
u8 *data = 0;
u32 tmp1, tmp2, tmp3;
+ u64 tmp4, tmp5;
ip4_address_t ip4;
ip4_address_t end_addr;
+ u32 responder_sw_if_index = (u32) ~ 0;
+ ip4_address_t responder_ip4;
+ ikev2_transform_encr_type_t crypto_alg;
+ ikev2_transform_integ_type_t integ_alg;
+ ikev2_transform_dh_type_t dh_type;
const char *valid_chars = "a-zA-Z0-9_";
@@ -308,6 +315,53 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
ip4, end_addr, /*remote */ 0);
goto done;
}
+ else if (unformat (line_input, "set %U responder %U %U",
+ unformat_token, valid_chars, &name,
+ unformat_vnet_sw_interface, vnm,
+ &responder_sw_if_index, unformat_ip4_address,
+ &responder_ip4))
+ {
+ r =
+ ikev2_set_profile_responder (vm, name, responder_sw_if_index,
+ responder_ip4);
+ goto done;
+ }
+ else
+ if (unformat
+ (line_input,
+ "set %U ike-crypto-alg %U %u ike-integ-alg %U ike-dh %U",
+ unformat_token, valid_chars, &name,
+ unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
+ unformat_ikev2_transform_integ_type, &integ_alg,
+ unformat_ikev2_transform_dh_type, &dh_type))
+ {
+ r =
+ ikev2_set_profile_ike_transforms (vm, name, crypto_alg, integ_alg,
+ dh_type, tmp1);
+ goto done;
+ }
+ else
+ if (unformat
+ (line_input,
+ "set %U esp-crypto-alg %U %u esp-integ-alg %U esp-dh %U",
+ unformat_token, valid_chars, &name,
+ unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
+ unformat_ikev2_transform_integ_type, &integ_alg,
+ unformat_ikev2_transform_dh_type, &dh_type))
+ {
+ r =
+ ikev2_set_profile_esp_transforms (vm, name, crypto_alg, integ_alg,
+ dh_type, tmp1);
+ goto done;
+ }
+ else if (unformat (line_input, "set %U sa-lifetime %lu %u %u %lu",
+ unformat_token, valid_chars, &name,
+ &tmp4, &tmp1, &tmp2, &tmp5))
+ {
+ r =
+ ikev2_set_profile_sa_lifetime (vm, name, tmp4, tmp1, tmp2, tmp5);
+ goto done;
+ }
else
break;
}
@@ -332,7 +386,11 @@ VLIB_CLI_COMMAND (ikev2_profile_add_del_command, static) = {
"ikev2 profile set <id> id <local|remote> <type> <data>\n"
"ikev2 profile set <id> traffic-selector <local|remote> ip-range "
"<start-addr> - <end-addr> port-range <start-port> - <end-port> "
- "protocol <protocol-number>",
+ "protocol <protocol-number>\n"
+ "ikev2 profile set <id> responder <interface> <addr>\n"
+ "ikev2 profile set <id> ike-crypto-alg <crypto alg> <key size> ike-integ-alg <integ alg> ike-dh <dh type>\n"
+ "ikev2 profile set <id> esp-crypto-alg <crypto alg> <key size> esp-integ-alg <integ alg> esp-dh <dh type>\n"
+ "ikev2 profile set <id> sa-lifetime <seconds> <jitter> <handover> <max bytes>",
.function = ikev2_profile_add_del_command_fn,
};
/* *INDENT-ON* */
@@ -462,6 +520,71 @@ VLIB_CLI_COMMAND (set_ikev2_local_key_command, static) = {
};
/* *INDENT-ON* */
+
+static clib_error_t *
+ikev2_initiate_command_fn (vlib_main_t * vm,
+ unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+ unformat_input_t _line_input, *line_input = &_line_input;
+ clib_error_t *r = 0;
+ u8 *name = 0;
+ u32 tmp1;
+ u64 tmp2;
+
+ const char *valid_chars = "a-zA-Z0-9_";
+
+ if (!unformat_user (input, unformat_line_input, line_input))
+ return 0;
+
+ while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat
+ (line_input, "sa-init %U", unformat_token, valid_chars, &name))
+ {
+ r = ikev2_initiate_sa_init (vm, name);
+ goto done;
+ }
+ else if (unformat (line_input, "del-child-sa %x", &tmp1))
+ {
+ r = ikev2_initiate_delete_child_sa (vm, tmp1);
+ goto done;
+ }
+ else if (unformat (line_input, "del-sa %lx", &tmp2))
+ {
+ r = ikev2_initiate_delete_ike_sa (vm, tmp2);
+ goto done;
+ }
+ else if (unformat (line_input, "rekey-child-sa %x", &tmp1))
+ {
+ r = ikev2_initiate_rekey_child_sa (vm, tmp1);
+ goto done;
+ }
+ else
+ break;
+ }
+
+ r = clib_error_return (0, "parse error: '%U'",
+ format_unformat_error, line_input);
+
+done:
+ vec_free (name);
+ unformat_free (line_input);
+ return r;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (ikev2_initiate_command, static) = {
+ .path = "ikev2 initiate",
+ .short_help =
+ "ikev2 initiate sa-init <profile id>\n"
+ "ikev2 initiate del-child-sa <child sa ispi>\n"
+ "ikev2 initiate del-sa <sa ispi>\n"
+ "ikev2 initiate rekey-child-sa <profile id> <child sa ispi>\n",
+ .function = ikev2_initiate_command_fn,
+};
+/* *INDENT-ON* */
+
+
clib_error_t *
ikev2_cli_init (vlib_main_t * vm)
{
diff --git a/src/vnet/ipsec/ikev2_crypto.c b/src/vnet/ipsec/ikev2_crypto.c
index 32927629c5e..c201d3eb41f 100644
--- a/src/vnet/ipsec/ikev2_crypto.c
+++ b/src/vnet/ipsec/ikev2_crypto.c
@@ -343,6 +343,7 @@ ikev2_decrypt_data (ikev2_sa_t * sa, u8 * data, int len)
v8 *r;
int out_len = 0, block_size;
ikev2_sa_transform_t *tr_encr;
+ u8 *key = sa->is_initiator ? sa->sk_er : sa->sk_ei;
tr_encr =
ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
@@ -357,7 +358,7 @@ ikev2_decrypt_data (ikev2_sa_t * sa, u8 * data, int len)
EVP_CIPHER_CTX_init (&ctx);
r = vec_new (u8, len - block_size);
- EVP_DecryptInit_ex (&ctx, tr_encr->cipher, NULL, sa->sk_ei, data);
+ EVP_DecryptInit_ex (&ctx, tr_encr->cipher, NULL, key, data);
EVP_DecryptUpdate (&ctx, r, &out_len, data + block_size, len - block_size);
EVP_DecryptFinal_ex (&ctx, r + out_len, &out_len);
@@ -375,6 +376,7 @@ ikev2_encrypt_data (ikev2_sa_t * sa, v8 * src, u8 * dst)
int out_len;
int bs;
ikev2_sa_transform_t *tr_encr;
+ u8 *key = sa->is_initiator ? sa->sk_ei : sa->sk_er;
tr_encr =
ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
@@ -385,7 +387,7 @@ ikev2_encrypt_data (ikev2_sa_t * sa, v8 * src, u8 * dst)
EVP_CIPHER_CTX_init (&ctx);
- EVP_EncryptInit_ex (&ctx, tr_encr->cipher, NULL, sa->sk_er, dst /* dst */ );
+ EVP_EncryptInit_ex (&ctx, tr_encr->cipher, NULL, key, dst /* dst */ );
EVP_EncryptUpdate (&ctx, dst + bs, &out_len, src, vec_len (src));
EVP_CIPHER_CTX_cleanup (&ctx);
@@ -407,16 +409,29 @@ ikev2_generate_dh (ikev2_sa_t * sa, ikev2_sa_transform_t * t)
BN_hex2bn (&dh->g, t->dh_g);
DH_generate_key (dh);
- sa->r_dh_data = vec_new (u8, t->key_len);
- r = BN_bn2bin (dh->pub_key, sa->r_dh_data);
- ASSERT (r == t->key_len);
+ if (sa->is_initiator)
+ {
+ sa->i_dh_data = vec_new (u8, t->key_len);
+ r = BN_bn2bin (dh->pub_key, sa->i_dh_data);
+ ASSERT (r == t->key_len);
- BIGNUM *ex;
- sa->dh_shared_key = vec_new (u8, t->key_len);
- ex = BN_bin2bn (sa->i_dh_data, vec_len (sa->i_dh_data), NULL);
- r = DH_compute_key (sa->dh_shared_key, ex, dh);
- ASSERT (r == t->key_len);
- BN_clear_free (ex);
+ sa->dh_private_key = vec_new (u8, t->key_len);
+ r = BN_bn2bin (dh->priv_key, sa->dh_private_key);
+ ASSERT (r == t->key_len);
+
+ }
+ else
+ {
+ sa->r_dh_data = vec_new (u8, t->key_len);
+ r = BN_bn2bin (dh->pub_key, sa->r_dh_data);
+ ASSERT (r == t->key_len);
+ BIGNUM *ex;
+ sa->dh_shared_key = vec_new (u8, t->key_len);
+ ex = BN_bin2bn (sa->i_dh_data, vec_len (sa->i_dh_data), NULL);
+ r = DH_compute_key (sa->dh_shared_key, ex, dh);
+ ASSERT (r == t->key_len);
+ BN_clear_free (ex);
+ }
DH_free (dh);
}
else if (t->dh_group == IKEV2_DH_GROUP_ECP)
@@ -439,21 +454,113 @@ ikev2_generate_dh (ikev2_sa_t * sa, ikev2_sa_transform_t * t)
len = t->key_len / 2;
EC_POINT_get_affine_coordinates_GFp (group, r_point, x, y, bn_ctx);
- sa->r_dh_data = vec_new (u8, t->key_len);
- x_off = len - BN_num_bytes (x);
- memset (sa->r_dh_data, 0, x_off);
- BN_bn2bin (x, sa->r_dh_data + x_off);
- y_off = t->key_len - BN_num_bytes (y);
- memset (sa->r_dh_data + len, 0, y_off - len);
- BN_bn2bin (y, sa->r_dh_data + y_off);
+
+ if (sa->is_initiator)
+ {
+ sa->i_dh_data = vec_new (u8, t->key_len);
+ x_off = len - BN_num_bytes (x);
+ memset (sa->i_dh_data, 0, x_off);
+ BN_bn2bin (x, sa->i_dh_data + x_off);
+ y_off = t->key_len - BN_num_bytes (y);
+ memset (sa->i_dh_data + len, 0, y_off - len);
+ BN_bn2bin (y, sa->i_dh_data + y_off);
+
+ const BIGNUM *prv = EC_KEY_get0_private_key (ec);
+ sa->dh_private_key = vec_new (u8, BN_num_bytes (prv));
+ r = BN_bn2bin (prv, sa->dh_private_key);
+ ASSERT (r == BN_num_bytes (prv));
+ }
+ else
+ {
+ sa->r_dh_data = vec_new (u8, t->key_len);
+ x_off = len - BN_num_bytes (x);
+ memset (sa->r_dh_data, 0, x_off);
+ BN_bn2bin (x, sa->r_dh_data + x_off);
+ y_off = t->key_len - BN_num_bytes (y);
+ memset (sa->r_dh_data + len, 0, y_off - len);
+ BN_bn2bin (y, sa->r_dh_data + y_off);
+
+ x = BN_bin2bn (sa->i_dh_data, len, x);
+ y = BN_bin2bn (sa->i_dh_data + len, len, y);
+ EC_POINT_set_affine_coordinates_GFp (group, i_point, x, y, bn_ctx);
+ sa->dh_shared_key = vec_new (u8, t->key_len);
+ EC_POINT_mul (group, shared_point, NULL, i_point,
+ EC_KEY_get0_private_key (ec), NULL);
+ EC_POINT_get_affine_coordinates_GFp (group, shared_point, x, y,
+ bn_ctx);
+ x_off = len - BN_num_bytes (x);
+ memset (sa->dh_shared_key, 0, x_off);
+ BN_bn2bin (x, sa->dh_shared_key + x_off);
+ y_off = t->key_len - BN_num_bytes (y);
+ memset (sa->dh_shared_key + len, 0, y_off - len);
+ BN_bn2bin (y, sa->dh_shared_key + y_off);
+ }
+
+ EC_KEY_free (ec);
+ BN_free (x);
+ BN_free (y);
+ BN_CTX_free (bn_ctx);
+ EC_POINT_free (i_point);
+ EC_POINT_free (shared_point);
+ }
+}
+
+void
+ikev2_complete_dh (ikev2_sa_t * sa, ikev2_sa_transform_t * t)
+{
+ int r;
+
+ if (t->dh_group == IKEV2_DH_GROUP_MODP)
+ {
+ DH *dh = DH_new ();
+ BN_hex2bn (&dh->p, t->dh_p);
+ BN_hex2bn (&dh->g, t->dh_g);
+ dh->priv_key =
+ BN_bin2bn (sa->dh_private_key, vec_len (sa->dh_private_key), NULL);
+
+ BIGNUM *ex;
+ sa->dh_shared_key = vec_new (u8, t->key_len);
+ ex = BN_bin2bn (sa->r_dh_data, vec_len (sa->r_dh_data), NULL);
+ r = DH_compute_key (sa->dh_shared_key, ex, dh);
+ ASSERT (r == t->key_len);
+ BN_clear_free (ex);
+ DH_free (dh);
+ }
+ else if (t->dh_group == IKEV2_DH_GROUP_ECP)
+ {
+ EC_KEY *ec = EC_KEY_new_by_curve_name (t->nid);
+ ASSERT (ec);
+
+ const EC_GROUP *group = EC_KEY_get0_group (ec);
+ BIGNUM *x = NULL, *y = NULL;
+ BN_CTX *bn_ctx = BN_CTX_new ();
+ u16 x_off, y_off, len;
+ BIGNUM *prv;
+
+ prv =
+ BN_bin2bn (sa->dh_private_key, vec_len (sa->dh_private_key), NULL);
+ EC_KEY_set_private_key (ec, prv);
+
+ x = BN_new ();
+ y = BN_new ();
+ len = t->key_len / 2;
+
+ x = BN_bin2bn (sa->r_dh_data, len, x);
+ y = BN_bin2bn (sa->r_dh_data + len, len, y);
+ EC_POINT *r_point = EC_POINT_new (group);
+ EC_POINT_set_affine_coordinates_GFp (group, r_point, x, y, bn_ctx);
+ EC_KEY_set_public_key (ec, r_point);
+
+ EC_POINT *i_point = EC_POINT_new (group);
+ EC_POINT *shared_point = EC_POINT_new (group);
x = BN_bin2bn (sa->i_dh_data, len, x);
y = BN_bin2bn (sa->i_dh_data + len, len, y);
EC_POINT_set_affine_coordinates_GFp (group, i_point, x, y, bn_ctx);
- sa->dh_shared_key = vec_new (u8, t->key_len);
- EC_POINT_mul (group, shared_point, NULL, i_point,
+ EC_POINT_mul (group, shared_point, NULL, r_point,
EC_KEY_get0_private_key (ec), NULL);
EC_POINT_get_affine_coordinates_GFp (group, shared_point, x, y, bn_ctx);
+ sa->dh_shared_key = vec_new (u8, t->key_len);
x_off = len - BN_num_bytes (x);
memset (sa->dh_shared_key, 0, x_off);
BN_bn2bin (x, sa->dh_shared_key + x_off);
@@ -464,8 +571,10 @@ ikev2_generate_dh (ikev2_sa_t * sa, ikev2_sa_transform_t * t)
EC_KEY_free (ec);
BN_free (x);
BN_free (y);
+ BN_free (prv);
BN_CTX_free (bn_ctx);
EC_POINT_free (i_point);
+ EC_POINT_free (r_point);
EC_POINT_free (shared_point);
}
}
diff --git a/src/vnet/ipsec/ikev2_payload.c b/src/vnet/ipsec/ikev2_payload.c
index dd14812b550..34595380ec1 100644
--- a/src/vnet/ipsec/ikev2_payload.c
+++ b/src/vnet/ipsec/ikev2_payload.c
@@ -133,13 +133,28 @@ ikev2_payload_add_data (ikev2_payload_chain_t * c, u8 * data)
void
ikev2_payload_add_notify (ikev2_payload_chain_t * c, u16 msg_type, u8 * data)
{
+ ikev2_payload_add_notify_2(c, msg_type, data, 0);
+}
+
+void
+ikev2_payload_add_notify_2 (ikev2_payload_chain_t * c, u16 msg_type,
+ u8 * data, ikev2_notify_t * notify)
+{
ike_notify_payload_header_t *n;
n =
(ike_notify_payload_header_t *) ikev2_payload_add_hdr (c,
- IKEV2_PAYLOAD_NOTIFY,
- sizeof (*n));
+ IKEV2_PAYLOAD_NOTIFY,
+ sizeof (*n));
n->msg_type = clib_host_to_net_u16 (msg_type);
+ if (notify)
+ {
+ n->protocol_id = notify->protocol_id;
+ if (notify->spi)
+ {
+ n->spi_size = 4;
+ }
+ }
ikev2_payload_add_data (c, data);
}
diff --git a/src/vnet/ipsec/ikev2_priv.h b/src/vnet/ipsec/ikev2_priv.h
index 9f67ad2ae6f..5a3dc5202f4 100644
--- a/src/vnet/ipsec/ikev2_priv.h
+++ b/src/vnet/ipsec/ikev2_priv.h
@@ -109,6 +109,21 @@ typedef struct
typedef struct
{
+ u32 sw_if_index;
+ ip4_address_t ip4;
+} ikev2_responder_t;
+
+typedef struct
+{
+ ikev2_transform_encr_type_t crypto_alg;
+ ikev2_transform_integ_type_t integ_alg;
+ ikev2_transform_dh_type_t dh_type;
+ u32 crypto_key_size;
+} ikev2_transforms_set;
+
+
+typedef struct
+{
ikev2_id_type_t type:8;
u8 *data;
} ikev2_id_t;
@@ -128,6 +143,11 @@ typedef struct
u8 *sk_ar;
u8 *sk_ei;
u8 *sk_er;
+
+ /* lifetime data */
+ f64 time_to_expiration;
+ u8 is_expired;
+ i8 rekey_retries;
} ikev2_child_sa_t;
typedef struct
@@ -140,6 +160,7 @@ typedef struct
{
u8 protocol_id;
u32 spi;
+ u32 ispi;
ikev2_sa_proposal_t *i_proposal;
ikev2_sa_proposal_t *r_proposal;
ikev2_ts_t *tsi;
@@ -154,6 +175,24 @@ typedef struct
u8 *data;
} ikev2_notify_t;
+typedef struct
+{
+ u8 *name;
+ u8 is_enabled;
+
+ ikev2_auth_t auth;
+ ikev2_id_t loc_id;
+ ikev2_id_t rem_id;
+ ikev2_ts_t loc_ts;
+ ikev2_ts_t rem_ts;
+ ikev2_responder_t responder;
+ ikev2_transforms_set ike_ts;
+ ikev2_transforms_set esp_ts;
+ u64 lifetime;
+ u64 lifetime_maxdata;
+ u32 lifetime_jitter;
+ u32 handover;
+} ikev2_profile_t;
typedef struct
{
@@ -170,6 +209,7 @@ typedef struct
/* DH data */
u16 dh_group;
u8 *dh_shared_key;
+ u8 *dh_private_key;
u8 *i_dh_data;
u8 *r_dh_data;
@@ -208,20 +248,13 @@ typedef struct
u32 last_msg_id;
u8 *last_res_packet_data;
+ u8 is_initiator;
+ u32 last_init_msg_id;
+ ikev2_profile_t *profile;
+
ikev2_child_sa_t *childs;
} ikev2_sa_t;
-typedef struct
-{
- u8 *name;
- u8 is_enabled;
-
- ikev2_auth_t auth;
- ikev2_id_t loc_id;
- ikev2_id_t rem_id;
- ikev2_ts_t loc_ts;
- ikev2_ts_t rem_ts;
-} ikev2_profile_t;
typedef struct
{
@@ -250,6 +283,11 @@ typedef struct
vlib_main_t *vlib_main;
vnet_main_t *vnet_main;
+ /* pool of IKEv2 Security Associations created in initiator mode */
+ ikev2_sa_t *sais;
+ /* hash */
+ uword *sa_by_ispi;
+
ikev2_main_per_thread_data_t *per_thread_data;
} ikev2_main_t;
@@ -269,6 +307,7 @@ v8 *ikev2_calc_integr (ikev2_sa_transform_t * tr, v8 * key, u8 * data,
v8 *ikev2_decrypt_data (ikev2_sa_t * sa, u8 * data, int len);
int ikev2_encrypt_data (ikev2_sa_t * sa, v8 * src, u8 * dst);
void ikev2_generate_dh (ikev2_sa_t * sa, ikev2_sa_transform_t * t);
+void ikev2_complete_dh (ikev2_sa_t * sa, ikev2_sa_transform_t * t);
int ikev2_verify_sign (EVP_PKEY * pkey, u8 * sigbuf, u8 * data);
u8 *ikev2_calc_sign (EVP_PKEY * pkey, u8 * data);
EVP_PKEY *ikev2_load_cert_file (u8 * file);
@@ -291,6 +330,8 @@ typedef struct
void ikev2_payload_add_notify (ikev2_payload_chain_t * c, u16 msg_type,
u8 * data);
+void ikev2_payload_add_notify_2 (ikev2_payload_chain_t * c, u16 msg_type,
+ u8 * data, ikev2_notify_t * notify);
void ikev2_payload_add_sa (ikev2_payload_chain_t * c,
ikev2_sa_proposal_t * proposals);
void ikev2_payload_add_ke (ikev2_payload_chain_t * c, u16 dh_group,
diff --git a/src/vnet/ipsec/ipsec.api b/src/vnet/ipsec/ipsec.api
index 178bb757168..ef090f84c8f 100644
--- a/src/vnet/ipsec/ipsec.api
+++ b/src/vnet/ipsec/ipsec.api
@@ -394,6 +394,234 @@ define ikev2_set_local_key_reply
i32 retval;
};
+/** \brief IKEv2: Set IKEv2 responder interface and IP address
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+
+ @param name - IKEv2 profile name
+ @param sw_if_index - interface index
+ @param address - interface address
+*/
+define ikev2_set_responder
+{
+ u32 client_index;
+ u32 context;
+
+ u8 name[64];
+ u32 sw_if_index;
+ u8 address[4];
+};
+
+/** \brief Reply for IKEv2: Set IKEv2 responder interface and IP address
+ @param context - returned sender context, to match reply w/ request
+ @param retval - return code
+*/
+define ikev2_set_responder_reply
+{
+ u32 context;
+ i32 retval;
+};
+
+
+/** \brief IKEv2: Set IKEv2 IKE transforms in SA_INIT proposal (RFC 7296)
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+
+ @param name - IKEv2 profile name
+ @param crypto_alg - encryption algorithm
+ @param crypto_key_size - encryption key size
+ @param integ_alg - integrity algorithm
+ @param dh_group - Diffie-Hellman group
+
+*/
+define ikev2_set_ike_transforms
+{
+ u32 client_index;
+ u32 context;
+
+ u8 name[64];
+ u32 crypto_alg;
+ u32 crypto_key_size;
+ u32 integ_alg;
+ u32 dh_group;
+};
+
+/** \brief Reply for IKEv2: Set IKEv2 IKE transforms
+ @param context - returned sender context, to match reply w/ request
+ @param retval - return code
+*/
+define ikev2_set_ike_transforms_reply
+{
+ u32 context;
+ i32 retval;
+};
+
+/** \brief IKEv2: Set IKEv2 ESP transforms in SA_INIT proposal (RFC 7296)
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+
+ @param name - IKEv2 profile name
+ @param crypto_alg - encryption algorithm
+ @param crypto_key_size - encryption key size
+ @param integ_alg - integrity algorithm
+ @param dh_group - Diffie-Hellman group
+
+*/
+define ikev2_set_esp_transforms
+{
+ u32 client_index;
+ u32 context;
+
+ u8 name[64];
+ u32 crypto_alg;
+ u32 crypto_key_size;
+ u32 integ_alg;
+ u32 dh_group;
+};
+
+/** \brief Reply for IKEv2: Set IKEv2 ESP transforms
+ @param context - returned sender context, to match reply w/ request
+ @param retval - return code
+*/
+define ikev2_set_esp_transforms_reply
+{
+ u32 context;
+ i32 retval;
+};
+
+/** \brief IKEv2: Set Child SA lifetime, limited by time and/or data
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+
+ @param name - IKEv2 profile name
+ @param lifetime - SA maximum life time in seconds (0 to disable)
+ @param lifetime_jitter - Jitter added to prevent simultaneounus rekeying
+ @param handover - Hand over time
+ @param lifetime_maxdata - SA maximum life time in bytes (0 to disable)
+
+*/
+define ikev2_set_sa_lifetime
+{
+ u32 client_index;
+ u32 context;
+
+ u8 name[64];
+ u64 lifetime;
+ u32 lifetime_jitter;
+ u32 handover;
+ u64 lifetime_maxdata;
+};
+
+/** \brief Reply for IKEv2: Set Child SA lifetime
+ @param context - returned sender context, to match reply w/ request
+ @param retval - return code
+*/
+define ikev2_set_sa_lifetime_reply
+{
+ u32 context;
+ i32 retval;
+};
+
+/** \brief IKEv2: Initiate the SA_INIT exchange
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+
+ @param name - IKEv2 profile name
+
+*/
+define ikev2_initiate_sa_init
+{
+ u32 client_index;
+ u32 context;
+
+ u8 name[64];
+};
+
+/** \brief Reply for IKEv2: Initiate the SA_INIT exchange
+ @param context - returned sender context, to match reply w/ request
+ @param retval - return code
+*/
+define ikev2_initiate_sa_init_reply
+{
+ u32 context;
+ i32 retval;
+};
+
+/** \brief IKEv2: Initiate the delete IKE SA exchange
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+
+ @param ispi - IKE SA initiator SPI
+
+*/
+define ikev2_initiate_del_ike_sa
+{
+ u32 client_index;
+ u32 context;
+
+ u64 ispi;
+};
+
+/** \brief Reply for IKEv2: Initiate the delete IKE SA exchange
+ @param context - returned sender context, to match reply w/ request
+ @param retval - return code
+*/
+define ikev2_initiate_del_ike_sa_reply
+{
+ u32 context;
+ i32 retval;
+};
+
+/** \brief IKEv2: Initiate the delete Child SA exchange
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+
+ @param ispi - Child SA initiator SPI
+
+*/
+define ikev2_initiate_del_child_sa
+{
+ u32 client_index;
+ u32 context;
+
+ u32 ispi;
+};
+
+/** \brief Reply for IKEv2: Initiate the delete Child SA exchange
+ @param context - returned sender context, to match reply w/ request
+ @param retval - return code
+*/
+define ikev2_initiate_del_child_sa_reply
+{
+ u32 context;
+ i32 retval;
+};
+
+/** \brief IKEv2: Initiate the rekey Child SA exchange
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+
+ @param ispi - Child SA initiator SPI
+
+*/
+define ikev2_initiate_rekey_child_sa
+{
+ u32 client_index;
+ u32 context;
+
+ u32 ispi;
+};
+
+/** \brief Reply for IKEv2: Initiate the rekey Child SA exchange
+ @param context - returned sender context, to match reply w/ request
+ @param retval - return code
+*/
+define ikev2_initiate_rekey_child_sa_reply
+{
+ u32 context;
+ i32 retval;
+};
+
/** \brief Dump ipsec policy database data
@param client_index - opaque cookie to identify the sender
@param context - sender context, to match reply w/ request
diff --git a/src/vnet/ipsec/ipsec.h b/src/vnet/ipsec/ipsec.h
index 6726dba08ae..58f0f145a5a 100644
--- a/src/vnet/ipsec/ipsec.h
+++ b/src/vnet/ipsec/ipsec.h
@@ -127,6 +127,9 @@ typedef struct
u32 last_seq;
u32 last_seq_hi;
u64 replay_window;
+
+ /*lifetime data */
+ u64 total_data_size;
} ipsec_sa_t;
typedef struct
diff --git a/src/vnet/ipsec/ipsec_api.c b/src/vnet/ipsec/ipsec_api.c
index 30732266ed4..49b475cf3b6 100644
--- a/src/vnet/ipsec/ipsec_api.c
+++ b/src/vnet/ipsec/ipsec_api.c
@@ -58,7 +58,15 @@ _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del) \
_(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth) \
_(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id) \
_(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts) \
-_(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)
+_(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key) \
+_(IKEV2_SET_RESPONDER, ikev2_set_responder) \
+_(IKEV2_SET_IKE_TRANSFORMS, ikev2_set_ike_transforms) \
+_(IKEV2_SET_ESP_TRANSFORMS, ikev2_set_esp_transforms) \
+_(IKEV2_SET_SA_LIFETIME, ikev2_set_sa_lifetime) \
+_(IKEV2_INITIATE_SA_INIT, ikev2_initiate_sa_init) \
+_(IKEV2_INITIATE_DEL_IKE_SA, ikev2_initiate_del_ike_sa) \
+_(IKEV2_INITIATE_DEL_CHILD_SA, ikev2_initiate_del_child_sa) \
+_(IKEV2_INITIATE_REKEY_CHILD_SA, ikev2_initiate_rekey_child_sa)
static void vl_api_ipsec_spd_add_del_t_handler
(vl_api_ipsec_spd_add_del_t * mp)
@@ -461,6 +469,194 @@ vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
}
+static void
+vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
+{
+ vl_api_ikev2_set_responder_reply_t *rmp;
+ int rv = 0;
+
+#if WITH_LIBSSL > 0
+ vlib_main_t *vm = vlib_get_main ();
+ clib_error_t *error;
+
+ u8 *tmp = format (0, "%s", mp->name);
+ ip4_address_t ip4;
+ clib_memcpy (&ip4, mp->address, sizeof (ip4));
+
+ error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
+ vec_free (tmp);
+ if (error)
+ rv = VNET_API_ERROR_UNSPECIFIED;
+#else
+ rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+ REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
+}
+
+static void
+vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
+ mp)
+{
+ vl_api_ikev2_set_ike_transforms_reply_t *rmp;
+ int rv = 0;
+
+#if WITH_LIBSSL > 0
+ vlib_main_t *vm = vlib_get_main ();
+ clib_error_t *error;
+
+ u8 *tmp = format (0, "%s", mp->name);
+
+ error =
+ ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
+ mp->dh_group, mp->crypto_key_size);
+ vec_free (tmp);
+ if (error)
+ rv = VNET_API_ERROR_UNSPECIFIED;
+#else
+ rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+ REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
+}
+
+static void
+vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
+ mp)
+{
+ vl_api_ikev2_set_esp_transforms_reply_t *rmp;
+ int rv = 0;
+
+#if WITH_LIBSSL > 0
+ vlib_main_t *vm = vlib_get_main ();
+ clib_error_t *error;
+
+ u8 *tmp = format (0, "%s", mp->name);
+
+ error =
+ ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
+ mp->dh_group, mp->crypto_key_size);
+ vec_free (tmp);
+ if (error)
+ rv = VNET_API_ERROR_UNSPECIFIED;
+#else
+ rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+ REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
+}
+
+static void
+vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
+{
+ vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
+ int rv = 0;
+
+#if WITH_LIBSSL > 0
+ vlib_main_t *vm = vlib_get_main ();
+ clib_error_t *error;
+
+ u8 *tmp = format (0, "%s", mp->name);
+
+ error =
+ ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
+ mp->handover, mp->lifetime_maxdata);
+ vec_free (tmp);
+ if (error)
+ rv = VNET_API_ERROR_UNSPECIFIED;
+#else
+ rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+ REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
+}
+
+static void
+vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
+{
+ vl_api_ikev2_initiate_sa_init_reply_t *rmp;
+ int rv = 0;
+
+#if WITH_LIBSSL > 0
+ vlib_main_t *vm = vlib_get_main ();
+ clib_error_t *error;
+
+ u8 *tmp = format (0, "%s", mp->name);
+
+ error = ikev2_initiate_sa_init (vm, tmp);
+ vec_free (tmp);
+ if (error)
+ rv = VNET_API_ERROR_UNSPECIFIED;
+#else
+ rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+ REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
+}
+
+static void
+vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
+ * mp)
+{
+ vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
+ int rv = 0;
+
+#if WITH_LIBSSL > 0
+ vlib_main_t *vm = vlib_get_main ();
+ clib_error_t *error;
+
+ error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
+ if (error)
+ rv = VNET_API_ERROR_UNSPECIFIED;
+#else
+ rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+ REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
+}
+
+static void
+ vl_api_ikev2_initiate_del_child_sa_t_handler
+ (vl_api_ikev2_initiate_del_child_sa_t * mp)
+{
+ vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
+ int rv = 0;
+
+#if WITH_LIBSSL > 0
+ vlib_main_t *vm = vlib_get_main ();
+ clib_error_t *error;
+
+ error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
+ if (error)
+ rv = VNET_API_ERROR_UNSPECIFIED;
+#else
+ rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+ REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
+}
+
+static void
+ vl_api_ikev2_initiate_rekey_child_sa_t_handler
+ (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
+{
+ vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
+ int rv = 0;
+
+#if WITH_LIBSSL > 0
+ vlib_main_t *vm = vlib_get_main ();
+ clib_error_t *error;
+
+ error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
+ if (error)
+ rv = VNET_API_ERROR_UNSPECIFIED;
+#else
+ rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+ REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
+}
+
/*
* ipsec_api_hookup
* Add vpe's API message handlers to the table.