blob: b0da5bf048c4aa17cb5e5f18bd165478e9b78f41 [file] [log] [blame]
San Mehat9d10b342010-01-18 09:51:02 -08001/*
2 * Copyright (C) 2008 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
17#ifndef _TETHER_CONTROLLER_H
18#define _TETHER_CONTROLLER_H
19
Erik Kline70c03662016-03-31 11:39:53 +090020#include <list>
Lorenzo Colitti799625c2015-02-25 12:52:00 +090021#include <set>
22#include <string>
San Mehat9d10b342010-01-18 09:51:02 -080023
Lorenzo Colittia93126d2017-08-24 13:28:19 +090024#include <sysutils/SocketClient.h>
25
26#include "NetdConstants.h"
27
Lorenzo Colittie20a5262017-05-09 18:30:44 +090028namespace android {
29namespace net {
San Mehat9d10b342010-01-18 09:51:02 -080030
31class TetherController {
Erik Kline2c5aaa12016-06-08 13:24:45 +090032private:
33 std::list<std::string> mInterfaces;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090034
Lorenzo Colitti667c4772014-08-26 14:13:07 -070035 // NetId to use for forwarded DNS queries. This may not be the default
36 // network, e.g., in the case where we are tethering to a DUN APN.
Erik Kline2c5aaa12016-06-08 13:24:45 +090037 unsigned mDnsNetId;
38 std::list<std::string> mDnsForwarders;
39 pid_t mDaemonPid;
40 int mDaemonFd;
41 std::set<std::string> mForwardingRequests;
San Mehat9d10b342010-01-18 09:51:02 -080042
43public:
Lorenzo Colittia93126d2017-08-24 13:28:19 +090044
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070045 TetherController();
San Mehat9d10b342010-01-18 09:51:02 -080046 virtual ~TetherController();
47
Lorenzo Colittia93126d2017-08-24 13:28:19 +090048 // List of strings of interface pairs. Public because it's used by CommandListener.
49 // TODO: merge with mInterfaces, and make private.
50 std::list<std::string> ifacePairList;
51
Lorenzo Colitti799625c2015-02-25 12:52:00 +090052 bool enableForwarding(const char* requester);
53 bool disableForwarding(const char* requester);
54 size_t forwardingRequestCount();
San Mehat9d10b342010-01-18 09:51:02 -080055
Erik Kline13fa01f2015-11-12 17:49:23 +090056 int startTethering(int num_addrs, char **dhcp_ranges);
San Mehat9d10b342010-01-18 09:51:02 -080057 int stopTethering();
58 bool isTetheringStarted();
59
Lorenzo Colitti667c4772014-08-26 14:13:07 -070060 unsigned getDnsNetId();
61 int setDnsForwarders(unsigned netId, char **servers, int numServers);
Erik Kline2c5aaa12016-06-08 13:24:45 +090062 const std::list<std::string> &getDnsForwarders() const;
San Mehat9d10b342010-01-18 09:51:02 -080063
64 int tetherInterface(const char *interface);
65 int untetherInterface(const char *interface);
Erik Kline2c5aaa12016-06-08 13:24:45 +090066 const std::list<std::string> &getTetheredInterfaceList() const;
Erik Kline212c4052016-07-18 04:02:07 +090067 bool applyDnsInterfaces();
Robert Greenwalt3d4c7582012-12-11 12:33:37 -080068
Lorenzo Colittia93126d2017-08-24 13:28:19 +090069 int enableNat(const char* intIface, const char* extIface);
70 int disableNat(const char* intIface, const char* extIface);
71 int setupIptablesHooks();
72
73 class TetherStats {
74 public:
75 TetherStats() = default;
76 TetherStats(std::string intIfn, std::string extIfn,
77 int64_t rxB, int64_t rxP,
78 int64_t txB, int64_t txP)
79 : intIface(intIfn), extIface(extIfn),
80 rxBytes(rxB), rxPackets(rxP),
81 txBytes(txB), txPackets(txP) {};
82 std::string intIface;
83 std::string extIface;
84 int64_t rxBytes = -1;
85 int64_t rxPackets = -1;
86 int64_t txBytes = -1;
87 int64_t txPackets = -1;
88 /*
89 * Returns a new string representing this:
90 * intIface extIface rx_bytes rx_packets tx_bytes tx_packets
91 */
92 std::string getStatsLine() const;
93
94 bool addStatsIfMatch(const TetherStats& other) {
95 if (intIface == other.intIface && extIface == other.extIface) {
96 rxBytes += other.rxBytes;
97 rxPackets += other.rxPackets;
98 txBytes += other.txBytes;
99 txPackets += other.txPackets;
100 return true;
101 }
102 return false;
103 }
104 };
105
106 /*
Lorenzo Colitti09353392017-08-24 14:20:32 +0900107 * Sends out to the cli a list of stats TetheringStatsListResult+CommandOkay).
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900108 * Error is to be handled on the outside.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900109 */
Lorenzo Colitti09353392017-08-24 14:20:32 +0900110 int getTetherStats(SocketClient *cli, std::string &extraProcessingInfo);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900111 int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo);
112
113 typedef std::vector<TetherStats> TetherStatsList;
114
115 static void addStats(TetherStatsList& statsList, const TetherStats& stats);
116
117 /*
Lorenzo Colitti09353392017-08-24 14:20:32 +0900118 * output should be a file to the apropriate FORWARD chain of iptables rules.
119 * extraProcessingInfo: contains raw parsed data, and error info.
120 * This strongly requires that setup of the rules is in a specific order:
121 * in:intIface out:extIface
122 * in:extIface out:intIface
123 * and the rules are grouped in pairs when more that one tethering was setup.
124 */
125 static int addForwardChainStats(TetherStatsList& statsList, const std::string& iptOutput,
126 std::string &extraProcessingInfo);
127
128 /*
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900129 * stats should never have only intIface initialized. Other 3 combos are ok.
130 * fp should be a file to the apropriate FORWARD chain of iptables rules.
131 * extraProcessingInfo: contains raw parsed data, and error info.
132 * This strongly requires that setup of the rules is in a specific order:
133 * in:intIface out:extIface
134 * in:extIface out:intIface
135 * and the rules are grouped in pairs when more that one tethering was setup.
136 */
137 static int addForwardChainStats(const TetherStats& filter,
138 TetherStatsList& statsList, const std::string& iptOutput,
139 std::string &extraProcessingInfo);
140
141 static constexpr const char* LOCAL_FORWARD = "natctrl_FORWARD";
142 static constexpr const char* LOCAL_MANGLE_FORWARD = "natctrl_mangle_FORWARD";
143 static constexpr const char* LOCAL_NAT_POSTROUTING = "natctrl_nat_POSTROUTING";
144 static constexpr const char* LOCAL_RAW_PREROUTING = "natctrl_raw_PREROUTING";
145 static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters";
146
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800147private:
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900148 bool setIpFwdEnabled();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900149
150 int natCount;
151
152 static std::string makeTetherCountingRule(const char *if1, const char *if2);
153 bool checkTetherCountingRuleExist(const std::string& pair_name);
154
155 int setDefaults();
156 int setForwardRules(bool set, const char *intIface, const char *extIface);
157 int setTetherCountingRules(bool add, const char *intIface, const char *extIface);
158
159 // For testing.
160 friend class TetherControllerTest;
161 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
San Mehat9d10b342010-01-18 09:51:02 -0800162};
163
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900164} // namespace net
165} // namespace android
166
San Mehat9d10b342010-01-18 09:51:02 -0800167#endif