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 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace net { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 31 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 32 | using android::netdutils::StatusOr; |
| 33 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 34 | class TetherController { |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 35 | private: |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 36 | struct ForwardingDownstream { |
| 37 | std::string iface; |
| 38 | bool active; |
| 39 | }; |
| 40 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 41 | std::list<std::string> mInterfaces; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 42 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 43 | // 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 Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 47 | // 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 VAN | 7d9bebf | 2018-03-29 11:32:29 +0900 | [diff] [blame] | 49 | unsigned mDnsNetId = 0; |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 50 | std::list<std::string> mDnsForwarders; |
Remi NGUYEN VAN | 7d9bebf | 2018-03-29 11:32:29 +0900 | [diff] [blame] | 51 | pid_t mDaemonPid = 0; |
| 52 | int mDaemonFd = -1; |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 53 | std::set<std::string> mForwardingRequests; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 54 | |
| 55 | public: |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 56 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 57 | TetherController(); |
Remi NGUYEN VAN | 7d9bebf | 2018-03-29 11:32:29 +0900 | [diff] [blame] | 58 | ~TetherController() = default; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 59 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 60 | bool enableForwarding(const char* requester); |
| 61 | bool disableForwarding(const char* requester); |
| 62 | size_t forwardingRequestCount(); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 63 | |
Erik Kline | 13fa01f | 2015-11-12 17:49:23 +0900 | [diff] [blame] | 64 | int startTethering(int num_addrs, char **dhcp_ranges); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 65 | int stopTethering(); |
| 66 | bool isTetheringStarted(); |
| 67 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 68 | unsigned getDnsNetId(); |
| 69 | int setDnsForwarders(unsigned netId, char **servers, int numServers); |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 70 | const std::list<std::string> &getDnsForwarders() const; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 71 | |
| 72 | int tetherInterface(const char *interface); |
| 73 | int untetherInterface(const char *interface); |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 74 | const std::list<std::string> &getTetheredInterfaceList() const; |
Erik Kline | 212c405 | 2016-07-18 04:02:07 +0900 | [diff] [blame] | 75 | bool applyDnsInterfaces(); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 76 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 77 | int enableNat(const char* intIface, const char* extIface); |
| 78 | int disableNat(const char* intIface, const char* extIface); |
| 79 | int setupIptablesHooks(); |
| 80 | |
| 81 | class TetherStats { |
| 82 | public: |
| 83 | TetherStats() = default; |
| 84 | TetherStats(std::string intIfn, std::string extIfn, |
| 85 | int64_t rxB, int64_t rxP, |
| 86 | int64_t txB, int64_t txP) |
| 87 | : intIface(intIfn), extIface(extIfn), |
| 88 | rxBytes(rxB), rxPackets(rxP), |
| 89 | txBytes(txB), txPackets(txP) {}; |
| 90 | std::string intIface; |
| 91 | std::string extIface; |
| 92 | int64_t rxBytes = -1; |
| 93 | int64_t rxPackets = -1; |
| 94 | int64_t txBytes = -1; |
| 95 | int64_t txPackets = -1; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 96 | |
| 97 | bool addStatsIfMatch(const TetherStats& other) { |
| 98 | if (intIface == other.intIface && extIface == other.extIface) { |
| 99 | rxBytes += other.rxBytes; |
| 100 | rxPackets += other.rxPackets; |
| 101 | txBytes += other.txBytes; |
| 102 | txPackets += other.txPackets; |
| 103 | return true; |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | }; |
| 108 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 109 | typedef std::vector<TetherStats> TetherStatsList; |
| 110 | |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 111 | StatusOr<TetherStatsList> getTetherStats(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 112 | |
| 113 | /* |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 114 | * extraProcessingInfo: contains raw parsed data, and error info. |
| 115 | * This strongly requires that setup of the rules is in a specific order: |
| 116 | * in:intIface out:extIface |
| 117 | * in:extIface out:intIface |
| 118 | * and the rules are grouped in pairs when more that one tethering was setup. |
| 119 | */ |
| 120 | static int addForwardChainStats(TetherStatsList& statsList, const std::string& iptOutput, |
| 121 | std::string &extraProcessingInfo); |
| 122 | |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 123 | static constexpr const char* LOCAL_FORWARD = "tetherctrl_FORWARD"; |
| 124 | static constexpr const char* LOCAL_MANGLE_FORWARD = "tetherctrl_mangle_FORWARD"; |
| 125 | static constexpr const char* LOCAL_NAT_POSTROUTING = "tetherctrl_nat_POSTROUTING"; |
| 126 | static constexpr const char* LOCAL_RAW_PREROUTING = "tetherctrl_raw_PREROUTING"; |
| 127 | static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "tetherctrl_counters"; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 128 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 129 | android::RWLock lock; |
| 130 | |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 131 | private: |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 132 | bool setIpFwdEnabled(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 133 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 134 | int setupIPv6CountersChain(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 135 | static std::string makeTetherCountingRule(const char *if1, const char *if2); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 136 | ForwardingDownstream* findForwardingDownstream(const std::string& intIface, |
| 137 | const std::string& extIface); |
| 138 | void addForwardingPair(const std::string& intIface, const std::string& extIface); |
| 139 | void markForwardingPairDisabled(const std::string& intIface, const std::string& extIface); |
| 140 | |
| 141 | bool isForwardingPairEnabled(const std::string& intIface, const std::string& extIface); |
| 142 | bool isAnyForwardingEnabledOnUpstream(const std::string& extIface); |
| 143 | bool isAnyForwardingPairEnabled(); |
| 144 | bool tetherCountingRuleExists(const std::string& iface1, const std::string& iface2); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 145 | |
| 146 | int setDefaults(); |
| 147 | int setForwardRules(bool set, const char *intIface, const char *extIface); |
| 148 | int setTetherCountingRules(bool add, const char *intIface, const char *extIface); |
| 149 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 150 | static void addStats(TetherStatsList& statsList, const TetherStats& stats); |
| 151 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 152 | // For testing. |
| 153 | friend class TetherControllerTest; |
| 154 | static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 157 | } // namespace net |
| 158 | } // namespace android |
| 159 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 160 | #endif |