BandwidthController.cpp revision c8dc63b14f5bcef34a23061c17d3bbe66df51429
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>
4314150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand#include <logwrap/logwrap.h>
444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall#include "NetdConstants.h"
464a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include "BandwidthController.h"
474a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
48db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* Alphabetical */
4992009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall#define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %lld --name %s"
50db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::ALERT_RULE_POS_IN_COSTLY_CHAIN = 4;
51c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallconst char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert";
528e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_INPUT = "bw_INPUT";
538e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_FORWARD = "bw_FORWARD";
548e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_OUTPUT = "bw_OUTPUT";
558e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_RAW_PREROUTING = "bw_raw_PREROUTING";
568e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_MANGLE_POSTROUTING = "bw_mangle_POSTROUTING";
57db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_CMD_ARGS = 32;
58db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_CMD_LEN = 1024;
59db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_IFACENAME_LEN = 64;
60db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256;
61db7da58e8d2aa021060098057f944ef754be06e3JP 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.
6629e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      E.g. "-I bw_INPUT -i rmnet0 --jump costly"
674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    - quota'd rules in the costly chain should be before penalty_box lookups.
6829e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *    - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * global quota vs per interface quota
714a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   - global quota for all costly interfaces uses a single costly chain:
724a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    . initial rules
73bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -N costly_shared
7429e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_INPUT -i iface0 --jump costly_shared
7529e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_OUTPUT -o iface0 --jump costly_shared
76bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I costly_shared -m quota \! --quota 500000 \
77bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *          --jump REJECT --reject-with icmp-net-prohibited
78bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_shared --jump penalty_box
798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *
804a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    . adding a new iface to this, E.g.:
8129e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_INPUT -i iface1 --jump costly_shared
8229e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_OUTPUT -o iface1 --jump 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
8729e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_INPUT -i iface0 --jump costly_iface0
8829e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_OUTPUT -o iface0 --jump 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 *
934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * penalty_box handling:
944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *  - only one penalty_box for all interfaces
954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   E.g  Adding an app:
96bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *    iptables -A penalty_box -m owner --uid-owner app_3 \
97bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *        --jump REJECT --reject-with icmp-net-prohibited
984a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */
990031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_FLUSH_COMMANDS[] = {
1000031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /*
1010031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * Cleanup rules.
1020031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * Should normally include costly_<iface>, but we rely on the way they are setup
1030031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * to allow coexistance.
10439f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall     */
1050031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F bw_INPUT",
1060031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F bw_OUTPUT",
1070031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F bw_FORWARD",
1080031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F penalty_box",
1090031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F costly_shared",
110f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall
111f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t raw -F bw_raw_PREROUTING",
112f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t mangle -F bw_mangle_POSTROUTING",
1130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall};
1140031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
1150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall/* The cleanup commands assume flushing has been done. */
1160031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_CLEANUP_COMMANDS[] = {
1170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-X penalty_box",
1180031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-X costly_shared",
1190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1204a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
121db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_SETUP_COMMANDS[] = {
122bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-N costly_shared",
1230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-N penalty_box",
1240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
126db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = {
1270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */
1280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
1290031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
1300dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
131bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-A costly_shared --jump penalty_box",
132f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall
13392009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    "-t raw -A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */
13492009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    "-t mangle -A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */
1350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP AbgrallBandwidthController::BandwidthController(void) {
1384a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
1394a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
140ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrallint BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling,
141ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall                                         IptFailureLog failureHandling) {
1420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
1438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
1443fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("runIpxtablesCmd(cmd=%s)", cmd);
145ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    res |= runIptablesCmd(cmd, rejectHandling, IptIpV4, failureHandling);
146ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    res |= runIptablesCmd(cmd, rejectHandling, IptIpV6, failureHandling);
1470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
1480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
1490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
15026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) {
15126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
15226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    memset(buffer, '\0', buffSize);  // strncpy() is not filling leftover with '\0'
15326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    strncpy(buffer, src, buffSize);
15426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return buffer[buffSize - 1];
15526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall}
15626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
1578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandling,
158ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall                                        IptIpVer iptVer, IptFailureLog failureHandling) {
15926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    char buffer[MAX_CMD_LEN];
1604a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    const char *argv[MAX_CMD_ARGS];
16126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    int argc = 0;
1624a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char *next = buffer;
1634a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char *tmp;
16411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    int res;
16514150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    int status = 0;
1664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string fullCmd = cmd;
16826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
16926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (rejectHandling == IptRejectAdd) {
1700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        fullCmd += " --jump REJECT --reject-with";
17126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        switch (iptVer) {
17226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        case IptIpV4:
1738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            fullCmd += " icmp-net-prohibited";
1748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
17526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        case IptIpV6:
1768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            fullCmd += " icmp6-adm-prohibited";
1778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
1780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
1790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
1800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
18111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    fullCmd.insert(0, " ");
18211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
1834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
18414150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
18514150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand        ALOGE("iptables command too long");
18614150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand        return -1;
18714150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    }
18814150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand
18914150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    argc = 0;
19014150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    while ((tmp = strsep(&next, " "))) {
19114150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand        argv[argc++] = tmp;
19214150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand        if (argc >= MAX_CMD_ARGS) {
19314150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand            ALOGE("iptables argument overflow");
1944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            return -1;
1954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall        }
19614150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    }
197fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
19814150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    argv[argc] = NULL;
19914150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    res = android_fork_execvp(argc, (char **)argv, &status, false,
20014150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand            failureHandling == IptFailShow);
201c8dc63b14f5bcef34a23061c17d3bbe66df51429JP Abgrall    res = res || !WIFEXITED(status) || WEXITSTATUS(status);
202c8dc63b14f5bcef34a23061c17d3bbe66df51429JP Abgrall    if (res && failureHandling == IptFailShow) {
203c8dc63b14f5bcef34a23061c17d3bbe66df51429JP Abgrall      ALOGE("runIptablesCmd(): res=%d status=%d failed %s", res, status,
204c8dc63b14f5bcef34a23061c17d3bbe66df51429JP Abgrall            fullCmd.c_str());
20511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    }
20611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    return res;
2074a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2084a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2090031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::setupIptablesHooks(void) {
2100031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2110031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /* Some of the initialCommands are allowed to fail */
2120031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
2140031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
2160031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
2170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2180031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*),
2190031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_SETUP_COMMANDS, RunCmdFailureBad);
2200031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2210031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    return 0;
2220031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall}
2230031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2240031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::enableBandwidthControl(bool force) {
225fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res;
2260031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    char value[PROPERTY_VALUE_MAX];
2270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2280031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    if (!force) {
2290031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            property_get("persist.bandwidth.enable", value, "1");
2300031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            if (!strcmp(value, "0"))
2310031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall                    return 0;
2320031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    }
2338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
234db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Let's pretend we started from scratch ... */
2358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    sharedQuotaIfaces.clear();
2368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    quotaIfaces.clear();
2378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    naughtyAppUids.clear();
238db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    globalAlertBytes = 0;
239c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount = 0;
240db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    sharedQuotaBytes = sharedAlertBytes = 0;
241db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
2420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
244db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
2450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*),
246db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad);
2478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
248fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
2494a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2504a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2514a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2524a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::disableBandwidthControl(void) {
2530031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2540031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
255fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
2564a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2574a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runCommands(int numCommands, const char *commands[],
2598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                                     RunCmdErrHandling cmdErrHandling) {
260fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
261ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    IptFailureLog failureLogging = IptFailShow;
262ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    if (cmdErrHandling == RunCmdFailureOk) {
263ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        failureLogging = IptFailHide;
264ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    }
2653fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("runCommands(): %d commands", numCommands);
266fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
267ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd, failureLogging);
2680031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        if (res && cmdErrHandling != RunCmdFailureOk)
269fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            return res;
270fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
2710031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    return 0;
272fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
273fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
2740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallstd::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) {
275fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
2768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
2778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
278fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
279fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
2808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
2818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
2828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
2838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
2848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
2858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
2868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
2878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
2888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
2898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
290fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
2918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s penalty_box -m owner --uid-owner %d", opFlag, uid);
2928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
2938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
294fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
295fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
296fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
297fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
29826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd);
299fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
300fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
301fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
30226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove);
303fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
304fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
30526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) {
306fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    char cmd[MAX_CMD_LEN];
307fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int uidNum;
30826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *failLogTemplate;
30926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    IptOp op;
310fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int appUids[numUids];
31126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string naughtyCmd;
312b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall    std::list<int /*uid*/>::iterator it;
3138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
31426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (appOp) {
31526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpAdd:
3168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpInsert;
3178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to add app uid %d to penalty box.";
3188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
31926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpRemove:
3208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpDelete;
3218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to delete app uid %d from penalty box.";
3228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3230031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
3240031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected app Op %d", appOp);
3250031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
32626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
32726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
328fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
329fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        appUids[uidNum] = atol(appStrUids[uidNum]);
330fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (appUids[uidNum] == 0) {
3315ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE(failLogTemplate, appUids[uidNum]);
332fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_parse;
333fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
334fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
335fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
336fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
337b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        int uid = appUids[uidNum];
338b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        for (it = naughtyAppUids.begin(); it != naughtyAppUids.end(); it++) {
339b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (*it == uid)
340b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                break;
341b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        }
342b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        bool found = (it != naughtyAppUids.end());
343b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall
344b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        if (appOp == NaughtyAppOpRemove) {
345b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (!found) {
346b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                ALOGE("No such appUid %d to remove", uid);
347b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                return -1;
348b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            }
349b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            naughtyAppUids.erase(it);
350b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        } else {
351b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (found) {
352b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                ALOGE("appUid %d exists already", uid);
353b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                return -1;
354b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            }
355b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            naughtyAppUids.push_front(uid);
356b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        }
357b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall
358b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        naughtyCmd = makeIptablesNaughtyCmd(op, uid);
35926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) {
360b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            ALOGE(failLogTemplate, uid);
361fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_with_uidNum;
362fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
363fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
364fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
365fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
36626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum:
367fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    /* Try to remove the uid that failed in any case*/
36826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]);
36926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd);
37026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse:
37126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return -1;
3724a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
3734a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
37426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
375fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
3768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
3778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
3780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
3793fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota);
3800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
381fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
3828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
3838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
3848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
3868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
3878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
3898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
3908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
3918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
392fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
3938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
394bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    // The requried IP version specific --jump REJECT ... will be added later.
3958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota,
3968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall             costName);
3978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
3988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
3990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
4000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
4010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
40226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
4030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
4040031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    int res = 0, res1, res2;
4058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int ruleInsertPos = 1;
4060dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
4070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
4080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* The "-N costly" is created upfront, no need to handle it here. */
41026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
41126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
412bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
4130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
4140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
4150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        /*
4160031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * Flush the costly_<iface> is allowed to fail in case it didn't exist.
4170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * Creating a new one is allowed to fail in case it existed.
4180031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * This helps with netd restarts.
4190031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         */
4200031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
421ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res1 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-N %s", costCString);
423ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res2 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4240031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        res = (res1 && res2) || (!res1 && !res2);
4250031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString);
42726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
42826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
42926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
430bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
43126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
4320031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
4330031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected quotatype %d", quotaType);
4340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
4350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
4360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
4388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* The alert rule comes 1st */
4398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        ruleInsertPos = 2;
4408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
4410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
443ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString);
44626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4470031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4480031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
449ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4500031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString);
45226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
4540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
4550dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
45626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
4570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
4580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
4590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
4600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
4610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
46226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
46326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
464bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
4650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
4660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
46726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
46826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
469bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
47026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
4710031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
4720031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected quotatype %d", quotaType);
4730031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
4740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
4750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4760031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
47726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4780031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
47926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
481bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    /* The "-N costly_shared" is created upfront, no need to handle it here. */
48226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (quotaType == QuotaUnique) {
4830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
48426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
485a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        snprintf(cmd, sizeof(cmd), "-X %s", costCString);
486a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
487fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
488fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
489fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
4904a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4910dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
4924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char cmd[MAX_CMD_LEN];
4934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
494fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
49526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
4968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::string ifaceName;
4978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ;
498bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
49926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<std::string>::iterator it;
5004a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
5028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
5035ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
5048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
5058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
50626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
5075ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
50826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
50926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
51026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
5114a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5124a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    if (maxBytes == -1) {
513fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return removeInterfaceSharedQuota(ifn);
5144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5154a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5164a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /* Insert ingress quota. */
5170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
5180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
519fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
5204a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
521fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
5220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
52326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaShared);
5240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (sharedQuotaIfaces.empty()) {
5250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
52626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall            res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
5274a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            if (res) {
5285ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block                ALOGE("Failed set quota rule");
529fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall                goto fail;
5304a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            }
531fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            sharedQuotaBytes = maxBytes;
532fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
5330dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        sharedQuotaIfaces.push_front(ifaceName);
534fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
535fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
536fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
537fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    if (maxBytes != sharedQuotaBytes) {
5388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
539fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (res) {
5405ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", costName);
541fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail;
542fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
543fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        sharedQuotaBytes = maxBytes;
5444a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5454a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return 0;
546fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
547fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    fail:
5484a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /*
5494a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
5504a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * rules in the kernel to see which ones need cleaning up.
551fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
552fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * which resets everything.
5534a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     */
554fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    removeInterfaceSharedQuota(ifn);
5554a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return -1;
5564a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
5574a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */
559fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) {
5604a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
561fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
56226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
5630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::list<std::string>::iterator it;
564bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
5654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
5675ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
56826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
56926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
5708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ifaceName = ifn;
57126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
5720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
5730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
574fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
5754a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
5775ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
578fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return -1;
5794a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
580fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
58126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaShared);
5820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    sharedQuotaIfaces.erase(it);
583fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
5840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (sharedQuotaIfaces.empty()) {
585fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        std::string quotaCmd;
586bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
58726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
5888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        sharedQuotaBytes = 0;
5898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (sharedAlertBytes) {
5908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            removeSharedAlert();
5918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            sharedAlertBytes = 0;
5928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        }
593fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
5944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return res;
5954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
5960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
5980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
5990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
60026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
60126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
60226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
60326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
6040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
6068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
6075ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
6088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (maxBytes == -1) {
61126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return removeInterfaceQuota(iface);
6120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
6155ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
61626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
61726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
61826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
61926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
62026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
6210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Insert ingress quota. */
6220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
6238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
6240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
6250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
62826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaUnique);
6290dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
63026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
6310dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
6325ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed set quota rule");
6330dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
6340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
6350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
6370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    } else {
6398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
6400dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
6415ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", iface);
6420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
6430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
6448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        it->quota = maxBytes;
6450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return 0;
6470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    fail:
6490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /*
6500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
6510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * rules in the kernel to see which ones need cleaning up.
6520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
6530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * which resets everything.
6540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     */
6550dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    removeInterfaceSharedQuota(ifn);
6560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return -1;
6570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
6580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
6608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return getInterfaceQuota("shared", bytes);
6618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
6648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
6658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
6668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int scanRes;
6678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", costName);
6698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "r");
6708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
6718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
6725ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
6738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    scanRes = fscanf(fp, "%lld", bytes);
6763fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes);
6778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
6788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return scanRes == 1 ? 0 : -1;
6798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) {
6820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
6840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
68526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
68626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
68726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
6880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
6905ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
69126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
69226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
69326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
69426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
6950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
6978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
6980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
6990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
7025ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
7030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        return -1;
7040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7060dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* This also removes the quota command of CostlyIface chain. */
70726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaUnique);
7080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    quotaIfaces.erase(it);
7100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
7120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
7138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
7158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
7168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
7178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
7198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "w");
7208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
7218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
7225ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
7238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fprintf(fp, "%lld\n", bytes);
7268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
7278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return 0;
7288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
7318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
7338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
7348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    switch (op) {
7368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
7378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
7388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
7408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
7418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
7438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
7448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
7458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
74892009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT",
749c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
7508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
7518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
75292009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT",
753c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
7548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
7558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
7568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
7578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
759c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
760c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
761c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *opFlag;
7628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
763c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
764c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    switch (op) {
765c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpInsert:
766c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-I";
767c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
768c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpReplace:
769c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-R";
770c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
771c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    default:
772c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpDelete:
773c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-D";
774c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
775c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
776c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
77792009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD",
778c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
779c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
780c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    free(alertQuotaCmd);
781c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
782c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
783c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
784c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) {
785c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
7868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
7895ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
7908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
7938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, bytes);
7948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
7958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
796c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        if (globalAlertTetherCount) {
7973fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
798c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall            res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
799c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        }
8008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = bytes;
8028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
805c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlertInForwardChain(void) {
806c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
807c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
808c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
809c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount++;
8103fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
811c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
812c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
813c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
814c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but this is not the 1st
815c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tether, we are also done.
816c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
817c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount != 1) {
818c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
819c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
820c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
821c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only add the rule if this was the 1st tether added. */
822c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
823c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
824c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
825c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
8268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) {
8278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
828c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
8298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
8308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!globalAlertBytes) {
8325ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
8338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
836c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (globalAlertTetherCount) {
837c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
838c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
8398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = 0;
8408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
843c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::removeGlobalAlertInForwardChain(void) {
844c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
845c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
846c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
847c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertTetherCount) {
8485ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
849c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return -1;
850c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
851c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
852c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount--;
853c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
854c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
855c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but there are more
856c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tethers, we are also done.
857c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
858c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount >= 1) {
859c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
860c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
861c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
862c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only detete the rule if this was the last tether removed. */
863c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
864c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
865c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
866c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
8678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) {
8688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!sharedQuotaBytes) {
8695ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior shared quota set to set an alert");
8708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
8735ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
8748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert("shared", bytes, &sharedAlertBytes);
8778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) {
8808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert("shared", &sharedAlertBytes);
8818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
8848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
8858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
8875ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
8888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
8918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
8928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
8938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
8965ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior interface quota set to set an alert");
8978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert(iface, bytes, &it->alert);
9018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) {
9048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
9058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
9078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
9088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
9098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
9125ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for interface %s", iface);
9138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert(iface, &it->alert);
9178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
9208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
9218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainNameAndPos;
9228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
9238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
9248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
9265ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
9278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
9308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (*alertBytes) {
9318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, *alertBytes);
9328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
9338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN);
93492009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall        asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-I", chainNameAndPos, bytes, alertName);
9358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
9368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(alertQuotaCmd);
9378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(chainNameAndPos);
9388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = bytes;
9408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
9418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
9428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
9458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
9468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainName;
9478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
9488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
9498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
9518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!*alertBytes) {
9525ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for %s alert", costName);
9538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&chainName, "costly_%s", costName);
95792009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
9588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
9598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
9608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(chainName);
9618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = 0;
9638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
9648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
9658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
966db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
967db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/*
968db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of:
9690031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Chain FORWARD (policy RETURN 0 packets, 0 bytes)
970db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *     pkts      bytes target     prot opt in     out     source               destination
9710031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall *        0        0 RETURN     all  --  rmnet0 wlan0   0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
972db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *        0        0 DROP       all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0            state INVALID
9730031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall *        0        0 RETURN     all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0
974db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *
975db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */
976a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp,
977a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall                                                std::string &extraProcessingInfo) {
978db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
979db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
980db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface0[MAX_IPT_OUTPUT_LINE_LEN];
981db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface1[MAX_IPT_OUTPUT_LINE_LEN];
982db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char rest[MAX_IPT_OUTPUT_LINE_LEN];
983db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
984db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *buffPtr;
985db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int64_t packets, bytes;
986db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
987db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
988db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        /* Clean up, so a failed parse can still print info */
989db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
9900031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s",
991db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall                &packets, &bytes, iface0, iface1, rest);
9923fb42e026ffebab2c8f282e42501040121e32d83Steve Block        ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res,
993db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall             iface0, iface1, packets, bytes, rest, buffPtr);
994a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall        extraProcessingInfo += buffPtr;
995a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall
996db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if (res != 5) {
997db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            continue;
998db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
999db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) {
10003fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1001db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxPackets = packets;
1002db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxBytes = bytes;
1003db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) {
10043fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets);
1005db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txPackets = packets;
1006db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txBytes = bytes;
1007db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
1008db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1009db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Failure if rx or tx was not found */
1010db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0;
1011db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1012db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1013db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1014db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) {
1015db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *msg;
1016db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(),
1017db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            rxBytes, rxPackets, txBytes, txPackets);
1018db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return msg;
1019db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1020db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1021a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::getTetherStats(TetherStats &stats, std::string &extraProcessingInfo) {
1022db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
1023db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    std::string fullCmd;
1024db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    FILE *iptOutput;
1025db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    const char *cmd;
1026db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1027db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (stats.rxBytes != -1 || stats.txBytes != -1) {
10285ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Unexpected input stats. Byte counts should be -1.");
1029db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
1030db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1031db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1032db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /*
1033db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Why not use some kind of lib to talk to iptables?
1034db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Because the only libs are libiptc and libip6tc in iptables, and they are
1035db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * not easy to use. They require the known iptables match modules to be
1036db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * preloaded/linked, and require apparently a lot of wrapper code to get
1037db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * the wanted info.
1038db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     */
1039db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    fullCmd = IPTABLES_PATH;
10400031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    fullCmd += " -nvx -L natctrl_FORWARD";
1041db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    iptOutput = popen(fullCmd.c_str(), "r");
1042db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (!iptOutput) {
10435ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
1044a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall            extraProcessingInfo += "Failed to run iptables.";
1045db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
1046db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1047a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall    res = parseForwardChainStats(stats, iptOutput, extraProcessingInfo);
1048db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    pclose(iptOutput);
1049db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1050db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Currently NatController doesn't do ipv6 tethering, so we are done. */
1051db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return res;
1052db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1053