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 | 52db391 | 2020-02-17 23:59:45 +0900 | [diff] [blame] | 24 | #include <netdutils/DumpWriter.h> |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 25 | #include <netdutils/StatusOr.h> |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 26 | #include <sysutils/SocketClient.h> |
| 27 | |
| 28 | #include "NetdConstants.h" |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 29 | #include "android-base/result.h" |
| 30 | #include "bpf/BpfMap.h" |
| 31 | #include "netdbpf/bpf_shared.h" |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 32 | |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 33 | #include "android/net/TetherOffloadRuleParcel.h" |
| 34 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace net { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 37 | |
| 38 | class TetherController { |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 39 | private: |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 40 | struct ForwardingDownstream { |
| 41 | std::string iface; |
| 42 | bool active; |
| 43 | }; |
| 44 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 45 | std::list<std::string> mInterfaces; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 46 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 47 | // Map upstream iface -> downstream iface. A pair is in the map if forwarding was enabled at |
| 48 | // some point since the controller was initialized. |
| 49 | std::multimap<std::string, ForwardingDownstream> mFwdIfaces; |
| 50 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 51 | bool mIsTetheringStarted = false; |
| 52 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 53 | // NetId to use for forwarded DNS queries. This may not be the default |
| 54 | // 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] | 55 | unsigned mDnsNetId = 0; |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 56 | std::list<std::string> mDnsForwarders; |
Remi NGUYEN VAN | 7d9bebf | 2018-03-29 11:32:29 +0900 | [diff] [blame] | 57 | pid_t mDaemonPid = 0; |
| 58 | int mDaemonFd = -1; |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 59 | std::set<std::string> mForwardingRequests; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 60 | |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 61 | struct DnsmasqState { |
| 62 | static int sendCmd(int daemonFd, const std::string& cmd); |
| 63 | |
| 64 | // List of downstream interfaces on which to serve. The format used is: |
| 65 | // update_ifaces|<ifname1>|<ifname2>|... |
| 66 | std::string update_ifaces_cmd; |
| 67 | // Forwarding (upstream) DNS configuration to use. The format used is: |
| 68 | // update_dns|<hex_socket_mark>|<ip1>|<ip2>|... |
| 69 | std::string update_dns_cmd; |
| 70 | |
| 71 | void clear(); |
| 72 | int sendAllState(int daemonFd) const; |
| 73 | } mDnsmasqState{}; |
| 74 | |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 75 | // BPF maps, initialized by maybeInitMaps. |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 76 | bpf::BpfMap<TetherIngressKey, TetherIngressValue> mBpfIngressMap; |
| 77 | bpf::BpfMap<uint32_t, TetherStatsValue> mBpfStatsMap; |
Maciej Żenczykowski | d25a734 | 2020-05-04 18:18:38 -0700 | [diff] [blame] | 78 | bpf::BpfMap<uint32_t, uint64_t> mBpfLimitMap; |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 79 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 80 | public: |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 81 | TetherController(); |
Remi NGUYEN VAN | 7d9bebf | 2018-03-29 11:32:29 +0900 | [diff] [blame] | 82 | ~TetherController() = default; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 83 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 84 | bool enableForwarding(const char* requester); |
| 85 | bool disableForwarding(const char* requester); |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 86 | const std::set<std::string>& getIpfwdRequesterList() const; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 87 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 88 | //TODO: Clean up the overload function |
| 89 | int startTethering(bool isLegacyDnsProxy, int num_addrs, char** dhcp_ranges); |
| 90 | int startTethering(bool isLegacyDnsProxy, const std::vector<std::string>& dhcpRanges); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 91 | int stopTethering(); |
| 92 | bool isTetheringStarted(); |
| 93 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 94 | unsigned getDnsNetId(); |
| 95 | int setDnsForwarders(unsigned netId, char **servers, int numServers); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 96 | int setDnsForwarders(unsigned netId, const std::vector<std::string>& servers); |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 97 | const std::list<std::string> &getDnsForwarders() const; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 98 | |
| 99 | int tetherInterface(const char *interface); |
| 100 | int untetherInterface(const char *interface); |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 101 | const std::list<std::string> &getTetheredInterfaceList() const; |
Erik Kline | 212c405 | 2016-07-18 04:02:07 +0900 | [diff] [blame] | 102 | bool applyDnsInterfaces(); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 103 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 104 | int enableNat(const char* intIface, const char* extIface); |
| 105 | int disableNat(const char* intIface, const char* extIface); |
| 106 | int setupIptablesHooks(); |
| 107 | |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 108 | base::Result<void> addOffloadRule(const TetherOffloadRuleParcel& rule); |
| 109 | base::Result<void> removeOffloadRule(const TetherOffloadRuleParcel& rule); |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 110 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 111 | class TetherStats { |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 112 | public: |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 113 | TetherStats() = default; |
| 114 | TetherStats(std::string intIfn, std::string extIfn, |
| 115 | int64_t rxB, int64_t rxP, |
| 116 | int64_t txB, int64_t txP) |
| 117 | : intIface(intIfn), extIface(extIfn), |
| 118 | rxBytes(rxB), rxPackets(rxP), |
| 119 | txBytes(txB), txPackets(txP) {}; |
| 120 | std::string intIface; |
| 121 | std::string extIface; |
| 122 | int64_t rxBytes = -1; |
| 123 | int64_t rxPackets = -1; |
| 124 | int64_t txBytes = -1; |
| 125 | int64_t txPackets = -1; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 126 | |
| 127 | bool addStatsIfMatch(const TetherStats& other) { |
| 128 | if (intIface == other.intIface && extIface == other.extIface) { |
| 129 | rxBytes += other.rxBytes; |
| 130 | rxPackets += other.rxPackets; |
| 131 | txBytes += other.txBytes; |
| 132 | txPackets += other.txPackets; |
| 133 | return true; |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | }; |
| 138 | |
Hungming Chen | 41b0ed9 | 2020-06-02 00:13:20 +0000 | [diff] [blame^] | 139 | struct TetherOffloadStats { |
| 140 | int ifIndex; |
| 141 | int64_t rxBytes; |
| 142 | int64_t rxPackets; |
| 143 | int64_t txBytes; |
| 144 | int64_t txPackets; |
| 145 | }; |
| 146 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 147 | typedef std::vector<TetherStats> TetherStatsList; |
Hungming Chen | 41b0ed9 | 2020-06-02 00:13:20 +0000 | [diff] [blame^] | 148 | typedef std::vector<TetherOffloadStats> TetherOffloadStatsList; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 149 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 150 | netdutils::StatusOr<TetherStatsList> getTetherStats(); |
Hungming Chen | 41b0ed9 | 2020-06-02 00:13:20 +0000 | [diff] [blame^] | 151 | netdutils::StatusOr<TetherOffloadStatsList> getTetherOffloadStats(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 152 | |
| 153 | /* |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 154 | * extraProcessingInfo: contains raw parsed data, and error info. |
| 155 | * This strongly requires that setup of the rules is in a specific order: |
| 156 | * in:intIface out:extIface |
| 157 | * in:extIface out:intIface |
| 158 | * and the rules are grouped in pairs when more that one tethering was setup. |
| 159 | */ |
| 160 | static int addForwardChainStats(TetherStatsList& statsList, const std::string& iptOutput, |
| 161 | std::string &extraProcessingInfo); |
| 162 | |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 163 | static constexpr const char* LOCAL_FORWARD = "tetherctrl_FORWARD"; |
| 164 | static constexpr const char* LOCAL_MANGLE_FORWARD = "tetherctrl_mangle_FORWARD"; |
| 165 | static constexpr const char* LOCAL_NAT_POSTROUTING = "tetherctrl_nat_POSTROUTING"; |
| 166 | static constexpr const char* LOCAL_RAW_PREROUTING = "tetherctrl_raw_PREROUTING"; |
| 167 | static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "tetherctrl_counters"; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 168 | |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 169 | std::mutex lock; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 170 | |
Lorenzo Colitti | 52db391 | 2020-02-17 23:59:45 +0900 | [diff] [blame] | 171 | void dump(netdutils::DumpWriter& dw); |
| 172 | void dumpIfaces(netdutils::DumpWriter& dw); |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 173 | void dumpBpf(netdutils::DumpWriter& dw); |
Lorenzo Colitti | 52db391 | 2020-02-17 23:59:45 +0900 | [diff] [blame] | 174 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 175 | private: |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 176 | bool setIpFwdEnabled(); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 177 | std::vector<char*> toCstrVec(const std::vector<std::string>& addrs); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 178 | int setupIPv6CountersChain(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 179 | static std::string makeTetherCountingRule(const char *if1, const char *if2); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 180 | ForwardingDownstream* findForwardingDownstream(const std::string& intIface, |
| 181 | const std::string& extIface); |
| 182 | void addForwardingPair(const std::string& intIface, const std::string& extIface); |
| 183 | void markForwardingPairDisabled(const std::string& intIface, const std::string& extIface); |
| 184 | |
| 185 | bool isForwardingPairEnabled(const std::string& intIface, const std::string& extIface); |
| 186 | bool isAnyForwardingEnabledOnUpstream(const std::string& extIface); |
| 187 | bool isAnyForwardingPairEnabled(); |
| 188 | bool tetherCountingRuleExists(const std::string& iface1, const std::string& iface2); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 189 | |
| 190 | int setDefaults(); |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 191 | int setTetherGlobalAlertRule(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 192 | int setForwardRules(bool set, const char *intIface, const char *extIface); |
| 193 | int setTetherCountingRules(bool add, const char *intIface, const char *extIface); |
| 194 | |
Maciej Żenczykowski | ac14cb5 | 2020-05-23 20:48:00 +0000 | [diff] [blame] | 195 | base::Result<void> setBpfLimit(uint32_t ifIndex, uint64_t limit); |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 196 | void maybeInitMaps(); |
Hungming Chen | 1da9008 | 2020-02-14 20:24:16 +0800 | [diff] [blame] | 197 | void maybeStartBpf(const char* extIface); |
| 198 | void maybeStopBpf(const char* extIface); |
| 199 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 200 | static void addStats(TetherStatsList& statsList, const TetherStats& stats); |
| 201 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 202 | // For testing. |
| 203 | friend class TetherControllerTest; |
| 204 | static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 205 | }; |
| 206 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 207 | } // namespace net |
| 208 | } // namespace android |
| 209 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 210 | #endif |