blob: e715a4135c931eef336c20a9144bed75f655ae33 [file] [log] [blame]
San Mehat9d10b342010-01-18 09:51:02 -08001/*
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 Kline70c03662016-03-31 11:39:53 +090020#include <list>
Lorenzo Colitti799625c2015-02-25 12:52:00 +090021#include <set>
22#include <string>
San Mehat9d10b342010-01-18 09:51:02 -080023
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090024#include <netdutils/StatusOr.h>
Lorenzo Colittia93126d2017-08-24 13:28:19 +090025#include <sysutils/SocketClient.h>
26
27#include "NetdConstants.h"
28
Lorenzo Colittie20a5262017-05-09 18:30:44 +090029namespace android {
30namespace net {
San Mehat9d10b342010-01-18 09:51:02 -080031
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090032using android::netdutils::StatusOr;
33
San Mehat9d10b342010-01-18 09:51:02 -080034class TetherController {
Erik Kline2c5aaa12016-06-08 13:24:45 +090035private:
36 std::list<std::string> mInterfaces;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090037
Lorenzo Colitti667c4772014-08-26 14:13:07 -070038 // 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 Kline2c5aaa12016-06-08 13:24:45 +090040 unsigned mDnsNetId;
41 std::list<std::string> mDnsForwarders;
42 pid_t mDaemonPid;
43 int mDaemonFd;
44 std::set<std::string> mForwardingRequests;
San Mehat9d10b342010-01-18 09:51:02 -080045
46public:
Lorenzo Colittia93126d2017-08-24 13:28:19 +090047
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070048 TetherController();
San Mehat9d10b342010-01-18 09:51:02 -080049 virtual ~TetherController();
50
Lorenzo Colittia93126d2017-08-24 13:28:19 +090051 // 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 Colitti799625c2015-02-25 12:52:00 +090055 bool enableForwarding(const char* requester);
56 bool disableForwarding(const char* requester);
57 size_t forwardingRequestCount();
San Mehat9d10b342010-01-18 09:51:02 -080058
Erik Kline13fa01f2015-11-12 17:49:23 +090059 int startTethering(int num_addrs, char **dhcp_ranges);
San Mehat9d10b342010-01-18 09:51:02 -080060 int stopTethering();
61 bool isTetheringStarted();
62
Lorenzo Colitti667c4772014-08-26 14:13:07 -070063 unsigned getDnsNetId();
64 int setDnsForwarders(unsigned netId, char **servers, int numServers);
Erik Kline2c5aaa12016-06-08 13:24:45 +090065 const std::list<std::string> &getDnsForwarders() const;
San Mehat9d10b342010-01-18 09:51:02 -080066
67 int tetherInterface(const char *interface);
68 int untetherInterface(const char *interface);
Erik Kline2c5aaa12016-06-08 13:24:45 +090069 const std::list<std::string> &getTetheredInterfaceList() const;
Erik Kline212c4052016-07-18 04:02:07 +090070 bool applyDnsInterfaces();
Robert Greenwalt3d4c7582012-12-11 12:33:37 -080071
Lorenzo Colittia93126d2017-08-24 13:28:19 +090072 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;
91 /*
92 * Returns a new string representing this:
93 * intIface extIface rx_bytes rx_packets tx_bytes tx_packets
94 */
95 std::string getStatsLine() const;
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 Colitti9a8a9ff2017-01-31 19:06:59 +0900109 typedef std::vector<TetherStats> TetherStatsList;
110
111 StatusOr<TetherStatsList> getTetherStats(std::string &extraProcessingInfo);
112
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900113 /*
Lorenzo Colitti09353392017-08-24 14:20:32 +0900114 * Sends out to the cli a list of stats TetheringStatsListResult+CommandOkay).
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900115 * Error is to be handled on the outside.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900116 */
Lorenzo Colitti09353392017-08-24 14:20:32 +0900117 int getTetherStats(SocketClient *cli, std::string &extraProcessingInfo);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900118
119 /*
Lorenzo Colitti09353392017-08-24 14:20:32 +0900120 * extraProcessingInfo: contains raw parsed data, and error info.
121 * This strongly requires that setup of the rules is in a specific order:
122 * in:intIface out:extIface
123 * in:extIface out:intIface
124 * and the rules are grouped in pairs when more that one tethering was setup.
125 */
126 static int addForwardChainStats(TetherStatsList& statsList, const std::string& iptOutput,
127 std::string &extraProcessingInfo);
128
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900129 static constexpr const char* LOCAL_FORWARD = "tetherctrl_FORWARD";
130 static constexpr const char* LOCAL_MANGLE_FORWARD = "tetherctrl_mangle_FORWARD";
131 static constexpr const char* LOCAL_NAT_POSTROUTING = "tetherctrl_nat_POSTROUTING";
132 static constexpr const char* LOCAL_RAW_PREROUTING = "tetherctrl_raw_PREROUTING";
133 static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "tetherctrl_counters";
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900134
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900135 android::RWLock lock;
136
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800137private:
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900138 bool setIpFwdEnabled();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900139
140 int natCount;
141
142 static std::string makeTetherCountingRule(const char *if1, const char *if2);
143 bool checkTetherCountingRuleExist(const std::string& pair_name);
144
145 int setDefaults();
146 int setForwardRules(bool set, const char *intIface, const char *extIface);
147 int setTetherCountingRules(bool add, const char *intIface, const char *extIface);
148
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900149 static void addStats(TetherStatsList& statsList, const TetherStats& stats);
150
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900151 // For testing.
152 friend class TetherControllerTest;
153 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
San Mehat9d10b342010-01-18 09:51:02 -0800154};
155
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900156} // namespace net
157} // namespace android
158
San Mehat9d10b342010-01-18 09:51:02 -0800159#endif