blob: 0b6bcbe3f401cb886086a22fdf9f44eecdfe710f [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
JP Abgralle4788732013-07-02 20:28:45 -070017// #define LOG_NDEBUG 0
JP Abgralldb7da582011-09-18 12:57:32 -070018
19/*
20 * The CommandListener, FrameworkListener don't allow for
21 * multiple calls in parallel to reach the BandwidthController.
22 * If they ever were to allow it, then netd/ would need some tweaking.
23 */
24
JP Abgrall8a932722011-07-13 19:17:35 -070025#include <errno.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070026#include <fcntl.h>
JP Abgralldb7da582011-09-18 12:57:32 -070027#include <stdio.h>
JP Abgrall8a932722011-07-13 19:17:35 -070028#include <stdlib.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070029#include <string.h>
30
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35
36#include <linux/netlink.h>
37#include <linux/rtnetlink.h>
38#include <linux/pkt_sched.h>
39
40#define LOG_TAG "BandwidthController"
41#include <cutils/log.h>
42#include <cutils/properties.h>
Rom Lemarchand14150212013-01-24 10:01:04 -080043#include <logwrap/logwrap.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070044
JP Abgrall0031cea2012-04-17 16:38:23 -070045#include "NetdConstants.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070046#include "BandwidthController.h"
JP Abgrallbaeccc42013-06-25 09:44:10 -070047#include "NatController.h" /* For LOCAL_TETHER_COUNTERS_CHAIN */
48#include "ResponseCode.h"
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070049
JP Abgralldb7da582011-09-18 12:57:32 -070050/* Alphabetical */
JP Abgrall92009c82013-02-06 18:01:24 -080051#define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %lld --name %s"
JP Abgrallc6c67342011-10-07 16:28:54 -070052const char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070053const char* BandwidthController::LOCAL_INPUT = "bw_INPUT";
54const char* BandwidthController::LOCAL_FORWARD = "bw_FORWARD";
55const char* BandwidthController::LOCAL_OUTPUT = "bw_OUTPUT";
56const char* BandwidthController::LOCAL_RAW_PREROUTING = "bw_raw_PREROUTING";
57const char* BandwidthController::LOCAL_MANGLE_POSTROUTING = "bw_mangle_POSTROUTING";
JP Abgralldb7da582011-09-18 12:57:32 -070058const int BandwidthController::MAX_CMD_ARGS = 32;
59const int BandwidthController::MAX_CMD_LEN = 1024;
60const int BandwidthController::MAX_IFACENAME_LEN = 64;
61const int BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256;
62
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070063/**
64 * Some comments about the rules:
65 * * Ordering
66 * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
JP Abgrall29e8de22012-05-03 12:52:15 -070067 * E.g. "-I bw_INPUT -i rmnet0 --jump costly"
JP Abgrall7e51cde2013-07-03 13:33:05 -070068 * - quota'd rules in the costly chain should be before bw_penalty_box lookups.
69 * - bw_happy_box rejects everything by default.
JP Abgrall29e8de22012-05-03 12:52:15 -070070 * - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070071 *
72 * * global quota vs per interface quota
73 * - global quota for all costly interfaces uses a single costly chain:
74 * . initial rules
JP Abgrall7e51cde2013-07-03 13:33:05 -070075 * iptables -N bw_costly_shared
76 * iptables -I bw_INPUT -i iface0 --jump bw_costly_shared
77 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_shared
78 * iptables -I bw_costly_shared -m quota \! --quota 500000 \
JP Abgrallbfa74662011-06-29 19:23:04 -070079 * --jump REJECT --reject-with icmp-net-prohibited
JP Abgrall7e51cde2013-07-03 13:33:05 -070080 * iptables -A bw_costly_shared --jump bw_penalty_box
JP Abgralle4788732013-07-02 20:28:45 -070081 * If the happy box is enabled,
JP Abgrall7e51cde2013-07-03 13:33:05 -070082 * iptables -A bw_penalty_box --jump bw_happy_box
JP Abgrall8a932722011-07-13 19:17:35 -070083 *
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070084 * . adding a new iface to this, E.g.:
JP Abgrall7e51cde2013-07-03 13:33:05 -070085 * iptables -I bw_INPUT -i iface1 --jump bw_costly_shared
86 * iptables -I bw_OUTPUT -o iface1 --jump bw_costly_shared
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070087 *
88 * - quota per interface. This is achieve by having "costly" chains per quota.
89 * E.g. adding a new costly interface iface0 with its own quota:
JP Abgrall7e51cde2013-07-03 13:33:05 -070090 * iptables -N bw_costly_iface0
91 * iptables -I bw_INPUT -i iface0 --jump bw_costly_iface0
92 * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_iface0
93 * iptables -A bw_costly_iface0 -m quota \! --quota 500000 \
JP Abgralle4788732013-07-02 20:28:45 -070094 * --jump REJECT --reject-with icmp-port-unreachable
JP Abgrall7e51cde2013-07-03 13:33:05 -070095 * iptables -A bw_costly_iface0 --jump bw_penalty_box
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070096 *
JP Abgrall7e51cde2013-07-03 13:33:05 -070097 * * bw_penalty_box handling:
98 * - only one bw_penalty_box for all interfaces
99 * E.g Adding an app, it has to preserve the appened bw_happy_box, so "-I":
100 * iptables -I bw_penalty_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700101 * --jump REJECT --reject-with icmp-port-unreachable
102 *
JP Abgrall7e51cde2013-07-03 13:33:05 -0700103 * * bw_happy_box handling:
104 * - The bw_happy_box goes at the end of the penalty box.
JP Abgralle4788732013-07-02 20:28:45 -0700105 * E.g Adding a happy app,
JP Abgrall7e51cde2013-07-03 13:33:05 -0700106 * iptables -I bw_happy_box -m owner --uid-owner app_3 \
JP Abgralle4788732013-07-02 20:28:45 -0700107 * --jump RETURN
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700108 */
JP Abgrall0031cea2012-04-17 16:38:23 -0700109const char *BandwidthController::IPT_FLUSH_COMMANDS[] = {
110 /*
111 * Cleanup rules.
JP Abgrall7e51cde2013-07-03 13:33:05 -0700112 * Should normally include bw_costly_<iface>, but we rely on the way they are setup
JP Abgrall0031cea2012-04-17 16:38:23 -0700113 * to allow coexistance.
JP Abgrall39f8f242011-06-29 19:21:58 -0700114 */
JP Abgrall0031cea2012-04-17 16:38:23 -0700115 "-F bw_INPUT",
116 "-F bw_OUTPUT",
117 "-F bw_FORWARD",
JP Abgrall7e51cde2013-07-03 13:33:05 -0700118 "-F bw_happy_box",
119 "-F bw_penalty_box",
120 "-F bw_costly_shared",
JP Abgrallf66d6e92012-04-27 00:22:57 -0700121
JP Abgralle4788732013-07-02 20:28:45 -0700122 /* Just a couple that are the most common. */
JP Abgrall7e51cde2013-07-03 13:33:05 -0700123 "-F bw_costly_rmnet0",
124 "-F bw_costly_wlan0",
JP Abgralle4788732013-07-02 20:28:45 -0700125
JP Abgrallf66d6e92012-04-27 00:22:57 -0700126 "-t raw -F bw_raw_PREROUTING",
127 "-t mangle -F bw_mangle_POSTROUTING",
JP Abgrall0031cea2012-04-17 16:38:23 -0700128};
129
130/* The cleanup commands assume flushing has been done. */
131const char *BandwidthController::IPT_CLEANUP_COMMANDS[] = {
JP Abgrall7e51cde2013-07-03 13:33:05 -0700132 "-X bw_happy_box",
133 "-X bw_penalty_box",
134 "-X bw_costly_shared",
JP Abgralle4788732013-07-02 20:28:45 -0700135
136 /* Just a couple that are the most common. */
JP Abgrall7e51cde2013-07-03 13:33:05 -0700137 "-X bw_costly_rmnet0",
138 "-X bw_costly_wlan0",
JP Abgrall0dad7c22011-06-24 11:58:14 -0700139};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700140
JP Abgralldb7da582011-09-18 12:57:32 -0700141const char *BandwidthController::IPT_SETUP_COMMANDS[] = {
JP Abgrall7e51cde2013-07-03 13:33:05 -0700142 "-N bw_happy_box",
143 "-N bw_penalty_box",
144 "-N bw_costly_shared",
JP Abgrall0dad7c22011-06-24 11:58:14 -0700145};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700146
JP Abgralldb7da582011-09-18 12:57:32 -0700147const char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = {
JP Abgrall0031cea2012-04-17 16:38:23 -0700148 "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700149
JP Abgrall0031cea2012-04-17 16:38:23 -0700150 "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700151
JP Abgrall7e51cde2013-07-03 13:33:05 -0700152 "-A bw_costly_shared --jump bw_penalty_box",
JP Abgrallf66d6e92012-04-27 00:22:57 -0700153
JP Abgrall92009c82013-02-06 18:01:24 -0800154 "-t raw -A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */
155 "-t mangle -A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700156};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700157
158BandwidthController::BandwidthController(void) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700159}
160
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700161int BandwidthController::runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrallad729ac2012-04-24 23:27:44 -0700162 IptFailureLog failureHandling) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700163 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700164
Steve Block3fb42e02011-10-20 11:55:56 +0100165 ALOGV("runIpxtablesCmd(cmd=%s)", cmd);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700166 res |= runIptablesCmd(cmd, jumpHandling, IptIpV4, failureHandling);
167 res |= runIptablesCmd(cmd, jumpHandling, IptIpV6, failureHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700168 return res;
169}
170
JP Abgrall26e0d492011-06-24 19:21:51 -0700171int BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) {
172
173 memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0'
174 strncpy(buffer, src, buffSize);
175 return buffer[buffSize - 1];
176}
177
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700178int BandwidthController::runIptablesCmd(const char *cmd, IptJumpOp jumpHandling,
JP Abgrallad729ac2012-04-24 23:27:44 -0700179 IptIpVer iptVer, IptFailureLog failureHandling) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700180 char buffer[MAX_CMD_LEN];
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700181 const char *argv[MAX_CMD_ARGS];
JP Abgrall26e0d492011-06-24 19:21:51 -0700182 int argc = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700183 char *next = buffer;
184 char *tmp;
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700185 int res;
Rom Lemarchand14150212013-01-24 10:01:04 -0800186 int status = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700187
JP Abgrall0dad7c22011-06-24 11:58:14 -0700188 std::string fullCmd = cmd;
JP Abgrall26e0d492011-06-24 19:21:51 -0700189
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700190 switch (jumpHandling) {
191 case IptJumpReject:
JP Abgrall340d5cc2013-06-28 17:06:00 -0700192 /*
193 * Must be carefull what one rejects with, as uper layer protocols will just
194 * keep on hammering the device until the number of retries are done.
195 * For port-unreachable (default), TCP should consider as an abort (RFC1122).
196 */
197 fullCmd += " --jump REJECT";
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700198 break;
199 case IptJumpReturn:
200 fullCmd += " --jump RETURN";
201 break;
202 case IptJumpNoAdd:
203 break;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700204 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700205
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700206 fullCmd.insert(0, " ");
207 fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700208
Rom Lemarchand14150212013-01-24 10:01:04 -0800209 if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
210 ALOGE("iptables command too long");
211 return -1;
212 }
213
214 argc = 0;
215 while ((tmp = strsep(&next, " "))) {
216 argv[argc++] = tmp;
217 if (argc >= MAX_CMD_ARGS) {
218 ALOGE("iptables argument overflow");
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700219 return -1;
220 }
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700221 }
Rom Lemarchand14150212013-01-24 10:01:04 -0800222
223 argv[argc] = NULL;
224 res = android_fork_execvp(argc, (char **)argv, &status, false,
225 failureHandling == IptFailShow);
JP Abgrallc8dc63b2013-02-13 16:30:00 -0800226 res = res || !WIFEXITED(status) || WEXITSTATUS(status);
227 if (res && failureHandling == IptFailShow) {
228 ALOGE("runIptablesCmd(): res=%d status=%d failed %s", res, status,
229 fullCmd.c_str());
JP Abgrall11b4e9b2011-08-11 15:34:49 -0700230 }
231 return res;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700232}
233
JP Abgrall0031cea2012-04-17 16:38:23 -0700234int BandwidthController::setupIptablesHooks(void) {
235
236 /* Some of the initialCommands are allowed to fail */
237 runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
238 IPT_FLUSH_COMMANDS, RunCmdFailureOk);
239
240 runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
241 IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
242
243 runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*),
244 IPT_SETUP_COMMANDS, RunCmdFailureBad);
245
246 return 0;
JP Abgrall0031cea2012-04-17 16:38:23 -0700247}
248
249int BandwidthController::enableBandwidthControl(bool force) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700250 int res;
JP Abgrall0031cea2012-04-17 16:38:23 -0700251 char value[PROPERTY_VALUE_MAX];
252
253 if (!force) {
254 property_get("persist.bandwidth.enable", value, "1");
255 if (!strcmp(value, "0"))
256 return 0;
257 }
JP Abgrall8a932722011-07-13 19:17:35 -0700258
JP Abgralldb7da582011-09-18 12:57:32 -0700259 /* Let's pretend we started from scratch ... */
JP Abgrall8a932722011-07-13 19:17:35 -0700260 sharedQuotaIfaces.clear();
261 quotaIfaces.clear();
262 naughtyAppUids.clear();
JP Abgralle4788732013-07-02 20:28:45 -0700263 niceAppUids.clear();
JP Abgralldb7da582011-09-18 12:57:32 -0700264 globalAlertBytes = 0;
JP Abgrallc6c67342011-10-07 16:28:54 -0700265 globalAlertTetherCount = 0;
JP Abgralldb7da582011-09-18 12:57:32 -0700266 sharedQuotaBytes = sharedAlertBytes = 0;
267
JP Abgrall0031cea2012-04-17 16:38:23 -0700268 res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
269 IPT_FLUSH_COMMANDS, RunCmdFailureOk);
JP Abgralldb7da582011-09-18 12:57:32 -0700270
JP Abgrall0031cea2012-04-17 16:38:23 -0700271 res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*),
JP Abgralldb7da582011-09-18 12:57:32 -0700272 IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad);
JP Abgrall8a932722011-07-13 19:17:35 -0700273
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700274 return res;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700275
276}
277
278int BandwidthController::disableBandwidthControl(void) {
JP Abgrall0031cea2012-04-17 16:38:23 -0700279 runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
280 IPT_FLUSH_COMMANDS, RunCmdFailureOk);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700281 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700282}
283
JP Abgrall8a932722011-07-13 19:17:35 -0700284int BandwidthController::runCommands(int numCommands, const char *commands[],
285 RunCmdErrHandling cmdErrHandling) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700286 int res = 0;
JP Abgrallad729ac2012-04-24 23:27:44 -0700287 IptFailureLog failureLogging = IptFailShow;
288 if (cmdErrHandling == RunCmdFailureOk) {
289 failureLogging = IptFailHide;
290 }
Steve Block3fb42e02011-10-20 11:55:56 +0100291 ALOGV("runCommands(): %d commands", numCommands);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700292 for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700293 res = runIpxtablesCmd(commands[cmdNum], IptJumpNoAdd, failureLogging);
JP Abgrall0031cea2012-04-17 16:38:23 -0700294 if (res && cmdErrHandling != RunCmdFailureOk)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700295 return res;
296 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700297 return 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700298}
299
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700300std::string BandwidthController::makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700301 std::string res;
JP Abgrall8a932722011-07-13 19:17:35 -0700302 char *buff;
303 const char *opFlag;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700304
305 switch (op) {
JP Abgrall8a932722011-07-13 19:17:35 -0700306 case IptOpInsert:
307 opFlag = "-I";
308 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800309 case IptOpAppend:
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700310 ALOGE("Append op not supported for %s uids", chain);
311 res = "";
312 return res;
JP Abgrall109899b2013-02-12 19:20:13 -0800313 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700314 case IptOpReplace:
315 opFlag = "-R";
316 break;
317 default:
318 case IptOpDelete:
319 opFlag = "-D";
320 break;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700321 }
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700322 asprintf(&buff, "%s %s -m owner --uid-owner %d", opFlag, chain, uid);
JP Abgrall8a932722011-07-13 19:17:35 -0700323 res = buff;
324 free(buff);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700325 return res;
326}
327
JP Abgralle4788732013-07-02 20:28:45 -0700328int BandwidthController::enableHappyBox(void) {
329 char cmd[MAX_CMD_LEN];
330 int res = 0;
331
332 /*
333 * We tentatively delete before adding, which helps recovering
334 * from bad states (e.g. netd died).
335 */
336
337 /* Should not exist, but ignore result if already there. */
JP Abgrall7e51cde2013-07-03 13:33:05 -0700338 snprintf(cmd, sizeof(cmd), "-N bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700339 runIpxtablesCmd(cmd, IptJumpNoAdd);
340
341 /* Should be empty, but clear in case something was wrong. */
342 niceAppUids.clear();
JP Abgrall7e51cde2013-07-03 13:33:05 -0700343 snprintf(cmd, sizeof(cmd), "-F bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700344 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
345
JP Abgrall7e51cde2013-07-03 13:33:05 -0700346 snprintf(cmd, sizeof(cmd), "-D bw_penalty_box -j bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700347 runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall7e51cde2013-07-03 13:33:05 -0700348 snprintf(cmd, sizeof(cmd), "-A bw_penalty_box -j bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700349 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
350
351 /* Reject. Defaulting to prot-unreachable */
JP Abgrall7e51cde2013-07-03 13:33:05 -0700352 snprintf(cmd, sizeof(cmd), "-D bw_happy_box -j REJECT");
JP Abgralle4788732013-07-02 20:28:45 -0700353 runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall7e51cde2013-07-03 13:33:05 -0700354 snprintf(cmd, sizeof(cmd), "-A bw_happy_box -j REJECT");
JP Abgralle4788732013-07-02 20:28:45 -0700355 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
356
357 return res;
358}
359
360int BandwidthController::disableHappyBox(void) {
361 char cmd[MAX_CMD_LEN];
362
363 /* Best effort */
JP Abgrall7e51cde2013-07-03 13:33:05 -0700364 snprintf(cmd, sizeof(cmd), "-D bw_penalty_box -j bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700365 runIpxtablesCmd(cmd, IptJumpNoAdd);
366 niceAppUids.clear();
JP Abgrall7e51cde2013-07-03 13:33:05 -0700367 snprintf(cmd, sizeof(cmd), "-F bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700368 runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall7e51cde2013-07-03 13:33:05 -0700369 snprintf(cmd, sizeof(cmd), "-X bw_happy_box");
JP Abgralle4788732013-07-02 20:28:45 -0700370 runIpxtablesCmd(cmd, IptJumpNoAdd);
371
372 return 0;
373}
374
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700375int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700376 return manipulateNaughtyApps(numUids, appUids, SpecialAppOpAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700377}
378
379int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700380 return manipulateNaughtyApps(numUids, appUids, SpecialAppOpRemove);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700381}
382
JP Abgralle4788732013-07-02 20:28:45 -0700383int BandwidthController::addNiceApps(int numUids, char *appUids[]) {
384 return manipulateNiceApps(numUids, appUids, SpecialAppOpAdd);
385}
386
387int BandwidthController::removeNiceApps(int numUids, char *appUids[]) {
388 return manipulateNiceApps(numUids, appUids, SpecialAppOpRemove);
389}
390
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700391int BandwidthController::manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
JP Abgrall7e51cde2013-07-03 13:33:05 -0700392 return manipulateSpecialApps(numUids, appStrUids, "bw_penalty_box", naughtyAppUids, IptJumpReject, appOp);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700393}
394
JP Abgralle4788732013-07-02 20:28:45 -0700395int BandwidthController::manipulateNiceApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
JP Abgrall7e51cde2013-07-03 13:33:05 -0700396 return manipulateSpecialApps(numUids, appStrUids, "bw_happy_box", niceAppUids, IptJumpReturn, appOp);
JP Abgralle4788732013-07-02 20:28:45 -0700397}
398
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700399
400int BandwidthController::manipulateSpecialApps(int numUids, char *appStrUids[],
401 const char *chain,
402 std::list<int /*appUid*/> &specialAppUids,
403 IptJumpOp jumpHandling, SpecialAppOp appOp) {
404
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700405 char cmd[MAX_CMD_LEN];
406 int uidNum;
JP Abgrall26e0d492011-06-24 19:21:51 -0700407 const char *failLogTemplate;
408 IptOp op;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700409 int appUids[numUids];
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700410 std::string iptCmd;
JP Abgrallb1d24092012-04-27 01:02:31 -0700411 std::list<int /*uid*/>::iterator it;
JP Abgrall8a932722011-07-13 19:17:35 -0700412
JP Abgrall26e0d492011-06-24 19:21:51 -0700413 switch (appOp) {
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700414 case SpecialAppOpAdd:
JP Abgrall8a932722011-07-13 19:17:35 -0700415 op = IptOpInsert;
JP Abgrallaf476f72013-07-03 12:23:55 -0700416 failLogTemplate = "Failed to add app uid %s(%d) to %s.";
JP Abgrall8a932722011-07-13 19:17:35 -0700417 break;
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700418 case SpecialAppOpRemove:
JP Abgrall8a932722011-07-13 19:17:35 -0700419 op = IptOpDelete;
JP Abgrallaf476f72013-07-03 12:23:55 -0700420 failLogTemplate = "Failed to delete app uid %s(%d) from %s box.";
JP Abgrall8a932722011-07-13 19:17:35 -0700421 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700422 default:
423 ALOGE("Unexpected app Op %d", appOp);
424 return -1;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700425 }
426
427 for (uidNum = 0; uidNum < numUids; uidNum++) {
JP Abgrallaf476f72013-07-03 12:23:55 -0700428 char *end;
429 appUids[uidNum] = strtoul(appStrUids[uidNum], &end, 0);
430 if (*end || !*appStrUids[uidNum]) {
431 ALOGE(failLogTemplate, appStrUids[uidNum], appUids[uidNum], chain);
JP Abgrall26e0d492011-06-24 19:21:51 -0700432 goto fail_parse;
433 }
434 }
JP Abgrall26e0d492011-06-24 19:21:51 -0700435
436 for (uidNum = 0; uidNum < numUids; uidNum++) {
JP Abgrallb1d24092012-04-27 01:02:31 -0700437 int uid = appUids[uidNum];
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700438 for (it = specialAppUids.begin(); it != specialAppUids.end(); it++) {
JP Abgrallb1d24092012-04-27 01:02:31 -0700439 if (*it == uid)
440 break;
441 }
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700442 bool found = (it != specialAppUids.end());
JP Abgrallb1d24092012-04-27 01:02:31 -0700443
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700444 if (appOp == SpecialAppOpRemove) {
JP Abgrallb1d24092012-04-27 01:02:31 -0700445 if (!found) {
446 ALOGE("No such appUid %d to remove", uid);
447 return -1;
448 }
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700449 specialAppUids.erase(it);
JP Abgrallb1d24092012-04-27 01:02:31 -0700450 } else {
451 if (found) {
452 ALOGE("appUid %d exists already", uid);
453 return -1;
454 }
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700455 specialAppUids.push_front(uid);
JP Abgrallb1d24092012-04-27 01:02:31 -0700456 }
457
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700458 iptCmd = makeIptablesSpecialAppCmd(op, uid, chain);
459 if (runIpxtablesCmd(iptCmd.c_str(), jumpHandling)) {
JP Abgrallaf476f72013-07-03 12:23:55 -0700460 ALOGE(failLogTemplate, appStrUids[uidNum], uid, chain);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700461 goto fail_with_uidNum;
462 }
463 }
464 return 0;
465
JP Abgrall26e0d492011-06-24 19:21:51 -0700466fail_with_uidNum:
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700467 /* Try to remove the uid that failed in any case*/
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700468 iptCmd = makeIptablesSpecialAppCmd(IptOpDelete, appUids[uidNum], chain);
469 runIpxtablesCmd(iptCmd.c_str(), jumpHandling);
JP Abgrall26e0d492011-06-24 19:21:51 -0700470fail_parse:
471 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700472}
473
JP Abgrall26e0d492011-06-24 19:21:51 -0700474std::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700475 std::string res;
JP Abgrall8a932722011-07-13 19:17:35 -0700476 char *buff;
477 const char *opFlag;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700478
Steve Block3fb42e02011-10-20 11:55:56 +0100479 ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700480
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700481 switch (op) {
JP Abgrall8a932722011-07-13 19:17:35 -0700482 case IptOpInsert:
483 opFlag = "-I";
484 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800485 case IptOpAppend:
486 opFlag = "-A";
487 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700488 case IptOpReplace:
489 opFlag = "-R";
490 break;
491 default:
492 case IptOpDelete:
493 opFlag = "-D";
494 break;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700495 }
JP Abgrall8a932722011-07-13 19:17:35 -0700496
JP Abgrallbfa74662011-06-29 19:23:04 -0700497 // The requried IP version specific --jump REJECT ... will be added later.
JP Abgrall7e51cde2013-07-03 13:33:05 -0700498 asprintf(&buff, "%s bw_costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota,
JP Abgrall8a932722011-07-13 19:17:35 -0700499 costName);
500 res = buff;
501 free(buff);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700502 return res;
503}
504
JP Abgrall26e0d492011-06-24 19:21:51 -0700505int BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700506 char cmd[MAX_CMD_LEN];
JP Abgrall0031cea2012-04-17 16:38:23 -0700507 int res = 0, res1, res2;
JP Abgrall8a932722011-07-13 19:17:35 -0700508 int ruleInsertPos = 1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700509 std::string costString;
510 const char *costCString;
511
JP Abgrall0dad7c22011-06-24 11:58:14 -0700512 /* The "-N costly" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700513 switch (quotaType) {
514 case QuotaUnique:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700515 costString = "bw_costly_";
JP Abgrall0dad7c22011-06-24 11:58:14 -0700516 costString += ifn;
517 costCString = costString.c_str();
JP Abgrall0031cea2012-04-17 16:38:23 -0700518 /*
JP Abgrall7e51cde2013-07-03 13:33:05 -0700519 * Flush the bw_costly_<iface> is allowed to fail in case it didn't exist.
JP Abgrall0031cea2012-04-17 16:38:23 -0700520 * Creating a new one is allowed to fail in case it existed.
521 * This helps with netd restarts.
522 */
523 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700524 res1 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700525 snprintf(cmd, sizeof(cmd), "-N %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700526 res2 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700527 res = (res1 && res2) || (!res1 && !res2);
528
JP Abgrall7e51cde2013-07-03 13:33:05 -0700529 snprintf(cmd, sizeof(cmd), "-A %s -j bw_penalty_box", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700530 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall26e0d492011-06-24 19:21:51 -0700531 break;
532 case QuotaShared:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700533 costCString = "bw_costly_shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700534 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700535 default:
536 ALOGE("Unexpected quotatype %d", quotaType);
537 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700538 }
539
JP Abgrall8a932722011-07-13 19:17:35 -0700540 if (globalAlertBytes) {
541 /* The alert rule comes 1st */
542 ruleInsertPos = 2;
543 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700544
545 snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700546 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700547
548 snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700549 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0031cea2012-04-17 16:38:23 -0700550
551 snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700552 runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
JP Abgrall0031cea2012-04-17 16:38:23 -0700553
554 snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700555 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700556 return res;
557}
558
JP Abgrall26e0d492011-06-24 19:21:51 -0700559int BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700560 char cmd[MAX_CMD_LEN];
561 int res = 0;
562 std::string costString;
563 const char *costCString;
564
JP Abgrall26e0d492011-06-24 19:21:51 -0700565 switch (quotaType) {
566 case QuotaUnique:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700567 costString = "bw_costly_";
JP Abgrall0dad7c22011-06-24 11:58:14 -0700568 costString += ifn;
569 costCString = costString.c_str();
JP Abgrall26e0d492011-06-24 19:21:51 -0700570 break;
571 case QuotaShared:
JP Abgrall7e51cde2013-07-03 13:33:05 -0700572 costCString = "bw_costly_shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700573 break;
JP Abgrall0031cea2012-04-17 16:38:23 -0700574 default:
575 ALOGE("Unexpected quotatype %d", quotaType);
576 return -1;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700577 }
578
JP Abgrall0031cea2012-04-17 16:38:23 -0700579 snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700580 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0031cea2012-04-17 16:38:23 -0700581 snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700582 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700583
JP Abgrall7e51cde2013-07-03 13:33:05 -0700584 /* The "-N bw_costly_shared" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700585 if (quotaType == QuotaUnique) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700586 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700587 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgralla9f802c2011-06-29 15:46:45 -0700588 snprintf(cmd, sizeof(cmd), "-X %s", costCString);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700589 res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700590 }
591 return res;
592}
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700593
JP Abgrall0dad7c22011-06-24 11:58:14 -0700594int BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700595 char cmd[MAX_CMD_LEN];
596 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700597 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700598 std::string quotaCmd;
JP Abgrall8a932722011-07-13 19:17:35 -0700599 std::string ifaceName;
600 ;
JP Abgrallbfa74662011-06-29 19:23:04 -0700601 const char *costName = "shared";
JP Abgrall26e0d492011-06-24 19:21:51 -0700602 std::list<std::string>::iterator it;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700603
JP Abgrall8a932722011-07-13 19:17:35 -0700604 if (!maxBytes) {
605 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000606 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700607 return -1;
608 }
JP Abgrall26e0d492011-06-24 19:21:51 -0700609 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000610 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700611 return -1;
612 }
613 ifaceName = ifn;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700614
615 if (maxBytes == -1) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700616 return removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700617 }
618
619 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700620 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
621 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700622 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700623 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700624
JP Abgrall0dad7c22011-06-24 11:58:14 -0700625 if (it == sharedQuotaIfaces.end()) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700626 res |= prepCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700627 if (sharedQuotaIfaces.empty()) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700628 quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700629 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700630 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000631 ALOGE("Failed set quota rule");
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700632 goto fail;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700633 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700634 sharedQuotaBytes = maxBytes;
635 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700636 sharedQuotaIfaces.push_front(ifaceName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700637
638 }
639
640 if (maxBytes != sharedQuotaBytes) {
JP Abgrall8a932722011-07-13 19:17:35 -0700641 res |= updateQuota(costName, maxBytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700642 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000643 ALOGE("Failed update quota for %s", costName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700644 goto fail;
645 }
646 sharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700647 }
648 return 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700649
650 fail:
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700651 /*
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700652 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
653 * rules in the kernel to see which ones need cleaning up.
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700654 * For now callers needs to choose if they want to "ndc bandwidth enable"
655 * which resets everything.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700656 */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700657 removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700658 return -1;
659}
660
JP Abgrall8a932722011-07-13 19:17:35 -0700661/* It will also cleanup any shared alerts */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700662int BandwidthController::removeInterfaceSharedQuota(const char *iface) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700663 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700664 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700665 std::string ifaceName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700666 std::list<std::string>::iterator it;
JP Abgrallbfa74662011-06-29 19:23:04 -0700667 const char *costName = "shared";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700668
JP Abgrall8a932722011-07-13 19:17:35 -0700669 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000670 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700671 return -1;
672 }
JP Abgrall8a932722011-07-13 19:17:35 -0700673 ifaceName = ifn;
JP Abgrall26e0d492011-06-24 19:21:51 -0700674
JP Abgrall0dad7c22011-06-24 11:58:14 -0700675 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
676 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700677 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700678 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700679 if (it == sharedQuotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000680 ALOGE("No such iface %s to delete", ifn);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700681 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700682 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700683
JP Abgrall26e0d492011-06-24 19:21:51 -0700684 res |= cleanupCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700685 sharedQuotaIfaces.erase(it);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700686
JP Abgrall0dad7c22011-06-24 11:58:14 -0700687 if (sharedQuotaIfaces.empty()) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700688 std::string quotaCmd;
JP Abgrallbfa74662011-06-29 19:23:04 -0700689 quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700690 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall8a932722011-07-13 19:17:35 -0700691 sharedQuotaBytes = 0;
692 if (sharedAlertBytes) {
693 removeSharedAlert();
694 sharedAlertBytes = 0;
695 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700696 }
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700697 return res;
698}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700699
700int BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
701 char ifn[MAX_IFACENAME_LEN];
702 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700703 std::string ifaceName;
704 const char *costName;
705 std::list<QuotaInfo>::iterator it;
706 std::string quotaCmd;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700707
JP Abgrall8a932722011-07-13 19:17:35 -0700708 if (!maxBytes) {
709 /* Don't talk about -1, deprecate it. */
Steve Block5ea0c052012-01-06 19:18:11 +0000710 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700711 return -1;
712 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700713 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700714 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700715 }
716
JP Abgrall8a932722011-07-13 19:17:35 -0700717 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000718 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700719 return -1;
720 }
721 ifaceName = ifn;
722 costName = iface;
723
JP Abgrall0dad7c22011-06-24 11:58:14 -0700724 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700725 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
JP Abgrall8a932722011-07-13 19:17:35 -0700726 if (it->ifaceName == ifaceName)
JP Abgrall0dad7c22011-06-24 11:58:14 -0700727 break;
728 }
729
730 if (it == quotaIfaces.end()) {
JP Abgralle4788732013-07-02 20:28:45 -0700731 /* Preparing the iface adds a penalty/happy box check */
JP Abgrall26e0d492011-06-24 19:21:51 -0700732 res |= prepCostlyIface(ifn, QuotaUnique);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700733 /*
JP Abgralle4788732013-07-02 20:28:45 -0700734 * The rejecting quota limit should go after the penalty/happy box checks
JP Abgrallbaeccc42013-06-25 09:44:10 -0700735 * or else a naughty app could just eat up the quota.
736 * So we append here.
737 */
JP Abgrall109899b2013-02-12 19:20:13 -0800738 quotaCmd = makeIptablesQuotaCmd(IptOpAppend, costName, maxBytes);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700739 res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700740 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000741 ALOGE("Failed set quota rule");
JP Abgrall0dad7c22011-06-24 11:58:14 -0700742 goto fail;
743 }
744
JP Abgrall8a932722011-07-13 19:17:35 -0700745 quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
JP Abgrall0dad7c22011-06-24 11:58:14 -0700746
747 } else {
JP Abgrall8a932722011-07-13 19:17:35 -0700748 res |= updateQuota(costName, maxBytes);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700749 if (res) {
Steve Block5ea0c052012-01-06 19:18:11 +0000750 ALOGE("Failed update quota for %s", iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700751 goto fail;
752 }
JP Abgrall8a932722011-07-13 19:17:35 -0700753 it->quota = maxBytes;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700754 }
755 return 0;
756
757 fail:
758 /*
759 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
760 * rules in the kernel to see which ones need cleaning up.
761 * For now callers needs to choose if they want to "ndc bandwidth enable"
762 * which resets everything.
763 */
764 removeInterfaceSharedQuota(ifn);
765 return -1;
766}
767
JP Abgrall8a932722011-07-13 19:17:35 -0700768int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
769 return getInterfaceQuota("shared", bytes);
770}
771
772int BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
773 FILE *fp;
774 char *fname;
775 int scanRes;
776
777 asprintf(&fname, "/proc/net/xt_quota/%s", costName);
778 fp = fopen(fname, "r");
779 free(fname);
780 if (!fp) {
Steve Block5ea0c052012-01-06 19:18:11 +0000781 ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
JP Abgrall8a932722011-07-13 19:17:35 -0700782 return -1;
783 }
784 scanRes = fscanf(fp, "%lld", bytes);
Steve Block3fb42e02011-10-20 11:55:56 +0100785 ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700786 fclose(fp);
787 return scanRes == 1 ? 0 : -1;
788}
789
JP Abgrall0dad7c22011-06-24 11:58:14 -0700790int BandwidthController::removeInterfaceQuota(const char *iface) {
791
792 char ifn[MAX_IFACENAME_LEN];
793 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700794 std::string ifaceName;
795 const char *costName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700796 std::list<QuotaInfo>::iterator it;
JP Abgrall26e0d492011-06-24 19:21:51 -0700797
JP Abgrall8a932722011-07-13 19:17:35 -0700798 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000799 ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
JP Abgrall26e0d492011-06-24 19:21:51 -0700800 return -1;
801 }
802 ifaceName = ifn;
803 costName = iface;
804
JP Abgrall0dad7c22011-06-24 11:58:14 -0700805 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
JP Abgrall8a932722011-07-13 19:17:35 -0700806 if (it->ifaceName == ifaceName)
JP Abgrall0dad7c22011-06-24 11:58:14 -0700807 break;
808 }
809
810 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000811 ALOGE("No such iface %s to delete", ifn);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700812 return -1;
813 }
814
815 /* This also removes the quota command of CostlyIface chain. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700816 res |= cleanupCostlyIface(ifn, QuotaUnique);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700817
818 quotaIfaces.erase(it);
819
820 return res;
821}
JP Abgrall8a932722011-07-13 19:17:35 -0700822
823int BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
824 FILE *fp;
825 char *fname;
826
827 asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
828 fp = fopen(fname, "w");
829 free(fname);
830 if (!fp) {
Steve Block5ea0c052012-01-06 19:18:11 +0000831 ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
JP Abgrall8a932722011-07-13 19:17:35 -0700832 return -1;
833 }
834 fprintf(fp, "%lld\n", bytes);
835 fclose(fp);
836 return 0;
837}
838
839int BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
840 int res = 0;
841 const char *opFlag;
842 char *alertQuotaCmd;
843
844 switch (op) {
845 case IptOpInsert:
846 opFlag = "-I";
847 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800848 case IptOpAppend:
849 opFlag = "-A";
850 break;
JP Abgrall8a932722011-07-13 19:17:35 -0700851 case IptOpReplace:
852 opFlag = "-R";
853 break;
854 default:
855 case IptOpDelete:
856 opFlag = "-D";
857 break;
858 }
859
JP Abgrall92009c82013-02-06 18:01:24 -0800860 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800861 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700862 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700863 free(alertQuotaCmd);
JP Abgrall92009c82013-02-06 18:01:24 -0800864 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800865 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700866 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -0700867 free(alertQuotaCmd);
868 return res;
869}
870
JP Abgrallc6c67342011-10-07 16:28:54 -0700871int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
872 int res = 0;
873 const char *opFlag;
JP Abgrall8a932722011-07-13 19:17:35 -0700874 char *alertQuotaCmd;
JP Abgrallc6c67342011-10-07 16:28:54 -0700875
876 switch (op) {
877 case IptOpInsert:
878 opFlag = "-I";
879 break;
JP Abgrall109899b2013-02-12 19:20:13 -0800880 case IptOpAppend:
881 opFlag = "-A";
882 break;
JP Abgrallc6c67342011-10-07 16:28:54 -0700883 case IptOpReplace:
884 opFlag = "-R";
885 break;
886 default:
887 case IptOpDelete:
888 opFlag = "-D";
889 break;
890 }
891
JP Abgrall92009c82013-02-06 18:01:24 -0800892 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD",
Nick Kralevichc2b26cb2012-02-23 13:04:26 -0800893 bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700894 res = runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrallc6c67342011-10-07 16:28:54 -0700895 free(alertQuotaCmd);
896 return res;
897}
898
899int BandwidthController::setGlobalAlert(int64_t bytes) {
900 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700901 int res = 0;
902
903 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000904 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700905 return -1;
906 }
907 if (globalAlertBytes) {
908 res = updateQuota(alertName, bytes);
909 } else {
910 res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700911 if (globalAlertTetherCount) {
Steve Block3fb42e02011-10-20 11:55:56 +0100912 ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700913 res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
914 }
JP Abgrall8a932722011-07-13 19:17:35 -0700915 }
916 globalAlertBytes = bytes;
917 return res;
918}
919
JP Abgrallc6c67342011-10-07 16:28:54 -0700920int BandwidthController::setGlobalAlertInForwardChain(void) {
921 const char *alertName = ALERT_GLOBAL_NAME;
922 int res = 0;
JP Abgrall8a932722011-07-13 19:17:35 -0700923
JP Abgrallc6c67342011-10-07 16:28:54 -0700924 globalAlertTetherCount++;
Steve Block3fb42e02011-10-20 11:55:56 +0100925 ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
JP Abgrallc6c67342011-10-07 16:28:54 -0700926
927 /*
928 * If there is no globalAlert active we are done.
929 * If there is an active globalAlert but this is not the 1st
930 * tether, we are also done.
931 */
932 if (!globalAlertBytes || globalAlertTetherCount != 1) {
933 return 0;
934 }
935
936 /* We only add the rule if this was the 1st tether added. */
937 res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
938 return res;
939}
940
941int BandwidthController::removeGlobalAlert(void) {
942
943 const char *alertName = ALERT_GLOBAL_NAME;
JP Abgrall8a932722011-07-13 19:17:35 -0700944 int res = 0;
945
946 if (!globalAlertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000947 ALOGE("No prior alert set");
JP Abgrall8a932722011-07-13 19:17:35 -0700948 return -1;
949 }
950 res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
JP Abgrallc6c67342011-10-07 16:28:54 -0700951 if (globalAlertTetherCount) {
952 res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
953 }
JP Abgrall8a932722011-07-13 19:17:35 -0700954 globalAlertBytes = 0;
955 return res;
956}
957
JP Abgrallc6c67342011-10-07 16:28:54 -0700958int BandwidthController::removeGlobalAlertInForwardChain(void) {
959 int res = 0;
960 const char *alertName = ALERT_GLOBAL_NAME;
961
962 if (!globalAlertTetherCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000963 ALOGE("No prior alert set");
JP Abgrallc6c67342011-10-07 16:28:54 -0700964 return -1;
965 }
966
967 globalAlertTetherCount--;
968 /*
969 * If there is no globalAlert active we are done.
970 * If there is an active globalAlert but there are more
971 * tethers, we are also done.
972 */
973 if (!globalAlertBytes || globalAlertTetherCount >= 1) {
974 return 0;
975 }
976
977 /* We only detete the rule if this was the last tether removed. */
978 res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
979 return res;
980}
981
JP Abgrall8a932722011-07-13 19:17:35 -0700982int BandwidthController::setSharedAlert(int64_t bytes) {
983 if (!sharedQuotaBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000984 ALOGE("Need to have a prior shared quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -0700985 return -1;
986 }
987 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +0000988 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -0700989 return -1;
990 }
991 return setCostlyAlert("shared", bytes, &sharedAlertBytes);
992}
993
994int BandwidthController::removeSharedAlert(void) {
995 return removeCostlyAlert("shared", &sharedAlertBytes);
996}
997
998int BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
999 std::list<QuotaInfo>::iterator it;
1000
1001 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001002 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -07001003 return -1;
1004 }
1005 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
1006 if (it->ifaceName == iface)
1007 break;
1008 }
1009
1010 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +00001011 ALOGE("Need to have a prior interface quota set to set an alert");
JP Abgrall8a932722011-07-13 19:17:35 -07001012 return -1;
1013 }
1014
1015 return setCostlyAlert(iface, bytes, &it->alert);
1016}
1017
1018int BandwidthController::removeInterfaceAlert(const char *iface) {
1019 std::list<QuotaInfo>::iterator it;
1020
1021 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
1022 if (it->ifaceName == iface)
1023 break;
1024 }
1025
1026 if (it == quotaIfaces.end()) {
Steve Block5ea0c052012-01-06 19:18:11 +00001027 ALOGE("No prior alert set for interface %s", iface);
JP Abgrall8a932722011-07-13 19:17:35 -07001028 return -1;
1029 }
1030
1031 return removeCostlyAlert(iface, &it->alert);
1032}
1033
1034int BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
1035 char *alertQuotaCmd;
JP Abgrall109899b2013-02-12 19:20:13 -08001036 char *chainName;
JP Abgrall8a932722011-07-13 19:17:35 -07001037 int res = 0;
1038 char *alertName;
1039
1040 if (!bytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001041 ALOGE("Invalid bytes value. 1..max_int64.");
JP Abgrall8a932722011-07-13 19:17:35 -07001042 return -1;
1043 }
1044 asprintf(&alertName, "%sAlert", costName);
1045 if (*alertBytes) {
1046 res = updateQuota(alertName, *alertBytes);
1047 } else {
JP Abgrall7e51cde2013-07-03 13:33:05 -07001048 asprintf(&chainName, "bw_costly_%s", costName);
JP Abgrall109899b2013-02-12 19:20:13 -08001049 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -07001050 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -07001051 free(alertQuotaCmd);
JP Abgrall109899b2013-02-12 19:20:13 -08001052 free(chainName);
JP Abgrall8a932722011-07-13 19:17:35 -07001053 }
1054 *alertBytes = bytes;
1055 free(alertName);
1056 return res;
1057}
1058
1059int BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
1060 char *alertQuotaCmd;
1061 char *chainName;
1062 char *alertName;
1063 int res = 0;
1064
1065 asprintf(&alertName, "%sAlert", costName);
1066 if (!*alertBytes) {
Steve Block5ea0c052012-01-06 19:18:11 +00001067 ALOGE("No prior alert set for %s alert", costName);
JP Abgrall8a932722011-07-13 19:17:35 -07001068 return -1;
1069 }
1070
JP Abgrall7e51cde2013-07-03 13:33:05 -07001071 asprintf(&chainName, "bw_costly_%s", costName);
JP Abgrall92009c82013-02-06 18:01:24 -08001072 asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
JP Abgralla9ba4cb2013-07-02 19:08:48 -07001073 res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
JP Abgrall8a932722011-07-13 19:17:35 -07001074 free(alertQuotaCmd);
1075 free(chainName);
1076
1077 *alertBytes = 0;
1078 free(alertName);
1079 return res;
1080}
JP Abgralldb7da582011-09-18 12:57:32 -07001081
1082/*
1083 * Parse the ptks and bytes out of:
JP Abgrallbaeccc42013-06-25 09:44:10 -07001084 * Chain natctrl_tether_counters (4 references)
1085 * pkts bytes target prot opt in out source destination
1086 * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 counter wlan0_rmnet0: 0 bytes
1087 * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 counter rmnet0_wlan0: 0 bytes
1088 * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0 counter bt-pan_rmnet0: 0 bytes
1089 * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0 counter rmnet0_bt-pan: 0 bytes
JP Abgralldb7da582011-09-18 12:57:32 -07001090 */
JP Abgrallbaeccc42013-06-25 09:44:10 -07001091int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherStats filter,
1092 FILE *fp, std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -07001093 int res;
1094 char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1095 char iface0[MAX_IPT_OUTPUT_LINE_LEN];
1096 char iface1[MAX_IPT_OUTPUT_LINE_LEN];
1097 char rest[MAX_IPT_OUTPUT_LINE_LEN];
1098
JP Abgrallbaeccc42013-06-25 09:44:10 -07001099 TetherStats stats;
JP Abgralldb7da582011-09-18 12:57:32 -07001100 char *buffPtr;
1101 int64_t packets, bytes;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001102 int statsFound = 0;
1103
1104 bool filterPair = filter.intIface[0] && filter.extIface[0];
1105
1106 char *filterMsg = filter.getStatsLine();
1107 ALOGV("filter: %s", filterMsg);
1108 free(filterMsg);
1109
1110 stats = filter;
JP Abgralldb7da582011-09-18 12:57:32 -07001111
1112 while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1113 /* Clean up, so a failed parse can still print info */
1114 iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
JP Abgrall0031cea2012-04-17 16:38:23 -07001115 res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s",
JP Abgralldb7da582011-09-18 12:57:32 -07001116 &packets, &bytes, iface0, iface1, rest);
Steve Block3fb42e02011-10-20 11:55:56 +01001117 ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res,
JP Abgralldb7da582011-09-18 12:57:32 -07001118 iface0, iface1, packets, bytes, rest, buffPtr);
JP Abgralla2a64f02011-11-11 20:36:16 -08001119 extraProcessingInfo += buffPtr;
1120
JP Abgralldb7da582011-09-18 12:57:32 -07001121 if (res != 5) {
1122 continue;
1123 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001124 /*
1125 * The following assumes that the 1st rule has in:extIface out:intIface,
1126 * which is what NatController sets up.
1127 * If not filtering, the 1st match rx, and sets up the pair for the tx side.
1128 */
1129 if (filter.intIface[0] && filter.extIface[0]) {
1130 if (filter.intIface == iface0 && filter.extIface == iface1) {
1131 ALOGV("2Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1132 stats.rxPackets = packets;
1133 stats.rxBytes = bytes;
1134 } else if (filter.intIface == iface1 && filter.extIface == iface0) {
1135 ALOGV("2Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1136 stats.txPackets = packets;
1137 stats.txBytes = bytes;
1138 }
1139 } else if (filter.intIface[0] || filter.extIface[0]) {
1140 if (filter.intIface == iface0 || filter.extIface == iface1) {
1141 ALOGV("1Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1142 stats.intIface = iface0;
1143 stats.extIface = iface1;
1144 stats.rxPackets = packets;
1145 stats.rxBytes = bytes;
1146 } else if (filter.intIface == iface1 || filter.extIface == iface0) {
1147 ALOGV("1Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1148 stats.intIface = iface1;
1149 stats.extIface = iface0;
1150 stats.txPackets = packets;
1151 stats.txBytes = bytes;
1152 }
1153 } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ {
1154 if (!stats.intIface[0]) {
1155 ALOGV("0Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1156 stats.intIface = iface0;
1157 stats.extIface = iface1;
1158 stats.rxPackets = packets;
1159 stats.rxBytes = bytes;
1160 } else if (stats.intIface == iface1 && stats.extIface == iface0) {
1161 ALOGV("0Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1162 stats.txPackets = packets;
1163 stats.txBytes = bytes;
1164 }
1165 }
1166 if (stats.rxBytes != -1 && stats.txBytes != -1) {
1167 ALOGV("rx_bytes=%lld tx_bytes=%lld filterPair=%d", stats.rxBytes, stats.txBytes, filterPair);
1168 /* Send out stats, and prep for the next if needed. */
1169 char *msg = stats.getStatsLine();
1170 if (filterPair) {
1171 cli->sendMsg(ResponseCode::TetheringStatsResult, msg, false);
1172 return 0;
1173 } else {
1174 cli->sendMsg(ResponseCode::TetheringStatsListResult, msg, false);
1175 stats = filter;
1176 }
1177 free(msg);
1178 statsFound++;
JP Abgralldb7da582011-09-18 12:57:32 -07001179 }
1180 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001181 /* We found some stats, and the last one isn't a partial stats. */
1182 if (statsFound && (stats.rxBytes == -1 || stats.txBytes == -1)) {
1183 cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
1184 return 0;
1185 }
1186 return -1;
JP Abgralldb7da582011-09-18 12:57:32 -07001187}
1188
JP Abgrallbaeccc42013-06-25 09:44:10 -07001189char *BandwidthController::TetherStats::getStatsLine(void) const {
JP Abgralldb7da582011-09-18 12:57:32 -07001190 char *msg;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001191 asprintf(&msg, "%s %s %lld %lld %lld %lld", intIface.c_str(), extIface.c_str(),
JP Abgralldb7da582011-09-18 12:57:32 -07001192 rxBytes, rxPackets, txBytes, txPackets);
1193 return msg;
1194}
1195
JP Abgrallbaeccc42013-06-25 09:44:10 -07001196int BandwidthController::getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo) {
JP Abgralldb7da582011-09-18 12:57:32 -07001197 int res;
1198 std::string fullCmd;
1199 FILE *iptOutput;
1200 const char *cmd;
1201
JP Abgralldb7da582011-09-18 12:57:32 -07001202 /*
1203 * Why not use some kind of lib to talk to iptables?
1204 * Because the only libs are libiptc and libip6tc in iptables, and they are
1205 * not easy to use. They require the known iptables match modules to be
1206 * preloaded/linked, and require apparently a lot of wrapper code to get
1207 * the wanted info.
1208 */
1209 fullCmd = IPTABLES_PATH;
JP Abgrallbaeccc42013-06-25 09:44:10 -07001210 fullCmd += " -nvx -L ";
1211 fullCmd += NatController::LOCAL_TETHER_COUNTERS_CHAIN;
JP Abgralldb7da582011-09-18 12:57:32 -07001212 iptOutput = popen(fullCmd.c_str(), "r");
1213 if (!iptOutput) {
Steve Block5ea0c052012-01-06 19:18:11 +00001214 ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
JP Abgralla2a64f02011-11-11 20:36:16 -08001215 extraProcessingInfo += "Failed to run iptables.";
JP Abgralldb7da582011-09-18 12:57:32 -07001216 return -1;
1217 }
JP Abgrallbaeccc42013-06-25 09:44:10 -07001218 res = parseForwardChainStats(cli, stats, iptOutput, extraProcessingInfo);
JP Abgralldb7da582011-09-18 12:57:32 -07001219 pclose(iptOutput);
1220
1221 /* Currently NatController doesn't do ipv6 tethering, so we are done. */
1222 return res;
1223}