Controllers.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2016 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. #include <regex>
  17. #include <set>
  18. #include <string>
  19. #include <android-base/stringprintf.h>
  20. #include <android-base/strings.h>
  21. #include <netdutils/Stopwatch.h>
  22. #define LOG_TAG "Netd"
  23. #include <log/log.h>
  24. #include "Controllers.h"
  25. #include "IdletimerController.h"
  26. #include "NetworkController.h"
  27. #include "RouteController.h"
  28. #include "XfrmController.h"
  29. #include "oem_iptables_hook.h"
  30. namespace android {
  31. namespace net {
  32. using android::base::StringAppendF;
  33. using android::base::StringPrintf;
  34. using android::netdutils::Stopwatch;
  35. auto Controllers::execIptablesRestore = ::execIptablesRestore;
  36. auto Controllers::execIptablesRestoreWithOutput = ::execIptablesRestoreWithOutput;
  37. netdutils::Log gLog("netd");
  38. netdutils::Log gUnsolicitedLog("netdUnsolicited");
  39. namespace {
  40. /**
  41. * List of module chains to be created, along with explicit ordering. ORDERING
  42. * IS CRITICAL, AND SHOULD BE TRIPLE-CHECKED WITH EACH CHANGE.
  43. */
  44. static const std::vector<const char*> FILTER_INPUT = {
  45. // Bandwidth should always be early in input chain, to make sure we
  46. // correctly count incoming traffic against data plan.
  47. BandwidthController::LOCAL_INPUT,
  48. FirewallController::LOCAL_INPUT,
  49. };
  50. static const std::vector<const char*> FILTER_FORWARD = {
  51. OEM_IPTABLES_FILTER_FORWARD,
  52. FirewallController::LOCAL_FORWARD,
  53. BandwidthController::LOCAL_FORWARD,
  54. TetherController::LOCAL_FORWARD,
  55. };
  56. static const std::vector<const char*> FILTER_OUTPUT = {
  57. OEM_IPTABLES_FILTER_OUTPUT,
  58. FirewallController::LOCAL_OUTPUT,
  59. StrictController::LOCAL_OUTPUT,
  60. BandwidthController::LOCAL_OUTPUT,
  61. };
  62. static const std::vector<const char*> RAW_PREROUTING = {
  63. ClatdController::LOCAL_RAW_PREROUTING,
  64. BandwidthController::LOCAL_RAW_PREROUTING,
  65. IdletimerController::LOCAL_RAW_PREROUTING,
  66. TetherController::LOCAL_RAW_PREROUTING,
  67. };
  68. static const std::vector<const char*> MANGLE_POSTROUTING = {
  69. OEM_IPTABLES_MANGLE_POSTROUTING,
  70. BandwidthController::LOCAL_MANGLE_POSTROUTING,
  71. IdletimerController::LOCAL_MANGLE_POSTROUTING,
  72. };
  73. static const std::vector<const char*> MANGLE_INPUT = {
  74. WakeupController::LOCAL_MANGLE_INPUT,
  75. RouteController::LOCAL_MANGLE_INPUT,
  76. };
  77. static const std::vector<const char*> MANGLE_FORWARD = {
  78. TetherController::LOCAL_MANGLE_FORWARD,
  79. };
  80. static const std::vector<const char*> NAT_PREROUTING = {
  81. OEM_IPTABLES_NAT_PREROUTING,
  82. };
  83. static const std::vector<const char*> NAT_POSTROUTING = {
  84. TetherController::LOCAL_NAT_POSTROUTING,
  85. };
  86. // Commands to create child chains and to match created chains in iptables -S output. Keep in sync.
  87. static const char* CHILD_CHAIN_TEMPLATE = "-A %s -j %s\n";
  88. static const std::regex CHILD_CHAIN_REGEX("^-A ([^ ]+) -j ([^ ]+)$",
  89. std::regex_constants::extended);
  90. } // namespace
  91. /* static */
  92. std::set<std::string> Controllers::findExistingChildChains(const IptablesTarget target,
  93. const char* table,
  94. const char* parentChain) {
  95. if (target == V4V6) {
  96. ALOGE("findExistingChildChains only supports one protocol at a time");
  97. abort();
  98. }
  99. std::set<std::string> existing;
  100. // List the current contents of parentChain.
  101. //
  102. // TODO: there is no guarantee that nothing else modifies the chain in the few milliseconds
  103. // between when we list the existing rules and when we delete them. However:
  104. // - Since this code is only run on startup, nothing else in netd will be running.
  105. // - While vendor code is known to add its own rules to chains created by netd, it should never
  106. // be modifying the rules in childChains or the rules that hook said chains into their parent
  107. // chains.
  108. std::string command = StringPrintf("*%s\n-S %s\nCOMMIT\n", table, parentChain);
  109. std::string output;
  110. if (Controllers::execIptablesRestoreWithOutput(target, command, &output) == -1) {
  111. ALOGE("Error listing chain %s in table %s\n", parentChain, table);
  112. return existing;
  113. }
  114. // The only rules added by createChildChains are of the simple form "-A <parent> -j <child>".
  115. // Find those rules and add each one's child chain to existing.
  116. std::smatch matches;
  117. std::stringstream stream(output);
  118. std::string rule;
  119. while (std::getline(stream, rule, '\n')) {
  120. if (std::regex_search(rule, matches, CHILD_CHAIN_REGEX) && matches[1] == parentChain) {
  121. existing.insert(matches[2]);
  122. }
  123. }
  124. return existing;
  125. }
  126. /* static */
  127. void Controllers::createChildChains(IptablesTarget target, const char* table,
  128. const char* parentChain,
  129. const std::vector<const char*>& childChains,
  130. bool exclusive) {
  131. std::string command = StringPrintf("*%s\n", table);
  132. // We cannot just clear all the chains we create because vendor code modifies filter OUTPUT and
  133. // mangle POSTROUTING directly. So:
  134. //
  135. // - If we're the exclusive owner of this chain, simply clear it entirely.
  136. // - If not, then list the chain's current contents to ensure that if we restart after a crash,
  137. // we leave the existing rules alone in the positions they currently occupy. This is faster
  138. // than blindly deleting our rules and recreating them, because deleting a rule that doesn't
  139. // exists causes iptables-restore to quit, which takes ~30ms per delete. It's also more
  140. // correct, because if we delete rules and re-add them, they'll be in the wrong position with
  141. // regards to the vendor rules.
  142. //
  143. // TODO: Make all chains exclusive once vendor code uses the oem_* rules.
  144. std::set<std::string> existingChildChains;
  145. if (exclusive) {
  146. // Just running ":chain -" flushes user-defined chains, but not built-in chains like INPUT.
  147. // Since at this point we don't know if parentChain is a built-in chain, do both.
  148. StringAppendF(&command, ":%s -\n", parentChain);
  149. StringAppendF(&command, "-F %s\n", parentChain);
  150. } else {
  151. existingChildChains = findExistingChildChains(target, table, parentChain);
  152. }
  153. for (const auto& childChain : childChains) {
  154. // Always clear the child chain.
  155. StringAppendF(&command, ":%s -\n", childChain);
  156. // But only add it to the parent chain if it's not already there.
  157. if (existingChildChains.find(childChain) == existingChildChains.end()) {
  158. StringAppendF(&command, CHILD_CHAIN_TEMPLATE, parentChain, childChain);
  159. }
  160. }
  161. command += "COMMIT\n";
  162. execIptablesRestore(target, command);
  163. }
  164. Controllers::Controllers()
  165. : clatdCtrl(&netCtrl),
  166. wakeupCtrl(
  167. [this](const WakeupController::ReportArgs& args) {
  168. const auto listener = eventReporter.getNetdEventListener();
  169. if (listener == nullptr) {
  170. gLog.error("getNetdEventListener() returned nullptr. dropping wakeup event");
  171. return;
  172. }
  173. String16 prefix = String16(args.prefix.c_str());
  174. String16 srcIp = String16(args.srcIp.c_str());
  175. String16 dstIp = String16(args.dstIp.c_str());
  176. listener->onWakeupEvent(prefix, args.uid, args.ethertype, args.ipNextHeader,
  177. args.dstHw, srcIp, dstIp, args.srcPort, args.dstPort,
  178. args.timestampNs);
  179. },
  180. &iptablesRestoreCtrl) {
  181. InterfaceController::initializeAll();
  182. }
  183. void Controllers::initChildChains() {
  184. /*
  185. * This is the only time we touch top-level chains in iptables; controllers
  186. * should only mutate rules inside of their children chains, as created by
  187. * the constants above.
  188. *
  189. * Modules should never ACCEPT packets (except in well-justified cases);
  190. * they should instead defer to any remaining modules using RETURN, or
  191. * otherwise DROP/REJECT.
  192. */
  193. // Create chains for child modules.
  194. createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true);
  195. createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD, true);
  196. createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING, true);
  197. createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD, true);
  198. createChildChains(V4V6, "mangle", "INPUT", MANGLE_INPUT, true);
  199. createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING, true);
  200. createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING, true);
  201. createChildChains(V4, "filter", "OUTPUT", FILTER_OUTPUT, false);
  202. createChildChains(V6, "filter", "OUTPUT", FILTER_OUTPUT, false);
  203. createChildChains(V4, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
  204. createChildChains(V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
  205. }
  206. void Controllers::initIptablesRules() {
  207. Stopwatch s;
  208. initChildChains();
  209. gLog.info("Creating child chains: %.1fms", s.getTimeAndReset());
  210. // Let each module setup their child chains
  211. setupOemIptablesHook();
  212. gLog.info("Setting up OEM hooks: %.1fms", s.getTimeAndReset());
  213. /* When enabled, DROPs all packets except those matching rules. */
  214. firewallCtrl.setupIptablesHooks();
  215. gLog.info("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset());
  216. /* Does DROPs in FORWARD by default */
  217. tetherCtrl.setupIptablesHooks();
  218. gLog.info("Setting up TetherController hooks: %.1fms", s.getTimeAndReset());
  219. /*
  220. * Does REJECT in INPUT, OUTPUT. Does counting also.
  221. * No DROP/REJECT allowed later in netfilter-flow hook order.
  222. */
  223. bandwidthCtrl.setupIptablesHooks();
  224. gLog.info("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset());
  225. /*
  226. * Counts in nat: PREROUTING, POSTROUTING.
  227. * No DROP/REJECT allowed later in netfilter-flow hook order.
  228. */
  229. idletimerCtrl.setupIptablesHooks();
  230. gLog.info("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset());
  231. /*
  232. * Add rules for detecting IPv6/IPv4 TCP/UDP connections with TLS/DTLS header
  233. */
  234. strictCtrl.setupIptablesHooks();
  235. gLog.info("Setting up StrictController hooks: %.1fms", s.getTimeAndReset());
  236. }
  237. void Controllers::init() {
  238. initIptablesRules();
  239. Stopwatch s;
  240. clatdCtrl.init();
  241. gLog.info("Initializing ClatdController: %.1fms", s.getTimeAndReset());
  242. netdutils::Status tcStatus = trafficCtrl.start();
  243. if (!isOk(tcStatus)) {
  244. gLog.error("Failed to start trafficcontroller: (%s)", toString(tcStatus).c_str());
  245. }
  246. gLog.info("Initializing traffic control: %.1fms", s.getTimeAndReset());
  247. bandwidthCtrl.setBpfEnabled(trafficCtrl.getBpfLevel() != android::bpf::BpfLevel::NONE);
  248. bandwidthCtrl.enableBandwidthControl();
  249. gLog.info("Enabling bandwidth control: %.1fms", s.getTimeAndReset());
  250. if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) {
  251. gLog.error("Failed to initialize RouteController (%s)", strerror(-ret));
  252. }
  253. gLog.info("Initializing RouteController: %.1fms", s.getTimeAndReset());
  254. netdutils::Status xStatus = XfrmController::Init();
  255. if (!isOk(xStatus)) {
  256. gLog.error("Failed to initialize XfrmController (%s)", netdutils::toString(xStatus).c_str());
  257. };
  258. gLog.info("Initializing XfrmController: %.1fms", s.getTimeAndReset());
  259. }
  260. Controllers* gCtls = nullptr;
  261. } // namespace net
  262. } // namespace android