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"; 548e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_INPUT = "bw_INPUT"; 558e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_FORWARD = "bw_FORWARD"; 568e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_OUTPUT = "bw_OUTPUT"; 578e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_RAW_PREROUTING = "bw_raw_PREROUTING"; 588e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_MANGLE_POSTROUTING = "bw_mangle_POSTROUTING"; 59db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int BandwidthController::MAX_CMD_ARGS = 32; 60db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int BandwidthController::MAX_CMD_LEN = 1024; 61db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int BandwidthController::MAX_IFACENAME_LEN = 64; 62db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256; 63db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 6411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrallbool BandwidthController::useLogwrapCall = false; 654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall/** 674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Some comments about the rules: 684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * Ordering 694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains. 7029e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall * E.g. "-I bw_INPUT -i rmnet0 --jump costly" 714a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * - quota'd rules in the costly chain should be before penalty_box lookups. 7229e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall * - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains. 734a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 744a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * global quota vs per interface quota 754a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * - global quota for all costly interfaces uses a single costly chain: 764a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * . initial rules 77bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -N costly_shared 7829e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall * iptables -I bw_INPUT -i iface0 --jump costly_shared 7929e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall * iptables -I bw_OUTPUT -o iface0 --jump costly_shared 80bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -I costly_shared -m quota \! --quota 500000 \ 81bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * --jump REJECT --reject-with icmp-net-prohibited 82bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -A costly_shared --jump penalty_box 838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall * 844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * . adding a new iface to this, E.g.: 8529e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall * iptables -I bw_INPUT -i iface1 --jump costly_shared 8629e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall * iptables -I bw_OUTPUT -o iface1 --jump costly_shared 874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 884a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * - quota per interface. This is achieve by having "costly" chains per quota. 894a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * E.g. adding a new costly interface iface0 with its own quota: 904a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * iptables -N costly_iface0 9129e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall * iptables -I bw_INPUT -i iface0 --jump costly_iface0 9229e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall * iptables -I bw_OUTPUT -o iface0 --jump costly_iface0 93bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -A costly_iface0 -m quota \! --quota 500000 \ 94bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * --jump REJECT --reject-with icmp-net-prohibited 95bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -A costly_iface0 --jump penalty_box 964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 974a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * penalty_box handling: 984a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * - only one penalty_box for all interfaces 994a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * E.g Adding an app: 100bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * iptables -A penalty_box -m owner --uid-owner app_3 \ 101bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall * --jump REJECT --reject-with icmp-net-prohibited 1024a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */ 1030031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_FLUSH_COMMANDS[] = { 1040031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall /* 1050031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Cleanup rules. 1060031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Should normally include costly_<iface>, but we rely on the way they are setup 1070031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * to allow coexistance. 10839f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall */ 1090031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-F bw_INPUT", 1100031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-F bw_OUTPUT", 1110031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-F bw_FORWARD", 1120031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-F penalty_box", 1130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-F costly_shared", 114f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall 115f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t raw -F bw_raw_PREROUTING", 116f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t mangle -F bw_mangle_POSTROUTING", 1170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall}; 1180031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 1190031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall/* The cleanup commands assume flushing has been done. */ 1200031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_CLEANUP_COMMANDS[] = { 1210031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X penalty_box", 1220031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X costly_shared", 1230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1244a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 125db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_SETUP_COMMANDS[] = { 126bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-N costly_shared", 1270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-N penalty_box", 1280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1290dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 130db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = { 1310031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_INPUT -i lo --jump RETURN", 1320031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */ 1330dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 1340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_OUTPUT -o lo --jump RETURN", 1350031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */ 1360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 137bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-A costly_shared --jump penalty_box", 138f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall 139f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t raw -A bw_raw_PREROUTING ! -i lo+ -m owner --socket-exists", /* This is a tracking rule. */ 140f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t mangle -A bw_mangle_POSTROUTING ! -o lo+ -m owner --socket-exists", /* This is a tracking rule. */ 1410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1424a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 1434a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP AbgrallBandwidthController::BandwidthController(void) { 1444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char value[PROPERTY_VALUE_MAX]; 1454a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 14611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall property_get("persist.bandwidth.uselogwrap", value, "0"); 14711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall useLogwrapCall = !strcmp(value, "1"); 1484a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 1494a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 150ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrallint BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling, 151ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall IptFailureLog failureHandling) { 1520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 1538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 1543fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("runIpxtablesCmd(cmd=%s)", cmd); 155ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res |= runIptablesCmd(cmd, rejectHandling, IptIpV4, failureHandling); 156ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res |= runIptablesCmd(cmd, rejectHandling, IptIpV6, failureHandling); 1570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 1580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 1590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 16026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) { 16126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 16226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0' 16326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall strncpy(buffer, src, buffSize); 16426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return buffer[buffSize - 1]; 16526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall} 16626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 1678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, 168ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall IptIpVer iptVer, IptFailureLog failureHandling) { 16926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall char buffer[MAX_CMD_LEN]; 1704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall const char *argv[MAX_CMD_ARGS]; 17126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall int argc = 0; 1724a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char *next = buffer; 1734a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char *tmp; 17411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall int res; 1754a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 1760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string fullCmd = cmd; 17726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 17826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (rejectHandling == IptRejectAdd) { 1790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall fullCmd += " --jump REJECT --reject-with"; 18026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (iptVer) { 18126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case IptIpV4: 1828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fullCmd += " icmp-net-prohibited"; 1838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 18426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case IptIpV6: 1858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fullCmd += " icmp6-adm-prohibited"; 1868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 1870dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 1880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 1890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 19011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall fullCmd.insert(0, " "); 19111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH); 1924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 19311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (!useLogwrapCall) { 1949e5e0ce62e88ddf9a09798eda51b0c270d354c8eJP Abgrall res = system_nosh(fullCmd.c_str()); 19511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } else { 19611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) { 1975ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("iptables command too long"); 1984a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return -1; 1994a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 200fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 20111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argc = 0; 20211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall while ((tmp = strsep(&next, " "))) { 20311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argv[argc++] = tmp; 20411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (argc >= MAX_CMD_ARGS) { 2055ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("iptables argument overflow"); 20611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall return -1; 20711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 20811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 20911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall 21011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argv[argc] = NULL; 211c4bbfa247dbe4dda17e28694d49afd3ec0b06badGlenn Kasten res = logwrap(argc, argv); 21211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 213ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall if (res && failureHandling == IptFailShow) { 2145ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("runIptablesCmd(): failed %s res=%d", fullCmd.c_str(), res); 21511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 21611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall return res; 2174a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2184a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2190031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::setupIptablesHooks(void) { 2200031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2210031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall /* Some of the initialCommands are allowed to fail */ 2220031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), 2230031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_FLUSH_COMMANDS, RunCmdFailureOk); 2240031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2250031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*), 2260031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_CLEANUP_COMMANDS, RunCmdFailureOk); 2270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2280031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*), 2290031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_SETUP_COMMANDS, RunCmdFailureBad); 2300031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2310031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return 0; 2320031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall} 2330031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::enableBandwidthControl(bool force) { 235fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res; 2360031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall char value[PROPERTY_VALUE_MAX]; 2370031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2380031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall if (!force) { 2390031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall property_get("persist.bandwidth.enable", value, "1"); 2400031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall if (!strcmp(value, "0")) 2410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return 0; 2420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall } 2438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 244db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Let's pretend we started from scratch ... */ 2458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedQuotaIfaces.clear(); 2468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall quotaIfaces.clear(); 2478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall naughtyAppUids.clear(); 248db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall globalAlertBytes = 0; 249c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount = 0; 250db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall sharedQuotaBytes = sharedAlertBytes = 0; 251db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 2520031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), 2530031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_FLUSH_COMMANDS, RunCmdFailureOk); 254db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 2550031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*), 256db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad); 2578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 258fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 2594a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2604a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2614a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2624a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::disableBandwidthControl(void) { 2630031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), 2640031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_FLUSH_COMMANDS, RunCmdFailureOk); 265fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return 0; 2664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runCommands(int numCommands, const char *commands[], 2698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall RunCmdErrHandling cmdErrHandling) { 270fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 271ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall IptFailureLog failureLogging = IptFailShow; 272ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall if (cmdErrHandling == RunCmdFailureOk) { 273ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall failureLogging = IptFailHide; 274ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall } 2753fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("runCommands(): %d commands", numCommands); 276fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) { 277ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd, failureLogging); 2780031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall if (res && cmdErrHandling != RunCmdFailureOk) 279fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 280fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 2810031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return 0; 282fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 283fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 2840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallstd::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) { 285fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string res; 2868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *buff; 2878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 288fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 289fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall switch (op) { 2908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 2918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 2928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 2938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 2948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 2958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 2968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 2978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 2988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 2998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 300fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 3018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&buff, "%s penalty_box -m owner --uid-owner %d", opFlag, uid); 3028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = buff; 3038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(buff); 304fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 305fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 306fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 307fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::addNaughtyApps(int numUids, char *appUids[]) { 30826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd); 309fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 310fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 311fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) { 31226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove); 313fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 314fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 31526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) { 316fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall char cmd[MAX_CMD_LEN]; 317fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int uidNum; 31826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *failLogTemplate; 31926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall IptOp op; 320fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int appUids[numUids]; 32126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string naughtyCmd; 322b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall std::list<int /*uid*/>::iterator it; 3238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 32426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (appOp) { 32526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case NaughtyAppOpAdd: 3268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall op = IptOpInsert; 3278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall failLogTemplate = "Failed to add app uid %d to penalty box."; 3288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 32926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case NaughtyAppOpRemove: 3308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall op = IptOpDelete; 3318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall failLogTemplate = "Failed to delete app uid %d from penalty box."; 3328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3330031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall default: 3340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall ALOGE("Unexpected app Op %d", appOp); 3350031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return -1; 33626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 33726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 338fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (uidNum = 0; uidNum < numUids; uidNum++) { 339fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall appUids[uidNum] = atol(appStrUids[uidNum]); 340fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (appUids[uidNum] == 0) { 3415ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE(failLogTemplate, appUids[uidNum]); 342fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail_parse; 343fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 344fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 345fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 346fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (uidNum = 0; uidNum < numUids; uidNum++) { 347b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall int uid = appUids[uidNum]; 348b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall for (it = naughtyAppUids.begin(); it != naughtyAppUids.end(); it++) { 349b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall if (*it == uid) 350b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall break; 351b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall } 352b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall bool found = (it != naughtyAppUids.end()); 353b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall 354b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall if (appOp == NaughtyAppOpRemove) { 355b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall if (!found) { 356b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall ALOGE("No such appUid %d to remove", uid); 357b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall return -1; 358b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall } 359b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall naughtyAppUids.erase(it); 360b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall } else { 361b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall if (found) { 362b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall ALOGE("appUid %d exists already", uid); 363b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall return -1; 364b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall } 365b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall naughtyAppUids.push_front(uid); 366b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall } 367b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall 368b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall naughtyCmd = makeIptablesNaughtyCmd(op, uid); 36926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) { 370b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall ALOGE(failLogTemplate, uid); 371fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail_with_uidNum; 372fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 373fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 374fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return 0; 375fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 37626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum: 377fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall /* Try to remove the uid that failed in any case*/ 37826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]); 37926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd); 38026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse: 38126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 3824a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 3834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 38426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) { 385fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string res; 3868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *buff; 3878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 3880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 3893fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota); 3900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 391fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall switch (op) { 3928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 3938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 3948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 3968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 3978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 3998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 4008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 4018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 402fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 4038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 404bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall // The requried IP version specific --jump REJECT ... will be added later. 4058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota, 4068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall costName); 4078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = buff; 4088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(buff); 4090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 4100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 4110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 41226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) { 4130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char cmd[MAX_CMD_LEN]; 4140031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall int res = 0, res1, res2; 4158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int ruleInsertPos = 1; 4160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string costString; 4170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall const char *costCString; 4180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 4190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* The "-N costly" is created upfront, no need to handle it here. */ 42026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (quotaType) { 42126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaUnique: 422bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costString = "costly_"; 4230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costString += ifn; 4240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costCString = costString.c_str(); 4250031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall /* 4260031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Flush the costly_<iface> is allowed to fail in case it didn't exist. 4270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Creating a new one is allowed to fail in case it existed. 4280031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * This helps with netd restarts. 4290031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall */ 4300031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-F %s", costCString); 431ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res1 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-N %s", costCString); 433ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res2 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res = (res1 && res2) || (!res1 && !res2); 4350031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString); 43726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 43826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 43926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaShared: 440bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costCString = "costly_shared"; 44126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 4420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall default: 4430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall ALOGE("Unexpected quotatype %d", quotaType); 4440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return -1; 4450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 4460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 4478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (globalAlertBytes) { 4488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* The alert rule comes 1st */ 4498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ruleInsertPos = 2; 4508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 4510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4520031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); 453ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4540031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4550031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString); 45626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4570031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4580031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); 459ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4600031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4610031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString); 46226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 4640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 4650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 46626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) { 4670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char cmd[MAX_CMD_LEN]; 4680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 4690dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string costString; 4700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall const char *costCString; 4710dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 47226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (quotaType) { 47326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaUnique: 474bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costString = "costly_"; 4750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costString += ifn; 4760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costCString = costString.c_str(); 47726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 47826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaShared: 479bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costCString = "costly_shared"; 48026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 4810031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall default: 4820031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall ALOGE("Unexpected quotatype %d", quotaType); 4830031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return -1; 4840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 4850dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 4860031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); 48726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4880031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); 48926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 491bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall /* The "-N costly_shared" is created upfront, no need to handle it here. */ 49226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (quotaType == QuotaUnique) { 4930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-F %s", costCString); 49426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 495a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall snprintf(cmd, sizeof(cmd), "-X %s", costCString); 496a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 497fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 498fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 499fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 5004a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) { 5024a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char cmd[MAX_CMD_LEN]; 5034a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char ifn[MAX_IFACENAME_LEN]; 504fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 50526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string quotaCmd; 5068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::string ifaceName; 5078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ; 508bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall const char *costName = "shared"; 50926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<std::string>::iterator it; 5104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!maxBytes) { 5128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* Don't talk about -1, deprecate it. */ 5135ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 5148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 5158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 51626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 5175ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 51826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 51926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 52026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 5214a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5224a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall if (maxBytes == -1) { 523fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return removeInterfaceSharedQuota(ifn); 5244a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 5254a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5264a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall /* Insert ingress quota. */ 5270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { 5280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (*it == ifaceName) 529fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall break; 5304a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 531fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 5320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == sharedQuotaIfaces.end()) { 53326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= prepCostlyIface(ifn, QuotaShared); 5340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (sharedQuotaIfaces.empty()) { 5350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); 53626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 5374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall if (res) { 5385ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed set quota rule"); 539fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail; 5404a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 541fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall sharedQuotaBytes = maxBytes; 542fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 5430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall sharedQuotaIfaces.push_front(ifaceName); 544fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 545fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 546fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 547fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (maxBytes != sharedQuotaBytes) { 5488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= updateQuota(costName, maxBytes); 549fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (res) { 5505ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed update quota for %s", costName); 551fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail; 552fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 553fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall sharedQuotaBytes = maxBytes; 5544a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 5554a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return 0; 556fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 557fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall fail: 5584a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall /* 5594a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse 5604a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * rules in the kernel to see which ones need cleaning up. 561fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall * For now callers needs to choose if they want to "ndc bandwidth enable" 562fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall * which resets everything. 5634a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */ 564fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall removeInterfaceSharedQuota(ifn); 5654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return -1; 5664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 5674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */ 569fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) { 5704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char ifn[MAX_IFACENAME_LEN]; 571fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 57226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 5730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::list<std::string>::iterator it; 574bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall const char *costName = "shared"; 5754a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 5775ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 57826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 57926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 5808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ifaceName = ifn; 58126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 5820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { 5830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (*it == ifaceName) 584fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall break; 5854a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 5860dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == sharedQuotaIfaces.end()) { 5875ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No such iface %s to delete", ifn); 588fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return -1; 5894a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 590fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 59126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= cleanupCostlyIface(ifn, QuotaShared); 5920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall sharedQuotaIfaces.erase(it); 593fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 5940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (sharedQuotaIfaces.empty()) { 595fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string quotaCmd; 596bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes); 59726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 5988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedQuotaBytes = 0; 5998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (sharedAlertBytes) { 6008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall removeSharedAlert(); 6018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedAlertBytes = 0; 6028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 603fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 6044a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return res; 6054a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 6060dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) { 6080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char ifn[MAX_IFACENAME_LEN]; 6090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 61026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 61126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *costName; 61226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<QuotaInfo>::iterator it; 61326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string quotaCmd; 6140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!maxBytes) { 6168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* Don't talk about -1, deprecate it. */ 6175ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 6188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 6198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 6200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (maxBytes == -1) { 62126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return removeInterfaceQuota(iface); 6220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 6255ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 62626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 62726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 62826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 62926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall costName = iface; 63026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 6310dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* Insert ingress quota. */ 6320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 6338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == ifaceName) 6340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall break; 6350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == quotaIfaces.end()) { 63826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= prepCostlyIface(ifn, QuotaUnique); 6390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); 64026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 6410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (res) { 6425ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed set quota rule"); 6430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall goto fail; 6440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0)); 6470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } else { 6498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= updateQuota(costName, maxBytes); 6500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (res) { 6515ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed update quota for %s", iface); 6520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall goto fail; 6530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall it->quota = maxBytes; 6550dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return 0; 6570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall fail: 6590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* 6600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse 6610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * rules in the kernel to see which ones need cleaning up. 6620dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * For now callers needs to choose if they want to "ndc bandwidth enable" 6630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * which resets everything. 6640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall */ 6650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall removeInterfaceSharedQuota(ifn); 6660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return -1; 6670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 6680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) { 6708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return getInterfaceQuota("shared", bytes); 6718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 6728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) { 6748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall FILE *fp; 6758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *fname; 6768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int scanRes; 6778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&fname, "/proc/net/xt_quota/%s", costName); 6798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fp = fopen(fname, "r"); 6808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(fname); 6818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!fp) { 6825ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Reading quota %s failed (%s)", costName, strerror(errno)); 6838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 6848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 6858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall scanRes = fscanf(fp, "%lld", bytes); 6863fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes); 6878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fclose(fp); 6888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return scanRes == 1 ? 0 : -1; 6898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 6908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6910dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) { 6920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char ifn[MAX_IFACENAME_LEN]; 6940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 69526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 69626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *costName; 69726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<QuotaInfo>::iterator it; 6980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 7005ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 70126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 70226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 70326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 70426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall costName = iface; 7050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7060dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 7078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == ifaceName) 7080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall break; 7090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 7100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == quotaIfaces.end()) { 7125ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No such iface %s to delete", ifn); 7130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return -1; 7140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 7150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* This also removes the quota command of CostlyIface chain. */ 71726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= cleanupCostlyIface(ifn, QuotaUnique); 7180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaIfaces.erase(it); 7200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 7220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 7238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) { 7258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall FILE *fp; 7268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *fname; 7278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&fname, "/proc/net/xt_quota/%s", quotaName); 7298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fp = fopen(fname, "w"); 7308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(fname); 7318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!fp) { 7325ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno)); 7338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 7348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 7358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fprintf(fp, "%lld\n", bytes); 7368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fclose(fp); 7378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return 0; 7388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 7398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) { 7418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 7428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 743876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall const char *ifaceLimiting; 7448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 7458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall switch (op) { 7478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 7488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 7498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 7508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 7518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 7528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 7538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 7548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 7558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 7568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 7578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 7588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 759876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall ifaceLimiting = "! -i lo+"; 7600031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_INPUT", 761c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich bytes, alertName); 7628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 7638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 764876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall ifaceLimiting = "! -o lo+"; 7650031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_OUTPUT", 766c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich bytes, alertName); 7678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 7688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 7698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 7708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 7718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 772c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) { 773c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 774c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *opFlag; 775c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *ifaceLimiting; 7768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 777c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 778c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall switch (op) { 779c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpInsert: 780c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-I"; 781c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 782c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpReplace: 783c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-R"; 784c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 785c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall default: 786c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpDelete: 787c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-D"; 788c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 789c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 790c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 791c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall ifaceLimiting = "! -i lo+"; 7920031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_FORWARD", 793c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich bytes, alertName); 794c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 795c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall free(alertQuotaCmd); 796c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 797c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 798c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 799c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) { 800c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 8018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 8028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 8045ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 8058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (globalAlertBytes) { 8088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = updateQuota(alertName, bytes); 8098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } else { 8108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = runIptablesAlertCmd(IptOpInsert, alertName, bytes); 811c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (globalAlertTetherCount) { 8123fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount); 813c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes); 814c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 8158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall globalAlertBytes = bytes; 8178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 8188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 820c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlertInForwardChain(void) { 821c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 822c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 823c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 824c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount++; 8253fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount); 826c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 827c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* 828c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is no globalAlert active we are done. 829c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is an active globalAlert but this is not the 1st 830c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * tether, we are also done. 831c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall */ 832c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertBytes || globalAlertTetherCount != 1) { 833c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return 0; 834c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 835c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 836c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* We only add the rule if this was the 1st tether added. */ 837c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes); 838c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 839c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 840c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 8418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) { 8428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 843c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 8448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 8458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!globalAlertBytes) { 8475ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set"); 8488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes); 851c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (globalAlertTetherCount) { 852c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); 853c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 8548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall globalAlertBytes = 0; 8558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 8568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 858c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::removeGlobalAlertInForwardChain(void) { 859c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 860c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 861c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 862c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertTetherCount) { 8635ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set"); 864c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return -1; 865c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 866c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 867c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount--; 868c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* 869c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is no globalAlert active we are done. 870c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is an active globalAlert but there are more 871c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * tethers, we are also done. 872c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall */ 873c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertBytes || globalAlertTetherCount >= 1) { 874c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return 0; 875c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 876c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 877c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* We only detete the rule if this was the last tether removed. */ 878c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); 879c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 880c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 881c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 8828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) { 8838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!sharedQuotaBytes) { 8845ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Need to have a prior shared quota set to set an alert"); 8858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 8885ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 8898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return setCostlyAlert("shared", bytes, &sharedAlertBytes); 8928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) { 8958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return removeCostlyAlert("shared", &sharedAlertBytes); 8968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) { 8998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::list<QuotaInfo>::iterator it; 9008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 9025ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 9038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 9068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == iface) 9078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 9088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it == quotaIfaces.end()) { 9115ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Need to have a prior interface quota set to set an alert"); 9128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return setCostlyAlert(iface, bytes, &it->alert); 9168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) { 9198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::list<QuotaInfo>::iterator it; 9208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 9228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == iface) 9238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 9248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it == quotaIfaces.end()) { 9275ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set for interface %s", iface); 9288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return removeCostlyAlert(iface, &it->alert); 9328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) { 9358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 9368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *chainNameAndPos; 9378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 9388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertName; 9398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 9415ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 9428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&alertName, "%sAlert", costName); 9458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (*alertBytes) { 9468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = updateQuota(alertName, *alertBytes); 9478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } else { 9488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN); 949c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-I", chainNameAndPos, bytes, alertName); 9508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 9518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 9528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(chainNameAndPos); 9538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *alertBytes = bytes; 9558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertName); 9568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 9578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) { 9608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 9618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *chainName; 9628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertName; 9638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 9648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&alertName, "%sAlert", costName); 9668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!*alertBytes) { 9675ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set for %s alert", costName); 9688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&chainName, "costly_%s", costName); 972c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-D", chainName, *alertBytes, alertName); 9738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 9748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 9758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(chainName); 9768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *alertBytes = 0; 9788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertName); 9798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 9808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 981db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 982db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* 983db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of: 9840031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Chain FORWARD (policy RETURN 0 packets, 0 bytes) 985db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * pkts bytes target prot opt in out source destination 9860031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * 0 0 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 987db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * 0 0 DROP all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 state INVALID 9880031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * 0 0 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 989db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * 990db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 991a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp, 992a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall std::string &extraProcessingInfo) { 993db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int res; 994db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN]; 995db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char iface0[MAX_IPT_OUTPUT_LINE_LEN]; 996db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char iface1[MAX_IPT_OUTPUT_LINE_LEN]; 997db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char rest[MAX_IPT_OUTPUT_LINE_LEN]; 998db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 999db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char *buffPtr; 1000db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t packets, bytes; 1001db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1002db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) { 1003db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Clean up, so a failed parse can still print info */ 1004db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iface0[0] = iface1[0] = rest[0] = packets = bytes = 0; 10050031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s", 1006db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall &packets, &bytes, iface0, iface1, rest); 10073fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res, 1008db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iface0, iface1, packets, bytes, rest, buffPtr); 1009a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall extraProcessingInfo += buffPtr; 1010a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall 1011db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (res != 5) { 1012db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall continue; 1013db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1014db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) { 10153fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); 1016db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.rxPackets = packets; 1017db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.rxBytes = bytes; 1018db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) { 10193fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets); 1020db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.txPackets = packets; 1021db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.txBytes = bytes; 1022db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1023db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1024db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Failure if rx or tx was not found */ 1025db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0; 1026db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 1027db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1028db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1029db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) { 1030db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char *msg; 1031db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(), 1032db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall rxBytes, rxPackets, txBytes, txPackets); 1033db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return msg; 1034db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 1035db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1036a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::getTetherStats(TetherStats &stats, std::string &extraProcessingInfo) { 1037db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int res; 1038db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall std::string fullCmd; 1039db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall FILE *iptOutput; 1040db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall const char *cmd; 1041db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1042db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (stats.rxBytes != -1 || stats.txBytes != -1) { 10435ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Unexpected input stats. Byte counts should be -1."); 1044db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return -1; 1045db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1046db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1047db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* 1048db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Why not use some kind of lib to talk to iptables? 1049db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Because the only libs are libiptc and libip6tc in iptables, and they are 1050db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * not easy to use. They require the known iptables match modules to be 1051db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * preloaded/linked, and require apparently a lot of wrapper code to get 1052db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * the wanted info. 1053db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 1054db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall fullCmd = IPTABLES_PATH; 10550031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall fullCmd += " -nvx -L natctrl_FORWARD"; 1056db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iptOutput = popen(fullCmd.c_str(), "r"); 1057db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (!iptOutput) { 10585ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno)); 1059a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall extraProcessingInfo += "Failed to run iptables."; 1060db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return -1; 1061db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1062a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall res = parseForwardChainStats(stats, iptOutput, extraProcessingInfo); 1063db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall pclose(iptOutput); 1064db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1065db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Currently NatController doesn't do ipv6 tethering, so we are done. */ 1066db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return res; 1067db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 1068