BandwidthController.cpp revision 92009c8effc75c2d70a6c9a1ac33ba2c60b78820
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);
20111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall
20214150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    if ((res || !WIFEXITED(status) || WEXITSTATUS(status)) &&
20314150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand            failureHandling == IptFailShow) {
20414150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand        ALOGE("runIptablesCmd(): failed %s res=%d status=%d", fullCmd.c_str(),
20514150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand                res, status);
20611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    }
20711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    return res;
2084a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2094a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2100031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::setupIptablesHooks(void) {
2110031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2120031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /* Some of the initialCommands are allowed to fail */
2130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2140031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
2150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2160031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
2170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
2180031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2190031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*),
2200031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_SETUP_COMMANDS, RunCmdFailureBad);
2210031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2220031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    return 0;
2230031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall}
2240031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2250031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::enableBandwidthControl(bool force) {
226fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res;
2270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    char value[PROPERTY_VALUE_MAX];
2280031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2290031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    if (!force) {
2300031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            property_get("persist.bandwidth.enable", value, "1");
2310031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            if (!strcmp(value, "0"))
2320031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall                    return 0;
2330031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    }
2348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
235db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Let's pretend we started from scratch ... */
2368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    sharedQuotaIfaces.clear();
2378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    quotaIfaces.clear();
2388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    naughtyAppUids.clear();
239db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    globalAlertBytes = 0;
240c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount = 0;
241db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    sharedQuotaBytes = sharedAlertBytes = 0;
242db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
2430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
245db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
2460031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*),
247db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad);
2488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
249fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
2504a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2514a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2524a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2534a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::disableBandwidthControl(void) {
2540031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2550031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
256fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
2574a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2584a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runCommands(int numCommands, const char *commands[],
2608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                                     RunCmdErrHandling cmdErrHandling) {
261fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
262ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    IptFailureLog failureLogging = IptFailShow;
263ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    if (cmdErrHandling == RunCmdFailureOk) {
264ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        failureLogging = IptFailHide;
265ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    }
2663fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("runCommands(): %d commands", numCommands);
267fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
268ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd, failureLogging);
2690031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        if (res && cmdErrHandling != RunCmdFailureOk)
270fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            return res;
271fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
2720031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    return 0;
273fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
274fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
2750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallstd::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) {
276fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
2778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
2788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
279fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
280fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
2818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
2828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
2838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
2848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
2858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
2868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
2878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
2888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
2898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
2908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
291fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
2928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s penalty_box -m owner --uid-owner %d", opFlag, uid);
2938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
2948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
295fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
296fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
297fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
298fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
29926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd);
300fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
301fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
302fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
30326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove);
304fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
305fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
30626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) {
307fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    char cmd[MAX_CMD_LEN];
308fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int uidNum;
30926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *failLogTemplate;
31026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    IptOp op;
311fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int appUids[numUids];
31226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string naughtyCmd;
313b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall    std::list<int /*uid*/>::iterator it;
3148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
31526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (appOp) {
31626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpAdd:
3178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpInsert;
3188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to add app uid %d to penalty box.";
3198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
32026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpRemove:
3218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpDelete;
3228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to delete app uid %d from penalty box.";
3238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3240031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
3250031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected app Op %d", appOp);
3260031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
32726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
32826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
329fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
330fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        appUids[uidNum] = atol(appStrUids[uidNum]);
331fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (appUids[uidNum] == 0) {
3325ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE(failLogTemplate, appUids[uidNum]);
333fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_parse;
334fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
335fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
336fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
337fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
338b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        int uid = appUids[uidNum];
339b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        for (it = naughtyAppUids.begin(); it != naughtyAppUids.end(); it++) {
340b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (*it == uid)
341b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                break;
342b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        }
343b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        bool found = (it != naughtyAppUids.end());
344b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall
345b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        if (appOp == NaughtyAppOpRemove) {
346b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (!found) {
347b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                ALOGE("No such appUid %d to remove", uid);
348b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                return -1;
349b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            }
350b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            naughtyAppUids.erase(it);
351b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        } else {
352b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (found) {
353b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                ALOGE("appUid %d exists already", uid);
354b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                return -1;
355b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            }
356b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            naughtyAppUids.push_front(uid);
357b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        }
358b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall
359b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        naughtyCmd = makeIptablesNaughtyCmd(op, uid);
36026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) {
361b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            ALOGE(failLogTemplate, uid);
362fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_with_uidNum;
363fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
364fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
365fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
366fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
36726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum:
368fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    /* Try to remove the uid that failed in any case*/
36926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]);
37026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd);
37126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse:
37226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return -1;
3734a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
3744a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
37526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
376fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
3778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
3788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
3790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
3803fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota);
3810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
382fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
3838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
3848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
3858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
3878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
3888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
3908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
3918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
3928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
393fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
3948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
395bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    // The requried IP version specific --jump REJECT ... will be added later.
3968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota,
3978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall             costName);
3988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
3998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
4000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
4010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
4020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
40326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
4040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
4050031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    int res = 0, res1, res2;
4068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int ruleInsertPos = 1;
4070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
4080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
4090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* The "-N costly" is created upfront, no need to handle it here. */
41126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
41226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
413bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
4140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
4150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
4160031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        /*
4170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * Flush the costly_<iface> is allowed to fail in case it didn't exist.
4180031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * Creating a new one is allowed to fail in case it existed.
4190031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * This helps with netd restarts.
4200031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         */
4210031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
422ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res1 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-N %s", costCString);
424ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res2 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4250031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        res = (res1 && res2) || (!res1 && !res2);
4260031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString);
42826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
42926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
43026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
431bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
43226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
4330031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
4340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected quotatype %d", quotaType);
4350031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
4360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
4370dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
4398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* The alert rule comes 1st */
4408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        ruleInsertPos = 2;
4418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
4420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
444ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4460031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString);
44726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4480031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4490031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
450ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4520031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString);
45326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
4550dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
4560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
45726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
4580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
4590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
4600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
4610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
4620dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
46326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
46426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
465bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
4660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
4670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
46826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
46926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
470bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
47126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
4720031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
4730031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected quotatype %d", quotaType);
4740031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
4750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
4760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4770031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
47826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4790031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
48026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
482bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    /* The "-N costly_shared" is created upfront, no need to handle it here. */
48326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (quotaType == QuotaUnique) {
4840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
48526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
486a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        snprintf(cmd, sizeof(cmd), "-X %s", costCString);
487a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
488fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
489fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
490fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
4914a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
4920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
4934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char cmd[MAX_CMD_LEN];
4944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
495fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
49626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
4978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::string ifaceName;
4988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ;
499bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
50026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<std::string>::iterator it;
5014a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
5038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
5045ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
5058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
5068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
50726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
5085ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
50926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
51026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
51126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
5124a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5134a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    if (maxBytes == -1) {
514fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return removeInterfaceSharedQuota(ifn);
5154a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5164a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5174a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /* Insert ingress quota. */
5180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
5190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
520fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
5214a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
522fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
5230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
52426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaShared);
5250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (sharedQuotaIfaces.empty()) {
5260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
52726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall            res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
5284a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            if (res) {
5295ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block                ALOGE("Failed set quota rule");
530fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall                goto fail;
5314a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            }
532fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            sharedQuotaBytes = maxBytes;
533fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
5340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        sharedQuotaIfaces.push_front(ifaceName);
535fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
536fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
537fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
538fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    if (maxBytes != sharedQuotaBytes) {
5398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
540fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (res) {
5415ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", costName);
542fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail;
543fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
544fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        sharedQuotaBytes = maxBytes;
5454a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5464a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return 0;
547fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
548fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    fail:
5494a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /*
5504a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
5514a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * rules in the kernel to see which ones need cleaning up.
552fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
553fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * which resets everything.
5544a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     */
555fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    removeInterfaceSharedQuota(ifn);
5564a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return -1;
5574a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
5584a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */
560fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) {
5614a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
562fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
56326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
5640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::list<std::string>::iterator it;
565bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
5664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
5685ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
56926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
57026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
5718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ifaceName = ifn;
57226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
5730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
5740dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
575fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
5764a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5770dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
5785ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
579fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return -1;
5804a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
581fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
58226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaShared);
5830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    sharedQuotaIfaces.erase(it);
584fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
5850dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (sharedQuotaIfaces.empty()) {
586fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        std::string quotaCmd;
587bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
58826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
5898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        sharedQuotaBytes = 0;
5908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (sharedAlertBytes) {
5918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            removeSharedAlert();
5928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            sharedAlertBytes = 0;
5938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        }
594fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
5954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return res;
5964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
5970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
5990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
6000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
60126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
60226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
60326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
60426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
6050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
6078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
6085ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
6098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (maxBytes == -1) {
61226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return removeInterfaceQuota(iface);
6130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
6165ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
61726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
61826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
61926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
62026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
62126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
6220dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Insert ingress quota. */
6230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
6248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
6250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
6260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
62926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaUnique);
6300dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
63126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
6320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
6335ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed set quota rule");
6340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
6350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
6360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
6380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    } else {
6408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
6410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
6425ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", iface);
6430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
6440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
6458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        it->quota = maxBytes;
6460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return 0;
6480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    fail:
6500dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /*
6510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
6520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * rules in the kernel to see which ones need cleaning up.
6530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
6540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * which resets everything.
6550dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     */
6560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    removeInterfaceSharedQuota(ifn);
6570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return -1;
6580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
6590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
6618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return getInterfaceQuota("shared", bytes);
6628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
6658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
6668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
6678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int scanRes;
6688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", costName);
6708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "r");
6718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
6728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
6735ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
6748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    scanRes = fscanf(fp, "%lld", bytes);
6773fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes);
6788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
6798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return scanRes == 1 ? 0 : -1;
6808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) {
6830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
6850dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
68626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
68726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
68826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
6890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
6915ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
69226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
69326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
69426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
69526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
6960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
6988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
6990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
7000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
7035ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
7040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        return -1;
7050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7060dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* This also removes the quota command of CostlyIface chain. */
70826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaUnique);
7090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    quotaIfaces.erase(it);
7110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
7130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
7148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
7168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
7178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
7188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
7208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "w");
7218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
7228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
7235ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
7248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fprintf(fp, "%lld\n", bytes);
7278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
7288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return 0;
7298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
7328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
7348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
7358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    switch (op) {
7378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
7388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
7398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
7418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
7428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
7448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
7458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
7468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
74992009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT",
750c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
7518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
7528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
75392009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT",
754c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
7558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
7568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
7578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
7588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
760c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
761c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
762c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *opFlag;
7638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
764c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
765c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    switch (op) {
766c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpInsert:
767c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-I";
768c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
769c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpReplace:
770c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-R";
771c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
772c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    default:
773c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpDelete:
774c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-D";
775c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
776c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
777c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
77892009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD",
779c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
780c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
781c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    free(alertQuotaCmd);
782c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
783c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
784c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
785c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) {
786c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
7878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
7905ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
7918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
7948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, bytes);
7958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
7968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
797c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        if (globalAlertTetherCount) {
7983fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
799c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall            res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
800c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        }
8018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = bytes;
8038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
806c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlertInForwardChain(void) {
807c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
808c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
809c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
810c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount++;
8113fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
812c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
813c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
814c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
815c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but this is not the 1st
816c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tether, we are also done.
817c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
818c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount != 1) {
819c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
820c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
821c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
822c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only add the rule if this was the 1st tether added. */
823c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
824c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
825c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
826c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
8278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) {
8288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
829c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
8308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
8318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!globalAlertBytes) {
8335ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
8348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
837c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (globalAlertTetherCount) {
838c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
839c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
8408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = 0;
8418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
844c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::removeGlobalAlertInForwardChain(void) {
845c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
846c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
847c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
848c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertTetherCount) {
8495ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
850c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return -1;
851c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
852c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
853c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount--;
854c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
855c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
856c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but there are more
857c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tethers, we are also done.
858c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
859c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount >= 1) {
860c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
861c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
862c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
863c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only detete the rule if this was the last tether removed. */
864c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
865c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
866c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
867c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
8688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) {
8698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!sharedQuotaBytes) {
8705ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior shared quota set to set an alert");
8718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
8745ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
8758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert("shared", bytes, &sharedAlertBytes);
8788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) {
8818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert("shared", &sharedAlertBytes);
8828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
8858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
8868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
8885ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
8898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
8928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
8938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
8948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
8975ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior interface quota set to set an alert");
8988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert(iface, bytes, &it->alert);
9028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) {
9058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
9068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
9088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
9098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
9108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
9135ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for interface %s", iface);
9148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert(iface, &it->alert);
9188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
9218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
9228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainNameAndPos;
9238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
9248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
9258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
9275ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
9288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
9318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (*alertBytes) {
9328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, *alertBytes);
9338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
9348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN);
93592009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall        asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-I", chainNameAndPos, bytes, alertName);
9368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
9378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(alertQuotaCmd);
9388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(chainNameAndPos);
9398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = bytes;
9418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
9428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
9438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
9468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
9478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainName;
9488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
9498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
9508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
9528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!*alertBytes) {
9535ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for %s alert", costName);
9548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&chainName, "costly_%s", costName);
95892009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
9598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
9608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
9618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(chainName);
9628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = 0;
9648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
9658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
9668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
967db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
968db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/*
969db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of:
9700031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Chain FORWARD (policy RETURN 0 packets, 0 bytes)
971db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *     pkts      bytes target     prot opt in     out     source               destination
9720031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall *        0        0 RETURN     all  --  rmnet0 wlan0   0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
973db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *        0        0 DROP       all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0            state INVALID
9740031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall *        0        0 RETURN     all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0
975db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *
976db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */
977a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp,
978a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall                                                std::string &extraProcessingInfo) {
979db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
980db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
981db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface0[MAX_IPT_OUTPUT_LINE_LEN];
982db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface1[MAX_IPT_OUTPUT_LINE_LEN];
983db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char rest[MAX_IPT_OUTPUT_LINE_LEN];
984db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
985db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *buffPtr;
986db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int64_t packets, bytes;
987db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
988db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
989db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        /* Clean up, so a failed parse can still print info */
990db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
9910031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s",
992db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall                &packets, &bytes, iface0, iface1, rest);
9933fb42e026ffebab2c8f282e42501040121e32d83Steve Block        ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res,
994db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall             iface0, iface1, packets, bytes, rest, buffPtr);
995a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall        extraProcessingInfo += buffPtr;
996a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall
997db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if (res != 5) {
998db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            continue;
999db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
1000db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) {
10013fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1002db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxPackets = packets;
1003db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxBytes = bytes;
1004db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) {
10053fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets);
1006db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txPackets = packets;
1007db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txBytes = bytes;
1008db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
1009db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1010db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Failure if rx or tx was not found */
1011db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0;
1012db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1013db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1014db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1015db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) {
1016db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *msg;
1017db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(),
1018db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            rxBytes, rxPackets, txBytes, txPackets);
1019db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return msg;
1020db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1021db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1022a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::getTetherStats(TetherStats &stats, std::string &extraProcessingInfo) {
1023db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
1024db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    std::string fullCmd;
1025db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    FILE *iptOutput;
1026db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    const char *cmd;
1027db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1028db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (stats.rxBytes != -1 || stats.txBytes != -1) {
10295ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Unexpected input stats. Byte counts should be -1.");
1030db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
1031db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1032db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1033db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /*
1034db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Why not use some kind of lib to talk to iptables?
1035db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Because the only libs are libiptc and libip6tc in iptables, and they are
1036db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * not easy to use. They require the known iptables match modules to be
1037db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * preloaded/linked, and require apparently a lot of wrapper code to get
1038db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * the wanted info.
1039db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     */
1040db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    fullCmd = IPTABLES_PATH;
10410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    fullCmd += " -nvx -L natctrl_FORWARD";
1042db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    iptOutput = popen(fullCmd.c_str(), "r");
1043db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (!iptOutput) {
10445ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
1045a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall            extraProcessingInfo += "Failed to run iptables.";
1046db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
1047db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1048a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall    res = parseForwardChainStats(stats, iptOutput, extraProcessingInfo);
1049db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    pclose(iptOutput);
1050db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1051db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Currently NatController doesn't do ipv6 tethering, so we are done. */
1052db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return res;
1053db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1054