Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 17 | #include <set> |
| 18 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #define LOG_TAG "FirewallController" |
| 25 | #define LOG_NDEBUG 0 |
| 26 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 27 | #include <android-base/strings.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 29 | #include <cutils/log.h> |
| 30 | |
| 31 | #include "NetdConstants.h" |
| 32 | #include "FirewallController.h" |
| 33 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 34 | using android::base::Join; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 35 | using android::base::StringAppendF; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 36 | using android::base::StringPrintf; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 37 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 38 | auto FirewallController::execIptablesRestore = ::execIptablesRestore; |
| 39 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 40 | const char* FirewallController::TABLE = "filter"; |
| 41 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 42 | const char* FirewallController::LOCAL_INPUT = "fw_INPUT"; |
| 43 | const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT"; |
| 44 | const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD"; |
| 45 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 46 | const char* FirewallController::LOCAL_DOZABLE = "fw_dozable"; |
| 47 | const char* FirewallController::LOCAL_STANDBY = "fw_standby"; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 48 | const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave"; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 49 | |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 50 | // ICMPv6 types that are required for any form of IPv6 connectivity to work. Note that because the |
| 51 | // fw_dozable chain is called from both INPUT and OUTPUT, this includes both packets that we need |
| 52 | // to be able to send (e.g., RS, NS), and packets that we need to receive (e.g., RA, NA). |
| 53 | const char* FirewallController::ICMPV6_TYPES[] = { |
| 54 | "packet-too-big", |
| 55 | "router-solicitation", |
| 56 | "router-advertisement", |
| 57 | "neighbour-solicitation", |
| 58 | "neighbour-advertisement", |
| 59 | "redirect", |
| 60 | }; |
| 61 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 62 | FirewallController::FirewallController(void) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 63 | // If no rules are set, it's in BLACKLIST mode |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 64 | mFirewallType = BLACKLIST; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 65 | mIfaceRules = {}; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int FirewallController::setupIptablesHooks(void) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 69 | int res = 0; |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 70 | res |= createChain(LOCAL_DOZABLE, getFirewallType(DOZABLE)); |
| 71 | res |= createChain(LOCAL_STANDBY, getFirewallType(STANDBY)); |
| 72 | res |= createChain(LOCAL_POWERSAVE, getFirewallType(POWERSAVE)); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 73 | return res; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 76 | int FirewallController::enableFirewall(FirewallType ftype) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 77 | int res = 0; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 78 | if (mFirewallType != ftype) { |
| 79 | // flush any existing rules |
| 80 | disableFirewall(); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 81 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 82 | if (ftype == WHITELIST) { |
| 83 | // create default rule to drop all traffic |
Lorenzo Colitti | d351bea | 2017-07-16 22:52:30 +0900 | [diff] [blame] | 84 | std::string command = |
| 85 | "*filter\n" |
| 86 | "-A fw_INPUT -j DROP\n" |
| 87 | "-A fw_OUTPUT -j REJECT\n" |
| 88 | "-A fw_FORWARD -j REJECT\n" |
| 89 | "COMMIT\n"; |
| 90 | res = execIptablesRestore(V4V6, command.c_str()); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 91 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 92 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 93 | // Set this after calling disableFirewall(), since it defaults to WHITELIST there |
| 94 | mFirewallType = ftype; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 95 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 96 | return res; |
| 97 | } |
| 98 | |
| 99 | int FirewallController::disableFirewall(void) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 100 | mFirewallType = WHITELIST; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 101 | mIfaceRules.clear(); |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 102 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 103 | // flush any existing rules |
Lorenzo Colitti | d351bea | 2017-07-16 22:52:30 +0900 | [diff] [blame] | 104 | std::string command = |
| 105 | "*filter\n" |
| 106 | ":fw_INPUT -\n" |
| 107 | ":fw_OUTPUT -\n" |
| 108 | ":fw_FORWARD -\n" |
| 109 | "COMMIT\n"; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 110 | |
Lorenzo Colitti | d351bea | 2017-07-16 22:52:30 +0900 | [diff] [blame] | 111 | return execIptablesRestore(V4V6, command.c_str()); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 114 | int FirewallController::enableChildChains(ChildChain chain, bool enable) { |
| 115 | int res = 0; |
| 116 | const char* name; |
| 117 | switch(chain) { |
| 118 | case DOZABLE: |
| 119 | name = LOCAL_DOZABLE; |
| 120 | break; |
| 121 | case STANDBY: |
| 122 | name = LOCAL_STANDBY; |
| 123 | break; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 124 | case POWERSAVE: |
| 125 | name = LOCAL_POWERSAVE; |
| 126 | break; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 127 | default: |
| 128 | return res; |
| 129 | } |
| 130 | |
Lorenzo Colitti | a161196 | 2017-04-26 16:30:39 +0900 | [diff] [blame] | 131 | std::string command = "*filter\n"; |
| 132 | for (const char *parent : { LOCAL_INPUT, LOCAL_OUTPUT }) { |
| 133 | StringAppendF(&command, "%s %s -j %s\n", (enable ? "-A" : "-D"), parent, name); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 134 | } |
Lorenzo Colitti | a161196 | 2017-04-26 16:30:39 +0900 | [diff] [blame] | 135 | StringAppendF(&command, "COMMIT\n"); |
| 136 | |
| 137 | return execIptablesRestore(V4V6, command); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 140 | int FirewallController::isFirewallEnabled(void) { |
| 141 | // TODO: verify that rules are still in place near top |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 146 | if (mFirewallType == BLACKLIST) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 147 | // Unsupported in BLACKLIST mode |
| 148 | return -1; |
| 149 | } |
| 150 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 151 | if (!isIfaceName(iface)) { |
| 152 | errno = ENOENT; |
| 153 | return -1; |
| 154 | } |
| 155 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 156 | // Only delete rules if we actually added them, because otherwise our iptables-restore |
| 157 | // processes will terminate with "no such rule" errors and cause latency penalties while we |
| 158 | // spin up new ones. |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 159 | const char* op; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 160 | if (rule == ALLOW && mIfaceRules.find(iface) == mIfaceRules.end()) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 161 | op = "-I"; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 162 | mIfaceRules.insert(iface); |
| 163 | } else if (rule == DENY && mIfaceRules.find(iface) != mIfaceRules.end()) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 164 | op = "-D"; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 165 | mIfaceRules.erase(iface); |
| 166 | } else { |
| 167 | return 0; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame^] | 170 | std::string command = Join(std::vector<std::string> { |
| 171 | "*filter", |
| 172 | StringPrintf("%s fw_INPUT -i %s -j RETURN", op, iface), |
| 173 | StringPrintf("%s fw_OUTPUT -o %s -j RETURN", op, iface), |
| 174 | "COMMIT\n" |
| 175 | }, "\n"); |
| 176 | return execIptablesRestore(V4V6, command); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 179 | FirewallType FirewallController::getFirewallType(ChildChain chain) { |
| 180 | switch(chain) { |
| 181 | case DOZABLE: |
| 182 | return WHITELIST; |
| 183 | case STANDBY: |
| 184 | return BLACKLIST; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 185 | case POWERSAVE: |
| 186 | return WHITELIST; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 187 | case NONE: |
| 188 | return mFirewallType; |
| 189 | default: |
| 190 | return BLACKLIST; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 195 | const char* op; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 196 | const char* target; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 197 | FirewallType firewallType = getFirewallType(chain); |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 198 | if (firewallType == WHITELIST) { |
| 199 | target = "RETURN"; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 200 | // When adding, insert RETURN rules at the front, before the catch-all DROP at the end. |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 201 | op = (rule == ALLOW)? "-I" : "-D"; |
| 202 | } else { // BLACKLIST mode |
| 203 | target = "DROP"; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 204 | // When adding, append DROP rules at the end, after the RETURN rule that matches TCP RSTs. |
| 205 | op = (rule == DENY)? "-A" : "-D"; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 208 | std::vector<std::string> chainNames; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 209 | switch(chain) { |
| 210 | case DOZABLE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 211 | chainNames = { LOCAL_DOZABLE }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 212 | break; |
| 213 | case STANDBY: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 214 | chainNames = { LOCAL_STANDBY }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 215 | break; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 216 | case POWERSAVE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 217 | chainNames = { LOCAL_POWERSAVE }; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 218 | break; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 219 | case NONE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 220 | chainNames = { LOCAL_INPUT, LOCAL_OUTPUT }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 221 | break; |
| 222 | default: |
| 223 | ALOGW("Unknown child chain: %d", chain); |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 224 | return -1; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 225 | } |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 226 | |
| 227 | std::string command = "*filter\n"; |
| 228 | for (std::string chainName : chainNames) { |
| 229 | StringAppendF(&command, "%s %s -m owner --uid-owner %d -j %s\n", |
| 230 | op, chainName.c_str(), uid, target); |
| 231 | } |
| 232 | StringAppendF(&command, "COMMIT\n"); |
| 233 | |
| 234 | return execIptablesRestore(V4V6, command); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 237 | int FirewallController::createChain(const char* chain, FirewallType type) { |
| 238 | static const std::vector<int32_t> NO_UIDS; |
| 239 | return replaceUidChain(chain, type == WHITELIST, NO_UIDS); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 240 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 241 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 242 | std::string FirewallController::makeUidRules(IptablesTarget target, const char *name, |
| 243 | bool isWhitelist, const std::vector<int32_t>& uids) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 244 | std::string commands; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 245 | StringAppendF(&commands, "*filter\n:%s -\n", name); |
| 246 | |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame] | 247 | // Whitelist chains have UIDs at the beginning, and new UIDs are added with '-I'. |
| 248 | if (isWhitelist) { |
| 249 | for (auto uid : uids) { |
| 250 | StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j RETURN\n", name, uid); |
| 251 | } |
| 252 | |
| 253 | // Always whitelist system UIDs. |
| 254 | StringAppendF(&commands, |
| 255 | "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID); |
| 256 | } |
| 257 | |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame] | 258 | // Always allow networking on loopback. |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 259 | StringAppendF(&commands, "-A %s -i lo -j RETURN\n", name); |
| 260 | StringAppendF(&commands, "-A %s -o lo -j RETURN\n", name); |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame] | 261 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 262 | // Allow TCP RSTs so we can cleanly close TCP connections of apps that no longer have network |
| 263 | // access. Both incoming and outgoing RSTs are allowed. |
| 264 | StringAppendF(&commands, "-A %s -p tcp --tcp-flags RST RST -j RETURN\n", name); |
| 265 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 266 | if (isWhitelist) { |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 267 | // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 . |
| 268 | if (target == V6) { |
| 269 | for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) { |
| 270 | StringAppendF(&commands, "-A %s -p icmpv6 --icmpv6-type %s -j RETURN\n", |
| 271 | name, ICMPV6_TYPES[i]); |
| 272 | } |
| 273 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 274 | } |
| 275 | |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame] | 276 | // Blacklist chains have UIDs at the end, and new UIDs are added with '-A'. |
| 277 | if (!isWhitelist) { |
| 278 | for (auto uid : uids) { |
| 279 | StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j DROP\n", name, uid); |
| 280 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 281 | } |
| 282 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 283 | // If it's a whitelist chain, add a default DROP at the end. This is not necessary for a |
| 284 | // blacklist chain, because all user-defined chains implicitly RETURN at the end. |
| 285 | if (isWhitelist) { |
| 286 | StringAppendF(&commands, "-A %s -j DROP\n", name); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 287 | } |
| 288 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 289 | StringAppendF(&commands, "COMMIT\n"); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 290 | |
| 291 | return commands; |
| 292 | } |
| 293 | |
| 294 | int FirewallController::replaceUidChain( |
| 295 | const char *name, bool isWhitelist, const std::vector<int32_t>& uids) { |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 296 | std::string commands4 = makeUidRules(V4, name, isWhitelist, uids); |
| 297 | std::string commands6 = makeUidRules(V6, name, isWhitelist, uids); |
| 298 | return execIptablesRestore(V4, commands4.c_str()) | execIptablesRestore(V6, commands6.c_str()); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 299 | } |