San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 1 | /* |
| 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 Kline | 70c0366 | 2016-03-31 11:39:53 +0900 | [diff] [blame] | 20 | #include <list> |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 21 | #include <set> |
| 22 | #include <string> |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 23 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 24 | #include <netdutils/StatusOr.h> |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 25 | #include <sysutils/SocketClient.h> |
| 26 | |
| 27 | #include "NetdConstants.h" |
| 28 | |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 29 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace net { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 32 | |
| 33 | class TetherController { |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 34 | private: |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 35 | struct ForwardingDownstream { |
| 36 | std::string iface; |
| 37 | bool active; |
| 38 | }; |
| 39 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 40 | std::list<std::string> mInterfaces; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 41 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 42 | // 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 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 46 | bool mIsTetheringStarted = false; |
| 47 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 48 | // NetId to use for forwarded DNS queries. This may not be the default |
| 49 | // network, e.g., in the case where we are tethering to a DUN APN. |
Remi NGUYEN VAN | 7d9bebf | 2018-03-29 11:32:29 +0900 | [diff] [blame] | 50 | unsigned mDnsNetId = 0; |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 51 | std::list<std::string> mDnsForwarders; |
Remi NGUYEN VAN | 7d9bebf | 2018-03-29 11:32:29 +0900 | [diff] [blame] | 52 | pid_t mDaemonPid = 0; |
| 53 | int mDaemonFd = -1; |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 54 | std::set<std::string> mForwardingRequests; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 55 | |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 56 | struct DnsmasqState { |
| 57 | static int sendCmd(int daemonFd, const std::string& cmd); |
| 58 | |
| 59 | // List of downstream interfaces on which to serve. The format used is: |
| 60 | // update_ifaces|<ifname1>|<ifname2>|... |
| 61 | std::string update_ifaces_cmd; |
| 62 | // Forwarding (upstream) DNS configuration to use. The format used is: |
| 63 | // update_dns|<hex_socket_mark>|<ip1>|<ip2>|... |
| 64 | std::string update_dns_cmd; |
| 65 | |
| 66 | void clear(); |
| 67 | int sendAllState(int daemonFd) const; |
| 68 | } mDnsmasqState{}; |
| 69 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 70 | public: |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 71 | TetherController(); |
Remi NGUYEN VAN | 7d9bebf | 2018-03-29 11:32:29 +0900 | [diff] [blame] | 72 | ~TetherController() = default; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 73 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 74 | bool enableForwarding(const char* requester); |
| 75 | bool disableForwarding(const char* requester); |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 76 | const std::set<std::string>& getIpfwdRequesterList() const; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 77 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 78 | //TODO: Clean up the overload function |
| 79 | int startTethering(bool isLegacyDnsProxy, int num_addrs, char** dhcp_ranges); |
| 80 | int startTethering(bool isLegacyDnsProxy, const std::vector<std::string>& dhcpRanges); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 81 | int stopTethering(); |
| 82 | bool isTetheringStarted(); |
| 83 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 84 | unsigned getDnsNetId(); |
| 85 | int setDnsForwarders(unsigned netId, char **servers, int numServers); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 86 | int setDnsForwarders(unsigned netId, const std::vector<std::string>& servers); |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 87 | const std::list<std::string> &getDnsForwarders() const; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 88 | |
| 89 | int tetherInterface(const char *interface); |
| 90 | int untetherInterface(const char *interface); |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 91 | const std::list<std::string> &getTetheredInterfaceList() const; |
Erik Kline | 212c405 | 2016-07-18 04:02:07 +0900 | [diff] [blame] | 92 | bool applyDnsInterfaces(); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 93 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 94 | int enableNat(const char* intIface, const char* extIface); |
| 95 | int disableNat(const char* intIface, const char* extIface); |
| 96 | int setupIptablesHooks(); |
| 97 | |
| 98 | class TetherStats { |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 99 | public: |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 100 | TetherStats() = default; |
| 101 | TetherStats(std::string intIfn, std::string extIfn, |
| 102 | int64_t rxB, int64_t rxP, |
| 103 | int64_t txB, int64_t txP) |
| 104 | : intIface(intIfn), extIface(extIfn), |
| 105 | rxBytes(rxB), rxPackets(rxP), |
| 106 | txBytes(txB), txPackets(txP) {}; |
| 107 | std::string intIface; |
| 108 | std::string extIface; |
| 109 | int64_t rxBytes = -1; |
| 110 | int64_t rxPackets = -1; |
| 111 | int64_t txBytes = -1; |
| 112 | int64_t txPackets = -1; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 113 | |
| 114 | bool addStatsIfMatch(const TetherStats& other) { |
| 115 | if (intIface == other.intIface && extIface == other.extIface) { |
| 116 | rxBytes += other.rxBytes; |
| 117 | rxPackets += other.rxPackets; |
| 118 | txBytes += other.txBytes; |
| 119 | txPackets += other.txPackets; |
| 120 | return true; |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | }; |
| 125 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 126 | typedef std::vector<TetherStats> TetherStatsList; |
| 127 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 128 | netdutils::StatusOr<TetherStatsList> getTetherStats(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 129 | |
| 130 | /* |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 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(TetherStatsList& statsList, const std::string& iptOutput, |
| 138 | std::string &extraProcessingInfo); |
| 139 | |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 140 | static constexpr const char* LOCAL_FORWARD = "tetherctrl_FORWARD"; |
| 141 | static constexpr const char* LOCAL_MANGLE_FORWARD = "tetherctrl_mangle_FORWARD"; |
| 142 | static constexpr const char* LOCAL_NAT_POSTROUTING = "tetherctrl_nat_POSTROUTING"; |
| 143 | static constexpr const char* LOCAL_RAW_PREROUTING = "tetherctrl_raw_PREROUTING"; |
| 144 | static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "tetherctrl_counters"; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 145 | |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 146 | std::mutex lock; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 147 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 148 | private: |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 149 | bool setIpFwdEnabled(); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 150 | std::vector<char*> toCstrVec(const std::vector<std::string>& addrs); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 151 | int setupIPv6CountersChain(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 152 | static std::string makeTetherCountingRule(const char *if1, const char *if2); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 153 | ForwardingDownstream* findForwardingDownstream(const std::string& intIface, |
| 154 | const std::string& extIface); |
| 155 | void addForwardingPair(const std::string& intIface, const std::string& extIface); |
| 156 | void markForwardingPairDisabled(const std::string& intIface, const std::string& extIface); |
| 157 | |
| 158 | bool isForwardingPairEnabled(const std::string& intIface, const std::string& extIface); |
| 159 | bool isAnyForwardingEnabledOnUpstream(const std::string& extIface); |
| 160 | bool isAnyForwardingPairEnabled(); |
| 161 | bool tetherCountingRuleExists(const std::string& iface1, const std::string& iface2); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 162 | |
| 163 | int setDefaults(); |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 164 | int setTetherGlobalAlertRule(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 165 | int setForwardRules(bool set, const char *intIface, const char *extIface); |
| 166 | int setTetherCountingRules(bool add, const char *intIface, const char *extIface); |
| 167 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 168 | static void addStats(TetherStatsList& statsList, const TetherStats& stats); |
| 169 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 170 | // For testing. |
| 171 | friend class TetherControllerTest; |
| 172 | static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 175 | } // namespace net |
| 176 | } // namespace android |
| 177 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 178 | #endif |