Fix idletimer iptable rules.

Now idletimer should be triggered by every packet.

Change-Id: I8cc7c40ed9eb71ff6e7627ee9482c7e228d80f38
diff --git a/CommandListener.cpp b/CommandListener.cpp
index df44549..969fa65 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -84,22 +84,22 @@
 
 static const char* RAW_PREROUTING[] = {
         BandwidthController::LOCAL_RAW_PREROUTING,
+        IdletimerController::LOCAL_RAW_PREROUTING,
         NULL,
 };
 
 static const char* MANGLE_POSTROUTING[] = {
         BandwidthController::LOCAL_MANGLE_POSTROUTING,
+        IdletimerController::LOCAL_MANGLE_POSTROUTING,
         NULL,
 };
 
 static const char* NAT_PREROUTING[] = {
         OEM_IPTABLES_NAT_PREROUTING,
-        IdletimerController::LOCAL_NAT_PREROUTING,
         NULL,
 };
 
 static const char* NAT_POSTROUTING[] = {
-        IdletimerController::LOCAL_NAT_POSTROUTING,
         NatController::LOCAL_NAT_POSTROUTING,
         NULL,
 };
diff --git a/IdletimerController.cpp b/IdletimerController.cpp
index 8e6a67f..7623f33 100644
--- a/IdletimerController.cpp
+++ b/IdletimerController.cpp
@@ -24,27 +24,28 @@
  *
  * iptables -F
  *
- * iptables -t nat -F idletimer_PREROUTING
- * iptables -t nat -F idletimer_POSTROUTING
+ * iptables -t raw -F idletimer_PREROUTING
+ * iptables -t mangle -F idletimer_POSTROUTING
  *
  *
- * iptables -t nat -N idletimer_PREROUTING
- * iptables -t nat -N idletimer_POSTROUTING
+ * iptables -t raw -N idletimer_PREROUTING
+ * iptables -t mangle -N idletimer_POSTROUTING
  *
- * iptables -t nat -D PREROUTING -j idletimer_PREROUTING
- * iptables -t nat -D POSTROUTING -j idletimer_POSTROUTING
+ * iptables -t raw -D PREROUTING -j idletimer_PREROUTING
+ * iptables -t mangle -D POSTROUTING -j idletimer_POSTROUTING
  *
  *
- * iptables -t nat -I PREROUTING -j idletimer_PREROUTING
- * iptables -t nat -I POSTROUTING -j idletimer_POSTROUTING
+ * iptables -t raw -I PREROUTING -j idletimer_PREROUTING
+ * iptables -t mangle -I POSTROUTING -j idletimer_POSTROUTING
  *
  * # For notifications to work the lable name must match the name of a valid interface.
  * # If the label name does match an interface, the rules will be a no-op.
  *
- * iptables -t nat -A idletimer_PREROUTING -i rmnet0 -j IDLETIMER  --timeout 5 --label test-chain --send_nl_msg 1
- * iptables -t nat -A idletimer_POSTROUTING -o rmnet0 -j IDLETIMER  --timeout 5 --label test-chain --send_nl_msg 1
+ * iptables -t raw -A idletimer_PREROUTING -i rmnet0 -j IDLETIMER  --timeout 5 --label test-chain --send_nl_msg 1
+ * iptables -t mangle -A idletimer_POSTROUTING -o rmnet0 -j IDLETIMER  --timeout 5 --label test-chain --send_nl_msg 1
  *
- * iptables -nxvL -t nat
+ * iptables -nxvL -t raw
+ * iptables -nxvL -t mangle
  *
  * =================
  *
@@ -55,11 +56,33 @@
  * ndc idletimer remove <iface> <timeout> <class label>
  *
  * Monitor effect on the iptables chains after each step using:
- *     iptables -nxvL -t nat
+ *     iptables -nxvL -t raw
+ *     iptables -nxvL -t mangle
  *
  * Remember that the timeout value has to be same at the time of the
  * removal.
  *
+ * =================
+ *
+ * Verifying the iptables rule
+ * ---------------------------
+ * We want to make sure the iptable rules capture every packet. It can be
+ * verified with tcpdump. First take a note of the pkts count for the two rules:
+ *
+ * adb shell iptables -t mangle -L idletimer_mangle_POSTROUTING -v && adb shell iptables -t raw -L idletimer_raw_PREROUTING -v
+ *
+ * And then, before any network traffics happen on the device, run tcpdump:
+ *
+ * adb shell tcpdump | tee tcpdump.log
+ *
+ * After a while run iptables commands again, you could then count the number
+ * of incoming and outgoing packets captured by tcpdump, and compare that with
+ * the numbers reported by iptables command. There shouldn't be too much
+ * difference on these numbers, i.e., with 2000 packets captured it should
+ * differ by less than 5.
+ *
+ * =================
+ *
  * Note that currently if the name of the iface is incorrect, iptables
  * will setup rules without checking if it is the name of a valid
  * interface (although no notifications will ever be received).  It is
@@ -90,8 +113,8 @@
 
 extern "C" int system_nosh(const char *command);
 
-const char* IdletimerController::LOCAL_NAT_PREROUTING = "idletimer_nat_PREROUTING";
-const char* IdletimerController::LOCAL_NAT_POSTROUTING = "idletimer_nat_POSTROUTING";
+const char* IdletimerController::LOCAL_RAW_PREROUTING = "idletimer_raw_PREROUTING";
+const char* IdletimerController::LOCAL_MANGLE_POSTROUTING = "idletimer_mangle_POSTROUTING";
 
 IdletimerController::IdletimerController() {
 }
@@ -122,10 +145,19 @@
 }
 
 int IdletimerController::setDefaults() {
-  if (runIpxtablesCmd("-t nat -F idletimer_nat_PREROUTING")
-      || runIpxtablesCmd("-t nat -F idletimer_nat_POSTROUTING") )
-      return -1;
-  return 0;
+  int res;
+  char *buffer;
+  asprintf(&buffer, "-t raw -F %s", LOCAL_RAW_PREROUTING);
+  res = runIpxtablesCmd(buffer);
+  free(buffer);
+
+  if (res)
+    return res;
+
+  asprintf(&buffer, "-t mangle -F %s", LOCAL_MANGLE_POSTROUTING);
+  res = runIpxtablesCmd(buffer);
+  free(buffer);
+  return res;
 }
 
 int IdletimerController::enableIdletimerControl() {
@@ -143,16 +175,19 @@
                                                   const char *classLabel) {
   int res;
   char *buffer;
-  asprintf(&buffer, "-t nat -%c idletimer_nat_PREROUTING -i %s -j IDLETIMER"
+  asprintf(&buffer, "-t raw -%c %s -i %s -j IDLETIMER"
            " --timeout %u --label %s --send_nl_msg 1",
-           (op == IptOpAdd) ? 'A' : 'D', iface, timeout, classLabel);
+           (op == IptOpAdd) ? 'A' : 'D', LOCAL_RAW_PREROUTING, iface, timeout, classLabel);
   res = runIpxtablesCmd(buffer);
   free(buffer);
 
-  asprintf(&buffer, "-t nat -%c idletimer_nat_POSTROUTING -o %s -j IDLETIMER"
+  if (res)
+    return res;
+
+  asprintf(&buffer, "-t mangle -%c %s -o %s -j IDLETIMER"
            " --timeout %u --label %s --send_nl_msg 1",
-           (op == IptOpAdd) ? 'A' : 'D', iface, timeout, classLabel);
-  res |= runIpxtablesCmd(buffer);
+           (op == IptOpAdd) ? 'A' : 'D', LOCAL_MANGLE_POSTROUTING, iface, timeout, classLabel);
+  res = runIpxtablesCmd(buffer);
   free(buffer);
 
   return res;
diff --git a/IdletimerController.h b/IdletimerController.h
index 2af0d9b..eb2aa35 100644
--- a/IdletimerController.h
+++ b/IdletimerController.h
@@ -30,8 +30,8 @@
                                  const char *classLabel);
     bool setupIptablesHooks();
 
-    static const char* LOCAL_NAT_PREROUTING;
-    static const char* LOCAL_NAT_POSTROUTING;
+    static const char* LOCAL_RAW_PREROUTING;
+    static const char* LOCAL_MANGLE_POSTROUTING;
 
  private:
     enum IptOp { IptOpAdd, IptOpDelete };