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