BandwidthController.cpp revision 7e51cde19af016456fff750f745db8132f3124a5
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
17e478873947f995e44e8c559342462c177a420ae0JP 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"
47baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall#include "NatController.h"  /* For LOCAL_TETHER_COUNTERS_CHAIN */
48baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall#include "ResponseCode.h"
494a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
50db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/* Alphabetical */
5192009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall#define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %lld --name %s"
52c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallconst char BandwidthController::ALERT_GLOBAL_NAME[] = "globalAlert";
538e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_INPUT = "bw_INPUT";
548e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_FORWARD = "bw_FORWARD";
558e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_OUTPUT = "bw_OUTPUT";
568e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_RAW_PREROUTING = "bw_raw_PREROUTING";
578e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyconst char* BandwidthController::LOCAL_MANGLE_POSTROUTING = "bw_mangle_POSTROUTING";
58db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_CMD_ARGS = 32;
59db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_CMD_LEN = 1024;
60db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_IFACENAME_LEN = 64;
61db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst int  BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256;
62db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
634a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall/**
644a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Some comments about the rules:
654a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *  * Ordering
664a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    - when an interface is marked as costly it should be INSERTED into the INPUT/OUTPUT chains.
6729e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *      E.g. "-I bw_INPUT -i rmnet0 --jump costly"
687e51cde19af016456fff750f745db8132f3124a5JP Abgrall *    - quota'd rules in the costly chain should be before bw_penalty_box lookups.
697e51cde19af016456fff750f745db8132f3124a5JP Abgrall *    - bw_happy_box rejects everything by default.
7029e8de22ad806bdcaa971ffeeb360c9f908aa346JP Abgrall *    - the qtaguid counting is done at the end of the bw_INPUT/bw_OUTPUT user chains.
714a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
724a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * * global quota vs per interface quota
734a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   - global quota for all costly interfaces uses a single costly chain:
744a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    . initial rules
757e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -N bw_costly_shared
767e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -I bw_INPUT -i iface0 --jump bw_costly_shared
777e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -I bw_OUTPUT -o iface0 --jump bw_costly_shared
787e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -I bw_costly_shared -m quota \! --quota 500000 \
79bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall *          --jump REJECT --reject-with icmp-net-prohibited
807e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -A bw_costly_shared --jump bw_penalty_box
81e478873947f995e44e8c559342462c177a420ae0JP Abgrall *      If the happy box is enabled,
827e51cde19af016456fff750f745db8132f3124a5JP Abgrall *        iptables -A bw_penalty_box --jump bw_happy_box
838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall *
844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *    . adding a new iface to this, E.g.:
857e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -I bw_INPUT -i iface1 --jump bw_costly_shared
867e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -I bw_OUTPUT -o iface1 --jump bw_costly_shared
874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
884a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *   - quota per interface. This is achieve by having "costly" chains per quota.
894a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *     E.g. adding a new costly interface iface0 with its own quota:
907e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -N bw_costly_iface0
917e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -I bw_INPUT -i iface0 --jump bw_costly_iface0
927e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -I bw_OUTPUT -o iface0 --jump bw_costly_iface0
937e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -A bw_costly_iface0 -m quota \! --quota 500000 \
94e478873947f995e44e8c559342462c177a420ae0JP Abgrall *          --jump REJECT --reject-with icmp-port-unreachable
957e51cde19af016456fff750f745db8132f3124a5JP Abgrall *      iptables -A bw_costly_iface0 --jump bw_penalty_box
964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall *
977e51cde19af016456fff750f745db8132f3124a5JP Abgrall * * bw_penalty_box handling:
987e51cde19af016456fff750f745db8132f3124a5JP Abgrall *  - only one bw_penalty_box for all interfaces
997e51cde19af016456fff750f745db8132f3124a5JP Abgrall *   E.g  Adding an app, it has to preserve the appened bw_happy_box, so "-I":
1007e51cde19af016456fff750f745db8132f3124a5JP Abgrall *    iptables -I bw_penalty_box -m owner --uid-owner app_3 \
101e478873947f995e44e8c559342462c177a420ae0JP Abgrall *        --jump REJECT --reject-with icmp-port-unreachable
102e478873947f995e44e8c559342462c177a420ae0JP Abgrall *
1037e51cde19af016456fff750f745db8132f3124a5JP Abgrall * * bw_happy_box handling:
1047e51cde19af016456fff750f745db8132f3124a5JP Abgrall *  - The bw_happy_box goes at the end of the penalty box.
105e478873947f995e44e8c559342462c177a420ae0JP Abgrall *   E.g  Adding a happy app,
1067e51cde19af016456fff750f745db8132f3124a5JP Abgrall *    iptables -I bw_happy_box -m owner --uid-owner app_3 \
107e478873947f995e44e8c559342462c177a420ae0JP Abgrall *        --jump RETURN
1084a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */
1090031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_FLUSH_COMMANDS[] = {
1100031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /*
1110031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * Cleanup rules.
1127e51cde19af016456fff750f745db8132f3124a5JP Abgrall     * Should normally include bw_costly_<iface>, but we rely on the way they are setup
1130031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * to allow coexistance.
11439f8f24246a5dac21be5cc5e32c0f395ee803766JP Abgrall     */
1150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F bw_INPUT",
1160031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F bw_OUTPUT",
1170031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-F bw_FORWARD",
1187e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-F bw_happy_box",
1197e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-F bw_penalty_box",
1207e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-F bw_costly_shared",
121f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall
122e478873947f995e44e8c559342462c177a420ae0JP Abgrall    /* Just a couple that are the most common. */
1237e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-F bw_costly_rmnet0",
1247e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-F bw_costly_wlan0",
125e478873947f995e44e8c559342462c177a420ae0JP Abgrall
126f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t raw -F bw_raw_PREROUTING",
127f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall    "-t mangle -F bw_mangle_POSTROUTING",
1280031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall};
1290031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
1300031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall/* The cleanup commands assume flushing has been done. */
1310031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char *BandwidthController::IPT_CLEANUP_COMMANDS[] = {
1327e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-X bw_happy_box",
1337e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-X bw_penalty_box",
1347e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-X bw_costly_shared",
135e478873947f995e44e8c559342462c177a420ae0JP Abgrall
136e478873947f995e44e8c559342462c177a420ae0JP Abgrall    /* Just a couple that are the most common. */
1377e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-X bw_costly_rmnet0",
1387e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-X bw_costly_wlan0",
1390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1404a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
141db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_SETUP_COMMANDS[] = {
1427e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-N bw_happy_box",
1437e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-N bw_penalty_box",
1447e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-N bw_costly_shared",
1450dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
147db7da58e8d2aa021060098057f944ef754be06e3JP Abgrallconst char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = {
1480031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-A bw_INPUT -m owner --socket-exists", /* This is a tracking rule. */
1490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
1500031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    "-A bw_OUTPUT -m owner --socket-exists", /* This is a tracking rule. */
1510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
1527e51cde19af016456fff750f745db8132f3124a5JP Abgrall    "-A bw_costly_shared --jump bw_penalty_box",
153f66d6e9db6be3e94b80c59fab99e237d2e2968c5JP Abgrall
15492009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    "-t raw -A bw_raw_PREROUTING -m owner --socket-exists", /* This is a tracking rule. */
15592009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    "-t mangle -A bw_mangle_POSTROUTING -m owner --socket-exists", /* This is a tracking rule. */
1560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall};
1574a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1584a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP AbgrallBandwidthController::BandwidthController(void) {
1594a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
1604a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
161a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrallint BandwidthController::runIpxtablesCmd(const char *cmd, IptJumpOp jumpHandling,
162ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall                                         IptFailureLog failureHandling) {
1630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
1648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
1653fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("runIpxtablesCmd(cmd=%s)", cmd);
166a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    res |= runIptablesCmd(cmd, jumpHandling, IptIpV4, failureHandling);
167a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    res |= runIptablesCmd(cmd, jumpHandling, IptIpV6, failureHandling);
1680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
1690dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
1700dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
17126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::StrncpyAndCheck(char *buffer, const char *src, size_t buffSize) {
17226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
17326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    memset(buffer, '\0', buffSize);  // strncpy() is not filling leftover with '\0'
17426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    strncpy(buffer, src, buffSize);
17526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return buffer[buffSize - 1];
17626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall}
17726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
178a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrallint BandwidthController::runIptablesCmd(const char *cmd, IptJumpOp jumpHandling,
179ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall                                        IptIpVer iptVer, IptFailureLog failureHandling) {
18026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    char buffer[MAX_CMD_LEN];
1814a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    const char *argv[MAX_CMD_ARGS];
18226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    int argc = 0;
1834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char *next = buffer;
1844a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char *tmp;
18511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    int res;
18614150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    int status = 0;
1874a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1880dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string fullCmd = cmd;
18926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
190a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    switch (jumpHandling) {
191a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    case IptJumpReject:
192340d5ccf04d4d441d8dd1788a7925d0313038b7cJP Abgrall        /*
193340d5ccf04d4d441d8dd1788a7925d0313038b7cJP Abgrall         * Must be carefull what one rejects with, as uper layer protocols will just
194340d5ccf04d4d441d8dd1788a7925d0313038b7cJP Abgrall         * keep on hammering the device until the number of retries are done.
195340d5ccf04d4d441d8dd1788a7925d0313038b7cJP Abgrall         * For port-unreachable (default), TCP should consider as an abort (RFC1122).
196340d5ccf04d4d441d8dd1788a7925d0313038b7cJP Abgrall         */
197340d5ccf04d4d441d8dd1788a7925d0313038b7cJP Abgrall        fullCmd += " --jump REJECT";
198a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        break;
199a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    case IptJumpReturn:
200a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        fullCmd += " --jump RETURN";
201a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        break;
202a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    case IptJumpNoAdd:
203a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        break;
2040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
2050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
20611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    fullCmd.insert(0, " ");
20711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
2084a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
20914150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
21014150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand        ALOGE("iptables command too long");
21114150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand        return -1;
21214150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    }
21314150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand
21414150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    argc = 0;
21514150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    while ((tmp = strsep(&next, " "))) {
21614150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand        argv[argc++] = tmp;
21714150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand        if (argc >= MAX_CMD_ARGS) {
21814150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand            ALOGE("iptables argument overflow");
2194a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            return -1;
2204a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall        }
22114150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    }
222fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
22314150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    argv[argc] = NULL;
22414150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand    res = android_fork_execvp(argc, (char **)argv, &status, false,
22514150215fcd9060c25a25930e85057df5904f6f6Rom Lemarchand            failureHandling == IptFailShow);
226c8dc63b14f5bcef34a23061c17d3bbe66df51429JP Abgrall    res = res || !WIFEXITED(status) || WEXITSTATUS(status);
227c8dc63b14f5bcef34a23061c17d3bbe66df51429JP Abgrall    if (res && failureHandling == IptFailShow) {
228c8dc63b14f5bcef34a23061c17d3bbe66df51429JP Abgrall      ALOGE("runIptablesCmd(): res=%d status=%d failed %s", res, status,
229c8dc63b14f5bcef34a23061c17d3bbe66df51429JP Abgrall            fullCmd.c_str());
23011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    }
23111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    return res;
2324a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2340031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::setupIptablesHooks(void) {
2350031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2360031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /* Some of the initialCommands are allowed to fail */
2370031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2380031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
2390031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2400031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_CLEANUP_COMMANDS) / sizeof(char*),
2410031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_CLEANUP_COMMANDS, RunCmdFailureOk);
2420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2430031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_SETUP_COMMANDS) / sizeof(char*),
2440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_SETUP_COMMANDS, RunCmdFailureBad);
2450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2460031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    return 0;
2470031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall}
2480031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2490031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallint BandwidthController::enableBandwidthControl(bool force) {
250fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res;
2510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    char value[PROPERTY_VALUE_MAX];
2520031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
2530031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    if (!force) {
2540031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            property_get("persist.bandwidth.enable", value, "1");
2550031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            if (!strcmp(value, "0"))
2560031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall                    return 0;
2570031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    }
2588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
259db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Let's pretend we started from scratch ... */
2608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    sharedQuotaIfaces.clear();
2618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    quotaIfaces.clear();
2628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    naughtyAppUids.clear();
263e478873947f995e44e8c559342462c177a420ae0JP Abgrall    niceAppUids.clear();
264db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    globalAlertBytes = 0;
265c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount = 0;
266db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    sharedQuotaBytes = sharedAlertBytes = 0;
267db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
2680031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    res = runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2690031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
270db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
2710031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    res |= runCommands(sizeof(IPT_BASIC_ACCOUNTING_COMMANDS) / sizeof(char*),
272db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            IPT_BASIC_ACCOUNTING_COMMANDS, RunCmdFailureBad);
2738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
274fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
2754a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2764a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2774a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2784a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallint BandwidthController::disableBandwidthControl(void) {
2790031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    runCommands(sizeof(IPT_FLUSH_COMMANDS) / sizeof(char*),
2800031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall            IPT_FLUSH_COMMANDS, RunCmdFailureOk);
281fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
2824a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
2834a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
2848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runCommands(int numCommands, const char *commands[],
2858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall                                     RunCmdErrHandling cmdErrHandling) {
286fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
287ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    IptFailureLog failureLogging = IptFailShow;
288ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    if (cmdErrHandling == RunCmdFailureOk) {
289ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall        failureLogging = IptFailHide;
290ad729ac11e080b1f1bdd9cd32807d9ae74ce2a63JP Abgrall    }
2913fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("runCommands(): %d commands", numCommands);
292fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (int cmdNum = 0; cmdNum < numCommands; cmdNum++) {
293a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        res = runIpxtablesCmd(commands[cmdNum], IptJumpNoAdd, failureLogging);
2940031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        if (res && cmdErrHandling != RunCmdFailureOk)
295fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            return res;
296fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
2970031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    return 0;
298fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
299fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
300a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrallstd::string BandwidthController::makeIptablesSpecialAppCmd(IptOp op, int uid, const char *chain) {
301fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
3028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
3038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
304fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
305fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
3068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
3078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
3088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
309109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall    case IptOpAppend:
310a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        ALOGE("Append op not supported for %s uids", chain);
311a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        res = "";
312a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        return res;
313109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall        break;
3148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
3158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
3168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
3178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
3188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
3198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
3208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
321fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
322a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    asprintf(&buff, "%s %s -m owner --uid-owner %d", opFlag, chain, uid);
3238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
3248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
325fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
326fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
327fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
328e478873947f995e44e8c559342462c177a420ae0JP Abgrallint BandwidthController::enableHappyBox(void) {
329e478873947f995e44e8c559342462c177a420ae0JP Abgrall    char cmd[MAX_CMD_LEN];
330e478873947f995e44e8c559342462c177a420ae0JP Abgrall    int res = 0;
331e478873947f995e44e8c559342462c177a420ae0JP Abgrall
332e478873947f995e44e8c559342462c177a420ae0JP Abgrall    /*
333e478873947f995e44e8c559342462c177a420ae0JP Abgrall     * We tentatively delete before adding, which helps recovering
334e478873947f995e44e8c559342462c177a420ae0JP Abgrall     * from bad states (e.g. netd died).
335e478873947f995e44e8c559342462c177a420ae0JP Abgrall     */
336e478873947f995e44e8c559342462c177a420ae0JP Abgrall
337e478873947f995e44e8c559342462c177a420ae0JP Abgrall    /* Should not exist, but ignore result if already there. */
3387e51cde19af016456fff750f745db8132f3124a5JP Abgrall    snprintf(cmd, sizeof(cmd), "-N bw_happy_box");
339e478873947f995e44e8c559342462c177a420ae0JP Abgrall    runIpxtablesCmd(cmd, IptJumpNoAdd);
340e478873947f995e44e8c559342462c177a420ae0JP Abgrall
341e478873947f995e44e8c559342462c177a420ae0JP Abgrall    /* Should be empty, but clear in case something was wrong. */
342e478873947f995e44e8c559342462c177a420ae0JP Abgrall    niceAppUids.clear();
3437e51cde19af016456fff750f745db8132f3124a5JP Abgrall    snprintf(cmd, sizeof(cmd), "-F bw_happy_box");
344e478873947f995e44e8c559342462c177a420ae0JP Abgrall    res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
345e478873947f995e44e8c559342462c177a420ae0JP Abgrall
3467e51cde19af016456fff750f745db8132f3124a5JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_penalty_box -j bw_happy_box");
347e478873947f995e44e8c559342462c177a420ae0JP Abgrall    runIpxtablesCmd(cmd, IptJumpNoAdd);
3487e51cde19af016456fff750f745db8132f3124a5JP Abgrall    snprintf(cmd, sizeof(cmd), "-A bw_penalty_box -j bw_happy_box");
349e478873947f995e44e8c559342462c177a420ae0JP Abgrall    res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
350e478873947f995e44e8c559342462c177a420ae0JP Abgrall
351e478873947f995e44e8c559342462c177a420ae0JP Abgrall    /* Reject. Defaulting to prot-unreachable */
3527e51cde19af016456fff750f745db8132f3124a5JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_happy_box -j REJECT");
353e478873947f995e44e8c559342462c177a420ae0JP Abgrall    runIpxtablesCmd(cmd, IptJumpNoAdd);
3547e51cde19af016456fff750f745db8132f3124a5JP Abgrall    snprintf(cmd, sizeof(cmd), "-A bw_happy_box -j REJECT");
355e478873947f995e44e8c559342462c177a420ae0JP Abgrall    res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
356e478873947f995e44e8c559342462c177a420ae0JP Abgrall
357e478873947f995e44e8c559342462c177a420ae0JP Abgrall    return res;
358e478873947f995e44e8c559342462c177a420ae0JP Abgrall}
359e478873947f995e44e8c559342462c177a420ae0JP Abgrall
360e478873947f995e44e8c559342462c177a420ae0JP Abgrallint BandwidthController::disableHappyBox(void) {
361e478873947f995e44e8c559342462c177a420ae0JP Abgrall    char cmd[MAX_CMD_LEN];
362e478873947f995e44e8c559342462c177a420ae0JP Abgrall
363e478873947f995e44e8c559342462c177a420ae0JP Abgrall    /* Best effort */
3647e51cde19af016456fff750f745db8132f3124a5JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_penalty_box -j bw_happy_box");
365e478873947f995e44e8c559342462c177a420ae0JP Abgrall    runIpxtablesCmd(cmd, IptJumpNoAdd);
366e478873947f995e44e8c559342462c177a420ae0JP Abgrall    niceAppUids.clear();
3677e51cde19af016456fff750f745db8132f3124a5JP Abgrall    snprintf(cmd, sizeof(cmd), "-F bw_happy_box");
368e478873947f995e44e8c559342462c177a420ae0JP Abgrall    runIpxtablesCmd(cmd, IptJumpNoAdd);
3697e51cde19af016456fff750f745db8132f3124a5JP Abgrall    snprintf(cmd, sizeof(cmd), "-X bw_happy_box");
370e478873947f995e44e8c559342462c177a420ae0JP Abgrall    runIpxtablesCmd(cmd, IptJumpNoAdd);
371e478873947f995e44e8c559342462c177a420ae0JP Abgrall
372e478873947f995e44e8c559342462c177a420ae0JP Abgrall    return 0;
373e478873947f995e44e8c559342462c177a420ae0JP Abgrall}
374e478873947f995e44e8c559342462c177a420ae0JP Abgrall
375fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::addNaughtyApps(int numUids, char *appUids[]) {
376a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    return manipulateNaughtyApps(numUids, appUids, SpecialAppOpAdd);
377fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
378fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
379fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeNaughtyApps(int numUids, char *appUids[]) {
380a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    return manipulateNaughtyApps(numUids, appUids, SpecialAppOpRemove);
381fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
382fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
383e478873947f995e44e8c559342462c177a420ae0JP Abgrallint BandwidthController::addNiceApps(int numUids, char *appUids[]) {
384e478873947f995e44e8c559342462c177a420ae0JP Abgrall    return manipulateNiceApps(numUids, appUids, SpecialAppOpAdd);
385e478873947f995e44e8c559342462c177a420ae0JP Abgrall}
386e478873947f995e44e8c559342462c177a420ae0JP Abgrall
387e478873947f995e44e8c559342462c177a420ae0JP Abgrallint BandwidthController::removeNiceApps(int numUids, char *appUids[]) {
388e478873947f995e44e8c559342462c177a420ae0JP Abgrall    return manipulateNiceApps(numUids, appUids, SpecialAppOpRemove);
389e478873947f995e44e8c559342462c177a420ae0JP Abgrall}
390e478873947f995e44e8c559342462c177a420ae0JP Abgrall
391a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrallint BandwidthController::manipulateNaughtyApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
3927e51cde19af016456fff750f745db8132f3124a5JP Abgrall    return manipulateSpecialApps(numUids, appStrUids, "bw_penalty_box", naughtyAppUids, IptJumpReject, appOp);
393a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall}
394a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall
395e478873947f995e44e8c559342462c177a420ae0JP Abgrallint BandwidthController::manipulateNiceApps(int numUids, char *appStrUids[], SpecialAppOp appOp) {
3967e51cde19af016456fff750f745db8132f3124a5JP Abgrall    return manipulateSpecialApps(numUids, appStrUids, "bw_happy_box", niceAppUids, IptJumpReturn, appOp);
397e478873947f995e44e8c559342462c177a420ae0JP Abgrall}
398e478873947f995e44e8c559342462c177a420ae0JP Abgrall
399a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall
400a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrallint BandwidthController::manipulateSpecialApps(int numUids, char *appStrUids[],
401a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall                                               const char *chain,
402a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall                                               std::list<int /*appUid*/> &specialAppUids,
403a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall                                               IptJumpOp jumpHandling, SpecialAppOp appOp) {
404a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall
405fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    char cmd[MAX_CMD_LEN];
406fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int uidNum;
40726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *failLogTemplate;
40826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    IptOp op;
409fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int appUids[numUids];
410a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    std::string iptCmd;
411b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall    std::list<int /*uid*/>::iterator it;
4128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
41326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (appOp) {
414a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    case SpecialAppOpAdd:
4158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpInsert;
416af476f7b659beff8315a83f094ce697c5179dae6JP Abgrall        failLogTemplate = "Failed to add app uid %s(%d) to %s.";
4178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
418a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    case SpecialAppOpRemove:
4198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        op = IptOpDelete;
420af476f7b659beff8315a83f094ce697c5179dae6JP Abgrall        failLogTemplate = "Failed to delete app uid %s(%d) from %s box.";
4218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
4220031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
4230031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected app Op %d", appOp);
4240031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
42526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
42626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
427fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
428af476f7b659beff8315a83f094ce697c5179dae6JP Abgrall        char *end;
429af476f7b659beff8315a83f094ce697c5179dae6JP Abgrall        appUids[uidNum] = strtoul(appStrUids[uidNum], &end, 0);
430af476f7b659beff8315a83f094ce697c5179dae6JP Abgrall        if (*end || !*appStrUids[uidNum]) {
431af476f7b659beff8315a83f094ce697c5179dae6JP Abgrall            ALOGE(failLogTemplate, appStrUids[uidNum], appUids[uidNum], chain);
432fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_parse;
433fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
434fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
435fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
436fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    for (uidNum = 0; uidNum < numUids; uidNum++) {
437b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        int uid = appUids[uidNum];
438a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        for (it = specialAppUids.begin(); it != specialAppUids.end(); it++) {
439b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (*it == uid)
440b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                break;
441b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        }
442a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        bool found = (it != specialAppUids.end());
443b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall
444a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        if (appOp == SpecialAppOpRemove) {
445b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (!found) {
446b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                ALOGE("No such appUid %d to remove", uid);
447b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                return -1;
448b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            }
449a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall            specialAppUids.erase(it);
450b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        } else {
451b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            if (found) {
452b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                ALOGE("appUid %d exists already", uid);
453b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall                return -1;
454b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall            }
455a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall            specialAppUids.push_front(uid);
456b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall        }
457b1d24094c2c5d48bbb3dfad4a0551ff0bf77ce6cJP Abgrall
458a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        iptCmd = makeIptablesSpecialAppCmd(op, uid, chain);
459a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        if (runIpxtablesCmd(iptCmd.c_str(), jumpHandling)) {
460af476f7b659beff8315a83f094ce697c5179dae6JP Abgrall            ALOGE(failLogTemplate, appStrUids[uidNum], uid, chain);
461fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail_with_uidNum;
462fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
463fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
464fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return 0;
465fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
46626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_with_uidNum:
467fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    /* Try to remove the uid that failed in any case*/
468a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    iptCmd = makeIptablesSpecialAppCmd(IptOpDelete, appUids[uidNum], chain);
469a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    runIpxtablesCmd(iptCmd.c_str(), jumpHandling);
47026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallfail_parse:
47126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    return -1;
4724a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
4734a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
47426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallstd::string BandwidthController::makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota) {
475fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string res;
4768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *buff;
4778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
4780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
4793fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("makeIptablesQuotaCmd(%d, %lld)", op, quota);
4800dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
481fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    switch (op) {
4828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
4838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
4848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
485109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall    case IptOpAppend:
486109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall        opFlag = "-A";
487109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall        break;
4888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
4898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
4908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
4918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
4928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
4938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
4948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
495fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
4968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
497bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    // The requried IP version specific --jump REJECT ... will be added later.
4987e51cde19af016456fff750f745db8132f3124a5JP Abgrall    asprintf(&buff, "%s bw_costly_%s -m quota2 ! --quota %lld --name %s", opFlag, costName, quota,
4998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall             costName);
5008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = buff;
5018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(buff);
5020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
5030dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
5040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
50526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::prepCostlyIface(const char *ifn, QuotaType quotaType) {
5060dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
5070031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    int res = 0, res1, res2;
5088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int ruleInsertPos = 1;
5090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
5100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
5110dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* The "-N costly" is created upfront, no need to handle it here. */
51326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
51426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
5157e51cde19af016456fff750f745db8132f3124a5JP Abgrall        costString = "bw_costly_";
5160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
5170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
5180031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        /*
5197e51cde19af016456fff750f745db8132f3124a5JP Abgrall         * Flush the bw_costly_<iface> is allowed to fail in case it didn't exist.
5200031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * Creating a new one is allowed to fail in case it existed.
5210031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         * This helps with netd restarts.
5220031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall         */
5230031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
524a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        res1 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
5250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-N %s", costCString);
526a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        res2 = runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
5270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        res = (res1 && res2) || (!res1 && !res2);
5280031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
5297e51cde19af016456fff750f745db8132f3124a5JP Abgrall        snprintf(cmd, sizeof(cmd), "-A %s -j bw_penalty_box", costCString);
530a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
53126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
53226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
5337e51cde19af016456fff750f745db8132f3124a5JP Abgrall        costCString = "bw_costly_shared";
53426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
5350031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
5360031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected quotatype %d", quotaType);
5370031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
5380dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
5390dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
5418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* The alert rule comes 1st */
5428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        ruleInsertPos = 2;
5438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
5440031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
5450031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
546a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
5470031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
5480031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-I bw_INPUT %d -i %s --jump %s", ruleInsertPos, ifn, costCString);
549a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
5500031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
5510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
552a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    runIpxtablesCmd(cmd, IptJumpNoAdd, IptFailHide);
5530031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
5540031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-I bw_OUTPUT %d -o %s --jump %s", ruleInsertPos, ifn, costCString);
555a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
5560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
5570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
5580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
55926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrallint BandwidthController::cleanupCostlyIface(const char *ifn, QuotaType quotaType) {
5600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char cmd[MAX_CMD_LEN];
5610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
5620dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string costString;
5630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    const char *costCString;
5640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
56526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    switch (quotaType) {
56626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaUnique:
5677e51cde19af016456fff750f745db8132f3124a5JP Abgrall        costString = "bw_costly_";
5680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costString += ifn;
5690dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        costCString = costString.c_str();
57026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
57126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    case QuotaShared:
5727e51cde19af016456fff750f745db8132f3124a5JP Abgrall        costCString = "bw_costly_shared";
57326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        break;
5740031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    default:
5750031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        ALOGE("Unexpected quotatype %d", quotaType);
5760031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        return -1;
5770dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
5780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5790031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_INPUT -i %s --jump %s", ifn, costCString);
580a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
5810031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    snprintf(cmd, sizeof(cmd), "-D bw_OUTPUT -o %s --jump %s", ifn, costCString);
582a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
5830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
5847e51cde19af016456fff750f745db8132f3124a5JP Abgrall    /* The "-N bw_costly_shared" is created upfront, no need to handle it here. */
58526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (quotaType == QuotaUnique) {
5860dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        snprintf(cmd, sizeof(cmd), "-F %s", costCString);
587a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
588a9f802c23f4c2c53fa1065b75f712ce46f384c3aJP Abgrall        snprintf(cmd, sizeof(cmd), "-X %s", costCString);
589a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        res |= runIpxtablesCmd(cmd, IptJumpNoAdd);
590fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
591fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    return res;
592fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall}
5934a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
5940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceSharedQuota(const char *iface, int64_t maxBytes) {
5954a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char cmd[MAX_CMD_LEN];
5964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
597fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
59826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
5998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::string ifaceName;
6008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ;
601bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
60226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<std::string>::iterator it;
6034a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
6048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
6058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
6065ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
6078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
6088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
60926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
6105ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
61126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
61226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
61326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
6144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
6154a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    if (maxBytes == -1) {
616fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return removeInterfaceSharedQuota(ifn);
6174a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
6184a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
6194a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /* Insert ingress quota. */
6200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
6210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
622fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
6234a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
624fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
6250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
62626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaShared);
6270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (sharedQuotaIfaces.empty()) {
6280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            quotaCmd = makeIptablesQuotaCmd(IptOpInsert, costName, maxBytes);
629a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall            res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
6304a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            if (res) {
6315ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block                ALOGE("Failed set quota rule");
632fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall                goto fail;
6334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall            }
634fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            sharedQuotaBytes = maxBytes;
635fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
6360dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        sharedQuotaIfaces.push_front(ifaceName);
637fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
638fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
639fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
640fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    if (maxBytes != sharedQuotaBytes) {
6418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
642fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        if (res) {
6435ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", costName);
644fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            goto fail;
645fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        }
646fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        sharedQuotaBytes = maxBytes;
6474a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
6484a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return 0;
649fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
650fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    fail:
6514a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    /*
6524a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
6534a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     * rules in the kernel to see which ones need cleaning up.
654fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
655fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall     * which resets everything.
6564a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall     */
657fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    removeInterfaceSharedQuota(ifn);
6584a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return -1;
6594a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
6604a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
6618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall/* It will also cleanup any shared alerts */
662fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrallint BandwidthController::removeInterfaceSharedQuota(const char *iface) {
6634a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    char ifn[MAX_IFACENAME_LEN];
664fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int res = 0;
66526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
6660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::list<std::string>::iterator it;
667bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall    const char *costName = "shared";
6684a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
6698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
6705ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
67126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
67226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
6738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    ifaceName = ifn;
67426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
6750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = sharedQuotaIfaces.begin(); it != sharedQuotaIfaces.end(); it++) {
6760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (*it == ifaceName)
677fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            break;
6784a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
6790dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == sharedQuotaIfaces.end()) {
6805ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
681fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        return -1;
6824a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    }
683fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
68426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaShared);
6850dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    sharedQuotaIfaces.erase(it);
686fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
6870dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (sharedQuotaIfaces.empty()) {
688fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall        std::string quotaCmd;
689bfa7466b328101a8b04807f26e85c84526c9a774JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpDelete, costName, sharedQuotaBytes);
690a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
6918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        sharedQuotaBytes = 0;
6928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (sharedAlertBytes) {
6938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            removeSharedAlert();
6948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            sharedAlertBytes = 0;
6958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        }
696fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    }
6974a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    return res;
6984a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}
6990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::setInterfaceQuota(const char *iface, int64_t maxBytes) {
7010dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
7020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
70326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
70426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
70526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
70626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string quotaCmd;
7070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!maxBytes) {
7098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        /* Don't talk about -1, deprecate it. */
7105ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
7118a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (maxBytes == -1) {
71426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return removeInterfaceQuota(iface);
7150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7160dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
7185ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
71926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
72026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
72126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
72226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
72326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
7240dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Insert ingress quota. */
7250dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
7268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
7270dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
7280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7290dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7300dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
731e478873947f995e44e8c559342462c177a420ae0JP Abgrall        /* Preparing the iface adds a penalty/happy box check */
73226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        res |= prepCostlyIface(ifn, QuotaUnique);
733baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall        /*
734e478873947f995e44e8c559342462c177a420ae0JP Abgrall         * The rejecting quota limit should go after the penalty/happy box checks
735baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall         * or else a naughty app could just eat up the quota.
736baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall         * So we append here.
737baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall         */
738109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall        quotaCmd = makeIptablesQuotaCmd(IptOpAppend, costName, maxBytes);
739a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        res |= runIpxtablesCmd(quotaCmd.c_str(), IptJumpReject);
7400dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
7415ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed set quota rule");
7420dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
7430dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
7440dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        quotaIfaces.push_front(QuotaInfo(ifaceName, maxBytes, 0));
7460dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    } else {
7488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res |= updateQuota(costName, maxBytes);
7490dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        if (res) {
7505ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed update quota for %s", iface);
7510dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            goto fail;
7520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        }
7538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        it->quota = maxBytes;
7540dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
7550dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return 0;
7560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7570dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    fail:
7580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /*
7590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * TODO(jpa): once we get rid of iptables in favor of rtnetlink, reparse
7600dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * rules in the kernel to see which ones need cleaning up.
7610dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * For now callers needs to choose if they want to "ndc bandwidth enable"
7620dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     * which resets everything.
7630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall     */
7640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    removeInterfaceSharedQuota(ifn);
7650dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return -1;
7660dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
7670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceSharedQuota(int64_t *bytes) {
7698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return getInterfaceQuota("shared", bytes);
7708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7718a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7728a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::getInterfaceQuota(const char *costName, int64_t *bytes) {
7738a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
7748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
7758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int scanRes;
7768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", costName);
7788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "r");
7798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
7808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
7815ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Reading quota %s failed (%s)", costName, strerror(errno));
7828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
7838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
7848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    scanRes = fscanf(fp, "%lld", bytes);
7853fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("Read quota res=%d bytes=%lld", scanRes, *bytes);
7868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
7878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return scanRes == 1 ? 0 : -1;
7888a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
7898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
7900dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrallint BandwidthController::removeInterfaceQuota(const char *iface) {
7910dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7920dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    char ifn[MAX_IFACENAME_LEN];
7930dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int res = 0;
79426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string ifaceName;
79526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    const char *costName;
79626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::list<QuotaInfo>::iterator it;
7970dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
7988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (StrncpyAndCheck(ifn, iface, sizeof(ifn))) {
7995ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Interface name longer than %d", MAX_IFACENAME_LEN);
80026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall        return -1;
80126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    }
80226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    ifaceName = ifn;
80326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    costName = iface;
8040dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
8050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
8068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == ifaceName)
8070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall            break;
8080dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
8090dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
8100dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    if (it == quotaIfaces.end()) {
8115ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No such iface %s to delete", ifn);
8120dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall        return -1;
8130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    }
8140dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
8150dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* This also removes the quota command of CostlyIface chain. */
81626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    res |= cleanupCostlyIface(ifn, QuotaUnique);
8170dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
8180dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    quotaIfaces.erase(it);
8190dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
8200dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    return res;
8210dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall}
8228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::updateQuota(const char *quotaName, int64_t bytes) {
8248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    FILE *fp;
8258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *fname;
8268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8278a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&fname, "/proc/net/xt_quota/%s", quotaName);
8288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fp = fopen(fname, "w");
8298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(fname);
8308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!fp) {
8315ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Updating quota %s failed (%s)", quotaName, strerror(errno));
8328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
8338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fprintf(fp, "%lld\n", bytes);
8358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    fclose(fp);
8368a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return 0;
8378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes) {
8408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
8418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    const char *opFlag;
8428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
8438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
8448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    switch (op) {
8458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpInsert:
8468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-I";
8478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
848109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall    case IptOpAppend:
849109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall        opFlag = "-A";
850109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall        break;
8518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpReplace:
8528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-R";
8538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
8548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    default:
8558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    case IptOpDelete:
8568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        opFlag = "-D";
8578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        break;
8588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
8598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
86092009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_INPUT",
861c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
862a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
8638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
86492009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_OUTPUT",
865c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
866a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
8678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
8688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
8698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
8708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
871c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::runIptablesAlertFwdCmd(IptOp op, const char *alertName, int64_t bytes) {
872c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
873c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *opFlag;
8748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
875c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
876c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    switch (op) {
877c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpInsert:
878c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-I";
879c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
880109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall    case IptOpAppend:
881109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall        opFlag = "-A";
882109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall        break;
883c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpReplace:
884c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-R";
885c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
886c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    default:
887c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    case IptOpDelete:
888c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        opFlag = "-D";
889c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        break;
890c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
891c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
89292009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, opFlag, "bw_FORWARD",
893c2b26cb83d9bf3f91e986625efcc40fc8eb79a13Nick Kralevich        bytes, alertName);
894a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    res = runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
895c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    free(alertQuotaCmd);
896c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
897c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
898c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
899c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlert(int64_t bytes) {
900c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
9018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
9028a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
9045ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
9058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (globalAlertBytes) {
9088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, bytes);
9098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
9108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = runIptablesAlertCmd(IptOpInsert, alertName, bytes);
911c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        if (globalAlertTetherCount) {
9123fb42e026ffebab2c8f282e42501040121e32d83Steve Block            ALOGV("setGlobalAlert for %d tether", globalAlertTetherCount);
913c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall            res |= runIptablesAlertFwdCmd(IptOpInsert, alertName, bytes);
914c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        }
9158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = bytes;
9178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
9188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
920c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::setGlobalAlertInForwardChain(void) {
921c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
922c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
923c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
924c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount++;
9253fb42e026ffebab2c8f282e42501040121e32d83Steve Block    ALOGV("setGlobalAlertInForwardChain(): %d tether", globalAlertTetherCount);
926c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
927c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
928c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
929c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but this is not the 1st
930c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tether, we are also done.
931c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
932c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount != 1) {
933c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
934c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
935c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
936c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only add the rule if this was the 1st tether added. */
937c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpInsert, alertName, globalAlertBytes);
938c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
939c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
940c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
9418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeGlobalAlert(void) {
9428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
943c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
9448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
9458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!globalAlertBytes) {
9475ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
9488a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    res = runIptablesAlertCmd(IptOpDelete, alertName, globalAlertBytes);
951c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (globalAlertTetherCount) {
952c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        res |= runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
953c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
9548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    globalAlertBytes = 0;
9558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
9568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
958c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrallint BandwidthController::removeGlobalAlertInForwardChain(void) {
959c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    int res = 0;
960c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    const char *alertName = ALERT_GLOBAL_NAME;
961c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
962c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertTetherCount) {
9635ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set");
964c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return -1;
965c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
966c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
967c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    globalAlertTetherCount--;
968c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /*
969c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is no globalAlert active we are done.
970c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * If there is an active globalAlert but there are more
971c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     * tethers, we are also done.
972c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall     */
973c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    if (!globalAlertBytes || globalAlertTetherCount >= 1) {
974c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        return 0;
975c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    }
976c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
977c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    /* We only detete the rule if this was the last tether removed. */
978c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    res = runIptablesAlertFwdCmd(IptOpDelete, alertName, globalAlertBytes);
979c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall    return res;
980c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall}
981c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall
9828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setSharedAlert(int64_t bytes) {
9838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!sharedQuotaBytes) {
9845ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior shared quota set to set an alert");
9858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9878a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
9885ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
9898a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
9908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
9918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert("shared", bytes, &sharedAlertBytes);
9928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeSharedAlert(void) {
9958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert("shared", &sharedAlertBytes);
9968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
9978a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
9988a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setInterfaceAlert(const char *iface, int64_t bytes) {
9998a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
10008a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10018a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
10025ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
10038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
10048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
10058a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
10068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
10078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
10088a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
10098a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10108a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
10115ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Need to have a prior interface quota set to set an alert");
10128a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
10138a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
10148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return setCostlyAlert(iface, bytes, &it->alert);
10168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
10178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeInterfaceAlert(const char *iface) {
10198a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    std::list<QuotaInfo>::iterator it;
10208a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10218a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    for (it = quotaIfaces.begin(); it != quotaIfaces.end(); it++) {
10228a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        if (it->ifaceName == iface)
10238a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall            break;
10248a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
10258a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10268a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (it == quotaIfaces.end()) {
10275ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for interface %s", iface);
10288a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
10298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
10308a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10318a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return removeCostlyAlert(iface, &it->alert);
10328a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
10338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10348a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes) {
10358a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
1036109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall    char *chainName;
10378a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
10388a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
10398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!bytes) {
10415ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Invalid bytes value. 1..max_int64.");
10428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
10438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
10448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
10458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (*alertBytes) {
10468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        res = updateQuota(alertName, *alertBytes);
10478a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    } else {
10487e51cde19af016456fff750f745db8132f3124a5JP Abgrall        asprintf(&chainName, "bw_costly_%s", costName);
1049109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall        asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-A", chainName, bytes, alertName);
1050a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall        res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
10518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        free(alertQuotaCmd);
1052109899bc63139c5260cb9a7dc409f92efaf2c4b7JP Abgrall        free(chainName);
10538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
10548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = bytes;
10558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
10568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
10578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
10588a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10598a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrallint BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertBytes) {
10608a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertQuotaCmd;
10618a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *chainName;
10628a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    char *alertName;
10638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int res = 0;
10648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    asprintf(&alertName, "%sAlert", costName);
10668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    if (!*alertBytes) {
10675ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("No prior alert set for %s alert", costName);
10688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        return -1;
10698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    }
10708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10717e51cde19af016456fff750f745db8132f3124a5JP Abgrall    asprintf(&chainName, "bw_costly_%s", costName);
107292009c8effc75c2d70a6c9a1ac33ba2c60b78820JP Abgrall    asprintf(&alertQuotaCmd, ALERT_IPT_TEMPLATE, "-D", chainName, *alertBytes, alertName);
1073a9ba4cba3369e07aae05607f82424cc0075c9c34JP Abgrall    res |= runIpxtablesCmd(alertQuotaCmd, IptJumpNoAdd);
10748a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertQuotaCmd);
10758a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(chainName);
10768a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
10778a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    *alertBytes = 0;
10788a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    free(alertName);
10798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    return res;
10808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall}
1081db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1082db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall/*
1083db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Parse the ptks and bytes out of:
1084baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall *   Chain natctrl_tether_counters (4 references)
1085baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall *       pkts      bytes target     prot opt in     out     source               destination
1086baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall *         26     2373 RETURN     all  --  wlan0  rmnet0  0.0.0.0/0            0.0.0.0/0            counter wlan0_rmnet0: 0 bytes
1087baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall *         27     2002 RETURN     all  --  rmnet0 wlan0   0.0.0.0/0            0.0.0.0/0            counter rmnet0_wlan0: 0 bytes
1088baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall *       1040   107471 RETURN     all  --  bt-pan rmnet0  0.0.0.0/0            0.0.0.0/0            counter bt-pan_rmnet0: 0 bytes
1089baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall *       1450  1708806 RETURN     all  --  rmnet0 bt-pan  0.0.0.0/0            0.0.0.0/0            counter rmnet0_bt-pan: 0 bytes
1090db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */
1091baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrallint BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherStats filter,
1092baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                                                FILE *fp, std::string &extraProcessingInfo) {
1093db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
1094db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
1095db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface0[MAX_IPT_OUTPUT_LINE_LEN];
1096db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char iface1[MAX_IPT_OUTPUT_LINE_LEN];
1097db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char rest[MAX_IPT_OUTPUT_LINE_LEN];
1098db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1099baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    TetherStats stats;
1100db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *buffPtr;
1101db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int64_t packets, bytes;
1102baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    int statsFound = 0;
1103baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall
1104baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    bool filterPair = filter.intIface[0] && filter.extIface[0];
1105baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall
1106baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    char *filterMsg = filter.getStatsLine();
1107baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    ALOGV("filter: %s",  filterMsg);
1108baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    free(filterMsg);
1109baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall
1110baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    stats = filter;
1111db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1112db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
1113db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        /* Clean up, so a failed parse can still print info */
1114db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
11150031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        res = sscanf(buffPtr, "%lld %lld RETURN all -- %s %s 0.%s",
1116db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall                &packets, &bytes, iface0, iface1, rest);
11173fb42e026ffebab2c8f282e42501040121e32d83Steve Block        ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%lld bytes=%lld rest=<%s> orig line=<%s>", res,
1118db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall             iface0, iface1, packets, bytes, rest, buffPtr);
1119a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall        extraProcessingInfo += buffPtr;
1120a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall
1121db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        if (res != 5) {
1122db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            continue;
1123db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
1124baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall        /*
1125baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall         * The following assumes that the 1st rule has in:extIface out:intIface,
1126baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall         * which is what NatController sets up.
1127baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall         * If not filtering, the 1st match rx, and sets up the pair for the tx side.
1128baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall         */
1129baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall        if (filter.intIface[0] && filter.extIface[0]) {
1130baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            if (filter.intIface == iface0 && filter.extIface == iface1) {
1131baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                ALOGV("2Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1132baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.rxPackets = packets;
1133baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.rxBytes = bytes;
1134baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            } else if (filter.intIface == iface1 && filter.extIface == iface0) {
1135baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                ALOGV("2Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1136baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.txPackets = packets;
1137baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.txBytes = bytes;
1138baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            }
1139baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall        } else if (filter.intIface[0] || filter.extIface[0]) {
1140baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            if (filter.intIface == iface0 || filter.extIface == iface1) {
1141baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                ALOGV("1Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1142baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.intIface = iface0;
1143baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.extIface = iface1;
1144baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.rxPackets = packets;
1145baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.rxBytes = bytes;
1146baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            } else if (filter.intIface == iface1 || filter.extIface == iface0) {
1147baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                ALOGV("1Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1148baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.intIface = iface1;
1149baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.extIface = iface0;
1150baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.txPackets = packets;
1151baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.txBytes = bytes;
1152baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            }
1153baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall        } else /* if (!filter.intFace[0] && !filter.extIface[0]) */ {
1154baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            if (!stats.intIface[0]) {
1155baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                ALOGV("0Filter RX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1156baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.intIface = iface0;
1157baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.extIface = iface1;
1158baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.rxPackets = packets;
1159baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.rxBytes = bytes;
1160baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            } else if (stats.intIface == iface1 && stats.extIface == iface0) {
1161baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                ALOGV("0Filter TX iface_in=%s iface_out=%s rx_bytes=%lld rx_packets=%lld ", iface0, iface1, bytes, packets);
1162baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.txPackets = packets;
1163baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats.txBytes = bytes;
1164baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            }
1165baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall        }
1166baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall        if (stats.rxBytes != -1 && stats.txBytes != -1) {
1167baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            ALOGV("rx_bytes=%lld tx_bytes=%lld filterPair=%d", stats.rxBytes, stats.txBytes, filterPair);
1168baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            /* Send out stats, and prep for the next if needed. */
1169baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            char *msg = stats.getStatsLine();
1170baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            if (filterPair) {
1171baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                cli->sendMsg(ResponseCode::TetheringStatsResult, msg, false);
1172baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                return 0;
1173baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            } else {
1174baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                cli->sendMsg(ResponseCode::TetheringStatsListResult, msg, false);
1175baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall                stats = filter;
1176baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            }
1177baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            free(msg);
1178baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall            statsFound++;
1179db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        }
1180db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1181baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    /* We found some stats, and the last one isn't a partial stats. */
1182baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    if (statsFound && (stats.rxBytes == -1 || stats.txBytes == -1)) {
1183baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall        cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
1184baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall        return 0;
1185baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    }
1186baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    return -1;
1187db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1188db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1189baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrallchar *BandwidthController::TetherStats::getStatsLine(void) const {
1190db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    char *msg;
1191baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    asprintf(&msg, "%s %s %lld %lld %lld %lld", intIface.c_str(), extIface.c_str(),
1192db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall            rxBytes, rxPackets, txBytes, txPackets);
1193db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return msg;
1194db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1195db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1196baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrallint BandwidthController::getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo) {
1197db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    int res;
1198db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    std::string fullCmd;
1199db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    FILE *iptOutput;
1200db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    const char *cmd;
1201db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1202db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /*
1203db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Why not use some kind of lib to talk to iptables?
1204db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * Because the only libs are libiptc and libip6tc in iptables, and they are
1205db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * not easy to use. They require the known iptables match modules to be
1206db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * preloaded/linked, and require apparently a lot of wrapper code to get
1207db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     * the wanted info.
1208db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall     */
1209db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    fullCmd = IPTABLES_PATH;
1210baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    fullCmd += " -nvx -L ";
1211baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    fullCmd += NatController::LOCAL_TETHER_COUNTERS_CHAIN;
1212db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    iptOutput = popen(fullCmd.c_str(), "r");
1213db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    if (!iptOutput) {
12145ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block            ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
1215a2a64f004f1677daf16b0b03d589d6572ec547c2JP Abgrall            extraProcessingInfo += "Failed to run iptables.";
1216db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall        return -1;
1217db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    }
1218baeccc455b293c2c83dbe6463f56b741177bd612JP Abgrall    res = parseForwardChainStats(cli, stats, iptOutput, extraProcessingInfo);
1219db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    pclose(iptOutput);
1220db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
1221db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    /* Currently NatController doesn't do ipv6 tethering, so we are done. */
1222db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall    return res;
1223db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall}
1224