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