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