TetherController.h 6.4 KB

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