blob: 13805af6d5ee303a5bcb9b82f8625690edfe9d16 [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 Abgrall4a5f5ca2011-06-15 18:37:39 -070069
JP Abgrall8a932722011-07-13 19:17:35 -070070 int setGlobalAlert(int64_t bytes);
71 int removeGlobalAlert(void);
JP Abgrallc6c67342011-10-07 16:28:54 -070072 int setGlobalAlertInForwardChain(void);
73 int removeGlobalAlertInForwardChain(void);
JP Abgrall8a932722011-07-13 19:17:35 -070074
75 int setSharedAlert(int64_t bytes);
76 int removeSharedAlert(void);
77
78 int setInterfaceAlert(const char *iface, int64_t bytes);
79 int removeInterfaceAlert(const char *iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -070080
JP Abgralldb7da582011-09-18 12:57:32 -070081 /*
JP Abgrallbaeccc42013-06-25 09:44:10 -070082 * For single pair of ifaces, stats should have ifaceIn and ifaceOut initialized.
83 * For all pairs, stats should have ifaceIn=ifaceOut="".
84 * Sends out to the cli the single stat (TetheringStatsReluts) or a list of stats
85 * (TetheringStatsListResult+CommandOkay).
86 * Error is to be handled on the outside
JP Abgralldb7da582011-09-18 12:57:32 -070087 */
JP Abgrallbaeccc42013-06-25 09:44:10 -070088 int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -070089
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070090 static const char* LOCAL_INPUT;
91 static const char* LOCAL_FORWARD;
92 static const char* LOCAL_OUTPUT;
93 static const char* LOCAL_RAW_PREROUTING;
94 static const char* LOCAL_MANGLE_POSTROUTING;
95
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070096protected:
JP Abgrall8a932722011-07-13 19:17:35 -070097 class QuotaInfo {
98 public:
99 QuotaInfo(std::string ifn, int64_t q, int64_t a)
100 : ifaceName(ifn), quota(q), alert(a) {};
101 std::string ifaceName;
102 int64_t quota;
103 int64_t alert;
104 };
JP Abgralldb7da582011-09-18 12:57:32 -0700105
JP Abgrall26e0d492011-06-24 19:21:51 -0700106 enum IptIpVer { IptIpV4, IptIpV6 };
JP Abgrall109899b2013-02-12 19:20:13 -0800107 enum IptOp { IptOpInsert, IptOpReplace, IptOpDelete, IptOpAppend };
JP Abgrall26e0d492011-06-24 19:21:51 -0700108 enum IptRejectOp { IptRejectAdd, IptRejectNoAdd };
109 enum NaughtyAppOp { NaughtyAppOpAdd, NaughtyAppOpRemove };
110 enum QuotaType { QuotaUnique, QuotaShared };
111 enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
JP Abgrall1fb02df2012-04-24 23:27:44 -0700112#if LOG_NDEBUG
113 enum IptFailureLog { IptFailShow, IptFailHide };
114#else
115 enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow };
116#endif
JP Abgrall26e0d492011-06-24 19:21:51 -0700117 int maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700118
JP Abgrall26e0d492011-06-24 19:21:51 -0700119 int prepCostlyIface(const char *ifn, QuotaType quotaType);
120 int cleanupCostlyIface(const char *ifn, QuotaType quotaType);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700121
122 std::string makeIptablesNaughtyCmd(IptOp op, int uid);
JP Abgrall26e0d492011-06-24 19:21:51 -0700123 std::string makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700124
JP Abgrall8a932722011-07-13 19:17:35 -0700125 int runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700126 int runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700127
JP Abgrall0dad7c22011-06-24 11:58:14 -0700128 /* Runs for both ipv4 and ipv6 iptables */
JP Abgrall26e0d492011-06-24 19:21:51 -0700129 int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700130 /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */
JP Abgrall1fb02df2012-04-24 23:27:44 -0700131 static int runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling,
132 IptFailureLog failureHandling = IptFailShow);
133 static int runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, IptIpVer iptIpVer,
134 IptFailureLog failureHandling = IptFailShow);
135
JP Abgrall26e0d492011-06-24 19:21:51 -0700136
137 // Provides strncpy() + check overflow.
138 static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700139
JP Abgrall8a932722011-07-13 19:17:35 -0700140 int updateQuota(const char *alertName, int64_t bytes);
141
JP Abgrall8a932722011-07-13 19:17:35 -0700142 int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes);
143 int removeCostlyAlert(const char *costName, int64_t *alertBytes);
144
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700145 /*
JP Abgrallbaeccc42013-06-25 09:44:10 -0700146 * stats should never have only intIface initialized. Other 3 combos are ok.
147 * fp should be a file to the apropriate FORWARD chain of iptables rules.
JP Abgralla2a64f02011-11-11 20:36:16 -0800148 * extraProcessingInfo: contains raw parsed data, and error info.
JP Abgrallbaeccc42013-06-25 09:44:10 -0700149 * This strongly requires that setup of the rules is in a specific order:
150 * in:intIface out:extIface
151 * in:extIface out:intIface
152 * and the rules are grouped in pairs when more that one tethering was setup.
JP Abgralldb7da582011-09-18 12:57:32 -0700153 */
JP Abgrallbaeccc42013-06-25 09:44:10 -0700154 static int parseForwardChainStats(SocketClient *cli, const TetherStats filter, FILE *fp,
JP Abgrall0031cea2012-04-17 16:38:23 -0700155 std::string &extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -0700156
157 /*------------------*/
158
159 std::list<std::string> sharedQuotaIfaces;
160 int64_t sharedQuotaBytes;
161 int64_t sharedAlertBytes;
162 int64_t globalAlertBytes;
JP Abgrallc6c67342011-10-07 16:28:54 -0700163 /*
164 * This tracks the number of tethers setup.
165 * The FORWARD chain is updated in the following cases:
166 * - The 1st time a globalAlert is setup and there are tethers setup.
167 * - Anytime a globalAlert is removed and there are tethers setup.
168 * - The 1st tether is setup and there is a globalAlert active.
169 * - The last tether is removed and there is a globalAlert active.
170 */
171 int globalAlertTetherCount;
172
JP Abgralldb7da582011-09-18 12:57:32 -0700173 std::list<QuotaInfo> quotaIfaces;
174 std::list<int /*appUid*/> naughtyAppUids;
175
176private:
JP Abgrall0031cea2012-04-17 16:38:23 -0700177 static const char *IPT_FLUSH_COMMANDS[];
JP Abgralldb7da582011-09-18 12:57:32 -0700178 static const char *IPT_CLEANUP_COMMANDS[];
179 static const char *IPT_SETUP_COMMANDS[];
180 static const char *IPT_BASIC_ACCOUNTING_COMMANDS[];
181
182 /* Alphabetical */
JP Abgrallc6c67342011-10-07 16:28:54 -0700183 static const char ALERT_GLOBAL_NAME[];
JP Abgralldb7da582011-09-18 12:57:32 -0700184 static const int MAX_CMD_ARGS;
185 static const int MAX_CMD_LEN;
186 static const int MAX_IFACENAME_LEN;
187 static const int MAX_IPT_OUTPUT_LINE_LEN;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700188};
189
190#endif