blob: e163ae854204889623036566ae3ed0b25eed4c57 [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
Lorenzo Colittie20a5262017-05-09 18:30:44 +090029namespace android {
30namespace net {
San Mehat9d10b342010-01-18 09:51:02 -080031
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090032using android::netdutils::StatusOr;
33
San Mehat9d10b342010-01-18 09:51:02 -080034class TetherController {
Erik Kline2c5aaa12016-06-08 13:24:45 +090035private:
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +090036 struct ForwardingDownstream {
37 std::string iface;
38 bool active;
39 };
40
Erik Kline2c5aaa12016-06-08 13:24:45 +090041 std::list<std::string> mInterfaces;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090042
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +090043 // Map upstream iface -> downstream iface. A pair is in the map if forwarding was enabled at
44 // some point since the controller was initialized.
45 std::multimap<std::string, ForwardingDownstream> mFwdIfaces;
46
Lorenzo Colitti667c4772014-08-26 14:13:07 -070047 // NetId to use for forwarded DNS queries. This may not be the default
48 // network, e.g., in the case where we are tethering to a DUN APN.
Remi NGUYEN VAN7d9bebf2018-03-29 11:32:29 +090049 unsigned mDnsNetId = 0;
Erik Kline2c5aaa12016-06-08 13:24:45 +090050 std::list<std::string> mDnsForwarders;
Remi NGUYEN VAN7d9bebf2018-03-29 11:32:29 +090051 pid_t mDaemonPid = 0;
52 int mDaemonFd = -1;
Erik Kline2c5aaa12016-06-08 13:24:45 +090053 std::set<std::string> mForwardingRequests;
San Mehat9d10b342010-01-18 09:51:02 -080054
Erik Kline15079dd2018-05-18 23:10:56 +090055 struct DnsmasqState {
56 static int sendCmd(int daemonFd, const std::string& cmd);
57
58 // List of downstream interfaces on which to serve. The format used is:
59 // update_ifaces|<ifname1>|<ifname2>|...
60 std::string update_ifaces_cmd;
61 // Forwarding (upstream) DNS configuration to use. The format used is:
62 // update_dns|<hex_socket_mark>|<ip1>|<ip2>|...
63 std::string update_dns_cmd;
64
65 void clear();
66 int sendAllState(int daemonFd) const;
67 } mDnsmasqState{};
68
San Mehat9d10b342010-01-18 09:51:02 -080069public:
Lorenzo Colittia93126d2017-08-24 13:28:19 +090070
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070071 TetherController();
Remi NGUYEN VAN7d9bebf2018-03-29 11:32:29 +090072 ~TetherController() = default;
San Mehat9d10b342010-01-18 09:51:02 -080073
Lorenzo Colitti799625c2015-02-25 12:52:00 +090074 bool enableForwarding(const char* requester);
75 bool disableForwarding(const char* requester);
76 size_t forwardingRequestCount();
San Mehat9d10b342010-01-18 09:51:02 -080077
Erik Kline13fa01f2015-11-12 17:49:23 +090078 int startTethering(int num_addrs, char **dhcp_ranges);
San Mehat9d10b342010-01-18 09:51:02 -080079 int stopTethering();
80 bool isTetheringStarted();
81
Lorenzo Colitti667c4772014-08-26 14:13:07 -070082 unsigned getDnsNetId();
83 int setDnsForwarders(unsigned netId, char **servers, int numServers);
Erik Kline2c5aaa12016-06-08 13:24:45 +090084 const std::list<std::string> &getDnsForwarders() const;
San Mehat9d10b342010-01-18 09:51:02 -080085
86 int tetherInterface(const char *interface);
87 int untetherInterface(const char *interface);
Erik Kline2c5aaa12016-06-08 13:24:45 +090088 const std::list<std::string> &getTetheredInterfaceList() const;
Erik Kline212c4052016-07-18 04:02:07 +090089 bool applyDnsInterfaces();
Robert Greenwalt3d4c7582012-12-11 12:33:37 -080090
Lorenzo Colittia93126d2017-08-24 13:28:19 +090091 int enableNat(const char* intIface, const char* extIface);
92 int disableNat(const char* intIface, const char* extIface);
93 int setupIptablesHooks();
94
95 class TetherStats {
96 public:
97 TetherStats() = default;
98 TetherStats(std::string intIfn, std::string extIfn,
99 int64_t rxB, int64_t rxP,
100 int64_t txB, int64_t txP)
101 : intIface(intIfn), extIface(extIfn),
102 rxBytes(rxB), rxPackets(rxP),
103 txBytes(txB), txPackets(txP) {};
104 std::string intIface;
105 std::string extIface;
106 int64_t rxBytes = -1;
107 int64_t rxPackets = -1;
108 int64_t txBytes = -1;
109 int64_t txPackets = -1;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900110
111 bool addStatsIfMatch(const TetherStats& other) {
112 if (intIface == other.intIface && extIface == other.extIface) {
113 rxBytes += other.rxBytes;
114 rxPackets += other.rxPackets;
115 txBytes += other.txBytes;
116 txPackets += other.txPackets;
117 return true;
118 }
119 return false;
120 }
121 };
122
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900123 typedef std::vector<TetherStats> TetherStatsList;
124
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900125 StatusOr<TetherStatsList> getTetherStats();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900126
127 /*
Lorenzo Colitti09353392017-08-24 14:20:32 +0900128 * extraProcessingInfo: contains raw parsed data, and error info.
129 * This strongly requires that setup of the rules is in a specific order:
130 * in:intIface out:extIface
131 * in:extIface out:intIface
132 * and the rules are grouped in pairs when more that one tethering was setup.
133 */
134 static int addForwardChainStats(TetherStatsList& statsList, const std::string& iptOutput,
135 std::string &extraProcessingInfo);
136
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900137 static constexpr const char* LOCAL_FORWARD = "tetherctrl_FORWARD";
138 static constexpr const char* LOCAL_MANGLE_FORWARD = "tetherctrl_mangle_FORWARD";
139 static constexpr const char* LOCAL_NAT_POSTROUTING = "tetherctrl_nat_POSTROUTING";
140 static constexpr const char* LOCAL_RAW_PREROUTING = "tetherctrl_raw_PREROUTING";
141 static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "tetherctrl_counters";
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900142
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900143 android::RWLock lock;
144
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800145private:
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900146 bool setIpFwdEnabled();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900147
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900148 int setupIPv6CountersChain();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900149 static std::string makeTetherCountingRule(const char *if1, const char *if2);
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900150 ForwardingDownstream* findForwardingDownstream(const std::string& intIface,
151 const std::string& extIface);
152 void addForwardingPair(const std::string& intIface, const std::string& extIface);
153 void markForwardingPairDisabled(const std::string& intIface, const std::string& extIface);
154
155 bool isForwardingPairEnabled(const std::string& intIface, const std::string& extIface);
156 bool isAnyForwardingEnabledOnUpstream(const std::string& extIface);
157 bool isAnyForwardingPairEnabled();
158 bool tetherCountingRuleExists(const std::string& iface1, const std::string& iface2);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900159
160 int setDefaults();
161 int setForwardRules(bool set, const char *intIface, const char *extIface);
162 int setTetherCountingRules(bool add, const char *intIface, const char *extIface);
163
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900164 static void addStats(TetherStatsList& statsList, const TetherStats& stats);
165
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900166 // For testing.
167 friend class TetherControllerTest;
168 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
San Mehat9d10b342010-01-18 09:51:02 -0800169};
170
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900171} // namespace net
172} // namespace android
173
San Mehat9d10b342010-01-18 09:51:02 -0800174#endif