blob: 85b6b1e17fb4dc83ea051a75351eca16d637e6cb [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 Colitti19ee8a82017-02-02 12:59:05 +090017#define LOG_TAG "Netd"
18#include <cutils/log.h>
19
Pierre Imai1cfa5432016-02-24 18:00:03 +090020#include "Controllers.h"
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090021#include "IdletimerController.h"
22#include "NetworkController.h"
23#include "RouteController.h"
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +090024#include "Stopwatch.h"
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090025#include "oem_iptables_hook.h"
Pierre Imai1cfa5432016-02-24 18:00:03 +090026
27namespace android {
28namespace net {
29
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090030namespace {
31/**
32 * List of module chains to be created, along with explicit ordering. ORDERING
33 * IS CRITICAL, AND SHOULD BE TRIPLE-CHECKED WITH EACH CHANGE.
34 */
35static const char* FILTER_INPUT[] = {
36 // Bandwidth should always be early in input chain, to make sure we
37 // correctly count incoming traffic against data plan.
38 BandwidthController::LOCAL_INPUT,
39 FirewallController::LOCAL_INPUT,
40 NULL,
41};
42
43static const char* FILTER_FORWARD[] = {
44 OEM_IPTABLES_FILTER_FORWARD,
45 FirewallController::LOCAL_FORWARD,
46 BandwidthController::LOCAL_FORWARD,
47 NatController::LOCAL_FORWARD,
48 NULL,
49};
50
51static const char* FILTER_OUTPUT[] = {
52 OEM_IPTABLES_FILTER_OUTPUT,
53 FirewallController::LOCAL_OUTPUT,
54 StrictController::LOCAL_OUTPUT,
55 BandwidthController::LOCAL_OUTPUT,
56 NULL,
57};
58
59static const char* RAW_PREROUTING[] = {
60 BandwidthController::LOCAL_RAW_PREROUTING,
61 IdletimerController::LOCAL_RAW_PREROUTING,
62 NatController::LOCAL_RAW_PREROUTING,
63 NULL,
64};
65
66static const char* MANGLE_POSTROUTING[] = {
67 BandwidthController::LOCAL_MANGLE_POSTROUTING,
68 IdletimerController::LOCAL_MANGLE_POSTROUTING,
69 NULL,
70};
71
72static const char* MANGLE_FORWARD[] = {
73 NatController::LOCAL_MANGLE_FORWARD,
74 NULL,
75};
76
77static const char* NAT_PREROUTING[] = {
78 OEM_IPTABLES_NAT_PREROUTING,
79 NULL,
80};
81
82static const char* NAT_POSTROUTING[] = {
83 NatController::LOCAL_NAT_POSTROUTING,
84 NULL,
85};
86
87static void createChildChains(IptablesTarget target, const char* table, const char* parentChain,
88 const char** childChains) {
89 const char** childChain = childChains;
90 do {
91 // Order is important:
92 // -D to delete any pre-existing jump rule (removes references
93 // that would prevent -X from working)
94 // -F to flush any existing chain
95 // -X to delete any existing chain
96 // -N to create the chain
97 // -A to append the chain to parent
98
99 execIptablesSilently(target, "-t", table, "-D", parentChain, "-j", *childChain, NULL);
100 execIptablesSilently(target, "-t", table, "-F", *childChain, NULL);
101 execIptablesSilently(target, "-t", table, "-X", *childChain, NULL);
102 execIptables(target, "-t", table, "-N", *childChain, NULL);
103 execIptables(target, "-t", table, "-A", parentChain, "-j", *childChain, NULL);
104 } while (*(++childChain) != NULL);
105}
106
107} // namespace
108
Erik Kline2c5aaa12016-06-08 13:24:45 +0900109Controllers::Controllers() : clatdCtrl(&netCtrl) {
110 InterfaceController::initializeAll();
Narayan Kamatha5ace892017-01-06 15:10:02 +0000111 IptablesRestoreController::installSignalHandler(&iptablesRestoreCtrl);
Erik Kline2c5aaa12016-06-08 13:24:45 +0900112}
Pierre Imai1cfa5432016-02-24 18:00:03 +0900113
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900114void Controllers::initIptablesRules() {
115 /*
116 * This is the only time we touch top-level chains in iptables; controllers
117 * should only mutate rules inside of their children chains, as created by
118 * the constants above.
119 *
120 * Modules should never ACCEPT packets (except in well-justified cases);
121 * they should instead defer to any remaining modules using RETURN, or
122 * otherwise DROP/REJECT.
123 */
124
Lorenzo Colittif81cda02017-02-07 14:24:17 +0900125 // Create chains for children modules
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900126 Stopwatch s;
Lorenzo Colittif81cda02017-02-07 14:24:17 +0900127 createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT);
128 createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD);
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900129 createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT);
Lorenzo Colittif81cda02017-02-07 14:24:17 +0900130 createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING);
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900131 createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING);
Lorenzo Colittif81cda02017-02-07 14:24:17 +0900132 createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD);
133 createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING);
134 createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900135 ALOGI("Creating child chains: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900136
137 // Let each module setup their child chains
138 setupOemIptablesHook();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900139 ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900140
141 /* When enabled, DROPs all packets except those matching rules. */
142 firewallCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900143 ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900144
145 /* Does DROPs in FORWARD by default */
146 natCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900147 ALOGI("Setting up NatController hooks: %.1fms", s.getTimeAndReset());
148
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900149 /*
150 * Does REJECT in INPUT, OUTPUT. Does counting also.
151 * No DROP/REJECT allowed later in netfilter-flow hook order.
152 */
153 bandwidthCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900154 ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset());
155
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900156 /*
157 * Counts in nat: PREROUTING, POSTROUTING.
158 * No DROP/REJECT allowed later in netfilter-flow hook order.
159 */
160 idletimerCtrl.setupIptablesHooks();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900161 ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900162}
163
164void Controllers::init() {
165 initIptablesRules();
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900166
167 Stopwatch s;
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900168 bandwidthCtrl.enableBandwidthControl(false);
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900169 ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900170
171 if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) {
172 ALOGE("failed to initialize RouteController (%s)", strerror(-ret));
173 }
Lorenzo Colitti19ee8a82017-02-02 12:59:05 +0900174 ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset());
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900175}
176
Pierre Imai1cfa5432016-02-24 18:00:03 +0900177Controllers* gCtls = nullptr;
178
179} // namespace net
180} // namespace android