blob: a25e05a6d5329bd2486dc3587c614b77d25a355d [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 Colitti05306fb2017-02-09 04:56:00 +090017#include <android-base/stringprintf.h>
18
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +090019#define LOG_TAG "Netd"
20#include <cutils/log.h>
21
Pierre Imai1cfa5432016-02-24 18:00:03 +090022#include "Controllers.h"
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090023#include "IdletimerController.h"
24#include "NetworkController.h"
25#include "RouteController.h"
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +090026#include "Stopwatch.h"
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090027#include "oem_iptables_hook.h"
Pierre Imai1cfa5432016-02-24 18:00:03 +090028
29namespace android {
30namespace net {
31
Lorenzo Colitti341d3a02017-08-08 17:31:35 +090032auto Controllers::execIptablesRestore = ::execIptablesRestore;
33auto Controllers::execIptablesSilently = ::execIptablesSilently;
34
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090035namespace {
36/**
37 * List of module chains to be created, along with explicit ordering. ORDERING
38 * IS CRITICAL, AND SHOULD BE TRIPLE-CHECKED WITH EACH CHANGE.
39 */
40static const char* FILTER_INPUT[] = {
41 // Bandwidth should always be early in input chain, to make sure we
42 // correctly count incoming traffic against data plan.
43 BandwidthController::LOCAL_INPUT,
44 FirewallController::LOCAL_INPUT,
45 NULL,
46};
47
48static const char* FILTER_FORWARD[] = {
49 OEM_IPTABLES_FILTER_FORWARD,
50 FirewallController::LOCAL_FORWARD,
51 BandwidthController::LOCAL_FORWARD,
52 NatController::LOCAL_FORWARD,
53 NULL,
54};
55
56static const char* FILTER_OUTPUT[] = {
57 OEM_IPTABLES_FILTER_OUTPUT,
58 FirewallController::LOCAL_OUTPUT,
59 StrictController::LOCAL_OUTPUT,
60 BandwidthController::LOCAL_OUTPUT,
61 NULL,
62};
63
64static const char* RAW_PREROUTING[] = {
65 BandwidthController::LOCAL_RAW_PREROUTING,
66 IdletimerController::LOCAL_RAW_PREROUTING,
67 NatController::LOCAL_RAW_PREROUTING,
68 NULL,
69};
70
71static const char* MANGLE_POSTROUTING[] = {
Lorenzo Colitti05306fb2017-02-09 04:56:00 +090072 OEM_IPTABLES_MANGLE_POSTROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090073 BandwidthController::LOCAL_MANGLE_POSTROUTING,
74 IdletimerController::LOCAL_MANGLE_POSTROUTING,
75 NULL,
76};
77
Lorenzo Colittid78843e2017-03-27 05:52:31 +090078static const char* MANGLE_INPUT[] = {
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090079 WakeupController::LOCAL_MANGLE_INPUT,
Lorenzo Colittid78843e2017-03-27 05:52:31 +090080 RouteController::LOCAL_MANGLE_INPUT,
81 NULL,
82};
83
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090084static const char* MANGLE_FORWARD[] = {
85 NatController::LOCAL_MANGLE_FORWARD,
86 NULL,
87};
88
89static const char* NAT_PREROUTING[] = {
90 OEM_IPTABLES_NAT_PREROUTING,
91 NULL,
92};
93
94static const char* NAT_POSTROUTING[] = {
95 NatController::LOCAL_NAT_POSTROUTING,
96 NULL,
97};
98
Lorenzo Colitti341d3a02017-08-08 17:31:35 +090099} // namespace
100
101/* static */
102void Controllers::createChildChains(IptablesTarget target, const char* table,
103 const char* parentChain,
104 const char** childChains,
105 bool exclusive) {
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900106 std::string command = android::base::StringPrintf("*%s\n", table);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900107
108 // If we're the exclusive owner of this chain, clear it entirely. This saves us from having to
109 // run one execIptablesSilently command to delete each child chain. We can't use -D in
110 // iptables-restore because it's a fatal error if the rule doesn't exist.
111 // TODO: Make all chains exclusive once vendor code uses the oem_* rules.
112 if (exclusive) {
113 // Just running ":chain -" flushes user-defined chains, but not built-in chains like INPUT.
114 // Since at this point we don't know if parentChain is a built-in chain, do both.
115 command += android::base::StringPrintf(":%s -\n", parentChain);
116 command += android::base::StringPrintf("-F %s\n", parentChain);
117 }
118
119 const char** childChain = childChains;
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900120 do {
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900121 if (!exclusive) {
122 execIptablesSilently(target, "-t", table, "-D", parentChain, "-j", *childChain, NULL);
123 }
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900124 command += android::base::StringPrintf(":%s -\n", *childChain);
125 command += android::base::StringPrintf("-A %s -j %s\n", parentChain, *childChain);
126 } while (*(++childChain) != NULL);
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900127 command += "COMMIT\n";
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900128 execIptablesRestore(target, command);
129}
130
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900131Controllers::Controllers()
132 : clatdCtrl(&netCtrl),
133 wakeupCtrl(
134 [this](const std::string& prefix, uid_t uid, gid_t gid, uint64_t timestampNs) {
135 const auto listener = eventReporter.getNetdEventListener();
136 if (listener == nullptr) {
137 ALOGE("getNetdEventListener() returned nullptr. dropping wakeup event");
138 return;
139 }
140 listener->onWakeupEvent(String16(prefix.c_str()), uid, gid, timestampNs);
141 },
142 &iptablesRestoreCtrl) {
Erik Kline2c5aaa12016-06-08 13:24:45 +0900143 InterfaceController::initializeAll();
144}
Pierre Imai1cfa5432016-02-24 18:00:03 +0900145
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900146void Controllers::initChildChains() {
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900147 /*
148 * This is the only time we touch top-level chains in iptables; controllers
149 * should only mutate rules inside of their children chains, as created by
150 * the constants above.
151 *
152 * Modules should never ACCEPT packets (except in well-justified cases);
153 * they should instead defer to any remaining modules using RETURN, or
154 * otherwise DROP/REJECT.
155 */
156
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900157 // Create chains for child modules.
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900158 createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true);
159 createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900160 createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900161 createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD, true);
Lorenzo Colittid78843e2017-03-27 05:52:31 +0900162 createChildChains(V4V6, "mangle", "INPUT", MANGLE_INPUT, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900163 createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING, true);
164 createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING, true);
Lorenzo Colitti341d3a02017-08-08 17:31:35 +0900165
166 // We cannot use createChildChainsFast for all chains because vendor code modifies filter OUTPUT
167 // and mangle POSTROUTING directly.
168 createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT, false);
169 createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
170}
171
172void Controllers::initIptablesRules() {
173 Stopwatch s;
174 initChildChains();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900175 ALOGI("Creating child chains: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900176
177 // Let each module setup their child chains
178 setupOemIptablesHook();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900179 ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900180
181 /* When enabled, DROPs all packets except those matching rules. */
182 firewallCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900183 ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900184
185 /* Does DROPs in FORWARD by default */
186 natCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900187 ALOGI("Setting up NatController hooks: %.1fms", s.getTimeAndReset());
188
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900189 /*
190 * Does REJECT in INPUT, OUTPUT. Does counting also.
191 * No DROP/REJECT allowed later in netfilter-flow hook order.
192 */
193 bandwidthCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900194 ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset());
195
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900196 /*
197 * Counts in nat: PREROUTING, POSTROUTING.
198 * No DROP/REJECT allowed later in netfilter-flow hook order.
199 */
200 idletimerCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900201 ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900202}
203
204void Controllers::init() {
205 initIptablesRules();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900206
207 Stopwatch s;
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900208 bandwidthCtrl.enableBandwidthControl(false);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900209 ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900210
211 if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) {
212 ALOGE("failed to initialize RouteController (%s)", strerror(-ret));
213 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900214 ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900215}
216
Pierre Imai1cfa5432016-02-24 18:00:03 +0900217Controllers* gCtls = nullptr;
218
219} // namespace net
220} // namespace android