blob: 4ed94ee8187efff566efd8e50708b6a69f69d1b6 [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 /*
107 * For single pair of ifaces, stats should have ifaceIn and ifaceOut initialized.
108 * For all pairs, stats should have ifaceIn=ifaceOut="".
109 * Sends out to the cli the single stat (TetheringStatsReluts) or a list of stats
110 * (TetheringStatsListResult+CommandOkay).
111 * Error is to be handled on the outside.
112 * It results in an error if invoked and no tethering counter rules exist.
113 */
114 int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo);
115
116 typedef std::vector<TetherStats> TetherStatsList;
117
118 static void addStats(TetherStatsList& statsList, const TetherStats& stats);
119
120 /*
121 * stats should never have only intIface initialized. Other 3 combos are ok.
122 * fp should be a file to the apropriate FORWARD chain of iptables rules.
123 * extraProcessingInfo: contains raw parsed data, and error info.
124 * This strongly requires that setup of the rules is in a specific order:
125 * in:intIface out:extIface
126 * in:extIface out:intIface
127 * and the rules are grouped in pairs when more that one tethering was setup.
128 */
129 static int addForwardChainStats(const TetherStats& filter,
130 TetherStatsList& statsList, const std::string& iptOutput,
131 std::string &extraProcessingInfo);
132
133 static constexpr const char* LOCAL_FORWARD = "natctrl_FORWARD";
134 static constexpr const char* LOCAL_MANGLE_FORWARD = "natctrl_mangle_FORWARD";
135 static constexpr const char* LOCAL_NAT_POSTROUTING = "natctrl_nat_POSTROUTING";
136 static constexpr const char* LOCAL_RAW_PREROUTING = "natctrl_raw_PREROUTING";
137 static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters";
138
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800139private:
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900140 bool setIpFwdEnabled();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900141
142 int natCount;
143
144 static std::string makeTetherCountingRule(const char *if1, const char *if2);
145 bool checkTetherCountingRuleExist(const std::string& pair_name);
146
147 int setDefaults();
148 int setForwardRules(bool set, const char *intIface, const char *extIface);
149 int setTetherCountingRules(bool add, const char *intIface, const char *extIface);
150
151 // For testing.
152 friend class TetherControllerTest;
153 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
San Mehat9d10b342010-01-18 09:51:02 -0800154};
155
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900156} // namespace net
157} // namespace android
158
San Mehat9d10b342010-01-18 09:51:02 -0800159#endif