blob: 2468cd9c48b3345f3597124de63a3fd308a91366 [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 Scherpelzced1dd92017-06-28 10:19:52 +0900424 mSharedQuotaIfaces.erase(it);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700425
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900426 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900427 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900428 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain),
429 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain),
430 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
431 };
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900432 if (mSharedQuotaIfaces.empty()) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900433 cmds.push_back(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64
434 " --name %s --jump REJECT",
435 chain, mSharedQuotaBytes, cost));
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900436
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900437 mSharedQuotaBytes = 0;
438 if (mSharedAlertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700439 removeSharedAlert();
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900440 mSharedAlertBytes = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700441 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700442 }
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900443 cmds.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900444
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900445 return iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700446}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700447
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900448int BandwidthController::setInterfaceQuota(const std::string& iface, int64_t maxBytes) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900449 const std::string& cost = iface;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700450
JP Abgrall69261cb2014-06-19 18:35:24 -0700451 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700452 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700453
JP Abgrall8a932722011-07-13 19:17:35 -0700454 if (!maxBytes) {
455 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000456 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700457 return -1;
458 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700459 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700460 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700461 }
462
JP Abgrall0dad7c22011-06-24 11:58:14 -0700463 /* Insert ingress quota. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900464 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700465
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900466 if (it != mQuotaIfaces.end()) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900467 if (updateQuota(cost, maxBytes) != 0) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900468 ALOGE("Failed update quota for %s", iface.c_str());
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900469 removeInterfaceQuota(iface);
470 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700471 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900472 it->second.quota = maxBytes;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900473 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700474 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700475
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900476 const std::string chain = "bw_costly_" + iface;
477 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
478 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900479 "*filter",
480 StringPrintf(":%s -", chain.c_str()),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900481 StringPrintf("-A %s -j bw_penalty_box", chain.c_str()),
482 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(),
483 chain.c_str()),
484 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(),
485 chain.c_str()),
486 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
487 StringPrintf("-A %s -m quota2 ! --quota %" PRId64 " --name %s --jump REJECT",
488 chain.c_str(), maxBytes, cost.c_str()),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900489 "COMMIT\n",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900490 };
491
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900492 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900493 ALOGE("Failed set quota rule");
494 removeInterfaceQuota(iface);
495 return -1;
496 }
497
498 mQuotaIfaces[iface] = QuotaInfo{maxBytes, 0};
499 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700500}
501
JP Abgrall8a932722011-07-13 19:17:35 -0700502int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
503 return getInterfaceQuota("shared", bytes);
504}
505
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900506int BandwidthController::getInterfaceQuota(const std::string& iface, int64_t* bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900507 const auto& sys = android::netdutils::sSyscalls.get();
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900508 const std::string fname = "/proc/net/xt_quota/" + iface;
JP Abgrall8a932722011-07-13 19:17:35 -0700509
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900510 if (!isIfaceName(iface)) return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700511
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900512 StatusOr<UniqueFile> file = sys.fopen(fname, "re");
513 if (!isOk(file)) {
514 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700515 return -1;
516 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900517 auto rv = sys.fscanf(file.value().get(), "%" SCNd64, bytes);
518 if (!isOk(rv)) {
519 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(rv).c_str());
520 return -1;
521 }
522 ALOGV("Read quota res=%d bytes=%" PRId64, rv.value(), *bytes);
523 return rv.value() == 1 ? 0 : -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700524}
525
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900526int BandwidthController::removeInterfaceQuota(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700527 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700528 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700529
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900530 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700531
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900532 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900533 ALOGE("No such iface %s to delete", iface.c_str());
JP Abgrall0dad7c22011-06-24 11:58:14 -0700534 return -1;
535 }
536
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900537 const std::string chain = "bw_costly_" + iface;
538 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900539 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900540 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain.c_str()),
541 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain.c_str()),
542 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
543 StringPrintf("-F %s", chain.c_str()),
544 StringPrintf("-X %s", chain.c_str()),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900545 "COMMIT\n",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900546 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900547
548 const int res = iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700549
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900550 mQuotaIfaces.erase(it);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700551
552 return res;
553}
JP Abgrall8a932722011-07-13 19:17:35 -0700554
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900555int BandwidthController::updateQuota(const std::string& quotaName, int64_t bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900556 const auto& sys = android::netdutils::sSyscalls.get();
557 const std::string fname = "/proc/net/xt_quota/" + quotaName;
JP Abgrall8a932722011-07-13 19:17:35 -0700558
JP Abgrall69261cb2014-06-19 18:35:24 -0700559 if (!isIfaceName(quotaName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900560 ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700561 return -1;
562 }
563
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900564 StatusOr<UniqueFile> file = sys.fopen(fname, "we");
565 if (!isOk(file)) {
566 ALOGE("Updating quota %s failed (%s)", quotaName.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700567 return -1;
568 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900569 sys.fprintf(file.value().get(), "%" PRId64 "\n", bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700570 return 0;
571}
572
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900573int BandwidthController::runIptablesAlertCmd(IptOp op, const std::string& alertName,
574 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900575 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900576 std::string alertQuotaCmd = "*filter\n";
JP Abgrall8a932722011-07-13 19:17:35 -0700577
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900578 // TODO: consider using an alternate template for the delete that does not include the --quota
579 // value. This code works because the --quota value is ignored by deletes
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900580 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT", bytes,
581 alertName.c_str());
582 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT", bytes,
583 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900584 StringAppendF(&alertQuotaCmd, "COMMIT\n");
585
Lorenzo Colitti4773cb42017-04-27 14:03:25 +0900586 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrall8a932722011-07-13 19:17:35 -0700587}
588
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900589int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const std::string& alertName,
590 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900591 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900592 std::string alertQuotaCmd = "*filter\n";
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900593 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD", bytes,
594 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900595 StringAppendF(&alertQuotaCmd, "COMMIT\n");
596
597 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrallc6c67342011-10-07 16:28:54 -0700598}
599
600int BandwidthController::setGlobalAlert(int64_t bytes) {
601 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700602 int res = 0;
603
604 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000605 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700606 return -1;
607 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900608 if (mGlobalAlertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700609 res = updateQuota(alertName, bytes);
610 } else {
611 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900612 if (mGlobalAlertTetherCount) {
613 ALOGV("setGlobalAlert for %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700614 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
615 }
JP Abgrall8a932722011-07-13 19:17:35 -0700616 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900617 mGlobalAlertBytes = bytes;
JP Abgrall8a932722011-07-13 19:17:35 -0700618 return res;
619}
620
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900621int BandwidthController::setGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700622 const char *alertName = ALERT_GLOBAL_NAME;
623 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700624
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900625 mGlobalAlertTetherCount++;
626 ALOGV("setGlobalAlertInForwardChain(): %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700627
628 /*
629 * If there is no globalAlert active we are done.
630 * If there is an active globalAlert but this is not the 1st
631 * tether, we are also done.
632 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900633 if (!mGlobalAlertBytes || mGlobalAlertTetherCount != 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700634 return 0;
635 }
636
637 /* We only add the rule if this was the 1st tether added. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900638 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700639 return res;
640}
641
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900642int BandwidthController::removeGlobalAlert() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700643
644 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700645 int res = 0;
646
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900647 if (!mGlobalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000648 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700649 return -1;
650 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900651 res = runIptablesAlertCmd(IptOpDelete, alertName, mGlobalAlertBytes);
652 if (mGlobalAlertTetherCount) {
653 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700654 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900655 mGlobalAlertBytes = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700656 return res;
657}
658
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900659int BandwidthController::removeGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700660 int res = 0;
661 const char *alertName = ALERT_GLOBAL_NAME;
662
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900663 if (!mGlobalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000664 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700665 return -1;
666 }
667
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900668 mGlobalAlertTetherCount--;
JP Abgrallc6c67342011-10-07 16:28:54 -0700669 /*
670 * If there is no globalAlert active we are done.
671 * If there is an active globalAlert but there are more
672 * tethers, we are also done.
673 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900674 if (!mGlobalAlertBytes || mGlobalAlertTetherCount >= 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700675 return 0;
676 }
677
678 /* We only detete the rule if this was the last tether removed. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900679 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700680 return res;
681}
682
JP Abgrall8a932722011-07-13 19:17:35 -0700683int BandwidthController::setSharedAlert(int64_t bytes) {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900684 if (!mSharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000685 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700686 return -1;
687 }
688 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000689 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700690 return -1;
691 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900692 return setCostlyAlert("shared", bytes, &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700693}
694
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900695int BandwidthController::removeSharedAlert() {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900696 return removeCostlyAlert("shared", &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700697}
698
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900699int BandwidthController::setInterfaceAlert(const std::string& iface, int64_t bytes) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700700 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900701 ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700702 return -1;
703 }
704
JP Abgrall8a932722011-07-13 19:17:35 -0700705 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000706 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700707 return -1;
708 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900709 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700710
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900711 if (it == mQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000712 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700713 return -1;
714 }
715
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900716 return setCostlyAlert(iface, bytes, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700717}
718
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900719int BandwidthController::removeInterfaceAlert(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700720 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900721 ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700722 return -1;
723 }
724
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900725 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700726
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900727 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900728 ALOGE("No prior alert set for interface %s", iface.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700729 return -1;
730 }
731
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900732 return removeCostlyAlert(iface, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700733}
734
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900735int BandwidthController::setCostlyAlert(const std::string& costName, int64_t bytes,
736 int64_t* alertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700737 char *alertQuotaCmd;
JP Abgrall109899b2013-02-12 19:20:13 -0800738 char *chainName;
JP Abgrall8a932722011-07-13 19:17:35 -0700739 int res = 0;
740 char *alertName;
741
JP Abgrall69261cb2014-06-19 18:35:24 -0700742 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900743 ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700744 return -1;
745 }
746
JP Abgrall8a932722011-07-13 19:17:35 -0700747 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000748 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700749 return -1;
750 }
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900751 asprintf(&alertName, "%sAlert", costName.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700752 if (*alertBytes) {
753 res = updateQuota(alertName, *alertBytes);
754 } else {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900755 asprintf(&chainName, "bw_costly_%s", costName.c_str());
JP Abgrall109899b2013-02-12 19:20:13 -0800756 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700757 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700758 free(alertQuotaCmd);
JP Abgrall109899b2013-02-12 19:20:13 -0800759 free(chainName);
JP Abgrall8a932722011-07-13 19:17:35 -0700760 }
761 *alertBytes = bytes;
762 free(alertName);
763 return res;
764}
765
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900766int BandwidthController::removeCostlyAlert(const std::string& costName, int64_t* alertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700767 char *alertQuotaCmd;
768 char *chainName;
769 char *alertName;
770 int res = 0;
771
JP Abgrall69261cb2014-06-19 18:35:24 -0700772 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900773 ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700774 return -1;
775 }
776
JP Abgrall8a932722011-07-13 19:17:35 -0700777 if (!*alertBytes) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900778 ALOGE("No prior alert set for %s alert", costName.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700779 return -1;
780 }
781
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900782 asprintf(&alertName, "%sAlert", costName.c_str());
783 asprintf(&chainName, "bw_costly_%s", costName.c_str());
JP Abgrall92009c82013-02-06 18:01:24 -0800784 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700785 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700786 free(alertQuotaCmd);
787 free(chainName);
788
789 *alertBytes = 0;
790 free(alertName);
791 return res;
792}
JP Abgralldb7da582011-09-18 12:57:32 -0700793
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900794void BandwidthController::addStats(TetherStatsList& statsList, const TetherStats& stats) {
795 for (TetherStats& existing : statsList) {
796 if (existing.addStatsIfMatch(stats)) {
797 return;
798 }
799 }
800 // No match. Insert a new interface pair.
801 statsList.push_back(stats);
802}
803
JP Abgralldb7da582011-09-18 12:57:32 -0700804/*
805 * Parse the ptks and bytes out of:
JP Abgrallbaeccc42013-06-25 09:44:10 -0700806 * Chain natctrl_tether_counters (4 references)
807 * pkts bytes target prot opt in out source destination
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700808 * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
809 * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0
810 * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0
811 * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900812 * or:
813 * Chain natctrl_tether_counters (0 references)
814 * pkts bytes target prot opt in out source destination
815 * 0 0 RETURN all wlan0 rmnet_data0 ::/0 ::/0
816 * 0 0 RETURN all rmnet_data0 wlan0 ::/0 ::/0
817 *
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700818 * It results in an error if invoked and no tethering counter rules exist. The constraint
819 * helps detect complete parsing failure.
JP Abgralldb7da582011-09-18 12:57:32 -0700820 */
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900821int BandwidthController::addForwardChainStats(const TetherStats& filter,
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900822 TetherStatsList& statsList,
823 const std::string& statsOutput,
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900824 std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -0700825 int res;
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900826 std::string statsLine;
JP Abgralldb7da582011-09-18 12:57:32 -0700827 char iface0[MAX_IPT_OUTPUT_LINE_LEN];
828 char iface1[MAX_IPT_OUTPUT_LINE_LEN];
829 char rest[MAX_IPT_OUTPUT_LINE_LEN];
830
JP Abgrallbaeccc42013-06-25 09:44:10 -0700831 TetherStats stats;
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900832 const char *buffPtr;
JP Abgralldb7da582011-09-18 12:57:32 -0700833 int64_t packets, bytes;
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700834 int statsFound = 0;
JP Abgrallbaeccc42013-06-25 09:44:10 -0700835
836 bool filterPair = filter.intIface[0] && filter.extIface[0];
837
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900838 ALOGV("filter: %s", filter.getStatsLine().c_str());
JP Abgrallbaeccc42013-06-25 09:44:10 -0700839
840 stats = filter;
JP Abgralldb7da582011-09-18 12:57:32 -0700841
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900842 std::stringstream stream(statsOutput);
843 while (std::getline(stream, statsLine, '\n')) {
844 buffPtr = statsLine.c_str();
845
JP Abgralldb7da582011-09-18 12:57:32 -0700846 /* Clean up, so a failed parse can still print info */
847 iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900848 if (strstr(buffPtr, "0.0.0.0")) {
849 // IPv4 has -- indicating what to do with fragments...
850 // 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
851 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s",
852 &packets, &bytes, iface0, iface1, rest);
853 } else {
854 // ... but IPv6 does not.
855 // 26 2373 RETURN all wlan0 rmnet0 ::/0 ::/0
856 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all %s %s ::/%s",
857 &packets, &bytes, iface0, iface1, rest);
858 }
SynergyDev7776cea2014-03-16 15:48:51 -0700859 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 -0700860 iface0, iface1, packets, bytes, rest, buffPtr);
JP Abgralla2a64f02011-11-11 20:36:16 -0800861 extraProcessingInfo += buffPtr;
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900862 extraProcessingInfo += "\n";
JP Abgralla2a64f02011-11-11 20:36:16 -0800863
JP Abgralldb7da582011-09-18 12:57:32 -0700864 if (res != 5) {
865 continue;
866 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700867 /*
868 * The following assumes that the 1st rule has in:extIface out:intIface,
869 * which is what NatController sets up.
870 * If not filtering, the 1st match rx, and sets up the pair for the tx side.
871 */
872 if (filter.intIface[0] && filter.extIface[0]) {
873 if (filter.intIface == iface0 && filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -0700874 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 -0700875 stats.rxPackets = packets;
876 stats.rxBytes = bytes;
877 } else if (filter.intIface == iface1 && filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -0700878 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 -0700879 stats.txPackets = packets;
880 stats.txBytes = bytes;
881 }
882 } else if (filter.intIface[0] || filter.extIface[0]) {
883 if (filter.intIface == iface0 || filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -0700884 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 -0700885 stats.intIface = iface0;
886 stats.extIface = iface1;
887 stats.rxPackets = packets;
888 stats.rxBytes = bytes;
889 } else if (filter.intIface == iface1 || filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -0700890 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 -0700891 stats.intIface = iface1;
892 stats.extIface = iface0;
893 stats.txPackets = packets;
894 stats.txBytes = bytes;
895 }
896 } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ {
897 if (!stats.intIface[0]) {
SynergyDev7776cea2014-03-16 15:48:51 -0700898 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 -0700899 stats.intIface = iface0;
900 stats.extIface = iface1;
901 stats.rxPackets = packets;
902 stats.rxBytes = bytes;
903 } else if (stats.intIface == iface1 && stats.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -0700904 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 -0700905 stats.txPackets = packets;
906 stats.txBytes = bytes;
907 }
908 }
909 if (stats.rxBytes != -1 && stats.txBytes != -1) {
SynergyDev7776cea2014-03-16 15:48:51 -0700910 ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64" filterPair=%d", stats.rxBytes, stats.txBytes, filterPair);
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900911 addStats(statsList, stats);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700912 if (filterPair) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700913 return 0;
914 } else {
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900915 statsFound++;
JP Abgrallbaeccc42013-06-25 09:44:10 -0700916 stats = filter;
917 }
JP Abgralldb7da582011-09-18 12:57:32 -0700918 }
919 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700920
921 /* It is always an error to find only one side of the stats. */
922 /* It is an error to find nothing when not filtering. */
923 if (((stats.rxBytes == -1) != (stats.txBytes == -1)) ||
924 (!statsFound && !filterPair)) {
925 return -1;
JP Abgrallbaeccc42013-06-25 09:44:10 -0700926 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700927 return 0;
JP Abgralldb7da582011-09-18 12:57:32 -0700928}
929
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900930std::string BandwidthController::TetherStats::getStatsLine() const {
931 std::string msg;
932 StringAppendF(&msg, "%s %s %" PRId64" %" PRId64" %" PRId64" %" PRId64, intIface.c_str(),
933 extIface.c_str(), rxBytes, rxPackets, txBytes, txPackets);
JP Abgralldb7da582011-09-18 12:57:32 -0700934 return msg;
935}
936
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900937int BandwidthController::getTetherStats(SocketClient *cli, TetherStats& filter,
938 std::string &extraProcessingInfo) {
939 int res = 0;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900940
941 TetherStatsList statsList;
942
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900943 for (const IptablesTarget target : {V4, V6}) {
944 std::string statsString;
945 res = iptablesRestoreFunction(target, GET_TETHER_STATS_COMMAND, &statsString);
946 if (res != 0) {
947 ALOGE("Failed to run %s err=%d", GET_TETHER_STATS_COMMAND.c_str(), res);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900948 return -1;
949 }
950
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900951 res = addForwardChainStats(filter, statsList, statsString, extraProcessingInfo);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900952 if (res != 0) {
953 return res;
954 }
955 }
JP Abgralldb7da582011-09-18 12:57:32 -0700956
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900957 if (filter.intIface[0] && filter.extIface[0] && statsList.size() == 1) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900958 cli->sendMsg(ResponseCode::TetheringStatsResult,
959 statsList[0].getStatsLine().c_str(), false);
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900960 } else {
961 for (const auto& stats: statsList) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900962 cli->sendMsg(ResponseCode::TetheringStatsListResult,
963 stats.getStatsLine().c_str(), false);
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900964 }
965 if (res == 0) {
966 cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
967 }
968 }
969
JP Abgralldb7da582011-09-18 12:57:32 -0700970 return res;
971}
JP Abgrall0e540ec2013-08-26 15:13:10 -0700972
973void BandwidthController::flushExistingCostlyTables(bool doClean) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900974 std::string fullCmd = "*filter\n-S\nCOMMIT\n";
975 std::string ruleList;
JP Abgrall0e540ec2013-08-26 15:13:10 -0700976
977 /* Only lookup ip4 table names as ip6 will have the same tables ... */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900978 if (int ret = iptablesRestoreFunction(V4, fullCmd, &ruleList)) {
979 ALOGE("Failed to list existing costly tables ret=%d", ret);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700980 return;
981 }
982 /* ... then flush/clean both ip4 and ip6 iptables. */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900983 parseAndFlushCostlyTables(ruleList, doClean);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700984}
985
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900986void BandwidthController::parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove) {
987 std::stringstream stream(ruleList);
988 std::string rule;
989 std::vector<std::string> clearCommands = { "*filter" };
990 std::string chainName;
JP Abgrall0e540ec2013-08-26 15:13:10 -0700991
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900992 // Find and flush all rules starting with "-N bw_costly_<iface>" except "-N bw_costly_shared".
993 while (std::getline(stream, rule, '\n')) {
994 if (rule.find(NEW_CHAIN_COMMAND) != 0) continue;
995 chainName = rule.substr(NEW_CHAIN_COMMAND.size());
996 ALOGV("parse chainName=<%s> orig line=<%s>", chainName.c_str(), rule.c_str());
997
998 if (chainName.find("bw_costly_") != 0 || chainName == std::string("bw_costly_shared")) {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700999 continue;
1000 }
1001
Lorenzo Colitti3c272702017-04-26 15:48:13 +09001002 clearCommands.push_back(StringPrintf(":%s -", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -07001003 if (doRemove) {
Lorenzo Colitti3c272702017-04-26 15:48:13 +09001004 clearCommands.push_back(StringPrintf("-X %s", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -07001005 }
1006 }
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +09001007
1008 if (clearCommands.size() == 1) {
1009 // No rules found.
1010 return;
1011 }
1012
1013 clearCommands.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +09001014 iptablesRestoreFunction(V4V6, Join(clearCommands, '\n'), nullptr);
JP Abgrall0e540ec2013-08-26 15:13:10 -07001015}
Lorenzo Colittid9db08c2017-04-28 11:06:40 +09001016
1017inline const char *BandwidthController::opToString(IptOp op) {
1018 switch (op) {
1019 case IptOpInsert:
1020 return "-I";
1021 case IptOpDelete:
1022 return "-D";
1023 }
1024}
1025
1026inline const char *BandwidthController::jumpToString(IptJumpOp jumpHandling) {
1027 /*
1028 * Must be careful what one rejects with, as upper layer protocols will just
1029 * keep on hammering the device until the number of retries are done.
1030 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
1031 */
1032 switch (jumpHandling) {
1033 case IptJumpNoAdd:
1034 return "";
1035 case IptJumpReject:
1036 return " --jump REJECT";
1037 case IptJumpReturn:
1038 return " --jump RETURN";
1039 }
1040}