blob: bb2b4dce74995889be638474387922ff1835c7b8 [file] [log] [blame]
JP Abgrall4a5f5ca2011-06-15 18:37:39 -07001/*
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
17#include <stdlib.h>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070018#include <fcntl.h>
19#include <string.h>
20
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25
26#include <linux/netlink.h>
27#include <linux/rtnetlink.h>
28#include <linux/pkt_sched.h>
29
30#define LOG_TAG "BandwidthController"
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
34extern "C" int logwrap(int argc, const char **argv, int background);
35
36#include "BandwidthController.h"
37
JP Abgrallfa6f46d2011-06-17 23:17:28 -070038const int BandwidthController::MAX_CMD_LEN = 1024;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070039const int BandwidthController::MAX_IFACENAME_LEN = 64;
40const int BandwidthController::MAX_CMD_ARGS = 32;
41const char BandwidthController::IPTABLES_PATH[] = "/system/bin/iptables";
JP Abgrallfa6f46d2011-06-17 23:17:28 -070042const char BandwidthController::IP6TABLES_PATH[] = "/system/bin/ip6tables";
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070043
44/**
45 * Some comments about the rules:
46 * * Ordering
47 * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
48 * E.g. "-I INPUT -i rmnet0 --goto costly"
49 * - quota'd rules in the costly chain should be before penalty_box lookups.
50 *
51 * * global quota vs per interface quota
52 * - global quota for all costly interfaces uses a single costly chain:
53 * . initial rules
54 * iptables -N costly
55 * iptables -I INPUT -i iface0 --goto costly
56 * iptables -I OUTPUT -o iface0 --goto costly
57 * iptables -I costly -m quota \! --quota 500000 --jump REJECT --reject-with icmp-net-prohibited
58 * iptables -A costly --jump penalty_box
59 * iptables -A costly -m owner --socket-exists
60 * . adding a new iface to this, E.g.:
61 * iptables -I INPUT -i iface1 --goto costly
62 * iptables -I OUTPUT -o iface1 --goto costly
63 *
64 * - quota per interface. This is achieve by having "costly" chains per quota.
65 * E.g. adding a new costly interface iface0 with its own quota:
66 * iptables -N costly_iface0
67 * iptables -I INPUT -i iface0 --goto costly_iface0
68 * iptables -I OUTPUT -o iface0 --goto costly_iface0
69 * iptables -A costly_iface0 -m quota \! --quota 500000 --jump REJECT --reject-with icmp-net-prohibited
70 * iptables -A costly_iface0 --jump penalty_box
71 * iptables -A costly_iface0 -m owner --socket-exists
72 *
73 * * penalty_box handling:
74 * - only one penalty_box for all interfaces
75 * E.g Adding an app:
76 * iptables -A penalty_box -m owner --uid-owner app_3 --jump REJECT --reject-with icmp-net-prohibited
77 */
78const char *BandwidthController::cleanupCommands[] = {
JP Abgrall0dad7c22011-06-24 11:58:14 -070079 /* Cleanup rules. */
80 "-F",
81 "-t raw -F",
82 "-X costly",
83 "-X penalty_box",
84};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070085
86const char *BandwidthController::setupCommands[] = {
JP Abgrall0dad7c22011-06-24 11:58:14 -070087 /* Created needed chains. */
88 "-N costly",
89 "-N penalty_box",
90};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070091
JP Abgrall0dad7c22011-06-24 11:58:14 -070092const char *BandwidthController::basicAccountingCommands[] = {
93 "-F INPUT",
94 "-A INPUT -i lo --jump ACCEPT",
95 "-A INPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070096
JP Abgrall0dad7c22011-06-24 11:58:14 -070097 "-F OUTPUT",
98 "-A OUTPUT -o lo --jump ACCEPT",
99 "-A OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700100
JP Abgrall0dad7c22011-06-24 11:58:14 -0700101 "-F costly",
102 "-A costly --jump penalty_box",
103 "-A costly -m owner --socket-exists", /* This is a tracking rule. */
104 /* TODO(jpa): Figure out why iptables doesn't correctly return from this
105 * chain. For now, hack the chain exit with an ACCEPT.
106 */
107 "-A costly --jump ACCEPT",
108};
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700109
110BandwidthController::BandwidthController(void) {
111
112 char value[PROPERTY_VALUE_MAX];
113
114 property_get("persist.bandwidth.enable", value, "0");
115 if (!strcmp(value, "1")) {
116 enableBandwidthControl();
117 }
118
119}
120
JP Abgrall26e0d492011-06-24 19:21:51 -0700121int BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700122 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700123 LOGD("runIpxtablesCmd(cmd=%s)", cmd);
124 res |= runIptablesCmd(cmd, rejectHandling, IptIpV4);
125 res |= runIptablesCmd(cmd, rejectHandling, IptIpV6);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700126 return res;
127}
128
JP Abgrall26e0d492011-06-24 19:21:51 -0700129int BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) {
130
131 memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0'
132 strncpy(buffer, src, buffSize);
133 return buffer[buffSize - 1];
134}
135
136int BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, IptIpVer iptVer) {
137 char buffer[MAX_CMD_LEN];
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700138 const char *argv[MAX_CMD_ARGS];
JP Abgrall26e0d492011-06-24 19:21:51 -0700139 int argc = 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700140 char *next = buffer;
141 char *tmp;
142
JP Abgrall0dad7c22011-06-24 11:58:14 -0700143 std::string fullCmd = cmd;
JP Abgrall26e0d492011-06-24 19:21:51 -0700144
145 if (rejectHandling == IptRejectAdd) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700146 fullCmd += " --jump REJECT --reject-with";
JP Abgrall26e0d492011-06-24 19:21:51 -0700147 switch (iptVer) {
148 case IptIpV4:
149 fullCmd += " icmp-net-prohibited";
150 break;
151 case IptIpV6:
152 fullCmd += " icmp6-adm-prohibited";
153 break;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700154 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700155 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700156
JP Abgrall26e0d492011-06-24 19:21:51 -0700157 argc = 0;
158 argv[argc++] = iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700159
JP Abgralla9f802c2011-06-29 15:46:45 -0700160 LOGD("runIptablesCmd(): %s %s", argv[0], fullCmd.c_str());
JP Abgrall26e0d492011-06-24 19:21:51 -0700161 if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700162 LOGE("iptables command too long");
JP Abgrall0dad7c22011-06-24 11:58:14 -0700163 return -1;
164 }
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700165
166 while ((tmp = strsep(&next, " "))) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700167 argv[argc++] = tmp;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700168 if (argc >= MAX_CMD_ARGS) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700169 LOGE("iptables argument overflow");
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700170 return -1;
171 }
172 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700173
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700174 argv[argc] = NULL;
175 /* TODO(jpa): Once this stabilizes, remove logwrap() as it tends to wedge netd
176 * Then just talk directly to the kernel via rtnetlink.
177 */
178 return logwrap(argc, argv, 0);
179}
180
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700181int BandwidthController::enableBandwidthControl(void) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700182 int res;
183 /* Some of the initialCommands are allowed to fail */
JP Abgrall26e0d492011-06-24 19:21:51 -0700184 runCommands(sizeof(cleanupCommands) / sizeof(char*), cleanupCommands, RunCmdFailureOk);
185 runCommands(sizeof(setupCommands) / sizeof(char*), setupCommands, RunCmdFailureOk);
186 res = runCommands(sizeof(basicAccountingCommands) / sizeof(char*), basicAccountingCommands, RunCmdFailureBad);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700187 return res;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700188
189}
190
191int BandwidthController::disableBandwidthControl(void) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700192 /* The cleanupCommands are allowed to fail. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700193 runCommands(sizeof(cleanupCommands) / sizeof(char*), cleanupCommands, RunCmdFailureOk);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700194 return 0;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700195}
196
JP Abgrall26e0d492011-06-24 19:21:51 -0700197int BandwidthController::runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700198 int res = 0;
199 LOGD("runCommands(): %d commands", numCommands);
200 for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700201 res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd);
202 if (res && cmdErrHandling != RunCmdFailureBad)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700203 return res;
204 }
JP Abgrall26e0d492011-06-24 19:21:51 -0700205 return cmdErrHandling == RunCmdFailureBad ? res : 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700206}
207
JP Abgrall0dad7c22011-06-24 11:58:14 -0700208std::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700209 std::string res;
JP Abgrall26e0d492011-06-24 19:21:51 -0700210 char *convBuff;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700211
212 switch (op) {
213 case IptOpInsert:
214 res = "-I";
215 break;
216 case IptOpReplace:
217 res = "-R";
218 break;
219 default:
220 case IptOpDelete:
221 res = "-D";
222 break;
223 }
224 res += " penalty_box";
JP Abgrall26e0d492011-06-24 19:21:51 -0700225 asprintf(&convBuff, "%d", uid);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700226 res += " -m owner --uid-owner ";
227 res += convBuff;
JP Abgrall26e0d492011-06-24 19:21:51 -0700228 free(convBuff);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700229 return res;
230}
231
232int BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700233 return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700234}
235
236int BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700237 return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700238}
239
JP Abgrall26e0d492011-06-24 19:21:51 -0700240int BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700241 char cmd[MAX_CMD_LEN];
242 int uidNum;
JP Abgrall26e0d492011-06-24 19:21:51 -0700243 const char *failLogTemplate;
244 IptOp op;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700245 int appUids[numUids];
JP Abgrall26e0d492011-06-24 19:21:51 -0700246 std::string naughtyCmd;
JP Abgrall26e0d492011-06-24 19:21:51 -0700247 switch (appOp) {
248 case NaughtyAppOpAdd:
249 op = IptOpInsert;
250 failLogTemplate = "Failed to add app uid %d to penalty box.";
251 break;
252 case NaughtyAppOpRemove:
253 op = IptOpDelete;
254 failLogTemplate = "Failed to delete app uid %d from penalty box.";
255 break;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700256 }
257
258 for (uidNum = 0; uidNum < numUids; uidNum++) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700259 appUids[uidNum] = atol(appStrUids[uidNum]);
260 if (appUids[uidNum] == 0) {
261 LOGE(failLogTemplate, appUids[uidNum]);
262 goto fail_parse;
263 }
264 }
JP Abgrall26e0d492011-06-24 19:21:51 -0700265
266 for (uidNum = 0; uidNum < numUids; uidNum++) {
267 naughtyCmd = makeIptablesNaughtyCmd(op, appUids[uidNum]);
268 if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) {
269 LOGE(failLogTemplate, appUids[uidNum]);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700270 goto fail_with_uidNum;
271 }
272 }
273 return 0;
274
JP Abgrall26e0d492011-06-24 19:21:51 -0700275fail_with_uidNum:
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700276 /* Try to remove the uid that failed in any case*/
JP Abgrall26e0d492011-06-24 19:21:51 -0700277 naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]);
278 runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd);
279fail_parse:
280 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700281}
282
JP Abgrall26e0d492011-06-24 19:21:51 -0700283std::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700284 std::string res;
285 char convBuff[21]; // log10(2^64) ~ 20
JP Abgrall0dad7c22011-06-24 11:58:14 -0700286
287 LOGD("makeIptablesQuotaCmd(%d, %llu)", op, quota);
288
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700289 switch (op) {
290 case IptOpInsert:
291 res = "-I";
292 break;
293 case IptOpReplace:
294 res = "-R";
295 break;
296 default:
297 case IptOpDelete:
298 res = "-D";
299 break;
300 }
301 res += " costly";
302 if (costName) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700303 res += "_";
304 res += costName;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700305 }
306 sprintf(convBuff, "%lld", quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700307 /* TODO(jpa): Use -m quota2 --name " + costName + " ! --quota "
308 * once available.
309 */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700310 res += " -m quota ! --quota ";
311 res += convBuff;
312 ;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700313 // The requried --jump REJECT ... will be added later.
314 return res;
315}
316
JP Abgrall26e0d492011-06-24 19:21:51 -0700317int BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700318 char cmd[MAX_CMD_LEN];
319 int res = 0;
320 std::string costString;
321 const char *costCString;
322
323 costString = "costly";
324 /* The "-N costly" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700325 switch (quotaType) {
326 case QuotaUnique:
JP Abgrall0dad7c22011-06-24 11:58:14 -0700327 costString += "_";
328 costString += ifn;
329 costCString = costString.c_str();
330 snprintf(cmd, sizeof(cmd), "-N %s", costCString);
JP Abgrall26e0d492011-06-24 19:21:51 -0700331 res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700332 snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString);
JP Abgrall26e0d492011-06-24 19:21:51 -0700333 res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700334 snprintf(cmd, sizeof(cmd), "-A %s -m owner --socket-exists", costCString);
JP Abgrall26e0d492011-06-24 19:21:51 -0700335 res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700336 /* TODO(jpa): Figure out why iptables doesn't correctly return from this
337 * chain. For now, hack the chain exit with an ACCEPT.
338 */
339 snprintf(cmd, sizeof(cmd), "-A %s --jump ACCEPT", costCString);
JP Abgrall26e0d492011-06-24 19:21:51 -0700340 res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
341 break;
342 case QuotaShared:
JP Abgrall0dad7c22011-06-24 11:58:14 -0700343 costCString = costString.c_str();
JP Abgrall26e0d492011-06-24 19:21:51 -0700344 break;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700345 }
346
347 snprintf(cmd, sizeof(cmd), "-I INPUT -i %s --goto %s", ifn, costCString);
JP Abgrall26e0d492011-06-24 19:21:51 -0700348 res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700349 snprintf(cmd, sizeof(cmd), "-I OUTPUT -o %s --goto %s", ifn, costCString);
JP Abgrall26e0d492011-06-24 19:21:51 -0700350 res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700351 return res;
352}
353
JP Abgrall26e0d492011-06-24 19:21:51 -0700354int BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700355 char cmd[MAX_CMD_LEN];
356 int res = 0;
357 std::string costString;
358 const char *costCString;
359
360 costString = "costly";
JP Abgrall26e0d492011-06-24 19:21:51 -0700361 switch (quotaType) {
362 case QuotaUnique:
JP Abgrall0dad7c22011-06-24 11:58:14 -0700363 costString += "_";
364 costString += ifn;
365 costCString = costString.c_str();
JP Abgrall26e0d492011-06-24 19:21:51 -0700366 break;
367 case QuotaShared:
JP Abgrall0dad7c22011-06-24 11:58:14 -0700368 costCString = costString.c_str();
JP Abgrall26e0d492011-06-24 19:21:51 -0700369 break;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700370 }
371
372 snprintf(cmd, sizeof(cmd), "-D INPUT -i %s --goto %s", ifn, costCString);
JP Abgrall26e0d492011-06-24 19:21:51 -0700373 res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700374 snprintf(cmd, sizeof(cmd), "-D OUTPUT -o %s --goto %s", ifn, costCString);
JP Abgrall26e0d492011-06-24 19:21:51 -0700375 res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700376
377 /* The "-N costly" is created upfront, no need to handle it here. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700378 if (quotaType == QuotaUnique) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700379 snprintf(cmd, sizeof(cmd), "-F %s", costCString);
JP Abgrall26e0d492011-06-24 19:21:51 -0700380 res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
JP Abgralla9f802c2011-06-29 15:46:45 -0700381 snprintf(cmd, sizeof(cmd), "-X %s", costCString);
382 res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700383 }
384 return res;
385}
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700386
JP Abgrall0dad7c22011-06-24 11:58:14 -0700387int BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700388 char cmd[MAX_CMD_LEN];
389 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700390 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700391 std::string quotaCmd;
392 std::string ifaceName;;
393 const char *costName = NULL; /* Shared quota */
394 std::list<std::string>::iterator it;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700395
JP Abgrall26e0d492011-06-24 19:21:51 -0700396 if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
397 LOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
398 return -1;
399 }
400 ifaceName = ifn;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700401
402 if (maxBytes == -1) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700403 return removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700404 }
405
406 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700407 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
408 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700409 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700410 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700411
JP Abgrall0dad7c22011-06-24 11:58:14 -0700412 if (it == sharedQuotaIfaces.end()) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700413 res |= prepCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700414 if (sharedQuotaIfaces.empty()) {
JP Abgrall0dad7c22011-06-24 11:58:14 -0700415 quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
JP Abgrall26e0d492011-06-24 19:21:51 -0700416 res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700417 if (res) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700418 LOGE("Failed set quota rule.");
419 goto fail;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700420 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700421 sharedQuotaBytes = maxBytes;
422 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700423 sharedQuotaIfaces.push_front(ifaceName);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700424
425 }
426
427 if (maxBytes != sharedQuotaBytes) {
428 /* Instead of replacing, which requires being aware of the rules in
429 * the kernel, we just add a new one, then delete the older one.
430 */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700431
JP Abgrall0dad7c22011-06-24 11:58:14 -0700432 quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
JP Abgrall26e0d492011-06-24 19:21:51 -0700433 res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700434
JP Abgrall0dad7c22011-06-24 11:58:14 -0700435 quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
JP Abgrall26e0d492011-06-24 19:21:51 -0700436 res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700437
438 if (res) {
439 LOGE("Failed replace quota rule.");
440 goto fail;
441 }
442 sharedQuotaBytes = maxBytes;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700443 }
444 return 0;
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700445
446 fail:
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700447 /*
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700448 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
449 * rules in the kernel to see which ones need cleaning up.
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700450 * For now callers needs to choose if they want to "ndc bandwidth enable"
451 * which resets everything.
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700452 */
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700453 removeInterfaceSharedQuota(ifn);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700454 return -1;
455}
456
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700457int BandwidthController::removeInterfaceSharedQuota(const char *iface) {
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700458 char ifn[MAX_IFACENAME_LEN];
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700459 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700460 std::string ifaceName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700461 std::list<std::string>::iterator it;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700462
JP Abgrall26e0d492011-06-24 19:21:51 -0700463 if(StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
464 LOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
465 return -1;
466 }
467 ifaceName =ifn;
468
JP Abgrall0dad7c22011-06-24 11:58:14 -0700469 for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
470 if (*it == ifaceName)
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700471 break;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700472 }
JP Abgrall0dad7c22011-06-24 11:58:14 -0700473 if (it == sharedQuotaIfaces.end()) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700474 LOGE("No such iface %s to delete.", ifn);
475 return -1;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700476 }
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700477
JP Abgrall26e0d492011-06-24 19:21:51 -0700478 res |= cleanupCostlyIface(ifn, QuotaShared);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700479 sharedQuotaIfaces.erase(it);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700480
JP Abgrall0dad7c22011-06-24 11:58:14 -0700481 if (sharedQuotaIfaces.empty()) {
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700482 std::string quotaCmd;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700483 quotaCmd = makeIptablesQuotaCmd(IptOpDelete, NULL, sharedQuotaBytes);
JP Abgrall26e0d492011-06-24 19:21:51 -0700484 res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
JP Abgrallfa6f46d2011-06-17 23:17:28 -0700485 sharedQuotaBytes = -1;
486 }
487
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700488 return res;
489}
JP Abgrall0dad7c22011-06-24 11:58:14 -0700490
491int BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
492 char ifn[MAX_IFACENAME_LEN];
493 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700494 std::string ifaceName;
495 const char *costName;
496 std::list<QuotaInfo>::iterator it;
497 std::string quotaCmd;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700498
499 if (maxBytes == -1) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700500 return removeInterfaceQuota(iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700501 }
502
JP Abgrall26e0d492011-06-24 19:21:51 -0700503 if(StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
504 LOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
505 return -1;
506 }
507 ifaceName = ifn;
508 costName = iface;
509
JP Abgrall0dad7c22011-06-24 11:58:14 -0700510
511 /* Insert ingress quota. */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700512 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
513 if (it->first == ifaceName)
514 break;
515 }
516
517 if (it == quotaIfaces.end()) {
JP Abgrall26e0d492011-06-24 19:21:51 -0700518 res |= prepCostlyIface(ifn, QuotaUnique);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700519 quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
JP Abgrall26e0d492011-06-24 19:21:51 -0700520 res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700521 if (res) {
522 LOGE("Failed set quota rule.");
523 goto fail;
524 }
525
526 quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes));
527
528 } else {
529 /* Instead of replacing, which requires being aware of the rules in
530 * the kernel, we just add a new one, then delete the older one.
531 */
JP Abgrall0dad7c22011-06-24 11:58:14 -0700532 quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
JP Abgrall26e0d492011-06-24 19:21:51 -0700533 res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700534
535 quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, it->second);
JP Abgrall26e0d492011-06-24 19:21:51 -0700536 res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700537
538 if (res) {
539 LOGE("Failed replace quota rule.");
540 goto fail;
541 }
542 it->second = maxBytes;
543 }
544 return 0;
545
546 fail:
547 /*
548 * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
549 * rules in the kernel to see which ones need cleaning up.
550 * For now callers needs to choose if they want to "ndc bandwidth enable"
551 * which resets everything.
552 */
553 removeInterfaceSharedQuota(ifn);
554 return -1;
555}
556
557int BandwidthController::removeInterfaceQuota(const char *iface) {
558
559 char ifn[MAX_IFACENAME_LEN];
560 int res = 0;
JP Abgrall26e0d492011-06-24 19:21:51 -0700561 std::string ifaceName;
562 const char *costName;
JP Abgrall0dad7c22011-06-24 11:58:14 -0700563 std::list<QuotaInfo>::iterator it;
JP Abgrall26e0d492011-06-24 19:21:51 -0700564
565 if(StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
566 LOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
567 return -1;
568 }
569 ifaceName = ifn;
570 costName = iface;
571
JP Abgrall0dad7c22011-06-24 11:58:14 -0700572 for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
573 if (it->first == ifaceName)
574 break;
575 }
576
577 if (it == quotaIfaces.end()) {
578 LOGE("No such iface %s to delete.", ifn);
579 return -1;
580 }
581
582 /* This also removes the quota command of CostlyIface chain. */
JP Abgrall26e0d492011-06-24 19:21:51 -0700583 res |= cleanupCostlyIface(ifn, QuotaUnique);
JP Abgrall0dad7c22011-06-24 11:58:14 -0700584
585 quotaIfaces.erase(it);
586
587 return res;
588}