blob: c653e2f84d5590640283de1786c248074e5a0502 [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>
24
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070025class BandwidthController {
26public:
JP Abgralldb7da582011-09-18 12:57:32 -070027 class TetherStats {
28 public:
29 TetherStats(void)
30 : rxBytes(-1), rxPackets(-1),
31 txBytes(-1), txPackets(-1) {};
JP Abgrallbaeccc42013-06-25 09:44:10 -070032 TetherStats(std::string intIfn, std::string extIfn,
JP Abgralldb7da582011-09-18 12:57:32 -070033 int64_t rxB, int64_t rxP,
34 int64_t txB, int64_t txP)
JP Abgrallbaeccc42013-06-25 09:44:10 -070035 : intIface(intIfn), extIface(extIfn),
JP Abgralldb7da582011-09-18 12:57:32 -070036 rxBytes(rxB), rxPackets(rxP),
JP Abgrallbaeccc42013-06-25 09:44:10 -070037 txBytes(txB), txPackets(txP) {};
38 /* Internal interface. Same as NatController's notion. */
39 std::string intIface;
40 /* External interface. Same as NatController's notion. */
41 std::string extIface;
JP Abgralldb7da582011-09-18 12:57:32 -070042 int64_t rxBytes, rxPackets;
43 int64_t txBytes, txPackets;
44 /*
45 * Allocates a new string representing this:
JP Abgrallbaeccc42013-06-25 09:44:10 -070046 * intIface extIface rx_bytes rx_packets tx_bytes tx_packets
JP Abgralldb7da582011-09-18 12:57:32 -070047 * The caller is responsible for free()'ing the returned ptr.
48 */
JP Abgrallbaeccc42013-06-25 09:44:10 -070049 char *getStatsLine(void) const;
JP Abgralldb7da582011-09-18 12:57:32 -070050 };
51
JP Abgrallfa6f46d2011-06-17 23:17:28 -070052 BandwidthController();
JP Abgrall0031cea2012-04-17 16:38:23 -070053
54 int setupIptablesHooks(void);
55
56 int enableBandwidthControl(bool force);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070057 int disableBandwidthControl(void);
58
JP Abgrall0dad7c22011-06-24 11:58:14 -070059 int setInterfaceSharedQuota(const char *iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070060 int getInterfaceSharedQuota(int64_t *bytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070061 int removeInterfaceSharedQuota(const char *iface);
62
JP Abgrall0dad7c22011-06-24 11:58:14 -070063 int setInterfaceQuota(const char *iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070064 int getInterfaceQuota(const char *iface, int64_t *bytes);
JP Abgrall0dad7c22011-06-24 11:58:14 -070065 int removeInterfaceQuota(const char *iface);
66
JP Abgrallfa6f46d2011-06-17 23:17:28 -070067 int addNaughtyApps(int numUids, char *appUids[]);
68 int removeNaughtyApps(int numUids, char *appUids[]);
JP Abgralle4788732013-07-02 20:28:45 -070069 int addNiceApps(int numUids, char *appUids[]);
70 int removeNiceApps(int numUids, char *appUids[]);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070071
JP Abgrall8a932722011-07-13 19:17:35 -070072 int setGlobalAlert(int64_t bytes);
73 int removeGlobalAlert(void);
JP Abgrallc6c67342011-10-07 16:28:54 -070074 int setGlobalAlertInForwardChain(void);
75 int removeGlobalAlertInForwardChain(void);
JP Abgrall8a932722011-07-13 19:17:35 -070076
77 int setSharedAlert(int64_t bytes);
78 int removeSharedAlert(void);
79
80 int setInterfaceAlert(const char *iface, int64_t bytes);
81 int removeInterfaceAlert(const char *iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -070082
JP Abgralldb7da582011-09-18 12:57:32 -070083 /*
JP Abgrallbaeccc42013-06-25 09:44:10 -070084 * For single pair of ifaces, stats should have ifaceIn and ifaceOut initialized.
85 * For all pairs, stats should have ifaceIn=ifaceOut="".
86 * Sends out to the cli the single stat (TetheringStatsReluts) or a list of stats
87 * (TetheringStatsListResult+CommandOkay).
JP Abgrallf3cc83f2013-09-11 20:01:59 -070088 * Error is to be handled on the outside.
89 * It results in an error if invoked and no tethering counter rules exist.
JP Abgralldb7da582011-09-18 12:57:32 -070090 */
JP Abgrallbaeccc42013-06-25 09:44:10 -070091 int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -070092
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070093 static const char* LOCAL_INPUT;
94 static const char* LOCAL_FORWARD;
95 static const char* LOCAL_OUTPUT;
96 static const char* LOCAL_RAW_PREROUTING;
97 static const char* LOCAL_MANGLE_POSTROUTING;
98
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070099protected:
JP Abgrall8a932722011-07-13 19:17:35 -0700100 class QuotaInfo {
101 public:
102 QuotaInfo(std::string ifn, int64_t q, int64_t a)
103 : ifaceName(ifn), quota(q), alert(a) {};
104 std::string ifaceName;
105 int64_t quota;
106 int64_t alert;
107 };
JP Abgralldb7da582011-09-18 12:57:32 -0700108
JP Abgrall26e0d492011-06-24 19:21:51 -0700109 enum IptIpVer { IptIpV4, IptIpV6 };
JP Abgrall109899b2013-02-12 19:20:13 -0800110 enum IptOp { IptOpInsert, IptOpReplace, IptOpDelete, IptOpAppend };
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700111 enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd };
112 enum SpecialAppOp { SpecialAppOpAdd, SpecialAppOpRemove };
JP Abgrall26e0d492011-06-24 19:21:51 -0700113 enum QuotaType { QuotaUnique, QuotaShared };
114 enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
JP Abgrall1fb02df2012-04-24 23:27:44 -0700115#if LOG_NDEBUG
116 enum IptFailureLog { IptFailShow, IptFailHide };
117#else
118 enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow };
119#endif
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700120
121 int manipulateSpecialApps(int numUids, char *appStrUids[],
122 const char *chain,
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700123 IptJumpOp jumpHandling, SpecialAppOp appOp);
124 int manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp);
JP Abgralle4788732013-07-02 20:28:45 -0700125 int manipulateNiceApps(int numUids, char *appStrUids[], SpecialAppOp appOp);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700126
JP Abgrall26e0d492011-06-24 19:21:51 -0700127 int prepCostlyIface(const char *ifn, QuotaType quotaType);
128 int cleanupCostlyIface(const char *ifn, QuotaType quotaType);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700129
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700130 std::string makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain);
JP Abgrall26e0d492011-06-24 19:21:51 -0700131 std::string makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700132
JP Abgrall8a932722011-07-13 19:17:35 -0700133 int runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700134 int runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700135
JP Abgrall0dad7c22011-06-24 11:58:14 -0700136 /* Runs for both ipv4 and ipv6 iptables */
JP Abgrall26e0d492011-06-24 19:21:51 -0700137 int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700138 /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700139 static int runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrall1fb02df2012-04-24 23:27:44 -0700140 IptFailureLog failureHandling = IptFailShow);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700141 static int runIptablesCmd(const char *cmd, IptJumpOp jumpHandling, IptIpVer iptIpVer,
JP Abgrall1fb02df2012-04-24 23:27:44 -0700142 IptFailureLog failureHandling = IptFailShow);
143
JP Abgrall26e0d492011-06-24 19:21:51 -0700144
145 // Provides strncpy() + check overflow.
146 static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700147
JP Abgrall8a932722011-07-13 19:17:35 -0700148 int updateQuota(const char *alertName, int64_t bytes);
149
JP Abgrall8a932722011-07-13 19:17:35 -0700150 int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes);
151 int removeCostlyAlert(const char *costName, int64_t *alertBytes);
152
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700153 /*
JP Abgrallbaeccc42013-06-25 09:44:10 -0700154 * stats should never have only intIface initialized. Other 3 combos are ok.
155 * fp should be a file to the apropriate FORWARD chain of iptables rules.
JP Abgralla2a64f02011-11-11 20:36:16 -0800156 * extraProcessingInfo: contains raw parsed data, and error info.
JP Abgrallbaeccc42013-06-25 09:44:10 -0700157 * This strongly requires that setup of the rules is in a specific order:
158 * in:intIface out:extIface
159 * in:extIface out:intIface
160 * and the rules are grouped in pairs when more that one tethering was setup.
JP Abgralldb7da582011-09-18 12:57:32 -0700161 */
JP Abgrallbaeccc42013-06-25 09:44:10 -0700162 static int parseForwardChainStats(SocketClient *cli, const TetherStats filter, FILE *fp,
JP Abgrall0031cea2012-04-17 16:38:23 -0700163 std::string &extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -0700164
JP Abgrall0e540ec2013-08-26 15:13:10 -0700165 /*
166 * Attempt to find the bw_costly_* tables that need flushing,
167 * and flush them.
168 * If doClean then remove the tables also.
169 * Deals with both ip4 and ip6 tables.
170 */
171 void flushExistingCostlyTables(bool doClean);
172 static void parseAndFlushCostlyTables(FILE *fp, bool doRemove);
173
174 /*
175 * Attempt to flush our tables.
176 * If doClean then remove them also.
177 * Deals with both ip4 and ip6 tables.
178 */
179 void flushCleanTables(bool doClean);
180
JP Abgralldb7da582011-09-18 12:57:32 -0700181 /*------------------*/
182
183 std::list<std::string> sharedQuotaIfaces;
184 int64_t sharedQuotaBytes;
185 int64_t sharedAlertBytes;
186 int64_t globalAlertBytes;
JP Abgrallc6c67342011-10-07 16:28:54 -0700187 /*
188 * This tracks the number of tethers setup.
189 * The FORWARD chain is updated in the following cases:
190 * - The 1st time a globalAlert is setup and there are tethers setup.
191 * - Anytime a globalAlert is removed and there are tethers setup.
192 * - The 1st tether is setup and there is a globalAlert active.
193 * - The last tether is removed and there is a globalAlert active.
194 */
195 int globalAlertTetherCount;
196
JP Abgralldb7da582011-09-18 12:57:32 -0700197 std::list<QuotaInfo> quotaIfaces;
JP Abgralldb7da582011-09-18 12:57:32 -0700198
199private:
JP Abgrall0031cea2012-04-17 16:38:23 -0700200 static const char *IPT_FLUSH_COMMANDS[];
JP Abgralldb7da582011-09-18 12:57:32 -0700201 static const char *IPT_CLEANUP_COMMANDS[];
202 static const char *IPT_SETUP_COMMANDS[];
203 static const char *IPT_BASIC_ACCOUNTING_COMMANDS[];
204
205 /* Alphabetical */
JP Abgrallc6c67342011-10-07 16:28:54 -0700206 static const char ALERT_GLOBAL_NAME[];
JP Abgralldb7da582011-09-18 12:57:32 -0700207 static const int MAX_CMD_ARGS;
208 static const int MAX_CMD_LEN;
209 static const int MAX_IFACENAME_LEN;
210 static const int MAX_IPT_OUTPUT_LINE_LEN;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700211};
212
213#endif