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 "NetdConstants.h" |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 56 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 57 | /* Alphabetical */ |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 58 | #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] | 59 | const char BandwidthController::LOCAL_INPUT[] = "bw_INPUT"; |
| 60 | const char BandwidthController::LOCAL_FORWARD[] = "bw_FORWARD"; |
| 61 | const char BandwidthController::LOCAL_OUTPUT[] = "bw_OUTPUT"; |
| 62 | const char BandwidthController::LOCAL_RAW_PREROUTING[] = "bw_raw_PREROUTING"; |
| 63 | const char BandwidthController::LOCAL_MANGLE_POSTROUTING[] = "bw_mangle_POSTROUTING"; |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 64 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 65 | auto BandwidthController::iptablesRestoreFunction = execIptablesRestoreWithOutput; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 66 | |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 67 | using android::base::Join; |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 68 | using android::base::StringAppendF; |
| 69 | using android::base::StringPrintf; |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 70 | using android::netdutils::StatusOr; |
| 71 | using android::netdutils::UniqueFile; |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 72 | |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 73 | namespace { |
| 74 | |
| 75 | const char ALERT_GLOBAL_NAME[] = "globalAlert"; |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 76 | const std::string NEW_CHAIN_COMMAND = "-N "; |
Lorenzo Colitti | ce6748a | 2017-02-02 01:34:33 +0900 | [diff] [blame] | 77 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 78 | const char NAUGHTY_CHAIN[] = "bw_penalty_box"; |
| 79 | const char NICE_CHAIN[] = "bw_happy_box"; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 80 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Some comments about the rules: |
| 83 | * * Ordering |
| 84 | * - 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] | 85 | * E.g. "-I bw_INPUT -i rmnet0 --jump costly" |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 86 | * - 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] | 87 | * - 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] | 88 | * |
| 89 | * * global quota vs per interface quota |
| 90 | * - global quota for all costly interfaces uses a single costly chain: |
| 91 | * . initial rules |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 92 | * iptables -N bw_costly_shared |
| 93 | * iptables -I bw_INPUT -i iface0 --jump bw_costly_shared |
| 94 | * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_shared |
| 95 | * iptables -I bw_costly_shared -m quota \! --quota 500000 \ |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 96 | * --jump REJECT --reject-with icmp-net-prohibited |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 97 | * iptables -A bw_costly_shared --jump bw_penalty_box |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 98 | * iptables -A bw_penalty_box --jump bw_happy_box |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 99 | * iptables -A bw_happy_box --jump bw_data_saver |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 100 | * |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 101 | * . adding a new iface to this, E.g.: |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 102 | * iptables -I bw_INPUT -i iface1 --jump bw_costly_shared |
| 103 | * iptables -I bw_OUTPUT -o iface1 --jump bw_costly_shared |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 104 | * |
| 105 | * - quota per interface. This is achieve by having "costly" chains per quota. |
| 106 | * E.g. adding a new costly interface iface0 with its own quota: |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 107 | * iptables -N bw_costly_iface0 |
| 108 | * iptables -I bw_INPUT -i iface0 --jump bw_costly_iface0 |
| 109 | * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_iface0 |
| 110 | * iptables -A bw_costly_iface0 -m quota \! --quota 500000 \ |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 111 | * --jump REJECT --reject-with icmp-port-unreachable |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 112 | * iptables -A bw_costly_iface0 --jump bw_penalty_box |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 113 | * |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 114 | * * Penalty box, happy box and data saver. |
| 115 | * - bw_penalty box is a blacklist of apps that are rejected. |
| 116 | * - bw_happy_box is a whitelist of apps. It always includes all system apps |
| 117 | * - bw_data_saver implements data usage restrictions. |
| 118 | * - Via the UI the user can add and remove apps from the whitelist and |
| 119 | * blacklist, and turn on/off data saver. |
| 120 | * - The blacklist takes precedence over the whitelist and the whitelist |
| 121 | * takes precedence over data saver. |
| 122 | * |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 123 | * * bw_penalty_box handling: |
| 124 | * - only one bw_penalty_box for all interfaces |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 125 | * E.g Adding an app: |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 126 | * iptables -I bw_penalty_box -m owner --uid-owner app_3 \ |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 127 | * --jump REJECT --reject-with icmp-port-unreachable |
| 128 | * |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 129 | * * bw_happy_box handling: |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 130 | * - The bw_happy_box comes after the penalty box. |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 131 | * E.g Adding a happy app, |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 132 | * iptables -I bw_happy_box -m owner --uid-owner app_3 \ |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 133 | * --jump RETURN |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 134 | * |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 135 | * * bw_data_saver handling: |
| 136 | * - The bw_data_saver comes after the happy box. |
| 137 | * Enable data saver: |
| 138 | * iptables -R 1 bw_data_saver --jump REJECT --reject-with icmp-port-unreachable |
| 139 | * Disable data saver: |
| 140 | * iptables -R 1 bw_data_saver --jump RETURN |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 141 | */ |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 142 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 143 | const std::string COMMIT_AND_CLOSE = "COMMIT\n"; |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 144 | const std::string HAPPY_BOX_WHITELIST_COMMAND = StringPrintf( |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 145 | "-I bw_happy_box -m owner --uid-owner %d-%d --jump RETURN", 0, MAX_SYSTEM_UID); |
| 146 | |
| 147 | static const std::vector<std::string> IPT_FLUSH_COMMANDS = { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 148 | /* |
| 149 | * Cleanup rules. |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 150 | * Should normally include bw_costly_<iface>, but we rely on the way they are setup |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 151 | * to allow coexistance. |
JP Abgrall | 39f8f24 | 2011-06-29 19:21:58 -0700 | [diff] [blame] | 152 | */ |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 153 | "*filter", |
| 154 | ":bw_INPUT -", |
| 155 | ":bw_OUTPUT -", |
| 156 | ":bw_FORWARD -", |
| 157 | ":bw_happy_box -", |
| 158 | ":bw_penalty_box -", |
| 159 | ":bw_data_saver -", |
| 160 | ":bw_costly_shared -", |
| 161 | "COMMIT", |
| 162 | "*raw", |
| 163 | ":bw_raw_PREROUTING -", |
| 164 | "COMMIT", |
| 165 | "*mangle", |
| 166 | ":bw_mangle_POSTROUTING -", |
| 167 | COMMIT_AND_CLOSE |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 170 | static const std::vector<std::string> IPT_BASIC_ACCOUNTING_COMMANDS = { |
| 171 | "*filter", |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 172 | "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */ |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 173 | "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */ |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 174 | "-A bw_costly_shared --jump bw_penalty_box", |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 175 | "-A bw_penalty_box --jump bw_happy_box", |
| 176 | "-A bw_happy_box --jump bw_data_saver", |
| 177 | "-A bw_data_saver -j RETURN", |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 178 | HAPPY_BOX_WHITELIST_COMMAND, |
| 179 | "COMMIT", |
| 180 | |
| 181 | "*raw", |
| 182 | "-A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */ |
| 183 | "COMMIT", |
| 184 | |
| 185 | "*mangle", |
| 186 | "-A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */ |
| 187 | COMMIT_AND_CLOSE |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 188 | }; |
| 189 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 190 | std::vector<std::string> toStrVec(int num, char* strs[]) { |
| 191 | std::vector<std::string> tmp; |
| 192 | for (int i = 0; i < num; ++i) { |
| 193 | tmp.emplace_back(strs[i]); |
| 194 | } |
| 195 | return tmp; |
| 196 | } |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 197 | |
| 198 | } // namespace |
| 199 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 200 | BandwidthController::BandwidthController() { |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 201 | } |
| 202 | |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 203 | void BandwidthController::flushCleanTables(bool doClean) { |
| 204 | /* Flush and remove the bw_costly_<iface> tables */ |
| 205 | flushExistingCostlyTables(doClean); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 206 | |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 207 | std::string commands = Join(IPT_FLUSH_COMMANDS, '\n'); |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 208 | iptablesRestoreFunction(V4V6, commands, nullptr); |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 209 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 210 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 211 | int BandwidthController::setupIptablesHooks() { |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 212 | /* flush+clean is allowed to fail */ |
| 213 | flushCleanTables(true); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 214 | return 0; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | int BandwidthController::enableBandwidthControl(bool force) { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 218 | char value[PROPERTY_VALUE_MAX]; |
| 219 | |
| 220 | if (!force) { |
| 221 | property_get("persist.bandwidth.enable", value, "1"); |
| 222 | if (!strcmp(value, "0")) |
| 223 | return 0; |
| 224 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 225 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 226 | /* Let's pretend we started from scratch ... */ |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 227 | mSharedQuotaIfaces.clear(); |
| 228 | mQuotaIfaces.clear(); |
| 229 | mGlobalAlertBytes = 0; |
| 230 | mGlobalAlertTetherCount = 0; |
| 231 | mSharedQuotaBytes = mSharedAlertBytes = 0; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 232 | |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 233 | flushCleanTables(false); |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 234 | std::string commands = Join(IPT_BASIC_ACCOUNTING_COMMANDS, '\n'); |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 235 | return iptablesRestoreFunction(V4V6, commands, nullptr); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 238 | int BandwidthController::disableBandwidthControl() { |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 239 | |
| 240 | flushCleanTables(false); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 241 | return 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 244 | int BandwidthController::enableDataSaver(bool enable) { |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 245 | std::string cmd = StringPrintf( |
| 246 | "*filter\n" |
| 247 | "-R bw_data_saver 1%s\n" |
| 248 | "COMMIT\n", jumpToString(enable ? IptJumpReject : IptJumpReturn)); |
| 249 | return iptablesRestoreFunction(V4V6, cmd, nullptr); |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 250 | } |
| 251 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 252 | int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 253 | return manipulateSpecialApps(toStrVec(numUids, appUids), NAUGHTY_CHAIN, |
| 254 | IptJumpReject, IptOpInsert); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 258 | return manipulateSpecialApps(toStrVec(numUids, appUids), NAUGHTY_CHAIN, |
| 259 | IptJumpReject, IptOpDelete); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 260 | } |
| 261 | |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 262 | int BandwidthController::addNiceApps(int numUids, char *appUids[]) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 263 | return manipulateSpecialApps(toStrVec(numUids, appUids), NICE_CHAIN, |
| 264 | IptJumpReturn, IptOpInsert); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | int BandwidthController::removeNiceApps(int numUids, char *appUids[]) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 268 | return manipulateSpecialApps(toStrVec(numUids, appUids), NICE_CHAIN, |
| 269 | IptJumpReturn, IptOpDelete); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 272 | int BandwidthController::manipulateSpecialApps(const std::vector<std::string>& appStrUids, |
| 273 | const std::string& chain, IptJumpOp jumpHandling, |
| 274 | IptOp op) { |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 275 | std::string cmd = "*filter\n"; |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 276 | for (const auto& appStrUid : appStrUids) { |
| 277 | StringAppendF(&cmd, "%s %s -m owner --uid-owner %s%s\n", opToString(op), chain.c_str(), |
| 278 | appStrUid.c_str(), jumpToString(jumpHandling)); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 279 | } |
Lorenzo Colitti | 911bc4c | 2017-04-28 14:34:01 +0900 | [diff] [blame] | 280 | StringAppendF(&cmd, "COMMIT\n"); |
| 281 | return iptablesRestoreFunction(V4V6, cmd, nullptr); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 284 | int BandwidthController::setInterfaceSharedQuota(const std::string& iface, int64_t maxBytes) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 285 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 286 | std::string quotaCmd; |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 287 | constexpr char cost[] = "shared"; |
| 288 | constexpr char chain[] = "bw_costly_shared"; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 289 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 290 | if (!maxBytes) { |
| 291 | /* Don't talk about -1, deprecate it. */ |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 292 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 293 | return -1; |
| 294 | } |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 295 | if (!isIfaceName(iface)) |
| 296 | return -1; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 297 | |
| 298 | if (maxBytes == -1) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 299 | return removeInterfaceSharedQuota(iface); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 302 | auto it = mSharedQuotaIfaces.find(iface); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 303 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 304 | if (it == mSharedQuotaIfaces.end()) { |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 305 | const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1; |
| 306 | std::vector<std::string> cmds = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 307 | "*filter", |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 308 | StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(), chain), |
| 309 | StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(), chain), |
Erik Kline | 51eb324 | 2017-09-20 18:30:47 +0900 | [diff] [blame^] | 310 | StringPrintf("-A bw_FORWARD -i %s --jump %s", iface.c_str(), chain), |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 311 | StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain), |
| 312 | }; |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 313 | if (mSharedQuotaIfaces.empty()) { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 314 | cmds.push_back(StringPrintf("-I %s -m quota2 ! --quota %" PRId64 |
| 315 | " --name %s --jump REJECT", |
| 316 | chain, maxBytes, cost)); |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 317 | } |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 318 | cmds.push_back("COMMIT\n"); |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 319 | |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 320 | res |= iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr); |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 321 | if (res) { |
| 322 | ALOGE("Failed set quota rule"); |
| 323 | removeInterfaceSharedQuota(iface); |
| 324 | return -1; |
| 325 | } |
| 326 | mSharedQuotaBytes = maxBytes; |
| 327 | mSharedQuotaIfaces.insert(iface); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 330 | if (maxBytes != mSharedQuotaBytes) { |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 331 | res |= updateQuota(cost, maxBytes); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 332 | if (res) { |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 333 | ALOGE("Failed update quota for %s", cost); |
| 334 | removeInterfaceSharedQuota(iface); |
| 335 | return -1; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 336 | } |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 337 | mSharedQuotaBytes = maxBytes; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 338 | } |
| 339 | return 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 340 | } |
| 341 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 342 | /* It will also cleanup any shared alerts */ |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 343 | int BandwidthController::removeInterfaceSharedQuota(const std::string& iface) { |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 344 | constexpr char cost[] = "shared"; |
| 345 | constexpr char chain[] = "bw_costly_shared"; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 346 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 347 | if (!isIfaceName(iface)) |
| 348 | return -1; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 349 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 350 | auto it = mSharedQuotaIfaces.find(iface); |
| 351 | |
| 352 | if (it == mSharedQuotaIfaces.end()) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 353 | ALOGE("No such iface %s to delete", iface.c_str()); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 354 | return -1; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 355 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 356 | |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 357 | std::vector<std::string> cmds = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 358 | "*filter", |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 359 | StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain), |
| 360 | StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain), |
Erik Kline | 51eb324 | 2017-09-20 18:30:47 +0900 | [diff] [blame^] | 361 | StringPrintf("-D bw_FORWARD -i %s --jump %s", iface.c_str(), chain), |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 362 | StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain), |
| 363 | }; |
Lorenzo Colitti | b7ac3f7 | 2017-07-06 16:52:52 +0900 | [diff] [blame] | 364 | if (mSharedQuotaIfaces.size() == 1) { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 365 | cmds.push_back(StringPrintf("-D %s -m quota2 ! --quota %" PRIu64 |
| 366 | " --name %s --jump REJECT", |
| 367 | chain, mSharedQuotaBytes, cost)); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 368 | } |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 369 | cmds.push_back("COMMIT\n"); |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 370 | |
Lorenzo Colitti | b7ac3f7 | 2017-07-06 16:52:52 +0900 | [diff] [blame] | 371 | if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) { |
| 372 | ALOGE("Failed to remove shared quota on %s", iface.c_str()); |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | int res = 0; |
| 377 | mSharedQuotaIfaces.erase(it); |
| 378 | if (mSharedQuotaIfaces.empty()) { |
| 379 | mSharedQuotaBytes = 0; |
| 380 | if (mSharedAlertBytes) { |
| 381 | res = removeSharedAlert(); |
| 382 | if (res == 0) { |
| 383 | mSharedAlertBytes = 0; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return res; |
| 389 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 390 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 391 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 392 | int BandwidthController::setInterfaceQuota(const std::string& iface, int64_t maxBytes) { |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 393 | const std::string& cost = iface; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 394 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 395 | if (!isIfaceName(iface)) |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 396 | return -1; |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 397 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 398 | if (!maxBytes) { |
| 399 | /* Don't talk about -1, deprecate it. */ |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 400 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 401 | return -1; |
| 402 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 403 | if (maxBytes == -1) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 404 | return removeInterfaceQuota(iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 405 | } |
| 406 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 407 | /* Insert ingress quota. */ |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 408 | auto it = mQuotaIfaces.find(iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 409 | |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 410 | if (it != mQuotaIfaces.end()) { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 411 | if (updateQuota(cost, maxBytes) != 0) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 412 | ALOGE("Failed update quota for %s", iface.c_str()); |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 413 | removeInterfaceQuota(iface); |
| 414 | return -1; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 415 | } |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 416 | it->second.quota = maxBytes; |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 417 | return 0; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 418 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 419 | |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 420 | const std::string chain = "bw_costly_" + iface; |
| 421 | const int ruleInsertPos = (mGlobalAlertBytes) ? 2 : 1; |
| 422 | std::vector<std::string> cmds = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 423 | "*filter", |
| 424 | StringPrintf(":%s -", chain.c_str()), |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 425 | StringPrintf("-A %s -j bw_penalty_box", chain.c_str()), |
| 426 | StringPrintf("-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, iface.c_str(), |
| 427 | chain.c_str()), |
| 428 | StringPrintf("-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, iface.c_str(), |
| 429 | chain.c_str()), |
Erik Kline | 51eb324 | 2017-09-20 18:30:47 +0900 | [diff] [blame^] | 430 | StringPrintf("-A bw_FORWARD -i %s --jump %s", iface.c_str(), chain.c_str()), |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 431 | StringPrintf("-A bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()), |
| 432 | StringPrintf("-A %s -m quota2 ! --quota %" PRId64 " --name %s --jump REJECT", |
| 433 | chain.c_str(), maxBytes, cost.c_str()), |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 434 | "COMMIT\n", |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 435 | }; |
| 436 | |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 437 | if (iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr) != 0) { |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 438 | ALOGE("Failed set quota rule"); |
| 439 | removeInterfaceQuota(iface); |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | mQuotaIfaces[iface] = QuotaInfo{maxBytes, 0}; |
| 444 | return 0; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 445 | } |
| 446 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 447 | int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) { |
| 448 | return getInterfaceQuota("shared", bytes); |
| 449 | } |
| 450 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 451 | int BandwidthController::getInterfaceQuota(const std::string& iface, int64_t* bytes) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 452 | const auto& sys = android::netdutils::sSyscalls.get(); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 453 | const std::string fname = "/proc/net/xt_quota/" + iface; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 454 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 455 | if (!isIfaceName(iface)) return -1; |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 456 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 457 | StatusOr<UniqueFile> file = sys.fopen(fname, "re"); |
| 458 | if (!isOk(file)) { |
| 459 | 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] | 460 | return -1; |
| 461 | } |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 462 | auto rv = sys.fscanf(file.value().get(), "%" SCNd64, bytes); |
| 463 | if (!isOk(rv)) { |
| 464 | ALOGE("Reading quota %s failed (%s)", iface.c_str(), toString(rv).c_str()); |
| 465 | return -1; |
| 466 | } |
| 467 | ALOGV("Read quota res=%d bytes=%" PRId64, rv.value(), *bytes); |
| 468 | return rv.value() == 1 ? 0 : -1; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 471 | int BandwidthController::removeInterfaceQuota(const std::string& iface) { |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 472 | if (!isIfaceName(iface)) |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 473 | return -1; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 474 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 475 | auto it = mQuotaIfaces.find(iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 476 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 477 | if (it == mQuotaIfaces.end()) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 478 | ALOGE("No such iface %s to delete", iface.c_str()); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 479 | return -1; |
| 480 | } |
| 481 | |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 482 | const std::string chain = "bw_costly_" + iface; |
| 483 | std::vector<std::string> cmds = { |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 484 | "*filter", |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 485 | StringPrintf("-D bw_INPUT -i %s --jump %s", iface.c_str(), chain.c_str()), |
| 486 | StringPrintf("-D bw_OUTPUT -o %s --jump %s", iface.c_str(), chain.c_str()), |
Erik Kline | 51eb324 | 2017-09-20 18:30:47 +0900 | [diff] [blame^] | 487 | StringPrintf("-D bw_FORWARD -i %s --jump %s", iface.c_str(), chain.c_str()), |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 488 | StringPrintf("-D bw_FORWARD -o %s --jump %s", iface.c_str(), chain.c_str()), |
| 489 | StringPrintf("-F %s", chain.c_str()), |
| 490 | StringPrintf("-X %s", chain.c_str()), |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 491 | "COMMIT\n", |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 492 | }; |
Lorenzo Colitti | 48f8300 | 2017-07-06 15:06:04 +0900 | [diff] [blame] | 493 | |
| 494 | const int res = iptablesRestoreFunction(V4V6, Join(cmds, "\n"), nullptr); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 495 | |
Lorenzo Colitti | b7ac3f7 | 2017-07-06 16:52:52 +0900 | [diff] [blame] | 496 | if (res == 0) { |
| 497 | mQuotaIfaces.erase(it); |
| 498 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 499 | |
| 500 | return res; |
| 501 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 502 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 503 | int BandwidthController::updateQuota(const std::string& quotaName, int64_t bytes) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 504 | const auto& sys = android::netdutils::sSyscalls.get(); |
| 505 | const std::string fname = "/proc/net/xt_quota/" + quotaName; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 506 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 507 | if (!isIfaceName(quotaName)) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 508 | ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName.c_str()); |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 509 | return -1; |
| 510 | } |
| 511 | |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 512 | StatusOr<UniqueFile> file = sys.fopen(fname, "we"); |
| 513 | if (!isOk(file)) { |
| 514 | 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] | 515 | return -1; |
| 516 | } |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 517 | sys.fprintf(file.value().get(), "%" PRId64 "\n", bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 521 | int BandwidthController::runIptablesAlertCmd(IptOp op, const std::string& alertName, |
| 522 | int64_t bytes) { |
Lorenzo Colitti | d9db08c | 2017-04-28 11:06:40 +0900 | [diff] [blame] | 523 | const char *opFlag = opToString(op); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 524 | std::string alertQuotaCmd = "*filter\n"; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 525 | |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 526 | // TODO: consider using an alternate template for the delete that does not include the --quota |
| 527 | // value. This code works because the --quota value is ignored by deletes |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 528 | StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT", bytes, |
| 529 | alertName.c_str()); |
| 530 | StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT", bytes, |
| 531 | alertName.c_str()); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 532 | StringAppendF(&alertQuotaCmd, "COMMIT\n"); |
| 533 | |
Lorenzo Colitti | 4773cb4 | 2017-04-27 14:03:25 +0900 | [diff] [blame] | 534 | return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 537 | int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, |
| 538 | int64_t bytes) { |
Lorenzo Colitti | d9db08c | 2017-04-28 11:06:40 +0900 | [diff] [blame] | 539 | const char *opFlag = opToString(op); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 540 | std::string alertQuotaCmd = "*filter\n"; |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 541 | StringAppendF(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD", bytes, |
| 542 | alertName.c_str()); |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 543 | StringAppendF(&alertQuotaCmd, "COMMIT\n"); |
| 544 | |
| 545 | return iptablesRestoreFunction(V4V6, alertQuotaCmd, nullptr); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | int BandwidthController::setGlobalAlert(int64_t bytes) { |
| 549 | const char *alertName = ALERT_GLOBAL_NAME; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 550 | int res = 0; |
| 551 | |
| 552 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 553 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 554 | return -1; |
| 555 | } |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 556 | if (mGlobalAlertBytes) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 557 | res = updateQuota(alertName, bytes); |
| 558 | } else { |
| 559 | res = runIptablesAlertCmd(IptOpInsert, alertName, bytes); |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 560 | if (mGlobalAlertTetherCount) { |
| 561 | ALOGV("setGlobalAlert for %d tether", mGlobalAlertTetherCount); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 562 | res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes); |
| 563 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 564 | } |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 565 | mGlobalAlertBytes = bytes; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 566 | return res; |
| 567 | } |
| 568 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 569 | int BandwidthController::setGlobalAlertInForwardChain() { |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 570 | const char *alertName = ALERT_GLOBAL_NAME; |
| 571 | int res = 0; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 572 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 573 | mGlobalAlertTetherCount++; |
| 574 | ALOGV("setGlobalAlertInForwardChain(): %d tether", mGlobalAlertTetherCount); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * If there is no globalAlert active we are done. |
| 578 | * If there is an active globalAlert but this is not the 1st |
| 579 | * tether, we are also done. |
| 580 | */ |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 581 | if (!mGlobalAlertBytes || mGlobalAlertTetherCount != 1) { |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | /* We only add the rule if this was the 1st tether added. */ |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 586 | res = runIptablesAlertFwdCmd(IptOpInsert, alertName, mGlobalAlertBytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 587 | return res; |
| 588 | } |
| 589 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 590 | int BandwidthController::removeGlobalAlert() { |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 591 | |
| 592 | const char *alertName = ALERT_GLOBAL_NAME; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 593 | int res = 0; |
| 594 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 595 | if (!mGlobalAlertBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 596 | ALOGE("No prior alert set"); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 597 | return -1; |
| 598 | } |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 599 | res = runIptablesAlertCmd(IptOpDelete, alertName, mGlobalAlertBytes); |
| 600 | if (mGlobalAlertTetherCount) { |
| 601 | res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 602 | } |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 603 | mGlobalAlertBytes = 0; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 604 | return res; |
| 605 | } |
| 606 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 607 | int BandwidthController::removeGlobalAlertInForwardChain() { |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 608 | int res = 0; |
| 609 | const char *alertName = ALERT_GLOBAL_NAME; |
| 610 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 611 | if (!mGlobalAlertTetherCount) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 612 | ALOGE("No prior alert set"); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 613 | return -1; |
| 614 | } |
| 615 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 616 | mGlobalAlertTetherCount--; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 617 | /* |
| 618 | * If there is no globalAlert active we are done. |
| 619 | * If there is an active globalAlert but there are more |
| 620 | * tethers, we are also done. |
| 621 | */ |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 622 | if (!mGlobalAlertBytes || mGlobalAlertTetherCount >= 1) { |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* We only detete the rule if this was the last tether removed. */ |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 627 | res = runIptablesAlertFwdCmd(IptOpDelete, alertName, mGlobalAlertBytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 628 | return res; |
| 629 | } |
| 630 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 631 | int BandwidthController::setSharedAlert(int64_t bytes) { |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 632 | if (!mSharedQuotaBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 633 | 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] | 634 | return -1; |
| 635 | } |
| 636 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 637 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 638 | return -1; |
| 639 | } |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 640 | return setCostlyAlert("shared", bytes, &mSharedAlertBytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 643 | int BandwidthController::removeSharedAlert() { |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 644 | return removeCostlyAlert("shared", &mSharedAlertBytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 647 | int BandwidthController::setInterfaceAlert(const std::string& iface, int64_t bytes) { |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 648 | if (!isIfaceName(iface)) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 649 | ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface.c_str()); |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 650 | return -1; |
| 651 | } |
| 652 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 653 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 654 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 655 | return -1; |
| 656 | } |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 657 | auto it = mQuotaIfaces.find(iface); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 658 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 659 | if (it == mQuotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 660 | 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] | 661 | return -1; |
| 662 | } |
| 663 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 664 | return setCostlyAlert(iface, bytes, &it->second.alert); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 667 | int BandwidthController::removeInterfaceAlert(const std::string& iface) { |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 668 | if (!isIfaceName(iface)) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 669 | ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface.c_str()); |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 670 | return -1; |
| 671 | } |
| 672 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 673 | auto it = mQuotaIfaces.find(iface); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 674 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 675 | if (it == mQuotaIfaces.end()) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 676 | ALOGE("No prior alert set for interface %s", iface.c_str()); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 677 | return -1; |
| 678 | } |
| 679 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 680 | return removeCostlyAlert(iface, &it->second.alert); |
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::setCostlyAlert(const std::string& costName, int64_t bytes, |
| 684 | int64_t* alertBytes) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 685 | int res = 0; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 686 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 687 | if (!isIfaceName(costName)) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 688 | ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName.c_str()); |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 689 | return -1; |
| 690 | } |
| 691 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 692 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 693 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 694 | return -1; |
| 695 | } |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 696 | |
| 697 | std::string alertName = costName + "Alert"; |
| 698 | std::string chainName = "bw_costly_" + costName; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 699 | if (*alertBytes) { |
| 700 | res = updateQuota(alertName, *alertBytes); |
| 701 | } else { |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 702 | std::vector<std::string> commands = { |
| 703 | "*filter\n", |
| 704 | StringPrintf(ALERT_IPT_TEMPLATE, "-A", chainName.c_str(), bytes, alertName.c_str()), |
| 705 | "COMMIT\n" |
| 706 | }; |
| 707 | res = iptablesRestoreFunction(V4V6, Join(commands, ""), nullptr); |
| 708 | if (res) { |
| 709 | ALOGE("Failed to set costly alert for %s", costName.c_str()); |
| 710 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 711 | } |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 712 | if (res == 0) { |
| 713 | *alertBytes = bytes; |
| 714 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 715 | return res; |
| 716 | } |
| 717 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 718 | int BandwidthController::removeCostlyAlert(const std::string& costName, int64_t* alertBytes) { |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 719 | if (!isIfaceName(costName)) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 720 | ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName.c_str()); |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 721 | return -1; |
| 722 | } |
| 723 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 724 | if (!*alertBytes) { |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 725 | ALOGE("No prior alert set for %s alert", costName.c_str()); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 726 | return -1; |
| 727 | } |
| 728 | |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 729 | std::string alertName = costName + "Alert"; |
| 730 | std::string chainName = "bw_costly_" + costName; |
| 731 | std::vector<std::string> commands = { |
| 732 | "*filter\n", |
| 733 | StringPrintf(ALERT_IPT_TEMPLATE, "-D", chainName.c_str(), *alertBytes, alertName.c_str()), |
| 734 | "COMMIT\n" |
| 735 | }; |
| 736 | if (iptablesRestoreFunction(V4V6, Join(commands, ""), nullptr) != 0) { |
| 737 | ALOGE("Failed to remove costly alert %s", costName.c_str()); |
| 738 | return -1; |
| 739 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 740 | |
| 741 | *alertBytes = 0; |
Lorenzo Colitti | e85ffe1 | 2017-07-06 17:25:37 +0900 | [diff] [blame] | 742 | return 0; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 743 | } |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 744 | |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 745 | void BandwidthController::flushExistingCostlyTables(bool doClean) { |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 746 | std::string fullCmd = "*filter\n-S\nCOMMIT\n"; |
| 747 | std::string ruleList; |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 748 | |
| 749 | /* Only lookup ip4 table names as ip6 will have the same tables ... */ |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 750 | if (int ret = iptablesRestoreFunction(V4, fullCmd, &ruleList)) { |
| 751 | ALOGE("Failed to list existing costly tables ret=%d", ret); |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 752 | return; |
| 753 | } |
| 754 | /* ... then flush/clean both ip4 and ip6 iptables. */ |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 755 | parseAndFlushCostlyTables(ruleList, doClean); |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 758 | void BandwidthController::parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove) { |
| 759 | std::stringstream stream(ruleList); |
| 760 | std::string rule; |
| 761 | std::vector<std::string> clearCommands = { "*filter" }; |
| 762 | std::string chainName; |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 763 | |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 764 | // Find and flush all rules starting with "-N bw_costly_<iface>" except "-N bw_costly_shared". |
| 765 | while (std::getline(stream, rule, '\n')) { |
| 766 | if (rule.find(NEW_CHAIN_COMMAND) != 0) continue; |
| 767 | chainName = rule.substr(NEW_CHAIN_COMMAND.size()); |
| 768 | ALOGV("parse chainName=<%s> orig line=<%s>", chainName.c_str(), rule.c_str()); |
| 769 | |
| 770 | if (chainName.find("bw_costly_") != 0 || chainName == std::string("bw_costly_shared")) { |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 771 | continue; |
| 772 | } |
| 773 | |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 774 | clearCommands.push_back(StringPrintf(":%s -", chainName.c_str())); |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 775 | if (doRemove) { |
Lorenzo Colitti | 3c27270 | 2017-04-26 15:48:13 +0900 | [diff] [blame] | 776 | clearCommands.push_back(StringPrintf("-X %s", chainName.c_str())); |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 777 | } |
| 778 | } |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 779 | |
| 780 | if (clearCommands.size() == 1) { |
| 781 | // No rules found. |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | clearCommands.push_back("COMMIT\n"); |
Joel Scherpelz | d59526a | 2017-06-28 16:24:09 +0900 | [diff] [blame] | 786 | iptablesRestoreFunction(V4V6, Join(clearCommands, '\n'), nullptr); |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 787 | } |
Lorenzo Colitti | d9db08c | 2017-04-28 11:06:40 +0900 | [diff] [blame] | 788 | |
| 789 | inline const char *BandwidthController::opToString(IptOp op) { |
| 790 | switch (op) { |
| 791 | case IptOpInsert: |
| 792 | return "-I"; |
| 793 | case IptOpDelete: |
| 794 | return "-D"; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | inline const char *BandwidthController::jumpToString(IptJumpOp jumpHandling) { |
| 799 | /* |
| 800 | * Must be careful what one rejects with, as upper layer protocols will just |
| 801 | * keep on hammering the device until the number of retries are done. |
| 802 | * For port-unreachable (default), TCP should consider as an abort (RFC1122). |
| 803 | */ |
| 804 | switch (jumpHandling) { |
| 805 | case IptJumpNoAdd: |
| 806 | return ""; |
| 807 | case IptJumpReject: |
| 808 | return " --jump REJECT"; |
| 809 | case IptJumpReturn: |
| 810 | return " --jump RETURN"; |
| 811 | } |
| 812 | } |