blob: 84f719b4934b08a5f50251b02e5e57469aca2c9b [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[] = {
76 RouteController::LOCAL_MANGLE_INPUT,
77 NULL,
78};
79
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090080static const char* MANGLE_FORWARD[] = {
81 NatController::LOCAL_MANGLE_FORWARD,
82 NULL,
83};
84
85static const char* NAT_PREROUTING[] = {
86 OEM_IPTABLES_NAT_PREROUTING,
87 NULL,
88};
89
90static const char* NAT_POSTROUTING[] = {
91 NatController::LOCAL_NAT_POSTROUTING,
92 NULL,
93};
94
95static void createChildChains(IptablesTarget target, const char* table, const char* parentChain,
Lorenzo Colitticda022e2017-02-03 12:35:46 +090096 const char** childChains, bool exclusive) {
Lorenzo Colitti05306fb2017-02-09 04:56:00 +090097 std::string command = android::base::StringPrintf("*%s\n", table);
Lorenzo Colitticda022e2017-02-03 12:35:46 +090098
99 // If we're the exclusive owner of this chain, clear it entirely. This saves us from having to
100 // run one execIptablesSilently command to delete each child chain. We can't use -D in
101 // iptables-restore because it's a fatal error if the rule doesn't exist.
102 // TODO: Make all chains exclusive once vendor code uses the oem_* rules.
103 if (exclusive) {
104 // Just running ":chain -" flushes user-defined chains, but not built-in chains like INPUT.
105 // Since at this point we don't know if parentChain is a built-in chain, do both.
106 command += android::base::StringPrintf(":%s -\n", parentChain);
107 command += android::base::StringPrintf("-F %s\n", parentChain);
108 }
109
110 const char** childChain = childChains;
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900111 do {
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900112 if (!exclusive) {
113 execIptablesSilently(target, "-t", table, "-D", parentChain, "-j", *childChain, NULL);
114 }
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900115 command += android::base::StringPrintf(":%s -\n", *childChain);
116 command += android::base::StringPrintf("-A %s -j %s\n", parentChain, *childChain);
117 } while (*(++childChain) != NULL);
118 command += "COMMIT\n\n";
119 execIptablesRestore(target, command);
120}
121
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900122} // namespace
123
Erik Kline2c5aaa12016-06-08 13:24:45 +0900124Controllers::Controllers() : clatdCtrl(&netCtrl) {
125 InterfaceController::initializeAll();
126}
Pierre Imai1cfa5432016-02-24 18:00:03 +0900127
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900128void Controllers::initIptablesRules() {
129 /*
130 * This is the only time we touch top-level chains in iptables; controllers
131 * should only mutate rules inside of their children chains, as created by
132 * the constants above.
133 *
134 * Modules should never ACCEPT packets (except in well-justified cases);
135 * they should instead defer to any remaining modules using RETURN, or
136 * otherwise DROP/REJECT.
137 */
138
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900139 // Create chains for child modules.
140 // We cannot use createChildChainsFast for all chains because vendor code modifies filter OUTPUT
141 // and mangle POSTROUTING directly.
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900142 Stopwatch s;
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900143 createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true);
144 createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD, true);
145 createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT, false);
146 createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING, true);
147 createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false);
148 createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD, true);
Lorenzo Colittid78843e2017-03-27 05:52:31 +0900149 createChildChains(V4V6, "mangle", "INPUT", MANGLE_INPUT, true);
Lorenzo Colitticda022e2017-02-03 12:35:46 +0900150 createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING, true);
151 createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING, true);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900152 ALOGI("Creating child chains: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900153
154 // Let each module setup their child chains
155 setupOemIptablesHook();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900156 ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900157
158 /* When enabled, DROPs all packets except those matching rules. */
159 firewallCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900160 ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900161
162 /* Does DROPs in FORWARD by default */
163 natCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900164 ALOGI("Setting up NatController hooks: %.1fms", s.getTimeAndReset());
165
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900166 /*
167 * Does REJECT in INPUT, OUTPUT. Does counting also.
168 * No DROP/REJECT allowed later in netfilter-flow hook order.
169 */
170 bandwidthCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900171 ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset());
172
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900173 /*
174 * Counts in nat: PREROUTING, POSTROUTING.
175 * No DROP/REJECT allowed later in netfilter-flow hook order.
176 */
177 idletimerCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900178 ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900179}
180
181void Controllers::init() {
182 initIptablesRules();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900183
184 Stopwatch s;
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900185 bandwidthCtrl.enableBandwidthControl(false);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900186 ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900187
188 if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) {
189 ALOGE("failed to initialize RouteController (%s)", strerror(-ret));
190 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900191 ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900192}
193
Pierre Imai1cfa5432016-02-24 18:00:03 +0900194Controllers* gCtls = nullptr;
195
196} // namespace net
197} // namespace android