blob: 1484187a695ee330ec3ac5bbe23945187211ce55 [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 "NetdConstants.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070056
JP Abgralldb7da582011-09-18 12:57:32 -070057/* Alphabetical */
Lorenzo Colitti3c272702017-04-26 15:48:13 +090058#define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %" PRId64" --name %s\n"
Joel Scherpelzbcad6612017-05-30 10:55:11 +090059const char BandwidthController::LOCAL_INPUT[] = "bw_INPUT";
60const char BandwidthController::LOCAL_FORWARD[] = "bw_FORWARD";
61const char BandwidthController::LOCAL_OUTPUT[] = "bw_OUTPUT";
62const char BandwidthController::LOCAL_RAW_PREROUTING[] = "bw_raw_PREROUTING";
63const char BandwidthController::LOCAL_MANGLE_POSTROUTING[] = "bw_mangle_POSTROUTING";
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090064
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090065auto BandwidthController::iptablesRestoreFunction = execIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090066
Joel Scherpelzd59526a2017-06-28 16:24:09 +090067using android::base::Join;
Lorenzo Colitti3c272702017-04-26 15:48:13 +090068using android::base::StringAppendF;
69using android::base::StringPrintf;
Joel Scherpelz01cc5492017-06-16 10:45:14 +090070using android::netdutils::StatusOr;
71using android::netdutils::UniqueFile;
Lorenzo Colitti3c272702017-04-26 15:48:13 +090072
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090073namespace {
74
75const char ALERT_GLOBAL_NAME[] = "globalAlert";
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090076const std::string NEW_CHAIN_COMMAND = "-N ";
Lorenzo Colittice6748a2017-02-02 01:34:33 +090077
Joel Scherpelzbcad6612017-05-30 10:55:11 +090078const char NAUGHTY_CHAIN[] = "bw_penalty_box";
79const char NICE_CHAIN[] = "bw_happy_box";
JP Abgralldb7da582011-09-18 12:57:32 -070080
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070081/**
82 * Some comments about the rules:
83 * * Ordering
84 * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
JP Abgrall29e8de22012-05-03 12:52:15 -070085 * E.g. "-I bw_INPUT -i rmnet0 --jump costly"
JP Abgrall7e51cde2013-07-03 13:33:05 -070086 * - quota'd rules in the costly chain should be before bw_penalty_box lookups.
JP Abgrall29e8de22012-05-03 12:52:15 -070087 * - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070088 *
89 * * global quota vs per interface quota
90 * - global quota for all costly interfaces uses a single costly chain:
91 * . initial rules
JP Abgrall7e51cde2013-07-03 13:33:05 -070092 * iptables -N bw_costly_shared
93 * iptables -I bw_INPUT -i iface0 --jump bw_costly_shared
94 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_shared
95 * iptables -I bw_costly_shared -m quota \! --quota 500000 \
JP Abgrallbfa74662011-06-29 19:23:04 -070096 * --jump REJECT --reject-with icmp-net-prohibited
JP Abgrall7e51cde2013-07-03 13:33:05 -070097 * iptables -A bw_costly_shared --jump bw_penalty_box
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090098 * iptables -A bw_penalty_box --jump bw_happy_box
Lorenzo Colitti464eabe2016-03-25 13:38:19 +090099 * iptables -A bw_happy_box --jump bw_data_saver
JP Abgrall8a932722011-07-13 19:17:35 -0700100 *
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700101 * . adding a new iface to this, E.g.:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700102 * iptables -I bw_INPUT -i iface1 --jump bw_costly_shared
103 * iptables -I bw_OUTPUT -o iface1 --jump bw_costly_shared
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700104 *
105 * - quota per interface. This is achieve by having "costly" chains per quota.
106 * E.g. adding a new costly interface iface0 with its own quota:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700107 * iptables -N bw_costly_iface0
108 * iptables -I bw_INPUT -i iface0 --jump bw_costly_iface0
109 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_iface0
110 * iptables -A bw_costly_iface0 -m quota \! --quota 500000 \
JP Abgralle4788732013-07-02 20:28:45 -0700111 * --jump REJECT --reject-with icmp-port-unreachable
JP Abgrall7e51cde2013-07-03 13:33:05 -0700112 * iptables -A bw_costly_iface0 --jump bw_penalty_box
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700113 *
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900114 * * Penalty box, happy box and data saver.
115 * - bw_penalty box is a blacklist of apps that are rejected.
116 * - bw_happy_box is a whitelist of apps. It always includes all system apps
117 * - bw_data_saver implements data usage restrictions.
118 * - Via the UI the user can add and remove apps from the whitelist and
119 * blacklist, and turn on/off data saver.
120 * - The blacklist takes precedence over the whitelist and the whitelist
121 * takes precedence over data saver.
122 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700123 * * bw_penalty_box handling:
124 * - only one bw_penalty_box for all interfaces
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900125 * E.g Adding an app:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700126 * iptables -I bw_penalty_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700127 * --jump REJECT --reject-with icmp-port-unreachable
128 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700129 * * bw_happy_box handling:
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900130 * - The bw_happy_box comes after the penalty box.
JP Abgralle4788732013-07-02 20:28:45 -0700131 * E.g Adding a happy app,
JP Abgrall7e51cde2013-07-03 13:33:05 -0700132 * iptables -I bw_happy_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700133 * --jump RETURN
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900134 *
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900135 * * bw_data_saver handling:
136 * - The bw_data_saver comes after the happy box.
137 * Enable data saver:
138 * iptables -R 1 bw_data_saver --jump REJECT --reject-with icmp-port-unreachable
139 * Disable data saver:
140 * iptables -R 1 bw_data_saver --jump RETURN
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700141 */
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900142
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900143const std::string COMMIT_AND_CLOSE = "COMMIT\n";
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900144const std::string HAPPY_BOX_WHITELIST_COMMAND = StringPrintf(
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900145 "-I bw_happy_box -m owner --uid-owner %d-%d --jump RETURN", 0, MAX_SYSTEM_UID);
146
147static const std::vector<std::string> IPT_FLUSH_COMMANDS = {
JP Abgrall0031cea2012-04-17 16:38:23 -0700148 /*
149 * Cleanup rules.
JP Abgrall7e51cde2013-07-03 13:33:05 -0700150 * Should normally include bw_costly_<iface>, but we rely on the way they are setup
JP Abgrall0031cea2012-04-17 16:38:23 -0700151 * to allow coexistance.
JP Abgrall39f8f242011-06-29 19:21:58 -0700152 */
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900153 "*filter",
154 ":bw_INPUT -",
155 ":bw_OUTPUT -",
156 ":bw_FORWARD -",
157 ":bw_happy_box -",
158 ":bw_penalty_box -",
159 ":bw_data_saver -",
160 ":bw_costly_shared -",
161 "COMMIT",
162 "*raw",
163 ":bw_raw_PREROUTING -",
164 "COMMIT",
165 "*mangle",
166 ":bw_mangle_POSTROUTING -",
167 COMMIT_AND_CLOSE
JP Abgrall0031cea2012-04-17 16:38:23 -0700168};
169
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900170static const std::vector<std::string> IPT_BASIC_ACCOUNTING_COMMANDS = {
171 "*filter",
JP Abgrall0031cea2012-04-17 16:38:23 -0700172 "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall0031cea2012-04-17 16:38:23 -0700173 "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900174 "-A bw_costly_shared --jump bw_penalty_box",
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900175 "-A bw_penalty_box --jump bw_happy_box",
176 "-A bw_happy_box --jump bw_data_saver",
177 "-A bw_data_saver -j RETURN",
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900178 HAPPY_BOX_WHITELIST_COMMAND,
179 "COMMIT",
180
181 "*raw",
182 "-A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */
183 "COMMIT",
184
185 "*mangle",
186 "-A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */
187 COMMIT_AND_CLOSE
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900188};
189
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900190std::vector<std::string> toStrVec(int num, char* strs[]) {
191 std::vector<std::string> tmp;
192 for (int i = 0; i < num; ++i) {
193 tmp.emplace_back(strs[i]);
194 }
195 return tmp;
196}
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900197
198} // namespace
199
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900200BandwidthController::BandwidthController() {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700201}
202
JP Abgrall0e540ec2013-08-26 15:13:10 -0700203void BandwidthController::flushCleanTables(bool doClean) {
204 /* Flush and remove the bw_costly_<iface> tables */
205 flushExistingCostlyTables(doClean);
JP Abgrall0031cea2012-04-17 16:38:23 -0700206
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900207 std::string commands = Join(IPT_FLUSH_COMMANDS, '\n');
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900208 iptablesRestoreFunction(V4V6, commands, nullptr);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700209}
JP Abgrall0031cea2012-04-17 16:38:23 -0700210
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900211int BandwidthController::setupIptablesHooks() {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700212 /* flush+clean is allowed to fail */
213 flushCleanTables(true);
JP Abgrall0031cea2012-04-17 16:38:23 -0700214 return 0;
JP Abgrall0031cea2012-04-17 16:38:23 -0700215}
216
217int BandwidthController::enableBandwidthControl(bool force) {
JP Abgrall0031cea2012-04-17 16:38:23 -0700218 char value[PROPERTY_VALUE_MAX];
219
220 if (!force) {
221 property_get("persist.bandwidth.enable", value, "1");
222 if (!strcmp(value, "0"))
223 return 0;
224 }
JP Abgrall8a932722011-07-13 19:17:35 -0700225
JP Abgralldb7da582011-09-18 12:57:32 -0700226 /* Let's pretend we started from scratch ... */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900227 mSharedQuotaIfaces.clear();
228 mQuotaIfaces.clear();
229 mGlobalAlertBytes = 0;
230 mGlobalAlertTetherCount = 0;
231 mSharedQuotaBytes = mSharedAlertBytes = 0;
JP Abgralldb7da582011-09-18 12:57:32 -0700232
JP Abgrall0e540ec2013-08-26 15:13:10 -0700233 flushCleanTables(false);
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900234 std::string commands = Join(IPT_BASIC_ACCOUNTING_COMMANDS, '\n');
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900235 return iptablesRestoreFunction(V4V6, commands, nullptr);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700236}
237
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900238int BandwidthController::disableBandwidthControl() {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700239
240 flushCleanTables(false);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700241 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700242}
243
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900244int BandwidthController::enableDataSaver(bool enable) {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900245 std::string cmd = StringPrintf(
246 "*filter\n"
247 "-R bw_data_saver 1%s\n"
248 "COMMIT\n", jumpToString(enable ? IptJumpReject : IptJumpReturn));
249 return iptablesRestoreFunction(V4V6, cmd, nullptr);
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900250}
251
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700252int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900253 return manipulateSpecialApps(toStrVec(numUids, appUids), NAUGHTY_CHAIN,
254 IptJumpReject, IptOpInsert);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700255}
256
257int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900258 return manipulateSpecialApps(toStrVec(numUids, appUids), NAUGHTY_CHAIN,
259 IptJumpReject, IptOpDelete);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700260}
261
JP Abgralle4788732013-07-02 20:28:45 -0700262int BandwidthController::addNiceApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900263 return manipulateSpecialApps(toStrVec(numUids, appUids), NICE_CHAIN,
264 IptJumpReturn, IptOpInsert);
JP Abgralle4788732013-07-02 20:28:45 -0700265}
266
267int BandwidthController::removeNiceApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900268 return manipulateSpecialApps(toStrVec(numUids, appUids), NICE_CHAIN,
269 IptJumpReturn, IptOpDelete);
JP Abgralle4788732013-07-02 20:28:45 -0700270}
271
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900272int BandwidthController::manipulateSpecialApps(const std::vector<std::string>& appStrUids,
273 const std::string& chain, IptJumpOp jumpHandling,
274 IptOp op) {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900275 std::string cmd = "*filter\n";
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900276 for (const auto& appStrUid : appStrUids) {
277 StringAppendF(&cmd, "%s %s -m owner --uid-owner %s%s\n", opToString(op), chain.c_str(),
278 appStrUid.c_str(), jumpToString(jumpHandling));
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700279 }
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900280 StringAppendF(&cmd, "COMMIT\n");
281 return iptablesRestoreFunction(V4V6, cmd, nullptr);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700282}
283
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900284int BandwidthController::setInterfaceSharedQuota(const std::string& iface, int64_t maxBytes) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700285 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700286 std::string quotaCmd;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900287 constexpr char cost[] = "shared";
288 constexpr char chain[] = "bw_costly_shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700289
JP Abgrall8a932722011-07-13 19:17:35 -0700290 if (!maxBytes) {
291 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000292 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700293 return -1;
294 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700295 if (!isIfaceName(iface))
296 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700297
298 if (maxBytes == -1) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900299 return removeInterfaceSharedQuota(iface);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700300 }
301
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900302 auto it = mSharedQuotaIfaces.find(iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700303
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900304 if (it == mSharedQuotaIfaces.end()) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900305 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
306 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900307 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900308 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(), chain),
309 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(), chain),
Erik Kline51eb3242017-09-20 18:30:47 +0900310 StringPrintf("-A bw_FORWARD -i %s --jump %s", iface.c_str(), chain),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900311 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
312 };
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900313 if (mSharedQuotaIfaces.empty()) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900314 cmds.push_back(StringPrintf("-I %s -m quota2 ! --quota %" PRId64
315 " --name %s --jump REJECT",
316 chain, maxBytes, cost));
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900317 }
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900318 cmds.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900319
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900320 res |= iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900321 if (res) {
322 ALOGE("Failed set quota rule");
323 removeInterfaceSharedQuota(iface);
324 return -1;
325 }
326 mSharedQuotaBytes = maxBytes;
327 mSharedQuotaIfaces.insert(iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700328 }
329
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900330 if (maxBytes != mSharedQuotaBytes) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900331 res |= updateQuota(cost, maxBytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700332 if (res) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900333 ALOGE("Failed update quota for %s", cost);
334 removeInterfaceSharedQuota(iface);
335 return -1;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700336 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900337 mSharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700338 }
339 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700340}
341
JP Abgrall8a932722011-07-13 19:17:35 -0700342/* It will also cleanup any shared alerts */
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900343int BandwidthController::removeInterfaceSharedQuota(const std::string& iface) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900344 constexpr char cost[] = "shared";
345 constexpr char chain[] = "bw_costly_shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700346
JP Abgrall69261cb2014-06-19 18:35:24 -0700347 if (!isIfaceName(iface))
348 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700349
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900350 auto it = mSharedQuotaIfaces.find(iface);
351
352 if (it == mSharedQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900353 ALOGE("No such iface %s to delete", iface.c_str());
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700354 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700355 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700356
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900357 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900358 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900359 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain),
360 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain),
Erik Kline51eb3242017-09-20 18:30:47 +0900361 StringPrintf("-D bw_FORWARD -i %s --jump %s", iface.c_str(), chain),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900362 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
363 };
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900364 if (mSharedQuotaIfaces.size() == 1) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900365 cmds.push_back(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64
366 " --name %s --jump REJECT",
367 chain, mSharedQuotaBytes, cost));
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700368 }
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900369 cmds.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900370
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900371 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
372 ALOGE("Failed to remove shared quota on %s", iface.c_str());
373 return -1;
374 }
375
376 int res = 0;
377 mSharedQuotaIfaces.erase(it);
378 if (mSharedQuotaIfaces.empty()) {
379 mSharedQuotaBytes = 0;
380 if (mSharedAlertBytes) {
381 res = removeSharedAlert();
382 if (res == 0) {
383 mSharedAlertBytes = 0;
384 }
385 }
386 }
387
388 return res;
389
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700390}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700391
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900392int BandwidthController::setInterfaceQuota(const std::string& iface, int64_t maxBytes) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900393 const std::string& cost = iface;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700394
JP Abgrall69261cb2014-06-19 18:35:24 -0700395 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700396 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700397
JP Abgrall8a932722011-07-13 19:17:35 -0700398 if (!maxBytes) {
399 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000400 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700401 return -1;
402 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700403 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700404 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700405 }
406
JP Abgrall0dad7c22011-06-24 11:58:14 -0700407 /* Insert ingress quota. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900408 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700409
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900410 if (it != mQuotaIfaces.end()) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900411 if (updateQuota(cost, maxBytes) != 0) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900412 ALOGE("Failed update quota for %s", iface.c_str());
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900413 removeInterfaceQuota(iface);
414 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700415 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900416 it->second.quota = maxBytes;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900417 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700418 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700419
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900420 const std::string chain = "bw_costly_" + iface;
421 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
422 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900423 "*filter",
424 StringPrintf(":%s -", chain.c_str()),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900425 StringPrintf("-A %s -j bw_penalty_box", chain.c_str()),
426 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(),
427 chain.c_str()),
428 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(),
429 chain.c_str()),
Erik Kline51eb3242017-09-20 18:30:47 +0900430 StringPrintf("-A bw_FORWARD -i %s --jump %s", iface.c_str(), chain.c_str()),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900431 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
432 StringPrintf("-A %s -m quota2 ! --quota %" PRId64 " --name %s --jump REJECT",
433 chain.c_str(), maxBytes, cost.c_str()),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900434 "COMMIT\n",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900435 };
436
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900437 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900438 ALOGE("Failed set quota rule");
439 removeInterfaceQuota(iface);
440 return -1;
441 }
442
443 mQuotaIfaces[iface] = QuotaInfo{maxBytes, 0};
444 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700445}
446
JP Abgrall8a932722011-07-13 19:17:35 -0700447int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
448 return getInterfaceQuota("shared", bytes);
449}
450
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900451int BandwidthController::getInterfaceQuota(const std::string& iface, int64_t* bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900452 const auto& sys = android::netdutils::sSyscalls.get();
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900453 const std::string fname = "/proc/net/xt_quota/" + iface;
JP Abgrall8a932722011-07-13 19:17:35 -0700454
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900455 if (!isIfaceName(iface)) return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700456
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900457 StatusOr<UniqueFile> file = sys.fopen(fname, "re");
458 if (!isOk(file)) {
459 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700460 return -1;
461 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900462 auto rv = sys.fscanf(file.value().get(), "%" SCNd64, bytes);
463 if (!isOk(rv)) {
464 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(rv).c_str());
465 return -1;
466 }
467 ALOGV("Read quota res=%d bytes=%" PRId64, rv.value(), *bytes);
468 return rv.value() == 1 ? 0 : -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700469}
470
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900471int BandwidthController::removeInterfaceQuota(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700472 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700473 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700474
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900475 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700476
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900477 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900478 ALOGE("No such iface %s to delete", iface.c_str());
JP Abgrall0dad7c22011-06-24 11:58:14 -0700479 return -1;
480 }
481
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900482 const std::string chain = "bw_costly_" + iface;
483 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900484 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900485 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain.c_str()),
486 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain.c_str()),
Erik Kline51eb3242017-09-20 18:30:47 +0900487 StringPrintf("-D bw_FORWARD -i %s --jump %s", iface.c_str(), chain.c_str()),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900488 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
489 StringPrintf("-F %s", chain.c_str()),
490 StringPrintf("-X %s", chain.c_str()),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900491 "COMMIT\n",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900492 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900493
494 const int res = iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700495
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900496 if (res == 0) {
497 mQuotaIfaces.erase(it);
498 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700499
500 return res;
501}
JP Abgrall8a932722011-07-13 19:17:35 -0700502
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900503int BandwidthController::updateQuota(const std::string& quotaName, int64_t bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900504 const auto& sys = android::netdutils::sSyscalls.get();
505 const std::string fname = "/proc/net/xt_quota/" + quotaName;
JP Abgrall8a932722011-07-13 19:17:35 -0700506
JP Abgrall69261cb2014-06-19 18:35:24 -0700507 if (!isIfaceName(quotaName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900508 ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700509 return -1;
510 }
511
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900512 StatusOr<UniqueFile> file = sys.fopen(fname, "we");
513 if (!isOk(file)) {
514 ALOGE("Updating quota %s failed (%s)", quotaName.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 sys.fprintf(file.value().get(), "%" PRId64 "\n", bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700518 return 0;
519}
520
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900521int BandwidthController::runIptablesAlertCmd(IptOp op, const std::string& alertName,
522 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900523 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900524 std::string alertQuotaCmd = "*filter\n";
JP Abgrall8a932722011-07-13 19:17:35 -0700525
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900526 // TODO: consider using an alternate template for the delete that does not include the --quota
527 // value. This code works because the --quota value is ignored by deletes
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900528 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT", bytes,
529 alertName.c_str());
530 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT", bytes,
531 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900532 StringAppendF(&alertQuotaCmd, "COMMIT\n");
533
Lorenzo Colitti4773cb42017-04-27 14:03:25 +0900534 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrall8a932722011-07-13 19:17:35 -0700535}
536
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900537int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const std::string& alertName,
538 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900539 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900540 std::string alertQuotaCmd = "*filter\n";
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900541 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD", bytes,
542 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900543 StringAppendF(&alertQuotaCmd, "COMMIT\n");
544
545 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrallc6c67342011-10-07 16:28:54 -0700546}
547
548int BandwidthController::setGlobalAlert(int64_t bytes) {
549 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700550 int res = 0;
551
552 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000553 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700554 return -1;
555 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900556 if (mGlobalAlertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700557 res = updateQuota(alertName, bytes);
558 } else {
559 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900560 if (mGlobalAlertTetherCount) {
561 ALOGV("setGlobalAlert for %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700562 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
563 }
JP Abgrall8a932722011-07-13 19:17:35 -0700564 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900565 mGlobalAlertBytes = bytes;
JP Abgrall8a932722011-07-13 19:17:35 -0700566 return res;
567}
568
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900569int BandwidthController::setGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700570 const char *alertName = ALERT_GLOBAL_NAME;
571 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700572
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900573 mGlobalAlertTetherCount++;
574 ALOGV("setGlobalAlertInForwardChain(): %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700575
576 /*
577 * If there is no globalAlert active we are done.
578 * If there is an active globalAlert but this is not the 1st
579 * tether, we are also done.
580 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900581 if (!mGlobalAlertBytes || mGlobalAlertTetherCount != 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700582 return 0;
583 }
584
585 /* We only add the rule if this was the 1st tether added. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900586 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700587 return res;
588}
589
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900590int BandwidthController::removeGlobalAlert() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700591
592 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700593 int res = 0;
594
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900595 if (!mGlobalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000596 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700597 return -1;
598 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900599 res = runIptablesAlertCmd(IptOpDelete, alertName, mGlobalAlertBytes);
600 if (mGlobalAlertTetherCount) {
601 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700602 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900603 mGlobalAlertBytes = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700604 return res;
605}
606
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900607int BandwidthController::removeGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700608 int res = 0;
609 const char *alertName = ALERT_GLOBAL_NAME;
610
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900611 if (!mGlobalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000612 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700613 return -1;
614 }
615
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900616 mGlobalAlertTetherCount--;
JP Abgrallc6c67342011-10-07 16:28:54 -0700617 /*
618 * If there is no globalAlert active we are done.
619 * If there is an active globalAlert but there are more
620 * tethers, we are also done.
621 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900622 if (!mGlobalAlertBytes || mGlobalAlertTetherCount >= 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700623 return 0;
624 }
625
626 /* We only detete the rule if this was the last tether removed. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900627 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700628 return res;
629}
630
JP Abgrall8a932722011-07-13 19:17:35 -0700631int BandwidthController::setSharedAlert(int64_t bytes) {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900632 if (!mSharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000633 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700634 return -1;
635 }
636 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000637 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700638 return -1;
639 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900640 return setCostlyAlert("shared", bytes, &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700641}
642
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900643int BandwidthController::removeSharedAlert() {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900644 return removeCostlyAlert("shared", &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700645}
646
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900647int BandwidthController::setInterfaceAlert(const std::string& iface, int64_t bytes) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700648 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900649 ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700650 return -1;
651 }
652
JP Abgrall8a932722011-07-13 19:17:35 -0700653 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000654 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700655 return -1;
656 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900657 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700658
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900659 if (it == mQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000660 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700661 return -1;
662 }
663
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900664 return setCostlyAlert(iface, bytes, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700665}
666
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900667int BandwidthController::removeInterfaceAlert(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700668 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900669 ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700670 return -1;
671 }
672
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900673 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700674
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900675 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900676 ALOGE("No prior alert set for interface %s", iface.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700677 return -1;
678 }
679
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900680 return removeCostlyAlert(iface, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700681}
682
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900683int BandwidthController::setCostlyAlert(const std::string& costName, int64_t bytes,
684 int64_t* alertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700685 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700686
JP Abgrall69261cb2014-06-19 18:35:24 -0700687 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900688 ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700689 return -1;
690 }
691
JP Abgrall8a932722011-07-13 19:17:35 -0700692 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000693 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700694 return -1;
695 }
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900696
697 std::string alertName = costName + "Alert";
698 std::string chainName = "bw_costly_" + costName;
JP Abgrall8a932722011-07-13 19:17:35 -0700699 if (*alertBytes) {
700 res = updateQuota(alertName, *alertBytes);
701 } else {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900702 std::vector<std::string> commands = {
703 "*filter\n",
704 StringPrintf(ALERT_IPT_TEMPLATE, "-A", chainName.c_str(), bytes, alertName.c_str()),
705 "COMMIT\n"
706 };
707 res = iptablesRestoreFunction(V4V6, Join(commands, ""), nullptr);
708 if (res) {
709 ALOGE("Failed to set costly alert for %s", costName.c_str());
710 }
JP Abgrall8a932722011-07-13 19:17:35 -0700711 }
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900712 if (res == 0) {
713 *alertBytes = bytes;
714 }
JP Abgrall8a932722011-07-13 19:17:35 -0700715 return res;
716}
717
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900718int BandwidthController::removeCostlyAlert(const std::string& costName, int64_t* alertBytes) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700719 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900720 ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700721 return -1;
722 }
723
JP Abgrall8a932722011-07-13 19:17:35 -0700724 if (!*alertBytes) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900725 ALOGE("No prior alert set for %s alert", costName.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700726 return -1;
727 }
728
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900729 std::string alertName = costName + "Alert";
730 std::string chainName = "bw_costly_" + costName;
731 std::vector<std::string> commands = {
732 "*filter\n",
733 StringPrintf(ALERT_IPT_TEMPLATE, "-D", chainName.c_str(), *alertBytes, alertName.c_str()),
734 "COMMIT\n"
735 };
736 if (iptablesRestoreFunction(V4V6, Join(commands, ""), nullptr) != 0) {
737 ALOGE("Failed to remove costly alert %s", costName.c_str());
738 return -1;
739 }
JP Abgrall8a932722011-07-13 19:17:35 -0700740
741 *alertBytes = 0;
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900742 return 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700743}
JP Abgralldb7da582011-09-18 12:57:32 -0700744
JP Abgrall0e540ec2013-08-26 15:13:10 -0700745void BandwidthController::flushExistingCostlyTables(bool doClean) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900746 std::string fullCmd = "*filter\n-S\nCOMMIT\n";
747 std::string ruleList;
JP Abgrall0e540ec2013-08-26 15:13:10 -0700748
749 /* Only lookup ip4 table names as ip6 will have the same tables ... */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900750 if (int ret = iptablesRestoreFunction(V4, fullCmd, &ruleList)) {
751 ALOGE("Failed to list existing costly tables ret=%d", ret);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700752 return;
753 }
754 /* ... then flush/clean both ip4 and ip6 iptables. */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900755 parseAndFlushCostlyTables(ruleList, doClean);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700756}
757
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900758void BandwidthController::parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove) {
759 std::stringstream stream(ruleList);
760 std::string rule;
761 std::vector<std::string> clearCommands = { "*filter" };
762 std::string chainName;
JP Abgrall0e540ec2013-08-26 15:13:10 -0700763
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900764 // Find and flush all rules starting with "-N bw_costly_<iface>" except "-N bw_costly_shared".
765 while (std::getline(stream, rule, '\n')) {
766 if (rule.find(NEW_CHAIN_COMMAND) != 0) continue;
767 chainName = rule.substr(NEW_CHAIN_COMMAND.size());
768 ALOGV("parse chainName=<%s> orig line=<%s>", chainName.c_str(), rule.c_str());
769
770 if (chainName.find("bw_costly_") != 0 || chainName == std::string("bw_costly_shared")) {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700771 continue;
772 }
773
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900774 clearCommands.push_back(StringPrintf(":%s -", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -0700775 if (doRemove) {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900776 clearCommands.push_back(StringPrintf("-X %s", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -0700777 }
778 }
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900779
780 if (clearCommands.size() == 1) {
781 // No rules found.
782 return;
783 }
784
785 clearCommands.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900786 iptablesRestoreFunction(V4V6, Join(clearCommands, '\n'), nullptr);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700787}
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900788
789inline const char *BandwidthController::opToString(IptOp op) {
790 switch (op) {
791 case IptOpInsert:
792 return "-I";
793 case IptOpDelete:
794 return "-D";
795 }
796}
797
798inline const char *BandwidthController::jumpToString(IptJumpOp jumpHandling) {
799 /*
800 * Must be careful what one rejects with, as upper layer protocols will just
801 * keep on hammering the device until the number of retries are done.
802 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
803 */
804 switch (jumpHandling) {
805 case IptJumpNoAdd:
806 return "";
807 case IptJumpReject:
808 return " --jump REJECT";
809 case IptJumpReturn:
810 return " --jump RETURN";
811 }
812}