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 | |
Joel Scherpelz | 5a9a090 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 19 | #include <map> |
| 20 | #include <set> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 21 | #include <string> |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 22 | #include <utility> |
| 23 | #include <vector> |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 24 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 25 | #include <sysutils/SocketClient.h> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 26 | #include <utils/RWLock.h> |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 27 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 28 | #include "NetdConstants.h" |
| 29 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 30 | class BandwidthController { |
| 31 | public: |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 32 | android::RWLock lock; |
| 33 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 34 | class TetherStats { |
| 35 | public: |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 36 | TetherStats() = default; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 37 | TetherStats(std::string intIfn, std::string extIfn, |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 38 | int64_t rxB, int64_t rxP, |
| 39 | int64_t txB, int64_t txP) |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 40 | : intIface(intIfn), extIface(extIfn), |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 41 | rxBytes(rxB), rxPackets(rxP), |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 42 | 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; |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 47 | int64_t rxBytes = -1; |
| 48 | int64_t rxPackets = -1; |
| 49 | int64_t txBytes = -1; |
| 50 | int64_t txPackets = -1; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Allocates a new string representing this: |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 53 | * intIface extIface rx_bytes rx_packets tx_bytes tx_packets |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 54 | * The caller is responsible for free()'ing the returned ptr. |
| 55 | */ |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 56 | std::string getStatsLine() const; |
Lorenzo Colitti | 7364b75 | 2016-07-08 18:24:53 +0900 | [diff] [blame] | 57 | |
| 58 | bool addStatsIfMatch(const TetherStats& other) { |
| 59 | if (intIface == other.intIface && extIface == other.extIface) { |
| 60 | rxBytes += other.rxBytes; |
| 61 | rxPackets += other.rxPackets; |
| 62 | txBytes += other.txBytes; |
| 63 | txPackets += other.txPackets; |
| 64 | return true; |
| 65 | } |
| 66 | return false; |
| 67 | } |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 70 | BandwidthController(); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 71 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 72 | int setupIptablesHooks(); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 73 | |
| 74 | int enableBandwidthControl(bool force); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 75 | int disableBandwidthControl(); |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 76 | int enableDataSaver(bool enable); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 77 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 78 | int setInterfaceSharedQuota(const std::string& iface, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 79 | int getInterfaceSharedQuota(int64_t *bytes); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 80 | int removeInterfaceSharedQuota(const std::string& iface); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 81 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 82 | int setInterfaceQuota(const std::string& iface, int64_t bytes); |
| 83 | int getInterfaceQuota(const std::string& iface, int64_t* bytes); |
| 84 | int removeInterfaceQuota(const std::string& iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 85 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 86 | int addNaughtyApps(int numUids, char *appUids[]); |
| 87 | int removeNaughtyApps(int numUids, char *appUids[]); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 88 | int addNiceApps(int numUids, char *appUids[]); |
| 89 | int removeNiceApps(int numUids, char *appUids[]); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 90 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 91 | int setGlobalAlert(int64_t bytes); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 92 | int removeGlobalAlert(); |
| 93 | int setGlobalAlertInForwardChain(); |
| 94 | int removeGlobalAlertInForwardChain(); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 95 | |
| 96 | int setSharedAlert(int64_t bytes); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 97 | int removeSharedAlert(); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 98 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 99 | int setInterfaceAlert(const std::string& iface, int64_t bytes); |
| 100 | int removeInterfaceAlert(const std::string& iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 101 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 102 | /* |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 103 | * For single pair of ifaces, stats should have ifaceIn and ifaceOut initialized. |
| 104 | * For all pairs, stats should have ifaceIn=ifaceOut="". |
| 105 | * Sends out to the cli the single stat (TetheringStatsReluts) or a list of stats |
| 106 | * (TetheringStatsListResult+CommandOkay). |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 107 | * Error is to be handled on the outside. |
| 108 | * It results in an error if invoked and no tethering counter rules exist. |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 109 | */ |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 110 | int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 111 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 112 | static const char LOCAL_INPUT[]; |
| 113 | static const char LOCAL_FORWARD[]; |
| 114 | static const char LOCAL_OUTPUT[]; |
| 115 | static const char LOCAL_RAW_PREROUTING[]; |
| 116 | static const char LOCAL_MANGLE_POSTROUTING[]; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 117 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 118 | private: |
Joel Scherpelz | 5a9a090 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 119 | struct QuotaInfo { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 120 | int64_t quota; |
| 121 | int64_t alert; |
| 122 | }; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 123 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 124 | enum IptIpVer { IptIpV4, IptIpV6 }; |
Lorenzo Colitti | d9db08c | 2017-04-28 11:06:40 +0900 | [diff] [blame] | 125 | enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend }; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 126 | enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd }; |
Lorenzo Colitti | d9db08c | 2017-04-28 11:06:40 +0900 | [diff] [blame] | 127 | enum IptOp { IptOpInsert, IptOpDelete }; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 128 | enum QuotaType { QuotaUnique, QuotaShared }; |
| 129 | enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk }; |
JP Abgrall | 1fb02df | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 130 | #if LOG_NDEBUG |
| 131 | enum IptFailureLog { IptFailShow, IptFailHide }; |
| 132 | #else |
| 133 | enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow }; |
| 134 | #endif |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 135 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 136 | int manipulateSpecialApps(const std::vector<std::string>& appStrUids, const std::string& chain, |
| 137 | IptJumpOp jumpHandling, IptOp appOp); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 138 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 139 | int runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes); |
| 140 | int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 141 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 142 | // Provides strncpy() + check overflow. |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 143 | static int StrncpyAndCheck(char* buffer, const std::string& src, size_t buffSize); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 144 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 145 | int updateQuota(const std::string& alertName, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 146 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 147 | int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes); |
| 148 | int removeCostlyAlert(const std::string& costName, int64_t* alertBytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 149 | |
Lorenzo Colitti | 7364b75 | 2016-07-08 18:24:53 +0900 | [diff] [blame] | 150 | typedef std::vector<TetherStats> TetherStatsList; |
| 151 | |
| 152 | static void addStats(TetherStatsList& statsList, const TetherStats& stats); |
| 153 | |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 154 | /* |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 155 | * stats should never have only intIface initialized. Other 3 combos are ok. |
| 156 | * 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] | 157 | * extraProcessingInfo: contains raw parsed data, and error info. |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 158 | * This strongly requires that setup of the rules is in a specific order: |
| 159 | * in:intIface out:extIface |
| 160 | * in:extIface out:intIface |
| 161 | * 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] | 162 | */ |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 163 | static int addForwardChainStats(const TetherStats& filter, |
| 164 | TetherStatsList& statsList, const std::string& iptOutput, |
| 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); |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 174 | static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove); |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 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 | |
Joel Scherpelz | 5a9a090 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 183 | // For testing. |
| 184 | friend class BandwidthControllerTest; |
| 185 | static int (*execFunction)(int, char **, int *, bool, bool); |
| 186 | static FILE *(*popenFunction)(const char *, const char *); |
| 187 | static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 188 | |
Joel Scherpelz | 5a9a090 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 189 | static const char *opToString(IptOp op); |
| 190 | static const char *jumpToString(IptJumpOp jumpHandling); |
| 191 | |
| 192 | int64_t mSharedQuotaBytes = 0; |
| 193 | int64_t mSharedAlertBytes = 0; |
| 194 | int64_t mGlobalAlertBytes = 0; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 195 | /* |
| 196 | * This tracks the number of tethers setup. |
| 197 | * The FORWARD chain is updated in the following cases: |
| 198 | * - The 1st time a globalAlert is setup and there are tethers setup. |
| 199 | * - Anytime a globalAlert is removed and there are tethers setup. |
| 200 | * - The 1st tether is setup and there is a globalAlert active. |
| 201 | * - The last tether is removed and there is a globalAlert active. |
| 202 | */ |
Joel Scherpelz | 5a9a090 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 203 | int mGlobalAlertTetherCount = 0; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 204 | |
Joel Scherpelz | 5a9a090 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 205 | std::map<std::string, QuotaInfo> mQuotaIfaces; |
| 206 | std::set<std::string> mSharedQuotaIfaces; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | #endif |