blob: d66ccf57fbe3e069196c455bf8b082789b330ea4 [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),
310 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
311 };
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900312 if (mSharedQuotaIfaces.empty()) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900313 cmds.push_back(StringPrintf("-I %s -m quota2 ! --quota %" PRId64
314 " --name %s --jump REJECT",
315 chain, maxBytes, cost));
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900316 }
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900317 cmds.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900318
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900319 res |= iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900320 if (res) {
321 ALOGE("Failed set quota rule");
322 removeInterfaceSharedQuota(iface);
323 return -1;
324 }
325 mSharedQuotaBytes = maxBytes;
326 mSharedQuotaIfaces.insert(iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700327 }
328
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900329 if (maxBytes != mSharedQuotaBytes) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900330 res |= updateQuota(cost, maxBytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700331 if (res) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900332 ALOGE("Failed update quota for %s", cost);
333 removeInterfaceSharedQuota(iface);
334 return -1;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700335 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900336 mSharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700337 }
338 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700339}
340
JP Abgrall8a932722011-07-13 19:17:35 -0700341/* It will also cleanup any shared alerts */
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900342int BandwidthController::removeInterfaceSharedQuota(const std::string& iface) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900343 constexpr char cost[] = "shared";
344 constexpr char chain[] = "bw_costly_shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700345
JP Abgrall69261cb2014-06-19 18:35:24 -0700346 if (!isIfaceName(iface))
347 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700348
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900349 auto it = mSharedQuotaIfaces.find(iface);
350
351 if (it == mSharedQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900352 ALOGE("No such iface %s to delete", iface.c_str());
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700353 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700354 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700355
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900356 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900357 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900358 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain),
359 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain),
360 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain),
361 };
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900362 if (mSharedQuotaIfaces.size() == 1) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900363 cmds.push_back(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64
364 " --name %s --jump REJECT",
365 chain, mSharedQuotaBytes, cost));
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700366 }
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900367 cmds.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900368
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900369 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
370 ALOGE("Failed to remove shared quota on %s", iface.c_str());
371 return -1;
372 }
373
374 int res = 0;
375 mSharedQuotaIfaces.erase(it);
376 if (mSharedQuotaIfaces.empty()) {
377 mSharedQuotaBytes = 0;
378 if (mSharedAlertBytes) {
379 res = removeSharedAlert();
380 if (res == 0) {
381 mSharedAlertBytes = 0;
382 }
383 }
384 }
385
386 return res;
387
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700388}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700389
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900390int BandwidthController::setInterfaceQuota(const std::string& iface, int64_t maxBytes) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900391 const std::string& cost = iface;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700392
JP Abgrall69261cb2014-06-19 18:35:24 -0700393 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700394 return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700395
JP Abgrall8a932722011-07-13 19:17:35 -0700396 if (!maxBytes) {
397 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000398 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700399 return -1;
400 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700401 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700402 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700403 }
404
JP Abgrall0dad7c22011-06-24 11:58:14 -0700405 /* Insert ingress quota. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900406 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700407
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900408 if (it != mQuotaIfaces.end()) {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900409 if (updateQuota(cost, maxBytes) != 0) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900410 ALOGE("Failed update quota for %s", iface.c_str());
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900411 removeInterfaceQuota(iface);
412 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700413 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900414 it->second.quota = maxBytes;
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900415 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700416 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700417
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900418 const std::string chain = "bw_costly_" + iface;
419 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
420 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900421 "*filter",
422 StringPrintf(":%s -", chain.c_str()),
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900423 StringPrintf("-A %s -j bw_penalty_box", chain.c_str()),
424 StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(),
425 chain.c_str()),
426 StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(),
427 chain.c_str()),
428 StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
429 StringPrintf("-A %s -m quota2 ! --quota %" PRId64 " --name %s --jump REJECT",
430 chain.c_str(), maxBytes, cost.c_str()),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900431 "COMMIT\n",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900432 };
433
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900434 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900435 ALOGE("Failed set quota rule");
436 removeInterfaceQuota(iface);
437 return -1;
438 }
439
440 mQuotaIfaces[iface] = QuotaInfo{maxBytes, 0};
441 return 0;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700442}
443
JP Abgrall8a932722011-07-13 19:17:35 -0700444int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
445 return getInterfaceQuota("shared", bytes);
446}
447
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900448int BandwidthController::getInterfaceQuota(const std::string& iface, int64_t* bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900449 const auto& sys = android::netdutils::sSyscalls.get();
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900450 const std::string fname = "/proc/net/xt_quota/" + iface;
JP Abgrall8a932722011-07-13 19:17:35 -0700451
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900452 if (!isIfaceName(iface)) return -1;
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700453
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900454 StatusOr<UniqueFile> file = sys.fopen(fname, "re");
455 if (!isOk(file)) {
456 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700457 return -1;
458 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900459 auto rv = sys.fscanf(file.value().get(), "%" SCNd64, bytes);
460 if (!isOk(rv)) {
461 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(rv).c_str());
462 return -1;
463 }
464 ALOGV("Read quota res=%d bytes=%" PRId64, rv.value(), *bytes);
465 return rv.value() == 1 ? 0 : -1;
JP Abgrall8a932722011-07-13 19:17:35 -0700466}
467
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900468int BandwidthController::removeInterfaceQuota(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700469 if (!isIfaceName(iface))
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700470 return -1;
JP Abgrall26e0d492011-06-24 19:21:51 -0700471
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900472 auto it = mQuotaIfaces.find(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700473
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900474 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900475 ALOGE("No such iface %s to delete", iface.c_str());
JP Abgrall0dad7c22011-06-24 11:58:14 -0700476 return -1;
477 }
478
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900479 const std::string chain = "bw_costly_" + iface;
480 std::vector<std::string> cmds = {
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900481 "*filter",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900482 StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain.c_str()),
483 StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain.c_str()),
484 StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()),
485 StringPrintf("-F %s", chain.c_str()),
486 StringPrintf("-X %s", chain.c_str()),
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900487 "COMMIT\n",
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900488 };
Lorenzo Colitti48f83002017-07-06 15:06:04 +0900489
490 const int res = iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700491
Lorenzo Colittib7ac3f72017-07-06 16:52:52 +0900492 if (res == 0) {
493 mQuotaIfaces.erase(it);
494 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700495
496 return res;
497}
JP Abgrall8a932722011-07-13 19:17:35 -0700498
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900499int BandwidthController::updateQuota(const std::string& quotaName, int64_t bytes) {
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900500 const auto& sys = android::netdutils::sSyscalls.get();
501 const std::string fname = "/proc/net/xt_quota/" + quotaName;
JP Abgrall8a932722011-07-13 19:17:35 -0700502
JP Abgrall69261cb2014-06-19 18:35:24 -0700503 if (!isIfaceName(quotaName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900504 ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700505 return -1;
506 }
507
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900508 StatusOr<UniqueFile> file = sys.fopen(fname, "we");
509 if (!isOk(file)) {
510 ALOGE("Updating quota %s failed (%s)", quotaName.c_str(), toString(file).c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700511 return -1;
512 }
Joel Scherpelz01cc5492017-06-16 10:45:14 +0900513 sys.fprintf(file.value().get(), "%" PRId64 "\n", bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700514 return 0;
515}
516
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900517int BandwidthController::runIptablesAlertCmd(IptOp op, const std::string& alertName,
518 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900519 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900520 std::string alertQuotaCmd = "*filter\n";
JP Abgrall8a932722011-07-13 19:17:35 -0700521
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900522 // TODO: consider using an alternate template for the delete that does not include the --quota
523 // value. This code works because the --quota value is ignored by deletes
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900524 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT", bytes,
525 alertName.c_str());
526 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT", bytes,
527 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900528 StringAppendF(&alertQuotaCmd, "COMMIT\n");
529
Lorenzo Colitti4773cb42017-04-27 14:03:25 +0900530 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrall8a932722011-07-13 19:17:35 -0700531}
532
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900533int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const std::string& alertName,
534 int64_t bytes) {
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900535 const char *opFlag = opToString(op);
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900536 std::string alertQuotaCmd = "*filter\n";
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900537 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD", bytes,
538 alertName.c_str());
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900539 StringAppendF(&alertQuotaCmd, "COMMIT\n");
540
541 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
JP Abgrallc6c67342011-10-07 16:28:54 -0700542}
543
544int BandwidthController::setGlobalAlert(int64_t bytes) {
545 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700546 int res = 0;
547
548 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000549 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700550 return -1;
551 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900552 if (mGlobalAlertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700553 res = updateQuota(alertName, bytes);
554 } else {
555 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900556 if (mGlobalAlertTetherCount) {
557 ALOGV("setGlobalAlert for %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700558 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
559 }
JP Abgrall8a932722011-07-13 19:17:35 -0700560 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900561 mGlobalAlertBytes = bytes;
JP Abgrall8a932722011-07-13 19:17:35 -0700562 return res;
563}
564
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900565int BandwidthController::setGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700566 const char *alertName = ALERT_GLOBAL_NAME;
567 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700568
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900569 mGlobalAlertTetherCount++;
570 ALOGV("setGlobalAlertInForwardChain(): %d tether", mGlobalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700571
572 /*
573 * If there is no globalAlert active we are done.
574 * If there is an active globalAlert but this is not the 1st
575 * tether, we are also done.
576 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900577 if (!mGlobalAlertBytes || mGlobalAlertTetherCount != 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700578 return 0;
579 }
580
581 /* We only add the rule if this was the 1st tether added. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900582 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700583 return res;
584}
585
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900586int BandwidthController::removeGlobalAlert() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700587
588 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700589 int res = 0;
590
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900591 if (!mGlobalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000592 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700593 return -1;
594 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900595 res = runIptablesAlertCmd(IptOpDelete, alertName, mGlobalAlertBytes);
596 if (mGlobalAlertTetherCount) {
597 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700598 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900599 mGlobalAlertBytes = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700600 return res;
601}
602
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900603int BandwidthController::removeGlobalAlertInForwardChain() {
JP Abgrallc6c67342011-10-07 16:28:54 -0700604 int res = 0;
605 const char *alertName = ALERT_GLOBAL_NAME;
606
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900607 if (!mGlobalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000608 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700609 return -1;
610 }
611
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900612 mGlobalAlertTetherCount--;
JP Abgrallc6c67342011-10-07 16:28:54 -0700613 /*
614 * If there is no globalAlert active we are done.
615 * If there is an active globalAlert but there are more
616 * tethers, we are also done.
617 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900618 if (!mGlobalAlertBytes || mGlobalAlertTetherCount >= 1) {
JP Abgrallc6c67342011-10-07 16:28:54 -0700619 return 0;
620 }
621
622 /* We only detete the rule if this was the last tether removed. */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900623 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700624 return res;
625}
626
JP Abgrall8a932722011-07-13 19:17:35 -0700627int BandwidthController::setSharedAlert(int64_t bytes) {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900628 if (!mSharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000629 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700630 return -1;
631 }
632 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000633 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700634 return -1;
635 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900636 return setCostlyAlert("shared", bytes, &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700637}
638
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900639int BandwidthController::removeSharedAlert() {
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900640 return removeCostlyAlert("shared", &mSharedAlertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700641}
642
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900643int BandwidthController::setInterfaceAlert(const std::string& iface, int64_t bytes) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700644 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900645 ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700646 return -1;
647 }
648
JP Abgrall8a932722011-07-13 19:17:35 -0700649 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000650 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700651 return -1;
652 }
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900653 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700654
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900655 if (it == mQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000656 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700657 return -1;
658 }
659
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900660 return setCostlyAlert(iface, bytes, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700661}
662
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900663int BandwidthController::removeInterfaceAlert(const std::string& iface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700664 if (!isIfaceName(iface)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900665 ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700666 return -1;
667 }
668
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900669 auto it = mQuotaIfaces.find(iface);
JP Abgrall8a932722011-07-13 19:17:35 -0700670
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900671 if (it == mQuotaIfaces.end()) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900672 ALOGE("No prior alert set for interface %s", iface.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700673 return -1;
674 }
675
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900676 return removeCostlyAlert(iface, &it->second.alert);
JP Abgrall8a932722011-07-13 19:17:35 -0700677}
678
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900679int BandwidthController::setCostlyAlert(const std::string& costName, int64_t bytes,
680 int64_t* alertBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700681 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700682
JP Abgrall69261cb2014-06-19 18:35:24 -0700683 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900684 ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700685 return -1;
686 }
687
JP Abgrall8a932722011-07-13 19:17:35 -0700688 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000689 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700690 return -1;
691 }
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900692
693 std::string alertName = costName + "Alert";
694 std::string chainName = "bw_costly_" + costName;
JP Abgrall8a932722011-07-13 19:17:35 -0700695 if (*alertBytes) {
696 res = updateQuota(alertName, *alertBytes);
697 } else {
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900698 std::vector<std::string> commands = {
699 "*filter\n",
700 StringPrintf(ALERT_IPT_TEMPLATE, "-A", chainName.c_str(), bytes, alertName.c_str()),
701 "COMMIT\n"
702 };
703 res = iptablesRestoreFunction(V4V6, Join(commands, ""), nullptr);
704 if (res) {
705 ALOGE("Failed to set costly alert for %s", costName.c_str());
706 }
JP Abgrall8a932722011-07-13 19:17:35 -0700707 }
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900708 if (res == 0) {
709 *alertBytes = bytes;
710 }
JP Abgrall8a932722011-07-13 19:17:35 -0700711 return res;
712}
713
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900714int BandwidthController::removeCostlyAlert(const std::string& costName, int64_t* alertBytes) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700715 if (!isIfaceName(costName)) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900716 ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName.c_str());
Nick Kralevich0b2b9022014-05-01 13:10:45 -0700717 return -1;
718 }
719
JP Abgrall8a932722011-07-13 19:17:35 -0700720 if (!*alertBytes) {
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900721 ALOGE("No prior alert set for %s alert", costName.c_str());
JP Abgrall8a932722011-07-13 19:17:35 -0700722 return -1;
723 }
724
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900725 std::string alertName = costName + "Alert";
726 std::string chainName = "bw_costly_" + costName;
727 std::vector<std::string> commands = {
728 "*filter\n",
729 StringPrintf(ALERT_IPT_TEMPLATE, "-D", chainName.c_str(), *alertBytes, alertName.c_str()),
730 "COMMIT\n"
731 };
732 if (iptablesRestoreFunction(V4V6, Join(commands, ""), nullptr) != 0) {
733 ALOGE("Failed to remove costly alert %s", costName.c_str());
734 return -1;
735 }
JP Abgrall8a932722011-07-13 19:17:35 -0700736
737 *alertBytes = 0;
Lorenzo Colittie85ffe12017-07-06 17:25:37 +0900738 return 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700739}
JP Abgralldb7da582011-09-18 12:57:32 -0700740
JP Abgrall0e540ec2013-08-26 15:13:10 -0700741void BandwidthController::flushExistingCostlyTables(bool doClean) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900742 std::string fullCmd = "*filter\n-S\nCOMMIT\n";
743 std::string ruleList;
JP Abgrall0e540ec2013-08-26 15:13:10 -0700744
745 /* Only lookup ip4 table names as ip6 will have the same tables ... */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900746 if (int ret = iptablesRestoreFunction(V4, fullCmd, &ruleList)) {
747 ALOGE("Failed to list existing costly tables ret=%d", ret);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700748 return;
749 }
750 /* ... then flush/clean both ip4 and ip6 iptables. */
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900751 parseAndFlushCostlyTables(ruleList, doClean);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700752}
753
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900754void BandwidthController::parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove) {
755 std::stringstream stream(ruleList);
756 std::string rule;
757 std::vector<std::string> clearCommands = { "*filter" };
758 std::string chainName;
JP Abgrall0e540ec2013-08-26 15:13:10 -0700759
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900760 // Find and flush all rules starting with "-N bw_costly_<iface>" except "-N bw_costly_shared".
761 while (std::getline(stream, rule, '\n')) {
762 if (rule.find(NEW_CHAIN_COMMAND) != 0) continue;
763 chainName = rule.substr(NEW_CHAIN_COMMAND.size());
764 ALOGV("parse chainName=<%s> orig line=<%s>", chainName.c_str(), rule.c_str());
765
766 if (chainName.find("bw_costly_") != 0 || chainName == std::string("bw_costly_shared")) {
JP Abgrall0e540ec2013-08-26 15:13:10 -0700767 continue;
768 }
769
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900770 clearCommands.push_back(StringPrintf(":%s -", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -0700771 if (doRemove) {
Lorenzo Colitti3c272702017-04-26 15:48:13 +0900772 clearCommands.push_back(StringPrintf("-X %s", chainName.c_str()));
JP Abgrall0e540ec2013-08-26 15:13:10 -0700773 }
774 }
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900775
776 if (clearCommands.size() == 1) {
777 // No rules found.
778 return;
779 }
780
781 clearCommands.push_back("COMMIT\n");
Joel Scherpelzd59526a2017-06-28 16:24:09 +0900782 iptablesRestoreFunction(V4V6, Join(clearCommands, '\n'), nullptr);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700783}
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900784
785inline const char *BandwidthController::opToString(IptOp op) {
786 switch (op) {
787 case IptOpInsert:
788 return "-I";
789 case IptOpDelete:
790 return "-D";
791 }
792}
793
794inline const char *BandwidthController::jumpToString(IptJumpOp jumpHandling) {
795 /*
796 * Must be careful what one rejects with, as upper layer protocols will just
797 * keep on hammering the device until the number of retries are done.
798 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
799 */
800 switch (jumpHandling) {
801 case IptJumpNoAdd:
802 return "";
803 case IptJumpReject:
804 return " --jump REJECT";
805 case IptJumpReturn:
806 return " --jump RETURN";
807 }
808}