nat: session cleanup fix
[vpp.git] / src / plugins / nat / in2out.c
index 8bdb792..6cb111c 100755 (executable)
 #include <vnet/ip/ip.h>
 #include <vnet/ethernet/ethernet.h>
 #include <vnet/fib/ip4_fib.h>
+#include <vnet/udp/udp.h>
 #include <nat/nat.h>
 #include <nat/nat_ipfix_logging.h>
 #include <nat/nat_reass.h>
 #include <nat/nat_inlines.h>
+#include <nat/nat44_inlines.h>
 #include <nat/nat_syslog.h>
 #include <nat/nat_ha.h>
 
@@ -210,7 +212,7 @@ nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
     {
       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");
+       nat_elog_warn ("out2in key del failed");
 
       snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
                                           s->in2out.addr.as_u32,
@@ -267,11 +269,14 @@ slow_path (snat_main_t * sm, vlib_buffer_t * b0,
   };
   nat44_is_idle_session_ctx_t ctx0;
 
+  nat44_session_try_cleanup (&ip0->src_address, rx_fib_index0, thread_index,
+                            now);
+
   if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
     {
       b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
       nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
-      nat_log_notice ("maximum sessions exceeded");
+      nat_elog_notice ("maximum sessions exceeded");
       return SNAT_IN2OUT_NEXT_DROP;
     }
 
@@ -307,7 +312,7 @@ slow_path (snat_main_t * sm, vlib_buffer_t * b0,
                              thread_index);
   if (!u)
     {
-      nat_log_warn ("create NAT user failed");
+      nat_elog_warn ("create NAT user failed");
       return SNAT_IN2OUT_NEXT_DROP;
     }
 
@@ -315,7 +320,7 @@ slow_path (snat_main_t * sm, vlib_buffer_t * b0,
   if (!s)
     {
       nat44_delete_user_with_no_session (sm, u, thread_index);
-      nat_log_warn ("create NAT session failed");
+      nat_elog_warn ("create NAT session failed");
       return SNAT_IN2OUT_NEXT_DROP;
     }
 
@@ -363,7 +368,7 @@ slow_path (snat_main_t * sm, vlib_buffer_t * b0,
   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");
+    nat_elog_notice ("in2out key add failed");
 
   kv0.key = s->out2in.as_u64;
   kv0.value = s - sm->per_thread_data[thread_index].sessions;
@@ -371,7 +376,7 @@ slow_path (snat_main_t * sm, vlib_buffer_t * b0,
   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");
+    nat_elog_notice ("out2in key add failed");
 
   /* log NAT event */
   snat_ipfix_logging_nat44_ses_create (thread_index,
@@ -1032,6 +1037,18 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
                                                                           thread_index,
                                                                           sw_if_index0)))
                        goto trace00;
+
+                     /*
+                      * Send DHCP packets to the ipv4 stack, or we won't
+                      * be able to use dhcp client on the outside interface
+                      */
+                     if (PREDICT_FALSE
+                         ((b0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)
+                          && proto0 == SNAT_PROTOCOL_UDP
+                          && (udp0->dst_port ==
+                              clib_host_to_net_u16
+                              (UDP_DST_PORT_dhcp_to_server))))
+                       goto trace00;
                    }
                  else
                    {
@@ -1075,12 +1092,11 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
                                 src_address /* changed member */ );
          ip0->checksum = ip_csum_fold (sum0);
 
+         old_port0 = udp0->src_port;
+         new_port0 = udp0->src_port = s0->out2in.port;
+
          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,
@@ -1094,9 +1110,17 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
            }
          else
            {
-             old_port0 = udp0->src_port;
-             udp0->src_port = s0->out2in.port;
-             udp0->checksum = 0;
+             if (PREDICT_FALSE (udp0->checksum))
+               {
+                 sum0 = udp0->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 */ );
+                 udp0->checksum = ip_csum_fold (sum0);
+               }
              udp_packets++;
            }
 
@@ -1221,6 +1245,18 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
                                                                           thread_index,
                                                                           sw_if_index1)))
                        goto trace01;
+
+                     /*
+                      * Send DHCP packets to the ipv4 stack, or we won't
+                      * be able to use dhcp client on the outside interface
+                      */
+                     if (PREDICT_FALSE
+                         ((b1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)
+                          && proto1 == SNAT_PROTOCOL_UDP
+                          && (udp1->dst_port ==
+                              clib_host_to_net_u16
+                              (UDP_DST_PORT_dhcp_to_server))))
+                       goto trace01;
                    }
                  else
                    {
@@ -1264,12 +1300,11 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
                                 src_address /* changed member */ );
          ip1->checksum = ip_csum_fold (sum1);
 
+         old_port1 = udp1->src_port;
+         new_port1 = udp1->src_port = s1->out2in.port;
+
          if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
            {
-             old_port1 = tcp1->src_port;
-             tcp1->src_port = s1->out2in.port;
-             new_port1 = tcp1->src_port;
-
              sum1 = tcp1->checksum;
              sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
                                     ip4_header_t,
@@ -1283,9 +1318,17 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
            }
          else
            {
-             old_port1 = udp1->src_port;
-             udp1->src_port = s1->out2in.port;
-             udp1->checksum = 0;
+             if (PREDICT_FALSE (udp1->checksum))
+               {
+                 sum1 = udp1->checksum;
+                 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
+                                        ip4_header_t,
+                                        dst_address /* changed member */ );
+                 sum1 = ip_csum_update (sum1, old_port1, new_port1,
+                                        ip4_header_t /* cheat */ ,
+                                        length /* changed member */ );
+                 udp1->checksum = ip_csum_fold (sum1);
+               }
              udp_packets++;
            }
 
@@ -1444,6 +1487,18 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
                                                                           thread_index,
                                                                           sw_if_index0)))
                        goto trace0;
+
+                     /*
+                      * Send DHCP packets to the ipv4 stack, or we won't
+                      * be able to use dhcp client on the outside interface
+                      */
+                     if (PREDICT_FALSE
+                         ((b0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)
+                          && proto0 == SNAT_PROTOCOL_UDP
+                          && (udp0->dst_port ==
+                              clib_host_to_net_u16
+                              (UDP_DST_PORT_dhcp_to_server))))
+                       goto trace0;
                    }
                  else
                    {
@@ -1488,12 +1543,11 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
                                 src_address /* changed member */ );
          ip0->checksum = ip_csum_fold (sum0);
 
+         old_port0 = udp0->src_port;
+         new_port0 = udp0->src_port = s0->out2in.port;
+
          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,
@@ -1507,9 +1561,17 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
            }
          else
            {
-             old_port0 = udp0->src_port;
-             udp0->src_port = s0->out2in.port;
-             udp0->checksum = 0;
+             if (PREDICT_FALSE (udp0->checksum))
+               {
+                 sum0 = udp0->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 */ );
+                 udp0->checksum = ip_csum_fold (sum0);
+               }
              udp_packets++;
            }
 
@@ -1552,7 +1614,7 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
   vlib_node_increment_counter (vm, stats_node_index,
                               SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
   vlib_node_increment_counter (vm, stats_node_index,
-                              SNAT_IN2OUT_ERROR_UDP_PACKETS, tcp_packets);
+                              SNAT_IN2OUT_ERROR_UDP_PACKETS, udp_packets);
   vlib_node_increment_counter (vm, stats_node_index,
                               SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
   vlib_node_increment_counter (vm, stats_node_index,
@@ -1777,7 +1839,7 @@ VLIB_NODE_FN (nat44_in2out_reass_node) (vlib_main_t * vm,
            {
              next0 = SNAT_IN2OUT_NEXT_DROP;
              b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
-             nat_log_notice ("maximum reassemblies exceeded");
+             nat_elog_notice ("maximum reassemblies exceeded");
              goto trace0;
            }
 
@@ -1844,7 +1906,7 @@ VLIB_NODE_FN (nat44_in2out_reass_node) (vlib_main_t * vm,
                      (thread_index, reass0, bi0, &fragments_to_drop))
                    {
                      b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
-                     nat_log_notice
+                     nat_elog_notice
                        ("maximum fragments per reassembly exceeded");
                      next0 = SNAT_IN2OUT_NEXT_DROP;
                      goto trace0;
@@ -1869,12 +1931,11 @@ VLIB_NODE_FN (nat44_in2out_reass_node) (vlib_main_t * vm,
 
          if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
            {
+             old_port0 = udp0->src_port;
+             new_port0 = udp0->src_port = s0->out2in.port;
+
              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,
@@ -1884,11 +1945,16 @@ VLIB_NODE_FN (nat44_in2out_reass_node) (vlib_main_t * vm,
                                         length /* changed member */ );
                  tcp0->checksum = ip_csum_fold (sum0);
                }
-             else
+             else if (PREDICT_FALSE (udp0->checksum))
                {
-                 old_port0 = udp0->src_port;
-                 udp0->src_port = s0->out2in.port;
-                 udp0->checksum = 0;
+                 sum0 = udp0->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 */ );
+                 udp0->checksum = ip_csum_fold (sum0);
                }
            }
 
@@ -2099,11 +2165,11 @@ VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
 
          if (PREDICT_FALSE (new_port0 != udp0->dst_port))
            {
+             old_port0 = udp0->src_port;
+             udp0->src_port = new_port0;
+
              if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
                {
-                 old_port0 = tcp0->src_port;
-                 tcp0->src_port = new_port0;
-
                  sum0 = tcp0->checksum;
                  sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
                                         ip4_header_t,
@@ -2114,11 +2180,16 @@ VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
                  mss_clamping (sm, tcp0, &sum0);
                  tcp0->checksum = ip_csum_fold (sum0);
                }
-             else
+             else if (udp0->checksum)
                {
-                 old_port0 = udp0->src_port;
-                 udp0->src_port = new_port0;
-                 udp0->checksum = 0;
+                 sum0 = udp0->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 */ );
+                 udp0->checksum = ip_csum_fold (sum0);
                }
            }
          else
@@ -2132,6 +2203,14 @@ VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
                  mss_clamping (sm, tcp0, &sum0);
                  tcp0->checksum = ip_csum_fold (sum0);
                }
+             else if (udp0->checksum)
+               {
+                 sum0 = udp0->checksum;
+                 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
+                                        ip4_header_t,
+                                        dst_address /* changed member */ );
+                 udp0->checksum = ip_csum_fold (sum0);
+               }
            }
 
          /* Hairpinning */