OSDN Git Service

Merge "Workaround ASan false positive in RouteController." into nyc-dev
[android-x86/system-netd.git] / server / FirewallController.cpp
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
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
25 #include <android-base/stringprintf.h>
26 #include <cutils/log.h>
27
28 #include "NetdConstants.h"
29 #include "FirewallController.h"
30
31 using android::base::StringAppendF;
32
33 const char* FirewallController::TABLE = "filter";
34
35 const char* FirewallController::LOCAL_INPUT = "fw_INPUT";
36 const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT";
37 const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD";
38
39 const char* FirewallController::LOCAL_DOZABLE = "fw_dozable";
40 const char* FirewallController::LOCAL_STANDBY = "fw_standby";
41 const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave";
42
43 // 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).
46 const char* FirewallController::ICMPV6_TYPES[] = {
47     "packet-too-big",
48     "router-solicitation",
49     "router-advertisement",
50     "neighbour-solicitation",
51     "neighbour-advertisement",
52     "redirect",
53 };
54
55 FirewallController::FirewallController(void) {
56     // If no rules are set, it's in BLACKLIST mode
57     mFirewallType = BLACKLIST;
58 }
59
60 int FirewallController::setupIptablesHooks(void) {
61     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
69     firewallType = getFirewallType(POWERSAVE);
70     res |= createChain(LOCAL_POWERSAVE, LOCAL_INPUT, firewallType);
71
72     return res;
73 }
74
75 int FirewallController::enableFirewall(FirewallType ftype) {
76     int res = 0;
77     if (mFirewallType != ftype) {
78         // flush any existing rules
79         disableFirewall();
80
81         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         }
87
88         // Set this after calling disableFirewall(), since it defaults to WHITELIST there
89         mFirewallType = ftype;
90     }
91     return res;
92 }
93
94 int FirewallController::disableFirewall(void) {
95     int res = 0;
96
97     mFirewallType = WHITELIST;
98
99     // 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
107 int 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;
117         case POWERSAVE:
118             name = LOCAL_POWERSAVE;
119             break;
120         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
134 int FirewallController::isFirewallEnabled(void) {
135     // TODO: verify that rules are still in place near top
136     return -1;
137 }
138
139 int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) {
140     if (mFirewallType == BLACKLIST) {
141         // Unsupported in BLACKLIST mode
142         return -1;
143     }
144
145     if (!isIfaceName(iface)) {
146         errno = ENOENT;
147         return -1;
148     }
149
150     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
163 int FirewallController::setEgressSourceRule(const char* addr, FirewallRule rule) {
164     if (mFirewallType == BLACKLIST) {
165         // Unsupported in BLACKLIST mode
166         return -1;
167     }
168
169     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
187 int FirewallController::setEgressDestRule(const char* addr, int protocol, int port,
188         FirewallRule rule) {
189     if (mFirewallType == BLACKLIST) {
190         // Unsupported in BLACKLIST mode
191         return -1;
192     }
193
194     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
220 FirewallType FirewallController::getFirewallType(ChildChain chain) {
221     switch(chain) {
222         case DOZABLE:
223             return WHITELIST;
224         case STANDBY:
225             return BLACKLIST;
226         case POWERSAVE:
227             return WHITELIST;
228         case NONE:
229             return mFirewallType;
230         default:
231             return BLACKLIST;
232     }
233 }
234
235 int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) {
236     char uidStr[16];
237     sprintf(uidStr, "%d", uid);
238
239     const char* op;
240     const char* target;
241     FirewallType firewallType = getFirewallType(chain);
242     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";
248     }
249
250     int res = 0;
251     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;
260         case POWERSAVE:
261             res |= execIptables(V4V6, op, LOCAL_POWERSAVE, "-m", "owner", "--uid-owner",
262                     uidStr, "-j", target, NULL);
263             break;
264         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
277 int FirewallController::attachChain(const char* childChain, const char* parentChain) {
278     return execIptables(V4V6, "-t", TABLE, "-A", parentChain, "-j", childChain, NULL);
279 }
280
281 int FirewallController::detachChain(const char* childChain, const char* parentChain) {
282     return execIptables(V4V6, "-t", TABLE, "-D", parentChain, "-j", childChain, NULL);
283 }
284
285 int 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) {
294         // 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
300         // create default white list for system uid range
301         char uidStr[16];
302         sprintf(uidStr, "0-%d", MAX_SYSTEM_UID);
303         res |= execIptables(V4V6, "-A", childChain, "-m", "owner", "--uid-owner",
304                 uidStr, "-j", "RETURN", NULL);
305
306         // create default rule to drop all traffic
307         res |= execIptables(V4V6, "-A", childChain, "-j", "DROP", NULL);
308     }
309     return res;
310 }
311
312 std::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
341 int 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 }