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 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 25 | #include <errno.h> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 26 | #include <fcntl.h> |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 27 | #include <stdio.h> |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 28 | #include <stdlib.h> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 29 | #include <string.h> |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 30 | #include <ctype.h> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 31 | |
Matthew Leach | 2a54d96 | 2013-01-14 15:07:12 +0000 | [diff] [blame] | 32 | #define __STDC_FORMAT_MACROS 1 |
| 33 | #include <inttypes.h> |
| 34 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 35 | #include <sys/socket.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/wait.h> |
| 39 | |
| 40 | #include <linux/netlink.h> |
| 41 | #include <linux/rtnetlink.h> |
| 42 | #include <linux/pkt_sched.h> |
| 43 | |
| 44 | #define LOG_TAG "BandwidthController" |
| 45 | #include <cutils/log.h> |
| 46 | #include <cutils/properties.h> |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame] | 47 | #include <logwrap/logwrap.h> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 48 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 49 | #include "NetdConstants.h" |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 50 | #include "BandwidthController.h" |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 51 | #include "NatController.h" /* For LOCAL_TETHER_COUNTERS_CHAIN */ |
| 52 | #include "ResponseCode.h" |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 53 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 54 | /* Alphabetical */ |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 55 | #define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %" PRId64" --name %s" |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 56 | const char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert"; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 57 | const char* BandwidthController::LOCAL_INPUT = "bw_INPUT"; |
| 58 | const char* BandwidthController::LOCAL_FORWARD = "bw_FORWARD"; |
| 59 | const char* BandwidthController::LOCAL_OUTPUT = "bw_OUTPUT"; |
| 60 | const char* BandwidthController::LOCAL_RAW_PREROUTING = "bw_raw_PREROUTING"; |
| 61 | const char* BandwidthController::LOCAL_MANGLE_POSTROUTING = "bw_mangle_POSTROUTING"; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 62 | const int BandwidthController::MAX_CMD_ARGS = 32; |
| 63 | const int BandwidthController::MAX_CMD_LEN = 1024; |
| 64 | const int BandwidthController::MAX_IFACENAME_LEN = 64; |
| 65 | const int BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256; |
| 66 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Some comments about the rules: |
| 69 | * * Ordering |
| 70 | * - 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] | 71 | * E.g. "-I bw_INPUT -i rmnet0 --jump costly" |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 72 | * - quota'd rules in the costly chain should be before bw_penalty_box lookups. |
| 73 | * - bw_happy_box rejects everything by default. |
JP Abgrall | 29e8de2 | 2012-05-03 12:52:15 -0700 | [diff] [blame] | 74 | * - 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] | 75 | * |
| 76 | * * global quota vs per interface quota |
| 77 | * - global quota for all costly interfaces uses a single costly chain: |
| 78 | * . initial rules |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 79 | * iptables -N bw_costly_shared |
| 80 | * iptables -I bw_INPUT -i iface0 --jump bw_costly_shared |
| 81 | * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_shared |
| 82 | * iptables -I bw_costly_shared -m quota \! --quota 500000 \ |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 83 | * --jump REJECT --reject-with icmp-net-prohibited |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 84 | * iptables -A bw_costly_shared --jump bw_penalty_box |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 85 | * If the happy box is enabled, |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 86 | * iptables -A bw_penalty_box --jump bw_happy_box |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 87 | * |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 88 | * . adding a new iface to this, E.g.: |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 89 | * iptables -I bw_INPUT -i iface1 --jump bw_costly_shared |
| 90 | * iptables -I bw_OUTPUT -o iface1 --jump bw_costly_shared |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 91 | * |
| 92 | * - quota per interface. This is achieve by having "costly" chains per quota. |
| 93 | * E.g. adding a new costly interface iface0 with its own quota: |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 94 | * iptables -N bw_costly_iface0 |
| 95 | * iptables -I bw_INPUT -i iface0 --jump bw_costly_iface0 |
| 96 | * iptables -I bw_OUTPUT -o iface0 --jump bw_costly_iface0 |
| 97 | * iptables -A bw_costly_iface0 -m quota \! --quota 500000 \ |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 98 | * --jump REJECT --reject-with icmp-port-unreachable |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 99 | * iptables -A bw_costly_iface0 --jump bw_penalty_box |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 100 | * |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 101 | * * bw_penalty_box handling: |
| 102 | * - only one bw_penalty_box for all interfaces |
| 103 | * E.g Adding an app, it has to preserve the appened bw_happy_box, so "-I": |
| 104 | * iptables -I bw_penalty_box -m owner --uid-owner app_3 \ |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 105 | * --jump REJECT --reject-with icmp-port-unreachable |
| 106 | * |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 107 | * * bw_happy_box handling: |
| 108 | * - The bw_happy_box goes at the end of the penalty box. |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 109 | * E.g Adding a happy app, |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 110 | * iptables -I bw_happy_box -m owner --uid-owner app_3 \ |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 111 | * --jump RETURN |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 112 | */ |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 113 | const char *BandwidthController::IPT_FLUSH_COMMANDS[] = { |
| 114 | /* |
| 115 | * Cleanup rules. |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 116 | * 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] | 117 | * to allow coexistance. |
JP Abgrall | 39f8f24 | 2011-06-29 19:21:58 -0700 | [diff] [blame] | 118 | */ |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 119 | "-F bw_INPUT", |
| 120 | "-F bw_OUTPUT", |
| 121 | "-F bw_FORWARD", |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 122 | "-F bw_happy_box", |
| 123 | "-F bw_penalty_box", |
| 124 | "-F bw_costly_shared", |
JP Abgrall | f66d6e9 | 2012-04-27 00:22:57 -0700 | [diff] [blame] | 125 | |
| 126 | "-t raw -F bw_raw_PREROUTING", |
| 127 | "-t mangle -F bw_mangle_POSTROUTING", |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /* The cleanup commands assume flushing has been done. */ |
| 131 | const char *BandwidthController::IPT_CLEANUP_COMMANDS[] = { |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 132 | "-X bw_happy_box", |
| 133 | "-X bw_penalty_box", |
| 134 | "-X bw_costly_shared", |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 135 | }; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 136 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 137 | const char *BandwidthController::IPT_SETUP_COMMANDS[] = { |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 138 | "-N bw_happy_box", |
| 139 | "-N bw_penalty_box", |
| 140 | "-N bw_costly_shared", |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 141 | }; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 142 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 143 | const char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 144 | "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */ |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 145 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 146 | "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */ |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 147 | |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 148 | "-A bw_costly_shared --jump bw_penalty_box", |
JP Abgrall | f66d6e9 | 2012-04-27 00:22:57 -0700 | [diff] [blame] | 149 | |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 150 | "-t raw -A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */ |
| 151 | "-t mangle -A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */ |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 152 | }; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 153 | |
| 154 | BandwidthController::BandwidthController(void) { |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 155 | } |
| 156 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 157 | int BandwidthController::runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling, |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 158 | IptFailureLog failureHandling) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 159 | int res = 0; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 160 | |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 161 | ALOGV("runIpxtablesCmd(cmd=%s)", cmd); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 162 | res |= runIptablesCmd(cmd, jumpHandling, IptIpV4, failureHandling); |
| 163 | res |= runIptablesCmd(cmd, jumpHandling, IptIpV6, failureHandling); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 164 | return res; |
| 165 | } |
| 166 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 167 | int BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) { |
| 168 | |
| 169 | memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0' |
| 170 | strncpy(buffer, src, buffSize); |
| 171 | return buffer[buffSize - 1]; |
| 172 | } |
| 173 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 174 | int BandwidthController::runIptablesCmd(const char *cmd, IptJumpOp jumpHandling, |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 175 | IptIpVer iptVer, IptFailureLog failureHandling) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 176 | char buffer[MAX_CMD_LEN]; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 177 | const char *argv[MAX_CMD_ARGS]; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 178 | int argc = 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 179 | char *next = buffer; |
| 180 | char *tmp; |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 181 | int res; |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame] | 182 | int status = 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 183 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 184 | std::string fullCmd = cmd; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 185 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 186 | switch (jumpHandling) { |
| 187 | case IptJumpReject: |
JP Abgrall | 340d5cc | 2013-06-28 17:06:00 -0700 | [diff] [blame] | 188 | /* |
| 189 | * Must be carefull what one rejects with, as uper layer protocols will just |
| 190 | * keep on hammering the device until the number of retries are done. |
| 191 | * For port-unreachable (default), TCP should consider as an abort (RFC1122). |
| 192 | */ |
| 193 | fullCmd += " --jump REJECT"; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 194 | break; |
| 195 | case IptJumpReturn: |
| 196 | fullCmd += " --jump RETURN"; |
| 197 | break; |
| 198 | case IptJumpNoAdd: |
| 199 | break; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 200 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 201 | |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 202 | fullCmd.insert(0, " -w "); |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 203 | fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 204 | |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame] | 205 | if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) { |
| 206 | ALOGE("iptables command too long"); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | argc = 0; |
| 211 | while ((tmp = strsep(&next, " "))) { |
| 212 | argv[argc++] = tmp; |
| 213 | if (argc >= MAX_CMD_ARGS) { |
| 214 | ALOGE("iptables argument overflow"); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 215 | return -1; |
| 216 | } |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 217 | } |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame] | 218 | |
| 219 | argv[argc] = NULL; |
| 220 | res = android_fork_execvp(argc, (char **)argv, &status, false, |
| 221 | failureHandling == IptFailShow); |
JP Abgrall | c8dc63b | 2013-02-13 16:30:00 -0800 | [diff] [blame] | 222 | res = res || !WIFEXITED(status) || WEXITSTATUS(status); |
| 223 | if (res && failureHandling == IptFailShow) { |
| 224 | ALOGE("runIptablesCmd(): res=%d status=%d failed %s", res, status, |
| 225 | fullCmd.c_str()); |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 226 | } |
| 227 | return res; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 228 | } |
| 229 | |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 230 | void BandwidthController::flushCleanTables(bool doClean) { |
| 231 | /* Flush and remove the bw_costly_<iface> tables */ |
| 232 | flushExistingCostlyTables(doClean); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 233 | |
| 234 | /* Some of the initialCommands are allowed to fail */ |
| 235 | runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), |
| 236 | IPT_FLUSH_COMMANDS, RunCmdFailureOk); |
| 237 | |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 238 | if (doClean) { |
| 239 | runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*), |
| 240 | IPT_CLEANUP_COMMANDS, RunCmdFailureOk); |
| 241 | } |
| 242 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 243 | |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 244 | int BandwidthController::setupIptablesHooks(void) { |
| 245 | |
| 246 | /* flush+clean is allowed to fail */ |
| 247 | flushCleanTables(true); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 248 | runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*), |
| 249 | IPT_SETUP_COMMANDS, RunCmdFailureBad); |
| 250 | |
| 251 | return 0; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | int BandwidthController::enableBandwidthControl(bool force) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 255 | int res; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 256 | char value[PROPERTY_VALUE_MAX]; |
| 257 | |
| 258 | if (!force) { |
| 259 | property_get("persist.bandwidth.enable", value, "1"); |
| 260 | if (!strcmp(value, "0")) |
| 261 | return 0; |
| 262 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 263 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 264 | /* Let's pretend we started from scratch ... */ |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 265 | sharedQuotaIfaces.clear(); |
| 266 | quotaIfaces.clear(); |
| 267 | naughtyAppUids.clear(); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 268 | niceAppUids.clear(); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 269 | globalAlertBytes = 0; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 270 | globalAlertTetherCount = 0; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 271 | sharedQuotaBytes = sharedAlertBytes = 0; |
| 272 | |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 273 | flushCleanTables(false); |
| 274 | res = runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*), |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 275 | IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 276 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 277 | return res; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 278 | |
| 279 | } |
| 280 | |
| 281 | int BandwidthController::disableBandwidthControl(void) { |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 282 | |
| 283 | flushCleanTables(false); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 284 | return 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 285 | } |
| 286 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 287 | int BandwidthController::runCommands(int numCommands, const char *commands[], |
| 288 | RunCmdErrHandling cmdErrHandling) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 289 | int res = 0; |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 290 | IptFailureLog failureLogging = IptFailShow; |
| 291 | if (cmdErrHandling == RunCmdFailureOk) { |
| 292 | failureLogging = IptFailHide; |
| 293 | } |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 294 | ALOGV("runCommands(): %d commands", numCommands); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 295 | for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) { |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 296 | res = runIpxtablesCmd(commands[cmdNum], IptJumpNoAdd, failureLogging); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 297 | if (res && cmdErrHandling != RunCmdFailureOk) |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 298 | return res; |
| 299 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 300 | return 0; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 301 | } |
| 302 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 303 | std::string BandwidthController::makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 304 | std::string res; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 305 | char *buff; |
| 306 | const char *opFlag; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 307 | |
| 308 | switch (op) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 309 | case IptOpInsert: |
| 310 | opFlag = "-I"; |
| 311 | break; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 312 | case IptOpAppend: |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 313 | ALOGE("Append op not supported for %s uids", chain); |
| 314 | res = ""; |
| 315 | return res; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 316 | break; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 317 | case IptOpReplace: |
| 318 | opFlag = "-R"; |
| 319 | break; |
| 320 | default: |
| 321 | case IptOpDelete: |
| 322 | opFlag = "-D"; |
| 323 | break; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 324 | } |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 325 | asprintf(&buff, "%s %s -m owner --uid-owner %d", opFlag, chain, uid); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 326 | res = buff; |
| 327 | free(buff); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 328 | return res; |
| 329 | } |
| 330 | |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 331 | int BandwidthController::enableHappyBox(void) { |
| 332 | char cmd[MAX_CMD_LEN]; |
| 333 | int res = 0; |
| 334 | |
| 335 | /* |
| 336 | * We tentatively delete before adding, which helps recovering |
| 337 | * from bad states (e.g. netd died). |
| 338 | */ |
| 339 | |
| 340 | /* Should not exist, but ignore result if already there. */ |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 341 | snprintf(cmd, sizeof(cmd), "-N bw_happy_box"); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 342 | runIpxtablesCmd(cmd, IptJumpNoAdd); |
| 343 | |
| 344 | /* Should be empty, but clear in case something was wrong. */ |
| 345 | niceAppUids.clear(); |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 346 | snprintf(cmd, sizeof(cmd), "-F bw_happy_box"); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 347 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
| 348 | |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 349 | snprintf(cmd, sizeof(cmd), "-D bw_penalty_box -j bw_happy_box"); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 350 | runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 351 | snprintf(cmd, sizeof(cmd), "-A bw_penalty_box -j bw_happy_box"); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 352 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
| 353 | |
Felipe Leme | 5ebbbd8 | 2016-03-07 09:25:50 -0800 | [diff] [blame] | 354 | /* Whitelist all system apps. */ |
| 355 | snprintf(cmd, sizeof(cmd), |
| 356 | "-A bw_happy_box -m owner --uid-owner %d-%d -j RETURN", 0, MAX_SYSTEM_UID); |
| 357 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
| 358 | |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 359 | /* Reject. Defaulting to prot-unreachable */ |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 360 | snprintf(cmd, sizeof(cmd), "-A bw_happy_box -j REJECT"); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 361 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
| 362 | |
| 363 | return res; |
| 364 | } |
| 365 | |
| 366 | int BandwidthController::disableHappyBox(void) { |
| 367 | char cmd[MAX_CMD_LEN]; |
| 368 | |
| 369 | /* Best effort */ |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 370 | snprintf(cmd, sizeof(cmd), "-D bw_penalty_box -j bw_happy_box"); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 371 | runIpxtablesCmd(cmd, IptJumpNoAdd); |
| 372 | niceAppUids.clear(); |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 373 | snprintf(cmd, sizeof(cmd), "-F bw_happy_box"); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 374 | runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 375 | snprintf(cmd, sizeof(cmd), "-X bw_happy_box"); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 376 | runIpxtablesCmd(cmd, IptJumpNoAdd); |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 381 | int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) { |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 382 | return manipulateNaughtyApps(numUids, appUids, SpecialAppOpAdd); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) { |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 386 | return manipulateNaughtyApps(numUids, appUids, SpecialAppOpRemove); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 387 | } |
| 388 | |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 389 | int BandwidthController::addNiceApps(int numUids, char *appUids[]) { |
| 390 | return manipulateNiceApps(numUids, appUids, SpecialAppOpAdd); |
| 391 | } |
| 392 | |
| 393 | int BandwidthController::removeNiceApps(int numUids, char *appUids[]) { |
| 394 | return manipulateNiceApps(numUids, appUids, SpecialAppOpRemove); |
| 395 | } |
| 396 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 397 | int BandwidthController::manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp) { |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 398 | return manipulateSpecialApps(numUids, appStrUids, "bw_penalty_box", naughtyAppUids, IptJumpReject, appOp); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 399 | } |
| 400 | |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 401 | int BandwidthController::manipulateNiceApps(int numUids, char *appStrUids[], SpecialAppOp appOp) { |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 402 | return manipulateSpecialApps(numUids, appStrUids, "bw_happy_box", niceAppUids, IptJumpReturn, appOp); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 403 | } |
| 404 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 405 | |
| 406 | int BandwidthController::manipulateSpecialApps(int numUids, char *appStrUids[], |
| 407 | const char *chain, |
| 408 | std::list<int /*appUid*/> &specialAppUids, |
| 409 | IptJumpOp jumpHandling, SpecialAppOp appOp) { |
| 410 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 411 | int uidNum; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 412 | const char *failLogTemplate; |
| 413 | IptOp op; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 414 | int appUids[numUids]; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 415 | std::string iptCmd; |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 416 | std::list<int /*uid*/>::iterator it; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 417 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 418 | switch (appOp) { |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 419 | case SpecialAppOpAdd: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 420 | op = IptOpInsert; |
JP Abgrall | af476f7 | 2013-07-03 12:23:55 -0700 | [diff] [blame] | 421 | failLogTemplate = "Failed to add app uid %s(%d) to %s."; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 422 | break; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 423 | case SpecialAppOpRemove: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 424 | op = IptOpDelete; |
JP Abgrall | af476f7 | 2013-07-03 12:23:55 -0700 | [diff] [blame] | 425 | failLogTemplate = "Failed to delete app uid %s(%d) from %s box."; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 426 | break; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 427 | default: |
| 428 | ALOGE("Unexpected app Op %d", appOp); |
| 429 | return -1; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | for (uidNum = 0; uidNum < numUids; uidNum++) { |
JP Abgrall | af476f7 | 2013-07-03 12:23:55 -0700 | [diff] [blame] | 433 | char *end; |
| 434 | appUids[uidNum] = strtoul(appStrUids[uidNum], &end, 0); |
| 435 | if (*end || !*appStrUids[uidNum]) { |
| 436 | ALOGE(failLogTemplate, appStrUids[uidNum], appUids[uidNum], chain); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 437 | goto fail_parse; |
| 438 | } |
| 439 | } |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 440 | |
| 441 | for (uidNum = 0; uidNum < numUids; uidNum++) { |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 442 | int uid = appUids[uidNum]; |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 443 | for (it = specialAppUids.begin(); it != specialAppUids.end(); it++) { |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 444 | if (*it == uid) |
| 445 | break; |
| 446 | } |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 447 | bool found = (it != specialAppUids.end()); |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 448 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 449 | if (appOp == SpecialAppOpRemove) { |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 450 | if (!found) { |
| 451 | ALOGE("No such appUid %d to remove", uid); |
| 452 | return -1; |
| 453 | } |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 454 | specialAppUids.erase(it); |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 455 | } else { |
| 456 | if (found) { |
| 457 | ALOGE("appUid %d exists already", uid); |
| 458 | return -1; |
| 459 | } |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 460 | specialAppUids.push_front(uid); |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 461 | } |
| 462 | |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 463 | iptCmd = makeIptablesSpecialAppCmd(op, uid, chain); |
| 464 | if (runIpxtablesCmd(iptCmd.c_str(), jumpHandling)) { |
JP Abgrall | af476f7 | 2013-07-03 12:23:55 -0700 | [diff] [blame] | 465 | ALOGE(failLogTemplate, appStrUids[uidNum], uid, chain); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 466 | goto fail_with_uidNum; |
| 467 | } |
| 468 | } |
| 469 | return 0; |
| 470 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 471 | fail_with_uidNum: |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 472 | /* Try to remove the uid that failed in any case*/ |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 473 | iptCmd = makeIptablesSpecialAppCmd(IptOpDelete, appUids[uidNum], chain); |
| 474 | runIpxtablesCmd(iptCmd.c_str(), jumpHandling); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 475 | fail_parse: |
| 476 | return -1; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 477 | } |
| 478 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 479 | std::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 480 | std::string res; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 481 | char *buff; |
| 482 | const char *opFlag; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 483 | |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 484 | ALOGV("makeIptablesQuotaCmd(%d, %" PRId64")", op, quota); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 485 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 486 | switch (op) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 487 | case IptOpInsert: |
| 488 | opFlag = "-I"; |
| 489 | break; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 490 | case IptOpAppend: |
| 491 | opFlag = "-A"; |
| 492 | break; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 493 | case IptOpReplace: |
| 494 | opFlag = "-R"; |
| 495 | break; |
| 496 | default: |
| 497 | case IptOpDelete: |
| 498 | opFlag = "-D"; |
| 499 | break; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 500 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 501 | |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 502 | // The requried IP version specific --jump REJECT ... will be added later. |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 503 | asprintf(&buff, "%s bw_costly_%s -m quota2 ! --quota %" PRId64" --name %s", opFlag, costName, quota, |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 504 | costName); |
| 505 | res = buff; |
| 506 | free(buff); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 507 | return res; |
| 508 | } |
| 509 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 510 | int BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 511 | char cmd[MAX_CMD_LEN]; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 512 | int res = 0, res1, res2; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 513 | int ruleInsertPos = 1; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 514 | std::string costString; |
| 515 | const char *costCString; |
| 516 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 517 | /* The "-N costly" is created upfront, no need to handle it here. */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 518 | switch (quotaType) { |
| 519 | case QuotaUnique: |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 520 | costString = "bw_costly_"; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 521 | costString += ifn; |
| 522 | costCString = costString.c_str(); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 523 | /* |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 524 | * Flush the bw_costly_<iface> is allowed to fail in case it didn't exist. |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 525 | * Creating a new one is allowed to fail in case it existed. |
| 526 | * This helps with netd restarts. |
| 527 | */ |
| 528 | snprintf(cmd, sizeof(cmd), "-F %s", costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 529 | res1 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 530 | snprintf(cmd, sizeof(cmd), "-N %s", costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 531 | res2 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 532 | res = (res1 && res2) || (!res1 && !res2); |
| 533 | |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 534 | snprintf(cmd, sizeof(cmd), "-A %s -j bw_penalty_box", costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 535 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 536 | break; |
| 537 | case QuotaShared: |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 538 | costCString = "bw_costly_shared"; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 539 | break; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 540 | default: |
| 541 | ALOGE("Unexpected quotatype %d", quotaType); |
| 542 | return -1; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 543 | } |
| 544 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 545 | if (globalAlertBytes) { |
| 546 | /* The alert rule comes 1st */ |
| 547 | ruleInsertPos = 2; |
| 548 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 549 | |
| 550 | snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 551 | runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 552 | |
| 553 | snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 554 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 555 | |
| 556 | snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 557 | runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 558 | |
| 559 | snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 560 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
Erik Kline | 58a9448 | 2015-10-02 17:52:37 +0900 | [diff] [blame] | 561 | |
| 562 | snprintf(cmd, sizeof(cmd), "-D bw_FORWARD -o %s --jump %s", ifn, costCString); |
| 563 | runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
| 564 | snprintf(cmd, sizeof(cmd), "-A bw_FORWARD -o %s --jump %s", ifn, costCString); |
| 565 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
| 566 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 567 | return res; |
| 568 | } |
| 569 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 570 | int BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 571 | char cmd[MAX_CMD_LEN]; |
| 572 | int res = 0; |
| 573 | std::string costString; |
| 574 | const char *costCString; |
| 575 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 576 | switch (quotaType) { |
| 577 | case QuotaUnique: |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 578 | costString = "bw_costly_"; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 579 | costString += ifn; |
| 580 | costCString = costString.c_str(); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 581 | break; |
| 582 | case QuotaShared: |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 583 | costCString = "bw_costly_shared"; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 584 | break; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 585 | default: |
| 586 | ALOGE("Unexpected quotatype %d", quotaType); |
| 587 | return -1; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 588 | } |
| 589 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 590 | snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 591 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
Erik Kline | 58a9448 | 2015-10-02 17:52:37 +0900 | [diff] [blame] | 592 | for (const auto tableName : {LOCAL_OUTPUT, LOCAL_FORWARD}) { |
| 593 | snprintf(cmd, sizeof(cmd), "-D %s -o %s --jump %s", tableName, ifn, costCString); |
| 594 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
| 595 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 596 | |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 597 | /* The "-N bw_costly_shared" is created upfront, no need to handle it here. */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 598 | if (quotaType == QuotaUnique) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 599 | snprintf(cmd, sizeof(cmd), "-F %s", costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 600 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | a9f802c | 2011-06-29 15:46:45 -0700 | [diff] [blame] | 601 | snprintf(cmd, sizeof(cmd), "-X %s", costCString); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 602 | res |= runIpxtablesCmd(cmd, IptJumpNoAdd); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 603 | } |
| 604 | return res; |
| 605 | } |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 606 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 607 | int BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) { |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 608 | char ifn[MAX_IFACENAME_LEN]; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 609 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 610 | std::string quotaCmd; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 611 | std::string ifaceName; |
| 612 | ; |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 613 | const char *costName = "shared"; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 614 | std::list<std::string>::iterator it; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 615 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 616 | if (!maxBytes) { |
| 617 | /* Don't talk about -1, deprecate it. */ |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 618 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 619 | return -1; |
| 620 | } |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 621 | if (!isIfaceName(iface)) |
| 622 | return -1; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 623 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 624 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 625 | return -1; |
| 626 | } |
| 627 | ifaceName = ifn; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 628 | |
| 629 | if (maxBytes == -1) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 630 | return removeInterfaceSharedQuota(ifn); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* Insert ingress quota. */ |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 634 | for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { |
| 635 | if (*it == ifaceName) |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 636 | break; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 637 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 638 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 639 | if (it == sharedQuotaIfaces.end()) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 640 | res |= prepCostlyIface(ifn, QuotaShared); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 641 | if (sharedQuotaIfaces.empty()) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 642 | quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 643 | res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 644 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 645 | ALOGE("Failed set quota rule"); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 646 | goto fail; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 647 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 648 | sharedQuotaBytes = maxBytes; |
| 649 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 650 | sharedQuotaIfaces.push_front(ifaceName); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 651 | |
| 652 | } |
| 653 | |
| 654 | if (maxBytes != sharedQuotaBytes) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 655 | res |= updateQuota(costName, maxBytes); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 656 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 657 | ALOGE("Failed update quota for %s", costName); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 658 | goto fail; |
| 659 | } |
| 660 | sharedQuotaBytes = maxBytes; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 661 | } |
| 662 | return 0; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 663 | |
| 664 | fail: |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 665 | /* |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 666 | * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse |
| 667 | * rules in the kernel to see which ones need cleaning up. |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 668 | * For now callers needs to choose if they want to "ndc bandwidth enable" |
| 669 | * which resets everything. |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 670 | */ |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 671 | removeInterfaceSharedQuota(ifn); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 672 | return -1; |
| 673 | } |
| 674 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 675 | /* It will also cleanup any shared alerts */ |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 676 | int BandwidthController::removeInterfaceSharedQuota(const char *iface) { |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 677 | char ifn[MAX_IFACENAME_LEN]; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 678 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 679 | std::string ifaceName; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 680 | std::list<std::string>::iterator it; |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 681 | const char *costName = "shared"; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 682 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 683 | if (!isIfaceName(iface)) |
| 684 | return -1; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 685 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 686 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 687 | return -1; |
| 688 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 689 | ifaceName = ifn; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 690 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 691 | for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { |
| 692 | if (*it == ifaceName) |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 693 | break; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 694 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 695 | if (it == sharedQuotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 696 | ALOGE("No such iface %s to delete", ifn); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 697 | return -1; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 698 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 699 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 700 | res |= cleanupCostlyIface(ifn, QuotaShared); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 701 | sharedQuotaIfaces.erase(it); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 702 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 703 | if (sharedQuotaIfaces.empty()) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 704 | std::string quotaCmd; |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 705 | quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 706 | res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 707 | sharedQuotaBytes = 0; |
| 708 | if (sharedAlertBytes) { |
| 709 | removeSharedAlert(); |
| 710 | sharedAlertBytes = 0; |
| 711 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 712 | } |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 713 | return res; |
| 714 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 715 | |
| 716 | int BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) { |
| 717 | char ifn[MAX_IFACENAME_LEN]; |
| 718 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 719 | std::string ifaceName; |
| 720 | const char *costName; |
| 721 | std::list<QuotaInfo>::iterator it; |
| 722 | std::string quotaCmd; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 723 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 724 | if (!isIfaceName(iface)) |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 725 | return -1; |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 726 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 727 | if (!maxBytes) { |
| 728 | /* Don't talk about -1, deprecate it. */ |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 729 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 730 | return -1; |
| 731 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 732 | if (maxBytes == -1) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 733 | return removeInterfaceQuota(iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 734 | } |
| 735 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 736 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 737 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 738 | return -1; |
| 739 | } |
| 740 | ifaceName = ifn; |
| 741 | costName = iface; |
| 742 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 743 | /* Insert ingress quota. */ |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 744 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 745 | if (it->ifaceName == ifaceName) |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 746 | break; |
| 747 | } |
| 748 | |
| 749 | if (it == quotaIfaces.end()) { |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 750 | /* Preparing the iface adds a penalty/happy box check */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 751 | res |= prepCostlyIface(ifn, QuotaUnique); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 752 | /* |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 753 | * The rejecting quota limit should go after the penalty/happy box checks |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 754 | * or else a naughty app could just eat up the quota. |
| 755 | * So we append here. |
| 756 | */ |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 757 | quotaCmd = makeIptablesQuotaCmd(IptOpAppend, costName, maxBytes); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 758 | res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 759 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 760 | ALOGE("Failed set quota rule"); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 761 | goto fail; |
| 762 | } |
| 763 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 764 | quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0)); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 765 | |
| 766 | } else { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 767 | res |= updateQuota(costName, maxBytes); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 768 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 769 | ALOGE("Failed update quota for %s", iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 770 | goto fail; |
| 771 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 772 | it->quota = maxBytes; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 773 | } |
| 774 | return 0; |
| 775 | |
| 776 | fail: |
| 777 | /* |
| 778 | * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse |
| 779 | * rules in the kernel to see which ones need cleaning up. |
| 780 | * For now callers needs to choose if they want to "ndc bandwidth enable" |
| 781 | * which resets everything. |
| 782 | */ |
| 783 | removeInterfaceSharedQuota(ifn); |
| 784 | return -1; |
| 785 | } |
| 786 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 787 | int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) { |
| 788 | return getInterfaceQuota("shared", bytes); |
| 789 | } |
| 790 | |
| 791 | int BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) { |
| 792 | FILE *fp; |
| 793 | char *fname; |
| 794 | int scanRes; |
| 795 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 796 | if (!isIfaceName(costName)) |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 797 | return -1; |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 798 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 799 | asprintf(&fname, "/proc/net/xt_quota/%s", costName); |
Nick Kralevich | 53ea9ca | 2015-01-31 13:54:00 -0800 | [diff] [blame] | 800 | fp = fopen(fname, "re"); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 801 | free(fname); |
| 802 | if (!fp) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 803 | ALOGE("Reading quota %s failed (%s)", costName, strerror(errno)); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 804 | return -1; |
| 805 | } |
Mark Salyzyn | ca0b5e2 | 2014-03-26 14:15:03 -0700 | [diff] [blame] | 806 | scanRes = fscanf(fp, "%" SCNd64, bytes); |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 807 | ALOGV("Read quota res=%d bytes=%" PRId64, scanRes, *bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 808 | fclose(fp); |
| 809 | return scanRes == 1 ? 0 : -1; |
| 810 | } |
| 811 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 812 | int BandwidthController::removeInterfaceQuota(const char *iface) { |
| 813 | |
| 814 | char ifn[MAX_IFACENAME_LEN]; |
| 815 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 816 | std::string ifaceName; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 817 | std::list<QuotaInfo>::iterator it; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 818 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 819 | if (!isIfaceName(iface)) |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 820 | return -1; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 821 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 822 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 823 | return -1; |
| 824 | } |
| 825 | ifaceName = ifn; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 826 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 827 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 828 | if (it->ifaceName == ifaceName) |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 829 | break; |
| 830 | } |
| 831 | |
| 832 | if (it == quotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 833 | ALOGE("No such iface %s to delete", ifn); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 834 | return -1; |
| 835 | } |
| 836 | |
| 837 | /* This also removes the quota command of CostlyIface chain. */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 838 | res |= cleanupCostlyIface(ifn, QuotaUnique); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 839 | |
| 840 | quotaIfaces.erase(it); |
| 841 | |
| 842 | return res; |
| 843 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 844 | |
| 845 | int BandwidthController::updateQuota(const char *quotaName, int64_t bytes) { |
| 846 | FILE *fp; |
| 847 | char *fname; |
| 848 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 849 | if (!isIfaceName(quotaName)) { |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 850 | ALOGE("updateQuota: Invalid quotaName \"%s\"", quotaName); |
| 851 | return -1; |
| 852 | } |
| 853 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 854 | asprintf(&fname, "/proc/net/xt_quota/%s", quotaName); |
Nick Kralevich | 53ea9ca | 2015-01-31 13:54:00 -0800 | [diff] [blame] | 855 | fp = fopen(fname, "we"); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 856 | free(fname); |
| 857 | if (!fp) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 858 | ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno)); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 859 | return -1; |
| 860 | } |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 861 | fprintf(fp, "%" PRId64"\n", bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 862 | fclose(fp); |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | int BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) { |
| 867 | int res = 0; |
| 868 | const char *opFlag; |
| 869 | char *alertQuotaCmd; |
| 870 | |
| 871 | switch (op) { |
| 872 | case IptOpInsert: |
| 873 | opFlag = "-I"; |
| 874 | break; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 875 | case IptOpAppend: |
| 876 | opFlag = "-A"; |
| 877 | break; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 878 | case IptOpReplace: |
| 879 | opFlag = "-R"; |
| 880 | break; |
| 881 | default: |
| 882 | case IptOpDelete: |
| 883 | opFlag = "-D"; |
| 884 | break; |
| 885 | } |
| 886 | |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 887 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT", |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 888 | bytes, alertName); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 889 | res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 890 | free(alertQuotaCmd); |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 891 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT", |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 892 | bytes, alertName); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 893 | res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 894 | free(alertQuotaCmd); |
| 895 | return res; |
| 896 | } |
| 897 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 898 | int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) { |
| 899 | int res = 0; |
| 900 | const char *opFlag; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 901 | char *alertQuotaCmd; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 902 | |
| 903 | switch (op) { |
| 904 | case IptOpInsert: |
| 905 | opFlag = "-I"; |
| 906 | break; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 907 | case IptOpAppend: |
| 908 | opFlag = "-A"; |
| 909 | break; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 910 | case IptOpReplace: |
| 911 | opFlag = "-R"; |
| 912 | break; |
| 913 | default: |
| 914 | case IptOpDelete: |
| 915 | opFlag = "-D"; |
| 916 | break; |
| 917 | } |
| 918 | |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 919 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD", |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 920 | bytes, alertName); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 921 | res = runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 922 | free(alertQuotaCmd); |
| 923 | return res; |
| 924 | } |
| 925 | |
| 926 | int BandwidthController::setGlobalAlert(int64_t bytes) { |
| 927 | const char *alertName = ALERT_GLOBAL_NAME; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 928 | int res = 0; |
| 929 | |
| 930 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 931 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 932 | return -1; |
| 933 | } |
| 934 | if (globalAlertBytes) { |
| 935 | res = updateQuota(alertName, bytes); |
| 936 | } else { |
| 937 | res = runIptablesAlertCmd(IptOpInsert, alertName, bytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 938 | if (globalAlertTetherCount) { |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 939 | ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 940 | res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes); |
| 941 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 942 | } |
| 943 | globalAlertBytes = bytes; |
| 944 | return res; |
| 945 | } |
| 946 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 947 | int BandwidthController::setGlobalAlertInForwardChain(void) { |
| 948 | const char *alertName = ALERT_GLOBAL_NAME; |
| 949 | int res = 0; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 950 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 951 | globalAlertTetherCount++; |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 952 | ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 953 | |
| 954 | /* |
| 955 | * If there is no globalAlert active we are done. |
| 956 | * If there is an active globalAlert but this is not the 1st |
| 957 | * tether, we are also done. |
| 958 | */ |
| 959 | if (!globalAlertBytes || globalAlertTetherCount != 1) { |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | /* We only add the rule if this was the 1st tether added. */ |
| 964 | res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes); |
| 965 | return res; |
| 966 | } |
| 967 | |
| 968 | int BandwidthController::removeGlobalAlert(void) { |
| 969 | |
| 970 | const char *alertName = ALERT_GLOBAL_NAME; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 971 | int res = 0; |
| 972 | |
| 973 | if (!globalAlertBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 974 | ALOGE("No prior alert set"); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 975 | return -1; |
| 976 | } |
| 977 | res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 978 | if (globalAlertTetherCount) { |
| 979 | res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); |
| 980 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 981 | globalAlertBytes = 0; |
| 982 | return res; |
| 983 | } |
| 984 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 985 | int BandwidthController::removeGlobalAlertInForwardChain(void) { |
| 986 | int res = 0; |
| 987 | const char *alertName = ALERT_GLOBAL_NAME; |
| 988 | |
| 989 | if (!globalAlertTetherCount) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 990 | ALOGE("No prior alert set"); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 991 | return -1; |
| 992 | } |
| 993 | |
| 994 | globalAlertTetherCount--; |
| 995 | /* |
| 996 | * If there is no globalAlert active we are done. |
| 997 | * If there is an active globalAlert but there are more |
| 998 | * tethers, we are also done. |
| 999 | */ |
| 1000 | if (!globalAlertBytes || globalAlertTetherCount >= 1) { |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | /* We only detete the rule if this was the last tether removed. */ |
| 1005 | res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); |
| 1006 | return res; |
| 1007 | } |
| 1008 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1009 | int BandwidthController::setSharedAlert(int64_t bytes) { |
| 1010 | if (!sharedQuotaBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1011 | 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] | 1012 | return -1; |
| 1013 | } |
| 1014 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1015 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1016 | return -1; |
| 1017 | } |
| 1018 | return setCostlyAlert("shared", bytes, &sharedAlertBytes); |
| 1019 | } |
| 1020 | |
| 1021 | int BandwidthController::removeSharedAlert(void) { |
| 1022 | return removeCostlyAlert("shared", &sharedAlertBytes); |
| 1023 | } |
| 1024 | |
| 1025 | int BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) { |
| 1026 | std::list<QuotaInfo>::iterator it; |
| 1027 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 1028 | if (!isIfaceName(iface)) { |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 1029 | ALOGE("setInterfaceAlert: Invalid iface \"%s\"", iface); |
| 1030 | return -1; |
| 1031 | } |
| 1032 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1033 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1034 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1035 | return -1; |
| 1036 | } |
| 1037 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
| 1038 | if (it->ifaceName == iface) |
| 1039 | break; |
| 1040 | } |
| 1041 | |
| 1042 | if (it == quotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1043 | 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] | 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | return setCostlyAlert(iface, bytes, &it->alert); |
| 1048 | } |
| 1049 | |
| 1050 | int BandwidthController::removeInterfaceAlert(const char *iface) { |
| 1051 | std::list<QuotaInfo>::iterator it; |
| 1052 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 1053 | if (!isIfaceName(iface)) { |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 1054 | ALOGE("removeInterfaceAlert: Invalid iface \"%s\"", iface); |
| 1055 | return -1; |
| 1056 | } |
| 1057 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1058 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
| 1059 | if (it->ifaceName == iface) |
| 1060 | break; |
| 1061 | } |
| 1062 | |
| 1063 | if (it == quotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1064 | ALOGE("No prior alert set for interface %s", iface); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1065 | return -1; |
| 1066 | } |
| 1067 | |
| 1068 | return removeCostlyAlert(iface, &it->alert); |
| 1069 | } |
| 1070 | |
| 1071 | int BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) { |
| 1072 | char *alertQuotaCmd; |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 1073 | char *chainName; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1074 | int res = 0; |
| 1075 | char *alertName; |
| 1076 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 1077 | if (!isIfaceName(costName)) { |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 1078 | ALOGE("setCostlyAlert: Invalid costName \"%s\"", costName); |
| 1079 | return -1; |
| 1080 | } |
| 1081 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1082 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1083 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1084 | return -1; |
| 1085 | } |
| 1086 | asprintf(&alertName, "%sAlert", costName); |
| 1087 | if (*alertBytes) { |
| 1088 | res = updateQuota(alertName, *alertBytes); |
| 1089 | } else { |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 1090 | asprintf(&chainName, "bw_costly_%s", costName); |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 1091 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 1092 | res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1093 | free(alertQuotaCmd); |
JP Abgrall | 109899b | 2013-02-12 19:20:13 -0800 | [diff] [blame] | 1094 | free(chainName); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1095 | } |
| 1096 | *alertBytes = bytes; |
| 1097 | free(alertName); |
| 1098 | return res; |
| 1099 | } |
| 1100 | |
| 1101 | int BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) { |
| 1102 | char *alertQuotaCmd; |
| 1103 | char *chainName; |
| 1104 | char *alertName; |
| 1105 | int res = 0; |
| 1106 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 1107 | if (!isIfaceName(costName)) { |
Nick Kralevich | 0b2b902 | 2014-05-01 13:10:45 -0700 | [diff] [blame] | 1108 | ALOGE("removeCostlyAlert: Invalid costName \"%s\"", costName); |
| 1109 | return -1; |
| 1110 | } |
| 1111 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1112 | if (!*alertBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1113 | ALOGE("No prior alert set for %s alert", costName); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1114 | return -1; |
| 1115 | } |
| 1116 | |
Jesper Hansson | a9d791f | 2012-04-27 13:54:27 +0200 | [diff] [blame] | 1117 | asprintf(&alertName, "%sAlert", costName); |
JP Abgrall | 7e51cde | 2013-07-03 13:33:05 -0700 | [diff] [blame] | 1118 | asprintf(&chainName, "bw_costly_%s", costName); |
JP Abgrall | 92009c8 | 2013-02-06 18:01:24 -0800 | [diff] [blame] | 1119 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName); |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 1120 | res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 1121 | free(alertQuotaCmd); |
| 1122 | free(chainName); |
| 1123 | |
| 1124 | *alertBytes = 0; |
| 1125 | free(alertName); |
| 1126 | return res; |
| 1127 | } |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1128 | |
| 1129 | /* |
| 1130 | * Parse the ptks and bytes out of: |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1131 | * Chain natctrl_tether_counters (4 references) |
| 1132 | * pkts bytes target prot opt in out source destination |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 1133 | * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 1134 | * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 |
| 1135 | * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 1136 | * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0 |
| 1137 | * It results in an error if invoked and no tethering counter rules exist. The constraint |
| 1138 | * helps detect complete parsing failure. |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1139 | */ |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1140 | int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherStats filter, |
| 1141 | FILE *fp, std::string &extraProcessingInfo) { |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1142 | int res; |
| 1143 | char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN]; |
| 1144 | char iface0[MAX_IPT_OUTPUT_LINE_LEN]; |
| 1145 | char iface1[MAX_IPT_OUTPUT_LINE_LEN]; |
| 1146 | char rest[MAX_IPT_OUTPUT_LINE_LEN]; |
| 1147 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1148 | TetherStats stats; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1149 | char *buffPtr; |
| 1150 | int64_t packets, bytes; |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 1151 | int statsFound = 0; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1152 | |
| 1153 | bool filterPair = filter.intIface[0] && filter.extIface[0]; |
| 1154 | |
| 1155 | char *filterMsg = filter.getStatsLine(); |
| 1156 | ALOGV("filter: %s", filterMsg); |
| 1157 | free(filterMsg); |
| 1158 | |
| 1159 | stats = filter; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1160 | |
| 1161 | while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) { |
| 1162 | /* Clean up, so a failed parse can still print info */ |
| 1163 | iface0[0] = iface1[0] = rest[0] = packets = bytes = 0; |
Mark Salyzyn | ca0b5e2 | 2014-03-26 14:15:03 -0700 | [diff] [blame] | 1164 | res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s", |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1165 | &packets, &bytes, iface0, iface1, rest); |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 1166 | ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%" PRId64" bytes=%" PRId64" rest=<%s> orig line=<%s>", res, |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1167 | iface0, iface1, packets, bytes, rest, buffPtr); |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 1168 | extraProcessingInfo += buffPtr; |
| 1169 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1170 | if (res != 5) { |
| 1171 | continue; |
| 1172 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1173 | /* |
| 1174 | * The following assumes that the 1st rule has in:extIface out:intIface, |
| 1175 | * which is what NatController sets up. |
| 1176 | * If not filtering, the 1st match rx, and sets up the pair for the tx side. |
| 1177 | */ |
| 1178 | if (filter.intIface[0] && filter.extIface[0]) { |
| 1179 | if (filter.intIface == iface0 && filter.extIface == iface1) { |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 1180 | ALOGV("2Filter RX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1181 | stats.rxPackets = packets; |
| 1182 | stats.rxBytes = bytes; |
| 1183 | } else if (filter.intIface == iface1 && filter.extIface == iface0) { |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 1184 | ALOGV("2Filter TX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1185 | stats.txPackets = packets; |
| 1186 | stats.txBytes = bytes; |
| 1187 | } |
| 1188 | } else if (filter.intIface[0] || filter.extIface[0]) { |
| 1189 | if (filter.intIface == iface0 || filter.extIface == iface1) { |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 1190 | ALOGV("1Filter RX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1191 | stats.intIface = iface0; |
| 1192 | stats.extIface = iface1; |
| 1193 | stats.rxPackets = packets; |
| 1194 | stats.rxBytes = bytes; |
| 1195 | } else if (filter.intIface == iface1 || filter.extIface == iface0) { |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 1196 | ALOGV("1Filter TX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1197 | stats.intIface = iface1; |
| 1198 | stats.extIface = iface0; |
| 1199 | stats.txPackets = packets; |
| 1200 | stats.txBytes = bytes; |
| 1201 | } |
| 1202 | } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ { |
| 1203 | if (!stats.intIface[0]) { |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 1204 | ALOGV("0Filter RX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1205 | stats.intIface = iface0; |
| 1206 | stats.extIface = iface1; |
| 1207 | stats.rxPackets = packets; |
| 1208 | stats.rxBytes = bytes; |
| 1209 | } else if (stats.intIface == iface1 && stats.extIface == iface0) { |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 1210 | ALOGV("0Filter TX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1211 | stats.txPackets = packets; |
| 1212 | stats.txBytes = bytes; |
| 1213 | } |
| 1214 | } |
| 1215 | if (stats.rxBytes != -1 && stats.txBytes != -1) { |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 1216 | ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64" filterPair=%d", stats.rxBytes, stats.txBytes, filterPair); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1217 | /* Send out stats, and prep for the next if needed. */ |
| 1218 | char *msg = stats.getStatsLine(); |
| 1219 | if (filterPair) { |
| 1220 | cli->sendMsg(ResponseCode::TetheringStatsResult, msg, false); |
| 1221 | return 0; |
| 1222 | } else { |
| 1223 | cli->sendMsg(ResponseCode::TetheringStatsListResult, msg, false); |
| 1224 | stats = filter; |
| 1225 | } |
| 1226 | free(msg); |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 1227 | statsFound++; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1228 | } |
| 1229 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 1230 | |
| 1231 | /* It is always an error to find only one side of the stats. */ |
| 1232 | /* It is an error to find nothing when not filtering. */ |
| 1233 | if (((stats.rxBytes == -1) != (stats.txBytes == -1)) || |
| 1234 | (!statsFound && !filterPair)) { |
| 1235 | return -1; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1236 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 1237 | cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false); |
| 1238 | return 0; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1241 | char *BandwidthController::TetherStats::getStatsLine(void) const { |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1242 | char *msg; |
SynergyDev | 7776cea | 2014-03-16 15:48:51 -0700 | [diff] [blame] | 1243 | asprintf(&msg, "%s %s %" PRId64" %" PRId64" %" PRId64" %" PRId64, intIface.c_str(), extIface.c_str(), |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1244 | rxBytes, rxPackets, txBytes, txPackets); |
| 1245 | return msg; |
| 1246 | } |
| 1247 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1248 | int BandwidthController::getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo) { |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1249 | int res; |
| 1250 | std::string fullCmd; |
| 1251 | FILE *iptOutput; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1252 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1253 | /* |
| 1254 | * Why not use some kind of lib to talk to iptables? |
| 1255 | * Because the only libs are libiptc and libip6tc in iptables, and they are |
| 1256 | * not easy to use. They require the known iptables match modules to be |
| 1257 | * preloaded/linked, and require apparently a lot of wrapper code to get |
| 1258 | * the wanted info. |
| 1259 | */ |
| 1260 | fullCmd = IPTABLES_PATH; |
Yusuke Sato | 99b4050 | 2015-08-19 13:47:30 -0700 | [diff] [blame] | 1261 | fullCmd += " -nvx -w -L "; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1262 | fullCmd += NatController::LOCAL_TETHER_COUNTERS_CHAIN; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1263 | iptOutput = popen(fullCmd.c_str(), "r"); |
| 1264 | if (!iptOutput) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1265 | ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno)); |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 1266 | extraProcessingInfo += "Failed to run iptables."; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1267 | return -1; |
| 1268 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 1269 | res = parseForwardChainStats(cli, stats, iptOutput, extraProcessingInfo); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1270 | pclose(iptOutput); |
| 1271 | |
| 1272 | /* Currently NatController doesn't do ipv6 tethering, so we are done. */ |
| 1273 | return res; |
| 1274 | } |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 1275 | |
| 1276 | void BandwidthController::flushExistingCostlyTables(bool doClean) { |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 1277 | std::string fullCmd; |
| 1278 | FILE *iptOutput; |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 1279 | |
| 1280 | /* Only lookup ip4 table names as ip6 will have the same tables ... */ |
| 1281 | fullCmd = IPTABLES_PATH; |
Yusuke Sato | 99b4050 | 2015-08-19 13:47:30 -0700 | [diff] [blame] | 1282 | fullCmd += " -w -S"; |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 1283 | iptOutput = popen(fullCmd.c_str(), "r"); |
| 1284 | if (!iptOutput) { |
| 1285 | ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno)); |
| 1286 | return; |
| 1287 | } |
| 1288 | /* ... then flush/clean both ip4 and ip6 iptables. */ |
| 1289 | parseAndFlushCostlyTables(iptOutput, doClean); |
| 1290 | pclose(iptOutput); |
| 1291 | } |
| 1292 | |
| 1293 | void BandwidthController::parseAndFlushCostlyTables(FILE *fp, bool doRemove) { |
| 1294 | int res; |
| 1295 | char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN]; |
| 1296 | char costlyIfaceName[MAX_IPT_OUTPUT_LINE_LEN]; |
| 1297 | char cmd[MAX_CMD_LEN]; |
| 1298 | char *buffPtr; |
| 1299 | |
| 1300 | while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) { |
| 1301 | costlyIfaceName[0] = '\0'; /* So that debugging output always works */ |
| 1302 | res = sscanf(buffPtr, "-N bw_costly_%s", costlyIfaceName); |
| 1303 | ALOGV("parse res=%d costly=<%s> orig line=<%s>", res, |
| 1304 | costlyIfaceName, buffPtr); |
| 1305 | if (res != 1) { |
| 1306 | continue; |
| 1307 | } |
| 1308 | /* Exclusions: "shared" is not an ifacename */ |
| 1309 | if (!strcmp(costlyIfaceName, "shared")) { |
| 1310 | continue; |
| 1311 | } |
| 1312 | |
| 1313 | snprintf(cmd, sizeof(cmd), "-F bw_costly_%s", costlyIfaceName); |
| 1314 | runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
| 1315 | if (doRemove) { |
| 1316 | snprintf(cmd, sizeof(cmd), "-X bw_costly_%s", costlyIfaceName); |
| 1317 | runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide); |
| 1318 | } |
| 1319 | } |
| 1320 | } |