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 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 17 | #define LOG_NDEBUG 0 |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 18 | |
| 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 Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 25 | #include <errno.h> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 26 | #include <fcntl.h> |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 27 | #include <stdio.h> |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 28 | #include <stdlib.h> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 29 | #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 Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame] | 43 | #include <logwrap/logwrap.h> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 44 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 45 | #include "NetdConstants.h" |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 46 | #include "BandwidthController.h" |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 47 | #include "NatController.h" /* For LOCAL_TETHER_COUNTERS_CHAIN */ |
| 48 | #include "ResponseCode.h" |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 49 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 50 | /* Alphabetical */ |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 51 | #define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %lld --name %s" |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 52 | const char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert"; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 53 | const char* BandwidthController::LOCAL_INPUT = "bw_INPUT"; |
| 54 | const char* BandwidthController::LOCAL_FORWARD = "bw_FORWARD"; |
| 55 | const char* BandwidthController::LOCAL_OUTPUT = "bw_OUTPUT"; |
| 56 | const char* BandwidthController::LOCAL_RAW_PREROUTING = "bw_raw_PREROUTING"; |
| 57 | const char* BandwidthController::LOCAL_MANGLE_POSTROUTING = "bw_mangle_POSTROUTING"; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 58 | const int BandwidthController::MAX_CMD_ARGS = 32; |
| 59 | const int BandwidthController::MAX_CMD_LEN = 1024; |
| 60 | const int BandwidthController::MAX_IFACENAME_LEN = 64; |
| 61 | const int BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256; |
| 62 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 63 | /** |
| 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 Abgrall | 29e8de2 | 2012-05-03 12:52:15 -0700 | [diff] [blame] | 67 | * E.g. "-I bw_INPUT -i rmnet0 --jump costly" |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 68 | * - quota'd rules in the costly chain should be before penalty_box lookups. |
JP Abgrall | 29e8de2 | 2012-05-03 12:52:15 -0700 | [diff] [blame] | 69 | * - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains. |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 70 | * |
| 71 | * * global quota vs per interface quota |
| 72 | * - global quota for all costly interfaces uses a single costly chain: |
| 73 | * . initial rules |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 74 | * iptables -N costly_shared |
JP Abgrall | 29e8de2 | 2012-05-03 12:52:15 -0700 | [diff] [blame] | 75 | * iptables -I bw_INPUT -i iface0 --jump costly_shared |
| 76 | * iptables -I bw_OUTPUT -o iface0 --jump costly_shared |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 77 | * iptables -I costly_shared -m quota \! --quota 500000 \ |
| 78 | * --jump REJECT --reject-with icmp-net-prohibited |
| 79 | * iptables -A costly_shared --jump penalty_box |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 80 | * |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 81 | * . adding a new iface to this, E.g.: |
JP Abgrall | 29e8de2 | 2012-05-03 12:52:15 -0700 | [diff] [blame] | 82 | * iptables -I bw_INPUT -i iface1 --jump costly_shared |
| 83 | * iptables -I bw_OUTPUT -o iface1 --jump costly_shared |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 84 | * |
| 85 | * - quota per interface. This is achieve by having "costly" chains per quota. |
| 86 | * E.g. adding a new costly interface iface0 with its own quota: |
| 87 | * iptables -N costly_iface0 |
JP Abgrall | 29e8de2 | 2012-05-03 12:52:15 -0700 | [diff] [blame] | 88 | * iptables -I bw_INPUT -i iface0 --jump costly_iface0 |
| 89 | * iptables -I bw_OUTPUT -o iface0 --jump costly_iface0 |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 90 | * iptables -A costly_iface0 -m quota \! --quota 500000 \ |
| 91 | * --jump REJECT --reject-with icmp-net-prohibited |
| 92 | * iptables -A costly_iface0 --jump penalty_box |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 93 | * |
| 94 | * * penalty_box handling: |
| 95 | * - only one penalty_box for all interfaces |
| 96 | * E.g Adding an app: |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 97 | * iptables -A penalty_box -m owner --uid-owner app_3 \ |
| 98 | * --jump REJECT --reject-with icmp-net-prohibited |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 99 | */ |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 100 | const char *BandwidthController::IPT_FLUSH_COMMANDS[] = { |
| 101 | /* |
| 102 | * Cleanup rules. |
| 103 | * Should normally include costly_<iface>, but we rely on the way they are setup |
| 104 | * to allow coexistance. |
JP Abgrall | 39f8f24 | 2011-06-29 19:21:58 -0700 | [diff] [blame] | 105 | */ |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 106 | "-F bw_INPUT", |
| 107 | "-F bw_OUTPUT", |
| 108 | "-F bw_FORWARD", |
| 109 | "-F penalty_box", |
| 110 | "-F costly_shared", |
JP Abgrall | f66d6e9 | 2012-04-27 00:22:57 -0700 | [diff] [blame] | 111 | |
| 112 | "-t raw -F bw_raw_PREROUTING", |
| 113 | "-t mangle -F bw_mangle_POSTROUTING", |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /* The cleanup commands assume flushing has been done. */ |
| 117 | const char *BandwidthController::IPT_CLEANUP_COMMANDS[] = { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 118 | "-X penalty_box", |
| 119 | "-X costly_shared", |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 120 | }; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 121 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 122 | const char *BandwidthController::IPT_SETUP_COMMANDS[] = { |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 123 | "-N costly_shared", |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 124 | "-N penalty_box", |
| 125 | }; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 126 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 127 | const char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 128 | "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */ |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 129 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 130 | "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */ |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 131 | |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 132 | "-A costly_shared --jump penalty_box", |
JP Abgrall | f66d6e9 | 2012-04-27 00:22:57 -0700 | [diff] [blame] | 133 | |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 134 | "-t raw -A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */ |
| 135 | "-t mangle -A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */ |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 136 | }; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 137 | |
| 138 | BandwidthController::BandwidthController(void) { |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 139 | } |
| 140 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 141 | int BandwidthController::runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling, |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 142 | IptFailureLog failureHandling) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 143 | int res = 0; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 144 | |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 145 | ALOGV("runIpxtablesCmd(cmd=%s)", cmd); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 146 | res |= runIptablesCmd(cmd, jumpHandling, IptIpV4, failureHandling); |
| 147 | res |= runIptablesCmd(cmd, jumpHandling, IptIpV6, failureHandling); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 148 | return res; |
| 149 | } |
| 150 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 151 | int BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) { |
| 152 | |
| 153 | memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0' |
| 154 | strncpy(buffer, src, buffSize); |
| 155 | return buffer[buffSize - 1]; |
| 156 | } |
| 157 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 158 | int BandwidthController::runIptablesCmd(const char *cmd, IptJumpOp jumpHandling, |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 159 | IptIpVer iptVer, IptFailureLog failureHandling) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 160 | char buffer[MAX_CMD_LEN]; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 161 | const char *argv[MAX_CMD_ARGS]; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 162 | int argc = 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 163 | char *next = buffer; |
| 164 | char *tmp; |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 165 | int res; |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame] | 166 | int status = 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 167 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 168 | std::string fullCmd = cmd; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 169 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 170 | switch (jumpHandling) { |
| 171 | case IptJumpReject: |
JP Abgrall | 340d5cc | 2013-06-28 17:06:00 -0700 | [diff] [blame] | 172 | /* |
| 173 | * Must be carefull what one rejects with, as uper layer protocols will just |
| 174 | * keep on hammering the device until the number of retries are done. |
| 175 | * For port-unreachable (default), TCP should consider as an abort (RFC1122). |
| 176 | */ |
| 177 | fullCmd += " --jump REJECT"; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 178 | break; |
| 179 | case IptJumpReturn: |
| 180 | fullCmd += " --jump RETURN"; |
| 181 | break; |
| 182 | case IptJumpNoAdd: |
| 183 | break; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 184 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 185 | |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 186 | fullCmd.insert(0, " "); |
| 187 | fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 188 | |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame] | 189 | if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) { |
| 190 | ALOGE("iptables command too long"); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | argc = 0; |
| 195 | while ((tmp = strsep(&next, " "))) { |
| 196 | argv[argc++] = tmp; |
| 197 | if (argc >= MAX_CMD_ARGS) { |
| 198 | ALOGE("iptables argument overflow"); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 199 | return -1; |
| 200 | } |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 201 | } |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame] | 202 | |
| 203 | argv[argc] = NULL; |
| 204 | res = android_fork_execvp(argc, (char **)argv, &status, false, |
| 205 | failureHandling == IptFailShow); |
JP Abgrall | c8dc63b | 2013-02-13 16:30:00 -0800 | [diff] [blame] | 206 | res = res || !WIFEXITED(status) || WEXITSTATUS(status); |
| 207 | if (res && failureHandling == IptFailShow) { |
| 208 | ALOGE("runIptablesCmd(): res=%d status=%d failed %s", res, status, |
| 209 | fullCmd.c_str()); |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 210 | } |
| 211 | return res; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 212 | } |
| 213 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 214 | int BandwidthController::setupIptablesHooks(void) { |
| 215 | |
| 216 | /* Some of the initialCommands are allowed to fail */ |
| 217 | runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), |
| 218 | IPT_FLUSH_COMMANDS, RunCmdFailureOk); |
| 219 | |
| 220 | runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*), |
| 221 | IPT_CLEANUP_COMMANDS, RunCmdFailureOk); |
| 222 | |
| 223 | runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*), |
| 224 | IPT_SETUP_COMMANDS, RunCmdFailureBad); |
| 225 | |
| 226 | return 0; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | int BandwidthController::enableBandwidthControl(bool force) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 230 | int res; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 231 | char value[PROPERTY_VALUE_MAX]; |
| 232 | |
| 233 | if (!force) { |
| 234 | property_get("persist.bandwidth.enable", value, "1"); |
| 235 | if (!strcmp(value, "0")) |
| 236 | return 0; |
| 237 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 238 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 239 | /* Let's pretend we started from scratch ... */ |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 240 | sharedQuotaIfaces.clear(); |
| 241 | quotaIfaces.clear(); |
| 242 | naughtyAppUids.clear(); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 243 | globalAlertBytes = 0; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 244 | globalAlertTetherCount = 0; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 245 | sharedQuotaBytes = sharedAlertBytes = 0; |
| 246 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 247 | res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), |
| 248 | IPT_FLUSH_COMMANDS, RunCmdFailureOk); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 249 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 250 | res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*), |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 251 | IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 252 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 253 | return res; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 254 | |
| 255 | } |
| 256 | |
| 257 | int BandwidthController::disableBandwidthControl(void) { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 258 | runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), |
| 259 | IPT_FLUSH_COMMANDS, RunCmdFailureOk); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 260 | return 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 261 | } |
| 262 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 263 | int BandwidthController::runCommands(int numCommands, const char *commands[], |
| 264 | RunCmdErrHandling cmdErrHandling) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 265 | int res = 0; |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 266 | IptFailureLog failureLogging = IptFailShow; |
| 267 | if (cmdErrHandling == RunCmdFailureOk) { |
| 268 | failureLogging = IptFailHide; |
| 269 | } |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 270 | ALOGV("runCommands(): %d commands", numCommands); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 271 | for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) { |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 272 | res = runIpxtablesCmd(commands[cmdNum], IptJumpNoAdd, failureLogging); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 273 | if (res && cmdErrHandling != RunCmdFailureOk) |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 274 | return res; |
| 275 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 276 | return 0; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 277 | } |
| 278 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 279 | std::string BandwidthController::makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 280 | std::string res; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 281 | char *buff; |
| 282 | const char *opFlag; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 283 | |
| 284 | switch (op) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 285 | case IptOpInsert: |
| 286 | opFlag = "-I"; |
| 287 | break; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 288 | case IptOpAppend: |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 289 | ALOGE("Append op not supported for %s uids", chain); |
| 290 | res = ""; |
| 291 | return res; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 292 | break; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 293 | case IptOpReplace: |
| 294 | opFlag = "-R"; |
| 295 | break; |
| 296 | default: |
| 297 | case IptOpDelete: |
| 298 | opFlag = "-D"; |
| 299 | break; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 300 | } |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 301 | asprintf(&buff, "%s %s -m owner --uid-owner %d", opFlag, chain, uid); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 302 | res = buff; |
| 303 | free(buff); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 304 | return res; |
| 305 | } |
| 306 | |
| 307 | int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) { |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 308 | return manipulateNaughtyApps(numUids, appUids, SpecialAppOpAdd); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) { |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 312 | return manipulateNaughtyApps(numUids, appUids, SpecialAppOpRemove); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 313 | } |
| 314 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 315 | int BandwidthController::manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp) { |
| 316 | return manipulateSpecialApps(numUids, appStrUids, "penalty_box", naughtyAppUids, IptJumpReject, appOp); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | int BandwidthController::manipulateSpecialApps(int numUids, char *appStrUids[], |
| 321 | const char *chain, |
| 322 | std::list<int /*appUid*/> &specialAppUids, |
| 323 | IptJumpOp jumpHandling, SpecialAppOp appOp) { |
| 324 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 325 | char cmd[MAX_CMD_LEN]; |
| 326 | int uidNum; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 327 | const char *failLogTemplate; |
| 328 | IptOp op; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 329 | int appUids[numUids]; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 330 | std::string iptCmd; |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 331 | std::list<int /*uid*/>::iterator it; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 332 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 333 | switch (appOp) { |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 334 | case SpecialAppOpAdd: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 335 | op = IptOpInsert; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 336 | failLogTemplate = "Failed to add app uid %d to %s."; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 337 | break; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 338 | case SpecialAppOpRemove: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 339 | op = IptOpDelete; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 340 | failLogTemplate = "Failed to delete app uid %d from %s box."; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 341 | break; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 342 | default: |
| 343 | ALOGE("Unexpected app Op %d", appOp); |
| 344 | return -1; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | for (uidNum = 0; uidNum < numUids; uidNum++) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 348 | appUids[uidNum] = atol(appStrUids[uidNum]); |
| 349 | if (appUids[uidNum] == 0) { |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 350 | ALOGE(failLogTemplate, appUids[uidNum], chain); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 351 | goto fail_parse; |
| 352 | } |
| 353 | } |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 354 | |
| 355 | for (uidNum = 0; uidNum < numUids; uidNum++) { |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 356 | int uid = appUids[uidNum]; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 357 | for (it = specialAppUids.begin(); it != specialAppUids.end(); it++) { |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 358 | if (*it == uid) |
| 359 | break; |
| 360 | } |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 361 | bool found = (it != specialAppUids.end()); |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 362 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 363 | if (appOp == SpecialAppOpRemove) { |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 364 | if (!found) { |
| 365 | ALOGE("No such appUid %d to remove", uid); |
| 366 | return -1; |
| 367 | } |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 368 | specialAppUids.erase(it); |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 369 | } else { |
| 370 | if (found) { |
| 371 | ALOGE("appUid %d exists already", uid); |
| 372 | return -1; |
| 373 | } |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 374 | specialAppUids.push_front(uid); |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 375 | } |
| 376 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 377 | iptCmd = makeIptablesSpecialAppCmd(op, uid, chain); |
| 378 | if (runIpxtablesCmd(iptCmd.c_str(), jumpHandling)) { |
| 379 | ALOGE(failLogTemplate, uid, chain); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 380 | goto fail_with_uidNum; |
| 381 | } |
| 382 | } |
| 383 | return 0; |
| 384 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 385 | fail_with_uidNum: |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 386 | /* Try to remove the uid that failed in any case*/ |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 387 | iptCmd = makeIptablesSpecialAppCmd(IptOpDelete, appUids[uidNum], chain); |
| 388 | runIpxtablesCmd(iptCmd.c_str(), jumpHandling); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 389 | fail_parse: |
| 390 | return -1; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 391 | } |
| 392 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 393 | std::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 394 | std::string res; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 395 | char *buff; |
| 396 | const char *opFlag; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 397 | |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 398 | ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 399 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 400 | switch (op) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 401 | case IptOpInsert: |
| 402 | opFlag = "-I"; |
| 403 | break; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 404 | case IptOpAppend: |
| 405 | opFlag = "-A"; |
| 406 | break; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 407 | case IptOpReplace: |
| 408 | opFlag = "-R"; |
| 409 | break; |
| 410 | default: |
| 411 | case IptOpDelete: |
| 412 | opFlag = "-D"; |
| 413 | break; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 414 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 415 | |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 416 | // The requried IP version specific --jump REJECT ... will be added later. |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 417 | asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota, |
| 418 | costName); |
| 419 | res = buff; |
| 420 | free(buff); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 421 | return res; |
| 422 | } |
| 423 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 424 | int BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 425 | char cmd[MAX_CMD_LEN]; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 426 | int res = 0, res1, res2; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 427 | int ruleInsertPos = 1; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 428 | std::string costString; |
| 429 | const char *costCString; |
| 430 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 431 | /* The "-N costly" is created upfront, no need to handle it here. */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 432 | switch (quotaType) { |
| 433 | case QuotaUnique: |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 434 | costString = "costly_"; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 435 | costString += ifn; |
| 436 | costCString = costString.c_str(); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 437 | /* |
| 438 | * Flush the costly_<iface> is allowed to fail in case it didn't exist. |
| 439 | * Creating a new one is allowed to fail in case it existed. |
| 440 | * This helps with netd restarts. |
| 441 | */ |
| 442 | snprintf(cmd, sizeof(cmd), "-F %s", costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 443 | res1 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 444 | snprintf(cmd, sizeof(cmd), "-N %s", costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 445 | res2 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 446 | res = (res1 && res2) || (!res1 && !res2); |
| 447 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 448 | snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 449 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 450 | break; |
| 451 | case QuotaShared: |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 452 | costCString = "costly_shared"; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 453 | break; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 454 | default: |
| 455 | ALOGE("Unexpected quotatype %d", quotaType); |
| 456 | return -1; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 457 | } |
| 458 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 459 | if (globalAlertBytes) { |
| 460 | /* The alert rule comes 1st */ |
| 461 | ruleInsertPos = 2; |
| 462 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 463 | |
| 464 | snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 465 | runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 466 | |
| 467 | snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 468 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 469 | |
| 470 | snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 471 | runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 472 | |
| 473 | snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 474 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 475 | return res; |
| 476 | } |
| 477 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 478 | int BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 479 | char cmd[MAX_CMD_LEN]; |
| 480 | int res = 0; |
| 481 | std::string costString; |
| 482 | const char *costCString; |
| 483 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 484 | switch (quotaType) { |
| 485 | case QuotaUnique: |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 486 | costString = "costly_"; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 487 | costString += ifn; |
| 488 | costCString = costString.c_str(); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 489 | break; |
| 490 | case QuotaShared: |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 491 | costCString = "costly_shared"; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 492 | break; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 493 | default: |
| 494 | ALOGE("Unexpected quotatype %d", quotaType); |
| 495 | return -1; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 496 | } |
| 497 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 498 | snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 499 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 500 | snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 501 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 502 | |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 503 | /* The "-N costly_shared" is created upfront, no need to handle it here. */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 504 | if (quotaType == QuotaUnique) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 505 | snprintf(cmd, sizeof(cmd), "-F %s", costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 506 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | a9f802c | 2011-06-29 15:46:45 -0700 | [diff] [blame] | 507 | snprintf(cmd, sizeof(cmd), "-X %s", costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 508 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 509 | } |
| 510 | return res; |
| 511 | } |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 512 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 513 | int BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) { |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 514 | char cmd[MAX_CMD_LEN]; |
| 515 | char ifn[MAX_IFACENAME_LEN]; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 516 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 517 | std::string quotaCmd; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 518 | std::string ifaceName; |
| 519 | ; |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 520 | const char *costName = "shared"; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 521 | std::list<std::string>::iterator it; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 522 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 523 | if (!maxBytes) { |
| 524 | /* Don't talk about -1, deprecate it. */ |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 525 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 526 | return -1; |
| 527 | } |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 528 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 529 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 530 | return -1; |
| 531 | } |
| 532 | ifaceName = ifn; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 533 | |
| 534 | if (maxBytes == -1) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 535 | return removeInterfaceSharedQuota(ifn); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* Insert ingress quota. */ |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 539 | for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { |
| 540 | if (*it == ifaceName) |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 541 | break; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 542 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 543 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 544 | if (it == sharedQuotaIfaces.end()) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 545 | res |= prepCostlyIface(ifn, QuotaShared); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 546 | if (sharedQuotaIfaces.empty()) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 547 | quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 548 | res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 549 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 550 | ALOGE("Failed set quota rule"); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 551 | goto fail; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 552 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 553 | sharedQuotaBytes = maxBytes; |
| 554 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 555 | sharedQuotaIfaces.push_front(ifaceName); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 556 | |
| 557 | } |
| 558 | |
| 559 | if (maxBytes != sharedQuotaBytes) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 560 | res |= updateQuota(costName, maxBytes); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 561 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 562 | ALOGE("Failed update quota for %s", costName); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 563 | goto fail; |
| 564 | } |
| 565 | sharedQuotaBytes = maxBytes; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 566 | } |
| 567 | return 0; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 568 | |
| 569 | fail: |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 570 | /* |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 571 | * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse |
| 572 | * rules in the kernel to see which ones need cleaning up. |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 573 | * For now callers needs to choose if they want to "ndc bandwidth enable" |
| 574 | * which resets everything. |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 575 | */ |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 576 | removeInterfaceSharedQuota(ifn); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 577 | return -1; |
| 578 | } |
| 579 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 580 | /* It will also cleanup any shared alerts */ |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 581 | int BandwidthController::removeInterfaceSharedQuota(const char *iface) { |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 582 | char ifn[MAX_IFACENAME_LEN]; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 583 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 584 | std::string ifaceName; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 585 | std::list<std::string>::iterator it; |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 586 | const char *costName = "shared"; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 587 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 588 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 589 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 590 | return -1; |
| 591 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 592 | ifaceName = ifn; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 593 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 594 | for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { |
| 595 | if (*it == ifaceName) |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 596 | break; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 597 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 598 | if (it == sharedQuotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 599 | ALOGE("No such iface %s to delete", ifn); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 600 | return -1; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 601 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 602 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 603 | res |= cleanupCostlyIface(ifn, QuotaShared); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 604 | sharedQuotaIfaces.erase(it); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 605 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 606 | if (sharedQuotaIfaces.empty()) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 607 | std::string quotaCmd; |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 608 | quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 609 | res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 610 | sharedQuotaBytes = 0; |
| 611 | if (sharedAlertBytes) { |
| 612 | removeSharedAlert(); |
| 613 | sharedAlertBytes = 0; |
| 614 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 615 | } |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 616 | return res; |
| 617 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 618 | |
| 619 | int BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) { |
| 620 | char ifn[MAX_IFACENAME_LEN]; |
| 621 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 622 | std::string ifaceName; |
| 623 | const char *costName; |
| 624 | std::list<QuotaInfo>::iterator it; |
| 625 | std::string quotaCmd; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 626 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 627 | if (!maxBytes) { |
| 628 | /* Don't talk about -1, deprecate it. */ |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 629 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 630 | return -1; |
| 631 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 632 | if (maxBytes == -1) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 633 | return removeInterfaceQuota(iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 634 | } |
| 635 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 636 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 637 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 638 | return -1; |
| 639 | } |
| 640 | ifaceName = ifn; |
| 641 | costName = iface; |
| 642 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 643 | /* Insert ingress quota. */ |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 644 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 645 | if (it->ifaceName == ifaceName) |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 646 | break; |
| 647 | } |
| 648 | |
| 649 | if (it == quotaIfaces.end()) { |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 650 | /* Preparing the iface adds a penalty_box check */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 651 | res |= prepCostlyIface(ifn, QuotaUnique); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 652 | /* |
| 653 | * The rejecting quota limit should go after the penalty box checks |
| 654 | * or else a naughty app could just eat up the quota. |
| 655 | * So we append here. |
| 656 | */ |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 657 | quotaCmd = makeIptablesQuotaCmd(IptOpAppend, costName, maxBytes); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 658 | res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 659 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 660 | ALOGE("Failed set quota rule"); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 661 | goto fail; |
| 662 | } |
| 663 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 664 | quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0)); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 665 | |
| 666 | } else { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 667 | res |= updateQuota(costName, maxBytes); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 668 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 669 | ALOGE("Failed update quota for %s", iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 670 | goto fail; |
| 671 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 672 | it->quota = maxBytes; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 673 | } |
| 674 | return 0; |
| 675 | |
| 676 | fail: |
| 677 | /* |
| 678 | * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse |
| 679 | * rules in the kernel to see which ones need cleaning up. |
| 680 | * For now callers needs to choose if they want to "ndc bandwidth enable" |
| 681 | * which resets everything. |
| 682 | */ |
| 683 | removeInterfaceSharedQuota(ifn); |
| 684 | return -1; |
| 685 | } |
| 686 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 687 | int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) { |
| 688 | return getInterfaceQuota("shared", bytes); |
| 689 | } |
| 690 | |
| 691 | int BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) { |
| 692 | FILE *fp; |
| 693 | char *fname; |
| 694 | int scanRes; |
| 695 | |
| 696 | asprintf(&fname, "/proc/net/xt_quota/%s", costName); |
| 697 | fp = fopen(fname, "r"); |
| 698 | free(fname); |
| 699 | if (!fp) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 700 | ALOGE("Reading quota %s failed (%s)", costName, strerror(errno)); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 701 | return -1; |
| 702 | } |
| 703 | scanRes = fscanf(fp, "%lld", bytes); |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 704 | ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 705 | fclose(fp); |
| 706 | return scanRes == 1 ? 0 : -1; |
| 707 | } |
| 708 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 709 | int BandwidthController::removeInterfaceQuota(const char *iface) { |
| 710 | |
| 711 | char ifn[MAX_IFACENAME_LEN]; |
| 712 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 713 | std::string ifaceName; |
| 714 | const char *costName; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 715 | std::list<QuotaInfo>::iterator it; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 716 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 717 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 718 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 719 | return -1; |
| 720 | } |
| 721 | ifaceName = ifn; |
| 722 | costName = iface; |
| 723 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 724 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 725 | if (it->ifaceName == ifaceName) |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 726 | break; |
| 727 | } |
| 728 | |
| 729 | if (it == quotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 730 | ALOGE("No such iface %s to delete", ifn); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 731 | return -1; |
| 732 | } |
| 733 | |
| 734 | /* This also removes the quota command of CostlyIface chain. */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 735 | res |= cleanupCostlyIface(ifn, QuotaUnique); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 736 | |
| 737 | quotaIfaces.erase(it); |
| 738 | |
| 739 | return res; |
| 740 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 741 | |
| 742 | int BandwidthController::updateQuota(const char *quotaName, int64_t bytes) { |
| 743 | FILE *fp; |
| 744 | char *fname; |
| 745 | |
| 746 | asprintf(&fname, "/proc/net/xt_quota/%s", quotaName); |
| 747 | fp = fopen(fname, "w"); |
| 748 | free(fname); |
| 749 | if (!fp) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 750 | ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno)); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 751 | return -1; |
| 752 | } |
| 753 | fprintf(fp, "%lld\n", bytes); |
| 754 | fclose(fp); |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | int BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) { |
| 759 | int res = 0; |
| 760 | const char *opFlag; |
| 761 | char *alertQuotaCmd; |
| 762 | |
| 763 | switch (op) { |
| 764 | case IptOpInsert: |
| 765 | opFlag = "-I"; |
| 766 | break; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 767 | case IptOpAppend: |
| 768 | opFlag = "-A"; |
| 769 | break; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 770 | case IptOpReplace: |
| 771 | opFlag = "-R"; |
| 772 | break; |
| 773 | default: |
| 774 | case IptOpDelete: |
| 775 | opFlag = "-D"; |
| 776 | break; |
| 777 | } |
| 778 | |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 779 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT", |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 780 | bytes, alertName); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 781 | res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 782 | free(alertQuotaCmd); |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 783 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT", |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 784 | bytes, alertName); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 785 | res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 786 | free(alertQuotaCmd); |
| 787 | return res; |
| 788 | } |
| 789 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 790 | int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) { |
| 791 | int res = 0; |
| 792 | const char *opFlag; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 793 | char *alertQuotaCmd; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 794 | |
| 795 | switch (op) { |
| 796 | case IptOpInsert: |
| 797 | opFlag = "-I"; |
| 798 | break; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 799 | case IptOpAppend: |
| 800 | opFlag = "-A"; |
| 801 | break; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 802 | case IptOpReplace: |
| 803 | opFlag = "-R"; |
| 804 | break; |
| 805 | default: |
| 806 | case IptOpDelete: |
| 807 | opFlag = "-D"; |
| 808 | break; |
| 809 | } |
| 810 | |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 811 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD", |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 812 | bytes, alertName); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 813 | res = runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 814 | free(alertQuotaCmd); |
| 815 | return res; |
| 816 | } |
| 817 | |
| 818 | int BandwidthController::setGlobalAlert(int64_t bytes) { |
| 819 | const char *alertName = ALERT_GLOBAL_NAME; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 820 | int res = 0; |
| 821 | |
| 822 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 823 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 824 | return -1; |
| 825 | } |
| 826 | if (globalAlertBytes) { |
| 827 | res = updateQuota(alertName, bytes); |
| 828 | } else { |
| 829 | res = runIptablesAlertCmd(IptOpInsert, alertName, bytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 830 | if (globalAlertTetherCount) { |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 831 | ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 832 | res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes); |
| 833 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 834 | } |
| 835 | globalAlertBytes = bytes; |
| 836 | return res; |
| 837 | } |
| 838 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 839 | int BandwidthController::setGlobalAlertInForwardChain(void) { |
| 840 | const char *alertName = ALERT_GLOBAL_NAME; |
| 841 | int res = 0; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 842 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 843 | globalAlertTetherCount++; |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 844 | ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 845 | |
| 846 | /* |
| 847 | * If there is no globalAlert active we are done. |
| 848 | * If there is an active globalAlert but this is not the 1st |
| 849 | * tether, we are also done. |
| 850 | */ |
| 851 | if (!globalAlertBytes || globalAlertTetherCount != 1) { |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | /* We only add the rule if this was the 1st tether added. */ |
| 856 | res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes); |
| 857 | return res; |
| 858 | } |
| 859 | |
| 860 | int BandwidthController::removeGlobalAlert(void) { |
| 861 | |
| 862 | const char *alertName = ALERT_GLOBAL_NAME; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 863 | int res = 0; |
| 864 | |
| 865 | if (!globalAlertBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 866 | ALOGE("No prior alert set"); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 867 | return -1; |
| 868 | } |
| 869 | res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 870 | if (globalAlertTetherCount) { |
| 871 | res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); |
| 872 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 873 | globalAlertBytes = 0; |
| 874 | return res; |
| 875 | } |
| 876 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 877 | int BandwidthController::removeGlobalAlertInForwardChain(void) { |
| 878 | int res = 0; |
| 879 | const char *alertName = ALERT_GLOBAL_NAME; |
| 880 | |
| 881 | if (!globalAlertTetherCount) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 882 | ALOGE("No prior alert set"); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 883 | return -1; |
| 884 | } |
| 885 | |
| 886 | globalAlertTetherCount--; |
| 887 | /* |
| 888 | * If there is no globalAlert active we are done. |
| 889 | * If there is an active globalAlert but there are more |
| 890 | * tethers, we are also done. |
| 891 | */ |
| 892 | if (!globalAlertBytes || globalAlertTetherCount >= 1) { |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | /* We only detete the rule if this was the last tether removed. */ |
| 897 | res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); |
| 898 | return res; |
| 899 | } |
| 900 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 901 | int BandwidthController::setSharedAlert(int64_t bytes) { |
| 902 | if (!sharedQuotaBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 903 | ALOGE("Need to have a prior shared quota set to set an alert"); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 904 | return -1; |
| 905 | } |
| 906 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 907 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 908 | return -1; |
| 909 | } |
| 910 | return setCostlyAlert("shared", bytes, &sharedAlertBytes); |
| 911 | } |
| 912 | |
| 913 | int BandwidthController::removeSharedAlert(void) { |
| 914 | return removeCostlyAlert("shared", &sharedAlertBytes); |
| 915 | } |
| 916 | |
| 917 | int BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) { |
| 918 | std::list<QuotaInfo>::iterator it; |
| 919 | |
| 920 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 921 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 922 | return -1; |
| 923 | } |
| 924 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
| 925 | if (it->ifaceName == iface) |
| 926 | break; |
| 927 | } |
| 928 | |
| 929 | if (it == quotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 930 | ALOGE("Need to have a prior interface quota set to set an alert"); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 931 | return -1; |
| 932 | } |
| 933 | |
| 934 | return setCostlyAlert(iface, bytes, &it->alert); |
| 935 | } |
| 936 | |
| 937 | int BandwidthController::removeInterfaceAlert(const char *iface) { |
| 938 | std::list<QuotaInfo>::iterator it; |
| 939 | |
| 940 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
| 941 | if (it->ifaceName == iface) |
| 942 | break; |
| 943 | } |
| 944 | |
| 945 | if (it == quotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 946 | ALOGE("No prior alert set for interface %s", iface); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 947 | return -1; |
| 948 | } |
| 949 | |
| 950 | return removeCostlyAlert(iface, &it->alert); |
| 951 | } |
| 952 | |
| 953 | int BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) { |
| 954 | char *alertQuotaCmd; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 955 | char *chainName; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 956 | int res = 0; |
| 957 | char *alertName; |
| 958 | |
| 959 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 960 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 961 | return -1; |
| 962 | } |
| 963 | asprintf(&alertName, "%sAlert", costName); |
| 964 | if (*alertBytes) { |
| 965 | res = updateQuota(alertName, *alertBytes); |
| 966 | } else { |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 967 | asprintf(&chainName, "costly_%s", costName); |
| 968 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 969 | res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 970 | free(alertQuotaCmd); |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 971 | free(chainName); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 972 | } |
| 973 | *alertBytes = bytes; |
| 974 | free(alertName); |
| 975 | return res; |
| 976 | } |
| 977 | |
| 978 | int BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) { |
| 979 | char *alertQuotaCmd; |
| 980 | char *chainName; |
| 981 | char *alertName; |
| 982 | int res = 0; |
| 983 | |
| 984 | asprintf(&alertName, "%sAlert", costName); |
| 985 | if (!*alertBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 986 | ALOGE("No prior alert set for %s alert", costName); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 987 | return -1; |
| 988 | } |
| 989 | |
| 990 | asprintf(&chainName, "costly_%s", costName); |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 991 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame^] | 992 | res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 993 | free(alertQuotaCmd); |
| 994 | free(chainName); |
| 995 | |
| 996 | *alertBytes = 0; |
| 997 | free(alertName); |
| 998 | return res; |
| 999 | } |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1000 | |
| 1001 | /* |
| 1002 | * Parse the ptks and bytes out of: |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1003 | * Chain natctrl_tether_counters (4 references) |
| 1004 | * pkts bytes target prot opt in out source destination |
| 1005 | * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 counter wlan0_rmnet0: 0 bytes |
| 1006 | * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 counter rmnet0_wlan0: 0 bytes |
| 1007 | * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0 counter bt-pan_rmnet0: 0 bytes |
| 1008 | * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0 counter rmnet0_bt-pan: 0 bytes |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1009 | */ |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1010 | int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherStats filter, |
| 1011 | FILE *fp, std::string &extraProcessingInfo) { |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1012 | int res; |
| 1013 | char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN]; |
| 1014 | char iface0[MAX_IPT_OUTPUT_LINE_LEN]; |
| 1015 | char iface1[MAX_IPT_OUTPUT_LINE_LEN]; |
| 1016 | char rest[MAX_IPT_OUTPUT_LINE_LEN]; |
| 1017 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1018 | TetherStats stats; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1019 | char *buffPtr; |
| 1020 | int64_t packets, bytes; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1021 | int statsFound = 0; |
| 1022 | |
| 1023 | bool filterPair = filter.intIface[0] && filter.extIface[0]; |
| 1024 | |
| 1025 | char *filterMsg = filter.getStatsLine(); |
| 1026 | ALOGV("filter: %s", filterMsg); |
| 1027 | free(filterMsg); |
| 1028 | |
| 1029 | stats = filter; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1030 | |
| 1031 | while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) { |
| 1032 | /* Clean up, so a failed parse can still print info */ |
| 1033 | iface0[0] = iface1[0] = rest[0] = packets = bytes = 0; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 1034 | res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s", |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1035 | &packets, &bytes, iface0, iface1, rest); |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 1036 | ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res, |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1037 | iface0, iface1, packets, bytes, rest, buffPtr); |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 1038 | extraProcessingInfo += buffPtr; |
| 1039 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1040 | if (res != 5) { |
| 1041 | continue; |
| 1042 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1043 | /* |
| 1044 | * The following assumes that the 1st rule has in:extIface out:intIface, |
| 1045 | * which is what NatController sets up. |
| 1046 | * If not filtering, the 1st match rx, and sets up the pair for the tx side. |
| 1047 | */ |
| 1048 | if (filter.intIface[0] && filter.extIface[0]) { |
| 1049 | if (filter.intIface == iface0 && filter.extIface == iface1) { |
| 1050 | ALOGV("2Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); |
| 1051 | stats.rxPackets = packets; |
| 1052 | stats.rxBytes = bytes; |
| 1053 | } else if (filter.intIface == iface1 && filter.extIface == iface0) { |
| 1054 | ALOGV("2Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); |
| 1055 | stats.txPackets = packets; |
| 1056 | stats.txBytes = bytes; |
| 1057 | } |
| 1058 | } else if (filter.intIface[0] || filter.extIface[0]) { |
| 1059 | if (filter.intIface == iface0 || filter.extIface == iface1) { |
| 1060 | ALOGV("1Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); |
| 1061 | stats.intIface = iface0; |
| 1062 | stats.extIface = iface1; |
| 1063 | stats.rxPackets = packets; |
| 1064 | stats.rxBytes = bytes; |
| 1065 | } else if (filter.intIface == iface1 || filter.extIface == iface0) { |
| 1066 | ALOGV("1Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); |
| 1067 | stats.intIface = iface1; |
| 1068 | stats.extIface = iface0; |
| 1069 | stats.txPackets = packets; |
| 1070 | stats.txBytes = bytes; |
| 1071 | } |
| 1072 | } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ { |
| 1073 | if (!stats.intIface[0]) { |
| 1074 | ALOGV("0Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); |
| 1075 | stats.intIface = iface0; |
| 1076 | stats.extIface = iface1; |
| 1077 | stats.rxPackets = packets; |
| 1078 | stats.rxBytes = bytes; |
| 1079 | } else if (stats.intIface == iface1 && stats.extIface == iface0) { |
| 1080 | ALOGV("0Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); |
| 1081 | stats.txPackets = packets; |
| 1082 | stats.txBytes = bytes; |
| 1083 | } |
| 1084 | } |
| 1085 | if (stats.rxBytes != -1 && stats.txBytes != -1) { |
| 1086 | ALOGV("rx_bytes=%lld tx_bytes=%lld filterPair=%d", stats.rxBytes, stats.txBytes, filterPair); |
| 1087 | /* Send out stats, and prep for the next if needed. */ |
| 1088 | char *msg = stats.getStatsLine(); |
| 1089 | if (filterPair) { |
| 1090 | cli->sendMsg(ResponseCode::TetheringStatsResult, msg, false); |
| 1091 | return 0; |
| 1092 | } else { |
| 1093 | cli->sendMsg(ResponseCode::TetheringStatsListResult, msg, false); |
| 1094 | stats = filter; |
| 1095 | } |
| 1096 | free(msg); |
| 1097 | statsFound++; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1098 | } |
| 1099 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1100 | /* We found some stats, and the last one isn't a partial stats. */ |
| 1101 | if (statsFound && (stats.rxBytes == -1 || stats.txBytes == -1)) { |
| 1102 | cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false); |
| 1103 | return 0; |
| 1104 | } |
| 1105 | return -1; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1108 | char *BandwidthController::TetherStats::getStatsLine(void) const { |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1109 | char *msg; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1110 | asprintf(&msg, "%s %s %lld %lld %lld %lld", intIface.c_str(), extIface.c_str(), |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1111 | rxBytes, rxPackets, txBytes, txPackets); |
| 1112 | return msg; |
| 1113 | } |
| 1114 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1115 | int BandwidthController::getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo) { |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1116 | int res; |
| 1117 | std::string fullCmd; |
| 1118 | FILE *iptOutput; |
| 1119 | const char *cmd; |
| 1120 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1121 | /* |
| 1122 | * Why not use some kind of lib to talk to iptables? |
| 1123 | * Because the only libs are libiptc and libip6tc in iptables, and they are |
| 1124 | * not easy to use. They require the known iptables match modules to be |
| 1125 | * preloaded/linked, and require apparently a lot of wrapper code to get |
| 1126 | * the wanted info. |
| 1127 | */ |
| 1128 | fullCmd = IPTABLES_PATH; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1129 | fullCmd += " -nvx -L "; |
| 1130 | fullCmd += NatController::LOCAL_TETHER_COUNTERS_CHAIN; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1131 | iptOutput = popen(fullCmd.c_str(), "r"); |
| 1132 | if (!iptOutput) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1133 | ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno)); |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 1134 | extraProcessingInfo += "Failed to run iptables."; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1135 | return -1; |
| 1136 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1137 | res = parseForwardChainStats(cli, stats, iptOutput, extraProcessingInfo); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1138 | pclose(iptOutput); |
| 1139 | |
| 1140 | /* Currently NatController doesn't do ipv6 tethering, so we are done. */ |
| 1141 | return res; |
| 1142 | } |