blob: 36181d416585369bbf4882b2a139cfc834319992 [file] [log] [blame]
Pierre Imai1cfa5432016-02-24 18:00:03 +09001/*
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
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090017#include <regex>
18#include <set>
19#include <string>
20
21#include <android-base/strings.h>
Lorenzo Colitti05306fb2017-02-09 04:56:00 +090022#include <android-base/stringprintf.h>
23
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +090024#define LOG_TAG "Netd"
Logan Chien3f461482018-04-23 14:31:32 +080025#include <log/log.h>
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +090026
Pierre Imai1cfa5432016-02-24 18:00:03 +090027#include "Controllers.h"
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090028#include "IdletimerController.h"
29#include "NetworkController.h"
30#include "RouteController.h"
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +090031#include "Stopwatch.h"
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090032#include "oem_iptables_hook.h"
Nathan Harold21299f72018-03-16 20:13:03 -070033#include "XfrmController.h"
Pierre Imai1cfa5432016-02-24 18:00:03 +090034
35namespace android {
36namespace net {
37
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090038using android::base::Join;
39using android::base::StringPrintf;
40using android::base::StringAppendF;
41
Lorenzo Colitti341d3a02017-08-08 17:31:35 +090042auto Controllers::execIptablesRestore = ::execIptablesRestore;
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090043auto Controllers::execIptablesRestoreWithOutput = ::execIptablesRestoreWithOutput;
Lorenzo Colitti341d3a02017-08-08 17:31:35 +090044
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090045namespace {
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090046
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090047/**
48 * List of module chains to be created, along with explicit ordering. ORDERING
49 * IS CRITICAL, AND SHOULD BE TRIPLE-CHECKED WITH EACH CHANGE.
50 */
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090051static const std::vector<const char*> FILTER_INPUT = {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090052 // Bandwidth should always be early in input chain, to make sure we
53 // correctly count incoming traffic against data plan.
54 BandwidthController::LOCAL_INPUT,
55 FirewallController::LOCAL_INPUT,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090056};
57
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090058static const std::vector<const char*> FILTER_FORWARD = {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090059 OEM_IPTABLES_FILTER_FORWARD,
60 FirewallController::LOCAL_FORWARD,
61 BandwidthController::LOCAL_FORWARD,
Lorenzo Colittia93126d2017-08-24 13:28:19 +090062 TetherController::LOCAL_FORWARD,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090063};
64
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090065static const std::vector<const char*> FILTER_OUTPUT = {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090066 OEM_IPTABLES_FILTER_OUTPUT,
67 FirewallController::LOCAL_OUTPUT,
68 StrictController::LOCAL_OUTPUT,
69 BandwidthController::LOCAL_OUTPUT,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090070};
71
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090072static const std::vector<const char*> RAW_PREROUTING = {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090073 BandwidthController::LOCAL_RAW_PREROUTING,
74 IdletimerController::LOCAL_RAW_PREROUTING,
Lorenzo Colittia93126d2017-08-24 13:28:19 +090075 TetherController::LOCAL_RAW_PREROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090076};
77
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090078static const std::vector<const char*> MANGLE_POSTROUTING = {
Lorenzo Colitti05306fb2017-02-09 04:56:00 +090079 OEM_IPTABLES_MANGLE_POSTROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090080 BandwidthController::LOCAL_MANGLE_POSTROUTING,
81 IdletimerController::LOCAL_MANGLE_POSTROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090082};
83
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090084static const std::vector<const char*> MANGLE_INPUT = {
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090085 WakeupController::LOCAL_MANGLE_INPUT,
Lorenzo Colittid78843e2017-03-27 05:52:31 +090086 RouteController::LOCAL_MANGLE_INPUT,
Lorenzo Colittid78843e2017-03-27 05:52:31 +090087};
88
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090089static const std::vector<const char*> MANGLE_FORWARD = {
Lorenzo Colittia93126d2017-08-24 13:28:19 +090090 TetherController::LOCAL_MANGLE_FORWARD,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090091};
92
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090093static const std::vector<const char*> NAT_PREROUTING = {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090094 OEM_IPTABLES_NAT_PREROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090095};
96
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090097static const std::vector<const char*> NAT_POSTROUTING = {
Lorenzo Colittia93126d2017-08-24 13:28:19 +090098 TetherController::LOCAL_NAT_POSTROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090099};
100
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900101// Commands to create child chains and to match created chains in iptables -S output. Keep in sync.
102static const char* CHILD_CHAIN_TEMPLATE = "-A %s -j %s\n";
103static const std::regex CHILD_CHAIN_REGEX("^-A ([^ ]+) -j ([^ ]+)$",
104 std::regex_constants::extended);
105
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900106} // namespace
107
108/* static */
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900109std::set<std::string> Controllers::findExistingChildChains(const IptablesTarget target,
110 const char* table,
111 const char* parentChain) {
112 if (target == V4V6) {
113 ALOGE("findExistingChildChains only supports one protocol at a time");
114 abort();
115 }
116
117 std::set<std::string> existing;
118
119 // List the current contents of parentChain.
120 //
121 // TODO: there is no guarantee that nothing else modifies the chain in the few milliseconds
122 // between when we list the existing rules and when we delete them. However:
123 // - Since this code is only run on startup, nothing else in netd will be running.
124 // - While vendor code is known to add its own rules to chains created by netd, it should never
125 // be modifying the rules in childChains or the rules that hook said chains into their parent
126 // chains.
127 std::string command = StringPrintf("*%s\n-S %s\nCOMMIT\n", table, parentChain);
128 std::string output;
129 if (Controllers::execIptablesRestoreWithOutput(target, command, &output) == -1) {
130 ALOGE("Error listing chain %s in table %s\n", parentChain, table);
131 return existing;
132 }
133
134 // The only rules added by createChildChains are of the simple form "-A <parent> -j <child>".
135 // Find those rules and add each one's child chain to existing.
136 std::smatch matches;
137 std::stringstream stream(output);
138 std::string rule;
139 while (std::getline(stream, rule, '\n')) {
140 if (std::regex_search(rule, matches, CHILD_CHAIN_REGEX) && matches[1] == parentChain) {
141 existing.insert(matches[2]);
142 }
143 }
144
145 return existing;
146}
147
148/* static */
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900149void Controllers::createChildChains(IptablesTarget target, const char* table,
150 const char* parentChain,
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900151 const std::vector<const char*>& childChains,
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900152 bool exclusive) {
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900153 std::string command = StringPrintf("*%s\n", table);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900154
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900155 // We cannot just clear all the chains we create because vendor code modifies filter OUTPUT and
156 // mangle POSTROUTING directly. So:
157 //
158 // - If we're the exclusive owner of this chain, simply clear it entirely.
159 // - If not, then list the chain's current contents to ensure that if we restart after a crash,
160 // we leave the existing rules alone in the positions they currently occupy. This is faster
161 // than blindly deleting our rules and recreating them, because deleting a rule that doesn't
162 // exists causes iptables-restore to quit, which takes ~30ms per delete. It's also more
163 // correct, because if we delete rules and re-add them, they'll be in the wrong position with
164 // regards to the vendor rules.
165 //
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900166 // TODO: Make all chains exclusive once vendor code uses the oem_* rules.
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900167 std::set<std::string> existingChildChains;
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900168 if (exclusive) {
169 // Just running ":chain -" flushes user-defined chains, but not built-in chains like INPUT.
170 // Since at this point we don't know if parentChain is a built-in chain, do both.
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900171 StringAppendF(&command, ":%s -\n", parentChain);
172 StringAppendF(&command, "-F %s\n", parentChain);
173 } else {
174 existingChildChains = findExistingChildChains(target, table, parentChain);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900175 }
176
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900177 for (const auto& childChain : childChains) {
178 // Always clear the child chain.
179 StringAppendF(&command, ":%s -\n", childChain);
180 // But only add it to the parent chain if it's not already there.
181 if (existingChildChains.find(childChain) == existingChildChains.end()) {
182 StringAppendF(&command, CHILD_CHAIN_TEMPLATE, parentChain, childChain);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900183 }
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900184 }
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900185 command += "COMMIT\n";
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900186 execIptablesRestore(target, command);
187}
188
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900189Controllers::Controllers()
190 : clatdCtrl(&netCtrl),
191 wakeupCtrl(
Hugo Benichie874b7f2017-10-12 21:34:46 +0900192 [this](const WakeupController::ReportArgs& args) {
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900193 const auto listener = eventReporter.getNetdEventListener();
194 if (listener == nullptr) {
195 ALOGE("getNetdEventListener() returned nullptr. dropping wakeup event");
196 return;
197 }
Hugo Benichie874b7f2017-10-12 21:34:46 +0900198 String16 prefix = String16(args.prefix.c_str());
199 String16 srcIp = String16(args.srcIp.c_str());
200 String16 dstIp = String16(args.dstIp.c_str());
201 listener->onWakeupEvent(prefix, args.uid, args.ethertype, args.ipNextHeader,
202 args.dstHw, srcIp, dstIp, args.srcPort, args.dstPort,
203 args.timestampNs);
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900204 },
205 &iptablesRestoreCtrl) {
Erik Kline2c5aaa12016-06-08 13:24:45 +0900206 InterfaceController::initializeAll();
207}
Pierre Imai1cfa5432016-02-24 18:00:03 +0900208
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900209void Controllers::initChildChains() {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900210 /*
211 * This is the only time we touch top-level chains in iptables; controllers
212 * should only mutate rules inside of their children chains, as created by
213 * the constants above.
214 *
215 * Modules should never ACCEPT packets (except in well-justified cases);
216 * they should instead defer to any remaining modules using RETURN, or
217 * otherwise DROP/REJECT.
218 */
219
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900220 // Create chains for child modules.
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900221 createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true);
222 createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900223 createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900224 createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD, true);
Lorenzo Colittid78843e2017-03-27 05:52:31 +0900225 createChildChains(V4V6, "mangle", "INPUT", MANGLE_INPUT, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900226 createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING, true);
227 createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING, true);
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900228
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900229 createChildChains(V4, "filter", "OUTPUT", FILTER_OUTPUT, false);
230 createChildChains(V6, "filter", "OUTPUT", FILTER_OUTPUT, false);
231 createChildChains(V4, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
232 createChildChains(V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900233}
234
235void Controllers::initIptablesRules() {
236 Stopwatch s;
237 initChildChains();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900238 ALOGI("Creating child chains: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900239
240 // Let each module setup their child chains
241 setupOemIptablesHook();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900242 ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900243
244 /* When enabled, DROPs all packets except those matching rules. */
245 firewallCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900246 ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900247
248 /* Does DROPs in FORWARD by default */
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900249 tetherCtrl.setupIptablesHooks();
250 ALOGI("Setting up TetherController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900251
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900252 /*
253 * Does REJECT in INPUT, OUTPUT. Does counting also.
254 * No DROP/REJECT allowed later in netfilter-flow hook order.
255 */
256 bandwidthCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900257 ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset());
258
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900259 /*
260 * Counts in nat: PREROUTING, POSTROUTING.
261 * No DROP/REJECT allowed later in netfilter-flow hook order.
262 */
263 idletimerCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900264 ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900265}
266
267void Controllers::init() {
268 initIptablesRules();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900269 Stopwatch s;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800270 netdutils::Status tcStatus = trafficCtrl.start();
271 if (!isOk(tcStatus)) {
272 ALOGE("failed to start trafficcontroller: (%s)", toString(tcStatus).c_str());
273 }
274 ALOGI("initializing traffic control: %.1fms", s.getTimeAndReset());
275
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900276 bandwidthCtrl.enableBandwidthControl(false);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900277 ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900278
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800279 if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900280 ALOGE("failed to initialize RouteController (%s)", strerror(-ret));
281 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900282 ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset());
Nathan Harold21299f72018-03-16 20:13:03 -0700283
284 netdutils::Status xStatus = XfrmController::Init();
285 if (!isOk(xStatus)) {
286 ALOGE("Failed to initialize XfrmController (%s)", netdutils::toString(xStatus).c_str());
287 };
288 ALOGI("Initializing XfrmController: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900289}
290
Pierre Imai1cfa5432016-02-24 18:00:03 +0900291Controllers* gCtls = nullptr;
292
293} // namespace net
294} // namespace android