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 | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 24 | #include <sysutils/SocketClient.h> |
| 25 | |
| 26 | #include "NetdConstants.h" |
| 27 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace net { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 30 | |
| 31 | class TetherController { |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 32 | private: |
| 33 | std::list<std::string> mInterfaces; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 34 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 35 | // NetId to use for forwarded DNS queries. This may not be the default |
| 36 | // network, e.g., in the case where we are tethering to a DUN APN. |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 37 | unsigned mDnsNetId; |
| 38 | std::list<std::string> mDnsForwarders; |
| 39 | pid_t mDaemonPid; |
| 40 | int mDaemonFd; |
| 41 | std::set<std::string> mForwardingRequests; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 42 | |
| 43 | public: |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 44 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 45 | TetherController(); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 46 | virtual ~TetherController(); |
| 47 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 48 | // List of strings of interface pairs. Public because it's used by CommandListener. |
| 49 | // TODO: merge with mInterfaces, and make private. |
| 50 | std::list<std::string> ifacePairList; |
| 51 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 52 | bool enableForwarding(const char* requester); |
| 53 | bool disableForwarding(const char* requester); |
| 54 | size_t forwardingRequestCount(); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 55 | |
Erik Kline | 13fa01f | 2015-11-12 17:49:23 +0900 | [diff] [blame] | 56 | int startTethering(int num_addrs, char **dhcp_ranges); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 57 | int stopTethering(); |
| 58 | bool isTetheringStarted(); |
| 59 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 60 | unsigned getDnsNetId(); |
| 61 | int setDnsForwarders(unsigned netId, char **servers, int numServers); |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 62 | const std::list<std::string> &getDnsForwarders() const; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 63 | |
| 64 | int tetherInterface(const char *interface); |
| 65 | int untetherInterface(const char *interface); |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 66 | const std::list<std::string> &getTetheredInterfaceList() const; |
Erik Kline | 212c405 | 2016-07-18 04:02:07 +0900 | [diff] [blame] | 67 | bool applyDnsInterfaces(); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 68 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 69 | int enableNat(const char* intIface, const char* extIface); |
| 70 | int disableNat(const char* intIface, const char* extIface); |
| 71 | int setupIptablesHooks(); |
| 72 | |
| 73 | class TetherStats { |
| 74 | public: |
| 75 | TetherStats() = default; |
| 76 | TetherStats(std::string intIfn, std::string extIfn, |
| 77 | int64_t rxB, int64_t rxP, |
| 78 | int64_t txB, int64_t txP) |
| 79 | : intIface(intIfn), extIface(extIfn), |
| 80 | rxBytes(rxB), rxPackets(rxP), |
| 81 | txBytes(txB), txPackets(txP) {}; |
| 82 | std::string intIface; |
| 83 | std::string extIface; |
| 84 | int64_t rxBytes = -1; |
| 85 | int64_t rxPackets = -1; |
| 86 | int64_t txBytes = -1; |
| 87 | int64_t txPackets = -1; |
| 88 | /* |
| 89 | * Returns a new string representing this: |
| 90 | * intIface extIface rx_bytes rx_packets tx_bytes tx_packets |
| 91 | */ |
| 92 | std::string getStatsLine() const; |
| 93 | |
| 94 | bool addStatsIfMatch(const TetherStats& other) { |
| 95 | if (intIface == other.intIface && extIface == other.extIface) { |
| 96 | rxBytes += other.rxBytes; |
| 97 | rxPackets += other.rxPackets; |
| 98 | txBytes += other.txBytes; |
| 99 | txPackets += other.txPackets; |
| 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | /* |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame^] | 107 | * Sends out to the cli a list of stats TetheringStatsListResult+CommandOkay). |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 108 | * Error is to be handled on the outside. |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 109 | */ |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame^] | 110 | int getTetherStats(SocketClient *cli, std::string &extraProcessingInfo); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 111 | int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo); |
| 112 | |
| 113 | typedef std::vector<TetherStats> TetherStatsList; |
| 114 | |
| 115 | static void addStats(TetherStatsList& statsList, const TetherStats& stats); |
| 116 | |
| 117 | /* |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame^] | 118 | * output should be a file to the apropriate FORWARD chain of iptables rules. |
| 119 | * extraProcessingInfo: contains raw parsed data, and error info. |
| 120 | * This strongly requires that setup of the rules is in a specific order: |
| 121 | * in:intIface out:extIface |
| 122 | * in:extIface out:intIface |
| 123 | * and the rules are grouped in pairs when more that one tethering was setup. |
| 124 | */ |
| 125 | static int addForwardChainStats(TetherStatsList& statsList, const std::string& iptOutput, |
| 126 | std::string &extraProcessingInfo); |
| 127 | |
| 128 | /* |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 129 | * stats should never have only intIface initialized. Other 3 combos are ok. |
| 130 | * fp should be a file to the apropriate FORWARD chain of iptables rules. |
| 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(const TetherStats& filter, |
| 138 | TetherStatsList& statsList, const std::string& iptOutput, |
| 139 | std::string &extraProcessingInfo); |
| 140 | |
| 141 | static constexpr const char* LOCAL_FORWARD = "natctrl_FORWARD"; |
| 142 | static constexpr const char* LOCAL_MANGLE_FORWARD = "natctrl_mangle_FORWARD"; |
| 143 | static constexpr const char* LOCAL_NAT_POSTROUTING = "natctrl_nat_POSTROUTING"; |
| 144 | static constexpr const char* LOCAL_RAW_PREROUTING = "natctrl_raw_PREROUTING"; |
| 145 | static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters"; |
| 146 | |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 147 | private: |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 148 | bool setIpFwdEnabled(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 149 | |
| 150 | int natCount; |
| 151 | |
| 152 | static std::string makeTetherCountingRule(const char *if1, const char *if2); |
| 153 | bool checkTetherCountingRuleExist(const std::string& pair_name); |
| 154 | |
| 155 | int setDefaults(); |
| 156 | int setForwardRules(bool set, const char *intIface, const char *extIface); |
| 157 | int setTetherCountingRules(bool add, const char *intIface, const char *extIface); |
| 158 | |
| 159 | // For testing. |
| 160 | friend class TetherControllerTest; |
| 161 | static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 164 | } // namespace net |
| 165 | } // namespace android |
| 166 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 167 | #endif |