blob: df43a7b1b7429fa9260e88425a23e913314ca85a [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;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090091
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 Colitti9a8a9ff2017-01-31 19:06:59 +0900104 typedef std::vector<TetherStats> TetherStatsList;
105
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900106 StatusOr<TetherStatsList> getTetherStats();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900107
108 /*
Lorenzo Colitti09353392017-08-24 14:20:32 +0900109 * 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 Colitti4604b4a2017-08-24 19:21:50 +0900118 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 Colittia93126d2017-08-24 13:28:19 +0900123
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900124 android::RWLock lock;
125
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800126private:
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900127 bool setIpFwdEnabled();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900128
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 Colitti9a8a9ff2017-01-31 19:06:59 +0900138 static void addStats(TetherStatsList& statsList, const TetherStats& stats);
139
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900140 // For testing.
141 friend class TetherControllerTest;
142 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
San Mehat9d10b342010-01-18 09:51:02 -0800143};
144
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900145} // namespace net
146} // namespace android
147
San Mehat9d10b342010-01-18 09:51:02 -0800148#endif