blob: 955ea3849a40dbd783dcf4c03cb6c46fd0e645e1 [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
Lalit Kansara75d8bcd2016-12-06 22:46:44 +053022#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:
35 TetherStats(void)
36 : rxBytes(-1), rxPackets(-1),
37 txBytes(-1), txPackets(-1) {};
JP Abgrallbaeccc42013-06-25 09:44:10 -070038 TetherStats(std::string intIfn, std::string extIfn,
JP Abgralldb7da582011-09-18 12:57:32 -070039 int64_t rxB, int64_t rxP,
40 int64_t txB, int64_t txP)
JP Abgrallbaeccc42013-06-25 09:44:10 -070041 : intIface(intIfn), extIface(extIfn),
JP Abgralldb7da582011-09-18 12:57:32 -070042 rxBytes(rxB), rxPackets(rxP),
JP Abgrallbaeccc42013-06-25 09:44:10 -070043 txBytes(txB), txPackets(txP) {};
44 /* Internal interface. Same as NatController's notion. */
45 std::string intIface;
46 /* External interface. Same as NatController's notion. */
47 std::string extIface;
JP Abgralldb7da582011-09-18 12:57:32 -070048 int64_t rxBytes, rxPackets;
49 int64_t txBytes, txPackets;
50 /*
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 */
JP Abgrallbaeccc42013-06-25 09:44:10 -070055 char *getStatsLine(void) 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
71 int setupIptablesHooks(void);
72
73 int enableBandwidthControl(bool force);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070074 int disableBandwidthControl(void);
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090075 int enableDataSaver(bool enable);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070076
JP Abgrall0dad7c22011-06-24 11:58:14 -070077 int setInterfaceSharedQuota(const char *iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070078 int getInterfaceSharedQuota(int64_t *bytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070079 int removeInterfaceSharedQuota(const char *iface);
80
JP Abgrall0dad7c22011-06-24 11:58:14 -070081 int setInterfaceQuota(const char *iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070082 int getInterfaceQuota(const char *iface, int64_t *bytes);
JP Abgrall0dad7c22011-06-24 11:58:14 -070083 int removeInterfaceQuota(const char *iface);
84
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);
91 int removeGlobalAlert(void);
JP Abgrallc6c67342011-10-07 16:28:54 -070092 int setGlobalAlertInForwardChain(void);
93 int removeGlobalAlertInForwardChain(void);
JP Abgrall8a932722011-07-13 19:17:35 -070094
95 int setSharedAlert(int64_t bytes);
96 int removeSharedAlert(void);
97
98 int setInterfaceAlert(const char *iface, int64_t bytes);
99 int removeInterfaceAlert(const char *iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700100
farenld228c542016-09-01 12:30:35 +0800101 int addRestrictAppsOnData(int numUids, char *appUids[]);
102 int removeRestrictAppsOnData(int numUids, char *appUids[]);
103
104 int addRestrictAppsOnWlan(int numUids, char *appUids[]);
105 int removeRestrictAppsOnWlan(int numUids, char *appUids[]);
106
JP Abgralldb7da582011-09-18 12:57:32 -0700107 /*
JP Abgrallbaeccc42013-06-25 09:44:10 -0700108 * For single pair of ifaces, stats should have ifaceIn and ifaceOut initialized.
109 * For all pairs, stats should have ifaceIn=ifaceOut="".
110 * Sends out to the cli the single stat (TetheringStatsReluts) or a list of stats
111 * (TetheringStatsListResult+CommandOkay).
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700112 * Error is to be handled on the outside.
113 * It results in an error if invoked and no tethering counter rules exist.
JP Abgralldb7da582011-09-18 12:57:32 -0700114 */
JP Abgrallbaeccc42013-06-25 09:44:10 -0700115 int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -0700116
Jeff Sharkey8e188ed2012-07-12 18:32:03 -0700117 static const char* LOCAL_INPUT;
118 static const char* LOCAL_FORWARD;
119 static const char* LOCAL_OUTPUT;
120 static const char* LOCAL_RAW_PREROUTING;
121 static const char* LOCAL_MANGLE_POSTROUTING;
122
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700123protected:
JP Abgrall8a932722011-07-13 19:17:35 -0700124 class QuotaInfo {
125 public:
126 QuotaInfo(std::string ifn, int64_t q, int64_t a)
127 : ifaceName(ifn), quota(q), alert(a) {};
128 std::string ifaceName;
129 int64_t quota;
130 int64_t alert;
131 };
JP Abgralldb7da582011-09-18 12:57:32 -0700132
JP Abgrall26e0d492011-06-24 19:21:51 -0700133 enum IptIpVer { IptIpV4, IptIpV6 };
JP Abgrall109899b2013-02-12 19:20:13 -0800134 enum IptOp { IptOpInsert, IptOpReplace, IptOpDelete, IptOpAppend };
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700135 enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd };
136 enum SpecialAppOp { SpecialAppOpAdd, SpecialAppOpRemove };
farenld228c542016-09-01 12:30:35 +0800137 enum RestrictAppOp { RestrictAppOpAdd, RestrictAppOpRemove};
JP Abgrall26e0d492011-06-24 19:21:51 -0700138 enum QuotaType { QuotaUnique, QuotaShared };
139 enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
JP Abgrall1fb02df2012-04-24 23:27:44 -0700140#if LOG_NDEBUG
141 enum IptFailureLog { IptFailShow, IptFailHide };
142#else
143 enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow };
144#endif
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700145
146 int manipulateSpecialApps(int numUids, char *appStrUids[],
147 const char *chain,
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700148 IptJumpOp jumpHandling, SpecialAppOp appOp);
149 int manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp);
JP Abgralle4788732013-07-02 20:28:45 -0700150 int manipulateNiceApps(int numUids, char *appStrUids[], SpecialAppOp appOp);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700151
farenld228c542016-09-01 12:30:35 +0800152 int manipulateRestrictAppsOnData(int numUids, char* appStrUids[], RestrictAppOp appOp);
153 int manipulateRestrictAppsOnWlan(int numUids, char* appStrUids[], RestrictAppOp appOp);
154 int manipulateRestrictApps(int numUids, char *appStrUids[],
155 const char *chain,
156 std::list<int /*appUid*/> &restrictAppUids,
157 RestrictAppOp appOp);
158
JP Abgrall26e0d492011-06-24 19:21:51 -0700159 int prepCostlyIface(const char *ifn, QuotaType quotaType);
160 int cleanupCostlyIface(const char *ifn, QuotaType quotaType);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700161
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700162 std::string makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain);
JP Abgrall26e0d492011-06-24 19:21:51 -0700163 std::string makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700164
JP Abgrall8a932722011-07-13 19:17:35 -0700165 int runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700166 int runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700167
JP Abgrall0dad7c22011-06-24 11:58:14 -0700168 /* Runs for both ipv4 and ipv6 iptables */
JP Abgrall26e0d492011-06-24 19:21:51 -0700169 int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700170 /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700171 static int runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrall1fb02df2012-04-24 23:27:44 -0700172 IptFailureLog failureHandling = IptFailShow);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700173 static int runIptablesCmd(const char *cmd, IptJumpOp jumpHandling, IptIpVer iptIpVer,
JP Abgrall1fb02df2012-04-24 23:27:44 -0700174 IptFailureLog failureHandling = IptFailShow);
175
JP Abgrall26e0d492011-06-24 19:21:51 -0700176
177 // Provides strncpy() + check overflow.
178 static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700179
JP Abgrall8a932722011-07-13 19:17:35 -0700180 int updateQuota(const char *alertName, int64_t bytes);
181
JP Abgrall8a932722011-07-13 19:17:35 -0700182 int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes);
183 int removeCostlyAlert(const char *costName, int64_t *alertBytes);
184
Lorenzo Colitti7364b752016-07-08 18:24:53 +0900185 typedef std::vector<TetherStats> TetherStatsList;
186
187 static void addStats(TetherStatsList& statsList, const TetherStats& stats);
188
189 static int addForwardChainStats(const TetherStats& filter,
190 TetherStatsList& statsList, FILE *fp,
191 std::string &extraProcessingInfo);
192
193
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700194 /*
JP Abgrallbaeccc42013-06-25 09:44:10 -0700195 * stats should never have only intIface initialized. Other 3 combos are ok.
196 * fp should be a file to the apropriate FORWARD chain of iptables rules.
JP Abgralla2a64f02011-11-11 20:36:16 -0800197 * extraProcessingInfo: contains raw parsed data, and error info.
JP Abgrallbaeccc42013-06-25 09:44:10 -0700198 * This strongly requires that setup of the rules is in a specific order:
199 * in:intIface out:extIface
200 * in:extIface out:intIface
201 * and the rules are grouped in pairs when more that one tethering was setup.
JP Abgralldb7da582011-09-18 12:57:32 -0700202 */
JP Abgrallbaeccc42013-06-25 09:44:10 -0700203 static int parseForwardChainStats(SocketClient *cli, const TetherStats filter, FILE *fp,
JP Abgrall0031cea2012-04-17 16:38:23 -0700204 std::string &extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -0700205
JP Abgrall0e540ec2013-08-26 15:13:10 -0700206 /*
207 * Attempt to find the bw_costly_* tables that need flushing,
208 * and flush them.
209 * If doClean then remove the tables also.
210 * Deals with both ip4 and ip6 tables.
211 */
212 void flushExistingCostlyTables(bool doClean);
213 static void parseAndFlushCostlyTables(FILE *fp, bool doRemove);
214
215 /*
216 * Attempt to flush our tables.
217 * If doClean then remove them also.
218 * Deals with both ip4 and ip6 tables.
219 */
220 void flushCleanTables(bool doClean);
221
JP Abgralldb7da582011-09-18 12:57:32 -0700222 /*------------------*/
223
224 std::list<std::string> sharedQuotaIfaces;
225 int64_t sharedQuotaBytes;
226 int64_t sharedAlertBytes;
227 int64_t globalAlertBytes;
JP Abgrallc6c67342011-10-07 16:28:54 -0700228 /*
229 * This tracks the number of tethers setup.
230 * The FORWARD chain is updated in the following cases:
231 * - The 1st time a globalAlert is setup and there are tethers setup.
232 * - Anytime a globalAlert is removed and there are tethers setup.
233 * - The 1st tether is setup and there is a globalAlert active.
234 * - The last tether is removed and there is a globalAlert active.
235 */
236 int globalAlertTetherCount;
237
JP Abgralldb7da582011-09-18 12:57:32 -0700238 std::list<QuotaInfo> quotaIfaces;
JP Abgralldb7da582011-09-18 12:57:32 -0700239
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900240 // For testing.
241 friend class BandwidthControllerTest;
242 static int (*execFunction)(int, char **, int *, bool, bool);
243 static FILE *(*popenFunction)(const char *, const char *);
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900244 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&);
farenld228c542016-09-01 12:30:35 +0800245
246 std::list<int /*appUid*/> restrictAppUidsOnData;
247 std::list<int /*appUid*/> restrictAppUidsOnWlan;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700248};
249
250#endif