1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef _BANDWIDTH_CONTROLLER_H
17 #define _BANDWIDTH_CONTROLLER_H
18 
19 #include <map>
20 #include <set>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 #include <mutex>
25 
26 #include "NetdConstants.h"
27 
28 class BandwidthController {
29 public:
30     std::mutex lock;
31 
32     BandwidthController();
33 
34     int setupIptablesHooks();
35 
36     int enableBandwidthControl();
37     int enableDataSaver(bool enable);
38 
39     int setInterfaceSharedQuota(const std::string& iface, int64_t bytes);
40     int getInterfaceSharedQuota(int64_t *bytes);
41     int removeInterfaceSharedQuota(const std::string& iface);
42 
43     int setInterfaceQuota(const std::string& iface, int64_t bytes);
44     int getInterfaceQuota(const std::string& iface, int64_t* bytes);
45     int removeInterfaceQuota(const std::string& iface);
46 
47     int addNaughtyApps(const std::vector<uint32_t>& appUids);
48     int removeNaughtyApps(const std::vector<uint32_t>& appUids);
49     int addNiceApps(const std::vector<uint32_t>& appUids);
50     int removeNiceApps(const std::vector<uint32_t>& appUids);
51 
52     int setGlobalAlert(int64_t bytes);
53     int removeGlobalAlert();
54     int setGlobalAlertInForwardChain();
55     int removeGlobalAlertInForwardChain();
56 
57     int setInterfaceAlert(const std::string& iface, int64_t bytes);
58     int removeInterfaceAlert(const std::string& iface);
59 
60     static const char LOCAL_INPUT[];
61     static const char LOCAL_FORWARD[];
62     static const char LOCAL_OUTPUT[];
63     static const char LOCAL_RAW_PREROUTING[];
64     static const char LOCAL_MANGLE_POSTROUTING[];
65     static const char LOCAL_GLOBAL_ALERT[];
66 
67     enum IptJumpOp { IptJumpReject, IptJumpReturn };
68     enum IptOp { IptOpInsert, IptOpDelete };
69 
70   private:
71     struct QuotaInfo {
72         int64_t quota;
73         int64_t alert;
74     };
75 
76     enum IptIpVer { IptIpV4, IptIpV6 };
77     enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend };
78     enum QuotaType { QuotaUnique, QuotaShared };
79     enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
80 #if LOG_NDEBUG
81     enum IptFailureLog { IptFailShow, IptFailHide };
82 #else
83     enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow };
84 #endif
85 
86     std::string makeDataSaverCommand(IptablesTarget target, bool enable);
87 
88     int runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes);
89     int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes);
90 
91     int updateQuota(const std::string& alertName, int64_t bytes);
92 
93     int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes);
94     int removeCostlyAlert(const std::string& costName, int64_t* alertBytes);
95 
96     /*
97      * Attempt to find the bw_costly_* tables that need flushing,
98      * and flush them.
99      * If doClean then remove the tables also.
100      * Deals with both ip4 and ip6 tables.
101      */
102     void flushExistingCostlyTables(bool doClean);
103     static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove);
104 
105     /*
106      * Attempt to flush our tables.
107      * If doClean then remove them also.
108      * Deals with both ip4 and ip6 tables.
109      */
110     void flushCleanTables(bool doClean);
111 
112     // For testing.
113     friend class BandwidthControllerTest;
114     static int (*execFunction)(int, char **, int *, bool, bool);
115     static FILE *(*popenFunction)(const char *, const char *);
116     static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
117 
118     static const char *opToString(IptOp op);
119     static const char *jumpToString(IptJumpOp jumpHandling);
120 
121     int64_t mSharedQuotaBytes = 0;
122     int64_t mGlobalAlertBytes = 0;
123 
124     std::map<std::string, QuotaInfo> mQuotaIfaces;
125     std::set<std::string> mSharedQuotaIfaces;
126 };
127 
128 #endif
129