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