blob: 4231a531069949cf7bdd1c12ec385baabd1c9414 [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(
191 [this](const std::string& prefix, uid_t uid, gid_t gid, uint64_t timestampNs) {
192 const auto listener = eventReporter.getNetdEventListener();
193 if (listener == nullptr) {
194 ALOGE("getNetdEventListener() returned nullptr. dropping wakeup event");
195 return;
196 }
197 listener->onWakeupEvent(String16(prefix.c_str()), uid, gid, timestampNs);
198 },
199 &iptablesRestoreCtrl) {
Erik Kline2c5aaa12016-06-08 13:24:45 +0900200 InterfaceController::initializeAll();
201}
Pierre Imai1cfa5432016-02-24 18:00:03 +0900202
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900203void Controllers::initChildChains() {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900204 /*
205 * This is the only time we touch top-level chains in iptables; controllers
206 * should only mutate rules inside of their children chains, as created by
207 * the constants above.
208 *
209 * Modules should never ACCEPT packets (except in well-justified cases);
210 * they should instead defer to any remaining modules using RETURN, or
211 * otherwise DROP/REJECT.
212 */
213
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900214 // Create chains for child modules.
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900215 createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true);
216 createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900217 createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900218 createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD, true);
Lorenzo Colittid78843e2017-03-27 05:52:31 +0900219 createChildChains(V4V6, "mangle", "INPUT", MANGLE_INPUT, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900220 createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING, true);
221 createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING, true);
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900222
Lorenzo Colittiadc3a5f2017-08-08 17:23:12 +0900223 createChildChains(V4, "filter", "OUTPUT", FILTER_OUTPUT, false);
224 createChildChains(V6, "filter", "OUTPUT", FILTER_OUTPUT, false);
225 createChildChains(V4, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
226 createChildChains(V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900227}
228
229void Controllers::initIptablesRules() {
230 Stopwatch s;
231 initChildChains();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900232 ALOGI("Creating child chains: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900233
234 // Let each module setup their child chains
235 setupOemIptablesHook();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900236 ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900237
238 /* When enabled, DROPs all packets except those matching rules. */
239 firewallCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900240 ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900241
242 /* Does DROPs in FORWARD by default */
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900243 tetherCtrl.setupIptablesHooks();
244 ALOGI("Setting up TetherController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900245
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900246 /*
247 * Does REJECT in INPUT, OUTPUT. Does counting also.
248 * No DROP/REJECT allowed later in netfilter-flow hook order.
249 */
250 bandwidthCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900251 ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset());
252
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900253 /*
254 * Counts in nat: PREROUTING, POSTROUTING.
255 * No DROP/REJECT allowed later in netfilter-flow hook order.
256 */
257 idletimerCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900258 ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900259}
260
261void Controllers::init() {
262 initIptablesRules();
Chenbo Fengf2759682017-10-10 17:31:57 -0700263 int ret = trafficCtrl.start();
264 if (ret) {
265 ALOGE("failed to start trafficcontroller: (%s)", strerror(-ret));
266 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900267 Stopwatch s;
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900268 bandwidthCtrl.enableBandwidthControl(false);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900269 ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900270
Chenbo Fengf2759682017-10-10 17:31:57 -0700271 ret = RouteController::Init(NetworkController::LOCAL_NET_ID);
272 if (ret) {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900273 ALOGE("failed to initialize RouteController (%s)", strerror(-ret));
274 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900275 ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900276}
277
Pierre Imai1cfa5432016-02-24 18:00:03 +0900278Controllers* gCtls = nullptr;
279
280} // namespace net
281} // namespace android