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