blob: cdc5dadeb6ba70c7cd816a3fa62512807a5f7c9e [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
17#include "Controllers.h"
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090018#include "IdletimerController.h"
19#include "NetworkController.h"
20#include "RouteController.h"
21#include "oem_iptables_hook.h"
Pierre Imai1cfa5432016-02-24 18:00:03 +090022
23namespace android {
24namespace net {
25
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090026namespace {
27/**
28 * List of module chains to be created, along with explicit ordering. ORDERING
29 * IS CRITICAL, AND SHOULD BE TRIPLE-CHECKED WITH EACH CHANGE.
30 */
31static const char* FILTER_INPUT[] = {
32 // Bandwidth should always be early in input chain, to make sure we
33 // correctly count incoming traffic against data plan.
34 BandwidthController::LOCAL_INPUT,
35 FirewallController::LOCAL_INPUT,
36 NULL,
37};
38
39static const char* FILTER_FORWARD[] = {
40 OEM_IPTABLES_FILTER_FORWARD,
41 FirewallController::LOCAL_FORWARD,
42 BandwidthController::LOCAL_FORWARD,
43 NatController::LOCAL_FORWARD,
44 NULL,
45};
46
47static const char* FILTER_OUTPUT[] = {
48 OEM_IPTABLES_FILTER_OUTPUT,
49 FirewallController::LOCAL_OUTPUT,
50 StrictController::LOCAL_OUTPUT,
51 BandwidthController::LOCAL_OUTPUT,
52 NULL,
53};
54
55static const char* RAW_PREROUTING[] = {
56 BandwidthController::LOCAL_RAW_PREROUTING,
57 IdletimerController::LOCAL_RAW_PREROUTING,
58 NatController::LOCAL_RAW_PREROUTING,
59 NULL,
60};
61
62static const char* MANGLE_POSTROUTING[] = {
63 BandwidthController::LOCAL_MANGLE_POSTROUTING,
64 IdletimerController::LOCAL_MANGLE_POSTROUTING,
65 NULL,
66};
67
68static const char* MANGLE_FORWARD[] = {
69 NatController::LOCAL_MANGLE_FORWARD,
70 NULL,
71};
72
73static const char* NAT_PREROUTING[] = {
74 OEM_IPTABLES_NAT_PREROUTING,
75 NULL,
76};
77
78static const char* NAT_POSTROUTING[] = {
79 NatController::LOCAL_NAT_POSTROUTING,
80 NULL,
81};
82
83static void createChildChains(IptablesTarget target, const char* table, const char* parentChain,
84 const char** childChains) {
85 const char** childChain = childChains;
86 do {
87 // Order is important:
88 // -D to delete any pre-existing jump rule (removes references
89 // that would prevent -X from working)
90 // -F to flush any existing chain
91 // -X to delete any existing chain
92 // -N to create the chain
93 // -A to append the chain to parent
94
95 execIptablesSilently(target, "-t", table, "-D", parentChain, "-j", *childChain, NULL);
96 execIptablesSilently(target, "-t", table, "-F", *childChain, NULL);
97 execIptablesSilently(target, "-t", table, "-X", *childChain, NULL);
98 execIptables(target, "-t", table, "-N", *childChain, NULL);
99 execIptables(target, "-t", table, "-A", parentChain, "-j", *childChain, NULL);
100 } while (*(++childChain) != NULL);
101}
102
103} // namespace
104
Erik Kline2c5aaa12016-06-08 13:24:45 +0900105Controllers::Controllers() : clatdCtrl(&netCtrl) {
106 InterfaceController::initializeAll();
Narayan Kamatha5ace892017-01-06 15:10:02 +0000107 IptablesRestoreController::installSignalHandler(&iptablesRestoreCtrl);
Erik Kline2c5aaa12016-06-08 13:24:45 +0900108}
Pierre Imai1cfa5432016-02-24 18:00:03 +0900109
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900110void Controllers::initIptablesRules() {
111 /*
112 * This is the only time we touch top-level chains in iptables; controllers
113 * should only mutate rules inside of their children chains, as created by
114 * the constants above.
115 *
116 * Modules should never ACCEPT packets (except in well-justified cases);
117 * they should instead defer to any remaining modules using RETURN, or
118 * otherwise DROP/REJECT.
119 */
120
121 // Create chains for children modules
122 createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT);
123 createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD);
124 createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT);
125 createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING);
126 createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING);
127 createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD);
128 createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING);
129 createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING);
130
131 // Let each module setup their child chains
132 setupOemIptablesHook();
133
134 /* When enabled, DROPs all packets except those matching rules. */
135 firewallCtrl.setupIptablesHooks();
136
137 /* Does DROPs in FORWARD by default */
138 natCtrl.setupIptablesHooks();
139 /*
140 * Does REJECT in INPUT, OUTPUT. Does counting also.
141 * No DROP/REJECT allowed later in netfilter-flow hook order.
142 */
143 bandwidthCtrl.setupIptablesHooks();
144 /*
145 * Counts in nat: PREROUTING, POSTROUTING.
146 * No DROP/REJECT allowed later in netfilter-flow hook order.
147 */
148 idletimerCtrl.setupIptablesHooks();
149}
150
151void Controllers::init() {
152 initIptablesRules();
153 bandwidthCtrl.enableBandwidthControl(false);
154
155 if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) {
156 ALOGE("failed to initialize RouteController (%s)", strerror(-ret));
157 }
158}
159
Pierre Imai1cfa5432016-02-24 18:00:03 +0900160Controllers* gCtls = nullptr;
161
162} // namespace net
163} // namespace android