From 2ca0c68ea5292992a69b62bf9424caef00e026ad Mon Sep 17 00:00:00 2001
From: llccd <lu_5030683@outlook.com>
Date: Sat, 4 Apr 2020 15:13:37 +0800
Subject: [PATCH] Reorganize code

---
 xt_FULLCONENAT.c | 87 +++++++++++++++++++++++++-----------------------
 1 file changed, 45 insertions(+), 42 deletions(-)

diff --git a/xt_FULLCONENAT.c b/xt_FULLCONENAT.c
index 6bbd005..3a3d64f 100644
--- a/xt_FULLCONENAT.c
+++ b/xt_FULLCONENAT.c
@@ -740,48 +740,7 @@ static void handle_dying_tuples(void) {
     /* we dont know the conntrack direction for now so we try in both ways. */
     ct_tuple = &(item->tuple_original);
 #if IS_ENABLED(CONFIG_IPV6)
-    if (ct_tuple->src.l3num == PF_INET) {
-#endif
-      ip = (ct_tuple->src).u3.ip;
-      port = be16_to_cpu((ct_tuple->src).u.udp.port);
-      mapping = get_mapping_by_int_src(ip, port);
-      if (mapping == NULL) {
-        ct_tuple = &(item->tuple_reply);
-        ip = (ct_tuple->src).u3.ip;
-        port = be16_to_cpu((ct_tuple->src).u.udp.port);
-        mapping = get_mapping_by_int_src(ip, port);
-        if (mapping != NULL) {
-          pr_debug("xt_FULLCONENAT: handle_dying_tuples(): INBOUND dying conntrack at ext port %d\n", mapping->port);
-        }
-      } else {
-        pr_debug("xt_FULLCONENAT: handle_dying_tuples(): OUTBOUND dying conntrack at ext port %d\n", mapping->port);
-      }
-
-      if (mapping == NULL) {
-        goto next;
-      }
-
-      /* look for the corresponding out-dated tuple and free it */
-      list_for_each_safe(iter_2, tmp_2, &mapping->original_tuple_list) {
-        original_tuple_item = list_entry(iter_2, struct nat_mapping_original_tuple, node);
-
-        if (nf_ct_tuple_equal(&original_tuple_item->tuple, &(item->tuple_original))) {
-          pr_debug("xt_FULLCONENAT: handle_dying_tuples(): tuple %s expired. free this tuple.\n",
-            nf_ct_stringify_tuple(&original_tuple_item->tuple));
-          list_del(&original_tuple_item->node);
-          kfree(original_tuple_item);
-          (mapping->refer_count)--;
-        }
-      }
-
-      /* then kill the mapping if needed*/
-      pr_debug("xt_FULLCONENAT: handle_dying_tuples(): refer_count for mapping at ext_port %d is now %d\n", mapping->port, mapping->refer_count);
-      if (mapping->refer_count <= 0) {
-        pr_debug("xt_FULLCONENAT: handle_dying_tuples(): kill expired mapping at ext port %d\n", mapping->port);
-        kill_mapping(mapping);
-      }
-#if IS_ENABLED(CONFIG_IPV6)
-    } else if (likely(ct_tuple->src.l3num == PF_INET6)) {
+    if (ct_tuple->src.l3num == PF_INET6) {
       ip6 = &(ct_tuple->src).u3;
       port = be16_to_cpu((ct_tuple->src).u.udp.port);
       ext_ip6 = &item->tuple_reply.dst.u3;
@@ -822,8 +781,52 @@ static void handle_dying_tuples(void) {
         pr_debug("xt_FULLCONENAT: handle_dying_tuples(): kill expired mapping at ext port %d\n", mapping6->port);
         kill_mapping6(mapping6);
       }
+      goto next;
     }
+    if (unlikely(ct_tuple->src.l3num != PF_INET))
+#else
+    if (ct_tuple->src.l3num != PF_INET)
 #endif
+      goto next;
+
+    ip = (ct_tuple->src).u3.ip;
+    port = be16_to_cpu((ct_tuple->src).u.udp.port);
+    mapping = get_mapping_by_int_src(ip, port);
+    if (mapping == NULL) {
+      ct_tuple = &(item->tuple_reply);
+      ip = (ct_tuple->src).u3.ip;
+      port = be16_to_cpu((ct_tuple->src).u.udp.port);
+      mapping = get_mapping_by_int_src(ip, port);
+      if (mapping != NULL) {
+        pr_debug("xt_FULLCONENAT: handle_dying_tuples(): INBOUND dying conntrack at ext port %d\n", mapping->port);
+      }
+    } else {
+      pr_debug("xt_FULLCONENAT: handle_dying_tuples(): OUTBOUND dying conntrack at ext port %d\n", mapping->port);
+    }
+
+    if (mapping == NULL) {
+      goto next;
+    }
+
+    /* look for the corresponding out-dated tuple and free it */
+    list_for_each_safe(iter_2, tmp_2, &mapping->original_tuple_list) {
+      original_tuple_item = list_entry(iter_2, struct nat_mapping_original_tuple, node);
+
+      if (nf_ct_tuple_equal(&original_tuple_item->tuple, &(item->tuple_original))) {
+        pr_debug("xt_FULLCONENAT: handle_dying_tuples(): tuple %s expired. free this tuple.\n",
+          nf_ct_stringify_tuple(&original_tuple_item->tuple));
+        list_del(&original_tuple_item->node);
+        kfree(original_tuple_item);
+        (mapping->refer_count)--;
+      }
+    }
+
+    /* then kill the mapping if needed*/
+    pr_debug("xt_FULLCONENAT: handle_dying_tuples(): refer_count for mapping at ext_port %d is now %d\n", mapping->port, mapping->refer_count);
+    if (mapping->refer_count <= 0) {
+      pr_debug("xt_FULLCONENAT: handle_dying_tuples(): kill expired mapping at ext port %d\n", mapping->port);
+      kill_mapping(mapping);
+    }
 
 next:
     list_del(&item->list);