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