blob: ae78d95e84967bcb6edda0361242ef6b7a9ebbcb [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
JP Abgralle4788732013-07-02 20:28:45 -0700329int BandwidthController::enableHappyBox(void) {
330 char cmd[MAX_CMD_LEN];
331 int res = 0;
332
333 /*
334 * We tentatively delete before adding, which helps recovering
335 * from bad states (e.g. netd died).
336 */
337
338 /* Should not exist, but ignore result if already there. */
JP Abgrall7e51cde2013-07-03 13:33:05 -0700339 snprintf(cmd, sizeof(cmd), "-N bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700340 runIpxtablesCmd(cmd, IptJumpNoAdd);
341
342 /* Should be empty, but clear in case something was wrong. */
JP Abgrall7e51cde2013-07-03 13:33:05 -0700343 snprintf(cmd, sizeof(cmd), "-F bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700344 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
345
JP Abgrall7e51cde2013-07-03 13:33:05 -0700346 snprintf(cmd, sizeof(cmd), "-D bw_penalty_box -j bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700347 runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall7e51cde2013-07-03 13:33:05 -0700348 snprintf(cmd, sizeof(cmd), "-A bw_penalty_box -j bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700349 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
350
Felipe Leme5ebbbd82016-03-07 09:25:50 -0800351 /* Whitelist all system apps. */
352 snprintf(cmd, sizeof(cmd),
353 "-A bw_happy_box -m owner --uid-owner %d-%d -j RETURN", 0, MAX_SYSTEM_UID);
354 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
355
JP Abgralle4788732013-07-02 20:28:45 -0700356 /* Reject. Defaulting to prot-unreachable */
JP Abgrall7e51cde2013-07-03 13:33:05 -0700357 snprintf(cmd, sizeof(cmd), "-A bw_happy_box -j REJECT");
JP Abgralle4788732013-07-02 20:28:45 -0700358 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
359
360 return res;
361}
362
363int BandwidthController::disableHappyBox(void) {
364 char cmd[MAX_CMD_LEN];
365
366 /* Best effort */
JP Abgrall7e51cde2013-07-03 13:33:05 -0700367 snprintf(cmd, sizeof(cmd), "-D bw_penalty_box -j bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700368 runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall7e51cde2013-07-03 13:33:05 -0700369 snprintf(cmd, sizeof(cmd), "-F bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700370 runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall7e51cde2013-07-03 13:33:05 -0700371 snprintf(cmd, sizeof(cmd), "-X bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700372 runIpxtablesCmd(cmd, IptJumpNoAdd);
373
374 return 0;
375}
376
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700377int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700378 return manipulateNaughtyApps(numUids, appUids, SpecialAppOpAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700379}
380
381int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700382 return manipulateNaughtyApps(numUids, appUids, SpecialAppOpRemove);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700383}
384
JP Abgralle4788732013-07-02 20:28:45 -0700385int BandwidthController::addNiceApps(int numUids, char *appUids[]) {
386 return manipulateNiceApps(numUids, appUids, SpecialAppOpAdd);
387}
388
389int BandwidthController::removeNiceApps(int numUids, char *appUids[]) {
390 return manipulateNiceApps(numUids, appUids, SpecialAppOpRemove);
391}
392
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700393int BandwidthController::manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
Lorenzo Colittib1f05572016-03-18 11:55:56 +0900394 return manipulateSpecialApps(numUids, appStrUids, "bw_penalty_box", IptJumpReject, appOp);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700395}
396
JP Abgralle4788732013-07-02 20:28:45 -0700397int BandwidthController::manipulateNiceApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
Lorenzo Colittib1f05572016-03-18 11:55:56 +0900398 return manipulateSpecialApps(numUids, appStrUids, "bw_happy_box", IptJumpReturn, appOp);
JP Abgralle4788732013-07-02 20:28:45 -0700399}
400
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700401
402int BandwidthController::manipulateSpecialApps(int numUids, char *appStrUids[],
403 const char *chain,
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700404 IptJumpOp jumpHandling, SpecialAppOp appOp) {
405
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700406 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 Abgrall8a932722011-07-13 19:17:35 -0700411
JP Abgrall26e0d492011-06-24 19:21:51 -0700412 switch (appOp) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700413 case SpecialAppOpAdd:
JP Abgrall8a932722011-07-13 19:17:35 -0700414 op = IptOpInsert;
JP Abgrallaf476f72013-07-03 12:23:55 -0700415 failLogTemplate = "Failed to add app uid %s(%d) to %s.";
JP Abgrall8a932722011-07-13 19:17:35 -0700416 break;
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700417 case SpecialAppOpRemove:
JP Abgrall8a932722011-07-13 19:17:35 -0700418 op = IptOpDelete;
JP Abgrallaf476f72013-07-03 12:23:55 -0700419 failLogTemplate = "Failed to delete app uid %s(%d) from %s box.";
JP Abgrall8a932722011-07-13 19:17:35 -0700420 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700421 default:
422 ALOGE("Unexpected app Op %d", appOp);
423 return -1;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700424 }
425
426 for (uidNum = 0; uidNum < numUids; uidNum++) {
JP Abgrallaf476f72013-07-03 12:23:55 -0700427 char *end;
428 appUids[uidNum] = strtoul(appStrUids[uidNum], &end, 0);
429 if (*end || !*appStrUids[uidNum]) {
430 ALOGE(failLogTemplate, appStrUids[uidNum], 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 Abgrallb1d24092012-04-27 01:02:31 -0700437
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700438 iptCmd = makeIptablesSpecialAppCmd(op, uid, chain);
439 if (runIpxtablesCmd(iptCmd.c_str(), jumpHandling)) {
JP Abgrallaf476f72013-07-03 12:23:55 -0700440 ALOGE(failLogTemplate, appStrUids[uidNum], uid, chain);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700441 goto fail_with_uidNum;
442 }
443 }
444 return 0;
445
JP Abgrall26e0d492011-06-24 19:21:51 -0700446fail_with_uidNum:
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700447 /* Try to remove the uid that failed in any case*/
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700448 iptCmd = makeIptablesSpecialAppCmd(IptOpDelete, appUids[uidNum], chain);
449 runIpxtablesCmd(iptCmd.c_str(), jumpHandling);
JP Abgrall26e0d492011-06-24 19:21:51 -0700450fail_parse:
451 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700452}
453
JP Abgrall26e0d492011-06-24 19:21:51 -0700454std::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700455 std::string res;
JP Abgrall8a932722011-07-13 19:17:35 -0700456 char *buff;
457 const char *opFlag;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700458
SynergyDev7776cea2014-03-16 15:48:51 -0700459 ALOGV("makeIptablesQuotaCmd(%d, %" PRId64")", op, quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700460
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700461 switch (op) {
JP Abgrall8a932722011-07-13 19:17:35 -0700462 case IptOpInsert:
463 opFlag = "-I";
464 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800465 case IptOpAppend:
466 opFlag = "-A";
467 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700468 case IptOpReplace:
469 opFlag = "-R";
470 break;
471 default:
472 case IptOpDelete:
473 opFlag = "-D";
474 break;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700475 }
JP Abgrall8a932722011-07-13 19:17:35 -0700476
JP Abgrallbfa74662011-06-29 19:23:04 -0700477 // The requried IP version specific --jump REJECT ... will be added later.
SynergyDev7776cea2014-03-16 15:48:51 -0700478 asprintf(&buff, "%s bw_costly_%s -m quota2 ! --quota %" PRId64" --name %s", opFlag, costName, quota,
JP Abgrall8a932722011-07-13 19:17:35 -0700479 costName);
480 res = buff;
481 free(buff);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700482 return res;
483}
484
JP Abgrall26e0d492011-06-24 19:21:51 -0700485int BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700486 char cmd[MAX_CMD_LEN];
JP Abgrall0031cea2012-04-17 16:38:23 -0700487 int res = 0, res1, res2;
JP Abgrall8a932722011-07-13 19:17:35 -0700488 int ruleInsertPos = 1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700489 std::string costString;
490 const char *costCString;
491
JP Abgrall0dad7c22011-06-24 11:58:14 -0700492 /* The "-N costly" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700493 switch (quotaType) {
494 case QuotaUnique:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700495 costString = "bw_costly_";
JP Abgrall0dad7c22011-06-24 11:58:14 -0700496 costString += ifn;
497 costCString = costString.c_str();
JP Abgrall0031cea2012-04-17 16:38:23 -0700498 /*
JP Abgrall7e51cde2013-07-03 13:33:05 -0700499 * Flush the bw_costly_<iface> is allowed to fail in case it didn't exist.
JP Abgrall0031cea2012-04-17 16:38:23 -0700500 * Creating a new one is allowed to fail in case it existed.
501 * This helps with netd restarts.
502 */
503 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700504 res1 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700505 snprintf(cmd, sizeof(cmd), "-N %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700506 res2 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700507 res = (res1 && res2) || (!res1 && !res2);
508
JP Abgrall7e51cde2013-07-03 13:33:05 -0700509 snprintf(cmd, sizeof(cmd), "-A %s -j bw_penalty_box", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700510 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall26e0d492011-06-24 19:21:51 -0700511 break;
512 case QuotaShared:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700513 costCString = "bw_costly_shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700514 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700515 default:
516 ALOGE("Unexpected quotatype %d", quotaType);
517 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700518 }
519
JP Abgrall8a932722011-07-13 19:17:35 -0700520 if (globalAlertBytes) {
521 /* The alert rule comes 1st */
522 ruleInsertPos = 2;
523 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700524
525 snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700526 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700527
528 snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700529 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0031cea2012-04-17 16:38:23 -0700530
531 snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700532 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700533
534 snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700535 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
Erik Kline58a94482015-10-02 17:52:37 +0900536
537 snprintf(cmd, sizeof(cmd), "-D bw_FORWARD -o %s --jump %s", ifn, costCString);
538 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
539 snprintf(cmd, sizeof(cmd), "-A bw_FORWARD -o %s --jump %s", ifn, costCString);
540 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
541
JP Abgrall0dad7c22011-06-24 11:58:14 -0700542 return res;
543}
544
JP Abgrall26e0d492011-06-24 19:21:51 -0700545int BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700546 char cmd[MAX_CMD_LEN];
547 int res = 0;
548 std::string costString;
549 const char *costCString;
550
JP Abgrall26e0d492011-06-24 19:21:51 -0700551 switch (quotaType) {
552 case QuotaUnique:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700553 costString = "bw_costly_";
JP Abgrall0dad7c22011-06-24 11:58:14 -0700554 costString += ifn;
555 costCString = costString.c_str();
JP Abgrall26e0d492011-06-24 19:21:51 -0700556 break;
557 case QuotaShared:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700558 costCString = "bw_costly_shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700559 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700560 default:
561 ALOGE("Unexpected quotatype %d", quotaType);
562 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700563 }
564
JP Abgrall0031cea2012-04-17 16:38:23 -0700565 snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700566 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
Erik Kline58a94482015-10-02 17:52:37 +0900567 for (const auto tableName : {LOCAL_OUTPUT, LOCAL_FORWARD}) {
568 snprintf(cmd, sizeof(cmd), "-D %s -o %s --jump %s", tableName, ifn, costCString);
569 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
570 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700571
JP Abgrall7e51cde2013-07-03 13:33:05 -0700572 /* The "-N bw_costly_shared" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700573 if (quotaType == QuotaUnique) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700574 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700575 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgralla9f802c2011-06-29 15:46:45 -0700576 snprintf(cmd, sizeof(cmd), "-X %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700577 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700578 }
579 return res;
580}
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700581
JP Abgrall0dad7c22011-06-24 11:58:14 -0700582int BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700583 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700584 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700585 std::string quotaCmd;
JP Abgrall8a932722011-07-13 19:17:35 -0700586 std::string ifaceName;
587 ;
JP Abgrallbfa74662011-06-29 19:23:04 -0700588 const char *costName = "shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700589 std::list<std::string>::iterator it;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700590
JP Abgrall8a932722011-07-13 19:17:35 -0700591 if (!maxBytes) {
592 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000593 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700594 return -1;
595 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700596 if (!isIfaceName(iface))
597 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700598 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000599 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700600 return -1;
601 }
602 ifaceName = ifn;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700603
604 if (maxBytes == -1) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700605 return removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700606 }
607
608 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700609 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
610 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700611 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700612 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700613
JP Abgrall0dad7c22011-06-24 11:58:14 -0700614 if (it == sharedQuotaIfaces.end()) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700615 res |= prepCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700616 if (sharedQuotaIfaces.empty()) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700617 quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700618 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700619 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000620 ALOGE("Failed set quota rule");
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700621 goto fail;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700622 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700623 sharedQuotaBytes = maxBytes;
624 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700625 sharedQuotaIfaces.push_front(ifaceName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700626
627 }
628
629 if (maxBytes != sharedQuotaBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700630 res |= updateQuota(costName, maxBytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700631 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000632 ALOGE("Failed update quota for %s", costName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700633 goto fail;
634 }
635 sharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700636 }
637 return 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700638
639 fail:
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700640 /*
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700641 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
642 * rules in the kernel to see which ones need cleaning up.
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700643 * For now callers needs to choose if they want to "ndc bandwidth enable"
644 * which resets everything.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700645 */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700646 removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700647 return -1;
648}
649
JP Abgrall8a932722011-07-13 19:17:35 -0700650/* It will also cleanup any shared alerts */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700651int BandwidthController::removeInterfaceSharedQuota(const char *iface) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700652 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700653 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700654 std::string ifaceName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700655 std::list<std::string>::iterator it;
JP Abgrallbfa74662011-06-29 19:23:04 -0700656 const char *costName = "shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700657
JP Abgrall69261cb2014-06-19 18:35:24 -0700658 if (!isIfaceName(iface))
659 return -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700660 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000661 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700662 return -1;
663 }
JP Abgrall8a932722011-07-13 19:17:35 -0700664 ifaceName = ifn;
JP Abgrall26e0d492011-06-24 19:21:51 -0700665
JP Abgrall0dad7c22011-06-24 11:58:14 -0700666 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
667 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700668 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700669 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700670 if (it == sharedQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000671 ALOGE("No such iface %s to delete", ifn);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700672 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700673 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700674
JP Abgrall26e0d492011-06-24 19:21:51 -0700675 res |= cleanupCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700676 sharedQuotaIfaces.erase(it);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700677
JP Abgrall0dad7c22011-06-24 11:58:14 -0700678 if (sharedQuotaIfaces.empty()) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700679 std::string quotaCmd;
JP Abgrallbfa74662011-06-29 19:23:04 -0700680 quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700681 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall8a932722011-07-13 19:17:35 -0700682 sharedQuotaBytes = 0;
683 if (sharedAlertBytes) {
684 removeSharedAlert();
685 sharedAlertBytes = 0;
686 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700687 }
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700688 return res;
689}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700690
691int BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
692 char ifn[MAX_IFACENAME_LEN];
693 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700694 std::string ifaceName;
695 const char *costName;
696 std::list<QuotaInfo>::iterator it;
697 std::string quotaCmd;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700698
JP Abgrall69261cb2014-06-19 18:35:24 -0700699 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700700 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700701
JP Abgrall8a932722011-07-13 19:17:35 -0700702 if (!maxBytes) {
703 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000704 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700705 return -1;
706 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700707 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700708 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700709 }
710
JP Abgrall8a932722011-07-13 19:17:35 -0700711 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000712 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700713 return -1;
714 }
715 ifaceName = ifn;
716 costName = iface;
717
JP Abgrall0dad7c22011-06-24 11:58:14 -0700718 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700719 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
JP Abgrall8a932722011-07-13 19:17:35 -0700720 if (it->ifaceName == ifaceName)
JP Abgrall0dad7c22011-06-24 11:58:14 -0700721 break;
722 }
723
724 if (it == quotaIfaces.end()) {
JP Abgralle4788732013-07-02 20:28:45 -0700725 /* Preparing the iface adds a penalty/happy box check */
JP Abgrall26e0d492011-06-24 19:21:51 -0700726 res |= prepCostlyIface(ifn, QuotaUnique);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700727 /*
JP Abgralle4788732013-07-02 20:28:45 -0700728 * The rejecting quota limit should go after the penalty/happy box checks
JP Abgrallbaeccc42013-06-25 09:44:10 -0700729 * or else a naughty app could just eat up the quota.
730 * So we append here.
731 */
JP Abgrall109899b2013-02-12 19:20:13 -0800732 quotaCmd = makeIptablesQuotaCmd(IptOpAppend, costName, maxBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700733 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700734 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000735 ALOGE("Failed set quota rule");
JP Abgrall0dad7c22011-06-24 11:58:14 -0700736 goto fail;
737 }
738
JP Abgrall8a932722011-07-13 19:17:35 -0700739 quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
JP Abgrall0dad7c22011-06-24 11:58:14 -0700740
741 } else {
JP Abgrall8a932722011-07-13 19:17:35 -0700742 res |= updateQuota(costName, maxBytes);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700743 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000744 ALOGE("Failed update quota for %s", iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700745 goto fail;
746 }
JP Abgrall8a932722011-07-13 19:17:35 -0700747 it->quota = maxBytes;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700748 }
749 return 0;
750
751 fail:
752 /*
753 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
754 * rules in the kernel to see which ones need cleaning up.
755 * For now callers needs to choose if they want to "ndc bandwidth enable"
756 * which resets everything.
757 */
758 removeInterfaceSharedQuota(ifn);
759 return -1;
760}
761
JP Abgrall8a932722011-07-13 19:17:35 -0700762int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
763 return getInterfaceQuota("shared", bytes);
764}
765
766int BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
767 FILE *fp;
768 char *fname;
769 int scanRes;
770
JP Abgrall69261cb2014-06-19 18:35:24 -0700771 if (!isIfaceName(costName))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700772 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700773
JP Abgrall8a932722011-07-13 19:17:35 -0700774 asprintf(&fname, "/proc/net/xt_quota/%s", costName);
Nick Kralevich53ea9ca2015-01-31 13:54:00 -0800775 fp = fopen(fname, "re");
JP Abgrall8a932722011-07-13 19:17:35 -0700776 free(fname);
777 if (!fp) {
Steve Block5ea0c052012-01-06 19:18:11 +0000778 ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
JP Abgrall8a932722011-07-13 19:17:35 -0700779 return -1;
780 }
Mark Salyzynca0b5e22014-03-26 14:15:03 -0700781 scanRes = fscanf(fp, "%" SCNd64, bytes);
SynergyDev7776cea2014-03-16 15:48:51 -0700782 ALOGV("Read quota res=%d bytes=%" PRId64, scanRes, *bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700783 fclose(fp);
784 return scanRes == 1 ? 0 : -1;
785}
786
JP Abgrall0dad7c22011-06-24 11:58:14 -0700787int BandwidthController::removeInterfaceQuota(const char *iface) {
788
789 char ifn[MAX_IFACENAME_LEN];
790 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700791 std::string ifaceName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700792 std::list<QuotaInfo>::iterator it;
JP Abgrall26e0d492011-06-24 19:21:51 -0700793
JP Abgrall69261cb2014-06-19 18:35:24 -0700794 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700795 return -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700796 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000797 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700798 return -1;
799 }
800 ifaceName = ifn;
JP Abgrall26e0d492011-06-24 19:21:51 -0700801
JP Abgrall0dad7c22011-06-24 11:58:14 -0700802 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
JP Abgrall8a932722011-07-13 19:17:35 -0700803 if (it->ifaceName == ifaceName)
JP Abgrall0dad7c22011-06-24 11:58:14 -0700804 break;
805 }
806
807 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000808 ALOGE("No such iface %s to delete", ifn);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700809 return -1;
810 }
811
812 /* This also removes the quota command of CostlyIface chain. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700813 res |= cleanupCostlyIface(ifn, QuotaUnique);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700814
815 quotaIfaces.erase(it);
816
817 return res;
818}
JP Abgrall8a932722011-07-13 19:17:35 -0700819
820int BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
821 FILE *fp;
822 char *fname;
823
JP Abgrall69261cb2014-06-19 18:35:24 -0700824 if (!isIfaceName(quotaName)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700825 ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName);
826 return -1;
827 }
828
JP Abgrall8a932722011-07-13 19:17:35 -0700829 asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
Nick Kralevich53ea9ca2015-01-31 13:54:00 -0800830 fp = fopen(fname, "we");
JP Abgrall8a932722011-07-13 19:17:35 -0700831 free(fname);
832 if (!fp) {
Steve Block5ea0c052012-01-06 19:18:11 +0000833 ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
JP Abgrall8a932722011-07-13 19:17:35 -0700834 return -1;
835 }
SynergyDev7776cea2014-03-16 15:48:51 -0700836 fprintf(fp, "%" PRId64"\n", bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700837 fclose(fp);
838 return 0;
839}
840
841int BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
842 int res = 0;
843 const char *opFlag;
844 char *alertQuotaCmd;
845
846 switch (op) {
847 case IptOpInsert:
848 opFlag = "-I";
849 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800850 case IptOpAppend:
851 opFlag = "-A";
852 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700853 case IptOpReplace:
854 opFlag = "-R";
855 break;
856 default:
857 case IptOpDelete:
858 opFlag = "-D";
859 break;
860 }
861
JP Abgrall92009c82013-02-06 18:01:24 -0800862 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800863 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700864 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700865 free(alertQuotaCmd);
JP Abgrall92009c82013-02-06 18:01:24 -0800866 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800867 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700868 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700869 free(alertQuotaCmd);
870 return res;
871}
872
JP Abgrallc6c67342011-10-07 16:28:54 -0700873int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
874 int res = 0;
875 const char *opFlag;
JP Abgrall8a932722011-07-13 19:17:35 -0700876 char *alertQuotaCmd;
JP Abgrallc6c67342011-10-07 16:28:54 -0700877
878 switch (op) {
879 case IptOpInsert:
880 opFlag = "-I";
881 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800882 case IptOpAppend:
883 opFlag = "-A";
884 break;
JP Abgrallc6c67342011-10-07 16:28:54 -0700885 case IptOpReplace:
886 opFlag = "-R";
887 break;
888 default:
889 case IptOpDelete:
890 opFlag = "-D";
891 break;
892 }
893
JP Abgrall92009c82013-02-06 18:01:24 -0800894 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800895 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700896 res = runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrallc6c67342011-10-07 16:28:54 -0700897 free(alertQuotaCmd);
898 return res;
899}
900
901int BandwidthController::setGlobalAlert(int64_t bytes) {
902 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700903 int res = 0;
904
905 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000906 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700907 return -1;
908 }
909 if (globalAlertBytes) {
910 res = updateQuota(alertName, bytes);
911 } else {
912 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700913 if (globalAlertTetherCount) {
Steve Block3fb42e02011-10-20 11:55:56 +0100914 ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700915 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
916 }
JP Abgrall8a932722011-07-13 19:17:35 -0700917 }
918 globalAlertBytes = bytes;
919 return res;
920}
921
JP Abgrallc6c67342011-10-07 16:28:54 -0700922int BandwidthController::setGlobalAlertInForwardChain(void) {
923 const char *alertName = ALERT_GLOBAL_NAME;
924 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700925
JP Abgrallc6c67342011-10-07 16:28:54 -0700926 globalAlertTetherCount++;
Steve Block3fb42e02011-10-20 11:55:56 +0100927 ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700928
929 /*
930 * If there is no globalAlert active we are done.
931 * If there is an active globalAlert but this is not the 1st
932 * tether, we are also done.
933 */
934 if (!globalAlertBytes || globalAlertTetherCount != 1) {
935 return 0;
936 }
937
938 /* We only add the rule if this was the 1st tether added. */
939 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
940 return res;
941}
942
943int BandwidthController::removeGlobalAlert(void) {
944
945 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700946 int res = 0;
947
948 if (!globalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000949 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700950 return -1;
951 }
952 res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700953 if (globalAlertTetherCount) {
954 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
955 }
JP Abgrall8a932722011-07-13 19:17:35 -0700956 globalAlertBytes = 0;
957 return res;
958}
959
JP Abgrallc6c67342011-10-07 16:28:54 -0700960int BandwidthController::removeGlobalAlertInForwardChain(void) {
961 int res = 0;
962 const char *alertName = ALERT_GLOBAL_NAME;
963
964 if (!globalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000965 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700966 return -1;
967 }
968
969 globalAlertTetherCount--;
970 /*
971 * If there is no globalAlert active we are done.
972 * If there is an active globalAlert but there are more
973 * tethers, we are also done.
974 */
975 if (!globalAlertBytes || globalAlertTetherCount >= 1) {
976 return 0;
977 }
978
979 /* We only detete the rule if this was the last tether removed. */
980 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
981 return res;
982}
983
JP Abgrall8a932722011-07-13 19:17:35 -0700984int BandwidthController::setSharedAlert(int64_t bytes) {
985 if (!sharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000986 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700987 return -1;
988 }
989 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000990 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700991 return -1;
992 }
993 return setCostlyAlert("shared", bytes, &sharedAlertBytes);
994}
995
996int BandwidthController::removeSharedAlert(void) {
997 return removeCostlyAlert("shared", &sharedAlertBytes);
998}
999
1000int BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
1001 std::list<QuotaInfo>::iterator it;
1002
JP Abgrall69261cb2014-06-19 18:35:24 -07001003 if (!isIfaceName(iface)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -07001004 ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface);
1005 return -1;
1006 }
1007
JP Abgrall8a932722011-07-13 19:17:35 -07001008 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001009 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -07001010 return -1;
1011 }
1012 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
1013 if (it->ifaceName == iface)
1014 break;
1015 }
1016
1017 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +00001018 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -07001019 return -1;
1020 }
1021
1022 return setCostlyAlert(iface, bytes, &it->alert);
1023}
1024
1025int BandwidthController::removeInterfaceAlert(const char *iface) {
1026 std::list<QuotaInfo>::iterator it;
1027
JP Abgrall69261cb2014-06-19 18:35:24 -07001028 if (!isIfaceName(iface)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -07001029 ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface);
1030 return -1;
1031 }
1032
JP Abgrall8a932722011-07-13 19:17:35 -07001033 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
1034 if (it->ifaceName == iface)
1035 break;
1036 }
1037
1038 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +00001039 ALOGE("No prior alert set for interface %s", iface);
JP Abgrall8a932722011-07-13 19:17:35 -07001040 return -1;
1041 }
1042
1043 return removeCostlyAlert(iface, &it->alert);
1044}
1045
1046int BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
1047 char *alertQuotaCmd;
JP Abgrall109899b2013-02-12 19:20:13 -08001048 char *chainName;
JP Abgrall8a932722011-07-13 19:17:35 -07001049 int res = 0;
1050 char *alertName;
1051
JP Abgrall69261cb2014-06-19 18:35:24 -07001052 if (!isIfaceName(costName)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -07001053 ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName);
1054 return -1;
1055 }
1056
JP Abgrall8a932722011-07-13 19:17:35 -07001057 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001058 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -07001059 return -1;
1060 }
1061 asprintf(&alertName, "%sAlert", costName);
1062 if (*alertBytes) {
1063 res = updateQuota(alertName, *alertBytes);
1064 } else {
JP Abgrall7e51cde2013-07-03 13:33:05 -07001065 asprintf(&chainName, "bw_costly_%s", costName);
JP Abgrall109899b2013-02-12 19:20:13 -08001066 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -07001067 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -07001068 free(alertQuotaCmd);
JP Abgrall109899b2013-02-12 19:20:13 -08001069 free(chainName);
JP Abgrall8a932722011-07-13 19:17:35 -07001070 }
1071 *alertBytes = bytes;
1072 free(alertName);
1073 return res;
1074}
1075
1076int BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
1077 char *alertQuotaCmd;
1078 char *chainName;
1079 char *alertName;
1080 int res = 0;
1081
JP Abgrall69261cb2014-06-19 18:35:24 -07001082 if (!isIfaceName(costName)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -07001083 ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName);
1084 return -1;
1085 }
1086
JP Abgrall8a932722011-07-13 19:17:35 -07001087 if (!*alertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001088 ALOGE("No prior alert set for %s alert", costName);
JP Abgrall8a932722011-07-13 19:17:35 -07001089 return -1;
1090 }
1091
Jesper Hanssona9d791f2012-04-27 13:54:27 +02001092 asprintf(&alertName, "%sAlert", costName);
JP Abgrall7e51cde2013-07-03 13:33:05 -07001093 asprintf(&chainName, "bw_costly_%s", costName);
JP Abgrall92009c82013-02-06 18:01:24 -08001094 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -07001095 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -07001096 free(alertQuotaCmd);
1097 free(chainName);
1098
1099 *alertBytes = 0;
1100 free(alertName);
1101 return res;
1102}
JP Abgralldb7da582011-09-18 12:57:32 -07001103
1104/*
1105 * Parse the ptks and bytes out of:
JP Abgrallbaeccc42013-06-25 09:44:10 -07001106 * Chain natctrl_tether_counters (4 references)
1107 * pkts bytes target prot opt in out source destination
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001108 * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
1109 * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0
1110 * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0
1111 * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0
1112 * It results in an error if invoked and no tethering counter rules exist. The constraint
1113 * helps detect complete parsing failure.
JP Abgralldb7da582011-09-18 12:57:32 -07001114 */
JP Abgrallbaeccc42013-06-25 09:44:10 -07001115int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherStats filter,
1116 FILE *fp, std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -07001117 int res;
1118 char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1119 char iface0[MAX_IPT_OUTPUT_LINE_LEN];
1120 char iface1[MAX_IPT_OUTPUT_LINE_LEN];
1121 char rest[MAX_IPT_OUTPUT_LINE_LEN];
1122
JP Abgrallbaeccc42013-06-25 09:44:10 -07001123 TetherStats stats;
JP Abgralldb7da582011-09-18 12:57:32 -07001124 char *buffPtr;
1125 int64_t packets, bytes;
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001126 int statsFound = 0;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001127
1128 bool filterPair = filter.intIface[0] && filter.extIface[0];
1129
1130 char *filterMsg = filter.getStatsLine();
1131 ALOGV("filter: %s", filterMsg);
1132 free(filterMsg);
1133
1134 stats = filter;
JP Abgralldb7da582011-09-18 12:57:32 -07001135
1136 while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1137 /* Clean up, so a failed parse can still print info */
1138 iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
Mark Salyzynca0b5e22014-03-26 14:15:03 -07001139 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s",
JP Abgralldb7da582011-09-18 12:57:32 -07001140 &packets, &bytes, iface0, iface1, rest);
SynergyDev7776cea2014-03-16 15:48:51 -07001141 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 -07001142 iface0, iface1, packets, bytes, rest, buffPtr);
JP Abgralla2a64f02011-11-11 20:36:16 -08001143 extraProcessingInfo += buffPtr;
1144
JP Abgralldb7da582011-09-18 12:57:32 -07001145 if (res != 5) {
1146 continue;
1147 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001148 /*
1149 * The following assumes that the 1st rule has in:extIface out:intIface,
1150 * which is what NatController sets up.
1151 * If not filtering, the 1st match rx, and sets up the pair for the tx side.
1152 */
1153 if (filter.intIface[0] && filter.extIface[0]) {
1154 if (filter.intIface == iface0 && filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -07001155 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 -07001156 stats.rxPackets = packets;
1157 stats.rxBytes = bytes;
1158 } else if (filter.intIface == iface1 && filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -07001159 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 -07001160 stats.txPackets = packets;
1161 stats.txBytes = bytes;
1162 }
1163 } else if (filter.intIface[0] || filter.extIface[0]) {
1164 if (filter.intIface == iface0 || filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -07001165 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 -07001166 stats.intIface = iface0;
1167 stats.extIface = iface1;
1168 stats.rxPackets = packets;
1169 stats.rxBytes = bytes;
1170 } else if (filter.intIface == iface1 || filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -07001171 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 -07001172 stats.intIface = iface1;
1173 stats.extIface = iface0;
1174 stats.txPackets = packets;
1175 stats.txBytes = bytes;
1176 }
1177 } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ {
1178 if (!stats.intIface[0]) {
SynergyDev7776cea2014-03-16 15:48:51 -07001179 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 -07001180 stats.intIface = iface0;
1181 stats.extIface = iface1;
1182 stats.rxPackets = packets;
1183 stats.rxBytes = bytes;
1184 } else if (stats.intIface == iface1 && stats.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -07001185 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 -07001186 stats.txPackets = packets;
1187 stats.txBytes = bytes;
1188 }
1189 }
1190 if (stats.rxBytes != -1 && stats.txBytes != -1) {
SynergyDev7776cea2014-03-16 15:48:51 -07001191 ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64" filterPair=%d", stats.rxBytes, stats.txBytes, filterPair);
JP Abgrallbaeccc42013-06-25 09:44:10 -07001192 /* Send out stats, and prep for the next if needed. */
1193 char *msg = stats.getStatsLine();
1194 if (filterPair) {
1195 cli->sendMsg(ResponseCode::TetheringStatsResult, msg, false);
1196 return 0;
1197 } else {
1198 cli->sendMsg(ResponseCode::TetheringStatsListResult, msg, false);
1199 stats = filter;
1200 }
1201 free(msg);
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001202 statsFound++;
JP Abgralldb7da582011-09-18 12:57:32 -07001203 }
1204 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001205
1206 /* It is always an error to find only one side of the stats. */
1207 /* It is an error to find nothing when not filtering. */
1208 if (((stats.rxBytes == -1) != (stats.txBytes == -1)) ||
1209 (!statsFound && !filterPair)) {
1210 return -1;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001211 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001212 cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
1213 return 0;
JP Abgralldb7da582011-09-18 12:57:32 -07001214}
1215
JP Abgrallbaeccc42013-06-25 09:44:10 -07001216char *BandwidthController::TetherStats::getStatsLine(void) const {
JP Abgralldb7da582011-09-18 12:57:32 -07001217 char *msg;
SynergyDev7776cea2014-03-16 15:48:51 -07001218 asprintf(&msg, "%s %s %" PRId64" %" PRId64" %" PRId64" %" PRId64, intIface.c_str(), extIface.c_str(),
JP Abgralldb7da582011-09-18 12:57:32 -07001219 rxBytes, rxPackets, txBytes, txPackets);
1220 return msg;
1221}
1222
JP Abgrallbaeccc42013-06-25 09:44:10 -07001223int BandwidthController::getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -07001224 int res;
1225 std::string fullCmd;
1226 FILE *iptOutput;
JP Abgralldb7da582011-09-18 12:57:32 -07001227
JP Abgralldb7da582011-09-18 12:57:32 -07001228 /*
1229 * Why not use some kind of lib to talk to iptables?
1230 * Because the only libs are libiptc and libip6tc in iptables, and they are
1231 * not easy to use. They require the known iptables match modules to be
1232 * preloaded/linked, and require apparently a lot of wrapper code to get
1233 * the wanted info.
1234 */
1235 fullCmd = IPTABLES_PATH;
Yusuke Sato99b40502015-08-19 13:47:30 -07001236 fullCmd += " -nvx -w -L ";
JP Abgrallbaeccc42013-06-25 09:44:10 -07001237 fullCmd += NatController::LOCAL_TETHER_COUNTERS_CHAIN;
JP Abgralldb7da582011-09-18 12:57:32 -07001238 iptOutput = popen(fullCmd.c_str(), "r");
1239 if (!iptOutput) {
Steve Block5ea0c052012-01-06 19:18:11 +00001240 ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
JP Abgralla2a64f02011-11-11 20:36:16 -08001241 extraProcessingInfo += "Failed to run iptables.";
JP Abgralldb7da582011-09-18 12:57:32 -07001242 return -1;
1243 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001244 res = parseForwardChainStats(cli, stats, iptOutput, extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -07001245 pclose(iptOutput);
1246
1247 /* Currently NatController doesn't do ipv6 tethering, so we are done. */
1248 return res;
1249}
JP Abgrall0e540ec2013-08-26 15:13:10 -07001250
1251void BandwidthController::flushExistingCostlyTables(bool doClean) {
JP Abgrall0e540ec2013-08-26 15:13:10 -07001252 std::string fullCmd;
1253 FILE *iptOutput;
JP Abgrall0e540ec2013-08-26 15:13:10 -07001254
1255 /* Only lookup ip4 table names as ip6 will have the same tables ... */
1256 fullCmd = IPTABLES_PATH;
Yusuke Sato99b40502015-08-19 13:47:30 -07001257 fullCmd += " -w -S";
JP Abgrall0e540ec2013-08-26 15:13:10 -07001258 iptOutput = popen(fullCmd.c_str(), "r");
1259 if (!iptOutput) {
1260 ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
1261 return;
1262 }
1263 /* ... then flush/clean both ip4 and ip6 iptables. */
1264 parseAndFlushCostlyTables(iptOutput, doClean);
1265 pclose(iptOutput);
1266}
1267
1268void BandwidthController::parseAndFlushCostlyTables(FILE *fp, bool doRemove) {
1269 int res;
1270 char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1271 char costlyIfaceName[MAX_IPT_OUTPUT_LINE_LEN];
1272 char cmd[MAX_CMD_LEN];
1273 char *buffPtr;
1274
1275 while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1276 costlyIfaceName[0] = '\0'; /* So that debugging output always works */
1277 res = sscanf(buffPtr, "-N bw_costly_%s", costlyIfaceName);
1278 ALOGV("parse res=%d costly=<%s> orig line=<%s>", res,
1279 costlyIfaceName, buffPtr);
1280 if (res != 1) {
1281 continue;
1282 }
1283 /* Exclusions: "shared" is not an ifacename */
1284 if (!strcmp(costlyIfaceName, "shared")) {
1285 continue;
1286 }
1287
1288 snprintf(cmd, sizeof(cmd), "-F bw_costly_%s", costlyIfaceName);
1289 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
1290 if (doRemove) {
1291 snprintf(cmd, sizeof(cmd), "-X bw_costly_%s", costlyIfaceName);
1292 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
1293 }
1294 }
1295}