blob: 8b02f6095ef7abe2e84c5875562a2348fb93af39 [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"
25#include <cutils/log.h>
26
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"
Pierre Imai1cfa5432016-02-24 18:00:03 +090033
34namespace android {
35namespace net {
36
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090037using android::base::Join;
38using android::base::StringPrintf;
39using android::base::StringAppendF;
40
Lorenzo Colitti341d3a02017-08-08 17:31:35 +090041auto Controllers::execIptablesRestore = ::execIptablesRestore;
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090042auto Controllers::execIptablesRestoreWithOutput = ::execIptablesRestoreWithOutput;
Lorenzo Colitti341d3a02017-08-08 17:31:35 +090043
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090044namespace {
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090045
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090046/**
47 * List of module chains to be created, along with explicit ordering. ORDERING
48 * IS CRITICAL, AND SHOULD BE TRIPLE-CHECKED WITH EACH CHANGE.
49 */
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090050static const std::vector<const char*> FILTER_INPUT = {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090051 // Bandwidth should always be early in input chain, to make sure we
52 // correctly count incoming traffic against data plan.
53 BandwidthController::LOCAL_INPUT,
54 FirewallController::LOCAL_INPUT,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090055};
56
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090057static const std::vector<const char*> FILTER_FORWARD = {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090058 OEM_IPTABLES_FILTER_FORWARD,
59 FirewallController::LOCAL_FORWARD,
60 BandwidthController::LOCAL_FORWARD,
Lorenzo Colittia93126d2017-08-24 13:28:19 +090061 TetherController::LOCAL_FORWARD,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090062};
63
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090064static const std::vector<const char*> FILTER_OUTPUT = {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090065 OEM_IPTABLES_FILTER_OUTPUT,
66 FirewallController::LOCAL_OUTPUT,
67 StrictController::LOCAL_OUTPUT,
68 BandwidthController::LOCAL_OUTPUT,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090069};
70
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090071static const std::vector<const char*> RAW_PREROUTING = {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090072 BandwidthController::LOCAL_RAW_PREROUTING,
73 IdletimerController::LOCAL_RAW_PREROUTING,
Lorenzo Colittia93126d2017-08-24 13:28:19 +090074 TetherController::LOCAL_RAW_PREROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090075};
76
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090077static const std::vector<const char*> MANGLE_POSTROUTING = {
Lorenzo Colitti05306fb2017-02-09 04:56:00 +090078 OEM_IPTABLES_MANGLE_POSTROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090079 BandwidthController::LOCAL_MANGLE_POSTROUTING,
80 IdletimerController::LOCAL_MANGLE_POSTROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090081};
82
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090083static const std::vector<const char*> MANGLE_INPUT = {
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090084 WakeupController::LOCAL_MANGLE_INPUT,
Lorenzo Colittid78843e2017-03-27 05:52:31 +090085 RouteController::LOCAL_MANGLE_INPUT,
Lorenzo Colittid78843e2017-03-27 05:52:31 +090086};
87
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090088static const std::vector<const char*> MANGLE_FORWARD = {
Lorenzo Colittia93126d2017-08-24 13:28:19 +090089 TetherController::LOCAL_MANGLE_FORWARD,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090090};
91
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090092static const std::vector<const char*> NAT_PREROUTING = {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090093 OEM_IPTABLES_NAT_PREROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090094};
95
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +090096static const std::vector<const char*> NAT_POSTROUTING = {
Lorenzo Colittia93126d2017-08-24 13:28:19 +090097 TetherController::LOCAL_NAT_POSTROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090098};
99
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900100// Commands to create child chains and to match created chains in iptables -S output. Keep in sync.
101static const char* CHILD_CHAIN_TEMPLATE = "-A %s -j %s\n";
102static const std::regex CHILD_CHAIN_REGEX("^-A ([^ ]+) -j ([^ ]+)$",
103 std::regex_constants::extended);
104
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900105} // namespace
106
107/* static */
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900108std::set<std::string> Controllers::findExistingChildChains(const IptablesTarget target,
109 const char* table,
110 const char* parentChain) {
111 if (target == V4V6) {
112 ALOGE("findExistingChildChains only supports one protocol at a time");
113 abort();
114 }
115
116 std::set<std::string> existing;
117
118 // List the current contents of parentChain.
119 //
120 // TODO: there is no guarantee that nothing else modifies the chain in the few milliseconds
121 // between when we list the existing rules and when we delete them. However:
122 // - Since this code is only run on startup, nothing else in netd will be running.
123 // - While vendor code is known to add its own rules to chains created by netd, it should never
124 // be modifying the rules in childChains or the rules that hook said chains into their parent
125 // chains.
126 std::string command = StringPrintf("*%s\n-S %s\nCOMMIT\n", table, parentChain);
127 std::string output;
128 if (Controllers::execIptablesRestoreWithOutput(target, command, &output) == -1) {
129 ALOGE("Error listing chain %s in table %s\n", parentChain, table);
130 return existing;
131 }
132
133 // The only rules added by createChildChains are of the simple form "-A <parent> -j <child>".
134 // Find those rules and add each one's child chain to existing.
135 std::smatch matches;
136 std::stringstream stream(output);
137 std::string rule;
138 while (std::getline(stream, rule, '\n')) {
139 if (std::regex_search(rule, matches, CHILD_CHAIN_REGEX) && matches[1] == parentChain) {
140 existing.insert(matches[2]);
141 }
142 }
143
144 return existing;
145}
146
147/* static */
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900148void Controllers::createChildChains(IptablesTarget target, const char* table,
149 const char* parentChain,
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900150 const std::vector<const char*>& childChains,
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900151 bool exclusive) {
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900152 std::string command = StringPrintf("*%s\n", table);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900153
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900154 // We cannot just clear all the chains we create because vendor code modifies filter OUTPUT and
155 // mangle POSTROUTING directly. So:
156 //
157 // - If we're the exclusive owner of this chain, simply clear it entirely.
158 // - If not, then list the chain's current contents to ensure that if we restart after a crash,
159 // we leave the existing rules alone in the positions they currently occupy. This is faster
160 // than blindly deleting our rules and recreating them, because deleting a rule that doesn't
161 // exists causes iptables-restore to quit, which takes ~30ms per delete. It's also more
162 // correct, because if we delete rules and re-add them, they'll be in the wrong position with
163 // regards to the vendor rules.
164 //
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900165 // TODO: Make all chains exclusive once vendor code uses the oem_* rules.
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900166 std::set<std::string> existingChildChains;
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900167 if (exclusive) {
168 // Just running ":chain -" flushes user-defined chains, but not built-in chains like INPUT.
169 // Since at this point we don't know if parentChain is a built-in chain, do both.
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900170 StringAppendF(&command, ":%s -\n", parentChain);
171 StringAppendF(&command, "-F %s\n", parentChain);
172 } else {
173 existingChildChains = findExistingChildChains(target, table, parentChain);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900174 }
175
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900176 for (const auto& childChain : childChains) {
177 // Always clear the child chain.
178 StringAppendF(&command, ":%s -\n", childChain);
179 // But only add it to the parent chain if it's not already there.
180 if (existingChildChains.find(childChain) == existingChildChains.end()) {
181 StringAppendF(&command, CHILD_CHAIN_TEMPLATE, parentChain, childChain);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900182 }
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900183 }
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900184 command += "COMMIT\n";
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900185 execIptablesRestore(target, command);
186}
187
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900188Controllers::Controllers()
189 : clatdCtrl(&netCtrl),
190 wakeupCtrl(
Hugo Benichie874b7f2017-10-12 21:34:46 +0900191 [this](const WakeupController::ReportArgs& args) {
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900192 const auto listener = eventReporter.getNetdEventListener();
193 if (listener == nullptr) {
194 ALOGE("getNetdEventListener() returned nullptr. dropping wakeup event");
195 return;
196 }
Hugo Benichie874b7f2017-10-12 21:34:46 +0900197 String16 prefix = String16(args.prefix.c_str());
198 String16 srcIp = String16(args.srcIp.c_str());
199 String16 dstIp = String16(args.dstIp.c_str());
200 listener->onWakeupEvent(prefix, args.uid, args.ethertype, args.ipNextHeader,
201 args.dstHw, srcIp, dstIp, args.srcPort, args.dstPort,
202 args.timestampNs);
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900203 },
204 &iptablesRestoreCtrl) {
Erik Kline2c5aaa12016-06-08 13:24:45 +0900205 InterfaceController::initializeAll();
206}
Pierre Imai1cfa5432016-02-24 18:00:03 +0900207
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900208void Controllers::initChildChains() {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900209 /*
210 * This is the only time we touch top-level chains in iptables; controllers
211 * should only mutate rules inside of their children chains, as created by
212 * the constants above.
213 *
214 * Modules should never ACCEPT packets (except in well-justified cases);
215 * they should instead defer to any remaining modules using RETURN, or
216 * otherwise DROP/REJECT.
217 */
218
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900219 // Create chains for child modules.
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900220 createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true);
221 createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900222 createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900223 createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD, true);
Lorenzo Colittid78843e2017-03-27 05:52:31 +0900224 createChildChains(V4V6, "mangle", "INPUT", MANGLE_INPUT, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900225 createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING, true);
226 createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING, true);
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900227
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900228 createChildChains(V4, "filter", "OUTPUT", FILTER_OUTPUT, false);
229 createChildChains(V6, "filter", "OUTPUT", FILTER_OUTPUT, false);
230 createChildChains(V4, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
231 createChildChains(V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900232}
233
234void Controllers::initIptablesRules() {
235 Stopwatch s;
236 initChildChains();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900237 ALOGI("Creating child chains: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900238
239 // Let each module setup their child chains
240 setupOemIptablesHook();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900241 ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900242
243 /* When enabled, DROPs all packets except those matching rules. */
244 firewallCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900245 ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900246
247 /* Does DROPs in FORWARD by default */
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900248 tetherCtrl.setupIptablesHooks();
249 ALOGI("Setting up TetherController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900250
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900251 /*
252 * Does REJECT in INPUT, OUTPUT. Does counting also.
253 * No DROP/REJECT allowed later in netfilter-flow hook order.
254 */
255 bandwidthCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900256 ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset());
257
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900258 /*
259 * Counts in nat: PREROUTING, POSTROUTING.
260 * No DROP/REJECT allowed later in netfilter-flow hook order.
261 */
262 idletimerCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900263 ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900264}
265
266void Controllers::init() {
267 initIptablesRules();
Chenbo Fengf2759682017-10-10 17:31:57 -0700268 int ret = trafficCtrl.start();
269 if (ret) {
270 ALOGE("failed to start trafficcontroller: (%s)", strerror(-ret));
271 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900272 Stopwatch s;
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900273 bandwidthCtrl.enableBandwidthControl(false);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900274 ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900275
Chenbo Fengf2759682017-10-10 17:31:57 -0700276 ret = RouteController::Init(NetworkController::LOCAL_NET_ID);
277 if (ret) {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900278 ALOGE("failed to initialize RouteController (%s)", strerror(-ret));
279 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900280 ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900281}
282
Pierre Imai1cfa5432016-02-24 18:00:03 +0900283Controllers* gCtls = nullptr;
284
285} // namespace net
286} // namespace android