blob: b739841e1369e01e02da5c150cd21fb6e445c3ba [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#ifndef _BANDWIDTH_CONTROLLER_H
17#define _BANDWIDTH_CONTROLLER_H
18
19#include <list>
20#include <string>
Joel Scherpelzbcad6612017-05-30 10:55:11 +090021#include <utility>
22#include <vector>
JP Abgralldb7da582011-09-18 12:57:32 -070023
JP Abgrallbaeccc42013-06-25 09:44:10 -070024#include <sysutils/SocketClient.h>
Lorenzo Colittidedd2712016-03-22 12:36:29 +090025#include <utils/RWLock.h>
JP Abgrallbaeccc42013-06-25 09:44:10 -070026
Lorenzo Colitti13debb82016-03-27 17:46:30 +090027#include "NetdConstants.h"
28
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070029class BandwidthController {
30public:
Lorenzo Colittidedd2712016-03-22 12:36:29 +090031 android::RWLock lock;
32
JP Abgralldb7da582011-09-18 12:57:32 -070033 class TetherStats {
34 public:
Joel Scherpelzbcad6612017-05-30 10:55:11 +090035 TetherStats() = default;
JP Abgrallbaeccc42013-06-25 09:44:10 -070036 TetherStats(std::string intIfn, std::string extIfn,
JP Abgralldb7da582011-09-18 12:57:32 -070037 int64_t rxB, int64_t rxP,
38 int64_t txB, int64_t txP)
JP Abgrallbaeccc42013-06-25 09:44:10 -070039 : intIface(intIfn), extIface(extIfn),
JP Abgralldb7da582011-09-18 12:57:32 -070040 rxBytes(rxB), rxPackets(rxP),
JP Abgrallbaeccc42013-06-25 09:44:10 -070041 txBytes(txB), txPackets(txP) {};
42 /* Internal interface. Same as NatController's notion. */
43 std::string intIface;
44 /* External interface. Same as NatController's notion. */
45 std::string extIface;
Joel Scherpelzbcad6612017-05-30 10:55:11 +090046 int64_t rxBytes = -1;
47 int64_t rxPackets = -1;
48 int64_t txBytes = -1;
49 int64_t txPackets = -1;
JP Abgralldb7da582011-09-18 12:57:32 -070050 /*
51 * Allocates a new string representing this:
JP Abgrallbaeccc42013-06-25 09:44:10 -070052 * intIface extIface rx_bytes rx_packets tx_bytes tx_packets
JP Abgralldb7da582011-09-18 12:57:32 -070053 * The caller is responsible for free()'ing the returned ptr.
54 */
Joel Scherpelzbcad6612017-05-30 10:55:11 +090055 std::string getStatsLine() const;
Lorenzo Colitti7364b752016-07-08 18:24:53 +090056
57 bool addStatsIfMatch(const TetherStats& other) {
58 if (intIface == other.intIface && extIface == other.extIface) {
59 rxBytes += other.rxBytes;
60 rxPackets += other.rxPackets;
61 txBytes += other.txBytes;
62 txPackets += other.txPackets;
63 return true;
64 }
65 return false;
66 }
JP Abgralldb7da582011-09-18 12:57:32 -070067 };
68
JP Abgrallfa6f46d2011-06-17 23:17:28 -070069 BandwidthController();
JP Abgrall0031cea2012-04-17 16:38:23 -070070
Joel Scherpelzbcad6612017-05-30 10:55:11 +090071 int setupIptablesHooks();
JP Abgrall0031cea2012-04-17 16:38:23 -070072
73 int enableBandwidthControl(bool force);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090074 int disableBandwidthControl();
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090075 int enableDataSaver(bool enable);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070076
Joel Scherpelzbcad6612017-05-30 10:55:11 +090077 int setInterfaceSharedQuota(const std::string& iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070078 int getInterfaceSharedQuota(int64_t *bytes);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090079 int removeInterfaceSharedQuota(const std::string& iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070080
Joel Scherpelzbcad6612017-05-30 10:55:11 +090081 int setInterfaceQuota(const std::string& iface, int64_t bytes);
82 int getInterfaceQuota(const std::string& iface, int64_t* bytes);
83 int removeInterfaceQuota(const std::string& iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -070084
JP Abgrallfa6f46d2011-06-17 23:17:28 -070085 int addNaughtyApps(int numUids, char *appUids[]);
86 int removeNaughtyApps(int numUids, char *appUids[]);
JP Abgralle4788732013-07-02 20:28:45 -070087 int addNiceApps(int numUids, char *appUids[]);
88 int removeNiceApps(int numUids, char *appUids[]);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070089
JP Abgrall8a932722011-07-13 19:17:35 -070090 int setGlobalAlert(int64_t bytes);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090091 int removeGlobalAlert();
92 int setGlobalAlertInForwardChain();
93 int removeGlobalAlertInForwardChain();
JP Abgrall8a932722011-07-13 19:17:35 -070094
95 int setSharedAlert(int64_t bytes);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090096 int removeSharedAlert();
JP Abgrall8a932722011-07-13 19:17:35 -070097
Joel Scherpelzbcad6612017-05-30 10:55:11 +090098 int setInterfaceAlert(const std::string& iface, int64_t bytes);
99 int removeInterfaceAlert(const std::string& iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700100
JP Abgralldb7da582011-09-18 12:57:32 -0700101 /*
JP Abgrallbaeccc42013-06-25 09:44:10 -0700102 * For single pair of ifaces, stats should have ifaceIn and ifaceOut initialized.
103 * For all pairs, stats should have ifaceIn=ifaceOut="".
104 * Sends out to the cli the single stat (TetheringStatsReluts) or a list of stats
105 * (TetheringStatsListResult+CommandOkay).
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700106 * Error is to be handled on the outside.
107 * It results in an error if invoked and no tethering counter rules exist.
JP Abgralldb7da582011-09-18 12:57:32 -0700108 */
JP Abgrallbaeccc42013-06-25 09:44:10 -0700109 int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -0700110
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900111 static const char LOCAL_INPUT[];
112 static const char LOCAL_FORWARD[];
113 static const char LOCAL_OUTPUT[];
114 static const char LOCAL_RAW_PREROUTING[];
115 static const char LOCAL_MANGLE_POSTROUTING[];
Jeff Sharkey8e188ed2012-07-12 18:32:03 -0700116
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900117 private:
JP Abgrall8a932722011-07-13 19:17:35 -0700118 class QuotaInfo {
119 public:
120 QuotaInfo(std::string ifn, int64_t q, int64_t a)
121 : ifaceName(ifn), quota(q), alert(a) {};
122 std::string ifaceName;
123 int64_t quota;
124 int64_t alert;
125 };
JP Abgralldb7da582011-09-18 12:57:32 -0700126
JP Abgrall26e0d492011-06-24 19:21:51 -0700127 enum IptIpVer { IptIpV4, IptIpV6 };
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900128 enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend };
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700129 enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd };
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900130 enum IptOp { IptOpInsert, IptOpDelete };
JP Abgrall26e0d492011-06-24 19:21:51 -0700131 enum QuotaType { QuotaUnique, QuotaShared };
132 enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
JP Abgrall1fb02df2012-04-24 23:27:44 -0700133#if LOG_NDEBUG
134 enum IptFailureLog { IptFailShow, IptFailHide };
135#else
136 enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow };
137#endif
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700138
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900139 int manipulateSpecialApps(const std::vector<std::string>& appStrUids, const std::string& chain,
140 IptJumpOp jumpHandling, IptOp appOp);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700141
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900142 int prepCostlyIface(const std::string& ifn, QuotaType quotaType);
143 int cleanupCostlyIface(const std::string& ifn, QuotaType quotaType);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700144
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900145 std::string makeIptablesQuotaCmd(IptFullOp op, const std::string& costName, int64_t quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700146
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900147 int runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes);
148 int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700149
JP Abgrall0dad7c22011-06-24 11:58:14 -0700150 /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900151 static int runIpxtablesCmd(const std::string& cmd, IptJumpOp jumpHandling,
JP Abgrall1fb02df2012-04-24 23:27:44 -0700152 IptFailureLog failureHandling = IptFailShow);
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900153 static int runIptablesCmd(const std::string& cmd, IptJumpOp jumpHandling, IptIpVer iptIpVer,
JP Abgrall1fb02df2012-04-24 23:27:44 -0700154 IptFailureLog failureHandling = IptFailShow);
155
JP Abgrall26e0d492011-06-24 19:21:51 -0700156 // Provides strncpy() + check overflow.
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900157 static int StrncpyAndCheck(char* buffer, const std::string& src, size_t buffSize);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700158
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900159 int updateQuota(const std::string& alertName, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700160
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900161 int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes);
162 int removeCostlyAlert(const std::string& costName, int64_t* alertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700163
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900164 typedef std::vector<TetherStats> TetherStatsList;
165
166 static void addStats(TetherStatsList& statsList, const TetherStats& stats);
167
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700168 /*
JP Abgrallbaeccc42013-06-25 09:44:10 -0700169 * stats should never have only intIface initialized. Other 3 combos are ok.
170 * fp should be a file to the apropriate FORWARD chain of iptables rules.
JP Abgralla2a64f02011-11-11 20:36:16 -0800171 * extraProcessingInfo: contains raw parsed data, and error info.
JP Abgrallbaeccc42013-06-25 09:44:10 -0700172 * This strongly requires that setup of the rules is in a specific order:
173 * in:intIface out:extIface
174 * in:extIface out:intIface
175 * and the rules are grouped in pairs when more that one tethering was setup.
JP Abgralldb7da582011-09-18 12:57:32 -0700176 */
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900177 static int addForwardChainStats(const TetherStats& filter,
178 TetherStatsList& statsList, const std::string& iptOutput,
179 std::string &extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -0700180
JP Abgrall0e540ec2013-08-26 15:13:10 -0700181 /*
182 * Attempt to find the bw_costly_* tables that need flushing,
183 * and flush them.
184 * If doClean then remove the tables also.
185 * Deals with both ip4 and ip6 tables.
186 */
187 void flushExistingCostlyTables(bool doClean);
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900188 static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700189
190 /*
191 * Attempt to flush our tables.
192 * If doClean then remove them also.
193 * Deals with both ip4 and ip6 tables.
194 */
195 void flushCleanTables(bool doClean);
196
JP Abgralldb7da582011-09-18 12:57:32 -0700197 /*------------------*/
198
199 std::list<std::string> sharedQuotaIfaces;
200 int64_t sharedQuotaBytes;
201 int64_t sharedAlertBytes;
202 int64_t globalAlertBytes;
JP Abgrallc6c67342011-10-07 16:28:54 -0700203 /*
204 * This tracks the number of tethers setup.
205 * The FORWARD chain is updated in the following cases:
206 * - The 1st time a globalAlert is setup and there are tethers setup.
207 * - Anytime a globalAlert is removed and there are tethers setup.
208 * - The 1st tether is setup and there is a globalAlert active.
209 * - The last tether is removed and there is a globalAlert active.
210 */
211 int globalAlertTetherCount;
212
JP Abgralldb7da582011-09-18 12:57:32 -0700213 std::list<QuotaInfo> quotaIfaces;
JP Abgralldb7da582011-09-18 12:57:32 -0700214
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900215 // For testing.
216 friend class BandwidthControllerTest;
217 static int (*execFunction)(int, char **, int *, bool, bool);
218 static FILE *(*popenFunction)(const char *, const char *);
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900219 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900220
221private:
222 static const char *opToString(IptOp op);
223 static const char *jumpToString(IptJumpOp jumpHandling);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700224};
225
226#endif