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