• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <list>
21 #include <set>
22 #include <string>
23 
24 #include <netdutils/DumpWriter.h>
25 #include <netdutils/StatusOr.h>
26 #include <sysutils/SocketClient.h>
27 
28 #include "NetdConstants.h"
29 #include "android-base/result.h"
30 #include "bpf/BpfMap.h"
31 
32 #include "android/net/TetherOffloadRuleParcel.h"
33 
34 #include "mainline/XtBpfProgLocations.h"
35 
36 namespace android {
37 namespace net {
38 
39 class TetherController {
40   private:
41     struct ForwardingDownstream {
42         std::string iface;
43         bool active;
44     };
45 
46     std::list<std::string> mInterfaces;
47 
48     // Map upstream iface -> downstream iface. A pair is in the map if forwarding was enabled at
49     // some point since the controller was initialized.
50     std::multimap<std::string, ForwardingDownstream> mFwdIfaces;
51 
52     bool mIsTetheringStarted = false;
53 
54     // NetId to use for forwarded DNS queries. This may not be the default
55     // network, e.g., in the case where we are tethering to a DUN APN.
56     unsigned               mDnsNetId = 0;
57     std::list<std::string> mDnsForwarders;
58     pid_t                  mDaemonPid = 0;
59     int                    mDaemonFd = -1;
60     std::set<std::string>  mForwardingRequests;
61 
62     struct DnsmasqState {
63         static int sendCmd(int daemonFd, const std::string& cmd);
64 
65         // List of downstream interfaces on which to serve. The format used is:
66         //     update_ifaces|<ifname1>|<ifname2>|...
67         std::string update_ifaces_cmd;
68         // Forwarding (upstream) DNS configuration to use. The format used is:
69         //     update_dns|<hex_socket_mark>|<ip1>|<ip2>|...
70         std::string update_dns_cmd;
71 
72         void clear();
73         int sendAllState(int daemonFd) const;
74     } mDnsmasqState{};
75 
76   public:
77     TetherController();
78     ~TetherController() = default;
79 
80     bool enableForwarding(const char* requester);
81     bool disableForwarding(const char* requester);
82     const std::set<std::string>& getIpfwdRequesterList() const;
83 
84     //TODO: Clean up the overload function
85     int startTethering(bool isLegacyDnsProxy, int num_addrs, char** dhcp_ranges);
86     int startTethering(bool isLegacyDnsProxy, const std::vector<std::string>& dhcpRanges);
87     int stopTethering();
88     bool isTetheringStarted();
89 
90     unsigned getDnsNetId();
91     int setDnsForwarders(unsigned netId, char **servers, int numServers);
92     int setDnsForwarders(unsigned netId, const std::vector<std::string>& servers);
93     const std::list<std::string> &getDnsForwarders() const;
94 
95     int tetherInterface(const char *interface);
96     int untetherInterface(const char *interface);
97     const std::list<std::string> &getTetheredInterfaceList() const;
98     bool applyDnsInterfaces();
99 
100     int enableNat(const char* intIface, const char* extIface);
101     int disableNat(const char* intIface, const char* extIface);
102     int setupIptablesHooks();
103 
104     class TetherStats {
105       public:
106         TetherStats() = default;
TetherStats(std::string intIfn,std::string extIfn,int64_t rxB,int64_t rxP,int64_t txB,int64_t txP)107         TetherStats(std::string intIfn, std::string extIfn,
108                 int64_t rxB, int64_t rxP,
109                 int64_t txB, int64_t txP)
110                         : intIface(intIfn), extIface(extIfn),
111                             rxBytes(rxB), rxPackets(rxP),
112                             txBytes(txB), txPackets(txP) {};
113         std::string intIface;
114         std::string extIface;
115         int64_t rxBytes = -1;
116         int64_t rxPackets = -1;
117         int64_t txBytes = -1;
118         int64_t txPackets = -1;
119 
addStatsIfMatch(const TetherStats & other)120         bool addStatsIfMatch(const TetherStats& other) {
121             if (intIface == other.intIface && extIface == other.extIface) {
122                 rxBytes   += other.rxBytes;
123                 rxPackets += other.rxPackets;
124                 txBytes   += other.txBytes;
125                 txPackets += other.txPackets;
126                 return true;
127             }
128             return false;
129         }
130     };
131 
132     typedef std::vector<TetherStats> TetherStatsList;
133 
134     netdutils::StatusOr<TetherStatsList> getTetherStats();
135 
136     /*
137      * extraProcessingInfo: contains raw parsed data, and error info.
138      * This strongly requires that setup of the rules is in a specific order:
139      *  in:intIface out:extIface
140      *  in:extIface out:intIface
141      * and the rules are grouped in pairs when more that one tethering was setup.
142      */
143     static int addForwardChainStats(TetherStatsList& statsList, const std::string& iptOutput,
144                                     std::string &extraProcessingInfo);
145 
146     static constexpr const char* LOCAL_FORWARD               = "tetherctrl_FORWARD";
147     static constexpr const char* LOCAL_MANGLE_FORWARD        = "tetherctrl_mangle_FORWARD";
148     static constexpr const char* LOCAL_NAT_POSTROUTING       = "tetherctrl_nat_POSTROUTING";
149     static constexpr const char* LOCAL_RAW_PREROUTING        = "tetherctrl_raw_PREROUTING";
150     static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "tetherctrl_counters";
151 
152     std::mutex lock;
153 
154     void dump(netdutils::DumpWriter& dw);
155     void dumpIfaces(netdutils::DumpWriter& dw);
156 
157   private:
158     bool setIpFwdEnabled();
159     std::vector<char*> toCstrVec(const std::vector<std::string>& addrs);
160     int setupIPv6CountersChain();
161     static std::string makeTetherCountingRule(const char *if1, const char *if2);
162     ForwardingDownstream* findForwardingDownstream(const std::string& intIface,
163         const std::string& extIface);
164     void addForwardingPair(const std::string& intIface, const std::string& extIface);
165     void markForwardingPairDisabled(const std::string& intIface, const std::string& extIface);
166 
167     bool isForwardingPairEnabled(const std::string& intIface, const std::string& extIface);
168     bool isAnyForwardingEnabledOnUpstream(const std::string& extIface);
169     bool isAnyForwardingPairEnabled();
170     bool tetherCountingRuleExists(const std::string& iface1, const std::string& iface2);
171 
172     int setDefaults();
173     int setTetherGlobalAlertRule();
174     int setForwardRules(bool set, const char *intIface, const char *extIface);
175     int setTetherCountingRules(bool add, const char *intIface, const char *extIface);
176 
177     static void addStats(TetherStatsList& statsList, const TetherStats& stats);
178 
179     // For testing.
180     friend class TetherControllerTest;
181     static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
182 };
183 
184 }  // namespace net
185 }  // namespace android
186 
187 #endif
188