BandwidthController.cpp revision b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6c
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;
349b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall    std::list<int /*uid*/>::iterator it;
3508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
35126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (appOp) {
35226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpAdd:
3538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpInsert;
3548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to add app uid %d to penalty box.";
3558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
35626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpRemove:
3578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpDelete;
3588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to delete app uid %d from penalty box.";
3598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3600031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
3610031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected app Op %d", appOp);
3620031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
36326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
36426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
365fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
366fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        appUids[uidNum] = atol(appStrUids[uidNum]);
367fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (appUids[uidNum] == 0) {
3685ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE(failLogTemplate, appUids[uidNum]);
369fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_parse;
370fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
371fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
372fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
373fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
374b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        int uid = appUids[uidNum];
375b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        for (it = naughtyAppUids.begin(); it != naughtyAppUids.end(); it++) {
376b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (*it == uid)
377b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                break;
378b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        }
379b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        bool found = (it != naughtyAppUids.end());
380b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall
381b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        if (appOp == NaughtyAppOpRemove) {
382b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (!found) {
383b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                ALOGE("No such appUid %d to remove", uid);
384b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                return -1;
385b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            }
386b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            naughtyAppUids.erase(it);
387b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        } else {
388b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (found) {
389b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                ALOGE("appUid %d exists already", uid);
390b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                return -1;
391b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            }
392b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            naughtyAppUids.push_front(uid);
393b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        }
394b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall
395b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        naughtyCmd = makeIptablesNaughtyCmd(op, uid);
39626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) {
397b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            ALOGE(failLogTemplate, uid);
398fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_with_uidNum;
399fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
400fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
401fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
402fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
40326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum:
404fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    /* Try to remove the uid that failed in any case*/
40526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]);
40626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd);
40726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse:
40826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return -1;
4094a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
4104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
41126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
412fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
4138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
4148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
4150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4163fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota);
4170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
418fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
4198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
4208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
4218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
4228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
4238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
4248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
4258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
4268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
4278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
4288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
429fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
4308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
431bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    // The requried IP version specific --jump REJECT ... will be added later.
4328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota,
4338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall             costName);
4348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
4358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
4360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
4370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
4380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
43926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
4400dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
4410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    int res = 0, res1, res2;
4428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int ruleInsertPos = 1;
4430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
4440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
4450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* The "-N costly" is created upfront, no need to handle it here. */
44726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
44826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
449bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
4500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
4510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
4520031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        /*
4530031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * Flush the costly_<iface> is allowed to fail in case it didn't exist.
4540031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * Creating a new one is allowed to fail in case it existed.
4550031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * This helps with netd restarts.
4560031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         */
4570031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
458ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res1 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-N %s", costCString);
460ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res2 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4610031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        res = (res1 && res2) || (!res1 && !res2);
4620031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString);
46426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s -m owner --socket-exists", costCString);
46626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
46726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
46826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
469bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
47026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
4710031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
4720031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected quotatype %d", quotaType);
4730031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
4740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
4750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
4778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* The alert rule comes 1st */
4788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        ruleInsertPos = 2;
4798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
4800031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4810031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
482ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4830031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4840031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString);
48526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4860031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4870031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
488ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4890031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4900031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString);
49126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
4930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
4940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
49526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
4960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
4970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
4980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
4990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
5000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
50126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
50226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
503bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
5040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
5050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
50626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
50726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
508bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
50926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
5100031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
5110031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected quotatype %d", quotaType);
5120031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
5130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
5140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
51626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
5170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
51826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
5190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
520bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    /* The "-N costly_shared" is created upfront, no need to handle it here. */
52126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (quotaType == QuotaUnique) {
5220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
52326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
524a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        snprintf(cmd, sizeof(cmd), "-X %s", costCString);
525a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
526fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
527fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
528fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
5294a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5300dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
5314a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char cmd[MAX_CMD_LEN];
5324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
533fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
53426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
5358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::string ifaceName;
5368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ;
537bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
53826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<std::string>::iterator it;
5394a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
5418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
5425ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
5438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
5448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
54526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
5465ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
54726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
54826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
54926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
5504a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5514a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    if (maxBytes == -1) {
552fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return removeInterfaceSharedQuota(ifn);
5534a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5544a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5554a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /* Insert ingress quota. */
5560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
5570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
558fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
5594a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
560fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
5610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
56226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaShared);
5630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (sharedQuotaIfaces.empty()) {
5640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
56526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall            res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
5664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            if (res) {
5675ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block                ALOGE("Failed set quota rule");
568fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall                goto fail;
5694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            }
570fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            sharedQuotaBytes = maxBytes;
571fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
5720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        sharedQuotaIfaces.push_front(ifaceName);
573fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
574fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
575fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
576fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    if (maxBytes != sharedQuotaBytes) {
5778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
578fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (res) {
5795ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", costName);
580fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail;
581fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
582fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        sharedQuotaBytes = maxBytes;
5834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return 0;
585fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
586fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    fail:
5874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /*
5884a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
5894a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * rules in the kernel to see which ones need cleaning up.
590fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
591fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * which resets everything.
5924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     */
593fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    removeInterfaceSharedQuota(ifn);
5944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return -1;
5954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
5964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */
598fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) {
5994a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
600fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
60126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
6020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::list<std::string>::iterator it;
603bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
6044a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
6058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
6065ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
60726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
60826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
6098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ifaceName = ifn;
61026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
6110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
6120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
613fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
6144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
6150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
6165ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
617fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return -1;
6184a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
619fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
62026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaShared);
6210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    sharedQuotaIfaces.erase(it);
622fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
6230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (sharedQuotaIfaces.empty()) {
624fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        std::string quotaCmd;
625bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
62626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
6278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        sharedQuotaBytes = 0;
6288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (sharedAlertBytes) {
6298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            removeSharedAlert();
6308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            sharedAlertBytes = 0;
6318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        }
632fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
6334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return res;
6344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
6350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
6370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
6380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
63926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
64026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
64126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
64226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
6430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
6458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
6465ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
6478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (maxBytes == -1) {
65026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return removeInterfaceQuota(iface);
6510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
6545ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
65526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
65626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
65726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
65826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
65926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
6600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Insert ingress quota. */
6610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
6628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
6630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
6640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
66726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaUnique);
6680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
66926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
6700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
6715ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed set quota rule");
6720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
6730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
6740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
6760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6770dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    } else {
6788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
6790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
6805ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", iface);
6810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
6820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
6838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        it->quota = maxBytes;
6840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6850dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return 0;
6860dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6870dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    fail:
6880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /*
6890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
6900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * rules in the kernel to see which ones need cleaning up.
6910dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
6920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * which resets everything.
6930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     */
6940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    removeInterfaceSharedQuota(ifn);
6950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return -1;
6960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
6970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
6998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return getInterfaceQuota("shared", bytes);
7008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
7038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
7048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
7058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int scanRes;
7068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", costName);
7088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "r");
7098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
7108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
7115ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
7128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    scanRes = fscanf(fp, "%lld", bytes);
7153fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes);
7168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
7178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return scanRes == 1 ? 0 : -1;
7188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) {
7210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
7230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
72426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
72526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
72626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
7270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
7295ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
73026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
73126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
73226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
73326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
7340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
7368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
7370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
7380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7400dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
7415ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
7420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        return -1;
7430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* This also removes the quota command of CostlyIface chain. */
74626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaUnique);
7470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    quotaIfaces.erase(it);
7490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
7510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
7528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
7548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
7558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
7568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
7588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "w");
7598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
7608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
7615ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
7628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fprintf(fp, "%lld\n", bytes);
7658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
7668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return 0;
7678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
7708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
772876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    const char *ifaceLimiting;
7738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
7748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    switch (op) {
7768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
7778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
7788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
7808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
7818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
7838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
7848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
7858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
788876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    ifaceLimiting = "! -i lo+";
7890031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_INPUT",
790c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
7918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
7928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
793876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    ifaceLimiting = "! -o lo+";
7940031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_OUTPUT",
795c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
7968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
7978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
7988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
7998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
801c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
802c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
803c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *opFlag;
804c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *ifaceLimiting;
8058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
806c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
807c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    switch (op) {
808c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpInsert:
809c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-I";
810c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
811c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpReplace:
812c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-R";
813c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
814c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    default:
815c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpDelete:
816c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-D";
817c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
818c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
819c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
820c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    ifaceLimiting = "! -i lo+";
8210031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_FORWARD",
822c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
823c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
824c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    free(alertQuotaCmd);
825c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
826c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
827c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
828c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) {
829c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
8308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
8318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
8335ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
8348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
8378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, bytes);
8388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
8398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
840c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        if (globalAlertTetherCount) {
8413fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
842c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall            res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
843c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        }
8448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = bytes;
8468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
849c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlertInForwardChain(void) {
850c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
851c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
852c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
853c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount++;
8543fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
855c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
856c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
857c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
858c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but this is not the 1st
859c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tether, we are also done.
860c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
861c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount != 1) {
862c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
863c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
864c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
865c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only add the rule if this was the 1st tether added. */
866c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
867c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
868c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
869c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
8708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) {
8718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
872c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
8738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
8748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!globalAlertBytes) {
8765ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
8778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
880c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (globalAlertTetherCount) {
881c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
882c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
8838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = 0;
8848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
887c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::removeGlobalAlertInForwardChain(void) {
888c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
889c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
890c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
891c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertTetherCount) {
8925ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
893c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return -1;
894c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
895c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
896c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount--;
897c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
898c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
899c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but there are more
900c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tethers, we are also done.
901c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
902c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount >= 1) {
903c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
904c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
905c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
906c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only detete the rule if this was the last tether removed. */
907c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
908c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
909c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
910c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
9118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) {
9128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!sharedQuotaBytes) {
9135ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior shared quota set to set an alert");
9148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
9175ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
9188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert("shared", bytes, &sharedAlertBytes);
9218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) {
9248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert("shared", &sharedAlertBytes);
9258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
9288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
9298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
9315ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
9328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
9358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
9368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
9378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
9405ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior interface quota set to set an alert");
9418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert(iface, bytes, &it->alert);
9458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) {
9488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
9498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
9518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
9528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
9538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
9565ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for interface %s", iface);
9578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert(iface, &it->alert);
9618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
9648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
9658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainNameAndPos;
9668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
9678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
9688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
9705ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
9718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
9748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (*alertBytes) {
9758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, *alertBytes);
9768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
9778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN);
978c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-I", chainNameAndPos, bytes, alertName);
9798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
9808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(alertQuotaCmd);
9818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(chainNameAndPos);
9828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = bytes;
9848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
9858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
9868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
9898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
9908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainName;
9918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
9928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
9938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
9958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!*alertBytes) {
9965ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for %s alert", costName);
9978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&chainName, "costly_%s", costName);
1001c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-D", chainName, *alertBytes, alertName);
10028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
10038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
10048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(chainName);
10058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = 0;
10078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
10088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
10098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
1010db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1011db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/*
1012db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of:
10130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Chain FORWARD (policy RETURN 0 packets, 0 bytes)
1014db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *     pkts      bytes target     prot opt in     out     source               destination
10150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall *        0        0 RETURN     all  --  rmnet0 wlan0   0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
1016db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *        0        0 DROP       all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0            state INVALID
10170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall *        0        0 RETURN     all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0
1018db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *
1019db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */
1020a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp,
1021a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall                                                std::string &extraProcessingInfo) {
1022db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
1023db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1024db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface0[MAX_IPT_OUTPUT_LINE_LEN];
1025db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface1[MAX_IPT_OUTPUT_LINE_LEN];
1026db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char rest[MAX_IPT_OUTPUT_LINE_LEN];
1027db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1028db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *buffPtr;
1029db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int64_t packets, bytes;
1030db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1031db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1032db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        /* Clean up, so a failed parse can still print info */
1033db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
10340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s",
1035db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall                &packets, &bytes, iface0, iface1, rest);
10363fb42e026ffebab2c8f282e42501040121e32d83Steve Block        ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res,
1037db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall             iface0, iface1, packets, bytes, rest, buffPtr);
1038a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall        extraProcessingInfo += buffPtr;
1039a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall
1040db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if (res != 5) {
1041db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            continue;
1042db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
1043db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) {
10443fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1045db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxPackets = packets;
1046db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxBytes = bytes;
1047db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) {
10483fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets);
1049db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txPackets = packets;
1050db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txBytes = bytes;
1051db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
1052db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1053db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Failure if rx or tx was not found */
1054db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0;
1055db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1056db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1057db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1058db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) {
1059db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *msg;
1060db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(),
1061db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            rxBytes, rxPackets, txBytes, txPackets);
1062db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return msg;
1063db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1064db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1065a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::getTetherStats(TetherStats &stats, std::string &extraProcessingInfo) {
1066db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
1067db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    std::string fullCmd;
1068db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    FILE *iptOutput;
1069db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    const char *cmd;
1070db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1071db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (stats.rxBytes != -1 || stats.txBytes != -1) {
10725ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Unexpected input stats. Byte counts should be -1.");
1073db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
1074db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1075db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1076db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /*
1077db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Why not use some kind of lib to talk to iptables?
1078db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Because the only libs are libiptc and libip6tc in iptables, and they are
1079db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * not easy to use. They require the known iptables match modules to be
1080db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * preloaded/linked, and require apparently a lot of wrapper code to get
1081db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * the wanted info.
1082db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     */
1083db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    fullCmd = IPTABLES_PATH;
10840031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    fullCmd += " -nvx -L natctrl_FORWARD";
1085db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    iptOutput = popen(fullCmd.c_str(), "r");
1086db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (!iptOutput) {
10875ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
1088a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall            extraProcessingInfo += "Failed to run iptables.";
1089db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
1090db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1091a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall    res = parseForwardChainStats(stats, iptOutput, extraProcessingInfo);
1092db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    pclose(iptOutput);
1093db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1094db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Currently NatController doesn't do ipv6 tethering, so we are done. */
1095db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return res;
1096db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1097