nat: deny adding intf addr if static-mapping-only
[vpp.git] / src / plugins / nat / nat44_api.c
index 37c3dba..a5e2919 100644 (file)
@@ -31,7 +31,8 @@
 #include <nat/nat44.api_enum.h>
 #include <nat/nat44.api_types.h>
 
-#include <nat/nat_ha.h>
+#include <nat/nat44-ei/nat44_ei_ha.h>
+#include <nat/nat44-ei/nat44_ei.h>
 #include <nat/nat_inlines.h>
 
 #include <nat/nat44/inlines.h>
@@ -332,6 +333,31 @@ vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp)
   /* *INDENT-ON* */
 }
 
+static void
+vl_api_nat_set_fq_options_t_handler (vl_api_nat_set_fq_options_t *mp)
+{
+  snat_main_t *sm = &snat_main;
+  vl_api_nat_set_fq_options_reply_t *rmp;
+  int rv = 0;
+  u32 frame_queue_nelts = ntohl (mp->frame_queue_nelts);
+  rv = snat_set_frame_queue_nelts (frame_queue_nelts);
+  REPLY_MACRO (VL_API_NAT_SET_FQ_OPTIONS_REPLY);
+}
+
+static void
+vl_api_nat_show_fq_options_t_handler (vl_api_nat_show_fq_options_t *mp)
+{
+  vl_api_nat_show_fq_options_reply_t *rmp;
+  snat_main_t *sm = &snat_main;
+  int rv = 0;
+  /* clang-format off */
+  REPLY_MACRO2_ZERO (VL_API_NAT_SHOW_FQ_OPTIONS_REPLY,
+  ({
+    rmp->frame_queue_nelts = htonl (sm->frame_queue_nelts);
+  }));
+  /* clang-format on */
+}
+
 static void
   vl_api_nat_set_addr_and_port_alloc_alg_t_handler
   (vl_api_nat_set_addr_and_port_alloc_alg_t * mp)
@@ -350,11 +376,11 @@ static void
   switch (mp->alg)
     {
     case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
-      nat_set_alloc_addr_and_port_default ();
+      nat44_ei_set_alloc_default ();
       break;
     case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
-      nat_set_alloc_addr_and_port_mape (ntohs (mp->psid), mp->psid_offset,
-                                       mp->psid_length);
+      nat44_ei_set_alloc_mape (ntohs (mp->psid), mp->psid_offset,
+                              mp->psid_length);
       break;
     case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
       port_start = ntohs (mp->start_port);
@@ -364,7 +390,7 @@ static void
          rv = VNET_API_ERROR_INVALID_VALUE;
          goto send_reply;
        }
-      nat_set_alloc_addr_and_port_range (port_start, port_end);
+      nat44_ei_set_alloc_range (port_start, port_end);
       break;
     default:
       rv = VNET_API_ERROR_INVALID_VALUE;
@@ -725,10 +751,10 @@ vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
     return;
 
   /* *INDENT-OFF* */
-  pool_foreach (i, sm->interfaces,
-  ({
+  pool_foreach (i, sm->interfaces)
+   {
     send_nat44_interface_details(i, reg, mp->context);
-  }));
+  }
   /* *INDENT-ON* */
 }
 
@@ -785,10 +811,10 @@ static void
     return;
 
   /* *INDENT-OFF* */
-  pool_foreach (i, sm->output_feature_interfaces,
-  ({
+  pool_foreach (i, sm->output_feature_interfaces)
+   {
     send_nat44_interface_output_feature_details(i, reg, mp->context);
-  }));
+  }
   /* *INDENT-ON* */
 }
 
@@ -827,13 +853,10 @@ static void
   tag = format (0, "%s", mp->tag);
   vec_terminate_c_string (tag);
 
-  rv = snat_add_static_mapping (local_addr, external_addr, local_port,
-                               external_port, vrf_id,
-                               mp->flags & NAT_API_IS_ADDR_ONLY,
-                               external_sw_if_index, proto,
-                               mp->is_add, twice_nat,
-                               mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0,
-                               pool_addr, 0);
+  rv = snat_add_static_mapping (
+    local_addr, external_addr, local_port, external_port, vrf_id,
+    mp->flags & NAT_API_IS_ADDR_ONLY, external_sw_if_index, proto, mp->is_add,
+    twice_nat, mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0, pool_addr, 0);
   vec_free (tag);
 
   REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
@@ -982,11 +1005,11 @@ vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
     return;
 
   /* *INDENT-OFF* */
-  pool_foreach (m, sm->static_mappings,
-  ({
+  pool_foreach (m, sm->static_mappings)
+   {
       if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
         send_nat44_static_mapping_details (m, reg, mp->context);
-  }));
+  }
   /* *INDENT-ON* */
 
   for (j = 0; j < vec_len (sm->to_resolve); j++)
@@ -1104,16 +1127,16 @@ static void
     return;
 
   /* *INDENT-OFF* */
-  pool_foreach (m, sm->static_mappings,
-  ({
+  pool_foreach (m, sm->static_mappings)
+   {
       if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
         {
-          pool_foreach_index (j, m->locals,
-          ({
+          pool_foreach_index (j, m->locals)
+           {
             send_nat44_identity_mapping_details (m, j, reg, mp->context);
-          }));
+          }
         }
-  }));
+  }
   /* *INDENT-ON* */
 
   for (j = 0; j < vec_len (sm->to_resolve); j++)
@@ -1134,6 +1157,12 @@ static void
   int rv = 0;
   u8 is_del;
 
+  if (sm->static_mapping_only)
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto send_reply;
+    }
+
   is_del = !mp->is_add;
 
   VALIDATE_SW_IF_INDEX (mp);
@@ -1142,6 +1171,8 @@ static void
                                   mp->flags & NAT_API_IS_TWICE_NAT);
 
   BAD_SW_IF_INDEX_LABEL;
+
+send_reply:
   REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
 }
 
@@ -1251,7 +1282,7 @@ nat_ed_users_create (snat_main_per_thread_data_t * tsm)
 {
   snat_session_t *s;
   /* *INDENT-OFF* */
-  pool_foreach (s, tsm->sessions, { nat_ed_user_create_helper (tsm, s); });
+  pool_foreach (s, tsm->sessions) { nat_ed_user_create_helper (tsm, s); }
   /* *INDENT-ON* */
 }
 
@@ -1286,10 +1317,10 @@ vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
        {
          nat_ed_users_create (tsm);
        }
-      pool_foreach (u, tsm->users,
-      ({
+      pool_foreach (u, tsm->users)
+       {
         send_nat44_user_details (u, reg, mp->context);
-      }));
+      }
       if (sm->endpoint_dependent)
        {
          nat_ed_users_destroy (tsm);
@@ -1407,12 +1438,12 @@ vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
   else
     {
       /* *INDENT-OFF* */
-      pool_foreach (s, tsm->sessions, {
+      pool_foreach (s, tsm->sessions) {
         if (s->in2out.addr.as_u32 == ukey.addr.as_u32)
           {
             send_nat44_user_session_details (s, reg, mp->context);
           }
-      });
+      }
       /* *INDENT-ON* */
     }
 }
@@ -1509,10 +1540,9 @@ static void
 
   rv =
     nat44_lb_static_mapping_add_del_local (e_addr,
-                                          clib_net_to_host_u16
-                                          (mp->external_port), l_addr,
-                                          clib_net_to_host_u16 (mp->
-                                                                local.port),
+                                          mp->external_port,
+                                          l_addr,
+                                          mp->local.port,
                                           proto,
                                           clib_net_to_host_u32 (mp->
                                                                 local.vrf_id),
@@ -1557,15 +1587,15 @@ send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
 
   locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
   /* *INDENT-OFF* */
-  pool_foreach (ap, m->locals,
-  ({
+  pool_foreach (ap, m->locals)
+   {
     clib_memcpy (locals->addr, &(ap->addr), 4);
     locals->port = ap->port;
     locals->probability = ap->probability;
     locals->vrf_id = ntohl (ap->vrf_id);
     locals++;
     local_num++;
-  }));
+  }
   /* *INDENT-ON* */
   rmp->local_num = ntohl (local_num);
 
@@ -1588,11 +1618,11 @@ static void
     return;
 
   /* *INDENT-OFF* */
-  pool_foreach (m, sm->static_mappings,
-  ({
+  pool_foreach (m, sm->static_mappings)
+   {
       if (is_lb_static_mapping(m))
         send_nat44_lb_static_mapping_details (m, reg, mp->context);
-  }));
+  }
   /* *INDENT-ON* */
 }
 
@@ -1622,7 +1652,7 @@ vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
       nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
                            vrf_id, is_in);
   else
-    rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
+    rv = nat44_ei_del_session (sm, &addr, port, proto, vrf_id, is_in);
 
   REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
 }
@@ -1645,13 +1675,13 @@ static void
       /* *INDENT-OFF* */
       vec_foreach (tsm, sm->per_thread_data)
       {
-        pool_foreach (s, tsm->sessions,
-        ({
+        pool_foreach (s, tsm->sessions)
+         {
           if (is_fwd_bypass_session(s))
             {
               vec_add1 (ses_to_be_removed, s - tsm->sessions);
             }
-        }));
+        }
        if(sm->endpoint_dependent){
            vec_foreach (ses_index, ses_to_be_removed)
              {