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 | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 23 | class BandwidthController { |
| 24 | public: |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 25 | class TetherStats { |
| 26 | public: |
| 27 | TetherStats(void) |
| 28 | : rxBytes(-1), rxPackets(-1), |
| 29 | txBytes(-1), txPackets(-1) {}; |
| 30 | TetherStats(std::string ifnIn, std::string ifnOut, |
| 31 | int64_t rxB, int64_t rxP, |
| 32 | int64_t txB, int64_t txP) |
| 33 | : ifaceIn(ifnIn), ifaceOut(ifnOut), |
| 34 | rxBytes(rxB), rxPackets(rxP), |
| 35 | txBytes(txB), txPackets(txP) {}; |
| 36 | std::string ifaceIn; |
| 37 | std::string ifaceOut; |
| 38 | int64_t rxBytes, rxPackets; |
| 39 | int64_t txBytes, txPackets; |
| 40 | /* |
| 41 | * Allocates a new string representing this: |
| 42 | * ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets |
| 43 | * The caller is responsible for free()'ing the returned ptr. |
| 44 | */ |
| 45 | char *getStatsLine(void); |
| 46 | }; |
| 47 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 48 | BandwidthController(); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 49 | |
| 50 | int setupIptablesHooks(void); |
| 51 | |
| 52 | int enableBandwidthControl(bool force); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 53 | int disableBandwidthControl(void); |
| 54 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 55 | int setInterfaceSharedQuota(const char *iface, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 56 | int getInterfaceSharedQuota(int64_t *bytes); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 57 | int removeInterfaceSharedQuota(const char *iface); |
| 58 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 59 | int setInterfaceQuota(const char *iface, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 60 | int getInterfaceQuota(const char *iface, int64_t *bytes); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 61 | int removeInterfaceQuota(const char *iface); |
| 62 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 63 | int addNaughtyApps(int numUids, char *appUids[]); |
| 64 | int removeNaughtyApps(int numUids, char *appUids[]); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 65 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 66 | int setGlobalAlert(int64_t bytes); |
| 67 | int removeGlobalAlert(void); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 68 | int setGlobalAlertInForwardChain(void); |
| 69 | int removeGlobalAlertInForwardChain(void); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 70 | |
| 71 | int setSharedAlert(int64_t bytes); |
| 72 | int removeSharedAlert(void); |
| 73 | |
| 74 | int setInterfaceAlert(const char *iface, int64_t bytes); |
| 75 | int removeInterfaceAlert(const char *iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 76 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 77 | /* |
| 78 | * stats should have ifaceIn and ifaceOut initialized. |
| 79 | * Byte counts should be left to the default (-1). |
| 80 | */ |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 81 | int getTetherStats(TetherStats &stats, std::string &extraProcessingInfo); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 82 | |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 83 | static const char* LOCAL_INPUT; |
| 84 | static const char* LOCAL_FORWARD; |
| 85 | static const char* LOCAL_OUTPUT; |
| 86 | static const char* LOCAL_RAW_PREROUTING; |
| 87 | static const char* LOCAL_MANGLE_POSTROUTING; |
| 88 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 89 | protected: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 90 | class QuotaInfo { |
| 91 | public: |
| 92 | QuotaInfo(std::string ifn, int64_t q, int64_t a) |
| 93 | : ifaceName(ifn), quota(q), alert(a) {}; |
| 94 | std::string ifaceName; |
| 95 | int64_t quota; |
| 96 | int64_t alert; |
| 97 | }; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 98 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 99 | enum IptIpVer { IptIpV4, IptIpV6 }; |
| 100 | enum IptOp { IptOpInsert, IptOpReplace, IptOpDelete }; |
| 101 | enum IptRejectOp { IptRejectAdd, IptRejectNoAdd }; |
| 102 | enum NaughtyAppOp { NaughtyAppOpAdd, NaughtyAppOpRemove }; |
| 103 | enum QuotaType { QuotaUnique, QuotaShared }; |
| 104 | enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk }; |
JP Abgrall | 1fb02df | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 105 | #if LOG_NDEBUG |
| 106 | enum IptFailureLog { IptFailShow, IptFailHide }; |
| 107 | #else |
| 108 | enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow }; |
| 109 | #endif |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 110 | int maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 111 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 112 | int prepCostlyIface(const char *ifn, QuotaType quotaType); |
| 113 | int cleanupCostlyIface(const char *ifn, QuotaType quotaType); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 114 | |
| 115 | std::string makeIptablesNaughtyCmd(IptOp op, int uid); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 116 | std::string makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 117 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 118 | int runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 119 | int runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 120 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 121 | /* Runs for both ipv4 and ipv6 iptables */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 122 | int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 123 | /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */ |
JP Abgrall | 1fb02df | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 124 | static int runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling, |
| 125 | IptFailureLog failureHandling = IptFailShow); |
| 126 | static int runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, IptIpVer iptIpVer, |
| 127 | IptFailureLog failureHandling = IptFailShow); |
| 128 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 129 | |
| 130 | // Provides strncpy() + check overflow. |
| 131 | static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 132 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 133 | int updateQuota(const char *alertName, int64_t bytes); |
| 134 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 135 | int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes); |
| 136 | int removeCostlyAlert(const char *costName, int64_t *alertBytes); |
| 137 | |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 138 | /* |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 139 | * stats should have ifaceIn and ifaceOut initialized. |
| 140 | * fp should be a file to the FORWARD rules of iptables. |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 141 | * extraProcessingInfo: contains raw parsed data, and error info. |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 142 | */ |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 143 | static int parseForwardChainStats(TetherStats &stats, FILE *fp, |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 144 | std::string &extraProcessingInfo); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 145 | |
| 146 | /*------------------*/ |
| 147 | |
| 148 | std::list<std::string> sharedQuotaIfaces; |
| 149 | int64_t sharedQuotaBytes; |
| 150 | int64_t sharedAlertBytes; |
| 151 | int64_t globalAlertBytes; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 152 | /* |
| 153 | * This tracks the number of tethers setup. |
| 154 | * The FORWARD chain is updated in the following cases: |
| 155 | * - The 1st time a globalAlert is setup and there are tethers setup. |
| 156 | * - Anytime a globalAlert is removed and there are tethers setup. |
| 157 | * - The 1st tether is setup and there is a globalAlert active. |
| 158 | * - The last tether is removed and there is a globalAlert active. |
| 159 | */ |
| 160 | int globalAlertTetherCount; |
| 161 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 162 | std::list<QuotaInfo> quotaIfaces; |
| 163 | std::list<int /*appUid*/> naughtyAppUids; |
| 164 | |
| 165 | private: |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 166 | static const char *IPT_FLUSH_COMMANDS[]; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 167 | static const char *IPT_CLEANUP_COMMANDS[]; |
| 168 | static const char *IPT_SETUP_COMMANDS[]; |
| 169 | static const char *IPT_BASIC_ACCOUNTING_COMMANDS[]; |
| 170 | |
| 171 | /* Alphabetical */ |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 172 | static const int ALERT_RULE_POS_IN_COSTLY_CHAIN; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 173 | static const char ALERT_GLOBAL_NAME[]; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 174 | static const int MAX_CMD_ARGS; |
| 175 | static const int MAX_CMD_LEN; |
| 176 | static const int MAX_IFACENAME_LEN; |
| 177 | static const int MAX_IPT_OUTPUT_LINE_LEN; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | #endif |