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