blob: 7bf8d3273956cfbb74ec778fff3f619ed6886dcf [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 Colitti52db3912020-02-17 23:59:45 +090024#include <netdutils/DumpWriter.h>
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090025#include <netdutils/StatusOr.h>
Lorenzo Colittia93126d2017-08-24 13:28:19 +090026#include <sysutils/SocketClient.h>
27
28#include "NetdConstants.h"
Lorenzo Colittie801d3c2020-02-18 00:00:35 +090029#include "android-base/result.h"
30#include "bpf/BpfMap.h"
31#include "netdbpf/bpf_shared.h"
Luke Huangd1ee4622018-06-29 13:49:58 +080032
Lorenzo Colittie20a5262017-05-09 18:30:44 +090033namespace android {
34namespace net {
San Mehat9d10b342010-01-18 09:51:02 -080035
36class TetherController {
waynema71a0b592018-11-21 13:31:34 +080037 private:
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +090038 struct ForwardingDownstream {
39 std::string iface;
40 bool active;
41 };
42
Erik Kline2c5aaa12016-06-08 13:24:45 +090043 std::list<std::string> mInterfaces;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090044
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +090045 // Map upstream iface -> downstream iface. A pair is in the map if forwarding was enabled at
46 // some point since the controller was initialized.
47 std::multimap<std::string, ForwardingDownstream> mFwdIfaces;
48
Luke Huang91bd3e12019-08-20 11:33:52 +080049 bool mIsTetheringStarted = false;
50
Lorenzo Colitti667c4772014-08-26 14:13:07 -070051 // NetId to use for forwarded DNS queries. This may not be the default
52 // network, e.g., in the case where we are tethering to a DUN APN.
Remi NGUYEN VAN7d9bebf2018-03-29 11:32:29 +090053 unsigned mDnsNetId = 0;
Erik Kline2c5aaa12016-06-08 13:24:45 +090054 std::list<std::string> mDnsForwarders;
Remi NGUYEN VAN7d9bebf2018-03-29 11:32:29 +090055 pid_t mDaemonPid = 0;
56 int mDaemonFd = -1;
Erik Kline2c5aaa12016-06-08 13:24:45 +090057 std::set<std::string> mForwardingRequests;
San Mehat9d10b342010-01-18 09:51:02 -080058
Erik Kline15079dd2018-05-18 23:10:56 +090059 struct DnsmasqState {
60 static int sendCmd(int daemonFd, const std::string& cmd);
61
62 // List of downstream interfaces on which to serve. The format used is:
63 // update_ifaces|<ifname1>|<ifname2>|...
64 std::string update_ifaces_cmd;
65 // Forwarding (upstream) DNS configuration to use. The format used is:
66 // update_dns|<hex_socket_mark>|<ip1>|<ip2>|...
67 std::string update_dns_cmd;
68
69 void clear();
70 int sendAllState(int daemonFd) const;
71 } mDnsmasqState{};
72
Lorenzo Colittie801d3c2020-02-18 00:00:35 +090073 bpf::BpfMap<TetherIngressKey, TetherIngressValue> mBpfIngressMap;
74 bpf::BpfMap<uint32_t, TetherStatsValue> mBpfStatsMap;
75
Erik Klineb31fd692018-06-06 20:50:11 +090076 public:
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070077 TetherController();
Remi NGUYEN VAN7d9bebf2018-03-29 11:32:29 +090078 ~TetherController() = default;
San Mehat9d10b342010-01-18 09:51:02 -080079
Lorenzo Colitti799625c2015-02-25 12:52:00 +090080 bool enableForwarding(const char* requester);
81 bool disableForwarding(const char* requester);
Luke Huang728cf4c2019-03-14 19:43:02 +080082 const std::set<std::string>& getIpfwdRequesterList() const;
San Mehat9d10b342010-01-18 09:51:02 -080083
Luke Huang91bd3e12019-08-20 11:33:52 +080084 //TODO: Clean up the overload function
85 int startTethering(bool isLegacyDnsProxy, int num_addrs, char** dhcp_ranges);
86 int startTethering(bool isLegacyDnsProxy, const std::vector<std::string>& dhcpRanges);
San Mehat9d10b342010-01-18 09:51:02 -080087 int stopTethering();
88 bool isTetheringStarted();
89
Lorenzo Colitti667c4772014-08-26 14:13:07 -070090 unsigned getDnsNetId();
91 int setDnsForwarders(unsigned netId, char **servers, int numServers);
Luke Huangb5733d72018-08-21 17:17:19 +080092 int setDnsForwarders(unsigned netId, const std::vector<std::string>& servers);
Erik Kline2c5aaa12016-06-08 13:24:45 +090093 const std::list<std::string> &getDnsForwarders() const;
San Mehat9d10b342010-01-18 09:51:02 -080094
95 int tetherInterface(const char *interface);
96 int untetherInterface(const char *interface);
Erik Kline2c5aaa12016-06-08 13:24:45 +090097 const std::list<std::string> &getTetheredInterfaceList() const;
Erik Kline212c4052016-07-18 04:02:07 +090098 bool applyDnsInterfaces();
Robert Greenwalt3d4c7582012-12-11 12:33:37 -080099
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900100 int enableNat(const char* intIface, const char* extIface);
101 int disableNat(const char* intIface, const char* extIface);
102 int setupIptablesHooks();
103
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900104 base::Result<void> addDownstreamIpv6Rule(int intIfaceIndex, int extIfaceIndex,
105 const std::vector<uint8_t>& ipAddress,
106 const std::vector<uint8_t>& srcL2Address,
107 const std::vector<uint8_t>& dstL2Address);
108
109 base::Result<void> removeDownstreamIpv6Rule(int extifaceIndex,
110 const std::vector<uint8_t>& ipAddress);
111
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900112 class TetherStats {
waynema71a0b592018-11-21 13:31:34 +0800113 public:
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900114 TetherStats() = default;
115 TetherStats(std::string intIfn, std::string extIfn,
116 int64_t rxB, int64_t rxP,
117 int64_t txB, int64_t txP)
118 : intIface(intIfn), extIface(extIfn),
119 rxBytes(rxB), rxPackets(rxP),
120 txBytes(txB), txPackets(txP) {};
121 std::string intIface;
122 std::string extIface;
123 int64_t rxBytes = -1;
124 int64_t rxPackets = -1;
125 int64_t txBytes = -1;
126 int64_t txPackets = -1;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900127
128 bool addStatsIfMatch(const TetherStats& other) {
129 if (intIface == other.intIface && extIface == other.extIface) {
130 rxBytes += other.rxBytes;
131 rxPackets += other.rxPackets;
132 txBytes += other.txBytes;
133 txPackets += other.txPackets;
134 return true;
135 }
136 return false;
137 }
138 };
139
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900140 typedef std::vector<TetherStats> TetherStatsList;
141
Erik Klineb31fd692018-06-06 20:50:11 +0900142 netdutils::StatusOr<TetherStatsList> getTetherStats();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900143
144 /*
Lorenzo Colitti09353392017-08-24 14:20:32 +0900145 * extraProcessingInfo: contains raw parsed data, and error info.
146 * This strongly requires that setup of the rules is in a specific order:
147 * in:intIface out:extIface
148 * in:extIface out:intIface
149 * and the rules are grouped in pairs when more that one tethering was setup.
150 */
151 static int addForwardChainStats(TetherStatsList& statsList, const std::string& iptOutput,
152 std::string &extraProcessingInfo);
153
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900154 static constexpr const char* LOCAL_FORWARD = "tetherctrl_FORWARD";
155 static constexpr const char* LOCAL_MANGLE_FORWARD = "tetherctrl_mangle_FORWARD";
156 static constexpr const char* LOCAL_NAT_POSTROUTING = "tetherctrl_nat_POSTROUTING";
157 static constexpr const char* LOCAL_RAW_PREROUTING = "tetherctrl_raw_PREROUTING";
158 static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "tetherctrl_counters";
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900159
Luke Huangd1ee4622018-06-29 13:49:58 +0800160 std::mutex lock;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900161
Lorenzo Colitti52db3912020-02-17 23:59:45 +0900162 void dump(netdutils::DumpWriter& dw);
163 void dumpIfaces(netdutils::DumpWriter& dw);
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900164 void dumpBpf(netdutils::DumpWriter& dw);
Lorenzo Colitti52db3912020-02-17 23:59:45 +0900165
waynema71a0b592018-11-21 13:31:34 +0800166 private:
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900167 bool setIpFwdEnabled();
Luke Huangb5733d72018-08-21 17:17:19 +0800168 std::vector<char*> toCstrVec(const std::vector<std::string>& addrs);
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900169 int setupIPv6CountersChain();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900170 static std::string makeTetherCountingRule(const char *if1, const char *if2);
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900171 ForwardingDownstream* findForwardingDownstream(const std::string& intIface,
172 const std::string& extIface);
173 void addForwardingPair(const std::string& intIface, const std::string& extIface);
174 void markForwardingPairDisabled(const std::string& intIface, const std::string& extIface);
175
176 bool isForwardingPairEnabled(const std::string& intIface, const std::string& extIface);
177 bool isAnyForwardingEnabledOnUpstream(const std::string& extIface);
178 bool isAnyForwardingPairEnabled();
179 bool tetherCountingRuleExists(const std::string& iface1, const std::string& iface2);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900180
181 int setDefaults();
Luke Huangae038f82018-11-05 11:17:31 +0900182 int setTetherGlobalAlertRule();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900183 int setForwardRules(bool set, const char *intIface, const char *extIface);
184 int setTetherCountingRules(bool add, const char *intIface, const char *extIface);
185
Hungming Chen1da90082020-02-14 20:24:16 +0800186 void maybeStartBpf(const char* extIface);
187 void maybeStopBpf(const char* extIface);
188
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900189 static void addStats(TetherStatsList& statsList, const TetherStats& stats);
190
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900191 // For testing.
192 friend class TetherControllerTest;
193 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
San Mehat9d10b342010-01-18 09:51:02 -0800194};
195
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900196} // namespace net
197} // namespace android
198
San Mehat9d10b342010-01-18 09:51:02 -0800199#endif