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 | |
| 17 | #include "Controllers.h" |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame^] | 18 | #include "IdletimerController.h" |
| 19 | #include "NetworkController.h" |
| 20 | #include "RouteController.h" |
| 21 | #include "oem_iptables_hook.h" |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace net { |
| 25 | |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame^] | 26 | namespace { |
| 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 | */ |
| 31 | static 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 | |
| 39 | static 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 | |
| 47 | static 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 | |
| 55 | static const char* RAW_PREROUTING[] = { |
| 56 | BandwidthController::LOCAL_RAW_PREROUTING, |
| 57 | IdletimerController::LOCAL_RAW_PREROUTING, |
| 58 | NatController::LOCAL_RAW_PREROUTING, |
| 59 | NULL, |
| 60 | }; |
| 61 | |
| 62 | static const char* MANGLE_POSTROUTING[] = { |
| 63 | BandwidthController::LOCAL_MANGLE_POSTROUTING, |
| 64 | IdletimerController::LOCAL_MANGLE_POSTROUTING, |
| 65 | NULL, |
| 66 | }; |
| 67 | |
| 68 | static const char* MANGLE_FORWARD[] = { |
| 69 | NatController::LOCAL_MANGLE_FORWARD, |
| 70 | NULL, |
| 71 | }; |
| 72 | |
| 73 | static const char* NAT_PREROUTING[] = { |
| 74 | OEM_IPTABLES_NAT_PREROUTING, |
| 75 | NULL, |
| 76 | }; |
| 77 | |
| 78 | static const char* NAT_POSTROUTING[] = { |
| 79 | NatController::LOCAL_NAT_POSTROUTING, |
| 80 | NULL, |
| 81 | }; |
| 82 | |
| 83 | static 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 Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 105 | Controllers::Controllers() : clatdCtrl(&netCtrl) { |
| 106 | InterfaceController::initializeAll(); |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 107 | IptablesRestoreController::installSignalHandler(&iptablesRestoreCtrl); |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 108 | } |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 109 | |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame^] | 110 | void 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 | |
| 151 | void 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 Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 160 | Controllers* gCtls = nullptr; |
| 161 | |
| 162 | } // namespace net |
| 163 | } // namespace android |