blob: 04e5d75e8278ec63112b98cd53ff943db2081e0e [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"
Lorenzo Colittiaff28792017-09-26 17:46:18 +090055#include "FirewallController.h" /* For makeCriticalCommands */
Joel Scherpelz01cc5492017-06-16 10:45:14 +090056#include "NetdConstants.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070057
JP Abgralldb7da582011-09-18 12:57:32 -070058/* Alphabetical */
Lorenzo Colitti3c272702017-04-26 15:48:13 +090059#define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %" PRId64" --name %s\n"
Joel Scherpelzbcad6612017-05-30 10:55:11 +090060const char BandwidthController::LOCAL_INPUT[] = "bw_INPUT";
61const char BandwidthController::LOCAL_FORWARD[] = "bw_FORWARD";
62const char BandwidthController::LOCAL_OUTPUT[] = "bw_OUTPUT";
63const char BandwidthController::LOCAL_RAW_PREROUTING[] = "bw_raw_PREROUTING";
64const char BandwidthController::LOCAL_MANGLE_POSTROUTING[] = "bw_mangle_POSTROUTING";
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090065
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090066auto BandwidthController::iptablesRestoreFunction = execIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090067
Joel Scherpelzd59526a2017-06-28 16:24:09 +090068using android::base::Join;
Lorenzo Colitti3c272702017-04-26 15:48:13 +090069using android::base::StringAppendF;
70using android::base::StringPrintf;
Joel Scherpelz01cc5492017-06-16 10:45:14 +090071using android::netdutils::StatusOr;
72using android::netdutils::UniqueFile;
Lorenzo Colitti3c272702017-04-26 15:48:13 +090073
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090074namespace {
75
76const char ALERT_GLOBAL_NAME[] = "globalAlert";
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090077const std::string NEW_CHAIN_COMMAND = "-N ";
Lorenzo Colittice6748a2017-02-02 01:34:33 +090078
Joel Scherpelzbcad6612017-05-30 10:55:11 +090079const char NAUGHTY_CHAIN[] = "bw_penalty_box";
80const char NICE_CHAIN[] = "bw_happy_box";
JP Abgralldb7da582011-09-18 12:57:32 -070081
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070082/**
83 * Some comments about the rules:
84 * * Ordering
85 * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
JP Abgrall29e8de22012-05-03 12:52:15 -070086 * E.g. "-I bw_INPUT -i rmnet0 --jump costly"
JP Abgrall7e51cde2013-07-03 13:33:05 -070087 * - quota'd rules in the costly chain should be before bw_penalty_box lookups.
JP Abgrall29e8de22012-05-03 12:52:15 -070088 * - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070089 *
90 * * global quota vs per interface quota
91 * - global quota for all costly interfaces uses a single costly chain:
92 * . initial rules
JP Abgrall7e51cde2013-07-03 13:33:05 -070093 * iptables -N bw_costly_shared
94 * iptables -I bw_INPUT -i iface0 --jump bw_costly_shared
95 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_shared
96 * iptables -I bw_costly_shared -m quota \! --quota 500000 \
JP Abgrallbfa74662011-06-29 19:23:04 -070097 * --jump REJECT --reject-with icmp-net-prohibited
JP Abgrall7e51cde2013-07-03 13:33:05 -070098 * iptables -A bw_costly_shared --jump bw_penalty_box
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090099 * iptables -A bw_penalty_box --jump bw_happy_box
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900100 * iptables -A bw_happy_box --jump bw_data_saver
JP Abgrall8a932722011-07-13 19:17:35 -0700101 *
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700102 * . adding a new iface to this, E.g.:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700103 * iptables -I bw_INPUT -i iface1 --jump bw_costly_shared
104 * iptables -I bw_OUTPUT -o iface1 --jump bw_costly_shared
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700105 *
106 * - quota per interface. This is achieve by having "costly" chains per quota.
107 * E.g. adding a new costly interface iface0 with its own quota:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700108 * iptables -N bw_costly_iface0
109 * iptables -I bw_INPUT -i iface0 --jump bw_costly_iface0
110 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_iface0
111 * iptables -A bw_costly_iface0 -m quota \! --quota 500000 \
JP Abgralle4788732013-07-02 20:28:45 -0700112 * --jump REJECT --reject-with icmp-port-unreachable
JP Abgrall7e51cde2013-07-03 13:33:05 -0700113 * iptables -A bw_costly_iface0 --jump bw_penalty_box
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700114 *
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900115 * * Penalty box, happy box and data saver.
116 * - bw_penalty box is a blacklist of apps that are rejected.
117 * - bw_happy_box is a whitelist of apps. It always includes all system apps
118 * - bw_data_saver implements data usage restrictions.
119 * - Via the UI the user can add and remove apps from the whitelist and
120 * blacklist, and turn on/off data saver.
121 * - The blacklist takes precedence over the whitelist and the whitelist
122 * takes precedence over data saver.
123 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700124 * * bw_penalty_box handling:
125 * - only one bw_penalty_box for all interfaces
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900126 * E.g Adding an app:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700127 * iptables -I bw_penalty_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700128 * --jump REJECT --reject-with icmp-port-unreachable
129 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700130 * * bw_happy_box handling:
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900131 * - The bw_happy_box comes after the penalty box.
JP Abgralle4788732013-07-02 20:28:45 -0700132 * E.g Adding a happy app,
JP Abgrall7e51cde2013-07-03 13:33:05 -0700133 * iptables -I bw_happy_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700134 * --jump RETURN
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900135 *
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900136 * * bw_data_saver handling:
137 * - The bw_data_saver comes after the happy box.
138 * Enable data saver:
139 * iptables -R 1 bw_data_saver --jump REJECT --reject-with icmp-port-unreachable
140 * Disable data saver:
141 * iptables -R 1 bw_data_saver --jump RETURN
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700142 */
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900143
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900144const std::string COMMIT_AND_CLOSE = "COMMIT\n";
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900145const std::string HAPPY_BOX_WHITELIST_COMMAND = StringPrintf(
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900146 "-I bw_happy_box -m owner --uid-owner %d-%d --jump RETURN", 0, MAX_SYSTEM_UID);
147
148static const std::vector<std::string> IPT_FLUSH_COMMANDS = {
JP Abgrall0031cea2012-04-17 16:38:23 -0700149 /*
150 * Cleanup rules.
JP Abgrall7e51cde2013-07-03 13:33:05 -0700151 * Should normally include bw_costly_<iface>, but we rely on the way they are setup
JP Abgrall0031cea2012-04-17 16:38:23 -0700152 * to allow coexistance.
JP Abgrall39f8f242011-06-29 19:21:58 -0700153 */
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900154 "*filter",
155 ":bw_INPUT -",
156 ":bw_OUTPUT -",
157 ":bw_FORWARD -",
158 ":bw_happy_box -",
159 ":bw_penalty_box -",
160 ":bw_data_saver -",
161 ":bw_costly_shared -",
162 "COMMIT",
163 "*raw",
164 ":bw_raw_PREROUTING -",
165 "COMMIT",
166 "*mangle",
167 ":bw_mangle_POSTROUTING -",
168 COMMIT_AND_CLOSE
JP Abgrall0031cea2012-04-17 16:38:23 -0700169};
170
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900171static const std::vector<std::string> IPT_BASIC_ACCOUNTING_COMMANDS = {
172 "*filter",
JP Abgrall0031cea2012-04-17 16:38:23 -0700173 "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall0031cea2012-04-17 16:38:23 -0700174 "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900175 "-A bw_costly_shared --jump bw_penalty_box",
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900176 "-A bw_penalty_box --jump bw_happy_box",
177 "-A bw_happy_box --jump bw_data_saver",
178 "-A bw_data_saver -j RETURN",
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900179 HAPPY_BOX_WHITELIST_COMMAND,
180 "COMMIT",
181
182 "*raw",
183 "-A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */
184 "COMMIT",
185
186 "*mangle",
187 "-A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */
188 COMMIT_AND_CLOSE
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900189};
190
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900191std::vector<std::string> toStrVec(int num, char* strs[]) {
192 std::vector<std::string> tmp;
193 for (int i = 0; i < num; ++i) {
194 tmp.emplace_back(strs[i]);
195 }
196 return tmp;
197}
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900198
199} // namespace
200
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900201BandwidthController::BandwidthController() {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700202}
203
JP Abgrall0e540ec2013-08-26 15:13:10 -0700204void BandwidthController::flushCleanTables(bool doClean) {
205 /* Flush and remove the bw_costly_<iface> tables */
206 flushExistingCostlyTables(doClean);
JP Abgrall0031cea2012-04-17 16:38:23 -0700207
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900208 std::string commands = Join(IPT_FLUSH_COMMANDS, '\n');
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900209 iptablesRestoreFunction(V4V6, commands, nullptr);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700210}
JP Abgrall0031cea2012-04-17 16:38:23 -0700211
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900212int BandwidthController::setupIptablesHooks() {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700213 /* flush+clean is allowed to fail */
214 flushCleanTables(true);
JP Abgrall0031cea2012-04-17 16:38:23 -0700215 return 0;
JP Abgrall0031cea2012-04-17 16:38:23 -0700216}
217
218int BandwidthController::enableBandwidthControl(bool force) {
JP Abgrall0031cea2012-04-17 16:38:23 -0700219 char value[PROPERTY_VALUE_MAX];
220
221 if (!force) {
222 property_get("persist.bandwidth.enable", value, "1");
223 if (!strcmp(value, "0"))
224 return 0;
225 }
JP Abgrall8a932722011-07-13 19:17:35 -0700226
JP Abgralldb7da582011-09-18 12:57:32 -0700227 /* Let's pretend we started from scratch ... */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900228 mSharedQuotaIfaces.clear();
229 mQuotaIfaces.clear();
230 mGlobalAlertBytes = 0;
231 mGlobalAlertTetherCount = 0;
232 mSharedQuotaBytes = mSharedAlertBytes = 0;
JP Abgralldb7da582011-09-18 12:57:32 -0700233
JP Abgrall0e540ec2013-08-26 15:13:10 -0700234 flushCleanTables(false);
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900235 std::string commands = Join(IPT_BASIC_ACCOUNTING_COMMANDS, '\n');
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900236 return iptablesRestoreFunction(V4V6, commands, nullptr);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700237}
238
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900239int BandwidthController::disableBandwidthControl() {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700240
241 flushCleanTables(false);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700242 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700243}
244
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900245std::string BandwidthController::makeDataSaverCommand(IptablesTarget target, bool enable) {
246 std::string cmd;
247 const char *chainName = "bw_data_saver";
248 const char *op = jumpToString(enable ? IptJumpReject : IptJumpReturn);
249 std::string criticalCommands = enable ?
250 FirewallController::makeCriticalCommands(target, chainName) : "";
251 StringAppendF(&cmd,
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900252 "*filter\n"
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900253 ":%s -\n"
254 "%s"
255 "-A %s%s\n"
256 "COMMIT\n", chainName, criticalCommands.c_str(), chainName, op);
257 return cmd;
258}
259
260int BandwidthController::enableDataSaver(bool enable) {
261 int ret = iptablesRestoreFunction(V4, makeDataSaverCommand(V4, enable), nullptr);
262 ret |= iptablesRestoreFunction(V6, makeDataSaverCommand(V6, enable), nullptr);
263 return ret;
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +0900264}
265
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700266int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900267 return manipulateSpecialApps(toStrVec(numUids, appUids), NAUGHTY_CHAIN,
268 IptJumpReject, IptOpInsert);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700269}
270
271int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900272 return manipulateSpecialApps(toStrVec(numUids, appUids), NAUGHTY_CHAIN,
273 IptJumpReject, IptOpDelete);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700274}
275
JP Abgralle4788732013-07-02 20:28:45 -0700276int BandwidthController::addNiceApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900277 return manipulateSpecialApps(toStrVec(numUids, appUids), NICE_CHAIN,
278 IptJumpReturn, IptOpInsert);
JP Abgralle4788732013-07-02 20:28:45 -0700279}
280
281int BandwidthController::removeNiceApps(int numUids, char *appUids[]) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900282 return manipulateSpecialApps(toStrVec(numUids, appUids), NICE_CHAIN,
283 IptJumpReturn, IptOpDelete);
JP Abgralle4788732013-07-02 20:28:45 -0700284}
285
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900286int BandwidthController::manipulateSpecialApps(const std::vector<std::string>& appStrUids,
287 const std::string& chain, IptJumpOp jumpHandling,
288 IptOp op) {
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900289 std::string cmd = "*filter\n";
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900290 for (const auto& appStrUid : appStrUids) {
291 StringAppendF(&cmd, "%s %s -m owner --uid-owner %s%s\n", opToString(op), chain.c_str(),
292 appStrUid.c_str(), jumpToString(jumpHandling));
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700293 }
Lorenzo Colitti911bc4c2017-04-28 14:34:01 +0900294 StringAppendF(&cmd, "COMMIT\n");
295 return iptablesRestoreFunction(V4V6, cmd, nullptr);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700296}
297
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900298int BandwidthController::setInterfaceSharedQuota(const std::string& iface, int64_t maxBytes) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700299 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700300 std::string quotaCmd;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900301 constexpr char cost[] = "shared";
302 constexpr char chain[] = "bw_costly_shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700303
JP Abgrall8a932722011-07-13 19:17:35 -0700304 if (!maxBytes) {
305 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000306 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700307 return -1;
308 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700309 if (!isIfaceName(iface))
310 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700311
312 if (maxBytes == -1) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900313 return removeInterfaceSharedQuota(iface);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700314 }
315
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900316 auto it = mSharedQuotaIfaces.find(iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700317
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900318 if (it == mSharedQuotaIfaces.end()) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900319 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
320 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900321 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900322 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(), chain),
323 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(), chain),
Erik Kline51eb3242017-09-20 18:30:47 +0900324 StringPrintf("-A bw_FORWARD -i %s --jump %s", iface.c_str(), chain),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900325 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
326 };
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900327 if (mSharedQuotaIfaces.empty()) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900328 cmds.push_back(StringPrintf("-I %s -m quota2 ! --quota %" PRId64
329 " --name %s --jump REJECT",
330 chain, maxBytes, cost));
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900331 }
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900332 cmds.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900333
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900334 res |= iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900335 if (res) {
336 ALOGE("Failed set quota rule");
337 removeInterfaceSharedQuota(iface);
338 return -1;
339 }
340 mSharedQuotaBytes = maxBytes;
341 mSharedQuotaIfaces.insert(iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700342 }
343
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900344 if (maxBytes != mSharedQuotaBytes) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900345 res |= updateQuota(cost, maxBytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700346 if (res) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900347 ALOGE("Failed update quota for %s", cost);
348 removeInterfaceSharedQuota(iface);
349 return -1;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700350 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900351 mSharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700352 }
353 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700354}
355
JP Abgrall8a932722011-07-13 19:17:35 -0700356/* It will also cleanup any shared alerts */
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900357int BandwidthController::removeInterfaceSharedQuota(const std::string& iface) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900358 constexpr char cost[] = "shared";
359 constexpr char chain[] = "bw_costly_shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700360
JP Abgrall69261cb2014-06-19 18:35:24 -0700361 if (!isIfaceName(iface))
362 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700363
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900364 auto it = mSharedQuotaIfaces.find(iface);
365
366 if (it == mSharedQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900367 ALOGE("No such iface %s to delete", iface.c_str());
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700368 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700369 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700370
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900371 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900372 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900373 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain),
374 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain),
Erik Kline51eb3242017-09-20 18:30:47 +0900375 StringPrintf("-D bw_FORWARD -i %s --jump %s", iface.c_str(), chain),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900376 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
377 };
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900378 if (mSharedQuotaIfaces.size() == 1) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900379 cmds.push_back(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64
380 " --name %s --jump REJECT",
381 chain, mSharedQuotaBytes, cost));
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700382 }
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900383 cmds.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900384
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900385 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
386 ALOGE("Failed to remove shared quota on %s", iface.c_str());
387 return -1;
388 }
389
390 int res = 0;
391 mSharedQuotaIfaces.erase(it);
392 if (mSharedQuotaIfaces.empty()) {
393 mSharedQuotaBytes = 0;
394 if (mSharedAlertBytes) {
395 res = removeSharedAlert();
396 if (res == 0) {
397 mSharedAlertBytes = 0;
398 }
399 }
400 }
401
402 return res;
403
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700404}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700405
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900406int BandwidthController::setInterfaceQuota(const std::string& iface, int64_t maxBytes) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900407 const std::string& cost = iface;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700408
JP Abgrall69261cb2014-06-19 18:35:24 -0700409 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700410 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700411
JP Abgrall8a932722011-07-13 19:17:35 -0700412 if (!maxBytes) {
413 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000414 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700415 return -1;
416 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700417 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700418 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700419 }
420
JP Abgrall0dad7c22011-06-24 11:58:14 -0700421 /* Insert ingress quota. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900422 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700423
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900424 if (it != mQuotaIfaces.end()) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900425 if (updateQuota(cost, maxBytes) != 0) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900426 ALOGE("Failed update quota for %s", iface.c_str());
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900427 removeInterfaceQuota(iface);
428 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700429 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900430 it->second.quota = maxBytes;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900431 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700432 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700433
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900434 const std::string chain = "bw_costly_" + iface;
435 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
436 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900437 "*filter",
438 StringPrintf(":%s -", chain.c_str()),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900439 StringPrintf("-A %s -j bw_penalty_box", chain.c_str()),
440 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(),
441 chain.c_str()),
442 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(),
443 chain.c_str()),
Erik Kline51eb3242017-09-20 18:30:47 +0900444 StringPrintf("-A bw_FORWARD -i %s --jump %s", iface.c_str(), chain.c_str()),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900445 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
446 StringPrintf("-A %s -m quota2 ! --quota %" PRId64 " --name %s --jump REJECT",
447 chain.c_str(), maxBytes, cost.c_str()),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900448 "COMMIT\n",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900449 };
450
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900451 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900452 ALOGE("Failed set quota rule");
453 removeInterfaceQuota(iface);
454 return -1;
455 }
456
457 mQuotaIfaces[iface] = QuotaInfo{maxBytes, 0};
458 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700459}
460
JP Abgrall8a932722011-07-13 19:17:35 -0700461int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
462 return getInterfaceQuota("shared", bytes);
463}
464
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900465int BandwidthController::getInterfaceQuota(const std::string& iface, int64_t* bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900466 const auto& sys = android::netdutils::sSyscalls.get();
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900467 const std::string fname = "/proc/net/xt_quota/" + iface;
JP Abgrall8a932722011-07-13 19:17:35 -0700468
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900469 if (!isIfaceName(iface)) return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700470
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900471 StatusOr<UniqueFile> file = sys.fopen(fname, "re");
472 if (!isOk(file)) {
473 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700474 return -1;
475 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900476 auto rv = sys.fscanf(file.value().get(), "%" SCNd64, bytes);
477 if (!isOk(rv)) {
478 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(rv).c_str());
479 return -1;
480 }
481 ALOGV("Read quota res=%d bytes=%" PRId64, rv.value(), *bytes);
482 return rv.value() == 1 ? 0 : -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700483}
484
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900485int BandwidthController::removeInterfaceQuota(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700486 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700487 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700488
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900489 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700490
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900491 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900492 ALOGE("No such iface %s to delete", iface.c_str());
JP Abgrall0dad7c22011-06-24 11:58:14 -0700493 return -1;
494 }
495
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900496 const std::string chain = "bw_costly_" + iface;
497 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900498 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900499 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain.c_str()),
500 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain.c_str()),
Erik Kline51eb3242017-09-20 18:30:47 +0900501 StringPrintf("-D bw_FORWARD -i %s --jump %s", iface.c_str(), chain.c_str()),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900502 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
503 StringPrintf("-F %s", chain.c_str()),
504 StringPrintf("-X %s", chain.c_str()),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900505 "COMMIT\n",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900506 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900507
508 const int res = iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700509
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900510 if (res == 0) {
511 mQuotaIfaces.erase(it);
512 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700513
514 return res;
515}
JP Abgrall8a932722011-07-13 19:17:35 -0700516
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900517int BandwidthController::updateQuota(const std::string& quotaName, int64_t bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900518 const auto& sys = android::netdutils::sSyscalls.get();
519 const std::string fname = "/proc/net/xt_quota/" + quotaName;
JP Abgrall8a932722011-07-13 19:17:35 -0700520
JP Abgrall69261cb2014-06-19 18:35:24 -0700521 if (!isIfaceName(quotaName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900522 ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700523 return -1;
524 }
525
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900526 StatusOr<UniqueFile> file = sys.fopen(fname, "we");
527 if (!isOk(file)) {
528 ALOGE("Updating quota %s failed (%s)", quotaName.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700529 return -1;
530 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900531 sys.fprintf(file.value().get(), "%" PRId64 "\n", bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700532 return 0;
533}
534
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900535int BandwidthController::runIptablesAlertCmd(IptOp op, const std::string& alertName,
536 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900537 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900538 std::string alertQuotaCmd = "*filter\n";
JP Abgrall8a932722011-07-13 19:17:35 -0700539
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900540 // TODO: consider using an alternate template for the delete that does not include the --quota
541 // value. This code works because the --quota value is ignored by deletes
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900542 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT", bytes,
543 alertName.c_str());
544 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT", bytes,
545 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900546 StringAppendF(&alertQuotaCmd, "COMMIT\n");
547
Lorenzo Colitti4773cb42017-04-27 14:03:25 +0900548 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrall8a932722011-07-13 19:17:35 -0700549}
550
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900551int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const std::string& alertName,
552 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900553 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900554 std::string alertQuotaCmd = "*filter\n";
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900555 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD", bytes,
556 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900557 StringAppendF(&alertQuotaCmd, "COMMIT\n");
558
559 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrallc6c67342011-10-07 16:28:54 -0700560}
561
562int BandwidthController::setGlobalAlert(int64_t bytes) {
563 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700564 int res = 0;
565
566 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000567 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700568 return -1;
569 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900570 if (mGlobalAlertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700571 res = updateQuota(alertName, bytes);
572 } else {
573 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900574 if (mGlobalAlertTetherCount) {
575 ALOGV("setGlobalAlert for %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700576 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
577 }
JP Abgrall8a932722011-07-13 19:17:35 -0700578 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900579 mGlobalAlertBytes = bytes;
JP Abgrall8a932722011-07-13 19:17:35 -0700580 return res;
581}
582
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900583int BandwidthController::setGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700584 const char *alertName = ALERT_GLOBAL_NAME;
585 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700586
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900587 mGlobalAlertTetherCount++;
588 ALOGV("setGlobalAlertInForwardChain(): %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700589
590 /*
591 * If there is no globalAlert active we are done.
592 * If there is an active globalAlert but this is not the 1st
593 * tether, we are also done.
594 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900595 if (!mGlobalAlertBytes || mGlobalAlertTetherCount != 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700596 return 0;
597 }
598
599 /* We only add the rule if this was the 1st tether added. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900600 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700601 return res;
602}
603
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900604int BandwidthController::removeGlobalAlert() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700605
606 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700607 int res = 0;
608
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900609 if (!mGlobalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000610 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700611 return -1;
612 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900613 res = runIptablesAlertCmd(IptOpDelete, alertName, mGlobalAlertBytes);
614 if (mGlobalAlertTetherCount) {
615 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700616 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900617 mGlobalAlertBytes = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700618 return res;
619}
620
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900621int BandwidthController::removeGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700622 int res = 0;
623 const char *alertName = ALERT_GLOBAL_NAME;
624
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900625 if (!mGlobalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000626 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700627 return -1;
628 }
629
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900630 mGlobalAlertTetherCount--;
JP Abgrallc6c67342011-10-07 16:28:54 -0700631 /*
632 * If there is no globalAlert active we are done.
633 * If there is an active globalAlert but there are more
634 * tethers, we are also done.
635 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900636 if (!mGlobalAlertBytes || mGlobalAlertTetherCount >= 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700637 return 0;
638 }
639
640 /* We only detete the rule if this was the last tether removed. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900641 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700642 return res;
643}
644
JP Abgrall8a932722011-07-13 19:17:35 -0700645int BandwidthController::setSharedAlert(int64_t bytes) {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900646 if (!mSharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000647 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700648 return -1;
649 }
650 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000651 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700652 return -1;
653 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900654 return setCostlyAlert("shared", bytes, &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700655}
656
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900657int BandwidthController::removeSharedAlert() {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900658 return removeCostlyAlert("shared", &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700659}
660
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900661int BandwidthController::setInterfaceAlert(const std::string& iface, int64_t bytes) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700662 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900663 ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700664 return -1;
665 }
666
JP Abgrall8a932722011-07-13 19:17:35 -0700667 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000668 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700669 return -1;
670 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900671 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700672
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900673 if (it == mQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000674 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700675 return -1;
676 }
677
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900678 return setCostlyAlert(iface, bytes, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700679}
680
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900681int BandwidthController::removeInterfaceAlert(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700682 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900683 ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700684 return -1;
685 }
686
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900687 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700688
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900689 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900690 ALOGE("No prior alert set for interface %s", iface.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700691 return -1;
692 }
693
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900694 return removeCostlyAlert(iface, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700695}
696
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900697int BandwidthController::setCostlyAlert(const std::string& costName, int64_t bytes,
698 int64_t* alertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700699 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700700
JP Abgrall69261cb2014-06-19 18:35:24 -0700701 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900702 ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700703 return -1;
704 }
705
JP Abgrall8a932722011-07-13 19:17:35 -0700706 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000707 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700708 return -1;
709 }
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900710
711 std::string alertName = costName + "Alert";
712 std::string chainName = "bw_costly_" + costName;
JP Abgrall8a932722011-07-13 19:17:35 -0700713 if (*alertBytes) {
714 res = updateQuota(alertName, *alertBytes);
715 } else {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900716 std::vector<std::string> commands = {
717 "*filter\n",
718 StringPrintf(ALERT_IPT_TEMPLATE, "-A", chainName.c_str(), bytes, alertName.c_str()),
719 "COMMIT\n"
720 };
721 res = iptablesRestoreFunction(V4V6, Join(commands, ""), nullptr);
722 if (res) {
723 ALOGE("Failed to set costly alert for %s", costName.c_str());
724 }
JP Abgrall8a932722011-07-13 19:17:35 -0700725 }
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900726 if (res == 0) {
727 *alertBytes = bytes;
728 }
JP Abgrall8a932722011-07-13 19:17:35 -0700729 return res;
730}
731
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900732int BandwidthController::removeCostlyAlert(const std::string& costName, int64_t* alertBytes) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700733 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900734 ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700735 return -1;
736 }
737
JP Abgrall8a932722011-07-13 19:17:35 -0700738 if (!*alertBytes) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900739 ALOGE("No prior alert set for %s alert", costName.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700740 return -1;
741 }
742
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900743 std::string alertName = costName + "Alert";
744 std::string chainName = "bw_costly_" + costName;
745 std::vector<std::string> commands = {
746 "*filter\n",
747 StringPrintf(ALERT_IPT_TEMPLATE, "-D", chainName.c_str(), *alertBytes, alertName.c_str()),
748 "COMMIT\n"
749 };
750 if (iptablesRestoreFunction(V4V6, Join(commands, ""), nullptr) != 0) {
751 ALOGE("Failed to remove costly alert %s", costName.c_str());
752 return -1;
753 }
JP Abgrall8a932722011-07-13 19:17:35 -0700754
755 *alertBytes = 0;
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900756 return 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700757}
JP Abgralldb7da582011-09-18 12:57:32 -0700758
JP Abgrall0e540ec2013-08-26 15:13:10 -0700759void BandwidthController::flushExistingCostlyTables(bool doClean) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900760 std::string fullCmd = "*filter\n-S\nCOMMIT\n";
761 std::string ruleList;
JP Abgrall0e540ec2013-08-26 15:13:10 -0700762
763 /* Only lookup ip4 table names as ip6 will have the same tables ... */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900764 if (int ret = iptablesRestoreFunction(V4, fullCmd, &ruleList)) {
765 ALOGE("Failed to list existing costly tables ret=%d", ret);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700766 return;
767 }
768 /* ... then flush/clean both ip4 and ip6 iptables. */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900769 parseAndFlushCostlyTables(ruleList, doClean);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700770}
771
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900772void BandwidthController::parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove) {
773 std::stringstream stream(ruleList);
774 std::string rule;
775 std::vector<std::string> clearCommands = { "*filter" };
776 std::string chainName;
JP Abgrall0e540ec2013-08-26 15:13:10 -0700777
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900778 // Find and flush all rules starting with "-N bw_costly_<iface>" except "-N bw_costly_shared".
779 while (std::getline(stream, rule, '\n')) {
780 if (rule.find(NEW_CHAIN_COMMAND) != 0) continue;
781 chainName = rule.substr(NEW_CHAIN_COMMAND.size());
782 ALOGV("parse chainName=<%s> orig line=<%s>", chainName.c_str(), rule.c_str());
783
784 if (chainName.find("bw_costly_") != 0 || chainName == std::string("bw_costly_shared")) {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700785 continue;
786 }
787
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900788 clearCommands.push_back(StringPrintf(":%s -", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -0700789 if (doRemove) {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900790 clearCommands.push_back(StringPrintf("-X %s", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -0700791 }
792 }
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900793
794 if (clearCommands.size() == 1) {
795 // No rules found.
796 return;
797 }
798
799 clearCommands.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900800 iptablesRestoreFunction(V4V6, Join(clearCommands, '\n'), nullptr);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700801}
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900802
803inline const char *BandwidthController::opToString(IptOp op) {
804 switch (op) {
805 case IptOpInsert:
806 return "-I";
807 case IptOpDelete:
808 return "-D";
809 }
810}
811
812inline const char *BandwidthController::jumpToString(IptJumpOp jumpHandling) {
813 /*
814 * Must be careful what one rejects with, as upper layer protocols will just
815 * keep on hammering the device until the number of retries are done.
816 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
817 */
818 switch (jumpHandling) {
819 case IptJumpNoAdd:
820 return "";
821 case IptJumpReject:
822 return " --jump REJECT";
823 case IptJumpReturn:
824 return " --jump RETURN";
825 }
826}