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