BandwidthController.cpp revision 5ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13
14a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall/*
24a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Copyright (C) 2011 The Android Open Source Project
34a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
44a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Licensed under the Apache License, Version 2.0 (the "License");
54a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * you may not use this file except in compliance with the License.
64a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * You may obtain a copy of the License at
74a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
84a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      http://www.apache.org/licenses/LICENSE-2.0
94a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Unless required by applicable law or agreed to in writing, software
114a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * distributed under the License is distributed on an "AS IS" BASIS,
124a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * See the License for the specific language governing permissions and
144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * limitations under the License.
154a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */
164a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
17db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall// #define LOG_NDEBUG 0
18db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
19db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/*
20db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * The CommandListener, FrameworkListener don't allow for
21db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * multiple calls in parallel to reach the BandwidthController.
22db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * If they ever were to allow it, then netd/ would need some tweaking.
23db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */
24db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall#include <errno.h>
264a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <fcntl.h>
27db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall#include <stdio.h>
288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall#include <stdlib.h>
294a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <string.h>
304a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
314a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/socket.h>
324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/stat.h>
334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/types.h>
344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/wait.h>
354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <linux/netlink.h>
374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <linux/rtnetlink.h>
384a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <linux/pkt_sched.h>
394a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
404a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#define LOG_TAG "BandwidthController"
414a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <cutils/log.h>
424a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <cutils/properties.h>
434a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallextern "C" int logwrap(int argc, const char **argv, int background);
459e5e0ce62e88ddf9a09798eda51b0c270d354c8eJP Abgrallextern "C" int system_nosh(const char *command);
464a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
474a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include "BandwidthController.h"
484a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
49db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* Alphabetical */
50876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrallconst char BandwidthController::ALERT_IPT_TEMPLATE[] = "%s %s %s -m quota2 ! --quota %lld --name %s";
51db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::ALERT_RULE_POS_IN_COSTLY_CHAIN = 4;
52c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallconst char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert";
53db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char BandwidthController::IP6TABLES_PATH[] = "/system/bin/ip6tables";
54db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char BandwidthController::IPTABLES_PATH[] = "/system/bin/iptables";
55db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_CMD_ARGS = 32;
56db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_CMD_LEN = 1024;
57db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_IFACENAME_LEN = 64;
58db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256;
59db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
6011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrallbool BandwidthController::useLogwrapCall = false;
614a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
624a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall/**
634a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Some comments about the rules:
644a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *  * Ordering
654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      E.g. "-I INPUT -i rmnet0 --goto costly"
674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    - quota'd rules in the costly chain should be before penalty_box lookups.
684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * global quota vs per interface quota
704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   - global quota for all costly interfaces uses a single costly chain:
714a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    . initial rules
72bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -N costly_shared
73bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I INPUT -i iface0 --goto costly_shared
74bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I OUTPUT -o iface0 --goto costly_shared
75bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I costly_shared -m quota \! --quota 500000 \
76bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *          --jump REJECT --reject-with icmp-net-prohibited
77bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_shared --jump penalty_box
78bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_shared -m owner --socket-exists
798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *
804a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    . adding a new iface to this, E.g.:
81bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I INPUT -i iface1 --goto costly_shared
82bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I OUTPUT -o iface1 --goto costly_shared
834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   - quota per interface. This is achieve by having "costly" chains per quota.
854a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *     E.g. adding a new costly interface iface0 with its own quota:
864a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      iptables -N costly_iface0
874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      iptables -I INPUT -i iface0 --goto costly_iface0
884a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      iptables -I OUTPUT -o iface0 --goto costly_iface0
89bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_iface0 -m quota \! --quota 500000 \
90bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *          --jump REJECT --reject-with icmp-net-prohibited
91bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_iface0 --jump penalty_box
924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      iptables -A costly_iface0 -m owner --socket-exists
934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * penalty_box handling:
954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *  - only one penalty_box for all interfaces
964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   E.g  Adding an app:
97bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *    iptables -A penalty_box -m owner --uid-owner app_3 \
98bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *        --jump REJECT --reject-with icmp-net-prohibited
994a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */
100db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_CLEANUP_COMMANDS[] = {
1010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Cleanup rules. */
1020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-F",
1030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-t raw -F",
10439f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall    /* TODO: If at some point we need more user chains than here, then we will need
10539f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall     * a different cleanup approach.
10639f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall     */
107bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-X",  /* Should normally only be costly_shared, penalty_box, and costly_<iface>  */
1080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1094a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
110db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_SETUP_COMMANDS[] = {
1110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Created needed chains. */
112bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-N costly_shared",
1130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-N penalty_box",
1140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
116db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = {
1170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-F INPUT",
1180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-A INPUT -i lo --jump ACCEPT",
1190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-A INPUT -m owner --socket-exists", /* This is a tracking rule. */
1200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
1210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-F OUTPUT",
1220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-A OUTPUT -o lo --jump ACCEPT",
1230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-A OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
1240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
125bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-F costly_shared",
126bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-A costly_shared --jump penalty_box",
127bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-A costly_shared -m owner --socket-exists", /* This is a tracking rule. */
1280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* TODO(jpa): Figure out why iptables doesn't correctly return from this
1290dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * chain. For now, hack the chain exit with an ACCEPT.
1300dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     */
131bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-A costly_shared --jump ACCEPT",
1320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP AbgrallBandwidthController::BandwidthController(void) {
1354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char value[PROPERTY_VALUE_MAX];
1364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    property_get("persist.bandwidth.enable", value, "0");
1384a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    if (!strcmp(value, "1")) {
1394a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall        enableBandwidthControl();
1404a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
1414a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
14211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    property_get("persist.bandwidth.uselogwrap", value, "0");
14311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    useLogwrapCall = !strcmp(value, "1");
1444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
1454a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
14626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling) {
1470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
1488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
1493fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("runIpxtablesCmd(cmd=%s)", cmd);
15026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIptablesCmd(cmd, rejectHandling, IptIpV4);
15126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIptablesCmd(cmd, rejectHandling, IptIpV6);
1520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
1530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
1540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
15526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) {
15626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
15726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    memset(buffer, '\0', buffSize);  // strncpy() is not filling leftover with '\0'
15826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    strncpy(buffer, src, buffSize);
15926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return buffer[buffSize - 1];
16026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall}
16126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
1628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandling,
1638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                                        IptIpVer iptVer) {
16426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    char buffer[MAX_CMD_LEN];
1654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    const char *argv[MAX_CMD_ARGS];
16626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    int argc = 0;
1674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char *next = buffer;
1684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char *tmp;
16911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    int res;
1704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1710dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string fullCmd = cmd;
17226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
17326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (rejectHandling == IptRejectAdd) {
1740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        fullCmd += " --jump REJECT --reject-with";
17526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        switch (iptVer) {
17626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        case IptIpV4:
1778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            fullCmd += " icmp-net-prohibited";
1788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
17926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        case IptIpV6:
1808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            fullCmd += " icmp6-adm-prohibited";
1818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
1820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
1830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
1840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
18511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    fullCmd.insert(0, " ");
18611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
1874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
18811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    if (!useLogwrapCall) {
1899e5e0ce62e88ddf9a09798eda51b0c270d354c8eJP Abgrall        res = system_nosh(fullCmd.c_str());
19011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    } else {
19111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
1925ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("iptables command too long");
1934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            return -1;
1944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall        }
195fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
19611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        argc = 0;
19711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        while ((tmp = strsep(&next, " "))) {
19811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall            argv[argc++] = tmp;
19911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall            if (argc >= MAX_CMD_ARGS) {
2005ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block                ALOGE("iptables argument overflow");
20111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall                return -1;
20211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall            }
20311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        }
20411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall
20511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        argv[argc] = NULL;
20611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        res = logwrap(argc, argv, 0);
20711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    }
20811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    if (res) {
2095ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("runIptablesCmd(): failed %s res=%d", fullCmd.c_str(), res);
21011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    }
21111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    return res;
2124a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2134a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::enableBandwidthControl(void) {
215fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res;
2168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
217db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Let's pretend we started from scratch ... */
2188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    sharedQuotaIfaces.clear();
2198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    quotaIfaces.clear();
2208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    naughtyAppUids.clear();
221db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    globalAlertBytes = 0;
222c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount = 0;
223db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    sharedQuotaBytes = sharedAlertBytes = 0;
224db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
225db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
226db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Some of the initialCommands are allowed to fail */
227db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
228db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
229db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*),
230db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_SETUP_COMMANDS, RunCmdFailureOk);
231db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    res = runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*),
232db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad);
2338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
234fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
2354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2384a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::disableBandwidthControl(void) {
239db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* The IPT_CLEANUP_COMMANDS are allowed to fail. */
240db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
241db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
242fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
2434a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runCommands(int numCommands, const char *commands[],
2468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                                     RunCmdErrHandling cmdErrHandling) {
247fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
2483fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("runCommands(): %d commands", numCommands);
249fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
25026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd);
25126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        if (res && cmdErrHandling != RunCmdFailureBad)
252fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            return res;
253fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
25426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return cmdErrHandling == RunCmdFailureBad ? res : 0;
255fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
256fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
2570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallstd::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) {
258fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
2598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
2608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
261fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
262fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
2638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
2648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
2658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
2668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
2678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
2688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
2698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
2708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
2718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
2728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
273fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
2748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s penalty_box -m owner --uid-owner %d", opFlag, uid);
2758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
2768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
277fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
278fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
279fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
280fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
28126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd);
282fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
283fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
284fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
28526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove);
286fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
287fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
28826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) {
289fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    char cmd[MAX_CMD_LEN];
290fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int uidNum;
29126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *failLogTemplate;
29226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    IptOp op;
293fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int appUids[numUids];
29426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string naughtyCmd;
2958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
29626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (appOp) {
29726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpAdd:
2988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpInsert;
2998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to add app uid %d to penalty box.";
3008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
30126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpRemove:
3028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpDelete;
3038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to delete app uid %d from penalty box.";
3048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
30526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
30626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
307fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
308fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        appUids[uidNum] = atol(appStrUids[uidNum]);
309fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (appUids[uidNum] == 0) {
3105ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE(failLogTemplate, appUids[uidNum]);
311fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_parse;
312fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
313fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
314fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
315fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
31626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        naughtyCmd = makeIptablesNaughtyCmd(op, appUids[uidNum]);
31726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) {
3185ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE(failLogTemplate, appUids[uidNum]);
319fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_with_uidNum;
320fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
321fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
322fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
323fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
32426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum:
325fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    /* Try to remove the uid that failed in any case*/
32626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]);
32726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd);
32826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse:
32926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return -1;
3304a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
3314a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
33226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
333fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
3348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
3358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
3360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
3373fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota);
3380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
339fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
3408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
3418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
3428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
3448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
3458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
3478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
3488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
3498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
350fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
3518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
352bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    // The requried IP version specific --jump REJECT ... will be added later.
3538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota,
3548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall             costName);
3558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
3568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
3570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
3580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
3590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
36026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
3610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
3620dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
3638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int ruleInsertPos = 1;
3640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
3650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
3660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
3670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* The "-N costly" is created upfront, no need to handle it here. */
36826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
36926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
370bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
3710dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
3720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
3730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-N %s", costCString);
37426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
3750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString);
37626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
3770dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s -m owner --socket-exists", costCString);
37826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
3790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        /* TODO(jpa): Figure out why iptables doesn't correctly return from this
3800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall         * chain. For now, hack the chain exit with an ACCEPT.
3810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall         */
3820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s --jump ACCEPT", costCString);
38326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
38426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
38526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
386bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
38726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
3880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
3890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
3908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
3918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* The alert rule comes 1st */
3928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        ruleInsertPos = 2;
3938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
3948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    snprintf(cmd, sizeof(cmd), "-I INPUT %d -i %s --goto %s", ruleInsertPos, ifn, costCString);
39526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
3968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    snprintf(cmd, sizeof(cmd), "-I OUTPUT %d -o %s --goto %s", ruleInsertPos, ifn, costCString);
39726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
3980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
3990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
4000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
40126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
4020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
4030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
4040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
4050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
4060dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
40726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
40826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
409bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
4100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
4110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
41226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
41326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
414bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
41526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
4160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
4170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    snprintf(cmd, sizeof(cmd), "-D INPUT -i %s --goto %s", ifn, costCString);
41926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    snprintf(cmd, sizeof(cmd), "-D OUTPUT -o %s --goto %s", ifn, costCString);
42126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
423bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    /* The "-N costly_shared" is created upfront, no need to handle it here. */
42426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (quotaType == QuotaUnique) {
4250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
42626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
427a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        snprintf(cmd, sizeof(cmd), "-X %s", costCString);
428a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
429fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
430fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
431fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
4324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4330dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
4344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char cmd[MAX_CMD_LEN];
4354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
436fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
43726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
4388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::string ifaceName;
4398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ;
440bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
44126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<std::string>::iterator it;
4424a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
4448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
4455ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
4468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
4478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
44826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
4495ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
45026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
45126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
45226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
4534a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4544a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    if (maxBytes == -1) {
455fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return removeInterfaceSharedQuota(ifn);
4564a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
4574a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4584a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /* Insert ingress quota. */
4590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
4600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
461fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
4624a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
463fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
4640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
46526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaShared);
4660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (sharedQuotaIfaces.empty()) {
4670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
46826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall            res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
4694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            if (res) {
4705ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block                ALOGE("Failed set quota rule");
471fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall                goto fail;
4724a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            }
473fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            sharedQuotaBytes = maxBytes;
474fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
4750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        sharedQuotaIfaces.push_front(ifaceName);
476fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
477fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
478fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
479fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    if (maxBytes != sharedQuotaBytes) {
4808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
481fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (res) {
4825ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", costName);
483fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail;
484fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
485fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        sharedQuotaBytes = maxBytes;
4864a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
4874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return 0;
488fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
489fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    fail:
4904a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /*
4914a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
4924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * rules in the kernel to see which ones need cleaning up.
493fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
494fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * which resets everything.
4954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     */
496fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    removeInterfaceSharedQuota(ifn);
4974a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return -1;
4984a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
4994a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */
501fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) {
5024a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
503fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
50426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
5050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::list<std::string>::iterator it;
506bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
5074a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
5095ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
51026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
51126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
5128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ifaceName = ifn;
51326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
5140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
5150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
516fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
5174a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
5195ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
520fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return -1;
5214a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
522fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
52326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaShared);
5240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    sharedQuotaIfaces.erase(it);
525fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
5260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (sharedQuotaIfaces.empty()) {
527fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        std::string quotaCmd;
528bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
52926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
5308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        sharedQuotaBytes = 0;
5318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (sharedAlertBytes) {
5328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            removeSharedAlert();
5338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            sharedAlertBytes = 0;
5348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        }
535fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
5364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return res;
5374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
5380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
5400dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
5410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
54226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
54326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
54426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
54526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
5460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
5488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
5495ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
5508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
5518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
5520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (maxBytes == -1) {
55326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return removeInterfaceQuota(iface);
5540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
5550dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
5575ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
55826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
55926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
56026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
56126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
56226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
5630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Insert ingress quota. */
5640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
5658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
5660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
5670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
5680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5690dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
57026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaUnique);
5710dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
57226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
5730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
5745ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed set quota rule");
5750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
5760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
5770dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
5790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    } else {
5818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
5820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
5835ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", iface);
5840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
5850dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
5868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        it->quota = maxBytes;
5870dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
5880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return 0;
5890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    fail:
5910dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /*
5920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
5930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * rules in the kernel to see which ones need cleaning up.
5940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
5950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * which resets everything.
5960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     */
5970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    removeInterfaceSharedQuota(ifn);
5980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return -1;
5990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
6000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
6028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return getInterfaceQuota("shared", bytes);
6038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
6068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
6078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
6088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int scanRes;
6098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", costName);
6118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "r");
6128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
6138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
6145ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
6158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    scanRes = fscanf(fp, "%lld", bytes);
6183fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes);
6198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
6208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return scanRes == 1 ? 0 : -1;
6218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) {
6240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
6260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
62726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
62826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
62926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
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;
6370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
6398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
6400dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
6410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
6445ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
6450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        return -1;
6460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* This also removes the quota command of CostlyIface chain. */
64926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaUnique);
6500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    quotaIfaces.erase(it);
6520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
6540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
6558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
6578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
6588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
6598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
6618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "w");
6628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
6638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
6645ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
6658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fprintf(fp, "%lld\n", bytes);
6688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
6698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return 0;
6708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
6738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
6748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
675876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    const char *ifaceLimiting;
6768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
6778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    switch (op) {
6798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
6808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
6818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
6828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
6838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
6848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
6858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
6868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
6878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
6888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
6898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
691876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    ifaceLimiting = "! -i lo+";
692876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "INPUT",
693876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall        bytes, alertName, alertName);
6948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
6958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
696876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    ifaceLimiting = "! -o lo+";
697876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "OUTPUT",
698876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall        bytes, alertName, alertName);
6998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
7008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
7018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
7028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
704c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
705c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
706c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *opFlag;
707c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *ifaceLimiting;
7088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
709c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
710c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    switch (op) {
711c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpInsert:
712c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-I";
713c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
714c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpReplace:
715c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-R";
716c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
717c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    default:
718c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpDelete:
719c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-D";
720c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
721c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
722c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
723c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    ifaceLimiting = "! -i lo+";
724c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "FORWARD",
725c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        bytes, alertName, alertName);
726c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
727c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    free(alertQuotaCmd);
728c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
729c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
730c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
731c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) {
732c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
7338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
7365ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
7378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
7408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, bytes);
7418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
7428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
743c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        if (globalAlertTetherCount) {
7443fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
745c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall            res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
746c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        }
7478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = bytes;
7498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
7508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
752c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlertInForwardChain(void) {
753c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
754c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
755c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
756c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount++;
7573fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
758c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
759c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
760c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
761c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but this is not the 1st
762c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tether, we are also done.
763c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
764c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount != 1) {
765c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
766c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
767c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
768c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only add the rule if this was the 1st tether added. */
769c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
770c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
771c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
772c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
7738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) {
7748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
775c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
7768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!globalAlertBytes) {
7795ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
7808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
783c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (globalAlertTetherCount) {
784c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
785c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
7868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = 0;
7878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
7888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
790c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::removeGlobalAlertInForwardChain(void) {
791c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
792c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
793c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
794c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertTetherCount) {
7955ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
796c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return -1;
797c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
798c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
799c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount--;
800c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
801c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
802c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but there are more
803c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tethers, we are also done.
804c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
805c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount >= 1) {
806c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
807c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
808c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
809c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only detete the rule if this was the last tether removed. */
810c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
811c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
812c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
813c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
8148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) {
8158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!sharedQuotaBytes) {
8165ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior shared quota set to set an alert");
8178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
8205ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
8218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert("shared", bytes, &sharedAlertBytes);
8248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) {
8278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert("shared", &sharedAlertBytes);
8288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
8318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
8328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
8345ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
8358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
8388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
8398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
8408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
8435ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior interface quota set to set an alert");
8448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert(iface, bytes, &it->alert);
8488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) {
8518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
8528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
8548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
8558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
8568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
8595ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for interface %s", iface);
8608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert(iface, &it->alert);
8648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
8678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
8688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainNameAndPos;
8698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
8708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
8718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
8735ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
8748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
8778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (*alertBytes) {
8788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, *alertBytes);
8798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
8808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN);
8818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-I", chainNameAndPos, bytes, alertName,
8828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                 alertName);
8838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
8848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(alertQuotaCmd);
8858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(chainNameAndPos);
8868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = bytes;
8888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
8898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
8938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
8948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainName;
8958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
8968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
8978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
8998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!*alertBytes) {
9005ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for %s alert", costName);
9018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&chainName, "costly_%s", costName);
9058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName, alertName);
9068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
9078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
9088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(chainName);
9098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = 0;
9118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
9128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
9138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
914db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
915db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/*
916db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of:
917db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
918db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *     pkts      bytes target     prot opt in     out     source               destination
919db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *        0        0 ACCEPT     all  --  rmnet0 wlan0   0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
920db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *        0        0 DROP       all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0            state INVALID
921db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *        0        0 ACCEPT     all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0
922db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *
923db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */
924a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp,
925a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall                                                std::string &extraProcessingInfo) {
926db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
927db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
928db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface0[MAX_IPT_OUTPUT_LINE_LEN];
929db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface1[MAX_IPT_OUTPUT_LINE_LEN];
930db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char rest[MAX_IPT_OUTPUT_LINE_LEN];
931db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
932db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *buffPtr;
933db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int64_t packets, bytes;
934db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
935db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
936db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        /* Clean up, so a failed parse can still print info */
937db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
938db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        res = sscanf(buffPtr, "%lld %lld ACCEPT all -- %s %s 0.%s",
939db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall                &packets, &bytes, iface0, iface1, rest);
9403fb42e026ffebab2c8f282e42501040121e32d83Steve Block        ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res,
941db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall             iface0, iface1, packets, bytes, rest, buffPtr);
942a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall        extraProcessingInfo += buffPtr;
943a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall
944db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if (res != 5) {
945db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            continue;
946db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
947db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) {
9483fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
949db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxPackets = packets;
950db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxBytes = bytes;
951db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) {
9523fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets);
953db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txPackets = packets;
954db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txBytes = bytes;
955db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
956db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
957db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Failure if rx or tx was not found */
958db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0;
959db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
960db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
961db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
962db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) {
963db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *msg;
964db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(),
965db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            rxBytes, rxPackets, txBytes, txPackets);
966db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return msg;
967db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
968db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
969a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::getTetherStats(TetherStats &stats, std::string &extraProcessingInfo) {
970db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
971db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    std::string fullCmd;
972db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    FILE *iptOutput;
973db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    const char *cmd;
974db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
975db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (stats.rxBytes != -1 || stats.txBytes != -1) {
9765ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Unexpected input stats. Byte counts should be -1.");
977db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
978db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
979db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
980db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /*
981db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Why not use some kind of lib to talk to iptables?
982db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Because the only libs are libiptc and libip6tc in iptables, and they are
983db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * not easy to use. They require the known iptables match modules to be
984db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * preloaded/linked, and require apparently a lot of wrapper code to get
985db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * the wanted info.
986db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     */
987db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    fullCmd = IPTABLES_PATH;
988db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    fullCmd += " -nvx -L FORWARD";
989db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    iptOutput = popen(fullCmd.c_str(), "r");
990db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (!iptOutput) {
9915ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
992a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall            extraProcessingInfo += "Failed to run iptables.";
993db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
994db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
995a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall    res = parseForwardChainStats(stats, iptOutput, extraProcessingInfo);
996db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    pclose(iptOutput);
997db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
998db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Currently NatController doesn't do ipv6 tethering, so we are done. */
999db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return res;
1000db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1001