ikev2: support responder hostname
[vpp.git] / src / plugins / ikev2 / ikev2_cli.c
index 84bf84f..3523ce0 100644 (file)
@@ -14,9 +14,7 @@
  */
 #include <vlib/vlib.h>
 #include <vnet/vnet.h>
-#include <vnet/pg/pg.h>
 #include <vppinfra/error.h>
-#include <vnet/udp/udp.h>
 #include <vnet/ipsec/ipsec_sa.h>
 #include <plugins/ikev2/ikev2.h>
 #include <plugins/ikev2/ikev2_priv.h>
@@ -29,18 +27,24 @@ format_ikev2_id_type_and_data (u8 * s, va_list * args)
   if (id->type == 0 || vec_len (id->data) == 0)
     return format (s, "none");
 
-  s = format (s, "%U", format_ikev2_id_type, id->type);
+  s = format (s, "id-type %U data ", format_ikev2_id_type, id->type);
 
-  if (id->type == IKEV2_ID_TYPE_ID_FQDN ||
-      id->type == IKEV2_ID_TYPE_ID_RFC822_ADDR)
+  switch (id->type)
     {
-      s = format (s, " %v", id->data);
-    }
-  else
-    {
-      s =
-       format (s, " %U", format_hex_bytes, &id->data,
-               (uword) (vec_len (id->data)));
+    case IKEV2_ID_TYPE_ID_IPV4_ADDR:
+      s = format (s, "%U", format_ip4_address, id->data);
+      break;
+    case IKEV2_ID_TYPE_ID_IPV6_ADDR:
+      s = format (s, "%U", format_ip6_address, id->data);
+      break;
+    case IKEV2_ID_TYPE_ID_FQDN:        /* fallthrough */
+    case IKEV2_ID_TYPE_ID_RFC822_ADDR:
+      s = format (s, "%v", id->data);
+      break;
+    default:
+      s = format (s, "0x%U", format_hex_bytes, &id->data,
+                 (uword) (vec_len (id->data)));
+      break;
     }
 
   return s;
@@ -55,8 +59,8 @@ format_ikev2_traffic_selector (u8 * s, va_list * va)
   s = format (s, "%u type %u protocol_id %u addr "
              "%U - %U port %u - %u\n",
              index, ts->ts_type, ts->protocol_id,
-             format_ip4_address, &ts->start_addr,
-             format_ip4_address, &ts->end_addr,
+             format_ip_address, &ts->start_addr,
+             format_ip_address, &ts->end_addr,
              clib_net_to_host_u16 (ts->start_port),
              clib_net_to_host_u16 (ts->end_port));
   return s;
@@ -127,8 +131,8 @@ format_ikev2_sa (u8 * s, va_list * va)
   u32 indent = 1;
 
   s = format (s, "iip %U ispi %lx rip %U rspi %lx",
-             format_ip4_address, &sa->iaddr, sa->ispi,
-             format_ip4_address, &sa->raddr, sa->rspi);
+             format_ip_address, &sa->iaddr, sa->ispi,
+             format_ip_address, &sa->raddr, sa->rspi);
   if (!details)
     return s;
 
@@ -187,6 +191,14 @@ format_ikev2_sa (u8 * s, va_list * va)
                format_ikev2_child_sa, child, child - sa->childs);
   }
 
+  s = format (s, "Stats:\n");
+  s = format (s, " keepalives :%u\n", sa->stats.n_keepalives);
+  s = format (s, " rekey :%u\n", sa->stats.n_rekey_req);
+  s = format (s, " SA init :%u (retransmit: %u)\n", sa->stats.n_sa_init_req,
+             sa->stats.n_init_retransmit);
+  s = format (s, " retransmit: %u\n", sa->stats.n_retransmit);
+  s = format (s, " SA auth :%u\n", sa->stats.n_sa_auth_req);
+
   return s;
 }
 
@@ -221,7 +233,7 @@ show_ikev2_sa_command_fn (vlib_main_t * vm,
   vec_foreach (tkm, km->per_thread_data)
   {
     /* *INDENT-OFF* */
-    pool_foreach (sa, tkm->sas, ({
+    pool_foreach (sa, tkm->sas)  {
       if (show_one)
         {
           if (sa->rspi == rspi)
@@ -232,7 +244,7 @@ show_ikev2_sa_command_fn (vlib_main_t * vm,
         }
       else
         s = format (s, "%U\n", format_ikev2_sa, sa, details);
-    }));
+    }
     /* *INDENT-ON* */
   }
 
@@ -249,6 +261,40 @@ VLIB_CLI_COMMAND (show_ikev2_sa_command, static) = {
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+ikev2_disable_dpd_command_fn (vlib_main_t * vm,
+                             unformat_input_t * input,
+                             vlib_cli_command_t * cmd)
+{
+  ikev2_disable_dpd ();
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (ikev2_cli_disable_dpd_command, static) = {
+  .path = "ikev2 dpd disable",
+  .short_help = "ikev2 dpd disable",
+  .function = ikev2_disable_dpd_command_fn,
+};
+/* *INDENT-ON* */
+
+static uword
+unformat_ikev2_token (unformat_input_t * input, va_list * va)
+{
+  u8 **string_return = va_arg (*va, u8 **);
+  const char *token_chars = "a-zA-Z0-9_";
+  if (*string_return)
+    {
+      /* if string_return was already allocated (eg. because of a previous
+       * partial match with a successful unformat_token()), we must free it
+       * before reusing the pointer, otherwise we'll be leaking memory
+       */
+      vec_free (*string_return);
+      *string_return = 0;
+    }
+  return unformat_user (input, unformat_token, token_chars, string_return);
+}
+
 static clib_error_t *
 ikev2_profile_add_del_command_fn (vlib_main_t * vm,
                                  unformat_input_t * input,
@@ -262,36 +308,30 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
   u8 *data = 0;
   u32 tmp1, tmp2, tmp3;
   u64 tmp4, tmp5;
-  ip4_address_t ip4;
-  ip4_address_t end_addr;
+  ip_address_t ip, end_addr;
   u32 responder_sw_if_index = (u32) ~ 0;
   u32 tun_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_";
-
   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, "add %U", unformat_token, valid_chars, &name))
+      if (unformat (line_input, "add %U", unformat_ikev2_token, &name))
        {
          r = ikev2_add_del_profile (vm, name, 1);
          goto done;
        }
-      else
-       if (unformat
-           (line_input, "del %U", unformat_token, valid_chars, &name))
+      else if (unformat (line_input, "del %U", unformat_ikev2_token, &name))
        {
          r = ikev2_add_del_profile (vm, name, 0);
          goto done;
        }
       else if (unformat (line_input, "set %U auth shared-key-mic string %v",
-                        unformat_token, valid_chars, &name, &data))
+                        unformat_ikev2_token, &name, &data))
        {
          r =
            ikev2_set_profile_auth (vm, name,
@@ -300,7 +340,7 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
          goto done;
        }
       else if (unformat (line_input, "set %U auth shared-key-mic hex %U",
-                        unformat_token, valid_chars, &name,
+                        unformat_ikev2_token, &name,
                         unformat_hex_string, &data))
        {
          r =
@@ -310,7 +350,7 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
          goto done;
        }
       else if (unformat (line_input, "set %U auth rsa-sig cert-file %v",
-                        unformat_token, valid_chars, &name, &data))
+                        unformat_ikev2_token, &name, &data))
        {
          r =
            ikev2_set_profile_auth (vm, name, IKEV2_AUTH_METHOD_RSA_SIG, data,
@@ -318,18 +358,18 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
          goto done;
        }
       else if (unformat (line_input, "set %U id local %U %U",
-                        unformat_token, valid_chars, &name,
+                        unformat_ikev2_token, &name,
                         unformat_ikev2_id_type, &id_type,
-                        unformat_ip4_address, &ip4))
+                        unformat_ip_address, &ip))
        {
-         data = vec_new (u8, 4);
-         clib_memcpy (data, ip4.as_u8, 4);
+         data = vec_new (u8, ip_address_size (&ip));
+         clib_memcpy (data, ip_addr_bytes (&ip), ip_address_size (&ip));
          r =
            ikev2_set_profile_id (vm, name, (u8) id_type, data, /*local */ 1);
          goto done;
        }
       else if (unformat (line_input, "set %U id local %U 0x%U",
-                        unformat_token, valid_chars, &name,
+                        unformat_ikev2_token, &name,
                         unformat_ikev2_id_type, &id_type,
                         unformat_hex_string, &data))
        {
@@ -338,7 +378,7 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
          goto done;
        }
       else if (unformat (line_input, "set %U id local %U %v",
-                        unformat_token, valid_chars, &name,
+                        unformat_ikev2_token, &name,
                         unformat_ikev2_id_type, &id_type, &data))
        {
          r =
@@ -346,18 +386,18 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
          goto done;
        }
       else if (unformat (line_input, "set %U id remote %U %U",
-                        unformat_token, valid_chars, &name,
+                        unformat_ikev2_token, &name,
                         unformat_ikev2_id_type, &id_type,
-                        unformat_ip4_address, &ip4))
+                        unformat_ip_address, &ip))
        {
-         data = vec_new (u8, 4);
-         clib_memcpy (data, ip4.as_u8, 4);
+         data = vec_new (u8, ip_address_size (&ip));
+         clib_memcpy (data, ip_addr_bytes (&ip), ip_address_size (&ip));
          r = ikev2_set_profile_id (vm, name, (u8) id_type, data,       /*remote */
                                    0);
          goto done;
        }
       else if (unformat (line_input, "set %U id remote %U 0x%U",
-                        unformat_token, valid_chars, &name,
+                        unformat_ikev2_token, &name,
                         unformat_ikev2_id_type, &id_type,
                         unformat_hex_string, &data))
        {
@@ -366,7 +406,7 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
          goto done;
        }
       else if (unformat (line_input, "set %U id remote %U %v",
-                        unformat_token, valid_chars, &name,
+                        unformat_ikev2_token, &name,
                         unformat_ikev2_id_type, &id_type, &data))
        {
          r = ikev2_set_profile_id (vm, name, (u8) id_type, data,       /*remote */
@@ -375,41 +415,46 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
        }
       else if (unformat (line_input, "set %U traffic-selector local "
                         "ip-range %U - %U port-range %u - %u protocol %u",
-                        unformat_token, valid_chars, &name,
-                        unformat_ip4_address, &ip4,
-                        unformat_ip4_address, &end_addr,
-                        &tmp1, &tmp2, &tmp3))
+                        unformat_ikev2_token, &name,
+                        unformat_ip_address, &ip,
+                        unformat_ip_address, &end_addr, &tmp1, &tmp2, &tmp3))
        {
          r =
            ikev2_set_profile_ts (vm, name, (u8) tmp3, (u16) tmp1, (u16) tmp2,
-                                 ip4, end_addr, /*local */ 1);
+                                 ip, end_addr, /*local */ 1);
          goto done;
        }
       else if (unformat (line_input, "set %U traffic-selector remote "
                         "ip-range %U - %U port-range %u - %u protocol %u",
-                        unformat_token, valid_chars, &name,
-                        unformat_ip4_address, &ip4,
-                        unformat_ip4_address, &end_addr,
-                        &tmp1, &tmp2, &tmp3))
+                        unformat_ikev2_token, &name,
+                        unformat_ip_address, &ip,
+                        unformat_ip_address, &end_addr, &tmp1, &tmp2, &tmp3))
        {
          r =
            ikev2_set_profile_ts (vm, name, (u8) tmp3, (u16) tmp1, (u16) tmp2,
-                                 ip4, end_addr, /*remote */ 0);
+                                 ip, end_addr, /*remote */ 0);
          goto done;
        }
       else if (unformat (line_input, "set %U responder %U %U",
-                        unformat_token, valid_chars, &name,
+                        unformat_ikev2_token, &name,
                         unformat_vnet_sw_interface, vnm,
-                        &responder_sw_if_index, unformat_ip4_address,
-                        &responder_ip4))
+                        &responder_sw_if_index, unformat_ip_address, &ip))
        {
          r =
-           ikev2_set_profile_responder (vm, name, responder_sw_if_index,
-                                        responder_ip4);
+           ikev2_set_profile_responder (vm, name, responder_sw_if_index, ip);
+         goto done;
+       }
+      else if (unformat (line_input, "set %U responder %U %v",
+                        unformat_ikev2_token, &name,
+                        unformat_vnet_sw_interface, vnm,
+                        &responder_sw_if_index, &data))
+       {
+         r = ikev2_set_profile_responder_hostname (vm, name, data,
+                                                   responder_sw_if_index);
          goto done;
        }
       else if (unformat (line_input, "set %U tunnel %U",
-                        unformat_token, valid_chars, &name,
+                        unformat_ikev2_token, &name,
                         unformat_vnet_sw_interface, vnm, &tun_sw_if_index))
        {
          r = ikev2_set_profile_tunnel_interface (vm, name, tun_sw_if_index);
@@ -419,7 +464,7 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
        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_token, &name,
             unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
             unformat_ikev2_transform_integ_type, &integ_alg,
             unformat_ikev2_transform_dh_type, &dh_type))
@@ -433,7 +478,7 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
        if (unformat
            (line_input,
             "set %U ike-crypto-alg %U %u ike-dh %U",
-            unformat_token, valid_chars, &name,
+            unformat_ikev2_token, &name,
             unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
             unformat_ikev2_transform_dh_type, &dh_type))
        {
@@ -447,7 +492,7 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
        if (unformat
            (line_input,
             "set %U esp-crypto-alg %U %u esp-integ-alg %U",
-            unformat_token, valid_chars, &name,
+            unformat_ikev2_token, &name,
             unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
             unformat_ikev2_transform_integ_type, &integ_alg))
        {
@@ -459,7 +504,7 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
       else if (unformat
               (line_input,
                "set %U esp-crypto-alg %U %u",
-               unformat_token, valid_chars, &name,
+               unformat_ikev2_token, &name,
                unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1))
        {
          r =
@@ -467,7 +512,7 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
          goto done;
        }
       else if (unformat (line_input, "set %U sa-lifetime %lu %u %u %lu",
-                        unformat_token, valid_chars, &name,
+                        unformat_ikev2_token, &name,
                         &tmp4, &tmp1, &tmp2, &tmp5))
        {
          r =
@@ -475,19 +520,25 @@ ikev2_profile_add_del_command_fn (vlib_main_t * vm,
          goto done;
        }
       else if (unformat (line_input, "set %U udp-encap",
-                        unformat_token, valid_chars, &name))
+                        unformat_ikev2_token, &name))
        {
          r = ikev2_set_profile_udp_encap (vm, name);
          goto done;
        }
       else if (unformat (line_input, "set %U ipsec-over-udp port %u",
-                        unformat_token, valid_chars, &name, &tmp1))
+                        unformat_ikev2_token, &name, &tmp1))
        {
          int rv = ikev2_set_profile_ipsec_udp_port (vm, name, tmp1, 1);
          if (rv)
            r = clib_error_return (0, "Error: %U", format_vnet_api_errno, rv);
          goto done;
        }
+      else if (unformat (line_input, "set %U disable natt",
+                        unformat_ikev2_token, &name))
+       {
+         r = ikev2_profile_natt_disable (name);
+         goto done;
+       }
       else
        break;
     }
@@ -519,7 +570,8 @@ VLIB_CLI_COMMAND (ikev2_profile_add_del_command, static) = {
     "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>]\n"
-    "ikev2 profile set <id> sa-lifetime <seconds> <jitter> <handover> <max bytes>",
+    "ikev2 profile set <id> sa-lifetime <seconds> <jitter> <handover> <max bytes>"
+    "ikev2 profile set <id> disable natt\n",
     .function = ikev2_profile_add_del_command_fn,
 };
 /* *INDENT-ON* */
@@ -533,7 +585,7 @@ show_ikev2_profile_command_fn (vlib_main_t * vm,
   ikev2_profile_t *p;
 
   /* *INDENT-OFF* */
-  pool_foreach (p, km->profiles, ({
+  pool_foreach (p, km->profiles)  {
     vlib_cli_output(vm, "profile %v", p->name);
 
     if (p->auth.data)
@@ -548,62 +600,40 @@ show_ikev2_profile_command_fn (vlib_main_t * vm,
       }
 
     if (p->loc_id.data)
-      {
-        if (p->loc_id.type == IKEV2_ID_TYPE_ID_IPV4_ADDR)
-          vlib_cli_output(vm, "  local id-type %U data %U",
-                          format_ikev2_id_type, p->loc_id.type,
-                          format_ip4_address, p->loc_id.data);
-        else if (p->loc_id.type == IKEV2_ID_TYPE_ID_KEY_ID)
-          vlib_cli_output(vm, "  local id-type %U data 0x%U",
-                          format_ikev2_id_type, p->loc_id.type,
-                          format_hex_bytes, p->loc_id.data,
-                          vec_len(p->loc_id.data));
-        else
-          vlib_cli_output(vm, "  local id-type %U data %v",
-                          format_ikev2_id_type, p->loc_id.type, p->loc_id.data);
-      }
+      vlib_cli_output(vm, "  local %U", format_ikev2_id_type_and_data, &p->loc_id);
 
     if (p->rem_id.data)
-      {
-        if (p->rem_id.type == IKEV2_ID_TYPE_ID_IPV4_ADDR)
-          vlib_cli_output(vm, "  remote id-type %U data %U",
-                          format_ikev2_id_type, p->rem_id.type,
-                          format_ip4_address, p->rem_id.data);
-        else if (p->rem_id.type == IKEV2_ID_TYPE_ID_KEY_ID)
-          vlib_cli_output(vm, "  remote id-type %U data 0x%U",
-                          format_ikev2_id_type, p->rem_id.type,
-                          format_hex_bytes, p->rem_id.data,
-                          vec_len(p->rem_id.data));
-        else
-          vlib_cli_output(vm, "  remote id-type %U data %v",
-                          format_ikev2_id_type, p->rem_id.type, p->rem_id.data);
-      }
+      vlib_cli_output(vm, "  remote %U", format_ikev2_id_type_and_data, &p->rem_id);
 
-    if (p->loc_ts.end_addr.as_u32)
+    if (!ip_address_is_zero (&p->loc_ts.start_addr))
       vlib_cli_output(vm, "  local traffic-selector addr %U - %U port %u - %u"
                       " protocol %u",
-                      format_ip4_address, &p->loc_ts.start_addr,
-                      format_ip4_address, &p->loc_ts.end_addr,
+                      format_ip_address, &p->loc_ts.start_addr,
+                      format_ip_address, &p->loc_ts.end_addr,
                       p->loc_ts.start_port, p->loc_ts.end_port,
                       p->loc_ts.protocol_id);
 
-    if (p->rem_ts.end_addr.as_u32)
+    if (!ip_address_is_zero (&p->rem_ts.start_addr))
       vlib_cli_output(vm, "  remote traffic-selector addr %U - %U port %u - %u"
                       " protocol %u",
-                      format_ip4_address, &p->rem_ts.start_addr,
-                      format_ip4_address, &p->rem_ts.end_addr,
+                      format_ip_address, &p->rem_ts.start_addr,
+                      format_ip_address, &p->rem_ts.end_addr,
                       p->rem_ts.start_port, p->rem_ts.end_port,
                       p->rem_ts.protocol_id);
     if (~0 != p->tun_itf)
       vlib_cli_output(vm, "  protected tunnel %U",
                       format_vnet_sw_if_index_name, vnet_get_main(), p->tun_itf);
     if (~0 != p->responder.sw_if_index)
-      vlib_cli_output(vm, "  responder %U %U",
-                      format_vnet_sw_if_index_name, vnet_get_main(), p->responder.sw_if_index,
-                      format_ip4_address, &p->responder.ip4);
+      vlib_cli_output (vm, "  responder %U %U %v",
+                      format_vnet_sw_if_index_name, vnet_get_main (),
+                      p->responder.sw_if_index, format_ip_address,
+                      &p->responder.addr, p->responder.hostname);
     if (p->udp_encap)
       vlib_cli_output(vm, "  udp-encap");
 
+    if (p->natt_disabled)
+      vlib_cli_output(vm, "  NAT-T disabled");
+
     if (p->ipsec_over_udp_port != IPSEC_UDP_PORT_NONE)
       vlib_cli_output(vm, "  ipsec-over-udp port %d", p->ipsec_over_udp_port);
 
@@ -620,7 +650,7 @@ show_ikev2_profile_command_fn (vlib_main_t * vm,
 
     vlib_cli_output(vm, "  lifetime %d jitter %d handover %d maxdata %d",
                     p->lifetime, p->lifetime_jitter, p->handover, p->lifetime_maxdata);
-  }));
+  }
   /* *INDENT-ON* */
 
   return 0;
@@ -725,15 +755,12 @@ ikev2_initiate_command_fn (vlib_main_t * vm,
   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))
+      if (unformat (line_input, "sa-init %U", unformat_ikev2_token, &name))
        {
          r = ikev2_initiate_sa_init (vm, name);
          goto done;
@@ -773,7 +800,7 @@ VLIB_CLI_COMMAND (ikev2_initiate_command, static) = {
         "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",
+        "ikev2 initiate rekey-child-sa <child sa ispi>\n",
     .function = ikev2_initiate_command_fn,
 };
 /* *INDENT-ON* */