BandwidthController.cpp revision f66d6e9db6be3e94b80c59fab99e237d2e2968c5
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", 110f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall 111f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t raw -F bw_raw_PREROUTING", 112f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t mangle -F bw_mangle_POSTROUTING", 1130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall}; 1140031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 1150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall/* The cleanup commands assume flushing has been done. */ 1160031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_CLEANUP_COMMANDS[] = { 1170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall /* Delete hooks to custom chains. */ 1180031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-D INPUT -j bw_INPUT", 1190031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-D OUTPUT -j bw_OUTPUT", 1200031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-D FORWARD -j bw_FORWARD", 121f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall 122f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t raw -D bw_raw_PREROUTING", 123f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t mangle -D bw_mangle_POSTROUTING", 124f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall 1250031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X bw_INPUT", 1260031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X bw_OUTPUT", 1270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X bw_FORWARD", 1280031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X penalty_box", 1290031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-X costly_shared", 130f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall 131f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t raw -X bw_raw_PREROUTING", 132f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t mangle -X bw_mangle_POSTROUTING", 1330dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 135db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_SETUP_COMMANDS[] = { 1360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* Created needed chains. */ 1370031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-N bw_INPUT", 1380031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A INPUT -j bw_INPUT", 1390031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 1400031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-N bw_OUTPUT", 1410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A OUTPUT -j bw_OUTPUT", 1420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 1430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-N bw_FORWARD", 1440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-I FORWARD -j bw_FORWARD", 1450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 146bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-N costly_shared", 1470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall "-N penalty_box", 148f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall 149f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t raw -N bw_raw_PREROUTING", 150f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t raw -A PREROUTING -j bw_raw_PREROUTING", 151f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t mangle -N bw_mangle_POSTROUTING", 152f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t mangle -A POSTROUTING -j bw_mangle_POSTROUTING", 1530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 155db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = { 1560031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_INPUT -i lo --jump RETURN", 1570031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */ 1580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 1590031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_OUTPUT -o lo --jump RETURN", 1600031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */ 1610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 162bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-A costly_shared --jump penalty_box", 163bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall "-A costly_shared -m owner --socket-exists", /* This is a tracking rule. */ 164f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall 165f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t raw -A bw_raw_PREROUTING ! -i lo+ -m owner --socket-exists", /* This is a tracking rule. */ 166f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall "-t mangle -A bw_mangle_POSTROUTING ! -o lo+ -m owner --socket-exists", /* This is a tracking rule. */ 1670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}; 1684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 1694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP AbgrallBandwidthController::BandwidthController(void) { 1704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char value[PROPERTY_VALUE_MAX]; 1714a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 17211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall property_get("persist.bandwidth.uselogwrap", value, "0"); 17311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall useLogwrapCall = !strcmp(value, "1"); 1744a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 1754a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 176ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrallint BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling, 177ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall IptFailureLog failureHandling) { 1780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 1798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 1803fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("runIpxtablesCmd(cmd=%s)", cmd); 181ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res |= runIptablesCmd(cmd, rejectHandling, IptIpV4, failureHandling); 182ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res |= runIptablesCmd(cmd, rejectHandling, IptIpV6, failureHandling); 1830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 1840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 1850dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 18626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) { 18726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 18826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall memset(buffer, '\0', buffSize); // strncpy() is not filling leftover with '\0' 18926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall strncpy(buffer, src, buffSize); 19026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return buffer[buffSize - 1]; 19126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall} 19226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 1938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, 194ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall IptIpVer iptVer, IptFailureLog failureHandling) { 19526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall char buffer[MAX_CMD_LEN]; 1964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall const char *argv[MAX_CMD_ARGS]; 19726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall int argc = 0; 1984a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char *next = buffer; 1994a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char *tmp; 20011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall int res; 2014a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string fullCmd = cmd; 20326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 20426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (rejectHandling == IptRejectAdd) { 2050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall fullCmd += " --jump REJECT --reject-with"; 20626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (iptVer) { 20726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case IptIpV4: 2088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fullCmd += " icmp-net-prohibited"; 2098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 21026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case IptIpV6: 2118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fullCmd += " icmp6-adm-prohibited"; 2128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 2130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 2140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 2150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 21611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall fullCmd.insert(0, " "); 21711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH); 2184a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 21911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (!useLogwrapCall) { 2209e5e0ce62e88ddf9a09798eda51b0c270d354c8eJP Abgrall res = system_nosh(fullCmd.c_str()); 22111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } else { 22211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) { 2235ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("iptables command too long"); 2244a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return -1; 2254a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 226fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 22711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argc = 0; 22811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall while ((tmp = strsep(&next, " "))) { 22911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argv[argc++] = tmp; 23011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall if (argc >= MAX_CMD_ARGS) { 2315ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("iptables argument overflow"); 23211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall return -1; 23311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 23411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 23511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall 23611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall argv[argc] = NULL; 237c4bbfa247dbe4dda17e28694d49afd3ec0b06badGlenn Kasten res = logwrap(argc, argv); 23811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 239ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall if (res && failureHandling == IptFailShow) { 2405ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("runIptablesCmd(): failed %s res=%d", fullCmd.c_str(), res); 24111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall } 24211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall return res; 2434a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::setupIptablesHooks(void) { 2460031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2470031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall /* Some of the initialCommands are allowed to fail */ 2480031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), 2490031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_FLUSH_COMMANDS, RunCmdFailureOk); 2500031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*), 2520031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_CLEANUP_COMMANDS, RunCmdFailureOk); 2530031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2540031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*), 2550031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_SETUP_COMMANDS, RunCmdFailureBad); 2560031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2570031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return 0; 2580031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2590031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall} 2600031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2610031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::enableBandwidthControl(bool force) { 262fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res; 2630031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall char value[PROPERTY_VALUE_MAX]; 2640031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 2650031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall if (!force) { 2660031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall property_get("persist.bandwidth.enable", value, "1"); 2670031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall if (!strcmp(value, "0")) 2680031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return 0; 2690031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall } 2708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 271db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Let's pretend we started from scratch ... */ 2728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedQuotaIfaces.clear(); 2738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall quotaIfaces.clear(); 2748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall naughtyAppUids.clear(); 275db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall globalAlertBytes = 0; 276c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount = 0; 277db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall sharedQuotaBytes = sharedAlertBytes = 0; 278db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 2790031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), 2800031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_FLUSH_COMMANDS, RunCmdFailureOk); 281db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 2820031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*), 283db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad); 2848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 285fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 2864a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2884a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2894a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::disableBandwidthControl(void) { 2900031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*), 2910031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall IPT_FLUSH_COMMANDS, RunCmdFailureOk); 292fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return 0; 2934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 2944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 2958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runCommands(int numCommands, const char *commands[], 2968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall RunCmdErrHandling cmdErrHandling) { 297fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 298ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall IptFailureLog failureLogging = IptFailShow; 299ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall if (cmdErrHandling == RunCmdFailureOk) { 300ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall failureLogging = IptFailHide; 301ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall } 3023fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("runCommands(): %d commands", numCommands); 303fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) { 304ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd, failureLogging); 3050031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall if (res && cmdErrHandling != RunCmdFailureOk) 306fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 307fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 3080031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return 0; 309fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 310fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 3110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallstd::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) { 312fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string res; 3138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *buff; 3148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 315fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 316fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall switch (op) { 3178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 3188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 3198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 3218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 3228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 3248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 3258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 3268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 327fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 3288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&buff, "%s penalty_box -m owner --uid-owner %d", opFlag, uid); 3298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = buff; 3308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(buff); 331fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 332fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 333fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 334fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::addNaughtyApps(int numUids, char *appUids[]) { 33526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd); 336fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 337fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 338fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) { 33926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove); 340fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 341fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 34226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) { 343fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall char cmd[MAX_CMD_LEN]; 344fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int uidNum; 34526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *failLogTemplate; 34626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall IptOp op; 347fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int appUids[numUids]; 34826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string naughtyCmd; 3498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 35026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (appOp) { 35126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case NaughtyAppOpAdd: 3528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall op = IptOpInsert; 3538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall failLogTemplate = "Failed to add app uid %d to penalty box."; 3548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 35526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case NaughtyAppOpRemove: 3568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall op = IptOpDelete; 3578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall failLogTemplate = "Failed to delete app uid %d from penalty box."; 3588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 3590031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall default: 3600031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall ALOGE("Unexpected app Op %d", appOp); 3610031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return -1; 36226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 36326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 364fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (uidNum = 0; uidNum < numUids; uidNum++) { 365fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall appUids[uidNum] = atol(appStrUids[uidNum]); 366fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (appUids[uidNum] == 0) { 3675ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE(failLogTemplate, appUids[uidNum]); 368fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail_parse; 369fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 370fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 371fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 372fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall for (uidNum = 0; uidNum < numUids; uidNum++) { 37326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall naughtyCmd = makeIptablesNaughtyCmd(op, appUids[uidNum]); 37426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) { 3755ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE(failLogTemplate, appUids[uidNum]); 376fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail_with_uidNum; 377fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 378fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 379fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return 0; 380fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 38126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum: 382fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall /* Try to remove the uid that failed in any case*/ 38326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]); 38426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd); 38526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse: 38626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 3874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 3884a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 38926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) { 390fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string res; 3918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *buff; 3928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 3930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 3943fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota); 3950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 396fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall switch (op) { 3978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 3988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 3998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 4008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 4018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 4028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 4038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 4048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 4058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 4068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 407fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 4088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 409bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall // The requried IP version specific --jump REJECT ... will be added later. 4108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota, 4118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall costName); 4128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = buff; 4138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(buff); 4140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 4150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 4160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 41726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) { 4180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char cmd[MAX_CMD_LEN]; 4190031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall int res = 0, res1, res2; 4208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int ruleInsertPos = 1; 4210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string costString; 4220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall const char *costCString; 4230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 4240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* The "-N costly" is created upfront, no need to handle it here. */ 42526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (quotaType) { 42626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaUnique: 427bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costString = "costly_"; 4280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costString += ifn; 4290dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costCString = costString.c_str(); 4300031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall /* 4310031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Flush the costly_<iface> is allowed to fail in case it didn't exist. 4320031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Creating a new one is allowed to fail in case it existed. 4330031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * This helps with netd restarts. 4340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall */ 4350031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-F %s", costCString); 436ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res1 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-N %s", costCString); 438ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall res2 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4390031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res = (res1 && res2) || (!res1 && !res2); 4400031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString); 44226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-A %s -m owner --socket-exists", costCString); 44426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 44526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 44626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaShared: 447bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costCString = "costly_shared"; 44826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 4490031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall default: 4500031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall ALOGE("Unexpected quotatype %d", quotaType); 4510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return -1; 4520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 4530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 4548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (globalAlertBytes) { 4558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* The alert rule comes 1st */ 4568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ruleInsertPos = 2; 4578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 4580031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4590031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); 460ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4610031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4620031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString); 46326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4640031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4650031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); 466ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide); 4670031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall 4680031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString); 46926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 4710dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 4720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 47326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) { 4740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char cmd[MAX_CMD_LEN]; 4750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 4760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string costString; 4770dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall const char *costCString; 4780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 47926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall switch (quotaType) { 48026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaUnique: 481bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costString = "costly_"; 4820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costString += ifn; 4830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall costCString = costString.c_str(); 48426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 48526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall case QuotaShared: 486bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall costCString = "costly_shared"; 48726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall break; 4880031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall default: 4890031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall ALOGE("Unexpected quotatype %d", quotaType); 4900031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall return -1; 4910dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 4920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 4930031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString); 49426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4950031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString); 49626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 4970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 498bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall /* The "-N costly_shared" is created upfront, no need to handle it here. */ 49926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (quotaType == QuotaUnique) { 5000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall snprintf(cmd, sizeof(cmd), "-F %s", costCString); 50126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 502a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall snprintf(cmd, sizeof(cmd), "-X %s", costCString); 503a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall res |= runIpxtablesCmd(cmd, IptRejectNoAdd); 504fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 505fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return res; 506fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall} 5074a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) { 5094a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char cmd[MAX_CMD_LEN]; 5104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char ifn[MAX_IFACENAME_LEN]; 511fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 51226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string quotaCmd; 5138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::string ifaceName; 5148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ; 515bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall const char *costName = "shared"; 51626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<std::string>::iterator it; 5174a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!maxBytes) { 5198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* Don't talk about -1, deprecate it. */ 5205ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 5218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 5228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 52326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 5245ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 52526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 52626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 52726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 5284a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5294a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall if (maxBytes == -1) { 530fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return removeInterfaceSharedQuota(ifn); 5314a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 5324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall /* Insert ingress quota. */ 5340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { 5350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (*it == ifaceName) 536fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall break; 5374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 538fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 5390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == sharedQuotaIfaces.end()) { 54026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= prepCostlyIface(ifn, QuotaShared); 5410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (sharedQuotaIfaces.empty()) { 5420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); 54326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 5444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall if (res) { 5455ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed set quota rule"); 546fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail; 5474a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 548fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall sharedQuotaBytes = maxBytes; 549fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 5500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall sharedQuotaIfaces.push_front(ifaceName); 551fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 552fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 553fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 554fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (maxBytes != sharedQuotaBytes) { 5558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= updateQuota(costName, maxBytes); 556fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall if (res) { 5575ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed update quota for %s", costName); 558fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall goto fail; 559fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 560fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall sharedQuotaBytes = maxBytes; 5614a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 5624a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return 0; 563fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 564fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall fail: 5654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall /* 5664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse 5674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * rules in the kernel to see which ones need cleaning up. 568fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall * For now callers needs to choose if they want to "ndc bandwidth enable" 569fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall * which resets everything. 5704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */ 571fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall removeInterfaceSharedQuota(ifn); 5724a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return -1; 5734a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 5744a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */ 576fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) { 5774a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall char ifn[MAX_IFACENAME_LEN]; 578fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int res = 0; 57926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 5800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::list<std::string>::iterator it; 581bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall const char *costName = "shared"; 5824a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 5838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 5845ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 58526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 58626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 5878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall ifaceName = ifn; 58826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 5890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) { 5900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (*it == ifaceName) 591fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall break; 5924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 5930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == sharedQuotaIfaces.end()) { 5945ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No such iface %s to delete", ifn); 595fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall return -1; 5964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall } 597fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 59826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= cleanupCostlyIface(ifn, QuotaShared); 5990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall sharedQuotaIfaces.erase(it); 600fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 6010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (sharedQuotaIfaces.empty()) { 602fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall std::string quotaCmd; 603bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes); 60426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 6058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedQuotaBytes = 0; 6068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (sharedAlertBytes) { 6078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall removeSharedAlert(); 6088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall sharedAlertBytes = 0; 6098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 610fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall } 6114a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall return res; 6124a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall} 6130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) { 6150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char ifn[MAX_IFACENAME_LEN]; 6160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 61726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 61826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *costName; 61926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<QuotaInfo>::iterator it; 62026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string quotaCmd; 6210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!maxBytes) { 6238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall /* Don't talk about -1, deprecate it. */ 6245ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 6258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 6268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 6270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (maxBytes == -1) { 62826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return removeInterfaceQuota(iface); 6290dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6300dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 6325ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 63326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 63426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 63526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 63626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall costName = iface; 63726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 6380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* Insert ingress quota. */ 6390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 6408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == ifaceName) 6410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall break; 6420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == quotaIfaces.end()) { 64526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= prepCostlyIface(ifn, QuotaUnique); 6460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes); 64726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd); 6480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (res) { 6495ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed set quota rule"); 6500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall goto fail; 6510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0)); 6540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6550dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } else { 6568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= updateQuota(costName, maxBytes); 6570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (res) { 6585ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed update quota for %s", iface); 6590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall goto fail; 6600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall it->quota = maxBytes; 6620dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 6630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return 0; 6640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall fail: 6660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* 6670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse 6680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * rules in the kernel to see which ones need cleaning up. 6690dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * For now callers needs to choose if they want to "ndc bandwidth enable" 6700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall * which resets everything. 6710dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall */ 6720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall removeInterfaceSharedQuota(ifn); 6730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return -1; 6740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 6750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 6768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) { 6778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return getInterfaceQuota("shared", bytes); 6788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 6798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) { 6818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall FILE *fp; 6828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *fname; 6838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int scanRes; 6848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&fname, "/proc/net/xt_quota/%s", costName); 6868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fp = fopen(fname, "r"); 6878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(fname); 6888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!fp) { 6895ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Reading quota %s failed (%s)", costName, strerror(errno)); 6908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 6918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 6928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall scanRes = fscanf(fp, "%lld", bytes); 6933fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes); 6948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fclose(fp); 6958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return scanRes == 1 ? 0 : -1; 6968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 6978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 6980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) { 6990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall char ifn[MAX_IFACENAME_LEN]; 7010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int res = 0; 70226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string ifaceName; 70326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall const char *costName; 70426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::list<QuotaInfo>::iterator it; 7050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) { 7075ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN); 70826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall return -1; 70926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall } 71026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall ifaceName = ifn; 71126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall costName = iface; 7120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 7148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == ifaceName) 7150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall break; 7160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 7170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall if (it == quotaIfaces.end()) { 7195ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No such iface %s to delete", ifn); 7200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return -1; 7210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall } 7220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* This also removes the quota command of CostlyIface chain. */ 72426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall res |= cleanupCostlyIface(ifn, QuotaUnique); 7250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall quotaIfaces.erase(it); 7270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 7280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall return res; 7290dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall} 7308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) { 7328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall FILE *fp; 7338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *fname; 7348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&fname, "/proc/net/xt_quota/%s", quotaName); 7368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fp = fopen(fname, "w"); 7378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(fname); 7388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!fp) { 7395ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno)); 7408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 7418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 7428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fprintf(fp, "%lld\n", bytes); 7438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall fclose(fp); 7448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return 0; 7458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 7468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) { 7488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 7498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall const char *opFlag; 750876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall const char *ifaceLimiting; 7518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 7528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 7538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall switch (op) { 7548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpInsert: 7558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-I"; 7568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 7578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpReplace: 7588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-R"; 7598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 7608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall default: 7618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall case IptOpDelete: 7628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall opFlag = "-D"; 7638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 7648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 7658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 766876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall ifaceLimiting = "! -i lo+"; 7670031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_INPUT", 768c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich bytes, alertName); 7698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 7708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 771876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall ifaceLimiting = "! -o lo+"; 7720031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_OUTPUT", 773c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich bytes, alertName); 7748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 7758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 7768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 7778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 7788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 779c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) { 780c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 781c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *opFlag; 782c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *ifaceLimiting; 7838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 784c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 785c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall switch (op) { 786c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpInsert: 787c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-I"; 788c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 789c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpReplace: 790c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-R"; 791c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 792c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall default: 793c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall case IptOpDelete: 794c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall opFlag = "-D"; 795c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall break; 796c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 797c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 798c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall ifaceLimiting = "! -i lo+"; 7990031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_FORWARD", 800c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich bytes, alertName); 801c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 802c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall free(alertQuotaCmd); 803c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 804c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 805c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 806c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) { 807c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 8088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 8098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 8115ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 8128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (globalAlertBytes) { 8158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = updateQuota(alertName, bytes); 8168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } else { 8178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = runIptablesAlertCmd(IptOpInsert, alertName, bytes); 818c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (globalAlertTetherCount) { 8193fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount); 820c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes); 821c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 8228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall globalAlertBytes = bytes; 8248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 8258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 827c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlertInForwardChain(void) { 828c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 829c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 830c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 831c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount++; 8323fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount); 833c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 834c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* 835c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is no globalAlert active we are done. 836c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is an active globalAlert but this is not the 1st 837c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * tether, we are also done. 838c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall */ 839c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertBytes || globalAlertTetherCount != 1) { 840c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return 0; 841c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 842c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 843c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* We only add the rule if this was the 1st tether added. */ 844c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes); 845c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 846c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 847c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 8488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) { 8498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 850c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 8518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 8528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 8538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!globalAlertBytes) { 8545ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set"); 8558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes); 858c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (globalAlertTetherCount) { 859c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); 860c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 8618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall globalAlertBytes = 0; 8628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 8638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 8648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 865c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::removeGlobalAlertInForwardChain(void) { 866c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall int res = 0; 867c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall const char *alertName = ALERT_GLOBAL_NAME; 868c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 869c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertTetherCount) { 8705ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set"); 871c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return -1; 872c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 873c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 874c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall globalAlertTetherCount--; 875c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* 876c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is no globalAlert active we are done. 877c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * If there is an active globalAlert but there are more 878c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall * tethers, we are also done. 879c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall */ 880c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall if (!globalAlertBytes || globalAlertTetherCount >= 1) { 881c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return 0; 882c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall } 883c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 884c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall /* We only detete the rule if this was the last tether removed. */ 885c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes); 886c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall return res; 887c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall} 888c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall 8898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) { 8908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!sharedQuotaBytes) { 8915ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Need to have a prior shared quota set to set an alert"); 8928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 8955ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 8968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 8978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 8988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return setCostlyAlert("shared", bytes, &sharedAlertBytes); 8998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) { 9028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return removeCostlyAlert("shared", &sharedAlertBytes); 9038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) { 9068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::list<QuotaInfo>::iterator it; 9078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 9095ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 9108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 9138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == iface) 9148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 9158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it == quotaIfaces.end()) { 9185ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Need to have a prior interface quota set to set an alert"); 9198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return setCostlyAlert(iface, bytes, &it->alert); 9238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) { 9268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::list<QuotaInfo>::iterator it; 9278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) { 9298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it->ifaceName == iface) 9308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall break; 9318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (it == quotaIfaces.end()) { 9345ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set for interface %s", iface); 9358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return removeCostlyAlert(iface, &it->alert); 9398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) { 9428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 9438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *chainNameAndPos; 9448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 9458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertName; 9468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!bytes) { 9485ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Invalid bytes value. 1..max_int64."); 9498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&alertName, "%sAlert", costName); 9528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (*alertBytes) { 9538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res = updateQuota(alertName, *alertBytes); 9548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } else { 9558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN); 956c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-I", chainNameAndPos, bytes, alertName); 9578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 9588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 9598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(chainNameAndPos); 9608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *alertBytes = bytes; 9628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertName); 9638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 9648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 9658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) { 9678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertQuotaCmd; 9688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *chainName; 9698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall char *alertName; 9708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int res = 0; 9718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&alertName, "%sAlert", costName); 9738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall if (!*alertBytes) { 9745ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("No prior alert set for %s alert", costName); 9758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return -1; 9768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall } 9778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall asprintf(&chainName, "costly_%s", costName); 979c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-D", chainName, *alertBytes, alertName); 9808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd); 9818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertQuotaCmd); 9828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(chainName); 9838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 9848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *alertBytes = 0; 9858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall free(alertName); 9868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall return res; 9878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall} 988db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 989db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* 990db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of: 9910031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Chain FORWARD (policy RETURN 0 packets, 0 bytes) 992db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * pkts bytes target prot opt in out source destination 9930031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * 0 0 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 994db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * 0 0 DROP all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 state INVALID 9950031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * 0 0 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 996db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * 997db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 998a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp, 999a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall std::string &extraProcessingInfo) { 1000db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int res; 1001db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN]; 1002db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char iface0[MAX_IPT_OUTPUT_LINE_LEN]; 1003db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char iface1[MAX_IPT_OUTPUT_LINE_LEN]; 1004db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char rest[MAX_IPT_OUTPUT_LINE_LEN]; 1005db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1006db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char *buffPtr; 1007db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t packets, bytes; 1008db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1009db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) { 1010db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Clean up, so a failed parse can still print info */ 1011db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iface0[0] = iface1[0] = rest[0] = packets = bytes = 0; 10120031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s", 1013db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall &packets, &bytes, iface0, iface1, rest); 10143fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res, 1015db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iface0, iface1, packets, bytes, rest, buffPtr); 1016a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall extraProcessingInfo += buffPtr; 1017a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall 1018db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (res != 5) { 1019db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall continue; 1020db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1021db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) { 10223fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets); 1023db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.rxPackets = packets; 1024db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.rxBytes = bytes; 1025db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) { 10263fb42e026ffebab2c8f282e42501040121e32d83Steve Block ALOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets); 1027db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.txPackets = packets; 1028db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall stats.txBytes = bytes; 1029db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1030db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1031db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Failure if rx or tx was not found */ 1032db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0; 1033db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 1034db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1035db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1036db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) { 1037db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char *msg; 1038db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(), 1039db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall rxBytes, rxPackets, txBytes, txPackets); 1040db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return msg; 1041db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 1042db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1043a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::getTetherStats(TetherStats &stats, std::string &extraProcessingInfo) { 1044db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int res; 1045db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall std::string fullCmd; 1046db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall FILE *iptOutput; 1047db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall const char *cmd; 1048db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1049db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (stats.rxBytes != -1 || stats.txBytes != -1) { 10505ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Unexpected input stats. Byte counts should be -1."); 1051db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return -1; 1052db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1053db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1054db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* 1055db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Why not use some kind of lib to talk to iptables? 1056db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Because the only libs are libiptc and libip6tc in iptables, and they are 1057db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * not easy to use. They require the known iptables match modules to be 1058db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * preloaded/linked, and require apparently a lot of wrapper code to get 1059db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * the wanted info. 1060db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 1061db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall fullCmd = IPTABLES_PATH; 10620031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall fullCmd += " -nvx -L natctrl_FORWARD"; 1063db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall iptOutput = popen(fullCmd.c_str(), "r"); 1064db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall if (!iptOutput) { 10655ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno)); 1066a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall extraProcessingInfo += "Failed to run iptables."; 1067db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return -1; 1068db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall } 1069a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall res = parseForwardChainStats(stats, iptOutput, extraProcessingInfo); 1070db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall pclose(iptOutput); 1071db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 1072db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Currently NatController doesn't do ipv6 tethering, so we are done. */ 1073db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall return res; 1074db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall} 1075