blob: dcebb9f1135a38301ebf2979beb9fb1b9d32e4b7 [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>
Nick Kralevich0b2b9022014-05-01 13:10:45 -070030#include <ctype.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070031
Matthew Leach2a54d962013-01-14 15:07:12 +000032#define __STDC_FORMAT_MACROS 1
33#include <inttypes.h>
34
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070035#include <sys/socket.h>
36#include <sys/stat.h>
37#include <sys/types.h>
38#include <sys/wait.h>
39
40#include <linux/netlink.h>
41#include <linux/rtnetlink.h>
42#include <linux/pkt_sched.h>
43
44#define LOG_TAG "BandwidthController"
45#include <cutils/log.h>
46#include <cutils/properties.h>
Rom Lemarchand14150212013-01-24 10:01:04 -080047#include <logwrap/logwrap.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070048
JP Abgrall0031cea2012-04-17 16:38:23 -070049#include "NetdConstants.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070050#include "BandwidthController.h"
JP Abgrallbaeccc42013-06-25 09:44:10 -070051#include "NatController.h" /* For LOCAL_TETHER_COUNTERS_CHAIN */
52#include "ResponseCode.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070053
JP Abgralldb7da582011-09-18 12:57:32 -070054/* Alphabetical */
SynergyDev7776cea2014-03-16 15:48:51 -070055#define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %" PRId64" --name %s"
JP Abgrallc6c67342011-10-07 16:28:54 -070056const char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070057const char* BandwidthController::LOCAL_INPUT = "bw_INPUT";
58const char* BandwidthController::LOCAL_FORWARD = "bw_FORWARD";
59const char* BandwidthController::LOCAL_OUTPUT = "bw_OUTPUT";
60const char* BandwidthController::LOCAL_RAW_PREROUTING = "bw_raw_PREROUTING";
61const char* BandwidthController::LOCAL_MANGLE_POSTROUTING = "bw_mangle_POSTROUTING";
JP Abgralldb7da582011-09-18 12:57:32 -070062const int BandwidthController::MAX_CMD_ARGS = 32;
63const int BandwidthController::MAX_CMD_LEN = 1024;
64const int BandwidthController::MAX_IFACENAME_LEN = 64;
65const int BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256;
66
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070067/**
68 * Some comments about the rules:
69 * * Ordering
70 * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
JP Abgrall29e8de22012-05-03 12:52:15 -070071 * E.g. "-I bw_INPUT -i rmnet0 --jump costly"
JP Abgrall7e51cde2013-07-03 13:33:05 -070072 * - quota'd rules in the costly chain should be before bw_penalty_box lookups.
73 * - bw_happy_box rejects everything by default.
JP Abgrall29e8de22012-05-03 12:52:15 -070074 * - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070075 *
76 * * global quota vs per interface quota
77 * - global quota for all costly interfaces uses a single costly chain:
78 * . initial rules
JP Abgrall7e51cde2013-07-03 13:33:05 -070079 * iptables -N bw_costly_shared
80 * iptables -I bw_INPUT -i iface0 --jump bw_costly_shared
81 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_shared
82 * iptables -I bw_costly_shared -m quota \! --quota 500000 \
JP Abgrallbfa74662011-06-29 19:23:04 -070083 * --jump REJECT --reject-with icmp-net-prohibited
JP Abgrall7e51cde2013-07-03 13:33:05 -070084 * iptables -A bw_costly_shared --jump bw_penalty_box
JP Abgralle4788732013-07-02 20:28:45 -070085 * If the happy box is enabled,
JP Abgrall7e51cde2013-07-03 13:33:05 -070086 * iptables -A bw_penalty_box --jump bw_happy_box
JP Abgrall8a932722011-07-13 19:17:35 -070087 *
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070088 * . adding a new iface to this, E.g.:
JP Abgrall7e51cde2013-07-03 13:33:05 -070089 * iptables -I bw_INPUT -i iface1 --jump bw_costly_shared
90 * iptables -I bw_OUTPUT -o iface1 --jump bw_costly_shared
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070091 *
92 * - quota per interface. This is achieve by having "costly" chains per quota.
93 * E.g. adding a new costly interface iface0 with its own quota:
JP Abgrall7e51cde2013-07-03 13:33:05 -070094 * iptables -N bw_costly_iface0
95 * iptables -I bw_INPUT -i iface0 --jump bw_costly_iface0
96 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_iface0
97 * iptables -A bw_costly_iface0 -m quota \! --quota 500000 \
JP Abgralle4788732013-07-02 20:28:45 -070098 * --jump REJECT --reject-with icmp-port-unreachable
JP Abgrall7e51cde2013-07-03 13:33:05 -070099 * iptables -A bw_costly_iface0 --jump bw_penalty_box
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700100 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700101 * * bw_penalty_box handling:
102 * - only one bw_penalty_box for all interfaces
103 * E.g Adding an app, it has to preserve the appened bw_happy_box, so "-I":
104 * iptables -I bw_penalty_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700105 * --jump REJECT --reject-with icmp-port-unreachable
106 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700107 * * bw_happy_box handling:
108 * - The bw_happy_box goes at the end of the penalty box.
JP Abgralle4788732013-07-02 20:28:45 -0700109 * E.g Adding a happy app,
JP Abgrall7e51cde2013-07-03 13:33:05 -0700110 * iptables -I bw_happy_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700111 * --jump RETURN
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700112 */
JP Abgrall0031cea2012-04-17 16:38:23 -0700113const char *BandwidthController::IPT_FLUSH_COMMANDS[] = {
114 /*
115 * Cleanup rules.
JP Abgrall7e51cde2013-07-03 13:33:05 -0700116 * Should normally include bw_costly_<iface>, but we rely on the way they are setup
JP Abgrall0031cea2012-04-17 16:38:23 -0700117 * to allow coexistance.
JP Abgrall39f8f242011-06-29 19:21:58 -0700118 */
JP Abgrall0031cea2012-04-17 16:38:23 -0700119 "-F bw_INPUT",
120 "-F bw_OUTPUT",
121 "-F bw_FORWARD",
JP Abgrall7e51cde2013-07-03 13:33:05 -0700122 "-F bw_happy_box",
123 "-F bw_penalty_box",
124 "-F bw_costly_shared",
JP Abgrallf66d6e92012-04-27 00:22:57 -0700125
126 "-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 Abgrall7e51cde2013-07-03 13:33:05 -0700132 "-X bw_happy_box",
133 "-X bw_penalty_box",
134 "-X bw_costly_shared",
JP Abgrall0dad7c22011-06-24 11:58:14 -0700135};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700136
JP Abgralldb7da582011-09-18 12:57:32 -0700137const char *BandwidthController::IPT_SETUP_COMMANDS[] = {
JP Abgrall7e51cde2013-07-03 13:33:05 -0700138 "-N bw_happy_box",
139 "-N bw_penalty_box",
140 "-N bw_costly_shared",
JP Abgrall0dad7c22011-06-24 11:58:14 -0700141};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700142
JP Abgralldb7da582011-09-18 12:57:32 -0700143const char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = {
JP Abgrall0031cea2012-04-17 16:38:23 -0700144 "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700145
JP Abgrall0031cea2012-04-17 16:38:23 -0700146 "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700147
JP Abgrall7e51cde2013-07-03 13:33:05 -0700148 "-A bw_costly_shared --jump bw_penalty_box",
JP Abgrallf66d6e92012-04-27 00:22:57 -0700149
JP Abgrall92009c82013-02-06 18:01:24 -0800150 "-t raw -A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */
151 "-t mangle -A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700152};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700153
154BandwidthController::BandwidthController(void) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700155}
156
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700157int BandwidthController::runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrallad729ac2012-04-24 23:27:44 -0700158 IptFailureLog failureHandling) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700159 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700160
Steve Block3fb42e02011-10-20 11:55:56 +0100161 ALOGV("runIpxtablesCmd(cmd=%s)", cmd);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700162 res |= runIptablesCmd(cmd, jumpHandling, IptIpV4, failureHandling);
163 res |= runIptablesCmd(cmd, jumpHandling, IptIpV6, failureHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700164 return res;
165}
166
JP Abgrall26e0d492011-06-24 19:21:51 -0700167int BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) {
168
169 memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0'
170 strncpy(buffer, src, buffSize);
171 return buffer[buffSize - 1];
172}
173
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700174int BandwidthController::runIptablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrallad729ac2012-04-24 23:27:44 -0700175 IptIpVer iptVer, IptFailureLog failureHandling) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700176 char buffer[MAX_CMD_LEN];
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700177 const char *argv[MAX_CMD_ARGS];
JP Abgrall26e0d492011-06-24 19:21:51 -0700178 int argc = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700179 char *next = buffer;
180 char *tmp;
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700181 int res;
Rom Lemarchand14150212013-01-24 10:01:04 -0800182 int status = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700183
JP Abgrall0dad7c22011-06-24 11:58:14 -0700184 std::string fullCmd = cmd;
JP Abgrall26e0d492011-06-24 19:21:51 -0700185
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700186 switch (jumpHandling) {
187 case IptJumpReject:
JP Abgrall340d5cc2013-06-28 17:06:00 -0700188 /*
189 * Must be carefull what one rejects with, as uper layer protocols will just
190 * keep on hammering the device until the number of retries are done.
191 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
192 */
193 fullCmd += " --jump REJECT";
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700194 break;
195 case IptJumpReturn:
196 fullCmd += " --jump RETURN";
197 break;
198 case IptJumpNoAdd:
199 break;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700200 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700201
Paul Jensen94b2ab92015-08-04 10:35:05 -0400202 fullCmd.insert(0, " -w ");
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700203 fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700204
Rom Lemarchand14150212013-01-24 10:01:04 -0800205 if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
206 ALOGE("iptables command too long");
207 return -1;
208 }
209
210 argc = 0;
211 while ((tmp = strsep(&next, " "))) {
212 argv[argc++] = tmp;
213 if (argc >= MAX_CMD_ARGS) {
214 ALOGE("iptables argument overflow");
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700215 return -1;
216 }
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700217 }
Rom Lemarchand14150212013-01-24 10:01:04 -0800218
219 argv[argc] = NULL;
220 res = android_fork_execvp(argc, (char **)argv, &status, false,
221 failureHandling == IptFailShow);
JP Abgrallc8dc63b2013-02-13 16:30:00 -0800222 res = res || !WIFEXITED(status) || WEXITSTATUS(status);
223 if (res && failureHandling == IptFailShow) {
224 ALOGE("runIptablesCmd(): res=%d status=%d failed %s", res, status,
225 fullCmd.c_str());
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700226 }
227 return res;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700228}
229
JP Abgrall0e540ec2013-08-26 15:13:10 -0700230void BandwidthController::flushCleanTables(bool doClean) {
231 /* Flush and remove the bw_costly_<iface> tables */
232 flushExistingCostlyTables(doClean);
JP Abgrall0031cea2012-04-17 16:38:23 -0700233
234 /* Some of the initialCommands are allowed to fail */
235 runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
236 IPT_FLUSH_COMMANDS, RunCmdFailureOk);
237
JP Abgrall0e540ec2013-08-26 15:13:10 -0700238 if (doClean) {
239 runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
240 IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
241 }
242}
JP Abgrall0031cea2012-04-17 16:38:23 -0700243
JP Abgrall0e540ec2013-08-26 15:13:10 -0700244int BandwidthController::setupIptablesHooks(void) {
245
246 /* flush+clean is allowed to fail */
247 flushCleanTables(true);
JP Abgrall0031cea2012-04-17 16:38:23 -0700248 runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*),
249 IPT_SETUP_COMMANDS, RunCmdFailureBad);
250
251 return 0;
JP Abgrall0031cea2012-04-17 16:38:23 -0700252}
253
254int BandwidthController::enableBandwidthControl(bool force) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700255 int res;
JP Abgrall0031cea2012-04-17 16:38:23 -0700256 char value[PROPERTY_VALUE_MAX];
257
258 if (!force) {
259 property_get("persist.bandwidth.enable", value, "1");
260 if (!strcmp(value, "0"))
261 return 0;
262 }
JP Abgrall8a932722011-07-13 19:17:35 -0700263
JP Abgralldb7da582011-09-18 12:57:32 -0700264 /* Let's pretend we started from scratch ... */
JP Abgrall8a932722011-07-13 19:17:35 -0700265 sharedQuotaIfaces.clear();
266 quotaIfaces.clear();
JP Abgralldb7da582011-09-18 12:57:32 -0700267 globalAlertBytes = 0;
JP Abgrallc6c67342011-10-07 16:28:54 -0700268 globalAlertTetherCount = 0;
JP Abgralldb7da582011-09-18 12:57:32 -0700269 sharedQuotaBytes = sharedAlertBytes = 0;
270
JP Abgrall0e540ec2013-08-26 15:13:10 -0700271 flushCleanTables(false);
272 res = runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*),
JP Abgralldb7da582011-09-18 12:57:32 -0700273 IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad);
JP Abgrall8a932722011-07-13 19:17:35 -0700274
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700275 return res;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700276
277}
278
279int BandwidthController::disableBandwidthControl(void) {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700280
281 flushCleanTables(false);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700282 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700283}
284
JP Abgrall8a932722011-07-13 19:17:35 -0700285int BandwidthController::runCommands(int numCommands, const char *commands[],
286 RunCmdErrHandling cmdErrHandling) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700287 int res = 0;
JP Abgrallad729ac2012-04-24 23:27:44 -0700288 IptFailureLog failureLogging = IptFailShow;
289 if (cmdErrHandling == RunCmdFailureOk) {
290 failureLogging = IptFailHide;
291 }
Steve Block3fb42e02011-10-20 11:55:56 +0100292 ALOGV("runCommands(): %d commands", numCommands);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700293 for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700294 res = runIpxtablesCmd(commands[cmdNum], IptJumpNoAdd, failureLogging);
JP Abgrall0031cea2012-04-17 16:38:23 -0700295 if (res && cmdErrHandling != RunCmdFailureOk)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700296 return res;
297 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700298 return 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700299}
300
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700301std::string BandwidthController::makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700302 std::string res;
JP Abgrall8a932722011-07-13 19:17:35 -0700303 char *buff;
304 const char *opFlag;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700305
306 switch (op) {
JP Abgrall8a932722011-07-13 19:17:35 -0700307 case IptOpInsert:
308 opFlag = "-I";
309 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800310 case IptOpAppend:
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700311 ALOGE("Append op not supported for %s uids", chain);
312 res = "";
313 return res;
JP Abgrall109899b2013-02-12 19:20:13 -0800314 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700315 case IptOpReplace:
316 opFlag = "-R";
317 break;
318 default:
319 case IptOpDelete:
320 opFlag = "-D";
321 break;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700322 }
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700323 asprintf(&buff, "%s %s -m owner --uid-owner %d", opFlag, chain, uid);
JP Abgrall8a932722011-07-13 19:17:35 -0700324 res = buff;
325 free(buff);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700326 return res;
327}
328
329int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700330 return manipulateNaughtyApps(numUids, appUids, SpecialAppOpAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700331}
332
333int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700334 return manipulateNaughtyApps(numUids, appUids, SpecialAppOpRemove);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700335}
336
JP Abgralle4788732013-07-02 20:28:45 -0700337int BandwidthController::addNiceApps(int numUids, char *appUids[]) {
338 return manipulateNiceApps(numUids, appUids, SpecialAppOpAdd);
339}
340
341int BandwidthController::removeNiceApps(int numUids, char *appUids[]) {
342 return manipulateNiceApps(numUids, appUids, SpecialAppOpRemove);
343}
344
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700345int BandwidthController::manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
Lorenzo Colittib1f05572016-03-18 11:55:56 +0900346 return manipulateSpecialApps(numUids, appStrUids, "bw_penalty_box", IptJumpReject, appOp);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700347}
348
JP Abgralle4788732013-07-02 20:28:45 -0700349int BandwidthController::manipulateNiceApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
Lorenzo Colittib1f05572016-03-18 11:55:56 +0900350 return manipulateSpecialApps(numUids, appStrUids, "bw_happy_box", IptJumpReturn, appOp);
JP Abgralle4788732013-07-02 20:28:45 -0700351}
352
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700353
354int BandwidthController::manipulateSpecialApps(int numUids, char *appStrUids[],
355 const char *chain,
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700356 IptJumpOp jumpHandling, SpecialAppOp appOp) {
357
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700358 int uidNum;
JP Abgrall26e0d492011-06-24 19:21:51 -0700359 const char *failLogTemplate;
360 IptOp op;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700361 int appUids[numUids];
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700362 std::string iptCmd;
JP Abgrall8a932722011-07-13 19:17:35 -0700363
JP Abgrall26e0d492011-06-24 19:21:51 -0700364 switch (appOp) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700365 case SpecialAppOpAdd:
JP Abgrall8a932722011-07-13 19:17:35 -0700366 op = IptOpInsert;
JP Abgrallaf476f72013-07-03 12:23:55 -0700367 failLogTemplate = "Failed to add app uid %s(%d) to %s.";
JP Abgrall8a932722011-07-13 19:17:35 -0700368 break;
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700369 case SpecialAppOpRemove:
JP Abgrall8a932722011-07-13 19:17:35 -0700370 op = IptOpDelete;
JP Abgrallaf476f72013-07-03 12:23:55 -0700371 failLogTemplate = "Failed to delete app uid %s(%d) from %s box.";
JP Abgrall8a932722011-07-13 19:17:35 -0700372 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700373 default:
374 ALOGE("Unexpected app Op %d", appOp);
375 return -1;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700376 }
377
378 for (uidNum = 0; uidNum < numUids; uidNum++) {
JP Abgrallaf476f72013-07-03 12:23:55 -0700379 char *end;
380 appUids[uidNum] = strtoul(appStrUids[uidNum], &end, 0);
381 if (*end || !*appStrUids[uidNum]) {
382 ALOGE(failLogTemplate, appStrUids[uidNum], appUids[uidNum], chain);
JP Abgrall26e0d492011-06-24 19:21:51 -0700383 goto fail_parse;
384 }
385 }
JP Abgrall26e0d492011-06-24 19:21:51 -0700386
387 for (uidNum = 0; uidNum < numUids; uidNum++) {
JP Abgrallb1d24092012-04-27 01:02:31 -0700388 int uid = appUids[uidNum];
JP Abgrallb1d24092012-04-27 01:02:31 -0700389
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700390 iptCmd = makeIptablesSpecialAppCmd(op, uid, chain);
391 if (runIpxtablesCmd(iptCmd.c_str(), jumpHandling)) {
JP Abgrallaf476f72013-07-03 12:23:55 -0700392 ALOGE(failLogTemplate, appStrUids[uidNum], uid, chain);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700393 goto fail_with_uidNum;
394 }
395 }
396 return 0;
397
JP Abgrall26e0d492011-06-24 19:21:51 -0700398fail_with_uidNum:
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700399 /* Try to remove the uid that failed in any case*/
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700400 iptCmd = makeIptablesSpecialAppCmd(IptOpDelete, appUids[uidNum], chain);
401 runIpxtablesCmd(iptCmd.c_str(), jumpHandling);
JP Abgrall26e0d492011-06-24 19:21:51 -0700402fail_parse:
403 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700404}
405
JP Abgrall26e0d492011-06-24 19:21:51 -0700406std::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700407 std::string res;
JP Abgrall8a932722011-07-13 19:17:35 -0700408 char *buff;
409 const char *opFlag;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700410
SynergyDev7776cea2014-03-16 15:48:51 -0700411 ALOGV("makeIptablesQuotaCmd(%d, %" PRId64")", op, quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700412
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700413 switch (op) {
JP Abgrall8a932722011-07-13 19:17:35 -0700414 case IptOpInsert:
415 opFlag = "-I";
416 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800417 case IptOpAppend:
418 opFlag = "-A";
419 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700420 case IptOpReplace:
421 opFlag = "-R";
422 break;
423 default:
424 case IptOpDelete:
425 opFlag = "-D";
426 break;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700427 }
JP Abgrall8a932722011-07-13 19:17:35 -0700428
JP Abgrallbfa74662011-06-29 19:23:04 -0700429 // The requried IP version specific --jump REJECT ... will be added later.
SynergyDev7776cea2014-03-16 15:48:51 -0700430 asprintf(&buff, "%s bw_costly_%s -m quota2 ! --quota %" PRId64" --name %s", opFlag, costName, quota,
JP Abgrall8a932722011-07-13 19:17:35 -0700431 costName);
432 res = buff;
433 free(buff);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700434 return res;
435}
436
JP Abgrall26e0d492011-06-24 19:21:51 -0700437int BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700438 char cmd[MAX_CMD_LEN];
JP Abgrall0031cea2012-04-17 16:38:23 -0700439 int res = 0, res1, res2;
JP Abgrall8a932722011-07-13 19:17:35 -0700440 int ruleInsertPos = 1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700441 std::string costString;
442 const char *costCString;
443
JP Abgrall0dad7c22011-06-24 11:58:14 -0700444 /* The "-N costly" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700445 switch (quotaType) {
446 case QuotaUnique:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700447 costString = "bw_costly_";
JP Abgrall0dad7c22011-06-24 11:58:14 -0700448 costString += ifn;
449 costCString = costString.c_str();
JP Abgrall0031cea2012-04-17 16:38:23 -0700450 /*
JP Abgrall7e51cde2013-07-03 13:33:05 -0700451 * Flush the bw_costly_<iface> is allowed to fail in case it didn't exist.
JP Abgrall0031cea2012-04-17 16:38:23 -0700452 * Creating a new one is allowed to fail in case it existed.
453 * This helps with netd restarts.
454 */
455 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700456 res1 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700457 snprintf(cmd, sizeof(cmd), "-N %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700458 res2 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700459 res = (res1 && res2) || (!res1 && !res2);
460
JP Abgrall7e51cde2013-07-03 13:33:05 -0700461 snprintf(cmd, sizeof(cmd), "-A %s -j bw_penalty_box", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700462 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall26e0d492011-06-24 19:21:51 -0700463 break;
464 case QuotaShared:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700465 costCString = "bw_costly_shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700466 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700467 default:
468 ALOGE("Unexpected quotatype %d", quotaType);
469 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700470 }
471
JP Abgrall8a932722011-07-13 19:17:35 -0700472 if (globalAlertBytes) {
473 /* The alert rule comes 1st */
474 ruleInsertPos = 2;
475 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700476
477 snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700478 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700479
480 snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700481 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0031cea2012-04-17 16:38:23 -0700482
483 snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700484 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700485
486 snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700487 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
Erik Kline58a94482015-10-02 17:52:37 +0900488
489 snprintf(cmd, sizeof(cmd), "-D bw_FORWARD -o %s --jump %s", ifn, costCString);
490 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
491 snprintf(cmd, sizeof(cmd), "-A bw_FORWARD -o %s --jump %s", ifn, costCString);
492 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
493
JP Abgrall0dad7c22011-06-24 11:58:14 -0700494 return res;
495}
496
JP Abgrall26e0d492011-06-24 19:21:51 -0700497int BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700498 char cmd[MAX_CMD_LEN];
499 int res = 0;
500 std::string costString;
501 const char *costCString;
502
JP Abgrall26e0d492011-06-24 19:21:51 -0700503 switch (quotaType) {
504 case QuotaUnique:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700505 costString = "bw_costly_";
JP Abgrall0dad7c22011-06-24 11:58:14 -0700506 costString += ifn;
507 costCString = costString.c_str();
JP Abgrall26e0d492011-06-24 19:21:51 -0700508 break;
509 case QuotaShared:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700510 costCString = "bw_costly_shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700511 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700512 default:
513 ALOGE("Unexpected quotatype %d", quotaType);
514 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700515 }
516
JP Abgrall0031cea2012-04-17 16:38:23 -0700517 snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700518 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
Erik Kline58a94482015-10-02 17:52:37 +0900519 for (const auto tableName : {LOCAL_OUTPUT, LOCAL_FORWARD}) {
520 snprintf(cmd, sizeof(cmd), "-D %s -o %s --jump %s", tableName, ifn, costCString);
521 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
522 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700523
JP Abgrall7e51cde2013-07-03 13:33:05 -0700524 /* The "-N bw_costly_shared" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700525 if (quotaType == QuotaUnique) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700526 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700527 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgralla9f802c2011-06-29 15:46:45 -0700528 snprintf(cmd, sizeof(cmd), "-X %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700529 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700530 }
531 return res;
532}
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700533
JP Abgrall0dad7c22011-06-24 11:58:14 -0700534int BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700535 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700536 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700537 std::string quotaCmd;
JP Abgrall8a932722011-07-13 19:17:35 -0700538 std::string ifaceName;
539 ;
JP Abgrallbfa74662011-06-29 19:23:04 -0700540 const char *costName = "shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700541 std::list<std::string>::iterator it;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700542
JP Abgrall8a932722011-07-13 19:17:35 -0700543 if (!maxBytes) {
544 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000545 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700546 return -1;
547 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700548 if (!isIfaceName(iface))
549 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700550 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000551 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700552 return -1;
553 }
554 ifaceName = ifn;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700555
556 if (maxBytes == -1) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700557 return removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700558 }
559
560 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700561 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
562 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700563 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700564 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700565
JP Abgrall0dad7c22011-06-24 11:58:14 -0700566 if (it == sharedQuotaIfaces.end()) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700567 res |= prepCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700568 if (sharedQuotaIfaces.empty()) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700569 quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700570 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700571 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000572 ALOGE("Failed set quota rule");
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700573 goto fail;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700574 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700575 sharedQuotaBytes = maxBytes;
576 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700577 sharedQuotaIfaces.push_front(ifaceName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700578
579 }
580
581 if (maxBytes != sharedQuotaBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700582 res |= updateQuota(costName, maxBytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700583 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000584 ALOGE("Failed update quota for %s", costName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700585 goto fail;
586 }
587 sharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700588 }
589 return 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700590
591 fail:
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700592 /*
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700593 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
594 * rules in the kernel to see which ones need cleaning up.
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700595 * For now callers needs to choose if they want to "ndc bandwidth enable"
596 * which resets everything.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700597 */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700598 removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700599 return -1;
600}
601
JP Abgrall8a932722011-07-13 19:17:35 -0700602/* It will also cleanup any shared alerts */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700603int BandwidthController::removeInterfaceSharedQuota(const char *iface) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700604 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700605 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700606 std::string ifaceName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700607 std::list<std::string>::iterator it;
JP Abgrallbfa74662011-06-29 19:23:04 -0700608 const char *costName = "shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700609
JP Abgrall69261cb2014-06-19 18:35:24 -0700610 if (!isIfaceName(iface))
611 return -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700612 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000613 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700614 return -1;
615 }
JP Abgrall8a932722011-07-13 19:17:35 -0700616 ifaceName = ifn;
JP Abgrall26e0d492011-06-24 19:21:51 -0700617
JP Abgrall0dad7c22011-06-24 11:58:14 -0700618 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
619 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700620 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700621 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700622 if (it == sharedQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000623 ALOGE("No such iface %s to delete", ifn);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700624 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700625 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700626
JP Abgrall26e0d492011-06-24 19:21:51 -0700627 res |= cleanupCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700628 sharedQuotaIfaces.erase(it);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700629
JP Abgrall0dad7c22011-06-24 11:58:14 -0700630 if (sharedQuotaIfaces.empty()) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700631 std::string quotaCmd;
JP Abgrallbfa74662011-06-29 19:23:04 -0700632 quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700633 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall8a932722011-07-13 19:17:35 -0700634 sharedQuotaBytes = 0;
635 if (sharedAlertBytes) {
636 removeSharedAlert();
637 sharedAlertBytes = 0;
638 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700639 }
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700640 return res;
641}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700642
643int BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
644 char ifn[MAX_IFACENAME_LEN];
645 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700646 std::string ifaceName;
647 const char *costName;
648 std::list<QuotaInfo>::iterator it;
649 std::string quotaCmd;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700650
JP Abgrall69261cb2014-06-19 18:35:24 -0700651 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700652 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700653
JP Abgrall8a932722011-07-13 19:17:35 -0700654 if (!maxBytes) {
655 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000656 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700657 return -1;
658 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700659 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700660 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700661 }
662
JP Abgrall8a932722011-07-13 19:17:35 -0700663 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000664 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700665 return -1;
666 }
667 ifaceName = ifn;
668 costName = iface;
669
JP Abgrall0dad7c22011-06-24 11:58:14 -0700670 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700671 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
JP Abgrall8a932722011-07-13 19:17:35 -0700672 if (it->ifaceName == ifaceName)
JP Abgrall0dad7c22011-06-24 11:58:14 -0700673 break;
674 }
675
676 if (it == quotaIfaces.end()) {
JP Abgralle4788732013-07-02 20:28:45 -0700677 /* Preparing the iface adds a penalty/happy box check */
JP Abgrall26e0d492011-06-24 19:21:51 -0700678 res |= prepCostlyIface(ifn, QuotaUnique);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700679 /*
JP Abgralle4788732013-07-02 20:28:45 -0700680 * The rejecting quota limit should go after the penalty/happy box checks
JP Abgrallbaeccc42013-06-25 09:44:10 -0700681 * or else a naughty app could just eat up the quota.
682 * So we append here.
683 */
JP Abgrall109899b2013-02-12 19:20:13 -0800684 quotaCmd = makeIptablesQuotaCmd(IptOpAppend, costName, maxBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700685 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700686 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000687 ALOGE("Failed set quota rule");
JP Abgrall0dad7c22011-06-24 11:58:14 -0700688 goto fail;
689 }
690
JP Abgrall8a932722011-07-13 19:17:35 -0700691 quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
JP Abgrall0dad7c22011-06-24 11:58:14 -0700692
693 } else {
JP Abgrall8a932722011-07-13 19:17:35 -0700694 res |= updateQuota(costName, maxBytes);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700695 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000696 ALOGE("Failed update quota for %s", iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700697 goto fail;
698 }
JP Abgrall8a932722011-07-13 19:17:35 -0700699 it->quota = maxBytes;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700700 }
701 return 0;
702
703 fail:
704 /*
705 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
706 * rules in the kernel to see which ones need cleaning up.
707 * For now callers needs to choose if they want to "ndc bandwidth enable"
708 * which resets everything.
709 */
710 removeInterfaceSharedQuota(ifn);
711 return -1;
712}
713
JP Abgrall8a932722011-07-13 19:17:35 -0700714int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
715 return getInterfaceQuota("shared", bytes);
716}
717
718int BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
719 FILE *fp;
720 char *fname;
721 int scanRes;
722
JP Abgrall69261cb2014-06-19 18:35:24 -0700723 if (!isIfaceName(costName))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700724 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700725
JP Abgrall8a932722011-07-13 19:17:35 -0700726 asprintf(&fname, "/proc/net/xt_quota/%s", costName);
Nick Kralevich53ea9ca2015-01-31 13:54:00 -0800727 fp = fopen(fname, "re");
JP Abgrall8a932722011-07-13 19:17:35 -0700728 free(fname);
729 if (!fp) {
Steve Block5ea0c052012-01-06 19:18:11 +0000730 ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
JP Abgrall8a932722011-07-13 19:17:35 -0700731 return -1;
732 }
Mark Salyzynca0b5e22014-03-26 14:15:03 -0700733 scanRes = fscanf(fp, "%" SCNd64, bytes);
SynergyDev7776cea2014-03-16 15:48:51 -0700734 ALOGV("Read quota res=%d bytes=%" PRId64, scanRes, *bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700735 fclose(fp);
736 return scanRes == 1 ? 0 : -1;
737}
738
JP Abgrall0dad7c22011-06-24 11:58:14 -0700739int BandwidthController::removeInterfaceQuota(const char *iface) {
740
741 char ifn[MAX_IFACENAME_LEN];
742 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700743 std::string ifaceName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700744 std::list<QuotaInfo>::iterator it;
JP Abgrall26e0d492011-06-24 19:21:51 -0700745
JP Abgrall69261cb2014-06-19 18:35:24 -0700746 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700747 return -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700748 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000749 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700750 return -1;
751 }
752 ifaceName = ifn;
JP Abgrall26e0d492011-06-24 19:21:51 -0700753
JP Abgrall0dad7c22011-06-24 11:58:14 -0700754 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
JP Abgrall8a932722011-07-13 19:17:35 -0700755 if (it->ifaceName == ifaceName)
JP Abgrall0dad7c22011-06-24 11:58:14 -0700756 break;
757 }
758
759 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000760 ALOGE("No such iface %s to delete", ifn);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700761 return -1;
762 }
763
764 /* This also removes the quota command of CostlyIface chain. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700765 res |= cleanupCostlyIface(ifn, QuotaUnique);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700766
767 quotaIfaces.erase(it);
768
769 return res;
770}
JP Abgrall8a932722011-07-13 19:17:35 -0700771
772int BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
773 FILE *fp;
774 char *fname;
775
JP Abgrall69261cb2014-06-19 18:35:24 -0700776 if (!isIfaceName(quotaName)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700777 ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName);
778 return -1;
779 }
780
JP Abgrall8a932722011-07-13 19:17:35 -0700781 asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
Nick Kralevich53ea9ca2015-01-31 13:54:00 -0800782 fp = fopen(fname, "we");
JP Abgrall8a932722011-07-13 19:17:35 -0700783 free(fname);
784 if (!fp) {
Steve Block5ea0c052012-01-06 19:18:11 +0000785 ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
JP Abgrall8a932722011-07-13 19:17:35 -0700786 return -1;
787 }
SynergyDev7776cea2014-03-16 15:48:51 -0700788 fprintf(fp, "%" PRId64"\n", bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700789 fclose(fp);
790 return 0;
791}
792
793int BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
794 int res = 0;
795 const char *opFlag;
796 char *alertQuotaCmd;
797
798 switch (op) {
799 case IptOpInsert:
800 opFlag = "-I";
801 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800802 case IptOpAppend:
803 opFlag = "-A";
804 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700805 case IptOpReplace:
806 opFlag = "-R";
807 break;
808 default:
809 case IptOpDelete:
810 opFlag = "-D";
811 break;
812 }
813
JP Abgrall92009c82013-02-06 18:01:24 -0800814 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800815 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700816 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700817 free(alertQuotaCmd);
JP Abgrall92009c82013-02-06 18:01:24 -0800818 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800819 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700820 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700821 free(alertQuotaCmd);
822 return res;
823}
824
JP Abgrallc6c67342011-10-07 16:28:54 -0700825int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
826 int res = 0;
827 const char *opFlag;
JP Abgrall8a932722011-07-13 19:17:35 -0700828 char *alertQuotaCmd;
JP Abgrallc6c67342011-10-07 16:28:54 -0700829
830 switch (op) {
831 case IptOpInsert:
832 opFlag = "-I";
833 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800834 case IptOpAppend:
835 opFlag = "-A";
836 break;
JP Abgrallc6c67342011-10-07 16:28:54 -0700837 case IptOpReplace:
838 opFlag = "-R";
839 break;
840 default:
841 case IptOpDelete:
842 opFlag = "-D";
843 break;
844 }
845
JP Abgrall92009c82013-02-06 18:01:24 -0800846 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800847 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700848 res = runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrallc6c67342011-10-07 16:28:54 -0700849 free(alertQuotaCmd);
850 return res;
851}
852
853int BandwidthController::setGlobalAlert(int64_t bytes) {
854 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700855 int res = 0;
856
857 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000858 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700859 return -1;
860 }
861 if (globalAlertBytes) {
862 res = updateQuota(alertName, bytes);
863 } else {
864 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700865 if (globalAlertTetherCount) {
Steve Block3fb42e02011-10-20 11:55:56 +0100866 ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700867 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
868 }
JP Abgrall8a932722011-07-13 19:17:35 -0700869 }
870 globalAlertBytes = bytes;
871 return res;
872}
873
JP Abgrallc6c67342011-10-07 16:28:54 -0700874int BandwidthController::setGlobalAlertInForwardChain(void) {
875 const char *alertName = ALERT_GLOBAL_NAME;
876 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700877
JP Abgrallc6c67342011-10-07 16:28:54 -0700878 globalAlertTetherCount++;
Steve Block3fb42e02011-10-20 11:55:56 +0100879 ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700880
881 /*
882 * If there is no globalAlert active we are done.
883 * If there is an active globalAlert but this is not the 1st
884 * tether, we are also done.
885 */
886 if (!globalAlertBytes || globalAlertTetherCount != 1) {
887 return 0;
888 }
889
890 /* We only add the rule if this was the 1st tether added. */
891 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
892 return res;
893}
894
895int BandwidthController::removeGlobalAlert(void) {
896
897 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700898 int res = 0;
899
900 if (!globalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000901 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700902 return -1;
903 }
904 res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700905 if (globalAlertTetherCount) {
906 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
907 }
JP Abgrall8a932722011-07-13 19:17:35 -0700908 globalAlertBytes = 0;
909 return res;
910}
911
JP Abgrallc6c67342011-10-07 16:28:54 -0700912int BandwidthController::removeGlobalAlertInForwardChain(void) {
913 int res = 0;
914 const char *alertName = ALERT_GLOBAL_NAME;
915
916 if (!globalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000917 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700918 return -1;
919 }
920
921 globalAlertTetherCount--;
922 /*
923 * If there is no globalAlert active we are done.
924 * If there is an active globalAlert but there are more
925 * tethers, we are also done.
926 */
927 if (!globalAlertBytes || globalAlertTetherCount >= 1) {
928 return 0;
929 }
930
931 /* We only detete the rule if this was the last tether removed. */
932 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
933 return res;
934}
935
JP Abgrall8a932722011-07-13 19:17:35 -0700936int BandwidthController::setSharedAlert(int64_t bytes) {
937 if (!sharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000938 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700939 return -1;
940 }
941 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000942 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700943 return -1;
944 }
945 return setCostlyAlert("shared", bytes, &sharedAlertBytes);
946}
947
948int BandwidthController::removeSharedAlert(void) {
949 return removeCostlyAlert("shared", &sharedAlertBytes);
950}
951
952int BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
953 std::list<QuotaInfo>::iterator it;
954
JP Abgrall69261cb2014-06-19 18:35:24 -0700955 if (!isIfaceName(iface)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700956 ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface);
957 return -1;
958 }
959
JP Abgrall8a932722011-07-13 19:17:35 -0700960 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000961 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700962 return -1;
963 }
964 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
965 if (it->ifaceName == iface)
966 break;
967 }
968
969 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000970 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700971 return -1;
972 }
973
974 return setCostlyAlert(iface, bytes, &it->alert);
975}
976
977int BandwidthController::removeInterfaceAlert(const char *iface) {
978 std::list<QuotaInfo>::iterator it;
979
JP Abgrall69261cb2014-06-19 18:35:24 -0700980 if (!isIfaceName(iface)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700981 ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface);
982 return -1;
983 }
984
JP Abgrall8a932722011-07-13 19:17:35 -0700985 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
986 if (it->ifaceName == iface)
987 break;
988 }
989
990 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000991 ALOGE("No prior alert set for interface %s", iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700992 return -1;
993 }
994
995 return removeCostlyAlert(iface, &it->alert);
996}
997
998int BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
999 char *alertQuotaCmd;
JP Abgrall109899b2013-02-12 19:20:13 -08001000 char *chainName;
JP Abgrall8a932722011-07-13 19:17:35 -07001001 int res = 0;
1002 char *alertName;
1003
JP Abgrall69261cb2014-06-19 18:35:24 -07001004 if (!isIfaceName(costName)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -07001005 ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName);
1006 return -1;
1007 }
1008
JP Abgrall8a932722011-07-13 19:17:35 -07001009 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001010 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -07001011 return -1;
1012 }
1013 asprintf(&alertName, "%sAlert", costName);
1014 if (*alertBytes) {
1015 res = updateQuota(alertName, *alertBytes);
1016 } else {
JP Abgrall7e51cde2013-07-03 13:33:05 -07001017 asprintf(&chainName, "bw_costly_%s", costName);
JP Abgrall109899b2013-02-12 19:20:13 -08001018 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -07001019 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -07001020 free(alertQuotaCmd);
JP Abgrall109899b2013-02-12 19:20:13 -08001021 free(chainName);
JP Abgrall8a932722011-07-13 19:17:35 -07001022 }
1023 *alertBytes = bytes;
1024 free(alertName);
1025 return res;
1026}
1027
1028int BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
1029 char *alertQuotaCmd;
1030 char *chainName;
1031 char *alertName;
1032 int res = 0;
1033
JP Abgrall69261cb2014-06-19 18:35:24 -07001034 if (!isIfaceName(costName)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -07001035 ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName);
1036 return -1;
1037 }
1038
JP Abgrall8a932722011-07-13 19:17:35 -07001039 if (!*alertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001040 ALOGE("No prior alert set for %s alert", costName);
JP Abgrall8a932722011-07-13 19:17:35 -07001041 return -1;
1042 }
1043
Jesper Hanssona9d791f2012-04-27 13:54:27 +02001044 asprintf(&alertName, "%sAlert", costName);
JP Abgrall7e51cde2013-07-03 13:33:05 -07001045 asprintf(&chainName, "bw_costly_%s", costName);
JP Abgrall92009c82013-02-06 18:01:24 -08001046 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -07001047 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -07001048 free(alertQuotaCmd);
1049 free(chainName);
1050
1051 *alertBytes = 0;
1052 free(alertName);
1053 return res;
1054}
JP Abgralldb7da582011-09-18 12:57:32 -07001055
1056/*
1057 * Parse the ptks and bytes out of:
JP Abgrallbaeccc42013-06-25 09:44:10 -07001058 * Chain natctrl_tether_counters (4 references)
1059 * pkts bytes target prot opt in out source destination
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001060 * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
1061 * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0
1062 * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0
1063 * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0
1064 * It results in an error if invoked and no tethering counter rules exist. The constraint
1065 * helps detect complete parsing failure.
JP Abgralldb7da582011-09-18 12:57:32 -07001066 */
JP Abgrallbaeccc42013-06-25 09:44:10 -07001067int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherStats filter,
1068 FILE *fp, std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -07001069 int res;
1070 char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1071 char iface0[MAX_IPT_OUTPUT_LINE_LEN];
1072 char iface1[MAX_IPT_OUTPUT_LINE_LEN];
1073 char rest[MAX_IPT_OUTPUT_LINE_LEN];
1074
JP Abgrallbaeccc42013-06-25 09:44:10 -07001075 TetherStats stats;
JP Abgralldb7da582011-09-18 12:57:32 -07001076 char *buffPtr;
1077 int64_t packets, bytes;
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001078 int statsFound = 0;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001079
1080 bool filterPair = filter.intIface[0] && filter.extIface[0];
1081
1082 char *filterMsg = filter.getStatsLine();
1083 ALOGV("filter: %s", filterMsg);
1084 free(filterMsg);
1085
1086 stats = filter;
JP Abgralldb7da582011-09-18 12:57:32 -07001087
1088 while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1089 /* Clean up, so a failed parse can still print info */
1090 iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
Mark Salyzynca0b5e22014-03-26 14:15:03 -07001091 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s",
JP Abgralldb7da582011-09-18 12:57:32 -07001092 &packets, &bytes, iface0, iface1, rest);
SynergyDev7776cea2014-03-16 15:48:51 -07001093 ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%" PRId64" bytes=%" PRId64" rest=<%s> orig line=<%s>", res,
JP Abgralldb7da582011-09-18 12:57:32 -07001094 iface0, iface1, packets, bytes, rest, buffPtr);
JP Abgralla2a64f02011-11-11 20:36:16 -08001095 extraProcessingInfo += buffPtr;
1096
JP Abgralldb7da582011-09-18 12:57:32 -07001097 if (res != 5) {
1098 continue;
1099 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001100 /*
1101 * The following assumes that the 1st rule has in:extIface out:intIface,
1102 * which is what NatController sets up.
1103 * If not filtering, the 1st match rx, and sets up the pair for the tx side.
1104 */
1105 if (filter.intIface[0] && filter.extIface[0]) {
1106 if (filter.intIface == iface0 && filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -07001107 ALOGV("2Filter RX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets);
JP Abgrallbaeccc42013-06-25 09:44:10 -07001108 stats.rxPackets = packets;
1109 stats.rxBytes = bytes;
1110 } else if (filter.intIface == iface1 && filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -07001111 ALOGV("2Filter TX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets);
JP Abgrallbaeccc42013-06-25 09:44:10 -07001112 stats.txPackets = packets;
1113 stats.txBytes = bytes;
1114 }
1115 } else if (filter.intIface[0] || filter.extIface[0]) {
1116 if (filter.intIface == iface0 || filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -07001117 ALOGV("1Filter RX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets);
JP Abgrallbaeccc42013-06-25 09:44:10 -07001118 stats.intIface = iface0;
1119 stats.extIface = iface1;
1120 stats.rxPackets = packets;
1121 stats.rxBytes = bytes;
1122 } else if (filter.intIface == iface1 || filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -07001123 ALOGV("1Filter TX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets);
JP Abgrallbaeccc42013-06-25 09:44:10 -07001124 stats.intIface = iface1;
1125 stats.extIface = iface0;
1126 stats.txPackets = packets;
1127 stats.txBytes = bytes;
1128 }
1129 } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ {
1130 if (!stats.intIface[0]) {
SynergyDev7776cea2014-03-16 15:48:51 -07001131 ALOGV("0Filter RX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets);
JP Abgrallbaeccc42013-06-25 09:44:10 -07001132 stats.intIface = iface0;
1133 stats.extIface = iface1;
1134 stats.rxPackets = packets;
1135 stats.rxBytes = bytes;
1136 } else if (stats.intIface == iface1 && stats.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -07001137 ALOGV("0Filter TX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets);
JP Abgrallbaeccc42013-06-25 09:44:10 -07001138 stats.txPackets = packets;
1139 stats.txBytes = bytes;
1140 }
1141 }
1142 if (stats.rxBytes != -1 && stats.txBytes != -1) {
SynergyDev7776cea2014-03-16 15:48:51 -07001143 ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64" filterPair=%d", stats.rxBytes, stats.txBytes, filterPair);
JP Abgrallbaeccc42013-06-25 09:44:10 -07001144 /* Send out stats, and prep for the next if needed. */
1145 char *msg = stats.getStatsLine();
1146 if (filterPair) {
1147 cli->sendMsg(ResponseCode::TetheringStatsResult, msg, false);
1148 return 0;
1149 } else {
1150 cli->sendMsg(ResponseCode::TetheringStatsListResult, msg, false);
1151 stats = filter;
1152 }
1153 free(msg);
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001154 statsFound++;
JP Abgralldb7da582011-09-18 12:57:32 -07001155 }
1156 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001157
1158 /* It is always an error to find only one side of the stats. */
1159 /* It is an error to find nothing when not filtering. */
1160 if (((stats.rxBytes == -1) != (stats.txBytes == -1)) ||
1161 (!statsFound && !filterPair)) {
1162 return -1;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001163 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001164 cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
1165 return 0;
JP Abgralldb7da582011-09-18 12:57:32 -07001166}
1167
JP Abgrallbaeccc42013-06-25 09:44:10 -07001168char *BandwidthController::TetherStats::getStatsLine(void) const {
JP Abgralldb7da582011-09-18 12:57:32 -07001169 char *msg;
SynergyDev7776cea2014-03-16 15:48:51 -07001170 asprintf(&msg, "%s %s %" PRId64" %" PRId64" %" PRId64" %" PRId64, intIface.c_str(), extIface.c_str(),
JP Abgralldb7da582011-09-18 12:57:32 -07001171 rxBytes, rxPackets, txBytes, txPackets);
1172 return msg;
1173}
1174
JP Abgrallbaeccc42013-06-25 09:44:10 -07001175int BandwidthController::getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -07001176 int res;
1177 std::string fullCmd;
1178 FILE *iptOutput;
JP Abgralldb7da582011-09-18 12:57:32 -07001179
JP Abgralldb7da582011-09-18 12:57:32 -07001180 /*
1181 * Why not use some kind of lib to talk to iptables?
1182 * Because the only libs are libiptc and libip6tc in iptables, and they are
1183 * not easy to use. They require the known iptables match modules to be
1184 * preloaded/linked, and require apparently a lot of wrapper code to get
1185 * the wanted info.
1186 */
1187 fullCmd = IPTABLES_PATH;
Yusuke Sato99b40502015-08-19 13:47:30 -07001188 fullCmd += " -nvx -w -L ";
JP Abgrallbaeccc42013-06-25 09:44:10 -07001189 fullCmd += NatController::LOCAL_TETHER_COUNTERS_CHAIN;
JP Abgralldb7da582011-09-18 12:57:32 -07001190 iptOutput = popen(fullCmd.c_str(), "r");
1191 if (!iptOutput) {
Steve Block5ea0c052012-01-06 19:18:11 +00001192 ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
JP Abgralla2a64f02011-11-11 20:36:16 -08001193 extraProcessingInfo += "Failed to run iptables.";
JP Abgralldb7da582011-09-18 12:57:32 -07001194 return -1;
1195 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001196 res = parseForwardChainStats(cli, stats, iptOutput, extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -07001197 pclose(iptOutput);
1198
1199 /* Currently NatController doesn't do ipv6 tethering, so we are done. */
1200 return res;
1201}
JP Abgrall0e540ec2013-08-26 15:13:10 -07001202
1203void BandwidthController::flushExistingCostlyTables(bool doClean) {
JP Abgrall0e540ec2013-08-26 15:13:10 -07001204 std::string fullCmd;
1205 FILE *iptOutput;
JP Abgrall0e540ec2013-08-26 15:13:10 -07001206
1207 /* Only lookup ip4 table names as ip6 will have the same tables ... */
1208 fullCmd = IPTABLES_PATH;
Yusuke Sato99b40502015-08-19 13:47:30 -07001209 fullCmd += " -w -S";
JP Abgrall0e540ec2013-08-26 15:13:10 -07001210 iptOutput = popen(fullCmd.c_str(), "r");
1211 if (!iptOutput) {
1212 ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
1213 return;
1214 }
1215 /* ... then flush/clean both ip4 and ip6 iptables. */
1216 parseAndFlushCostlyTables(iptOutput, doClean);
1217 pclose(iptOutput);
1218}
1219
1220void BandwidthController::parseAndFlushCostlyTables(FILE *fp, bool doRemove) {
1221 int res;
1222 char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1223 char costlyIfaceName[MAX_IPT_OUTPUT_LINE_LEN];
1224 char cmd[MAX_CMD_LEN];
1225 char *buffPtr;
1226
1227 while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1228 costlyIfaceName[0] = '\0'; /* So that debugging output always works */
1229 res = sscanf(buffPtr, "-N bw_costly_%s", costlyIfaceName);
1230 ALOGV("parse res=%d costly=<%s> orig line=<%s>", res,
1231 costlyIfaceName, buffPtr);
1232 if (res != 1) {
1233 continue;
1234 }
1235 /* Exclusions: "shared" is not an ifacename */
1236 if (!strcmp(costlyIfaceName, "shared")) {
1237 continue;
1238 }
1239
1240 snprintf(cmd, sizeof(cmd), "-F bw_costly_%s", costlyIfaceName);
1241 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
1242 if (doRemove) {
1243 snprintf(cmd, sizeof(cmd), "-X bw_costly_%s", costlyIfaceName);
1244 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
1245 }
1246 }
1247}