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