blob: 0584d09b79300bd1a6ef1423a9e160bce1b82e89 [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
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 Colitti05306fb2017-02-09 04:56:00 +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();
132}
Pierre Imai1cfa5432016-02-24 18:00:03 +0900133
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900134void Controllers::initIptablesRules() {
135 /*
136 * This is the only time we touch top-level chains in iptables; controllers
137 * should only mutate rules inside of their children chains, as created by
138 * the constants above.
139 *
140 * Modules should never ACCEPT packets (except in well-justified cases);
141 * they should instead defer to any remaining modules using RETURN, or
142 * otherwise DROP/REJECT.
143 */
144
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900145 // Create chains for child modules.
146 // We cannot use createChildChainsFast for all chains because vendor code modifies filter OUTPUT
147 // and mangle POSTROUTING directly.
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900148 Stopwatch s;
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900149 createChildChainsFast(V4V6, "filter", "INPUT", FILTER_INPUT);
150 createChildChainsFast(V4V6, "filter", "FORWARD", FILTER_FORWARD);
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900151 createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT);
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900152 createChildChainsFast(V4V6, "raw", "PREROUTING", RAW_PREROUTING);
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900153 createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING);
Lorenzo Colitti05306fb2017-02-09 04:56:00 +0900154 createChildChainsFast(V4V6, "mangle", "FORWARD", MANGLE_FORWARD);
155 createChildChainsFast(V4, "nat", "PREROUTING", NAT_PREROUTING);
156 createChildChainsFast(V4, "nat", "POSTROUTING", NAT_POSTROUTING);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900157 ALOGI("Creating child chains: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900158
159 // Let each module setup their child chains
160 setupOemIptablesHook();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900161 ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900162
163 /* When enabled, DROPs all packets except those matching rules. */
164 firewallCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900165 ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900166
167 /* Does DROPs in FORWARD by default */
168 natCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900169 ALOGI("Setting up NatController hooks: %.1fms", s.getTimeAndReset());
170
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900171 /*
172 * Does REJECT in INPUT, OUTPUT. Does counting also.
173 * No DROP/REJECT allowed later in netfilter-flow hook order.
174 */
175 bandwidthCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900176 ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset());
177
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900178 /*
179 * Counts in nat: PREROUTING, POSTROUTING.
180 * No DROP/REJECT allowed later in netfilter-flow hook order.
181 */
182 idletimerCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900183 ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900184}
185
186void Controllers::init() {
187 initIptablesRules();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900188
189 Stopwatch s;
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900190 bandwidthCtrl.enableBandwidthControl(false);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900191 ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900192
193 if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) {
194 ALOGE("failed to initialize RouteController (%s)", strerror(-ret));
195 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900196 ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900197}
198
Pierre Imai1cfa5432016-02-24 18:00:03 +0900199Controllers* gCtls = nullptr;
200
201} // namespace net
202} // namespace android