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 | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
| 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> |
| 30 | |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/wait.h> |
| 35 | |
| 36 | #include <linux/netlink.h> |
| 37 | #include <linux/rtnetlink.h> |
| 38 | #include <linux/pkt_sched.h> |
| 39 | |
| 40 | #define LOG_TAG "BandwidthController" |
| 41 | #include <cutils/log.h> |
| 42 | #include <cutils/properties.h> |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame^] | 43 | #include <logwrap/logwrap.h> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 44 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 45 | #include "NetdConstants.h" |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 46 | #include "BandwidthController.h" |
| 47 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 48 | /* Alphabetical */ |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 49 | #define ALERT_IPT_TEMPLATE "%s %s %s -m quota2 ! --quota %lld --name %s" |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 50 | const int BandwidthController::ALERT_RULE_POS_IN_COSTLY_CHAIN = 4; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 51 | const char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert"; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 52 | const char* BandwidthController::LOCAL_INPUT = "bw_INPUT"; |
| 53 | const char* BandwidthController::LOCAL_FORWARD = "bw_FORWARD"; |
| 54 | const char* BandwidthController::LOCAL_OUTPUT = "bw_OUTPUT"; |
| 55 | const char* BandwidthController::LOCAL_RAW_PREROUTING = "bw_raw_PREROUTING"; |
| 56 | const char* BandwidthController::LOCAL_MANGLE_POSTROUTING = "bw_mangle_POSTROUTING"; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 57 | const int BandwidthController::MAX_CMD_ARGS = 32; |
| 58 | const int BandwidthController::MAX_CMD_LEN = 1024; |
| 59 | const int BandwidthController::MAX_IFACENAME_LEN = 64; |
| 60 | const int BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256; |
| 61 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Some comments about the rules: |
| 64 | * * Ordering |
| 65 | * - 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] | 66 | * E.g. "-I bw_INPUT -i rmnet0 --jump costly" |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 67 | * - quota'd rules in the costly chain should be before penalty_box lookups. |
JP Abgrall | 29e8de2 | 2012-05-03 12:52:15 -0700 | [diff] [blame] | 68 | * - 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] | 69 | * |
| 70 | * * global quota vs per interface quota |
| 71 | * - global quota for all costly interfaces uses a single costly chain: |
| 72 | * . initial rules |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 73 | * iptables -N costly_shared |
JP Abgrall | 29e8de2 | 2012-05-03 12:52:15 -0700 | [diff] [blame] | 74 | * iptables -I bw_INPUT -i iface0 --jump costly_shared |
| 75 | * iptables -I bw_OUTPUT -o iface0 --jump costly_shared |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 76 | * iptables -I costly_shared -m quota \! --quota 500000 \ |
| 77 | * --jump REJECT --reject-with icmp-net-prohibited |
| 78 | * iptables -A costly_shared --jump penalty_box |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 79 | * |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 80 | * . adding a new iface to this, E.g.: |
JP Abgrall | 29e8de2 | 2012-05-03 12:52:15 -0700 | [diff] [blame] | 81 | * iptables -I bw_INPUT -i iface1 --jump costly_shared |
| 82 | * iptables -I bw_OUTPUT -o iface1 --jump costly_shared |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 83 | * |
| 84 | * - quota per interface. This is achieve by having "costly" chains per quota. |
| 85 | * E.g. adding a new costly interface iface0 with its own quota: |
| 86 | * iptables -N costly_iface0 |
JP Abgrall | 29e8de2 | 2012-05-03 12:52:15 -0700 | [diff] [blame] | 87 | * iptables -I bw_INPUT -i iface0 --jump costly_iface0 |
| 88 | * iptables -I bw_OUTPUT -o iface0 --jump costly_iface0 |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 89 | * iptables -A costly_iface0 -m quota \! --quota 500000 \ |
| 90 | * --jump REJECT --reject-with icmp-net-prohibited |
| 91 | * iptables -A costly_iface0 --jump penalty_box |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 92 | * |
| 93 | * * penalty_box handling: |
| 94 | * - only one penalty_box for all interfaces |
| 95 | * E.g Adding an app: |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 96 | * iptables -A penalty_box -m owner --uid-owner app_3 \ |
| 97 | * --jump REJECT --reject-with icmp-net-prohibited |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 98 | */ |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 99 | const char *BandwidthController::IPT_FLUSH_COMMANDS[] = { |
| 100 | /* |
| 101 | * Cleanup rules. |
| 102 | * Should normally include costly_<iface>, but we rely on the way they are setup |
| 103 | * to allow coexistance. |
JP Abgrall | 39f8f24 | 2011-06-29 19:21:58 -0700 | [diff] [blame] | 104 | */ |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 105 | "-F bw_INPUT", |
| 106 | "-F bw_OUTPUT", |
| 107 | "-F bw_FORWARD", |
| 108 | "-F penalty_box", |
| 109 | "-F costly_shared", |
JP Abgrall | f66d6e9 | 2012-04-27 00:22:57 -0700 | [diff] [blame] | 110 | |
| 111 | "-t raw -F bw_raw_PREROUTING", |
| 112 | "-t mangle -F bw_mangle_POSTROUTING", |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /* The cleanup commands assume flushing has been done. */ |
| 116 | const char *BandwidthController::IPT_CLEANUP_COMMANDS[] = { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 117 | "-X penalty_box", |
| 118 | "-X costly_shared", |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 119 | }; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 120 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 121 | const char *BandwidthController::IPT_SETUP_COMMANDS[] = { |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 122 | "-N costly_shared", |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 123 | "-N penalty_box", |
| 124 | }; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 125 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 126 | const char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 127 | "-A bw_INPUT -i lo --jump RETURN", |
| 128 | "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */ |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 129 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 130 | "-A bw_OUTPUT -o lo --jump RETURN", |
| 131 | "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */ |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 132 | |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 133 | "-A costly_shared --jump penalty_box", |
JP Abgrall | f66d6e9 | 2012-04-27 00:22:57 -0700 | [diff] [blame] | 134 | |
| 135 | "-t raw -A bw_raw_PREROUTING ! -i lo+ -m owner --socket-exists", /* This is a tracking rule. */ |
| 136 | "-t mangle -A bw_mangle_POSTROUTING ! -o lo+ -m owner --socket-exists", /* This is a tracking rule. */ |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 137 | }; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 138 | |
| 139 | BandwidthController::BandwidthController(void) { |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 140 | } |
| 141 | |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 142 | int BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling, |
| 143 | IptFailureLog failureHandling) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 144 | int res = 0; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 145 | |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 146 | ALOGV("runIpxtablesCmd(cmd=%s)", cmd); |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 147 | res |= runIptablesCmd(cmd, rejectHandling, IptIpV4, failureHandling); |
| 148 | res |= runIptablesCmd(cmd, rejectHandling, IptIpV6, failureHandling); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 149 | return res; |
| 150 | } |
| 151 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 152 | int BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) { |
| 153 | |
| 154 | memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0' |
| 155 | strncpy(buffer, src, buffSize); |
| 156 | return buffer[buffSize - 1]; |
| 157 | } |
| 158 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 159 | int BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 160 | IptIpVer iptVer, IptFailureLog failureHandling) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 161 | char buffer[MAX_CMD_LEN]; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 162 | const char *argv[MAX_CMD_ARGS]; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 163 | int argc = 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 164 | char *next = buffer; |
| 165 | char *tmp; |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 166 | int res; |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame^] | 167 | int status = 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 168 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 169 | std::string fullCmd = cmd; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 170 | |
| 171 | if (rejectHandling == IptRejectAdd) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 172 | fullCmd += " --jump REJECT --reject-with"; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 173 | switch (iptVer) { |
| 174 | case IptIpV4: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 175 | fullCmd += " icmp-net-prohibited"; |
| 176 | break; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 177 | case IptIpV6: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 178 | fullCmd += " icmp6-adm-prohibited"; |
| 179 | break; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 180 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 181 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 182 | |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 183 | fullCmd.insert(0, " "); |
| 184 | fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 185 | |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame^] | 186 | if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) { |
| 187 | ALOGE("iptables command too long"); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | argc = 0; |
| 192 | while ((tmp = strsep(&next, " "))) { |
| 193 | argv[argc++] = tmp; |
| 194 | if (argc >= MAX_CMD_ARGS) { |
| 195 | ALOGE("iptables argument overflow"); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 196 | return -1; |
| 197 | } |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 198 | } |
Rom Lemarchand | 1415021 | 2013-01-24 10:01:04 -0800 | [diff] [blame^] | 199 | |
| 200 | argv[argc] = NULL; |
| 201 | res = android_fork_execvp(argc, (char **)argv, &status, false, |
| 202 | failureHandling == IptFailShow); |
| 203 | |
| 204 | if ((res || !WIFEXITED(status) || WEXITSTATUS(status)) && |
| 205 | failureHandling == IptFailShow) { |
| 206 | ALOGE("runIptablesCmd(): failed %s res=%d status=%d", fullCmd.c_str(), |
| 207 | res, status); |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 208 | } |
| 209 | return res; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 210 | } |
| 211 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 212 | int BandwidthController::setupIptablesHooks(void) { |
| 213 | |
| 214 | /* Some of the initialCommands are allowed to fail */ |
| 215 | runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), |
| 216 | IPT_FLUSH_COMMANDS, RunCmdFailureOk); |
| 217 | |
| 218 | runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*), |
| 219 | IPT_CLEANUP_COMMANDS, RunCmdFailureOk); |
| 220 | |
| 221 | runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*), |
| 222 | IPT_SETUP_COMMANDS, RunCmdFailureBad); |
| 223 | |
| 224 | return 0; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | int BandwidthController::enableBandwidthControl(bool force) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 228 | int res; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 229 | char value[PROPERTY_VALUE_MAX]; |
| 230 | |
| 231 | if (!force) { |
| 232 | property_get("persist.bandwidth.enable", value, "1"); |
| 233 | if (!strcmp(value, "0")) |
| 234 | return 0; |
| 235 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 236 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 237 | /* Let's pretend we started from scratch ... */ |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 238 | sharedQuotaIfaces.clear(); |
| 239 | quotaIfaces.clear(); |
| 240 | naughtyAppUids.clear(); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 241 | globalAlertBytes = 0; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 242 | globalAlertTetherCount = 0; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 243 | sharedQuotaBytes = sharedAlertBytes = 0; |
| 244 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 245 | res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), |
| 246 | IPT_FLUSH_COMMANDS, RunCmdFailureOk); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 247 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 248 | res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*), |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 249 | IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 250 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 251 | return res; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 252 | |
| 253 | } |
| 254 | |
| 255 | int BandwidthController::disableBandwidthControl(void) { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 256 | runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), |
| 257 | IPT_FLUSH_COMMANDS, RunCmdFailureOk); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 258 | return 0; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 259 | } |
| 260 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 261 | int BandwidthController::runCommands(int numCommands, const char *commands[], |
| 262 | RunCmdErrHandling cmdErrHandling) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 263 | int res = 0; |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 264 | IptFailureLog failureLogging = IptFailShow; |
| 265 | if (cmdErrHandling == RunCmdFailureOk) { |
| 266 | failureLogging = IptFailHide; |
| 267 | } |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 268 | ALOGV("runCommands(): %d commands", numCommands); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 269 | for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) { |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 270 | res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd, failureLogging); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 271 | if (res && cmdErrHandling != RunCmdFailureOk) |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 272 | return res; |
| 273 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 274 | return 0; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 275 | } |
| 276 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 277 | std::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 278 | std::string res; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 279 | char *buff; |
| 280 | const char *opFlag; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 281 | |
| 282 | switch (op) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 283 | case IptOpInsert: |
| 284 | opFlag = "-I"; |
| 285 | break; |
| 286 | case IptOpReplace: |
| 287 | opFlag = "-R"; |
| 288 | break; |
| 289 | default: |
| 290 | case IptOpDelete: |
| 291 | opFlag = "-D"; |
| 292 | break; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 293 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 294 | asprintf(&buff, "%s penalty_box -m owner --uid-owner %d", opFlag, uid); |
| 295 | res = buff; |
| 296 | free(buff); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 297 | return res; |
| 298 | } |
| 299 | |
| 300 | int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 301 | return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 305 | return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 306 | } |
| 307 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 308 | int BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 309 | char cmd[MAX_CMD_LEN]; |
| 310 | int uidNum; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 311 | const char *failLogTemplate; |
| 312 | IptOp op; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 313 | int appUids[numUids]; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 314 | std::string naughtyCmd; |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 315 | std::list<int /*uid*/>::iterator it; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 316 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 317 | switch (appOp) { |
| 318 | case NaughtyAppOpAdd: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 319 | op = IptOpInsert; |
| 320 | failLogTemplate = "Failed to add app uid %d to penalty box."; |
| 321 | break; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 322 | case NaughtyAppOpRemove: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 323 | op = IptOpDelete; |
| 324 | failLogTemplate = "Failed to delete app uid %d from penalty box."; |
| 325 | break; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 326 | default: |
| 327 | ALOGE("Unexpected app Op %d", appOp); |
| 328 | return -1; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | for (uidNum = 0; uidNum < numUids; uidNum++) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 332 | appUids[uidNum] = atol(appStrUids[uidNum]); |
| 333 | if (appUids[uidNum] == 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 334 | ALOGE(failLogTemplate, appUids[uidNum]); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 335 | goto fail_parse; |
| 336 | } |
| 337 | } |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 338 | |
| 339 | for (uidNum = 0; uidNum < numUids; uidNum++) { |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 340 | int uid = appUids[uidNum]; |
| 341 | for (it = naughtyAppUids.begin(); it != naughtyAppUids.end(); it++) { |
| 342 | if (*it == uid) |
| 343 | break; |
| 344 | } |
| 345 | bool found = (it != naughtyAppUids.end()); |
| 346 | |
| 347 | if (appOp == NaughtyAppOpRemove) { |
| 348 | if (!found) { |
| 349 | ALOGE("No such appUid %d to remove", uid); |
| 350 | return -1; |
| 351 | } |
| 352 | naughtyAppUids.erase(it); |
| 353 | } else { |
| 354 | if (found) { |
| 355 | ALOGE("appUid %d exists already", uid); |
| 356 | return -1; |
| 357 | } |
| 358 | naughtyAppUids.push_front(uid); |
| 359 | } |
| 360 | |
| 361 | naughtyCmd = makeIptablesNaughtyCmd(op, uid); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 362 | if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) { |
JP Abgrall | b1d2409 | 2012-04-27 01:02:31 -0700 | [diff] [blame] | 363 | ALOGE(failLogTemplate, uid); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 364 | goto fail_with_uidNum; |
| 365 | } |
| 366 | } |
| 367 | return 0; |
| 368 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 369 | fail_with_uidNum: |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 370 | /* Try to remove the uid that failed in any case*/ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 371 | naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]); |
| 372 | runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd); |
| 373 | fail_parse: |
| 374 | return -1; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 375 | } |
| 376 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 377 | std::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 378 | std::string res; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 379 | char *buff; |
| 380 | const char *opFlag; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 381 | |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 382 | ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 383 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 384 | switch (op) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 385 | case IptOpInsert: |
| 386 | opFlag = "-I"; |
| 387 | break; |
| 388 | case IptOpReplace: |
| 389 | opFlag = "-R"; |
| 390 | break; |
| 391 | default: |
| 392 | case IptOpDelete: |
| 393 | opFlag = "-D"; |
| 394 | break; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 395 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 396 | |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 397 | // The requried IP version specific --jump REJECT ... will be added later. |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 398 | asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota, |
| 399 | costName); |
| 400 | res = buff; |
| 401 | free(buff); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 402 | return res; |
| 403 | } |
| 404 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 405 | int BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 406 | char cmd[MAX_CMD_LEN]; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 407 | int res = 0, res1, res2; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 408 | int ruleInsertPos = 1; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 409 | std::string costString; |
| 410 | const char *costCString; |
| 411 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 412 | /* The "-N costly" is created upfront, no need to handle it here. */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 413 | switch (quotaType) { |
| 414 | case QuotaUnique: |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 415 | costString = "costly_"; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 416 | costString += ifn; |
| 417 | costCString = costString.c_str(); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 418 | /* |
| 419 | * Flush the costly_<iface> is allowed to fail in case it didn't exist. |
| 420 | * Creating a new one is allowed to fail in case it existed. |
| 421 | * This helps with netd restarts. |
| 422 | */ |
| 423 | snprintf(cmd, sizeof(cmd), "-F %s", costCString); |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 424 | res1 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 425 | snprintf(cmd, sizeof(cmd), "-N %s", costCString); |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 426 | res2 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 427 | res = (res1 && res2) || (!res1 && !res2); |
| 428 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 429 | snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 430 | res |= runIpxtablesCmd(cmd, IptRejectNoAdd); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 431 | break; |
| 432 | case QuotaShared: |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 433 | costCString = "costly_shared"; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 434 | break; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 435 | default: |
| 436 | ALOGE("Unexpected quotatype %d", quotaType); |
| 437 | return -1; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 438 | } |
| 439 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 440 | if (globalAlertBytes) { |
| 441 | /* The alert rule comes 1st */ |
| 442 | ruleInsertPos = 2; |
| 443 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 444 | |
| 445 | snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 446 | runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 447 | |
| 448 | snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 449 | res |= runIpxtablesCmd(cmd, IptRejectNoAdd); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 450 | |
| 451 | snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); |
JP Abgrall | ad729ac | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 452 | runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 453 | |
| 454 | snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 455 | res |= runIpxtablesCmd(cmd, IptRejectNoAdd); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 456 | return res; |
| 457 | } |
| 458 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 459 | int BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 460 | char cmd[MAX_CMD_LEN]; |
| 461 | int res = 0; |
| 462 | std::string costString; |
| 463 | const char *costCString; |
| 464 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 465 | switch (quotaType) { |
| 466 | case QuotaUnique: |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 467 | costString = "costly_"; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 468 | costString += ifn; |
| 469 | costCString = costString.c_str(); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 470 | break; |
| 471 | case QuotaShared: |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 472 | costCString = "costly_shared"; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 473 | break; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 474 | default: |
| 475 | ALOGE("Unexpected quotatype %d", quotaType); |
| 476 | return -1; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 477 | } |
| 478 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 479 | snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 480 | res |= runIpxtablesCmd(cmd, IptRejectNoAdd); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 481 | snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 482 | res |= runIpxtablesCmd(cmd, IptRejectNoAdd); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 483 | |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 484 | /* The "-N costly_shared" is created upfront, no need to handle it here. */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 485 | if (quotaType == QuotaUnique) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 486 | snprintf(cmd, sizeof(cmd), "-F %s", costCString); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 487 | res |= runIpxtablesCmd(cmd, IptRejectNoAdd); |
JP Abgrall | a9f802c | 2011-06-29 15:46:45 -0700 | [diff] [blame] | 488 | snprintf(cmd, sizeof(cmd), "-X %s", costCString); |
| 489 | res |= runIpxtablesCmd(cmd, IptRejectNoAdd); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 490 | } |
| 491 | return res; |
| 492 | } |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 493 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 494 | int BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) { |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 495 | char cmd[MAX_CMD_LEN]; |
| 496 | char ifn[MAX_IFACENAME_LEN]; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 497 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 498 | std::string quotaCmd; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 499 | std::string ifaceName; |
| 500 | ; |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 501 | const char *costName = "shared"; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 502 | std::list<std::string>::iterator it; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 503 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 504 | if (!maxBytes) { |
| 505 | /* Don't talk about -1, deprecate it. */ |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 506 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 507 | return -1; |
| 508 | } |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 509 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 510 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 511 | return -1; |
| 512 | } |
| 513 | ifaceName = ifn; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 514 | |
| 515 | if (maxBytes == -1) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 516 | return removeInterfaceSharedQuota(ifn); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* Insert ingress quota. */ |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 520 | for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { |
| 521 | if (*it == ifaceName) |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 522 | break; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 523 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 524 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 525 | if (it == sharedQuotaIfaces.end()) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 526 | res |= prepCostlyIface(ifn, QuotaShared); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 527 | if (sharedQuotaIfaces.empty()) { |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 528 | quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 529 | res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 530 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 531 | ALOGE("Failed set quota rule"); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 532 | goto fail; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 533 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 534 | sharedQuotaBytes = maxBytes; |
| 535 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 536 | sharedQuotaIfaces.push_front(ifaceName); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 537 | |
| 538 | } |
| 539 | |
| 540 | if (maxBytes != sharedQuotaBytes) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 541 | res |= updateQuota(costName, maxBytes); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 542 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 543 | ALOGE("Failed update quota for %s", costName); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 544 | goto fail; |
| 545 | } |
| 546 | sharedQuotaBytes = maxBytes; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 547 | } |
| 548 | return 0; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 549 | |
| 550 | fail: |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 551 | /* |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 552 | * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse |
| 553 | * rules in the kernel to see which ones need cleaning up. |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 554 | * For now callers needs to choose if they want to "ndc bandwidth enable" |
| 555 | * which resets everything. |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 556 | */ |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 557 | removeInterfaceSharedQuota(ifn); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 558 | return -1; |
| 559 | } |
| 560 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 561 | /* It will also cleanup any shared alerts */ |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 562 | int BandwidthController::removeInterfaceSharedQuota(const char *iface) { |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 563 | char ifn[MAX_IFACENAME_LEN]; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 564 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 565 | std::string ifaceName; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 566 | std::list<std::string>::iterator it; |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 567 | const char *costName = "shared"; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 568 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 569 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 570 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 571 | return -1; |
| 572 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 573 | ifaceName = ifn; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 574 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 575 | for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { |
| 576 | if (*it == ifaceName) |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 577 | break; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 578 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 579 | if (it == sharedQuotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 580 | ALOGE("No such iface %s to delete", ifn); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 581 | return -1; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 582 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 583 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 584 | res |= cleanupCostlyIface(ifn, QuotaShared); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 585 | sharedQuotaIfaces.erase(it); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 586 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 587 | if (sharedQuotaIfaces.empty()) { |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 588 | std::string quotaCmd; |
JP Abgrall | bfa7466 | 2011-06-29 19:23:04 -0700 | [diff] [blame] | 589 | quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 590 | res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 591 | sharedQuotaBytes = 0; |
| 592 | if (sharedAlertBytes) { |
| 593 | removeSharedAlert(); |
| 594 | sharedAlertBytes = 0; |
| 595 | } |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 596 | } |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 597 | return res; |
| 598 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 599 | |
| 600 | int BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) { |
| 601 | char ifn[MAX_IFACENAME_LEN]; |
| 602 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 603 | std::string ifaceName; |
| 604 | const char *costName; |
| 605 | std::list<QuotaInfo>::iterator it; |
| 606 | std::string quotaCmd; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 607 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 608 | if (!maxBytes) { |
| 609 | /* Don't talk about -1, deprecate it. */ |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 610 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 611 | return -1; |
| 612 | } |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 613 | if (maxBytes == -1) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 614 | return removeInterfaceQuota(iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 615 | } |
| 616 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 617 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 618 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 619 | return -1; |
| 620 | } |
| 621 | ifaceName = ifn; |
| 622 | costName = iface; |
| 623 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 624 | /* Insert ingress quota. */ |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 625 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 626 | if (it->ifaceName == ifaceName) |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 627 | break; |
| 628 | } |
| 629 | |
| 630 | if (it == quotaIfaces.end()) { |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 631 | res |= prepCostlyIface(ifn, QuotaUnique); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 632 | quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 633 | res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 634 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 635 | ALOGE("Failed set quota rule"); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 636 | goto fail; |
| 637 | } |
| 638 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 639 | quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0)); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 640 | |
| 641 | } else { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 642 | res |= updateQuota(costName, maxBytes); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 643 | if (res) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 644 | ALOGE("Failed update quota for %s", iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 645 | goto fail; |
| 646 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 647 | it->quota = maxBytes; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 648 | } |
| 649 | return 0; |
| 650 | |
| 651 | fail: |
| 652 | /* |
| 653 | * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse |
| 654 | * rules in the kernel to see which ones need cleaning up. |
| 655 | * For now callers needs to choose if they want to "ndc bandwidth enable" |
| 656 | * which resets everything. |
| 657 | */ |
| 658 | removeInterfaceSharedQuota(ifn); |
| 659 | return -1; |
| 660 | } |
| 661 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 662 | int BandwidthController::getInterfaceSharedQuota(int64_t *bytes) { |
| 663 | return getInterfaceQuota("shared", bytes); |
| 664 | } |
| 665 | |
| 666 | int BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) { |
| 667 | FILE *fp; |
| 668 | char *fname; |
| 669 | int scanRes; |
| 670 | |
| 671 | asprintf(&fname, "/proc/net/xt_quota/%s", costName); |
| 672 | fp = fopen(fname, "r"); |
| 673 | free(fname); |
| 674 | if (!fp) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 675 | ALOGE("Reading quota %s failed (%s)", costName, strerror(errno)); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 676 | return -1; |
| 677 | } |
| 678 | scanRes = fscanf(fp, "%lld", bytes); |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 679 | ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 680 | fclose(fp); |
| 681 | return scanRes == 1 ? 0 : -1; |
| 682 | } |
| 683 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 684 | int BandwidthController::removeInterfaceQuota(const char *iface) { |
| 685 | |
| 686 | char ifn[MAX_IFACENAME_LEN]; |
| 687 | int res = 0; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 688 | std::string ifaceName; |
| 689 | const char *costName; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 690 | std::list<QuotaInfo>::iterator it; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 691 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 692 | if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 693 | ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 694 | return -1; |
| 695 | } |
| 696 | ifaceName = ifn; |
| 697 | costName = iface; |
| 698 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 699 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 700 | if (it->ifaceName == ifaceName) |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 701 | break; |
| 702 | } |
| 703 | |
| 704 | if (it == quotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 705 | ALOGE("No such iface %s to delete", ifn); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 706 | return -1; |
| 707 | } |
| 708 | |
| 709 | /* This also removes the quota command of CostlyIface chain. */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 710 | res |= cleanupCostlyIface(ifn, QuotaUnique); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 711 | |
| 712 | quotaIfaces.erase(it); |
| 713 | |
| 714 | return res; |
| 715 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 716 | |
| 717 | int BandwidthController::updateQuota(const char *quotaName, int64_t bytes) { |
| 718 | FILE *fp; |
| 719 | char *fname; |
| 720 | |
| 721 | asprintf(&fname, "/proc/net/xt_quota/%s", quotaName); |
| 722 | fp = fopen(fname, "w"); |
| 723 | free(fname); |
| 724 | if (!fp) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 725 | ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno)); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 726 | return -1; |
| 727 | } |
| 728 | fprintf(fp, "%lld\n", bytes); |
| 729 | fclose(fp); |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | int BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) { |
| 734 | int res = 0; |
| 735 | const char *opFlag; |
JP Abgrall | 8766669 | 2011-09-08 13:44:10 -0700 | [diff] [blame] | 736 | const char *ifaceLimiting; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 737 | char *alertQuotaCmd; |
| 738 | |
| 739 | switch (op) { |
| 740 | case IptOpInsert: |
| 741 | opFlag = "-I"; |
| 742 | break; |
| 743 | case IptOpReplace: |
| 744 | opFlag = "-R"; |
| 745 | break; |
| 746 | default: |
| 747 | case IptOpDelete: |
| 748 | opFlag = "-D"; |
| 749 | break; |
| 750 | } |
| 751 | |
JP Abgrall | 8766669 | 2011-09-08 13:44:10 -0700 | [diff] [blame] | 752 | ifaceLimiting = "! -i lo+"; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 753 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_INPUT", |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 754 | bytes, alertName); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 755 | res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); |
| 756 | free(alertQuotaCmd); |
JP Abgrall | 8766669 | 2011-09-08 13:44:10 -0700 | [diff] [blame] | 757 | ifaceLimiting = "! -o lo+"; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 758 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_OUTPUT", |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 759 | bytes, alertName); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 760 | res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); |
| 761 | free(alertQuotaCmd); |
| 762 | return res; |
| 763 | } |
| 764 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 765 | int BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) { |
| 766 | int res = 0; |
| 767 | const char *opFlag; |
| 768 | const char *ifaceLimiting; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 769 | char *alertQuotaCmd; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 770 | |
| 771 | switch (op) { |
| 772 | case IptOpInsert: |
| 773 | opFlag = "-I"; |
| 774 | break; |
| 775 | case IptOpReplace: |
| 776 | opFlag = "-R"; |
| 777 | break; |
| 778 | default: |
| 779 | case IptOpDelete: |
| 780 | opFlag = "-D"; |
| 781 | break; |
| 782 | } |
| 783 | |
| 784 | ifaceLimiting = "! -i lo+"; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 785 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_FORWARD", |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 786 | bytes, alertName); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 787 | res = runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); |
| 788 | free(alertQuotaCmd); |
| 789 | return res; |
| 790 | } |
| 791 | |
| 792 | int BandwidthController::setGlobalAlert(int64_t bytes) { |
| 793 | const char *alertName = ALERT_GLOBAL_NAME; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 794 | int res = 0; |
| 795 | |
| 796 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 797 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 798 | return -1; |
| 799 | } |
| 800 | if (globalAlertBytes) { |
| 801 | res = updateQuota(alertName, bytes); |
| 802 | } else { |
| 803 | res = runIptablesAlertCmd(IptOpInsert, alertName, bytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 804 | if (globalAlertTetherCount) { |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 805 | ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 806 | res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes); |
| 807 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 808 | } |
| 809 | globalAlertBytes = bytes; |
| 810 | return res; |
| 811 | } |
| 812 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 813 | int BandwidthController::setGlobalAlertInForwardChain(void) { |
| 814 | const char *alertName = ALERT_GLOBAL_NAME; |
| 815 | int res = 0; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 816 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 817 | globalAlertTetherCount++; |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 818 | ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 819 | |
| 820 | /* |
| 821 | * If there is no globalAlert active we are done. |
| 822 | * If there is an active globalAlert but this is not the 1st |
| 823 | * tether, we are also done. |
| 824 | */ |
| 825 | if (!globalAlertBytes || globalAlertTetherCount != 1) { |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | /* We only add the rule if this was the 1st tether added. */ |
| 830 | res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes); |
| 831 | return res; |
| 832 | } |
| 833 | |
| 834 | int BandwidthController::removeGlobalAlert(void) { |
| 835 | |
| 836 | const char *alertName = ALERT_GLOBAL_NAME; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 837 | int res = 0; |
| 838 | |
| 839 | if (!globalAlertBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 840 | ALOGE("No prior alert set"); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 841 | return -1; |
| 842 | } |
| 843 | res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 844 | if (globalAlertTetherCount) { |
| 845 | res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); |
| 846 | } |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 847 | globalAlertBytes = 0; |
| 848 | return res; |
| 849 | } |
| 850 | |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 851 | int BandwidthController::removeGlobalAlertInForwardChain(void) { |
| 852 | int res = 0; |
| 853 | const char *alertName = ALERT_GLOBAL_NAME; |
| 854 | |
| 855 | if (!globalAlertTetherCount) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 856 | ALOGE("No prior alert set"); |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 857 | return -1; |
| 858 | } |
| 859 | |
| 860 | globalAlertTetherCount--; |
| 861 | /* |
| 862 | * If there is no globalAlert active we are done. |
| 863 | * If there is an active globalAlert but there are more |
| 864 | * tethers, we are also done. |
| 865 | */ |
| 866 | if (!globalAlertBytes || globalAlertTetherCount >= 1) { |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /* We only detete the rule if this was the last tether removed. */ |
| 871 | res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); |
| 872 | return res; |
| 873 | } |
| 874 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 875 | int BandwidthController::setSharedAlert(int64_t bytes) { |
| 876 | if (!sharedQuotaBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 877 | 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] | 878 | return -1; |
| 879 | } |
| 880 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 881 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 882 | return -1; |
| 883 | } |
| 884 | return setCostlyAlert("shared", bytes, &sharedAlertBytes); |
| 885 | } |
| 886 | |
| 887 | int BandwidthController::removeSharedAlert(void) { |
| 888 | return removeCostlyAlert("shared", &sharedAlertBytes); |
| 889 | } |
| 890 | |
| 891 | int BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) { |
| 892 | std::list<QuotaInfo>::iterator it; |
| 893 | |
| 894 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 895 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 896 | return -1; |
| 897 | } |
| 898 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
| 899 | if (it->ifaceName == iface) |
| 900 | break; |
| 901 | } |
| 902 | |
| 903 | if (it == quotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 904 | 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] | 905 | return -1; |
| 906 | } |
| 907 | |
| 908 | return setCostlyAlert(iface, bytes, &it->alert); |
| 909 | } |
| 910 | |
| 911 | int BandwidthController::removeInterfaceAlert(const char *iface) { |
| 912 | std::list<QuotaInfo>::iterator it; |
| 913 | |
| 914 | for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { |
| 915 | if (it->ifaceName == iface) |
| 916 | break; |
| 917 | } |
| 918 | |
| 919 | if (it == quotaIfaces.end()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 920 | ALOGE("No prior alert set for interface %s", iface); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 921 | return -1; |
| 922 | } |
| 923 | |
| 924 | return removeCostlyAlert(iface, &it->alert); |
| 925 | } |
| 926 | |
| 927 | int BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) { |
| 928 | char *alertQuotaCmd; |
| 929 | char *chainNameAndPos; |
| 930 | int res = 0; |
| 931 | char *alertName; |
| 932 | |
| 933 | if (!bytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 934 | ALOGE("Invalid bytes value. 1..max_int64."); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 935 | return -1; |
| 936 | } |
| 937 | asprintf(&alertName, "%sAlert", costName); |
| 938 | if (*alertBytes) { |
| 939 | res = updateQuota(alertName, *alertBytes); |
| 940 | } else { |
| 941 | asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN); |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 942 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-I", chainNameAndPos, bytes, alertName); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 943 | res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); |
| 944 | free(alertQuotaCmd); |
| 945 | free(chainNameAndPos); |
| 946 | } |
| 947 | *alertBytes = bytes; |
| 948 | free(alertName); |
| 949 | return res; |
| 950 | } |
| 951 | |
| 952 | int BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) { |
| 953 | char *alertQuotaCmd; |
| 954 | char *chainName; |
| 955 | char *alertName; |
| 956 | int res = 0; |
| 957 | |
| 958 | asprintf(&alertName, "%sAlert", costName); |
| 959 | if (!*alertBytes) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 960 | ALOGE("No prior alert set for %s alert", costName); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 961 | return -1; |
| 962 | } |
| 963 | |
| 964 | asprintf(&chainName, "costly_%s", costName); |
Nick Kralevich | c2b26cb | 2012-02-23 13:04:26 -0800 | [diff] [blame] | 965 | asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-D", chainName, *alertBytes, alertName); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 966 | res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); |
| 967 | free(alertQuotaCmd); |
| 968 | free(chainName); |
| 969 | |
| 970 | *alertBytes = 0; |
| 971 | free(alertName); |
| 972 | return res; |
| 973 | } |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 974 | |
| 975 | /* |
| 976 | * Parse the ptks and bytes out of: |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 977 | * Chain FORWARD (policy RETURN 0 packets, 0 bytes) |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 978 | * pkts bytes target prot opt in out source destination |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 979 | * 0 0 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 980 | * 0 0 DROP all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 state INVALID |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 981 | * 0 0 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 982 | * |
| 983 | */ |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 984 | int BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp, |
| 985 | std::string &extraProcessingInfo) { |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 986 | int res; |
| 987 | char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN]; |
| 988 | char iface0[MAX_IPT_OUTPUT_LINE_LEN]; |
| 989 | char iface1[MAX_IPT_OUTPUT_LINE_LEN]; |
| 990 | char rest[MAX_IPT_OUTPUT_LINE_LEN]; |
| 991 | |
| 992 | char *buffPtr; |
| 993 | int64_t packets, bytes; |
| 994 | |
| 995 | while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) { |
| 996 | /* Clean up, so a failed parse can still print info */ |
| 997 | iface0[0] = iface1[0] = rest[0] = packets = bytes = 0; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 998 | res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s", |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 999 | &packets, &bytes, iface0, iface1, rest); |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 1000 | ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res, |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1001 | iface0, iface1, packets, bytes, rest, buffPtr); |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 1002 | extraProcessingInfo += buffPtr; |
| 1003 | |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1004 | if (res != 5) { |
| 1005 | continue; |
| 1006 | } |
| 1007 | if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) { |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 1008 | ALOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1009 | stats.rxPackets = packets; |
| 1010 | stats.rxBytes = bytes; |
| 1011 | } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) { |
Steve Block | 3fb42e0 | 2011-10-20 11:55:56 +0100 | [diff] [blame] | 1012 | ALOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1013 | stats.txPackets = packets; |
| 1014 | stats.txBytes = bytes; |
| 1015 | } |
| 1016 | } |
| 1017 | /* Failure if rx or tx was not found */ |
| 1018 | return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0; |
| 1019 | } |
| 1020 | |
| 1021 | |
| 1022 | char *BandwidthController::TetherStats::getStatsLine(void) { |
| 1023 | char *msg; |
| 1024 | asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(), |
| 1025 | rxBytes, rxPackets, txBytes, txPackets); |
| 1026 | return msg; |
| 1027 | } |
| 1028 | |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 1029 | int BandwidthController::getTetherStats(TetherStats &stats, std::string &extraProcessingInfo) { |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1030 | int res; |
| 1031 | std::string fullCmd; |
| 1032 | FILE *iptOutput; |
| 1033 | const char *cmd; |
| 1034 | |
| 1035 | if (stats.rxBytes != -1 || stats.txBytes != -1) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1036 | ALOGE("Unexpected input stats. Byte counts should be -1."); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1037 | return -1; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * Why not use some kind of lib to talk to iptables? |
| 1042 | * Because the only libs are libiptc and libip6tc in iptables, and they are |
| 1043 | * not easy to use. They require the known iptables match modules to be |
| 1044 | * preloaded/linked, and require apparently a lot of wrapper code to get |
| 1045 | * the wanted info. |
| 1046 | */ |
| 1047 | fullCmd = IPTABLES_PATH; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 1048 | fullCmd += " -nvx -L natctrl_FORWARD"; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1049 | iptOutput = popen(fullCmd.c_str(), "r"); |
| 1050 | if (!iptOutput) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 1051 | ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno)); |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 1052 | extraProcessingInfo += "Failed to run iptables."; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1053 | return -1; |
| 1054 | } |
JP Abgrall | a2a64f0 | 2011-11-11 20:36:16 -0800 | [diff] [blame] | 1055 | res = parseForwardChainStats(stats, iptOutput, extraProcessingInfo); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 1056 | pclose(iptOutput); |
| 1057 | |
| 1058 | /* Currently NatController doesn't do ipv6 tethering, so we are done. */ |
| 1059 | return res; |
| 1060 | } |