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