BandwidthController.cpp revision ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63
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 44c4bbfa247dbe4dda17e28694d49afd3ec0b06badGlenn Kastenextern "C" int logwrap(int argc, const char **argv); 459e5e0ce62e88ddf9a09798eda51b0c270d354c8eJP Abgrallextern "C" int system_nosh(const char *command); 464a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 470031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall#include "NetdConstants.h" 484a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include "BandwidthController.h" 494a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 50db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* Alphabetical */ 51c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich#define ALERT_IPT_TEMPLATE "%s %s %s -m quota2 ! --quota %lld --name %s" 52db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int BandwidthController::ALERT_RULE_POS_IN_COSTLY_CHAIN = 4; 53c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallconst char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert"; 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. 650031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * E.g. "-I INPUT -i rmnet0 --jump 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 720031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * iptables -I INPUT -i iface0 --jump costly_shared 730031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * iptables -I OUTPUT -o iface0 --jump 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.: 800031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * iptables -I INPUT -i iface1 --jump costly_shared 810031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * iptables -I OUTPUT -o iface1 --jump 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 860031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * iptables -I INPUT -i iface0 --jump costly_iface0 870031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * iptables -I OUTPUT -o iface0 --jump 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 */ 990031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_FLUSH_COMMANDS[] = { 1000031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall /* 1010031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Cleanup rules. 1020031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Should normally include costly_<iface>, but we rely on the way they are setup 1030031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * to allow coexistance. 10439f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall */ 1050031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-F bw_INPUT", 1060031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-F bw_OUTPUT", 1070031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-F bw_FORWARD", 1080031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-F penalty_box", 1090031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-F costly_shared", 1100031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall}; 1110031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 1120031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall/* The cleanup commands assume flushing has been done. */ 1130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_CLEANUP_COMMANDS[] = { 1140031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall /* Delete hooks to custom chains. */ 1150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-D INPUT -j bw_INPUT", 1160031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-D OUTPUT -j bw_OUTPUT", 1170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-D FORWARD -j bw_FORWARD", 1180031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X bw_INPUT", 1190031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X bw_OUTPUT", 1200031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X bw_FORWARD", 1210031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X penalty_box", 1220031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X costly_shared", 1230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1244a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 125db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_SETUP_COMMANDS[] = { 1260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* Created needed chains. */ 1270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-N bw_INPUT", 1280031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A INPUT -j bw_INPUT", 1290031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 1300031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-N bw_OUTPUT", 1310031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A OUTPUT -j bw_OUTPUT", 1320031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 1330031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-N bw_FORWARD", 1340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-I FORWARD -j bw_FORWARD", 1350031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 136bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-N costly_shared", 1370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-N penalty_box", 1380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 140db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = { 1410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_INPUT -i lo --jump RETURN", 1420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */ 1430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 1440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_OUTPUT -o lo --jump RETURN", 1450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */ 1460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 147bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-A costly_shared --jump penalty_box", 148bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-A costly_shared -m owner --socket-exists", /* This is a tracking rule. */ 1490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1504a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 1514a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP AbgrallBandwidthController::BandwidthController(void) { 1524a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char value[PROPERTY_VALUE_MAX]; 1534a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 15411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall property_get("persist.bandwidth.uselogwrap", value, "0"); 15511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall useLogwrapCall = !strcmp(value, "1"); 1564a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 1574a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 158ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrallint BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling, 159ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall IptFailureLog failureHandling) { 1600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 1618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 1623fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("runIpxtablesCmd(cmd=%s)", cmd); 163ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res |= runIptablesCmd(cmd, rejectHandling, IptIpV4, failureHandling); 164ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res |= runIptablesCmd(cmd, rejectHandling, IptIpV6, failureHandling); 1650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 1660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 1670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 16826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) { 16926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 17026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0' 17126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall strncpy(buffer, src, buffSize); 17226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return buffer[buffSize - 1]; 17326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall} 17426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 1758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, 176ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall IptIpVer iptVer, IptFailureLog failureHandling) { 17726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall char buffer[MAX_CMD_LEN]; 1784a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall const char *argv[MAX_CMD_ARGS]; 17926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall int argc = 0; 1804a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char *next = buffer; 1814a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char *tmp; 18211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall int res; 1834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 1840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string fullCmd = cmd; 18526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 18626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (rejectHandling == IptRejectAdd) { 1870dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall fullCmd += " --jump REJECT --reject-with"; 18826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (iptVer) { 18926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case IptIpV4: 1908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fullCmd += " icmp-net-prohibited"; 1918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 19226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case IptIpV6: 1938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fullCmd += " icmp6-adm-prohibited"; 1948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 1950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 1960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 1970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 19811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall fullCmd.insert(0, " "); 19911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH); 2004a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 20111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (!useLogwrapCall) { 2029e5e0ce62e88ddf9a09798eda51b0c270d354c8eJP Abgrall res = system_nosh(fullCmd.c_str()); 20311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } else { 20411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) { 2055ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("iptables command too long"); 2064a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return -1; 2074a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 208fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 20911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argc = 0; 21011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall while ((tmp = strsep(&next, " "))) { 21111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argv[argc++] = tmp; 21211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (argc >= MAX_CMD_ARGS) { 2135ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("iptables argument overflow"); 21411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall return -1; 21511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 21611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 21711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall 21811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argv[argc] = NULL; 219c4bbfa247dbe4dda17e28694d49afd3ec0b06badGlenn Kasten res = logwrap(argc, argv); 22011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 221ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall if (res && failureHandling == IptFailShow) { 2225ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("runIptablesCmd(): failed %s res=%d", fullCmd.c_str(), res); 22311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 22411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall return res; 2254a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2264a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::setupIptablesHooks(void) { 2280031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2290031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall /* Some of the initialCommands are allowed to fail */ 2300031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), 2310031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_FLUSH_COMMANDS, RunCmdFailureOk); 2320031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2330031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*), 2340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_CLEANUP_COMMANDS, RunCmdFailureOk); 2350031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2360031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*), 2370031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_SETUP_COMMANDS, RunCmdFailureBad); 2380031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2390031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return 0; 2400031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall} 2420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::enableBandwidthControl(bool force) { 244fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res; 2450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall char value[PROPERTY_VALUE_MAX]; 2460031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2470031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall if (!force) { 2480031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall property_get("persist.bandwidth.enable", value, "1"); 2490031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall if (!strcmp(value, "0")) 2500031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return 0; 2510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall } 2528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 253db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Let's pretend we started from scratch ... */ 2548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedQuotaIfaces.clear(); 2558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall quotaIfaces.clear(); 2568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall naughtyAppUids.clear(); 257db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall globalAlertBytes = 0; 258c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount = 0; 259db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall sharedQuotaBytes = sharedAlertBytes = 0; 260db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 2610031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), 2620031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_FLUSH_COMMANDS, RunCmdFailureOk); 263db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 2640031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*), 265db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad); 2668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 267fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 2684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2714a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::disableBandwidthControl(void) { 2720031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), 2730031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_FLUSH_COMMANDS, RunCmdFailureOk); 274fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return 0; 2754a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2764a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runCommands(int numCommands, const char *commands[], 2788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall RunCmdErrHandling cmdErrHandling) { 279fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 280ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall IptFailureLog failureLogging = IptFailShow; 281ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall if (cmdErrHandling == RunCmdFailureOk) { 282ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall failureLogging = IptFailHide; 283ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall } 2843fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("runCommands(): %d commands", numCommands); 285fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) { 286ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd, failureLogging); 2870031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall if (res && cmdErrHandling != RunCmdFailureOk) 288fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 289fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 2900031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return 0; 291fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 292fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 2930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallstd::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) { 294fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string res; 2958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *buff; 2968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 297fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 298fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall switch (op) { 2998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 3008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 3018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 3038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 3048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 3068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 3078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 3088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 309fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 3108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&buff, "%s penalty_box -m owner --uid-owner %d", opFlag, uid); 3118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = buff; 3128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(buff); 313fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 314fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 315fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 316fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::addNaughtyApps(int numUids, char *appUids[]) { 31726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd); 318fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 319fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 320fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) { 32126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove); 322fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 323fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 32426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) { 325fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall char cmd[MAX_CMD_LEN]; 326fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int uidNum; 32726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *failLogTemplate; 32826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall IptOp op; 329fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int appUids[numUids]; 33026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string naughtyCmd; 3318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 33226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (appOp) { 33326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case NaughtyAppOpAdd: 3348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall op = IptOpInsert; 3358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall failLogTemplate = "Failed to add app uid %d to penalty box."; 3368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 33726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case NaughtyAppOpRemove: 3388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall op = IptOpDelete; 3398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall failLogTemplate = "Failed to delete app uid %d from penalty box."; 3408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall default: 3420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall ALOGE("Unexpected app Op %d", appOp); 3430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return -1; 34426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 34526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 346fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (uidNum = 0; uidNum < numUids; uidNum++) { 347fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall appUids[uidNum] = atol(appStrUids[uidNum]); 348fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (appUids[uidNum] == 0) { 3495ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE(failLogTemplate, appUids[uidNum]); 350fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail_parse; 351fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 352fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 353fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 354fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (uidNum = 0; uidNum < numUids; uidNum++) { 35526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall naughtyCmd = makeIptablesNaughtyCmd(op, appUids[uidNum]); 35626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) { 3575ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE(failLogTemplate, appUids[uidNum]); 358fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail_with_uidNum; 359fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 360fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 361fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return 0; 362fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 36326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum: 364fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall /* Try to remove the uid that failed in any case*/ 36526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]); 36626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd); 36726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse: 36826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 3694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 3704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 37126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) { 372fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string res; 3738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *buff; 3748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 3750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 3763fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota); 3770dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 378fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall switch (op) { 3798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 3808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 3818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 3838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 3848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 3868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 3878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 3888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 389fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 3908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 391bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall // The requried IP version specific --jump REJECT ... will be added later. 3928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota, 3938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall costName); 3948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = buff; 3958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(buff); 3960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 3970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 3980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 39926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) { 4000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char cmd[MAX_CMD_LEN]; 4010031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall int res = 0, res1, res2; 4028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int ruleInsertPos = 1; 4030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string costString; 4040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall const char *costCString; 4050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 4060dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* The "-N costly" is created upfront, no need to handle it here. */ 40726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (quotaType) { 40826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaUnique: 409bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costString = "costly_"; 4100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costString += ifn; 4110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costCString = costString.c_str(); 4120031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall /* 4130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Flush the costly_<iface> is allowed to fail in case it didn't exist. 4140031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Creating a new one is allowed to fail in case it existed. 4150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * This helps with netd restarts. 4160031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall */ 4170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-F %s", costCString); 418ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res1 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-N %s", costCString); 420ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res2 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4210031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res = (res1 && res2) || (!res1 && !res2); 4220031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString); 42426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-A %s -m owner --socket-exists", costCString); 42626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 42726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 42826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaShared: 429bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costCString = "costly_shared"; 43026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 4310031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall default: 4320031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall ALOGE("Unexpected quotatype %d", quotaType); 4330031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return -1; 4340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 4350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 4368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (globalAlertBytes) { 4378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* The alert rule comes 1st */ 4388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ruleInsertPos = 2; 4398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 4400031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); 442ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString); 44526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4460031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4470031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); 448ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4490031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4500031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString); 45126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 4530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 4540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 45526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) { 4560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char cmd[MAX_CMD_LEN]; 4570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 4580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string costString; 4590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall const char *costCString; 4600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 46126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (quotaType) { 46226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaUnique: 463bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costString = "costly_"; 4640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costString += ifn; 4650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costCString = costString.c_str(); 46626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 46726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaShared: 468bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costCString = "costly_shared"; 46926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 4700031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall default: 4710031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall ALOGE("Unexpected quotatype %d", quotaType); 4720031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return -1; 4730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 4740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 4750031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); 47626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4770031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); 47826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 480bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall /* The "-N costly_shared" is created upfront, no need to handle it here. */ 48126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (quotaType == QuotaUnique) { 4820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-F %s", costCString); 48326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 484a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall snprintf(cmd, sizeof(cmd), "-X %s", costCString); 485a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 486fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 487fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 488fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 4894a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 4900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) { 4914a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char cmd[MAX_CMD_LEN]; 4924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char ifn[MAX_IFACENAME_LEN]; 493fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 49426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string quotaCmd; 4958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::string ifaceName; 4968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ; 497bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall const char *costName = "shared"; 49826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<std::string>::iterator it; 4994a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!maxBytes) { 5018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* Don't talk about -1, deprecate it. */ 5025ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 5038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 5048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 50526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 5065ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 50726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 50826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 50926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 5104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5114a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall if (maxBytes == -1) { 512fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return removeInterfaceSharedQuota(ifn); 5134a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 5144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5154a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall /* Insert ingress quota. */ 5160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { 5170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (*it == ifaceName) 518fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall break; 5194a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 520fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 5210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == sharedQuotaIfaces.end()) { 52226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= prepCostlyIface(ifn, QuotaShared); 5230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (sharedQuotaIfaces.empty()) { 5240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); 52526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 5264a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall if (res) { 5275ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed set quota rule"); 528fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail; 5294a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 530fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall sharedQuotaBytes = maxBytes; 531fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 5320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall sharedQuotaIfaces.push_front(ifaceName); 533fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 534fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 535fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 536fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (maxBytes != sharedQuotaBytes) { 5378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= updateQuota(costName, maxBytes); 538fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (res) { 5395ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed update quota for %s", costName); 540fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail; 541fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 542fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall sharedQuotaBytes = maxBytes; 5434a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 5444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return 0; 545fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 546fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall fail: 5474a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall /* 5484a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse 5494a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * rules in the kernel to see which ones need cleaning up. 550fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall * For now callers needs to choose if they want to "ndc bandwidth enable" 551fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall * which resets everything. 5524a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */ 553fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall removeInterfaceSharedQuota(ifn); 5544a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return -1; 5554a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 5564a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */ 558fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) { 5594a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char ifn[MAX_IFACENAME_LEN]; 560fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 56126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 5620dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::list<std::string>::iterator it; 563bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall const char *costName = "shared"; 5644a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 5665ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 56726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 56826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 5698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ifaceName = ifn; 57026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 5710dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { 5720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (*it == ifaceName) 573fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall break; 5744a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 5750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == sharedQuotaIfaces.end()) { 5765ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No such iface %s to delete", ifn); 577fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return -1; 5784a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 579fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 58026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= cleanupCostlyIface(ifn, QuotaShared); 5810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall sharedQuotaIfaces.erase(it); 582fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 5830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (sharedQuotaIfaces.empty()) { 584fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string quotaCmd; 585bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes); 58626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 5878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedQuotaBytes = 0; 5888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (sharedAlertBytes) { 5898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall removeSharedAlert(); 5908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedAlertBytes = 0; 5918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 592fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 5934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return res; 5944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 5950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 5960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) { 5970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char ifn[MAX_IFACENAME_LEN]; 5980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 59926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 60026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *costName; 60126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<QuotaInfo>::iterator it; 60226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string quotaCmd; 6030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!maxBytes) { 6058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* Don't talk about -1, deprecate it. */ 6065ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 6078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 6088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 6090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (maxBytes == -1) { 61026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return removeInterfaceQuota(iface); 6110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 6145ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 61526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 61626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 61726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 61826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall costName = iface; 61926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 6200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* Insert ingress quota. */ 6210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 6228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == ifaceName) 6230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall break; 6240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == quotaIfaces.end()) { 62726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= prepCostlyIface(ifn, QuotaUnique); 6280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); 62926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 6300dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (res) { 6315ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed set quota rule"); 6320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall goto fail; 6330dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0)); 6360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } else { 6388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= updateQuota(costName, maxBytes); 6390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (res) { 6405ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed update quota for %s", iface); 6410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall goto fail; 6420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall it->quota = maxBytes; 6440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return 0; 6460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall fail: 6480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* 6490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse 6500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * rules in the kernel to see which ones need cleaning up. 6510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * For now callers needs to choose if they want to "ndc bandwidth enable" 6520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * which resets everything. 6530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall */ 6540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall removeInterfaceSharedQuota(ifn); 6550dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return -1; 6560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 6570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) { 6598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return getInterfaceQuota("shared", bytes); 6608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 6618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) { 6638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall FILE *fp; 6648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *fname; 6658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int scanRes; 6668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&fname, "/proc/net/xt_quota/%s", costName); 6688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fp = fopen(fname, "r"); 6698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(fname); 6708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!fp) { 6715ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Reading quota %s failed (%s)", costName, strerror(errno)); 6728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 6738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 6748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall scanRes = fscanf(fp, "%lld", bytes); 6753fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes); 6768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fclose(fp); 6778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return scanRes == 1 ? 0 : -1; 6788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 6798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) { 6810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char ifn[MAX_IFACENAME_LEN]; 6830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 68426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 68526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *costName; 68626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<QuotaInfo>::iterator it; 6870dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 6895ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 69026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 69126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 69226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 69326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall costName = iface; 6940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 6968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == ifaceName) 6970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall break; 6980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == quotaIfaces.end()) { 7015ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No such iface %s to delete", ifn); 7020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return -1; 7030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 7040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* This also removes the quota command of CostlyIface chain. */ 70626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= cleanupCostlyIface(ifn, QuotaUnique); 7070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaIfaces.erase(it); 7090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 7110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 7128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) { 7148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall FILE *fp; 7158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *fname; 7168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&fname, "/proc/net/xt_quota/%s", quotaName); 7188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fp = fopen(fname, "w"); 7198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(fname); 7208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!fp) { 7215ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno)); 7228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 7238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 7248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fprintf(fp, "%lld\n", bytes); 7258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fclose(fp); 7268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return 0; 7278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 7288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) { 7308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 7318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 732876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall const char *ifaceLimiting; 7338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 7348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall switch (op) { 7368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 7378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 7388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 7398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 7408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 7418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 7428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 7438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 7448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 7458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 7468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 7478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 748876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall ifaceLimiting = "! -i lo+"; 7490031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_INPUT", 750c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich bytes, alertName); 7518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 7528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 753876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall ifaceLimiting = "! -o lo+"; 7540031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_OUTPUT", 755c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich bytes, alertName); 7568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 7578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 7588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 7598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 7608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 761c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) { 762c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 763c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *opFlag; 764c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *ifaceLimiting; 7658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 766c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 767c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall switch (op) { 768c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpInsert: 769c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-I"; 770c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 771c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpReplace: 772c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-R"; 773c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 774c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall default: 775c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpDelete: 776c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-D"; 777c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 778c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 779c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 780c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall ifaceLimiting = "! -i lo+"; 7810031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_FORWARD", 782c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich bytes, alertName); 783c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 784c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall free(alertQuotaCmd); 785c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 786c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 787c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 788c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) { 789c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 7908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 7918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 7935ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 7948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 7958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 7968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (globalAlertBytes) { 7978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = updateQuota(alertName, bytes); 7988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } else { 7998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = runIptablesAlertCmd(IptOpInsert, alertName, bytes); 800c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (globalAlertTetherCount) { 8013fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount); 802c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes); 803c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 8048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall globalAlertBytes = bytes; 8068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 8078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 809c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlertInForwardChain(void) { 810c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 811c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 812c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 813c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount++; 8143fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount); 815c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 816c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* 817c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is no globalAlert active we are done. 818c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is an active globalAlert but this is not the 1st 819c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * tether, we are also done. 820c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall */ 821c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertBytes || globalAlertTetherCount != 1) { 822c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return 0; 823c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 824c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 825c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* We only add the rule if this was the 1st tether added. */ 826c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes); 827c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 828c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 829c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 8308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) { 8318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 832c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 8338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 8348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!globalAlertBytes) { 8365ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set"); 8378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes); 840c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (globalAlertTetherCount) { 841c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); 842c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 8438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall globalAlertBytes = 0; 8448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 8458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 847c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::removeGlobalAlertInForwardChain(void) { 848c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 849c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 850c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 851c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertTetherCount) { 8525ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set"); 853c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return -1; 854c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 855c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 856c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount--; 857c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* 858c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is no globalAlert active we are done. 859c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is an active globalAlert but there are more 860c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * tethers, we are also done. 861c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall */ 862c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertBytes || globalAlertTetherCount >= 1) { 863c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return 0; 864c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 865c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 866c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* We only detete the rule if this was the last tether removed. */ 867c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); 868c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 869c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 870c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 8718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) { 8728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!sharedQuotaBytes) { 8735ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Need to have a prior shared quota set to set an alert"); 8748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 8775ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 8788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return setCostlyAlert("shared", bytes, &sharedAlertBytes); 8818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) { 8848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return removeCostlyAlert("shared", &sharedAlertBytes); 8858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) { 8888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::list<QuotaInfo>::iterator it; 8898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 8915ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 8928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 8958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == iface) 8968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 8978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it == quotaIfaces.end()) { 9005ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Need to have a prior interface quota set to set an alert"); 9018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return setCostlyAlert(iface, bytes, &it->alert); 9058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) { 9088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::list<QuotaInfo>::iterator it; 9098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 9118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == iface) 9128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 9138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it == quotaIfaces.end()) { 9165ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set for interface %s", iface); 9178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return removeCostlyAlert(iface, &it->alert); 9218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) { 9248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 9258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *chainNameAndPos; 9268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 9278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertName; 9288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 9305ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 9318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&alertName, "%sAlert", costName); 9348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (*alertBytes) { 9358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = updateQuota(alertName, *alertBytes); 9368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } else { 9378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN); 938c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-I", chainNameAndPos, bytes, alertName); 9398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 9408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 9418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(chainNameAndPos); 9428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *alertBytes = bytes; 9448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertName); 9458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 9468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) { 9498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 9508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *chainName; 9518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertName; 9528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 9538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&alertName, "%sAlert", costName); 9558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!*alertBytes) { 9565ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set for %s alert", costName); 9578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&chainName, "costly_%s", costName); 961c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-D", chainName, *alertBytes, alertName); 9628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 9638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 9648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(chainName); 9658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *alertBytes = 0; 9678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertName); 9688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 9698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 970db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 971db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* 972db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of: 9730031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Chain FORWARD (policy RETURN 0 packets, 0 bytes) 974db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * pkts bytes target prot opt in out source destination 9750031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * 0 0 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 976db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * 0 0 DROP all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 state INVALID 9770031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * 0 0 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 978db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * 979db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 980a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp, 981a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall std::string &extraProcessingInfo) { 982db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int res; 983db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN]; 984db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char iface0[MAX_IPT_OUTPUT_LINE_LEN]; 985db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char iface1[MAX_IPT_OUTPUT_LINE_LEN]; 986db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char rest[MAX_IPT_OUTPUT_LINE_LEN]; 987db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 988db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char *buffPtr; 989db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t packets, bytes; 990db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 991db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) { 992db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Clean up, so a failed parse can still print info */ 993db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iface0[0] = iface1[0] = rest[0] = packets = bytes = 0; 9940031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s", 995db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall &packets, &bytes, iface0, iface1, rest); 9963fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res, 997db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iface0, iface1, packets, bytes, rest, buffPtr); 998a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall extraProcessingInfo += buffPtr; 999a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall 1000db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (res != 5) { 1001db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall continue; 1002db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1003db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) { 10043fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); 1005db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.rxPackets = packets; 1006db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.rxBytes = bytes; 1007db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) { 10083fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets); 1009db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.txPackets = packets; 1010db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.txBytes = bytes; 1011db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1012db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1013db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Failure if rx or tx was not found */ 1014db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0; 1015db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 1016db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1017db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1018db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) { 1019db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char *msg; 1020db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(), 1021db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall rxBytes, rxPackets, txBytes, txPackets); 1022db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return msg; 1023db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 1024db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1025a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::getTetherStats(TetherStats &stats, std::string &extraProcessingInfo) { 1026db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int res; 1027db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall std::string fullCmd; 1028db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall FILE *iptOutput; 1029db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall const char *cmd; 1030db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1031db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (stats.rxBytes != -1 || stats.txBytes != -1) { 10325ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Unexpected input stats. Byte counts should be -1."); 1033db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return -1; 1034db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1035db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1036db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* 1037db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Why not use some kind of lib to talk to iptables? 1038db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Because the only libs are libiptc and libip6tc in iptables, and they are 1039db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * not easy to use. They require the known iptables match modules to be 1040db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * preloaded/linked, and require apparently a lot of wrapper code to get 1041db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * the wanted info. 1042db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 1043db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall fullCmd = IPTABLES_PATH; 10440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall fullCmd += " -nvx -L natctrl_FORWARD"; 1045db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iptOutput = popen(fullCmd.c_str(), "r"); 1046db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (!iptOutput) { 10475ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno)); 1048a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall extraProcessingInfo += "Failed to run iptables."; 1049db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return -1; 1050db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1051a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall res = parseForwardChainStats(stats, iptOutput, extraProcessingInfo); 1052db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall pclose(iptOutput); 1053db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1054db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Currently NatController doesn't do ipv6 tethering, so we are done. */ 1055db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return res; 1056db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 1057