blob: d1d337ef573374e1be4c741c2b0c43caeab55f3e [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 Colitti9a8a9ff2017-01-31 19:06:59 +090024#include <netdutils/StatusOr.h>
Lorenzo Colittia93126d2017-08-24 13:28:19 +090025#include <sysutils/SocketClient.h>
26
27#include "NetdConstants.h"
28
Luke Huangd1ee4622018-06-29 13:49:58 +080029
Lorenzo Colittie20a5262017-05-09 18:30:44 +090030namespace android {
31namespace net {
San Mehat9d10b342010-01-18 09:51:02 -080032
33class TetherController {
Erik Kline2c5aaa12016-06-08 13:24:45 +090034private:
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +090035 struct ForwardingDownstream {
36 std::string iface;
37 bool active;
38 };
39
Erik Kline2c5aaa12016-06-08 13:24:45 +090040 std::list<std::string> mInterfaces;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090041
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +090042 // Map upstream iface -> downstream iface. A pair is in the map if forwarding was enabled at
43 // some point since the controller was initialized.
44 std::multimap<std::string, ForwardingDownstream> mFwdIfaces;
45
Lorenzo Colitti667c4772014-08-26 14:13:07 -070046 // NetId to use for forwarded DNS queries. This may not be the default
47 // network, e.g., in the case where we are tethering to a DUN APN.
Remi NGUYEN VAN7d9bebf2018-03-29 11:32:29 +090048 unsigned mDnsNetId = 0;
Erik Kline2c5aaa12016-06-08 13:24:45 +090049 std::list<std::string> mDnsForwarders;
Remi NGUYEN VAN7d9bebf2018-03-29 11:32:29 +090050 pid_t mDaemonPid = 0;
51 int mDaemonFd = -1;
Erik Kline2c5aaa12016-06-08 13:24:45 +090052 std::set<std::string> mForwardingRequests;
San Mehat9d10b342010-01-18 09:51:02 -080053
Erik Kline15079dd2018-05-18 23:10:56 +090054 struct DnsmasqState {
55 static int sendCmd(int daemonFd, const std::string& cmd);
56
57 // List of downstream interfaces on which to serve. The format used is:
58 // update_ifaces|<ifname1>|<ifname2>|...
59 std::string update_ifaces_cmd;
60 // Forwarding (upstream) DNS configuration to use. The format used is:
61 // update_dns|<hex_socket_mark>|<ip1>|<ip2>|...
62 std::string update_dns_cmd;
63
64 void clear();
65 int sendAllState(int daemonFd) const;
66 } mDnsmasqState{};
67
Erik Klineb31fd692018-06-06 20:50:11 +090068 public:
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070069 TetherController();
Remi NGUYEN VAN7d9bebf2018-03-29 11:32:29 +090070 ~TetherController() = default;
San Mehat9d10b342010-01-18 09:51:02 -080071
Lorenzo Colitti799625c2015-02-25 12:52:00 +090072 bool enableForwarding(const char* requester);
73 bool disableForwarding(const char* requester);
74 size_t forwardingRequestCount();
San Mehat9d10b342010-01-18 09:51:02 -080075
Erik Kline13fa01f2015-11-12 17:49:23 +090076 int startTethering(int num_addrs, char **dhcp_ranges);
San Mehat9d10b342010-01-18 09:51:02 -080077 int stopTethering();
78 bool isTetheringStarted();
79
Lorenzo Colitti667c4772014-08-26 14:13:07 -070080 unsigned getDnsNetId();
81 int setDnsForwarders(unsigned netId, char **servers, int numServers);
Erik Kline2c5aaa12016-06-08 13:24:45 +090082 const std::list<std::string> &getDnsForwarders() const;
San Mehat9d10b342010-01-18 09:51:02 -080083
84 int tetherInterface(const char *interface);
85 int untetherInterface(const char *interface);
Erik Kline2c5aaa12016-06-08 13:24:45 +090086 const std::list<std::string> &getTetheredInterfaceList() const;
Erik Kline212c4052016-07-18 04:02:07 +090087 bool applyDnsInterfaces();
Robert Greenwalt3d4c7582012-12-11 12:33:37 -080088
Lorenzo Colittia93126d2017-08-24 13:28:19 +090089 int enableNat(const char* intIface, const char* extIface);
90 int disableNat(const char* intIface, const char* extIface);
91 int setupIptablesHooks();
92
93 class TetherStats {
94 public:
95 TetherStats() = default;
96 TetherStats(std::string intIfn, std::string extIfn,
97 int64_t rxB, int64_t rxP,
98 int64_t txB, int64_t txP)
99 : intIface(intIfn), extIface(extIfn),
100 rxBytes(rxB), rxPackets(rxP),
101 txBytes(txB), txPackets(txP) {};
102 std::string intIface;
103 std::string extIface;
104 int64_t rxBytes = -1;
105 int64_t rxPackets = -1;
106 int64_t txBytes = -1;
107 int64_t txPackets = -1;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900108
109 bool addStatsIfMatch(const TetherStats& other) {
110 if (intIface == other.intIface && extIface == other.extIface) {
111 rxBytes += other.rxBytes;
112 rxPackets += other.rxPackets;
113 txBytes += other.txBytes;
114 txPackets += other.txPackets;
115 return true;
116 }
117 return false;
118 }
119 };
120
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900121 typedef std::vector<TetherStats> TetherStatsList;
122
Erik Klineb31fd692018-06-06 20:50:11 +0900123 netdutils::StatusOr<TetherStatsList> getTetherStats();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900124
125 /*
Lorenzo Colitti09353392017-08-24 14:20:32 +0900126 * extraProcessingInfo: contains raw parsed data, and error info.
127 * This strongly requires that setup of the rules is in a specific order:
128 * in:intIface out:extIface
129 * in:extIface out:intIface
130 * and the rules are grouped in pairs when more that one tethering was setup.
131 */
132 static int addForwardChainStats(TetherStatsList& statsList, const std::string& iptOutput,
133 std::string &extraProcessingInfo);
134
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900135 static constexpr const char* LOCAL_FORWARD = "tetherctrl_FORWARD";
136 static constexpr const char* LOCAL_MANGLE_FORWARD = "tetherctrl_mangle_FORWARD";
137 static constexpr const char* LOCAL_NAT_POSTROUTING = "tetherctrl_nat_POSTROUTING";
138 static constexpr const char* LOCAL_RAW_PREROUTING = "tetherctrl_raw_PREROUTING";
139 static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "tetherctrl_counters";
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900140
Luke Huangd1ee4622018-06-29 13:49:58 +0800141 std::mutex lock;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900142
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800143private:
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900144 bool setIpFwdEnabled();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900145
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900146 int setupIPv6CountersChain();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900147 static std::string makeTetherCountingRule(const char *if1, const char *if2);
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900148 ForwardingDownstream* findForwardingDownstream(const std::string& intIface,
149 const std::string& extIface);
150 void addForwardingPair(const std::string& intIface, const std::string& extIface);
151 void markForwardingPairDisabled(const std::string& intIface, const std::string& extIface);
152
153 bool isForwardingPairEnabled(const std::string& intIface, const std::string& extIface);
154 bool isAnyForwardingEnabledOnUpstream(const std::string& extIface);
155 bool isAnyForwardingPairEnabled();
156 bool tetherCountingRuleExists(const std::string& iface1, const std::string& iface2);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900157
158 int setDefaults();
159 int setForwardRules(bool set, const char *intIface, const char *extIface);
160 int setTetherCountingRules(bool add, const char *intIface, const char *extIface);
161
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900162 static void addStats(TetherStatsList& statsList, const TetherStats& stats);
163
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900164 // For testing.
165 friend class TetherControllerTest;
166 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
San Mehat9d10b342010-01-18 09:51:02 -0800167};
168
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900169} // namespace net
170} // namespace android
171
San Mehat9d10b342010-01-18 09:51:02 -0800172#endif