#include <plugins/ikev2/ikev2_priv.h>
#include <openssl/sha.h>
+#define IKEV2_LIVENESS_RETRIES 3
+#define IKEV2_LIVENESS_PERIOD_CHECK 30
+
ikev2_main_t ikev2_main;
static int ikev2_delete_tunnel_interface (vnet_main_t * vnm,
ikev2_sa_transform_t *t = 0, *t2;
ikev2_main_t *km = &ikev2_main;
+ sai->init_response_received = 1;
/*move some data to the new SA */
#define _(A) ({void* __tmp__ = (A); (A) = 0; __tmp__;})
sa->last_sa_init_req_packet_data = _(sai->last_sa_init_req_packet_data);
sa->childs = _(sai->childs);
sa->udp_encap = sai->udp_encap;
+ sa->dst_port = sai->dst_port;
#undef _
ike_payload_header_t *ikep;
u32 plen;
+ sa->liveness_retries = 0;
ikev2_elog_exchange ("ispi %lx rspi %lx INFORMATIONAL received "
"from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
clib_host_to_net_u64 (ike->rspi), sa->iaddr.as_u32);
}
}
- if (sel_p)
- sa->udp_encap = sel_p->udp_encap;
-
vec_free(auth);
vec_free(psk);
}));
/* *INDENT-ON* */
+ if (sel_p)
+ {
+ sa->udp_encap = sel_p->udp_encap;
+ sa->dst_port = sel_p->dst_port;
+ }
vec_free (authmsg);
if (sa->state == IKEV2_STATE_AUTHENTICATED)
ipsec_key_t loc_ckey, rem_ckey, loc_ikey, rem_ikey;
u8 is_rekey;
u32 old_remote_sa_id;
+ u16 dst_port;
} ikev2_add_ipsec_tunnel_args_t;
static void
IPSEC_PROTOCOL_ESP, a->encr_type,
&a->loc_ckey, a->integ_type, &a->loc_ikey,
a->flags, 0, a->salt_local, &a->local_ip,
- &a->remote_ip, NULL);
+ &a->remote_ip, NULL, a->dst_port);
rv |= ipsec_sa_add_and_lock (a->remote_sa_id, a->remote_spi,
IPSEC_PROTOCOL_ESP, a->encr_type, &a->rem_ckey,
a->integ_type, &a->rem_ikey,
(a->flags | IPSEC_SA_FLAG_IS_INBOUND), 0,
a->salt_remote, &a->remote_ip,
- &a->local_ip, NULL);
+ &a->local_ip, NULL, a->dst_port);
rv |= ipsec_tun_protect_update (sw_if_index, NULL, a->local_sa_id, sas_in);
}
static int
-ikev2_create_tunnel_interface (vnet_main_t * vnm,
+ikev2_create_tunnel_interface (vlib_main_t * vm,
u32 thread_index,
ikev2_sa_t * sa,
ikev2_child_sa_t * child, u32 sa_index,
}
a.flags = IPSEC_SA_FLAG_USE_ANTI_REPLAY;
- a.flags |= IPSEC_SA_FLAG_IS_TUNNEL;
if (sa->udp_encap)
- a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
+ {
+ a.flags |= IPSEC_SA_FLAG_IS_TUNNEL;
+ a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
+ }
a.is_rekey = is_rekey;
tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
if (p && p->lifetime)
{
- child->time_to_expiration =
- vlib_time_now (vnm->vlib_main) + p->lifetime;
+ child->time_to_expiration = vlib_time_now (vm) + p->lifetime;
if (p->lifetime_jitter)
{
// This is not much better than rand(3), which Coverity warns
// however fast. If this perturbance to the expiration time
// needs to use a better RNG then we may need to use something
// like /dev/urandom which has significant overhead.
- u32 rnd = (u32) (vlib_time_now (vnm->vlib_main) * 1e6);
+ u32 rnd = (u32) (vlib_time_now (vm) * 1e6);
rnd = random_u32 (&rnd);
child->time_to_expiration += 1 + (rnd % p->lifetime_jitter);
child->remote_sa_id = a.remote_sa_id = remote_sa_id;
a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
+ a.dst_port = sa->dst_port;
vl_api_rpc_call_main_thread (ikev2_add_tunnel_from_main,
(u8 *) & a, sizeof (a));
return -1;
}
+static void
+ikev2_init_sa (vlib_main_t * vm, ikev2_sa_t * sa)
+{
+ ikev2_main_t *km = &ikev2_main;
+ sa->liveness_period_check = vlib_time_now (vm) + km->liveness_period;
+}
static uword
ikev2_node_fn (vlib_main_t * vm,
pool_get (km->per_thread_data[thread_index].sas,
sa0);
clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
+ ikev2_init_sa (vm, sa0);
hash_set (km->
per_thread_data[thread_index].sa_by_rspi,
sa0->rspi,
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 (sai->init_response_received)
+ {
+ /* we've already processed sa-init response */
+ sa0->state = IKEV2_STATE_UNKNOWN;
+ }
+ else
+ {
+ ikev2_complete_sa_data (sa0, sai);
+ ikev2_calc_keys (sa0);
+ ikev2_sa_auth_init (sa0);
+ len = ikev2_generate_message (sa0, ike0, 0);
+ }
}
}
ikev2_initial_contact_cleanup (sa0);
ikev2_sa_match_ts (sa0);
if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
- ikev2_create_tunnel_interface (km->vnet_main,
- thread_index, sa0,
+ ikev2_create_tunnel_interface (vm, thread_index, sa0,
&sa0->childs[0],
p[0], 0, 0);
}
}
}
}
- if (!sa0->is_initiator)
+ if (!(ike0->flags & IKEV2_HDR_FLAG_RESPONSE))
{
+ ike0->flags |= IKEV2_HDR_FLAG_RESPONSE;
len = ikev2_generate_message (sa0, ike0, 0);
}
}
child->i_proposals = sa0->rekey[0].i_proposal;
child->tsi = sa0->rekey[0].tsi;
child->tsr = sa0->rekey[0].tsr;
- ikev2_create_tunnel_interface (km->vnet_main,
- thread_index, sa0,
- child, p[0],
+ ikev2_create_tunnel_interface (vm, thread_index,
+ sa0, child, p[0],
child - sa0->childs,
1);
}
return r;
}
- if (IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16 != ts->crypto_alg)
+ if (is_ike || IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16 != ts->crypto_alg)
{
/* Integrity */
error = 1;
return 0;
}
+static_always_inline vnet_api_error_t
+ikev2_register_udp_port (ikev2_profile_t * p, u16 port)
+{
+ ikev2_main_t *km = &ikev2_main;
+ udp_dst_port_info_t *pi;
+
+ uword *v = hash_get (km->udp_ports, port);
+ pi = udp_get_dst_port_info (&udp_main, port, UDP_IP4);
+
+ if (v)
+ {
+ /* IKE already uses this port, only increment reference counter */
+ ASSERT (pi);
+ v[0]++;
+ }
+ else
+ {
+ if (pi)
+ return VNET_API_ERROR_UDP_PORT_TAKEN;
+
+ udp_register_dst_port (km->vlib_main, port,
+ ipsec4_tun_input_node.index, 1);
+ hash_set (km->udp_ports, port, 1);
+ }
+ p->dst_port = port;
+ return 0;
+}
+
+static_always_inline void
+ikev2_unregister_udp_port (ikev2_profile_t * p)
+{
+ ikev2_main_t *km = &ikev2_main;
+ uword *v;
+
+ if (p->dst_port == IPSEC_UDP_PORT_NONE)
+ return;
+
+ v = hash_get (km->udp_ports, p->dst_port);
+ if (!v)
+ return;
+
+ v[0]--;
+
+ if (v[0] == 0)
+ {
+ udp_unregister_dst_port (km->vlib_main, p->dst_port, 1);
+ hash_unset (km->udp_ports, p->dst_port);
+ }
+
+ p->dst_port = IPSEC_UDP_PORT_NONE;
+}
+
clib_error_t *
ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add)
{
pool_get (km->profiles, p);
clib_memset (p, 0, sizeof (*p));
p->name = vec_dup (name);
+ p->dst_port = IPSEC_UDP_PORT_NONE;
p->responder.sw_if_index = ~0;
p->tun_itf = ~0;
uword index = p - km->profiles;
if (!p)
return clib_error_return (0, "policy %v does not exists", name);
+ ikev2_unregister_udp_port (p);
+
vec_free (p->name);
pool_put (km->profiles, p);
mhash_unset (&km->profile_index_by_name, name, 0);
return 0;
}
+vnet_api_error_t
+ikev2_set_profile_ipsec_udp_port (vlib_main_t * vm, u8 * name, u16 port,
+ u8 is_set)
+{
+ ikev2_profile_t *p = ikev2_profile_index_by_name (name);
+ ikev2_main_t *km = &ikev2_main;
+ vnet_api_error_t rv = 0;
+ uword *v;
+
+ if (!p)
+ return VNET_API_ERROR_INVALID_VALUE;
+
+ if (is_set)
+ {
+ if (p->dst_port != IPSEC_UDP_PORT_NONE)
+ return VNET_API_ERROR_VALUE_EXIST;
+
+ rv = ikev2_register_udp_port (p, port);
+ }
+ else
+ {
+ v = hash_get (km->udp_ports, port);
+ if (!v)
+ return VNET_API_ERROR_IKE_NO_PORT;
+
+ if (p->dst_port == IPSEC_UDP_PORT_NONE)
+ return VNET_API_ERROR_INVALID_VALUE;
+
+ ikev2_unregister_udp_port (p);
+ }
+ return rv;
+}
+
clib_error_t *
ikev2_set_profile_udp_encap (vlib_main_t * vm, u8 * name)
{
ikev2_sa_free_proposal_vector (&proposals);
sa.is_initiator = 1;
- sa.profile_index = km->profiles - p;
+ sa.profile_index = p - km->profiles;
sa.is_profile_index_set = 1;
sa.state = IKEV2_STATE_SA_INIT;
sa.tun_itf = p->tun_itf;
sa.udp_encap = p->udp_encap;
+ sa.dst_port = p->dst_port;
sa.is_tun_itf_set = 1;
sa.initial_contact = 1;
ikev2_generate_sa_init_data (&sa);
km->vnet_main = vnet_get_main ();
km->vlib_main = vm;
+ km->liveness_period = IKEV2_LIVENESS_PERIOD_CHECK;
+ km->liveness_max_retries = IKEV2_LIVENESS_RETRIES;
ikev2_crypto_init (km);
mhash_init_vec_string (&km->profile_index_by_name, sizeof (uword));
km->sa_by_ispi = hash_create (0, sizeof (uword));
km->sw_if_indices = hash_create (0, 0);
+ km->udp_ports = hash_create (0, sizeof (uword));
udp_register_dst_port (vm, 500, ikev2_node.index, 1);
return 0;
}
+clib_error_t *
+ikev2_set_liveness_params (u32 period, u32 max_retries)
+{
+ ikev2_main_t *km = &ikev2_main;
+
+ if (period == 0 || max_retries == 0)
+ return clib_error_return (0, "invalid args");
+
+ km->liveness_period = period;
+ km->liveness_max_retries = max_retries;
+ return 0;
+}
+
static void
ikev2_mngr_process_ipsec_sa (ipsec_sa_t * ipsec_sa)
{
hash_foreach (ispi, sai, km->sa_by_ispi,
({
sa = pool_elt_at_index (km->sais, sai);
+ if (sa->init_response_received)
+ continue;
+
u32 bi0;
if (vlib_buffer_alloc (km->vlib_main, &bi0, 1) != 1)
return;
static vlib_node_registration_t ikev2_mngr_process_node;
+static void
+ikev2_send_informational_request (ikev2_sa_t * sa)
+{
+ ikev2_main_t *km = &ikev2_main;
+ ip4_address_t *src, *dst;
+ ike_header_t *ike0;
+ u32 bi0 = 0;
+ int len;
+
+ bi0 = ikev2_get_new_ike_header_buff (km->vlib_main, &ike0);
+
+ ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
+ 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);
+ len = ikev2_generate_message (sa, ike0, 0);
+
+ if (sa->is_initiator)
+ {
+ src = &sa->iaddr;
+ dst = &sa->raddr;
+ }
+ else
+ {
+
+ dst = &sa->iaddr;
+ src = &sa->raddr;
+ }
+
+ ikev2_send_ike (km->vlib_main, src, dst, bi0, len);
+}
+
+static_always_inline int
+ikev2_mngr_process_responder_sas (ikev2_sa_t * sa)
+{
+ ikev2_main_t *km = &ikev2_main;
+ vlib_main_t *vm = km->vlib_main;
+
+ if (!sa->sk_ai || !sa->sk_ar)
+ return 0;
+
+ if (sa->liveness_retries >= km->liveness_max_retries)
+ return 1;
+
+ f64 now = vlib_time_now (vm);
+
+ if (sa->liveness_period_check < now)
+ {
+ sa->liveness_retries++;
+ sa->liveness_period_check = now + km->liveness_period;
+ ikev2_send_informational_request (sa);
+ }
+ return 0;
+}
+
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;
+ ikev2_profile_t *p;
+ ikev2_child_sa_t *c;
+ u32 *sai;
while (1)
{
vec_foreach (tkm, km->per_thread_data)
{
ikev2_sa_t *sa;
+ u32 *to_be_deleted = 0;
+
/* *INDENT-OFF* */
pool_foreach (sa, tkm->sas, ({
ikev2_child_sa_t *c;
{
req_sent |= ikev2_mngr_process_child_sa(sa, c, del_old_ids);
}
+
+ if (ikev2_mngr_process_responder_sas (sa))
+ vec_add1 (to_be_deleted, sa - tkm->sas);
}));
/* *INDENT-ON* */
+
+ vec_foreach (sai, to_be_deleted)
+ {
+ sa = pool_elt_at_index (tkm->sas, sai[0]);
+ if (sa->is_initiator && sa->is_profile_index_set)
+ {
+ p = pool_elt_at_index (km->profiles, sa->profile_index);
+ if (p)
+ {
+ ikev2_initiate_sa_init (vm, p->name);
+ continue;
+ }
+ }
+ vec_foreach (c, sa->childs)
+ ikev2_delete_tunnel_interface (km->vnet_main, sa, c);
+ hash_unset (tkm->sa_by_rspi, sa->rspi);
+ pool_put (tkm->sas, sa);
+ }
}
/* process ipsec sas */