blob: 909e765a55f3b73cfbacd005ae70f7657423721f [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>
21
Amith Yamasani390e4ea2015-04-25 19:08:57 -070022enum FirewallRule { DENY, ALLOW };
23
24// WHITELIST means the firewall denies all by default, uids must be explicitly ALLOWed
25// BLACKLIST means the firewall allows all by default, uids must be explicitly DENYed
26
27enum FirewallType { WHITELIST, BLACKLIST };
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070028
Felipe Leme3f624342016-02-10 18:12:39 -080029enum ChildChain { NONE, DOZABLE, STANDBY, POWERSAVE, INVALID_CHAIN };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070030
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070031#define PROTOCOL_TCP 6
32#define PROTOCOL_UDP 17
33
34/*
35 * Simple firewall that drops all packets except those matching explicitly
36 * defined ALLOW rules.
37 */
38class FirewallController {
39public:
40 FirewallController();
41
42 int setupIptablesHooks(void);
43
Amith Yamasani390e4ea2015-04-25 19:08:57 -070044 int enableFirewall(FirewallType);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070045 int disableFirewall(void);
46 int isFirewallEnabled(void);
47
48 /* Match traffic going in/out over the given iface. */
49 int setInterfaceRule(const char*, FirewallRule);
50 /* Match traffic coming-in-to or going-out-from given address. */
51 int setEgressSourceRule(const char*, FirewallRule);
52 /* Match traffic coming-in-from or going-out-to given address, port, and protocol. */
53 int setEgressDestRule(const char*, int, int, FirewallRule);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070054 /* Match traffic owned by given UID. This is specific to a particular chain. */
55 int setUidRule(ChildChain, int, FirewallRule);
56
57 int enableChildChains(ChildChain, bool);
58
59 static const char* TABLE;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070060
61 static const char* LOCAL_INPUT;
62 static const char* LOCAL_OUTPUT;
63 static const char* LOCAL_FORWARD;
64
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070065 static const char* LOCAL_DOZABLE;
66 static const char* LOCAL_STANDBY;
Felipe Leme3f624342016-02-10 18:12:39 -080067 static const char* LOCAL_POWERSAVE;
Lorenzo Colittic8683d72015-09-01 16:53:35 +090068
69 static const char* ICMPV6_TYPES[];
70
Amith Yamasani390e4ea2015-04-25 19:08:57 -070071private:
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070072 FirewallType mFirewallType;
73 int attachChain(const char*, const char*);
74 int detachChain(const char*, const char*);
75 int createChain(const char*, const char*, FirewallType);
76 FirewallType getFirewallType(ChildChain);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070077};
78
79#endif