BandwidthController.cpp revision db7da58e8d2aa021060098057f944ef754be06e3
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);
454a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
464a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include "BandwidthController.h"
474a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
48db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* Alphabetical */
49876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrallconst char BandwidthController::ALERT_IPT_TEMPLATE[] = "%s %s %s -m quota2 ! --quota %lld --name %s";
50db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::ALERT_RULE_POS_IN_COSTLY_CHAIN = 4;
51db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char BandwidthController::IP6TABLES_PATH[] = "/system/bin/ip6tables";
52db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char BandwidthController::IPTABLES_PATH[] = "/system/bin/iptables";
53db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_CMD_ARGS = 32;
54db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_CMD_LEN = 1024;
55db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_IFACENAME_LEN = 64;
56db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256;
57db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
5811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrallbool BandwidthController::useLogwrapCall = false;
594a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
604a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall/**
614a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Some comments about the rules:
624a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *  * Ordering
634a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
644a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      E.g. "-I INPUT -i rmnet0 --goto costly"
654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    - quota'd rules in the costly chain should be before penalty_box lookups.
664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * global quota vs per interface quota
684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   - global quota for all costly interfaces uses a single costly chain:
694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    . initial rules
70bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -N costly_shared
71bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I INPUT -i iface0 --goto costly_shared
72bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I OUTPUT -o iface0 --goto costly_shared
73bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I costly_shared -m quota \! --quota 500000 \
74bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *          --jump REJECT --reject-with icmp-net-prohibited
75bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_shared --jump penalty_box
76bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_shared -m owner --socket-exists
778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *
784a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    . adding a new iface to this, E.g.:
79bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I INPUT -i iface1 --goto costly_shared
80bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I OUTPUT -o iface1 --goto costly_shared
814a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
824a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   - quota per interface. This is achieve by having "costly" chains per quota.
834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *     E.g. adding a new costly interface iface0 with its own quota:
844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      iptables -N costly_iface0
854a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      iptables -I INPUT -i iface0 --goto costly_iface0
864a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      iptables -I OUTPUT -o iface0 --goto costly_iface0
87bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_iface0 -m quota \! --quota 500000 \
88bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *          --jump REJECT --reject-with icmp-net-prohibited
89bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_iface0 --jump penalty_box
904a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      iptables -A costly_iface0 -m owner --socket-exists
914a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * penalty_box handling:
934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *  - only one penalty_box for all interfaces
944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   E.g  Adding an app:
95bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *    iptables -A penalty_box -m owner --uid-owner app_3 \
96bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *        --jump REJECT --reject-with icmp-net-prohibited
974a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */
98db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_CLEANUP_COMMANDS[] = {
990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Cleanup rules. */
1000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-F",
1010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-t raw -F",
10239f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall    /* TODO: If at some point we need more user chains than here, then we will need
10339f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall     * a different cleanup approach.
10439f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall     */
105bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-X",  /* Should normally only be costly_shared, penalty_box, and costly_<iface>  */
1060dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1074a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
108db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_SETUP_COMMANDS[] = {
1090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Created needed chains. */
110bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-N costly_shared",
1110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-N penalty_box",
1120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
114db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = {
1150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-F INPUT",
1160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-A INPUT -i lo --jump ACCEPT",
1170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-A INPUT -m owner --socket-exists", /* This is a tracking rule. */
1180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
1190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-F OUTPUT",
1200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-A OUTPUT -o lo --jump ACCEPT",
1210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-A OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
1220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
123bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-F costly_shared",
124bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-A costly_shared --jump penalty_box",
125bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-A costly_shared -m owner --socket-exists", /* This is a tracking rule. */
1260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* TODO(jpa): Figure out why iptables doesn't correctly return from this
1270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * chain. For now, hack the chain exit with an ACCEPT.
1280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     */
129bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-A costly_shared --jump ACCEPT",
1300dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1314a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP AbgrallBandwidthController::BandwidthController(void) {
1334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char value[PROPERTY_VALUE_MAX];
1344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    property_get("persist.bandwidth.enable", value, "0");
1364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    if (!strcmp(value, "1")) {
1374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall        enableBandwidthControl();
1384a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
1394a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
14011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    property_get("persist.bandwidth.uselogwrap", value, "0");
14111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    useLogwrapCall = !strcmp(value, "1");
1424a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
1434a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
14426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling) {
1450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
1468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
147db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    LOGV("runIpxtablesCmd(cmd=%s)", cmd);
14826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIptablesCmd(cmd, rejectHandling, IptIpV4);
14926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIptablesCmd(cmd, rejectHandling, IptIpV6);
1500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
1510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
1520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
15326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) {
15426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
15526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    memset(buffer, '\0', buffSize);  // strncpy() is not filling leftover with '\0'
15626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    strncpy(buffer, src, buffSize);
15726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return buffer[buffSize - 1];
15826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall}
15926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
1608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandling,
1618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                                        IptIpVer iptVer) {
16226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    char buffer[MAX_CMD_LEN];
1634a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    const char *argv[MAX_CMD_ARGS];
16426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    int argc = 0;
1654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char *next = buffer;
1664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char *tmp;
16711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    int res;
1684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1690dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string fullCmd = cmd;
17026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
17126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (rejectHandling == IptRejectAdd) {
1720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        fullCmd += " --jump REJECT --reject-with";
17326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        switch (iptVer) {
17426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        case IptIpV4:
1758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            fullCmd += " icmp-net-prohibited";
1768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
17726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        case IptIpV6:
1788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            fullCmd += " icmp6-adm-prohibited";
1798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
1800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
1810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
1820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
18311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    fullCmd.insert(0, " ");
18411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
1854a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
18611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    if (!useLogwrapCall) {
18711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        res = system(fullCmd.c_str());
18811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    } else {
18911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
19011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall            LOGE("iptables command too long");
1914a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            return -1;
1924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall        }
193fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
19411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        argc = 0;
19511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        while ((tmp = strsep(&next, " "))) {
19611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall            argv[argc++] = tmp;
19711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall            if (argc >= MAX_CMD_ARGS) {
19811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall                LOGE("iptables argument overflow");
19911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall                return -1;
20011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall            }
20111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        }
20211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall
20311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        argv[argc] = NULL;
20411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        res = logwrap(argc, argv, 0);
20511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    }
20611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    if (res) {
20711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        LOGE("runIptablesCmd(): failed %s res=%d", fullCmd.c_str(), res);
20811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    }
20911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    return res;
2104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2114a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2124a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::enableBandwidthControl(void) {
213fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res;
2148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
215db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Let's pretend we started from scratch ... */
2168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    sharedQuotaIfaces.clear();
2178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    quotaIfaces.clear();
2188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    naughtyAppUids.clear();
219db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    globalAlertBytes = 0;
220db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    sharedQuotaBytes = sharedAlertBytes = 0;
221db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
222db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
223db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Some of the initialCommands are allowed to fail */
224db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
225db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
226db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*),
227db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_SETUP_COMMANDS, RunCmdFailureOk);
228db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    res = runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*),
229db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad);
2308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
231fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
2324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::disableBandwidthControl(void) {
236db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* The IPT_CLEANUP_COMMANDS are allowed to fail. */
237db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
238db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
239fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
2404a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2414a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runCommands(int numCommands, const char *commands[],
2438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                                     RunCmdErrHandling cmdErrHandling) {
244fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
245db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    LOGV("runCommands(): %d commands", numCommands);
246fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
24726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd);
24826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        if (res && cmdErrHandling != RunCmdFailureBad)
249fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            return res;
250fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
25126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return cmdErrHandling == RunCmdFailureBad ? res : 0;
252fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
253fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
2540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallstd::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) {
255fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
2568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
2578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
258fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
259fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
2608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
2618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
2628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
2638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
2648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
2658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
2668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
2678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
2688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
2698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
270fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
2718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s penalty_box -m owner --uid-owner %d", opFlag, uid);
2728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
2738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
274fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
275fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
276fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
277fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
27826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd);
279fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
280fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
281fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
28226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove);
283fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
284fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
28526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) {
286fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    char cmd[MAX_CMD_LEN];
287fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int uidNum;
28826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *failLogTemplate;
28926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    IptOp op;
290fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int appUids[numUids];
29126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string naughtyCmd;
2928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
29326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (appOp) {
29426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpAdd:
2958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpInsert;
2968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to add app uid %d to penalty box.";
2978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
29826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpRemove:
2998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpDelete;
3008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to delete app uid %d from penalty box.";
3018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
30226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
30326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
304fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
305fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        appUids[uidNum] = atol(appStrUids[uidNum]);
306fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (appUids[uidNum] == 0) {
30726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall            LOGE(failLogTemplate, appUids[uidNum]);
308fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_parse;
309fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
310fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
311fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
312fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
31326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        naughtyCmd = makeIptablesNaughtyCmd(op, appUids[uidNum]);
31426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) {
31526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall            LOGE(failLogTemplate, appUids[uidNum]);
316fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_with_uidNum;
317fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
318fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
319fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
320fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
32126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum:
322fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    /* Try to remove the uid that failed in any case*/
32326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]);
32426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd);
32526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse:
32626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return -1;
3274a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
3284a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
32926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
330fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
3318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
3328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
3330dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
334db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    LOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota);
3350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
336fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
3378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
3388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
3398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
3418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
3428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
3448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
3458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
3468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
347fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
3488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
349bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    // The requried IP version specific --jump REJECT ... will be added later.
3508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota,
3518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall             costName);
3528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
3538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
3540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
3550dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
3560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
35726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
3580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
3590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
3608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int ruleInsertPos = 1;
3610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
3620dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
3630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
3640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* The "-N costly" is created upfront, no need to handle it here. */
36526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
36626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
367bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
3680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
3690dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
3700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-N %s", costCString);
37126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
3720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString);
37326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
3740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s -m owner --socket-exists", costCString);
37526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
3760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        /* TODO(jpa): Figure out why iptables doesn't correctly return from this
3770dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall         * chain. For now, hack the chain exit with an ACCEPT.
3780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall         */
3790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s --jump ACCEPT", costCString);
38026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
38126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
38226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
383bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
38426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
3850dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
3860dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
3878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
3888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* The alert rule comes 1st */
3898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        ruleInsertPos = 2;
3908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
3918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    snprintf(cmd, sizeof(cmd), "-I INPUT %d -i %s --goto %s", ruleInsertPos, ifn, costCString);
39226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
3938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    snprintf(cmd, sizeof(cmd), "-I OUTPUT %d -o %s --goto %s", ruleInsertPos, ifn, costCString);
39426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
3950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
3960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
3970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
39826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
3990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
4000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
4010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
4020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
4030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
40426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
40526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
406bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
4070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
4080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
40926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
41026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
411bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
41226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
4130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
4140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    snprintf(cmd, sizeof(cmd), "-D INPUT -i %s --goto %s", ifn, costCString);
41626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    snprintf(cmd, sizeof(cmd), "-D OUTPUT -o %s --goto %s", ifn, costCString);
41826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
420bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    /* The "-N costly_shared" is created upfront, no need to handle it here. */
42126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (quotaType == QuotaUnique) {
4220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
42326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
424a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        snprintf(cmd, sizeof(cmd), "-X %s", costCString);
425a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
426fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
427fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
428fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
4294a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4300dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
4314a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char cmd[MAX_CMD_LEN];
4324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
433fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
43426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
4358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::string ifaceName;
4368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ;
437bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
43826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<std::string>::iterator it;
4394a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
4418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
4428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("Invalid bytes value. 1..max_int64.");
4438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
4448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
44526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
44626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        LOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
44726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
44826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
44926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
4504a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4514a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    if (maxBytes == -1) {
452fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return removeInterfaceSharedQuota(ifn);
4534a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
4544a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4554a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /* Insert ingress quota. */
4560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
4570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
458fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
4594a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
460fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
4610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
46226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaShared);
4630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (sharedQuotaIfaces.empty()) {
4640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
46526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall            res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
4664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            if (res) {
4678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                LOGE("Failed set quota rule");
468fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall                goto fail;
4694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            }
470fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            sharedQuotaBytes = maxBytes;
471fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
4720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        sharedQuotaIfaces.push_front(ifaceName);
473fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
474fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
475fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
476fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    if (maxBytes != sharedQuotaBytes) {
4778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
478fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (res) {
4798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            LOGE("Failed update quota for %s", costName);
480fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail;
481fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
482fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        sharedQuotaBytes = maxBytes;
4834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
4844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return 0;
485fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
486fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    fail:
4874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /*
4884a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
4894a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * rules in the kernel to see which ones need cleaning up.
490fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
491fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * which resets everything.
4924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     */
493fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    removeInterfaceSharedQuota(ifn);
4944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return -1;
4954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
4964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */
498fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) {
4994a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
500fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
50126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
5020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::list<std::string>::iterator it;
503bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
5044a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
50626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        LOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
50726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
50826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
5098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ifaceName = ifn;
51026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
5110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
5120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
513fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
5144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
5168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("No such iface %s to delete", ifn);
517fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return -1;
5184a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
519fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
52026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaShared);
5210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    sharedQuotaIfaces.erase(it);
522fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
5230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (sharedQuotaIfaces.empty()) {
524fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        std::string quotaCmd;
525bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
52626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
5278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        sharedQuotaBytes = 0;
5288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (sharedAlertBytes) {
5298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            removeSharedAlert();
5308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            sharedAlertBytes = 0;
5318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        }
532fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
5334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return res;
5344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
5350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
5370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
5380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
53926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
54026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
54126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
54226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
5430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
5458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
5468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("Invalid bytes value. 1..max_int64.");
5478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
5488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
5490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (maxBytes == -1) {
55026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return removeInterfaceQuota(iface);
5510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
5520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
55426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        LOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
55526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
55626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
55726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
55826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
55926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
5600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Insert ingress quota. */
5610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
5628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
5630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
5640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
5650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
56726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaUnique);
5680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
56926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
5700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
5718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            LOGE("Failed set quota rule");
5720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
5730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
5740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
5760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5770dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    } else {
5788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
5790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
5808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            LOGE("Failed update quota for %s", iface);
5810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
5820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
5838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        it->quota = maxBytes;
5840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
5850dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return 0;
5860dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5870dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    fail:
5880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /*
5890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
5900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * rules in the kernel to see which ones need cleaning up.
5910dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
5920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * which resets everything.
5930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     */
5940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    removeInterfaceSharedQuota(ifn);
5950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return -1;
5960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
5970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
5998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return getInterfaceQuota("shared", bytes);
6008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
6038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
6048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
6058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int scanRes;
6068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", costName);
6088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "r");
6098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
6108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
6118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("Reading quota %s failed (%s)", costName, strerror(errno));
6128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    scanRes = fscanf(fp, "%lld", bytes);
615db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    LOGV("Read quota res=%d bytes=%lld", scanRes, *bytes);
6168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
6178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return scanRes == 1 ? 0 : -1;
6188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) {
6210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
6230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
62426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
62526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
62626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
6270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
62926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        LOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
63026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
63126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
63226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
63326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
6340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
6368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
6370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
6380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6400dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
6418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("No such iface %s to delete", ifn);
6420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        return -1;
6430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* This also removes the quota command of CostlyIface chain. */
64626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaUnique);
6470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    quotaIfaces.erase(it);
6490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
6510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
6528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
6548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
6558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
6568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
6588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "w");
6598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
6608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
6618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
6628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fprintf(fp, "%lld\n", bytes);
6658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
6668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return 0;
6678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
6708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
6718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
672876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    const char *ifaceLimiting;
6738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
6748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    switch (op) {
6768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
6778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
6788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
6798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
6808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
6818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
6828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
6838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
6848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
6858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
6868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
688876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    ifaceLimiting = "! -i lo+";
689876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "INPUT",
690876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall        bytes, alertName, alertName);
6918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
6928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
693876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    ifaceLimiting = "! -o lo+";
694876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "OUTPUT",
695876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall        bytes, alertName, alertName);
6968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
6978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
6988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
6998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) {
7028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
7038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *alertName = "globalAlert";
7048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
7078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("Invalid bytes value. 1..max_int64.");
7088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
7118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, bytes);
7128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
7138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
7148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = bytes;
7168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
7178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) {
7208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
7218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *alertName = "globalAlert";
7238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!globalAlertBytes) {
7268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("No prior alert set");
7278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
7308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = 0;
7318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
7328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) {
7358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!sharedQuotaBytes) {
7368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("Need to have a prior shared quota set to set an alert");
7378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
7408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("Invalid bytes value. 1..max_int64.");
7418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert("shared", bytes, &sharedAlertBytes);
7448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) {
7478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert("shared", &sharedAlertBytes);
7488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
7518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
7528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
7548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("Invalid bytes value. 1..max_int64.");
7558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
7588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
7598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
7608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
7638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("Need to have a prior interface quota set to set an alert");
7648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert(iface, bytes, &it->alert);
7688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) {
7718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
7728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
7748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
7758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
7768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
7798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("No prior alert set for interface %s", iface);
7808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert(iface, &it->alert);
7848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
7878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
7888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainNameAndPos;
7898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
7918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
7938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("Invalid bytes value. 1..max_int64.");
7948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
7978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (*alertBytes) {
7988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, *alertBytes);
7998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
8008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN);
8018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-I", chainNameAndPos, bytes, alertName,
8028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                 alertName);
8038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
8048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(alertQuotaCmd);
8058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(chainNameAndPos);
8068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = bytes;
8088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
8098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
8138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
8148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainName;
8158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
8168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
8178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
8198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!*alertBytes) {
8208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        LOGE("No prior alert set for %s alert", costName);
8218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&chainName, "costly_%s", costName);
8258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName, alertName);
8268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
8278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
8288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(chainName);
8298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = 0;
8318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
8328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
834db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
835db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/*
836db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of:
837db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
838db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *     pkts      bytes target     prot opt in     out     source               destination
839db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *        0        0 ACCEPT     all  --  rmnet0 wlan0   0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
840db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *        0        0 DROP       all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0            state INVALID
841db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *        0        0 ACCEPT     all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0
842db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *
843db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */
844db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallint BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp) {
845db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
846db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
847db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface0[MAX_IPT_OUTPUT_LINE_LEN];
848db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface1[MAX_IPT_OUTPUT_LINE_LEN];
849db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char rest[MAX_IPT_OUTPUT_LINE_LEN];
850db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
851db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *buffPtr;
852db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int64_t packets, bytes;
853db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
854db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
855db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        /* Clean up, so a failed parse can still print info */
856db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
857db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        res = sscanf(buffPtr, "%lld %lld ACCEPT all -- %s %s 0.%s",
858db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall                &packets, &bytes, iface0, iface1, rest);
859db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        LOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res,
860db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall             iface0, iface1, packets, bytes, rest, buffPtr);
861db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if (res != 5) {
862db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            continue;
863db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
864db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) {
865db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            LOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
866db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxPackets = packets;
867db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxBytes = bytes;
868db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) {
869db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            LOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets);
870db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txPackets = packets;
871db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txBytes = bytes;
872db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
873db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
874db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Failure if rx or tx was not found */
875db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0;
876db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
877db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
878db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
879db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) {
880db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *msg;
881db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(),
882db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            rxBytes, rxPackets, txBytes, txPackets);
883db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return msg;
884db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
885db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
886db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallint BandwidthController::getTetherStats(TetherStats &stats) {
887db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
888db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    std::string fullCmd;
889db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    FILE *iptOutput;
890db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    const char *cmd;
891db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
892db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (stats.rxBytes != -1 || stats.txBytes != -1) {
893db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        LOGE("Unexpected input stats. Byte counts should be -1.");
894db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
895db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
896db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
897db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /*
898db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Why not use some kind of lib to talk to iptables?
899db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Because the only libs are libiptc and libip6tc in iptables, and they are
900db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * not easy to use. They require the known iptables match modules to be
901db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * preloaded/linked, and require apparently a lot of wrapper code to get
902db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * the wanted info.
903db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     */
904db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    fullCmd = IPTABLES_PATH;
905db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    fullCmd += " -nvx -L FORWARD";
906db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    iptOutput = popen(fullCmd.c_str(), "r");
907db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (!iptOutput) {
908db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            LOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
909db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
910db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
911db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    res = parseForwardChainStats(stats, iptOutput);
912db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    pclose(iptOutput);
913db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
914db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Currently NatController doesn't do ipv6 tethering, so we are done. */
915db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return res;
916db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
917