blob: 89831ff8f17e95e67cd7ca5f9dbc58ef5ad8c2df [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 = {
375 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(), chain),
376 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(), chain),
377 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
378 };
379 for (const auto& cmd : cmds) {
380 res |= runIpxtablesCmd(cmd.c_str(), IptJumpNoAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700381 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700382
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900383 if (mSharedQuotaIfaces.empty()) {
384 res |= runIpxtablesCmd(StringPrintf("-I %s -m quota2 ! --quota %" PRId64
385 " --name %s --jump REJECT",
386 chain, maxBytes, cost).c_str(), IptJumpNoAdd);
387 }
388
389 if (res) {
390 ALOGE("Failed set quota rule");
391 removeInterfaceSharedQuota(iface);
392 return -1;
393 }
394 mSharedQuotaBytes = maxBytes;
395 mSharedQuotaIfaces.insert(iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700396 }
397
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900398 if (maxBytes != mSharedQuotaBytes) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900399 res |= updateQuota(cost, maxBytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700400 if (res) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900401 ALOGE("Failed update quota for %s", cost);
402 removeInterfaceSharedQuota(iface);
403 return -1;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700404 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900405 mSharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700406 }
407 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700408}
409
JP Abgrall8a932722011-07-13 19:17:35 -0700410/* It will also cleanup any shared alerts */
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900411int BandwidthController::removeInterfaceSharedQuota(const std::string& iface) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700412 int res = 0;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900413 constexpr char cost[] = "shared";
414 constexpr char chain[] = "bw_costly_shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700415
JP Abgrall69261cb2014-06-19 18:35:24 -0700416 if (!isIfaceName(iface))
417 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700418
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900419 auto it = mSharedQuotaIfaces.find(iface);
420
421 if (it == mSharedQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900422 ALOGE("No such iface %s to delete", iface.c_str());
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700423 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700424 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700425
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900426 mSharedQuotaIfaces.erase(it);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700427
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900428 std::vector<std::string> cmds = {
429 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain),
430 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain),
431 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
432 };
433 for (const auto& cmd : cmds) {
434 res |= runIpxtablesCmd(cmd.c_str(), IptJumpNoAdd);
435 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900436 if (mSharedQuotaIfaces.empty()) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900437 res |= runIpxtablesCmd(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64
438 " --name %s --jump REJECT",
439 chain, mSharedQuotaBytes, cost).c_str(), IptJumpNoAdd);
440
441
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900442 mSharedQuotaBytes = 0;
443 if (mSharedAlertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700444 removeSharedAlert();
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900445 mSharedAlertBytes = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700446 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700447 }
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900448
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700449 return res;
450}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700451
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900452int BandwidthController::setInterfaceQuota(const std::string& iface, int64_t maxBytes) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700453 int res = 0;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900454 const std::string& cost = iface;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700455
JP Abgrall69261cb2014-06-19 18:35:24 -0700456 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700457 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700458
JP Abgrall8a932722011-07-13 19:17:35 -0700459 if (!maxBytes) {
460 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000461 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700462 return -1;
463 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700464 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700465 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700466 }
467
JP Abgrall0dad7c22011-06-24 11:58:14 -0700468 /* Insert ingress quota. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900469 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700470
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900471 if (it != mQuotaIfaces.end()) {
472 res |= updateQuota(cost, maxBytes);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700473 if (res) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900474 ALOGE("Failed update quota for %s", iface.c_str());
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900475 removeInterfaceQuota(iface);
476 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700477 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900478 it->second.quota = maxBytes;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900479 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700480 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700481
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900482 const std::string chain = "bw_costly_" + iface;
483 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
484 std::vector<std::string> cmds = {
485 StringPrintf("-N %s", chain.c_str()),
486 StringPrintf("-F %s", chain.c_str()),
487 StringPrintf("-A %s -j bw_penalty_box", chain.c_str()),
488 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(),
489 chain.c_str()),
490 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(),
491 chain.c_str()),
492 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
493 StringPrintf("-A %s -m quota2 ! --quota %" PRId64 " --name %s --jump REJECT",
494 chain.c_str(), maxBytes, cost.c_str()),
495 };
496
497 for (const auto& cmd : cmds) {
498 res |= runIpxtablesCmd(cmd.c_str(), IptJumpNoAdd);
499 }
500 if (res) {
501 ALOGE("Failed set quota rule");
502 removeInterfaceQuota(iface);
503 return -1;
504 }
505
506 mQuotaIfaces[iface] = QuotaInfo{maxBytes, 0};
507 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700508}
509
JP Abgrall8a932722011-07-13 19:17:35 -0700510int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
511 return getInterfaceQuota("shared", bytes);
512}
513
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900514int BandwidthController::getInterfaceQuota(const std::string& iface, int64_t* bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900515 const auto& sys = android::netdutils::sSyscalls.get();
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900516 const std::string fname = "/proc/net/xt_quota/" + iface;
JP Abgrall8a932722011-07-13 19:17:35 -0700517
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900518 if (!isIfaceName(iface)) return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700519
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900520 StatusOr<UniqueFile> file = sys.fopen(fname, "re");
521 if (!isOk(file)) {
522 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700523 return -1;
524 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900525 auto rv = sys.fscanf(file.value().get(), "%" SCNd64, bytes);
526 if (!isOk(rv)) {
527 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(rv).c_str());
528 return -1;
529 }
530 ALOGV("Read quota res=%d bytes=%" PRId64, rv.value(), *bytes);
531 return rv.value() == 1 ? 0 : -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700532}
533
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900534int BandwidthController::removeInterfaceQuota(const std::string& iface) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700535 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700536
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 = {
549 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain.c_str()),
550 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain.c_str()),
551 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
552 StringPrintf("-F %s", chain.c_str()),
553 StringPrintf("-X %s", chain.c_str()),
554 };
555 for (const auto& cmd : cmds) {
556 res |= runIpxtablesCmd(cmd.c_str(), IptJumpNoAdd);
557 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700558
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900559 mQuotaIfaces.erase(it);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700560
561 return res;
562}
JP Abgrall8a932722011-07-13 19:17:35 -0700563
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900564int BandwidthController::updateQuota(const std::string& quotaName, int64_t bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900565 const auto& sys = android::netdutils::sSyscalls.get();
566 const std::string fname = "/proc/net/xt_quota/" + quotaName;
JP Abgrall8a932722011-07-13 19:17:35 -0700567
JP Abgrall69261cb2014-06-19 18:35:24 -0700568 if (!isIfaceName(quotaName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900569 ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700570 return -1;
571 }
572
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900573 StatusOr<UniqueFile> file = sys.fopen(fname, "we");
574 if (!isOk(file)) {
575 ALOGE("Updating quota %s failed (%s)", quotaName.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700576 return -1;
577 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900578 sys.fprintf(file.value().get(), "%" PRId64 "\n", bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700579 return 0;
580}
581
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900582int BandwidthController::runIptablesAlertCmd(IptOp op, const std::string& alertName,
583 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900584 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900585 std::string alertQuotaCmd = "*filter\n";
JP Abgrall8a932722011-07-13 19:17:35 -0700586
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900587 // TODO: consider using an alternate template for the delete that does not include the --quota
588 // value. This code works because the --quota value is ignored by deletes
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900589 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT", bytes,
590 alertName.c_str());
591 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT", bytes,
592 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900593 StringAppendF(&alertQuotaCmd, "COMMIT\n");
594
Lorenzo Colitti4773cb42017-04-27 14:03:25 +0900595 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrall8a932722011-07-13 19:17:35 -0700596}
597
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900598int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const std::string& alertName,
599 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900600 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900601 std::string alertQuotaCmd = "*filter\n";
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900602 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD", bytes,
603 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900604 StringAppendF(&alertQuotaCmd, "COMMIT\n");
605
606 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrallc6c67342011-10-07 16:28:54 -0700607}
608
609int BandwidthController::setGlobalAlert(int64_t bytes) {
610 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700611 int res = 0;
612
613 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000614 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700615 return -1;
616 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900617 if (mGlobalAlertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700618 res = updateQuota(alertName, bytes);
619 } else {
620 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900621 if (mGlobalAlertTetherCount) {
622 ALOGV("setGlobalAlert for %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700623 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
624 }
JP Abgrall8a932722011-07-13 19:17:35 -0700625 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900626 mGlobalAlertBytes = bytes;
JP Abgrall8a932722011-07-13 19:17:35 -0700627 return res;
628}
629
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900630int BandwidthController::setGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700631 const char *alertName = ALERT_GLOBAL_NAME;
632 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700633
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900634 mGlobalAlertTetherCount++;
635 ALOGV("setGlobalAlertInForwardChain(): %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700636
637 /*
638 * If there is no globalAlert active we are done.
639 * If there is an active globalAlert but this is not the 1st
640 * tether, we are also done.
641 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900642 if (!mGlobalAlertBytes || mGlobalAlertTetherCount != 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700643 return 0;
644 }
645
646 /* We only add the rule if this was the 1st tether added. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900647 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700648 return res;
649}
650
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900651int BandwidthController::removeGlobalAlert() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700652
653 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700654 int res = 0;
655
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900656 if (!mGlobalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000657 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700658 return -1;
659 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900660 res = runIptablesAlertCmd(IptOpDelete, alertName, mGlobalAlertBytes);
661 if (mGlobalAlertTetherCount) {
662 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700663 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900664 mGlobalAlertBytes = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700665 return res;
666}
667
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900668int BandwidthController::removeGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700669 int res = 0;
670 const char *alertName = ALERT_GLOBAL_NAME;
671
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900672 if (!mGlobalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000673 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700674 return -1;
675 }
676
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900677 mGlobalAlertTetherCount--;
JP Abgrallc6c67342011-10-07 16:28:54 -0700678 /*
679 * If there is no globalAlert active we are done.
680 * If there is an active globalAlert but there are more
681 * tethers, we are also done.
682 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900683 if (!mGlobalAlertBytes || mGlobalAlertTetherCount >= 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700684 return 0;
685 }
686
687 /* We only detete the rule if this was the last tether removed. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900688 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700689 return res;
690}
691
JP Abgrall8a932722011-07-13 19:17:35 -0700692int BandwidthController::setSharedAlert(int64_t bytes) {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900693 if (!mSharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000694 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700695 return -1;
696 }
697 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000698 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700699 return -1;
700 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900701 return setCostlyAlert("shared", bytes, &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700702}
703
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900704int BandwidthController::removeSharedAlert() {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900705 return removeCostlyAlert("shared", &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700706}
707
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900708int BandwidthController::setInterfaceAlert(const std::string& iface, int64_t bytes) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700709 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900710 ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700711 return -1;
712 }
713
JP Abgrall8a932722011-07-13 19:17:35 -0700714 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000715 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700716 return -1;
717 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900718 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700719
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900720 if (it == mQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000721 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700722 return -1;
723 }
724
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900725 return setCostlyAlert(iface, bytes, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700726}
727
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900728int BandwidthController::removeInterfaceAlert(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700729 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900730 ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700731 return -1;
732 }
733
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900734 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700735
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900736 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900737 ALOGE("No prior alert set for interface %s", iface.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700738 return -1;
739 }
740
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900741 return removeCostlyAlert(iface, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700742}
743
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900744int BandwidthController::setCostlyAlert(const std::string& costName, int64_t bytes,
745 int64_t* alertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700746 char *alertQuotaCmd;
JP Abgrall109899b2013-02-12 19:20:13 -0800747 char *chainName;
JP Abgrall8a932722011-07-13 19:17:35 -0700748 int res = 0;
749 char *alertName;
750
JP Abgrall69261cb2014-06-19 18:35:24 -0700751 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900752 ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700753 return -1;
754 }
755
JP Abgrall8a932722011-07-13 19:17:35 -0700756 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000757 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700758 return -1;
759 }
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900760 asprintf(&alertName, "%sAlert", costName.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700761 if (*alertBytes) {
762 res = updateQuota(alertName, *alertBytes);
763 } else {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900764 asprintf(&chainName, "bw_costly_%s", costName.c_str());
JP Abgrall109899b2013-02-12 19:20:13 -0800765 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700766 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700767 free(alertQuotaCmd);
JP Abgrall109899b2013-02-12 19:20:13 -0800768 free(chainName);
JP Abgrall8a932722011-07-13 19:17:35 -0700769 }
770 *alertBytes = bytes;
771 free(alertName);
772 return res;
773}
774
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900775int BandwidthController::removeCostlyAlert(const std::string& costName, int64_t* alertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700776 char *alertQuotaCmd;
777 char *chainName;
778 char *alertName;
779 int res = 0;
780
JP Abgrall69261cb2014-06-19 18:35:24 -0700781 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900782 ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700783 return -1;
784 }
785
JP Abgrall8a932722011-07-13 19:17:35 -0700786 if (!*alertBytes) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900787 ALOGE("No prior alert set for %s alert", costName.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700788 return -1;
789 }
790
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900791 asprintf(&alertName, "%sAlert", costName.c_str());
792 asprintf(&chainName, "bw_costly_%s", costName.c_str());
JP Abgrall92009c82013-02-06 18:01:24 -0800793 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700794 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700795 free(alertQuotaCmd);
796 free(chainName);
797
798 *alertBytes = 0;
799 free(alertName);
800 return res;
801}
JP Abgralldb7da582011-09-18 12:57:32 -0700802
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900803void BandwidthController::addStats(TetherStatsList& statsList, const TetherStats& stats) {
804 for (TetherStats& existing : statsList) {
805 if (existing.addStatsIfMatch(stats)) {
806 return;
807 }
808 }
809 // No match. Insert a new interface pair.
810 statsList.push_back(stats);
811}
812
JP Abgralldb7da582011-09-18 12:57:32 -0700813/*
814 * Parse the ptks and bytes out of:
JP Abgrallbaeccc42013-06-25 09:44:10 -0700815 * Chain natctrl_tether_counters (4 references)
816 * pkts bytes target prot opt in out source destination
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700817 * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
818 * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0
819 * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0
820 * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900821 * or:
822 * Chain natctrl_tether_counters (0 references)
823 * pkts bytes target prot opt in out source destination
824 * 0 0 RETURN all wlan0 rmnet_data0 ::/0 ::/0
825 * 0 0 RETURN all rmnet_data0 wlan0 ::/0 ::/0
826 *
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700827 * It results in an error if invoked and no tethering counter rules exist. The constraint
828 * helps detect complete parsing failure.
JP Abgralldb7da582011-09-18 12:57:32 -0700829 */
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900830int BandwidthController::addForwardChainStats(const TetherStats& filter,
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900831 TetherStatsList& statsList,
832 const std::string& statsOutput,
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900833 std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -0700834 int res;
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900835 std::string statsLine;
JP Abgralldb7da582011-09-18 12:57:32 -0700836 char iface0[MAX_IPT_OUTPUT_LINE_LEN];
837 char iface1[MAX_IPT_OUTPUT_LINE_LEN];
838 char rest[MAX_IPT_OUTPUT_LINE_LEN];
839
JP Abgrallbaeccc42013-06-25 09:44:10 -0700840 TetherStats stats;
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900841 const char *buffPtr;
JP Abgralldb7da582011-09-18 12:57:32 -0700842 int64_t packets, bytes;
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700843 int statsFound = 0;
JP Abgrallbaeccc42013-06-25 09:44:10 -0700844
845 bool filterPair = filter.intIface[0] && filter.extIface[0];
846
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900847 ALOGV("filter: %s", filter.getStatsLine().c_str());
JP Abgrallbaeccc42013-06-25 09:44:10 -0700848
849 stats = filter;
JP Abgralldb7da582011-09-18 12:57:32 -0700850
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900851 std::stringstream stream(statsOutput);
852 while (std::getline(stream, statsLine, '\n')) {
853 buffPtr = statsLine.c_str();
854
JP Abgralldb7da582011-09-18 12:57:32 -0700855 /* Clean up, so a failed parse can still print info */
856 iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900857 if (strstr(buffPtr, "0.0.0.0")) {
858 // IPv4 has -- indicating what to do with fragments...
859 // 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
860 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s",
861 &packets, &bytes, iface0, iface1, rest);
862 } else {
863 // ... but IPv6 does not.
864 // 26 2373 RETURN all wlan0 rmnet0 ::/0 ::/0
865 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all %s %s ::/%s",
866 &packets, &bytes, iface0, iface1, rest);
867 }
SynergyDev7776cea2014-03-16 15:48:51 -0700868 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 -0700869 iface0, iface1, packets, bytes, rest, buffPtr);
JP Abgralla2a64f02011-11-11 20:36:16 -0800870 extraProcessingInfo += buffPtr;
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900871 extraProcessingInfo += "\n";
JP Abgralla2a64f02011-11-11 20:36:16 -0800872
JP Abgralldb7da582011-09-18 12:57:32 -0700873 if (res != 5) {
874 continue;
875 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700876 /*
877 * The following assumes that the 1st rule has in:extIface out:intIface,
878 * which is what NatController sets up.
879 * If not filtering, the 1st match rx, and sets up the pair for the tx side.
880 */
881 if (filter.intIface[0] && filter.extIface[0]) {
882 if (filter.intIface == iface0 && filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -0700883 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 -0700884 stats.rxPackets = packets;
885 stats.rxBytes = bytes;
886 } else if (filter.intIface == iface1 && filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -0700887 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 -0700888 stats.txPackets = packets;
889 stats.txBytes = bytes;
890 }
891 } else if (filter.intIface[0] || filter.extIface[0]) {
892 if (filter.intIface == iface0 || filter.extIface == iface1) {
SynergyDev7776cea2014-03-16 15:48:51 -0700893 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 -0700894 stats.intIface = iface0;
895 stats.extIface = iface1;
896 stats.rxPackets = packets;
897 stats.rxBytes = bytes;
898 } else if (filter.intIface == iface1 || filter.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -0700899 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 -0700900 stats.intIface = iface1;
901 stats.extIface = iface0;
902 stats.txPackets = packets;
903 stats.txBytes = bytes;
904 }
905 } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ {
906 if (!stats.intIface[0]) {
SynergyDev7776cea2014-03-16 15:48:51 -0700907 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 -0700908 stats.intIface = iface0;
909 stats.extIface = iface1;
910 stats.rxPackets = packets;
911 stats.rxBytes = bytes;
912 } else if (stats.intIface == iface1 && stats.extIface == iface0) {
SynergyDev7776cea2014-03-16 15:48:51 -0700913 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 -0700914 stats.txPackets = packets;
915 stats.txBytes = bytes;
916 }
917 }
918 if (stats.rxBytes != -1 && stats.txBytes != -1) {
SynergyDev7776cea2014-03-16 15:48:51 -0700919 ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64" filterPair=%d", stats.rxBytes, stats.txBytes, filterPair);
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900920 addStats(statsList, stats);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700921 if (filterPair) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700922 return 0;
923 } else {
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900924 statsFound++;
JP Abgrallbaeccc42013-06-25 09:44:10 -0700925 stats = filter;
926 }
JP Abgralldb7da582011-09-18 12:57:32 -0700927 }
928 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700929
930 /* It is always an error to find only one side of the stats. */
931 /* It is an error to find nothing when not filtering. */
932 if (((stats.rxBytes == -1) != (stats.txBytes == -1)) ||
933 (!statsFound && !filterPair)) {
934 return -1;
JP Abgrallbaeccc42013-06-25 09:44:10 -0700935 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700936 return 0;
JP Abgralldb7da582011-09-18 12:57:32 -0700937}
938
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900939std::string BandwidthController::TetherStats::getStatsLine() const {
940 std::string msg;
941 StringAppendF(&msg, "%s %s %" PRId64" %" PRId64" %" PRId64" %" PRId64, intIface.c_str(),
942 extIface.c_str(), rxBytes, rxPackets, txBytes, txPackets);
JP Abgralldb7da582011-09-18 12:57:32 -0700943 return msg;
944}
945
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900946int BandwidthController::getTetherStats(SocketClient *cli, TetherStats& filter,
947 std::string &extraProcessingInfo) {
948 int res = 0;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900949
950 TetherStatsList statsList;
951
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900952 for (const IptablesTarget target : {V4, V6}) {
953 std::string statsString;
954 res = iptablesRestoreFunction(target, GET_TETHER_STATS_COMMAND, &statsString);
955 if (res != 0) {
956 ALOGE("Failed to run %s err=%d", GET_TETHER_STATS_COMMAND.c_str(), res);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900957 return -1;
958 }
959
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900960 res = addForwardChainStats(filter, statsList, statsString, extraProcessingInfo);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900961 if (res != 0) {
962 return res;
963 }
964 }
JP Abgralldb7da582011-09-18 12:57:32 -0700965
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900966 if (filter.intIface[0] && filter.extIface[0] && statsList.size() == 1) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900967 cli->sendMsg(ResponseCode::TetheringStatsResult,
968 statsList[0].getStatsLine().c_str(), false);
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900969 } else {
970 for (const auto& stats: statsList) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900971 cli->sendMsg(ResponseCode::TetheringStatsListResult,
972 stats.getStatsLine().c_str(), false);
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900973 }
974 if (res == 0) {
975 cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
976 }
977 }
978
JP Abgralldb7da582011-09-18 12:57:32 -0700979 return res;
980}
JP Abgrall0e540ec2013-08-26 15:13:10 -0700981
982void BandwidthController::flushExistingCostlyTables(bool doClean) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900983 std::string fullCmd = "*filter\n-S\nCOMMIT\n";
984 std::string ruleList;
JP Abgrall0e540ec2013-08-26 15:13:10 -0700985
986 /* Only lookup ip4 table names as ip6 will have the same tables ... */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900987 if (int ret = iptablesRestoreFunction(V4, fullCmd, &ruleList)) {
988 ALOGE("Failed to list existing costly tables ret=%d", ret);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700989 return;
990 }
991 /* ... then flush/clean both ip4 and ip6 iptables. */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900992 parseAndFlushCostlyTables(ruleList, doClean);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700993}
994
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900995void BandwidthController::parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove) {
996 std::stringstream stream(ruleList);
997 std::string rule;
998 std::vector<std::string> clearCommands = { "*filter" };
999 std::string chainName;
JP Abgrall0e540ec2013-08-26 15:13:10 -07001000
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +09001001 // Find and flush all rules starting with "-N bw_costly_<iface>" except "-N bw_costly_shared".
1002 while (std::getline(stream, rule, '\n')) {
1003 if (rule.find(NEW_CHAIN_COMMAND) != 0) continue;
1004 chainName = rule.substr(NEW_CHAIN_COMMAND.size());
1005 ALOGV("parse chainName=<%s> orig line=<%s>", chainName.c_str(), rule.c_str());
1006
1007 if (chainName.find("bw_costly_") != 0 || chainName == std::string("bw_costly_shared")) {
JP Abgrall0e540ec2013-08-26 15:13:10 -07001008 continue;
1009 }
1010
Lorenzo Colitti3c272702017-04-26 15:48:13 +09001011 clearCommands.push_back(StringPrintf(":%s -", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -07001012 if (doRemove) {
Lorenzo Colitti3c272702017-04-26 15:48:13 +09001013 clearCommands.push_back(StringPrintf("-X %s", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -07001014 }
1015 }
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +09001016
1017 if (clearCommands.size() == 1) {
1018 // No rules found.
1019 return;
1020 }
1021
1022 clearCommands.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +09001023 iptablesRestoreFunction(V4V6, Join(clearCommands, '\n'), nullptr);
JP Abgrall0e540ec2013-08-26 15:13:10 -07001024}
Lorenzo Colittid9db08c2017-04-28 11:06:40 +09001025
1026inline const char *BandwidthController::opToString(IptOp op) {
1027 switch (op) {
1028 case IptOpInsert:
1029 return "-I";
1030 case IptOpDelete:
1031 return "-D";
1032 }
1033}
1034
1035inline const char *BandwidthController::jumpToString(IptJumpOp jumpHandling) {
1036 /*
1037 * Must be careful what one rejects with, as upper layer protocols will just
1038 * keep on hammering the device until the number of retries are done.
1039 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
1040 */
1041 switch (jumpHandling) {
1042 case IptJumpNoAdd:
1043 return "";
1044 case IptJumpReject:
1045 return " --jump REJECT";
1046 case IptJumpReturn:
1047 return " --jump RETURN";
1048 }
1049}