blob: 1a7bde90069b4894eb74bdaa31840803c5a2675d [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
JP Abgrall0031cea2012-04-17 16:38:23 -070017/*
18 * MODUS OPERANDI
19 * --------------
20 *
21 * IPTABLES command sequence:
22 *
23 * iptables -F
24 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070025 * iptables -t raw -F idletimer_PREROUTING
26 * iptables -t mangle -F idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070027 *
28 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070029 * iptables -t raw -N idletimer_PREROUTING
30 * iptables -t mangle -N idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070031 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070032 * iptables -t raw -D PREROUTING -j idletimer_PREROUTING
33 * iptables -t mangle -D POSTROUTING -j idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070034 *
35 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070036 * iptables -t raw -I PREROUTING -j idletimer_PREROUTING
37 * iptables -t mangle -I POSTROUTING -j idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070038 *
39 * # For notifications to work the lable name must match the name of a valid interface.
40 * # If the label name does match an interface, the rules will be a no-op.
41 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070042 * iptables -t raw -A idletimer_PREROUTING -i rmnet0 -j IDLETIMER --timeout 5 --label test-chain --send_nl_msg 1
43 * 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 -070044 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070045 * iptables -nxvL -t raw
46 * iptables -nxvL -t mangle
JP Abgrall0031cea2012-04-17 16:38:23 -070047 *
48 * =================
49 *
50 * ndc command sequence
51 * ------------------
52 * ndc idletimer enable
Haoyu Bai98f65d32012-06-28 16:16:51 -070053 * ndc idletimer add <iface> <timeout> <class label>
54 * ndc idletimer remove <iface> <timeout> <class label>
JP Abgrall0031cea2012-04-17 16:38:23 -070055 *
56 * Monitor effect on the iptables chains after each step using:
Haoyu Bai8c54ec52012-07-26 15:36:53 -070057 * iptables -nxvL -t raw
58 * iptables -nxvL -t mangle
JP Abgrall0031cea2012-04-17 16:38:23 -070059 *
60 * Remember that the timeout value has to be same at the time of the
61 * removal.
62 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070063 * =================
64 *
65 * Verifying the iptables rule
66 * ---------------------------
67 * We want to make sure the iptable rules capture every packet. It can be
68 * verified with tcpdump. First take a note of the pkts count for the two rules:
69 *
70 * adb shell iptables -t mangle -L idletimer_mangle_POSTROUTING -v && adb shell iptables -t raw -L idletimer_raw_PREROUTING -v
71 *
72 * And then, before any network traffics happen on the device, run tcpdump:
73 *
74 * adb shell tcpdump | tee tcpdump.log
75 *
76 * After a while run iptables commands again, you could then count the number
77 * of incoming and outgoing packets captured by tcpdump, and compare that with
78 * the numbers reported by iptables command. There shouldn't be too much
79 * difference on these numbers, i.e., with 2000 packets captured it should
80 * differ by less than 5.
81 *
82 * =================
83 *
JP Abgrall0031cea2012-04-17 16:38:23 -070084 * Note that currently if the name of the iface is incorrect, iptables
85 * will setup rules without checking if it is the name of a valid
86 * interface (although no notifications will ever be received). It is
87 * the responsibility of code in Java land to ensure that the interface name
88 * is correct. The benefit of this, is that idletimers can be setup on
89 * interfaces than come and go.
90 *
91 * A remove should be called for each add command issued during cleanup, as duplicate
92 * entries of the rule may exist and will all have to removed.
93 *
94 */
95
Ashish Sharma1c2c27f2014-02-03 17:28:04 -080096#define LOG_NDEBUG 0
97
JP Abgrall0031cea2012-04-17 16:38:23 -070098#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) {
Ashish Sharma1c2c27f2014-02-03 17:28:04 -0800126 int resIpv4, resIpv6;
JP Abgrall0031cea2012-04-17 16:38:23 -0700127
Ashish Sharma1c2c27f2014-02-03 17:28:04 -0800128 // Running for IPv4
129 argv[0] = IPTABLES_PATH;
130 resIpv4 = android_fork_execvp(argc, (char **)argv, NULL, false, false);
131
132 // Running for IPv6
133 argv[0] = IP6TABLES_PATH;
134 resIpv6 = android_fork_execvp(argc, (char **)argv, NULL, false, false);
135
136#if !LOG_NDEBUG
137 std::string full_cmd = argv[0];
138 argc--; argv++;
139 for (; argc; argc--, argv++) {
140 full_cmd += " ";
141 full_cmd += argv[0];
142 }
143 ALOGV("runCmd(%s) res_ipv4=%d, res_ipv6=%d", full_cmd.c_str(), resIpv4, resIpv6);
144#endif
145
146 return (resIpv4 == 0 && resIpv6 == 0) ? 0 : -1;
JP Abgrall0031cea2012-04-17 16:38:23 -0700147}
148
149bool IdletimerController::setupIptablesHooks() {
JP Abgrall0031cea2012-04-17 16:38:23 -0700150 return true;
151}
152
153int IdletimerController::setDefaults() {
Haoyu Bai8c54ec52012-07-26 15:36:53 -0700154 int res;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800155 const char *cmd1[] = {
Ashish Sharma1c2c27f2014-02-03 17:28:04 -0800156 NULL, // To be filled inside runIpxtablesCmd
Paul Jensen94b2ab92015-08-04 10:35:05 -0400157 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530158 "-W",
159 IPTABLES_RETRY_INTERVAL,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800160 "-t",
161 "raw",
162 "-F",
163 LOCAL_RAW_PREROUTING
164 };
165 res = runIpxtablesCmd(ARRAY_SIZE(cmd1), cmd1);
Haoyu Bai8c54ec52012-07-26 15:36:53 -0700166
167 if (res)
168 return res;
169
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800170 const char *cmd2[] = {
Ashish Sharma1c2c27f2014-02-03 17:28:04 -0800171 NULL, // To be filled inside runIpxtablesCmd
Paul Jensen94b2ab92015-08-04 10:35:05 -0400172 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530173 "-W",
174 IPTABLES_RETRY_INTERVAL,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800175 "-t",
176 "mangle",
177 "-F",
178 LOCAL_MANGLE_POSTROUTING
179 };
180 res = runIpxtablesCmd(ARRAY_SIZE(cmd2), cmd2);
181
Haoyu Bai8c54ec52012-07-26 15:36:53 -0700182 return res;
JP Abgrall0031cea2012-04-17 16:38:23 -0700183}
184
185int IdletimerController::enableIdletimerControl() {
186 int res = setDefaults();
187 return res;
188}
189
190int IdletimerController::disableIdletimerControl() {
191 int res = setDefaults();
192 return res;
193}
194
195int IdletimerController::modifyInterfaceIdletimer(IptOp op, const char *iface,
Haoyu Bai98f65d32012-06-28 16:16:51 -0700196 uint32_t timeout,
197 const char *classLabel) {
JP Abgrall0031cea2012-04-17 16:38:23 -0700198 int res;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800199 char timeout_str[11]; //enough to store any 32-bit unsigned decimal
200
JP Abgrall69261cb2014-06-19 18:35:24 -0700201 if (!isIfaceName(iface)) {
202 errno = ENOENT;
203 return -1;
204 }
205
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800206 snprintf(timeout_str, sizeof(timeout_str), "%u", timeout);
207
208 const char *cmd1[] = {
Ashish Sharma1c2c27f2014-02-03 17:28:04 -0800209 NULL, // To be filled inside runIpxtablesCmd
Paul Jensen94b2ab92015-08-04 10:35:05 -0400210 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530211 "-W",
212 IPTABLES_RETRY_INTERVAL,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800213 "-t",
214 "raw",
215 (op == IptOpAdd) ? "-A" : "-D",
216 LOCAL_RAW_PREROUTING,
217 "-i",
218 iface,
219 "-j",
220 "IDLETIMER",
221 "--timeout",
222 timeout_str,
223 "--label",
224 classLabel,
225 "--send_nl_msg",
226 "1"
227 };
228 res = runIpxtablesCmd(ARRAY_SIZE(cmd1), cmd1);
JP Abgrall0031cea2012-04-17 16:38:23 -0700229
Haoyu Bai8c54ec52012-07-26 15:36:53 -0700230 if (res)
231 return res;
232
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800233 const char *cmd2[] = {
Ashish Sharma1c2c27f2014-02-03 17:28:04 -0800234 NULL, // To be filled inside runIpxtablesCmd
Paul Jensen94b2ab92015-08-04 10:35:05 -0400235 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530236 "-W",
237 IPTABLES_RETRY_INTERVAL,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800238 "-t",
239 "mangle",
240 (op == IptOpAdd) ? "-A" : "-D",
241 LOCAL_MANGLE_POSTROUTING,
242 "-o",
243 iface,
244 "-j",
245 "IDLETIMER",
246 "--timeout",
247 timeout_str,
248 "--label",
249 classLabel,
250 "--send_nl_msg",
251 "1"
252 };
253 res = runIpxtablesCmd(ARRAY_SIZE(cmd2), cmd2);
JP Abgrall0031cea2012-04-17 16:38:23 -0700254
255 return res;
256}
257
Haoyu Bai98f65d32012-06-28 16:16:51 -0700258int IdletimerController::addInterfaceIdletimer(const char *iface,
259 uint32_t timeout,
260 const char *classLabel) {
261 return modifyInterfaceIdletimer(IptOpAdd, iface, timeout, classLabel);
JP Abgrall0031cea2012-04-17 16:38:23 -0700262}
263
Haoyu Bai98f65d32012-06-28 16:16:51 -0700264int IdletimerController::removeInterfaceIdletimer(const char *iface,
265 uint32_t timeout,
266 const char *classLabel) {
267 return modifyInterfaceIdletimer(IptOpDelete, iface, timeout, classLabel);
JP Abgrall0031cea2012-04-17 16:38:23 -0700268}