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