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