blob: 54b4edd4283a30e4ef57f68f189c18f1d240e544 [file] [log] [blame]
JP Abgrall0031cea2012-04-17 16:38:23 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// #define LOG_NDEBUG 0
18
19/*
20 * MODUS OPERANDI
21 * --------------
22 *
23 * IPTABLES command sequence:
24 *
25 * iptables -F
26 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070027 * iptables -t raw -F idletimer_PREROUTING
28 * iptables -t mangle -F idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070029 *
30 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070031 * iptables -t raw -N idletimer_PREROUTING
32 * iptables -t mangle -N idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070033 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070034 * iptables -t raw -D PREROUTING -j idletimer_PREROUTING
35 * iptables -t mangle -D POSTROUTING -j idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070036 *
37 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070038 * iptables -t raw -I PREROUTING -j idletimer_PREROUTING
39 * iptables -t mangle -I POSTROUTING -j idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070040 *
41 * # For notifications to work the lable name must match the name of a valid interface.
42 * # If the label name does match an interface, the rules will be a no-op.
43 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070044 * iptables -t raw -A idletimer_PREROUTING -i rmnet0 -j IDLETIMER --timeout 5 --label test-chain --send_nl_msg 1
45 * iptables -t mangle -A idletimer_POSTROUTING -o rmnet0 -j IDLETIMER --timeout 5 --label test-chain --send_nl_msg 1
JP Abgrall0031cea2012-04-17 16:38:23 -070046 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070047 * iptables -nxvL -t raw
48 * iptables -nxvL -t mangle
JP Abgrall0031cea2012-04-17 16:38:23 -070049 *
50 * =================
51 *
52 * ndc command sequence
53 * ------------------
54 * ndc idletimer enable
Haoyu Bai98f65d32012-06-28 16:16:51 -070055 * ndc idletimer add <iface> <timeout> <class label>
56 * ndc idletimer remove <iface> <timeout> <class label>
JP Abgrall0031cea2012-04-17 16:38:23 -070057 *
58 * Monitor effect on the iptables chains after each step using:
Haoyu Bai8c54ec52012-07-26 15:36:53 -070059 * iptables -nxvL -t raw
60 * iptables -nxvL -t mangle
JP Abgrall0031cea2012-04-17 16:38:23 -070061 *
62 * Remember that the timeout value has to be same at the time of the
63 * removal.
64 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070065 * =================
66 *
67 * Verifying the iptables rule
68 * ---------------------------
69 * We want to make sure the iptable rules capture every packet. It can be
70 * verified with tcpdump. First take a note of the pkts count for the two rules:
71 *
72 * adb shell iptables -t mangle -L idletimer_mangle_POSTROUTING -v && adb shell iptables -t raw -L idletimer_raw_PREROUTING -v
73 *
74 * And then, before any network traffics happen on the device, run tcpdump:
75 *
76 * adb shell tcpdump | tee tcpdump.log
77 *
78 * After a while run iptables commands again, you could then count the number
79 * of incoming and outgoing packets captured by tcpdump, and compare that with
80 * the numbers reported by iptables command. There shouldn't be too much
81 * difference on these numbers, i.e., with 2000 packets captured it should
82 * differ by less than 5.
83 *
84 * =================
85 *
JP Abgrall0031cea2012-04-17 16:38:23 -070086 * Note that currently if the name of the iface is incorrect, iptables
87 * will setup rules without checking if it is the name of a valid
88 * interface (although no notifications will ever be received). It is
89 * the responsibility of code in Java land to ensure that the interface name
90 * is correct. The benefit of this, is that idletimers can be setup on
91 * interfaces than come and go.
92 *
93 * A remove should be called for each add command issued during cleanup, as duplicate
94 * entries of the rule may exist and will all have to removed.
95 *
96 */
97
98#include <stdlib.h>
99#include <errno.h>
100#include <sys/socket.h>
101#include <sys/stat.h>
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800102#include <sys/wait.h>
JP Abgrall0031cea2012-04-17 16:38:23 -0700103#include <fcntl.h>
104#include <netinet/in.h>
105#include <arpa/inet.h>
106#include <string.h>
107#include <cutils/properties.h>
108
109#define LOG_TAG "IdletimerController"
110#include <cutils/log.h>
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800111#include <logwrap/logwrap.h>
JP Abgrall0031cea2012-04-17 16:38:23 -0700112
113#include "IdletimerController.h"
114#include "NetdConstants.h"
115
Haoyu Bai8c54ec52012-07-26 15:36:53 -0700116const char* IdletimerController::LOCAL_RAW_PREROUTING = "idletimer_raw_PREROUTING";
117const char* IdletimerController::LOCAL_MANGLE_POSTROUTING = "idletimer_mangle_POSTROUTING";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -0700118
JP Abgrall0031cea2012-04-17 16:38:23 -0700119IdletimerController::IdletimerController() {
120}
121
122IdletimerController::~IdletimerController() {
123}
124/* return 0 or non-zero */
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800125int IdletimerController::runIpxtablesCmd(int argc, const char **argv) {
JP Abgrall0031cea2012-04-17 16:38:23 -0700126 int res;
127
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800128 res = android_fork_execvp(argc, (char **)argv, NULL, false, false);
129 ALOGV("runCmd() res=%d", res);
JP Abgrall0031cea2012-04-17 16:38:23 -0700130 return res;
131}
132
133bool IdletimerController::setupIptablesHooks() {
JP Abgrall0031cea2012-04-17 16:38:23 -0700134 return true;
135}
136
137int IdletimerController::setDefaults() {
Haoyu Bai8c54ec52012-07-26 15:36:53 -0700138 int res;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800139 const char *cmd1[] = {
140 IPTABLES_PATH,
141 "-t",
142 "raw",
143 "-F",
144 LOCAL_RAW_PREROUTING
145 };
146 res = runIpxtablesCmd(ARRAY_SIZE(cmd1), cmd1);
Haoyu Bai8c54ec52012-07-26 15:36:53 -0700147
148 if (res)
149 return res;
150
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800151 const char *cmd2[] = {
152 IPTABLES_PATH,
153 "-t",
154 "mangle",
155 "-F",
156 LOCAL_MANGLE_POSTROUTING
157 };
158 res = runIpxtablesCmd(ARRAY_SIZE(cmd2), cmd2);
159
Haoyu Bai8c54ec52012-07-26 15:36:53 -0700160 return res;
JP Abgrall0031cea2012-04-17 16:38:23 -0700161}
162
163int IdletimerController::enableIdletimerControl() {
164 int res = setDefaults();
165 return res;
166}
167
168int IdletimerController::disableIdletimerControl() {
169 int res = setDefaults();
170 return res;
171}
172
173int IdletimerController::modifyInterfaceIdletimer(IptOp op, const char *iface,
Haoyu Bai98f65d32012-06-28 16:16:51 -0700174 uint32_t timeout,
175 const char *classLabel) {
JP Abgrall0031cea2012-04-17 16:38:23 -0700176 int res;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800177 char timeout_str[11]; //enough to store any 32-bit unsigned decimal
178
179 snprintf(timeout_str, sizeof(timeout_str), "%u", timeout);
180
181 const char *cmd1[] = {
182 IPTABLES_PATH,
183 "-t",
184 "raw",
185 (op == IptOpAdd) ? "-A" : "-D",
186 LOCAL_RAW_PREROUTING,
187 "-i",
188 iface,
189 "-j",
190 "IDLETIMER",
191 "--timeout",
192 timeout_str,
193 "--label",
194 classLabel,
195 "--send_nl_msg",
196 "1"
197 };
198 res = runIpxtablesCmd(ARRAY_SIZE(cmd1), cmd1);
JP Abgrall0031cea2012-04-17 16:38:23 -0700199
Haoyu Bai8c54ec52012-07-26 15:36:53 -0700200 if (res)
201 return res;
202
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800203 const char *cmd2[] = {
204 IPTABLES_PATH,
205 "-t",
206 "mangle",
207 (op == IptOpAdd) ? "-A" : "-D",
208 LOCAL_MANGLE_POSTROUTING,
209 "-o",
210 iface,
211 "-j",
212 "IDLETIMER",
213 "--timeout",
214 timeout_str,
215 "--label",
216 classLabel,
217 "--send_nl_msg",
218 "1"
219 };
220 res = runIpxtablesCmd(ARRAY_SIZE(cmd2), cmd2);
JP Abgrall0031cea2012-04-17 16:38:23 -0700221
222 return res;
223}
224
Haoyu Bai98f65d32012-06-28 16:16:51 -0700225int IdletimerController::addInterfaceIdletimer(const char *iface,
226 uint32_t timeout,
227 const char *classLabel) {
228 return modifyInterfaceIdletimer(IptOpAdd, iface, timeout, classLabel);
JP Abgrall0031cea2012-04-17 16:38:23 -0700229}
230
Haoyu Bai98f65d32012-06-28 16:16:51 -0700231int IdletimerController::removeInterfaceIdletimer(const char *iface,
232 uint32_t timeout,
233 const char *classLabel) {
234 return modifyInterfaceIdletimer(IptOpDelete, iface, timeout, classLabel);
JP Abgrall0031cea2012-04-17 16:38:23 -0700235}