Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 1 | /* |
| 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 Colitti | 05306fb | 2017-02-09 04:56:00 +0900 | [diff] [blame] | 17 | #include <android-base/stringprintf.h> |
| 18 | |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 19 | #define LOG_TAG "Netd" |
| 20 | #include <cutils/log.h> |
| 21 | |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 22 | #include "Controllers.h" |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 23 | #include "IdletimerController.h" |
| 24 | #include "NetworkController.h" |
| 25 | #include "RouteController.h" |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 26 | #include "Stopwatch.h" |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 27 | #include "oem_iptables_hook.h" |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace net { |
| 31 | |
Lorenzo Colitti | 341d3a0 | 2017-08-08 17:31:35 +0900 | [diff] [blame] | 32 | auto Controllers::execIptablesRestore = ::execIptablesRestore; |
| 33 | auto Controllers::execIptablesSilently = ::execIptablesSilently; |
| 34 | |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 35 | namespace { |
| 36 | /** |
| 37 | * List of module chains to be created, along with explicit ordering. ORDERING |
| 38 | * IS CRITICAL, AND SHOULD BE TRIPLE-CHECKED WITH EACH CHANGE. |
| 39 | */ |
| 40 | static const char* FILTER_INPUT[] = { |
| 41 | // Bandwidth should always be early in input chain, to make sure we |
| 42 | // correctly count incoming traffic against data plan. |
| 43 | BandwidthController::LOCAL_INPUT, |
| 44 | FirewallController::LOCAL_INPUT, |
| 45 | NULL, |
| 46 | }; |
| 47 | |
| 48 | static const char* FILTER_FORWARD[] = { |
| 49 | OEM_IPTABLES_FILTER_FORWARD, |
| 50 | FirewallController::LOCAL_FORWARD, |
| 51 | BandwidthController::LOCAL_FORWARD, |
| 52 | NatController::LOCAL_FORWARD, |
| 53 | NULL, |
| 54 | }; |
| 55 | |
| 56 | static const char* FILTER_OUTPUT[] = { |
| 57 | OEM_IPTABLES_FILTER_OUTPUT, |
| 58 | FirewallController::LOCAL_OUTPUT, |
| 59 | StrictController::LOCAL_OUTPUT, |
| 60 | BandwidthController::LOCAL_OUTPUT, |
| 61 | NULL, |
| 62 | }; |
| 63 | |
| 64 | static const char* RAW_PREROUTING[] = { |
| 65 | BandwidthController::LOCAL_RAW_PREROUTING, |
| 66 | IdletimerController::LOCAL_RAW_PREROUTING, |
| 67 | NatController::LOCAL_RAW_PREROUTING, |
| 68 | NULL, |
| 69 | }; |
| 70 | |
| 71 | static const char* MANGLE_POSTROUTING[] = { |
Lorenzo Colitti | 05306fb | 2017-02-09 04:56:00 +0900 | [diff] [blame] | 72 | OEM_IPTABLES_MANGLE_POSTROUTING, |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 73 | BandwidthController::LOCAL_MANGLE_POSTROUTING, |
| 74 | IdletimerController::LOCAL_MANGLE_POSTROUTING, |
| 75 | NULL, |
| 76 | }; |
| 77 | |
Lorenzo Colitti | d78843e | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 78 | static const char* MANGLE_INPUT[] = { |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 79 | WakeupController::LOCAL_MANGLE_INPUT, |
Lorenzo Colitti | d78843e | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 80 | RouteController::LOCAL_MANGLE_INPUT, |
| 81 | NULL, |
| 82 | }; |
| 83 | |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 84 | static const char* MANGLE_FORWARD[] = { |
| 85 | NatController::LOCAL_MANGLE_FORWARD, |
| 86 | NULL, |
| 87 | }; |
| 88 | |
| 89 | static const char* NAT_PREROUTING[] = { |
| 90 | OEM_IPTABLES_NAT_PREROUTING, |
| 91 | NULL, |
| 92 | }; |
| 93 | |
| 94 | static const char* NAT_POSTROUTING[] = { |
| 95 | NatController::LOCAL_NAT_POSTROUTING, |
| 96 | NULL, |
| 97 | }; |
| 98 | |
Lorenzo Colitti | 341d3a0 | 2017-08-08 17:31:35 +0900 | [diff] [blame] | 99 | } // namespace |
| 100 | |
| 101 | /* static */ |
| 102 | void Controllers::createChildChains(IptablesTarget target, const char* table, |
| 103 | const char* parentChain, |
| 104 | const char** childChains, |
| 105 | bool exclusive) { |
Lorenzo Colitti | 05306fb | 2017-02-09 04:56:00 +0900 | [diff] [blame] | 106 | std::string command = android::base::StringPrintf("*%s\n", table); |
Lorenzo Colitti | cda022e | 2017-02-03 12:35:46 +0900 | [diff] [blame] | 107 | |
| 108 | // If we're the exclusive owner of this chain, clear it entirely. This saves us from having to |
| 109 | // run one execIptablesSilently command to delete each child chain. We can't use -D in |
| 110 | // iptables-restore because it's a fatal error if the rule doesn't exist. |
| 111 | // TODO: Make all chains exclusive once vendor code uses the oem_* rules. |
| 112 | if (exclusive) { |
| 113 | // Just running ":chain -" flushes user-defined chains, but not built-in chains like INPUT. |
| 114 | // Since at this point we don't know if parentChain is a built-in chain, do both. |
| 115 | command += android::base::StringPrintf(":%s -\n", parentChain); |
| 116 | command += android::base::StringPrintf("-F %s\n", parentChain); |
| 117 | } |
| 118 | |
| 119 | const char** childChain = childChains; |
Lorenzo Colitti | 05306fb | 2017-02-09 04:56:00 +0900 | [diff] [blame] | 120 | do { |
Lorenzo Colitti | cda022e | 2017-02-03 12:35:46 +0900 | [diff] [blame] | 121 | if (!exclusive) { |
| 122 | execIptablesSilently(target, "-t", table, "-D", parentChain, "-j", *childChain, NULL); |
| 123 | } |
Lorenzo Colitti | 05306fb | 2017-02-09 04:56:00 +0900 | [diff] [blame] | 124 | command += android::base::StringPrintf(":%s -\n", *childChain); |
| 125 | command += android::base::StringPrintf("-A %s -j %s\n", parentChain, *childChain); |
| 126 | } while (*(++childChain) != NULL); |
Lorenzo Colitti | 341d3a0 | 2017-08-08 17:31:35 +0900 | [diff] [blame] | 127 | command += "COMMIT\n"; |
Lorenzo Colitti | 05306fb | 2017-02-09 04:56:00 +0900 | [diff] [blame] | 128 | execIptablesRestore(target, command); |
| 129 | } |
| 130 | |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 131 | Controllers::Controllers() |
| 132 | : clatdCtrl(&netCtrl), |
| 133 | wakeupCtrl( |
| 134 | [this](const std::string& prefix, uid_t uid, gid_t gid, uint64_t timestampNs) { |
| 135 | const auto listener = eventReporter.getNetdEventListener(); |
| 136 | if (listener == nullptr) { |
| 137 | ALOGE("getNetdEventListener() returned nullptr. dropping wakeup event"); |
| 138 | return; |
| 139 | } |
| 140 | listener->onWakeupEvent(String16(prefix.c_str()), uid, gid, timestampNs); |
| 141 | }, |
| 142 | &iptablesRestoreCtrl) { |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 143 | InterfaceController::initializeAll(); |
| 144 | } |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 145 | |
Lorenzo Colitti | 341d3a0 | 2017-08-08 17:31:35 +0900 | [diff] [blame] | 146 | void Controllers::initChildChains() { |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 147 | /* |
| 148 | * This is the only time we touch top-level chains in iptables; controllers |
| 149 | * should only mutate rules inside of their children chains, as created by |
| 150 | * the constants above. |
| 151 | * |
| 152 | * Modules should never ACCEPT packets (except in well-justified cases); |
| 153 | * they should instead defer to any remaining modules using RETURN, or |
| 154 | * otherwise DROP/REJECT. |
| 155 | */ |
| 156 | |
Lorenzo Colitti | 05306fb | 2017-02-09 04:56:00 +0900 | [diff] [blame] | 157 | // Create chains for child modules. |
Lorenzo Colitti | cda022e | 2017-02-03 12:35:46 +0900 | [diff] [blame] | 158 | createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT, true); |
| 159 | createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD, true); |
Lorenzo Colitti | cda022e | 2017-02-03 12:35:46 +0900 | [diff] [blame] | 160 | createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING, true); |
Lorenzo Colitti | cda022e | 2017-02-03 12:35:46 +0900 | [diff] [blame] | 161 | createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD, true); |
Lorenzo Colitti | d78843e | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 162 | createChildChains(V4V6, "mangle", "INPUT", MANGLE_INPUT, true); |
Lorenzo Colitti | cda022e | 2017-02-03 12:35:46 +0900 | [diff] [blame] | 163 | createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING, true); |
| 164 | createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING, true); |
Lorenzo Colitti | 341d3a0 | 2017-08-08 17:31:35 +0900 | [diff] [blame] | 165 | |
| 166 | // We cannot use createChildChainsFast for all chains because vendor code modifies filter OUTPUT |
| 167 | // and mangle POSTROUTING directly. |
| 168 | createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT, false); |
| 169 | createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING, false); |
| 170 | } |
| 171 | |
| 172 | void Controllers::initIptablesRules() { |
| 173 | Stopwatch s; |
| 174 | initChildChains(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 175 | ALOGI("Creating child chains: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 176 | |
| 177 | // Let each module setup their child chains |
| 178 | setupOemIptablesHook(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 179 | ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 180 | |
| 181 | /* When enabled, DROPs all packets except those matching rules. */ |
| 182 | firewallCtrl.setupIptablesHooks(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 183 | ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 184 | |
| 185 | /* Does DROPs in FORWARD by default */ |
| 186 | natCtrl.setupIptablesHooks(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 187 | ALOGI("Setting up NatController hooks: %.1fms", s.getTimeAndReset()); |
| 188 | |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 189 | /* |
| 190 | * Does REJECT in INPUT, OUTPUT. Does counting also. |
| 191 | * No DROP/REJECT allowed later in netfilter-flow hook order. |
| 192 | */ |
| 193 | bandwidthCtrl.setupIptablesHooks(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 194 | ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset()); |
| 195 | |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 196 | /* |
| 197 | * Counts in nat: PREROUTING, POSTROUTING. |
| 198 | * No DROP/REJECT allowed later in netfilter-flow hook order. |
| 199 | */ |
| 200 | idletimerCtrl.setupIptablesHooks(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 201 | ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void Controllers::init() { |
| 205 | initIptablesRules(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 206 | |
| 207 | Stopwatch s; |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 208 | bandwidthCtrl.enableBandwidthControl(false); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 209 | ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 210 | |
| 211 | if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) { |
| 212 | ALOGE("failed to initialize RouteController (%s)", strerror(-ret)); |
| 213 | } |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 214 | ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 217 | Controllers* gCtls = nullptr; |
| 218 | |
| 219 | } // namespace net |
| 220 | } // namespace android |