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