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