1 /*
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
17 // #define LOG_NDEBUG 0
18
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
25 #include <ctype.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <string>
32 #include <vector>
33
34 #define __STDC_FORMAT_MACROS 1
35 #include <inttypes.h>
36
37 #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
46 #include "android-base/stringprintf.h"
47 #include "android-base/strings.h"
48 #define LOG_TAG "BandwidthController"
49 #include <cutils/properties.h>
50 #include <log/log.h>
51
52 #include <netdutils/Syscalls.h>
53 #include "BandwidthController.h"
54 #include "Controllers.h"
55 #include "FirewallController.h" /* For makeCriticalCommands */
56 #include "Fwmark.h"
57 #include "NetdConstants.h"
58 #include "TrafficController.h"
59 #include "bpf/BpfUtils.h"
60
61 /* Alphabetical */
62 #define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %" PRId64" --name %s\n"
63 const char BandwidthController::LOCAL_INPUT[] = "bw_INPUT";
64 const char BandwidthController::LOCAL_FORWARD[] = "bw_FORWARD";
65 const char BandwidthController::LOCAL_OUTPUT[] = "bw_OUTPUT";
66 const char BandwidthController::LOCAL_RAW_PREROUTING[] = "bw_raw_PREROUTING";
67 const char BandwidthController::LOCAL_MANGLE_POSTROUTING[] = "bw_mangle_POSTROUTING";
68 const char BandwidthController::LOCAL_GLOBAL_ALERT[] = "bw_global_alert";
69
70 auto BandwidthController::iptablesRestoreFunction = execIptablesRestoreWithOutput;
71
72 using android::base::Join;
73 using android::base::StartsWith;
74 using android::base::StringAppendF;
75 using android::base::StringPrintf;
76 using android::net::FirewallController;
77 using android::net::gCtls;
78 using android::netdutils::Status;
79 using android::netdutils::StatusOr;
80 using android::netdutils::UniqueFile;
81
82 namespace {
83
84 const char ALERT_GLOBAL_NAME[] = "globalAlert";
85 const std::string NEW_CHAIN_COMMAND = "-N ";
86
87 /**
88 * Some comments about the rules:
89 * * Ordering
90 * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
91 * E.g. "-I bw_INPUT -i rmnet0 -j costly"
92 * - quota'd rules in the costly chain should be before bw_penalty_box lookups.
93 * - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
94 *
95 * * global quota vs per interface quota
96 * - global quota for all costly interfaces uses a single costly chain:
97 * . initial rules
98 * iptables -N bw_costly_shared
99 * iptables -I bw_INPUT -i iface0 -j bw_costly_shared
100 * iptables -I bw_OUTPUT -o iface0 -j bw_costly_shared
101 * iptables -I bw_costly_shared -m quota \! --quota 500000 \
102 * -j REJECT --reject-with icmp-net-prohibited
103 * iptables -A bw_costly_shared -j bw_penalty_box
104 * iptables -A bw_penalty_box -j bw_happy_box
105 * iptables -A bw_happy_box -j bw_data_saver
106 *
107 * . adding a new iface to this, E.g.:
108 * iptables -I bw_INPUT -i iface1 -j bw_costly_shared
109 * iptables -I bw_OUTPUT -o iface1 -j bw_costly_shared
110 *
111 * - quota per interface. This is achieve by having "costly" chains per quota.
112 * E.g. adding a new costly interface iface0 with its own quota:
113 * iptables -N bw_costly_iface0
114 * iptables -I bw_INPUT -i iface0 -j bw_costly_iface0
115 * iptables -I bw_OUTPUT -o iface0 -j bw_costly_iface0
116 * iptables -A bw_costly_iface0 -m quota \! --quota 500000 \
117 * -j REJECT --reject-with icmp-port-unreachable
118 * iptables -A bw_costly_iface0 -j bw_penalty_box
119 *
120 * * Penalty box, happy box and data saver.
121 * - bw_penalty box is a denylist of apps that are rejected.
122 * - bw_happy_box is an allowlist of apps. It always includes all system apps
123 * - bw_data_saver implements data usage restrictions.
124 * - Via the UI the user can add and remove apps from the allowlist and
125 * denylist, and turn on/off data saver.
126 * - The denylist takes precedence over the allowlist and the allowlist
127 * takes precedence over data saver.
128 *
129 * * bw_penalty_box handling:
130 * - only one bw_penalty_box for all interfaces
131 * E.g Adding an app:
132 * iptables -I bw_penalty_box -m owner --uid-owner app_3 \
133 * -j REJECT --reject-with icmp-port-unreachable
134 *
135 * * bw_happy_box handling:
136 * - The bw_happy_box comes after the penalty box.
137 * E.g Adding a happy app,
138 * iptables -I bw_happy_box -m owner --uid-owner app_3 \
139 * -j RETURN
140 *
141 * * bw_data_saver handling:
142 * - The bw_data_saver comes after the happy box.
143 * Enable data saver:
144 * iptables -R 1 bw_data_saver -j REJECT --reject-with icmp-port-unreachable
145 * Disable data saver:
146 * iptables -R 1 bw_data_saver -j RETURN
147 */
148
149 const std::string COMMIT_AND_CLOSE = "COMMIT\n";
150 const std::string BPF_PENALTY_BOX_MATCH_DENYLIST_COMMAND = StringPrintf(
151 "-I bw_penalty_box -m bpf --object-pinned %s -j REJECT", XT_BPF_DENYLIST_PROG_PATH);
152
153 static const std::vector<std::string> IPT_FLUSH_COMMANDS = {
154 /*
155 * Cleanup rules.
156 * Should normally include bw_costly_<iface>, but we rely on the way they are setup
157 * to allow coexistance.
158 */
159 "*filter",
160 ":bw_INPUT -",
161 ":bw_OUTPUT -",
162 ":bw_FORWARD -",
163 ":bw_happy_box -",
164 ":bw_penalty_box -",
165 ":bw_data_saver -",
166 ":bw_costly_shared -",
167 ":bw_global_alert -",
168 "COMMIT",
169 "*raw",
170 ":bw_raw_PREROUTING -",
171 "COMMIT",
172 "*mangle",
173 ":bw_mangle_POSTROUTING -",
174 COMMIT_AND_CLOSE};
175
176 static const uint32_t uidBillingMask = Fwmark::getUidBillingMask();
177
178 /**
179 * Basic commands for creation of hooks into data accounting and data boxes.
180 *
181 * Included in these commands are rules to prevent the double-counting of IPsec
182 * packets. The general overview is as follows:
183 * > All interface counters (counted in PREROUTING, POSTROUTING) must be
184 * completely accurate, and count only the outer packet. As such, the inner
185 * packet must be ignored, which is done through the use of two rules: use
186 * of the policy module (for tunnel mode), and VTI interface checks (for
187 * tunnel or transport-in-tunnel mode). The VTI interfaces should be named
188 * ipsec*
189 * > Outbound UID billing can always be done with the outer packets, due to the
190 * ability to always find the correct UID (based on the skb->sk). As such,
191 * the inner packets should be ignored based on the policy module, or the
192 * output interface if a VTI (ipsec+)
193 * > Inbound UDP-encap-ESP packets can be correctly mapped to the UID that
194 * opened the encap socket, and as such, should be billed as early as
195 * possible (for transport mode; tunnel mode usage should be billed to
196 * sending/receiving application). Due to the inner packet being
197 * indistinguishable from the inner packet of ESP, a uidBillingDone mark
198 * has to be applied to prevent counting a second time.
199 * > Inbound ESP has no socket, and as such must be accounted later. ESP
200 * protocol packets are skipped via a blanket rule.
201 * > Note that this solution is asymmetrical. Adding the VTI or policy matcher
202 * ignore rule in the input chain would actually break the INPUT chain;
203 * Those rules are designed to ignore inner packets, and in the tunnel
204 * mode UDP, or any ESP case, we would not have billed the outer packet.
205 *
206 * See go/ipsec-data-accounting for more information.
207 */
208
getBasicAccountingCommands()209 std::vector<std::string> getBasicAccountingCommands() {
210 // clang-format off
211 std::vector<std::string> ipt_basic_accounting_commands = {
212 "*filter",
213
214 "-A bw_INPUT -j bw_global_alert",
215 // Prevents IPSec double counting (ESP and UDP-encap-ESP respectively)
216 "-A bw_INPUT -p esp -j RETURN",
217 StringPrintf("-A bw_INPUT -m mark --mark 0x%x/0x%x -j RETURN", uidBillingMask,
218 uidBillingMask),
219 StringPrintf("-A bw_INPUT -j MARK --or-mark 0x%x", uidBillingMask),
220 "-A bw_OUTPUT -j bw_global_alert",
221 "-A bw_costly_shared -j bw_penalty_box",
222 ("-I bw_penalty_box -m bpf --object-pinned " XT_BPF_DENYLIST_PROG_PATH " -j REJECT"),
223 "-A bw_penalty_box -j bw_happy_box",
224 "-A bw_happy_box -j bw_data_saver",
225 "-A bw_data_saver -j RETURN",
226 ("-I bw_happy_box -m bpf --object-pinned " XT_BPF_ALLOWLIST_PROG_PATH " -j RETURN"),
227 "COMMIT",
228
229 "*raw",
230 // Prevents IPSec double counting (Tunnel mode and Transport mode,
231 // respectively)
232 ("-A bw_raw_PREROUTING -i " IPSEC_IFACE_PREFIX "+ -j RETURN"),
233 "-A bw_raw_PREROUTING -m policy --pol ipsec --dir in -j RETURN",
234 // This is ingress interface accounting. There is no need to do anything specific
235 // for 464xlat here, because we only ever account 464xlat traffic on the clat
236 // interface and later correct for overhead (+20 bytes/packet).
237 //
238 // Note: eBPF offloaded packets never hit base interface's ip6tables, and non
239 // offloaded packets (which when using xt_qtaguid means all packets, because
240 // clat eBPF offload does not work on xt_qtaguid devices) are dropped in
241 // clat_raw_PREROUTING.
242 //
243 // Hence we will never double count and additional corrections are not needed.
244 // We can simply take the sum of base and stacked (+20B/pkt) interface counts.
245 ("-A bw_raw_PREROUTING -m bpf --object-pinned " XT_BPF_INGRESS_PROG_PATH),
246 "COMMIT",
247
248 "*mangle",
249 // Prevents IPSec double counting (Tunnel mode and Transport mode,
250 // respectively)
251 ("-A bw_mangle_POSTROUTING -o " IPSEC_IFACE_PREFIX "+ -j RETURN"),
252 "-A bw_mangle_POSTROUTING -m policy --pol ipsec --dir out -j RETURN",
253 // Clear the uid billing done (egress) mark before sending this packet
254 StringPrintf("-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x%x", uidBillingMask),
255 // Packets from the clat daemon have already been counted on egress through the
256 // stacked v4-* interface.
257 "-A bw_mangle_POSTROUTING -m owner --uid-owner clat -j RETURN",
258 // This is egress interface accounting: we account 464xlat traffic only on
259 // the clat interface (as offloaded packets never hit base interface's ip6tables)
260 // and later sum base and stacked with overhead (+20B/pkt) in higher layers
261 ("-A bw_mangle_POSTROUTING -m bpf --object-pinned " XT_BPF_EGRESS_PROG_PATH),
262 COMMIT_AND_CLOSE};
263 // clang-format on
264 return ipt_basic_accounting_commands;
265 }
266
267 } // namespace
268
BandwidthController()269 BandwidthController::BandwidthController() {
270 }
271
flushCleanTables(bool doClean)272 void BandwidthController::flushCleanTables(bool doClean) {
273 /* Flush and remove the bw_costly_<iface> tables */
274 flushExistingCostlyTables(doClean);
275
276 std::string commands = Join(IPT_FLUSH_COMMANDS, '\n');
277 iptablesRestoreFunction(V4V6, commands, nullptr);
278 }
279
setupIptablesHooks()280 int BandwidthController::setupIptablesHooks() {
281 /* flush+clean is allowed to fail */
282 flushCleanTables(true);
283 return 0;
284 }
285
enableBandwidthControl()286 int BandwidthController::enableBandwidthControl() {
287 /* Let's pretend we started from scratch ... */
288 mSharedQuotaIfaces.clear();
289 mQuotaIfaces.clear();
290 mGlobalAlertBytes = 0;
291 mSharedQuotaBytes = mSharedAlertBytes = 0;
292
293 flushCleanTables(false);
294
295 std::string commands = Join(getBasicAccountingCommands(), '\n');
296 return iptablesRestoreFunction(V4V6, commands, nullptr);
297 }
298
disableBandwidthControl()299 int BandwidthController::disableBandwidthControl() {
300
301 flushCleanTables(false);
302 return 0;
303 }
304
makeDataSaverCommand(IptablesTarget target,bool enable)305 std::string BandwidthController::makeDataSaverCommand(IptablesTarget target, bool enable) {
306 std::string cmd;
307 const char *chainName = "bw_data_saver";
308 const char *op = jumpToString(enable ? IptJumpReject : IptJumpReturn);
309 std::string criticalCommands = enable ?
310 FirewallController::makeCriticalCommands(target, chainName) : "";
311 StringAppendF(&cmd,
312 "*filter\n"
313 ":%s -\n"
314 "%s"
315 "-A %s%s\n"
316 "COMMIT\n", chainName, criticalCommands.c_str(), chainName, op);
317 return cmd;
318 }
319
enableDataSaver(bool enable)320 int BandwidthController::enableDataSaver(bool enable) {
321 int ret = iptablesRestoreFunction(V4, makeDataSaverCommand(V4, enable), nullptr);
322 ret |= iptablesRestoreFunction(V6, makeDataSaverCommand(V6, enable), nullptr);
323 return ret;
324 }
325
addNaughtyApps(const std::vector<uint32_t> & appUids)326 int BandwidthController::addNaughtyApps(const std::vector<uint32_t>& appUids) {
327 return manipulateSpecialApps(appUids, PENALTY_BOX_MATCH, IptOpInsert);
328 }
329
removeNaughtyApps(const std::vector<uint32_t> & appUids)330 int BandwidthController::removeNaughtyApps(const std::vector<uint32_t>& appUids) {
331 return manipulateSpecialApps(appUids, PENALTY_BOX_MATCH, IptOpDelete);
332 }
333
addNiceApps(const std::vector<uint32_t> & appUids)334 int BandwidthController::addNiceApps(const std::vector<uint32_t>& appUids) {
335 return manipulateSpecialApps(appUids, HAPPY_BOX_MATCH, IptOpInsert);
336 }
337
removeNiceApps(const std::vector<uint32_t> & appUids)338 int BandwidthController::removeNiceApps(const std::vector<uint32_t>& appUids) {
339 return manipulateSpecialApps(appUids, HAPPY_BOX_MATCH, IptOpDelete);
340 }
341
manipulateSpecialApps(const std::vector<uint32_t> & appUids,UidOwnerMatchType matchType,IptOp op)342 int BandwidthController::manipulateSpecialApps(const std::vector<uint32_t>& appUids,
343 UidOwnerMatchType matchType, IptOp op) {
344 Status status = gCtls->trafficCtrl.updateUidOwnerMap(appUids, matchType, op);
345 if (!isOk(status)) {
346 ALOGE("unable to update the Bandwidth Uid Map: %s", toString(status).c_str());
347 }
348 return status.code();
349 }
350
setInterfaceSharedQuota(const std::string & iface,int64_t maxBytes)351 int BandwidthController::setInterfaceSharedQuota(const std::string& iface, int64_t maxBytes) {
352 int res = 0;
353 std::string quotaCmd;
354 constexpr char cost[] = "shared";
355 constexpr char chain[] = "bw_costly_shared";
356
357 if (!maxBytes) {
358 /* Don't talk about -1, deprecate it. */
359 ALOGE("Invalid bytes value. 1..max_int64.");
360 return -1;
361 }
362 if (!isIfaceName(iface))
363 return -1;
364
365 if (maxBytes == -1) {
366 return removeInterfaceSharedQuota(iface);
367 }
368
369 auto it = mSharedQuotaIfaces.find(iface);
370
371 if (it == mSharedQuotaIfaces.end()) {
372 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
373 std::vector<std::string> cmds = {
374 "*filter",
375 StringPrintf("-I bw_INPUT %d -i %s -j %s", ruleInsertPos, iface.c_str(), chain),
376 StringPrintf("-I bw_OUTPUT %d -o %s -j %s", ruleInsertPos, iface.c_str(), chain),
377 StringPrintf("-A bw_FORWARD -i %s -j %s", iface.c_str(), chain),
378 StringPrintf("-A bw_FORWARD -o %s -j %s", iface.c_str(), chain),
379 };
380 if (mSharedQuotaIfaces.empty()) {
381 cmds.push_back(StringPrintf("-I %s -m quota2 ! --quota %" PRId64 " --name %s -j REJECT",
382 chain, maxBytes, cost));
383 }
384 cmds.push_back("COMMIT\n");
385
386 res |= iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
387 if (res) {
388 ALOGE("Failed set quota rule");
389 removeInterfaceSharedQuota(iface);
390 return -1;
391 }
392 mSharedQuotaBytes = maxBytes;
393 mSharedQuotaIfaces.insert(iface);
394 }
395
396 if (maxBytes != mSharedQuotaBytes) {
397 res |= updateQuota(cost, maxBytes);
398 if (res) {
399 ALOGE("Failed update quota for %s", cost);
400 removeInterfaceSharedQuota(iface);
401 return -1;
402 }
403 mSharedQuotaBytes = maxBytes;
404 }
405 return 0;
406 }
407
408 /* It will also cleanup any shared alerts */
removeInterfaceSharedQuota(const std::string & iface)409 int BandwidthController::removeInterfaceSharedQuota(const std::string& iface) {
410 constexpr char cost[] = "shared";
411 constexpr char chain[] = "bw_costly_shared";
412
413 if (!isIfaceName(iface))
414 return -1;
415
416 auto it = mSharedQuotaIfaces.find(iface);
417
418 if (it == mSharedQuotaIfaces.end()) {
419 ALOGE("No such iface %s to delete", iface.c_str());
420 return -1;
421 }
422
423 std::vector<std::string> cmds = {
424 "*filter",
425 StringPrintf("-D bw_INPUT -i %s -j %s", iface.c_str(), chain),
426 StringPrintf("-D bw_OUTPUT -o %s -j %s", iface.c_str(), chain),
427 StringPrintf("-D bw_FORWARD -i %s -j %s", iface.c_str(), chain),
428 StringPrintf("-D bw_FORWARD -o %s -j %s", iface.c_str(), chain),
429 };
430 if (mSharedQuotaIfaces.size() == 1) {
431 cmds.push_back(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64 " --name %s -j REJECT",
432 chain, mSharedQuotaBytes, cost));
433 }
434 cmds.push_back("COMMIT\n");
435
436 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
437 ALOGE("Failed to remove shared quota on %s", iface.c_str());
438 return -1;
439 }
440
441 int res = 0;
442 mSharedQuotaIfaces.erase(it);
443 if (mSharedQuotaIfaces.empty()) {
444 mSharedQuotaBytes = 0;
445 if (mSharedAlertBytes) {
446 res = removeSharedAlert();
447 if (res == 0) {
448 mSharedAlertBytes = 0;
449 }
450 }
451 }
452
453 return res;
454
455 }
456
setInterfaceQuota(const std::string & iface,int64_t maxBytes)457 int BandwidthController::setInterfaceQuota(const std::string& iface, int64_t maxBytes) {
458 const std::string& cost = iface;
459
460 if (!isIfaceName(iface)) return -EINVAL;
461
462 if (!maxBytes) {
463 ALOGE("Invalid bytes value. 1..max_int64.");
464 return -ERANGE;
465 }
466 if (maxBytes == -1) {
467 return removeInterfaceQuota(iface);
468 }
469
470 /* Insert ingress quota. */
471 auto it = mQuotaIfaces.find(iface);
472
473 if (it != mQuotaIfaces.end()) {
474 if (int res = updateQuota(cost, maxBytes)) {
475 ALOGE("Failed update quota for %s", iface.c_str());
476 removeInterfaceQuota(iface);
477 return res;
478 }
479 it->second.quota = maxBytes;
480 return 0;
481 }
482
483 const std::string chain = "bw_costly_" + iface;
484 const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1;
485 std::vector<std::string> cmds = {
486 "*filter",
487 StringPrintf(":%s -", chain.c_str()),
488 StringPrintf("-A %s -j bw_penalty_box", chain.c_str()),
489 StringPrintf("-I bw_INPUT %d -i %s -j %s", ruleInsertPos, iface.c_str(), chain.c_str()),
490 StringPrintf("-I bw_OUTPUT %d -o %s -j %s", ruleInsertPos, iface.c_str(),
491 chain.c_str()),
492 StringPrintf("-A bw_FORWARD -i %s -j %s", iface.c_str(), chain.c_str()),
493 StringPrintf("-A bw_FORWARD -o %s -j %s", iface.c_str(), chain.c_str()),
494 StringPrintf("-A %s -m quota2 ! --quota %" PRId64 " --name %s -j REJECT", chain.c_str(),
495 maxBytes, cost.c_str()),
496 "COMMIT\n",
497 };
498 if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) {
499 ALOGE("Failed set quota rule");
500 removeInterfaceQuota(iface);
501 return -EREMOTEIO;
502 }
503
504 mQuotaIfaces[iface] = QuotaInfo{maxBytes, 0};
505 return 0;
506 }
507
getInterfaceSharedQuota(int64_t * bytes)508 int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
509 return getInterfaceQuota("shared", bytes);
510 }
511
getInterfaceQuota(const std::string & iface,int64_t * bytes)512 int BandwidthController::getInterfaceQuota(const std::string& iface, int64_t* bytes) {
513 const auto& sys = android::netdutils::sSyscalls.get();
514 const std::string fname = "/proc/net/xt_quota/" + iface;
515
516 if (!isIfaceName(iface)) return -1;
517
518 StatusOr<UniqueFile> file = sys.fopen(fname, "re");
519 if (!isOk(file)) {
520 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(file).c_str());
521 return -1;
522 }
523 auto rv = sys.fscanf(file.value().get(), "%" SCNd64, bytes);
524 if (!isOk(rv)) {
525 ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(rv).c_str());
526 return -1;
527 }
528 ALOGV("Read quota res=%d bytes=%" PRId64, rv.value(), *bytes);
529 return rv.value() == 1 ? 0 : -1;
530 }
531
removeInterfaceQuota(const std::string & iface)532 int BandwidthController::removeInterfaceQuota(const std::string& iface) {
533 if (!isIfaceName(iface)) return -EINVAL;
534
535 auto it = mQuotaIfaces.find(iface);
536
537 if (it == mQuotaIfaces.end()) {
538 ALOGE("No such iface %s to delete", iface.c_str());
539 return -ENODEV;
540 }
541
542 const std::string chain = "bw_costly_" + iface;
543 std::vector<std::string> cmds = {
544 "*filter",
545 StringPrintf("-D bw_INPUT -i %s -j %s", iface.c_str(), chain.c_str()),
546 StringPrintf("-D bw_OUTPUT -o %s -j %s", iface.c_str(), chain.c_str()),
547 StringPrintf("-D bw_FORWARD -i %s -j %s", iface.c_str(), chain.c_str()),
548 StringPrintf("-D bw_FORWARD -o %s -j %s", iface.c_str(), chain.c_str()),
549 StringPrintf("-F %s", chain.c_str()),
550 StringPrintf("-X %s", chain.c_str()),
551 "COMMIT\n",
552 };
553
554 const int res = iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr);
555
556 if (res == 0) {
557 mQuotaIfaces.erase(it);
558 }
559
560 return res ? -EREMOTEIO : 0;
561 }
562
updateQuota(const std::string & quotaName,int64_t bytes)563 int BandwidthController::updateQuota(const std::string& quotaName, int64_t bytes) {
564 const auto& sys = android::netdutils::sSyscalls.get();
565 const std::string fname = "/proc/net/xt_quota/" + quotaName;
566
567 if (!isIfaceName(quotaName)) {
568 ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName.c_str());
569 return -EINVAL;
570 }
571
572 StatusOr<UniqueFile> file = sys.fopen(fname, "we");
573 if (!isOk(file)) {
574 int res = errno;
575 ALOGE("Updating quota %s failed (%s)", quotaName.c_str(), toString(file).c_str());
576 return -res;
577 }
578 // TODO: should we propagate this error?
579 sys.fprintf(file.value().get(), "%" PRId64 "\n", bytes).ignoreError();
580 return 0;
581 }
582
runIptablesAlertCmd(IptOp op,const std::string & alertName,int64_t bytes)583 int BandwidthController::runIptablesAlertCmd(IptOp op, const std::string& alertName,
584 int64_t bytes) {
585 const char *opFlag = opToString(op);
586 std::string alertQuotaCmd = "*filter\n";
587
588 // TODO: consider using an alternate template for the delete that does not include the --quota
589 // value. This code works because the --quota value is ignored by deletes
590
591 /*
592 * Add alert rule in bw_global_alert chain, 3 chains might reference bw_global_alert.
593 * bw_INPUT, bw_OUTPUT (added by BandwidthController in enableBandwidthControl)
594 * bw_FORWARD (added by TetherController in setTetherGlobalAlertRule if nat enable/disable)
595 */
596 StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, LOCAL_GLOBAL_ALERT, bytes,
597 alertName.c_str());
598 StringAppendF(&alertQuotaCmd, "COMMIT\n");
599
600 return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr);
601 }
602
setGlobalAlert(int64_t bytes)603 int BandwidthController::setGlobalAlert(int64_t bytes) {
604 const char *alertName = ALERT_GLOBAL_NAME;
605
606 if (!bytes) {
607 ALOGE("Invalid bytes value. 1..max_int64.");
608 return -ERANGE;
609 }
610
611 int res = 0;
612 if (mGlobalAlertBytes) {
613 res = updateQuota(alertName, bytes);
614 } else {
615 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
616 if (res) {
617 res = -EREMOTEIO;
618 }
619 }
620 mGlobalAlertBytes = bytes;
621 return res;
622 }
623
removeGlobalAlert()624 int BandwidthController::removeGlobalAlert() {
625
626 const char *alertName = ALERT_GLOBAL_NAME;
627
628 if (!mGlobalAlertBytes) {
629 ALOGE("No prior alert set");
630 return -1;
631 }
632
633 int res = 0;
634 res = runIptablesAlertCmd(IptOpDelete, alertName, mGlobalAlertBytes);
635 mGlobalAlertBytes = 0;
636 return res;
637 }
638
setSharedAlert(int64_t bytes)639 int BandwidthController::setSharedAlert(int64_t bytes) {
640 if (!mSharedQuotaBytes) {
641 ALOGE("Need to have a prior shared quota set to set an alert");
642 return -1;
643 }
644 if (!bytes) {
645 ALOGE("Invalid bytes value. 1..max_int64.");
646 return -1;
647 }
648 return setCostlyAlert("shared", bytes, &mSharedAlertBytes);
649 }
650
removeSharedAlert()651 int BandwidthController::removeSharedAlert() {
652 return removeCostlyAlert("shared", &mSharedAlertBytes);
653 }
654
setInterfaceAlert(const std::string & iface,int64_t bytes)655 int BandwidthController::setInterfaceAlert(const std::string& iface, int64_t bytes) {
656 if (!isIfaceName(iface)) {
657 ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
658 return -EINVAL;
659 }
660
661 if (!bytes) {
662 ALOGE("Invalid bytes value. 1..max_int64.");
663 return -ERANGE;
664 }
665 auto it = mQuotaIfaces.find(iface);
666
667 if (it == mQuotaIfaces.end()) {
668 ALOGE("Need to have a prior interface quota set to set an alert");
669 return -ENOENT;
670 }
671
672 return setCostlyAlert(iface, bytes, &it->second.alert);
673 }
674
removeInterfaceAlert(const std::string & iface)675 int BandwidthController::removeInterfaceAlert(const std::string& iface) {
676 if (!isIfaceName(iface)) {
677 ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface.c_str());
678 return -EINVAL;
679 }
680
681 auto it = mQuotaIfaces.find(iface);
682
683 if (it == mQuotaIfaces.end()) {
684 ALOGE("No prior alert set for interface %s", iface.c_str());
685 return -ENOENT;
686 }
687
688 return removeCostlyAlert(iface, &it->second.alert);
689 }
690
setCostlyAlert(const std::string & costName,int64_t bytes,int64_t * alertBytes)691 int BandwidthController::setCostlyAlert(const std::string& costName, int64_t bytes,
692 int64_t* alertBytes) {
693 int res = 0;
694
695 if (!isIfaceName(costName)) {
696 ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName.c_str());
697 return -EINVAL;
698 }
699
700 if (!bytes) {
701 ALOGE("Invalid bytes value. 1..max_int64.");
702 return -ERANGE;
703 }
704
705 std::string alertName = costName + "Alert";
706 std::string chainName = "bw_costly_" + costName;
707 if (*alertBytes) {
708 res = updateQuota(alertName, *alertBytes);
709 } else {
710 std::vector<std::string> commands = {
711 "*filter\n",
712 StringPrintf(ALERT_IPT_TEMPLATE, "-A", chainName.c_str(), bytes, alertName.c_str()),
713 "COMMIT\n"
714 };
715 res = iptablesRestoreFunction(V4V6, Join(commands, ""), nullptr);
716 if (res) {
717 ALOGE("Failed to set costly alert for %s", costName.c_str());
718 res = -EREMOTEIO;
719 }
720 }
721 if (res == 0) {
722 *alertBytes = bytes;
723 }
724 return res;
725 }
726
removeCostlyAlert(const std::string & costName,int64_t * alertBytes)727 int BandwidthController::removeCostlyAlert(const std::string& costName, int64_t* alertBytes) {
728 if (!isIfaceName(costName)) {
729 ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName.c_str());
730 return -EINVAL;
731 }
732
733 if (!*alertBytes) {
734 ALOGE("No prior alert set for %s alert", costName.c_str());
735 return -ENOENT;
736 }
737
738 std::string alertName = costName + "Alert";
739 std::string chainName = "bw_costly_" + costName;
740 std::vector<std::string> commands = {
741 "*filter\n",
742 StringPrintf(ALERT_IPT_TEMPLATE, "-D", chainName.c_str(), *alertBytes, alertName.c_str()),
743 "COMMIT\n"
744 };
745 if (iptablesRestoreFunction(V4V6, Join(commands, ""), nullptr) != 0) {
746 ALOGE("Failed to remove costly alert %s", costName.c_str());
747 return -EREMOTEIO;
748 }
749
750 *alertBytes = 0;
751 return 0;
752 }
753
flushExistingCostlyTables(bool doClean)754 void BandwidthController::flushExistingCostlyTables(bool doClean) {
755 std::string fullCmd = "*filter\n-S\nCOMMIT\n";
756 std::string ruleList;
757
758 /* Only lookup ip4 table names as ip6 will have the same tables ... */
759 if (int ret = iptablesRestoreFunction(V4, fullCmd, &ruleList)) {
760 ALOGE("Failed to list existing costly tables ret=%d", ret);
761 return;
762 }
763 /* ... then flush/clean both ip4 and ip6 iptables. */
764 parseAndFlushCostlyTables(ruleList, doClean);
765 }
766
parseAndFlushCostlyTables(const std::string & ruleList,bool doRemove)767 void BandwidthController::parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove) {
768 std::stringstream stream(ruleList);
769 std::string rule;
770 std::vector<std::string> clearCommands = { "*filter" };
771 std::string chainName;
772
773 // Find and flush all rules starting with "-N bw_costly_<iface>" except "-N bw_costly_shared".
774 while (std::getline(stream, rule, '\n')) {
775 if (!StartsWith(rule, NEW_CHAIN_COMMAND)) continue;
776 chainName = rule.substr(NEW_CHAIN_COMMAND.size());
777 ALOGV("parse chainName=<%s> orig line=<%s>", chainName.c_str(), rule.c_str());
778
779 if (!StartsWith(chainName, "bw_costly_") || chainName == std::string("bw_costly_shared")) {
780 continue;
781 }
782
783 clearCommands.push_back(StringPrintf(":%s -", chainName.c_str()));
784 if (doRemove) {
785 clearCommands.push_back(StringPrintf("-X %s", chainName.c_str()));
786 }
787 }
788
789 if (clearCommands.size() == 1) {
790 // No rules found.
791 return;
792 }
793
794 clearCommands.push_back("COMMIT\n");
795 iptablesRestoreFunction(V4V6, Join(clearCommands, '\n'), nullptr);
796 }
797
opToString(IptOp op)798 inline const char *BandwidthController::opToString(IptOp op) {
799 switch (op) {
800 case IptOpInsert:
801 return "-I";
802 case IptOpDelete:
803 return "-D";
804 }
805 }
806
jumpToString(IptJumpOp jumpHandling)807 inline const char *BandwidthController::jumpToString(IptJumpOp jumpHandling) {
808 /*
809 * Must be careful what one rejects with, as upper layer protocols will just
810 * keep on hammering the device until the number of retries are done.
811 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
812 */
813 switch (jumpHandling) {
814 case IptJumpReject:
815 return " -j REJECT";
816 case IptJumpReturn:
817 return " -j RETURN";
818 }
819 }
820