blob: ad77ee1878dee40819871edc8f26286a649caabf [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 Colitti1ed96e22017-02-02 12:21:56 +090032namespace {
33/**
34 * List of module chains to be created, along with explicit ordering. ORDERING
35 * IS CRITICAL, AND SHOULD BE TRIPLE-CHECKED WITH EACH CHANGE.
36 */
37static const char* FILTER_INPUT[] = {
38 // Bandwidth should always be early in input chain, to make sure we
39 // correctly count incoming traffic against data plan.
40 BandwidthController::LOCAL_INPUT,
41 FirewallController::LOCAL_INPUT,
42 NULL,
43};
44
45static const char* FILTER_FORWARD[] = {
46 OEM_IPTABLES_FILTER_FORWARD,
47 FirewallController::LOCAL_FORWARD,
48 BandwidthController::LOCAL_FORWARD,
49 NatController::LOCAL_FORWARD,
50 NULL,
51};
52
53static const char* FILTER_OUTPUT[] = {
54 OEM_IPTABLES_FILTER_OUTPUT,
55 FirewallController::LOCAL_OUTPUT,
56 StrictController::LOCAL_OUTPUT,
57 BandwidthController::LOCAL_OUTPUT,
58 NULL,
59};
60
61static const char* RAW_PREROUTING[] = {
62 BandwidthController::LOCAL_RAW_PREROUTING,
63 IdletimerController::LOCAL_RAW_PREROUTING,
64 NatController::LOCAL_RAW_PREROUTING,
65 NULL,
66};
67
68static const char* MANGLE_POSTROUTING[] = {
Lorenzo Colitti05306fb2017-02-09 04:56:00 +090069 OEM_IPTABLES_MANGLE_POSTROUTING,
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090070 BandwidthController::LOCAL_MANGLE_POSTROUTING,
71 IdletimerController::LOCAL_MANGLE_POSTROUTING,
72 NULL,
73};
74
Lorenzo Colittid78843e2017-03-27 05:52:31 +090075static const char* MANGLE_INPUT[] = {
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090076 WakeupController::LOCAL_MANGLE_INPUT,
Lorenzo Colittid78843e2017-03-27 05:52:31 +090077 RouteController::LOCAL_MANGLE_INPUT,
78 NULL,
79};
80
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090081static const char* MANGLE_FORWARD[] = {
82 NatController::LOCAL_MANGLE_FORWARD,
83 NULL,
84};
85
86static const char* NAT_PREROUTING[] = {
87 OEM_IPTABLES_NAT_PREROUTING,
88 NULL,
89};
90
91static const char* NAT_POSTROUTING[] = {
92 NatController::LOCAL_NAT_POSTROUTING,
93 NULL,
94};
95
96static void createChildChains(IptablesTarget target, const char* table, const char* parentChain,
Lorenzo Colitticda022e2017-02-03 12:35:46 +090097 const char** childChains, bool exclusive) {
Lorenzo Colitti05306fb2017-02-09 04:56:00 +090098 std::string command = android::base::StringPrintf("*%s\n", table);
Lorenzo Colitticda022e2017-02-03 12:35:46 +090099
100 // If we're the exclusive owner of this chain, clear it entirely. This saves us from having to
101 // run one execIptablesSilently command to delete each child chain. We can't use -D in
102 // iptables-restore because it's a fatal error if the rule doesn't exist.
103 // TODO: Make all chains exclusive once vendor code uses the oem_* rules.
104 if (exclusive) {
105 // Just running ":chain -" flushes user-defined chains, but not built-in chains like INPUT.
106 // Since at this point we don't know if parentChain is a built-in chain, do both.
107 command += android::base::StringPrintf(":%s -\n", parentChain);
108 command += android::base::StringPrintf("-F %s\n", parentChain);
109 }
110
111 const char** childChain = childChains;
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900112 do {
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900113 if (!exclusive) {
114 execIptablesSilently(target, "-t", table, "-D", parentChain, "-j", *childChain, NULL);
115 }
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900116 command += android::base::StringPrintf(":%s -\n", *childChain);
117 command += android::base::StringPrintf("-A %s -j %s\n", parentChain, *childChain);
118 } while (*(++childChain) != NULL);
119 command += "COMMIT\n\n";
120 execIptablesRestore(target, command);
121}
122
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900123} // namespace
124
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900125Controllers::Controllers()
126 : clatdCtrl(&netCtrl),
127 wakeupCtrl(
128 [this](const std::string& prefix, uid_t uid, gid_t gid, uint64_t timestampNs) {
129 const auto listener = eventReporter.getNetdEventListener();
130 if (listener == nullptr) {
131 ALOGE("getNetdEventListener() returned nullptr. dropping wakeup event");
132 return;
133 }
134 listener->onWakeupEvent(String16(prefix.c_str()), uid, gid, timestampNs);
135 },
136 &iptablesRestoreCtrl) {
Erik Kline2c5aaa12016-06-08 13:24:45 +0900137 InterfaceController::initializeAll();
138}
Pierre Imai1cfa5432016-02-24 18:00:03 +0900139
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900140void Controllers::initIptablesRules() {
141 /*
142 * This is the only time we touch top-level chains in iptables; controllers
143 * should only mutate rules inside of their children chains, as created by
144 * the constants above.
145 *
146 * Modules should never ACCEPT packets (except in well-justified cases);
147 * they should instead defer to any remaining modules using RETURN, or
148 * otherwise DROP/REJECT.
149 */
150
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900151 // Create chains for child modules.
152 // We cannot use createChildChainsFast for all chains because vendor code modifies filter OUTPUT
153 // and mangle POSTROUTING directly.
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900154 Stopwatch s;
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900155 createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true);
156 createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD, true);
157 createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT, false);
158 createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING, true);
159 createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
160 createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD, true);
Lorenzo Colittid78843e2017-03-27 05:52:31 +0900161 createChildChains(V4V6, "mangle", "INPUT", MANGLE_INPUT, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900162 createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING, true);
163 createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING, true);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900164 ALOGI("Creating child chains: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900165
166 // Let each module setup their child chains
167 setupOemIptablesHook();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900168 ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900169
170 /* When enabled, DROPs all packets except those matching rules. */
171 firewallCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900172 ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900173
174 /* Does DROPs in FORWARD by default */
175 natCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900176 ALOGI("Setting up NatController hooks: %.1fms", s.getTimeAndReset());
177
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900178 /*
179 * Does REJECT in INPUT, OUTPUT. Does counting also.
180 * No DROP/REJECT allowed later in netfilter-flow hook order.
181 */
182 bandwidthCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900183 ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset());
184
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900185 /*
186 * Counts in nat: PREROUTING, POSTROUTING.
187 * No DROP/REJECT allowed later in netfilter-flow hook order.
188 */
189 idletimerCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900190 ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900191}
192
193void Controllers::init() {
194 initIptablesRules();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900195
196 Stopwatch s;
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900197 bandwidthCtrl.enableBandwidthControl(false);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900198 ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900199
200 if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) {
201 ALOGE("failed to initialize RouteController (%s)", strerror(-ret));
202 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900203 ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900204}
205
Pierre Imai1cfa5432016-02-24 18:00:03 +0900206Controllers* gCtls = nullptr;
207
208} // namespace net
209} // namespace android