blob: 401609f70ea4a5ac9c822dbf15f4b0da98a470f3 [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 Abgrall4a5f5ca2011-06-15 18:37:39 -070023class BandwidthController {
24public:
JP Abgralldb7da582011-09-18 12:57:32 -070025 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 Abgrallfa6f46d2011-06-17 23:17:28 -070048 BandwidthController();
49 int enableBandwidthControl(void);
50 int disableBandwidthControl(void);
51
JP Abgrall0dad7c22011-06-24 11:58:14 -070052 int setInterfaceSharedQuota(const char *iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070053 int getInterfaceSharedQuota(int64_t *bytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070054 int removeInterfaceSharedQuota(const char *iface);
55
JP Abgrall0dad7c22011-06-24 11:58:14 -070056 int setInterfaceQuota(const char *iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070057 int getInterfaceQuota(const char *iface, int64_t *bytes);
JP Abgrall0dad7c22011-06-24 11:58:14 -070058 int removeInterfaceQuota(const char *iface);
59
JP Abgrallfa6f46d2011-06-17 23:17:28 -070060 int addNaughtyApps(int numUids, char *appUids[]);
61 int removeNaughtyApps(int numUids, char *appUids[]);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070062
JP Abgrall8a932722011-07-13 19:17:35 -070063 int setGlobalAlert(int64_t bytes);
64 int removeGlobalAlert(void);
65
66 int setSharedAlert(int64_t bytes);
67 int removeSharedAlert(void);
68
69 int setInterfaceAlert(const char *iface, int64_t bytes);
70 int removeInterfaceAlert(const char *iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -070071
JP Abgralldb7da582011-09-18 12:57:32 -070072 /*
73 * stats should have ifaceIn and ifaceOut initialized.
74 * Byte counts should be left to the default (-1).
75 */
76 int getTetherStats(TetherStats &stats);
77
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070078protected:
JP Abgrall8a932722011-07-13 19:17:35 -070079 class QuotaInfo {
80 public:
81 QuotaInfo(std::string ifn, int64_t q, int64_t a)
82 : ifaceName(ifn), quota(q), alert(a) {};
83 std::string ifaceName;
84 int64_t quota;
85 int64_t alert;
86 };
JP Abgralldb7da582011-09-18 12:57:32 -070087
JP Abgrall26e0d492011-06-24 19:21:51 -070088 enum IptIpVer { IptIpV4, IptIpV6 };
89 enum IptOp { IptOpInsert, IptOpReplace, IptOpDelete };
90 enum IptRejectOp { IptRejectAdd, IptRejectNoAdd };
91 enum NaughtyAppOp { NaughtyAppOpAdd, NaughtyAppOpRemove };
92 enum QuotaType { QuotaUnique, QuotaShared };
93 enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
JP Abgrall0dad7c22011-06-24 11:58:14 -070094
JP Abgrall26e0d492011-06-24 19:21:51 -070095 int maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070096
JP Abgrall26e0d492011-06-24 19:21:51 -070097 int prepCostlyIface(const char *ifn, QuotaType quotaType);
98 int cleanupCostlyIface(const char *ifn, QuotaType quotaType);
JP Abgrall0dad7c22011-06-24 11:58:14 -070099
100 std::string makeIptablesNaughtyCmd(IptOp op, int uid);
JP Abgrall26e0d492011-06-24 19:21:51 -0700101 std::string makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700102
JP Abgrall8a932722011-07-13 19:17:35 -0700103 int runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes);
104
JP Abgrall0dad7c22011-06-24 11:58:14 -0700105 /* Runs for both ipv4 and ipv6 iptables */
JP Abgrall26e0d492011-06-24 19:21:51 -0700106 int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700107 /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */
JP Abgrall26e0d492011-06-24 19:21:51 -0700108 static int runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling);
109 static int runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, IptIpVer iptIpVer);
110
111 // Provides strncpy() + check overflow.
112 static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700113
JP Abgrall8a932722011-07-13 19:17:35 -0700114 int updateQuota(const char *alertName, int64_t bytes);
115
JP Abgrall8a932722011-07-13 19:17:35 -0700116 int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes);
117 int removeCostlyAlert(const char *costName, int64_t *alertBytes);
118
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700119 /*
JP Abgralldb7da582011-09-18 12:57:32 -0700120 * stats should have ifaceIn and ifaceOut initialized.
121 * fp should be a file to the FORWARD rules of iptables.
122 */
123 static int parseForwardChainStats(TetherStats &stats, FILE *fp);
124
125 /*------------------*/
126
127 std::list<std::string> sharedQuotaIfaces;
128 int64_t sharedQuotaBytes;
129 int64_t sharedAlertBytes;
130 int64_t globalAlertBytes;
131 std::list<QuotaInfo> quotaIfaces;
132 std::list<int /*appUid*/> naughtyAppUids;
133
134private:
135 static const char *IPT_CLEANUP_COMMANDS[];
136 static const char *IPT_SETUP_COMMANDS[];
137 static const char *IPT_BASIC_ACCOUNTING_COMMANDS[];
138
139 /* Alphabetical */
140 static const char ALERT_IPT_TEMPLATE[];
141 static const int ALERT_RULE_POS_IN_COSTLY_CHAIN;
142 static const char IP6TABLES_PATH[];
143 static const char IPTABLES_PATH[];
144 static const int MAX_CMD_ARGS;
145 static const int MAX_CMD_LEN;
146 static const int MAX_IFACENAME_LEN;
147 static const int MAX_IPT_OUTPUT_LINE_LEN;
148
149 /*
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700150 * When false, it will directly use system() instead of logwrap()
151 */
152 static bool useLogwrapCall;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700153};
154
155#endif