nat: NAT44-ED api fix and improvement
[vpp.git] / src / plugins / nat / nat44-ed / nat44_ed_api.c
index d37c285..1505975 100644 (file)
@@ -840,10 +840,12 @@ send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
   rmp->vrf_id = htonl (m->vrf_id);
   rmp->context = context;
 
-  if (m->twice_nat)
-    rmp->flags |= NAT_API_IS_TWICE_NAT;
+  if (is_sm_twice_nat (m->flags))
+    {
+      rmp->flags |= NAT_API_IS_TWICE_NAT;
+    }
 
-  if (m->addr_only)
+  if (is_sm_addr_only (m->flags))
     {
       rmp->flags |= NAT_API_IS_ADDR_ONLY;
     }
@@ -853,6 +855,7 @@ send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
       rmp->external_port = m->e_port;
       rmp->local_port = m->l_port;
     }
+
   if (m->tag)
     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
 
@@ -882,7 +885,7 @@ vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
   for (j = 0; j < vec_len (sm->to_resolve); j++)
     {
       rp = sm->to_resolve + j;
-      if (!rp->identity_nat)
+      if (!is_sm_identity_nat (rp->flags))
        send_nat44_static_map_resolve_details (rp, reg, mp->context);
     }
 }
@@ -985,7 +988,7 @@ send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
   rmp->_vl_msg_id =
     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
 
-  if (m->addr_only)
+  if (is_sm_addr_only (m->flags))
     rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
 
   rmp->port = m->l_port;
@@ -1027,7 +1030,7 @@ static void
   for (j = 0; j < vec_len (sm->to_resolve); j++)
     {
       rp = sm->to_resolve + j;
-      if (rp->identity_nat)
+      if (is_sm_identity_nat (rp->flags))
        send_nat44_identity_map_resolve_details (rp, reg, mp->context);
     }
 }
@@ -1219,6 +1222,7 @@ send_nat44_lb_static_mapping_details (snat_static_mapping_t *m,
 
   rmp = vl_msg_api_alloc (
     sizeof (*rmp) + (pool_elts (m->locals) * sizeof (nat44_lb_addr_port_t)));
+
   clib_memset (rmp, 0, sizeof (*rmp));
   rmp->_vl_msg_id =
     ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
@@ -1742,7 +1746,7 @@ send_nat44_user_session_details (snat_session_t * s,
   rmp->context = context;
   rmp->outside_port = s->out2in.port;
   rmp->inside_port = s->in2out.port;
-  rmp->protocol = ntohs (s->proto);
+  rmp->protocol = clib_host_to_net_u16 (s->proto);
   clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
   rmp->ext_host_port = s->ext_host_port;
   if (nat44_ed_is_twice_nat_session (s))
@@ -1815,18 +1819,9 @@ send_nat44_user_session_v2_details (snat_session_t *s,
   rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
   rmp->total_pkts = ntohl (s->total_pkts);
   rmp->context = context;
-  if (snat_is_unk_proto_session (s))
-    {
-      rmp->outside_port = 0;
-      rmp->inside_port = 0;
-      rmp->protocol = ntohs (s->in2out.port);
-    }
-  else
-    {
-      rmp->outside_port = s->out2in.port;
-      rmp->inside_port = s->in2out.port;
-      rmp->protocol = ntohs (nat_proto_to_ip_proto (s->nat_proto));
-    }
+  rmp->outside_port = s->out2in.port;
+  rmp->inside_port = s->in2out.port;
+  rmp->protocol = clib_host_to_net_u16 (s->proto);
   clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
   rmp->ext_host_port = s->ext_host_port;
   if (nat44_ed_is_twice_nat_session (s))
@@ -1835,8 +1830,26 @@ send_nat44_user_session_v2_details (snat_session_t *s,
       rmp->ext_host_nat_port = s->ext_host_nat_port;
     }
 
-  sess_timeout_time = s->last_heard + (f64) nat_session_get_timeout (
-                                       &sm->timeouts, s->nat_proto, s->state);
+  sess_timeout_time = s->last_heard;
+  switch (s->proto)
+    {
+    case IP_PROTOCOL_TCP:
+      if (s->state)
+       sess_timeout_time += sm->timeouts.tcp.established;
+      else
+       sess_timeout_time += sm->timeouts.tcp.transitory;
+      break;
+    case IP_PROTOCOL_UDP:
+      sess_timeout_time += sm->timeouts.udp;
+      break;
+    case IP_PROTOCOL_ICMP:
+      sess_timeout_time += sm->timeouts.icmp;
+      break;
+    default:
+      sess_timeout_time += sm->timeouts.udp;
+      break;
+    }
+
   rmp->is_timed_out = (now >= sess_timeout_time);
 
   vl_api_send_msg (reg, (u8 *) rmp);