ikev2: add support for custom ipsec-over-udp port
[vpp.git] / src / plugins / ikev2 / ikev2_api.c
index b7b8d6d..0064f57 100644 (file)
 #include <ikev2/ikev2_priv.h>
 
 /* define message IDs */
-#include <plugins/ikev2/ikev2_msg_enum.h>
-
-#define vl_typedefs            /* define message structures */
-#include <ikev2/ikev2_all_api.h>
-#undef vl_typedefs
-
-#define vl_endianfun           /* define message structures */
-#include <ikev2/ikev2_all_api.h>
-#undef vl_endianfun
-
-/* instantiate all the print functions we know about */
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
-#define vl_printfun
-#include <ikev2/ikev2_all_api.h>
-#undef vl_printfun
-
-/* Get the API version number */
-#define vl_api_version(n,v) static u32 api_version=(v);
-#include <ikev2/ikev2_all_api.h>
-#undef vl_api_version
+#include <vnet/format_fns.h>
+#include <plugins/ikev2/ikev2.api_enum.h>
+#include <plugins/ikev2/ikev2.api_types.h>
 
 extern ikev2_main_t ikev2_main;
 
@@ -54,22 +37,6 @@ extern ikev2_main_t ikev2_main;
 #define REPLY_MSG_ID_BASE ikev2_main.msg_id_base
 #include <vlibapi/api_helper_macros.h>
 
-#define foreach_ikev2_api_msg                                   \
-_(IKEV2_PLUGIN_GET_VERSION, ikev2_plugin_get_version)           \
-_(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_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_ikev2_plugin_get_version_t_handler (vl_api_ikev2_plugin_get_version_t *
                                           mp)
@@ -144,7 +111,7 @@ static void
 static void
 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
 {
-  vl_api_ikev2_profile_add_del_reply_t *rmp;
+  vl_api_ikev2_profile_set_id_reply_t *rmp;
   int rv = 0;
 
 #if WITH_LIBSSL > 0
@@ -166,6 +133,28 @@ vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
 }
 
+static void
+  vl_api_ikev2_profile_set_udp_encap_t_handler
+  (vl_api_ikev2_profile_set_udp_encap_t * mp)
+{
+  vl_api_ikev2_profile_set_udp_encap_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_udp_encap (vm, tmp);
+  vec_free (tmp);
+  if (error)
+    rv = VNET_API_ERROR_UNSPECIFIED;
+#else
+  rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_UDP_ENCAP);
+}
+
 static void
 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
 {
@@ -176,9 +165,12 @@ vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
   vlib_main_t *vm = vlib_get_main ();
   clib_error_t *error;
   u8 *tmp = format (0, "%s", mp->name);
-  error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
-                               mp->end_port, (ip4_address_t) mp->start_addr,
-                               (ip4_address_t) mp->end_addr, mp->is_local);
+  error =
+    ikev2_set_profile_ts (vm, tmp, mp->proto,
+                         clib_net_to_host_u16 (mp->start_port),
+                         clib_net_to_host_u16 (mp->end_port),
+                         (ip4_address_t) mp->start_addr,
+                         (ip4_address_t) mp->end_addr, mp->is_local);
   vec_free (tmp);
   if (error)
     rv = VNET_API_ERROR_UNSPECIFIED;
@@ -192,7 +184,7 @@ vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
 static void
 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
 {
-  vl_api_ikev2_profile_set_ts_reply_t *rmp;
+  vl_api_ikev2_set_local_key_reply_t *rmp;
   int rv = 0;
 
 #if WITH_LIBSSL > 0
@@ -223,7 +215,7 @@ vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
   ip4_address_t ip4;
   clib_memcpy (&ip4, mp->address, sizeof (ip4));
 
-  error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
+  error = ikev2_set_profile_responder (vm, tmp, ntohl (mp->sw_if_index), ip4);
   vec_free (tmp);
   if (error)
     rv = VNET_API_ERROR_UNSPECIFIED;
@@ -248,8 +240,10 @@ vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
   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);
+    ikev2_set_profile_ike_transforms (vm, tmp, ntohl (mp->crypto_alg),
+                                     ntohl (mp->integ_alg),
+                                     ntohl (mp->dh_group),
+                                     ntohl (mp->crypto_key_size));
   vec_free (tmp);
   if (error)
     rv = VNET_API_ERROR_UNSPECIFIED;
@@ -274,8 +268,10 @@ vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
   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);
+    ikev2_set_profile_esp_transforms (vm, tmp, ntohl (mp->crypto_alg),
+                                     ntohl (mp->integ_alg),
+                                     ntohl (mp->dh_group),
+                                     ntohl (mp->crypto_key_size));
   vec_free (tmp);
   if (error)
     rv = VNET_API_ERROR_UNSPECIFIED;
@@ -299,8 +295,12 @@ vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
   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);
+    ikev2_set_profile_sa_lifetime (vm, tmp,
+                                  clib_net_to_host_u64 (mp->lifetime),
+                                  ntohl (mp->lifetime_jitter),
+                                  ntohl (mp->handover),
+                                  clib_net_to_host_u64
+                                  (mp->lifetime_maxdata));
   vec_free (tmp);
   if (error)
     rv = VNET_API_ERROR_UNSPECIFIED;
@@ -311,6 +311,57 @@ vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
   REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
 }
 
+static void
+  vl_api_ikev2_profile_set_ipsec_udp_port_t_handler
+  (vl_api_ikev2_profile_set_ipsec_udp_port_t * mp)
+{
+  vl_api_ikev2_profile_set_ipsec_udp_port_reply_t *rmp;
+  int rv = 0;
+
+#if WITH_LIBSSL > 0
+  vlib_main_t *vm = vlib_get_main ();
+
+  u8 *tmp = format (0, "%s", mp->name);
+
+  rv =
+    ikev2_set_profile_ipsec_udp_port (vm, tmp,
+                                     clib_net_to_host_u16 (mp->port),
+                                     mp->is_set);
+  vec_free (tmp);
+#else
+  rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_IPSEC_UDP_PORT_REPLY);
+}
+
+static void
+  vl_api_ikev2_set_tunnel_interface_t_handler
+  (vl_api_ikev2_set_tunnel_interface_t * mp)
+{
+  vl_api_ikev2_set_tunnel_interface_reply_t *rmp;
+  int rv = 0;
+
+  VALIDATE_SW_IF_INDEX (mp);
+
+#if WITH_LIBSSL > 0
+  u8 *tmp = format (0, "%s", mp->name);
+  clib_error_t *error;
+
+  error = ikev2_set_profile_tunnel_interface (vlib_get_main (), tmp,
+                                             ntohl (mp->sw_if_index));
+
+  if (error)
+    rv = VNET_API_ERROR_UNSPECIFIED;
+  vec_free (tmp);
+#else
+  rv = VNET_API_ERROR_UNIMPLEMENTED;
+#endif
+
+  BAD_SW_IF_INDEX_LABEL;
+  REPLY_MACRO (VL_API_IKEV2_SET_TUNNEL_INTERFACE_REPLY);
+}
+
 static void
 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
 {
@@ -397,64 +448,16 @@ static void
   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
 }
 
-/*
- * ikev2_api_hookup
- * Add vpe's API message handlers to the table.
- * vlib has already mapped shared memory and
- * added the client registration handlers.
- * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
- */
-#define vl_msg_name_crc_list
-#include <ikev2/ikev2_all_api.h>
-#undef vl_msg_name_crc_list
-
-static void
-setup_message_id_table (ikev2_main_t * im, api_main_t * am)
-{
-#define _(id,n,crc) \
-  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + im->msg_id_base);
-  foreach_vl_msg_name_crc_ikev2;
-#undef _
-}
-
-static clib_error_t *
-ikev2_plugin_api_hookup (vlib_main_t * vm)
-{
-  ikev2_main_t *im = &ikev2_main;
-#define _(N,n)                                                  \
-  vl_msg_api_set_handlers(VL_API_##N + im->msg_id_base, #n,     \
-                           vl_api_##n##_t_handler,              \
-                           vl_noop_handler,                     \
-                           vl_api_##n##_t_endian,               \
-                           vl_api_##n##_t_print,                \
-                           sizeof(vl_api_##n##_t), 1);
-  foreach_ikev2_api_msg;
-#undef _
-
-  return (NULL);
-}
-
+#include <ikev2/ikev2.api.c>
 static clib_error_t *
 ikev2_api_init (vlib_main_t * vm)
 {
   ikev2_main_t *im = &ikev2_main;
-  clib_error_t *error = 0;
-  u8 *name;
-
-  name = format (0, "ikev2_%08x%c", api_version, 0);
 
   /* Ask for a correctly-sized block of API message decode slots */
-  im->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
-                                           VL_MSG_FIRST_AVAILABLE);
-
-  error = ikev2_plugin_api_hookup (vm);
-
-  /* Add our API messages to the global name_crc hash table */
-  setup_message_id_table (im, &api_main);
-
-  vec_free (name);
+  im->msg_id_base = setup_message_id_table ();
 
-  return (error);
+  return 0;
 }
 
 VLIB_INIT_FUNCTION (ikev2_api_init);