JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 1 | /* |
| 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 Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 21 | #include <utility> // for pair |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 22 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 23 | #include <sysutils/SocketClient.h> |
| 24 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 25 | class BandwidthController { |
| 26 | public: |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 27 | class TetherStats { |
| 28 | public: |
| 29 | TetherStats(void) |
| 30 | : rxBytes(-1), rxPackets(-1), |
| 31 | txBytes(-1), txPackets(-1) {}; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 32 | TetherStats(std::string intIfn, std::string extIfn, |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 33 | int64_t rxB, int64_t rxP, |
| 34 | int64_t txB, int64_t txP) |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 35 | : intIface(intIfn), extIface(extIfn), |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 36 | rxBytes(rxB), rxPackets(rxP), |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 37 | 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 Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 42 | int64_t rxBytes, rxPackets; |
| 43 | int64_t txBytes, txPackets; |
| 44 | /* |
| 45 | * Allocates a new string representing this: |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 46 | * intIface extIface rx_bytes rx_packets tx_bytes tx_packets |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 47 | * The caller is responsible for free()'ing the returned ptr. |
| 48 | */ |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 49 | char *getStatsLine(void) const; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 52 | BandwidthController(); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 53 | |
| 54 | int setupIptablesHooks(void); |
| 55 | |
| 56 | int enableBandwidthControl(bool force); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 57 | int disableBandwidthControl(void); |
| 58 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 59 | int setInterfaceSharedQuota(const char *iface, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 60 | int getInterfaceSharedQuota(int64_t *bytes); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 61 | int removeInterfaceSharedQuota(const char *iface); |
| 62 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 63 | int setInterfaceQuota(const char *iface, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 64 | int getInterfaceQuota(const char *iface, int64_t *bytes); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 65 | int removeInterfaceQuota(const char *iface); |
| 66 | |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 67 | int enableHappyBox(void); |
| 68 | int disableHappyBox(void); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 69 | int addNaughtyApps(int numUids, char *appUids[]); |
| 70 | int removeNaughtyApps(int numUids, char *appUids[]); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 71 | int addNiceApps(int numUids, char *appUids[]); |
| 72 | int removeNiceApps(int numUids, char *appUids[]); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 73 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 74 | int setGlobalAlert(int64_t bytes); |
| 75 | int removeGlobalAlert(void); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 76 | int setGlobalAlertInForwardChain(void); |
| 77 | int removeGlobalAlertInForwardChain(void); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 78 | |
| 79 | int setSharedAlert(int64_t bytes); |
| 80 | int removeSharedAlert(void); |
| 81 | |
| 82 | int setInterfaceAlert(const char *iface, int64_t bytes); |
| 83 | int removeInterfaceAlert(const char *iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 84 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 85 | /* |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 86 | * For single pair of ifaces, stats should have ifaceIn and ifaceOut initialized. |
| 87 | * For all pairs, stats should have ifaceIn=ifaceOut="". |
| 88 | * Sends out to the cli the single stat (TetheringStatsReluts) or a list of stats |
| 89 | * (TetheringStatsListResult+CommandOkay). |
| 90 | * Error is to be handled on the outside |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 91 | */ |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 92 | int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 93 | |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 94 | static const char* LOCAL_INPUT; |
| 95 | static const char* LOCAL_FORWARD; |
| 96 | static const char* LOCAL_OUTPUT; |
| 97 | static const char* LOCAL_RAW_PREROUTING; |
| 98 | static const char* LOCAL_MANGLE_POSTROUTING; |
| 99 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 100 | protected: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 101 | class QuotaInfo { |
| 102 | public: |
| 103 | QuotaInfo(std::string ifn, int64_t q, int64_t a) |
| 104 | : ifaceName(ifn), quota(q), alert(a) {}; |
| 105 | std::string ifaceName; |
| 106 | int64_t quota; |
| 107 | int64_t alert; |
| 108 | }; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 109 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 110 | enum IptIpVer { IptIpV4, IptIpV6 }; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 111 | enum IptOp { IptOpInsert, IptOpReplace, IptOpDelete, IptOpAppend }; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 112 | enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd }; |
| 113 | enum SpecialAppOp { SpecialAppOpAdd, SpecialAppOpRemove }; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 114 | enum QuotaType { QuotaUnique, QuotaShared }; |
| 115 | enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk }; |
JP Abgrall | 1fb02df | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 116 | #if LOG_NDEBUG |
| 117 | enum IptFailureLog { IptFailShow, IptFailHide }; |
| 118 | #else |
| 119 | enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow }; |
| 120 | #endif |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 121 | |
| 122 | int manipulateSpecialApps(int numUids, char *appStrUids[], |
| 123 | const char *chain, |
| 124 | std::list<int /*appUid*/> &specialAppUids, |
| 125 | IptJumpOp jumpHandling, SpecialAppOp appOp); |
| 126 | int manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 127 | int manipulateNiceApps(int numUids, char *appStrUids[], SpecialAppOp appOp); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 128 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 129 | int prepCostlyIface(const char *ifn, QuotaType quotaType); |
| 130 | int cleanupCostlyIface(const char *ifn, QuotaType quotaType); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 131 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 132 | std::string makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 133 | std::string makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 134 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 135 | int runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 136 | int runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 137 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 138 | /* Runs for both ipv4 and ipv6 iptables */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 139 | int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 140 | /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */ |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 141 | static int runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling, |
JP Abgrall | 1fb02df | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 142 | IptFailureLog failureHandling = IptFailShow); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 143 | static int runIptablesCmd(const char *cmd, IptJumpOp jumpHandling, IptIpVer iptIpVer, |
JP Abgrall | 1fb02df | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 144 | IptFailureLog failureHandling = IptFailShow); |
| 145 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 146 | |
| 147 | // Provides strncpy() + check overflow. |
| 148 | static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 149 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 150 | int updateQuota(const char *alertName, int64_t bytes); |
| 151 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 152 | int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes); |
| 153 | int removeCostlyAlert(const char *costName, int64_t *alertBytes); |
| 154 | |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 155 | /* |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 156 | * stats should never have only intIface initialized. Other 3 combos are ok. |
| 157 | * fp should be a file to the apropriate FORWARD chain of iptables rules. |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 158 | * extraProcessingInfo: contains raw parsed data, and error info. |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 159 | * This strongly requires that setup of the rules is in a specific order: |
| 160 | * in:intIface out:extIface |
| 161 | * in:extIface out:intIface |
| 162 | * and the rules are grouped in pairs when more that one tethering was setup. |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 163 | */ |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 164 | static int parseForwardChainStats(SocketClient *cli, const TetherStats filter, FILE *fp, |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 165 | std::string &extraProcessingInfo); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 166 | |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame^] | 167 | /* |
| 168 | * Attempt to find the bw_costly_* tables that need flushing, |
| 169 | * and flush them. |
| 170 | * If doClean then remove the tables also. |
| 171 | * Deals with both ip4 and ip6 tables. |
| 172 | */ |
| 173 | void flushExistingCostlyTables(bool doClean); |
| 174 | static void parseAndFlushCostlyTables(FILE *fp, bool doRemove); |
| 175 | |
| 176 | /* |
| 177 | * Attempt to flush our tables. |
| 178 | * If doClean then remove them also. |
| 179 | * Deals with both ip4 and ip6 tables. |
| 180 | */ |
| 181 | void flushCleanTables(bool doClean); |
| 182 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 183 | /*------------------*/ |
| 184 | |
| 185 | std::list<std::string> sharedQuotaIfaces; |
| 186 | int64_t sharedQuotaBytes; |
| 187 | int64_t sharedAlertBytes; |
| 188 | int64_t globalAlertBytes; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 189 | /* |
| 190 | * This tracks the number of tethers setup. |
| 191 | * The FORWARD chain is updated in the following cases: |
| 192 | * - The 1st time a globalAlert is setup and there are tethers setup. |
| 193 | * - Anytime a globalAlert is removed and there are tethers setup. |
| 194 | * - The 1st tether is setup and there is a globalAlert active. |
| 195 | * - The last tether is removed and there is a globalAlert active. |
| 196 | */ |
| 197 | int globalAlertTetherCount; |
| 198 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 199 | std::list<QuotaInfo> quotaIfaces; |
| 200 | std::list<int /*appUid*/> naughtyAppUids; |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 201 | std::list<int /*appUid*/> niceAppUids; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 202 | |
| 203 | private: |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 204 | static const char *IPT_FLUSH_COMMANDS[]; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 205 | static const char *IPT_CLEANUP_COMMANDS[]; |
| 206 | static const char *IPT_SETUP_COMMANDS[]; |
| 207 | static const char *IPT_BASIC_ACCOUNTING_COMMANDS[]; |
| 208 | |
| 209 | /* Alphabetical */ |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 210 | static const char ALERT_GLOBAL_NAME[]; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 211 | static const int MAX_CMD_ARGS; |
| 212 | static const int MAX_CMD_LEN; |
| 213 | static const int MAX_IFACENAME_LEN; |
| 214 | static const int MAX_IPT_OUTPUT_LINE_LEN; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | #endif |