blob: 8f07a56cdf7acbd143991c0466ea27670f2f35d2 [file] [log] [blame]
Jeff Sharkeyd8c64022012-07-13 18:04:07 -07001/*
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
17#include <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#define LOG_TAG "FirewallController"
23#define LOG_NDEBUG 0
24
Lorenzo Colitti89faa342016-02-26 11:38:47 +090025#include <android-base/stringprintf.h>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070026#include <cutils/log.h>
27
28#include "NetdConstants.h"
29#include "FirewallController.h"
30
Lorenzo Colitti89faa342016-02-26 11:38:47 +090031using android::base::StringAppendF;
32
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070033const char* FirewallController::TABLE = "filter";
34
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070035const char* FirewallController::LOCAL_INPUT = "fw_INPUT";
36const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT";
37const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD";
38
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070039const char* FirewallController::LOCAL_DOZABLE = "fw_dozable";
40const char* FirewallController::LOCAL_STANDBY = "fw_standby";
Felipe Leme3f624342016-02-10 18:12:39 -080041const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave";
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070042
Lorenzo Colittic8683d72015-09-01 16:53:35 +090043// ICMPv6 types that are required for any form of IPv6 connectivity to work. Note that because the
44// fw_dozable chain is called from both INPUT and OUTPUT, this includes both packets that we need
45// to be able to send (e.g., RS, NS), and packets that we need to receive (e.g., RA, NA).
46const char* FirewallController::ICMPV6_TYPES[] = {
47 "packet-too-big",
48 "router-solicitation",
49 "router-advertisement",
50 "neighbour-solicitation",
51 "neighbour-advertisement",
52 "redirect",
53};
54
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070055FirewallController::FirewallController(void) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -070056 // If no rules are set, it's in BLACKLIST mode
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070057 mFirewallType = BLACKLIST;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070058}
59
60int FirewallController::setupIptablesHooks(void) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070061 int res = 0;
62 // child chains are created but not attached, they will be attached explicitly.
63 FirewallType firewallType = getFirewallType(DOZABLE);
64 res |= createChain(LOCAL_DOZABLE, LOCAL_INPUT, firewallType);
65
66 firewallType = getFirewallType(STANDBY);
67 res |= createChain(LOCAL_STANDBY, LOCAL_INPUT, firewallType);
68
Felipe Leme3f624342016-02-10 18:12:39 -080069 firewallType = getFirewallType(POWERSAVE);
70 res |= createChain(LOCAL_POWERSAVE, LOCAL_INPUT, firewallType);
71
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070072 return res;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070073}
74
Amith Yamasani390e4ea2015-04-25 19:08:57 -070075int FirewallController::enableFirewall(FirewallType ftype) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070076 int res = 0;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070077 if (mFirewallType != ftype) {
78 // flush any existing rules
79 disableFirewall();
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070080
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070081 if (ftype == WHITELIST) {
82 // create default rule to drop all traffic
83 res |= execIptables(V4V6, "-A", LOCAL_INPUT, "-j", "DROP", NULL);
84 res |= execIptables(V4V6, "-A", LOCAL_OUTPUT, "-j", "REJECT", NULL);
85 res |= execIptables(V4V6, "-A", LOCAL_FORWARD, "-j", "REJECT", NULL);
86 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070087
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070088 // Set this after calling disableFirewall(), since it defaults to WHITELIST there
89 mFirewallType = ftype;
Amith Yamasani390e4ea2015-04-25 19:08:57 -070090 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070091 return res;
92}
93
94int FirewallController::disableFirewall(void) {
95 int res = 0;
96
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070097 mFirewallType = WHITELIST;
Amith Yamasani390e4ea2015-04-25 19:08:57 -070098
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070099 // flush any existing rules
100 res |= execIptables(V4V6, "-F", LOCAL_INPUT, NULL);
101 res |= execIptables(V4V6, "-F", LOCAL_OUTPUT, NULL);
102 res |= execIptables(V4V6, "-F", LOCAL_FORWARD, NULL);
103
104 return res;
105}
106
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700107int FirewallController::enableChildChains(ChildChain chain, bool enable) {
108 int res = 0;
109 const char* name;
110 switch(chain) {
111 case DOZABLE:
112 name = LOCAL_DOZABLE;
113 break;
114 case STANDBY:
115 name = LOCAL_STANDBY;
116 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800117 case POWERSAVE:
118 name = LOCAL_POWERSAVE;
119 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700120 default:
121 return res;
122 }
123
124 if (enable) {
125 res |= attachChain(name, LOCAL_INPUT);
126 res |= attachChain(name, LOCAL_OUTPUT);
127 } else {
128 res |= detachChain(name, LOCAL_INPUT);
129 res |= detachChain(name, LOCAL_OUTPUT);
130 }
131 return res;
132}
133
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700134int FirewallController::isFirewallEnabled(void) {
135 // TODO: verify that rules are still in place near top
136 return -1;
137}
138
139int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700140 if (mFirewallType == BLACKLIST) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700141 // Unsupported in BLACKLIST mode
142 return -1;
143 }
144
JP Abgrall69261cb2014-06-19 18:35:24 -0700145 if (!isIfaceName(iface)) {
146 errno = ENOENT;
147 return -1;
148 }
149
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700150 const char* op;
151 if (rule == ALLOW) {
152 op = "-I";
153 } else {
154 op = "-D";
155 }
156
157 int res = 0;
158 res |= execIptables(V4V6, op, LOCAL_INPUT, "-i", iface, "-j", "RETURN", NULL);
159 res |= execIptables(V4V6, op, LOCAL_OUTPUT, "-o", iface, "-j", "RETURN", NULL);
160 return res;
161}
162
163int FirewallController::setEgressSourceRule(const char* addr, FirewallRule rule) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700164 if (mFirewallType == BLACKLIST) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700165 // Unsupported in BLACKLIST mode
166 return -1;
167 }
168
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700169 IptablesTarget target = V4;
170 if (strchr(addr, ':')) {
171 target = V6;
172 }
173
174 const char* op;
175 if (rule == ALLOW) {
176 op = "-I";
177 } else {
178 op = "-D";
179 }
180
181 int res = 0;
182 res |= execIptables(target, op, LOCAL_INPUT, "-d", addr, "-j", "RETURN", NULL);
183 res |= execIptables(target, op, LOCAL_OUTPUT, "-s", addr, "-j", "RETURN", NULL);
184 return res;
185}
186
187int FirewallController::setEgressDestRule(const char* addr, int protocol, int port,
188 FirewallRule rule) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700189 if (mFirewallType == BLACKLIST) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700190 // Unsupported in BLACKLIST mode
191 return -1;
192 }
193
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700194 IptablesTarget target = V4;
195 if (strchr(addr, ':')) {
196 target = V6;
197 }
198
199 char protocolStr[16];
200 sprintf(protocolStr, "%d", protocol);
201
202 char portStr[16];
203 sprintf(portStr, "%d", port);
204
205 const char* op;
206 if (rule == ALLOW) {
207 op = "-I";
208 } else {
209 op = "-D";
210 }
211
212 int res = 0;
213 res |= execIptables(target, op, LOCAL_INPUT, "-s", addr, "-p", protocolStr,
214 "--sport", portStr, "-j", "RETURN", NULL);
215 res |= execIptables(target, op, LOCAL_OUTPUT, "-d", addr, "-p", protocolStr,
216 "--dport", portStr, "-j", "RETURN", NULL);
217 return res;
218}
219
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700220FirewallType FirewallController::getFirewallType(ChildChain chain) {
221 switch(chain) {
222 case DOZABLE:
223 return WHITELIST;
224 case STANDBY:
225 return BLACKLIST;
Felipe Leme3f624342016-02-10 18:12:39 -0800226 case POWERSAVE:
227 return WHITELIST;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700228 case NONE:
229 return mFirewallType;
230 default:
231 return BLACKLIST;
232 }
233}
234
235int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700236 char uidStr[16];
237 sprintf(uidStr, "%d", uid);
238
239 const char* op;
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700240 const char* target;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700241 FirewallType firewallType = getFirewallType(chain);
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700242 if (firewallType == WHITELIST) {
243 target = "RETURN";
244 op = (rule == ALLOW)? "-I" : "-D";
245 } else { // BLACKLIST mode
246 target = "DROP";
247 op = (rule == DENY)? "-I" : "-D";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700248 }
249
250 int res = 0;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700251 switch(chain) {
252 case DOZABLE:
253 res |= execIptables(V4V6, op, LOCAL_DOZABLE, "-m", "owner", "--uid-owner",
254 uidStr, "-j", target, NULL);
255 break;
256 case STANDBY:
257 res |= execIptables(V4V6, op, LOCAL_STANDBY, "-m", "owner", "--uid-owner",
258 uidStr, "-j", target, NULL);
259 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800260 case POWERSAVE:
261 res |= execIptables(V4V6, op, LOCAL_POWERSAVE, "-m", "owner", "--uid-owner",
262 uidStr, "-j", target, NULL);
263 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700264 case NONE:
265 res |= execIptables(V4V6, op, LOCAL_INPUT, "-m", "owner", "--uid-owner", uidStr,
266 "-j", target, NULL);
267 res |= execIptables(V4V6, op, LOCAL_OUTPUT, "-m", "owner", "--uid-owner", uidStr,
268 "-j", target, NULL);
269 break;
270 default:
271 ALOGW("Unknown child chain: %d", chain);
272 break;
273 }
274 return res;
275}
276
277int FirewallController::attachChain(const char* childChain, const char* parentChain) {
278 return execIptables(V4V6, "-t", TABLE, "-A", parentChain, "-j", childChain, NULL);
279}
280
281int FirewallController::detachChain(const char* childChain, const char* parentChain) {
282 return execIptables(V4V6, "-t", TABLE, "-D", parentChain, "-j", childChain, NULL);
283}
284
285int FirewallController::createChain(const char* childChain,
286 const char* parentChain, FirewallType type) {
287 // Order is important, otherwise later steps may fail.
288 execIptablesSilently(V4V6, "-t", TABLE, "-D", parentChain, "-j", childChain, NULL);
289 execIptablesSilently(V4V6, "-t", TABLE, "-F", childChain, NULL);
290 execIptablesSilently(V4V6, "-t", TABLE, "-X", childChain, NULL);
291 int res = 0;
292 res |= execIptables(V4V6, "-t", TABLE, "-N", childChain, NULL);
293 if (type == WHITELIST) {
Lorenzo Colittic8683d72015-09-01 16:53:35 +0900294 // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 .
295 for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) {
296 res |= execIptables(V6, "-A", childChain, "-p", "icmpv6", "--icmpv6-type",
297 ICMPV6_TYPES[i], "-j", "RETURN", NULL);
298 }
299
Xiaohui Chenfeb2b612015-06-25 21:19:38 -0700300 // create default white list for system uid range
301 char uidStr[16];
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900302 sprintf(uidStr, "0-%d", MAX_SYSTEM_UID);
Xiaohui Chenfeb2b612015-06-25 21:19:38 -0700303 res |= execIptables(V4V6, "-A", childChain, "-m", "owner", "--uid-owner",
304 uidStr, "-j", "RETURN", NULL);
Lorenzo Colittic8683d72015-09-01 16:53:35 +0900305
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700306 // create default rule to drop all traffic
307 res |= execIptables(V4V6, "-A", childChain, "-j", "DROP", NULL);
308 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700309 return res;
310}
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900311
312std::string FirewallController::makeUidRules(
313 const char *name, bool isWhitelist, const std::vector<int32_t>& uids) {
314 const char *action = isWhitelist ? "RETURN" : "DROP";
315 const char *defaultAction = isWhitelist ? "DROP" : "RETURN";
316
317 std::string commands;
318
319 StringAppendF(&commands, "*filter\n:%s -\n", name);
320
321 if (isWhitelist) {
322 // Always whitelist system UIDs.
323 StringAppendF(&commands,
324 "-A %s -m owner --uid-owner %d-%d -j %s\n", name, 0, MAX_SYSTEM_UID, action);
325 }
326
327 for (auto uid : uids) {
328 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j %s\n", name, uid, action);
329 }
330
331 // If it's a blacklist chain that blacklists nothing, then don't add a default action.
332 if (isWhitelist || uids.size() > 0) {
333 StringAppendF(&commands, "-A %s -j %s\n", name, defaultAction);
334 }
335
336 StringAppendF(&commands, "COMMIT\n\x04"); // EOT.
337
338 return commands;
339}
340
341int FirewallController::replaceUidChain(
342 const char *name, bool isWhitelist, const std::vector<int32_t>& uids) {
343 std::string commands = makeUidRules(name, isWhitelist, uids);
344 return execIptablesRestore(V4V6, commands.c_str());
345}