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