blob: 1a618c119b6d30725ffb946763bdccbd1feeea9a [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 Colitti81ff64e2017-02-02 02:30:13 +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 Colitti81ff64e2017-02-02 02:30:13 +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
75static const char* MANGLE_FORWARD[] = {
76 NatController::LOCAL_MANGLE_FORWARD,
77 NULL,
78};
79
80static const char* NAT_PREROUTING[] = {
81 OEM_IPTABLES_NAT_PREROUTING,
82 NULL,
83};
84
85static const char* NAT_POSTROUTING[] = {
86 NatController::LOCAL_NAT_POSTROUTING,
87 NULL,
88};
89
90static void createChildChains(IptablesTarget target, const char* table, const char* parentChain,
91 const char** childChains) {
92 const char** childChain = childChains;
93 do {
94 // Order is important:
95 // -D to delete any pre-existing jump rule (removes references
96 // that would prevent -X from working)
97 // -F to flush any existing chain
98 // -X to delete any existing chain
99 // -N to create the chain
100 // -A to append the chain to parent
101
102 execIptablesSilently(target, "-t", table, "-D", parentChain, "-j", *childChain, NULL);
103 execIptablesSilently(target, "-t", table, "-F", *childChain, NULL);
104 execIptablesSilently(target, "-t", table, "-X", *childChain, NULL);
105 execIptables(target, "-t", table, "-N", *childChain, NULL);
106 execIptables(target, "-t", table, "-A", parentChain, "-j", *childChain, NULL);
107 } while (*(++childChain) != NULL);
108}
109
Lorenzo Colitti81ff64e2017-02-02 02:30:13 +0900110// Fast version of createChildChains. This is only safe to use if the parent chain contains nothing
111// apart from the specified child chains.
112static void createChildChainsFast(IptablesTarget target, const char* table, const char* parentChain,
113 const char** childChains) {
114 const char** childChain = childChains;
115 std::string command = android::base::StringPrintf("*%s\n", table);
116 command += android::base::StringPrintf(":%s -\n", parentChain);
117 // Just running ":chain -" flushes user-defined chains, but not built-in chains like INPUT.
118 // Since at this point we don't know if parentChain is a built-in chain, do both.
119 command += android::base::StringPrintf("-F %s\n", parentChain);
120 do {
121 command += android::base::StringPrintf(":%s -\n", *childChain);
122 command += android::base::StringPrintf("-A %s -j %s\n", parentChain, *childChain);
123 } while (*(++childChain) != NULL);
124 command += "COMMIT\n\n";
125 execIptablesRestore(target, command);
126}
127
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900128} // namespace
129
Erik Kline2c5aaa12016-06-08 13:24:45 +0900130Controllers::Controllers() : clatdCtrl(&netCtrl) {
131 InterfaceController::initializeAll();
Narayan Kamatha5ace892017-01-06 15:10:02 +0000132 IptablesRestoreController::installSignalHandler(&iptablesRestoreCtrl);
Erik Kline2c5aaa12016-06-08 13:24:45 +0900133}
Pierre Imai1cfa5432016-02-24 18:00:03 +0900134
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900135void Controllers::initIptablesRules() {
136 /*
137 * This is the only time we touch top-level chains in iptables; controllers
138 * should only mutate rules inside of their children chains, as created by
139 * the constants above.
140 *
141 * Modules should never ACCEPT packets (except in well-justified cases);
142 * they should instead defer to any remaining modules using RETURN, or
143 * otherwise DROP/REJECT.
144 */
145
Lorenzo Colitti81ff64e2017-02-02 02:30:13 +0900146 // Create chains for child modules.
147 // We cannot use createChildChainsFast for all chains because vendor code modifies filter OUTPUT
148 // and mangle POSTROUTING directly.
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900149 Stopwatch s;
Lorenzo Colitti81ff64e2017-02-02 02:30:13 +0900150 createChildChainsFast(V4V6, "filter", "INPUT", FILTER_INPUT);
151 createChildChainsFast(V4V6, "filter", "FORWARD", FILTER_FORWARD);
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900152 createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT);
Lorenzo Colitti81ff64e2017-02-02 02:30:13 +0900153 createChildChainsFast(V4V6, "raw", "PREROUTING", RAW_PREROUTING);
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900154 createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING);
Lorenzo Colitti81ff64e2017-02-02 02:30:13 +0900155 createChildChainsFast(V4V6, "mangle", "FORWARD", MANGLE_FORWARD);
156 createChildChainsFast(V4, "nat", "PREROUTING", NAT_PREROUTING);
157 createChildChainsFast(V4, "nat", "POSTROUTING", NAT_POSTROUTING);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900158 ALOGI("Creating child chains: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900159
160 // Let each module setup their child chains
161 setupOemIptablesHook();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900162 ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900163
164 /* When enabled, DROPs all packets except those matching rules. */
165 firewallCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900166 ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900167
168 /* Does DROPs in FORWARD by default */
169 natCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900170 ALOGI("Setting up NatController hooks: %.1fms", s.getTimeAndReset());
171
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900172 /*
173 * Does REJECT in INPUT, OUTPUT. Does counting also.
174 * No DROP/REJECT allowed later in netfilter-flow hook order.
175 */
176 bandwidthCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900177 ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset());
178
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900179 /*
180 * Counts in nat: PREROUTING, POSTROUTING.
181 * No DROP/REJECT allowed later in netfilter-flow hook order.
182 */
183 idletimerCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900184 ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900185}
186
187void Controllers::init() {
188 initIptablesRules();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900189
190 Stopwatch s;
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900191 bandwidthCtrl.enableBandwidthControl(false);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900192 ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900193
194 if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) {
195 ALOGE("failed to initialize RouteController (%s)", strerror(-ret));
196 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900197 ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900198}
199
Pierre Imai1cfa5432016-02-24 18:00:03 +0900200Controllers* gCtls = nullptr;
201
202} // namespace net
203} // namespace android