BandwidthController.cpp revision c6c673496184bed6d62cf92a6fc7ed43fd94acd5
14a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall/* 24a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Copyright (C) 2011 The Android Open Source Project 34a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 44a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Licensed under the Apache License, Version 2.0 (the "License"); 54a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * you may not use this file except in compliance with the License. 64a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * You may obtain a copy of the License at 74a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 84a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * http://www.apache.org/licenses/LICENSE-2.0 94a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Unless required by applicable law or agreed to in writing, software 114a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * distributed under the License is distributed on an "AS IS" BASIS, 124a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * See the License for the specific language governing permissions and 144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * limitations under the License. 154a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */ 164a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 17db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall// #define LOG_NDEBUG 0 18db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 19db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* 20db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * The CommandListener, FrameworkListener don't allow for 21db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * multiple calls in parallel to reach the BandwidthController. 22db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * If they ever were to allow it, then netd/ would need some tweaking. 23db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 24db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall#include <errno.h> 264a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <fcntl.h> 27db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall#include <stdio.h> 288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall#include <stdlib.h> 294a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <string.h> 304a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 314a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/socket.h> 324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/stat.h> 334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/types.h> 344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/wait.h> 354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <linux/netlink.h> 374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <linux/rtnetlink.h> 384a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <linux/pkt_sched.h> 394a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 404a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#define LOG_TAG "BandwidthController" 414a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <cutils/log.h> 424a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <cutils/properties.h> 434a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallextern "C" int logwrap(int argc, const char **argv, int background); 454a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 464a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include "BandwidthController.h" 474a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 48db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* Alphabetical */ 49876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrallconst char BandwidthController::ALERT_IPT_TEMPLATE[] = "%s %s %s -m quota2 ! --quota %lld --name %s"; 50db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int BandwidthController::ALERT_RULE_POS_IN_COSTLY_CHAIN = 4; 51c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallconst char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert"; 52db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char BandwidthController::IP6TABLES_PATH[] = "/system/bin/ip6tables"; 53db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char BandwidthController::IPTABLES_PATH[] = "/system/bin/iptables"; 54db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int BandwidthController::MAX_CMD_ARGS = 32; 55db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int BandwidthController::MAX_CMD_LEN = 1024; 56db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int BandwidthController::MAX_IFACENAME_LEN = 64; 57db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256; 58db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 5911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrallbool BandwidthController::useLogwrapCall = false; 604a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 614a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall/** 624a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Some comments about the rules: 634a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * Ordering 644a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains. 654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * E.g. "-I INPUT -i rmnet0 --goto costly" 664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * - quota'd rules in the costly chain should be before penalty_box lookups. 674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * global quota vs per interface quota 694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * - global quota for all costly interfaces uses a single costly chain: 704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * . initial rules 71bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -N costly_shared 72bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -I INPUT -i iface0 --goto costly_shared 73bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -I OUTPUT -o iface0 --goto costly_shared 74bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -I costly_shared -m quota \! --quota 500000 \ 75bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * --jump REJECT --reject-with icmp-net-prohibited 76bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -A costly_shared --jump penalty_box 77bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -A costly_shared -m owner --socket-exists 788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall * 794a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * . adding a new iface to this, E.g.: 80bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -I INPUT -i iface1 --goto costly_shared 81bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -I OUTPUT -o iface1 --goto costly_shared 824a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * - quota per interface. This is achieve by having "costly" chains per quota. 844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * E.g. adding a new costly interface iface0 with its own quota: 854a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * iptables -N costly_iface0 864a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * iptables -I INPUT -i iface0 --goto costly_iface0 874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * iptables -I OUTPUT -o iface0 --goto costly_iface0 88bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -A costly_iface0 -m quota \! --quota 500000 \ 89bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * --jump REJECT --reject-with icmp-net-prohibited 90bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -A costly_iface0 --jump penalty_box 914a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * iptables -A costly_iface0 -m owner --socket-exists 924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * penalty_box handling: 944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * - only one penalty_box for all interfaces 954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * E.g Adding an app: 96bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -A penalty_box -m owner --uid-owner app_3 \ 97bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * --jump REJECT --reject-with icmp-net-prohibited 984a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */ 99db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_CLEANUP_COMMANDS[] = { 1000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* Cleanup rules. */ 1010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-F", 1020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-t raw -F", 10339f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall /* TODO: If at some point we need more user chains than here, then we will need 10439f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall * a different cleanup approach. 10539f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall */ 106bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-X", /* Should normally only be costly_shared, penalty_box, and costly_<iface> */ 1070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1084a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 109db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_SETUP_COMMANDS[] = { 1100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* Created needed chains. */ 111bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-N costly_shared", 1120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-N penalty_box", 1130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 115db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = { 1160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-F INPUT", 1170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-A INPUT -i lo --jump ACCEPT", 1180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-A INPUT -m owner --socket-exists", /* This is a tracking rule. */ 1190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 1200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-F OUTPUT", 1210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-A OUTPUT -o lo --jump ACCEPT", 1220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-A OUTPUT -m owner --socket-exists", /* This is a tracking rule. */ 1230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 124bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-F costly_shared", 125bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-A costly_shared --jump penalty_box", 126bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-A costly_shared -m owner --socket-exists", /* This is a tracking rule. */ 1270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* TODO(jpa): Figure out why iptables doesn't correctly return from this 1280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * chain. For now, hack the chain exit with an ACCEPT. 1290dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall */ 130bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-A costly_shared --jump ACCEPT", 1310dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 1334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP AbgrallBandwidthController::BandwidthController(void) { 1344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char value[PROPERTY_VALUE_MAX]; 1354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 1364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall property_get("persist.bandwidth.enable", value, "0"); 1374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall if (!strcmp(value, "1")) { 1384a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall enableBandwidthControl(); 1394a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 1404a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 14111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall property_get("persist.bandwidth.uselogwrap", value, "0"); 14211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall useLogwrapCall = !strcmp(value, "1"); 1434a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 1444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 14526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling) { 1460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 1478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 148db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall LOGV("runIpxtablesCmd(cmd=%s)", cmd); 14926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIptablesCmd(cmd, rejectHandling, IptIpV4); 15026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIptablesCmd(cmd, rejectHandling, IptIpV6); 1510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 1520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 1530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 15426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) { 15526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 15626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0' 15726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall strncpy(buffer, src, buffSize); 15826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return buffer[buffSize - 1]; 15926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall} 16026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 1618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, 1628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall IptIpVer iptVer) { 16326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall char buffer[MAX_CMD_LEN]; 1644a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall const char *argv[MAX_CMD_ARGS]; 16526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall int argc = 0; 1664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char *next = buffer; 1674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char *tmp; 16811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall int res; 1694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 1700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string fullCmd = cmd; 17126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 17226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (rejectHandling == IptRejectAdd) { 1730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall fullCmd += " --jump REJECT --reject-with"; 17426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (iptVer) { 17526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case IptIpV4: 1768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fullCmd += " icmp-net-prohibited"; 1778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 17826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case IptIpV6: 1798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fullCmd += " icmp6-adm-prohibited"; 1808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 1810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 1820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 1830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 18411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall fullCmd.insert(0, " "); 18511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH); 1864a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 18711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (!useLogwrapCall) { 18811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall res = system(fullCmd.c_str()); 18911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } else { 19011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) { 19111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall LOGE("iptables command too long"); 1924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return -1; 1934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 194fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 19511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argc = 0; 19611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall while ((tmp = strsep(&next, " "))) { 19711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argv[argc++] = tmp; 19811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (argc >= MAX_CMD_ARGS) { 19911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall LOGE("iptables argument overflow"); 20011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall return -1; 20111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 20211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 20311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall 20411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argv[argc] = NULL; 20511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall res = logwrap(argc, argv, 0); 20611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 20711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (res) { 20811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall LOGE("runIptablesCmd(): failed %s res=%d", fullCmd.c_str(), res); 20911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 21011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall return res; 2114a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2124a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2134a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::enableBandwidthControl(void) { 214fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res; 2158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 216db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Let's pretend we started from scratch ... */ 2178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedQuotaIfaces.clear(); 2188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall quotaIfaces.clear(); 2198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall naughtyAppUids.clear(); 220db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall globalAlertBytes = 0; 221c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount = 0; 222db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall sharedQuotaBytes = sharedAlertBytes = 0; 223db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 224db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 225db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Some of the initialCommands are allowed to fail */ 226db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*), 227db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall IPT_CLEANUP_COMMANDS, RunCmdFailureOk); 228db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*), 229db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall IPT_SETUP_COMMANDS, RunCmdFailureOk); 230db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall res = runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*), 231db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad); 2328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 233fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 2344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::disableBandwidthControl(void) { 238db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* The IPT_CLEANUP_COMMANDS are allowed to fail. */ 239db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*), 240db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall IPT_CLEANUP_COMMANDS, RunCmdFailureOk); 241fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return 0; 2424a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2434a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runCommands(int numCommands, const char *commands[], 2458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall RunCmdErrHandling cmdErrHandling) { 246fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 247db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall LOGV("runCommands(): %d commands", numCommands); 248fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) { 24926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd); 25026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (res && cmdErrHandling != RunCmdFailureBad) 251fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 252fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 25326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return cmdErrHandling == RunCmdFailureBad ? res : 0; 254fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 255fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 2560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallstd::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) { 257fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string res; 2588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *buff; 2598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 260fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 261fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall switch (op) { 2628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 2638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 2648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 2658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 2668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 2678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 2688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 2698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 2708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 2718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 272fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 2738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&buff, "%s penalty_box -m owner --uid-owner %d", opFlag, uid); 2748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = buff; 2758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(buff); 276fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 277fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 278fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 279fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::addNaughtyApps(int numUids, char *appUids[]) { 28026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd); 281fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 282fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 283fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) { 28426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove); 285fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 286fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 28726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) { 288fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall char cmd[MAX_CMD_LEN]; 289fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int uidNum; 29026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *failLogTemplate; 29126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall IptOp op; 292fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int appUids[numUids]; 29326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string naughtyCmd; 2948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 29526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (appOp) { 29626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case NaughtyAppOpAdd: 2978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall op = IptOpInsert; 2988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall failLogTemplate = "Failed to add app uid %d to penalty box."; 2998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 30026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case NaughtyAppOpRemove: 3018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall op = IptOpDelete; 3028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall failLogTemplate = "Failed to delete app uid %d from penalty box."; 3038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 30426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 30526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 306fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (uidNum = 0; uidNum < numUids; uidNum++) { 307fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall appUids[uidNum] = atol(appStrUids[uidNum]); 308fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (appUids[uidNum] == 0) { 30926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall LOGE(failLogTemplate, appUids[uidNum]); 310fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail_parse; 311fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 312fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 313fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 314fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (uidNum = 0; uidNum < numUids; uidNum++) { 31526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall naughtyCmd = makeIptablesNaughtyCmd(op, appUids[uidNum]); 31626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) { 31726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall LOGE(failLogTemplate, appUids[uidNum]); 318fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail_with_uidNum; 319fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 320fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 321fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return 0; 322fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 32326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum: 324fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall /* Try to remove the uid that failed in any case*/ 32526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]); 32626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd); 32726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse: 32826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 3294a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 3304a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 33126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) { 332fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string res; 3338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *buff; 3348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 3350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 336db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall LOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota); 3370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 338fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall switch (op) { 3398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 3408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 3418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 3438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 3448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 3468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 3478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 3488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 349fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 3508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 351bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall // The requried IP version specific --jump REJECT ... will be added later. 3528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota, 3538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall costName); 3548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = buff; 3558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(buff); 3560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 3570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 3580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 35926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) { 3600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char cmd[MAX_CMD_LEN]; 3610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 3628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int ruleInsertPos = 1; 3630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string costString; 3640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall const char *costCString; 3650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 3660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* The "-N costly" is created upfront, no need to handle it here. */ 36726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (quotaType) { 36826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaUnique: 369bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costString = "costly_"; 3700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costString += ifn; 3710dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costCString = costString.c_str(); 3720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-N %s", costCString); 37326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 3740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString); 37526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 3760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-A %s -m owner --socket-exists", costCString); 37726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 3780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* TODO(jpa): Figure out why iptables doesn't correctly return from this 3790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * chain. For now, hack the chain exit with an ACCEPT. 3800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall */ 3810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-A %s --jump ACCEPT", costCString); 38226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 38326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 38426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaShared: 385bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costCString = "costly_shared"; 38626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 3870dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 3880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 3898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (globalAlertBytes) { 3908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* The alert rule comes 1st */ 3918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ruleInsertPos = 2; 3928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 3938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall snprintf(cmd, sizeof(cmd), "-I INPUT %d -i %s --goto %s", ruleInsertPos, ifn, costCString); 39426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 3958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall snprintf(cmd, sizeof(cmd), "-I OUTPUT %d -o %s --goto %s", ruleInsertPos, ifn, costCString); 39626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 3970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 3980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 3990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 40026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) { 4010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char cmd[MAX_CMD_LEN]; 4020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 4030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string costString; 4040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall const char *costCString; 4050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 40626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (quotaType) { 40726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaUnique: 408bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costString = "costly_"; 4090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costString += ifn; 4100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costCString = costString.c_str(); 41126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 41226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaShared: 413bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costCString = "costly_shared"; 41426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 4150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 4160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 4170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-D INPUT -i %s --goto %s", ifn, costCString); 41826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-D OUTPUT -o %s --goto %s", ifn, costCString); 42026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 422bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall /* The "-N costly_shared" is created upfront, no need to handle it here. */ 42326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (quotaType == QuotaUnique) { 4240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-F %s", costCString); 42526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 426a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall snprintf(cmd, sizeof(cmd), "-X %s", costCString); 427a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 428fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 429fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 430fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 4314a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 4320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) { 4334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char cmd[MAX_CMD_LEN]; 4344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char ifn[MAX_IFACENAME_LEN]; 435fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 43626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string quotaCmd; 4378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::string ifaceName; 4388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ; 439bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall const char *costName = "shared"; 44026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<std::string>::iterator it; 4414a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 4428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!maxBytes) { 4438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* Don't talk about -1, deprecate it. */ 4448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Invalid bytes value. 1..max_int64."); 4458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 4468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 44726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 44826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall LOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 44926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 45026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 45126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 4524a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 4534a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall if (maxBytes == -1) { 454fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return removeInterfaceSharedQuota(ifn); 4554a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 4564a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 4574a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall /* Insert ingress quota. */ 4580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { 4590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (*it == ifaceName) 460fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall break; 4614a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 462fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 4630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == sharedQuotaIfaces.end()) { 46426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= prepCostlyIface(ifn, QuotaShared); 4650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (sharedQuotaIfaces.empty()) { 4660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); 46726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 4684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall if (res) { 4698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Failed set quota rule"); 470fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail; 4714a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 472fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall sharedQuotaBytes = maxBytes; 473fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 4740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall sharedQuotaIfaces.push_front(ifaceName); 475fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 476fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 477fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 478fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (maxBytes != sharedQuotaBytes) { 4798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= updateQuota(costName, maxBytes); 480fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (res) { 4818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Failed update quota for %s", costName); 482fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail; 483fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 484fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall sharedQuotaBytes = maxBytes; 4854a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 4864a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return 0; 487fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 488fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall fail: 4894a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall /* 4904a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse 4914a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * rules in the kernel to see which ones need cleaning up. 492fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall * For now callers needs to choose if they want to "ndc bandwidth enable" 493fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall * which resets everything. 4944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */ 495fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall removeInterfaceSharedQuota(ifn); 4964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return -1; 4974a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 4984a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 4998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */ 500fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) { 5014a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char ifn[MAX_IFACENAME_LEN]; 502fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 50326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 5040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::list<std::string>::iterator it; 505bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall const char *costName = "shared"; 5064a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 50826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall LOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 50926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 51026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 5118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ifaceName = ifn; 51226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 5130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { 5140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (*it == ifaceName) 515fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall break; 5164a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 5170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == sharedQuotaIfaces.end()) { 5188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("No such iface %s to delete", ifn); 519fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return -1; 5204a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 521fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 52226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= cleanupCostlyIface(ifn, QuotaShared); 5230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall sharedQuotaIfaces.erase(it); 524fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 5250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (sharedQuotaIfaces.empty()) { 526fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string quotaCmd; 527bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes); 52826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 5298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedQuotaBytes = 0; 5308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (sharedAlertBytes) { 5318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall removeSharedAlert(); 5328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedAlertBytes = 0; 5338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 534fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 5354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return res; 5364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 5370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 5380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) { 5390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char ifn[MAX_IFACENAME_LEN]; 5400dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 54126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 54226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *costName; 54326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<QuotaInfo>::iterator it; 54426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string quotaCmd; 5450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 5468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!maxBytes) { 5478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* Don't talk about -1, deprecate it. */ 5488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Invalid bytes value. 1..max_int64."); 5498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 5508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 5510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (maxBytes == -1) { 55226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return removeInterfaceQuota(iface); 5530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 5540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 5558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 55626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall LOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 55726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 55826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 55926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 56026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall costName = iface; 56126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 5620dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* Insert ingress quota. */ 5630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 5648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == ifaceName) 5650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall break; 5660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 5670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 5680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == quotaIfaces.end()) { 56926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= prepCostlyIface(ifn, QuotaUnique); 5700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); 57126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 5720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (res) { 5738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Failed set quota rule"); 5740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall goto fail; 5750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 5760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 5778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0)); 5780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 5790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } else { 5808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= updateQuota(costName, maxBytes); 5810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (res) { 5828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Failed update quota for %s", iface); 5830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall goto fail; 5840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 5858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall it->quota = maxBytes; 5860dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 5870dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return 0; 5880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 5890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall fail: 5900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* 5910dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse 5920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * rules in the kernel to see which ones need cleaning up. 5930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * For now callers needs to choose if they want to "ndc bandwidth enable" 5940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * which resets everything. 5950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall */ 5960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall removeInterfaceSharedQuota(ifn); 5970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return -1; 5980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 5990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) { 6018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return getInterfaceQuota("shared", bytes); 6028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 6038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) { 6058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall FILE *fp; 6068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *fname; 6078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int scanRes; 6088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&fname, "/proc/net/xt_quota/%s", costName); 6108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fp = fopen(fname, "r"); 6118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(fname); 6128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!fp) { 6138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Reading quota %s failed (%s)", costName, strerror(errno)); 6148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 6158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 6168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall scanRes = fscanf(fp, "%lld", bytes); 617db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall LOGV("Read quota res=%d bytes=%lld", scanRes, *bytes); 6188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fclose(fp); 6198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return scanRes == 1 ? 0 : -1; 6208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 6218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) { 6230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char ifn[MAX_IFACENAME_LEN]; 6250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 62626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 62726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *costName; 62826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<QuotaInfo>::iterator it; 6290dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 63126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall LOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 63226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 63326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 63426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 63526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall costName = iface; 6360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 6388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == ifaceName) 6390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall break; 6400dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == quotaIfaces.end()) { 6438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("No such iface %s to delete", ifn); 6440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return -1; 6450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* This also removes the quota command of CostlyIface chain. */ 64826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= cleanupCostlyIface(ifn, QuotaUnique); 6490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaIfaces.erase(it); 6510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 6530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 6548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) { 6568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall FILE *fp; 6578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *fname; 6588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&fname, "/proc/net/xt_quota/%s", quotaName); 6608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fp = fopen(fname, "w"); 6618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(fname); 6628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!fp) { 6638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Updating quota %s failed (%s)", quotaName, strerror(errno)); 6648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 6658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 6668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fprintf(fp, "%lld\n", bytes); 6678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fclose(fp); 6688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return 0; 6698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 6708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) { 6728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 6738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 674876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall const char *ifaceLimiting; 6758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 6768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall switch (op) { 6788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 6798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 6808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 6818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 6828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 6838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 6848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 6858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 6868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 6878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 6888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 6898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 690876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall ifaceLimiting = "! -i lo+"; 691876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "INPUT", 692876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall bytes, alertName, alertName); 6938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 6948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 695876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall ifaceLimiting = "! -o lo+"; 696876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "OUTPUT", 697876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall bytes, alertName, alertName); 6988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 6998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 7008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 7018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 7028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 703c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) { 704c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 705c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *opFlag; 706c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *ifaceLimiting; 7078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 708c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 709c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall switch (op) { 710c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpInsert: 711c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-I"; 712c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 713c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpReplace: 714c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-R"; 715c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 716c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall default: 717c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpDelete: 718c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-D"; 719c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 720c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 721c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 722c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall ifaceLimiting = "! -i lo+"; 723c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "FORWARD", 724c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall bytes, alertName, alertName); 725c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 726c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall free(alertQuotaCmd); 727c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 728c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 729c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 730c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) { 731c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 7328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 7338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 7358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Invalid bytes value. 1..max_int64."); 7368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 7378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 7388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (globalAlertBytes) { 7398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = updateQuota(alertName, bytes); 7408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } else { 7418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = runIptablesAlertCmd(IptOpInsert, alertName, bytes); 742c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (globalAlertTetherCount) { 743c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall LOGV("setGlobalAlert for %d tether", globalAlertTetherCount); 744c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes); 745c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 7468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 7478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall globalAlertBytes = bytes; 7488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 7498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 7508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 751c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlertInForwardChain(void) { 752c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 753c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 754c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 755c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount++; 756c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall LOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount); 757c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 758c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* 759c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is no globalAlert active we are done. 760c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is an active globalAlert but this is not the 1st 761c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * tether, we are also done. 762c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall */ 763c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertBytes || globalAlertTetherCount != 1) { 764c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return 0; 765c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 766c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 767c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* We only add the rule if this was the 1st tether added. */ 768c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes); 769c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 770c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 771c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 7728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) { 7738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 774c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 7758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 7768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!globalAlertBytes) { 7788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("No prior alert set"); 7798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 7808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 7818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes); 782c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (globalAlertTetherCount) { 783c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); 784c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 7858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall globalAlertBytes = 0; 7868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 7878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 7888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 789c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::removeGlobalAlertInForwardChain(void) { 790c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 791c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 792c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 793c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertTetherCount) { 794c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall LOGE("No prior alert set"); 795c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return -1; 796c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 797c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 798c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount--; 799c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* 800c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is no globalAlert active we are done. 801c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is an active globalAlert but there are more 802c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * tethers, we are also done. 803c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall */ 804c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertBytes || globalAlertTetherCount >= 1) { 805c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return 0; 806c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 807c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 808c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* We only detete the rule if this was the last tether removed. */ 809c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); 810c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 811c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 812c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 8138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) { 8148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!sharedQuotaBytes) { 8158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Need to have a prior shared quota set to set an alert"); 8168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 8198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Invalid bytes value. 1..max_int64."); 8208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return setCostlyAlert("shared", bytes, &sharedAlertBytes); 8238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) { 8268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return removeCostlyAlert("shared", &sharedAlertBytes); 8278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) { 8308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::list<QuotaInfo>::iterator it; 8318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 8338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Invalid bytes value. 1..max_int64."); 8348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 8378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == iface) 8388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 8398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it == quotaIfaces.end()) { 8428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Need to have a prior interface quota set to set an alert"); 8438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return setCostlyAlert(iface, bytes, &it->alert); 8478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) { 8508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::list<QuotaInfo>::iterator it; 8518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 8538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == iface) 8548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 8558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it == quotaIfaces.end()) { 8588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("No prior alert set for interface %s", iface); 8598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return removeCostlyAlert(iface, &it->alert); 8638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) { 8668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 8678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *chainNameAndPos; 8688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 8698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertName; 8708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 8728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("Invalid bytes value. 1..max_int64."); 8738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&alertName, "%sAlert", costName); 8768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (*alertBytes) { 8778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = updateQuota(alertName, *alertBytes); 8788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } else { 8798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN); 8808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-I", chainNameAndPos, bytes, alertName, 8818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall alertName); 8828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 8838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 8848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(chainNameAndPos); 8858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *alertBytes = bytes; 8878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertName); 8888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 8898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) { 8928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 8938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *chainName; 8948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertName; 8958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 8968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&alertName, "%sAlert", costName); 8988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!*alertBytes) { 8998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall LOGE("No prior alert set for %s alert", costName); 9008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&chainName, "costly_%s", costName); 9048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName, alertName); 9058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 9068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 9078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(chainName); 9088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *alertBytes = 0; 9108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertName); 9118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 9128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 913db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 914db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* 915db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of: 916db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) 917db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * pkts bytes target prot opt in out source destination 918db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * 0 0 ACCEPT all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 919db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * 0 0 DROP all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 state INVALID 920db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * 0 0 ACCEPT all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 921db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * 922db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 923db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallint BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp) { 924db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int res; 925db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN]; 926db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char iface0[MAX_IPT_OUTPUT_LINE_LEN]; 927db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char iface1[MAX_IPT_OUTPUT_LINE_LEN]; 928db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char rest[MAX_IPT_OUTPUT_LINE_LEN]; 929db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 930db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char *buffPtr; 931db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t packets, bytes; 932db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 933db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) { 934db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Clean up, so a failed parse can still print info */ 935db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iface0[0] = iface1[0] = rest[0] = packets = bytes = 0; 936db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall res = sscanf(buffPtr, "%lld %lld ACCEPT all -- %s %s 0.%s", 937db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall &packets, &bytes, iface0, iface1, rest); 938db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall LOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res, 939db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iface0, iface1, packets, bytes, rest, buffPtr); 940db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (res != 5) { 941db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall continue; 942db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 943db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) { 944db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall LOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); 945db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.rxPackets = packets; 946db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.rxBytes = bytes; 947db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) { 948db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall LOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets); 949db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.txPackets = packets; 950db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.txBytes = bytes; 951db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 952db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 953db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Failure if rx or tx was not found */ 954db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0; 955db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 956db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 957db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 958db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) { 959db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char *msg; 960db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(), 961db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall rxBytes, rxPackets, txBytes, txPackets); 962db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return msg; 963db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 964db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 965db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallint BandwidthController::getTetherStats(TetherStats &stats) { 966db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int res; 967db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall std::string fullCmd; 968db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall FILE *iptOutput; 969db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall const char *cmd; 970db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 971db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (stats.rxBytes != -1 || stats.txBytes != -1) { 972db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall LOGE("Unexpected input stats. Byte counts should be -1."); 973db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return -1; 974db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 975db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 976db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* 977db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Why not use some kind of lib to talk to iptables? 978db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Because the only libs are libiptc and libip6tc in iptables, and they are 979db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * not easy to use. They require the known iptables match modules to be 980db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * preloaded/linked, and require apparently a lot of wrapper code to get 981db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * the wanted info. 982db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 983db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall fullCmd = IPTABLES_PATH; 984db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall fullCmd += " -nvx -L FORWARD"; 985db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iptOutput = popen(fullCmd.c_str(), "r"); 986db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (!iptOutput) { 987db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall LOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno)); 988db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return -1; 989db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 990db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall res = parseForwardChainStats(stats, iptOutput); 991db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall pclose(iptOutput); 992db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 993db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Currently NatController doesn't do ipv6 tethering, so we are done. */ 994db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return res; 995db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 996