blob: 51bb29f10c0ad2dd21915f4a1f9a509e87544583 [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
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090044#include "android-base/stringprintf.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070045#define LOG_TAG "BandwidthController"
46#include <cutils/log.h>
47#include <cutils/properties.h>
Rom Lemarchand14150212013-01-24 10:01:04 -080048#include <logwrap/logwrap.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070049
JP Abgrall0031cea2012-04-17 16:38:23 -070050#include "NetdConstants.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070051#include "BandwidthController.h"
JP Abgrallbaeccc42013-06-25 09:44:10 -070052#include "NatController.h" /* For LOCAL_TETHER_COUNTERS_CHAIN */
53#include "ResponseCode.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070054
JP Abgralldb7da582011-09-18 12:57:32 -070055/* Alphabetical */
SynergyDev7776cea2014-03-16 15:48:51 -070056#define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %" PRId64" --name %s"
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";
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090062
Lorenzo Colitti86a47982016-03-18 17:52:25 +090063auto BandwidthController::execFunction = android_fork_execvp;
64auto BandwidthController::popenFunction = popen;
65
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090066namespace {
67
68const char ALERT_GLOBAL_NAME[] = "globalAlert";
69const int MAX_CMD_ARGS = 32;
70const int MAX_CMD_LEN = 1024;
71const int MAX_IFACENAME_LEN = 64;
72const int MAX_IPT_OUTPUT_LINE_LEN = 256;
JP Abgralldb7da582011-09-18 12:57:32 -070073
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070074/**
75 * Some comments about the rules:
76 * * Ordering
77 * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
JP Abgrall29e8de22012-05-03 12:52:15 -070078 * E.g. "-I bw_INPUT -i rmnet0 --jump costly"
JP Abgrall7e51cde2013-07-03 13:33:05 -070079 * - quota'd rules in the costly chain should be before bw_penalty_box lookups.
JP Abgrall29e8de22012-05-03 12:52:15 -070080 * - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070081 *
82 * * global quota vs per interface quota
83 * - global quota for all costly interfaces uses a single costly chain:
84 * . initial rules
JP Abgrall7e51cde2013-07-03 13:33:05 -070085 * iptables -N bw_costly_shared
86 * iptables -I bw_INPUT -i iface0 --jump bw_costly_shared
87 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_shared
88 * iptables -I bw_costly_shared -m quota \! --quota 500000 \
JP Abgrallbfa74662011-06-29 19:23:04 -070089 * --jump REJECT --reject-with icmp-net-prohibited
JP Abgrall7e51cde2013-07-03 13:33:05 -070090 * iptables -A bw_costly_shared --jump bw_penalty_box
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090091 * iptables -A bw_penalty_box --jump bw_happy_box
Lorenzo Colitti464eabe2016-03-25 13:38:19 +090092 * iptables -A bw_happy_box --jump bw_data_saver
JP Abgrall8a932722011-07-13 19:17:35 -070093 *
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070094 * . adding a new iface to this, E.g.:
JP Abgrall7e51cde2013-07-03 13:33:05 -070095 * iptables -I bw_INPUT -i iface1 --jump bw_costly_shared
96 * iptables -I bw_OUTPUT -o iface1 --jump bw_costly_shared
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070097 *
98 * - quota per interface. This is achieve by having "costly" chains per quota.
99 * E.g. adding a new costly interface iface0 with its own quota:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700100 * iptables -N bw_costly_iface0
101 * iptables -I bw_INPUT -i iface0 --jump bw_costly_iface0
102 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_iface0
103 * iptables -A bw_costly_iface0 -m quota \! --quota 500000 \
JP Abgralle4788732013-07-02 20:28:45 -0700104 * --jump REJECT --reject-with icmp-port-unreachable
JP Abgrall7e51cde2013-07-03 13:33:05 -0700105 * iptables -A bw_costly_iface0 --jump bw_penalty_box
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700106 *
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900107 * * Penalty box, happy box and data saver.
108 * - bw_penalty box is a blacklist of apps that are rejected.
109 * - bw_happy_box is a whitelist of apps. It always includes all system apps
110 * - bw_data_saver implements data usage restrictions.
111 * - Via the UI the user can add and remove apps from the whitelist and
112 * blacklist, and turn on/off data saver.
113 * - The blacklist takes precedence over the whitelist and the whitelist
114 * takes precedence over data saver.
115 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700116 * * bw_penalty_box handling:
117 * - only one bw_penalty_box for all interfaces
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900118 * E.g Adding an app:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700119 * iptables -I bw_penalty_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700120 * --jump REJECT --reject-with icmp-port-unreachable
121 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700122 * * bw_happy_box handling:
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900123 * - The bw_happy_box comes after the penalty box.
JP Abgralle4788732013-07-02 20:28:45 -0700124 * E.g Adding a happy app,
JP Abgrall7e51cde2013-07-03 13:33:05 -0700125 * iptables -I bw_happy_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700126 * --jump RETURN
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900127 *
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900128 * * bw_data_saver handling:
129 * - The bw_data_saver comes after the happy box.
130 * Enable data saver:
131 * iptables -R 1 bw_data_saver --jump REJECT --reject-with icmp-port-unreachable
132 * Disable data saver:
133 * iptables -R 1 bw_data_saver --jump RETURN
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700134 */
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900135
136const char *IPT_FLUSH_COMMANDS[] = {
JP Abgrall0031cea2012-04-17 16:38:23 -0700137 /*
138 * Cleanup rules.
JP Abgrall7e51cde2013-07-03 13:33:05 -0700139 * Should normally include bw_costly_<iface>, but we rely on the way they are setup
JP Abgrall0031cea2012-04-17 16:38:23 -0700140 * to allow coexistance.
JP Abgrall39f8f242011-06-29 19:21:58 -0700141 */
JP Abgrall0031cea2012-04-17 16:38:23 -0700142 "-F bw_INPUT",
143 "-F bw_OUTPUT",
144 "-F bw_FORWARD",
JP Abgrall7e51cde2013-07-03 13:33:05 -0700145 "-F bw_happy_box",
146 "-F bw_penalty_box",
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900147 "-F bw_data_saver",
JP Abgrall7e51cde2013-07-03 13:33:05 -0700148 "-F bw_costly_shared",
JP Abgrallf66d6e92012-04-27 00:22:57 -0700149
150 "-t raw -F bw_raw_PREROUTING",
151 "-t mangle -F bw_mangle_POSTROUTING",
JP Abgrall0031cea2012-04-17 16:38:23 -0700152};
153
154/* The cleanup commands assume flushing has been done. */
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900155const char *IPT_CLEANUP_COMMANDS[] = {
JP Abgrall7e51cde2013-07-03 13:33:05 -0700156 "-X bw_happy_box",
157 "-X bw_penalty_box",
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900158 "-X bw_data_saver",
JP Abgrall7e51cde2013-07-03 13:33:05 -0700159 "-X bw_costly_shared",
JP Abgrall0dad7c22011-06-24 11:58:14 -0700160};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700161
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900162const char *IPT_SETUP_COMMANDS[] = {
JP Abgrall7e51cde2013-07-03 13:33:05 -0700163 "-N bw_happy_box",
164 "-N bw_penalty_box",
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900165 "-N bw_data_saver",
JP Abgrall7e51cde2013-07-03 13:33:05 -0700166 "-N bw_costly_shared",
JP Abgrall0dad7c22011-06-24 11:58:14 -0700167};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700168
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900169const char *IPT_BASIC_ACCOUNTING_COMMANDS[] = {
JP Abgrall0031cea2012-04-17 16:38:23 -0700170 "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700171
JP Abgrall0031cea2012-04-17 16:38:23 -0700172 "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700173
JP Abgrall92009c82013-02-06 18:01:24 -0800174 "-t raw -A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */
175 "-t mangle -A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700176
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900177 "-A bw_costly_shared --jump bw_penalty_box",
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900178
179 "-A bw_penalty_box --jump bw_happy_box",
180 "-A bw_happy_box --jump bw_data_saver",
181 "-A bw_data_saver -j RETURN",
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900182};
183
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900184const std::string kDataSaverEnableCommand = "-R bw_data_saver 1";
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900185
186} // namespace
187
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700188BandwidthController::BandwidthController(void) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700189}
190
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700191int BandwidthController::runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrallad729ac2012-04-24 23:27:44 -0700192 IptFailureLog failureHandling) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700193 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700194
Steve Block3fb42e02011-10-20 11:55:56 +0100195 ALOGV("runIpxtablesCmd(cmd=%s)", cmd);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700196 res |= runIptablesCmd(cmd, jumpHandling, IptIpV4, failureHandling);
197 res |= runIptablesCmd(cmd, jumpHandling, IptIpV6, failureHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700198 return res;
199}
200
JP Abgrall26e0d492011-06-24 19:21:51 -0700201int BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) {
202
203 memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0'
204 strncpy(buffer, src, buffSize);
205 return buffer[buffSize - 1];
206}
207
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700208int BandwidthController::runIptablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrallad729ac2012-04-24 23:27:44 -0700209 IptIpVer iptVer, IptFailureLog failureHandling) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700210 char buffer[MAX_CMD_LEN];
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700211 const char *argv[MAX_CMD_ARGS];
JP Abgrall26e0d492011-06-24 19:21:51 -0700212 int argc = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700213 char *next = buffer;
214 char *tmp;
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700215 int res;
Rom Lemarchand14150212013-01-24 10:01:04 -0800216 int status = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700217
JP Abgrall0dad7c22011-06-24 11:58:14 -0700218 std::string fullCmd = cmd;
JP Abgrall26e0d492011-06-24 19:21:51 -0700219
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700220 switch (jumpHandling) {
221 case IptJumpReject:
JP Abgrall340d5cc2013-06-28 17:06:00 -0700222 /*
223 * Must be carefull what one rejects with, as uper layer protocols will just
224 * keep on hammering the device until the number of retries are done.
225 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
226 */
227 fullCmd += " --jump REJECT";
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700228 break;
229 case IptJumpReturn:
230 fullCmd += " --jump RETURN";
231 break;
232 case IptJumpNoAdd:
233 break;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700234 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700235
Paul Jensen94b2ab92015-08-04 10:35:05 -0400236 fullCmd.insert(0, " -w ");
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700237 fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700238
Rom Lemarchand14150212013-01-24 10:01:04 -0800239 if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
240 ALOGE("iptables command too long");
241 return -1;
242 }
243
244 argc = 0;
245 while ((tmp = strsep(&next, " "))) {
246 argv[argc++] = tmp;
247 if (argc >= MAX_CMD_ARGS) {
248 ALOGE("iptables argument overflow");
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700249 return -1;
250 }
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700251 }
Rom Lemarchand14150212013-01-24 10:01:04 -0800252
253 argv[argc] = NULL;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900254 res = execFunction(argc, (char **)argv, &status, false,
Rom Lemarchand14150212013-01-24 10:01:04 -0800255 failureHandling == IptFailShow);
JP Abgrallc8dc63b2013-02-13 16:30:00 -0800256 res = res || !WIFEXITED(status) || WEXITSTATUS(status);
257 if (res && failureHandling == IptFailShow) {
258 ALOGE("runIptablesCmd(): res=%d status=%d failed %s", res, status,
259 fullCmd.c_str());
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700260 }
261 return res;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700262}
263
JP Abgrall0e540ec2013-08-26 15:13:10 -0700264void BandwidthController::flushCleanTables(bool doClean) {
265 /* Flush and remove the bw_costly_<iface> tables */
266 flushExistingCostlyTables(doClean);
JP Abgrall0031cea2012-04-17 16:38:23 -0700267
268 /* Some of the initialCommands are allowed to fail */
269 runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
270 IPT_FLUSH_COMMANDS, RunCmdFailureOk);
271
JP Abgrall0e540ec2013-08-26 15:13:10 -0700272 if (doClean) {
273 runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
274 IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
275 }
276}
JP Abgrall0031cea2012-04-17 16:38:23 -0700277
JP Abgrall0e540ec2013-08-26 15:13:10 -0700278int BandwidthController::setupIptablesHooks(void) {
279
280 /* flush+clean is allowed to fail */
281 flushCleanTables(true);
JP Abgrall0031cea2012-04-17 16:38:23 -0700282 runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*),
283 IPT_SETUP_COMMANDS, RunCmdFailureBad);
284
285 return 0;
JP Abgrall0031cea2012-04-17 16:38:23 -0700286}
287
288int BandwidthController::enableBandwidthControl(bool force) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700289 int res;
JP Abgrall0031cea2012-04-17 16:38:23 -0700290 char value[PROPERTY_VALUE_MAX];
291
292 if (!force) {
293 property_get("persist.bandwidth.enable", value, "1");
294 if (!strcmp(value, "0"))
295 return 0;
296 }
JP Abgrall8a932722011-07-13 19:17:35 -0700297
JP Abgralldb7da582011-09-18 12:57:32 -0700298 /* Let's pretend we started from scratch ... */
JP Abgrall8a932722011-07-13 19:17:35 -0700299 sharedQuotaIfaces.clear();
300 quotaIfaces.clear();
JP Abgralldb7da582011-09-18 12:57:32 -0700301 globalAlertBytes = 0;
JP Abgrallc6c67342011-10-07 16:28:54 -0700302 globalAlertTetherCount = 0;
JP Abgralldb7da582011-09-18 12:57:32 -0700303 sharedQuotaBytes = sharedAlertBytes = 0;
304
JP Abgrall0e540ec2013-08-26 15:13:10 -0700305 flushCleanTables(false);
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900306 res = runCommands(ARRAY_SIZE(IPT_BASIC_ACCOUNTING_COMMANDS),
JP Abgralldb7da582011-09-18 12:57:32 -0700307 IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad);
JP Abgrall8a932722011-07-13 19:17:35 -0700308
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900309 char cmd[MAX_CMD_LEN];
310 snprintf(cmd, sizeof(cmd),
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900311 "-I bw_happy_box -m owner --uid-owner %d-%d", 0, MAX_SYSTEM_UID);
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900312 runIpxtablesCmd(cmd, IptJumpReturn);
313
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700314 return res;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700315
316}
317
318int BandwidthController::disableBandwidthControl(void) {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700319
320 flushCleanTables(false);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700321 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700322}
323
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900324int BandwidthController::enableDataSaver(bool enable) {
325 return runIpxtablesCmd(kDataSaverEnableCommand.c_str(),
326 enable ? IptJumpReject : IptJumpReturn, IptFailShow);
327}
328
JP Abgrall8a932722011-07-13 19:17:35 -0700329int BandwidthController::runCommands(int numCommands, const char *commands[],
330 RunCmdErrHandling cmdErrHandling) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700331 int res = 0;
JP Abgrallad729ac2012-04-24 23:27:44 -0700332 IptFailureLog failureLogging = IptFailShow;
333 if (cmdErrHandling == RunCmdFailureOk) {
334 failureLogging = IptFailHide;
335 }
Steve Block3fb42e02011-10-20 11:55:56 +0100336 ALOGV("runCommands(): %d commands", numCommands);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700337 for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700338 res = runIpxtablesCmd(commands[cmdNum], IptJumpNoAdd, failureLogging);
JP Abgrall0031cea2012-04-17 16:38:23 -0700339 if (res && cmdErrHandling != RunCmdFailureOk)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700340 return res;
341 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700342 return 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700343}
344
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700345std::string BandwidthController::makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700346 std::string res;
JP Abgrall8a932722011-07-13 19:17:35 -0700347 char *buff;
348 const char *opFlag;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700349
350 switch (op) {
JP Abgrall8a932722011-07-13 19:17:35 -0700351 case IptOpInsert:
352 opFlag = "-I";
353 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800354 case IptOpAppend:
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700355 ALOGE("Append op not supported for %s uids", chain);
356 res = "";
357 return res;
JP Abgrall109899b2013-02-12 19:20:13 -0800358 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700359 case IptOpReplace:
360 opFlag = "-R";
361 break;
362 default:
363 case IptOpDelete:
364 opFlag = "-D";
365 break;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700366 }
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700367 asprintf(&buff, "%s %s -m owner --uid-owner %d", opFlag, chain, uid);
JP Abgrall8a932722011-07-13 19:17:35 -0700368 res = buff;
369 free(buff);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700370 return res;
371}
372
373int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700374 return manipulateNaughtyApps(numUids, appUids, SpecialAppOpAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700375}
376
377int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700378 return manipulateNaughtyApps(numUids, appUids, SpecialAppOpRemove);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700379}
380
JP Abgralle4788732013-07-02 20:28:45 -0700381int BandwidthController::addNiceApps(int numUids, char *appUids[]) {
382 return manipulateNiceApps(numUids, appUids, SpecialAppOpAdd);
383}
384
385int BandwidthController::removeNiceApps(int numUids, char *appUids[]) {
386 return manipulateNiceApps(numUids, appUids, SpecialAppOpRemove);
387}
388
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700389int BandwidthController::manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
Lorenzo Colittib1f05572016-03-18 11:55:56 +0900390 return manipulateSpecialApps(numUids, appStrUids, "bw_penalty_box", IptJumpReject, appOp);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700391}
392
JP Abgralle4788732013-07-02 20:28:45 -0700393int BandwidthController::manipulateNiceApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
Lorenzo Colittib1f05572016-03-18 11:55:56 +0900394 return manipulateSpecialApps(numUids, appStrUids, "bw_happy_box", IptJumpReturn, appOp);
JP Abgralle4788732013-07-02 20:28:45 -0700395}
396
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700397
398int BandwidthController::manipulateSpecialApps(int numUids, char *appStrUids[],
399 const char *chain,
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700400 IptJumpOp jumpHandling, SpecialAppOp appOp) {
401
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700402 int uidNum;
JP Abgrall26e0d492011-06-24 19:21:51 -0700403 const char *failLogTemplate;
404 IptOp op;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700405 int appUids[numUids];
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700406 std::string iptCmd;
JP Abgrall8a932722011-07-13 19:17:35 -0700407
JP Abgrall26e0d492011-06-24 19:21:51 -0700408 switch (appOp) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700409 case SpecialAppOpAdd:
JP Abgrall8a932722011-07-13 19:17:35 -0700410 op = IptOpInsert;
JP Abgrallaf476f72013-07-03 12:23:55 -0700411 failLogTemplate = "Failed to add app uid %s(%d) to %s.";
JP Abgrall8a932722011-07-13 19:17:35 -0700412 break;
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700413 case SpecialAppOpRemove:
JP Abgrall8a932722011-07-13 19:17:35 -0700414 op = IptOpDelete;
JP Abgrallaf476f72013-07-03 12:23:55 -0700415 failLogTemplate = "Failed to delete app uid %s(%d) from %s box.";
JP Abgrall8a932722011-07-13 19:17:35 -0700416 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700417 default:
418 ALOGE("Unexpected app Op %d", appOp);
419 return -1;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700420 }
421
422 for (uidNum = 0; uidNum < numUids; uidNum++) {
JP Abgrallaf476f72013-07-03 12:23:55 -0700423 char *end;
424 appUids[uidNum] = strtoul(appStrUids[uidNum], &end, 0);
425 if (*end || !*appStrUids[uidNum]) {
426 ALOGE(failLogTemplate, appStrUids[uidNum], appUids[uidNum], chain);
JP Abgrall26e0d492011-06-24 19:21:51 -0700427 goto fail_parse;
428 }
429 }
JP Abgrall26e0d492011-06-24 19:21:51 -0700430
431 for (uidNum = 0; uidNum < numUids; uidNum++) {
JP Abgrallb1d24092012-04-27 01:02:31 -0700432 int uid = appUids[uidNum];
JP Abgrallb1d24092012-04-27 01:02:31 -0700433
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700434 iptCmd = makeIptablesSpecialAppCmd(op, uid, chain);
435 if (runIpxtablesCmd(iptCmd.c_str(), jumpHandling)) {
JP Abgrallaf476f72013-07-03 12:23:55 -0700436 ALOGE(failLogTemplate, appStrUids[uidNum], uid, chain);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700437 goto fail_with_uidNum;
438 }
439 }
440 return 0;
441
JP Abgrall26e0d492011-06-24 19:21:51 -0700442fail_with_uidNum:
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700443 /* Try to remove the uid that failed in any case*/
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700444 iptCmd = makeIptablesSpecialAppCmd(IptOpDelete, appUids[uidNum], chain);
445 runIpxtablesCmd(iptCmd.c_str(), jumpHandling);
JP Abgrall26e0d492011-06-24 19:21:51 -0700446fail_parse:
447 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700448}
449
JP Abgrall26e0d492011-06-24 19:21:51 -0700450std::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700451 std::string res;
JP Abgrall8a932722011-07-13 19:17:35 -0700452 char *buff;
453 const char *opFlag;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700454
SynergyDev7776cea2014-03-16 15:48:51 -0700455 ALOGV("makeIptablesQuotaCmd(%d, %" PRId64")", op, quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700456
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700457 switch (op) {
JP Abgrall8a932722011-07-13 19:17:35 -0700458 case IptOpInsert:
459 opFlag = "-I";
460 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800461 case IptOpAppend:
462 opFlag = "-A";
463 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700464 case IptOpReplace:
465 opFlag = "-R";
466 break;
467 default:
468 case IptOpDelete:
469 opFlag = "-D";
470 break;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700471 }
JP Abgrall8a932722011-07-13 19:17:35 -0700472
JP Abgrallbfa74662011-06-29 19:23:04 -0700473 // The requried IP version specific --jump REJECT ... will be added later.
SynergyDev7776cea2014-03-16 15:48:51 -0700474 asprintf(&buff, "%s bw_costly_%s -m quota2 ! --quota %" PRId64" --name %s", opFlag, costName, quota,
JP Abgrall8a932722011-07-13 19:17:35 -0700475 costName);
476 res = buff;
477 free(buff);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700478 return res;
479}
480
JP Abgrall26e0d492011-06-24 19:21:51 -0700481int BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700482 char cmd[MAX_CMD_LEN];
JP Abgrall0031cea2012-04-17 16:38:23 -0700483 int res = 0, res1, res2;
JP Abgrall8a932722011-07-13 19:17:35 -0700484 int ruleInsertPos = 1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700485 std::string costString;
486 const char *costCString;
487
JP Abgrall0dad7c22011-06-24 11:58:14 -0700488 /* The "-N costly" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700489 switch (quotaType) {
490 case QuotaUnique:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700491 costString = "bw_costly_";
JP Abgrall0dad7c22011-06-24 11:58:14 -0700492 costString += ifn;
493 costCString = costString.c_str();
JP Abgrall0031cea2012-04-17 16:38:23 -0700494 /*
JP Abgrall7e51cde2013-07-03 13:33:05 -0700495 * Flush the bw_costly_<iface> is allowed to fail in case it didn't exist.
JP Abgrall0031cea2012-04-17 16:38:23 -0700496 * Creating a new one is allowed to fail in case it existed.
497 * This helps with netd restarts.
498 */
499 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700500 res1 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700501 snprintf(cmd, sizeof(cmd), "-N %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700502 res2 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700503 res = (res1 && res2) || (!res1 && !res2);
504
JP Abgrall7e51cde2013-07-03 13:33:05 -0700505 snprintf(cmd, sizeof(cmd), "-A %s -j bw_penalty_box", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700506 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall26e0d492011-06-24 19:21:51 -0700507 break;
508 case QuotaShared:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700509 costCString = "bw_costly_shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700510 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700511 default:
512 ALOGE("Unexpected quotatype %d", quotaType);
513 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700514 }
515
JP Abgrall8a932722011-07-13 19:17:35 -0700516 if (globalAlertBytes) {
517 /* The alert rule comes 1st */
518 ruleInsertPos = 2;
519 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700520
521 snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700522 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700523
524 snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700525 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0031cea2012-04-17 16:38:23 -0700526
527 snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700528 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700529
530 snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700531 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
Erik Kline58a94482015-10-02 17:52:37 +0900532
533 snprintf(cmd, sizeof(cmd), "-D bw_FORWARD -o %s --jump %s", ifn, costCString);
534 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
535 snprintf(cmd, sizeof(cmd), "-A bw_FORWARD -o %s --jump %s", ifn, costCString);
536 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
537
JP Abgrall0dad7c22011-06-24 11:58:14 -0700538 return res;
539}
540
JP Abgrall26e0d492011-06-24 19:21:51 -0700541int BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700542 char cmd[MAX_CMD_LEN];
543 int res = 0;
544 std::string costString;
545 const char *costCString;
546
JP Abgrall26e0d492011-06-24 19:21:51 -0700547 switch (quotaType) {
548 case QuotaUnique:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700549 costString = "bw_costly_";
JP Abgrall0dad7c22011-06-24 11:58:14 -0700550 costString += ifn;
551 costCString = costString.c_str();
JP Abgrall26e0d492011-06-24 19:21:51 -0700552 break;
553 case QuotaShared:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700554 costCString = "bw_costly_shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700555 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700556 default:
557 ALOGE("Unexpected quotatype %d", quotaType);
558 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700559 }
560
JP Abgrall0031cea2012-04-17 16:38:23 -0700561 snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700562 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
Erik Kline58a94482015-10-02 17:52:37 +0900563 for (const auto tableName : {LOCAL_OUTPUT, LOCAL_FORWARD}) {
564 snprintf(cmd, sizeof(cmd), "-D %s -o %s --jump %s", tableName, ifn, costCString);
565 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
566 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700567
JP Abgrall7e51cde2013-07-03 13:33:05 -0700568 /* The "-N bw_costly_shared" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700569 if (quotaType == QuotaUnique) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700570 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700571 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgralla9f802c2011-06-29 15:46:45 -0700572 snprintf(cmd, sizeof(cmd), "-X %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700573 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700574 }
575 return res;
576}
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700577
JP Abgrall0dad7c22011-06-24 11:58:14 -0700578int BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700579 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700580 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700581 std::string quotaCmd;
JP Abgrall8a932722011-07-13 19:17:35 -0700582 std::string ifaceName;
583 ;
JP Abgrallbfa74662011-06-29 19:23:04 -0700584 const char *costName = "shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700585 std::list<std::string>::iterator it;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700586
JP Abgrall8a932722011-07-13 19:17:35 -0700587 if (!maxBytes) {
588 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000589 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700590 return -1;
591 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700592 if (!isIfaceName(iface))
593 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700594 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000595 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700596 return -1;
597 }
598 ifaceName = ifn;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700599
600 if (maxBytes == -1) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700601 return removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700602 }
603
604 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700605 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
606 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700607 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700608 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700609
JP Abgrall0dad7c22011-06-24 11:58:14 -0700610 if (it == sharedQuotaIfaces.end()) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700611 res |= prepCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700612 if (sharedQuotaIfaces.empty()) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700613 quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700614 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700615 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000616 ALOGE("Failed set quota rule");
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700617 goto fail;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700618 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700619 sharedQuotaBytes = maxBytes;
620 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700621 sharedQuotaIfaces.push_front(ifaceName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700622
623 }
624
625 if (maxBytes != sharedQuotaBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700626 res |= updateQuota(costName, maxBytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700627 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000628 ALOGE("Failed update quota for %s", costName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700629 goto fail;
630 }
631 sharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700632 }
633 return 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700634
635 fail:
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700636 /*
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700637 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
638 * rules in the kernel to see which ones need cleaning up.
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700639 * For now callers needs to choose if they want to "ndc bandwidth enable"
640 * which resets everything.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700641 */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700642 removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700643 return -1;
644}
645
JP Abgrall8a932722011-07-13 19:17:35 -0700646/* It will also cleanup any shared alerts */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700647int BandwidthController::removeInterfaceSharedQuota(const char *iface) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700648 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700649 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700650 std::string ifaceName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700651 std::list<std::string>::iterator it;
JP Abgrallbfa74662011-06-29 19:23:04 -0700652 const char *costName = "shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700653
JP Abgrall69261cb2014-06-19 18:35:24 -0700654 if (!isIfaceName(iface))
655 return -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700656 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000657 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700658 return -1;
659 }
JP Abgrall8a932722011-07-13 19:17:35 -0700660 ifaceName = ifn;
JP Abgrall26e0d492011-06-24 19:21:51 -0700661
JP Abgrall0dad7c22011-06-24 11:58:14 -0700662 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
663 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700664 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700665 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700666 if (it == sharedQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000667 ALOGE("No such iface %s to delete", ifn);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700668 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700669 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700670
JP Abgrall26e0d492011-06-24 19:21:51 -0700671 res |= cleanupCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700672 sharedQuotaIfaces.erase(it);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700673
JP Abgrall0dad7c22011-06-24 11:58:14 -0700674 if (sharedQuotaIfaces.empty()) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700675 std::string quotaCmd;
JP Abgrallbfa74662011-06-29 19:23:04 -0700676 quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700677 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall8a932722011-07-13 19:17:35 -0700678 sharedQuotaBytes = 0;
679 if (sharedAlertBytes) {
680 removeSharedAlert();
681 sharedAlertBytes = 0;
682 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700683 }
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700684 return res;
685}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700686
687int BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
688 char ifn[MAX_IFACENAME_LEN];
689 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700690 std::string ifaceName;
691 const char *costName;
692 std::list<QuotaInfo>::iterator it;
693 std::string quotaCmd;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700694
JP Abgrall69261cb2014-06-19 18:35:24 -0700695 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700696 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700697
JP Abgrall8a932722011-07-13 19:17:35 -0700698 if (!maxBytes) {
699 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000700 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700701 return -1;
702 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700703 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700704 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700705 }
706
JP Abgrall8a932722011-07-13 19:17:35 -0700707 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000708 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700709 return -1;
710 }
711 ifaceName = ifn;
712 costName = iface;
713
JP Abgrall0dad7c22011-06-24 11:58:14 -0700714 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700715 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
JP Abgrall8a932722011-07-13 19:17:35 -0700716 if (it->ifaceName == ifaceName)
JP Abgrall0dad7c22011-06-24 11:58:14 -0700717 break;
718 }
719
720 if (it == quotaIfaces.end()) {
JP Abgralle4788732013-07-02 20:28:45 -0700721 /* Preparing the iface adds a penalty/happy box check */
JP Abgrall26e0d492011-06-24 19:21:51 -0700722 res |= prepCostlyIface(ifn, QuotaUnique);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700723 /*
JP Abgralle4788732013-07-02 20:28:45 -0700724 * The rejecting quota limit should go after the penalty/happy box checks
JP Abgrallbaeccc42013-06-25 09:44:10 -0700725 * or else a naughty app could just eat up the quota.
726 * So we append here.
727 */
JP Abgrall109899b2013-02-12 19:20:13 -0800728 quotaCmd = makeIptablesQuotaCmd(IptOpAppend, costName, maxBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700729 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700730 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000731 ALOGE("Failed set quota rule");
JP Abgrall0dad7c22011-06-24 11:58:14 -0700732 goto fail;
733 }
734
JP Abgrall8a932722011-07-13 19:17:35 -0700735 quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
JP Abgrall0dad7c22011-06-24 11:58:14 -0700736
737 } else {
JP Abgrall8a932722011-07-13 19:17:35 -0700738 res |= updateQuota(costName, maxBytes);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700739 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000740 ALOGE("Failed update quota for %s", iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700741 goto fail;
742 }
JP Abgrall8a932722011-07-13 19:17:35 -0700743 it->quota = maxBytes;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700744 }
745 return 0;
746
747 fail:
748 /*
749 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
750 * rules in the kernel to see which ones need cleaning up.
751 * For now callers needs to choose if they want to "ndc bandwidth enable"
752 * which resets everything.
753 */
754 removeInterfaceSharedQuota(ifn);
755 return -1;
756}
757
JP Abgrall8a932722011-07-13 19:17:35 -0700758int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
759 return getInterfaceQuota("shared", bytes);
760}
761
762int BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
763 FILE *fp;
764 char *fname;
765 int scanRes;
766
JP Abgrall69261cb2014-06-19 18:35:24 -0700767 if (!isIfaceName(costName))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700768 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700769
JP Abgrall8a932722011-07-13 19:17:35 -0700770 asprintf(&fname, "/proc/net/xt_quota/%s", costName);
Nick Kralevich53ea9ca2015-01-31 13:54:00 -0800771 fp = fopen(fname, "re");
JP Abgrall8a932722011-07-13 19:17:35 -0700772 free(fname);
773 if (!fp) {
Steve Block5ea0c052012-01-06 19:18:11 +0000774 ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
JP Abgrall8a932722011-07-13 19:17:35 -0700775 return -1;
776 }
Mark Salyzynca0b5e22014-03-26 14:15:03 -0700777 scanRes = fscanf(fp, "%" SCNd64, bytes);
SynergyDev7776cea2014-03-16 15:48:51 -0700778 ALOGV("Read quota res=%d bytes=%" PRId64, scanRes, *bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700779 fclose(fp);
780 return scanRes == 1 ? 0 : -1;
781}
782
JP Abgrall0dad7c22011-06-24 11:58:14 -0700783int BandwidthController::removeInterfaceQuota(const char *iface) {
784
785 char ifn[MAX_IFACENAME_LEN];
786 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700787 std::string ifaceName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700788 std::list<QuotaInfo>::iterator it;
JP Abgrall26e0d492011-06-24 19:21:51 -0700789
JP Abgrall69261cb2014-06-19 18:35:24 -0700790 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700791 return -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700792 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000793 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700794 return -1;
795 }
796 ifaceName = ifn;
JP Abgrall26e0d492011-06-24 19:21:51 -0700797
JP Abgrall0dad7c22011-06-24 11:58:14 -0700798 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
JP Abgrall8a932722011-07-13 19:17:35 -0700799 if (it->ifaceName == ifaceName)
JP Abgrall0dad7c22011-06-24 11:58:14 -0700800 break;
801 }
802
803 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000804 ALOGE("No such iface %s to delete", ifn);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700805 return -1;
806 }
807
808 /* This also removes the quota command of CostlyIface chain. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700809 res |= cleanupCostlyIface(ifn, QuotaUnique);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700810
811 quotaIfaces.erase(it);
812
813 return res;
814}
JP Abgrall8a932722011-07-13 19:17:35 -0700815
816int BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
817 FILE *fp;
818 char *fname;
819
JP Abgrall69261cb2014-06-19 18:35:24 -0700820 if (!isIfaceName(quotaName)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700821 ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName);
822 return -1;
823 }
824
JP Abgrall8a932722011-07-13 19:17:35 -0700825 asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
Nick Kralevich53ea9ca2015-01-31 13:54:00 -0800826 fp = fopen(fname, "we");
JP Abgrall8a932722011-07-13 19:17:35 -0700827 free(fname);
828 if (!fp) {
Steve Block5ea0c052012-01-06 19:18:11 +0000829 ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
JP Abgrall8a932722011-07-13 19:17:35 -0700830 return -1;
831 }
SynergyDev7776cea2014-03-16 15:48:51 -0700832 fprintf(fp, "%" PRId64"\n", bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700833 fclose(fp);
834 return 0;
835}
836
837int BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
838 int res = 0;
839 const char *opFlag;
840 char *alertQuotaCmd;
841
842 switch (op) {
843 case IptOpInsert:
844 opFlag = "-I";
845 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800846 case IptOpAppend:
847 opFlag = "-A";
848 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700849 case IptOpReplace:
850 opFlag = "-R";
851 break;
852 default:
853 case IptOpDelete:
854 opFlag = "-D";
855 break;
856 }
857
JP Abgrall92009c82013-02-06 18:01:24 -0800858 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800859 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700860 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700861 free(alertQuotaCmd);
JP Abgrall92009c82013-02-06 18:01:24 -0800862 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT",
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);
866 return res;
867}
868
JP Abgrallc6c67342011-10-07 16:28:54 -0700869int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
870 int res = 0;
871 const char *opFlag;
JP Abgrall8a932722011-07-13 19:17:35 -0700872 char *alertQuotaCmd;
JP Abgrallc6c67342011-10-07 16:28:54 -0700873
874 switch (op) {
875 case IptOpInsert:
876 opFlag = "-I";
877 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800878 case IptOpAppend:
879 opFlag = "-A";
880 break;
JP Abgrallc6c67342011-10-07 16:28:54 -0700881 case IptOpReplace:
882 opFlag = "-R";
883 break;
884 default:
885 case IptOpDelete:
886 opFlag = "-D";
887 break;
888 }
889
JP Abgrall92009c82013-02-06 18:01:24 -0800890 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800891 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700892 res = runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrallc6c67342011-10-07 16:28:54 -0700893 free(alertQuotaCmd);
894 return res;
895}
896
897int BandwidthController::setGlobalAlert(int64_t bytes) {
898 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700899 int res = 0;
900
901 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000902 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700903 return -1;
904 }
905 if (globalAlertBytes) {
906 res = updateQuota(alertName, bytes);
907 } else {
908 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700909 if (globalAlertTetherCount) {
Steve Block3fb42e02011-10-20 11:55:56 +0100910 ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700911 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
912 }
JP Abgrall8a932722011-07-13 19:17:35 -0700913 }
914 globalAlertBytes = bytes;
915 return res;
916}
917
JP Abgrallc6c67342011-10-07 16:28:54 -0700918int BandwidthController::setGlobalAlertInForwardChain(void) {
919 const char *alertName = ALERT_GLOBAL_NAME;
920 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700921
JP Abgrallc6c67342011-10-07 16:28:54 -0700922 globalAlertTetherCount++;
Steve Block3fb42e02011-10-20 11:55:56 +0100923 ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700924
925 /*
926 * If there is no globalAlert active we are done.
927 * If there is an active globalAlert but this is not the 1st
928 * tether, we are also done.
929 */
930 if (!globalAlertBytes || globalAlertTetherCount != 1) {
931 return 0;
932 }
933
934 /* We only add the rule if this was the 1st tether added. */
935 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
936 return res;
937}
938
939int BandwidthController::removeGlobalAlert(void) {
940
941 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700942 int res = 0;
943
944 if (!globalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000945 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700946 return -1;
947 }
948 res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700949 if (globalAlertTetherCount) {
950 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
951 }
JP Abgrall8a932722011-07-13 19:17:35 -0700952 globalAlertBytes = 0;
953 return res;
954}
955
JP Abgrallc6c67342011-10-07 16:28:54 -0700956int BandwidthController::removeGlobalAlertInForwardChain(void) {
957 int res = 0;
958 const char *alertName = ALERT_GLOBAL_NAME;
959
960 if (!globalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000961 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700962 return -1;
963 }
964
965 globalAlertTetherCount--;
966 /*
967 * If there is no globalAlert active we are done.
968 * If there is an active globalAlert but there are more
969 * tethers, we are also done.
970 */
971 if (!globalAlertBytes || globalAlertTetherCount >= 1) {
972 return 0;
973 }
974
975 /* We only detete the rule if this was the last tether removed. */
976 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
977 return res;
978}
979
JP Abgrall8a932722011-07-13 19:17:35 -0700980int BandwidthController::setSharedAlert(int64_t bytes) {
981 if (!sharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000982 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700983 return -1;
984 }
985 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000986 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700987 return -1;
988 }
989 return setCostlyAlert("shared", bytes, &sharedAlertBytes);
990}
991
992int BandwidthController::removeSharedAlert(void) {
993 return removeCostlyAlert("shared", &sharedAlertBytes);
994}
995
996int BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
997 std::list<QuotaInfo>::iterator it;
998
JP Abgrall69261cb2014-06-19 18:35:24 -0700999 if (!isIfaceName(iface)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -07001000 ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface);
1001 return -1;
1002 }
1003
JP Abgrall8a932722011-07-13 19:17:35 -07001004 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001005 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -07001006 return -1;
1007 }
1008 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
1009 if (it->ifaceName == iface)
1010 break;
1011 }
1012
1013 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +00001014 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -07001015 return -1;
1016 }
1017
1018 return setCostlyAlert(iface, bytes, &it->alert);
1019}
1020
1021int BandwidthController::removeInterfaceAlert(const char *iface) {
1022 std::list<QuotaInfo>::iterator it;
1023
JP Abgrall69261cb2014-06-19 18:35:24 -07001024 if (!isIfaceName(iface)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -07001025 ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface);
1026 return -1;
1027 }
1028
JP Abgrall8a932722011-07-13 19:17:35 -07001029 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
1030 if (it->ifaceName == iface)
1031 break;
1032 }
1033
1034 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +00001035 ALOGE("No prior alert set for interface %s", iface);
JP Abgrall8a932722011-07-13 19:17:35 -07001036 return -1;
1037 }
1038
1039 return removeCostlyAlert(iface, &it->alert);
1040}
1041
1042int BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
1043 char *alertQuotaCmd;
JP Abgrall109899b2013-02-12 19:20:13 -08001044 char *chainName;
JP Abgrall8a932722011-07-13 19:17:35 -07001045 int res = 0;
1046 char *alertName;
1047
JP Abgrall69261cb2014-06-19 18:35:24 -07001048 if (!isIfaceName(costName)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -07001049 ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName);
1050 return -1;
1051 }
1052
JP Abgrall8a932722011-07-13 19:17:35 -07001053 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001054 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -07001055 return -1;
1056 }
1057 asprintf(&alertName, "%sAlert", costName);
1058 if (*alertBytes) {
1059 res = updateQuota(alertName, *alertBytes);
1060 } else {
JP Abgrall7e51cde2013-07-03 13:33:05 -07001061 asprintf(&chainName, "bw_costly_%s", costName);
JP Abgrall109899b2013-02-12 19:20:13 -08001062 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -07001063 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -07001064 free(alertQuotaCmd);
JP Abgrall109899b2013-02-12 19:20:13 -08001065 free(chainName);
JP Abgrall8a932722011-07-13 19:17:35 -07001066 }
1067 *alertBytes = bytes;
1068 free(alertName);
1069 return res;
1070}
1071
1072int BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
1073 char *alertQuotaCmd;
1074 char *chainName;
1075 char *alertName;
1076 int res = 0;
1077
JP Abgrall69261cb2014-06-19 18:35:24 -07001078 if (!isIfaceName(costName)) {
Nick Kralevich0b2b9022014-05-01 13:10:45 -07001079 ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName);
1080 return -1;
1081 }
1082
JP Abgrall8a932722011-07-13 19:17:35 -07001083 if (!*alertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001084 ALOGE("No prior alert set for %s alert", costName);
JP Abgrall8a932722011-07-13 19:17:35 -07001085 return -1;
1086 }
1087
Jesper Hanssona9d791f2012-04-27 13:54:27 +02001088 asprintf(&alertName, "%sAlert", costName);
JP Abgrall7e51cde2013-07-03 13:33:05 -07001089 asprintf(&chainName, "bw_costly_%s", costName);
JP Abgrall92009c82013-02-06 18:01:24 -08001090 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -07001091 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -07001092 free(alertQuotaCmd);
1093 free(chainName);
1094
1095 *alertBytes = 0;
1096 free(alertName);
1097 return res;
1098}
JP Abgralldb7da582011-09-18 12:57:32 -07001099
1100/*
1101 * Parse the ptks and bytes out of:
JP Abgrallbaeccc42013-06-25 09:44:10 -07001102 * Chain natctrl_tether_counters (4 references)
1103 * pkts bytes target prot opt in out source destination
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001104 * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
1105 * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0
1106 * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0
1107 * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0
1108 * It results in an error if invoked and no tethering counter rules exist. The constraint
1109 * helps detect complete parsing failure.
JP Abgralldb7da582011-09-18 12:57:32 -07001110 */
JP Abgrallbaeccc42013-06-25 09:44:10 -07001111int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherStats filter,
1112 FILE *fp, std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -07001113 int res;
1114 char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1115 char iface0[MAX_IPT_OUTPUT_LINE_LEN];
1116 char iface1[MAX_IPT_OUTPUT_LINE_LEN];
1117 char rest[MAX_IPT_OUTPUT_LINE_LEN];
1118
JP Abgrallbaeccc42013-06-25 09:44:10 -07001119 TetherStats stats;
JP Abgralldb7da582011-09-18 12:57:32 -07001120 char *buffPtr;
1121 int64_t packets, bytes;
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001122 int statsFound = 0;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001123
1124 bool filterPair = filter.intIface[0] && filter.extIface[0];
1125
1126 char *filterMsg = filter.getStatsLine();
1127 ALOGV("filter: %s", filterMsg);
1128 free(filterMsg);
1129
1130 stats = filter;
JP Abgralldb7da582011-09-18 12:57:32 -07001131
1132 while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1133 /* Clean up, so a failed parse can still print info */
1134 iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
Mark Salyzynca0b5e22014-03-26 14:15:03 -07001135 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s",
JP Abgralldb7da582011-09-18 12:57:32 -07001136 &packets, &bytes, iface0, iface1, rest);
SynergyDev7776cea2014-03-16 15:48:51 -07001137 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 -07001138 iface0, iface1, packets, bytes, rest, buffPtr);
JP Abgralla2a64f02011-11-11 20:36:16 -08001139 extraProcessingInfo += buffPtr;
1140
JP Abgralldb7da582011-09-18 12:57:32 -07001141 if (res != 5) {
1142 continue;
1143 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001144 /*
1145 * The following assumes that the 1st rule has in:extIface out:intIface,
1146 * which is what NatController sets up.
1147 * If not filtering, the 1st match rx, and sets up the pair for the tx side.
1148 */
1149 if (filter.intIface[0] && filter.extIface[0]) {
1150 if (filter.intIface == iface0 && filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -07001151 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 -07001152 stats.rxPackets = packets;
1153 stats.rxBytes = bytes;
1154 } else if (filter.intIface == iface1 && filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -07001155 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 -07001156 stats.txPackets = packets;
1157 stats.txBytes = bytes;
1158 }
1159 } else if (filter.intIface[0] || filter.extIface[0]) {
1160 if (filter.intIface == iface0 || filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -07001161 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 -07001162 stats.intIface = iface0;
1163 stats.extIface = iface1;
1164 stats.rxPackets = packets;
1165 stats.rxBytes = bytes;
1166 } else if (filter.intIface == iface1 || filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -07001167 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 -07001168 stats.intIface = iface1;
1169 stats.extIface = iface0;
1170 stats.txPackets = packets;
1171 stats.txBytes = bytes;
1172 }
1173 } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ {
1174 if (!stats.intIface[0]) {
SynergyDev7776cea2014-03-16 15:48:51 -07001175 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 -07001176 stats.intIface = iface0;
1177 stats.extIface = iface1;
1178 stats.rxPackets = packets;
1179 stats.rxBytes = bytes;
1180 } else if (stats.intIface == iface1 && stats.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -07001181 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 -07001182 stats.txPackets = packets;
1183 stats.txBytes = bytes;
1184 }
1185 }
1186 if (stats.rxBytes != -1 && stats.txBytes != -1) {
SynergyDev7776cea2014-03-16 15:48:51 -07001187 ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64" filterPair=%d", stats.rxBytes, stats.txBytes, filterPair);
JP Abgrallbaeccc42013-06-25 09:44:10 -07001188 /* Send out stats, and prep for the next if needed. */
1189 char *msg = stats.getStatsLine();
1190 if (filterPair) {
1191 cli->sendMsg(ResponseCode::TetheringStatsResult, msg, false);
1192 return 0;
1193 } else {
1194 cli->sendMsg(ResponseCode::TetheringStatsListResult, msg, false);
1195 stats = filter;
1196 }
1197 free(msg);
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001198 statsFound++;
JP Abgralldb7da582011-09-18 12:57:32 -07001199 }
1200 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001201
1202 /* It is always an error to find only one side of the stats. */
1203 /* It is an error to find nothing when not filtering. */
1204 if (((stats.rxBytes == -1) != (stats.txBytes == -1)) ||
1205 (!statsFound && !filterPair)) {
1206 return -1;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001207 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -07001208 cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
1209 return 0;
JP Abgralldb7da582011-09-18 12:57:32 -07001210}
1211
JP Abgrallbaeccc42013-06-25 09:44:10 -07001212char *BandwidthController::TetherStats::getStatsLine(void) const {
JP Abgralldb7da582011-09-18 12:57:32 -07001213 char *msg;
SynergyDev7776cea2014-03-16 15:48:51 -07001214 asprintf(&msg, "%s %s %" PRId64" %" PRId64" %" PRId64" %" PRId64, intIface.c_str(), extIface.c_str(),
JP Abgralldb7da582011-09-18 12:57:32 -07001215 rxBytes, rxPackets, txBytes, txPackets);
1216 return msg;
1217}
1218
JP Abgrallbaeccc42013-06-25 09:44:10 -07001219int BandwidthController::getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -07001220 int res;
1221 std::string fullCmd;
1222 FILE *iptOutput;
JP Abgralldb7da582011-09-18 12:57:32 -07001223
JP Abgralldb7da582011-09-18 12:57:32 -07001224 /*
1225 * Why not use some kind of lib to talk to iptables?
1226 * Because the only libs are libiptc and libip6tc in iptables, and they are
1227 * not easy to use. They require the known iptables match modules to be
1228 * preloaded/linked, and require apparently a lot of wrapper code to get
1229 * the wanted info.
1230 */
1231 fullCmd = IPTABLES_PATH;
Yusuke Sato99b40502015-08-19 13:47:30 -07001232 fullCmd += " -nvx -w -L ";
JP Abgrallbaeccc42013-06-25 09:44:10 -07001233 fullCmd += NatController::LOCAL_TETHER_COUNTERS_CHAIN;
Lorenzo Colitti86a47982016-03-18 17:52:25 +09001234 iptOutput = popenFunction(fullCmd.c_str(), "r");
JP Abgralldb7da582011-09-18 12:57:32 -07001235 if (!iptOutput) {
Steve Block5ea0c052012-01-06 19:18:11 +00001236 ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
JP Abgralla2a64f02011-11-11 20:36:16 -08001237 extraProcessingInfo += "Failed to run iptables.";
JP Abgralldb7da582011-09-18 12:57:32 -07001238 return -1;
1239 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001240 res = parseForwardChainStats(cli, stats, iptOutput, extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -07001241 pclose(iptOutput);
1242
1243 /* Currently NatController doesn't do ipv6 tethering, so we are done. */
1244 return res;
1245}
JP Abgrall0e540ec2013-08-26 15:13:10 -07001246
1247void BandwidthController::flushExistingCostlyTables(bool doClean) {
JP Abgrall0e540ec2013-08-26 15:13:10 -07001248 std::string fullCmd;
1249 FILE *iptOutput;
JP Abgrall0e540ec2013-08-26 15:13:10 -07001250
1251 /* Only lookup ip4 table names as ip6 will have the same tables ... */
1252 fullCmd = IPTABLES_PATH;
Yusuke Sato99b40502015-08-19 13:47:30 -07001253 fullCmd += " -w -S";
Lorenzo Colitti86a47982016-03-18 17:52:25 +09001254 iptOutput = popenFunction(fullCmd.c_str(), "r");
JP Abgrall0e540ec2013-08-26 15:13:10 -07001255 if (!iptOutput) {
1256 ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
1257 return;
1258 }
1259 /* ... then flush/clean both ip4 and ip6 iptables. */
1260 parseAndFlushCostlyTables(iptOutput, doClean);
1261 pclose(iptOutput);
1262}
1263
1264void BandwidthController::parseAndFlushCostlyTables(FILE *fp, bool doRemove) {
1265 int res;
1266 char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1267 char costlyIfaceName[MAX_IPT_OUTPUT_LINE_LEN];
1268 char cmd[MAX_CMD_LEN];
1269 char *buffPtr;
1270
1271 while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1272 costlyIfaceName[0] = '\0'; /* So that debugging output always works */
1273 res = sscanf(buffPtr, "-N bw_costly_%s", costlyIfaceName);
1274 ALOGV("parse res=%d costly=<%s> orig line=<%s>", res,
1275 costlyIfaceName, buffPtr);
1276 if (res != 1) {
1277 continue;
1278 }
1279 /* Exclusions: "shared" is not an ifacename */
1280 if (!strcmp(costlyIfaceName, "shared")) {
1281 continue;
1282 }
1283
1284 snprintf(cmd, sizeof(cmd), "-F bw_costly_%s", costlyIfaceName);
1285 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
1286 if (doRemove) {
1287 snprintf(cmd, sizeof(cmd), "-X bw_costly_%s", costlyIfaceName);
1288 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
1289 }
1290 }
1291}