14a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall/*
24a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Copyright (C) 2011 The Android Open Source Project
34a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
44a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Licensed under the Apache License, Version 2.0 (the "License");
54a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * you may not use this file except in compliance with the License.
64a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * You may obtain a copy of the License at
74a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
84a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      http://www.apache.org/licenses/LICENSE-2.0
94a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Unless required by applicable law or agreed to in writing, software
114a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * distributed under the License is distributed on an "AS IS" BASIS,
124a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * See the License for the specific language governing permissions and
144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * limitations under the License.
154a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */
164a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
17db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall// #define LOG_NDEBUG 0
18db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
19db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/*
20db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * The CommandListener, FrameworkListener don't allow for
21db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * multiple calls in parallel to reach the BandwidthController.
22db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * If they ever were to allow it, then netd/ would need some tweaking.
23db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */
24db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall#include <errno.h>
264a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <fcntl.h>
27db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall#include <stdio.h>
288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall#include <stdlib.h>
294a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <string.h>
304a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
314a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/socket.h>
324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/stat.h>
334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/types.h>
344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <sys/wait.h>
354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
364a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <linux/netlink.h>
374a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <linux/rtnetlink.h>
384a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <linux/pkt_sched.h>
394a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
404a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#define LOG_TAG "BandwidthController"
414a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <cutils/log.h>
424a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <cutils/properties.h>
434a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
44c4bbfa247dbe4dda17e28694d49afd3ec0b06badGlenn Kastenextern "C" int logwrap(int argc, const char **argv);
459e5e0ce62e88ddf9a09798eda51b0c270d354c8eJP Abgrallextern "C" int system_nosh(const char *command);
464a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
470031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall#include "NetdConstants.h"
484a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include "BandwidthController.h"
494a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
50db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* Alphabetical */
51c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich#define ALERT_IPT_TEMPLATE "%s %s %s -m quota2 ! --quota %lld --name %s"
52db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::ALERT_RULE_POS_IN_COSTLY_CHAIN = 4;
53c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallconst char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert";
54db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_CMD_ARGS = 32;
55db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_CMD_LEN = 1024;
56db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_IFACENAME_LEN = 64;
57db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256;
58db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
5911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrallbool BandwidthController::useLogwrapCall = false;
604a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
614a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall/**
624a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Some comments about the rules:
634a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *  * Ordering
644a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
6529e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      E.g. "-I bw_INPUT -i rmnet0 --jump costly"
664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    - quota'd rules in the costly chain should be before penalty_box lookups.
6729e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *    - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * global quota vs per interface quota
704a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   - global quota for all costly interfaces uses a single costly chain:
714a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    . initial rules
72bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -N costly_shared
7329e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_INPUT -i iface0 --jump costly_shared
7429e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_OUTPUT -o iface0 --jump costly_shared
75bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -I costly_shared -m quota \! --quota 500000 \
76bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *          --jump REJECT --reject-with icmp-net-prohibited
77bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_shared --jump penalty_box
788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *
794a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    . adding a new iface to this, E.g.:
8029e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_INPUT -i iface1 --jump costly_shared
8129e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_OUTPUT -o iface1 --jump costly_shared
824a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   - quota per interface. This is achieve by having "costly" chains per quota.
844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *     E.g. adding a new costly interface iface0 with its own quota:
854a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *      iptables -N costly_iface0
8629e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_INPUT -i iface0 --jump costly_iface0
8729e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      iptables -I bw_OUTPUT -o iface0 --jump costly_iface0
88bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_iface0 -m quota \! --quota 500000 \
89bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *          --jump REJECT --reject-with icmp-net-prohibited
90bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *      iptables -A costly_iface0 --jump penalty_box
914a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * penalty_box handling:
934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *  - only one penalty_box for all interfaces
944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   E.g  Adding an app:
95bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *    iptables -A penalty_box -m owner --uid-owner app_3 \
96bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *        --jump REJECT --reject-with icmp-net-prohibited
974a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */
980031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_FLUSH_COMMANDS[] = {
990031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /*
1000031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * Cleanup rules.
1010031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * Should normally include costly_<iface>, but we rely on the way they are setup
1020031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * to allow coexistance.
10339f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall     */
1040031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F bw_INPUT",
1050031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F bw_OUTPUT",
1060031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F bw_FORWARD",
1070031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F penalty_box",
1080031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F costly_shared",
109f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall
110f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t raw -F bw_raw_PREROUTING",
111f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t mangle -F bw_mangle_POSTROUTING",
1120031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall};
1130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
1140031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall/* The cleanup commands assume flushing has been done. */
1150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_CLEANUP_COMMANDS[] = {
1160031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /* Delete hooks to custom chains. */
1170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-D INPUT -j bw_INPUT",
1180031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-D OUTPUT -j bw_OUTPUT",
1190031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-D FORWARD -j bw_FORWARD",
120f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall
121f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t raw -D bw_raw_PREROUTING",
122f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t mangle -D bw_mangle_POSTROUTING",
123f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall
1240031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-X bw_INPUT",
1250031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-X bw_OUTPUT",
1260031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-X bw_FORWARD",
1270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-X penalty_box",
1280031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-X costly_shared",
129f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall
130f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t raw -X bw_raw_PREROUTING",
131f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t mangle -X bw_mangle_POSTROUTING",
1320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
134db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_SETUP_COMMANDS[] = {
1350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Created needed chains. */
1360031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-N bw_INPUT",
1370031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-A INPUT -j bw_INPUT",
1380031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
1390031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-N bw_OUTPUT",
1400031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-A OUTPUT -j bw_OUTPUT",
1410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
1420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-N bw_FORWARD",
1430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-I FORWARD -j bw_FORWARD",
1440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
145bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-N costly_shared",
1460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    "-N penalty_box",
147f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall
148f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t raw -N bw_raw_PREROUTING",
149f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t raw -A PREROUTING -j bw_raw_PREROUTING",
150f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t mangle -N bw_mangle_POSTROUTING",
151f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t mangle -A POSTROUTING -j bw_mangle_POSTROUTING",
1520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
154db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = {
1550031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-A bw_INPUT -i lo --jump RETURN",
1560031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */
1570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
1580031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-A bw_OUTPUT -o lo --jump RETURN",
1590031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
1600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
161bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    "-A costly_shared --jump penalty_box",
162f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall
163f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t raw -A bw_raw_PREROUTING ! -i lo+ -m owner --socket-exists", /* This is a tracking rule. */
164f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t mangle -A bw_mangle_POSTROUTING ! -o lo+ -m owner --socket-exists", /* This is a tracking rule. */
1650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1674a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP AbgrallBandwidthController::BandwidthController(void) {
1684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char value[PROPERTY_VALUE_MAX];
1694a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
17011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    property_get("persist.bandwidth.uselogwrap", value, "0");
17111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    useLogwrapCall = !strcmp(value, "1");
1724a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
1734a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
174ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrallint BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling,
175ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall                                         IptFailureLog failureHandling) {
1760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
1778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
1783fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("runIpxtablesCmd(cmd=%s)", cmd);
179ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    res |= runIptablesCmd(cmd, rejectHandling, IptIpV4, failureHandling);
180ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    res |= runIptablesCmd(cmd, rejectHandling, IptIpV6, failureHandling);
1810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
1820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
1830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
18426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) {
18526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
18626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    memset(buffer, '\0', buffSize);  // strncpy() is not filling leftover with '\0'
18726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    strncpy(buffer, src, buffSize);
18826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return buffer[buffSize - 1];
18926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall}
19026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
1918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandling,
192ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall                                        IptIpVer iptVer, IptFailureLog failureHandling) {
19326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    char buffer[MAX_CMD_LEN];
1944a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    const char *argv[MAX_CMD_ARGS];
19526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    int argc = 0;
1964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char *next = buffer;
1974a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char *tmp;
19811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    int res;
1994a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string fullCmd = cmd;
20126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
20226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (rejectHandling == IptRejectAdd) {
2030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        fullCmd += " --jump REJECT --reject-with";
20426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        switch (iptVer) {
20526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        case IptIpV4:
2068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            fullCmd += " icmp-net-prohibited";
2078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
20826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        case IptIpV6:
2098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            fullCmd += " icmp6-adm-prohibited";
2108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
2110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
2120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
2130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
21411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    fullCmd.insert(0, " ");
21511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
2164a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
21711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    if (!useLogwrapCall) {
2189e5e0ce62e88ddf9a09798eda51b0c270d354c8eJP Abgrall        res = system_nosh(fullCmd.c_str());
21911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    } else {
22011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
2215ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("iptables command too long");
2224a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            return -1;
2234a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall        }
224fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
22511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        argc = 0;
22611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        while ((tmp = strsep(&next, " "))) {
22711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall            argv[argc++] = tmp;
22811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall            if (argc >= MAX_CMD_ARGS) {
2295ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block                ALOGE("iptables argument overflow");
23011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall                return -1;
23111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall            }
23211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        }
23311b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall
23411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        argv[argc] = NULL;
235c4bbfa247dbe4dda17e28694d49afd3ec0b06badGlenn Kasten        res = logwrap(argc, argv);
23611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    }
237ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    if (res && failureHandling == IptFailShow) {
2385ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("runIptablesCmd(): failed %s res=%d", fullCmd.c_str(), res);
23911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    }
24011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    return res;
2414a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2424a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::setupIptablesHooks(void) {
2440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /* Some of the initialCommands are allowed to fail */
2460031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2470031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
2480031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2490031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
2500031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
2510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2520031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*),
2530031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_SETUP_COMMANDS, RunCmdFailureBad);
2540031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2550031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    return 0;
2560031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2570031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall}
2580031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2590031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::enableBandwidthControl(bool force) {
260fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res;
2610031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    char value[PROPERTY_VALUE_MAX];
2620031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2630031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    if (!force) {
2640031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            property_get("persist.bandwidth.enable", value, "1");
2650031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            if (!strcmp(value, "0"))
2660031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall                    return 0;
2670031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    }
2688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
269db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Let's pretend we started from scratch ... */
2708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    sharedQuotaIfaces.clear();
2718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    quotaIfaces.clear();
2728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    naughtyAppUids.clear();
273db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    globalAlertBytes = 0;
274c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount = 0;
275db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    sharedQuotaBytes = sharedAlertBytes = 0;
276db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
2770031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2780031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
279db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
2800031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*),
281db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad);
2828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
283fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
2844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2854a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2864a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::disableBandwidthControl(void) {
2880031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2890031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
290fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
2914a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runCommands(int numCommands, const char *commands[],
2948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                                     RunCmdErrHandling cmdErrHandling) {
295fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
296ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    IptFailureLog failureLogging = IptFailShow;
297ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    if (cmdErrHandling == RunCmdFailureOk) {
298ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        failureLogging = IptFailHide;
299ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    }
3003fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("runCommands(): %d commands", numCommands);
301fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
302ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res = runIpxtablesCmd(commands[cmdNum], IptRejectNoAdd, failureLogging);
3030031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        if (res && cmdErrHandling != RunCmdFailureOk)
304fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            return res;
305fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
3060031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    return 0;
307fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
308fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
3090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallstd::string BandwidthController::makeIptablesNaughtyCmd(IptOp op, int uid) {
310fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
3118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
3128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
313fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
314fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
3158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
3168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
3178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
3198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
3208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
3228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
3238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
3248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
325fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
3268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s penalty_box -m owner --uid-owner %d", opFlag, uid);
3278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
3288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
329fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
330fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
331fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
332fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
33326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpAdd);
334fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
335fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
336fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
33726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return maninpulateNaughtyApps(numUids, appUids, NaughtyAppOpRemove);
338fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
339fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
34026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp) {
341fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    char cmd[MAX_CMD_LEN];
342fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int uidNum;
34326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *failLogTemplate;
34426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    IptOp op;
345fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int appUids[numUids];
34626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string naughtyCmd;
347b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall    std::list<int /*uid*/>::iterator it;
3488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
34926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (appOp) {
35026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpAdd:
3518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpInsert;
3528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to add app uid %d to penalty box.";
3538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
35426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case NaughtyAppOpRemove:
3558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpDelete;
3568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        failLogTemplate = "Failed to delete app uid %d from penalty box.";
3578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3580031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
3590031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected app Op %d", appOp);
3600031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
36126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
36226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
363fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
364fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        appUids[uidNum] = atol(appStrUids[uidNum]);
365fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (appUids[uidNum] == 0) {
3665ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE(failLogTemplate, appUids[uidNum]);
367fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_parse;
368fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
369fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
370fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
371fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
372b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        int uid = appUids[uidNum];
373b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        for (it = naughtyAppUids.begin(); it != naughtyAppUids.end(); it++) {
374b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (*it == uid)
375b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                break;
376b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        }
377b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        bool found = (it != naughtyAppUids.end());
378b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall
379b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        if (appOp == NaughtyAppOpRemove) {
380b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (!found) {
381b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                ALOGE("No such appUid %d to remove", uid);
382b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                return -1;
383b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            }
384b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            naughtyAppUids.erase(it);
385b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        } else {
386b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (found) {
387b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                ALOGE("appUid %d exists already", uid);
388b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                return -1;
389b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            }
390b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            naughtyAppUids.push_front(uid);
391b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        }
392b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall
393b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        naughtyCmd = makeIptablesNaughtyCmd(op, uid);
39426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        if (runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd)) {
395b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            ALOGE(failLogTemplate, uid);
396fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_with_uidNum;
397fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
398fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
399fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
400fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
40126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum:
402fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    /* Try to remove the uid that failed in any case*/
40326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    naughtyCmd = makeIptablesNaughtyCmd(IptOpDelete, appUids[uidNum]);
40426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    runIpxtablesCmd(naughtyCmd.c_str(), IptRejectAdd);
40526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse:
40626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return -1;
4074a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
4084a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
40926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
410fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
4118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
4128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
4130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4143fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota);
4150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
416fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
4178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
4188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
4198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
4208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
4218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
4228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
4238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
4248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
4258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
4268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
427fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
4288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
429bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    // The requried IP version specific --jump REJECT ... will be added later.
4308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&buff, "%s costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota,
4318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall             costName);
4328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
4338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
4340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
4350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
4360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
43726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
4380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
4390031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    int res = 0, res1, res2;
4408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int ruleInsertPos = 1;
4410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
4420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
4430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* The "-N costly" is created upfront, no need to handle it here. */
44526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
44626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
447bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
4480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
4490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
4500031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        /*
4510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * Flush the costly_<iface> is allowed to fail in case it didn't exist.
4520031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * Creating a new one is allowed to fail in case it existed.
4530031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * This helps with netd restarts.
4540031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         */
4550031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
456ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res1 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-N %s", costCString);
458ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        res2 = runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4590031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        res = (res1 && res2) || (!res1 && !res2);
4600031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s -j penalty_box", costCString);
46226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
46326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
46426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
465bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
46626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
4670031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
4680031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected quotatype %d", quotaType);
4690031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
4700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
4710dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
4738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* The alert rule comes 1st */
4748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        ruleInsertPos = 2;
4758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
4760031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4770031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
478ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4790031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4800031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString);
48126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4820031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4830031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
484ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    runIpxtablesCmd(cmd, IptRejectNoAdd, IptFailHide);
4850031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
4860031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString);
48726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
4880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
4890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
4900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
49126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
4920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
4930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
4940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
4950dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
4960dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
49726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
49826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
499bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costString = "costly_";
5000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
5010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
50226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
50326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
504bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        costCString = "costly_shared";
50526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
5060031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
5070031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected quotatype %d", quotaType);
5080031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
5090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
5100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5110031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
51226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
5130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
51426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
5150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
516bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    /* The "-N costly_shared" is created upfront, no need to handle it here. */
51726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (quotaType == QuotaUnique) {
5180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
51926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
520a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        snprintf(cmd, sizeof(cmd), "-X %s", costCString);
521a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        res |= runIpxtablesCmd(cmd, IptRejectNoAdd);
522fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
523fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
524fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
5254a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5260dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
5274a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char cmd[MAX_CMD_LEN];
5284a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
529fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
53026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
5318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::string ifaceName;
5328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ;
533bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
53426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<std::string>::iterator it;
5354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
5378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
5385ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
5398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
5408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
54126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
5425ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
54326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
54426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
54526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
5464a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5474a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    if (maxBytes == -1) {
548fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return removeInterfaceSharedQuota(ifn);
5494a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5504a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5514a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /* Insert ingress quota. */
5520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
5530dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
554fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
5554a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
556fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
5570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
55826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaShared);
5590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (sharedQuotaIfaces.empty()) {
5600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
56126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall            res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
5624a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            if (res) {
5635ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block                ALOGE("Failed set quota rule");
564fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall                goto fail;
5654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            }
566fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            sharedQuotaBytes = maxBytes;
567fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
5680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        sharedQuotaIfaces.push_front(ifaceName);
569fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
570fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
571fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
572fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    if (maxBytes != sharedQuotaBytes) {
5738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
574fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (res) {
5755ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", costName);
576fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail;
577fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
578fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        sharedQuotaBytes = maxBytes;
5794a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
5804a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return 0;
581fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
582fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    fail:
5834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /*
5844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
5854a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * rules in the kernel to see which ones need cleaning up.
586fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
587fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * which resets everything.
5884a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     */
589fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    removeInterfaceSharedQuota(ifn);
5904a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return -1;
5914a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
5924a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */
594fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) {
5954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
596fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
59726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
5980dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::list<std::string>::iterator it;
599bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
6004a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
6018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
6025ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
60326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
60426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
6058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ifaceName = ifn;
60626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
6070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
6080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
609fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
6104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
6110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
6125ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
613fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return -1;
6144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
615fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
61626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaShared);
6170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    sharedQuotaIfaces.erase(it);
618fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
6190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (sharedQuotaIfaces.empty()) {
620fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        std::string quotaCmd;
621bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
62226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
6238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        sharedQuotaBytes = 0;
6248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (sharedAlertBytes) {
6258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            removeSharedAlert();
6268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            sharedAlertBytes = 0;
6278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        }
628fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
6294a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return res;
6304a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
6310dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
6330dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
6340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
63526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
63626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
63726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
63826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
6390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
6418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
6425ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
6438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
6450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (maxBytes == -1) {
64626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return removeInterfaceQuota(iface);
6470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6480dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
6505ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
65126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
65226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
65326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
65426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
65526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
6560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Insert ingress quota. */
6570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
6588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
6590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
6600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6620dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
66326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaUnique);
6640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
66526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptRejectAdd);
6660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
6675ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed set quota rule");
6680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
6690dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
6700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
6720dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6730dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    } else {
6748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
6750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
6765ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", iface);
6770dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
6780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
6798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        it->quota = maxBytes;
6800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
6810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return 0;
6820dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    fail:
6840dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /*
6850dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
6860dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * rules in the kernel to see which ones need cleaning up.
6870dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
6880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * which resets everything.
6890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     */
6900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    removeInterfaceSharedQuota(ifn);
6910dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return -1;
6920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
6930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
6948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
6958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return getInterfaceQuota("shared", bytes);
6968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
6978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
6988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
6998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
7008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
7018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int scanRes;
7028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", costName);
7048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "r");
7058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
7068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
7075ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
7088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    scanRes = fscanf(fp, "%lld", bytes);
7113fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes);
7128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
7138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return scanRes == 1 ? 0 : -1;
7148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) {
7170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
7190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
72026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
72126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
72226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
7230dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
7255ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
72626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
72726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
72826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
72926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
7300dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7310dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
7328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
7330dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
7340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
7375ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
7380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        return -1;
7390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7400dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7410dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* This also removes the quota command of CostlyIface chain. */
74226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaUnique);
7430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    quotaIfaces.erase(it);
7450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
7470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
7488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
7508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
7518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
7528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
7548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "w");
7558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
7568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
7575ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
7588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fprintf(fp, "%lld\n", bytes);
7618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
7628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return 0;
7638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
7668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
7678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
768876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    const char *ifaceLimiting;
7698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
7708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    switch (op) {
7728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
7738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
7748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
7768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
7778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
7798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
7808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
7818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
7828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
784876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    ifaceLimiting = "! -i lo+";
7850031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_INPUT",
786c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
7878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
7888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
789876666947664c718a8d0cae9bbddb06cc91f912cJP Abgrall    ifaceLimiting = "! -o lo+";
7900031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_OUTPUT",
791c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
7928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
7938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
7948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
7958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
797c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
798c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
799c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *opFlag;
800c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *ifaceLimiting;
8018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
802c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
803c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    switch (op) {
804c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpInsert:
805c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-I";
806c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
807c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpReplace:
808c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-R";
809c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
810c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    default:
811c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpDelete:
812c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-D";
813c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
814c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
815c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
816c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    ifaceLimiting = "! -i lo+";
8170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, ifaceLimiting, opFlag, "bw_FORWARD",
818c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
819c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
820c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    free(alertQuotaCmd);
821c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
822c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
823c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
824c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) {
825c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
8268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
8278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
8295ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
8308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
8338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, bytes);
8348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
8358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
836c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        if (globalAlertTetherCount) {
8373fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
838c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall            res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
839c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        }
8408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = bytes;
8428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
845c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlertInForwardChain(void) {
846c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
847c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
848c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
849c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount++;
8503fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
851c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
852c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
853c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
854c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but this is not the 1st
855c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tether, we are also done.
856c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
857c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount != 1) {
858c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
859c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
860c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
861c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only add the rule if this was the 1st tether added. */
862c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
863c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
864c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
865c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
8668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) {
8678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
868c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
8698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
8708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!globalAlertBytes) {
8725ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
8738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
876c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (globalAlertTetherCount) {
877c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
878c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
8798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = 0;
8808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
883c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::removeGlobalAlertInForwardChain(void) {
884c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
885c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
886c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
887c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertTetherCount) {
8885ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
889c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return -1;
890c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
891c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
892c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount--;
893c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
894c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
895c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but there are more
896c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tethers, we are also done.
897c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
898c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount >= 1) {
899c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
900c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
901c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
902c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only detete the rule if this was the last tether removed. */
903c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
904c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
905c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
906c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
9078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) {
9088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!sharedQuotaBytes) {
9095ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior shared quota set to set an alert");
9108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
9135ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
9148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert("shared", bytes, &sharedAlertBytes);
9178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) {
9208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert("shared", &sharedAlertBytes);
9218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
9248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
9258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
9275ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
9288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
9318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
9328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
9338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
9365ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior interface quota set to set an alert");
9378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert(iface, bytes, &it->alert);
9418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) {
9448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
9458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
9478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
9488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
9498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
9525ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for interface %s", iface);
9538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert(iface, &it->alert);
9578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
9608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
9618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainNameAndPos;
9628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
9638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
9648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
9665ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
9678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
9708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (*alertBytes) {
9718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, *alertBytes);
9728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
9738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        asprintf(&chainNameAndPos, "costly_%s %d", costName, ALERT_RULE_POS_IN_COSTLY_CHAIN);
974c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-I", chainNameAndPos, bytes, alertName);
9758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
9768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(alertQuotaCmd);
9778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(chainNameAndPos);
9788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = bytes;
9808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
9818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
9828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
9858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
9868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainName;
9878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
9888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
9898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
9918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!*alertBytes) {
9925ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for %s alert", costName);
9938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&chainName, "costly_%s", costName);
997c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "", "-D", chainName, *alertBytes, alertName);
9988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptRejectNoAdd);
9998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
10008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(chainName);
10018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = 0;
10038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
10048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
10058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
1006db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1007db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/*
1008db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of:
10090031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall * Chain FORWARD (policy RETURN 0 packets, 0 bytes)
1010db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *     pkts      bytes target     prot opt in     out     source               destination
10110031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall *        0        0 RETURN     all  --  rmnet0 wlan0   0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
1012db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *        0        0 DROP       all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0            state INVALID
10130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall *        0        0 RETURN     all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0
1014db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall *
1015db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */
1016a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::parseForwardChainStats(TetherStats &stats, FILE *fp,
1017a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall                                                std::string &extraProcessingInfo) {
1018db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
1019db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1020db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface0[MAX_IPT_OUTPUT_LINE_LEN];
1021db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface1[MAX_IPT_OUTPUT_LINE_LEN];
1022db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char rest[MAX_IPT_OUTPUT_LINE_LEN];
1023db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1024db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *buffPtr;
1025db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int64_t packets, bytes;
1026db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1027db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1028db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        /* Clean up, so a failed parse can still print info */
1029db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
10300031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s",
1031db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall                &packets, &bytes, iface0, iface1, rest);
10323fb42e026ffebab2c8f282e42501040121e32d83Steve Block        ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res,
1033db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall             iface0, iface1, packets, bytes, rest, buffPtr);
1034a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall        extraProcessingInfo += buffPtr;
1035a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall
1036db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if (res != 5) {
1037db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            continue;
1038db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
1039db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if ((stats.ifaceIn == iface0) && (stats.ifaceOut == iface1)) {
10403fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1041db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxPackets = packets;
1042db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.rxBytes = bytes;
1043db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        } else if ((stats.ifaceOut == iface0) && (stats.ifaceIn == iface1)) {
10443fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("iface_in=%s iface_out=%s tx_bytes=%lld tx_packets=%lld ", iface1, iface0, bytes, packets);
1045db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txPackets = packets;
1046db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            stats.txBytes = bytes;
1047db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
1048db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1049db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Failure if rx or tx was not found */
1050db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return (stats.rxBytes == -1 || stats.txBytes == -1) ? -1 : 0;
1051db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1052db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1053db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1054db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) {
1055db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *msg;
1056db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    asprintf(&msg, "%s %s %lld %lld %lld %lld", ifaceIn.c_str(), ifaceOut.c_str(),
1057db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            rxBytes, rxPackets, txBytes, txPackets);
1058db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return msg;
1059db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1060db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1061a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrallint BandwidthController::getTetherStats(TetherStats &stats, std::string &extraProcessingInfo) {
1062db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
1063db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    std::string fullCmd;
1064db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    FILE *iptOutput;
1065db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    const char *cmd;
1066db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1067db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (stats.rxBytes != -1 || stats.txBytes != -1) {
10685ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Unexpected input stats. Byte counts should be -1.");
1069db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
1070db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1071db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1072db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /*
1073db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Why not use some kind of lib to talk to iptables?
1074db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Because the only libs are libiptc and libip6tc in iptables, and they are
1075db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * not easy to use. They require the known iptables match modules to be
1076db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * preloaded/linked, and require apparently a lot of wrapper code to get
1077db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * the wanted info.
1078db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     */
1079db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    fullCmd = IPTABLES_PATH;
10800031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    fullCmd += " -nvx -L natctrl_FORWARD";
1081db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    iptOutput = popen(fullCmd.c_str(), "r");
1082db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (!iptOutput) {
10835ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
1084a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall            extraProcessingInfo += "Failed to run iptables.";
1085db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
1086db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1087a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall    res = parseForwardChainStats(stats, iptOutput, extraProcessingInfo);
1088db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    pclose(iptOutput);
1089db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1090db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Currently NatController doesn't do ipv6 tethering, so we are done. */
1091db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return res;
1092db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1093