blob: 0a51346b80761216da1f14e201c33d064a27d3c1 [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>
JP Abgrallfa6f46d2011-06-17 23:17:28 -070021#include <utility> // for pair
JP Abgralldb7da582011-09-18 12:57:32 -070022
JP Abgrallbaeccc42013-06-25 09:44:10 -070023#include <sysutils/SocketClient.h>
Lorenzo Colittidedd2712016-03-22 12:36:29 +090024#include <utils/RWLock.h>
JP Abgrallbaeccc42013-06-25 09:44:10 -070025
Lorenzo Colitti13debb82016-03-27 17:46:30 +090026#include "NetdConstants.h"
27
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070028class BandwidthController {
29public:
Lorenzo Colittidedd2712016-03-22 12:36:29 +090030 android::RWLock lock;
31
JP Abgralldb7da582011-09-18 12:57:32 -070032 class TetherStats {
33 public:
34 TetherStats(void)
35 : rxBytes(-1), rxPackets(-1),
36 txBytes(-1), txPackets(-1) {};
JP Abgrallbaeccc42013-06-25 09:44:10 -070037 TetherStats(std::string intIfn, std::string extIfn,
JP Abgralldb7da582011-09-18 12:57:32 -070038 int64_t rxB, int64_t rxP,
39 int64_t txB, int64_t txP)
JP Abgrallbaeccc42013-06-25 09:44:10 -070040 : intIface(intIfn), extIface(extIfn),
JP Abgralldb7da582011-09-18 12:57:32 -070041 rxBytes(rxB), rxPackets(rxP),
JP Abgrallbaeccc42013-06-25 09:44:10 -070042 txBytes(txB), txPackets(txP) {};
43 /* Internal interface. Same as NatController's notion. */
44 std::string intIface;
45 /* External interface. Same as NatController's notion. */
46 std::string extIface;
JP Abgralldb7da582011-09-18 12:57:32 -070047 int64_t rxBytes, rxPackets;
48 int64_t txBytes, txPackets;
49 /*
50 * Allocates a new string representing this:
JP Abgrallbaeccc42013-06-25 09:44:10 -070051 * intIface extIface rx_bytes rx_packets tx_bytes tx_packets
JP Abgralldb7da582011-09-18 12:57:32 -070052 * The caller is responsible for free()'ing the returned ptr.
53 */
JP Abgrallbaeccc42013-06-25 09:44:10 -070054 char *getStatsLine(void) const;
Lorenzo Colitti7364b752016-07-08 18:24:53 +090055
56 bool addStatsIfMatch(const TetherStats& other) {
57 if (intIface == other.intIface && extIface == other.extIface) {
58 rxBytes += other.rxBytes;
59 rxPackets += other.rxPackets;
60 txBytes += other.txBytes;
61 txPackets += other.txPackets;
62 return true;
63 }
64 return false;
65 }
JP Abgralldb7da582011-09-18 12:57:32 -070066 };
67
JP Abgrallfa6f46d2011-06-17 23:17:28 -070068 BandwidthController();
JP Abgrall0031cea2012-04-17 16:38:23 -070069
70 int setupIptablesHooks(void);
71
72 int enableBandwidthControl(bool force);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070073 int disableBandwidthControl(void);
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090074 int enableDataSaver(bool enable);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070075
JP Abgrall0dad7c22011-06-24 11:58:14 -070076 int setInterfaceSharedQuota(const char *iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070077 int getInterfaceSharedQuota(int64_t *bytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070078 int removeInterfaceSharedQuota(const char *iface);
79
JP Abgrall0dad7c22011-06-24 11:58:14 -070080 int setInterfaceQuota(const char *iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070081 int getInterfaceQuota(const char *iface, int64_t *bytes);
JP Abgrall0dad7c22011-06-24 11:58:14 -070082 int removeInterfaceQuota(const char *iface);
83
JP Abgrallfa6f46d2011-06-17 23:17:28 -070084 int addNaughtyApps(int numUids, char *appUids[]);
85 int removeNaughtyApps(int numUids, char *appUids[]);
JP Abgralle4788732013-07-02 20:28:45 -070086 int addNiceApps(int numUids, char *appUids[]);
87 int removeNiceApps(int numUids, char *appUids[]);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070088
JP Abgrall8a932722011-07-13 19:17:35 -070089 int setGlobalAlert(int64_t bytes);
90 int removeGlobalAlert(void);
JP Abgrallc6c67342011-10-07 16:28:54 -070091 int setGlobalAlertInForwardChain(void);
92 int removeGlobalAlertInForwardChain(void);
JP Abgrall8a932722011-07-13 19:17:35 -070093
94 int setSharedAlert(int64_t bytes);
95 int removeSharedAlert(void);
96
97 int setInterfaceAlert(const char *iface, int64_t bytes);
98 int removeInterfaceAlert(const char *iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -070099
JP Abgralldb7da582011-09-18 12:57:32 -0700100 /*
JP Abgrallbaeccc42013-06-25 09:44:10 -0700101 * For single pair of ifaces, stats should have ifaceIn and ifaceOut initialized.
102 * For all pairs, stats should have ifaceIn=ifaceOut="".
103 * Sends out to the cli the single stat (TetheringStatsReluts) or a list of stats
104 * (TetheringStatsListResult+CommandOkay).
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700105 * Error is to be handled on the outside.
106 * It results in an error if invoked and no tethering counter rules exist.
JP Abgralldb7da582011-09-18 12:57:32 -0700107 */
JP Abgrallbaeccc42013-06-25 09:44:10 -0700108 int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -0700109
Jeff Sharkey8e188ed2012-07-12 18:32:03 -0700110 static const char* LOCAL_INPUT;
111 static const char* LOCAL_FORWARD;
112 static const char* LOCAL_OUTPUT;
113 static const char* LOCAL_RAW_PREROUTING;
114 static const char* LOCAL_MANGLE_POSTROUTING;
115
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700116protected:
JP Abgrall8a932722011-07-13 19:17:35 -0700117 class QuotaInfo {
118 public:
119 QuotaInfo(std::string ifn, int64_t q, int64_t a)
120 : ifaceName(ifn), quota(q), alert(a) {};
121 std::string ifaceName;
122 int64_t quota;
123 int64_t alert;
124 };
JP Abgralldb7da582011-09-18 12:57:32 -0700125
JP Abgrall26e0d492011-06-24 19:21:51 -0700126 enum IptIpVer { IptIpV4, IptIpV6 };
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900127 enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend };
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700128 enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd };
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900129 enum IptOp { IptOpInsert, IptOpDelete };
JP Abgrall26e0d492011-06-24 19:21:51 -0700130 enum QuotaType { QuotaUnique, QuotaShared };
131 enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
JP Abgrall1fb02df2012-04-24 23:27:44 -0700132#if LOG_NDEBUG
133 enum IptFailureLog { IptFailShow, IptFailHide };
134#else
135 enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow };
136#endif
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700137
138 int manipulateSpecialApps(int numUids, char *appStrUids[],
139 const char *chain,
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900140 IptJumpOp jumpHandling, IptOp appOp);
141 int manipulateNaughtyApps(int numUids, char *appStrUids[], IptOp appOp);
142 int manipulateNiceApps(int numUids, char *appStrUids[], IptOp appOp);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700143
JP Abgrall26e0d492011-06-24 19:21:51 -0700144 int prepCostlyIface(const char *ifn, QuotaType quotaType);
145 int cleanupCostlyIface(const char *ifn, QuotaType quotaType);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700146
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700147 std::string makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain);
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900148 std::string makeIptablesQuotaCmd(IptFullOp op, const char *costName, int64_t quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700149
JP Abgrall8a932722011-07-13 19:17:35 -0700150 int runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700151 int runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700152
JP Abgrall0dad7c22011-06-24 11:58:14 -0700153 /* Runs for both ipv4 and ipv6 iptables */
JP Abgrall26e0d492011-06-24 19:21:51 -0700154 int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700155 /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700156 static int runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrall1fb02df2012-04-24 23:27:44 -0700157 IptFailureLog failureHandling = IptFailShow);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700158 static int runIptablesCmd(const char *cmd, IptJumpOp jumpHandling, IptIpVer iptIpVer,
JP Abgrall1fb02df2012-04-24 23:27:44 -0700159 IptFailureLog failureHandling = IptFailShow);
160
JP Abgrall26e0d492011-06-24 19:21:51 -0700161
162 // Provides strncpy() + check overflow.
163 static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700164
JP Abgrall8a932722011-07-13 19:17:35 -0700165 int updateQuota(const char *alertName, int64_t bytes);
166
JP Abgrall8a932722011-07-13 19:17:35 -0700167 int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes);
168 int removeCostlyAlert(const char *costName, int64_t *alertBytes);
169
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900170 typedef std::vector<TetherStats> TetherStatsList;
171
172 static void addStats(TetherStatsList& statsList, const TetherStats& stats);
173
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700174 /*
JP Abgrallbaeccc42013-06-25 09:44:10 -0700175 * stats should never have only intIface initialized. Other 3 combos are ok.
176 * fp should be a file to the apropriate FORWARD chain of iptables rules.
JP Abgralla2a64f02011-11-11 20:36:16 -0800177 * extraProcessingInfo: contains raw parsed data, and error info.
JP Abgrallbaeccc42013-06-25 09:44:10 -0700178 * This strongly requires that setup of the rules is in a specific order:
179 * in:intIface out:extIface
180 * in:extIface out:intIface
181 * and the rules are grouped in pairs when more that one tethering was setup.
JP Abgralldb7da582011-09-18 12:57:32 -0700182 */
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900183 static int addForwardChainStats(const TetherStats& filter,
184 TetherStatsList& statsList, const std::string& iptOutput,
185 std::string &extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -0700186
JP Abgrall0e540ec2013-08-26 15:13:10 -0700187 /*
188 * Attempt to find the bw_costly_* tables that need flushing,
189 * and flush them.
190 * If doClean then remove the tables also.
191 * Deals with both ip4 and ip6 tables.
192 */
193 void flushExistingCostlyTables(bool doClean);
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900194 static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700195
196 /*
197 * Attempt to flush our tables.
198 * If doClean then remove them also.
199 * Deals with both ip4 and ip6 tables.
200 */
201 void flushCleanTables(bool doClean);
202
JP Abgralldb7da582011-09-18 12:57:32 -0700203 /*------------------*/
204
205 std::list<std::string> sharedQuotaIfaces;
206 int64_t sharedQuotaBytes;
207 int64_t sharedAlertBytes;
208 int64_t globalAlertBytes;
JP Abgrallc6c67342011-10-07 16:28:54 -0700209 /*
210 * This tracks the number of tethers setup.
211 * The FORWARD chain is updated in the following cases:
212 * - The 1st time a globalAlert is setup and there are tethers setup.
213 * - Anytime a globalAlert is removed and there are tethers setup.
214 * - The 1st tether is setup and there is a globalAlert active.
215 * - The last tether is removed and there is a globalAlert active.
216 */
217 int globalAlertTetherCount;
218
JP Abgralldb7da582011-09-18 12:57:32 -0700219 std::list<QuotaInfo> quotaIfaces;
JP Abgralldb7da582011-09-18 12:57:32 -0700220
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900221 // For testing.
222 friend class BandwidthControllerTest;
223 static int (*execFunction)(int, char **, int *, bool, bool);
224 static FILE *(*popenFunction)(const char *, const char *);
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900225 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
Lorenzo Colittid9db08c2017-04-28 11:06:40 +0900226
227private:
228 static const char *opToString(IptOp op);
229 static const char *jumpToString(IptJumpOp jumpHandling);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700230};
231
232#endif