nat: harden ICMP handling
[vpp.git] / src / plugins / nat / nat44-ed / nat44_ed.c
index 1a996cd..dcd7ae0 100644 (file)
@@ -1393,17 +1393,46 @@ update_per_vrf_sessions_vec (u32 fib_index, int is_del)
     }
 }
 
+static_always_inline nat_outside_fib_t *
+nat44_ed_get_outside_fib (nat_outside_fib_t *outside_fibs, u32 fib_index)
+{
+  nat_outside_fib_t *f;
+  vec_foreach (f, outside_fibs)
+    {
+      if (f->fib_index == fib_index)
+       {
+         return f;
+       }
+    }
+  return 0;
+}
+
+static_always_inline snat_interface_t *
+nat44_ed_get_interface (snat_interface_t *interfaces, u32 sw_if_index)
+{
+  snat_interface_t *i;
+  pool_foreach (i, interfaces)
+    {
+      if (i->sw_if_index == sw_if_index)
+       {
+         return i;
+       }
+    }
+  return 0;
+}
+
 int
-snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
+nat44_ed_add_interface (u32 sw_if_index, u8 is_inside)
 {
+  const char *del_feature_name, *feature_name;
   snat_main_t *sm = &snat_main;
+
+  nat_outside_fib_t *outside_fib;
+  snat_static_mapping_t *m;
   snat_interface_t *i;
-  const char *feature_name, *del_feature_name;
   snat_address_t *ap;
-  snat_static_mapping_t *m;
-  nat_outside_fib_t *outside_fib;
-  u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
-                                                      sw_if_index);
+  u32 fib_index;
+  int rv;
 
   if (!sm->enabled)
     {
@@ -1411,197 +1440,238 @@ snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
       return VNET_API_ERROR_UNSUPPORTED;
     }
 
-  pool_foreach (i, sm->output_feature_interfaces)
-   {
-    if (i->sw_if_index == sw_if_index)
-      {
-        nat_log_err ("error interface already configured");
-        return VNET_API_ERROR_VALUE_EXIST;
-      }
-  }
+  if (nat44_ed_get_interface (sm->output_feature_interfaces, sw_if_index))
+    {
+      nat_log_err ("error interface already configured");
+      return VNET_API_ERROR_VALUE_EXIST;
+    }
 
-  if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
-    feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
-  else
+  i = nat44_ed_get_interface (sm->interfaces, sw_if_index);
+  if (i)
     {
+      if ((nat_interface_is_inside (i) && is_inside) ||
+         (nat_interface_is_outside (i) && !is_inside))
+       {
+         return 0;
+       }
       if (sm->num_workers > 1)
-       feature_name =
-         is_inside ? "nat44-in2out-worker-handoff" :
-         "nat44-out2in-worker-handoff";
+       {
+         del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
+                                         "nat44-out2in-worker-handoff";
+         feature_name = "nat44-handoff-classify";
+       }
       else
-       feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
+       {
+         del_feature_name = !is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
+
+         feature_name = "nat44-ed-classify";
+       }
+
+      rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
+      if (rv)
+       return rv;
+      vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
+                                  sw_if_index, 0, 0, 0);
+      vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1,
+                                  0, 0);
     }
+  else
+    {
+      if (sm->num_workers > 1)
+       {
+         feature_name = is_inside ? "nat44-in2out-worker-handoff" :
+                                    "nat44-out2in-worker-handoff";
+       }
+      else
+       {
+         feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
+       }
 
-  ASSERT (sm->frame_queue_nelts > 0);
+      nat_validate_interface_counters (sm, sw_if_index);
+      rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
+      if (rv)
+       return rv;
+      vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1,
+                                  0, 0);
 
-  if (sm->fq_in2out_index == ~0 && sm->num_workers > 1)
-    sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index,
-                                                     sm->frame_queue_nelts);
+      pool_get (sm->interfaces, i);
+      i->sw_if_index = sw_if_index;
+      i->flags = 0;
+    }
 
-  if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
-    sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index,
-                                                     sm->frame_queue_nelts);
+  fib_index =
+    fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
 
-  update_per_vrf_sessions_vec (fib_index, is_del);
+  update_per_vrf_sessions_vec (fib_index, 0 /*is_del*/);
 
   if (!is_inside)
     {
-      vec_foreach (outside_fib, sm->outside_fibs)
-        {
-          if (outside_fib->fib_index == fib_index)
-            {
-              if (is_del)
-                {
-                 outside_fib->refcount--;
-                  if (!outside_fib->refcount)
-                    vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
-                }
-              else
-                outside_fib->refcount++;
-              goto feature_set;
-            }
-        }
-      if (!is_del)
+      i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
+
+      outside_fib = nat44_ed_get_outside_fib (sm->outside_fibs, fib_index);
+      if (outside_fib)
+       {
+         outside_fib->refcount++;
+       }
+      else
        {
          vec_add2 (sm->outside_fibs, outside_fib, 1);
-         outside_fib->refcount = 1;
          outside_fib->fib_index = fib_index;
+         outside_fib->refcount = 1;
        }
-    }
-
-feature_set:
-  pool_foreach (i, sm->interfaces)
-   {
-    if (i->sw_if_index == sw_if_index)
-      {
-        if (is_del)
-          {
-            if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
-              {
-                if (is_inside)
-                  i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
-                else
-                  i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
-
-                if (sm->num_workers > 1)
-                  {
-                    del_feature_name = "nat44-handoff-classify";
-                    feature_name = !is_inside ?  "nat44-in2out-worker-handoff" :
-                                                 "nat44-out2in-worker-handoff";
-                  }
-               else
-                 {
-                   del_feature_name = "nat44-ed-classify";
-                   feature_name =
-                     !is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
-                 }
 
-               int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
-               if (rv)
-                 return rv;
-                vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
-                                             sw_if_index, 0, 0, 0);
-                vnet_feature_enable_disable ("ip4-unicast", feature_name,
-                                             sw_if_index, 1, 0, 0);
-             }
-           else
-             {
-               int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
-               if (rv)
-                 return rv;
-                vnet_feature_enable_disable ("ip4-unicast", feature_name,
-                                             sw_if_index, 0, 0, 0);
-                pool_put (sm->interfaces, i);
-             }
-         }
-       else
-         {
-           if ((nat_interface_is_inside (i) && is_inside) ||
-               (nat_interface_is_outside (i) && !is_inside))
-             return 0;
+      vec_foreach (ap, sm->addresses)
+       {
+         snat_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, 1);
+       }
+      pool_foreach (m, sm->static_mappings)
+       {
+         if (!(is_addr_only_static_mapping (m)) ||
+             (m->local_addr.as_u32 == m->external_addr.as_u32))
+           {
+             continue;
+           }
+         snat_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index, 1);
+       }
+    }
+  else
+    {
+      i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
+    }
 
-           if (sm->num_workers > 1)
-             {
-               del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
-                                               "nat44-out2in-worker-handoff";
-               feature_name = "nat44-handoff-classify";
-             }
-           else
-             {
-               del_feature_name =
-                 !is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
+  return 0;
+}
 
-               feature_name = "nat44-ed-classify";
-             }
+int
+nat44_ed_del_interface (u32 sw_if_index, u8 is_inside)
+{
+  const char *del_feature_name, *feature_name;
+  snat_main_t *sm = &snat_main;
 
-           int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
-           if (rv)
-             return rv;
-            vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
-                                         sw_if_index, 0, 0, 0);
-            vnet_feature_enable_disable ("ip4-unicast", feature_name,
-                                         sw_if_index, 1, 0, 0);
-           goto set_flags;
-         }
+  nat_outside_fib_t *outside_fib;
+  snat_static_mapping_t *m;
+  snat_interface_t *i;
+  snat_address_t *ap;
+  u32 fib_index;
+  int rv;
 
-       goto fib;
-      }
-  }
+  if (!sm->enabled)
+    {
+      nat_log_err ("nat44 is disabled");
+      return VNET_API_ERROR_UNSUPPORTED;
+    }
 
-  if (is_del)
+  i = nat44_ed_get_interface (sm->interfaces, sw_if_index);
+  if (i == 0)
     {
       nat_log_err ("error interface couldn't be found");
       return VNET_API_ERROR_NO_SUCH_ENTRY;
     }
 
-  pool_get (sm->interfaces, i);
-  i->sw_if_index = sw_if_index;
-  i->flags = 0;
-  nat_validate_interface_counters (sm, sw_if_index);
-
-  vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
-                              0);
+  if (nat_interface_is_inside (i) && nat_interface_is_outside (i))
+    {
+      if (sm->num_workers > 1)
+       {
+         del_feature_name = "nat44-handoff-classify";
+         feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
+                                     "nat44-out2in-worker-handoff";
+       }
+      else
+       {
+         del_feature_name = "nat44-ed-classify";
+         feature_name = !is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
+       }
 
-  int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
-  if (rv)
-    return rv;
+      rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
+      if (rv)
+       {
+         return rv;
+       }
+      vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
+                                  sw_if_index, 0, 0, 0);
+      vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1,
+                                  0, 0);
 
-set_flags:
-  if (is_inside)
-    {
-      i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
-      return 0;
+      if (is_inside)
+       {
+         i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
+       }
+      else
+       {
+         i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
+       }
     }
   else
-    i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
+    {
+      if (sm->num_workers > 1)
+       {
+         feature_name = is_inside ? "nat44-in2out-worker-handoff" :
+                                    "nat44-out2in-worker-handoff";
+       }
+      else
+       {
+         feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
+       }
 
-  /* Add/delete external addresses to FIB */
-fib:
-  vec_foreach (ap, sm->addresses)
-    snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
+      rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
+      if (rv)
+       {
+         return rv;
+       }
+      vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 0,
+                                  0, 0);
 
-  pool_foreach (m, sm->static_mappings)
-   {
-    if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
-      continue;
+      // remove interface
+      pool_put (sm->interfaces, i);
+    }
 
-    snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
-  }
+  fib_index =
+    fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
+
+  update_per_vrf_sessions_vec (fib_index, 1 /*is_del*/);
+
+  if (!is_inside)
+    {
+      outside_fib = nat44_ed_get_outside_fib (sm->outside_fibs, fib_index);
+      if (outside_fib)
+       {
+         outside_fib->refcount--;
+         if (!outside_fib->refcount)
+           {
+             vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
+           }
+       }
+
+      vec_foreach (ap, sm->addresses)
+       {
+         snat_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, 0);
+       }
+
+      pool_foreach (m, sm->static_mappings)
+       {
+         if (!(is_addr_only_static_mapping (m)) ||
+             (m->local_addr.as_u32 == m->external_addr.as_u32))
+           {
+             continue;
+           }
+         snat_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index, 0);
+       }
+    }
 
   return 0;
 }
 
 int
-snat_interface_add_del_output_feature (u32 sw_if_index,
-                                      u8 is_inside, int is_del)
+nat44_ed_add_output_interface (u32 sw_if_index)
 {
   snat_main_t *sm = &snat_main;
+
+  nat_outside_fib_t *outside_fib;
+  snat_static_mapping_t *m;
   snat_interface_t *i;
   snat_address_t *ap;
-  snat_static_mapping_t *m;
-  nat_outside_fib_t *outside_fib;
-  u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
-                                                      sw_if_index);
+  u32 fib_index;
+  int rv;
 
   if (!sm->enabled)
     {
@@ -1609,148 +1679,196 @@ snat_interface_add_del_output_feature (u32 sw_if_index,
       return VNET_API_ERROR_UNSUPPORTED;
     }
 
-  if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
+  if (nat44_ed_get_interface (sm->interfaces, sw_if_index))
     {
-      nat_log_err ("error unsupported");
-      return VNET_API_ERROR_UNSUPPORTED;
+      nat_log_err ("error interface already configured");
+      return VNET_API_ERROR_VALUE_EXIST;
     }
 
-  pool_foreach (i, sm->interfaces)
-   {
-    if (i->sw_if_index == sw_if_index)
-      {
-        nat_log_err ("error interface already configured");
-        return VNET_API_ERROR_VALUE_EXIST;
-      }
-  }
+  if (nat44_ed_get_interface (sm->output_feature_interfaces, sw_if_index))
+    {
+      nat_log_err ("error interface already configured");
+      return VNET_API_ERROR_VALUE_EXIST;
+    }
 
-  update_per_vrf_sessions_vec (fib_index, is_del);
+  if (sm->num_workers > 1)
+    {
+      rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
+      if (rv)
+       {
+         return rv;
+       }
 
-  if (!is_inside)
+      rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 1);
+      if (rv)
+       {
+         return rv;
+       }
+
+      vnet_feature_enable_disable (
+       "ip4-unicast", "nat44-out2in-worker-handoff", sw_if_index, 1, 0, 0);
+      vnet_feature_enable_disable ("ip4-output",
+                                  "nat44-in2out-output-worker-handoff",
+                                  sw_if_index, 1, 0, 0);
+    }
+  else
     {
-      vec_foreach (outside_fib, sm->outside_fibs)
-        {
-          if (outside_fib->fib_index == fib_index)
-            {
-              if (is_del)
-                {
-                 outside_fib->refcount--;
-                  if (!outside_fib->refcount)
-                    vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
-                }
-              else
-                outside_fib->refcount++;
-              goto feature_set;
-            }
-        }
-      if (!is_del)
+      rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
+      if (rv)
        {
-         vec_add2 (sm->outside_fibs, outside_fib, 1);
-         outside_fib->refcount = 1;
-         outside_fib->fib_index = fib_index;
+         return rv;
+       }
+
+      rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 1);
+      if (rv)
+       {
+         return rv;
        }
+
+      vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
+                                  sw_if_index, 1, 0, 0);
+      vnet_feature_enable_disable ("ip4-output", "nat-pre-in2out-output",
+                                  sw_if_index, 1, 0, 0);
     }
 
-feature_set:
-  if (is_inside)
+  nat_validate_interface_counters (sm, sw_if_index);
+
+  pool_get (sm->output_feature_interfaces, i);
+  i->sw_if_index = sw_if_index;
+  i->flags = 0;
+  i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
+  i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
+
+  fib_index =
+    fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
+  update_per_vrf_sessions_vec (fib_index, 0 /*is_del*/);
+
+  outside_fib = nat44_ed_get_outside_fib (sm->outside_fibs, fib_index);
+  if (outside_fib)
     {
-         int rv =
-           ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
-         if (rv)
-           return rv;
-         rv =
-           ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
-                                                           !is_del);
-         if (rv)
-           return rv;
-      goto fq;
+      outside_fib->refcount++;
+    }
+  else
+    {
+      vec_add2 (sm->outside_fibs, outside_fib, 1);
+      outside_fib->fib_index = fib_index;
+      outside_fib->refcount = 1;
+    }
+
+  vec_foreach (ap, sm->addresses)
+    {
+      snat_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, 1);
+    }
+
+  pool_foreach (m, sm->static_mappings)
+    {
+      if (!((is_addr_only_static_mapping (m))) ||
+         (m->local_addr.as_u32 == m->external_addr.as_u32))
+       {
+         continue;
+       }
+      snat_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index, 1);
+    }
+
+  return 0;
+}
+
+int
+nat44_ed_del_output_interface (u32 sw_if_index)
+{
+  snat_main_t *sm = &snat_main;
+
+  nat_outside_fib_t *outside_fib;
+  snat_static_mapping_t *m;
+  snat_interface_t *i;
+  snat_address_t *ap;
+  u32 fib_index;
+  int rv;
+
+  if (!sm->enabled)
+    {
+      nat_log_err ("nat44 is disabled");
+      return VNET_API_ERROR_UNSUPPORTED;
+    }
+
+  i = nat44_ed_get_interface (sm->output_feature_interfaces, sw_if_index);
+  if (!i)
+    {
+      nat_log_err ("error interface couldn't be found");
+      return VNET_API_ERROR_NO_SUCH_ENTRY;
     }
 
   if (sm->num_workers > 1)
     {
-      int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
+      rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
       if (rv)
-       return rv;
-      rv =
-       ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
+       {
+         return rv;
+       }
+
+      rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 0);
       if (rv)
-       return rv;
-      vnet_feature_enable_disable ("ip4-unicast",
-                                  "nat44-out2in-worker-handoff",
-                                  sw_if_index, !is_del, 0, 0);
+       {
+         return rv;
+       }
+
+      vnet_feature_enable_disable (
+       "ip4-unicast", "nat44-out2in-worker-handoff", sw_if_index, 0, 0, 0);
       vnet_feature_enable_disable ("ip4-output",
                                   "nat44-in2out-output-worker-handoff",
-                                  sw_if_index, !is_del, 0, 0);
+                                  sw_if_index, 0, 0, 0);
     }
   else
     {
-         int rv =
-           ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
-         if (rv)
-           return rv;
-         rv =
-           ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
-                                                           !is_del);
-         if (rv)
-           return rv;
-         vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
-                                      sw_if_index, !is_del, 0, 0);
-         vnet_feature_enable_disable ("ip4-output", "nat-pre-in2out-output",
-                                      sw_if_index, !is_del, 0, 0);
-    }
-
-fq:
-  if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
-    sm->fq_in2out_output_index =
-      vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
-
-  if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
-    sm->fq_out2in_index =
-      vlib_frame_queue_main_init (sm->out2in_node_index, 0);
-
-  pool_foreach (i, sm->output_feature_interfaces)
-   {
-    if (i->sw_if_index == sw_if_index)
-      {
-        if (is_del)
-          pool_put (sm->output_feature_interfaces, i);
-        else
-          return VNET_API_ERROR_VALUE_EXIST;
+      rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
+      if (rv)
+       {
+         return rv;
+       }
 
-        goto fib;
-      }
-  }
+      rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 0);
+      if (rv)
+       {
+         return rv;
+       }
 
-  if (is_del)
-    {
-      nat_log_err ("error interface couldn't be found");
-      return VNET_API_ERROR_NO_SUCH_ENTRY;
+      vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
+                                  sw_if_index, 0, 0, 0);
+      vnet_feature_enable_disable ("ip4-output", "nat-pre-in2out-output",
+                                  sw_if_index, 0, 0, 0);
     }
 
-  pool_get (sm->output_feature_interfaces, i);
-  i->sw_if_index = sw_if_index;
-  i->flags = 0;
-  nat_validate_interface_counters (sm, sw_if_index);
-  if (is_inside)
-    i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
-  else
-    i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
+  // remove interface
+  pool_put (sm->output_feature_interfaces, i);
 
-  /* Add/delete external addresses to FIB */
-fib:
-  if (is_inside)
-    return 0;
+  fib_index =
+    fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
+  update_per_vrf_sessions_vec (fib_index, 1 /*is_del*/);
+
+  outside_fib = nat44_ed_get_outside_fib (sm->outside_fibs, fib_index);
+  if (outside_fib)
+    {
+      outside_fib->refcount--;
+      if (!outside_fib->refcount)
+       {
+         vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
+       }
+    }
 
   vec_foreach (ap, sm->addresses)
-    snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
+    {
+      snat_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, 0);
+    }
 
   pool_foreach (m, sm->static_mappings)
-   {
-    if (!((is_addr_only_static_mapping(m)))  || (m->local_addr.as_u32 == m->external_addr.as_u32))
-      continue;
-
-    snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
-  }
+    {
+      if (!((is_addr_only_static_mapping (m))) ||
+         (m->local_addr.as_u32 == m->external_addr.as_u32))
+       {
+         continue;
+       }
+      snat_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index, 0);
+    }
 
   return 0;
 }
@@ -1936,14 +2054,14 @@ static clib_error_t *
 nat_ip_table_add_del (vnet_main_t * vnm, u32 table_id, u32 is_add)
 {
   u32 fib_index;
-
-      // TODO: consider removing all NAT interfaces
-      if (!is_add)
+  if (!is_add)
+    {
+      fib_index = ip4_fib_index_from_table_id (table_id);
+      if (fib_index != ~0)
        {
-         fib_index = ip4_fib_index_from_table_id (table_id);
-         if (fib_index != ~0)
-           expire_per_vrf_sessions (fib_index);
+         expire_per_vrf_sessions (fib_index);
        }
+    }
   return 0;
 }
 
@@ -2022,6 +2140,7 @@ nat_init (vlib_main_t * vm)
   sm->log_level = NAT_LOG_ERROR;
 
   nat44_set_node_indexes (sm, vm);
+
   sm->log_class = vlib_log_register_class ("nat", 0);
   nat_ipfix_logging_init (vm);
 
@@ -2061,14 +2180,15 @@ nat_init (vlib_main_t * vm)
   /* Use all available workers by default */
   if (sm->num_workers > 1)
     {
-
       for (i = 0; i < sm->num_workers; i++)
        bitmap = clib_bitmap_set (bitmap, i, 1);
       snat_set_workers (bitmap);
       clib_bitmap_free (bitmap);
     }
   else
-    sm->per_thread_data[0].snat_thread_index = 0;
+    {
+      sm->per_thread_data[0].snat_thread_index = 0;
+    }
 
   /* callbacks to call when interface address changes. */
   cbi.function = snat_ip4_add_del_interface_address_cb;
@@ -2102,19 +2222,12 @@ nat44_plugin_enable (nat44_config_t c)
 
   fail_if_enabled ();
 
-  // UPDATE based on these appropriate API/CLI
-  // c.static_mapping_only + c.connection_tracking
-  //  - supported in NAT EI & NAT ED
-  // c.out2in_dpo, c.static_mapping_only
-  //  - supported in NAT EI
-
   if (c.static_mapping_only && !c.connection_tracking)
     {
       nat_log_err ("unsupported combination of configuration");
       return 1;
     }
 
-  // nat44 feature configuration
   sm->static_mapping_only = c.static_mapping_only;
   sm->static_mapping_connection_tracking = c.connection_tracking;
 
@@ -2131,7 +2244,6 @@ nat44_plugin_enable (nat44_config_t c)
                                  sm->max_translations_per_thread);
   sm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
 
-  // ED only feature
   vec_add1 (sm->max_translations_per_fib, sm->max_translations_per_thread);
 
   sm->inside_vrf_id = c.inside_vrf;
@@ -2152,7 +2264,28 @@ nat44_plugin_enable (nat44_config_t c)
   vlib_zero_simple_counter (&sm->total_sessions, 0);
 
   if (!sm->frame_queue_nelts)
-    sm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT;
+    {
+      sm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT;
+    }
+
+  if (sm->num_workers > 1)
+    {
+      if (sm->fq_in2out_index == ~0)
+       {
+         sm->fq_in2out_index = vlib_frame_queue_main_init (
+           sm->in2out_node_index, sm->frame_queue_nelts);
+       }
+      if (sm->fq_out2in_index == ~0)
+       {
+         sm->fq_out2in_index = vlib_frame_queue_main_init (
+           sm->out2in_node_index, sm->frame_queue_nelts);
+       }
+      if (sm->fq_in2out_output_index == ~0)
+       {
+         sm->fq_in2out_output_index = vlib_frame_queue_main_init (
+           sm->in2out_output_node_index, sm->frame_queue_nelts);
+       }
+    }
 
   sm->enabled = 1;
   sm->rconfig = c;
@@ -2179,44 +2312,44 @@ int
 nat44_plugin_disable ()
 {
   snat_main_t *sm = &snat_main;
-  snat_interface_t *i, *vec;
+  snat_interface_t *i, *pool;
   int error = 0;
 
   fail_if_disabled ();
 
-  // first unregister all nodes from interfaces
-  vec = vec_dup (sm->interfaces);
-  vec_foreach (i, vec)
+  pool = pool_dup (sm->interfaces);
+  pool_foreach (i, pool)
     {
-      if (nat_interface_is_inside(i))
-        error = snat_interface_add_del (i->sw_if_index, 1, 1);
-      if (nat_interface_is_outside(i))
-        error = snat_interface_add_del (i->sw_if_index, 0, 1);
-
+      if (nat_interface_is_inside (i))
+       {
+         error = nat44_ed_del_interface (i->sw_if_index, 1);
+       }
+      if (nat_interface_is_outside (i))
+       {
+         error = nat44_ed_del_interface (i->sw_if_index, 0);
+       }
       if (error)
         {
           nat_log_err ("error occurred while removing interface %u",
                        i->sw_if_index);
         }
     }
-  vec_free (vec);
+  pool_free (sm->interfaces);
+  pool_free (pool);
   sm->interfaces = 0;
 
-  vec = vec_dup (sm->output_feature_interfaces);
-  vec_foreach (i, vec)
+  pool = pool_dup (sm->output_feature_interfaces);
+  pool_foreach (i, pool)
     {
-      if (nat_interface_is_inside(i))
-        error = snat_interface_add_del_output_feature (i->sw_if_index, 1, 1);
-      if (nat_interface_is_outside(i))
-        error = snat_interface_add_del_output_feature (i->sw_if_index, 0, 1);
-
+      error = nat44_ed_del_output_interface (i->sw_if_index);
       if (error)
         {
           nat_log_err ("error occurred while removing interface %u",
                        i->sw_if_index);
         }
     }
-  vec_free (vec);
+  pool_free (sm->output_feature_interfaces);
+  pool_free (pool);
   sm->output_feature_interfaces = 0;
 
   vec_free (sm->max_translations_per_fib);
@@ -3268,10 +3401,9 @@ nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f)
     }
 }
 
-static_always_inline int nat_6t_flow_icmp_translate (snat_main_t *sm,
-                                                    vlib_buffer_t *b,
-                                                    ip4_header_t *ip,
-                                                    nat_6t_flow_t *f);
+static_always_inline int
+nat_6t_flow_icmp_translate (vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b,
+                           ip4_header_t *ip, nat_6t_flow_t *f);
 
 static_always_inline void
 nat_6t_flow_ip4_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
@@ -3348,7 +3480,16 @@ nat_6t_flow_ip4_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
 }
 
 static_always_inline int
-nat_6t_flow_icmp_translate (snat_main_t *sm, vlib_buffer_t *b,
+it_fits (vlib_main_t *vm, vlib_buffer_t *b, void *object, size_t size)
+{
+  int result = ((u8 *) object + size <=
+               (u8 *) vlib_buffer_get_current (b) + b->current_length) &&
+              vlib_object_within_buffer_data (vm, b, object, size);
+  return result;
+}
+
+static_always_inline int
+nat_6t_flow_icmp_translate (vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b,
                            ip4_header_t *ip, nat_6t_flow_t *f)
 {
   if (IP_PROTOCOL_ICMP != ip->protocol)
@@ -3359,8 +3500,19 @@ nat_6t_flow_icmp_translate (snat_main_t *sm, vlib_buffer_t *b,
 
   if ((!vnet_buffer (b)->ip.reass.is_non_first_fragment))
     {
-      if (icmp->checksum == 0)
-       icmp->checksum = 0xffff;
+      if (!it_fits (vm, b, icmp, sizeof (*icmp)))
+       {
+         return NAT_ED_TRNSL_ERR_PACKET_TRUNCATED;
+       }
+
+      ssize_t icmp_offset = (u8 *) icmp - (u8 *) vlib_buffer_get_current (b);
+      ip_csum_t sum =
+       ip_incremental_checksum (0, icmp, b->current_length - icmp_offset);
+      sum = (u16) ~ip_csum_fold (sum);
+      if (sum != 0)
+       {
+         return NAT_ED_TRNSL_ERR_INVALID_CSUM;
+       }
 
       if (!icmp_type_is_error_message (icmp->type))
        {
@@ -3382,7 +3534,7 @@ nat_6t_flow_icmp_translate (snat_main_t *sm, vlib_buffer_t *b,
 
          if (!ip4_header_checksum_is_valid (inner_ip))
            {
-             return NAT_ED_TRNSL_ERR_TRANSLATION_FAILED;
+             return NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT;
            }
 
          nat_protocol_t inner_proto =
@@ -3400,6 +3552,10 @@ nat_6t_flow_icmp_translate (snat_main_t *sm, vlib_buffer_t *b,
            {
            case NAT_PROTOCOL_UDP:
              udp = (udp_header_t *) (inner_ip + 1);
+             if (!it_fits (vm, b, udp, sizeof (*udp)))
+               {
+                 return NAT_ED_TRNSL_ERR_PACKET_TRUNCATED;
+               }
              old_udp_sum = udp->checksum;
              nat_6t_flow_ip4_translate (sm, b, inner_ip, f, inner_proto,
                                         1 /* is_icmp_inner_ip4 */,
@@ -3413,12 +3569,14 @@ nat_6t_flow_icmp_translate (snat_main_t *sm, vlib_buffer_t *b,
                ip_csum_update (new_icmp_sum, old_udp_sum, udp->checksum,
                                udp_header_t, checksum);
              new_icmp_sum = ip_csum_fold (new_icmp_sum);
-             if (0xffff == new_icmp_sum)
-               new_icmp_sum = 0;
              icmp->checksum = new_icmp_sum;
              break;
            case NAT_PROTOCOL_TCP:
              tcp = (tcp_header_t *) (inner_ip + 1);
+             if (!it_fits (vm, b, tcp, sizeof (*tcp)))
+               {
+                 return NAT_ED_TRNSL_ERR_PACKET_TRUNCATED;
+               }
              old_tcp_sum = tcp->checksum;
              nat_6t_flow_ip4_translate (sm, b, inner_ip, f, inner_proto,
                                         1 /* is_icmp_inner_ip4 */,
@@ -3432,14 +3590,16 @@ nat_6t_flow_icmp_translate (snat_main_t *sm, vlib_buffer_t *b,
                ip_csum_update (new_icmp_sum, old_tcp_sum, tcp->checksum,
                                tcp_header_t, checksum);
              new_icmp_sum = ip_csum_fold (new_icmp_sum);
-             if (0xffff == new_icmp_sum)
-               new_icmp_sum = 0;
              icmp->checksum = new_icmp_sum;
              break;
            case NAT_PROTOCOL_ICMP:
              if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE)
                {
                  icmp46_header_t *inner_icmp = ip4_next_header (inner_ip);
+                 if (!it_fits (vm, b, inner_icmp, sizeof (*inner_icmp)))
+                   {
+                     return NAT_ED_TRNSL_ERR_PACKET_TRUNCATED;
+                   }
                  icmp_echo_header_t *inner_echo =
                    (icmp_echo_header_t *) (inner_icmp + 1);
                  if (f->rewrite.icmp_id != inner_echo->identifier)
@@ -3469,9 +3629,10 @@ nat_6t_flow_icmp_translate (snat_main_t *sm, vlib_buffer_t *b,
 }
 
 static_always_inline nat_translation_error_e
-nat_6t_flow_buf_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
-                          nat_6t_flow_t *f, nat_protocol_t proto,
-                          int is_output_feature, int is_i2o)
+nat_6t_flow_buf_translate (vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b,
+                          ip4_header_t *ip, nat_6t_flow_t *f,
+                          nat_protocol_t proto, int is_output_feature,
+                          int is_i2o)
 {
   if (!is_output_feature && f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
     {
@@ -3494,7 +3655,7 @@ nat_6t_flow_buf_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
                                     0 /* is_icmp_inner_ip4 */,
                                     0 /* skip_saddr_rewrite */);
        }
-      return nat_6t_flow_icmp_translate (sm, b, ip, f);
+      return nat_6t_flow_icmp_translate (vm, sm, b, ip, f);
     }
 
   nat_6t_flow_ip4_translate (sm, b, ip, f, proto, 0 /* is_icmp_inner_ip4 */,
@@ -3504,20 +3665,22 @@ nat_6t_flow_buf_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
 }
 
 nat_translation_error_e
-nat_6t_flow_buf_translate_i2o (snat_main_t *sm, vlib_buffer_t *b,
-                              ip4_header_t *ip, nat_6t_flow_t *f,
-                              nat_protocol_t proto, int is_output_feature)
+nat_6t_flow_buf_translate_i2o (vlib_main_t *vm, snat_main_t *sm,
+                              vlib_buffer_t *b, ip4_header_t *ip,
+                              nat_6t_flow_t *f, nat_protocol_t proto,
+                              int is_output_feature)
 {
-  return nat_6t_flow_buf_translate (sm, b, ip, f, proto, is_output_feature,
+  return nat_6t_flow_buf_translate (vm, sm, b, ip, f, proto, is_output_feature,
                                    1 /* is_i2o */);
 }
 
 nat_translation_error_e
-nat_6t_flow_buf_translate_o2i (snat_main_t *sm, vlib_buffer_t *b,
-                              ip4_header_t *ip, nat_6t_flow_t *f,
-                              nat_protocol_t proto, int is_output_feature)
+nat_6t_flow_buf_translate_o2i (vlib_main_t *vm, snat_main_t *sm,
+                              vlib_buffer_t *b, ip4_header_t *ip,
+                              nat_6t_flow_t *f, nat_protocol_t proto,
+                              int is_output_feature)
 {
-  return nat_6t_flow_buf_translate (sm, b, ip, f, proto, is_output_feature,
+  return nat_6t_flow_buf_translate (vm, sm, b, ip, f, proto, is_output_feature,
                                    0 /* is_i2o */);
 }
 
@@ -3550,6 +3713,15 @@ format_nat_ed_translation_error (u8 *s, va_list *args)
     case NAT_ED_TRNSL_ERR_FLOW_MISMATCH:
       s = format (s, "flow-mismatch");
       break;
+    case NAT_ED_TRNSL_ERR_PACKET_TRUNCATED:
+      s = format (s, "packet-truncated");
+      break;
+    case NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT:
+      s = format (s, "inner-ip-corrupted");
+      break;
+    case NAT_ED_TRNSL_ERR_INVALID_CSUM:
+      s = format (s, "invalid-checksum");
+      break;
     }
   return s;
 }