NAT44 virtual fragmentation reassembly for endpoint-dependent mode (VPP-1325)
[vpp.git] / src / plugins / nat / in2out.c
index 2a41b95..335bd85 100755 (executable)
@@ -123,6 +123,7 @@ vlib_node_registration_t nat44_ed_in2out_output_slowpath_node;
 vlib_node_registration_t nat44_ed_hairpin_dst_node;
 vlib_node_registration_t nat44_ed_hairpin_src_node;
 vlib_node_registration_t nat44_ed_hairpinning_node;
+vlib_node_registration_t nat44_ed_in2out_reass_node;
 
 #define foreach_snat_in2out_error                       \
 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol")         \
@@ -191,6 +192,7 @@ snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t *node,
     return 0;
 
   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
+  nat_outside_fib_t *outside_fib;
   fib_prefix_t pfx = {
     .fp_proto = FIB_PROTOCOL_IP4,
     .fp_len = 32,
@@ -210,10 +212,20 @@ snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t *node,
       u32 sw_if_index = fib_entry_get_resolving_interface (fei);
       if (sw_if_index == ~0)
         {
-          fei = fib_table_lookup (sm->outside_fib_index, &pfx);
-          if (FIB_NODE_INDEX_INVALID != fei)
-            sw_if_index = fib_entry_get_resolving_interface (fei);
+          vec_foreach (outside_fib, sm->outside_fibs)
+            {
+              fei = fib_table_lookup (outside_fib->fib_index, &pfx);
+              if (FIB_NODE_INDEX_INVALID != fei)
+                {
+                  sw_if_index = fib_entry_get_resolving_interface (fei);
+                  if (sw_if_index != ~0)
+                    break;
+                }
+            }
         }
+      if (sw_if_index == ~0)
+        return 1;
+
       snat_interface_t *i;
       pool_foreach (i, sm->interfaces,
       ({
@@ -247,7 +259,7 @@ snat_not_translate (snat_main_t * sm, vlib_node_runtime_t *node,
                               &value0))
     {
       /* or is static mappings */
-      if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+      if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
         return 0;
     }
   else
@@ -273,7 +285,7 @@ nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
   key0.addr = ip0->src_address;
   key0.port = src_port;
   key0.protocol = proto0;
-  key0.fib_index = sm->outside_fib_index;
+  key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
   kv0.key = key0.as_u64;
 
   if (!clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
@@ -284,7 +296,6 @@ nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
   key0.addr = ip0->dst_address;
   key0.port = dst_port;
   key0.protocol = proto0;
-  key0.fib_index = sm->inside_fib_index;
   kv0.key = key0.as_u64;
   if (!clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
                                &value0))
@@ -301,6 +312,43 @@ nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
   return 0;
 }
 
+int
+nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void * arg)
+{
+  snat_main_t *sm = &snat_main;
+  nat44_is_idle_session_ctx_t *ctx = arg;
+  snat_session_t *s;
+  u64 sess_timeout_time;
+  snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
+                                                       ctx->thread_index);
+  clib_bihash_kv_8_8_t s_kv;
+
+  s = pool_elt_at_index (tsm->sessions, kv->value);
+  sess_timeout_time = s->last_heard + (f64)nat44_session_get_timeout(sm, s);
+  if (ctx->now >= sess_timeout_time)
+    {
+      s_kv.key = s->out2in.as_u64;
+      if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
+        nat_log_warn ("out2in key del failed");
+
+      snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
+                                          s->out2in.addr.as_u32,
+                                          s->in2out.protocol,
+                                          s->in2out.port,
+                                          s->out2in.port,
+                                          s->in2out.fib_index);
+
+      if (!snat_is_session_static (s))
+        snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
+                                            &s->out2in);
+
+      nat44_delete_session (sm, s, ctx->thread_index);
+      return 1;
+    }
+
+  return 0;
+}
+
 static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
                       ip4_header_t * ip0,
                       u32 rx_fib_index0,
@@ -308,17 +356,26 @@ static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
                       snat_session_t ** sessionp,
                       vlib_node_runtime_t * node,
                       u32 next0,
-                      u32 thread_index)
+                      u32 thread_index,
+                      f64 now)
 {
   snat_user_t *u;
   snat_session_t *s;
   clib_bihash_kv_8_8_t kv0;
   snat_session_key_t key1;
   u32 address_index = ~0;
-  u32 outside_fib_index;
-  uword * p;
   udp_header_t * udp0 = ip4_next_header (ip0);
   u8 is_sm = 0;
+  nat_outside_fib_t *outside_fib;
+  fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
+  fib_prefix_t pfx = {
+    .fp_proto = FIB_PROTOCOL_IP4,
+    .fp_len = 32,
+    .fp_addr = {
+        .ip4.as_u32 = ip0->dst_address.as_u32,
+    },
+  };
+  nat44_is_idle_session_ctx_t ctx0;
 
   if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
     {
@@ -328,26 +385,10 @@ static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
       return SNAT_IN2OUT_NEXT_DROP;
     }
 
-  p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
-  if (! p)
-    {
-      b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB];
-      return SNAT_IN2OUT_NEXT_DROP;
-    }
-  outside_fib_index = p[0];
-
   key1.protocol = key0->protocol;
 
-  u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
-                              thread_index);
-  if (!u)
-    {
-      nat_log_warn ("create NAT user failed");
-      return SNAT_IN2OUT_NEXT_DROP;
-    }
-
   /* First try to match static mapping by local address and port */
-  if (snat_static_mapping_match (sm, *key0, &key1, 0, 0, 0, 0))
+  if (snat_static_mapping_match (sm, *key0, &key1, 0, 0, 0, 0, 0))
     {
       /* Try to create dynamic translation */
       if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
@@ -363,9 +404,18 @@ static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
   else
     is_sm = 1;
 
+  u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
+                              thread_index);
+  if (!u)
+    {
+      nat_log_warn ("create NAT user failed");
+      return SNAT_IN2OUT_NEXT_DROP;
+    }
+
   s = nat_session_alloc_or_recycle (sm, u, thread_index);
   if (!s)
     {
+      nat44_delete_user_with_no_session (sm, u, thread_index);
       nat_log_warn ("create NAT session failed");
       return SNAT_IN2OUT_NEXT_DROP;
     }
@@ -377,23 +427,50 @@ static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
   s->in2out = *key0;
   s->out2in = key1;
   s->out2in.protocol = key0->protocol;
-  s->out2in.fib_index = outside_fib_index;
+  s->out2in.fib_index = sm->outside_fib_index;
+  switch (vec_len (sm->outside_fibs))
+    {
+    case 0:
+      s->out2in.fib_index = sm->outside_fib_index;
+      break;
+    case 1:
+      s->out2in.fib_index = sm->outside_fibs[0].fib_index;
+      break;
+    default:
+      vec_foreach (outside_fib, sm->outside_fibs)
+        {
+          fei = fib_table_lookup (outside_fib->fib_index, &pfx);
+          if (FIB_NODE_INDEX_INVALID != fei)
+            {
+              if (fib_entry_get_resolving_interface (fei) != ~0)
+                {
+                  s->out2in.fib_index = outside_fib->fib_index;
+                  break;
+                }
+            }
+        }
+      break;
+    }
   s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
   s->ext_host_port = udp0->dst_port;
   *sessionp = s;
 
   /* Add to translation hashes */
+  ctx0.now = now;
+  ctx0.thread_index = thread_index;
   kv0.key = s->in2out.as_u64;
   kv0.value = s - sm->per_thread_data[thread_index].sessions;
-  if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
-                               1 /* is_add */))
+  if (clib_bihash_add_or_overwrite_stale_8_8 (
+        &sm->per_thread_data[thread_index].in2out, &kv0,
+        nat44_i2o_is_idle_session_cb, &ctx0))
       nat_log_notice ("in2out key add failed");
 
   kv0.key = s->out2in.as_u64;
   kv0.value = s - sm->per_thread_data[thread_index].sessions;
 
-  if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
-                               1 /* is_add */))
+  if (clib_bihash_add_or_overwrite_stale_8_8 (
+        &sm->per_thread_data[thread_index].out2in, &kv0,
+        nat44_o2i_is_idle_session_cb, &ctx0))
       nat_log_notice ("out2in key add failed");
 
   /* log NAT event */
@@ -525,8 +602,8 @@ u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
           goto out;
         }
 
-      next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
-                         &s0, node, next0, thread_index);
+      next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
+                         thread_index, vlib_time_now (sm->vlib_main));
 
       if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
         goto out;
@@ -598,7 +675,7 @@ u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
     }
   key0.fib_index = rx_fib_index0;
 
-  if (snat_static_mapping_match(sm, key0, &sm0, 0, &is_addr_only, 0, 0))
+  if (snat_static_mapping_match(sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0))
     {
       if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
           IP_PROTOCOL_ICMP, rx_fib_index0)))
@@ -635,6 +712,13 @@ out2:
   return next0;
 }
 
+static inline u32
+snat_icmp_hairpinning (snat_main_t *sm,
+                       vlib_buffer_t * b0,
+                       ip4_header_t * ip0,
+                       icmp46_header_t * icmp0,
+                       int is_ed);
+
 static inline u32 icmp_in2out (snat_main_t *sm,
                                vlib_buffer_t * b0,
                                ip4_header_t * ip0,
@@ -656,6 +740,7 @@ static inline u32 icmp_in2out (snat_main_t *sm,
   u8 dont_translate;
   u32 new_addr0, old_addr0;
   u16 old_id0, new_id0;
+  u16 old_checksum0, new_checksum0;
   ip_csum_t sum0;
   u16 checksum0;
   u32 next0_tmp;
@@ -669,19 +754,20 @@ static inline u32 icmp_in2out (snat_main_t *sm,
   if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
     goto out;
 
-  sum0 = ip_incremental_checksum (0, icmp0,
-                                  ntohs(ip0->length) - ip4_header_bytes (ip0));
-  checksum0 = ~ip_csum_fold (sum0);
-  if (PREDICT_FALSE(checksum0 != 0 && checksum0 != 0xffff))
+  if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
     {
-      next0 = SNAT_IN2OUT_NEXT_DROP;
-      goto out;
+      sum0 = ip_incremental_checksum (0, icmp0,
+                                      ntohs(ip0->length) - ip4_header_bytes (ip0));
+      checksum0 = ~ip_csum_fold (sum0);
+      if (PREDICT_FALSE(checksum0 != 0 && checksum0 != 0xffff))
+        {
+          next0 = SNAT_IN2OUT_NEXT_DROP;
+          goto out;
+        }
     }
 
   old_addr0 = ip0->src_address.as_u32;
   new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
-  if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0)
-    vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
 
   sum0 = ip0->checksum;
   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
@@ -717,15 +803,27 @@ static inline u32 icmp_in2out (snat_main_t *sm,
           goto out;
         }
 
+      /* update inner destination IP address */
       old_addr0 = inner_ip0->dst_address.as_u32;
       inner_ip0->dst_address = sm0.addr;
       new_addr0 = inner_ip0->dst_address.as_u32;
-
       sum0 = icmp0->checksum;
       sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
                              dst_address /* changed member */);
       icmp0->checksum = ip_csum_fold (sum0);
 
+      /* update inner IP header checksum */
+      old_checksum0 = inner_ip0->checksum;
+      sum0 = inner_ip0->checksum;
+      sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
+                             dst_address /* changed member */);
+      inner_ip0->checksum = ip_csum_fold (sum0);
+      new_checksum0 = inner_ip0->checksum;
+      sum0 = icmp0->checksum;
+      sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
+                             checksum);
+      icmp0->checksum = ip_csum_fold (sum0);
+
       switch (protocol)
         {
           case SNAT_PROTOCOL_ICMP:
@@ -757,6 +855,14 @@ static inline u32 icmp_in2out (snat_main_t *sm,
         }
     }
 
+  if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0)
+    {
+      if (sm->deterministic ||
+          0 != snat_icmp_hairpinning(sm, b0, ip0, icmp0,
+                                     sm->endpoint_dependent))
+        vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
+    }
+
 out:
   return next0;
 }
@@ -799,7 +905,7 @@ snat_hairpinning (snat_main_t *sm,
   kv0.key = key0.as_u64;
 
   /* Check if destination is static mappings */
-  if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+  if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
     {
       new_dst_addr0 = sm0.addr.as_u32;
       new_dst_port0 = sm0.port;
@@ -882,84 +988,153 @@ snat_hairpinning (snat_main_t *sm,
   return 0;
 }
 
-static inline void
+static inline u32
 snat_icmp_hairpinning (snat_main_t *sm,
                        vlib_buffer_t * b0,
                        ip4_header_t * ip0,
                        icmp46_header_t * icmp0,
                        int is_ed)
 {
-  snat_session_key_t key0, sm0;
+  snat_session_key_t key0;
   clib_bihash_kv_8_8_t kv0, value0;
-  u32 new_dst_addr0 = 0, old_dst_addr0, si, ti = 0;
+  u32 old_dst_addr0, new_dst_addr0;
+  u32 old_addr0, new_addr0;
+  u16 old_port0, new_port0;
+  u16 old_checksum0, new_checksum0;
+  u32 si, ti = 0;
   ip_csum_t sum0;
   snat_session_t *s0;
-  int rv;
+  snat_static_mapping_t *m0;
 
-  if (!icmp_is_error_message (icmp0))
+  if (icmp_is_error_message (icmp0))
     {
-      icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
-      u16 icmp_id0 = echo0->identifier;
-      key0.addr = ip0->dst_address;
-      key0.port = icmp_id0;
-      key0.protocol = SNAT_PROTOCOL_ICMP;
-      key0.fib_index = sm->outside_fib_index;
-      kv0.key = key0.as_u64;
+      ip4_header_t *inner_ip0 = 0;
+      tcp_udp_header_t *l4_header = 0;
 
-      if (sm->num_workers > 1)
-        ti = (clib_net_to_host_u16 (icmp_id0) - 1024) / sm->port_per_thread;
-      else
-        ti = sm->num_workers;
+      inner_ip0 = (ip4_header_t *)((icmp_echo_header_t *)(icmp0+1)+1);
+      l4_header = ip4_next_header (inner_ip0);
+      u32 protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
+
+      if (protocol != SNAT_PROTOCOL_TCP && protocol != SNAT_PROTOCOL_UDP)
+        return 1;
 
-      /* Check if destination is in active sessions */
       if (is_ed)
         {
           clib_bihash_kv_16_8_t ed_kv, ed_value;
           make_ed_kv (&ed_kv, &ip0->dst_address, &ip0->src_address,
-                      IP_PROTOCOL_ICMP, sm->outside_fib_index, icmp_id0, 0);
-          rv = clib_bihash_search_16_8 (&sm->per_thread_data[ti].out2in_ed,
-                                        &ed_kv, &ed_value);
+                     inner_ip0->protocol, sm->outside_fib_index,
+                     l4_header->src_port, l4_header->dst_port);
+          if (clib_bihash_search_16_8 (&sm->per_thread_data[ti].out2in_ed,
+                                       &ed_kv, &ed_value))
+            return 1;
           si = ed_value.value;
         }
       else
         {
-          rv = clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
-                                       &value0);
+          key0.addr = ip0->dst_address;
+          key0.port = l4_header->src_port;
+          key0.protocol = protocol;
+          key0.fib_index = sm->outside_fib_index;
+          kv0.key = key0.as_u64;
+          if (clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
+                                      &value0))
+            return 1;
           si = value0.value;
         }
-      if (rv)
+      s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
+      new_dst_addr0 = s0->in2out.addr.as_u32;
+      vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
+
+      /* update inner source IP address */
+      old_addr0 = inner_ip0->src_address.as_u32;
+      inner_ip0->src_address.as_u32 = new_dst_addr0;
+      new_addr0 = inner_ip0->src_address.as_u32;
+      sum0 = icmp0->checksum;
+      sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
+                             src_address);
+      icmp0->checksum = ip_csum_fold (sum0);
+
+      /* update inner IP header checksum */
+      old_checksum0 = inner_ip0->checksum;
+      sum0 = inner_ip0->checksum;
+      sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
+                             src_address);
+      inner_ip0->checksum = ip_csum_fold (sum0);
+      new_checksum0 = inner_ip0->checksum;
+      sum0 = icmp0->checksum;
+      sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
+                             checksum);
+      icmp0->checksum = ip_csum_fold (sum0);
+
+      /* update inner source port */
+      old_port0 = l4_header->src_port;
+      l4_header->src_port = s0->in2out.port;
+      new_port0 = l4_header->src_port;
+      sum0 = icmp0->checksum;
+      sum0 = ip_csum_update (sum0, old_port0, new_port0, tcp_udp_header_t,
+                             src_port);
+      icmp0->checksum = ip_csum_fold (sum0);
+    }
+  else
+    {
+      if (!is_ed)
         {
-          /* or static mappings */
-          if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+          icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
+          u16 icmp_id0 = echo0->identifier;
+          key0.addr = ip0->dst_address;
+          key0.port = icmp_id0;
+          key0.protocol = SNAT_PROTOCOL_ICMP;
+          key0.fib_index = sm->outside_fib_index;
+          kv0.key = key0.as_u64;
+          if (sm->num_workers > 1)
+            ti = (clib_net_to_host_u16 (icmp_id0) - 1024) / sm->port_per_thread;
+          else
+            ti = sm->num_workers;
+          int rv = clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
+                                           &value0);
+          if (!rv)
             {
-              new_dst_addr0 = sm0.addr.as_u32;
-              vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
+              si = value0.value;
+              s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
+              new_dst_addr0 = s0->in2out.addr.as_u32;
+              vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
+              echo0->identifier = s0->in2out.port;
+              sum0 = icmp0->checksum;
+              sum0 = ip_csum_update (sum0, icmp_id0, s0->in2out.port,
+                                     icmp_echo_header_t, identifier);
+              icmp0->checksum = ip_csum_fold (sum0);
+              goto change_addr;
             }
-        }
-      else
-        {
-          s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
-          new_dst_addr0 = s0->in2out.addr.as_u32;
-          vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
-          echo0->identifier = s0->in2out.port;
-          sum0 = icmp0->checksum;
-          sum0 = ip_csum_update (sum0, icmp_id0, s0->in2out.port,
-                                 icmp_echo_header_t, identifier);
-          icmp0->checksum = ip_csum_fold (sum0);
+          ti = 0;
         }
 
-      /* Destination is behind the same NAT, use internal address and port */
-      if (new_dst_addr0)
-        {
-          old_dst_addr0 = ip0->dst_address.as_u32;
-          ip0->dst_address.as_u32 = new_dst_addr0;
-          sum0 = ip0->checksum;
-          sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
-                                 ip4_header_t, dst_address);
-          ip0->checksum = ip_csum_fold (sum0);
-        }
-    }
+      key0.addr = ip0->dst_address;
+      key0.port = 0;
+      key0.protocol = 0;
+      key0.fib_index = sm->outside_fib_index;
+      kv0.key = key0.as_u64;
 
+      if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
+        return 1;
+
+      m0 = pool_elt_at_index (sm->static_mappings, value0.value);
+
+      new_dst_addr0 = m0->local_addr.as_u32;
+      if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0)
+        vnet_buffer(b0)->sw_if_index[VLIB_TX] = m0->fib_index;
+    }
+change_addr:
+  /* Destination is behind the same NAT, use internal address and port */
+  if (new_dst_addr0)
+    {
+      old_dst_addr0 = ip0->dst_address.as_u32;
+      ip0->dst_address.as_u32 = new_dst_addr0;
+      sum0 = ip0->checksum;
+      sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
+                             ip4_header_t, dst_address);
+      ip0->checksum = ip_csum_fold (sum0);
+    }
+  return 0;
 }
 
 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
@@ -979,9 +1154,6 @@ static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
   snat_session_t * s0 = *p_s0;
   if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP && s0))
     {
-      /* Hairpinning */
-      if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == 0)
-        snat_icmp_hairpinning(sm, b0, ip0, icmp0, sm->endpoint_dependent);
       /* Accounting */
       nat44_session_update_counters (s0, now,
                                      vlib_buffer_length_in_chain (sm->vlib_main, b0));
@@ -1001,7 +1173,7 @@ nat_hairpinning_sm_unknown_proto (snat_main_t * sm,
   u32 old_addr, new_addr;
   ip_csum_t sum;
 
-  make_sm_kv (&kv, &ip->dst_address, 0, sm->outside_fib_index, 0);
+  make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
     return;
 
@@ -1049,8 +1221,8 @@ nat_in2out_sm_unknown_proto (snat_main_t *sm,
   /* Hairpinning */
   if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
     {
+      vnet_buffer(b)->sw_if_index[VLIB_TX] = m->fib_index;
       nat_hairpinning_sm_unknown_proto (sm, b, ip);
-      vnet_buffer(b)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
     }
 
   return 0;
@@ -1068,7 +1240,7 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
   snat_main_t * sm = &snat_main;
   f64 now = vlib_time_now (vm);
   u32 stats_node_index;
-  u32 thread_index = vlib_get_thread_index ();
+  u32 thread_index = vm->thread_index;
 
   stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
     snat_in2out_node.index;
@@ -1219,7 +1391,7 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
                     }
 
                   next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
-                                     &s0, node, next0, thread_index);
+                                     &s0, node, next0, thread_index, now);
                   if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
                     goto trace00;
                 }
@@ -1260,6 +1432,7 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
               sum0 = ip_csum_update (sum0, old_port0, new_port0,
                                      ip4_header_t /* cheat */,
                                      length /* changed member */);
+              mss_clamping (sm, tcp0, &sum0);
               tcp0->checksum = ip_csum_fold(sum0);
             }
           else
@@ -1379,7 +1552,7 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
                     }
 
                   next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
-                                     &s1, node, next1, thread_index);
+                                     &s1, node, next1, thread_index, now);
                   if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
                     goto trace01;
                 }
@@ -1420,6 +1593,7 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
               sum1 = ip_csum_update (sum1, old_port1, new_port1,
                                      ip4_header_t /* cheat */,
                                      length /* changed member */);
+              mss_clamping (sm, tcp1, &sum1);
               tcp1->checksum = ip_csum_fold(sum1);
             }
           else
@@ -1575,7 +1749,7 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
                     }
 
                   next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
-                                     &s0, node, next0, thread_index);
+                                     &s0, node, next0, thread_index, now);
 
                   if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
                     goto trace0;
@@ -1617,6 +1791,7 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
               sum0 = ip_csum_update (sum0, old_port0, new_port0,
                                      ip4_header_t /* cheat */,
                                      length /* changed member */);
+              mss_clamping (sm, tcp0, &sum0);
               tcp0->checksum = ip_csum_fold(sum0);
             }
           else
@@ -1935,7 +2110,8 @@ nat44_reass_hairpinning (snat_main_t *sm,
                          ip4_header_t * ip0,
                          u16 sport,
                          u16 dport,
-                         u32 proto0)
+                         u32 proto0,
+                        int is_ed)
 {
   snat_session_key_t key0, sm0;
   snat_session_t * s0;
@@ -1945,6 +2121,7 @@ nat44_reass_hairpinning (snat_main_t *sm,
   u16 new_dst_port0, old_dst_port0;
   udp_header_t * udp0;
   tcp_header_t * tcp0;
+  int rv;
 
   key0.addr = ip0->dst_address;
   key0.port = dport;
@@ -1955,7 +2132,7 @@ nat44_reass_hairpinning (snat_main_t *sm,
   udp0 = ip4_next_header (ip0);
 
   /* Check if destination is static mappings */
-  if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+  if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
     {
       new_dst_addr0 = sm0.addr.as_u32;
       new_dst_port0 = sm0.port;
@@ -1969,9 +2146,24 @@ nat44_reass_hairpinning (snat_main_t *sm,
       else
         ti = sm->num_workers;
 
-      if (!clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0, &value0))
+      if (is_ed)
+        {
+          clib_bihash_kv_16_8_t ed_kv, ed_value;
+          make_ed_kv (&ed_kv, &ip0->dst_address, &ip0->src_address,
+                      ip0->protocol, sm->outside_fib_index, udp0->dst_port,
+                      udp0->src_port);
+          rv = clib_bihash_search_16_8 (&sm->per_thread_data[ti].out2in_ed,
+                                        &ed_kv, &ed_value);
+          si = ed_value.value;
+        }
+      else
         {
+          rv = clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
+                                       &value0);
           si = value0.value;
+        }
+      if (!rv)
+        {
           s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
           new_dst_addr0 = s0->in2out.addr.as_u32;
           new_dst_port0 = s0->in2out.port;
@@ -2034,7 +2226,7 @@ nat44_in2out_reass_node_fn (vlib_main_t * vm,
   u32 pkts_processed = 0;
   snat_main_t *sm = &snat_main;
   f64 now = vlib_time_now (vm);
-  u32 thread_index = vlib_get_thread_index ();
+  u32 thread_index = vm->thread_index;
   snat_main_per_thread_data_t *per_thread_data =
     &sm->per_thread_data[thread_index];
   u32 *fragments_to_drop = 0;
@@ -2123,7 +2315,7 @@ nat44_in2out_reass_node_fn (vlib_main_t * vm,
                     goto trace0;
 
                   next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
-                                     &s0, node, next0, thread_index);
+                                     &s0, node, next0, thread_index, now);
 
                   if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
                     goto trace0;
@@ -2142,7 +2334,7 @@ nat44_in2out_reass_node_fn (vlib_main_t * vm,
             {
               if (PREDICT_FALSE (reass0->sess_index == (u32) ~0))
                 {
-                  if (nat_ip4_reass_add_fragment (reass0, bi0))
+                  if (nat_ip4_reass_add_fragment (reass0, bi0, &fragments_to_drop))
                     {
                       b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
                       nat_log_notice ("maximum fragments per reassembly exceeded");
@@ -2194,7 +2386,7 @@ nat44_in2out_reass_node_fn (vlib_main_t * vm,
 
           /* Hairpinning */
           nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
-                                   s0->ext_host_port, proto0);
+                                   s0->ext_host_port, proto0, 0);
 
           /* Accounting */
           nat44_session_update_counters (s0, now,
@@ -2341,6 +2533,110 @@ icmp_get_ed_key(ip4_header_t *ip0, nat_ed_ses_key_t *p_key0)
   return 0;
 }
 
+int
+nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void * arg)
+{
+  snat_main_t *sm = &snat_main;
+  nat44_is_idle_session_ctx_t *ctx = arg;
+  snat_session_t *s;
+  u64 sess_timeout_time;
+  nat_ed_ses_key_t ed_key;
+  clib_bihash_kv_16_8_t ed_kv;
+  int i;
+  snat_address_t *a;
+  snat_session_key_t key;
+  snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
+                                                       ctx->thread_index);
+
+  s = pool_elt_at_index (tsm->sessions, kv->value);
+  sess_timeout_time = s->last_heard + (f64)nat44_session_get_timeout(sm, s);
+  if (ctx->now >= sess_timeout_time)
+    {
+      if (is_fwd_bypass_session (s))
+        goto delete;
+
+      ed_key.l_addr = s->out2in.addr;
+      ed_key.r_addr = s->ext_host_addr;
+      ed_key.fib_index = s->out2in.fib_index;
+      if (snat_is_unk_proto_session (s))
+        {
+          ed_key.proto = s->in2out.port;
+          ed_key.r_port = 0;
+          ed_key.l_port = 0;
+        }
+      else
+        {
+          ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
+          ed_key.l_port = s->out2in.port;
+          ed_key.r_port = s->ext_host_port;
+        }
+      ed_kv.key[0] = ed_key.as_u64[0];
+      ed_kv.key[1] = ed_key.as_u64[1];
+      if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
+        nat_log_warn ("out2in_ed key del failed");
+
+      if (snat_is_unk_proto_session (s))
+        goto delete;
+
+      snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
+                                          s->out2in.addr.as_u32,
+                                          s->in2out.protocol,
+                                          s->in2out.port,
+                                          s->out2in.port,
+                                          s->in2out.fib_index);
+
+      if (is_twice_nat_session (s))
+        {
+          for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
+            {
+              key.protocol = s->in2out.protocol;
+              key.port = s->ext_host_nat_port;
+              a = sm->twice_nat_addresses + i;
+              if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
+                {
+                  snat_free_outside_address_and_port (sm->twice_nat_addresses,
+                                                      ctx->thread_index, &key);
+                  break;
+                }
+            }
+        }
+
+      if (snat_is_session_static (s))
+        goto delete;
+
+      if (s->outside_address_index != ~0)
+        snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
+                                            &s->out2in);
+    delete:
+      nat44_delete_session (sm, s, ctx->thread_index);
+      return 1;
+    }
+
+  return 0;
+}
+
+static inline u32
+icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
+                          ip4_header_t * ip0, icmp46_header_t * icmp0,
+                          u32 sw_if_index0, u32 rx_fib_index0,
+                          vlib_node_runtime_t * node, u32 next0, f64 now,
+                          u32 thread_index, snat_session_t ** p_s0)
+{
+  next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
+                      next0, thread_index, p_s0, 0);
+  snat_session_t * s0 = *p_s0;
+  if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP && s0))
+    {
+      /* Hairpinning */
+      if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0)
+        snat_icmp_hairpinning(sm, b0, ip0, icmp0, sm->endpoint_dependent);
+      /* Accounting */
+      nat44_session_update_counters (s0, now,
+                                     vlib_buffer_length_in_chain (sm->vlib_main, b0));
+    }
+  return next0;
+}
+
 static u32
 slow_path_ed (snat_main_t *sm,
               vlib_buffer_t *b,
@@ -2349,16 +2645,27 @@ slow_path_ed (snat_main_t *sm,
               snat_session_t ** sessionp,
               vlib_node_runtime_t * node,
               u32 next,
-              u32 thread_index)
+              u32 thread_index,
+              f64 now)
 {
   snat_session_t *s;
   snat_user_t *u;
   snat_session_key_t key0, key1;
-  u8 lb = 0, is_sm = 0;
+  lb_nat_type_t lb = 0, is_sm = 0;
   u32 address_index = ~0;
   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
   nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
   u32 proto = ip_proto_to_snat_proto (key->proto);
+  nat_outside_fib_t *outside_fib;
+  fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
+  fib_prefix_t pfx = {
+    .fp_proto = FIB_PROTOCOL_IP4,
+    .fp_len = 32,
+    .fp_addr = {
+        .ip4.as_u32 = key->r_addr.as_u32,
+    },
+  };
+  nat44_is_idle_session_ctx_t ctx;
 
   if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
     {
@@ -2374,7 +2681,7 @@ slow_path_ed (snat_main_t *sm,
   key0.fib_index = rx_fib_index;
   key1.fib_index = sm->outside_fib_index;
   /* First try to match static mapping by local address and port */
-  if (snat_static_mapping_match (sm, key0, &key1, 0, 0, 0, &lb))
+  if (snat_static_mapping_match (sm, key0, &key1, 0, 0, 0, &lb, 0))
     {
       /* Try to create dynamic translation */
       if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index,
@@ -2395,13 +2702,20 @@ slow_path_ed (snat_main_t *sm,
   if (!u)
     {
       nat_log_warn ("create NAT user failed");
+      if (!is_sm)
+        snat_free_outside_address_and_port (sm->addresses,
+                                            thread_index, &key1);
       return SNAT_IN2OUT_NEXT_DROP;
     }
 
-  s = nat_session_alloc_or_recycle (sm, u, thread_index);
+  s = nat_ed_session_alloc (sm, u, thread_index);
   if (!s)
     {
+      nat44_delete_user_with_no_session (sm, u, thread_index);
       nat_log_warn ("create NAT session failed");
+      if (!is_sm)
+        snat_free_outside_address_and_port (sm->addresses,
+                                            thread_index, &key1);
       return SNAT_IN2OUT_NEXT_DROP;
     }
 
@@ -2418,15 +2732,45 @@ slow_path_ed (snat_main_t *sm,
   s->out2in = key1;
   s->out2in.protocol = key0.protocol;
 
+  switch (vec_len (sm->outside_fibs))
+    {
+    case 0:
+      s->out2in.fib_index = sm->outside_fib_index;
+      break;
+    case 1:
+      s->out2in.fib_index = sm->outside_fibs[0].fib_index;
+      break;
+    default:
+      vec_foreach (outside_fib, sm->outside_fibs)
+        {
+          fei = fib_table_lookup (outside_fib->fib_index, &pfx);
+          if (FIB_NODE_INDEX_INVALID != fei)
+            {
+              if (fib_entry_get_resolving_interface (fei) != ~0)
+                {
+                  s->out2in.fib_index = outside_fib->fib_index;
+                  break;
+                }
+            }
+        }
+      break;
+    }
+
   /* Add to lookup tables */
   kv->value = s - tsm->sessions;
-  if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, kv, 1))
+  ctx.now = now;
+  ctx.thread_index = thread_index;
+  if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, kv,
+                                               nat44_i2o_ed_is_idle_session_cb,
+                                               &ctx))
     nat_log_notice ("in2out-ed key add failed");
 
-  make_ed_kv (kv, &key1.addr, &key->r_addr, key->proto, key1.fib_index,
+  make_ed_kv (kv, &key1.addr, &key->r_addr, key->proto, s->out2in.fib_index,
               key1.port, key->r_port);
   kv->value = s - tsm->sessions;
-  if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, kv, 1))
+  if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, kv,
+                                               nat44_o2i_ed_is_idle_session_cb,
+                                               &ctx))
     nat_log_notice ("out2in-ed key add failed");
 
   *sessionp = s;
@@ -2463,7 +2807,7 @@ nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t *node,
       key0.protocol = proto;
       key0.fib_index = sm->outside_fib_index;
       /* or is static mappings */
-      if (!snat_static_mapping_match(sm, key0, &key1, 1, 0, 0, 0))
+      if (!snat_static_mapping_match(sm, key0, &key1, 1, 0, 0, 0, 0))
         return 0;
     }
   else
@@ -2521,8 +2865,6 @@ nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
               if (nat44_set_tcp_session_state_i2o (sm, s, tcp, thread_index))
                 return 1;
             }
-          /* Per-user LRU list maintenance */
-          nat44_session_update_lru (sm, s, thread_index);
           /* Accounting */
           nat44_session_update_counters (s, now,
                                          vlib_buffer_length_in_chain (vm, b));
@@ -2538,22 +2880,25 @@ nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
 static_always_inline int
 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
                                        u8 proto, u16 src_port, u16 dst_port,
-                                       u32 thread_index, u32 sw_if_index)
+                                       u32 thread_index, u32 rx_sw_if_index,
+                                       u32 tx_sw_if_index)
 {
   clib_bihash_kv_16_8_t kv, value;
   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
   snat_interface_t *i;
   snat_session_t *s;
+  u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
+  u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
 
   /* src NAT check */
-  make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
-              sm->outside_fib_index, src_port, dst_port);
+  make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, tx_fib_index,
+              src_port, dst_port);
   if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
     return 1;
 
   /* dst NAT check */
-  make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto,
-              sm->inside_fib_index, dst_port, src_port);
+  make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
+              dst_port, src_port);
   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
   {
     s = pool_elt_at_index (tsm->sessions, value.value);
@@ -2563,7 +2908,7 @@ nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
     /* hairpinning */
     pool_foreach (i, sm->output_feature_interfaces,
     ({
-      if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
+      if ((nat_interface_is_inside(i)) && (rx_sw_if_index == i->sw_if_index))
         return 0;
     }));
     return 1;
@@ -2611,7 +2956,8 @@ icmp_match_in2out_ed(snat_main_t *sm, vlib_node_runtime_t *node,
       if (vnet_buffer(b)->sw_if_index[VLIB_TX] != ~0)
         {
           if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(sm, ip,
-              key.proto, key.l_port, key.r_port, thread_index, sw_if_index)))
+              key.proto, key.l_port, key.r_port, thread_index, sw_if_index,
+              vnet_buffer(b)->sw_if_index[VLIB_TX])))
             {
               dont_translate = 1;
               goto out;
@@ -2635,7 +2981,7 @@ icmp_match_in2out_ed(snat_main_t *sm, vlib_node_runtime_t *node,
         }
 
       next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
-                           thread_index);
+                           thread_index, vlib_time_now (sm->vlib_main));
 
       if (PREDICT_FALSE (next == SNAT_IN2OUT_NEXT_DROP))
         goto out;
@@ -2688,7 +3034,7 @@ nat44_ed_hairpinning_unknown_proto (snat_main_t *sm,
               sm->outside_fib_index, 0, 0);
   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
     {
-      make_sm_kv (&kv, &ip->dst_address, 0, sm->outside_fib_index, 0);
+      make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
       if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
         return;
 
@@ -2729,10 +3075,42 @@ nat44_ed_in2out_unknown_proto (snat_main_t *sm,
   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
   u32 elt_index, head_index, ses_index;
   snat_session_t * s;
-  u32 address_index = ~0;
+  u32 address_index = ~0, outside_fib_index = sm->outside_fib_index;
   int i;
   u8 is_sm = 0;
+  nat_outside_fib_t *outside_fib;
+  fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
+  fib_prefix_t pfx = {
+    .fp_proto = FIB_PROTOCOL_IP4,
+    .fp_len = 32,
+    .fp_addr = {
+        .ip4.as_u32 = ip->dst_address.as_u32,
+    },
+  };
 
+  switch (vec_len (sm->outside_fibs))
+    {
+    case 0:
+      outside_fib_index = sm->outside_fib_index;
+      break;
+    case 1:
+      outside_fib_index = sm->outside_fibs[0].fib_index;
+      break;
+    default:
+      vec_foreach (outside_fib, sm->outside_fibs)
+        {
+          fei = fib_table_lookup (outside_fib->fib_index, &pfx);
+          if (FIB_NODE_INDEX_INVALID != fei)
+            {
+              if (fib_entry_get_resolving_interface (fei) != ~0)
+                {
+                  outside_fib_index = outside_fib->fib_index;
+                  break;
+                }
+            }
+        }
+      break;
+    }
   old_addr = ip->src_address.as_u32;
 
   make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
@@ -2799,7 +3177,7 @@ nat44_ed_in2out_unknown_proto (snat_main_t *sm,
                   address_index = s->outside_address_index;
 
                   make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
-                              ip->protocol, sm->outside_fib_index, 0, 0);
+                              ip->protocol, outside_fib_index, 0, 0);
                   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
                     goto create_ses;
 
@@ -2810,7 +3188,7 @@ nat44_ed_in2out_unknown_proto (snat_main_t *sm,
           for (i = 0; i < vec_len (sm->addresses); i++)
             {
               make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
-                          ip->protocol, sm->outside_fib_index, 0, 0);
+                          ip->protocol, outside_fib_index, 0, 0);
               if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
                 {
                   new_addr = ip->src_address.as_u32 =
@@ -2823,9 +3201,10 @@ nat44_ed_in2out_unknown_proto (snat_main_t *sm,
         }
 
 create_ses:
-      s = nat_session_alloc_or_recycle (sm, u, thread_index);
+      s = nat_ed_session_alloc (sm, u, thread_index);
       if (!s)
         {
+          nat44_delete_user_with_no_session (sm, u, thread_index);
           nat_log_warn ("create NAT session failed");
           return 0;
         }
@@ -2835,7 +3214,7 @@ create_ses:
       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
       s->outside_address_index = address_index;
       s->out2in.addr.as_u32 = new_addr;
-      s->out2in.fib_index = sm->outside_fib_index;
+      s->out2in.fib_index = outside_fib_index;
       s->in2out.addr.as_u32 = old_addr;
       s->in2out.fib_index = rx_fib_index;
       s->in2out.port = s->out2in.port = ip->protocol;
@@ -2851,7 +3230,7 @@ create_ses:
         nat_log_notice ("in2out key add failed");
 
       make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
-                  sm->outside_fib_index, 0, 0);
+                  outside_fib_index, 0, 0);
       s_kv.value = s - tsm->sessions;
       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
         nat_log_notice ("out2in key add failed");
@@ -2864,15 +3243,13 @@ create_ses:
 
   /* Accounting */
   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b));
-  /* Per-user LRU list maintenance */
-  nat44_session_update_lru (sm, s, thread_index);
 
   /* Hairpinning */
   if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
     nat44_ed_hairpinning_unknown_proto(sm, b, ip);
 
   if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
-    vnet_buffer(b)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
+    vnet_buffer(b)->sw_if_index[VLIB_TX] = outside_fib_index;
 
   return s;
 }
@@ -2887,7 +3264,7 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
   snat_in2out_next_t next_index;
   snat_main_t *sm = &snat_main;
   f64 now = vlib_time_now (vm);
-  u32 thread_index = vlib_get_thread_index ();
+  u32 thread_index = vm->thread_index;
   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
 
   stats_node_index = is_slow_path ? nat44_ed_in2out_slowpath_node.index :
@@ -2987,7 +3364,7 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
 
               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
                 {
-                  next0 = icmp_in2out_slow_path
+                  next0 = icmp_in2out_ed_slow_path
                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
                      next0, now, thread_index, &s0);
                   goto trace00;
@@ -2995,6 +3372,18 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
             }
           else
             {
+               if (PREDICT_FALSE (proto0 == ~0))
+                 {
+                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
+                   goto trace00;
+                 }
+
+               if (ip4_is_fragment (ip0))
+                 {
+                   next0 = SNAT_IN2OUT_NEXT_REASS;
+                   goto trace00;
+                 }
+
                if (is_output_feature)
                 {
                   if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(
@@ -3002,18 +3391,11 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     goto trace00;
                 }
 
-              if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
-                {
-                  next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
-                  goto trace00;
-                }
-
-              if (ip4_is_fragment (ip0))
-                {
-                  b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
-                  next0 = SNAT_IN2OUT_NEXT_DROP;
-                  goto trace00;
-                }
+               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
+                 {
+                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
+                   goto trace00;
+                 }
             }
 
           make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address, ip0->protocol,
@@ -3027,7 +3409,8 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     {
                       if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(
                           sm, ip0, ip0->protocol, udp0->src_port,
-                          udp0->dst_port, thread_index, sw_if_index0)))
+                          udp0->dst_port, thread_index, sw_if_index0,
+                          vnet_buffer(b0)->sw_if_index[VLIB_TX])))
                         goto trace00;
                     }
                   else
@@ -3039,7 +3422,7 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     }
 
                   next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
-                                        next0, thread_index);
+                                        next0, thread_index, now);
 
                   if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
                     goto trace00;
@@ -3092,6 +3475,7 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                   tcp0->dst_port = s0->ext_host_port;
                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
                 }
+              mss_clamping (sm, tcp0, &sum0);
               tcp0->checksum = ip_csum_fold(sum0);
               if (nat44_set_tcp_session_state_i2o (sm, s0, tcp0, thread_index))
                 goto trace00;
@@ -3110,8 +3494,6 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
           /* Accounting */
           nat44_session_update_counters (s0, now,
                                          vlib_buffer_length_in_chain (vm, b0));
-          /* Per-user LRU list maintenance */
-          nat44_session_update_lru (sm, s0, thread_index);
 
         trace00:
           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
@@ -3172,7 +3554,7 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
 
               if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
                 {
-                  next1 = icmp_in2out_slow_path
+                  next1 = icmp_in2out_ed_slow_path
                     (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
                      next1, now, thread_index, &s1);
                   goto trace01;
@@ -3180,6 +3562,18 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
             }
           else
             {
+               if (PREDICT_FALSE (proto1 == ~0))
+                 {
+                   next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
+                   goto trace01;
+                 }
+
+               if (ip4_is_fragment (ip1))
+                 {
+                   next1 = SNAT_IN2OUT_NEXT_REASS;
+                   goto trace01;
+                 }
+
                if (is_output_feature)
                 {
                   if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(
@@ -3187,18 +3581,11 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     goto trace01;
                 }
 
-              if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
-                {
-                  next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
-                  goto trace01;
-                }
-
-              if (ip4_is_fragment (ip1))
-                {
-                  b1->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
-                  next1 = SNAT_IN2OUT_NEXT_DROP;
-                  goto trace01;
-                }
+               if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
+                 {
+                   next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
+                   goto trace01;
+                 }
             }
 
           make_ed_kv (&kv1, &ip1->src_address, &ip1->dst_address, ip1->protocol,
@@ -3212,7 +3599,8 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     {
                       if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(
                           sm, ip1, ip1->protocol, udp1->src_port,
-                          udp1->dst_port, thread_index, sw_if_index1)))
+                          udp1->dst_port, thread_index, sw_if_index1,
+                          vnet_buffer(b1)->sw_if_index[VLIB_TX])))
                         goto trace01;
                     }
                   else
@@ -3224,7 +3612,7 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     }
 
                   next1 = slow_path_ed (sm, b1, rx_fib_index1, &kv1, &s1, node,
-                                        next1, thread_index);
+                                        next1, thread_index, now);
 
                   if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
                     goto trace01;
@@ -3278,6 +3666,7 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                   ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
                 }
               tcp1->checksum = ip_csum_fold(sum1);
+              mss_clamping (sm, tcp1, &sum1);
               if (nat44_set_tcp_session_state_i2o (sm, s1, tcp1, thread_index))
                 goto trace01;
             }
@@ -3295,8 +3684,6 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
           /* Accounting */
           nat44_session_update_counters (s1, now,
                                          vlib_buffer_length_in_chain (vm, b1));
-          /* Per-user LRU list maintenance */
-          nat44_session_update_lru (sm, s1, thread_index);
 
         trace01:
           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
@@ -3386,7 +3773,7 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
 
               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
                 {
-                  next0 = icmp_in2out_slow_path
+                  next0 = icmp_in2out_ed_slow_path
                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
                      next0, now, thread_index, &s0);
                   goto trace0;
@@ -3394,23 +3781,28 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
             }
           else
             {
-               if (is_output_feature)
+              if (PREDICT_FALSE (proto0 == ~0))
                 {
-                  if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(
-                      sm, ip0, thread_index, now, vm, b0)))
-                    goto trace0;
+                  next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
+                  goto trace0;
                 }
 
-              if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
+              if (ip4_is_fragment (ip0))
                 {
-                  next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
+                  next0 = SNAT_IN2OUT_NEXT_REASS;
                   goto trace0;
                 }
 
-              if (ip4_is_fragment (ip0))
+              if (is_output_feature)
+               {
+                 if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(
+                     sm, ip0, thread_index, now, vm, b0)))
+                   goto trace0;
+               }
+
+              if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
                 {
-                  b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
-                  next0 = SNAT_IN2OUT_NEXT_DROP;
+                  next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
                   goto trace0;
                 }
             }
@@ -3426,7 +3818,8 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     {
                       if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(
                           sm, ip0, ip0->protocol, udp0->src_port,
-                          udp0->dst_port, thread_index, sw_if_index0)))
+                          udp0->dst_port, thread_index, sw_if_index0,
+                          vnet_buffer(b0)->sw_if_index[VLIB_TX])))
                         goto trace0;
                     }
                   else
@@ -3438,7 +3831,7 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     }
 
                   next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
-                                        next0, thread_index);
+                                        next0, thread_index, now);
 
                   if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
                     goto trace0;
@@ -3491,6 +3884,7 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                   tcp0->dst_port = s0->ext_host_port;
                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
                 }
+              mss_clamping (sm, tcp0, &sum0);
               tcp0->checksum = ip_csum_fold(sum0);
               if (nat44_set_tcp_session_state_i2o (sm, s0, tcp0, thread_index))
                 goto trace0;
@@ -3509,8 +3903,6 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
           /* Accounting */
           nat44_session_update_counters (s0, now,
                                          vlib_buffer_length_in_chain (vm, b0));
-          /* Per-user LRU list maintenance */
-          nat44_session_update_lru (sm, s0, thread_index);
 
         trace0:
           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
@@ -3571,7 +3963,7 @@ VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
-    [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
+    [SNAT_IN2OUT_NEXT_REASS] = "nat44-ed-in2out-reass",
   },
 };
 
@@ -3605,7 +3997,7 @@ VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
     [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
-    [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
+    [SNAT_IN2OUT_NEXT_REASS] = "nat44-ed-in2out-reass-output",
   },
 };
 
@@ -3640,7 +4032,7 @@ VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
-    [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
+    [SNAT_IN2OUT_NEXT_REASS] = "nat44-ed-in2out-reass",
   },
 };
 
@@ -3675,13 +4067,394 @@ VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
     [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
-    [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
+    [SNAT_IN2OUT_NEXT_REASS] = "nat44-ed-in2out-reass",
   },
 };
 
 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_output_slowpath_node,
                               nat44_ed_in2out_output_slow_path_fn);
 
+static inline uword
+nat44_ed_in2out_reass_node_fn_inline (vlib_main_t * vm,
+                                      vlib_node_runtime_t * node,
+                                      vlib_frame_t * frame,
+                                      int is_output_feature)
+{
+  u32 n_left_from, *from, *to_next;
+  snat_in2out_next_t next_index;
+  u32 pkts_processed = 0;
+  snat_main_t *sm = &snat_main;
+  f64 now = vlib_time_now (vm);
+  u32 thread_index = vm->thread_index;
+  snat_main_per_thread_data_t *per_thread_data =
+    &sm->per_thread_data[thread_index];
+  u32 *fragments_to_drop = 0;
+  u32 *fragments_to_loopback = 0;
+
+  from = vlib_frame_vector_args (frame);
+  n_left_from = frame->n_vectors;
+  next_index = node->cached_next_index;
+
+  while (n_left_from > 0)
+    {
+      u32 n_left_to_next;
+
+      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
+
+      while (n_left_from > 0 && n_left_to_next > 0)
+       {
+          u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
+          u32 iph_offset0 = 0;
+         vlib_buffer_t *b0;
+          u32 next0;
+          u8 cached0 = 0;
+          ip4_header_t *ip0 = 0;
+          nat_reass_ip4_t *reass0;
+          udp_header_t * udp0;
+          tcp_header_t * tcp0;
+          icmp46_header_t * icmp0;
+          clib_bihash_kv_16_8_t kv0, value0;
+          snat_session_t * s0 = 0;
+          u16 old_port0, new_port0;
+          ip_csum_t sum0;
+
+          /* speculatively enqueue b0 to the current next frame */
+         bi0 = from[0];
+         to_next[0] = bi0;
+         from += 1;
+         to_next += 1;
+         n_left_from -= 1;
+         n_left_to_next -= 1;
+
+         b0 = vlib_get_buffer (vm, bi0);
+
+          next0 = SNAT_IN2OUT_NEXT_LOOKUP;
+
+          sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
+          rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
+                                                               sw_if_index0);
+
+          if (PREDICT_FALSE (nat_reass_is_drop_frag(0)))
+            {
+              next0 = SNAT_IN2OUT_NEXT_DROP;
+              b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
+              goto trace0;
+            }
+
+          if (is_output_feature)
+            iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
+
+          ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
+                 iph_offset0);
+
+          udp0 = ip4_next_header (ip0);
+          tcp0 = (tcp_header_t *) udp0;
+          icmp0 = (icmp46_header_t *) udp0;
+          proto0 = ip_proto_to_snat_proto (ip0->protocol);
+
+          reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
+                                                 ip0->dst_address,
+                                                 ip0->fragment_id,
+                                                 ip0->protocol,
+                                                 1,
+                                                 &fragments_to_drop);
+
+          if (PREDICT_FALSE (!reass0))
+            {
+              next0 = SNAT_IN2OUT_NEXT_DROP;
+              b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
+              nat_log_notice ("maximum reassemblies exceeded");
+              goto trace0;
+            }
+
+          if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
+            {
+              if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
+                {
+                  if (is_output_feature)
+                    {
+                      if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(
+                          sm, ip0, thread_index, now, vm, b0)))
+                          reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
+                        goto trace0;
+                    }
+
+                  next0 = icmp_in2out_slow_path
+                      (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
+                       next0, now, thread_index, &s0);
+
+                  if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP))
+                    {
+                      if (s0)
+                        reass0->sess_index = s0 - per_thread_data->sessions;
+                      else
+                        reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
+                      nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
+                    }
+
+                  goto trace0;
+                }
+
+              make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address, ip0->protocol,
+                          rx_fib_index0, udp0->src_port, udp0->dst_port);
+
+              if (clib_bihash_search_16_8 (&per_thread_data->in2out_ed, &kv0, &value0))
+                {
+                  if (is_output_feature)
+                    {
+                      if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(
+                          sm, ip0, ip0->protocol, udp0->src_port,
+                          udp0->dst_port, thread_index, sw_if_index0,
+                         vnet_buffer(b0)->sw_if_index[VLIB_TX])))
+                        {
+                          reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
+                          nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
+                          goto trace0;
+                        }
+                    }
+                  else
+                    {
+                      if (PREDICT_FALSE(nat44_ed_not_translate(sm, node,
+                          sw_if_index0, ip0, proto0, rx_fib_index0,
+                          thread_index)))
+                        {
+                          reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
+                          nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
+                          goto trace0;
+                        }
+                    }
+
+                  next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0,
+                                        &s0, node, next0, thread_index, now);
+
+                  if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
+                    goto trace0;
+
+                  reass0->sess_index = s0 - per_thread_data->sessions;
+                }
+              else
+                {
+                  s0 = pool_elt_at_index (per_thread_data->sessions,
+                                          value0.value);
+                  reass0->sess_index = value0.value;
+                }
+              nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
+            }
+          else
+            {
+              if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
+                goto trace0;
+              if (PREDICT_FALSE (reass0->sess_index == (u32) ~0))
+                {
+                  if (nat_ip4_reass_add_fragment (reass0, bi0, &fragments_to_drop))
+                    {
+                      b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
+                      nat_log_notice ("maximum fragments per reassembly exceeded");
+                      next0 = SNAT_IN2OUT_NEXT_DROP;
+                      goto trace0;
+                    }
+                  cached0 = 1;
+                  goto trace0;
+                }
+              s0 = pool_elt_at_index (per_thread_data->sessions,
+                                      reass0->sess_index);
+            }
+
+          old_addr0 = ip0->src_address.as_u32;
+          ip0->src_address = s0->out2in.addr;
+          new_addr0 = ip0->src_address.as_u32;
+          if (!is_output_feature)
+            vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
+
+          sum0 = ip0->checksum;
+          sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
+                                 ip4_header_t,
+                                 src_address /* changed member */);
+          if (PREDICT_FALSE (is_twice_nat_session (s0)))
+            sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
+                                   s0->ext_host_addr.as_u32, ip4_header_t,
+                                   dst_address);
+          ip0->checksum = ip_csum_fold (sum0);
+
+          if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
+            {
+              if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
+                {
+                  old_port0 = tcp0->src_port;
+                  tcp0->src_port = s0->out2in.port;
+                  new_port0 = tcp0->src_port;
+
+                  sum0 = tcp0->checksum;
+                  sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
+                                         ip4_header_t,
+                                         dst_address /* changed member */);
+                  sum0 = ip_csum_update (sum0, old_port0, new_port0,
+                                         ip4_header_t /* cheat */,
+                                         length /* changed member */);
+                  if (PREDICT_FALSE (is_twice_nat_session (s0)))
+                    {
+                      sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
+                                             s0->ext_host_addr.as_u32,
+                                             ip4_header_t, dst_address);
+                      sum0 = ip_csum_update (sum0, tcp0->dst_port,
+                                             s0->ext_host_port, ip4_header_t,
+                                             length);
+                      tcp0->dst_port = s0->ext_host_port;
+                      ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
+                    }
+                  tcp0->checksum = ip_csum_fold(sum0);
+                }
+              else
+                {
+                  old_port0 = udp0->src_port;
+                  udp0->src_port = s0->out2in.port;
+                  udp0->checksum = 0;
+                  if (PREDICT_FALSE (is_twice_nat_session (s0)))
+                    {
+                      udp0->dst_port = s0->ext_host_port;
+                      ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
+                    }
+                }
+            }
+
+          /* Hairpinning */
+          if (PREDICT_TRUE(proto0 != SNAT_PROTOCOL_ICMP))
+            nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
+                                     s0->ext_host_port, proto0, 1);
+          else
+            snat_icmp_hairpinning(sm, b0, ip0, icmp0, 1);
+
+          /* Accounting */
+          nat44_session_update_counters (s0, now,
+                                         vlib_buffer_length_in_chain (vm, b0));
+          /* Per-user LRU list maintenance */
+          nat44_session_update_lru (sm, s0, thread_index);
+
+        trace0:
+          if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
+                            && (b0->flags & VLIB_BUFFER_IS_TRACED)))
+            {
+              nat44_in2out_reass_trace_t *t =
+                 vlib_add_trace (vm, node, b0, sizeof (*t));
+              t->cached = cached0;
+              t->sw_if_index = sw_if_index0;
+              t->next_index = next0;
+            }
+
+          if (cached0)
+            {
+              n_left_to_next++;
+              to_next--;
+            }
+          else
+            {
+              pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
+
+              /* verify speculative enqueue, maybe switch current next frame */
+              vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
+                                               to_next, n_left_to_next,
+                                               bi0, next0);
+            }
+
+         if (n_left_from == 0 && vec_len (fragments_to_loopback))
+           {
+             from = vlib_frame_vector_args (frame);
+             u32 len = vec_len (fragments_to_loopback);
+             if (len <= VLIB_FRAME_SIZE)
+               {
+                 clib_memcpy (from, fragments_to_loopback, sizeof (u32) * len);
+                 n_left_from = len;
+                 vec_reset_length (fragments_to_loopback);
+               }
+             else
+               {
+                 clib_memcpy (from,
+                               fragments_to_loopback + (len - VLIB_FRAME_SIZE),
+                               sizeof (u32) * VLIB_FRAME_SIZE);
+                 n_left_from = VLIB_FRAME_SIZE;
+                 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
+               }
+           }
+       }
+
+      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+    }
+
+  vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
+                               SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
+                               pkts_processed);
+
+  nat_send_all_to_node (vm, fragments_to_drop, node,
+                        &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
+                        SNAT_IN2OUT_NEXT_DROP);
+
+  vec_free (fragments_to_drop);
+  vec_free (fragments_to_loopback);
+  return frame->n_vectors;
+}
+
+static uword
+nat44_ed_in2out_reass_node_fn (vlib_main_t * vm,
+                               vlib_node_runtime_t * node,
+                               vlib_frame_t * frame)
+{
+  return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 0);
+}
+
+VLIB_REGISTER_NODE (nat44_ed_in2out_reass_node) = {
+  .function = nat44_ed_in2out_reass_node_fn,
+  .name = "nat44-ed-in2out-reass",
+  .vector_size = sizeof (u32),
+  .format_trace = format_nat44_in2out_reass_trace,
+  .type = VLIB_NODE_TYPE_INTERNAL,
+
+  .n_errors = ARRAY_LEN(snat_in2out_error_strings),
+  .error_strings = snat_in2out_error_strings,
+
+  .n_next_nodes = SNAT_IN2OUT_N_NEXT,
+  .next_nodes = {
+    [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
+    [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
+    [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
+    [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
+    [SNAT_IN2OUT_NEXT_REASS] = "nat44-ed-in2out-reass",
+  },
+};
+
+VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_reass_node,
+                              nat44_ed_in2out_reass_node_fn);
+
+static uword
+nat44_ed_in2out_reass_output_node_fn (vlib_main_t * vm,
+                                      vlib_node_runtime_t * node,
+                                      vlib_frame_t * frame)
+{
+  return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 1);
+}
+
+VLIB_REGISTER_NODE (nat44_ed_in2out_reass_output_node) = {
+  .function = nat44_ed_in2out_reass_output_node_fn,
+  .name = "nat44-ed-in2out-reass-output",
+  .vector_size = sizeof (u32),
+  .format_trace = format_nat44_in2out_reass_trace,
+  .type = VLIB_NODE_TYPE_INTERNAL,
+
+  .n_errors = ARRAY_LEN(snat_in2out_error_strings),
+  .error_strings = snat_in2out_error_strings,
+
+  .n_next_nodes = SNAT_IN2OUT_N_NEXT,
+  .next_nodes = {
+    [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
+    [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
+    [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
+    [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
+    [SNAT_IN2OUT_NEXT_REASS] = "nat44-ed-in2out-reass",
+  },
+};
+
+VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_reass_output_node,
+                              nat44_ed_in2out_reass_output_node_fn);
+
 /**************************/
 /*** deterministic mode ***/
 /**************************/
@@ -3695,7 +4468,7 @@ snat_det_in2out_node_fn (vlib_main_t * vm,
   u32 pkts_processed = 0;
   snat_main_t * sm = &snat_main;
   u32 now = (u32) vlib_time_now (vm);
-  u32 thread_index = vlib_get_thread_index ();
+  u32 thread_index = vm->thread_index;
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
@@ -3864,6 +4637,7 @@ snat_det_in2out_node_fn (vlib_main_t * vm,
               sum0 = ip_csum_update (sum0, old_port0, new_port0,
                                      ip4_header_t /* cheat */,
                                      length /* changed member */);
+              mss_clamping (sm, tcp0, &sum0);
               tcp0->checksum = ip_csum_fold(sum0);
             }
           else
@@ -4014,6 +4788,7 @@ snat_det_in2out_node_fn (vlib_main_t * vm,
               sum1 = ip_csum_update (sum1, old_port1, new_port1,
                                      ip4_header_t /* cheat */,
                                      length /* changed member */);
+              mss_clamping (sm, tcp1, &sum1);
               tcp1->checksum = ip_csum_fold(sum1);
             }
           else
@@ -4200,6 +4975,7 @@ snat_det_in2out_node_fn (vlib_main_t * vm,
               sum0 = ip_csum_update (sum0, old_port0, new_port0,
                                      ip4_header_t /* cheat */,
                                      length /* changed member */);
+              mss_clamping (sm, tcp0, &sum0);
               tcp0->checksum = ip_csum_fold(sum0);
             }
           else
@@ -4460,7 +5236,7 @@ snat_in2out_worker_handoff_fn_inline (vlib_main_t * vm,
   u32 n_left_to_next_worker = 0, *to_next_worker = 0;
   u32 next_worker_index = 0;
   u32 current_worker_index = ~0;
-  u32 thread_index = vlib_get_thread_index ();
+  u32 thread_index = vm->thread_index;
   u32 fq_index;
   u32 to_node_index;
   vlib_frame_t *d = 0;
@@ -4692,7 +5468,7 @@ is_hairpinning (snat_main_t *sm, ip4_address_t * dst_addr)
     }
 
   m_key.addr.as_u32 = dst_addr->as_u32;
-  m_key.fib_index = sm->outside_fib_index;
+  m_key.fib_index = 0;
   m_key.port = 0;
   m_key.protocol = 0;
   kv.key = m_key.as_u64;
@@ -5063,7 +5839,7 @@ snat_in2out_fast_static_map_fn (vlib_main_t * vm,
           key0.port = udp0->src_port;
           key0.fib_index = rx_fib_index0;
 
-          if (snat_static_mapping_match(sm, key0, &sm0, 0, 0, 0, 0))
+          if (snat_static_mapping_match(sm, key0, &sm0, 0, 0, 0, 0, 0))
             {
               b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
               next0= SNAT_IN2OUT_NEXT_DROP;
@@ -5096,6 +5872,7 @@ snat_in2out_fast_static_map_fn (vlib_main_t * vm,
                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
                                          ip4_header_t /* cheat */,
                                          length /* changed member */);
+                  mss_clamping (sm, tcp0, &sum0);
                   tcp0->checksum = ip_csum_fold(sum0);
                 }
               else
@@ -5113,6 +5890,7 @@ snat_in2out_fast_static_map_fn (vlib_main_t * vm,
                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
                                          ip4_header_t,
                                          dst_address /* changed member */);
+                  mss_clamping (sm, tcp0, &sum0);
                   tcp0->checksum = ip_csum_fold(sum0);
                 }
             }