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 | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 17 | #define LOG_TAG "Netd" |
| 18 | #include <cutils/log.h> |
| 19 | |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 20 | #include "Controllers.h" |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 21 | #include "IdletimerController.h" |
| 22 | #include "NetworkController.h" |
| 23 | #include "RouteController.h" |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 24 | #include "Stopwatch.h" |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 25 | #include "oem_iptables_hook.h" |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace net { |
| 29 | |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 30 | namespace { |
| 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 | */ |
| 35 | static 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 | |
| 43 | static 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 | |
| 51 | static 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 | |
| 59 | static const char* RAW_PREROUTING[] = { |
| 60 | BandwidthController::LOCAL_RAW_PREROUTING, |
| 61 | IdletimerController::LOCAL_RAW_PREROUTING, |
| 62 | NatController::LOCAL_RAW_PREROUTING, |
| 63 | NULL, |
| 64 | }; |
| 65 | |
| 66 | static const char* MANGLE_POSTROUTING[] = { |
| 67 | BandwidthController::LOCAL_MANGLE_POSTROUTING, |
| 68 | IdletimerController::LOCAL_MANGLE_POSTROUTING, |
| 69 | NULL, |
| 70 | }; |
| 71 | |
| 72 | static const char* MANGLE_FORWARD[] = { |
| 73 | NatController::LOCAL_MANGLE_FORWARD, |
| 74 | NULL, |
| 75 | }; |
| 76 | |
| 77 | static const char* NAT_PREROUTING[] = { |
| 78 | OEM_IPTABLES_NAT_PREROUTING, |
| 79 | NULL, |
| 80 | }; |
| 81 | |
| 82 | static const char* NAT_POSTROUTING[] = { |
| 83 | NatController::LOCAL_NAT_POSTROUTING, |
| 84 | NULL, |
| 85 | }; |
| 86 | |
| 87 | static 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 Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 109 | Controllers::Controllers() : clatdCtrl(&netCtrl) { |
| 110 | InterfaceController::initializeAll(); |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 111 | IptablesRestoreController::installSignalHandler(&iptablesRestoreCtrl); |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 112 | } |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 113 | |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 114 | void 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 Colitti | f81cda0 | 2017-02-07 14:24:17 +0900 | [diff] [blame] | 125 | // Create chains for children modules |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 126 | Stopwatch s; |
Lorenzo Colitti | f81cda0 | 2017-02-07 14:24:17 +0900 | [diff] [blame] | 127 | createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT); |
| 128 | createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 129 | createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT); |
Lorenzo Colitti | f81cda0 | 2017-02-07 14:24:17 +0900 | [diff] [blame] | 130 | createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 131 | createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING); |
Lorenzo Colitti | f81cda0 | 2017-02-07 14:24:17 +0900 | [diff] [blame] | 132 | createChildChains(V4V6, "mangle", "FORWARD", MANGLE_FORWARD); |
| 133 | createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING); |
| 134 | createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 135 | ALOGI("Creating child chains: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 136 | |
| 137 | // Let each module setup their child chains |
| 138 | setupOemIptablesHook(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 139 | ALOGI("Setting up OEM hooks: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 140 | |
| 141 | /* When enabled, DROPs all packets except those matching rules. */ |
| 142 | firewallCtrl.setupIptablesHooks(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 143 | ALOGI("Setting up FirewallController hooks: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 144 | |
| 145 | /* Does DROPs in FORWARD by default */ |
| 146 | natCtrl.setupIptablesHooks(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 147 | ALOGI("Setting up NatController hooks: %.1fms", s.getTimeAndReset()); |
| 148 | |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 149 | /* |
| 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 Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 154 | ALOGI("Setting up BandwidthController hooks: %.1fms", s.getTimeAndReset()); |
| 155 | |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 156 | /* |
| 157 | * Counts in nat: PREROUTING, POSTROUTING. |
| 158 | * No DROP/REJECT allowed later in netfilter-flow hook order. |
| 159 | */ |
| 160 | idletimerCtrl.setupIptablesHooks(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 161 | ALOGI("Setting up IdletimerController hooks: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void Controllers::init() { |
| 165 | initIptablesRules(); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 166 | |
| 167 | Stopwatch s; |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 168 | bandwidthCtrl.enableBandwidthControl(false); |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 169 | ALOGI("Disabling bandwidth control: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 170 | |
| 171 | if (int ret = RouteController::Init(NetworkController::LOCAL_NET_ID)) { |
| 172 | ALOGE("failed to initialize RouteController (%s)", strerror(-ret)); |
| 173 | } |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 174 | ALOGI("Initializing RouteController: %.1fms", s.getTimeAndReset()); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 177 | Controllers* gCtls = nullptr; |
| 178 | |
| 179 | } // namespace net |
| 180 | } // namespace android |