blob: 3d1a56ca23659bbb7dafc9a52c99ebcf4dd6fef1 [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
Joel Scherpelz01cc5492017-06-16 10:45:14 +090025#include <ctype.h>
JP Abgrall8a932722011-07-13 19:17:35 -070026#include <errno.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070027#include <fcntl.h>
JP Abgralldb7da582011-09-18 12:57:32 -070028#include <stdio.h>
JP Abgrall8a932722011-07-13 19:17:35 -070029#include <stdlib.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070030#include <string.h>
Joel Scherpelz01cc5492017-06-16 10:45:14 +090031#include <string>
32#include <vector>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070033
Matthew Leach2a54d962013-01-14 15:07:12 +000034#define __STDC_FORMAT_MACROS 1
35#include <inttypes.h>
36
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070037#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40#include <sys/wait.h>
41
42#include <linux/netlink.h>
43#include <linux/rtnetlink.h>
44#include <linux/pkt_sched.h>
45
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090046#include "android-base/stringprintf.h"
Lorenzo Colitti13debb82016-03-27 17:46:30 +090047#include "android-base/strings.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070048#define LOG_TAG "BandwidthController"
49#include <cutils/log.h>
50#include <cutils/properties.h>
Rom Lemarchand14150212013-01-24 10:01:04 -080051#include <logwrap/logwrap.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070052
Joel Scherpelz01cc5492017-06-16 10:45:14 +090053#include <netdutils/Syscalls.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070054#include "BandwidthController.h"
Joel Scherpelz01cc5492017-06-16 10:45:14 +090055#include "NatController.h" /* For LOCAL_TETHER_COUNTERS_CHAIN */
56#include "NetdConstants.h"
JP Abgrallbaeccc42013-06-25 09:44:10 -070057#include "ResponseCode.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070058
JP Abgralldb7da582011-09-18 12:57:32 -070059/* Alphabetical */
Lorenzo Colitti3c272702017-04-26 15:48:13 +090060#define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %" PRId64" --name %s\n"
Joel Scherpelzbcad6612017-05-30 10:55:11 +090061const char BandwidthController::LOCAL_INPUT[] = "bw_INPUT";
62const char BandwidthController::LOCAL_FORWARD[] = "bw_FORWARD";
63const char BandwidthController::LOCAL_OUTPUT[] = "bw_OUTPUT";
64const char BandwidthController::LOCAL_RAW_PREROUTING[] = "bw_raw_PREROUTING";
65const char BandwidthController::LOCAL_MANGLE_POSTROUTING[] = "bw_mangle_POSTROUTING";
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090066
Lorenzo Colitti86a47982016-03-18 17:52:25 +090067auto BandwidthController::execFunction = android_fork_execvp;
68auto BandwidthController::popenFunction = popen;
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090069auto BandwidthController::iptablesRestoreFunction = execIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090070
Joel Scherpelzd59526a2017-06-28 16:24:09 +090071using android::base::Join;
Lorenzo Colitti3c272702017-04-26 15:48:13 +090072using android::base::StringAppendF;
73using android::base::StringPrintf;
Joel Scherpelz01cc5492017-06-16 10:45:14 +090074using android::netdutils::StatusOr;
75using android::netdutils::UniqueFile;
Lorenzo Colitti3c272702017-04-26 15:48:13 +090076
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090077namespace {
78
79const char ALERT_GLOBAL_NAME[] = "globalAlert";
80const int MAX_CMD_ARGS = 32;
81const int MAX_CMD_LEN = 1024;
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090082const int MAX_IPT_OUTPUT_LINE_LEN = 256;
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090083const std::string NEW_CHAIN_COMMAND = "-N ";
Lorenzo Colitti3c272702017-04-26 15:48:13 +090084const std::string GET_TETHER_STATS_COMMAND = StringPrintf(
Lorenzo Colittice6748a2017-02-02 01:34:33 +090085 "*filter\n"
86 "-nvx -L %s\n"
87 "COMMIT\n", NatController::LOCAL_TETHER_COUNTERS_CHAIN);
88
Joel Scherpelzbcad6612017-05-30 10:55:11 +090089const char NAUGHTY_CHAIN[] = "bw_penalty_box";
90const char NICE_CHAIN[] = "bw_happy_box";
JP Abgralldb7da582011-09-18 12:57:32 -070091
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070092/**
93 * Some comments about the rules:
94 * * Ordering
95 * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
JP Abgrall29e8de22012-05-03 12:52:15 -070096 * E.g. "-I bw_INPUT -i rmnet0 --jump costly"
JP Abgrall7e51cde2013-07-03 13:33:05 -070097 * - quota'd rules in the costly chain should be before bw_penalty_box lookups.
JP Abgrall29e8de22012-05-03 12:52:15 -070098 * - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070099 *
100 * * global quota vs per interface quota
101 * - global quota for all costly interfaces uses a single costly chain:
102 * . initial rules
JP Abgrall7e51cde2013-07-03 13:33:05 -0700103 * iptables -N bw_costly_shared
104 * iptables -I bw_INPUT -i iface0 --jump bw_costly_shared
105 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_shared
106 * iptables -I bw_costly_shared -m quota \! --quota 500000 \
JP Abgrallbfa74662011-06-29 19:23:04 -0700107 * --jump REJECT --reject-with icmp-net-prohibited
JP Abgrall7e51cde2013-07-03 13:33:05 -0700108 * iptables -A bw_costly_shared --jump bw_penalty_box
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900109 * iptables -A bw_penalty_box --jump bw_happy_box
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900110 * iptables -A bw_happy_box --jump bw_data_saver
JP Abgrall8a932722011-07-13 19:17:35 -0700111 *
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700112 * . adding a new iface to this, E.g.:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700113 * iptables -I bw_INPUT -i iface1 --jump bw_costly_shared
114 * iptables -I bw_OUTPUT -o iface1 --jump bw_costly_shared
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700115 *
116 * - quota per interface. This is achieve by having "costly" chains per quota.
117 * E.g. adding a new costly interface iface0 with its own quota:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700118 * iptables -N bw_costly_iface0
119 * iptables -I bw_INPUT -i iface0 --jump bw_costly_iface0
120 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_iface0
121 * iptables -A bw_costly_iface0 -m quota \! --quota 500000 \
JP Abgralle4788732013-07-02 20:28:45 -0700122 * --jump REJECT --reject-with icmp-port-unreachable
JP Abgrall7e51cde2013-07-03 13:33:05 -0700123 * iptables -A bw_costly_iface0 --jump bw_penalty_box
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700124 *
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900125 * * Penalty box, happy box and data saver.
126 * - bw_penalty box is a blacklist of apps that are rejected.
127 * - bw_happy_box is a whitelist of apps. It always includes all system apps
128 * - bw_data_saver implements data usage restrictions.
129 * - Via the UI the user can add and remove apps from the whitelist and
130 * blacklist, and turn on/off data saver.
131 * - The blacklist takes precedence over the whitelist and the whitelist
132 * takes precedence over data saver.
133 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700134 * * bw_penalty_box handling:
135 * - only one bw_penalty_box for all interfaces
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900136 * E.g Adding an app:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700137 * iptables -I bw_penalty_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700138 * --jump REJECT --reject-with icmp-port-unreachable
139 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700140 * * bw_happy_box handling:
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900141 * - The bw_happy_box comes after the penalty box.
JP Abgralle4788732013-07-02 20:28:45 -0700142 * E.g Adding a happy app,
JP Abgrall7e51cde2013-07-03 13:33:05 -0700143 * iptables -I bw_happy_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700144 * --jump RETURN
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900145 *
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900146 * * bw_data_saver handling:
147 * - The bw_data_saver comes after the happy box.
148 * Enable data saver:
149 * iptables -R 1 bw_data_saver --jump REJECT --reject-with icmp-port-unreachable
150 * Disable data saver:
151 * iptables -R 1 bw_data_saver --jump RETURN
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700152 */
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900153
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900154const std::string COMMIT_AND_CLOSE = "COMMIT\n";
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900155const std::string HAPPY_BOX_WHITELIST_COMMAND = StringPrintf(
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900156 "-I bw_happy_box -m owner --uid-owner %d-%d --jump RETURN", 0, MAX_SYSTEM_UID);
157
158static const std::vector<std::string> IPT_FLUSH_COMMANDS = {
JP Abgrall0031cea2012-04-17 16:38:23 -0700159 /*
160 * Cleanup rules.
JP Abgrall7e51cde2013-07-03 13:33:05 -0700161 * Should normally include bw_costly_<iface>, but we rely on the way they are setup
JP Abgrall0031cea2012-04-17 16:38:23 -0700162 * to allow coexistance.
JP Abgrall39f8f242011-06-29 19:21:58 -0700163 */
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900164 "*filter",
165 ":bw_INPUT -",
166 ":bw_OUTPUT -",
167 ":bw_FORWARD -",
168 ":bw_happy_box -",
169 ":bw_penalty_box -",
170 ":bw_data_saver -",
171 ":bw_costly_shared -",
172 "COMMIT",
173 "*raw",
174 ":bw_raw_PREROUTING -",
175 "COMMIT",
176 "*mangle",
177 ":bw_mangle_POSTROUTING -",
178 COMMIT_AND_CLOSE
JP Abgrall0031cea2012-04-17 16:38:23 -0700179};
180
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900181static const std::vector<std::string> IPT_BASIC_ACCOUNTING_COMMANDS = {
182 "*filter",
JP Abgrall0031cea2012-04-17 16:38:23 -0700183 "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall0031cea2012-04-17 16:38:23 -0700184 "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900185 "-A bw_costly_shared --jump bw_penalty_box",
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900186 "-A bw_penalty_box --jump bw_happy_box",
187 "-A bw_happy_box --jump bw_data_saver",
188 "-A bw_data_saver -j RETURN",
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900189 HAPPY_BOX_WHITELIST_COMMAND,
190 "COMMIT",
191
192 "*raw",
193 "-A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */
194 "COMMIT",
195
196 "*mangle",
197 "-A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */
198 COMMIT_AND_CLOSE
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900199};
200
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900201std::vector<std::string> toStrVec(int num, char* strs[]) {
202 std::vector<std::string> tmp;
203 for (int i = 0; i < num; ++i) {
204 tmp.emplace_back(strs[i]);
205 }
206 return tmp;
207}
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900208
209} // namespace
210
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900211BandwidthController::BandwidthController() {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700212}
213
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900214int BandwidthController::runIpxtablesCmd(const std::string& cmd, IptJumpOp jumpHandling,
JP Abgrallad729ac2012-04-24 23:27:44 -0700215 IptFailureLog failureHandling) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700216 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700217
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900218 ALOGV("runIpxtablesCmd(cmd=%s)", cmd.c_str());
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700219 res |= runIptablesCmd(cmd, jumpHandling, IptIpV4, failureHandling);
220 res |= runIptablesCmd(cmd, jumpHandling, IptIpV6, failureHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700221 return res;
222}
223
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900224int BandwidthController::StrncpyAndCheck(char* buffer, const std::string& src, size_t buffSize) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700225 memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0'
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900226 strncpy(buffer, src.c_str(), buffSize);
JP Abgrall26e0d492011-06-24 19:21:51 -0700227 return buffer[buffSize - 1];
228}
229
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900230int BandwidthController::runIptablesCmd(const std::string& cmd, IptJumpOp jumpHandling,
JP Abgrallad729ac2012-04-24 23:27:44 -0700231 IptIpVer iptVer, IptFailureLog failureHandling) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700232 char buffer[MAX_CMD_LEN];
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700233 const char *argv[MAX_CMD_ARGS];
JP Abgrall26e0d492011-06-24 19:21:51 -0700234 int argc = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700235 char *next = buffer;
236 char *tmp;
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700237 int res;
Rom Lemarchand14150212013-01-24 10:01:04 -0800238 int status = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700239
JP Abgrall0dad7c22011-06-24 11:58:14 -0700240 std::string fullCmd = cmd;
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900241 fullCmd += jumpToString(jumpHandling);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700242
Paul Jensen94b2ab92015-08-04 10:35:05 -0400243 fullCmd.insert(0, " -w ");
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700244 fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700245
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900246 if (StrncpyAndCheck(buffer, fullCmd, sizeof(buffer))) {
Rom Lemarchand14150212013-01-24 10:01:04 -0800247 ALOGE("iptables command too long");
248 return -1;
249 }
250
251 argc = 0;
252 while ((tmp = strsep(&next, " "))) {
253 argv[argc++] = tmp;
254 if (argc >= MAX_CMD_ARGS) {
255 ALOGE("iptables argument overflow");
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700256 return -1;
257 }
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700258 }
Rom Lemarchand14150212013-01-24 10:01:04 -0800259
260 argv[argc] = NULL;
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900261 res = execFunction(argc, (char **)argv, &status, false,
Rom Lemarchand14150212013-01-24 10:01:04 -0800262 failureHandling == IptFailShow);
JP Abgrallc8dc63b2013-02-13 16:30:00 -0800263 res = res || !WIFEXITED(status) || WEXITSTATUS(status);
264 if (res && failureHandling == IptFailShow) {
265 ALOGE("runIptablesCmd(): res=%d status=%d failed %s", res, status,
266 fullCmd.c_str());
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700267 }
268 return res;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700269}
270
JP Abgrall0e540ec2013-08-26 15:13:10 -0700271void BandwidthController::flushCleanTables(bool doClean) {
272 /* Flush and remove the bw_costly_<iface> tables */
273 flushExistingCostlyTables(doClean);
JP Abgrall0031cea2012-04-17 16:38:23 -0700274
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900275 std::string commands = Join(IPT_FLUSH_COMMANDS, '\n');
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900276 iptablesRestoreFunction(V4V6, commands, nullptr);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700277}
JP Abgrall0031cea2012-04-17 16:38:23 -0700278
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900279int BandwidthController::setupIptablesHooks() {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700280 /* flush+clean is allowed to fail */
281 flushCleanTables(true);
JP Abgrall0031cea2012-04-17 16:38:23 -0700282 return 0;
JP Abgrall0031cea2012-04-17 16:38:23 -0700283}
284
285int BandwidthController::enableBandwidthControl(bool force) {
JP Abgrall0031cea2012-04-17 16:38:23 -0700286 char value[PROPERTY_VALUE_MAX];
287
288 if (!force) {
289 property_get("persist.bandwidth.enable", value, "1");
290 if (!strcmp(value, "0"))
291 return 0;
292 }
JP Abgrall8a932722011-07-13 19:17:35 -0700293
JP Abgralldb7da582011-09-18 12:57:32 -0700294 /* Let's pretend we started from scratch ... */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900295 mSharedQuotaIfaces.clear();
296 mQuotaIfaces.clear();
297 mGlobalAlertBytes = 0;
298 mGlobalAlertTetherCount = 0;
299 mSharedQuotaBytes = mSharedAlertBytes = 0;
JP Abgralldb7da582011-09-18 12:57:32 -0700300
JP Abgrall0e540ec2013-08-26 15:13:10 -0700301 flushCleanTables(false);
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900302 std::string commands = Join(IPT_BASIC_ACCOUNTING_COMMANDS, '\n');
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900303 return iptablesRestoreFunction(V4V6, commands, nullptr);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700304}
305
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900306int BandwidthController::disableBandwidthControl() {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700307
308 flushCleanTables(false);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700309 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700310}
311
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900312int BandwidthController::enableDataSaver(bool enable) {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900313 std::string cmd = StringPrintf(
314 "*filter\n"
315 "-R bw_data_saver 1%s\n"
316 "COMMIT\n", jumpToString(enable ? IptJumpReject : IptJumpReturn));
317 return iptablesRestoreFunction(V4V6, cmd, nullptr);
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900318}
319
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700320int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900321 return manipulateSpecialApps(toStrVec(numUids, appUids), NAUGHTY_CHAIN,
322 IptJumpReject, IptOpInsert);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700323}
324
325int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900326 return manipulateSpecialApps(toStrVec(numUids, appUids), NAUGHTY_CHAIN,
327 IptJumpReject, IptOpDelete);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700328}
329
JP Abgralle4788732013-07-02 20:28:45 -0700330int BandwidthController::addNiceApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900331 return manipulateSpecialApps(toStrVec(numUids, appUids), NICE_CHAIN,
332 IptJumpReturn, IptOpInsert);
JP Abgralle4788732013-07-02 20:28:45 -0700333}
334
335int BandwidthController::removeNiceApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900336 return manipulateSpecialApps(toStrVec(numUids, appUids), NICE_CHAIN,
337 IptJumpReturn, IptOpDelete);
JP Abgralle4788732013-07-02 20:28:45 -0700338}
339
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900340int BandwidthController::manipulateSpecialApps(const std::vector<std::string>& appStrUids,
341 const std::string& chain, IptJumpOp jumpHandling,
342 IptOp op) {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900343 std::string cmd = "*filter\n";
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900344 for (const auto& appStrUid : appStrUids) {
345 StringAppendF(&cmd, "%s %s -m owner --uid-owner %s%s\n", opToString(op), chain.c_str(),
346 appStrUid.c_str(), jumpToString(jumpHandling));
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700347 }
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900348 StringAppendF(&cmd, "COMMIT\n");
349 return iptablesRestoreFunction(V4V6, cmd, nullptr);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700350}
351
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900352int BandwidthController::setInterfaceSharedQuota(const std::string& iface, int64_t maxBytes) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700353 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700354 std::string quotaCmd;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900355 constexpr char cost[] = "shared";
356 constexpr char chain[] = "bw_costly_shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700357
JP Abgrall8a932722011-07-13 19:17:35 -0700358 if (!maxBytes) {
359 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000360 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700361 return -1;
362 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700363 if (!isIfaceName(iface))
364 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700365
366 if (maxBytes == -1) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900367 return removeInterfaceSharedQuota(iface);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700368 }
369
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900370 auto it = mSharedQuotaIfaces.find(iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700371
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900372 if (it == mSharedQuotaIfaces.end()) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900373 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
374 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900375 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900376 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(), chain),
377 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(), chain),
378 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
379 };
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900380 if (mSharedQuotaIfaces.empty()) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900381 cmds.push_back(StringPrintf("-I %s -m quota2 ! --quota %" PRId64
382 " --name %s --jump REJECT",
383 chain, maxBytes, cost));
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900384 }
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900385 cmds.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900386
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900387 res |= iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900388 if (res) {
389 ALOGE("Failed set quota rule");
390 removeInterfaceSharedQuota(iface);
391 return -1;
392 }
393 mSharedQuotaBytes = maxBytes;
394 mSharedQuotaIfaces.insert(iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700395 }
396
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900397 if (maxBytes != mSharedQuotaBytes) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900398 res |= updateQuota(cost, maxBytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700399 if (res) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900400 ALOGE("Failed update quota for %s", cost);
401 removeInterfaceSharedQuota(iface);
402 return -1;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700403 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900404 mSharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700405 }
406 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700407}
408
JP Abgrall8a932722011-07-13 19:17:35 -0700409/* It will also cleanup any shared alerts */
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900410int BandwidthController::removeInterfaceSharedQuota(const std::string& iface) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900411 constexpr char cost[] = "shared";
412 constexpr char chain[] = "bw_costly_shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700413
JP Abgrall69261cb2014-06-19 18:35:24 -0700414 if (!isIfaceName(iface))
415 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700416
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900417 auto it = mSharedQuotaIfaces.find(iface);
418
419 if (it == mSharedQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900420 ALOGE("No such iface %s to delete", iface.c_str());
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700421 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700422 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700423
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900424 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900425 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900426 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain),
427 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain),
428 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
429 };
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900430 if (mSharedQuotaIfaces.size() == 1) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900431 cmds.push_back(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64
432 " --name %s --jump REJECT",
433 chain, mSharedQuotaBytes, cost));
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700434 }
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900435 cmds.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900436
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900437 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
438 ALOGE("Failed to remove shared quota on %s", iface.c_str());
439 return -1;
440 }
441
442 int res = 0;
443 mSharedQuotaIfaces.erase(it);
444 if (mSharedQuotaIfaces.empty()) {
445 mSharedQuotaBytes = 0;
446 if (mSharedAlertBytes) {
447 res = removeSharedAlert();
448 if (res == 0) {
449 mSharedAlertBytes = 0;
450 }
451 }
452 }
453
454 return res;
455
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700456}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700457
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900458int BandwidthController::setInterfaceQuota(const std::string& iface, int64_t maxBytes) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900459 const std::string& cost = iface;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700460
JP Abgrall69261cb2014-06-19 18:35:24 -0700461 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700462 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700463
JP Abgrall8a932722011-07-13 19:17:35 -0700464 if (!maxBytes) {
465 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000466 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700467 return -1;
468 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700469 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700470 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700471 }
472
JP Abgrall0dad7c22011-06-24 11:58:14 -0700473 /* Insert ingress quota. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900474 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700475
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900476 if (it != mQuotaIfaces.end()) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900477 if (updateQuota(cost, maxBytes) != 0) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900478 ALOGE("Failed update quota for %s", iface.c_str());
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900479 removeInterfaceQuota(iface);
480 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700481 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900482 it->second.quota = maxBytes;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900483 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700484 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700485
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900486 const std::string chain = "bw_costly_" + iface;
487 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
488 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900489 "*filter",
490 StringPrintf(":%s -", chain.c_str()),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900491 StringPrintf("-A %s -j bw_penalty_box", chain.c_str()),
492 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(),
493 chain.c_str()),
494 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(),
495 chain.c_str()),
496 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
497 StringPrintf("-A %s -m quota2 ! --quota %" PRId64 " --name %s --jump REJECT",
498 chain.c_str(), maxBytes, cost.c_str()),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900499 "COMMIT\n",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900500 };
501
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900502 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900503 ALOGE("Failed set quota rule");
504 removeInterfaceQuota(iface);
505 return -1;
506 }
507
508 mQuotaIfaces[iface] = QuotaInfo{maxBytes, 0};
509 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700510}
511
JP Abgrall8a932722011-07-13 19:17:35 -0700512int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
513 return getInterfaceQuota("shared", bytes);
514}
515
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900516int BandwidthController::getInterfaceQuota(const std::string& iface, int64_t* bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900517 const auto& sys = android::netdutils::sSyscalls.get();
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900518 const std::string fname = "/proc/net/xt_quota/" + iface;
JP Abgrall8a932722011-07-13 19:17:35 -0700519
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900520 if (!isIfaceName(iface)) return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700521
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900522 StatusOr<UniqueFile> file = sys.fopen(fname, "re");
523 if (!isOk(file)) {
524 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700525 return -1;
526 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900527 auto rv = sys.fscanf(file.value().get(), "%" SCNd64, bytes);
528 if (!isOk(rv)) {
529 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(rv).c_str());
530 return -1;
531 }
532 ALOGV("Read quota res=%d bytes=%" PRId64, rv.value(), *bytes);
533 return rv.value() == 1 ? 0 : -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700534}
535
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900536int BandwidthController::removeInterfaceQuota(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700537 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700538 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700539
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900540 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700541
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900542 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900543 ALOGE("No such iface %s to delete", iface.c_str());
JP Abgrall0dad7c22011-06-24 11:58:14 -0700544 return -1;
545 }
546
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900547 const std::string chain = "bw_costly_" + iface;
548 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900549 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900550 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain.c_str()),
551 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain.c_str()),
552 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
553 StringPrintf("-F %s", chain.c_str()),
554 StringPrintf("-X %s", chain.c_str()),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900555 "COMMIT\n",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900556 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900557
558 const int res = iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700559
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900560 if (res == 0) {
561 mQuotaIfaces.erase(it);
562 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700563
564 return res;
565}
JP Abgrall8a932722011-07-13 19:17:35 -0700566
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900567int BandwidthController::updateQuota(const std::string& quotaName, int64_t bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900568 const auto& sys = android::netdutils::sSyscalls.get();
569 const std::string fname = "/proc/net/xt_quota/" + quotaName;
JP Abgrall8a932722011-07-13 19:17:35 -0700570
JP Abgrall69261cb2014-06-19 18:35:24 -0700571 if (!isIfaceName(quotaName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900572 ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700573 return -1;
574 }
575
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900576 StatusOr<UniqueFile> file = sys.fopen(fname, "we");
577 if (!isOk(file)) {
578 ALOGE("Updating quota %s failed (%s)", quotaName.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700579 return -1;
580 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900581 sys.fprintf(file.value().get(), "%" PRId64 "\n", bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700582 return 0;
583}
584
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900585int BandwidthController::runIptablesAlertCmd(IptOp op, const std::string& alertName,
586 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900587 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900588 std::string alertQuotaCmd = "*filter\n";
JP Abgrall8a932722011-07-13 19:17:35 -0700589
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900590 // TODO: consider using an alternate template for the delete that does not include the --quota
591 // value. This code works because the --quota value is ignored by deletes
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900592 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT", bytes,
593 alertName.c_str());
594 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT", bytes,
595 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900596 StringAppendF(&alertQuotaCmd, "COMMIT\n");
597
Lorenzo Colitti4773cb42017-04-27 14:03:25 +0900598 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrall8a932722011-07-13 19:17:35 -0700599}
600
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900601int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const std::string& alertName,
602 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900603 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900604 std::string alertQuotaCmd = "*filter\n";
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900605 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD", bytes,
606 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900607 StringAppendF(&alertQuotaCmd, "COMMIT\n");
608
609 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrallc6c67342011-10-07 16:28:54 -0700610}
611
612int BandwidthController::setGlobalAlert(int64_t bytes) {
613 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700614 int res = 0;
615
616 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000617 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700618 return -1;
619 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900620 if (mGlobalAlertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700621 res = updateQuota(alertName, bytes);
622 } else {
623 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900624 if (mGlobalAlertTetherCount) {
625 ALOGV("setGlobalAlert for %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700626 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
627 }
JP Abgrall8a932722011-07-13 19:17:35 -0700628 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900629 mGlobalAlertBytes = bytes;
JP Abgrall8a932722011-07-13 19:17:35 -0700630 return res;
631}
632
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900633int BandwidthController::setGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700634 const char *alertName = ALERT_GLOBAL_NAME;
635 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700636
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900637 mGlobalAlertTetherCount++;
638 ALOGV("setGlobalAlertInForwardChain(): %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700639
640 /*
641 * If there is no globalAlert active we are done.
642 * If there is an active globalAlert but this is not the 1st
643 * tether, we are also done.
644 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900645 if (!mGlobalAlertBytes || mGlobalAlertTetherCount != 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700646 return 0;
647 }
648
649 /* We only add the rule if this was the 1st tether added. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900650 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700651 return res;
652}
653
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900654int BandwidthController::removeGlobalAlert() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700655
656 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700657 int res = 0;
658
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900659 if (!mGlobalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000660 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700661 return -1;
662 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900663 res = runIptablesAlertCmd(IptOpDelete, alertName, mGlobalAlertBytes);
664 if (mGlobalAlertTetherCount) {
665 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700666 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900667 mGlobalAlertBytes = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700668 return res;
669}
670
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900671int BandwidthController::removeGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700672 int res = 0;
673 const char *alertName = ALERT_GLOBAL_NAME;
674
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900675 if (!mGlobalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000676 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700677 return -1;
678 }
679
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900680 mGlobalAlertTetherCount--;
JP Abgrallc6c67342011-10-07 16:28:54 -0700681 /*
682 * If there is no globalAlert active we are done.
683 * If there is an active globalAlert but there are more
684 * tethers, we are also done.
685 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900686 if (!mGlobalAlertBytes || mGlobalAlertTetherCount >= 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700687 return 0;
688 }
689
690 /* We only detete the rule if this was the last tether removed. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900691 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700692 return res;
693}
694
JP Abgrall8a932722011-07-13 19:17:35 -0700695int BandwidthController::setSharedAlert(int64_t bytes) {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900696 if (!mSharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000697 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700698 return -1;
699 }
700 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000701 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700702 return -1;
703 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900704 return setCostlyAlert("shared", bytes, &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700705}
706
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900707int BandwidthController::removeSharedAlert() {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900708 return removeCostlyAlert("shared", &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700709}
710
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900711int BandwidthController::setInterfaceAlert(const std::string& iface, int64_t bytes) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700712 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900713 ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700714 return -1;
715 }
716
JP Abgrall8a932722011-07-13 19:17:35 -0700717 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000718 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700719 return -1;
720 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900721 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700722
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900723 if (it == mQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000724 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700725 return -1;
726 }
727
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900728 return setCostlyAlert(iface, bytes, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700729}
730
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900731int BandwidthController::removeInterfaceAlert(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700732 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900733 ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700734 return -1;
735 }
736
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900737 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700738
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900739 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900740 ALOGE("No prior alert set for interface %s", iface.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700741 return -1;
742 }
743
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900744 return removeCostlyAlert(iface, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700745}
746
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900747int BandwidthController::setCostlyAlert(const std::string& costName, int64_t bytes,
748 int64_t* alertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700749 char *alertQuotaCmd;
JP Abgrall109899b2013-02-12 19:20:13 -0800750 char *chainName;
JP Abgrall8a932722011-07-13 19:17:35 -0700751 int res = 0;
752 char *alertName;
753
JP Abgrall69261cb2014-06-19 18:35:24 -0700754 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900755 ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700756 return -1;
757 }
758
JP Abgrall8a932722011-07-13 19:17:35 -0700759 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000760 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700761 return -1;
762 }
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900763 asprintf(&alertName, "%sAlert", costName.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700764 if (*alertBytes) {
765 res = updateQuota(alertName, *alertBytes);
766 } else {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900767 asprintf(&chainName, "bw_costly_%s", costName.c_str());
JP Abgrall109899b2013-02-12 19:20:13 -0800768 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700769 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700770 free(alertQuotaCmd);
JP Abgrall109899b2013-02-12 19:20:13 -0800771 free(chainName);
JP Abgrall8a932722011-07-13 19:17:35 -0700772 }
773 *alertBytes = bytes;
774 free(alertName);
775 return res;
776}
777
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900778int BandwidthController::removeCostlyAlert(const std::string& costName, int64_t* alertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700779 char *alertQuotaCmd;
780 char *chainName;
781 char *alertName;
782 int res = 0;
783
JP Abgrall69261cb2014-06-19 18:35:24 -0700784 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900785 ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700786 return -1;
787 }
788
JP Abgrall8a932722011-07-13 19:17:35 -0700789 if (!*alertBytes) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900790 ALOGE("No prior alert set for %s alert", costName.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700791 return -1;
792 }
793
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900794 asprintf(&alertName, "%sAlert", costName.c_str());
795 asprintf(&chainName, "bw_costly_%s", costName.c_str());
JP Abgrall92009c82013-02-06 18:01:24 -0800796 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700797 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700798 free(alertQuotaCmd);
799 free(chainName);
800
801 *alertBytes = 0;
802 free(alertName);
803 return res;
804}
JP Abgralldb7da582011-09-18 12:57:32 -0700805
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900806void BandwidthController::addStats(TetherStatsList& statsList, const TetherStats& stats) {
807 for (TetherStats& existing : statsList) {
808 if (existing.addStatsIfMatch(stats)) {
809 return;
810 }
811 }
812 // No match. Insert a new interface pair.
813 statsList.push_back(stats);
814}
815
JP Abgralldb7da582011-09-18 12:57:32 -0700816/*
817 * Parse the ptks and bytes out of:
JP Abgrallbaeccc42013-06-25 09:44:10 -0700818 * Chain natctrl_tether_counters (4 references)
819 * pkts bytes target prot opt in out source destination
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700820 * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
821 * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0
822 * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0
823 * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900824 * or:
825 * Chain natctrl_tether_counters (0 references)
826 * pkts bytes target prot opt in out source destination
827 * 0 0 RETURN all wlan0 rmnet_data0 ::/0 ::/0
828 * 0 0 RETURN all rmnet_data0 wlan0 ::/0 ::/0
829 *
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700830 * It results in an error if invoked and no tethering counter rules exist. The constraint
831 * helps detect complete parsing failure.
JP Abgralldb7da582011-09-18 12:57:32 -0700832 */
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900833int BandwidthController::addForwardChainStats(const TetherStats& filter,
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900834 TetherStatsList& statsList,
835 const std::string& statsOutput,
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900836 std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -0700837 int res;
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900838 std::string statsLine;
JP Abgralldb7da582011-09-18 12:57:32 -0700839 char iface0[MAX_IPT_OUTPUT_LINE_LEN];
840 char iface1[MAX_IPT_OUTPUT_LINE_LEN];
841 char rest[MAX_IPT_OUTPUT_LINE_LEN];
842
JP Abgrallbaeccc42013-06-25 09:44:10 -0700843 TetherStats stats;
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900844 const char *buffPtr;
JP Abgralldb7da582011-09-18 12:57:32 -0700845 int64_t packets, bytes;
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700846 int statsFound = 0;
JP Abgrallbaeccc42013-06-25 09:44:10 -0700847
848 bool filterPair = filter.intIface[0] && filter.extIface[0];
849
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900850 ALOGV("filter: %s", filter.getStatsLine().c_str());
JP Abgrallbaeccc42013-06-25 09:44:10 -0700851
852 stats = filter;
JP Abgralldb7da582011-09-18 12:57:32 -0700853
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900854 std::stringstream stream(statsOutput);
855 while (std::getline(stream, statsLine, '\n')) {
856 buffPtr = statsLine.c_str();
857
JP Abgralldb7da582011-09-18 12:57:32 -0700858 /* Clean up, so a failed parse can still print info */
859 iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900860 if (strstr(buffPtr, "0.0.0.0")) {
861 // IPv4 has -- indicating what to do with fragments...
862 // 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
863 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s",
864 &packets, &bytes, iface0, iface1, rest);
865 } else {
866 // ... but IPv6 does not.
867 // 26 2373 RETURN all wlan0 rmnet0 ::/0 ::/0
868 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all %s %s ::/%s",
869 &packets, &bytes, iface0, iface1, rest);
870 }
SynergyDev7776cea2014-03-16 15:48:51 -0700871 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 -0700872 iface0, iface1, packets, bytes, rest, buffPtr);
JP Abgralla2a64f02011-11-11 20:36:16 -0800873 extraProcessingInfo += buffPtr;
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900874 extraProcessingInfo += "\n";
JP Abgralla2a64f02011-11-11 20:36:16 -0800875
JP Abgralldb7da582011-09-18 12:57:32 -0700876 if (res != 5) {
877 continue;
878 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700879 /*
880 * The following assumes that the 1st rule has in:extIface out:intIface,
881 * which is what NatController sets up.
882 * If not filtering, the 1st match rx, and sets up the pair for the tx side.
883 */
884 if (filter.intIface[0] && filter.extIface[0]) {
885 if (filter.intIface == iface0 && filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -0700886 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 -0700887 stats.rxPackets = packets;
888 stats.rxBytes = bytes;
889 } else if (filter.intIface == iface1 && filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -0700890 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 -0700891 stats.txPackets = packets;
892 stats.txBytes = bytes;
893 }
894 } else if (filter.intIface[0] || filter.extIface[0]) {
895 if (filter.intIface == iface0 || filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -0700896 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 -0700897 stats.intIface = iface0;
898 stats.extIface = iface1;
899 stats.rxPackets = packets;
900 stats.rxBytes = bytes;
901 } else if (filter.intIface == iface1 || filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -0700902 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 -0700903 stats.intIface = iface1;
904 stats.extIface = iface0;
905 stats.txPackets = packets;
906 stats.txBytes = bytes;
907 }
908 } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ {
909 if (!stats.intIface[0]) {
SynergyDev7776cea2014-03-16 15:48:51 -0700910 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 -0700911 stats.intIface = iface0;
912 stats.extIface = iface1;
913 stats.rxPackets = packets;
914 stats.rxBytes = bytes;
915 } else if (stats.intIface == iface1 && stats.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -0700916 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 -0700917 stats.txPackets = packets;
918 stats.txBytes = bytes;
919 }
920 }
921 if (stats.rxBytes != -1 && stats.txBytes != -1) {
SynergyDev7776cea2014-03-16 15:48:51 -0700922 ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64" filterPair=%d", stats.rxBytes, stats.txBytes, filterPair);
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900923 addStats(statsList, stats);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700924 if (filterPair) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700925 return 0;
926 } else {
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900927 statsFound++;
JP Abgrallbaeccc42013-06-25 09:44:10 -0700928 stats = filter;
929 }
JP Abgralldb7da582011-09-18 12:57:32 -0700930 }
931 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700932
933 /* It is always an error to find only one side of the stats. */
934 /* It is an error to find nothing when not filtering. */
935 if (((stats.rxBytes == -1) != (stats.txBytes == -1)) ||
936 (!statsFound && !filterPair)) {
937 return -1;
JP Abgrallbaeccc42013-06-25 09:44:10 -0700938 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700939 return 0;
JP Abgralldb7da582011-09-18 12:57:32 -0700940}
941
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900942std::string BandwidthController::TetherStats::getStatsLine() const {
943 std::string msg;
944 StringAppendF(&msg, "%s %s %" PRId64" %" PRId64" %" PRId64" %" PRId64, intIface.c_str(),
945 extIface.c_str(), rxBytes, rxPackets, txBytes, txPackets);
JP Abgralldb7da582011-09-18 12:57:32 -0700946 return msg;
947}
948
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900949int BandwidthController::getTetherStats(SocketClient *cli, TetherStats& filter,
950 std::string &extraProcessingInfo) {
951 int res = 0;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900952
953 TetherStatsList statsList;
954
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900955 for (const IptablesTarget target : {V4, V6}) {
956 std::string statsString;
957 res = iptablesRestoreFunction(target, GET_TETHER_STATS_COMMAND, &statsString);
958 if (res != 0) {
959 ALOGE("Failed to run %s err=%d", GET_TETHER_STATS_COMMAND.c_str(), res);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900960 return -1;
961 }
962
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900963 res = addForwardChainStats(filter, statsList, statsString, extraProcessingInfo);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900964 if (res != 0) {
965 return res;
966 }
967 }
JP Abgralldb7da582011-09-18 12:57:32 -0700968
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900969 if (filter.intIface[0] && filter.extIface[0] && statsList.size() == 1) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900970 cli->sendMsg(ResponseCode::TetheringStatsResult,
971 statsList[0].getStatsLine().c_str(), false);
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900972 } else {
973 for (const auto& stats: statsList) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900974 cli->sendMsg(ResponseCode::TetheringStatsListResult,
975 stats.getStatsLine().c_str(), false);
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900976 }
977 if (res == 0) {
978 cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
979 }
980 }
981
JP Abgralldb7da582011-09-18 12:57:32 -0700982 return res;
983}
JP Abgrall0e540ec2013-08-26 15:13:10 -0700984
985void BandwidthController::flushExistingCostlyTables(bool doClean) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900986 std::string fullCmd = "*filter\n-S\nCOMMIT\n";
987 std::string ruleList;
JP Abgrall0e540ec2013-08-26 15:13:10 -0700988
989 /* Only lookup ip4 table names as ip6 will have the same tables ... */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900990 if (int ret = iptablesRestoreFunction(V4, fullCmd, &ruleList)) {
991 ALOGE("Failed to list existing costly tables ret=%d", ret);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700992 return;
993 }
994 /* ... then flush/clean both ip4 and ip6 iptables. */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900995 parseAndFlushCostlyTables(ruleList, doClean);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700996}
997
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900998void BandwidthController::parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove) {
999 std::stringstream stream(ruleList);
1000 std::string rule;
1001 std::vector<std::string> clearCommands = { "*filter" };
1002 std::string chainName;
JP Abgrall0e540ec2013-08-26 15:13:10 -07001003
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +09001004 // Find and flush all rules starting with "-N bw_costly_<iface>" except "-N bw_costly_shared".
1005 while (std::getline(stream, rule, '\n')) {
1006 if (rule.find(NEW_CHAIN_COMMAND) != 0) continue;
1007 chainName = rule.substr(NEW_CHAIN_COMMAND.size());
1008 ALOGV("parse chainName=<%s> orig line=<%s>", chainName.c_str(), rule.c_str());
1009
1010 if (chainName.find("bw_costly_") != 0 || chainName == std::string("bw_costly_shared")) {
JP Abgrall0e540ec2013-08-26 15:13:10 -07001011 continue;
1012 }
1013
Lorenzo Colitti3c272702017-04-26 15:48:13 +09001014 clearCommands.push_back(StringPrintf(":%s -", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -07001015 if (doRemove) {
Lorenzo Colitti3c272702017-04-26 15:48:13 +09001016 clearCommands.push_back(StringPrintf("-X %s", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -07001017 }
1018 }
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +09001019
1020 if (clearCommands.size() == 1) {
1021 // No rules found.
1022 return;
1023 }
1024
1025 clearCommands.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +09001026 iptablesRestoreFunction(V4V6, Join(clearCommands, '\n'), nullptr);
JP Abgrall0e540ec2013-08-26 15:13:10 -07001027}
Lorenzo Colittid9db08c2017-04-28 11:06:40 +09001028
1029inline const char *BandwidthController::opToString(IptOp op) {
1030 switch (op) {
1031 case IptOpInsert:
1032 return "-I";
1033 case IptOpDelete:
1034 return "-D";
1035 }
1036}
1037
1038inline const char *BandwidthController::jumpToString(IptJumpOp jumpHandling) {
1039 /*
1040 * Must be careful what one rejects with, as upper layer protocols will just
1041 * keep on hammering the device until the number of retries are done.
1042 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
1043 */
1044 switch (jumpHandling) {
1045 case IptJumpNoAdd:
1046 return "";
1047 case IptJumpReject:
1048 return " --jump REJECT";
1049 case IptJumpReturn:
1050 return " --jump RETURN";
1051 }
1052}