blob: 67d632caa3f4e4e1e072b9f48461f5288d6bcef4 [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#ifndef _FIREWALL_CONTROLLER_H
18#define _FIREWALL_CONTROLLER_H
19
20#include <string>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090021#include <vector>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070022
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090023#include <utils/RWLock.h>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070024
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090025#include "NetdConstants.h"
26
Amith Yamasani390e4ea2015-04-25 19:08:57 -070027enum FirewallRule { DENY, ALLOW };
28
29// WHITELIST means the firewall denies all by default, uids must be explicitly ALLOWed
30// BLACKLIST means the firewall allows all by default, uids must be explicitly DENYed
31
32enum FirewallType { WHITELIST, BLACKLIST };
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070033
Felipe Leme3f624342016-02-10 18:12:39 -080034enum ChildChain { NONE, DOZABLE, STANDBY, POWERSAVE, INVALID_CHAIN };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070035
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070036#define PROTOCOL_TCP 6
37#define PROTOCOL_UDP 17
38
39/*
40 * Simple firewall that drops all packets except those matching explicitly
41 * defined ALLOW rules.
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090042 *
43 * Methods in this class must be called when holding a write lock on |lock|, and may not call
44 * any other controller without explicitly managing that controller's lock. There are currently
45 * no such methods.
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070046 */
47class FirewallController {
48public:
49 FirewallController();
50
51 int setupIptablesHooks(void);
52
Amith Yamasani390e4ea2015-04-25 19:08:57 -070053 int enableFirewall(FirewallType);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070054 int disableFirewall(void);
55 int isFirewallEnabled(void);
56
57 /* Match traffic going in/out over the given iface. */
58 int setInterfaceRule(const char*, FirewallRule);
59 /* Match traffic coming-in-to or going-out-from given address. */
60 int setEgressSourceRule(const char*, FirewallRule);
61 /* Match traffic coming-in-from or going-out-to given address, port, and protocol. */
62 int setEgressDestRule(const char*, int, int, FirewallRule);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070063 /* Match traffic owned by given UID. This is specific to a particular chain. */
64 int setUidRule(ChildChain, int, FirewallRule);
65
66 int enableChildChains(ChildChain, bool);
67
Lorenzo Colitti89faa342016-02-26 11:38:47 +090068 int replaceUidChain(const char*, bool, const std::vector<int32_t>&);
69
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070070 static const char* TABLE;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070071
72 static const char* LOCAL_INPUT;
73 static const char* LOCAL_OUTPUT;
74 static const char* LOCAL_FORWARD;
75
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070076 static const char* LOCAL_DOZABLE;
77 static const char* LOCAL_STANDBY;
Felipe Leme3f624342016-02-10 18:12:39 -080078 static const char* LOCAL_POWERSAVE;
Lorenzo Colittic8683d72015-09-01 16:53:35 +090079
80 static const char* ICMPV6_TYPES[];
81
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090082 android::RWLock lock;
83
Lorenzo Colitti89faa342016-02-26 11:38:47 +090084protected:
85 friend class FirewallControllerTest;
Lorenzo Colittif157caf2016-05-13 11:25:54 +090086 std::string makeUidRules(IptablesTarget target, const char *name, bool isWhitelist,
87 const std::vector<int32_t>& uids);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090088 static int (*execIptables)(IptablesTarget target, ...);
89 static int (*execIptablesSilently)(IptablesTarget target, ...);
90 static int (*execIptablesRestore)(IptablesTarget target, const std::string& commands);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090091
Amith Yamasani390e4ea2015-04-25 19:08:57 -070092private:
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070093 FirewallType mFirewallType;
94 int attachChain(const char*, const char*);
95 int detachChain(const char*, const char*);
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090096 int createChain(const char*, FirewallType);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070097 FirewallType getFirewallType(ChildChain);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070098};
99
100#endif