blob: debb005f0440670fc93516e29744696f126f19e5 [file] [log] [blame]
JP Abgrall4a5f5ca2011-06-15 18:37:39 -07001/*
2 * Copyright (C) 2011 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 Abgralle4788732013-07-02 20:28:45 -070017// #define LOG_NDEBUG 0
JP Abgralldb7da582011-09-18 12:57:32 -070018
19/*
20 * The CommandListener, FrameworkListener don't allow for
21 * multiple calls in parallel to reach the BandwidthController.
22 * If they ever were to allow it, then netd/ would need some tweaking.
23 */
24
JP Abgrall8a932722011-07-13 19:17:35 -070025#include <errno.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070026#include <fcntl.h>
JP Abgralldb7da582011-09-18 12:57:32 -070027#include <stdio.h>
JP Abgrall8a932722011-07-13 19:17:35 -070028#include <stdlib.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070029#include <string.h>
30
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35
36#include <linux/netlink.h>
37#include <linux/rtnetlink.h>
38#include <linux/pkt_sched.h>
39
40#define LOG_TAG "BandwidthController"
41#include <cutils/log.h>
42#include <cutils/properties.h>
Rom Lemarchand14150212013-01-24 10:01:04 -080043#include <logwrap/logwrap.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070044
JP Abgrall0031cea2012-04-17 16:38:23 -070045#include "NetdConstants.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070046#include "BandwidthController.h"
JP Abgrallbaeccc42013-06-25 09:44:10 -070047#include "NatController.h" /* For LOCAL_TETHER_COUNTERS_CHAIN */
48#include "ResponseCode.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070049
JP Abgralldb7da582011-09-18 12:57:32 -070050/* Alphabetical */
JP Abgrall92009c82013-02-06 18:01:24 -080051#define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %lld --name %s"
JP Abgrallc6c67342011-10-07 16:28:54 -070052const char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070053const char* BandwidthController::LOCAL_INPUT = "bw_INPUT";
54const char* BandwidthController::LOCAL_FORWARD = "bw_FORWARD";
55const char* BandwidthController::LOCAL_OUTPUT = "bw_OUTPUT";
56const char* BandwidthController::LOCAL_RAW_PREROUTING = "bw_raw_PREROUTING";
57const char* BandwidthController::LOCAL_MANGLE_POSTROUTING = "bw_mangle_POSTROUTING";
JP Abgralldb7da582011-09-18 12:57:32 -070058const int BandwidthController::MAX_CMD_ARGS = 32;
59const int BandwidthController::MAX_CMD_LEN = 1024;
60const int BandwidthController::MAX_IFACENAME_LEN = 64;
61const int BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256;
62
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070063/**
64 * Some comments about the rules:
65 * * Ordering
66 * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
JP Abgrall29e8de22012-05-03 12:52:15 -070067 * E.g. "-I bw_INPUT -i rmnet0 --jump costly"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070068 * - quota'd rules in the costly chain should be before penalty_box lookups.
JP Abgralle4788732013-07-02 20:28:45 -070069 * - happy_box rejects everything by default.
JP Abgrall29e8de22012-05-03 12:52:15 -070070 * - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070071 *
72 * * global quota vs per interface quota
73 * - global quota for all costly interfaces uses a single costly chain:
74 * . initial rules
JP Abgrallbfa74662011-06-29 19:23:04 -070075 * iptables -N costly_shared
JP Abgrall29e8de22012-05-03 12:52:15 -070076 * iptables -I bw_INPUT -i iface0 --jump costly_shared
77 * iptables -I bw_OUTPUT -o iface0 --jump costly_shared
JP Abgrallbfa74662011-06-29 19:23:04 -070078 * iptables -I costly_shared -m quota \! --quota 500000 \
79 * --jump REJECT --reject-with icmp-net-prohibited
80 * iptables -A costly_shared --jump penalty_box
JP Abgralle4788732013-07-02 20:28:45 -070081 * If the happy box is enabled,
82 * iptables -A penalty_box --jump happy_box
JP Abgrall8a932722011-07-13 19:17:35 -070083 *
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070084 * . adding a new iface to this, E.g.:
JP Abgrall29e8de22012-05-03 12:52:15 -070085 * iptables -I bw_INPUT -i iface1 --jump costly_shared
86 * iptables -I bw_OUTPUT -o iface1 --jump costly_shared
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070087 *
88 * - quota per interface. This is achieve by having "costly" chains per quota.
89 * E.g. adding a new costly interface iface0 with its own quota:
90 * iptables -N costly_iface0
JP Abgrall29e8de22012-05-03 12:52:15 -070091 * iptables -I bw_INPUT -i iface0 --jump costly_iface0
92 * iptables -I bw_OUTPUT -o iface0 --jump costly_iface0
JP Abgrallbfa74662011-06-29 19:23:04 -070093 * iptables -A costly_iface0 -m quota \! --quota 500000 \
JP Abgralle4788732013-07-02 20:28:45 -070094 * --jump REJECT --reject-with icmp-port-unreachable
JP Abgrallbfa74662011-06-29 19:23:04 -070095 * iptables -A costly_iface0 --jump penalty_box
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070096 *
97 * * penalty_box handling:
98 * - only one penalty_box for all interfaces
JP Abgralle4788732013-07-02 20:28:45 -070099 * E.g Adding an app, it has to preserve the appened happy_box, so "-I":
100 * iptables -I penalty_box -m owner --uid-owner app_3 \
101 * --jump REJECT --reject-with icmp-port-unreachable
102 *
103 * * happy_box handling:
104 * - The happy_box goes at the end of the penalty box.
105 * E.g Adding a happy app,
106 * iptables -I happy_box -m owner --uid-owner app_3 \
107 * --jump RETURN
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700108 */
JP Abgrall0031cea2012-04-17 16:38:23 -0700109const char *BandwidthController::IPT_FLUSH_COMMANDS[] = {
110 /*
111 * Cleanup rules.
112 * Should normally include costly_<iface>, but we rely on the way they are setup
113 * to allow coexistance.
JP Abgrall39f8f242011-06-29 19:21:58 -0700114 */
JP Abgrall0031cea2012-04-17 16:38:23 -0700115 "-F bw_INPUT",
116 "-F bw_OUTPUT",
117 "-F bw_FORWARD",
JP Abgralle4788732013-07-02 20:28:45 -0700118 "-F happy_box",
JP Abgrall0031cea2012-04-17 16:38:23 -0700119 "-F penalty_box",
120 "-F costly_shared",
JP Abgrallf66d6e92012-04-27 00:22:57 -0700121
JP Abgralle4788732013-07-02 20:28:45 -0700122 /* Just a couple that are the most common. */
123 "-F costly_rmnet0",
124 "-F costly_wlan0",
125
JP Abgrallf66d6e92012-04-27 00:22:57 -0700126 "-t raw -F bw_raw_PREROUTING",
127 "-t mangle -F bw_mangle_POSTROUTING",
JP Abgrall0031cea2012-04-17 16:38:23 -0700128};
129
130/* The cleanup commands assume flushing has been done. */
131const char *BandwidthController::IPT_CLEANUP_COMMANDS[] = {
JP Abgralle4788732013-07-02 20:28:45 -0700132 "-X happy_box",
JP Abgrall0031cea2012-04-17 16:38:23 -0700133 "-X penalty_box",
134 "-X costly_shared",
JP Abgralle4788732013-07-02 20:28:45 -0700135
136 /* Just a couple that are the most common. */
137 "-X costly_rmnet0",
138 "-X costly_wlan0",
JP Abgrall0dad7c22011-06-24 11:58:14 -0700139};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700140
JP Abgralldb7da582011-09-18 12:57:32 -0700141const char *BandwidthController::IPT_SETUP_COMMANDS[] = {
JP Abgralle4788732013-07-02 20:28:45 -0700142 "-N happy_box",
JP Abgrall0dad7c22011-06-24 11:58:14 -0700143 "-N penalty_box",
JP Abgralle4788732013-07-02 20:28:45 -0700144 "-N costly_shared",
JP Abgrall0dad7c22011-06-24 11:58:14 -0700145};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700146
JP Abgralldb7da582011-09-18 12:57:32 -0700147const char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = {
JP Abgrall0031cea2012-04-17 16:38:23 -0700148 "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700149
JP Abgrall0031cea2012-04-17 16:38:23 -0700150 "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700151
JP Abgrallbfa74662011-06-29 19:23:04 -0700152 "-A costly_shared --jump penalty_box",
JP Abgrallf66d6e92012-04-27 00:22:57 -0700153
JP Abgrall92009c82013-02-06 18:01:24 -0800154 "-t raw -A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */
155 "-t mangle -A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700156};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700157
158BandwidthController::BandwidthController(void) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700159}
160
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700161int BandwidthController::runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrallad729ac2012-04-24 23:27:44 -0700162 IptFailureLog failureHandling) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700163 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700164
Steve Block3fb42e02011-10-20 11:55:56 +0100165 ALOGV("runIpxtablesCmd(cmd=%s)", cmd);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700166 res |= runIptablesCmd(cmd, jumpHandling, IptIpV4, failureHandling);
167 res |= runIptablesCmd(cmd, jumpHandling, IptIpV6, failureHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700168 return res;
169}
170
JP Abgrall26e0d492011-06-24 19:21:51 -0700171int BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) {
172
173 memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0'
174 strncpy(buffer, src, buffSize);
175 return buffer[buffSize - 1];
176}
177
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700178int BandwidthController::runIptablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrallad729ac2012-04-24 23:27:44 -0700179 IptIpVer iptVer, IptFailureLog failureHandling) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700180 char buffer[MAX_CMD_LEN];
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700181 const char *argv[MAX_CMD_ARGS];
JP Abgrall26e0d492011-06-24 19:21:51 -0700182 int argc = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700183 char *next = buffer;
184 char *tmp;
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700185 int res;
Rom Lemarchand14150212013-01-24 10:01:04 -0800186 int status = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700187
JP Abgrall0dad7c22011-06-24 11:58:14 -0700188 std::string fullCmd = cmd;
JP Abgrall26e0d492011-06-24 19:21:51 -0700189
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700190 switch (jumpHandling) {
191 case IptJumpReject:
JP Abgrall340d5cc2013-06-28 17:06:00 -0700192 /*
193 * Must be carefull what one rejects with, as uper layer protocols will just
194 * keep on hammering the device until the number of retries are done.
195 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
196 */
197 fullCmd += " --jump REJECT";
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700198 break;
199 case IptJumpReturn:
200 fullCmd += " --jump RETURN";
201 break;
202 case IptJumpNoAdd:
203 break;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700204 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700205
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700206 fullCmd.insert(0, " ");
207 fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700208
Rom Lemarchand14150212013-01-24 10:01:04 -0800209 if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
210 ALOGE("iptables command too long");
211 return -1;
212 }
213
214 argc = 0;
215 while ((tmp = strsep(&next, " "))) {
216 argv[argc++] = tmp;
217 if (argc >= MAX_CMD_ARGS) {
218 ALOGE("iptables argument overflow");
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700219 return -1;
220 }
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700221 }
Rom Lemarchand14150212013-01-24 10:01:04 -0800222
223 argv[argc] = NULL;
224 res = android_fork_execvp(argc, (char **)argv, &status, false,
225 failureHandling == IptFailShow);
JP Abgrallc8dc63b2013-02-13 16:30:00 -0800226 res = res || !WIFEXITED(status) || WEXITSTATUS(status);
227 if (res && failureHandling == IptFailShow) {
228 ALOGE("runIptablesCmd(): res=%d status=%d failed %s", res, status,
229 fullCmd.c_str());
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700230 }
231 return res;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700232}
233
JP Abgrall0031cea2012-04-17 16:38:23 -0700234int BandwidthController::setupIptablesHooks(void) {
235
236 /* Some of the initialCommands are allowed to fail */
237 runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
238 IPT_FLUSH_COMMANDS, RunCmdFailureOk);
239
240 runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
241 IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
242
243 runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*),
244 IPT_SETUP_COMMANDS, RunCmdFailureBad);
245
246 return 0;
JP Abgrall0031cea2012-04-17 16:38:23 -0700247}
248
249int BandwidthController::enableBandwidthControl(bool force) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700250 int res;
JP Abgrall0031cea2012-04-17 16:38:23 -0700251 char value[PROPERTY_VALUE_MAX];
252
253 if (!force) {
254 property_get("persist.bandwidth.enable", value, "1");
255 if (!strcmp(value, "0"))
256 return 0;
257 }
JP Abgrall8a932722011-07-13 19:17:35 -0700258
JP Abgralldb7da582011-09-18 12:57:32 -0700259 /* Let's pretend we started from scratch ... */
JP Abgrall8a932722011-07-13 19:17:35 -0700260 sharedQuotaIfaces.clear();
261 quotaIfaces.clear();
262 naughtyAppUids.clear();
JP Abgralle4788732013-07-02 20:28:45 -0700263 niceAppUids.clear();
JP Abgralldb7da582011-09-18 12:57:32 -0700264 globalAlertBytes = 0;
JP Abgrallc6c67342011-10-07 16:28:54 -0700265 globalAlertTetherCount = 0;
JP Abgralldb7da582011-09-18 12:57:32 -0700266 sharedQuotaBytes = sharedAlertBytes = 0;
267
JP Abgrall0031cea2012-04-17 16:38:23 -0700268 res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
269 IPT_FLUSH_COMMANDS, RunCmdFailureOk);
JP Abgralldb7da582011-09-18 12:57:32 -0700270
JP Abgrall0031cea2012-04-17 16:38:23 -0700271 res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*),
JP Abgralldb7da582011-09-18 12:57:32 -0700272 IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad);
JP Abgrall8a932722011-07-13 19:17:35 -0700273
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700274 return res;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700275
276}
277
278int BandwidthController::disableBandwidthControl(void) {
JP Abgrall0031cea2012-04-17 16:38:23 -0700279 runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
280 IPT_FLUSH_COMMANDS, RunCmdFailureOk);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700281 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700282}
283
JP Abgrall8a932722011-07-13 19:17:35 -0700284int BandwidthController::runCommands(int numCommands, const char *commands[],
285 RunCmdErrHandling cmdErrHandling) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700286 int res = 0;
JP Abgrallad729ac2012-04-24 23:27:44 -0700287 IptFailureLog failureLogging = IptFailShow;
288 if (cmdErrHandling == RunCmdFailureOk) {
289 failureLogging = IptFailHide;
290 }
Steve Block3fb42e02011-10-20 11:55:56 +0100291 ALOGV("runCommands(): %d commands", numCommands);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700292 for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700293 res = runIpxtablesCmd(commands[cmdNum], IptJumpNoAdd, failureLogging);
JP Abgrall0031cea2012-04-17 16:38:23 -0700294 if (res && cmdErrHandling != RunCmdFailureOk)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700295 return res;
296 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700297 return 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700298}
299
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700300std::string BandwidthController::makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700301 std::string res;
JP Abgrall8a932722011-07-13 19:17:35 -0700302 char *buff;
303 const char *opFlag;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700304
305 switch (op) {
JP Abgrall8a932722011-07-13 19:17:35 -0700306 case IptOpInsert:
307 opFlag = "-I";
308 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800309 case IptOpAppend:
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700310 ALOGE("Append op not supported for %s uids", chain);
311 res = "";
312 return res;
JP Abgrall109899b2013-02-12 19:20:13 -0800313 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700314 case IptOpReplace:
315 opFlag = "-R";
316 break;
317 default:
318 case IptOpDelete:
319 opFlag = "-D";
320 break;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700321 }
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700322 asprintf(&buff, "%s %s -m owner --uid-owner %d", opFlag, chain, uid);
JP Abgrall8a932722011-07-13 19:17:35 -0700323 res = buff;
324 free(buff);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700325 return res;
326}
327
JP Abgralle4788732013-07-02 20:28:45 -0700328int BandwidthController::enableHappyBox(void) {
329 char cmd[MAX_CMD_LEN];
330 int res = 0;
331
332 /*
333 * We tentatively delete before adding, which helps recovering
334 * from bad states (e.g. netd died).
335 */
336
337 /* Should not exist, but ignore result if already there. */
338 snprintf(cmd, sizeof(cmd), "-N happy_box");
339 runIpxtablesCmd(cmd, IptJumpNoAdd);
340
341 /* Should be empty, but clear in case something was wrong. */
342 niceAppUids.clear();
343 snprintf(cmd, sizeof(cmd), "-F happy_box");
344 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
345
346 snprintf(cmd, sizeof(cmd), "-D penalty_box -j happy_box");
347 runIpxtablesCmd(cmd, IptJumpNoAdd);
348 snprintf(cmd, sizeof(cmd), "-A penalty_box -j happy_box");
349 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
350
351 /* Reject. Defaulting to prot-unreachable */
352 snprintf(cmd, sizeof(cmd), "-D happy_box -j REJECT");
353 runIpxtablesCmd(cmd, IptJumpNoAdd);
354 snprintf(cmd, sizeof(cmd), "-A happy_box -j REJECT");
355 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
356
357 return res;
358}
359
360int BandwidthController::disableHappyBox(void) {
361 char cmd[MAX_CMD_LEN];
362
363 /* Best effort */
364 snprintf(cmd, sizeof(cmd), "-D penalty_box -j happy_box");
365 runIpxtablesCmd(cmd, IptJumpNoAdd);
366 niceAppUids.clear();
367 snprintf(cmd, sizeof(cmd), "-F happy_box");
368 runIpxtablesCmd(cmd, IptJumpNoAdd);
369 snprintf(cmd, sizeof(cmd), "-X happy_box");
370 runIpxtablesCmd(cmd, IptJumpNoAdd);
371
372 return 0;
373}
374
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700375int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700376 return manipulateNaughtyApps(numUids, appUids, SpecialAppOpAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700377}
378
379int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700380 return manipulateNaughtyApps(numUids, appUids, SpecialAppOpRemove);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700381}
382
JP Abgralle4788732013-07-02 20:28:45 -0700383int BandwidthController::addNiceApps(int numUids, char *appUids[]) {
384 return manipulateNiceApps(numUids, appUids, SpecialAppOpAdd);
385}
386
387int BandwidthController::removeNiceApps(int numUids, char *appUids[]) {
388 return manipulateNiceApps(numUids, appUids, SpecialAppOpRemove);
389}
390
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700391int BandwidthController::manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
392 return manipulateSpecialApps(numUids, appStrUids, "penalty_box", naughtyAppUids, IptJumpReject, appOp);
393}
394
JP Abgralle4788732013-07-02 20:28:45 -0700395int BandwidthController::manipulateNiceApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
396 return manipulateSpecialApps(numUids, appStrUids, "happy_box", niceAppUids, IptJumpReturn, appOp);
397}
398
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700399
400int BandwidthController::manipulateSpecialApps(int numUids, char *appStrUids[],
401 const char *chain,
402 std::list<int /*appUid*/> &specialAppUids,
403 IptJumpOp jumpHandling, SpecialAppOp appOp) {
404
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700405 char cmd[MAX_CMD_LEN];
406 int uidNum;
JP Abgrall26e0d492011-06-24 19:21:51 -0700407 const char *failLogTemplate;
408 IptOp op;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700409 int appUids[numUids];
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700410 std::string iptCmd;
JP Abgrallb1d24092012-04-27 01:02:31 -0700411 std::list<int /*uid*/>::iterator it;
JP Abgrall8a932722011-07-13 19:17:35 -0700412
JP Abgrall26e0d492011-06-24 19:21:51 -0700413 switch (appOp) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700414 case SpecialAppOpAdd:
JP Abgrall8a932722011-07-13 19:17:35 -0700415 op = IptOpInsert;
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700416 failLogTemplate = "Failed to add app uid %d to %s.";
JP Abgrall8a932722011-07-13 19:17:35 -0700417 break;
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700418 case SpecialAppOpRemove:
JP Abgrall8a932722011-07-13 19:17:35 -0700419 op = IptOpDelete;
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700420 failLogTemplate = "Failed to delete app uid %d from %s box.";
JP Abgrall8a932722011-07-13 19:17:35 -0700421 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700422 default:
423 ALOGE("Unexpected app Op %d", appOp);
424 return -1;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700425 }
426
427 for (uidNum = 0; uidNum < numUids; uidNum++) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700428 appUids[uidNum] = atol(appStrUids[uidNum]);
429 if (appUids[uidNum] == 0) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700430 ALOGE(failLogTemplate, appUids[uidNum], chain);
JP Abgrall26e0d492011-06-24 19:21:51 -0700431 goto fail_parse;
432 }
433 }
JP Abgrall26e0d492011-06-24 19:21:51 -0700434
435 for (uidNum = 0; uidNum < numUids; uidNum++) {
JP Abgrallb1d24092012-04-27 01:02:31 -0700436 int uid = appUids[uidNum];
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700437 for (it = specialAppUids.begin(); it != specialAppUids.end(); it++) {
JP Abgrallb1d24092012-04-27 01:02:31 -0700438 if (*it == uid)
439 break;
440 }
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700441 bool found = (it != specialAppUids.end());
JP Abgrallb1d24092012-04-27 01:02:31 -0700442
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700443 if (appOp == SpecialAppOpRemove) {
JP Abgrallb1d24092012-04-27 01:02:31 -0700444 if (!found) {
445 ALOGE("No such appUid %d to remove", uid);
446 return -1;
447 }
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700448 specialAppUids.erase(it);
JP Abgrallb1d24092012-04-27 01:02:31 -0700449 } else {
450 if (found) {
451 ALOGE("appUid %d exists already", uid);
452 return -1;
453 }
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700454 specialAppUids.push_front(uid);
JP Abgrallb1d24092012-04-27 01:02:31 -0700455 }
456
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700457 iptCmd = makeIptablesSpecialAppCmd(op, uid, chain);
458 if (runIpxtablesCmd(iptCmd.c_str(), jumpHandling)) {
459 ALOGE(failLogTemplate, uid, chain);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700460 goto fail_with_uidNum;
461 }
462 }
463 return 0;
464
JP Abgrall26e0d492011-06-24 19:21:51 -0700465fail_with_uidNum:
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700466 /* Try to remove the uid that failed in any case*/
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700467 iptCmd = makeIptablesSpecialAppCmd(IptOpDelete, appUids[uidNum], chain);
468 runIpxtablesCmd(iptCmd.c_str(), jumpHandling);
JP Abgrall26e0d492011-06-24 19:21:51 -0700469fail_parse:
470 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700471}
472
JP Abgrall26e0d492011-06-24 19:21:51 -0700473std::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700474 std::string res;
JP Abgrall8a932722011-07-13 19:17:35 -0700475 char *buff;
476 const char *opFlag;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700477
Steve Block3fb42e02011-10-20 11:55:56 +0100478 ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700479
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700480 switch (op) {
JP Abgrall8a932722011-07-13 19:17:35 -0700481 case IptOpInsert:
482 opFlag = "-I";
483 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800484 case IptOpAppend:
485 opFlag = "-A";
486 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700487 case IptOpReplace:
488 opFlag = "-R";
489 break;
490 default:
491 case IptOpDelete:
492 opFlag = "-D";
493 break;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700494 }
JP Abgrall8a932722011-07-13 19:17:35 -0700495
JP Abgrallbfa74662011-06-29 19:23:04 -0700496 // The requried IP version specific --jump REJECT ... will be added later.
JP Abgrall8a932722011-07-13 19:17:35 -0700497 asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota,
498 costName);
499 res = buff;
500 free(buff);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700501 return res;
502}
503
JP Abgrall26e0d492011-06-24 19:21:51 -0700504int BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700505 char cmd[MAX_CMD_LEN];
JP Abgrall0031cea2012-04-17 16:38:23 -0700506 int res = 0, res1, res2;
JP Abgrall8a932722011-07-13 19:17:35 -0700507 int ruleInsertPos = 1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700508 std::string costString;
509 const char *costCString;
510
JP Abgrall0dad7c22011-06-24 11:58:14 -0700511 /* The "-N costly" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700512 switch (quotaType) {
513 case QuotaUnique:
JP Abgrallbfa74662011-06-29 19:23:04 -0700514 costString = "costly_";
JP Abgrall0dad7c22011-06-24 11:58:14 -0700515 costString += ifn;
516 costCString = costString.c_str();
JP Abgrall0031cea2012-04-17 16:38:23 -0700517 /*
518 * Flush the costly_<iface> is allowed to fail in case it didn't exist.
519 * Creating a new one is allowed to fail in case it existed.
520 * This helps with netd restarts.
521 */
522 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700523 res1 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700524 snprintf(cmd, sizeof(cmd), "-N %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700525 res2 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700526 res = (res1 && res2) || (!res1 && !res2);
527
JP Abgrall0dad7c22011-06-24 11:58:14 -0700528 snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700529 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall26e0d492011-06-24 19:21:51 -0700530 break;
531 case QuotaShared:
JP Abgrallbfa74662011-06-29 19:23:04 -0700532 costCString = "costly_shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700533 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700534 default:
535 ALOGE("Unexpected quotatype %d", quotaType);
536 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700537 }
538
JP Abgrall8a932722011-07-13 19:17:35 -0700539 if (globalAlertBytes) {
540 /* The alert rule comes 1st */
541 ruleInsertPos = 2;
542 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700543
544 snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700545 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700546
547 snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700548 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0031cea2012-04-17 16:38:23 -0700549
550 snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700551 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700552
553 snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700554 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700555 return res;
556}
557
JP Abgrall26e0d492011-06-24 19:21:51 -0700558int BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700559 char cmd[MAX_CMD_LEN];
560 int res = 0;
561 std::string costString;
562 const char *costCString;
563
JP Abgrall26e0d492011-06-24 19:21:51 -0700564 switch (quotaType) {
565 case QuotaUnique:
JP Abgrallbfa74662011-06-29 19:23:04 -0700566 costString = "costly_";
JP Abgrall0dad7c22011-06-24 11:58:14 -0700567 costString += ifn;
568 costCString = costString.c_str();
JP Abgrall26e0d492011-06-24 19:21:51 -0700569 break;
570 case QuotaShared:
JP Abgrallbfa74662011-06-29 19:23:04 -0700571 costCString = "costly_shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700572 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700573 default:
574 ALOGE("Unexpected quotatype %d", quotaType);
575 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700576 }
577
JP Abgrall0031cea2012-04-17 16:38:23 -0700578 snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700579 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0031cea2012-04-17 16:38:23 -0700580 snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700581 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700582
JP Abgrallbfa74662011-06-29 19:23:04 -0700583 /* The "-N costly_shared" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700584 if (quotaType == QuotaUnique) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700585 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700586 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgralla9f802c2011-06-29 15:46:45 -0700587 snprintf(cmd, sizeof(cmd), "-X %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700588 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700589 }
590 return res;
591}
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700592
JP Abgrall0dad7c22011-06-24 11:58:14 -0700593int BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700594 char cmd[MAX_CMD_LEN];
595 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700596 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700597 std::string quotaCmd;
JP Abgrall8a932722011-07-13 19:17:35 -0700598 std::string ifaceName;
599 ;
JP Abgrallbfa74662011-06-29 19:23:04 -0700600 const char *costName = "shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700601 std::list<std::string>::iterator it;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700602
JP Abgrall8a932722011-07-13 19:17:35 -0700603 if (!maxBytes) {
604 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000605 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700606 return -1;
607 }
JP Abgrall26e0d492011-06-24 19:21:51 -0700608 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000609 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700610 return -1;
611 }
612 ifaceName = ifn;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700613
614 if (maxBytes == -1) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700615 return removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700616 }
617
618 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700619 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
620 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700621 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700622 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700623
JP Abgrall0dad7c22011-06-24 11:58:14 -0700624 if (it == sharedQuotaIfaces.end()) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700625 res |= prepCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700626 if (sharedQuotaIfaces.empty()) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700627 quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700628 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700629 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000630 ALOGE("Failed set quota rule");
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700631 goto fail;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700632 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700633 sharedQuotaBytes = maxBytes;
634 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700635 sharedQuotaIfaces.push_front(ifaceName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700636
637 }
638
639 if (maxBytes != sharedQuotaBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700640 res |= updateQuota(costName, maxBytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700641 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000642 ALOGE("Failed update quota for %s", costName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700643 goto fail;
644 }
645 sharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700646 }
647 return 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700648
649 fail:
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700650 /*
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700651 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
652 * rules in the kernel to see which ones need cleaning up.
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700653 * For now callers needs to choose if they want to "ndc bandwidth enable"
654 * which resets everything.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700655 */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700656 removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700657 return -1;
658}
659
JP Abgrall8a932722011-07-13 19:17:35 -0700660/* It will also cleanup any shared alerts */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700661int BandwidthController::removeInterfaceSharedQuota(const char *iface) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700662 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700663 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700664 std::string ifaceName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700665 std::list<std::string>::iterator it;
JP Abgrallbfa74662011-06-29 19:23:04 -0700666 const char *costName = "shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700667
JP Abgrall8a932722011-07-13 19:17:35 -0700668 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000669 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700670 return -1;
671 }
JP Abgrall8a932722011-07-13 19:17:35 -0700672 ifaceName = ifn;
JP Abgrall26e0d492011-06-24 19:21:51 -0700673
JP Abgrall0dad7c22011-06-24 11:58:14 -0700674 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
675 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700676 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700677 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700678 if (it == sharedQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000679 ALOGE("No such iface %s to delete", ifn);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700680 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700681 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700682
JP Abgrall26e0d492011-06-24 19:21:51 -0700683 res |= cleanupCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700684 sharedQuotaIfaces.erase(it);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700685
JP Abgrall0dad7c22011-06-24 11:58:14 -0700686 if (sharedQuotaIfaces.empty()) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700687 std::string quotaCmd;
JP Abgrallbfa74662011-06-29 19:23:04 -0700688 quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700689 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall8a932722011-07-13 19:17:35 -0700690 sharedQuotaBytes = 0;
691 if (sharedAlertBytes) {
692 removeSharedAlert();
693 sharedAlertBytes = 0;
694 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700695 }
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700696 return res;
697}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700698
699int BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
700 char ifn[MAX_IFACENAME_LEN];
701 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700702 std::string ifaceName;
703 const char *costName;
704 std::list<QuotaInfo>::iterator it;
705 std::string quotaCmd;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700706
JP Abgrall8a932722011-07-13 19:17:35 -0700707 if (!maxBytes) {
708 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000709 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700710 return -1;
711 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700712 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700713 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700714 }
715
JP Abgrall8a932722011-07-13 19:17:35 -0700716 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000717 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700718 return -1;
719 }
720 ifaceName = ifn;
721 costName = iface;
722
JP Abgrall0dad7c22011-06-24 11:58:14 -0700723 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700724 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
JP Abgrall8a932722011-07-13 19:17:35 -0700725 if (it->ifaceName == ifaceName)
JP Abgrall0dad7c22011-06-24 11:58:14 -0700726 break;
727 }
728
729 if (it == quotaIfaces.end()) {
JP Abgralle4788732013-07-02 20:28:45 -0700730 /* Preparing the iface adds a penalty/happy box check */
JP Abgrall26e0d492011-06-24 19:21:51 -0700731 res |= prepCostlyIface(ifn, QuotaUnique);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700732 /*
JP Abgralle4788732013-07-02 20:28:45 -0700733 * The rejecting quota limit should go after the penalty/happy box checks
JP Abgrallbaeccc42013-06-25 09:44:10 -0700734 * or else a naughty app could just eat up the quota.
735 * So we append here.
736 */
JP Abgrall109899b2013-02-12 19:20:13 -0800737 quotaCmd = makeIptablesQuotaCmd(IptOpAppend, costName, maxBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700738 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700739 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000740 ALOGE("Failed set quota rule");
JP Abgrall0dad7c22011-06-24 11:58:14 -0700741 goto fail;
742 }
743
JP Abgrall8a932722011-07-13 19:17:35 -0700744 quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
JP Abgrall0dad7c22011-06-24 11:58:14 -0700745
746 } else {
JP Abgrall8a932722011-07-13 19:17:35 -0700747 res |= updateQuota(costName, maxBytes);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700748 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000749 ALOGE("Failed update quota for %s", iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700750 goto fail;
751 }
JP Abgrall8a932722011-07-13 19:17:35 -0700752 it->quota = maxBytes;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700753 }
754 return 0;
755
756 fail:
757 /*
758 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
759 * rules in the kernel to see which ones need cleaning up.
760 * For now callers needs to choose if they want to "ndc bandwidth enable"
761 * which resets everything.
762 */
763 removeInterfaceSharedQuota(ifn);
764 return -1;
765}
766
JP Abgrall8a932722011-07-13 19:17:35 -0700767int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
768 return getInterfaceQuota("shared", bytes);
769}
770
771int BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
772 FILE *fp;
773 char *fname;
774 int scanRes;
775
776 asprintf(&fname, "/proc/net/xt_quota/%s", costName);
777 fp = fopen(fname, "r");
778 free(fname);
779 if (!fp) {
Steve Block5ea0c052012-01-06 19:18:11 +0000780 ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
JP Abgrall8a932722011-07-13 19:17:35 -0700781 return -1;
782 }
783 scanRes = fscanf(fp, "%lld", bytes);
Steve Block3fb42e02011-10-20 11:55:56 +0100784 ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700785 fclose(fp);
786 return scanRes == 1 ? 0 : -1;
787}
788
JP Abgrall0dad7c22011-06-24 11:58:14 -0700789int BandwidthController::removeInterfaceQuota(const char *iface) {
790
791 char ifn[MAX_IFACENAME_LEN];
792 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700793 std::string ifaceName;
794 const char *costName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700795 std::list<QuotaInfo>::iterator it;
JP Abgrall26e0d492011-06-24 19:21:51 -0700796
JP Abgrall8a932722011-07-13 19:17:35 -0700797 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000798 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700799 return -1;
800 }
801 ifaceName = ifn;
802 costName = iface;
803
JP Abgrall0dad7c22011-06-24 11:58:14 -0700804 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
JP Abgrall8a932722011-07-13 19:17:35 -0700805 if (it->ifaceName == ifaceName)
JP Abgrall0dad7c22011-06-24 11:58:14 -0700806 break;
807 }
808
809 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000810 ALOGE("No such iface %s to delete", ifn);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700811 return -1;
812 }
813
814 /* This also removes the quota command of CostlyIface chain. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700815 res |= cleanupCostlyIface(ifn, QuotaUnique);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700816
817 quotaIfaces.erase(it);
818
819 return res;
820}
JP Abgrall8a932722011-07-13 19:17:35 -0700821
822int BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
823 FILE *fp;
824 char *fname;
825
826 asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
827 fp = fopen(fname, "w");
828 free(fname);
829 if (!fp) {
Steve Block5ea0c052012-01-06 19:18:11 +0000830 ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
JP Abgrall8a932722011-07-13 19:17:35 -0700831 return -1;
832 }
833 fprintf(fp, "%lld\n", bytes);
834 fclose(fp);
835 return 0;
836}
837
838int BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
839 int res = 0;
840 const char *opFlag;
841 char *alertQuotaCmd;
842
843 switch (op) {
844 case IptOpInsert:
845 opFlag = "-I";
846 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800847 case IptOpAppend:
848 opFlag = "-A";
849 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700850 case IptOpReplace:
851 opFlag = "-R";
852 break;
853 default:
854 case IptOpDelete:
855 opFlag = "-D";
856 break;
857 }
858
JP Abgrall92009c82013-02-06 18:01:24 -0800859 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800860 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700861 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700862 free(alertQuotaCmd);
JP Abgrall92009c82013-02-06 18:01:24 -0800863 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800864 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700865 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700866 free(alertQuotaCmd);
867 return res;
868}
869
JP Abgrallc6c67342011-10-07 16:28:54 -0700870int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
871 int res = 0;
872 const char *opFlag;
JP Abgrall8a932722011-07-13 19:17:35 -0700873 char *alertQuotaCmd;
JP Abgrallc6c67342011-10-07 16:28:54 -0700874
875 switch (op) {
876 case IptOpInsert:
877 opFlag = "-I";
878 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800879 case IptOpAppend:
880 opFlag = "-A";
881 break;
JP Abgrallc6c67342011-10-07 16:28:54 -0700882 case IptOpReplace:
883 opFlag = "-R";
884 break;
885 default:
886 case IptOpDelete:
887 opFlag = "-D";
888 break;
889 }
890
JP Abgrall92009c82013-02-06 18:01:24 -0800891 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800892 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700893 res = runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrallc6c67342011-10-07 16:28:54 -0700894 free(alertQuotaCmd);
895 return res;
896}
897
898int BandwidthController::setGlobalAlert(int64_t bytes) {
899 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700900 int res = 0;
901
902 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000903 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700904 return -1;
905 }
906 if (globalAlertBytes) {
907 res = updateQuota(alertName, bytes);
908 } else {
909 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700910 if (globalAlertTetherCount) {
Steve Block3fb42e02011-10-20 11:55:56 +0100911 ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700912 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
913 }
JP Abgrall8a932722011-07-13 19:17:35 -0700914 }
915 globalAlertBytes = bytes;
916 return res;
917}
918
JP Abgrallc6c67342011-10-07 16:28:54 -0700919int BandwidthController::setGlobalAlertInForwardChain(void) {
920 const char *alertName = ALERT_GLOBAL_NAME;
921 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700922
JP Abgrallc6c67342011-10-07 16:28:54 -0700923 globalAlertTetherCount++;
Steve Block3fb42e02011-10-20 11:55:56 +0100924 ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700925
926 /*
927 * If there is no globalAlert active we are done.
928 * If there is an active globalAlert but this is not the 1st
929 * tether, we are also done.
930 */
931 if (!globalAlertBytes || globalAlertTetherCount != 1) {
932 return 0;
933 }
934
935 /* We only add the rule if this was the 1st tether added. */
936 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
937 return res;
938}
939
940int BandwidthController::removeGlobalAlert(void) {
941
942 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700943 int res = 0;
944
945 if (!globalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000946 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700947 return -1;
948 }
949 res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700950 if (globalAlertTetherCount) {
951 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
952 }
JP Abgrall8a932722011-07-13 19:17:35 -0700953 globalAlertBytes = 0;
954 return res;
955}
956
JP Abgrallc6c67342011-10-07 16:28:54 -0700957int BandwidthController::removeGlobalAlertInForwardChain(void) {
958 int res = 0;
959 const char *alertName = ALERT_GLOBAL_NAME;
960
961 if (!globalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000962 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700963 return -1;
964 }
965
966 globalAlertTetherCount--;
967 /*
968 * If there is no globalAlert active we are done.
969 * If there is an active globalAlert but there are more
970 * tethers, we are also done.
971 */
972 if (!globalAlertBytes || globalAlertTetherCount >= 1) {
973 return 0;
974 }
975
976 /* We only detete the rule if this was the last tether removed. */
977 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
978 return res;
979}
980
JP Abgrall8a932722011-07-13 19:17:35 -0700981int BandwidthController::setSharedAlert(int64_t bytes) {
982 if (!sharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000983 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700984 return -1;
985 }
986 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000987 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700988 return -1;
989 }
990 return setCostlyAlert("shared", bytes, &sharedAlertBytes);
991}
992
993int BandwidthController::removeSharedAlert(void) {
994 return removeCostlyAlert("shared", &sharedAlertBytes);
995}
996
997int BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
998 std::list<QuotaInfo>::iterator it;
999
1000 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001001 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -07001002 return -1;
1003 }
1004 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
1005 if (it->ifaceName == iface)
1006 break;
1007 }
1008
1009 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +00001010 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -07001011 return -1;
1012 }
1013
1014 return setCostlyAlert(iface, bytes, &it->alert);
1015}
1016
1017int BandwidthController::removeInterfaceAlert(const char *iface) {
1018 std::list<QuotaInfo>::iterator it;
1019
1020 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
1021 if (it->ifaceName == iface)
1022 break;
1023 }
1024
1025 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +00001026 ALOGE("No prior alert set for interface %s", iface);
JP Abgrall8a932722011-07-13 19:17:35 -07001027 return -1;
1028 }
1029
1030 return removeCostlyAlert(iface, &it->alert);
1031}
1032
1033int BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
1034 char *alertQuotaCmd;
JP Abgrall109899b2013-02-12 19:20:13 -08001035 char *chainName;
JP Abgrall8a932722011-07-13 19:17:35 -07001036 int res = 0;
1037 char *alertName;
1038
1039 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001040 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -07001041 return -1;
1042 }
1043 asprintf(&alertName, "%sAlert", costName);
1044 if (*alertBytes) {
1045 res = updateQuota(alertName, *alertBytes);
1046 } else {
JP Abgrall109899b2013-02-12 19:20:13 -08001047 asprintf(&chainName, "costly_%s", costName);
1048 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -07001049 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -07001050 free(alertQuotaCmd);
JP Abgrall109899b2013-02-12 19:20:13 -08001051 free(chainName);
JP Abgrall8a932722011-07-13 19:17:35 -07001052 }
1053 *alertBytes = bytes;
1054 free(alertName);
1055 return res;
1056}
1057
1058int BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
1059 char *alertQuotaCmd;
1060 char *chainName;
1061 char *alertName;
1062 int res = 0;
1063
1064 asprintf(&alertName, "%sAlert", costName);
1065 if (!*alertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001066 ALOGE("No prior alert set for %s alert", costName);
JP Abgrall8a932722011-07-13 19:17:35 -07001067 return -1;
1068 }
1069
1070 asprintf(&chainName, "costly_%s", costName);
JP Abgrall92009c82013-02-06 18:01:24 -08001071 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -07001072 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -07001073 free(alertQuotaCmd);
1074 free(chainName);
1075
1076 *alertBytes = 0;
1077 free(alertName);
1078 return res;
1079}
JP Abgralldb7da582011-09-18 12:57:32 -07001080
1081/*
1082 * Parse the ptks and bytes out of:
JP Abgrallbaeccc42013-06-25 09:44:10 -07001083 * Chain natctrl_tether_counters (4 references)
1084 * pkts bytes target prot opt in out source destination
1085 * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 counter wlan0_rmnet0: 0 bytes
1086 * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 counter rmnet0_wlan0: 0 bytes
1087 * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0 counter bt-pan_rmnet0: 0 bytes
1088 * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0 counter rmnet0_bt-pan: 0 bytes
JP Abgralldb7da582011-09-18 12:57:32 -07001089 */
JP Abgrallbaeccc42013-06-25 09:44:10 -07001090int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherStats filter,
1091 FILE *fp, std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -07001092 int res;
1093 char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1094 char iface0[MAX_IPT_OUTPUT_LINE_LEN];
1095 char iface1[MAX_IPT_OUTPUT_LINE_LEN];
1096 char rest[MAX_IPT_OUTPUT_LINE_LEN];
1097
JP Abgrallbaeccc42013-06-25 09:44:10 -07001098 TetherStats stats;
JP Abgralldb7da582011-09-18 12:57:32 -07001099 char *buffPtr;
1100 int64_t packets, bytes;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001101 int statsFound = 0;
1102
1103 bool filterPair = filter.intIface[0] && filter.extIface[0];
1104
1105 char *filterMsg = filter.getStatsLine();
1106 ALOGV("filter: %s", filterMsg);
1107 free(filterMsg);
1108
1109 stats = filter;
JP Abgralldb7da582011-09-18 12:57:32 -07001110
1111 while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1112 /* Clean up, so a failed parse can still print info */
1113 iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
JP Abgrall0031cea2012-04-17 16:38:23 -07001114 res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s",
JP Abgralldb7da582011-09-18 12:57:32 -07001115 &packets, &bytes, iface0, iface1, rest);
Steve Block3fb42e02011-10-20 11:55:56 +01001116 ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res,
JP Abgralldb7da582011-09-18 12:57:32 -07001117 iface0, iface1, packets, bytes, rest, buffPtr);
JP Abgralla2a64f02011-11-11 20:36:16 -08001118 extraProcessingInfo += buffPtr;
1119
JP Abgralldb7da582011-09-18 12:57:32 -07001120 if (res != 5) {
1121 continue;
1122 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001123 /*
1124 * The following assumes that the 1st rule has in:extIface out:intIface,
1125 * which is what NatController sets up.
1126 * If not filtering, the 1st match rx, and sets up the pair for the tx side.
1127 */
1128 if (filter.intIface[0] && filter.extIface[0]) {
1129 if (filter.intIface == iface0 && filter.extIface == iface1) {
1130 ALOGV("2Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1131 stats.rxPackets = packets;
1132 stats.rxBytes = bytes;
1133 } else if (filter.intIface == iface1 && filter.extIface == iface0) {
1134 ALOGV("2Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1135 stats.txPackets = packets;
1136 stats.txBytes = bytes;
1137 }
1138 } else if (filter.intIface[0] || filter.extIface[0]) {
1139 if (filter.intIface == iface0 || filter.extIface == iface1) {
1140 ALOGV("1Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1141 stats.intIface = iface0;
1142 stats.extIface = iface1;
1143 stats.rxPackets = packets;
1144 stats.rxBytes = bytes;
1145 } else if (filter.intIface == iface1 || filter.extIface == iface0) {
1146 ALOGV("1Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1147 stats.intIface = iface1;
1148 stats.extIface = iface0;
1149 stats.txPackets = packets;
1150 stats.txBytes = bytes;
1151 }
1152 } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ {
1153 if (!stats.intIface[0]) {
1154 ALOGV("0Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1155 stats.intIface = iface0;
1156 stats.extIface = iface1;
1157 stats.rxPackets = packets;
1158 stats.rxBytes = bytes;
1159 } else if (stats.intIface == iface1 && stats.extIface == iface0) {
1160 ALOGV("0Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1161 stats.txPackets = packets;
1162 stats.txBytes = bytes;
1163 }
1164 }
1165 if (stats.rxBytes != -1 && stats.txBytes != -1) {
1166 ALOGV("rx_bytes=%lld tx_bytes=%lld filterPair=%d", stats.rxBytes, stats.txBytes, filterPair);
1167 /* Send out stats, and prep for the next if needed. */
1168 char *msg = stats.getStatsLine();
1169 if (filterPair) {
1170 cli->sendMsg(ResponseCode::TetheringStatsResult, msg, false);
1171 return 0;
1172 } else {
1173 cli->sendMsg(ResponseCode::TetheringStatsListResult, msg, false);
1174 stats = filter;
1175 }
1176 free(msg);
1177 statsFound++;
JP Abgralldb7da582011-09-18 12:57:32 -07001178 }
1179 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001180 /* We found some stats, and the last one isn't a partial stats. */
1181 if (statsFound && (stats.rxBytes == -1 || stats.txBytes == -1)) {
1182 cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
1183 return 0;
1184 }
1185 return -1;
JP Abgralldb7da582011-09-18 12:57:32 -07001186}
1187
JP Abgrallbaeccc42013-06-25 09:44:10 -07001188char *BandwidthController::TetherStats::getStatsLine(void) const {
JP Abgralldb7da582011-09-18 12:57:32 -07001189 char *msg;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001190 asprintf(&msg, "%s %s %lld %lld %lld %lld", intIface.c_str(), extIface.c_str(),
JP Abgralldb7da582011-09-18 12:57:32 -07001191 rxBytes, rxPackets, txBytes, txPackets);
1192 return msg;
1193}
1194
JP Abgrallbaeccc42013-06-25 09:44:10 -07001195int BandwidthController::getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -07001196 int res;
1197 std::string fullCmd;
1198 FILE *iptOutput;
1199 const char *cmd;
1200
JP Abgralldb7da582011-09-18 12:57:32 -07001201 /*
1202 * Why not use some kind of lib to talk to iptables?
1203 * Because the only libs are libiptc and libip6tc in iptables, and they are
1204 * not easy to use. They require the known iptables match modules to be
1205 * preloaded/linked, and require apparently a lot of wrapper code to get
1206 * the wanted info.
1207 */
1208 fullCmd = IPTABLES_PATH;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001209 fullCmd += " -nvx -L ";
1210 fullCmd += NatController::LOCAL_TETHER_COUNTERS_CHAIN;
JP Abgralldb7da582011-09-18 12:57:32 -07001211 iptOutput = popen(fullCmd.c_str(), "r");
1212 if (!iptOutput) {
Steve Block5ea0c052012-01-06 19:18:11 +00001213 ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
JP Abgralla2a64f02011-11-11 20:36:16 -08001214 extraProcessingInfo += "Failed to run iptables.";
JP Abgralldb7da582011-09-18 12:57:32 -07001215 return -1;
1216 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001217 res = parseForwardChainStats(cli, stats, iptOutput, extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -07001218 pclose(iptOutput);
1219
1220 /* Currently NatController doesn't do ipv6 tethering, so we are done. */
1221 return res;
1222}