diff options
-rw-r--r-- | src/plugins/ikev2/ikev2.api | 125 | ||||
-rw-r--r-- | src/plugins/ikev2/ikev2_api.c | 277 | ||||
-rw-r--r-- | src/plugins/ikev2/ikev2_test.c | 351 | ||||
-rw-r--r-- | src/plugins/ikev2/ikev2_types.api | 67 | ||||
-rw-r--r-- | src/plugins/ikev2/test/test_ikev2.py | 93 |
5 files changed, 907 insertions, 6 deletions
diff --git a/src/plugins/ikev2/ikev2.api b/src/plugins/ikev2/ikev2.api index 2a1a6d6365b..1e1dbf9589d 100644 --- a/src/plugins/ikev2/ikev2.api +++ b/src/plugins/ikev2/ikev2.api @@ -1,6 +1,6 @@ /* Hey Emacs use -*- mode: C -*- */ /* - * Copyright (c) 2015-2016 Cisco and/or its affiliates. + * Copyright (c) 2015-2020 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -64,6 +64,129 @@ define ikev2_profile_details option status="in_progress"; }; +/** \brief Dump all SAs + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request +*/ +define ikev2_sa_dump +{ + u32 client_index; + u32 context; + + option status = "in_progress"; +}; + +/** \brief Details about IKE SA + @param context - sender context, to match reply w/ request + @param retval - return code + @param sa - SA data +*/ +define ikev2_sa_details +{ + u32 context; + i32 retval; + + vl_api_ikev2_sa_t sa; + option status = "in_progress"; +}; + +/** \brief Dump child SA of specific SA + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sa_index - index of specific sa +*/ +define ikev2_child_sa_dump +{ + u32 client_index; + u32 context; + + u32 sa_index; + option vat_help = "sa_index <index>"; + option status = "in_progress"; +}; + +/** \brief Child SA details + @param context - sender context, to match reply w/ request + @param retval - return code + @param child_sa - child SA data +*/ +define ikev2_child_sa_details +{ + u32 context; + i32 retval; + + vl_api_ikev2_child_sa_t child_sa; + option status = "in_progress"; +}; + +/** \brief get specific nonce + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param is_initiator - specify type initiator|responder of nonce + @param sa_index - index of specific sa +*/ +define ikev2_nonce_get +{ + u32 client_index; + u32 context; + + bool is_initiator; + u32 sa_index; + option vat_help = "initiator|responder sa_index <index>"; + option status = "in_progress"; +}; + +/** \brief reply on specific nonce + @param context - sender context, to match reply w/ request + @param retval - return code + @param data_len - nonce length + @param nonce - nonce data +*/ + +define ikev2_nonce_get_reply +{ + u32 context; + i32 retval; + + u32 data_len; + u8 nonce[data_len]; + option status = "in_progress"; +}; + +/** \brief dump traffic selectors + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param is_initiator - specify type initiator|responder of nonce + @param sa_index - index of specific sa + @param child_sa_index - index of specific sa child of specific sa +*/ + +define ikev2_traffic_selector_dump +{ + u32 client_index; + u32 context; + + bool is_initiator; + u32 sa_index; + u32 child_sa_index; + option vat_help = "initiator|responder sa_index <index> child_sa_index <index>"; + option status = "in_progress"; +}; + +/** \brief details on specific traffic selector + @param context - sender context, to match reply w/ request + @param retval - return code + @param ts - traffic selector data +*/ + +define ikev2_traffic_selector_details +{ + u32 context; + i32 retval; + + vl_api_ikev2_ts_t ts; + option status = "in_progress"; +}; /** \brief IKEv2: Add/delete profile @param client_index - opaque cookie to identify the sender diff --git a/src/plugins/ikev2/ikev2_api.c b/src/plugins/ikev2/ikev2_api.c index f1ff40a8a0a..c73505b2421 100644 --- a/src/plugins/ikev2/ikev2_api.c +++ b/src/plugins/ikev2/ikev2_api.c @@ -32,6 +32,7 @@ #define vl_endianfun /* define message structures */ +#include <plugins/ikev2/ikev2.api.h> #include <plugins/ikev2/ikev2_types.api.h> #undef vl_endianfun @@ -42,6 +43,19 @@ extern ikev2_main_t ikev2_main; #define REPLY_MSG_ID_BASE ikev2_main.msg_id_base #include <vlibapi/api_helper_macros.h> +static u32 +ikev2_encode_sa_index (u32 sai, u32 ti) +{ + return (ti << 16) | sai; +} + +static void +ikev2_decode_sa_index (u32 api_sai, u32 * sai, u32 * ti) +{ + *sai = api_sai & 0xffff; + *ti = api_sai >> 16; +} + static void cp_ike_transforms (vl_api_ikev2_ike_transforms_t * vl_api_ts, ikev2_transforms_set * ts) @@ -105,6 +119,18 @@ cp_responder (vl_api_ikev2_responder_t * vl_api_responder, ip4_address_encode (&responder->ip4, vl_api_responder->ip4); } +void +cp_sa_transform (vl_api_ikev2_sa_transform_t * vl_tr, + ikev2_sa_transform_t * tr) +{ + vl_tr->transform_type = tr->type; + vl_tr->key_len = tr->key_len; + vl_tr->key_trunc = tr->key_trunc; + vl_tr->block_size = tr->block_size; + vl_tr->dh_group = tr->dh_group; + vl_tr->transform_id = tr->encr_type; +} + static void send_profile (ikev2_profile_t * profile, vl_api_registration_t * reg, u32 context) @@ -169,6 +195,257 @@ vl_api_ikev2_profile_dump_t_handler (vl_api_ikev2_profile_dump_t * mp) } static void +send_sa (ikev2_sa_t * sa, vl_api_ikev2_sa_dump_t * mp, u32 api_sa_index) +{ + vl_api_ikev2_sa_details_t *rmp = 0; + int rv = 0; + ikev2_sa_transform_t *tr; + + /* *INDENT-OFF* */ + REPLY_MACRO2_ZERO (VL_API_IKEV2_SA_DETAILS, + { + vl_api_ikev2_sa_t *rsa = &rmp->sa; + vl_api_ikev2_keys_t* k = &rsa->keys; + rsa->profile_index = rsa->profile_index; + rsa->sa_index = api_sa_index; + ip4_address_encode (&sa->iaddr, rsa->iaddr); + ip4_address_encode (&sa->raddr, rsa->raddr); + rsa->ispi = sa->ispi; + rsa->rspi = sa->rspi; + cp_id(&rsa->i_id, &sa->i_id); + cp_id(&rsa->r_id, &sa->r_id); + + tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR); + if (tr) + cp_sa_transform (&rsa->encryption, tr); + + tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF); + if (tr) + cp_sa_transform (&rsa->prf, tr); + + tr = ikev2_sa_get_td_for_type (sa->r_proposals, + IKEV2_TRANSFORM_TYPE_INTEG); + if (tr) + cp_sa_transform (&rsa->integrity, tr); + + tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_DH); + if (tr) + cp_sa_transform (&rsa->dh, tr); + + k->sk_d_len = vec_len (sa->sk_d); + clib_memcpy (&k->sk_d, sa->sk_d, k->sk_d_len); + + k->sk_ai_len = vec_len (sa->sk_ai); + clib_memcpy (&k->sk_ai, sa->sk_ai, k->sk_ai_len); + + k->sk_ar_len = vec_len (sa->sk_ar); + clib_memcpy (&k->sk_ar, sa->sk_ar, k->sk_ar_len); + + k->sk_ei_len = vec_len (sa->sk_ei); + clib_memcpy (&k->sk_ei, sa->sk_ei, k->sk_ei_len); + + k->sk_er_len = vec_len (sa->sk_er); + clib_memcpy (&k->sk_er, sa->sk_er, k->sk_er_len); + + k->sk_pi_len = vec_len (sa->sk_pi); + clib_memcpy (&k->sk_pi, sa->sk_pi, k->sk_pi_len); + + k->sk_pr_len = vec_len (sa->sk_pr); + clib_memcpy (&k->sk_pr, sa->sk_pr, k->sk_pr_len); + + vl_api_ikev2_sa_t_endian(rsa); + }); + /* *INDENT-ON* */ +} + +static void +vl_api_ikev2_sa_dump_t_handler (vl_api_ikev2_sa_dump_t * mp) +{ + ikev2_main_t *km = &ikev2_main; + ikev2_main_per_thread_data_t *tkm; + ikev2_sa_t *sa; + + vec_foreach (tkm, km->per_thread_data) + { + /* *INDENT-OFF* */ + pool_foreach (sa, tkm->sas, + ({ + u32 api_sa_index = ikev2_encode_sa_index (sa - tkm->sas, + tkm - km->per_thread_data); + send_sa (sa, mp, api_sa_index); + })); + /* *INDENT-ON* */ + } +} + + +static void +send_child_sa (ikev2_child_sa_t * child, + vl_api_ikev2_child_sa_dump_t * mp, u32 child_sa_index, + u32 sa_index) +{ + vl_api_ikev2_child_sa_details_t *rmp = 0; + int rv = 0; + ikev2_sa_transform_t *tr; + + /* *INDENT-OFF* */ + REPLY_MACRO2_ZERO (VL_API_IKEV2_CHILD_SA_DETAILS, + { + vl_api_ikev2_keys_t *k = &rmp->child_sa.keys; + rmp->child_sa.child_sa_index = child_sa_index; + rmp->child_sa.sa_index = sa_index; + rmp->child_sa.i_spi = + child->i_proposals ? child->i_proposals[0].spi : 0; + rmp->child_sa.r_spi = + child->r_proposals ? child->r_proposals[0].spi : 0; + + tr = ikev2_sa_get_td_for_type (child->r_proposals, + IKEV2_TRANSFORM_TYPE_ENCR); + if (tr) + cp_sa_transform (&rmp->child_sa.encryption, tr); + + tr = ikev2_sa_get_td_for_type (child->r_proposals, + IKEV2_TRANSFORM_TYPE_INTEG); + if (tr) + cp_sa_transform (&rmp->child_sa.integrity, tr); + + tr = ikev2_sa_get_td_for_type (child->r_proposals, + IKEV2_TRANSFORM_TYPE_ESN); + if (tr) + cp_sa_transform (&rmp->child_sa.esn, tr); + + k->sk_ei_len = vec_len (child->sk_ei); + clib_memcpy (&k->sk_ei, child->sk_ei, k->sk_ei_len); + + k->sk_er_len = vec_len (child->sk_er); + clib_memcpy (&k->sk_er, child->sk_er, k->sk_er_len); + + if (vec_len (child->sk_ai)) + { + k->sk_ai_len = vec_len (child->sk_ai); + clib_memcpy (&k->sk_ai, child->sk_ai, + k->sk_ai_len); + + k->sk_ar_len = vec_len (child->sk_ar); + clib_memcpy (&k->sk_ar, child->sk_ar, + k->sk_ar_len); + } + + vl_api_ikev2_child_sa_t_endian (&rmp->child_sa); + }); + /* *INDENT-ON* */ +} + +static void +vl_api_ikev2_child_sa_dump_t_handler (vl_api_ikev2_child_sa_dump_t * mp) +{ + ikev2_main_t *im = &ikev2_main; + ikev2_main_per_thread_data_t *tkm; + ikev2_sa_t *sa; + ikev2_child_sa_t *child; + u32 sai = ~0, ti = ~0; + + ikev2_decode_sa_index (clib_net_to_host_u32 (mp->sa_index), &sai, &ti); + + if (vec_len (im->per_thread_data) <= ti) + return; + + tkm = vec_elt_at_index (im->per_thread_data, ti); + + if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai)) + return; + + sa = pool_elt_at_index (tkm->sas, sai); + + vec_foreach (child, sa->childs) + { + u32 child_sa_index = child - sa->childs; + send_child_sa (child, mp, child_sa_index, sai); + } +} + +static void + vl_api_ikev2_traffic_selector_dump_t_handler + (vl_api_ikev2_traffic_selector_dump_t * mp) +{ + ikev2_main_t *im = &ikev2_main; + ikev2_main_per_thread_data_t *tkm; + ikev2_sa_t *sa; + ikev2_child_sa_t *child; + ikev2_ts_t *ts; + u32 sai = ~0, ti = ~0; + + u32 api_sa_index = clib_net_to_host_u32 (mp->sa_index); + u32 child_sa_index = clib_net_to_host_u32 (mp->child_sa_index); + ikev2_decode_sa_index (api_sa_index, &sai, &ti); + + if (vec_len (im->per_thread_data) <= ti) + return; + + tkm = vec_elt_at_index (im->per_thread_data, ti); + + if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai)) + return; + + sa = pool_elt_at_index (tkm->sas, sai); + + if (vec_len (sa->childs) <= child_sa_index) + return; + + child = vec_elt_at_index (sa->childs, child_sa_index); + + vec_foreach (ts, mp->is_initiator ? child->tsi : child->tsr) + { + vl_api_ikev2_traffic_selector_details_t *rmp = 0; + int rv = 0; + + /* *INDENT-OFF* */ + REPLY_MACRO2_ZERO (VL_API_IKEV2_TRAFFIC_SELECTOR_DETAILS, + { + rmp->ts.sa_index = api_sa_index; + rmp->ts.child_sa_index = child_sa_index; + cp_ts (&rmp->ts, ts, mp->is_initiator); + vl_api_ikev2_ts_t_endian (&rmp->ts); + }); + /* *INDENT-ON* */ + } +} + +static void +vl_api_ikev2_nonce_get_t_handler (vl_api_ikev2_nonce_get_t * mp) +{ + ikev2_main_t *im = &ikev2_main; + ikev2_main_per_thread_data_t *tkm; + ikev2_sa_t *sa; + u32 sai = ~0, ti = ~0; + + ikev2_decode_sa_index (clib_net_to_host_u32 (mp->sa_index), &sai, &ti); + + if (vec_len (im->per_thread_data) <= ti) + return; + + tkm = vec_elt_at_index (im->per_thread_data, ti); + + if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai)) + return; + + sa = pool_elt_at_index (tkm->sas, sai); + + u8 *nonce = mp->is_initiator ? sa->i_nonce : sa->r_nonce; + vl_api_ikev2_nonce_get_reply_t *rmp = 0; + int data_len = vec_len (nonce); + int rv = 0; + + /* *INDENT-OFF* */ + REPLY_MACRO3_ZERO (VL_API_IKEV2_NONCE_GET_REPLY, data_len, + { + rmp->data_len = clib_host_to_net_u32 (data_len); + clib_memcpy (rmp->nonce, nonce, data_len); + }); + /* *INDENT-ON* */ +} + +static void vl_api_ikev2_plugin_get_version_t_handler (vl_api_ikev2_plugin_get_version_t * mp) { diff --git a/src/plugins/ikev2/ikev2_test.c b/src/plugins/ikev2/ikev2_test.c index fae47726783..222f01ad123 100644 --- a/src/plugins/ikev2/ikev2_test.c +++ b/src/plugins/ikev2/ikev2_test.c @@ -34,6 +34,10 @@ #include <ikev2/ikev2.api_types.h> #include <vpp/api/vpe.api_types.h> +#define vl_endianfun /* define message structures */ +#include <plugins/ikev2/ikev2.api.h> +#undef vl_endianfun + typedef struct { /* API message ID base */ @@ -93,15 +97,24 @@ MACRO_FORMAT (auth_method) #define _(v,f,str) case IKEV2_ID_TYPE_##f: t = str; break; MACRO_FORMAT (id_type) #undef _ +#define _(v,f,str) case IKEV2_TRANSFORM_TYPE_##f: t = str; break; + MACRO_FORMAT (transform_type) +#undef _ #define _(v,f,str) case IKEV2_TRANSFORM_ENCR_TYPE_##f: t = str; break; MACRO_FORMAT (transform_encr_type) #undef _ +#define _(v,f,str) case IKEV2_TRANSFORM_PRF_TYPE_##f: t = str; break; + MACRO_FORMAT (transform_prf_type) +#undef _ #define _(v,f,str) case IKEV2_TRANSFORM_INTEG_TYPE_##f: t = str; break; MACRO_FORMAT (transform_integ_type) #undef _ #define _(v,f,str) case IKEV2_TRANSFORM_DH_TYPE_##f: t = str; break; MACRO_FORMAT (transform_dh_type) #undef _ +#define _(v,f,str) case IKEV2_TRANSFORM_ESN_TYPE_##f: t = str; break; + MACRO_FORMAT (transform_esn_type) +#undef _ u8 *format_ikev2_id_type_and_data (u8 * s, va_list * args) { vl_api_ikev2_id_t *id = va_arg (*args, vl_api_ikev2_id_t *); @@ -134,6 +147,50 @@ MACRO_FORMAT (auth_method) return s; } +u8 * +format_ikev2_sa_transform (u8 * s, va_list * args) +{ + vl_api_ikev2_sa_transform_t *tr = + va_arg (*args, vl_api_ikev2_sa_transform_t *); + + if (!tr) + return s; + + if (tr->transform_type >= IKEV2_TRANSFORM_NUM_TYPES) + return s; + + s = format (s, "%U:", format_ikev2_transform_type, tr->transform_type); + + switch (tr->transform_type) + { + case IKEV2_TRANSFORM_TYPE_ENCR: + s = + format (s, "%U", format_ikev2_transform_encr_type, tr->transform_id); + break; + case IKEV2_TRANSFORM_TYPE_PRF: + s = format (s, "%U", format_ikev2_transform_prf_type, tr->transform_id); + break; + case IKEV2_TRANSFORM_TYPE_INTEG: + s = + format (s, "%U", format_ikev2_transform_integ_type, tr->transform_id); + break; + case IKEV2_TRANSFORM_TYPE_DH: + s = format (s, "%U", format_ikev2_transform_dh_type, tr->transform_id); + break; + case IKEV2_TRANSFORM_TYPE_ESN: + s = format (s, "%U", format_ikev2_transform_esn_type, tr->transform_id); + break; + default: + break; + } + + if (tr->transform_type == IKEV2_TRANSFORM_TYPE_ENCR && + tr->transform_id == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len) + s = format (s, "-%u", tr->key_len * 8); + + return s; +} + static int api_ikev2_profile_dump (vat_main_t * vam) { @@ -155,8 +212,6 @@ api_ikev2_profile_dump (vat_main_t * vam) mp_ping->_vl_msg_id = htons (ik->ping_id); mp_ping->client_index = vam->my_client_index; - fformat (vam->ofp, "Sending ping id=%d\n", ik->ping_id); - vam->result_ready = 0; S (mp_ping); @@ -260,6 +315,298 @@ static void vl_api_ikev2_profile_details_t_handler } static int +api_ikev2_sa_dump (vat_main_t * vam) +{ + ikev2_test_main_t *im = &ikev2_test_main; + vl_api_ikev2_sa_dump_t *mp; + vl_api_control_ping_t *mp_ping; + int ret; + + /* Construct the API message */ + M (IKEV2_SA_DUMP, mp); + + /* send it... */ + S (mp); + + /* Use a control ping for synchronization */ + if (!im->ping_id) + im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC)); + mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); + mp_ping->_vl_msg_id = htons (im->ping_id); + mp_ping->client_index = vam->my_client_index; + vam->result_ready = 0; + + S (mp_ping); + + /* Wait for a reply... */ + W (ret); + return ret; +} + +static void +vl_api_ikev2_sa_details_t_handler (vl_api_ikev2_sa_details_t * mp) +{ + vat_main_t *vam = ikev2_test_main.vat_main; + vl_api_ikev2_sa_t *sa = &mp->sa; + ip4_address_t iaddr; + ip4_address_t raddr; + vl_api_ikev2_keys_t *k = &sa->keys; + vl_api_ikev2_sa_t_endian (sa); + + ip4_address_decode (sa->iaddr, &iaddr); + ip4_address_decode (sa->raddr, &raddr); + + fformat (vam->ofp, "profile index %d sa index: %d\n", + mp->sa.profile_index, mp->sa.sa_index); + fformat (vam->ofp, " iip %U ispi %lx rip %U rspi %lx\n", format_ip4_address, + &iaddr, sa->ispi, format_ip4_address, &raddr, sa->rspi); + fformat (vam->ofp, " %U ", format_ikev2_sa_transform, &sa->encryption); + fformat (vam->ofp, "%U ", format_ikev2_sa_transform, &sa->prf); + fformat (vam->ofp, "%U ", format_ikev2_sa_transform, &sa->integrity); + fformat (vam->ofp, "%U \n", format_ikev2_sa_transform, &sa->dh); + + fformat (vam->ofp, " SK_d %U\n", + format_hex_bytes, k->sk_d, k->sk_d_len); + + fformat (vam->ofp, " SK_a i:%U\n r:%U\n", + format_hex_bytes, k->sk_ai, k->sk_ai_len, format_hex_bytes, + k->sk_ar, k->sk_ar_len); + + fformat (vam->ofp, " SK_e i:%U\n r:%U\n", format_hex_bytes, + k->sk_ei, k->sk_ei_len, format_hex_bytes, k->sk_er, k->sk_er_len); + + fformat (vam->ofp, " SK_p i:%U\n r:%U\n", format_hex_bytes, + k->sk_pi, k->sk_pi_len, format_hex_bytes, k->sk_pr, k->sk_pr_len); + + fformat (vam->ofp, " identifier (i) %U\n", + format_ikev2_id_type_and_data, &sa->i_id); + fformat (vam->ofp, " identifier (r) %U\n", + format_ikev2_id_type_and_data, &sa->r_id); + + vam->result_ready = 1; +} + +static int +api_ikev2_child_sa_dump (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + ikev2_test_main_t *im = &ikev2_test_main; + vl_api_ikev2_child_sa_dump_t *mp; + vl_api_control_ping_t *mp_ping; + int ret; + u32 sa_index = ~0; + + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "sa_index %d", &sa_index)) + ; + else + { + errmsg ("parse error '%U'", format_unformat_error, i); + return -99; + } + } + + if (sa_index == ~0) + return -99; + + /* Construct the API message */ + M (IKEV2_CHILD_SA_DUMP, mp); + + mp->sa_index = clib_net_to_host_u32 (sa_index); + + /* send it... */ + S (mp); + + /* Use a control ping for synchronization */ + if (!im->ping_id) + im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC)); + mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); + mp_ping->_vl_msg_id = htons (im->ping_id); + mp_ping->client_index = vam->my_client_index; + vam->result_ready = 0; + + S (mp_ping); + + /* Wait for a reply... */ + W (ret); + return ret; +} + +static void +vl_api_ikev2_child_sa_details_t_handler (vl_api_ikev2_child_sa_details_t * mp) +{ + vat_main_t *vam = ikev2_test_main.vat_main; + vl_api_ikev2_child_sa_t *child_sa = &mp->child_sa; + vl_api_ikev2_keys_t *k = &child_sa->keys; + vl_api_ikev2_child_sa_t_endian (child_sa); + + fformat (vam->ofp, " child sa %u:\n", child_sa->child_sa_index); + + fformat (vam->ofp, " %U ", format_ikev2_sa_transform, + &child_sa->encryption); + fformat (vam->ofp, "%U ", format_ikev2_sa_transform, &child_sa->integrity); + fformat (vam->ofp, "%U \n", format_ikev2_sa_transform, &child_sa->esn); + + fformat (vam->ofp, " spi(i) %lx spi(r) %lx\n", + child_sa->i_spi, child_sa->r_spi); + + fformat (vam->ofp, " SK_e i:%U\n r:%U\n", + format_hex_bytes, k->sk_ei, k->sk_ei_len, + format_hex_bytes, k->sk_er, k->sk_er_len); + if (k->sk_ai_len) + { + fformat (vam->ofp, " SK_a i:%U\n r:%U\n", + format_hex_bytes, k->sk_ai, k->sk_ai_len, + format_hex_bytes, k->sk_ar, k->sk_ar_len); + } + vam->result_ready = 1; +} + +static int +api_ikev2_traffic_selector_dump (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + ikev2_test_main_t *im = &ikev2_test_main; + vl_api_ikev2_traffic_selector_dump_t *mp; + vl_api_control_ping_t *mp_ping; + int is_initiator = ~0; + int sa_index = ~0; + int child_sa_index = ~0; + int ret; + + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "initiator")) + is_initiator = 1; + else if (unformat (i, "responder")) + is_initiator = 0; + else if (unformat (i, "sa_index %d", &sa_index)) + ; + else if (unformat (i, "child_sa_index %d", &child_sa_index)) + ; + else + { + errmsg ("parse error '%U'", format_unformat_error, i); + return -99; + } + } + + if (child_sa_index == ~0 || sa_index == ~0 || is_initiator == ~0) + return -99; + + /* Construct the API message */ + M (IKEV2_TRAFFIC_SELECTOR_DUMP, mp); + + mp->is_initiator = is_initiator; + mp->sa_index = clib_host_to_net_u32 (sa_index); + mp->child_sa_index = clib_host_to_net_u32 (child_sa_index); + + /* send it... */ + S (mp); + + /* Use a control ping for synchronization */ + if (!im->ping_id) + im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC)); + mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); + mp_ping->_vl_msg_id = htons (im->ping_id); + mp_ping->client_index = vam->my_client_index; + vam->result_ready = 0; + + S (mp_ping); + + /* Wait for a reply... */ + W (ret); + return ret; +} + +static void + vl_api_ikev2_traffic_selector_details_t_handler + (vl_api_ikev2_traffic_selector_details_t * mp) +{ + vat_main_t *vam = ikev2_test_main.vat_main; + vl_api_ikev2_ts_t *ts = &mp->ts; + ip4_address_t start_addr; + ip4_address_t end_addr; + vl_api_ikev2_ts_t_endian (ts); + + ip4_address_decode (ts->start_addr, &start_addr); + ip4_address_decode (ts->end_addr, &end_addr); + + fformat (vam->ofp, " %s protocol_id %u addr " + "%U - %U port %u - %u\n", + ts->is_local, ts->protocol_id, + format_ip4_address, &start_addr, + format_ip4_address, &end_addr, ts->start_port, ts->end_port); + vam->result_ready = 1; +} + +static int +api_ikev2_nonce_get (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + ikev2_test_main_t *im = &ikev2_test_main; + vl_api_ikev2_nonce_get_t *mp; + vl_api_control_ping_t *mp_ping; + u32 is_initiator = ~0; + u32 sa_index = ~0; + int ret; + + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "initiator")) + is_initiator = 1; + else if (unformat (i, "responder")) + is_initiator = 0; + else if (unformat (i, "sa_index %d", &sa_index)) + ; + else + { + errmsg ("parse error '%U'", format_unformat_error, i); + return -99; + } + } + + if (sa_index == ~0 || is_initiator == ~0) + return -99; + + /* Construct the API message */ + M (IKEV2_NONCE_GET, mp); + + mp->is_initiator = is_initiator; + mp->sa_index = clib_host_to_net_u32 (sa_index); + + /* send it... */ + S (mp); + + /* Use a control ping for synchronization */ + if (!im->ping_id) + im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC)); + mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); + mp_ping->_vl_msg_id = htons (im->ping_id); + mp_ping->client_index = vam->my_client_index; + vam->result_ready = 0; + + S (mp_ping); + + /* Wait for a reply... */ + W (ret); + return ret; +} + +static void +vl_api_ikev2_nonce_get_reply_t_handler (vl_api_ikev2_nonce_get_reply_t * mp) +{ + vat_main_t *vam = ikev2_test_main.vat_main; + mp->data_len = clib_net_to_host_u32 (mp->data_len); + + fformat (vam->ofp, " nonce:%U\n", + format_hex_bytes, mp->nonce, mp->data_len); + + vam->result_ready = 1; +} + +static int api_ikev2_plugin_get_version (vat_main_t * vam) { ikev2_test_main_t *sm = &ikev2_test_main; diff --git a/src/plugins/ikev2/ikev2_types.api b/src/plugins/ikev2/ikev2_types.api index bee48f8cef0..58297c05e9c 100644 --- a/src/plugins/ikev2/ikev2_types.api +++ b/src/plugins/ikev2/ikev2_types.api @@ -1,6 +1,6 @@ /* Hey Emacs use -*- mode: C -*- */ /* - * Copyright (c) 2015-2016 Cisco and/or its affiliates. + * Copyright (c) 2015-2020 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -27,6 +27,9 @@ typedef ikev2_id typedef ikev2_ts { + u32 sa_index; + u32 child_sa_index; + bool is_local; u8 protocol_id; u16 start_port; @@ -83,3 +86,65 @@ typedef ikev2_profile bool udp_encap; vl_api_ikev2_auth_t auth; }; + +typedef ikev2_sa_transform +{ + u8 transform_type; + u16 transform_id; + u16 key_len; + u16 key_trunc; + u16 block_size; + u8 dh_group; +}; + +typedef ikev2_keys +{ + u8 sk_d[64]; + u8 sk_d_len; + u8 sk_ai[64]; + u8 sk_ai_len; + u8 sk_ar[64]; + u8 sk_ar_len; + u8 sk_ei[64]; + u8 sk_ei_len; + u8 sk_er[64]; + u8 sk_er_len; + u8 sk_pi[64]; + u8 sk_pi_len; + u8 sk_pr[64]; + u8 sk_pr_len; +}; + +typedef ikev2_child_sa +{ + u32 sa_index; + u32 child_sa_index; + u32 i_spi; + u32 r_spi; + vl_api_ikev2_keys_t keys; + vl_api_ikev2_sa_transform_t encryption; + vl_api_ikev2_sa_transform_t integrity; + vl_api_ikev2_sa_transform_t esn; +}; + +typedef ikev2_sa +{ + u32 sa_index; + u32 profile_index; + + u64 ispi; + u64 rspi; + vl_api_ip4_address_t iaddr; + vl_api_ip4_address_t raddr; + + vl_api_ikev2_keys_t keys; + + /* ID */ + vl_api_ikev2_id_t i_id; + vl_api_ikev2_id_t r_id; + + vl_api_ikev2_sa_transform_t encryption; + vl_api_ikev2_sa_transform_t integrity; + vl_api_ikev2_sa_transform_t prf; + vl_api_ikev2_sa_transform_t dh; +}; diff --git a/src/plugins/ikev2/test/test_ikev2.py b/src/plugins/ikev2/test/test_ikev2.py index 02d1bde8f3d..6116ebb29a7 100644 --- a/src/plugins/ikev2/test/test_ikev2.py +++ b/src/plugins/ikev2/test/test_ikev2.py @@ -415,6 +415,8 @@ class IKEv2SA(object): c = self.child_sas[0] ts1 = ikev2.IPv4TrafficSelector( IP_protocol_ID=0, + start_port=0, + end_port=0xffff, starting_address_v4=c.local_ts['start_addr'], ending_address_v4=c.local_ts['end_addr']) ts2 = ikev2.IPv4TrafficSelector( @@ -692,7 +694,7 @@ class TemplateResponder(VppTestCase): plain = self.sa.hmac_and_decrypt(ike) self.sa.calc_child_keys() - def verify_child_sas(self): + def verify_ipsec_sas(self): sas = self.vapi.ipsec_sa_dump() self.assertEqual(len(sas), 2) sa0 = sas[0].entry @@ -726,10 +728,97 @@ class TemplateResponder(VppTestCase): self.assertEqual(sa0.salt.to_bytes(4, 'little'), c.salt_er) self.assertEqual(sa1.salt.to_bytes(4, 'little'), c.salt_ei) + def verify_keymat(self, api_keys, keys, name): + km = getattr(keys, name) + api_km = getattr(api_keys, name) + api_km_len = getattr(api_keys, name + '_len') + self.assertEqual(len(km), api_km_len) + self.assertEqual(km, api_km[:api_km_len]) + + def verify_id(self, api_id, exp_id): + self.assertEqual(api_id.type, IDType.value(exp_id.type)) + self.assertEqual(api_id.data_len, exp_id.data_len) + self.assertEqual(bytes(api_id.data, 'ascii'), exp_id.type) + + def verify_ike_sas(self): + r = self.vapi.ikev2_sa_dump() + self.assertEqual(len(r), 1) + sa = r[0].sa + self.assertEqual(self.sa.ispi, (sa.ispi).to_bytes(8, 'little')) + self.assertEqual(self.sa.rspi, (sa.rspi).to_bytes(8, 'big')) + self.assertEqual(sa.iaddr, IPv4Address(self.pg0.remote_ip4)) + self.assertEqual(sa.raddr, IPv4Address(self.pg0.local_ip4)) + self.verify_keymat(sa.keys, self.sa, 'sk_d') + self.verify_keymat(sa.keys, self.sa, 'sk_ai') + self.verify_keymat(sa.keys, self.sa, 'sk_ar') + self.verify_keymat(sa.keys, self.sa, 'sk_ei') + self.verify_keymat(sa.keys, self.sa, 'sk_er') + self.verify_keymat(sa.keys, self.sa, 'sk_pi') + self.verify_keymat(sa.keys, self.sa, 'sk_pr') + + self.assertEqual(sa.i_id.type, self.sa.id_type) + self.assertEqual(sa.r_id.type, self.sa.id_type) + self.assertEqual(sa.i_id.data_len, len(self.sa.i_id)) + self.assertEqual(sa.r_id.data_len, len(self.sa.r_id)) + self.assertEqual(bytes(sa.i_id.data, 'ascii'), self.sa.i_id) + self.assertEqual(bytes(sa.r_id.data, 'ascii'), self.sa.r_id) + + r = self.vapi.ikev2_child_sa_dump(sa_index=sa.sa_index) + self.assertEqual(len(r), 1) + csa = r[0].child_sa + self.assertEqual(csa.sa_index, sa.sa_index) + c = self.sa.child_sas[0] + if hasattr(c, 'sk_ai'): + self.verify_keymat(csa.keys, c, 'sk_ai') + self.verify_keymat(csa.keys, c, 'sk_ar') + self.verify_keymat(csa.keys, c, 'sk_ei') + self.verify_keymat(csa.keys, c, 'sk_er') + + tsi, tsr = self.sa.generate_ts() + tsi = tsi[0] + tsr = tsr[0] + r = self.vapi.ikev2_traffic_selector_dump( + is_initiator=True, sa_index=sa.sa_index, + child_sa_index=csa.child_sa_index) + self.assertEqual(len(r), 1) + ts = r[0].ts + self.verify_ts(r[0].ts, tsi[0], True) + + r = self.vapi.ikev2_traffic_selector_dump( + is_initiator=False, sa_index=sa.sa_index, + child_sa_index=csa.child_sa_index) + self.assertEqual(len(r), 1) + self.verify_ts(r[0].ts, tsr[0], False) + + n = self.vapi.ikev2_nonce_get(is_initiator=True, + sa_index=sa.sa_index) + self.verify_nonce(n, self.sa.i_nonce) + n = self.vapi.ikev2_nonce_get(is_initiator=False, + sa_index=sa.sa_index) + self.verify_nonce(n, self.sa.r_nonce) + + def verify_nonce(self, api_nonce, nonce): + self.assertEqual(api_nonce.data_len, len(nonce)) + self.assertEqual(api_nonce.nonce, nonce) + + def verify_ts(self, api_ts, ts, is_initiator): + if is_initiator: + self.assertTrue(api_ts.is_local) + else: + self.assertFalse(api_ts.is_local) + self.assertEqual(api_ts.start_addr, + IPv4Address(ts.starting_address_v4)) + self.assertEqual(api_ts.end_addr, + IPv4Address(ts.ending_address_v4)) + self.assertEqual(api_ts.start_port, ts.start_port) + self.assertEqual(api_ts.end_port, ts.end_port) + self.assertEqual(api_ts.protocol_id, ts.IP_protocol_ID) + def test_responder(self): self.send_sa_init(self.sa.natt) self.send_sa_auth() - self.verify_child_sas() + self.verify_ipsec_sas() + self.verify_ike_sas() class Ikev2Params(object): |